1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016, NVIDIA CORPORATION.
4  */
5 
6 #include <dm.h>
7 #include <log.h>
8 #include <malloc.h>
9 #include <reset-uclass.h>
10 #include <asm/io.h>
11 #include <asm/reset.h>
12 
13 #define SANDBOX_RESET_SIGNALS 101
14 
15 struct sandbox_reset_signal {
16 	bool asserted;
17 	bool requested;
18 };
19 
20 struct sandbox_reset {
21 	struct sandbox_reset_signal signals[SANDBOX_RESET_SIGNALS];
22 };
23 
sandbox_reset_request(struct reset_ctl * reset_ctl)24 static int sandbox_reset_request(struct reset_ctl *reset_ctl)
25 {
26 	struct sandbox_reset *sbr = dev_get_priv(reset_ctl->dev);
27 
28 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
29 
30 	if (reset_ctl->id >= SANDBOX_RESET_SIGNALS)
31 		return -EINVAL;
32 
33 	sbr->signals[reset_ctl->id].requested = true;
34 	return 0;
35 }
36 
sandbox_reset_free(struct reset_ctl * reset_ctl)37 static int sandbox_reset_free(struct reset_ctl *reset_ctl)
38 {
39 	struct sandbox_reset *sbr = dev_get_priv(reset_ctl->dev);
40 
41 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
42 
43 	sbr->signals[reset_ctl->id].requested = false;
44 	return 0;
45 }
46 
sandbox_reset_assert(struct reset_ctl * reset_ctl)47 static int sandbox_reset_assert(struct reset_ctl *reset_ctl)
48 {
49 	struct sandbox_reset *sbr = dev_get_priv(reset_ctl->dev);
50 
51 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
52 
53 	sbr->signals[reset_ctl->id].asserted = true;
54 
55 	return 0;
56 }
57 
sandbox_reset_deassert(struct reset_ctl * reset_ctl)58 static int sandbox_reset_deassert(struct reset_ctl *reset_ctl)
59 {
60 	struct sandbox_reset *sbr = dev_get_priv(reset_ctl->dev);
61 
62 	debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
63 
64 	sbr->signals[reset_ctl->id].asserted = false;
65 
66 	return 0;
67 }
68 
sandbox_reset_bind(struct udevice * dev)69 static int sandbox_reset_bind(struct udevice *dev)
70 {
71 	debug("%s(dev=%p)\n", __func__, dev);
72 
73 	return 0;
74 }
75 
sandbox_reset_probe(struct udevice * dev)76 static int sandbox_reset_probe(struct udevice *dev)
77 {
78 	debug("%s(dev=%p)\n", __func__, dev);
79 
80 	return 0;
81 }
82 
83 static const struct udevice_id sandbox_reset_ids[] = {
84 	{ .compatible = "sandbox,reset-ctl" },
85 	{ }
86 };
87 
88 struct reset_ops sandbox_reset_reset_ops = {
89 	.request = sandbox_reset_request,
90 	.rfree = sandbox_reset_free,
91 	.rst_assert = sandbox_reset_assert,
92 	.rst_deassert = sandbox_reset_deassert,
93 };
94 
95 U_BOOT_DRIVER(sandbox_reset) = {
96 	.name = "sandbox_reset",
97 	.id = UCLASS_RESET,
98 	.of_match = sandbox_reset_ids,
99 	.bind = sandbox_reset_bind,
100 	.probe = sandbox_reset_probe,
101 	.priv_auto	= sizeof(struct sandbox_reset),
102 	.ops = &sandbox_reset_reset_ops,
103 };
104 
sandbox_reset_query(struct udevice * dev,unsigned long id)105 int sandbox_reset_query(struct udevice *dev, unsigned long id)
106 {
107 	struct sandbox_reset *sbr = dev_get_priv(dev);
108 
109 	debug("%s(dev=%p, id=%ld)\n", __func__, dev, id);
110 
111 	if (id >= SANDBOX_RESET_SIGNALS)
112 		return -EINVAL;
113 
114 	return sbr->signals[id].asserted;
115 }
116 
sandbox_reset_is_requested(struct udevice * dev,unsigned long id)117 int sandbox_reset_is_requested(struct udevice *dev, unsigned long id)
118 {
119 	struct sandbox_reset *sbr = dev_get_priv(dev);
120 
121 	debug("%s(dev=%p, id=%ld)\n", __func__, dev, id);
122 
123 	if (id >= SANDBOX_RESET_SIGNALS)
124 		return -EINVAL;
125 
126 	return sbr->signals[id].requested;
127 }
128