Blackfin arch: extract gpio number from PIN function
[pandora-kernel.git] / arch / blackfin / kernel / bfin_gpio.c
1 /*
2  * File:         arch/blackfin/kernel/bfin_gpio.c
3  * Based on:
4  * Author:       Michael Hennerich (hennerich@blackfin.uclinux.org)
5  *
6  * Created:
7  * Description:  GPIO Abstraction Layer
8  *
9  * Modified:
10  *               Copyright 2006 Analog Devices Inc.
11  *
12  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, see the file COPYING, or write
26  * to the Free Software Foundation, Inc.,
27  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28  */
29
30 /*
31 *  Number     BF537/6/4    BF561    BF533/2/1
32 *
33 *  GPIO_0       PF0         PF0        PF0
34 *  GPIO_1       PF1         PF1        PF1
35 *  GPIO_2       PF2         PF2        PF2
36 *  GPIO_3       PF3         PF3        PF3
37 *  GPIO_4       PF4         PF4        PF4
38 *  GPIO_5       PF5         PF5        PF5
39 *  GPIO_6       PF6         PF6        PF6
40 *  GPIO_7       PF7         PF7        PF7
41 *  GPIO_8       PF8         PF8        PF8
42 *  GPIO_9       PF9         PF9        PF9
43 *  GPIO_10      PF10        PF10       PF10
44 *  GPIO_11      PF11        PF11       PF11
45 *  GPIO_12      PF12        PF12       PF12
46 *  GPIO_13      PF13        PF13       PF13
47 *  GPIO_14      PF14        PF14       PF14
48 *  GPIO_15      PF15        PF15       PF15
49 *  GPIO_16      PG0         PF16
50 *  GPIO_17      PG1         PF17
51 *  GPIO_18      PG2         PF18
52 *  GPIO_19      PG3         PF19
53 *  GPIO_20      PG4         PF20
54 *  GPIO_21      PG5         PF21
55 *  GPIO_22      PG6         PF22
56 *  GPIO_23      PG7         PF23
57 *  GPIO_24      PG8         PF24
58 *  GPIO_25      PG9         PF25
59 *  GPIO_26      PG10        PF26
60 *  GPIO_27      PG11        PF27
61 *  GPIO_28      PG12        PF28
62 *  GPIO_29      PG13        PF29
63 *  GPIO_30      PG14        PF30
64 *  GPIO_31      PG15        PF31
65 *  GPIO_32      PH0         PF32
66 *  GPIO_33      PH1         PF33
67 *  GPIO_34      PH2         PF34
68 *  GPIO_35      PH3         PF35
69 *  GPIO_36      PH4         PF36
70 *  GPIO_37      PH5         PF37
71 *  GPIO_38      PH6         PF38
72 *  GPIO_39      PH7         PF39
73 *  GPIO_40      PH8         PF40
74 *  GPIO_41      PH9         PF41
75 *  GPIO_42      PH10        PF42
76 *  GPIO_43      PH11        PF43
77 *  GPIO_44      PH12        PF44
78 *  GPIO_45      PH13        PF45
79 *  GPIO_46      PH14        PF46
80 *  GPIO_47      PH15        PF47
81 */
82
83 #include <linux/delay.h>
84 #include <linux/module.h>
85 #include <linux/err.h>
86 #include <asm/blackfin.h>
87 #include <asm/gpio.h>
88 #include <asm/portmux.h>
89 #include <linux/irq.h>
90
91 #ifdef BF533_FAMILY
92 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
93         (struct gpio_port_t *) FIO_FLAG_D,
94 };
95 #endif
96
97 #ifdef BF537_FAMILY
98 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
99         (struct gpio_port_t *) PORTFIO,
100         (struct gpio_port_t *) PORTGIO,
101         (struct gpio_port_t *) PORTHIO,
102 };
103
104 static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
105         (unsigned short *) PORTF_FER,
106         (unsigned short *) PORTG_FER,
107         (unsigned short *) PORTH_FER,
108 };
109
110 #endif
111
112 #ifdef BF561_FAMILY
113 static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
114         (struct gpio_port_t *) FIO0_FLAG_D,
115         (struct gpio_port_t *) FIO1_FLAG_D,
116         (struct gpio_port_t *) FIO2_FLAG_D,
117 };
118 #endif
119
120 static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
121 static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)];
122
123 #define MAX_RESOURCES           256
124 #define RESOURCE_LABEL_SIZE     16
125
126 struct str_ident {
127         char name[RESOURCE_LABEL_SIZE];
128 } *str_ident;
129
130
131 #ifdef CONFIG_PM
132 static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
133 static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
134 static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
135
136 #ifdef BF533_FAMILY
137 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
138 #endif
139
140 #ifdef BF537_FAMILY
141 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
142 #endif
143
144 #ifdef BF561_FAMILY
145 static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
146 #endif
147
148 #endif /* CONFIG_PM */
149
150 inline int check_gpio(unsigned short gpio)
151 {
152         if (gpio >= MAX_BLACKFIN_GPIOS)
153                 return -EINVAL;
154         return 0;
155 }
156
157 static void set_label(unsigned short ident, const char *label)
158 {
159
160         if (label && str_ident) {
161                 strncpy(str_ident[ident].name, label,
162                          RESOURCE_LABEL_SIZE);
163                 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
164         }
165 }
166
167 static char *get_label(unsigned short ident)
168 {
169         if (!str_ident)
170                 return "UNKNOWN";
171
172         return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
173 }
174
175 static int cmp_label(unsigned short ident, const char *label)
176 {
177         if (label && str_ident)
178                 return strncmp(str_ident[ident].name,
179                                  label, strlen(label));
180         else
181                 return -EINVAL;
182 }
183
184 #ifdef BF537_FAMILY
185 static void port_setup(unsigned short gpio, unsigned short usage)
186 {
187         if (!check_gpio(gpio)) {
188                 if (usage == GPIO_USAGE) {
189                         *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
190                 } else
191                         *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
192                 SSYNC();
193         }
194 }
195 #else
196 # define port_setup(...)  do { } while (0)
197 #endif
198
199 #ifdef BF537_FAMILY
200
201 static struct {
202         unsigned short res;
203         unsigned short offset;
204 } port_mux_lut[] = {
205         {.res = P_PPI0_D13, .offset = 11},
206         {.res = P_PPI0_D14, .offset = 11},
207         {.res = P_PPI0_D15, .offset = 11},
208         {.res = P_SPORT1_TFS, .offset = 11},
209         {.res = P_SPORT1_TSCLK, .offset = 11},
210         {.res = P_SPORT1_DTPRI, .offset = 11},
211         {.res = P_PPI0_D10, .offset = 10},
212         {.res = P_PPI0_D11, .offset = 10},
213         {.res = P_PPI0_D12, .offset = 10},
214         {.res = P_SPORT1_RSCLK, .offset = 10},
215         {.res = P_SPORT1_RFS, .offset = 10},
216         {.res = P_SPORT1_DRPRI, .offset = 10},
217         {.res = P_PPI0_D8, .offset = 9},
218         {.res = P_PPI0_D9, .offset = 9},
219         {.res = P_SPORT1_DRSEC, .offset = 9},
220         {.res = P_SPORT1_DTSEC, .offset = 9},
221         {.res = P_TMR2, .offset = 8},
222         {.res = P_PPI0_FS3, .offset = 8},
223         {.res = P_TMR3, .offset = 7},
224         {.res = P_SPI0_SSEL4, .offset = 7},
225         {.res = P_TMR4, .offset = 6},
226         {.res = P_SPI0_SSEL5, .offset = 6},
227         {.res = P_TMR5, .offset = 5},
228         {.res = P_SPI0_SSEL6, .offset = 5},
229         {.res = P_UART1_RX, .offset = 4},
230         {.res = P_UART1_TX, .offset = 4},
231         {.res = P_TMR6, .offset = 4},
232         {.res = P_TMR7, .offset = 4},
233         {.res = P_UART0_RX, .offset = 3},
234         {.res = P_UART0_TX, .offset = 3},
235         {.res = P_DMAR0, .offset = 3},
236         {.res = P_DMAR1, .offset = 3},
237         {.res = P_SPORT0_DTSEC, .offset = 1},
238         {.res = P_SPORT0_DRSEC, .offset = 1},
239         {.res = P_CAN0_RX, .offset = 1},
240         {.res = P_CAN0_TX, .offset = 1},
241         {.res = P_SPI0_SSEL7, .offset = 1},
242         {.res = P_SPORT0_TFS, .offset = 0},
243         {.res = P_SPORT0_DTPRI, .offset = 0},
244         {.res = P_SPI0_SSEL2, .offset = 0},
245         {.res = P_SPI0_SSEL3, .offset = 0},
246 };
247
248 static void portmux_setup(unsigned short per, unsigned short function)
249 {
250         u16 y, offset, muxreg;
251
252         for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
253                 if (port_mux_lut[y].res == per) {
254
255                         /* SET PORTMUX REG */
256
257                         offset = port_mux_lut[y].offset;
258                         muxreg = bfin_read_PORT_MUX();
259
260                         if (offset != 1) {
261                                 muxreg &= ~(1 << offset);
262                         } else {
263                                 muxreg &= ~(3 << 1);
264                         }
265
266                         muxreg |= (function << offset);
267                         bfin_write_PORT_MUX(muxreg);
268                 }
269         }
270 }
271
272 #else
273 # define portmux_setup(...)  do { } while (0)
274 #endif
275
276 static void default_gpio(unsigned short gpio)
277 {
278         unsigned short bank, bitmask;
279
280         bank = gpio_bank(gpio);
281         bitmask = gpio_bit(gpio);
282
283         gpio_bankb[bank]->maska_clear = bitmask;
284         gpio_bankb[bank]->maskb_clear = bitmask;
285         SSYNC();
286         gpio_bankb[bank]->inen &= ~bitmask;
287         gpio_bankb[bank]->dir &= ~bitmask;
288         gpio_bankb[bank]->polar &= ~bitmask;
289         gpio_bankb[bank]->both &= ~bitmask;
290         gpio_bankb[bank]->edge &= ~bitmask;
291 }
292
293 static int __init bfin_gpio_init(void)
294 {
295         str_ident = kcalloc(MAX_RESOURCES,
296                                  sizeof(struct str_ident), GFP_KERNEL);
297         if (str_ident == NULL)
298                 return -ENOMEM;
299
300         memset(str_ident, 0, MAX_RESOURCES * sizeof(struct str_ident));
301
302         printk(KERN_INFO "Blackfin GPIO Controller\n");
303
304         return 0;
305
306 }
307 arch_initcall(bfin_gpio_init);
308
309
310 /***********************************************************
311 *
312 * FUNCTIONS: Blackfin General Purpose Ports Access Functions
313 *
314 * INPUTS/OUTPUTS:
315 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
316 *
317 *
318 * DESCRIPTION: These functions abstract direct register access
319 *              to Blackfin processor General Purpose
320 *              Ports Regsiters
321 *
322 * CAUTION: These functions do not belong to the GPIO Driver API
323 *************************************************************
324 * MODIFICATION HISTORY :
325 **************************************************************/
326
327 /* Set a specific bit */
328
329 #define SET_GPIO(name) \
330 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
331 { \
332         unsigned long flags; \
333         BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
334         local_irq_save(flags); \
335         if (arg) \
336                 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
337         else \
338                 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
339         local_irq_restore(flags); \
340 } \
341 EXPORT_SYMBOL(set_gpio_ ## name);
342
343 SET_GPIO(dir)
344 SET_GPIO(inen)
345 SET_GPIO(polar)
346 SET_GPIO(edge)
347 SET_GPIO(both)
348
349
350 #define SET_GPIO_SC(name) \
351 void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
352 { \
353         BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
354         if (arg) \
355                 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
356         else \
357                 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
358 } \
359 EXPORT_SYMBOL(set_gpio_ ## name);
360
361 SET_GPIO_SC(maska)
362 SET_GPIO_SC(maskb)
363
364 #if ANOMALY_05000311
365 void set_gpio_data(unsigned short gpio, unsigned short arg)
366 {
367         unsigned long flags;
368         BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
369         local_irq_save(flags);
370         if (arg)
371                 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
372         else
373                 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
374         bfin_read_CHIPID();
375         local_irq_restore(flags);
376 }
377 EXPORT_SYMBOL(set_gpio_data);
378 #else
379 SET_GPIO_SC(data)
380 #endif
381
382
383 #if ANOMALY_05000311
384 void set_gpio_toggle(unsigned short gpio)
385 {
386         unsigned long flags;
387         BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
388         local_irq_save(flags);
389         gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
390         bfin_read_CHIPID();
391         local_irq_restore(flags);
392 }
393 #else
394 void set_gpio_toggle(unsigned short gpio)
395 {
396         BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
397         gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
398 }
399 #endif
400 EXPORT_SYMBOL(set_gpio_toggle);
401
402
403 /*Set current PORT date (16-bit word)*/
404
405 #define SET_GPIO_P(name) \
406 void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
407 { \
408         gpio_bankb[gpio_bank(gpio)]->name = arg; \
409 } \
410 EXPORT_SYMBOL(set_gpiop_ ## name);
411
412 SET_GPIO_P(dir)
413 SET_GPIO_P(inen)
414 SET_GPIO_P(polar)
415 SET_GPIO_P(edge)
416 SET_GPIO_P(both)
417 SET_GPIO_P(maska)
418 SET_GPIO_P(maskb)
419
420
421 #if ANOMALY_05000311
422 void set_gpiop_data(unsigned short gpio, unsigned short arg)
423 {
424         unsigned long flags;
425         local_irq_save(flags);
426         gpio_bankb[gpio_bank(gpio)]->data = arg;
427         bfin_read_CHIPID();
428         local_irq_restore(flags);
429 }
430 EXPORT_SYMBOL(set_gpiop_data);
431 #else
432 SET_GPIO_P(data)
433 #endif
434
435
436
437 /* Get a specific bit */
438
439 #define GET_GPIO(name) \
440 unsigned short get_gpio_ ## name(unsigned short gpio) \
441 { \
442         return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
443 } \
444 EXPORT_SYMBOL(get_gpio_ ## name);
445
446 GET_GPIO(dir)
447 GET_GPIO(inen)
448 GET_GPIO(polar)
449 GET_GPIO(edge)
450 GET_GPIO(both)
451 GET_GPIO(maska)
452 GET_GPIO(maskb)
453
454
455 #if ANOMALY_05000311
456 unsigned short get_gpio_data(unsigned short gpio)
457 {
458         unsigned long flags;
459         unsigned short ret;
460         BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
461         local_irq_save(flags);
462         ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
463         bfin_read_CHIPID();
464         local_irq_restore(flags);
465         return ret;
466 }
467 EXPORT_SYMBOL(get_gpio_data);
468 #else
469 GET_GPIO(data)
470 #endif
471
472 /*Get current PORT date (16-bit word)*/
473
474 #define GET_GPIO_P(name) \
475 unsigned short get_gpiop_ ## name(unsigned short gpio) \
476 { \
477         return (gpio_bankb[gpio_bank(gpio)]->name);\
478 } \
479 EXPORT_SYMBOL(get_gpiop_ ## name);
480
481 GET_GPIO_P(dir)
482 GET_GPIO_P(inen)
483 GET_GPIO_P(polar)
484 GET_GPIO_P(edge)
485 GET_GPIO_P(both)
486 GET_GPIO_P(maska)
487 GET_GPIO_P(maskb)
488
489 #if ANOMALY_05000311
490 unsigned short get_gpiop_data(unsigned short gpio)
491 {
492         unsigned long flags;
493         unsigned short ret;
494         local_irq_save(flags);
495         ret = gpio_bankb[gpio_bank(gpio)]->data;
496         bfin_read_CHIPID();
497         local_irq_restore(flags);
498         return ret;
499 }
500 EXPORT_SYMBOL(get_gpiop_data);
501 #else
502 GET_GPIO_P(data)
503 #endif
504
505 #ifdef CONFIG_PM
506 /***********************************************************
507 *
508 * FUNCTIONS: Blackfin PM Setup API
509 *
510 * INPUTS/OUTPUTS:
511 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
512 * type -
513 *       PM_WAKE_RISING
514 *       PM_WAKE_FALLING
515 *       PM_WAKE_HIGH
516 *       PM_WAKE_LOW
517 *       PM_WAKE_BOTH_EDGES
518 *
519 * DESCRIPTION: Blackfin PM Driver API
520 *
521 * CAUTION:
522 *************************************************************
523 * MODIFICATION HISTORY :
524 **************************************************************/
525 int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type)
526 {
527         unsigned long flags;
528
529         if ((check_gpio(gpio) < 0) || !type)
530                 return -EINVAL;
531
532         local_irq_save(flags);
533
534         wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
535         wakeup_flags_map[gpio] = type;
536         local_irq_restore(flags);
537
538         return 0;
539 }
540 EXPORT_SYMBOL(gpio_pm_wakeup_request);
541
542 void gpio_pm_wakeup_free(unsigned short gpio)
543 {
544         unsigned long flags;
545
546         if (check_gpio(gpio) < 0)
547                 return;
548
549         local_irq_save(flags);
550
551         wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
552
553         local_irq_restore(flags);
554 }
555 EXPORT_SYMBOL(gpio_pm_wakeup_free);
556
557 static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type)
558 {
559         port_setup(gpio, GPIO_USAGE);
560         set_gpio_dir(gpio, 0);
561         set_gpio_inen(gpio, 1);
562
563         if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
564                 set_gpio_edge(gpio, 1);
565          else
566                 set_gpio_edge(gpio, 0);
567
568         if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
569                 set_gpio_both(gpio, 1);
570         else
571                 set_gpio_both(gpio, 0);
572
573         if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
574                 set_gpio_polar(gpio, 1);
575         else
576                 set_gpio_polar(gpio, 0);
577
578         SSYNC();
579
580         return 0;
581 }
582
583 u32 gpio_pm_setup(void)
584 {
585         u32 sic_iwr = 0;
586         u16 bank, mask, i, gpio;
587
588         for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
589                 mask = wakeup_map[gpio_bank(i)];
590                 bank = gpio_bank(i);
591
592                 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
593                 gpio_bankb[bank]->maskb = 0;
594
595                 if (mask) {
596 #ifdef BF537_FAMILY
597                         gpio_bank_saved[bank].fer   = *port_fer[bank];
598 #endif
599                         gpio_bank_saved[bank].inen  = gpio_bankb[bank]->inen;
600                         gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
601                         gpio_bank_saved[bank].dir   = gpio_bankb[bank]->dir;
602                         gpio_bank_saved[bank].edge  = gpio_bankb[bank]->edge;
603                         gpio_bank_saved[bank].both  = gpio_bankb[bank]->both;
604                         gpio_bank_saved[bank].reserved =
605                                                 reserved_gpio_map[bank];
606
607                         gpio = i;
608
609                         while (mask) {
610                                 if (mask & 1) {
611                                         reserved_gpio_map[gpio_bank(gpio)] |=
612                                                         gpio_bit(gpio);
613                                         bfin_gpio_wakeup_type(gpio,
614                                                 wakeup_flags_map[gpio]);
615                                         set_gpio_data(gpio, 0); /*Clear*/
616                                 }
617                                 gpio++;
618                                 mask >>= 1;
619                         }
620
621                         sic_iwr |= 1 <<
622                                 (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
623                         gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
624                 }
625         }
626
627         if (sic_iwr)
628                 return sic_iwr;
629         else
630                 return IWR_ENABLE_ALL;
631 }
632
633 void gpio_pm_restore(void)
634 {
635         u16 bank, mask, i;
636
637         for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
638                 mask = wakeup_map[gpio_bank(i)];
639                 bank = gpio_bank(i);
640
641                 if (mask) {
642 #ifdef BF537_FAMILY
643                         *port_fer[bank]         = gpio_bank_saved[bank].fer;
644 #endif
645                         gpio_bankb[bank]->inen  = gpio_bank_saved[bank].inen;
646                         gpio_bankb[bank]->dir   = gpio_bank_saved[bank].dir;
647                         gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
648                         gpio_bankb[bank]->edge  = gpio_bank_saved[bank].edge;
649                         gpio_bankb[bank]->both  = gpio_bank_saved[bank].both;
650
651                         reserved_gpio_map[bank] =
652                                         gpio_bank_saved[bank].reserved;
653
654                 }
655
656                 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
657         }
658 }
659
660 #endif
661
662
663
664
665 int peripheral_request(unsigned short per, const char *label)
666 {
667         unsigned long flags;
668         unsigned short ident = P_IDENT(per);
669
670         /*
671          * Don't cares are pins with only one dedicated function
672          */
673
674         if (per & P_DONTCARE)
675                 return 0;
676
677         if (!(per & P_DEFINED))
678                 return -ENODEV;
679
680         local_irq_save(flags);
681
682         if (!check_gpio(ident)) {
683
684         if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
685                 printk(KERN_ERR
686                        "%s: Peripheral %d is already reserved as GPIO by %s !\n",
687                        __FUNCTION__, ident, get_label(ident));
688                 dump_stack();
689                 local_irq_restore(flags);
690                 return -EBUSY;
691         }
692
693         }
694
695         if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
696
697         /*
698          * Pin functions like AMC address strobes my
699          * be requested and used by several drivers
700          */
701
702         if (!(per & P_MAYSHARE)) {
703
704         /*
705          * Allow that the identical pin function can
706          * be requested from the same driver twice
707          */
708
709                 if (cmp_label(ident, label) == 0)
710                         goto anyway;
711
712                         printk(KERN_ERR
713                                "%s: Peripheral %d function %d is already"
714                                " reserved by %s !\n",
715                                __FUNCTION__, ident, P_FUNCT2MUX(per),
716                                 get_label(ident));
717                         dump_stack();
718                         local_irq_restore(flags);
719                         return -EBUSY;
720                 }
721
722         }
723
724 anyway:
725
726
727         portmux_setup(per, P_FUNCT2MUX(per));
728
729         port_setup(ident, PERIPHERAL_USAGE);
730
731         reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
732         local_irq_restore(flags);
733         set_label(ident, label);
734
735         return 0;
736 }
737 EXPORT_SYMBOL(peripheral_request);
738
739 int peripheral_request_list(unsigned short per[], const char *label)
740 {
741         u16 cnt;
742         int ret;
743
744         for (cnt = 0; per[cnt] != 0; cnt++) {
745
746                 ret = peripheral_request(per[cnt], label);
747
748                 if (ret < 0) {
749                         for ( ; cnt > 0; cnt--) {
750                                 peripheral_free(per[cnt - 1]);
751                         }
752                 return ret;
753                 }
754         }
755
756         return 0;
757 }
758 EXPORT_SYMBOL(peripheral_request_list);
759
760 void peripheral_free(unsigned short per)
761 {
762         unsigned long flags;
763         unsigned short ident = P_IDENT(per);
764
765         if (per & P_DONTCARE)
766                 return;
767
768         if (!(per & P_DEFINED))
769                 return;
770
771         if (check_gpio(ident) < 0)
772                 return;
773
774         local_irq_save(flags);
775
776         if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
777                          & gpio_bit(ident)))) {
778                 local_irq_restore(flags);
779                 return;
780         }
781
782         if (!(per & P_MAYSHARE)) {
783                 port_setup(ident, GPIO_USAGE);
784         }
785
786         reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
787
788         local_irq_restore(flags);
789 }
790 EXPORT_SYMBOL(peripheral_free);
791
792 void peripheral_free_list(unsigned short per[])
793 {
794         u16 cnt;
795
796         for (cnt = 0; per[cnt] != 0; cnt++) {
797                 peripheral_free(per[cnt]);
798         }
799
800 }
801 EXPORT_SYMBOL(peripheral_free_list);
802
803 /***********************************************************
804 *
805 * FUNCTIONS: Blackfin GPIO Driver
806 *
807 * INPUTS/OUTPUTS:
808 * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
809 *
810 *
811 * DESCRIPTION: Blackfin GPIO Driver API
812 *
813 * CAUTION:
814 *************************************************************
815 * MODIFICATION HISTORY :
816 **************************************************************/
817
818 int gpio_request(unsigned short gpio, const char *label)
819 {
820         unsigned long flags;
821
822         if (check_gpio(gpio) < 0)
823                 return -EINVAL;
824
825         local_irq_save(flags);
826
827         if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
828                 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
829                 dump_stack();
830                 local_irq_restore(flags);
831                 return -EBUSY;
832         }
833         reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
834
835         local_irq_restore(flags);
836
837         port_setup(gpio, GPIO_USAGE);
838
839         return 0;
840 }
841 EXPORT_SYMBOL(gpio_request);
842
843 void gpio_free(unsigned short gpio)
844 {
845         unsigned long flags;
846
847         if (check_gpio(gpio) < 0)
848                 return;
849
850         local_irq_save(flags);
851
852         if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
853                 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
854                 dump_stack();
855                 local_irq_restore(flags);
856                 return;
857         }
858
859         default_gpio(gpio);
860
861         reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
862
863         local_irq_restore(flags);
864 }
865 EXPORT_SYMBOL(gpio_free);
866
867 void gpio_direction_input(unsigned short gpio)
868 {
869         unsigned long flags;
870
871         BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
872
873         local_irq_save(flags);
874         gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
875         gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
876         local_irq_restore(flags);
877 }
878 EXPORT_SYMBOL(gpio_direction_input);
879
880 void gpio_direction_output(unsigned short gpio)
881 {
882         unsigned long flags;
883
884         BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
885
886         local_irq_save(flags);
887         gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
888         gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
889         local_irq_restore(flags);
890 }
891 EXPORT_SYMBOL(gpio_direction_output);
892
893 /* If we are booting from SPI and our board lacks a strong enough pull up,
894  * the core can reset and execute the bootrom faster than the resistor can
895  * pull the signal logically high.  To work around this (common) error in
896  * board design, we explicitly set the pin back to GPIO mode, force /CS
897  * high, and wait for the electrons to do their thing.
898  *
899  * This function only makes sense to be called from reset code, but it
900  * lives here as we need to force all the GPIO states w/out going through
901  * BUG() checks and such.
902  */
903 void bfin_gpio_reset_spi0_ssel1(void)
904 {
905         u16 gpio = P_IDENT(P_SPI0_SSEL1);
906
907         port_setup(gpio, GPIO_USAGE);
908         gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
909         udelay(1);
910 }