1*0753bc2dSmaxims@google.com /*
2*0753bc2dSmaxims@google.com  * Copyright 2017 Google, Inc
3*0753bc2dSmaxims@google.com  *
4*0753bc2dSmaxims@google.com  * SPDX-License-Identifier:	GPL-2.0+
5*0753bc2dSmaxims@google.com  */
6*0753bc2dSmaxims@google.com 
7*0753bc2dSmaxims@google.com #include <common.h>
8*0753bc2dSmaxims@google.com #include <dm.h>
9*0753bc2dSmaxims@google.com #include <asm/state.h>
10*0753bc2dSmaxims@google.com #include <wdt.h>
11*0753bc2dSmaxims@google.com 
12*0753bc2dSmaxims@google.com DECLARE_GLOBAL_DATA_PTR;
13*0753bc2dSmaxims@google.com 
14*0753bc2dSmaxims@google.com static int sandbox_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
15*0753bc2dSmaxims@google.com {
16*0753bc2dSmaxims@google.com 	struct sandbox_state *state = state_get_current();
17*0753bc2dSmaxims@google.com 
18*0753bc2dSmaxims@google.com 	state->wdt.counter = timeout;
19*0753bc2dSmaxims@google.com 	state->wdt.running = true;
20*0753bc2dSmaxims@google.com 
21*0753bc2dSmaxims@google.com 	return 0;
22*0753bc2dSmaxims@google.com }
23*0753bc2dSmaxims@google.com 
24*0753bc2dSmaxims@google.com static int sandbox_wdt_stop(struct udevice *dev)
25*0753bc2dSmaxims@google.com {
26*0753bc2dSmaxims@google.com 	struct sandbox_state *state = state_get_current();
27*0753bc2dSmaxims@google.com 
28*0753bc2dSmaxims@google.com 	state->wdt.running = false;
29*0753bc2dSmaxims@google.com 
30*0753bc2dSmaxims@google.com 	return 0;
31*0753bc2dSmaxims@google.com }
32*0753bc2dSmaxims@google.com 
33*0753bc2dSmaxims@google.com static int sandbox_wdt_reset(struct udevice *dev)
34*0753bc2dSmaxims@google.com {
35*0753bc2dSmaxims@google.com 	struct sandbox_state *state = state_get_current();
36*0753bc2dSmaxims@google.com 
37*0753bc2dSmaxims@google.com 	state->wdt.reset_count++;
38*0753bc2dSmaxims@google.com 
39*0753bc2dSmaxims@google.com 	return 0;
40*0753bc2dSmaxims@google.com }
41*0753bc2dSmaxims@google.com 
42*0753bc2dSmaxims@google.com static int sandbox_wdt_expire_now(struct udevice *dev, ulong flags)
43*0753bc2dSmaxims@google.com {
44*0753bc2dSmaxims@google.com 	sandbox_wdt_start(dev, 1, flags);
45*0753bc2dSmaxims@google.com 
46*0753bc2dSmaxims@google.com 	return 0;
47*0753bc2dSmaxims@google.com }
48*0753bc2dSmaxims@google.com 
49*0753bc2dSmaxims@google.com static int sandbox_wdt_probe(struct udevice *dev)
50*0753bc2dSmaxims@google.com {
51*0753bc2dSmaxims@google.com 	struct sandbox_state *state = state_get_current();
52*0753bc2dSmaxims@google.com 
53*0753bc2dSmaxims@google.com 	memset(&state->wdt, 0, sizeof(state->wdt));
54*0753bc2dSmaxims@google.com 
55*0753bc2dSmaxims@google.com 	return 0;
56*0753bc2dSmaxims@google.com }
57*0753bc2dSmaxims@google.com 
58*0753bc2dSmaxims@google.com static const struct wdt_ops sandbox_wdt_ops = {
59*0753bc2dSmaxims@google.com 	.start = sandbox_wdt_start,
60*0753bc2dSmaxims@google.com 	.reset = sandbox_wdt_reset,
61*0753bc2dSmaxims@google.com 	.stop = sandbox_wdt_stop,
62*0753bc2dSmaxims@google.com 	.expire_now = sandbox_wdt_expire_now,
63*0753bc2dSmaxims@google.com };
64*0753bc2dSmaxims@google.com 
65*0753bc2dSmaxims@google.com static const struct udevice_id sandbox_wdt_ids[] = {
66*0753bc2dSmaxims@google.com 	{ .compatible = "sandbox,wdt" },
67*0753bc2dSmaxims@google.com 	{}
68*0753bc2dSmaxims@google.com };
69*0753bc2dSmaxims@google.com 
70*0753bc2dSmaxims@google.com U_BOOT_DRIVER(wdt_sandbox) = {
71*0753bc2dSmaxims@google.com 	.name = "wdt_sandbox",
72*0753bc2dSmaxims@google.com 	.id = UCLASS_WDT,
73*0753bc2dSmaxims@google.com 	.of_match = sandbox_wdt_ids,
74*0753bc2dSmaxims@google.com 	.ops = &sandbox_wdt_ops,
75*0753bc2dSmaxims@google.com 	.probe = sandbox_wdt_probe,
76*0753bc2dSmaxims@google.com };
77