blob: 05bf85ba7443158d00d8e75b65f034e9f74c5064 [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
Yi Li6a01f232009-01-07 23:14:39 +0800980 local_irq_save_hw(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800981
Mike Frysingerd171c232008-03-26 08:35:46 +0800982 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
Yi Li6a01f232009-01-07 23:14:39 +0800983 local_irq_restore_hw(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800984 return;
985 }
986
Mike Frysingerd171c232008-03-26 08:35:46 +0800987 if (!(per & P_MAYSHARE))
Michael Hennerichc58c2142007-10-04 00:35:05 +0800988 port_setup(ident, GPIO_USAGE);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800989
990 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
991
Michael Hennerich2acde902007-10-11 00:24:40 +0800992 set_label(ident, "free");
993
Yi Li6a01f232009-01-07 23:14:39 +0800994 local_irq_restore_hw(flags);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800995}
996EXPORT_SYMBOL(peripheral_free);
997
Mike Frysinger68179372008-04-24 05:04:24 +0800998void peripheral_free_list(const unsigned short per[])
Michael Hennerichc58c2142007-10-04 00:35:05 +0800999{
1000 u16 cnt;
Mike Frysingerd171c232008-03-26 08:35:46 +08001001 for (cnt = 0; per[cnt] != 0; cnt++)
Michael Hennerichc58c2142007-10-04 00:35:05 +08001002 peripheral_free(per[cnt]);
Michael Hennerichc58c2142007-10-04 00:35:05 +08001003}
1004EXPORT_SYMBOL(peripheral_free_list);
1005
Bryan Wu1394f032007-05-06 14:50:22 -07001006/***********************************************************
1007*
1008* FUNCTIONS: Blackfin GPIO Driver
1009*
1010* INPUTS/OUTPUTS:
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001011* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1012* label String
Bryan Wu1394f032007-05-06 14:50:22 -07001013*
1014* DESCRIPTION: Blackfin GPIO Driver API
1015*
1016* CAUTION:
1017*************************************************************
1018* MODIFICATION HISTORY :
1019**************************************************************/
1020
Michael Hennericha4f0b322008-11-18 17:48:22 +08001021int bfin_gpio_request(unsigned gpio, const char *label)
Bryan Wu1394f032007-05-06 14:50:22 -07001022{
1023 unsigned long flags;
1024
1025 if (check_gpio(gpio) < 0)
1026 return -EINVAL;
1027
Yi Li6a01f232009-01-07 23:14:39 +08001028 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001029
Michael Hennerich2acde902007-10-11 00:24:40 +08001030 /*
1031 * Allow that the identical GPIO can
1032 * be requested from the same driver twice
1033 * Do nothing and return -
1034 */
1035
1036 if (cmp_label(gpio, label) == 0) {
Yi Li6a01f232009-01-07 23:14:39 +08001037 local_irq_restore_hw(flags);
Michael Hennerich2acde902007-10-11 00:24:40 +08001038 return 0;
1039 }
1040
Michael Hennerichc58c2142007-10-04 00:35:05 +08001041 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001042 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001043 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
Graf Yang9570ff42009-01-07 23:14:38 +08001044 gpio, get_label(gpio));
Yi Li6a01f232009-01-07 23:14:39 +08001045 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001046 return -EBUSY;
1047 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001048 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001049 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001050 printk(KERN_ERR
1051 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1052 gpio, get_label(gpio));
Yi Li6a01f232009-01-07 23:14:39 +08001053 local_irq_restore_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001054 return -EBUSY;
1055 }
Michael Hennericha2be3932009-02-04 16:49:45 +08001056 if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Graf Yang9570ff42009-01-07 23:14:38 +08001057 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
1058 " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
Michael Hennericha2be3932009-02-04 16:49:45 +08001059 }
1060#ifndef BF548_FAMILY
1061 else { /* Reset POLAR setting when acquiring a gpio for the first time */
1062 set_gpio_polar(gpio, 0);
1063 }
1064#endif
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001065
Michael Hennerichc58c2142007-10-04 00:35:05 +08001066 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Graf Yang9570ff42009-01-07 23:14:38 +08001067 set_label(gpio, label);
Bryan Wu1394f032007-05-06 14:50:22 -07001068
Yi Li6a01f232009-01-07 23:14:39 +08001069 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001070
1071 port_setup(gpio, GPIO_USAGE);
1072
1073 return 0;
1074}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001075EXPORT_SYMBOL(bfin_gpio_request);
Bryan Wu1394f032007-05-06 14:50:22 -07001076
Michael Hennericha4f0b322008-11-18 17:48:22 +08001077void bfin_gpio_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001078{
1079 unsigned long flags;
1080
1081 if (check_gpio(gpio) < 0)
1082 return;
1083
Uwe Kleine-Koenig45c4f2a2009-02-04 17:02:30 +08001084 might_sleep();
1085
Yi Li6a01f232009-01-07 23:14:39 +08001086 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001087
Michael Hennerichc58c2142007-10-04 00:35:05 +08001088 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
Bryan Wu1394f032007-05-06 14:50:22 -07001089 dump_stack();
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001090 gpio_error(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001091 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001092 return;
1093 }
1094
Michael Hennerichc58c2142007-10-04 00:35:05 +08001095 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001096
Michael Hennerich2acde902007-10-11 00:24:40 +08001097 set_label(gpio, "free");
1098
Yi Li6a01f232009-01-07 23:14:39 +08001099 local_irq_restore_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001100}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001101EXPORT_SYMBOL(bfin_gpio_free);
Bryan Wu1394f032007-05-06 14:50:22 -07001102
Graf Yang9570ff42009-01-07 23:14:38 +08001103int bfin_gpio_irq_request(unsigned gpio, const char *label)
1104{
1105 unsigned long flags;
1106
1107 if (check_gpio(gpio) < 0)
1108 return -EINVAL;
1109
Yi Li6a01f232009-01-07 23:14:39 +08001110 local_irq_save_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001111
1112 if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1113 dump_stack();
1114 printk(KERN_ERR
1115 "bfin-gpio: GPIO %d is already reserved as gpio-irq !\n",
1116 gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001117 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001118 return -EBUSY;
1119 }
1120 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1121 dump_stack();
1122 printk(KERN_ERR
1123 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1124 gpio, get_label(gpio));
Yi Li6a01f232009-01-07 23:14:39 +08001125 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001126 return -EBUSY;
1127 }
1128 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))
1129 printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
1130 "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
1131 gpio, get_label(gpio));
1132
1133 reserved_gpio_irq_map[gpio_bank(gpio)] |= gpio_bit(gpio);
1134 set_label(gpio, label);
1135
Yi Li6a01f232009-01-07 23:14:39 +08001136 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001137
1138 port_setup(gpio, GPIO_USAGE);
1139
1140 return 0;
1141}
1142
1143void bfin_gpio_irq_free(unsigned gpio)
1144{
1145 unsigned long flags;
1146
1147 if (check_gpio(gpio) < 0)
1148 return;
1149
Yi Li6a01f232009-01-07 23:14:39 +08001150 local_irq_save_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001151
1152 if (unlikely(!(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1153 dump_stack();
1154 gpio_error(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001155 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001156 return;
1157 }
1158
1159 reserved_gpio_irq_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
1160
1161 set_label(gpio, "free");
1162
Yi Li6a01f232009-01-07 23:14:39 +08001163 local_irq_restore_hw(flags);
Graf Yang9570ff42009-01-07 23:14:38 +08001164}
1165
Michael Hennerichacbcd262008-01-22 18:36:20 +08001166
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001167#ifdef BF548_FAMILY
Michael Hennericha4f0b322008-11-18 17:48:22 +08001168int bfin_gpio_direction_input(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001169{
1170 unsigned long flags;
1171
Michael Hennerichacbcd262008-01-22 18:36:20 +08001172 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1173 gpio_error(gpio);
1174 return -EINVAL;
1175 }
1176
Yi Li6a01f232009-01-07 23:14:39 +08001177 local_irq_save_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001178 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1179 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001180 local_irq_restore_hw(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001181
1182 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001183}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001184EXPORT_SYMBOL(bfin_gpio_direction_input);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001185
Michael Hennericha4f0b322008-11-18 17:48:22 +08001186int bfin_gpio_direction_output(unsigned gpio, int value)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001187{
1188 unsigned long flags;
1189
Michael Hennerichacbcd262008-01-22 18:36:20 +08001190 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1191 gpio_error(gpio);
1192 return -EINVAL;
1193 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001194
Yi Li6a01f232009-01-07 23:14:39 +08001195 local_irq_save_hw(flags);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001196 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001197 gpio_set_value(gpio, value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001198 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001199 local_irq_restore_hw(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001200
1201 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001202}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001203EXPORT_SYMBOL(bfin_gpio_direction_output);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001204
Michael Hennericha4f0b322008-11-18 17:48:22 +08001205void bfin_gpio_set_value(unsigned gpio, int arg)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001206{
1207 if (arg)
1208 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1209 else
1210 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001211}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001212EXPORT_SYMBOL(bfin_gpio_set_value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001213
Michael Hennericha4f0b322008-11-18 17:48:22 +08001214int bfin_gpio_get_value(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001215{
1216 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1217}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001218EXPORT_SYMBOL(bfin_gpio_get_value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001219
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001220void bfin_gpio_irq_prepare(unsigned gpio)
1221{
1222 unsigned long flags;
1223
1224 port_setup(gpio, GPIO_USAGE);
1225
Yi Li6a01f232009-01-07 23:14:39 +08001226 local_irq_save_hw(flags);
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001227 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1228 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
Yi Li6a01f232009-01-07 23:14:39 +08001229 local_irq_restore_hw(flags);
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001230}
1231
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001232#else
1233
Michael Hennericha4f0b322008-11-18 17:48:22 +08001234int bfin_gpio_get_value(unsigned gpio)
Michael Hennerich803a8d22008-05-17 16:01:51 +08001235{
1236 unsigned long flags;
1237 int ret;
1238
1239 if (unlikely(get_gpio_edge(gpio))) {
Yi Li6a01f232009-01-07 23:14:39 +08001240 local_irq_save_hw(flags);
Michael Hennerich803a8d22008-05-17 16:01:51 +08001241 set_gpio_edge(gpio, 0);
1242 ret = get_gpio_data(gpio);
1243 set_gpio_edge(gpio, 1);
Yi Li6a01f232009-01-07 23:14:39 +08001244 local_irq_restore_hw(flags);
Michael Hennerich803a8d22008-05-17 16:01:51 +08001245
1246 return ret;
1247 } else
1248 return get_gpio_data(gpio);
1249}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001250EXPORT_SYMBOL(bfin_gpio_get_value);
Michael Hennerich803a8d22008-05-17 16:01:51 +08001251
1252
Michael Hennericha4f0b322008-11-18 17:48:22 +08001253int bfin_gpio_direction_input(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001254{
1255 unsigned long flags;
1256
Michael Hennerichacbcd262008-01-22 18:36:20 +08001257 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1258 gpio_error(gpio);
1259 return -EINVAL;
1260 }
Bryan Wu1394f032007-05-06 14:50:22 -07001261
Yi Li6a01f232009-01-07 23:14:39 +08001262 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001263 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1264 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001265 AWA_DUMMY_READ(inen);
Yi Li6a01f232009-01-07 23:14:39 +08001266 local_irq_restore_hw(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001267
1268 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001269}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001270EXPORT_SYMBOL(bfin_gpio_direction_input);
Bryan Wu1394f032007-05-06 14:50:22 -07001271
Michael Hennericha4f0b322008-11-18 17:48:22 +08001272int bfin_gpio_direction_output(unsigned gpio, int value)
Bryan Wu1394f032007-05-06 14:50:22 -07001273{
1274 unsigned long flags;
1275
Michael Hennerichacbcd262008-01-22 18:36:20 +08001276 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1277 gpio_error(gpio);
1278 return -EINVAL;
1279 }
Bryan Wu1394f032007-05-06 14:50:22 -07001280
Yi Li6a01f232009-01-07 23:14:39 +08001281 local_irq_save_hw(flags);
Bryan Wu1394f032007-05-06 14:50:22 -07001282 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001283
1284 if (value)
1285 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1286 else
1287 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1288
Bryan Wu1394f032007-05-06 14:50:22 -07001289 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001290 AWA_DUMMY_READ(dir);
Yi Li6a01f232009-01-07 23:14:39 +08001291 local_irq_restore_hw(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001292
1293 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001294}
Michael Hennericha4f0b322008-11-18 17:48:22 +08001295EXPORT_SYMBOL(bfin_gpio_direction_output);
Mike Frysinger168f1212007-10-11 00:22:35 +08001296
1297/* If we are booting from SPI and our board lacks a strong enough pull up,
1298 * the core can reset and execute the bootrom faster than the resistor can
1299 * pull the signal logically high. To work around this (common) error in
1300 * board design, we explicitly set the pin back to GPIO mode, force /CS
1301 * high, and wait for the electrons to do their thing.
1302 *
1303 * This function only makes sense to be called from reset code, but it
1304 * lives here as we need to force all the GPIO states w/out going through
1305 * BUG() checks and such.
1306 */
1307void bfin_gpio_reset_spi0_ssel1(void)
1308{
Michael Hennerich4d5f4ed2007-08-27 16:46:17 +08001309 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1310
1311 port_setup(gpio, GPIO_USAGE);
1312 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001313 AWA_DUMMY_READ(data_set);
Mike Frysinger168f1212007-10-11 00:22:35 +08001314 udelay(1);
1315}
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001316
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001317void bfin_gpio_irq_prepare(unsigned gpio)
1318{
1319 port_setup(gpio, GPIO_USAGE);
1320}
1321
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001322#endif /*BF548_FAMILY */
Mike Frysinger1545a112007-12-24 16:54:48 +08001323
1324#if defined(CONFIG_PROC_FS)
1325static int gpio_proc_read(char *buf, char **start, off_t offset,
1326 int len, int *unused_i, void *unused_v)
1327{
Graf Yang9570ff42009-01-07 23:14:38 +08001328 int c, irq, gpio, outlen = 0;
Mike Frysinger1545a112007-12-24 16:54:48 +08001329
1330 for (c = 0; c < MAX_RESOURCES; c++) {
Graf Yang9570ff42009-01-07 23:14:38 +08001331 irq = reserved_gpio_irq_map[gpio_bank(c)] & gpio_bit(c);
1332 gpio = reserved_gpio_map[gpio_bank(c)] & gpio_bit(c);
1333 if (!check_gpio(c) && (gpio || irq))
1334 len = sprintf(buf, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1335 get_label(c), (gpio && irq) ? " *" : "",
1336 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
Mike Frysinger1545a112007-12-24 16:54:48 +08001337 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
Graf Yang8eb3e3b2008-11-18 17:48:22 +08001338 len = sprintf(buf, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
Mike Frysinger1545a112007-12-24 16:54:48 +08001339 else
1340 continue;
1341 buf += len;
1342 outlen += len;
1343 }
1344 return outlen;
1345}
1346
1347static __init int gpio_register_proc(void)
1348{
1349 struct proc_dir_entry *proc_gpio;
1350
1351 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1352 if (proc_gpio)
1353 proc_gpio->read_proc = gpio_proc_read;
1354 return proc_gpio != NULL;
1355}
Mike Frysinger1545a112007-12-24 16:54:48 +08001356__initcall(gpio_register_proc);
1357#endif
Michael Hennericha4f0b322008-11-18 17:48:22 +08001358
1359#ifdef CONFIG_GPIOLIB
1360int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1361{
1362 return bfin_gpio_direction_input(gpio);
1363}
1364
1365int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1366{
1367 return bfin_gpio_direction_output(gpio, level);
1368}
1369
1370int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1371{
1372 return bfin_gpio_get_value(gpio);
1373}
1374
1375void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1376{
1377#ifdef BF548_FAMILY
1378 return bfin_gpio_set_value(gpio, value);
1379#else
1380 return set_gpio_data(gpio, value);
1381#endif
1382}
1383
1384int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1385{
1386 return bfin_gpio_request(gpio, chip->label);
1387}
1388
1389void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1390{
1391 return bfin_gpio_free(gpio);
1392}
1393
1394static struct gpio_chip bfin_chip = {
1395 .label = "Blackfin-GPIOlib",
1396 .direction_input = bfin_gpiolib_direction_input,
1397 .get = bfin_gpiolib_get_value,
1398 .direction_output = bfin_gpiolib_direction_output,
1399 .set = bfin_gpiolib_set_value,
1400 .request = bfin_gpiolib_gpio_request,
1401 .free = bfin_gpiolib_gpio_free,
1402 .base = 0,
1403 .ngpio = MAX_BLACKFIN_GPIOS,
1404};
1405
1406static int __init bfin_gpiolib_setup(void)
1407{
1408 return gpiochip_add(&bfin_chip);
1409}
1410arch_initcall(bfin_gpiolib_setup);
1411#endif