2 * sp5100_tco : TCO timer driver for sp5100 chipsets
4 * (c) Copyright 2009 Google Inc., All Rights Reserved.
7 * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights
9 * http://www.kernelconcepts.de
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
16 * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide"
20 * Includes, defines, variables, module parameters, ...
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/types.h>
26 #include <linux/miscdevice.h>
27 #include <linux/watchdog.h>
28 #include <linux/init.h>
30 #include <linux/pci.h>
31 #include <linux/ioport.h>
32 #include <linux/platform_device.h>
33 #include <linux/uaccess.h>
36 #include "sp5100_tco.h"
38 /* Module and version information */
39 #define TCO_VERSION "0.01"
40 #define TCO_MODULE_NAME "SP5100 TCO timer"
41 #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
42 #define PFX TCO_MODULE_NAME ": "
44 /* internal variables */
45 static void __iomem
*tcobase
;
46 static unsigned int pm_iobase
;
47 static DEFINE_SPINLOCK(tco_lock
); /* Guards the hardware */
48 static unsigned long timer_alive
;
49 static char tco_expect_close
;
50 static struct pci_dev
*sp5100_tco_pci
;
52 /* the watchdog platform device */
53 static struct platform_device
*sp5100_tco_platform_device
;
55 /* module parameters */
57 #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
58 static int heartbeat
= WATCHDOG_HEARTBEAT
; /* in seconds */
59 module_param(heartbeat
, int, 0);
60 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds. (default="
61 __MODULE_STRING(WATCHDOG_HEARTBEAT
) ")");
63 static int nowayout
= WATCHDOG_NOWAYOUT
;
64 module_param(nowayout
, int, 0);
65 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started"
66 " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
69 * Some TCO specific functions
71 static void tco_timer_start(void)
76 spin_lock_irqsave(&tco_lock
, flags
);
77 val
= readl(SP5100_WDT_CONTROL(tcobase
));
78 val
|= SP5100_WDT_START_STOP_BIT
;
79 writel(val
, SP5100_WDT_CONTROL(tcobase
));
80 spin_unlock_irqrestore(&tco_lock
, flags
);
83 static void tco_timer_stop(void)
88 spin_lock_irqsave(&tco_lock
, flags
);
89 val
= readl(SP5100_WDT_CONTROL(tcobase
));
90 val
&= ~SP5100_WDT_START_STOP_BIT
;
91 writel(val
, SP5100_WDT_CONTROL(tcobase
));
92 spin_unlock_irqrestore(&tco_lock
, flags
);
95 static void tco_timer_keepalive(void)
100 spin_lock_irqsave(&tco_lock
, flags
);
101 val
= readl(SP5100_WDT_CONTROL(tcobase
));
102 val
|= SP5100_WDT_TRIGGER_BIT
;
103 writel(val
, SP5100_WDT_CONTROL(tcobase
));
104 spin_unlock_irqrestore(&tco_lock
, flags
);
107 static int tco_timer_set_heartbeat(int t
)
111 if (t
< 0 || t
> 0xffff)
114 /* Write new heartbeat to watchdog */
115 spin_lock_irqsave(&tco_lock
, flags
);
116 writel(t
, SP5100_WDT_COUNT(tcobase
));
117 spin_unlock_irqrestore(&tco_lock
, flags
);
124 * /dev/watchdog handling
127 static int sp5100_tco_open(struct inode
*inode
, struct file
*file
)
129 /* /dev/watchdog can only be opened once */
130 if (test_and_set_bit(0, &timer_alive
))
133 /* Reload and activate timer */
135 tco_timer_keepalive();
136 return nonseekable_open(inode
, file
);
139 static int sp5100_tco_release(struct inode
*inode
, struct file
*file
)
141 /* Shut off the timer. */
142 if (tco_expect_close
== 42) {
146 "Unexpected close, not stopping watchdog!\n");
147 tco_timer_keepalive();
149 clear_bit(0, &timer_alive
);
150 tco_expect_close
= 0;
154 static ssize_t
sp5100_tco_write(struct file
*file
, const char __user
*data
,
155 size_t len
, loff_t
*ppos
)
157 /* See if we got the magic character 'V' and reload the timer */
162 /* note: just in case someone wrote the magic character
163 * five months ago... */
164 tco_expect_close
= 0;
166 /* scan to see whether or not we got the magic character
168 for (i
= 0; i
!= len
; i
++) {
170 if (get_user(c
, data
+ i
))
173 tco_expect_close
= 42;
177 /* someone wrote to us, we should reload the timer */
178 tco_timer_keepalive();
183 static long sp5100_tco_ioctl(struct file
*file
, unsigned int cmd
,
186 int new_options
, retval
= -EINVAL
;
188 void __user
*argp
= (void __user
*)arg
;
189 int __user
*p
= argp
;
190 static const struct watchdog_info ident
= {
191 .options
= WDIOF_SETTIMEOUT
|
192 WDIOF_KEEPALIVEPING
|
194 .firmware_version
= 0,
195 .identity
= TCO_MODULE_NAME
,
199 case WDIOC_GETSUPPORT
:
200 return copy_to_user(argp
, &ident
,
201 sizeof(ident
)) ? -EFAULT
: 0;
202 case WDIOC_GETSTATUS
:
203 case WDIOC_GETBOOTSTATUS
:
204 return put_user(0, p
);
205 case WDIOC_SETOPTIONS
:
206 if (get_user(new_options
, p
))
208 if (new_options
& WDIOS_DISABLECARD
) {
212 if (new_options
& WDIOS_ENABLECARD
) {
214 tco_timer_keepalive();
218 case WDIOC_KEEPALIVE
:
219 tco_timer_keepalive();
221 case WDIOC_SETTIMEOUT
:
222 if (get_user(new_heartbeat
, p
))
224 if (tco_timer_set_heartbeat(new_heartbeat
))
226 tco_timer_keepalive();
228 case WDIOC_GETTIMEOUT
:
229 return put_user(heartbeat
, p
);
239 static const struct file_operations sp5100_tco_fops
= {
240 .owner
= THIS_MODULE
,
242 .write
= sp5100_tco_write
,
243 .unlocked_ioctl
= sp5100_tco_ioctl
,
244 .open
= sp5100_tco_open
,
245 .release
= sp5100_tco_release
,
248 static struct miscdevice sp5100_tco_miscdev
= {
249 .minor
= WATCHDOG_MINOR
,
251 .fops
= &sp5100_tco_fops
,
255 * Data for PCI driver interface
257 * This data only exists for exporting the supported
258 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
259 * register a pci_driver, because someone else might
260 * want to register another driver on the same PCI id.
262 static DEFINE_PCI_DEVICE_TABLE(sp5100_tco_pci_tbl
) = {
263 { PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_SBX00_SMBUS
, PCI_ANY_ID
,
265 { 0, }, /* End of list */
267 MODULE_DEVICE_TABLE(pci
, sp5100_tco_pci_tbl
);
270 * Init & exit routines
273 static unsigned char __devinit
sp5100_tco_setupdevice(void)
275 struct pci_dev
*dev
= NULL
;
278 /* Match the PCI device */
279 for_each_pci_dev(dev
) {
280 if (pci_match_id(sp5100_tco_pci_tbl
, dev
) != NULL
) {
281 sp5100_tco_pci
= dev
;
289 /* Request the IO ports used by this driver */
290 pm_iobase
= SP5100_IO_PM_INDEX_REG
;
291 if (!request_region(pm_iobase
, SP5100_PM_IOPORTS_SIZE
, "SP5100 TCO")) {
292 printk(KERN_ERR PFX
"I/O address 0x%04x already in use\n",
297 /* Find the watchdog base address. */
298 outb(SP5100_PM_WATCHDOG_BASE3
, SP5100_IO_PM_INDEX_REG
);
299 val
= inb(SP5100_IO_PM_DATA_REG
);
300 outb(SP5100_PM_WATCHDOG_BASE2
, SP5100_IO_PM_INDEX_REG
);
301 val
= val
<< 8 | inb(SP5100_IO_PM_DATA_REG
);
302 outb(SP5100_PM_WATCHDOG_BASE1
, SP5100_IO_PM_INDEX_REG
);
303 val
= val
<< 8 | inb(SP5100_IO_PM_DATA_REG
);
304 outb(SP5100_PM_WATCHDOG_BASE0
, SP5100_IO_PM_INDEX_REG
);
305 /* Low three bits of BASE0 are reserved. */
306 val
= val
<< 8 | (inb(SP5100_IO_PM_DATA_REG
) & 0xf8);
308 tcobase
= ioremap(val
, SP5100_WDT_MEM_MAP_SIZE
);
310 printk(KERN_ERR PFX
"failed to get tcobase address\n");
314 /* Enable watchdog decode bit */
315 pci_read_config_dword(sp5100_tco_pci
,
316 SP5100_PCI_WATCHDOG_MISC_REG
,
319 val
|= SP5100_PCI_WATCHDOG_DECODE_EN
;
321 pci_write_config_dword(sp5100_tco_pci
,
322 SP5100_PCI_WATCHDOG_MISC_REG
,
325 /* Enable Watchdog timer and set the resolution to 1 sec. */
326 outb(SP5100_PM_WATCHDOG_CONTROL
, SP5100_IO_PM_INDEX_REG
);
327 val
= inb(SP5100_IO_PM_DATA_REG
);
328 val
|= SP5100_PM_WATCHDOG_SECOND_RES
;
329 val
&= ~SP5100_PM_WATCHDOG_DISABLE
;
330 outb(val
, SP5100_IO_PM_DATA_REG
);
332 /* Check that the watchdog action is set to reset the system. */
333 val
= readl(SP5100_WDT_CONTROL(tcobase
));
334 val
&= ~SP5100_PM_WATCHDOG_ACTION_RESET
;
335 writel(val
, SP5100_WDT_CONTROL(tcobase
));
337 /* Set a reasonable heartbeat before we stop the timer */
338 tco_timer_set_heartbeat(heartbeat
);
341 * Stop the TCO before we change anything so we don't race with
351 release_region(pm_iobase
, SP5100_PM_IOPORTS_SIZE
);
356 static int __devinit
sp5100_tco_init(struct platform_device
*dev
)
361 /* Check whether or not the hardware watchdog is there. If found, then
364 if (!sp5100_tco_setupdevice())
367 /* Check to see if last reboot was due to watchdog timeout */
368 printk(KERN_INFO PFX
"Watchdog reboot %sdetected.\n",
369 readl(SP5100_WDT_CONTROL(tcobase
)) & SP5100_PM_WATCHDOG_FIRED
?
372 /* Clear out the old status */
373 val
= readl(SP5100_WDT_CONTROL(tcobase
));
374 val
&= ~SP5100_PM_WATCHDOG_FIRED
;
375 writel(val
, SP5100_WDT_CONTROL(tcobase
));
378 * Check that the heartbeat value is within it's range.
379 * If not, reset to the default.
381 if (tco_timer_set_heartbeat(heartbeat
)) {
382 heartbeat
= WATCHDOG_HEARTBEAT
;
383 tco_timer_set_heartbeat(heartbeat
);
386 ret
= misc_register(&sp5100_tco_miscdev
);
388 printk(KERN_ERR PFX
"cannot register miscdev on minor="
390 WATCHDOG_MINOR
, ret
);
394 clear_bit(0, &timer_alive
);
396 printk(KERN_INFO PFX
"initialized (0x%p). heartbeat=%d sec"
398 tcobase
, heartbeat
, nowayout
);
404 release_region(pm_iobase
, SP5100_PM_IOPORTS_SIZE
);
408 static void __devexit
sp5100_tco_cleanup(void)
410 /* Stop the timer before we leave */
415 misc_deregister(&sp5100_tco_miscdev
);
417 release_region(pm_iobase
, SP5100_PM_IOPORTS_SIZE
);
420 static int __devexit
sp5100_tco_remove(struct platform_device
*dev
)
423 sp5100_tco_cleanup();
427 static void sp5100_tco_shutdown(struct platform_device
*dev
)
432 static struct platform_driver sp5100_tco_driver
= {
433 .probe
= sp5100_tco_init
,
434 .remove
= __devexit_p(sp5100_tco_remove
),
435 .shutdown
= sp5100_tco_shutdown
,
437 .owner
= THIS_MODULE
,
438 .name
= TCO_MODULE_NAME
,
442 static int __init
sp5100_tco_init_module(void)
446 printk(KERN_INFO PFX
"SP5100 TCO WatchDog Timer Driver v%s\n",
449 err
= platform_driver_register(&sp5100_tco_driver
);
453 sp5100_tco_platform_device
= platform_device_register_simple(
454 TCO_MODULE_NAME
, -1, NULL
, 0);
455 if (IS_ERR(sp5100_tco_platform_device
)) {
456 err
= PTR_ERR(sp5100_tco_platform_device
);
457 goto unreg_platform_driver
;
462 unreg_platform_driver
:
463 platform_driver_unregister(&sp5100_tco_driver
);
467 static void __exit
sp5100_tco_cleanup_module(void)
469 platform_device_unregister(sp5100_tco_platform_device
);
470 platform_driver_unregister(&sp5100_tco_driver
);
471 printk(KERN_INFO PFX
"SP5100 TCO Watchdog Module Unloaded.\n");
474 module_init(sp5100_tco_init_module
);
475 module_exit(sp5100_tco_cleanup_module
);
477 MODULE_AUTHOR("Priyanka Gupta");
478 MODULE_DESCRIPTION("TCO timer driver for SP5100 chipset");
479 MODULE_LICENSE("GPL");
480 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);