2 * GPIO Abstraction Layer
4 * Copyright 2006-2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/proc_fs.h>
13 #include <asm/blackfin.h>
15 #include <asm/portmux.h>
16 #include <linux/irq.h>
18 #if ANOMALY_05000311 || ANOMALY_05000323
21 AWA_data_clear
= SYSCR
,
24 AWA_maska
= BFIN_UART_SCR
,
25 AWA_maska_clear
= BFIN_UART_SCR
,
26 AWA_maska_set
= BFIN_UART_SCR
,
27 AWA_maska_toggle
= BFIN_UART_SCR
,
28 AWA_maskb
= BFIN_UART_GCTL
,
29 AWA_maskb_clear
= BFIN_UART_GCTL
,
30 AWA_maskb_set
= BFIN_UART_GCTL
,
31 AWA_maskb_toggle
= BFIN_UART_GCTL
,
32 AWA_dir
= SPORT1_STAT
,
33 AWA_polar
= SPORT1_STAT
,
34 AWA_edge
= SPORT1_STAT
,
35 AWA_both
= SPORT1_STAT
,
37 AWA_inen
= TIMER_ENABLE
,
38 #elif ANOMALY_05000323
39 AWA_inen
= DMA1_1_CONFIG
,
42 /* Anomaly Workaround */
43 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
45 #define AWA_DUMMY_READ(...) do { } while (0)
48 static struct gpio_port_t
* const gpio_array
[] = {
49 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
50 (struct gpio_port_t
*) FIO_FLAG_D
,
51 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
52 (struct gpio_port_t
*) PORTFIO
,
53 (struct gpio_port_t
*) PORTGIO
,
54 (struct gpio_port_t
*) PORTHIO
,
55 #elif defined(BF561_FAMILY)
56 (struct gpio_port_t
*) FIO0_FLAG_D
,
57 (struct gpio_port_t
*) FIO1_FLAG_D
,
58 (struct gpio_port_t
*) FIO2_FLAG_D
,
59 #elif defined(CONFIG_BF54x)
60 (struct gpio_port_t
*)PORTA_FER
,
61 (struct gpio_port_t
*)PORTB_FER
,
62 (struct gpio_port_t
*)PORTC_FER
,
63 (struct gpio_port_t
*)PORTD_FER
,
64 (struct gpio_port_t
*)PORTE_FER
,
65 (struct gpio_port_t
*)PORTF_FER
,
66 (struct gpio_port_t
*)PORTG_FER
,
67 (struct gpio_port_t
*)PORTH_FER
,
68 (struct gpio_port_t
*)PORTI_FER
,
69 (struct gpio_port_t
*)PORTJ_FER
,
71 # error no gpio arrays defined
75 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
76 static unsigned short * const port_fer
[] = {
77 (unsigned short *) PORTF_FER
,
78 (unsigned short *) PORTG_FER
,
79 (unsigned short *) PORTH_FER
,
82 # if !defined(BF537_FAMILY)
83 static unsigned short * const port_mux
[] = {
84 (unsigned short *) PORTF_MUX
,
85 (unsigned short *) PORTG_MUX
,
86 (unsigned short *) PORTH_MUX
,
90 u8 pmux_offset
[][16] = {
91 # if defined(CONFIG_BF52x)
92 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
93 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
94 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
95 # elif defined(CONFIG_BF51x)
96 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
97 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
98 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
103 #elif defined(BF538_FAMILY)
104 static unsigned short * const port_fer
[] = {
105 (unsigned short *) PORTCIO_FER
,
106 (unsigned short *) PORTDIO_FER
,
107 (unsigned short *) PORTEIO_FER
,
111 static unsigned short reserved_gpio_map
[GPIO_BANK_NUM
];
112 static unsigned short reserved_peri_map
[gpio_bank(MAX_RESOURCES
)];
113 static unsigned short reserved_gpio_irq_map
[GPIO_BANK_NUM
];
115 #define RESOURCE_LABEL_SIZE 16
117 static struct str_ident
{
118 char name
[RESOURCE_LABEL_SIZE
];
119 } str_ident
[MAX_RESOURCES
];
121 #if defined(CONFIG_PM)
122 static struct gpio_port_s gpio_bank_saved
[GPIO_BANK_NUM
];
125 inline int check_gpio(unsigned gpio
)
127 #if defined(CONFIG_BF54x)
128 if (gpio
== GPIO_PB15
|| gpio
== GPIO_PC14
|| gpio
== GPIO_PC15
129 || gpio
== GPIO_PH14
|| gpio
== GPIO_PH15
130 || gpio
== GPIO_PJ14
|| gpio
== GPIO_PJ15
)
133 if (gpio
>= MAX_BLACKFIN_GPIOS
)
138 static void gpio_error(unsigned gpio
)
140 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
143 static void set_label(unsigned short ident
, const char *label
)
146 strncpy(str_ident
[ident
].name
, label
,
147 RESOURCE_LABEL_SIZE
);
148 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
152 static char *get_label(unsigned short ident
)
154 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
157 static int cmp_label(unsigned short ident
, const char *label
)
161 printk(KERN_ERR
"Please provide none-null label\n");
165 return strcmp(str_ident
[ident
].name
, label
);
170 static void port_setup(unsigned gpio
, unsigned short usage
)
172 #if defined(BF538_FAMILY)
174 * BF538/9 Port C,D and E are special.
175 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
176 * Regular PORT F GPIOs are handled here, CDE are exclusively
180 if (gpio
< MAX_BLACKFIN_GPIOS
|| gpio
>= MAX_RESOURCES
)
183 gpio
-= MAX_BLACKFIN_GPIOS
;
185 if (usage
== GPIO_USAGE
)
186 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
188 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
193 if (check_gpio(gpio
))
196 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
197 if (usage
== GPIO_USAGE
)
198 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
200 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
202 #elif defined(CONFIG_BF54x)
203 if (usage
== GPIO_USAGE
)
204 gpio_array
[gpio_bank(gpio
)]->port_fer
&= ~gpio_bit(gpio
);
206 gpio_array
[gpio_bank(gpio
)]->port_fer
|= gpio_bit(gpio
);
214 unsigned short offset
;
216 {.res
= P_PPI0_D13
, .offset
= 11},
217 {.res
= P_PPI0_D14
, .offset
= 11},
218 {.res
= P_PPI0_D15
, .offset
= 11},
219 {.res
= P_SPORT1_TFS
, .offset
= 11},
220 {.res
= P_SPORT1_TSCLK
, .offset
= 11},
221 {.res
= P_SPORT1_DTPRI
, .offset
= 11},
222 {.res
= P_PPI0_D10
, .offset
= 10},
223 {.res
= P_PPI0_D11
, .offset
= 10},
224 {.res
= P_PPI0_D12
, .offset
= 10},
225 {.res
= P_SPORT1_RSCLK
, .offset
= 10},
226 {.res
= P_SPORT1_RFS
, .offset
= 10},
227 {.res
= P_SPORT1_DRPRI
, .offset
= 10},
228 {.res
= P_PPI0_D8
, .offset
= 9},
229 {.res
= P_PPI0_D9
, .offset
= 9},
230 {.res
= P_SPORT1_DRSEC
, .offset
= 9},
231 {.res
= P_SPORT1_DTSEC
, .offset
= 9},
232 {.res
= P_TMR2
, .offset
= 8},
233 {.res
= P_PPI0_FS3
, .offset
= 8},
234 {.res
= P_TMR3
, .offset
= 7},
235 {.res
= P_SPI0_SSEL4
, .offset
= 7},
236 {.res
= P_TMR4
, .offset
= 6},
237 {.res
= P_SPI0_SSEL5
, .offset
= 6},
238 {.res
= P_TMR5
, .offset
= 5},
239 {.res
= P_SPI0_SSEL6
, .offset
= 5},
240 {.res
= P_UART1_RX
, .offset
= 4},
241 {.res
= P_UART1_TX
, .offset
= 4},
242 {.res
= P_TMR6
, .offset
= 4},
243 {.res
= P_TMR7
, .offset
= 4},
244 {.res
= P_UART0_RX
, .offset
= 3},
245 {.res
= P_UART0_TX
, .offset
= 3},
246 {.res
= P_DMAR0
, .offset
= 3},
247 {.res
= P_DMAR1
, .offset
= 3},
248 {.res
= P_SPORT0_DTSEC
, .offset
= 1},
249 {.res
= P_SPORT0_DRSEC
, .offset
= 1},
250 {.res
= P_CAN0_RX
, .offset
= 1},
251 {.res
= P_CAN0_TX
, .offset
= 1},
252 {.res
= P_SPI0_SSEL7
, .offset
= 1},
253 {.res
= P_SPORT0_TFS
, .offset
= 0},
254 {.res
= P_SPORT0_DTPRI
, .offset
= 0},
255 {.res
= P_SPI0_SSEL2
, .offset
= 0},
256 {.res
= P_SPI0_SSEL3
, .offset
= 0},
259 static void portmux_setup(unsigned short per
)
261 u16 y
, offset
, muxreg
;
262 u16 function
= P_FUNCT2MUX(per
);
264 for (y
= 0; y
< ARRAY_SIZE(port_mux_lut
); y
++) {
265 if (port_mux_lut
[y
].res
== per
) {
267 /* SET PORTMUX REG */
269 offset
= port_mux_lut
[y
].offset
;
270 muxreg
= bfin_read_PORT_MUX();
273 muxreg
&= ~(1 << offset
);
277 muxreg
|= (function
<< offset
);
278 bfin_write_PORT_MUX(muxreg
);
282 #elif defined(CONFIG_BF54x)
283 inline void portmux_setup(unsigned short per
)
286 u16 ident
= P_IDENT(per
);
287 u16 function
= P_FUNCT2MUX(per
);
289 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
291 pmux
&= ~(0x3 << (2 * gpio_sub_n(ident
)));
292 pmux
|= (function
& 0x3) << (2 * gpio_sub_n(ident
));
294 gpio_array
[gpio_bank(ident
)]->port_mux
= pmux
;
297 inline u16
get_portmux(unsigned short per
)
300 u16 ident
= P_IDENT(per
);
302 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
304 return (pmux
>> (2 * gpio_sub_n(ident
)) & 0x3);
306 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
307 inline void portmux_setup(unsigned short per
)
309 u16 pmux
, ident
= P_IDENT(per
), function
= P_FUNCT2MUX(per
);
310 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
312 pmux
= *port_mux
[gpio_bank(ident
)];
313 pmux
&= ~(3 << offset
);
314 pmux
|= (function
& 3) << offset
;
315 *port_mux
[gpio_bank(ident
)] = pmux
;
319 # define portmux_setup(...) do { } while (0)
323 /***********************************************************
325 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
328 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
331 * DESCRIPTION: These functions abstract direct register access
332 * to Blackfin processor General Purpose
335 * CAUTION: These functions do not belong to the GPIO Driver API
336 *************************************************************
337 * MODIFICATION HISTORY :
338 **************************************************************/
340 /* Set a specific bit */
342 #define SET_GPIO(name) \
343 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
345 unsigned long flags; \
346 local_irq_save_hw(flags); \
348 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
350 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
351 AWA_DUMMY_READ(name); \
352 local_irq_restore_hw(flags); \
354 EXPORT_SYMBOL(set_gpio_ ## name);
356 SET_GPIO(dir
) /* set_gpio_dir() */
357 SET_GPIO(inen
) /* set_gpio_inen() */
358 SET_GPIO(polar
) /* set_gpio_polar() */
359 SET_GPIO(edge
) /* set_gpio_edge() */
360 SET_GPIO(both
) /* set_gpio_both() */
363 #define SET_GPIO_SC(name) \
364 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
366 unsigned long flags; \
367 if (ANOMALY_05000311 || ANOMALY_05000323) \
368 local_irq_save_hw(flags); \
370 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
372 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
373 if (ANOMALY_05000311 || ANOMALY_05000323) { \
374 AWA_DUMMY_READ(name); \
375 local_irq_restore_hw(flags); \
378 EXPORT_SYMBOL(set_gpio_ ## name);
384 void set_gpio_toggle(unsigned gpio
)
387 if (ANOMALY_05000311
|| ANOMALY_05000323
)
388 local_irq_save_hw(flags
);
389 gpio_array
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
390 if (ANOMALY_05000311
|| ANOMALY_05000323
) {
391 AWA_DUMMY_READ(toggle
);
392 local_irq_restore_hw(flags
);
395 EXPORT_SYMBOL(set_gpio_toggle
);
398 /*Set current PORT date (16-bit word)*/
400 #define SET_GPIO_P(name) \
401 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
403 unsigned long flags; \
404 if (ANOMALY_05000311 || ANOMALY_05000323) \
405 local_irq_save_hw(flags); \
406 gpio_array[gpio_bank(gpio)]->name = arg; \
407 if (ANOMALY_05000311 || ANOMALY_05000323) { \
408 AWA_DUMMY_READ(name); \
409 local_irq_restore_hw(flags); \
412 EXPORT_SYMBOL(set_gpiop_ ## name);
423 /* Get a specific bit */
424 #define GET_GPIO(name) \
425 unsigned short get_gpio_ ## name(unsigned gpio) \
427 unsigned long flags; \
428 unsigned short ret; \
429 if (ANOMALY_05000311 || ANOMALY_05000323) \
430 local_irq_save_hw(flags); \
431 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
432 if (ANOMALY_05000311 || ANOMALY_05000323) { \
433 AWA_DUMMY_READ(name); \
434 local_irq_restore_hw(flags); \
438 EXPORT_SYMBOL(get_gpio_ ## name);
449 /*Get current PORT date (16-bit word)*/
451 #define GET_GPIO_P(name) \
452 unsigned short get_gpiop_ ## name(unsigned gpio) \
454 unsigned long flags; \
455 unsigned short ret; \
456 if (ANOMALY_05000311 || ANOMALY_05000323) \
457 local_irq_save_hw(flags); \
458 ret = (gpio_array[gpio_bank(gpio)]->name); \
459 if (ANOMALY_05000311 || ANOMALY_05000323) { \
460 AWA_DUMMY_READ(name); \
461 local_irq_restore_hw(flags); \
465 EXPORT_SYMBOL(get_gpiop_ ## name);
478 static unsigned short wakeup_map
[GPIO_BANK_NUM
];
480 static const unsigned int sic_iwr_irqs
[] = {
481 #if defined(BF533_FAMILY)
483 #elif defined(BF537_FAMILY)
484 IRQ_PROG_INTB
, IRQ_PORTG_INTB
, IRQ_MAC_TX
485 #elif defined(BF538_FAMILY)
487 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
488 IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
489 #elif defined(BF561_FAMILY)
490 IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
492 # error no SIC_IWR defined
496 /***********************************************************
498 * FUNCTIONS: Blackfin PM Setup API
501 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
509 * DESCRIPTION: Blackfin PM Driver API
512 *************************************************************
513 * MODIFICATION HISTORY :
514 **************************************************************/
515 int gpio_pm_wakeup_ctrl(unsigned gpio
, unsigned ctrl
)
519 if (check_gpio(gpio
) < 0)
522 local_irq_save_hw(flags
);
524 wakeup_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
526 wakeup_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
528 set_gpio_maskb(gpio
, ctrl
);
529 local_irq_restore_hw(flags
);
534 int bfin_pm_standby_ctrl(unsigned ctrl
)
538 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
539 mask
= wakeup_map
[gpio_bank(i
)];
543 bfin_internal_set_wake(sic_iwr_irqs
[bank
], ctrl
);
548 void bfin_gpio_pm_hibernate_suspend(void)
552 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
555 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
556 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
557 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
558 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
561 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
564 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
565 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
566 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
567 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
568 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
569 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
570 gpio_bank_saved
[bank
].maska
= gpio_array
[bank
]->maska
;
573 AWA_DUMMY_READ(maska
);
576 void bfin_gpio_pm_hibernate_restore(void)
580 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
583 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
584 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
585 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
588 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
590 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
592 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
593 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
594 & gpio_bank_saved
[bank
].dir
;
595 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
596 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
597 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
598 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
599 gpio_array
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
601 AWA_DUMMY_READ(maska
);
606 #else /* CONFIG_BF54x */
609 int bfin_pm_standby_ctrl(unsigned ctrl
)
614 void bfin_gpio_pm_hibernate_suspend(void)
618 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
621 gpio_bank_saved
[bank
].fer
= gpio_array
[bank
]->port_fer
;
622 gpio_bank_saved
[bank
].mux
= gpio_array
[bank
]->port_mux
;
623 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
624 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
625 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir_set
;
629 void bfin_gpio_pm_hibernate_restore(void)
633 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
636 gpio_array
[bank
]->port_mux
= gpio_bank_saved
[bank
].mux
;
637 gpio_array
[bank
]->port_fer
= gpio_bank_saved
[bank
].fer
;
638 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
639 gpio_array
[bank
]->dir_set
= gpio_bank_saved
[bank
].dir
;
640 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
641 | gpio_bank_saved
[bank
].dir
;
646 unsigned short get_gpio_dir(unsigned gpio
)
648 return (0x01 & (gpio_array
[gpio_bank(gpio
)]->dir_clear
>> gpio_sub_n(gpio
)));
650 EXPORT_SYMBOL(get_gpio_dir
);
652 #endif /* CONFIG_BF54x */
654 /***********************************************************
656 * FUNCTIONS: Blackfin Peripheral Resource Allocation
660 * per Peripheral Identifier
663 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
666 *************************************************************
667 * MODIFICATION HISTORY :
668 **************************************************************/
670 int peripheral_request(unsigned short per
, const char *label
)
673 unsigned short ident
= P_IDENT(per
);
676 * Don't cares are pins with only one dedicated function
679 if (per
& P_DONTCARE
)
682 if (!(per
& P_DEFINED
))
685 BUG_ON(ident
>= MAX_RESOURCES
);
687 local_irq_save_hw(flags
);
689 /* If a pin can be muxed as either GPIO or peripheral, make
690 * sure it is not already a GPIO pin when we request it.
692 if (unlikely(!check_gpio(ident
) &&
693 reserved_gpio_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
694 if (system_state
== SYSTEM_BOOTING
)
697 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
698 __func__
, ident
, get_label(ident
));
699 local_irq_restore_hw(flags
);
703 if (unlikely(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
706 * Pin functions like AMC address strobes my
707 * be requested and used by several drivers
711 if (!((per
& P_MAYSHARE
) && get_portmux(per
) == P_FUNCT2MUX(per
))) {
713 if (!(per
& P_MAYSHARE
)) {
716 * Allow that the identical pin function can
717 * be requested from the same driver twice
720 if (cmp_label(ident
, label
) == 0)
723 if (system_state
== SYSTEM_BOOTING
)
726 "%s: Peripheral %d function %d is already reserved by %s !\n",
727 __func__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
728 local_irq_restore_hw(flags
);
734 reserved_peri_map
[gpio_bank(ident
)] |= gpio_bit(ident
);
737 port_setup(ident
, PERIPHERAL_USAGE
);
739 local_irq_restore_hw(flags
);
740 set_label(ident
, label
);
744 EXPORT_SYMBOL(peripheral_request
);
746 int peripheral_request_list(const unsigned short per
[], const char *label
)
751 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
753 ret
= peripheral_request(per
[cnt
], label
);
756 for ( ; cnt
> 0; cnt
--)
757 peripheral_free(per
[cnt
- 1]);
765 EXPORT_SYMBOL(peripheral_request_list
);
767 void peripheral_free(unsigned short per
)
770 unsigned short ident
= P_IDENT(per
);
772 if (per
& P_DONTCARE
)
775 if (!(per
& P_DEFINED
))
778 local_irq_save_hw(flags
);
780 if (unlikely(!(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
)))) {
781 local_irq_restore_hw(flags
);
785 if (!(per
& P_MAYSHARE
))
786 port_setup(ident
, GPIO_USAGE
);
788 reserved_peri_map
[gpio_bank(ident
)] &= ~gpio_bit(ident
);
790 set_label(ident
, "free");
792 local_irq_restore_hw(flags
);
794 EXPORT_SYMBOL(peripheral_free
);
796 void peripheral_free_list(const unsigned short per
[])
799 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
800 peripheral_free(per
[cnt
]);
802 EXPORT_SYMBOL(peripheral_free_list
);
804 /***********************************************************
806 * FUNCTIONS: Blackfin GPIO Driver
809 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
812 * DESCRIPTION: Blackfin GPIO Driver API
815 *************************************************************
816 * MODIFICATION HISTORY :
817 **************************************************************/
819 int bfin_gpio_request(unsigned gpio
, const char *label
)
823 if (check_gpio(gpio
) < 0)
826 local_irq_save_hw(flags
);
829 * Allow that the identical GPIO can
830 * be requested from the same driver twice
831 * Do nothing and return -
834 if (cmp_label(gpio
, label
) == 0) {
835 local_irq_restore_hw(flags
);
839 if (unlikely(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
840 if (system_state
== SYSTEM_BOOTING
)
842 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
843 gpio
, get_label(gpio
));
844 local_irq_restore_hw(flags
);
847 if (unlikely(reserved_peri_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
848 if (system_state
== SYSTEM_BOOTING
)
851 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
852 gpio
, get_label(gpio
));
853 local_irq_restore_hw(flags
);
856 if (unlikely(reserved_gpio_irq_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
857 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved as gpio-irq!"
858 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio
);
861 else { /* Reset POLAR setting when acquiring a gpio for the first time */
862 set_gpio_polar(gpio
, 0);
866 reserved_gpio_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
867 set_label(gpio
, label
);
869 local_irq_restore_hw(flags
);
871 port_setup(gpio
, GPIO_USAGE
);
875 EXPORT_SYMBOL(bfin_gpio_request
);
877 void bfin_gpio_free(unsigned gpio
)
881 if (check_gpio(gpio
) < 0)
886 local_irq_save_hw(flags
);
888 if (unlikely(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))) {
889 if (system_state
== SYSTEM_BOOTING
)
892 local_irq_restore_hw(flags
);
896 reserved_gpio_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
898 set_label(gpio
, "free");
900 local_irq_restore_hw(flags
);
902 EXPORT_SYMBOL(bfin_gpio_free
);
904 #ifdef BFIN_SPECIAL_GPIO_BANKS
905 static unsigned short reserved_special_gpio_map
[gpio_bank(MAX_RESOURCES
)];
907 int bfin_special_gpio_request(unsigned gpio
, const char *label
)
911 local_irq_save_hw(flags
);
914 * Allow that the identical GPIO can
915 * be requested from the same driver twice
916 * Do nothing and return -
919 if (cmp_label(gpio
, label
) == 0) {
920 local_irq_restore_hw(flags
);
924 if (unlikely(reserved_special_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
925 local_irq_restore_hw(flags
);
926 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
927 gpio
, get_label(gpio
));
931 if (unlikely(reserved_peri_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
932 local_irq_restore_hw(flags
);
934 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
935 gpio
, get_label(gpio
));
940 reserved_special_gpio_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
941 reserved_peri_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
943 set_label(gpio
, label
);
944 local_irq_restore_hw(flags
);
945 port_setup(gpio
, GPIO_USAGE
);
949 EXPORT_SYMBOL(bfin_special_gpio_request
);
951 void bfin_special_gpio_free(unsigned gpio
)
957 local_irq_save_hw(flags
);
959 if (unlikely(!(reserved_special_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))) {
961 local_irq_restore_hw(flags
);
965 reserved_special_gpio_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
966 reserved_peri_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
967 set_label(gpio
, "free");
968 local_irq_restore_hw(flags
);
970 EXPORT_SYMBOL(bfin_special_gpio_free
);
974 int bfin_gpio_irq_request(unsigned gpio
, const char *label
)
978 if (check_gpio(gpio
) < 0)
981 local_irq_save_hw(flags
);
983 if (unlikely(reserved_peri_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
984 if (system_state
== SYSTEM_BOOTING
)
987 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
988 gpio
, get_label(gpio
));
989 local_irq_restore_hw(flags
);
992 if (unlikely(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))
993 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved by %s! "
994 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
995 gpio
, get_label(gpio
));
997 reserved_gpio_irq_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
998 set_label(gpio
, label
);
1000 local_irq_restore_hw(flags
);
1002 port_setup(gpio
, GPIO_USAGE
);
1007 void bfin_gpio_irq_free(unsigned gpio
)
1009 unsigned long flags
;
1011 if (check_gpio(gpio
) < 0)
1014 local_irq_save_hw(flags
);
1016 if (unlikely(!(reserved_gpio_irq_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))) {
1017 if (system_state
== SYSTEM_BOOTING
)
1020 local_irq_restore_hw(flags
);
1024 reserved_gpio_irq_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
1026 set_label(gpio
, "free");
1028 local_irq_restore_hw(flags
);
1031 static inline void __bfin_gpio_direction_input(unsigned gpio
)
1034 gpio_array
[gpio_bank(gpio
)]->dir_clear
= gpio_bit(gpio
);
1036 gpio_array
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1038 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1041 int bfin_gpio_direction_input(unsigned gpio
)
1043 unsigned long flags
;
1045 if (!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1050 local_irq_save_hw(flags
);
1051 __bfin_gpio_direction_input(gpio
);
1052 AWA_DUMMY_READ(inen
);
1053 local_irq_restore_hw(flags
);
1057 EXPORT_SYMBOL(bfin_gpio_direction_input
);
1059 void bfin_gpio_irq_prepare(unsigned gpio
)
1062 unsigned long flags
;
1065 port_setup(gpio
, GPIO_USAGE
);
1068 local_irq_save_hw(flags
);
1069 __bfin_gpio_direction_input(gpio
);
1070 local_irq_restore_hw(flags
);
1074 void bfin_gpio_set_value(unsigned gpio
, int arg
)
1077 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1079 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1081 EXPORT_SYMBOL(bfin_gpio_set_value
);
1083 int bfin_gpio_direction_output(unsigned gpio
, int value
)
1085 unsigned long flags
;
1087 if (!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1092 local_irq_save_hw(flags
);
1094 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1095 gpio_set_value(gpio
, value
);
1097 gpio_array
[gpio_bank(gpio
)]->dir_set
= gpio_bit(gpio
);
1099 gpio_array
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1102 AWA_DUMMY_READ(dir
);
1103 local_irq_restore_hw(flags
);
1107 EXPORT_SYMBOL(bfin_gpio_direction_output
);
1109 int bfin_gpio_get_value(unsigned gpio
)
1112 return (1 & (gpio_array
[gpio_bank(gpio
)]->data
>> gpio_sub_n(gpio
)));
1114 unsigned long flags
;
1116 if (unlikely(get_gpio_edge(gpio
))) {
1118 local_irq_save_hw(flags
);
1119 set_gpio_edge(gpio
, 0);
1120 ret
= get_gpio_data(gpio
);
1121 set_gpio_edge(gpio
, 1);
1122 local_irq_restore_hw(flags
);
1125 return get_gpio_data(gpio
);
1128 EXPORT_SYMBOL(bfin_gpio_get_value
);
1130 /* If we are booting from SPI and our board lacks a strong enough pull up,
1131 * the core can reset and execute the bootrom faster than the resistor can
1132 * pull the signal logically high. To work around this (common) error in
1133 * board design, we explicitly set the pin back to GPIO mode, force /CS
1134 * high, and wait for the electrons to do their thing.
1136 * This function only makes sense to be called from reset code, but it
1137 * lives here as we need to force all the GPIO states w/out going through
1138 * BUG() checks and such.
1140 void bfin_reset_boot_spi_cs(unsigned short pin
)
1142 unsigned short gpio
= P_IDENT(pin
);
1143 port_setup(gpio
, GPIO_USAGE
);
1144 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1145 AWA_DUMMY_READ(data_set
);
1149 #if defined(CONFIG_PROC_FS)
1150 static int gpio_proc_read(char *buf
, char **start
, off_t offset
,
1151 int len
, int *unused_i
, void *unused_v
)
1153 int c
, irq
, gpio
, outlen
= 0;
1155 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1156 irq
= reserved_gpio_irq_map
[gpio_bank(c
)] & gpio_bit(c
);
1157 gpio
= reserved_gpio_map
[gpio_bank(c
)] & gpio_bit(c
);
1158 if (!check_gpio(c
) && (gpio
|| irq
))
1159 len
= sprintf(buf
, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c
,
1160 get_label(c
), (gpio
&& irq
) ? " *" : "",
1161 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1162 else if (reserved_peri_map
[gpio_bank(c
)] & gpio_bit(c
))
1163 len
= sprintf(buf
, "GPIO_%d: \t%s \t\tPeripheral\n", c
, get_label(c
));
1172 static __init
int gpio_register_proc(void)
1174 struct proc_dir_entry
*proc_gpio
;
1176 proc_gpio
= create_proc_entry("gpio", S_IRUGO
, NULL
);
1178 proc_gpio
->read_proc
= gpio_proc_read
;
1179 return proc_gpio
!= NULL
;
1181 __initcall(gpio_register_proc
);
1184 #ifdef CONFIG_GPIOLIB
1185 static int bfin_gpiolib_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
1187 return bfin_gpio_direction_input(gpio
);
1190 static int bfin_gpiolib_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int level
)
1192 return bfin_gpio_direction_output(gpio
, level
);
1195 static int bfin_gpiolib_get_value(struct gpio_chip
*chip
, unsigned gpio
)
1197 return bfin_gpio_get_value(gpio
);
1200 static void bfin_gpiolib_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
1202 return bfin_gpio_set_value(gpio
, value
);
1205 static int bfin_gpiolib_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
1207 return bfin_gpio_request(gpio
, chip
->label
);
1210 static void bfin_gpiolib_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
1212 return bfin_gpio_free(gpio
);
1215 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip
*chip
, unsigned gpio
)
1217 return gpio
+ GPIO_IRQ_BASE
;
1220 static struct gpio_chip bfin_chip
= {
1221 .label
= "BFIN-GPIO",
1222 .direction_input
= bfin_gpiolib_direction_input
,
1223 .get
= bfin_gpiolib_get_value
,
1224 .direction_output
= bfin_gpiolib_direction_output
,
1225 .set
= bfin_gpiolib_set_value
,
1226 .request
= bfin_gpiolib_gpio_request
,
1227 .free
= bfin_gpiolib_gpio_free
,
1228 .to_irq
= bfin_gpiolib_gpio_to_irq
,
1230 .ngpio
= MAX_BLACKFIN_GPIOS
,
1233 static int __init
bfin_gpiolib_setup(void)
1235 return gpiochip_add(&bfin_chip
);
1237 arch_initcall(bfin_gpiolib_setup
);