i6300esb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. * 20171016 Radu Rendec <rrendec@arista.com>
  25. * Change driver to use the watchdog subsystem
  26. * Add support for multiple 6300ESB devices
  27. */
  28. /*
  29. * Includes, defines, variables, module parameters, ...
  30. */
  31. #include <linux/module.h>
  32. #include <linux/types.h>
  33. #include <linux/kernel.h>
  34. #include <linux/fs.h>
  35. #include <linux/mm.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/watchdog.h>
  38. #include <linux/pci.h>
  39. #include <linux/ioport.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/io.h>
  42. /* Module and version information */
  43. #define ESB_MODULE_NAME "i6300ESB timer"
  44. /* PCI configuration registers */
  45. #define ESB_CONFIG_REG 0x60 /* Config register */
  46. #define ESB_LOCK_REG 0x68 /* WDT lock register */
  47. /* Memory mapped registers */
  48. #define ESB_TIMER1_REG(w) ((w)->base + 0x00)/* Timer1 value after each reset */
  49. #define ESB_TIMER2_REG(w) ((w)->base + 0x04)/* Timer2 value after each reset */
  50. #define ESB_GINTSR_REG(w) ((w)->base + 0x08)/* General Interrupt Status Reg */
  51. #define ESB_RELOAD_REG(w) ((w)->base + 0x0c)/* Reload register */
  52. /* Lock register bits */
  53. #define ESB_WDT_FUNC (0x01 << 2) /* Watchdog functionality */
  54. #define ESB_WDT_ENABLE (0x01 << 1) /* Enable WDT */
  55. #define ESB_WDT_LOCK (0x01 << 0) /* Lock (nowayout) */
  56. /* Config register bits */
  57. #define ESB_WDT_REBOOT (0x01 << 5) /* Enable reboot on timeout */
  58. #define ESB_WDT_FREQ (0x01 << 2) /* Decrement frequency */
  59. #define ESB_WDT_INTTYPE (0x03 << 0) /* Interrupt type on timer1 timeout */
  60. /* Reload register bits */
  61. #define ESB_WDT_TIMEOUT (0x01 << 9) /* Watchdog timed out */
  62. #define ESB_WDT_RELOAD (0x01 << 8) /* prevent timeout */
  63. /* Magic constants */
  64. #define ESB_UNLOCK1 0x80 /* Step 1 to unlock reset registers */
  65. #define ESB_UNLOCK2 0x86 /* Step 2 to unlock reset registers */
  66. /* module parameters */
  67. /* 30 sec default heartbeat (1 < heartbeat < 2*1023) */
  68. #define ESB_HEARTBEAT_MIN 1
  69. #define ESB_HEARTBEAT_MAX 2046
  70. #define ESB_HEARTBEAT_DEFAULT 30
  71. #define ESB_HEARTBEAT_RANGE __MODULE_STRING(ESB_HEARTBEAT_MIN) \
  72. "<heartbeat<" __MODULE_STRING(ESB_HEARTBEAT_MAX)
  73. static int heartbeat; /* in seconds */
  74. module_param(heartbeat, int, 0);
  75. MODULE_PARM_DESC(heartbeat,
  76. "Watchdog heartbeat in seconds. (" ESB_HEARTBEAT_RANGE
  77. ", default=" __MODULE_STRING(ESB_HEARTBEAT_DEFAULT) ")");
  78. static bool nowayout = WATCHDOG_NOWAYOUT;
  79. module_param(nowayout, bool, 0);
  80. MODULE_PARM_DESC(nowayout,
  81. "Watchdog cannot be stopped once started (default="
  82. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  83. /* internal variables */
  84. struct esb_dev {
  85. struct watchdog_device wdd;
  86. void __iomem *base;
  87. struct pci_dev *pdev;
  88. };
  89. #define to_esb_dev(wptr) container_of(wptr, struct esb_dev, wdd)
  90. /*
  91. * Some i6300ESB specific functions
  92. */
  93. /*
  94. * Prepare for reloading the timer by unlocking the proper registers.
  95. * This is performed by first writing 0x80 followed by 0x86 to the
  96. * reload register. After this the appropriate registers can be written
  97. * to once before they need to be unlocked again.
  98. */
  99. static inline void esb_unlock_registers(struct esb_dev *edev)
  100. {
  101. writew(ESB_UNLOCK1, ESB_RELOAD_REG(edev));
  102. writew(ESB_UNLOCK2, ESB_RELOAD_REG(edev));
  103. }
  104. static int esb_timer_start(struct watchdog_device *wdd)
  105. {
  106. struct esb_dev *edev = to_esb_dev(wdd);
  107. int _wdd_nowayout = test_bit(WDOG_NO_WAY_OUT, &wdd->status);
  108. u8 val;
  109. esb_unlock_registers(edev);
  110. writew(ESB_WDT_RELOAD, ESB_RELOAD_REG(edev));
  111. /* Enable or Enable + Lock? */
  112. val = ESB_WDT_ENABLE | (_wdd_nowayout ? ESB_WDT_LOCK : 0x00);
  113. pci_write_config_byte(edev->pdev, ESB_LOCK_REG, val);
  114. return 0;
  115. }
  116. static int esb_timer_stop(struct watchdog_device *wdd)
  117. {
  118. struct esb_dev *edev = to_esb_dev(wdd);
  119. u8 val;
  120. /* First, reset timers as suggested by the docs */
  121. esb_unlock_registers(edev);
  122. writew(ESB_WDT_RELOAD, ESB_RELOAD_REG(edev));
  123. /* Then disable the WDT */
  124. pci_write_config_byte(edev->pdev, ESB_LOCK_REG, 0x0);
  125. pci_read_config_byte(edev->pdev, ESB_LOCK_REG, &val);
  126. /* Returns 0 if the timer was disabled, non-zero otherwise */
  127. return val & ESB_WDT_ENABLE;
  128. }
  129. static int esb_timer_keepalive(struct watchdog_device *wdd)
  130. {
  131. struct esb_dev *edev = to_esb_dev(wdd);
  132. esb_unlock_registers(edev);
  133. writew(ESB_WDT_RELOAD, ESB_RELOAD_REG(edev));
  134. /* FIXME: Do we need to flush anything here? */
  135. return 0;
  136. }
  137. static int esb_timer_set_heartbeat(struct watchdog_device *wdd,
  138. unsigned int time)
  139. {
  140. struct esb_dev *edev = to_esb_dev(wdd);
  141. u32 val;
  142. /* We shift by 9, so if we are passed a value of 1 sec,
  143. * val will be 1 << 9 = 512, then write that to two
  144. * timers => 2 * 512 = 1024 (which is decremented at 1KHz)
  145. */
  146. val = time << 9;
  147. /* Write timer 1 */
  148. esb_unlock_registers(edev);
  149. writel(val, ESB_TIMER1_REG(edev));
  150. /* Write timer 2 */
  151. esb_unlock_registers(edev);
  152. writel(val, ESB_TIMER2_REG(edev));
  153. /* Reload */
  154. esb_unlock_registers(edev);
  155. writew(ESB_WDT_RELOAD, ESB_RELOAD_REG(edev));
  156. /* FIXME: Do we need to flush everything out? */
  157. /* Done */
  158. wdd->timeout = time;
  159. return 0;
  160. }
  161. /*
  162. * Watchdog Subsystem Interfaces
  163. */
  164. static struct watchdog_info esb_info = {
  165. .identity = ESB_MODULE_NAME,
  166. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  167. };
  168. static const struct watchdog_ops esb_ops = {
  169. .owner = THIS_MODULE,
  170. .start = esb_timer_start,
  171. .stop = esb_timer_stop,
  172. .set_timeout = esb_timer_set_heartbeat,
  173. .ping = esb_timer_keepalive,
  174. };
  175. /*
  176. * Data for PCI driver interface
  177. */
  178. static const struct pci_device_id esb_pci_tbl[] = {
  179. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ESB_9), },
  180. { 0, }, /* End of list */
  181. };
  182. MODULE_DEVICE_TABLE(pci, esb_pci_tbl);
  183. /*
  184. * Init & exit routines
  185. */
  186. static unsigned char esb_getdevice(struct esb_dev *edev)
  187. {
  188. if (pci_enable_device(edev->pdev)) {
  189. dev_err(&edev->pdev->dev, "failed to enable device\n");
  190. goto err_devput;
  191. }
  192. if (pci_request_region(edev->pdev, 0, ESB_MODULE_NAME)) {
  193. dev_err(&edev->pdev->dev, "failed to request region\n");
  194. goto err_disable;
  195. }
  196. edev->base = pci_ioremap_bar(edev->pdev, 0);
  197. if (edev->base == NULL) {
  198. /* Something's wrong here, BASEADDR has to be set */
  199. dev_err(&edev->pdev->dev, "failed to get BASEADDR\n");
  200. goto err_release;
  201. }
  202. /* Done */
  203. dev_set_drvdata(&edev->pdev->dev, edev);
  204. return 1;
  205. err_release:
  206. pci_release_region(edev->pdev, 0);
  207. err_disable:
  208. pci_disable_device(edev->pdev);
  209. err_devput:
  210. return 0;
  211. }
  212. static void esb_initdevice(struct esb_dev *edev)
  213. {
  214. u8 val1;
  215. u16 val2;
  216. /*
  217. * Config register:
  218. * Bit 5 : 0 = Enable WDT_OUTPUT
  219. * Bit 2 : 0 = set the timer frequency to the PCI clock
  220. * divided by 2^15 (approx 1KHz).
  221. * Bits 1:0 : 11 = WDT_INT_TYPE Disabled.
  222. * The watchdog has two timers, it can be setup so that the
  223. * expiry of timer1 results in an interrupt and the expiry of
  224. * timer2 results in a reboot. We set it to not generate
  225. * any interrupts as there is not much we can do with it
  226. * right now.
  227. */
  228. pci_write_config_word(edev->pdev, ESB_CONFIG_REG, 0x0003);
  229. /* Check that the WDT isn't already locked */
  230. pci_read_config_byte(edev->pdev, ESB_LOCK_REG, &val1);
  231. if (val1 & ESB_WDT_LOCK)
  232. dev_warn(&edev->pdev->dev, "nowayout already set\n");
  233. /* Set the timer to watchdog mode and disable it for now */
  234. pci_write_config_byte(edev->pdev, ESB_LOCK_REG, 0x00);
  235. /* Check if the watchdog was previously triggered */
  236. esb_unlock_registers(edev);
  237. val2 = readw(ESB_RELOAD_REG(edev));
  238. if (val2 & ESB_WDT_TIMEOUT)
  239. edev->wdd.bootstatus = WDIOF_CARDRESET;
  240. /* Reset WDT_TIMEOUT flag and timers */
  241. esb_unlock_registers(edev);
  242. writew((ESB_WDT_TIMEOUT | ESB_WDT_RELOAD), ESB_RELOAD_REG(edev));
  243. /* And set the correct timeout value */
  244. esb_timer_set_heartbeat(&edev->wdd, edev->wdd.timeout);
  245. }
  246. static int esb_probe(struct pci_dev *pdev,
  247. const struct pci_device_id *ent)
  248. {
  249. struct esb_dev *edev;
  250. int ret;
  251. edev = devm_kzalloc(&pdev->dev, sizeof(*edev), GFP_KERNEL);
  252. if (!edev)
  253. return -ENOMEM;
  254. /* Check whether or not the hardware watchdog is there */
  255. edev->pdev = pdev;
  256. if (!esb_getdevice(edev))
  257. return -ENODEV;
  258. /* Initialize the watchdog and make sure it does not run */
  259. edev->wdd.info = &esb_info;
  260. edev->wdd.ops = &esb_ops;
  261. edev->wdd.min_timeout = ESB_HEARTBEAT_MIN;
  262. edev->wdd.max_timeout = ESB_HEARTBEAT_MAX;
  263. edev->wdd.timeout = ESB_HEARTBEAT_DEFAULT;
  264. if (watchdog_init_timeout(&edev->wdd, heartbeat, NULL))
  265. dev_info(&pdev->dev,
  266. "heartbeat value must be " ESB_HEARTBEAT_RANGE
  267. ", using %u\n", edev->wdd.timeout);
  268. watchdog_set_nowayout(&edev->wdd, nowayout);
  269. watchdog_stop_on_reboot(&edev->wdd);
  270. watchdog_stop_on_unregister(&edev->wdd);
  271. esb_initdevice(edev);
  272. /* Register the watchdog so that userspace has access to it */
  273. ret = watchdog_register_device(&edev->wdd);
  274. if (ret != 0) {
  275. dev_err(&pdev->dev,
  276. "cannot register watchdog device (err=%d)\n", ret);
  277. goto err_unmap;
  278. }
  279. dev_info(&pdev->dev,
  280. "initialized (0x%p). heartbeat=%d sec (nowayout=%d)\n",
  281. edev->base, edev->wdd.timeout, nowayout);
  282. return 0;
  283. err_unmap:
  284. iounmap(edev->base);
  285. pci_release_region(edev->pdev, 0);
  286. pci_disable_device(edev->pdev);
  287. return ret;
  288. }
  289. static void esb_remove(struct pci_dev *pdev)
  290. {
  291. struct esb_dev *edev = dev_get_drvdata(&pdev->dev);
  292. watchdog_unregister_device(&edev->wdd);
  293. iounmap(edev->base);
  294. pci_release_region(edev->pdev, 0);
  295. pci_disable_device(edev->pdev);
  296. }
  297. static struct pci_driver esb_driver = {
  298. .name = ESB_MODULE_NAME,
  299. .id_table = esb_pci_tbl,
  300. .probe = esb_probe,
  301. .remove = esb_remove,
  302. };
  303. module_pci_driver(esb_driver);
  304. MODULE_AUTHOR("Ross Biro and David Härdeman");
  305. MODULE_DESCRIPTION("Watchdog driver for Intel 6300ESB chipsets");
  306. MODULE_LICENSE("GPL");