nv_tco.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * nv_tco 0.01: TCO timer driver for NV chipsets
  3. *
  4. * (c) Copyright 2005 Google Inc., All Rights Reserved.
  5. *
  6. * Based off 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. * TCO timer driver for NV chipsets
  17. * based on softdog.c by Alan Cox <alan@redhat.com>
  18. */
  19. /*
  20. * Includes, defines, variables, module parameters, ...
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/types.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/watchdog.h>
  27. #include <linux/init.h>
  28. #include <linux/fs.h>
  29. #include <linux/pci.h>
  30. #include <linux/ioport.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include "nv_tco.h"
  36. /* Module and version information */
  37. #define TCO_VERSION "0.01"
  38. #define TCO_MODULE_NAME "NV_TCO"
  39. #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
  40. #define PFX TCO_MODULE_NAME ": "
  41. /* internal variables */
  42. static unsigned int tcobase;
  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 *tco_pci;
  47. /* the watchdog platform device */
  48. static struct platform_device *nv_tco_platform_device;
  49. /* module parameters */
  50. #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat (2<heartbeat<39) */
  51. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  52. module_param(heartbeat, int, 0);
  53. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39, "
  54. "default=" __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 inline unsigned char seconds_to_ticks(int seconds)
  63. {
  64. /* the internal timer is stored as ticks which decrement
  65. * every 0.6 seconds */
  66. return (seconds * 10) / 6;
  67. }
  68. static void tco_timer_start(void)
  69. {
  70. u32 val;
  71. unsigned long flags;
  72. spin_lock_irqsave(&tco_lock, flags);
  73. val = inl(TCO_CNT(tcobase));
  74. val &= ~TCO_CNT_TCOHALT;
  75. outl(val, TCO_CNT(tcobase));
  76. spin_unlock_irqrestore(&tco_lock, flags);
  77. }
  78. static void tco_timer_stop(void)
  79. {
  80. u32 val;
  81. unsigned long flags;
  82. spin_lock_irqsave(&tco_lock, flags);
  83. val = inl(TCO_CNT(tcobase));
  84. val |= TCO_CNT_TCOHALT;
  85. outl(val, TCO_CNT(tcobase));
  86. spin_unlock_irqrestore(&tco_lock, flags);
  87. }
  88. static void tco_timer_keepalive(void)
  89. {
  90. unsigned long flags;
  91. spin_lock_irqsave(&tco_lock, flags);
  92. outb(0x01, TCO_RLD(tcobase));
  93. spin_unlock_irqrestore(&tco_lock, flags);
  94. }
  95. static int tco_timer_set_heartbeat(int t)
  96. {
  97. int ret = 0;
  98. unsigned char tmrval;
  99. unsigned long flags;
  100. u8 val;
  101. /*
  102. * note seconds_to_ticks(t) > t, so if t > 0x3f, so is
  103. * tmrval=seconds_to_ticks(t). Check that the count in seconds isn't
  104. * out of range on it's own (to avoid overflow in tmrval).
  105. */
  106. if (t < 0 || t > 0x3f)
  107. return -EINVAL;
  108. tmrval = seconds_to_ticks(t);
  109. /* "Values of 0h-3h are ignored and should not be attempted" */
  110. if (tmrval > 0x3f || tmrval < 0x04)
  111. return -EINVAL;
  112. /* Write new heartbeat to watchdog */
  113. spin_lock_irqsave(&tco_lock, flags);
  114. val = inb(TCO_TMR(tcobase));
  115. val &= 0xc0;
  116. val |= tmrval;
  117. outb(val, TCO_TMR(tcobase));
  118. val = inb(TCO_TMR(tcobase));
  119. if ((val & 0x3f) != tmrval)
  120. ret = -EINVAL;
  121. spin_unlock_irqrestore(&tco_lock, flags);
  122. if (ret)
  123. return ret;
  124. heartbeat = t;
  125. return 0;
  126. }
  127. /*
  128. * /dev/watchdog handling
  129. */
  130. static int nv_tco_open(struct inode *inode, struct file *file)
  131. {
  132. /* /dev/watchdog can only be opened once */
  133. if (test_and_set_bit(0, &timer_alive))
  134. return -EBUSY;
  135. /* Reload and activate timer */
  136. tco_timer_keepalive();
  137. tco_timer_start();
  138. return nonseekable_open(inode, file);
  139. }
  140. static int nv_tco_release(struct inode *inode, struct file *file)
  141. {
  142. /* Shut off the timer */
  143. if (tco_expect_close == 42) {
  144. tco_timer_stop();
  145. } else {
  146. printk(KERN_CRIT PFX "Unexpected close, not stopping "
  147. "watchdog!\n");
  148. tco_timer_keepalive();
  149. }
  150. clear_bit(0, &timer_alive);
  151. tco_expect_close = 0;
  152. return 0;
  153. }
  154. static ssize_t nv_tco_write(struct file *file, const char __user *data,
  155. size_t len, loff_t *ppos)
  156. {
  157. /* See if we got the magic character 'V' and reload the timer */
  158. if (len) {
  159. if (!nowayout) {
  160. size_t i;
  161. /*
  162. * note: just in case someone wrote the magic character
  163. * five months ago...
  164. */
  165. tco_expect_close = 0;
  166. /*
  167. * scan to see whether or not we got the magic
  168. * character
  169. */
  170. for (i = 0; i != len; i++) {
  171. char c;
  172. if (get_user(c, data + i))
  173. return -EFAULT;
  174. if (c == 'V')
  175. tco_expect_close = 42;
  176. }
  177. }
  178. /* someone wrote to us, we should reload the timer */
  179. tco_timer_keepalive();
  180. }
  181. return len;
  182. }
  183. static long nv_tco_ioctl(struct file *file, unsigned int cmd,
  184. unsigned long arg)
  185. {
  186. int new_options, retval = -EINVAL;
  187. int new_heartbeat;
  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 |
  193. WDIOF_MAGICCLOSE,
  194. .firmware_version = 0,
  195. .identity = TCO_MODULE_NAME,
  196. };
  197. switch (cmd) {
  198. case WDIOC_GETSUPPORT:
  199. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  200. case WDIOC_GETSTATUS:
  201. case WDIOC_GETBOOTSTATUS:
  202. return put_user(0, p);
  203. case WDIOC_SETOPTIONS:
  204. if (get_user(new_options, p))
  205. return -EFAULT;
  206. if (new_options & WDIOS_DISABLECARD) {
  207. tco_timer_stop();
  208. retval = 0;
  209. }
  210. if (new_options & WDIOS_ENABLECARD) {
  211. tco_timer_keepalive();
  212. tco_timer_start();
  213. retval = 0;
  214. }
  215. return retval;
  216. case WDIOC_KEEPALIVE:
  217. tco_timer_keepalive();
  218. return 0;
  219. case WDIOC_SETTIMEOUT:
  220. if (get_user(new_heartbeat, p))
  221. return -EFAULT;
  222. if (tco_timer_set_heartbeat(new_heartbeat))
  223. return -EINVAL;
  224. tco_timer_keepalive();
  225. /* Fall through */
  226. case WDIOC_GETTIMEOUT:
  227. return put_user(heartbeat, p);
  228. default:
  229. return -ENOTTY;
  230. }
  231. }
  232. /*
  233. * Kernel Interfaces
  234. */
  235. static const struct file_operations nv_tco_fops = {
  236. .owner = THIS_MODULE,
  237. .llseek = no_llseek,
  238. .write = nv_tco_write,
  239. .unlocked_ioctl = nv_tco_ioctl,
  240. .open = nv_tco_open,
  241. .release = nv_tco_release,
  242. };
  243. static struct miscdevice nv_tco_miscdev = {
  244. .minor = WATCHDOG_MINOR,
  245. .name = "watchdog",
  246. .fops = &nv_tco_fops,
  247. };
  248. /*
  249. * Data for PCI driver interface
  250. *
  251. * This data only exists for exporting the supported
  252. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  253. * register a pci_driver, because someone else might one day
  254. * want to register another driver on the same PCI id.
  255. */
  256. static DEFINE_PCI_DEVICE_TABLE(tco_pci_tbl) = {
  257. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS,
  258. PCI_ANY_ID, PCI_ANY_ID, },
  259. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS,
  260. PCI_ANY_ID, PCI_ANY_ID, },
  261. { 0, }, /* End of list */
  262. };
  263. MODULE_DEVICE_TABLE(pci, tco_pci_tbl);
  264. /*
  265. * Init & exit routines
  266. */
  267. static unsigned char __devinit nv_tco_getdevice(void)
  268. {
  269. struct pci_dev *dev = NULL;
  270. u32 val;
  271. /* Find the PCI device */
  272. for_each_pci_dev(dev) {
  273. if (pci_match_id(tco_pci_tbl, dev) != NULL) {
  274. tco_pci = dev;
  275. break;
  276. }
  277. }
  278. if (!tco_pci)
  279. return 0;
  280. /* Find the base io port */
  281. pci_read_config_dword(tco_pci, 0x64, &val);
  282. val &= 0xffff;
  283. if (val == 0x0001 || val == 0x0000) {
  284. /* Something is wrong here, bar isn't setup */
  285. printk(KERN_ERR PFX "failed to get tcobase address\n");
  286. return 0;
  287. }
  288. val &= 0xff00;
  289. tcobase = val + 0x40;
  290. if (!request_region(tcobase, 0x10, "NV TCO")) {
  291. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  292. tcobase);
  293. return 0;
  294. }
  295. /* Set a reasonable heartbeat before we stop the timer */
  296. tco_timer_set_heartbeat(30);
  297. /*
  298. * Stop the TCO before we change anything so we don't race with
  299. * a zeroed timer.
  300. */
  301. tco_timer_keepalive();
  302. tco_timer_stop();
  303. /* Disable SMI caused by TCO */
  304. if (!request_region(MCP51_SMI_EN(tcobase), 4, "NV TCO")) {
  305. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  306. MCP51_SMI_EN(tcobase));
  307. goto out;
  308. }
  309. val = inl(MCP51_SMI_EN(tcobase));
  310. val &= ~MCP51_SMI_EN_TCO;
  311. outl(val, MCP51_SMI_EN(tcobase));
  312. val = inl(MCP51_SMI_EN(tcobase));
  313. release_region(MCP51_SMI_EN(tcobase), 4);
  314. if (val & MCP51_SMI_EN_TCO) {
  315. printk(KERN_ERR PFX "Could not disable SMI caused by TCO\n");
  316. goto out;
  317. }
  318. /* Check chipset's NO_REBOOT bit */
  319. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  320. val |= MCP51_SMBUS_SETUP_B_TCO_REBOOT;
  321. pci_write_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, val);
  322. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  323. if (!(val & MCP51_SMBUS_SETUP_B_TCO_REBOOT)) {
  324. printk(KERN_ERR PFX "failed to reset NO_REBOOT flag, reboot "
  325. "disabled by hardware\n");
  326. goto out;
  327. }
  328. return 1;
  329. out:
  330. release_region(tcobase, 0x10);
  331. return 0;
  332. }
  333. static int __devinit nv_tco_init(struct platform_device *dev)
  334. {
  335. int ret;
  336. /* Check whether or not the hardware watchdog is there */
  337. if (!nv_tco_getdevice())
  338. return -ENODEV;
  339. /* Check to see if last reboot was due to watchdog timeout */
  340. printk(KERN_INFO PFX "Watchdog reboot %sdetected.\n",
  341. inl(TCO_STS(tcobase)) & TCO_STS_TCO2TO_STS ? "" : "not ");
  342. /* Clear out the old status */
  343. outl(TCO_STS_RESET, TCO_STS(tcobase));
  344. /*
  345. * Check that the heartbeat value is within it's range.
  346. * If not, reset to the default.
  347. */
  348. if (tco_timer_set_heartbeat(heartbeat)) {
  349. heartbeat = WATCHDOG_HEARTBEAT;
  350. tco_timer_set_heartbeat(heartbeat);
  351. printk(KERN_INFO PFX "heartbeat value must be 2<heartbeat<39, "
  352. "using %d\n", heartbeat);
  353. }
  354. ret = misc_register(&nv_tco_miscdev);
  355. if (ret != 0) {
  356. printk(KERN_ERR PFX "cannot register miscdev on minor=%d "
  357. "(err=%d)\n", WATCHDOG_MINOR, ret);
  358. goto unreg_region;
  359. }
  360. clear_bit(0, &timer_alive);
  361. tco_timer_stop();
  362. printk(KERN_INFO PFX "initialized (0x%04x). heartbeat=%d sec "
  363. "(nowayout=%d)\n", tcobase, heartbeat, nowayout);
  364. return 0;
  365. unreg_region:
  366. release_region(tcobase, 0x10);
  367. return ret;
  368. }
  369. static void __devexit nv_tco_cleanup(void)
  370. {
  371. u32 val;
  372. /* Stop the timer before we leave */
  373. if (!nowayout)
  374. tco_timer_stop();
  375. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  376. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  377. val &= ~MCP51_SMBUS_SETUP_B_TCO_REBOOT;
  378. pci_write_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, val);
  379. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  380. if (val & MCP51_SMBUS_SETUP_B_TCO_REBOOT) {
  381. printk(KERN_CRIT PFX "Couldn't unset REBOOT bit. Machine may "
  382. "soon reset\n");
  383. }
  384. /* Deregister */
  385. misc_deregister(&nv_tco_miscdev);
  386. release_region(tcobase, 0x10);
  387. }
  388. static int __devexit nv_tco_remove(struct platform_device *dev)
  389. {
  390. if (tcobase)
  391. nv_tco_cleanup();
  392. return 0;
  393. }
  394. static void nv_tco_shutdown(struct platform_device *dev)
  395. {
  396. tco_timer_stop();
  397. }
  398. static struct platform_driver nv_tco_driver = {
  399. .probe = nv_tco_init,
  400. .remove = __devexit_p(nv_tco_remove),
  401. .shutdown = nv_tco_shutdown,
  402. .driver = {
  403. .owner = THIS_MODULE,
  404. .name = TCO_MODULE_NAME,
  405. },
  406. };
  407. static int __init nv_tco_init_module(void)
  408. {
  409. int err;
  410. printk(KERN_INFO PFX "NV TCO WatchDog Timer Driver v%s\n",
  411. TCO_VERSION);
  412. err = platform_driver_register(&nv_tco_driver);
  413. if (err)
  414. return err;
  415. nv_tco_platform_device = platform_device_register_simple(
  416. TCO_MODULE_NAME, -1, NULL, 0);
  417. if (IS_ERR(nv_tco_platform_device)) {
  418. err = PTR_ERR(nv_tco_platform_device);
  419. goto unreg_platform_driver;
  420. }
  421. return 0;
  422. unreg_platform_driver:
  423. platform_driver_unregister(&nv_tco_driver);
  424. return err;
  425. }
  426. static void __exit nv_tco_cleanup_module(void)
  427. {
  428. platform_device_unregister(nv_tco_platform_device);
  429. platform_driver_unregister(&nv_tco_driver);
  430. printk(KERN_INFO PFX "NV TCO Watchdog Module Unloaded.\n");
  431. }
  432. module_init(nv_tco_init_module);
  433. module_exit(nv_tco_cleanup_module);
  434. MODULE_AUTHOR("Mike Waychison");
  435. MODULE_DESCRIPTION("TCO timer driver for NV chipsets");
  436. MODULE_LICENSE("GPL");
  437. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);