npcm_wdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018 Nuvoton Technology corporation.
  3. // Copyright (c) 2018 IBM Corp.
  4. #include <linux/bitops.h>
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/watchdog.h>
  14. #define NPCM_WTCR 0x1C
  15. #define NPCM_WTCLK (BIT(10) | BIT(11)) /* Clock divider */
  16. #define NPCM_WTE BIT(7) /* Enable */
  17. #define NPCM_WTIE BIT(6) /* Enable irq */
  18. #define NPCM_WTIS (BIT(4) | BIT(5)) /* Interval selection */
  19. #define NPCM_WTIF BIT(3) /* Interrupt flag*/
  20. #define NPCM_WTRF BIT(2) /* Reset flag */
  21. #define NPCM_WTRE BIT(1) /* Reset enable */
  22. #define NPCM_WTR BIT(0) /* Reset counter */
  23. /*
  24. * Watchdog timeouts
  25. *
  26. * 170 msec: WTCLK=01 WTIS=00 VAL= 0x400
  27. * 670 msec: WTCLK=01 WTIS=01 VAL= 0x410
  28. * 1360 msec: WTCLK=10 WTIS=00 VAL= 0x800
  29. * 2700 msec: WTCLK=01 WTIS=10 VAL= 0x420
  30. * 5360 msec: WTCLK=10 WTIS=01 VAL= 0x810
  31. * 10700 msec: WTCLK=01 WTIS=11 VAL= 0x430
  32. * 21600 msec: WTCLK=10 WTIS=10 VAL= 0x820
  33. * 43000 msec: WTCLK=11 WTIS=00 VAL= 0xC00
  34. * 85600 msec: WTCLK=10 WTIS=11 VAL= 0x830
  35. * 172000 msec: WTCLK=11 WTIS=01 VAL= 0xC10
  36. * 687000 msec: WTCLK=11 WTIS=10 VAL= 0xC20
  37. * 2750000 msec: WTCLK=11 WTIS=11 VAL= 0xC30
  38. */
  39. struct npcm_wdt {
  40. struct watchdog_device wdd;
  41. void __iomem *reg;
  42. struct clk *clk;
  43. };
  44. static inline struct npcm_wdt *to_npcm_wdt(struct watchdog_device *wdd)
  45. {
  46. return container_of(wdd, struct npcm_wdt, wdd);
  47. }
  48. static int npcm_wdt_ping(struct watchdog_device *wdd)
  49. {
  50. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  51. u32 val;
  52. val = readl(wdt->reg);
  53. writel(val | NPCM_WTR, wdt->reg);
  54. return 0;
  55. }
  56. static int npcm_wdt_start(struct watchdog_device *wdd)
  57. {
  58. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  59. u32 val;
  60. if (wdt->clk)
  61. clk_prepare_enable(wdt->clk);
  62. if (wdd->timeout < 2)
  63. val = 0x800;
  64. else if (wdd->timeout < 3)
  65. val = 0x420;
  66. else if (wdd->timeout < 6)
  67. val = 0x810;
  68. else if (wdd->timeout < 11)
  69. val = 0x430;
  70. else if (wdd->timeout < 22)
  71. val = 0x820;
  72. else if (wdd->timeout < 44)
  73. val = 0xC00;
  74. else if (wdd->timeout < 87)
  75. val = 0x830;
  76. else if (wdd->timeout < 173)
  77. val = 0xC10;
  78. else if (wdd->timeout < 688)
  79. val = 0xC20;
  80. else
  81. val = 0xC30;
  82. val |= NPCM_WTRE | NPCM_WTE | NPCM_WTR | NPCM_WTIE;
  83. writel(val, wdt->reg);
  84. return 0;
  85. }
  86. static int npcm_wdt_stop(struct watchdog_device *wdd)
  87. {
  88. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  89. writel(0, wdt->reg);
  90. if (wdt->clk)
  91. clk_disable_unprepare(wdt->clk);
  92. return 0;
  93. }
  94. static int npcm_wdt_set_timeout(struct watchdog_device *wdd,
  95. unsigned int timeout)
  96. {
  97. if (timeout < 2)
  98. wdd->timeout = 1;
  99. else if (timeout < 3)
  100. wdd->timeout = 2;
  101. else if (timeout < 6)
  102. wdd->timeout = 5;
  103. else if (timeout < 11)
  104. wdd->timeout = 10;
  105. else if (timeout < 22)
  106. wdd->timeout = 21;
  107. else if (timeout < 44)
  108. wdd->timeout = 43;
  109. else if (timeout < 87)
  110. wdd->timeout = 86;
  111. else if (timeout < 173)
  112. wdd->timeout = 172;
  113. else if (timeout < 688)
  114. wdd->timeout = 687;
  115. else
  116. wdd->timeout = 2750;
  117. if (watchdog_active(wdd))
  118. npcm_wdt_start(wdd);
  119. return 0;
  120. }
  121. static irqreturn_t npcm_wdt_interrupt(int irq, void *data)
  122. {
  123. struct npcm_wdt *wdt = data;
  124. watchdog_notify_pretimeout(&wdt->wdd);
  125. return IRQ_HANDLED;
  126. }
  127. static int npcm_wdt_restart(struct watchdog_device *wdd,
  128. unsigned long action, void *data)
  129. {
  130. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  131. /* For reset, we start the WDT clock and leave it running. */
  132. if (wdt->clk)
  133. clk_prepare_enable(wdt->clk);
  134. writel(NPCM_WTR | NPCM_WTRE | NPCM_WTE, wdt->reg);
  135. udelay(1000);
  136. return 0;
  137. }
  138. static bool npcm_is_running(struct watchdog_device *wdd)
  139. {
  140. struct npcm_wdt *wdt = to_npcm_wdt(wdd);
  141. return readl(wdt->reg) & NPCM_WTE;
  142. }
  143. static const struct watchdog_info npcm_wdt_info = {
  144. .identity = KBUILD_MODNAME,
  145. .options = WDIOF_SETTIMEOUT
  146. | WDIOF_KEEPALIVEPING
  147. | WDIOF_MAGICCLOSE,
  148. };
  149. static const struct watchdog_ops npcm_wdt_ops = {
  150. .owner = THIS_MODULE,
  151. .start = npcm_wdt_start,
  152. .stop = npcm_wdt_stop,
  153. .ping = npcm_wdt_ping,
  154. .set_timeout = npcm_wdt_set_timeout,
  155. .restart = npcm_wdt_restart,
  156. };
  157. static int npcm_wdt_probe(struct platform_device *pdev)
  158. {
  159. struct device *dev = &pdev->dev;
  160. struct npcm_wdt *wdt;
  161. int irq;
  162. int ret;
  163. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  164. if (!wdt)
  165. return -ENOMEM;
  166. wdt->reg = devm_platform_ioremap_resource(pdev, 0);
  167. if (IS_ERR(wdt->reg))
  168. return PTR_ERR(wdt->reg);
  169. wdt->clk = devm_clk_get_optional(&pdev->dev, NULL);
  170. if (IS_ERR(wdt->clk))
  171. return PTR_ERR(wdt->clk);
  172. irq = platform_get_irq(pdev, 0);
  173. if (irq < 0)
  174. return irq;
  175. wdt->wdd.info = &npcm_wdt_info;
  176. wdt->wdd.ops = &npcm_wdt_ops;
  177. wdt->wdd.min_timeout = 1;
  178. wdt->wdd.max_timeout = 2750;
  179. wdt->wdd.parent = dev;
  180. wdt->wdd.timeout = 86;
  181. watchdog_init_timeout(&wdt->wdd, 0, dev);
  182. /* Ensure timeout is able to be represented by the hardware */
  183. npcm_wdt_set_timeout(&wdt->wdd, wdt->wdd.timeout);
  184. if (npcm_is_running(&wdt->wdd)) {
  185. /* Restart with the default or device-tree specified timeout */
  186. npcm_wdt_start(&wdt->wdd);
  187. set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
  188. }
  189. ret = devm_request_irq(dev, irq, npcm_wdt_interrupt, 0, "watchdog",
  190. wdt);
  191. if (ret)
  192. return ret;
  193. ret = devm_watchdog_register_device(dev, &wdt->wdd);
  194. if (ret)
  195. return ret;
  196. dev_info(dev, "NPCM watchdog driver enabled\n");
  197. return 0;
  198. }
  199. #ifdef CONFIG_OF
  200. static const struct of_device_id npcm_wdt_match[] = {
  201. {.compatible = "nuvoton,wpcm450-wdt"},
  202. {.compatible = "nuvoton,npcm750-wdt"},
  203. {},
  204. };
  205. MODULE_DEVICE_TABLE(of, npcm_wdt_match);
  206. #endif
  207. static struct platform_driver npcm_wdt_driver = {
  208. .probe = npcm_wdt_probe,
  209. .driver = {
  210. .name = "npcm-wdt",
  211. .of_match_table = of_match_ptr(npcm_wdt_match),
  212. },
  213. };
  214. module_platform_driver(npcm_wdt_driver);
  215. MODULE_AUTHOR("Joel Stanley");
  216. MODULE_DESCRIPTION("Watchdog driver for NPCM");
  217. MODULE_LICENSE("GPL v2");