1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Marek Vasut <marex@denx.de>
5 * DesignWare APB GPIO driver
10 #include <asm/arch/gpio.h>
14 #include <dm/device-internal.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #define GPIO_SWPORT_DR(p) (0x00 + (p) * 0xc)
23 #define GPIO_SWPORT_DDR(p) (0x04 + (p) * 0xc)
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
31 #define GPIO_EXT_PORT(p) (0x50 + (p) * 4)
33 struct gpio_dwapb_priv {
34 struct reset_ctl_bulk resets;
37 struct gpio_dwapb_platdata {
44 static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin)
46 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
48 clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
52 static int dwapb_gpio_direction_output(struct udevice *dev, unsigned pin,
55 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
57 setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
60 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
62 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
67 static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin)
69 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
70 return !!(readl(plat->base + GPIO_EXT_PORT(plat->bank)) & (1 << pin));
74 static int dwapb_gpio_set_value(struct udevice *dev, unsigned pin, int val)
76 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
79 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
81 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
86 static int dwapb_gpio_get_function(struct udevice *dev, unsigned offset)
88 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
91 gpio = readl(plat->base + GPIO_SWPORT_DDR(plat->bank));
93 if (gpio & BIT(offset))
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,
104 .get_function = dwapb_gpio_get_function,
107 static int gpio_dwapb_reset(struct udevice *dev)
110 struct gpio_dwapb_priv *priv = dev_get_priv(dev);
112 ret = reset_get_bulk(dev, &priv->resets);
114 /* Return 0 if error due to !CONFIG_DM_RESET and reset
115 * DT property is not present.
117 if (ret == -ENOENT || ret == -ENOTSUPP)
120 dev_warn(dev, "Can't get reset: %d\n", ret);
124 ret = reset_deassert_bulk(&priv->resets);
126 reset_release_bulk(&priv->resets);
127 dev_err(dev, "Failed to reset: %d\n", ret);
134 static int gpio_dwapb_probe(struct udevice *dev)
136 struct gpio_dev_priv *priv = dev_get_uclass_priv(dev);
137 struct gpio_dwapb_platdata *plat = dev->platdata;
140 /* Reset on parent device only */
141 return gpio_dwapb_reset(dev);
144 priv->gpio_count = plat->pins;
145 priv->bank_name = plat->name;
150 static int gpio_dwapb_bind(struct udevice *dev)
152 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
153 const void *blob = gd->fdt_blob;
154 struct udevice *subdev;
156 int ret, node, bank = 0;
158 /* If this is a child device, there is nothing to do here */
162 base = dev_read_addr(dev);
163 if (base == FDT_ADDR_T_NONE) {
164 debug("Can't get the GPIO register base address\n");
168 for (node = fdt_first_subnode(blob, dev_of_offset(dev));
170 node = fdt_next_subnode(blob, node)) {
171 if (!fdtdec_get_bool(blob, node, "gpio-controller"))
174 plat = devm_kcalloc(dev, 1, sizeof(*plat), GFP_KERNEL);
180 plat->pins = fdtdec_get_int(blob, node, "snps,nr-gpios", 0);
181 plat->name = fdt_stringlist_get(blob, node, "bank-name", 0,
185 * Fall back to node name. This means accessing pins
186 * via bank name won't work.
188 plat->name = fdt_get_name(blob, node, NULL);
191 ret = device_bind(dev, dev->driver, plat->name,
196 dev_set_of_offset(subdev, node);
203 static int gpio_dwapb_remove(struct udevice *dev)
205 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
206 struct gpio_dwapb_priv *priv = dev_get_priv(dev);
209 return reset_release_bulk(&priv->resets);
214 static const struct udevice_id gpio_dwapb_ids[] = {
215 { .compatible = "snps,dw-apb-gpio" },
219 U_BOOT_DRIVER(gpio_dwapb) = {
220 .name = "gpio-dwapb",
222 .of_match = gpio_dwapb_ids,
223 .ops = &gpio_dwapb_ops,
224 .bind = gpio_dwapb_bind,
225 .probe = gpio_dwapb_probe,
226 .remove = gpio_dwapb_remove,
227 .priv_auto_alloc_size = sizeof(struct gpio_dwapb_priv),