GPIO reset support
v4 cover GPIO reset support
This series adds generic GPIO reset (poweroff/restart) support which is required by SiFive platforms.
These patches is on top of gpio_reset_v3 branch at:
https://github.com/avpatel/opensbi.git
Changes since v4:
- Added set() implementation
- Adjust mdelay loop count to '100000'
Changes since v3:
- Simplified GPIO reset driver in PATCH5
- Dropped PATCH6
Changes since v2:
- Re-designed the GPIO framework to probe GPIO drivers on demand and avoid touching generic OpenSBI library.
Anup Patel (3):
lib: utils/fdt: Add fdt_parse_phandle_with_args() API
lib: utils/gpio: Add generic GPIO configuration library
lib: utils/gpio: Add simple FDT based GPIO framework
Green Wan (2):
lib: utils/gpio: Add minimal SiFive GPIO driver
lib: utils/reset: Add generic GPIO reset driver
include/sbi_utils/fdt/fdt_helper.h | 11 +++ include/sbi_utils/gpio/fdt_gpio.h | 34 +++++++ include/sbi_utils/gpio/gpio.h | 99 ++++++++++++++++++++ lib/utils/fdt/fdt_helper.c | 47 ++++++++++ lib/utils/gpio/fdt_gpio.c | 135 +++++++++++++++++++++++++++ lib/utils/gpio/fdt_gpio_sifive.c | 105 +++++++++++++++++++++ lib/utils/gpio/gpio.c | 116 ++++++++++++++++++++++++ lib/utils/gpio/objects.mk | 12 +++ lib/utils/reset/fdt_reset.c | 2 + lib/utils/reset/fdt_reset_gpio.c | 141 +++++++++++++++++++++++++++++ lib/utils/reset/objects.mk | 1 + 11 files changed, 703 insertions(+) create mode 100644 include/sbi_utils/gpio/fdt_gpio.h create mode 100644 include/sbi_utils/gpio/gpio.h create mode 100644 lib/utils/gpio/fdt_gpio.c create mode 100644 lib/utils/gpio/fdt_gpio_sifive.c create mode 100644 lib/utils/gpio/gpio.c create mode 100644 lib/utils/gpio/objects.mk create mode 100644 lib/utils/reset/fdt_reset_gpio.c
Signature · 1 line
2.17.1
PATCH v4 1/5 lib: utils/fdt: Add fdt_parse_phandle_with_args() API
From: Anup Patel <[email protected]>
The libfdt project does not have a generic API to parse phandle with args from a DT node so we add fdt_parse_phandle_with_args() for this purpose. This new API will be useful to FDT based drivers.
--- include/sbi_utils/fdt/fdt_helper.h | 11 +++++++ lib/utils/fdt/fdt_helper.c | 47 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +11 -0
@@ -17,6 +17,13 @@ struct fdt_match {void *data; }; +#define FDT_MAX_PHANDLE_ARGS 16 +struct fdt_phandle_args { +int node_offset; +int args_count; +u32 args[FDT_MAX_PHANDLE_ARGS ]; +}; +struct platform_uart_data {unsigned long addr;unsigned long freq;
@@ -32,6 +39,10 @@ int fdt_find_match(void *fdt, int startoff,const struct fdt_match *match_table, const struct fdt_match **out_match); +int fdt_parse_phandle_with_args (void *fdt,int nodeoff, +const char *prop,const char *cells_prop, +int index,struct fdt_phandle_args *out_args); +int fdt_get_node_addr_size (void *fdt,int node,unsigned long *addr,unsigned long *size);
lib/utils/fdt/fdt_helper.c +47 -0
53 lines not on this page · raw message
Signature · 1 line
2.17.1
Anup Patel
AppliedPATCH v4 1/5 lib: utils/fdt: Add fdt_parse_phandle_with_args() API
Quoted 9 lines · Green Wan
From: Anup Patel <[email protected]>
The libfdt project does not have a generic API to parse phandle with args from a DT node so we add
fdt_parse_phandle_with_args()for this purpose. This new API will be useful to FDT based drivers.Signed-off-by Anup Patel <[email protected]>Reviewed-by Atish Patra <[email protected]>
Applied this patch to the riscv/opensbi repo
Thanks,
Anup
Quoted 100 lines
--- include/sbi_utils/fdt/fdt_helper.h | 11 +++++++ lib/utils/fdt/fdt_helper.c | 47 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)include/sbi_utils/fdt/fdt_helper.h +11
-0@@ -17,6 +17,13 @@ struct fdt_match {void *data; }; +#define FDT_MAX_PHANDLE_ARGS 16 +struct fdt_phandle_args { +int node_offset; +int args_count; +u32 args[FDT_MAX_PHANDLE_ARGS ]; +}; +struct platform_uart_data {unsigned long addr;unsigned long freq;@@ -32,6 +39,10 @@ int fdt_find_match(void *fdt, int startoff,const struct fdt_match *match_table, const struct fdt_match **out_match); +int fdt_parse_phandle_with_args (void *fdt,int nodeoff, +const char *prop,const char *cells_prop, +int index,struct fdt_phandle_args *out_args); +int fdt_get_node_addr_size (void *fdt,int node,unsigned long *addr,unsigned long *size);lib/utils/fdt/fdt_helper.c +47
-1@@ -72,6 +72,53 @@ int fdt_find_match(void *fdt, int startoff,return SBI_ENODEV ; } +int fdt_parse_phandle_with_args (void *fdt,int nodeoff, +const char *prop,const char *cells_prop, +int index,struct fdt_phandle_args *out_args) +{ +u32 i, pcells; +int len, pnodeoff; +const fdt32_t *list, *list_end, *val; + +if (!fdt || (nodeoff <0 ) || !prop || !cells_prop || !out_args) +return SBI_EINVAL ; + + list =fdt_getprop (fdt, nodeoff, prop, &len); +if (!list) +return SBI_ENOENT ; + list_end = list + (len /sizeof (*list)); + +while (list < list_end) { + pnodeoff =fdt_node_offset_by_phandle (fdt, +fdt32_to_cpu (*list)); +if (pnodeoff <0 ) +return pnodeoff; + list++; + + val =fdt_getprop (fdt, pnodeoff, cells_prop, &len); +if (!val) +return SBI_ENOENT ; + pcells =fdt32_to_cpu (*val); +if (FDT_MAX_PHANDLE_ARGS < pcells) +return SBI_EINVAL ; +if (list + pcells > list_end) +return SBI_ENOENT ; + +if (index >0 ) { + list += pcells; + index--; + }else { + out_args->node_offset = pnodeoff; + out_args->args_count = pcells; +for (i =0 ; i < pcells; i++) + out_args->args[i] =fdt32_to_cpu (list[i]); +return 0 ; + } + } + +return SBI_ENOENT ; +} +static int fdt_translate_address (void *fdt,uint64_t reg,int parent,unsigned long *addr) { --2.17.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 2/5 lib: utils/gpio: Add generic GPIO configuration library
In reply to Green WanFrom: Anup Patel <[email protected]>
We add generic GPIO configuration library which is independent of hardware description format (FDT or ACPI). The OpenSBI platform support or GPIO drivers can register GPIO chip instances which can be discovered and used by different GPIO clients. Each GPIO chip instance has a unique ID which can be used by GPIO clients to lookup GPIO chip instance.
--- include/sbi_utils/gpio/gpio.h | 99 +++++++++++++++++++++++++++++ lib/utils/gpio/gpio.c | 116 ++++++++++++++++++++++++++++++++++ lib/utils/gpio/objects.mk | 10 +++ 3 files changed, 225 insertions(+) create mode 100644 include/sbi_utils/gpio/gpio.h create mode 100644 lib/utils/gpio/gpio.c create mode 100644 lib/utils/gpio/objects.mk
include/sbi_utils/gpio/gpio.h new file +99 -0
@@ -0,0 +1,99 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* Authors: +* Anup Patel <[email protected]> +*/ + +#ifndef __GPIO_H__ +#define __GPIO_H__ + +#include <sbi/sbi_types.h> + +#define GPIO_LINE_DIRECTION_IN 1 +#define GPIO_LINE_DIRECTION_OUT 0 + +/** Representation of a GPIO pin */ +struct gpio_pin { +/** Pointer to the GPIO chip */ +struct gpio_chip *chip; +/** Identification of GPIO pin within GPIO chip */ +unsigned int offset; +/** +* Additional configuration flags of the GPIO pin desired +* by GPIO clients. +* +* NOTE: GPIO chip can have custom configuration flags. +*/ +unsigned int flags; +#define GPIO_FLAG_ACTIVE_LOW 0x1 +#define GPIO_FLAG_SINGLE_ENDED 0x2 +#define GPIO_FLAG_OPEN_DRAIN 0x4 +#define GPIO_FLAG_TRANSITORY 0x8 +#define GPIO_FLAG_PULL_UP 0x10 +#define GPIO_FLAG_PULL_DOWN 0x20 +}; + +/** Representation of a GPIO chip */ +struct gpio_chip { +/** Pointer to GPIO driver owning this GPIO chip */ +void *driver; +/** Uniquie ID of the GPIO chip assigned by the driver */ +unsigned int id; +/** Number of GPIOs supported by the GPIO chip */ +unsigned int ngpio; +/** +* Get current direction of GPIO pin +* +* @return 0=output, 1=input, or negative error +*/ +int (*get_direction)(struct gpio_pin *gp); +/** +* Set input direction of GPIO pin +* +* @return 0 on success and negative error code on failure +*/ +int (*direction_input)(struct gpio_pin *gp); +/** +* Set output direction of GPIO pin with given output value +* +* @return 0 on success and negative error code on failure +*/ +int (*direction_output)(struct gpio_pin *gp,int value); +/** +* Get current value of GPIO pin +* +* @return 0=low, 1=high, or negative error +*/ +int (*get)(struct gpio_pin *gp); +/** Set output value for GPIO pin */ +void (*set)(struct gpio_pin *gp,int value); +}; + +/** Find a registered GPIO chip */ +struct gpio_chip *gpio_chip_find (unsigned int id); + +/** Register GPIO chip */ +int gpio_chip_add (struct gpio_chip *gc); + +/** Un-register GPIO chip */ +void gpio_chip_remove (struct gpio_chip *gc); + +/** Get current direction of GPIO pin */ +int gpio_get_direction (struct gpio_pin *gp); + +/** Set input direction of GPIO pin */ +int gpio_direction_input (struct gpio_pin *gp); + +/** Set output direction of GPIO pin */ +int gpio_direction_output (struct gpio_pin *gp,int value); + +/** Get current value of GPIO pin */ +int gpio_get (struct gpio_pin *gp); + +/** Set output value of GPIO pin */ +int gpio_set (struct gpio_pin *gp,int value); + +#endif
lib/utils/gpio/gpio.c new file +116 -0
116 lines not on this page · raw message
lib/utils/gpio/objects.mk new file +10 -0
@@ -0,0 +1,10 @@ +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2021 Western Digital Corporation or its affiliates. +# +# Authors: +# Anup Patel <[email protected]> +# + +libsbiutils-objs-y += gpio/gpio.o
Signature · 1 line
2.17.1
Anup Patel
AppliedPATCH v4 2/5 lib: utils/gpio: Add generic GPIO configuration library
Quoted 11 lines · Green Wan
From: Anup Patel <[email protected]>
We add generic GPIO configuration library which is independent of hardware description format (FDT or ACPI). The OpenSBI platform support or GPIO drivers can register GPIO chip instances which can be discovered and used by different GPIO clients. Each GPIO chip instance has a unique ID which can be used by GPIO clients to lookup GPIO chip instance.
Signed-off-by Anup Patel <[email protected]>
Atish's reviewed-by is missing which I have added at time of merging
Applied this patch to the riscv/opensbi repo
Thanks,
Anup
Quoted 260 lines
--- include/sbi_utils/gpio/gpio.h | 99 +++++++++++++++++++++++++++++ lib/utils/gpio/gpio.c | 116 ++++++++++++++++++++++++++++++++++ lib/utils/gpio/objects.mk | 10 +++ 3 files changed, 225 insertions(+) create mode 100644 include/sbi_utils/gpio/gpio.h create mode 100644 lib/utils/gpio/gpio.c create mode 100644 lib/utils/gpio/objects.mkinclude/sbi_utils/gpio/gpio.h new file +99
-0@@ -0,0 +1,99 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* Authors: +* Anup Patel <[email protected]> +*/ + +#ifndef __GPIO_H__ +#define __GPIO_H__ + +#include <sbi/sbi_types.h> + +#define GPIO_LINE_DIRECTION_IN 1 +#define GPIO_LINE_DIRECTION_OUT 0 + +/** Representation of a GPIO pin */ +struct gpio_pin { +/** Pointer to the GPIO chip */ +struct gpio_chip *chip; +/** Identification of GPIO pin within GPIO chip */ +unsigned int offset; +/** +* Additional configuration flags of the GPIO pin desired +* by GPIO clients. +* +* NOTE: GPIO chip can have custom configuration flags. +*/ +unsigned int flags; +#define GPIO_FLAG_ACTIVE_LOW 0x1 +#define GPIO_FLAG_SINGLE_ENDED 0x2 +#define GPIO_FLAG_OPEN_DRAIN 0x4 +#define GPIO_FLAG_TRANSITORY 0x8 +#define GPIO_FLAG_PULL_UP 0x10 +#define GPIO_FLAG_PULL_DOWN 0x20 +}; + +/** Representation of a GPIO chip */ +struct gpio_chip { +/** Pointer to GPIO driver owning this GPIO chip */ +void *driver; +/** Uniquie ID of the GPIO chip assigned by the driver */ +unsigned int id; +/** Number of GPIOs supported by the GPIO chip */ +unsigned int ngpio; +/** +* Get current direction of GPIO pin +* +* @return 0=output, 1=input, or negative error +*/ +int (*get_direction)(struct gpio_pin *gp); +/** +* Set input direction of GPIO pin +* +* @return 0 on success and negative error code on failure +*/ +int (*direction_input)(struct gpio_pin *gp); +/** +* Set output direction of GPIO pin with given output value +* +* @return 0 on success and negative error code on failure +*/ +int (*direction_output)(struct gpio_pin *gp,int value); +/** +* Get current value of GPIO pin +* +* @return 0=low, 1=high, or negative error +*/ +int (*get)(struct gpio_pin *gp); +/** Set output value for GPIO pin */ +void (*set)(struct gpio_pin *gp,int value); +}; + +/** Find a registered GPIO chip */ +struct gpio_chip *gpio_chip_find (unsigned int id); + +/** Register GPIO chip */ +int gpio_chip_add (struct gpio_chip *gc); + +/** Un-register GPIO chip */ +void gpio_chip_remove (struct gpio_chip *gc); + +/** Get current direction of GPIO pin */ +int gpio_get_direction (struct gpio_pin *gp); + +/** Set input direction of GPIO pin */ +int gpio_direction_input (struct gpio_pin *gp); + +/** Set output direction of GPIO pin */ +int gpio_direction_output (struct gpio_pin *gp,int value); + +/** Get current value of GPIO pin */ +int gpio_get (struct gpio_pin *gp); + +/** Set output value of GPIO pin */ +int gpio_set (struct gpio_pin *gp,int value); + +#endif lib/utils/gpio/gpio.c new file +116
-0@@ -0,0 +1,116 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* Authors: +* Anup Patel <[email protected]> +*/ + +#include <sbi/sbi_error.h> +#include <sbi_utils/gpio/gpio.h> + +#define GPIO_CHIP_MAX 16 + +static struct gpio_chip *gc_array[GPIO_CHIP_MAX ]; + +struct gpio_chip *gpio_chip_find (unsigned int id) +{ +unsigned int i; +struct gpio_chip *ret =NULL ; + +for (i =0 ; i <GPIO_CHIP_MAX ; i++) { +if (gc_array[i] && gc_array[i]->id == id) { + ret = gc_array[i]; +break ; + } + } + +return ret; +} + +int gpio_chip_add (struct gpio_chip *gc) +{ +int i, ret =SBI_ENOSPC ; + +if (!gc) +return SBI_EINVAL ; +if (gpio_chip_find (gc->id)) +return SBI_EALREADY ; + +for (i =0 ; i <GPIO_CHIP_MAX ; i++) { +if (!gc_array[i]) { + gc_array[i] = gc; + ret =0 ; +break ; + } + } + +return ret; +} + +void gpio_chip_remove (struct gpio_chip *gc) +{ +int i; + +if (!gc) +return ; + +for (i =0 ; i <GPIO_CHIP_MAX ; i++) { +if (gc_array[i] == gc) { + gc_array[i] =NULL ; +break ; + } + } +} + +int gpio_get_direction (struct gpio_pin *gp) +{ +if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset)) +return SBI_EINVAL ; +if (!gp->chip->get_direction) +return SBI_ENOSYS ; + +return gp->chip->get_direction (gp); +} + +int gpio_direction_input (struct gpio_pin *gp) +{ +if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset)) +return SBI_EINVAL ; +if (!gp->chip->direction_input) +return SBI_ENOSYS ; + +return gp->chip->direction_input (gp); +} + +int gpio_direction_output (struct gpio_pin *gp,int value) +{ +if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset)) +return SBI_EINVAL ; +if (!gp->chip->direction_output) +return SBI_ENOSYS ; + +return gp->chip->direction_output (gp, value); +} + +int gpio_get (struct gpio_pin *gp) +{ +if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset)) +return SBI_EINVAL ; +if (!gp->chip->get) +return SBI_ENOSYS ; + +return gp->chip->get (gp); +} + +int gpio_set (struct gpio_pin *gp,int value) +{ +if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset)) +return SBI_EINVAL ; +if (!gp->chip->set) +return SBI_ENOSYS ; + + gp->chip->set (gp, value); +return 0 ; +}lib/utils/gpio/objects.mk new file +10
-1@@ -0,0 +1,10 @@ +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2021 Western Digital Corporation or its affiliates. +# +# Authors: +# Anup Patel <[email protected]> +# + +libsbiutils-objs-y += gpio/gpio.o --2.17.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 3/5 lib: utils/gpio: Add simple FDT based GPIO framework
In reply to Green WanFrom: Anup Patel <[email protected]>
We add a simple FDT based GPIO framework which is built on top of generic GPIO library. The phandle of FDT GPIO chip DT node is treated as unique GPIO chip ID required by the generic GPIO library. The FDT based GPIO chip drivers will be probed on-demand from fdt_gpio_pin_get() called by the GPIO client drivers.
--- include/sbi_utils/gpio/fdt_gpio.h | 34 ++++++++ lib/utils/gpio/fdt_gpio.c | 132 ++++++++++++++++++++++++++++++ lib/utils/gpio/objects.mk | 1 + 3 files changed, 167 insertions(+) create mode 100644 include/sbi_utils/gpio/fdt_gpio.h create mode 100644 lib/utils/gpio/fdt_gpio.c
include/sbi_utils/gpio/fdt_gpio.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* Authors: +* Anup Patel <[email protected]> +*/ + +#ifndef __FDT_GPIO_H__ +#define __FDT_GPIO_H__ + +#include <sbi_utils/gpio/gpio.h> + +/** FDT based GPIO driver */ +struct fdt_gpio { +const struct fdt_match *match_table; +int (*xlate)(struct gpio_chip *chip, +const struct fdt_phandle_args *pargs, +struct gpio_pin *out_pin); +int (*init)(void *fdt,int nodeoff,u32 phandle, +const struct fdt_match *match); +}; + +/** Get a GPIO pin using "gpios" DT property of client DT node */ +int fdt_gpio_pin_get (void *fdt,int nodeoff,int index, +struct gpio_pin *out_pin); + +/** Simple xlate function to convert two GPIO FDT cells into GPIO pin */ +int fdt_gpio_simple_xlate (struct gpio_chip *chip, +const struct fdt_phandle_args *pargs, +struct gpio_pin *out_pin); + +#endif
lib/utils/gpio/fdt_gpio.c new file +132 -0
132 lines not on this page · raw message
lib/utils/gpio/objects.mk +1 -0
@@ -7,4 +7,5 @@ # Anup Patel <[email protected]> # +libsbiutils-objs-y += gpio/fdt_gpio.o libsbiutils-objs-y += gpio/gpio.o
Signature · 1 line
2.17.1
Anup Patel
AppliedPATCH v4 3/5 lib: utils/gpio: Add simple FDT based GPIO framework
Quoted 11 lines · Green Wan
From: Anup Patel <[email protected]>
We add a simple FDT based GPIO framework which is built on top of generic GPIO library. The phandle of FDT GPIO chip DT node is treated as unique GPIO chip ID required by the generic GPIO library. The FDT based GPIO chip drivers will be probed on-demand from
fdt_gpio_pin_get()called by the GPIO client drivers.Signed-off-by Anup Patel <[email protected]>Reviewed-by Atish Patra <[email protected]>
Applied this patch to the riscv/opensbi repo
Thanks,
Anup
Quoted 204 lines
--- include/sbi_utils/gpio/fdt_gpio.h | 34 ++++++++ lib/utils/gpio/fdt_gpio.c | 132 ++++++++++++++++++++++++++++++ lib/utils/gpio/objects.mk | 1 + 3 files changed, 167 insertions(+) create mode 100644 include/sbi_utils/gpio/fdt_gpio.h create mode 100644 lib/utils/gpio/fdt_gpio.cinclude/sbi_utils/gpio/fdt_gpio.h new file +34
-0@@ -0,0 +1,34 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* Authors: +* Anup Patel <[email protected]> +*/ + +#ifndef __FDT_GPIO_H__ +#define __FDT_GPIO_H__ + +#include <sbi_utils/gpio/gpio.h> + +/** FDT based GPIO driver */ +struct fdt_gpio { +const struct fdt_match *match_table; +int (*xlate)(struct gpio_chip *chip, +const struct fdt_phandle_args *pargs, +struct gpio_pin *out_pin); +int (*init)(void *fdt,int nodeoff,u32 phandle, +const struct fdt_match *match); +}; + +/** Get a GPIO pin using "gpios" DT property of client DT node */ +int fdt_gpio_pin_get (void *fdt,int nodeoff,int index, +struct gpio_pin *out_pin); + +/** Simple xlate function to convert two GPIO FDT cells into GPIO pin */ +int fdt_gpio_simple_xlate (struct gpio_chip *chip, +const struct fdt_phandle_args *pargs, +struct gpio_pin *out_pin); + +#endif lib/utils/gpio/fdt_gpio.c new file +132
-0@@ -0,0 +1,132 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* Authors: +* Anup Patel <[email protected]> +*/ + +#include <libfdt.h> +#include <sbi/sbi_error.h> +#include <sbi_utils/fdt/fdt_helper.h> +#include <sbi_utils/gpio/fdt_gpio.h> + +static struct fdt_gpio *gpio_drivers[] = { +}; + +static struct fdt_gpio *fdt_gpio_driver (struct gpio_chip *chip) +{ +int pos; + +if (!chip) +return NULL ; + +for (pos =0 ; pos <array_size (gpio_drivers); pos++) { +if (chip->driver == gpio_drivers[pos]) +return gpio_drivers[pos]; + } + +return NULL ; +} + +static int fdt_gpio_init (void *fdt,u32 phandle) +{ +int pos, nodeoff, rc; +struct fdt_gpio *drv; +const struct fdt_match *match; + +/* Find node offset */ + nodeoff =fdt_node_offset_by_phandle (fdt, phandle); +if (nodeoff <0 ) +return nodeoff; + +/* Check "gpio-controller" property */ +if (!fdt_getprop (fdt, nodeoff,"gpio-controller" , &rc)) +return SBI_EINVAL ; + +/* Try all GPIO drivers one-by-one */ +for (pos =0 ; pos <array_size (gpio_drivers); pos++) { + drv = gpio_drivers[pos]; + + match =fdt_match_node (fdt, nodeoff, drv->match_table); +if (match && drv->init) { + rc = drv->init (fdt, nodeoff, phandle, match); +if (rc ==SBI_ENODEV ) +continue ; +if (rc) +return rc; +return 0 ; + } + } + +return SBI_ENOSYS ; +} + +static int fdt_gpio_chip_find (void *fdt,u32 phandle, +struct gpio_chip **out_chip) +{ +int rc; +struct gpio_chip *chip =gpio_chip_find (phandle); + +if (!chip) { +/* GPIO chip not found so initialize matching driver */ + rc =fdt_gpio_init (fdt, phandle); +if (rc) +return rc; + +/* Try to find GPIO chip again */ + chip =gpio_chip_find (phandle); +if (!chip) +return SBI_ENOSYS ; + } + +if (out_chip) + *out_chip = chip; + +return 0 ; +} + +int fdt_gpio_pin_get (void *fdt,int nodeoff,int index, +struct gpio_pin *out_pin) +{ +int rc; +u32 phandle; +struct fdt_gpio *drv; +struct gpio_chip *chip =NULL ; +struct fdt_phandle_args pargs; + +if (!fdt || (nodeoff <0 ) || (index <0 ) || !out_pin) +return SBI_EINVAL ; + + pargs.node_offset = pargs.args_count =0 ; + rc =fdt_parse_phandle_with_args (fdt, nodeoff, +"gpios" ,"#gpio-cells" , + index, &pargs); +if (rc) +return rc; + + phandle =fdt_get_phandle (fdt, pargs.node_offset); + rc =fdt_gpio_chip_find (fdt, phandle, &chip); +if (rc) +return rc; + + drv =fdt_gpio_driver (chip); +if (!drv || !drv->xlate) +return SBI_ENOSYS ; + +return drv->xlate (chip, &pargs, out_pin); +} + +int fdt_gpio_simple_xlate (struct gpio_chip *chip, +const struct fdt_phandle_args *pargs, +struct gpio_pin *out_pin) +{ +if ((pargs->args_count <2 ) || (chip->ngpio <= pargs->args[0 ])) +return SBI_EINVAL ; + + out_pin->chip = chip; + out_pin->offset = pargs->args[0 ]; + out_pin->flags = pargs->args[1 ]; +return 0 ; +}lib/utils/gpio/objects.mk +1
-1@@ -7,4 +7,5 @@ # Anup Patel <[email protected]> # +libsbiutils-objs-y += gpio/fdt_gpio.o libsbiutils-objs-y += gpio/gpio.o --2.17.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 4/5 lib: utils/gpio: Add minimal SiFive GPIO driver
In reply to Green WanWe add a minimal SiFive GPIO driver so that we can do GPIO based system power-off and reset.
--- lib/utils/gpio/fdt_gpio.c | 3 + lib/utils/gpio/fdt_gpio_sifive.c | 105 +++++++++++++++++++++++++++++++ lib/utils/gpio/objects.mk | 1 + 3 files changed, 109 insertions(+) create mode 100644 lib/utils/gpio/fdt_gpio_sifive.c
lib/utils/gpio/fdt_gpio.c +3 -0
@@ -12,7 +12,10 @@#include <sbi_utils/fdt/fdt_helper.h> #include <sbi_utils/gpio/fdt_gpio.h> +extern struct fdt_gpio fdt_gpio_sifive; +static struct fdt_gpio *gpio_drivers[] = { + &fdt_gpio_sifive };static struct fdt_gpio *fdt_gpio_driver (struct gpio_chip *chip)
lib/utils/gpio/fdt_gpio_sifive.c new file +105 -0
105 lines not on this page · raw message
lib/utils/gpio/objects.mk +1 -0
@@ -8,4 +8,5 @@ # libsbiutils-objs-y += gpio/fdt_gpio.o +libsbiutils-objs-y += gpio/fdt_gpio_sifive.o libsbiutils-objs-y += gpio/gpio.o
Signature · 1 line
2.17.1
Anup Patel
AppliedPATCH v4 4/5 lib: utils/gpio: Add minimal SiFive GPIO driver
Quoted 6 lines · Green Wan
We add a minimal SiFive GPIO driver so that we can do GPIO based system power-off and reset.
Signed-off-by Green Wan <[email protected]>Signed-off-by Anup Patel <[email protected]>
I have added a missing reviewed-by from Atish at time of merging.
Applied this patch to the riscv/opensbi repo
Thanks,
Anup
Quoted 151 lines
--- lib/utils/gpio/fdt_gpio.c | 3 + lib/utils/gpio/fdt_gpio_sifive.c | 105 +++++++++++++++++++++++++++++++ lib/utils/gpio/objects.mk | 1 + 3 files changed, 109 insertions(+) create mode 100644 lib/utils/gpio/fdt_gpio_sifive.clib/utils/gpio/fdt_gpio.c +3
-0@@ -12,7 +12,10 @@#include <sbi_utils/fdt/fdt_helper.h> #include <sbi_utils/gpio/fdt_gpio.h> +extern struct fdt_gpio fdt_gpio_sifive; +static struct fdt_gpio *gpio_drivers[] = { + &fdt_gpio_sifive };static struct fdt_gpio *fdt_gpio_driver (struct gpio_chip *chip)lib/utils/gpio/fdt_gpio_sifive.c new file +105
-0@@ -0,0 +1,105 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2021 SiFive +* +* Authors: +* Green Wan <[email protected]> +*/ + +#include <sbi/riscv_io.h> +#include <sbi/sbi_error.h> +#include <sbi_utils/fdt/fdt_helper.h> +#include <sbi_utils/gpio/fdt_gpio.h> + +#define SIFIVE_GPIO_CHIP_MAX 2 + +#define SIFIVE_GPIO_PINS_MIN 1 +#define SIFIVE_GPIO_PINS_MAX 32 +#define SIFIVE_GPIO_PINS_DEF 16 + +#define SIFIVE_GPIO_OUTEN 0x8 +#define SIFIVE_GPIO_OUTVAL 0xc +#define SIFIVE_GPIO_BIT (b) (1U << (b)) + +struct sifive_gpio_chip { +unsigned long addr; +struct gpio_chip chip; +}; + +static unsigned int sifive_gpio_chip_count; +static struct sifive_gpio_chip sifive_gpio_chip_array[SIFIVE_GPIO_CHIP_MAX ]; + +static int sifive_gpio_direction_output (struct gpio_pin *gp,int value) +{ +unsigned int v; +struct sifive_gpio_chip *chip = + container_of(gp->chip, struct sifive_gpio_chip, chip); + + v =readl ((volatile void *)(chip->addr +SIFIVE_GPIO_OUTEN )); + v |=SIFIVE_GPIO_BIT (gp->offset); +writel (v, (volatile void *)(chip->addr +SIFIVE_GPIO_OUTEN )); + + v =readl ((volatile void *)(chip->addr +SIFIVE_GPIO_OUTVAL )); +if (!value) + v &= ~SIFIVE_GPIO_BIT (gp->offset); +else + v |=SIFIVE_GPIO_BIT (gp->offset); +writel (v, (volatile void *)(chip->addr +SIFIVE_GPIO_OUTVAL )); + +return 0 ; +} + +static void sifive_gpio_set (struct gpio_pin *gp,int value) +{ +unsigned int v; +struct sifive_gpio_chip *chip = + container_of(gp->chip, struct sifive_gpio_chip, chip); + + v =readl ((volatile void *)(chip->addr +SIFIVE_GPIO_OUTVAL )); +if (!value) + v &= ~SIFIVE_GPIO_BIT (gp->offset); +else + v |=SIFIVE_GPIO_BIT (gp->offset); +writel (v, (volatile void *)(chip->addr +SIFIVE_GPIO_OUTVAL )); +} + +extern struct fdt_gpio fdt_gpio_sifive; + +static int sifive_gpio_init (void *fdt,int nodeoff,u32 phandle, +const struct fdt_match *match) +{ +int rc; +struct sifive_gpio_chip *chip; + +if (SIFIVE_GPIO_CHIP_MAX <= sifive_gpio_chip_count) +return SBI_ENOSPC ; + chip = &sifive_gpio_chip_array[sifive_gpio_chip_count]; + + rc =fdt_get_node_addr_size (fdt, nodeoff, &chip->addr,NULL ); +if (rc) +return rc; + + chip->chip.driver = &fdt_gpio_sifive; + chip->chip.id = phandle; + chip->chip.ngpio =SIFIVE_GPIO_PINS_DEF ; + chip->chip.direction_output = sifive_gpio_direction_output; + chip->chip.set = sifive_gpio_set; + rc =gpio_chip_add (&chip->chip); +if (rc) +return rc; + + sifive_gpio_chip_count++; +return 0 ; +} + +static const struct fdt_match sifive_gpio_match[] = { + { .compatible ="sifive,gpio0" }, + { }, +}; + +struct fdt_gpio fdt_gpio_sifive = { + .match_table = sifive_gpio_match, + .xlate = fdt_gpio_simple_xlate, + .init = sifive_gpio_init, +};lib/utils/gpio/objects.mk +1
-1@@ -8,4 +8,5 @@ # libsbiutils-objs-y += gpio/fdt_gpio.o +libsbiutils-objs-y += gpio/fdt_gpio_sifive.o libsbiutils-objs-y += gpio/gpio.o --2.17.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 5/5 lib: utils/reset: Add generic GPIO reset driver
In reply to Green WanWe add generic GPIO reset driver inspired from gpio-restart and gpio-poweroff drivers of Linux kernel.
--- lib/utils/reset/fdt_reset.c | 2 + lib/utils/reset/fdt_reset_gpio.c | 141 +++++++++++++++++++++++++++++++ lib/utils/reset/objects.mk | 1 + 3 files changed, 144 insertions(+) create mode 100644 lib/utils/reset/fdt_reset_gpio.c
lib/utils/reset/fdt_reset.c +2 -0
@@ -12,11 +12,13 @@#include <sbi_utils/fdt/fdt_helper.h> #include <sbi_utils/reset/fdt_reset.h> +extern struct fdt_reset fdt_reset_gpio;extern struct fdt_reset fdt_reset_sifive_test;extern struct fdt_reset fdt_reset_htif;extern struct fdt_reset fdt_reset_thead;static struct fdt_reset *reset_drivers[] = { + &fdt_reset_gpio, &fdt_reset_sifive_test, &fdt_reset_htif, &fdt_reset_thead,
lib/utils/reset/fdt_reset_gpio.c new file +141 -0
141 lines not on this page · raw message
lib/utils/reset/objects.mk +1 -0
@@ -8,6 +8,7 @@ # libsbiutils-objs-y += reset/fdt_reset.o +libsbiutils-objs-y += reset/fdt_reset_gpio.o libsbiutils-objs-y += reset/fdt_reset_htif.o libsbiutils-objs-y += reset/fdt_reset_thead.o libsbiutils-objs-y += reset/fdt_reset_thead_asm.o
Signature · 1 line
2.17.1
Anup Patel
AppliedPATCH v4 5/5 lib: utils/reset: Add generic GPIO reset driver
Quoted 6 lines · Green Wan
We add generic GPIO reset driver inspired from gpio-restart and gpio-poweroff drivers of Linux kernel.
Signed-off-by Green Wan <[email protected]>Signed-off-by Anup Patel <[email protected]>
I have added a missing reviewed-by from Atish at time of merging.
Applied this patch to the riscv/opensbi repo
Thanks,
Anup
Quoted 192 lines
--- lib/utils/reset/fdt_reset.c | 2 + lib/utils/reset/fdt_reset_gpio.c | 141 +++++++++++++++++++++++++++++++ lib/utils/reset/objects.mk | 1 + 3 files changed, 144 insertions(+) create mode 100644 lib/utils/reset/fdt_reset_gpio.clib/utils/reset/fdt_reset.c +2
-0@@ -12,11 +12,13 @@#include <sbi_utils/fdt/fdt_helper.h> #include <sbi_utils/reset/fdt_reset.h> +extern struct fdt_reset fdt_reset_gpio;extern struct fdt_reset fdt_reset_sifive_test;extern struct fdt_reset fdt_reset_htif;extern struct fdt_reset fdt_reset_thead;static struct fdt_reset *reset_drivers[] = { + &fdt_reset_gpio, &fdt_reset_sifive_test, &fdt_reset_htif, &fdt_reset_thead,lib/utils/reset/fdt_reset_gpio.c new file +141
-0@@ -0,0 +1,141 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Copyright (c) 2021 SiFive +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* Authors: +* Green Wan <[email protected]> +* Anup Patel <[email protected]> +*/ + +#include <libfdt.h> +#include <sbi/sbi_console.h> +#include <sbi/sbi_ecall_interface.h> +#include <sbi/sbi_hart.h> +#include <sbi/sbi_system.h> +#include <sbi_utils/fdt/fdt_helper.h> +#include <sbi_utils/gpio/fdt_gpio.h> +#include <sbi_utils/reset/fdt_reset.h> + +struct gpio_reset { +struct gpio_pin pin; +u32 active_delay; +u32 inactive_delay; +}; + +static struct gpio_reset poweroff = { + .active_delay =100 , + .inactive_delay =100 +}; + +static struct gpio_reset restart = { + .active_delay =100 , + .inactive_delay =100 +}; + +/* Custom mdelay function until we have a generic mdelay() API */ +static void gpio_mdelay (unsigned long msecs) +{ +volatile int i; +while (msecs--) +for (i =0 ; i <100000 ; i++) ; +} + +static int gpio_system_reset_check (u32 type,u32 reason) +{ +switch (type) { +case SBI_SRST_RESET_TYPE_SHUTDOWN : +case SBI_SRST_RESET_TYPE_COLD_REBOOT : +case SBI_SRST_RESET_TYPE_WARM_REBOOT : +return 1 ; + } + +return 0 ; +} + +static void gpio_system_reset (u32 type,u32 reason) +{ +struct gpio_reset *reset =NULL ; + +switch (type) { +case SBI_SRST_RESET_TYPE_SHUTDOWN : + reset = &poweroff; +break ; +case SBI_SRST_RESET_TYPE_COLD_REBOOT : +case SBI_SRST_RESET_TYPE_WARM_REBOOT : + reset = &restart; +break ; + } + +if (reset) { +if (!reset->pin.chip) { +sbi_printf ("%s: gpio pin not available\n" , __func__); + goto skip_reset; + } + +/* drive it active, also inactive->active edge */ +gpio_direction_output (&reset->pin,1 ); +gpio_mdelay (reset->active_delay); + +/* drive inactive, also active->inactive edge */ +gpio_set (&reset->pin,0 ); +gpio_mdelay (reset->inactive_delay); + +/* drive it active, also inactive->active edge */ +gpio_set (&reset->pin,1 ); + +skip_reset: +/* hang !!! */ +sbi_hart_hang (); + } +} + +static struct sbi_system_reset_device gpio_reset = { + .name ="gpio-reset" , + .system_reset_check = gpio_system_reset_check, + .system_reset = gpio_system_reset +}; + +static int gpio_reset_init (void *fdt,int nodeoff, +const struct fdt_match *match) +{ +int rc, len; +const fdt32_t *val; +bool is_restart = (ulong )match->data; +const char *dir_prop = (is_restart) ?"open-source" :"input" ; +struct gpio_reset *reset = (is_restart) ? &restart : &poweroff; + + rc =fdt_gpio_pin_get (fdt, nodeoff,0 , &reset->pin); +if (rc) +return rc; + +if (fdt_getprop (fdt, nodeoff, dir_prop, &len)) { + rc =gpio_direction_input (&reset->pin); +if (rc) +return rc; + } + + val =fdt_getprop (fdt, nodeoff,"active-delay-ms" , &len); +if (len >0 ) + reset->active_delay =fdt32_to_cpu (*val); + + val =fdt_getprop (fdt, nodeoff,"inactive-delay-ms" , &len); +if (len >0 ) + reset->inactive_delay =fdt32_to_cpu (*val); + +sbi_system_reset_set_device (&gpio_reset); + +return 0 ; +} + +static const struct fdt_match gpio_reset_match[] = { + { .compatible ="gpio-poweroff" , .data = (void *)FALSE }, + { .compatible ="gpio-restart" , .data = (void *)TRUE }, + { }, +}; + +struct fdt_reset fdt_reset_gpio = { + .match_table = gpio_reset_match, + .init = gpio_reset_init, +};lib/utils/reset/objects.mk +1
-1@@ -8,6 +8,7 @@ # libsbiutils-objs-y += reset/fdt_reset.o +libsbiutils-objs-y += reset/fdt_reset_gpio.o libsbiutils-objs-y += reset/fdt_reset_htif.o libsbiutils-objs-y += reset/fdt_reset_thead.o libsbiutils-objs-y += reset/fdt_reset_thead_asm.o --2.17.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi