blob: e69fb2255b6ac30e69b52d6d16366ddefa249183 [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
Stephen Warren11636252016-05-12 12:03:35 -06009#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060010#include <command.h>
Simon Glass9a3b4ce2019-12-28 10:45:01 -070011#include <cpu_func.h>
Stephen Warren11636252016-05-12 12:03:35 -060012#include <dm.h>
13#include <errno.h>
Simon Glass4c66cb42020-12-23 08:11:14 -070014#include <hang.h>
15#include <log.h>
Stephen Warren11636252016-05-12 12:03:35 -060016#include <regmap.h>
Simon Glass4c66cb42020-12-23 08:11:14 -070017#include <spl.h>
18#include <sysreset.h>
Stephen Warren11636252016-05-12 12:03:35 -060019#include <dm/device-internal.h>
20#include <dm/lists.h>
21#include <dm/root.h>
Simon Glassc05ed002020-05-10 11:40:11 -060022#include <linux/delay.h>
Stephen Warren11636252016-05-12 12:03:35 -060023#include <linux/err.h>
24
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 */
115void reset_cpu(ulong addr)
116{
117 sysreset_walk_halt(SYSRESET_WARM);
118}
119
120
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{
Bin Meng406be392018-07-19 03:07:31 -0700123 printf("resetting ...\n");
Heinrich Schuchardte20a6e42020-07-29 12:13:41 +0200124 mdelay(100);
Bin Meng406be392018-07-19 03:07:31 -0700125
Philipp Tomsichb53f69922017-11-24 18:37:58 +0100126 sysreset_walk_halt(SYSRESET_COLD);
Stephen Warren11636252016-05-12 12:03:35 -0600127
128 return 0;
129}
130
Urja Rannikkob8050512019-05-16 21:48:42 +0000131#if IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
Simon Glass09140112020-05-10 11:40:03 -0600132int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Urja Rannikkob8050512019-05-16 21:48:42 +0000133{
134 int ret;
135
136 puts("poweroff ...\n");
137 mdelay(100);
138
139 ret = sysreset_walk(SYSRESET_POWER_OFF);
140
141 if (ret == -EINPROGRESS)
142 mdelay(1000);
143
144 /*NOTREACHED when power off*/
145 return CMD_RET_FAILURE;
146}
147#endif
148
Michal Simek758de972018-07-12 10:36:07 +0200149static int sysreset_post_bind(struct udevice *dev)
150{
151#if defined(CONFIG_NEEDS_MANUAL_RELOC)
152 struct sysreset_ops *ops = sysreset_get_ops(dev);
153 static int reloc_done;
154
155 if (!reloc_done) {
156 if (ops->request)
157 ops->request += gd->reloc_off;
158 reloc_done++;
159 }
160#endif
161 return 0;
162}
163
Stephen Warren11636252016-05-12 12:03:35 -0600164UCLASS_DRIVER(sysreset) = {
165 .id = UCLASS_SYSRESET,
166 .name = "sysreset",
Michal Simek758de972018-07-12 10:36:07 +0200167 .post_bind = sysreset_post_bind,
Stephen Warren11636252016-05-12 12:03:35 -0600168};