blob: 7b50a34b3f9a1193104937970aea67e204014690 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut12a8d792015-06-23 15:54:19 +02002/*
3 * (C) Copyright 2015 Marek Vasut <marex@denx.de>
4 *
5 * DesignWare APB GPIO driver
Marek Vasut12a8d792015-06-23 15:54:19 +02006 */
7
8#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Marek Vasut12a8d792015-06-23 15:54:19 +020010#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 Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <dm/devres.h>
Marek Vasut12a8d792015-06-23 15:54:19 +020018#include <dm/lists.h>
19#include <dm/root.h>
20#include <errno.h>
Ley Foon Tana804dd02018-09-04 14:04:58 +080021#include <reset.h>
Marek Vasut12a8d792015-06-23 15:54:19 +020022
Phil Edworthy26c43ca2016-11-03 11:05:12 +000023#define GPIO_SWPORT_DR(p) (0x00 + (p) * 0xc)
24#define GPIO_SWPORT_DDR(p) (0x04 + (p) * 0xc)
Marek Vasut12a8d792015-06-23 15:54:19 +020025#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 Edworthy26c43ca2016-11-03 11:05:12 +000032#define GPIO_EXT_PORT(p) (0x50 + (p) * 4)
Marek Vasut12a8d792015-06-23 15:54:19 +020033
Ley Foon Tana804dd02018-09-04 14:04:58 +080034struct gpio_dwapb_priv {
35 struct reset_ctl_bulk resets;
36};
37
Marek Vasut12a8d792015-06-23 15:54:19 +020038struct gpio_dwapb_platdata {
39 const char *name;
40 int bank;
41 int pins;
42 fdt_addr_t base;
43};
44
45static int dwapb_gpio_direction_input(struct udevice *dev, unsigned pin)
46{
47 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
48
Phil Edworthy26c43ca2016-11-03 11:05:12 +000049 clrbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
Marek Vasut12a8d792015-06-23 15:54:19 +020050 return 0;
51}
52
53static 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 Edworthy26c43ca2016-11-03 11:05:12 +000058 setbits_le32(plat->base + GPIO_SWPORT_DDR(plat->bank), 1 << pin);
Marek Vasut12a8d792015-06-23 15:54:19 +020059
60 if (val)
Phil Edworthy26c43ca2016-11-03 11:05:12 +000061 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
Marek Vasut12a8d792015-06-23 15:54:19 +020062 else
Phil Edworthy26c43ca2016-11-03 11:05:12 +000063 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
Marek Vasut12a8d792015-06-23 15:54:19 +020064
65 return 0;
66}
67
68static int dwapb_gpio_get_value(struct udevice *dev, unsigned pin)
69{
70 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
Phil Edworthy26c43ca2016-11-03 11:05:12 +000071 return !!(readl(plat->base + GPIO_EXT_PORT(plat->bank)) & (1 << pin));
Marek Vasut12a8d792015-06-23 15:54:19 +020072}
73
74
75static 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 Edworthy26c43ca2016-11-03 11:05:12 +000080 setbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
Marek Vasut12a8d792015-06-23 15:54:19 +020081 else
Phil Edworthy26c43ca2016-11-03 11:05:12 +000082 clrbits_le32(plat->base + GPIO_SWPORT_DR(plat->bank), 1 << pin);
Marek Vasut12a8d792015-06-23 15:54:19 +020083
84 return 0;
85}
86
Ley Foon Tana4f80b72018-08-16 13:46:30 +080087static 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 Vasut12a8d792015-06-23 15:54:19 +0200100static 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 Tana4f80b72018-08-16 13:46:30 +0800105 .get_function = dwapb_gpio_get_function,
Marek Vasut12a8d792015-06-23 15:54:19 +0200106};
107
Ley Foon Tana804dd02018-09-04 14:04:58 +0800108static 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 Vasut12a8d792015-06-23 15:54:19 +0200135static 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 Tana804dd02018-09-04 14:04:58 +0800140 if (!plat) {
141 /* Reset on parent device only */
142 return gpio_dwapb_reset(dev);
143 }
Marek Vasut12a8d792015-06-23 15:54:19 +0200144
145 priv->gpio_count = plat->pins;
146 priv->bank_name = plat->name;
147
148 return 0;
149}
150
151static int gpio_dwapb_bind(struct udevice *dev)
152{
153 struct gpio_dwapb_platdata *plat = dev_get_platdata(dev);
Marek Vasut12a8d792015-06-23 15:54:19 +0200154 struct udevice *subdev;
155 fdt_addr_t base;
Simon Goldschmidt3aa78972019-05-09 22:12:00 +0200156 int ret, bank = 0;
157 ofnode node;
Marek Vasut12a8d792015-06-23 15:54:19 +0200158
159 /* If this is a child device, there is nothing to do here */
160 if (plat)
161 return 0;
162
Ley Foon Tan53bdf4e2018-08-16 02:05:54 +0800163 base = dev_read_addr(dev);
Marek Vasut12a8d792015-06-23 15:54:19 +0200164 if (base == FDT_ADDR_T_NONE) {
165 debug("Can't get the GPIO register base address\n");
166 return -ENXIO;
167 }
168
Simon Goldschmidt3aa78972019-05-09 22:12:00 +0200169 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 Vasut12a8d792015-06-23 15:54:19 +0200172 continue;
173
Ley Foon Tan16a48f52018-09-19 16:26:33 +0800174 plat = devm_kcalloc(dev, 1, sizeof(*plat), GFP_KERNEL);
Marek Vasut12a8d792015-06-23 15:54:19 +0200175 if (!plat)
176 return -ENOMEM;
177
178 plat->base = base;
179 plat->bank = bank;
Simon Goldschmidt3aa78972019-05-09 22:12:00 +0200180 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 Goldschmidt6a54b8a2018-11-02 11:54:50 +0100184 /*
185 * Fall back to node name. This means accessing pins
186 * via bank name won't work.
187 */
Simon Goldschmidt3aa78972019-05-09 22:12:00 +0200188 plat->name = ofnode_get_name(node);
Simon Goldschmidt6a54b8a2018-11-02 11:54:50 +0100189 }
Marek Vasut12a8d792015-06-23 15:54:19 +0200190
Simon Goldschmidt2ed8ea02019-05-21 22:03:12 +0200191 ret = device_bind_ofnode(dev, dev->driver, plat->name,
192 plat, node, &subdev);
Marek Vasut12a8d792015-06-23 15:54:19 +0200193 if (ret)
Ley Foon Tan16a48f52018-09-19 16:26:33 +0800194 return ret;
Marek Vasut12a8d792015-06-23 15:54:19 +0200195
Marek Vasut12a8d792015-06-23 15:54:19 +0200196 bank++;
197 }
198
199 return 0;
Marek Vasut12a8d792015-06-23 15:54:19 +0200200}
201
Ley Foon Tana804dd02018-09-04 14:04:58 +0800202static 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 Vasut12a8d792015-06-23 15:54:19 +0200213static const struct udevice_id gpio_dwapb_ids[] = {
214 { .compatible = "snps,dw-apb-gpio" },
215 { }
216};
217
218U_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 Tana804dd02018-09-04 14:04:58 +0800225 .remove = gpio_dwapb_remove,
226 .priv_auto_alloc_size = sizeof(struct gpio_dwapb_priv),
Marek Vasut12a8d792015-06-23 15:54:19 +0200227};