pic32-wdt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * PIC32 watchdog driver
  3. *
  4. * Joshua Henderson <joshua.henderson@microchip.com>
  5. * Copyright (c) 2016, Microchip Technology Inc.
  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. #include <linux/clk.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm.h>
  22. #include <linux/watchdog.h>
  23. #include <asm/mach-pic32/pic32.h>
  24. /* Watchdog Timer Registers */
  25. #define WDTCON_REG 0x00
  26. /* Watchdog Timer Control Register fields */
  27. #define WDTCON_WIN_EN BIT(0)
  28. #define WDTCON_RMCS_MASK 0x0003
  29. #define WDTCON_RMCS_SHIFT 0x0006
  30. #define WDTCON_RMPS_MASK 0x001F
  31. #define WDTCON_RMPS_SHIFT 0x0008
  32. #define WDTCON_ON BIT(15)
  33. #define WDTCON_CLR_KEY 0x5743
  34. /* Reset Control Register fields for watchdog */
  35. #define RESETCON_TIMEOUT_IDLE BIT(2)
  36. #define RESETCON_TIMEOUT_SLEEP BIT(3)
  37. #define RESETCON_WDT_TIMEOUT BIT(4)
  38. struct pic32_wdt {
  39. void __iomem *regs;
  40. void __iomem *rst_base;
  41. struct clk *clk;
  42. };
  43. static inline bool pic32_wdt_is_win_enabled(struct pic32_wdt *wdt)
  44. {
  45. return !!(readl(wdt->regs + WDTCON_REG) & WDTCON_WIN_EN);
  46. }
  47. static inline u32 pic32_wdt_get_post_scaler(struct pic32_wdt *wdt)
  48. {
  49. u32 v = readl(wdt->regs + WDTCON_REG);
  50. return (v >> WDTCON_RMPS_SHIFT) & WDTCON_RMPS_MASK;
  51. }
  52. static inline u32 pic32_wdt_get_clk_id(struct pic32_wdt *wdt)
  53. {
  54. u32 v = readl(wdt->regs + WDTCON_REG);
  55. return (v >> WDTCON_RMCS_SHIFT) & WDTCON_RMCS_MASK;
  56. }
  57. static int pic32_wdt_bootstatus(struct pic32_wdt *wdt)
  58. {
  59. u32 v = readl(wdt->rst_base);
  60. writel(RESETCON_WDT_TIMEOUT, PIC32_CLR(wdt->rst_base));
  61. return v & RESETCON_WDT_TIMEOUT;
  62. }
  63. static u32 pic32_wdt_get_timeout_secs(struct pic32_wdt *wdt, struct device *dev)
  64. {
  65. unsigned long rate;
  66. u32 period, ps, terminal;
  67. rate = clk_get_rate(wdt->clk);
  68. dev_dbg(dev, "wdt: clk_id %d, clk_rate %lu (prescale)\n",
  69. pic32_wdt_get_clk_id(wdt), rate);
  70. /* default, prescaler of 32 (i.e. div-by-32) is implicit. */
  71. rate >>= 5;
  72. if (!rate)
  73. return 0;
  74. /* calculate terminal count from postscaler. */
  75. ps = pic32_wdt_get_post_scaler(wdt);
  76. terminal = BIT(ps);
  77. /* find time taken (in secs) to reach terminal count */
  78. period = terminal / rate;
  79. dev_dbg(dev,
  80. "wdt: clk_rate %lu (postscale) / terminal %d, timeout %dsec\n",
  81. rate, terminal, period);
  82. return period;
  83. }
  84. static void pic32_wdt_keepalive(struct pic32_wdt *wdt)
  85. {
  86. /* write key through single half-word */
  87. writew(WDTCON_CLR_KEY, wdt->regs + WDTCON_REG + 2);
  88. }
  89. static int pic32_wdt_start(struct watchdog_device *wdd)
  90. {
  91. struct pic32_wdt *wdt = watchdog_get_drvdata(wdd);
  92. writel(WDTCON_ON, PIC32_SET(wdt->regs + WDTCON_REG));
  93. pic32_wdt_keepalive(wdt);
  94. return 0;
  95. }
  96. static int pic32_wdt_stop(struct watchdog_device *wdd)
  97. {
  98. struct pic32_wdt *wdt = watchdog_get_drvdata(wdd);
  99. writel(WDTCON_ON, PIC32_CLR(wdt->regs + WDTCON_REG));
  100. /*
  101. * Cannot touch registers in the CPU cycle following clearing the
  102. * ON bit.
  103. */
  104. nop();
  105. return 0;
  106. }
  107. static int pic32_wdt_ping(struct watchdog_device *wdd)
  108. {
  109. struct pic32_wdt *wdt = watchdog_get_drvdata(wdd);
  110. pic32_wdt_keepalive(wdt);
  111. return 0;
  112. }
  113. static const struct watchdog_ops pic32_wdt_fops = {
  114. .owner = THIS_MODULE,
  115. .start = pic32_wdt_start,
  116. .stop = pic32_wdt_stop,
  117. .ping = pic32_wdt_ping,
  118. };
  119. static const struct watchdog_info pic32_wdt_ident = {
  120. .options = WDIOF_KEEPALIVEPING |
  121. WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
  122. .identity = "PIC32 Watchdog",
  123. };
  124. static struct watchdog_device pic32_wdd = {
  125. .info = &pic32_wdt_ident,
  126. .ops = &pic32_wdt_fops,
  127. };
  128. static const struct of_device_id pic32_wdt_dt_ids[] = {
  129. { .compatible = "microchip,pic32mzda-wdt", },
  130. { /* sentinel */ }
  131. };
  132. MODULE_DEVICE_TABLE(of, pic32_wdt_dt_ids);
  133. static int pic32_wdt_drv_probe(struct platform_device *pdev)
  134. {
  135. int ret;
  136. struct watchdog_device *wdd = &pic32_wdd;
  137. struct pic32_wdt *wdt;
  138. struct resource *mem;
  139. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  140. if (!wdt)
  141. return -ENOMEM;
  142. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  143. wdt->regs = devm_ioremap_resource(&pdev->dev, mem);
  144. if (IS_ERR(wdt->regs))
  145. return PTR_ERR(wdt->regs);
  146. wdt->rst_base = devm_ioremap(&pdev->dev, PIC32_BASE_RESET, 0x10);
  147. if (!wdt->rst_base)
  148. return -ENOMEM;
  149. wdt->clk = devm_clk_get(&pdev->dev, NULL);
  150. if (IS_ERR(wdt->clk)) {
  151. dev_err(&pdev->dev, "clk not found\n");
  152. return PTR_ERR(wdt->clk);
  153. }
  154. ret = clk_prepare_enable(wdt->clk);
  155. if (ret) {
  156. dev_err(&pdev->dev, "clk enable failed\n");
  157. return ret;
  158. }
  159. if (pic32_wdt_is_win_enabled(wdt)) {
  160. dev_err(&pdev->dev, "windowed-clear mode is not supported.\n");
  161. ret = -ENODEV;
  162. goto out_disable_clk;
  163. }
  164. wdd->timeout = pic32_wdt_get_timeout_secs(wdt, &pdev->dev);
  165. if (!wdd->timeout) {
  166. dev_err(&pdev->dev,
  167. "failed to read watchdog register timeout\n");
  168. ret = -EINVAL;
  169. goto out_disable_clk;
  170. }
  171. dev_info(&pdev->dev, "timeout %d\n", wdd->timeout);
  172. wdd->bootstatus = pic32_wdt_bootstatus(wdt) ? WDIOF_CARDRESET : 0;
  173. watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
  174. watchdog_set_drvdata(wdd, wdt);
  175. ret = watchdog_register_device(wdd);
  176. if (ret) {
  177. dev_err(&pdev->dev, "watchdog register failed, err %d\n", ret);
  178. goto out_disable_clk;
  179. }
  180. platform_set_drvdata(pdev, wdd);
  181. return 0;
  182. out_disable_clk:
  183. clk_disable_unprepare(wdt->clk);
  184. return ret;
  185. }
  186. static int pic32_wdt_drv_remove(struct platform_device *pdev)
  187. {
  188. struct watchdog_device *wdd = platform_get_drvdata(pdev);
  189. struct pic32_wdt *wdt = watchdog_get_drvdata(wdd);
  190. watchdog_unregister_device(wdd);
  191. clk_disable_unprepare(wdt->clk);
  192. return 0;
  193. }
  194. static struct platform_driver pic32_wdt_driver = {
  195. .probe = pic32_wdt_drv_probe,
  196. .remove = pic32_wdt_drv_remove,
  197. .driver = {
  198. .name = "pic32-wdt",
  199. .of_match_table = of_match_ptr(pic32_wdt_dt_ids),
  200. }
  201. };
  202. module_platform_driver(pic32_wdt_driver);
  203. MODULE_AUTHOR("Joshua Henderson <joshua.henderson@microchip.com>");
  204. MODULE_DESCRIPTION("Microchip PIC32 Watchdog Timer");
  205. MODULE_LICENSE("GPL");