watchdog: octeon-wdt: Add support for cn68XX SOCs.
[linux-2.6-microblaze.git] / drivers / watchdog / octeon-wdt-main.c
1 /*
2  * Octeon Watchdog driver
3  *
4  * Copyright (C) 2007-2017 Cavium, Inc.
5  *
6  * Converted to use WATCHDOG_CORE by Aaro Koskinen <aaro.koskinen@iki.fi>.
7  *
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
57 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
58
59 #include <linux/interrupt.h>
60 #include <linux/watchdog.h>
61 #include <linux/cpumask.h>
62 #include <linux/module.h>
63 #include <linux/delay.h>
64 #include <linux/cpu.h>
65 #include <linux/irq.h>
66
67 #include <asm/mipsregs.h>
68 #include <asm/uasm.h>
69
70 #include <asm/octeon/octeon.h>
71 #include <asm/octeon/cvmx-boot-vector.h>
72 #include <asm/octeon/cvmx-ciu2-defs.h>
73
74 static int divisor;
75
76 /* The count needed to achieve timeout_sec. */
77 static unsigned int timeout_cnt;
78
79 /* The maximum period supported. */
80 static unsigned int max_timeout_sec;
81
82 /* The current period.  */
83 static unsigned int timeout_sec;
84
85 /* Set to non-zero when userspace countdown mode active */
86 static bool do_countdown;
87 static unsigned int countdown_reset;
88 static unsigned int per_cpu_countdown[NR_CPUS];
89
90 static cpumask_t irq_enabled_cpus;
91
92 #define WD_TIMO 60                      /* Default heartbeat = 60 seconds */
93
94 static int heartbeat = WD_TIMO;
95 module_param(heartbeat, int, 0444);
96 MODULE_PARM_DESC(heartbeat,
97         "Watchdog heartbeat in seconds. (0 < heartbeat, default="
98                                 __MODULE_STRING(WD_TIMO) ")");
99
100 static bool nowayout = WATCHDOG_NOWAYOUT;
101 module_param(nowayout, bool, 0444);
102 MODULE_PARM_DESC(nowayout,
103         "Watchdog cannot be stopped once started (default="
104                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
105
106 static int disable;
107 module_param(disable, int, 0444);
108 MODULE_PARM_DESC(disable,
109         "Disable the watchdog entirely (default=0)");
110
111 static struct cvmx_boot_vector_element *octeon_wdt_bootvector;
112
113 void octeon_wdt_nmi_stage2(void);
114
115 static int cpu2core(int cpu)
116 {
117 #ifdef CONFIG_SMP
118         return cpu_logical_map(cpu);
119 #else
120         return cvmx_get_core_num();
121 #endif
122 }
123
124 static int core2cpu(int coreid)
125 {
126 #ifdef CONFIG_SMP
127         return cpu_number_map(coreid);
128 #else
129         return 0;
130 #endif
131 }
132
133 /**
134  * Poke the watchdog when an interrupt is received
135  *
136  * @cpl:
137  * @dev_id:
138  *
139  * Returns
140  */
141 static irqreturn_t octeon_wdt_poke_irq(int cpl, void *dev_id)
142 {
143         unsigned int core = cvmx_get_core_num();
144         int cpu = core2cpu(core);
145
146         if (do_countdown) {
147                 if (per_cpu_countdown[cpu] > 0) {
148                         /* We're alive, poke the watchdog */
149                         cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
150                         per_cpu_countdown[cpu]--;
151                 } else {
152                         /* Bad news, you are about to reboot. */
153                         disable_irq_nosync(cpl);
154                         cpumask_clear_cpu(cpu, &irq_enabled_cpus);
155                 }
156         } else {
157                 /* Not open, just ping away... */
158                 cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
159         }
160         return IRQ_HANDLED;
161 }
162
163 /* From setup.c */
164 extern int prom_putchar(char c);
165
166 /**
167  * Write a string to the uart
168  *
169  * @str:        String to write
170  */
171 static void octeon_wdt_write_string(const char *str)
172 {
173         /* Just loop writing one byte at a time */
174         while (*str)
175                 prom_putchar(*str++);
176 }
177
178 /**
179  * Write a hex number out of the uart
180  *
181  * @value:      Number to display
182  * @digits:     Number of digits to print (1 to 16)
183  */
184 static void octeon_wdt_write_hex(u64 value, int digits)
185 {
186         int d;
187         int v;
188
189         for (d = 0; d < digits; d++) {
190                 v = (value >> ((digits - d - 1) * 4)) & 0xf;
191                 if (v >= 10)
192                         prom_putchar('a' + v - 10);
193                 else
194                         prom_putchar('0' + v);
195         }
196 }
197
198 static const char reg_name[][3] = {
199         "$0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
200         "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
201         "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
202         "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
203 };
204
205 /**
206  * NMI stage 3 handler. NMIs are handled in the following manner:
207  * 1) The first NMI handler enables CVMSEG and transfers from
208  * the bootbus region into normal memory. It is careful to not
209  * destroy any registers.
210  * 2) The second stage handler uses CVMSEG to save the registers
211  * and create a stack for C code. It then calls the third level
212  * handler with one argument, a pointer to the register values.
213  * 3) The third, and final, level handler is the following C
214  * function that prints out some useful infomration.
215  *
216  * @reg:    Pointer to register state before the NMI
217  */
218 void octeon_wdt_nmi_stage3(u64 reg[32])
219 {
220         u64 i;
221
222         unsigned int coreid = cvmx_get_core_num();
223         /*
224          * Save status and cause early to get them before any changes
225          * might happen.
226          */
227         u64 cp0_cause = read_c0_cause();
228         u64 cp0_status = read_c0_status();
229         u64 cp0_error_epc = read_c0_errorepc();
230         u64 cp0_epc = read_c0_epc();
231
232         /* Delay so output from all cores output is not jumbled together. */
233         udelay(85000 * coreid);
234
235         octeon_wdt_write_string("\r\n*** NMI Watchdog interrupt on Core 0x");
236         octeon_wdt_write_hex(coreid, 2);
237         octeon_wdt_write_string(" ***\r\n");
238         for (i = 0; i < 32; i++) {
239                 octeon_wdt_write_string("\t");
240                 octeon_wdt_write_string(reg_name[i]);
241                 octeon_wdt_write_string("\t0x");
242                 octeon_wdt_write_hex(reg[i], 16);
243                 if (i & 1)
244                         octeon_wdt_write_string("\r\n");
245         }
246         octeon_wdt_write_string("\terr_epc\t0x");
247         octeon_wdt_write_hex(cp0_error_epc, 16);
248
249         octeon_wdt_write_string("\tepc\t0x");
250         octeon_wdt_write_hex(cp0_epc, 16);
251         octeon_wdt_write_string("\r\n");
252
253         octeon_wdt_write_string("\tstatus\t0x");
254         octeon_wdt_write_hex(cp0_status, 16);
255         octeon_wdt_write_string("\tcause\t0x");
256         octeon_wdt_write_hex(cp0_cause, 16);
257         octeon_wdt_write_string("\r\n");
258
259         /* The CIU register is different for each Octeon model. */
260         if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {
261                 octeon_wdt_write_string("\tsrc_wd\t0x");
262                 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SRC_PPX_IP2_WDOG(coreid)), 16);
263                 octeon_wdt_write_string("\ten_wd\t0x");
264                 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_EN_PPX_IP2_WDOG(coreid)), 16);
265                 octeon_wdt_write_string("\r\n");
266                 octeon_wdt_write_string("\tsrc_rml\t0x");
267                 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SRC_PPX_IP2_RML(coreid)), 16);
268                 octeon_wdt_write_string("\ten_rml\t0x");
269                 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_EN_PPX_IP2_RML(coreid)), 16);
270                 octeon_wdt_write_string("\r\n");
271                 octeon_wdt_write_string("\tsum\t0x");
272                 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU2_SUM_PPX_IP2(coreid)), 16);
273                 octeon_wdt_write_string("\r\n");
274         } else if (!octeon_has_feature(OCTEON_FEATURE_CIU3)) {
275                 octeon_wdt_write_string("\tsum0\t0x");
276                 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_SUM0(coreid * 2)), 16);
277                 octeon_wdt_write_string("\ten0\t0x");
278                 octeon_wdt_write_hex(cvmx_read_csr(CVMX_CIU_INTX_EN0(coreid * 2)), 16);
279                 octeon_wdt_write_string("\r\n");
280         }
281
282         octeon_wdt_write_string("*** Chip soft reset soon ***\r\n");
283 }
284
285 static int octeon_wdt_cpu_pre_down(unsigned int cpu)
286 {
287         unsigned int core;
288         unsigned int irq;
289         union cvmx_ciu_wdogx ciu_wdog;
290
291         core = cpu2core(cpu);
292
293         irq = OCTEON_IRQ_WDOG0 + core;
294
295         /* Poke the watchdog to clear out its state */
296         cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
297
298         /* Disable the hardware. */
299         ciu_wdog.u64 = 0;
300         cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
301
302         free_irq(irq, octeon_wdt_poke_irq);
303         return 0;
304 }
305
306 static int octeon_wdt_cpu_online(unsigned int cpu)
307 {
308         unsigned int core;
309         unsigned int irq;
310         union cvmx_ciu_wdogx ciu_wdog;
311
312         core = cpu2core(cpu);
313
314         octeon_wdt_bootvector[core].target_ptr = (u64)octeon_wdt_nmi_stage2;
315
316         /* Disable it before doing anything with the interrupts. */
317         ciu_wdog.u64 = 0;
318         cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
319
320         per_cpu_countdown[cpu] = countdown_reset;
321
322         irq = OCTEON_IRQ_WDOG0 + core;
323
324         if (request_irq(irq, octeon_wdt_poke_irq,
325                         IRQF_NO_THREAD, "octeon_wdt", octeon_wdt_poke_irq))
326                 panic("octeon_wdt: Couldn't obtain irq %d", irq);
327
328         cpumask_set_cpu(cpu, &irq_enabled_cpus);
329
330         /* Poke the watchdog to clear out its state */
331         cvmx_write_csr(CVMX_CIU_PP_POKEX(core), 1);
332
333         /* Finally enable the watchdog now that all handlers are installed */
334         ciu_wdog.u64 = 0;
335         ciu_wdog.s.len = timeout_cnt;
336         ciu_wdog.s.mode = 3;    /* 3 = Interrupt + NMI + Soft-Reset */
337         cvmx_write_csr(CVMX_CIU_WDOGX(core), ciu_wdog.u64);
338
339         return 0;
340 }
341
342 static int octeon_wdt_ping(struct watchdog_device __always_unused *wdog)
343 {
344         int cpu;
345         int coreid;
346
347         if (disable)
348                 return 0;
349
350         for_each_online_cpu(cpu) {
351                 coreid = cpu2core(cpu);
352                 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
353                 per_cpu_countdown[cpu] = countdown_reset;
354                 if ((countdown_reset || !do_countdown) &&
355                     !cpumask_test_cpu(cpu, &irq_enabled_cpus)) {
356                         /* We have to enable the irq */
357                         int irq = OCTEON_IRQ_WDOG0 + coreid;
358
359                         enable_irq(irq);
360                         cpumask_set_cpu(cpu, &irq_enabled_cpus);
361                 }
362         }
363         return 0;
364 }
365
366 static void octeon_wdt_calc_parameters(int t)
367 {
368         unsigned int periods;
369
370         timeout_sec = max_timeout_sec;
371
372
373         /*
374          * Find the largest interrupt period, that can evenly divide
375          * the requested heartbeat time.
376          */
377         while ((t % timeout_sec) != 0)
378                 timeout_sec--;
379
380         periods = t / timeout_sec;
381
382         /*
383          * The last two periods are after the irq is disabled, and
384          * then to the nmi, so we subtract them off.
385          */
386
387         countdown_reset = periods > 2 ? periods - 2 : 0;
388         heartbeat = t;
389         timeout_cnt = ((octeon_get_io_clock_rate() / divisor) * timeout_sec) >> 8;
390 }
391
392 static int octeon_wdt_set_timeout(struct watchdog_device *wdog,
393                                   unsigned int t)
394 {
395         int cpu;
396         int coreid;
397         union cvmx_ciu_wdogx ciu_wdog;
398
399         if (t <= 0)
400                 return -1;
401
402         octeon_wdt_calc_parameters(t);
403
404         if (disable)
405                 return 0;
406
407         for_each_online_cpu(cpu) {
408                 coreid = cpu2core(cpu);
409                 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
410                 ciu_wdog.u64 = 0;
411                 ciu_wdog.s.len = timeout_cnt;
412                 ciu_wdog.s.mode = 3;    /* 3 = Interrupt + NMI + Soft-Reset */
413                 cvmx_write_csr(CVMX_CIU_WDOGX(coreid), ciu_wdog.u64);
414                 cvmx_write_csr(CVMX_CIU_PP_POKEX(coreid), 1);
415         }
416         octeon_wdt_ping(wdog); /* Get the irqs back on. */
417         return 0;
418 }
419
420 static int octeon_wdt_start(struct watchdog_device *wdog)
421 {
422         octeon_wdt_ping(wdog);
423         do_countdown = 1;
424         return 0;
425 }
426
427 static int octeon_wdt_stop(struct watchdog_device *wdog)
428 {
429         do_countdown = 0;
430         octeon_wdt_ping(wdog);
431         return 0;
432 }
433
434 static const struct watchdog_info octeon_wdt_info = {
435         .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
436         .identity = "OCTEON",
437 };
438
439 static const struct watchdog_ops octeon_wdt_ops = {
440         .owner          = THIS_MODULE,
441         .start          = octeon_wdt_start,
442         .stop           = octeon_wdt_stop,
443         .ping           = octeon_wdt_ping,
444         .set_timeout    = octeon_wdt_set_timeout,
445 };
446
447 static struct watchdog_device octeon_wdt = {
448         .info   = &octeon_wdt_info,
449         .ops    = &octeon_wdt_ops,
450 };
451
452 static enum cpuhp_state octeon_wdt_online;
453 /**
454  * Module/ driver initialization.
455  *
456  * Returns Zero on success
457  */
458 static int __init octeon_wdt_init(void)
459 {
460         int ret;
461
462         octeon_wdt_bootvector = cvmx_boot_vector_get();
463         if (!octeon_wdt_bootvector) {
464                 pr_err("Error: Cannot allocate boot vector.\n");
465                 return -ENOMEM;
466         }
467
468         if (OCTEON_IS_MODEL(OCTEON_CN68XX))
469                 divisor = 0x200;
470         else
471                 divisor = 0x100;
472
473         /*
474          * Watchdog time expiration length = The 16 bits of LEN
475          * represent the most significant bits of a 24 bit decrementer
476          * that decrements every divisor cycle.
477          *
478          * Try for a timeout of 5 sec, if that fails a smaller number
479          * of even seconds,
480          */
481         max_timeout_sec = 6;
482         do {
483                 max_timeout_sec--;
484                 timeout_cnt = ((octeon_get_io_clock_rate() / divisor) * max_timeout_sec) >> 8;
485         } while (timeout_cnt > 65535);
486
487         BUG_ON(timeout_cnt == 0);
488
489         octeon_wdt_calc_parameters(heartbeat);
490
491         pr_info("Initial granularity %d Sec\n", timeout_sec);
492
493         octeon_wdt.timeout      = timeout_sec;
494         octeon_wdt.max_timeout  = UINT_MAX;
495
496         watchdog_set_nowayout(&octeon_wdt, nowayout);
497
498         ret = watchdog_register_device(&octeon_wdt);
499         if (ret) {
500                 pr_err("watchdog_register_device() failed: %d\n", ret);
501                 return ret;
502         }
503
504         if (disable) {
505                 pr_notice("disabled\n");
506                 return 0;
507         }
508
509         cpumask_clear(&irq_enabled_cpus);
510
511         ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "watchdog/octeon:online",
512                                 octeon_wdt_cpu_online, octeon_wdt_cpu_pre_down);
513         if (ret < 0)
514                 goto err;
515         octeon_wdt_online = ret;
516         return 0;
517 err:
518         cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
519         watchdog_unregister_device(&octeon_wdt);
520         return ret;
521 }
522
523 /**
524  * Module / driver shutdown
525  */
526 static void __exit octeon_wdt_cleanup(void)
527 {
528         watchdog_unregister_device(&octeon_wdt);
529
530         if (disable)
531                 return;
532
533         cpuhp_remove_state(octeon_wdt_online);
534
535         /*
536          * Disable the boot-bus memory, the code it points to is soon
537          * to go missing.
538          */
539         cvmx_write_csr(CVMX_MIO_BOOT_LOC_CFGX(0), 0);
540 }
541
542 MODULE_LICENSE("GPL");
543 MODULE_AUTHOR("Cavium Inc. <support@cavium.com>");
544 MODULE_DESCRIPTION("Cavium Inc. OCTEON Watchdog driver.");
545 module_init(octeon_wdt_init);
546 module_exit(octeon_wdt_cleanup);