From d8a593c68bac7b9d9b26af6fa8c392671213ce93 Mon Sep 17 00:00:00 2001
From: Thomas Chou <thomas@wytron.com.tw>
Date: Mon, 27 Dec 2010 10:46:01 +0800
Subject: [PATCH] nios2: add gpio_is_valid

Signed-off-by: Thomas Chou <thomas@wytron.com.tw>
Signed-off-by: Scott McNutt <smcnutt@psyent.com>
---
 arch/nios2/include/asm/gpio.h            | 6 ++++++
 board/altera/nios2-generic/custom_fpga.h | 1 +
 board/altera/nios2-generic/gpio.c        | 6 ++++++
 3 files changed, 13 insertions(+)

diff --git a/arch/nios2/include/asm/gpio.h b/arch/nios2/include/asm/gpio.h
index 36a71324d7..4b21c8f7f2 100644
--- a/arch/nios2/include/asm/gpio.h
+++ b/arch/nios2/include/asm/gpio.h
@@ -52,6 +52,11 @@ static inline void gpio_set_value(unsigned gpio, int value)
 {
 	writel(value ? 3 : 2, CONFIG_SYS_GPIO_BASE + (gpio << 2));
 }
+
+static inline int gpio_is_valid(int number)
+{
+	return ((unsigned)number) < CONFIG_SYS_GPIO_WIDTH;
+}
 #else
 extern int gpio_request(unsigned gpio, const char *label);
 extern int gpio_free(unsigned gpio);
@@ -59,6 +64,7 @@ extern int gpio_direction_input(unsigned gpio);
 extern int gpio_direction_output(unsigned gpio, int value);
 extern int gpio_get_value(unsigned gpio);
 extern void gpio_set_value(unsigned gpio, int value);
+extern int gpio_is_valid(int number);
 #endif /* CONFIG_SYS_GPIO_BASE */
 
 #endif /* _ASM_NIOS2_GPIO_H_ */
diff --git a/board/altera/nios2-generic/custom_fpga.h b/board/altera/nios2-generic/custom_fpga.h
index a11add5591..f7f38535fa 100644
--- a/board/altera/nios2-generic/custom_fpga.h
+++ b/board/altera/nios2-generic/custom_fpga.h
@@ -50,6 +50,7 @@
 
 /* led_pio.s1 is a altera_avalon_pio */
 #define LED_PIO_BASE 0x82120870
+#define LED_PIO_WIDTH 8
 
 /* high_res_timer.s1 is a altera_avalon_timer */
 #define CONFIG_SYS_TIMER_BASE 0x82120820
diff --git a/board/altera/nios2-generic/gpio.c b/board/altera/nios2-generic/gpio.c
index 8c639ce360..4a30564358 100644
--- a/board/altera/nios2-generic/gpio.c
+++ b/board/altera/nios2-generic/gpio.c
@@ -10,6 +10,7 @@
 #ifndef CONFIG_SYS_GPIO_BASE
 
 #define ALTERA_PIO_BASE LED_PIO_BASE
+#define ALTERA_PIO_WIDTH LED_PIO_WIDTH
 #define ALTERA_PIO_DATA (ALTERA_PIO_BASE + 0)
 #define ALTERA_PIO_DIR (ALTERA_PIO_BASE + 4)
 static u32 pio_data_reg;
@@ -62,4 +63,9 @@ void gpio_set_value(unsigned gpio, int value)
 		pio_data_reg &= ~mask;
 	writel(pio_data_reg, ALTERA_PIO_DATA);
 }
+
+int gpio_is_valid(int number)
+{
+	return ((unsigned)number) < ALTERA_PIO_WIDTH;
+}
 #endif
-- 
2.39.5