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 #include <dm.h>
8 #include <errno.h>
9 #include <sysreset.h>
10 #include <asm/state.h>
11 #include <asm/test.h>
12 
sandbox_warm_sysreset_request(struct udevice * dev,enum sysreset_t type)13 static 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:
23 		return -EPROTONOSUPPORT;
24 	}
25 	if (!state->sysreset_allowed[type])
26 		return -EACCES;
27 
28 	return -EINPROGRESS;
29 }
30 
sandbox_warm_sysreset_get_status(struct udevice * dev,char * buf,int size)31 int 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 
sandbox_warm_sysreset_get_last(struct udevice * dev)38 int sandbox_warm_sysreset_get_last(struct udevice *dev)
39 {
40 	return SYSRESET_WARM;
41 }
42 
sandbox_sysreset_request(struct udevice * dev,enum sysreset_t type)43 static 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
49 	 * (see the U_BOOT_DRVINFO() declaration below) should not do anything.
50 	 * If we are that device, return an error.
51 	 */
52 	if (state->fdt_fname && !dev_has_ofnode(dev))
53 		return -ENODEV;
54 
55 	switch (type) {
56 	case SYSRESET_COLD:
57 		state->last_sysreset = type;
58 		if (!state->sysreset_allowed[type])
59 			return -EACCES;
60 		sandbox_reset();
61 		break;
62 	case SYSRESET_POWER_OFF:
63 		state->last_sysreset = type;
64 		if (!state->sysreset_allowed[type])
65 			return -EACCES;
66 		sandbox_exit();
67 	case SYSRESET_POWER:
68 		if (!state->sysreset_allowed[type])
69 			return -EACCES;
70 		sandbox_exit();
71 	default:
72 		return -EPROTONOSUPPORT;
73 	}
74 	if (!state->sysreset_allowed[type])
75 		return -EACCES;
76 
77 	return -EINPROGRESS;
78 }
79 
sandbox_sysreset_get_status(struct udevice * dev,char * buf,int size)80 int 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 
sandbox_sysreset_get_last(struct udevice * dev)87 int sandbox_sysreset_get_last(struct udevice *dev)
88 {
89 	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;
96 }
97 
98 static struct sysreset_ops sandbox_sysreset_ops = {
99 	.request	= sandbox_sysreset_request,
100 	.get_status	= sandbox_sysreset_get_status,
101 	.get_last	= sandbox_sysreset_get_last,
102 };
103 
104 static const struct udevice_id sandbox_sysreset_ids[] = {
105 	{ .compatible = "sandbox,reset" },
106 	{ }
107 };
108 
109 U_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 
116 static struct sysreset_ops sandbox_warm_sysreset_ops = {
117 	.request	= sandbox_warm_sysreset_request,
118 	.get_status	= sandbox_warm_sysreset_get_status,
119 	.get_last	= sandbox_warm_sysreset_get_last,
120 };
121 
122 static const struct udevice_id sandbox_warm_sysreset_ids[] = {
123 	{ .compatible = "sandbox,warm-reset" },
124 	{ }
125 };
126 
127 U_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 
134 #if CONFIG_IS_ENABLED(OF_REAL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
135 /* This is here in case we don't have a device tree */
136 U_BOOT_DRVINFO(sysreset_sandbox_non_fdt) = {
137 	.name = "sysreset_sandbox",
138 };
139 #endif
140