2 * File: arch/blackfin/kernel/bfin_gpio.c
4 * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
7 * Description: GPIO Abstraction Layer
10 * Copyright 2008 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 * Number BF537/6/4 BF561 BF533/2/1 BF549/8/4/2
33 * GPIO_0 PF0 PF0 PF0 PA0...PJ13
43 * GPIO_10 PF10 PF10 PF10
44 * GPIO_11 PF11 PF11 PF11
45 * GPIO_12 PF12 PF12 PF12
46 * GPIO_13 PF13 PF13 PF13
47 * GPIO_14 PF14 PF14 PF14
48 * GPIO_15 PF15 PF15 PF15
83 #include <linux/delay.h>
84 #include <linux/module.h>
85 #include <linux/err.h>
86 #include <linux/proc_fs.h>
87 #include <asm/blackfin.h>
89 #include <asm/portmux.h>
90 #include <linux/irq.h>
92 #if ANOMALY_05000311 || ANOMALY_05000323
95 AWA_data_clear = SYSCR,
98 AWA_maska = BFIN_UART_SCR,
99 AWA_maska_clear = BFIN_UART_SCR,
100 AWA_maska_set = BFIN_UART_SCR,
101 AWA_maska_toggle = BFIN_UART_SCR,
102 AWA_maskb = BFIN_UART_GCTL,
103 AWA_maskb_clear = BFIN_UART_GCTL,
104 AWA_maskb_set = BFIN_UART_GCTL,
105 AWA_maskb_toggle = BFIN_UART_GCTL,
106 AWA_dir = SPORT1_STAT,
107 AWA_polar = SPORT1_STAT,
108 AWA_edge = SPORT1_STAT,
109 AWA_both = SPORT1_STAT,
111 AWA_inen = TIMER_ENABLE,
112 #elif ANOMALY_05000323
113 AWA_inen = DMA1_1_CONFIG,
116 /* Anomaly Workaround */
117 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
119 #define AWA_DUMMY_READ(...) do { } while (0)
122 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
123 static struct gpio_port_t *gpio_bankb[] = {
124 (struct gpio_port_t *) FIO_FLAG_D,
128 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
129 static struct gpio_port_t *gpio_bankb[] = {
130 (struct gpio_port_t *) PORTFIO,
131 (struct gpio_port_t *) PORTGIO,
132 (struct gpio_port_t *) PORTHIO,
135 static unsigned short *port_fer[] = {
136 (unsigned short *) PORTF_FER,
137 (unsigned short *) PORTG_FER,
138 (unsigned short *) PORTH_FER,
142 #if defined(BF527_FAMILY) || defined(BF518_FAMILY)
143 static unsigned short *port_mux[] = {
144 (unsigned short *) PORTF_MUX,
145 (unsigned short *) PORTG_MUX,
146 (unsigned short *) PORTH_MUX,
150 u8 pmux_offset[][16] =
151 {{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
152 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
153 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
158 static struct gpio_port_t *gpio_bankb[] = {
159 (struct gpio_port_t *) FIO0_FLAG_D,
160 (struct gpio_port_t *) FIO1_FLAG_D,
161 (struct gpio_port_t *) FIO2_FLAG_D,
166 static struct gpio_port_t *gpio_array[] = {
167 (struct gpio_port_t *)PORTA_FER,
168 (struct gpio_port_t *)PORTB_FER,
169 (struct gpio_port_t *)PORTC_FER,
170 (struct gpio_port_t *)PORTD_FER,
171 (struct gpio_port_t *)PORTE_FER,
172 (struct gpio_port_t *)PORTF_FER,
173 (struct gpio_port_t *)PORTG_FER,
174 (struct gpio_port_t *)PORTH_FER,
175 (struct gpio_port_t *)PORTI_FER,
176 (struct gpio_port_t *)PORTJ_FER,
180 static unsigned short reserved_gpio_map[GPIO_BANK_NUM];
181 static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
182 static unsigned short reserved_gpio_irq_map[GPIO_BANK_NUM];
184 #define RESOURCE_LABEL_SIZE 16
186 static struct str_ident {
187 char name[RESOURCE_LABEL_SIZE];
188 } str_ident[MAX_RESOURCES];
190 #if defined(CONFIG_PM)
191 #if defined(CONFIG_BF54x)
192 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
194 static unsigned short wakeup_map[GPIO_BANK_NUM];
195 static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
196 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
199 static unsigned int sic_iwr_irqs[] = {IRQ_PROG_INTB};
203 static unsigned int sic_iwr_irqs[] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
207 static unsigned int sic_iwr_irqs[] = {IRQ_PORTF_INTB};
210 #if defined(BF527_FAMILY) || defined(BF518_FAMILY)
211 static unsigned int sic_iwr_irqs[] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
215 static unsigned int sic_iwr_irqs[] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
218 #endif /* CONFIG_PM */
220 inline int check_gpio(unsigned gpio)
222 #if defined(BF548_FAMILY)
223 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
224 || gpio == GPIO_PH14 || gpio == GPIO_PH15
225 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
228 if (gpio >= MAX_BLACKFIN_GPIOS)
233 static void gpio_error(unsigned gpio)
235 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
238 static void set_label(unsigned short ident, const char *label)
241 strncpy(str_ident[ident].name, label,
242 RESOURCE_LABEL_SIZE);
243 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
247 static char *get_label(unsigned short ident)
249 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
252 static int cmp_label(unsigned short ident, const char *label)
256 printk(KERN_ERR "Please provide none-null label\n");
260 return strcmp(str_ident[ident].name, label);
265 static void port_setup(unsigned gpio, unsigned short usage)
267 if (check_gpio(gpio))
270 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
271 if (usage == GPIO_USAGE)
272 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
274 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
276 #elif defined(BF548_FAMILY)
277 if (usage == GPIO_USAGE)
278 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
280 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
288 unsigned short offset;
290 {.res = P_PPI0_D13, .offset = 11},
291 {.res = P_PPI0_D14, .offset = 11},
292 {.res = P_PPI0_D15, .offset = 11},
293 {.res = P_SPORT1_TFS, .offset = 11},
294 {.res = P_SPORT1_TSCLK, .offset = 11},
295 {.res = P_SPORT1_DTPRI, .offset = 11},
296 {.res = P_PPI0_D10, .offset = 10},
297 {.res = P_PPI0_D11, .offset = 10},
298 {.res = P_PPI0_D12, .offset = 10},
299 {.res = P_SPORT1_RSCLK, .offset = 10},
300 {.res = P_SPORT1_RFS, .offset = 10},
301 {.res = P_SPORT1_DRPRI, .offset = 10},
302 {.res = P_PPI0_D8, .offset = 9},
303 {.res = P_PPI0_D9, .offset = 9},
304 {.res = P_SPORT1_DRSEC, .offset = 9},
305 {.res = P_SPORT1_DTSEC, .offset = 9},
306 {.res = P_TMR2, .offset = 8},
307 {.res = P_PPI0_FS3, .offset = 8},
308 {.res = P_TMR3, .offset = 7},
309 {.res = P_SPI0_SSEL4, .offset = 7},
310 {.res = P_TMR4, .offset = 6},
311 {.res = P_SPI0_SSEL5, .offset = 6},
312 {.res = P_TMR5, .offset = 5},
313 {.res = P_SPI0_SSEL6, .offset = 5},
314 {.res = P_UART1_RX, .offset = 4},
315 {.res = P_UART1_TX, .offset = 4},
316 {.res = P_TMR6, .offset = 4},
317 {.res = P_TMR7, .offset = 4},
318 {.res = P_UART0_RX, .offset = 3},
319 {.res = P_UART0_TX, .offset = 3},
320 {.res = P_DMAR0, .offset = 3},
321 {.res = P_DMAR1, .offset = 3},
322 {.res = P_SPORT0_DTSEC, .offset = 1},
323 {.res = P_SPORT0_DRSEC, .offset = 1},
324 {.res = P_CAN0_RX, .offset = 1},
325 {.res = P_CAN0_TX, .offset = 1},
326 {.res = P_SPI0_SSEL7, .offset = 1},
327 {.res = P_SPORT0_TFS, .offset = 0},
328 {.res = P_SPORT0_DTPRI, .offset = 0},
329 {.res = P_SPI0_SSEL2, .offset = 0},
330 {.res = P_SPI0_SSEL3, .offset = 0},
333 static void portmux_setup(unsigned short per, unsigned short function)
335 u16 y, offset, muxreg;
337 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
338 if (port_mux_lut[y].res == per) {
340 /* SET PORTMUX REG */
342 offset = port_mux_lut[y].offset;
343 muxreg = bfin_read_PORT_MUX();
346 muxreg &= ~(1 << offset);
350 muxreg |= (function << offset);
351 bfin_write_PORT_MUX(muxreg);
355 #elif defined(BF548_FAMILY)
356 inline void portmux_setup(unsigned short portno, unsigned short function)
360 pmux = gpio_array[gpio_bank(portno)]->port_mux;
362 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
363 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
365 gpio_array[gpio_bank(portno)]->port_mux = pmux;
368 inline u16 get_portmux(unsigned short portno)
372 pmux = gpio_array[gpio_bank(portno)]->port_mux;
374 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
376 #elif defined(BF527_FAMILY) || defined(BF518_FAMILY)
377 inline void portmux_setup(unsigned short portno, unsigned short function)
379 u16 pmux, ident = P_IDENT(portno);
380 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
382 pmux = *port_mux[gpio_bank(ident)];
383 pmux &= ~(3 << offset);
384 pmux |= (function & 3) << offset;
385 *port_mux[gpio_bank(ident)] = pmux;
389 # define portmux_setup(...) do { } while (0)
392 static int __init bfin_gpio_init(void)
394 printk(KERN_INFO "Blackfin GPIO Controller\n");
398 arch_initcall(bfin_gpio_init);
402 /***********************************************************
404 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
407 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
410 * DESCRIPTION: These functions abstract direct register access
411 * to Blackfin processor General Purpose
414 * CAUTION: These functions do not belong to the GPIO Driver API
415 *************************************************************
416 * MODIFICATION HISTORY :
417 **************************************************************/
419 /* Set a specific bit */
421 #define SET_GPIO(name) \
422 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
424 unsigned long flags; \
425 local_irq_save_hw(flags); \
427 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
429 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
430 AWA_DUMMY_READ(name); \
431 local_irq_restore_hw(flags); \
433 EXPORT_SYMBOL(set_gpio_ ## name);
442 #if ANOMALY_05000311 || ANOMALY_05000323
443 #define SET_GPIO_SC(name) \
444 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
446 unsigned long flags; \
447 local_irq_save_hw(flags); \
449 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
451 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
452 AWA_DUMMY_READ(name); \
453 local_irq_restore_hw(flags); \
455 EXPORT_SYMBOL(set_gpio_ ## name);
457 #define SET_GPIO_SC(name) \
458 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
461 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
463 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
465 EXPORT_SYMBOL(set_gpio_ ## name);
472 #if ANOMALY_05000311 || ANOMALY_05000323
473 void set_gpio_toggle(unsigned gpio)
476 local_irq_save_hw(flags);
477 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
478 AWA_DUMMY_READ(toggle);
479 local_irq_restore_hw(flags);
482 void set_gpio_toggle(unsigned gpio)
484 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
487 EXPORT_SYMBOL(set_gpio_toggle);
490 /*Set current PORT date (16-bit word)*/
492 #if ANOMALY_05000311 || ANOMALY_05000323
493 #define SET_GPIO_P(name) \
494 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
496 unsigned long flags; \
497 local_irq_save_hw(flags); \
498 gpio_bankb[gpio_bank(gpio)]->name = arg; \
499 AWA_DUMMY_READ(name); \
500 local_irq_restore_hw(flags); \
502 EXPORT_SYMBOL(set_gpiop_ ## name);
504 #define SET_GPIO_P(name) \
505 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
507 gpio_bankb[gpio_bank(gpio)]->name = arg; \
509 EXPORT_SYMBOL(set_gpiop_ ## name);
521 /* Get a specific bit */
522 #if ANOMALY_05000311 || ANOMALY_05000323
523 #define GET_GPIO(name) \
524 unsigned short get_gpio_ ## name(unsigned gpio) \
526 unsigned long flags; \
527 unsigned short ret; \
528 local_irq_save_hw(flags); \
529 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
530 AWA_DUMMY_READ(name); \
531 local_irq_restore_hw(flags); \
534 EXPORT_SYMBOL(get_gpio_ ## name);
536 #define GET_GPIO(name) \
537 unsigned short get_gpio_ ## name(unsigned gpio) \
539 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
541 EXPORT_SYMBOL(get_gpio_ ## name);
553 /*Get current PORT date (16-bit word)*/
555 #if ANOMALY_05000311 || ANOMALY_05000323
556 #define GET_GPIO_P(name) \
557 unsigned short get_gpiop_ ## name(unsigned gpio) \
559 unsigned long flags; \
560 unsigned short ret; \
561 local_irq_save_hw(flags); \
562 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
563 AWA_DUMMY_READ(name); \
564 local_irq_restore_hw(flags); \
567 EXPORT_SYMBOL(get_gpiop_ ## name);
569 #define GET_GPIO_P(name) \
570 unsigned short get_gpiop_ ## name(unsigned gpio) \
572 return (gpio_bankb[gpio_bank(gpio)]->name);\
574 EXPORT_SYMBOL(get_gpiop_ ## name);
588 /***********************************************************
590 * FUNCTIONS: Blackfin PM Setup API
593 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
601 * DESCRIPTION: Blackfin PM Driver API
604 *************************************************************
605 * MODIFICATION HISTORY :
606 **************************************************************/
607 int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
611 if ((check_gpio(gpio) < 0) || !type)
614 local_irq_save_hw(flags);
615 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
616 wakeup_flags_map[gpio] = type;
617 local_irq_restore_hw(flags);
621 EXPORT_SYMBOL(gpio_pm_wakeup_request);
623 void gpio_pm_wakeup_free(unsigned gpio)
627 if (check_gpio(gpio) < 0)
630 local_irq_save_hw(flags);
632 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
634 local_irq_restore_hw(flags);
636 EXPORT_SYMBOL(gpio_pm_wakeup_free);
638 static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
640 port_setup(gpio, GPIO_USAGE);
641 set_gpio_dir(gpio, 0);
642 set_gpio_inen(gpio, 1);
644 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
645 set_gpio_edge(gpio, 1);
647 set_gpio_edge(gpio, 0);
649 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
650 set_gpio_both(gpio, 1);
652 set_gpio_both(gpio, 0);
654 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
655 set_gpio_polar(gpio, 1);
657 set_gpio_polar(gpio, 0);
664 u32 bfin_pm_standby_setup(void)
666 u16 bank, mask, i, gpio;
668 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
669 mask = wakeup_map[gpio_bank(i)];
672 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
673 gpio_bankb[bank]->maskb = 0;
676 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
677 gpio_bank_saved[bank].fer = *port_fer[bank];
679 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
680 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
681 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
682 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
683 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
684 gpio_bank_saved[bank].reserved =
685 reserved_gpio_map[bank];
690 if ((mask & 1) && (wakeup_flags_map[gpio] !=
692 reserved_gpio_map[gpio_bank(gpio)] |=
694 bfin_gpio_wakeup_type(gpio,
695 wakeup_flags_map[gpio]);
696 set_gpio_data(gpio, 0); /*Clear*/
702 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
703 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
707 AWA_DUMMY_READ(maskb_set);
712 void bfin_pm_standby_restore(void)
716 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
717 mask = wakeup_map[gpio_bank(i)];
721 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
722 *port_fer[bank] = gpio_bank_saved[bank].fer;
724 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
725 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
726 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
727 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
728 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
730 reserved_gpio_map[bank] =
731 gpio_bank_saved[bank].reserved;
732 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
735 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
737 AWA_DUMMY_READ(maskb);
740 void bfin_gpio_pm_hibernate_suspend(void)
744 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
747 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
748 gpio_bank_saved[bank].fer = *port_fer[bank];
749 #if defined(BF527_FAMILY) || defined(BF518_FAMILY)
750 gpio_bank_saved[bank].mux = *port_mux[bank];
753 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
756 gpio_bank_saved[bank].data = gpio_bankb[bank]->data;
757 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
758 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
759 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
760 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
761 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
762 gpio_bank_saved[bank].maska = gpio_bankb[bank]->maska;
765 AWA_DUMMY_READ(maska);
768 void bfin_gpio_pm_hibernate_restore(void)
772 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
775 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
776 #if defined(BF527_FAMILY) || defined(BF518_FAMILY)
777 *port_mux[bank] = gpio_bank_saved[bank].mux;
780 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
782 *port_fer[bank] = gpio_bank_saved[bank].fer;
784 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
785 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
786 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
787 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
788 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
790 gpio_bankb[bank]->data_set = gpio_bank_saved[bank].data
791 | gpio_bank_saved[bank].dir;
793 gpio_bankb[bank]->maska = gpio_bank_saved[bank].maska;
795 AWA_DUMMY_READ(maska);
800 #else /* BF548_FAMILY */
803 u32 bfin_pm_standby_setup(void)
808 void bfin_pm_standby_restore(void)
813 void bfin_gpio_pm_hibernate_suspend(void)
817 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
820 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
821 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
822 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
823 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
824 gpio_bank_saved[bank].inen = gpio_array[bank]->port_inen;
825 gpio_bank_saved[bank].dir = gpio_array[bank]->port_dir_set;
829 void bfin_gpio_pm_hibernate_restore(void)
833 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
836 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
837 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
838 gpio_array[bank]->port_inen = gpio_bank_saved[bank].inen;
839 gpio_array[bank]->port_dir_set = gpio_bank_saved[bank].dir;
840 gpio_array[bank]->port_set = gpio_bank_saved[bank].data
841 | gpio_bank_saved[bank].dir;
846 unsigned short get_gpio_dir(unsigned gpio)
848 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
850 EXPORT_SYMBOL(get_gpio_dir);
852 #endif /* BF548_FAMILY */
854 /***********************************************************
856 * FUNCTIONS: Blackfin Peripheral Resource Allocation
860 * per Peripheral Identifier
863 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
866 *************************************************************
867 * MODIFICATION HISTORY :
868 **************************************************************/
870 int peripheral_request(unsigned short per, const char *label)
873 unsigned short ident = P_IDENT(per);
876 * Don't cares are pins with only one dedicated function
879 if (per & P_DONTCARE)
882 if (!(per & P_DEFINED))
885 local_irq_save_hw(flags);
887 /* If a pin can be muxed as either GPIO or peripheral, make
888 * sure it is not already a GPIO pin when we request it.
890 if (unlikely(!check_gpio(ident) &&
891 reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
894 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
895 __func__, ident, get_label(ident));
896 local_irq_restore_hw(flags);
900 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
903 * Pin functions like AMC address strobes my
904 * be requested and used by several drivers
908 u16 funct = get_portmux(ident);
910 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
912 if (!(per & P_MAYSHARE)) {
915 * Allow that the identical pin function can
916 * be requested from the same driver twice
919 if (cmp_label(ident, label) == 0)
924 "%s: Peripheral %d function %d is already reserved by %s !\n",
925 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
926 local_irq_restore_hw(flags);
932 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
935 portmux_setup(ident, P_FUNCT2MUX(per));
937 portmux_setup(per, P_FUNCT2MUX(per));
939 port_setup(ident, PERIPHERAL_USAGE);
941 local_irq_restore_hw(flags);
942 set_label(ident, label);
946 EXPORT_SYMBOL(peripheral_request);
948 int peripheral_request_list(const unsigned short per[], const char *label)
953 for (cnt = 0; per[cnt] != 0; cnt++) {
955 ret = peripheral_request(per[cnt], label);
958 for ( ; cnt > 0; cnt--)
959 peripheral_free(per[cnt - 1]);
967 EXPORT_SYMBOL(peripheral_request_list);
969 void peripheral_free(unsigned short per)
972 unsigned short ident = P_IDENT(per);
974 if (per & P_DONTCARE)
977 if (!(per & P_DEFINED))
980 if (check_gpio(ident) < 0)
983 local_irq_save_hw(flags);
985 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
986 local_irq_restore_hw(flags);
990 if (!(per & P_MAYSHARE))
991 port_setup(ident, GPIO_USAGE);
993 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
995 set_label(ident, "free");
997 local_irq_restore_hw(flags);
999 EXPORT_SYMBOL(peripheral_free);
1001 void peripheral_free_list(const unsigned short per[])
1004 for (cnt = 0; per[cnt] != 0; cnt++)
1005 peripheral_free(per[cnt]);
1007 EXPORT_SYMBOL(peripheral_free_list);
1009 /***********************************************************
1011 * FUNCTIONS: Blackfin GPIO Driver
1014 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1017 * DESCRIPTION: Blackfin GPIO Driver API
1020 *************************************************************
1021 * MODIFICATION HISTORY :
1022 **************************************************************/
1024 int bfin_gpio_request(unsigned gpio, const char *label)
1026 unsigned long flags;
1028 if (check_gpio(gpio) < 0)
1031 local_irq_save_hw(flags);
1034 * Allow that the identical GPIO can
1035 * be requested from the same driver twice
1036 * Do nothing and return -
1039 if (cmp_label(gpio, label) == 0) {
1040 local_irq_restore_hw(flags);
1044 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1046 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1047 gpio, get_label(gpio));
1048 local_irq_restore_hw(flags);
1051 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1054 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1055 gpio, get_label(gpio));
1056 local_irq_restore_hw(flags);
1059 if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))
1060 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
1061 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
1063 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
1064 set_label(gpio, label);
1066 local_irq_restore_hw(flags);
1068 port_setup(gpio, GPIO_USAGE);
1072 EXPORT_SYMBOL(bfin_gpio_request);
1074 void bfin_gpio_free(unsigned gpio)
1076 unsigned long flags;
1078 if (check_gpio(gpio) < 0)
1081 local_irq_save_hw(flags);
1083 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1086 local_irq_restore_hw(flags);
1090 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1092 set_label(gpio, "free");
1094 local_irq_restore_hw(flags);
1096 EXPORT_SYMBOL(bfin_gpio_free);
1098 int bfin_gpio_irq_request(unsigned gpio, const char *label)
1100 unsigned long flags;
1102 if (check_gpio(gpio) < 0)
1105 local_irq_save_hw(flags);
1107 if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1110 "bfin-gpio: GPIO %d is already reserved as gpio-irq !\n",
1112 local_irq_restore_hw(flags);
1115 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1118 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1119 gpio, get_label(gpio));
1120 local_irq_restore_hw(flags);
1123 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))
1124 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1125 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1126 gpio, get_label(gpio));
1128 reserved_gpio_irq_map[gpio_bank(gpio)] |= gpio_bit(gpio);
1129 set_label(gpio, label);
1131 local_irq_restore_hw(flags);
1133 port_setup(gpio, GPIO_USAGE);
1138 void bfin_gpio_irq_free(unsigned gpio)
1140 unsigned long flags;
1142 if (check_gpio(gpio) < 0)
1145 local_irq_save_hw(flags);
1147 if (unlikely(!(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1150 local_irq_restore_hw(flags);
1154 reserved_gpio_irq_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1156 set_label(gpio, "free");
1158 local_irq_restore_hw(flags);
1163 int bfin_gpio_direction_input(unsigned gpio)
1165 unsigned long flags;
1167 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1172 local_irq_save_hw(flags);
1173 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1174 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1175 local_irq_restore_hw(flags);
1179 EXPORT_SYMBOL(bfin_gpio_direction_input);
1181 int bfin_gpio_direction_output(unsigned gpio, int value)
1183 unsigned long flags;
1185 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1190 local_irq_save_hw(flags);
1191 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
1192 gpio_set_value(gpio, value);
1193 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1194 local_irq_restore_hw(flags);
1198 EXPORT_SYMBOL(bfin_gpio_direction_output);
1200 void bfin_gpio_set_value(unsigned gpio, int arg)
1203 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1205 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
1207 EXPORT_SYMBOL(bfin_gpio_set_value);
1209 int bfin_gpio_get_value(unsigned gpio)
1211 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1213 EXPORT_SYMBOL(bfin_gpio_get_value);
1215 void bfin_gpio_irq_prepare(unsigned gpio)
1217 unsigned long flags;
1219 port_setup(gpio, GPIO_USAGE);
1221 local_irq_save_hw(flags);
1222 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1223 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1224 local_irq_restore_hw(flags);
1229 int bfin_gpio_get_value(unsigned gpio)
1231 unsigned long flags;
1234 if (unlikely(get_gpio_edge(gpio))) {
1235 local_irq_save_hw(flags);
1236 set_gpio_edge(gpio, 0);
1237 ret = get_gpio_data(gpio);
1238 set_gpio_edge(gpio, 1);
1239 local_irq_restore_hw(flags);
1243 return get_gpio_data(gpio);
1245 EXPORT_SYMBOL(bfin_gpio_get_value);
1248 int bfin_gpio_direction_input(unsigned gpio)
1250 unsigned long flags;
1252 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1257 local_irq_save_hw(flags);
1258 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1259 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1260 AWA_DUMMY_READ(inen);
1261 local_irq_restore_hw(flags);
1265 EXPORT_SYMBOL(bfin_gpio_direction_input);
1267 int bfin_gpio_direction_output(unsigned gpio, int value)
1269 unsigned long flags;
1271 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1276 local_irq_save_hw(flags);
1277 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1280 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1282 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1284 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1285 AWA_DUMMY_READ(dir);
1286 local_irq_restore_hw(flags);
1290 EXPORT_SYMBOL(bfin_gpio_direction_output);
1292 /* If we are booting from SPI and our board lacks a strong enough pull up,
1293 * the core can reset and execute the bootrom faster than the resistor can
1294 * pull the signal logically high. To work around this (common) error in
1295 * board design, we explicitly set the pin back to GPIO mode, force /CS
1296 * high, and wait for the electrons to do their thing.
1298 * This function only makes sense to be called from reset code, but it
1299 * lives here as we need to force all the GPIO states w/out going through
1300 * BUG() checks and such.
1302 void bfin_gpio_reset_spi0_ssel1(void)
1304 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1306 port_setup(gpio, GPIO_USAGE);
1307 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1308 AWA_DUMMY_READ(data_set);
1312 void bfin_gpio_irq_prepare(unsigned gpio)
1314 port_setup(gpio, GPIO_USAGE);
1317 #endif /*BF548_FAMILY */
1319 #if defined(CONFIG_PROC_FS)
1320 static int gpio_proc_read(char *buf, char **start, off_t offset,
1321 int len, int *unused_i, void *unused_v)
1323 int c, irq, gpio, outlen = 0;
1325 for (c = 0; c < MAX_RESOURCES; c++) {
1326 irq = reserved_gpio_irq_map[gpio_bank(c)] & gpio_bit(c);
1327 gpio = reserved_gpio_map[gpio_bank(c)] & gpio_bit(c);
1328 if (!check_gpio(c) && (gpio || irq))
1329 len = sprintf(buf, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1330 get_label(c), (gpio && irq) ? " *" : "",
1331 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1332 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
1333 len = sprintf(buf, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1342 static __init int gpio_register_proc(void)
1344 struct proc_dir_entry *proc_gpio;
1346 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1348 proc_gpio->read_proc = gpio_proc_read;
1349 return proc_gpio != NULL;
1351 __initcall(gpio_register_proc);
1354 #ifdef CONFIG_GPIOLIB
1355 int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1357 return bfin_gpio_direction_input(gpio);
1360 int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1362 return bfin_gpio_direction_output(gpio, level);
1365 int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1367 return bfin_gpio_get_value(gpio);
1370 void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1373 return bfin_gpio_set_value(gpio, value);
1375 return set_gpio_data(gpio, value);
1379 int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1381 return bfin_gpio_request(gpio, chip->label);
1384 void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1386 return bfin_gpio_free(gpio);
1389 static struct gpio_chip bfin_chip = {
1390 .label = "Blackfin-GPIOlib",
1391 .direction_input = bfin_gpiolib_direction_input,
1392 .get = bfin_gpiolib_get_value,
1393 .direction_output = bfin_gpiolib_direction_output,
1394 .set = bfin_gpiolib_set_value,
1395 .request = bfin_gpiolib_gpio_request,
1396 .free = bfin_gpiolib_gpio_free,
1398 .ngpio = MAX_BLACKFIN_GPIOS,
1401 static int __init bfin_gpiolib_setup(void)
1403 return gpiochip_add(&bfin_chip);
1405 arch_initcall(bfin_gpiolib_setup);