blob: a09dc48050b4cde2391ad7780b16a26507a824f8 [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,
98 AWA_maska = UART_SCR,
99 AWA_maska_clear = UART_SCR,
100 AWA_maska_set = UART_SCR,
101 AWA_maska_toggle = UART_SCR,
102 AWA_maskb = UART_GCTL,
103 AWA_maskb_clear = UART_GCTL,
104 AWA_maskb_set = UART_GCTL,
105 AWA_maskb_toggle = UART_GCTL,
106 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 Hennerichcfefe3c2008-02-09 04:12:37 +0800189#if defined(CONFIG_PM) && !defined(CONFIG_BF54x)
Bryan Wu1394f032007-05-06 14:50:22 -0700190static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
191static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
192static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
193
194#ifdef BF533_FAMILY
195static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
196#endif
197
198#ifdef BF537_FAMILY
199static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
200#endif
201
Michael Hennerich59003142007-10-21 16:54:27 +0800202#ifdef BF527_FAMILY
203static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
204#endif
205
Bryan Wu1394f032007-05-06 14:50:22 -0700206#ifdef BF561_FAMILY
207static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
208#endif
209
210#endif /* CONFIG_PM */
211
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800212#if defined(BF548_FAMILY)
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800213inline int check_gpio(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800214{
215 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
216 || gpio == GPIO_PH14 || gpio == GPIO_PH15
217 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15
218 || gpio > MAX_BLACKFIN_GPIOS)
219 return -EINVAL;
220 return 0;
221}
222#else
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800223inline int check_gpio(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700224{
Michael Henneriche7613aa2007-06-11 16:37:57 +0800225 if (gpio >= MAX_BLACKFIN_GPIOS)
Bryan Wu1394f032007-05-06 14:50:22 -0700226 return -EINVAL;
227 return 0;
228}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800229#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700230
Michael Hennerichacbcd262008-01-22 18:36:20 +0800231void gpio_error(unsigned gpio)
232{
233 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
234}
235
Michael Hennerichc58c2142007-10-04 00:35:05 +0800236static void set_label(unsigned short ident, const char *label)
237{
Michael Hennerichc58c2142007-10-04 00:35:05 +0800238 if (label && str_ident) {
Michael Hennerich8c613622007-08-03 17:48:09 +0800239 strncpy(str_ident[ident].name, label,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800240 RESOURCE_LABEL_SIZE);
Michael Hennerich8c613622007-08-03 17:48:09 +0800241 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800242 }
243}
244
245static char *get_label(unsigned short ident)
246{
247 if (!str_ident)
248 return "UNKNOWN";
249
Michael Hennerich8c613622007-08-03 17:48:09 +0800250 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
Michael Hennerichc58c2142007-10-04 00:35:05 +0800251}
252
253static int cmp_label(unsigned short ident, const char *label)
254{
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800255 if (label == NULL) {
256 dump_stack();
257 printk(KERN_ERR "Please provide none-null label\n");
258 }
259
Michael Hennerichc58c2142007-10-04 00:35:05 +0800260 if (label && str_ident)
Michael Hennerich8c613622007-08-03 17:48:09 +0800261 return strncmp(str_ident[ident].name,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800262 label, strlen(label));
263 else
264 return -EINVAL;
265}
266
Michael Hennerich59003142007-10-21 16:54:27 +0800267#if defined(BF527_FAMILY) || defined(BF537_FAMILY)
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800268static void port_setup(unsigned gpio, unsigned short usage)
Bryan Wu1394f032007-05-06 14:50:22 -0700269{
Michael Hennerichcda6a202007-10-04 00:36:18 +0800270 if (!check_gpio(gpio)) {
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800271 if (usage == GPIO_USAGE)
Michael Hennerichcda6a202007-10-04 00:36:18 +0800272 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800273 else
Michael Hennerichcda6a202007-10-04 00:36:18 +0800274 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
275 SSYNC();
276 }
Bryan Wu1394f032007-05-06 14:50:22 -0700277}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800278#elif defined(BF548_FAMILY)
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800279static void port_setup(unsigned gpio, unsigned short usage)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800280{
281 if (usage == GPIO_USAGE)
282 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
283 else
284 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
285 SSYNC();
286}
Bryan Wu1394f032007-05-06 14:50:22 -0700287#else
288# define port_setup(...) do { } while (0)
289#endif
290
Michael Hennerichc58c2142007-10-04 00:35:05 +0800291#ifdef BF537_FAMILY
Michael Hennerich8c613622007-08-03 17:48:09 +0800292static struct {
293 unsigned short res;
294 unsigned short offset;
295} port_mux_lut[] = {
296 {.res = P_PPI0_D13, .offset = 11},
297 {.res = P_PPI0_D14, .offset = 11},
298 {.res = P_PPI0_D15, .offset = 11},
299 {.res = P_SPORT1_TFS, .offset = 11},
300 {.res = P_SPORT1_TSCLK, .offset = 11},
301 {.res = P_SPORT1_DTPRI, .offset = 11},
302 {.res = P_PPI0_D10, .offset = 10},
303 {.res = P_PPI0_D11, .offset = 10},
304 {.res = P_PPI0_D12, .offset = 10},
305 {.res = P_SPORT1_RSCLK, .offset = 10},
306 {.res = P_SPORT1_RFS, .offset = 10},
307 {.res = P_SPORT1_DRPRI, .offset = 10},
308 {.res = P_PPI0_D8, .offset = 9},
309 {.res = P_PPI0_D9, .offset = 9},
310 {.res = P_SPORT1_DRSEC, .offset = 9},
311 {.res = P_SPORT1_DTSEC, .offset = 9},
312 {.res = P_TMR2, .offset = 8},
313 {.res = P_PPI0_FS3, .offset = 8},
314 {.res = P_TMR3, .offset = 7},
315 {.res = P_SPI0_SSEL4, .offset = 7},
316 {.res = P_TMR4, .offset = 6},
317 {.res = P_SPI0_SSEL5, .offset = 6},
318 {.res = P_TMR5, .offset = 5},
319 {.res = P_SPI0_SSEL6, .offset = 5},
320 {.res = P_UART1_RX, .offset = 4},
321 {.res = P_UART1_TX, .offset = 4},
322 {.res = P_TMR6, .offset = 4},
323 {.res = P_TMR7, .offset = 4},
324 {.res = P_UART0_RX, .offset = 3},
325 {.res = P_UART0_TX, .offset = 3},
326 {.res = P_DMAR0, .offset = 3},
327 {.res = P_DMAR1, .offset = 3},
328 {.res = P_SPORT0_DTSEC, .offset = 1},
329 {.res = P_SPORT0_DRSEC, .offset = 1},
330 {.res = P_CAN0_RX, .offset = 1},
331 {.res = P_CAN0_TX, .offset = 1},
332 {.res = P_SPI0_SSEL7, .offset = 1},
333 {.res = P_SPORT0_TFS, .offset = 0},
334 {.res = P_SPORT0_DTPRI, .offset = 0},
335 {.res = P_SPI0_SSEL2, .offset = 0},
336 {.res = P_SPI0_SSEL3, .offset = 0},
Michael Hennerichc58c2142007-10-04 00:35:05 +0800337};
338
339static void portmux_setup(unsigned short per, unsigned short function)
340{
Michael Hennerich8c613622007-08-03 17:48:09 +0800341 u16 y, offset, muxreg;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800342
Michael Hennerich8c613622007-08-03 17:48:09 +0800343 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
344 if (port_mux_lut[y].res == per) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800345
346 /* SET PORTMUX REG */
347
Michael Hennerich8c613622007-08-03 17:48:09 +0800348 offset = port_mux_lut[y].offset;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800349 muxreg = bfin_read_PORT_MUX();
350
351 if (offset != 1) {
352 muxreg &= ~(1 << offset);
353 } else {
354 muxreg &= ~(3 << 1);
355 }
356
357 muxreg |= (function << offset);
358 bfin_write_PORT_MUX(muxreg);
359 }
360 }
361}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800362#elif defined(BF548_FAMILY)
363inline void portmux_setup(unsigned short portno, unsigned short function)
364{
365 u32 pmux;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800366
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800367 pmux = gpio_array[gpio_bank(portno)]->port_mux;
368
369 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
370 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
371
372 gpio_array[gpio_bank(portno)]->port_mux = pmux;
373}
374
375inline u16 get_portmux(unsigned short portno)
376{
377 u32 pmux;
378
379 pmux = gpio_array[gpio_bank(portno)]->port_mux;
380
381 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
382}
Michael Hennerich59003142007-10-21 16:54:27 +0800383#elif defined(BF527_FAMILY)
384inline void portmux_setup(unsigned short portno, unsigned short function)
385{
386 u16 pmux, ident = P_IDENT(portno);
387 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
388
389 pmux = *port_mux[gpio_bank(ident)];
390 pmux &= ~(3 << offset);
391 pmux |= (function & 3) << offset;
392 *port_mux[gpio_bank(ident)] = pmux;
393 SSYNC();
394}
Michael Hennerichc58c2142007-10-04 00:35:05 +0800395#else
396# define portmux_setup(...) do { } while (0)
397#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700398
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800399#ifndef BF548_FAMILY
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800400static void default_gpio(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700401{
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800402 unsigned short bank, bitmask;
Michael Hennerich2b393312007-10-10 16:58:49 +0800403 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700404
405 bank = gpio_bank(gpio);
406 bitmask = gpio_bit(gpio);
407
Michael Hennerich2b393312007-10-10 16:58:49 +0800408 local_irq_save(flags);
409
Bryan Wu1394f032007-05-06 14:50:22 -0700410 gpio_bankb[bank]->maska_clear = bitmask;
411 gpio_bankb[bank]->maskb_clear = bitmask;
412 SSYNC();
413 gpio_bankb[bank]->inen &= ~bitmask;
414 gpio_bankb[bank]->dir &= ~bitmask;
415 gpio_bankb[bank]->polar &= ~bitmask;
416 gpio_bankb[bank]->both &= ~bitmask;
417 gpio_bankb[bank]->edge &= ~bitmask;
Michael Hennerich2b393312007-10-10 16:58:49 +0800418 AWA_DUMMY_READ(edge);
419 local_irq_restore(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700420}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800421#else
422# define default_gpio(...) do { } while (0)
423#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700424
Mike Frysingera161bb02007-05-21 18:09:14 +0800425static int __init bfin_gpio_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700426{
Michael Hennerich8c613622007-08-03 17:48:09 +0800427
Bryan Wu1394f032007-05-06 14:50:22 -0700428 printk(KERN_INFO "Blackfin GPIO Controller\n");
429
Bryan Wu1394f032007-05-06 14:50:22 -0700430 return 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800431
Bryan Wu1394f032007-05-06 14:50:22 -0700432}
Bryan Wu1394f032007-05-06 14:50:22 -0700433arch_initcall(bfin_gpio_init);
434
435
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800436#ifndef BF548_FAMILY
Bryan Wu1394f032007-05-06 14:50:22 -0700437/***********************************************************
438*
439* FUNCTIONS: Blackfin General Purpose Ports Access Functions
440*
441* INPUTS/OUTPUTS:
442* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
443*
444*
445* DESCRIPTION: These functions abstract direct register access
446* to Blackfin processor General Purpose
447* Ports Regsiters
448*
449* CAUTION: These functions do not belong to the GPIO Driver API
450*************************************************************
451* MODIFICATION HISTORY :
452**************************************************************/
453
454/* Set a specific bit */
455
456#define SET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800457void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700458{ \
459 unsigned long flags; \
Bryan Wu1394f032007-05-06 14:50:22 -0700460 local_irq_save(flags); \
461 if (arg) \
462 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
463 else \
464 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800465 AWA_DUMMY_READ(name); \
Bryan Wu1394f032007-05-06 14:50:22 -0700466 local_irq_restore(flags); \
467} \
468EXPORT_SYMBOL(set_gpio_ ## name);
469
470SET_GPIO(dir)
471SET_GPIO(inen)
472SET_GPIO(polar)
473SET_GPIO(edge)
474SET_GPIO(both)
475
476
Michael Hennerich2b393312007-10-10 16:58:49 +0800477#if ANOMALY_05000311 || ANOMALY_05000323
478#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800479void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800480{ \
481 unsigned long flags; \
Michael Hennerich2b393312007-10-10 16:58:49 +0800482 local_irq_save(flags); \
483 if (arg) \
484 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
485 else \
486 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
487 AWA_DUMMY_READ(name); \
488 local_irq_restore(flags); \
489} \
490EXPORT_SYMBOL(set_gpio_ ## name);
491#else
Bryan Wu1394f032007-05-06 14:50:22 -0700492#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800493void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700494{ \
Bryan Wu1394f032007-05-06 14:50:22 -0700495 if (arg) \
496 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
497 else \
498 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
499} \
500EXPORT_SYMBOL(set_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800501#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700502
503SET_GPIO_SC(maska)
504SET_GPIO_SC(maskb)
Bryan Wu1394f032007-05-06 14:50:22 -0700505SET_GPIO_SC(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700506
Michael Hennerich2b393312007-10-10 16:58:49 +0800507#if ANOMALY_05000311 || ANOMALY_05000323
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800508void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700509{
510 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700511 local_irq_save(flags);
512 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +0800513 AWA_DUMMY_READ(toggle);
Bryan Wu1394f032007-05-06 14:50:22 -0700514 local_irq_restore(flags);
515}
516#else
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800517void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700518{
Bryan Wu1394f032007-05-06 14:50:22 -0700519 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
520}
521#endif
522EXPORT_SYMBOL(set_gpio_toggle);
523
524
525/*Set current PORT date (16-bit word)*/
526
Michael Hennerich2b393312007-10-10 16:58:49 +0800527#if ANOMALY_05000311 || ANOMALY_05000323
528#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800529void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800530{ \
531 unsigned long flags; \
532 local_irq_save(flags); \
533 gpio_bankb[gpio_bank(gpio)]->name = arg; \
534 AWA_DUMMY_READ(name); \
535 local_irq_restore(flags); \
536} \
537EXPORT_SYMBOL(set_gpiop_ ## name);
538#else
Bryan Wu1394f032007-05-06 14:50:22 -0700539#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800540void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700541{ \
542 gpio_bankb[gpio_bank(gpio)]->name = arg; \
543} \
544EXPORT_SYMBOL(set_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800545#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700546
Michael Hennerich2b393312007-10-10 16:58:49 +0800547SET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700548SET_GPIO_P(dir)
549SET_GPIO_P(inen)
550SET_GPIO_P(polar)
551SET_GPIO_P(edge)
552SET_GPIO_P(both)
553SET_GPIO_P(maska)
554SET_GPIO_P(maskb)
555
Bryan Wu1394f032007-05-06 14:50:22 -0700556/* Get a specific bit */
Michael Hennerich2b393312007-10-10 16:58:49 +0800557#if ANOMALY_05000311 || ANOMALY_05000323
558#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800559unsigned short get_gpio_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800560{ \
561 unsigned long flags; \
562 unsigned short ret; \
563 local_irq_save(flags); \
564 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
565 AWA_DUMMY_READ(name); \
566 local_irq_restore(flags); \
567 return ret; \
568} \
569EXPORT_SYMBOL(get_gpio_ ## name);
570#else
Bryan Wu1394f032007-05-06 14:50:22 -0700571#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800572unsigned short get_gpio_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700573{ \
574 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
575} \
576EXPORT_SYMBOL(get_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800577#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700578
Michael Hennerich2b393312007-10-10 16:58:49 +0800579GET_GPIO(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700580GET_GPIO(dir)
581GET_GPIO(inen)
582GET_GPIO(polar)
583GET_GPIO(edge)
584GET_GPIO(both)
585GET_GPIO(maska)
586GET_GPIO(maskb)
587
Bryan Wu1394f032007-05-06 14:50:22 -0700588/*Get current PORT date (16-bit word)*/
589
Michael Hennerich2b393312007-10-10 16:58:49 +0800590#if ANOMALY_05000311 || ANOMALY_05000323
591#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800592unsigned short get_gpiop_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800593{ \
594 unsigned long flags; \
595 unsigned short ret; \
596 local_irq_save(flags); \
597 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
598 AWA_DUMMY_READ(name); \
599 local_irq_restore(flags); \
600 return ret; \
601} \
602EXPORT_SYMBOL(get_gpiop_ ## name);
603#else
Bryan Wu1394f032007-05-06 14:50:22 -0700604#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800605unsigned short get_gpiop_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700606{ \
607 return (gpio_bankb[gpio_bank(gpio)]->name);\
608} \
609EXPORT_SYMBOL(get_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800610#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700611
Michael Hennerich2b393312007-10-10 16:58:49 +0800612GET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700613GET_GPIO_P(dir)
614GET_GPIO_P(inen)
615GET_GPIO_P(polar)
616GET_GPIO_P(edge)
617GET_GPIO_P(both)
618GET_GPIO_P(maska)
619GET_GPIO_P(maskb)
620
Bryan Wu1394f032007-05-06 14:50:22 -0700621
622#ifdef CONFIG_PM
623/***********************************************************
624*
625* FUNCTIONS: Blackfin PM Setup API
626*
627* INPUTS/OUTPUTS:
628* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
629* type -
630* PM_WAKE_RISING
631* PM_WAKE_FALLING
632* PM_WAKE_HIGH
633* PM_WAKE_LOW
634* PM_WAKE_BOTH_EDGES
635*
636* DESCRIPTION: Blackfin PM Driver API
637*
638* CAUTION:
639*************************************************************
640* MODIFICATION HISTORY :
641**************************************************************/
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800642int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700643{
644 unsigned long flags;
645
646 if ((check_gpio(gpio) < 0) || !type)
647 return -EINVAL;
648
649 local_irq_save(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700650 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
651 wakeup_flags_map[gpio] = type;
652 local_irq_restore(flags);
653
654 return 0;
655}
656EXPORT_SYMBOL(gpio_pm_wakeup_request);
657
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800658void gpio_pm_wakeup_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700659{
660 unsigned long flags;
661
662 if (check_gpio(gpio) < 0)
663 return;
664
665 local_irq_save(flags);
666
667 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
668
669 local_irq_restore(flags);
670}
671EXPORT_SYMBOL(gpio_pm_wakeup_free);
672
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800673static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700674{
675 port_setup(gpio, GPIO_USAGE);
676 set_gpio_dir(gpio, 0);
677 set_gpio_inen(gpio, 1);
678
679 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
680 set_gpio_edge(gpio, 1);
681 else
682 set_gpio_edge(gpio, 0);
683
684 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
685 set_gpio_both(gpio, 1);
686 else
687 set_gpio_both(gpio, 0);
688
689 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
690 set_gpio_polar(gpio, 1);
691 else
692 set_gpio_polar(gpio, 0);
693
694 SSYNC();
695
696 return 0;
697}
698
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800699u32 bfin_pm_setup(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700700{
Bryan Wu1394f032007-05-06 14:50:22 -0700701 u16 bank, mask, i, gpio;
702
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800703 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700704 mask = wakeup_map[gpio_bank(i)];
705 bank = gpio_bank(i);
706
707 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
708 gpio_bankb[bank]->maskb = 0;
709
710 if (mask) {
711#ifdef BF537_FAMILY
712 gpio_bank_saved[bank].fer = *port_fer[bank];
713#endif
714 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
715 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
716 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
717 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
718 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800719 gpio_bank_saved[bank].reserved =
720 reserved_gpio_map[bank];
Bryan Wu1394f032007-05-06 14:50:22 -0700721
722 gpio = i;
723
724 while (mask) {
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800725 if ((mask & 1) && (wakeup_flags_map[gpio] !=
726 PM_WAKE_IGNORE)) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800727 reserved_gpio_map[gpio_bank(gpio)] |=
Michael Hennerich581d62a2007-06-14 13:30:23 +0800728 gpio_bit(gpio);
729 bfin_gpio_wakeup_type(gpio,
730 wakeup_flags_map[gpio]);
Bryan Wu1394f032007-05-06 14:50:22 -0700731 set_gpio_data(gpio, 0); /*Clear*/
732 }
733 gpio++;
734 mask >>= 1;
735 }
736
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800737 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
Bryan Wu1394f032007-05-06 14:50:22 -0700738 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
739 }
740 }
741
Michael Hennerich2b393312007-10-10 16:58:49 +0800742 AWA_DUMMY_READ(maskb_set);
743
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800744 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700745}
746
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800747void bfin_pm_restore(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700748{
749 u16 bank, mask, i;
750
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800751 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700752 mask = wakeup_map[gpio_bank(i)];
753 bank = gpio_bank(i);
754
755 if (mask) {
756#ifdef BF537_FAMILY
757 *port_fer[bank] = gpio_bank_saved[bank].fer;
758#endif
759 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
760 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
761 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
762 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
763 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800764
Michael Hennerichc58c2142007-10-04 00:35:05 +0800765 reserved_gpio_map[bank] =
766 gpio_bank_saved[bank].reserved;
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800767 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
Bryan Wu1394f032007-05-06 14:50:22 -0700768 }
769
770 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
771 }
Michael Hennerich2b393312007-10-10 16:58:49 +0800772 AWA_DUMMY_READ(maskb);
Bryan Wu1394f032007-05-06 14:50:22 -0700773}
774
775#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800776#else /* BF548_FAMILY */
777
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800778unsigned short get_gpio_dir(unsigned gpio)
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800779{
780 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
781}
782EXPORT_SYMBOL(get_gpio_dir);
783
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800784#endif /* BF548_FAMILY */
Bryan Wu1394f032007-05-06 14:50:22 -0700785
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800786/***********************************************************
787*
788* FUNCTIONS: Blackfin Peripheral Resource Allocation
789* and PortMux Setup
790*
791* INPUTS/OUTPUTS:
792* per Peripheral Identifier
793* label String
794*
795* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
796*
797* CAUTION:
798*************************************************************
799* MODIFICATION HISTORY :
800**************************************************************/
Michael Hennerichc58c2142007-10-04 00:35:05 +0800801
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800802#ifdef BF548_FAMILY
803int peripheral_request(unsigned short per, const char *label)
804{
805 unsigned long flags;
806 unsigned short ident = P_IDENT(per);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800807
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800808 /*
809 * Don't cares are pins with only one dedicated function
810 */
811
812 if (per & P_DONTCARE)
813 return 0;
814
815 if (!(per & P_DEFINED))
816 return -ENODEV;
817
818 if (check_gpio(ident) < 0)
819 return -EINVAL;
820
821 local_irq_save(flags);
822
823 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800824 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800825 printk(KERN_ERR
826 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
827 __FUNCTION__, ident, get_label(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800828 local_irq_restore(flags);
829 return -EBUSY;
830 }
831
832 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
833
834 u16 funct = get_portmux(ident);
835
836 /*
837 * Pin functions like AMC address strobes my
838 * be requested and used by several drivers
839 */
840
841 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
842
843 /*
844 * Allow that the identical pin function can
845 * be requested from the same driver twice
846 */
847
848 if (cmp_label(ident, label) == 0)
849 goto anyway;
850
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800851 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800852 printk(KERN_ERR
853 "%s: Peripheral %d function %d is already reserved by %s !\n",
854 __FUNCTION__, ident, P_FUNCT2MUX(per), get_label(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800855 local_irq_restore(flags);
856 return -EBUSY;
857 }
858 }
859
860anyway:
861 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
862
863 portmux_setup(ident, P_FUNCT2MUX(per));
864 port_setup(ident, PERIPHERAL_USAGE);
865
866 local_irq_restore(flags);
867 set_label(ident, label);
868
869 return 0;
870}
871EXPORT_SYMBOL(peripheral_request);
872#else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800873
874int peripheral_request(unsigned short per, const char *label)
875{
876 unsigned long flags;
877 unsigned short ident = P_IDENT(per);
878
879 /*
880 * Don't cares are pins with only one dedicated function
881 */
882
883 if (per & P_DONTCARE)
884 return 0;
885
886 if (!(per & P_DEFINED))
887 return -ENODEV;
888
Michael Hennerichc58c2142007-10-04 00:35:05 +0800889 local_irq_save(flags);
890
Michael Hennerichcda6a202007-10-04 00:36:18 +0800891 if (!check_gpio(ident)) {
892
Michael Hennerichc58c2142007-10-04 00:35:05 +0800893 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800894 dump_stack();
Michael Hennerichc58c2142007-10-04 00:35:05 +0800895 printk(KERN_ERR
896 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
897 __FUNCTION__, ident, get_label(ident));
Michael Hennerichc58c2142007-10-04 00:35:05 +0800898 local_irq_restore(flags);
899 return -EBUSY;
900 }
901
Michael Hennerichcda6a202007-10-04 00:36:18 +0800902 }
903
Michael Hennerichc58c2142007-10-04 00:35:05 +0800904 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
905
906 /*
907 * Pin functions like AMC address strobes my
908 * be requested and used by several drivers
909 */
910
911 if (!(per & P_MAYSHARE)) {
912
913 /*
914 * Allow that the identical pin function can
915 * be requested from the same driver twice
916 */
917
918 if (cmp_label(ident, label) == 0)
919 goto anyway;
920
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800921 dump_stack();
Michael Hennerichc58c2142007-10-04 00:35:05 +0800922 printk(KERN_ERR
923 "%s: Peripheral %d function %d is already"
Michael Hennerich8c613622007-08-03 17:48:09 +0800924 " reserved by %s !\n",
Michael Hennerichc58c2142007-10-04 00:35:05 +0800925 __FUNCTION__, ident, P_FUNCT2MUX(per),
926 get_label(ident));
Michael Hennerichc58c2142007-10-04 00:35:05 +0800927 local_irq_restore(flags);
928 return -EBUSY;
929 }
930
931 }
932
933anyway:
Michael Hennerichc58c2142007-10-04 00:35:05 +0800934 portmux_setup(per, P_FUNCT2MUX(per));
935
936 port_setup(ident, PERIPHERAL_USAGE);
937
938 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
939 local_irq_restore(flags);
940 set_label(ident, label);
941
942 return 0;
943}
944EXPORT_SYMBOL(peripheral_request);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800945#endif
Michael Hennerichc58c2142007-10-04 00:35:05 +0800946
947int peripheral_request_list(unsigned short per[], const char *label)
948{
949 u16 cnt;
950 int ret;
951
952 for (cnt = 0; per[cnt] != 0; cnt++) {
Michael Hennerich314c98d2007-07-24 18:03:45 +0800953
Michael Hennerichc58c2142007-10-04 00:35:05 +0800954 ret = peripheral_request(per[cnt], label);
Michael Hennerich314c98d2007-07-24 18:03:45 +0800955
956 if (ret < 0) {
957 for ( ; cnt > 0; cnt--) {
958 peripheral_free(per[cnt - 1]);
959 }
960 return ret;
961 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800962 }
963
964 return 0;
965}
966EXPORT_SYMBOL(peripheral_request_list);
967
968void peripheral_free(unsigned short per)
969{
970 unsigned long flags;
971 unsigned short ident = P_IDENT(per);
972
973 if (per & P_DONTCARE)
974 return;
975
976 if (!(per & P_DEFINED))
977 return;
978
979 if (check_gpio(ident) < 0)
980 return;
981
982 local_irq_save(flags);
983
984 if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
985 & gpio_bit(ident)))) {
986 local_irq_restore(flags);
987 return;
988 }
989
990 if (!(per & P_MAYSHARE)) {
991 port_setup(ident, GPIO_USAGE);
992 }
993
994 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
995
Michael Hennerich2acde902007-10-11 00:24:40 +0800996 set_label(ident, "free");
997
Michael Hennerichc58c2142007-10-04 00:35:05 +0800998 local_irq_restore(flags);
999}
1000EXPORT_SYMBOL(peripheral_free);
1001
1002void peripheral_free_list(unsigned short per[])
1003{
1004 u16 cnt;
1005
1006 for (cnt = 0; per[cnt] != 0; cnt++) {
1007 peripheral_free(per[cnt]);
1008 }
1009
1010}
1011EXPORT_SYMBOL(peripheral_free_list);
1012
Bryan Wu1394f032007-05-06 14:50:22 -07001013/***********************************************************
1014*
1015* FUNCTIONS: Blackfin GPIO Driver
1016*
1017* INPUTS/OUTPUTS:
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001018* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1019* label String
Bryan Wu1394f032007-05-06 14:50:22 -07001020*
1021* DESCRIPTION: Blackfin GPIO Driver API
1022*
1023* CAUTION:
1024*************************************************************
1025* MODIFICATION HISTORY :
1026**************************************************************/
1027
Michael Hennerichacbcd262008-01-22 18:36:20 +08001028int gpio_request(unsigned gpio, const char *label)
Bryan Wu1394f032007-05-06 14:50:22 -07001029{
1030 unsigned long flags;
1031
1032 if (check_gpio(gpio) < 0)
1033 return -EINVAL;
1034
1035 local_irq_save(flags);
1036
Michael Hennerich2acde902007-10-11 00:24:40 +08001037 /*
1038 * Allow that the identical GPIO can
1039 * be requested from the same driver twice
1040 * Do nothing and return -
1041 */
1042
1043 if (cmp_label(gpio, label) == 0) {
1044 local_irq_restore(flags);
1045 return 0;
1046 }
1047
Michael Hennerichc58c2142007-10-04 00:35:05 +08001048 if (unlikely(reserved_gpio_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 "bfin-gpio: GPIO %d is already reserved by %s !\n",
1051 gpio, get_label(gpio));
Bryan Wu1394f032007-05-06 14:50:22 -07001052 local_irq_restore(flags);
1053 return -EBUSY;
1054 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001055 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001056 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001057 printk(KERN_ERR
1058 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1059 gpio, get_label(gpio));
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001060 local_irq_restore(flags);
1061 return -EBUSY;
1062 }
1063
Michael Hennerichc58c2142007-10-04 00:35:05 +08001064 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001065
1066 local_irq_restore(flags);
1067
1068 port_setup(gpio, GPIO_USAGE);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001069 set_label(gpio, label);
Bryan Wu1394f032007-05-06 14:50:22 -07001070
1071 return 0;
1072}
1073EXPORT_SYMBOL(gpio_request);
1074
Michael Hennerichacbcd262008-01-22 18:36:20 +08001075void gpio_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001076{
1077 unsigned long flags;
1078
1079 if (check_gpio(gpio) < 0)
1080 return;
1081
1082 local_irq_save(flags);
1083
Michael Hennerichc58c2142007-10-04 00:35:05 +08001084 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
Bryan Wu1394f032007-05-06 14:50:22 -07001085 dump_stack();
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001086 gpio_error(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001087 local_irq_restore(flags);
1088 return;
1089 }
1090
1091 default_gpio(gpio);
1092
Michael Hennerichc58c2142007-10-04 00:35:05 +08001093 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001094
Michael Hennerich2acde902007-10-11 00:24:40 +08001095 set_label(gpio, "free");
1096
Bryan Wu1394f032007-05-06 14:50:22 -07001097 local_irq_restore(flags);
1098}
1099EXPORT_SYMBOL(gpio_free);
1100
Michael Hennerichacbcd262008-01-22 18:36:20 +08001101
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001102#ifdef BF548_FAMILY
Michael Hennerichacbcd262008-01-22 18:36:20 +08001103int gpio_direction_input(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001104{
1105 unsigned long flags;
1106
Michael Hennerichacbcd262008-01-22 18:36:20 +08001107 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1108 gpio_error(gpio);
1109 return -EINVAL;
1110 }
1111
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001112 local_irq_save(flags);
1113 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1114 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1115 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001116
1117 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001118}
1119EXPORT_SYMBOL(gpio_direction_input);
1120
Michael Hennerichacbcd262008-01-22 18:36:20 +08001121int gpio_direction_output(unsigned gpio, int value)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001122{
1123 unsigned long flags;
1124
Michael Hennerichacbcd262008-01-22 18:36:20 +08001125 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1126 gpio_error(gpio);
1127 return -EINVAL;
1128 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001129
1130 local_irq_save(flags);
1131 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001132 gpio_set_value(gpio, value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001133 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1134 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001135
1136 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001137}
1138EXPORT_SYMBOL(gpio_direction_output);
1139
Michael Hennerichacbcd262008-01-22 18:36:20 +08001140void gpio_set_value(unsigned gpio, int arg)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001141{
1142 if (arg)
1143 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1144 else
1145 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001146}
1147EXPORT_SYMBOL(gpio_set_value);
1148
Michael Hennerichacbcd262008-01-22 18:36:20 +08001149int gpio_get_value(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001150{
1151 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1152}
1153EXPORT_SYMBOL(gpio_get_value);
1154
1155#else
1156
Michael Hennerichacbcd262008-01-22 18:36:20 +08001157int gpio_direction_input(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001158{
1159 unsigned long flags;
1160
Michael Hennerichacbcd262008-01-22 18:36:20 +08001161 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1162 gpio_error(gpio);
1163 return -EINVAL;
1164 }
Bryan Wu1394f032007-05-06 14:50:22 -07001165
1166 local_irq_save(flags);
1167 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1168 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001169 AWA_DUMMY_READ(inen);
Bryan Wu1394f032007-05-06 14:50:22 -07001170 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001171
1172 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001173}
1174EXPORT_SYMBOL(gpio_direction_input);
1175
Michael Hennerichacbcd262008-01-22 18:36:20 +08001176int gpio_direction_output(unsigned gpio, int value)
Bryan Wu1394f032007-05-06 14:50:22 -07001177{
1178 unsigned long flags;
1179
Michael Hennerichacbcd262008-01-22 18:36:20 +08001180 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1181 gpio_error(gpio);
1182 return -EINVAL;
1183 }
Bryan Wu1394f032007-05-06 14:50:22 -07001184
1185 local_irq_save(flags);
1186 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001187
1188 if (value)
1189 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1190 else
1191 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1192
Bryan Wu1394f032007-05-06 14:50:22 -07001193 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001194 AWA_DUMMY_READ(dir);
Bryan Wu1394f032007-05-06 14:50:22 -07001195 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001196
1197 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001198}
1199EXPORT_SYMBOL(gpio_direction_output);
Mike Frysinger168f1212007-10-11 00:22:35 +08001200
1201/* If we are booting from SPI and our board lacks a strong enough pull up,
1202 * the core can reset and execute the bootrom faster than the resistor can
1203 * pull the signal logically high. To work around this (common) error in
1204 * board design, we explicitly set the pin back to GPIO mode, force /CS
1205 * high, and wait for the electrons to do their thing.
1206 *
1207 * This function only makes sense to be called from reset code, but it
1208 * lives here as we need to force all the GPIO states w/out going through
1209 * BUG() checks and such.
1210 */
1211void bfin_gpio_reset_spi0_ssel1(void)
1212{
Michael Hennerich4d5f4ed2007-08-27 16:46:17 +08001213 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1214
1215 port_setup(gpio, GPIO_USAGE);
1216 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001217 AWA_DUMMY_READ(data_set);
Mike Frysinger168f1212007-10-11 00:22:35 +08001218 udelay(1);
1219}
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001220
1221#endif /*BF548_FAMILY */
Mike Frysinger1545a112007-12-24 16:54:48 +08001222
1223#if defined(CONFIG_PROC_FS)
1224static int gpio_proc_read(char *buf, char **start, off_t offset,
1225 int len, int *unused_i, void *unused_v)
1226{
1227 int c, outlen = 0;
1228
1229 for (c = 0; c < MAX_RESOURCES; c++) {
1230 if (!check_gpio(c) && (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c)))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001231 len = sprintf(buf, "GPIO_%d: %s \t\tGPIO %s\n", c,
Mike Frysinger1545a112007-12-24 16:54:48 +08001232 get_label(c), get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1233 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001234 len = sprintf(buf, "GPIO_%d: %s \t\tPeripheral\n", c, get_label(c));
Mike Frysinger1545a112007-12-24 16:54:48 +08001235 else
1236 continue;
1237 buf += len;
1238 outlen += len;
1239 }
1240 return outlen;
1241}
1242
1243static __init int gpio_register_proc(void)
1244{
1245 struct proc_dir_entry *proc_gpio;
1246
1247 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1248 if (proc_gpio)
1249 proc_gpio->read_proc = gpio_proc_read;
1250 return proc_gpio != NULL;
1251}
Mike Frysinger1545a112007-12-24 16:54:48 +08001252__initcall(gpio_register_proc);
1253#endif