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 <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 
14 static 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 
32 static int sandbox_sysreset_request(struct udevice *dev, enum sysreset_t type)
33 {
34 	struct sandbox_state *state = state_get_current();
35 
36 	/*
37 	 * If we have a device tree, the device we created from platform data
38 	 * (see the U_BOOT_DEVICE() declaration below) should not do anything.
39 	 * If we are that device, return an error.
40 	 */
41 	if (state->fdt_fname && !dev_of_valid(dev))
42 		return -ENODEV;
43 
44 	switch (type) {
45 	case SYSRESET_COLD:
46 		state->last_sysreset = type;
47 		break;
48 	case SYSRESET_POWER:
49 		state->last_sysreset = type;
50 		if (!state->sysreset_allowed[type])
51 			return -EACCES;
52 		sandbox_exit();
53 		break;
54 	default:
55 		return -ENOSYS;
56 	}
57 	if (!state->sysreset_allowed[type])
58 		return -EACCES;
59 
60 	return -EINPROGRESS;
61 }
62 
63 static struct sysreset_ops sandbox_sysreset_ops = {
64 	.request	= sandbox_sysreset_request,
65 };
66 
67 static const struct udevice_id sandbox_sysreset_ids[] = {
68 	{ .compatible = "sandbox,reset" },
69 	{ }
70 };
71 
72 U_BOOT_DRIVER(sysreset_sandbox) = {
73 	.name		= "sysreset_sandbox",
74 	.id		= UCLASS_SYSRESET,
75 	.of_match	= sandbox_sysreset_ids,
76 	.ops		= &sandbox_sysreset_ops,
77 };
78 
79 static struct sysreset_ops sandbox_warm_sysreset_ops = {
80 	.request	= sandbox_warm_sysreset_request,
81 };
82 
83 static const struct udevice_id sandbox_warm_sysreset_ids[] = {
84 	{ .compatible = "sandbox,warm-reset" },
85 	{ }
86 };
87 
88 U_BOOT_DRIVER(warm_sysreset_sandbox) = {
89 	.name		= "warm_sysreset_sandbox",
90 	.id		= UCLASS_SYSRESET,
91 	.of_match	= sandbox_warm_sysreset_ids,
92 	.ops		= &sandbox_warm_sysreset_ops,
93 };
94 
95 /* This is here in case we don't have a device tree */
96 U_BOOT_DEVICE(sysreset_sandbox_non_fdt) = {
97 	.name = "sysreset_sandbox",
98 };
99