blob: f9aff4d894ae95f2d69239e60127485cc5b1a1cc [file] [log] [blame]
Mike Frysinger4638b212010-06-02 04:17:26 -04001/*
Sonic Zhangda34aae2013-05-02 13:46:21 +08002 * ADI GPIO1 Abstraction Layer
3 * Support BF50x, BF51x, BF52x, BF53x and BF561 only.
Mike Frysinger4638b212010-06-02 04:17:26 -04004 *
5 * Copyright 2006-2010 Analog Devices Inc.
6 *
7 * Licensed under the GPL-2 or later
8 */
9
10#include <common.h>
11#include <asm/errno.h>
12#include <asm/gpio.h>
13#include <asm/portmux.h>
14
15#if ANOMALY_05000311 || ANOMALY_05000323
16enum {
17 AWA_data = SYSCR,
18 AWA_data_clear = SYSCR,
19 AWA_data_set = SYSCR,
20 AWA_toggle = SYSCR,
21 AWA_maska = UART_SCR,
22 AWA_maska_clear = UART_SCR,
23 AWA_maska_set = UART_SCR,
24 AWA_maska_toggle = UART_SCR,
25 AWA_maskb = UART_GCTL,
26 AWA_maskb_clear = UART_GCTL,
27 AWA_maskb_set = UART_GCTL,
28 AWA_maskb_toggle = UART_GCTL,
29 AWA_dir = SPORT1_STAT,
30 AWA_polar = SPORT1_STAT,
31 AWA_edge = SPORT1_STAT,
32 AWA_both = SPORT1_STAT,
33#if ANOMALY_05000311
34 AWA_inen = TIMER_ENABLE,
35#elif ANOMALY_05000323
36 AWA_inen = DMA1_1_CONFIG,
37#endif
38};
39 /* Anomaly Workaround */
40#define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
41#else
42#define AWA_DUMMY_READ(...) do { } while (0)
43#endif
44
45static struct gpio_port_t * const gpio_array[] = {
46#if defined(BF533_FAMILY)
47 (struct gpio_port_t *) FIO_FLAG_D,
48#elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) \
Mike Frysingercca07412010-12-17 15:25:09 -050049 || defined(BF538_FAMILY) || defined(CONFIG_BF50x)
Mike Frysinger4638b212010-06-02 04:17:26 -040050 (struct gpio_port_t *) PORTFIO,
51# if !defined(BF538_FAMILY)
52 (struct gpio_port_t *) PORTGIO,
53 (struct gpio_port_t *) PORTHIO,
54# endif
55#elif defined(BF561_FAMILY)
56 (struct gpio_port_t *) FIO0_FLAG_D,
57 (struct gpio_port_t *) FIO1_FLAG_D,
58 (struct gpio_port_t *) FIO2_FLAG_D,
Mike Frysinger4638b212010-06-02 04:17:26 -040059#else
60# error no gpio arrays defined
61#endif
62};
63
Mike Frysingercca07412010-12-17 15:25:09 -050064#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) || \
65 defined(CONFIG_BF50x)
Mike Frysinger4638b212010-06-02 04:17:26 -040066static unsigned short * const port_fer[] = {
67 (unsigned short *) PORTF_FER,
68 (unsigned short *) PORTG_FER,
69 (unsigned short *) PORTH_FER,
70};
71
72# if !defined(BF537_FAMILY)
73static unsigned short * const port_mux[] = {
74 (unsigned short *) PORTF_MUX,
75 (unsigned short *) PORTG_MUX,
76 (unsigned short *) PORTH_MUX,
77};
78
79static const
80u8 pmux_offset[][16] = {
81# if defined(CONFIG_BF52x)
82 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
83 { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
84 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
85# elif defined(CONFIG_BF51x)
86 { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
87 { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
88 { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
89# endif
90};
91# endif
92
93#elif defined(BF538_FAMILY)
94static unsigned short * const port_fer[] = {
95 (unsigned short *) PORTCIO_FER,
96 (unsigned short *) PORTDIO_FER,
97 (unsigned short *) PORTEIO_FER,
98};
99#endif
100
101#ifdef CONFIG_BFIN_GPIO_TRACK
102#define RESOURCE_LABEL_SIZE 16
103
104static struct str_ident {
105 char name[RESOURCE_LABEL_SIZE];
106} str_ident[MAX_RESOURCES];
107
108static void gpio_error(unsigned gpio)
109{
110 printf("bfin-gpio: GPIO %d wasn't requested!\n", gpio);
111}
112
113static void set_label(unsigned short ident, const char *label)
114{
115 if (label) {
116 strncpy(str_ident[ident].name, label,
117 RESOURCE_LABEL_SIZE);
118 str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
119 }
120}
121
122static char *get_label(unsigned short ident)
123{
124 return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
125}
126
127static int cmp_label(unsigned short ident, const char *label)
128{
129 if (label == NULL)
130 printf("bfin-gpio: please provide none-null label\n");
131
132 if (label)
133 return strcmp(str_ident[ident].name, label);
134 else
135 return -EINVAL;
136}
137
138#define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
139#define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
140#define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
141#define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
142#define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
143#else
144#define is_reserved(m, i, e) (!(e))
145#define reserve(m, i)
146#define unreserve(m, i)
147#define DECLARE_RESERVED_MAP(m, c)
148#define gpio_error(gpio)
149#define set_label(...)
150#define get_label(...) ""
151#define cmp_label(...) 1
152#endif
153
154DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
155DECLARE_RESERVED_MAP(peri, gpio_bank(MAX_RESOURCES));
156
157inline int check_gpio(unsigned gpio)
158{
Mike Frysinger4638b212010-06-02 04:17:26 -0400159 if (gpio >= MAX_BLACKFIN_GPIOS)
160 return -EINVAL;
161 return 0;
162}
163
164static void port_setup(unsigned gpio, unsigned short usage)
165{
166#if defined(BF538_FAMILY)
167 /*
168 * BF538/9 Port C,D and E are special.
169 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
170 * Regular PORT F GPIOs are handled here, CDE are exclusively
171 * managed by GPIOLIB
172 */
173
174 if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
175 return;
176
177 gpio -= MAX_BLACKFIN_GPIOS;
178
179 if (usage == GPIO_USAGE)
180 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
181 else
182 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
183 SSYNC();
184 return;
185#endif
186
187 if (check_gpio(gpio))
188 return;
189
Mike Frysingercca07412010-12-17 15:25:09 -0500190#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x) || \
191 defined(CONFIG_BF50x)
Mike Frysinger4638b212010-06-02 04:17:26 -0400192 if (usage == GPIO_USAGE)
193 *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
194 else
195 *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
196 SSYNC();
Mike Frysinger4638b212010-06-02 04:17:26 -0400197#endif
198}
199
200#ifdef BF537_FAMILY
201static struct {
202 unsigned short res;
203 unsigned short offset;
204} port_mux_lut[] = {
205 {.res = P_PPI0_D13, .offset = 11},
206 {.res = P_PPI0_D14, .offset = 11},
207 {.res = P_PPI0_D15, .offset = 11},
208 {.res = P_SPORT1_TFS, .offset = 11},
209 {.res = P_SPORT1_TSCLK, .offset = 11},
210 {.res = P_SPORT1_DTPRI, .offset = 11},
211 {.res = P_PPI0_D10, .offset = 10},
212 {.res = P_PPI0_D11, .offset = 10},
213 {.res = P_PPI0_D12, .offset = 10},
214 {.res = P_SPORT1_RSCLK, .offset = 10},
215 {.res = P_SPORT1_RFS, .offset = 10},
216 {.res = P_SPORT1_DRPRI, .offset = 10},
217 {.res = P_PPI0_D8, .offset = 9},
218 {.res = P_PPI0_D9, .offset = 9},
219 {.res = P_SPORT1_DRSEC, .offset = 9},
220 {.res = P_SPORT1_DTSEC, .offset = 9},
221 {.res = P_TMR2, .offset = 8},
222 {.res = P_PPI0_FS3, .offset = 8},
223 {.res = P_TMR3, .offset = 7},
224 {.res = P_SPI0_SSEL4, .offset = 7},
225 {.res = P_TMR4, .offset = 6},
226 {.res = P_SPI0_SSEL5, .offset = 6},
227 {.res = P_TMR5, .offset = 5},
228 {.res = P_SPI0_SSEL6, .offset = 5},
229 {.res = P_UART1_RX, .offset = 4},
230 {.res = P_UART1_TX, .offset = 4},
231 {.res = P_TMR6, .offset = 4},
232 {.res = P_TMR7, .offset = 4},
233 {.res = P_UART0_RX, .offset = 3},
234 {.res = P_UART0_TX, .offset = 3},
235 {.res = P_DMAR0, .offset = 3},
236 {.res = P_DMAR1, .offset = 3},
237 {.res = P_SPORT0_DTSEC, .offset = 1},
238 {.res = P_SPORT0_DRSEC, .offset = 1},
239 {.res = P_CAN0_RX, .offset = 1},
240 {.res = P_CAN0_TX, .offset = 1},
241 {.res = P_SPI0_SSEL7, .offset = 1},
242 {.res = P_SPORT0_TFS, .offset = 0},
243 {.res = P_SPORT0_DTPRI, .offset = 0},
244 {.res = P_SPI0_SSEL2, .offset = 0},
245 {.res = P_SPI0_SSEL3, .offset = 0},
246};
247
248static void portmux_setup(unsigned short per)
249{
Axel Lin9868b142013-06-28 14:45:06 +0800250 u16 y, offset, muxreg, mask;
Mike Frysinger4638b212010-06-02 04:17:26 -0400251 u16 function = P_FUNCT2MUX(per);
252
253 for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
254 if (port_mux_lut[y].res == per) {
255
256 /* SET PORTMUX REG */
257
258 offset = port_mux_lut[y].offset;
259 muxreg = bfin_read_PORT_MUX();
260
Axel Lin9868b142013-06-28 14:45:06 +0800261 if (offset == 1)
262 mask = 3;
Mike Frysinger4638b212010-06-02 04:17:26 -0400263 else
Axel Lin9868b142013-06-28 14:45:06 +0800264 mask = 1;
Mike Frysinger4638b212010-06-02 04:17:26 -0400265
Axel Lin9868b142013-06-28 14:45:06 +0800266 muxreg &= ~(mask << offset);
267 muxreg |= ((function & mask) << offset);
Mike Frysinger4638b212010-06-02 04:17:26 -0400268 bfin_write_PORT_MUX(muxreg);
269 }
270 }
271}
Mike Frysinger4638b212010-06-02 04:17:26 -0400272#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
273inline void portmux_setup(unsigned short per)
274{
275 u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
276 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
277
278 pmux = *port_mux[gpio_bank(ident)];
279 pmux &= ~(3 << offset);
280 pmux |= (function & 3) << offset;
281 *port_mux[gpio_bank(ident)] = pmux;
282 SSYNC();
283}
284#else
285# define portmux_setup(...) do { } while (0)
286#endif
287
Mike Frysinger4638b212010-06-02 04:17:26 -0400288/***********************************************************
289*
290* FUNCTIONS: Blackfin General Purpose Ports Access Functions
291*
292* INPUTS/OUTPUTS:
293* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
294*
295*
296* DESCRIPTION: These functions abstract direct register access
297* to Blackfin processor General Purpose
298* Ports Regsiters
299*
300* CAUTION: These functions do not belong to the GPIO Driver API
301*************************************************************
302* MODIFICATION HISTORY :
303**************************************************************/
304
305/* Set a specific bit */
306
307#define SET_GPIO(name) \
308void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
309{ \
310 unsigned long flags; \
311 local_irq_save(flags); \
312 if (arg) \
313 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
314 else \
315 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
316 AWA_DUMMY_READ(name); \
317 local_irq_restore(flags); \
318}
319
320SET_GPIO(dir) /* set_gpio_dir() */
321SET_GPIO(inen) /* set_gpio_inen() */
322SET_GPIO(polar) /* set_gpio_polar() */
323SET_GPIO(edge) /* set_gpio_edge() */
324SET_GPIO(both) /* set_gpio_both() */
325
326
327#define SET_GPIO_SC(name) \
328void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
329{ \
330 unsigned long flags; \
331 if (ANOMALY_05000311 || ANOMALY_05000323) \
332 local_irq_save(flags); \
333 if (arg) \
334 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
335 else \
336 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
337 if (ANOMALY_05000311 || ANOMALY_05000323) { \
338 AWA_DUMMY_READ(name); \
339 local_irq_restore(flags); \
340 } \
341}
342
343SET_GPIO_SC(maska)
344SET_GPIO_SC(maskb)
345SET_GPIO_SC(data)
346
347void set_gpio_toggle(unsigned gpio)
348{
349 unsigned long flags;
350 if (ANOMALY_05000311 || ANOMALY_05000323)
351 local_irq_save(flags);
352 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
353 if (ANOMALY_05000311 || ANOMALY_05000323) {
354 AWA_DUMMY_READ(toggle);
355 local_irq_restore(flags);
356 }
357}
358
359/* Set current PORT date (16-bit word) */
360
361#define SET_GPIO_P(name) \
362void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
363{ \
364 unsigned long flags; \
365 if (ANOMALY_05000311 || ANOMALY_05000323) \
366 local_irq_save(flags); \
367 gpio_array[gpio_bank(gpio)]->name = arg; \
368 if (ANOMALY_05000311 || ANOMALY_05000323) { \
369 AWA_DUMMY_READ(name); \
370 local_irq_restore(flags); \
371 } \
372}
373
374SET_GPIO_P(data)
375SET_GPIO_P(dir)
376SET_GPIO_P(inen)
377SET_GPIO_P(polar)
378SET_GPIO_P(edge)
379SET_GPIO_P(both)
380SET_GPIO_P(maska)
381SET_GPIO_P(maskb)
382
383/* Get a specific bit */
384#define GET_GPIO(name) \
385unsigned short get_gpio_ ## name(unsigned gpio) \
386{ \
387 unsigned long flags; \
388 unsigned short ret; \
389 if (ANOMALY_05000311 || ANOMALY_05000323) \
390 local_irq_save(flags); \
391 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
392 if (ANOMALY_05000311 || ANOMALY_05000323) { \
393 AWA_DUMMY_READ(name); \
394 local_irq_restore(flags); \
395 } \
396 return ret; \
397}
398
399GET_GPIO(data)
400GET_GPIO(dir)
401GET_GPIO(inen)
402GET_GPIO(polar)
403GET_GPIO(edge)
404GET_GPIO(both)
405GET_GPIO(maska)
406GET_GPIO(maskb)
407
408/* Get current PORT date (16-bit word) */
409
410#define GET_GPIO_P(name) \
411unsigned short get_gpiop_ ## name(unsigned gpio) \
412{ \
413 unsigned long flags; \
414 unsigned short ret; \
415 if (ANOMALY_05000311 || ANOMALY_05000323) \
416 local_irq_save(flags); \
417 ret = (gpio_array[gpio_bank(gpio)]->name); \
418 if (ANOMALY_05000311 || ANOMALY_05000323) { \
419 AWA_DUMMY_READ(name); \
420 local_irq_restore(flags); \
421 } \
422 return ret; \
423}
424
425GET_GPIO_P(data)
426GET_GPIO_P(dir)
427GET_GPIO_P(inen)
428GET_GPIO_P(polar)
429GET_GPIO_P(edge)
430GET_GPIO_P(both)
431GET_GPIO_P(maska)
432GET_GPIO_P(maskb)
433
Mike Frysinger4638b212010-06-02 04:17:26 -0400434/***********************************************************
435*
436* FUNCTIONS: Blackfin Peripheral Resource Allocation
437* and PortMux Setup
438*
439* INPUTS/OUTPUTS:
440* per Peripheral Identifier
441* label String
442*
443* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
444*
445* CAUTION:
446*************************************************************
447* MODIFICATION HISTORY :
448**************************************************************/
449
450int peripheral_request(unsigned short per, const char *label)
451{
452 unsigned short ident = P_IDENT(per);
453
454 /*
455 * Don't cares are pins with only one dedicated function
456 */
457
458 if (per & P_DONTCARE)
459 return 0;
460
461 if (!(per & P_DEFINED))
462 return -ENODEV;
463
464 BUG_ON(ident >= MAX_RESOURCES);
465
466 /* If a pin can be muxed as either GPIO or peripheral, make
467 * sure it is not already a GPIO pin when we request it.
468 */
469 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
470 printf("%s: Peripheral %d is already reserved as GPIO by %s !\n",
471 __func__, ident, get_label(ident));
472 return -EBUSY;
473 }
474
475 if (unlikely(is_reserved(peri, ident, 1))) {
476
477 /*
478 * Pin functions like AMC address strobes my
479 * be requested and used by several drivers
480 */
481
Mike Frysinger4638b212010-06-02 04:17:26 -0400482 if (!(per & P_MAYSHARE)) {
Mike Frysinger4638b212010-06-02 04:17:26 -0400483 /*
484 * Allow that the identical pin function can
485 * be requested from the same driver twice
486 */
487
488 if (cmp_label(ident, label) == 0)
489 goto anyway;
490
491 printf("%s: Peripheral %d function %d is already reserved by %s !\n",
492 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
493 return -EBUSY;
494 }
495 }
496
497 anyway:
498 reserve(peri, ident);
499
500 portmux_setup(per);
501 port_setup(ident, PERIPHERAL_USAGE);
502
503 set_label(ident, label);
504
505 return 0;
506}
507
508int peripheral_request_list(const unsigned short per[], const char *label)
509{
510 u16 cnt;
511 int ret;
512
513 for (cnt = 0; per[cnt] != 0; cnt++) {
514
515 ret = peripheral_request(per[cnt], label);
516
517 if (ret < 0) {
518 for ( ; cnt > 0; cnt--)
519 peripheral_free(per[cnt - 1]);
520
521 return ret;
522 }
523 }
524
525 return 0;
526}
527
528void peripheral_free(unsigned short per)
529{
530 unsigned short ident = P_IDENT(per);
531
532 if (per & P_DONTCARE)
533 return;
534
535 if (!(per & P_DEFINED))
536 return;
537
538 if (unlikely(!is_reserved(peri, ident, 0)))
539 return;
540
541 if (!(per & P_MAYSHARE))
542 port_setup(ident, GPIO_USAGE);
543
544 unreserve(peri, ident);
545
546 set_label(ident, "free");
547}
548
549void peripheral_free_list(const unsigned short per[])
550{
551 u16 cnt;
552 for (cnt = 0; per[cnt] != 0; cnt++)
553 peripheral_free(per[cnt]);
554}
555
556/***********************************************************
557*
558* FUNCTIONS: Blackfin GPIO Driver
559*
560* INPUTS/OUTPUTS:
561* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
562* label String
563*
564* DESCRIPTION: Blackfin GPIO Driver API
565*
566* CAUTION:
567*************************************************************
568* MODIFICATION HISTORY :
569**************************************************************/
570
Sonic Zhangda34aae2013-05-02 13:46:21 +0800571int gpio_request(unsigned gpio, const char *label)
Mike Frysinger4638b212010-06-02 04:17:26 -0400572{
573 if (check_gpio(gpio) < 0)
574 return -EINVAL;
575
576 /*
577 * Allow that the identical GPIO can
578 * be requested from the same driver twice
579 * Do nothing and return -
580 */
581
582 if (cmp_label(gpio, label) == 0)
583 return 0;
584
585 if (unlikely(is_reserved(gpio, gpio, 1))) {
586 printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
587 gpio, get_label(gpio));
588 return -EBUSY;
589 }
590 if (unlikely(is_reserved(peri, gpio, 1))) {
591 printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
592 gpio, get_label(gpio));
593 return -EBUSY;
594 }
Mike Frysinger4638b212010-06-02 04:17:26 -0400595 else { /* Reset POLAR setting when acquiring a gpio for the first time */
596 set_gpio_polar(gpio, 0);
597 }
Mike Frysinger4638b212010-06-02 04:17:26 -0400598
599 reserve(gpio, gpio);
600 set_label(gpio, label);
601
602 port_setup(gpio, GPIO_USAGE);
603
604 return 0;
605}
606
Sonic Zhangda34aae2013-05-02 13:46:21 +0800607int gpio_free(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400608{
609 if (check_gpio(gpio) < 0)
Sonic Zhangda34aae2013-05-02 13:46:21 +0800610 return -1;
Mike Frysinger4638b212010-06-02 04:17:26 -0400611
612 if (unlikely(!is_reserved(gpio, gpio, 0))) {
613 gpio_error(gpio);
Sonic Zhangda34aae2013-05-02 13:46:21 +0800614 return -1;
Mike Frysinger4638b212010-06-02 04:17:26 -0400615 }
616
617 unreserve(gpio, gpio);
618
619 set_label(gpio, "free");
Mike Frysinger4638b212010-06-02 04:17:26 -0400620
Sonic Zhangda34aae2013-05-02 13:46:21 +0800621 return 0;
622}
623
624#ifdef ADI_SPECIAL_GPIO_BANKS
Mike Frysinger4638b212010-06-02 04:17:26 -0400625DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
626
Sonic Zhangda34aae2013-05-02 13:46:21 +0800627int special_gpio_request(unsigned gpio, const char *label)
Mike Frysinger4638b212010-06-02 04:17:26 -0400628{
629 /*
630 * Allow that the identical GPIO can
631 * be requested from the same driver twice
632 * Do nothing and return -
633 */
634
635 if (cmp_label(gpio, label) == 0)
636 return 0;
637
638 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
639 printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
640 gpio, get_label(gpio));
641 return -EBUSY;
642 }
643 if (unlikely(is_reserved(peri, gpio, 1))) {
644 printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
645 gpio, get_label(gpio));
646
647 return -EBUSY;
648 }
649
650 reserve(special_gpio, gpio);
651 reserve(peri, gpio);
652
653 set_label(gpio, label);
654 port_setup(gpio, GPIO_USAGE);
655
656 return 0;
657}
658
Sonic Zhangda34aae2013-05-02 13:46:21 +0800659void special_gpio_free(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400660{
661 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
662 gpio_error(gpio);
663 return;
664 }
665
Axel Lin5460b8d2013-06-26 10:09:16 +0800666 unreserve(special_gpio, gpio);
667 unreserve(peri, gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400668 set_label(gpio, "free");
669}
670#endif
671
Sonic Zhangda34aae2013-05-02 13:46:21 +0800672static inline void __gpio_direction_input(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400673{
Mike Frysinger4638b212010-06-02 04:17:26 -0400674 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400675 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
676}
677
Sonic Zhangda34aae2013-05-02 13:46:21 +0800678int gpio_direction_input(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400679{
680 unsigned long flags;
681
682 if (!is_reserved(gpio, gpio, 0)) {
683 gpio_error(gpio);
684 return -EINVAL;
685 }
686
687 local_irq_save(flags);
Sonic Zhangda34aae2013-05-02 13:46:21 +0800688 __gpio_direction_input(gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400689 AWA_DUMMY_READ(inen);
690 local_irq_restore(flags);
691
692 return 0;
693}
694
Sonic Zhangda34aae2013-05-02 13:46:21 +0800695int gpio_set_value(unsigned gpio, int arg)
Mike Frysinger4638b212010-06-02 04:17:26 -0400696{
697 if (arg)
698 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
699 else
700 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
Sonic Zhangda34aae2013-05-02 13:46:21 +0800701
702 return 0;
Mike Frysinger4638b212010-06-02 04:17:26 -0400703}
704
Sonic Zhangda34aae2013-05-02 13:46:21 +0800705int gpio_direction_output(unsigned gpio, int value)
Mike Frysinger4638b212010-06-02 04:17:26 -0400706{
707 unsigned long flags;
708
709 if (!is_reserved(gpio, gpio, 0)) {
710 gpio_error(gpio);
711 return -EINVAL;
712 }
713
714 local_irq_save(flags);
715
716 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
717 gpio_set_value(gpio, value);
Mike Frysinger4638b212010-06-02 04:17:26 -0400718 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400719
720 AWA_DUMMY_READ(dir);
721 local_irq_restore(flags);
722
723 return 0;
724}
725
Sonic Zhangda34aae2013-05-02 13:46:21 +0800726int gpio_get_value(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400727{
Mike Frysinger4638b212010-06-02 04:17:26 -0400728 unsigned long flags;
729
730 if (unlikely(get_gpio_edge(gpio))) {
731 int ret;
732 local_irq_save(flags);
733 set_gpio_edge(gpio, 0);
734 ret = get_gpio_data(gpio);
735 set_gpio_edge(gpio, 1);
736 local_irq_restore(flags);
737 return ret;
738 } else
739 return get_gpio_data(gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400740}
741
742/* If we are booting from SPI and our board lacks a strong enough pull up,
743 * the core can reset and execute the bootrom faster than the resistor can
744 * pull the signal logically high. To work around this (common) error in
745 * board design, we explicitly set the pin back to GPIO mode, force /CS
746 * high, and wait for the electrons to do their thing.
747 *
748 * This function only makes sense to be called from reset code, but it
749 * lives here as we need to force all the GPIO states w/out going through
750 * BUG() checks and such.
751 */
752void bfin_reset_boot_spi_cs(unsigned short pin)
753{
754 unsigned short gpio = P_IDENT(pin);
755 port_setup(gpio, GPIO_USAGE);
756 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
757 AWA_DUMMY_READ(data_set);
758 udelay(1);
759}
760
Sonic Zhangda34aae2013-05-02 13:46:21 +0800761void gpio_labels(void)
Mike Frysinger4638b212010-06-02 04:17:26 -0400762{
763 int c, gpio;
764
765 for (c = 0; c < MAX_RESOURCES; c++) {
766 gpio = is_reserved(gpio, c, 1);
767 if (!check_gpio(c) && gpio)
768 printf("GPIO_%d:\t%s\tGPIO %s\n", c,
769 get_label(c),
770 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
771 else if (is_reserved(peri, c, 1))
772 printf("GPIO_%d:\t%s\tPeripheral\n", c, get_label(c));
773 else
774 continue;
775 }
776}