1 // SPDX-License-Identifier: GPL-1.0+
3 * IBM Automatic Server Restart driver.
5 * Copyright (c) 2005 Andrey Panin <pazke@donpac.ru>
7 * Based on driver written by Pete Reynolds.
8 * Copyright (c) IBM Corporation, 1998-2004.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/timer.h>
19 #include <linux/miscdevice.h>
20 #include <linux/watchdog.h>
21 #include <linux/dmi.h>
23 #include <linux/uaccess.h>
35 #define TOPAZ_ASR_REG_OFFSET 4
36 #define TOPAZ_ASR_TOGGLE 0x40
37 #define TOPAZ_ASR_DISABLE 0x80
39 /* PEARL ASR S/W REGISTER SUPERIO PORT ADDRESSES */
40 #define PEARL_BASE 0xe04
41 #define PEARL_WRITE 0xe06
42 #define PEARL_READ 0xe07
44 #define PEARL_ASR_DISABLE_MASK 0x80 /* bit 7: disable = 1, enable = 0 */
45 #define PEARL_ASR_TOGGLE_MASK 0x40 /* bit 6: 0, then 1, then 0 */
47 /* JASPER OFFSET FROM SIO BASE ADDR TO ASR S/W REGISTERS. */
48 #define JASPER_ASR_REG_OFFSET 0x38
50 #define JASPER_ASR_DISABLE_MASK 0x01 /* bit 0: disable = 1, enable = 0 */
51 #define JASPER_ASR_TOGGLE_MASK 0x02 /* bit 1: 0, then 1, then 0 */
53 #define JUNIPER_BASE_ADDRESS 0x54b /* Base address of Juniper ASR */
54 #define JUNIPER_ASR_DISABLE_MASK 0x01 /* bit 0: disable = 1 enable = 0 */
55 #define JUNIPER_ASR_TOGGLE_MASK 0x02 /* bit 1: 0, then 1, then 0 */
57 #define SPRUCE_BASE_ADDRESS 0x118e /* Base address of Spruce ASR */
58 #define SPRUCE_ASR_DISABLE_MASK 0x01 /* bit 1: disable = 1 enable = 0 */
59 #define SPRUCE_ASR_TOGGLE_MASK 0x02 /* bit 0: 0, then 1, then 0 */
62 static bool nowayout
= WATCHDOG_NOWAYOUT
;
64 static unsigned long asr_is_open
;
65 static char asr_expect_close
;
67 static unsigned int asr_type
, asr_base
, asr_length
;
68 static unsigned int asr_read_addr
, asr_write_addr
;
69 static unsigned char asr_toggle_mask
, asr_disable_mask
;
70 static DEFINE_SPINLOCK(asr_lock
);
72 static void __asr_toggle(void)
76 reg
= inb(asr_read_addr
);
78 outb(reg
& ~asr_toggle_mask
, asr_write_addr
);
79 reg
= inb(asr_read_addr
);
81 outb(reg
| asr_toggle_mask
, asr_write_addr
);
82 reg
= inb(asr_read_addr
);
84 outb(reg
& ~asr_toggle_mask
, asr_write_addr
);
85 reg
= inb(asr_read_addr
);
88 static void asr_toggle(void)
92 spin_unlock(&asr_lock
);
95 static void asr_enable(void)
100 if (asr_type
== ASMTYPE_TOPAZ
) {
101 /* asr_write_addr == asr_read_addr */
102 reg
= inb(asr_read_addr
);
103 outb(reg
& ~(TOPAZ_ASR_TOGGLE
| TOPAZ_ASR_DISABLE
),
107 * First make sure the hardware timer is reset by toggling
108 * ASR hardware timer line.
112 reg
= inb(asr_read_addr
);
113 outb(reg
& ~asr_disable_mask
, asr_write_addr
);
115 reg
= inb(asr_read_addr
);
116 spin_unlock(&asr_lock
);
119 static void asr_disable(void)
123 spin_lock(&asr_lock
);
124 reg
= inb(asr_read_addr
);
126 if (asr_type
== ASMTYPE_TOPAZ
)
127 /* asr_write_addr == asr_read_addr */
128 outb(reg
| TOPAZ_ASR_TOGGLE
| TOPAZ_ASR_DISABLE
,
131 outb(reg
| asr_toggle_mask
, asr_write_addr
);
132 reg
= inb(asr_read_addr
);
134 outb(reg
| asr_disable_mask
, asr_write_addr
);
136 reg
= inb(asr_read_addr
);
137 spin_unlock(&asr_lock
);
140 static int __init
asr_get_base_address(void)
142 unsigned char low
, high
;
143 const char *type
= "";
149 /* SELECT SuperIO CHIP FOR QUERYING
150 (WRITE 0x07 TO BOTH 0x2E and 0x2F) */
154 /* SELECT AND READ THE HIGH-NIBBLE OF THE GPIO BASE ADDRESS */
158 /* SELECT AND READ THE LOW-NIBBLE OF THE GPIO BASE ADDRESS */
162 asr_base
= (high
<< 16) | low
;
163 asr_read_addr
= asr_write_addr
=
164 asr_base
+ TOPAZ_ASR_REG_OFFSET
;
174 pdev
= pci_get_bus_and_slot(0, DEVFN(0x1f, 0));
177 pci_read_config_dword(pdev
, 0x58, &r
);
178 asr_base
= r
& 0xFFFE;
181 /* FIXME: need to use pci_config_lock here,
182 but it's not exported */
184 /* spin_lock_irqsave(&pci_config_lock, flags);*/
186 /* Select the SuperIO chip in the PCI I/O port register */
187 outl(0x8000f858, 0xcf8);
189 /* BUS 0, Slot 1F, fnc 0, offset 58 */
192 * Read the base address for the SuperIO chip.
193 * Only the lower 16 bits are valid, but the address is word
194 * aligned so the last bit must be masked off.
196 asr_base
= inl(0xcfc) & 0xfffe;
198 /* spin_unlock_irqrestore(&pci_config_lock, flags);*/
200 asr_read_addr
= asr_write_addr
=
201 asr_base
+ JASPER_ASR_REG_OFFSET
;
202 asr_toggle_mask
= JASPER_ASR_TOGGLE_MASK
;
203 asr_disable_mask
= JASPER_ASR_DISABLE_MASK
;
204 asr_length
= JASPER_ASR_REG_OFFSET
+ 1;
210 asr_base
= PEARL_BASE
;
211 asr_read_addr
= PEARL_READ
;
212 asr_write_addr
= PEARL_WRITE
;
213 asr_toggle_mask
= PEARL_ASR_TOGGLE_MASK
;
214 asr_disable_mask
= PEARL_ASR_DISABLE_MASK
;
218 case ASMTYPE_JUNIPER
:
220 asr_base
= JUNIPER_BASE_ADDRESS
;
221 asr_read_addr
= asr_write_addr
= asr_base
;
222 asr_toggle_mask
= JUNIPER_ASR_TOGGLE_MASK
;
223 asr_disable_mask
= JUNIPER_ASR_DISABLE_MASK
;
228 asr_base
= SPRUCE_BASE_ADDRESS
;
229 asr_read_addr
= asr_write_addr
= asr_base
;
230 asr_toggle_mask
= SPRUCE_ASR_TOGGLE_MASK
;
231 asr_disable_mask
= SPRUCE_ASR_DISABLE_MASK
;
235 if (!request_region(asr_base
, asr_length
, "ibmasr")) {
236 pr_err("address %#x already in use\n", asr_base
);
240 pr_info("found %sASR @ addr %#x\n", type
, asr_base
);
246 static ssize_t
asr_write(struct file
*file
, const char __user
*buf
,
247 size_t count
, loff_t
*ppos
)
253 /* In case it was set long ago */
254 asr_expect_close
= 0;
256 for (i
= 0; i
!= count
; i
++) {
258 if (get_user(c
, buf
+ i
))
261 asr_expect_close
= 42;
269 static long asr_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
271 static const struct watchdog_info ident
= {
272 .options
= WDIOF_KEEPALIVEPING
|
274 .identity
= "IBM ASR",
276 void __user
*argp
= (void __user
*)arg
;
277 int __user
*p
= argp
;
281 case WDIOC_GETSUPPORT
:
282 return copy_to_user(argp
, &ident
, sizeof(ident
)) ? -EFAULT
: 0;
283 case WDIOC_GETSTATUS
:
284 case WDIOC_GETBOOTSTATUS
:
285 return put_user(0, p
);
286 case WDIOC_SETOPTIONS
:
288 int new_options
, retval
= -EINVAL
;
289 if (get_user(new_options
, p
))
291 if (new_options
& WDIOS_DISABLECARD
) {
295 if (new_options
& WDIOS_ENABLECARD
) {
302 case WDIOC_KEEPALIVE
:
306 * The hardware has a fixed timeout value, so no WDIOC_SETTIMEOUT
307 * and WDIOC_GETTIMEOUT always returns 256.
309 case WDIOC_GETTIMEOUT
:
311 return put_user(heartbeat
, p
);
317 static int asr_open(struct inode
*inode
, struct file
*file
)
319 if (test_and_set_bit(0, &asr_is_open
))
325 return stream_open(inode
, file
);
328 static int asr_release(struct inode
*inode
, struct file
*file
)
330 if (asr_expect_close
== 42)
333 pr_crit("unexpected close, not stopping watchdog!\n");
336 clear_bit(0, &asr_is_open
);
337 asr_expect_close
= 0;
341 static const struct file_operations asr_fops
= {
342 .owner
= THIS_MODULE
,
344 .unlocked_ioctl
= asr_ioctl
,
345 .compat_ioctl
= compat_ptr_ioctl
,
347 .release
= asr_release
,
350 static struct miscdevice asr_miscdev
= {
351 .minor
= WATCHDOG_MINOR
,
362 static struct ibmasr_id ibmasr_id_table
[] __initdata
= {
363 { "IBM Automatic Server Restart - eserver xSeries 220", ASMTYPE_TOPAZ
},
364 { "IBM Automatic Server Restart - Machine Type 8673", ASMTYPE_PEARL
},
365 { "IBM Automatic Server Restart - Machine Type 8480", ASMTYPE_JASPER
},
366 { "IBM Automatic Server Restart - Machine Type 8482", ASMTYPE_JUNIPER
},
367 { "IBM Automatic Server Restart - Machine Type 8648", ASMTYPE_SPRUCE
},
371 static int __init
ibmasr_init(void)
373 struct ibmasr_id
*id
;
376 for (id
= ibmasr_id_table
; id
->desc
; id
++) {
377 if (dmi_find_device(DMI_DEV_TYPE_OTHER
, id
->desc
, NULL
)) {
386 rc
= asr_get_base_address();
390 rc
= misc_register(&asr_miscdev
);
392 release_region(asr_base
, asr_length
);
393 pr_err("failed to register misc device\n");
400 static void __exit
ibmasr_exit(void)
405 misc_deregister(&asr_miscdev
);
407 release_region(asr_base
, asr_length
);
410 module_init(ibmasr_init
);
411 module_exit(ibmasr_exit
);
413 module_param(nowayout
, bool, 0);
414 MODULE_PARM_DESC(nowayout
,
415 "Watchdog cannot be stopped once started (default="
416 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
418 MODULE_DESCRIPTION("IBM Automatic Server Restart driver");
419 MODULE_AUTHOR("Andrey Panin");
420 MODULE_LICENSE("GPL");