blob: 6939272e9ed410dadfb7060de33ca997d2833666 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
2 * File: arch/blackfin/kernel/bfin_gpio.c
3 * Based on:
4 * Author: Michael Hennerich (hennerich@blackfin.uclinux.org)
5 *
6 * Created:
7 * Description: GPIO Abstraction Layer
8 *
9 * Modified:
Michael Hennericha2c8cfe2008-01-22 17:20:10 +080010 * Copyright 2008 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -070011 *
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29
30/*
Michael Hennerichd2b11a42007-08-28 16:47:46 +080031* Number BF537/6/4 BF561 BF533/2/1 BF549/8/4/2
Bryan Wu1394f032007-05-06 14:50:22 -070032*
Michael Hennerichd2b11a42007-08-28 16:47:46 +080033* GPIO_0 PF0 PF0 PF0 PA0...PJ13
Bryan Wu1394f032007-05-06 14:50:22 -070034* GPIO_1 PF1 PF1 PF1
35* GPIO_2 PF2 PF2 PF2
36* GPIO_3 PF3 PF3 PF3
37* GPIO_4 PF4 PF4 PF4
38* GPIO_5 PF5 PF5 PF5
39* GPIO_6 PF6 PF6 PF6
40* GPIO_7 PF7 PF7 PF7
41* GPIO_8 PF8 PF8 PF8
42* GPIO_9 PF9 PF9 PF9
43* GPIO_10 PF10 PF10 PF10
44* GPIO_11 PF11 PF11 PF11
45* GPIO_12 PF12 PF12 PF12
46* GPIO_13 PF13 PF13 PF13
47* GPIO_14 PF14 PF14 PF14
48* GPIO_15 PF15 PF15 PF15
49* GPIO_16 PG0 PF16
50* GPIO_17 PG1 PF17
51* GPIO_18 PG2 PF18
52* GPIO_19 PG3 PF19
53* GPIO_20 PG4 PF20
54* GPIO_21 PG5 PF21
55* GPIO_22 PG6 PF22
56* GPIO_23 PG7 PF23
57* GPIO_24 PG8 PF24
58* GPIO_25 PG9 PF25
59* GPIO_26 PG10 PF26
60* GPIO_27 PG11 PF27
61* GPIO_28 PG12 PF28
62* GPIO_29 PG13 PF29
63* GPIO_30 PG14 PF30
64* GPIO_31 PG15 PF31
65* GPIO_32 PH0 PF32
66* GPIO_33 PH1 PF33
67* GPIO_34 PH2 PF34
68* GPIO_35 PH3 PF35
69* GPIO_36 PH4 PF36
70* GPIO_37 PH5 PF37
71* GPIO_38 PH6 PF38
72* GPIO_39 PH7 PF39
73* GPIO_40 PH8 PF40
74* GPIO_41 PH9 PF41
75* GPIO_42 PH10 PF42
76* GPIO_43 PH11 PF43
77* GPIO_44 PH12 PF44
78* GPIO_45 PH13 PF45
79* GPIO_46 PH14 PF46
80* GPIO_47 PH15 PF47
81*/
82
Mike Frysinger168f1212007-10-11 00:22:35 +080083#include <linux/delay.h>
Bryan Wu1394f032007-05-06 14:50:22 -070084#include <linux/module.h>
85#include <linux/err.h>
Mike Frysinger1545a112007-12-24 16:54:48 +080086#include <linux/proc_fs.h>
Bryan Wu1394f032007-05-06 14:50:22 -070087#include <asm/blackfin.h>
88#include <asm/gpio.h>
Michael Hennerichc58c2142007-10-04 00:35:05 +080089#include <asm/portmux.h>
Bryan Wu1394f032007-05-06 14:50:22 -070090#include <linux/irq.h>
91
Michael Hennerich2b393312007-10-10 16:58:49 +080092#if ANOMALY_05000311 || ANOMALY_05000323
93enum {
94 AWA_data = SYSCR,
95 AWA_data_clear = SYSCR,
96 AWA_data_set = SYSCR,
97 AWA_toggle = SYSCR,
Graf Yang6ed83942008-04-24 04:43:14 +080098 AWA_maska = BFIN_UART_SCR,
99 AWA_maska_clear = BFIN_UART_SCR,
100 AWA_maska_set = BFIN_UART_SCR,
101 AWA_maska_toggle = BFIN_UART_SCR,
102 AWA_maskb = BFIN_UART_GCTL,
103 AWA_maskb_clear = BFIN_UART_GCTL,
104 AWA_maskb_set = BFIN_UART_GCTL,
105 AWA_maskb_toggle = BFIN_UART_GCTL,
Michael Hennerich2b393312007-10-10 16:58:49 +0800106 AWA_dir = SPORT1_STAT,
107 AWA_polar = SPORT1_STAT,
108 AWA_edge = SPORT1_STAT,
109 AWA_both = SPORT1_STAT,
110#if ANOMALY_05000311
111 AWA_inen = TIMER_ENABLE,
112#elif ANOMALY_05000323
113 AWA_inen = DMA1_1_CONFIG,
114#endif
115};
116 /* Anomaly Workaround */
117#define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
118#else
119#define AWA_DUMMY_READ(...) do { } while (0)
120#endif
121
Michael Hennerichdc26aec2008-11-18 17:48:22 +0800122#if defined(BF533_FAMILY) || defined(BF538_FAMILY)
Bryan Wu397861c2008-11-18 17:48:22 +0800123static struct gpio_port_t *gpio_bankb[] = {
Bryan Wu1394f032007-05-06 14:50:22 -0700124 (struct gpio_port_t *) FIO_FLAG_D,
125};
126#endif
127
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800128#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Bryan Wu397861c2008-11-18 17:48:22 +0800129static struct gpio_port_t *gpio_bankb[] = {
Bryan Wu1394f032007-05-06 14:50:22 -0700130 (struct gpio_port_t *) PORTFIO,
131 (struct gpio_port_t *) PORTGIO,
132 (struct gpio_port_t *) PORTHIO,
133};
134
Bryan Wu397861c2008-11-18 17:48:22 +0800135static unsigned short *port_fer[] = {
Bryan Wu1394f032007-05-06 14:50:22 -0700136 (unsigned short *) PORTF_FER,
137 (unsigned short *) PORTG_FER,
138 (unsigned short *) PORTH_FER,
139};
Bryan Wu1394f032007-05-06 14:50:22 -0700140#endif
141
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800142#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Bryan Wu397861c2008-11-18 17:48:22 +0800143static unsigned short *port_mux[] = {
Michael Hennerich59003142007-10-21 16:54:27 +0800144 (unsigned short *) PORTF_MUX,
145 (unsigned short *) PORTG_MUX,
146 (unsigned short *) PORTH_MUX,
147};
148
149static const
150u8 pmux_offset[][16] =
151 {{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
152 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
153 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
154 };
155#endif
156
Bryan Wu1394f032007-05-06 14:50:22 -0700157#ifdef BF561_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800158static struct gpio_port_t *gpio_bankb[] = {
Bryan Wu1394f032007-05-06 14:50:22 -0700159 (struct gpio_port_t *) FIO0_FLAG_D,
160 (struct gpio_port_t *) FIO1_FLAG_D,
161 (struct gpio_port_t *) FIO2_FLAG_D,
162};
163#endif
164
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800165#ifdef BF548_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800166static struct gpio_port_t *gpio_array[] = {
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800167 (struct gpio_port_t *)PORTA_FER,
168 (struct gpio_port_t *)PORTB_FER,
169 (struct gpio_port_t *)PORTC_FER,
170 (struct gpio_port_t *)PORTD_FER,
171 (struct gpio_port_t *)PORTE_FER,
172 (struct gpio_port_t *)PORTF_FER,
173 (struct gpio_port_t *)PORTG_FER,
174 (struct gpio_port_t *)PORTH_FER,
175 (struct gpio_port_t *)PORTI_FER,
176 (struct gpio_port_t *)PORTJ_FER,
177};
178#endif
179
Bryan Wu397861c2008-11-18 17:48:22 +0800180static unsigned short reserved_gpio_map[GPIO_BANK_NUM];
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800181static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
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)
Bryan Wu397861c2008-11-18 17:48:22 +0800191static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800192#else
Bryan Wu397861c2008-11-18 17:48:22 +0800193static unsigned short wakeup_map[GPIO_BANK_NUM];
Bryan Wu1394f032007-05-06 14:50:22 -0700194static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];
Bryan Wu397861c2008-11-18 17:48:22 +0800195static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
Bryan Wu1394f032007-05-06 14:50:22 -0700196
197#ifdef BF533_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800198static unsigned int sic_iwr_irqs[] = {IRQ_PROG_INTB};
Bryan Wu1394f032007-05-06 14:50:22 -0700199#endif
200
201#ifdef BF537_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800202static unsigned int sic_iwr_irqs[] = {IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX};
Bryan Wu1394f032007-05-06 14:50:22 -0700203#endif
204
Michael Hennerichdc26aec2008-11-18 17:48:22 +0800205#ifdef BF538_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800206static unsigned int sic_iwr_irqs[] = {IRQ_PORTF_INTB};
Michael Hennerichdc26aec2008-11-18 17:48:22 +0800207#endif
208
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800209#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Bryan Wu397861c2008-11-18 17:48:22 +0800210static unsigned int sic_iwr_irqs[] = {IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB};
Michael Hennerich59003142007-10-21 16:54:27 +0800211#endif
212
Bryan Wu1394f032007-05-06 14:50:22 -0700213#ifdef BF561_FAMILY
Bryan Wu397861c2008-11-18 17:48:22 +0800214static unsigned int sic_iwr_irqs[] = {IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB};
Bryan Wu1394f032007-05-06 14:50:22 -0700215#endif
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800216#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700217#endif /* CONFIG_PM */
218
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800219inline int check_gpio(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800220{
Mike Frysinger27228b22008-10-28 15:45:42 +0800221#if defined(BF548_FAMILY)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800222 if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
223 || gpio == GPIO_PH14 || gpio == GPIO_PH15
Mike Frysinger27228b22008-10-28 15:45:42 +0800224 || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800225 return -EINVAL;
Mike Frysinger27228b22008-10-28 15:45:42 +0800226#endif
Michael Henneriche7613aa2007-06-11 16:37:57 +0800227 if (gpio >= MAX_BLACKFIN_GPIOS)
Bryan Wu1394f032007-05-06 14:50:22 -0700228 return -EINVAL;
229 return 0;
230}
231
Mike Frysinger74c04502008-10-08 16:13:17 +0800232static void gpio_error(unsigned gpio)
Michael Hennerichacbcd262008-01-22 18:36:20 +0800233{
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{
Michael Henneriche9fae182008-10-13 11:35:22 +0800239 if (label) {
Michael Hennerich8c613622007-08-03 17:48:09 +0800240 strncpy(str_ident[ident].name, label,
Michael Hennerichc58c2142007-10-04 00:35:05 +0800241 RESOURCE_LABEL_SIZE);
Michael Hennerich8c613622007-08-03 17:48:09 +0800242 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800243 }
244}
245
246static char *get_label(unsigned short ident)
247{
Michael Hennerich8c613622007-08-03 17:48:09 +0800248 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
Michael Hennerichc58c2142007-10-04 00:35:05 +0800249}
250
251static int cmp_label(unsigned short ident, const char *label)
252{
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800253 if (label == NULL) {
254 dump_stack();
255 printk(KERN_ERR "Please provide none-null label\n");
256 }
257
Michael Henneriche9fae182008-10-13 11:35:22 +0800258 if (label)
Mike Frysinger1f7d3732008-10-28 15:47:11 +0800259 return strcmp(str_ident[ident].name, label);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800260 else
261 return -EINVAL;
262}
263
Mike Frysingera2d03a12008-10-28 15:53:37 +0800264static void port_setup(unsigned gpio, unsigned short usage)
265{
266 if (check_gpio(gpio))
267 return;
268
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800269#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Mike Frysingera2d03a12008-10-28 15:53:37 +0800270 if (usage == GPIO_USAGE)
271 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
272 else
273 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
274 SSYNC();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800275#elif defined(BF548_FAMILY)
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800276 if (usage == GPIO_USAGE)
277 gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
278 else
279 gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
280 SSYNC();
Bryan Wu1394f032007-05-06 14:50:22 -0700281#endif
Mike Frysingera2d03a12008-10-28 15:53:37 +0800282}
Bryan Wu1394f032007-05-06 14:50:22 -0700283
Michael Hennerichc58c2142007-10-04 00:35:05 +0800284#ifdef BF537_FAMILY
Michael Hennerich8c613622007-08-03 17:48:09 +0800285static struct {
286 unsigned short res;
287 unsigned short offset;
288} port_mux_lut[] = {
289 {.res = P_PPI0_D13, .offset = 11},
290 {.res = P_PPI0_D14, .offset = 11},
291 {.res = P_PPI0_D15, .offset = 11},
292 {.res = P_SPORT1_TFS, .offset = 11},
293 {.res = P_SPORT1_TSCLK, .offset = 11},
294 {.res = P_SPORT1_DTPRI, .offset = 11},
295 {.res = P_PPI0_D10, .offset = 10},
296 {.res = P_PPI0_D11, .offset = 10},
297 {.res = P_PPI0_D12, .offset = 10},
298 {.res = P_SPORT1_RSCLK, .offset = 10},
299 {.res = P_SPORT1_RFS, .offset = 10},
300 {.res = P_SPORT1_DRPRI, .offset = 10},
301 {.res = P_PPI0_D8, .offset = 9},
302 {.res = P_PPI0_D9, .offset = 9},
303 {.res = P_SPORT1_DRSEC, .offset = 9},
304 {.res = P_SPORT1_DTSEC, .offset = 9},
305 {.res = P_TMR2, .offset = 8},
306 {.res = P_PPI0_FS3, .offset = 8},
307 {.res = P_TMR3, .offset = 7},
308 {.res = P_SPI0_SSEL4, .offset = 7},
309 {.res = P_TMR4, .offset = 6},
310 {.res = P_SPI0_SSEL5, .offset = 6},
311 {.res = P_TMR5, .offset = 5},
312 {.res = P_SPI0_SSEL6, .offset = 5},
313 {.res = P_UART1_RX, .offset = 4},
314 {.res = P_UART1_TX, .offset = 4},
315 {.res = P_TMR6, .offset = 4},
316 {.res = P_TMR7, .offset = 4},
317 {.res = P_UART0_RX, .offset = 3},
318 {.res = P_UART0_TX, .offset = 3},
319 {.res = P_DMAR0, .offset = 3},
320 {.res = P_DMAR1, .offset = 3},
321 {.res = P_SPORT0_DTSEC, .offset = 1},
322 {.res = P_SPORT0_DRSEC, .offset = 1},
323 {.res = P_CAN0_RX, .offset = 1},
324 {.res = P_CAN0_TX, .offset = 1},
325 {.res = P_SPI0_SSEL7, .offset = 1},
326 {.res = P_SPORT0_TFS, .offset = 0},
327 {.res = P_SPORT0_DTPRI, .offset = 0},
328 {.res = P_SPI0_SSEL2, .offset = 0},
329 {.res = P_SPI0_SSEL3, .offset = 0},
Michael Hennerichc58c2142007-10-04 00:35:05 +0800330};
331
332static void portmux_setup(unsigned short per, unsigned short function)
333{
Michael Hennerich8c613622007-08-03 17:48:09 +0800334 u16 y, offset, muxreg;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800335
Michael Hennerich8c613622007-08-03 17:48:09 +0800336 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
337 if (port_mux_lut[y].res == per) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800338
339 /* SET PORTMUX REG */
340
Michael Hennerich8c613622007-08-03 17:48:09 +0800341 offset = port_mux_lut[y].offset;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800342 muxreg = bfin_read_PORT_MUX();
343
Mike Frysingerd171c232008-03-26 08:35:46 +0800344 if (offset != 1)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800345 muxreg &= ~(1 << offset);
Mike Frysingerd171c232008-03-26 08:35:46 +0800346 else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800347 muxreg &= ~(3 << 1);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800348
349 muxreg |= (function << offset);
350 bfin_write_PORT_MUX(muxreg);
351 }
352 }
353}
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800354#elif defined(BF548_FAMILY)
355inline void portmux_setup(unsigned short portno, unsigned short function)
356{
357 u32 pmux;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800358
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800359 pmux = gpio_array[gpio_bank(portno)]->port_mux;
360
361 pmux &= ~(0x3 << (2 * gpio_sub_n(portno)));
362 pmux |= (function & 0x3) << (2 * gpio_sub_n(portno));
363
364 gpio_array[gpio_bank(portno)]->port_mux = pmux;
365}
366
367inline u16 get_portmux(unsigned short portno)
368{
369 u32 pmux;
370
371 pmux = gpio_array[gpio_bank(portno)]->port_mux;
372
373 return (pmux >> (2 * gpio_sub_n(portno)) & 0x3);
374}
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800375#elif defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich59003142007-10-21 16:54:27 +0800376inline void portmux_setup(unsigned short portno, unsigned short function)
377{
378 u16 pmux, ident = P_IDENT(portno);
379 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
380
381 pmux = *port_mux[gpio_bank(ident)];
382 pmux &= ~(3 << offset);
383 pmux |= (function & 3) << offset;
384 *port_mux[gpio_bank(ident)] = pmux;
385 SSYNC();
386}
Michael Hennerichc58c2142007-10-04 00:35:05 +0800387#else
388# define portmux_setup(...) do { } while (0)
389#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700390
Mike Frysingera161bb02007-05-21 18:09:14 +0800391static int __init bfin_gpio_init(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700392{
Bryan Wu1394f032007-05-06 14:50:22 -0700393 printk(KERN_INFO "Blackfin GPIO Controller\n");
394
Bryan Wu1394f032007-05-06 14:50:22 -0700395 return 0;
396}
Bryan Wu1394f032007-05-06 14:50:22 -0700397arch_initcall(bfin_gpio_init);
398
399
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800400#ifndef BF548_FAMILY
Bryan Wu1394f032007-05-06 14:50:22 -0700401/***********************************************************
402*
403* FUNCTIONS: Blackfin General Purpose Ports Access Functions
404*
405* INPUTS/OUTPUTS:
406* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
407*
408*
409* DESCRIPTION: These functions abstract direct register access
410* to Blackfin processor General Purpose
411* Ports Regsiters
412*
413* CAUTION: These functions do not belong to the GPIO Driver API
414*************************************************************
415* MODIFICATION HISTORY :
416**************************************************************/
417
418/* Set a specific bit */
419
420#define SET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800421void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700422{ \
423 unsigned long flags; \
Bryan Wu1394f032007-05-06 14:50:22 -0700424 local_irq_save(flags); \
425 if (arg) \
426 gpio_bankb[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
427 else \
428 gpio_bankb[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
Michael Hennerich2b393312007-10-10 16:58:49 +0800429 AWA_DUMMY_READ(name); \
Bryan Wu1394f032007-05-06 14:50:22 -0700430 local_irq_restore(flags); \
431} \
432EXPORT_SYMBOL(set_gpio_ ## name);
433
434SET_GPIO(dir)
435SET_GPIO(inen)
436SET_GPIO(polar)
437SET_GPIO(edge)
438SET_GPIO(both)
439
440
Michael Hennerich2b393312007-10-10 16:58:49 +0800441#if ANOMALY_05000311 || ANOMALY_05000323
442#define SET_GPIO_SC(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800443void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800444{ \
445 unsigned long flags; \
Michael Hennerich2b393312007-10-10 16:58:49 +0800446 local_irq_save(flags); \
447 if (arg) \
448 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
449 else \
450 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
451 AWA_DUMMY_READ(name); \
452 local_irq_restore(flags); \
453} \
454EXPORT_SYMBOL(set_gpio_ ## name);
455#else
Bryan Wu1394f032007-05-06 14:50:22 -0700456#define SET_GPIO_SC(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{ \
Bryan Wu1394f032007-05-06 14:50:22 -0700459 if (arg) \
460 gpio_bankb[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
461 else \
462 gpio_bankb[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
463} \
464EXPORT_SYMBOL(set_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800465#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700466
467SET_GPIO_SC(maska)
468SET_GPIO_SC(maskb)
Bryan Wu1394f032007-05-06 14:50:22 -0700469SET_GPIO_SC(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700470
Michael Hennerich2b393312007-10-10 16:58:49 +0800471#if ANOMALY_05000311 || ANOMALY_05000323
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800472void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700473{
474 unsigned long flags;
Bryan Wu1394f032007-05-06 14:50:22 -0700475 local_irq_save(flags);
476 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +0800477 AWA_DUMMY_READ(toggle);
Bryan Wu1394f032007-05-06 14:50:22 -0700478 local_irq_restore(flags);
479}
480#else
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800481void set_gpio_toggle(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700482{
Bryan Wu1394f032007-05-06 14:50:22 -0700483 gpio_bankb[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
484}
485#endif
486EXPORT_SYMBOL(set_gpio_toggle);
487
488
489/*Set current PORT date (16-bit word)*/
490
Michael Hennerich2b393312007-10-10 16:58:49 +0800491#if ANOMALY_05000311 || ANOMALY_05000323
492#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800493void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800494{ \
495 unsigned long flags; \
496 local_irq_save(flags); \
497 gpio_bankb[gpio_bank(gpio)]->name = arg; \
498 AWA_DUMMY_READ(name); \
499 local_irq_restore(flags); \
500} \
501EXPORT_SYMBOL(set_gpiop_ ## name);
502#else
Bryan Wu1394f032007-05-06 14:50:22 -0700503#define SET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800504void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu1394f032007-05-06 14:50:22 -0700505{ \
506 gpio_bankb[gpio_bank(gpio)]->name = arg; \
507} \
508EXPORT_SYMBOL(set_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800509#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700510
Michael Hennerich2b393312007-10-10 16:58:49 +0800511SET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700512SET_GPIO_P(dir)
513SET_GPIO_P(inen)
514SET_GPIO_P(polar)
515SET_GPIO_P(edge)
516SET_GPIO_P(both)
517SET_GPIO_P(maska)
518SET_GPIO_P(maskb)
519
Bryan Wu1394f032007-05-06 14:50:22 -0700520/* Get a specific bit */
Michael Hennerich2b393312007-10-10 16:58:49 +0800521#if ANOMALY_05000311 || ANOMALY_05000323
522#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800523unsigned short get_gpio_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800524{ \
525 unsigned long flags; \
526 unsigned short ret; \
527 local_irq_save(flags); \
528 ret = 0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
529 AWA_DUMMY_READ(name); \
530 local_irq_restore(flags); \
531 return ret; \
532} \
533EXPORT_SYMBOL(get_gpio_ ## name);
534#else
Bryan Wu1394f032007-05-06 14:50:22 -0700535#define GET_GPIO(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800536unsigned short get_gpio_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700537{ \
538 return (0x01 & (gpio_bankb[gpio_bank(gpio)]->name >> gpio_sub_n(gpio))); \
539} \
540EXPORT_SYMBOL(get_gpio_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800541#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700542
Michael Hennerich2b393312007-10-10 16:58:49 +0800543GET_GPIO(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700544GET_GPIO(dir)
545GET_GPIO(inen)
546GET_GPIO(polar)
547GET_GPIO(edge)
548GET_GPIO(both)
549GET_GPIO(maska)
550GET_GPIO(maskb)
551
Bryan Wu1394f032007-05-06 14:50:22 -0700552/*Get current PORT date (16-bit word)*/
553
Michael Hennerich2b393312007-10-10 16:58:49 +0800554#if ANOMALY_05000311 || ANOMALY_05000323
555#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800556unsigned short get_gpiop_ ## name(unsigned gpio) \
Michael Hennerich2b393312007-10-10 16:58:49 +0800557{ \
558 unsigned long flags; \
559 unsigned short ret; \
560 local_irq_save(flags); \
561 ret = (gpio_bankb[gpio_bank(gpio)]->name); \
562 AWA_DUMMY_READ(name); \
563 local_irq_restore(flags); \
564 return ret; \
565} \
566EXPORT_SYMBOL(get_gpiop_ ## name);
567#else
Bryan Wu1394f032007-05-06 14:50:22 -0700568#define GET_GPIO_P(name) \
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800569unsigned short get_gpiop_ ## name(unsigned gpio) \
Bryan Wu1394f032007-05-06 14:50:22 -0700570{ \
571 return (gpio_bankb[gpio_bank(gpio)]->name);\
572} \
573EXPORT_SYMBOL(get_gpiop_ ## name);
Michael Hennerich2b393312007-10-10 16:58:49 +0800574#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700575
Michael Hennerich2b393312007-10-10 16:58:49 +0800576GET_GPIO_P(data)
Bryan Wu1394f032007-05-06 14:50:22 -0700577GET_GPIO_P(dir)
578GET_GPIO_P(inen)
579GET_GPIO_P(polar)
580GET_GPIO_P(edge)
581GET_GPIO_P(both)
582GET_GPIO_P(maska)
583GET_GPIO_P(maskb)
584
Bryan Wu1394f032007-05-06 14:50:22 -0700585
586#ifdef CONFIG_PM
587/***********************************************************
588*
589* FUNCTIONS: Blackfin PM Setup API
590*
591* INPUTS/OUTPUTS:
592* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
593* type -
594* PM_WAKE_RISING
595* PM_WAKE_FALLING
596* PM_WAKE_HIGH
597* PM_WAKE_LOW
598* PM_WAKE_BOTH_EDGES
599*
600* DESCRIPTION: Blackfin PM Driver API
601*
602* CAUTION:
603*************************************************************
604* MODIFICATION HISTORY :
605**************************************************************/
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800606int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700607{
608 unsigned long flags;
609
610 if ((check_gpio(gpio) < 0) || !type)
611 return -EINVAL;
612
613 local_irq_save(flags);
Bryan Wu1394f032007-05-06 14:50:22 -0700614 wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
615 wakeup_flags_map[gpio] = type;
616 local_irq_restore(flags);
617
618 return 0;
619}
620EXPORT_SYMBOL(gpio_pm_wakeup_request);
621
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800622void gpio_pm_wakeup_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -0700623{
624 unsigned long flags;
625
626 if (check_gpio(gpio) < 0)
627 return;
628
629 local_irq_save(flags);
630
631 wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
632
633 local_irq_restore(flags);
634}
635EXPORT_SYMBOL(gpio_pm_wakeup_free);
636
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800637static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
Bryan Wu1394f032007-05-06 14:50:22 -0700638{
639 port_setup(gpio, GPIO_USAGE);
640 set_gpio_dir(gpio, 0);
641 set_gpio_inen(gpio, 1);
642
643 if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
644 set_gpio_edge(gpio, 1);
645 else
646 set_gpio_edge(gpio, 0);
647
648 if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
649 set_gpio_both(gpio, 1);
650 else
651 set_gpio_both(gpio, 0);
652
653 if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
654 set_gpio_polar(gpio, 1);
655 else
656 set_gpio_polar(gpio, 0);
657
658 SSYNC();
659
660 return 0;
661}
662
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800663u32 bfin_pm_standby_setup(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700664{
Bryan Wu1394f032007-05-06 14:50:22 -0700665 u16 bank, mask, i, gpio;
666
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800667 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700668 mask = wakeup_map[gpio_bank(i)];
669 bank = gpio_bank(i);
670
671 gpio_bank_saved[bank].maskb = gpio_bankb[bank]->maskb;
672 gpio_bankb[bank]->maskb = 0;
673
674 if (mask) {
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800675#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700676 gpio_bank_saved[bank].fer = *port_fer[bank];
677#endif
678 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
679 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
680 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
681 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
682 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
Michael Hennerichc58c2142007-10-04 00:35:05 +0800683 gpio_bank_saved[bank].reserved =
684 reserved_gpio_map[bank];
Bryan Wu1394f032007-05-06 14:50:22 -0700685
686 gpio = i;
687
688 while (mask) {
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800689 if ((mask & 1) && (wakeup_flags_map[gpio] !=
690 PM_WAKE_IGNORE)) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800691 reserved_gpio_map[gpio_bank(gpio)] |=
Michael Hennerich581d62a2007-06-14 13:30:23 +0800692 gpio_bit(gpio);
693 bfin_gpio_wakeup_type(gpio,
694 wakeup_flags_map[gpio]);
Bryan Wu1394f032007-05-06 14:50:22 -0700695 set_gpio_data(gpio, 0); /*Clear*/
696 }
697 gpio++;
698 mask >>= 1;
699 }
700
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800701 bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
Bryan Wu1394f032007-05-06 14:50:22 -0700702 gpio_bankb[bank]->maskb_set = wakeup_map[gpio_bank(i)];
703 }
704 }
705
Michael Hennerich2b393312007-10-10 16:58:49 +0800706 AWA_DUMMY_READ(maskb_set);
707
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800708 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700709}
710
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800711void bfin_pm_standby_restore(void)
Bryan Wu1394f032007-05-06 14:50:22 -0700712{
713 u16 bank, mask, i;
714
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800715 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu1394f032007-05-06 14:50:22 -0700716 mask = wakeup_map[gpio_bank(i)];
717 bank = gpio_bank(i);
718
719 if (mask) {
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800720#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Bryan Wu1394f032007-05-06 14:50:22 -0700721 *port_fer[bank] = gpio_bank_saved[bank].fer;
722#endif
723 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
724 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
725 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
726 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
727 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
Michael Hennerich581d62a2007-06-14 13:30:23 +0800728
Michael Hennerichc58c2142007-10-04 00:35:05 +0800729 reserved_gpio_map[bank] =
730 gpio_bank_saved[bank].reserved;
Michael Hennerichcfefe3c2008-02-09 04:12:37 +0800731 bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
Bryan Wu1394f032007-05-06 14:50:22 -0700732 }
733
734 gpio_bankb[bank]->maskb = gpio_bank_saved[bank].maskb;
735 }
Michael Hennerich2b393312007-10-10 16:58:49 +0800736 AWA_DUMMY_READ(maskb);
Bryan Wu1394f032007-05-06 14:50:22 -0700737}
738
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800739void bfin_gpio_pm_hibernate_suspend(void)
740{
741 int i, bank;
742
743 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
744 bank = gpio_bank(i);
745
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800746#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800747 gpio_bank_saved[bank].fer = *port_fer[bank];
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800748#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800749 gpio_bank_saved[bank].mux = *port_mux[bank];
750#else
751 if (bank == 0)
752 gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
753#endif
754#endif
755 gpio_bank_saved[bank].data = gpio_bankb[bank]->data;
756 gpio_bank_saved[bank].inen = gpio_bankb[bank]->inen;
757 gpio_bank_saved[bank].polar = gpio_bankb[bank]->polar;
758 gpio_bank_saved[bank].dir = gpio_bankb[bank]->dir;
759 gpio_bank_saved[bank].edge = gpio_bankb[bank]->edge;
760 gpio_bank_saved[bank].both = gpio_bankb[bank]->both;
761 gpio_bank_saved[bank].maska = gpio_bankb[bank]->maska;
762 }
763
764 AWA_DUMMY_READ(maska);
765}
766
767void bfin_gpio_pm_hibernate_restore(void)
768{
769 int i, bank;
770
771 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
772 bank = gpio_bank(i);
773
Bryan Wu2f6f4bc2008-11-18 17:48:21 +0800774#if defined(BF527_FAMILY) || defined(BF537_FAMILY) || defined(BF518_FAMILY)
775#if defined(BF527_FAMILY) || defined(BF518_FAMILY)
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800776 *port_mux[bank] = gpio_bank_saved[bank].mux;
777#else
778 if (bank == 0)
779 bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
780#endif
781 *port_fer[bank] = gpio_bank_saved[bank].fer;
782#endif
783 gpio_bankb[bank]->inen = gpio_bank_saved[bank].inen;
784 gpio_bankb[bank]->dir = gpio_bank_saved[bank].dir;
785 gpio_bankb[bank]->polar = gpio_bank_saved[bank].polar;
786 gpio_bankb[bank]->edge = gpio_bank_saved[bank].edge;
787 gpio_bankb[bank]->both = gpio_bank_saved[bank].both;
788
789 gpio_bankb[bank]->data_set = gpio_bank_saved[bank].data
790 | gpio_bank_saved[bank].dir;
791
792 gpio_bankb[bank]->maska = gpio_bank_saved[bank].maska;
793 }
794 AWA_DUMMY_READ(maska);
795}
796
797
Bryan Wu1394f032007-05-06 14:50:22 -0700798#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800799#else /* BF548_FAMILY */
Michael Hennerich1efc80b2008-07-19 16:57:32 +0800800#ifdef CONFIG_PM
801
802u32 bfin_pm_standby_setup(void)
803{
804 return 0;
805}
806
807void bfin_pm_standby_restore(void)
808{
809
810}
811
812void bfin_gpio_pm_hibernate_suspend(void)
813{
814 int i, bank;
815
816 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
817 bank = gpio_bank(i);
818
819 gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
820 gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
821 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
822 gpio_bank_saved[bank].data = gpio_array[bank]->port_data;
823 gpio_bank_saved[bank].inen = gpio_array[bank]->port_inen;
824 gpio_bank_saved[bank].dir = gpio_array[bank]->port_dir_set;
825 }
826}
827
828void bfin_gpio_pm_hibernate_restore(void)
829{
830 int i, bank;
831
832 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
833 bank = gpio_bank(i);
834
835 gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
836 gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
837 gpio_array[bank]->port_inen = gpio_bank_saved[bank].inen;
838 gpio_array[bank]->port_dir_set = gpio_bank_saved[bank].dir;
839 gpio_array[bank]->port_set = gpio_bank_saved[bank].data
840 | gpio_bank_saved[bank].dir;
841 }
842}
843#endif
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800844
Michael Hennericha2c8cfe2008-01-22 17:20:10 +0800845unsigned short get_gpio_dir(unsigned gpio)
Michael Hennerichfac3cf42007-12-24 20:07:03 +0800846{
847 return (0x01 & (gpio_array[gpio_bank(gpio)]->port_dir_clear >> gpio_sub_n(gpio)));
848}
849EXPORT_SYMBOL(get_gpio_dir);
850
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800851#endif /* BF548_FAMILY */
Bryan Wu1394f032007-05-06 14:50:22 -0700852
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800853/***********************************************************
854*
855* FUNCTIONS: Blackfin Peripheral Resource Allocation
856* and PortMux Setup
857*
858* INPUTS/OUTPUTS:
859* per Peripheral Identifier
860* label String
861*
862* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
863*
864* CAUTION:
865*************************************************************
866* MODIFICATION HISTORY :
867**************************************************************/
Michael Hennerichc58c2142007-10-04 00:35:05 +0800868
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800869int peripheral_request(unsigned short per, const char *label)
870{
871 unsigned long flags;
872 unsigned short ident = P_IDENT(per);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800873
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800874 /*
875 * Don't cares are pins with only one dedicated function
876 */
877
878 if (per & P_DONTCARE)
879 return 0;
880
881 if (!(per & P_DEFINED))
882 return -ENODEV;
883
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800884 local_irq_save(flags);
885
Mike Frysinger6a87d292008-10-28 16:16:29 +0800886 /* If a pin can be muxed as either GPIO or peripheral, make
887 * sure it is not already a GPIO pin when we request it.
888 */
889 if (unlikely(!check_gpio(ident) &&
890 reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800891 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800892 printk(KERN_ERR
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800893 "%s: Peripheral %d is already reserved as GPIO by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800894 __func__, ident, get_label(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800895 local_irq_restore(flags);
896 return -EBUSY;
897 }
898
899 if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {
900
Mike Frysingerd171c232008-03-26 08:35:46 +0800901 /*
902 * Pin functions like AMC address strobes my
903 * be requested and used by several drivers
904 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800905
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800906#ifdef BF548_FAMILY
907 u16 funct = get_portmux(ident);
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800908
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800909 if (!((per & P_MAYSHARE) && (funct == P_FUNCT2MUX(per)))) {
910#else
911 if (!(per & P_MAYSHARE)) {
912#endif
Mike Frysingerd171c232008-03-26 08:35:46 +0800913 /*
914 * Allow that the identical pin function can
915 * be requested from the same driver twice
916 */
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800917
Mike Frysingerd171c232008-03-26 08:35:46 +0800918 if (cmp_label(ident, label) == 0)
919 goto anyway;
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800920
Mike Frysingerf85c4ab2008-03-26 08:34:23 +0800921 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800922 printk(KERN_ERR
923 "%s: Peripheral %d function %d is already reserved by %s !\n",
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800924 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800925 local_irq_restore(flags);
926 return -EBUSY;
927 }
928 }
929
Mike Frysingerd171c232008-03-26 08:35:46 +0800930 anyway:
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800931 reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);
932
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800933#ifdef BF548_FAMILY
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800934 portmux_setup(ident, P_FUNCT2MUX(per));
Michael Hennerichd2b11a42007-08-28 16:47:46 +0800935#else
Michael Hennerichc58c2142007-10-04 00:35:05 +0800936 portmux_setup(per, P_FUNCT2MUX(per));
Mike Frysinger6c7ec0e2008-10-28 15:49:59 +0800937#endif
Michael Hennerichc58c2142007-10-04 00:35:05 +0800938 port_setup(ident, PERIPHERAL_USAGE);
939
Michael Hennerichc58c2142007-10-04 00:35:05 +0800940 local_irq_restore(flags);
941 set_label(ident, label);
942
943 return 0;
944}
945EXPORT_SYMBOL(peripheral_request);
946
Mike Frysinger68179372008-04-24 05:04:24 +0800947int peripheral_request_list(const unsigned short per[], const char *label)
Michael Hennerichc58c2142007-10-04 00:35:05 +0800948{
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) {
Mike Frysingerd171c232008-03-26 08:35:46 +0800957 for ( ; cnt > 0; cnt--)
Michael Hennerich314c98d2007-07-24 18:03:45 +0800958 peripheral_free(per[cnt - 1]);
Mike Frysingerd171c232008-03-26 08:35:46 +0800959
960 return ret;
Michael Hennerich314c98d2007-07-24 18:03:45 +0800961 }
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
Mike Frysingerd171c232008-03-26 08:35:46 +0800984 if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
Michael Hennerichc58c2142007-10-04 00:35:05 +0800985 local_irq_restore(flags);
986 return;
987 }
988
Mike Frysingerd171c232008-03-26 08:35:46 +0800989 if (!(per & P_MAYSHARE))
Michael Hennerichc58c2142007-10-04 00:35:05 +0800990 port_setup(ident, GPIO_USAGE);
Michael Hennerichc58c2142007-10-04 00:35:05 +0800991
992 reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);
993
Michael Hennerich2acde902007-10-11 00:24:40 +0800994 set_label(ident, "free");
995
Michael Hennerichc58c2142007-10-04 00:35:05 +0800996 local_irq_restore(flags);
997}
998EXPORT_SYMBOL(peripheral_free);
999
Mike Frysinger68179372008-04-24 05:04:24 +08001000void peripheral_free_list(const unsigned short per[])
Michael Hennerichc58c2142007-10-04 00:35:05 +08001001{
1002 u16 cnt;
Mike Frysingerd171c232008-03-26 08:35:46 +08001003 for (cnt = 0; per[cnt] != 0; cnt++)
Michael Hennerichc58c2142007-10-04 00:35:05 +08001004 peripheral_free(per[cnt]);
Michael Hennerichc58c2142007-10-04 00:35:05 +08001005}
1006EXPORT_SYMBOL(peripheral_free_list);
1007
Bryan Wu1394f032007-05-06 14:50:22 -07001008/***********************************************************
1009*
1010* FUNCTIONS: Blackfin GPIO Driver
1011*
1012* INPUTS/OUTPUTS:
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001013* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
1014* label String
Bryan Wu1394f032007-05-06 14:50:22 -07001015*
1016* DESCRIPTION: Blackfin GPIO Driver API
1017*
1018* CAUTION:
1019*************************************************************
1020* MODIFICATION HISTORY :
1021**************************************************************/
1022
Michael Hennerichacbcd262008-01-22 18:36:20 +08001023int gpio_request(unsigned gpio, const char *label)
Bryan Wu1394f032007-05-06 14:50:22 -07001024{
1025 unsigned long flags;
1026
1027 if (check_gpio(gpio) < 0)
1028 return -EINVAL;
1029
1030 local_irq_save(flags);
1031
Michael Hennerich2acde902007-10-11 00:24:40 +08001032 /*
1033 * Allow that the identical GPIO can
1034 * be requested from the same driver twice
1035 * Do nothing and return -
1036 */
1037
1038 if (cmp_label(gpio, label) == 0) {
1039 local_irq_restore(flags);
1040 return 0;
1041 }
1042
Michael Hennerichc58c2142007-10-04 00:35:05 +08001043 if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001044 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001045 printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
1046 gpio, get_label(gpio));
Bryan Wu1394f032007-05-06 14:50:22 -07001047 local_irq_restore(flags);
1048 return -EBUSY;
1049 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001050 if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001051 dump_stack();
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001052 printk(KERN_ERR
1053 "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
1054 gpio, get_label(gpio));
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001055 local_irq_restore(flags);
1056 return -EBUSY;
1057 }
1058
Michael Hennerichc58c2142007-10-04 00:35:05 +08001059 reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001060
1061 local_irq_restore(flags);
1062
1063 port_setup(gpio, GPIO_USAGE);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001064 set_label(gpio, label);
Bryan Wu1394f032007-05-06 14:50:22 -07001065
1066 return 0;
1067}
1068EXPORT_SYMBOL(gpio_request);
1069
Michael Hennerichacbcd262008-01-22 18:36:20 +08001070void gpio_free(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001071{
1072 unsigned long flags;
1073
1074 if (check_gpio(gpio) < 0)
1075 return;
1076
1077 local_irq_save(flags);
1078
Michael Hennerichc58c2142007-10-04 00:35:05 +08001079 if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
Bryan Wu1394f032007-05-06 14:50:22 -07001080 dump_stack();
Mike Frysingerf85c4ab2008-03-26 08:34:23 +08001081 gpio_error(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001082 local_irq_restore(flags);
1083 return;
1084 }
1085
Michael Hennerichc58c2142007-10-04 00:35:05 +08001086 reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu1394f032007-05-06 14:50:22 -07001087
Michael Hennerich2acde902007-10-11 00:24:40 +08001088 set_label(gpio, "free");
1089
Bryan Wu1394f032007-05-06 14:50:22 -07001090 local_irq_restore(flags);
1091}
1092EXPORT_SYMBOL(gpio_free);
1093
Michael Hennerichacbcd262008-01-22 18:36:20 +08001094
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001095#ifdef BF548_FAMILY
Michael Hennerichacbcd262008-01-22 18:36:20 +08001096int gpio_direction_input(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001097{
1098 unsigned long flags;
1099
Michael Hennerichacbcd262008-01-22 18:36:20 +08001100 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1101 gpio_error(gpio);
1102 return -EINVAL;
1103 }
1104
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001105 local_irq_save(flags);
1106 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1107 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1108 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001109
1110 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001111}
1112EXPORT_SYMBOL(gpio_direction_input);
1113
Michael Hennerichacbcd262008-01-22 18:36:20 +08001114int gpio_direction_output(unsigned gpio, int value)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001115{
1116 unsigned long flags;
1117
Michael Hennerichacbcd262008-01-22 18:36:20 +08001118 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1119 gpio_error(gpio);
1120 return -EINVAL;
1121 }
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001122
1123 local_irq_save(flags);
1124 gpio_array[gpio_bank(gpio)]->port_inen &= ~gpio_bit(gpio);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001125 gpio_set_value(gpio, value);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001126 gpio_array[gpio_bank(gpio)]->port_dir_set = gpio_bit(gpio);
1127 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001128
1129 return 0;
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001130}
1131EXPORT_SYMBOL(gpio_direction_output);
1132
Michael Hennerichacbcd262008-01-22 18:36:20 +08001133void gpio_set_value(unsigned gpio, int arg)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001134{
1135 if (arg)
1136 gpio_array[gpio_bank(gpio)]->port_set = gpio_bit(gpio);
1137 else
1138 gpio_array[gpio_bank(gpio)]->port_clear = gpio_bit(gpio);
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001139}
1140EXPORT_SYMBOL(gpio_set_value);
1141
Michael Hennerichacbcd262008-01-22 18:36:20 +08001142int gpio_get_value(unsigned gpio)
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001143{
1144 return (1 & (gpio_array[gpio_bank(gpio)]->port_data >> gpio_sub_n(gpio)));
1145}
1146EXPORT_SYMBOL(gpio_get_value);
1147
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001148void bfin_gpio_irq_prepare(unsigned gpio)
1149{
1150 unsigned long flags;
1151
1152 port_setup(gpio, GPIO_USAGE);
1153
1154 local_irq_save(flags);
1155 gpio_array[gpio_bank(gpio)]->port_dir_clear = gpio_bit(gpio);
1156 gpio_array[gpio_bank(gpio)]->port_inen |= gpio_bit(gpio);
1157 local_irq_restore(flags);
1158}
1159
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001160#else
1161
Michael Hennerich803a8d22008-05-17 16:01:51 +08001162int gpio_get_value(unsigned gpio)
1163{
1164 unsigned long flags;
1165 int ret;
1166
1167 if (unlikely(get_gpio_edge(gpio))) {
1168 local_irq_save(flags);
1169 set_gpio_edge(gpio, 0);
1170 ret = get_gpio_data(gpio);
1171 set_gpio_edge(gpio, 1);
1172 local_irq_restore(flags);
1173
1174 return ret;
1175 } else
1176 return get_gpio_data(gpio);
1177}
1178EXPORT_SYMBOL(gpio_get_value);
1179
1180
Michael Hennerichacbcd262008-01-22 18:36:20 +08001181int gpio_direction_input(unsigned gpio)
Bryan Wu1394f032007-05-06 14:50:22 -07001182{
1183 unsigned long flags;
1184
Michael Hennerichacbcd262008-01-22 18:36:20 +08001185 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1186 gpio_error(gpio);
1187 return -EINVAL;
1188 }
Bryan Wu1394f032007-05-06 14:50:22 -07001189
1190 local_irq_save(flags);
1191 gpio_bankb[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1192 gpio_bankb[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001193 AWA_DUMMY_READ(inen);
Bryan Wu1394f032007-05-06 14:50:22 -07001194 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001195
1196 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001197}
1198EXPORT_SYMBOL(gpio_direction_input);
1199
Michael Hennerichacbcd262008-01-22 18:36:20 +08001200int gpio_direction_output(unsigned gpio, int value)
Bryan Wu1394f032007-05-06 14:50:22 -07001201{
1202 unsigned long flags;
1203
Michael Hennerichacbcd262008-01-22 18:36:20 +08001204 if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1205 gpio_error(gpio);
1206 return -EINVAL;
1207 }
Bryan Wu1394f032007-05-06 14:50:22 -07001208
1209 local_irq_save(flags);
1210 gpio_bankb[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001211
1212 if (value)
1213 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1214 else
1215 gpio_bankb[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1216
Bryan Wu1394f032007-05-06 14:50:22 -07001217 gpio_bankb[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Michael Hennerich2b393312007-10-10 16:58:49 +08001218 AWA_DUMMY_READ(dir);
Bryan Wu1394f032007-05-06 14:50:22 -07001219 local_irq_restore(flags);
Michael Hennerichacbcd262008-01-22 18:36:20 +08001220
1221 return 0;
Bryan Wu1394f032007-05-06 14:50:22 -07001222}
1223EXPORT_SYMBOL(gpio_direction_output);
Mike Frysinger168f1212007-10-11 00:22:35 +08001224
1225/* If we are booting from SPI and our board lacks a strong enough pull up,
1226 * the core can reset and execute the bootrom faster than the resistor can
1227 * pull the signal logically high. To work around this (common) error in
1228 * board design, we explicitly set the pin back to GPIO mode, force /CS
1229 * high, and wait for the electrons to do their thing.
1230 *
1231 * This function only makes sense to be called from reset code, but it
1232 * lives here as we need to force all the GPIO states w/out going through
1233 * BUG() checks and such.
1234 */
1235void bfin_gpio_reset_spi0_ssel1(void)
1236{
Michael Hennerich4d5f4ed2007-08-27 16:46:17 +08001237 u16 gpio = P_IDENT(P_SPI0_SSEL1);
1238
1239 port_setup(gpio, GPIO_USAGE);
1240 gpio_bankb[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
Michael Hennericha2c8cfe2008-01-22 17:20:10 +08001241 AWA_DUMMY_READ(data_set);
Mike Frysinger168f1212007-10-11 00:22:35 +08001242 udelay(1);
1243}
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001244
Michael Hennerichaffee2b2008-04-24 08:10:10 +08001245void bfin_gpio_irq_prepare(unsigned gpio)
1246{
1247 port_setup(gpio, GPIO_USAGE);
1248}
1249
Michael Hennerichd2b11a42007-08-28 16:47:46 +08001250#endif /*BF548_FAMILY */
Mike Frysinger1545a112007-12-24 16:54:48 +08001251
1252#if defined(CONFIG_PROC_FS)
1253static int gpio_proc_read(char *buf, char **start, off_t offset,
1254 int len, int *unused_i, void *unused_v)
1255{
1256 int c, outlen = 0;
1257
1258 for (c = 0; c < MAX_RESOURCES; c++) {
1259 if (!check_gpio(c) && (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c)))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001260 len = sprintf(buf, "GPIO_%d: %s \t\tGPIO %s\n", c,
Mike Frysinger1545a112007-12-24 16:54:48 +08001261 get_label(c), get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1262 else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
Michael Hennerichfac3cf42007-12-24 20:07:03 +08001263 len = sprintf(buf, "GPIO_%d: %s \t\tPeripheral\n", c, get_label(c));
Mike Frysinger1545a112007-12-24 16:54:48 +08001264 else
1265 continue;
1266 buf += len;
1267 outlen += len;
1268 }
1269 return outlen;
1270}
1271
1272static __init int gpio_register_proc(void)
1273{
1274 struct proc_dir_entry *proc_gpio;
1275
1276 proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
1277 if (proc_gpio)
1278 proc_gpio->read_proc = gpio_proc_read;
1279 return proc_gpio != NULL;
1280}
Mike Frysinger1545a112007-12-24 16:54:48 +08001281__initcall(gpio_register_proc);
1282#endif