1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7 #define LOG_CATEGORY UCLASS_SYSRESET
8
9 #include <common.h>
10 #include <command.h>
11 #include <cpu_func.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <hang.h>
15 #include <log.h>
16 #include <regmap.h>
17 #include <spl.h>
18 #include <sysreset.h>
19 #include <dm/device-internal.h>
20 #include <dm/lists.h>
21 #include <dm/root.h>
22 #include <linux/delay.h>
23 #include <linux/err.h>
24 #include <asm/global_data.h>
25
sysreset_request(struct udevice * dev,enum sysreset_t type)26 int 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
sysreset_get_status(struct udevice * dev,char * buf,int size)36 int 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
sysreset_get_last(struct udevice * dev)46 int 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
sysreset_walk(enum sysreset_t type)56 int 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
sysreset_get_last_walk(void)75 int 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
sysreset_walk_halt(enum sysreset_t type)95 void 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 */
106 if (spl_phase() <= PHASE_SPL)
107 log_err("no sysreset\n");
108 else
109 log_err("System reset not supported on this platform\n");
110 hang();
111 }
112
113 /**
114 * reset_cpu() - calls sysreset_walk(SYSRESET_WARM)
115 */
reset_cpu(ulong addr)116 void reset_cpu(ulong addr)
117 {
118 sysreset_walk_halt(SYSRESET_WARM);
119 }
120
121
122 #if IS_ENABLED(CONFIG_SYSRESET_CMD_RESET)
do_reset(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])123 int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
124 {
125 printf("resetting ...\n");
126 mdelay(100);
127
128 sysreset_walk_halt(SYSRESET_COLD);
129
130 return 0;
131 }
132 #endif
133
134 #if IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
do_poweroff(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])135 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
136 {
137 int ret;
138
139 puts("poweroff ...\n");
140 mdelay(100);
141
142 ret = sysreset_walk(SYSRESET_POWER_OFF);
143
144 if (ret == -EINPROGRESS)
145 mdelay(1000);
146
147 /*NOTREACHED when power off*/
148 return CMD_RET_FAILURE;
149 }
150 #endif
151
sysreset_post_bind(struct udevice * dev)152 static int sysreset_post_bind(struct udevice *dev)
153 {
154 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
155 struct sysreset_ops *ops = sysreset_get_ops(dev);
156 static int reloc_done;
157
158 if (!reloc_done) {
159 if (ops->request)
160 ops->request += gd->reloc_off;
161 reloc_done++;
162 }
163 #endif
164 return 0;
165 }
166
167 UCLASS_DRIVER(sysreset) = {
168 .id = UCLASS_SYSRESET,
169 .name = "sysreset",
170 .post_bind = sysreset_post_bind,
171 };
172