per-zone and reclaim enhancements for memory controller: per zone lru for cgroup
[linux-2.6/linux-loongson.git] / arch / blackfin / kernel / bfin_gpio.c
blob6bbe0a2fccb8f14c7774d4198377b64bc761d111
1 /*
2 * File: arch/blackfin/kernel/bfin_gpio.c
3 * Based on:
4 * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
6 * Created:
7 * Description: GPIO Abstraction Layer
9 * Modified:
10 * Copyright 2008 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 * Number BF537/6/4 BF561 BF533/2/1 BF549/8/4/2
33 * GPIO_0 PF0 PF0 PF0 PA0...PJ13
34 * GPIO_1 PF1 PF1 PF1
35 * GPIO_2 PF2 PF2 PF2
36 * GPIO_3 PF3 PF3 PF3
37 * GPIO_4 PF4 PF4 PF4
38 * GPIO_5 PF5 PF5 PF5
39 * GPIO_6 PF6 PF6 PF6
40 * GPIO_7 PF7 PF7 PF7
41 * GPIO_8 PF8 PF8 PF8
42 * GPIO_9 PF9 PF9 PF9
43 * GPIO_10 PF10 PF10 PF10
44 * GPIO_11 PF11 PF11 PF11
45 * GPIO_12 PF12 PF12 PF12
46 * GPIO_13 PF13 PF13 PF13
47 * GPIO_14 PF14 PF14 PF14
48 * GPIO_15 PF15 PF15 PF15
49 * GPIO_16 PG0 PF16
50 * GPIO_17 PG1 PF17
51 * GPIO_18 PG2 PF18
52 * GPIO_19 PG3 PF19
53 * GPIO_20 PG4 PF20
54 * GPIO_21 PG5 PF21
55 * GPIO_22 PG6 PF22
56 * GPIO_23 PG7 PF23
57 * GPIO_24 PG8 PF24
58 * GPIO_25 PG9 PF25
59 * GPIO_26 PG10 PF26
60 * GPIO_27 PG11 PF27
61 * GPIO_28 PG12 PF28
62 * GPIO_29 PG13 PF29
63 * GPIO_30 PG14 PF30
64 * GPIO_31 PG15 PF31
65 * GPIO_32 PH0 PF32
66 * GPIO_33 PH1 PF33
67 * GPIO_34 PH2 PF34
68 * GPIO_35 PH3 PF35
69 * GPIO_36 PH4 PF36
70 * GPIO_37 PH5 PF37
71 * GPIO_38 PH6 PF38
72 * GPIO_39 PH7 PF39
73 * GPIO_40 PH8 PF40
74 * GPIO_41 PH9 PF41
75 * GPIO_42 PH10 PF42
76 * GPIO_43 PH11 PF43
77 * GPIO_44 PH12 PF44
78 * GPIO_45 PH13 PF45
79 * GPIO_46 PH14 PF46
80 * GPIO_47 PH15 PF47
83 #include <linux/delay.h>
84 #include <linux/module.h>
85 #include <linux/err.h>
86 #include <linux/proc_fs.h>
87 #include <asm/blackfin.h>
88 #include <asm/gpio.h>
89 #include <asm/portmux.h>
90 #include <linux/irq.h>
92 #if ANOMALY_05000311 || ANOMALY_05000323
93 enum {
94 AWA_data = SYSCR,
95 AWA_data_clear = SYSCR,
96 AWA_data_set = SYSCR,
97 AWA_toggle = SYSCR,
98 AWA_maska = UART_SCR,
99 AWA_maska_clear = UART_SCR,
100 AWA_maska_set = UART_SCR,
101 AWA_maska_toggle = UART_SCR,
102 AWA_maskb = UART_GCTL,
103 AWA_maskb_clear = UART_GCTL,
104 AWA_maskb_set = UART_GCTL,
105 AWA_maskb_toggle = UART_GCTL,
106 AWA_dir = SPORT1_STAT,
107 AWA_polar = SPORT1_STAT,
108 AWA_edge = SPORT1_STAT,
109 AWA_both = SPORT1_STAT,
110 #if ANOMALY_05000311
111 AWA_inen = TIMER_ENABLE,
112 #elif ANOMALY_05000323
113 AWA_inen = DMA1_1_CONFIG,
114 #endif
116 /* Anomaly Workaround */
117 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
118 #else
119 #define AWA_DUMMY_READ(...) do { } while (0)
120 #endif
122 #ifdef BF533_FAMILY
123 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
124 (struct gpio_port_t *) FIO_FLAG_D,
126 #endif
128 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
129 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
130 (struct gpio_port_t *) PORTFIO,
131 (struct gpio_port_t *) PORTGIO,
132 (struct gpio_port_t *) PORTHIO,
135 static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
136 (unsigned short *) PORTF_FER,
137 (unsigned short *) PORTG_FER,
138 (unsigned short *) PORTH_FER,
140 #endif
142 #ifdef BF527_FAMILY
143 static unsigned short *port_mux[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
144 (unsigned short *) PORTF_MUX,
145 (unsigned short *) PORTG_MUX,
146 (unsigned short *) PORTH_MUX,
149 static const
150 u8 pmux_offset[][16] =
151 {{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
152 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
153 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
155 #endif
157 #ifdef BF561_FAMILY
158 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
159 (struct gpio_port_t *) FIO0_FLAG_D,
160 (struct gpio_port_t *) FIO1_FLAG_D,
161 (struct gpio_port_t *) FIO2_FLAG_D,
163 #endif
165 #ifdef BF548_FAMILY
166 static struct gpio_port_t *gpio_array[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
167 (struct gpio_port_t *)PORTA_FER,
168 (struct gpio_port_t *)PORTB_FER,
169 (struct gpio_port_t *)PORTC_FER,
170 (struct gpio_port_t *)PORTD_FER,
171 (struct gpio_port_t *)PORTE_FER,
172 (struct gpio_port_t *)PORTF_FER,
173 (struct gpio_port_t *)PORTG_FER,
174 (struct gpio_port_t *)PORTH_FER,
175 (struct gpio_port_t *)PORTI_FER,
176 (struct gpio_port_t *)PORTJ_FER,
178 #endif
180 static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
181 static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
183 #define RESOURCE_LABEL_SIZE 16
185 static struct str_ident {
186 char name[RESOURCE_LABEL_SIZE];
187 } str_ident[MAX_RESOURCES];
189 #ifdef CONFIG_PM
190 static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
191 static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
192 static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
194 #ifdef BF533_FAMILY
195 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
196 #endif
198 #ifdef BF537_FAMILY
199 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
200 #endif
202 #ifdef BF527_FAMILY
203 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
204 #endif
206 #ifdef BF561_FAMILY
207 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
208 #endif
210 #endif /* CONFIG_PM */
212 #if defined(BF548_FAMILY)
213 inline int check_gpio(unsigned gpio)
215 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
216 || gpio == GPIO_PH14 || gpio == GPIO_PH15
217 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15
218 || gpio > MAX_BLACKFIN_GPIOS)
219 return -EINVAL;
220 return 0;
222 #else
223 inline int check_gpio(unsigned gpio)
225 if (gpio >= MAX_BLACKFIN_GPIOS)
226 return -EINVAL;
227 return 0;
229 #endif
231 void gpio_error(unsigned gpio)
233 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
236 static void set_label(unsigned short ident, const char *label)
238 if (label && str_ident) {
239 strncpy(str_ident[ident].name, label,
240 RESOURCE_LABEL_SIZE);
241 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
245 static char *get_label(unsigned short ident)
247 if (!str_ident)
248 return "UNKNOWN";
250 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
253 static int cmp_label(unsigned short ident, const char *label)
255 if (label == NULL) {
256 dump_stack();
257 printk(KERN_ERR "Please provide none-null label\n");
260 if (label && str_ident)
261 return strncmp(str_ident[ident].name,
262 label, strlen(label));
263 else
264 return -EINVAL;
267 #if defined(BF527_FAMILY) || defined(BF537_FAMILY)
268 static void port_setup(unsigned gpio, unsigned short usage)
270 if (!check_gpio(gpio)) {
271 if (usage == GPIO_USAGE)
272 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
273 else
274 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
275 SSYNC();
278 #elif defined(BF548_FAMILY)
279 static void port_setup(unsigned gpio, unsigned short usage)
281 if (usage == GPIO_USAGE)
282 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
283 else
284 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
285 SSYNC();
287 #else
288 # define port_setup(...) do { } while (0)
289 #endif
291 #ifdef BF537_FAMILY
292 static struct {
293 unsigned short res;
294 unsigned short offset;
295 } port_mux_lut[] = {
296 {.res = P_PPI0_D13, .offset = 11},
297 {.res = P_PPI0_D14, .offset = 11},
298 {.res = P_PPI0_D15, .offset = 11},
299 {.res = P_SPORT1_TFS, .offset = 11},
300 {.res = P_SPORT1_TSCLK, .offset = 11},
301 {.res = P_SPORT1_DTPRI, .offset = 11},
302 {.res = P_PPI0_D10, .offset = 10},
303 {.res = P_PPI0_D11, .offset = 10},
304 {.res = P_PPI0_D12, .offset = 10},
305 {.res = P_SPORT1_RSCLK, .offset = 10},
306 {.res = P_SPORT1_RFS, .offset = 10},
307 {.res = P_SPORT1_DRPRI, .offset = 10},
308 {.res = P_PPI0_D8, .offset = 9},
309 {.res = P_PPI0_D9, .offset = 9},
310 {.res = P_SPORT1_DRSEC, .offset = 9},
311 {.res = P_SPORT1_DTSEC, .offset = 9},
312 {.res = P_TMR2, .offset = 8},
313 {.res = P_PPI0_FS3, .offset = 8},
314 {.res = P_TMR3, .offset = 7},
315 {.res = P_SPI0_SSEL4, .offset = 7},
316 {.res = P_TMR4, .offset = 6},
317 {.res = P_SPI0_SSEL5, .offset = 6},
318 {.res = P_TMR5, .offset = 5},
319 {.res = P_SPI0_SSEL6, .offset = 5},
320 {.res = P_UART1_RX, .offset = 4},
321 {.res = P_UART1_TX, .offset = 4},
322 {.res = P_TMR6, .offset = 4},
323 {.res = P_TMR7, .offset = 4},
324 {.res = P_UART0_RX, .offset = 3},
325 {.res = P_UART0_TX, .offset = 3},
326 {.res = P_DMAR0, .offset = 3},
327 {.res = P_DMAR1, .offset = 3},
328 {.res = P_SPORT0_DTSEC, .offset = 1},
329 {.res = P_SPORT0_DRSEC, .offset = 1},
330 {.res = P_CAN0_RX, .offset = 1},
331 {.res = P_CAN0_TX, .offset = 1},
332 {.res = P_SPI0_SSEL7, .offset = 1},
333 {.res = P_SPORT0_TFS, .offset = 0},
334 {.res = P_SPORT0_DTPRI, .offset = 0},
335 {.res = P_SPI0_SSEL2, .offset = 0},
336 {.res = P_SPI0_SSEL3, .offset = 0},
339 static void portmux_setup(unsigned short per, unsigned short function)
341 u16 y, offset, muxreg;
343 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
344 if (port_mux_lut[y].res == per) {
346 /* SET PORTMUX REG */
348 offset = port_mux_lut[y].offset;
349 muxreg = bfin_read_PORT_MUX();
351 if (offset != 1) {
352 muxreg &= ~(1 << offset);
353 } else {
354 muxreg &= ~(3 << 1);
357 muxreg |= (function << offset);
358 bfin_write_PORT_MUX(muxreg);
362 #elif defined(BF548_FAMILY)
363 inline void portmux_setup(unsigned short portno, unsigned short function)
365 u32 pmux;
367 pmux = gpio_array[gpio_bank(portno)]->port_mux;
369 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
370 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
372 gpio_array[gpio_bank(portno)]->port_mux = pmux;
375 inline u16 get_portmux(unsigned short portno)
377 u32 pmux;
379 pmux = gpio_array[gpio_bank(portno)]->port_mux;
381 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
383 #elif defined(BF527_FAMILY)
384 inline void portmux_setup(unsigned short portno, unsigned short function)
386 u16 pmux, ident = P_IDENT(portno);
387 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
389 pmux = *port_mux[gpio_bank(ident)];
390 pmux &= ~(3 << offset);
391 pmux |= (function & 3) << offset;
392 *port_mux[gpio_bank(ident)] = pmux;
393 SSYNC();
395 #else
396 # define portmux_setup(...) do { } while (0)
397 #endif
399 #ifndef BF548_FAMILY
400 static void default_gpio(unsigned gpio)
402 unsigned short bank, bitmask;
403 unsigned long flags;
405 bank = gpio_bank(gpio);
406 bitmask = gpio_bit(gpio);
408 local_irq_save(flags);
410 gpio_bankb[bank]->maska_clear = bitmask;
411 gpio_bankb[bank]->maskb_clear = bitmask;
412 SSYNC();
413 gpio_bankb[bank]->inen &= ~bitmask;
414 gpio_bankb[bank]->dir &= ~bitmask;
415 gpio_bankb[bank]->polar &= ~bitmask;
416 gpio_bankb[bank]->both &= ~bitmask;
417 gpio_bankb[bank]->edge &= ~bitmask;
418 AWA_DUMMY_READ(edge);
419 local_irq_restore(flags);
421 #else
422 # define default_gpio(...) do { } while (0)
423 #endif
425 static int __init bfin_gpio_init(void)
428 printk(KERN_INFO "Blackfin GPIO Controller\n");
430 return 0;
433 arch_initcall(bfin_gpio_init);
436 #ifndef BF548_FAMILY
437 /***********************************************************
439 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
441 * INPUTS/OUTPUTS:
442 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
445 * DESCRIPTION: These functions abstract direct register access
446 * to Blackfin processor General Purpose
447 * Ports Regsiters
449 * CAUTION: These functions do not belong to the GPIO Driver API
450 *************************************************************
451 * MODIFICATION HISTORY :
452 **************************************************************/
454 /* Set a specific bit */
456 #define SET_GPIO(name) \
457 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
459 unsigned long flags; \
460 local_irq_save(flags); \
461 if (arg) \
462 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
463 else \
464 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
465 AWA_DUMMY_READ(name); \
466 local_irq_restore(flags); \
468 EXPORT_SYMBOL(set_gpio_ ## name);
470 SET_GPIO(dir)
471 SET_GPIO(inen)
472 SET_GPIO(polar)
473 SET_GPIO(edge)
474 SET_GPIO(both)
477 #if ANOMALY_05000311 || ANOMALY_05000323
478 #define SET_GPIO_SC(name) \
479 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
481 unsigned long flags; \
482 local_irq_save(flags); \
483 if (arg) \
484 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
485 else \
486 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
487 AWA_DUMMY_READ(name); \
488 local_irq_restore(flags); \
490 EXPORT_SYMBOL(set_gpio_ ## name);
491 #else
492 #define SET_GPIO_SC(name) \
493 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
495 if (arg) \
496 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
497 else \
498 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
500 EXPORT_SYMBOL(set_gpio_ ## name);
501 #endif
503 SET_GPIO_SC(maska)
504 SET_GPIO_SC(maskb)
505 SET_GPIO_SC(data)
507 #if ANOMALY_05000311 || ANOMALY_05000323
508 void set_gpio_toggle(unsigned gpio)
510 unsigned long flags;
511 local_irq_save(flags);
512 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
513 AWA_DUMMY_READ(toggle);
514 local_irq_restore(flags);
516 #else
517 void set_gpio_toggle(unsigned gpio)
519 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
521 #endif
522 EXPORT_SYMBOL(set_gpio_toggle);
525 /*Set current PORT date (16-bit word)*/
527 #if ANOMALY_05000311 || ANOMALY_05000323
528 #define SET_GPIO_P(name) \
529 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
531 unsigned long flags; \
532 local_irq_save(flags); \
533 gpio_bankb[gpio_bank(gpio)]->name = arg; \
534 AWA_DUMMY_READ(name); \
535 local_irq_restore(flags); \
537 EXPORT_SYMBOL(set_gpiop_ ## name);
538 #else
539 #define SET_GPIO_P(name) \
540 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
542 gpio_bankb[gpio_bank(gpio)]->name = arg; \
544 EXPORT_SYMBOL(set_gpiop_ ## name);
545 #endif
547 SET_GPIO_P(data)
548 SET_GPIO_P(dir)
549 SET_GPIO_P(inen)
550 SET_GPIO_P(polar)
551 SET_GPIO_P(edge)
552 SET_GPIO_P(both)
553 SET_GPIO_P(maska)
554 SET_GPIO_P(maskb)
556 /* Get a specific bit */
557 #if ANOMALY_05000311 || ANOMALY_05000323
558 #define GET_GPIO(name) \
559 unsigned short get_gpio_ ## name(unsigned gpio) \
561 unsigned long flags; \
562 unsigned short ret; \
563 local_irq_save(flags); \
564 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
565 AWA_DUMMY_READ(name); \
566 local_irq_restore(flags); \
567 return ret; \
569 EXPORT_SYMBOL(get_gpio_ ## name);
570 #else
571 #define GET_GPIO(name) \
572 unsigned short get_gpio_ ## name(unsigned gpio) \
574 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
576 EXPORT_SYMBOL(get_gpio_ ## name);
577 #endif
579 GET_GPIO(data)
580 GET_GPIO(dir)
581 GET_GPIO(inen)
582 GET_GPIO(polar)
583 GET_GPIO(edge)
584 GET_GPIO(both)
585 GET_GPIO(maska)
586 GET_GPIO(maskb)
588 /*Get current PORT date (16-bit word)*/
590 #if ANOMALY_05000311 || ANOMALY_05000323
591 #define GET_GPIO_P(name) \
592 unsigned short get_gpiop_ ## name(unsigned gpio) \
594 unsigned long flags; \
595 unsigned short ret; \
596 local_irq_save(flags); \
597 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
598 AWA_DUMMY_READ(name); \
599 local_irq_restore(flags); \
600 return ret; \
602 EXPORT_SYMBOL(get_gpiop_ ## name);
603 #else
604 #define GET_GPIO_P(name) \
605 unsigned short get_gpiop_ ## name(unsigned gpio) \
607 return (gpio_bankb[gpio_bank(gpio)]->name);\
609 EXPORT_SYMBOL(get_gpiop_ ## name);
610 #endif
612 GET_GPIO_P(data)
613 GET_GPIO_P(dir)
614 GET_GPIO_P(inen)
615 GET_GPIO_P(polar)
616 GET_GPIO_P(edge)
617 GET_GPIO_P(both)
618 GET_GPIO_P(maska)
619 GET_GPIO_P(maskb)
622 #ifdef CONFIG_PM
623 /***********************************************************
625 * FUNCTIONS: Blackfin PM Setup API
627 * INPUTS/OUTPUTS:
628 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
629 * type -
630 * PM_WAKE_RISING
631 * PM_WAKE_FALLING
632 * PM_WAKE_HIGH
633 * PM_WAKE_LOW
634 * PM_WAKE_BOTH_EDGES
636 * DESCRIPTION: Blackfin PM Driver API
638 * CAUTION:
639 *************************************************************
640 * MODIFICATION HISTORY :
641 **************************************************************/
642 int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
644 unsigned long flags;
646 if ((check_gpio(gpio) < 0) || !type)
647 return -EINVAL;
649 local_irq_save(flags);
650 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
651 wakeup_flags_map[gpio] = type;
652 local_irq_restore(flags);
654 return 0;
656 EXPORT_SYMBOL(gpio_pm_wakeup_request);
658 void gpio_pm_wakeup_free(unsigned gpio)
660 unsigned long flags;
662 if (check_gpio(gpio) < 0)
663 return;
665 local_irq_save(flags);
667 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
669 local_irq_restore(flags);
671 EXPORT_SYMBOL(gpio_pm_wakeup_free);
673 static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
675 port_setup(gpio, GPIO_USAGE);
676 set_gpio_dir(gpio, 0);
677 set_gpio_inen(gpio, 1);
679 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
680 set_gpio_edge(gpio, 1);
681 else
682 set_gpio_edge(gpio, 0);
684 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
685 set_gpio_both(gpio, 1);
686 else
687 set_gpio_both(gpio, 0);
689 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
690 set_gpio_polar(gpio, 1);
691 else
692 set_gpio_polar(gpio, 0);
694 SSYNC();
696 return 0;
699 u32 gpio_pm_setup(void)
701 u32 sic_iwr = 0;
702 u16 bank, mask, i, gpio;
704 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
705 mask = wakeup_map[gpio_bank(i)];
706 bank = gpio_bank(i);
708 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
709 gpio_bankb[bank]->maskb = 0;
711 if (mask) {
712 #ifdef BF537_FAMILY
713 gpio_bank_saved[bank].fer = *port_fer[bank];
714 #endif
715 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
716 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
717 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
718 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
719 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
720 gpio_bank_saved[bank].reserved =
721 reserved_gpio_map[bank];
723 gpio = i;
725 while (mask) {
726 if (mask & 1) {
727 reserved_gpio_map[gpio_bank(gpio)] |=
728 gpio_bit(gpio);
729 bfin_gpio_wakeup_type(gpio,
730 wakeup_flags_map[gpio]);
731 set_gpio_data(gpio, 0); /*Clear*/
733 gpio++;
734 mask >>= 1;
737 sic_iwr |= 1 <<
738 (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
739 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
743 AWA_DUMMY_READ(maskb_set);
745 if (sic_iwr)
746 return sic_iwr;
747 else
748 return IWR_ENABLE_ALL;
751 void gpio_pm_restore(void)
753 u16 bank, mask, i;
755 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
756 mask = wakeup_map[gpio_bank(i)];
757 bank = gpio_bank(i);
759 if (mask) {
760 #ifdef BF537_FAMILY
761 *port_fer[bank] = gpio_bank_saved[bank].fer;
762 #endif
763 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
764 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
765 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
766 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
767 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
769 reserved_gpio_map[bank] =
770 gpio_bank_saved[bank].reserved;
774 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
776 AWA_DUMMY_READ(maskb);
779 #endif
780 #else /* BF548_FAMILY */
782 unsigned short get_gpio_dir(unsigned gpio)
784 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
786 EXPORT_SYMBOL(get_gpio_dir);
788 #endif /* BF548_FAMILY */
790 /***********************************************************
792 * FUNCTIONS: Blackfin Peripheral Resource Allocation
793 * and PortMux Setup
795 * INPUTS/OUTPUTS:
796 * per Peripheral Identifier
797 * label String
799 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
801 * CAUTION:
802 *************************************************************
803 * MODIFICATION HISTORY :
804 **************************************************************/
806 #ifdef BF548_FAMILY
807 int peripheral_request(unsigned short per, const char *label)
809 unsigned long flags;
810 unsigned short ident = P_IDENT(per);
813 * Don't cares are pins with only one dedicated function
816 if (per & P_DONTCARE)
817 return 0;
819 if (!(per & P_DEFINED))
820 return -ENODEV;
822 if (check_gpio(ident) < 0)
823 return -EINVAL;
825 local_irq_save(flags);
827 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
828 printk(KERN_ERR
829 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
830 __FUNCTION__, ident, get_label(ident));
831 dump_stack();
832 local_irq_restore(flags);
833 return -EBUSY;
836 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
838 u16 funct = get_portmux(ident);
841 * Pin functions like AMC address strobes my
842 * be requested and used by several drivers
845 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
848 * Allow that the identical pin function can
849 * be requested from the same driver twice
852 if (cmp_label(ident, label) == 0)
853 goto anyway;
855 printk(KERN_ERR
856 "%s: Peripheral %d function %d is already reserved by %s !\n",
857 __FUNCTION__, ident, P_FUNCT2MUX(per), get_label(ident));
858 dump_stack();
859 local_irq_restore(flags);
860 return -EBUSY;
864 anyway:
865 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
867 portmux_setup(ident, P_FUNCT2MUX(per));
868 port_setup(ident, PERIPHERAL_USAGE);
870 local_irq_restore(flags);
871 set_label(ident, label);
873 return 0;
875 EXPORT_SYMBOL(peripheral_request);
876 #else
878 int peripheral_request(unsigned short per, const char *label)
880 unsigned long flags;
881 unsigned short ident = P_IDENT(per);
884 * Don't cares are pins with only one dedicated function
887 if (per & P_DONTCARE)
888 return 0;
890 if (!(per & P_DEFINED))
891 return -ENODEV;
893 local_irq_save(flags);
895 if (!check_gpio(ident)) {
897 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
898 printk(KERN_ERR
899 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
900 __FUNCTION__, ident, get_label(ident));
901 dump_stack();
902 local_irq_restore(flags);
903 return -EBUSY;
908 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
911 * Pin functions like AMC address strobes my
912 * be requested and used by several drivers
915 if (!(per & P_MAYSHARE)) {
918 * Allow that the identical pin function can
919 * be requested from the same driver twice
922 if (cmp_label(ident, label) == 0)
923 goto anyway;
925 printk(KERN_ERR
926 "%s: Peripheral %d function %d is already"
927 " reserved by %s !\n",
928 __FUNCTION__, ident, P_FUNCT2MUX(per),
929 get_label(ident));
930 dump_stack();
931 local_irq_restore(flags);
932 return -EBUSY;
937 anyway:
938 portmux_setup(per, P_FUNCT2MUX(per));
940 port_setup(ident, PERIPHERAL_USAGE);
942 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
943 local_irq_restore(flags);
944 set_label(ident, label);
946 return 0;
948 EXPORT_SYMBOL(peripheral_request);
949 #endif
951 int peripheral_request_list(unsigned short per[], const char *label)
953 u16 cnt;
954 int ret;
956 for (cnt = 0; per[cnt] != 0; cnt++) {
958 ret = peripheral_request(per[cnt], label);
960 if (ret < 0) {
961 for ( ; cnt > 0; cnt--) {
962 peripheral_free(per[cnt - 1]);
964 return ret;
968 return 0;
970 EXPORT_SYMBOL(peripheral_request_list);
972 void peripheral_free(unsigned short per)
974 unsigned long flags;
975 unsigned short ident = P_IDENT(per);
977 if (per & P_DONTCARE)
978 return;
980 if (!(per & P_DEFINED))
981 return;
983 if (check_gpio(ident) < 0)
984 return;
986 local_irq_save(flags);
988 if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
989 & gpio_bit(ident)))) {
990 local_irq_restore(flags);
991 return;
994 if (!(per & P_MAYSHARE)) {
995 port_setup(ident, GPIO_USAGE);
998 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
1000 set_label(ident, "free");
1002 local_irq_restore(flags);
1004 EXPORT_SYMBOL(peripheral_free);
1006 void peripheral_free_list(unsigned short per[])
1008 u16 cnt;
1010 for (cnt = 0; per[cnt] != 0; cnt++) {
1011 peripheral_free(per[cnt]);
1015 EXPORT_SYMBOL(peripheral_free_list);
1017 /***********************************************************
1019 * FUNCTIONS: Blackfin GPIO Driver
1021 * INPUTS/OUTPUTS:
1022 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1023 * label String
1025 * DESCRIPTION: Blackfin GPIO Driver API
1027 * CAUTION:
1028 *************************************************************
1029 * MODIFICATION HISTORY :
1030 **************************************************************/
1032 int gpio_request(unsigned gpio, const char *label)
1034 unsigned long flags;
1036 if (check_gpio(gpio) < 0)
1037 return -EINVAL;
1039 local_irq_save(flags);
1042 * Allow that the identical GPIO can
1043 * be requested from the same driver twice
1044 * Do nothing and return -
1047 if (cmp_label(gpio, label) == 0) {
1048 local_irq_restore(flags);
1049 return 0;
1052 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1053 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1054 gpio, get_label(gpio));
1055 dump_stack();
1056 local_irq_restore(flags);
1057 return -EBUSY;
1059 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1060 printk(KERN_ERR
1061 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1062 gpio, get_label(gpio));
1063 dump_stack();
1064 local_irq_restore(flags);
1065 return -EBUSY;
1068 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
1070 local_irq_restore(flags);
1072 port_setup(gpio, GPIO_USAGE);
1073 set_label(gpio, label);
1075 return 0;
1077 EXPORT_SYMBOL(gpio_request);
1079 void gpio_free(unsigned gpio)
1081 unsigned long flags;
1083 if (check_gpio(gpio) < 0)
1084 return;
1086 local_irq_save(flags);
1088 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1089 gpio_error(gpio);
1090 dump_stack();
1091 local_irq_restore(flags);
1092 return;
1095 default_gpio(gpio);
1097 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1099 set_label(gpio, "free");
1101 local_irq_restore(flags);
1103 EXPORT_SYMBOL(gpio_free);
1106 #ifdef BF548_FAMILY
1107 int gpio_direction_input(unsigned gpio)
1109 unsigned long flags;
1111 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1112 gpio_error(gpio);
1113 return -EINVAL;
1116 local_irq_save(flags);
1117 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1118 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1119 local_irq_restore(flags);
1121 return 0;
1123 EXPORT_SYMBOL(gpio_direction_input);
1125 int gpio_direction_output(unsigned gpio, int value)
1127 unsigned long flags;
1129 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1130 gpio_error(gpio);
1131 return -EINVAL;
1134 local_irq_save(flags);
1135 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
1136 gpio_set_value(gpio, value);
1137 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1138 local_irq_restore(flags);
1140 return 0;
1142 EXPORT_SYMBOL(gpio_direction_output);
1144 void gpio_set_value(unsigned gpio, int arg)
1146 if (arg)
1147 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1148 else
1149 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
1151 EXPORT_SYMBOL(gpio_set_value);
1153 int gpio_get_value(unsigned gpio)
1155 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1157 EXPORT_SYMBOL(gpio_get_value);
1159 #else
1161 int gpio_direction_input(unsigned gpio)
1163 unsigned long flags;
1165 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1166 gpio_error(gpio);
1167 return -EINVAL;
1170 local_irq_save(flags);
1171 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1172 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1173 AWA_DUMMY_READ(inen);
1174 local_irq_restore(flags);
1176 return 0;
1178 EXPORT_SYMBOL(gpio_direction_input);
1180 int gpio_direction_output(unsigned gpio, int value)
1182 unsigned long flags;
1184 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1185 gpio_error(gpio);
1186 return -EINVAL;
1189 local_irq_save(flags);
1190 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1192 if (value)
1193 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1194 else
1195 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1197 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1198 AWA_DUMMY_READ(dir);
1199 local_irq_restore(flags);
1201 return 0;
1203 EXPORT_SYMBOL(gpio_direction_output);
1205 /* If we are booting from SPI and our board lacks a strong enough pull up,
1206 * the core can reset and execute the bootrom faster than the resistor can
1207 * pull the signal logically high. To work around this (common) error in
1208 * board design, we explicitly set the pin back to GPIO mode, force /CS
1209 * high, and wait for the electrons to do their thing.
1211 * This function only makes sense to be called from reset code, but it
1212 * lives here as we need to force all the GPIO states w/out going through
1213 * BUG() checks and such.
1215 void bfin_gpio_reset_spi0_ssel1(void)
1217 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1219 port_setup(gpio, GPIO_USAGE);
1220 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1221 AWA_DUMMY_READ(data_set);
1222 udelay(1);
1225 #endif /*BF548_FAMILY */
1227 #if defined(CONFIG_PROC_FS)
1228 static int gpio_proc_read(char *buf, char **start, off_t offset,
1229 int len, int *unused_i, void *unused_v)
1231 int c, outlen = 0;
1233 for (c = 0; c < MAX_RESOURCES; c++) {
1234 if (!check_gpio(c) && (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c)))
1235 len = sprintf(buf, "GPIO_%d: %s \t\tGPIO %s\n", c,
1236 get_label(c), get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1237 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
1238 len = sprintf(buf, "GPIO_%d: %s \t\tPeripheral\n", c, get_label(c));
1239 else
1240 continue;
1241 buf += len;
1242 outlen += len;
1244 return outlen;
1247 static __init int gpio_register_proc(void)
1249 struct proc_dir_entry *proc_gpio;
1251 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1252 if (proc_gpio)
1253 proc_gpio->read_proc = gpio_proc_read;
1254 return proc_gpio != NULL;
1256 __initcall(gpio_register_proc);
1257 #endif