2 * (C) Copyright 2009 Alessandro Rubini
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <asm/arch/gpio.h>
27 static unsigned long gpio_base[4] = {
35 GPIO_DAT = 0x00, /* data register */
36 GPIO_DATS = 0x04, /* data set */
37 GPIO_DATC = 0x08, /* data clear */
38 GPIO_PDIS = 0x0c, /* pull disable */
39 GPIO_DIR = 0x10, /* direction */
40 GPIO_DIRS = 0x14, /* direction set */
41 GPIO_DIRC = 0x18, /* direction clear */
42 GPIO_AFSLA = 0x20, /* alternate function select A */
43 GPIO_AFSLB = 0x24, /* alternate function select B */
46 static inline unsigned long gpio_to_base(int gpio)
48 return gpio_base[gpio / 32];
51 static inline u32 gpio_to_bit(int gpio)
53 return 1 << (gpio & 0x1f);
56 void nmk_gpio_af(int gpio, int alternate_function)
58 unsigned long base = gpio_to_base(gpio);
59 u32 bit = gpio_to_bit(gpio);
62 /* alternate function is 0..3, with one bit per register */
63 afunc = readl(base + GPIO_AFSLA) & ~bit;
64 bfunc = readl(base + GPIO_AFSLB) & ~bit;
65 if (alternate_function & 1) afunc |= bit;
66 if (alternate_function & 2) bfunc |= bit;
67 writel(afunc, base + GPIO_AFSLA);
68 writel(bfunc, base + GPIO_AFSLB);
71 void nmk_gpio_dir(int gpio, int dir)
73 unsigned long base = gpio_to_base(gpio);
74 u32 bit = gpio_to_bit(gpio);
77 writel(bit, base + GPIO_DIRS);
79 writel(bit, base + GPIO_DIRC);
82 void nmk_gpio_set(int gpio, int val)
84 unsigned long base = gpio_to_base(gpio);
85 u32 bit = gpio_to_bit(gpio);
88 writel(bit, base + GPIO_DATS);
90 writel(bit, base + GPIO_DATC);
93 int nmk_gpio_get(int gpio)
95 unsigned long base = gpio_to_base(gpio);
96 u32 bit = gpio_to_bit(gpio);
98 return readl(base + GPIO_DAT) & bit;