blob: 8d12853a64fdfb6f3454bbbdf5cd0442d2ab4aa9 [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:
Michael Hennericha2c8cfe2008-01-22 17:20:10 +080010 * Copyright 2008 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -070011 *
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/*
Michael Hennerichd2b11a42007-08-28 16:47:46 +080031* Number BF537/6/4 BF561 BF533/2/1 BF549/8/4/2
Bryan Wu1394f032007-05-06 14:50:22 -070032*
Michael Hennerichd2b11a42007-08-28 16:47:46 +080033* GPIO_0 PF0 PF0 PF0 PA0...PJ13
Bryan Wu1394f032007-05-06 14:50:22 -070034* 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
Mike Frysinger168f1212007-10-11 00:22:35 +080083#include <linux/delay.h>
Bryan Wu1394f032007-05-06 14:50:22 -070084#include <linux/module.h>
85#include <linux/err.h>
Mike Frysinger1545a112007-12-24 16:54:48 +080086#include <linux/proc_fs.h>
Bryan Wu1394f032007-05-06 14:50:22 -070087#include <asm/blackfin.h>
88#include <asm/gpio.h>
Michael Hennerichc58c2142007-10-04 00:35:05 +080089#include <asm/portmux.h>
Bryan Wu1394f032007-05-06 14:50:22 -070090#include <linux/irq.h>
91
Michael Hennerich2b393312007-10-10 16:58:49 +080092#if ANOMALY_05000311 || ANOMALY_05000323
93enum {
94 AWA_data = SYSCR,
95 AWA_data_clear = SYSCR,
96 AWA_data_set = SYSCR,
97 AWA_toggle = SYSCR,
Graf Yang6ed83942008-04-24 04:43:14 +080098 AWA_maska = BFIN_UART_SCR,
99 AWA_maska_clear = BFIN_UART_SCR,
100 AWA_maska_set = BFIN_UART_SCR,
101 AWA_maska_toggle = BFIN_UART_SCR,
102 AWA_maskb = BFIN_UART_GCTL,
103 AWA_maskb_clear = BFIN_UART_GCTL,
104 AWA_maskb_set = BFIN_UART_GCTL,
105 AWA_maskb_toggle = BFIN_UART_GCTL,
Michael Hennerich2b393312007-10-10 16:58:49 +0800106 AWA_dir = SPORT1_STAT,
107 AWA_polar = SPORT1_STAT,
108 AWA_edge = SPORT1_STAT,
109 AWA_both = SPORT1_STAT,
110#if ANOMALY_05000311
111 AWA_inen = TIMER_ENABLE,
112#elif ANOMALY_05000323
113 AWA_inen = DMA1_1_CONFIG,
114#endif
115};
116 /* Anomaly Workaround */
117#define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
118#else
119#define AWA_DUMMY_READ(...) do { } while (0)
120#endif
121
Michael Hennerichdc26aec2008-11-18 17:48:22 +0800122#if defined(BF533_FAMILY) || defined(BF538_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700123static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
124 (struct gpio_port_t *) FIO_FLAG_D,
125};
126#endif
127
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800128#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700129static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
130 (struct gpio_port_t *) PORTFIO,
131 (struct gpio_port_t *) PORTGIO,
132 (struct gpio_port_t *) PORTHIO,
133};
134
135static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
136 (unsigned short *) PORTF_FER,
137 (unsigned short *) PORTG_FER,
138 (unsigned short *) PORTH_FER,
139};
Bryan Wu1394f032007-05-06 14:50:22 -0700140#endif
141
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800142#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich59003142007-10-21 16:54:27 +0800143static unsigned short *port_mux[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
144 (unsigned short *) PORTF_MUX,
145 (unsigned short *) PORTG_MUX,
146 (unsigned short *) PORTH_MUX,
147};
148
149static const
150u8 pmux_offset[][16] =
151 {{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
152 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
153 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
154 };
155#endif
156
Bryan Wu1394f032007-05-06 14:50:22 -0700157#ifdef BF561_FAMILY
158static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
159 (struct gpio_port_t *) FIO0_FLAG_D,
160 (struct gpio_port_t *) FIO1_FLAG_D,
161 (struct gpio_port_t *) FIO2_FLAG_D,
162};
163#endif
164
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800165#ifdef BF548_FAMILY
166static struct gpio_port_t *gpio_array[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
167 (struct gpio_port_t *)PORTA_FER,
168 (struct gpio_port_t *)PORTB_FER,
169 (struct gpio_port_t *)PORTC_FER,
170 (struct gpio_port_t *)PORTD_FER,
171 (struct gpio_port_t *)PORTE_FER,
172 (struct gpio_port_t *)PORTF_FER,
173 (struct gpio_port_t *)PORTG_FER,
174 (struct gpio_port_t *)PORTH_FER,
175 (struct gpio_port_t *)PORTI_FER,
176 (struct gpio_port_t *)PORTJ_FER,
177};
178#endif
179
Michael Hennerichc58c2142007-10-04 00:35:05 +0800180static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800181static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
Michael Hennerichc58c2142007-10-04 00:35:05 +0800182
Michael Hennerich8c613622007-08-03 17:48:09 +0800183#define RESOURCE_LABEL_SIZE 16
184
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800185static struct str_ident {
Michael Hennerich8c613622007-08-03 17:48:09 +0800186 char name[RESOURCE_LABEL_SIZE];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800187} str_ident[MAX_RESOURCES];
Bryan Wu1394f032007-05-06 14:50:22 -0700188
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800189#if defined(CONFIG_PM)
190#if defined(CONFIG_BF54x)
191static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
192#else
Bryan Wu1394f032007-05-06 14:50:22 -0700193static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
194static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
195static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
196
197#ifdef BF533_FAMILY
198static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
199#endif
200
201#ifdef BF537_FAMILY
202static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
203#endif
204
Michael Hennerichdc26aec2008-11-18 17:48:22 +0800205#ifdef BF538_FAMILY
206static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PORTF_INTB};
207#endif
208
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800209#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich59003142007-10-21 16:54:27 +0800210static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
211#endif
212
Bryan Wu1394f032007-05-06 14:50:22 -0700213#ifdef BF561_FAMILY
214static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
215#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800216#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700217#endif /* CONFIG_PM */
218
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800219inline int check_gpio(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800220{
Mike Frysinger27228b22008-10-28 15:45:42 +0800221#if defined(BF548_FAMILY)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800222 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
223 || gpio == GPIO_PH14 || gpio == GPIO_PH15
Mike Frysinger27228b22008-10-28 15:45:42 +0800224 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800225 return -EINVAL;
Mike Frysinger27228b22008-10-28 15:45:42 +0800226#endif
Michael Henneriche7613aa2007-06-11 16:37:57 +0800227 if (gpio >= MAX_BLACKFIN_GPIOS)
Bryan Wu1394f032007-05-06 14:50:22 -0700228 return -EINVAL;
229 return 0;
230}
231
Mike Frysinger74c04502008-10-08 16:13:17 +0800232static void gpio_error(unsigned gpio)
Michael Hennerichacbcd262008-01-22 18:36:20 +0800233{
234 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
235}
236
Michael Hennerichc58c2142007-10-04 00:35:05 +0800237static void set_label(unsigned short ident, const char *label)
238{
Michael Henneriche9fae182008-10-13 11:35:22 +0800239 if (label) {
Michael Hennerich8c613622007-08-03 17:48:09 +0800240 strncpy(str_ident[ident].name, label,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800241 RESOURCE_LABEL_SIZE);
Michael Hennerich8c613622007-08-03 17:48:09 +0800242 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800243 }
244}
245
246static char *get_label(unsigned short ident)
247{
Michael Hennerich8c613622007-08-03 17:48:09 +0800248 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
Michael Hennerichc58c2142007-10-04 00:35:05 +0800249}
250
251static int cmp_label(unsigned short ident, const char *label)
252{
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800253 if (label == NULL) {
254 dump_stack();
255 printk(KERN_ERR "Please provide none-null label\n");
256 }
257
Michael Henneriche9fae182008-10-13 11:35:22 +0800258 if (label)
Mike Frysinger1f7d3732008-10-28 15:47:11 +0800259 return strcmp(str_ident[ident].name, label);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800260 else
261 return -EINVAL;
262}
263
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800264#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800265static void port_setup(unsigned gpio, unsigned short usage)
Bryan Wu1394f032007-05-06 14:50:22 -0700266{
Michael Hennerichcda6a202007-10-04 00:36:18 +0800267 if (!check_gpio(gpio)) {
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800268 if (usage == GPIO_USAGE)
Michael Hennerichcda6a202007-10-04 00:36:18 +0800269 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800270 else
Michael Hennerichcda6a202007-10-04 00:36:18 +0800271 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
272 SSYNC();
273 }
Bryan Wu1394f032007-05-06 14:50:22 -0700274}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800275#elif defined(BF548_FAMILY)
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800276static void port_setup(unsigned gpio, unsigned short usage)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800277{
278 if (usage == GPIO_USAGE)
279 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
280 else
281 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
282 SSYNC();
283}
Bryan Wu1394f032007-05-06 14:50:22 -0700284#else
285# define port_setup(...) do { } while (0)
286#endif
287
Michael Hennerichc58c2142007-10-04 00:35:05 +0800288#ifdef BF537_FAMILY
Michael Hennerich8c613622007-08-03 17:48:09 +0800289static struct {
290 unsigned short res;
291 unsigned short offset;
292} port_mux_lut[] = {
293 {.res = P_PPI0_D13, .offset = 11},
294 {.res = P_PPI0_D14, .offset = 11},
295 {.res = P_PPI0_D15, .offset = 11},
296 {.res = P_SPORT1_TFS, .offset = 11},
297 {.res = P_SPORT1_TSCLK, .offset = 11},
298 {.res = P_SPORT1_DTPRI, .offset = 11},
299 {.res = P_PPI0_D10, .offset = 10},
300 {.res = P_PPI0_D11, .offset = 10},
301 {.res = P_PPI0_D12, .offset = 10},
302 {.res = P_SPORT1_RSCLK, .offset = 10},
303 {.res = P_SPORT1_RFS, .offset = 10},
304 {.res = P_SPORT1_DRPRI, .offset = 10},
305 {.res = P_PPI0_D8, .offset = 9},
306 {.res = P_PPI0_D9, .offset = 9},
307 {.res = P_SPORT1_DRSEC, .offset = 9},
308 {.res = P_SPORT1_DTSEC, .offset = 9},
309 {.res = P_TMR2, .offset = 8},
310 {.res = P_PPI0_FS3, .offset = 8},
311 {.res = P_TMR3, .offset = 7},
312 {.res = P_SPI0_SSEL4, .offset = 7},
313 {.res = P_TMR4, .offset = 6},
314 {.res = P_SPI0_SSEL5, .offset = 6},
315 {.res = P_TMR5, .offset = 5},
316 {.res = P_SPI0_SSEL6, .offset = 5},
317 {.res = P_UART1_RX, .offset = 4},
318 {.res = P_UART1_TX, .offset = 4},
319 {.res = P_TMR6, .offset = 4},
320 {.res = P_TMR7, .offset = 4},
321 {.res = P_UART0_RX, .offset = 3},
322 {.res = P_UART0_TX, .offset = 3},
323 {.res = P_DMAR0, .offset = 3},
324 {.res = P_DMAR1, .offset = 3},
325 {.res = P_SPORT0_DTSEC, .offset = 1},
326 {.res = P_SPORT0_DRSEC, .offset = 1},
327 {.res = P_CAN0_RX, .offset = 1},
328 {.res = P_CAN0_TX, .offset = 1},
329 {.res = P_SPI0_SSEL7, .offset = 1},
330 {.res = P_SPORT0_TFS, .offset = 0},
331 {.res = P_SPORT0_DTPRI, .offset = 0},
332 {.res = P_SPI0_SSEL2, .offset = 0},
333 {.res = P_SPI0_SSEL3, .offset = 0},
Michael Hennerichc58c2142007-10-04 00:35:05 +0800334};
335
336static void portmux_setup(unsigned short per, unsigned short function)
337{
Michael Hennerich8c613622007-08-03 17:48:09 +0800338 u16 y, offset, muxreg;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800339
Michael Hennerich8c613622007-08-03 17:48:09 +0800340 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
341 if (port_mux_lut[y].res == per) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800342
343 /* SET PORTMUX REG */
344
Michael Hennerich8c613622007-08-03 17:48:09 +0800345 offset = port_mux_lut[y].offset;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800346 muxreg = bfin_read_PORT_MUX();
347
Mike Frysingerd171c232008-03-26 08:35:46 +0800348 if (offset != 1)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800349 muxreg &= ~(1 << offset);
Mike Frysingerd171c232008-03-26 08:35:46 +0800350 else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800351 muxreg &= ~(3 << 1);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800352
353 muxreg |= (function << offset);
354 bfin_write_PORT_MUX(muxreg);
355 }
356 }
357}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800358#elif defined(BF548_FAMILY)
359inline void portmux_setup(unsigned short portno, unsigned short function)
360{
361 u32 pmux;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800362
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800363 pmux = gpio_array[gpio_bank(portno)]->port_mux;
364
365 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
366 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
367
368 gpio_array[gpio_bank(portno)]->port_mux = pmux;
369}
370
371inline u16 get_portmux(unsigned short portno)
372{
373 u32 pmux;
374
375 pmux = gpio_array[gpio_bank(portno)]->port_mux;
376
377 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
378}
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800379#elif defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich59003142007-10-21 16:54:27 +0800380inline void portmux_setup(unsigned short portno, unsigned short function)
381{
382 u16 pmux, ident = P_IDENT(portno);
383 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
384
385 pmux = *port_mux[gpio_bank(ident)];
386 pmux &= ~(3 << offset);
387 pmux |= (function & 3) << offset;
388 *port_mux[gpio_bank(ident)] = pmux;
389 SSYNC();
390}
Michael Hennerichc58c2142007-10-04 00:35:05 +0800391#else
392# define portmux_setup(...) do { } while (0)
393#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700394
Mike Frysingera161bb02007-05-21 18:09:14 +0800395static int __init bfin_gpio_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700396{
Bryan Wu1394f032007-05-06 14:50:22 -0700397 printk(KERN_INFO "Blackfin GPIO Controller\n");
398
Bryan Wu1394f032007-05-06 14:50:22 -0700399 return 0;
400}
Bryan Wu1394f032007-05-06 14:50:22 -0700401arch_initcall(bfin_gpio_init);
402
403
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800404#ifndef BF548_FAMILY
Bryan Wu1394f032007-05-06 14:50:22 -0700405/***********************************************************
406*
407* FUNCTIONS: Blackfin General Purpose Ports Access Functions
408*
409* INPUTS/OUTPUTS:
410* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
411*
412*
413* DESCRIPTION: These functions abstract direct register access
414* to Blackfin processor General Purpose
415* Ports Regsiters
416*
417* CAUTION: These functions do not belong to the GPIO Driver API
418*************************************************************
419* MODIFICATION HISTORY :
420**************************************************************/
421
422/* Set a specific bit */
423
424#define SET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800425void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700426{ \
427 unsigned long flags; \
Bryan Wu1394f032007-05-06 14:50:22 -0700428 local_irq_save(flags); \
429 if (arg) \
430 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
431 else \
432 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800433 AWA_DUMMY_READ(name); \
Bryan Wu1394f032007-05-06 14:50:22 -0700434 local_irq_restore(flags); \
435} \
436EXPORT_SYMBOL(set_gpio_ ## name);
437
438SET_GPIO(dir)
439SET_GPIO(inen)
440SET_GPIO(polar)
441SET_GPIO(edge)
442SET_GPIO(both)
443
444
Michael Hennerich2b393312007-10-10 16:58:49 +0800445#if ANOMALY_05000311 || ANOMALY_05000323
446#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800447void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800448{ \
449 unsigned long flags; \
Michael Hennerich2b393312007-10-10 16:58:49 +0800450 local_irq_save(flags); \
451 if (arg) \
452 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
453 else \
454 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
455 AWA_DUMMY_READ(name); \
456 local_irq_restore(flags); \
457} \
458EXPORT_SYMBOL(set_gpio_ ## name);
459#else
Bryan Wu1394f032007-05-06 14:50:22 -0700460#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800461void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700462{ \
Bryan Wu1394f032007-05-06 14:50:22 -0700463 if (arg) \
464 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
465 else \
466 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
467} \
468EXPORT_SYMBOL(set_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800469#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700470
471SET_GPIO_SC(maska)
472SET_GPIO_SC(maskb)
Bryan Wu1394f032007-05-06 14:50:22 -0700473SET_GPIO_SC(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700474
Michael Hennerich2b393312007-10-10 16:58:49 +0800475#if ANOMALY_05000311 || ANOMALY_05000323
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800476void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700477{
478 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700479 local_irq_save(flags);
480 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +0800481 AWA_DUMMY_READ(toggle);
Bryan Wu1394f032007-05-06 14:50:22 -0700482 local_irq_restore(flags);
483}
484#else
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800485void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700486{
Bryan Wu1394f032007-05-06 14:50:22 -0700487 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
488}
489#endif
490EXPORT_SYMBOL(set_gpio_toggle);
491
492
493/*Set current PORT date (16-bit word)*/
494
Michael Hennerich2b393312007-10-10 16:58:49 +0800495#if ANOMALY_05000311 || ANOMALY_05000323
496#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800497void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800498{ \
499 unsigned long flags; \
500 local_irq_save(flags); \
501 gpio_bankb[gpio_bank(gpio)]->name = arg; \
502 AWA_DUMMY_READ(name); \
503 local_irq_restore(flags); \
504} \
505EXPORT_SYMBOL(set_gpiop_ ## name);
506#else
Bryan Wu1394f032007-05-06 14:50:22 -0700507#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800508void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700509{ \
510 gpio_bankb[gpio_bank(gpio)]->name = arg; \
511} \
512EXPORT_SYMBOL(set_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800513#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700514
Michael Hennerich2b393312007-10-10 16:58:49 +0800515SET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700516SET_GPIO_P(dir)
517SET_GPIO_P(inen)
518SET_GPIO_P(polar)
519SET_GPIO_P(edge)
520SET_GPIO_P(both)
521SET_GPIO_P(maska)
522SET_GPIO_P(maskb)
523
Bryan Wu1394f032007-05-06 14:50:22 -0700524/* Get a specific bit */
Michael Hennerich2b393312007-10-10 16:58:49 +0800525#if ANOMALY_05000311 || ANOMALY_05000323
526#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800527unsigned short get_gpio_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800528{ \
529 unsigned long flags; \
530 unsigned short ret; \
531 local_irq_save(flags); \
532 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
533 AWA_DUMMY_READ(name); \
534 local_irq_restore(flags); \
535 return ret; \
536} \
537EXPORT_SYMBOL(get_gpio_ ## name);
538#else
Bryan Wu1394f032007-05-06 14:50:22 -0700539#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800540unsigned short get_gpio_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700541{ \
542 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
543} \
544EXPORT_SYMBOL(get_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800545#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700546
Michael Hennerich2b393312007-10-10 16:58:49 +0800547GET_GPIO(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700548GET_GPIO(dir)
549GET_GPIO(inen)
550GET_GPIO(polar)
551GET_GPIO(edge)
552GET_GPIO(both)
553GET_GPIO(maska)
554GET_GPIO(maskb)
555
Bryan Wu1394f032007-05-06 14:50:22 -0700556/*Get current PORT date (16-bit word)*/
557
Michael Hennerich2b393312007-10-10 16:58:49 +0800558#if ANOMALY_05000311 || ANOMALY_05000323
559#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800560unsigned short get_gpiop_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800561{ \
562 unsigned long flags; \
563 unsigned short ret; \
564 local_irq_save(flags); \
565 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
566 AWA_DUMMY_READ(name); \
567 local_irq_restore(flags); \
568 return ret; \
569} \
570EXPORT_SYMBOL(get_gpiop_ ## name);
571#else
Bryan Wu1394f032007-05-06 14:50:22 -0700572#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800573unsigned short get_gpiop_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700574{ \
575 return (gpio_bankb[gpio_bank(gpio)]->name);\
576} \
577EXPORT_SYMBOL(get_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800578#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700579
Michael Hennerich2b393312007-10-10 16:58:49 +0800580GET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700581GET_GPIO_P(dir)
582GET_GPIO_P(inen)
583GET_GPIO_P(polar)
584GET_GPIO_P(edge)
585GET_GPIO_P(both)
586GET_GPIO_P(maska)
587GET_GPIO_P(maskb)
588
Bryan Wu1394f032007-05-06 14:50:22 -0700589
590#ifdef CONFIG_PM
591/***********************************************************
592*
593* FUNCTIONS: Blackfin PM Setup API
594*
595* INPUTS/OUTPUTS:
596* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
597* type -
598* PM_WAKE_RISING
599* PM_WAKE_FALLING
600* PM_WAKE_HIGH
601* PM_WAKE_LOW
602* PM_WAKE_BOTH_EDGES
603*
604* DESCRIPTION: Blackfin PM Driver API
605*
606* CAUTION:
607*************************************************************
608* MODIFICATION HISTORY :
609**************************************************************/
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800610int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700611{
612 unsigned long flags;
613
614 if ((check_gpio(gpio) < 0) || !type)
615 return -EINVAL;
616
617 local_irq_save(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700618 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
619 wakeup_flags_map[gpio] = type;
620 local_irq_restore(flags);
621
622 return 0;
623}
624EXPORT_SYMBOL(gpio_pm_wakeup_request);
625
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800626void gpio_pm_wakeup_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700627{
628 unsigned long flags;
629
630 if (check_gpio(gpio) < 0)
631 return;
632
633 local_irq_save(flags);
634
635 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
636
637 local_irq_restore(flags);
638}
639EXPORT_SYMBOL(gpio_pm_wakeup_free);
640
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800641static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700642{
643 port_setup(gpio, GPIO_USAGE);
644 set_gpio_dir(gpio, 0);
645 set_gpio_inen(gpio, 1);
646
647 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
648 set_gpio_edge(gpio, 1);
649 else
650 set_gpio_edge(gpio, 0);
651
652 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
653 set_gpio_both(gpio, 1);
654 else
655 set_gpio_both(gpio, 0);
656
657 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
658 set_gpio_polar(gpio, 1);
659 else
660 set_gpio_polar(gpio, 0);
661
662 SSYNC();
663
664 return 0;
665}
666
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800667u32 bfin_pm_standby_setup(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700668{
Bryan Wu1394f032007-05-06 14:50:22 -0700669 u16 bank, mask, i, gpio;
670
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800671 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700672 mask = wakeup_map[gpio_bank(i)];
673 bank = gpio_bank(i);
674
675 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
676 gpio_bankb[bank]->maskb = 0;
677
678 if (mask) {
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800679#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700680 gpio_bank_saved[bank].fer = *port_fer[bank];
681#endif
682 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
683 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
684 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
685 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
686 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800687 gpio_bank_saved[bank].reserved =
688 reserved_gpio_map[bank];
Bryan Wu1394f032007-05-06 14:50:22 -0700689
690 gpio = i;
691
692 while (mask) {
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800693 if ((mask & 1) && (wakeup_flags_map[gpio] !=
694 PM_WAKE_IGNORE)) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800695 reserved_gpio_map[gpio_bank(gpio)] |=
Michael Hennerich581d62a2007-06-14 13:30:23 +0800696 gpio_bit(gpio);
697 bfin_gpio_wakeup_type(gpio,
698 wakeup_flags_map[gpio]);
Bryan Wu1394f032007-05-06 14:50:22 -0700699 set_gpio_data(gpio, 0); /*Clear*/
700 }
701 gpio++;
702 mask >>= 1;
703 }
704
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800705 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
Bryan Wu1394f032007-05-06 14:50:22 -0700706 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
707 }
708 }
709
Michael Hennerich2b393312007-10-10 16:58:49 +0800710 AWA_DUMMY_READ(maskb_set);
711
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800712 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700713}
714
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800715void bfin_pm_standby_restore(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700716{
717 u16 bank, mask, i;
718
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800719 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700720 mask = wakeup_map[gpio_bank(i)];
721 bank = gpio_bank(i);
722
723 if (mask) {
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800724#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700725 *port_fer[bank] = gpio_bank_saved[bank].fer;
726#endif
727 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
728 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
729 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
730 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
731 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800732
Michael Hennerichc58c2142007-10-04 00:35:05 +0800733 reserved_gpio_map[bank] =
734 gpio_bank_saved[bank].reserved;
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800735 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
Bryan Wu1394f032007-05-06 14:50:22 -0700736 }
737
738 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
739 }
Michael Hennerich2b393312007-10-10 16:58:49 +0800740 AWA_DUMMY_READ(maskb);
Bryan Wu1394f032007-05-06 14:50:22 -0700741}
742
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800743void bfin_gpio_pm_hibernate_suspend(void)
744{
745 int i, bank;
746
747 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
748 bank = gpio_bank(i);
749
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800750#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800751 gpio_bank_saved[bank].fer = *port_fer[bank];
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800752#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800753 gpio_bank_saved[bank].mux = *port_mux[bank];
754#else
755 if (bank == 0)
756 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
757#endif
758#endif
759 gpio_bank_saved[bank].data = gpio_bankb[bank]->data;
760 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
761 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
762 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
763 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
764 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
765 gpio_bank_saved[bank].maska = gpio_bankb[bank]->maska;
766 }
767
768 AWA_DUMMY_READ(maska);
769}
770
771void bfin_gpio_pm_hibernate_restore(void)
772{
773 int i, bank;
774
775 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
776 bank = gpio_bank(i);
777
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800778#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
779#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800780 *port_mux[bank] = gpio_bank_saved[bank].mux;
781#else
782 if (bank == 0)
783 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
784#endif
785 *port_fer[bank] = gpio_bank_saved[bank].fer;
786#endif
787 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
788 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
789 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
790 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
791 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
792
793 gpio_bankb[bank]->data_set = gpio_bank_saved[bank].data
794 | gpio_bank_saved[bank].dir;
795
796 gpio_bankb[bank]->maska = gpio_bank_saved[bank].maska;
797 }
798 AWA_DUMMY_READ(maska);
799}
800
801
Bryan Wu1394f032007-05-06 14:50:22 -0700802#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800803#else /* BF548_FAMILY */
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800804#ifdef CONFIG_PM
805
806u32 bfin_pm_standby_setup(void)
807{
808 return 0;
809}
810
811void bfin_pm_standby_restore(void)
812{
813
814}
815
816void bfin_gpio_pm_hibernate_suspend(void)
817{
818 int i, bank;
819
820 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
821 bank = gpio_bank(i);
822
823 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
824 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
825 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
826 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
827 gpio_bank_saved[bank].inen = gpio_array[bank]->port_inen;
828 gpio_bank_saved[bank].dir = gpio_array[bank]->port_dir_set;
829 }
830}
831
832void bfin_gpio_pm_hibernate_restore(void)
833{
834 int i, bank;
835
836 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
837 bank = gpio_bank(i);
838
839 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
840 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
841 gpio_array[bank]->port_inen = gpio_bank_saved[bank].inen;
842 gpio_array[bank]->port_dir_set = gpio_bank_saved[bank].dir;
843 gpio_array[bank]->port_set = gpio_bank_saved[bank].data
844 | gpio_bank_saved[bank].dir;
845 }
846}
847#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800848
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800849unsigned short get_gpio_dir(unsigned gpio)
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800850{
851 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
852}
853EXPORT_SYMBOL(get_gpio_dir);
854
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800855#endif /* BF548_FAMILY */
Bryan Wu1394f032007-05-06 14:50:22 -0700856
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800857/***********************************************************
858*
859* FUNCTIONS: Blackfin Peripheral Resource Allocation
860* and PortMux Setup
861*
862* INPUTS/OUTPUTS:
863* per Peripheral Identifier
864* label String
865*
866* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
867*
868* CAUTION:
869*************************************************************
870* MODIFICATION HISTORY :
871**************************************************************/
Michael Hennerichc58c2142007-10-04 00:35:05 +0800872
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800873int peripheral_request(unsigned short per, const char *label)
874{
875 unsigned long flags;
876 unsigned short ident = P_IDENT(per);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800877
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800878 /*
879 * Don't cares are pins with only one dedicated function
880 */
881
882 if (per & P_DONTCARE)
883 return 0;
884
885 if (!(per & P_DEFINED))
886 return -ENODEV;
887
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800888 if (check_gpio(ident))
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800889 return -EINVAL;
890
891 local_irq_save(flags);
892
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800893 /* Can't do GPIO and peripheral at the same time */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800894 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800895 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800896 printk(KERN_ERR
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800897 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800898 __func__, ident, get_label(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800899 local_irq_restore(flags);
900 return -EBUSY;
901 }
902
903 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
904
Mike Frysingerd171c232008-03-26 08:35:46 +0800905 /*
906 * Pin functions like AMC address strobes my
907 * be requested and used by several drivers
908 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800909
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800910#ifdef BF548_FAMILY
911 u16 funct = get_portmux(ident);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800912
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800913 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
914#else
915 if (!(per & P_MAYSHARE)) {
916#endif
Mike Frysingerd171c232008-03-26 08:35:46 +0800917 /*
918 * Allow that the identical pin function can
919 * be requested from the same driver twice
920 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800921
Mike Frysingerd171c232008-03-26 08:35:46 +0800922 if (cmp_label(ident, label) == 0)
923 goto anyway;
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800924
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800925 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800926 printk(KERN_ERR
927 "%s: Peripheral %d function %d is already reserved by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800928 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800929 local_irq_restore(flags);
930 return -EBUSY;
931 }
932 }
933
Mike Frysingerd171c232008-03-26 08:35:46 +0800934 anyway:
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800935 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
936
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800937#ifdef BF548_FAMILY
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800938 portmux_setup(ident, P_FUNCT2MUX(per));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800939#else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800940 portmux_setup(per, P_FUNCT2MUX(per));
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800941#endif
Michael Hennerichc58c2142007-10-04 00:35:05 +0800942 port_setup(ident, PERIPHERAL_USAGE);
943
Michael Hennerichc58c2142007-10-04 00:35:05 +0800944 local_irq_restore(flags);
945 set_label(ident, label);
946
947 return 0;
948}
949EXPORT_SYMBOL(peripheral_request);
950
Mike Frysinger68179372008-04-24 05:04:24 +0800951int peripheral_request_list(const unsigned short per[], const char *label)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800952{
953 u16 cnt;
954 int ret;
955
956 for (cnt = 0; per[cnt] != 0; cnt++) {
Michael Hennerich314c98d2007-07-24 18:03:45 +0800957
Michael Hennerichc58c2142007-10-04 00:35:05 +0800958 ret = peripheral_request(per[cnt], label);
Michael Hennerich314c98d2007-07-24 18:03:45 +0800959
960 if (ret < 0) {
Mike Frysingerd171c232008-03-26 08:35:46 +0800961 for ( ; cnt > 0; cnt--)
Michael Hennerich314c98d2007-07-24 18:03:45 +0800962 peripheral_free(per[cnt - 1]);
Mike Frysingerd171c232008-03-26 08:35:46 +0800963
964 return ret;
Michael Hennerich314c98d2007-07-24 18:03:45 +0800965 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800966 }
967
968 return 0;
969}
970EXPORT_SYMBOL(peripheral_request_list);
971
972void peripheral_free(unsigned short per)
973{
974 unsigned long flags;
975 unsigned short ident = P_IDENT(per);
976
977 if (per & P_DONTCARE)
978 return;
979
980 if (!(per & P_DEFINED))
981 return;
982
983 if (check_gpio(ident) < 0)
984 return;
985
986 local_irq_save(flags);
987
Mike Frysingerd171c232008-03-26 08:35:46 +0800988 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800989 local_irq_restore(flags);
990 return;
991 }
992
Mike Frysingerd171c232008-03-26 08:35:46 +0800993 if (!(per & P_MAYSHARE))
Michael Hennerichc58c2142007-10-04 00:35:05 +0800994 port_setup(ident, GPIO_USAGE);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800995
996 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
997
Michael Hennerich2acde902007-10-11 00:24:40 +0800998 set_label(ident, "free");
999
Michael Hennerichc58c2142007-10-04 00:35:05 +08001000 local_irq_restore(flags);
1001}
1002EXPORT_SYMBOL(peripheral_free);
1003
Mike Frysinger68179372008-04-24 05:04:24 +08001004void peripheral_free_list(const unsigned short per[])
Michael Hennerichc58c2142007-10-04 00:35:05 +08001005{
1006 u16 cnt;
Mike Frysingerd171c232008-03-26 08:35:46 +08001007 for (cnt = 0; per[cnt] != 0; cnt++)
Michael Hennerichc58c2142007-10-04 00:35:05 +08001008 peripheral_free(per[cnt]);
Michael Hennerichc58c2142007-10-04 00:35:05 +08001009}
1010EXPORT_SYMBOL(peripheral_free_list);
1011
Bryan Wu1394f032007-05-06 14:50:22 -07001012/***********************************************************
1013*
1014* FUNCTIONS: Blackfin GPIO Driver
1015*
1016* INPUTS/OUTPUTS:
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001017* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1018* label String
Bryan Wu1394f032007-05-06 14:50:22 -07001019*
1020* DESCRIPTION: Blackfin GPIO Driver API
1021*
1022* CAUTION:
1023*************************************************************
1024* MODIFICATION HISTORY :
1025**************************************************************/
1026
Michael Hennerichacbcd262008-01-22 18:36:20 +08001027int gpio_request(unsigned gpio, const char *label)
Bryan Wu1394f032007-05-06 14:50:22 -07001028{
1029 unsigned long flags;
1030
1031 if (check_gpio(gpio) < 0)
1032 return -EINVAL;
1033
1034 local_irq_save(flags);
1035
Michael Hennerich2acde902007-10-11 00:24:40 +08001036 /*
1037 * Allow that the identical GPIO can
1038 * be requested from the same driver twice
1039 * Do nothing and return -
1040 */
1041
1042 if (cmp_label(gpio, label) == 0) {
1043 local_irq_restore(flags);
1044 return 0;
1045 }
1046
Michael Hennerichc58c2142007-10-04 00:35:05 +08001047 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001048 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001049 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1050 gpio, get_label(gpio));
Bryan Wu1394f032007-05-06 14:50:22 -07001051 local_irq_restore(flags);
1052 return -EBUSY;
1053 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001054 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001055 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001056 printk(KERN_ERR
1057 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1058 gpio, get_label(gpio));
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001059 local_irq_restore(flags);
1060 return -EBUSY;
1061 }
1062
Michael Hennerichc58c2142007-10-04 00:35:05 +08001063 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001064
1065 local_irq_restore(flags);
1066
1067 port_setup(gpio, GPIO_USAGE);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001068 set_label(gpio, label);
Bryan Wu1394f032007-05-06 14:50:22 -07001069
1070 return 0;
1071}
1072EXPORT_SYMBOL(gpio_request);
1073
Michael Hennerichacbcd262008-01-22 18:36:20 +08001074void gpio_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001075{
1076 unsigned long flags;
1077
1078 if (check_gpio(gpio) < 0)
1079 return;
1080
1081 local_irq_save(flags);
1082
Michael Hennerichc58c2142007-10-04 00:35:05 +08001083 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
Bryan Wu1394f032007-05-06 14:50:22 -07001084 dump_stack();
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001085 gpio_error(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001086 local_irq_restore(flags);
1087 return;
1088 }
1089
Michael Hennerichc58c2142007-10-04 00:35:05 +08001090 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001091
Michael Hennerich2acde902007-10-11 00:24:40 +08001092 set_label(gpio, "free");
1093
Bryan Wu1394f032007-05-06 14:50:22 -07001094 local_irq_restore(flags);
1095}
1096EXPORT_SYMBOL(gpio_free);
1097
Michael Hennerichacbcd262008-01-22 18:36:20 +08001098
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001099#ifdef BF548_FAMILY
Michael Hennerichacbcd262008-01-22 18:36:20 +08001100int gpio_direction_input(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001101{
1102 unsigned long flags;
1103
Michael Hennerichacbcd262008-01-22 18:36:20 +08001104 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1105 gpio_error(gpio);
1106 return -EINVAL;
1107 }
1108
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001109 local_irq_save(flags);
1110 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1111 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1112 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001113
1114 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001115}
1116EXPORT_SYMBOL(gpio_direction_input);
1117
Michael Hennerichacbcd262008-01-22 18:36:20 +08001118int gpio_direction_output(unsigned gpio, int value)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001119{
1120 unsigned long flags;
1121
Michael Hennerichacbcd262008-01-22 18:36:20 +08001122 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1123 gpio_error(gpio);
1124 return -EINVAL;
1125 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001126
1127 local_irq_save(flags);
1128 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001129 gpio_set_value(gpio, value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001130 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1131 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001132
1133 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001134}
1135EXPORT_SYMBOL(gpio_direction_output);
1136
Michael Hennerichacbcd262008-01-22 18:36:20 +08001137void gpio_set_value(unsigned gpio, int arg)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001138{
1139 if (arg)
1140 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1141 else
1142 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001143}
1144EXPORT_SYMBOL(gpio_set_value);
1145
Michael Hennerichacbcd262008-01-22 18:36:20 +08001146int gpio_get_value(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001147{
1148 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1149}
1150EXPORT_SYMBOL(gpio_get_value);
1151
Michael Hennerichaffee2b22008-04-24 08:10:10 +08001152void bfin_gpio_irq_prepare(unsigned gpio)
1153{
1154 unsigned long flags;
1155
1156 port_setup(gpio, GPIO_USAGE);
1157
1158 local_irq_save(flags);
1159 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1160 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1161 local_irq_restore(flags);
1162}
1163
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001164#else
1165
Michael Hennerich803a8d22008-05-17 16:01:51 +08001166int gpio_get_value(unsigned gpio)
1167{
1168 unsigned long flags;
1169 int ret;
1170
1171 if (unlikely(get_gpio_edge(gpio))) {
1172 local_irq_save(flags);
1173 set_gpio_edge(gpio, 0);
1174 ret = get_gpio_data(gpio);
1175 set_gpio_edge(gpio, 1);
1176 local_irq_restore(flags);
1177
1178 return ret;
1179 } else
1180 return get_gpio_data(gpio);
1181}
1182EXPORT_SYMBOL(gpio_get_value);
1183
1184
Michael Hennerichacbcd262008-01-22 18:36:20 +08001185int gpio_direction_input(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001186{
1187 unsigned long flags;
1188
Michael Hennerichacbcd262008-01-22 18:36:20 +08001189 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1190 gpio_error(gpio);
1191 return -EINVAL;
1192 }
Bryan Wu1394f032007-05-06 14:50:22 -07001193
1194 local_irq_save(flags);
1195 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1196 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001197 AWA_DUMMY_READ(inen);
Bryan Wu1394f032007-05-06 14:50:22 -07001198 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001199
1200 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001201}
1202EXPORT_SYMBOL(gpio_direction_input);
1203
Michael Hennerichacbcd262008-01-22 18:36:20 +08001204int gpio_direction_output(unsigned gpio, int value)
Bryan Wu1394f032007-05-06 14:50:22 -07001205{
1206 unsigned long flags;
1207
Michael Hennerichacbcd262008-01-22 18:36:20 +08001208 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1209 gpio_error(gpio);
1210 return -EINVAL;
1211 }
Bryan Wu1394f032007-05-06 14:50:22 -07001212
1213 local_irq_save(flags);
1214 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001215
1216 if (value)
1217 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1218 else
1219 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1220
Bryan Wu1394f032007-05-06 14:50:22 -07001221 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001222 AWA_DUMMY_READ(dir);
Bryan Wu1394f032007-05-06 14:50:22 -07001223 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001224
1225 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001226}
1227EXPORT_SYMBOL(gpio_direction_output);
Mike Frysinger168f1212007-10-11 00:22:35 +08001228
1229/* If we are booting from SPI and our board lacks a strong enough pull up,
1230 * the core can reset and execute the bootrom faster than the resistor can
1231 * pull the signal logically high. To work around this (common) error in
1232 * board design, we explicitly set the pin back to GPIO mode, force /CS
1233 * high, and wait for the electrons to do their thing.
1234 *
1235 * This function only makes sense to be called from reset code, but it
1236 * lives here as we need to force all the GPIO states w/out going through
1237 * BUG() checks and such.
1238 */
1239void bfin_gpio_reset_spi0_ssel1(void)
1240{
Michael Hennerich4d5f4ed2007-08-27 16:46:17 +08001241 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1242
1243 port_setup(gpio, GPIO_USAGE);
1244 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001245 AWA_DUMMY_READ(data_set);
Mike Frysinger168f1212007-10-11 00:22:35 +08001246 udelay(1);
1247}
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001248
Michael Hennerichaffee2b22008-04-24 08:10:10 +08001249void bfin_gpio_irq_prepare(unsigned gpio)
1250{
1251 port_setup(gpio, GPIO_USAGE);
1252}
1253
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001254#endif /*BF548_FAMILY */
Mike Frysinger1545a112007-12-24 16:54:48 +08001255
1256#if defined(CONFIG_PROC_FS)
1257static int gpio_proc_read(char *buf, char **start, off_t offset,
1258 int len, int *unused_i, void *unused_v)
1259{
1260 int c, outlen = 0;
1261
1262 for (c = 0; c < MAX_RESOURCES; c++) {
1263 if (!check_gpio(c) && (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c)))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001264 len = sprintf(buf, "GPIO_%d: %s \t\tGPIO %s\n", c,
Mike Frysinger1545a112007-12-24 16:54:48 +08001265 get_label(c), get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1266 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001267 len = sprintf(buf, "GPIO_%d: %s \t\tPeripheral\n", c, get_label(c));
Mike Frysinger1545a112007-12-24 16:54:48 +08001268 else
1269 continue;
1270 buf += len;
1271 outlen += len;
1272 }
1273 return outlen;
1274}
1275
1276static __init int gpio_register_proc(void)
1277{
1278 struct proc_dir_entry *proc_gpio;
1279
1280 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1281 if (proc_gpio)
1282 proc_gpio->read_proc = gpio_proc_read;
1283 return proc_gpio != NULL;
1284}
Mike Frysinger1545a112007-12-24 16:54:48 +08001285__initcall(gpio_register_proc);
1286#endif