mm-only debug patch...
[mmotm.git] / arch / blackfin / kernel / bfin_gpio.c
blob22705eeff34f5b64c4f510cc4a781a18d6afc0f8
1 /*
2 * GPIO Abstraction Layer
4 * Copyright 2006-2009 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 <asm/blackfin.h>
14 #include <asm/gpio.h>
15 #include <asm/portmux.h>
16 #include <linux/irq.h>
18 #if ANOMALY_05000311 || ANOMALY_05000323
19 enum {
20 AWA_data = SYSCR,
21 AWA_data_clear = SYSCR,
22 AWA_data_set = SYSCR,
23 AWA_toggle = SYSCR,
24 AWA_maska = BFIN_UART_SCR,
25 AWA_maska_clear = BFIN_UART_SCR,
26 AWA_maska_set = BFIN_UART_SCR,
27 AWA_maska_toggle = BFIN_UART_SCR,
28 AWA_maskb = BFIN_UART_GCTL,
29 AWA_maskb_clear = BFIN_UART_GCTL,
30 AWA_maskb_set = BFIN_UART_GCTL,
31 AWA_maskb_toggle = BFIN_UART_GCTL,
32 AWA_dir = SPORT1_STAT,
33 AWA_polar = SPORT1_STAT,
34 AWA_edge = SPORT1_STAT,
35 AWA_both = SPORT1_STAT,
36 #if ANOMALY_05000311
37 AWA_inen = TIMER_ENABLE,
38 #elif ANOMALY_05000323
39 AWA_inen = DMA1_1_CONFIG,
40 #endif
42 /* Anomaly Workaround */
43 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
44 #else
45 #define AWA_DUMMY_READ(...) do { } while (0)
46 #endif
48 static struct gpio_port_t * const gpio_array[] = {
49 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
50 (struct gpio_port_t *) FIO_FLAG_D,
51 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
52 (struct gpio_port_t *) PORTFIO,
53 (struct gpio_port_t *) PORTGIO,
54 (struct gpio_port_t *) PORTHIO,
55 #elif defined(BF561_FAMILY)
56 (struct gpio_port_t *) FIO0_FLAG_D,
57 (struct gpio_port_t *) FIO1_FLAG_D,
58 (struct gpio_port_t *) FIO2_FLAG_D,
59 #elif defined(CONFIG_BF54x)
60 (struct gpio_port_t *)PORTA_FER,
61 (struct gpio_port_t *)PORTB_FER,
62 (struct gpio_port_t *)PORTC_FER,
63 (struct gpio_port_t *)PORTD_FER,
64 (struct gpio_port_t *)PORTE_FER,
65 (struct gpio_port_t *)PORTF_FER,
66 (struct gpio_port_t *)PORTG_FER,
67 (struct gpio_port_t *)PORTH_FER,
68 (struct gpio_port_t *)PORTI_FER,
69 (struct gpio_port_t *)PORTJ_FER,
70 #else
71 # error no gpio arrays defined
72 #endif
75 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
76 static unsigned short * const port_fer[] = {
77 (unsigned short *) PORTF_FER,
78 (unsigned short *) PORTG_FER,
79 (unsigned short *) PORTH_FER,
82 # if !defined(BF537_FAMILY)
83 static unsigned short * const port_mux[] = {
84 (unsigned short *) PORTF_MUX,
85 (unsigned short *) PORTG_MUX,
86 (unsigned short *) PORTH_MUX,
89 static const
90 u8 pmux_offset[][16] = {
91 # if defined(CONFIG_BF52x)
92 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
93 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
94 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
95 # elif defined(CONFIG_BF51x)
96 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
97 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
98 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
99 # endif
101 # endif
103 #endif
105 static unsigned short reserved_gpio_map[GPIO_BANK_NUM];
106 static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
107 static unsigned short reserved_gpio_irq_map[GPIO_BANK_NUM];
109 #define RESOURCE_LABEL_SIZE 16
111 static struct str_ident {
112 char name[RESOURCE_LABEL_SIZE];
113 } str_ident[MAX_RESOURCES];
115 #if defined(CONFIG_PM)
116 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
117 #endif
119 inline int check_gpio(unsigned gpio)
121 #if defined(CONFIG_BF54x)
122 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
123 || gpio == GPIO_PH14 || gpio == GPIO_PH15
124 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
125 return -EINVAL;
126 #endif
127 if (gpio >= MAX_BLACKFIN_GPIOS)
128 return -EINVAL;
129 return 0;
132 static void gpio_error(unsigned gpio)
134 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
137 static void set_label(unsigned short ident, const char *label)
139 if (label) {
140 strncpy(str_ident[ident].name, label,
141 RESOURCE_LABEL_SIZE);
142 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
146 static char *get_label(unsigned short ident)
148 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
151 static int cmp_label(unsigned short ident, const char *label)
153 if (label == NULL) {
154 dump_stack();
155 printk(KERN_ERR "Please provide none-null label\n");
158 if (label)
159 return strcmp(str_ident[ident].name, label);
160 else
161 return -EINVAL;
164 static void port_setup(unsigned gpio, unsigned short usage)
166 if (check_gpio(gpio))
167 return;
169 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
170 if (usage == GPIO_USAGE)
171 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
172 else
173 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
174 SSYNC();
175 #elif defined(CONFIG_BF54x)
176 if (usage == GPIO_USAGE)
177 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
178 else
179 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
180 SSYNC();
181 #endif
184 #ifdef BF537_FAMILY
185 static struct {
186 unsigned short res;
187 unsigned short offset;
188 } port_mux_lut[] = {
189 {.res = P_PPI0_D13, .offset = 11},
190 {.res = P_PPI0_D14, .offset = 11},
191 {.res = P_PPI0_D15, .offset = 11},
192 {.res = P_SPORT1_TFS, .offset = 11},
193 {.res = P_SPORT1_TSCLK, .offset = 11},
194 {.res = P_SPORT1_DTPRI, .offset = 11},
195 {.res = P_PPI0_D10, .offset = 10},
196 {.res = P_PPI0_D11, .offset = 10},
197 {.res = P_PPI0_D12, .offset = 10},
198 {.res = P_SPORT1_RSCLK, .offset = 10},
199 {.res = P_SPORT1_RFS, .offset = 10},
200 {.res = P_SPORT1_DRPRI, .offset = 10},
201 {.res = P_PPI0_D8, .offset = 9},
202 {.res = P_PPI0_D9, .offset = 9},
203 {.res = P_SPORT1_DRSEC, .offset = 9},
204 {.res = P_SPORT1_DTSEC, .offset = 9},
205 {.res = P_TMR2, .offset = 8},
206 {.res = P_PPI0_FS3, .offset = 8},
207 {.res = P_TMR3, .offset = 7},
208 {.res = P_SPI0_SSEL4, .offset = 7},
209 {.res = P_TMR4, .offset = 6},
210 {.res = P_SPI0_SSEL5, .offset = 6},
211 {.res = P_TMR5, .offset = 5},
212 {.res = P_SPI0_SSEL6, .offset = 5},
213 {.res = P_UART1_RX, .offset = 4},
214 {.res = P_UART1_TX, .offset = 4},
215 {.res = P_TMR6, .offset = 4},
216 {.res = P_TMR7, .offset = 4},
217 {.res = P_UART0_RX, .offset = 3},
218 {.res = P_UART0_TX, .offset = 3},
219 {.res = P_DMAR0, .offset = 3},
220 {.res = P_DMAR1, .offset = 3},
221 {.res = P_SPORT0_DTSEC, .offset = 1},
222 {.res = P_SPORT0_DRSEC, .offset = 1},
223 {.res = P_CAN0_RX, .offset = 1},
224 {.res = P_CAN0_TX, .offset = 1},
225 {.res = P_SPI0_SSEL7, .offset = 1},
226 {.res = P_SPORT0_TFS, .offset = 0},
227 {.res = P_SPORT0_DTPRI, .offset = 0},
228 {.res = P_SPI0_SSEL2, .offset = 0},
229 {.res = P_SPI0_SSEL3, .offset = 0},
232 static void portmux_setup(unsigned short per)
234 u16 y, offset, muxreg;
235 u16 function = P_FUNCT2MUX(per);
237 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
238 if (port_mux_lut[y].res == per) {
240 /* SET PORTMUX REG */
242 offset = port_mux_lut[y].offset;
243 muxreg = bfin_read_PORT_MUX();
245 if (offset != 1)
246 muxreg &= ~(1 << offset);
247 else
248 muxreg &= ~(3 << 1);
250 muxreg |= (function << offset);
251 bfin_write_PORT_MUX(muxreg);
255 #elif defined(CONFIG_BF54x)
256 inline void portmux_setup(unsigned short per)
258 u32 pmux;
259 u16 ident = P_IDENT(per);
260 u16 function = P_FUNCT2MUX(per);
262 pmux = gpio_array[gpio_bank(ident)]->port_mux;
264 pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
265 pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
267 gpio_array[gpio_bank(ident)]->port_mux = pmux;
270 inline u16 get_portmux(unsigned short per)
272 u32 pmux;
273 u16 ident = P_IDENT(per);
275 pmux = gpio_array[gpio_bank(ident)]->port_mux;
277 return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
279 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
280 inline void portmux_setup(unsigned short per)
282 u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
283 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
285 pmux = *port_mux[gpio_bank(ident)];
286 pmux &= ~(3 << offset);
287 pmux |= (function & 3) << offset;
288 *port_mux[gpio_bank(ident)] = pmux;
289 SSYNC();
291 #else
292 # define portmux_setup(...) do { } while (0)
293 #endif
295 #ifndef CONFIG_BF54x
296 /***********************************************************
298 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
300 * INPUTS/OUTPUTS:
301 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
304 * DESCRIPTION: These functions abstract direct register access
305 * to Blackfin processor General Purpose
306 * Ports Regsiters
308 * CAUTION: These functions do not belong to the GPIO Driver API
309 *************************************************************
310 * MODIFICATION HISTORY :
311 **************************************************************/
313 /* Set a specific bit */
315 #define SET_GPIO(name) \
316 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
318 unsigned long flags; \
319 local_irq_save_hw(flags); \
320 if (arg) \
321 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
322 else \
323 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
324 AWA_DUMMY_READ(name); \
325 local_irq_restore_hw(flags); \
327 EXPORT_SYMBOL(set_gpio_ ## name);
329 SET_GPIO(dir) /* set_gpio_dir() */
330 SET_GPIO(inen) /* set_gpio_inen() */
331 SET_GPIO(polar) /* set_gpio_polar() */
332 SET_GPIO(edge) /* set_gpio_edge() */
333 SET_GPIO(both) /* set_gpio_both() */
336 #define SET_GPIO_SC(name) \
337 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
339 unsigned long flags; \
340 if (ANOMALY_05000311 || ANOMALY_05000323) \
341 local_irq_save_hw(flags); \
342 if (arg) \
343 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
344 else \
345 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
346 if (ANOMALY_05000311 || ANOMALY_05000323) { \
347 AWA_DUMMY_READ(name); \
348 local_irq_restore_hw(flags); \
351 EXPORT_SYMBOL(set_gpio_ ## name);
353 SET_GPIO_SC(maska)
354 SET_GPIO_SC(maskb)
355 SET_GPIO_SC(data)
357 void set_gpio_toggle(unsigned gpio)
359 unsigned long flags;
360 if (ANOMALY_05000311 || ANOMALY_05000323)
361 local_irq_save_hw(flags);
362 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
363 if (ANOMALY_05000311 || ANOMALY_05000323) {
364 AWA_DUMMY_READ(toggle);
365 local_irq_restore_hw(flags);
368 EXPORT_SYMBOL(set_gpio_toggle);
371 /*Set current PORT date (16-bit word)*/
373 #define SET_GPIO_P(name) \
374 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
376 unsigned long flags; \
377 if (ANOMALY_05000311 || ANOMALY_05000323) \
378 local_irq_save_hw(flags); \
379 gpio_array[gpio_bank(gpio)]->name = arg; \
380 if (ANOMALY_05000311 || ANOMALY_05000323) { \
381 AWA_DUMMY_READ(name); \
382 local_irq_restore_hw(flags); \
385 EXPORT_SYMBOL(set_gpiop_ ## name);
387 SET_GPIO_P(data)
388 SET_GPIO_P(dir)
389 SET_GPIO_P(inen)
390 SET_GPIO_P(polar)
391 SET_GPIO_P(edge)
392 SET_GPIO_P(both)
393 SET_GPIO_P(maska)
394 SET_GPIO_P(maskb)
396 /* Get a specific bit */
397 #define GET_GPIO(name) \
398 unsigned short get_gpio_ ## name(unsigned gpio) \
400 unsigned long flags; \
401 unsigned short ret; \
402 if (ANOMALY_05000311 || ANOMALY_05000323) \
403 local_irq_save_hw(flags); \
404 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
405 if (ANOMALY_05000311 || ANOMALY_05000323) { \
406 AWA_DUMMY_READ(name); \
407 local_irq_restore_hw(flags); \
409 return ret; \
411 EXPORT_SYMBOL(get_gpio_ ## name);
413 GET_GPIO(data)
414 GET_GPIO(dir)
415 GET_GPIO(inen)
416 GET_GPIO(polar)
417 GET_GPIO(edge)
418 GET_GPIO(both)
419 GET_GPIO(maska)
420 GET_GPIO(maskb)
422 /*Get current PORT date (16-bit word)*/
424 #define GET_GPIO_P(name) \
425 unsigned short get_gpiop_ ## name(unsigned gpio) \
427 unsigned long flags; \
428 unsigned short ret; \
429 if (ANOMALY_05000311 || ANOMALY_05000323) \
430 local_irq_save_hw(flags); \
431 ret = (gpio_array[gpio_bank(gpio)]->name); \
432 if (ANOMALY_05000311 || ANOMALY_05000323) { \
433 AWA_DUMMY_READ(name); \
434 local_irq_restore_hw(flags); \
436 return ret; \
438 EXPORT_SYMBOL(get_gpiop_ ## name);
440 GET_GPIO_P(data)
441 GET_GPIO_P(dir)
442 GET_GPIO_P(inen)
443 GET_GPIO_P(polar)
444 GET_GPIO_P(edge)
445 GET_GPIO_P(both)
446 GET_GPIO_P(maska)
447 GET_GPIO_P(maskb)
450 #ifdef CONFIG_PM
452 static unsigned short wakeup_map[GPIO_BANK_NUM];
453 static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
455 static const unsigned int sic_iwr_irqs[] = {
456 #if defined(BF533_FAMILY)
457 IRQ_PROG_INTB
458 #elif defined(BF537_FAMILY)
459 IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX
460 #elif defined(BF538_FAMILY)
461 IRQ_PORTF_INTB
462 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
463 IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
464 #elif defined(BF561_FAMILY)
465 IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
466 #else
467 # error no SIC_IWR defined
468 #endif
471 /***********************************************************
473 * FUNCTIONS: Blackfin PM Setup API
475 * INPUTS/OUTPUTS:
476 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
477 * type -
478 * PM_WAKE_RISING
479 * PM_WAKE_FALLING
480 * PM_WAKE_HIGH
481 * PM_WAKE_LOW
482 * PM_WAKE_BOTH_EDGES
484 * DESCRIPTION: Blackfin PM Driver API
486 * CAUTION:
487 *************************************************************
488 * MODIFICATION HISTORY :
489 **************************************************************/
490 int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
492 unsigned long flags;
494 if ((check_gpio(gpio) < 0) || !type)
495 return -EINVAL;
497 local_irq_save_hw(flags);
498 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
499 wakeup_flags_map[gpio] = type;
500 local_irq_restore_hw(flags);
502 return 0;
504 EXPORT_SYMBOL(gpio_pm_wakeup_request);
506 void gpio_pm_wakeup_free(unsigned gpio)
508 unsigned long flags;
510 if (check_gpio(gpio) < 0)
511 return;
513 local_irq_save_hw(flags);
515 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
517 local_irq_restore_hw(flags);
519 EXPORT_SYMBOL(gpio_pm_wakeup_free);
521 static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
523 port_setup(gpio, GPIO_USAGE);
524 set_gpio_dir(gpio, 0);
525 set_gpio_inen(gpio, 1);
527 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
528 set_gpio_edge(gpio, 1);
529 else
530 set_gpio_edge(gpio, 0);
532 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
533 set_gpio_both(gpio, 1);
534 else
535 set_gpio_both(gpio, 0);
537 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
538 set_gpio_polar(gpio, 1);
539 else
540 set_gpio_polar(gpio, 0);
542 SSYNC();
544 return 0;
547 u32 bfin_pm_standby_setup(void)
549 u16 bank, mask, i, gpio;
551 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
552 mask = wakeup_map[gpio_bank(i)];
553 bank = gpio_bank(i);
555 gpio_bank_saved[bank].maskb = gpio_array[bank]->maskb;
556 gpio_array[bank]->maskb = 0;
558 if (mask) {
559 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
560 gpio_bank_saved[bank].fer = *port_fer[bank];
561 #endif
562 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
563 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
564 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
565 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
566 gpio_bank_saved[bank].both = gpio_array[bank]->both;
567 gpio_bank_saved[bank].reserved =
568 reserved_gpio_map[bank];
570 gpio = i;
572 while (mask) {
573 if ((mask & 1) && (wakeup_flags_map[gpio] !=
574 PM_WAKE_IGNORE)) {
575 reserved_gpio_map[gpio_bank(gpio)] |=
576 gpio_bit(gpio);
577 bfin_gpio_wakeup_type(gpio,
578 wakeup_flags_map[gpio]);
579 set_gpio_data(gpio, 0); /*Clear*/
581 gpio++;
582 mask >>= 1;
585 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
586 gpio_array[bank]->maskb_set = wakeup_map[gpio_bank(i)];
590 AWA_DUMMY_READ(maskb_set);
592 return 0;
595 void bfin_pm_standby_restore(void)
597 u16 bank, mask, i;
599 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
600 mask = wakeup_map[gpio_bank(i)];
601 bank = gpio_bank(i);
603 if (mask) {
604 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
605 *port_fer[bank] = gpio_bank_saved[bank].fer;
606 #endif
607 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
608 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
609 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
610 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
611 gpio_array[bank]->both = gpio_bank_saved[bank].both;
613 reserved_gpio_map[bank] =
614 gpio_bank_saved[bank].reserved;
615 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
618 gpio_array[bank]->maskb = gpio_bank_saved[bank].maskb;
620 AWA_DUMMY_READ(maskb);
623 void bfin_gpio_pm_hibernate_suspend(void)
625 int i, bank;
627 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
628 bank = gpio_bank(i);
630 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
631 gpio_bank_saved[bank].fer = *port_fer[bank];
632 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
633 gpio_bank_saved[bank].mux = *port_mux[bank];
634 #else
635 if (bank == 0)
636 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
637 #endif
638 #endif
639 gpio_bank_saved[bank].data = gpio_array[bank]->data;
640 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
641 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
642 gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
643 gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
644 gpio_bank_saved[bank].both = gpio_array[bank]->both;
645 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
648 AWA_DUMMY_READ(maska);
651 void bfin_gpio_pm_hibernate_restore(void)
653 int i, bank;
655 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
656 bank = gpio_bank(i);
658 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
659 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
660 *port_mux[bank] = gpio_bank_saved[bank].mux;
661 #else
662 if (bank == 0)
663 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
664 #endif
665 *port_fer[bank] = gpio_bank_saved[bank].fer;
666 #endif
667 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
668 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
669 & gpio_bank_saved[bank].dir;
670 gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
671 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
672 gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
673 gpio_array[bank]->both = gpio_bank_saved[bank].both;
674 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
676 AWA_DUMMY_READ(maska);
680 #endif
681 #else /* CONFIG_BF54x */
682 #ifdef CONFIG_PM
684 u32 bfin_pm_standby_setup(void)
686 return 0;
689 void bfin_pm_standby_restore(void)
694 void bfin_gpio_pm_hibernate_suspend(void)
696 int i, bank;
698 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
699 bank = gpio_bank(i);
701 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
702 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
703 gpio_bank_saved[bank].data = gpio_array[bank]->data;
704 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
705 gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
709 void bfin_gpio_pm_hibernate_restore(void)
711 int i, bank;
713 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
714 bank = gpio_bank(i);
716 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
717 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
718 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
719 gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
720 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
721 | gpio_bank_saved[bank].dir;
724 #endif
726 unsigned short get_gpio_dir(unsigned gpio)
728 return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
730 EXPORT_SYMBOL(get_gpio_dir);
732 #endif /* CONFIG_BF54x */
734 /***********************************************************
736 * FUNCTIONS: Blackfin Peripheral Resource Allocation
737 * and PortMux Setup
739 * INPUTS/OUTPUTS:
740 * per Peripheral Identifier
741 * label String
743 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
745 * CAUTION:
746 *************************************************************
747 * MODIFICATION HISTORY :
748 **************************************************************/
750 int peripheral_request(unsigned short per, const char *label)
752 unsigned long flags;
753 unsigned short ident = P_IDENT(per);
756 * Don't cares are pins with only one dedicated function
759 if (per & P_DONTCARE)
760 return 0;
762 if (!(per & P_DEFINED))
763 return -ENODEV;
765 local_irq_save_hw(flags);
767 /* If a pin can be muxed as either GPIO or peripheral, make
768 * sure it is not already a GPIO pin when we request it.
770 if (unlikely(!check_gpio(ident) &&
771 reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
772 if (system_state == SYSTEM_BOOTING)
773 dump_stack();
774 printk(KERN_ERR
775 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
776 __func__, ident, get_label(ident));
777 local_irq_restore_hw(flags);
778 return -EBUSY;
781 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
784 * Pin functions like AMC address strobes my
785 * be requested and used by several drivers
788 #ifdef CONFIG_BF54x
789 if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
790 #else
791 if (!(per & P_MAYSHARE)) {
792 #endif
794 * Allow that the identical pin function can
795 * be requested from the same driver twice
798 if (cmp_label(ident, label) == 0)
799 goto anyway;
801 if (system_state == SYSTEM_BOOTING)
802 dump_stack();
803 printk(KERN_ERR
804 "%s: Peripheral %d function %d is already reserved by %s !\n",
805 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
806 local_irq_restore_hw(flags);
807 return -EBUSY;
811 anyway:
812 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
814 portmux_setup(per);
815 port_setup(ident, PERIPHERAL_USAGE);
817 local_irq_restore_hw(flags);
818 set_label(ident, label);
820 return 0;
822 EXPORT_SYMBOL(peripheral_request);
824 int peripheral_request_list(const unsigned short per[], const char *label)
826 u16 cnt;
827 int ret;
829 for (cnt = 0; per[cnt] != 0; cnt++) {
831 ret = peripheral_request(per[cnt], label);
833 if (ret < 0) {
834 for ( ; cnt > 0; cnt--)
835 peripheral_free(per[cnt - 1]);
837 return ret;
841 return 0;
843 EXPORT_SYMBOL(peripheral_request_list);
845 void peripheral_free(unsigned short per)
847 unsigned long flags;
848 unsigned short ident = P_IDENT(per);
850 if (per & P_DONTCARE)
851 return;
853 if (!(per & P_DEFINED))
854 return;
856 local_irq_save_hw(flags);
858 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
859 local_irq_restore_hw(flags);
860 return;
863 if (!(per & P_MAYSHARE))
864 port_setup(ident, GPIO_USAGE);
866 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
868 set_label(ident, "free");
870 local_irq_restore_hw(flags);
872 EXPORT_SYMBOL(peripheral_free);
874 void peripheral_free_list(const unsigned short per[])
876 u16 cnt;
877 for (cnt = 0; per[cnt] != 0; cnt++)
878 peripheral_free(per[cnt]);
880 EXPORT_SYMBOL(peripheral_free_list);
882 /***********************************************************
884 * FUNCTIONS: Blackfin GPIO Driver
886 * INPUTS/OUTPUTS:
887 * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
888 * label String
890 * DESCRIPTION: Blackfin GPIO Driver API
892 * CAUTION:
893 *************************************************************
894 * MODIFICATION HISTORY :
895 **************************************************************/
897 int bfin_gpio_request(unsigned gpio, const char *label)
899 unsigned long flags;
901 if (check_gpio(gpio) < 0)
902 return -EINVAL;
904 local_irq_save_hw(flags);
907 * Allow that the identical GPIO can
908 * be requested from the same driver twice
909 * Do nothing and return -
912 if (cmp_label(gpio, label) == 0) {
913 local_irq_restore_hw(flags);
914 return 0;
917 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
918 if (system_state == SYSTEM_BOOTING)
919 dump_stack();
920 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
921 gpio, get_label(gpio));
922 local_irq_restore_hw(flags);
923 return -EBUSY;
925 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
926 if (system_state == SYSTEM_BOOTING)
927 dump_stack();
928 printk(KERN_ERR
929 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
930 gpio, get_label(gpio));
931 local_irq_restore_hw(flags);
932 return -EBUSY;
934 if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
935 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
936 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
938 #ifndef CONFIG_BF54x
939 else { /* Reset POLAR setting when acquiring a gpio for the first time */
940 set_gpio_polar(gpio, 0);
942 #endif
944 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
945 set_label(gpio, label);
947 local_irq_restore_hw(flags);
949 port_setup(gpio, GPIO_USAGE);
951 return 0;
953 EXPORT_SYMBOL(bfin_gpio_request);
955 void bfin_gpio_free(unsigned gpio)
957 unsigned long flags;
959 if (check_gpio(gpio) < 0)
960 return;
962 might_sleep();
964 local_irq_save_hw(flags);
966 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
967 if (system_state == SYSTEM_BOOTING)
968 dump_stack();
969 gpio_error(gpio);
970 local_irq_restore_hw(flags);
971 return;
974 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
976 set_label(gpio, "free");
978 local_irq_restore_hw(flags);
980 EXPORT_SYMBOL(bfin_gpio_free);
982 int bfin_gpio_irq_request(unsigned gpio, const char *label)
984 unsigned long flags;
986 if (check_gpio(gpio) < 0)
987 return -EINVAL;
989 local_irq_save_hw(flags);
991 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
992 if (system_state == SYSTEM_BOOTING)
993 dump_stack();
994 printk(KERN_ERR
995 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
996 gpio, get_label(gpio));
997 local_irq_restore_hw(flags);
998 return -EBUSY;
1000 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))
1001 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1002 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1003 gpio, get_label(gpio));
1005 reserved_gpio_irq_map[gpio_bank(gpio)] |= gpio_bit(gpio);
1006 set_label(gpio, label);
1008 local_irq_restore_hw(flags);
1010 port_setup(gpio, GPIO_USAGE);
1012 return 0;
1015 void bfin_gpio_irq_free(unsigned gpio)
1017 unsigned long flags;
1019 if (check_gpio(gpio) < 0)
1020 return;
1022 local_irq_save_hw(flags);
1024 if (unlikely(!(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1025 if (system_state == SYSTEM_BOOTING)
1026 dump_stack();
1027 gpio_error(gpio);
1028 local_irq_restore_hw(flags);
1029 return;
1032 reserved_gpio_irq_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1034 set_label(gpio, "free");
1036 local_irq_restore_hw(flags);
1039 static inline void __bfin_gpio_direction_input(unsigned gpio)
1041 #ifdef CONFIG_BF54x
1042 gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
1043 #else
1044 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1045 #endif
1046 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1049 int bfin_gpio_direction_input(unsigned gpio)
1051 unsigned long flags;
1053 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1054 gpio_error(gpio);
1055 return -EINVAL;
1058 local_irq_save_hw(flags);
1059 __bfin_gpio_direction_input(gpio);
1060 AWA_DUMMY_READ(inen);
1061 local_irq_restore_hw(flags);
1063 return 0;
1065 EXPORT_SYMBOL(bfin_gpio_direction_input);
1067 void bfin_gpio_irq_prepare(unsigned gpio)
1069 #ifdef CONFIG_BF54x
1070 unsigned long flags;
1071 #endif
1073 port_setup(gpio, GPIO_USAGE);
1075 #ifdef CONFIG_BF54x
1076 local_irq_save_hw(flags);
1077 __bfin_gpio_direction_input(gpio);
1078 local_irq_restore_hw(flags);
1079 #endif
1082 void bfin_gpio_set_value(unsigned gpio, int arg)
1084 if (arg)
1085 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1086 else
1087 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1089 EXPORT_SYMBOL(bfin_gpio_set_value);
1091 int bfin_gpio_direction_output(unsigned gpio, int value)
1093 unsigned long flags;
1095 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1096 gpio_error(gpio);
1097 return -EINVAL;
1100 local_irq_save_hw(flags);
1102 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1103 gpio_set_value(gpio, value);
1104 #ifdef CONFIG_BF54x
1105 gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
1106 #else
1107 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1108 #endif
1110 AWA_DUMMY_READ(dir);
1111 local_irq_restore_hw(flags);
1113 return 0;
1115 EXPORT_SYMBOL(bfin_gpio_direction_output);
1117 int bfin_gpio_get_value(unsigned gpio)
1119 #ifdef CONFIG_BF54x
1120 return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
1121 #else
1122 unsigned long flags;
1124 if (unlikely(get_gpio_edge(gpio))) {
1125 int ret;
1126 local_irq_save_hw(flags);
1127 set_gpio_edge(gpio, 0);
1128 ret = get_gpio_data(gpio);
1129 set_gpio_edge(gpio, 1);
1130 local_irq_restore_hw(flags);
1131 return ret;
1132 } else
1133 return get_gpio_data(gpio);
1134 #endif
1136 EXPORT_SYMBOL(bfin_gpio_get_value);
1138 /* If we are booting from SPI and our board lacks a strong enough pull up,
1139 * the core can reset and execute the bootrom faster than the resistor can
1140 * pull the signal logically high. To work around this (common) error in
1141 * board design, we explicitly set the pin back to GPIO mode, force /CS
1142 * high, and wait for the electrons to do their thing.
1144 * This function only makes sense to be called from reset code, but it
1145 * lives here as we need to force all the GPIO states w/out going through
1146 * BUG() checks and such.
1148 void bfin_reset_boot_spi_cs(unsigned short pin)
1150 unsigned short gpio = P_IDENT(pin);
1151 port_setup(gpio, GPIO_USAGE);
1152 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1153 AWA_DUMMY_READ(data_set);
1154 udelay(1);
1157 #if defined(CONFIG_PROC_FS)
1158 static int gpio_proc_read(char *buf, char **start, off_t offset,
1159 int len, int *unused_i, void *unused_v)
1161 int c, irq, gpio, outlen = 0;
1163 for (c = 0; c < MAX_RESOURCES; c++) {
1164 irq = reserved_gpio_irq_map[gpio_bank(c)] & gpio_bit(c);
1165 gpio = reserved_gpio_map[gpio_bank(c)] & gpio_bit(c);
1166 if (!check_gpio(c) && (gpio || irq))
1167 len = sprintf(buf, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1168 get_label(c), (gpio && irq) ? " *" : "",
1169 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1170 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
1171 len = sprintf(buf, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1172 else
1173 continue;
1174 buf += len;
1175 outlen += len;
1177 return outlen;
1180 static __init int gpio_register_proc(void)
1182 struct proc_dir_entry *proc_gpio;
1184 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1185 if (proc_gpio)
1186 proc_gpio->read_proc = gpio_proc_read;
1187 return proc_gpio != NULL;
1189 __initcall(gpio_register_proc);
1190 #endif
1192 #ifdef CONFIG_GPIOLIB
1193 int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1195 return bfin_gpio_direction_input(gpio);
1198 int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1200 return bfin_gpio_direction_output(gpio, level);
1203 int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1205 return bfin_gpio_get_value(gpio);
1208 void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1210 return bfin_gpio_set_value(gpio, value);
1213 int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1215 return bfin_gpio_request(gpio, chip->label);
1218 void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1220 return bfin_gpio_free(gpio);
1223 static struct gpio_chip bfin_chip = {
1224 .label = "Blackfin-GPIOlib",
1225 .direction_input = bfin_gpiolib_direction_input,
1226 .get = bfin_gpiolib_get_value,
1227 .direction_output = bfin_gpiolib_direction_output,
1228 .set = bfin_gpiolib_set_value,
1229 .request = bfin_gpiolib_gpio_request,
1230 .free = bfin_gpiolib_gpio_free,
1231 .base = 0,
1232 .ngpio = MAX_BLACKFIN_GPIOS,
1235 static int __init bfin_gpiolib_setup(void)
1237 return gpiochip_add(&bfin_chip);
1239 arch_initcall(bfin_gpiolib_setup);
1240 #endif