This provides a way of simulating GPIOs by setting values which are seen by the normal gpio_get/set_value() calls.
Signed-off-by: Simon Glass <s...@chromium.org> --- drivers/gpio/Makefile | 1 + drivers/gpio/sandbox.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++++ include/sandbox_gpio.h | 50 +++++++++++ 3 files changed, 264 insertions(+), 0 deletions(-) create mode 100644 drivers/gpio/sandbox.c create mode 100644 include/sandbox_gpio.h diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index f505813..f427127 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -33,6 +33,7 @@ COBJS-$(CONFIG_MXC_GPIO) += mxc_gpio.o COBJS-$(CONFIG_PCA953X) += pca953x.o COBJS-$(CONFIG_PCA9698) += pca9698.o COBJS-$(CONFIG_S5P) += s5p_gpio.o +COBJS-$(CONFIG_SANDBOX_GPIO) += sandbox.o COBJS-$(CONFIG_TEGRA2_GPIO) += tegra2_gpio.o COBJS-$(CONFIG_DA8XX_GPIO) += da8xx_gpio.o diff --git a/drivers/gpio/sandbox.c b/drivers/gpio/sandbox.c new file mode 100644 index 0000000..81ba803 --- /dev/null +++ b/drivers/gpio/sandbox.c @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2011 The Chromium OS Authors. + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include <common.h> +#include <asm/io.h> +#include <asm/bitops.h> +#include <asm/arch/gpio.h> + +enum { + CMD_INFO, + CMD_PORT, + CMD_OUTPUT, + CMD_INPUT, +}; + +/* Flags for each GPIO */ +enum { + GPIOF_OUTPUT = 1 << 1, + GPIOF_HIGH = 1 << 2, +}; + +/* TODO: Put this into sandbox state */ +static u8 state[GPIO_COUNT]; /* State of GPIOs */ + + +/* Access routines for GPIO state */ +static u8 *get_gpio(int gp) +{ + assert(gp >= 0 && gp < GPIO_COUNT); + return &state[gp]; +} + +static int get_gpio_flag(int gp, int flag) +{ + return *get_gpio(gp) & flag; +} + +static void set_gpio_flag(int gp, int flag, int value) +{ + u8 *gpio = get_gpio(gp); + + if (value) + *gpio |= flag; + else + *gpio &= ~flag; +} + +int sandbox_gpio_get_value(int gp) +{ + if (get_gpio_flag(gp, GPIOF_OUTPUT)) + printf("sandbox_gpio: get_value on output GPIO %d\n", gp); + return *get_gpio(gp) & GPIOF_HIGH; +} + +int sandbox_gpio_set_value(int gp, int value) +{ + set_gpio_flag(gp, GPIOF_HIGH, value); + return 0; +} + +int sandbox_gpio_get_direction(int gp) +{ + return get_gpio_flag(gp, GPIOF_OUTPUT); +} + +int sandbox_gpio_set_direction(int gp, int output) +{ + set_gpio_flag(gp, GPIOF_OUTPUT, output); + return 0; +} + + +/* These functions make up what could be a public interface within U-Boot */ + +/* set GPIO port 'gp' as an input */ +int gpio_direction_input(int gp) +{ + debug("gpio_direction_input: gp = %d\n", gp); + set_gpio_flag(gp, GPIOF_OUTPUT, 0); + return 0; +} + +/* set GPIO port 'gp' as an output, with polarity 'value' */ +int gpio_direction_output(int gp, int value) +{ + debug("gpio_direction_output: gp = %d, value = %d\n", + gp, value); + set_gpio_flag(gp, GPIOF_OUTPUT, 1); + return 0; +} + +/* read GPIO IN value of port 'gp' */ +int gpio_get_value(int gp) +{ + debug("gpio_get_value: gp = %d\n", gp); + if (get_gpio_flag(gp, GPIOF_OUTPUT)) + printf("sandbox_gpio: get_value on output GPIO %d\n", gp); + return *get_gpio(gp) & GPIOF_HIGH; +} + +/* write GPIO OUT value to port 'gp' */ +void gpio_set_value(int gp, int value) +{ + debug("gpio_set_value: gp = %d, value = %d\n", gp, value); + if (get_gpio_flag(gp, GPIOF_OUTPUT)) + set_gpio_flag(gp, GPIOF_HIGH, value); + else + printf("sandbox_gpio: set_value on input GPIO %d\n", gp); +} + +/* Display GPIO information */ +static int gpio_info(int gp) +{ + printf("Sandbox GPIOs\n"); + return 0; +} + +cmd_tbl_t cmd_gpio[] = { + U_BOOT_CMD_MKENT(offset2port, 3, 0, (void *)CMD_PORT, "", ""), + U_BOOT_CMD_MKENT(output, 4, 0, (void *)CMD_OUTPUT, "", ""), + U_BOOT_CMD_MKENT(input, 3, 0, (void *)CMD_INPUT, "", ""), +#ifdef CONFIG_CMD_GPIO_INFO + U_BOOT_CMD_MKENT(info, 3, 0, (void *)CMD_INFO, "", ""), +#endif +}; + +int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + static uint8_t offset; + int val __attribute__((unused)); + ulong ul_arg2, ul_arg3, ul_arg4; + cmd_tbl_t *c; + + ul_arg2 = ul_arg3 = ul_arg4 = 0; + + c = find_cmd_tbl(argv[1], cmd_gpio, ARRAY_SIZE(cmd_gpio)); + + /* All commands but "port" require 'maxargs' arguments */ + if (!c || !((argc == (c->maxargs)) || + (((long)c->cmd == CMD_PORT) && + (argc == (c->maxargs - 1))))) { + cmd_usage(cmdtp); + return 1; + } + + /* arg2 used as offset */ + if (argc > 2) + ul_arg2 = simple_strtoul(argv[2], NULL, 10); + + /* arg3 used as output value, 0 or 1 */ + if (argc > 3) + ul_arg3 = simple_strtoul(argv[3], NULL, 10) & 0x1; + + switch ((long)c->cmd) { + case CMD_INFO: + if (argc == 3) + offset = (uint8_t)ul_arg2; + return gpio_info(offset); + + case CMD_PORT: + if (argc == 3) + offset = (uint8_t)ul_arg2; + return 0; + + case CMD_INPUT: + /* arg2 = offset */ + gpio_direction_input(ul_arg2); + val = gpio_get_value(ul_arg2); + return 0; + + case CMD_OUTPUT: + /* args = offset, value */ + gpio_direction_output(ul_arg2, ul_arg3); + return 0; + + default: + /* We should never get here */ + return 1; + } +} + +U_BOOT_CMD( + gpio, 5, 1, do_gpio, + "GPIO access", + "offset2port offset\n" + " - show GPIO port:bit based on 'offset'\n" +#ifdef CONFIG_CMD_GPIO_INFO + " info offset\n" + " - display info for all bits in port based on 'offset'\n" +#endif + " output offset 0|1\n" + " - using 'offset', set port:bit as output and drive low or high\n" + " input offset\n" + " - using 'offset', set port:bit as input and read value" +); diff --git a/include/sandbox_gpio.h b/include/sandbox_gpio.h new file mode 100644 index 0000000..db92f7b --- /dev/null +++ b/include/sandbox_gpio.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2011 The Chromium OS Authors. + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +/** + * Return the value of a GPIO + * + * @param gp GPIO number + * @return -1 on error, 0 if GPIO is low, >0 if high + */ +int sandbox_gpio_get_value(int gp); + +/** + * @param gp GPIO number + * @param value value to set (0 for low, non-zero for high) + * @return -1 on error, 0 if ok + */ +int sandbox_gpio_set_value(int gp, int value); + +/** + * Return the direction of a GPIO + * + * @param gp GPIO number + * @return -1 on error, 0 if GPIO is input, >0 if output + */ +int sandbox_gpio_get_direction(int gp); + +/** + * @param gp GPIO number + * @param output 0 to set as input, 1 to set as output + * @return -1 on error, 0 if ok + */ +int sandbox_gpio_set_direction(int gp, int output); -- 1.7.3.1 _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot