1 /*
2  * Copyright (c) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <sysreset.h>
12 #include <asm/state.h>
13 #include <asm/test.h>
14 
15 static int sandbox_warm_sysreset_request(struct udevice *dev,
16 					 enum sysreset_t type)
17 {
18 	struct sandbox_state *state = state_get_current();
19 
20 	switch (type) {
21 	case SYSRESET_WARM:
22 		state->last_sysreset = type;
23 		break;
24 	default:
25 		return -ENOSYS;
26 	}
27 	if (!state->sysreset_allowed[type])
28 		return -EACCES;
29 
30 	return -EINPROGRESS;
31 }
32 
33 static int sandbox_sysreset_request(struct udevice *dev, enum sysreset_t type)
34 {
35 	struct sandbox_state *state = state_get_current();
36 
37 	/*
38 	 * If we have a device tree, the device we created from platform data
39 	 * (see the U_BOOT_DEVICE() declaration below) should not do anything.
40 	 * If we are that device, return an error.
41 	 */
42 	if (state->fdt_fname && !dev_of_valid(dev))
43 		return -ENODEV;
44 
45 	switch (type) {
46 	case SYSRESET_COLD:
47 		state->last_sysreset = type;
48 		break;
49 	case SYSRESET_POWER:
50 		state->last_sysreset = type;
51 		if (!state->sysreset_allowed[type])
52 			return -EACCES;
53 		sandbox_exit();
54 		break;
55 	default:
56 		return -ENOSYS;
57 	}
58 	if (!state->sysreset_allowed[type])
59 		return -EACCES;
60 
61 	return -EINPROGRESS;
62 }
63 
64 static struct sysreset_ops sandbox_sysreset_ops = {
65 	.request	= sandbox_sysreset_request,
66 };
67 
68 static const struct udevice_id sandbox_sysreset_ids[] = {
69 	{ .compatible = "sandbox,reset" },
70 	{ }
71 };
72 
73 U_BOOT_DRIVER(sysreset_sandbox) = {
74 	.name		= "sysreset_sandbox",
75 	.id		= UCLASS_SYSRESET,
76 	.of_match	= sandbox_sysreset_ids,
77 	.ops		= &sandbox_sysreset_ops,
78 };
79 
80 static struct sysreset_ops sandbox_warm_sysreset_ops = {
81 	.request	= sandbox_warm_sysreset_request,
82 };
83 
84 static const struct udevice_id sandbox_warm_sysreset_ids[] = {
85 	{ .compatible = "sandbox,warm-reset" },
86 	{ }
87 };
88 
89 U_BOOT_DRIVER(warm_sysreset_sandbox) = {
90 	.name		= "warm_sysreset_sandbox",
91 	.id		= UCLASS_SYSRESET,
92 	.of_match	= sandbox_warm_sysreset_ids,
93 	.ops		= &sandbox_warm_sysreset_ops,
94 };
95 
96 /* This is here in case we don't have a device tree */
97 U_BOOT_DEVICE(sysreset_sandbox_non_fdt) = {
98 	.name = "sysreset_sandbox",
99 };
100