exynos_wdt.c revision 1.1 1 /* $NetBSD: exynos_wdt.c,v 1.1 2014/04/13 02:26:26 matt Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "exynos_wdt.h"
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: exynos_wdt.c,v 1.1 2014/04/13 02:26:26 matt Exp $");
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/cpu.h>
40 #include <sys/device.h>
41 #include <sys/wdog.h>
42
43 #include <prop/proplib.h>
44
45 #include <dev/sysmon/sysmonvar.h>
46
47 #include <arm/samsung/exynos_reg.h>
48 #include <arm/samsung/exynos_var.h>
49
50 #if NEXYNOS_WDT > 0
51 static int exynos_wdt_match(device_t, cfdata_t, void *);
52 static void exynos_wdt_attach(device_t, device_t, void *);
53
54 struct exynos_wdt_softc {
55 struct sysmon_wdog sc_smw;
56 device_t sc_dev;
57 bus_space_tag_t sc_bst;
58 bus_space_handle_t sc_wdog_bsh;
59 u_int sc_wdog_period;
60 u_int sc_wdog_clock_select;
61 u_int sc_wdog_prescaler;
62 uint32_t sc_freq;
63 uint32_t sc_wdog_wtdat;
64 uint32_t sc_wdog_wtcon;
65 bool sc_wdog_armed;
66 };
67
68 #ifndef EXYNOS_WDT_PERIOD_DEFAULT
69 #define EXYNOS_WDT_PERIOD_DEFAULT 12
70 #endif
71
72 CFATTACH_DECL_NEW(exynos_wdt, sizeof(struct exynos_wdt_softc),
73 exynos_wdt_match, exynos_wdt_attach, NULL, NULL);
74
75 static inline uint32_t
76 exynos_wdt_wdog_read(struct exynos_wdt_softc *sc, bus_size_t o)
77 {
78 return bus_space_read_4(sc->sc_bst, sc->sc_wdog_bsh, o);
79 }
80
81 static inline void
82 exynos_wdt_wdog_write(struct exynos_wdt_softc *sc, bus_size_t o, uint32_t v)
83 {
84 bus_space_write_4(sc->sc_bst, sc->sc_wdog_bsh, o, v);
85 }
86
87 /* ARGSUSED */
88 static int
89 exynos_wdt_match(device_t parent, cfdata_t cf, void *aux)
90 {
91 return 1;
92 }
93
94 static int
95 exynos_wdt_tickle(struct sysmon_wdog *smw)
96 {
97 struct exynos_wdt_softc * const sc = smw->smw_cookie;
98
99 /*
100 * Cause the WDOG to restart counting.
101 */
102 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCNT, sc->sc_wdog_wtdat);
103 aprint_debug_dev(sc->sc_dev, "tickle\n");
104 return 0;
105 }
106
107 static int
108 exynos_wdt_setmode(struct sysmon_wdog *smw)
109 {
110 struct exynos_wdt_softc * const sc = smw->smw_cookie;
111
112 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
113 /*
114 * Emit magic sequence to turn off WDOG
115 */
116 sc->sc_wdog_wtcon &= ~(WTCON_ENABLE|WTCON_RESET_ENABLE);
117 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCON, sc->sc_wdog_wtcon);
118 delay(1);
119 aprint_debug_dev(sc->sc_dev, "setmode disable\n");
120 return 0;
121 }
122
123 /*
124 * If no changes, just tickle it and return.
125 */
126 if (sc->sc_wdog_armed && smw->smw_period == sc->sc_wdog_period) {
127 sc->sc_wdog_wtdat = sc->sc_freq * sc->sc_wdog_period - 1;
128 sc->sc_wdog_wtcon = WTCON_ENABLE | WTCON_RESET_ENABLE
129 | __SHIFTIN(sc->sc_wdog_clock_select, WTCON_CLOCK_SELECT)
130 | __SHIFTIN(sc->sc_wdog_prescaler - 1, WTCON_PRESCALER);
131
132 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCNT, sc->sc_wdog_wtdat);
133 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTDAT, sc->sc_wdog_wtdat);
134 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCON, sc->sc_wdog_wtcon);
135 aprint_debug_dev(sc->sc_dev, "setmode refresh\n");
136 return 0;
137 }
138
139 if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
140 sc->sc_wdog_period = EXYNOS_WDT_PERIOD_DEFAULT;
141 smw->smw_period = EXYNOS_WDT_PERIOD_DEFAULT;
142 }
143
144 /*
145 * Make sure we don't overflow the counter.
146 */
147 if (smw->smw_period * sc->sc_freq >= UINT16_MAX) {
148 return EINVAL;
149 }
150
151 sc->sc_wdog_wtdat = sc->sc_freq * sc->sc_wdog_period - 1;
152 sc->sc_wdog_wtcon = WTCON_ENABLE | WTCON_RESET_ENABLE
153 | __SHIFTIN(sc->sc_wdog_clock_select, WTCON_CLOCK_SELECT)
154 | __SHIFTIN(sc->sc_wdog_prescaler - 1, WTCON_PRESCALER);
155
156 /*
157 * Have to disable to be able to write WTDAT
158 */
159 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCON,
160 sc->sc_wdog_wtcon & ~(WTCON_ENABLE | WTCON_RESET_ENABLE));
161 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCNT, sc->sc_wdog_wtdat);
162 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTDAT, sc->sc_wdog_wtdat);
163 exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCON, sc->sc_wdog_wtcon);
164
165 aprint_debug_dev(sc->sc_dev, "setmode enable\n");
166 return 0;
167 }
168
169
170 static void
171 exynos_wdt_attach(device_t parent, device_t self, void *aux)
172 {
173 struct exynos_wdt_softc * const sc = device_private(self);
174 struct exyo_attach_args * const exyo = aux;
175 prop_dictionary_t dict = device_properties(self);
176
177 sc->sc_dev = self;
178 sc->sc_bst = exyo->exyo_core_bst;
179
180 if (bus_space_map(sc->sc_bst, exyo->exyo_loc.loc_offset,
181 exyo->exyo_loc.loc_size, 0, &sc->sc_wdog_bsh)) {
182 aprint_error(": failed to map registers\n");
183 return;
184 }
185
186 /*
187 * This runs at the Exynos Pclk.
188 */
189 prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq);
190
191 sc->sc_wdog_wtcon = exynos_wdt_wdog_read(sc, EXYNOS_WDT_WTCON);
192 sc->sc_wdog_armed = (sc->sc_wdog_wtcon & WTCON_ENABLE)
193 && (sc->sc_wdog_wtcon & WTCON_RESET_ENABLE);
194 if (sc->sc_wdog_armed) {
195 sc->sc_wdog_prescaler =
196 __SHIFTOUT(sc->sc_wdog_wtcon, WTCON_PRESCALER) + 1;
197 sc->sc_wdog_clock_select =
198 __SHIFTOUT(sc->sc_wdog_wtcon, WTCON_CLOCK_SELECT);
199 sc->sc_freq /= sc->sc_wdog_prescaler;
200 sc->sc_freq >>= 4 + sc->sc_wdog_clock_select;
201 sc->sc_wdog_wtdat = exynos_wdt_wdog_read(sc, EXYNOS_WDT_WTDAT);
202 sc->sc_wdog_period = (sc->sc_wdog_wtdat + 1) / sc->sc_freq;
203 } else {
204 sc->sc_wdog_period = EXYNOS_WDT_PERIOD_DEFAULT;
205 sc->sc_wdog_prescaler = 1;
206 /*
207 * Let's see what clock select we should use.
208 */
209 u_int n = __builtin_ffs(sc->sc_freq) - 1;
210 if (n > 7) {
211 sc->sc_wdog_clock_select = WTCON_CLOCK_SELECT_128;
212 sc->sc_freq >>= 7;
213 } else if (n >= 4) {
214 sc->sc_wdog_clock_select = n - 4;
215 sc->sc_freq >>= n;
216 }
217 /*
218 * Let's hope the timer frequency isn't prime. If it is, find
219 * the highest divisor which gives us the least remainder.
220 */
221 sc->sc_wdog_prescaler = 0;
222 u_int best_remainder = 256;
223 u_int max_period = 2 * EXYNOS_WDT_PERIOD_DEFAULT * sc->sc_freq;
224 for (size_t div = 256; UINT16_MAX > div * max_period; div++) {
225 u_int remainder = sc->sc_freq % div;
226 if (remainder == 0) {
227 sc->sc_wdog_prescaler = div;
228 break;
229 }
230 if (remainder < best_remainder) {
231 sc->sc_wdog_prescaler = div;
232 best_remainder = remainder;
233 }
234 }
235 KASSERT(sc->sc_wdog_prescaler != 0);
236 sc->sc_freq /= sc->sc_wdog_prescaler;
237 }
238
239 /*
240 * Does the config file tell us to turn on the watchdog?
241 */
242 if (device_cfdata(self)->cf_flags & 1)
243 sc->sc_wdog_armed = true;
244
245 aprint_naive("\n");
246 aprint_normal(": Exynos Watchdog Timer, default period is %u seconds%s\n",
247 sc->sc_wdog_period,
248 sc->sc_wdog_armed ? " (armed)" : "");
249
250 sc->sc_smw.smw_name = device_xname(self);
251 sc->sc_smw.smw_cookie = sc;
252 sc->sc_smw.smw_setmode = exynos_wdt_setmode;
253 sc->sc_smw.smw_tickle = exynos_wdt_tickle;
254 sc->sc_smw.smw_period = sc->sc_wdog_period;
255
256 if (sc->sc_wdog_armed) {
257 int error = sysmon_wdog_setmode(&sc->sc_smw, WDOG_MODE_KTICKLE,
258 sc->sc_wdog_period);
259 if (error)
260 aprint_error_dev(self,
261 "failed to start kernel tickler: %d\n", error);
262 }
263 }
264 #endif /* NEXYNOS_WDOG > 0 */
265
266 void
267 exynos_wdt_reset(void)
268 {
269 bus_space_tag_t bst = &exynos_bs_tag;
270 bus_space_handle_t bsh = exynos_core_bsh;
271 bus_addr_t wdt_offset = 0;
272 #ifdef EXYNOS4
273 if (IS_EXYNOS4_P()) {
274 wdt_offset = EXYNOS4_WDT_OFFSET;
275 }
276 #endif
277 #ifdef EXYNOS5
278 if (IS_EXYNOS5_P()) {
279 wdt_offset = EXYNOS5_WDT_OFFSET;
280 }
281 #endif
282 KASSERT(wdt_offset);
283
284 (void) splhigh();
285 bus_space_write_4(bst, bsh, wdt_offset + EXYNOS_WDT_WTCON, 0);
286 bus_space_write_4(bst, bsh, wdt_offset + EXYNOS_WDT_WTCNT, 1);
287 bus_space_write_4(bst, bsh, wdt_offset + EXYNOS_WDT_WTCON,
288 WTCON_ENABLE | WTCON_RESET_ENABLE);
289 }
290