d43c7b70034a38bf67603e9f9375d7d19753be5c
[oweals/u-boot.git] / drivers / sysreset / sysreset-uclass.c
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 <hang.h>
13 #include <log.h>
14 #include <sysreset.h>
15 #include <dm.h>
16 #include <errno.h>
17 #include <regmap.h>
18 #include <dm/device-internal.h>
19 #include <dm/lists.h>
20 #include <dm/root.h>
21 #include <linux/err.h>
22
23 int sysreset_request(struct udevice *dev, enum sysreset_t type)
24 {
25         struct sysreset_ops *ops = sysreset_get_ops(dev);
26
27         if (!ops->request)
28                 return -ENOSYS;
29
30         return ops->request(dev, type);
31 }
32
33 int sysreset_get_status(struct udevice *dev, char *buf, int size)
34 {
35         struct sysreset_ops *ops = sysreset_get_ops(dev);
36
37         if (!ops->get_status)
38                 return -ENOSYS;
39
40         return ops->get_status(dev, buf, size);
41 }
42
43 int sysreset_get_last(struct udevice *dev)
44 {
45         struct sysreset_ops *ops = sysreset_get_ops(dev);
46
47         if (!ops->get_last)
48                 return -ENOSYS;
49
50         return ops->get_last(dev);
51 }
52
53 int sysreset_walk(enum sysreset_t type)
54 {
55         struct udevice *dev;
56         int ret = -ENOSYS;
57
58         while (ret != -EINPROGRESS && type < SYSRESET_COUNT) {
59                 for (uclass_first_device(UCLASS_SYSRESET, &dev);
60                      dev;
61                      uclass_next_device(&dev)) {
62                         ret = sysreset_request(dev, type);
63                         if (ret == -EINPROGRESS)
64                                 break;
65                 }
66                 type++;
67         }
68
69         return ret;
70 }
71
72 int sysreset_get_last_walk(void)
73 {
74         struct udevice *dev;
75         int value = -ENOENT;
76
77         for (uclass_first_device(UCLASS_SYSRESET, &dev);
78              dev;
79              uclass_next_device(&dev)) {
80                 int ret;
81
82                 ret = sysreset_get_last(dev);
83                 if (ret >= 0) {
84                         value = ret;
85                         break;
86                 }
87         }
88
89         return value;
90 }
91
92 void sysreset_walk_halt(enum sysreset_t type)
93 {
94         int ret;
95
96         ret = sysreset_walk(type);
97
98         /* Wait for the reset to take effect */
99         if (ret == -EINPROGRESS)
100                 mdelay(100);
101
102         /* Still no reset? Give up */
103         log_err("System reset not supported on this platform\n");
104         hang();
105 }
106
107 /**
108  * reset_cpu() - calls sysreset_walk(SYSRESET_WARM)
109  */
110 void reset_cpu(ulong addr)
111 {
112         sysreset_walk_halt(SYSRESET_WARM);
113 }
114
115
116 int do_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
117 {
118         printf("resetting ...\n");
119
120         sysreset_walk_halt(SYSRESET_COLD);
121
122         return 0;
123 }
124
125 #if IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
126 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
127 {
128         int ret;
129
130         puts("poweroff ...\n");
131         mdelay(100);
132
133         ret = sysreset_walk(SYSRESET_POWER_OFF);
134
135         if (ret == -EINPROGRESS)
136                 mdelay(1000);
137
138         /*NOTREACHED when power off*/
139         return CMD_RET_FAILURE;
140 }
141 #endif
142
143 static int sysreset_post_bind(struct udevice *dev)
144 {
145 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
146         struct sysreset_ops *ops = sysreset_get_ops(dev);
147         static int reloc_done;
148
149         if (!reloc_done) {
150                 if (ops->request)
151                         ops->request += gd->reloc_off;
152                 reloc_done++;
153         }
154 #endif
155         return 0;
156 }
157
158 UCLASS_DRIVER(sysreset) = {
159         .id             = UCLASS_SYSRESET,
160         .name           = "sysreset",
161         .post_bind      = sysreset_post_bind,
162 };