blob: 6c9c6c28e06503e2dfa247794b25ed762e5b566e [file] [log] [blame]
Thomas Chou1e8e9ba2010-04-30 11:34:15 +08001/*
2 * board gpio driver
3 *
4 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
5 * Licensed under the GPL-2 or later.
6 */
7#include <common.h>
8#include <asm/io.h>
9
10#ifndef CONFIG_SYS_GPIO_BASE
11
12#define ALTERA_PIO_BASE LED_PIO_BASE
13#define ALTERA_PIO_DATA (ALTERA_PIO_BASE + 0)
14#define ALTERA_PIO_DIR (ALTERA_PIO_BASE + 4)
15static u32 pio_data_reg;
16static u32 pio_dir_reg;
17
18int gpio_direction_input(unsigned gpio)
19{
20 u32 mask = 1 << gpio;
21 writel(pio_dir_reg &= ~mask, ALTERA_PIO_DIR);
22 return 0;
23}
24
25int gpio_direction_output(unsigned gpio, int value)
26{
27 u32 mask = 1 << gpio;
28 if (value)
29 pio_data_reg |= mask;
30 else
31 pio_data_reg &= ~mask;
32 writel(pio_data_reg, ALTERA_PIO_DATA);
33 writel(pio_dir_reg |= mask, ALTERA_PIO_DIR);
34 return 0;
35}
36
37int gpio_get_value(unsigned gpio)
38{
39 u32 mask = 1 << gpio;
40 if (pio_dir_reg & mask)
41 return (pio_data_reg & mask) ? 1 : 0;
42 else
43 return (readl(ALTERA_PIO_DATA) & mask) ? 1 : 0;
44}
45
46void gpio_set_value(unsigned gpio, int value)
47{
48 u32 mask = 1 << gpio;
49 if (value)
50 pio_data_reg |= mask;
51 else
52 pio_data_reg &= ~mask;
53 writel(pio_data_reg, ALTERA_PIO_DATA);
54}
55#endif