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