blob: 4c14331978f6e369b70ea862632f4d913cc814f0 [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 Wu397861c2008-11-18 17:48:22 +0800123static struct gpio_port_t *gpio_bankb[] = {
Bryan Wu1394f032007-05-06 14:50:22 -0700124 (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 Wu397861c2008-11-18 17:48:22 +0800129static struct gpio_port_t *gpio_bankb[] = {
Bryan Wu1394f032007-05-06 14:50:22 -0700130 (struct gpio_port_t *) PORTFIO,
131 (struct gpio_port_t *) PORTGIO,
132 (struct gpio_port_t *) PORTHIO,
133};
134
Bryan Wu397861c2008-11-18 17:48:22 +0800135static unsigned short *port_fer[] = {
Bryan Wu1394f032007-05-06 14:50:22 -0700136 (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)
Bryan Wu397861c2008-11-18 17:48:22 +0800143static unsigned short *port_mux[] = {
Michael Hennerich59003142007-10-21 16:54:27 +0800144 (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
Bryan Wu397861c2008-11-18 17:48:22 +0800158static struct gpio_port_t *gpio_bankb[] = {
Bryan Wu1394f032007-05-06 14:50:22 -0700159 (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
Bryan Wu397861c2008-11-18 17:48:22 +0800166static struct gpio_port_t *gpio_array[] = {
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800167 (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
Bryan Wu397861c2008-11-18 17:48:22 +0800180static unsigned short reserved_gpio_map[GPIO_BANK_NUM];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800181static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
Graf Yang9570ff42009-01-07 23:14:38 +0800182static unsigned short reserved_gpio_irq_map[GPIO_BANK_NUM];
Michael Hennerichc58c2142007-10-04 00:35:05 +0800183
Michael Hennerich8c613622007-08-03 17:48:09 +0800184#define RESOURCE_LABEL_SIZE 16
185
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800186static struct str_ident {
Michael Hennerich8c613622007-08-03 17:48:09 +0800187 char name[RESOURCE_LABEL_SIZE];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800188} str_ident[MAX_RESOURCES];
Bryan Wu1394f032007-05-06 14:50:22 -0700189
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800190#if defined(CONFIG_PM)
191#if defined(CONFIG_BF54x)
Bryan Wu397861c2008-11-18 17:48:22 +0800192static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800193#else
Bryan Wu397861c2008-11-18 17:48:22 +0800194static unsigned short wakeup_map[GPIO_BANK_NUM];
Bryan Wu1394f032007-05-06 14:50:22 -0700195static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
Bryan Wu397861c2008-11-18 17:48:22 +0800196static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
Bryan Wu1394f032007-05-06 14:50:22 -0700197
198#ifdef BF533_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800199static unsigned int sic_iwr_irqs[] = {IRQ_PROG_INTB};
Bryan Wu1394f032007-05-06 14:50:22 -0700200#endif
201
202#ifdef BF537_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800203static unsigned int sic_iwr_irqs[] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
Bryan Wu1394f032007-05-06 14:50:22 -0700204#endif
205
Michael Hennerichdc26aec2008-11-18 17:48:22 +0800206#ifdef BF538_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800207static unsigned int sic_iwr_irqs[] = {IRQ_PORTF_INTB};
Michael Hennerichdc26aec2008-11-18 17:48:22 +0800208#endif
209
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800210#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Bryan Wu397861c2008-11-18 17:48:22 +0800211static unsigned int sic_iwr_irqs[] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
Michael Hennerich59003142007-10-21 16:54:27 +0800212#endif
213
Bryan Wu1394f032007-05-06 14:50:22 -0700214#ifdef BF561_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800215static unsigned int sic_iwr_irqs[] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
Bryan Wu1394f032007-05-06 14:50:22 -0700216#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800217#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700218#endif /* CONFIG_PM */
219
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800220inline int check_gpio(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800221{
Mike Frysinger27228b22008-10-28 15:45:42 +0800222#if defined(BF548_FAMILY)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800223 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
224 || gpio == GPIO_PH14 || gpio == GPIO_PH15
Mike Frysinger27228b22008-10-28 15:45:42 +0800225 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800226 return -EINVAL;
Mike Frysinger27228b22008-10-28 15:45:42 +0800227#endif
Michael Henneriche7613aa2007-06-11 16:37:57 +0800228 if (gpio >= MAX_BLACKFIN_GPIOS)
Bryan Wu1394f032007-05-06 14:50:22 -0700229 return -EINVAL;
230 return 0;
231}
232
Mike Frysinger74c04502008-10-08 16:13:17 +0800233static void gpio_error(unsigned gpio)
Michael Hennerichacbcd262008-01-22 18:36:20 +0800234{
235 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
236}
237
Michael Hennerichc58c2142007-10-04 00:35:05 +0800238static void set_label(unsigned short ident, const char *label)
239{
Michael Henneriche9fae182008-10-13 11:35:22 +0800240 if (label) {
Michael Hennerich8c613622007-08-03 17:48:09 +0800241 strncpy(str_ident[ident].name, label,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800242 RESOURCE_LABEL_SIZE);
Michael Hennerich8c613622007-08-03 17:48:09 +0800243 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800244 }
245}
246
247static char *get_label(unsigned short ident)
248{
Michael Hennerich8c613622007-08-03 17:48:09 +0800249 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
Michael Hennerichc58c2142007-10-04 00:35:05 +0800250}
251
252static int cmp_label(unsigned short ident, const char *label)
253{
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800254 if (label == NULL) {
255 dump_stack();
256 printk(KERN_ERR "Please provide none-null label\n");
257 }
258
Michael Henneriche9fae182008-10-13 11:35:22 +0800259 if (label)
Mike Frysinger1f7d3732008-10-28 15:47:11 +0800260 return strcmp(str_ident[ident].name, label);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800261 else
262 return -EINVAL;
263}
264
Mike Frysingera2d03a12008-10-28 15:53:37 +0800265static void port_setup(unsigned gpio, unsigned short usage)
266{
267 if (check_gpio(gpio))
268 return;
269
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800270#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Mike Frysingera2d03a12008-10-28 15:53:37 +0800271 if (usage == GPIO_USAGE)
272 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
273 else
274 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
275 SSYNC();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800276#elif defined(BF548_FAMILY)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800277 if (usage == GPIO_USAGE)
278 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
279 else
280 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
281 SSYNC();
Bryan Wu1394f032007-05-06 14:50:22 -0700282#endif
Mike Frysingera2d03a12008-10-28 15:53:37 +0800283}
Bryan Wu1394f032007-05-06 14:50:22 -0700284
Michael Hennerichc58c2142007-10-04 00:35:05 +0800285#ifdef BF537_FAMILY
Michael Hennerich8c613622007-08-03 17:48:09 +0800286static struct {
287 unsigned short res;
288 unsigned short offset;
289} port_mux_lut[] = {
290 {.res = P_PPI0_D13, .offset = 11},
291 {.res = P_PPI0_D14, .offset = 11},
292 {.res = P_PPI0_D15, .offset = 11},
293 {.res = P_SPORT1_TFS, .offset = 11},
294 {.res = P_SPORT1_TSCLK, .offset = 11},
295 {.res = P_SPORT1_DTPRI, .offset = 11},
296 {.res = P_PPI0_D10, .offset = 10},
297 {.res = P_PPI0_D11, .offset = 10},
298 {.res = P_PPI0_D12, .offset = 10},
299 {.res = P_SPORT1_RSCLK, .offset = 10},
300 {.res = P_SPORT1_RFS, .offset = 10},
301 {.res = P_SPORT1_DRPRI, .offset = 10},
302 {.res = P_PPI0_D8, .offset = 9},
303 {.res = P_PPI0_D9, .offset = 9},
304 {.res = P_SPORT1_DRSEC, .offset = 9},
305 {.res = P_SPORT1_DTSEC, .offset = 9},
306 {.res = P_TMR2, .offset = 8},
307 {.res = P_PPI0_FS3, .offset = 8},
308 {.res = P_TMR3, .offset = 7},
309 {.res = P_SPI0_SSEL4, .offset = 7},
310 {.res = P_TMR4, .offset = 6},
311 {.res = P_SPI0_SSEL5, .offset = 6},
312 {.res = P_TMR5, .offset = 5},
313 {.res = P_SPI0_SSEL6, .offset = 5},
314 {.res = P_UART1_RX, .offset = 4},
315 {.res = P_UART1_TX, .offset = 4},
316 {.res = P_TMR6, .offset = 4},
317 {.res = P_TMR7, .offset = 4},
318 {.res = P_UART0_RX, .offset = 3},
319 {.res = P_UART0_TX, .offset = 3},
320 {.res = P_DMAR0, .offset = 3},
321 {.res = P_DMAR1, .offset = 3},
322 {.res = P_SPORT0_DTSEC, .offset = 1},
323 {.res = P_SPORT0_DRSEC, .offset = 1},
324 {.res = P_CAN0_RX, .offset = 1},
325 {.res = P_CAN0_TX, .offset = 1},
326 {.res = P_SPI0_SSEL7, .offset = 1},
327 {.res = P_SPORT0_TFS, .offset = 0},
328 {.res = P_SPORT0_DTPRI, .offset = 0},
329 {.res = P_SPI0_SSEL2, .offset = 0},
330 {.res = P_SPI0_SSEL3, .offset = 0},
Michael Hennerichc58c2142007-10-04 00:35:05 +0800331};
332
333static void portmux_setup(unsigned short per, unsigned short function)
334{
Michael Hennerich8c613622007-08-03 17:48:09 +0800335 u16 y, offset, muxreg;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800336
Michael Hennerich8c613622007-08-03 17:48:09 +0800337 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
338 if (port_mux_lut[y].res == per) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800339
340 /* SET PORTMUX REG */
341
Michael Hennerich8c613622007-08-03 17:48:09 +0800342 offset = port_mux_lut[y].offset;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800343 muxreg = bfin_read_PORT_MUX();
344
Mike Frysingerd171c232008-03-26 08:35:46 +0800345 if (offset != 1)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800346 muxreg &= ~(1 << offset);
Mike Frysingerd171c232008-03-26 08:35:46 +0800347 else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800348 muxreg &= ~(3 << 1);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800349
350 muxreg |= (function << offset);
351 bfin_write_PORT_MUX(muxreg);
352 }
353 }
354}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800355#elif defined(BF548_FAMILY)
356inline void portmux_setup(unsigned short portno, unsigned short function)
357{
358 u32 pmux;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800359
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800360 pmux = gpio_array[gpio_bank(portno)]->port_mux;
361
362 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
363 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
364
365 gpio_array[gpio_bank(portno)]->port_mux = pmux;
366}
367
368inline u16 get_portmux(unsigned short portno)
369{
370 u32 pmux;
371
372 pmux = gpio_array[gpio_bank(portno)]->port_mux;
373
374 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
375}
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800376#elif defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich59003142007-10-21 16:54:27 +0800377inline void portmux_setup(unsigned short portno, unsigned short function)
378{
379 u16 pmux, ident = P_IDENT(portno);
380 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
381
382 pmux = *port_mux[gpio_bank(ident)];
383 pmux &= ~(3 << offset);
384 pmux |= (function & 3) << offset;
385 *port_mux[gpio_bank(ident)] = pmux;
386 SSYNC();
387}
Michael Hennerichc58c2142007-10-04 00:35:05 +0800388#else
389# define portmux_setup(...) do { } while (0)
390#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700391
Mike Frysingera161bb02007-05-21 18:09:14 +0800392static int __init bfin_gpio_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700393{
Bryan Wu1394f032007-05-06 14:50:22 -0700394 printk(KERN_INFO "Blackfin GPIO Controller\n");
395
Bryan Wu1394f032007-05-06 14:50:22 -0700396 return 0;
397}
Bryan Wu1394f032007-05-06 14:50:22 -0700398arch_initcall(bfin_gpio_init);
399
400
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800401#ifndef BF548_FAMILY
Bryan Wu1394f032007-05-06 14:50:22 -0700402/***********************************************************
403*
404* FUNCTIONS: Blackfin General Purpose Ports Access Functions
405*
406* INPUTS/OUTPUTS:
407* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
408*
409*
410* DESCRIPTION: These functions abstract direct register access
411* to Blackfin processor General Purpose
412* Ports Regsiters
413*
414* CAUTION: These functions do not belong to the GPIO Driver API
415*************************************************************
416* MODIFICATION HISTORY :
417**************************************************************/
418
419/* Set a specific bit */
420
421#define SET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800422void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700423{ \
424 unsigned long flags; \
Yi Li6a01f232009-01-07 23:14:39 +0800425 local_irq_save_hw(flags); \
Bryan Wu1394f032007-05-06 14:50:22 -0700426 if (arg) \
427 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
428 else \
429 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800430 AWA_DUMMY_READ(name); \
Yi Li6a01f232009-01-07 23:14:39 +0800431 local_irq_restore_hw(flags); \
Bryan Wu1394f032007-05-06 14:50:22 -0700432} \
433EXPORT_SYMBOL(set_gpio_ ## name);
434
435SET_GPIO(dir)
436SET_GPIO(inen)
437SET_GPIO(polar)
438SET_GPIO(edge)
439SET_GPIO(both)
440
441
Michael Hennerich2b393312007-10-10 16:58:49 +0800442#if ANOMALY_05000311 || ANOMALY_05000323
443#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800444void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800445{ \
446 unsigned long flags; \
Yi Li6a01f232009-01-07 23:14:39 +0800447 local_irq_save_hw(flags); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800448 if (arg) \
449 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
450 else \
451 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
452 AWA_DUMMY_READ(name); \
Yi Li6a01f232009-01-07 23:14:39 +0800453 local_irq_restore_hw(flags); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800454} \
455EXPORT_SYMBOL(set_gpio_ ## name);
456#else
Bryan Wu1394f032007-05-06 14:50:22 -0700457#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800458void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700459{ \
Bryan Wu1394f032007-05-06 14:50:22 -0700460 if (arg) \
461 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
462 else \
463 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
464} \
465EXPORT_SYMBOL(set_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800466#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700467
468SET_GPIO_SC(maska)
469SET_GPIO_SC(maskb)
Bryan Wu1394f032007-05-06 14:50:22 -0700470SET_GPIO_SC(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700471
Michael Hennerich2b393312007-10-10 16:58:49 +0800472#if ANOMALY_05000311 || ANOMALY_05000323
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800473void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700474{
475 unsigned long flags;
Yi Li6a01f232009-01-07 23:14:39 +0800476 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700477 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +0800478 AWA_DUMMY_READ(toggle);
Yi Li6a01f232009-01-07 23:14:39 +0800479 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700480}
481#else
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800482void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700483{
Bryan Wu1394f032007-05-06 14:50:22 -0700484 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
485}
486#endif
487EXPORT_SYMBOL(set_gpio_toggle);
488
489
490/*Set current PORT date (16-bit word)*/
491
Michael Hennerich2b393312007-10-10 16:58:49 +0800492#if ANOMALY_05000311 || ANOMALY_05000323
493#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800494void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800495{ \
496 unsigned long flags; \
Yi Li6a01f232009-01-07 23:14:39 +0800497 local_irq_save_hw(flags); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800498 gpio_bankb[gpio_bank(gpio)]->name = arg; \
499 AWA_DUMMY_READ(name); \
Yi Li6a01f232009-01-07 23:14:39 +0800500 local_irq_restore_hw(flags); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800501} \
502EXPORT_SYMBOL(set_gpiop_ ## name);
503#else
Bryan Wu1394f032007-05-06 14:50:22 -0700504#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800505void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700506{ \
507 gpio_bankb[gpio_bank(gpio)]->name = arg; \
508} \
509EXPORT_SYMBOL(set_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800510#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700511
Michael Hennerich2b393312007-10-10 16:58:49 +0800512SET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700513SET_GPIO_P(dir)
514SET_GPIO_P(inen)
515SET_GPIO_P(polar)
516SET_GPIO_P(edge)
517SET_GPIO_P(both)
518SET_GPIO_P(maska)
519SET_GPIO_P(maskb)
520
Bryan Wu1394f032007-05-06 14:50:22 -0700521/* Get a specific bit */
Michael Hennerich2b393312007-10-10 16:58:49 +0800522#if ANOMALY_05000311 || ANOMALY_05000323
523#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800524unsigned short get_gpio_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800525{ \
526 unsigned long flags; \
527 unsigned short ret; \
Yi Li6a01f232009-01-07 23:14:39 +0800528 local_irq_save_hw(flags); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800529 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
530 AWA_DUMMY_READ(name); \
Yi Li6a01f232009-01-07 23:14:39 +0800531 local_irq_restore_hw(flags); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800532 return ret; \
533} \
534EXPORT_SYMBOL(get_gpio_ ## name);
535#else
Bryan Wu1394f032007-05-06 14:50:22 -0700536#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800537unsigned short get_gpio_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700538{ \
539 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
540} \
541EXPORT_SYMBOL(get_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800542#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700543
Michael Hennerich2b393312007-10-10 16:58:49 +0800544GET_GPIO(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700545GET_GPIO(dir)
546GET_GPIO(inen)
547GET_GPIO(polar)
548GET_GPIO(edge)
549GET_GPIO(both)
550GET_GPIO(maska)
551GET_GPIO(maskb)
552
Bryan Wu1394f032007-05-06 14:50:22 -0700553/*Get current PORT date (16-bit word)*/
554
Michael Hennerich2b393312007-10-10 16:58:49 +0800555#if ANOMALY_05000311 || ANOMALY_05000323
556#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800557unsigned short get_gpiop_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800558{ \
559 unsigned long flags; \
560 unsigned short ret; \
Yi Li6a01f232009-01-07 23:14:39 +0800561 local_irq_save_hw(flags); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800562 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
563 AWA_DUMMY_READ(name); \
Yi Li6a01f232009-01-07 23:14:39 +0800564 local_irq_restore_hw(flags); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800565 return ret; \
566} \
567EXPORT_SYMBOL(get_gpiop_ ## name);
568#else
Bryan Wu1394f032007-05-06 14:50:22 -0700569#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800570unsigned short get_gpiop_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700571{ \
572 return (gpio_bankb[gpio_bank(gpio)]->name);\
573} \
574EXPORT_SYMBOL(get_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800575#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700576
Michael Hennerich2b393312007-10-10 16:58:49 +0800577GET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700578GET_GPIO_P(dir)
579GET_GPIO_P(inen)
580GET_GPIO_P(polar)
581GET_GPIO_P(edge)
582GET_GPIO_P(both)
583GET_GPIO_P(maska)
584GET_GPIO_P(maskb)
585
Bryan Wu1394f032007-05-06 14:50:22 -0700586
587#ifdef CONFIG_PM
588/***********************************************************
589*
590* FUNCTIONS: Blackfin PM Setup API
591*
592* INPUTS/OUTPUTS:
593* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
594* type -
595* PM_WAKE_RISING
596* PM_WAKE_FALLING
597* PM_WAKE_HIGH
598* PM_WAKE_LOW
599* PM_WAKE_BOTH_EDGES
600*
601* DESCRIPTION: Blackfin PM Driver API
602*
603* CAUTION:
604*************************************************************
605* MODIFICATION HISTORY :
606**************************************************************/
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800607int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700608{
609 unsigned long flags;
610
611 if ((check_gpio(gpio) < 0) || !type)
612 return -EINVAL;
613
Yi Li6a01f232009-01-07 23:14:39 +0800614 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700615 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
616 wakeup_flags_map[gpio] = type;
Yi Li6a01f232009-01-07 23:14:39 +0800617 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700618
619 return 0;
620}
621EXPORT_SYMBOL(gpio_pm_wakeup_request);
622
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800623void gpio_pm_wakeup_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700624{
625 unsigned long flags;
626
627 if (check_gpio(gpio) < 0)
628 return;
629
Yi Li6a01f232009-01-07 23:14:39 +0800630 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700631
632 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
633
Yi Li6a01f232009-01-07 23:14:39 +0800634 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700635}
636EXPORT_SYMBOL(gpio_pm_wakeup_free);
637
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800638static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700639{
640 port_setup(gpio, GPIO_USAGE);
641 set_gpio_dir(gpio, 0);
642 set_gpio_inen(gpio, 1);
643
644 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
645 set_gpio_edge(gpio, 1);
646 else
647 set_gpio_edge(gpio, 0);
648
649 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
650 set_gpio_both(gpio, 1);
651 else
652 set_gpio_both(gpio, 0);
653
654 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
655 set_gpio_polar(gpio, 1);
656 else
657 set_gpio_polar(gpio, 0);
658
659 SSYNC();
660
661 return 0;
662}
663
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800664u32 bfin_pm_standby_setup(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700665{
Bryan Wu1394f032007-05-06 14:50:22 -0700666 u16 bank, mask, i, gpio;
667
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800668 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700669 mask = wakeup_map[gpio_bank(i)];
670 bank = gpio_bank(i);
671
672 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
673 gpio_bankb[bank]->maskb = 0;
674
675 if (mask) {
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800676#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700677 gpio_bank_saved[bank].fer = *port_fer[bank];
678#endif
679 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
680 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
681 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
682 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
683 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800684 gpio_bank_saved[bank].reserved =
685 reserved_gpio_map[bank];
Bryan Wu1394f032007-05-06 14:50:22 -0700686
687 gpio = i;
688
689 while (mask) {
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800690 if ((mask & 1) && (wakeup_flags_map[gpio] !=
691 PM_WAKE_IGNORE)) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800692 reserved_gpio_map[gpio_bank(gpio)] |=
Michael Hennerich581d62a2007-06-14 13:30:23 +0800693 gpio_bit(gpio);
694 bfin_gpio_wakeup_type(gpio,
695 wakeup_flags_map[gpio]);
Bryan Wu1394f032007-05-06 14:50:22 -0700696 set_gpio_data(gpio, 0); /*Clear*/
697 }
698 gpio++;
699 mask >>= 1;
700 }
701
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800702 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
Bryan Wu1394f032007-05-06 14:50:22 -0700703 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
704 }
705 }
706
Michael Hennerich2b393312007-10-10 16:58:49 +0800707 AWA_DUMMY_READ(maskb_set);
708
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800709 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700710}
711
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800712void bfin_pm_standby_restore(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700713{
714 u16 bank, mask, i;
715
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800716 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700717 mask = wakeup_map[gpio_bank(i)];
718 bank = gpio_bank(i);
719
720 if (mask) {
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800721#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700722 *port_fer[bank] = gpio_bank_saved[bank].fer;
723#endif
724 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
725 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
726 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
727 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
728 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800729
Michael Hennerichc58c2142007-10-04 00:35:05 +0800730 reserved_gpio_map[bank] =
731 gpio_bank_saved[bank].reserved;
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800732 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
Bryan Wu1394f032007-05-06 14:50:22 -0700733 }
734
735 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
736 }
Michael Hennerich2b393312007-10-10 16:58:49 +0800737 AWA_DUMMY_READ(maskb);
Bryan Wu1394f032007-05-06 14:50:22 -0700738}
739
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800740void bfin_gpio_pm_hibernate_suspend(void)
741{
742 int i, bank;
743
744 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
745 bank = gpio_bank(i);
746
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800747#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800748 gpio_bank_saved[bank].fer = *port_fer[bank];
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800749#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800750 gpio_bank_saved[bank].mux = *port_mux[bank];
751#else
752 if (bank == 0)
753 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
754#endif
755#endif
756 gpio_bank_saved[bank].data = gpio_bankb[bank]->data;
757 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
758 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
759 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
760 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
761 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
762 gpio_bank_saved[bank].maska = gpio_bankb[bank]->maska;
763 }
764
765 AWA_DUMMY_READ(maska);
766}
767
768void bfin_gpio_pm_hibernate_restore(void)
769{
770 int i, bank;
771
772 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
773 bank = gpio_bank(i);
774
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800775#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
776#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800777 *port_mux[bank] = gpio_bank_saved[bank].mux;
778#else
779 if (bank == 0)
780 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
781#endif
782 *port_fer[bank] = gpio_bank_saved[bank].fer;
783#endif
784 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
785 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
786 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
787 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
788 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
789
790 gpio_bankb[bank]->data_set = gpio_bank_saved[bank].data
791 | gpio_bank_saved[bank].dir;
792
793 gpio_bankb[bank]->maska = gpio_bank_saved[bank].maska;
794 }
795 AWA_DUMMY_READ(maska);
796}
797
798
Bryan Wu1394f032007-05-06 14:50:22 -0700799#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800800#else /* BF548_FAMILY */
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800801#ifdef CONFIG_PM
802
803u32 bfin_pm_standby_setup(void)
804{
805 return 0;
806}
807
808void bfin_pm_standby_restore(void)
809{
810
811}
812
813void bfin_gpio_pm_hibernate_suspend(void)
814{
815 int i, bank;
816
817 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
818 bank = gpio_bank(i);
819
820 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
821 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
822 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
823 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
824 gpio_bank_saved[bank].inen = gpio_array[bank]->port_inen;
825 gpio_bank_saved[bank].dir = gpio_array[bank]->port_dir_set;
826 }
827}
828
829void bfin_gpio_pm_hibernate_restore(void)
830{
831 int i, bank;
832
833 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
834 bank = gpio_bank(i);
835
836 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
837 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
838 gpio_array[bank]->port_inen = gpio_bank_saved[bank].inen;
839 gpio_array[bank]->port_dir_set = gpio_bank_saved[bank].dir;
840 gpio_array[bank]->port_set = gpio_bank_saved[bank].data
841 | gpio_bank_saved[bank].dir;
842 }
843}
844#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800845
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800846unsigned short get_gpio_dir(unsigned gpio)
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800847{
848 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
849}
850EXPORT_SYMBOL(get_gpio_dir);
851
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800852#endif /* BF548_FAMILY */
Bryan Wu1394f032007-05-06 14:50:22 -0700853
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800854/***********************************************************
855*
856* FUNCTIONS: Blackfin Peripheral Resource Allocation
857* and PortMux Setup
858*
859* INPUTS/OUTPUTS:
860* per Peripheral Identifier
861* label String
862*
863* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
864*
865* CAUTION:
866*************************************************************
867* MODIFICATION HISTORY :
868**************************************************************/
Michael Hennerichc58c2142007-10-04 00:35:05 +0800869
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800870int peripheral_request(unsigned short per, const char *label)
871{
872 unsigned long flags;
873 unsigned short ident = P_IDENT(per);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800874
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800875 /*
876 * Don't cares are pins with only one dedicated function
877 */
878
879 if (per & P_DONTCARE)
880 return 0;
881
882 if (!(per & P_DEFINED))
883 return -ENODEV;
884
Yi Li6a01f232009-01-07 23:14:39 +0800885 local_irq_save_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800886
Mike Frysinger6a87d292008-10-28 16:16:29 +0800887 /* If a pin can be muxed as either GPIO or peripheral, make
888 * sure it is not already a GPIO pin when we request it.
889 */
890 if (unlikely(!check_gpio(ident) &&
891 reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800892 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800893 printk(KERN_ERR
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800894 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800895 __func__, ident, get_label(ident));
Yi Li6a01f232009-01-07 23:14:39 +0800896 local_irq_restore_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800897 return -EBUSY;
898 }
899
900 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
901
Mike Frysingerd171c232008-03-26 08:35:46 +0800902 /*
903 * Pin functions like AMC address strobes my
904 * be requested and used by several drivers
905 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800906
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800907#ifdef BF548_FAMILY
908 u16 funct = get_portmux(ident);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800909
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800910 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
911#else
912 if (!(per & P_MAYSHARE)) {
913#endif
Mike Frysingerd171c232008-03-26 08:35:46 +0800914 /*
915 * Allow that the identical pin function can
916 * be requested from the same driver twice
917 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800918
Mike Frysingerd171c232008-03-26 08:35:46 +0800919 if (cmp_label(ident, label) == 0)
920 goto anyway;
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800921
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800922 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800923 printk(KERN_ERR
924 "%s: Peripheral %d function %d is already reserved by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800925 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
Yi Li6a01f232009-01-07 23:14:39 +0800926 local_irq_restore_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800927 return -EBUSY;
928 }
929 }
930
Mike Frysingerd171c232008-03-26 08:35:46 +0800931 anyway:
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800932 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
933
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800934#ifdef BF548_FAMILY
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800935 portmux_setup(ident, P_FUNCT2MUX(per));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800936#else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800937 portmux_setup(per, P_FUNCT2MUX(per));
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800938#endif
Michael Hennerichc58c2142007-10-04 00:35:05 +0800939 port_setup(ident, PERIPHERAL_USAGE);
940
Yi Li6a01f232009-01-07 23:14:39 +0800941 local_irq_restore_hw(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800942 set_label(ident, label);
943
944 return 0;
945}
946EXPORT_SYMBOL(peripheral_request);
947
Mike Frysinger68179372008-04-24 05:04:24 +0800948int peripheral_request_list(const unsigned short per[], const char *label)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800949{
950 u16 cnt;
951 int ret;
952
953 for (cnt = 0; per[cnt] != 0; cnt++) {
Michael Hennerich314c98d2007-07-24 18:03:45 +0800954
Michael Hennerichc58c2142007-10-04 00:35:05 +0800955 ret = peripheral_request(per[cnt], label);
Michael Hennerich314c98d2007-07-24 18:03:45 +0800956
957 if (ret < 0) {
Mike Frysingerd171c232008-03-26 08:35:46 +0800958 for ( ; cnt > 0; cnt--)
Michael Hennerich314c98d2007-07-24 18:03:45 +0800959 peripheral_free(per[cnt - 1]);
Mike Frysingerd171c232008-03-26 08:35:46 +0800960
961 return ret;
Michael Hennerich314c98d2007-07-24 18:03:45 +0800962 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800963 }
964
965 return 0;
966}
967EXPORT_SYMBOL(peripheral_request_list);
968
969void peripheral_free(unsigned short per)
970{
971 unsigned long flags;
972 unsigned short ident = P_IDENT(per);
973
974 if (per & P_DONTCARE)
975 return;
976
977 if (!(per & P_DEFINED))
978 return;
979
980 if (check_gpio(ident) < 0)
981 return;
982
Yi Li6a01f232009-01-07 23:14:39 +0800983 local_irq_save_hw(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800984
Mike Frysingerd171c232008-03-26 08:35:46 +0800985 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
Yi Li6a01f232009-01-07 23:14:39 +0800986 local_irq_restore_hw(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800987 return;
988 }
989
Mike Frysingerd171c232008-03-26 08:35:46 +0800990 if (!(per & P_MAYSHARE))
Michael Hennerichc58c2142007-10-04 00:35:05 +0800991 port_setup(ident, GPIO_USAGE);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800992
993 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
994
Michael Hennerich2acde902007-10-11 00:24:40 +0800995 set_label(ident, "free");
996
Yi Li6a01f232009-01-07 23:14:39 +0800997 local_irq_restore_hw(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800998}
999EXPORT_SYMBOL(peripheral_free);
1000
Mike Frysinger68179372008-04-24 05:04:24 +08001001void peripheral_free_list(const unsigned short per[])
Michael Hennerichc58c2142007-10-04 00:35:05 +08001002{
1003 u16 cnt;
Mike Frysingerd171c232008-03-26 08:35:46 +08001004 for (cnt = 0; per[cnt] != 0; cnt++)
Michael Hennerichc58c2142007-10-04 00:35:05 +08001005 peripheral_free(per[cnt]);
Michael Hennerichc58c2142007-10-04 00:35:05 +08001006}
1007EXPORT_SYMBOL(peripheral_free_list);
1008
Bryan Wu1394f032007-05-06 14:50:22 -07001009/***********************************************************
1010*
1011* FUNCTIONS: Blackfin GPIO Driver
1012*
1013* INPUTS/OUTPUTS:
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001014* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1015* label String
Bryan Wu1394f032007-05-06 14:50:22 -07001016*
1017* DESCRIPTION: Blackfin GPIO Driver API
1018*
1019* CAUTION:
1020*************************************************************
1021* MODIFICATION HISTORY :
1022**************************************************************/
1023
Michael Hennericha4f0b322008-11-18 17:48:22 +08001024int bfin_gpio_request(unsigned gpio, const char *label)
Bryan Wu1394f032007-05-06 14:50:22 -07001025{
1026 unsigned long flags;
1027
1028 if (check_gpio(gpio) < 0)
1029 return -EINVAL;
1030
Yi Li6a01f232009-01-07 23:14:39 +08001031 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001032
Michael Hennerich2acde902007-10-11 00:24:40 +08001033 /*
1034 * Allow that the identical GPIO can
1035 * be requested from the same driver twice
1036 * Do nothing and return -
1037 */
1038
1039 if (cmp_label(gpio, label) == 0) {
Yi Li6a01f232009-01-07 23:14:39 +08001040 local_irq_restore_hw(flags);
Michael Hennerich2acde902007-10-11 00:24:40 +08001041 return 0;
1042 }
1043
Michael Hennerichc58c2142007-10-04 00:35:05 +08001044 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001045 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001046 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
Graf Yang9570ff42009-01-07 23:14:38 +08001047 gpio, get_label(gpio));
Yi Li6a01f232009-01-07 23:14:39 +08001048 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001049 return -EBUSY;
1050 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001051 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001052 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001053 printk(KERN_ERR
1054 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1055 gpio, get_label(gpio));
Yi Li6a01f232009-01-07 23:14:39 +08001056 local_irq_restore_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001057 return -EBUSY;
1058 }
Graf Yang9570ff42009-01-07 23:14:38 +08001059 if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))
1060 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
1061 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001062
Michael Hennerichc58c2142007-10-04 00:35:05 +08001063 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Graf Yang9570ff42009-01-07 23:14:38 +08001064 set_label(gpio, label);
Bryan Wu1394f032007-05-06 14:50:22 -07001065
Yi Li6a01f232009-01-07 23:14:39 +08001066 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001067
1068 port_setup(gpio, GPIO_USAGE);
1069
1070 return 0;
1071}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001072EXPORT_SYMBOL(bfin_gpio_request);
Bryan Wu1394f032007-05-06 14:50:22 -07001073
Michael Hennericha4f0b322008-11-18 17:48:22 +08001074void bfin_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
Yi Li6a01f232009-01-07 23:14:39 +08001081 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001082
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);
Yi Li6a01f232009-01-07 23:14:39 +08001086 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001087 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
Yi Li6a01f232009-01-07 23:14:39 +08001094 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001095}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001096EXPORT_SYMBOL(bfin_gpio_free);
Bryan Wu1394f032007-05-06 14:50:22 -07001097
Graf Yang9570ff42009-01-07 23:14:38 +08001098int bfin_gpio_irq_request(unsigned gpio, const char *label)
1099{
1100 unsigned long flags;
1101
1102 if (check_gpio(gpio) < 0)
1103 return -EINVAL;
1104
Yi Li6a01f232009-01-07 23:14:39 +08001105 local_irq_save_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001106
1107 if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1108 dump_stack();
1109 printk(KERN_ERR
1110 "bfin-gpio: GPIO %d is already reserved as gpio-irq !\n",
1111 gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001112 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001113 return -EBUSY;
1114 }
1115 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1116 dump_stack();
1117 printk(KERN_ERR
1118 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1119 gpio, get_label(gpio));
Yi Li6a01f232009-01-07 23:14:39 +08001120 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001121 return -EBUSY;
1122 }
1123 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))
1124 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1125 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1126 gpio, get_label(gpio));
1127
1128 reserved_gpio_irq_map[gpio_bank(gpio)] |= gpio_bit(gpio);
1129 set_label(gpio, label);
1130
Yi Li6a01f232009-01-07 23:14:39 +08001131 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001132
1133 port_setup(gpio, GPIO_USAGE);
1134
1135 return 0;
1136}
1137
1138void bfin_gpio_irq_free(unsigned gpio)
1139{
1140 unsigned long flags;
1141
1142 if (check_gpio(gpio) < 0)
1143 return;
1144
Yi Li6a01f232009-01-07 23:14:39 +08001145 local_irq_save_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001146
1147 if (unlikely(!(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1148 dump_stack();
1149 gpio_error(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001150 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001151 return;
1152 }
1153
1154 reserved_gpio_irq_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1155
1156 set_label(gpio, "free");
1157
Yi Li6a01f232009-01-07 23:14:39 +08001158 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001159}
1160
Michael Hennerichacbcd262008-01-22 18:36:20 +08001161
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001162#ifdef BF548_FAMILY
Michael Hennericha4f0b322008-11-18 17:48:22 +08001163int bfin_gpio_direction_input(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001164{
1165 unsigned long flags;
1166
Michael Hennerichacbcd262008-01-22 18:36:20 +08001167 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1168 gpio_error(gpio);
1169 return -EINVAL;
1170 }
1171
Yi Li6a01f232009-01-07 23:14:39 +08001172 local_irq_save_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001173 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1174 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001175 local_irq_restore_hw(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001176
1177 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001178}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001179EXPORT_SYMBOL(bfin_gpio_direction_input);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001180
Michael Hennericha4f0b322008-11-18 17:48:22 +08001181int bfin_gpio_direction_output(unsigned gpio, int value)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001182{
1183 unsigned long flags;
1184
Michael Hennerichacbcd262008-01-22 18:36:20 +08001185 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1186 gpio_error(gpio);
1187 return -EINVAL;
1188 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001189
Yi Li6a01f232009-01-07 23:14:39 +08001190 local_irq_save_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001191 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001192 gpio_set_value(gpio, value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001193 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001194 local_irq_restore_hw(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001195
1196 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001197}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001198EXPORT_SYMBOL(bfin_gpio_direction_output);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001199
Michael Hennericha4f0b322008-11-18 17:48:22 +08001200void bfin_gpio_set_value(unsigned gpio, int arg)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001201{
1202 if (arg)
1203 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1204 else
1205 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001206}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001207EXPORT_SYMBOL(bfin_gpio_set_value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001208
Michael Hennericha4f0b322008-11-18 17:48:22 +08001209int bfin_gpio_get_value(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001210{
1211 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1212}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001213EXPORT_SYMBOL(bfin_gpio_get_value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001214
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001215void bfin_gpio_irq_prepare(unsigned gpio)
1216{
1217 unsigned long flags;
1218
1219 port_setup(gpio, GPIO_USAGE);
1220
Yi Li6a01f232009-01-07 23:14:39 +08001221 local_irq_save_hw(flags);
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001222 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1223 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001224 local_irq_restore_hw(flags);
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001225}
1226
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001227#else
1228
Michael Hennericha4f0b322008-11-18 17:48:22 +08001229int bfin_gpio_get_value(unsigned gpio)
Michael Hennerich803a8d22008-05-17 16:01:51 +08001230{
1231 unsigned long flags;
1232 int ret;
1233
1234 if (unlikely(get_gpio_edge(gpio))) {
Yi Li6a01f232009-01-07 23:14:39 +08001235 local_irq_save_hw(flags);
Michael Hennerich803a8d22008-05-17 16:01:51 +08001236 set_gpio_edge(gpio, 0);
1237 ret = get_gpio_data(gpio);
1238 set_gpio_edge(gpio, 1);
Yi Li6a01f232009-01-07 23:14:39 +08001239 local_irq_restore_hw(flags);
Michael Hennerich803a8d22008-05-17 16:01:51 +08001240
1241 return ret;
1242 } else
1243 return get_gpio_data(gpio);
1244}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001245EXPORT_SYMBOL(bfin_gpio_get_value);
Michael Hennerich803a8d22008-05-17 16:01:51 +08001246
1247
Michael Hennericha4f0b322008-11-18 17:48:22 +08001248int bfin_gpio_direction_input(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001249{
1250 unsigned long flags;
1251
Michael Hennerichacbcd262008-01-22 18:36:20 +08001252 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1253 gpio_error(gpio);
1254 return -EINVAL;
1255 }
Bryan Wu1394f032007-05-06 14:50:22 -07001256
Yi Li6a01f232009-01-07 23:14:39 +08001257 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001258 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1259 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001260 AWA_DUMMY_READ(inen);
Yi Li6a01f232009-01-07 23:14:39 +08001261 local_irq_restore_hw(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001262
1263 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001264}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001265EXPORT_SYMBOL(bfin_gpio_direction_input);
Bryan Wu1394f032007-05-06 14:50:22 -07001266
Michael Hennericha4f0b322008-11-18 17:48:22 +08001267int bfin_gpio_direction_output(unsigned gpio, int value)
Bryan Wu1394f032007-05-06 14:50:22 -07001268{
1269 unsigned long flags;
1270
Michael Hennerichacbcd262008-01-22 18:36:20 +08001271 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1272 gpio_error(gpio);
1273 return -EINVAL;
1274 }
Bryan Wu1394f032007-05-06 14:50:22 -07001275
Yi Li6a01f232009-01-07 23:14:39 +08001276 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001277 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001278
1279 if (value)
1280 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1281 else
1282 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1283
Bryan Wu1394f032007-05-06 14:50:22 -07001284 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001285 AWA_DUMMY_READ(dir);
Yi Li6a01f232009-01-07 23:14:39 +08001286 local_irq_restore_hw(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001287
1288 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001289}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001290EXPORT_SYMBOL(bfin_gpio_direction_output);
Mike Frysinger168f1212007-10-11 00:22:35 +08001291
1292/* If we are booting from SPI and our board lacks a strong enough pull up,
1293 * the core can reset and execute the bootrom faster than the resistor can
1294 * pull the signal logically high. To work around this (common) error in
1295 * board design, we explicitly set the pin back to GPIO mode, force /CS
1296 * high, and wait for the electrons to do their thing.
1297 *
1298 * This function only makes sense to be called from reset code, but it
1299 * lives here as we need to force all the GPIO states w/out going through
1300 * BUG() checks and such.
1301 */
1302void bfin_gpio_reset_spi0_ssel1(void)
1303{
Michael Hennerich4d5f4ed2007-08-27 16:46:17 +08001304 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1305
1306 port_setup(gpio, GPIO_USAGE);
1307 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001308 AWA_DUMMY_READ(data_set);
Mike Frysinger168f1212007-10-11 00:22:35 +08001309 udelay(1);
1310}
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001311
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001312void bfin_gpio_irq_prepare(unsigned gpio)
1313{
1314 port_setup(gpio, GPIO_USAGE);
1315}
1316
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001317#endif /*BF548_FAMILY */
Mike Frysinger1545a112007-12-24 16:54:48 +08001318
1319#if defined(CONFIG_PROC_FS)
1320static int gpio_proc_read(char *buf, char **start, off_t offset,
1321 int len, int *unused_i, void *unused_v)
1322{
Graf Yang9570ff42009-01-07 23:14:38 +08001323 int c, irq, gpio, outlen = 0;
Mike Frysinger1545a112007-12-24 16:54:48 +08001324
1325 for (c = 0; c < MAX_RESOURCES; c++) {
Graf Yang9570ff42009-01-07 23:14:38 +08001326 irq = reserved_gpio_irq_map[gpio_bank(c)] & gpio_bit(c);
1327 gpio = reserved_gpio_map[gpio_bank(c)] & gpio_bit(c);
1328 if (!check_gpio(c) && (gpio || irq))
1329 len = sprintf(buf, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1330 get_label(c), (gpio && irq) ? " *" : "",
1331 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
Mike Frysinger1545a112007-12-24 16:54:48 +08001332 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
Graf Yang8eb3e3b2008-11-18 17:48:22 +08001333 len = sprintf(buf, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
Mike Frysinger1545a112007-12-24 16:54:48 +08001334 else
1335 continue;
1336 buf += len;
1337 outlen += len;
1338 }
1339 return outlen;
1340}
1341
1342static __init int gpio_register_proc(void)
1343{
1344 struct proc_dir_entry *proc_gpio;
1345
1346 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1347 if (proc_gpio)
1348 proc_gpio->read_proc = gpio_proc_read;
1349 return proc_gpio != NULL;
1350}
Mike Frysinger1545a112007-12-24 16:54:48 +08001351__initcall(gpio_register_proc);
1352#endif
Michael Hennericha4f0b322008-11-18 17:48:22 +08001353
1354#ifdef CONFIG_GPIOLIB
1355int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1356{
1357 return bfin_gpio_direction_input(gpio);
1358}
1359
1360int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1361{
1362 return bfin_gpio_direction_output(gpio, level);
1363}
1364
1365int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1366{
1367 return bfin_gpio_get_value(gpio);
1368}
1369
1370void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1371{
1372#ifdef BF548_FAMILY
1373 return bfin_gpio_set_value(gpio, value);
1374#else
1375 return set_gpio_data(gpio, value);
1376#endif
1377}
1378
1379int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1380{
1381 return bfin_gpio_request(gpio, chip->label);
1382}
1383
1384void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1385{
1386 return bfin_gpio_free(gpio);
1387}
1388
1389static struct gpio_chip bfin_chip = {
1390 .label = "Blackfin-GPIOlib",
1391 .direction_input = bfin_gpiolib_direction_input,
1392 .get = bfin_gpiolib_get_value,
1393 .direction_output = bfin_gpiolib_direction_output,
1394 .set = bfin_gpiolib_set_value,
1395 .request = bfin_gpiolib_gpio_request,
1396 .free = bfin_gpiolib_gpio_free,
1397 .base = 0,
1398 .ngpio = MAX_BLACKFIN_GPIOS,
1399};
1400
1401static int __init bfin_gpiolib_setup(void)
1402{
1403 return gpiochip_add(&bfin_chip);
1404}
1405arch_initcall(bfin_gpiolib_setup);
1406#endif