Commit | Line | Data |
---|---|---|
4c076fb4 DD |
1 | /* |
2 | * Octeon Watchdog driver | |
3 | * | |
4 | * Copyright (C) 2007, 2008, 2009, 2010 Cavium Networks | |
5 | * | |
3d588c93 AK |
6 | * Converted to use WATCHDOG_CORE by Aaro Koskinen <aaro.koskinen@iki.fi>. |
7 | * | |
4c076fb4 DD |
8 | * Some parts derived from wdt.c |
9 | * | |
10 | * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>, | |
11 | * All Rights Reserved. | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU General Public License | |
15 | * as published by the Free Software Foundation; either version | |
16 | * 2 of the License, or (at your option) any later version. | |
17 | * | |
18 | * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide | |
19 | * warranty for any of this software. This material is provided | |
20 | * "AS-IS" and at no charge. | |
21 | * | |
22 | * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk> | |
23 | * | |
24 | * This file is subject to the terms and conditions of the GNU General Public | |
25 | * License. See the file "COPYING" in the main directory of this archive | |
26 | * for more details. | |
27 | * | |
28 | * | |
29 | * The OCTEON watchdog has a maximum timeout of 2^32 * io_clock. | |
30 | * For most systems this is less than 10 seconds, so to allow for | |
31 | * software to request longer watchdog heartbeats, we maintain software | |
32 | * counters to count multiples of the base rate. If the system locks | |
33 | * up in such a manner that we can not run the software counters, the | |
34 | * only result is a watchdog reset sooner than was requested. But | |
35 | * that is OK, because in this case userspace would likely not be able | |
36 | * to do anything anyhow. | |
37 | * | |
38 | * The hardware watchdog interval we call the period. The OCTEON | |
39 | * watchdog goes through several stages, after the first period an | |
40 | * irq is asserted, then if it is not reset, after the next period NMI | |
41 | * is asserted, then after an additional period a chip wide soft reset. | |
42 | * So for the software counters, we reset watchdog after each period | |
43 | * and decrement the counter. But for the last two periods we need to | |
44 | * let the watchdog progress to the NMI stage so we disable the irq | |
45 | * and let it proceed. Once in the NMI, we print the register state | |
46 | * to the serial port and then wait for the reset. | |
47 | * | |
48 | * A watchdog is maintained for each CPU in the system, that way if | |
49 | * one CPU suffers a lockup, we also get a register dump and reset. | |
50 | * The userspace ping resets the watchdog on all CPUs. | |
51 | * | |
52 | * Before userspace opens the watchdog device, we still run the | |
53 | * watchdogs to catch any lockups that may be kernel related. | |
54 | * | |
55 | */ | |
56 | ||
27c766aa JP |
57 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
58 | ||
4c076fb4 DD |
59 | #include <linux/miscdevice.h> |
60 | #include <linux/interrupt.h> | |
61 | #include <linux/watchdog.h> | |
62 | #include <linux/cpumask.h> | |
63 | #include <linux/bitops.h> | |
64 | #include <linux/kernel.h> | |
65 | #include <linux/module.h> | |
66 | #include <linux/string.h> | |
67 | #include <linux/delay.h> | |
68 | #include <linux/cpu.h> | |
69 | #include <linux/smp.h> | |
70 | #include <linux/fs.h> | |
ca4d3e67 | 71 | #include <linux/irq.h> |
4c076fb4 DD |
72 | |
73 | #include <asm/mipsregs.h> | |
74 | #include <asm/uasm.h> | |
75 | ||
76 | #include <asm/octeon/octeon.h> | |
77 | ||
78 | /* The count needed to achieve timeout_sec. */ | |
79 | static unsigned int timeout_cnt; | |
80 | ||
81 | /* The maximum period supported. */ | |
82 | static unsigned int max_timeout_sec; | |
83 | ||
84 | /* The current period. */ | |
85 | static unsigned int timeout_sec; | |
86 | ||
87 | /* Set to non-zero when userspace countdown mode active */ | |
88 | static int do_coundown; | |
89 | static unsigned int countdown_reset; | |
90 | static unsigned int per_cpu_countdown[NR_CPUS]; | |
91 | ||
92 | static cpumask_t irq_enabled_cpus; | |
93 | ||
94 | #define WD_TIMO 60 /* Default heartbeat = 60 seconds */ | |
95 | ||
96 | static int heartbeat = WD_TIMO; | |
97 | module_param(heartbeat, int, S_IRUGO); | |
98 | MODULE_PARM_DESC(heartbeat, | |
99 | "Watchdog heartbeat in seconds. (0 < heartbeat, default=" | |
100 | __MODULE_STRING(WD_TIMO) ")"); | |
101 | ||
86a1e189 WVS |
102 | static bool nowayout = WATCHDOG_NOWAYOUT; |
103 | module_param(nowayout, bool, S_IRUGO); | |
4c076fb4 DD |
104 | MODULE_PARM_DESC(nowayout, |
105 | "Watchdog cannot be stopped once started (default=" | |
106 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | |
107 | ||
4c076fb4 DD |
108 | static u32 __initdata nmi_stage1_insns[64]; |
109 | /* We need one branch and therefore one relocation per target label. */ | |
110 | static struct uasm_label __initdata labels[5]; | |
111 | static struct uasm_reloc __initdata relocs[5]; | |
112 | ||
113 | enum lable_id { | |
114 | label_enter_bootloader = 1 | |
115 | }; | |
116 | ||
117 | /* Some CP0 registers */ | |
118 | #define K0 26 | |
119 | #define C0_CVMMEMCTL 11, 7 | |
120 | #define C0_STATUS 12, 0 | |
121 | #define C0_EBASE 15, 1 | |
122 | #define C0_DESAVE 31, 0 | |
123 | ||
124 | void octeon_wdt_nmi_stage2(void); | |
125 | ||
126 | static void __init octeon_wdt_build_stage1(void) | |
127 | { | |
128 | int i; | |
129 | int len; | |
130 | u32 *p = nmi_stage1_insns; | |
131 | #ifdef CONFIG_HOTPLUG_CPU | |
132 | struct uasm_label *l = labels; | |
133 | struct uasm_reloc *r = relocs; | |
134 | #endif | |
135 | ||
136 | /* | |
137 | * For the next few instructions running the debugger may | |
138 | * cause corruption of k0 in the saved registers. Since we're | |
139 | * about to crash, nobody probably cares. | |
140 | * | |
141 | * Save K0 into the debug scratch register | |
142 | */ | |
143 | uasm_i_dmtc0(&p, K0, C0_DESAVE); | |
144 | ||
145 | uasm_i_mfc0(&p, K0, C0_STATUS); | |
146 | #ifdef CONFIG_HOTPLUG_CPU | |
f64988f1 AK |
147 | if (octeon_bootloader_entry_addr) |
148 | uasm_il_bbit0(&p, &r, K0, ilog2(ST0_NMI), | |
149 | label_enter_bootloader); | |
4c076fb4 DD |
150 | #endif |
151 | /* Force 64-bit addressing enabled */ | |
152 | uasm_i_ori(&p, K0, K0, ST0_UX | ST0_SX | ST0_KX); | |
153 | uasm_i_mtc0(&p, K0, C0_STATUS); | |
154 | ||
155 | #ifdef CONFIG_HOTPLUG_CPU | |
f64988f1 AK |
156 | if (octeon_bootloader_entry_addr) { |
157 | uasm_i_mfc0(&p, K0, C0_EBASE); | |
158 | /* Coreid number in K0 */ | |
159 | uasm_i_andi(&p, K0, K0, 0xf); | |
160 | /* 8 * coreid in bits 16-31 */ | |
161 | uasm_i_dsll_safe(&p, K0, K0, 3 + 16); | |
162 | uasm_i_ori(&p, K0, K0, 0x8001); | |
163 | uasm_i_dsll_safe(&p, K0, K0, 16); | |
164 | uasm_i_ori(&p, K0, K0, 0x0700); | |
165 | uasm_i_drotr_safe(&p, K0, K0, 32); | |
166 | /* | |
167 | * Should result in: 0x8001,0700,0000,8*coreid which is | |
168 | * CVMX_CIU_WDOGX(coreid) - 0x0500 | |
169 | * | |
170 | * Now ld K0, CVMX_CIU_WDOGX(coreid) | |
171 | */ | |
172 | uasm_i_ld(&p, K0, 0x500, K0); | |
173 | /* | |
174 | * If bit one set handle the NMI as a watchdog event. | |
175 | * otherwise transfer control to bootloader. | |
176 | */ | |
177 | uasm_il_bbit0(&p, &r, K0, 1, label_enter_bootloader); | |
178 | uasm_i_nop(&p); | |
179 | } | |
4c076fb4 DD |
180 | #endif |
181 | ||
182 | /* Clear Dcache so cvmseg works right. */ | |
183 | uasm_i_cache(&p, 1, 0, 0); | |
184 | ||
185 | /* Use K0 to do a read/modify/write of CVMMEMCTL */ | |
186 | uasm_i_dmfc0(&p, K0, C0_CVMMEMCTL); | |
187 | /* Clear out the size of CVMSEG */ | |
188 | uasm_i_dins(&p, K0, 0, 0, 6); | |
189 | /* Set CVMSEG to its largest value */ | |
190 | uasm_i_ori(&p, K0, K0, 0x1c0 | 54); | |
191 | /* Store the CVMMEMCTL value */ | |
192 | uasm_i_dmtc0(&p, K0, C0_CVMMEMCTL); | |
193 | ||
194 | /* Load the address of the second stage handler */ | |
195 | UASM_i_LA(&p, K0, (long)octeon_wdt_nmi_stage2); | |
196 | uasm_i_jr(&p, K0); | |
197 | uasm_i_dmfc0(&p, K0, C0_DESAVE); | |
198 | ||
199 | #ifdef CONFIG_HOTPLUG_CPU | |
f64988f1 AK |
200 | if (octeon_bootloader_entry_addr) { |
201 | uasm_build_label(&l, p, label_enter_bootloader); | |
202 | /* Jump to the bootloader and restore K0 */ | |
203 | UASM_i_LA(&p, K0, (long)octeon_bootloader_entry_addr); | |
204 | uasm_i_jr(&p, K0); | |
205 | uasm_i_dmfc0(&p, K0, C0_DESAVE); | |
206 | } | |
4c076fb4 DD |
207 | #endif |
208 | uasm_resolve_relocs(relocs, labels); | |
209 | ||
210 | len = (int)(p - nmi_stage1_insns); | |
27c766aa | 211 | pr_debug("Synthesized NMI stage 1 handler (%d instructions)\n", len); |
4c076fb4 DD |
212 | |
213 | pr_debug("\t.set push\n"); | |
214 | pr_debug("\t.set noreorder\n"); | |
215 | for (i = 0; i < len; i++) | |
216 | pr_debug("\t.word 0x%08x\n", nmi_stage1_insns[i]); | |
217 | pr_debug("\t.set pop\n"); | |
218 | ||
219 | if (len > 32) | |
220 | panic("NMI stage 1 handler exceeds 32 instructions, was %d\n", len); | |
221 | } | |
222 | ||
223 | static int cpu2core(int cpu) | |
224 | { | |
225 | #ifdef CONFIG_SMP | |
226 | return cpu_logical_map(cpu); | |
227 | #else | |
228 | return cvmx_get_core_num(); | |
229 | #endif | |
230 | } | |
231 | ||
232 | static int core2cpu(int coreid) | |
233 | { | |
234 | #ifdef CONFIG_SMP | |
235 | return cpu_number_map(coreid); | |
236 | #else | |
237 | return 0; | |
238 | #endif | |
239 | } | |
240 | ||
241 | /** | |
242 | * Poke the watchdog when an interrupt is received | |
243 | * | |
244 | * @cpl: | |
245 | * @dev_id: | |
246 | * | |
247 | * Returns | |
248 | */ | |
249 | static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id) | |
250 | { | |
251 | unsigned int core = cvmx_get_core_num(); | |
252 | int cpu = core2cpu(core); | |
253 | ||
254 | if (do_coundown) { | |
255 | if (per_cpu_countdown[cpu] > 0) { | |
256 | /* We're alive, poke the watchdog */ | |
257 | cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1); | |
258 | per_cpu_countdown[cpu]--; | |
259 | } else { | |
260 | /* Bad news, you are about to reboot. */ | |
261 | disable_irq_nosync(cpl); | |
262 | cpumask_clear_cpu(cpu, &irq_enabled_cpus); | |
263 | } | |
264 | } else { | |
265 | /* Not open, just ping away... */ | |
266 | cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1); | |
267 | } | |
268 | return IRQ_HANDLED; | |
269 | } | |
270 | ||
271 | /* From setup.c */ | |
272 | extern int prom_putchar(char c); | |
273 | ||
274 | /** | |
275 | * Write a string to the uart | |
276 | * | |
277 | * @str: String to write | |
278 | */ | |
279 | static void octeon_wdt_write_string(const char *str) | |
280 | { | |
281 | /* Just loop writing one byte at a time */ | |
282 | while (*str) | |
283 | prom_putchar(*str++); | |
284 | } | |
285 | ||
286 | /** | |
287 | * Write a hex number out of the uart | |
288 | * | |
289 | * @value: Number to display | |
290 | * @digits: Number of digits to print (1 to 16) | |
291 | */ | |
292 | static void octeon_wdt_write_hex(u64 value, int digits) | |
293 | { | |
294 | int d; | |
295 | int v; | |
296 | for (d = 0; d < digits; d++) { | |
297 | v = (value >> ((digits - d - 1) * 4)) & 0xf; | |
298 | if (v >= 10) | |
299 | prom_putchar('a' + v - 10); | |
300 | else | |
301 | prom_putchar('0' + v); | |
302 | } | |
303 | } | |
304 | ||
305 | const char *reg_name[] = { | |
306 | "$0", "at", "v0", "v1", "a0", "a1", "a2", "a3", | |
307 | "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3", | |
308 | "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", | |
309 | "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra" | |
310 | }; | |
311 | ||
312 | /** | |
313 | * NMI stage 3 handler. NMIs are handled in the following manner: | |
314 | * 1) The first NMI handler enables CVMSEG and transfers from | |
315 | * the bootbus region into normal memory. It is careful to not | |
316 | * destroy any registers. | |
317 | * 2) The second stage handler uses CVMSEG to save the registers | |
318 | * and create a stack for C code. It then calls the third level | |
319 | * handler with one argument, a pointer to the register values. | |
320 | * 3) The third, and final, level handler is the following C | |
321 | * function that prints out some useful infomration. | |
322 | * | |
323 | * @reg: Pointer to register state before the NMI | |
324 | */ | |
325 | void octeon_wdt_nmi_stage3(u64 reg[32]) | |
326 | { | |
327 | u64 i; | |
328 | ||
329 | unsigned int coreid = cvmx_get_core_num(); | |
330 | /* | |
331 | * Save status and cause early to get them before any changes | |
332 | * might happen. | |
333 | */ | |
334 | u64 cp0_cause = read_c0_cause(); | |
335 | u64 cp0_status = read_c0_status(); | |
336 | u64 cp0_error_epc = read_c0_errorepc(); | |
337 | u64 cp0_epc = read_c0_epc(); | |
338 | ||
339 | /* Delay so output from all cores output is not jumbled together. */ | |
340 | __delay(100000000ull * coreid); | |
341 | ||
342 | octeon_wdt_write_string("\r\n*** NMI Watchdog interrupt on Core 0x"); | |
343 | octeon_wdt_write_hex(coreid, 1); | |
344 | octeon_wdt_write_string(" ***\r\n"); | |
345 | for (i = 0; i < 32; i++) { | |
346 | octeon_wdt_write_string("\t"); | |
347 | octeon_wdt_write_string(reg_name[i]); | |
348 | octeon_wdt_write_string("\t0x"); | |
349 | octeon_wdt_write_hex(reg[i], 16); | |
350 | if (i & 1) | |
351 | octeon_wdt_write_string("\r\n"); | |
352 | } | |
353 | octeon_wdt_write_string("\terr_epc\t0x"); | |
354 | octeon_wdt_write_hex(cp0_error_epc, 16); | |
355 | ||
356 | octeon_wdt_write_string("\tepc\t0x"); | |
357 | octeon_wdt_write_hex(cp0_epc, 16); | |
358 | octeon_wdt_write_string("\r\n"); | |
359 | ||
360 | octeon_wdt_write_string("\tstatus\t0x"); | |
361 | octeon_wdt_write_hex(cp0_status, 16); | |
362 | octeon_wdt_write_string("\tcause\t0x"); | |
363 | octeon_wdt_write_hex(cp0_cause, 16); | |
364 | octeon_wdt_write_string("\r\n"); | |
365 | ||
366 | octeon_wdt_write_string("\tsum0\t0x"); | |
367 | octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_SUM0(coreid * 2)), 16); | |
368 | octeon_wdt_write_string("\ten0\t0x"); | |
369 | octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2)), 16); | |
370 | octeon_wdt_write_string("\r\n"); | |
371 | ||
372 | octeon_wdt_write_string("*** Chip soft reset soon ***\r\n"); | |
373 | } | |
374 | ||
375 | static void octeon_wdt_disable_interrupt(int cpu) | |
376 | { | |
377 | unsigned int core; | |
378 | unsigned int irq; | |
379 | union cvmx_ciu_wdogx ciu_wdog; | |
380 | ||
381 | core = cpu2core(cpu); | |
382 | ||
383 | irq = OCTEON_IRQ_WDOG0 + core; | |
384 | ||
385 | /* Poke the watchdog to clear out its state */ | |
386 | cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1); | |
387 | ||
388 | /* Disable the hardware. */ | |
389 | ciu_wdog.u64 = 0; | |
390 | cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64); | |
391 | ||
392 | free_irq(irq, octeon_wdt_poke_irq); | |
393 | } | |
394 | ||
395 | static void octeon_wdt_setup_interrupt(int cpu) | |
396 | { | |
397 | unsigned int core; | |
398 | unsigned int irq; | |
399 | union cvmx_ciu_wdogx ciu_wdog; | |
400 | ||
401 | core = cpu2core(cpu); | |
402 | ||
403 | /* Disable it before doing anything with the interrupts. */ | |
404 | ciu_wdog.u64 = 0; | |
405 | cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64); | |
406 | ||
407 | per_cpu_countdown[cpu] = countdown_reset; | |
408 | ||
409 | irq = OCTEON_IRQ_WDOG0 + core; | |
410 | ||
411 | if (request_irq(irq, octeon_wdt_poke_irq, | |
47bfd058 | 412 | IRQF_NO_THREAD, "octeon_wdt", octeon_wdt_poke_irq)) |
4c076fb4 DD |
413 | panic("octeon_wdt: Couldn't obtain irq %d", irq); |
414 | ||
415 | cpumask_set_cpu(cpu, &irq_enabled_cpus); | |
416 | ||
417 | /* Poke the watchdog to clear out its state */ | |
418 | cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1); | |
419 | ||
420 | /* Finally enable the watchdog now that all handlers are installed */ | |
421 | ciu_wdog.u64 = 0; | |
422 | ciu_wdog.s.len = timeout_cnt; | |
423 | ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */ | |
424 | cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64); | |
425 | } | |
426 | ||
427 | static int octeon_wdt_cpu_callback(struct notifier_block *nfb, | |
428 | unsigned long action, void *hcpu) | |
429 | { | |
430 | unsigned int cpu = (unsigned long)hcpu; | |
431 | ||
432 | switch (action) { | |
433 | case CPU_DOWN_PREPARE: | |
434 | octeon_wdt_disable_interrupt(cpu); | |
435 | break; | |
436 | case CPU_ONLINE: | |
437 | case CPU_DOWN_FAILED: | |
438 | octeon_wdt_setup_interrupt(cpu); | |
439 | break; | |
440 | default: | |
441 | break; | |
442 | } | |
443 | return NOTIFY_OK; | |
444 | } | |
445 | ||
3d588c93 | 446 | static int octeon_wdt_ping(struct watchdog_device __always_unused *wdog) |
4c076fb4 DD |
447 | { |
448 | int cpu; | |
449 | int coreid; | |
450 | ||
451 | for_each_online_cpu(cpu) { | |
452 | coreid = cpu2core(cpu); | |
453 | cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1); | |
454 | per_cpu_countdown[cpu] = countdown_reset; | |
455 | if ((countdown_reset || !do_coundown) && | |
456 | !cpumask_test_cpu(cpu, &irq_enabled_cpus)) { | |
457 | /* We have to enable the irq */ | |
458 | int irq = OCTEON_IRQ_WDOG0 + coreid; | |
459 | enable_irq(irq); | |
460 | cpumask_set_cpu(cpu, &irq_enabled_cpus); | |
461 | } | |
462 | } | |
3d588c93 | 463 | return 0; |
4c076fb4 DD |
464 | } |
465 | ||
466 | static void octeon_wdt_calc_parameters(int t) | |
467 | { | |
468 | unsigned int periods; | |
469 | ||
470 | timeout_sec = max_timeout_sec; | |
471 | ||
472 | ||
473 | /* | |
474 | * Find the largest interrupt period, that can evenly divide | |
475 | * the requested heartbeat time. | |
476 | */ | |
477 | while ((t % timeout_sec) != 0) | |
478 | timeout_sec--; | |
479 | ||
480 | periods = t / timeout_sec; | |
481 | ||
482 | /* | |
483 | * The last two periods are after the irq is disabled, and | |
484 | * then to the nmi, so we subtract them off. | |
485 | */ | |
486 | ||
487 | countdown_reset = periods > 2 ? periods - 2 : 0; | |
488 | heartbeat = t; | |
468ffde4 | 489 | timeout_cnt = ((octeon_get_io_clock_rate() >> 8) * timeout_sec) >> 8; |
4c076fb4 DD |
490 | } |
491 | ||
3d588c93 AK |
492 | static int octeon_wdt_set_timeout(struct watchdog_device *wdog, |
493 | unsigned int t) | |
4c076fb4 DD |
494 | { |
495 | int cpu; | |
496 | int coreid; | |
497 | union cvmx_ciu_wdogx ciu_wdog; | |
498 | ||
499 | if (t <= 0) | |
500 | return -1; | |
501 | ||
502 | octeon_wdt_calc_parameters(t); | |
503 | ||
504 | for_each_online_cpu(cpu) { | |
505 | coreid = cpu2core(cpu); | |
506 | cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1); | |
507 | ciu_wdog.u64 = 0; | |
508 | ciu_wdog.s.len = timeout_cnt; | |
509 | ciu_wdog.s.mode = 3; /* 3 = Interrupt + NMI + Soft-Reset */ | |
510 | cvmx_write_csr(CVMX_CIU_WDOGX(coreid), ciu_wdog.u64); | |
511 | cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1); | |
512 | } | |
3d588c93 | 513 | octeon_wdt_ping(wdog); /* Get the irqs back on. */ |
4c076fb4 DD |
514 | return 0; |
515 | } | |
516 | ||
3d588c93 | 517 | static int octeon_wdt_start(struct watchdog_device *wdog) |
4c076fb4 | 518 | { |
3d588c93 | 519 | octeon_wdt_ping(wdog); |
4c076fb4 | 520 | do_coundown = 1; |
3d588c93 | 521 | return 0; |
4c076fb4 DD |
522 | } |
523 | ||
3d588c93 | 524 | static int octeon_wdt_stop(struct watchdog_device *wdog) |
4c076fb4 | 525 | { |
3d588c93 AK |
526 | do_coundown = 0; |
527 | octeon_wdt_ping(wdog); | |
4c076fb4 DD |
528 | return 0; |
529 | } | |
530 | ||
3d588c93 AK |
531 | static struct notifier_block octeon_wdt_cpu_notifier = { |
532 | .notifier_call = octeon_wdt_cpu_callback, | |
4c076fb4 DD |
533 | }; |
534 | ||
3d588c93 AK |
535 | static const struct watchdog_info octeon_wdt_info = { |
536 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, | |
537 | .identity = "OCTEON", | |
4c076fb4 DD |
538 | }; |
539 | ||
3d588c93 AK |
540 | static const struct watchdog_ops octeon_wdt_ops = { |
541 | .owner = THIS_MODULE, | |
542 | .start = octeon_wdt_start, | |
543 | .stop = octeon_wdt_stop, | |
544 | .ping = octeon_wdt_ping, | |
545 | .set_timeout = octeon_wdt_set_timeout, | |
4c076fb4 DD |
546 | }; |
547 | ||
3d588c93 AK |
548 | static struct watchdog_device octeon_wdt = { |
549 | .info = &octeon_wdt_info, | |
550 | .ops = &octeon_wdt_ops, | |
551 | }; | |
4c076fb4 DD |
552 | |
553 | /** | |
554 | * Module/ driver initialization. | |
555 | * | |
556 | * Returns Zero on success | |
557 | */ | |
558 | static int __init octeon_wdt_init(void) | |
559 | { | |
560 | int i; | |
561 | int ret; | |
562 | int cpu; | |
563 | u64 *ptr; | |
564 | ||
565 | /* | |
566 | * Watchdog time expiration length = The 16 bits of LEN | |
567 | * represent the most significant bits of a 24 bit decrementer | |
568 | * that decrements every 256 cycles. | |
569 | * | |
570 | * Try for a timeout of 5 sec, if that fails a smaller number | |
571 | * of even seconds, | |
572 | */ | |
573 | max_timeout_sec = 6; | |
574 | do { | |
575 | max_timeout_sec--; | |
468ffde4 | 576 | timeout_cnt = ((octeon_get_io_clock_rate() >> 8) * max_timeout_sec) >> 8; |
4c076fb4 DD |
577 | } while (timeout_cnt > 65535); |
578 | ||
579 | BUG_ON(timeout_cnt == 0); | |
580 | ||
581 | octeon_wdt_calc_parameters(heartbeat); | |
582 | ||
27c766aa | 583 | pr_info("Initial granularity %d Sec\n", timeout_sec); |
4c076fb4 | 584 | |
3d588c93 AK |
585 | octeon_wdt.timeout = timeout_sec; |
586 | octeon_wdt.max_timeout = UINT_MAX; | |
587 | ||
588 | watchdog_set_nowayout(&octeon_wdt, nowayout); | |
589 | ||
590 | ret = watchdog_register_device(&octeon_wdt); | |
4c076fb4 | 591 | if (ret) { |
3d588c93 AK |
592 | pr_err("watchdog_register_device() failed: %d\n", ret); |
593 | return ret; | |
4c076fb4 DD |
594 | } |
595 | ||
596 | /* Build the NMI handler ... */ | |
597 | octeon_wdt_build_stage1(); | |
598 | ||
599 | /* ... and install it. */ | |
600 | ptr = (u64 *) nmi_stage1_insns; | |
601 | for (i = 0; i < 16; i++) { | |
602 | cvmx_write_csr(CVMX_MIO_BOOT_LOC_ADR, i * 8); | |
603 | cvmx_write_csr(CVMX_MIO_BOOT_LOC_DAT, ptr[i]); | |
604 | } | |
605 | cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0x81fc0000); | |
606 | ||
607 | cpumask_clear(&irq_enabled_cpus); | |
608 | ||
99c3bf36 | 609 | cpu_notifier_register_begin(); |
4c076fb4 DD |
610 | for_each_online_cpu(cpu) |
611 | octeon_wdt_setup_interrupt(cpu); | |
612 | ||
99c3bf36 SB |
613 | __register_hotcpu_notifier(&octeon_wdt_cpu_notifier); |
614 | cpu_notifier_register_done(); | |
615 | ||
3d588c93 | 616 | return 0; |
4c076fb4 DD |
617 | } |
618 | ||
619 | /** | |
620 | * Module / driver shutdown | |
621 | */ | |
622 | static void __exit octeon_wdt_cleanup(void) | |
623 | { | |
624 | int cpu; | |
625 | ||
3d588c93 | 626 | watchdog_unregister_device(&octeon_wdt); |
4c076fb4 | 627 | |
99c3bf36 SB |
628 | cpu_notifier_register_begin(); |
629 | __unregister_hotcpu_notifier(&octeon_wdt_cpu_notifier); | |
4c076fb4 DD |
630 | |
631 | for_each_online_cpu(cpu) { | |
632 | int core = cpu2core(cpu); | |
633 | /* Disable the watchdog */ | |
634 | cvmx_write_csr(CVMX_CIU_WDOGX(core), 0); | |
635 | /* Free the interrupt handler */ | |
636 | free_irq(OCTEON_IRQ_WDOG0 + core, octeon_wdt_poke_irq); | |
637 | } | |
99c3bf36 SB |
638 | |
639 | cpu_notifier_register_done(); | |
640 | ||
4c076fb4 DD |
641 | /* |
642 | * Disable the boot-bus memory, the code it points to is soon | |
643 | * to go missing. | |
644 | */ | |
645 | cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0); | |
646 | } | |
647 | ||
648 | MODULE_LICENSE("GPL"); | |
649 | MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>"); | |
650 | MODULE_DESCRIPTION("Cavium Networks Octeon Watchdog driver."); | |
651 | module_init(octeon_wdt_init); | |
652 | module_exit(octeon_wdt_cleanup); |