ie6xx_wdt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel Atom E6xx Watchdog driver
  4. *
  5. * Copyright (C) 2011 Alexander Stein
  6. * <alexander.stein@systec-electronic.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/watchdog.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/spinlock.h>
  19. #define DRIVER_NAME "ie6xx_wdt"
  20. #define PV1 0x00
  21. #define PV2 0x04
  22. #define RR0 0x0c
  23. #define RR1 0x0d
  24. #define WDT_RELOAD 0x01
  25. #define WDT_TOUT 0x02
  26. #define WDTCR 0x10
  27. #define WDT_PRE_SEL 0x04
  28. #define WDT_RESET_SEL 0x08
  29. #define WDT_RESET_EN 0x10
  30. #define WDT_TOUT_EN 0x20
  31. #define DCR 0x14
  32. #define WDTLR 0x18
  33. #define WDT_LOCK 0x01
  34. #define WDT_ENABLE 0x02
  35. #define WDT_TOUT_CNF 0x03
  36. #define MIN_TIME 1
  37. #define MAX_TIME (10 * 60) /* 10 minutes */
  38. #define DEFAULT_TIME 60
  39. static unsigned int timeout = DEFAULT_TIME;
  40. module_param(timeout, uint, 0);
  41. MODULE_PARM_DESC(timeout,
  42. "Default Watchdog timer setting ("
  43. __MODULE_STRING(DEFAULT_TIME) "s)."
  44. "The range is from 1 to 600");
  45. static bool nowayout = WATCHDOG_NOWAYOUT;
  46. module_param(nowayout, bool, 0);
  47. MODULE_PARM_DESC(nowayout,
  48. "Watchdog cannot be stopped once started (default="
  49. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  50. static u8 resetmode = 0x10;
  51. module_param(resetmode, byte, 0);
  52. MODULE_PARM_DESC(resetmode,
  53. "Resetmode bits: 0x08 warm reset (cold reset otherwise), "
  54. "0x10 reset enable, 0x20 disable toggle GPIO[4] (default=0x10)");
  55. static struct {
  56. unsigned short sch_wdtba;
  57. spinlock_t unlock_sequence;
  58. #ifdef CONFIG_DEBUG_FS
  59. struct dentry *debugfs;
  60. #endif
  61. } ie6xx_wdt_data;
  62. /*
  63. * This is needed to write to preload and reload registers
  64. * struct ie6xx_wdt_data.unlock_sequence must be used
  65. * to prevent sequence interrupts
  66. */
  67. static void ie6xx_wdt_unlock_registers(void)
  68. {
  69. outb(0x80, ie6xx_wdt_data.sch_wdtba + RR0);
  70. outb(0x86, ie6xx_wdt_data.sch_wdtba + RR0);
  71. }
  72. static int ie6xx_wdt_ping(struct watchdog_device *wdd)
  73. {
  74. spin_lock(&ie6xx_wdt_data.unlock_sequence);
  75. ie6xx_wdt_unlock_registers();
  76. outb(WDT_RELOAD, ie6xx_wdt_data.sch_wdtba + RR1);
  77. spin_unlock(&ie6xx_wdt_data.unlock_sequence);
  78. return 0;
  79. }
  80. static int ie6xx_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
  81. {
  82. u32 preload;
  83. u64 clock;
  84. u8 wdtcr;
  85. /* Watchdog clock is PCI Clock (33MHz) */
  86. clock = 33000000;
  87. /* and the preload value is loaded into [34:15] of the down counter */
  88. preload = (t * clock) >> 15;
  89. /*
  90. * Manual states preload must be one less.
  91. * Does not wrap as t is at least 1
  92. */
  93. preload -= 1;
  94. spin_lock(&ie6xx_wdt_data.unlock_sequence);
  95. /* Set ResetMode & Enable prescaler for range 10ms to 10 min */
  96. wdtcr = resetmode & 0x38;
  97. outb(wdtcr, ie6xx_wdt_data.sch_wdtba + WDTCR);
  98. ie6xx_wdt_unlock_registers();
  99. outl(0, ie6xx_wdt_data.sch_wdtba + PV1);
  100. ie6xx_wdt_unlock_registers();
  101. outl(preload, ie6xx_wdt_data.sch_wdtba + PV2);
  102. ie6xx_wdt_unlock_registers();
  103. outb(WDT_RELOAD | WDT_TOUT, ie6xx_wdt_data.sch_wdtba + RR1);
  104. spin_unlock(&ie6xx_wdt_data.unlock_sequence);
  105. wdd->timeout = t;
  106. return 0;
  107. }
  108. static int ie6xx_wdt_start(struct watchdog_device *wdd)
  109. {
  110. ie6xx_wdt_set_timeout(wdd, wdd->timeout);
  111. /* Enable the watchdog timer */
  112. spin_lock(&ie6xx_wdt_data.unlock_sequence);
  113. outb(WDT_ENABLE, ie6xx_wdt_data.sch_wdtba + WDTLR);
  114. spin_unlock(&ie6xx_wdt_data.unlock_sequence);
  115. return 0;
  116. }
  117. static int ie6xx_wdt_stop(struct watchdog_device *wdd)
  118. {
  119. if (inb(ie6xx_wdt_data.sch_wdtba + WDTLR) & WDT_LOCK)
  120. return -1;
  121. /* Disable the watchdog timer */
  122. spin_lock(&ie6xx_wdt_data.unlock_sequence);
  123. outb(0, ie6xx_wdt_data.sch_wdtba + WDTLR);
  124. spin_unlock(&ie6xx_wdt_data.unlock_sequence);
  125. return 0;
  126. }
  127. static const struct watchdog_info ie6xx_wdt_info = {
  128. .identity = "Intel Atom E6xx Watchdog",
  129. .options = WDIOF_SETTIMEOUT |
  130. WDIOF_MAGICCLOSE |
  131. WDIOF_KEEPALIVEPING,
  132. };
  133. static const struct watchdog_ops ie6xx_wdt_ops = {
  134. .owner = THIS_MODULE,
  135. .start = ie6xx_wdt_start,
  136. .stop = ie6xx_wdt_stop,
  137. .ping = ie6xx_wdt_ping,
  138. .set_timeout = ie6xx_wdt_set_timeout,
  139. };
  140. static struct watchdog_device ie6xx_wdt_dev = {
  141. .info = &ie6xx_wdt_info,
  142. .ops = &ie6xx_wdt_ops,
  143. .min_timeout = MIN_TIME,
  144. .max_timeout = MAX_TIME,
  145. };
  146. #ifdef CONFIG_DEBUG_FS
  147. static int ie6xx_wdt_show(struct seq_file *s, void *unused)
  148. {
  149. seq_printf(s, "PV1 = 0x%08x\n",
  150. inl(ie6xx_wdt_data.sch_wdtba + PV1));
  151. seq_printf(s, "PV2 = 0x%08x\n",
  152. inl(ie6xx_wdt_data.sch_wdtba + PV2));
  153. seq_printf(s, "RR = 0x%08x\n",
  154. inw(ie6xx_wdt_data.sch_wdtba + RR0));
  155. seq_printf(s, "WDTCR = 0x%08x\n",
  156. inw(ie6xx_wdt_data.sch_wdtba + WDTCR));
  157. seq_printf(s, "DCR = 0x%08x\n",
  158. inl(ie6xx_wdt_data.sch_wdtba + DCR));
  159. seq_printf(s, "WDTLR = 0x%08x\n",
  160. inw(ie6xx_wdt_data.sch_wdtba + WDTLR));
  161. seq_printf(s, "\n");
  162. return 0;
  163. }
  164. DEFINE_SHOW_ATTRIBUTE(ie6xx_wdt);
  165. static void ie6xx_wdt_debugfs_init(void)
  166. {
  167. /* /sys/kernel/debug/ie6xx_wdt */
  168. ie6xx_wdt_data.debugfs = debugfs_create_file("ie6xx_wdt",
  169. S_IFREG | S_IRUGO, NULL, NULL, &ie6xx_wdt_fops);
  170. }
  171. static void ie6xx_wdt_debugfs_exit(void)
  172. {
  173. debugfs_remove(ie6xx_wdt_data.debugfs);
  174. }
  175. #else
  176. static void ie6xx_wdt_debugfs_init(void)
  177. {
  178. }
  179. static void ie6xx_wdt_debugfs_exit(void)
  180. {
  181. }
  182. #endif
  183. static int ie6xx_wdt_probe(struct platform_device *pdev)
  184. {
  185. struct resource *res;
  186. u8 wdtlr;
  187. int ret;
  188. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  189. if (!res)
  190. return -ENODEV;
  191. if (!request_region(res->start, resource_size(res), pdev->name)) {
  192. dev_err(&pdev->dev, "Watchdog region 0x%llx already in use!\n",
  193. (u64)res->start);
  194. return -EBUSY;
  195. }
  196. ie6xx_wdt_data.sch_wdtba = res->start;
  197. dev_dbg(&pdev->dev, "WDT = 0x%X\n", ie6xx_wdt_data.sch_wdtba);
  198. ie6xx_wdt_dev.timeout = timeout;
  199. watchdog_set_nowayout(&ie6xx_wdt_dev, nowayout);
  200. ie6xx_wdt_dev.parent = &pdev->dev;
  201. spin_lock_init(&ie6xx_wdt_data.unlock_sequence);
  202. wdtlr = inb(ie6xx_wdt_data.sch_wdtba + WDTLR);
  203. if (wdtlr & WDT_LOCK)
  204. dev_warn(&pdev->dev,
  205. "Watchdog Timer is Locked (Reg=0x%x)\n", wdtlr);
  206. ie6xx_wdt_debugfs_init();
  207. ret = watchdog_register_device(&ie6xx_wdt_dev);
  208. if (ret)
  209. goto misc_register_error;
  210. return 0;
  211. misc_register_error:
  212. ie6xx_wdt_debugfs_exit();
  213. release_region(res->start, resource_size(res));
  214. ie6xx_wdt_data.sch_wdtba = 0;
  215. return ret;
  216. }
  217. static int ie6xx_wdt_remove(struct platform_device *pdev)
  218. {
  219. struct resource *res;
  220. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  221. ie6xx_wdt_stop(NULL);
  222. watchdog_unregister_device(&ie6xx_wdt_dev);
  223. ie6xx_wdt_debugfs_exit();
  224. release_region(res->start, resource_size(res));
  225. ie6xx_wdt_data.sch_wdtba = 0;
  226. return 0;
  227. }
  228. static struct platform_driver ie6xx_wdt_driver = {
  229. .probe = ie6xx_wdt_probe,
  230. .remove = ie6xx_wdt_remove,
  231. .driver = {
  232. .name = DRIVER_NAME,
  233. },
  234. };
  235. static int __init ie6xx_wdt_init(void)
  236. {
  237. /* Check boot parameters to verify that their initial values */
  238. /* are in range. */
  239. if ((timeout < MIN_TIME) ||
  240. (timeout > MAX_TIME)) {
  241. pr_err("Watchdog timer: value of timeout %d (dec) "
  242. "is out of range from %d to %d (dec)\n",
  243. timeout, MIN_TIME, MAX_TIME);
  244. return -EINVAL;
  245. }
  246. return platform_driver_register(&ie6xx_wdt_driver);
  247. }
  248. static void __exit ie6xx_wdt_exit(void)
  249. {
  250. platform_driver_unregister(&ie6xx_wdt_driver);
  251. }
  252. late_initcall(ie6xx_wdt_init);
  253. module_exit(ie6xx_wdt_exit);
  254. MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
  255. MODULE_DESCRIPTION("Intel Atom E6xx Watchdog Device Driver");
  256. MODULE_LICENSE("GPL");
  257. MODULE_ALIAS("platform:" DRIVER_NAME);