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, mask;
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();
266 muxreg &= ~(mask << offset);
267 muxreg |= ((function & mask) << offset);
268 bfin_write_PORT_MUX(muxreg);
272 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
273 inline void portmux_setup(unsigned short per)
275 u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
276 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
278 pmux = *port_mux[gpio_bank(ident)];
279 pmux &= ~(3 << offset);
280 pmux |= (function & 3) << offset;
281 *port_mux[gpio_bank(ident)] = pmux;
285 # define portmux_setup(...) do { } while (0)
288 /***********************************************************
290 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
293 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
296 * DESCRIPTION: These functions abstract direct register access
297 * to Blackfin processor General Purpose
300 * CAUTION: These functions do not belong to the GPIO Driver API
301 *************************************************************
302 * MODIFICATION HISTORY :
303 **************************************************************/
305 /* Set a specific bit */
307 #define SET_GPIO(name) \
308 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
310 unsigned long flags; \
311 local_irq_save(flags); \
313 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
315 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
316 AWA_DUMMY_READ(name); \
317 local_irq_restore(flags); \
320 SET_GPIO(dir) /* set_gpio_dir() */
321 SET_GPIO(inen) /* set_gpio_inen() */
322 SET_GPIO(polar) /* set_gpio_polar() */
323 SET_GPIO(edge) /* set_gpio_edge() */
324 SET_GPIO(both) /* set_gpio_both() */
327 #define SET_GPIO_SC(name) \
328 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
330 unsigned long flags; \
331 if (ANOMALY_05000311 || ANOMALY_05000323) \
332 local_irq_save(flags); \
334 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
336 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
337 if (ANOMALY_05000311 || ANOMALY_05000323) { \
338 AWA_DUMMY_READ(name); \
339 local_irq_restore(flags); \
347 void set_gpio_toggle(unsigned gpio)
350 if (ANOMALY_05000311 || ANOMALY_05000323)
351 local_irq_save(flags);
352 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
353 if (ANOMALY_05000311 || ANOMALY_05000323) {
354 AWA_DUMMY_READ(toggle);
355 local_irq_restore(flags);
359 /* Set current PORT date (16-bit word) */
361 #define SET_GPIO_P(name) \
362 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
364 unsigned long flags; \
365 if (ANOMALY_05000311 || ANOMALY_05000323) \
366 local_irq_save(flags); \
367 gpio_array[gpio_bank(gpio)]->name = arg; \
368 if (ANOMALY_05000311 || ANOMALY_05000323) { \
369 AWA_DUMMY_READ(name); \
370 local_irq_restore(flags); \
383 /* Get a specific bit */
384 #define GET_GPIO(name) \
385 unsigned short get_gpio_ ## name(unsigned gpio) \
387 unsigned long flags; \
388 unsigned short ret; \
389 if (ANOMALY_05000311 || ANOMALY_05000323) \
390 local_irq_save(flags); \
391 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
392 if (ANOMALY_05000311 || ANOMALY_05000323) { \
393 AWA_DUMMY_READ(name); \
394 local_irq_restore(flags); \
408 /* Get current PORT date (16-bit word) */
410 #define GET_GPIO_P(name) \
411 unsigned short get_gpiop_ ## name(unsigned gpio) \
413 unsigned long flags; \
414 unsigned short ret; \
415 if (ANOMALY_05000311 || ANOMALY_05000323) \
416 local_irq_save(flags); \
417 ret = (gpio_array[gpio_bank(gpio)]->name); \
418 if (ANOMALY_05000311 || ANOMALY_05000323) { \
419 AWA_DUMMY_READ(name); \
420 local_irq_restore(flags); \
434 /***********************************************************
436 * FUNCTIONS: Blackfin Peripheral Resource Allocation
440 * per Peripheral Identifier
443 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
446 *************************************************************
447 * MODIFICATION HISTORY :
448 **************************************************************/
450 int peripheral_request(unsigned short per, const char *label)
452 unsigned short ident = P_IDENT(per);
455 * Don't cares are pins with only one dedicated function
458 if (per & P_DONTCARE)
461 if (!(per & P_DEFINED))
464 BUG_ON(ident >= MAX_RESOURCES);
466 /* If a pin can be muxed as either GPIO or peripheral, make
467 * sure it is not already a GPIO pin when we request it.
469 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
470 printf("%s: Peripheral %d is already reserved as GPIO by %s !\n",
471 __func__, ident, get_label(ident));
475 if (unlikely(is_reserved(peri, ident, 1))) {
478 * Pin functions like AMC address strobes my
479 * be requested and used by several drivers
482 if (!(per & P_MAYSHARE)) {
484 * Allow that the identical pin function can
485 * be requested from the same driver twice
488 if (cmp_label(ident, label) == 0)
491 printf("%s: Peripheral %d function %d is already reserved by %s !\n",
492 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
498 reserve(peri, ident);
501 port_setup(ident, PERIPHERAL_USAGE);
503 set_label(ident, label);
508 int peripheral_request_list(const unsigned short per[], const char *label)
513 for (cnt = 0; per[cnt] != 0; cnt++) {
515 ret = peripheral_request(per[cnt], label);
518 for ( ; cnt > 0; cnt--)
519 peripheral_free(per[cnt - 1]);
528 void peripheral_free(unsigned short per)
530 unsigned short ident = P_IDENT(per);
532 if (per & P_DONTCARE)
535 if (!(per & P_DEFINED))
538 if (unlikely(!is_reserved(peri, ident, 0)))
541 if (!(per & P_MAYSHARE))
542 port_setup(ident, GPIO_USAGE);
544 unreserve(peri, ident);
546 set_label(ident, "free");
549 void peripheral_free_list(const unsigned short per[])
552 for (cnt = 0; per[cnt] != 0; cnt++)
553 peripheral_free(per[cnt]);
556 /***********************************************************
558 * FUNCTIONS: Blackfin GPIO Driver
561 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
564 * DESCRIPTION: Blackfin GPIO Driver API
567 *************************************************************
568 * MODIFICATION HISTORY :
569 **************************************************************/
571 int gpio_request(unsigned gpio, const char *label)
573 if (check_gpio(gpio) < 0)
577 * Allow that the identical GPIO can
578 * be requested from the same driver twice
579 * Do nothing and return -
582 if (cmp_label(gpio, label) == 0)
585 if (unlikely(is_reserved(gpio, gpio, 1))) {
586 printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
587 gpio, get_label(gpio));
590 if (unlikely(is_reserved(peri, gpio, 1))) {
591 printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
592 gpio, get_label(gpio));
595 else { /* Reset POLAR setting when acquiring a gpio for the first time */
596 set_gpio_polar(gpio, 0);
600 set_label(gpio, label);
602 port_setup(gpio, GPIO_USAGE);
607 int gpio_free(unsigned gpio)
609 if (check_gpio(gpio) < 0)
612 if (unlikely(!is_reserved(gpio, gpio, 0))) {
617 unreserve(gpio, gpio);
619 set_label(gpio, "free");
624 #ifdef ADI_SPECIAL_GPIO_BANKS
625 DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
627 int special_gpio_request(unsigned gpio, const char *label)
630 * Allow that the identical GPIO can
631 * be requested from the same driver twice
632 * Do nothing and return -
635 if (cmp_label(gpio, label) == 0)
638 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
639 printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
640 gpio, get_label(gpio));
643 if (unlikely(is_reserved(peri, gpio, 1))) {
644 printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
645 gpio, get_label(gpio));
650 reserve(special_gpio, gpio);
653 set_label(gpio, label);
654 port_setup(gpio, GPIO_USAGE);
659 void special_gpio_free(unsigned gpio)
661 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
666 unreserve(special_gpio, gpio);
667 unreserve(peri, gpio);
668 set_label(gpio, "free");
672 static inline void __gpio_direction_input(unsigned gpio)
674 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
675 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
678 int gpio_direction_input(unsigned gpio)
682 if (!is_reserved(gpio, gpio, 0)) {
687 local_irq_save(flags);
688 __gpio_direction_input(gpio);
689 AWA_DUMMY_READ(inen);
690 local_irq_restore(flags);
695 int gpio_set_value(unsigned gpio, int arg)
698 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
700 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
705 int gpio_direction_output(unsigned gpio, int value)
709 if (!is_reserved(gpio, gpio, 0)) {
714 local_irq_save(flags);
716 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
717 gpio_set_value(gpio, value);
718 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
721 local_irq_restore(flags);
726 int gpio_get_value(unsigned gpio)
730 if (unlikely(get_gpio_edge(gpio))) {
732 local_irq_save(flags);
733 set_gpio_edge(gpio, 0);
734 ret = get_gpio_data(gpio);
735 set_gpio_edge(gpio, 1);
736 local_irq_restore(flags);
739 return get_gpio_data(gpio);
742 /* If we are booting from SPI and our board lacks a strong enough pull up,
743 * the core can reset and execute the bootrom faster than the resistor can
744 * pull the signal logically high. To work around this (common) error in
745 * board design, we explicitly set the pin back to GPIO mode, force /CS
746 * high, and wait for the electrons to do their thing.
748 * This function only makes sense to be called from reset code, but it
749 * lives here as we need to force all the GPIO states w/out going through
750 * BUG() checks and such.
752 void bfin_reset_boot_spi_cs(unsigned short pin)
754 unsigned short gpio = P_IDENT(pin);
755 port_setup(gpio, GPIO_USAGE);
756 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
757 AWA_DUMMY_READ(data_set);
761 void gpio_labels(void)
765 for (c = 0; c < MAX_RESOURCES; c++) {
766 gpio = is_reserved(gpio, c, 1);
767 if (!check_gpio(c) && gpio)
768 printf("GPIO_%d:\t%s\tGPIO %s\n", c,
770 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
771 else if (is_reserved(peri, c, 1))
772 printf("GPIO_%d:\t%s\tPeripheral\n", c, get_label(c));