blob: 5e86474d3dbb37c4eab7684d38e2355ccfdce00e [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
160
161/* =========== Non-DM code, used by the SPL. ============ */
162
Simon Glassfa4689a2019-12-06 21:41:35 -0700163#if !CONFIG_IS_ENABLED(DM_GPIO)
Andre Przywara6e632102022-09-06 10:07:18 +0100164static void sunxi_gpio_set_value(u32 pin, bool set)
Ian Campbellaf471472014-06-05 19:00:15 +0100165{
Ian Campbellaf471472014-06-05 19:00:15 +0100166 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100167 void *pio = BANK_TO_GPIO(bank);
Ian Campbellaf471472014-06-05 19:00:15 +0100168
Andre Przywara6e632102022-09-06 10:07:18 +0100169 sunxi_gpio_set_value_bank(pio, GPIO_NUM(pin), set);
Ian Campbellaf471472014-06-05 19:00:15 +0100170}
171
Andre Przywara6e632102022-09-06 10:07:18 +0100172static int sunxi_gpio_get_value(u32 pin)
Ian Campbellaf471472014-06-05 19:00:15 +0100173{
Ian Campbellaf471472014-06-05 19:00:15 +0100174 u32 bank = GPIO_BANK(pin);
Andre Przywara841ebfb32022-09-05 18:12:39 +0100175 void *pio = BANK_TO_GPIO(bank);
Ian Campbellaf471472014-06-05 19:00:15 +0100176
Andre Przywara6e632102022-09-06 10:07:18 +0100177 return sunxi_gpio_get_value_bank(pio, GPIO_NUM(pin));
Ian Campbellaf471472014-06-05 19:00:15 +0100178}
179
180int gpio_request(unsigned gpio, const char *label)
181{
182 return 0;
183}
184
185int gpio_free(unsigned gpio)
186{
187 return 0;
188}
189
190int gpio_direction_input(unsigned gpio)
191{
192 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
193
Axel Lin06da3462014-12-20 11:41:25 +0800194 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100195}
196
197int gpio_direction_output(unsigned gpio, int value)
198{
199 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
Andre Przywara6e632102022-09-06 10:07:18 +0100200 sunxi_gpio_set_value(gpio, value);
Ian Campbellaf471472014-06-05 19:00:15 +0100201
Andre Przywara6e632102022-09-06 10:07:18 +0100202 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100203}
204
205int gpio_get_value(unsigned gpio)
206{
Andre Przywara6e632102022-09-06 10:07:18 +0100207 return sunxi_gpio_get_value(gpio);
Ian Campbellaf471472014-06-05 19:00:15 +0100208}
209
210int gpio_set_value(unsigned gpio, int value)
211{
Andre Przywara6e632102022-09-06 10:07:18 +0100212 sunxi_gpio_set_value(gpio, value);
213
214 return 0;
Ian Campbellaf471472014-06-05 19:00:15 +0100215}
216
217int sunxi_name_to_gpio(const char *name)
218{
219 int group = 0;
220 int groupsize = 9 * 32;
221 long pin;
222 char *eptr;
Hans de Goede1fc9c4a2014-12-24 19:34:38 +0100223
Ian Campbellaf471472014-06-05 19:00:15 +0100224 if (*name == 'P' || *name == 'p')
225 name++;
226 if (*name >= 'A') {
227 group = *name - (*name > 'a' ? 'a' : 'A');
228 groupsize = 32;
229 name++;
230 }
231
232 pin = simple_strtol(name, &eptr, 10);
233 if (!*name || *eptr)
234 return -1;
235 if (pin < 0 || pin > groupsize || group >= 9)
236 return -1;
237 return group * 32 + pin;
238}
Andre Przywara82d307c2022-09-06 10:36:38 +0100239#endif /* !DM_GPIO */
240
241/* =========== DM code, used by U-Boot proper. ============ */
Simon Glass78304532014-10-30 20:25:49 -0600242
Simon Glassfa4689a2019-12-06 21:41:35 -0700243#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass9754d932015-04-18 11:33:43 -0600244/* TODO(sjg@chromium.org): Remove this function and use device tree */
245int sunxi_name_to_gpio(const char *name)
246{
247 unsigned int gpio;
248 int ret;
Hans de Goede08607d12015-04-22 11:31:22 +0200249#if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
250 char lookup[8];
Simon Glass9754d932015-04-18 11:33:43 -0600251
Samuel Holland5f9c8442023-01-22 17:46:22 -0600252 if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) {
Hans de Goede08607d12015-04-22 11:31:22 +0200253 sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
254 SUNXI_GPIO_AXP0_VBUS_ENABLE);
255 name = lookup;
256 }
257#endif
Simon Glass9754d932015-04-18 11:33:43 -0600258 ret = gpio_lookup_name(name, NULL, NULL, &gpio);
259
260 return ret ? ret : gpio;
261}
262
Simon Glass78304532014-10-30 20:25:49 -0600263static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
264{
Simon Glassb75b15b2020-12-03 16:55:23 -0700265 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass78304532014-10-30 20:25:49 -0600266
Andre Przywara6e632102022-09-06 10:07:18 +0100267 return sunxi_gpio_get_value_bank(plat->regs, offset);
Simon Glass78304532014-10-30 20:25:49 -0600268}
269
Simon Glass78304532014-10-30 20:25:49 -0600270static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
271{
Simon Glassb75b15b2020-12-03 16:55:23 -0700272 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glass78304532014-10-30 20:25:49 -0600273 int func;
274
275 func = sunxi_gpio_get_cfgbank(plat->regs, offset);
276 if (func == SUNXI_GPIO_OUTPUT)
277 return GPIOF_OUTPUT;
278 else if (func == SUNXI_GPIO_INPUT)
279 return GPIOF_INPUT;
280 else
281 return GPIOF_FUNC;
282}
283
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800284static int sunxi_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600285 struct ofnode_phandle_args *args)
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800286{
287 int ret;
288
289 ret = device_get_child(dev, args->args[0], &desc->dev);
290 if (ret)
291 return ret;
292 desc->offset = args->args[1];
Samuel Hollandbfda9492021-10-20 23:52:56 -0500293 desc->flags = gpio_flags_xlate(args->args[2]);
294
295 return 0;
296}
297
298static int sunxi_gpio_set_flags(struct udevice *dev, unsigned int offset,
299 ulong flags)
300{
301 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
302
303 if (flags & GPIOD_IS_OUT) {
304 u32 value = !!(flags & GPIOD_IS_OUT_ACTIVE);
Samuel Hollandbfda9492021-10-20 23:52:56 -0500305
Andre Przywara6e632102022-09-06 10:07:18 +0100306 sunxi_gpio_set_value_bank(plat->regs, offset, value);
Samuel Hollandbfda9492021-10-20 23:52:56 -0500307 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
308 } else if (flags & GPIOD_IS_IN) {
309 u32 pull = 0;
310
311 if (flags & GPIOD_PULL_UP)
312 pull = 1;
313 else if (flags & GPIOD_PULL_DOWN)
314 pull = 2;
315 sunxi_gpio_set_pull_bank(plat->regs, offset, pull);
316 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
317 }
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800318
319 return 0;
320}
321
Simon Glass78304532014-10-30 20:25:49 -0600322static const struct dm_gpio_ops gpio_sunxi_ops = {
Simon Glass78304532014-10-30 20:25:49 -0600323 .get_value = sunxi_gpio_get_value,
Simon Glass78304532014-10-30 20:25:49 -0600324 .get_function = sunxi_gpio_get_function,
Chen-Yu Tsaif3aa2822016-07-22 16:12:59 +0800325 .xlate = sunxi_gpio_xlate,
Samuel Hollandbfda9492021-10-20 23:52:56 -0500326 .set_flags = sunxi_gpio_set_flags,
Simon Glass78304532014-10-30 20:25:49 -0600327};
328
Simon Glass78304532014-10-30 20:25:49 -0600329static int gpio_sunxi_probe(struct udevice *dev)
330{
Simon Glassb75b15b2020-12-03 16:55:23 -0700331 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
Simon Glassde0977b2015-03-05 12:25:20 -0700332 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass78304532014-10-30 20:25:49 -0600333
334 /* Tell the uclass how many GPIOs we have */
335 if (plat) {
Samuel Hollande3095022021-08-12 20:09:43 -0500336 uc_priv->gpio_count = SUNXI_GPIOS_PER_BANK;
Simon Glass78304532014-10-30 20:25:49 -0600337 uc_priv->bank_name = plat->bank_name;
338 }
339
340 return 0;
341}
Stephen Warrenb56989e2016-05-11 15:26:25 -0600342
Simon Glass78304532014-10-30 20:25:49 -0600343U_BOOT_DRIVER(gpio_sunxi) = {
344 .name = "gpio_sunxi",
345 .id = UCLASS_GPIO,
Simon Glass78304532014-10-30 20:25:49 -0600346 .probe = gpio_sunxi_probe,
Samuel Hollande3095022021-08-12 20:09:43 -0500347 .ops = &gpio_sunxi_ops,
Simon Glass78304532014-10-30 20:25:49 -0600348};
Simon Glassfa4689a2019-12-06 21:41:35 -0700349#endif /* DM_GPIO */