blob: 069072e6747df321876e54c94410f2801bff0c7c [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
Kumar Galafabbfb92006-01-14 13:20:50 -080020#include <linux/fs.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
Rob Herring5af50732013-09-17 14:28:33 -050023#include <linux/of_address.h>
Anton Vorontsovef8ab122008-07-03 23:51:32 -070024#include <linux/of_platform.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080025#include <linux/module.h>
26#include <linux/watchdog.h>
Alan Coxf26ef3d2008-05-19 14:07:09 +010027#include <linux/io.h>
28#include <linux/uaccess.h>
Anton Vorontsovef8ab122008-07-03 23:51:32 -070029#include <sysdev/fsl_soc.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080030
Christophe Leroy19ce9492017-11-08 15:39:44 +010031#define WATCHDOG_TIMEOUT 10
32
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070033struct mpc8xxx_wdt {
Kumar Galafabbfb92006-01-14 13:20:50 -080034 __be32 res0;
35 __be32 swcrr; /* System watchdog control register */
36#define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
Christophe Leroy19ce9492017-11-08 15:39:44 +010037#define SWCRR_SWF 0x00000008 /* Software Watchdog Freeze (mpc8xx). */
Kumar Galafabbfb92006-01-14 13:20:50 -080038#define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
39#define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
40#define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
41 __be32 swcnr; /* System watchdog count register */
42 u8 res1[2];
43 __be16 swsrr; /* System watchdog service register */
44 u8 res2[0xF0];
45};
46
Anton Vorontsov59ca1b02008-07-03 23:51:35 -070047struct mpc8xxx_wdt_type {
Anton Vorontsov500c9192008-07-03 23:51:34 -070048 int prescaler;
49 bool hw_enabled;
Christophe Leroy38e48b72018-09-17 06:22:50 +000050 u32 rsr_mask;
Anton Vorontsov500c9192008-07-03 23:51:34 -070051};
52
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020053struct mpc8xxx_wdt_ddata {
54 struct mpc8xxx_wdt __iomem *base;
55 struct watchdog_device wdd;
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020056 spinlock_t lock;
Christophe Leroy19ce9492017-11-08 15:39:44 +010057 u16 swtc;
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020058};
Kumar Galafabbfb92006-01-14 13:20:50 -080059
Christophe Leroy19ce9492017-11-08 15:39:44 +010060static u16 timeout;
Kumar Galafabbfb92006-01-14 13:20:50 -080061module_param(timeout, ushort, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010062MODULE_PARM_DESC(timeout,
Christophe Leroy19ce9492017-11-08 15:39:44 +010063 "Watchdog timeout in seconds. (1<timeout<65535, default="
64 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Kumar Galafabbfb92006-01-14 13:20:50 -080065
Rusty Russell90ab5ee2012-01-13 09:32:20 +103066static bool reset = 1;
Kumar Galafabbfb92006-01-14 13:20:50 -080067module_param(reset, bool, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010068MODULE_PARM_DESC(reset,
69 "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
Kumar Galafabbfb92006-01-14 13:20:50 -080070
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010071static bool nowayout = WATCHDOG_NOWAYOUT;
72module_param(nowayout, bool, 0);
Anton Vorontsov500c9192008-07-03 23:51:34 -070073MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
74 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
75
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020076static void mpc8xxx_wdt_keepalive(struct mpc8xxx_wdt_ddata *ddata)
Kumar Galafabbfb92006-01-14 13:20:50 -080077{
78 /* Ping the WDT */
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020079 spin_lock(&ddata->lock);
80 out_be16(&ddata->base->swsrr, 0x556c);
81 out_be16(&ddata->base->swsrr, 0xaa39);
82 spin_unlock(&ddata->lock);
Kumar Galafabbfb92006-01-14 13:20:50 -080083}
84
Christophe Leroyd5cfaf02013-12-04 07:32:14 +010085static int mpc8xxx_wdt_start(struct watchdog_device *w)
Kumar Galafabbfb92006-01-14 13:20:50 -080086{
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020087 struct mpc8xxx_wdt_ddata *ddata =
88 container_of(w, struct mpc8xxx_wdt_ddata, wdd);
Christophe Leroy19ce9492017-11-08 15:39:44 +010089 u32 tmp = in_be32(&ddata->base->swcrr);
Kumar Galafabbfb92006-01-14 13:20:50 -080090
91 /* Good, fire up the show */
Christophe Leroy19ce9492017-11-08 15:39:44 +010092 tmp &= ~(SWCRR_SWTC | SWCRR_SWF | SWCRR_SWEN | SWCRR_SWRI | SWCRR_SWPR);
93 tmp |= SWCRR_SWEN | SWCRR_SWPR | (ddata->swtc << 16);
94
Kumar Galafabbfb92006-01-14 13:20:50 -080095 if (reset)
96 tmp |= SWCRR_SWRI;
97
Uwe Kleine-König7997eba2015-08-12 10:15:56 +020098 out_be32(&ddata->base->swcrr, tmp);
Kumar Galafabbfb92006-01-14 13:20:50 -080099
Christophe Leroy19ce9492017-11-08 15:39:44 +0100100 tmp = in_be32(&ddata->base->swcrr);
101 if (!(tmp & SWCRR_SWEN))
102 return -EOPNOTSUPP;
103
104 ddata->swtc = tmp >> 16;
105 set_bit(WDOG_HW_RUNNING, &ddata->wdd.status);
Anton Vorontsov500c9192008-07-03 23:51:34 -0700106
Kumar Galafabbfb92006-01-14 13:20:50 -0800107 return 0;
108}
109
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100110static int mpc8xxx_wdt_ping(struct watchdog_device *w)
Kumar Galafabbfb92006-01-14 13:20:50 -0800111{
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200112 struct mpc8xxx_wdt_ddata *ddata =
113 container_of(w, struct mpc8xxx_wdt_ddata, wdd);
114
115 mpc8xxx_wdt_keepalive(ddata);
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100116 return 0;
Kumar Galafabbfb92006-01-14 13:20:50 -0800117}
118
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100119static struct watchdog_info mpc8xxx_wdt_info = {
Christophe Leroy19ce9492017-11-08 15:39:44 +0100120 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100121 .firmware_version = 1,
122 .identity = "MPC8xxx",
Kumar Galafabbfb92006-01-14 13:20:50 -0800123};
124
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100125static struct watchdog_ops mpc8xxx_wdt_ops = {
126 .owner = THIS_MODULE,
127 .start = mpc8xxx_wdt_start,
128 .ping = mpc8xxx_wdt_ping,
Christophe Leroyd5cfaf02013-12-04 07:32:14 +0100129};
130
Bill Pemberton2d991a12012-11-19 13:21:41 -0500131static int mpc8xxx_wdt_probe(struct platform_device *ofdev)
Kumar Galafabbfb92006-01-14 13:20:50 -0800132{
Kumar Galafabbfb92006-01-14 13:20:50 -0800133 int ret;
Uwe Kleine-Königde5f7122015-08-12 10:15:55 +0200134 struct resource *res;
Arnd Bergmann639397e2012-05-21 21:57:39 +0200135 const struct mpc8xxx_wdt_type *wdt_type;
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200136 struct mpc8xxx_wdt_ddata *ddata;
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700137 u32 freq = fsl_get_sys_freq();
Anton Vorontsov500c9192008-07-03 23:51:34 -0700138 bool enabled;
Christophe Leroy79b10e02018-09-17 06:22:47 +0000139 struct device *dev = &ofdev->dev;
Kumar Galafabbfb92006-01-14 13:20:50 -0800140
Christophe Leroy79b10e02018-09-17 06:22:47 +0000141 wdt_type = of_device_get_match_data(dev);
Uwe Kleine-Königf0ded832015-08-12 10:15:54 +0200142 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
Christophe Leroy79b10e02018-09-17 06:22:47 +0000148 ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200149 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);
Christophe Leroy79b10e02018-09-17 06:22:47 +0000153 ddata->base = devm_ioremap_resource(dev, res);
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200154 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) {
Christophe Leroy79b10e02018-09-17 06:22:47 +0000159 dev_info(dev, "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
Christophe Leroy38e48b72018-09-17 06:22:50 +0000163 res = platform_get_resource(ofdev, IORESOURCE_MEM, 1);
164 if (res) {
165 bool status;
166 u32 __iomem *rsr = ioremap(res->start, resource_size(res));
167
168 if (!rsr)
169 return -ENOMEM;
170
171 status = in_be32(rsr) & wdt_type->rsr_mask;
172 ddata->wdd.bootstatus = status ? WDIOF_CARDRESET : 0;
173 /* clear reset status bits related to watchdog timer */
174 out_be32(rsr, wdt_type->rsr_mask);
175 iounmap(rsr);
176
177 dev_info(dev, "Last boot was %scaused by watchdog\n",
178 status ? "" : "not ");
179 }
180
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200181 spin_lock_init(&ddata->lock);
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200182
183 ddata->wdd.info = &mpc8xxx_wdt_info,
184 ddata->wdd.ops = &mpc8xxx_wdt_ops,
185
Christophe Leroy19ce9492017-11-08 15:39:44 +0100186 ddata->wdd.timeout = WATCHDOG_TIMEOUT;
Christophe Leroy79b10e02018-09-17 06:22:47 +0000187 watchdog_init_timeout(&ddata->wdd, timeout, dev);
Uwe Kleine-König50ffb532015-08-12 10:15:53 +0200188
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200189 watchdog_set_nowayout(&ddata->wdd, nowayout);
Uwe Kleine-König50ffb532015-08-12 10:15:53 +0200190
Christophe Leroy19ce9492017-11-08 15:39:44 +0100191 ddata->swtc = min(ddata->wdd.timeout * freq / wdt_type->prescaler,
192 0xffffU);
Anton Vorontsov500c9192008-07-03 23:51:34 -0700193
194 /*
195 * If the watchdog was previously enabled or we're running on
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700196 * MPC8xxx, we should ping the wdt from the kernel until the
Anton Vorontsov500c9192008-07-03 23:51:34 -0700197 * userspace handles it.
198 */
199 if (enabled)
Christophe Leroy19ce9492017-11-08 15:39:44 +0100200 mpc8xxx_wdt_start(&ddata->wdd);
201
202 ddata->wdd.max_hw_heartbeat_ms = (ddata->swtc * wdt_type->prescaler) /
203 (freq / 1000);
204 ddata->wdd.min_timeout = ddata->wdd.max_hw_heartbeat_ms / 1000;
205 if (ddata->wdd.timeout < ddata->wdd.min_timeout)
206 ddata->wdd.timeout = ddata->wdd.min_timeout;
207
208 ret = watchdog_register_device(&ddata->wdd);
209 if (ret) {
Christophe Leroy79b10e02018-09-17 06:22:47 +0000210 dev_err(dev, "cannot register watchdog device (err=%d)\n", ret);
Christophe Leroy19ce9492017-11-08 15:39:44 +0100211 return ret;
212 }
213
Christophe Leroy79b10e02018-09-17 06:22:47 +0000214 dev_info(dev,
215 "WDT driver for MPC8xxx initialized. mode:%s timeout=%d sec\n",
216 reset ? "reset" : "interrupt", ddata->wdd.timeout);
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200217
218 platform_set_drvdata(ofdev, ddata);
Kumar Galafabbfb92006-01-14 13:20:50 -0800219 return 0;
Kumar Galafabbfb92006-01-14 13:20:50 -0800220}
221
Bill Pemberton4b12b892012-11-19 13:26:24 -0500222static int mpc8xxx_wdt_remove(struct platform_device *ofdev)
Kumar Galafabbfb92006-01-14 13:20:50 -0800223{
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200224 struct mpc8xxx_wdt_ddata *ddata = platform_get_drvdata(ofdev);
225
Christophe Leroy79b10e02018-09-17 06:22:47 +0000226 dev_crit(&ofdev->dev, "Watchdog removed, expect the %s soon!\n",
227 reset ? "reset" : "machine check exception");
Uwe Kleine-König7997eba2015-08-12 10:15:56 +0200228 watchdog_unregister_device(&ddata->wdd);
Kumar Galafabbfb92006-01-14 13:20:50 -0800229
230 return 0;
231}
232
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700233static const struct of_device_id mpc8xxx_wdt_match[] = {
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700234 {
235 .compatible = "mpc83xx_wdt",
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700236 .data = &(struct mpc8xxx_wdt_type) {
Anton Vorontsov500c9192008-07-03 23:51:34 -0700237 .prescaler = 0x10000,
Christophe Leroy38e48b72018-09-17 06:22:50 +0000238 .rsr_mask = BIT(3), /* RSR Bit SWRS */
Anton Vorontsov500c9192008-07-03 23:51:34 -0700239 },
240 },
241 {
242 .compatible = "fsl,mpc8610-wdt",
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700243 .data = &(struct mpc8xxx_wdt_type) {
Anton Vorontsov500c9192008-07-03 23:51:34 -0700244 .prescaler = 0x10000,
245 .hw_enabled = true,
Christophe Leroy38e48b72018-09-17 06:22:50 +0000246 .rsr_mask = BIT(20), /* RSTRSCR Bit WDT_RR */
Anton Vorontsov500c9192008-07-03 23:51:34 -0700247 },
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700248 },
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700249 {
250 .compatible = "fsl,mpc823-wdt",
251 .data = &(struct mpc8xxx_wdt_type) {
252 .prescaler = 0x800,
Christophe Leroy4af897f2013-11-30 16:45:40 +0100253 .hw_enabled = true,
Christophe Leroy38e48b72018-09-17 06:22:50 +0000254 .rsr_mask = BIT(28), /* RSR Bit SWRS */
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700255 },
256 },
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700257 {},
258};
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700259MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
Anton Vorontsovef8ab122008-07-03 23:51:32 -0700260
Grant Likely1c48a5c2011-02-17 02:43:24 -0700261static struct platform_driver mpc8xxx_wdt_driver = {
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700262 .probe = mpc8xxx_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500263 .remove = mpc8xxx_wdt_remove,
Grant Likely40182942010-04-13 16:13:02 -0700264 .driver = {
265 .name = "mpc8xxx_wdt",
Grant Likely40182942010-04-13 16:13:02 -0700266 .of_match_table = mpc8xxx_wdt_match,
Kumar Galafabbfb92006-01-14 13:20:50 -0800267 },
268};
269
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700270static int __init mpc8xxx_wdt_init(void)
Kumar Galafabbfb92006-01-14 13:20:50 -0800271{
Grant Likely1c48a5c2011-02-17 02:43:24 -0700272 return platform_driver_register(&mpc8xxx_wdt_driver);
Kumar Galafabbfb92006-01-14 13:20:50 -0800273}
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700274arch_initcall(mpc8xxx_wdt_init);
Kumar Galafabbfb92006-01-14 13:20:50 -0800275
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700276static void __exit mpc8xxx_wdt_exit(void)
Kumar Galafabbfb92006-01-14 13:20:50 -0800277{
Grant Likely1c48a5c2011-02-17 02:43:24 -0700278 platform_driver_unregister(&mpc8xxx_wdt_driver);
Kumar Galafabbfb92006-01-14 13:20:50 -0800279}
Anton Vorontsov59ca1b02008-07-03 23:51:35 -0700280module_exit(mpc8xxx_wdt_exit);
Kumar Galafabbfb92006-01-14 13:20:50 -0800281
282MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
Anton Vorontsov0d7b1012008-07-03 23:51:36 -0700283MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
284 "uProcessors");
Kumar Galafabbfb92006-01-14 13:20:50 -0800285MODULE_LICENSE("GPL");