blob: a4d10d51608d6fbeabdddd394ed19dafb8cfafde [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{
250 u16 y, offset, muxreg;
251 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
261 if (offset != 1)
262 muxreg &= ~(1 << offset);
263 else
264 muxreg &= ~(3 << 1);
265
266 muxreg |= (function << offset);
267 bfin_write_PORT_MUX(muxreg);
268 }
269 }
270}
Mike Frysinger4638b212010-06-02 04:17:26 -0400271#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
272inline void portmux_setup(unsigned short per)
273{
274 u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
275 u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
276
277 pmux = *port_mux[gpio_bank(ident)];
278 pmux &= ~(3 << offset);
279 pmux |= (function & 3) << offset;
280 *port_mux[gpio_bank(ident)] = pmux;
281 SSYNC();
282}
283#else
284# define portmux_setup(...) do { } while (0)
285#endif
286
Mike Frysinger4638b212010-06-02 04:17:26 -0400287/***********************************************************
288*
289* FUNCTIONS: Blackfin General Purpose Ports Access Functions
290*
291* INPUTS/OUTPUTS:
292* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
293*
294*
295* DESCRIPTION: These functions abstract direct register access
296* to Blackfin processor General Purpose
297* Ports Regsiters
298*
299* CAUTION: These functions do not belong to the GPIO Driver API
300*************************************************************
301* MODIFICATION HISTORY :
302**************************************************************/
303
304/* Set a specific bit */
305
306#define SET_GPIO(name) \
307void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
308{ \
309 unsigned long flags; \
310 local_irq_save(flags); \
311 if (arg) \
312 gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
313 else \
314 gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
315 AWA_DUMMY_READ(name); \
316 local_irq_restore(flags); \
317}
318
319SET_GPIO(dir) /* set_gpio_dir() */
320SET_GPIO(inen) /* set_gpio_inen() */
321SET_GPIO(polar) /* set_gpio_polar() */
322SET_GPIO(edge) /* set_gpio_edge() */
323SET_GPIO(both) /* set_gpio_both() */
324
325
326#define SET_GPIO_SC(name) \
327void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
328{ \
329 unsigned long flags; \
330 if (ANOMALY_05000311 || ANOMALY_05000323) \
331 local_irq_save(flags); \
332 if (arg) \
333 gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
334 else \
335 gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
336 if (ANOMALY_05000311 || ANOMALY_05000323) { \
337 AWA_DUMMY_READ(name); \
338 local_irq_restore(flags); \
339 } \
340}
341
342SET_GPIO_SC(maska)
343SET_GPIO_SC(maskb)
344SET_GPIO_SC(data)
345
346void set_gpio_toggle(unsigned gpio)
347{
348 unsigned long flags;
349 if (ANOMALY_05000311 || ANOMALY_05000323)
350 local_irq_save(flags);
351 gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
352 if (ANOMALY_05000311 || ANOMALY_05000323) {
353 AWA_DUMMY_READ(toggle);
354 local_irq_restore(flags);
355 }
356}
357
358/* Set current PORT date (16-bit word) */
359
360#define SET_GPIO_P(name) \
361void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
362{ \
363 unsigned long flags; \
364 if (ANOMALY_05000311 || ANOMALY_05000323) \
365 local_irq_save(flags); \
366 gpio_array[gpio_bank(gpio)]->name = arg; \
367 if (ANOMALY_05000311 || ANOMALY_05000323) { \
368 AWA_DUMMY_READ(name); \
369 local_irq_restore(flags); \
370 } \
371}
372
373SET_GPIO_P(data)
374SET_GPIO_P(dir)
375SET_GPIO_P(inen)
376SET_GPIO_P(polar)
377SET_GPIO_P(edge)
378SET_GPIO_P(both)
379SET_GPIO_P(maska)
380SET_GPIO_P(maskb)
381
382/* Get a specific bit */
383#define GET_GPIO(name) \
384unsigned short get_gpio_ ## name(unsigned gpio) \
385{ \
386 unsigned long flags; \
387 unsigned short ret; \
388 if (ANOMALY_05000311 || ANOMALY_05000323) \
389 local_irq_save(flags); \
390 ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
391 if (ANOMALY_05000311 || ANOMALY_05000323) { \
392 AWA_DUMMY_READ(name); \
393 local_irq_restore(flags); \
394 } \
395 return ret; \
396}
397
398GET_GPIO(data)
399GET_GPIO(dir)
400GET_GPIO(inen)
401GET_GPIO(polar)
402GET_GPIO(edge)
403GET_GPIO(both)
404GET_GPIO(maska)
405GET_GPIO(maskb)
406
407/* Get current PORT date (16-bit word) */
408
409#define GET_GPIO_P(name) \
410unsigned short get_gpiop_ ## name(unsigned gpio) \
411{ \
412 unsigned long flags; \
413 unsigned short ret; \
414 if (ANOMALY_05000311 || ANOMALY_05000323) \
415 local_irq_save(flags); \
416 ret = (gpio_array[gpio_bank(gpio)]->name); \
417 if (ANOMALY_05000311 || ANOMALY_05000323) { \
418 AWA_DUMMY_READ(name); \
419 local_irq_restore(flags); \
420 } \
421 return ret; \
422}
423
424GET_GPIO_P(data)
425GET_GPIO_P(dir)
426GET_GPIO_P(inen)
427GET_GPIO_P(polar)
428GET_GPIO_P(edge)
429GET_GPIO_P(both)
430GET_GPIO_P(maska)
431GET_GPIO_P(maskb)
432
Mike Frysinger4638b212010-06-02 04:17:26 -0400433/***********************************************************
434*
435* FUNCTIONS: Blackfin Peripheral Resource Allocation
436* and PortMux Setup
437*
438* INPUTS/OUTPUTS:
439* per Peripheral Identifier
440* label String
441*
442* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
443*
444* CAUTION:
445*************************************************************
446* MODIFICATION HISTORY :
447**************************************************************/
448
449int peripheral_request(unsigned short per, const char *label)
450{
451 unsigned short ident = P_IDENT(per);
452
453 /*
454 * Don't cares are pins with only one dedicated function
455 */
456
457 if (per & P_DONTCARE)
458 return 0;
459
460 if (!(per & P_DEFINED))
461 return -ENODEV;
462
463 BUG_ON(ident >= MAX_RESOURCES);
464
465 /* If a pin can be muxed as either GPIO or peripheral, make
466 * sure it is not already a GPIO pin when we request it.
467 */
468 if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
469 printf("%s: Peripheral %d is already reserved as GPIO by %s !\n",
470 __func__, ident, get_label(ident));
471 return -EBUSY;
472 }
473
474 if (unlikely(is_reserved(peri, ident, 1))) {
475
476 /*
477 * Pin functions like AMC address strobes my
478 * be requested and used by several drivers
479 */
480
Mike Frysinger4638b212010-06-02 04:17:26 -0400481 if (!(per & P_MAYSHARE)) {
Mike Frysinger4638b212010-06-02 04:17:26 -0400482 /*
483 * Allow that the identical pin function can
484 * be requested from the same driver twice
485 */
486
487 if (cmp_label(ident, label) == 0)
488 goto anyway;
489
490 printf("%s: Peripheral %d function %d is already reserved by %s !\n",
491 __func__, ident, P_FUNCT2MUX(per), get_label(ident));
492 return -EBUSY;
493 }
494 }
495
496 anyway:
497 reserve(peri, ident);
498
499 portmux_setup(per);
500 port_setup(ident, PERIPHERAL_USAGE);
501
502 set_label(ident, label);
503
504 return 0;
505}
506
507int peripheral_request_list(const unsigned short per[], const char *label)
508{
509 u16 cnt;
510 int ret;
511
512 for (cnt = 0; per[cnt] != 0; cnt++) {
513
514 ret = peripheral_request(per[cnt], label);
515
516 if (ret < 0) {
517 for ( ; cnt > 0; cnt--)
518 peripheral_free(per[cnt - 1]);
519
520 return ret;
521 }
522 }
523
524 return 0;
525}
526
527void peripheral_free(unsigned short per)
528{
529 unsigned short ident = P_IDENT(per);
530
531 if (per & P_DONTCARE)
532 return;
533
534 if (!(per & P_DEFINED))
535 return;
536
537 if (unlikely(!is_reserved(peri, ident, 0)))
538 return;
539
540 if (!(per & P_MAYSHARE))
541 port_setup(ident, GPIO_USAGE);
542
543 unreserve(peri, ident);
544
545 set_label(ident, "free");
546}
547
548void peripheral_free_list(const unsigned short per[])
549{
550 u16 cnt;
551 for (cnt = 0; per[cnt] != 0; cnt++)
552 peripheral_free(per[cnt]);
553}
554
555/***********************************************************
556*
557* FUNCTIONS: Blackfin GPIO Driver
558*
559* INPUTS/OUTPUTS:
560* gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
561* label String
562*
563* DESCRIPTION: Blackfin GPIO Driver API
564*
565* CAUTION:
566*************************************************************
567* MODIFICATION HISTORY :
568**************************************************************/
569
Sonic Zhangda34aae2013-05-02 13:46:21 +0800570int gpio_request(unsigned gpio, const char *label)
Mike Frysinger4638b212010-06-02 04:17:26 -0400571{
572 if (check_gpio(gpio) < 0)
573 return -EINVAL;
574
575 /*
576 * Allow that the identical GPIO can
577 * be requested from the same driver twice
578 * Do nothing and return -
579 */
580
581 if (cmp_label(gpio, label) == 0)
582 return 0;
583
584 if (unlikely(is_reserved(gpio, gpio, 1))) {
585 printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
586 gpio, get_label(gpio));
587 return -EBUSY;
588 }
589 if (unlikely(is_reserved(peri, gpio, 1))) {
590 printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
591 gpio, get_label(gpio));
592 return -EBUSY;
593 }
Mike Frysinger4638b212010-06-02 04:17:26 -0400594 else { /* Reset POLAR setting when acquiring a gpio for the first time */
595 set_gpio_polar(gpio, 0);
596 }
Mike Frysinger4638b212010-06-02 04:17:26 -0400597
598 reserve(gpio, gpio);
599 set_label(gpio, label);
600
601 port_setup(gpio, GPIO_USAGE);
602
603 return 0;
604}
605
Sonic Zhangda34aae2013-05-02 13:46:21 +0800606int gpio_free(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400607{
608 if (check_gpio(gpio) < 0)
Sonic Zhangda34aae2013-05-02 13:46:21 +0800609 return -1;
Mike Frysinger4638b212010-06-02 04:17:26 -0400610
611 if (unlikely(!is_reserved(gpio, gpio, 0))) {
612 gpio_error(gpio);
Sonic Zhangda34aae2013-05-02 13:46:21 +0800613 return -1;
Mike Frysinger4638b212010-06-02 04:17:26 -0400614 }
615
616 unreserve(gpio, gpio);
617
618 set_label(gpio, "free");
Mike Frysinger4638b212010-06-02 04:17:26 -0400619
Sonic Zhangda34aae2013-05-02 13:46:21 +0800620 return 0;
621}
622
623#ifdef ADI_SPECIAL_GPIO_BANKS
Mike Frysinger4638b212010-06-02 04:17:26 -0400624DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
625
Sonic Zhangda34aae2013-05-02 13:46:21 +0800626int special_gpio_request(unsigned gpio, const char *label)
Mike Frysinger4638b212010-06-02 04:17:26 -0400627{
628 /*
629 * Allow that the identical GPIO can
630 * be requested from the same driver twice
631 * Do nothing and return -
632 */
633
634 if (cmp_label(gpio, label) == 0)
635 return 0;
636
637 if (unlikely(is_reserved(special_gpio, gpio, 1))) {
638 printf("bfin-gpio: GPIO %d is already reserved by %s !\n",
639 gpio, get_label(gpio));
640 return -EBUSY;
641 }
642 if (unlikely(is_reserved(peri, gpio, 1))) {
643 printf("bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
644 gpio, get_label(gpio));
645
646 return -EBUSY;
647 }
648
649 reserve(special_gpio, gpio);
650 reserve(peri, gpio);
651
652 set_label(gpio, label);
653 port_setup(gpio, GPIO_USAGE);
654
655 return 0;
656}
657
Sonic Zhangda34aae2013-05-02 13:46:21 +0800658void special_gpio_free(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400659{
660 if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
661 gpio_error(gpio);
662 return;
663 }
664
Axel Lin5460b8d2013-06-26 10:09:16 +0800665 unreserve(special_gpio, gpio);
666 unreserve(peri, gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400667 set_label(gpio, "free");
668}
669#endif
670
Sonic Zhangda34aae2013-05-02 13:46:21 +0800671static inline void __gpio_direction_input(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400672{
Mike Frysinger4638b212010-06-02 04:17:26 -0400673 gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400674 gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
675}
676
Sonic Zhangda34aae2013-05-02 13:46:21 +0800677int gpio_direction_input(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400678{
679 unsigned long flags;
680
681 if (!is_reserved(gpio, gpio, 0)) {
682 gpio_error(gpio);
683 return -EINVAL;
684 }
685
686 local_irq_save(flags);
Sonic Zhangda34aae2013-05-02 13:46:21 +0800687 __gpio_direction_input(gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400688 AWA_DUMMY_READ(inen);
689 local_irq_restore(flags);
690
691 return 0;
692}
693
Sonic Zhangda34aae2013-05-02 13:46:21 +0800694int gpio_set_value(unsigned gpio, int arg)
Mike Frysinger4638b212010-06-02 04:17:26 -0400695{
696 if (arg)
697 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
698 else
699 gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
Sonic Zhangda34aae2013-05-02 13:46:21 +0800700
701 return 0;
Mike Frysinger4638b212010-06-02 04:17:26 -0400702}
703
Sonic Zhangda34aae2013-05-02 13:46:21 +0800704int gpio_direction_output(unsigned gpio, int value)
Mike Frysinger4638b212010-06-02 04:17:26 -0400705{
706 unsigned long flags;
707
708 if (!is_reserved(gpio, gpio, 0)) {
709 gpio_error(gpio);
710 return -EINVAL;
711 }
712
713 local_irq_save(flags);
714
715 gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
716 gpio_set_value(gpio, value);
Mike Frysinger4638b212010-06-02 04:17:26 -0400717 gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400718
719 AWA_DUMMY_READ(dir);
720 local_irq_restore(flags);
721
722 return 0;
723}
724
Sonic Zhangda34aae2013-05-02 13:46:21 +0800725int gpio_get_value(unsigned gpio)
Mike Frysinger4638b212010-06-02 04:17:26 -0400726{
Mike Frysinger4638b212010-06-02 04:17:26 -0400727 unsigned long flags;
728
729 if (unlikely(get_gpio_edge(gpio))) {
730 int ret;
731 local_irq_save(flags);
732 set_gpio_edge(gpio, 0);
733 ret = get_gpio_data(gpio);
734 set_gpio_edge(gpio, 1);
735 local_irq_restore(flags);
736 return ret;
737 } else
738 return get_gpio_data(gpio);
Mike Frysinger4638b212010-06-02 04:17:26 -0400739}
740
741/* If we are booting from SPI and our board lacks a strong enough pull up,
742 * the core can reset and execute the bootrom faster than the resistor can
743 * pull the signal logically high. To work around this (common) error in
744 * board design, we explicitly set the pin back to GPIO mode, force /CS
745 * high, and wait for the electrons to do their thing.
746 *
747 * This function only makes sense to be called from reset code, but it
748 * lives here as we need to force all the GPIO states w/out going through
749 * BUG() checks and such.
750 */
751void bfin_reset_boot_spi_cs(unsigned short pin)
752{
753 unsigned short gpio = P_IDENT(pin);
754 port_setup(gpio, GPIO_USAGE);
755 gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
756 AWA_DUMMY_READ(data_set);
757 udelay(1);
758}
759
Sonic Zhangda34aae2013-05-02 13:46:21 +0800760void gpio_labels(void)
Mike Frysinger4638b212010-06-02 04:17:26 -0400761{
762 int c, gpio;
763
764 for (c = 0; c < MAX_RESOURCES; c++) {
765 gpio = is_reserved(gpio, c, 1);
766 if (!check_gpio(c) && gpio)
767 printf("GPIO_%d:\t%s\tGPIO %s\n", c,
768 get_label(c),
769 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
770 else if (is_reserved(peri, c, 1))
771 printf("GPIO_%d:\t%s\tPeripheral\n", c, get_label(c));
772 else
773 continue;
774 }
775}