blob: 69c22a70008fe5486d06459b4d569c398df93335 [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
50 * (see the U_BOOT_DEVICE() declaration below) should not do anything.
51 * If we are that device, return an error.
52 */
Simon Glass03753c92017-05-18 20:09:59 -060053 if (state->fdt_fname && !dev_of_valid(dev))
Stephen Warren11636252016-05-12 12:03:35 -060054 return -ENODEV;
55
56 switch (type) {
57 case SYSRESET_COLD:
58 state->last_sysreset = type;
59 break;
Urja Rannikko857f39d2019-05-16 21:48:41 +000060 case SYSRESET_POWER_OFF:
Stephen Warren11636252016-05-12 12:03:35 -060061 state->last_sysreset = type;
62 if (!state->sysreset_allowed[type])
63 return -EACCES;
64 sandbox_exit();
65 break;
Urja Rannikko857f39d2019-05-16 21:48:41 +000066 case SYSRESET_POWER:
Simon Glass751fed42018-10-01 12:22:46 -060067 if (!state->sysreset_allowed[type])
68 return -EACCES;
Simon Glass90723262019-05-18 11:59:43 -060069 sandbox_exit();
Stephen Warren11636252016-05-12 12:03:35 -060070 default:
71 return -ENOSYS;
72 }
73 if (!state->sysreset_allowed[type])
74 return -EACCES;
75
76 return -EINPROGRESS;
77}
78
Mario Sixcda46882018-08-06 10:23:33 +020079int sandbox_sysreset_get_status(struct udevice *dev, char *buf, int size)
80{
81 strlcpy(buf, "Reset Status: COLD", size);
82
83 return 0;
84}
85
Simon Glass751fed42018-10-01 12:22:46 -060086int sandbox_sysreset_get_last(struct udevice *dev)
87{
Simon Glass2a072692018-11-23 21:29:28 -070088 struct sandbox_state *state = state_get_current();
89
90 /*
91 * The first phase is a power reset, after that we assume we don't
92 * know.
93 */
94 return state->jumped_fname ? SYSRESET_WARM : SYSRESET_POWER;
Simon Glass751fed42018-10-01 12:22:46 -060095}
96
Stephen Warren11636252016-05-12 12:03:35 -060097static struct sysreset_ops sandbox_sysreset_ops = {
98 .request = sandbox_sysreset_request,
Mario Sixcda46882018-08-06 10:23:33 +020099 .get_status = sandbox_sysreset_get_status,
Simon Glass751fed42018-10-01 12:22:46 -0600100 .get_last = sandbox_sysreset_get_last,
Stephen Warren11636252016-05-12 12:03:35 -0600101};
102
103static const struct udevice_id sandbox_sysreset_ids[] = {
104 { .compatible = "sandbox,reset" },
105 { }
106};
107
108U_BOOT_DRIVER(sysreset_sandbox) = {
109 .name = "sysreset_sandbox",
110 .id = UCLASS_SYSRESET,
111 .of_match = sandbox_sysreset_ids,
112 .ops = &sandbox_sysreset_ops,
113};
114
115static struct sysreset_ops sandbox_warm_sysreset_ops = {
116 .request = sandbox_warm_sysreset_request,
Mario Sixcda46882018-08-06 10:23:33 +0200117 .get_status = sandbox_warm_sysreset_get_status,
Simon Glass751fed42018-10-01 12:22:46 -0600118 .get_last = sandbox_warm_sysreset_get_last,
Stephen Warren11636252016-05-12 12:03:35 -0600119};
120
121static const struct udevice_id sandbox_warm_sysreset_ids[] = {
122 { .compatible = "sandbox,warm-reset" },
123 { }
124};
125
126U_BOOT_DRIVER(warm_sysreset_sandbox) = {
127 .name = "warm_sysreset_sandbox",
128 .id = UCLASS_SYSRESET,
129 .of_match = sandbox_warm_sysreset_ids,
130 .ops = &sandbox_warm_sysreset_ops,
131};
132
133/* This is here in case we don't have a device tree */
134U_BOOT_DEVICE(sysreset_sandbox_non_fdt) = {
135 .name = "sysreset_sandbox",
136};