2 * File: arch/blackfin/kernel/bfin_gpio.c
4 * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
7 * Description: GPIO Abstraction Layer
10 * Copyright 2006 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
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 <asm/blackfin.h>
88 #include <asm/portmux.h>
89 #include <linux/irq.h>
92 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
93 (struct gpio_port_t *) FIO_FLAG_D,
98 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
99 (struct gpio_port_t *) PORTFIO,
100 (struct gpio_port_t *) PORTGIO,
101 (struct gpio_port_t *) PORTHIO,
104 static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
105 (unsigned short *) PORTF_FER,
106 (unsigned short *) PORTG_FER,
107 (unsigned short *) PORTH_FER,
113 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
114 (struct gpio_port_t *) FIO0_FLAG_D,
115 (struct gpio_port_t *) FIO1_FLAG_D,
116 (struct gpio_port_t *) FIO2_FLAG_D,
120 static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
121 static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)];
123 #define MAX_RESOURCES 256
124 #define RESOURCE_LABEL_SIZE 16
127 char name[RESOURCE_LABEL_SIZE];
132 static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
133 static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
134 static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
137 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
141 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
145 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
148 #endif /* CONFIG_PM */
150 inline int check_gpio(unsigned short gpio)
152 if (gpio >= MAX_BLACKFIN_GPIOS)
157 static void set_label(unsigned short ident, const char *label)
160 if (label && str_ident) {
161 strncpy(str_ident[ident].name, label,
162 RESOURCE_LABEL_SIZE);
163 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
167 static char *get_label(unsigned short ident)
172 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
175 static int cmp_label(unsigned short ident, const char *label)
177 if (label && str_ident)
178 return strncmp(str_ident[ident].name,
179 label, strlen(label));
185 static void port_setup(unsigned short gpio, unsigned short usage)
187 if (!check_gpio(gpio)) {
188 if (usage == GPIO_USAGE) {
189 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
191 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
196 # define port_setup(...) do { } while (0)
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, unsigned short function)
250 u16 y, offset, muxreg;
252 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
253 if (port_mux_lut[y].res == per) {
255 /* SET PORTMUX REG */
257 offset = port_mux_lut[y].offset;
258 muxreg = bfin_read_PORT_MUX();
261 muxreg &= ~(1 << offset);
266 muxreg |= (function << offset);
267 bfin_write_PORT_MUX(muxreg);
273 # define portmux_setup(...) do { } while (0)
276 static void default_gpio(unsigned short gpio)
278 unsigned short bank, bitmask;
280 bank = gpio_bank(gpio);
281 bitmask = gpio_bit(gpio);
283 gpio_bankb[bank]->maska_clear = bitmask;
284 gpio_bankb[bank]->maskb_clear = bitmask;
286 gpio_bankb[bank]->inen &= ~bitmask;
287 gpio_bankb[bank]->dir &= ~bitmask;
288 gpio_bankb[bank]->polar &= ~bitmask;
289 gpio_bankb[bank]->both &= ~bitmask;
290 gpio_bankb[bank]->edge &= ~bitmask;
293 static int __init bfin_gpio_init(void)
295 str_ident = kcalloc(MAX_RESOURCES,
296 sizeof(struct str_ident), GFP_KERNEL);
297 if (str_ident == NULL)
300 memset(str_ident, 0, MAX_RESOURCES * sizeof(struct str_ident));
302 printk(KERN_INFO "Blackfin GPIO Controller\n");
307 arch_initcall(bfin_gpio_init);
310 /***********************************************************
312 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
315 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
318 * DESCRIPTION: These functions abstract direct register access
319 * to Blackfin processor General Purpose
322 * CAUTION: These functions do not belong to the GPIO Driver API
323 *************************************************************
324 * MODIFICATION HISTORY :
325 **************************************************************/
327 /* Set a specific bit */
329 #define SET_GPIO(name) \
330 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
332 unsigned long flags; \
333 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
334 local_irq_save(flags); \
336 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
338 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
339 local_irq_restore(flags); \
341 EXPORT_SYMBOL(set_gpio_ ## name);
350 #define SET_GPIO_SC(name) \
351 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
353 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
355 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
357 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
359 EXPORT_SYMBOL(set_gpio_ ## name);
365 void set_gpio_data(unsigned short gpio, unsigned short arg)
368 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
369 local_irq_save(flags);
371 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
373 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
375 local_irq_restore(flags);
377 EXPORT_SYMBOL(set_gpio_data);
384 void set_gpio_toggle(unsigned short gpio)
387 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
388 local_irq_save(flags);
389 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
391 local_irq_restore(flags);
394 void set_gpio_toggle(unsigned short gpio)
396 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
397 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
400 EXPORT_SYMBOL(set_gpio_toggle);
403 /*Set current PORT date (16-bit word)*/
405 #define SET_GPIO_P(name) \
406 void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
408 gpio_bankb[gpio_bank(gpio)]->name = arg; \
410 EXPORT_SYMBOL(set_gpiop_ ## name);
422 void set_gpiop_data(unsigned short gpio, unsigned short arg)
425 local_irq_save(flags);
426 gpio_bankb[gpio_bank(gpio)]->data = arg;
428 local_irq_restore(flags);
430 EXPORT_SYMBOL(set_gpiop_data);
437 /* Get a specific bit */
439 #define GET_GPIO(name) \
440 unsigned short get_gpio_ ## name(unsigned short gpio) \
442 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
444 EXPORT_SYMBOL(get_gpio_ ## name);
456 unsigned short get_gpio_data(unsigned short gpio)
460 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
461 local_irq_save(flags);
462 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
464 local_irq_restore(flags);
467 EXPORT_SYMBOL(get_gpio_data);
472 /*Get current PORT date (16-bit word)*/
474 #define GET_GPIO_P(name) \
475 unsigned short get_gpiop_ ## name(unsigned short gpio) \
477 return (gpio_bankb[gpio_bank(gpio)]->name);\
479 EXPORT_SYMBOL(get_gpiop_ ## name);
490 unsigned short get_gpiop_data(unsigned short gpio)
494 local_irq_save(flags);
495 ret = gpio_bankb[gpio_bank(gpio)]->data;
497 local_irq_restore(flags);
500 EXPORT_SYMBOL(get_gpiop_data);
506 /***********************************************************
508 * FUNCTIONS: Blackfin PM Setup API
511 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
519 * DESCRIPTION: Blackfin PM Driver API
522 *************************************************************
523 * MODIFICATION HISTORY :
524 **************************************************************/
525 int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type)
529 if ((check_gpio(gpio) < 0) || !type)
532 local_irq_save(flags);
534 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
535 wakeup_flags_map[gpio] = type;
536 local_irq_restore(flags);
540 EXPORT_SYMBOL(gpio_pm_wakeup_request);
542 void gpio_pm_wakeup_free(unsigned short gpio)
546 if (check_gpio(gpio) < 0)
549 local_irq_save(flags);
551 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
553 local_irq_restore(flags);
555 EXPORT_SYMBOL(gpio_pm_wakeup_free);
557 static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type)
559 port_setup(gpio, GPIO_USAGE);
560 set_gpio_dir(gpio, 0);
561 set_gpio_inen(gpio, 1);
563 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
564 set_gpio_edge(gpio, 1);
566 set_gpio_edge(gpio, 0);
568 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
569 set_gpio_both(gpio, 1);
571 set_gpio_both(gpio, 0);
573 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
574 set_gpio_polar(gpio, 1);
576 set_gpio_polar(gpio, 0);
583 u32 gpio_pm_setup(void)
586 u16 bank, mask, i, gpio;
588 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
589 mask = wakeup_map[gpio_bank(i)];
592 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
593 gpio_bankb[bank]->maskb = 0;
597 gpio_bank_saved[bank].fer = *port_fer[bank];
599 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
600 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
601 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
602 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
603 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
604 gpio_bank_saved[bank].reserved =
605 reserved_gpio_map[bank];
611 reserved_gpio_map[gpio_bank(gpio)] |=
613 bfin_gpio_wakeup_type(gpio,
614 wakeup_flags_map[gpio]);
615 set_gpio_data(gpio, 0); /*Clear*/
622 (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
623 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
630 return IWR_ENABLE_ALL;
633 void gpio_pm_restore(void)
637 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
638 mask = wakeup_map[gpio_bank(i)];
643 *port_fer[bank] = gpio_bank_saved[bank].fer;
645 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
646 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
647 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
648 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
649 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
651 reserved_gpio_map[bank] =
652 gpio_bank_saved[bank].reserved;
656 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
665 int peripheral_request(unsigned short per, const char *label)
668 unsigned short ident = P_IDENT(per);
671 * Don't cares are pins with only one dedicated function
674 if (per & P_DONTCARE)
677 if (!(per & P_DEFINED))
680 local_irq_save(flags);
682 if (!check_gpio(ident)) {
684 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
686 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
687 __FUNCTION__, ident, get_label(ident));
689 local_irq_restore(flags);
695 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
698 * Pin functions like AMC address strobes my
699 * be requested and used by several drivers
702 if (!(per & P_MAYSHARE)) {
705 * Allow that the identical pin function can
706 * be requested from the same driver twice
709 if (cmp_label(ident, label) == 0)
713 "%s: Peripheral %d function %d is already"
714 " reserved by %s !\n",
715 __FUNCTION__, ident, P_FUNCT2MUX(per),
718 local_irq_restore(flags);
727 portmux_setup(per, P_FUNCT2MUX(per));
729 port_setup(ident, PERIPHERAL_USAGE);
731 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
732 local_irq_restore(flags);
733 set_label(ident, label);
737 EXPORT_SYMBOL(peripheral_request);
739 int peripheral_request_list(unsigned short per[], const char *label)
744 for (cnt = 0; per[cnt] != 0; cnt++) {
746 ret = peripheral_request(per[cnt], label);
749 for ( ; cnt > 0; cnt--) {
750 peripheral_free(per[cnt - 1]);
758 EXPORT_SYMBOL(peripheral_request_list);
760 void peripheral_free(unsigned short per)
763 unsigned short ident = P_IDENT(per);
765 if (per & P_DONTCARE)
768 if (!(per & P_DEFINED))
771 if (check_gpio(ident) < 0)
774 local_irq_save(flags);
776 if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
777 & gpio_bit(ident)))) {
778 local_irq_restore(flags);
782 if (!(per & P_MAYSHARE)) {
783 port_setup(ident, GPIO_USAGE);
786 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
788 local_irq_restore(flags);
790 EXPORT_SYMBOL(peripheral_free);
792 void peripheral_free_list(unsigned short per[])
796 for (cnt = 0; per[cnt] != 0; cnt++) {
797 peripheral_free(per[cnt]);
801 EXPORT_SYMBOL(peripheral_free_list);
803 /***********************************************************
805 * FUNCTIONS: Blackfin GPIO Driver
808 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
811 * DESCRIPTION: Blackfin GPIO Driver API
814 *************************************************************
815 * MODIFICATION HISTORY :
816 **************************************************************/
818 int gpio_request(unsigned short gpio, const char *label)
822 if (check_gpio(gpio) < 0)
825 local_irq_save(flags);
827 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
828 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
830 local_irq_restore(flags);
833 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
835 local_irq_restore(flags);
837 port_setup(gpio, GPIO_USAGE);
841 EXPORT_SYMBOL(gpio_request);
843 void gpio_free(unsigned short gpio)
847 if (check_gpio(gpio) < 0)
850 local_irq_save(flags);
852 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
853 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
855 local_irq_restore(flags);
861 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
863 local_irq_restore(flags);
865 EXPORT_SYMBOL(gpio_free);
867 void gpio_direction_input(unsigned short gpio)
871 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
873 local_irq_save(flags);
874 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
875 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
876 local_irq_restore(flags);
878 EXPORT_SYMBOL(gpio_direction_input);
880 void gpio_direction_output(unsigned short gpio)
884 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
886 local_irq_save(flags);
887 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
888 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
889 local_irq_restore(flags);
891 EXPORT_SYMBOL(gpio_direction_output);
893 /* If we are booting from SPI and our board lacks a strong enough pull up,
894 * the core can reset and execute the bootrom faster than the resistor can
895 * pull the signal logically high. To work around this (common) error in
896 * board design, we explicitly set the pin back to GPIO mode, force /CS
897 * high, and wait for the electrons to do their thing.
899 * This function only makes sense to be called from reset code, but it
900 * lives here as we need to force all the GPIO states w/out going through
901 * BUG() checks and such.
903 void bfin_gpio_reset_spi0_ssel1(void)
905 port_setup(P_SPI0_SSEL1, GPIO_USAGE);
906 gpio_bankb[gpio_bank(P_SPI0_SSEL1)]->data_set = gpio_bit(P_SPI0_SSEL1);