1/*
2 * GPIO Abstraction Layer
3 *
4 * Copyright 2006-2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later
7 */
8
9#include <linux/delay.h>
10#include <linux/module.h>
11#include <linux/err.h>
12#include <linux/proc_fs.h>
13#include <linux/seq_file.h>
14#include <linux/gpio.h>
15#include <linux/irq.h>
16
17#if ANOMALY_05000311 || ANOMALY_05000323
18enum {
19	AWA_data = SYSCR,
20	AWA_data_clear = SYSCR,
21	AWA_data_set = SYSCR,
22	AWA_toggle = SYSCR,
23	AWA_maska = BFIN_UART_SCR,
24	AWA_maska_clear = BFIN_UART_SCR,
25	AWA_maska_set = BFIN_UART_SCR,
26	AWA_maska_toggle = BFIN_UART_SCR,
27	AWA_maskb = BFIN_UART_GCTL,
28	AWA_maskb_clear = BFIN_UART_GCTL,
29	AWA_maskb_set = BFIN_UART_GCTL,
30	AWA_maskb_toggle = BFIN_UART_GCTL,
31	AWA_dir = SPORT1_STAT,
32	AWA_polar = SPORT1_STAT,
33	AWA_edge = SPORT1_STAT,
34	AWA_both = SPORT1_STAT,
35#if ANOMALY_05000311
36	AWA_inen = TIMER_ENABLE,
37#elif ANOMALY_05000323
38	AWA_inen = DMA1_1_CONFIG,
39#endif
40};
41	/* Anomaly Workaround */
42#define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
43#else
44#define AWA_DUMMY_READ(...)  do { } while (0)
45#endif
46
47static struct gpio_port_t * const gpio_array[] = {
48#if defined(BF533_FAMILY) || defined(BF538_FAMILY)
49	(struct gpio_port_t *) FIO_FLAG_D,
50#elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
51	(struct gpio_port_t *) PORTFIO,
52	(struct gpio_port_t *) PORTGIO,
53	(struct gpio_port_t *) PORTHIO,
54#elif defined(BF561_FAMILY)
55	(struct gpio_port_t *) FIO0_FLAG_D,
56	(struct gpio_port_t *) FIO1_FLAG_D,
57	(struct gpio_port_t *) FIO2_FLAG_D,
58#else
59# error no gpio arrays defined
60#endif
61};
62
63#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
64static unsigned short * const port_fer[] = {
65	(unsigned short *) PORTF_FER,
66	(unsigned short *) PORTG_FER,
67	(unsigned short *) PORTH_FER,
68};
69
70# if !defined(BF537_FAMILY)
71static unsigned short * const port_mux[] = {
72	(unsigned short *) PORTF_MUX,
73	(unsigned short *) PORTG_MUX,
74	(unsigned short *) PORTH_MUX,
75};
76
77static const
78u8 pmux_offset[][16] = {
79#  if defined(CONFIG_BF52x)
80	{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
81	{ 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
82	{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
83#  elif defined(CONFIG_BF51x)
84	{ 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
85	{ 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
86	{ 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
87#  endif
88};
89# endif
90
91#elif defined(BF538_FAMILY)
92static unsigned short * const port_fer[] = {
93	(unsigned short *) PORTCIO_FER,
94	(unsigned short *) PORTDIO_FER,
95	(unsigned short *) PORTEIO_FER,
96};
97#endif
98
99#define RESOURCE_LABEL_SIZE	16
100
101static struct str_ident {
102	char name[RESOURCE_LABEL_SIZE];
103} str_ident[MAX_RESOURCES];
104
105#if defined(CONFIG_PM)
106static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
107# ifdef BF538_FAMILY
108static unsigned short port_fer_saved[3];
109# endif
110#endif
111
112static void gpio_error(unsigned gpio)
113{
114	printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
115}
116
117static void set_label(unsigned short ident, const char *label)
118{
119	if (label) {
120		strncpy(str_ident[ident].name, label,
121			 RESOURCE_LABEL_SIZE);
122		str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
123	}
124}
125
126static char *get_label(unsigned short ident)
127{
128	return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
129}
130
131static int cmp_label(unsigned short ident, const char *label)
132{
133	if (label == NULL) {
134		dump_stack();
135		printk(KERN_ERR "Please provide none-null label\n");
136	}
137
138	if (label)
139		return strcmp(str_ident[ident].name, label);
140	else
141		return -EINVAL;
142}
143
144#define map_entry(m, i)      reserved_##m##_map[gpio_bank(i)]
145#define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
146#define reserve(m, i)        (map_entry(m, i) |= gpio_bit(i))
147#define unreserve(m, i)      (map_entry(m, i) &= ~gpio_bit(i))
148#define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
149
150DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
151DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
152DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
153
154inline int check_gpio(unsigned gpio)
155{
156	if (gpio >= MAX_BLACKFIN_GPIOS)
157		return -EINVAL;
158	return 0;
159}
160
161static void port_setup(unsigned gpio, unsigned short usage)
162{
163#if defined(BF538_FAMILY)
164	/*
165	 * BF538/9 Port C,D and E are special.
166	 * Inverted PORT_FER polarity on CDE and no PORF_FER on F
167	 * Regular PORT F GPIOs are handled here, CDE are exclusively
168	 * managed by GPIOLIB
169	 */
170
171	if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
172		return;
173
174	gpio -= MAX_BLACKFIN_GPIOS;
175
176	if (usage == GPIO_USAGE)
177		*port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
178	else
179		*port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
180	SSYNC();
181	return;
182#endif
183
184	if (check_gpio(gpio))
185		return;
186
187#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
188	if (usage == GPIO_USAGE)
189		*port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
190	else
191		*port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
192	SSYNC();
193#endif
194}
195
196#ifdef BF537_FAMILY
197static const s8 port_mux[] = {
198	[GPIO_PF0] = 3,
199	[GPIO_PF1] = 3,
200	[GPIO_PF2] = 4,
201	[GPIO_PF3] = 4,
202	[GPIO_PF4] = 5,
203	[GPIO_PF5] = 6,
204	[GPIO_PF6] = 7,
205	[GPIO_PF7] = 8,
206	[GPIO_PF8 ... GPIO_PF15] = -1,
207	[GPIO_PG0 ... GPIO_PG7] = -1,
208	[GPIO_PG8] = 9,
209	[GPIO_PG9] = 9,
210	[GPIO_PG10] = 10,
211	[GPIO_PG11] = 10,
212	[GPIO_PG12] = 10,
213	[GPIO_PG13] = 11,
214	[GPIO_PG14] = 11,
215	[GPIO_PG15] = 11,
216	[GPIO_PH0 ... GPIO_PH15] = -1,
217	[PORT_PJ0 ... PORT_PJ3] = -1,
218	[PORT_PJ4] = 1,
219	[PORT_PJ5] = 1,
220	[PORT_PJ6 ... PORT_PJ9] = -1,
221	[PORT_PJ10] = 0,
222	[PORT_PJ11] = 0,
223};
224
225static int portmux_group_check(unsigned short per)
226{
227	u16 ident = P_IDENT(per);
228	u16 function = P_FUNCT2MUX(per);
229	s8 offset = port_mux[ident];
230	u16 m, pmux, pfunc, mask;
231
232	if (offset < 0)
233		return 0;
234
235	pmux = bfin_read_PORT_MUX();
236	for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
237		if (m == ident)
238			continue;
239		if (port_mux[m] != offset)
240			continue;
241		if (!is_reserved(peri, m, 1))
242			continue;
243
244		if (offset == 1)
245			mask = 3;
246		else
247			mask = 1;
248
249		pfunc = (pmux >> offset) & mask;
250		if (pfunc != (function & mask)) {
251			pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
252				ident, function, m, pfunc);
253			return -EINVAL;
254		}
255	}
256
257	return 0;
258}
259
260static void portmux_setup(unsigned short per)
261{
262	u16 ident = P_IDENT(per);
263	u16 function = P_FUNCT2MUX(per);
264	s8 offset = port_mux[ident];
265	u16 pmux, mask;
266
267	if (offset == -1)
268		return;
269
270	pmux = bfin_read_PORT_MUX();
271	if (offset == 1)
272		mask = 3;
273	else
274		mask = 1;
275
276	pmux &= ~(mask << offset);
277	pmux |= ((function & mask) << offset);
278
279	bfin_write_PORT_MUX(pmux);
280}
281#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
282static int portmux_group_check(unsigned short per)
283{
284	u16 ident = P_IDENT(per);
285	u16 function = P_FUNCT2MUX(per);
286	u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
287	u16 pin, gpiopin, pfunc;
288
289	for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
290		if (offset != pmux_offset[gpio_bank(ident)][pin])
291			continue;
292
293		gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
294		if (gpiopin == ident)
295			continue;
296		if (!is_reserved(peri, gpiopin, 1))
297			continue;
298
299		pfunc = *port_mux[gpio_bank(ident)];
300		pfunc = (pfunc >> offset) & 3;
301		if (pfunc != function) {
302			pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
303				ident, function, gpiopin, pfunc);
304			return -EINVAL;
305		}
306	}
307
308	return 0;
309}
310
311inline void portmux_setup(unsigned short per)
312{
313	u16 ident = P_IDENT(per);
314	u16 function = P_FUNCT2MUX(per);
315	u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
316	u16 pmux;
317
318	pmux = *port_mux[gpio_bank(ident)];
319	if  (((pmux >> offset) & 3) == function)
320		return;
321	pmux &= ~(3 << offset);
322	pmux |= (function & 3) << offset;
323	*port_mux[gpio_bank(ident)] = pmux;
324	SSYNC();
325}
326#else
327# define portmux_setup(...)  do { } while (0)
328static int portmux_group_check(unsigned short per)
329{
330	return 0;
331}
332#endif
333
334/***********************************************************
335*
336* FUNCTIONS: Blackfin General Purpose Ports Access Functions
337*
338* INPUTS/OUTPUTS:
339* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
340*
341*
342* DESCRIPTION: These functions abstract direct register access
343*              to Blackfin processor General Purpose
344*              Ports Regsiters
345*
346* CAUTION: These functions do not belong to the GPIO Driver API
347*************************************************************
348* MODIFICATION HISTORY :
349**************************************************************/
350
351/* Set a specific bit */
352
353#define SET_GPIO(name) \
354void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
355{ \
356	unsigned long flags; \
357	flags = hard_local_irq_save(); \
358	if (arg) \
359		gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
360	else \
361		gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
362	AWA_DUMMY_READ(name); \
363	hard_local_irq_restore(flags); \
364} \
365EXPORT_SYMBOL(set_gpio_ ## name);
366
367SET_GPIO(dir)   /* set_gpio_dir() */
368SET_GPIO(inen)  /* set_gpio_inen() */
369SET_GPIO(polar) /* set_gpio_polar() */
370SET_GPIO(edge)  /* set_gpio_edge() */
371SET_GPIO(both)  /* set_gpio_both() */
372
373
374#define SET_GPIO_SC(name) \
375void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
376{ \
377	unsigned long flags; \
378	if (ANOMALY_05000311 || ANOMALY_05000323) \
379		flags = hard_local_irq_save(); \
380	if (arg) \
381		gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
382	else \
383		gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
384	if (ANOMALY_05000311 || ANOMALY_05000323) { \
385		AWA_DUMMY_READ(name); \
386		hard_local_irq_restore(flags); \
387	} \
388} \
389EXPORT_SYMBOL(set_gpio_ ## name);
390
391SET_GPIO_SC(maska)
392SET_GPIO_SC(maskb)
393SET_GPIO_SC(data)
394
395void set_gpio_toggle(unsigned gpio)
396{
397	unsigned long flags;
398	if (ANOMALY_05000311 || ANOMALY_05000323)
399		flags = hard_local_irq_save();
400	gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
401	if (ANOMALY_05000311 || ANOMALY_05000323) {
402		AWA_DUMMY_READ(toggle);
403		hard_local_irq_restore(flags);
404	}
405}
406EXPORT_SYMBOL(set_gpio_toggle);
407
408
409/*Set current PORT date (16-bit word)*/
410
411#define SET_GPIO_P(name) \
412void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
413{ \
414	unsigned long flags; \
415	if (ANOMALY_05000311 || ANOMALY_05000323) \
416		flags = hard_local_irq_save(); \
417	gpio_array[gpio_bank(gpio)]->name = arg; \
418	if (ANOMALY_05000311 || ANOMALY_05000323) { \
419		AWA_DUMMY_READ(name); \
420		hard_local_irq_restore(flags); \
421	} \
422} \
423EXPORT_SYMBOL(set_gpiop_ ## name);
424
425SET_GPIO_P(data)
426SET_GPIO_P(dir)
427SET_GPIO_P(inen)
428SET_GPIO_P(polar)
429SET_GPIO_P(edge)
430SET_GPIO_P(both)
431SET_GPIO_P(maska)
432SET_GPIO_P(maskb)
433
434/* Get a specific bit */
435#define GET_GPIO(name) \
436unsigned short get_gpio_ ## name(unsigned gpio) \
437{ \
438	unsigned long flags; \
439	unsigned short ret; \
440	if (ANOMALY_05000311 || ANOMALY_05000323) \
441		flags = hard_local_irq_save(); \
442	ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
443	if (ANOMALY_05000311 || ANOMALY_05000323) { \
444		AWA_DUMMY_READ(name); \
445		hard_local_irq_restore(flags); \
446	} \
447	return ret; \
448} \
449EXPORT_SYMBOL(get_gpio_ ## name);
450
451GET_GPIO(data)
452GET_GPIO(dir)
453GET_GPIO(inen)
454GET_GPIO(polar)
455GET_GPIO(edge)
456GET_GPIO(both)
457GET_GPIO(maska)
458GET_GPIO(maskb)
459
460/*Get current PORT date (16-bit word)*/
461
462#define GET_GPIO_P(name) \
463unsigned short get_gpiop_ ## name(unsigned gpio) \
464{ \
465	unsigned long flags; \
466	unsigned short ret; \
467	if (ANOMALY_05000311 || ANOMALY_05000323) \
468		flags = hard_local_irq_save(); \
469	ret = (gpio_array[gpio_bank(gpio)]->name); \
470	if (ANOMALY_05000311 || ANOMALY_05000323) { \
471		AWA_DUMMY_READ(name); \
472		hard_local_irq_restore(flags); \
473	} \
474	return ret; \
475} \
476EXPORT_SYMBOL(get_gpiop_ ## name);
477
478GET_GPIO_P(data)
479GET_GPIO_P(dir)
480GET_GPIO_P(inen)
481GET_GPIO_P(polar)
482GET_GPIO_P(edge)
483GET_GPIO_P(both)
484GET_GPIO_P(maska)
485GET_GPIO_P(maskb)
486
487
488#ifdef CONFIG_PM
489DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
490
491static const unsigned int sic_iwr_irqs[] = {
492#if defined(BF533_FAMILY)
493	IRQ_PROG_INTB
494#elif defined(BF537_FAMILY)
495	IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
496#elif defined(BF538_FAMILY)
497	IRQ_PORTF_INTB
498#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
499	IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
500#elif defined(BF561_FAMILY)
501	IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
502#else
503# error no SIC_IWR defined
504#endif
505};
506
507/***********************************************************
508*
509* FUNCTIONS: Blackfin PM Setup API
510*
511* INPUTS/OUTPUTS:
512* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
513* type -
514*	PM_WAKE_RISING
515*	PM_WAKE_FALLING
516*	PM_WAKE_HIGH
517*	PM_WAKE_LOW
518*	PM_WAKE_BOTH_EDGES
519*
520* DESCRIPTION: Blackfin PM Driver API
521*
522* CAUTION:
523*************************************************************
524* MODIFICATION HISTORY :
525**************************************************************/
526int bfin_gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
527{
528	unsigned long flags;
529
530	if (check_gpio(gpio) < 0)
531		return -EINVAL;
532
533	flags = hard_local_irq_save();
534	if (ctrl)
535		reserve(wakeup, gpio);
536	else
537		unreserve(wakeup, gpio);
538
539	set_gpio_maskb(gpio, ctrl);
540	hard_local_irq_restore(flags);
541
542	return 0;
543}
544
545int bfin_gpio_pm_standby_ctrl(unsigned ctrl)
546{
547	u16 bank, mask, i;
548
549	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
550		mask = map_entry(wakeup, i);
551		bank = gpio_bank(i);
552
553		if (mask)
554			bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
555	}
556	return 0;
557}
558
559void bfin_gpio_pm_hibernate_suspend(void)
560{
561	int i, bank;
562
563#ifdef BF538_FAMILY
564	for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
565		port_fer_saved[i] = *port_fer[i];
566#endif
567
568	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
569		bank = gpio_bank(i);
570
571#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
572		gpio_bank_saved[bank].fer = *port_fer[bank];
573#if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
574		gpio_bank_saved[bank].mux = *port_mux[bank];
575#else
576		if (bank == 0)
577			gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
578#endif
579#endif
580		gpio_bank_saved[bank].data  = gpio_array[bank]->data;
581		gpio_bank_saved[bank].inen  = gpio_array[bank]->inen;
582		gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
583		gpio_bank_saved[bank].dir   = gpio_array[bank]->dir;
584		gpio_bank_saved[bank].edge  = gpio_array[bank]->edge;
585		gpio_bank_saved[bank].both  = gpio_array[bank]->both;
586		gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
587	}
588
589#ifdef BFIN_SPECIAL_GPIO_BANKS
590	bfin_special_gpio_pm_hibernate_suspend();
591#endif
592
593	AWA_DUMMY_READ(maska);
594}
595
596void bfin_gpio_pm_hibernate_restore(void)
597{
598	int i, bank;
599
600#ifdef BF538_FAMILY
601	for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
602		*port_fer[i] = port_fer_saved[i];
603#endif
604
605	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
606		bank = gpio_bank(i);
607
608#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
609#if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
610		*port_mux[bank] = gpio_bank_saved[bank].mux;
611#else
612		if (bank == 0)
613			bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
614#endif
615		*port_fer[bank] = gpio_bank_saved[bank].fer;
616#endif
617		gpio_array[bank]->inen  = gpio_bank_saved[bank].inen;
618		gpio_array[bank]->data_set = gpio_bank_saved[bank].data
619						& gpio_bank_saved[bank].dir;
620		gpio_array[bank]->dir   = gpio_bank_saved[bank].dir;
621		gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
622		gpio_array[bank]->edge  = gpio_bank_saved[bank].edge;
623		gpio_array[bank]->both  = gpio_bank_saved[bank].both;
624		gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
625	}
626
627#ifdef BFIN_SPECIAL_GPIO_BANKS
628	bfin_special_gpio_pm_hibernate_restore();
629#endif
630
631	AWA_DUMMY_READ(maska);
632}
633
634
635#endif
636
637/***********************************************************
638*
639* FUNCTIONS:	Blackfin Peripheral Resource Allocation
640*		and PortMux Setup
641*
642* INPUTS/OUTPUTS:
643* per	Peripheral Identifier
644* label	String
645*
646* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
647*
648* CAUTION:
649*************************************************************
650* MODIFICATION HISTORY :
651**************************************************************/
652
653int peripheral_request(unsigned short per, const char *label)
654{
655	unsigned long flags;
656	unsigned short ident = P_IDENT(per);
657
658	/*
659	 * Don't cares are pins with only one dedicated function
660	 */
661
662	if (per & P_DONTCARE)
663		return 0;
664
665	if (!(per & P_DEFINED))
666		return -ENODEV;
667
668	BUG_ON(ident >= MAX_RESOURCES);
669
670	flags = hard_local_irq_save();
671
672	/* If a pin can be muxed as either GPIO or peripheral, make
673	 * sure it is not already a GPIO pin when we request it.
674	 */
675	if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
676		if (system_state == SYSTEM_BOOTING)
677			dump_stack();
678		printk(KERN_ERR
679		       "%s: Peripheral %d is already reserved as GPIO by %s !\n",
680		       __func__, ident, get_label(ident));
681		hard_local_irq_restore(flags);
682		return -EBUSY;
683	}
684
685	if (unlikely(is_reserved(peri, ident, 1))) {
686
687		/*
688		 * Pin functions like AMC address strobes my
689		 * be requested and used by several drivers
690		 */
691
692		if (!(per & P_MAYSHARE)) {
693			/*
694			 * Allow that the identical pin function can
695			 * be requested from the same driver twice
696			 */
697
698			if (cmp_label(ident, label) == 0)
699				goto anyway;
700
701			if (system_state == SYSTEM_BOOTING)
702				dump_stack();
703			printk(KERN_ERR
704			       "%s: Peripheral %d function %d is already reserved by %s !\n",
705			       __func__, ident, P_FUNCT2MUX(per), get_label(ident));
706			hard_local_irq_restore(flags);
707			return -EBUSY;
708		}
709	}
710
711	if (unlikely(portmux_group_check(per))) {
712		hard_local_irq_restore(flags);
713		return -EBUSY;
714	}
715 anyway:
716	reserve(peri, ident);
717
718	portmux_setup(per);
719	port_setup(ident, PERIPHERAL_USAGE);
720
721	hard_local_irq_restore(flags);
722	set_label(ident, label);
723
724	return 0;
725}
726EXPORT_SYMBOL(peripheral_request);
727
728int peripheral_request_list(const unsigned short per[], const char *label)
729{
730	u16 cnt;
731	int ret;
732
733	for (cnt = 0; per[cnt] != 0; cnt++) {
734
735		ret = peripheral_request(per[cnt], label);
736
737		if (ret < 0) {
738			for ( ; cnt > 0; cnt--)
739				peripheral_free(per[cnt - 1]);
740
741			return ret;
742		}
743	}
744
745	return 0;
746}
747EXPORT_SYMBOL(peripheral_request_list);
748
749void peripheral_free(unsigned short per)
750{
751	unsigned long flags;
752	unsigned short ident = P_IDENT(per);
753
754	if (per & P_DONTCARE)
755		return;
756
757	if (!(per & P_DEFINED))
758		return;
759
760	flags = hard_local_irq_save();
761
762	if (unlikely(!is_reserved(peri, ident, 0))) {
763		hard_local_irq_restore(flags);
764		return;
765	}
766
767	if (!(per & P_MAYSHARE))
768		port_setup(ident, GPIO_USAGE);
769
770	unreserve(peri, ident);
771
772	set_label(ident, "free");
773
774	hard_local_irq_restore(flags);
775}
776EXPORT_SYMBOL(peripheral_free);
777
778void peripheral_free_list(const unsigned short per[])
779{
780	u16 cnt;
781	for (cnt = 0; per[cnt] != 0; cnt++)
782		peripheral_free(per[cnt]);
783}
784EXPORT_SYMBOL(peripheral_free_list);
785
786/***********************************************************
787*
788* FUNCTIONS: Blackfin GPIO Driver
789*
790* INPUTS/OUTPUTS:
791* gpio	PIO Number between 0 and MAX_BLACKFIN_GPIOS
792* label	String
793*
794* DESCRIPTION: Blackfin GPIO Driver API
795*
796* CAUTION:
797*************************************************************
798* MODIFICATION HISTORY :
799**************************************************************/
800
801int bfin_gpio_request(unsigned gpio, const char *label)
802{
803	unsigned long flags;
804
805	if (check_gpio(gpio) < 0)
806		return -EINVAL;
807
808	flags = hard_local_irq_save();
809
810	/*
811	 * Allow that the identical GPIO can
812	 * be requested from the same driver twice
813	 * Do nothing and return -
814	 */
815
816	if (cmp_label(gpio, label) == 0) {
817		hard_local_irq_restore(flags);
818		return 0;
819	}
820
821	if (unlikely(is_reserved(gpio, gpio, 1))) {
822		if (system_state == SYSTEM_BOOTING)
823			dump_stack();
824		printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
825		       gpio, get_label(gpio));
826		hard_local_irq_restore(flags);
827		return -EBUSY;
828	}
829	if (unlikely(is_reserved(peri, gpio, 1))) {
830		if (system_state == SYSTEM_BOOTING)
831			dump_stack();
832		printk(KERN_ERR
833		       "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
834		       gpio, get_label(gpio));
835		hard_local_irq_restore(flags);
836		return -EBUSY;
837	}
838	if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
839		printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
840		       " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
841	} else {	/* Reset POLAR setting when acquiring a gpio for the first time */
842		set_gpio_polar(gpio, 0);
843	}
844
845	reserve(gpio, gpio);
846	set_label(gpio, label);
847
848	hard_local_irq_restore(flags);
849
850	port_setup(gpio, GPIO_USAGE);
851
852	return 0;
853}
854EXPORT_SYMBOL(bfin_gpio_request);
855
856void bfin_gpio_free(unsigned gpio)
857{
858	unsigned long flags;
859
860	if (check_gpio(gpio) < 0)
861		return;
862
863	might_sleep();
864
865	flags = hard_local_irq_save();
866
867	if (unlikely(!is_reserved(gpio, gpio, 0))) {
868		if (system_state == SYSTEM_BOOTING)
869			dump_stack();
870		gpio_error(gpio);
871		hard_local_irq_restore(flags);
872		return;
873	}
874
875	unreserve(gpio, gpio);
876
877	set_label(gpio, "free");
878
879	hard_local_irq_restore(flags);
880}
881EXPORT_SYMBOL(bfin_gpio_free);
882
883#ifdef BFIN_SPECIAL_GPIO_BANKS
884DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
885
886int bfin_special_gpio_request(unsigned gpio, const char *label)
887{
888	unsigned long flags;
889
890	flags = hard_local_irq_save();
891
892	/*
893	 * Allow that the identical GPIO can
894	 * be requested from the same driver twice
895	 * Do nothing and return -
896	 */
897
898	if (cmp_label(gpio, label) == 0) {
899		hard_local_irq_restore(flags);
900		return 0;
901	}
902
903	if (unlikely(is_reserved(special_gpio, gpio, 1))) {
904		hard_local_irq_restore(flags);
905		printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
906		       gpio, get_label(gpio));
907
908		return -EBUSY;
909	}
910	if (unlikely(is_reserved(peri, gpio, 1))) {
911		hard_local_irq_restore(flags);
912		printk(KERN_ERR
913		       "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
914		       gpio, get_label(gpio));
915
916		return -EBUSY;
917	}
918
919	reserve(special_gpio, gpio);
920	reserve(peri, gpio);
921
922	set_label(gpio, label);
923	hard_local_irq_restore(flags);
924	port_setup(gpio, GPIO_USAGE);
925
926	return 0;
927}
928EXPORT_SYMBOL(bfin_special_gpio_request);
929
930void bfin_special_gpio_free(unsigned gpio)
931{
932	unsigned long flags;
933
934	might_sleep();
935
936	flags = hard_local_irq_save();
937
938	if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
939		gpio_error(gpio);
940		hard_local_irq_restore(flags);
941		return;
942	}
943
944	unreserve(special_gpio, gpio);
945	unreserve(peri, gpio);
946	set_label(gpio, "free");
947	hard_local_irq_restore(flags);
948}
949EXPORT_SYMBOL(bfin_special_gpio_free);
950#endif
951
952
953int bfin_gpio_irq_request(unsigned gpio, const char *label)
954{
955	unsigned long flags;
956
957	if (check_gpio(gpio) < 0)
958		return -EINVAL;
959
960	flags = hard_local_irq_save();
961
962	if (unlikely(is_reserved(peri, gpio, 1))) {
963		if (system_state == SYSTEM_BOOTING)
964			dump_stack();
965		printk(KERN_ERR
966		       "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
967		       gpio, get_label(gpio));
968		hard_local_irq_restore(flags);
969		return -EBUSY;
970	}
971	if (unlikely(is_reserved(gpio, gpio, 1)))
972		printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
973		       "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
974		       gpio, get_label(gpio));
975
976	reserve(gpio_irq, gpio);
977	set_label(gpio, label);
978
979	hard_local_irq_restore(flags);
980
981	port_setup(gpio, GPIO_USAGE);
982
983	return 0;
984}
985
986void bfin_gpio_irq_free(unsigned gpio)
987{
988	unsigned long flags;
989
990	if (check_gpio(gpio) < 0)
991		return;
992
993	flags = hard_local_irq_save();
994
995	if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
996		if (system_state == SYSTEM_BOOTING)
997			dump_stack();
998		gpio_error(gpio);
999		hard_local_irq_restore(flags);
1000		return;
1001	}
1002
1003	unreserve(gpio_irq, gpio);
1004
1005	set_label(gpio, "free");
1006
1007	hard_local_irq_restore(flags);
1008}
1009
1010static inline void __bfin_gpio_direction_input(unsigned gpio)
1011{
1012	gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
1013	gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
1014}
1015
1016int bfin_gpio_direction_input(unsigned gpio)
1017{
1018	unsigned long flags;
1019
1020	if (unlikely(!is_reserved(gpio, gpio, 0))) {
1021		gpio_error(gpio);
1022		return -EINVAL;
1023	}
1024
1025	flags = hard_local_irq_save();
1026	__bfin_gpio_direction_input(gpio);
1027	AWA_DUMMY_READ(inen);
1028	hard_local_irq_restore(flags);
1029
1030	return 0;
1031}
1032EXPORT_SYMBOL(bfin_gpio_direction_input);
1033
1034void bfin_gpio_irq_prepare(unsigned gpio)
1035{
1036	port_setup(gpio, GPIO_USAGE);
1037}
1038
1039void bfin_gpio_set_value(unsigned gpio, int arg)
1040{
1041	if (arg)
1042		gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1043	else
1044		gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1045}
1046EXPORT_SYMBOL(bfin_gpio_set_value);
1047
1048int bfin_gpio_direction_output(unsigned gpio, int value)
1049{
1050	unsigned long flags;
1051
1052	if (unlikely(!is_reserved(gpio, gpio, 0))) {
1053		gpio_error(gpio);
1054		return -EINVAL;
1055	}
1056
1057	flags = hard_local_irq_save();
1058
1059	gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
1060	gpio_set_value(gpio, value);
1061	gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
1062
1063	AWA_DUMMY_READ(dir);
1064	hard_local_irq_restore(flags);
1065
1066	return 0;
1067}
1068EXPORT_SYMBOL(bfin_gpio_direction_output);
1069
1070int bfin_gpio_get_value(unsigned gpio)
1071{
1072	unsigned long flags;
1073
1074	if (unlikely(get_gpio_edge(gpio))) {
1075		int ret;
1076		flags = hard_local_irq_save();
1077		set_gpio_edge(gpio, 0);
1078		ret = get_gpio_data(gpio);
1079		set_gpio_edge(gpio, 1);
1080		hard_local_irq_restore(flags);
1081		return ret;
1082	} else
1083		return get_gpio_data(gpio);
1084}
1085EXPORT_SYMBOL(bfin_gpio_get_value);
1086
1087/* If we are booting from SPI and our board lacks a strong enough pull up,
1088 * the core can reset and execute the bootrom faster than the resistor can
1089 * pull the signal logically high.  To work around this (common) error in
1090 * board design, we explicitly set the pin back to GPIO mode, force /CS
1091 * high, and wait for the electrons to do their thing.
1092 *
1093 * This function only makes sense to be called from reset code, but it
1094 * lives here as we need to force all the GPIO states w/out going through
1095 * BUG() checks and such.
1096 */
1097void bfin_reset_boot_spi_cs(unsigned short pin)
1098{
1099	unsigned short gpio = P_IDENT(pin);
1100	port_setup(gpio, GPIO_USAGE);
1101	gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1102	AWA_DUMMY_READ(data_set);
1103	udelay(1);
1104}
1105
1106#if defined(CONFIG_PROC_FS)
1107static int gpio_proc_show(struct seq_file *m, void *v)
1108{
1109	int c, irq, gpio;
1110
1111	for (c = 0; c < MAX_RESOURCES; c++) {
1112		irq = is_reserved(gpio_irq, c, 1);
1113		gpio = is_reserved(gpio, c, 1);
1114		if (!check_gpio(c) && (gpio || irq))
1115			seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
1116				 get_label(c), (gpio && irq) ? " *" : "",
1117				 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1118		else if (is_reserved(peri, c, 1))
1119			seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1120		else
1121			continue;
1122	}
1123
1124	return 0;
1125}
1126
1127static int gpio_proc_open(struct inode *inode, struct file *file)
1128{
1129	return single_open(file, gpio_proc_show, NULL);
1130}
1131
1132static const struct file_operations gpio_proc_ops = {
1133	.open		= gpio_proc_open,
1134	.read		= seq_read,
1135	.llseek		= seq_lseek,
1136	.release	= single_release,
1137};
1138
1139static __init int gpio_register_proc(void)
1140{
1141	struct proc_dir_entry *proc_gpio;
1142
1143	proc_gpio = proc_create("gpio", 0, NULL, &gpio_proc_ops);
1144	return proc_gpio == NULL;
1145}
1146__initcall(gpio_register_proc);
1147#endif
1148
1149#ifdef CONFIG_GPIOLIB
1150static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
1151{
1152	return bfin_gpio_direction_input(gpio);
1153}
1154
1155static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
1156{
1157	return bfin_gpio_direction_output(gpio, level);
1158}
1159
1160static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
1161{
1162	return bfin_gpio_get_value(gpio);
1163}
1164
1165static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
1166{
1167	return bfin_gpio_set_value(gpio, value);
1168}
1169
1170static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
1171{
1172	return bfin_gpio_request(gpio, chip->label);
1173}
1174
1175static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
1176{
1177	return bfin_gpio_free(gpio);
1178}
1179
1180static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
1181{
1182	return gpio + GPIO_IRQ_BASE;
1183}
1184
1185static struct gpio_chip bfin_chip = {
1186	.label			= "BFIN-GPIO",
1187	.direction_input	= bfin_gpiolib_direction_input,
1188	.get			= bfin_gpiolib_get_value,
1189	.direction_output	= bfin_gpiolib_direction_output,
1190	.set			= bfin_gpiolib_set_value,
1191	.request		= bfin_gpiolib_gpio_request,
1192	.free			= bfin_gpiolib_gpio_free,
1193	.to_irq			= bfin_gpiolib_gpio_to_irq,
1194	.base			= 0,
1195	.ngpio			= MAX_BLACKFIN_GPIOS,
1196};
1197
1198static int __init bfin_gpiolib_setup(void)
1199{
1200	return gpiochip_add(&bfin_chip);
1201}
1202arch_initcall(bfin_gpiolib_setup);
1203#endif
1204