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
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {
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
[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
,
142 #if defined(BF527_FAMILY) || defined(BF518_FAMILY)
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
};
209 #if defined(BF527_FAMILY) || defined(BF518_FAMILY)
210 static unsigned int sic_iwr_irqs
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
};
214 static unsigned int sic_iwr_irqs
[gpio_bank(MAX_BLACKFIN_GPIOS
)] = {IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
};
217 #endif /* CONFIG_PM */
219 inline int check_gpio(unsigned gpio
)
221 #if defined(BF548_FAMILY)
222 if (gpio
== GPIO_PB15
|| gpio
== GPIO_PC14
|| gpio
== GPIO_PC15
223 || gpio
== GPIO_PH14
|| gpio
== GPIO_PH15
224 || gpio
== GPIO_PJ14
|| gpio
== GPIO_PJ15
)
227 if (gpio
>= MAX_BLACKFIN_GPIOS
)
232 static void gpio_error(unsigned gpio
)
234 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
237 static void set_label(unsigned short ident
, const char *label
)
240 strncpy(str_ident
[ident
].name
, label
,
241 RESOURCE_LABEL_SIZE
);
242 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
246 static char *get_label(unsigned short ident
)
248 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
251 static int cmp_label(unsigned short ident
, const char *label
)
255 printk(KERN_ERR
"Please provide none-null label\n");
259 return strcmp(str_ident
[ident
].name
, label
);
264 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
265 static void port_setup(unsigned gpio
, unsigned short usage
)
267 if (!check_gpio(gpio
)) {
268 if (usage
== GPIO_USAGE
)
269 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
271 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
275 #elif defined(BF548_FAMILY)
276 static void port_setup(unsigned gpio
, unsigned short usage
)
278 if (usage
== GPIO_USAGE
)
279 gpio_array
[gpio_bank(gpio
)]->port_fer
&= ~gpio_bit(gpio
);
281 gpio_array
[gpio_bank(gpio
)]->port_fer
|= gpio_bit(gpio
);
285 # define port_setup(...) do { } while (0)
291 unsigned short offset
;
293 {.res
= P_PPI0_D13
, .offset
= 11},
294 {.res
= P_PPI0_D14
, .offset
= 11},
295 {.res
= P_PPI0_D15
, .offset
= 11},
296 {.res
= P_SPORT1_TFS
, .offset
= 11},
297 {.res
= P_SPORT1_TSCLK
, .offset
= 11},
298 {.res
= P_SPORT1_DTPRI
, .offset
= 11},
299 {.res
= P_PPI0_D10
, .offset
= 10},
300 {.res
= P_PPI0_D11
, .offset
= 10},
301 {.res
= P_PPI0_D12
, .offset
= 10},
302 {.res
= P_SPORT1_RSCLK
, .offset
= 10},
303 {.res
= P_SPORT1_RFS
, .offset
= 10},
304 {.res
= P_SPORT1_DRPRI
, .offset
= 10},
305 {.res
= P_PPI0_D8
, .offset
= 9},
306 {.res
= P_PPI0_D9
, .offset
= 9},
307 {.res
= P_SPORT1_DRSEC
, .offset
= 9},
308 {.res
= P_SPORT1_DTSEC
, .offset
= 9},
309 {.res
= P_TMR2
, .offset
= 8},
310 {.res
= P_PPI0_FS3
, .offset
= 8},
311 {.res
= P_TMR3
, .offset
= 7},
312 {.res
= P_SPI0_SSEL4
, .offset
= 7},
313 {.res
= P_TMR4
, .offset
= 6},
314 {.res
= P_SPI0_SSEL5
, .offset
= 6},
315 {.res
= P_TMR5
, .offset
= 5},
316 {.res
= P_SPI0_SSEL6
, .offset
= 5},
317 {.res
= P_UART1_RX
, .offset
= 4},
318 {.res
= P_UART1_TX
, .offset
= 4},
319 {.res
= P_TMR6
, .offset
= 4},
320 {.res
= P_TMR7
, .offset
= 4},
321 {.res
= P_UART0_RX
, .offset
= 3},
322 {.res
= P_UART0_TX
, .offset
= 3},
323 {.res
= P_DMAR0
, .offset
= 3},
324 {.res
= P_DMAR1
, .offset
= 3},
325 {.res
= P_SPORT0_DTSEC
, .offset
= 1},
326 {.res
= P_SPORT0_DRSEC
, .offset
= 1},
327 {.res
= P_CAN0_RX
, .offset
= 1},
328 {.res
= P_CAN0_TX
, .offset
= 1},
329 {.res
= P_SPI0_SSEL7
, .offset
= 1},
330 {.res
= P_SPORT0_TFS
, .offset
= 0},
331 {.res
= P_SPORT0_DTPRI
, .offset
= 0},
332 {.res
= P_SPI0_SSEL2
, .offset
= 0},
333 {.res
= P_SPI0_SSEL3
, .offset
= 0},
336 static void portmux_setup(unsigned short per
, unsigned short function
)
338 u16 y
, offset
, muxreg
;
340 for (y
= 0; y
< ARRAY_SIZE(port_mux_lut
); y
++) {
341 if (port_mux_lut
[y
].res
== per
) {
343 /* SET PORTMUX REG */
345 offset
= port_mux_lut
[y
].offset
;
346 muxreg
= bfin_read_PORT_MUX();
349 muxreg
&= ~(1 << offset
);
353 muxreg
|= (function
<< offset
);
354 bfin_write_PORT_MUX(muxreg
);
358 #elif defined(BF548_FAMILY)
359 inline void portmux_setup(unsigned short portno
, unsigned short function
)
363 pmux
= gpio_array
[gpio_bank(portno
)]->port_mux
;
365 pmux
&= ~(0x3 << (2 * gpio_sub_n(portno
)));
366 pmux
|= (function
& 0x3) << (2 * gpio_sub_n(portno
));
368 gpio_array
[gpio_bank(portno
)]->port_mux
= pmux
;
371 inline u16
get_portmux(unsigned short portno
)
375 pmux
= gpio_array
[gpio_bank(portno
)]->port_mux
;
377 return (pmux
>> (2 * gpio_sub_n(portno
)) & 0x3);
379 #elif defined(BF527_FAMILY) || defined(BF518_FAMILY)
380 inline void portmux_setup(unsigned short portno
, unsigned short function
)
382 u16 pmux
, ident
= P_IDENT(portno
);
383 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
385 pmux
= *port_mux
[gpio_bank(ident
)];
386 pmux
&= ~(3 << offset
);
387 pmux
|= (function
& 3) << offset
;
388 *port_mux
[gpio_bank(ident
)] = pmux
;
392 # define portmux_setup(...) do { } while (0)
395 static int __init
bfin_gpio_init(void)
397 printk(KERN_INFO
"Blackfin GPIO Controller\n");
401 arch_initcall(bfin_gpio_init
);
405 /***********************************************************
407 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
410 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
413 * DESCRIPTION: These functions abstract direct register access
414 * to Blackfin processor General Purpose
417 * CAUTION: These functions do not belong to the GPIO Driver API
418 *************************************************************
419 * MODIFICATION HISTORY :
420 **************************************************************/
422 /* Set a specific bit */
424 #define SET_GPIO(name) \
425 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
427 unsigned long flags; \
428 local_irq_save(flags); \
430 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
432 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
433 AWA_DUMMY_READ(name); \
434 local_irq_restore(flags); \
436 EXPORT_SYMBOL(set_gpio_ ## name);
445 #if ANOMALY_05000311 || ANOMALY_05000323
446 #define SET_GPIO_SC(name) \
447 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
449 unsigned long flags; \
450 local_irq_save(flags); \
452 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
454 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
455 AWA_DUMMY_READ(name); \
456 local_irq_restore(flags); \
458 EXPORT_SYMBOL(set_gpio_ ## name);
460 #define SET_GPIO_SC(name) \
461 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
464 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
466 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
468 EXPORT_SYMBOL(set_gpio_ ## name);
475 #if ANOMALY_05000311 || ANOMALY_05000323
476 void set_gpio_toggle(unsigned gpio
)
479 local_irq_save(flags
);
480 gpio_bankb
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
481 AWA_DUMMY_READ(toggle
);
482 local_irq_restore(flags
);
485 void set_gpio_toggle(unsigned gpio
)
487 gpio_bankb
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
490 EXPORT_SYMBOL(set_gpio_toggle
);
493 /*Set current PORT date (16-bit word)*/
495 #if ANOMALY_05000311 || ANOMALY_05000323
496 #define SET_GPIO_P(name) \
497 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
499 unsigned long flags; \
500 local_irq_save(flags); \
501 gpio_bankb[gpio_bank(gpio)]->name = arg; \
502 AWA_DUMMY_READ(name); \
503 local_irq_restore(flags); \
505 EXPORT_SYMBOL(set_gpiop_ ## name);
507 #define SET_GPIO_P(name) \
508 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
510 gpio_bankb[gpio_bank(gpio)]->name = arg; \
512 EXPORT_SYMBOL(set_gpiop_ ## name);
524 /* Get a specific bit */
525 #if ANOMALY_05000311 || ANOMALY_05000323
526 #define GET_GPIO(name) \
527 unsigned short get_gpio_ ## name(unsigned gpio) \
529 unsigned long flags; \
530 unsigned short ret; \
531 local_irq_save(flags); \
532 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
533 AWA_DUMMY_READ(name); \
534 local_irq_restore(flags); \
537 EXPORT_SYMBOL(get_gpio_ ## name);
539 #define GET_GPIO(name) \
540 unsigned short get_gpio_ ## name(unsigned gpio) \
542 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
544 EXPORT_SYMBOL(get_gpio_ ## name);
556 /*Get current PORT date (16-bit word)*/
558 #if ANOMALY_05000311 || ANOMALY_05000323
559 #define GET_GPIO_P(name) \
560 unsigned short get_gpiop_ ## name(unsigned gpio) \
562 unsigned long flags; \
563 unsigned short ret; \
564 local_irq_save(flags); \
565 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
566 AWA_DUMMY_READ(name); \
567 local_irq_restore(flags); \
570 EXPORT_SYMBOL(get_gpiop_ ## name);
572 #define GET_GPIO_P(name) \
573 unsigned short get_gpiop_ ## name(unsigned gpio) \
575 return (gpio_bankb[gpio_bank(gpio)]->name);\
577 EXPORT_SYMBOL(get_gpiop_ ## name);
591 /***********************************************************
593 * FUNCTIONS: Blackfin PM Setup API
596 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
604 * DESCRIPTION: Blackfin PM Driver API
607 *************************************************************
608 * MODIFICATION HISTORY :
609 **************************************************************/
610 int gpio_pm_wakeup_request(unsigned gpio
, unsigned char type
)
614 if ((check_gpio(gpio
) < 0) || !type
)
617 local_irq_save(flags
);
618 wakeup_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
619 wakeup_flags_map
[gpio
] = type
;
620 local_irq_restore(flags
);
624 EXPORT_SYMBOL(gpio_pm_wakeup_request
);
626 void gpio_pm_wakeup_free(unsigned gpio
)
630 if (check_gpio(gpio
) < 0)
633 local_irq_save(flags
);
635 wakeup_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
637 local_irq_restore(flags
);
639 EXPORT_SYMBOL(gpio_pm_wakeup_free
);
641 static int bfin_gpio_wakeup_type(unsigned gpio
, unsigned char type
)
643 port_setup(gpio
, GPIO_USAGE
);
644 set_gpio_dir(gpio
, 0);
645 set_gpio_inen(gpio
, 1);
647 if (type
& (PM_WAKE_RISING
| PM_WAKE_FALLING
))
648 set_gpio_edge(gpio
, 1);
650 set_gpio_edge(gpio
, 0);
652 if ((type
& (PM_WAKE_BOTH_EDGES
)) == (PM_WAKE_BOTH_EDGES
))
653 set_gpio_both(gpio
, 1);
655 set_gpio_both(gpio
, 0);
657 if ((type
& (PM_WAKE_FALLING
| PM_WAKE_LOW
)))
658 set_gpio_polar(gpio
, 1);
660 set_gpio_polar(gpio
, 0);
667 u32
bfin_pm_standby_setup(void)
669 u16 bank
, mask
, i
, gpio
;
671 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
672 mask
= wakeup_map
[gpio_bank(i
)];
675 gpio_bank_saved
[bank
].maskb
= gpio_bankb
[bank
]->maskb
;
676 gpio_bankb
[bank
]->maskb
= 0;
679 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
680 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
682 gpio_bank_saved
[bank
].inen
= gpio_bankb
[bank
]->inen
;
683 gpio_bank_saved
[bank
].polar
= gpio_bankb
[bank
]->polar
;
684 gpio_bank_saved
[bank
].dir
= gpio_bankb
[bank
]->dir
;
685 gpio_bank_saved
[bank
].edge
= gpio_bankb
[bank
]->edge
;
686 gpio_bank_saved
[bank
].both
= gpio_bankb
[bank
]->both
;
687 gpio_bank_saved
[bank
].reserved
=
688 reserved_gpio_map
[bank
];
693 if ((mask
& 1) && (wakeup_flags_map
[gpio
] !=
695 reserved_gpio_map
[gpio_bank(gpio
)] |=
697 bfin_gpio_wakeup_type(gpio
,
698 wakeup_flags_map
[gpio
]);
699 set_gpio_data(gpio
, 0); /*Clear*/
705 bfin_internal_set_wake(sic_iwr_irqs
[bank
], 1);
706 gpio_bankb
[bank
]->maskb_set
= wakeup_map
[gpio_bank(i
)];
710 AWA_DUMMY_READ(maskb_set
);
715 void bfin_pm_standby_restore(void)
719 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
720 mask
= wakeup_map
[gpio_bank(i
)];
724 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
725 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
727 gpio_bankb
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
728 gpio_bankb
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
729 gpio_bankb
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
730 gpio_bankb
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
731 gpio_bankb
[bank
]->both
= gpio_bank_saved
[bank
].both
;
733 reserved_gpio_map
[bank
] =
734 gpio_bank_saved
[bank
].reserved
;
735 bfin_internal_set_wake(sic_iwr_irqs
[bank
], 0);
738 gpio_bankb
[bank
]->maskb
= gpio_bank_saved
[bank
].maskb
;
740 AWA_DUMMY_READ(maskb
);
743 void bfin_gpio_pm_hibernate_suspend(void)
747 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
750 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
751 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
752 #if defined(BF527_FAMILY) || defined(BF518_FAMILY)
753 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
756 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
759 gpio_bank_saved
[bank
].data
= gpio_bankb
[bank
]->data
;
760 gpio_bank_saved
[bank
].inen
= gpio_bankb
[bank
]->inen
;
761 gpio_bank_saved
[bank
].polar
= gpio_bankb
[bank
]->polar
;
762 gpio_bank_saved
[bank
].dir
= gpio_bankb
[bank
]->dir
;
763 gpio_bank_saved
[bank
].edge
= gpio_bankb
[bank
]->edge
;
764 gpio_bank_saved
[bank
].both
= gpio_bankb
[bank
]->both
;
765 gpio_bank_saved
[bank
].maska
= gpio_bankb
[bank
]->maska
;
768 AWA_DUMMY_READ(maska
);
771 void bfin_gpio_pm_hibernate_restore(void)
775 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
778 #if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
779 #if defined(BF527_FAMILY) || defined(BF518_FAMILY)
780 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
783 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
785 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
787 gpio_bankb
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
788 gpio_bankb
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
789 gpio_bankb
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
790 gpio_bankb
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
791 gpio_bankb
[bank
]->both
= gpio_bank_saved
[bank
].both
;
793 gpio_bankb
[bank
]->data_set
= gpio_bank_saved
[bank
].data
794 | gpio_bank_saved
[bank
].dir
;
796 gpio_bankb
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
798 AWA_DUMMY_READ(maska
);
803 #else /* BF548_FAMILY */
806 u32
bfin_pm_standby_setup(void)
811 void bfin_pm_standby_restore(void)
816 void bfin_gpio_pm_hibernate_suspend(void)
820 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
823 gpio_bank_saved
[bank
].fer
= gpio_array
[bank
]->port_fer
;
824 gpio_bank_saved
[bank
].mux
= gpio_array
[bank
]->port_mux
;
825 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->port_data
;
826 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->port_data
;
827 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->port_inen
;
828 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->port_dir_set
;
832 void bfin_gpio_pm_hibernate_restore(void)
836 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
839 gpio_array
[bank
]->port_mux
= gpio_bank_saved
[bank
].mux
;
840 gpio_array
[bank
]->port_fer
= gpio_bank_saved
[bank
].fer
;
841 gpio_array
[bank
]->port_inen
= gpio_bank_saved
[bank
].inen
;
842 gpio_array
[bank
]->port_dir_set
= gpio_bank_saved
[bank
].dir
;
843 gpio_array
[bank
]->port_set
= gpio_bank_saved
[bank
].data
844 | gpio_bank_saved
[bank
].dir
;
849 unsigned short get_gpio_dir(unsigned gpio
)
851 return (0x01 & (gpio_array
[gpio_bank(gpio
)]->port_dir_clear
>> gpio_sub_n(gpio
)));
853 EXPORT_SYMBOL(get_gpio_dir
);
855 #endif /* BF548_FAMILY */
857 /***********************************************************
859 * FUNCTIONS: Blackfin Peripheral Resource Allocation
863 * per Peripheral Identifier
866 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
869 *************************************************************
870 * MODIFICATION HISTORY :
871 **************************************************************/
874 int peripheral_request(unsigned short per
, const char *label
)
877 unsigned short ident
= P_IDENT(per
);
880 * Don't cares are pins with only one dedicated function
883 if (per
& P_DONTCARE
)
886 if (!(per
& P_DEFINED
))
889 if (check_gpio(ident
) < 0)
892 local_irq_save(flags
);
894 if (unlikely(reserved_gpio_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
897 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
898 __func__
, ident
, get_label(ident
));
899 local_irq_restore(flags
);
903 if (unlikely(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
905 u16 funct
= get_portmux(ident
);
908 * Pin functions like AMC address strobes my
909 * be requested and used by several drivers
912 if (!((per
& P_MAYSHARE
) && (funct
== P_FUNCT2MUX(per
)))) {
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(flags
);
932 reserved_peri_map
[gpio_bank(ident
)] |= gpio_bit(ident
);
934 portmux_setup(ident
, P_FUNCT2MUX(per
));
935 port_setup(ident
, PERIPHERAL_USAGE
);
937 local_irq_restore(flags
);
938 set_label(ident
, label
);
942 EXPORT_SYMBOL(peripheral_request
);
945 int peripheral_request(unsigned short per
, const char *label
)
948 unsigned short ident
= P_IDENT(per
);
951 * Don't cares are pins with only one dedicated function
954 if (per
& P_DONTCARE
)
957 if (!(per
& P_DEFINED
))
960 local_irq_save(flags
);
962 if (!check_gpio(ident
)) {
964 if (unlikely(reserved_gpio_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
967 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
968 __func__
, ident
, get_label(ident
));
969 local_irq_restore(flags
);
975 if (unlikely(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
978 * Pin functions like AMC address strobes my
979 * be requested and used by several drivers
982 if (!(per
& P_MAYSHARE
)) {
985 * Allow that the identical pin function can
986 * be requested from the same driver twice
989 if (cmp_label(ident
, label
) == 0)
994 "%s: Peripheral %d function %d is already"
995 " reserved by %s !\n",
996 __func__
, ident
, P_FUNCT2MUX(per
),
998 local_irq_restore(flags
);
1005 portmux_setup(per
, P_FUNCT2MUX(per
));
1007 port_setup(ident
, PERIPHERAL_USAGE
);
1009 reserved_peri_map
[gpio_bank(ident
)] |= gpio_bit(ident
);
1010 local_irq_restore(flags
);
1011 set_label(ident
, label
);
1015 EXPORT_SYMBOL(peripheral_request
);
1018 int peripheral_request_list(const unsigned short per
[], const char *label
)
1023 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
1025 ret
= peripheral_request(per
[cnt
], label
);
1028 for ( ; cnt
> 0; cnt
--)
1029 peripheral_free(per
[cnt
- 1]);
1037 EXPORT_SYMBOL(peripheral_request_list
);
1039 void peripheral_free(unsigned short per
)
1041 unsigned long flags
;
1042 unsigned short ident
= P_IDENT(per
);
1044 if (per
& P_DONTCARE
)
1047 if (!(per
& P_DEFINED
))
1050 if (check_gpio(ident
) < 0)
1053 local_irq_save(flags
);
1055 if (unlikely(!(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
)))) {
1056 local_irq_restore(flags
);
1060 if (!(per
& P_MAYSHARE
))
1061 port_setup(ident
, GPIO_USAGE
);
1063 reserved_peri_map
[gpio_bank(ident
)] &= ~gpio_bit(ident
);
1065 set_label(ident
, "free");
1067 local_irq_restore(flags
);
1069 EXPORT_SYMBOL(peripheral_free
);
1071 void peripheral_free_list(const unsigned short per
[])
1074 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
1075 peripheral_free(per
[cnt
]);
1077 EXPORT_SYMBOL(peripheral_free_list
);
1079 /***********************************************************
1081 * FUNCTIONS: Blackfin GPIO Driver
1084 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1087 * DESCRIPTION: Blackfin GPIO Driver API
1090 *************************************************************
1091 * MODIFICATION HISTORY :
1092 **************************************************************/
1094 int gpio_request(unsigned gpio
, const char *label
)
1096 unsigned long flags
;
1098 if (check_gpio(gpio
) < 0)
1101 local_irq_save(flags
);
1104 * Allow that the identical GPIO can
1105 * be requested from the same driver twice
1106 * Do nothing and return -
1109 if (cmp_label(gpio
, label
) == 0) {
1110 local_irq_restore(flags
);
1114 if (unlikely(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1116 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
1117 gpio
, get_label(gpio
));
1118 local_irq_restore(flags
);
1121 if (unlikely(reserved_peri_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1124 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1125 gpio
, get_label(gpio
));
1126 local_irq_restore(flags
);
1130 reserved_gpio_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
1132 local_irq_restore(flags
);
1134 port_setup(gpio
, GPIO_USAGE
);
1135 set_label(gpio
, label
);
1139 EXPORT_SYMBOL(gpio_request
);
1141 void gpio_free(unsigned gpio
)
1143 unsigned long flags
;
1145 if (check_gpio(gpio
) < 0)
1148 local_irq_save(flags
);
1150 if (unlikely(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))) {
1153 local_irq_restore(flags
);
1157 reserved_gpio_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
1159 set_label(gpio
, "free");
1161 local_irq_restore(flags
);
1163 EXPORT_SYMBOL(gpio_free
);
1167 int gpio_direction_input(unsigned gpio
)
1169 unsigned long flags
;
1171 if (!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1176 local_irq_save(flags
);
1177 gpio_array
[gpio_bank(gpio
)]->port_dir_clear
= gpio_bit(gpio
);
1178 gpio_array
[gpio_bank(gpio
)]->port_inen
|= gpio_bit(gpio
);
1179 local_irq_restore(flags
);
1183 EXPORT_SYMBOL(gpio_direction_input
);
1185 int gpio_direction_output(unsigned gpio
, int value
)
1187 unsigned long flags
;
1189 if (!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1194 local_irq_save(flags
);
1195 gpio_array
[gpio_bank(gpio
)]->port_inen
&= ~gpio_bit(gpio
);
1196 gpio_set_value(gpio
, value
);
1197 gpio_array
[gpio_bank(gpio
)]->port_dir_set
= gpio_bit(gpio
);
1198 local_irq_restore(flags
);
1202 EXPORT_SYMBOL(gpio_direction_output
);
1204 void gpio_set_value(unsigned gpio
, int arg
)
1207 gpio_array
[gpio_bank(gpio
)]->port_set
= gpio_bit(gpio
);
1209 gpio_array
[gpio_bank(gpio
)]->port_clear
= gpio_bit(gpio
);
1211 EXPORT_SYMBOL(gpio_set_value
);
1213 int gpio_get_value(unsigned gpio
)
1215 return (1 & (gpio_array
[gpio_bank(gpio
)]->port_data
>> gpio_sub_n(gpio
)));
1217 EXPORT_SYMBOL(gpio_get_value
);
1219 void bfin_gpio_irq_prepare(unsigned gpio
)
1221 unsigned long flags
;
1223 port_setup(gpio
, GPIO_USAGE
);
1225 local_irq_save(flags
);
1226 gpio_array
[gpio_bank(gpio
)]->port_dir_clear
= gpio_bit(gpio
);
1227 gpio_array
[gpio_bank(gpio
)]->port_inen
|= gpio_bit(gpio
);
1228 local_irq_restore(flags
);
1233 int gpio_get_value(unsigned gpio
)
1235 unsigned long flags
;
1238 if (unlikely(get_gpio_edge(gpio
))) {
1239 local_irq_save(flags
);
1240 set_gpio_edge(gpio
, 0);
1241 ret
= get_gpio_data(gpio
);
1242 set_gpio_edge(gpio
, 1);
1243 local_irq_restore(flags
);
1247 return get_gpio_data(gpio
);
1249 EXPORT_SYMBOL(gpio_get_value
);
1252 int gpio_direction_input(unsigned gpio
)
1254 unsigned long flags
;
1256 if (!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1261 local_irq_save(flags
);
1262 gpio_bankb
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1263 gpio_bankb
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1264 AWA_DUMMY_READ(inen
);
1265 local_irq_restore(flags
);
1269 EXPORT_SYMBOL(gpio_direction_input
);
1271 int gpio_direction_output(unsigned gpio
, int value
)
1273 unsigned long flags
;
1275 if (!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1280 local_irq_save(flags
);
1281 gpio_bankb
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1284 gpio_bankb
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1286 gpio_bankb
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1288 gpio_bankb
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1289 AWA_DUMMY_READ(dir
);
1290 local_irq_restore(flags
);
1294 EXPORT_SYMBOL(gpio_direction_output
);
1296 /* If we are booting from SPI and our board lacks a strong enough pull up,
1297 * the core can reset and execute the bootrom faster than the resistor can
1298 * pull the signal logically high. To work around this (common) error in
1299 * board design, we explicitly set the pin back to GPIO mode, force /CS
1300 * high, and wait for the electrons to do their thing.
1302 * This function only makes sense to be called from reset code, but it
1303 * lives here as we need to force all the GPIO states w/out going through
1304 * BUG() checks and such.
1306 void bfin_gpio_reset_spi0_ssel1(void)
1308 u16 gpio
= P_IDENT(P_SPI0_SSEL1
);
1310 port_setup(gpio
, GPIO_USAGE
);
1311 gpio_bankb
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1312 AWA_DUMMY_READ(data_set
);
1316 void bfin_gpio_irq_prepare(unsigned gpio
)
1318 port_setup(gpio
, GPIO_USAGE
);
1321 #endif /*BF548_FAMILY */
1323 #if defined(CONFIG_PROC_FS)
1324 static int gpio_proc_read(char *buf
, char **start
, off_t offset
,
1325 int len
, int *unused_i
, void *unused_v
)
1329 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1330 if (!check_gpio(c
) && (reserved_gpio_map
[gpio_bank(c
)] & gpio_bit(c
)))
1331 len
= sprintf(buf
, "GPIO_%d: %s \t\tGPIO %s\n", c
,
1332 get_label(c
), get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1333 else if (reserved_peri_map
[gpio_bank(c
)] & gpio_bit(c
))
1334 len
= sprintf(buf
, "GPIO_%d: %s \t\tPeripheral\n", c
, get_label(c
));
1343 static __init
int gpio_register_proc(void)
1345 struct proc_dir_entry
*proc_gpio
;
1347 proc_gpio
= create_proc_entry("gpio", S_IRUGO
, NULL
);
1349 proc_gpio
->read_proc
= gpio_proc_read
;
1350 return proc_gpio
!= NULL
;
1352 __initcall(gpio_register_proc
);