blob: 218ca2af397d1b9195fc84dbfa427461c06e0d21 [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
Simon Glass78304532014-10-30 20:25:49 -060012#include <dm.h>
13#include <errno.h>
14#include <fdtdec.h>
15#include <malloc.h>
Ian Campbellaf471472014-06-05 19:00:15 +010016#include <asm/io.h>
17#include <asm/gpio.h>
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +080018#include <dt-bindings/gpio/gpio.h>
Andre Przywaraf944a612022-09-06 10:36:38 +010019#include <sunxi_gpio.h>
Ian Campbellaf471472014-06-05 19:00:15 +010020
Andre Przywara82d307c2022-09-06 10:36:38 +010021/*
22 * =======================================================================
23 * Low level GPIO/pin controller access functions, to be shared by non-DM
24 * SPL code and the DM pinctrl/GPIO drivers.
25 * The functions ending in "bank" take a base pointer to a GPIO bank, and
26 * the pin offset is relative to that bank.
27 * The functions without "bank" in their name take a linear GPIO number,
28 * covering all ports, and starting at 0 for PortA.
29 * =======================================================================
30 */
31
Andre Przywara82d307c2022-09-06 10:36:38 +010032#define GPIO_BANK(pin) ((pin) >> 5)
33#define GPIO_NUM(pin) ((pin) & 0x1f)
34
Andre Przywara841ebfb32022-09-05 18:12:39 +010035#define GPIO_CFG_REG_OFFSET 0x00
Andre Przywara82d307c2022-09-06 10:36:38 +010036#define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3)
37#define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2)
38
Andre Przywara841ebfb32022-09-05 18:12:39 +010039#define GPIO_DAT_REG_OFFSET 0x10
40
41#define GPIO_DRV_REG_OFFSET 0x14
Andre Przywaraf6ad5102022-09-06 12:12:50 +010042
43/* Newer SoCs use a slightly different register layout */
44#ifdef CONFIG_SUNXI_NEW_PINCTRL
45/* pin drive strength: 4 bits per pin */
46#define GPIO_DRV_INDEX(pin) ((pin) / 8)
47#define GPIO_DRV_OFFSET(pin) (((pin) % 8) * 4)
48
49#define GPIO_PULL_REG_OFFSET 0x24
50
51#else /* older generation pin controllers */
52/* pin drive strength: 2 bits per pin */
53#define GPIO_DRV_INDEX(pin) ((pin) / 16)
54#define GPIO_DRV_OFFSET(pin) (((pin) % 16) * 2)
Andre Przywara82d307c2022-09-06 10:36:38 +010055
Andre Przywara841ebfb32022-09-05 18:12:39 +010056#define GPIO_PULL_REG_OFFSET 0x1c
Andre Przywaraf6ad5102022-09-06 12:12:50 +010057#endif
58
Andre Przywara82d307c2022-09-06 10:36:38 +010059#define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4)
60#define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
61
Andre Przywara841ebfb32022-09-05 18:12:39 +010062static void* BANK_TO_GPIO(int bank)
63{
64 void *pio_base;
65
66 if (bank < SUNXI_GPIO_L) {
67 pio_base = (void *)(uintptr_t)SUNXI_PIO_BASE;
68 } else {
69 pio_base = (void *)(uintptr_t)SUNXI_R_PIO_BASE;
70 bank -= SUNXI_GPIO_L;
71 }
72
73 return pio_base + bank * SUNXI_PINCTRL_BANK_SIZE;
74}
75
76void sunxi_gpio_set_cfgbank(void *bank_base, int pin_offset, u32 val)
Andre Przywara82d307c2022-09-06 10:36:38 +010077{
Andre Przywara841ebfb32022-09-05 18:12:39 +010078 u32 index = GPIO_CFG_INDEX(pin_offset);
79 u32 offset = GPIO_CFG_OFFSET(pin_offset);
Andre Przywara82d307c2022-09-06 10:36:38 +010080
Andre Przywara841ebfb32022-09-05 18:12:39 +010081 clrsetbits_le32(bank_base + GPIO_CFG_REG_OFFSET + index * 4,
82 0xfU << offset, val << offset);
Andre Przywara82d307c2022-09-06 10:36:38 +010083}
84
85void sunxi_gpio_set_cfgpin(u32 pin, u32 val)
86{
87 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +010088 void *pio = BANK_TO_GPIO(bank);
Andre Przywara82d307c2022-09-06 10:36:38 +010089
Andre Przywara841ebfb32022-09-05 18:12:39 +010090 sunxi_gpio_set_cfgbank(pio, GPIO_NUM(pin), val);
Andre Przywara82d307c2022-09-06 10:36:38 +010091}
92
Andre Przywara841ebfb32022-09-05 18:12:39 +010093int sunxi_gpio_get_cfgbank(void *bank_base, int pin_offset)
Andre Przywara82d307c2022-09-06 10:36:38 +010094{
Andre Przywara841ebfb32022-09-05 18:12:39 +010095 u32 index = GPIO_CFG_INDEX(pin_offset);
96 u32 offset = GPIO_CFG_OFFSET(pin_offset);
Andre Przywara82d307c2022-09-06 10:36:38 +010097 u32 cfg;
98
Andre Przywara841ebfb32022-09-05 18:12:39 +010099 cfg = readl(bank_base + GPIO_CFG_REG_OFFSET + index * 4);
Andre Przywara82d307c2022-09-06 10:36:38 +0100100 cfg >>= offset;
101
102 return cfg & 0xf;
103}
104
105int sunxi_gpio_get_cfgpin(u32 pin)
106{
107 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100108 void *bank_base = BANK_TO_GPIO(bank);
Andre Przywara82d307c2022-09-06 10:36:38 +0100109
Andre Przywara841ebfb32022-09-05 18:12:39 +0100110 return sunxi_gpio_get_cfgbank(bank_base, GPIO_NUM(pin));
Andre Przywara82d307c2022-09-06 10:36:38 +0100111}
112
Andre Przywara841ebfb32022-09-05 18:12:39 +0100113static void sunxi_gpio_set_value_bank(void *bank_base, int pin, bool set)
Andre Przywara6e632102022-09-06 10:07:18 +0100114{
115 u32 mask = 1U << pin;
116
Andre Przywara841ebfb32022-09-05 18:12:39 +0100117 clrsetbits_le32(bank_base + GPIO_DAT_REG_OFFSET,
118 set ? 0 : mask, set ? mask : 0);
Andre Przywara6e632102022-09-06 10:07:18 +0100119}
120
Andre Przywara841ebfb32022-09-05 18:12:39 +0100121static int sunxi_gpio_get_value_bank(void *bank_base, int pin)
Andre Przywara6e632102022-09-06 10:07:18 +0100122{
Andre Przywara841ebfb32022-09-05 18:12:39 +0100123 return !!(readl(bank_base + GPIO_DAT_REG_OFFSET) & (1U << pin));
Andre Przywara6e632102022-09-06 10:07:18 +0100124}
125
Andre Przywara82d307c2022-09-06 10:36:38 +0100126void sunxi_gpio_set_drv(u32 pin, u32 val)
127{
128 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100129 void *bank_base = BANK_TO_GPIO(bank);
Andre Przywara82d307c2022-09-06 10:36:38 +0100130
Andre Przywara841ebfb32022-09-05 18:12:39 +0100131 sunxi_gpio_set_drv_bank(bank_base, GPIO_NUM(pin), val);
Andre Przywara82d307c2022-09-06 10:36:38 +0100132}
133
Andre Przywara841ebfb32022-09-05 18:12:39 +0100134void sunxi_gpio_set_drv_bank(void *bank_base, u32 pin_offset, u32 val)
Andre Przywara82d307c2022-09-06 10:36:38 +0100135{
Andre Przywara841ebfb32022-09-05 18:12:39 +0100136 u32 index = GPIO_DRV_INDEX(pin_offset);
137 u32 offset = GPIO_DRV_OFFSET(pin_offset);
Andre Przywara82d307c2022-09-06 10:36:38 +0100138
Andre Przywara841ebfb32022-09-05 18:12:39 +0100139 clrsetbits_le32(bank_base + GPIO_DRV_REG_OFFSET + index * 4,
140 0x3U << offset, val << offset);
Andre Przywara82d307c2022-09-06 10:36:38 +0100141}
142
143void sunxi_gpio_set_pull(u32 pin, u32 val)
144{
145 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100146 void *bank_base = BANK_TO_GPIO(bank);
Andre Przywara82d307c2022-09-06 10:36:38 +0100147
Andre Przywara841ebfb32022-09-05 18:12:39 +0100148 sunxi_gpio_set_pull_bank(bank_base, GPIO_NUM(pin), val);
Andre Przywara82d307c2022-09-06 10:36:38 +0100149}
150
Andre Przywara841ebfb32022-09-05 18:12:39 +0100151void sunxi_gpio_set_pull_bank(void *bank_base, int pin_offset, u32 val)
Andre Przywara82d307c2022-09-06 10:36:38 +0100152{
Andre Przywara841ebfb32022-09-05 18:12:39 +0100153 u32 index = GPIO_PULL_INDEX(pin_offset);
154 u32 offset = GPIO_PULL_OFFSET(pin_offset);
Andre Przywara82d307c2022-09-06 10:36:38 +0100155
Andre Przywara841ebfb32022-09-05 18:12:39 +0100156 clrsetbits_le32(bank_base + GPIO_PULL_REG_OFFSET + index * 4,
157 0x3U << offset, val << offset);
Andre Przywara82d307c2022-09-06 10:36:38 +0100158}
159
Andre Przywara82d307c2022-09-06 10:36:38 +0100160/* =========== Non-DM code, used by the SPL. ============ */
161
Simon Glassfa4689a2019-12-06 21:41:35 -0700162#if !CONFIG_IS_ENABLED(DM_GPIO)
Andre Przywara6e632102022-09-06 10:07:18 +0100163static void sunxi_gpio_set_value(u32 pin, bool set)
Ian Campbellaf471472014-06-05 19:00:15 +0100164{
Ian Campbellaf471472014-06-05 19:00:15 +0100165 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100166 void *pio = BANK_TO_GPIO(bank);
Ian Campbellaf471472014-06-05 19:00:15 +0100167
Andre Przywara6e632102022-09-06 10:07:18 +0100168 sunxi_gpio_set_value_bank(pio, GPIO_NUM(pin), set);
Ian Campbellaf471472014-06-05 19:00:15 +0100169}
170
Andre Przywara6e632102022-09-06 10:07:18 +0100171static int sunxi_gpio_get_value(u32 pin)
Ian Campbellaf471472014-06-05 19:00:15 +0100172{
Ian Campbellaf471472014-06-05 19:00:15 +0100173 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100174 void *pio = BANK_TO_GPIO(bank);
Ian Campbellaf471472014-06-05 19:00:15 +0100175
Andre Przywara6e632102022-09-06 10:07:18 +0100176 return sunxi_gpio_get_value_bank(pio, GPIO_NUM(pin));
Ian Campbellaf471472014-06-05 19:00:15 +0100177}
178
179int gpio_request(unsigned gpio, const char *label)
180{
181 return 0;
182}
183
184int gpio_free(unsigned gpio)
185{
186 return 0;
187}
188
189int gpio_direction_input(unsigned gpio)
190{
191 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
192
Axel Lin06da3462014-12-20 11:41:25 +0800193 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100194}
195
196int gpio_direction_output(unsigned gpio, int value)
197{
198 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
Andre Przywara6e632102022-09-06 10:07:18 +0100199 sunxi_gpio_set_value(gpio, value);
Ian Campbellaf471472014-06-05 19:00:15 +0100200
Andre Przywara6e632102022-09-06 10:07:18 +0100201 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100202}
203
204int gpio_get_value(unsigned gpio)
205{
Andre Przywara6e632102022-09-06 10:07:18 +0100206 return sunxi_gpio_get_value(gpio);
Ian Campbellaf471472014-06-05 19:00:15 +0100207}
208
209int gpio_set_value(unsigned gpio, int value)
210{
Andre Przywara6e632102022-09-06 10:07:18 +0100211 sunxi_gpio_set_value(gpio, value);
212
213 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100214}
215
216int sunxi_name_to_gpio(const char *name)
217{
218 int group = 0;
219 int groupsize = 9 * 32;
220 long pin;
221 char *eptr;
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100222
Ian Campbellaf471472014-06-05 19:00:15 +0100223 if (*name == 'P' || *name == 'p')
224 name++;
225 if (*name >= 'A') {
226 group = *name - (*name > 'a' ? 'a' : 'A');
227 groupsize = 32;
228 name++;
229 }
230
231 pin = simple_strtol(name, &eptr, 10);
232 if (!*name || *eptr)
233 return -1;
234 if (pin < 0 || pin > groupsize || group >= 9)
235 return -1;
236 return group * 32 + pin;
237}
Andre Przywara82d307c2022-09-06 10:36:38 +0100238#endif /* !DM_GPIO */
239
240/* =========== DM code, used by U-Boot proper. ============ */
Simon Glass78304532014-10-30 20:25:49 -0600241
Simon Glassfa4689a2019-12-06 21:41:35 -0700242#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass9754d932015-04-18 11:33:43 -0600243/* TODO(sjg@chromium.org): Remove this function and use device tree */
244int sunxi_name_to_gpio(const char *name)
245{
246 unsigned int gpio;
247 int ret;
Hans de Goede08607d12015-04-22 11:31:22 +0200248#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
249 char lookup[8];
Simon Glass9754d932015-04-18 11:33:43 -0600250
Samuel Holland5f9c8442023-01-22 17:46:22 -0600251 if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) {
Hans de Goede08607d12015-04-22 11:31:22 +0200252 sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
253 SUNXI_GPIO_AXP0_VBUS_ENABLE);
254 name = lookup;
255 }
256#endif
Simon Glass9754d932015-04-18 11:33:43 -0600257 ret = gpio_lookup_name(name, NULL, NULL, &gpio);
258
259 return ret ? ret : gpio;
260}
261
Simon Glass78304532014-10-30 20:25:49 -0600262static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
263{
Simon Glassb75b15b2020-12-03 16:55:23 -0700264 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass78304532014-10-30 20:25:49 -0600265
Andre Przywara6e632102022-09-06 10:07:18 +0100266 return sunxi_gpio_get_value_bank(plat->regs, offset);
Simon Glass78304532014-10-30 20:25:49 -0600267}
268
Simon Glass78304532014-10-30 20:25:49 -0600269static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
270{
Simon Glassb75b15b2020-12-03 16:55:23 -0700271 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass78304532014-10-30 20:25:49 -0600272 int func;
273
274 func = sunxi_gpio_get_cfgbank(plat->regs, offset);
275 if (func == SUNXI_GPIO_OUTPUT)
276 return GPIOF_OUTPUT;
277 else if (func == SUNXI_GPIO_INPUT)
278 return GPIOF_INPUT;
279 else
280 return GPIOF_FUNC;
281}
282
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800283static int sunxi_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600284 struct ofnode_phandle_args *args)
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800285{
286 int ret;
287
288 ret = device_get_child(dev, args->args[0], &desc->dev);
289 if (ret)
290 return ret;
291 desc->offset = args->args[1];
Samuel Hollandbfda9492021-10-20 23:52:56 -0500292 desc->flags = gpio_flags_xlate(args->args[2]);
293
294 return 0;
295}
296
297static int sunxi_gpio_set_flags(struct udevice *dev, unsigned int offset,
298 ulong flags)
299{
300 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
301
302 if (flags & GPIOD_IS_OUT) {
303 u32 value = !!(flags & GPIOD_IS_OUT_ACTIVE);
Samuel Hollandbfda9492021-10-20 23:52:56 -0500304
Andre Przywara6e632102022-09-06 10:07:18 +0100305 sunxi_gpio_set_value_bank(plat->regs, offset, value);
Samuel Hollandbfda9492021-10-20 23:52:56 -0500306 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
307 } else if (flags & GPIOD_IS_IN) {
308 u32 pull = 0;
309
310 if (flags & GPIOD_PULL_UP)
311 pull = 1;
312 else if (flags & GPIOD_PULL_DOWN)
313 pull = 2;
314 sunxi_gpio_set_pull_bank(plat->regs, offset, pull);
315 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
316 }
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800317
318 return 0;
319}
320
Simon Glass78304532014-10-30 20:25:49 -0600321static const struct dm_gpio_ops gpio_sunxi_ops = {
Simon Glass78304532014-10-30 20:25:49 -0600322 .get_value = sunxi_gpio_get_value,
Simon Glass78304532014-10-30 20:25:49 -0600323 .get_function = sunxi_gpio_get_function,
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800324 .xlate = sunxi_gpio_xlate,
Samuel Hollandbfda9492021-10-20 23:52:56 -0500325 .set_flags = sunxi_gpio_set_flags,
Simon Glass78304532014-10-30 20:25:49 -0600326};
327
Simon Glass78304532014-10-30 20:25:49 -0600328static int gpio_sunxi_probe(struct udevice *dev)
329{
Simon Glassb75b15b2020-12-03 16:55:23 -0700330 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700331 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass78304532014-10-30 20:25:49 -0600332
333 /* Tell the uclass how many GPIOs we have */
334 if (plat) {
Samuel Hollande3095022021-08-12 20:09:43 -0500335 uc_priv->gpio_count = SUNXI_GPIOS_PER_BANK;
Simon Glass78304532014-10-30 20:25:49 -0600336 uc_priv->bank_name = plat->bank_name;
337 }
338
339 return 0;
340}
Stephen Warrenb56989e2016-05-11 15:26:25 -0600341
Simon Glass78304532014-10-30 20:25:49 -0600342U_BOOT_DRIVER(gpio_sunxi) = {
343 .name = "gpio_sunxi",
344 .id = UCLASS_GPIO,
Simon Glass78304532014-10-30 20:25:49 -0600345 .probe = gpio_sunxi_probe,
Samuel Hollande3095022021-08-12 20:09:43 -0500346 .ops = &gpio_sunxi_ops,
Simon Glass78304532014-10-30 20:25:49 -0600347};
Simon Glassfa4689a2019-12-06 21:41:35 -0700348#endif /* DM_GPIO */