blob: aca2d6323f8ad14d11294895081e9a67178d11bd [file] [log] [blame]
Kumar Galafabbfb92006-01-14 13:20:50 -08001/*
Anton Vorontsov0d7b1012008-07-03 23:51:36 -07002 * mpc8xxx_wdt.c - MPC8xx/MPC83xx/MPC86xx watchdog userspace interface
Kumar Galafabbfb92006-01-14 13:20:50 -08003 *
4 * Authors: Dave Updegraff <dave@cray.org>
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +00005 * Kumar Gala <galak@kernel.crashing.org>
6 * Attribution: from 83xx_wst: Florian Schirmer <jolt@tuxbox.org>
7 * ..and from sc520_wdt
Anton Vorontsov500c9192008-07-03 23:51:34 -07008 * Copyright (c) 2008 MontaVista Software, Inc.
9 * Anton Vorontsov <avorontsov@ru.mvista.com>
Kumar Galafabbfb92006-01-14 13:20:50 -080010 *
11 * Note: it appears that you can only actually ENABLE or DISABLE the thing
12 * once after POR. Once enabled, you cannot disable, and vice versa.
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 */
19
Joe Perches27c766a2012-02-15 15:06:19 -080020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Kumar Galafabbfb92006-01-14 13:20:50 -080022#include <linux/fs.h>
23#include <linux/init.h>
24#include <linux/kernel.h>
Rob Herring5af50732013-09-17 14:28:33 -050025#include <linux/of_address.h>
Anton Vorontsovef8ab122008-07-03 23:51:32 -070026#include <linux/of_platform.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080027#include <linux/module.h>
28#include <linux/watchdog.h>
Alan Coxf26ef3d2008-05-19 14:07:09 +010029#include <linux/io.h>
30#include <linux/uaccess.h>
Anton Vorontsovef8ab122008-07-03 23:51:32 -070031#include <sysdev/fsl_soc.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080032
Christophe Leroy19ce9492017-11-08 15:39:44 +010033#define WATCHDOG_TIMEOUT 10
34
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070035struct mpc8xxx_wdt {
Kumar Galafabbfb92006-01-14 13:20:50 -080036 __be32 res0;
37 __be32 swcrr; /* System watchdog control register */
38#define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
Christophe Leroy19ce9492017-11-08 15:39:44 +010039#define SWCRR_SWF 0x00000008 /* Software Watchdog Freeze (mpc8xx). */
Kumar Galafabbfb92006-01-14 13:20:50 -080040#define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
41#define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
42#define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
43 __be32 swcnr; /* System watchdog count register */
44 u8 res1[2];
45 __be16 swsrr; /* System watchdog service register */
46 u8 res2[0xF0];
47};
48
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070049struct mpc8xxx_wdt_type {
Anton Vorontsov500c9192008-07-03 23:51:34 -070050 int prescaler;
51 bool hw_enabled;
52};
53
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020054struct mpc8xxx_wdt_ddata {
55 struct mpc8xxx_wdt __iomem *base;
56 struct watchdog_device wdd;
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020057 spinlock_t lock;
Christophe Leroy19ce9492017-11-08 15:39:44 +010058 u16 swtc;
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020059};
Kumar Galafabbfb92006-01-14 13:20:50 -080060
Christophe Leroy19ce9492017-11-08 15:39:44 +010061static u16 timeout;
Kumar Galafabbfb92006-01-14 13:20:50 -080062module_param(timeout, ushort, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010063MODULE_PARM_DESC(timeout,
Christophe Leroy19ce9492017-11-08 15:39:44 +010064 "Watchdog timeout in seconds. (1<timeout<65535, default="
65 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Kumar Galafabbfb92006-01-14 13:20:50 -080066
Rusty Russell90ab5ee2012-01-13 09:32:20 +103067static bool reset = 1;
Kumar Galafabbfb92006-01-14 13:20:50 -080068module_param(reset, bool, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010069MODULE_PARM_DESC(reset,
70 "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
Kumar Galafabbfb92006-01-14 13:20:50 -080071
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010072static bool nowayout = WATCHDOG_NOWAYOUT;
73module_param(nowayout, bool, 0);
Anton Vorontsov500c9192008-07-03 23:51:34 -070074MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
75 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
76
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020077static void mpc8xxx_wdt_keepalive(struct mpc8xxx_wdt_ddata *ddata)
Kumar Galafabbfb92006-01-14 13:20:50 -080078{
79 /* Ping the WDT */
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020080 spin_lock(&ddata->lock);
81 out_be16(&ddata->base->swsrr, 0x556c);
82 out_be16(&ddata->base->swsrr, 0xaa39);
83 spin_unlock(&ddata->lock);
Kumar Galafabbfb92006-01-14 13:20:50 -080084}
85
Christophe Leroyd5cfaf02013-12-04 07:32:14 +010086static int mpc8xxx_wdt_start(struct watchdog_device *w)
Kumar Galafabbfb92006-01-14 13:20:50 -080087{
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020088 struct mpc8xxx_wdt_ddata *ddata =
89 container_of(w, struct mpc8xxx_wdt_ddata, wdd);
Christophe Leroy19ce9492017-11-08 15:39:44 +010090 u32 tmp = in_be32(&ddata->base->swcrr);
Kumar Galafabbfb92006-01-14 13:20:50 -080091
92 /* Good, fire up the show */
Christophe Leroy19ce9492017-11-08 15:39:44 +010093 tmp &= ~(SWCRR_SWTC | SWCRR_SWF | SWCRR_SWEN | SWCRR_SWRI | SWCRR_SWPR);
94 tmp |= SWCRR_SWEN | SWCRR_SWPR | (ddata->swtc << 16);
95
Kumar Galafabbfb92006-01-14 13:20:50 -080096 if (reset)
97 tmp |= SWCRR_SWRI;
98
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020099 out_be32(&ddata->base->swcrr, tmp);
Kumar Galafabbfb92006-01-14 13:20:50 -0800100
Christophe Leroy19ce9492017-11-08 15:39:44 +0100101 tmp = in_be32(&ddata->base->swcrr);
102 if (!(tmp & SWCRR_SWEN))
103 return -EOPNOTSUPP;
104
105 ddata->swtc = tmp >> 16;
106 set_bit(WDOG_HW_RUNNING, &ddata->wdd.status);
Anton Vorontsov500c9192008-07-03 23:51:34 -0700107
Kumar Galafabbfb92006-01-14 13:20:50 -0800108 return 0;
109}
110
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100111static int mpc8xxx_wdt_ping(struct watchdog_device *w)
Kumar Galafabbfb92006-01-14 13:20:50 -0800112{
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200113 struct mpc8xxx_wdt_ddata *ddata =
114 container_of(w, struct mpc8xxx_wdt_ddata, wdd);
115
116 mpc8xxx_wdt_keepalive(ddata);
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100117 return 0;
Kumar Galafabbfb92006-01-14 13:20:50 -0800118}
119
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100120static struct watchdog_info mpc8xxx_wdt_info = {
Christophe Leroy19ce9492017-11-08 15:39:44 +0100121 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100122 .firmware_version = 1,
123 .identity = "MPC8xxx",
Kumar Galafabbfb92006-01-14 13:20:50 -0800124};
125
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100126static struct watchdog_ops mpc8xxx_wdt_ops = {
127 .owner = THIS_MODULE,
128 .start = mpc8xxx_wdt_start,
129 .ping = mpc8xxx_wdt_ping,
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100130};
131
Bill Pemberton2d991a12012-11-19 13:21:41 -0500132static int mpc8xxx_wdt_probe(struct platform_device *ofdev)
Kumar Galafabbfb92006-01-14 13:20:50 -0800133{
Kumar Galafabbfb92006-01-14 13:20:50 -0800134 int ret;
Uwe Kleine-Königde5f7122015-08-12 10:15:55 +0200135 struct resource *res;
Arnd Bergmann639397e2012-05-21 21:57:39 +0200136 const struct mpc8xxx_wdt_type *wdt_type;
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200137 struct mpc8xxx_wdt_ddata *ddata;
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700138 u32 freq = fsl_get_sys_freq();
Anton Vorontsov500c9192008-07-03 23:51:34 -0700139 bool enabled;
Kumar Galafabbfb92006-01-14 13:20:50 -0800140
Uwe Kleine-Königf0ded832015-08-12 10:15:54 +0200141 wdt_type = of_device_get_match_data(&ofdev->dev);
142 if (!wdt_type)
Grant Likely1c48a5c2011-02-17 02:43:24 -0700143 return -EINVAL;
Grant Likely1c48a5c2011-02-17 02:43:24 -0700144
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700145 if (!freq || freq == -1)
146 return -EINVAL;
Kumar Galafabbfb92006-01-14 13:20:50 -0800147
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200148 ddata = devm_kzalloc(&ofdev->dev, sizeof(*ddata), GFP_KERNEL);
149 if (!ddata)
150 return -ENOMEM;
Kumar Galafabbfb92006-01-14 13:20:50 -0800151
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200152 res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
153 ddata->base = devm_ioremap_resource(&ofdev->dev, res);
154 if (IS_ERR(ddata->base))
155 return PTR_ERR(ddata->base);
156
157 enabled = in_be32(&ddata->base->swcrr) & SWCRR_SWEN;
Anton Vorontsov500c9192008-07-03 23:51:34 -0700158 if (!enabled && wdt_type->hw_enabled) {
Joe Perches27c766a2012-02-15 15:06:19 -0800159 pr_info("could not be enabled in software\n");
Uwe Kleine-König72cd5012015-08-12 10:15:57 +0200160 return -ENODEV;
Anton Vorontsov500c9192008-07-03 23:51:34 -0700161 }
162
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200163 spin_lock_init(&ddata->lock);
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200164
165 ddata->wdd.info = &mpc8xxx_wdt_info,
166 ddata->wdd.ops = &mpc8xxx_wdt_ops,
167
Christophe Leroy19ce9492017-11-08 15:39:44 +0100168 ddata->wdd.timeout = WATCHDOG_TIMEOUT;
169 watchdog_init_timeout(&ddata->wdd, timeout, &ofdev->dev);
Uwe Kleine-König50ffb532015-08-12 10:15:53 +0200170
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200171 watchdog_set_nowayout(&ddata->wdd, nowayout);
Uwe Kleine-König50ffb532015-08-12 10:15:53 +0200172
Christophe Leroy19ce9492017-11-08 15:39:44 +0100173 ddata->swtc = min(ddata->wdd.timeout * freq / wdt_type->prescaler,
174 0xffffU);
Anton Vorontsov500c9192008-07-03 23:51:34 -0700175
176 /*
177 * If the watchdog was previously enabled or we're running on
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700178 * MPC8xxx, we should ping the wdt from the kernel until the
Anton Vorontsov500c9192008-07-03 23:51:34 -0700179 * userspace handles it.
180 */
181 if (enabled)
Christophe Leroy19ce9492017-11-08 15:39:44 +0100182 mpc8xxx_wdt_start(&ddata->wdd);
183
184 ddata->wdd.max_hw_heartbeat_ms = (ddata->swtc * wdt_type->prescaler) /
185 (freq / 1000);
186 ddata->wdd.min_timeout = ddata->wdd.max_hw_heartbeat_ms / 1000;
187 if (ddata->wdd.timeout < ddata->wdd.min_timeout)
188 ddata->wdd.timeout = ddata->wdd.min_timeout;
189
190 ret = watchdog_register_device(&ddata->wdd);
191 if (ret) {
192 pr_err("cannot register watchdog device (err=%d)\n", ret);
193 return ret;
194 }
195
196 pr_info("WDT driver for MPC8xxx initialized. mode:%s timeout=%d sec\n",
197 reset ? "reset" : "interrupt", ddata->wdd.timeout);
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200198
199 platform_set_drvdata(ofdev, ddata);
Kumar Galafabbfb92006-01-14 13:20:50 -0800200 return 0;
Kumar Galafabbfb92006-01-14 13:20:50 -0800201}
202
Bill Pemberton4b12b892012-11-19 13:26:24 -0500203static int mpc8xxx_wdt_remove(struct platform_device *ofdev)
Kumar Galafabbfb92006-01-14 13:20:50 -0800204{
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200205 struct mpc8xxx_wdt_ddata *ddata = platform_get_drvdata(ofdev);
206
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100207 pr_crit("Watchdog removed, expect the %s soon!\n",
208 reset ? "reset" : "machine check exception");
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200209 watchdog_unregister_device(&ddata->wdd);
Kumar Galafabbfb92006-01-14 13:20:50 -0800210
211 return 0;
212}
213
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700214static const struct of_device_id mpc8xxx_wdt_match[] = {
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700215 {
216 .compatible = "mpc83xx_wdt",
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700217 .data = &(struct mpc8xxx_wdt_type) {
Anton Vorontsov500c9192008-07-03 23:51:34 -0700218 .prescaler = 0x10000,
219 },
220 },
221 {
222 .compatible = "fsl,mpc8610-wdt",
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700223 .data = &(struct mpc8xxx_wdt_type) {
Anton Vorontsov500c9192008-07-03 23:51:34 -0700224 .prescaler = 0x10000,
225 .hw_enabled = true,
226 },
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700227 },
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700228 {
229 .compatible = "fsl,mpc823-wdt",
230 .data = &(struct mpc8xxx_wdt_type) {
231 .prescaler = 0x800,
Christophe Leroy4af897f2013-11-30 16:45:40 +0100232 .hw_enabled = true,
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700233 },
234 },
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700235 {},
236};
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700237MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700238
Grant Likely1c48a5c2011-02-17 02:43:24 -0700239static struct platform_driver mpc8xxx_wdt_driver = {
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700240 .probe = mpc8xxx_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500241 .remove = mpc8xxx_wdt_remove,
Grant Likely40182942010-04-13 16:13:02 -0700242 .driver = {
243 .name = "mpc8xxx_wdt",
Grant Likely40182942010-04-13 16:13:02 -0700244 .of_match_table = mpc8xxx_wdt_match,
Kumar Galafabbfb92006-01-14 13:20:50 -0800245 },
246};
247
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700248static int __init mpc8xxx_wdt_init(void)
Kumar Galafabbfb92006-01-14 13:20:50 -0800249{
Grant Likely1c48a5c2011-02-17 02:43:24 -0700250 return platform_driver_register(&mpc8xxx_wdt_driver);
Kumar Galafabbfb92006-01-14 13:20:50 -0800251}
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700252arch_initcall(mpc8xxx_wdt_init);
Kumar Galafabbfb92006-01-14 13:20:50 -0800253
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700254static void __exit mpc8xxx_wdt_exit(void)
Kumar Galafabbfb92006-01-14 13:20:50 -0800255{
Grant Likely1c48a5c2011-02-17 02:43:24 -0700256 platform_driver_unregister(&mpc8xxx_wdt_driver);
Kumar Galafabbfb92006-01-14 13:20:50 -0800257}
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700258module_exit(mpc8xxx_wdt_exit);
Kumar Galafabbfb92006-01-14 13:20:50 -0800259
260MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700261MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
262 "uProcessors");
Kumar Galafabbfb92006-01-14 13:20:50 -0800263MODULE_LICENSE("GPL");