2 * Driver for Altera's PIO ip core
4 * Copyright (C) 2011 Missing Link Electronics
5 * Joachim Foerster <joachim@missinglinkelectronics.com>
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,
22 * To use this driver, in your board's config. header:
23 * #define CONFIG_ALTERA_PIO
24 * #define CONFIG_SYS_ALTERA_PIO_NUM <number-of-pio-cores>
25 * #define CONFIG_SYS_ALTERA_PIO_GPIO_NUM <total-number-of-gpios>
26 * And in your board's early setup routine:
27 * altera_pio_init(<baseaddr>, <width>, 'i'|'o'|'t',
28 * <reset-value>, <neg-mask>, "label");
29 * - 'i'|'o'|'t': PIO is input-only/output-only/tri-state
30 * - <reset-value>: for correct initial status display, output-only
31 * - <neg-mask> is meant to be used to in cases of active-low
32 * GPIOs, such as LEDs and buttons (on/pressed == 0). Each bit
33 * which is 1 in <neg-mask> inverts the corresponding GPIO's value
34 * before set/after get. So: gpio_set_value(gpio, 1) => LED on .
36 * Do NOT define CONFIG_SYS_GPIO_BASE !
38 * Optionally, in your board's config. header:
39 * - To force a GPIO numbering scheme like in Linux ...
40 * #define CONFIG_GPIO_DOWNTO_NUMBERING
41 * ... starting with 255 (default)
42 * #define CONFIG_GPIO_DOWNTO_MAX 255
48 #ifdef CONFIG_GPIO_DOWNTO_NUMBERING
49 #ifndef CONFIG_GPIO_DOWNTO_MAX
50 #define CONFIG_GPIO_DOWNTO_MAX 255
54 #define ALTERA_PIO_DATA 0x0
55 #define ALTERA_PIO_DIR 0x4
57 #define GPIO_LABEL_SIZE 9
60 static struct altera_pio {
68 char label[GPIO_LABEL_SIZE];
69 } pios[CONFIG_SYS_ALTERA_PIO_NUM];
73 static struct altera_pio_gpio {
75 struct altera_pio *pio;
76 char reqlabel[GPIO_LABEL_SIZE];
77 } gpios[CONFIG_SYS_ALTERA_PIO_GPIO_NUM];
79 static int pio_gpio_num;
82 static int altera_pio_gidx(unsigned gpio)
86 for (i = 0; i < pio_gpio_num; ++i) {
87 if (gpio == gpios[i].num)
90 if (i >= pio_gpio_num)
95 static struct altera_pio *altera_pio_get_and_mask(unsigned gpio, u32 *mask)
97 int gidx = altera_pio_gidx(gpio);
101 *mask = 1 << (gidx - gpios[gidx].pio->gidx);
102 return gpios[gidx].pio;
105 #define altera_pio_use_gidx(_gidx, _reqlabel) \
106 { strncpy(gpios[_gidx].reqlabel, _reqlabel, GPIO_LABEL_SIZE); }
107 #define altera_pio_unuse_gidx(_gidx) { gpios[_gidx].reqlabel[0] = '\0'; }
108 #define altera_pio_is_gidx_used(_gidx) (gpios[_gidx].reqlabel[0] != '\0')
110 static int altera_pio_gpio_init(struct altera_pio *pio, u8 width)
112 u8 gidx = pio_gpio_num;
117 if ((pio_gpio_num + width) > CONFIG_SYS_ALTERA_PIO_GPIO_NUM)
120 for (i = 0; i < width; ++i) {
121 #ifdef CONFIG_GPIO_DOWNTO_NUMBERING
122 gpios[pio_gpio_num + i].num = \
123 CONFIG_GPIO_DOWNTO_MAX + 1 - gidx - width + i;
125 gpios[pio_gpio_num + i].num = pio_gpio_num + i;
127 gpios[pio_gpio_num + i].pio = pio;
128 altera_pio_unuse_gidx(pio_gpio_num + i);
130 pio_gpio_num += width;
134 int altera_pio_init(u32 base, u8 width, char iot, u32 rstval, u32 negmask,
137 if (pio_num >= CONFIG_SYS_ALTERA_PIO_NUM)
140 pios[pio_num].base = base;
141 pios[pio_num].width = width;
142 pios[pio_num].iot = iot;
146 pios[pio_num].sh_dir = 0;
147 pios[pio_num].sh_data = readl(base + ALTERA_PIO_DATA);
151 pios[pio_num].sh_dir = 0xffffffff & ((1 << width) - 1);
152 pios[pio_num].sh_data = rstval;
155 /* bidir, tri-state */
156 pios[pio_num].sh_dir = readl(base + ALTERA_PIO_DIR);
157 pios[pio_num].sh_data = readl(base + ALTERA_PIO_DATA);
162 pios[pio_num].negmask = negmask & ((1 << width) - 1);
163 pios[pio_num].gidx = altera_pio_gpio_init(&pios[pio_num], width);
164 if (pios[pio_num].gidx < 0)
166 strncpy(pios[pio_num].label, label, GPIO_LABEL_SIZE);
170 void altera_pio_info(void)
177 for (i = 0; i < pio_num; ++i) {
178 printf("Altera PIO % 2d, @0x%08x, "
179 "width: %u, label: %s\n",
180 i, pios[i].base, pios[i].width, pios[i].label);
182 for (j = gidx; j < (gidx + pios[i].width); ++j) {
183 mask = 1 << (j - gidx);
184 printf("\tGPIO % 4d: %s %s [%c] %s\n",
186 gpios[j].pio->sh_dir & mask ? "out" : " in",
187 gpio_get_value(gpios[j].num) ? "set" : "clr",
188 altera_pio_is_gidx_used(j) ? 'x' : ' ',
195 int gpio_request(unsigned gpio, const char *label)
197 int gidx = altera_pio_gidx(gpio);
200 if (altera_pio_is_gidx_used(gidx))
203 altera_pio_use_gidx(gidx, label);
207 int gpio_free(unsigned gpio)
209 int gidx = altera_pio_gidx(gpio);
212 if (!altera_pio_is_gidx_used(gidx))
215 altera_pio_unuse_gidx(gidx);
219 int gpio_direction_input(unsigned gpio)
222 struct altera_pio *pio;
224 pio = altera_pio_get_and_mask(gpio, &mask);
230 writel(pio->sh_dir &= ~mask, pio->base + ALTERA_PIO_DIR);
234 int gpio_direction_output(unsigned gpio, int value)
237 struct altera_pio *pio;
239 pio = altera_pio_get_and_mask(gpio, &mask);
245 value = (pio->negmask & mask) ? !value : value;
247 pio->sh_data |= mask;
249 pio->sh_data &= ~mask;
250 writel(pio->sh_data, pio->base + ALTERA_PIO_DATA);
251 writel(pio->sh_dir |= mask, pio->base + ALTERA_PIO_DIR);
255 int gpio_get_value(unsigned gpio)
258 struct altera_pio *pio;
261 pio = altera_pio_get_and_mask(gpio, &mask);
265 if ((pio->sh_dir & mask) || (pio->iot == 'o'))
266 val = pio->sh_data & mask;
268 val = readl(pio->base + ALTERA_PIO_DATA) & mask;
269 return (pio->negmask & mask) ? !val : val;
272 void gpio_set_value(unsigned gpio, int value)
275 struct altera_pio *pio;
277 pio = altera_pio_get_and_mask(gpio, &mask);
283 value = (pio->negmask & mask) ? !value : value;
285 pio->sh_data |= mask;
287 pio->sh_data &= ~mask;
288 writel(pio->sh_data, pio->base + ALTERA_PIO_DATA);
292 int gpio_is_valid(int number)
294 int gidx = altera_pio_gidx(number);