2 * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com>
4 * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de)
6 * Copyright (C) 2005 HP Labs
8 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/sizes.h>
17 #include <asm/arch/hardware.h>
18 #include <asm/arch/at91_pio.h>
20 #define GPIO_PER_BANK 32
22 static struct at91_port
*at91_pio_get_port(unsigned port
)
26 return (struct at91_port
*)ATMEL_BASE_PIOA
;
28 return (struct at91_port
*)ATMEL_BASE_PIOB
;
30 return (struct at91_port
*)ATMEL_BASE_PIOC
;
31 #if (ATMEL_PIO_PORTS > 3)
33 return (struct at91_port
*)ATMEL_BASE_PIOD
;
34 #if (ATMEL_PIO_PORTS > 4)
36 return (struct at91_port
*)ATMEL_BASE_PIOE
;
40 printf("Error: at91_gpio: Fail to get PIO base!\n");
45 static void at91_set_port_pullup(struct at91_port
*at91_port
, unsigned offset
,
52 writel(mask
, &at91_port
->puer
);
54 writel(mask
, &at91_port
->pudr
);
55 writel(mask
, &at91_port
->per
);
58 int at91_set_pio_pullup(unsigned port
, unsigned pin
, int use_pullup
)
60 struct at91_port
*at91_port
= at91_pio_get_port(port
);
62 if (at91_port
&& (pin
< GPIO_PER_BANK
))
63 at91_set_port_pullup(at91_port
, pin
, use_pullup
);
69 * mux the pin to the "GPIO" peripheral role.
71 int at91_set_pio_periph(unsigned port
, unsigned pin
, int use_pullup
)
73 struct at91_port
*at91_port
= at91_pio_get_port(port
);
76 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
78 writel(mask
, &at91_port
->idr
);
79 at91_set_pio_pullup(port
, pin
, use_pullup
);
80 writel(mask
, &at91_port
->per
);
87 * mux the pin to the "A" internal peripheral role.
89 int at91_set_a_periph(unsigned port
, unsigned pin
, int use_pullup
)
91 struct at91_port
*at91_port
= at91_pio_get_port(port
);
94 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
96 writel(mask
, &at91_port
->idr
);
97 at91_set_pio_pullup(port
, pin
, use_pullup
);
98 #if defined(CPU_HAS_PIO3)
99 writel(readl(&at91_port
->abcdsr1
) & ~mask
,
100 &at91_port
->abcdsr1
);
101 writel(readl(&at91_port
->abcdsr2
) & ~mask
,
102 &at91_port
->abcdsr2
);
104 writel(mask
, &at91_port
->asr
);
106 writel(mask
, &at91_port
->pdr
);
113 * mux the pin to the "B" internal peripheral role.
115 int at91_set_b_periph(unsigned port
, unsigned pin
, int use_pullup
)
117 struct at91_port
*at91_port
= at91_pio_get_port(port
);
120 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
122 writel(mask
, &at91_port
->idr
);
123 at91_set_pio_pullup(port
, pin
, use_pullup
);
124 #if defined(CPU_HAS_PIO3)
125 writel(readl(&at91_port
->abcdsr1
) | mask
,
126 &at91_port
->abcdsr1
);
127 writel(readl(&at91_port
->abcdsr2
) & ~mask
,
128 &at91_port
->abcdsr2
);
130 writel(mask
, &at91_port
->bsr
);
132 writel(mask
, &at91_port
->pdr
);
138 #if defined(CPU_HAS_PIO3)
140 * mux the pin to the "C" internal peripheral role.
142 int at91_set_c_periph(unsigned port
, unsigned pin
, int use_pullup
)
144 struct at91_port
*at91_port
= at91_pio_get_port(port
);
147 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
149 writel(mask
, &at91_port
->idr
);
150 at91_set_pio_pullup(port
, pin
, use_pullup
);
151 writel(readl(&at91_port
->abcdsr1
) & ~mask
,
152 &at91_port
->abcdsr1
);
153 writel(readl(&at91_port
->abcdsr2
) | mask
,
154 &at91_port
->abcdsr2
);
155 writel(mask
, &at91_port
->pdr
);
162 * mux the pin to the "D" internal peripheral role.
164 int at91_set_d_periph(unsigned port
, unsigned pin
, int use_pullup
)
166 struct at91_port
*at91_port
= at91_pio_get_port(port
);
169 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
171 writel(mask
, &at91_port
->idr
);
172 at91_set_pio_pullup(port
, pin
, use_pullup
);
173 writel(readl(&at91_port
->abcdsr1
) | mask
,
174 &at91_port
->abcdsr1
);
175 writel(readl(&at91_port
->abcdsr2
) | mask
,
176 &at91_port
->abcdsr2
);
177 writel(mask
, &at91_port
->pdr
);
184 #ifdef CONFIG_DM_GPIO
185 static bool at91_get_port_output(struct at91_port
*at91_port
, int offset
)
190 val
= readl(&at91_port
->osr
);
195 static void at91_set_port_input(struct at91_port
*at91_port
, int offset
,
201 writel(mask
, &at91_port
->idr
);
202 at91_set_port_pullup(at91_port
, offset
, use_pullup
);
203 writel(mask
, &at91_port
->odr
);
204 writel(mask
, &at91_port
->per
);
208 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
209 * configure it for an input.
211 int at91_set_pio_input(unsigned port
, u32 pin
, int use_pullup
)
213 struct at91_port
*at91_port
= at91_pio_get_port(port
);
215 if (at91_port
&& (pin
< GPIO_PER_BANK
))
216 at91_set_port_input(at91_port
, pin
, use_pullup
);
221 static void at91_set_port_output(struct at91_port
*at91_port
, int offset
,
227 writel(mask
, &at91_port
->idr
);
228 writel(mask
, &at91_port
->pudr
);
230 writel(mask
, &at91_port
->sodr
);
232 writel(mask
, &at91_port
->codr
);
233 writel(mask
, &at91_port
->oer
);
234 writel(mask
, &at91_port
->per
);
238 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
239 * and configure it for an output.
241 int at91_set_pio_output(unsigned port
, u32 pin
, int value
)
243 struct at91_port
*at91_port
= at91_pio_get_port(port
);
245 if (at91_port
&& (pin
< GPIO_PER_BANK
))
246 at91_set_port_output(at91_port
, pin
, value
);
252 * enable/disable the glitch filter. mostly used with IRQ handling.
254 int at91_set_pio_deglitch(unsigned port
, unsigned pin
, int is_on
)
256 struct at91_port
*at91_port
= at91_pio_get_port(port
);
259 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
262 #if defined(CPU_HAS_PIO3)
263 writel(mask
, &at91_port
->ifscdr
);
265 writel(mask
, &at91_port
->ifer
);
267 writel(mask
, &at91_port
->ifdr
);
274 #if defined(CPU_HAS_PIO3)
276 * enable/disable the debounce filter.
278 int at91_set_pio_debounce(unsigned port
, unsigned pin
, int is_on
, int div
)
280 struct at91_port
*at91_port
= at91_pio_get_port(port
);
283 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
286 writel(mask
, &at91_port
->ifscer
);
287 writel(div
& PIO_SCDR_DIV
, &at91_port
->scdr
);
288 writel(mask
, &at91_port
->ifer
);
290 writel(mask
, &at91_port
->ifdr
);
298 * enable/disable the pull-down.
299 * If pull-up already enabled while calling the function, we disable it.
301 int at91_set_pio_pulldown(unsigned port
, unsigned pin
, int is_on
)
303 struct at91_port
*at91_port
= at91_pio_get_port(port
);
306 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
308 writel(mask
, &at91_port
->pudr
);
310 writel(mask
, &at91_port
->ppder
);
312 writel(mask
, &at91_port
->ppddr
);
319 * disable Schmitt trigger
321 int at91_set_pio_disable_schmitt_trig(unsigned port
, unsigned pin
)
323 struct at91_port
*at91_port
= at91_pio_get_port(port
);
326 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
328 writel(readl(&at91_port
->schmitt
) | mask
,
329 &at91_port
->schmitt
);
337 * enable/disable the multi-driver. This is only valid for output and
338 * allows the output pin to run as an open collector output.
340 int at91_set_pio_multi_drive(unsigned port
, unsigned pin
, int is_on
)
342 struct at91_port
*at91_port
= at91_pio_get_port(port
);
345 if (at91_port
&& (pin
< GPIO_PER_BANK
)) {
348 writel(mask
, &at91_port
->mder
);
350 writel(mask
, &at91_port
->mddr
);
356 static void at91_set_port_value(struct at91_port
*at91_port
, int offset
,
363 writel(mask
, &at91_port
->sodr
);
365 writel(mask
, &at91_port
->codr
);
369 * assuming the pin is muxed as a gpio output, set its value.
371 int at91_set_pio_value(unsigned port
, unsigned pin
, int value
)
373 struct at91_port
*at91_port
= at91_pio_get_port(port
);
375 if (at91_port
&& (pin
< GPIO_PER_BANK
))
376 at91_set_port_value(at91_port
, pin
, value
);
381 static int at91_get_port_value(struct at91_port
*at91_port
, int offset
)
386 pdsr
= readl(&at91_port
->pdsr
) & mask
;
391 * read the pin's value (works even if it's not muxed as a gpio).
393 int at91_get_pio_value(unsigned port
, unsigned pin
)
395 struct at91_port
*at91_port
= at91_pio_get_port(port
);
397 if (at91_port
&& (pin
< GPIO_PER_BANK
))
398 return at91_get_port_value(at91_port
, pin
);
403 #ifndef CONFIG_DM_GPIO
404 /* Common GPIO API */
406 int gpio_request(unsigned gpio
, const char *label
)
411 int gpio_free(unsigned gpio
)
416 int gpio_direction_input(unsigned gpio
)
418 at91_set_pio_input(at91_gpio_to_port(gpio
),
419 at91_gpio_to_pin(gpio
), 0);
423 int gpio_direction_output(unsigned gpio
, int value
)
425 at91_set_pio_output(at91_gpio_to_port(gpio
),
426 at91_gpio_to_pin(gpio
), value
);
430 int gpio_get_value(unsigned gpio
)
432 return at91_get_pio_value(at91_gpio_to_port(gpio
),
433 at91_gpio_to_pin(gpio
));
436 int gpio_set_value(unsigned gpio
, int value
)
438 at91_set_pio_value(at91_gpio_to_port(gpio
),
439 at91_gpio_to_pin(gpio
), value
);
445 #ifdef CONFIG_DM_GPIO
447 struct at91_port_priv
{
448 struct at91_port
*regs
;
451 /* set GPIO pin 'gpio' as an input */
452 static int at91_gpio_direction_input(struct udevice
*dev
, unsigned offset
)
454 struct at91_port_priv
*port
= dev_get_priv(dev
);
456 at91_set_port_input(port
->regs
, offset
, 0);
461 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
462 static int at91_gpio_direction_output(struct udevice
*dev
, unsigned offset
,
465 struct at91_port_priv
*port
= dev_get_priv(dev
);
467 at91_set_port_output(port
->regs
, offset
, value
);
472 /* read GPIO IN value of pin 'gpio' */
473 static int at91_gpio_get_value(struct udevice
*dev
, unsigned offset
)
475 struct at91_port_priv
*port
= dev_get_priv(dev
);
477 return at91_get_port_value(port
->regs
, offset
);
480 /* write GPIO OUT value to pin 'gpio' */
481 static int at91_gpio_set_value(struct udevice
*dev
, unsigned offset
,
484 struct at91_port_priv
*port
= dev_get_priv(dev
);
486 at91_set_port_value(port
->regs
, offset
, value
);
491 static int at91_gpio_get_function(struct udevice
*dev
, unsigned offset
)
493 struct at91_port_priv
*port
= dev_get_priv(dev
);
495 /* GPIOF_FUNC is not implemented yet */
496 if (at91_get_port_output(port
->regs
, offset
))
502 static const struct dm_gpio_ops gpio_at91_ops
= {
503 .direction_input
= at91_gpio_direction_input
,
504 .direction_output
= at91_gpio_direction_output
,
505 .get_value
= at91_gpio_get_value
,
506 .set_value
= at91_gpio_set_value
,
507 .get_function
= at91_gpio_get_function
,
510 static int at91_gpio_probe(struct udevice
*dev
)
512 struct at91_port_priv
*port
= dev_get_priv(dev
);
513 struct at91_port_platdata
*plat
= dev_get_platdata(dev
);
514 struct gpio_dev_priv
*uc_priv
= dev
->uclass_priv
;
516 uc_priv
->bank_name
= plat
->bank_name
;
517 uc_priv
->gpio_count
= GPIO_PER_BANK
;
518 port
->regs
= (struct at91_port
*)plat
->base_addr
;
523 U_BOOT_DRIVER(gpio_at91
) = {
526 .ops
= &gpio_at91_ops
,
527 .probe
= at91_gpio_probe
,
528 .priv_auto_alloc_size
= sizeof(struct at91_port_priv
),