2 * Driver for Altera's PIO ip core
4 * Copyright (C) 2011 Missing Link Electronics
5 * Joachim Foerster <joachim@missinglinkelectronics.com>
7 * SPDX-License-Identifier: GPL-2.0+
9 * To use this driver, in your board's config. header:
10 * #define CONFIG_ALTERA_PIO
11 * #define CONFIG_SYS_ALTERA_PIO_NUM <number-of-pio-cores>
12 * #define CONFIG_SYS_ALTERA_PIO_GPIO_NUM <total-number-of-gpios>
13 * And in your board's early setup routine:
14 * altera_pio_init(<baseaddr>, <width>, 'i'|'o'|'t',
15 * <reset-value>, <neg-mask>, "label");
16 * - 'i'|'o'|'t': PIO is input-only/output-only/tri-state
17 * - <reset-value>: for correct initial status display, output-only
18 * - <neg-mask> is meant to be used to in cases of active-low
19 * GPIOs, such as LEDs and buttons (on/pressed == 0). Each bit
20 * which is 1 in <neg-mask> inverts the corresponding GPIO's value
21 * before set/after get. So: gpio_set_value(gpio, 1) => LED on .
23 * Do NOT define CONFIG_SYS_GPIO_BASE !
25 * Optionally, in your board's config. header:
26 * - To force a GPIO numbering scheme like in Linux ...
27 * #define CONFIG_GPIO_DOWNTO_NUMBERING
28 * ... starting with 255 (default)
29 * #define CONFIG_GPIO_DOWNTO_MAX 255
35 #ifdef CONFIG_GPIO_DOWNTO_NUMBERING
36 #ifndef CONFIG_GPIO_DOWNTO_MAX
37 #define CONFIG_GPIO_DOWNTO_MAX 255
41 #define ALTERA_PIO_DATA 0x0
42 #define ALTERA_PIO_DIR 0x4
44 #define GPIO_LABEL_SIZE 9
47 static struct altera_pio
{
55 char label
[GPIO_LABEL_SIZE
];
56 } pios
[CONFIG_SYS_ALTERA_PIO_NUM
];
60 static struct altera_pio_gpio
{
62 struct altera_pio
*pio
;
63 char reqlabel
[GPIO_LABEL_SIZE
];
64 } gpios
[CONFIG_SYS_ALTERA_PIO_GPIO_NUM
];
66 static int pio_gpio_num
;
69 static int altera_pio_gidx(unsigned gpio
)
73 for (i
= 0; i
< pio_gpio_num
; ++i
) {
74 if (gpio
== gpios
[i
].num
)
77 if (i
>= pio_gpio_num
)
82 static struct altera_pio
*altera_pio_get_and_mask(unsigned gpio
, u32
*mask
)
84 int gidx
= altera_pio_gidx(gpio
);
88 *mask
= 1 << (gidx
- gpios
[gidx
].pio
->gidx
);
89 return gpios
[gidx
].pio
;
92 #define altera_pio_use_gidx(_gidx, _reqlabel) \
93 { strncpy(gpios[_gidx].reqlabel, _reqlabel, GPIO_LABEL_SIZE); }
94 #define altera_pio_unuse_gidx(_gidx) { gpios[_gidx].reqlabel[0] = '\0'; }
95 #define altera_pio_is_gidx_used(_gidx) (gpios[_gidx].reqlabel[0] != '\0')
97 static int altera_pio_gpio_init(struct altera_pio
*pio
, u8 width
)
99 u8 gidx
= pio_gpio_num
;
104 if ((pio_gpio_num
+ width
) > CONFIG_SYS_ALTERA_PIO_GPIO_NUM
)
107 for (i
= 0; i
< width
; ++i
) {
108 #ifdef CONFIG_GPIO_DOWNTO_NUMBERING
109 gpios
[pio_gpio_num
+ i
].num
= \
110 CONFIG_GPIO_DOWNTO_MAX
+ 1 - gidx
- width
+ i
;
112 gpios
[pio_gpio_num
+ i
].num
= pio_gpio_num
+ i
;
114 gpios
[pio_gpio_num
+ i
].pio
= pio
;
115 altera_pio_unuse_gidx(pio_gpio_num
+ i
);
117 pio_gpio_num
+= width
;
121 int altera_pio_init(u32 base
, u8 width
, char iot
, u32 rstval
, u32 negmask
,
124 if (pio_num
>= CONFIG_SYS_ALTERA_PIO_NUM
)
127 pios
[pio_num
].base
= base
;
128 pios
[pio_num
].width
= width
;
129 pios
[pio_num
].iot
= iot
;
133 pios
[pio_num
].sh_dir
= 0;
134 pios
[pio_num
].sh_data
= readl(base
+ ALTERA_PIO_DATA
);
138 pios
[pio_num
].sh_dir
= 0xffffffff & ((1 << width
) - 1);
139 pios
[pio_num
].sh_data
= rstval
;
142 /* bidir, tri-state */
143 pios
[pio_num
].sh_dir
= readl(base
+ ALTERA_PIO_DIR
);
144 pios
[pio_num
].sh_data
= readl(base
+ ALTERA_PIO_DATA
);
149 pios
[pio_num
].negmask
= negmask
& ((1 << width
) - 1);
150 pios
[pio_num
].gidx
= altera_pio_gpio_init(&pios
[pio_num
], width
);
151 if (pios
[pio_num
].gidx
< 0)
153 strncpy(pios
[pio_num
].label
, label
, GPIO_LABEL_SIZE
);
157 void altera_pio_info(void)
164 for (i
= 0; i
< pio_num
; ++i
) {
165 printf("Altera PIO % 2d, @0x%08x, "
166 "width: %u, label: %s\n",
167 i
, pios
[i
].base
, pios
[i
].width
, pios
[i
].label
);
169 for (j
= gidx
; j
< (gidx
+ pios
[i
].width
); ++j
) {
170 mask
= 1 << (j
- gidx
);
171 printf("\tGPIO % 4d: %s %s [%c] %s\n",
173 gpios
[j
].pio
->sh_dir
& mask
? "out" : " in",
174 gpio_get_value(gpios
[j
].num
) ? "set" : "clr",
175 altera_pio_is_gidx_used(j
) ? 'x' : ' ',
182 int gpio_request(unsigned gpio
, const char *label
)
184 int gidx
= altera_pio_gidx(gpio
);
187 if (altera_pio_is_gidx_used(gidx
))
190 altera_pio_use_gidx(gidx
, label
);
194 int gpio_free(unsigned gpio
)
196 int gidx
= altera_pio_gidx(gpio
);
199 if (!altera_pio_is_gidx_used(gidx
))
202 altera_pio_unuse_gidx(gidx
);
206 int gpio_direction_input(unsigned gpio
)
209 struct altera_pio
*pio
;
211 pio
= altera_pio_get_and_mask(gpio
, &mask
);
217 writel(pio
->sh_dir
&= ~mask
, pio
->base
+ ALTERA_PIO_DIR
);
221 int gpio_direction_output(unsigned gpio
, int value
)
224 struct altera_pio
*pio
;
226 pio
= altera_pio_get_and_mask(gpio
, &mask
);
232 value
= (pio
->negmask
& mask
) ? !value
: value
;
234 pio
->sh_data
|= mask
;
236 pio
->sh_data
&= ~mask
;
237 writel(pio
->sh_data
, pio
->base
+ ALTERA_PIO_DATA
);
238 writel(pio
->sh_dir
|= mask
, pio
->base
+ ALTERA_PIO_DIR
);
242 int gpio_get_value(unsigned gpio
)
245 struct altera_pio
*pio
;
248 pio
= altera_pio_get_and_mask(gpio
, &mask
);
252 if ((pio
->sh_dir
& mask
) || (pio
->iot
== 'o'))
253 val
= pio
->sh_data
& mask
;
255 val
= readl(pio
->base
+ ALTERA_PIO_DATA
) & mask
;
256 return (pio
->negmask
& mask
) ? !val
: val
;
259 void gpio_set_value(unsigned gpio
, int value
)
262 struct altera_pio
*pio
;
264 pio
= altera_pio_get_and_mask(gpio
, &mask
);
270 value
= (pio
->negmask
& mask
) ? !value
: value
;
272 pio
->sh_data
|= mask
;
274 pio
->sh_data
&= ~mask
;
275 writel(pio
->sh_data
, pio
->base
+ ALTERA_PIO_DATA
);
279 int gpio_is_valid(int number
)
281 int gidx
= altera_pio_gidx(number
);