blob: d30b008bf9a8c859671f629167510caf7f73f17d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren11636252016-05-12 12:03:35 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Stephen Warren11636252016-05-12 12:03:35 -06005 */
6
Simon Glasseb517312018-10-01 12:22:45 -06007#define LOG_CATEGORY UCLASS_SYSRESET
8
Simon Glass09140112020-05-10 11:40:03 -06009#include <command.h>
Simon Glass9a3b4ce2019-12-28 10:45:01 -070010#include <cpu_func.h>
Stephen Warren11636252016-05-12 12:03:35 -060011#include <dm.h>
12#include <errno.h>
Simon Glass4c66cb42020-12-23 08:11:14 -070013#include <hang.h>
14#include <log.h>
Stephen Warren11636252016-05-12 12:03:35 -060015#include <regmap.h>
Simon Glass4c66cb42020-12-23 08:11:14 -070016#include <spl.h>
17#include <sysreset.h>
Stephen Warren11636252016-05-12 12:03:35 -060018#include <dm/device-internal.h>
19#include <dm/lists.h>
20#include <dm/root.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
Stephen Warren11636252016-05-12 12:03:35 -060022#include <linux/err.h>
Simon Glass401d1c42020-10-30 21:38:53 -060023#include <asm/global_data.h>
Stephen Warren11636252016-05-12 12:03:35 -060024
25int sysreset_request(struct udevice *dev, enum sysreset_t type)
26{
27 struct sysreset_ops *ops = sysreset_get_ops(dev);
28
29 if (!ops->request)
30 return -ENOSYS;
31
32 return ops->request(dev, type);
33}
34
Mario Six245f5cd2018-08-06 10:23:32 +020035int sysreset_get_status(struct udevice *dev, char *buf, int size)
36{
37 struct sysreset_ops *ops = sysreset_get_ops(dev);
38
39 if (!ops->get_status)
40 return -ENOSYS;
41
42 return ops->get_status(dev, buf, size);
43}
44
Simon Glass751fed42018-10-01 12:22:46 -060045int sysreset_get_last(struct udevice *dev)
46{
47 struct sysreset_ops *ops = sysreset_get_ops(dev);
48
49 if (!ops->get_last)
50 return -ENOSYS;
51
52 return ops->get_last(dev);
53}
54
Stephen Warren11636252016-05-12 12:03:35 -060055int sysreset_walk(enum sysreset_t type)
56{
57 struct udevice *dev;
58 int ret = -ENOSYS;
59
60 while (ret != -EINPROGRESS && type < SYSRESET_COUNT) {
61 for (uclass_first_device(UCLASS_SYSRESET, &dev);
62 dev;
63 uclass_next_device(&dev)) {
64 ret = sysreset_request(dev, type);
65 if (ret == -EINPROGRESS)
66 break;
67 }
68 type++;
69 }
70
71 return ret;
72}
73
Simon Glass751fed42018-10-01 12:22:46 -060074int sysreset_get_last_walk(void)
75{
76 struct udevice *dev;
77 int value = -ENOENT;
78
79 for (uclass_first_device(UCLASS_SYSRESET, &dev);
80 dev;
81 uclass_next_device(&dev)) {
82 int ret;
83
84 ret = sysreset_get_last(dev);
85 if (ret >= 0) {
86 value = ret;
87 break;
88 }
89 }
90
91 return value;
92}
93
Stephen Warren11636252016-05-12 12:03:35 -060094void sysreset_walk_halt(enum sysreset_t type)
95{
96 int ret;
97
98 ret = sysreset_walk(type);
99
100 /* Wait for the reset to take effect */
101 if (ret == -EINPROGRESS)
102 mdelay(100);
103
104 /* Still no reset? Give up */
Simon Glass4c66cb42020-12-23 08:11:14 -0700105 if (spl_phase() <= PHASE_SPL)
106 log_err("no sysreset\n");
107 else
108 log_err("System reset not supported on this platform\n");
Stephen Warren11636252016-05-12 12:03:35 -0600109 hang();
110}
111
112/**
113 * reset_cpu() - calls sysreset_walk(SYSRESET_WARM)
114 */
Harald Seiler35b65dd2020-12-15 16:47:52 +0100115void reset_cpu(void)
Stephen Warren11636252016-05-12 12:03:35 -0600116{
117 sysreset_walk_halt(SYSRESET_WARM);
118}
119
Bin Meng5f1a08b2021-02-25 17:22:52 +0800120#if IS_ENABLED(CONFIG_SYSRESET_CMD_RESET)
Simon Glass09140112020-05-10 11:40:03 -0600121int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Stephen Warren11636252016-05-12 12:03:35 -0600122{
Igor Opaniuka6713b32021-04-01 02:01:55 +0300123 enum sysreset_t reset_type = SYSRESET_COLD;
124
125 if (argc > 2)
126 return CMD_RET_USAGE;
127
128 if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'w') {
129 reset_type = SYSRESET_WARM;
130 }
131
Bin Meng406be392018-07-19 03:07:31 -0700132 printf("resetting ...\n");
Heinrich Schuchardte20a6e42020-07-29 12:13:41 +0200133 mdelay(100);
Bin Meng406be392018-07-19 03:07:31 -0700134
Igor Opaniuka6713b32021-04-01 02:01:55 +0300135 sysreset_walk_halt(reset_type);
Stephen Warren11636252016-05-12 12:03:35 -0600136
137 return 0;
138}
Bin Meng5f1a08b2021-02-25 17:22:52 +0800139#endif
Stephen Warren11636252016-05-12 12:03:35 -0600140
Urja Rannikkob8050512019-05-16 21:48:42 +0000141#if IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
Simon Glass09140112020-05-10 11:40:03 -0600142int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Urja Rannikkob8050512019-05-16 21:48:42 +0000143{
144 int ret;
145
146 puts("poweroff ...\n");
147 mdelay(100);
148
149 ret = sysreset_walk(SYSRESET_POWER_OFF);
150
151 if (ret == -EINPROGRESS)
152 mdelay(1000);
153
154 /*NOTREACHED when power off*/
155 return CMD_RET_FAILURE;
156}
157#endif
158
Stephen Warren11636252016-05-12 12:03:35 -0600159UCLASS_DRIVER(sysreset) = {
160 .id = UCLASS_SYSRESET,
161 .name = "sysreset",
Stephen Warren11636252016-05-12 12:03:35 -0600162};