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