xref: /openbmc/linux/drivers/watchdog/cpwd.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28ab0dc33SDavid S. Miller /* cpwd.c - driver implementation for hardware watchdog
38ab0dc33SDavid S. Miller  * timers found on Sun Microsystems CP1400 and CP1500 boards.
48ab0dc33SDavid S. Miller  *
58ab0dc33SDavid S. Miller  * This device supports both the generic Linux watchdog
68ab0dc33SDavid S. Miller  * interface and Solaris-compatible ioctls as best it is
78ab0dc33SDavid S. Miller  * able.
88ab0dc33SDavid S. Miller  *
98ab0dc33SDavid S. Miller  * NOTE:	CP1400 systems appear to have a defective intr_mask
108ab0dc33SDavid S. Miller  *			register on the PLD, preventing the disabling of
118ab0dc33SDavid S. Miller  *			timer interrupts.  We use a timer to periodically
128ab0dc33SDavid S. Miller  *			reset 'stopped' watchdogs on affected platforms.
138ab0dc33SDavid S. Miller  *
148ab0dc33SDavid S. Miller  * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
158ab0dc33SDavid S. Miller  * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
168ab0dc33SDavid S. Miller  */
178ab0dc33SDavid S. Miller 
1827c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1927c766aaSJoe Perches 
208ab0dc33SDavid S. Miller #include <linux/kernel.h>
218ab0dc33SDavid S. Miller #include <linux/module.h>
228ab0dc33SDavid S. Miller #include <linux/fs.h>
238ab0dc33SDavid S. Miller #include <linux/errno.h>
248ab0dc33SDavid S. Miller #include <linux/major.h>
258ab0dc33SDavid S. Miller #include <linux/miscdevice.h>
268ab0dc33SDavid S. Miller #include <linux/interrupt.h>
278ab0dc33SDavid S. Miller #include <linux/ioport.h>
288ab0dc33SDavid S. Miller #include <linux/timer.h>
29c58e8134SArnd Bergmann #include <linux/compat.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31613655faSArnd Bergmann #include <linux/mutex.h>
328ab0dc33SDavid S. Miller #include <linux/io.h>
338ab0dc33SDavid S. Miller #include <linux/of.h>
34*cc85f87aSRob Herring #include <linux/platform_device.h>
35278aefc5SWim Van Sebroeck #include <linux/uaccess.h>
368ab0dc33SDavid S. Miller 
378ab0dc33SDavid S. Miller #include <asm/irq.h>
388ab0dc33SDavid S. Miller #include <asm/watchdog.h>
398ab0dc33SDavid S. Miller 
408ab0dc33SDavid S. Miller #define DRIVER_NAME	"cpwd"
418ab0dc33SDavid S. Miller 
428ab0dc33SDavid S. Miller #define WD_OBPNAME	"watchdog"
438ab0dc33SDavid S. Miller #define WD_BADMODEL	"SUNW,501-5336"
448ab0dc33SDavid S. Miller #define WD_BTIMEOUT	(jiffies + (HZ * 1000))
458ab0dc33SDavid S. Miller #define WD_BLIMIT	0xFFFF
468ab0dc33SDavid S. Miller 
478ab0dc33SDavid S. Miller #define WD0_MINOR	212
488ab0dc33SDavid S. Miller #define WD1_MINOR	213
498ab0dc33SDavid S. Miller #define WD2_MINOR	214
508ab0dc33SDavid S. Miller 
518ab0dc33SDavid S. Miller /* Internal driver definitions.  */
528ab0dc33SDavid S. Miller #define WD0_ID			0
538ab0dc33SDavid S. Miller #define WD1_ID			1
548ab0dc33SDavid S. Miller #define WD2_ID			2
558ab0dc33SDavid S. Miller #define WD_NUMDEVS		3
568ab0dc33SDavid S. Miller 
578ab0dc33SDavid S. Miller #define WD_INTR_OFF		0
588ab0dc33SDavid S. Miller #define WD_INTR_ON		1
598ab0dc33SDavid S. Miller 
608ab0dc33SDavid S. Miller #define WD_STAT_INIT	0x01	/* Watchdog timer is initialized	*/
618ab0dc33SDavid S. Miller #define WD_STAT_BSTOP	0x02	/* Watchdog timer is brokenstopped	*/
628ab0dc33SDavid S. Miller #define WD_STAT_SVCD	0x04	/* Watchdog interrupt occurred		*/
638ab0dc33SDavid S. Miller 
648ab0dc33SDavid S. Miller /* Register value definitions
658ab0dc33SDavid S. Miller  */
668ab0dc33SDavid S. Miller #define WD0_INTR_MASK	0x01	/* Watchdog device interrupt masks	*/
678ab0dc33SDavid S. Miller #define WD1_INTR_MASK	0x02
688ab0dc33SDavid S. Miller #define WD2_INTR_MASK	0x04
698ab0dc33SDavid S. Miller 
708ab0dc33SDavid S. Miller #define WD_S_RUNNING	0x01	/* Watchdog device status running	*/
718ab0dc33SDavid S. Miller #define WD_S_EXPIRED	0x02	/* Watchdog device status expired	*/
728ab0dc33SDavid S. Miller 
738ab0dc33SDavid S. Miller struct cpwd {
748ab0dc33SDavid S. Miller 	void __iomem	*regs;
758ab0dc33SDavid S. Miller 	spinlock_t	lock;
768ab0dc33SDavid S. Miller 
778ab0dc33SDavid S. Miller 	unsigned int	irq;
788ab0dc33SDavid S. Miller 
798ab0dc33SDavid S. Miller 	unsigned long	timeout;
808ab0dc33SDavid S. Miller 	bool		enabled;
818ab0dc33SDavid S. Miller 	bool		reboot;
828ab0dc33SDavid S. Miller 	bool		broken;
838ab0dc33SDavid S. Miller 	bool		initialized;
848ab0dc33SDavid S. Miller 
858ab0dc33SDavid S. Miller 	struct {
868ab0dc33SDavid S. Miller 		struct miscdevice	misc;
878ab0dc33SDavid S. Miller 		void __iomem		*regs;
888ab0dc33SDavid S. Miller 		u8			intr_mask;
898ab0dc33SDavid S. Miller 		u8			runstatus;
908ab0dc33SDavid S. Miller 		u16			timeout;
918ab0dc33SDavid S. Miller 	} devs[WD_NUMDEVS];
928ab0dc33SDavid S. Miller };
938ab0dc33SDavid S. Miller 
94613655faSArnd Bergmann static DEFINE_MUTEX(cpwd_mutex);
958ab0dc33SDavid S. Miller static struct cpwd *cpwd_device;
968ab0dc33SDavid S. Miller 
978ab0dc33SDavid S. Miller /* Sun uses Altera PLD EPF8820ATC144-4
988ab0dc33SDavid S. Miller  * providing three hardware watchdogs:
998ab0dc33SDavid S. Miller  *
1008ab0dc33SDavid S. Miller  * 1) RIC - sends an interrupt when triggered
1018ab0dc33SDavid S. Miller  * 2) XIR - asserts XIR_B_RESET when triggered, resets CPU
1028ab0dc33SDavid S. Miller  * 3) POR - asserts POR_B_RESET when triggered, resets CPU, backplane, board
1038ab0dc33SDavid S. Miller  *
1048ab0dc33SDavid S. Miller  *** Timer register block definition (struct wd_timer_regblk)
1058ab0dc33SDavid S. Miller  *
1068ab0dc33SDavid S. Miller  * dcntr and limit registers (halfword access):
1078ab0dc33SDavid S. Miller  * -------------------
1088ab0dc33SDavid S. Miller  * | 15 | ...| 1 | 0 |
1098ab0dc33SDavid S. Miller  * -------------------
1108ab0dc33SDavid S. Miller  * |-  counter val  -|
1118ab0dc33SDavid S. Miller  * -------------------
1128ab0dc33SDavid S. Miller  * dcntr -	Current 16-bit downcounter value.
1138ab0dc33SDavid S. Miller  *			When downcounter reaches '0' watchdog expires.
114927d6961SWim Van Sebroeck  *			Reading this register resets downcounter with
115927d6961SWim Van Sebroeck  *			'limit' value.
1168ab0dc33SDavid S. Miller  * limit -	16-bit countdown value in 1/10th second increments.
1178ab0dc33SDavid S. Miller  *			Writing this register begins countdown with input value.
1188ab0dc33SDavid S. Miller  *			Reading from this register does not affect counter.
1198ab0dc33SDavid S. Miller  * NOTES:	After watchdog reset, dcntr and limit contain '1'
1208ab0dc33SDavid S. Miller  *
1218ab0dc33SDavid S. Miller  * status register (byte access):
1228ab0dc33SDavid S. Miller  * ---------------------------
1238ab0dc33SDavid S. Miller  * | 7 | ... | 2 |  1  |  0  |
1248ab0dc33SDavid S. Miller  * --------------+------------
1258ab0dc33SDavid S. Miller  * |-   UNUSED  -| EXP | RUN |
1268ab0dc33SDavid S. Miller  * ---------------------------
1278ab0dc33SDavid S. Miller  * status-	Bit 0 - Watchdog is running
1288ab0dc33SDavid S. Miller  *			Bit 1 - Watchdog has expired
1298ab0dc33SDavid S. Miller  *
1308ab0dc33SDavid S. Miller  *** PLD register block definition (struct wd_pld_regblk)
1318ab0dc33SDavid S. Miller  *
1328ab0dc33SDavid S. Miller  * intr_mask register (byte access):
1338ab0dc33SDavid S. Miller  * ---------------------------------
1348ab0dc33SDavid S. Miller  * | 7 | ... | 3 |  2  |  1  |  0  |
1358ab0dc33SDavid S. Miller  * +-------------+------------------
1368ab0dc33SDavid S. Miller  * |-   UNUSED  -| WD3 | WD2 | WD1 |
1378ab0dc33SDavid S. Miller  * ---------------------------------
1388ab0dc33SDavid S. Miller  * WD3 -  1 == Interrupt disabled for watchdog 3
1398ab0dc33SDavid S. Miller  * WD2 -  1 == Interrupt disabled for watchdog 2
1408ab0dc33SDavid S. Miller  * WD1 -  1 == Interrupt disabled for watchdog 1
1418ab0dc33SDavid S. Miller  *
1428ab0dc33SDavid S. Miller  * pld_status register (byte access):
1438ab0dc33SDavid S. Miller  * UNKNOWN, MAGICAL MYSTERY REGISTER
1448ab0dc33SDavid S. Miller  *
1458ab0dc33SDavid S. Miller  */
1468ab0dc33SDavid S. Miller #define WD_TIMER_REGSZ	16
1478ab0dc33SDavid S. Miller #define WD0_OFF		0
1488ab0dc33SDavid S. Miller #define WD1_OFF		(WD_TIMER_REGSZ * 1)
1498ab0dc33SDavid S. Miller #define WD2_OFF		(WD_TIMER_REGSZ * 2)
1508ab0dc33SDavid S. Miller #define PLD_OFF		(WD_TIMER_REGSZ * 3)
1518ab0dc33SDavid S. Miller 
1528ab0dc33SDavid S. Miller #define WD_DCNTR	0x00
1538ab0dc33SDavid S. Miller #define WD_LIMIT	0x04
1548ab0dc33SDavid S. Miller #define WD_STATUS	0x08
1558ab0dc33SDavid S. Miller 
1568ab0dc33SDavid S. Miller #define PLD_IMASK	(PLD_OFF + 0x00)
1578ab0dc33SDavid S. Miller #define PLD_STATUS	(PLD_OFF + 0x04)
1588ab0dc33SDavid S. Miller 
1598ab0dc33SDavid S. Miller static struct timer_list cpwd_timer;
1608ab0dc33SDavid S. Miller 
161a77dba7eSWim Van Sebroeck static int wd0_timeout;
162a77dba7eSWim Van Sebroeck static int wd1_timeout;
163a77dba7eSWim Van Sebroeck static int wd2_timeout;
1648ab0dc33SDavid S. Miller 
1658ab0dc33SDavid S. Miller module_param(wd0_timeout, int, 0);
1668ab0dc33SDavid S. Miller MODULE_PARM_DESC(wd0_timeout, "Default watchdog0 timeout in 1/10secs");
1678ab0dc33SDavid S. Miller module_param(wd1_timeout, int, 0);
1688ab0dc33SDavid S. Miller MODULE_PARM_DESC(wd1_timeout, "Default watchdog1 timeout in 1/10secs");
1698ab0dc33SDavid S. Miller module_param(wd2_timeout, int, 0);
1708ab0dc33SDavid S. Miller MODULE_PARM_DESC(wd2_timeout, "Default watchdog2 timeout in 1/10secs");
1718ab0dc33SDavid S. Miller 
1728ab0dc33SDavid S. Miller MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
1738ab0dc33SDavid S. Miller MODULE_DESCRIPTION("Hardware watchdog driver for Sun Microsystems CP1400/1500");
1748ab0dc33SDavid S. Miller MODULE_LICENSE("GPL");
1758ab0dc33SDavid S. Miller 
cpwd_writew(u16 val,void __iomem * addr)1768ab0dc33SDavid S. Miller static void cpwd_writew(u16 val, void __iomem *addr)
1778ab0dc33SDavid S. Miller {
1788ab0dc33SDavid S. Miller 	writew(cpu_to_le16(val), addr);
1798ab0dc33SDavid S. Miller }
cpwd_readw(void __iomem * addr)1808ab0dc33SDavid S. Miller static u16 cpwd_readw(void __iomem *addr)
1818ab0dc33SDavid S. Miller {
1828ab0dc33SDavid S. Miller 	u16 val = readw(addr);
1838ab0dc33SDavid S. Miller 
1848ab0dc33SDavid S. Miller 	return le16_to_cpu(val);
1858ab0dc33SDavid S. Miller }
1868ab0dc33SDavid S. Miller 
cpwd_writeb(u8 val,void __iomem * addr)1878ab0dc33SDavid S. Miller static void cpwd_writeb(u8 val, void __iomem *addr)
1888ab0dc33SDavid S. Miller {
1898ab0dc33SDavid S. Miller 	writeb(val, addr);
1908ab0dc33SDavid S. Miller }
1918ab0dc33SDavid S. Miller 
cpwd_readb(void __iomem * addr)1928ab0dc33SDavid S. Miller static u8 cpwd_readb(void __iomem *addr)
1938ab0dc33SDavid S. Miller {
1948ab0dc33SDavid S. Miller 	return readb(addr);
1958ab0dc33SDavid S. Miller }
1968ab0dc33SDavid S. Miller 
1978ab0dc33SDavid S. Miller /* Enable or disable watchdog interrupts
1988ab0dc33SDavid S. Miller  * Because of the CP1400 defect this should only be
1998ab0dc33SDavid S. Miller  * called during initialzation or by wd_[start|stop]timer()
2008ab0dc33SDavid S. Miller  *
2018ab0dc33SDavid S. Miller  * index	- sub-device index, or -1 for 'all'
2028ab0dc33SDavid S. Miller  * enable	- non-zero to enable interrupts, zero to disable
2038ab0dc33SDavid S. Miller  */
cpwd_toggleintr(struct cpwd * p,int index,int enable)2048ab0dc33SDavid S. Miller static void cpwd_toggleintr(struct cpwd *p, int index, int enable)
2058ab0dc33SDavid S. Miller {
2068ab0dc33SDavid S. Miller 	unsigned char curregs = cpwd_readb(p->regs + PLD_IMASK);
2078ab0dc33SDavid S. Miller 	unsigned char setregs =
2088ab0dc33SDavid S. Miller 		(index == -1) ?
2098ab0dc33SDavid S. Miller 		(WD0_INTR_MASK | WD1_INTR_MASK | WD2_INTR_MASK) :
2108ab0dc33SDavid S. Miller 		(p->devs[index].intr_mask);
2118ab0dc33SDavid S. Miller 
2128ab0dc33SDavid S. Miller 	if (enable == WD_INTR_ON)
2138ab0dc33SDavid S. Miller 		curregs &= ~setregs;
2148ab0dc33SDavid S. Miller 	else
2158ab0dc33SDavid S. Miller 		curregs |= setregs;
2168ab0dc33SDavid S. Miller 
2178ab0dc33SDavid S. Miller 	cpwd_writeb(curregs, p->regs + PLD_IMASK);
2188ab0dc33SDavid S. Miller }
2198ab0dc33SDavid S. Miller 
2208ab0dc33SDavid S. Miller /* Restarts timer with maximum limit value and
2218ab0dc33SDavid S. Miller  * does not unset 'brokenstop' value.
2228ab0dc33SDavid S. Miller  */
cpwd_resetbrokentimer(struct cpwd * p,int index)2238ab0dc33SDavid S. Miller static void cpwd_resetbrokentimer(struct cpwd *p, int index)
2248ab0dc33SDavid S. Miller {
2258ab0dc33SDavid S. Miller 	cpwd_toggleintr(p, index, WD_INTR_ON);
2268ab0dc33SDavid S. Miller 	cpwd_writew(WD_BLIMIT, p->devs[index].regs + WD_LIMIT);
2278ab0dc33SDavid S. Miller }
2288ab0dc33SDavid S. Miller 
2298ab0dc33SDavid S. Miller /* Timer method called to reset stopped watchdogs--
2308ab0dc33SDavid S. Miller  * because of the PLD bug on CP1400, we cannot mask
2318ab0dc33SDavid S. Miller  * interrupts within the PLD so me must continually
2328ab0dc33SDavid S. Miller  * reset the timers ad infinitum.
2338ab0dc33SDavid S. Miller  */
cpwd_brokentimer(struct timer_list * unused)2344fa42b4eSKees Cook static void cpwd_brokentimer(struct timer_list *unused)
2358ab0dc33SDavid S. Miller {
2364fa42b4eSKees Cook 	struct cpwd *p = cpwd_device;
2378ab0dc33SDavid S. Miller 	int id, tripped = 0;
2388ab0dc33SDavid S. Miller 
2398ab0dc33SDavid S. Miller 	/* kill a running timer instance, in case we
2408ab0dc33SDavid S. Miller 	 * were called directly instead of by kernel timer
2418ab0dc33SDavid S. Miller 	 */
2428ab0dc33SDavid S. Miller 	if (timer_pending(&cpwd_timer))
2438ab0dc33SDavid S. Miller 		del_timer(&cpwd_timer);
2448ab0dc33SDavid S. Miller 
2458ab0dc33SDavid S. Miller 	for (id = 0; id < WD_NUMDEVS; id++) {
2468ab0dc33SDavid S. Miller 		if (p->devs[id].runstatus & WD_STAT_BSTOP) {
2478ab0dc33SDavid S. Miller 			++tripped;
2488ab0dc33SDavid S. Miller 			cpwd_resetbrokentimer(p, id);
2498ab0dc33SDavid S. Miller 		}
2508ab0dc33SDavid S. Miller 	}
2518ab0dc33SDavid S. Miller 
2528ab0dc33SDavid S. Miller 	if (tripped) {
2538ab0dc33SDavid S. Miller 		/* there is at least one timer brokenstopped-- reschedule */
2548ab0dc33SDavid S. Miller 		cpwd_timer.expires = WD_BTIMEOUT;
2558ab0dc33SDavid S. Miller 		add_timer(&cpwd_timer);
2568ab0dc33SDavid S. Miller 	}
2578ab0dc33SDavid S. Miller }
2588ab0dc33SDavid S. Miller 
2598ab0dc33SDavid S. Miller /* Reset countdown timer with 'limit' value and continue countdown.
2608ab0dc33SDavid S. Miller  * This will not start a stopped timer.
2618ab0dc33SDavid S. Miller  */
cpwd_pingtimer(struct cpwd * p,int index)2628ab0dc33SDavid S. Miller static void cpwd_pingtimer(struct cpwd *p, int index)
2638ab0dc33SDavid S. Miller {
2648ab0dc33SDavid S. Miller 	if (cpwd_readb(p->devs[index].regs + WD_STATUS) & WD_S_RUNNING)
2658ab0dc33SDavid S. Miller 		cpwd_readw(p->devs[index].regs + WD_DCNTR);
2668ab0dc33SDavid S. Miller }
2678ab0dc33SDavid S. Miller 
2688ab0dc33SDavid S. Miller /* Stop a running watchdog timer-- the timer actually keeps
2698ab0dc33SDavid S. Miller  * running, but the interrupt is masked so that no action is
2708ab0dc33SDavid S. Miller  * taken upon expiration.
2718ab0dc33SDavid S. Miller  */
cpwd_stoptimer(struct cpwd * p,int index)2728ab0dc33SDavid S. Miller static void cpwd_stoptimer(struct cpwd *p, int index)
2738ab0dc33SDavid S. Miller {
2748ab0dc33SDavid S. Miller 	if (cpwd_readb(p->devs[index].regs + WD_STATUS) & WD_S_RUNNING) {
2758ab0dc33SDavid S. Miller 		cpwd_toggleintr(p, index, WD_INTR_OFF);
2768ab0dc33SDavid S. Miller 
2778ab0dc33SDavid S. Miller 		if (p->broken) {
2788ab0dc33SDavid S. Miller 			p->devs[index].runstatus |= WD_STAT_BSTOP;
2794fa42b4eSKees Cook 			cpwd_brokentimer(NULL);
2808ab0dc33SDavid S. Miller 		}
2818ab0dc33SDavid S. Miller 	}
2828ab0dc33SDavid S. Miller }
2838ab0dc33SDavid S. Miller 
2848ab0dc33SDavid S. Miller /* Start a watchdog timer with the specified limit value
2858ab0dc33SDavid S. Miller  * If the watchdog is running, it will be restarted with
2868ab0dc33SDavid S. Miller  * the provided limit value.
2878ab0dc33SDavid S. Miller  *
2888ab0dc33SDavid S. Miller  * This function will enable interrupts on the specified
2898ab0dc33SDavid S. Miller  * watchdog.
2908ab0dc33SDavid S. Miller  */
cpwd_starttimer(struct cpwd * p,int index)2918ab0dc33SDavid S. Miller static void cpwd_starttimer(struct cpwd *p, int index)
2928ab0dc33SDavid S. Miller {
2938ab0dc33SDavid S. Miller 	if (p->broken)
2948ab0dc33SDavid S. Miller 		p->devs[index].runstatus &= ~WD_STAT_BSTOP;
2958ab0dc33SDavid S. Miller 
2968ab0dc33SDavid S. Miller 	p->devs[index].runstatus &= ~WD_STAT_SVCD;
2978ab0dc33SDavid S. Miller 
2988ab0dc33SDavid S. Miller 	cpwd_writew(p->devs[index].timeout, p->devs[index].regs + WD_LIMIT);
2998ab0dc33SDavid S. Miller 	cpwd_toggleintr(p, index, WD_INTR_ON);
3008ab0dc33SDavid S. Miller }
3018ab0dc33SDavid S. Miller 
cpwd_getstatus(struct cpwd * p,int index)3028ab0dc33SDavid S. Miller static int cpwd_getstatus(struct cpwd *p, int index)
3038ab0dc33SDavid S. Miller {
3048ab0dc33SDavid S. Miller 	unsigned char stat = cpwd_readb(p->devs[index].regs + WD_STATUS);
3058ab0dc33SDavid S. Miller 	unsigned char intr = cpwd_readb(p->devs[index].regs + PLD_IMASK);
3068ab0dc33SDavid S. Miller 	unsigned char ret  = WD_STOPPED;
3078ab0dc33SDavid S. Miller 
3088ab0dc33SDavid S. Miller 	/* determine STOPPED */
3098ab0dc33SDavid S. Miller 	if (!stat)
3108ab0dc33SDavid S. Miller 		return ret;
3118ab0dc33SDavid S. Miller 
3128ab0dc33SDavid S. Miller 	/* determine EXPIRED vs FREERUN vs RUNNING */
3138ab0dc33SDavid S. Miller 	else if (WD_S_EXPIRED & stat) {
3148ab0dc33SDavid S. Miller 		ret = WD_EXPIRED;
3158ab0dc33SDavid S. Miller 	} else if (WD_S_RUNNING & stat) {
3168ab0dc33SDavid S. Miller 		if (intr & p->devs[index].intr_mask) {
3178ab0dc33SDavid S. Miller 			ret = WD_FREERUN;
3188ab0dc33SDavid S. Miller 		} else {
3198ab0dc33SDavid S. Miller 			/* Fudge WD_EXPIRED status for defective CP1400--
3208ab0dc33SDavid S. Miller 			 * IF timer is running
3218ab0dc33SDavid S. Miller 			 *	AND brokenstop is set
3228ab0dc33SDavid S. Miller 			 *	AND an interrupt has been serviced
3238ab0dc33SDavid S. Miller 			 * we are WD_EXPIRED.
3248ab0dc33SDavid S. Miller 			 *
3258ab0dc33SDavid S. Miller 			 * IF timer is running
3268ab0dc33SDavid S. Miller 			 *	AND brokenstop is set
3278ab0dc33SDavid S. Miller 			 *	AND no interrupt has been serviced
3288ab0dc33SDavid S. Miller 			 * we are WD_FREERUN.
3298ab0dc33SDavid S. Miller 			 */
3308ab0dc33SDavid S. Miller 			if (p->broken &&
3318ab0dc33SDavid S. Miller 			    (p->devs[index].runstatus & WD_STAT_BSTOP)) {
3328ab0dc33SDavid S. Miller 				if (p->devs[index].runstatus & WD_STAT_SVCD) {
3338ab0dc33SDavid S. Miller 					ret = WD_EXPIRED;
3348ab0dc33SDavid S. Miller 				} else {
335927d6961SWim Van Sebroeck 					/* we could as well pretend
336927d6961SWim Van Sebroeck 					 * we are expired */
3378ab0dc33SDavid S. Miller 					ret = WD_FREERUN;
3388ab0dc33SDavid S. Miller 				}
3398ab0dc33SDavid S. Miller 			} else {
3408ab0dc33SDavid S. Miller 				ret = WD_RUNNING;
3418ab0dc33SDavid S. Miller 			}
3428ab0dc33SDavid S. Miller 		}
3438ab0dc33SDavid S. Miller 	}
3448ab0dc33SDavid S. Miller 
3458ab0dc33SDavid S. Miller 	/* determine SERVICED */
3468ab0dc33SDavid S. Miller 	if (p->devs[index].runstatus & WD_STAT_SVCD)
3478ab0dc33SDavid S. Miller 		ret |= WD_SERVICED;
3488ab0dc33SDavid S. Miller 
349927d6961SWim Van Sebroeck 	return ret;
3508ab0dc33SDavid S. Miller }
3518ab0dc33SDavid S. Miller 
cpwd_interrupt(int irq,void * dev_id)3528ab0dc33SDavid S. Miller static irqreturn_t cpwd_interrupt(int irq, void *dev_id)
3538ab0dc33SDavid S. Miller {
3548ab0dc33SDavid S. Miller 	struct cpwd *p = dev_id;
3558ab0dc33SDavid S. Miller 
3568ab0dc33SDavid S. Miller 	/* Only WD0 will interrupt-- others are NMI and we won't
3578ab0dc33SDavid S. Miller 	 * see them here....
3588ab0dc33SDavid S. Miller 	 */
3598ab0dc33SDavid S. Miller 	spin_lock_irq(&p->lock);
3608ab0dc33SDavid S. Miller 
3618ab0dc33SDavid S. Miller 	cpwd_stoptimer(p, WD0_ID);
3628ab0dc33SDavid S. Miller 	p->devs[WD0_ID].runstatus |=  WD_STAT_SVCD;
3638ab0dc33SDavid S. Miller 
3648ab0dc33SDavid S. Miller 	spin_unlock_irq(&p->lock);
3658ab0dc33SDavid S. Miller 
3668ab0dc33SDavid S. Miller 	return IRQ_HANDLED;
3678ab0dc33SDavid S. Miller }
3688ab0dc33SDavid S. Miller 
cpwd_open(struct inode * inode,struct file * f)3698ab0dc33SDavid S. Miller static int cpwd_open(struct inode *inode, struct file *f)
3708ab0dc33SDavid S. Miller {
3718ab0dc33SDavid S. Miller 	struct cpwd *p = cpwd_device;
3728ab0dc33SDavid S. Miller 
373613655faSArnd Bergmann 	mutex_lock(&cpwd_mutex);
3748ab0dc33SDavid S. Miller 	switch (iminor(inode)) {
3758ab0dc33SDavid S. Miller 	case WD0_MINOR:
3768ab0dc33SDavid S. Miller 	case WD1_MINOR:
3778ab0dc33SDavid S. Miller 	case WD2_MINOR:
3788ab0dc33SDavid S. Miller 		break;
3798ab0dc33SDavid S. Miller 
3808ab0dc33SDavid S. Miller 	default:
381613655faSArnd Bergmann 		mutex_unlock(&cpwd_mutex);
3828ab0dc33SDavid S. Miller 		return -ENODEV;
3838ab0dc33SDavid S. Miller 	}
3848ab0dc33SDavid S. Miller 
3858ab0dc33SDavid S. Miller 	/* Register IRQ on first open of device */
3868ab0dc33SDavid S. Miller 	if (!p->initialized) {
3878ab0dc33SDavid S. Miller 		if (request_irq(p->irq, &cpwd_interrupt,
3888ab0dc33SDavid S. Miller 				IRQF_SHARED, DRIVER_NAME, p)) {
38927c766aaSJoe Perches 			pr_err("Cannot register IRQ %d\n", p->irq);
390613655faSArnd Bergmann 			mutex_unlock(&cpwd_mutex);
3918ab0dc33SDavid S. Miller 			return -EBUSY;
3928ab0dc33SDavid S. Miller 		}
3938ab0dc33SDavid S. Miller 		p->initialized = true;
3948ab0dc33SDavid S. Miller 	}
3958ab0dc33SDavid S. Miller 
396613655faSArnd Bergmann 	mutex_unlock(&cpwd_mutex);
3978ab0dc33SDavid S. Miller 
398c5bf68feSKirill Smelkov 	return stream_open(inode, f);
3998ab0dc33SDavid S. Miller }
4008ab0dc33SDavid S. Miller 
cpwd_release(struct inode * inode,struct file * file)4018ab0dc33SDavid S. Miller static int cpwd_release(struct inode *inode, struct file *file)
4028ab0dc33SDavid S. Miller {
4038ab0dc33SDavid S. Miller 	return 0;
4048ab0dc33SDavid S. Miller }
4058ab0dc33SDavid S. Miller 
cpwd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)4069626dd75SWim Van Sebroeck static long cpwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4078ab0dc33SDavid S. Miller {
40842747d71SWim Van Sebroeck 	static const struct watchdog_info info = {
4098ab0dc33SDavid S. Miller 		.options		= WDIOF_SETTIMEOUT,
4108ab0dc33SDavid S. Miller 		.firmware_version	= 1,
4118ab0dc33SDavid S. Miller 		.identity		= DRIVER_NAME,
4128ab0dc33SDavid S. Miller 	};
4138ab0dc33SDavid S. Miller 	void __user *argp = (void __user *)arg;
414496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
4158ab0dc33SDavid S. Miller 	int index = iminor(inode) - WD0_MINOR;
4168ab0dc33SDavid S. Miller 	struct cpwd *p = cpwd_device;
4178ab0dc33SDavid S. Miller 	int setopt = 0;
4188ab0dc33SDavid S. Miller 
4198ab0dc33SDavid S. Miller 	switch (cmd) {
4208ab0dc33SDavid S. Miller 	/* Generic Linux IOCTLs */
4218ab0dc33SDavid S. Miller 	case WDIOC_GETSUPPORT:
4228ab0dc33SDavid S. Miller 		if (copy_to_user(argp, &info, sizeof(struct watchdog_info)))
4238ab0dc33SDavid S. Miller 			return -EFAULT;
4248ab0dc33SDavid S. Miller 		break;
4258ab0dc33SDavid S. Miller 
4268ab0dc33SDavid S. Miller 	case WDIOC_GETSTATUS:
4278ab0dc33SDavid S. Miller 	case WDIOC_GETBOOTSTATUS:
4288ab0dc33SDavid S. Miller 		if (put_user(0, (int __user *)argp))
4298ab0dc33SDavid S. Miller 			return -EFAULT;
4308ab0dc33SDavid S. Miller 		break;
4318ab0dc33SDavid S. Miller 
4328ab0dc33SDavid S. Miller 	case WDIOC_KEEPALIVE:
4338ab0dc33SDavid S. Miller 		cpwd_pingtimer(p, index);
4348ab0dc33SDavid S. Miller 		break;
4358ab0dc33SDavid S. Miller 
4368ab0dc33SDavid S. Miller 	case WDIOC_SETOPTIONS:
4378ab0dc33SDavid S. Miller 		if (copy_from_user(&setopt, argp, sizeof(unsigned int)))
4388ab0dc33SDavid S. Miller 			return -EFAULT;
4398ab0dc33SDavid S. Miller 
4408ab0dc33SDavid S. Miller 		if (setopt & WDIOS_DISABLECARD) {
4418ab0dc33SDavid S. Miller 			if (p->enabled)
4428ab0dc33SDavid S. Miller 				return -EINVAL;
4438ab0dc33SDavid S. Miller 			cpwd_stoptimer(p, index);
4448ab0dc33SDavid S. Miller 		} else if (setopt & WDIOS_ENABLECARD) {
4458ab0dc33SDavid S. Miller 			cpwd_starttimer(p, index);
4468ab0dc33SDavid S. Miller 		} else {
4478ab0dc33SDavid S. Miller 			return -EINVAL;
4488ab0dc33SDavid S. Miller 		}
4498ab0dc33SDavid S. Miller 		break;
4508ab0dc33SDavid S. Miller 
4518ab0dc33SDavid S. Miller 	/* Solaris-compatible IOCTLs */
4528ab0dc33SDavid S. Miller 	case WIOCGSTAT:
4538ab0dc33SDavid S. Miller 		setopt = cpwd_getstatus(p, index);
4548ab0dc33SDavid S. Miller 		if (copy_to_user(argp, &setopt, sizeof(unsigned int)))
4558ab0dc33SDavid S. Miller 			return -EFAULT;
4568ab0dc33SDavid S. Miller 		break;
4578ab0dc33SDavid S. Miller 
4588ab0dc33SDavid S. Miller 	case WIOCSTART:
4598ab0dc33SDavid S. Miller 		cpwd_starttimer(p, index);
4608ab0dc33SDavid S. Miller 		break;
4618ab0dc33SDavid S. Miller 
4628ab0dc33SDavid S. Miller 	case WIOCSTOP:
4638ab0dc33SDavid S. Miller 		if (p->enabled)
464927d6961SWim Van Sebroeck 			return -EINVAL;
4658ab0dc33SDavid S. Miller 
4668ab0dc33SDavid S. Miller 		cpwd_stoptimer(p, index);
4678ab0dc33SDavid S. Miller 		break;
4688ab0dc33SDavid S. Miller 
4698ab0dc33SDavid S. Miller 	default:
4708ab0dc33SDavid S. Miller 		return -EINVAL;
4718ab0dc33SDavid S. Miller 	}
4728ab0dc33SDavid S. Miller 
4738ab0dc33SDavid S. Miller 	return 0;
4748ab0dc33SDavid S. Miller }
4758ab0dc33SDavid S. Miller 
cpwd_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)476c58e8134SArnd Bergmann static long cpwd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
477c58e8134SArnd Bergmann {
478c58e8134SArnd Bergmann 	return cpwd_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
479c58e8134SArnd Bergmann }
480c58e8134SArnd Bergmann 
cpwd_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)4818ab0dc33SDavid S. Miller static ssize_t cpwd_write(struct file *file, const char __user *buf,
4828ab0dc33SDavid S. Miller 			  size_t count, loff_t *ppos)
4838ab0dc33SDavid S. Miller {
484496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
4858ab0dc33SDavid S. Miller 	struct cpwd *p = cpwd_device;
4868ab0dc33SDavid S. Miller 	int index = iminor(inode);
4878ab0dc33SDavid S. Miller 
4888ab0dc33SDavid S. Miller 	if (count) {
4898ab0dc33SDavid S. Miller 		cpwd_pingtimer(p, index);
4908ab0dc33SDavid S. Miller 		return 1;
4918ab0dc33SDavid S. Miller 	}
4928ab0dc33SDavid S. Miller 
4938ab0dc33SDavid S. Miller 	return 0;
4948ab0dc33SDavid S. Miller }
4958ab0dc33SDavid S. Miller 
cpwd_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)4968ab0dc33SDavid S. Miller static ssize_t cpwd_read(struct file *file, char __user *buffer,
4978ab0dc33SDavid S. Miller 			 size_t count, loff_t *ppos)
4988ab0dc33SDavid S. Miller {
4998ab0dc33SDavid S. Miller 	return -EINVAL;
5008ab0dc33SDavid S. Miller }
5018ab0dc33SDavid S. Miller 
5028ab0dc33SDavid S. Miller static const struct file_operations cpwd_fops = {
5038ab0dc33SDavid S. Miller 	.owner =		THIS_MODULE,
5049626dd75SWim Van Sebroeck 	.unlocked_ioctl =	cpwd_ioctl,
505c58e8134SArnd Bergmann 	.compat_ioctl =		cpwd_compat_ioctl,
5068ab0dc33SDavid S. Miller 	.open =			cpwd_open,
5078ab0dc33SDavid S. Miller 	.write =		cpwd_write,
5088ab0dc33SDavid S. Miller 	.read =			cpwd_read,
5098ab0dc33SDavid S. Miller 	.release =		cpwd_release,
5106038f373SArnd Bergmann 	.llseek =		no_llseek,
5118ab0dc33SDavid S. Miller };
5128ab0dc33SDavid S. Miller 
cpwd_probe(struct platform_device * op)5132d991a16SBill Pemberton static int cpwd_probe(struct platform_device *op)
5148ab0dc33SDavid S. Miller {
5158ab0dc33SDavid S. Miller 	struct device_node *options;
5168ab0dc33SDavid S. Miller 	const char *str_prop;
5178ab0dc33SDavid S. Miller 	const void *prop_val;
5188ab0dc33SDavid S. Miller 	int i, err = -EINVAL;
5198ab0dc33SDavid S. Miller 	struct cpwd *p;
5208ab0dc33SDavid S. Miller 
5218ab0dc33SDavid S. Miller 	if (cpwd_device)
5228ab0dc33SDavid S. Miller 		return -EINVAL;
5238ab0dc33SDavid S. Miller 
524b6621df5SAmit Kushwaha 	p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
525b6621df5SAmit Kushwaha 	if (!p)
526b6621df5SAmit Kushwaha 		return -ENOMEM;
5278ab0dc33SDavid S. Miller 
5281636f8acSGrant Likely 	p->irq = op->archdata.irqs[0];
5298ab0dc33SDavid S. Miller 
5308ab0dc33SDavid S. Miller 	spin_lock_init(&p->lock);
5318ab0dc33SDavid S. Miller 
5328ab0dc33SDavid S. Miller 	p->regs = of_ioremap(&op->resource[0], 0,
5338ab0dc33SDavid S. Miller 			     4 * WD_TIMER_REGSZ, DRIVER_NAME);
5348ab0dc33SDavid S. Miller 	if (!p->regs) {
53527c766aaSJoe Perches 		pr_err("Unable to map registers\n");
536b6621df5SAmit Kushwaha 		return -ENOMEM;
5378ab0dc33SDavid S. Miller 	}
5388ab0dc33SDavid S. Miller 
5398ab0dc33SDavid S. Miller 	options = of_find_node_by_path("/options");
5408ab0dc33SDavid S. Miller 	if (!options) {
541b6621df5SAmit Kushwaha 		err = -ENODEV;
54227c766aaSJoe Perches 		pr_err("Unable to find /options node\n");
5438ab0dc33SDavid S. Miller 		goto out_iounmap;
5448ab0dc33SDavid S. Miller 	}
5458ab0dc33SDavid S. Miller 
5468ab0dc33SDavid S. Miller 	prop_val = of_get_property(options, "watchdog-enable?", NULL);
5478ab0dc33SDavid S. Miller 	p->enabled = (prop_val ? true : false);
5488ab0dc33SDavid S. Miller 
5498ab0dc33SDavid S. Miller 	prop_val = of_get_property(options, "watchdog-reboot?", NULL);
5508ab0dc33SDavid S. Miller 	p->reboot = (prop_val ? true : false);
5518ab0dc33SDavid S. Miller 
5528ab0dc33SDavid S. Miller 	str_prop = of_get_property(options, "watchdog-timeout", NULL);
5538ab0dc33SDavid S. Miller 	if (str_prop)
5548ab0dc33SDavid S. Miller 		p->timeout = simple_strtoul(str_prop, NULL, 10);
5558ab0dc33SDavid S. Miller 
55606f8f2caSYangtao Li 	of_node_put(options);
55706f8f2caSYangtao Li 
5588ab0dc33SDavid S. Miller 	/* CP1400s seem to have broken PLD implementations-- the
5598ab0dc33SDavid S. Miller 	 * interrupt_mask register cannot be written, so no timer
5608ab0dc33SDavid S. Miller 	 * interrupts can be masked within the PLD.
5618ab0dc33SDavid S. Miller 	 */
56261c7a080SGrant Likely 	str_prop = of_get_property(op->dev.of_node, "model", NULL);
5638ab0dc33SDavid S. Miller 	p->broken = (str_prop && !strcmp(str_prop, WD_BADMODEL));
5648ab0dc33SDavid S. Miller 
5658ab0dc33SDavid S. Miller 	if (!p->enabled)
5668ab0dc33SDavid S. Miller 		cpwd_toggleintr(p, -1, WD_INTR_OFF);
5678ab0dc33SDavid S. Miller 
5688ab0dc33SDavid S. Miller 	for (i = 0; i < WD_NUMDEVS; i++) {
5698ab0dc33SDavid S. Miller 		static const char *cpwd_names[] = { "RIC", "XIR", "POR" };
5708ab0dc33SDavid S. Miller 		static int *parms[] = { &wd0_timeout,
5718ab0dc33SDavid S. Miller 					&wd1_timeout,
5728ab0dc33SDavid S. Miller 					&wd2_timeout };
5738ab0dc33SDavid S. Miller 		struct miscdevice *mp = &p->devs[i].misc;
5748ab0dc33SDavid S. Miller 
5758ab0dc33SDavid S. Miller 		mp->minor = WD0_MINOR + i;
5768ab0dc33SDavid S. Miller 		mp->name = cpwd_names[i];
5778ab0dc33SDavid S. Miller 		mp->fops = &cpwd_fops;
5788ab0dc33SDavid S. Miller 
5798ab0dc33SDavid S. Miller 		p->devs[i].regs = p->regs + (i * WD_TIMER_REGSZ);
5808ab0dc33SDavid S. Miller 		p->devs[i].intr_mask = (WD0_INTR_MASK << i);
5818ab0dc33SDavid S. Miller 		p->devs[i].runstatus &= ~WD_STAT_BSTOP;
5828ab0dc33SDavid S. Miller 		p->devs[i].runstatus |= WD_STAT_INIT;
5838ab0dc33SDavid S. Miller 		p->devs[i].timeout = p->timeout;
5848ab0dc33SDavid S. Miller 		if (*parms[i])
5858ab0dc33SDavid S. Miller 			p->devs[i].timeout = *parms[i];
5868ab0dc33SDavid S. Miller 
5878ab0dc33SDavid S. Miller 		err = misc_register(&p->devs[i].misc);
5888ab0dc33SDavid S. Miller 		if (err) {
58927c766aaSJoe Perches 			pr_err("Could not register misc device for dev %d\n",
59027c766aaSJoe Perches 			       i);
5918ab0dc33SDavid S. Miller 			goto out_unregister;
5928ab0dc33SDavid S. Miller 		}
5938ab0dc33SDavid S. Miller 	}
5948ab0dc33SDavid S. Miller 
5958ab0dc33SDavid S. Miller 	if (p->broken) {
5964fa42b4eSKees Cook 		timer_setup(&cpwd_timer, cpwd_brokentimer, 0);
5978ab0dc33SDavid S. Miller 		cpwd_timer.expires	= WD_BTIMEOUT;
5988ab0dc33SDavid S. Miller 
59927c766aaSJoe Perches 		pr_info("PLD defect workaround enabled for model %s\n",
60027c766aaSJoe Perches 			WD_BADMODEL);
6018ab0dc33SDavid S. Miller 	}
6028ab0dc33SDavid S. Miller 
60326556b6eSJingoo Han 	platform_set_drvdata(op, p);
6048ab0dc33SDavid S. Miller 	cpwd_device = p;
605b6621df5SAmit Kushwaha 	return 0;
6068ab0dc33SDavid S. Miller 
6078ab0dc33SDavid S. Miller out_unregister:
6088ab0dc33SDavid S. Miller 	for (i--; i >= 0; i--)
6098ab0dc33SDavid S. Miller 		misc_deregister(&p->devs[i].misc);
6108ab0dc33SDavid S. Miller 
6118ab0dc33SDavid S. Miller out_iounmap:
6128ab0dc33SDavid S. Miller 	of_iounmap(&op->resource[0], p->regs, 4 * WD_TIMER_REGSZ);
6138ab0dc33SDavid S. Miller 
614b6621df5SAmit Kushwaha 	return err;
6158ab0dc33SDavid S. Miller }
6168ab0dc33SDavid S. Miller 
cpwd_remove(struct platform_device * op)6175caafac4SUwe Kleine-König static void cpwd_remove(struct platform_device *op)
6188ab0dc33SDavid S. Miller {
61926556b6eSJingoo Han 	struct cpwd *p = platform_get_drvdata(op);
6208ab0dc33SDavid S. Miller 	int i;
6218ab0dc33SDavid S. Miller 
622bbd562d7SWim Van Sebroeck 	for (i = 0; i < WD_NUMDEVS; i++) {
6238ab0dc33SDavid S. Miller 		misc_deregister(&p->devs[i].misc);
6248ab0dc33SDavid S. Miller 
6258ab0dc33SDavid S. Miller 		if (!p->enabled) {
6268ab0dc33SDavid S. Miller 			cpwd_stoptimer(p, i);
6278ab0dc33SDavid S. Miller 			if (p->devs[i].runstatus & WD_STAT_BSTOP)
6288ab0dc33SDavid S. Miller 				cpwd_resetbrokentimer(p, i);
6298ab0dc33SDavid S. Miller 		}
6308ab0dc33SDavid S. Miller 	}
6318ab0dc33SDavid S. Miller 
6328ab0dc33SDavid S. Miller 	if (p->broken)
6338ab0dc33SDavid S. Miller 		del_timer_sync(&cpwd_timer);
6348ab0dc33SDavid S. Miller 
6358ab0dc33SDavid S. Miller 	if (p->initialized)
6368ab0dc33SDavid S. Miller 		free_irq(p->irq, p);
6378ab0dc33SDavid S. Miller 
6388ab0dc33SDavid S. Miller 	of_iounmap(&op->resource[0], p->regs, 4 * WD_TIMER_REGSZ);
6398ab0dc33SDavid S. Miller 
6408ab0dc33SDavid S. Miller 	cpwd_device = NULL;
6418ab0dc33SDavid S. Miller }
6428ab0dc33SDavid S. Miller 
643fd098316SDavid S. Miller static const struct of_device_id cpwd_match[] = {
6448ab0dc33SDavid S. Miller 	{
6458ab0dc33SDavid S. Miller 		.name = "watchdog",
6468ab0dc33SDavid S. Miller 	},
6478ab0dc33SDavid S. Miller 	{},
6488ab0dc33SDavid S. Miller };
6498ab0dc33SDavid S. Miller MODULE_DEVICE_TABLE(of, cpwd_match);
6508ab0dc33SDavid S. Miller 
6511c48a5c9SGrant Likely static struct platform_driver cpwd_driver = {
6524018294bSGrant Likely 	.driver = {
6538ab0dc33SDavid S. Miller 		.name = DRIVER_NAME,
6544018294bSGrant Likely 		.of_match_table = cpwd_match,
6554018294bSGrant Likely 	},
6568ab0dc33SDavid S. Miller 	.probe		= cpwd_probe,
6575caafac4SUwe Kleine-König 	.remove_new	= cpwd_remove,
6588ab0dc33SDavid S. Miller };
6598ab0dc33SDavid S. Miller 
660b8ec6118SAxel Lin module_platform_driver(cpwd_driver);
661