2 * ADI GPIO1 Abstraction Layer
3 * Support BF50x, BF51x, BF52x, BF53x and BF561 only.
5 * Copyright 2006-2010 Analog Devices Inc.
7 * Licensed under the GPL-2 or later
11 #include <asm/errno.h>
13 #include <asm/portmux.h>
15 #if ANOMALY_05000311 || ANOMALY_05000323
18 AWA_data_clear = SYSCR,
22 AWA_maska_clear = UART_SCR,
23 AWA_maska_set = UART_SCR,
24 AWA_maska_toggle = UART_SCR,
25 AWA_maskb = UART_GCTL,
26 AWA_maskb_clear = UART_GCTL,
27 AWA_maskb_set = UART_GCTL,
28 AWA_maskb_toggle = UART_GCTL,
29 AWA_dir = SPORT1_STAT,
30 AWA_polar = SPORT1_STAT,
31 AWA_edge = SPORT1_STAT,
32 AWA_both = SPORT1_STAT,
34 AWA_inen = TIMER_ENABLE,
35 #elif ANOMALY_05000323
36 AWA_inen = DMA1_1_CONFIG,
39 /* Anomaly Workaround */
40 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
42 #define AWA_DUMMY_READ(...) do { } while (0)
45 static struct gpio_port_t * const gpio_array[] = {
46 #if defined(BF533_FAMILY)
47 (struct gpio_port_t *) FIO_FLAG_D,
48 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) \
49 || defined(BF538_FAMILY) || defined(CONFIG_BF50x)
50 (struct gpio_port_t *) PORTFIO,
51 # if !defined(BF538_FAMILY)
52 (struct gpio_port_t *) PORTGIO,
53 (struct gpio_port_t *) PORTHIO,
55 #elif defined(BF561_FAMILY)
56 (struct gpio_port_t *) FIO0_FLAG_D,
57 (struct gpio_port_t *) FIO1_FLAG_D,
58 (struct gpio_port_t *) FIO2_FLAG_D,
60 # error no gpio arrays defined
64 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) || \
66 static unsigned short * const port_fer[] = {
67 (unsigned short *) PORTF_FER,
68 (unsigned short *) PORTG_FER,
69 (unsigned short *) PORTH_FER,
72 # if !defined(BF537_FAMILY)
73 static unsigned short * const port_mux[] = {
74 (unsigned short *) PORTF_MUX,
75 (unsigned short *) PORTG_MUX,
76 (unsigned short *) PORTH_MUX,
80 u8 pmux_offset[][16] = {
81 # if defined(CONFIG_BF52x)
82 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
83 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
84 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
85 # elif defined(CONFIG_BF51x)
86 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
87 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
88 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
93 #elif defined(BF538_FAMILY)
94 static unsigned short * const port_fer[] = {
95 (unsigned short *) PORTCIO_FER,
96 (unsigned short *) PORTDIO_FER,
97 (unsigned short *) PORTEIO_FER,
101 #ifdef CONFIG_BFIN_GPIO_TRACK
102 #define RESOURCE_LABEL_SIZE 16
104 static struct str_ident {
105 char name[RESOURCE_LABEL_SIZE];
106 } str_ident[MAX_RESOURCES];
108 static void gpio_error(unsigned gpio)
110 printf("bfin-gpio: GPIO %d wasn't requested!\n", gpio);
113 static void set_label(unsigned short ident, const char *label)
116 strncpy(str_ident[ident].name, label,
117 RESOURCE_LABEL_SIZE);
118 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
122 static char *get_label(unsigned short ident)
124 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
127 static int cmp_label(unsigned short ident, const char *label)
130 printf("bfin-gpio: please provide none-null label\n");
133 return strcmp(str_ident[ident].name, label);
138 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
139 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
140 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
141 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
142 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
144 #define is_reserved(m, i, e) (!(e))
145 #define reserve(m, i)
146 #define unreserve(m, i)
147 #define DECLARE_RESERVED_MAP(m, c)
148 #define gpio_error(gpio)
149 #define set_label(...)
150 #define get_label(...) ""
151 #define cmp_label(...) 1
154 DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
155 DECLARE_RESERVED_MAP(peri, gpio_bank(MAX_RESOURCES));
157 inline int check_gpio(unsigned gpio)
159 if (gpio >= MAX_BLACKFIN_GPIOS)
164 static void port_setup(unsigned gpio, unsigned short usage)
166 #if defined(BF538_FAMILY)
168 * BF538/9 Port C,D and E are special.
169 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
170 * Regular PORT F GPIOs are handled here, CDE are exclusively
174 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
177 gpio -= MAX_BLACKFIN_GPIOS;
179 if (usage == GPIO_USAGE)
180 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
182 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
187 if (check_gpio(gpio))
190 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) || \
191 defined(CONFIG_BF50x)
192 if (usage == GPIO_USAGE)
193 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
195 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
203 unsigned short offset;
205 {.res = P_PPI0_D13, .offset = 11},
206 {.res = P_PPI0_D14, .offset = 11},
207 {.res = P_PPI0_D15, .offset = 11},
208 {.res = P_SPORT1_TFS, .offset = 11},
209 {.res = P_SPORT1_TSCLK, .offset = 11},
210 {.res = P_SPORT1_DTPRI, .offset = 11},
211 {.res = P_PPI0_D10, .offset = 10},
212 {.res = P_PPI0_D11, .offset = 10},
213 {.res = P_PPI0_D12, .offset = 10},
214 {.res = P_SPORT1_RSCLK, .offset = 10},
215 {.res = P_SPORT1_RFS, .offset = 10},
216 {.res = P_SPORT1_DRPRI, .offset = 10},
217 {.res = P_PPI0_D8, .offset = 9},
218 {.res = P_PPI0_D9, .offset = 9},
219 {.res = P_SPORT1_DRSEC, .offset = 9},
220 {.res = P_SPORT1_DTSEC, .offset = 9},
221 {.res = P_TMR2, .offset = 8},
222 {.res = P_PPI0_FS3, .offset = 8},
223 {.res = P_TMR3, .offset = 7},
224 {.res = P_SPI0_SSEL4, .offset = 7},
225 {.res = P_TMR4, .offset = 6},
226 {.res = P_SPI0_SSEL5, .offset = 6},
227 {.res = P_TMR5, .offset = 5},
228 {.res = P_SPI0_SSEL6, .offset = 5},
229 {.res = P_UART1_RX, .offset = 4},
230 {.res = P_UART1_TX, .offset = 4},
231 {.res = P_TMR6, .offset = 4},
232 {.res = P_TMR7, .offset = 4},
233 {.res = P_UART0_RX, .offset = 3},
234 {.res = P_UART0_TX, .offset = 3},
235 {.res = P_DMAR0, .offset = 3},
236 {.res = P_DMAR1, .offset = 3},
237 {.res = P_SPORT0_DTSEC, .offset = 1},
238 {.res = P_SPORT0_DRSEC, .offset = 1},
239 {.res = P_CAN0_RX, .offset = 1},
240 {.res = P_CAN0_TX, .offset = 1},
241 {.res = P_SPI0_SSEL7, .offset = 1},
242 {.res = P_SPORT0_TFS, .offset = 0},
243 {.res = P_SPORT0_DTPRI, .offset = 0},
244 {.res = P_SPI0_SSEL2, .offset = 0},
245 {.res = P_SPI0_SSEL3, .offset = 0},
248 static void portmux_setup(unsigned short per)
250 u16 y, offset, muxreg;
251 u16 function = P_FUNCT2MUX(per);
253 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
254 if (port_mux_lut[y].res == per) {
256 /* SET PORTMUX REG */
258 offset = port_mux_lut[y].offset;
259 muxreg = bfin_read_PORT_MUX();
262 muxreg &= ~(1 << offset);
266 muxreg |= (function << offset);
267 bfin_write_PORT_MUX(muxreg);
271 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
272 inline void portmux_setup(unsigned short per)
274 u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
275 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
277 pmux = *port_mux[gpio_bank(ident)];
278 pmux &= ~(3 << offset);
279 pmux |= (function & 3) << offset;
280 *port_mux[gpio_bank(ident)] = pmux;
284 # define portmux_setup(...) do { } while (0)
287 /***********************************************************
289 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
292 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
295 * DESCRIPTION: These functions abstract direct register access
296 * to Blackfin processor General Purpose
299 * CAUTION: These functions do not belong to the GPIO Driver API
300 *************************************************************
301 * MODIFICATION HISTORY :
302 **************************************************************/
304 /* Set a specific bit */
306 #define SET_GPIO(name) \
307 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
309 unsigned long flags; \
310 local_irq_save(flags); \
312 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
314 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
315 AWA_DUMMY_READ(name); \
316 local_irq_restore(flags); \
319 SET_GPIO(dir) /* set_gpio_dir() */
320 SET_GPIO(inen) /* set_gpio_inen() */
321 SET_GPIO(polar) /* set_gpio_polar() */
322 SET_GPIO(edge) /* set_gpio_edge() */
323 SET_GPIO(both) /* set_gpio_both() */
326 #define SET_GPIO_SC(name) \
327 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
329 unsigned long flags; \
330 if (ANOMALY_05000311 || ANOMALY_05000323) \
331 local_irq_save(flags); \
333 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
335 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
336 if (ANOMALY_05000311 || ANOMALY_05000323) { \
337 AWA_DUMMY_READ(name); \
338 local_irq_restore(flags); \
346 void set_gpio_toggle(unsigned gpio)
349 if (ANOMALY_05000311 || ANOMALY_05000323)
350 local_irq_save(flags);
351 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
352 if (ANOMALY_05000311 || ANOMALY_05000323) {
353 AWA_DUMMY_READ(toggle);
354 local_irq_restore(flags);
358 /* Set current PORT date (16-bit word) */
360 #define SET_GPIO_P(name) \
361 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
363 unsigned long flags; \
364 if (ANOMALY_05000311 || ANOMALY_05000323) \
365 local_irq_save(flags); \
366 gpio_array[gpio_bank(gpio)]->name = arg; \
367 if (ANOMALY_05000311 || ANOMALY_05000323) { \
368 AWA_DUMMY_READ(name); \
369 local_irq_restore(flags); \
382 /* Get a specific bit */
383 #define GET_GPIO(name) \
384 unsigned short get_gpio_ ## name(unsigned gpio) \
386 unsigned long flags; \
387 unsigned short ret; \
388 if (ANOMALY_05000311 || ANOMALY_05000323) \
389 local_irq_save(flags); \
390 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
391 if (ANOMALY_05000311 || ANOMALY_05000323) { \
392 AWA_DUMMY_READ(name); \
393 local_irq_restore(flags); \
407 /* Get current PORT date (16-bit word) */
409 #define GET_GPIO_P(name) \
410 unsigned short get_gpiop_ ## name(unsigned gpio) \
412 unsigned long flags; \
413 unsigned short ret; \
414 if (ANOMALY_05000311 || ANOMALY_05000323) \
415 local_irq_save(flags); \
416 ret = (gpio_array[gpio_bank(gpio)]->name); \
417 if (ANOMALY_05000311 || ANOMALY_05000323) { \
418 AWA_DUMMY_READ(name); \
419 local_irq_restore(flags); \
433 /***********************************************************
435 * FUNCTIONS: Blackfin Peripheral Resource Allocation
439 * per Peripheral Identifier
442 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
445 *************************************************************
446 * MODIFICATION HISTORY :
447 **************************************************************/
449 int peripheral_request(unsigned short per, const char *label)
451 unsigned short ident = P_IDENT(per);
454 * Don't cares are pins with only one dedicated function
457 if (per & P_DONTCARE)
460 if (!(per & P_DEFINED))
463 BUG_ON(ident >= MAX_RESOURCES);
465 /* If a pin can be muxed as either GPIO or peripheral, make
466 * sure it is not already a GPIO pin when we request it.
468 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
469 printf("%s: Peripheral %d is already reserved as GPIO by %s !\n",
470 __func__, ident, get_label(ident));
474 if (unlikely(is_reserved(peri, ident, 1))) {
477 * Pin functions like AMC address strobes my
478 * be requested and used by several drivers
481 if (!(per & P_MAYSHARE)) {
483 * Allow that the identical pin function can
484 * be requested from the same driver twice
487 if (cmp_label(ident, label) == 0)
490 printf("%s: Peripheral %d function %d is already reserved by %s !\n",
491 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
497 reserve(peri, ident);
500 port_setup(ident, PERIPHERAL_USAGE);
502 set_label(ident, label);
507 int peripheral_request_list(const unsigned short per[], const char *label)
512 for (cnt = 0; per[cnt] != 0; cnt++) {
514 ret = peripheral_request(per[cnt], label);
517 for ( ; cnt > 0; cnt--)
518 peripheral_free(per[cnt - 1]);
527 void peripheral_free(unsigned short per)
529 unsigned short ident = P_IDENT(per);
531 if (per & P_DONTCARE)
534 if (!(per & P_DEFINED))
537 if (unlikely(!is_reserved(peri, ident, 0)))
540 if (!(per & P_MAYSHARE))
541 port_setup(ident, GPIO_USAGE);
543 unreserve(peri, ident);
545 set_label(ident, "free");
548 void peripheral_free_list(const unsigned short per[])
551 for (cnt = 0; per[cnt] != 0; cnt++)
552 peripheral_free(per[cnt]);
555 /***********************************************************
557 * FUNCTIONS: Blackfin GPIO Driver
560 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
563 * DESCRIPTION: Blackfin GPIO Driver API
566 *************************************************************
567 * MODIFICATION HISTORY :
568 **************************************************************/
570 int gpio_request(unsigned gpio, const char *label)
572 if (check_gpio(gpio) < 0)
576 * Allow that the identical GPIO can
577 * be requested from the same driver twice
578 * Do nothing and return -
581 if (cmp_label(gpio, label) == 0)
584 if (unlikely(is_reserved(gpio, gpio, 1))) {
585 printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
586 gpio, get_label(gpio));
589 if (unlikely(is_reserved(peri, gpio, 1))) {
590 printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
591 gpio, get_label(gpio));
594 else { /* Reset POLAR setting when acquiring a gpio for the first time */
595 set_gpio_polar(gpio, 0);
599 set_label(gpio, label);
601 port_setup(gpio, GPIO_USAGE);
606 int gpio_free(unsigned gpio)
608 if (check_gpio(gpio) < 0)
611 if (unlikely(!is_reserved(gpio, gpio, 0))) {
616 unreserve(gpio, gpio);
618 set_label(gpio, "free");
623 #ifdef ADI_SPECIAL_GPIO_BANKS
624 DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
626 int special_gpio_request(unsigned gpio, const char *label)
629 * Allow that the identical GPIO can
630 * be requested from the same driver twice
631 * Do nothing and return -
634 if (cmp_label(gpio, label) == 0)
637 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
638 printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
639 gpio, get_label(gpio));
642 if (unlikely(is_reserved(peri, gpio, 1))) {
643 printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
644 gpio, get_label(gpio));
649 reserve(special_gpio, gpio);
652 set_label(gpio, label);
653 port_setup(gpio, GPIO_USAGE);
658 void special_gpio_free(unsigned gpio)
660 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
665 reserve(special_gpio, gpio);
667 set_label(gpio, "free");
671 static inline void __gpio_direction_input(unsigned gpio)
673 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
674 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
677 int gpio_direction_input(unsigned gpio)
681 if (!is_reserved(gpio, gpio, 0)) {
686 local_irq_save(flags);
687 __gpio_direction_input(gpio);
688 AWA_DUMMY_READ(inen);
689 local_irq_restore(flags);
694 int gpio_set_value(unsigned gpio, int arg)
697 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
699 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
704 int gpio_direction_output(unsigned gpio, int value)
708 if (!is_reserved(gpio, gpio, 0)) {
713 local_irq_save(flags);
715 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
716 gpio_set_value(gpio, value);
717 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
720 local_irq_restore(flags);
725 int gpio_get_value(unsigned gpio)
729 if (unlikely(get_gpio_edge(gpio))) {
731 local_irq_save(flags);
732 set_gpio_edge(gpio, 0);
733 ret = get_gpio_data(gpio);
734 set_gpio_edge(gpio, 1);
735 local_irq_restore(flags);
738 return get_gpio_data(gpio);
741 /* If we are booting from SPI and our board lacks a strong enough pull up,
742 * the core can reset and execute the bootrom faster than the resistor can
743 * pull the signal logically high. To work around this (common) error in
744 * board design, we explicitly set the pin back to GPIO mode, force /CS
745 * high, and wait for the electrons to do their thing.
747 * This function only makes sense to be called from reset code, but it
748 * lives here as we need to force all the GPIO states w/out going through
749 * BUG() checks and such.
751 void bfin_reset_boot_spi_cs(unsigned short pin)
753 unsigned short gpio = P_IDENT(pin);
754 port_setup(gpio, GPIO_USAGE);
755 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
756 AWA_DUMMY_READ(data_set);
760 void gpio_labels(void)
764 for (c = 0; c < MAX_RESOURCES; c++) {
765 gpio = is_reserved(gpio, c, 1);
766 if (!check_gpio(c) && gpio)
767 printf("GPIO_%d:\t%s\tGPIO %s\n", c,
769 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
770 else if (is_reserved(peri, c, 1))
771 printf("GPIO_%d:\t%s\tPeripheral\n", c, get_label(c));