bfin_gpio.c 30.9 KB
Newer Older
Bryan Wu's avatar
Bryan Wu committed
1 2 3 4 5 6 7 8 9
/*
 * File:         arch/blackfin/kernel/bfin_gpio.c
 * Based on:
 * Author:       Michael Hennerich (hennerich@blackfin.uclinux.org)
 *
 * Created:
 * Description:  GPIO Abstraction Layer
 *
 * Modified:
10
 *               Copyright 2008 Analog Devices Inc.
Bryan Wu's avatar
Bryan Wu committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Bugs:         Enter bugs at http://blackfin.uclinux.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see the file COPYING, or write
 * to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

30
#include <linux/delay.h>
Bryan Wu's avatar
Bryan Wu committed
31 32
#include <linux/module.h>
#include <linux/err.h>
33
#include <linux/proc_fs.h>
Bryan Wu's avatar
Bryan Wu committed
34 35
#include <asm/blackfin.h>
#include <asm/gpio.h>
36
#include <asm/portmux.h>
Bryan Wu's avatar
Bryan Wu committed
37 38
#include <linux/irq.h>

39 40 41 42 43 44
#if ANOMALY_05000311 || ANOMALY_05000323
enum {
	AWA_data = SYSCR,
	AWA_data_clear = SYSCR,
	AWA_data_set = SYSCR,
	AWA_toggle = SYSCR,
45 46 47 48 49 50 51 52
	AWA_maska = BFIN_UART_SCR,
	AWA_maska_clear = BFIN_UART_SCR,
	AWA_maska_set = BFIN_UART_SCR,
	AWA_maska_toggle = BFIN_UART_SCR,
	AWA_maskb = BFIN_UART_GCTL,
	AWA_maskb_clear = BFIN_UART_GCTL,
	AWA_maskb_set = BFIN_UART_GCTL,
	AWA_maskb_toggle = BFIN_UART_GCTL,
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	AWA_dir = SPORT1_STAT,
	AWA_polar = SPORT1_STAT,
	AWA_edge = SPORT1_STAT,
	AWA_both = SPORT1_STAT,
#if ANOMALY_05000311
	AWA_inen = TIMER_ENABLE,
#elif ANOMALY_05000323
	AWA_inen = DMA1_1_CONFIG,
#endif
};
	/* Anomaly Workaround */
#define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
#else
#define AWA_DUMMY_READ(...)  do { } while (0)
#endif

69
static struct gpio_port_t * const gpio_array[] = {
70
#if defined(BF533_FAMILY) || defined(BF538_FAMILY)
Bryan Wu's avatar
Bryan Wu committed
71
	(struct gpio_port_t *) FIO_FLAG_D,
72
#elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
Bryan Wu's avatar
Bryan Wu committed
73 74 75
	(struct gpio_port_t *) PORTFIO,
	(struct gpio_port_t *) PORTGIO,
	(struct gpio_port_t *) PORTHIO,
76 77 78 79
#elif defined(BF561_FAMILY)
	(struct gpio_port_t *) FIO0_FLAG_D,
	(struct gpio_port_t *) FIO1_FLAG_D,
	(struct gpio_port_t *) FIO2_FLAG_D,
80
#elif defined(CONFIG_BF54x)
81 82 83 84 85 86 87 88 89 90 91 92 93
	(struct gpio_port_t *)PORTA_FER,
	(struct gpio_port_t *)PORTB_FER,
	(struct gpio_port_t *)PORTC_FER,
	(struct gpio_port_t *)PORTD_FER,
	(struct gpio_port_t *)PORTE_FER,
	(struct gpio_port_t *)PORTF_FER,
	(struct gpio_port_t *)PORTG_FER,
	(struct gpio_port_t *)PORTH_FER,
	(struct gpio_port_t *)PORTI_FER,
	(struct gpio_port_t *)PORTJ_FER,
#else
# error no gpio arrays defined
#endif
Bryan Wu's avatar
Bryan Wu committed
94 95
};

96
#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
97
static unsigned short * const port_fer[] = {
Bryan Wu's avatar
Bryan Wu committed
98 99 100 101 102
	(unsigned short *) PORTF_FER,
	(unsigned short *) PORTG_FER,
	(unsigned short *) PORTH_FER,
};

103 104
# if !defined(BF537_FAMILY)
static unsigned short * const port_mux[] = {
105 106 107 108 109 110
	(unsigned short *) PORTF_MUX,
	(unsigned short *) PORTG_MUX,
	(unsigned short *) PORTH_MUX,
};

static const
111
u8 pmux_offset[][16] = {
112
#  if defined(CONFIG_BF52x)
113 114 115
	{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
	{ 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
	{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
116
#  elif defined(CONFIG_BF51x)
117 118 119 120 121
	{ 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
	{ 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
	{ 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
#  endif
};
122
# endif
123

124 125
#endif

126
static unsigned short reserved_gpio_map[GPIO_BANK_NUM];
127
static unsigned short reserved_peri_map[gpio_bank(MAX_RESOURCES)];
128
static unsigned short reserved_gpio_irq_map[GPIO_BANK_NUM];
129

130 131
#define RESOURCE_LABEL_SIZE 	16

132
static struct str_ident {
133
	char name[RESOURCE_LABEL_SIZE];
134
} str_ident[MAX_RESOURCES];
Bryan Wu's avatar
Bryan Wu committed
135

136
#if defined(CONFIG_PM)
137
static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
138 139
#endif

140
inline int check_gpio(unsigned gpio)
141
{
142
#if defined(CONFIG_BF54x)
143 144
	if (gpio == GPIO_PB15 || gpio == GPIO_PC14 || gpio == GPIO_PC15
	    || gpio == GPIO_PH14 || gpio == GPIO_PH15
145
	    || gpio == GPIO_PJ14 || gpio == GPIO_PJ15)
146
		return -EINVAL;
147
#endif
148
	if (gpio >= MAX_BLACKFIN_GPIOS)
Bryan Wu's avatar
Bryan Wu committed
149 150 151 152
		return -EINVAL;
	return 0;
}

153
static void gpio_error(unsigned gpio)
154 155 156 157
{
	printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
}

158 159
static void set_label(unsigned short ident, const char *label)
{
160
	if (label) {
161
		strncpy(str_ident[ident].name, label,
162
			 RESOURCE_LABEL_SIZE);
163
		str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
164 165 166 167 168
	}
}

static char *get_label(unsigned short ident)
{
169
	return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
170 171 172 173
}

static int cmp_label(unsigned short ident, const char *label)
{
174 175 176 177 178
	if (label == NULL) {
		dump_stack();
		printk(KERN_ERR "Please provide none-null label\n");
	}

179
	if (label)
180
		return strcmp(str_ident[ident].name, label);
181 182 183 184
	else
		return -EINVAL;
}

185
static void port_setup(unsigned gpio, unsigned short usage)
Bryan Wu's avatar
Bryan Wu committed
186
{
187 188 189
	if (check_gpio(gpio))
		return;

190
#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
191 192 193 194 195
	if (usage == GPIO_USAGE)
		*port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
	else
		*port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
	SSYNC();
196
#elif defined(CONFIG_BF54x)
197 198 199 200 201
	if (usage == GPIO_USAGE)
		gpio_array[gpio_bank(gpio)]->port_fer &= ~gpio_bit(gpio);
	else
		gpio_array[gpio_bank(gpio)]->port_fer |= gpio_bit(gpio);
	SSYNC();
Bryan Wu's avatar
Bryan Wu committed
202
#endif
203
}
Bryan Wu's avatar
Bryan Wu committed
204

205
#ifdef BF537_FAMILY
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static struct {
	unsigned short res;
	unsigned short offset;
} port_mux_lut[] = {
	{.res = P_PPI0_D13, .offset = 11},
	{.res = P_PPI0_D14, .offset = 11},
	{.res = P_PPI0_D15, .offset = 11},
	{.res = P_SPORT1_TFS, .offset = 11},
	{.res = P_SPORT1_TSCLK, .offset = 11},
	{.res = P_SPORT1_DTPRI, .offset = 11},
	{.res = P_PPI0_D10, .offset = 10},
	{.res = P_PPI0_D11, .offset = 10},
	{.res = P_PPI0_D12, .offset = 10},
	{.res = P_SPORT1_RSCLK, .offset = 10},
	{.res = P_SPORT1_RFS, .offset = 10},
	{.res = P_SPORT1_DRPRI, .offset = 10},
	{.res = P_PPI0_D8, .offset = 9},
	{.res = P_PPI0_D9, .offset = 9},
	{.res = P_SPORT1_DRSEC, .offset = 9},
	{.res = P_SPORT1_DTSEC, .offset = 9},
	{.res = P_TMR2, .offset = 8},
	{.res = P_PPI0_FS3, .offset = 8},
	{.res = P_TMR3, .offset = 7},
	{.res = P_SPI0_SSEL4, .offset = 7},
	{.res = P_TMR4, .offset = 6},
	{.res = P_SPI0_SSEL5, .offset = 6},
	{.res = P_TMR5, .offset = 5},
	{.res = P_SPI0_SSEL6, .offset = 5},
	{.res = P_UART1_RX, .offset = 4},
	{.res = P_UART1_TX, .offset = 4},
	{.res = P_TMR6, .offset = 4},
	{.res = P_TMR7, .offset = 4},
	{.res = P_UART0_RX, .offset = 3},
	{.res = P_UART0_TX, .offset = 3},
	{.res = P_DMAR0, .offset = 3},
	{.res = P_DMAR1, .offset = 3},
	{.res = P_SPORT0_DTSEC, .offset = 1},
	{.res = P_SPORT0_DRSEC, .offset = 1},
	{.res = P_CAN0_RX, .offset = 1},
	{.res = P_CAN0_TX, .offset = 1},
	{.res = P_SPI0_SSEL7, .offset = 1},
	{.res = P_SPORT0_TFS, .offset = 0},
	{.res = P_SPORT0_DTPRI, .offset = 0},
	{.res = P_SPI0_SSEL2, .offset = 0},
	{.res = P_SPI0_SSEL3, .offset = 0},
251 252
};

253
static void portmux_setup(unsigned short per)
254
{
255
	u16 y, offset, muxreg;
256
	u16 function = P_FUNCT2MUX(per);
257

258 259
	for (y = 0; y < ARRAY_SIZE(port_mux_lut); y++) {
		if (port_mux_lut[y].res == per) {
260 261 262

			/* SET PORTMUX REG */

263
			offset = port_mux_lut[y].offset;
264 265
			muxreg = bfin_read_PORT_MUX();

266
			if (offset != 1)
267
				muxreg &= ~(1 << offset);
268
			else
269 270 271 272 273 274 275
				muxreg &= ~(3 << 1);

			muxreg |= (function << offset);
			bfin_write_PORT_MUX(muxreg);
		}
	}
}
276
#elif defined(CONFIG_BF54x)
277
inline void portmux_setup(unsigned short per)
278 279
{
	u32 pmux;
280 281
	u16 ident = P_IDENT(per);
	u16 function = P_FUNCT2MUX(per);
282

283
	pmux = gpio_array[gpio_bank(ident)]->port_mux;
284

285 286
	pmux &= ~(0x3 << (2 * gpio_sub_n(ident)));
	pmux |= (function & 0x3) << (2 * gpio_sub_n(ident));
287

288
	gpio_array[gpio_bank(ident)]->port_mux = pmux;
289 290
}

291
inline u16 get_portmux(unsigned short per)
292 293
{
	u32 pmux;
294
	u16 ident = P_IDENT(per);
295

296
	pmux = gpio_array[gpio_bank(ident)]->port_mux;
297

298
	return (pmux >> (2 * gpio_sub_n(ident)) & 0x3);
299
}
300
#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
301
inline void portmux_setup(unsigned short per)
302
{
303
	u16 pmux, ident = P_IDENT(per), function = P_FUNCT2MUX(per);
304 305 306 307 308 309 310 311
	u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];

	pmux = *port_mux[gpio_bank(ident)];
	pmux &= ~(3 << offset);
	pmux |= (function & 3) << offset;
	*port_mux[gpio_bank(ident)] = pmux;
	SSYNC();
}
312 313 314
#else
# define portmux_setup(...)  do { } while (0)
#endif
Bryan Wu's avatar
Bryan Wu committed
315

316
#ifndef CONFIG_BF54x
Bryan Wu's avatar
Bryan Wu committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/***********************************************************
*
* FUNCTIONS: Blackfin General Purpose Ports Access Functions
*
* INPUTS/OUTPUTS:
* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
*
*
* DESCRIPTION: These functions abstract direct register access
*              to Blackfin processor General Purpose
*              Ports Regsiters
*
* CAUTION: These functions do not belong to the GPIO Driver API
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/

/* Set a specific bit */

#define SET_GPIO(name) \
337
void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu's avatar
Bryan Wu committed
338 339
{ \
	unsigned long flags; \
340
	local_irq_save_hw(flags); \
Bryan Wu's avatar
Bryan Wu committed
341
	if (arg) \
342
		gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
Bryan Wu's avatar
Bryan Wu committed
343
	else \
344
		gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
345
	AWA_DUMMY_READ(name); \
346
	local_irq_restore_hw(flags); \
Bryan Wu's avatar
Bryan Wu committed
347 348 349
} \
EXPORT_SYMBOL(set_gpio_ ## name);

350 351 352 353 354
SET_GPIO(dir)   /* set_gpio_dir() */
SET_GPIO(inen)  /* set_gpio_inen() */
SET_GPIO(polar) /* set_gpio_polar() */
SET_GPIO(edge)  /* set_gpio_edge() */
SET_GPIO(both)  /* set_gpio_both() */
Bryan Wu's avatar
Bryan Wu committed
355 356


357
#define SET_GPIO_SC(name) \
358
void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
359 360
{ \
	unsigned long flags; \
361 362
	if (ANOMALY_05000311 || ANOMALY_05000323) \
		local_irq_save_hw(flags); \
Bryan Wu's avatar
Bryan Wu committed
363
	if (arg) \
364
		gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
Bryan Wu's avatar
Bryan Wu committed
365
	else \
366 367 368 369 370
		gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
	if (ANOMALY_05000311 || ANOMALY_05000323) { \
		AWA_DUMMY_READ(name); \
		local_irq_restore_hw(flags); \
	} \
Bryan Wu's avatar
Bryan Wu committed
371 372 373 374 375 376 377
} \
EXPORT_SYMBOL(set_gpio_ ## name);

SET_GPIO_SC(maska)
SET_GPIO_SC(maskb)
SET_GPIO_SC(data)

378
void set_gpio_toggle(unsigned gpio)
Bryan Wu's avatar
Bryan Wu committed
379 380
{
	unsigned long flags;
381 382 383 384 385 386 387
	if (ANOMALY_05000311 || ANOMALY_05000323)
		local_irq_save_hw(flags);
	gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
	if (ANOMALY_05000311 || ANOMALY_05000323) {
		AWA_DUMMY_READ(toggle);
		local_irq_restore_hw(flags);
	}
Bryan Wu's avatar
Bryan Wu committed
388 389 390 391 392 393 394
}
EXPORT_SYMBOL(set_gpio_toggle);


/*Set current PORT date (16-bit word)*/

#define SET_GPIO_P(name) \
395
void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
Bryan Wu's avatar
Bryan Wu committed
396
{ \
397
	unsigned long flags; \
398 399 400 401 402 403 404
	if (ANOMALY_05000311 || ANOMALY_05000323) \
		local_irq_save_hw(flags); \
	gpio_array[gpio_bank(gpio)]->name = arg; \
	if (ANOMALY_05000311 || ANOMALY_05000323) { \
		AWA_DUMMY_READ(name); \
		local_irq_restore_hw(flags); \
	} \
405 406
} \
EXPORT_SYMBOL(set_gpiop_ ## name);
Bryan Wu's avatar
Bryan Wu committed
407

408
SET_GPIO_P(data)
Bryan Wu's avatar
Bryan Wu committed
409 410 411 412 413 414 415 416 417
SET_GPIO_P(dir)
SET_GPIO_P(inen)
SET_GPIO_P(polar)
SET_GPIO_P(edge)
SET_GPIO_P(both)
SET_GPIO_P(maska)
SET_GPIO_P(maskb)

/* Get a specific bit */
418
#define GET_GPIO(name) \
419
unsigned short get_gpio_ ## name(unsigned gpio) \
420 421 422
{ \
	unsigned long flags; \
	unsigned short ret; \
423 424 425 426 427 428 429
	if (ANOMALY_05000311 || ANOMALY_05000323) \
		local_irq_save_hw(flags); \
	ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
	if (ANOMALY_05000311 || ANOMALY_05000323) { \
		AWA_DUMMY_READ(name); \
		local_irq_restore_hw(flags); \
	} \
430 431 432
	return ret; \
} \
EXPORT_SYMBOL(get_gpio_ ## name);
Bryan Wu's avatar
Bryan Wu committed
433

434
GET_GPIO(data)
Bryan Wu's avatar
Bryan Wu committed
435 436 437 438 439 440 441 442 443 444
GET_GPIO(dir)
GET_GPIO(inen)
GET_GPIO(polar)
GET_GPIO(edge)
GET_GPIO(both)
GET_GPIO(maska)
GET_GPIO(maskb)

/*Get current PORT date (16-bit word)*/

445
#define GET_GPIO_P(name) \
446
unsigned short get_gpiop_ ## name(unsigned gpio) \
447 448 449
{ \
	unsigned long flags; \
	unsigned short ret; \
450 451 452 453 454 455 456
	if (ANOMALY_05000311 || ANOMALY_05000323) \
		local_irq_save_hw(flags); \
	ret = (gpio_array[gpio_bank(gpio)]->name); \
	if (ANOMALY_05000311 || ANOMALY_05000323) { \
		AWA_DUMMY_READ(name); \
		local_irq_restore_hw(flags); \
	} \
457 458 459
	return ret; \
} \
EXPORT_SYMBOL(get_gpiop_ ## name);
Bryan Wu's avatar
Bryan Wu committed
460

461
GET_GPIO_P(data)
Bryan Wu's avatar
Bryan Wu committed
462 463 464 465 466 467 468 469 470 471
GET_GPIO_P(dir)
GET_GPIO_P(inen)
GET_GPIO_P(polar)
GET_GPIO_P(edge)
GET_GPIO_P(both)
GET_GPIO_P(maska)
GET_GPIO_P(maskb)


#ifdef CONFIG_PM
472 473 474 475 476 477 478 479 480 481 482

static unsigned short wakeup_map[GPIO_BANK_NUM];
static unsigned char wakeup_flags_map[MAX_BLACKFIN_GPIOS];

static const unsigned int sic_iwr_irqs[] = {
#if defined(BF533_FAMILY)
	IRQ_PROG_INTB
#elif defined(BF537_FAMILY)
	IRQ_PROG_INTB, IRQ_PORTG_INTB, IRQ_MAC_TX
#elif defined(BF538_FAMILY)
	IRQ_PORTF_INTB
483
#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
484 485 486 487 488 489 490 491
	IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
#elif defined(BF561_FAMILY)
	IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
#else
# error no SIC_IWR defined
#endif
};

Bryan Wu's avatar
Bryan Wu committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
/***********************************************************
*
* FUNCTIONS: Blackfin PM Setup API
*
* INPUTS/OUTPUTS:
* gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
* type -
*	PM_WAKE_RISING
*	PM_WAKE_FALLING
*	PM_WAKE_HIGH
*	PM_WAKE_LOW
*	PM_WAKE_BOTH_EDGES
*
* DESCRIPTION: Blackfin PM Driver API
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/
511
int gpio_pm_wakeup_request(unsigned gpio, unsigned char type)
Bryan Wu's avatar
Bryan Wu committed
512 513 514 515 516 517
{
	unsigned long flags;

	if ((check_gpio(gpio) < 0) || !type)
		return -EINVAL;

518
	local_irq_save_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
519 520
	wakeup_map[gpio_bank(gpio)] |= gpio_bit(gpio);
	wakeup_flags_map[gpio] = type;
521
	local_irq_restore_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
522 523 524 525 526

	return 0;
}
EXPORT_SYMBOL(gpio_pm_wakeup_request);

527
void gpio_pm_wakeup_free(unsigned gpio)
Bryan Wu's avatar
Bryan Wu committed
528 529 530 531 532 533
{
	unsigned long flags;

	if (check_gpio(gpio) < 0)
		return;

534
	local_irq_save_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
535 536 537

	wakeup_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);

538
	local_irq_restore_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
539 540 541
}
EXPORT_SYMBOL(gpio_pm_wakeup_free);

542
static int bfin_gpio_wakeup_type(unsigned gpio, unsigned char type)
Bryan Wu's avatar
Bryan Wu committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
{
	port_setup(gpio, GPIO_USAGE);
	set_gpio_dir(gpio, 0);
	set_gpio_inen(gpio, 1);

	if (type & (PM_WAKE_RISING | PM_WAKE_FALLING))
		set_gpio_edge(gpio, 1);
	 else
		set_gpio_edge(gpio, 0);

	if ((type & (PM_WAKE_BOTH_EDGES)) == (PM_WAKE_BOTH_EDGES))
		set_gpio_both(gpio, 1);
	else
		set_gpio_both(gpio, 0);

	if ((type & (PM_WAKE_FALLING | PM_WAKE_LOW)))
		set_gpio_polar(gpio, 1);
	else
		set_gpio_polar(gpio, 0);

	SSYNC();

	return 0;
}

568
u32 bfin_pm_standby_setup(void)
Bryan Wu's avatar
Bryan Wu committed
569 570 571
{
	u16 bank, mask, i, gpio;

572
	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu's avatar
Bryan Wu committed
573 574 575
		mask = wakeup_map[gpio_bank(i)];
		bank = gpio_bank(i);

576 577
		gpio_bank_saved[bank].maskb = gpio_array[bank]->maskb;
		gpio_array[bank]->maskb = 0;
Bryan Wu's avatar
Bryan Wu committed
578 579

		if (mask) {
580
#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
Bryan Wu's avatar
Bryan Wu committed
581 582
			gpio_bank_saved[bank].fer   = *port_fer[bank];
#endif
583 584 585 586 587
			gpio_bank_saved[bank].inen  = gpio_array[bank]->inen;
			gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
			gpio_bank_saved[bank].dir   = gpio_array[bank]->dir;
			gpio_bank_saved[bank].edge  = gpio_array[bank]->edge;
			gpio_bank_saved[bank].both  = gpio_array[bank]->both;
588 589
			gpio_bank_saved[bank].reserved =
						reserved_gpio_map[bank];
Bryan Wu's avatar
Bryan Wu committed
590 591 592 593

			gpio = i;

			while (mask) {
594 595
				if ((mask & 1) && (wakeup_flags_map[gpio] !=
					PM_WAKE_IGNORE)) {
596
					reserved_gpio_map[gpio_bank(gpio)] |=
597 598 599
							gpio_bit(gpio);
					bfin_gpio_wakeup_type(gpio,
						wakeup_flags_map[gpio]);
Bryan Wu's avatar
Bryan Wu committed
600 601 602 603 604 605
					set_gpio_data(gpio, 0); /*Clear*/
				}
				gpio++;
				mask >>= 1;
			}

606
			bfin_internal_set_wake(sic_iwr_irqs[bank], 1);
607
			gpio_array[bank]->maskb_set = wakeup_map[gpio_bank(i)];
Bryan Wu's avatar
Bryan Wu committed
608 609 610
		}
	}

611 612
	AWA_DUMMY_READ(maskb_set);

613
	return 0;
Bryan Wu's avatar
Bryan Wu committed
614 615
}

616
void bfin_pm_standby_restore(void)
Bryan Wu's avatar
Bryan Wu committed
617 618 619
{
	u16 bank, mask, i;

620
	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
Bryan Wu's avatar
Bryan Wu committed
621 622 623 624
		mask = wakeup_map[gpio_bank(i)];
		bank = gpio_bank(i);

		if (mask) {
625
#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
Bryan Wu's avatar
Bryan Wu committed
626 627
			*port_fer[bank]   	= gpio_bank_saved[bank].fer;
#endif
628 629 630 631 632
			gpio_array[bank]->inen  = gpio_bank_saved[bank].inen;
			gpio_array[bank]->dir   = gpio_bank_saved[bank].dir;
			gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
			gpio_array[bank]->edge  = gpio_bank_saved[bank].edge;
			gpio_array[bank]->both  = gpio_bank_saved[bank].both;
633

634 635
			reserved_gpio_map[bank] =
					gpio_bank_saved[bank].reserved;
636
			bfin_internal_set_wake(sic_iwr_irqs[bank], 0);
Bryan Wu's avatar
Bryan Wu committed
637 638
		}

639
		gpio_array[bank]->maskb = gpio_bank_saved[bank].maskb;
Bryan Wu's avatar
Bryan Wu committed
640
	}
641
	AWA_DUMMY_READ(maskb);
Bryan Wu's avatar
Bryan Wu committed
642 643
}

644 645 646 647 648 649 650
void bfin_gpio_pm_hibernate_suspend(void)
{
	int i, bank;

	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
		bank = gpio_bank(i);

651
#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
652
		gpio_bank_saved[bank].fer = *port_fer[bank];
653
#if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
654
		gpio_bank_saved[bank].mux = *port_mux[bank];
655
#else
656 657
		if (bank == 0)
			gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
658 659
#endif
#endif
660 661 662 663 664 665 666
		gpio_bank_saved[bank].data  = gpio_array[bank]->data;
		gpio_bank_saved[bank].inen  = gpio_array[bank]->inen;
		gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
		gpio_bank_saved[bank].dir   = gpio_array[bank]->dir;
		gpio_bank_saved[bank].edge  = gpio_array[bank]->edge;
		gpio_bank_saved[bank].both  = gpio_array[bank]->both;
		gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
667 668 669 670 671 672 673 674 675 676
	}

	AWA_DUMMY_READ(maska);
}

void bfin_gpio_pm_hibernate_restore(void)
{
	int i, bank;

	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
677
		bank = gpio_bank(i);
678

679 680
#if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
#if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
681
		*port_mux[bank] = gpio_bank_saved[bank].mux;
682
#else
683 684
		if (bank == 0)
			bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
685
#endif
686
		*port_fer[bank] = gpio_bank_saved[bank].fer;
687
#endif
688 689 690 691 692
		gpio_array[bank]->inen  = gpio_bank_saved[bank].inen;
		gpio_array[bank]->dir   = gpio_bank_saved[bank].dir;
		gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
		gpio_array[bank]->edge  = gpio_bank_saved[bank].edge;
		gpio_array[bank]->both  = gpio_bank_saved[bank].both;
693

694 695
		gpio_array[bank]->data_set = gpio_bank_saved[bank].data
						| gpio_bank_saved[bank].dir;
696

697
		gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
698 699 700 701 702
	}
	AWA_DUMMY_READ(maska);
}


Bryan Wu's avatar
Bryan Wu committed
703
#endif
704
#else /* CONFIG_BF54x */
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
#ifdef CONFIG_PM

u32 bfin_pm_standby_setup(void)
{
	return 0;
}

void bfin_pm_standby_restore(void)
{

}

void bfin_gpio_pm_hibernate_suspend(void)
{
	int i, bank;

	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
		bank = gpio_bank(i);

724 725 726 727 728 729
		gpio_bank_saved[bank].fer = gpio_array[bank]->port_fer;
		gpio_bank_saved[bank].mux = gpio_array[bank]->port_mux;
		gpio_bank_saved[bank].data = gpio_array[bank]->data;
		gpio_bank_saved[bank].data = gpio_array[bank]->data;
		gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
		gpio_bank_saved[bank].dir = gpio_array[bank]->dir_set;
730 731 732 733 734 735 736 737
	}
}

void bfin_gpio_pm_hibernate_restore(void)
{
	int i, bank;

	for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
738 739 740 741 742 743 744 745
		bank = gpio_bank(i);

		gpio_array[bank]->port_mux = gpio_bank_saved[bank].mux;
		gpio_array[bank]->port_fer = gpio_bank_saved[bank].fer;
		gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
		gpio_array[bank]->dir_set = gpio_bank_saved[bank].dir;
		gpio_array[bank]->data_set = gpio_bank_saved[bank].data
						| gpio_bank_saved[bank].dir;
746 747 748
	}
}
#endif
749

750
unsigned short get_gpio_dir(unsigned gpio)
751
{
752
	return (0x01 & (gpio_array[gpio_bank(gpio)]->dir_clear >> gpio_sub_n(gpio)));
753 754 755
}
EXPORT_SYMBOL(get_gpio_dir);

756
#endif /* CONFIG_BF54x */
Bryan Wu's avatar
Bryan Wu committed
757

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
/***********************************************************
*
* FUNCTIONS: 	Blackfin Peripheral Resource Allocation
*		and PortMux Setup
*
* INPUTS/OUTPUTS:
* per	Peripheral Identifier
* label	String
*
* DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/

int peripheral_request(unsigned short per, const char *label)
{
	unsigned long flags;
	unsigned short ident = P_IDENT(per);

	/*
	 * Don't cares are pins with only one dedicated function
	 */
782

783 784 785 786 787 788
	if (per & P_DONTCARE)
		return 0;

	if (!(per & P_DEFINED))
		return -ENODEV;

789
	local_irq_save_hw(flags);
790

791 792 793 794 795
	/* If a pin can be muxed as either GPIO or peripheral, make
	 * sure it is not already a GPIO pin when we request it.
	 */
	if (unlikely(!check_gpio(ident) &&
	    reserved_gpio_map[gpio_bank(ident)] & gpio_bit(ident))) {
796 797
		if (system_state == SYSTEM_BOOTING)
			dump_stack();
798
		printk(KERN_ERR
799
		       "%s: Peripheral %d is already reserved as GPIO by %s !\n",
800
		       __func__, ident, get_label(ident));
801
		local_irq_restore_hw(flags);
802 803 804 805 806
		return -EBUSY;
	}

	if (unlikely(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident))) {

807 808 809 810
		/*
		 * Pin functions like AMC address strobes my
		 * be requested and used by several drivers
		 */
811

812
#ifdef CONFIG_BF54x
813
		if (!((per & P_MAYSHARE) && get_portmux(per) == P_FUNCT2MUX(per))) {
814 815 816
#else
		if (!(per & P_MAYSHARE)) {
#endif
817 818 819 820
			/*
			 * Allow that the identical pin function can
			 * be requested from the same driver twice
			 */
821

822 823
			if (cmp_label(ident, label) == 0)
				goto anyway;
824

825 826
			if (system_state == SYSTEM_BOOTING)
				dump_stack();
827 828
			printk(KERN_ERR
			       "%s: Peripheral %d function %d is already reserved by %s !\n",
829
			       __func__, ident, P_FUNCT2MUX(per), get_label(ident));
830
			local_irq_restore_hw(flags);
831 832 833 834
			return -EBUSY;
		}
	}

835
 anyway:
836 837
	reserved_peri_map[gpio_bank(ident)] |= gpio_bit(ident);

838
	portmux_setup(per);
839 840
	port_setup(ident, PERIPHERAL_USAGE);

841
	local_irq_restore_hw(flags);
842 843 844 845 846 847
	set_label(ident, label);

	return 0;
}
EXPORT_SYMBOL(peripheral_request);

848
int peripheral_request_list(const unsigned short per[], const char *label)
849 850 851 852 853
{
	u16 cnt;
	int ret;

	for (cnt = 0; per[cnt] != 0; cnt++) {
854

855
		ret = peripheral_request(per[cnt], label);
856 857

		if (ret < 0) {
858
			for ( ; cnt > 0; cnt--)
859
				peripheral_free(per[cnt - 1]);
860 861

			return ret;
862
		}
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
	}

	return 0;
}
EXPORT_SYMBOL(peripheral_request_list);

void peripheral_free(unsigned short per)
{
	unsigned long flags;
	unsigned short ident = P_IDENT(per);

	if (per & P_DONTCARE)
		return;

	if (!(per & P_DEFINED))
		return;

880
	local_irq_save_hw(flags);
881

882
	if (unlikely(!(reserved_peri_map[gpio_bank(ident)] & gpio_bit(ident)))) {
883
		local_irq_restore_hw(flags);
884 885 886
		return;
	}

887
	if (!(per & P_MAYSHARE))
888 889 890 891
		port_setup(ident, GPIO_USAGE);

	reserved_peri_map[gpio_bank(ident)] &= ~gpio_bit(ident);

892 893
	set_label(ident, "free");

894
	local_irq_restore_hw(flags);
895 896 897
}
EXPORT_SYMBOL(peripheral_free);

898
void peripheral_free_list(const unsigned short per[])
899 900
{
	u16 cnt;
901
	for (cnt = 0; per[cnt] != 0; cnt++)
902 903 904 905
		peripheral_free(per[cnt]);
}
EXPORT_SYMBOL(peripheral_free_list);

Bryan Wu's avatar
Bryan Wu committed
906 907 908 909 910
/***********************************************************
*
* FUNCTIONS: Blackfin GPIO Driver
*
* INPUTS/OUTPUTS:
911 912
* gpio	PIO Number between 0 and MAX_BLACKFIN_GPIOS
* label	String
Bryan Wu's avatar
Bryan Wu committed
913 914 915 916 917 918 919 920
*
* DESCRIPTION: Blackfin GPIO Driver API
*
* CAUTION:
*************************************************************
* MODIFICATION HISTORY :
**************************************************************/

921
int bfin_gpio_request(unsigned gpio, const char *label)
Bryan Wu's avatar
Bryan Wu committed
922 923 924 925 926 927
{
	unsigned long flags;

	if (check_gpio(gpio) < 0)
		return -EINVAL;

928
	local_irq_save_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
929

930 931 932 933 934 935 936
	/*
	 * Allow that the identical GPIO can
	 * be requested from the same driver twice
	 * Do nothing and return -
	 */

	if (cmp_label(gpio, label) == 0) {
937
		local_irq_restore_hw(flags);
938 939 940
		return 0;
	}

941
	if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
942 943
		if (system_state == SYSTEM_BOOTING)
			dump_stack();
944
		printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
945
		       gpio, get_label(gpio));
946
		local_irq_restore_hw(flags);
947 948 949
		return -EBUSY;
	}
	if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
950 951
		if (system_state == SYSTEM_BOOTING)
			dump_stack();
952 953 954
		printk(KERN_ERR
		       "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
		       gpio, get_label(gpio));
955
		local_irq_restore_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
956 957
		return -EBUSY;
	}
958
	if (unlikely(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
959 960
		printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
		       " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
961
	}
962
#ifndef CONFIG_BF54x
963 964 965 966
	else {	/* Reset POLAR setting when acquiring a gpio for the first time */
		set_gpio_polar(gpio, 0);
	}
#endif
967

968
	reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
969
	set_label(gpio, label);
Bryan Wu's avatar
Bryan Wu committed
970

971
	local_irq_restore_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
972 973 974 975 976

	port_setup(gpio, GPIO_USAGE);

	return 0;
}
977
EXPORT_SYMBOL(bfin_gpio_request);
Bryan Wu's avatar
Bryan Wu committed
978

979
void bfin_gpio_free(unsigned gpio)
Bryan Wu's avatar
Bryan Wu committed
980 981 982 983 984 985
{
	unsigned long flags;

	if (check_gpio(gpio) < 0)
		return;

986 987
	might_sleep();

988
	local_irq_save_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
989

990
	if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
991 992
		if (system_state == SYSTEM_BOOTING)
			dump_stack();
993
		gpio_error(gpio);
994
		local_irq_restore_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
995 996 997
		return;
	}

998
	reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
Bryan Wu's avatar
Bryan Wu committed
999

1000 1001
	set_label(gpio, "free");

1002
	local_irq_restore_hw(flags);
Bryan Wu's avatar
Bryan Wu committed
1003
}
1004
EXPORT_SYMBOL(bfin_gpio_free);
Bryan Wu's avatar
Bryan Wu committed
1005

1006 1007 1008 1009 1010 1011 1012
int bfin_gpio_irq_request(unsigned gpio, const char *label)
{
	unsigned long flags;

	if (check_gpio(gpio) < 0)
		return -EINVAL;

1013
	local_irq_save_hw(flags);
1014 1015

	if (unlikely(reserved_peri_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
1016 1017
		if (system_state == SYSTEM_BOOTING)
			dump_stack();
1018 1019 1020
		printk(KERN_ERR
		       "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
		       gpio, get_label(gpio));
1021
		local_irq_restore_hw(flags);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
		return -EBUSY;
	}
	if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))
		printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
		       "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
		       gpio, get_label(gpio));

	reserved_gpio_irq_map[gpio_bank(gpio)] |= gpio_bit(gpio);
	set_label(gpio, label);

1032
	local_irq_restore_hw(flags);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045

	port_setup(gpio, GPIO_USAGE);

	return 0;
}

void bfin_gpio_irq_free(unsigned gpio)
{
	unsigned long flags;

	if (check_gpio(gpio) < 0)
		return;

1046
	local_irq_save_hw(flags);
1047 1048

	if (unlikely(!(reserved_gpio_irq_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
1049 1050
		if (system_state == SYSTEM_BOOTING)
			dump_stack();
1051
		gpio_error(gpio);
1052
		local_irq_restore_hw(flags);
1053 1054 1055 1056 1057 1058 1059
		return;
	}

	reserved_gpio_irq_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);

	set_label(gpio, "free");

1060
	local_irq_restore_hw(flags);
1061 1062
}

1063 1064
static inline void __bfin_gpio_direction_input(unsigned gpio)
{
1065
#ifdef CONFIG_BF54x
1066 1067 1068 1069 1070 1071 1072
	gpio_array[gpio_bank(gpio)]->dir_clear = gpio_bit(gpio);
#else
	gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
#endif
	gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
}

1073
int bfin_gpio_direction_input(unsigned gpio)
1074 1075 1076
{
	unsigned long flags;

1077 1078 1079 1080 1081
	if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
		gpio_error(gpio);
		return -EINVAL;
	}

1082
	local_irq_save_hw(flags);
1083 1084
	__bfin_gpio_direction_input(gpio);
	AWA_DUMMY_READ(inen);
1085
	local_irq_restore_hw(flags);
1086 1087

	return 0;
1088
}
1089
EXPORT_SYMBOL(bfin_gpio_direction_input);
1090

1091
void bfin_gpio_irq_prepare(unsigned gpio)
1092
{
1093
#ifdef CONFIG_BF54x
1094
	unsigned long flags;
1095
#endif
1096

1097
	port_setup(gpio, GPIO_USAGE);
1098

1099
#ifdef CONFIG_BF54x
1100
	local_irq_save_hw(flags);
1101
	__bfin_gpio_direction_input(gpio);
1102
	local_irq_restore_hw(flags);
1103
#endif
1104 1105
}

1106
void bfin_gpio_set_value(unsigned gpio, int arg)
1107 1108
{
	if (arg)
1109
		gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1110
	else
1111
		gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
1112
}
1113
EXPORT_SYMBOL(bfin_gpio_set_value);
1114

1115
int bfin_gpio_direction_output(unsigned gpio, int value)
1116 1117 1118
{
	unsigned long flags;

1119 1120 1121 1122
	if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
		gpio_error(gpio);
		return -EINVAL;
	}
1123

1124
	local_irq_save_hw(flags);
1125

1126 1127
	gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
	gpio_set_value(gpio, value);
1128
#ifdef CONFIG_BF54x
1129
	gpio_array[gpio_bank(gpio)]->dir_set = gpio_bit(gpio);
1130
#else
1131 1132 1133 1134 1135 1136 1137 1138 1139
	gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
#endif

	AWA_DUMMY_READ(dir);
	local_irq_restore_hw(flags);

	return 0;
}
EXPORT_SYMBOL(bfin_gpio_direction_output);
1140

1141
int bfin_gpio_get_value(unsigned gpio)
1142
{
1143
#ifdef CONFIG_BF54x
1144 1145
	return (1 & (gpio_array[gpio_bank(gpio)]->data >> gpio_sub_n(gpio)));
#else
1146 1147 1148
	unsigned long flags;

	if (unlikely(get_gpio_edge(gpio))) {
1149
		int ret;
1150
		local_irq_save_hw(flags);
1151 1152 1153
		set_gpio_edge(gpio, 0);
		ret = get_gpio_data(gpio);
		set_gpio_edge(gpio, 1);
1154
		local_irq_restore_hw(flags);
1155 1156 1157
		return ret;
	} else
		return get_gpio_data(gpio);
1158
#endif
1159
}
1160
EXPORT_SYMBOL(bfin_gpio_get_value);
1161

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
/* If we are booting from SPI and our board lacks a strong enough pull up,
 * the core can reset and execute the bootrom faster than the resistor can
 * pull the signal logically high.  To work around this (common) error in
 * board design, we explicitly set the pin back to GPIO mode, force /CS
 * high, and wait for the electrons to do their thing.
 *
 * This function only makes sense to be called from reset code, but it
 * lives here as we need to force all the GPIO states w/out going through
 * BUG() checks and such.
 */
1172
void bfin_reset_boot_spi_cs(unsigned short pin)
1173
{
1174
	unsigned short gpio = P_IDENT(pin);
1175
	port_setup(gpio, GPIO_USAGE);
1176
	gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
1177
	AWA_DUMMY_READ(data_set);
1178 1179
	udelay(1);
}
1180

1181 1182 1183 1184
#if defined(CONFIG_PROC_FS)
static int gpio_proc_read(char *buf, char **start, off_t offset,
			  int len, int *unused_i, void *unused_v)
{
1185
	int c, irq, gpio, outlen = 0;
1186 1187

	for (c = 0; c < MAX_RESOURCES; c++) {
1188 1189 1190 1191 1192 1193
		irq = reserved_gpio_irq_map[gpio_bank(c)] & gpio_bit(c);
		gpio = reserved_gpio_map[gpio_bank(c)] & gpio_bit(c);
		if (!check_gpio(c) && (gpio || irq))
			len = sprintf(buf, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
				 get_label(c), (gpio && irq) ? " *" : "",
				 get_gpio_dir(c) ? "OUTPUT" : "INPUT");
1194
		else if (reserved_peri_map[gpio_bank(c)] & gpio_bit(c))
1195
			len = sprintf(buf, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
		else
			continue;
		buf += len;
		outlen += len;
	}
	return outlen;
}

static __init int gpio_register_proc(void)
{
	struct proc_dir_entry *proc_gpio;

	proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
	if (proc_gpio)
		proc_gpio->read_proc = gpio_proc_read;
	return proc_gpio != NULL;
}
__initcall(gpio_register_proc);
#endif
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

#ifdef CONFIG_GPIOLIB
int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
{
	return bfin_gpio_direction_input(gpio);
}

int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
{
	return bfin_gpio_direction_output(gpio, level);
}

int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
{
	return bfin_gpio_get_value(gpio);
}

void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
{
	return bfin_gpio_set_value(gpio, value);
}

int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
{
	return bfin_gpio_request(gpio, chip->label);
}

void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
{
	return bfin_gpio_free(gpio);
}

static struct gpio_chip bfin_chip = {
	.label			= "Blackfin-GPIOlib",
	.direction_input	= bfin_gpiolib_direction_input,
	.get			= bfin_gpiolib_get_value,
	.direction_output	= bfin_gpiolib_direction_output,
	.set			= bfin_gpiolib_set_value,
	.request		= bfin_gpiolib_gpio_request,
	.free			= bfin_gpiolib_gpio_free,
	.base			= 0,
	.ngpio			= MAX_BLACKFIN_GPIOS,
};

static int __init bfin_gpiolib_setup(void)
{
	return gpiochip_add(&bfin_chip);
}
arch_initcall(bfin_gpiolib_setup);
#endif