Merge branch 'kconfig-for-40' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek...
[pandora-kernel.git] / arch / blackfin / kernel / bfin_gpio.c
1 /*
2  * GPIO Abstraction Layer
3  *
4  * Copyright 2006-2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later
7  */
8
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/proc_fs.h>
13 #include <linux/seq_file.h>
14 #include <asm/blackfin.h>
15 #include <asm/gpio.h>
16 #include <asm/portmux.h>
17 #include <linux/irq.h>
18 #include <asm/irq_handler.h>
19
20 #if ANOMALY_05000311 || ANOMALY_05000323
21 enum {
22         AWA_data = SYSCR,
23         AWA_data_clear = SYSCR,
24         AWA_data_set = SYSCR,
25         AWA_toggle = SYSCR,
26         AWA_maska = BFIN_UART_SCR,
27         AWA_maska_clear = BFIN_UART_SCR,
28         AWA_maska_set = BFIN_UART_SCR,
29         AWA_maska_toggle = BFIN_UART_SCR,
30         AWA_maskb = BFIN_UART_GCTL,
31         AWA_maskb_clear = BFIN_UART_GCTL,
32         AWA_maskb_set = BFIN_UART_GCTL,
33         AWA_maskb_toggle = BFIN_UART_GCTL,
34         AWA_dir = SPORT1_STAT,
35         AWA_polar = SPORT1_STAT,
36         AWA_edge = SPORT1_STAT,
37         AWA_both = SPORT1_STAT,
38 #if ANOMALY_05000311
39         AWA_inen = TIMER_ENABLE,
40 #elif ANOMALY_05000323
41         AWA_inen = DMA1_1_CONFIG,
42 #endif
43 };
44         /* Anomaly Workaround */
45 #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
46 #else
47 #define AWA_DUMMY_READ(...)  do { } while (0)
48 #endif
49
50 static struct gpio_port_t * const gpio_array[] = {
51 #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
52         (struct gpio_port_t *) FIO_FLAG_D,
53 #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
54         (struct gpio_port_t *) PORTFIO,
55         (struct gpio_port_t *) PORTGIO,
56         (struct gpio_port_t *) PORTHIO,
57 #elif defined(BF561_FAMILY)
58         (struct gpio_port_t *) FIO0_FLAG_D,
59         (struct gpio_port_t *) FIO1_FLAG_D,
60         (struct gpio_port_t *) FIO2_FLAG_D,
61 #elif defined(CONFIG_BF54x)
62         (struct gpio_port_t *)PORTA_FER,
63         (struct gpio_port_t *)PORTB_FER,
64         (struct gpio_port_t *)PORTC_FER,
65         (struct gpio_port_t *)PORTD_FER,
66         (struct gpio_port_t *)PORTE_FER,
67         (struct gpio_port_t *)PORTF_FER,
68         (struct gpio_port_t *)PORTG_FER,
69         (struct gpio_port_t *)PORTH_FER,
70         (struct gpio_port_t *)PORTI_FER,
71         (struct gpio_port_t *)PORTJ_FER,
72 #else
73 # error no gpio arrays defined
74 #endif
75 };
76
77 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
78 static unsigned short * const port_fer[] = {
79         (unsigned short *) PORTF_FER,
80         (unsigned short *) PORTG_FER,
81         (unsigned short *) PORTH_FER,
82 };
83
84 # if !defined(BF537_FAMILY)
85 static unsigned short * const port_mux[] = {
86         (unsigned short *) PORTF_MUX,
87         (unsigned short *) PORTG_MUX,
88         (unsigned short *) PORTH_MUX,
89 };
90
91 static const
92 u8 pmux_offset[][16] = {
93 #  if defined(CONFIG_BF52x)
94         { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
95         { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
96         { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
97 #  elif defined(CONFIG_BF51x)
98         { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
99         { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
100         { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
101 #  endif
102 };
103 # endif
104
105 #elif defined(BF538_FAMILY)
106 static unsigned short * const port_fer[] = {
107         (unsigned short *) PORTCIO_FER,
108         (unsigned short *) PORTDIO_FER,
109         (unsigned short *) PORTEIO_FER,
110 };
111 #endif
112
113 #define RESOURCE_LABEL_SIZE     16
114
115 static struct str_ident {
116         char name[RESOURCE_LABEL_SIZE];
117 } str_ident[MAX_RESOURCES];
118
119 #if defined(CONFIG_PM)
120 static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
121 #endif
122
123 static void gpio_error(unsigned gpio)
124 {
125         printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
126 }
127
128 static void set_label(unsigned short ident, const char *label)
129 {
130         if (label) {
131                 strncpy(str_ident[ident].name, label,
132                          RESOURCE_LABEL_SIZE);
133                 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
134         }
135 }
136
137 static char *get_label(unsigned short ident)
138 {
139         return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
140 }
141
142 static int cmp_label(unsigned short ident, const char *label)
143 {
144         if (label == NULL) {
145                 dump_stack();
146                 printk(KERN_ERR "Please provide none-null label\n");
147         }
148
149         if (label)
150                 return strcmp(str_ident[ident].name, label);
151         else
152                 return -EINVAL;
153 }
154
155 #define map_entry(m, i)      reserved_##m##_map[gpio_bank(i)]
156 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
157 #define reserve(m, i)        (map_entry(m, i) |= gpio_bit(i))
158 #define unreserve(m, i)      (map_entry(m, i) &= ~gpio_bit(i))
159 #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
160
161 DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
162 DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
163 DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
164
165 inline int check_gpio(unsigned gpio)
166 {
167 #if defined(CONFIG_BF54x)
168         if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
169             || gpio == GPIO_PH14 || gpio == GPIO_PH15
170             || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
171                 return -EINVAL;
172 #endif
173         if (gpio >= MAX_BLACKFIN_GPIOS)
174                 return -EINVAL;
175         return 0;
176 }
177
178 static void port_setup(unsigned gpio, unsigned short usage)
179 {
180 #if defined(BF538_FAMILY)
181         /*
182          * BF538/9 Port C,D and E are special.
183          * Inverted PORT_FER polarity on CDE and no PORF_FER on F
184          * Regular PORT F GPIOs are handled here, CDE are exclusively
185          * managed by GPIOLIB
186          */
187
188         if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
189                 return;
190
191         gpio -= MAX_BLACKFIN_GPIOS;
192
193         if (usage == GPIO_USAGE)
194                 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
195         else
196                 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
197         SSYNC();
198         return;
199 #endif
200
201         if (check_gpio(gpio))
202                 return;
203
204 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
205         if (usage == GPIO_USAGE)
206                 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
207         else
208                 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
209         SSYNC();
210 #elif defined(CONFIG_BF54x)
211         if (usage == GPIO_USAGE)
212                 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
213         else
214                 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
215         SSYNC();
216 #endif
217 }
218
219 #ifdef BF537_FAMILY
220 static const s8 port_mux[] = {
221         [GPIO_PF0] = 3,
222         [GPIO_PF1] = 3,
223         [GPIO_PF2] = 4,
224         [GPIO_PF3] = 4,
225         [GPIO_PF4] = 5,
226         [GPIO_PF5] = 6,
227         [GPIO_PF6] = 7,
228         [GPIO_PF7] = 8,
229         [GPIO_PF8 ... GPIO_PF15] = -1,
230         [GPIO_PG0 ... GPIO_PG7] = -1,
231         [GPIO_PG8] = 9,
232         [GPIO_PG9] = 9,
233         [GPIO_PG10] = 10,
234         [GPIO_PG11] = 10,
235         [GPIO_PG12] = 10,
236         [GPIO_PG13] = 11,
237         [GPIO_PG14] = 11,
238         [GPIO_PG15] = 11,
239         [GPIO_PH0 ... GPIO_PH15] = -1,
240         [PORT_PJ0 ... PORT_PJ3] = -1,
241         [PORT_PJ4] = 1,
242         [PORT_PJ5] = 1,
243         [PORT_PJ6 ... PORT_PJ9] = -1,
244         [PORT_PJ10] = 0,
245         [PORT_PJ11] = 0,
246 };
247
248 static int portmux_group_check(unsigned short per)
249 {
250         u16 ident = P_IDENT(per);
251         u16 function = P_FUNCT2MUX(per);
252         s8 offset = port_mux[ident];
253         u16 m, pmux, pfunc;
254
255         if (offset < 0)
256                 return 0;
257
258         pmux = bfin_read_PORT_MUX();
259         for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
260                 if (m == ident)
261                         continue;
262                 if (port_mux[m] != offset)
263                         continue;
264                 if (!is_reserved(peri, m, 1))
265                         continue;
266
267                 if (offset == 1)
268                         pfunc = (pmux >> offset) & 3;
269                 else
270                         pfunc = (pmux >> offset) & 1;
271                 if (pfunc != function) {
272                         pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
273                                 ident, function, m, pfunc);
274                         return -EINVAL;
275                 }
276         }
277
278         return 0;
279 }
280
281 static void portmux_setup(unsigned short per)
282 {
283         u16 ident = P_IDENT(per);
284         u16 function = P_FUNCT2MUX(per);
285         s8 offset = port_mux[ident];
286         u16 pmux;
287
288         if (offset == -1)
289                 return;
290
291         pmux = bfin_read_PORT_MUX();
292         if (offset != 1)
293                 pmux &= ~(1 << offset);
294         else
295                 pmux &= ~(3 << 1);
296         pmux |= (function << offset);
297         bfin_write_PORT_MUX(pmux);
298 }
299 #elif defined(CONFIG_BF54x)
300 inline void portmux_setup(unsigned short per)
301 {
302         u16 ident = P_IDENT(per);
303         u16 function = P_FUNCT2MUX(per);
304         u32 pmux;
305
306         pmux = gpio_array[gpio_bank(ident)]->port_mux;
307
308         pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
309         pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
310
311         gpio_array[gpio_bank(ident)]->port_mux = pmux;
312 }
313
314 inline u16 get_portmux(unsigned short per)
315 {
316         u16 ident = P_IDENT(per);
317         u32 pmux = gpio_array[gpio_bank(ident)]->port_mux;
318         return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
319 }
320 static int portmux_group_check(unsigned short per)
321 {
322         return 0;
323 }
324 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
325 static int portmux_group_check(unsigned short per)
326 {
327         u16 ident = P_IDENT(per);
328         u16 function = P_FUNCT2MUX(per);
329         u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
330         u16 pin, gpiopin, pfunc;
331
332         for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
333                 if (offset != pmux_offset[gpio_bank(ident)][pin])
334                         continue;
335
336                 gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
337                 if (gpiopin == ident)
338                         continue;
339                 if (!is_reserved(peri, gpiopin, 1))
340                         continue;
341
342                 pfunc = *port_mux[gpio_bank(ident)];
343                 pfunc = (pfunc >> offset) & 3;
344                 if (pfunc != function) {
345                         pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
346                                 ident, function, gpiopin, pfunc);
347                         return -EINVAL;
348                 }
349         }
350
351         return 0;
352 }
353
354 inline void portmux_setup(unsigned short per)
355 {
356         u16 ident = P_IDENT(per);
357         u16 function = P_FUNCT2MUX(per);
358         u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
359         u16 pmux;
360
361         pmux = *port_mux[gpio_bank(ident)];
362         if  (((pmux >> offset) & 3) == function)
363                 return;
364         pmux &= ~(3 << offset);
365         pmux |= (function & 3) << offset;
366         *port_mux[gpio_bank(ident)] = pmux;
367         SSYNC();
368 }
369 #else
370 # define portmux_setup(...)  do { } while (0)
371 static int portmux_group_check(unsigned short per)
372 {
373         return 0;
374 }
375 #endif
376
377 #ifndef CONFIG_BF54x
378 /***********************************************************
379 *
380 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
381 *
382 * INPUTS/OUTPUTS:
383 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
384 *
385 *
386 * DESCRIPTION: These functions abstract direct register access
387 *              to Blackfin processor General Purpose
388 *              Ports Regsiters
389 *
390 * CAUTION: These functions do not belong to the GPIO Driver API
391 *************************************************************
392 * MODIFICATION HISTORY :
393 **************************************************************/
394
395 /* Set a specific bit */
396
397 #define SET_GPIO(name) \
398 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
399 { \
400         unsigned long flags; \
401         flags = hard_local_irq_save(); \
402         if (arg) \
403                 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
404         else \
405                 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
406         AWA_DUMMY_READ(name); \
407         hard_local_irq_restore(flags); \
408 } \
409 EXPORT_SYMBOL(set_gpio_ ## name);
410
411 SET_GPIO(dir)   /* set_gpio_dir() */
412 SET_GPIO(inen)  /* set_gpio_inen() */
413 SET_GPIO(polar) /* set_gpio_polar() */
414 SET_GPIO(edge)  /* set_gpio_edge() */
415 SET_GPIO(both)  /* set_gpio_both() */
416
417
418 #define SET_GPIO_SC(name) \
419 void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
420 { \
421         unsigned long flags; \
422         if (ANOMALY_05000311 || ANOMALY_05000323) \
423                 flags = hard_local_irq_save(); \
424         if (arg) \
425                 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
426         else \
427                 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
428         if (ANOMALY_05000311 || ANOMALY_05000323) { \
429                 AWA_DUMMY_READ(name); \
430                 hard_local_irq_restore(flags); \
431         } \
432 } \
433 EXPORT_SYMBOL(set_gpio_ ## name);
434
435 SET_GPIO_SC(maska)
436 SET_GPIO_SC(maskb)
437 SET_GPIO_SC(data)
438
439 void set_gpio_toggle(unsigned gpio)
440 {
441         unsigned long flags;
442         if (ANOMALY_05000311 || ANOMALY_05000323)
443                 flags = hard_local_irq_save();
444         gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
445         if (ANOMALY_05000311 || ANOMALY_05000323) {
446                 AWA_DUMMY_READ(toggle);
447                 hard_local_irq_restore(flags);
448         }
449 }
450 EXPORT_SYMBOL(set_gpio_toggle);
451
452
453 /*Set current PORT date (16-bit word)*/
454
455 #define SET_GPIO_P(name) \
456 void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
457 { \
458         unsigned long flags; \
459         if (ANOMALY_05000311 || ANOMALY_05000323) \
460                 flags = hard_local_irq_save(); \
461         gpio_array[gpio_bank(gpio)]->name = arg; \
462         if (ANOMALY_05000311 || ANOMALY_05000323) { \
463                 AWA_DUMMY_READ(name); \
464                 hard_local_irq_restore(flags); \
465         } \
466 } \
467 EXPORT_SYMBOL(set_gpiop_ ## name);
468
469 SET_GPIO_P(data)
470 SET_GPIO_P(dir)
471 SET_GPIO_P(inen)
472 SET_GPIO_P(polar)
473 SET_GPIO_P(edge)
474 SET_GPIO_P(both)
475 SET_GPIO_P(maska)
476 SET_GPIO_P(maskb)
477
478 /* Get a specific bit */
479 #define GET_GPIO(name) \
480 unsigned short get_gpio_ ## name(unsigned gpio) \
481 { \
482         unsigned long flags; \
483         unsigned short ret; \
484         if (ANOMALY_05000311 || ANOMALY_05000323) \
485                 flags = hard_local_irq_save(); \
486         ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
487         if (ANOMALY_05000311 || ANOMALY_05000323) { \
488                 AWA_DUMMY_READ(name); \
489                 hard_local_irq_restore(flags); \
490         } \
491         return ret; \
492 } \
493 EXPORT_SYMBOL(get_gpio_ ## name);
494
495 GET_GPIO(data)
496 GET_GPIO(dir)
497 GET_GPIO(inen)
498 GET_GPIO(polar)
499 GET_GPIO(edge)
500 GET_GPIO(both)
501 GET_GPIO(maska)
502 GET_GPIO(maskb)
503
504 /*Get current PORT date (16-bit word)*/
505
506 #define GET_GPIO_P(name) \
507 unsigned short get_gpiop_ ## name(unsigned gpio) \
508 { \
509         unsigned long flags; \
510         unsigned short ret; \
511         if (ANOMALY_05000311 || ANOMALY_05000323) \
512                 flags = hard_local_irq_save(); \
513         ret = (gpio_array[gpio_bank(gpio)]->name); \
514         if (ANOMALY_05000311 || ANOMALY_05000323) { \
515                 AWA_DUMMY_READ(name); \
516                 hard_local_irq_restore(flags); \
517         } \
518         return ret; \
519 } \
520 EXPORT_SYMBOL(get_gpiop_ ## name);
521
522 GET_GPIO_P(data)
523 GET_GPIO_P(dir)
524 GET_GPIO_P(inen)
525 GET_GPIO_P(polar)
526 GET_GPIO_P(edge)
527 GET_GPIO_P(both)
528 GET_GPIO_P(maska)
529 GET_GPIO_P(maskb)
530
531
532 #ifdef CONFIG_PM
533 DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
534
535 static const unsigned int sic_iwr_irqs[] = {
536 #if defined(BF533_FAMILY)
537         IRQ_PROG_INTB
538 #elif defined(BF537_FAMILY)
539         IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
540 #elif defined(BF538_FAMILY)
541         IRQ_PORTF_INTB
542 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
543         IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
544 #elif defined(BF561_FAMILY)
545         IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
546 #else
547 # error no SIC_IWR defined
548 #endif
549 };
550
551 /***********************************************************
552 *
553 * FUNCTIONS: Blackfin PM Setup API
554 *
555 * INPUTS/OUTPUTS:
556 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
557 * type -
558 *       PM_WAKE_RISING
559 *       PM_WAKE_FALLING
560 *       PM_WAKE_HIGH
561 *       PM_WAKE_LOW
562 *       PM_WAKE_BOTH_EDGES
563 *
564 * DESCRIPTION: Blackfin PM Driver API
565 *
566 * CAUTION:
567 *************************************************************
568 * MODIFICATION HISTORY :
569 **************************************************************/
570 int gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
571 {
572         unsigned long flags;
573
574         if (check_gpio(gpio) < 0)
575                 return -EINVAL;
576
577         flags = hard_local_irq_save();
578         if (ctrl)
579                 reserve(wakeup, gpio);
580         else
581                 unreserve(wakeup, gpio);
582
583         set_gpio_maskb(gpio, ctrl);
584         hard_local_irq_restore(flags);
585
586         return 0;
587 }
588
589 int bfin_pm_standby_ctrl(unsigned ctrl)
590 {
591         u16 bank, mask, i;
592
593         for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
594                 mask = map_entry(wakeup, i);
595                 bank = gpio_bank(i);
596
597                 if (mask)
598                         bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
599         }
600         return 0;
601 }
602
603 void bfin_gpio_pm_hibernate_suspend(void)
604 {
605         int i, bank;
606
607         for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
608                 bank = gpio_bank(i);
609
610 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
611                 gpio_bank_saved[bank].fer = *port_fer[bank];
612 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
613                 gpio_bank_saved[bank].mux = *port_mux[bank];
614 #else
615                 if (bank == 0)
616                         gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
617 #endif
618 #endif
619                 gpio_bank_saved[bank].data  = gpio_array[bank]->data;
620                 gpio_bank_saved[bank].inen  = gpio_array[bank]->inen;
621                 gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
622                 gpio_bank_saved[bank].dir   = gpio_array[bank]->dir;
623                 gpio_bank_saved[bank].edge  = gpio_array[bank]->edge;
624                 gpio_bank_saved[bank].both  = gpio_array[bank]->both;
625                 gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
626         }
627
628         AWA_DUMMY_READ(maska);
629 }
630
631 void bfin_gpio_pm_hibernate_restore(void)
632 {
633         int i, bank;
634
635         for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
636                 bank = gpio_bank(i);
637
638 #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
639 #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
640                 *port_mux[bank] = gpio_bank_saved[bank].mux;
641 #else
642                 if (bank == 0)
643                         bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
644 #endif
645                 *port_fer[bank] = gpio_bank_saved[bank].fer;
646 #endif
647                 gpio_array[bank]->inen  = gpio_bank_saved[bank].inen;
648                 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
649                                                 & gpio_bank_saved[bank].dir;
650                 gpio_array[bank]->dir   = gpio_bank_saved[bank].dir;
651                 gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
652                 gpio_array[bank]->edge  = gpio_bank_saved[bank].edge;
653                 gpio_array[bank]->both  = gpio_bank_saved[bank].both;
654                 gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
655         }
656         AWA_DUMMY_READ(maska);
657 }
658
659
660 #endif
661 #else /* CONFIG_BF54x */
662 #ifdef CONFIG_PM
663
664 int bfin_pm_standby_ctrl(unsigned ctrl)
665 {
666         return 0;
667 }
668
669 void bfin_gpio_pm_hibernate_suspend(void)
670 {
671         int i, bank;
672
673         for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
674                 bank = gpio_bank(i);
675
676                 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
677                 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
678                 gpio_bank_saved[bank].data = gpio_array[bank]->data;
679                 gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
680                 gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
681         }
682 }
683
684 void bfin_gpio_pm_hibernate_restore(void)
685 {
686         int i, bank;
687
688         for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
689                 bank = gpio_bank(i);
690
691                 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
692                 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
693                 gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
694                 gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
695                 gpio_array[bank]->data_set = gpio_bank_saved[bank].data
696                                                 | gpio_bank_saved[bank].dir;
697         }
698 }
699 #endif
700
701 unsigned short get_gpio_dir(unsigned gpio)
702 {
703         return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
704 }
705 EXPORT_SYMBOL(get_gpio_dir);
706
707 #endif /* CONFIG_BF54x */
708
709 /***********************************************************
710 *
711 * FUNCTIONS:    Blackfin Peripheral Resource Allocation
712 *               and PortMux Setup
713 *
714 * INPUTS/OUTPUTS:
715 * per   Peripheral Identifier
716 * label String
717 *
718 * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
719 *
720 * CAUTION:
721 *************************************************************
722 * MODIFICATION HISTORY :
723 **************************************************************/
724
725 int peripheral_request(unsigned short per, const char *label)
726 {
727         unsigned long flags;
728         unsigned short ident = P_IDENT(per);
729
730         /*
731          * Don't cares are pins with only one dedicated function
732          */
733
734         if (per & P_DONTCARE)
735                 return 0;
736
737         if (!(per & P_DEFINED))
738                 return -ENODEV;
739
740         BUG_ON(ident >= MAX_RESOURCES);
741
742         flags = hard_local_irq_save();
743
744         /* If a pin can be muxed as either GPIO or peripheral, make
745          * sure it is not already a GPIO pin when we request it.
746          */
747         if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
748                 if (system_state == SYSTEM_BOOTING)
749                         dump_stack();
750                 printk(KERN_ERR
751                        "%s: Peripheral %d is already reserved as GPIO by %s !\n",
752                        __func__, ident, get_label(ident));
753                 hard_local_irq_restore(flags);
754                 return -EBUSY;
755         }
756
757         if (unlikely(is_reserved(peri, ident, 1))) {
758
759                 /*
760                  * Pin functions like AMC address strobes my
761                  * be requested and used by several drivers
762                  */
763
764 #ifdef CONFIG_BF54x
765                 if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
766 #else
767                 if (!(per & P_MAYSHARE)) {
768 #endif
769                         /*
770                          * Allow that the identical pin function can
771                          * be requested from the same driver twice
772                          */
773
774                         if (cmp_label(ident, label) == 0)
775                                 goto anyway;
776
777                         if (system_state == SYSTEM_BOOTING)
778                                 dump_stack();
779                         printk(KERN_ERR
780                                "%s: Peripheral %d function %d is already reserved by %s !\n",
781                                __func__, ident, P_FUNCT2MUX(per), get_label(ident));
782                         hard_local_irq_restore(flags);
783                         return -EBUSY;
784                 }
785         }
786
787         if (unlikely(portmux_group_check(per))) {
788                 hard_local_irq_restore(flags);
789                 return -EBUSY;
790         }
791  anyway:
792         reserve(peri, ident);
793
794         portmux_setup(per);
795         port_setup(ident, PERIPHERAL_USAGE);
796
797         hard_local_irq_restore(flags);
798         set_label(ident, label);
799
800         return 0;
801 }
802 EXPORT_SYMBOL(peripheral_request);
803
804 int peripheral_request_list(const unsigned short per[], const char *label)
805 {
806         u16 cnt;
807         int ret;
808
809         for (cnt = 0; per[cnt] != 0; cnt++) {
810
811                 ret = peripheral_request(per[cnt], label);
812
813                 if (ret < 0) {
814                         for ( ; cnt > 0; cnt--)
815                                 peripheral_free(per[cnt - 1]);
816
817                         return ret;
818                 }
819         }
820
821         return 0;
822 }
823 EXPORT_SYMBOL(peripheral_request_list);
824
825 void peripheral_free(unsigned short per)
826 {
827         unsigned long flags;
828         unsigned short ident = P_IDENT(per);
829
830         if (per & P_DONTCARE)
831                 return;
832
833         if (!(per & P_DEFINED))
834                 return;
835
836         flags = hard_local_irq_save();
837
838         if (unlikely(!is_reserved(peri, ident, 0))) {
839                 hard_local_irq_restore(flags);
840                 return;
841         }
842
843         if (!(per & P_MAYSHARE))
844                 port_setup(ident, GPIO_USAGE);
845
846         unreserve(peri, ident);
847
848         set_label(ident, "free");
849
850         hard_local_irq_restore(flags);
851 }
852 EXPORT_SYMBOL(peripheral_free);
853
854 void peripheral_free_list(const unsigned short per[])
855 {
856         u16 cnt;
857         for (cnt = 0; per[cnt] != 0; cnt++)
858                 peripheral_free(per[cnt]);
859 }
860 EXPORT_SYMBOL(peripheral_free_list);
861
862 /***********************************************************
863 *
864 * FUNCTIONS: Blackfin GPIO Driver
865 *
866 * INPUTS/OUTPUTS:
867 * gpio  PIO Number between 0 and MAX_BLACKFIN_GPIOS
868 * label String
869 *
870 * DESCRIPTION: Blackfin GPIO Driver API
871 *
872 * CAUTION:
873 *************************************************************
874 * MODIFICATION HISTORY :
875 **************************************************************/
876
877 int bfin_gpio_request(unsigned gpio, const char *label)
878 {
879         unsigned long flags;
880
881         if (check_gpio(gpio) < 0)
882                 return -EINVAL;
883
884         flags = hard_local_irq_save();
885
886         /*
887          * Allow that the identical GPIO can
888          * be requested from the same driver twice
889          * Do nothing and return -
890          */
891
892         if (cmp_label(gpio, label) == 0) {
893                 hard_local_irq_restore(flags);
894                 return 0;
895         }
896
897         if (unlikely(is_reserved(gpio, gpio, 1))) {
898                 if (system_state == SYSTEM_BOOTING)
899                         dump_stack();
900                 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
901                        gpio, get_label(gpio));
902                 hard_local_irq_restore(flags);
903                 return -EBUSY;
904         }
905         if (unlikely(is_reserved(peri, gpio, 1))) {
906                 if (system_state == SYSTEM_BOOTING)
907                         dump_stack();
908                 printk(KERN_ERR
909                        "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
910                        gpio, get_label(gpio));
911                 hard_local_irq_restore(flags);
912                 return -EBUSY;
913         }
914         if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
915                 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
916                        " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
917         }
918 #ifndef CONFIG_BF54x
919         else {  /* Reset POLAR setting when acquiring a gpio for the first time */
920                 set_gpio_polar(gpio, 0);
921         }
922 #endif
923
924         reserve(gpio, gpio);
925         set_label(gpio, label);
926
927         hard_local_irq_restore(flags);
928
929         port_setup(gpio, GPIO_USAGE);
930
931         return 0;
932 }
933 EXPORT_SYMBOL(bfin_gpio_request);
934
935 void bfin_gpio_free(unsigned gpio)
936 {
937         unsigned long flags;
938
939         if (check_gpio(gpio) < 0)
940                 return;
941
942         might_sleep();
943
944         flags = hard_local_irq_save();
945
946         if (unlikely(!is_reserved(gpio, gpio, 0))) {
947                 if (system_state == SYSTEM_BOOTING)
948                         dump_stack();
949                 gpio_error(gpio);
950                 hard_local_irq_restore(flags);
951                 return;
952         }
953
954         unreserve(gpio, gpio);
955
956         set_label(gpio, "free");
957
958         hard_local_irq_restore(flags);
959 }
960 EXPORT_SYMBOL(bfin_gpio_free);
961
962 #ifdef BFIN_SPECIAL_GPIO_BANKS
963 DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
964
965 int bfin_special_gpio_request(unsigned gpio, const char *label)
966 {
967         unsigned long flags;
968
969         flags = hard_local_irq_save();
970
971         /*
972          * Allow that the identical GPIO can
973          * be requested from the same driver twice
974          * Do nothing and return -
975          */
976
977         if (cmp_label(gpio, label) == 0) {
978                 hard_local_irq_restore(flags);
979                 return 0;
980         }
981
982         if (unlikely(is_reserved(special_gpio, gpio, 1))) {
983                 hard_local_irq_restore(flags);
984                 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
985                        gpio, get_label(gpio));
986
987                 return -EBUSY;
988         }
989         if (unlikely(is_reserved(peri, gpio, 1))) {
990                 hard_local_irq_restore(flags);
991                 printk(KERN_ERR
992                        "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
993                        gpio, get_label(gpio));
994
995                 return -EBUSY;
996         }
997
998         reserve(special_gpio, gpio);
999         reserve(peri, gpio);
1000
1001         set_label(gpio, label);
1002         hard_local_irq_restore(flags);
1003         port_setup(gpio, GPIO_USAGE);
1004
1005         return 0;
1006 }
1007 EXPORT_SYMBOL(bfin_special_gpio_request);
1008
1009 void bfin_special_gpio_free(unsigned gpio)
1010 {
1011         unsigned long flags;
1012
1013         might_sleep();
1014
1015         flags = hard_local_irq_save();
1016
1017         if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
1018                 gpio_error(gpio);
1019                 hard_local_irq_restore(flags);
1020                 return;
1021         }
1022
1023         unreserve(special_gpio, gpio);
1024         unreserve(peri, gpio);
1025         set_label(gpio, "free");
1026         hard_local_irq_restore(flags);
1027 }
1028 EXPORT_SYMBOL(bfin_special_gpio_free);
1029 #endif
1030
1031
1032 int bfin_gpio_irq_request(unsigned gpio, const char *label)
1033 {
1034         unsigned long flags;
1035
1036         if (check_gpio(gpio) < 0)
1037                 return -EINVAL;
1038
1039         flags = hard_local_irq_save();
1040
1041         if (unlikely(is_reserved(peri, gpio, 1))) {
1042                 if (system_state == SYSTEM_BOOTING)
1043                         dump_stack();
1044                 printk(KERN_ERR
1045                        "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1046                        gpio, get_label(gpio));
1047                 hard_local_irq_restore(flags);
1048                 return -EBUSY;
1049         }
1050         if (unlikely(is_reserved(gpio, gpio, 1)))
1051                 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1052                        "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1053                        gpio, get_label(gpio));
1054
1055         reserve(gpio_irq, gpio);
1056         set_label(gpio, label);
1057
1058         hard_local_irq_restore(flags);
1059
1060         port_setup(gpio, GPIO_USAGE);
1061
1062         return 0;
1063 }
1064
1065 void bfin_gpio_irq_free(unsigned gpio)
1066 {
1067         unsigned long flags;
1068
1069         if (check_gpio(gpio) < 0)
1070                 return;
1071
1072         flags = hard_local_irq_save();
1073
1074         if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
1075                 if (system_state == SYSTEM_BOOTING)
1076                         dump_stack();
1077                 gpio_error(gpio);
1078                 hard_local_irq_restore(flags);
1079                 return;
1080         }
1081
1082         unreserve(gpio_irq, gpio);
1083
1084         set_label(gpio, "free");
1085
1086         hard_local_irq_restore(flags);
1087 }
1088
1089 static inline void __bfin_gpio_direction_input(unsigned gpio)
1090 {
1091 #ifdef CONFIG_BF54x
1092         gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
1093 #else
1094         gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1095 #endif
1096         gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1097 }
1098
1099 int bfin_gpio_direction_input(unsigned gpio)
1100 {
1101         unsigned long flags;
1102
1103         if (unlikely(!is_reserved(gpio, gpio, 0))) {
1104                 gpio_error(gpio);
1105                 return -EINVAL;
1106         }
1107
1108         flags = hard_local_irq_save();
1109         __bfin_gpio_direction_input(gpio);
1110         AWA_DUMMY_READ(inen);
1111         hard_local_irq_restore(flags);
1112
1113         return 0;
1114 }
1115 EXPORT_SYMBOL(bfin_gpio_direction_input);
1116
1117 void bfin_gpio_irq_prepare(unsigned gpio)
1118 {
1119 #ifdef CONFIG_BF54x
1120         unsigned long flags;
1121 #endif
1122
1123         port_setup(gpio, GPIO_USAGE);
1124
1125 #ifdef CONFIG_BF54x
1126         flags = hard_local_irq_save();
1127         __bfin_gpio_direction_input(gpio);
1128         hard_local_irq_restore(flags);
1129 #endif
1130 }
1131
1132 void bfin_gpio_set_value(unsigned gpio, int arg)
1133 {
1134         if (arg)
1135                 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1136         else
1137                 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1138 }
1139 EXPORT_SYMBOL(bfin_gpio_set_value);
1140
1141 int bfin_gpio_direction_output(unsigned gpio, int value)
1142 {
1143         unsigned long flags;
1144
1145         if (unlikely(!is_reserved(gpio, gpio, 0))) {
1146                 gpio_error(gpio);
1147                 return -EINVAL;
1148         }
1149
1150         flags = hard_local_irq_save();
1151
1152         gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1153         gpio_set_value(gpio, value);
1154 #ifdef CONFIG_BF54x
1155         gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
1156 #else
1157         gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1158 #endif
1159
1160         AWA_DUMMY_READ(dir);
1161         hard_local_irq_restore(flags);
1162
1163         return 0;
1164 }
1165 EXPORT_SYMBOL(bfin_gpio_direction_output);
1166
1167 int bfin_gpio_get_value(unsigned gpio)
1168 {
1169 #ifdef CONFIG_BF54x
1170         return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
1171 #else
1172         unsigned long flags;
1173
1174         if (unlikely(get_gpio_edge(gpio))) {
1175                 int ret;
1176                 flags = hard_local_irq_save();
1177                 set_gpio_edge(gpio, 0);
1178                 ret = get_gpio_data(gpio);
1179                 set_gpio_edge(gpio, 1);
1180                 hard_local_irq_restore(flags);
1181                 return ret;
1182         } else
1183                 return get_gpio_data(gpio);
1184 #endif
1185 }
1186 EXPORT_SYMBOL(bfin_gpio_get_value);
1187
1188 /* If we are booting from SPI and our board lacks a strong enough pull up,
1189  * the core can reset and execute the bootrom faster than the resistor can
1190  * pull the signal logically high.  To work around this (common) error in
1191  * board design, we explicitly set the pin back to GPIO mode, force /CS
1192  * high, and wait for the electrons to do their thing.
1193  *
1194  * This function only makes sense to be called from reset code, but it
1195  * lives here as we need to force all the GPIO states w/out going through
1196  * BUG() checks and such.
1197  */
1198 void bfin_reset_boot_spi_cs(unsigned short pin)
1199 {
1200         unsigned short gpio = P_IDENT(pin);
1201         port_setup(gpio, GPIO_USAGE);
1202         gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1203         AWA_DUMMY_READ(data_set);
1204         udelay(1);
1205 }
1206
1207 #if defined(CONFIG_PROC_FS)
1208 static int gpio_proc_show(struct seq_file *m, void *v)
1209 {
1210         int c, irq, gpio;
1211
1212         for (c = 0; c < MAX_RESOURCES; c++) {
1213                 irq = is_reserved(gpio_irq, c, 1);
1214                 gpio = is_reserved(gpio, c, 1);
1215                 if (!check_gpio(c) && (gpio || irq))
1216                         seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1217                                  get_label(c), (gpio && irq) ? " *" : "",
1218                                  get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1219                 else if (is_reserved(peri, c, 1))
1220                         seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1221                 else
1222                         continue;
1223         }
1224
1225         return 0;
1226 }
1227
1228 static int gpio_proc_open(struct inode *inode, struct file *file)
1229 {
1230         return single_open(file, gpio_proc_show, NULL);
1231 }
1232
1233 static const struct file_operations gpio_proc_ops = {
1234         .open           = gpio_proc_open,
1235         .read           = seq_read,
1236         .llseek         = seq_lseek,
1237         .release        = single_release,
1238 };
1239
1240 static __init int gpio_register_proc(void)
1241 {
1242         struct proc_dir_entry *proc_gpio;
1243
1244         proc_gpio = proc_create("gpio", S_IRUGO, NULL, &gpio_proc_ops);
1245         return proc_gpio != NULL;
1246 }
1247 __initcall(gpio_register_proc);
1248 #endif
1249
1250 #ifdef CONFIG_GPIOLIB
1251 static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1252 {
1253         return bfin_gpio_direction_input(gpio);
1254 }
1255
1256 static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1257 {
1258         return bfin_gpio_direction_output(gpio, level);
1259 }
1260
1261 static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1262 {
1263         return bfin_gpio_get_value(gpio);
1264 }
1265
1266 static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1267 {
1268         return bfin_gpio_set_value(gpio, value);
1269 }
1270
1271 static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1272 {
1273         return bfin_gpio_request(gpio, chip->label);
1274 }
1275
1276 static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1277 {
1278         return bfin_gpio_free(gpio);
1279 }
1280
1281 static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1282 {
1283         return gpio + GPIO_IRQ_BASE;
1284 }
1285
1286 static struct gpio_chip bfin_chip = {
1287         .label                  = "BFIN-GPIO",
1288         .direction_input        = bfin_gpiolib_direction_input,
1289         .get                    = bfin_gpiolib_get_value,
1290         .direction_output       = bfin_gpiolib_direction_output,
1291         .set                    = bfin_gpiolib_set_value,
1292         .request                = bfin_gpiolib_gpio_request,
1293         .free                   = bfin_gpiolib_gpio_free,
1294         .to_irq                 = bfin_gpiolib_gpio_to_irq,
1295         .base                   = 0,
1296         .ngpio                  = MAX_BLACKFIN_GPIOS,
1297 };
1298
1299 static int __init bfin_gpiolib_setup(void)
1300 {
1301         return gpiochip_add(&bfin_chip);
1302 }
1303 arch_initcall(bfin_gpiolib_setup);
1304 #endif