tizen 2.4 release
[kernel/u-boot-tm1.git] / board / altera / nios2-generic / gpio.c
1 /*
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)
15 static u32 pio_data_reg;
16 static u32 pio_dir_reg;
17
18 int gpio_request(unsigned gpio, const char *label)
19 {
20         return 0;
21 }
22
23 int gpio_direction_input(unsigned gpio)
24 {
25         u32 mask = 1 << gpio;
26         writel(pio_dir_reg &= ~mask, ALTERA_PIO_DIR);
27         return 0;
28 }
29
30 int gpio_direction_output(unsigned gpio, int value)
31 {
32         u32 mask = 1 << gpio;
33         if (value)
34                 pio_data_reg |= mask;
35         else
36                 pio_data_reg &= ~mask;
37         writel(pio_data_reg, ALTERA_PIO_DATA);
38         writel(pio_dir_reg |= mask, ALTERA_PIO_DIR);
39         return 0;
40 }
41
42 int gpio_get_value(unsigned gpio)
43 {
44         u32 mask = 1 << gpio;
45         if (pio_dir_reg & mask)
46                 return (pio_data_reg & mask) ? 1 : 0;
47         else
48                 return (readl(ALTERA_PIO_DATA) & mask) ? 1 : 0;
49 }
50
51 void gpio_set_value(unsigned gpio, int value)
52 {
53         u32 mask = 1 << gpio;
54         if (value)
55                 pio_data_reg |= mask;
56         else
57                 pio_data_reg &= ~mask;
58         writel(pio_data_reg, ALTERA_PIO_DATA);
59 }
60 #endif