blob: 3a56bc3609247f4fbb6e8fe5fffd5bacb2e209d1 [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>
Alan Coxaee334c2008-05-19 14:07:37 +010033#include <linux/io.h>
34#include <linux/uaccess.h>
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020035
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020036#include <asm/system.h>
37
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020038/* #define DEBUG 1 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020039
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000040#define DEFAULT_TIMEOUT 1 /* 1 minute */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020041#define MAX_TIMEOUT 255
42
43#define VERSION "1.1"
44#define MODNAME "pc87413 WDT"
45#define PFX MODNAME ": "
46#define DPFX MODNAME " - DEBUG: "
47
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000048#define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020049#define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020050#define SWC_LDN 0x04
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000051#define SIOCFG2 0x22 /* Serial IO register */
52#define WDCTL 0x10 /* Watchdog-Timer-Controll-Register */
53#define WDTO 0x11 /* Watchdog timeout register */
54#define WDCFG 0x12 /* Watchdog config register */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020055
Randy Dunlap76550d32010-05-01 09:46:15 -070056#define IO_DEFAULT 0x2E /* Address used on Portwell Boards */
57
58static int io = IO_DEFAULT;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020059
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000060static int timeout = DEFAULT_TIMEOUT; /* timeout value */
Alan Coxaee334c2008-05-19 14:07:37 +010061static unsigned long timer_enabled; /* is the timer enabled? */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020062
Alan Coxaee334c2008-05-19 14:07:37 +010063static char expect_close; /* is the close expected? */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020064
Alan Coxaee334c2008-05-19 14:07:37 +010065static DEFINE_SPINLOCK(io_lock); /* to guard us 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
Alan Coxaee334c2008-05-19 14:07:37 +010073static 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
Alan Coxaee334c2008-05-19 14:07:37 +010081 cr_data = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020082
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
Alan Coxaee334c2008-05-19 14:07:37 +010089 printk(KERN_INFO DPFX
90 "Select multiple pin,pin55,as WDT output: Bit7 to 1: %d\n",
91 cr_data);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020092#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020093}
94
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020095/* Enable SWC functions */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020096
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020097static inline void pc87413_enable_swc(void)
98{
Alan Coxaee334c2008-05-19 14:07:37 +010099 unsigned int cr_data = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200100
101 /* Step 2: Enable SWC functions */
102
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000103 outb_p(0x07, WDT_INDEX_IO_PORT); /* Point SWC_LDN (LDN=4) */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200104 outb_p(SWC_LDN, WDT_DATA_IO_PORT);
105
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000106 outb_p(0x30, WDT_INDEX_IO_PORT); /* Read Index 0x30 First */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200107 cr_data = inb(WDT_DATA_IO_PORT);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000108 cr_data |= 0x01; /* Set Bit0 to 1 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200109 outb_p(0x30, WDT_INDEX_IO_PORT);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000110 outb_p(cr_data, WDT_DATA_IO_PORT); /* Index0x30_bit0P1 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200111
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200112#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200113 printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200114#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200115}
116
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200117/* Read SWC I/O base address */
118
119static inline unsigned int pc87413_get_swc_base(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200120{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200121 unsigned int swc_base_addr = 0;
122 unsigned char addr_l, addr_h = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200123
124 /* Step 3: Read SWC I/O Base Address */
125
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000126 outb_p(0x60, WDT_INDEX_IO_PORT); /* Read Index 0x60 */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200127 addr_h = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200128
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000129 outb_p(0x61, WDT_INDEX_IO_PORT); /* Read Index 0x61 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200130
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200131 addr_l = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200132
133 swc_base_addr = (addr_h << 8) + addr_l;
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200134#ifdef DEBUG
Alan Coxaee334c2008-05-19 14:07:37 +0100135 printk(KERN_INFO DPFX
136 "Read SWC I/O Base Address: low %d, high %d, res %d\n",
137 addr_l, addr_h, swc_base_addr);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200138#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200139 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 */
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 Junker00b3b3e2006-10-16 18:18:09 +0200148#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200149 printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200150#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200151}
152
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200153/* Set watchdog timeout to x minutes */
154
155static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
156 char pc87413_time)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200157{
158 /* Step 5: Programm WDTO, Twd. */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200159 outb_p(pc87413_time, swc_base_addr + WDTO);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200160#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200161 printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200162#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200163}
164
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200165/* Enable WDEN */
166
167static inline void pc87413_enable_wden(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200168{
169 /* Step 6: Enable WDEN */
Alan Coxaee334c2008-05-19 14:07:37 +0100170 outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200171#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200172 printk(KERN_INFO DPFX "Enable WDEN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200173#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200174}
175
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200176/* Enable SW_WD_TREN */
177static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200178{
179 /* Enable SW_WD_TREN */
Alan Coxaee334c2008-05-19 14:07:37 +0100180 outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200181#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200182 printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200183#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200184}
185
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200186/* Disable SW_WD_TREN */
187
188static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200189{
190 /* Disable SW_WD_TREN */
Alan Coxaee334c2008-05-19 14:07:37 +0100191 outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200192#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200193 printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200194#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200195}
196
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200197/* Enable SW_WD_TRG */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200198
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200199static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200200{
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200201 /* Enable SW_WD_TRG */
Alan Coxaee334c2008-05-19 14:07:37 +0100202 outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200203#ifdef DEBUG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200204 printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200205#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200206}
207
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200208/* Disable SW_WD_TRG */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200209
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200210static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
211{
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200212 /* Disable SW_WD_TRG */
Alan Coxaee334c2008-05-19 14:07:37 +0100213 outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
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 "Disable 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/* -- Higher level functions ------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200220
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200221/* Enable the watchdog */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200222
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200223static void pc87413_enable(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200224{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200225 unsigned int swc_base_addr;
226
227 spin_lock(&io_lock);
228
229 pc87413_select_wdt_out();
230 pc87413_enable_swc();
231 swc_base_addr = pc87413_get_swc_base();
232 pc87413_swc_bank3(swc_base_addr);
233 pc87413_programm_wdto(swc_base_addr, timeout);
234 pc87413_enable_wden(swc_base_addr);
235 pc87413_enable_sw_wd_tren(swc_base_addr);
236 pc87413_enable_sw_wd_trg(swc_base_addr);
237
238 spin_unlock(&io_lock);
239}
240
241/* Disable the watchdog */
242
243static void pc87413_disable(void)
244{
245 unsigned int swc_base_addr;
246
247 spin_lock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200248
249 pc87413_select_wdt_out();
250 pc87413_enable_swc();
251 swc_base_addr = pc87413_get_swc_base();
252 pc87413_swc_bank3(swc_base_addr);
253 pc87413_disable_sw_wd_tren(swc_base_addr);
254 pc87413_disable_sw_wd_trg(swc_base_addr);
255 pc87413_programm_wdto(swc_base_addr, 0);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200256
257 spin_unlock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200258}
259
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200260/* Refresh the watchdog */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200261
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200262static void pc87413_refresh(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200263{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200264 unsigned int swc_base_addr;
265
266 spin_lock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200267
268 pc87413_select_wdt_out();
269 pc87413_enable_swc();
270 swc_base_addr = pc87413_get_swc_base();
271 pc87413_swc_bank3(swc_base_addr);
272 pc87413_disable_sw_wd_tren(swc_base_addr);
273 pc87413_disable_sw_wd_trg(swc_base_addr);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200274 pc87413_programm_wdto(swc_base_addr, timeout);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200275 pc87413_enable_wden(swc_base_addr);
276 pc87413_enable_sw_wd_tren(swc_base_addr);
277 pc87413_enable_sw_wd_trg(swc_base_addr);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200278
279 spin_unlock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200280}
281
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200282/* -- File operations -------------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200283
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200284/**
285 * pc87413_open:
286 * @inode: inode of device
287 * @file: file handle to device
288 *
289 */
290
291static int pc87413_open(struct inode *inode, struct file *file)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200292{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200293 /* /dev/watchdog can only be opened once */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200294
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200295 if (test_and_set_bit(0, &timer_enabled))
296 return -EBUSY;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200297
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200298 if (nowayout)
299 __module_get(THIS_MODULE);
300
301 /* Reload and activate timer */
302 pc87413_refresh();
303
Alan Coxaee334c2008-05-19 14:07:37 +0100304 printk(KERN_INFO MODNAME
305 "Watchdog enabled. Timeout set to %d minute(s).\n", timeout);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200306
307 return nonseekable_open(inode, file);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200308}
309
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200310/**
311 * pc87413_release:
312 * @inode: inode to board
313 * @file: file handle to board
314 *
315 * The watchdog has a configurable API. There is a religious dispute
316 * between people who want their watchdog to be able to shut down and
317 * those who want to be sure if the watchdog manager dies the machine
318 * reboots. In the former case we disable the counters, in the latter
319 * case you have to open it again very soon.
320 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200321
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200322static int pc87413_release(struct inode *inode, struct file *file)
323{
324 /* Shut off the timer. */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200325
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200326 if (expect_close == 42) {
327 pc87413_disable();
Alan Coxaee334c2008-05-19 14:07:37 +0100328 printk(KERN_INFO MODNAME
329 "Watchdog disabled, sleeping again...\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200330 } else {
Alan Coxaee334c2008-05-19 14:07:37 +0100331 printk(KERN_CRIT MODNAME
332 "Unexpected close, not stopping watchdog!\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200333 pc87413_refresh();
334 }
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200335 clear_bit(0, &timer_enabled);
336 expect_close = 0;
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200337 return 0;
338}
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200339
340/**
341 * pc87413_status:
342 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200343 * return, if the watchdog is enabled (timeout is set...)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200344 */
345
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200346
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200347static int pc87413_status(void)
348{
Wim Van Sebroeck0835caa2006-10-23 18:21:52 +0200349 return 0; /* currently not supported */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200350}
351
352/**
353 * pc87413_write:
354 * @file: file handle to the watchdog
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200355 * @data: data buffer to write
356 * @len: length in bytes
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200357 * @ppos: pointer to the position to write. No seeks allowed
358 *
359 * A write to a watchdog device is defined as a keepalive signal. Any
360 * write of data will do, as we we don't define content meaning.
361 */
362
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200363static ssize_t pc87413_write(struct file *file, const char __user *data,
364 size_t len, loff_t *ppos)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200365{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200366 /* See if we got the magic character 'V' and reload the timer */
367 if (len) {
368 if (!nowayout) {
369 size_t i;
370
371 /* reset expect flag */
372 expect_close = 0;
373
Alan Coxaee334c2008-05-19 14:07:37 +0100374 /* scan to see whether or not we got the
375 magic character */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200376 for (i = 0; i != len; i++) {
377 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000378 if (get_user(c, data + i))
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200379 return -EFAULT;
380 if (c == 'V')
381 expect_close = 42;
382 }
383 }
384
385 /* someone wrote to us, we should reload the timer */
386 pc87413_refresh();
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200387 }
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200388 return len;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200389}
390
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200391/**
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200392 * pc87413_ioctl:
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200393 * @file: file handle to the device
394 * @cmd: watchdog command
395 * @arg: argument pointer
396 *
397 * The watchdog API defines a common set of functions for all watchdogs
398 * according to their available features. We only actually usefully support
399 * querying capabilities and current status.
400 */
401
Alan Coxaee334c2008-05-19 14:07:37 +0100402static long pc87413_ioctl(struct file *file, unsigned int cmd,
403 unsigned long arg)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200404{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200405 int new_timeout;
406
407 union {
408 struct watchdog_info __user *ident;
409 int __user *i;
410 } uarg;
411
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000412 static const struct watchdog_info ident = {
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200413 .options = WDIOF_KEEPALIVEPING |
Alan Coxaee334c2008-05-19 14:07:37 +0100414 WDIOF_SETTIMEOUT |
415 WDIOF_MAGICCLOSE,
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200416 .firmware_version = 1,
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000417 .identity = "PC87413(HF/F) watchdog",
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200418 };
419
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200420 uarg.i = (int __user *)arg;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200421
Alan Coxaee334c2008-05-19 14:07:37 +0100422 switch (cmd) {
423 case WDIOC_GETSUPPORT:
424 return copy_to_user(uarg.ident, &ident,
425 sizeof(ident)) ? -EFAULT : 0;
426 case WDIOC_GETSTATUS:
427 return put_user(pc87413_status(), uarg.i);
428 case WDIOC_GETBOOTSTATUS:
429 return put_user(0, uarg.i);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000430 case WDIOC_SETOPTIONS:
431 {
432 int options, retval = -EINVAL;
433 if (get_user(options, uarg.i))
434 return -EFAULT;
435 if (options & WDIOS_DISABLECARD) {
436 pc87413_disable();
437 retval = 0;
438 }
439 if (options & WDIOS_ENABLECARD) {
440 pc87413_enable();
441 retval = 0;
442 }
443 return retval;
444 }
Alan Coxaee334c2008-05-19 14:07:37 +0100445 case WDIOC_KEEPALIVE:
446 pc87413_refresh();
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200447#ifdef DEBUG
Alan Coxaee334c2008-05-19 14:07:37 +0100448 printk(KERN_INFO DPFX "keepalive\n");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200449#endif
Alan Coxaee334c2008-05-19 14:07:37 +0100450 return 0;
451 case WDIOC_SETTIMEOUT:
452 if (get_user(new_timeout, uarg.i))
453 return -EFAULT;
454 /* the API states this is given in secs */
455 new_timeout /= 60;
456 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
457 return -EINVAL;
458 timeout = new_timeout;
459 pc87413_refresh();
460 /* fall through and return the new timeout... */
461 case WDIOC_GETTIMEOUT:
462 new_timeout = timeout * 60;
463 return put_user(new_timeout, uarg.i);
Alan Coxaee334c2008-05-19 14:07:37 +0100464 default:
465 return -ENOTTY;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200466 }
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200467}
468
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200469/* -- Notifier funtions -----------------------------------------*/
470
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200471/**
472 * notify_sys:
473 * @this: our notifier block
474 * @code: the event being reported
475 * @unused: unused
476 *
477 * Our notifier is called on system shutdowns. We want to turn the card
478 * off at reboot otherwise the machine will reboot again during memory
479 * test or worse yet during the following fsck. This would suck, in fact
480 * trust me - if it happens it does suck.
481 */
482
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200483static int pc87413_notify_sys(struct notifier_block *this,
484 unsigned long code,
485 void *unused)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200486{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200487 if (code == SYS_DOWN || code == SYS_HALT)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200488 /* Turn the card off */
489 pc87413_disable();
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200490 return NOTIFY_DONE;
491}
492
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200493/* -- Module's structures ---------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200494
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800495static const struct file_operations pc87413_fops = {
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200496 .owner = THIS_MODULE,
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200497 .llseek = no_llseek,
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200498 .write = pc87413_write,
Alan Coxaee334c2008-05-19 14:07:37 +0100499 .unlocked_ioctl = pc87413_ioctl,
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200500 .open = pc87413_open,
501 .release = pc87413_release,
502};
503
Alan Coxaee334c2008-05-19 14:07:37 +0100504static struct notifier_block pc87413_notifier = {
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200505 .notifier_call = pc87413_notify_sys,
506};
507
Alan Coxaee334c2008-05-19 14:07:37 +0100508static struct miscdevice pc87413_miscdev = {
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200509 .minor = WATCHDOG_MINOR,
510 .name = "watchdog",
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000511 .fops = &pc87413_fops,
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200512};
513
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200514/* -- Module init functions -------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200515
516/**
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200517 * pc87413_init: module's "constructor"
518 *
519 * Set up the WDT watchdog board. All we have to do is grab the
520 * resources we require and bitch if anyone beat us to them.
521 * The open() function will actually kick the board off.
522 */
523
524static int __init pc87413_init(void)
525{
526 int ret;
527
Alan Coxaee334c2008-05-19 14:07:37 +0100528 printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n",
529 WDT_INDEX_IO_PORT);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200530
531 /* request_region(io, 2, "pc87413"); */
532
533 ret = register_reboot_notifier(&pc87413_notifier);
534 if (ret != 0) {
Alan Coxaee334c2008-05-19 14:07:37 +0100535 printk(KERN_ERR PFX
536 "cannot register reboot notifier (err=%d)\n", ret);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200537 }
538
539 ret = misc_register(&pc87413_miscdev);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200540 if (ret != 0) {
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000541 printk(KERN_ERR PFX
542 "cannot register miscdev on minor=%d (err=%d)\n",
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200543 WATCHDOG_MINOR, ret);
544 unregister_reboot_notifier(&pc87413_notifier);
545 return ret;
546 }
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200547 printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200548 pc87413_enable();
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200549 return 0;
550}
551
552/**
553 * pc87413_exit: module's "destructor"
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200554 *
555 * Unload the watchdog. You cannot do this with any file handles open.
556 * If your watchdog is set to continue ticking on close and you unload
557 * it, well it keeps ticking. We won't get the interrupt but the board
558 * will not touch PC memory so all is fine. You just have to load a new
559 * module in 60 seconds or reboot.
560 */
561
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200562static void __exit pc87413_exit(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200563{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200564 /* Stop the timer before we leave */
Alan Coxaee334c2008-05-19 14:07:37 +0100565 if (!nowayout) {
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200566 pc87413_disable();
567 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
568 }
569
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200570 misc_deregister(&pc87413_miscdev);
571 unregister_reboot_notifier(&pc87413_notifier);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000572 /* release_region(io, 2); */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200573
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000574 printk(KERN_INFO MODNAME " watchdog component driver removed.\n");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200575}
576
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200577module_init(pc87413_init);
578module_exit(pc87413_exit);
579
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000580MODULE_AUTHOR("Sven Anders <anders@anduras.de>, "
581 "Marcus Junker <junker@anduras.de>,");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200582MODULE_DESCRIPTION("PC87413 WDT driver");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200583MODULE_LICENSE("GPL");
584
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200585MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
586
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200587module_param(io, int, 0);
Randy Dunlap76550d32010-05-01 09:46:15 -0700588MODULE_PARM_DESC(io, MODNAME " I/O port (default: "
589 __MODULE_STRING(IO_DEFAULT) ").");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200590
591module_param(timeout, int, 0);
Alan Coxaee334c2008-05-19 14:07:37 +0100592MODULE_PARM_DESC(timeout,
593 "Watchdog timeout in minutes (default="
Randy Dunlap76550d32010-05-01 09:46:15 -0700594 __MODULE_STRING(DEFAULT_TIMEOUT) ").");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200595
596module_param(nowayout, int, 0);
Alan Coxaee334c2008-05-19 14:07:37 +0100597MODULE_PARM_DESC(nowayout,
598 "Watchdog cannot be stopped once started (default="
599 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200600