blob: 0ee286cbb34f1f44f6b2565227e80ce4d85164c5 [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
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <sysreset.h>
11#include <asm/state.h>
12#include <asm/test.h>
13
Stephen Warren11636252016-05-12 12:03:35 -060014static int sandbox_warm_sysreset_request(struct udevice *dev,
15 enum sysreset_t type)
16{
17 struct sandbox_state *state = state_get_current();
18
19 switch (type) {
20 case SYSRESET_WARM:
21 state->last_sysreset = type;
22 break;
23 default:
24 return -ENOSYS;
25 }
26 if (!state->sysreset_allowed[type])
27 return -EACCES;
28
29 return -EINPROGRESS;
30}
31
Mario Sixcda46882018-08-06 10:23:33 +020032int sandbox_warm_sysreset_get_status(struct udevice *dev, char *buf, int size)
33{
34 strlcpy(buf, "Reset Status: WARM", size);
35
36 return 0;
37}
38
Simon Glass751fed42018-10-01 12:22:46 -060039int sandbox_warm_sysreset_get_last(struct udevice *dev)
40{
41 return SYSRESET_WARM;
42}
43
Stephen Warren11636252016-05-12 12:03:35 -060044static int sandbox_sysreset_request(struct udevice *dev, enum sysreset_t type)
45{
46 struct sandbox_state *state = state_get_current();
47
48 /*
49 * If we have a device tree, the device we created from platform data
Simon Glass20e442a2020-12-28 20:34:54 -070050 * (see the U_BOOT_DRVINFO() declaration below) should not do anything.
Stephen Warren11636252016-05-12 12:03:35 -060051 * If we are that device, return an error.
52 */
Simon Glass7d14ee42020-12-19 10:40:13 -070053 if (state->fdt_fname && !dev_has_ofnode(dev))
Stephen Warren11636252016-05-12 12:03:35 -060054 return -ENODEV;
55
56 switch (type) {
57 case SYSRESET_COLD:
58 state->last_sysreset = type;
Heinrich Schuchardt329dccc2020-10-27 20:29:25 +010059 if (!state->sysreset_allowed[type])
60 return -EACCES;
61 sandbox_reset();
Stephen Warren11636252016-05-12 12:03:35 -060062 break;
Urja Rannikko857f39d2019-05-16 21:48:41 +000063 case SYSRESET_POWER_OFF:
Stephen Warren11636252016-05-12 12:03:35 -060064 state->last_sysreset = type;
65 if (!state->sysreset_allowed[type])
66 return -EACCES;
67 sandbox_exit();
68 break;
Urja Rannikko857f39d2019-05-16 21:48:41 +000069 case SYSRESET_POWER:
Simon Glass751fed42018-10-01 12:22:46 -060070 if (!state->sysreset_allowed[type])
71 return -EACCES;
Simon Glass90723262019-05-18 11:59:43 -060072 sandbox_exit();
Stephen Warren11636252016-05-12 12:03:35 -060073 default:
74 return -ENOSYS;
75 }
76 if (!state->sysreset_allowed[type])
77 return -EACCES;
78
79 return -EINPROGRESS;
80}
81
Mario Sixcda46882018-08-06 10:23:33 +020082int sandbox_sysreset_get_status(struct udevice *dev, char *buf, int size)
83{
84 strlcpy(buf, "Reset Status: COLD", size);
85
86 return 0;
87}
88
Simon Glass751fed42018-10-01 12:22:46 -060089int sandbox_sysreset_get_last(struct udevice *dev)
90{
Simon Glass2a072692018-11-23 21:29:28 -070091 struct sandbox_state *state = state_get_current();
92
93 /*
94 * The first phase is a power reset, after that we assume we don't
95 * know.
96 */
97 return state->jumped_fname ? SYSRESET_WARM : SYSRESET_POWER;
Simon Glass751fed42018-10-01 12:22:46 -060098}
99
Stephen Warren11636252016-05-12 12:03:35 -0600100static struct sysreset_ops sandbox_sysreset_ops = {
101 .request = sandbox_sysreset_request,
Mario Sixcda46882018-08-06 10:23:33 +0200102 .get_status = sandbox_sysreset_get_status,
Simon Glass751fed42018-10-01 12:22:46 -0600103 .get_last = sandbox_sysreset_get_last,
Stephen Warren11636252016-05-12 12:03:35 -0600104};
105
106static const struct udevice_id sandbox_sysreset_ids[] = {
107 { .compatible = "sandbox,reset" },
108 { }
109};
110
111U_BOOT_DRIVER(sysreset_sandbox) = {
112 .name = "sysreset_sandbox",
113 .id = UCLASS_SYSRESET,
114 .of_match = sandbox_sysreset_ids,
115 .ops = &sandbox_sysreset_ops,
116};
117
118static struct sysreset_ops sandbox_warm_sysreset_ops = {
119 .request = sandbox_warm_sysreset_request,
Mario Sixcda46882018-08-06 10:23:33 +0200120 .get_status = sandbox_warm_sysreset_get_status,
Simon Glass751fed42018-10-01 12:22:46 -0600121 .get_last = sandbox_warm_sysreset_get_last,
Stephen Warren11636252016-05-12 12:03:35 -0600122};
123
124static const struct udevice_id sandbox_warm_sysreset_ids[] = {
125 { .compatible = "sandbox,warm-reset" },
126 { }
127};
128
129U_BOOT_DRIVER(warm_sysreset_sandbox) = {
130 .name = "warm_sysreset_sandbox",
131 .id = UCLASS_SYSRESET,
132 .of_match = sandbox_warm_sysreset_ids,
133 .ops = &sandbox_warm_sysreset_ops,
134};
135
Simon Glass95397382021-08-07 07:24:04 -0600136#if CONFIG_IS_ENABLED(OF_REAL)
Stephen Warren11636252016-05-12 12:03:35 -0600137/* This is here in case we don't have a device tree */
Simon Glass20e442a2020-12-28 20:34:54 -0700138U_BOOT_DRVINFO(sysreset_sandbox_non_fdt) = {
Stephen Warren11636252016-05-12 12:03:35 -0600139 .name = "sysreset_sandbox",
140};
Simon Glassc1473292020-10-03 11:31:23 -0600141#endif