1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * mpc8xxx_wdt.c - MPC8xx/MPC83xx/MPC86xx watchdog userspace interface
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Authors: Dave Updegraff <dave@cray.org>
6*4882a593Smuzhiyun * Kumar Gala <galak@kernel.crashing.org>
7*4882a593Smuzhiyun * Attribution: from 83xx_wst: Florian Schirmer <jolt@tuxbox.org>
8*4882a593Smuzhiyun * ..and from sc520_wdt
9*4882a593Smuzhiyun * Copyright (c) 2008 MontaVista Software, Inc.
10*4882a593Smuzhiyun * Anton Vorontsov <avorontsov@ru.mvista.com>
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Note: it appears that you can only actually ENABLE or DISABLE the thing
13*4882a593Smuzhiyun * once after POR. Once enabled, you cannot disable, and vice versa.
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/fs.h>
17*4882a593Smuzhiyun #include <linux/init.h>
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/of_address.h>
20*4882a593Smuzhiyun #include <linux/of_platform.h>
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/watchdog.h>
23*4882a593Smuzhiyun #include <linux/io.h>
24*4882a593Smuzhiyun #include <linux/uaccess.h>
25*4882a593Smuzhiyun #include <sysdev/fsl_soc.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define WATCHDOG_TIMEOUT 10
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun struct mpc8xxx_wdt {
30*4882a593Smuzhiyun __be32 res0;
31*4882a593Smuzhiyun __be32 swcrr; /* System watchdog control register */
32*4882a593Smuzhiyun #define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
33*4882a593Smuzhiyun #define SWCRR_SWF 0x00000008 /* Software Watchdog Freeze (mpc8xx). */
34*4882a593Smuzhiyun #define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
35*4882a593Smuzhiyun #define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
36*4882a593Smuzhiyun #define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
37*4882a593Smuzhiyun __be32 swcnr; /* System watchdog count register */
38*4882a593Smuzhiyun u8 res1[2];
39*4882a593Smuzhiyun __be16 swsrr; /* System watchdog service register */
40*4882a593Smuzhiyun u8 res2[0xF0];
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun struct mpc8xxx_wdt_type {
44*4882a593Smuzhiyun int prescaler;
45*4882a593Smuzhiyun bool hw_enabled;
46*4882a593Smuzhiyun u32 rsr_mask;
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun struct mpc8xxx_wdt_ddata {
50*4882a593Smuzhiyun struct mpc8xxx_wdt __iomem *base;
51*4882a593Smuzhiyun struct watchdog_device wdd;
52*4882a593Smuzhiyun spinlock_t lock;
53*4882a593Smuzhiyun u16 swtc;
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun static u16 timeout;
57*4882a593Smuzhiyun module_param(timeout, ushort, 0);
58*4882a593Smuzhiyun MODULE_PARM_DESC(timeout,
59*4882a593Smuzhiyun "Watchdog timeout in seconds. (1<timeout<65535, default="
60*4882a593Smuzhiyun __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun static bool reset = 1;
63*4882a593Smuzhiyun module_param(reset, bool, 0);
64*4882a593Smuzhiyun MODULE_PARM_DESC(reset,
65*4882a593Smuzhiyun "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
68*4882a593Smuzhiyun module_param(nowayout, bool, 0);
69*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
70*4882a593Smuzhiyun "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
71*4882a593Smuzhiyun
mpc8xxx_wdt_keepalive(struct mpc8xxx_wdt_ddata * ddata)72*4882a593Smuzhiyun static void mpc8xxx_wdt_keepalive(struct mpc8xxx_wdt_ddata *ddata)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun /* Ping the WDT */
75*4882a593Smuzhiyun spin_lock(&ddata->lock);
76*4882a593Smuzhiyun out_be16(&ddata->base->swsrr, 0x556c);
77*4882a593Smuzhiyun out_be16(&ddata->base->swsrr, 0xaa39);
78*4882a593Smuzhiyun spin_unlock(&ddata->lock);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
mpc8xxx_wdt_start(struct watchdog_device * w)81*4882a593Smuzhiyun static int mpc8xxx_wdt_start(struct watchdog_device *w)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct mpc8xxx_wdt_ddata *ddata =
84*4882a593Smuzhiyun container_of(w, struct mpc8xxx_wdt_ddata, wdd);
85*4882a593Smuzhiyun u32 tmp = in_be32(&ddata->base->swcrr);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Good, fire up the show */
88*4882a593Smuzhiyun tmp &= ~(SWCRR_SWTC | SWCRR_SWF | SWCRR_SWEN | SWCRR_SWRI | SWCRR_SWPR);
89*4882a593Smuzhiyun tmp |= SWCRR_SWEN | SWCRR_SWPR | (ddata->swtc << 16);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun if (reset)
92*4882a593Smuzhiyun tmp |= SWCRR_SWRI;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun out_be32(&ddata->base->swcrr, tmp);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun tmp = in_be32(&ddata->base->swcrr);
97*4882a593Smuzhiyun if (!(tmp & SWCRR_SWEN))
98*4882a593Smuzhiyun return -EOPNOTSUPP;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun ddata->swtc = tmp >> 16;
101*4882a593Smuzhiyun set_bit(WDOG_HW_RUNNING, &ddata->wdd.status);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun return 0;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
mpc8xxx_wdt_ping(struct watchdog_device * w)106*4882a593Smuzhiyun static int mpc8xxx_wdt_ping(struct watchdog_device *w)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun struct mpc8xxx_wdt_ddata *ddata =
109*4882a593Smuzhiyun container_of(w, struct mpc8xxx_wdt_ddata, wdd);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun mpc8xxx_wdt_keepalive(ddata);
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun static struct watchdog_info mpc8xxx_wdt_info = {
116*4882a593Smuzhiyun .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT,
117*4882a593Smuzhiyun .firmware_version = 1,
118*4882a593Smuzhiyun .identity = "MPC8xxx",
119*4882a593Smuzhiyun };
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun static struct watchdog_ops mpc8xxx_wdt_ops = {
122*4882a593Smuzhiyun .owner = THIS_MODULE,
123*4882a593Smuzhiyun .start = mpc8xxx_wdt_start,
124*4882a593Smuzhiyun .ping = mpc8xxx_wdt_ping,
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun
mpc8xxx_wdt_probe(struct platform_device * ofdev)127*4882a593Smuzhiyun static int mpc8xxx_wdt_probe(struct platform_device *ofdev)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun int ret;
130*4882a593Smuzhiyun struct resource *res;
131*4882a593Smuzhiyun const struct mpc8xxx_wdt_type *wdt_type;
132*4882a593Smuzhiyun struct mpc8xxx_wdt_ddata *ddata;
133*4882a593Smuzhiyun u32 freq = fsl_get_sys_freq();
134*4882a593Smuzhiyun bool enabled;
135*4882a593Smuzhiyun struct device *dev = &ofdev->dev;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun wdt_type = of_device_get_match_data(dev);
138*4882a593Smuzhiyun if (!wdt_type)
139*4882a593Smuzhiyun return -EINVAL;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun if (!freq || freq == -1)
142*4882a593Smuzhiyun return -EINVAL;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
145*4882a593Smuzhiyun if (!ddata)
146*4882a593Smuzhiyun return -ENOMEM;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun ddata->base = devm_platform_ioremap_resource(ofdev, 0);
149*4882a593Smuzhiyun if (IS_ERR(ddata->base))
150*4882a593Smuzhiyun return PTR_ERR(ddata->base);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun enabled = in_be32(&ddata->base->swcrr) & SWCRR_SWEN;
153*4882a593Smuzhiyun if (!enabled && wdt_type->hw_enabled) {
154*4882a593Smuzhiyun dev_info(dev, "could not be enabled in software\n");
155*4882a593Smuzhiyun return -ENODEV;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun res = platform_get_resource(ofdev, IORESOURCE_MEM, 1);
159*4882a593Smuzhiyun if (res) {
160*4882a593Smuzhiyun bool status;
161*4882a593Smuzhiyun u32 __iomem *rsr = ioremap(res->start, resource_size(res));
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (!rsr)
164*4882a593Smuzhiyun return -ENOMEM;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun status = in_be32(rsr) & wdt_type->rsr_mask;
167*4882a593Smuzhiyun ddata->wdd.bootstatus = status ? WDIOF_CARDRESET : 0;
168*4882a593Smuzhiyun /* clear reset status bits related to watchdog timer */
169*4882a593Smuzhiyun out_be32(rsr, wdt_type->rsr_mask);
170*4882a593Smuzhiyun iounmap(rsr);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun dev_info(dev, "Last boot was %scaused by watchdog\n",
173*4882a593Smuzhiyun status ? "" : "not ");
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun spin_lock_init(&ddata->lock);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun ddata->wdd.info = &mpc8xxx_wdt_info,
179*4882a593Smuzhiyun ddata->wdd.ops = &mpc8xxx_wdt_ops,
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun ddata->wdd.timeout = WATCHDOG_TIMEOUT;
182*4882a593Smuzhiyun watchdog_init_timeout(&ddata->wdd, timeout, dev);
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun watchdog_set_nowayout(&ddata->wdd, nowayout);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun ddata->swtc = min(ddata->wdd.timeout * freq / wdt_type->prescaler,
187*4882a593Smuzhiyun 0xffffU);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun * If the watchdog was previously enabled or we're running on
191*4882a593Smuzhiyun * MPC8xxx, we should ping the wdt from the kernel until the
192*4882a593Smuzhiyun * userspace handles it.
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun if (enabled)
195*4882a593Smuzhiyun mpc8xxx_wdt_start(&ddata->wdd);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun ddata->wdd.max_hw_heartbeat_ms = (ddata->swtc * wdt_type->prescaler) /
198*4882a593Smuzhiyun (freq / 1000);
199*4882a593Smuzhiyun ddata->wdd.min_timeout = ddata->wdd.max_hw_heartbeat_ms / 1000;
200*4882a593Smuzhiyun if (ddata->wdd.timeout < ddata->wdd.min_timeout)
201*4882a593Smuzhiyun ddata->wdd.timeout = ddata->wdd.min_timeout;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun ret = devm_watchdog_register_device(dev, &ddata->wdd);
204*4882a593Smuzhiyun if (ret)
205*4882a593Smuzhiyun return ret;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun dev_info(dev,
208*4882a593Smuzhiyun "WDT driver for MPC8xxx initialized. mode:%s timeout=%d sec\n",
209*4882a593Smuzhiyun reset ? "reset" : "interrupt", ddata->wdd.timeout);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun platform_set_drvdata(ofdev, ddata);
212*4882a593Smuzhiyun return 0;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun static const struct of_device_id mpc8xxx_wdt_match[] = {
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun .compatible = "mpc83xx_wdt",
218*4882a593Smuzhiyun .data = &(struct mpc8xxx_wdt_type) {
219*4882a593Smuzhiyun .prescaler = 0x10000,
220*4882a593Smuzhiyun .rsr_mask = BIT(3), /* RSR Bit SWRS */
221*4882a593Smuzhiyun },
222*4882a593Smuzhiyun },
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun .compatible = "fsl,mpc8610-wdt",
225*4882a593Smuzhiyun .data = &(struct mpc8xxx_wdt_type) {
226*4882a593Smuzhiyun .prescaler = 0x10000,
227*4882a593Smuzhiyun .hw_enabled = true,
228*4882a593Smuzhiyun .rsr_mask = BIT(20), /* RSTRSCR Bit WDT_RR */
229*4882a593Smuzhiyun },
230*4882a593Smuzhiyun },
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun .compatible = "fsl,mpc823-wdt",
233*4882a593Smuzhiyun .data = &(struct mpc8xxx_wdt_type) {
234*4882a593Smuzhiyun .prescaler = 0x800,
235*4882a593Smuzhiyun .hw_enabled = true,
236*4882a593Smuzhiyun .rsr_mask = BIT(28), /* RSR Bit SWRS */
237*4882a593Smuzhiyun },
238*4882a593Smuzhiyun },
239*4882a593Smuzhiyun {},
240*4882a593Smuzhiyun };
241*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, mpc8xxx_wdt_match);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun static struct platform_driver mpc8xxx_wdt_driver = {
244*4882a593Smuzhiyun .probe = mpc8xxx_wdt_probe,
245*4882a593Smuzhiyun .driver = {
246*4882a593Smuzhiyun .name = "mpc8xxx_wdt",
247*4882a593Smuzhiyun .of_match_table = mpc8xxx_wdt_match,
248*4882a593Smuzhiyun },
249*4882a593Smuzhiyun };
250*4882a593Smuzhiyun
mpc8xxx_wdt_init(void)251*4882a593Smuzhiyun static int __init mpc8xxx_wdt_init(void)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun return platform_driver_register(&mpc8xxx_wdt_driver);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun arch_initcall(mpc8xxx_wdt_init);
256*4882a593Smuzhiyun
mpc8xxx_wdt_exit(void)257*4882a593Smuzhiyun static void __exit mpc8xxx_wdt_exit(void)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun platform_driver_unregister(&mpc8xxx_wdt_driver);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun module_exit(mpc8xxx_wdt_exit);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
264*4882a593Smuzhiyun MODULE_DESCRIPTION("Driver for watchdog timer in MPC8xx/MPC83xx/MPC86xx "
265*4882a593Smuzhiyun "uProcessors");
266*4882a593Smuzhiyun MODULE_LICENSE("GPL");
267