1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com>
5 * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de)
7 * Copyright (C) 2005 HP Labs
16 #include <linux/sizes.h>
18 #include <asm/arch/hardware.h>
19 #include <asm/arch/at91_pio.h>
21 #define GPIO_PER_BANK 32
23 static struct at91_port *at91_pio_get_port(unsigned port)
27 return (struct at91_port *)ATMEL_BASE_PIOA;
29 return (struct at91_port *)ATMEL_BASE_PIOB;
31 return (struct at91_port *)ATMEL_BASE_PIOC;
32 #if (ATMEL_PIO_PORTS > 3)
34 return (struct at91_port *)ATMEL_BASE_PIOD;
35 #if (ATMEL_PIO_PORTS > 4)
37 return (struct at91_port *)ATMEL_BASE_PIOE;
41 printf("Error: at91_gpio: Fail to get PIO base!\n");
46 static void at91_set_port_pullup(struct at91_port *at91_port, unsigned offset,
53 writel(mask, &at91_port->puer);
55 writel(mask, &at91_port->pudr);
56 writel(mask, &at91_port->per);
59 int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
61 struct at91_port *at91_port = at91_pio_get_port(port);
63 if (at91_port && (pin < GPIO_PER_BANK))
64 at91_set_port_pullup(at91_port, pin, use_pullup);
70 * mux the pin to the "GPIO" peripheral role.
72 int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
74 struct at91_port *at91_port = at91_pio_get_port(port);
77 if (at91_port && (pin < GPIO_PER_BANK)) {
79 writel(mask, &at91_port->idr);
80 at91_set_pio_pullup(port, pin, use_pullup);
81 writel(mask, &at91_port->per);
88 * mux the pin to the "A" internal peripheral role.
90 int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
92 struct at91_port *at91_port = at91_pio_get_port(port);
95 if (at91_port && (pin < GPIO_PER_BANK)) {
97 writel(mask, &at91_port->idr);
98 at91_set_pio_pullup(port, pin, use_pullup);
99 writel(mask, &at91_port->mux.pio2.asr);
100 writel(mask, &at91_port->pdr);
107 * mux the pin to the "B" internal peripheral role.
109 int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
111 struct at91_port *at91_port = at91_pio_get_port(port);
114 if (at91_port && (pin < GPIO_PER_BANK)) {
116 writel(mask, &at91_port->idr);
117 at91_set_pio_pullup(port, pin, use_pullup);
118 writel(mask, &at91_port->mux.pio2.bsr);
119 writel(mask, &at91_port->pdr);
126 * mux the pin to the "A" internal peripheral role.
128 int at91_pio3_set_a_periph(unsigned port, unsigned pin, int use_pullup)
130 struct at91_port *at91_port = at91_pio_get_port(port);
133 if (at91_port && (pin < GPIO_PER_BANK)) {
135 writel(mask, &at91_port->idr);
136 at91_set_pio_pullup(port, pin, use_pullup);
137 writel(readl(&at91_port->mux.pio3.abcdsr1) & ~mask,
138 &at91_port->mux.pio3.abcdsr1);
139 writel(readl(&at91_port->mux.pio3.abcdsr2) & ~mask,
140 &at91_port->mux.pio3.abcdsr2);
142 writel(mask, &at91_port->pdr);
149 * mux the pin to the "B" internal peripheral role.
151 int at91_pio3_set_b_periph(unsigned port, unsigned pin, int use_pullup)
153 struct at91_port *at91_port = at91_pio_get_port(port);
156 if (at91_port && (pin < GPIO_PER_BANK)) {
158 writel(mask, &at91_port->idr);
159 at91_set_pio_pullup(port, pin, use_pullup);
160 writel(readl(&at91_port->mux.pio3.abcdsr1) | mask,
161 &at91_port->mux.pio3.abcdsr1);
162 writel(readl(&at91_port->mux.pio3.abcdsr2) & ~mask,
163 &at91_port->mux.pio3.abcdsr2);
165 writel(mask, &at91_port->pdr);
171 * mux the pin to the "C" internal peripheral role.
173 int at91_pio3_set_c_periph(unsigned port, unsigned pin, int use_pullup)
175 struct at91_port *at91_port = at91_pio_get_port(port);
178 if (at91_port && (pin < GPIO_PER_BANK)) {
180 writel(mask, &at91_port->idr);
181 at91_set_pio_pullup(port, pin, use_pullup);
182 writel(readl(&at91_port->mux.pio3.abcdsr1) & ~mask,
183 &at91_port->mux.pio3.abcdsr1);
184 writel(readl(&at91_port->mux.pio3.abcdsr2) | mask,
185 &at91_port->mux.pio3.abcdsr2);
186 writel(mask, &at91_port->pdr);
193 * mux the pin to the "D" internal peripheral role.
195 int at91_pio3_set_d_periph(unsigned port, unsigned pin, int use_pullup)
197 struct at91_port *at91_port = at91_pio_get_port(port);
200 if (at91_port && (pin < GPIO_PER_BANK)) {
202 writel(mask, &at91_port->idr);
203 at91_set_pio_pullup(port, pin, use_pullup);
204 writel(readl(&at91_port->mux.pio3.abcdsr1) | mask,
205 &at91_port->mux.pio3.abcdsr1);
206 writel(readl(&at91_port->mux.pio3.abcdsr2) | mask,
207 &at91_port->mux.pio3.abcdsr2);
208 writel(mask, &at91_port->pdr);
214 #if CONFIG_IS_ENABLED(DM_GPIO)
215 static bool at91_get_port_output(struct at91_port *at91_port, int offset)
220 val = readl(&at91_port->osr);
225 static void at91_set_port_input(struct at91_port *at91_port, int offset,
231 writel(mask, &at91_port->idr);
232 at91_set_port_pullup(at91_port, offset, use_pullup);
233 writel(mask, &at91_port->odr);
234 writel(mask, &at91_port->per);
238 * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
239 * configure it for an input.
241 int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
243 struct at91_port *at91_port = at91_pio_get_port(port);
245 if (at91_port && (pin < GPIO_PER_BANK))
246 at91_set_port_input(at91_port, pin, use_pullup);
251 static void at91_set_port_output(struct at91_port *at91_port, int offset,
257 writel(mask, &at91_port->idr);
258 writel(mask, &at91_port->pudr);
260 writel(mask, &at91_port->sodr);
262 writel(mask, &at91_port->codr);
263 writel(mask, &at91_port->oer);
264 writel(mask, &at91_port->per);
268 * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
269 * and configure it for an output.
271 int at91_set_pio_output(unsigned port, u32 pin, int value)
273 struct at91_port *at91_port = at91_pio_get_port(port);
275 if (at91_port && (pin < GPIO_PER_BANK))
276 at91_set_port_output(at91_port, pin, value);
282 * enable/disable the glitch filter. mostly used with IRQ handling.
284 int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
286 struct at91_port *at91_port = at91_pio_get_port(port);
289 if (at91_port && (pin < GPIO_PER_BANK)) {
292 writel(mask, &at91_port->ifer);
294 writel(mask, &at91_port->ifdr);
301 * enable/disable the glitch filter. mostly used with IRQ handling.
303 int at91_pio3_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
305 struct at91_port *at91_port = at91_pio_get_port(port);
308 if (at91_port && (pin < GPIO_PER_BANK)) {
311 writel(mask, &at91_port->mux.pio3.ifscdr);
312 writel(mask, &at91_port->ifer);
314 writel(mask, &at91_port->ifdr);
322 * enable/disable the debounce filter.
324 int at91_pio3_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
326 struct at91_port *at91_port = at91_pio_get_port(port);
329 if (at91_port && (pin < GPIO_PER_BANK)) {
332 writel(mask, &at91_port->mux.pio3.ifscer);
333 writel(div & PIO_SCDR_DIV, &at91_port->mux.pio3.scdr);
334 writel(mask, &at91_port->ifer);
336 writel(mask, &at91_port->ifdr);
344 * enable/disable the pull-down.
345 * If pull-up already enabled while calling the function, we disable it.
347 int at91_pio3_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
349 struct at91_port *at91_port = at91_pio_get_port(port);
352 if (at91_port && (pin < GPIO_PER_BANK)) {
355 at91_set_pio_pullup(port, pin, 0);
356 writel(mask, &at91_port->mux.pio3.ppder);
358 writel(mask, &at91_port->mux.pio3.ppddr);
364 int at91_pio3_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
366 struct at91_port *at91_port = at91_pio_get_port(port);
369 at91_pio3_set_pio_pulldown(port, pin, 0);
371 if (at91_port && (pin < GPIO_PER_BANK))
372 at91_set_port_pullup(at91_port, pin, use_pullup);
378 * disable Schmitt trigger
380 int at91_pio3_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
382 struct at91_port *at91_port = at91_pio_get_port(port);
385 if (at91_port && (pin < GPIO_PER_BANK)) {
387 writel(readl(&at91_port->schmitt) | mask,
388 &at91_port->schmitt);
395 * enable/disable the multi-driver. This is only valid for output and
396 * allows the output pin to run as an open collector output.
398 int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
400 struct at91_port *at91_port = at91_pio_get_port(port);
403 if (at91_port && (pin < GPIO_PER_BANK)) {
406 writel(mask, &at91_port->mder);
408 writel(mask, &at91_port->mddr);
414 static void at91_set_port_value(struct at91_port *at91_port, int offset,
421 writel(mask, &at91_port->sodr);
423 writel(mask, &at91_port->codr);
427 * assuming the pin is muxed as a gpio output, set its value.
429 int at91_set_pio_value(unsigned port, unsigned pin, int value)
431 struct at91_port *at91_port = at91_pio_get_port(port);
433 if (at91_port && (pin < GPIO_PER_BANK))
434 at91_set_port_value(at91_port, pin, value);
439 static int at91_get_port_value(struct at91_port *at91_port, int offset)
444 pdsr = readl(&at91_port->pdsr) & mask;
449 * read the pin's value (works even if it's not muxed as a gpio).
451 int at91_get_pio_value(unsigned port, unsigned pin)
453 struct at91_port *at91_port = at91_pio_get_port(port);
455 if (at91_port && (pin < GPIO_PER_BANK))
456 return at91_get_port_value(at91_port, pin);
461 #if !CONFIG_IS_ENABLED(DM_GPIO)
462 /* Common GPIO API */
464 int gpio_request(unsigned gpio, const char *label)
469 int gpio_free(unsigned gpio)
474 int gpio_direction_input(unsigned gpio)
476 at91_set_pio_input(at91_gpio_to_port(gpio),
477 at91_gpio_to_pin(gpio), 0);
481 int gpio_direction_output(unsigned gpio, int value)
483 at91_set_pio_output(at91_gpio_to_port(gpio),
484 at91_gpio_to_pin(gpio), value);
488 int gpio_get_value(unsigned gpio)
490 return at91_get_pio_value(at91_gpio_to_port(gpio),
491 at91_gpio_to_pin(gpio));
494 int gpio_set_value(unsigned gpio, int value)
496 at91_set_pio_value(at91_gpio_to_port(gpio),
497 at91_gpio_to_pin(gpio), value);
503 #if CONFIG_IS_ENABLED(DM_GPIO)
505 struct at91_port_priv {
506 struct at91_port *regs;
509 /* set GPIO pin 'gpio' as an input */
510 static int at91_gpio_direction_input(struct udevice *dev, unsigned offset)
512 struct at91_port_priv *port = dev_get_priv(dev);
514 at91_set_port_input(port->regs, offset, 0);
519 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
520 static int at91_gpio_direction_output(struct udevice *dev, unsigned offset,
523 struct at91_port_priv *port = dev_get_priv(dev);
525 at91_set_port_output(port->regs, offset, value);
530 /* read GPIO IN value of pin 'gpio' */
531 static int at91_gpio_get_value(struct udevice *dev, unsigned offset)
533 struct at91_port_priv *port = dev_get_priv(dev);
535 return at91_get_port_value(port->regs, offset);
538 /* write GPIO OUT value to pin 'gpio' */
539 static int at91_gpio_set_value(struct udevice *dev, unsigned offset,
542 struct at91_port_priv *port = dev_get_priv(dev);
544 at91_set_port_value(port->regs, offset, value);
549 static int at91_gpio_get_function(struct udevice *dev, unsigned offset)
551 struct at91_port_priv *port = dev_get_priv(dev);
553 /* GPIOF_FUNC is not implemented yet */
554 if (at91_get_port_output(port->regs, offset))
560 static const char *at91_get_bank_name(uint32_t base_addr)
563 case ATMEL_BASE_PIOA:
565 case ATMEL_BASE_PIOB:
567 case ATMEL_BASE_PIOC:
569 #if (ATMEL_PIO_PORTS > 3)
570 case ATMEL_BASE_PIOD:
572 #if (ATMEL_PIO_PORTS > 4)
573 case ATMEL_BASE_PIOE:
582 static const struct dm_gpio_ops gpio_at91_ops = {
583 .direction_input = at91_gpio_direction_input,
584 .direction_output = at91_gpio_direction_output,
585 .get_value = at91_gpio_get_value,
586 .set_value = at91_gpio_set_value,
587 .get_function = at91_gpio_get_function,
590 static int at91_gpio_probe(struct udevice *dev)
592 struct at91_port_priv *port = dev_get_priv(dev);
593 struct at91_port_plat *plat = dev_get_plat(dev);
594 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
598 ret = clk_get_by_index(dev, 0, &clk);
602 ret = clk_enable(&clk);
608 #if CONFIG_IS_ENABLED(OF_CONTROL)
609 plat->base_addr = dev_read_addr(dev);
611 plat->bank_name = at91_get_bank_name(plat->base_addr);
612 port->regs = (struct at91_port *)plat->base_addr;
614 uc_priv->bank_name = plat->bank_name;
615 uc_priv->gpio_count = GPIO_PER_BANK;
620 #if CONFIG_IS_ENABLED(OF_CONTROL)
621 static const struct udevice_id at91_gpio_ids[] = {
622 { .compatible = "atmel,at91rm9200-gpio" },
627 U_BOOT_DRIVER(atmel_at91rm9200_gpio) = {
628 .name = "atmel_at91rm9200_gpio",
630 #if CONFIG_IS_ENABLED(OF_CONTROL)
631 .of_match = at91_gpio_ids,
632 .plat_auto = sizeof(struct at91_port_plat),
634 .ops = &gpio_at91_ops,
635 .probe = at91_gpio_probe,
636 .priv_auto = sizeof(struct at91_port_priv),