i6300esb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/module.h>
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/fs.h>
  33. #include <linux/mm.h>
  34. #include <linux/miscdevice.h>
  35. #include <linux/watchdog.h>
  36. #include <linux/init.h>
  37. #include <linux/pci.h>
  38. #include <linux/ioport.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/io.h>
  41. /* Module and version information */
  42. #define ESB_VERSION "0.05"
  43. #define ESB_MODULE_NAME "i6300ESB timer"
  44. #define ESB_DRIVER_NAME ESB_MODULE_NAME ", v" ESB_VERSION
  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 bool nowayout = WATCHDOG_NOWAYOUT;
  85. module_param(nowayout, bool, 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. pr_crit("Unexpected close, not stopping watchdog!\n");
  182. esb_timer_keepalive();
  183. }
  184. clear_bit(0, &timer_alive);
  185. esb_expect_close = 0;
  186. return 0;
  187. }
  188. static ssize_t esb_write(struct file *file, const char __user *data,
  189. size_t len, loff_t *ppos)
  190. {
  191. /* See if we got the magic character 'V' and reload the timer */
  192. if (len) {
  193. if (!nowayout) {
  194. size_t i;
  195. /* note: just in case someone wrote the magic character
  196. * five months ago... */
  197. esb_expect_close = 0;
  198. /* scan to see whether or not we got the
  199. * magic character */
  200. for (i = 0; i != len; i++) {
  201. char c;
  202. if (get_user(c, data + i))
  203. return -EFAULT;
  204. if (c == 'V')
  205. esb_expect_close = 42;
  206. }
  207. }
  208. /* someone wrote to us, we should reload the timer */
  209. esb_timer_keepalive();
  210. }
  211. return len;
  212. }
  213. static long esb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  214. {
  215. int new_options, retval = -EINVAL;
  216. int new_heartbeat;
  217. void __user *argp = (void __user *)arg;
  218. int __user *p = argp;
  219. static const struct watchdog_info ident = {
  220. .options = WDIOF_SETTIMEOUT |
  221. WDIOF_KEEPALIVEPING |
  222. WDIOF_MAGICCLOSE,
  223. .firmware_version = 0,
  224. .identity = ESB_MODULE_NAME,
  225. };
  226. switch (cmd) {
  227. case WDIOC_GETSUPPORT:
  228. return copy_to_user(argp, &ident,
  229. sizeof(ident)) ? -EFAULT : 0;
  230. case WDIOC_GETSTATUS:
  231. return put_user(0, p);
  232. case WDIOC_GETBOOTSTATUS:
  233. return put_user(triggered, p);
  234. case WDIOC_SETOPTIONS:
  235. {
  236. if (get_user(new_options, p))
  237. return -EFAULT;
  238. if (new_options & WDIOS_DISABLECARD) {
  239. esb_timer_stop();
  240. retval = 0;
  241. }
  242. if (new_options & WDIOS_ENABLECARD) {
  243. esb_timer_start();
  244. retval = 0;
  245. }
  246. return retval;
  247. }
  248. case WDIOC_KEEPALIVE:
  249. esb_timer_keepalive();
  250. return 0;
  251. case WDIOC_SETTIMEOUT:
  252. {
  253. if (get_user(new_heartbeat, p))
  254. return -EFAULT;
  255. if (esb_timer_set_heartbeat(new_heartbeat))
  256. return -EINVAL;
  257. esb_timer_keepalive();
  258. /* Fall */
  259. }
  260. case WDIOC_GETTIMEOUT:
  261. return put_user(heartbeat, p);
  262. default:
  263. return -ENOTTY;
  264. }
  265. }
  266. /*
  267. * Kernel Interfaces
  268. */
  269. static const struct file_operations esb_fops = {
  270. .owner = THIS_MODULE,
  271. .llseek = no_llseek,
  272. .write = esb_write,
  273. .unlocked_ioctl = esb_ioctl,
  274. .open = esb_open,
  275. .release = esb_release,
  276. };
  277. static struct miscdevice esb_miscdev = {
  278. .minor = WATCHDOG_MINOR,
  279. .name = "watchdog",
  280. .fops = &esb_fops,
  281. };
  282. /*
  283. * Data for PCI driver interface
  284. */
  285. static DEFINE_PCI_DEVICE_TABLE(esb_pci_tbl) = {
  286. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_9), },
  287. { 0, }, /* End of list */
  288. };
  289. MODULE_DEVICE_TABLE(pci, esb_pci_tbl);
  290. /*
  291. * Init & exit routines
  292. */
  293. static unsigned char __devinit esb_getdevice(struct pci_dev *pdev)
  294. {
  295. if (pci_enable_device(pdev)) {
  296. pr_err("failed to enable device\n");
  297. goto err_devput;
  298. }
  299. if (pci_request_region(pdev, 0, ESB_MODULE_NAME)) {
  300. pr_err("failed to request region\n");
  301. goto err_disable;
  302. }
  303. BASEADDR = pci_ioremap_bar(pdev, 0);
  304. if (BASEADDR == NULL) {
  305. /* Something's wrong here, BASEADDR has to be set */
  306. pr_err("failed to get BASEADDR\n");
  307. goto err_release;
  308. }
  309. /* Done */
  310. esb_pci = pdev;
  311. return 1;
  312. err_release:
  313. pci_release_region(pdev, 0);
  314. err_disable:
  315. pci_disable_device(pdev);
  316. err_devput:
  317. return 0;
  318. }
  319. static void __devinit esb_initdevice(void)
  320. {
  321. u8 val1;
  322. u16 val2;
  323. /*
  324. * Config register:
  325. * Bit 5 : 0 = Enable WDT_OUTPUT
  326. * Bit 2 : 0 = set the timer frequency to the PCI clock
  327. * divided by 2^15 (approx 1KHz).
  328. * Bits 1:0 : 11 = WDT_INT_TYPE Disabled.
  329. * The watchdog has two timers, it can be setup so that the
  330. * expiry of timer1 results in an interrupt and the expiry of
  331. * timer2 results in a reboot. We set it to not generate
  332. * any interrupts as there is not much we can do with it
  333. * right now.
  334. */
  335. pci_write_config_word(esb_pci, ESB_CONFIG_REG, 0x0003);
  336. /* Check that the WDT isn't already locked */
  337. pci_read_config_byte(esb_pci, ESB_LOCK_REG, &val1);
  338. if (val1 & ESB_WDT_LOCK)
  339. pr_warn("nowayout already set\n");
  340. /* Set the timer to watchdog mode and disable it for now */
  341. pci_write_config_byte(esb_pci, ESB_LOCK_REG, 0x00);
  342. /* Check if the watchdog was previously triggered */
  343. esb_unlock_registers();
  344. val2 = readw(ESB_RELOAD_REG);
  345. if (val2 & ESB_WDT_TIMEOUT)
  346. triggered = WDIOF_CARDRESET;
  347. /* Reset WDT_TIMEOUT flag and timers */
  348. esb_unlock_registers();
  349. writew((ESB_WDT_TIMEOUT | ESB_WDT_RELOAD), ESB_RELOAD_REG);
  350. /* And set the correct timeout value */
  351. esb_timer_set_heartbeat(heartbeat);
  352. }
  353. static int __devinit esb_probe(struct pci_dev *pdev,
  354. const struct pci_device_id *ent)
  355. {
  356. int ret;
  357. cards_found++;
  358. if (cards_found == 1)
  359. pr_info("Intel 6300ESB WatchDog Timer Driver v%s\n",
  360. ESB_VERSION);
  361. if (cards_found > 1) {
  362. pr_err("This driver only supports 1 device\n");
  363. return -ENODEV;
  364. }
  365. /* Check whether or not the hardware watchdog is there */
  366. if (!esb_getdevice(pdev) || esb_pci == NULL)
  367. return -ENODEV;
  368. /* Check that the heartbeat value is within it's range;
  369. if not reset to the default */
  370. if (heartbeat < 0x1 || heartbeat > 2 * 0x03ff) {
  371. heartbeat = WATCHDOG_HEARTBEAT;
  372. pr_info("heartbeat value must be 1<heartbeat<2046, using %d\n",
  373. heartbeat);
  374. }
  375. /* Initialize the watchdog and make sure it does not run */
  376. esb_initdevice();
  377. /* Register the watchdog so that userspace has access to it */
  378. ret = misc_register(&esb_miscdev);
  379. if (ret != 0) {
  380. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  381. WATCHDOG_MINOR, ret);
  382. goto err_unmap;
  383. }
  384. pr_info("initialized (0x%p). heartbeat=%d sec (nowayout=%d)\n",
  385. BASEADDR, heartbeat, nowayout);
  386. return 0;
  387. err_unmap:
  388. iounmap(BASEADDR);
  389. pci_release_region(esb_pci, 0);
  390. pci_disable_device(esb_pci);
  391. esb_pci = NULL;
  392. return ret;
  393. }
  394. static void __devexit esb_remove(struct pci_dev *pdev)
  395. {
  396. /* Stop the timer before we leave */
  397. if (!nowayout)
  398. esb_timer_stop();
  399. /* Deregister */
  400. misc_deregister(&esb_miscdev);
  401. iounmap(BASEADDR);
  402. pci_release_region(esb_pci, 0);
  403. pci_disable_device(esb_pci);
  404. esb_pci = NULL;
  405. }
  406. static void esb_shutdown(struct pci_dev *pdev)
  407. {
  408. esb_timer_stop();
  409. }
  410. static struct pci_driver esb_driver = {
  411. .name = ESB_MODULE_NAME,
  412. .id_table = esb_pci_tbl,
  413. .probe = esb_probe,
  414. .remove = __devexit_p(esb_remove),
  415. .shutdown = esb_shutdown,
  416. };
  417. static int __init watchdog_init(void)
  418. {
  419. return pci_register_driver(&esb_driver);
  420. }
  421. static void __exit watchdog_cleanup(void)
  422. {
  423. pci_unregister_driver(&esb_driver);
  424. pr_info("Watchdog Module Unloaded\n");
  425. }
  426. module_init(watchdog_init);
  427. module_exit(watchdog_cleanup);
  428. MODULE_AUTHOR("Ross Biro and David Härdeman");
  429. MODULE_DESCRIPTION("Watchdog driver for Intel 6300ESB chipsets");
  430. MODULE_LICENSE("GPL");
  431. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);