x86: xen: size struct xen_spinlock to always fit in arch_spinlock_t
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / blackfin / kernel / bfin_gpio.c
blob02796b88443de4ebd2aef9e089f0893723607695
1 /*
2 * GPIO Abstraction Layer
4 * Copyright 2006-2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later
7 */
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <asm/blackfin.h>
15 #include <asm/gpio.h>
16 #include <asm/portmux.h>
17 #include <linux/irq.h>
18 #include <asm/irq_handler.h>
20 #if ANOMALY_05000311 || ANOMALY_05000323
21 enum {
22 AWA_data = SYSCR,
23 AWA_data_clear = SYSCR,
24 AWA_data_set = SYSCR,
25 AWA_toggle = SYSCR,
26 AWA_maska = BFIN_UART_SCR,
27 AWA_maska_clear = BFIN_UART_SCR,
28 AWA_maska_set = BFIN_UART_SCR,
29 AWA_maska_toggle = BFIN_UART_SCR,
30 AWA_maskb = BFIN_UART_GCTL,
31 AWA_maskb_clear = BFIN_UART_GCTL,
32 AWA_maskb_set = BFIN_UART_GCTL,
33 AWA_maskb_toggle = BFIN_UART_GCTL,
34 AWA_dir = SPORT1_STAT,
35 AWA_polar = SPORT1_STAT,
36 AWA_edge = SPORT1_STAT,
37 AWA_both = SPORT1_STAT,
38 #if ANOMALY_05000311
39 AWA_inen = TIMER_ENABLE,
40 #elif ANOMALY_05000323
41 AWA_inen = DMA1_1_CONFIG,
42 #endif
44 /* Anomaly Workaround */
45 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
46 #else
47 #define AWA_DUMMY_READ(...) do { } while (0)
48 #endif
50 static struct gpio_port_t * const gpio_array[] = {
51 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
52 (struct gpio_port_t *) FIO_FLAG_D,
53 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
54 (struct gpio_port_t *) PORTFIO,
55 (struct gpio_port_t *) PORTGIO,
56 (struct gpio_port_t *) PORTHIO,
57 #elif defined(BF561_FAMILY)
58 (struct gpio_port_t *) FIO0_FLAG_D,
59 (struct gpio_port_t *) FIO1_FLAG_D,
60 (struct gpio_port_t *) FIO2_FLAG_D,
61 #elif defined(CONFIG_BF54x)
62 (struct gpio_port_t *)PORTA_FER,
63 (struct gpio_port_t *)PORTB_FER,
64 (struct gpio_port_t *)PORTC_FER,
65 (struct gpio_port_t *)PORTD_FER,
66 (struct gpio_port_t *)PORTE_FER,
67 (struct gpio_port_t *)PORTF_FER,
68 (struct gpio_port_t *)PORTG_FER,
69 (struct gpio_port_t *)PORTH_FER,
70 (struct gpio_port_t *)PORTI_FER,
71 (struct gpio_port_t *)PORTJ_FER,
72 #else
73 # error no gpio arrays defined
74 #endif
77 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
78 static unsigned short * const port_fer[] = {
79 (unsigned short *) PORTF_FER,
80 (unsigned short *) PORTG_FER,
81 (unsigned short *) PORTH_FER,
84 # if !defined(BF537_FAMILY)
85 static unsigned short * const port_mux[] = {
86 (unsigned short *) PORTF_MUX,
87 (unsigned short *) PORTG_MUX,
88 (unsigned short *) PORTH_MUX,
91 static const
92 u8 pmux_offset[][16] = {
93 # if defined(CONFIG_BF52x)
94 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
95 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
96 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
97 # elif defined(CONFIG_BF51x)
98 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
99 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
100 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
101 # endif
103 # endif
105 #elif defined(BF538_FAMILY)
106 static unsigned short * const port_fer[] = {
107 (unsigned short *) PORTCIO_FER,
108 (unsigned short *) PORTDIO_FER,
109 (unsigned short *) PORTEIO_FER,
111 #endif
113 #define RESOURCE_LABEL_SIZE 16
115 static struct str_ident {
116 char name[RESOURCE_LABEL_SIZE];
117 } str_ident[MAX_RESOURCES];
119 #if defined(CONFIG_PM)
120 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
121 # ifdef BF538_FAMILY
122 static unsigned short port_fer_saved[3];
123 # endif
124 #endif
126 static void gpio_error(unsigned gpio)
128 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
131 static void set_label(unsigned short ident, const char *label)
133 if (label) {
134 strncpy(str_ident[ident].name, label,
135 RESOURCE_LABEL_SIZE);
136 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
140 static char *get_label(unsigned short ident)
142 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
145 static int cmp_label(unsigned short ident, const char *label)
147 if (label == NULL) {
148 dump_stack();
149 printk(KERN_ERR "Please provide none-null label\n");
152 if (label)
153 return strcmp(str_ident[ident].name, label);
154 else
155 return -EINVAL;
158 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
159 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
160 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
161 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
162 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
164 DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
165 DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
166 DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
168 inline int check_gpio(unsigned gpio)
170 #if defined(CONFIG_BF54x)
171 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
172 || gpio == GPIO_PH14 || gpio == GPIO_PH15
173 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
174 return -EINVAL;
175 #endif
176 if (gpio >= MAX_BLACKFIN_GPIOS)
177 return -EINVAL;
178 return 0;
181 static void port_setup(unsigned gpio, unsigned short usage)
183 #if defined(BF538_FAMILY)
185 * BF538/9 Port C,D and E are special.
186 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
187 * Regular PORT F GPIOs are handled here, CDE are exclusively
188 * managed by GPIOLIB
191 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
192 return;
194 gpio -= MAX_BLACKFIN_GPIOS;
196 if (usage == GPIO_USAGE)
197 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
198 else
199 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
200 SSYNC();
201 return;
202 #endif
204 if (check_gpio(gpio))
205 return;
207 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
208 if (usage == GPIO_USAGE)
209 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
210 else
211 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
212 SSYNC();
213 #elif defined(CONFIG_BF54x)
214 if (usage == GPIO_USAGE)
215 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
216 else
217 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
218 SSYNC();
219 #endif
222 #ifdef BF537_FAMILY
223 static const s8 port_mux[] = {
224 [GPIO_PF0] = 3,
225 [GPIO_PF1] = 3,
226 [GPIO_PF2] = 4,
227 [GPIO_PF3] = 4,
228 [GPIO_PF4] = 5,
229 [GPIO_PF5] = 6,
230 [GPIO_PF6] = 7,
231 [GPIO_PF7] = 8,
232 [GPIO_PF8 ... GPIO_PF15] = -1,
233 [GPIO_PG0 ... GPIO_PG7] = -1,
234 [GPIO_PG8] = 9,
235 [GPIO_PG9] = 9,
236 [GPIO_PG10] = 10,
237 [GPIO_PG11] = 10,
238 [GPIO_PG12] = 10,
239 [GPIO_PG13] = 11,
240 [GPIO_PG14] = 11,
241 [GPIO_PG15] = 11,
242 [GPIO_PH0 ... GPIO_PH15] = -1,
243 [PORT_PJ0 ... PORT_PJ3] = -1,
244 [PORT_PJ4] = 1,
245 [PORT_PJ5] = 1,
246 [PORT_PJ6 ... PORT_PJ9] = -1,
247 [PORT_PJ10] = 0,
248 [PORT_PJ11] = 0,
251 static int portmux_group_check(unsigned short per)
253 u16 ident = P_IDENT(per);
254 u16 function = P_FUNCT2MUX(per);
255 s8 offset = port_mux[ident];
256 u16 m, pmux, pfunc;
258 if (offset < 0)
259 return 0;
261 pmux = bfin_read_PORT_MUX();
262 for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
263 if (m == ident)
264 continue;
265 if (port_mux[m] != offset)
266 continue;
267 if (!is_reserved(peri, m, 1))
268 continue;
270 if (offset == 1)
271 pfunc = (pmux >> offset) & 3;
272 else
273 pfunc = (pmux >> offset) & 1;
274 if (pfunc != function) {
275 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
276 ident, function, m, pfunc);
277 return -EINVAL;
281 return 0;
284 static void portmux_setup(unsigned short per)
286 u16 ident = P_IDENT(per);
287 u16 function = P_FUNCT2MUX(per);
288 s8 offset = port_mux[ident];
289 u16 pmux;
291 if (offset == -1)
292 return;
294 pmux = bfin_read_PORT_MUX();
295 if (offset != 1)
296 pmux &= ~(1 << offset);
297 else
298 pmux &= ~(3 << 1);
299 pmux |= (function << offset);
300 bfin_write_PORT_MUX(pmux);
302 #elif defined(CONFIG_BF54x)
303 inline void portmux_setup(unsigned short per)
305 u16 ident = P_IDENT(per);
306 u16 function = P_FUNCT2MUX(per);
307 u32 pmux;
309 pmux = gpio_array[gpio_bank(ident)]->port_mux;
311 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
312 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
314 gpio_array[gpio_bank(ident)]->port_mux = pmux;
317 inline u16 get_portmux(unsigned short per)
319 u16 ident = P_IDENT(per);
320 u32 pmux = gpio_array[gpio_bank(ident)]->port_mux;
321 return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
323 static int portmux_group_check(unsigned short per)
325 return 0;
327 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
328 static int portmux_group_check(unsigned short per)
330 u16 ident = P_IDENT(per);
331 u16 function = P_FUNCT2MUX(per);
332 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
333 u16 pin, gpiopin, pfunc;
335 for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
336 if (offset != pmux_offset[gpio_bank(ident)][pin])
337 continue;
339 gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
340 if (gpiopin == ident)
341 continue;
342 if (!is_reserved(peri, gpiopin, 1))
343 continue;
345 pfunc = *port_mux[gpio_bank(ident)];
346 pfunc = (pfunc >> offset) & 3;
347 if (pfunc != function) {
348 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
349 ident, function, gpiopin, pfunc);
350 return -EINVAL;
354 return 0;
357 inline void portmux_setup(unsigned short per)
359 u16 ident = P_IDENT(per);
360 u16 function = P_FUNCT2MUX(per);
361 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
362 u16 pmux;
364 pmux = *port_mux[gpio_bank(ident)];
365 if (((pmux >> offset) & 3) == function)
366 return;
367 pmux &= ~(3 << offset);
368 pmux |= (function & 3) << offset;
369 *port_mux[gpio_bank(ident)] = pmux;
370 SSYNC();
372 #else
373 # define portmux_setup(...) do { } while (0)
374 static int portmux_group_check(unsigned short per)
376 return 0;
378 #endif
380 #ifndef CONFIG_BF54x
381 /***********************************************************
383 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
385 * INPUTS/OUTPUTS:
386 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
389 * DESCRIPTION: These functions abstract direct register access
390 * to Blackfin processor General Purpose
391 * Ports Regsiters
393 * CAUTION: These functions do not belong to the GPIO Driver API
394 *************************************************************
395 * MODIFICATION HISTORY :
396 **************************************************************/
398 /* Set a specific bit */
400 #define SET_GPIO(name) \
401 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
403 unsigned long flags; \
404 flags = hard_local_irq_save(); \
405 if (arg) \
406 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
407 else \
408 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
409 AWA_DUMMY_READ(name); \
410 hard_local_irq_restore(flags); \
412 EXPORT_SYMBOL(set_gpio_ ## name);
414 SET_GPIO(dir) /* set_gpio_dir() */
415 SET_GPIO(inen) /* set_gpio_inen() */
416 SET_GPIO(polar) /* set_gpio_polar() */
417 SET_GPIO(edge) /* set_gpio_edge() */
418 SET_GPIO(both) /* set_gpio_both() */
421 #define SET_GPIO_SC(name) \
422 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
424 unsigned long flags; \
425 if (ANOMALY_05000311 || ANOMALY_05000323) \
426 flags = hard_local_irq_save(); \
427 if (arg) \
428 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
429 else \
430 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
431 if (ANOMALY_05000311 || ANOMALY_05000323) { \
432 AWA_DUMMY_READ(name); \
433 hard_local_irq_restore(flags); \
436 EXPORT_SYMBOL(set_gpio_ ## name);
438 SET_GPIO_SC(maska)
439 SET_GPIO_SC(maskb)
440 SET_GPIO_SC(data)
442 void set_gpio_toggle(unsigned gpio)
444 unsigned long flags;
445 if (ANOMALY_05000311 || ANOMALY_05000323)
446 flags = hard_local_irq_save();
447 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
448 if (ANOMALY_05000311 || ANOMALY_05000323) {
449 AWA_DUMMY_READ(toggle);
450 hard_local_irq_restore(flags);
453 EXPORT_SYMBOL(set_gpio_toggle);
456 /*Set current PORT date (16-bit word)*/
458 #define SET_GPIO_P(name) \
459 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
461 unsigned long flags; \
462 if (ANOMALY_05000311 || ANOMALY_05000323) \
463 flags = hard_local_irq_save(); \
464 gpio_array[gpio_bank(gpio)]->name = arg; \
465 if (ANOMALY_05000311 || ANOMALY_05000323) { \
466 AWA_DUMMY_READ(name); \
467 hard_local_irq_restore(flags); \
470 EXPORT_SYMBOL(set_gpiop_ ## name);
472 SET_GPIO_P(data)
473 SET_GPIO_P(dir)
474 SET_GPIO_P(inen)
475 SET_GPIO_P(polar)
476 SET_GPIO_P(edge)
477 SET_GPIO_P(both)
478 SET_GPIO_P(maska)
479 SET_GPIO_P(maskb)
481 /* Get a specific bit */
482 #define GET_GPIO(name) \
483 unsigned short get_gpio_ ## name(unsigned gpio) \
485 unsigned long flags; \
486 unsigned short ret; \
487 if (ANOMALY_05000311 || ANOMALY_05000323) \
488 flags = hard_local_irq_save(); \
489 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
490 if (ANOMALY_05000311 || ANOMALY_05000323) { \
491 AWA_DUMMY_READ(name); \
492 hard_local_irq_restore(flags); \
494 return ret; \
496 EXPORT_SYMBOL(get_gpio_ ## name);
498 GET_GPIO(data)
499 GET_GPIO(dir)
500 GET_GPIO(inen)
501 GET_GPIO(polar)
502 GET_GPIO(edge)
503 GET_GPIO(both)
504 GET_GPIO(maska)
505 GET_GPIO(maskb)
507 /*Get current PORT date (16-bit word)*/
509 #define GET_GPIO_P(name) \
510 unsigned short get_gpiop_ ## name(unsigned gpio) \
512 unsigned long flags; \
513 unsigned short ret; \
514 if (ANOMALY_05000311 || ANOMALY_05000323) \
515 flags = hard_local_irq_save(); \
516 ret = (gpio_array[gpio_bank(gpio)]->name); \
517 if (ANOMALY_05000311 || ANOMALY_05000323) { \
518 AWA_DUMMY_READ(name); \
519 hard_local_irq_restore(flags); \
521 return ret; \
523 EXPORT_SYMBOL(get_gpiop_ ## name);
525 GET_GPIO_P(data)
526 GET_GPIO_P(dir)
527 GET_GPIO_P(inen)
528 GET_GPIO_P(polar)
529 GET_GPIO_P(edge)
530 GET_GPIO_P(both)
531 GET_GPIO_P(maska)
532 GET_GPIO_P(maskb)
535 #ifdef CONFIG_PM
536 DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
538 static const unsigned int sic_iwr_irqs[] = {
539 #if defined(BF533_FAMILY)
540 IRQ_PROG_INTB
541 #elif defined(BF537_FAMILY)
542 IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
543 #elif defined(BF538_FAMILY)
544 IRQ_PORTF_INTB
545 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
546 IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
547 #elif defined(BF561_FAMILY)
548 IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
549 #else
550 # error no SIC_IWR defined
551 #endif
554 /***********************************************************
556 * FUNCTIONS: Blackfin PM Setup API
558 * INPUTS/OUTPUTS:
559 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
560 * type -
561 * PM_WAKE_RISING
562 * PM_WAKE_FALLING
563 * PM_WAKE_HIGH
564 * PM_WAKE_LOW
565 * PM_WAKE_BOTH_EDGES
567 * DESCRIPTION: Blackfin PM Driver API
569 * CAUTION:
570 *************************************************************
571 * MODIFICATION HISTORY :
572 **************************************************************/
573 int gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
575 unsigned long flags;
577 if (check_gpio(gpio) < 0)
578 return -EINVAL;
580 flags = hard_local_irq_save();
581 if (ctrl)
582 reserve(wakeup, gpio);
583 else
584 unreserve(wakeup, gpio);
586 set_gpio_maskb(gpio, ctrl);
587 hard_local_irq_restore(flags);
589 return 0;
592 int bfin_pm_standby_ctrl(unsigned ctrl)
594 u16 bank, mask, i;
596 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
597 mask = map_entry(wakeup, i);
598 bank = gpio_bank(i);
600 if (mask)
601 bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
603 return 0;
606 void bfin_gpio_pm_hibernate_suspend(void)
608 int i, bank;
610 #ifdef BF538_FAMILY
611 for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
612 port_fer_saved[i] = *port_fer[i];
613 #endif
615 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
616 bank = gpio_bank(i);
618 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
619 gpio_bank_saved[bank].fer = *port_fer[bank];
620 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
621 gpio_bank_saved[bank].mux = *port_mux[bank];
622 #else
623 if (bank == 0)
624 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
625 #endif
626 #endif
627 gpio_bank_saved[bank].data = gpio_array[bank]->data;
628 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
629 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
630 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
631 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
632 gpio_bank_saved[bank].both = gpio_array[bank]->both;
633 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
636 #ifdef BFIN_SPECIAL_GPIO_BANKS
637 bfin_special_gpio_pm_hibernate_suspend();
638 #endif
640 AWA_DUMMY_READ(maska);
643 void bfin_gpio_pm_hibernate_restore(void)
645 int i, bank;
647 #ifdef BF538_FAMILY
648 for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
649 *port_fer[i] = port_fer_saved[i];
650 #endif
652 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
653 bank = gpio_bank(i);
655 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
656 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
657 *port_mux[bank] = gpio_bank_saved[bank].mux;
658 #else
659 if (bank == 0)
660 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
661 #endif
662 *port_fer[bank] = gpio_bank_saved[bank].fer;
663 #endif
664 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
665 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
666 & gpio_bank_saved[bank].dir;
667 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
668 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
669 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
670 gpio_array[bank]->both = gpio_bank_saved[bank].both;
671 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
674 #ifdef BFIN_SPECIAL_GPIO_BANKS
675 bfin_special_gpio_pm_hibernate_restore();
676 #endif
678 AWA_DUMMY_READ(maska);
682 #endif
683 #else /* CONFIG_BF54x */
684 #ifdef CONFIG_PM
686 int bfin_pm_standby_ctrl(unsigned ctrl)
688 return 0;
691 void bfin_gpio_pm_hibernate_suspend(void)
693 int i, bank;
695 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
696 bank = gpio_bank(i);
698 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
699 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
700 gpio_bank_saved[bank].data = gpio_array[bank]->data;
701 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
702 gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
706 void bfin_gpio_pm_hibernate_restore(void)
708 int i, bank;
710 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
711 bank = gpio_bank(i);
713 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
714 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
715 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
716 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
717 & gpio_bank_saved[bank].dir;
718 gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
721 #endif
723 unsigned short get_gpio_dir(unsigned gpio)
725 return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
727 EXPORT_SYMBOL(get_gpio_dir);
729 #endif /* CONFIG_BF54x */
731 /***********************************************************
733 * FUNCTIONS: Blackfin Peripheral Resource Allocation
734 * and PortMux Setup
736 * INPUTS/OUTPUTS:
737 * per Peripheral Identifier
738 * label String
740 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
742 * CAUTION:
743 *************************************************************
744 * MODIFICATION HISTORY :
745 **************************************************************/
747 int peripheral_request(unsigned short per, const char *label)
749 unsigned long flags;
750 unsigned short ident = P_IDENT(per);
753 * Don't cares are pins with only one dedicated function
756 if (per & P_DONTCARE)
757 return 0;
759 if (!(per & P_DEFINED))
760 return -ENODEV;
762 BUG_ON(ident >= MAX_RESOURCES);
764 flags = hard_local_irq_save();
766 /* If a pin can be muxed as either GPIO or peripheral, make
767 * sure it is not already a GPIO pin when we request it.
769 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
770 if (system_state == SYSTEM_BOOTING)
771 dump_stack();
772 printk(KERN_ERR
773 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
774 __func__, ident, get_label(ident));
775 hard_local_irq_restore(flags);
776 return -EBUSY;
779 if (unlikely(is_reserved(peri, ident, 1))) {
782 * Pin functions like AMC address strobes my
783 * be requested and used by several drivers
786 #ifdef CONFIG_BF54x
787 if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
788 #else
789 if (!(per & P_MAYSHARE)) {
790 #endif
792 * Allow that the identical pin function can
793 * be requested from the same driver twice
796 if (cmp_label(ident, label) == 0)
797 goto anyway;
799 if (system_state == SYSTEM_BOOTING)
800 dump_stack();
801 printk(KERN_ERR
802 "%s: Peripheral %d function %d is already reserved by %s !\n",
803 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
804 hard_local_irq_restore(flags);
805 return -EBUSY;
809 if (unlikely(portmux_group_check(per))) {
810 hard_local_irq_restore(flags);
811 return -EBUSY;
813 anyway:
814 reserve(peri, ident);
816 portmux_setup(per);
817 port_setup(ident, PERIPHERAL_USAGE);
819 hard_local_irq_restore(flags);
820 set_label(ident, label);
822 return 0;
824 EXPORT_SYMBOL(peripheral_request);
826 int peripheral_request_list(const unsigned short per[], const char *label)
828 u16 cnt;
829 int ret;
831 for (cnt = 0; per[cnt] != 0; cnt++) {
833 ret = peripheral_request(per[cnt], label);
835 if (ret < 0) {
836 for ( ; cnt > 0; cnt--)
837 peripheral_free(per[cnt - 1]);
839 return ret;
843 return 0;
845 EXPORT_SYMBOL(peripheral_request_list);
847 void peripheral_free(unsigned short per)
849 unsigned long flags;
850 unsigned short ident = P_IDENT(per);
852 if (per & P_DONTCARE)
853 return;
855 if (!(per & P_DEFINED))
856 return;
858 flags = hard_local_irq_save();
860 if (unlikely(!is_reserved(peri, ident, 0))) {
861 hard_local_irq_restore(flags);
862 return;
865 if (!(per & P_MAYSHARE))
866 port_setup(ident, GPIO_USAGE);
868 unreserve(peri, ident);
870 set_label(ident, "free");
872 hard_local_irq_restore(flags);
874 EXPORT_SYMBOL(peripheral_free);
876 void peripheral_free_list(const unsigned short per[])
878 u16 cnt;
879 for (cnt = 0; per[cnt] != 0; cnt++)
880 peripheral_free(per[cnt]);
882 EXPORT_SYMBOL(peripheral_free_list);
884 /***********************************************************
886 * FUNCTIONS: Blackfin GPIO Driver
888 * INPUTS/OUTPUTS:
889 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
890 * label String
892 * DESCRIPTION: Blackfin GPIO Driver API
894 * CAUTION:
895 *************************************************************
896 * MODIFICATION HISTORY :
897 **************************************************************/
899 int bfin_gpio_request(unsigned gpio, const char *label)
901 unsigned long flags;
903 if (check_gpio(gpio) < 0)
904 return -EINVAL;
906 flags = hard_local_irq_save();
909 * Allow that the identical GPIO can
910 * be requested from the same driver twice
911 * Do nothing and return -
914 if (cmp_label(gpio, label) == 0) {
915 hard_local_irq_restore(flags);
916 return 0;
919 if (unlikely(is_reserved(gpio, gpio, 1))) {
920 if (system_state == SYSTEM_BOOTING)
921 dump_stack();
922 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
923 gpio, get_label(gpio));
924 hard_local_irq_restore(flags);
925 return -EBUSY;
927 if (unlikely(is_reserved(peri, gpio, 1))) {
928 if (system_state == SYSTEM_BOOTING)
929 dump_stack();
930 printk(KERN_ERR
931 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
932 gpio, get_label(gpio));
933 hard_local_irq_restore(flags);
934 return -EBUSY;
936 if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
937 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
938 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
940 #ifndef CONFIG_BF54x
941 else { /* Reset POLAR setting when acquiring a gpio for the first time */
942 set_gpio_polar(gpio, 0);
944 #endif
946 reserve(gpio, gpio);
947 set_label(gpio, label);
949 hard_local_irq_restore(flags);
951 port_setup(gpio, GPIO_USAGE);
953 return 0;
955 EXPORT_SYMBOL(bfin_gpio_request);
957 void bfin_gpio_free(unsigned gpio)
959 unsigned long flags;
961 if (check_gpio(gpio) < 0)
962 return;
964 might_sleep();
966 flags = hard_local_irq_save();
968 if (unlikely(!is_reserved(gpio, gpio, 0))) {
969 if (system_state == SYSTEM_BOOTING)
970 dump_stack();
971 gpio_error(gpio);
972 hard_local_irq_restore(flags);
973 return;
976 unreserve(gpio, gpio);
978 set_label(gpio, "free");
980 hard_local_irq_restore(flags);
982 EXPORT_SYMBOL(bfin_gpio_free);
984 #ifdef BFIN_SPECIAL_GPIO_BANKS
985 DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
987 int bfin_special_gpio_request(unsigned gpio, const char *label)
989 unsigned long flags;
991 flags = hard_local_irq_save();
994 * Allow that the identical GPIO can
995 * be requested from the same driver twice
996 * Do nothing and return -
999 if (cmp_label(gpio, label) == 0) {
1000 hard_local_irq_restore(flags);
1001 return 0;
1004 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
1005 hard_local_irq_restore(flags);
1006 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1007 gpio, get_label(gpio));
1009 return -EBUSY;
1011 if (unlikely(is_reserved(peri, gpio, 1))) {
1012 hard_local_irq_restore(flags);
1013 printk(KERN_ERR
1014 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1015 gpio, get_label(gpio));
1017 return -EBUSY;
1020 reserve(special_gpio, gpio);
1021 reserve(peri, gpio);
1023 set_label(gpio, label);
1024 hard_local_irq_restore(flags);
1025 port_setup(gpio, GPIO_USAGE);
1027 return 0;
1029 EXPORT_SYMBOL(bfin_special_gpio_request);
1031 void bfin_special_gpio_free(unsigned gpio)
1033 unsigned long flags;
1035 might_sleep();
1037 flags = hard_local_irq_save();
1039 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
1040 gpio_error(gpio);
1041 hard_local_irq_restore(flags);
1042 return;
1045 unreserve(special_gpio, gpio);
1046 unreserve(peri, gpio);
1047 set_label(gpio, "free");
1048 hard_local_irq_restore(flags);
1050 EXPORT_SYMBOL(bfin_special_gpio_free);
1051 #endif
1054 int bfin_gpio_irq_request(unsigned gpio, const char *label)
1056 unsigned long flags;
1058 if (check_gpio(gpio) < 0)
1059 return -EINVAL;
1061 flags = hard_local_irq_save();
1063 if (unlikely(is_reserved(peri, gpio, 1))) {
1064 if (system_state == SYSTEM_BOOTING)
1065 dump_stack();
1066 printk(KERN_ERR
1067 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1068 gpio, get_label(gpio));
1069 hard_local_irq_restore(flags);
1070 return -EBUSY;
1072 if (unlikely(is_reserved(gpio, gpio, 1)))
1073 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1074 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1075 gpio, get_label(gpio));
1077 reserve(gpio_irq, gpio);
1078 set_label(gpio, label);
1080 hard_local_irq_restore(flags);
1082 port_setup(gpio, GPIO_USAGE);
1084 return 0;
1087 void bfin_gpio_irq_free(unsigned gpio)
1089 unsigned long flags;
1091 if (check_gpio(gpio) < 0)
1092 return;
1094 flags = hard_local_irq_save();
1096 if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
1097 if (system_state == SYSTEM_BOOTING)
1098 dump_stack();
1099 gpio_error(gpio);
1100 hard_local_irq_restore(flags);
1101 return;
1104 unreserve(gpio_irq, gpio);
1106 set_label(gpio, "free");
1108 hard_local_irq_restore(flags);
1111 static inline void __bfin_gpio_direction_input(unsigned gpio)
1113 #ifdef CONFIG_BF54x
1114 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
1115 #else
1116 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1117 #endif
1118 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1121 int bfin_gpio_direction_input(unsigned gpio)
1123 unsigned long flags;
1125 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1126 gpio_error(gpio);
1127 return -EINVAL;
1130 flags = hard_local_irq_save();
1131 __bfin_gpio_direction_input(gpio);
1132 AWA_DUMMY_READ(inen);
1133 hard_local_irq_restore(flags);
1135 return 0;
1137 EXPORT_SYMBOL(bfin_gpio_direction_input);
1139 void bfin_gpio_irq_prepare(unsigned gpio)
1141 #ifdef CONFIG_BF54x
1142 unsigned long flags;
1143 #endif
1145 port_setup(gpio, GPIO_USAGE);
1147 #ifdef CONFIG_BF54x
1148 flags = hard_local_irq_save();
1149 __bfin_gpio_direction_input(gpio);
1150 hard_local_irq_restore(flags);
1151 #endif
1154 void bfin_gpio_set_value(unsigned gpio, int arg)
1156 if (arg)
1157 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1158 else
1159 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1161 EXPORT_SYMBOL(bfin_gpio_set_value);
1163 int bfin_gpio_direction_output(unsigned gpio, int value)
1165 unsigned long flags;
1167 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1168 gpio_error(gpio);
1169 return -EINVAL;
1172 flags = hard_local_irq_save();
1174 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1175 gpio_set_value(gpio, value);
1176 #ifdef CONFIG_BF54x
1177 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
1178 #else
1179 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1180 #endif
1182 AWA_DUMMY_READ(dir);
1183 hard_local_irq_restore(flags);
1185 return 0;
1187 EXPORT_SYMBOL(bfin_gpio_direction_output);
1189 int bfin_gpio_get_value(unsigned gpio)
1191 #ifdef CONFIG_BF54x
1192 return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
1193 #else
1194 unsigned long flags;
1196 if (unlikely(get_gpio_edge(gpio))) {
1197 int ret;
1198 flags = hard_local_irq_save();
1199 set_gpio_edge(gpio, 0);
1200 ret = get_gpio_data(gpio);
1201 set_gpio_edge(gpio, 1);
1202 hard_local_irq_restore(flags);
1203 return ret;
1204 } else
1205 return get_gpio_data(gpio);
1206 #endif
1208 EXPORT_SYMBOL(bfin_gpio_get_value);
1210 /* If we are booting from SPI and our board lacks a strong enough pull up,
1211 * the core can reset and execute the bootrom faster than the resistor can
1212 * pull the signal logically high. To work around this (common) error in
1213 * board design, we explicitly set the pin back to GPIO mode, force /CS
1214 * high, and wait for the electrons to do their thing.
1216 * This function only makes sense to be called from reset code, but it
1217 * lives here as we need to force all the GPIO states w/out going through
1218 * BUG() checks and such.
1220 void bfin_reset_boot_spi_cs(unsigned short pin)
1222 unsigned short gpio = P_IDENT(pin);
1223 port_setup(gpio, GPIO_USAGE);
1224 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1225 AWA_DUMMY_READ(data_set);
1226 udelay(1);
1229 #if defined(CONFIG_PROC_FS)
1230 static int gpio_proc_show(struct seq_file *m, void *v)
1232 int c, irq, gpio;
1234 for (c = 0; c < MAX_RESOURCES; c++) {
1235 irq = is_reserved(gpio_irq, c, 1);
1236 gpio = is_reserved(gpio, c, 1);
1237 if (!check_gpio(c) && (gpio || irq))
1238 seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1239 get_label(c), (gpio && irq) ? " *" : "",
1240 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1241 else if (is_reserved(peri, c, 1))
1242 seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1243 else
1244 continue;
1247 return 0;
1250 static int gpio_proc_open(struct inode *inode, struct file *file)
1252 return single_open(file, gpio_proc_show, NULL);
1255 static const struct file_operations gpio_proc_ops = {
1256 .open = gpio_proc_open,
1257 .read = seq_read,
1258 .llseek = seq_lseek,
1259 .release = single_release,
1262 static __init int gpio_register_proc(void)
1264 struct proc_dir_entry *proc_gpio;
1266 proc_gpio = proc_create("gpio", S_IRUGO, NULL, &gpio_proc_ops);
1267 return proc_gpio != NULL;
1269 __initcall(gpio_register_proc);
1270 #endif
1272 #ifdef CONFIG_GPIOLIB
1273 static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1275 return bfin_gpio_direction_input(gpio);
1278 static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1280 return bfin_gpio_direction_output(gpio, level);
1283 static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1285 return bfin_gpio_get_value(gpio);
1288 static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1290 return bfin_gpio_set_value(gpio, value);
1293 static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1295 return bfin_gpio_request(gpio, chip->label);
1298 static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1300 return bfin_gpio_free(gpio);
1303 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1305 return gpio + GPIO_IRQ_BASE;
1308 static struct gpio_chip bfin_chip = {
1309 .label = "BFIN-GPIO",
1310 .direction_input = bfin_gpiolib_direction_input,
1311 .get = bfin_gpiolib_get_value,
1312 .direction_output = bfin_gpiolib_direction_output,
1313 .set = bfin_gpiolib_set_value,
1314 .request = bfin_gpiolib_gpio_request,
1315 .free = bfin_gpiolib_gpio_free,
1316 .to_irq = bfin_gpiolib_gpio_to_irq,
1317 .base = 0,
1318 .ngpio = MAX_BLACKFIN_GPIOS,
1321 static int __init bfin_gpiolib_setup(void)
1323 return gpiochip_add(&bfin_chip);
1325 arch_initcall(bfin_gpiolib_setup);
1326 #endif