blob: 93179f90bbb8828e00b5eb5699afd56642d44523 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren859f2562016-05-12 12:03:35 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Stephen Warren859f2562016-05-12 12:03:35 -06005 */
6
Stephen Warren859f2562016-05-12 12:03:35 -06007#include <dm.h>
8#include <errno.h>
9#include <sysreset.h>
10#include <asm/state.h>
11#include <asm/test.h>
12
Stephen Warren859f2562016-05-12 12:03:35 -060013static int sandbox_warm_sysreset_request(struct udevice *dev,
14 enum sysreset_t type)
15{
16 struct sandbox_state *state = state_get_current();
17
18 switch (type) {
19 case SYSRESET_WARM:
20 state->last_sysreset = type;
21 break;
22 default:
Paul Barkerbdb3a3f2023-11-08 08:51:10 +000023 return -EPROTONOSUPPORT;
Stephen Warren859f2562016-05-12 12:03:35 -060024 }
25 if (!state->sysreset_allowed[type])
26 return -EACCES;
27
28 return -EINPROGRESS;
29}
30
Mario Sixb6da1fb2018-08-06 10:23:33 +020031int sandbox_warm_sysreset_get_status(struct udevice *dev, char *buf, int size)
32{
33 strlcpy(buf, "Reset Status: WARM", size);
34
35 return 0;
36}
37
Simon Glass07fbba52018-10-01 12:22:46 -060038int sandbox_warm_sysreset_get_last(struct udevice *dev)
39{
40 return SYSRESET_WARM;
41}
42
Stephen Warren859f2562016-05-12 12:03:35 -060043static int sandbox_sysreset_request(struct udevice *dev, enum sysreset_t type)
44{
45 struct sandbox_state *state = state_get_current();
46
47 /*
48 * If we have a device tree, the device we created from platform data
Simon Glass1d8364a2020-12-28 20:34:54 -070049 * (see the U_BOOT_DRVINFO() declaration below) should not do anything.
Stephen Warren859f2562016-05-12 12:03:35 -060050 * If we are that device, return an error.
51 */
Simon Glassf1d50f72020-12-19 10:40:13 -070052 if (state->fdt_fname && !dev_has_ofnode(dev))
Stephen Warren859f2562016-05-12 12:03:35 -060053 return -ENODEV;
54
55 switch (type) {
56 case SYSRESET_COLD:
57 state->last_sysreset = type;
Heinrich Schuchardt1c678442020-10-27 20:29:25 +010058 if (!state->sysreset_allowed[type])
59 return -EACCES;
60 sandbox_reset();
Stephen Warren859f2562016-05-12 12:03:35 -060061 break;
Urja Rannikko25394e72019-05-16 21:48:41 +000062 case SYSRESET_POWER_OFF:
Stephen Warren859f2562016-05-12 12:03:35 -060063 state->last_sysreset = type;
64 if (!state->sysreset_allowed[type])
65 return -EACCES;
66 sandbox_exit();
Urja Rannikko25394e72019-05-16 21:48:41 +000067 case SYSRESET_POWER:
Simon Glass07fbba52018-10-01 12:22:46 -060068 if (!state->sysreset_allowed[type])
69 return -EACCES;
Simon Glass2fe5b912019-05-18 11:59:43 -060070 sandbox_exit();
Stephen Warren859f2562016-05-12 12:03:35 -060071 default:
Paul Barkerbdb3a3f2023-11-08 08:51:10 +000072 return -EPROTONOSUPPORT;
Stephen Warren859f2562016-05-12 12:03:35 -060073 }
74 if (!state->sysreset_allowed[type])
75 return -EACCES;
76
77 return -EINPROGRESS;
78}
79
Mario Sixb6da1fb2018-08-06 10:23:33 +020080int sandbox_sysreset_get_status(struct udevice *dev, char *buf, int size)
81{
82 strlcpy(buf, "Reset Status: COLD", size);
83
84 return 0;
85}
86
Simon Glass07fbba52018-10-01 12:22:46 -060087int sandbox_sysreset_get_last(struct udevice *dev)
88{
Simon Glass7e4d49c2018-11-23 21:29:28 -070089 struct sandbox_state *state = state_get_current();
90
91 /*
92 * The first phase is a power reset, after that we assume we don't
93 * know.
94 */
95 return state->jumped_fname ? SYSRESET_WARM : SYSRESET_POWER;
Simon Glass07fbba52018-10-01 12:22:46 -060096}
97
Stephen Warren859f2562016-05-12 12:03:35 -060098static struct sysreset_ops sandbox_sysreset_ops = {
99 .request = sandbox_sysreset_request,
Mario Sixb6da1fb2018-08-06 10:23:33 +0200100 .get_status = sandbox_sysreset_get_status,
Simon Glass07fbba52018-10-01 12:22:46 -0600101 .get_last = sandbox_sysreset_get_last,
Stephen Warren859f2562016-05-12 12:03:35 -0600102};
103
104static const struct udevice_id sandbox_sysreset_ids[] = {
105 { .compatible = "sandbox,reset" },
106 { }
107};
108
109U_BOOT_DRIVER(sysreset_sandbox) = {
110 .name = "sysreset_sandbox",
111 .id = UCLASS_SYSRESET,
112 .of_match = sandbox_sysreset_ids,
113 .ops = &sandbox_sysreset_ops,
114};
115
116static struct sysreset_ops sandbox_warm_sysreset_ops = {
117 .request = sandbox_warm_sysreset_request,
Mario Sixb6da1fb2018-08-06 10:23:33 +0200118 .get_status = sandbox_warm_sysreset_get_status,
Simon Glass07fbba52018-10-01 12:22:46 -0600119 .get_last = sandbox_warm_sysreset_get_last,
Stephen Warren859f2562016-05-12 12:03:35 -0600120};
121
122static const struct udevice_id sandbox_warm_sysreset_ids[] = {
123 { .compatible = "sandbox,warm-reset" },
124 { }
125};
126
127U_BOOT_DRIVER(warm_sysreset_sandbox) = {
128 .name = "warm_sysreset_sandbox",
129 .id = UCLASS_SYSRESET,
130 .of_match = sandbox_warm_sysreset_ids,
131 .ops = &sandbox_warm_sysreset_ops,
132};
133
Sean Anderson7bf2f8c2023-10-14 16:47:46 -0400134#if CONFIG_IS_ENABLED(OF_REAL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Stephen Warren859f2562016-05-12 12:03:35 -0600135/* This is here in case we don't have a device tree */
Simon Glass1d8364a2020-12-28 20:34:54 -0700136U_BOOT_DRVINFO(sysreset_sandbox_non_fdt) = {
Stephen Warren859f2562016-05-12 12:03:35 -0600137 .name = "sysreset_sandbox",
138};
Simon Glasscb90bd32020-10-03 11:31:23 -0600139#endif