1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
7 #define LOG_CATEGORY UCLASS_SYSRESET
19 #include <dm/device-internal.h>
22 #include <linux/delay.h>
23 #include <linux/err.h>
24 #include <asm/global_data.h>
26 int sysreset_request(struct udevice *dev, enum sysreset_t type)
28 struct sysreset_ops *ops = sysreset_get_ops(dev);
33 return ops->request(dev, type);
36 int sysreset_get_status(struct udevice *dev, char *buf, int size)
38 struct sysreset_ops *ops = sysreset_get_ops(dev);
43 return ops->get_status(dev, buf, size);
46 int sysreset_get_last(struct udevice *dev)
48 struct sysreset_ops *ops = sysreset_get_ops(dev);
53 return ops->get_last(dev);
56 int sysreset_walk(enum sysreset_t type)
61 while (ret != -EINPROGRESS && type < SYSRESET_COUNT) {
62 for (uclass_first_device(UCLASS_SYSRESET, &dev);
64 uclass_next_device(&dev)) {
65 ret = sysreset_request(dev, type);
66 if (ret == -EINPROGRESS)
75 int sysreset_get_last_walk(void)
80 for (uclass_first_device(UCLASS_SYSRESET, &dev);
82 uclass_next_device(&dev)) {
85 ret = sysreset_get_last(dev);
95 void sysreset_walk_halt(enum sysreset_t type)
99 ret = sysreset_walk(type);
101 /* Wait for the reset to take effect */
102 if (ret == -EINPROGRESS)
105 /* Still no reset? Give up */
106 if (spl_phase() <= PHASE_SPL)
107 log_err("no sysreset\n");
109 log_err("System reset not supported on this platform\n");
114 * reset_cpu() - calls sysreset_walk(SYSRESET_WARM)
118 sysreset_walk_halt(SYSRESET_WARM);
122 #if IS_ENABLED(CONFIG_SYSRESET_CMD_RESET)
123 int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
125 enum sysreset_t reset_type = SYSRESET_COLD;
128 return CMD_RET_USAGE;
130 if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'w') {
131 reset_type = SYSRESET_WARM;
134 printf("resetting ...\n");
137 sysreset_walk_halt(reset_type);
143 #if IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
144 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
148 puts("poweroff ...\n");
151 ret = sysreset_walk(SYSRESET_POWER_OFF);
153 if (ret == -EINPROGRESS)
156 /*NOTREACHED when power off*/
157 return CMD_RET_FAILURE;
161 static int sysreset_post_bind(struct udevice *dev)
163 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
164 struct sysreset_ops *ops = sysreset_get_ops(dev);
165 static int reloc_done;
169 ops->request += gd->reloc_off;
176 UCLASS_DRIVER(sysreset) = {
177 .id = UCLASS_SYSRESET,
179 .post_bind = sysreset_post_bind,