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