blob: 979cf79d379955d5dac3c924da06a7021b1e4fe9 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
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/module.h>
84#include <linux/err.h>
85#include <asm/blackfin.h>
86#include <asm/gpio.h>
Michael Hennerichc58c2142007-10-04 00:35:05 +080087#include <asm/portmux.h>
Bryan Wu1394f032007-05-06 14:50:22 -070088#include <linux/irq.h>
89
90#ifdef BF533_FAMILY
91static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
92 (struct gpio_port_t *) FIO_FLAG_D,
93};
94#endif
95
96#ifdef BF537_FAMILY
97static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
98 (struct gpio_port_t *) PORTFIO,
99 (struct gpio_port_t *) PORTGIO,
100 (struct gpio_port_t *) PORTHIO,
101};
102
103static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
104 (unsigned short *) PORTF_FER,
105 (unsigned short *) PORTG_FER,
106 (unsigned short *) PORTH_FER,
107};
108
109#endif
110
111#ifdef BF561_FAMILY
112static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
113 (struct gpio_port_t *) FIO0_FLAG_D,
114 (struct gpio_port_t *) FIO1_FLAG_D,
115 (struct gpio_port_t *) FIO2_FLAG_D,
116};
117#endif
118
Michael Hennerichc58c2142007-10-04 00:35:05 +0800119static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
120static unsigned short reserved_peri_map[gpio_bank(MAX_BLACKFIN_GPIOS + 16)];
121char *str_ident = NULL;
122
123#define RESOURCE_LABEL_SIZE 16
Bryan Wu1394f032007-05-06 14:50:22 -0700124
125#ifdef CONFIG_PM
126static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
127static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
128static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
129
130#ifdef BF533_FAMILY
131static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
132#endif
133
134#ifdef BF537_FAMILY
135static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
136#endif
137
138#ifdef BF561_FAMILY
139static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
140#endif
141
142#endif /* CONFIG_PM */
143
144inline int check_gpio(unsigned short gpio)
145{
Michael Henneriche7613aa2007-06-11 16:37:57 +0800146 if (gpio >= MAX_BLACKFIN_GPIOS)
Bryan Wu1394f032007-05-06 14:50:22 -0700147 return -EINVAL;
148 return 0;
149}
150
Michael Hennerichc58c2142007-10-04 00:35:05 +0800151static void set_label(unsigned short ident, const char *label)
152{
153
154 if (label && str_ident) {
155 strncpy(str_ident + ident * RESOURCE_LABEL_SIZE, label,
156 RESOURCE_LABEL_SIZE);
157 str_ident[ident * RESOURCE_LABEL_SIZE +
158 RESOURCE_LABEL_SIZE - 1] = 0;
159 }
160}
161
162static char *get_label(unsigned short ident)
163{
164 if (!str_ident)
165 return "UNKNOWN";
166
167 return (str_ident[ident * RESOURCE_LABEL_SIZE] ?
168 (str_ident + ident * RESOURCE_LABEL_SIZE) : "UNKNOWN");
169}
170
171static int cmp_label(unsigned short ident, const char *label)
172{
173 if (label && str_ident)
174 return strncmp(str_ident + ident * RESOURCE_LABEL_SIZE,
175 label, strlen(label));
176 else
177 return -EINVAL;
178}
179
Bryan Wu1394f032007-05-06 14:50:22 -0700180#ifdef BF537_FAMILY
Mike Frysingera161bb02007-05-21 18:09:14 +0800181static void port_setup(unsigned short gpio, unsigned short usage)
Bryan Wu1394f032007-05-06 14:50:22 -0700182{
Michael Hennerichcda6a202007-10-04 00:36:18 +0800183 if (!check_gpio(gpio)) {
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 }
Bryan Wu1394f032007-05-06 14:50:22 -0700190}
191#else
192# define port_setup(...) do { } while (0)
193#endif
194
Michael Hennerichc58c2142007-10-04 00:35:05 +0800195#ifdef BF537_FAMILY
196
197#define PMUX_LUT_RES 0
198#define PMUX_LUT_OFFSET 1
199#define PMUX_LUT_ENTRIES 41
200#define PMUX_LUT_SIZE 2
201
202static unsigned short port_mux_lut[PMUX_LUT_ENTRIES][PMUX_LUT_SIZE] = {
203 {P_PPI0_D13, 11}, {P_PPI0_D14, 11}, {P_PPI0_D15, 11},
204 {P_SPORT1_TFS, 11}, {P_SPORT1_TSCLK, 11}, {P_SPORT1_DTPRI, 11},
205 {P_PPI0_D10, 10}, {P_PPI0_D11, 10}, {P_PPI0_D12, 10},
206 {P_SPORT1_RSCLK, 10}, {P_SPORT1_RFS, 10}, {P_SPORT1_DRPRI, 10},
207 {P_PPI0_D8, 9}, {P_PPI0_D9, 9}, {P_SPORT1_DRSEC, 9},
208 {P_SPORT1_DTSEC, 9}, {P_TMR2, 8}, {P_PPI0_FS3, 8}, {P_TMR3, 7},
209 {P_SPI0_SSEL4, 7}, {P_TMR4, 6}, {P_SPI0_SSEL5, 6}, {P_TMR5, 5},
210 {P_SPI0_SSEL6, 5}, {P_UART1_RX, 4}, {P_UART1_TX, 4}, {P_TMR6, 4},
211 {P_TMR7, 4}, {P_UART0_RX, 3}, {P_UART0_TX, 3}, {P_DMAR0, 3},
212 {P_DMAR1, 3}, {P_SPORT0_DTSEC, 1}, {P_SPORT0_DRSEC, 1},
213 {P_CAN0_RX, 1}, {P_CAN0_TX, 1}, {P_SPI0_SSEL7, 1},
214 {P_SPORT0_TFS, 0}, {P_SPORT0_DTPRI, 0}, {P_SPI0_SSEL2, 0},
215 {P_SPI0_SSEL3, 0}
216};
217
218static void portmux_setup(unsigned short per, unsigned short function)
219{
220 u16 y, muxreg, offset;
221
222 for (y = 0; y < PMUX_LUT_ENTRIES; y++) {
223 if (port_mux_lut[y][PMUX_LUT_RES] == per) {
224
225 /* SET PORTMUX REG */
226
227 offset = port_mux_lut[y][PMUX_LUT_OFFSET];
228 muxreg = bfin_read_PORT_MUX();
229
230 if (offset != 1) {
231 muxreg &= ~(1 << offset);
232 } else {
233 muxreg &= ~(3 << 1);
234 }
235
236 muxreg |= (function << offset);
237 bfin_write_PORT_MUX(muxreg);
238 }
239 }
240}
241
242#else
243# define portmux_setup(...) do { } while (0)
244#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700245
Mike Frysingera161bb02007-05-21 18:09:14 +0800246static void default_gpio(unsigned short gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700247{
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800248 unsigned short bank, bitmask;
Bryan Wu1394f032007-05-06 14:50:22 -0700249
250 bank = gpio_bank(gpio);
251 bitmask = gpio_bit(gpio);
252
253 gpio_bankb[bank]->maska_clear = bitmask;
254 gpio_bankb[bank]->maskb_clear = bitmask;
255 SSYNC();
256 gpio_bankb[bank]->inen &= ~bitmask;
257 gpio_bankb[bank]->dir &= ~bitmask;
258 gpio_bankb[bank]->polar &= ~bitmask;
259 gpio_bankb[bank]->both &= ~bitmask;
260 gpio_bankb[bank]->edge &= ~bitmask;
261}
262
Mike Frysingera161bb02007-05-21 18:09:14 +0800263static int __init bfin_gpio_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700264{
Michael Hennerichc58c2142007-10-04 00:35:05 +0800265
266 str_ident = kzalloc(RESOURCE_LABEL_SIZE * 256, GFP_KERNEL);
267 if (!str_ident)
268 return -ENOMEM;
Bryan Wu1394f032007-05-06 14:50:22 -0700269
270 printk(KERN_INFO "Blackfin GPIO Controller\n");
271
Bryan Wu1394f032007-05-06 14:50:22 -0700272 return 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800273
Bryan Wu1394f032007-05-06 14:50:22 -0700274}
275
276arch_initcall(bfin_gpio_init);
277
278
279/***********************************************************
280*
281* FUNCTIONS: Blackfin General Purpose Ports Access Functions
282*
283* INPUTS/OUTPUTS:
284* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
285*
286*
287* DESCRIPTION: These functions abstract direct register access
288* to Blackfin processor General Purpose
289* Ports Regsiters
290*
291* CAUTION: These functions do not belong to the GPIO Driver API
292*************************************************************
293* MODIFICATION HISTORY :
294**************************************************************/
295
296/* Set a specific bit */
297
298#define SET_GPIO(name) \
299void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
300{ \
301 unsigned long flags; \
Michael Hennerichc58c2142007-10-04 00:35:05 +0800302 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
Bryan Wu1394f032007-05-06 14:50:22 -0700303 local_irq_save(flags); \
304 if (arg) \
305 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
306 else \
307 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
308 local_irq_restore(flags); \
309} \
310EXPORT_SYMBOL(set_gpio_ ## name);
311
312SET_GPIO(dir)
313SET_GPIO(inen)
314SET_GPIO(polar)
315SET_GPIO(edge)
316SET_GPIO(both)
317
318
319#define SET_GPIO_SC(name) \
320void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
321{ \
Michael Hennerichc58c2142007-10-04 00:35:05 +0800322 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
Bryan Wu1394f032007-05-06 14:50:22 -0700323 if (arg) \
324 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
325 else \
326 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
327} \
328EXPORT_SYMBOL(set_gpio_ ## name);
329
330SET_GPIO_SC(maska)
331SET_GPIO_SC(maskb)
332
Mike Frysinger1aafd902007-07-25 11:19:14 +0800333#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700334void set_gpio_data(unsigned short gpio, unsigned short arg)
335{
336 unsigned long flags;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800337 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700338 local_irq_save(flags);
339 if (arg)
340 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
341 else
342 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
343 bfin_read_CHIPID();
344 local_irq_restore(flags);
345}
346EXPORT_SYMBOL(set_gpio_data);
347#else
348SET_GPIO_SC(data)
349#endif
350
351
Mike Frysinger1aafd902007-07-25 11:19:14 +0800352#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700353void set_gpio_toggle(unsigned short gpio)
354{
355 unsigned long flags;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800356 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700357 local_irq_save(flags);
358 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
359 bfin_read_CHIPID();
360 local_irq_restore(flags);
361}
362#else
363void set_gpio_toggle(unsigned short gpio)
364{
Michael Hennerichc58c2142007-10-04 00:35:05 +0800365 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700366 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
367}
368#endif
369EXPORT_SYMBOL(set_gpio_toggle);
370
371
372/*Set current PORT date (16-bit word)*/
373
374#define SET_GPIO_P(name) \
375void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
376{ \
377 gpio_bankb[gpio_bank(gpio)]->name = arg; \
378} \
379EXPORT_SYMBOL(set_gpiop_ ## name);
380
381SET_GPIO_P(dir)
382SET_GPIO_P(inen)
383SET_GPIO_P(polar)
384SET_GPIO_P(edge)
385SET_GPIO_P(both)
386SET_GPIO_P(maska)
387SET_GPIO_P(maskb)
388
389
Mike Frysinger1aafd902007-07-25 11:19:14 +0800390#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700391void set_gpiop_data(unsigned short gpio, unsigned short arg)
392{
393 unsigned long flags;
394 local_irq_save(flags);
395 gpio_bankb[gpio_bank(gpio)]->data = arg;
396 bfin_read_CHIPID();
397 local_irq_restore(flags);
398}
399EXPORT_SYMBOL(set_gpiop_data);
400#else
401SET_GPIO_P(data)
402#endif
403
404
405
406/* Get a specific bit */
407
408#define GET_GPIO(name) \
409unsigned short get_gpio_ ## name(unsigned short gpio) \
410{ \
411 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
412} \
413EXPORT_SYMBOL(get_gpio_ ## name);
414
415GET_GPIO(dir)
416GET_GPIO(inen)
417GET_GPIO(polar)
418GET_GPIO(edge)
419GET_GPIO(both)
420GET_GPIO(maska)
421GET_GPIO(maskb)
422
423
Mike Frysinger1aafd902007-07-25 11:19:14 +0800424#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700425unsigned short get_gpio_data(unsigned short gpio)
426{
427 unsigned long flags;
428 unsigned short ret;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800429 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700430 local_irq_save(flags);
431 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->data >> gpio_sub_n(gpio));
432 bfin_read_CHIPID();
433 local_irq_restore(flags);
434 return ret;
435}
436EXPORT_SYMBOL(get_gpio_data);
437#else
438GET_GPIO(data)
439#endif
440
441/*Get current PORT date (16-bit word)*/
442
443#define GET_GPIO_P(name) \
444unsigned short get_gpiop_ ## name(unsigned short gpio) \
445{ \
446 return (gpio_bankb[gpio_bank(gpio)]->name);\
447} \
448EXPORT_SYMBOL(get_gpiop_ ## name);
449
450GET_GPIO_P(dir)
451GET_GPIO_P(inen)
452GET_GPIO_P(polar)
453GET_GPIO_P(edge)
454GET_GPIO_P(both)
455GET_GPIO_P(maska)
456GET_GPIO_P(maskb)
457
Mike Frysinger1aafd902007-07-25 11:19:14 +0800458#if ANOMALY_05000311
Bryan Wu1394f032007-05-06 14:50:22 -0700459unsigned short get_gpiop_data(unsigned short gpio)
460{
461 unsigned long flags;
462 unsigned short ret;
463 local_irq_save(flags);
464 ret = gpio_bankb[gpio_bank(gpio)]->data;
465 bfin_read_CHIPID();
466 local_irq_restore(flags);
467 return ret;
468}
469EXPORT_SYMBOL(get_gpiop_data);
470#else
471GET_GPIO_P(data)
472#endif
473
474#ifdef CONFIG_PM
475/***********************************************************
476*
477* FUNCTIONS: Blackfin PM Setup API
478*
479* INPUTS/OUTPUTS:
480* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
481* type -
482* PM_WAKE_RISING
483* PM_WAKE_FALLING
484* PM_WAKE_HIGH
485* PM_WAKE_LOW
486* PM_WAKE_BOTH_EDGES
487*
488* DESCRIPTION: Blackfin PM Driver API
489*
490* CAUTION:
491*************************************************************
492* MODIFICATION HISTORY :
493**************************************************************/
494int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type)
495{
496 unsigned long flags;
497
498 if ((check_gpio(gpio) < 0) || !type)
499 return -EINVAL;
500
501 local_irq_save(flags);
502
503 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
504 wakeup_flags_map[gpio] = type;
505 local_irq_restore(flags);
506
507 return 0;
508}
509EXPORT_SYMBOL(gpio_pm_wakeup_request);
510
511void gpio_pm_wakeup_free(unsigned short gpio)
512{
513 unsigned long flags;
514
515 if (check_gpio(gpio) < 0)
516 return;
517
518 local_irq_save(flags);
519
520 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
521
522 local_irq_restore(flags);
523}
524EXPORT_SYMBOL(gpio_pm_wakeup_free);
525
526static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type)
527{
528 port_setup(gpio, GPIO_USAGE);
529 set_gpio_dir(gpio, 0);
530 set_gpio_inen(gpio, 1);
531
532 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
533 set_gpio_edge(gpio, 1);
534 else
535 set_gpio_edge(gpio, 0);
536
537 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
538 set_gpio_both(gpio, 1);
539 else
540 set_gpio_both(gpio, 0);
541
542 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
543 set_gpio_polar(gpio, 1);
544 else
545 set_gpio_polar(gpio, 0);
546
547 SSYNC();
548
549 return 0;
550}
551
552u32 gpio_pm_setup(void)
553{
554 u32 sic_iwr = 0;
555 u16 bank, mask, i, gpio;
556
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800557 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700558 mask = wakeup_map[gpio_bank(i)];
559 bank = gpio_bank(i);
560
561 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
562 gpio_bankb[bank]->maskb = 0;
563
564 if (mask) {
565#ifdef BF537_FAMILY
566 gpio_bank_saved[bank].fer = *port_fer[bank];
567#endif
568 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
569 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
570 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
571 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
572 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800573 gpio_bank_saved[bank].reserved =
574 reserved_gpio_map[bank];
Bryan Wu1394f032007-05-06 14:50:22 -0700575
576 gpio = i;
577
578 while (mask) {
579 if (mask & 1) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800580 reserved_gpio_map[gpio_bank(gpio)] |=
Michael Hennerich581d62a2007-06-14 13:30:23 +0800581 gpio_bit(gpio);
582 bfin_gpio_wakeup_type(gpio,
583 wakeup_flags_map[gpio]);
Bryan Wu1394f032007-05-06 14:50:22 -0700584 set_gpio_data(gpio, 0); /*Clear*/
585 }
586 gpio++;
587 mask >>= 1;
588 }
589
Michael Hennerich581d62a2007-06-14 13:30:23 +0800590 sic_iwr |= 1 <<
591 (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
Bryan Wu1394f032007-05-06 14:50:22 -0700592 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
593 }
594 }
595
596 if (sic_iwr)
597 return sic_iwr;
598 else
599 return IWR_ENABLE_ALL;
600}
601
Bryan Wu1394f032007-05-06 14:50:22 -0700602void gpio_pm_restore(void)
603{
604 u16 bank, mask, i;
605
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800606 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700607 mask = wakeup_map[gpio_bank(i)];
608 bank = gpio_bank(i);
609
610 if (mask) {
611#ifdef BF537_FAMILY
612 *port_fer[bank] = gpio_bank_saved[bank].fer;
613#endif
614 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
615 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
616 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
617 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
618 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800619
Michael Hennerichc58c2142007-10-04 00:35:05 +0800620 reserved_gpio_map[bank] =
621 gpio_bank_saved[bank].reserved;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800622
Bryan Wu1394f032007-05-06 14:50:22 -0700623 }
624
625 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
626 }
627}
628
629#endif
630
Michael Hennerichc58c2142007-10-04 00:35:05 +0800631
632
633
634int peripheral_request(unsigned short per, const char *label)
635{
636 unsigned long flags;
637 unsigned short ident = P_IDENT(per);
638
639 /*
640 * Don't cares are pins with only one dedicated function
641 */
642
643 if (per & P_DONTCARE)
644 return 0;
645
646 if (!(per & P_DEFINED))
647 return -ENODEV;
648
Michael Hennerichc58c2142007-10-04 00:35:05 +0800649 local_irq_save(flags);
650
Michael Hennerichcda6a202007-10-04 00:36:18 +0800651 if (!check_gpio(ident)) {
652
Michael Hennerichc58c2142007-10-04 00:35:05 +0800653 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
654 printk(KERN_ERR
655 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
656 __FUNCTION__, ident, get_label(ident));
657 dump_stack();
658 local_irq_restore(flags);
659 return -EBUSY;
660 }
661
Michael Hennerichcda6a202007-10-04 00:36:18 +0800662 }
663
Michael Hennerichc58c2142007-10-04 00:35:05 +0800664 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
665
666 /*
667 * Pin functions like AMC address strobes my
668 * be requested and used by several drivers
669 */
670
671 if (!(per & P_MAYSHARE)) {
672
673 /*
674 * Allow that the identical pin function can
675 * be requested from the same driver twice
676 */
677
678 if (cmp_label(ident, label) == 0)
679 goto anyway;
680
681 printk(KERN_ERR
682 "%s: Peripheral %d function %d is already"
683 "reserved by %s !\n",
684 __FUNCTION__, ident, P_FUNCT2MUX(per),
685 get_label(ident));
686 dump_stack();
687 local_irq_restore(flags);
688 return -EBUSY;
689 }
690
691 }
692
693anyway:
694
695
696 portmux_setup(per, P_FUNCT2MUX(per));
697
698 port_setup(ident, PERIPHERAL_USAGE);
699
700 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
701 local_irq_restore(flags);
702 set_label(ident, label);
703
704 return 0;
705}
706EXPORT_SYMBOL(peripheral_request);
707
708int peripheral_request_list(unsigned short per[], const char *label)
709{
710 u16 cnt;
711 int ret;
712
713 for (cnt = 0; per[cnt] != 0; cnt++) {
Michael Hennerich314c98d2007-07-24 18:03:45 +0800714
Michael Hennerichc58c2142007-10-04 00:35:05 +0800715 ret = peripheral_request(per[cnt], label);
Michael Hennerich314c98d2007-07-24 18:03:45 +0800716
717 if (ret < 0) {
718 for ( ; cnt > 0; cnt--) {
719 peripheral_free(per[cnt - 1]);
720 }
721 return ret;
722 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800723 }
724
725 return 0;
726}
727EXPORT_SYMBOL(peripheral_request_list);
728
729void peripheral_free(unsigned short per)
730{
731 unsigned long flags;
732 unsigned short ident = P_IDENT(per);
733
734 if (per & P_DONTCARE)
735 return;
736
737 if (!(per & P_DEFINED))
738 return;
739
740 if (check_gpio(ident) < 0)
741 return;
742
743 local_irq_save(flags);
744
745 if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
746 & gpio_bit(ident)))) {
747 local_irq_restore(flags);
748 return;
749 }
750
751 if (!(per & P_MAYSHARE)) {
752 port_setup(ident, GPIO_USAGE);
753 }
754
755 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
756
757 local_irq_restore(flags);
758}
759EXPORT_SYMBOL(peripheral_free);
760
761void peripheral_free_list(unsigned short per[])
762{
763 u16 cnt;
764
765 for (cnt = 0; per[cnt] != 0; cnt++) {
766 peripheral_free(per[cnt]);
767 }
768
769}
770EXPORT_SYMBOL(peripheral_free_list);
771
Bryan Wu1394f032007-05-06 14:50:22 -0700772/***********************************************************
773*
774* FUNCTIONS: Blackfin GPIO Driver
775*
776* INPUTS/OUTPUTS:
777* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
778*
779*
780* DESCRIPTION: Blackfin GPIO Driver API
781*
782* CAUTION:
783*************************************************************
784* MODIFICATION HISTORY :
785**************************************************************/
786
787int gpio_request(unsigned short gpio, const char *label)
788{
789 unsigned long flags;
790
791 if (check_gpio(gpio) < 0)
792 return -EINVAL;
793
794 local_irq_save(flags);
795
Michael Hennerichc58c2142007-10-04 00:35:05 +0800796 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700797 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved!\n", gpio);
798 dump_stack();
799 local_irq_restore(flags);
800 return -EBUSY;
801 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800802 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -0700803
804 local_irq_restore(flags);
805
806 port_setup(gpio, GPIO_USAGE);
807
808 return 0;
809}
810EXPORT_SYMBOL(gpio_request);
811
Bryan Wu1394f032007-05-06 14:50:22 -0700812void gpio_free(unsigned short gpio)
813{
814 unsigned long flags;
815
816 if (check_gpio(gpio) < 0)
817 return;
818
819 local_irq_save(flags);
820
Michael Hennerichc58c2142007-10-04 00:35:05 +0800821 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700822 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't reserved!\n", gpio);
823 dump_stack();
824 local_irq_restore(flags);
825 return;
826 }
827
828 default_gpio(gpio);
829
Michael Hennerichc58c2142007-10-04 00:35:05 +0800830 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -0700831
832 local_irq_restore(flags);
833}
834EXPORT_SYMBOL(gpio_free);
835
Bryan Wu1394f032007-05-06 14:50:22 -0700836void gpio_direction_input(unsigned short gpio)
837{
838 unsigned long flags;
839
Michael Hennerichc58c2142007-10-04 00:35:05 +0800840 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700841
842 local_irq_save(flags);
843 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
844 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
845 local_irq_restore(flags);
846}
847EXPORT_SYMBOL(gpio_direction_input);
848
849void gpio_direction_output(unsigned short gpio)
850{
851 unsigned long flags;
852
Michael Hennerichc58c2142007-10-04 00:35:05 +0800853 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700854
855 local_irq_save(flags);
856 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
857 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
858 local_irq_restore(flags);
859}
860EXPORT_SYMBOL(gpio_direction_output);