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 | |
| 8 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Marek Vasut | 12a8d79 | 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 | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <dm/devres.h> |
Marek Vasut | 12a8d79 | 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 | a804dd0 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 21 | #include <reset.h> |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 22 | |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 23 | #define GPIO_SWPORT_DR(p) (0x00 + (p) * 0xc) |
| 24 | #define GPIO_SWPORT_DDR(p) (0x04 + (p) * 0xc) |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 25 | #define GPIO_INTEN 0x30 |
| 26 | #define GPIO_INTMASK 0x34 |
| 27 | #define GPIO_INTTYPE_LEVEL 0x38 |
| 28 | #define GPIO_INT_POLARITY 0x3c |
| 29 | #define GPIO_INTSTATUS 0x40 |
| 30 | #define GPIO_PORTA_DEBOUNCE 0x48 |
| 31 | #define GPIO_PORTA_EOI 0x4c |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 32 | #define GPIO_EXT_PORT(p) (0x50 + (p) * 4) |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 33 | |
Ley Foon Tan | a804dd0 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 34 | struct gpio_dwapb_priv { |
| 35 | struct reset_ctl_bulk resets; |
| 36 | }; |
| 37 | |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 38 | struct gpio_dwapb_platdata { |
| 39 | const char *name; |
| 40 | int bank; |
| 41 | int pins; |
| 42 | fdt_addr_t base; |
| 43 | }; |
| 44 | |
| 45 | static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin) |
| 46 | { |
| 47 | struct gpio_dwapb_platdata *plat = dev_get_platdata(dev); |
| 48 | |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 49 | clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin); |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int dwapb_gpio_direction_output(struct udevice *dev, unsigned pin, |
| 54 | int val) |
| 55 | { |
| 56 | struct gpio_dwapb_platdata *plat = dev_get_platdata(dev); |
| 57 | |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 58 | setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin); |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 59 | |
| 60 | if (val) |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 61 | setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin); |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 62 | else |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 63 | clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin); |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin) |
| 69 | { |
| 70 | struct gpio_dwapb_platdata *plat = dev_get_platdata(dev); |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 71 | return !!(readl(plat->base + GPIO_EXT_PORT(plat->bank)) & (1 << pin)); |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
| 75 | static int dwapb_gpio_set_value(struct udevice *dev, unsigned pin, int val) |
| 76 | { |
| 77 | struct gpio_dwapb_platdata *plat = dev_get_platdata(dev); |
| 78 | |
| 79 | if (val) |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 80 | setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin); |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 81 | else |
Phil Edworthy | 26c43ca | 2016-11-03 11:05:12 +0000 | [diff] [blame] | 82 | clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin); |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Ley Foon Tan | a4f80b7 | 2018-08-16 13:46:30 +0800 | [diff] [blame] | 87 | static int dwapb_gpio_get_function(struct udevice *dev, unsigned offset) |
| 88 | { |
| 89 | struct gpio_dwapb_platdata *plat = dev_get_platdata(dev); |
| 90 | u32 gpio; |
| 91 | |
| 92 | gpio = readl(plat->base + GPIO_SWPORT_DDR(plat->bank)); |
| 93 | |
| 94 | if (gpio & BIT(offset)) |
| 95 | return GPIOF_OUTPUT; |
| 96 | else |
| 97 | return GPIOF_INPUT; |
| 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); |
| 138 | struct gpio_dwapb_platdata *plat = dev->platdata; |
| 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 | { |
| 153 | struct gpio_dwapb_platdata *plat = dev_get_platdata(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 | |
| 178 | plat->base = base; |
| 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 | */ |
Simon Goldschmidt | 3aa7897 | 2019-05-09 22:12:00 +0200 | [diff] [blame] | 188 | plat->name = ofnode_get_name(node); |
Simon Goldschmidt | 6a54b8a | 2018-11-02 11:54:50 +0100 | [diff] [blame] | 189 | } |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 190 | |
Simon Goldschmidt | 2ed8ea0 | 2019-05-21 22:03:12 +0200 | [diff] [blame] | 191 | ret = device_bind_ofnode(dev, dev->driver, plat->name, |
| 192 | plat, node, &subdev); |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 193 | if (ret) |
Ley Foon Tan | 16a48f5 | 2018-09-19 16:26:33 +0800 | [diff] [blame] | 194 | return ret; |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 195 | |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 196 | bank++; |
| 197 | } |
| 198 | |
| 199 | return 0; |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Ley Foon Tan | a804dd0 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 202 | static int gpio_dwapb_remove(struct udevice *dev) |
| 203 | { |
| 204 | struct gpio_dwapb_platdata *plat = dev_get_platdata(dev); |
| 205 | struct gpio_dwapb_priv *priv = dev_get_priv(dev); |
| 206 | |
| 207 | if (!plat && priv) |
| 208 | return reset_release_bulk(&priv->resets); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 213 | static const struct udevice_id gpio_dwapb_ids[] = { |
| 214 | { .compatible = "snps,dw-apb-gpio" }, |
| 215 | { } |
| 216 | }; |
| 217 | |
| 218 | U_BOOT_DRIVER(gpio_dwapb) = { |
| 219 | .name = "gpio-dwapb", |
| 220 | .id = UCLASS_GPIO, |
| 221 | .of_match = gpio_dwapb_ids, |
| 222 | .ops = &gpio_dwapb_ops, |
| 223 | .bind = gpio_dwapb_bind, |
| 224 | .probe = gpio_dwapb_probe, |
Ley Foon Tan | a804dd0 | 2018-09-04 14:04:58 +0800 | [diff] [blame] | 225 | .remove = gpio_dwapb_remove, |
| 226 | .priv_auto_alloc_size = sizeof(struct gpio_dwapb_priv), |
Marek Vasut | 12a8d79 | 2015-06-23 15:54:19 +0200 | [diff] [blame] | 227 | }; |