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