diff options
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/Kconfig | 2 | ||||
-rw-r--r-- | drivers/Makefile | 1 | ||||
-rw-r--r-- | drivers/reboot-mode/Kconfig | 36 | ||||
-rw-r--r-- | drivers/reboot-mode/Makefile | 9 | ||||
-rw-r--r-- | drivers/reboot-mode/reboot-mode-gpio.c | 128 | ||||
-rw-r--r-- | drivers/reboot-mode/reboot-mode-rtc.c | 127 | ||||
-rw-r--r-- | drivers/reboot-mode/reboot-mode-uclass.c | 134 |
7 files changed, 437 insertions, 0 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig index c9c812b7526..417d6f88c29 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -92,6 +92,8 @@ source "drivers/qe/Kconfig" source "drivers/ram/Kconfig" +source "drivers/reboot-mode/Kconfig" + source "drivers/remoteproc/Kconfig" source "drivers/reset/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index 40812891046..82d3c98e063 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -95,6 +95,7 @@ obj-y += dfu/ obj-$(CONFIG_PCH) += pch/ obj-y += phy/allwinner/ obj-y += phy/marvell/ +obj-$(CONFIG_DM_REBOOT_MODE) += reboot-mode/ obj-y += phy/rockchip/ obj-y += phy/socionext/ obj-y += rtc/ diff --git a/drivers/reboot-mode/Kconfig b/drivers/reboot-mode/Kconfig new file mode 100644 index 00000000000..ac67bfcef62 --- /dev/null +++ b/drivers/reboot-mode/Kconfig @@ -0,0 +1,36 @@ +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (c), Vaisala Oyj +# + +menu "Reboot Mode Support" + +config DM_REBOOT_MODE + bool "Enable reboot mode using Driver Model" + depends on DM + default n + help + Enable support for reboot mode control. This will allow users to + adjust the boot process based on reboot mode parameter + passed to U-Boot. + +config DM_REBOOT_MODE_GPIO + bool "Use GPIOs as reboot mode backend" + depends on DM_REBOOT_MODE + default n + help + Use GPIOs to control the reboot mode. This will allow users to boot + a device in a specific mode by using a GPIO that can be controlled + outside U-Boot. + +config DM_REBOOT_MODE_RTC + bool "Use RTC as reboot mode backend" + depends on DM_REBOOT_MODE + default n + help + Use RTC non volatile memory to control the reboot mode. This will allow users to boot + a device in a specific mode by using a register(s) that can be controlled + outside U-Boot (e.g. Kernel). + +endmenu diff --git a/drivers/reboot-mode/Makefile b/drivers/reboot-mode/Makefile new file mode 100644 index 00000000000..2c13780ced4 --- /dev/null +++ b/drivers/reboot-mode/Makefile @@ -0,0 +1,9 @@ +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (c), Vaisala Oyj +# + +obj-$(CONFIG_DM_REBOOT_MODE) += reboot-mode-uclass.o +obj-$(CONFIG_DM_REBOOT_MODE_GPIO) += reboot-mode-gpio.o +obj-$(CONFIG_DM_REBOOT_MODE_RTC) += reboot-mode-rtc.o diff --git a/drivers/reboot-mode/reboot-mode-gpio.c b/drivers/reboot-mode/reboot-mode-gpio.c new file mode 100644 index 00000000000..305174736ed --- /dev/null +++ b/drivers/reboot-mode/reboot-mode-gpio.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c), Vaisala Oyj + */ + +#include <common.h> +#include <asm/gpio.h> +#include <dm.h> +#include <dm/devres.h> +#include <errno.h> +#include <reboot-mode/reboot-mode-gpio.h> +#include <reboot-mode/reboot-mode.h> + +DECLARE_GLOBAL_DATA_PTR; + +static int reboot_mode_get(struct udevice *dev, u32 *buf) +{ + int ret; + struct reboot_mode_gpio_platdata *plat_data; + + if (!buf) + return -EINVAL; + + plat_data = dev_get_plat(dev); + if (!plat_data) + return -EINVAL; + + ret = dm_gpio_get_values_as_int(plat_data->gpio_desc, + plat_data->gpio_count); + if (ret < 0) + return ret; + + *buf = ret; + + return 0; +} + +static int reboot_mode_probe(struct udevice *dev) +{ + struct reboot_mode_gpio_platdata *plat_data; + + plat_data = dev_get_plat(dev); + if (!plat_data) + return -EINVAL; + + int ret; + +#if CONFIG_IS_ENABLED(OF_CONTROL) + ret = gpio_get_list_count(dev, "gpios"); + if (ret < 0) + return ret; + + plat_data->gpio_count = ret; +#endif + + if (plat_data->gpio_count <= 0) + return -EINVAL; + + plat_data->gpio_desc = devm_kcalloc(dev, plat_data->gpio_count, + sizeof(struct gpio_desc), 0); + if (!plat_data->gpio_desc) + return -ENOMEM; + +#if CONFIG_IS_ENABLED(OF_CONTROL) + ret = gpio_request_list_by_name(dev, "gpios", plat_data->gpio_desc, + plat_data->gpio_count, GPIOD_IS_IN); + if (ret < 0) + return ret; +#else + for (int i = 0; i < plat_data->gpio_count; i++) { + struct reboot_mode_gpio_config *gpio = + plat_data->gpios_config + i; + struct gpio_desc *desc = plat_data->gpio_desc + i; + + ret = uclass_get_device_by_seq(UCLASS_GPIO, + gpio->gpio_dev_offset, + &desc->dev); + if (ret < 0) + return ret; + + desc->flags = gpio->flags; + desc->offset = gpio->gpio_offset; + + ret = dm_gpio_request(desc, ""); + if (ret < 0) + return ret; + + ret = dm_gpio_set_dir(desc); + if (ret < 0) + return ret; + } +#endif + return 0; +} + +static int reboot_mode_remove(struct udevice *dev) +{ + struct reboot_mode_gpio_platdata *plat_data; + + plat_data = dev_get_plat(dev); + if (!plat_data) + return -EINVAL; + + return gpio_free_list(dev, plat_data->gpio_desc, plat_data->gpio_count); +} + +#if CONFIG_IS_ENABLED(OF_CONTROL) +static const struct udevice_id reboot_mode_ids[] = { + { .compatible = "reboot-mode-gpio", 0 }, + { } +}; +#endif + +static const struct reboot_mode_ops reboot_mode_gpio_ops = { + .get = reboot_mode_get, +}; + +U_BOOT_DRIVER(reboot_mode_gpio) = { + .name = "reboot-mode-gpio", + .id = UCLASS_REBOOT_MODE, + .probe = reboot_mode_probe, + .remove = reboot_mode_remove, +#if CONFIG_IS_ENABLED(OF_CONTROL) + .of_match = reboot_mode_ids, +#endif + .plat_auto = sizeof(struct reboot_mode_gpio_platdata), + .ops = &reboot_mode_gpio_ops, +}; diff --git a/drivers/reboot-mode/reboot-mode-rtc.c b/drivers/reboot-mode/reboot-mode-rtc.c new file mode 100644 index 00000000000..972d0cdbcb5 --- /dev/null +++ b/drivers/reboot-mode/reboot-mode-rtc.c @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c), Vaisala Oyj + */ + +#include <common.h> +#include <dm.h> +#include <dm/device_compat.h> +#include <reboot-mode/reboot-mode-rtc.h> +#include <reboot-mode/reboot-mode.h> +#include <rtc.h> + +DECLARE_GLOBAL_DATA_PTR; + +static int reboot_mode_get(struct udevice *dev, u32 *buf) +{ + if (!buf) + return -EINVAL; + + int ret; + u8 *val = (u8 *)buf; + struct reboot_mode_rtc_platdata *plat_data; + + plat_data = dev_get_plat(dev); + if (!plat_data) + return -EINVAL; + + for (int i = 0; i < plat_data->size; i++) { + ret = rtc_read8(plat_data->rtc, plat_data->addr + i); + if (ret < 0) + return ret; + + val[i] = ret; + } + + if (plat_data->is_big_endian) + *buf = __be32_to_cpu(*buf); + else + *buf = __le32_to_cpu(*buf); + + return 0; +} + +static int reboot_mode_set(struct udevice *dev, u32 buf) +{ + int ret; + u8 *val; + struct reboot_mode_rtc_platdata *plat_data; + + plat_data = dev_get_plat(dev); + if (!plat_data) + return -EINVAL; + + if (plat_data->is_big_endian) + buf = __cpu_to_be32(buf); + else + buf = __cpu_to_le32(buf); + + val = (u8 *)&buf; + + for (int i = 0; i < plat_data->size; i++) { + ret = rtc_write8(plat_data->rtc, (plat_data->addr + i), val[i]); + if (ret < 0) + return ret; + } + + return 0; +} + +#if CONFIG_IS_ENABLED(OF_CONTROL) +static int reboot_mode_ofdata_to_platdata(struct udevice *dev) +{ + struct ofnode_phandle_args phandle_args; + struct reboot_mode_rtc_platdata *plat_data; + + plat_data = dev_get_plat(dev); + if (!plat_data) + return -EINVAL; + + if (dev_read_phandle_with_args(dev, "rtc", NULL, 0, 0, &phandle_args)) { + dev_err(dev, "RTC device not specified\n"); + return -ENOENT; + } + + if (uclass_get_device_by_ofnode(UCLASS_RTC, phandle_args.node, + &plat_data->rtc)) { + dev_err(dev, "could not get the RTC device\n"); + return -ENODEV; + } + + plat_data->addr = + dev_read_addr_size_index(dev, 0, (fdt_size_t *)&plat_data->size); + if (plat_data->addr == FDT_ADDR_T_NONE) { + dev_err(dev, "Invalid RTC address\n"); + return -EINVAL; + } + if (plat_data->size > sizeof(u32)) { + dev_err(dev, "Invalid reg size\n"); + return -EINVAL; + } + + plat_data->is_big_endian = ofnode_read_bool(dev_ofnode(dev), "big-endian"); + + return 0; +} + +static const struct udevice_id reboot_mode_ids[] = { + { .compatible = "reboot-mode-rtc", 0 }, + {} +}; +#endif + +static const struct reboot_mode_ops reboot_mode_rtc_ops = { + .get = reboot_mode_get, + .set = reboot_mode_set, +}; + +U_BOOT_DRIVER(reboot_mode_rtc) = { + .name = "reboot-mode-rtc", + .id = UCLASS_REBOOT_MODE, +#if CONFIG_IS_ENABLED(OF_CONTROL) + .of_match = reboot_mode_ids, + .of_to_plat = reboot_mode_ofdata_to_platdata, +#endif + .plat_auto = sizeof(struct reboot_mode_rtc_platdata), + .ops = &reboot_mode_rtc_ops, +}; diff --git a/drivers/reboot-mode/reboot-mode-uclass.c b/drivers/reboot-mode/reboot-mode-uclass.c new file mode 100644 index 00000000000..bb7a355fbf8 --- /dev/null +++ b/drivers/reboot-mode/reboot-mode-uclass.c @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c), Vaisala Oyj + */ + +#include <common.h> +#include <dm.h> +#include <dm/device_compat.h> +#include <dm/devres.h> +#include <exports.h> +#include <reboot-mode/reboot-mode.h> + +DECLARE_GLOBAL_DATA_PTR; + +int dm_reboot_mode_update(struct udevice *dev) +{ + struct reboot_mode_ops *ops = reboot_mode_get_ops(dev); + u32 rebootmode; + int ret, i; + + assert(ops); + + if (!ops->get) + return -ENOSYS; + + ret = ops->get(dev, &rebootmode); + if (ret < 0) { + dev_err(dev, "Failed to retrieve the reboot mode value\n"); + return ret; + } + + const struct reboot_mode_uclass_platdata *plat_data = + dev_get_uclass_plat(dev); + + for (i = 0; i < plat_data->count; i++) { + if (plat_data->modes[i].mode_id == rebootmode) { + ret = env_set(plat_data->env_variable, + plat_data->modes[i].mode_name); + if (ret) { + dev_err(dev, "Failed to set env: %s\n", + plat_data->env_variable); + return ret; + } + } + } + + if (ops->set) { + /* Clear the value */ + rebootmode = 0; + ret = ops->set(dev, rebootmode); + if (ret) { + dev_err(dev, "Failed to clear the reboot mode\n"); + return ret; + } + } + + return 0; +} + +int dm_reboot_mode_pre_probe(struct udevice *dev) +{ + struct reboot_mode_uclass_platdata *plat_data; + + plat_data = dev_get_uclass_plat(dev); + if (!plat_data) + return -EINVAL; + +#if CONFIG_IS_ENABLED(OF_CONTROL) + const int node = dev_of_offset(dev); + const char *mode_prefix = "mode-"; + const int mode_prefix_len = strlen(mode_prefix); + int property; + const u32 *propvalue; + const char *propname; + + plat_data->env_variable = fdt_getprop(gd->fdt_blob, + node, + "u-boot,env-variable", + NULL); + if (!plat_data->env_variable) + plat_data->env_variable = "reboot-mode"; + + plat_data->count = 0; + + fdt_for_each_property_offset(property, gd->fdt_blob, node) { + propvalue = fdt_getprop_by_offset(gd->fdt_blob, + property, &propname, NULL); + if (!propvalue) { + dev_err(dev, "Could not get the value for property %s\n", + propname); + return -EINVAL; + } + + if (!strncmp(propname, mode_prefix, mode_prefix_len)) + plat_data->count++; + } + + plat_data->modes = devm_kcalloc(dev, plat_data->count, + sizeof(struct reboot_mode_mode), 0); + + struct reboot_mode_mode *next = plat_data->modes; + + fdt_for_each_property_offset(property, gd->fdt_blob, node) { + propvalue = fdt_getprop_by_offset(gd->fdt_blob, + property, &propname, NULL); + if (!propvalue) { + dev_err(dev, "Could not get the value for property %s\n", + propname); + return -EINVAL; + } + + if (!strncmp(propname, mode_prefix, mode_prefix_len)) { + next->mode_name = &propname[mode_prefix_len]; + next->mode_id = fdt32_to_cpu(*propvalue); + + next++; + } + } +#else + if (!plat_data->env_variable) + plat_data->env_variable = "reboot-mode"; + +#endif + + return 0; +} + +UCLASS_DRIVER(reboot_mode) = { + .name = "reboot-mode", + .id = UCLASS_REBOOT_MODE, + .pre_probe = dm_reboot_mode_pre_probe, + .per_device_plat_auto = + sizeof(struct reboot_mode_uclass_platdata), +}; |