Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Marek Vasut <marex@denx.de> |
| 4 | * |
| 5 | * DesignWare APB GPIO driver |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <asm/arch/gpio.h> |
| 12 | #include <asm/gpio.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <dm.h> |
| 15 | #include <dm/device-internal.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <dm/devres.h> |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 18 | #include <dm/lists.h> |
| 19 | #include <dm/root.h> |
| 20 | #include <errno.h> |
Ley Foon Tan | db6a158 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 21 | #include <reset.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 22 | #include <linux/bitops.h> |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 23 | |
Phil Edworthy | 2d0c2c4 | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 24 | #define GPIO_SWPORT_DR(p) (0x00 + (p) * 0xc) |
| 25 | #define GPIO_SWPORT_DDR(p) (0x04 + (p) * 0xc) |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 26 | #define GPIO_INTEN 0x30 |
| 27 | #define GPIO_INTMASK 0x34 |
| 28 | #define GPIO_INTTYPE_LEVEL 0x38 |
| 29 | #define GPIO_INT_POLARITY 0x3c |
| 30 | #define GPIO_INTSTATUS 0x40 |
| 31 | #define GPIO_PORTA_DEBOUNCE 0x48 |
| 32 | #define GPIO_PORTA_EOI 0x4c |
Phil Edworthy | 2d0c2c4 | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 33 | #define GPIO_EXT_PORT(p) (0x50 + (p) * 4) |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 34 | |
Ley Foon Tan | db6a158 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 35 | struct gpio_dwapb_priv { |
| 36 | struct reset_ctl_bulk resets; |
| 37 | }; |
| 38 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 39 | struct gpio_dwapb_plat { |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 40 | const char *name; |
| 41 | int bank; |
| 42 | int pins; |
Sean Anderson | af551da | 2020-09-14 11:01:59 -0400 | [diff] [blame] | 43 | void __iomem *base; |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin) |
| 47 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 48 | struct gpio_dwapb_plat *plat = dev_get_plat(dev); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 49 | |
Phil Edworthy | 2d0c2c4 | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 50 | clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int dwapb_gpio_direction_output(struct udevice *dev, unsigned pin, |
| 55 | int val) |
| 56 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 57 | struct gpio_dwapb_plat *plat = dev_get_plat(dev); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 58 | |
Phil Edworthy | 2d0c2c4 | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 59 | setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 60 | |
| 61 | if (val) |
Phil Edworthy | 2d0c2c4 | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 62 | setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 63 | else |
Phil Edworthy | 2d0c2c4 | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 64 | clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 69 | static int dwapb_gpio_set_value(struct udevice *dev, unsigned pin, int val) |
| 70 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 71 | struct gpio_dwapb_plat *plat = dev_get_plat(dev); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 72 | |
| 73 | if (val) |
Phil Edworthy | 2d0c2c4 | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 74 | setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 75 | else |
Phil Edworthy | 2d0c2c4 | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 76 | clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Ley Foon Tan | 71f2700 | 2018-08-16 13:46:30 +0800 | [diff] [blame] | 81 | static int dwapb_gpio_get_function(struct udevice *dev, unsigned offset) |
| 82 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 83 | struct gpio_dwapb_plat *plat = dev_get_plat(dev); |
Ley Foon Tan | 71f2700 | 2018-08-16 13:46:30 +0800 | [diff] [blame] | 84 | u32 gpio; |
| 85 | |
| 86 | gpio = readl(plat->base + GPIO_SWPORT_DDR(plat->bank)); |
| 87 | |
| 88 | if (gpio & BIT(offset)) |
| 89 | return GPIOF_OUTPUT; |
| 90 | else |
| 91 | return GPIOF_INPUT; |
| 92 | } |
| 93 | |
Sean Anderson | b4167aa | 2020-09-14 11:02:01 -0400 | [diff] [blame] | 94 | static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin) |
| 95 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 96 | struct gpio_dwapb_plat *plat = dev_get_plat(dev); |
Sean Anderson | b4167aa | 2020-09-14 11:02:01 -0400 | [diff] [blame] | 97 | u32 value; |
| 98 | |
| 99 | if (dwapb_gpio_get_function(dev, pin) == GPIOF_OUTPUT) |
| 100 | value = readl(plat->base + GPIO_SWPORT_DR(plat->bank)); |
| 101 | else |
| 102 | value = readl(plat->base + GPIO_EXT_PORT(plat->bank)); |
| 103 | return !!(value & BIT(pin)); |
| 104 | } |
| 105 | |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 106 | static const struct dm_gpio_ops gpio_dwapb_ops = { |
| 107 | .direction_input = dwapb_gpio_direction_input, |
| 108 | .direction_output = dwapb_gpio_direction_output, |
| 109 | .get_value = dwapb_gpio_get_value, |
| 110 | .set_value = dwapb_gpio_set_value, |
Ley Foon Tan | 71f2700 | 2018-08-16 13:46:30 +0800 | [diff] [blame] | 111 | .get_function = dwapb_gpio_get_function, |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
Ley Foon Tan | db6a158 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 114 | static int gpio_dwapb_reset(struct udevice *dev) |
| 115 | { |
| 116 | int ret; |
| 117 | struct gpio_dwapb_priv *priv = dev_get_priv(dev); |
| 118 | |
| 119 | ret = reset_get_bulk(dev, &priv->resets); |
| 120 | if (ret) { |
| 121 | /* Return 0 if error due to !CONFIG_DM_RESET and reset |
| 122 | * DT property is not present. |
| 123 | */ |
| 124 | if (ret == -ENOENT || ret == -ENOTSUPP) |
| 125 | return 0; |
| 126 | |
| 127 | dev_warn(dev, "Can't get reset: %d\n", ret); |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | ret = reset_deassert_bulk(&priv->resets); |
| 132 | if (ret) { |
| 133 | reset_release_bulk(&priv->resets); |
| 134 | dev_err(dev, "Failed to reset: %d\n", ret); |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 141 | static int gpio_dwapb_probe(struct udevice *dev) |
| 142 | { |
| 143 | struct gpio_dev_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 144 | struct gpio_dwapb_plat *plat = dev_get_plat(dev); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 145 | |
Ley Foon Tan | db6a158 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 146 | if (!plat) { |
| 147 | /* Reset on parent device only */ |
| 148 | return gpio_dwapb_reset(dev); |
| 149 | } |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 150 | |
| 151 | priv->gpio_count = plat->pins; |
| 152 | priv->bank_name = plat->name; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int gpio_dwapb_bind(struct udevice *dev) |
| 158 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 159 | struct gpio_dwapb_plat *plat = dev_get_plat(dev); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 160 | struct udevice *subdev; |
| 161 | fdt_addr_t base; |
Simon Goldschmidt | 1b898ff | 2019-05-09 22:12:00 +0200 | [diff] [blame] | 162 | int ret, bank = 0; |
| 163 | ofnode node; |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 164 | |
| 165 | /* If this is a child device, there is nothing to do here */ |
| 166 | if (plat) |
| 167 | return 0; |
| 168 | |
Ley Foon Tan | 9ea3544 | 2018-08-16 02:05:54 +0800 | [diff] [blame] | 169 | base = dev_read_addr(dev); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 170 | if (base == FDT_ADDR_T_NONE) { |
| 171 | debug("Can't get the GPIO register base address\n"); |
| 172 | return -ENXIO; |
| 173 | } |
| 174 | |
Simon Goldschmidt | 1b898ff | 2019-05-09 22:12:00 +0200 | [diff] [blame] | 175 | for (node = dev_read_first_subnode(dev); ofnode_valid(node); |
| 176 | node = dev_read_next_subnode(node)) { |
| 177 | if (!ofnode_read_bool(node, "gpio-controller")) |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 178 | continue; |
| 179 | |
Ley Foon Tan | 86d56a5 | 2018-09-19 16:26:33 +0800 | [diff] [blame] | 180 | plat = devm_kcalloc(dev, 1, sizeof(*plat), GFP_KERNEL); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 181 | if (!plat) |
| 182 | return -ENOMEM; |
| 183 | |
Sean Anderson | af551da | 2020-09-14 11:01:59 -0400 | [diff] [blame] | 184 | plat->base = (void *)base; |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 185 | plat->bank = bank; |
Simon Goldschmidt | 1b898ff | 2019-05-09 22:12:00 +0200 | [diff] [blame] | 186 | plat->pins = ofnode_read_u32_default(node, "snps,nr-gpios", 0); |
| 187 | |
| 188 | if (ofnode_read_string_index(node, "bank-name", 0, |
| 189 | &plat->name)) { |
Simon Goldschmidt | 89f1fe5 | 2018-11-02 11:54:50 +0100 | [diff] [blame] | 190 | /* |
| 191 | * Fall back to node name. This means accessing pins |
| 192 | * via bank name won't work. |
| 193 | */ |
Sean Anderson | 18dbb7b | 2020-09-14 11:02:00 -0400 | [diff] [blame] | 194 | char name[32]; |
| 195 | |
| 196 | snprintf(name, sizeof(name), "%s_", |
| 197 | ofnode_get_name(node)); |
| 198 | plat->name = strdup(name); |
| 199 | if (!plat->name) { |
| 200 | kfree(plat); |
| 201 | return -ENOMEM; |
| 202 | } |
Simon Goldschmidt | 89f1fe5 | 2018-11-02 11:54:50 +0100 | [diff] [blame] | 203 | } |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 204 | |
Simon Glass | 734206d | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 205 | ret = device_bind(dev, dev->driver, plat->name, plat, node, |
| 206 | &subdev); |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 207 | if (ret) |
Ley Foon Tan | 86d56a5 | 2018-09-19 16:26:33 +0800 | [diff] [blame] | 208 | return ret; |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 209 | |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 210 | bank++; |
| 211 | } |
| 212 | |
| 213 | return 0; |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Ley Foon Tan | db6a158 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 216 | static int gpio_dwapb_remove(struct udevice *dev) |
| 217 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 218 | struct gpio_dwapb_plat *plat = dev_get_plat(dev); |
Ley Foon Tan | db6a158 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 219 | struct gpio_dwapb_priv *priv = dev_get_priv(dev); |
| 220 | |
| 221 | if (!plat && priv) |
| 222 | return reset_release_bulk(&priv->resets); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 227 | static const struct udevice_id gpio_dwapb_ids[] = { |
| 228 | { .compatible = "snps,dw-apb-gpio" }, |
| 229 | { } |
| 230 | }; |
| 231 | |
| 232 | U_BOOT_DRIVER(gpio_dwapb) = { |
| 233 | .name = "gpio-dwapb", |
| 234 | .id = UCLASS_GPIO, |
| 235 | .of_match = gpio_dwapb_ids, |
| 236 | .ops = &gpio_dwapb_ops, |
| 237 | .bind = gpio_dwapb_bind, |
| 238 | .probe = gpio_dwapb_probe, |
Ley Foon Tan | db6a158 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 239 | .remove = gpio_dwapb_remove, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 240 | .priv_auto = sizeof(struct gpio_dwapb_priv), |
Marek Vasut | e30a70c | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 241 | }; |