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