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