1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <hang.h>
10 #include <log.h>
11 #include <time.h>
12 #include <wdt.h>
13 #include <asm/global_data.h>
14 #include <dm/device-internal.h>
15 #include <dm/lists.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 #define WATCHDOG_TIMEOUT_SECS	(CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
20 
21 /*
22  * Reset every 1000ms, or however often is required as indicated by a
23  * hw_margin_ms property.
24  */
25 static ulong reset_period = 1000;
26 
initr_watchdog(void)27 int initr_watchdog(void)
28 {
29 	u32 timeout = WATCHDOG_TIMEOUT_SECS;
30 
31 	/*
32 	 * Init watchdog: This will call the probe function of the
33 	 * watchdog driver, enabling the use of the device
34 	 */
35 	if (uclass_get_device_by_seq(UCLASS_WDT, 0,
36 				     (struct udevice **)&gd->watchdog_dev)) {
37 		debug("WDT:   Not found by seq!\n");
38 		if (uclass_get_device(UCLASS_WDT, 0,
39 				      (struct udevice **)&gd->watchdog_dev)) {
40 			printf("WDT:   Not found!\n");
41 			return 0;
42 		}
43 	}
44 
45 	if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
46 		timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
47 					       WATCHDOG_TIMEOUT_SECS);
48 		reset_period = dev_read_u32_default(gd->watchdog_dev,
49 						    "hw_margin_ms",
50 						    4 * reset_period) / 4;
51 	}
52 
53 	wdt_start(gd->watchdog_dev, timeout * 1000, 0);
54 	gd->flags |= GD_FLG_WDT_READY;
55 	printf("WDT:   Started with%s servicing (%ds timeout)\n",
56 	       IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
57 
58 	return 0;
59 }
60 
wdt_start(struct udevice * dev,u64 timeout_ms,ulong flags)61 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
62 {
63 	const struct wdt_ops *ops = device_get_ops(dev);
64 
65 	if (!ops->start)
66 		return -ENOSYS;
67 
68 	return ops->start(dev, timeout_ms, flags);
69 }
70 
wdt_stop(struct udevice * dev)71 int wdt_stop(struct udevice *dev)
72 {
73 	const struct wdt_ops *ops = device_get_ops(dev);
74 
75 	if (!ops->stop)
76 		return -ENOSYS;
77 
78 	return ops->stop(dev);
79 }
80 
wdt_reset(struct udevice * dev)81 int wdt_reset(struct udevice *dev)
82 {
83 	const struct wdt_ops *ops = device_get_ops(dev);
84 
85 	if (!ops->reset)
86 		return -ENOSYS;
87 
88 	return ops->reset(dev);
89 }
90 
wdt_expire_now(struct udevice * dev,ulong flags)91 int wdt_expire_now(struct udevice *dev, ulong flags)
92 {
93 	int ret = 0;
94 	const struct wdt_ops *ops;
95 
96 	debug("WDT Resetting: %lu\n", flags);
97 	ops = device_get_ops(dev);
98 	if (ops->expire_now) {
99 		return ops->expire_now(dev, flags);
100 	} else {
101 		if (!ops->start)
102 			return -ENOSYS;
103 
104 		ret = ops->start(dev, 1, flags);
105 		if (ret < 0)
106 			return ret;
107 
108 		hang();
109 	}
110 
111 	return ret;
112 }
113 
114 #if defined(CONFIG_WATCHDOG)
115 /*
116  * Called by macro WATCHDOG_RESET. This function be called *very* early,
117  * so we need to make sure, that the watchdog driver is ready before using
118  * it in this function.
119  */
watchdog_reset(void)120 void watchdog_reset(void)
121 {
122 	static ulong next_reset;
123 	ulong now;
124 
125 	/* Exit if GD is not ready or watchdog is not initialized yet */
126 	if (!gd || !(gd->flags & GD_FLG_WDT_READY))
127 		return;
128 
129 	/* Do not reset the watchdog too often */
130 	now = get_timer(0);
131 	if (time_after(now, next_reset)) {
132 		next_reset = now + reset_period;
133 		wdt_reset(gd->watchdog_dev);
134 	}
135 }
136 #endif
137 
wdt_post_bind(struct udevice * dev)138 static int wdt_post_bind(struct udevice *dev)
139 {
140 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
141 	struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
142 	static int reloc_done;
143 
144 	if (!reloc_done) {
145 		if (ops->start)
146 			ops->start += gd->reloc_off;
147 		if (ops->stop)
148 			ops->stop += gd->reloc_off;
149 		if (ops->reset)
150 			ops->reset += gd->reloc_off;
151 		if (ops->expire_now)
152 			ops->expire_now += gd->reloc_off;
153 
154 		reloc_done++;
155 	}
156 #endif
157 	return 0;
158 }
159 
160 UCLASS_DRIVER(wdt) = {
161 	.id		= UCLASS_WDT,
162 	.name		= "watchdog",
163 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
164 	.post_bind	= wdt_post_bind,
165 };
166