i6300esb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * i6300esb: Watchdog timer driver for Intel 6300ESB chipset
  3. *
  4. * (c) Copyright 2004 Google Inc.
  5. * (c) Copyright 2005 David Härdeman <david@2gen.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * based on i810-tco.c which is in turn based on softdog.c
  13. *
  14. * The timer is implemented in the following I/O controller hubs:
  15. * (See the intel documentation on http://developer.intel.com.)
  16. * 6300ESB chip : document number 300641-004
  17. *
  18. * 2004YYZZ Ross Biro
  19. * Initial version 0.01
  20. * 2004YYZZ Ross Biro
  21. * Version 0.02
  22. * 20050210 David Härdeman <david@2gen.com>
  23. * Ported driver to kernel 2.6
  24. */
  25. /*
  26. * Includes, defines, variables, module parameters, ...
  27. */
  28. #include <linux/module.h>
  29. #include <linux/types.h>
  30. #include <linux/kernel.h>
  31. #include <linux/fs.h>
  32. #include <linux/mm.h>
  33. #include <linux/miscdevice.h>
  34. #include <linux/watchdog.h>
  35. #include <linux/init.h>
  36. #include <linux/pci.h>
  37. #include <linux/ioport.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/io.h>
  40. /* Module and version information */
  41. #define ESB_VERSION "0.05"
  42. #define ESB_MODULE_NAME "i6300ESB timer"
  43. #define ESB_DRIVER_NAME ESB_MODULE_NAME ", v" ESB_VERSION
  44. #define PFX ESB_MODULE_NAME ": "
  45. /* PCI configuration registers */
  46. #define ESB_CONFIG_REG 0x60 /* Config register */
  47. #define ESB_LOCK_REG 0x68 /* WDT lock register */
  48. /* Memory mapped registers */
  49. #define ESB_TIMER1_REG (BASEADDR + 0x00)/* Timer1 value after each reset */
  50. #define ESB_TIMER2_REG (BASEADDR + 0x04)/* Timer2 value after each reset */
  51. #define ESB_GINTSR_REG (BASEADDR + 0x08)/* General Interrupt Status Register */
  52. #define ESB_RELOAD_REG (BASEADDR + 0x0c)/* Reload register */
  53. /* Lock register bits */
  54. #define ESB_WDT_FUNC (0x01 << 2) /* Watchdog functionality */
  55. #define ESB_WDT_ENABLE (0x01 << 1) /* Enable WDT */
  56. #define ESB_WDT_LOCK (0x01 << 0) /* Lock (nowayout) */
  57. /* Config register bits */
  58. #define ESB_WDT_REBOOT (0x01 << 5) /* Enable reboot on timeout */
  59. #define ESB_WDT_FREQ (0x01 << 2) /* Decrement frequency */
  60. #define ESB_WDT_INTTYPE (0x03 << 0) /* Interrupt type on timer1 timeout */
  61. /* Reload register bits */
  62. #define ESB_WDT_TIMEOUT (0x01 << 9) /* Watchdog timed out */
  63. #define ESB_WDT_RELOAD (0x01 << 8) /* prevent timeout */
  64. /* Magic constants */
  65. #define ESB_UNLOCK1 0x80 /* Step 1 to unlock reset registers */
  66. #define ESB_UNLOCK2 0x86 /* Step 2 to unlock reset registers */
  67. /* internal variables */
  68. static void __iomem *BASEADDR;
  69. static DEFINE_SPINLOCK(esb_lock); /* Guards the hardware */
  70. static unsigned long timer_alive;
  71. static struct pci_dev *esb_pci;
  72. static unsigned short triggered; /* The status of the watchdog upon boot */
  73. static char esb_expect_close;
  74. /* We can only use 1 card due to the /dev/watchdog restriction */
  75. static int cards_found;
  76. /* module parameters */
  77. /* 30 sec default heartbeat (1 < heartbeat < 2*1023) */
  78. #define WATCHDOG_HEARTBEAT 30
  79. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  80. module_param(heartbeat, int, 0);
  81. MODULE_PARM_DESC(heartbeat,
  82. "Watchdog heartbeat in seconds. (1<heartbeat<2046, default="
  83. __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  84. static int nowayout = WATCHDOG_NOWAYOUT;
  85. module_param(nowayout, int, 0);
  86. MODULE_PARM_DESC(nowayout,
  87. "Watchdog cannot be stopped once started (default="
  88. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  89. /*
  90. * Some i6300ESB specific functions
  91. */
  92. /*
  93. * Prepare for reloading the timer by unlocking the proper registers.
  94. * This is performed by first writing 0x80 followed by 0x86 to the
  95. * reload register. After this the appropriate registers can be written
  96. * to once before they need to be unlocked again.
  97. */
  98. static inline void esb_unlock_registers(void)
  99. {
  100. writew(ESB_UNLOCK1, ESB_RELOAD_REG);
  101. writew(ESB_UNLOCK2, ESB_RELOAD_REG);
  102. }
  103. static int esb_timer_start(void)
  104. {
  105. u8 val;
  106. spin_lock(&esb_lock);
  107. esb_unlock_registers();
  108. writew(ESB_WDT_RELOAD, ESB_RELOAD_REG);
  109. /* Enable or Enable + Lock? */
  110. val = ESB_WDT_ENABLE | (nowayout ? ESB_WDT_LOCK : 0x00);
  111. pci_write_config_byte(esb_pci, ESB_LOCK_REG, val);
  112. spin_unlock(&esb_lock);
  113. return 0;
  114. }
  115. static int esb_timer_stop(void)
  116. {
  117. u8 val;
  118. spin_lock(&esb_lock);
  119. /* First, reset timers as suggested by the docs */
  120. esb_unlock_registers();
  121. writew(ESB_WDT_RELOAD, ESB_RELOAD_REG);
  122. /* Then disable the WDT */
  123. pci_write_config_byte(esb_pci, ESB_LOCK_REG, 0x0);
  124. pci_read_config_byte(esb_pci, ESB_LOCK_REG, &val);
  125. spin_unlock(&esb_lock);
  126. /* Returns 0 if the timer was disabled, non-zero otherwise */
  127. return val & ESB_WDT_ENABLE;
  128. }
  129. static void esb_timer_keepalive(void)
  130. {
  131. spin_lock(&esb_lock);
  132. esb_unlock_registers();
  133. writew(ESB_WDT_RELOAD, ESB_RELOAD_REG);
  134. /* FIXME: Do we need to flush anything here? */
  135. spin_unlock(&esb_lock);
  136. }
  137. static int esb_timer_set_heartbeat(int time)
  138. {
  139. u32 val;
  140. if (time < 0x1 || time > (2 * 0x03ff))
  141. return -EINVAL;
  142. spin_lock(&esb_lock);
  143. /* We shift by 9, so if we are passed a value of 1 sec,
  144. * val will be 1 << 9 = 512, then write that to two
  145. * timers => 2 * 512 = 1024 (which is decremented at 1KHz)
  146. */
  147. val = time << 9;
  148. /* Write timer 1 */
  149. esb_unlock_registers();
  150. writel(val, ESB_TIMER1_REG);
  151. /* Write timer 2 */
  152. esb_unlock_registers();
  153. writel(val, ESB_TIMER2_REG);
  154. /* Reload */
  155. esb_unlock_registers();
  156. writew(ESB_WDT_RELOAD, ESB_RELOAD_REG);
  157. /* FIXME: Do we need to flush everything out? */
  158. /* Done */
  159. heartbeat = time;
  160. spin_unlock(&esb_lock);
  161. return 0;
  162. }
  163. /*
  164. * /dev/watchdog handling
  165. */
  166. static int esb_open(struct inode *inode, struct file *file)
  167. {
  168. /* /dev/watchdog can only be opened once */
  169. if (test_and_set_bit(0, &timer_alive))
  170. return -EBUSY;
  171. /* Reload and activate timer */
  172. esb_timer_start();
  173. return nonseekable_open(inode, file);
  174. }
  175. static int esb_release(struct inode *inode, struct file *file)
  176. {
  177. /* Shut off the timer. */
  178. if (esb_expect_close == 42)
  179. esb_timer_stop();
  180. else {
  181. printk(KERN_CRIT PFX
  182. "Unexpected close, not stopping watchdog!\n");
  183. esb_timer_keepalive();
  184. }
  185. clear_bit(0, &timer_alive);
  186. esb_expect_close = 0;
  187. return 0;
  188. }
  189. static ssize_t esb_write(struct file *file, const char __user *data,
  190. size_t len, loff_t *ppos)
  191. {
  192. /* See if we got the magic character 'V' and reload the timer */
  193. if (len) {
  194. if (!nowayout) {
  195. size_t i;
  196. /* note: just in case someone wrote the magic character
  197. * five months ago... */
  198. esb_expect_close = 0;
  199. /* scan to see whether or not we got the
  200. * magic character */
  201. for (i = 0; i != len; i++) {
  202. char c;
  203. if (get_user(c, data + i))
  204. return -EFAULT;
  205. if (c == 'V')
  206. esb_expect_close = 42;
  207. }
  208. }
  209. /* someone wrote to us, we should reload the timer */
  210. esb_timer_keepalive();
  211. }
  212. return len;
  213. }
  214. static long esb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  215. {
  216. int new_options, retval = -EINVAL;
  217. int new_heartbeat;
  218. void __user *argp = (void __user *)arg;
  219. int __user *p = argp;
  220. static const struct watchdog_info ident = {
  221. .options = WDIOF_SETTIMEOUT |
  222. WDIOF_KEEPALIVEPING |
  223. WDIOF_MAGICCLOSE,
  224. .firmware_version = 0,
  225. .identity = ESB_MODULE_NAME,
  226. };
  227. switch (cmd) {
  228. case WDIOC_GETSUPPORT:
  229. return copy_to_user(argp, &ident,
  230. sizeof(ident)) ? -EFAULT : 0;
  231. case WDIOC_GETSTATUS:
  232. return put_user(0, p);
  233. case WDIOC_GETBOOTSTATUS:
  234. return put_user(triggered, p);
  235. case WDIOC_SETOPTIONS:
  236. {
  237. if (get_user(new_options, p))
  238. return -EFAULT;
  239. if (new_options & WDIOS_DISABLECARD) {
  240. esb_timer_stop();
  241. retval = 0;
  242. }
  243. if (new_options & WDIOS_ENABLECARD) {
  244. esb_timer_start();
  245. retval = 0;
  246. }
  247. return retval;
  248. }
  249. case WDIOC_KEEPALIVE:
  250. esb_timer_keepalive();
  251. return 0;
  252. case WDIOC_SETTIMEOUT:
  253. {
  254. if (get_user(new_heartbeat, p))
  255. return -EFAULT;
  256. if (esb_timer_set_heartbeat(new_heartbeat))
  257. return -EINVAL;
  258. esb_timer_keepalive();
  259. /* Fall */
  260. }
  261. case WDIOC_GETTIMEOUT:
  262. return put_user(heartbeat, p);
  263. default:
  264. return -ENOTTY;
  265. }
  266. }
  267. /*
  268. * Kernel Interfaces
  269. */
  270. static const struct file_operations esb_fops = {
  271. .owner = THIS_MODULE,
  272. .llseek = no_llseek,
  273. .write = esb_write,
  274. .unlocked_ioctl = esb_ioctl,
  275. .open = esb_open,
  276. .release = esb_release,
  277. };
  278. static struct miscdevice esb_miscdev = {
  279. .minor = WATCHDOG_MINOR,
  280. .name = "watchdog",
  281. .fops = &esb_fops,
  282. };
  283. /*
  284. * Data for PCI driver interface
  285. */
  286. static DEFINE_PCI_DEVICE_TABLE(esb_pci_tbl) = {
  287. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_9), },
  288. { 0, }, /* End of list */
  289. };
  290. MODULE_DEVICE_TABLE(pci, esb_pci_tbl);
  291. /*
  292. * Init & exit routines
  293. */
  294. static unsigned char __devinit esb_getdevice(struct pci_dev *pdev)
  295. {
  296. if (pci_enable_device(pdev)) {
  297. printk(KERN_ERR PFX "failed to enable device\n");
  298. goto err_devput;
  299. }
  300. if (pci_request_region(pdev, 0, ESB_MODULE_NAME)) {
  301. printk(KERN_ERR PFX "failed to request region\n");
  302. goto err_disable;
  303. }
  304. BASEADDR = pci_ioremap_bar(pdev, 0);
  305. if (BASEADDR == NULL) {
  306. /* Something's wrong here, BASEADDR has to be set */
  307. printk(KERN_ERR PFX "failed to get BASEADDR\n");
  308. goto err_release;
  309. }
  310. /* Done */
  311. esb_pci = pdev;
  312. return 1;
  313. err_release:
  314. pci_release_region(pdev, 0);
  315. err_disable:
  316. pci_disable_device(pdev);
  317. err_devput:
  318. return 0;
  319. }
  320. static void __devinit esb_initdevice(void)
  321. {
  322. u8 val1;
  323. u16 val2;
  324. /*
  325. * Config register:
  326. * Bit 5 : 0 = Enable WDT_OUTPUT
  327. * Bit 2 : 0 = set the timer frequency to the PCI clock
  328. * divided by 2^15 (approx 1KHz).
  329. * Bits 1:0 : 11 = WDT_INT_TYPE Disabled.
  330. * The watchdog has two timers, it can be setup so that the
  331. * expiry of timer1 results in an interrupt and the expiry of
  332. * timer2 results in a reboot. We set it to not generate
  333. * any interrupts as there is not much we can do with it
  334. * right now.
  335. */
  336. pci_write_config_word(esb_pci, ESB_CONFIG_REG, 0x0003);
  337. /* Check that the WDT isn't already locked */
  338. pci_read_config_byte(esb_pci, ESB_LOCK_REG, &val1);
  339. if (val1 & ESB_WDT_LOCK)
  340. printk(KERN_WARNING PFX "nowayout already set\n");
  341. /* Set the timer to watchdog mode and disable it for now */
  342. pci_write_config_byte(esb_pci, ESB_LOCK_REG, 0x00);
  343. /* Check if the watchdog was previously triggered */
  344. esb_unlock_registers();
  345. val2 = readw(ESB_RELOAD_REG);
  346. if (val2 & ESB_WDT_TIMEOUT)
  347. triggered = WDIOF_CARDRESET;
  348. /* Reset WDT_TIMEOUT flag and timers */
  349. esb_unlock_registers();
  350. writew((ESB_WDT_TIMEOUT | ESB_WDT_RELOAD), ESB_RELOAD_REG);
  351. /* And set the correct timeout value */
  352. esb_timer_set_heartbeat(heartbeat);
  353. }
  354. static int __devinit esb_probe(struct pci_dev *pdev,
  355. const struct pci_device_id *ent)
  356. {
  357. int ret;
  358. cards_found++;
  359. if (cards_found == 1)
  360. printk(KERN_INFO PFX "Intel 6300ESB WatchDog Timer Driver v%s\n",
  361. ESB_VERSION);
  362. if (cards_found > 1) {
  363. printk(KERN_ERR PFX "This driver only supports 1 device\n");
  364. return -ENODEV;
  365. }
  366. /* Check whether or not the hardware watchdog is there */
  367. if (!esb_getdevice(pdev) || esb_pci == NULL)
  368. return -ENODEV;
  369. /* Check that the heartbeat value is within it's range;
  370. if not reset to the default */
  371. if (heartbeat < 0x1 || heartbeat > 2 * 0x03ff) {
  372. heartbeat = WATCHDOG_HEARTBEAT;
  373. printk(KERN_INFO PFX
  374. "heartbeat value must be 1<heartbeat<2046, using %d\n",
  375. heartbeat);
  376. }
  377. /* Initialize the watchdog and make sure it does not run */
  378. esb_initdevice();
  379. /* Register the watchdog so that userspace has access to it */
  380. ret = misc_register(&esb_miscdev);
  381. if (ret != 0) {
  382. printk(KERN_ERR PFX
  383. "cannot register miscdev on minor=%d (err=%d)\n",
  384. WATCHDOG_MINOR, ret);
  385. goto err_unmap;
  386. }
  387. printk(KERN_INFO PFX
  388. "initialized (0x%p). heartbeat=%d sec (nowayout=%d)\n",
  389. BASEADDR, heartbeat, nowayout);
  390. return 0;
  391. err_unmap:
  392. iounmap(BASEADDR);
  393. pci_release_region(esb_pci, 0);
  394. pci_disable_device(esb_pci);
  395. esb_pci = NULL;
  396. return ret;
  397. }
  398. static void __devexit esb_remove(struct pci_dev *pdev)
  399. {
  400. /* Stop the timer before we leave */
  401. if (!nowayout)
  402. esb_timer_stop();
  403. /* Deregister */
  404. misc_deregister(&esb_miscdev);
  405. iounmap(BASEADDR);
  406. pci_release_region(esb_pci, 0);
  407. pci_disable_device(esb_pci);
  408. esb_pci = NULL;
  409. }
  410. static void esb_shutdown(struct pci_dev *pdev)
  411. {
  412. esb_timer_stop();
  413. }
  414. static struct pci_driver esb_driver = {
  415. .name = ESB_MODULE_NAME,
  416. .id_table = esb_pci_tbl,
  417. .probe = esb_probe,
  418. .remove = __devexit_p(esb_remove),
  419. .shutdown = esb_shutdown,
  420. };
  421. static int __init watchdog_init(void)
  422. {
  423. return pci_register_driver(&esb_driver);
  424. }
  425. static void __exit watchdog_cleanup(void)
  426. {
  427. pci_unregister_driver(&esb_driver);
  428. printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
  429. }
  430. module_init(watchdog_init);
  431. module_exit(watchdog_cleanup);
  432. MODULE_AUTHOR("Ross Biro and David Härdeman");
  433. MODULE_DESCRIPTION("Watchdog driver for Intel 6300ESB chipsets");
  434. MODULE_LICENSE("GPL");
  435. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);