]> git.sur5r.net Git - u-boot/blob - board/altera/nios2-generic/gpio.c
Merge branch 'master' of git://git.denx.de/u-boot-marvell
[u-boot] / 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_direction_input(unsigned gpio)
19 {
20         u32 mask = 1 << gpio;
21         writel(pio_dir_reg &= ~mask, ALTERA_PIO_DIR);
22         return 0;
23 }
24
25 int 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
37 int 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
46 void 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