blob: 3d3deae0d64ba0668f28fb275a082531a7e62e1c [file] [log] [blame]
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +02001/*
2 * NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
3 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +02004 * This code is based on wdt.c with original copyright.
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +02005 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +02006 * (C) Copyright 2006 Sven Anders, <anders@anduras.de>
7 * and Marcus Junker, <junker@anduras.de>
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +02008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020014 * Neither Sven Anders, Marcus Junker nor ANDURAS AG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020015 * admit liability nor provide warranty for any of this software.
16 * This material is provided "AS-IS" and at no charge.
17 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020018 * Release 1.1
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020019 */
20
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020021#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/miscdevice.h>
24#include <linux/watchdog.h>
25#include <linux/ioport.h>
26#include <linux/delay.h>
27#include <linux/notifier.h>
28#include <linux/fs.h>
29#include <linux/reboot.h>
30#include <linux/init.h>
31#include <linux/spinlock.h>
32#include <linux/moduleparam.h>
33#include <linux/version.h>
34
35#include <asm/io.h>
36#include <asm/uaccess.h>
37#include <asm/system.h>
38
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020039/* #define DEBUG 1 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020040
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020041#define DEFAULT_TIMEOUT 1 /* 1 minute */
42#define MAX_TIMEOUT 255
43
44#define VERSION "1.1"
45#define MODNAME "pc87413 WDT"
46#define PFX MODNAME ": "
47#define DPFX MODNAME " - DEBUG: "
48
49#define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020050#define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020051#define SWC_LDN 0x04
52#define SIOCFG2 0x22 /* Serial IO register */
53#define WDCTL 0x10 /* Watchdog-Timer-Controll-Register */
54#define WDTO 0x11 /* Watchdog timeout register */
55#define WDCFG 0x12 /* Watchdog config register */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020056
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020057static int io = 0x2E; /* Address used on Portwell Boards */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020058
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020059static int timeout = DEFAULT_TIMEOUT; /* timeout value */
60static unsigned long timer_enabled = 0; /* is the timer enabled? */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020061
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020062static char expect_close; /* is the close expected? */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020063
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020064static spinlock_t io_lock; /* to guard the watchdog from io races */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020065
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020066static int nowayout = WATCHDOG_NOWAYOUT;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020067
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020068/* -- Low level function ----------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020069
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020070/* Select pins for Watchdog output */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020071
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020072static inline void pc87413_select_wdt_out (void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020073{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020074 unsigned int cr_data = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020075
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020076 /* Step 1: Select multiple pin,pin55,as WDT output */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020077
78 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
79
80 cr_data = inb (WDT_DATA_IO_PORT);
81
82 cr_data |= 0x80; /* Set Bit7 to 1*/
83 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
84
85 outb_p(cr_data, WDT_DATA_IO_PORT);
86
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020087#ifdef DEBUG
88 printk(KERN_INFO DPFX "Select multiple pin,pin55,as WDT output:"
89 " Bit7 to 1: %d\n", cr_data);
90#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020091}
92
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020093/* Enable SWC functions */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020094
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020095static inline void pc87413_enable_swc(void)
96{
97 unsigned int cr_data=0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020098
99 /* Step 2: Enable SWC functions */
100
101 outb_p(0x07, WDT_INDEX_IO_PORT); /* Point SWC_LDN (LDN=4) */
102 outb_p(SWC_LDN, WDT_DATA_IO_PORT);
103
104 outb_p(0x30, WDT_INDEX_IO_PORT); /* Read Index 0x30 First */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200105 cr_data = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200106 cr_data |= 0x01; /* Set Bit0 to 1 */
107 outb_p(0x30, WDT_INDEX_IO_PORT);
108 outb_p(cr_data, WDT_DATA_IO_PORT); /* Index0x30_bit0P1 */
109
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200110#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200111 printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200112#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200113}
114
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200115/* Read SWC I/O base address */
116
117static inline unsigned int pc87413_get_swc_base(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200118{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200119 unsigned int swc_base_addr = 0;
120 unsigned char addr_l, addr_h = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200121
122 /* Step 3: Read SWC I/O Base Address */
123
124 outb_p(0x60, WDT_INDEX_IO_PORT); /* Read Index 0x60 */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200125 addr_h = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200126
127 outb_p(0x61, WDT_INDEX_IO_PORT); /* Read Index 0x61 */
128
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200129 addr_l = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200130
131 swc_base_addr = (addr_h << 8) + addr_l;
132
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200133#ifdef DEBUG
134 printk(KERN_INFO DPFX "Read SWC I/O Base Address: low %d, high %d,"
135 " res %d\n", addr_l, addr_h, swc_base_addr);
136#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200137
138 return swc_base_addr;
139}
140
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200141/* Select Bank 3 of SWC */
142
143static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200144{
145 /* Step 4: Select Bank3 of SWC */
146
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200147 outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200148
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200149#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200150 printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200151#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200152}
153
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200154/* Set watchdog timeout to x minutes */
155
156static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
157 char pc87413_time)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200158{
159 /* Step 5: Programm WDTO, Twd. */
160
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200161 outb_p(pc87413_time, swc_base_addr + WDTO);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200162
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200163#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200164 printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200165#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200166}
167
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200168/* Enable WDEN */
169
170static inline void pc87413_enable_wden(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200171{
172 /* Step 6: Enable WDEN */
173
174 outb_p(inb (swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
175
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200176#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200177 printk(KERN_INFO DPFX "Enable WDEN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200178#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200179}
180
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200181/* Enable SW_WD_TREN */
182static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200183{
184 /* Enable SW_WD_TREN */
185
186 outb_p(inb (swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
187
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200188#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200189 printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200190#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200191}
192
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200193/* Disable SW_WD_TREN */
194
195static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200196{
197 /* Disable SW_WD_TREN */
198
199 outb_p(inb (swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
200
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200201#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200202 printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200203#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200204}
205
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200206/* Enable SW_WD_TRG */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200207
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200208static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200209{
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200210 /* Enable SW_WD_TRG */
211
212 outb_p(inb (swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
213
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200214#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200215 printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200216#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200217}
218
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200219/* Disable SW_WD_TRG */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200220
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200221static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
222{
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200223 /* Disable SW_WD_TRG */
224
225 outb_p(inb (swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
226
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200227#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200228 printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200229#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200230}
231
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200232/* -- Higher level functions ------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200233
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200234/* Enable the watchdog */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200235
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200236static void pc87413_enable(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200237{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200238 unsigned int swc_base_addr;
239
240 spin_lock(&io_lock);
241
242 pc87413_select_wdt_out();
243 pc87413_enable_swc();
244 swc_base_addr = pc87413_get_swc_base();
245 pc87413_swc_bank3(swc_base_addr);
246 pc87413_programm_wdto(swc_base_addr, timeout);
247 pc87413_enable_wden(swc_base_addr);
248 pc87413_enable_sw_wd_tren(swc_base_addr);
249 pc87413_enable_sw_wd_trg(swc_base_addr);
250
251 spin_unlock(&io_lock);
252}
253
254/* Disable the watchdog */
255
256static void pc87413_disable(void)
257{
258 unsigned int swc_base_addr;
259
260 spin_lock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200261
262 pc87413_select_wdt_out();
263 pc87413_enable_swc();
264 swc_base_addr = pc87413_get_swc_base();
265 pc87413_swc_bank3(swc_base_addr);
266 pc87413_disable_sw_wd_tren(swc_base_addr);
267 pc87413_disable_sw_wd_trg(swc_base_addr);
268 pc87413_programm_wdto(swc_base_addr, 0);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200269
270 spin_unlock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200271}
272
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200273/* Refresh the watchdog */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200274
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200275static void pc87413_refresh(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200276{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200277 unsigned int swc_base_addr;
278
279 spin_lock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200280
281 pc87413_select_wdt_out();
282 pc87413_enable_swc();
283 swc_base_addr = pc87413_get_swc_base();
284 pc87413_swc_bank3(swc_base_addr);
285 pc87413_disable_sw_wd_tren(swc_base_addr);
286 pc87413_disable_sw_wd_trg(swc_base_addr);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200287 pc87413_programm_wdto(swc_base_addr, timeout);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200288 pc87413_enable_wden(swc_base_addr);
289 pc87413_enable_sw_wd_tren(swc_base_addr);
290 pc87413_enable_sw_wd_trg(swc_base_addr);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200291
292 spin_unlock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200293}
294
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200295/* -- File operations -------------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200296
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200297/**
298 * pc87413_open:
299 * @inode: inode of device
300 * @file: file handle to device
301 *
302 */
303
304static int pc87413_open(struct inode *inode, struct file *file)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200305{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200306 /* /dev/watchdog can only be opened once */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200307
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200308 if (test_and_set_bit(0, &timer_enabled))
309 return -EBUSY;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200310
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200311 if (nowayout)
312 __module_get(THIS_MODULE);
313
314 /* Reload and activate timer */
315 pc87413_refresh();
316
317 printk(KERN_INFO MODNAME "Watchdog enabled. Timeout set to"
318 " %d minute(s).\n", timeout);
319
320 return nonseekable_open(inode, file);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200321}
322
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200323/**
324 * pc87413_release:
325 * @inode: inode to board
326 * @file: file handle to board
327 *
328 * The watchdog has a configurable API. There is a religious dispute
329 * between people who want their watchdog to be able to shut down and
330 * those who want to be sure if the watchdog manager dies the machine
331 * reboots. In the former case we disable the counters, in the latter
332 * case you have to open it again very soon.
333 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200334
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200335static int pc87413_release(struct inode *inode, struct file *file)
336{
337 /* Shut off the timer. */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200338
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200339 if (expect_close == 42) {
340 pc87413_disable();
341 printk(KERN_INFO MODNAME "Watchdog disabled,"
342 " sleeping again...\n");
343 } else {
344 printk(KERN_CRIT MODNAME "Unexpected close, not stopping"
345 " watchdog!\n");
346 pc87413_refresh();
347 }
348
349 clear_bit(0, &timer_enabled);
350 expect_close = 0;
351
352 return 0;
353}
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200354
355/**
356 * pc87413_status:
357 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200358 * return, if the watchdog is enabled (timeout is set...)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200359 */
360
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200361
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200362static int pc87413_status(void)
363{
Wim Van Sebroeck0835caa2006-10-23 18:21:52 +0200364 return 0; /* currently not supported */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200365}
366
367/**
368 * pc87413_write:
369 * @file: file handle to the watchdog
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200370 * @data: data buffer to write
371 * @len: length in bytes
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200372 * @ppos: pointer to the position to write. No seeks allowed
373 *
374 * A write to a watchdog device is defined as a keepalive signal. Any
375 * write of data will do, as we we don't define content meaning.
376 */
377
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200378static ssize_t pc87413_write(struct file *file, const char __user *data,
379 size_t len, loff_t *ppos)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200380{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200381 /* See if we got the magic character 'V' and reload the timer */
382 if (len) {
383 if (!nowayout) {
384 size_t i;
385
386 /* reset expect flag */
387 expect_close = 0;
388
389 /* scan to see whether or not we got the magic character */
390 for (i = 0; i != len; i++) {
391 char c;
392 if (get_user(c, data+i))
393 return -EFAULT;
394 if (c == 'V')
395 expect_close = 42;
396 }
397 }
398
399 /* someone wrote to us, we should reload the timer */
400 pc87413_refresh();
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200401 }
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200402 return len;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200403}
404
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200405/**
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200406 * pc87413_ioctl:
407 * @inode: inode of the device
408 * @file: file handle to the device
409 * @cmd: watchdog command
410 * @arg: argument pointer
411 *
412 * The watchdog API defines a common set of functions for all watchdogs
413 * according to their available features. We only actually usefully support
414 * querying capabilities and current status.
415 */
416
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200417static int pc87413_ioctl(struct inode *inode, struct file *file,
418 unsigned int cmd, unsigned long arg)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200419{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200420 int new_timeout;
421
422 union {
423 struct watchdog_info __user *ident;
424 int __user *i;
425 } uarg;
426
427 static struct watchdog_info ident = {
428 .options = WDIOF_KEEPALIVEPING |
429 WDIOF_SETTIMEOUT |
430 WDIOF_MAGICCLOSE,
431 .firmware_version = 1,
432 .identity = "PC87413(HF/F) watchdog"
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200433 };
434
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200435 uarg.i = (int __user *)arg;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200436
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200437 switch(cmd) {
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200438 default:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200439 return -ENOTTY;
440
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200441 case WDIOC_GETSUPPORT:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200442 return copy_to_user(uarg.ident, &ident,
443 sizeof(ident)) ? -EFAULT : 0;
444
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200445 case WDIOC_GETSTATUS:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200446 return put_user(pc87413_status(), uarg.i);
447
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200448 case WDIOC_GETBOOTSTATUS:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200449 return put_user(0, uarg.i);
450
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200451 case WDIOC_KEEPALIVE:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200452 pc87413_refresh();
453#ifdef DEBUG
454 printk(KERN_INFO DPFX "keepalive\n");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200455#endif
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200456 return 0;
457
458 case WDIOC_SETTIMEOUT:
459 if (get_user(new_timeout, uarg.i))
460 return -EFAULT;
461
462 // the API states this is given in secs
463 new_timeout /= 60;
464
465 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
466 return -EINVAL;
467
468 timeout = new_timeout;
469 pc87413_refresh();
470
471 // fall through and return the new timeout...
472
473 case WDIOC_GETTIMEOUT:
474
475 new_timeout = timeout * 60;
476
477 return put_user(new_timeout, uarg.i);
478
479 case WDIOC_SETOPTIONS:
480 {
481 int options, retval = -EINVAL;
482
483 if (get_user(options, uarg.i))
484 return -EFAULT;
485
486 if (options & WDIOS_DISABLECARD) {
487 pc87413_disable();
488 retval = 0;
489 }
490
491 if (options & WDIOS_ENABLECARD) {
492 pc87413_enable();
493 retval = 0;
494 }
495
496 return retval;
497 }
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200498 }
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200499}
500
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200501/* -- Notifier funtions -----------------------------------------*/
502
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200503/**
504 * notify_sys:
505 * @this: our notifier block
506 * @code: the event being reported
507 * @unused: unused
508 *
509 * Our notifier is called on system shutdowns. We want to turn the card
510 * off at reboot otherwise the machine will reboot again during memory
511 * test or worse yet during the following fsck. This would suck, in fact
512 * trust me - if it happens it does suck.
513 */
514
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200515static int pc87413_notify_sys(struct notifier_block *this,
516 unsigned long code,
517 void *unused)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200518{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200519 if (code == SYS_DOWN || code == SYS_HALT)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200520 {
521 /* Turn the card off */
522 pc87413_disable();
523 }
524 return NOTIFY_DONE;
525}
526
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200527/* -- Module's structures ---------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200528
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800529static const struct file_operations pc87413_fops = {
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200530 .owner = THIS_MODULE,
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200531 .llseek = no_llseek,
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200532 .write = pc87413_write,
533 .ioctl = pc87413_ioctl,
534 .open = pc87413_open,
535 .release = pc87413_release,
536};
537
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200538static struct notifier_block pc87413_notifier =
539{
540 .notifier_call = pc87413_notify_sys,
541};
542
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200543static struct miscdevice pc87413_miscdev=
544{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200545 .minor = WATCHDOG_MINOR,
546 .name = "watchdog",
547 .fops = &pc87413_fops
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200548};
549
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200550/* -- Module init functions -------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200551
552/**
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200553 * pc87413_init: module's "constructor"
554 *
555 * Set up the WDT watchdog board. All we have to do is grab the
556 * resources we require and bitch if anyone beat us to them.
557 * The open() function will actually kick the board off.
558 */
559
560static int __init pc87413_init(void)
561{
562 int ret;
563
564 spin_lock_init(&io_lock);
565
566 printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n", WDT_INDEX_IO_PORT);
567
568 /* request_region(io, 2, "pc87413"); */
569
570 ret = register_reboot_notifier(&pc87413_notifier);
571 if (ret != 0) {
572 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
573 ret);
574 }
575
576 ret = misc_register(&pc87413_miscdev);
577
578 if (ret != 0) {
579 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
580 WATCHDOG_MINOR, ret);
581 unregister_reboot_notifier(&pc87413_notifier);
582 return ret;
583 }
584
585 printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
586
587 pc87413_enable();
588
589 return 0;
590}
591
592/**
593 * pc87413_exit: module's "destructor"
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200594 *
595 * Unload the watchdog. You cannot do this with any file handles open.
596 * If your watchdog is set to continue ticking on close and you unload
597 * it, well it keeps ticking. We won't get the interrupt but the board
598 * will not touch PC memory so all is fine. You just have to load a new
599 * module in 60 seconds or reboot.
600 */
601
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200602static void __exit pc87413_exit(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200603{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200604 /* Stop the timer before we leave */
605 if (!nowayout)
606 {
607 pc87413_disable();
608 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
609 }
610
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200611 misc_deregister(&pc87413_miscdev);
612 unregister_reboot_notifier(&pc87413_notifier);
613 /* release_region(io,2); */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200614
615 printk(MODNAME " watchdog component driver removed.\n");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200616}
617
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200618module_init(pc87413_init);
619module_exit(pc87413_exit);
620
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200621MODULE_AUTHOR("Sven Anders <anders@anduras.de>, Marcus Junker <junker@anduras.de>,");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200622MODULE_DESCRIPTION("PC87413 WDT driver");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200623MODULE_LICENSE("GPL");
624
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200625MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
626
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200627module_param(io, int, 0);
Wim Van Sebroeck0835caa2006-10-23 18:21:52 +0200628MODULE_PARM_DESC(io, MODNAME " I/O port (default: " __MODULE_STRING(io) ").");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200629
630module_param(timeout, int, 0);
631MODULE_PARM_DESC(timeout, "Watchdog timeout in minutes (default=" __MODULE_STRING(timeout) ").");
632
633module_param(nowayout, int, 0);
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +0100634MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200635