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 u32 tcobase_phys
;
46 static void __iomem
*tcobase
;
47 static unsigned int pm_iobase
;
48 static DEFINE_SPINLOCK(tco_lock
); /* Guards the hardware */
49 static unsigned long timer_alive
;
50 static char tco_expect_close
;
51 static struct pci_dev
*sp5100_tco_pci
;
53 /* the watchdog platform device */
54 static struct platform_device
*sp5100_tco_platform_device
;
56 /* module parameters */
58 #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
59 static int heartbeat
= WATCHDOG_HEARTBEAT
; /* in seconds */
60 module_param(heartbeat
, int, 0);
61 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds. (default="
62 __MODULE_STRING(WATCHDOG_HEARTBEAT
) ")");
64 static int nowayout
= WATCHDOG_NOWAYOUT
;
65 module_param(nowayout
, int, 0);
66 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started"
67 " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
70 * Some TCO specific functions
72 static void tco_timer_start(void)
77 spin_lock_irqsave(&tco_lock
, flags
);
78 val
= readl(SP5100_WDT_CONTROL(tcobase
));
79 val
|= SP5100_WDT_START_STOP_BIT
;
80 writel(val
, SP5100_WDT_CONTROL(tcobase
));
81 spin_unlock_irqrestore(&tco_lock
, flags
);
84 static void tco_timer_stop(void)
89 spin_lock_irqsave(&tco_lock
, flags
);
90 val
= readl(SP5100_WDT_CONTROL(tcobase
));
91 val
&= ~SP5100_WDT_START_STOP_BIT
;
92 writel(val
, SP5100_WDT_CONTROL(tcobase
));
93 spin_unlock_irqrestore(&tco_lock
, flags
);
96 static void tco_timer_keepalive(void)
101 spin_lock_irqsave(&tco_lock
, flags
);
102 val
= readl(SP5100_WDT_CONTROL(tcobase
));
103 val
|= SP5100_WDT_TRIGGER_BIT
;
104 writel(val
, SP5100_WDT_CONTROL(tcobase
));
105 spin_unlock_irqrestore(&tco_lock
, flags
);
108 static int tco_timer_set_heartbeat(int t
)
112 if (t
< 0 || t
> 0xffff)
115 /* Write new heartbeat to watchdog */
116 spin_lock_irqsave(&tco_lock
, flags
);
117 writel(t
, SP5100_WDT_COUNT(tcobase
));
118 spin_unlock_irqrestore(&tco_lock
, flags
);
125 * /dev/watchdog handling
128 static int sp5100_tco_open(struct inode
*inode
, struct file
*file
)
130 /* /dev/watchdog can only be opened once */
131 if (test_and_set_bit(0, &timer_alive
))
134 /* Reload and activate timer */
136 tco_timer_keepalive();
137 return nonseekable_open(inode
, file
);
140 static int sp5100_tco_release(struct inode
*inode
, struct file
*file
)
142 /* Shut off the timer. */
143 if (tco_expect_close
== 42) {
147 "Unexpected close, not stopping watchdog!\n");
148 tco_timer_keepalive();
150 clear_bit(0, &timer_alive
);
151 tco_expect_close
= 0;
155 static ssize_t
sp5100_tco_write(struct file
*file
, const char __user
*data
,
156 size_t len
, loff_t
*ppos
)
158 /* See if we got the magic character 'V' and reload the timer */
163 /* note: just in case someone wrote the magic character
164 * five months ago... */
165 tco_expect_close
= 0;
167 /* scan to see whether or not we got the magic character
169 for (i
= 0; i
!= len
; i
++) {
171 if (get_user(c
, data
+ i
))
174 tco_expect_close
= 42;
178 /* someone wrote to us, we should reload the timer */
179 tco_timer_keepalive();
184 static long sp5100_tco_ioctl(struct file
*file
, unsigned int cmd
,
187 int new_options
, retval
= -EINVAL
;
189 void __user
*argp
= (void __user
*)arg
;
190 int __user
*p
= argp
;
191 static const struct watchdog_info ident
= {
192 .options
= WDIOF_SETTIMEOUT
|
193 WDIOF_KEEPALIVEPING
|
195 .firmware_version
= 0,
196 .identity
= TCO_MODULE_NAME
,
200 case WDIOC_GETSUPPORT
:
201 return copy_to_user(argp
, &ident
,
202 sizeof(ident
)) ? -EFAULT
: 0;
203 case WDIOC_GETSTATUS
:
204 case WDIOC_GETBOOTSTATUS
:
205 return put_user(0, p
);
206 case WDIOC_SETOPTIONS
:
207 if (get_user(new_options
, p
))
209 if (new_options
& WDIOS_DISABLECARD
) {
213 if (new_options
& WDIOS_ENABLECARD
) {
215 tco_timer_keepalive();
219 case WDIOC_KEEPALIVE
:
220 tco_timer_keepalive();
222 case WDIOC_SETTIMEOUT
:
223 if (get_user(new_heartbeat
, p
))
225 if (tco_timer_set_heartbeat(new_heartbeat
))
227 tco_timer_keepalive();
229 case WDIOC_GETTIMEOUT
:
230 return put_user(heartbeat
, p
);
240 static const struct file_operations sp5100_tco_fops
= {
241 .owner
= THIS_MODULE
,
243 .write
= sp5100_tco_write
,
244 .unlocked_ioctl
= sp5100_tco_ioctl
,
245 .open
= sp5100_tco_open
,
246 .release
= sp5100_tco_release
,
249 static struct miscdevice sp5100_tco_miscdev
= {
250 .minor
= WATCHDOG_MINOR
,
252 .fops
= &sp5100_tco_fops
,
256 * Data for PCI driver interface
258 * This data only exists for exporting the supported
259 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
260 * register a pci_driver, because someone else might
261 * want to register another driver on the same PCI id.
263 static DEFINE_PCI_DEVICE_TABLE(sp5100_tco_pci_tbl
) = {
264 { PCI_VENDOR_ID_ATI
, PCI_DEVICE_ID_ATI_SBX00_SMBUS
, PCI_ANY_ID
,
266 { 0, }, /* End of list */
268 MODULE_DEVICE_TABLE(pci
, sp5100_tco_pci_tbl
);
271 * Init & exit routines
274 static unsigned char __devinit
sp5100_tco_setupdevice(void)
276 struct pci_dev
*dev
= NULL
;
279 /* Match the PCI device */
280 for_each_pci_dev(dev
) {
281 if (pci_match_id(sp5100_tco_pci_tbl
, dev
) != NULL
) {
282 sp5100_tco_pci
= dev
;
290 /* Request the IO ports used by this driver */
291 pm_iobase
= SP5100_IO_PM_INDEX_REG
;
292 if (!request_region(pm_iobase
, SP5100_PM_IOPORTS_SIZE
, "SP5100 TCO")) {
293 printk(KERN_ERR PFX
"I/O address 0x%04x already in use\n",
298 /* Find the watchdog base address. */
299 outb(SP5100_PM_WATCHDOG_BASE3
, SP5100_IO_PM_INDEX_REG
);
300 val
= inb(SP5100_IO_PM_DATA_REG
);
301 outb(SP5100_PM_WATCHDOG_BASE2
, SP5100_IO_PM_INDEX_REG
);
302 val
= val
<< 8 | inb(SP5100_IO_PM_DATA_REG
);
303 outb(SP5100_PM_WATCHDOG_BASE1
, SP5100_IO_PM_INDEX_REG
);
304 val
= val
<< 8 | inb(SP5100_IO_PM_DATA_REG
);
305 outb(SP5100_PM_WATCHDOG_BASE0
, SP5100_IO_PM_INDEX_REG
);
306 /* Low three bits of BASE0 are reserved. */
307 val
= val
<< 8 | (inb(SP5100_IO_PM_DATA_REG
) & 0xf8);
309 if (!request_mem_region_exclusive(val
, SP5100_WDT_MEM_MAP_SIZE
,
311 printk(KERN_ERR PFX
"mmio address 0x%04x already in use\n",
317 tcobase
= ioremap(val
, SP5100_WDT_MEM_MAP_SIZE
);
319 printk(KERN_ERR PFX
"failed to get tcobase address\n");
320 goto unreg_mem_region
;
323 /* Enable watchdog decode bit */
324 pci_read_config_dword(sp5100_tco_pci
,
325 SP5100_PCI_WATCHDOG_MISC_REG
,
328 val
|= SP5100_PCI_WATCHDOG_DECODE_EN
;
330 pci_write_config_dword(sp5100_tco_pci
,
331 SP5100_PCI_WATCHDOG_MISC_REG
,
334 /* Enable Watchdog timer and set the resolution to 1 sec. */
335 outb(SP5100_PM_WATCHDOG_CONTROL
, SP5100_IO_PM_INDEX_REG
);
336 val
= inb(SP5100_IO_PM_DATA_REG
);
337 val
|= SP5100_PM_WATCHDOG_SECOND_RES
;
338 val
&= ~SP5100_PM_WATCHDOG_DISABLE
;
339 outb(val
, SP5100_IO_PM_DATA_REG
);
341 /* Check that the watchdog action is set to reset the system. */
342 val
= readl(SP5100_WDT_CONTROL(tcobase
));
343 val
&= ~SP5100_PM_WATCHDOG_ACTION_RESET
;
344 writel(val
, SP5100_WDT_CONTROL(tcobase
));
346 /* Set a reasonable heartbeat before we stop the timer */
347 tco_timer_set_heartbeat(heartbeat
);
350 * Stop the TCO before we change anything so we don't race with
359 release_mem_region(tcobase_phys
, SP5100_WDT_MEM_MAP_SIZE
);
361 release_region(pm_iobase
, SP5100_PM_IOPORTS_SIZE
);
366 static int __devinit
sp5100_tco_init(struct platform_device
*dev
)
371 /* Check whether or not the hardware watchdog is there. If found, then
374 if (!sp5100_tco_setupdevice())
377 /* Check to see if last reboot was due to watchdog timeout */
378 printk(KERN_INFO PFX
"Watchdog reboot %sdetected.\n",
379 readl(SP5100_WDT_CONTROL(tcobase
)) & SP5100_PM_WATCHDOG_FIRED
?
382 /* Clear out the old status */
383 val
= readl(SP5100_WDT_CONTROL(tcobase
));
384 val
&= ~SP5100_PM_WATCHDOG_FIRED
;
385 writel(val
, SP5100_WDT_CONTROL(tcobase
));
388 * Check that the heartbeat value is within it's range.
389 * If not, reset to the default.
391 if (tco_timer_set_heartbeat(heartbeat
)) {
392 heartbeat
= WATCHDOG_HEARTBEAT
;
393 tco_timer_set_heartbeat(heartbeat
);
396 ret
= misc_register(&sp5100_tco_miscdev
);
398 printk(KERN_ERR PFX
"cannot register miscdev on minor="
400 WATCHDOG_MINOR
, ret
);
404 clear_bit(0, &timer_alive
);
406 printk(KERN_INFO PFX
"initialized (0x%p). heartbeat=%d sec"
408 tcobase
, heartbeat
, nowayout
);
414 release_mem_region(tcobase_phys
, SP5100_WDT_MEM_MAP_SIZE
);
415 release_region(pm_iobase
, SP5100_PM_IOPORTS_SIZE
);
419 static void __devexit
sp5100_tco_cleanup(void)
421 /* Stop the timer before we leave */
426 misc_deregister(&sp5100_tco_miscdev
);
428 release_mem_region(tcobase_phys
, SP5100_WDT_MEM_MAP_SIZE
);
429 release_region(pm_iobase
, SP5100_PM_IOPORTS_SIZE
);
432 static int __devexit
sp5100_tco_remove(struct platform_device
*dev
)
435 sp5100_tco_cleanup();
439 static void sp5100_tco_shutdown(struct platform_device
*dev
)
444 static struct platform_driver sp5100_tco_driver
= {
445 .probe
= sp5100_tco_init
,
446 .remove
= __devexit_p(sp5100_tco_remove
),
447 .shutdown
= sp5100_tco_shutdown
,
449 .owner
= THIS_MODULE
,
450 .name
= TCO_MODULE_NAME
,
454 static int __init
sp5100_tco_init_module(void)
458 printk(KERN_INFO PFX
"SP5100 TCO WatchDog Timer Driver v%s\n",
461 err
= platform_driver_register(&sp5100_tco_driver
);
465 sp5100_tco_platform_device
= platform_device_register_simple(
466 TCO_MODULE_NAME
, -1, NULL
, 0);
467 if (IS_ERR(sp5100_tco_platform_device
)) {
468 err
= PTR_ERR(sp5100_tco_platform_device
);
469 goto unreg_platform_driver
;
474 unreg_platform_driver
:
475 platform_driver_unregister(&sp5100_tco_driver
);
479 static void __exit
sp5100_tco_cleanup_module(void)
481 platform_device_unregister(sp5100_tco_platform_device
);
482 platform_driver_unregister(&sp5100_tco_driver
);
483 printk(KERN_INFO PFX
"SP5100 TCO Watchdog Module Unloaded.\n");
486 module_init(sp5100_tco_init_module
);
487 module_exit(sp5100_tco_cleanup_module
);
489 MODULE_AUTHOR("Priyanka Gupta");
490 MODULE_DESCRIPTION("TCO timer driver for SP5100 chipset");
491 MODULE_LICENSE("GPL");
492 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);