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/module.h>
84 #include <linux/err.h>
85 #include <asm/blackfin.h>
87 #include <asm/portmux.h>
88 #include <linux/irq.h>
91 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
92 (struct gpio_port_t *) FIO_FLAG_D,
97 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
98 (struct gpio_port_t *) PORTFIO,
99 (struct gpio_port_t *) PORTGIO,
100 (struct gpio_port_t *) PORTHIO,
103 static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
104 (unsigned short *) PORTF_FER,
105 (unsigned short *) PORTG_FER,
106 (unsigned short *) PORTH_FER,
112 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
113 (struct gpio_port_t *) FIO0_FLAG_D,
114 (struct gpio_port_t *) FIO1_FLAG_D,
115 (struct gpio_port_t *) FIO2_FLAG_D,
119 static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
120 static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)];
121 char *str_ident = NULL;
123 #define RESOURCE_LABEL_SIZE 16
126 static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
127 static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
128 static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
131 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
135 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
139 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
142 #endif /* CONFIG_PM */
144 inline int check_gpio(unsigned short gpio)
146 if (gpio >= MAX_BLACKFIN_GPIOS)
151 static void set_label(unsigned short ident, const char *label)
154 if (label && str_ident) {
155 strncpy(str_ident + ident * RESOURCE_LABEL_SIZE, label,
156 RESOURCE_LABEL_SIZE);
157 str_ident[ident * RESOURCE_LABEL_SIZE +
158 RESOURCE_LABEL_SIZE - 1] = 0;
162 static char *get_label(unsigned short ident)
167 return (str_ident[ident * RESOURCE_LABEL_SIZE] ?
168 (str_ident + ident * RESOURCE_LABEL_SIZE) : "UNKNOWN");
171 static int cmp_label(unsigned short ident, const char *label)
173 if (label && str_ident)
174 return strncmp(str_ident + ident * RESOURCE_LABEL_SIZE,
175 label, strlen(label));
181 static void port_setup(unsigned short gpio, unsigned short usage)
183 if (!check_gpio(gpio)) {
184 if (usage == GPIO_USAGE) {
185 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
187 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
192 # define port_setup(...) do { } while (0)
197 #define PMUX_LUT_RES 0
198 #define PMUX_LUT_OFFSET 1
199 #define PMUX_LUT_ENTRIES 41
200 #define PMUX_LUT_SIZE 2
202 static unsigned short port_mux_lut[PMUX_LUT_ENTRIES][PMUX_LUT_SIZE] = {
203 {P_PPI0_D13, 11}, {P_PPI0_D14, 11}, {P_PPI0_D15, 11},
204 {P_SPORT1_TFS, 11}, {P_SPORT1_TSCLK, 11}, {P_SPORT1_DTPRI, 11},
205 {P_PPI0_D10, 10}, {P_PPI0_D11, 10}, {P_PPI0_D12, 10},
206 {P_SPORT1_RSCLK, 10}, {P_SPORT1_RFS, 10}, {P_SPORT1_DRPRI, 10},
207 {P_PPI0_D8, 9}, {P_PPI0_D9, 9}, {P_SPORT1_DRSEC, 9},
208 {P_SPORT1_DTSEC, 9}, {P_TMR2, 8}, {P_PPI0_FS3, 8}, {P_TMR3, 7},
209 {P_SPI0_SSEL4, 7}, {P_TMR4, 6}, {P_SPI0_SSEL5, 6}, {P_TMR5, 5},
210 {P_SPI0_SSEL6, 5}, {P_UART1_RX, 4}, {P_UART1_TX, 4}, {P_TMR6, 4},
211 {P_TMR7, 4}, {P_UART0_RX, 3}, {P_UART0_TX, 3}, {P_DMAR0, 3},
212 {P_DMAR1, 3}, {P_SPORT0_DTSEC, 1}, {P_SPORT0_DRSEC, 1},
213 {P_CAN0_RX, 1}, {P_CAN0_TX, 1}, {P_SPI0_SSEL7, 1},
214 {P_SPORT0_TFS, 0}, {P_SPORT0_DTPRI, 0}, {P_SPI0_SSEL2, 0},
218 static void portmux_setup(unsigned short per, unsigned short function)
220 u16 y, muxreg, offset;
222 for (y = 0; y < PMUX_LUT_ENTRIES; y++) {
223 if (port_mux_lut[y][PMUX_LUT_RES] == per) {
225 /* SET PORTMUX REG */
227 offset = port_mux_lut[y][PMUX_LUT_OFFSET];
228 muxreg = bfin_read_PORT_MUX();
231 muxreg &= ~(1 << offset);
236 muxreg |= (function << offset);
237 bfin_write_PORT_MUX(muxreg);
243 # define portmux_setup(...) do { } while (0)
246 static void default_gpio(unsigned short gpio)
248 unsigned short bank, bitmask;
250 bank = gpio_bank(gpio);
251 bitmask = gpio_bit(gpio);
253 gpio_bankb[bank]->maska_clear = bitmask;
254 gpio_bankb[bank]->maskb_clear = bitmask;
256 gpio_bankb[bank]->inen &= ~bitmask;
257 gpio_bankb[bank]->dir &= ~bitmask;
258 gpio_bankb[bank]->polar &= ~bitmask;
259 gpio_bankb[bank]->both &= ~bitmask;
260 gpio_bankb[bank]->edge &= ~bitmask;
263 static int __init bfin_gpio_init(void)
266 str_ident = kzalloc(RESOURCE_LABEL_SIZE * 256, GFP_KERNEL);
270 printk(KERN_INFO "Blackfin GPIO Controller\n");
276 arch_initcall(bfin_gpio_init);
279 /***********************************************************
281 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
284 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
287 * DESCRIPTION: These functions abstract direct register access
288 * to Blackfin processor General Purpose
291 * CAUTION: These functions do not belong to the GPIO Driver API
292 *************************************************************
293 * MODIFICATION HISTORY :
294 **************************************************************/
296 /* Set a specific bit */
298 #define SET_GPIO(name) \
299 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
301 unsigned long flags; \
302 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
303 local_irq_save(flags); \
305 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
307 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
308 local_irq_restore(flags); \
310 EXPORT_SYMBOL(set_gpio_ ## name);
319 #define SET_GPIO_SC(name) \
320 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
322 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
324 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
326 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
328 EXPORT_SYMBOL(set_gpio_ ## name);
333 #if defined(ANOMALY_05000311)
334 void set_gpio_data(unsigned short gpio, unsigned short arg)
337 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
338 local_irq_save(flags);
340 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
342 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
344 local_irq_restore(flags);
346 EXPORT_SYMBOL(set_gpio_data);
352 #if defined(ANOMALY_05000311)
353 void set_gpio_toggle(unsigned short gpio)
356 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
357 local_irq_save(flags);
358 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
360 local_irq_restore(flags);
363 void set_gpio_toggle(unsigned short gpio)
365 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
366 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
369 EXPORT_SYMBOL(set_gpio_toggle);
372 /*Set current PORT date (16-bit word)*/
374 #define SET_GPIO_P(name) \
375 void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
377 gpio_bankb[gpio_bank(gpio)]->name = arg; \
379 EXPORT_SYMBOL(set_gpiop_ ## name);
390 #if defined(ANOMALY_05000311)
391 void set_gpiop_data(unsigned short gpio, unsigned short arg)
394 local_irq_save(flags);
395 gpio_bankb[gpio_bank(gpio)]->data = arg;
397 local_irq_restore(flags);
399 EXPORT_SYMBOL(set_gpiop_data);
406 /* Get a specific bit */
408 #define GET_GPIO(name) \
409 unsigned short get_gpio_ ## name(unsigned short gpio) \
411 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
413 EXPORT_SYMBOL(get_gpio_ ## name);
424 #if defined(ANOMALY_05000311)
425 unsigned short get_gpio_data(unsigned short gpio)
429 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
430 local_irq_save(flags);
431 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
433 local_irq_restore(flags);
436 EXPORT_SYMBOL(get_gpio_data);
441 /*Get current PORT date (16-bit word)*/
443 #define GET_GPIO_P(name) \
444 unsigned short get_gpiop_ ## name(unsigned short gpio) \
446 return (gpio_bankb[gpio_bank(gpio)]->name);\
448 EXPORT_SYMBOL(get_gpiop_ ## name);
458 #if defined(ANOMALY_05000311)
459 unsigned short get_gpiop_data(unsigned short gpio)
463 local_irq_save(flags);
464 ret = gpio_bankb[gpio_bank(gpio)]->data;
466 local_irq_restore(flags);
469 EXPORT_SYMBOL(get_gpiop_data);
475 /***********************************************************
477 * FUNCTIONS: Blackfin PM Setup API
480 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
488 * DESCRIPTION: Blackfin PM Driver API
491 *************************************************************
492 * MODIFICATION HISTORY :
493 **************************************************************/
494 int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type)
498 if ((check_gpio(gpio) < 0) || !type)
501 local_irq_save(flags);
503 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
504 wakeup_flags_map[gpio] = type;
505 local_irq_restore(flags);
509 EXPORT_SYMBOL(gpio_pm_wakeup_request);
511 void gpio_pm_wakeup_free(unsigned short gpio)
515 if (check_gpio(gpio) < 0)
518 local_irq_save(flags);
520 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
522 local_irq_restore(flags);
524 EXPORT_SYMBOL(gpio_pm_wakeup_free);
526 static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type)
528 port_setup(gpio, GPIO_USAGE);
529 set_gpio_dir(gpio, 0);
530 set_gpio_inen(gpio, 1);
532 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
533 set_gpio_edge(gpio, 1);
535 set_gpio_edge(gpio, 0);
537 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
538 set_gpio_both(gpio, 1);
540 set_gpio_both(gpio, 0);
542 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
543 set_gpio_polar(gpio, 1);
545 set_gpio_polar(gpio, 0);
552 u32 gpio_pm_setup(void)
555 u16 bank, mask, i, gpio;
557 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
558 mask = wakeup_map[gpio_bank(i)];
561 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
562 gpio_bankb[bank]->maskb = 0;
566 gpio_bank_saved[bank].fer = *port_fer[bank];
568 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
569 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
570 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
571 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
572 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
573 gpio_bank_saved[bank].reserved =
574 reserved_gpio_map[bank];
580 reserved_gpio_map[gpio_bank(gpio)] |=
582 bfin_gpio_wakeup_type(gpio,
583 wakeup_flags_map[gpio]);
584 set_gpio_data(gpio, 0); /*Clear*/
591 (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
592 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
599 return IWR_ENABLE_ALL;
602 void gpio_pm_restore(void)
606 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
607 mask = wakeup_map[gpio_bank(i)];
612 *port_fer[bank] = gpio_bank_saved[bank].fer;
614 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
615 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
616 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
617 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
618 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
620 reserved_gpio_map[bank] =
621 gpio_bank_saved[bank].reserved;
625 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
634 int peripheral_request(unsigned short per, const char *label)
637 unsigned short ident = P_IDENT(per);
640 * Don't cares are pins with only one dedicated function
643 if (per & P_DONTCARE)
646 if (!(per & P_DEFINED))
649 local_irq_save(flags);
651 if (!check_gpio(ident)) {
653 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
655 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
656 __FUNCTION__, ident, get_label(ident));
658 local_irq_restore(flags);
664 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
667 * Pin functions like AMC address strobes my
668 * be requested and used by several drivers
671 if (!(per & P_MAYSHARE)) {
674 * Allow that the identical pin function can
675 * be requested from the same driver twice
678 if (cmp_label(ident, label) == 0)
682 "%s: Peripheral %d function %d is already"
683 "reserved by %s !\n",
684 __FUNCTION__, ident, P_FUNCT2MUX(per),
687 local_irq_restore(flags);
696 portmux_setup(per, P_FUNCT2MUX(per));
698 port_setup(ident, PERIPHERAL_USAGE);
700 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
701 local_irq_restore(flags);
702 set_label(ident, label);
706 EXPORT_SYMBOL(peripheral_request);
708 int peripheral_request_list(unsigned short per[], const char *label)
713 for (cnt = 0; per[cnt] != 0; cnt++) {
714 ret = peripheral_request(per[cnt], label);
721 EXPORT_SYMBOL(peripheral_request_list);
723 void peripheral_free(unsigned short per)
726 unsigned short ident = P_IDENT(per);
728 if (per & P_DONTCARE)
731 if (!(per & P_DEFINED))
734 if (check_gpio(ident) < 0)
737 local_irq_save(flags);
739 if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
740 & gpio_bit(ident)))) {
741 local_irq_restore(flags);
745 if (!(per & P_MAYSHARE)) {
746 port_setup(ident, GPIO_USAGE);
749 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
751 local_irq_restore(flags);
753 EXPORT_SYMBOL(peripheral_free);
755 void peripheral_free_list(unsigned short per[])
759 for (cnt = 0; per[cnt] != 0; cnt++) {
760 peripheral_free(per[cnt]);
764 EXPORT_SYMBOL(peripheral_free_list);
766 /***********************************************************
768 * FUNCTIONS: Blackfin GPIO Driver
771 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
774 * DESCRIPTION: Blackfin GPIO Driver API
777 *************************************************************
778 * MODIFICATION HISTORY :
779 **************************************************************/
781 int gpio_request(unsigned short gpio, const char *label)
785 if (check_gpio(gpio) < 0)
788 local_irq_save(flags);
790 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
791 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
793 local_irq_restore(flags);
796 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
798 local_irq_restore(flags);
800 port_setup(gpio, GPIO_USAGE);
804 EXPORT_SYMBOL(gpio_request);
806 void gpio_free(unsigned short gpio)
810 if (check_gpio(gpio) < 0)
813 local_irq_save(flags);
815 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
816 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
818 local_irq_restore(flags);
824 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
826 local_irq_restore(flags);
828 EXPORT_SYMBOL(gpio_free);
830 void gpio_direction_input(unsigned short gpio)
834 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
836 local_irq_save(flags);
837 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
838 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
839 local_irq_restore(flags);
841 EXPORT_SYMBOL(gpio_direction_input);
843 void gpio_direction_output(unsigned short gpio)
847 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
849 local_irq_save(flags);
850 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
851 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
852 local_irq_restore(flags);
854 EXPORT_SYMBOL(gpio_direction_output);