blob: a9908ebf795622d1913a0feaaadc49fea7246749 [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>
Simon Glass401d1c42020-10-30 21:38:53 -060024#include <asm/global_data.h>
Stephen Warren11636252016-05-12 12:03:35 -060025
26int sysreset_request(struct udevice *dev, enum sysreset_t type)
27{
28 struct sysreset_ops *ops = sysreset_get_ops(dev);
29
30 if (!ops->request)
31 return -ENOSYS;
32
33 return ops->request(dev, type);
34}
35
Mario Six245f5cd2018-08-06 10:23:32 +020036int sysreset_get_status(struct udevice *dev, char *buf, int size)
37{
38 struct sysreset_ops *ops = sysreset_get_ops(dev);
39
40 if (!ops->get_status)
41 return -ENOSYS;
42
43 return ops->get_status(dev, buf, size);
44}
45
Simon Glass751fed42018-10-01 12:22:46 -060046int sysreset_get_last(struct udevice *dev)
47{
48 struct sysreset_ops *ops = sysreset_get_ops(dev);
49
50 if (!ops->get_last)
51 return -ENOSYS;
52
53 return ops->get_last(dev);
54}
55
Stephen Warren11636252016-05-12 12:03:35 -060056int sysreset_walk(enum sysreset_t type)
57{
58 struct udevice *dev;
59 int ret = -ENOSYS;
60
61 while (ret != -EINPROGRESS && type < SYSRESET_COUNT) {
62 for (uclass_first_device(UCLASS_SYSRESET, &dev);
63 dev;
64 uclass_next_device(&dev)) {
65 ret = sysreset_request(dev, type);
66 if (ret == -EINPROGRESS)
67 break;
68 }
69 type++;
70 }
71
72 return ret;
73}
74
Simon Glass751fed42018-10-01 12:22:46 -060075int sysreset_get_last_walk(void)
76{
77 struct udevice *dev;
78 int value = -ENOENT;
79
80 for (uclass_first_device(UCLASS_SYSRESET, &dev);
81 dev;
82 uclass_next_device(&dev)) {
83 int ret;
84
85 ret = sysreset_get_last(dev);
86 if (ret >= 0) {
87 value = ret;
88 break;
89 }
90 }
91
92 return value;
93}
94
Stephen Warren11636252016-05-12 12:03:35 -060095void sysreset_walk_halt(enum sysreset_t type)
96{
97 int ret;
98
99 ret = sysreset_walk(type);
100
101 /* Wait for the reset to take effect */
102 if (ret == -EINPROGRESS)
103 mdelay(100);
104
105 /* Still no reset? Give up */
Simon Glass4c66cb42020-12-23 08:11:14 -0700106 if (spl_phase() <= PHASE_SPL)
107 log_err("no sysreset\n");
108 else
109 log_err("System reset not supported on this platform\n");
Stephen Warren11636252016-05-12 12:03:35 -0600110 hang();
111}
112
113/**
114 * reset_cpu() - calls sysreset_walk(SYSRESET_WARM)
115 */
116void reset_cpu(ulong addr)
117{
118 sysreset_walk_halt(SYSRESET_WARM);
119}
120
121
Simon Glass09140112020-05-10 11:40:03 -0600122int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Stephen Warren11636252016-05-12 12:03:35 -0600123{
Bin Meng406be392018-07-19 03:07:31 -0700124 printf("resetting ...\n");
Heinrich Schuchardte20a6e42020-07-29 12:13:41 +0200125 mdelay(100);
Bin Meng406be392018-07-19 03:07:31 -0700126
Philipp Tomsichb53f69922017-11-24 18:37:58 +0100127 sysreset_walk_halt(SYSRESET_COLD);
Stephen Warren11636252016-05-12 12:03:35 -0600128
129 return 0;
130}
131
Urja Rannikkob8050512019-05-16 21:48:42 +0000132#if IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
Simon Glass09140112020-05-10 11:40:03 -0600133int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Urja Rannikkob8050512019-05-16 21:48:42 +0000134{
135 int ret;
136
137 puts("poweroff ...\n");
138 mdelay(100);
139
140 ret = sysreset_walk(SYSRESET_POWER_OFF);
141
142 if (ret == -EINPROGRESS)
143 mdelay(1000);
144
145 /*NOTREACHED when power off*/
146 return CMD_RET_FAILURE;
147}
148#endif
149
Michal Simek758de972018-07-12 10:36:07 +0200150static int sysreset_post_bind(struct udevice *dev)
151{
152#if defined(CONFIG_NEEDS_MANUAL_RELOC)
153 struct sysreset_ops *ops = sysreset_get_ops(dev);
154 static int reloc_done;
155
156 if (!reloc_done) {
157 if (ops->request)
158 ops->request += gd->reloc_off;
159 reloc_done++;
160 }
161#endif
162 return 0;
163}
164
Stephen Warren11636252016-05-12 12:03:35 -0600165UCLASS_DRIVER(sysreset) = {
166 .id = UCLASS_SYSRESET,
167 .name = "sysreset",
Michal Simek758de972018-07-12 10:36:07 +0200168 .post_bind = sysreset_post_bind,
Stephen Warren11636252016-05-12 12:03:35 -0600169};