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 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)
241 if (label && str_ident) {
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)
253 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
256 static int cmp_label(unsigned short ident, const char *label)
260 printk(KERN_ERR "Please provide none-null label\n");
263 if (label && str_ident)
264 return strncmp(str_ident[ident].name,
265 label, strlen(label));
270 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
271 static void port_setup(unsigned gpio, unsigned short usage)
273 if (!check_gpio(gpio)) {
274 if (usage == GPIO_USAGE)
275 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
277 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
281 #elif defined(BF548_FAMILY)
282 static void port_setup(unsigned gpio, unsigned short usage)
284 if (usage == GPIO_USAGE)
285 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
287 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
291 # define port_setup(...) do { } while (0)
297 unsigned short offset;
299 {.res = P_PPI0_D13, .offset = 11},
300 {.res = P_PPI0_D14, .offset = 11},
301 {.res = P_PPI0_D15, .offset = 11},
302 {.res = P_SPORT1_TFS, .offset = 11},
303 {.res = P_SPORT1_TSCLK, .offset = 11},
304 {.res = P_SPORT1_DTPRI, .offset = 11},
305 {.res = P_PPI0_D10, .offset = 10},
306 {.res = P_PPI0_D11, .offset = 10},
307 {.res = P_PPI0_D12, .offset = 10},
308 {.res = P_SPORT1_RSCLK, .offset = 10},
309 {.res = P_SPORT1_RFS, .offset = 10},
310 {.res = P_SPORT1_DRPRI, .offset = 10},
311 {.res = P_PPI0_D8, .offset = 9},
312 {.res = P_PPI0_D9, .offset = 9},
313 {.res = P_SPORT1_DRSEC, .offset = 9},
314 {.res = P_SPORT1_DTSEC, .offset = 9},
315 {.res = P_TMR2, .offset = 8},
316 {.res = P_PPI0_FS3, .offset = 8},
317 {.res = P_TMR3, .offset = 7},
318 {.res = P_SPI0_SSEL4, .offset = 7},
319 {.res = P_TMR4, .offset = 6},
320 {.res = P_SPI0_SSEL5, .offset = 6},
321 {.res = P_TMR5, .offset = 5},
322 {.res = P_SPI0_SSEL6, .offset = 5},
323 {.res = P_UART1_RX, .offset = 4},
324 {.res = P_UART1_TX, .offset = 4},
325 {.res = P_TMR6, .offset = 4},
326 {.res = P_TMR7, .offset = 4},
327 {.res = P_UART0_RX, .offset = 3},
328 {.res = P_UART0_TX, .offset = 3},
329 {.res = P_DMAR0, .offset = 3},
330 {.res = P_DMAR1, .offset = 3},
331 {.res = P_SPORT0_DTSEC, .offset = 1},
332 {.res = P_SPORT0_DRSEC, .offset = 1},
333 {.res = P_CAN0_RX, .offset = 1},
334 {.res = P_CAN0_TX, .offset = 1},
335 {.res = P_SPI0_SSEL7, .offset = 1},
336 {.res = P_SPORT0_TFS, .offset = 0},
337 {.res = P_SPORT0_DTPRI, .offset = 0},
338 {.res = P_SPI0_SSEL2, .offset = 0},
339 {.res = P_SPI0_SSEL3, .offset = 0},
342 static void portmux_setup(unsigned short per, unsigned short function)
344 u16 y, offset, muxreg;
346 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
347 if (port_mux_lut[y].res == per) {
349 /* SET PORTMUX REG */
351 offset = port_mux_lut[y].offset;
352 muxreg = bfin_read_PORT_MUX();
355 muxreg &= ~(1 << offset);
359 muxreg |= (function << offset);
360 bfin_write_PORT_MUX(muxreg);
364 #elif defined(BF548_FAMILY)
365 inline void portmux_setup(unsigned short portno, unsigned short function)
369 pmux = gpio_array[gpio_bank(portno)]->port_mux;
371 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
372 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
374 gpio_array[gpio_bank(portno)]->port_mux = pmux;
377 inline u16 get_portmux(unsigned short portno)
381 pmux = gpio_array[gpio_bank(portno)]->port_mux;
383 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
385 #elif defined(BF527_FAMILY)
386 inline void portmux_setup(unsigned short portno, unsigned short function)
388 u16 pmux, ident = P_IDENT(portno);
389 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
391 pmux = *port_mux[gpio_bank(ident)];
392 pmux &= ~(3 << offset);
393 pmux |= (function & 3) << offset;
394 *port_mux[gpio_bank(ident)] = pmux;
398 # define portmux_setup(...) do { } while (0)
401 static int __init bfin_gpio_init(void)
403 printk(KERN_INFO "Blackfin GPIO Controller\n");
407 arch_initcall(bfin_gpio_init);
411 /***********************************************************
413 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
416 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
419 * DESCRIPTION: These functions abstract direct register access
420 * to Blackfin processor General Purpose
423 * CAUTION: These functions do not belong to the GPIO Driver API
424 *************************************************************
425 * MODIFICATION HISTORY :
426 **************************************************************/
428 /* Set a specific bit */
430 #define SET_GPIO(name) \
431 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
433 unsigned long flags; \
434 local_irq_save(flags); \
436 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
438 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
439 AWA_DUMMY_READ(name); \
440 local_irq_restore(flags); \
442 EXPORT_SYMBOL(set_gpio_ ## name);
451 #if ANOMALY_05000311 || ANOMALY_05000323
452 #define SET_GPIO_SC(name) \
453 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
455 unsigned long flags; \
456 local_irq_save(flags); \
458 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
460 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
461 AWA_DUMMY_READ(name); \
462 local_irq_restore(flags); \
464 EXPORT_SYMBOL(set_gpio_ ## name);
466 #define SET_GPIO_SC(name) \
467 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
470 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
472 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
474 EXPORT_SYMBOL(set_gpio_ ## name);
481 #if ANOMALY_05000311 || ANOMALY_05000323
482 void set_gpio_toggle(unsigned gpio)
485 local_irq_save(flags);
486 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
487 AWA_DUMMY_READ(toggle);
488 local_irq_restore(flags);
491 void set_gpio_toggle(unsigned gpio)
493 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
496 EXPORT_SYMBOL(set_gpio_toggle);
499 /*Set current PORT date (16-bit word)*/
501 #if ANOMALY_05000311 || ANOMALY_05000323
502 #define SET_GPIO_P(name) \
503 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
505 unsigned long flags; \
506 local_irq_save(flags); \
507 gpio_bankb[gpio_bank(gpio)]->name = arg; \
508 AWA_DUMMY_READ(name); \
509 local_irq_restore(flags); \
511 EXPORT_SYMBOL(set_gpiop_ ## name);
513 #define SET_GPIO_P(name) \
514 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
516 gpio_bankb[gpio_bank(gpio)]->name = arg; \
518 EXPORT_SYMBOL(set_gpiop_ ## name);
530 /* Get a specific bit */
531 #if ANOMALY_05000311 || ANOMALY_05000323
532 #define GET_GPIO(name) \
533 unsigned short get_gpio_ ## name(unsigned gpio) \
535 unsigned long flags; \
536 unsigned short ret; \
537 local_irq_save(flags); \
538 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
539 AWA_DUMMY_READ(name); \
540 local_irq_restore(flags); \
543 EXPORT_SYMBOL(get_gpio_ ## name);
545 #define GET_GPIO(name) \
546 unsigned short get_gpio_ ## name(unsigned gpio) \
548 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
550 EXPORT_SYMBOL(get_gpio_ ## name);
562 /*Get current PORT date (16-bit word)*/
564 #if ANOMALY_05000311 || ANOMALY_05000323
565 #define GET_GPIO_P(name) \
566 unsigned short get_gpiop_ ## name(unsigned gpio) \
568 unsigned long flags; \
569 unsigned short ret; \
570 local_irq_save(flags); \
571 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
572 AWA_DUMMY_READ(name); \
573 local_irq_restore(flags); \
576 EXPORT_SYMBOL(get_gpiop_ ## name);
578 #define GET_GPIO_P(name) \
579 unsigned short get_gpiop_ ## name(unsigned gpio) \
581 return (gpio_bankb[gpio_bank(gpio)]->name);\
583 EXPORT_SYMBOL(get_gpiop_ ## name);
597 /***********************************************************
599 * FUNCTIONS: Blackfin PM Setup API
602 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
610 * DESCRIPTION: Blackfin PM Driver API
613 *************************************************************
614 * MODIFICATION HISTORY :
615 **************************************************************/
616 int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
620 if ((check_gpio(gpio) < 0) || !type)
623 local_irq_save(flags);
624 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
625 wakeup_flags_map[gpio] = type;
626 local_irq_restore(flags);
630 EXPORT_SYMBOL(gpio_pm_wakeup_request);
632 void gpio_pm_wakeup_free(unsigned gpio)
636 if (check_gpio(gpio) < 0)
639 local_irq_save(flags);
641 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
643 local_irq_restore(flags);
645 EXPORT_SYMBOL(gpio_pm_wakeup_free);
647 static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
649 port_setup(gpio, GPIO_USAGE);
650 set_gpio_dir(gpio, 0);
651 set_gpio_inen(gpio, 1);
653 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
654 set_gpio_edge(gpio, 1);
656 set_gpio_edge(gpio, 0);
658 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
659 set_gpio_both(gpio, 1);
661 set_gpio_both(gpio, 0);
663 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
664 set_gpio_polar(gpio, 1);
666 set_gpio_polar(gpio, 0);
673 u32 bfin_pm_standby_setup(void)
675 u16 bank, mask, i, gpio;
677 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
678 mask = wakeup_map[gpio_bank(i)];
681 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
682 gpio_bankb[bank]->maskb = 0;
685 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
686 gpio_bank_saved[bank].fer = *port_fer[bank];
688 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
689 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
690 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
691 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
692 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
693 gpio_bank_saved[bank].reserved =
694 reserved_gpio_map[bank];
699 if ((mask & 1) && (wakeup_flags_map[gpio] !=
701 reserved_gpio_map[gpio_bank(gpio)] |=
703 bfin_gpio_wakeup_type(gpio,
704 wakeup_flags_map[gpio]);
705 set_gpio_data(gpio, 0); /*Clear*/
711 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
712 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
716 AWA_DUMMY_READ(maskb_set);
721 void bfin_pm_standby_restore(void)
725 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
726 mask = wakeup_map[gpio_bank(i)];
730 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
731 *port_fer[bank] = gpio_bank_saved[bank].fer;
733 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
734 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
735 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
736 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
737 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
739 reserved_gpio_map[bank] =
740 gpio_bank_saved[bank].reserved;
741 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
744 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
746 AWA_DUMMY_READ(maskb);
749 void bfin_gpio_pm_hibernate_suspend(void)
753 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
756 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
757 gpio_bank_saved[bank].fer = *port_fer[bank];
759 gpio_bank_saved[bank].mux = *port_mux[bank];
762 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
765 gpio_bank_saved[bank].data = gpio_bankb[bank]->data;
766 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
767 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
768 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
769 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
770 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
771 gpio_bank_saved[bank].maska = gpio_bankb[bank]->maska;
774 AWA_DUMMY_READ(maska);
777 void bfin_gpio_pm_hibernate_restore(void)
781 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
784 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
786 *port_mux[bank] = gpio_bank_saved[bank].mux;
789 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
791 *port_fer[bank] = gpio_bank_saved[bank].fer;
793 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
794 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
795 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
796 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
797 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
799 gpio_bankb[bank]->data_set = gpio_bank_saved[bank].data
800 | gpio_bank_saved[bank].dir;
802 gpio_bankb[bank]->maska = gpio_bank_saved[bank].maska;
804 AWA_DUMMY_READ(maska);
809 #else /* BF548_FAMILY */
812 u32 bfin_pm_standby_setup(void)
817 void bfin_pm_standby_restore(void)
822 void bfin_gpio_pm_hibernate_suspend(void)
826 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
829 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
830 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
831 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
832 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
833 gpio_bank_saved[bank].inen = gpio_array[bank]->port_inen;
834 gpio_bank_saved[bank].dir = gpio_array[bank]->port_dir_set;
838 void bfin_gpio_pm_hibernate_restore(void)
842 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
845 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
846 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
847 gpio_array[bank]->port_inen = gpio_bank_saved[bank].inen;
848 gpio_array[bank]->port_dir_set = gpio_bank_saved[bank].dir;
849 gpio_array[bank]->port_set = gpio_bank_saved[bank].data
850 | gpio_bank_saved[bank].dir;
855 unsigned short get_gpio_dir(unsigned gpio)
857 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
859 EXPORT_SYMBOL(get_gpio_dir);
861 #endif /* BF548_FAMILY */
863 /***********************************************************
865 * FUNCTIONS: Blackfin Peripheral Resource Allocation
869 * per Peripheral Identifier
872 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
875 *************************************************************
876 * MODIFICATION HISTORY :
877 **************************************************************/
880 int peripheral_request(unsigned short per, const char *label)
883 unsigned short ident = P_IDENT(per);
886 * Don't cares are pins with only one dedicated function
889 if (per & P_DONTCARE)
892 if (!(per & P_DEFINED))
895 if (check_gpio(ident) < 0)
898 local_irq_save(flags);
900 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
903 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
904 __func__, ident, get_label(ident));
905 local_irq_restore(flags);
909 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
911 u16 funct = get_portmux(ident);
914 * Pin functions like AMC address strobes my
915 * be requested and used by several drivers
918 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
921 * Allow that the identical pin function can
922 * be requested from the same driver twice
925 if (cmp_label(ident, label) == 0)
930 "%s: Peripheral %d function %d is already reserved by %s !\n",
931 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
932 local_irq_restore(flags);
938 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
940 portmux_setup(ident, P_FUNCT2MUX(per));
941 port_setup(ident, PERIPHERAL_USAGE);
943 local_irq_restore(flags);
944 set_label(ident, label);
948 EXPORT_SYMBOL(peripheral_request);
951 int peripheral_request(unsigned short per, const char *label)
954 unsigned short ident = P_IDENT(per);
957 * Don't cares are pins with only one dedicated function
960 if (per & P_DONTCARE)
963 if (!(per & P_DEFINED))
966 local_irq_save(flags);
968 if (!check_gpio(ident)) {
970 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
973 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
974 __func__, ident, get_label(ident));
975 local_irq_restore(flags);
981 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
984 * Pin functions like AMC address strobes my
985 * be requested and used by several drivers
988 if (!(per & P_MAYSHARE)) {
991 * Allow that the identical pin function can
992 * be requested from the same driver twice
995 if (cmp_label(ident, label) == 0)
1000 "%s: Peripheral %d function %d is already"
1001 " reserved by %s !\n",
1002 __func__, ident, P_FUNCT2MUX(per),
1004 local_irq_restore(flags);
1011 portmux_setup(per, P_FUNCT2MUX(per));
1013 port_setup(ident, PERIPHERAL_USAGE);
1015 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
1016 local_irq_restore(flags);
1017 set_label(ident, label);
1021 EXPORT_SYMBOL(peripheral_request);
1024 int peripheral_request_list(const unsigned short per[], const char *label)
1029 for (cnt = 0; per[cnt] != 0; cnt++) {
1031 ret = peripheral_request(per[cnt], label);
1034 for ( ; cnt > 0; cnt--)
1035 peripheral_free(per[cnt - 1]);
1043 EXPORT_SYMBOL(peripheral_request_list);
1045 void peripheral_free(unsigned short per)
1047 unsigned long flags;
1048 unsigned short ident = P_IDENT(per);
1050 if (per & P_DONTCARE)
1053 if (!(per & P_DEFINED))
1056 if (check_gpio(ident) < 0)
1059 local_irq_save(flags);
1061 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
1062 local_irq_restore(flags);
1066 if (!(per & P_MAYSHARE))
1067 port_setup(ident, GPIO_USAGE);
1069 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
1071 set_label(ident, "free");
1073 local_irq_restore(flags);
1075 EXPORT_SYMBOL(peripheral_free);
1077 void peripheral_free_list(const unsigned short per[])
1080 for (cnt = 0; per[cnt] != 0; cnt++)
1081 peripheral_free(per[cnt]);
1083 EXPORT_SYMBOL(peripheral_free_list);
1085 /***********************************************************
1087 * FUNCTIONS: Blackfin GPIO Driver
1090 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1093 * DESCRIPTION: Blackfin GPIO Driver API
1096 *************************************************************
1097 * MODIFICATION HISTORY :
1098 **************************************************************/
1100 int gpio_request(unsigned gpio, const char *label)
1102 unsigned long flags;
1104 if (check_gpio(gpio) < 0)
1107 local_irq_save(flags);
1110 * Allow that the identical GPIO can
1111 * be requested from the same driver twice
1112 * Do nothing and return -
1115 if (cmp_label(gpio, label) == 0) {
1116 local_irq_restore(flags);
1120 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1122 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1123 gpio, get_label(gpio));
1124 local_irq_restore(flags);
1127 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1130 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1131 gpio, get_label(gpio));
1132 local_irq_restore(flags);
1136 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
1138 local_irq_restore(flags);
1140 port_setup(gpio, GPIO_USAGE);
1141 set_label(gpio, label);
1145 EXPORT_SYMBOL(gpio_request);
1147 void gpio_free(unsigned gpio)
1149 unsigned long flags;
1151 if (check_gpio(gpio) < 0)
1154 local_irq_save(flags);
1156 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1159 local_irq_restore(flags);
1163 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1165 set_label(gpio, "free");
1167 local_irq_restore(flags);
1169 EXPORT_SYMBOL(gpio_free);
1173 int gpio_direction_input(unsigned gpio)
1175 unsigned long flags;
1177 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1182 local_irq_save(flags);
1183 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1184 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1185 local_irq_restore(flags);
1189 EXPORT_SYMBOL(gpio_direction_input);
1191 int gpio_direction_output(unsigned gpio, int value)
1193 unsigned long flags;
1195 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1200 local_irq_save(flags);
1201 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
1202 gpio_set_value(gpio, value);
1203 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1204 local_irq_restore(flags);
1208 EXPORT_SYMBOL(gpio_direction_output);
1210 void gpio_set_value(unsigned gpio, int arg)
1213 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1215 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
1217 EXPORT_SYMBOL(gpio_set_value);
1219 int gpio_get_value(unsigned gpio)
1221 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1223 EXPORT_SYMBOL(gpio_get_value);
1225 void bfin_gpio_irq_prepare(unsigned gpio)
1227 unsigned long flags;
1229 port_setup(gpio, GPIO_USAGE);
1231 local_irq_save(flags);
1232 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1233 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1234 local_irq_restore(flags);
1239 int gpio_get_value(unsigned gpio)
1241 unsigned long flags;
1244 if (unlikely(get_gpio_edge(gpio))) {
1245 local_irq_save(flags);
1246 set_gpio_edge(gpio, 0);
1247 ret = get_gpio_data(gpio);
1248 set_gpio_edge(gpio, 1);
1249 local_irq_restore(flags);
1253 return get_gpio_data(gpio);
1255 EXPORT_SYMBOL(gpio_get_value);
1258 int gpio_direction_input(unsigned gpio)
1260 unsigned long flags;
1262 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1267 local_irq_save(flags);
1268 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1269 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1270 AWA_DUMMY_READ(inen);
1271 local_irq_restore(flags);
1275 EXPORT_SYMBOL(gpio_direction_input);
1277 int gpio_direction_output(unsigned gpio, int value)
1279 unsigned long flags;
1281 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1286 local_irq_save(flags);
1287 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1290 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1292 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1294 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1295 AWA_DUMMY_READ(dir);
1296 local_irq_restore(flags);
1300 EXPORT_SYMBOL(gpio_direction_output);
1302 /* If we are booting from SPI and our board lacks a strong enough pull up,
1303 * the core can reset and execute the bootrom faster than the resistor can
1304 * pull the signal logically high. To work around this (common) error in
1305 * board design, we explicitly set the pin back to GPIO mode, force /CS
1306 * high, and wait for the electrons to do their thing.
1308 * This function only makes sense to be called from reset code, but it
1309 * lives here as we need to force all the GPIO states w/out going through
1310 * BUG() checks and such.
1312 void bfin_gpio_reset_spi0_ssel1(void)
1314 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1316 port_setup(gpio, GPIO_USAGE);
1317 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1318 AWA_DUMMY_READ(data_set);
1322 void bfin_gpio_irq_prepare(unsigned gpio)
1324 port_setup(gpio, GPIO_USAGE);
1327 #endif /*BF548_FAMILY */
1329 #if defined(CONFIG_PROC_FS)
1330 static int gpio_proc_read(char *buf, char **start, off_t offset,
1331 int len, int *unused_i, void *unused_v)
1335 for (c = 0; c < MAX_RESOURCES; c++) {
1336 if (!check_gpio(c) && (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c)))
1337 len = sprintf(buf, "GPIO_%d: %s \t\tGPIO %s\n", c,
1338 get_label(c), get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1339 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
1340 len = sprintf(buf, "GPIO_%d: %s \t\tPeripheral\n", c, get_label(c));
1349 static __init int gpio_register_proc(void)
1351 struct proc_dir_entry *proc_gpio;
1353 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1355 proc_gpio->read_proc = gpio_proc_read;
1356 return proc_gpio != NULL;
1358 __initcall(gpio_register_proc);