Blame view

drivers/watchdog/cpwd.c 16.1 KB
8ab0dc333   David S. Miller   cpwatchdog: Move ...
1
  /* cpwd.c - driver implementation for hardware watchdog
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
   * timers found on Sun Microsystems CP1400 and CP1500 boards.
   *
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
4
   * This device supports both the generic Linux watchdog
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
   * interface and Solaris-compatible ioctls as best it is
   * able.
   *
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
8
9
10
11
   * NOTE:	CP1400 systems appear to have a defective intr_mask
   *			register on the PLD, preventing the disabling of
   *			timer interrupts.  We use a timer to periodically
   *			reset 'stopped' watchdogs on affected platforms.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
   * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
c5f8556cb   David S. Miller   cpwatchdog: Clean...
14
   * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
17
18
19
20
21
22
23
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/fs.h>
  #include <linux/errno.h>
  #include <linux/major.h>
  #include <linux/init.h>
  #include <linux/miscdevice.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
  #include <linux/interrupt.h>
  #include <linux/ioport.h>
  #include <linux/timer.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
27
  #include <linux/slab.h>
613655fa3   Arnd Bergmann   drivers: autoconv...
28
  #include <linux/mutex.h>
347e03df1   Andrew Morton   cpwatchdog build fix
29
  #include <linux/io.h>
c5f8556cb   David S. Miller   cpwatchdog: Clean...
30
31
  #include <linux/of.h>
  #include <linux/of_device.h>
278aefc51   Wim Van Sebroeck   [WATCHDOG] Fix io...
32
  #include <linux/uaccess.h>
c5f8556cb   David S. Miller   cpwatchdog: Clean...
33

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  #include <asm/irq.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
  #include <asm/watchdog.h>
c5f8556cb   David S. Miller   cpwatchdog: Clean...
36
37
  #define DRIVER_NAME	"cpwd"
  #define PFX		DRIVER_NAME ": "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  #define WD_OBPNAME	"watchdog"
c5f8556cb   David S. Miller   cpwatchdog: Clean...
39
  #define WD_BADMODEL	"SUNW,501-5336"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
  #define WD_BTIMEOUT	(jiffies + (HZ * 1000))
  #define WD_BLIMIT	0xFFFF
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
  #define WD0_MINOR	212
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
43
44
  #define WD1_MINOR	213
  #define WD2_MINOR	214
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45

c5f8556cb   David S. Miller   cpwatchdog: Clean...
46
47
48
49
50
  /* Internal driver definitions.  */
  #define WD0_ID			0
  #define WD1_ID			1
  #define WD2_ID			2
  #define WD_NUMDEVS		3
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51

c5f8556cb   David S. Miller   cpwatchdog: Clean...
52
53
  #define WD_INTR_OFF		0
  #define WD_INTR_ON		1
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
61
62
63
64
65
66
  
  #define WD_STAT_INIT	0x01	/* Watchdog timer is initialized	*/
  #define WD_STAT_BSTOP	0x02	/* Watchdog timer is brokenstopped	*/
  #define WD_STAT_SVCD	0x04	/* Watchdog interrupt occurred		*/
  
  /* Register value definitions
   */
  #define WD0_INTR_MASK	0x01	/* Watchdog device interrupt masks	*/
  #define WD1_INTR_MASK	0x02
  #define WD2_INTR_MASK	0x04
  
  #define WD_S_RUNNING	0x01	/* Watchdog device status running	*/
  #define WD_S_EXPIRED	0x02	/* Watchdog device status expired	*/
c5f8556cb   David S. Miller   cpwatchdog: Clean...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  struct cpwd {
  	void __iomem	*regs;
  	spinlock_t	lock;
  
  	unsigned int	irq;
  
  	unsigned long	timeout;
  	bool		enabled;
  	bool		reboot;
  	bool		broken;
  	bool		initialized;
  
  	struct {
  		struct miscdevice	misc;
  		void __iomem		*regs;
  		u8			intr_mask;
  		u8			runstatus;
  		u16			timeout;
  	} devs[WD_NUMDEVS];
  };
613655fa3   Arnd Bergmann   drivers: autoconv...
87
  static DEFINE_MUTEX(cpwd_mutex);
c5f8556cb   David S. Miller   cpwatchdog: Clean...
88
  static struct cpwd *cpwd_device;
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
89
  /* Sun uses Altera PLD EPF8820ATC144-4
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
   * providing three hardware watchdogs:
   *
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
92
93
94
   * 1) RIC - sends an interrupt when triggered
   * 2) XIR - asserts XIR_B_RESET when triggered, resets CPU
   * 3) POR - asserts POR_B_RESET when triggered, resets CPU, backplane, board
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
   *
   *** Timer register block definition (struct wd_timer_regblk)
   *
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
98
   * dcntr and limit registers (halfword access):
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
103
   * -------------------
   * | 15 | ...| 1 | 0 |
   * -------------------
   * |-  counter val  -|
   * -------------------
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
104
105
106
107
108
109
110
   * dcntr -	Current 16-bit downcounter value.
   *			When downcounter reaches '0' watchdog expires.
   *			Reading this register resets downcounter with
   *			'limit' value.
   * limit -	16-bit countdown value in 1/10th second increments.
   *			Writing this register begins countdown with input value.
   *			Reading from this register does not affect counter.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
112
113
114
115
116
117
118
119
   * NOTES:	After watchdog reset, dcntr and limit contain '1'
   *
   * status register (byte access):
   * ---------------------------
   * | 7 | ... | 2 |  1  |  0  |
   * --------------+------------
   * |-   UNUSED  -| EXP | RUN |
   * ---------------------------
   * status-	Bit 0 - Watchdog is running
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
120
   *			Bit 1 - Watchdog has expired
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
   *
   *** PLD register block definition (struct wd_pld_regblk)
   *
   * intr_mask register (byte access):
   * ---------------------------------
   * | 7 | ... | 3 |  2  |  1  |  0  |
   * +-------------+------------------
   * |-   UNUSED  -| WD3 | WD2 | WD1 |
   * ---------------------------------
   * WD3 -  1 == Interrupt disabled for watchdog 3
   * WD2 -  1 == Interrupt disabled for watchdog 2
   * WD1 -  1 == Interrupt disabled for watchdog 1
   *
   * pld_status register (byte access):
   * UNKNOWN, MAGICAL MYSTERY REGISTER
   *
   */
  #define WD_TIMER_REGSZ	16
  #define WD0_OFF		0
  #define WD1_OFF		(WD_TIMER_REGSZ * 1)
  #define WD2_OFF		(WD_TIMER_REGSZ * 2)
  #define PLD_OFF		(WD_TIMER_REGSZ * 3)
  
  #define WD_DCNTR	0x00
  #define WD_LIMIT	0x04
  #define WD_STATUS	0x08
  
  #define PLD_IMASK	(PLD_OFF + 0x00)
  #define PLD_STATUS	(PLD_OFF + 0x04)
c5f8556cb   David S. Miller   cpwatchdog: Clean...
150
  static struct timer_list cpwd_timer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151

a77dba7e4   Wim Van Sebroeck   [WATCHDOG] Some m...
152
153
154
  static int wd0_timeout;
  static int wd1_timeout;
  static int wd2_timeout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155

927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
156
  module_param(wd0_timeout, int, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
  MODULE_PARM_DESC(wd0_timeout, "Default watchdog0 timeout in 1/10secs");
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
158
  module_param(wd1_timeout, int, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
  MODULE_PARM_DESC(wd1_timeout, "Default watchdog1 timeout in 1/10secs");
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
160
  module_param(wd2_timeout, int, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
  MODULE_PARM_DESC(wd2_timeout, "Default watchdog2 timeout in 1/10secs");
c5f8556cb   David S. Miller   cpwatchdog: Clean...
162
163
  MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
  MODULE_DESCRIPTION("Hardware watchdog driver for Sun Microsystems CP1400/1500");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
  MODULE_LICENSE("GPL");
c5f8556cb   David S. Miller   cpwatchdog: Clean...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  MODULE_SUPPORTED_DEVICE("watchdog");
  
  static void cpwd_writew(u16 val, void __iomem *addr)
  {
  	writew(cpu_to_le16(val), addr);
  }
  static u16 cpwd_readw(void __iomem *addr)
  {
  	u16 val = readw(addr);
  
  	return le16_to_cpu(val);
  }
  
  static void cpwd_writeb(u8 val, void __iomem *addr)
  {
  	writeb(val, addr);
  }
  
  static u8 cpwd_readb(void __iomem *addr)
  {
  	return readb(addr);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187

c5f8556cb   David S. Miller   cpwatchdog: Clean...
188
189
190
191
  /* Enable or disable watchdog interrupts
   * Because of the CP1400 defect this should only be
   * called during initialzation or by wd_[start|stop]timer()
   *
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
192
   * index	- sub-device index, or -1 for 'all'
c5f8556cb   David S. Miller   cpwatchdog: Clean...
193
   * enable	- non-zero to enable interrupts, zero to disable
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
   */
c5f8556cb   David S. Miller   cpwatchdog: Clean...
195
196
197
  static void cpwd_toggleintr(struct cpwd *p, int index, int enable)
  {
  	unsigned char curregs = cpwd_readb(p->regs + PLD_IMASK);
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
198
199
200
  	unsigned char setregs =
  		(index == -1) ?
  		(WD0_INTR_MASK | WD1_INTR_MASK | WD2_INTR_MASK) :
c5f8556cb   David S. Miller   cpwatchdog: Clean...
201
202
203
204
205
206
207
208
209
210
211
212
  		(p->devs[index].intr_mask);
  
  	if (enable == WD_INTR_ON)
  		curregs &= ~setregs;
  	else
  		curregs |= setregs;
  
  	cpwd_writeb(curregs, p->regs + PLD_IMASK);
  }
  
  /* Restarts timer with maximum limit value and
   * does not unset 'brokenstop' value.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
   */
c5f8556cb   David S. Miller   cpwatchdog: Clean...
214
  static void cpwd_resetbrokentimer(struct cpwd *p, int index)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
216
217
  	cpwd_toggleintr(p, index, WD_INTR_ON);
  	cpwd_writew(WD_BLIMIT, p->devs[index].regs + WD_LIMIT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  }
c5f8556cb   David S. Miller   cpwatchdog: Clean...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  /* Timer method called to reset stopped watchdogs--
   * because of the PLD bug on CP1400, we cannot mask
   * interrupts within the PLD so me must continually
   * reset the timers ad infinitum.
   */
  static void cpwd_brokentimer(unsigned long data)
  {
  	struct cpwd *p = (struct cpwd *) data;
  	int id, tripped = 0;
  
  	/* kill a running timer instance, in case we
  	 * were called directly instead of by kernel timer
  	 */
  	if (timer_pending(&cpwd_timer))
  		del_timer(&cpwd_timer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234

c5f8556cb   David S. Miller   cpwatchdog: Clean...
235
236
237
238
239
240
  	for (id = 0; id < WD_NUMDEVS; id++) {
  		if (p->devs[id].runstatus & WD_STAT_BSTOP) {
  			++tripped;
  			cpwd_resetbrokentimer(p, id);
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241

c5f8556cb   David S. Miller   cpwatchdog: Clean...
242
243
244
245
246
247
248
249
250
  	if (tripped) {
  		/* there is at least one timer brokenstopped-- reschedule */
  		cpwd_timer.expires = WD_BTIMEOUT;
  		add_timer(&cpwd_timer);
  	}
  }
  
  /* Reset countdown timer with 'limit' value and continue countdown.
   * This will not start a stopped timer.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
   */
c5f8556cb   David S. Miller   cpwatchdog: Clean...
252
  static void cpwd_pingtimer(struct cpwd *p, int index)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
254
255
  	if (cpwd_readb(p->devs[index].regs + WD_STATUS) & WD_S_RUNNING)
  		cpwd_readw(p->devs[index].regs + WD_DCNTR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
  }
c5f8556cb   David S. Miller   cpwatchdog: Clean...
257
258
259
260
  
  /* Stop a running watchdog timer-- the timer actually keeps
   * running, but the interrupt is masked so that no action is
   * taken upon expiration.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
   */
c5f8556cb   David S. Miller   cpwatchdog: Clean...
262
  static void cpwd_stoptimer(struct cpwd *p, int index)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
264
265
  	if (cpwd_readb(p->devs[index].regs + WD_STATUS) & WD_S_RUNNING) {
  		cpwd_toggleintr(p, index, WD_INTR_OFF);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266

c5f8556cb   David S. Miller   cpwatchdog: Clean...
267
268
269
270
271
  		if (p->broken) {
  			p->devs[index].runstatus |= WD_STAT_BSTOP;
  			cpwd_brokentimer((unsigned long) p);
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
  }
c5f8556cb   David S. Miller   cpwatchdog: Clean...
273
274
275
276
277
278
  /* Start a watchdog timer with the specified limit value
   * If the watchdog is running, it will be restarted with
   * the provided limit value.
   *
   * This function will enable interrupts on the specified
   * watchdog.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
   */
c5f8556cb   David S. Miller   cpwatchdog: Clean...
280
  static void cpwd_starttimer(struct cpwd *p, int index)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
282
283
284
285
  	if (p->broken)
  		p->devs[index].runstatus &= ~WD_STAT_BSTOP;
  
  	p->devs[index].runstatus &= ~WD_STAT_SVCD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286

c5f8556cb   David S. Miller   cpwatchdog: Clean...
287
288
  	cpwd_writew(p->devs[index].timeout, p->devs[index].regs + WD_LIMIT);
  	cpwd_toggleintr(p, index, WD_INTR_ON);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  }
c5f8556cb   David S. Miller   cpwatchdog: Clean...
290
  static int cpwd_getstatus(struct cpwd *p, int index)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
292
293
294
295
296
  	unsigned char stat = cpwd_readb(p->devs[index].regs + WD_STATUS);
  	unsigned char intr = cpwd_readb(p->devs[index].regs + PLD_IMASK);
  	unsigned char ret  = WD_STOPPED;
  
  	/* determine STOPPED */
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
297
  	if (!stat)
c5f8556cb   David S. Miller   cpwatchdog: Clean...
298
299
300
301
302
  		return ret;
  
  	/* determine EXPIRED vs FREERUN vs RUNNING */
  	else if (WD_S_EXPIRED & stat) {
  		ret = WD_EXPIRED;
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
303
  	} else if (WD_S_RUNNING & stat) {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
304
305
306
307
  		if (intr & p->devs[index].intr_mask) {
  			ret = WD_FREERUN;
  		} else {
  			/* Fudge WD_EXPIRED status for defective CP1400--
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
308
  			 * IF timer is running
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
309
310
  			 *	AND brokenstop is set
  			 *	AND an interrupt has been serviced
c5f8556cb   David S. Miller   cpwatchdog: Clean...
311
312
  			 * we are WD_EXPIRED.
  			 *
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
313
  			 * IF timer is running
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
314
315
  			 *	AND brokenstop is set
  			 *	AND no interrupt has been serviced
c5f8556cb   David S. Miller   cpwatchdog: Clean...
316
317
318
319
320
321
322
  			 * we are WD_FREERUN.
  			 */
  			if (p->broken &&
  			    (p->devs[index].runstatus & WD_STAT_BSTOP)) {
  				if (p->devs[index].runstatus & WD_STAT_SVCD) {
  					ret = WD_EXPIRED;
  				} else {
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
323
324
  					/* we could as well pretend
  					 * we are expired */
c5f8556cb   David S. Miller   cpwatchdog: Clean...
325
326
327
328
  					ret = WD_FREERUN;
  				}
  			} else {
  				ret = WD_RUNNING;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
330
331
  			}
  		}
  	}
c5f8556cb   David S. Miller   cpwatchdog: Clean...
332
333
334
335
  
  	/* determine SERVICED */
  	if (p->devs[index].runstatus & WD_STAT_SVCD)
  		ret |= WD_SERVICED;
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
336
  	return ret;
c5f8556cb   David S. Miller   cpwatchdog: Clean...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  }
  
  static irqreturn_t cpwd_interrupt(int irq, void *dev_id)
  {
  	struct cpwd *p = dev_id;
  
  	/* Only WD0 will interrupt-- others are NMI and we won't
  	 * see them here....
  	 */
  	spin_lock_irq(&p->lock);
  
  	cpwd_stoptimer(p, WD0_ID);
  	p->devs[WD0_ID].runstatus |=  WD_STAT_SVCD;
  
  	spin_unlock_irq(&p->lock);
  
  	return IRQ_HANDLED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
  }
c5f8556cb   David S. Miller   cpwatchdog: Clean...
355
  static int cpwd_open(struct inode *inode, struct file *f)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
357
  	struct cpwd *p = cpwd_device;
613655fa3   Arnd Bergmann   drivers: autoconv...
358
  	mutex_lock(&cpwd_mutex);
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
359
360
361
362
363
  	switch (iminor(inode)) {
  	case WD0_MINOR:
  	case WD1_MINOR:
  	case WD2_MINOR:
  		break;
c5f8556cb   David S. Miller   cpwatchdog: Clean...
364

927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
365
  	default:
613655fa3   Arnd Bergmann   drivers: autoconv...
366
  		mutex_unlock(&cpwd_mutex);
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
367
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
370
  	}
  
  	/* Register IRQ on first open of device */
c5f8556cb   David S. Miller   cpwatchdog: Clean...
371
  	if (!p->initialized) {
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
372
  		if (request_irq(p->irq, &cpwd_interrupt,
c5f8556cb   David S. Miller   cpwatchdog: Clean...
373
  				IRQF_SHARED, DRIVER_NAME, p)) {
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
374
375
  			printk(KERN_ERR PFX "Cannot register IRQ %d
  ",
c5f8556cb   David S. Miller   cpwatchdog: Clean...
376
  				p->irq);
613655fa3   Arnd Bergmann   drivers: autoconv...
377
  			mutex_unlock(&cpwd_mutex);
c5f8556cb   David S. Miller   cpwatchdog: Clean...
378
  			return -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
  		}
c5f8556cb   David S. Miller   cpwatchdog: Clean...
380
  		p->initialized = true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
  	}
613655fa3   Arnd Bergmann   drivers: autoconv...
382
  	mutex_unlock(&cpwd_mutex);
c5f8556cb   David S. Miller   cpwatchdog: Clean...
383
384
  
  	return nonseekable_open(inode, f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385
  }
c5f8556cb   David S. Miller   cpwatchdog: Clean...
386
  static int cpwd_release(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
388
389
  {
  	return 0;
  }
9626dd75c   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
390
  static long cpwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
  {
42747d712   Wim Van Sebroeck   [WATCHDOG] watchd...
392
  	static const struct watchdog_info info = {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
393
394
395
  		.options		= WDIOF_SETTIMEOUT,
  		.firmware_version	= 1,
  		.identity		= DRIVER_NAME,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
  	};
c5f8556cb   David S. Miller   cpwatchdog: Clean...
397
  	void __user *argp = (void __user *)arg;
9626dd75c   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
398
  	struct inode *inode = file->f_path.dentry->d_inode;
c5f8556cb   David S. Miller   cpwatchdog: Clean...
399
400
401
  	int index = iminor(inode) - WD0_MINOR;
  	struct cpwd *p = cpwd_device;
  	int setopt = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402

c5f8556cb   David S. Miller   cpwatchdog: Clean...
403
404
405
406
407
408
  	switch (cmd) {
  	/* Generic Linux IOCTLs */
  	case WDIOC_GETSUPPORT:
  		if (copy_to_user(argp, &info, sizeof(struct watchdog_info)))
  			return -EFAULT;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409

c5f8556cb   David S. Miller   cpwatchdog: Clean...
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  	case WDIOC_GETSTATUS:
  	case WDIOC_GETBOOTSTATUS:
  		if (put_user(0, (int __user *)argp))
  			return -EFAULT;
  		break;
  
  	case WDIOC_KEEPALIVE:
  		cpwd_pingtimer(p, index);
  		break;
  
  	case WDIOC_SETOPTIONS:
  		if (copy_from_user(&setopt, argp, sizeof(unsigned int)))
  			return -EFAULT;
  
  		if (setopt & WDIOS_DISABLECARD) {
  			if (p->enabled)
  				return -EINVAL;
  			cpwd_stoptimer(p, index);
  		} else if (setopt & WDIOS_ENABLECARD) {
  			cpwd_starttimer(p, index);
  		} else {
  			return -EINVAL;
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
432
  		}
c5f8556cb   David S. Miller   cpwatchdog: Clean...
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
  		break;
  
  	/* Solaris-compatible IOCTLs */
  	case WIOCGSTAT:
  		setopt = cpwd_getstatus(p, index);
  		if (copy_to_user(argp, &setopt, sizeof(unsigned int)))
  			return -EFAULT;
  		break;
  
  	case WIOCSTART:
  		cpwd_starttimer(p, index);
  		break;
  
  	case WIOCSTOP:
  		if (p->enabled)
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
448
  			return -EINVAL;
c5f8556cb   David S. Miller   cpwatchdog: Clean...
449
450
451
452
453
454
  
  		cpwd_stoptimer(p, index);
  		break;
  
  	default:
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  	}
c5f8556cb   David S. Miller   cpwatchdog: Clean...
456
457
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
  }
c5f8556cb   David S. Miller   cpwatchdog: Clean...
459
460
  static long cpwd_compat_ioctl(struct file *file, unsigned int cmd,
  			      unsigned long arg)
b66621fef   Christoph Hellwig   [SPARC] cpwatchdo...
461
462
463
464
465
466
467
468
  {
  	int rval = -ENOIOCTLCMD;
  
  	switch (cmd) {
  	/* solaris ioctls are specific to this driver */
  	case WIOCSTART:
  	case WIOCSTOP:
  	case WIOCGSTAT:
613655fa3   Arnd Bergmann   drivers: autoconv...
469
  		mutex_lock(&cpwd_mutex);
9626dd75c   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
470
  		rval = cpwd_ioctl(file, cmd, arg);
613655fa3   Arnd Bergmann   drivers: autoconv...
471
  		mutex_unlock(&cpwd_mutex);
b66621fef   Christoph Hellwig   [SPARC] cpwatchdo...
472
  		break;
c5f8556cb   David S. Miller   cpwatchdog: Clean...
473

b66621fef   Christoph Hellwig   [SPARC] cpwatchdo...
474
475
476
477
478
479
480
  	/* everything else is handled by the generic compat layer */
  	default:
  		break;
  	}
  
  	return rval;
  }
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
481
  static ssize_t cpwd_write(struct file *file, const char __user *buf,
c5f8556cb   David S. Miller   cpwatchdog: Clean...
482
  			  size_t count, loff_t *ppos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
484
485
486
  	struct inode *inode = file->f_path.dentry->d_inode;
  	struct cpwd *p = cpwd_device;
  	int index = iminor(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
488
  
  	if (count) {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
489
  		cpwd_pingtimer(p, index);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
  		return 1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492

c5f8556cb   David S. Miller   cpwatchdog: Clean...
493
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
  }
927d69611   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
495
  static ssize_t cpwd_read(struct file *file, char __user *buffer,
c5f8556cb   David S. Miller   cpwatchdog: Clean...
496
  			 size_t count, loff_t *ppos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
498
  	return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
  }
c5f8556cb   David S. Miller   cpwatchdog: Clean...
500
  static const struct file_operations cpwd_fops = {
9626dd75c   Wim Van Sebroeck   [WATCHDOG] cpwd.c...
501
502
503
504
505
506
507
  	.owner =		THIS_MODULE,
  	.unlocked_ioctl =	cpwd_ioctl,
  	.compat_ioctl =		cpwd_compat_ioctl,
  	.open =			cpwd_open,
  	.write =		cpwd_write,
  	.read =			cpwd_read,
  	.release =		cpwd_release,
6038f373a   Arnd Bergmann   llseek: automatic...
508
  	.llseek =		no_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
  };
1c48a5c93   Grant Likely   dt: Eliminate of_...
510
  static int __devinit cpwd_probe(struct platform_device *op)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
512
513
514
515
516
  	struct device_node *options;
  	const char *str_prop;
  	const void *prop_val;
  	int i, err = -EINVAL;
  	struct cpwd *p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
517

c5f8556cb   David S. Miller   cpwatchdog: Clean...
518
519
  	if (cpwd_device)
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520

c5f8556cb   David S. Miller   cpwatchdog: Clean...
521
522
523
524
525
526
  	p = kzalloc(sizeof(*p), GFP_KERNEL);
  	err = -ENOMEM;
  	if (!p) {
  		printk(KERN_ERR PFX "Unable to allocate struct cpwd.
  ");
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528

1636f8ac2   Grant Likely   sparc/of: Move of...
529
  	p->irq = op->archdata.irqs[0];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
530

c5f8556cb   David S. Miller   cpwatchdog: Clean...
531
  	spin_lock_init(&p->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532

c5f8556cb   David S. Miller   cpwatchdog: Clean...
533
534
535
536
537
538
  	p->regs = of_ioremap(&op->resource[0], 0,
  			     4 * WD_TIMER_REGSZ, DRIVER_NAME);
  	if (!p->regs) {
  		printk(KERN_ERR PFX "Unable to map registers.
  ");
  		goto out_free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540

c5f8556cb   David S. Miller   cpwatchdog: Clean...
541
542
543
544
545
546
547
  	options = of_find_node_by_path("/options");
  	err = -ENODEV;
  	if (!options) {
  		printk(KERN_ERR PFX "Unable to find /options node.
  ");
  		goto out_iounmap;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548

c5f8556cb   David S. Miller   cpwatchdog: Clean...
549
550
  	prop_val = of_get_property(options, "watchdog-enable?", NULL);
  	p->enabled = (prop_val ? true : false);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551

c5f8556cb   David S. Miller   cpwatchdog: Clean...
552
553
  	prop_val = of_get_property(options, "watchdog-reboot?", NULL);
  	p->reboot = (prop_val ? true : false);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
554

c5f8556cb   David S. Miller   cpwatchdog: Clean...
555
556
557
  	str_prop = of_get_property(options, "watchdog-timeout", NULL);
  	if (str_prop)
  		p->timeout = simple_strtoul(str_prop, NULL, 10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
558

c5f8556cb   David S. Miller   cpwatchdog: Clean...
559
560
561
  	/* CP1400s seem to have broken PLD implementations-- the
  	 * interrupt_mask register cannot be written, so no timer
  	 * interrupts can be masked within the PLD.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562
  	 */
61c7a080a   Grant Likely   of: Always use 's...
563
  	str_prop = of_get_property(op->dev.of_node, "model", NULL);
c5f8556cb   David S. Miller   cpwatchdog: Clean...
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
  	p->broken = (str_prop && !strcmp(str_prop, WD_BADMODEL));
  
  	if (!p->enabled)
  		cpwd_toggleintr(p, -1, WD_INTR_OFF);
  
  	for (i = 0; i < WD_NUMDEVS; i++) {
  		static const char *cpwd_names[] = { "RIC", "XIR", "POR" };
  		static int *parms[] = { &wd0_timeout,
  					&wd1_timeout,
  					&wd2_timeout };
  		struct miscdevice *mp = &p->devs[i].misc;
  
  		mp->minor = WD0_MINOR + i;
  		mp->name = cpwd_names[i];
  		mp->fops = &cpwd_fops;
  
  		p->devs[i].regs = p->regs + (i * WD_TIMER_REGSZ);
  		p->devs[i].intr_mask = (WD0_INTR_MASK << i);
  		p->devs[i].runstatus &= ~WD_STAT_BSTOP;
  		p->devs[i].runstatus |= WD_STAT_INIT;
  		p->devs[i].timeout = p->timeout;
  		if (*parms[i])
  			p->devs[i].timeout = *parms[i];
  
  		err = misc_register(&p->devs[i].misc);
  		if (err) {
  			printk(KERN_ERR "Could not register misc device for "
  			       "dev %d
  ", i);
  			goto out_unregister;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
595
  		}
  	}
c5f8556cb   David S. Miller   cpwatchdog: Clean...
596
597
  	if (p->broken) {
  		init_timer(&cpwd_timer);
5f3b27569   Wim Van Sebroeck   watchdog: cleanup...
598
  		cpwd_timer.function	= cpwd_brokentimer;
c5f8556cb   David S. Miller   cpwatchdog: Clean...
599
600
601
602
603
604
  		cpwd_timer.data		= (unsigned long) p;
  		cpwd_timer.expires	= WD_BTIMEOUT;
  
  		printk(KERN_INFO PFX "PLD defect workaround enabled for "
  		       "model " WD_BADMODEL ".
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
606

c5f8556cb   David S. Miller   cpwatchdog: Clean...
607
608
609
  	dev_set_drvdata(&op->dev, p);
  	cpwd_device = p;
  	err = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
610

c5f8556cb   David S. Miller   cpwatchdog: Clean...
611
612
  out:
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
613

c5f8556cb   David S. Miller   cpwatchdog: Clean...
614
615
616
  out_unregister:
  	for (i--; i >= 0; i--)
  		misc_deregister(&p->devs[i].misc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
617

c5f8556cb   David S. Miller   cpwatchdog: Clean...
618
619
620
621
622
623
  out_iounmap:
  	of_iounmap(&op->resource[0], p->regs, 4 * WD_TIMER_REGSZ);
  
  out_free:
  	kfree(p);
  	goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
  }
2dc115813   Grant Likely   of/device: Replac...
625
  static int __devexit cpwd_remove(struct platform_device *op)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
  {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
627
628
  	struct cpwd *p = dev_get_drvdata(&op->dev);
  	int i;
bbd562d71   Wim Van Sebroeck   watchdog: cpwd: F...
629
  	for (i = 0; i < WD_NUMDEVS; i++) {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
630
631
632
633
634
635
  		misc_deregister(&p->devs[i].misc);
  
  		if (!p->enabled) {
  			cpwd_stoptimer(p, i);
  			if (p->devs[i].runstatus & WD_STAT_BSTOP)
  				cpwd_resetbrokentimer(p, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
636
637
  		}
  	}
c5f8556cb   David S. Miller   cpwatchdog: Clean...
638
639
  	if (p->broken)
  		del_timer_sync(&cpwd_timer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640

c5f8556cb   David S. Miller   cpwatchdog: Clean...
641
642
  	if (p->initialized)
  		free_irq(p->irq, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
643

c5f8556cb   David S. Miller   cpwatchdog: Clean...
644
645
  	of_iounmap(&op->resource[0], p->regs, 4 * WD_TIMER_REGSZ);
  	kfree(p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
646

c5f8556cb   David S. Miller   cpwatchdog: Clean...
647
  	cpwd_device = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
648

c5f8556cb   David S. Miller   cpwatchdog: Clean...
649
650
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651

fd098316e   David S. Miller   sparc: Annotate o...
652
  static const struct of_device_id cpwd_match[] = {
c5f8556cb   David S. Miller   cpwatchdog: Clean...
653
654
655
656
657
658
  	{
  		.name = "watchdog",
  	},
  	{},
  };
  MODULE_DEVICE_TABLE(of, cpwd_match);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659

1c48a5c93   Grant Likely   dt: Eliminate of_...
660
  static struct platform_driver cpwd_driver = {
4018294b5   Grant Likely   of: Remove duplic...
661
662
663
664
665
  	.driver = {
  		.name = DRIVER_NAME,
  		.owner = THIS_MODULE,
  		.of_match_table = cpwd_match,
  	},
c5f8556cb   David S. Miller   cpwatchdog: Clean...
666
667
668
  	.probe		= cpwd_probe,
  	.remove		= __devexit_p(cpwd_remove),
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669

b8ec61189   Axel Lin   watchdog: convert...
670
  module_platform_driver(cpwd_driver);