xtensa: fix high memory/reserved memory collision
[cris-mirror.git] / arch / blackfin / kernel / bfin_gpio.c
blob63da80bbadf600d4359ea85afe8098a3c65c9798
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 <linux/gpio/driver.h>
15 /* FIXME: consumer API required for gpio_set_value() etc, get rid of this */
16 #include <linux/gpio.h>
17 #include <linux/irq.h>
18 #include <asm/gpio.h>
19 #include <asm/irq_handler.h>
20 #include <asm/portmux.h>
22 #if ANOMALY_05000311 || ANOMALY_05000323
23 enum {
24 AWA_data = SYSCR,
25 AWA_data_clear = SYSCR,
26 AWA_data_set = SYSCR,
27 AWA_toggle = SYSCR,
28 AWA_maska = BFIN_UART_SCR,
29 AWA_maska_clear = BFIN_UART_SCR,
30 AWA_maska_set = BFIN_UART_SCR,
31 AWA_maska_toggle = BFIN_UART_SCR,
32 AWA_maskb = BFIN_UART_GCTL,
33 AWA_maskb_clear = BFIN_UART_GCTL,
34 AWA_maskb_set = BFIN_UART_GCTL,
35 AWA_maskb_toggle = BFIN_UART_GCTL,
36 AWA_dir = SPORT1_STAT,
37 AWA_polar = SPORT1_STAT,
38 AWA_edge = SPORT1_STAT,
39 AWA_both = SPORT1_STAT,
40 #if ANOMALY_05000311
41 AWA_inen = TIMER_ENABLE,
42 #elif ANOMALY_05000323
43 AWA_inen = DMA1_1_CONFIG,
44 #endif
46 /* Anomaly Workaround */
47 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
48 #else
49 #define AWA_DUMMY_READ(...) do { } while (0)
50 #endif
52 static struct gpio_port_t * const gpio_array[] = {
53 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
54 (struct gpio_port_t *) FIO_FLAG_D,
55 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
56 (struct gpio_port_t *) PORTFIO,
57 (struct gpio_port_t *) PORTGIO,
58 (struct gpio_port_t *) PORTHIO,
59 #elif defined(BF561_FAMILY)
60 (struct gpio_port_t *) FIO0_FLAG_D,
61 (struct gpio_port_t *) FIO1_FLAG_D,
62 (struct gpio_port_t *) FIO2_FLAG_D,
63 #else
64 # error no gpio arrays defined
65 #endif
68 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
69 static unsigned short * const port_fer[] = {
70 (unsigned short *) PORTF_FER,
71 (unsigned short *) PORTG_FER,
72 (unsigned short *) PORTH_FER,
75 # if !defined(BF537_FAMILY)
76 static unsigned short * const port_mux[] = {
77 (unsigned short *) PORTF_MUX,
78 (unsigned short *) PORTG_MUX,
79 (unsigned short *) PORTH_MUX,
82 static const
83 u8 pmux_offset[][16] = {
84 # if defined(CONFIG_BF52x)
85 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
86 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
87 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
88 # elif defined(CONFIG_BF51x)
89 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
90 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
91 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
92 # endif
94 # endif
96 #elif defined(BF538_FAMILY)
97 static unsigned short * const port_fer[] = {
98 (unsigned short *) PORTCIO_FER,
99 (unsigned short *) PORTDIO_FER,
100 (unsigned short *) PORTEIO_FER,
102 #endif
104 #define RESOURCE_LABEL_SIZE 16
106 static struct str_ident {
107 char name[RESOURCE_LABEL_SIZE];
108 } str_ident[MAX_RESOURCES];
110 #if defined(CONFIG_PM)
111 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
112 # ifdef BF538_FAMILY
113 static unsigned short port_fer_saved[3];
114 # endif
115 #endif
117 static void gpio_error(unsigned gpio)
119 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
122 static void set_label(unsigned short ident, const char *label)
124 if (label) {
125 strncpy(str_ident[ident].name, label,
126 RESOURCE_LABEL_SIZE);
127 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
131 static char *get_label(unsigned short ident)
133 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
136 static int cmp_label(unsigned short ident, const char *label)
138 if (label == NULL) {
139 dump_stack();
140 printk(KERN_ERR "Please provide none-null label\n");
143 if (label)
144 return strcmp(str_ident[ident].name, label);
145 else
146 return -EINVAL;
149 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
150 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
151 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
152 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
153 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
155 DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
156 DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
157 DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
159 inline int check_gpio(unsigned gpio)
161 if (gpio >= MAX_BLACKFIN_GPIOS)
162 return -EINVAL;
163 return 0;
166 static void port_setup(unsigned gpio, unsigned short usage)
168 #if defined(BF538_FAMILY)
170 * BF538/9 Port C,D and E are special.
171 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
172 * Regular PORT F GPIOs are handled here, CDE are exclusively
173 * managed by GPIOLIB
176 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
177 return;
179 gpio -= MAX_BLACKFIN_GPIOS;
181 if (usage == GPIO_USAGE)
182 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
183 else
184 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
185 SSYNC();
186 return;
187 #endif
189 if (check_gpio(gpio))
190 return;
192 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
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 #endif
201 #ifdef BF537_FAMILY
202 static const s8 port_mux[] = {
203 [GPIO_PF0] = 3,
204 [GPIO_PF1] = 3,
205 [GPIO_PF2] = 4,
206 [GPIO_PF3] = 4,
207 [GPIO_PF4] = 5,
208 [GPIO_PF5] = 6,
209 [GPIO_PF6] = 7,
210 [GPIO_PF7] = 8,
211 [GPIO_PF8 ... GPIO_PF15] = -1,
212 [GPIO_PG0 ... GPIO_PG7] = -1,
213 [GPIO_PG8] = 9,
214 [GPIO_PG9] = 9,
215 [GPIO_PG10] = 10,
216 [GPIO_PG11] = 10,
217 [GPIO_PG12] = 10,
218 [GPIO_PG13] = 11,
219 [GPIO_PG14] = 11,
220 [GPIO_PG15] = 11,
221 [GPIO_PH0 ... GPIO_PH15] = -1,
222 [PORT_PJ0 ... PORT_PJ3] = -1,
223 [PORT_PJ4] = 1,
224 [PORT_PJ5] = 1,
225 [PORT_PJ6 ... PORT_PJ9] = -1,
226 [PORT_PJ10] = 0,
227 [PORT_PJ11] = 0,
230 static int portmux_group_check(unsigned short per)
232 u16 ident = P_IDENT(per);
233 u16 function = P_FUNCT2MUX(per);
234 s8 offset = port_mux[ident];
235 u16 m, pmux, pfunc, mask;
237 if (offset < 0)
238 return 0;
240 pmux = bfin_read_PORT_MUX();
241 for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
242 if (m == ident)
243 continue;
244 if (port_mux[m] != offset)
245 continue;
246 if (!is_reserved(peri, m, 1))
247 continue;
249 if (offset == 1)
250 mask = 3;
251 else
252 mask = 1;
254 pfunc = (pmux >> offset) & mask;
255 if (pfunc != (function & mask)) {
256 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
257 ident, function, m, pfunc);
258 return -EINVAL;
262 return 0;
265 static void portmux_setup(unsigned short per)
267 u16 ident = P_IDENT(per);
268 u16 function = P_FUNCT2MUX(per);
269 s8 offset = port_mux[ident];
270 u16 pmux, mask;
272 if (offset == -1)
273 return;
275 pmux = bfin_read_PORT_MUX();
276 if (offset == 1)
277 mask = 3;
278 else
279 mask = 1;
281 pmux &= ~(mask << offset);
282 pmux |= ((function & mask) << offset);
284 bfin_write_PORT_MUX(pmux);
286 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
287 static int portmux_group_check(unsigned short per)
289 u16 ident = P_IDENT(per);
290 u16 function = P_FUNCT2MUX(per);
291 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
292 u16 pin, gpiopin, pfunc;
294 for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
295 if (offset != pmux_offset[gpio_bank(ident)][pin])
296 continue;
298 gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
299 if (gpiopin == ident)
300 continue;
301 if (!is_reserved(peri, gpiopin, 1))
302 continue;
304 pfunc = *port_mux[gpio_bank(ident)];
305 pfunc = (pfunc >> offset) & 3;
306 if (pfunc != function) {
307 pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
308 ident, function, gpiopin, pfunc);
309 return -EINVAL;
313 return 0;
316 inline void portmux_setup(unsigned short per)
318 u16 ident = P_IDENT(per);
319 u16 function = P_FUNCT2MUX(per);
320 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
321 u16 pmux;
323 pmux = *port_mux[gpio_bank(ident)];
324 if (((pmux >> offset) & 3) == function)
325 return;
326 pmux &= ~(3 << offset);
327 pmux |= (function & 3) << offset;
328 *port_mux[gpio_bank(ident)] = pmux;
329 SSYNC();
331 #else
332 # define portmux_setup(...) do { } while (0)
333 static int portmux_group_check(unsigned short per)
335 return 0;
337 #endif
339 /***********************************************************
341 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
343 * INPUTS/OUTPUTS:
344 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
347 * DESCRIPTION: These functions abstract direct register access
348 * to Blackfin processor General Purpose
349 * Ports Regsiters
351 * CAUTION: These functions do not belong to the GPIO Driver API
352 *************************************************************
353 * MODIFICATION HISTORY :
354 **************************************************************/
356 /* Set a specific bit */
358 #define SET_GPIO(name) \
359 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
361 unsigned long flags; \
362 flags = hard_local_irq_save(); \
363 if (arg) \
364 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
365 else \
366 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
367 AWA_DUMMY_READ(name); \
368 hard_local_irq_restore(flags); \
370 EXPORT_SYMBOL(set_gpio_ ## name);
372 SET_GPIO(dir) /* set_gpio_dir() */
373 SET_GPIO(inen) /* set_gpio_inen() */
374 SET_GPIO(polar) /* set_gpio_polar() */
375 SET_GPIO(edge) /* set_gpio_edge() */
376 SET_GPIO(both) /* set_gpio_both() */
379 #define SET_GPIO_SC(name) \
380 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
382 unsigned long flags; \
383 if (ANOMALY_05000311 || ANOMALY_05000323) \
384 flags = hard_local_irq_save(); \
385 if (arg) \
386 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
387 else \
388 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
389 if (ANOMALY_05000311 || ANOMALY_05000323) { \
390 AWA_DUMMY_READ(name); \
391 hard_local_irq_restore(flags); \
394 EXPORT_SYMBOL(set_gpio_ ## name);
396 SET_GPIO_SC(maska)
397 SET_GPIO_SC(maskb)
398 SET_GPIO_SC(data)
400 void set_gpio_toggle(unsigned gpio)
402 unsigned long flags;
403 if (ANOMALY_05000311 || ANOMALY_05000323)
404 flags = hard_local_irq_save();
405 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
406 if (ANOMALY_05000311 || ANOMALY_05000323) {
407 AWA_DUMMY_READ(toggle);
408 hard_local_irq_restore(flags);
411 EXPORT_SYMBOL(set_gpio_toggle);
414 /*Set current PORT date (16-bit word)*/
416 #define SET_GPIO_P(name) \
417 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
419 unsigned long flags; \
420 if (ANOMALY_05000311 || ANOMALY_05000323) \
421 flags = hard_local_irq_save(); \
422 gpio_array[gpio_bank(gpio)]->name = arg; \
423 if (ANOMALY_05000311 || ANOMALY_05000323) { \
424 AWA_DUMMY_READ(name); \
425 hard_local_irq_restore(flags); \
428 EXPORT_SYMBOL(set_gpiop_ ## name);
430 SET_GPIO_P(data)
431 SET_GPIO_P(dir)
432 SET_GPIO_P(inen)
433 SET_GPIO_P(polar)
434 SET_GPIO_P(edge)
435 SET_GPIO_P(both)
436 SET_GPIO_P(maska)
437 SET_GPIO_P(maskb)
439 /* Get a specific bit */
440 #define GET_GPIO(name) \
441 unsigned short get_gpio_ ## name(unsigned gpio) \
443 unsigned long flags; \
444 unsigned short ret; \
445 if (ANOMALY_05000311 || ANOMALY_05000323) \
446 flags = hard_local_irq_save(); \
447 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
448 if (ANOMALY_05000311 || ANOMALY_05000323) { \
449 AWA_DUMMY_READ(name); \
450 hard_local_irq_restore(flags); \
452 return ret; \
454 EXPORT_SYMBOL(get_gpio_ ## name);
456 GET_GPIO(data)
457 GET_GPIO(dir)
458 GET_GPIO(inen)
459 GET_GPIO(polar)
460 GET_GPIO(edge)
461 GET_GPIO(both)
462 GET_GPIO(maska)
463 GET_GPIO(maskb)
465 /*Get current PORT date (16-bit word)*/
467 #define GET_GPIO_P(name) \
468 unsigned short get_gpiop_ ## name(unsigned gpio) \
470 unsigned long flags; \
471 unsigned short ret; \
472 if (ANOMALY_05000311 || ANOMALY_05000323) \
473 flags = hard_local_irq_save(); \
474 ret = (gpio_array[gpio_bank(gpio)]->name); \
475 if (ANOMALY_05000311 || ANOMALY_05000323) { \
476 AWA_DUMMY_READ(name); \
477 hard_local_irq_restore(flags); \
479 return ret; \
481 EXPORT_SYMBOL(get_gpiop_ ## name);
483 GET_GPIO_P(data)
484 GET_GPIO_P(dir)
485 GET_GPIO_P(inen)
486 GET_GPIO_P(polar)
487 GET_GPIO_P(edge)
488 GET_GPIO_P(both)
489 GET_GPIO_P(maska)
490 GET_GPIO_P(maskb)
493 #ifdef CONFIG_PM
494 DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
496 static const unsigned int sic_iwr_irqs[] = {
497 #if defined(BF533_FAMILY)
498 IRQ_PROG_INTB
499 #elif defined(BF537_FAMILY)
500 IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
501 #elif defined(BF538_FAMILY)
502 IRQ_PORTF_INTB
503 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
504 IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
505 #elif defined(BF561_FAMILY)
506 IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
507 #else
508 # error no SIC_IWR defined
509 #endif
512 /***********************************************************
514 * FUNCTIONS: Blackfin PM Setup API
516 * INPUTS/OUTPUTS:
517 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
518 * type -
519 * PM_WAKE_RISING
520 * PM_WAKE_FALLING
521 * PM_WAKE_HIGH
522 * PM_WAKE_LOW
523 * PM_WAKE_BOTH_EDGES
525 * DESCRIPTION: Blackfin PM Driver API
527 * CAUTION:
528 *************************************************************
529 * MODIFICATION HISTORY :
530 **************************************************************/
531 int bfin_gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
533 unsigned long flags;
535 if (check_gpio(gpio) < 0)
536 return -EINVAL;
538 flags = hard_local_irq_save();
539 if (ctrl)
540 reserve(wakeup, gpio);
541 else
542 unreserve(wakeup, gpio);
544 set_gpio_maskb(gpio, ctrl);
545 hard_local_irq_restore(flags);
547 return 0;
550 int bfin_gpio_pm_standby_ctrl(unsigned ctrl)
552 u16 bank, mask, i;
554 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
555 mask = map_entry(wakeup, i);
556 bank = gpio_bank(i);
558 if (mask)
559 bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
561 return 0;
564 void bfin_gpio_pm_hibernate_suspend(void)
566 int i, bank;
568 #ifdef BF538_FAMILY
569 for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
570 port_fer_saved[i] = *port_fer[i];
571 #endif
573 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
574 bank = gpio_bank(i);
576 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
577 gpio_bank_saved[bank].fer = *port_fer[bank];
578 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
579 gpio_bank_saved[bank].mux = *port_mux[bank];
580 #else
581 if (bank == 0)
582 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
583 #endif
584 #endif
585 gpio_bank_saved[bank].data = gpio_array[bank]->data;
586 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
587 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
588 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
589 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
590 gpio_bank_saved[bank].both = gpio_array[bank]->both;
591 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
594 #ifdef BFIN_SPECIAL_GPIO_BANKS
595 bfin_special_gpio_pm_hibernate_suspend();
596 #endif
598 AWA_DUMMY_READ(maska);
601 void bfin_gpio_pm_hibernate_restore(void)
603 int i, bank;
605 #ifdef BF538_FAMILY
606 for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
607 *port_fer[i] = port_fer_saved[i];
608 #endif
610 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
611 bank = gpio_bank(i);
613 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
614 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
615 *port_mux[bank] = gpio_bank_saved[bank].mux;
616 #else
617 if (bank == 0)
618 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
619 #endif
620 *port_fer[bank] = gpio_bank_saved[bank].fer;
621 #endif
622 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
623 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
624 & gpio_bank_saved[bank].dir;
625 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
626 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
627 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
628 gpio_array[bank]->both = gpio_bank_saved[bank].both;
629 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
632 #ifdef BFIN_SPECIAL_GPIO_BANKS
633 bfin_special_gpio_pm_hibernate_restore();
634 #endif
636 AWA_DUMMY_READ(maska);
640 #endif
642 /***********************************************************
644 * FUNCTIONS: Blackfin Peripheral Resource Allocation
645 * and PortMux Setup
647 * INPUTS/OUTPUTS:
648 * per Peripheral Identifier
649 * label String
651 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
653 * CAUTION:
654 *************************************************************
655 * MODIFICATION HISTORY :
656 **************************************************************/
658 int peripheral_request(unsigned short per, const char *label)
660 unsigned long flags;
661 unsigned short ident = P_IDENT(per);
664 * Don't cares are pins with only one dedicated function
667 if (per & P_DONTCARE)
668 return 0;
670 if (!(per & P_DEFINED))
671 return -ENODEV;
673 BUG_ON(ident >= MAX_RESOURCES);
675 flags = hard_local_irq_save();
677 /* If a pin can be muxed as either GPIO or peripheral, make
678 * sure it is not already a GPIO pin when we request it.
680 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
681 if (system_state == SYSTEM_BOOTING)
682 dump_stack();
683 printk(KERN_ERR
684 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
685 __func__, ident, get_label(ident));
686 hard_local_irq_restore(flags);
687 return -EBUSY;
690 if (unlikely(is_reserved(peri, ident, 1))) {
693 * Pin functions like AMC address strobes my
694 * be requested and used by several drivers
697 if (!(per & P_MAYSHARE)) {
699 * Allow that the identical pin function can
700 * be requested from the same driver twice
703 if (cmp_label(ident, label) == 0)
704 goto anyway;
706 if (system_state == SYSTEM_BOOTING)
707 dump_stack();
708 printk(KERN_ERR
709 "%s: Peripheral %d function %d is already reserved by %s !\n",
710 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
711 hard_local_irq_restore(flags);
712 return -EBUSY;
716 if (unlikely(portmux_group_check(per))) {
717 hard_local_irq_restore(flags);
718 return -EBUSY;
720 anyway:
721 reserve(peri, ident);
723 portmux_setup(per);
724 port_setup(ident, PERIPHERAL_USAGE);
726 hard_local_irq_restore(flags);
727 set_label(ident, label);
729 return 0;
731 EXPORT_SYMBOL(peripheral_request);
733 int peripheral_request_list(const unsigned short per[], const char *label)
735 u16 cnt;
736 int ret;
738 for (cnt = 0; per[cnt] != 0; cnt++) {
740 ret = peripheral_request(per[cnt], label);
742 if (ret < 0) {
743 for ( ; cnt > 0; cnt--)
744 peripheral_free(per[cnt - 1]);
746 return ret;
750 return 0;
752 EXPORT_SYMBOL(peripheral_request_list);
754 void peripheral_free(unsigned short per)
756 unsigned long flags;
757 unsigned short ident = P_IDENT(per);
759 if (per & P_DONTCARE)
760 return;
762 if (!(per & P_DEFINED))
763 return;
765 flags = hard_local_irq_save();
767 if (unlikely(!is_reserved(peri, ident, 0))) {
768 hard_local_irq_restore(flags);
769 return;
772 if (!(per & P_MAYSHARE))
773 port_setup(ident, GPIO_USAGE);
775 unreserve(peri, ident);
777 set_label(ident, "free");
779 hard_local_irq_restore(flags);
781 EXPORT_SYMBOL(peripheral_free);
783 void peripheral_free_list(const unsigned short per[])
785 u16 cnt;
786 for (cnt = 0; per[cnt] != 0; cnt++)
787 peripheral_free(per[cnt]);
789 EXPORT_SYMBOL(peripheral_free_list);
791 /***********************************************************
793 * FUNCTIONS: Blackfin GPIO Driver
795 * INPUTS/OUTPUTS:
796 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
797 * label String
799 * DESCRIPTION: Blackfin GPIO Driver API
801 * CAUTION:
802 *************************************************************
803 * MODIFICATION HISTORY :
804 **************************************************************/
806 int bfin_gpio_request(unsigned gpio, const char *label)
808 unsigned long flags;
810 if (check_gpio(gpio) < 0)
811 return -EINVAL;
813 flags = hard_local_irq_save();
816 * Allow that the identical GPIO can
817 * be requested from the same driver twice
818 * Do nothing and return -
821 if (cmp_label(gpio, label) == 0) {
822 hard_local_irq_restore(flags);
823 return 0;
826 if (unlikely(is_reserved(gpio, gpio, 1))) {
827 if (system_state == SYSTEM_BOOTING)
828 dump_stack();
829 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
830 gpio, get_label(gpio));
831 hard_local_irq_restore(flags);
832 return -EBUSY;
834 if (unlikely(is_reserved(peri, gpio, 1))) {
835 if (system_state == SYSTEM_BOOTING)
836 dump_stack();
837 printk(KERN_ERR
838 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
839 gpio, get_label(gpio));
840 hard_local_irq_restore(flags);
841 return -EBUSY;
843 if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
844 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
845 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
846 } else { /* Reset POLAR setting when acquiring a gpio for the first time */
847 set_gpio_polar(gpio, 0);
850 reserve(gpio, gpio);
851 set_label(gpio, label);
853 hard_local_irq_restore(flags);
855 port_setup(gpio, GPIO_USAGE);
857 return 0;
859 EXPORT_SYMBOL(bfin_gpio_request);
861 void bfin_gpio_free(unsigned gpio)
863 unsigned long flags;
865 if (check_gpio(gpio) < 0)
866 return;
868 might_sleep();
870 flags = hard_local_irq_save();
872 if (unlikely(!is_reserved(gpio, gpio, 0))) {
873 if (system_state == SYSTEM_BOOTING)
874 dump_stack();
875 gpio_error(gpio);
876 hard_local_irq_restore(flags);
877 return;
880 unreserve(gpio, gpio);
882 set_label(gpio, "free");
884 hard_local_irq_restore(flags);
886 EXPORT_SYMBOL(bfin_gpio_free);
888 #ifdef BFIN_SPECIAL_GPIO_BANKS
889 DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
891 int bfin_special_gpio_request(unsigned gpio, const char *label)
893 unsigned long flags;
895 flags = hard_local_irq_save();
898 * Allow that the identical GPIO can
899 * be requested from the same driver twice
900 * Do nothing and return -
903 if (cmp_label(gpio, label) == 0) {
904 hard_local_irq_restore(flags);
905 return 0;
908 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
909 hard_local_irq_restore(flags);
910 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
911 gpio, get_label(gpio));
913 return -EBUSY;
915 if (unlikely(is_reserved(peri, gpio, 1))) {
916 hard_local_irq_restore(flags);
917 printk(KERN_ERR
918 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
919 gpio, get_label(gpio));
921 return -EBUSY;
924 reserve(special_gpio, gpio);
925 reserve(peri, gpio);
927 set_label(gpio, label);
928 hard_local_irq_restore(flags);
929 port_setup(gpio, GPIO_USAGE);
931 return 0;
933 EXPORT_SYMBOL(bfin_special_gpio_request);
935 void bfin_special_gpio_free(unsigned gpio)
937 unsigned long flags;
939 might_sleep();
941 flags = hard_local_irq_save();
943 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
944 gpio_error(gpio);
945 hard_local_irq_restore(flags);
946 return;
949 unreserve(special_gpio, gpio);
950 unreserve(peri, gpio);
951 set_label(gpio, "free");
952 hard_local_irq_restore(flags);
954 EXPORT_SYMBOL(bfin_special_gpio_free);
955 #endif
958 int bfin_gpio_irq_request(unsigned gpio, const char *label)
960 unsigned long flags;
962 if (check_gpio(gpio) < 0)
963 return -EINVAL;
965 flags = hard_local_irq_save();
967 if (unlikely(is_reserved(peri, gpio, 1))) {
968 if (system_state == SYSTEM_BOOTING)
969 dump_stack();
970 printk(KERN_ERR
971 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
972 gpio, get_label(gpio));
973 hard_local_irq_restore(flags);
974 return -EBUSY;
976 if (unlikely(is_reserved(gpio, gpio, 1)))
977 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
978 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
979 gpio, get_label(gpio));
981 reserve(gpio_irq, gpio);
982 set_label(gpio, label);
984 hard_local_irq_restore(flags);
986 port_setup(gpio, GPIO_USAGE);
988 return 0;
991 void bfin_gpio_irq_free(unsigned gpio)
993 unsigned long flags;
995 if (check_gpio(gpio) < 0)
996 return;
998 flags = hard_local_irq_save();
1000 if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
1001 if (system_state == SYSTEM_BOOTING)
1002 dump_stack();
1003 gpio_error(gpio);
1004 hard_local_irq_restore(flags);
1005 return;
1008 unreserve(gpio_irq, gpio);
1010 set_label(gpio, "free");
1012 hard_local_irq_restore(flags);
1015 static inline void __bfin_gpio_direction_input(unsigned gpio)
1017 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1018 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1021 int bfin_gpio_direction_input(unsigned gpio)
1023 unsigned long flags;
1025 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1026 gpio_error(gpio);
1027 return -EINVAL;
1030 flags = hard_local_irq_save();
1031 __bfin_gpio_direction_input(gpio);
1032 AWA_DUMMY_READ(inen);
1033 hard_local_irq_restore(flags);
1035 return 0;
1037 EXPORT_SYMBOL(bfin_gpio_direction_input);
1039 void bfin_gpio_irq_prepare(unsigned gpio)
1041 port_setup(gpio, GPIO_USAGE);
1044 void bfin_gpio_set_value(unsigned gpio, int arg)
1046 if (arg)
1047 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1048 else
1049 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1051 EXPORT_SYMBOL(bfin_gpio_set_value);
1053 int bfin_gpio_direction_output(unsigned gpio, int value)
1055 unsigned long flags;
1057 if (unlikely(!is_reserved(gpio, gpio, 0))) {
1058 gpio_error(gpio);
1059 return -EINVAL;
1062 flags = hard_local_irq_save();
1064 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1065 gpio_set_value(gpio, value);
1066 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1068 AWA_DUMMY_READ(dir);
1069 hard_local_irq_restore(flags);
1071 return 0;
1073 EXPORT_SYMBOL(bfin_gpio_direction_output);
1075 int bfin_gpio_get_value(unsigned gpio)
1077 unsigned long flags;
1079 if (unlikely(get_gpio_edge(gpio))) {
1080 int ret;
1081 flags = hard_local_irq_save();
1082 set_gpio_edge(gpio, 0);
1083 ret = get_gpio_data(gpio);
1084 set_gpio_edge(gpio, 1);
1085 hard_local_irq_restore(flags);
1086 return ret;
1087 } else
1088 return get_gpio_data(gpio);
1090 EXPORT_SYMBOL(bfin_gpio_get_value);
1092 /* If we are booting from SPI and our board lacks a strong enough pull up,
1093 * the core can reset and execute the bootrom faster than the resistor can
1094 * pull the signal logically high. To work around this (common) error in
1095 * board design, we explicitly set the pin back to GPIO mode, force /CS
1096 * high, and wait for the electrons to do their thing.
1098 * This function only makes sense to be called from reset code, but it
1099 * lives here as we need to force all the GPIO states w/out going through
1100 * BUG() checks and such.
1102 void bfin_reset_boot_spi_cs(unsigned short pin)
1104 unsigned short gpio = P_IDENT(pin);
1105 port_setup(gpio, GPIO_USAGE);
1106 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1107 AWA_DUMMY_READ(data_set);
1108 udelay(1);
1111 #if defined(CONFIG_PROC_FS)
1112 static int gpio_proc_show(struct seq_file *m, void *v)
1114 int c, irq, gpio;
1116 for (c = 0; c < MAX_RESOURCES; c++) {
1117 irq = is_reserved(gpio_irq, c, 1);
1118 gpio = is_reserved(gpio, c, 1);
1119 if (!check_gpio(c) && (gpio || irq))
1120 seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1121 get_label(c), (gpio && irq) ? " *" : "",
1122 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1123 else if (is_reserved(peri, c, 1))
1124 seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1125 else
1126 continue;
1129 return 0;
1132 static int gpio_proc_open(struct inode *inode, struct file *file)
1134 return single_open(file, gpio_proc_show, NULL);
1137 static const struct file_operations gpio_proc_ops = {
1138 .open = gpio_proc_open,
1139 .read = seq_read,
1140 .llseek = seq_lseek,
1141 .release = single_release,
1144 static __init int gpio_register_proc(void)
1146 struct proc_dir_entry *proc_gpio;
1148 proc_gpio = proc_create("gpio", 0, NULL, &gpio_proc_ops);
1149 return proc_gpio == NULL;
1151 __initcall(gpio_register_proc);
1152 #endif
1154 #ifdef CONFIG_GPIOLIB
1155 static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1157 return bfin_gpio_direction_input(gpio);
1160 static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1162 return bfin_gpio_direction_output(gpio, level);
1165 static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1167 return !!bfin_gpio_get_value(gpio);
1170 static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1172 return bfin_gpio_set_value(gpio, value);
1175 static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1177 return bfin_gpio_request(gpio, chip->label);
1180 static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1182 return bfin_gpio_free(gpio);
1185 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1187 return gpio + GPIO_IRQ_BASE;
1190 static struct gpio_chip bfin_chip = {
1191 .label = "BFIN-GPIO",
1192 .direction_input = bfin_gpiolib_direction_input,
1193 .get = bfin_gpiolib_get_value,
1194 .direction_output = bfin_gpiolib_direction_output,
1195 .set = bfin_gpiolib_set_value,
1196 .request = bfin_gpiolib_gpio_request,
1197 .free = bfin_gpiolib_gpio_free,
1198 .to_irq = bfin_gpiolib_gpio_to_irq,
1199 .base = 0,
1200 .ngpio = MAX_BLACKFIN_GPIOS,
1203 static int __init bfin_gpiolib_setup(void)
1205 return gpiochip_add_data(&bfin_chip, NULL);
1207 arch_initcall(bfin_gpiolib_setup);
1208 #endif