From 1e8e9bad2db38e93c3bc9f4b6238b3d8be99e469 Mon Sep 17 00:00:00 2001 From: Thomas Chou Date: Fri, 30 Apr 2010 11:34:15 +0800 Subject: nios2: add gpio support to nios2-generic board This patch adds gpio support of Altera PIO component to the nios2-generic board. Though it drives only gpio_led at the moment, it supports bidirectional port to control bit-banging I2C, NAND flash busy status or button switches, etc. Signed-off-by: Thomas Chou Tested-by: Ian Abbott Signed-off-by: Scott McNutt --- board/altera/nios2-generic/gpio.c | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 board/altera/nios2-generic/gpio.c (limited to 'board/altera/nios2-generic/gpio.c') diff --git a/board/altera/nios2-generic/gpio.c b/board/altera/nios2-generic/gpio.c new file mode 100644 index 0000000000..6c9c6c28e0 --- /dev/null +++ b/board/altera/nios2-generic/gpio.c @@ -0,0 +1,55 @@ +/* + * board gpio driver + * + * Copyright (C) 2010 Thomas Chou + * Licensed under the GPL-2 or later. + */ +#include +#include + +#ifndef CONFIG_SYS_GPIO_BASE + +#define ALTERA_PIO_BASE LED_PIO_BASE +#define ALTERA_PIO_DATA (ALTERA_PIO_BASE + 0) +#define ALTERA_PIO_DIR (ALTERA_PIO_BASE + 4) +static u32 pio_data_reg; +static u32 pio_dir_reg; + +int gpio_direction_input(unsigned gpio) +{ + u32 mask = 1 << gpio; + writel(pio_dir_reg &= ~mask, ALTERA_PIO_DIR); + return 0; +} + +int gpio_direction_output(unsigned gpio, int value) +{ + u32 mask = 1 << gpio; + if (value) + pio_data_reg |= mask; + else + pio_data_reg &= ~mask; + writel(pio_data_reg, ALTERA_PIO_DATA); + writel(pio_dir_reg |= mask, ALTERA_PIO_DIR); + return 0; +} + +int gpio_get_value(unsigned gpio) +{ + u32 mask = 1 << gpio; + if (pio_dir_reg & mask) + return (pio_data_reg & mask) ? 1 : 0; + else + return (readl(ALTERA_PIO_DATA) & mask) ? 1 : 0; +} + +void gpio_set_value(unsigned gpio, int value) +{ + u32 mask = 1 << gpio; + if (value) + pio_data_reg |= mask; + else + pio_data_reg &= ~mask; + writel(pio_data_reg, ALTERA_PIO_DATA); +} +#endif -- cgit