sp5100_tco.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * sp5100_tco : TCO timer driver for sp5100 chipsets
  3. *
  4. * (c) Copyright 2009 Google Inc., All Rights Reserved.
  5. *
  6. * Based on i8xx_tco.c:
  7. * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights
  8. * Reserved.
  9. * http://www.kernelconcepts.de
  10. *
  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.
  15. *
  16. * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide"
  17. */
  18. /*
  19. * Includes, defines, variables, module parameters, ...
  20. */
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/types.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/watchdog.h>
  26. #include <linux/init.h>
  27. #include <linux/fs.h>
  28. #include <linux/pci.h>
  29. #include <linux/ioport.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/io.h>
  33. #include "sp5100_tco.h"
  34. /* Module and version information */
  35. #define TCO_VERSION "0.01"
  36. #define TCO_MODULE_NAME "SP5100 TCO timer"
  37. #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
  38. #define PFX TCO_MODULE_NAME ": "
  39. /* internal variables */
  40. static u32 tcobase_phys;
  41. static void __iomem *tcobase;
  42. static unsigned int pm_iobase;
  43. static DEFINE_SPINLOCK(tco_lock); /* Guards the hardware */
  44. static unsigned long timer_alive;
  45. static char tco_expect_close;
  46. static struct pci_dev *sp5100_tco_pci;
  47. /* the watchdog platform device */
  48. static struct platform_device *sp5100_tco_platform_device;
  49. /* module parameters */
  50. #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
  51. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  52. module_param(heartbeat, int, 0);
  53. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default="
  54. __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  55. static int nowayout = WATCHDOG_NOWAYOUT;
  56. module_param(nowayout, int, 0);
  57. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
  58. " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  59. /*
  60. * Some TCO specific functions
  61. */
  62. static void tco_timer_start(void)
  63. {
  64. u32 val;
  65. unsigned long flags;
  66. spin_lock_irqsave(&tco_lock, flags);
  67. val = readl(SP5100_WDT_CONTROL(tcobase));
  68. val |= SP5100_WDT_START_STOP_BIT;
  69. writel(val, SP5100_WDT_CONTROL(tcobase));
  70. spin_unlock_irqrestore(&tco_lock, flags);
  71. }
  72. static void tco_timer_stop(void)
  73. {
  74. u32 val;
  75. unsigned long flags;
  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);
  81. }
  82. static void tco_timer_keepalive(void)
  83. {
  84. u32 val;
  85. unsigned long flags;
  86. spin_lock_irqsave(&tco_lock, flags);
  87. val = readl(SP5100_WDT_CONTROL(tcobase));
  88. val |= SP5100_WDT_TRIGGER_BIT;
  89. writel(val, SP5100_WDT_CONTROL(tcobase));
  90. spin_unlock_irqrestore(&tco_lock, flags);
  91. }
  92. static int tco_timer_set_heartbeat(int t)
  93. {
  94. unsigned long flags;
  95. if (t < 0 || t > 0xffff)
  96. return -EINVAL;
  97. /* Write new heartbeat to watchdog */
  98. spin_lock_irqsave(&tco_lock, flags);
  99. writel(t, SP5100_WDT_COUNT(tcobase));
  100. spin_unlock_irqrestore(&tco_lock, flags);
  101. heartbeat = t;
  102. return 0;
  103. }
  104. /*
  105. * /dev/watchdog handling
  106. */
  107. static int sp5100_tco_open(struct inode *inode, struct file *file)
  108. {
  109. /* /dev/watchdog can only be opened once */
  110. if (test_and_set_bit(0, &timer_alive))
  111. return -EBUSY;
  112. /* Reload and activate timer */
  113. tco_timer_start();
  114. tco_timer_keepalive();
  115. return nonseekable_open(inode, file);
  116. }
  117. static int sp5100_tco_release(struct inode *inode, struct file *file)
  118. {
  119. /* Shut off the timer. */
  120. if (tco_expect_close == 42) {
  121. tco_timer_stop();
  122. } else {
  123. printk(KERN_CRIT PFX
  124. "Unexpected close, not stopping watchdog!\n");
  125. tco_timer_keepalive();
  126. }
  127. clear_bit(0, &timer_alive);
  128. tco_expect_close = 0;
  129. return 0;
  130. }
  131. static ssize_t sp5100_tco_write(struct file *file, const char __user *data,
  132. size_t len, loff_t *ppos)
  133. {
  134. /* See if we got the magic character 'V' and reload the timer */
  135. if (len) {
  136. if (!nowayout) {
  137. size_t i;
  138. /* note: just in case someone wrote the magic character
  139. * five months ago... */
  140. tco_expect_close = 0;
  141. /* scan to see whether or not we got the magic character
  142. */
  143. for (i = 0; i != len; i++) {
  144. char c;
  145. if (get_user(c, data + i))
  146. return -EFAULT;
  147. if (c == 'V')
  148. tco_expect_close = 42;
  149. }
  150. }
  151. /* someone wrote to us, we should reload the timer */
  152. tco_timer_keepalive();
  153. }
  154. return len;
  155. }
  156. static long sp5100_tco_ioctl(struct file *file, unsigned int cmd,
  157. unsigned long arg)
  158. {
  159. int new_options, retval = -EINVAL;
  160. int new_heartbeat;
  161. void __user *argp = (void __user *)arg;
  162. int __user *p = argp;
  163. static const struct watchdog_info ident = {
  164. .options = WDIOF_SETTIMEOUT |
  165. WDIOF_KEEPALIVEPING |
  166. WDIOF_MAGICCLOSE,
  167. .firmware_version = 0,
  168. .identity = TCO_MODULE_NAME,
  169. };
  170. switch (cmd) {
  171. case WDIOC_GETSUPPORT:
  172. return copy_to_user(argp, &ident,
  173. sizeof(ident)) ? -EFAULT : 0;
  174. case WDIOC_GETSTATUS:
  175. case WDIOC_GETBOOTSTATUS:
  176. return put_user(0, p);
  177. case WDIOC_SETOPTIONS:
  178. if (get_user(new_options, p))
  179. return -EFAULT;
  180. if (new_options & WDIOS_DISABLECARD) {
  181. tco_timer_stop();
  182. retval = 0;
  183. }
  184. if (new_options & WDIOS_ENABLECARD) {
  185. tco_timer_start();
  186. tco_timer_keepalive();
  187. retval = 0;
  188. }
  189. return retval;
  190. case WDIOC_KEEPALIVE:
  191. tco_timer_keepalive();
  192. return 0;
  193. case WDIOC_SETTIMEOUT:
  194. if (get_user(new_heartbeat, p))
  195. return -EFAULT;
  196. if (tco_timer_set_heartbeat(new_heartbeat))
  197. return -EINVAL;
  198. tco_timer_keepalive();
  199. /* Fall through */
  200. case WDIOC_GETTIMEOUT:
  201. return put_user(heartbeat, p);
  202. default:
  203. return -ENOTTY;
  204. }
  205. }
  206. /*
  207. * Kernel Interfaces
  208. */
  209. static const struct file_operations sp5100_tco_fops = {
  210. .owner = THIS_MODULE,
  211. .llseek = no_llseek,
  212. .write = sp5100_tco_write,
  213. .unlocked_ioctl = sp5100_tco_ioctl,
  214. .open = sp5100_tco_open,
  215. .release = sp5100_tco_release,
  216. };
  217. static struct miscdevice sp5100_tco_miscdev = {
  218. .minor = WATCHDOG_MINOR,
  219. .name = "watchdog",
  220. .fops = &sp5100_tco_fops,
  221. };
  222. /*
  223. * Data for PCI driver interface
  224. *
  225. * This data only exists for exporting the supported
  226. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  227. * register a pci_driver, because someone else might
  228. * want to register another driver on the same PCI id.
  229. */
  230. static DEFINE_PCI_DEVICE_TABLE(sp5100_tco_pci_tbl) = {
  231. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS, PCI_ANY_ID,
  232. PCI_ANY_ID, },
  233. { 0, }, /* End of list */
  234. };
  235. MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl);
  236. /*
  237. * Init & exit routines
  238. */
  239. static unsigned char __devinit sp5100_tco_setupdevice(void)
  240. {
  241. struct pci_dev *dev = NULL;
  242. u32 val;
  243. /* Match the PCI device */
  244. for_each_pci_dev(dev) {
  245. if (pci_match_id(sp5100_tco_pci_tbl, dev) != NULL) {
  246. sp5100_tco_pci = dev;
  247. break;
  248. }
  249. }
  250. if (!sp5100_tco_pci)
  251. return 0;
  252. /* Request the IO ports used by this driver */
  253. pm_iobase = SP5100_IO_PM_INDEX_REG;
  254. if (!request_region(pm_iobase, SP5100_PM_IOPORTS_SIZE, "SP5100 TCO")) {
  255. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  256. pm_iobase);
  257. goto exit;
  258. }
  259. /* Find the watchdog base address. */
  260. outb(SP5100_PM_WATCHDOG_BASE3, SP5100_IO_PM_INDEX_REG);
  261. val = inb(SP5100_IO_PM_DATA_REG);
  262. outb(SP5100_PM_WATCHDOG_BASE2, SP5100_IO_PM_INDEX_REG);
  263. val = val << 8 | inb(SP5100_IO_PM_DATA_REG);
  264. outb(SP5100_PM_WATCHDOG_BASE1, SP5100_IO_PM_INDEX_REG);
  265. val = val << 8 | inb(SP5100_IO_PM_DATA_REG);
  266. outb(SP5100_PM_WATCHDOG_BASE0, SP5100_IO_PM_INDEX_REG);
  267. /* Low three bits of BASE0 are reserved. */
  268. val = val << 8 | (inb(SP5100_IO_PM_DATA_REG) & 0xf8);
  269. if (!request_mem_region_exclusive(val, SP5100_WDT_MEM_MAP_SIZE,
  270. "SP5100 TCO")) {
  271. printk(KERN_ERR PFX "mmio address 0x%04x already in use\n",
  272. val);
  273. goto unreg_region;
  274. }
  275. tcobase_phys = val;
  276. tcobase = ioremap(val, SP5100_WDT_MEM_MAP_SIZE);
  277. if (tcobase == 0) {
  278. printk(KERN_ERR PFX "failed to get tcobase address\n");
  279. goto unreg_mem_region;
  280. }
  281. /* Enable watchdog decode bit */
  282. pci_read_config_dword(sp5100_tco_pci,
  283. SP5100_PCI_WATCHDOG_MISC_REG,
  284. &val);
  285. val |= SP5100_PCI_WATCHDOG_DECODE_EN;
  286. pci_write_config_dword(sp5100_tco_pci,
  287. SP5100_PCI_WATCHDOG_MISC_REG,
  288. val);
  289. /* Enable Watchdog timer and set the resolution to 1 sec. */
  290. outb(SP5100_PM_WATCHDOG_CONTROL, SP5100_IO_PM_INDEX_REG);
  291. val = inb(SP5100_IO_PM_DATA_REG);
  292. val |= SP5100_PM_WATCHDOG_SECOND_RES;
  293. val &= ~SP5100_PM_WATCHDOG_DISABLE;
  294. outb(val, SP5100_IO_PM_DATA_REG);
  295. /* Check that the watchdog action is set to reset the system. */
  296. val = readl(SP5100_WDT_CONTROL(tcobase));
  297. val &= ~SP5100_PM_WATCHDOG_ACTION_RESET;
  298. writel(val, SP5100_WDT_CONTROL(tcobase));
  299. /* Set a reasonable heartbeat before we stop the timer */
  300. tco_timer_set_heartbeat(heartbeat);
  301. /*
  302. * Stop the TCO before we change anything so we don't race with
  303. * a zeroed timer.
  304. */
  305. tco_timer_stop();
  306. /* Done */
  307. return 1;
  308. unreg_mem_region:
  309. release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
  310. unreg_region:
  311. release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
  312. exit:
  313. return 0;
  314. }
  315. static int __devinit sp5100_tco_init(struct platform_device *dev)
  316. {
  317. int ret;
  318. u32 val;
  319. /* Check whether or not the hardware watchdog is there. If found, then
  320. * set it up.
  321. */
  322. if (!sp5100_tco_setupdevice())
  323. return -ENODEV;
  324. /* Check to see if last reboot was due to watchdog timeout */
  325. printk(KERN_INFO PFX "Watchdog reboot %sdetected.\n",
  326. readl(SP5100_WDT_CONTROL(tcobase)) & SP5100_PM_WATCHDOG_FIRED ?
  327. "" : "not ");
  328. /* Clear out the old status */
  329. val = readl(SP5100_WDT_CONTROL(tcobase));
  330. val &= ~SP5100_PM_WATCHDOG_FIRED;
  331. writel(val, SP5100_WDT_CONTROL(tcobase));
  332. /*
  333. * Check that the heartbeat value is within it's range.
  334. * If not, reset to the default.
  335. */
  336. if (tco_timer_set_heartbeat(heartbeat)) {
  337. heartbeat = WATCHDOG_HEARTBEAT;
  338. tco_timer_set_heartbeat(heartbeat);
  339. }
  340. ret = misc_register(&sp5100_tco_miscdev);
  341. if (ret != 0) {
  342. printk(KERN_ERR PFX "cannot register miscdev on minor="
  343. "%d (err=%d)\n",
  344. WATCHDOG_MINOR, ret);
  345. goto exit;
  346. }
  347. clear_bit(0, &timer_alive);
  348. printk(KERN_INFO PFX "initialized (0x%p). heartbeat=%d sec"
  349. " (nowayout=%d)\n",
  350. tcobase, heartbeat, nowayout);
  351. return 0;
  352. exit:
  353. iounmap(tcobase);
  354. release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
  355. release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
  356. return ret;
  357. }
  358. static void __devexit sp5100_tco_cleanup(void)
  359. {
  360. /* Stop the timer before we leave */
  361. if (!nowayout)
  362. tco_timer_stop();
  363. /* Deregister */
  364. misc_deregister(&sp5100_tco_miscdev);
  365. iounmap(tcobase);
  366. release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
  367. release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
  368. }
  369. static int __devexit sp5100_tco_remove(struct platform_device *dev)
  370. {
  371. if (tcobase)
  372. sp5100_tco_cleanup();
  373. return 0;
  374. }
  375. static void sp5100_tco_shutdown(struct platform_device *dev)
  376. {
  377. tco_timer_stop();
  378. }
  379. static struct platform_driver sp5100_tco_driver = {
  380. .probe = sp5100_tco_init,
  381. .remove = __devexit_p(sp5100_tco_remove),
  382. .shutdown = sp5100_tco_shutdown,
  383. .driver = {
  384. .owner = THIS_MODULE,
  385. .name = TCO_MODULE_NAME,
  386. },
  387. };
  388. static int __init sp5100_tco_init_module(void)
  389. {
  390. int err;
  391. printk(KERN_INFO PFX "SP5100 TCO WatchDog Timer Driver v%s\n",
  392. TCO_VERSION);
  393. err = platform_driver_register(&sp5100_tco_driver);
  394. if (err)
  395. return err;
  396. sp5100_tco_platform_device = platform_device_register_simple(
  397. TCO_MODULE_NAME, -1, NULL, 0);
  398. if (IS_ERR(sp5100_tco_platform_device)) {
  399. err = PTR_ERR(sp5100_tco_platform_device);
  400. goto unreg_platform_driver;
  401. }
  402. return 0;
  403. unreg_platform_driver:
  404. platform_driver_unregister(&sp5100_tco_driver);
  405. return err;
  406. }
  407. static void __exit sp5100_tco_cleanup_module(void)
  408. {
  409. platform_device_unregister(sp5100_tco_platform_device);
  410. platform_driver_unregister(&sp5100_tco_driver);
  411. printk(KERN_INFO PFX "SP5100 TCO Watchdog Module Unloaded.\n");
  412. }
  413. module_init(sp5100_tco_init_module);
  414. module_exit(sp5100_tco_cleanup_module);
  415. MODULE_AUTHOR("Priyanka Gupta");
  416. MODULE_DESCRIPTION("TCO timer driver for SP5100 chipset");
  417. MODULE_LICENSE("GPL");
  418. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);