blob: 4d5612146cded76629c7aa9132ee39c9387627b4 [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/config.h>
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/miscdevice.h>
25#include <linux/watchdog.h>
26#include <linux/ioport.h>
27#include <linux/delay.h>
28#include <linux/notifier.h>
29#include <linux/fs.h>
30#include <linux/reboot.h>
31#include <linux/init.h>
32#include <linux/spinlock.h>
33#include <linux/moduleparam.h>
34#include <linux/version.h>
35
36#include <asm/io.h>
37#include <asm/uaccess.h>
38#include <asm/system.h>
39
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020040/* #define DEBUG 1 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020041
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020042#define DEFAULT_TIMEOUT 1 /* 1 minute */
43#define MAX_TIMEOUT 255
44
45#define VERSION "1.1"
46#define MODNAME "pc87413 WDT"
47#define PFX MODNAME ": "
48#define DPFX MODNAME " - DEBUG: "
49
50#define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020051#define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020052#define SWC_LDN 0x04
53#define SIOCFG2 0x22 /* Serial IO register */
54#define WDCTL 0x10 /* Watchdog-Timer-Controll-Register */
55#define WDTO 0x11 /* Watchdog timeout register */
56#define WDCFG 0x12 /* Watchdog config register */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020057
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020058static int io = 0x2E; /* Address used on Portwell Boards */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020059
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020060static int timeout = DEFAULT_TIMEOUT; /* timeout value */
61static unsigned long timer_enabled = 0; /* is the timer enabled? */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020062
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020063static char expect_close; /* is the close expected? */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020064
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020065static spinlock_t io_lock; /* to guard the watchdog from io races */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020066
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020067static int nowayout = WATCHDOG_NOWAYOUT;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020068
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020069/* -- Low level function ----------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020070
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020071/* Select pins for Watchdog output */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020072
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020073static inline void pc87413_select_wdt_out (void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020074{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020075 unsigned int cr_data = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020076
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020077 /* Step 1: Select multiple pin,pin55,as WDT output */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020078
79 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
80
81 cr_data = inb (WDT_DATA_IO_PORT);
82
83 cr_data |= 0x80; /* Set Bit7 to 1*/
84 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
85
86 outb_p(cr_data, WDT_DATA_IO_PORT);
87
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020088#ifdef DEBUG
89 printk(KERN_INFO DPFX "Select multiple pin,pin55,as WDT output:"
90 " Bit7 to 1: %d\n", cr_data);
91#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020092}
93
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020094/* Enable SWC functions */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020095
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020096static inline void pc87413_enable_swc(void)
97{
98 unsigned int cr_data=0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020099
100 /* Step 2: Enable SWC functions */
101
102 outb_p(0x07, WDT_INDEX_IO_PORT); /* Point SWC_LDN (LDN=4) */
103 outb_p(SWC_LDN, WDT_DATA_IO_PORT);
104
105 outb_p(0x30, WDT_INDEX_IO_PORT); /* Read Index 0x30 First */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200106 cr_data = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200107 cr_data |= 0x01; /* Set Bit0 to 1 */
108 outb_p(0x30, WDT_INDEX_IO_PORT);
109 outb_p(cr_data, WDT_DATA_IO_PORT); /* Index0x30_bit0P1 */
110
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200111#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200112 printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200113#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200114}
115
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200116/* Read SWC I/O base address */
117
118static inline unsigned int pc87413_get_swc_base(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200119{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200120 unsigned int swc_base_addr = 0;
121 unsigned char addr_l, addr_h = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200122
123 /* Step 3: Read SWC I/O Base Address */
124
125 outb_p(0x60, WDT_INDEX_IO_PORT); /* Read Index 0x60 */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200126 addr_h = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200127
128 outb_p(0x61, WDT_INDEX_IO_PORT); /* Read Index 0x61 */
129
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200130 addr_l = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200131
132 swc_base_addr = (addr_h << 8) + addr_l;
133
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200134#ifdef DEBUG
135 printk(KERN_INFO DPFX "Read SWC I/O Base Address: low %d, high %d,"
136 " res %d\n", addr_l, addr_h, swc_base_addr);
137#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200138
139 return swc_base_addr;
140}
141
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200142/* Select Bank 3 of SWC */
143
144static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200145{
146 /* Step 4: Select Bank3 of SWC */
147
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200148 outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200149
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200150#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200151 printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200152#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200153}
154
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200155/* Set watchdog timeout to x minutes */
156
157static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
158 char pc87413_time)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200159{
160 /* Step 5: Programm WDTO, Twd. */
161
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200162 outb_p(pc87413_time, swc_base_addr + WDTO);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200163
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200164#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200165 printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200166#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200167}
168
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200169/* Enable WDEN */
170
171static inline void pc87413_enable_wden(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200172{
173 /* Step 6: Enable WDEN */
174
175 outb_p(inb (swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
176
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200177#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200178 printk(KERN_INFO DPFX "Enable WDEN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200179#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200180}
181
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200182/* Enable SW_WD_TREN */
183static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200184{
185 /* Enable SW_WD_TREN */
186
187 outb_p(inb (swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
188
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200189#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200190 printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200191#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200192}
193
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200194/* Disable SW_WD_TREN */
195
196static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200197{
198 /* Disable SW_WD_TREN */
199
200 outb_p(inb (swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
201
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200202#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200203 printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200204#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200205}
206
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200207/* Enable SW_WD_TRG */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200208
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200209static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200210{
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200211 /* Enable SW_WD_TRG */
212
213 outb_p(inb (swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
214
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200215#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200216 printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200217#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200218}
219
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200220/* Disable SW_WD_TRG */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200221
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200222static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
223{
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200224 /* Disable SW_WD_TRG */
225
226 outb_p(inb (swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
227
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200228#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200229 printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200230#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200231}
232
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200233/* -- Higher level functions ------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200234
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200235/* Enable the watchdog */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200236
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200237static void pc87413_enable(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200238{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200239 unsigned int swc_base_addr;
240
241 spin_lock(&io_lock);
242
243 pc87413_select_wdt_out();
244 pc87413_enable_swc();
245 swc_base_addr = pc87413_get_swc_base();
246 pc87413_swc_bank3(swc_base_addr);
247 pc87413_programm_wdto(swc_base_addr, timeout);
248 pc87413_enable_wden(swc_base_addr);
249 pc87413_enable_sw_wd_tren(swc_base_addr);
250 pc87413_enable_sw_wd_trg(swc_base_addr);
251
252 spin_unlock(&io_lock);
253}
254
255/* Disable the watchdog */
256
257static void pc87413_disable(void)
258{
259 unsigned int swc_base_addr;
260
261 spin_lock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200262
263 pc87413_select_wdt_out();
264 pc87413_enable_swc();
265 swc_base_addr = pc87413_get_swc_base();
266 pc87413_swc_bank3(swc_base_addr);
267 pc87413_disable_sw_wd_tren(swc_base_addr);
268 pc87413_disable_sw_wd_trg(swc_base_addr);
269 pc87413_programm_wdto(swc_base_addr, 0);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200270
271 spin_unlock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200272}
273
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200274/* Refresh the watchdog */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200275
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200276static void pc87413_refresh(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200277{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200278 unsigned int swc_base_addr;
279
280 spin_lock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200281
282 pc87413_select_wdt_out();
283 pc87413_enable_swc();
284 swc_base_addr = pc87413_get_swc_base();
285 pc87413_swc_bank3(swc_base_addr);
286 pc87413_disable_sw_wd_tren(swc_base_addr);
287 pc87413_disable_sw_wd_trg(swc_base_addr);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200288 pc87413_programm_wdto(swc_base_addr, timeout);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200289 pc87413_enable_wden(swc_base_addr);
290 pc87413_enable_sw_wd_tren(swc_base_addr);
291 pc87413_enable_sw_wd_trg(swc_base_addr);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200292
293 spin_unlock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200294}
295
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200296/* -- File operations -------------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200297
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200298/**
299 * pc87413_open:
300 * @inode: inode of device
301 * @file: file handle to device
302 *
303 */
304
305static int pc87413_open(struct inode *inode, struct file *file)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200306{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200307 /* /dev/watchdog can only be opened once */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200308
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200309 if (test_and_set_bit(0, &timer_enabled))
310 return -EBUSY;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200311
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200312 if (nowayout)
313 __module_get(THIS_MODULE);
314
315 /* Reload and activate timer */
316 pc87413_refresh();
317
318 printk(KERN_INFO MODNAME "Watchdog enabled. Timeout set to"
319 " %d minute(s).\n", timeout);
320
321 return nonseekable_open(inode, file);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200322}
323
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200324/**
325 * pc87413_release:
326 * @inode: inode to board
327 * @file: file handle to board
328 *
329 * The watchdog has a configurable API. There is a religious dispute
330 * between people who want their watchdog to be able to shut down and
331 * those who want to be sure if the watchdog manager dies the machine
332 * reboots. In the former case we disable the counters, in the latter
333 * case you have to open it again very soon.
334 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200335
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200336static int pc87413_release(struct inode *inode, struct file *file)
337{
338 /* Shut off the timer. */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200339
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200340 if (expect_close == 42) {
341 pc87413_disable();
342 printk(KERN_INFO MODNAME "Watchdog disabled,"
343 " sleeping again...\n");
344 } else {
345 printk(KERN_CRIT MODNAME "Unexpected close, not stopping"
346 " watchdog!\n");
347 pc87413_refresh();
348 }
349
350 clear_bit(0, &timer_enabled);
351 expect_close = 0;
352
353 return 0;
354}
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200355
356/**
357 * pc87413_status:
358 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200359 * return, if the watchdog is enabled (timeout is set...)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200360 */
361
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200362
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200363static int pc87413_status(void)
364{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200365 return 1; /* currently not supported */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200366}
367
368/**
369 * pc87413_write:
370 * @file: file handle to the watchdog
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200371 * @data: data buffer to write
372 * @len: length in bytes
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200373 * @ppos: pointer to the position to write. No seeks allowed
374 *
375 * A write to a watchdog device is defined as a keepalive signal. Any
376 * write of data will do, as we we don't define content meaning.
377 */
378
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200379static ssize_t pc87413_write(struct file *file, const char __user *data,
380 size_t len, loff_t *ppos)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200381{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200382 /* See if we got the magic character 'V' and reload the timer */
383 if (len) {
384 if (!nowayout) {
385 size_t i;
386
387 /* reset expect flag */
388 expect_close = 0;
389
390 /* scan to see whether or not we got the magic character */
391 for (i = 0; i != len; i++) {
392 char c;
393 if (get_user(c, data+i))
394 return -EFAULT;
395 if (c == 'V')
396 expect_close = 42;
397 }
398 }
399
400 /* someone wrote to us, we should reload the timer */
401 pc87413_refresh();
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200402 }
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200403 return len;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200404}
405
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200406/**
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200407 * pc87413_ioctl:
408 * @inode: inode of the device
409 * @file: file handle to the device
410 * @cmd: watchdog command
411 * @arg: argument pointer
412 *
413 * The watchdog API defines a common set of functions for all watchdogs
414 * according to their available features. We only actually usefully support
415 * querying capabilities and current status.
416 */
417
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200418static int pc87413_ioctl(struct inode *inode, struct file *file,
419 unsigned int cmd, unsigned long arg)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200420{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200421 int new_timeout;
422
423 union {
424 struct watchdog_info __user *ident;
425 int __user *i;
426 } uarg;
427
428 static struct watchdog_info ident = {
429 .options = WDIOF_KEEPALIVEPING |
430 WDIOF_SETTIMEOUT |
431 WDIOF_MAGICCLOSE,
432 .firmware_version = 1,
433 .identity = "PC87413(HF/F) watchdog"
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200434 };
435
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200436 uarg.i = (int __user *)arg;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200437
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200438 switch(cmd) {
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200439 default:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200440 return -ENOTTY;
441
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200442 case WDIOC_GETSUPPORT:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200443 return copy_to_user(uarg.ident, &ident,
444 sizeof(ident)) ? -EFAULT : 0;
445
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200446 case WDIOC_GETSTATUS:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200447 return put_user(pc87413_status(), uarg.i);
448
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200449 case WDIOC_GETBOOTSTATUS:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200450 return put_user(0, uarg.i);
451
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200452 case WDIOC_KEEPALIVE:
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200453 pc87413_refresh();
454#ifdef DEBUG
455 printk(KERN_INFO DPFX "keepalive\n");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200456#endif
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200457 return 0;
458
459 case WDIOC_SETTIMEOUT:
460 if (get_user(new_timeout, uarg.i))
461 return -EFAULT;
462
463 // the API states this is given in secs
464 new_timeout /= 60;
465
466 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
467 return -EINVAL;
468
469 timeout = new_timeout;
470 pc87413_refresh();
471
472 // fall through and return the new timeout...
473
474 case WDIOC_GETTIMEOUT:
475
476 new_timeout = timeout * 60;
477
478 return put_user(new_timeout, uarg.i);
479
480 case WDIOC_SETOPTIONS:
481 {
482 int options, retval = -EINVAL;
483
484 if (get_user(options, uarg.i))
485 return -EFAULT;
486
487 if (options & WDIOS_DISABLECARD) {
488 pc87413_disable();
489 retval = 0;
490 }
491
492 if (options & WDIOS_ENABLECARD) {
493 pc87413_enable();
494 retval = 0;
495 }
496
497 return retval;
498 }
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200499 }
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200500}
501
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200502/* -- Notifier funtions -----------------------------------------*/
503
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200504/**
505 * notify_sys:
506 * @this: our notifier block
507 * @code: the event being reported
508 * @unused: unused
509 *
510 * Our notifier is called on system shutdowns. We want to turn the card
511 * off at reboot otherwise the machine will reboot again during memory
512 * test or worse yet during the following fsck. This would suck, in fact
513 * trust me - if it happens it does suck.
514 */
515
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200516static int pc87413_notify_sys(struct notifier_block *this,
517 unsigned long code,
518 void *unused)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200519{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200520 if (code == SYS_DOWN || code == SYS_HALT)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200521 {
522 /* Turn the card off */
523 pc87413_disable();
524 }
525 return NOTIFY_DONE;
526}
527
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200528/* -- Module's structures ---------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200529
530static struct file_operations pc87413_fops = {
531 .owner = THIS_MODULE,
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200532 .llseek = no_llseek,
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200533 .write = pc87413_write,
534 .ioctl = pc87413_ioctl,
535 .open = pc87413_open,
536 .release = pc87413_release,
537};
538
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200539static struct notifier_block pc87413_notifier =
540{
541 .notifier_call = pc87413_notify_sys,
542};
543
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200544static struct miscdevice pc87413_miscdev=
545{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200546 .minor = WATCHDOG_MINOR,
547 .name = "watchdog",
548 .fops = &pc87413_fops
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200549};
550
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200551/* -- Module init functions -------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200552
553/**
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200554 * pc87413_init: module's "constructor"
555 *
556 * Set up the WDT watchdog board. All we have to do is grab the
557 * resources we require and bitch if anyone beat us to them.
558 * The open() function will actually kick the board off.
559 */
560
561static int __init pc87413_init(void)
562{
563 int ret;
564
565 spin_lock_init(&io_lock);
566
567 printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n", WDT_INDEX_IO_PORT);
568
569 /* request_region(io, 2, "pc87413"); */
570
571 ret = register_reboot_notifier(&pc87413_notifier);
572 if (ret != 0) {
573 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
574 ret);
575 }
576
577 ret = misc_register(&pc87413_miscdev);
578
579 if (ret != 0) {
580 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
581 WATCHDOG_MINOR, ret);
582 unregister_reboot_notifier(&pc87413_notifier);
583 return ret;
584 }
585
586 printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
587
588 pc87413_enable();
589
590 return 0;
591}
592
593/**
594 * pc87413_exit: module's "destructor"
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200595 *
596 * Unload the watchdog. You cannot do this with any file handles open.
597 * If your watchdog is set to continue ticking on close and you unload
598 * it, well it keeps ticking. We won't get the interrupt but the board
599 * will not touch PC memory so all is fine. You just have to load a new
600 * module in 60 seconds or reboot.
601 */
602
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200603static void __exit pc87413_exit(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200604{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200605 /* Stop the timer before we leave */
606 if (!nowayout)
607 {
608 pc87413_disable();
609 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
610 }
611
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200612 misc_deregister(&pc87413_miscdev);
613 unregister_reboot_notifier(&pc87413_notifier);
614 /* release_region(io,2); */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200615
616 printk(MODNAME " watchdog component driver removed.\n");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200617}
618
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200619module_init(pc87413_init);
620module_exit(pc87413_exit);
621
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200622MODULE_AUTHOR("Sven Anders <anders@anduras.de>, Marcus Junker <junker@anduras.de>,");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200623MODULE_DESCRIPTION("PC87413 WDT driver");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200624MODULE_LICENSE("GPL");
625
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200626MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
627
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200628module_param(io, int, 0);
629MODULE_PARM_DESC(wdt_io, MODNAME " I/O port (default: " __MODULE_STRING(io) ").");
630
631module_param(timeout, int, 0);
632MODULE_PARM_DESC(timeout, "Watchdog timeout in minutes (default=" __MODULE_STRING(timeout) ").");
633
634module_param(nowayout, int, 0);
635MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
636