blob: 12710ed180bede49142ea6e5c2e09d11052db729 [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
Bryan Wu1394f032007-05-06 14:50:22 -0700122#ifdef BF533_FAMILY
123static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
124 (struct gpio_port_t *) FIO_FLAG_D,
125};
126#endif
127
Michael Hennerich59003142007-10-21 16:54:27 +0800128#if defined(BF527_FAMILY) || defined(BF537_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700129static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
130 (struct gpio_port_t *) PORTFIO,
131 (struct gpio_port_t *) PORTGIO,
132 (struct gpio_port_t *) PORTHIO,
133};
134
135static unsigned short *port_fer[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
136 (unsigned short *) PORTF_FER,
137 (unsigned short *) PORTG_FER,
138 (unsigned short *) PORTH_FER,
139};
Bryan Wu1394f032007-05-06 14:50:22 -0700140#endif
141
Michael Hennerich59003142007-10-21 16:54:27 +0800142#ifdef BF527_FAMILY
143static unsigned short *port_mux[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
144 (unsigned short *) PORTF_MUX,
145 (unsigned short *) PORTG_MUX,
146 (unsigned short *) PORTH_MUX,
147};
148
149static const
150u8 pmux_offset[][16] =
151 {{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
152 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
153 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
154 };
155#endif
156
Bryan Wu1394f032007-05-06 14:50:22 -0700157#ifdef BF561_FAMILY
158static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
159 (struct gpio_port_t *) FIO0_FLAG_D,
160 (struct gpio_port_t *) FIO1_FLAG_D,
161 (struct gpio_port_t *) FIO2_FLAG_D,
162};
163#endif
164
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800165#ifdef BF548_FAMILY
166static struct gpio_port_t *gpio_array[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
167 (struct gpio_port_t *)PORTA_FER,
168 (struct gpio_port_t *)PORTB_FER,
169 (struct gpio_port_t *)PORTC_FER,
170 (struct gpio_port_t *)PORTD_FER,
171 (struct gpio_port_t *)PORTE_FER,
172 (struct gpio_port_t *)PORTF_FER,
173 (struct gpio_port_t *)PORTG_FER,
174 (struct gpio_port_t *)PORTH_FER,
175 (struct gpio_port_t *)PORTI_FER,
176 (struct gpio_port_t *)PORTJ_FER,
177};
178#endif
179
Michael Hennerichc58c2142007-10-04 00:35:05 +0800180static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800181static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
Michael Hennerichc58c2142007-10-04 00:35:05 +0800182
Michael Hennerich8c613622007-08-03 17:48:09 +0800183#define RESOURCE_LABEL_SIZE 16
184
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800185static struct str_ident {
Michael Hennerich8c613622007-08-03 17:48:09 +0800186 char name[RESOURCE_LABEL_SIZE];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800187} str_ident[MAX_RESOURCES];
Bryan Wu1394f032007-05-06 14:50:22 -0700188
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800189#if defined(CONFIG_PM)
190#if defined(CONFIG_BF54x)
191static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
192#else
Bryan Wu1394f032007-05-06 14:50:22 -0700193static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
194static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
195static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
196
197#ifdef BF533_FAMILY
198static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
199#endif
200
201#ifdef BF537_FAMILY
202static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
203#endif
204
Michael Hennerich59003142007-10-21 16:54:27 +0800205#ifdef BF527_FAMILY
206static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
207#endif
208
Bryan Wu1394f032007-05-06 14:50:22 -0700209#ifdef BF561_FAMILY
210static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
211#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800212#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700213#endif /* CONFIG_PM */
214
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800215#if defined(BF548_FAMILY)
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800216inline int check_gpio(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800217{
218 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
219 || gpio == GPIO_PH14 || gpio == GPIO_PH15
220 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15
221 || gpio > MAX_BLACKFIN_GPIOS)
222 return -EINVAL;
223 return 0;
224}
225#else
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800226inline int check_gpio(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700227{
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}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800232#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700233
Mike Frysinger74c04502008-10-08 16:13:17 +0800234static void gpio_error(unsigned gpio)
Michael Hennerichacbcd262008-01-22 18:36:20 +0800235{
236 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
237}
238
Michael Hennerichc58c2142007-10-04 00:35:05 +0800239static void set_label(unsigned short ident, const char *label)
240{
Michael Hennerichc58c2142007-10-04 00:35:05 +0800241 if (label && str_ident) {
Michael Hennerich8c613622007-08-03 17:48:09 +0800242 strncpy(str_ident[ident].name, label,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800243 RESOURCE_LABEL_SIZE);
Michael Hennerich8c613622007-08-03 17:48:09 +0800244 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800245 }
246}
247
248static char *get_label(unsigned short ident)
249{
250 if (!str_ident)
251 return "UNKNOWN";
252
Michael Hennerich8c613622007-08-03 17:48:09 +0800253 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
Michael Hennerichc58c2142007-10-04 00:35:05 +0800254}
255
256static int cmp_label(unsigned short ident, const char *label)
257{
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800258 if (label == NULL) {
259 dump_stack();
260 printk(KERN_ERR "Please provide none-null label\n");
261 }
262
Michael Hennerichc58c2142007-10-04 00:35:05 +0800263 if (label && str_ident)
Michael Hennerich8c613622007-08-03 17:48:09 +0800264 return strncmp(str_ident[ident].name,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800265 label, strlen(label));
266 else
267 return -EINVAL;
268}
269
Michael Hennerich59003142007-10-21 16:54:27 +0800270#if defined(BF527_FAMILY) || defined(BF537_FAMILY)
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800271static void port_setup(unsigned gpio, unsigned short usage)
Bryan Wu1394f032007-05-06 14:50:22 -0700272{
Michael Hennerichcda6a202007-10-04 00:36:18 +0800273 if (!check_gpio(gpio)) {
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800274 if (usage == GPIO_USAGE)
Michael Hennerichcda6a202007-10-04 00:36:18 +0800275 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800276 else
Michael Hennerichcda6a202007-10-04 00:36:18 +0800277 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
278 SSYNC();
279 }
Bryan Wu1394f032007-05-06 14:50:22 -0700280}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800281#elif defined(BF548_FAMILY)
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800282static void port_setup(unsigned gpio, unsigned short usage)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800283{
284 if (usage == GPIO_USAGE)
285 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
286 else
287 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
288 SSYNC();
289}
Bryan Wu1394f032007-05-06 14:50:22 -0700290#else
291# define port_setup(...) do { } while (0)
292#endif
293
Michael Hennerichc58c2142007-10-04 00:35:05 +0800294#ifdef BF537_FAMILY
Michael Hennerich8c613622007-08-03 17:48:09 +0800295static struct {
296 unsigned short res;
297 unsigned short offset;
298} port_mux_lut[] = {
299 {.res = P_PPI0_D13, .offset = 11},
300 {.res = P_PPI0_D14, .offset = 11},
301 {.res = P_PPI0_D15, .offset = 11},
302 {.res = P_SPORT1_TFS, .offset = 11},
303 {.res = P_SPORT1_TSCLK, .offset = 11},
304 {.res = P_SPORT1_DTPRI, .offset = 11},
305 {.res = P_PPI0_D10, .offset = 10},
306 {.res = P_PPI0_D11, .offset = 10},
307 {.res = P_PPI0_D12, .offset = 10},
308 {.res = P_SPORT1_RSCLK, .offset = 10},
309 {.res = P_SPORT1_RFS, .offset = 10},
310 {.res = P_SPORT1_DRPRI, .offset = 10},
311 {.res = P_PPI0_D8, .offset = 9},
312 {.res = P_PPI0_D9, .offset = 9},
313 {.res = P_SPORT1_DRSEC, .offset = 9},
314 {.res = P_SPORT1_DTSEC, .offset = 9},
315 {.res = P_TMR2, .offset = 8},
316 {.res = P_PPI0_FS3, .offset = 8},
317 {.res = P_TMR3, .offset = 7},
318 {.res = P_SPI0_SSEL4, .offset = 7},
319 {.res = P_TMR4, .offset = 6},
320 {.res = P_SPI0_SSEL5, .offset = 6},
321 {.res = P_TMR5, .offset = 5},
322 {.res = P_SPI0_SSEL6, .offset = 5},
323 {.res = P_UART1_RX, .offset = 4},
324 {.res = P_UART1_TX, .offset = 4},
325 {.res = P_TMR6, .offset = 4},
326 {.res = P_TMR7, .offset = 4},
327 {.res = P_UART0_RX, .offset = 3},
328 {.res = P_UART0_TX, .offset = 3},
329 {.res = P_DMAR0, .offset = 3},
330 {.res = P_DMAR1, .offset = 3},
331 {.res = P_SPORT0_DTSEC, .offset = 1},
332 {.res = P_SPORT0_DRSEC, .offset = 1},
333 {.res = P_CAN0_RX, .offset = 1},
334 {.res = P_CAN0_TX, .offset = 1},
335 {.res = P_SPI0_SSEL7, .offset = 1},
336 {.res = P_SPORT0_TFS, .offset = 0},
337 {.res = P_SPORT0_DTPRI, .offset = 0},
338 {.res = P_SPI0_SSEL2, .offset = 0},
339 {.res = P_SPI0_SSEL3, .offset = 0},
Michael Hennerichc58c2142007-10-04 00:35:05 +0800340};
341
342static void portmux_setup(unsigned short per, unsigned short function)
343{
Michael Hennerich8c613622007-08-03 17:48:09 +0800344 u16 y, offset, muxreg;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800345
Michael Hennerich8c613622007-08-03 17:48:09 +0800346 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
347 if (port_mux_lut[y].res == per) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800348
349 /* SET PORTMUX REG */
350
Michael Hennerich8c613622007-08-03 17:48:09 +0800351 offset = port_mux_lut[y].offset;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800352 muxreg = bfin_read_PORT_MUX();
353
Mike Frysingerd171c232008-03-26 08:35:46 +0800354 if (offset != 1)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800355 muxreg &= ~(1 << offset);
Mike Frysingerd171c232008-03-26 08:35:46 +0800356 else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800357 muxreg &= ~(3 << 1);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800358
359 muxreg |= (function << offset);
360 bfin_write_PORT_MUX(muxreg);
361 }
362 }
363}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800364#elif defined(BF548_FAMILY)
365inline void portmux_setup(unsigned short portno, unsigned short function)
366{
367 u32 pmux;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800368
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800369 pmux = gpio_array[gpio_bank(portno)]->port_mux;
370
371 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
372 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
373
374 gpio_array[gpio_bank(portno)]->port_mux = pmux;
375}
376
377inline u16 get_portmux(unsigned short portno)
378{
379 u32 pmux;
380
381 pmux = gpio_array[gpio_bank(portno)]->port_mux;
382
383 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
384}
Michael Hennerich59003142007-10-21 16:54:27 +0800385#elif defined(BF527_FAMILY)
386inline void portmux_setup(unsigned short portno, unsigned short function)
387{
388 u16 pmux, ident = P_IDENT(portno);
389 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
390
391 pmux = *port_mux[gpio_bank(ident)];
392 pmux &= ~(3 << offset);
393 pmux |= (function & 3) << offset;
394 *port_mux[gpio_bank(ident)] = pmux;
395 SSYNC();
396}
Michael Hennerichc58c2142007-10-04 00:35:05 +0800397#else
398# define portmux_setup(...) do { } while (0)
399#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700400
Mike Frysingera161bb02007-05-21 18:09:14 +0800401static int __init bfin_gpio_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700402{
Bryan Wu1394f032007-05-06 14:50:22 -0700403 printk(KERN_INFO "Blackfin GPIO Controller\n");
404
Bryan Wu1394f032007-05-06 14:50:22 -0700405 return 0;
406}
Bryan Wu1394f032007-05-06 14:50:22 -0700407arch_initcall(bfin_gpio_init);
408
409
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800410#ifndef BF548_FAMILY
Bryan Wu1394f032007-05-06 14:50:22 -0700411/***********************************************************
412*
413* FUNCTIONS: Blackfin General Purpose Ports Access Functions
414*
415* INPUTS/OUTPUTS:
416* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
417*
418*
419* DESCRIPTION: These functions abstract direct register access
420* to Blackfin processor General Purpose
421* Ports Regsiters
422*
423* CAUTION: These functions do not belong to the GPIO Driver API
424*************************************************************
425* MODIFICATION HISTORY :
426**************************************************************/
427
428/* Set a specific bit */
429
430#define SET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800431void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700432{ \
433 unsigned long flags; \
Bryan Wu1394f032007-05-06 14:50:22 -0700434 local_irq_save(flags); \
435 if (arg) \
436 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
437 else \
438 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800439 AWA_DUMMY_READ(name); \
Bryan Wu1394f032007-05-06 14:50:22 -0700440 local_irq_restore(flags); \
441} \
442EXPORT_SYMBOL(set_gpio_ ## name);
443
444SET_GPIO(dir)
445SET_GPIO(inen)
446SET_GPIO(polar)
447SET_GPIO(edge)
448SET_GPIO(both)
449
450
Michael Hennerich2b393312007-10-10 16:58:49 +0800451#if ANOMALY_05000311 || ANOMALY_05000323
452#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800453void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800454{ \
455 unsigned long flags; \
Michael Hennerich2b393312007-10-10 16:58:49 +0800456 local_irq_save(flags); \
457 if (arg) \
458 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
459 else \
460 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
461 AWA_DUMMY_READ(name); \
462 local_irq_restore(flags); \
463} \
464EXPORT_SYMBOL(set_gpio_ ## name);
465#else
Bryan Wu1394f032007-05-06 14:50:22 -0700466#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800467void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700468{ \
Bryan Wu1394f032007-05-06 14:50:22 -0700469 if (arg) \
470 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
471 else \
472 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
473} \
474EXPORT_SYMBOL(set_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800475#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700476
477SET_GPIO_SC(maska)
478SET_GPIO_SC(maskb)
Bryan Wu1394f032007-05-06 14:50:22 -0700479SET_GPIO_SC(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700480
Michael Hennerich2b393312007-10-10 16:58:49 +0800481#if ANOMALY_05000311 || ANOMALY_05000323
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800482void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700483{
484 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700485 local_irq_save(flags);
486 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +0800487 AWA_DUMMY_READ(toggle);
Bryan Wu1394f032007-05-06 14:50:22 -0700488 local_irq_restore(flags);
489}
490#else
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800491void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700492{
Bryan Wu1394f032007-05-06 14:50:22 -0700493 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
494}
495#endif
496EXPORT_SYMBOL(set_gpio_toggle);
497
498
499/*Set current PORT date (16-bit word)*/
500
Michael Hennerich2b393312007-10-10 16:58:49 +0800501#if ANOMALY_05000311 || ANOMALY_05000323
502#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800503void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800504{ \
505 unsigned long flags; \
506 local_irq_save(flags); \
507 gpio_bankb[gpio_bank(gpio)]->name = arg; \
508 AWA_DUMMY_READ(name); \
509 local_irq_restore(flags); \
510} \
511EXPORT_SYMBOL(set_gpiop_ ## name);
512#else
Bryan Wu1394f032007-05-06 14:50:22 -0700513#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800514void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700515{ \
516 gpio_bankb[gpio_bank(gpio)]->name = arg; \
517} \
518EXPORT_SYMBOL(set_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800519#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700520
Michael Hennerich2b393312007-10-10 16:58:49 +0800521SET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700522SET_GPIO_P(dir)
523SET_GPIO_P(inen)
524SET_GPIO_P(polar)
525SET_GPIO_P(edge)
526SET_GPIO_P(both)
527SET_GPIO_P(maska)
528SET_GPIO_P(maskb)
529
Bryan Wu1394f032007-05-06 14:50:22 -0700530/* Get a specific bit */
Michael Hennerich2b393312007-10-10 16:58:49 +0800531#if ANOMALY_05000311 || ANOMALY_05000323
532#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800533unsigned short get_gpio_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800534{ \
535 unsigned long flags; \
536 unsigned short ret; \
537 local_irq_save(flags); \
538 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
539 AWA_DUMMY_READ(name); \
540 local_irq_restore(flags); \
541 return ret; \
542} \
543EXPORT_SYMBOL(get_gpio_ ## name);
544#else
Bryan Wu1394f032007-05-06 14:50:22 -0700545#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800546unsigned short get_gpio_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700547{ \
548 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
549} \
550EXPORT_SYMBOL(get_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800551#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700552
Michael Hennerich2b393312007-10-10 16:58:49 +0800553GET_GPIO(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700554GET_GPIO(dir)
555GET_GPIO(inen)
556GET_GPIO(polar)
557GET_GPIO(edge)
558GET_GPIO(both)
559GET_GPIO(maska)
560GET_GPIO(maskb)
561
Bryan Wu1394f032007-05-06 14:50:22 -0700562/*Get current PORT date (16-bit word)*/
563
Michael Hennerich2b393312007-10-10 16:58:49 +0800564#if ANOMALY_05000311 || ANOMALY_05000323
565#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800566unsigned short get_gpiop_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800567{ \
568 unsigned long flags; \
569 unsigned short ret; \
570 local_irq_save(flags); \
571 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
572 AWA_DUMMY_READ(name); \
573 local_irq_restore(flags); \
574 return ret; \
575} \
576EXPORT_SYMBOL(get_gpiop_ ## name);
577#else
Bryan Wu1394f032007-05-06 14:50:22 -0700578#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800579unsigned short get_gpiop_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700580{ \
581 return (gpio_bankb[gpio_bank(gpio)]->name);\
582} \
583EXPORT_SYMBOL(get_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800584#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700585
Michael Hennerich2b393312007-10-10 16:58:49 +0800586GET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700587GET_GPIO_P(dir)
588GET_GPIO_P(inen)
589GET_GPIO_P(polar)
590GET_GPIO_P(edge)
591GET_GPIO_P(both)
592GET_GPIO_P(maska)
593GET_GPIO_P(maskb)
594
Bryan Wu1394f032007-05-06 14:50:22 -0700595
596#ifdef CONFIG_PM
597/***********************************************************
598*
599* FUNCTIONS: Blackfin PM Setup API
600*
601* INPUTS/OUTPUTS:
602* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
603* type -
604* PM_WAKE_RISING
605* PM_WAKE_FALLING
606* PM_WAKE_HIGH
607* PM_WAKE_LOW
608* PM_WAKE_BOTH_EDGES
609*
610* DESCRIPTION: Blackfin PM Driver API
611*
612* CAUTION:
613*************************************************************
614* MODIFICATION HISTORY :
615**************************************************************/
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800616int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700617{
618 unsigned long flags;
619
620 if ((check_gpio(gpio) < 0) || !type)
621 return -EINVAL;
622
623 local_irq_save(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700624 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
625 wakeup_flags_map[gpio] = type;
626 local_irq_restore(flags);
627
628 return 0;
629}
630EXPORT_SYMBOL(gpio_pm_wakeup_request);
631
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800632void gpio_pm_wakeup_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700633{
634 unsigned long flags;
635
636 if (check_gpio(gpio) < 0)
637 return;
638
639 local_irq_save(flags);
640
641 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
642
643 local_irq_restore(flags);
644}
645EXPORT_SYMBOL(gpio_pm_wakeup_free);
646
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800647static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700648{
649 port_setup(gpio, GPIO_USAGE);
650 set_gpio_dir(gpio, 0);
651 set_gpio_inen(gpio, 1);
652
653 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
654 set_gpio_edge(gpio, 1);
655 else
656 set_gpio_edge(gpio, 0);
657
658 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
659 set_gpio_both(gpio, 1);
660 else
661 set_gpio_both(gpio, 0);
662
663 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
664 set_gpio_polar(gpio, 1);
665 else
666 set_gpio_polar(gpio, 0);
667
668 SSYNC();
669
670 return 0;
671}
672
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800673u32 bfin_pm_standby_setup(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700674{
Bryan Wu1394f032007-05-06 14:50:22 -0700675 u16 bank, mask, i, gpio;
676
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800677 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700678 mask = wakeup_map[gpio_bank(i)];
679 bank = gpio_bank(i);
680
681 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
682 gpio_bankb[bank]->maskb = 0;
683
684 if (mask) {
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800685#if defined(BF527_FAMILY) || defined(BF537_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700686 gpio_bank_saved[bank].fer = *port_fer[bank];
687#endif
688 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
689 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
690 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
691 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
692 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800693 gpio_bank_saved[bank].reserved =
694 reserved_gpio_map[bank];
Bryan Wu1394f032007-05-06 14:50:22 -0700695
696 gpio = i;
697
698 while (mask) {
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800699 if ((mask & 1) && (wakeup_flags_map[gpio] !=
700 PM_WAKE_IGNORE)) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800701 reserved_gpio_map[gpio_bank(gpio)] |=
Michael Hennerich581d62a2007-06-14 13:30:23 +0800702 gpio_bit(gpio);
703 bfin_gpio_wakeup_type(gpio,
704 wakeup_flags_map[gpio]);
Bryan Wu1394f032007-05-06 14:50:22 -0700705 set_gpio_data(gpio, 0); /*Clear*/
706 }
707 gpio++;
708 mask >>= 1;
709 }
710
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800711 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
Bryan Wu1394f032007-05-06 14:50:22 -0700712 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
713 }
714 }
715
Michael Hennerich2b393312007-10-10 16:58:49 +0800716 AWA_DUMMY_READ(maskb_set);
717
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800718 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700719}
720
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800721void bfin_pm_standby_restore(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700722{
723 u16 bank, mask, i;
724
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800725 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700726 mask = wakeup_map[gpio_bank(i)];
727 bank = gpio_bank(i);
728
729 if (mask) {
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800730#if defined(BF527_FAMILY) || defined(BF537_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700731 *port_fer[bank] = gpio_bank_saved[bank].fer;
732#endif
733 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
734 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
735 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
736 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
737 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800738
Michael Hennerichc58c2142007-10-04 00:35:05 +0800739 reserved_gpio_map[bank] =
740 gpio_bank_saved[bank].reserved;
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800741 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
Bryan Wu1394f032007-05-06 14:50:22 -0700742 }
743
744 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
745 }
Michael Hennerich2b393312007-10-10 16:58:49 +0800746 AWA_DUMMY_READ(maskb);
Bryan Wu1394f032007-05-06 14:50:22 -0700747}
748
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800749void bfin_gpio_pm_hibernate_suspend(void)
750{
751 int i, bank;
752
753 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
754 bank = gpio_bank(i);
755
756#if defined(BF527_FAMILY) || defined(BF537_FAMILY)
757 gpio_bank_saved[bank].fer = *port_fer[bank];
758#ifdef BF527_FAMILY
759 gpio_bank_saved[bank].mux = *port_mux[bank];
760#else
761 if (bank == 0)
762 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
763#endif
764#endif
765 gpio_bank_saved[bank].data = gpio_bankb[bank]->data;
766 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
767 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
768 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
769 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
770 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
771 gpio_bank_saved[bank].maska = gpio_bankb[bank]->maska;
772 }
773
774 AWA_DUMMY_READ(maska);
775}
776
777void bfin_gpio_pm_hibernate_restore(void)
778{
779 int i, bank;
780
781 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
782 bank = gpio_bank(i);
783
784#if defined(BF527_FAMILY) || defined(BF537_FAMILY)
785#ifdef BF527_FAMILY
786 *port_mux[bank] = gpio_bank_saved[bank].mux;
787#else
788 if (bank == 0)
789 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
790#endif
791 *port_fer[bank] = gpio_bank_saved[bank].fer;
792#endif
793 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
794 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
795 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
796 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
797 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
798
799 gpio_bankb[bank]->data_set = gpio_bank_saved[bank].data
800 | gpio_bank_saved[bank].dir;
801
802 gpio_bankb[bank]->maska = gpio_bank_saved[bank].maska;
803 }
804 AWA_DUMMY_READ(maska);
805}
806
807
Bryan Wu1394f032007-05-06 14:50:22 -0700808#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800809#else /* BF548_FAMILY */
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800810#ifdef CONFIG_PM
811
812u32 bfin_pm_standby_setup(void)
813{
814 return 0;
815}
816
817void bfin_pm_standby_restore(void)
818{
819
820}
821
822void bfin_gpio_pm_hibernate_suspend(void)
823{
824 int i, bank;
825
826 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
827 bank = gpio_bank(i);
828
829 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
830 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
831 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
832 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
833 gpio_bank_saved[bank].inen = gpio_array[bank]->port_inen;
834 gpio_bank_saved[bank].dir = gpio_array[bank]->port_dir_set;
835 }
836}
837
838void bfin_gpio_pm_hibernate_restore(void)
839{
840 int i, bank;
841
842 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
843 bank = gpio_bank(i);
844
845 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
846 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
847 gpio_array[bank]->port_inen = gpio_bank_saved[bank].inen;
848 gpio_array[bank]->port_dir_set = gpio_bank_saved[bank].dir;
849 gpio_array[bank]->port_set = gpio_bank_saved[bank].data
850 | gpio_bank_saved[bank].dir;
851 }
852}
853#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800854
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800855unsigned short get_gpio_dir(unsigned gpio)
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800856{
857 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
858}
859EXPORT_SYMBOL(get_gpio_dir);
860
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800861#endif /* BF548_FAMILY */
Bryan Wu1394f032007-05-06 14:50:22 -0700862
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800863/***********************************************************
864*
865* FUNCTIONS: Blackfin Peripheral Resource Allocation
866* and PortMux Setup
867*
868* INPUTS/OUTPUTS:
869* per Peripheral Identifier
870* label String
871*
872* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
873*
874* CAUTION:
875*************************************************************
876* MODIFICATION HISTORY :
877**************************************************************/
Michael Hennerichc58c2142007-10-04 00:35:05 +0800878
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800879#ifdef BF548_FAMILY
880int peripheral_request(unsigned short per, const char *label)
881{
882 unsigned long flags;
883 unsigned short ident = P_IDENT(per);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800884
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800885 /*
886 * Don't cares are pins with only one dedicated function
887 */
888
889 if (per & P_DONTCARE)
890 return 0;
891
892 if (!(per & P_DEFINED))
893 return -ENODEV;
894
895 if (check_gpio(ident) < 0)
896 return -EINVAL;
897
898 local_irq_save(flags);
899
900 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800901 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800902 printk(KERN_ERR
903 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800904 __func__, ident, get_label(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800905 local_irq_restore(flags);
906 return -EBUSY;
907 }
908
909 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
910
911 u16 funct = get_portmux(ident);
912
Mike Frysingerd171c232008-03-26 08:35:46 +0800913 /*
914 * Pin functions like AMC address strobes my
915 * be requested and used by several drivers
916 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800917
918 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
919
Mike Frysingerd171c232008-03-26 08:35:46 +0800920 /*
921 * Allow that the identical pin function can
922 * be requested from the same driver twice
923 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800924
Mike Frysingerd171c232008-03-26 08:35:46 +0800925 if (cmp_label(ident, label) == 0)
926 goto anyway;
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800927
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800928 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800929 printk(KERN_ERR
930 "%s: Peripheral %d function %d is already reserved by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800931 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800932 local_irq_restore(flags);
933 return -EBUSY;
934 }
935 }
936
Mike Frysingerd171c232008-03-26 08:35:46 +0800937 anyway:
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800938 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
939
940 portmux_setup(ident, P_FUNCT2MUX(per));
941 port_setup(ident, PERIPHERAL_USAGE);
942
943 local_irq_restore(flags);
944 set_label(ident, label);
945
946 return 0;
947}
948EXPORT_SYMBOL(peripheral_request);
949#else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800950
951int peripheral_request(unsigned short per, const char *label)
952{
953 unsigned long flags;
954 unsigned short ident = P_IDENT(per);
955
956 /*
957 * Don't cares are pins with only one dedicated function
958 */
959
960 if (per & P_DONTCARE)
961 return 0;
962
963 if (!(per & P_DEFINED))
964 return -ENODEV;
965
Michael Hennerichc58c2142007-10-04 00:35:05 +0800966 local_irq_save(flags);
967
Michael Hennerichcda6a202007-10-04 00:36:18 +0800968 if (!check_gpio(ident)) {
969
Mike Frysingerd171c232008-03-26 08:35:46 +0800970 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
971 dump_stack();
972 printk(KERN_ERR
973 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800974 __func__, ident, get_label(ident));
Mike Frysingerd171c232008-03-26 08:35:46 +0800975 local_irq_restore(flags);
976 return -EBUSY;
977 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800978
Michael Hennerichcda6a202007-10-04 00:36:18 +0800979 }
980
Michael Hennerichc58c2142007-10-04 00:35:05 +0800981 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
982
Mike Frysingerd171c232008-03-26 08:35:46 +0800983 /*
984 * Pin functions like AMC address strobes my
985 * be requested and used by several drivers
986 */
Michael Hennerichc58c2142007-10-04 00:35:05 +0800987
Mike Frysingerd171c232008-03-26 08:35:46 +0800988 if (!(per & P_MAYSHARE)) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800989
Mike Frysingerd171c232008-03-26 08:35:46 +0800990 /*
991 * Allow that the identical pin function can
992 * be requested from the same driver twice
993 */
Michael Hennerichc58c2142007-10-04 00:35:05 +0800994
Mike Frysingerd171c232008-03-26 08:35:46 +0800995 if (cmp_label(ident, label) == 0)
996 goto anyway;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800997
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800998 dump_stack();
Michael Hennerichc58c2142007-10-04 00:35:05 +0800999 printk(KERN_ERR
1000 "%s: Peripheral %d function %d is already"
Michael Hennerich8c613622007-08-03 17:48:09 +08001001 " reserved by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +08001002 __func__, ident, P_FUNCT2MUX(per),
Michael Hennerichc58c2142007-10-04 00:35:05 +08001003 get_label(ident));
Michael Hennerichc58c2142007-10-04 00:35:05 +08001004 local_irq_restore(flags);
1005 return -EBUSY;
1006 }
1007
1008 }
1009
Mike Frysingerd171c232008-03-26 08:35:46 +08001010 anyway:
Michael Hennerichc58c2142007-10-04 00:35:05 +08001011 portmux_setup(per, P_FUNCT2MUX(per));
1012
1013 port_setup(ident, PERIPHERAL_USAGE);
1014
1015 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
1016 local_irq_restore(flags);
1017 set_label(ident, label);
1018
1019 return 0;
1020}
1021EXPORT_SYMBOL(peripheral_request);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001022#endif
Michael Hennerichc58c2142007-10-04 00:35:05 +08001023
Mike Frysinger68179372008-04-24 05:04:24 +08001024int peripheral_request_list(const unsigned short per[], const char *label)
Michael Hennerichc58c2142007-10-04 00:35:05 +08001025{
1026 u16 cnt;
1027 int ret;
1028
1029 for (cnt = 0; per[cnt] != 0; cnt++) {
Michael Hennerich314c98d2007-07-24 18:03:45 +08001030
Michael Hennerichc58c2142007-10-04 00:35:05 +08001031 ret = peripheral_request(per[cnt], label);
Michael Hennerich314c98d2007-07-24 18:03:45 +08001032
1033 if (ret < 0) {
Mike Frysingerd171c232008-03-26 08:35:46 +08001034 for ( ; cnt > 0; cnt--)
Michael Hennerich314c98d2007-07-24 18:03:45 +08001035 peripheral_free(per[cnt - 1]);
Mike Frysingerd171c232008-03-26 08:35:46 +08001036
1037 return ret;
Michael Hennerich314c98d2007-07-24 18:03:45 +08001038 }
Michael Hennerichc58c2142007-10-04 00:35:05 +08001039 }
1040
1041 return 0;
1042}
1043EXPORT_SYMBOL(peripheral_request_list);
1044
1045void peripheral_free(unsigned short per)
1046{
1047 unsigned long flags;
1048 unsigned short ident = P_IDENT(per);
1049
1050 if (per & P_DONTCARE)
1051 return;
1052
1053 if (!(per & P_DEFINED))
1054 return;
1055
1056 if (check_gpio(ident) < 0)
1057 return;
1058
1059 local_irq_save(flags);
1060
Mike Frysingerd171c232008-03-26 08:35:46 +08001061 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
Michael Hennerichc58c2142007-10-04 00:35:05 +08001062 local_irq_restore(flags);
1063 return;
1064 }
1065
Mike Frysingerd171c232008-03-26 08:35:46 +08001066 if (!(per & P_MAYSHARE))
Michael Hennerichc58c2142007-10-04 00:35:05 +08001067 port_setup(ident, GPIO_USAGE);
Michael Hennerichc58c2142007-10-04 00:35:05 +08001068
1069 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
1070
Michael Hennerich2acde902007-10-11 00:24:40 +08001071 set_label(ident, "free");
1072
Michael Hennerichc58c2142007-10-04 00:35:05 +08001073 local_irq_restore(flags);
1074}
1075EXPORT_SYMBOL(peripheral_free);
1076
Mike Frysinger68179372008-04-24 05:04:24 +08001077void peripheral_free_list(const unsigned short per[])
Michael Hennerichc58c2142007-10-04 00:35:05 +08001078{
1079 u16 cnt;
Mike Frysingerd171c232008-03-26 08:35:46 +08001080 for (cnt = 0; per[cnt] != 0; cnt++)
Michael Hennerichc58c2142007-10-04 00:35:05 +08001081 peripheral_free(per[cnt]);
Michael Hennerichc58c2142007-10-04 00:35:05 +08001082}
1083EXPORT_SYMBOL(peripheral_free_list);
1084
Bryan Wu1394f032007-05-06 14:50:22 -07001085/***********************************************************
1086*
1087* FUNCTIONS: Blackfin GPIO Driver
1088*
1089* INPUTS/OUTPUTS:
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001090* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1091* label String
Bryan Wu1394f032007-05-06 14:50:22 -07001092*
1093* DESCRIPTION: Blackfin GPIO Driver API
1094*
1095* CAUTION:
1096*************************************************************
1097* MODIFICATION HISTORY :
1098**************************************************************/
1099
Michael Hennerichacbcd262008-01-22 18:36:20 +08001100int gpio_request(unsigned gpio, const char *label)
Bryan Wu1394f032007-05-06 14:50:22 -07001101{
1102 unsigned long flags;
1103
1104 if (check_gpio(gpio) < 0)
1105 return -EINVAL;
1106
1107 local_irq_save(flags);
1108
Michael Hennerich2acde902007-10-11 00:24:40 +08001109 /*
1110 * Allow that the identical GPIO can
1111 * be requested from the same driver twice
1112 * Do nothing and return -
1113 */
1114
1115 if (cmp_label(gpio, label) == 0) {
1116 local_irq_restore(flags);
1117 return 0;
1118 }
1119
Michael Hennerichc58c2142007-10-04 00:35:05 +08001120 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001121 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001122 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1123 gpio, get_label(gpio));
Bryan Wu1394f032007-05-06 14:50:22 -07001124 local_irq_restore(flags);
1125 return -EBUSY;
1126 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001127 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001128 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001129 printk(KERN_ERR
1130 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1131 gpio, get_label(gpio));
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001132 local_irq_restore(flags);
1133 return -EBUSY;
1134 }
1135
Michael Hennerichc58c2142007-10-04 00:35:05 +08001136 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001137
1138 local_irq_restore(flags);
1139
1140 port_setup(gpio, GPIO_USAGE);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001141 set_label(gpio, label);
Bryan Wu1394f032007-05-06 14:50:22 -07001142
1143 return 0;
1144}
1145EXPORT_SYMBOL(gpio_request);
1146
Michael Hennerichacbcd262008-01-22 18:36:20 +08001147void gpio_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001148{
1149 unsigned long flags;
1150
1151 if (check_gpio(gpio) < 0)
1152 return;
1153
1154 local_irq_save(flags);
1155
Michael Hennerichc58c2142007-10-04 00:35:05 +08001156 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
Bryan Wu1394f032007-05-06 14:50:22 -07001157 dump_stack();
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001158 gpio_error(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001159 local_irq_restore(flags);
1160 return;
1161 }
1162
Michael Hennerichc58c2142007-10-04 00:35:05 +08001163 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001164
Michael Hennerich2acde902007-10-11 00:24:40 +08001165 set_label(gpio, "free");
1166
Bryan Wu1394f032007-05-06 14:50:22 -07001167 local_irq_restore(flags);
1168}
1169EXPORT_SYMBOL(gpio_free);
1170
Michael Hennerichacbcd262008-01-22 18:36:20 +08001171
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001172#ifdef BF548_FAMILY
Michael Hennerichacbcd262008-01-22 18:36:20 +08001173int gpio_direction_input(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001174{
1175 unsigned long flags;
1176
Michael Hennerichacbcd262008-01-22 18:36:20 +08001177 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1178 gpio_error(gpio);
1179 return -EINVAL;
1180 }
1181
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001182 local_irq_save(flags);
1183 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1184 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1185 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001186
1187 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001188}
1189EXPORT_SYMBOL(gpio_direction_input);
1190
Michael Hennerichacbcd262008-01-22 18:36:20 +08001191int gpio_direction_output(unsigned gpio, int value)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001192{
1193 unsigned long flags;
1194
Michael Hennerichacbcd262008-01-22 18:36:20 +08001195 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1196 gpio_error(gpio);
1197 return -EINVAL;
1198 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001199
1200 local_irq_save(flags);
1201 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001202 gpio_set_value(gpio, value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001203 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1204 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001205
1206 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001207}
1208EXPORT_SYMBOL(gpio_direction_output);
1209
Michael Hennerichacbcd262008-01-22 18:36:20 +08001210void gpio_set_value(unsigned gpio, int arg)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001211{
1212 if (arg)
1213 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1214 else
1215 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001216}
1217EXPORT_SYMBOL(gpio_set_value);
1218
Michael Hennerichacbcd262008-01-22 18:36:20 +08001219int gpio_get_value(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001220{
1221 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1222}
1223EXPORT_SYMBOL(gpio_get_value);
1224
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001225void bfin_gpio_irq_prepare(unsigned gpio)
1226{
1227 unsigned long flags;
1228
1229 port_setup(gpio, GPIO_USAGE);
1230
1231 local_irq_save(flags);
1232 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1233 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1234 local_irq_restore(flags);
1235}
1236
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001237#else
1238
Michael Hennerich803a8d22008-05-17 16:01:51 +08001239int gpio_get_value(unsigned gpio)
1240{
1241 unsigned long flags;
1242 int ret;
1243
1244 if (unlikely(get_gpio_edge(gpio))) {
1245 local_irq_save(flags);
1246 set_gpio_edge(gpio, 0);
1247 ret = get_gpio_data(gpio);
1248 set_gpio_edge(gpio, 1);
1249 local_irq_restore(flags);
1250
1251 return ret;
1252 } else
1253 return get_gpio_data(gpio);
1254}
1255EXPORT_SYMBOL(gpio_get_value);
1256
1257
Michael Hennerichacbcd262008-01-22 18:36:20 +08001258int gpio_direction_input(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001259{
1260 unsigned long flags;
1261
Michael Hennerichacbcd262008-01-22 18:36:20 +08001262 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1263 gpio_error(gpio);
1264 return -EINVAL;
1265 }
Bryan Wu1394f032007-05-06 14:50:22 -07001266
1267 local_irq_save(flags);
1268 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1269 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001270 AWA_DUMMY_READ(inen);
Bryan Wu1394f032007-05-06 14:50:22 -07001271 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001272
1273 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001274}
1275EXPORT_SYMBOL(gpio_direction_input);
1276
Michael Hennerichacbcd262008-01-22 18:36:20 +08001277int gpio_direction_output(unsigned gpio, int value)
Bryan Wu1394f032007-05-06 14:50:22 -07001278{
1279 unsigned long flags;
1280
Michael Hennerichacbcd262008-01-22 18:36:20 +08001281 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1282 gpio_error(gpio);
1283 return -EINVAL;
1284 }
Bryan Wu1394f032007-05-06 14:50:22 -07001285
1286 local_irq_save(flags);
1287 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001288
1289 if (value)
1290 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1291 else
1292 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1293
Bryan Wu1394f032007-05-06 14:50:22 -07001294 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001295 AWA_DUMMY_READ(dir);
Bryan Wu1394f032007-05-06 14:50:22 -07001296 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001297
1298 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001299}
1300EXPORT_SYMBOL(gpio_direction_output);
Mike Frysinger168f1212007-10-11 00:22:35 +08001301
1302/* If we are booting from SPI and our board lacks a strong enough pull up,
1303 * the core can reset and execute the bootrom faster than the resistor can
1304 * pull the signal logically high. To work around this (common) error in
1305 * board design, we explicitly set the pin back to GPIO mode, force /CS
1306 * high, and wait for the electrons to do their thing.
1307 *
1308 * This function only makes sense to be called from reset code, but it
1309 * lives here as we need to force all the GPIO states w/out going through
1310 * BUG() checks and such.
1311 */
1312void bfin_gpio_reset_spi0_ssel1(void)
1313{
Michael Hennerich4d5f4ed2007-08-27 16:46:17 +08001314 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1315
1316 port_setup(gpio, GPIO_USAGE);
1317 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001318 AWA_DUMMY_READ(data_set);
Mike Frysinger168f1212007-10-11 00:22:35 +08001319 udelay(1);
1320}
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001321
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001322void bfin_gpio_irq_prepare(unsigned gpio)
1323{
1324 port_setup(gpio, GPIO_USAGE);
1325}
1326
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001327#endif /*BF548_FAMILY */
Mike Frysinger1545a112007-12-24 16:54:48 +08001328
1329#if defined(CONFIG_PROC_FS)
1330static int gpio_proc_read(char *buf, char **start, off_t offset,
1331 int len, int *unused_i, void *unused_v)
1332{
1333 int c, outlen = 0;
1334
1335 for (c = 0; c < MAX_RESOURCES; c++) {
1336 if (!check_gpio(c) && (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c)))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001337 len = sprintf(buf, "GPIO_%d: %s \t\tGPIO %s\n", c,
Mike Frysinger1545a112007-12-24 16:54:48 +08001338 get_label(c), get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1339 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001340 len = sprintf(buf, "GPIO_%d: %s \t\tPeripheral\n", c, get_label(c));
Mike Frysinger1545a112007-12-24 16:54:48 +08001341 else
1342 continue;
1343 buf += len;
1344 outlen += len;
1345 }
1346 return outlen;
1347}
1348
1349static __init int gpio_register_proc(void)
1350{
1351 struct proc_dir_entry *proc_gpio;
1352
1353 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1354 if (proc_gpio)
1355 proc_gpio->read_proc = gpio_proc_read;
1356 return proc_gpio != NULL;
1357}
Mike Frysinger1545a112007-12-24 16:54:48 +08001358__initcall(gpio_register_proc);
1359#endif