blob: e335496581ff918080c367f9d9a226dbfe8a40a3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbellaf471472014-06-05 19:00:15 +01002/*
3 * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net>
4 *
5 * Based on earlier arch/arm/cpu/armv7/sunxi/gpio.c:
6 *
7 * (C) Copyright 2007-2011
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9 * Tom Cubie <tangliang@allwinnertech.com>
Ian Campbellaf471472014-06-05 19:00:15 +010010 */
11
12#include <common.h>
Simon Glass78304532014-10-30 20:25:49 -060013#include <dm.h>
14#include <errno.h>
15#include <fdtdec.h>
16#include <malloc.h>
Ian Campbellaf471472014-06-05 19:00:15 +010017#include <asm/io.h>
18#include <asm/gpio.h>
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +080019#include <dt-bindings/gpio/gpio.h>
Andre Przywaraf944a612022-09-06 10:36:38 +010020#include <sunxi_gpio.h>
Ian Campbellaf471472014-06-05 19:00:15 +010021
Andre Przywara82d307c2022-09-06 10:36:38 +010022/*
23 * =======================================================================
24 * Low level GPIO/pin controller access functions, to be shared by non-DM
25 * SPL code and the DM pinctrl/GPIO drivers.
26 * The functions ending in "bank" take a base pointer to a GPIO bank, and
27 * the pin offset is relative to that bank.
28 * The functions without "bank" in their name take a linear GPIO number,
29 * covering all ports, and starting at 0 for PortA.
30 * =======================================================================
31 */
32
Andre Przywara82d307c2022-09-06 10:36:38 +010033#define GPIO_BANK(pin) ((pin) >> 5)
34#define GPIO_NUM(pin) ((pin) & 0x1f)
35
Andre Przywara841ebfb32022-09-05 18:12:39 +010036#define GPIO_CFG_REG_OFFSET 0x00
Andre Przywara82d307c2022-09-06 10:36:38 +010037#define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3)
38#define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2)
39
Andre Przywara841ebfb32022-09-05 18:12:39 +010040#define GPIO_DAT_REG_OFFSET 0x10
41
42#define GPIO_DRV_REG_OFFSET 0x14
Andre Przywara82d307c2022-09-06 10:36:38 +010043#define GPIO_DRV_INDEX(pin) (((pin) & 0x1f) >> 4)
44#define GPIO_DRV_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
45
Andre Przywara841ebfb32022-09-05 18:12:39 +010046#define GPIO_PULL_REG_OFFSET 0x1c
Andre Przywara82d307c2022-09-06 10:36:38 +010047#define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4)
48#define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
49
Andre Przywara841ebfb32022-09-05 18:12:39 +010050static void* BANK_TO_GPIO(int bank)
51{
52 void *pio_base;
53
54 if (bank < SUNXI_GPIO_L) {
55 pio_base = (void *)(uintptr_t)SUNXI_PIO_BASE;
56 } else {
57 pio_base = (void *)(uintptr_t)SUNXI_R_PIO_BASE;
58 bank -= SUNXI_GPIO_L;
59 }
60
61 return pio_base + bank * SUNXI_PINCTRL_BANK_SIZE;
62}
63
64void sunxi_gpio_set_cfgbank(void *bank_base, int pin_offset, u32 val)
Andre Przywara82d307c2022-09-06 10:36:38 +010065{
Andre Przywara841ebfb32022-09-05 18:12:39 +010066 u32 index = GPIO_CFG_INDEX(pin_offset);
67 u32 offset = GPIO_CFG_OFFSET(pin_offset);
Andre Przywara82d307c2022-09-06 10:36:38 +010068
Andre Przywara841ebfb32022-09-05 18:12:39 +010069 clrsetbits_le32(bank_base + GPIO_CFG_REG_OFFSET + index * 4,
70 0xfU << offset, val << offset);
Andre Przywara82d307c2022-09-06 10:36:38 +010071}
72
73void sunxi_gpio_set_cfgpin(u32 pin, u32 val)
74{
75 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +010076 void *pio = BANK_TO_GPIO(bank);
Andre Przywara82d307c2022-09-06 10:36:38 +010077
Andre Przywara841ebfb32022-09-05 18:12:39 +010078 sunxi_gpio_set_cfgbank(pio, GPIO_NUM(pin), val);
Andre Przywara82d307c2022-09-06 10:36:38 +010079}
80
Andre Przywara841ebfb32022-09-05 18:12:39 +010081int sunxi_gpio_get_cfgbank(void *bank_base, int pin_offset)
Andre Przywara82d307c2022-09-06 10:36:38 +010082{
Andre Przywara841ebfb32022-09-05 18:12:39 +010083 u32 index = GPIO_CFG_INDEX(pin_offset);
84 u32 offset = GPIO_CFG_OFFSET(pin_offset);
Andre Przywara82d307c2022-09-06 10:36:38 +010085 u32 cfg;
86
Andre Przywara841ebfb32022-09-05 18:12:39 +010087 cfg = readl(bank_base + GPIO_CFG_REG_OFFSET + index * 4);
Andre Przywara82d307c2022-09-06 10:36:38 +010088 cfg >>= offset;
89
90 return cfg & 0xf;
91}
92
93int sunxi_gpio_get_cfgpin(u32 pin)
94{
95 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +010096 void *bank_base = BANK_TO_GPIO(bank);
Andre Przywara82d307c2022-09-06 10:36:38 +010097
Andre Przywara841ebfb32022-09-05 18:12:39 +010098 return sunxi_gpio_get_cfgbank(bank_base, GPIO_NUM(pin));
Andre Przywara82d307c2022-09-06 10:36:38 +010099}
100
Andre Przywara841ebfb32022-09-05 18:12:39 +0100101static void sunxi_gpio_set_value_bank(void *bank_base, int pin, bool set)
Andre Przywara6e632102022-09-06 10:07:18 +0100102{
103 u32 mask = 1U << pin;
104
Andre Przywara841ebfb32022-09-05 18:12:39 +0100105 clrsetbits_le32(bank_base + GPIO_DAT_REG_OFFSET,
106 set ? 0 : mask, set ? mask : 0);
Andre Przywara6e632102022-09-06 10:07:18 +0100107}
108
Andre Przywara841ebfb32022-09-05 18:12:39 +0100109static int sunxi_gpio_get_value_bank(void *bank_base, int pin)
Andre Przywara6e632102022-09-06 10:07:18 +0100110{
Andre Przywara841ebfb32022-09-05 18:12:39 +0100111 return !!(readl(bank_base + GPIO_DAT_REG_OFFSET) & (1U << pin));
Andre Przywara6e632102022-09-06 10:07:18 +0100112}
113
Andre Przywara82d307c2022-09-06 10:36:38 +0100114void sunxi_gpio_set_drv(u32 pin, u32 val)
115{
116 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100117 void *bank_base = BANK_TO_GPIO(bank);
Andre Przywara82d307c2022-09-06 10:36:38 +0100118
Andre Przywara841ebfb32022-09-05 18:12:39 +0100119 sunxi_gpio_set_drv_bank(bank_base, GPIO_NUM(pin), val);
Andre Przywara82d307c2022-09-06 10:36:38 +0100120}
121
Andre Przywara841ebfb32022-09-05 18:12:39 +0100122void sunxi_gpio_set_drv_bank(void *bank_base, u32 pin_offset, u32 val)
Andre Przywara82d307c2022-09-06 10:36:38 +0100123{
Andre Przywara841ebfb32022-09-05 18:12:39 +0100124 u32 index = GPIO_DRV_INDEX(pin_offset);
125 u32 offset = GPIO_DRV_OFFSET(pin_offset);
Andre Przywara82d307c2022-09-06 10:36:38 +0100126
Andre Przywara841ebfb32022-09-05 18:12:39 +0100127 clrsetbits_le32(bank_base + GPIO_DRV_REG_OFFSET + index * 4,
128 0x3U << offset, val << offset);
Andre Przywara82d307c2022-09-06 10:36:38 +0100129}
130
131void sunxi_gpio_set_pull(u32 pin, u32 val)
132{
133 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100134 void *bank_base = BANK_TO_GPIO(bank);
Andre Przywara82d307c2022-09-06 10:36:38 +0100135
Andre Przywara841ebfb32022-09-05 18:12:39 +0100136 sunxi_gpio_set_pull_bank(bank_base, GPIO_NUM(pin), val);
Andre Przywara82d307c2022-09-06 10:36:38 +0100137}
138
Andre Przywara841ebfb32022-09-05 18:12:39 +0100139void sunxi_gpio_set_pull_bank(void *bank_base, int pin_offset, u32 val)
Andre Przywara82d307c2022-09-06 10:36:38 +0100140{
Andre Przywara841ebfb32022-09-05 18:12:39 +0100141 u32 index = GPIO_PULL_INDEX(pin_offset);
142 u32 offset = GPIO_PULL_OFFSET(pin_offset);
Andre Przywara82d307c2022-09-06 10:36:38 +0100143
Andre Przywara841ebfb32022-09-05 18:12:39 +0100144 clrsetbits_le32(bank_base + GPIO_PULL_REG_OFFSET + index * 4,
145 0x3U << offset, val << offset);
Andre Przywara82d307c2022-09-06 10:36:38 +0100146}
147
148
149/* =========== Non-DM code, used by the SPL. ============ */
150
Simon Glassfa4689a2019-12-06 21:41:35 -0700151#if !CONFIG_IS_ENABLED(DM_GPIO)
Andre Przywara6e632102022-09-06 10:07:18 +0100152static void sunxi_gpio_set_value(u32 pin, bool set)
Ian Campbellaf471472014-06-05 19:00:15 +0100153{
Ian Campbellaf471472014-06-05 19:00:15 +0100154 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100155 void *pio = BANK_TO_GPIO(bank);
Ian Campbellaf471472014-06-05 19:00:15 +0100156
Andre Przywara6e632102022-09-06 10:07:18 +0100157 sunxi_gpio_set_value_bank(pio, GPIO_NUM(pin), set);
Ian Campbellaf471472014-06-05 19:00:15 +0100158}
159
Andre Przywara6e632102022-09-06 10:07:18 +0100160static int sunxi_gpio_get_value(u32 pin)
Ian Campbellaf471472014-06-05 19:00:15 +0100161{
Ian Campbellaf471472014-06-05 19:00:15 +0100162 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100163 void *pio = BANK_TO_GPIO(bank);
Ian Campbellaf471472014-06-05 19:00:15 +0100164
Andre Przywara6e632102022-09-06 10:07:18 +0100165 return sunxi_gpio_get_value_bank(pio, GPIO_NUM(pin));
Ian Campbellaf471472014-06-05 19:00:15 +0100166}
167
168int gpio_request(unsigned gpio, const char *label)
169{
170 return 0;
171}
172
173int gpio_free(unsigned gpio)
174{
175 return 0;
176}
177
178int gpio_direction_input(unsigned gpio)
179{
180 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
181
Axel Lin06da3462014-12-20 11:41:25 +0800182 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100183}
184
185int gpio_direction_output(unsigned gpio, int value)
186{
187 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
Andre Przywara6e632102022-09-06 10:07:18 +0100188 sunxi_gpio_set_value(gpio, value);
Ian Campbellaf471472014-06-05 19:00:15 +0100189
Andre Przywara6e632102022-09-06 10:07:18 +0100190 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100191}
192
193int gpio_get_value(unsigned gpio)
194{
Andre Przywara6e632102022-09-06 10:07:18 +0100195 return sunxi_gpio_get_value(gpio);
Ian Campbellaf471472014-06-05 19:00:15 +0100196}
197
198int gpio_set_value(unsigned gpio, int value)
199{
Andre Przywara6e632102022-09-06 10:07:18 +0100200 sunxi_gpio_set_value(gpio, value);
201
202 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100203}
204
205int sunxi_name_to_gpio(const char *name)
206{
207 int group = 0;
208 int groupsize = 9 * 32;
209 long pin;
210 char *eptr;
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100211
Ian Campbellaf471472014-06-05 19:00:15 +0100212 if (*name == 'P' || *name == 'p')
213 name++;
214 if (*name >= 'A') {
215 group = *name - (*name > 'a' ? 'a' : 'A');
216 groupsize = 32;
217 name++;
218 }
219
220 pin = simple_strtol(name, &eptr, 10);
221 if (!*name || *eptr)
222 return -1;
223 if (pin < 0 || pin > groupsize || group >= 9)
224 return -1;
225 return group * 32 + pin;
226}
Andre Przywara82d307c2022-09-06 10:36:38 +0100227#endif /* !DM_GPIO */
228
229/* =========== DM code, used by U-Boot proper. ============ */
Simon Glass78304532014-10-30 20:25:49 -0600230
Simon Glassfa4689a2019-12-06 21:41:35 -0700231#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass9754d932015-04-18 11:33:43 -0600232/* TODO(sjg@chromium.org): Remove this function and use device tree */
233int sunxi_name_to_gpio(const char *name)
234{
235 unsigned int gpio;
236 int ret;
Hans de Goede08607d12015-04-22 11:31:22 +0200237#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
238 char lookup[8];
Simon Glass9754d932015-04-18 11:33:43 -0600239
Samuel Holland5f9c8442023-01-22 17:46:22 -0600240 if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) {
Hans de Goede08607d12015-04-22 11:31:22 +0200241 sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
242 SUNXI_GPIO_AXP0_VBUS_ENABLE);
243 name = lookup;
244 }
245#endif
Simon Glass9754d932015-04-18 11:33:43 -0600246 ret = gpio_lookup_name(name, NULL, NULL, &gpio);
247
248 return ret ? ret : gpio;
249}
250
Simon Glass78304532014-10-30 20:25:49 -0600251static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
252{
Simon Glassb75b15b2020-12-03 16:55:23 -0700253 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass78304532014-10-30 20:25:49 -0600254
Andre Przywara6e632102022-09-06 10:07:18 +0100255 return sunxi_gpio_get_value_bank(plat->regs, offset);
Simon Glass78304532014-10-30 20:25:49 -0600256}
257
Simon Glass78304532014-10-30 20:25:49 -0600258static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
259{
Simon Glassb75b15b2020-12-03 16:55:23 -0700260 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass78304532014-10-30 20:25:49 -0600261 int func;
262
263 func = sunxi_gpio_get_cfgbank(plat->regs, offset);
264 if (func == SUNXI_GPIO_OUTPUT)
265 return GPIOF_OUTPUT;
266 else if (func == SUNXI_GPIO_INPUT)
267 return GPIOF_INPUT;
268 else
269 return GPIOF_FUNC;
270}
271
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800272static int sunxi_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600273 struct ofnode_phandle_args *args)
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800274{
275 int ret;
276
277 ret = device_get_child(dev, args->args[0], &desc->dev);
278 if (ret)
279 return ret;
280 desc->offset = args->args[1];
Samuel Hollandbfda9492021-10-20 23:52:56 -0500281 desc->flags = gpio_flags_xlate(args->args[2]);
282
283 return 0;
284}
285
286static int sunxi_gpio_set_flags(struct udevice *dev, unsigned int offset,
287 ulong flags)
288{
289 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
290
291 if (flags & GPIOD_IS_OUT) {
292 u32 value = !!(flags & GPIOD_IS_OUT_ACTIVE);
Samuel Hollandbfda9492021-10-20 23:52:56 -0500293
Andre Przywara6e632102022-09-06 10:07:18 +0100294 sunxi_gpio_set_value_bank(plat->regs, offset, value);
Samuel Hollandbfda9492021-10-20 23:52:56 -0500295 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
296 } else if (flags & GPIOD_IS_IN) {
297 u32 pull = 0;
298
299 if (flags & GPIOD_PULL_UP)
300 pull = 1;
301 else if (flags & GPIOD_PULL_DOWN)
302 pull = 2;
303 sunxi_gpio_set_pull_bank(plat->regs, offset, pull);
304 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
305 }
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800306
307 return 0;
308}
309
Simon Glass78304532014-10-30 20:25:49 -0600310static const struct dm_gpio_ops gpio_sunxi_ops = {
Simon Glass78304532014-10-30 20:25:49 -0600311 .get_value = sunxi_gpio_get_value,
Simon Glass78304532014-10-30 20:25:49 -0600312 .get_function = sunxi_gpio_get_function,
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800313 .xlate = sunxi_gpio_xlate,
Samuel Hollandbfda9492021-10-20 23:52:56 -0500314 .set_flags = sunxi_gpio_set_flags,
Simon Glass78304532014-10-30 20:25:49 -0600315};
316
Simon Glass78304532014-10-30 20:25:49 -0600317static int gpio_sunxi_probe(struct udevice *dev)
318{
Simon Glassb75b15b2020-12-03 16:55:23 -0700319 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700320 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass78304532014-10-30 20:25:49 -0600321
322 /* Tell the uclass how many GPIOs we have */
323 if (plat) {
Samuel Hollande3095022021-08-12 20:09:43 -0500324 uc_priv->gpio_count = SUNXI_GPIOS_PER_BANK;
Simon Glass78304532014-10-30 20:25:49 -0600325 uc_priv->bank_name = plat->bank_name;
326 }
327
328 return 0;
329}
Stephen Warrenb56989e2016-05-11 15:26:25 -0600330
Simon Glass78304532014-10-30 20:25:49 -0600331U_BOOT_DRIVER(gpio_sunxi) = {
332 .name = "gpio_sunxi",
333 .id = UCLASS_GPIO,
Simon Glass78304532014-10-30 20:25:49 -0600334 .probe = gpio_sunxi_probe,
Samuel Hollande3095022021-08-12 20:09:43 -0500335 .ops = &gpio_sunxi_ops,
Simon Glass78304532014-10-30 20:25:49 -0600336};
Simon Glassfa4689a2019-12-06 21:41:35 -0700337#endif /* DM_GPIO */