2 * (C) Copyright 2009 Alessandro Rubini
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/arch/gpio.h>
11 static unsigned long gpio_base[4] = {
19 GPIO_DAT = 0x00, /* data register */
20 GPIO_DATS = 0x04, /* data set */
21 GPIO_DATC = 0x08, /* data clear */
22 GPIO_PDIS = 0x0c, /* pull disable */
23 GPIO_DIR = 0x10, /* direction */
24 GPIO_DIRS = 0x14, /* direction set */
25 GPIO_DIRC = 0x18, /* direction clear */
26 GPIO_AFSLA = 0x20, /* alternate function select A */
27 GPIO_AFSLB = 0x24, /* alternate function select B */
30 static inline unsigned long gpio_to_base(int gpio)
32 return gpio_base[gpio / 32];
35 static inline u32 gpio_to_bit(int gpio)
37 return 1 << (gpio & 0x1f);
40 void nmk_gpio_af(int gpio, int alternate_function)
42 unsigned long base = gpio_to_base(gpio);
43 u32 bit = gpio_to_bit(gpio);
46 /* alternate function is 0..3, with one bit per register */
47 afunc = readl(base + GPIO_AFSLA) & ~bit;
48 bfunc = readl(base + GPIO_AFSLB) & ~bit;
49 if (alternate_function & 1) afunc |= bit;
50 if (alternate_function & 2) bfunc |= bit;
51 writel(afunc, base + GPIO_AFSLA);
52 writel(bfunc, base + GPIO_AFSLB);
55 void nmk_gpio_dir(int gpio, int dir)
57 unsigned long base = gpio_to_base(gpio);
58 u32 bit = gpio_to_bit(gpio);
61 writel(bit, base + GPIO_DIRS);
63 writel(bit, base + GPIO_DIRC);
66 void nmk_gpio_set(int gpio, int val)
68 unsigned long base = gpio_to_base(gpio);
69 u32 bit = gpio_to_bit(gpio);
72 writel(bit, base + GPIO_DATS);
74 writel(bit, base + GPIO_DATC);
77 int nmk_gpio_get(int gpio)
79 unsigned long base = gpio_to_base(gpio);
80 u32 bit = gpio_to_bit(gpio);
82 return readl(base + GPIO_DAT) & bit;