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