kvm tools, setup: Create private directory
[linux-2.6/next.git] / arch / blackfin / kernel / bfin_gpio.c
blobbcf8cf6fe4126d6608a560b39e2b92ade8b89a7a
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 #endif
123 static void gpio_error(unsigned gpio)
125 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
128 static void set_label(unsigned short ident, const char *label)
130 if (label) {
131 strncpy(str_ident[ident].name, label,
132 RESOURCE_LABEL_SIZE);
133 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
137 static char *get_label(unsigned short ident)
139 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
142 static int cmp_label(unsigned short ident, const char *label)
144 if (label == NULL) {
145 dump_stack();
146 printk(KERN_ERR "Please provide none-null label\n");
149 if (label)
150 return strcmp(str_ident[ident].name, label);
151 else
152 return -EINVAL;
155 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
156 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
157 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
158 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
159 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
161 DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
162 DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
163 DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
165 inline int check_gpio(unsigned gpio)
167 #if defined(CONFIG_BF54x)
168 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
169 || gpio == GPIO_PH14 || gpio == GPIO_PH15
170 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
171 return -EINVAL;
172 #endif
173 if (gpio >= MAX_BLACKFIN_GPIOS)
174 return -EINVAL;
175 return 0;
178 static void port_setup(unsigned gpio, unsigned short usage)
180 #if defined(BF538_FAMILY)
182 * BF538/9 Port C,D and E are special.
183 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
184 * Regular PORT F GPIOs are handled here, CDE are exclusively
185 * managed by GPIOLIB
188 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
189 return;
191 gpio -= MAX_BLACKFIN_GPIOS;
193 if (usage == GPIO_USAGE)
194 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
195 else
196 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
197 SSYNC();
198 return;
199 #endif
201 if (check_gpio(gpio))
202 return;
204 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
205 if (usage == GPIO_USAGE)
206 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
207 else
208 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
209 SSYNC();
210 #elif defined(CONFIG_BF54x)
211 if (usage == GPIO_USAGE)
212 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
213 else
214 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
215 SSYNC();
216 #endif
219 #ifdef BF537_FAMILY
220 static const s8 port_mux[] = {
221 [GPIO_PF0] = 3,
222 [GPIO_PF1] = 3,
223 [GPIO_PF2] = 4,
224 [GPIO_PF3] = 4,
225 [GPIO_PF4] = 5,
226 [GPIO_PF5] = 6,
227 [GPIO_PF6] = 7,
228 [GPIO_PF7] = 8,
229 [GPIO_PF8 ... GPIO_PF15] = -1,
230 [GPIO_PG0 ... GPIO_PG7] = -1,
231 [GPIO_PG8] = 9,
232 [GPIO_PG9] = 9,
233 [GPIO_PG10] = 10,
234 [GPIO_PG11] = 10,
235 [GPIO_PG12] = 10,
236 [GPIO_PG13] = 11,
237 [GPIO_PG14] = 11,
238 [GPIO_PG15] = 11,
239 [GPIO_PH0 ... GPIO_PH15] = -1,
240 [PORT_PJ0 ... PORT_PJ3] = -1,
241 [PORT_PJ4] = 1,
242 [PORT_PJ5] = 1,
243 [PORT_PJ6 ... PORT_PJ9] = -1,
244 [PORT_PJ10] = 0,
245 [PORT_PJ11] = 0,
248 static int portmux_group_check(unsigned short per)
250 u16 ident = P_IDENT(per);
251 u16 function = P_FUNCT2MUX(per);
252 s8 offset = port_mux[ident];
253 u16 m, pmux, pfunc;
255 if (offset < 0)
256 return 0;
258 pmux = bfin_read_PORT_MUX();
259 for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
260 if (m == ident)
261 continue;
262 if (port_mux[m] != offset)
263 continue;
264 if (!is_reserved(peri, m, 1))
265 continue;
267 if (offset == 1)
268 pfunc = (pmux >> offset) & 3;
269 else
270 pfunc = (pmux >> offset) & 1;
271 if (pfunc != function) {
272 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
273 ident, function, m, pfunc);
274 return -EINVAL;
278 return 0;
281 static void portmux_setup(unsigned short per)
283 u16 ident = P_IDENT(per);
284 u16 function = P_FUNCT2MUX(per);
285 s8 offset = port_mux[ident];
286 u16 pmux;
288 if (offset == -1)
289 return;
291 pmux = bfin_read_PORT_MUX();
292 if (offset != 1)
293 pmux &= ~(1 << offset);
294 else
295 pmux &= ~(3 << 1);
296 pmux |= (function << offset);
297 bfin_write_PORT_MUX(pmux);
299 #elif defined(CONFIG_BF54x)
300 inline void portmux_setup(unsigned short per)
302 u16 ident = P_IDENT(per);
303 u16 function = P_FUNCT2MUX(per);
304 u32 pmux;
306 pmux = gpio_array[gpio_bank(ident)]->port_mux;
308 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
309 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
311 gpio_array[gpio_bank(ident)]->port_mux = pmux;
314 inline u16 get_portmux(unsigned short per)
316 u16 ident = P_IDENT(per);
317 u32 pmux = gpio_array[gpio_bank(ident)]->port_mux;
318 return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
320 static int portmux_group_check(unsigned short per)
322 return 0;
324 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
325 static int portmux_group_check(unsigned short per)
327 u16 ident = P_IDENT(per);
328 u16 function = P_FUNCT2MUX(per);
329 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
330 u16 pin, gpiopin, pfunc;
332 for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
333 if (offset != pmux_offset[gpio_bank(ident)][pin])
334 continue;
336 gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
337 if (gpiopin == ident)
338 continue;
339 if (!is_reserved(peri, gpiopin, 1))
340 continue;
342 pfunc = *port_mux[gpio_bank(ident)];
343 pfunc = (pfunc >> offset) & 3;
344 if (pfunc != function) {
345 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
346 ident, function, gpiopin, pfunc);
347 return -EINVAL;
351 return 0;
354 inline void portmux_setup(unsigned short per)
356 u16 ident = P_IDENT(per);
357 u16 function = P_FUNCT2MUX(per);
358 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
359 u16 pmux;
361 pmux = *port_mux[gpio_bank(ident)];
362 if (((pmux >> offset) & 3) == function)
363 return;
364 pmux &= ~(3 << offset);
365 pmux |= (function & 3) << offset;
366 *port_mux[gpio_bank(ident)] = pmux;
367 SSYNC();
369 #else
370 # define portmux_setup(...) do { } while (0)
371 static int portmux_group_check(unsigned short per)
373 return 0;
375 #endif
377 #ifndef CONFIG_BF54x
378 /***********************************************************
380 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
382 * INPUTS/OUTPUTS:
383 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
386 * DESCRIPTION: These functions abstract direct register access
387 * to Blackfin processor General Purpose
388 * Ports Regsiters
390 * CAUTION: These functions do not belong to the GPIO Driver API
391 *************************************************************
392 * MODIFICATION HISTORY :
393 **************************************************************/
395 /* Set a specific bit */
397 #define SET_GPIO(name) \
398 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
400 unsigned long flags; \
401 flags = hard_local_irq_save(); \
402 if (arg) \
403 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
404 else \
405 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
406 AWA_DUMMY_READ(name); \
407 hard_local_irq_restore(flags); \
409 EXPORT_SYMBOL(set_gpio_ ## name);
411 SET_GPIO(dir) /* set_gpio_dir() */
412 SET_GPIO(inen) /* set_gpio_inen() */
413 SET_GPIO(polar) /* set_gpio_polar() */
414 SET_GPIO(edge) /* set_gpio_edge() */
415 SET_GPIO(both) /* set_gpio_both() */
418 #define SET_GPIO_SC(name) \
419 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
421 unsigned long flags; \
422 if (ANOMALY_05000311 || ANOMALY_05000323) \
423 flags = hard_local_irq_save(); \
424 if (arg) \
425 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
426 else \
427 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
428 if (ANOMALY_05000311 || ANOMALY_05000323) { \
429 AWA_DUMMY_READ(name); \
430 hard_local_irq_restore(flags); \
433 EXPORT_SYMBOL(set_gpio_ ## name);
435 SET_GPIO_SC(maska)
436 SET_GPIO_SC(maskb)
437 SET_GPIO_SC(data)
439 void set_gpio_toggle(unsigned gpio)
441 unsigned long flags;
442 if (ANOMALY_05000311 || ANOMALY_05000323)
443 flags = hard_local_irq_save();
444 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
445 if (ANOMALY_05000311 || ANOMALY_05000323) {
446 AWA_DUMMY_READ(toggle);
447 hard_local_irq_restore(flags);
450 EXPORT_SYMBOL(set_gpio_toggle);
453 /*Set current PORT date (16-bit word)*/
455 #define SET_GPIO_P(name) \
456 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
458 unsigned long flags; \
459 if (ANOMALY_05000311 || ANOMALY_05000323) \
460 flags = hard_local_irq_save(); \
461 gpio_array[gpio_bank(gpio)]->name = arg; \
462 if (ANOMALY_05000311 || ANOMALY_05000323) { \
463 AWA_DUMMY_READ(name); \
464 hard_local_irq_restore(flags); \
467 EXPORT_SYMBOL(set_gpiop_ ## name);
469 SET_GPIO_P(data)
470 SET_GPIO_P(dir)
471 SET_GPIO_P(inen)
472 SET_GPIO_P(polar)
473 SET_GPIO_P(edge)
474 SET_GPIO_P(both)
475 SET_GPIO_P(maska)
476 SET_GPIO_P(maskb)
478 /* Get a specific bit */
479 #define GET_GPIO(name) \
480 unsigned short get_gpio_ ## name(unsigned gpio) \
482 unsigned long flags; \
483 unsigned short ret; \
484 if (ANOMALY_05000311 || ANOMALY_05000323) \
485 flags = hard_local_irq_save(); \
486 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
487 if (ANOMALY_05000311 || ANOMALY_05000323) { \
488 AWA_DUMMY_READ(name); \
489 hard_local_irq_restore(flags); \
491 return ret; \
493 EXPORT_SYMBOL(get_gpio_ ## name);
495 GET_GPIO(data)
496 GET_GPIO(dir)
497 GET_GPIO(inen)
498 GET_GPIO(polar)
499 GET_GPIO(edge)
500 GET_GPIO(both)
501 GET_GPIO(maska)
502 GET_GPIO(maskb)
504 /*Get current PORT date (16-bit word)*/
506 #define GET_GPIO_P(name) \
507 unsigned short get_gpiop_ ## name(unsigned gpio) \
509 unsigned long flags; \
510 unsigned short ret; \
511 if (ANOMALY_05000311 || ANOMALY_05000323) \
512 flags = hard_local_irq_save(); \
513 ret = (gpio_array[gpio_bank(gpio)]->name); \
514 if (ANOMALY_05000311 || ANOMALY_05000323) { \
515 AWA_DUMMY_READ(name); \
516 hard_local_irq_restore(flags); \
518 return ret; \
520 EXPORT_SYMBOL(get_gpiop_ ## name);
522 GET_GPIO_P(data)
523 GET_GPIO_P(dir)
524 GET_GPIO_P(inen)
525 GET_GPIO_P(polar)
526 GET_GPIO_P(edge)
527 GET_GPIO_P(both)
528 GET_GPIO_P(maska)
529 GET_GPIO_P(maskb)
532 #ifdef CONFIG_PM
533 DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
535 static const unsigned int sic_iwr_irqs[] = {
536 #if defined(BF533_FAMILY)
537 IRQ_PROG_INTB
538 #elif defined(BF537_FAMILY)
539 IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
540 #elif defined(BF538_FAMILY)
541 IRQ_PORTF_INTB
542 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
543 IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
544 #elif defined(BF561_FAMILY)
545 IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
546 #else
547 # error no SIC_IWR defined
548 #endif
551 /***********************************************************
553 * FUNCTIONS: Blackfin PM Setup API
555 * INPUTS/OUTPUTS:
556 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
557 * type -
558 * PM_WAKE_RISING
559 * PM_WAKE_FALLING
560 * PM_WAKE_HIGH
561 * PM_WAKE_LOW
562 * PM_WAKE_BOTH_EDGES
564 * DESCRIPTION: Blackfin PM Driver API
566 * CAUTION:
567 *************************************************************
568 * MODIFICATION HISTORY :
569 **************************************************************/
570 int gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
572 unsigned long flags;
574 if (check_gpio(gpio) < 0)
575 return -EINVAL;
577 flags = hard_local_irq_save();
578 if (ctrl)
579 reserve(wakeup, gpio);
580 else
581 unreserve(wakeup, gpio);
583 set_gpio_maskb(gpio, ctrl);
584 hard_local_irq_restore(flags);
586 return 0;
589 int bfin_pm_standby_ctrl(unsigned ctrl)
591 u16 bank, mask, i;
593 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
594 mask = map_entry(wakeup, i);
595 bank = gpio_bank(i);
597 if (mask)
598 bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
600 return 0;
603 void bfin_gpio_pm_hibernate_suspend(void)
605 int i, bank;
607 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
608 bank = gpio_bank(i);
610 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
611 gpio_bank_saved[bank].fer = *port_fer[bank];
612 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
613 gpio_bank_saved[bank].mux = *port_mux[bank];
614 #else
615 if (bank == 0)
616 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
617 #endif
618 #endif
619 gpio_bank_saved[bank].data = gpio_array[bank]->data;
620 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
621 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
622 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
623 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
624 gpio_bank_saved[bank].both = gpio_array[bank]->both;
625 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
628 AWA_DUMMY_READ(maska);
631 void bfin_gpio_pm_hibernate_restore(void)
633 int i, bank;
635 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
636 bank = gpio_bank(i);
638 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
639 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
640 *port_mux[bank] = gpio_bank_saved[bank].mux;
641 #else
642 if (bank == 0)
643 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
644 #endif
645 *port_fer[bank] = gpio_bank_saved[bank].fer;
646 #endif
647 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
648 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
649 & gpio_bank_saved[bank].dir;
650 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
651 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
652 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
653 gpio_array[bank]->both = gpio_bank_saved[bank].both;
654 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
656 AWA_DUMMY_READ(maska);
660 #endif
661 #else /* CONFIG_BF54x */
662 #ifdef CONFIG_PM
664 int bfin_pm_standby_ctrl(unsigned ctrl)
666 return 0;
669 void bfin_gpio_pm_hibernate_suspend(void)
671 int i, bank;
673 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
674 bank = gpio_bank(i);
676 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
677 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
678 gpio_bank_saved[bank].data = gpio_array[bank]->data;
679 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
680 gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
684 void bfin_gpio_pm_hibernate_restore(void)
686 int i, bank;
688 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
689 bank = gpio_bank(i);
691 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
692 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
693 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
694 gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
695 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
696 | gpio_bank_saved[bank].dir;
699 #endif
701 unsigned short get_gpio_dir(unsigned gpio)
703 return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
705 EXPORT_SYMBOL(get_gpio_dir);
707 #endif /* CONFIG_BF54x */
709 /***********************************************************
711 * FUNCTIONS: Blackfin Peripheral Resource Allocation
712 * and PortMux Setup
714 * INPUTS/OUTPUTS:
715 * per Peripheral Identifier
716 * label String
718 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
720 * CAUTION:
721 *************************************************************
722 * MODIFICATION HISTORY :
723 **************************************************************/
725 int peripheral_request(unsigned short per, const char *label)
727 unsigned long flags;
728 unsigned short ident = P_IDENT(per);
731 * Don't cares are pins with only one dedicated function
734 if (per & P_DONTCARE)
735 return 0;
737 if (!(per & P_DEFINED))
738 return -ENODEV;
740 BUG_ON(ident >= MAX_RESOURCES);
742 flags = hard_local_irq_save();
744 /* If a pin can be muxed as either GPIO or peripheral, make
745 * sure it is not already a GPIO pin when we request it.
747 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
748 if (system_state == SYSTEM_BOOTING)
749 dump_stack();
750 printk(KERN_ERR
751 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
752 __func__, ident, get_label(ident));
753 hard_local_irq_restore(flags);
754 return -EBUSY;
757 if (unlikely(is_reserved(peri, ident, 1))) {
760 * Pin functions like AMC address strobes my
761 * be requested and used by several drivers
764 #ifdef CONFIG_BF54x
765 if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
766 #else
767 if (!(per & P_MAYSHARE)) {
768 #endif
770 * Allow that the identical pin function can
771 * be requested from the same driver twice
774 if (cmp_label(ident, label) == 0)
775 goto anyway;
777 if (system_state == SYSTEM_BOOTING)
778 dump_stack();
779 printk(KERN_ERR
780 "%s: Peripheral %d function %d is already reserved by %s !\n",
781 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
782 hard_local_irq_restore(flags);
783 return -EBUSY;
787 if (unlikely(portmux_group_check(per))) {
788 hard_local_irq_restore(flags);
789 return -EBUSY;
791 anyway:
792 reserve(peri, ident);
794 portmux_setup(per);
795 port_setup(ident, PERIPHERAL_USAGE);
797 hard_local_irq_restore(flags);
798 set_label(ident, label);
800 return 0;
802 EXPORT_SYMBOL(peripheral_request);
804 int peripheral_request_list(const unsigned short per[], const char *label)
806 u16 cnt;
807 int ret;
809 for (cnt = 0; per[cnt] != 0; cnt++) {
811 ret = peripheral_request(per[cnt], label);
813 if (ret < 0) {
814 for ( ; cnt > 0; cnt--)
815 peripheral_free(per[cnt - 1]);
817 return ret;
821 return 0;
823 EXPORT_SYMBOL(peripheral_request_list);
825 void peripheral_free(unsigned short per)
827 unsigned long flags;
828 unsigned short ident = P_IDENT(per);
830 if (per & P_DONTCARE)
831 return;
833 if (!(per & P_DEFINED))
834 return;
836 flags = hard_local_irq_save();
838 if (unlikely(!is_reserved(peri, ident, 0))) {
839 hard_local_irq_restore(flags);
840 return;
843 if (!(per & P_MAYSHARE))
844 port_setup(ident, GPIO_USAGE);
846 unreserve(peri, ident);
848 set_label(ident, "free");
850 hard_local_irq_restore(flags);
852 EXPORT_SYMBOL(peripheral_free);
854 void peripheral_free_list(const unsigned short per[])
856 u16 cnt;
857 for (cnt = 0; per[cnt] != 0; cnt++)
858 peripheral_free(per[cnt]);
860 EXPORT_SYMBOL(peripheral_free_list);
862 /***********************************************************
864 * FUNCTIONS: Blackfin GPIO Driver
866 * INPUTS/OUTPUTS:
867 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
868 * label String
870 * DESCRIPTION: Blackfin GPIO Driver API
872 * CAUTION:
873 *************************************************************
874 * MODIFICATION HISTORY :
875 **************************************************************/
877 int bfin_gpio_request(unsigned gpio, const char *label)
879 unsigned long flags;
881 if (check_gpio(gpio) < 0)
882 return -EINVAL;
884 flags = hard_local_irq_save();
887 * Allow that the identical GPIO can
888 * be requested from the same driver twice
889 * Do nothing and return -
892 if (cmp_label(gpio, label) == 0) {
893 hard_local_irq_restore(flags);
894 return 0;
897 if (unlikely(is_reserved(gpio, gpio, 1))) {
898 if (system_state == SYSTEM_BOOTING)
899 dump_stack();
900 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
901 gpio, get_label(gpio));
902 hard_local_irq_restore(flags);
903 return -EBUSY;
905 if (unlikely(is_reserved(peri, gpio, 1))) {
906 if (system_state == SYSTEM_BOOTING)
907 dump_stack();
908 printk(KERN_ERR
909 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
910 gpio, get_label(gpio));
911 hard_local_irq_restore(flags);
912 return -EBUSY;
914 if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
915 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
916 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
918 #ifndef CONFIG_BF54x
919 else { /* Reset POLAR setting when acquiring a gpio for the first time */
920 set_gpio_polar(gpio, 0);
922 #endif
924 reserve(gpio, gpio);
925 set_label(gpio, label);
927 hard_local_irq_restore(flags);
929 port_setup(gpio, GPIO_USAGE);
931 return 0;
933 EXPORT_SYMBOL(bfin_gpio_request);
935 void bfin_gpio_free(unsigned gpio)
937 unsigned long flags;
939 if (check_gpio(gpio) < 0)
940 return;
942 might_sleep();
944 flags = hard_local_irq_save();
946 if (unlikely(!is_reserved(gpio, gpio, 0))) {
947 if (system_state == SYSTEM_BOOTING)
948 dump_stack();
949 gpio_error(gpio);
950 hard_local_irq_restore(flags);
951 return;
954 unreserve(gpio, gpio);
956 set_label(gpio, "free");
958 hard_local_irq_restore(flags);
960 EXPORT_SYMBOL(bfin_gpio_free);
962 #ifdef BFIN_SPECIAL_GPIO_BANKS
963 DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
965 int bfin_special_gpio_request(unsigned gpio, const char *label)
967 unsigned long flags;
969 flags = hard_local_irq_save();
972 * Allow that the identical GPIO can
973 * be requested from the same driver twice
974 * Do nothing and return -
977 if (cmp_label(gpio, label) == 0) {
978 hard_local_irq_restore(flags);
979 return 0;
982 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
983 hard_local_irq_restore(flags);
984 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
985 gpio, get_label(gpio));
987 return -EBUSY;
989 if (unlikely(is_reserved(peri, gpio, 1))) {
990 hard_local_irq_restore(flags);
991 printk(KERN_ERR
992 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
993 gpio, get_label(gpio));
995 return -EBUSY;
998 reserve(special_gpio, gpio);
999 reserve(peri, gpio);
1001 set_label(gpio, label);
1002 hard_local_irq_restore(flags);
1003 port_setup(gpio, GPIO_USAGE);
1005 return 0;
1007 EXPORT_SYMBOL(bfin_special_gpio_request);
1009 void bfin_special_gpio_free(unsigned gpio)
1011 unsigned long flags;
1013 might_sleep();
1015 flags = hard_local_irq_save();
1017 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
1018 gpio_error(gpio);
1019 hard_local_irq_restore(flags);
1020 return;
1023 unreserve(special_gpio, gpio);
1024 unreserve(peri, gpio);
1025 set_label(gpio, "free");
1026 hard_local_irq_restore(flags);
1028 EXPORT_SYMBOL(bfin_special_gpio_free);
1029 #endif
1032 int bfin_gpio_irq_request(unsigned gpio, const char *label)
1034 unsigned long flags;
1036 if (check_gpio(gpio) < 0)
1037 return -EINVAL;
1039 flags = hard_local_irq_save();
1041 if (unlikely(is_reserved(peri, gpio, 1))) {
1042 if (system_state == SYSTEM_BOOTING)
1043 dump_stack();
1044 printk(KERN_ERR
1045 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1046 gpio, get_label(gpio));
1047 hard_local_irq_restore(flags);
1048 return -EBUSY;
1050 if (unlikely(is_reserved(gpio, gpio, 1)))
1051 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1052 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1053 gpio, get_label(gpio));
1055 reserve(gpio_irq, gpio);
1056 set_label(gpio, label);
1058 hard_local_irq_restore(flags);
1060 port_setup(gpio, GPIO_USAGE);
1062 return 0;
1065 void bfin_gpio_irq_free(unsigned gpio)
1067 unsigned long flags;
1069 if (check_gpio(gpio) < 0)
1070 return;
1072 flags = hard_local_irq_save();
1074 if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
1075 if (system_state == SYSTEM_BOOTING)
1076 dump_stack();
1077 gpio_error(gpio);
1078 hard_local_irq_restore(flags);
1079 return;
1082 unreserve(gpio_irq, gpio);
1084 set_label(gpio, "free");
1086 hard_local_irq_restore(flags);
1089 static inline void __bfin_gpio_direction_input(unsigned gpio)
1091 #ifdef CONFIG_BF54x
1092 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
1093 #else
1094 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1095 #endif
1096 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1099 int bfin_gpio_direction_input(unsigned gpio)
1101 unsigned long flags;
1103 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1104 gpio_error(gpio);
1105 return -EINVAL;
1108 flags = hard_local_irq_save();
1109 __bfin_gpio_direction_input(gpio);
1110 AWA_DUMMY_READ(inen);
1111 hard_local_irq_restore(flags);
1113 return 0;
1115 EXPORT_SYMBOL(bfin_gpio_direction_input);
1117 void bfin_gpio_irq_prepare(unsigned gpio)
1119 #ifdef CONFIG_BF54x
1120 unsigned long flags;
1121 #endif
1123 port_setup(gpio, GPIO_USAGE);
1125 #ifdef CONFIG_BF54x
1126 flags = hard_local_irq_save();
1127 __bfin_gpio_direction_input(gpio);
1128 hard_local_irq_restore(flags);
1129 #endif
1132 void bfin_gpio_set_value(unsigned gpio, int arg)
1134 if (arg)
1135 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1136 else
1137 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1139 EXPORT_SYMBOL(bfin_gpio_set_value);
1141 int bfin_gpio_direction_output(unsigned gpio, int value)
1143 unsigned long flags;
1145 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1146 gpio_error(gpio);
1147 return -EINVAL;
1150 flags = hard_local_irq_save();
1152 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1153 gpio_set_value(gpio, value);
1154 #ifdef CONFIG_BF54x
1155 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
1156 #else
1157 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1158 #endif
1160 AWA_DUMMY_READ(dir);
1161 hard_local_irq_restore(flags);
1163 return 0;
1165 EXPORT_SYMBOL(bfin_gpio_direction_output);
1167 int bfin_gpio_get_value(unsigned gpio)
1169 #ifdef CONFIG_BF54x
1170 return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
1171 #else
1172 unsigned long flags;
1174 if (unlikely(get_gpio_edge(gpio))) {
1175 int ret;
1176 flags = hard_local_irq_save();
1177 set_gpio_edge(gpio, 0);
1178 ret = get_gpio_data(gpio);
1179 set_gpio_edge(gpio, 1);
1180 hard_local_irq_restore(flags);
1181 return ret;
1182 } else
1183 return get_gpio_data(gpio);
1184 #endif
1186 EXPORT_SYMBOL(bfin_gpio_get_value);
1188 /* If we are booting from SPI and our board lacks a strong enough pull up,
1189 * the core can reset and execute the bootrom faster than the resistor can
1190 * pull the signal logically high. To work around this (common) error in
1191 * board design, we explicitly set the pin back to GPIO mode, force /CS
1192 * high, and wait for the electrons to do their thing.
1194 * This function only makes sense to be called from reset code, but it
1195 * lives here as we need to force all the GPIO states w/out going through
1196 * BUG() checks and such.
1198 void bfin_reset_boot_spi_cs(unsigned short pin)
1200 unsigned short gpio = P_IDENT(pin);
1201 port_setup(gpio, GPIO_USAGE);
1202 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1203 AWA_DUMMY_READ(data_set);
1204 udelay(1);
1207 #if defined(CONFIG_PROC_FS)
1208 static int gpio_proc_show(struct seq_file *m, void *v)
1210 int c, irq, gpio;
1212 for (c = 0; c < MAX_RESOURCES; c++) {
1213 irq = is_reserved(gpio_irq, c, 1);
1214 gpio = is_reserved(gpio, c, 1);
1215 if (!check_gpio(c) && (gpio || irq))
1216 seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1217 get_label(c), (gpio && irq) ? " *" : "",
1218 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1219 else if (is_reserved(peri, c, 1))
1220 seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1221 else
1222 continue;
1225 return 0;
1228 static int gpio_proc_open(struct inode *inode, struct file *file)
1230 return single_open(file, gpio_proc_show, NULL);
1233 static const struct file_operations gpio_proc_ops = {
1234 .open = gpio_proc_open,
1235 .read = seq_read,
1236 .llseek = seq_lseek,
1237 .release = single_release,
1240 static __init int gpio_register_proc(void)
1242 struct proc_dir_entry *proc_gpio;
1244 proc_gpio = proc_create("gpio", S_IRUGO, NULL, &gpio_proc_ops);
1245 return proc_gpio != NULL;
1247 __initcall(gpio_register_proc);
1248 #endif
1250 #ifdef CONFIG_GPIOLIB
1251 static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1253 return bfin_gpio_direction_input(gpio);
1256 static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1258 return bfin_gpio_direction_output(gpio, level);
1261 static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1263 return bfin_gpio_get_value(gpio);
1266 static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1268 return bfin_gpio_set_value(gpio, value);
1271 static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1273 return bfin_gpio_request(gpio, chip->label);
1276 static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1278 return bfin_gpio_free(gpio);
1281 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1283 return gpio + GPIO_IRQ_BASE;
1286 static struct gpio_chip bfin_chip = {
1287 .label = "BFIN-GPIO",
1288 .direction_input = bfin_gpiolib_direction_input,
1289 .get = bfin_gpiolib_get_value,
1290 .direction_output = bfin_gpiolib_direction_output,
1291 .set = bfin_gpiolib_set_value,
1292 .request = bfin_gpiolib_gpio_request,
1293 .free = bfin_gpiolib_gpio_free,
1294 .to_irq = bfin_gpiolib_gpio_to_irq,
1295 .base = 0,
1296 .ngpio = MAX_BLACKFIN_GPIOS,
1299 static int __init bfin_gpiolib_setup(void)
1301 return gpiochip_add(&bfin_chip);
1303 arch_initcall(bfin_gpiolib_setup);
1304 #endif