mtd: spinand: toshiba: Support for new Kioxia Serial NAND
[oweals/u-boot.git] / drivers / gpio / dwapb_gpio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Marek Vasut <marex@denx.de>
4  *
5  * DesignWare APB GPIO driver
6  */
7
8 #include <common.h>
9 #include <log.h>
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>
16 #include <dm/device_compat.h>
17 #include <dm/devres.h>
18 #include <dm/lists.h>
19 #include <dm/root.h>
20 #include <errno.h>
21 #include <reset.h>
22 #include <linux/bitops.h>
23
24 #define GPIO_SWPORT_DR(p)       (0x00 + (p) * 0xc)
25 #define GPIO_SWPORT_DDR(p)      (0x04 + (p) * 0xc)
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
33 #define GPIO_EXT_PORT(p)        (0x50 + (p) * 4)
34
35 struct gpio_dwapb_priv {
36         struct reset_ctl_bulk   resets;
37 };
38
39 struct gpio_dwapb_platdata {
40         const char      *name;
41         int             bank;
42         int             pins;
43         fdt_addr_t      base;
44 };
45
46 static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin)
47 {
48         struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
49
50         clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
51         return 0;
52 }
53
54 static int dwapb_gpio_direction_output(struct udevice *dev, unsigned pin,
55                                      int val)
56 {
57         struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
58
59         setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
60
61         if (val)
62                 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
63         else
64                 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
65
66         return 0;
67 }
68
69 static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin)
70 {
71         struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
72         return !!(readl(plat->base + GPIO_EXT_PORT(plat->bank)) & (1 << pin));
73 }
74
75
76 static int dwapb_gpio_set_value(struct udevice *dev, unsigned pin, int val)
77 {
78         struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
79
80         if (val)
81                 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
82         else
83                 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
84
85         return 0;
86 }
87
88 static int dwapb_gpio_get_function(struct udevice *dev, unsigned offset)
89 {
90         struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
91         u32 gpio;
92
93         gpio = readl(plat->base + GPIO_SWPORT_DDR(plat->bank));
94
95         if (gpio & BIT(offset))
96                 return GPIOF_OUTPUT;
97         else
98                 return GPIOF_INPUT;
99 }
100
101 static const struct dm_gpio_ops gpio_dwapb_ops = {
102         .direction_input        = dwapb_gpio_direction_input,
103         .direction_output       = dwapb_gpio_direction_output,
104         .get_value              = dwapb_gpio_get_value,
105         .set_value              = dwapb_gpio_set_value,
106         .get_function           = dwapb_gpio_get_function,
107 };
108
109 static int gpio_dwapb_reset(struct udevice *dev)
110 {
111         int ret;
112         struct gpio_dwapb_priv *priv = dev_get_priv(dev);
113
114         ret = reset_get_bulk(dev, &priv->resets);
115         if (ret) {
116                 /* Return 0 if error due to !CONFIG_DM_RESET and reset
117                  * DT property is not present.
118                  */
119                 if (ret == -ENOENT || ret == -ENOTSUPP)
120                         return 0;
121
122                 dev_warn(dev, "Can't get reset: %d\n", ret);
123                 return ret;
124         }
125
126         ret = reset_deassert_bulk(&priv->resets);
127         if (ret) {
128                 reset_release_bulk(&priv->resets);
129                 dev_err(dev, "Failed to reset: %d\n", ret);
130                 return ret;
131         }
132
133         return 0;
134 }
135
136 static int gpio_dwapb_probe(struct udevice *dev)
137 {
138         struct gpio_dev_priv *priv = dev_get_uclass_priv(dev);
139         struct gpio_dwapb_platdata *plat = dev->platdata;
140
141         if (!plat) {
142                 /* Reset on parent device only */
143                 return gpio_dwapb_reset(dev);
144         }
145
146         priv->gpio_count = plat->pins;
147         priv->bank_name = plat->name;
148
149         return 0;
150 }
151
152 static int gpio_dwapb_bind(struct udevice *dev)
153 {
154         struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
155         struct udevice *subdev;
156         fdt_addr_t base;
157         int ret, bank = 0;
158         ofnode node;
159
160         /* If this is a child device, there is nothing to do here */
161         if (plat)
162                 return 0;
163
164         base = dev_read_addr(dev);
165         if (base == FDT_ADDR_T_NONE) {
166                 debug("Can't get the GPIO register base address\n");
167                 return -ENXIO;
168         }
169
170         for (node = dev_read_first_subnode(dev); ofnode_valid(node);
171              node = dev_read_next_subnode(node)) {
172                 if (!ofnode_read_bool(node, "gpio-controller"))
173                         continue;
174
175                 plat = devm_kcalloc(dev, 1, sizeof(*plat), GFP_KERNEL);
176                 if (!plat)
177                         return -ENOMEM;
178
179                 plat->base = base;
180                 plat->bank = bank;
181                 plat->pins = ofnode_read_u32_default(node, "snps,nr-gpios", 0);
182
183                 if (ofnode_read_string_index(node, "bank-name", 0,
184                                              &plat->name)) {
185                         /*
186                          * Fall back to node name. This means accessing pins
187                          * via bank name won't work.
188                          */
189                         plat->name = ofnode_get_name(node);
190                 }
191
192                 ret = device_bind_ofnode(dev, dev->driver, plat->name,
193                                          plat, node, &subdev);
194                 if (ret)
195                         return ret;
196
197                 bank++;
198         }
199
200         return 0;
201 }
202
203 static int gpio_dwapb_remove(struct udevice *dev)
204 {
205         struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
206         struct gpio_dwapb_priv *priv = dev_get_priv(dev);
207
208         if (!plat && priv)
209                 return reset_release_bulk(&priv->resets);
210
211         return 0;
212 }
213
214 static const struct udevice_id gpio_dwapb_ids[] = {
215         { .compatible = "snps,dw-apb-gpio" },
216         { }
217 };
218
219 U_BOOT_DRIVER(gpio_dwapb) = {
220         .name           = "gpio-dwapb",
221         .id             = UCLASS_GPIO,
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),
228 };