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 */
105 static unsigned short reserved_gpio_map
[GPIO_BANK_NUM
];
106 static unsigned short reserved_peri_map
[gpio_bank(MAX_RESOURCES
)];
107 static unsigned short reserved_gpio_irq_map
[GPIO_BANK_NUM
];
109 #define RESOURCE_LABEL_SIZE 16
111 static struct str_ident
{
112 char name
[RESOURCE_LABEL_SIZE
];
113 } str_ident
[MAX_RESOURCES
];
115 #if defined(CONFIG_PM)
116 static struct gpio_port_s gpio_bank_saved
[GPIO_BANK_NUM
];
119 inline int check_gpio(unsigned gpio
)
121 #if defined(CONFIG_BF54x)
122 if (gpio
== GPIO_PB15
|| gpio
== GPIO_PC14
|| gpio
== GPIO_PC15
123 || gpio
== GPIO_PH14
|| gpio
== GPIO_PH15
124 || gpio
== GPIO_PJ14
|| gpio
== GPIO_PJ15
)
127 if (gpio
>= MAX_BLACKFIN_GPIOS
)
132 static void gpio_error(unsigned gpio
)
134 printk(KERN_ERR
"bfin-gpio: GPIO %d wasn't requested!\n", gpio
);
137 static void set_label(unsigned short ident
, const char *label
)
140 strncpy(str_ident
[ident
].name
, label
,
141 RESOURCE_LABEL_SIZE
);
142 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
146 static char *get_label(unsigned short ident
)
148 return (*str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN");
151 static int cmp_label(unsigned short ident
, const char *label
)
155 printk(KERN_ERR
"Please provide none-null label\n");
159 return strcmp(str_ident
[ident
].name
, label
);
164 static void port_setup(unsigned gpio
, unsigned short usage
)
166 if (check_gpio(gpio
))
169 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
170 if (usage
== GPIO_USAGE
)
171 *port_fer
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
173 *port_fer
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
175 #elif defined(CONFIG_BF54x)
176 if (usage
== GPIO_USAGE
)
177 gpio_array
[gpio_bank(gpio
)]->port_fer
&= ~gpio_bit(gpio
);
179 gpio_array
[gpio_bank(gpio
)]->port_fer
|= gpio_bit(gpio
);
187 unsigned short offset
;
189 {.res
= P_PPI0_D13
, .offset
= 11},
190 {.res
= P_PPI0_D14
, .offset
= 11},
191 {.res
= P_PPI0_D15
, .offset
= 11},
192 {.res
= P_SPORT1_TFS
, .offset
= 11},
193 {.res
= P_SPORT1_TSCLK
, .offset
= 11},
194 {.res
= P_SPORT1_DTPRI
, .offset
= 11},
195 {.res
= P_PPI0_D10
, .offset
= 10},
196 {.res
= P_PPI0_D11
, .offset
= 10},
197 {.res
= P_PPI0_D12
, .offset
= 10},
198 {.res
= P_SPORT1_RSCLK
, .offset
= 10},
199 {.res
= P_SPORT1_RFS
, .offset
= 10},
200 {.res
= P_SPORT1_DRPRI
, .offset
= 10},
201 {.res
= P_PPI0_D8
, .offset
= 9},
202 {.res
= P_PPI0_D9
, .offset
= 9},
203 {.res
= P_SPORT1_DRSEC
, .offset
= 9},
204 {.res
= P_SPORT1_DTSEC
, .offset
= 9},
205 {.res
= P_TMR2
, .offset
= 8},
206 {.res
= P_PPI0_FS3
, .offset
= 8},
207 {.res
= P_TMR3
, .offset
= 7},
208 {.res
= P_SPI0_SSEL4
, .offset
= 7},
209 {.res
= P_TMR4
, .offset
= 6},
210 {.res
= P_SPI0_SSEL5
, .offset
= 6},
211 {.res
= P_TMR5
, .offset
= 5},
212 {.res
= P_SPI0_SSEL6
, .offset
= 5},
213 {.res
= P_UART1_RX
, .offset
= 4},
214 {.res
= P_UART1_TX
, .offset
= 4},
215 {.res
= P_TMR6
, .offset
= 4},
216 {.res
= P_TMR7
, .offset
= 4},
217 {.res
= P_UART0_RX
, .offset
= 3},
218 {.res
= P_UART0_TX
, .offset
= 3},
219 {.res
= P_DMAR0
, .offset
= 3},
220 {.res
= P_DMAR1
, .offset
= 3},
221 {.res
= P_SPORT0_DTSEC
, .offset
= 1},
222 {.res
= P_SPORT0_DRSEC
, .offset
= 1},
223 {.res
= P_CAN0_RX
, .offset
= 1},
224 {.res
= P_CAN0_TX
, .offset
= 1},
225 {.res
= P_SPI0_SSEL7
, .offset
= 1},
226 {.res
= P_SPORT0_TFS
, .offset
= 0},
227 {.res
= P_SPORT0_DTPRI
, .offset
= 0},
228 {.res
= P_SPI0_SSEL2
, .offset
= 0},
229 {.res
= P_SPI0_SSEL3
, .offset
= 0},
232 static void portmux_setup(unsigned short per
)
234 u16 y
, offset
, muxreg
;
235 u16 function
= P_FUNCT2MUX(per
);
237 for (y
= 0; y
< ARRAY_SIZE(port_mux_lut
); y
++) {
238 if (port_mux_lut
[y
].res
== per
) {
240 /* SET PORTMUX REG */
242 offset
= port_mux_lut
[y
].offset
;
243 muxreg
= bfin_read_PORT_MUX();
246 muxreg
&= ~(1 << offset
);
250 muxreg
|= (function
<< offset
);
251 bfin_write_PORT_MUX(muxreg
);
255 #elif defined(CONFIG_BF54x)
256 inline void portmux_setup(unsigned short per
)
259 u16 ident
= P_IDENT(per
);
260 u16 function
= P_FUNCT2MUX(per
);
262 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
264 pmux
&= ~(0x3 << (2 * gpio_sub_n(ident
)));
265 pmux
|= (function
& 0x3) << (2 * gpio_sub_n(ident
));
267 gpio_array
[gpio_bank(ident
)]->port_mux
= pmux
;
270 inline u16
get_portmux(unsigned short per
)
273 u16 ident
= P_IDENT(per
);
275 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
277 return (pmux
>> (2 * gpio_sub_n(ident
)) & 0x3);
279 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
280 inline void portmux_setup(unsigned short per
)
282 u16 pmux
, ident
= P_IDENT(per
), function
= P_FUNCT2MUX(per
);
283 u8 offset
= pmux_offset
[gpio_bank(ident
)][gpio_sub_n(ident
)];
285 pmux
= *port_mux
[gpio_bank(ident
)];
286 pmux
&= ~(3 << offset
);
287 pmux
|= (function
& 3) << offset
;
288 *port_mux
[gpio_bank(ident
)] = pmux
;
292 # define portmux_setup(...) do { } while (0)
296 /***********************************************************
298 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
301 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
304 * DESCRIPTION: These functions abstract direct register access
305 * to Blackfin processor General Purpose
308 * CAUTION: These functions do not belong to the GPIO Driver API
309 *************************************************************
310 * MODIFICATION HISTORY :
311 **************************************************************/
313 /* Set a specific bit */
315 #define SET_GPIO(name) \
316 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
318 unsigned long flags; \
319 local_irq_save_hw(flags); \
321 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
323 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
324 AWA_DUMMY_READ(name); \
325 local_irq_restore_hw(flags); \
327 EXPORT_SYMBOL(set_gpio_ ## name);
329 SET_GPIO(dir
) /* set_gpio_dir() */
330 SET_GPIO(inen
) /* set_gpio_inen() */
331 SET_GPIO(polar
) /* set_gpio_polar() */
332 SET_GPIO(edge
) /* set_gpio_edge() */
333 SET_GPIO(both
) /* set_gpio_both() */
336 #define SET_GPIO_SC(name) \
337 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
339 unsigned long flags; \
340 if (ANOMALY_05000311 || ANOMALY_05000323) \
341 local_irq_save_hw(flags); \
343 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
345 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
346 if (ANOMALY_05000311 || ANOMALY_05000323) { \
347 AWA_DUMMY_READ(name); \
348 local_irq_restore_hw(flags); \
351 EXPORT_SYMBOL(set_gpio_ ## name);
357 void set_gpio_toggle(unsigned gpio
)
360 if (ANOMALY_05000311
|| ANOMALY_05000323
)
361 local_irq_save_hw(flags
);
362 gpio_array
[gpio_bank(gpio
)]->toggle
= gpio_bit(gpio
);
363 if (ANOMALY_05000311
|| ANOMALY_05000323
) {
364 AWA_DUMMY_READ(toggle
);
365 local_irq_restore_hw(flags
);
368 EXPORT_SYMBOL(set_gpio_toggle
);
371 /*Set current PORT date (16-bit word)*/
373 #define SET_GPIO_P(name) \
374 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
376 unsigned long flags; \
377 if (ANOMALY_05000311 || ANOMALY_05000323) \
378 local_irq_save_hw(flags); \
379 gpio_array[gpio_bank(gpio)]->name = arg; \
380 if (ANOMALY_05000311 || ANOMALY_05000323) { \
381 AWA_DUMMY_READ(name); \
382 local_irq_restore_hw(flags); \
385 EXPORT_SYMBOL(set_gpiop_ ## name);
396 /* Get a specific bit */
397 #define GET_GPIO(name) \
398 unsigned short get_gpio_ ## name(unsigned gpio) \
400 unsigned long flags; \
401 unsigned short ret; \
402 if (ANOMALY_05000311 || ANOMALY_05000323) \
403 local_irq_save_hw(flags); \
404 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
405 if (ANOMALY_05000311 || ANOMALY_05000323) { \
406 AWA_DUMMY_READ(name); \
407 local_irq_restore_hw(flags); \
411 EXPORT_SYMBOL(get_gpio_ ## name);
422 /*Get current PORT date (16-bit word)*/
424 #define GET_GPIO_P(name) \
425 unsigned short get_gpiop_ ## 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 = (gpio_array[gpio_bank(gpio)]->name); \
432 if (ANOMALY_05000311 || ANOMALY_05000323) { \
433 AWA_DUMMY_READ(name); \
434 local_irq_restore_hw(flags); \
438 EXPORT_SYMBOL(get_gpiop_ ## name);
452 static unsigned short wakeup_map
[GPIO_BANK_NUM
];
453 static unsigned char wakeup_flags_map
[MAX_BLACKFIN_GPIOS
];
455 static const unsigned int sic_iwr_irqs
[] = {
456 #if defined(BF533_FAMILY)
458 #elif defined(BF537_FAMILY)
459 IRQ_PROG_INTB
, IRQ_PORTG_INTB
, IRQ_MAC_TX
460 #elif defined(BF538_FAMILY)
462 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
463 IRQ_PORTF_INTB
, IRQ_PORTG_INTB
, IRQ_PORTH_INTB
464 #elif defined(BF561_FAMILY)
465 IRQ_PROG0_INTB
, IRQ_PROG1_INTB
, IRQ_PROG2_INTB
467 # error no SIC_IWR defined
471 /***********************************************************
473 * FUNCTIONS: Blackfin PM Setup API
476 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
484 * DESCRIPTION: Blackfin PM Driver API
487 *************************************************************
488 * MODIFICATION HISTORY :
489 **************************************************************/
490 int gpio_pm_wakeup_request(unsigned gpio
, unsigned char type
)
494 if ((check_gpio(gpio
) < 0) || !type
)
497 local_irq_save_hw(flags
);
498 wakeup_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
499 wakeup_flags_map
[gpio
] = type
;
500 local_irq_restore_hw(flags
);
504 EXPORT_SYMBOL(gpio_pm_wakeup_request
);
506 void gpio_pm_wakeup_free(unsigned gpio
)
510 if (check_gpio(gpio
) < 0)
513 local_irq_save_hw(flags
);
515 wakeup_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
517 local_irq_restore_hw(flags
);
519 EXPORT_SYMBOL(gpio_pm_wakeup_free
);
521 static int bfin_gpio_wakeup_type(unsigned gpio
, unsigned char type
)
523 port_setup(gpio
, GPIO_USAGE
);
524 set_gpio_dir(gpio
, 0);
525 set_gpio_inen(gpio
, 1);
527 if (type
& (PM_WAKE_RISING
| PM_WAKE_FALLING
))
528 set_gpio_edge(gpio
, 1);
530 set_gpio_edge(gpio
, 0);
532 if ((type
& (PM_WAKE_BOTH_EDGES
)) == (PM_WAKE_BOTH_EDGES
))
533 set_gpio_both(gpio
, 1);
535 set_gpio_both(gpio
, 0);
537 if ((type
& (PM_WAKE_FALLING
| PM_WAKE_LOW
)))
538 set_gpio_polar(gpio
, 1);
540 set_gpio_polar(gpio
, 0);
547 u32
bfin_pm_standby_setup(void)
549 u16 bank
, mask
, i
, gpio
;
551 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
552 mask
= wakeup_map
[gpio_bank(i
)];
555 gpio_bank_saved
[bank
].maskb
= gpio_array
[bank
]->maskb
;
556 gpio_array
[bank
]->maskb
= 0;
559 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
560 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
562 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
563 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
564 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
565 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
566 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
567 gpio_bank_saved
[bank
].reserved
=
568 reserved_gpio_map
[bank
];
573 if ((mask
& 1) && (wakeup_flags_map
[gpio
] !=
575 reserved_gpio_map
[gpio_bank(gpio
)] |=
577 bfin_gpio_wakeup_type(gpio
,
578 wakeup_flags_map
[gpio
]);
579 set_gpio_data(gpio
, 0); /*Clear*/
585 bfin_internal_set_wake(sic_iwr_irqs
[bank
], 1);
586 gpio_array
[bank
]->maskb_set
= wakeup_map
[gpio_bank(i
)];
590 AWA_DUMMY_READ(maskb_set
);
595 void bfin_pm_standby_restore(void)
599 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
600 mask
= wakeup_map
[gpio_bank(i
)];
604 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
605 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
607 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
608 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
609 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
610 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
611 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
613 reserved_gpio_map
[bank
] =
614 gpio_bank_saved
[bank
].reserved
;
615 bfin_internal_set_wake(sic_iwr_irqs
[bank
], 0);
618 gpio_array
[bank
]->maskb
= gpio_bank_saved
[bank
].maskb
;
620 AWA_DUMMY_READ(maskb
);
623 void bfin_gpio_pm_hibernate_suspend(void)
627 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
630 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
631 gpio_bank_saved
[bank
].fer
= *port_fer
[bank
];
632 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
633 gpio_bank_saved
[bank
].mux
= *port_mux
[bank
];
636 gpio_bank_saved
[bank
].mux
= bfin_read_PORT_MUX();
639 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
640 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
641 gpio_bank_saved
[bank
].polar
= gpio_array
[bank
]->polar
;
642 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir
;
643 gpio_bank_saved
[bank
].edge
= gpio_array
[bank
]->edge
;
644 gpio_bank_saved
[bank
].both
= gpio_array
[bank
]->both
;
645 gpio_bank_saved
[bank
].maska
= gpio_array
[bank
]->maska
;
648 AWA_DUMMY_READ(maska
);
651 void bfin_gpio_pm_hibernate_restore(void)
655 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
658 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
659 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
660 *port_mux
[bank
] = gpio_bank_saved
[bank
].mux
;
663 bfin_write_PORT_MUX(gpio_bank_saved
[bank
].mux
);
665 *port_fer
[bank
] = gpio_bank_saved
[bank
].fer
;
667 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
668 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
669 & gpio_bank_saved
[bank
].dir
;
670 gpio_array
[bank
]->dir
= gpio_bank_saved
[bank
].dir
;
671 gpio_array
[bank
]->polar
= gpio_bank_saved
[bank
].polar
;
672 gpio_array
[bank
]->edge
= gpio_bank_saved
[bank
].edge
;
673 gpio_array
[bank
]->both
= gpio_bank_saved
[bank
].both
;
674 gpio_array
[bank
]->maska
= gpio_bank_saved
[bank
].maska
;
676 AWA_DUMMY_READ(maska
);
681 #else /* CONFIG_BF54x */
684 u32
bfin_pm_standby_setup(void)
689 void bfin_pm_standby_restore(void)
694 void bfin_gpio_pm_hibernate_suspend(void)
698 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
701 gpio_bank_saved
[bank
].fer
= gpio_array
[bank
]->port_fer
;
702 gpio_bank_saved
[bank
].mux
= gpio_array
[bank
]->port_mux
;
703 gpio_bank_saved
[bank
].data
= gpio_array
[bank
]->data
;
704 gpio_bank_saved
[bank
].inen
= gpio_array
[bank
]->inen
;
705 gpio_bank_saved
[bank
].dir
= gpio_array
[bank
]->dir_set
;
709 void bfin_gpio_pm_hibernate_restore(void)
713 for (i
= 0; i
< MAX_BLACKFIN_GPIOS
; i
+= GPIO_BANKSIZE
) {
716 gpio_array
[bank
]->port_mux
= gpio_bank_saved
[bank
].mux
;
717 gpio_array
[bank
]->port_fer
= gpio_bank_saved
[bank
].fer
;
718 gpio_array
[bank
]->inen
= gpio_bank_saved
[bank
].inen
;
719 gpio_array
[bank
]->dir_set
= gpio_bank_saved
[bank
].dir
;
720 gpio_array
[bank
]->data_set
= gpio_bank_saved
[bank
].data
721 | gpio_bank_saved
[bank
].dir
;
726 unsigned short get_gpio_dir(unsigned gpio
)
728 return (0x01 & (gpio_array
[gpio_bank(gpio
)]->dir_clear
>> gpio_sub_n(gpio
)));
730 EXPORT_SYMBOL(get_gpio_dir
);
732 #endif /* CONFIG_BF54x */
734 /***********************************************************
736 * FUNCTIONS: Blackfin Peripheral Resource Allocation
740 * per Peripheral Identifier
743 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
746 *************************************************************
747 * MODIFICATION HISTORY :
748 **************************************************************/
750 int peripheral_request(unsigned short per
, const char *label
)
753 unsigned short ident
= P_IDENT(per
);
756 * Don't cares are pins with only one dedicated function
759 if (per
& P_DONTCARE
)
762 if (!(per
& P_DEFINED
))
765 local_irq_save_hw(flags
);
767 /* If a pin can be muxed as either GPIO or peripheral, make
768 * sure it is not already a GPIO pin when we request it.
770 if (unlikely(!check_gpio(ident
) &&
771 reserved_gpio_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
772 if (system_state
== SYSTEM_BOOTING
)
775 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
776 __func__
, ident
, get_label(ident
));
777 local_irq_restore_hw(flags
);
781 if (unlikely(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
))) {
784 * Pin functions like AMC address strobes my
785 * be requested and used by several drivers
789 if (!((per
& P_MAYSHARE
) && get_portmux(per
) == P_FUNCT2MUX(per
))) {
791 if (!(per
& P_MAYSHARE
)) {
794 * Allow that the identical pin function can
795 * be requested from the same driver twice
798 if (cmp_label(ident
, label
) == 0)
801 if (system_state
== SYSTEM_BOOTING
)
804 "%s: Peripheral %d function %d is already reserved by %s !\n",
805 __func__
, ident
, P_FUNCT2MUX(per
), get_label(ident
));
806 local_irq_restore_hw(flags
);
812 reserved_peri_map
[gpio_bank(ident
)] |= gpio_bit(ident
);
815 port_setup(ident
, PERIPHERAL_USAGE
);
817 local_irq_restore_hw(flags
);
818 set_label(ident
, label
);
822 EXPORT_SYMBOL(peripheral_request
);
824 int peripheral_request_list(const unsigned short per
[], const char *label
)
829 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
831 ret
= peripheral_request(per
[cnt
], label
);
834 for ( ; cnt
> 0; cnt
--)
835 peripheral_free(per
[cnt
- 1]);
843 EXPORT_SYMBOL(peripheral_request_list
);
845 void peripheral_free(unsigned short per
)
848 unsigned short ident
= P_IDENT(per
);
850 if (per
& P_DONTCARE
)
853 if (!(per
& P_DEFINED
))
856 local_irq_save_hw(flags
);
858 if (unlikely(!(reserved_peri_map
[gpio_bank(ident
)] & gpio_bit(ident
)))) {
859 local_irq_restore_hw(flags
);
863 if (!(per
& P_MAYSHARE
))
864 port_setup(ident
, GPIO_USAGE
);
866 reserved_peri_map
[gpio_bank(ident
)] &= ~gpio_bit(ident
);
868 set_label(ident
, "free");
870 local_irq_restore_hw(flags
);
872 EXPORT_SYMBOL(peripheral_free
);
874 void peripheral_free_list(const unsigned short per
[])
877 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
878 peripheral_free(per
[cnt
]);
880 EXPORT_SYMBOL(peripheral_free_list
);
882 /***********************************************************
884 * FUNCTIONS: Blackfin GPIO Driver
887 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
890 * DESCRIPTION: Blackfin GPIO Driver API
893 *************************************************************
894 * MODIFICATION HISTORY :
895 **************************************************************/
897 int bfin_gpio_request(unsigned gpio
, const char *label
)
901 if (check_gpio(gpio
) < 0)
904 local_irq_save_hw(flags
);
907 * Allow that the identical GPIO can
908 * be requested from the same driver twice
909 * Do nothing and return -
912 if (cmp_label(gpio
, label
) == 0) {
913 local_irq_restore_hw(flags
);
917 if (unlikely(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
918 if (system_state
== SYSTEM_BOOTING
)
920 printk(KERN_ERR
"bfin-gpio: GPIO %d is already reserved by %s !\n",
921 gpio
, get_label(gpio
));
922 local_irq_restore_hw(flags
);
925 if (unlikely(reserved_peri_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
926 if (system_state
== SYSTEM_BOOTING
)
929 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
930 gpio
, get_label(gpio
));
931 local_irq_restore_hw(flags
);
934 if (unlikely(reserved_gpio_irq_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
935 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved as gpio-irq!"
936 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio
);
939 else { /* Reset POLAR setting when acquiring a gpio for the first time */
940 set_gpio_polar(gpio
, 0);
944 reserved_gpio_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
945 set_label(gpio
, label
);
947 local_irq_restore_hw(flags
);
949 port_setup(gpio
, GPIO_USAGE
);
953 EXPORT_SYMBOL(bfin_gpio_request
);
955 void bfin_gpio_free(unsigned gpio
)
959 if (check_gpio(gpio
) < 0)
964 local_irq_save_hw(flags
);
966 if (unlikely(!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))) {
967 if (system_state
== SYSTEM_BOOTING
)
970 local_irq_restore_hw(flags
);
974 reserved_gpio_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
976 set_label(gpio
, "free");
978 local_irq_restore_hw(flags
);
980 EXPORT_SYMBOL(bfin_gpio_free
);
982 int bfin_gpio_irq_request(unsigned gpio
, const char *label
)
986 if (check_gpio(gpio
) < 0)
989 local_irq_save_hw(flags
);
991 if (unlikely(reserved_peri_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
992 if (system_state
== SYSTEM_BOOTING
)
995 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
996 gpio
, get_label(gpio
));
997 local_irq_restore_hw(flags
);
1000 if (unlikely(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))
1001 printk(KERN_NOTICE
"bfin-gpio: GPIO %d is already reserved by %s! "
1002 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1003 gpio
, get_label(gpio
));
1005 reserved_gpio_irq_map
[gpio_bank(gpio
)] |= gpio_bit(gpio
);
1006 set_label(gpio
, label
);
1008 local_irq_restore_hw(flags
);
1010 port_setup(gpio
, GPIO_USAGE
);
1015 void bfin_gpio_irq_free(unsigned gpio
)
1017 unsigned long flags
;
1019 if (check_gpio(gpio
) < 0)
1022 local_irq_save_hw(flags
);
1024 if (unlikely(!(reserved_gpio_irq_map
[gpio_bank(gpio
)] & gpio_bit(gpio
)))) {
1025 if (system_state
== SYSTEM_BOOTING
)
1028 local_irq_restore_hw(flags
);
1032 reserved_gpio_irq_map
[gpio_bank(gpio
)] &= ~gpio_bit(gpio
);
1034 set_label(gpio
, "free");
1036 local_irq_restore_hw(flags
);
1039 static inline void __bfin_gpio_direction_input(unsigned gpio
)
1042 gpio_array
[gpio_bank(gpio
)]->dir_clear
= gpio_bit(gpio
);
1044 gpio_array
[gpio_bank(gpio
)]->dir
&= ~gpio_bit(gpio
);
1046 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
1049 int bfin_gpio_direction_input(unsigned gpio
)
1051 unsigned long flags
;
1053 if (!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1058 local_irq_save_hw(flags
);
1059 __bfin_gpio_direction_input(gpio
);
1060 AWA_DUMMY_READ(inen
);
1061 local_irq_restore_hw(flags
);
1065 EXPORT_SYMBOL(bfin_gpio_direction_input
);
1067 void bfin_gpio_irq_prepare(unsigned gpio
)
1070 unsigned long flags
;
1073 port_setup(gpio
, GPIO_USAGE
);
1076 local_irq_save_hw(flags
);
1077 __bfin_gpio_direction_input(gpio
);
1078 local_irq_restore_hw(flags
);
1082 void bfin_gpio_set_value(unsigned gpio
, int arg
)
1085 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1087 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
1089 EXPORT_SYMBOL(bfin_gpio_set_value
);
1091 int bfin_gpio_direction_output(unsigned gpio
, int value
)
1093 unsigned long flags
;
1095 if (!(reserved_gpio_map
[gpio_bank(gpio
)] & gpio_bit(gpio
))) {
1100 local_irq_save_hw(flags
);
1102 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
1103 gpio_set_value(gpio
, value
);
1105 gpio_array
[gpio_bank(gpio
)]->dir_set
= gpio_bit(gpio
);
1107 gpio_array
[gpio_bank(gpio
)]->dir
|= gpio_bit(gpio
);
1110 AWA_DUMMY_READ(dir
);
1111 local_irq_restore_hw(flags
);
1115 EXPORT_SYMBOL(bfin_gpio_direction_output
);
1117 int bfin_gpio_get_value(unsigned gpio
)
1120 return (1 & (gpio_array
[gpio_bank(gpio
)]->data
>> gpio_sub_n(gpio
)));
1122 unsigned long flags
;
1124 if (unlikely(get_gpio_edge(gpio
))) {
1126 local_irq_save_hw(flags
);
1127 set_gpio_edge(gpio
, 0);
1128 ret
= get_gpio_data(gpio
);
1129 set_gpio_edge(gpio
, 1);
1130 local_irq_restore_hw(flags
);
1133 return get_gpio_data(gpio
);
1136 EXPORT_SYMBOL(bfin_gpio_get_value
);
1138 /* If we are booting from SPI and our board lacks a strong enough pull up,
1139 * the core can reset and execute the bootrom faster than the resistor can
1140 * pull the signal logically high. To work around this (common) error in
1141 * board design, we explicitly set the pin back to GPIO mode, force /CS
1142 * high, and wait for the electrons to do their thing.
1144 * This function only makes sense to be called from reset code, but it
1145 * lives here as we need to force all the GPIO states w/out going through
1146 * BUG() checks and such.
1148 void bfin_reset_boot_spi_cs(unsigned short pin
)
1150 unsigned short gpio
= P_IDENT(pin
);
1151 port_setup(gpio
, GPIO_USAGE
);
1152 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
1153 AWA_DUMMY_READ(data_set
);
1157 #if defined(CONFIG_PROC_FS)
1158 static int gpio_proc_read(char *buf
, char **start
, off_t offset
,
1159 int len
, int *unused_i
, void *unused_v
)
1161 int c
, irq
, gpio
, outlen
= 0;
1163 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
1164 irq
= reserved_gpio_irq_map
[gpio_bank(c
)] & gpio_bit(c
);
1165 gpio
= reserved_gpio_map
[gpio_bank(c
)] & gpio_bit(c
);
1166 if (!check_gpio(c
) && (gpio
|| irq
))
1167 len
= sprintf(buf
, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c
,
1168 get_label(c
), (gpio
&& irq
) ? " *" : "",
1169 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
1170 else if (reserved_peri_map
[gpio_bank(c
)] & gpio_bit(c
))
1171 len
= sprintf(buf
, "GPIO_%d: \t%s \t\tPeripheral\n", c
, get_label(c
));
1180 static __init
int gpio_register_proc(void)
1182 struct proc_dir_entry
*proc_gpio
;
1184 proc_gpio
= create_proc_entry("gpio", S_IRUGO
, NULL
);
1186 proc_gpio
->read_proc
= gpio_proc_read
;
1187 return proc_gpio
!= NULL
;
1189 __initcall(gpio_register_proc
);
1192 #ifdef CONFIG_GPIOLIB
1193 int bfin_gpiolib_direction_input(struct gpio_chip
*chip
, unsigned gpio
)
1195 return bfin_gpio_direction_input(gpio
);
1198 int bfin_gpiolib_direction_output(struct gpio_chip
*chip
, unsigned gpio
, int level
)
1200 return bfin_gpio_direction_output(gpio
, level
);
1203 int bfin_gpiolib_get_value(struct gpio_chip
*chip
, unsigned gpio
)
1205 return bfin_gpio_get_value(gpio
);
1208 void bfin_gpiolib_set_value(struct gpio_chip
*chip
, unsigned gpio
, int value
)
1210 return bfin_gpio_set_value(gpio
, value
);
1213 int bfin_gpiolib_gpio_request(struct gpio_chip
*chip
, unsigned gpio
)
1215 return bfin_gpio_request(gpio
, chip
->label
);
1218 void bfin_gpiolib_gpio_free(struct gpio_chip
*chip
, unsigned gpio
)
1220 return bfin_gpio_free(gpio
);
1223 static struct gpio_chip bfin_chip
= {
1224 .label
= "Blackfin-GPIOlib",
1225 .direction_input
= bfin_gpiolib_direction_input
,
1226 .get
= bfin_gpiolib_get_value
,
1227 .direction_output
= bfin_gpiolib_direction_output
,
1228 .set
= bfin_gpiolib_set_value
,
1229 .request
= bfin_gpiolib_gpio_request
,
1230 .free
= bfin_gpiolib_gpio_free
,
1232 .ngpio
= MAX_BLACKFIN_GPIOS
,
1235 static int __init
bfin_gpiolib_setup(void)
1237 return gpiochip_add(&bfin_chip
);
1239 arch_initcall(bfin_gpiolib_setup
);