blob: 7312011a4211c9655820b73d0b24a83279e87dcb [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 Hennerichd2b11a42007-08-28 16:47:46 +080010 * Copyright 2007 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};
140
141#endif
142
Michael Hennerich59003142007-10-21 16:54:27 +0800143#ifdef BF527_FAMILY
144static unsigned short *port_mux[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
145 (unsigned short *) PORTF_MUX,
146 (unsigned short *) PORTG_MUX,
147 (unsigned short *) PORTH_MUX,
148};
149
150static const
151u8 pmux_offset[][16] =
152 {{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
153 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
154 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
155 };
156#endif
157
Bryan Wu1394f032007-05-06 14:50:22 -0700158#ifdef BF561_FAMILY
159static struct gpio_port_t *gpio_bankb[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
160 (struct gpio_port_t *) FIO0_FLAG_D,
161 (struct gpio_port_t *) FIO1_FLAG_D,
162 (struct gpio_port_t *) FIO2_FLAG_D,
163};
164#endif
165
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800166#ifdef BF548_FAMILY
167static struct gpio_port_t *gpio_array[gpio_bank(MAX_BLACKFIN_GPIOS)] = {
168 (struct gpio_port_t *)PORTA_FER,
169 (struct gpio_port_t *)PORTB_FER,
170 (struct gpio_port_t *)PORTC_FER,
171 (struct gpio_port_t *)PORTD_FER,
172 (struct gpio_port_t *)PORTE_FER,
173 (struct gpio_port_t *)PORTF_FER,
174 (struct gpio_port_t *)PORTG_FER,
175 (struct gpio_port_t *)PORTH_FER,
176 (struct gpio_port_t *)PORTI_FER,
177 (struct gpio_port_t *)PORTJ_FER,
178};
179#endif
180
Michael Hennerichc58c2142007-10-04 00:35:05 +0800181static unsigned short reserved_gpio_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800182static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
Michael Hennerichc58c2142007-10-04 00:35:05 +0800183
Michael Hennerich8c613622007-08-03 17:48:09 +0800184#define RESOURCE_LABEL_SIZE 16
185
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800186static struct str_ident {
Michael Hennerich8c613622007-08-03 17:48:09 +0800187 char name[RESOURCE_LABEL_SIZE];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800188} str_ident[MAX_RESOURCES];
Bryan Wu1394f032007-05-06 14:50:22 -0700189
190#ifdef CONFIG_PM
191static unsigned short wakeup_map[gpio_bank(MAX_BLACKFIN_GPIOS)];
192static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
193static struct gpio_port_s gpio_bank_saved[gpio_bank(MAX_BLACKFIN_GPIOS)];
194
195#ifdef BF533_FAMILY
196static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB};
197#endif
198
199#ifdef BF537_FAMILY
200static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
201#endif
202
Michael Hennerich59003142007-10-21 16:54:27 +0800203#ifdef BF527_FAMILY
204static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
205#endif
206
Bryan Wu1394f032007-05-06 14:50:22 -0700207#ifdef BF561_FAMILY
208static unsigned int sic_iwr_irqs[gpio_bank(MAX_BLACKFIN_GPIOS)] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
209#endif
210
211#endif /* CONFIG_PM */
212
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800213#if defined(BF548_FAMILY)
214inline int check_gpio(unsigned short gpio)
215{
216 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
217 || gpio == GPIO_PH14 || gpio == GPIO_PH15
218 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15
219 || gpio > MAX_BLACKFIN_GPIOS)
220 return -EINVAL;
221 return 0;
222}
223#else
Bryan Wu1394f032007-05-06 14:50:22 -0700224inline int check_gpio(unsigned short gpio)
225{
Michael Henneriche7613aa2007-06-11 16:37:57 +0800226 if (gpio >= MAX_BLACKFIN_GPIOS)
Bryan Wu1394f032007-05-06 14:50:22 -0700227 return -EINVAL;
228 return 0;
229}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800230#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700231
Michael Hennerichacbcd262008-01-22 18:36:20 +0800232void gpio_error(unsigned gpio)
233{
234 printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
235}
236
Michael Hennerichc58c2142007-10-04 00:35:05 +0800237static void set_label(unsigned short ident, const char *label)
238{
239
240 if (label && str_ident) {
Michael Hennerich8c613622007-08-03 17:48:09 +0800241 strncpy(str_ident[ident].name, label,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800242 RESOURCE_LABEL_SIZE);
Michael Hennerich8c613622007-08-03 17:48:09 +0800243 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800244 }
245}
246
247static char *get_label(unsigned short ident)
248{
249 if (!str_ident)
250 return "UNKNOWN";
251
Michael Hennerich8c613622007-08-03 17:48:09 +0800252 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
Michael Hennerichc58c2142007-10-04 00:35:05 +0800253}
254
255static int cmp_label(unsigned short ident, const char *label)
256{
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800257 if (label == NULL) {
258 dump_stack();
259 printk(KERN_ERR "Please provide none-null label\n");
260 }
261
Michael Hennerichc58c2142007-10-04 00:35:05 +0800262 if (label && str_ident)
Michael Hennerich8c613622007-08-03 17:48:09 +0800263 return strncmp(str_ident[ident].name,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800264 label, strlen(label));
265 else
266 return -EINVAL;
267}
268
Michael Hennerich59003142007-10-21 16:54:27 +0800269#if defined(BF527_FAMILY) || defined(BF537_FAMILY)
Mike Frysingera161bb02007-05-21 18:09:14 +0800270static void port_setup(unsigned short gpio, unsigned short usage)
Bryan Wu1394f032007-05-06 14:50:22 -0700271{
Michael Hennerichcda6a202007-10-04 00:36:18 +0800272 if (!check_gpio(gpio)) {
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800273 if (usage == GPIO_USAGE)
Michael Hennerichcda6a202007-10-04 00:36:18 +0800274 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800275 else
Michael Hennerichcda6a202007-10-04 00:36:18 +0800276 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
277 SSYNC();
278 }
Bryan Wu1394f032007-05-06 14:50:22 -0700279}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800280#elif defined(BF548_FAMILY)
281static void port_setup(unsigned short gpio, unsigned short usage)
282{
283 if (usage == GPIO_USAGE)
284 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
285 else
286 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
287 SSYNC();
288}
Bryan Wu1394f032007-05-06 14:50:22 -0700289#else
290# define port_setup(...) do { } while (0)
291#endif
292
Michael Hennerichc58c2142007-10-04 00:35:05 +0800293#ifdef BF537_FAMILY
Michael Hennerich8c613622007-08-03 17:48:09 +0800294static struct {
295 unsigned short res;
296 unsigned short offset;
297} port_mux_lut[] = {
298 {.res = P_PPI0_D13, .offset = 11},
299 {.res = P_PPI0_D14, .offset = 11},
300 {.res = P_PPI0_D15, .offset = 11},
301 {.res = P_SPORT1_TFS, .offset = 11},
302 {.res = P_SPORT1_TSCLK, .offset = 11},
303 {.res = P_SPORT1_DTPRI, .offset = 11},
304 {.res = P_PPI0_D10, .offset = 10},
305 {.res = P_PPI0_D11, .offset = 10},
306 {.res = P_PPI0_D12, .offset = 10},
307 {.res = P_SPORT1_RSCLK, .offset = 10},
308 {.res = P_SPORT1_RFS, .offset = 10},
309 {.res = P_SPORT1_DRPRI, .offset = 10},
310 {.res = P_PPI0_D8, .offset = 9},
311 {.res = P_PPI0_D9, .offset = 9},
312 {.res = P_SPORT1_DRSEC, .offset = 9},
313 {.res = P_SPORT1_DTSEC, .offset = 9},
314 {.res = P_TMR2, .offset = 8},
315 {.res = P_PPI0_FS3, .offset = 8},
316 {.res = P_TMR3, .offset = 7},
317 {.res = P_SPI0_SSEL4, .offset = 7},
318 {.res = P_TMR4, .offset = 6},
319 {.res = P_SPI0_SSEL5, .offset = 6},
320 {.res = P_TMR5, .offset = 5},
321 {.res = P_SPI0_SSEL6, .offset = 5},
322 {.res = P_UART1_RX, .offset = 4},
323 {.res = P_UART1_TX, .offset = 4},
324 {.res = P_TMR6, .offset = 4},
325 {.res = P_TMR7, .offset = 4},
326 {.res = P_UART0_RX, .offset = 3},
327 {.res = P_UART0_TX, .offset = 3},
328 {.res = P_DMAR0, .offset = 3},
329 {.res = P_DMAR1, .offset = 3},
330 {.res = P_SPORT0_DTSEC, .offset = 1},
331 {.res = P_SPORT0_DRSEC, .offset = 1},
332 {.res = P_CAN0_RX, .offset = 1},
333 {.res = P_CAN0_TX, .offset = 1},
334 {.res = P_SPI0_SSEL7, .offset = 1},
335 {.res = P_SPORT0_TFS, .offset = 0},
336 {.res = P_SPORT0_DTPRI, .offset = 0},
337 {.res = P_SPI0_SSEL2, .offset = 0},
338 {.res = P_SPI0_SSEL3, .offset = 0},
Michael Hennerichc58c2142007-10-04 00:35:05 +0800339};
340
341static void portmux_setup(unsigned short per, unsigned short function)
342{
Michael Hennerich8c613622007-08-03 17:48:09 +0800343 u16 y, offset, muxreg;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800344
Michael Hennerich8c613622007-08-03 17:48:09 +0800345 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
346 if (port_mux_lut[y].res == per) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800347
348 /* SET PORTMUX REG */
349
Michael Hennerich8c613622007-08-03 17:48:09 +0800350 offset = port_mux_lut[y].offset;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800351 muxreg = bfin_read_PORT_MUX();
352
353 if (offset != 1) {
354 muxreg &= ~(1 << offset);
355 } else {
356 muxreg &= ~(3 << 1);
357 }
358
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
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800401#ifndef BF548_FAMILY
Mike Frysingera161bb02007-05-21 18:09:14 +0800402static void default_gpio(unsigned short gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700403{
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800404 unsigned short bank, bitmask;
Michael Hennerich2b393312007-10-10 16:58:49 +0800405 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700406
407 bank = gpio_bank(gpio);
408 bitmask = gpio_bit(gpio);
409
Michael Hennerich2b393312007-10-10 16:58:49 +0800410 local_irq_save(flags);
411
Bryan Wu1394f032007-05-06 14:50:22 -0700412 gpio_bankb[bank]->maska_clear = bitmask;
413 gpio_bankb[bank]->maskb_clear = bitmask;
414 SSYNC();
415 gpio_bankb[bank]->inen &= ~bitmask;
416 gpio_bankb[bank]->dir &= ~bitmask;
417 gpio_bankb[bank]->polar &= ~bitmask;
418 gpio_bankb[bank]->both &= ~bitmask;
419 gpio_bankb[bank]->edge &= ~bitmask;
Michael Hennerich2b393312007-10-10 16:58:49 +0800420 AWA_DUMMY_READ(edge);
421 local_irq_restore(flags);
422
Bryan Wu1394f032007-05-06 14:50:22 -0700423}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800424#else
425# define default_gpio(...) do { } while (0)
426#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700427
Mike Frysingera161bb02007-05-21 18:09:14 +0800428static int __init bfin_gpio_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700429{
Michael Hennerich8c613622007-08-03 17:48:09 +0800430
Bryan Wu1394f032007-05-06 14:50:22 -0700431 printk(KERN_INFO "Blackfin GPIO Controller\n");
432
Bryan Wu1394f032007-05-06 14:50:22 -0700433 return 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800434
Bryan Wu1394f032007-05-06 14:50:22 -0700435}
Bryan Wu1394f032007-05-06 14:50:22 -0700436arch_initcall(bfin_gpio_init);
437
438
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800439#ifndef BF548_FAMILY
Bryan Wu1394f032007-05-06 14:50:22 -0700440/***********************************************************
441*
442* FUNCTIONS: Blackfin General Purpose Ports Access Functions
443*
444* INPUTS/OUTPUTS:
445* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
446*
447*
448* DESCRIPTION: These functions abstract direct register access
449* to Blackfin processor General Purpose
450* Ports Regsiters
451*
452* CAUTION: These functions do not belong to the GPIO Driver API
453*************************************************************
454* MODIFICATION HISTORY :
455**************************************************************/
456
457/* Set a specific bit */
458
459#define SET_GPIO(name) \
460void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
461{ \
462 unsigned long flags; \
Michael Hennerichc58c2142007-10-04 00:35:05 +0800463 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
Bryan Wu1394f032007-05-06 14:50:22 -0700464 local_irq_save(flags); \
465 if (arg) \
466 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
467 else \
468 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800469 AWA_DUMMY_READ(name); \
Bryan Wu1394f032007-05-06 14:50:22 -0700470 local_irq_restore(flags); \
471} \
472EXPORT_SYMBOL(set_gpio_ ## name);
473
474SET_GPIO(dir)
475SET_GPIO(inen)
476SET_GPIO(polar)
477SET_GPIO(edge)
478SET_GPIO(both)
479
480
Michael Hennerich2b393312007-10-10 16:58:49 +0800481#if ANOMALY_05000311 || ANOMALY_05000323
482#define SET_GPIO_SC(name) \
483void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
484{ \
485 unsigned long flags; \
486 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
487 local_irq_save(flags); \
488 if (arg) \
489 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
490 else \
491 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
492 AWA_DUMMY_READ(name); \
493 local_irq_restore(flags); \
494} \
495EXPORT_SYMBOL(set_gpio_ ## name);
496#else
Bryan Wu1394f032007-05-06 14:50:22 -0700497#define SET_GPIO_SC(name) \
498void set_gpio_ ## name(unsigned short gpio, unsigned short arg) \
499{ \
Michael Hennerichc58c2142007-10-04 00:35:05 +0800500 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))); \
Bryan Wu1394f032007-05-06 14:50:22 -0700501 if (arg) \
502 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
503 else \
504 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
505} \
506EXPORT_SYMBOL(set_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800507#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700508
509SET_GPIO_SC(maska)
510SET_GPIO_SC(maskb)
Bryan Wu1394f032007-05-06 14:50:22 -0700511SET_GPIO_SC(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700512
Michael Hennerich2b393312007-10-10 16:58:49 +0800513#if ANOMALY_05000311 || ANOMALY_05000323
Bryan Wu1394f032007-05-06 14:50:22 -0700514void set_gpio_toggle(unsigned short gpio)
515{
516 unsigned long flags;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800517 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700518 local_irq_save(flags);
519 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +0800520 AWA_DUMMY_READ(toggle);
Bryan Wu1394f032007-05-06 14:50:22 -0700521 local_irq_restore(flags);
522}
523#else
524void set_gpio_toggle(unsigned short gpio)
525{
Michael Hennerichc58c2142007-10-04 00:35:05 +0800526 BUG_ON(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)));
Bryan Wu1394f032007-05-06 14:50:22 -0700527 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
528}
529#endif
530EXPORT_SYMBOL(set_gpio_toggle);
531
532
533/*Set current PORT date (16-bit word)*/
534
Michael Hennerich2b393312007-10-10 16:58:49 +0800535#if ANOMALY_05000311 || ANOMALY_05000323
536#define SET_GPIO_P(name) \
537void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
538{ \
539 unsigned long flags; \
540 local_irq_save(flags); \
541 gpio_bankb[gpio_bank(gpio)]->name = arg; \
542 AWA_DUMMY_READ(name); \
543 local_irq_restore(flags); \
544} \
545EXPORT_SYMBOL(set_gpiop_ ## name);
546#else
Bryan Wu1394f032007-05-06 14:50:22 -0700547#define SET_GPIO_P(name) \
548void set_gpiop_ ## name(unsigned short gpio, unsigned short arg) \
549{ \
550 gpio_bankb[gpio_bank(gpio)]->name = arg; \
551} \
552EXPORT_SYMBOL(set_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800553#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700554
Michael Hennerich2b393312007-10-10 16:58:49 +0800555SET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700556SET_GPIO_P(dir)
557SET_GPIO_P(inen)
558SET_GPIO_P(polar)
559SET_GPIO_P(edge)
560SET_GPIO_P(both)
561SET_GPIO_P(maska)
562SET_GPIO_P(maskb)
563
564
Bryan Wu1394f032007-05-06 14:50:22 -0700565/* Get a specific bit */
Michael Hennerich2b393312007-10-10 16:58:49 +0800566#if ANOMALY_05000311 || ANOMALY_05000323
567#define GET_GPIO(name) \
568unsigned short get_gpio_ ## name(unsigned short gpio) \
569{ \
570 unsigned long flags; \
571 unsigned short ret; \
572 local_irq_save(flags); \
573 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
574 AWA_DUMMY_READ(name); \
575 local_irq_restore(flags); \
576 return ret; \
577} \
578EXPORT_SYMBOL(get_gpio_ ## name);
579#else
Bryan Wu1394f032007-05-06 14:50:22 -0700580#define GET_GPIO(name) \
581unsigned short get_gpio_ ## name(unsigned short gpio) \
582{ \
583 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
584} \
585EXPORT_SYMBOL(get_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800586#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700587
Michael Hennerich2b393312007-10-10 16:58:49 +0800588GET_GPIO(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700589GET_GPIO(dir)
590GET_GPIO(inen)
591GET_GPIO(polar)
592GET_GPIO(edge)
593GET_GPIO(both)
594GET_GPIO(maska)
595GET_GPIO(maskb)
596
Bryan Wu1394f032007-05-06 14:50:22 -0700597/*Get current PORT date (16-bit word)*/
598
Michael Hennerich2b393312007-10-10 16:58:49 +0800599#if ANOMALY_05000311 || ANOMALY_05000323
600#define GET_GPIO_P(name) \
601unsigned short get_gpiop_ ## name(unsigned short gpio) \
602{ \
603 unsigned long flags; \
604 unsigned short ret; \
605 local_irq_save(flags); \
606 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
607 AWA_DUMMY_READ(name); \
608 local_irq_restore(flags); \
609 return ret; \
610} \
611EXPORT_SYMBOL(get_gpiop_ ## name);
612#else
Bryan Wu1394f032007-05-06 14:50:22 -0700613#define GET_GPIO_P(name) \
614unsigned short get_gpiop_ ## name(unsigned short gpio) \
615{ \
616 return (gpio_bankb[gpio_bank(gpio)]->name);\
617} \
618EXPORT_SYMBOL(get_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800619#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700620
Michael Hennerich2b393312007-10-10 16:58:49 +0800621GET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700622GET_GPIO_P(dir)
623GET_GPIO_P(inen)
624GET_GPIO_P(polar)
625GET_GPIO_P(edge)
626GET_GPIO_P(both)
627GET_GPIO_P(maska)
628GET_GPIO_P(maskb)
629
Bryan Wu1394f032007-05-06 14:50:22 -0700630
631#ifdef CONFIG_PM
632/***********************************************************
633*
634* FUNCTIONS: Blackfin PM Setup API
635*
636* INPUTS/OUTPUTS:
637* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
638* type -
639* PM_WAKE_RISING
640* PM_WAKE_FALLING
641* PM_WAKE_HIGH
642* PM_WAKE_LOW
643* PM_WAKE_BOTH_EDGES
644*
645* DESCRIPTION: Blackfin PM Driver API
646*
647* CAUTION:
648*************************************************************
649* MODIFICATION HISTORY :
650**************************************************************/
651int gpio_pm_wakeup_request(unsigned short gpio, unsigned char type)
652{
653 unsigned long flags;
654
655 if ((check_gpio(gpio) < 0) || !type)
656 return -EINVAL;
657
658 local_irq_save(flags);
659
660 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
661 wakeup_flags_map[gpio] = type;
662 local_irq_restore(flags);
663
664 return 0;
665}
666EXPORT_SYMBOL(gpio_pm_wakeup_request);
667
668void gpio_pm_wakeup_free(unsigned short gpio)
669{
670 unsigned long flags;
671
672 if (check_gpio(gpio) < 0)
673 return;
674
675 local_irq_save(flags);
676
677 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
678
679 local_irq_restore(flags);
680}
681EXPORT_SYMBOL(gpio_pm_wakeup_free);
682
683static int bfin_gpio_wakeup_type(unsigned short gpio, unsigned char type)
684{
685 port_setup(gpio, GPIO_USAGE);
686 set_gpio_dir(gpio, 0);
687 set_gpio_inen(gpio, 1);
688
689 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
690 set_gpio_edge(gpio, 1);
691 else
692 set_gpio_edge(gpio, 0);
693
694 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
695 set_gpio_both(gpio, 1);
696 else
697 set_gpio_both(gpio, 0);
698
699 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
700 set_gpio_polar(gpio, 1);
701 else
702 set_gpio_polar(gpio, 0);
703
704 SSYNC();
705
706 return 0;
707}
708
709u32 gpio_pm_setup(void)
710{
711 u32 sic_iwr = 0;
712 u16 bank, mask, i, gpio;
713
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800714 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700715 mask = wakeup_map[gpio_bank(i)];
716 bank = gpio_bank(i);
717
718 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
719 gpio_bankb[bank]->maskb = 0;
720
721 if (mask) {
722#ifdef BF537_FAMILY
723 gpio_bank_saved[bank].fer = *port_fer[bank];
724#endif
725 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
726 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
727 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
728 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
729 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800730 gpio_bank_saved[bank].reserved =
731 reserved_gpio_map[bank];
Bryan Wu1394f032007-05-06 14:50:22 -0700732
733 gpio = i;
734
735 while (mask) {
736 if (mask & 1) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800737 reserved_gpio_map[gpio_bank(gpio)] |=
Michael Hennerich581d62a2007-06-14 13:30:23 +0800738 gpio_bit(gpio);
739 bfin_gpio_wakeup_type(gpio,
740 wakeup_flags_map[gpio]);
Bryan Wu1394f032007-05-06 14:50:22 -0700741 set_gpio_data(gpio, 0); /*Clear*/
742 }
743 gpio++;
744 mask >>= 1;
745 }
746
Michael Hennerich581d62a2007-06-14 13:30:23 +0800747 sic_iwr |= 1 <<
748 (sic_iwr_irqs[bank] - (IRQ_CORETMR + 1));
Bryan Wu1394f032007-05-06 14:50:22 -0700749 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
750 }
751 }
752
Michael Hennerich2b393312007-10-10 16:58:49 +0800753 AWA_DUMMY_READ(maskb_set);
754
Bryan Wu1394f032007-05-06 14:50:22 -0700755 if (sic_iwr)
756 return sic_iwr;
757 else
758 return IWR_ENABLE_ALL;
759}
760
Bryan Wu1394f032007-05-06 14:50:22 -0700761void gpio_pm_restore(void)
762{
763 u16 bank, mask, i;
764
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800765 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700766 mask = wakeup_map[gpio_bank(i)];
767 bank = gpio_bank(i);
768
769 if (mask) {
770#ifdef BF537_FAMILY
771 *port_fer[bank] = gpio_bank_saved[bank].fer;
772#endif
773 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
774 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
775 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
776 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
777 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800778
Michael Hennerichc58c2142007-10-04 00:35:05 +0800779 reserved_gpio_map[bank] =
780 gpio_bank_saved[bank].reserved;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800781
Bryan Wu1394f032007-05-06 14:50:22 -0700782 }
783
784 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
785 }
Michael Hennerich2b393312007-10-10 16:58:49 +0800786 AWA_DUMMY_READ(maskb);
Bryan Wu1394f032007-05-06 14:50:22 -0700787}
788
789#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800790#else /* BF548_FAMILY */
791
792unsigned short get_gpio_dir(unsigned short gpio)
793{
794 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
795}
796EXPORT_SYMBOL(get_gpio_dir);
797
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800798#endif /* BF548_FAMILY */
Bryan Wu1394f032007-05-06 14:50:22 -0700799
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800800/***********************************************************
801*
802* FUNCTIONS: Blackfin Peripheral Resource Allocation
803* and PortMux Setup
804*
805* INPUTS/OUTPUTS:
806* per Peripheral Identifier
807* label String
808*
809* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
810*
811* CAUTION:
812*************************************************************
813* MODIFICATION HISTORY :
814**************************************************************/
Michael Hennerichc58c2142007-10-04 00:35:05 +0800815
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800816#ifdef BF548_FAMILY
817int peripheral_request(unsigned short per, const char *label)
818{
819 unsigned long flags;
820 unsigned short ident = P_IDENT(per);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800821
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800822 /*
823 * Don't cares are pins with only one dedicated function
824 */
825
826 if (per & P_DONTCARE)
827 return 0;
828
829 if (!(per & P_DEFINED))
830 return -ENODEV;
831
832 if (check_gpio(ident) < 0)
833 return -EINVAL;
834
835 local_irq_save(flags);
836
837 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
838 printk(KERN_ERR
839 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
840 __FUNCTION__, ident, get_label(ident));
841 dump_stack();
842 local_irq_restore(flags);
843 return -EBUSY;
844 }
845
846 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
847
848 u16 funct = get_portmux(ident);
849
850 /*
851 * Pin functions like AMC address strobes my
852 * be requested and used by several drivers
853 */
854
855 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
856
857 /*
858 * Allow that the identical pin function can
859 * be requested from the same driver twice
860 */
861
862 if (cmp_label(ident, label) == 0)
863 goto anyway;
864
865 printk(KERN_ERR
866 "%s: Peripheral %d function %d is already reserved by %s !\n",
867 __FUNCTION__, ident, P_FUNCT2MUX(per), get_label(ident));
868 dump_stack();
869 local_irq_restore(flags);
870 return -EBUSY;
871 }
872 }
873
874anyway:
875 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
876
877 portmux_setup(ident, P_FUNCT2MUX(per));
878 port_setup(ident, PERIPHERAL_USAGE);
879
880 local_irq_restore(flags);
881 set_label(ident, label);
882
883 return 0;
884}
885EXPORT_SYMBOL(peripheral_request);
886#else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800887
888int peripheral_request(unsigned short per, const char *label)
889{
890 unsigned long flags;
891 unsigned short ident = P_IDENT(per);
892
893 /*
894 * Don't cares are pins with only one dedicated function
895 */
896
897 if (per & P_DONTCARE)
898 return 0;
899
900 if (!(per & P_DEFINED))
901 return -ENODEV;
902
Michael Hennerichc58c2142007-10-04 00:35:05 +0800903 local_irq_save(flags);
904
Michael Hennerichcda6a202007-10-04 00:36:18 +0800905 if (!check_gpio(ident)) {
906
Michael Hennerichc58c2142007-10-04 00:35:05 +0800907 if (unlikely(reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
908 printk(KERN_ERR
909 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
910 __FUNCTION__, ident, get_label(ident));
911 dump_stack();
912 local_irq_restore(flags);
913 return -EBUSY;
914 }
915
Michael Hennerichcda6a202007-10-04 00:36:18 +0800916 }
917
Michael Hennerichc58c2142007-10-04 00:35:05 +0800918 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
919
920 /*
921 * Pin functions like AMC address strobes my
922 * be requested and used by several drivers
923 */
924
925 if (!(per & P_MAYSHARE)) {
926
927 /*
928 * Allow that the identical pin function can
929 * be requested from the same driver twice
930 */
931
932 if (cmp_label(ident, label) == 0)
933 goto anyway;
934
935 printk(KERN_ERR
936 "%s: Peripheral %d function %d is already"
Michael Hennerich8c613622007-08-03 17:48:09 +0800937 " reserved by %s !\n",
Michael Hennerichc58c2142007-10-04 00:35:05 +0800938 __FUNCTION__, ident, P_FUNCT2MUX(per),
939 get_label(ident));
940 dump_stack();
941 local_irq_restore(flags);
942 return -EBUSY;
943 }
944
945 }
946
947anyway:
Michael Hennerichc58c2142007-10-04 00:35:05 +0800948 portmux_setup(per, P_FUNCT2MUX(per));
949
950 port_setup(ident, PERIPHERAL_USAGE);
951
952 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
953 local_irq_restore(flags);
954 set_label(ident, label);
955
956 return 0;
957}
958EXPORT_SYMBOL(peripheral_request);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800959#endif
Michael Hennerichc58c2142007-10-04 00:35:05 +0800960
961int peripheral_request_list(unsigned short per[], const char *label)
962{
963 u16 cnt;
964 int ret;
965
966 for (cnt = 0; per[cnt] != 0; cnt++) {
Michael Hennerich314c98d2007-07-24 18:03:45 +0800967
Michael Hennerichc58c2142007-10-04 00:35:05 +0800968 ret = peripheral_request(per[cnt], label);
Michael Hennerich314c98d2007-07-24 18:03:45 +0800969
970 if (ret < 0) {
971 for ( ; cnt > 0; cnt--) {
972 peripheral_free(per[cnt - 1]);
973 }
974 return ret;
975 }
Michael Hennerichc58c2142007-10-04 00:35:05 +0800976 }
977
978 return 0;
979}
980EXPORT_SYMBOL(peripheral_request_list);
981
982void peripheral_free(unsigned short per)
983{
984 unsigned long flags;
985 unsigned short ident = P_IDENT(per);
986
987 if (per & P_DONTCARE)
988 return;
989
990 if (!(per & P_DEFINED))
991 return;
992
993 if (check_gpio(ident) < 0)
994 return;
995
996 local_irq_save(flags);
997
998 if (unlikely(!(reserved_peri_map[gpio_bank(ident)]
999 & gpio_bit(ident)))) {
1000 local_irq_restore(flags);
1001 return;
1002 }
1003
1004 if (!(per & P_MAYSHARE)) {
1005 port_setup(ident, GPIO_USAGE);
1006 }
1007
1008 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
1009
Michael Hennerich2acde902007-10-11 00:24:40 +08001010 set_label(ident, "free");
1011
Michael Hennerichc58c2142007-10-04 00:35:05 +08001012 local_irq_restore(flags);
1013}
1014EXPORT_SYMBOL(peripheral_free);
1015
1016void peripheral_free_list(unsigned short per[])
1017{
1018 u16 cnt;
1019
1020 for (cnt = 0; per[cnt] != 0; cnt++) {
1021 peripheral_free(per[cnt]);
1022 }
1023
1024}
1025EXPORT_SYMBOL(peripheral_free_list);
1026
Bryan Wu1394f032007-05-06 14:50:22 -07001027/***********************************************************
1028*
1029* FUNCTIONS: Blackfin GPIO Driver
1030*
1031* INPUTS/OUTPUTS:
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001032* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1033* label String
Bryan Wu1394f032007-05-06 14:50:22 -07001034*
1035* DESCRIPTION: Blackfin GPIO Driver API
1036*
1037* CAUTION:
1038*************************************************************
1039* MODIFICATION HISTORY :
1040**************************************************************/
1041
Michael Hennerichacbcd262008-01-22 18:36:20 +08001042int gpio_request(unsigned gpio, const char *label)
Bryan Wu1394f032007-05-06 14:50:22 -07001043{
1044 unsigned long flags;
1045
1046 if (check_gpio(gpio) < 0)
1047 return -EINVAL;
1048
1049 local_irq_save(flags);
1050
Michael Hennerich2acde902007-10-11 00:24:40 +08001051 /*
1052 * Allow that the identical GPIO can
1053 * be requested from the same driver twice
1054 * Do nothing and return -
1055 */
1056
1057 if (cmp_label(gpio, label) == 0) {
1058 local_irq_restore(flags);
1059 return 0;
1060 }
1061
Michael Hennerichc58c2142007-10-04 00:35:05 +08001062 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001063 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1064 gpio, get_label(gpio));
Bryan Wu1394f032007-05-06 14:50:22 -07001065 dump_stack();
1066 local_irq_restore(flags);
1067 return -EBUSY;
1068 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001069 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1070 printk(KERN_ERR
1071 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1072 gpio, get_label(gpio));
1073 dump_stack();
1074 local_irq_restore(flags);
1075 return -EBUSY;
1076 }
1077
Michael Hennerichc58c2142007-10-04 00:35:05 +08001078 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001079
1080 local_irq_restore(flags);
1081
1082 port_setup(gpio, GPIO_USAGE);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001083 set_label(gpio, label);
Bryan Wu1394f032007-05-06 14:50:22 -07001084
1085 return 0;
1086}
1087EXPORT_SYMBOL(gpio_request);
1088
Michael Hennerichacbcd262008-01-22 18:36:20 +08001089void gpio_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001090{
1091 unsigned long flags;
1092
1093 if (check_gpio(gpio) < 0)
1094 return;
1095
1096 local_irq_save(flags);
1097
Michael Hennerichc58c2142007-10-04 00:35:05 +08001098 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
Michael Hennerichacbcd262008-01-22 18:36:20 +08001099 gpio_error(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001100 dump_stack();
1101 local_irq_restore(flags);
1102 return;
1103 }
1104
1105 default_gpio(gpio);
1106
Michael Hennerichc58c2142007-10-04 00:35:05 +08001107 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001108
Michael Hennerich2acde902007-10-11 00:24:40 +08001109 set_label(gpio, "free");
1110
Bryan Wu1394f032007-05-06 14:50:22 -07001111 local_irq_restore(flags);
1112}
1113EXPORT_SYMBOL(gpio_free);
1114
Michael Hennerichacbcd262008-01-22 18:36:20 +08001115
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001116#ifdef BF548_FAMILY
Michael Hennerichacbcd262008-01-22 18:36:20 +08001117int gpio_direction_input(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001118{
1119 unsigned long flags;
1120
Michael Hennerichacbcd262008-01-22 18:36:20 +08001121 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1122 gpio_error(gpio);
1123 return -EINVAL;
1124 }
1125
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001126
1127 local_irq_save(flags);
1128 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1129 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1130 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001131
1132 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001133}
1134EXPORT_SYMBOL(gpio_direction_input);
1135
Michael Hennerichacbcd262008-01-22 18:36:20 +08001136int gpio_direction_output(unsigned gpio, int value)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001137{
1138 unsigned long flags;
1139
Michael Hennerichacbcd262008-01-22 18:36:20 +08001140 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1141 gpio_error(gpio);
1142 return -EINVAL;
1143 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001144
1145 local_irq_save(flags);
1146 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001147 gpio_set_value(gpio, value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001148 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1149 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001150
1151 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001152}
1153EXPORT_SYMBOL(gpio_direction_output);
1154
Michael Hennerichacbcd262008-01-22 18:36:20 +08001155void gpio_set_value(unsigned gpio, int arg)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001156{
1157 if (arg)
1158 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1159 else
1160 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
1161
1162}
1163EXPORT_SYMBOL(gpio_set_value);
1164
Michael Hennerichacbcd262008-01-22 18:36:20 +08001165int gpio_get_value(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001166{
1167 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1168}
1169EXPORT_SYMBOL(gpio_get_value);
1170
1171#else
1172
Michael Hennerichacbcd262008-01-22 18:36:20 +08001173int gpio_direction_input(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001174{
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 }
Bryan Wu1394f032007-05-06 14:50:22 -07001181
1182 local_irq_save(flags);
1183 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1184 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001185 AWA_DUMMY_READ(inen);
Bryan Wu1394f032007-05-06 14:50:22 -07001186 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001187
1188 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001189}
1190EXPORT_SYMBOL(gpio_direction_input);
1191
Michael Hennerichacbcd262008-01-22 18:36:20 +08001192int gpio_direction_output(unsigned gpio, int value)
Bryan Wu1394f032007-05-06 14:50:22 -07001193{
1194 unsigned long flags;
1195
Michael Hennerichacbcd262008-01-22 18:36:20 +08001196 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1197 gpio_error(gpio);
1198 return -EINVAL;
1199 }
Bryan Wu1394f032007-05-06 14:50:22 -07001200
1201 local_irq_save(flags);
1202 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001203 gpio_set_value(gpio, value);
Bryan Wu1394f032007-05-06 14:50:22 -07001204 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001205 AWA_DUMMY_READ(dir);
Bryan Wu1394f032007-05-06 14:50:22 -07001206 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001207
1208 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001209}
1210EXPORT_SYMBOL(gpio_direction_output);
Mike Frysinger168f1212007-10-11 00:22:35 +08001211
1212/* If we are booting from SPI and our board lacks a strong enough pull up,
1213 * the core can reset and execute the bootrom faster than the resistor can
1214 * pull the signal logically high. To work around this (common) error in
1215 * board design, we explicitly set the pin back to GPIO mode, force /CS
1216 * high, and wait for the electrons to do their thing.
1217 *
1218 * This function only makes sense to be called from reset code, but it
1219 * lives here as we need to force all the GPIO states w/out going through
1220 * BUG() checks and such.
1221 */
1222void bfin_gpio_reset_spi0_ssel1(void)
1223{
Michael Hennerich4d5f4ed2007-08-27 16:46:17 +08001224 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1225
1226 port_setup(gpio, GPIO_USAGE);
1227 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
Mike Frysinger168f1212007-10-11 00:22:35 +08001228 udelay(1);
1229}
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001230
1231#endif /*BF548_FAMILY */
Mike Frysinger1545a112007-12-24 16:54:48 +08001232
1233#if defined(CONFIG_PROC_FS)
1234static int gpio_proc_read(char *buf, char **start, off_t offset,
1235 int len, int *unused_i, void *unused_v)
1236{
1237 int c, outlen = 0;
1238
1239 for (c = 0; c < MAX_RESOURCES; c++) {
1240 if (!check_gpio(c) && (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c)))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001241 len = sprintf(buf, "GPIO_%d: %s \t\tGPIO %s\n", c,
Mike Frysinger1545a112007-12-24 16:54:48 +08001242 get_label(c), get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1243 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001244 len = sprintf(buf, "GPIO_%d: %s \t\tPeripheral\n", c, get_label(c));
Mike Frysinger1545a112007-12-24 16:54:48 +08001245 else
1246 continue;
1247 buf += len;
1248 outlen += len;
1249 }
1250 return outlen;
1251}
1252
1253static __init int gpio_register_proc(void)
1254{
1255 struct proc_dir_entry *proc_gpio;
1256
1257 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1258 if (proc_gpio)
1259 proc_gpio->read_proc = gpio_proc_read;
1260 return proc_gpio != NULL;
1261}
1262
1263__initcall(gpio_register_proc);
1264#endif