blob: 9f9a340427fc9f536841c79df2e1b75062d40593 [file] [log] [blame]
Guenter Roeckd0173272019-06-20 09:28:46 -07001// SPDX-License-Identifier: GPL-2.0+
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +02002/*
3 * NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
4 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +02005 * This code is based on wdt.c with original copyright.
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +02006 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +02007 * (C) Copyright 2006 Sven Anders, <anders@anduras.de>
8 * and Marcus Junker, <junker@anduras.de>
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +02009 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020010 * Neither Sven Anders, Marcus Junker nor ANDURAS AG
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020011 * admit liability nor provide warranty for any of this software.
12 * This material is provided "AS-IS" and at no charge.
13 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020014 * Release 1.1
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020015 */
16
Joe Perches27c766a2012-02-15 15:06:19 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020019#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/miscdevice.h>
22#include <linux/watchdog.h>
23#include <linux/ioport.h>
24#include <linux/delay.h>
25#include <linux/notifier.h>
26#include <linux/fs.h>
27#include <linux/reboot.h>
28#include <linux/init.h>
29#include <linux/spinlock.h>
30#include <linux/moduleparam.h>
Alan Coxaee334c2008-05-19 14:07:37 +010031#include <linux/io.h>
32#include <linux/uaccess.h>
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020033
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020034
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020035/* #define DEBUG 1 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020036
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000037#define DEFAULT_TIMEOUT 1 /* 1 minute */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020038#define MAX_TIMEOUT 255
39
40#define VERSION "1.1"
41#define MODNAME "pc87413 WDT"
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020042#define DPFX MODNAME " - DEBUG: "
43
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000044#define WDT_INDEX_IO_PORT (io+0) /* I/O port base (index register) */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020045#define WDT_DATA_IO_PORT (WDT_INDEX_IO_PORT+1)
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020046#define SWC_LDN 0x04
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000047#define SIOCFG2 0x22 /* Serial IO register */
Lucas De Marchi25985ed2011-03-30 22:57:33 -030048#define WDCTL 0x10 /* Watchdog-Timer-Control-Register */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000049#define WDTO 0x11 /* Watchdog timeout register */
50#define WDCFG 0x12 /* Watchdog config register */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020051
Randy Dunlap76550d32010-05-01 09:46:15 -070052#define IO_DEFAULT 0x2E /* Address used on Portwell Boards */
53
54static int io = IO_DEFAULT;
Jonathan McDowell7ccdb942011-04-14 12:02:39 -070055static int swc_base_addr = -1;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020056
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000057static int timeout = DEFAULT_TIMEOUT; /* timeout value */
Alan Coxaee334c2008-05-19 14:07:37 +010058static unsigned long timer_enabled; /* is the timer enabled? */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020059
Alan Coxaee334c2008-05-19 14:07:37 +010060static char expect_close; /* is the close expected? */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020061
Alan Coxaee334c2008-05-19 14:07:37 +010062static DEFINE_SPINLOCK(io_lock); /* to guard us from io races */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020063
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010064static bool nowayout = WATCHDOG_NOWAYOUT;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020065
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020066/* -- Low level function ----------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020067
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020068/* Select pins for Watchdog output */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020069
Alan Coxaee334c2008-05-19 14:07:37 +010070static inline void pc87413_select_wdt_out(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020071{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020072 unsigned int cr_data = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020073
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020074 /* Step 1: Select multiple pin,pin55,as WDT output */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020075
76 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
77
Alan Coxaee334c2008-05-19 14:07:37 +010078 cr_data = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020079
80 cr_data |= 0x80; /* Set Bit7 to 1*/
81 outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
82
83 outb_p(cr_data, WDT_DATA_IO_PORT);
84
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020085#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -080086 pr_info(DPFX
Alan Coxaee334c2008-05-19 14:07:37 +010087 "Select multiple pin,pin55,as WDT output: Bit7 to 1: %d\n",
88 cr_data);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020089#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020090}
91
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020092/* Enable SWC functions */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020093
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +020094static inline void pc87413_enable_swc(void)
95{
Alan Coxaee334c2008-05-19 14:07:37 +010096 unsigned int cr_data = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +020097
98 /* Step 2: Enable SWC functions */
99
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000100 outb_p(0x07, WDT_INDEX_IO_PORT); /* Point SWC_LDN (LDN=4) */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200101 outb_p(SWC_LDN, WDT_DATA_IO_PORT);
102
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000103 outb_p(0x30, WDT_INDEX_IO_PORT); /* Read Index 0x30 First */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200104 cr_data = inb(WDT_DATA_IO_PORT);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000105 cr_data |= 0x01; /* Set Bit0 to 1 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200106 outb_p(0x30, WDT_INDEX_IO_PORT);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000107 outb_p(cr_data, WDT_DATA_IO_PORT); /* Index0x30_bit0P1 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200108
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200109#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800110 pr_info(DPFX "pc87413 - Enable SWC functions\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200111#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200112}
113
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200114/* Read SWC I/O base address */
115
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700116static void pc87413_get_swc_base_addr(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200117{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200118 unsigned char addr_l, addr_h = 0;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200119
120 /* Step 3: Read SWC I/O Base Address */
121
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000122 outb_p(0x60, WDT_INDEX_IO_PORT); /* Read Index 0x60 */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200123 addr_h = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200124
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000125 outb_p(0x61, WDT_INDEX_IO_PORT); /* Read Index 0x61 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200126
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200127 addr_l = inb(WDT_DATA_IO_PORT);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200128
129 swc_base_addr = (addr_h << 8) + addr_l;
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200130#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800131 pr_info(DPFX
Alan Coxaee334c2008-05-19 14:07:37 +0100132 "Read SWC I/O Base Address: low %d, high %d, res %d\n",
133 addr_l, addr_h, swc_base_addr);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200134#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200135}
136
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200137/* Select Bank 3 of SWC */
138
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700139static inline void pc87413_swc_bank3(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200140{
141 /* Step 4: Select Bank3 of SWC */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200142 outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200143#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800144 pr_info(DPFX "Select Bank3 of SWC\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200145#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200146}
147
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200148/* Set watchdog timeout to x minutes */
149
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700150static inline void pc87413_programm_wdto(char pc87413_time)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200151{
152 /* Step 5: Programm WDTO, Twd. */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200153 outb_p(pc87413_time, swc_base_addr + WDTO);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200154#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800155 pr_info(DPFX "Set WDTO to %d minutes\n", pc87413_time);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200156#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200157}
158
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200159/* Enable WDEN */
160
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700161static inline void pc87413_enable_wden(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200162{
163 /* Step 6: Enable WDEN */
Alan Coxaee334c2008-05-19 14:07:37 +0100164 outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200165#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800166 pr_info(DPFX "Enable WDEN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200167#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200168}
169
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200170/* Enable SW_WD_TREN */
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700171static inline void pc87413_enable_sw_wd_tren(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200172{
173 /* Enable SW_WD_TREN */
Alan Coxaee334c2008-05-19 14:07:37 +0100174 outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200175#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800176 pr_info(DPFX "Enable SW_WD_TREN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200177#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200178}
179
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200180/* Disable SW_WD_TREN */
181
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700182static inline void pc87413_disable_sw_wd_tren(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200183{
184 /* Disable SW_WD_TREN */
Alan Coxaee334c2008-05-19 14:07:37 +0100185 outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200186#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800187 pr_info(DPFX "pc87413 - Disable SW_WD_TREN\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200188#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200189}
190
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200191/* Enable SW_WD_TRG */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200192
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700193static inline void pc87413_enable_sw_wd_trg(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200194{
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200195 /* Enable SW_WD_TRG */
Alan Coxaee334c2008-05-19 14:07:37 +0100196 outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200197#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800198 pr_info(DPFX "pc87413 - Enable SW_WD_TRG\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200199#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200200}
201
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200202/* Disable SW_WD_TRG */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200203
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700204static inline void pc87413_disable_sw_wd_trg(void)
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200205{
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200206 /* Disable SW_WD_TRG */
Alan Coxaee334c2008-05-19 14:07:37 +0100207 outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200208#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800209 pr_info(DPFX "Disable SW_WD_TRG\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200210#endif
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200211}
212
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200213/* -- Higher level functions ------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200214
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200215/* Enable the watchdog */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200216
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200217static void pc87413_enable(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200218{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200219 spin_lock(&io_lock);
220
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700221 pc87413_swc_bank3();
222 pc87413_programm_wdto(timeout);
223 pc87413_enable_wden();
224 pc87413_enable_sw_wd_tren();
225 pc87413_enable_sw_wd_trg();
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200226
227 spin_unlock(&io_lock);
228}
229
230/* Disable the watchdog */
231
232static void pc87413_disable(void)
233{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200234 spin_lock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200235
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700236 pc87413_swc_bank3();
237 pc87413_disable_sw_wd_tren();
238 pc87413_disable_sw_wd_trg();
239 pc87413_programm_wdto(0);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200240
241 spin_unlock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200242}
243
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200244/* Refresh the watchdog */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200245
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200246static void pc87413_refresh(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200247{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200248 spin_lock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200249
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700250 pc87413_swc_bank3();
251 pc87413_disable_sw_wd_tren();
252 pc87413_disable_sw_wd_trg();
253 pc87413_programm_wdto(timeout);
254 pc87413_enable_wden();
255 pc87413_enable_sw_wd_tren();
256 pc87413_enable_sw_wd_trg();
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200257
258 spin_unlock(&io_lock);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200259}
260
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200261/* -- File operations -------------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200262
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200263/**
264 * pc87413_open:
265 * @inode: inode of device
266 * @file: file handle to device
267 *
268 */
269
270static int pc87413_open(struct inode *inode, struct file *file)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200271{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200272 /* /dev/watchdog can only be opened once */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200273
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200274 if (test_and_set_bit(0, &timer_enabled))
275 return -EBUSY;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200276
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200277 if (nowayout)
278 __module_get(THIS_MODULE);
279
280 /* Reload and activate timer */
281 pc87413_refresh();
282
Joe Perches27c766a2012-02-15 15:06:19 -0800283 pr_info("Watchdog enabled. Timeout set to %d minute(s).\n", timeout);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200284
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300285 return stream_open(inode, file);
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200286}
287
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200288/**
289 * pc87413_release:
290 * @inode: inode to board
291 * @file: file handle to board
292 *
293 * The watchdog has a configurable API. There is a religious dispute
294 * between people who want their watchdog to be able to shut down and
295 * those who want to be sure if the watchdog manager dies the machine
296 * reboots. In the former case we disable the counters, in the latter
297 * case you have to open it again very soon.
298 */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200299
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200300static int pc87413_release(struct inode *inode, struct file *file)
301{
302 /* Shut off the timer. */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200303
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200304 if (expect_close == 42) {
305 pc87413_disable();
Joe Perches27c766a2012-02-15 15:06:19 -0800306 pr_info("Watchdog disabled, sleeping again...\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200307 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800308 pr_crit("Unexpected close, not stopping watchdog!\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200309 pc87413_refresh();
310 }
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200311 clear_bit(0, &timer_enabled);
312 expect_close = 0;
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200313 return 0;
314}
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200315
316/**
317 * pc87413_status:
318 *
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200319 * return, if the watchdog is enabled (timeout is set...)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200320 */
321
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200322
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200323static int pc87413_status(void)
324{
Wim Van Sebroeck0835caa2006-10-23 18:21:52 +0200325 return 0; /* currently not supported */
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200326}
327
328/**
329 * pc87413_write:
330 * @file: file handle to the watchdog
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200331 * @data: data buffer to write
332 * @len: length in bytes
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200333 * @ppos: pointer to the position to write. No seeks allowed
334 *
335 * A write to a watchdog device is defined as a keepalive signal. Any
336 * write of data will do, as we we don't define content meaning.
337 */
338
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200339static ssize_t pc87413_write(struct file *file, const char __user *data,
340 size_t len, loff_t *ppos)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200341{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200342 /* See if we got the magic character 'V' and reload the timer */
343 if (len) {
344 if (!nowayout) {
345 size_t i;
346
347 /* reset expect flag */
348 expect_close = 0;
349
Alan Coxaee334c2008-05-19 14:07:37 +0100350 /* scan to see whether or not we got the
351 magic character */
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200352 for (i = 0; i != len; i++) {
353 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000354 if (get_user(c, data + i))
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200355 return -EFAULT;
356 if (c == 'V')
357 expect_close = 42;
358 }
359 }
360
361 /* someone wrote to us, we should reload the timer */
362 pc87413_refresh();
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200363 }
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200364 return len;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200365}
366
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200367/**
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200368 * pc87413_ioctl:
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200369 * @file: file handle to the device
370 * @cmd: watchdog command
371 * @arg: argument pointer
372 *
373 * The watchdog API defines a common set of functions for all watchdogs
374 * according to their available features. We only actually usefully support
375 * querying capabilities and current status.
376 */
377
Alan Coxaee334c2008-05-19 14:07:37 +0100378static long pc87413_ioctl(struct file *file, unsigned int cmd,
379 unsigned long arg)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200380{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200381 int new_timeout;
382
383 union {
384 struct watchdog_info __user *ident;
385 int __user *i;
386 } uarg;
387
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000388 static const struct watchdog_info ident = {
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200389 .options = WDIOF_KEEPALIVEPING |
Alan Coxaee334c2008-05-19 14:07:37 +0100390 WDIOF_SETTIMEOUT |
391 WDIOF_MAGICCLOSE,
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200392 .firmware_version = 1,
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000393 .identity = "PC87413(HF/F) watchdog",
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200394 };
395
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200396 uarg.i = (int __user *)arg;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200397
Alan Coxaee334c2008-05-19 14:07:37 +0100398 switch (cmd) {
399 case WDIOC_GETSUPPORT:
400 return copy_to_user(uarg.ident, &ident,
401 sizeof(ident)) ? -EFAULT : 0;
402 case WDIOC_GETSTATUS:
403 return put_user(pc87413_status(), uarg.i);
404 case WDIOC_GETBOOTSTATUS:
405 return put_user(0, uarg.i);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000406 case WDIOC_SETOPTIONS:
407 {
408 int options, retval = -EINVAL;
409 if (get_user(options, uarg.i))
410 return -EFAULT;
411 if (options & WDIOS_DISABLECARD) {
412 pc87413_disable();
413 retval = 0;
414 }
415 if (options & WDIOS_ENABLECARD) {
416 pc87413_enable();
417 retval = 0;
418 }
419 return retval;
420 }
Alan Coxaee334c2008-05-19 14:07:37 +0100421 case WDIOC_KEEPALIVE:
422 pc87413_refresh();
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200423#ifdef DEBUG
Joe Perches27c766a2012-02-15 15:06:19 -0800424 pr_info(DPFX "keepalive\n");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200425#endif
Alan Coxaee334c2008-05-19 14:07:37 +0100426 return 0;
427 case WDIOC_SETTIMEOUT:
428 if (get_user(new_timeout, uarg.i))
429 return -EFAULT;
430 /* the API states this is given in secs */
431 new_timeout /= 60;
432 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
433 return -EINVAL;
434 timeout = new_timeout;
435 pc87413_refresh();
Gustavo A. R. Silvabd490f82020-07-07 12:11:21 -0500436 fallthrough; /* and return the new timeout */
Alan Coxaee334c2008-05-19 14:07:37 +0100437 case WDIOC_GETTIMEOUT:
438 new_timeout = timeout * 60;
439 return put_user(new_timeout, uarg.i);
Alan Coxaee334c2008-05-19 14:07:37 +0100440 default:
441 return -ENOTTY;
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200442 }
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200443}
444
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200445/* -- Notifier funtions -----------------------------------------*/
446
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200447/**
Lukas Bulwahn4700df02021-03-22 07:53:37 +0100448 * pc87413_notify_sys:
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200449 * @this: our notifier block
450 * @code: the event being reported
451 * @unused: unused
452 *
453 * Our notifier is called on system shutdowns. We want to turn the card
454 * off at reboot otherwise the machine will reboot again during memory
455 * test or worse yet during the following fsck. This would suck, in fact
456 * trust me - if it happens it does suck.
457 */
458
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200459static int pc87413_notify_sys(struct notifier_block *this,
460 unsigned long code,
461 void *unused)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200462{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200463 if (code == SYS_DOWN || code == SYS_HALT)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200464 /* Turn the card off */
465 pc87413_disable();
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200466 return NOTIFY_DONE;
467}
468
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200469/* -- Module's structures ---------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200470
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800471static const struct file_operations pc87413_fops = {
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200472 .owner = THIS_MODULE,
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200473 .llseek = no_llseek,
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200474 .write = pc87413_write,
Alan Coxaee334c2008-05-19 14:07:37 +0100475 .unlocked_ioctl = pc87413_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200476 .compat_ioctl = compat_ptr_ioctl,
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200477 .open = pc87413_open,
478 .release = pc87413_release,
479};
480
Alan Coxaee334c2008-05-19 14:07:37 +0100481static struct notifier_block pc87413_notifier = {
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200482 .notifier_call = pc87413_notify_sys,
483};
484
Alan Coxaee334c2008-05-19 14:07:37 +0100485static struct miscdevice pc87413_miscdev = {
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200486 .minor = WATCHDOG_MINOR,
487 .name = "watchdog",
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000488 .fops = &pc87413_fops,
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200489};
490
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200491/* -- Module init functions -------------------------------------*/
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200492
493/**
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000494 * pc87413_init: module's "constructor"
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200495 *
496 * Set up the WDT watchdog board. All we have to do is grab the
497 * resources we require and bitch if anyone beat us to them.
498 * The open() function will actually kick the board off.
499 */
500
501static int __init pc87413_init(void)
502{
503 int ret;
504
Joe Perches27c766a2012-02-15 15:06:19 -0800505 pr_info("Version " VERSION " at io 0x%X\n",
Alan Coxaee334c2008-05-19 14:07:37 +0100506 WDT_INDEX_IO_PORT);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200507
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700508 if (!request_muxed_region(io, 2, MODNAME))
509 return -EBUSY;
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200510
511 ret = register_reboot_notifier(&pc87413_notifier);
Jingoo Han5f5e1902014-02-27 14:41:42 +0900512 if (ret != 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800513 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200514
515 ret = misc_register(&pc87413_miscdev);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200516 if (ret != 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800517 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
518 WATCHDOG_MINOR, ret);
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700519 goto reboot_unreg;
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200520 }
Joe Perches27c766a2012-02-15 15:06:19 -0800521 pr_info("initialized. timeout=%d min\n", timeout);
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700522
523 pc87413_select_wdt_out();
524 pc87413_enable_swc();
525 pc87413_get_swc_base_addr();
526
527 if (!request_region(swc_base_addr, 0x20, MODNAME)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800528 pr_err("cannot request SWC region at 0x%x\n", swc_base_addr);
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700529 ret = -EBUSY;
530 goto misc_unreg;
531 }
532
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200533 pc87413_enable();
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700534
535 release_region(io, 2);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200536 return 0;
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700537
538misc_unreg:
539 misc_deregister(&pc87413_miscdev);
540reboot_unreg:
541 unregister_reboot_notifier(&pc87413_notifier);
542 release_region(io, 2);
543 return ret;
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200544}
545
546/**
547 * pc87413_exit: module's "destructor"
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200548 *
549 * Unload the watchdog. You cannot do this with any file handles open.
550 * If your watchdog is set to continue ticking on close and you unload
551 * it, well it keeps ticking. We won't get the interrupt but the board
552 * will not touch PC memory so all is fine. You just have to load a new
553 * module in 60 seconds or reboot.
554 */
555
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200556static void __exit pc87413_exit(void)
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200557{
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200558 /* Stop the timer before we leave */
Alan Coxaee334c2008-05-19 14:07:37 +0100559 if (!nowayout) {
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200560 pc87413_disable();
Joe Perches27c766a2012-02-15 15:06:19 -0800561 pr_info("Watchdog disabled\n");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200562 }
563
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200564 misc_deregister(&pc87413_miscdev);
565 unregister_reboot_notifier(&pc87413_notifier);
Jonathan McDowell7ccdb942011-04-14 12:02:39 -0700566 release_region(swc_base_addr, 0x20);
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200567
Joe Perches27c766a2012-02-15 15:06:19 -0800568 pr_info("watchdog component driver removed\n");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200569}
570
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200571module_init(pc87413_init);
572module_exit(pc87413_exit);
573
Jingoo Han5f5e1902014-02-27 14:41:42 +0900574MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
575MODULE_AUTHOR("Marcus Junker <junker@anduras.de>");
Sven Anders & Marcus Junker789fc0a2006-08-24 17:11:50 +0200576MODULE_DESCRIPTION("PC87413 WDT driver");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200577MODULE_LICENSE("GPL");
578
David Howells5d1c93c2017-04-04 16:54:29 +0100579module_param_hw(io, int, ioport, 0);
Randy Dunlap76550d32010-05-01 09:46:15 -0700580MODULE_PARM_DESC(io, MODNAME " I/O port (default: "
581 __MODULE_STRING(IO_DEFAULT) ").");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200582
583module_param(timeout, int, 0);
Alan Coxaee334c2008-05-19 14:07:37 +0100584MODULE_PARM_DESC(timeout,
585 "Watchdog timeout in minutes (default="
Randy Dunlap76550d32010-05-01 09:46:15 -0700586 __MODULE_STRING(DEFAULT_TIMEOUT) ").");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200587
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100588module_param(nowayout, bool, 0);
Alan Coxaee334c2008-05-19 14:07:37 +0100589MODULE_PARM_DESC(nowayout,
590 "Watchdog cannot be stopped once started (default="
591 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Sven Anders & Marcus Junker00b3b3e2006-10-16 18:18:09 +0200592