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)
123 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
124 (struct gpio_port_t *) FIO_FLAG_D,
128 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
129 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
130 (struct gpio_port_t *) PORTFIO,
131 (struct gpio_port_t *) PORTGIO,
132 (struct gpio_port_t *) PORTHIO,
135 static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
136 (unsigned short *) PORTF_FER,
137 (unsigned short *) PORTG_FER,
138 (unsigned short *) PORTH_FER,
143 static unsigned short *port_mux[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
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[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
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[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
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(MAX_BLACKFIN_GPIOS)];
181 static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
183 #define RESOURCE_LABEL_SIZE 16
185 static struct str_ident {
186 char name[RESOURCE_LABEL_SIZE];
187 } str_ident[MAX_RESOURCES];
189 #if defined(CONFIG_PM)
190 #if defined(CONFIG_BF54x)
191 static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
193 static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
194 static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
195 static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
198 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
202 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
206 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
210 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
213 #endif /* CONFIG_PM */
215 #if defined(BF548_FAMILY)
216 inline int check_gpio(unsigned gpio)
218 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
219 || gpio == GPIO_PH14 || gpio == GPIO_PH15
220 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15
221 || gpio > MAX_BLACKFIN_GPIOS)
226 inline int check_gpio(unsigned gpio)
228 if (gpio >= MAX_BLACKFIN_GPIOS)
234 static void gpio_error(unsigned gpio)
236 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
239 static void set_label(unsigned short ident, const char *label)
242 strncpy(str_ident[ident].name, label,
243 RESOURCE_LABEL_SIZE);
244 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
248 static char *get_label(unsigned short ident)
250 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
253 static int cmp_label(unsigned short ident, const char *label)
257 printk(KERN_ERR "Please provide none-null label\n");
261 return strncmp(str_ident[ident].name,
262 label, strlen(label));
267 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
268 static void port_setup(unsigned gpio, unsigned short usage)
270 if (!check_gpio(gpio)) {
271 if (usage == GPIO_USAGE)
272 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
274 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
278 #elif defined(BF548_FAMILY)
279 static void port_setup(unsigned gpio, unsigned short usage)
281 if (usage == GPIO_USAGE)
282 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
284 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
288 # define port_setup(...) do { } while (0)
294 unsigned short offset;
296 {.res = P_PPI0_D13, .offset = 11},
297 {.res = P_PPI0_D14, .offset = 11},
298 {.res = P_PPI0_D15, .offset = 11},
299 {.res = P_SPORT1_TFS, .offset = 11},
300 {.res = P_SPORT1_TSCLK, .offset = 11},
301 {.res = P_SPORT1_DTPRI, .offset = 11},
302 {.res = P_PPI0_D10, .offset = 10},
303 {.res = P_PPI0_D11, .offset = 10},
304 {.res = P_PPI0_D12, .offset = 10},
305 {.res = P_SPORT1_RSCLK, .offset = 10},
306 {.res = P_SPORT1_RFS, .offset = 10},
307 {.res = P_SPORT1_DRPRI, .offset = 10},
308 {.res = P_PPI0_D8, .offset = 9},
309 {.res = P_PPI0_D9, .offset = 9},
310 {.res = P_SPORT1_DRSEC, .offset = 9},
311 {.res = P_SPORT1_DTSEC, .offset = 9},
312 {.res = P_TMR2, .offset = 8},
313 {.res = P_PPI0_FS3, .offset = 8},
314 {.res = P_TMR3, .offset = 7},
315 {.res = P_SPI0_SSEL4, .offset = 7},
316 {.res = P_TMR4, .offset = 6},
317 {.res = P_SPI0_SSEL5, .offset = 6},
318 {.res = P_TMR5, .offset = 5},
319 {.res = P_SPI0_SSEL6, .offset = 5},
320 {.res = P_UART1_RX, .offset = 4},
321 {.res = P_UART1_TX, .offset = 4},
322 {.res = P_TMR6, .offset = 4},
323 {.res = P_TMR7, .offset = 4},
324 {.res = P_UART0_RX, .offset = 3},
325 {.res = P_UART0_TX, .offset = 3},
326 {.res = P_DMAR0, .offset = 3},
327 {.res = P_DMAR1, .offset = 3},
328 {.res = P_SPORT0_DTSEC, .offset = 1},
329 {.res = P_SPORT0_DRSEC, .offset = 1},
330 {.res = P_CAN0_RX, .offset = 1},
331 {.res = P_CAN0_TX, .offset = 1},
332 {.res = P_SPI0_SSEL7, .offset = 1},
333 {.res = P_SPORT0_TFS, .offset = 0},
334 {.res = P_SPORT0_DTPRI, .offset = 0},
335 {.res = P_SPI0_SSEL2, .offset = 0},
336 {.res = P_SPI0_SSEL3, .offset = 0},
339 static void portmux_setup(unsigned short per, unsigned short function)
341 u16 y, offset, muxreg;
343 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
344 if (port_mux_lut[y].res == per) {
346 /* SET PORTMUX REG */
348 offset = port_mux_lut[y].offset;
349 muxreg = bfin_read_PORT_MUX();
352 muxreg &= ~(1 << offset);
356 muxreg |= (function << offset);
357 bfin_write_PORT_MUX(muxreg);
361 #elif defined(BF548_FAMILY)
362 inline void portmux_setup(unsigned short portno, unsigned short function)
366 pmux = gpio_array[gpio_bank(portno)]->port_mux;
368 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
369 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
371 gpio_array[gpio_bank(portno)]->port_mux = pmux;
374 inline u16 get_portmux(unsigned short portno)
378 pmux = gpio_array[gpio_bank(portno)]->port_mux;
380 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
382 #elif defined(BF527_FAMILY)
383 inline void portmux_setup(unsigned short portno, unsigned short function)
385 u16 pmux, ident = P_IDENT(portno);
386 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
388 pmux = *port_mux[gpio_bank(ident)];
389 pmux &= ~(3 << offset);
390 pmux |= (function & 3) << offset;
391 *port_mux[gpio_bank(ident)] = pmux;
395 # define portmux_setup(...) do { } while (0)
398 static int __init bfin_gpio_init(void)
400 printk(KERN_INFO "Blackfin GPIO Controller\n");
404 arch_initcall(bfin_gpio_init);
408 /***********************************************************
410 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
413 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
416 * DESCRIPTION: These functions abstract direct register access
417 * to Blackfin processor General Purpose
420 * CAUTION: These functions do not belong to the GPIO Driver API
421 *************************************************************
422 * MODIFICATION HISTORY :
423 **************************************************************/
425 /* Set a specific bit */
427 #define SET_GPIO(name) \
428 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
430 unsigned long flags; \
431 local_irq_save(flags); \
433 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
435 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
436 AWA_DUMMY_READ(name); \
437 local_irq_restore(flags); \
439 EXPORT_SYMBOL(set_gpio_ ## name);
448 #if ANOMALY_05000311 || ANOMALY_05000323
449 #define SET_GPIO_SC(name) \
450 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
452 unsigned long flags; \
453 local_irq_save(flags); \
455 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
457 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
458 AWA_DUMMY_READ(name); \
459 local_irq_restore(flags); \
461 EXPORT_SYMBOL(set_gpio_ ## name);
463 #define SET_GPIO_SC(name) \
464 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
467 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
469 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
471 EXPORT_SYMBOL(set_gpio_ ## name);
478 #if ANOMALY_05000311 || ANOMALY_05000323
479 void set_gpio_toggle(unsigned gpio)
482 local_irq_save(flags);
483 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
484 AWA_DUMMY_READ(toggle);
485 local_irq_restore(flags);
488 void set_gpio_toggle(unsigned gpio)
490 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
493 EXPORT_SYMBOL(set_gpio_toggle);
496 /*Set current PORT date (16-bit word)*/
498 #if ANOMALY_05000311 || ANOMALY_05000323
499 #define SET_GPIO_P(name) \
500 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
502 unsigned long flags; \
503 local_irq_save(flags); \
504 gpio_bankb[gpio_bank(gpio)]->name = arg; \
505 AWA_DUMMY_READ(name); \
506 local_irq_restore(flags); \
508 EXPORT_SYMBOL(set_gpiop_ ## name);
510 #define SET_GPIO_P(name) \
511 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
513 gpio_bankb[gpio_bank(gpio)]->name = arg; \
515 EXPORT_SYMBOL(set_gpiop_ ## name);
527 /* Get a specific bit */
528 #if ANOMALY_05000311 || ANOMALY_05000323
529 #define GET_GPIO(name) \
530 unsigned short get_gpio_ ## name(unsigned gpio) \
532 unsigned long flags; \
533 unsigned short ret; \
534 local_irq_save(flags); \
535 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
536 AWA_DUMMY_READ(name); \
537 local_irq_restore(flags); \
540 EXPORT_SYMBOL(get_gpio_ ## name);
542 #define GET_GPIO(name) \
543 unsigned short get_gpio_ ## name(unsigned gpio) \
545 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
547 EXPORT_SYMBOL(get_gpio_ ## name);
559 /*Get current PORT date (16-bit word)*/
561 #if ANOMALY_05000311 || ANOMALY_05000323
562 #define GET_GPIO_P(name) \
563 unsigned short get_gpiop_ ## name(unsigned gpio) \
565 unsigned long flags; \
566 unsigned short ret; \
567 local_irq_save(flags); \
568 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
569 AWA_DUMMY_READ(name); \
570 local_irq_restore(flags); \
573 EXPORT_SYMBOL(get_gpiop_ ## name);
575 #define GET_GPIO_P(name) \
576 unsigned short get_gpiop_ ## name(unsigned gpio) \
578 return (gpio_bankb[gpio_bank(gpio)]->name);\
580 EXPORT_SYMBOL(get_gpiop_ ## name);
594 /***********************************************************
596 * FUNCTIONS: Blackfin PM Setup API
599 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
607 * DESCRIPTION: Blackfin PM Driver API
610 *************************************************************
611 * MODIFICATION HISTORY :
612 **************************************************************/
613 int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
617 if ((check_gpio(gpio) < 0) || !type)
620 local_irq_save(flags);
621 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
622 wakeup_flags_map[gpio] = type;
623 local_irq_restore(flags);
627 EXPORT_SYMBOL(gpio_pm_wakeup_request);
629 void gpio_pm_wakeup_free(unsigned gpio)
633 if (check_gpio(gpio) < 0)
636 local_irq_save(flags);
638 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
640 local_irq_restore(flags);
642 EXPORT_SYMBOL(gpio_pm_wakeup_free);
644 static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
646 port_setup(gpio, GPIO_USAGE);
647 set_gpio_dir(gpio, 0);
648 set_gpio_inen(gpio, 1);
650 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
651 set_gpio_edge(gpio, 1);
653 set_gpio_edge(gpio, 0);
655 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
656 set_gpio_both(gpio, 1);
658 set_gpio_both(gpio, 0);
660 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
661 set_gpio_polar(gpio, 1);
663 set_gpio_polar(gpio, 0);
670 u32 bfin_pm_standby_setup(void)
672 u16 bank, mask, i, gpio;
674 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
675 mask = wakeup_map[gpio_bank(i)];
678 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
679 gpio_bankb[bank]->maskb = 0;
682 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
683 gpio_bank_saved[bank].fer = *port_fer[bank];
685 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
686 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
687 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
688 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
689 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
690 gpio_bank_saved[bank].reserved =
691 reserved_gpio_map[bank];
696 if ((mask & 1) && (wakeup_flags_map[gpio] !=
698 reserved_gpio_map[gpio_bank(gpio)] |=
700 bfin_gpio_wakeup_type(gpio,
701 wakeup_flags_map[gpio]);
702 set_gpio_data(gpio, 0); /*Clear*/
708 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
709 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
713 AWA_DUMMY_READ(maskb_set);
718 void bfin_pm_standby_restore(void)
722 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
723 mask = wakeup_map[gpio_bank(i)];
727 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
728 *port_fer[bank] = gpio_bank_saved[bank].fer;
730 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
731 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
732 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
733 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
734 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
736 reserved_gpio_map[bank] =
737 gpio_bank_saved[bank].reserved;
738 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
741 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
743 AWA_DUMMY_READ(maskb);
746 void bfin_gpio_pm_hibernate_suspend(void)
750 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
753 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
754 gpio_bank_saved[bank].fer = *port_fer[bank];
756 gpio_bank_saved[bank].mux = *port_mux[bank];
759 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
762 gpio_bank_saved[bank].data = gpio_bankb[bank]->data;
763 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
764 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
765 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
766 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
767 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
768 gpio_bank_saved[bank].maska = gpio_bankb[bank]->maska;
771 AWA_DUMMY_READ(maska);
774 void bfin_gpio_pm_hibernate_restore(void)
778 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
781 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
783 *port_mux[bank] = gpio_bank_saved[bank].mux;
786 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
788 *port_fer[bank] = gpio_bank_saved[bank].fer;
790 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
791 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
792 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
793 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
794 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
796 gpio_bankb[bank]->data_set = gpio_bank_saved[bank].data
797 | gpio_bank_saved[bank].dir;
799 gpio_bankb[bank]->maska = gpio_bank_saved[bank].maska;
801 AWA_DUMMY_READ(maska);
806 #else /* BF548_FAMILY */
809 u32 bfin_pm_standby_setup(void)
814 void bfin_pm_standby_restore(void)
819 void bfin_gpio_pm_hibernate_suspend(void)
823 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
826 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
827 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
828 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
829 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
830 gpio_bank_saved[bank].inen = gpio_array[bank]->port_inen;
831 gpio_bank_saved[bank].dir = gpio_array[bank]->port_dir_set;
835 void bfin_gpio_pm_hibernate_restore(void)
839 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
842 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
843 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
844 gpio_array[bank]->port_inen = gpio_bank_saved[bank].inen;
845 gpio_array[bank]->port_dir_set = gpio_bank_saved[bank].dir;
846 gpio_array[bank]->port_set = gpio_bank_saved[bank].data
847 | gpio_bank_saved[bank].dir;
852 unsigned short get_gpio_dir(unsigned gpio)
854 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
856 EXPORT_SYMBOL(get_gpio_dir);
858 #endif /* BF548_FAMILY */
860 /***********************************************************
862 * FUNCTIONS: Blackfin Peripheral Resource Allocation
866 * per Peripheral Identifier
869 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
872 *************************************************************
873 * MODIFICATION HISTORY :
874 **************************************************************/
877 int peripheral_request(unsigned short per, const char *label)
880 unsigned short ident = P_IDENT(per);
883 * Don't cares are pins with only one dedicated function
886 if (per & P_DONTCARE)
889 if (!(per & P_DEFINED))
892 if (check_gpio(ident) < 0)
895 local_irq_save(flags);
897 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
900 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
901 __func__, ident, get_label(ident));
902 local_irq_restore(flags);
906 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
908 u16 funct = get_portmux(ident);
911 * Pin functions like AMC address strobes my
912 * be requested and used by several drivers
915 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
918 * Allow that the identical pin function can
919 * be requested from the same driver twice
922 if (cmp_label(ident, label) == 0)
927 "%s: Peripheral %d function %d is already reserved by %s !\n",
928 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
929 local_irq_restore(flags);
935 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
937 portmux_setup(ident, P_FUNCT2MUX(per));
938 port_setup(ident, PERIPHERAL_USAGE);
940 local_irq_restore(flags);
941 set_label(ident, label);
945 EXPORT_SYMBOL(peripheral_request);
948 int peripheral_request(unsigned short per, const char *label)
951 unsigned short ident = P_IDENT(per);
954 * Don't cares are pins with only one dedicated function
957 if (per & P_DONTCARE)
960 if (!(per & P_DEFINED))
963 local_irq_save(flags);
965 if (!check_gpio(ident)) {
967 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
970 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
971 __func__, ident, get_label(ident));
972 local_irq_restore(flags);
978 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
981 * Pin functions like AMC address strobes my
982 * be requested and used by several drivers
985 if (!(per & P_MAYSHARE)) {
988 * Allow that the identical pin function can
989 * be requested from the same driver twice
992 if (cmp_label(ident, label) == 0)
997 "%s: Peripheral %d function %d is already"
998 " reserved by %s !\n",
999 __func__, ident, P_FUNCT2MUX(per),
1001 local_irq_restore(flags);
1008 portmux_setup(per, P_FUNCT2MUX(per));
1010 port_setup(ident, PERIPHERAL_USAGE);
1012 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
1013 local_irq_restore(flags);
1014 set_label(ident, label);
1018 EXPORT_SYMBOL(peripheral_request);
1021 int peripheral_request_list(const unsigned short per[], const char *label)
1026 for (cnt = 0; per[cnt] != 0; cnt++) {
1028 ret = peripheral_request(per[cnt], label);
1031 for ( ; cnt > 0; cnt--)
1032 peripheral_free(per[cnt - 1]);
1040 EXPORT_SYMBOL(peripheral_request_list);
1042 void peripheral_free(unsigned short per)
1044 unsigned long flags;
1045 unsigned short ident = P_IDENT(per);
1047 if (per & P_DONTCARE)
1050 if (!(per & P_DEFINED))
1053 if (check_gpio(ident) < 0)
1056 local_irq_save(flags);
1058 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
1059 local_irq_restore(flags);
1063 if (!(per & P_MAYSHARE))
1064 port_setup(ident, GPIO_USAGE);
1066 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
1068 set_label(ident, "free");
1070 local_irq_restore(flags);
1072 EXPORT_SYMBOL(peripheral_free);
1074 void peripheral_free_list(const unsigned short per[])
1077 for (cnt = 0; per[cnt] != 0; cnt++)
1078 peripheral_free(per[cnt]);
1080 EXPORT_SYMBOL(peripheral_free_list);
1082 /***********************************************************
1084 * FUNCTIONS: Blackfin GPIO Driver
1087 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1090 * DESCRIPTION: Blackfin GPIO Driver API
1093 *************************************************************
1094 * MODIFICATION HISTORY :
1095 **************************************************************/
1097 int gpio_request(unsigned gpio, const char *label)
1099 unsigned long flags;
1101 if (check_gpio(gpio) < 0)
1104 local_irq_save(flags);
1107 * Allow that the identical GPIO can
1108 * be requested from the same driver twice
1109 * Do nothing and return -
1112 if (cmp_label(gpio, label) == 0) {
1113 local_irq_restore(flags);
1117 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1119 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1120 gpio, get_label(gpio));
1121 local_irq_restore(flags);
1124 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1127 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1128 gpio, get_label(gpio));
1129 local_irq_restore(flags);
1133 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
1135 local_irq_restore(flags);
1137 port_setup(gpio, GPIO_USAGE);
1138 set_label(gpio, label);
1142 EXPORT_SYMBOL(gpio_request);
1144 void gpio_free(unsigned gpio)
1146 unsigned long flags;
1148 if (check_gpio(gpio) < 0)
1151 local_irq_save(flags);
1153 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1156 local_irq_restore(flags);
1160 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1162 set_label(gpio, "free");
1164 local_irq_restore(flags);
1166 EXPORT_SYMBOL(gpio_free);
1170 int gpio_direction_input(unsigned gpio)
1172 unsigned long flags;
1174 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1179 local_irq_save(flags);
1180 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1181 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1182 local_irq_restore(flags);
1186 EXPORT_SYMBOL(gpio_direction_input);
1188 int gpio_direction_output(unsigned gpio, int value)
1190 unsigned long flags;
1192 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1197 local_irq_save(flags);
1198 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
1199 gpio_set_value(gpio, value);
1200 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1201 local_irq_restore(flags);
1205 EXPORT_SYMBOL(gpio_direction_output);
1207 void gpio_set_value(unsigned gpio, int arg)
1210 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1212 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
1214 EXPORT_SYMBOL(gpio_set_value);
1216 int gpio_get_value(unsigned gpio)
1218 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1220 EXPORT_SYMBOL(gpio_get_value);
1222 void bfin_gpio_irq_prepare(unsigned gpio)
1224 unsigned long flags;
1226 port_setup(gpio, GPIO_USAGE);
1228 local_irq_save(flags);
1229 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1230 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1231 local_irq_restore(flags);
1236 int gpio_get_value(unsigned gpio)
1238 unsigned long flags;
1241 if (unlikely(get_gpio_edge(gpio))) {
1242 local_irq_save(flags);
1243 set_gpio_edge(gpio, 0);
1244 ret = get_gpio_data(gpio);
1245 set_gpio_edge(gpio, 1);
1246 local_irq_restore(flags);
1250 return get_gpio_data(gpio);
1252 EXPORT_SYMBOL(gpio_get_value);
1255 int gpio_direction_input(unsigned gpio)
1257 unsigned long flags;
1259 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1264 local_irq_save(flags);
1265 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1266 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1267 AWA_DUMMY_READ(inen);
1268 local_irq_restore(flags);
1272 EXPORT_SYMBOL(gpio_direction_input);
1274 int gpio_direction_output(unsigned gpio, int value)
1276 unsigned long flags;
1278 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1283 local_irq_save(flags);
1284 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1287 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1289 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1291 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1292 AWA_DUMMY_READ(dir);
1293 local_irq_restore(flags);
1297 EXPORT_SYMBOL(gpio_direction_output);
1299 /* If we are booting from SPI and our board lacks a strong enough pull up,
1300 * the core can reset and execute the bootrom faster than the resistor can
1301 * pull the signal logically high. To work around this (common) error in
1302 * board design, we explicitly set the pin back to GPIO mode, force /CS
1303 * high, and wait for the electrons to do their thing.
1305 * This function only makes sense to be called from reset code, but it
1306 * lives here as we need to force all the GPIO states w/out going through
1307 * BUG() checks and such.
1309 void bfin_gpio_reset_spi0_ssel1(void)
1311 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1313 port_setup(gpio, GPIO_USAGE);
1314 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1315 AWA_DUMMY_READ(data_set);
1319 void bfin_gpio_irq_prepare(unsigned gpio)
1321 port_setup(gpio, GPIO_USAGE);
1324 #endif /*BF548_FAMILY */
1326 #if defined(CONFIG_PROC_FS)
1327 static int gpio_proc_read(char *buf, char **start, off_t offset,
1328 int len, int *unused_i, void *unused_v)
1332 for (c = 0; c < MAX_RESOURCES; c++) {
1333 if (!check_gpio(c) && (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c)))
1334 len = sprintf(buf, "GPIO_%d: %s \t\tGPIO %s\n", c,
1335 get_label(c), get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1336 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
1337 len = sprintf(buf, "GPIO_%d: %s \t\tPeripheral\n", c, get_label(c));
1346 static __init int gpio_register_proc(void)
1348 struct proc_dir_entry *proc_gpio;
1350 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1352 proc_gpio->read_proc = gpio_proc_read;
1353 return proc_gpio != NULL;
1355 __initcall(gpio_register_proc);