pic32-dmt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PIC32 deadman timer driver
  4. *
  5. * Purna Chandra Mandal <purna.mandal@microchip.com>
  6. * Copyright (c) 2016, Microchip Technology Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_data/pic32.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm.h>
  18. #include <linux/watchdog.h>
  19. /* Deadman Timer Regs */
  20. #define DMTCON_REG 0x00
  21. #define DMTPRECLR_REG 0x10
  22. #define DMTCLR_REG 0x20
  23. #define DMTSTAT_REG 0x30
  24. #define DMTCNT_REG 0x40
  25. #define DMTPSCNT_REG 0x60
  26. #define DMTPSINTV_REG 0x70
  27. /* Deadman Timer Regs fields */
  28. #define DMT_ON BIT(15)
  29. #define DMT_STEP1_KEY BIT(6)
  30. #define DMT_STEP2_KEY BIT(3)
  31. #define DMTSTAT_WINOPN BIT(0)
  32. #define DMTSTAT_EVENT BIT(5)
  33. #define DMTSTAT_BAD2 BIT(6)
  34. #define DMTSTAT_BAD1 BIT(7)
  35. /* Reset Control Register fields for watchdog */
  36. #define RESETCON_DMT_TIMEOUT BIT(5)
  37. struct pic32_dmt {
  38. void __iomem *regs;
  39. struct clk *clk;
  40. };
  41. static inline void dmt_enable(struct pic32_dmt *dmt)
  42. {
  43. writel(DMT_ON, PIC32_SET(dmt->regs + DMTCON_REG));
  44. }
  45. static inline void dmt_disable(struct pic32_dmt *dmt)
  46. {
  47. writel(DMT_ON, PIC32_CLR(dmt->regs + DMTCON_REG));
  48. /*
  49. * Cannot touch registers in the CPU cycle following clearing the
  50. * ON bit.
  51. */
  52. nop();
  53. }
  54. static inline int dmt_bad_status(struct pic32_dmt *dmt)
  55. {
  56. u32 val;
  57. val = readl(dmt->regs + DMTSTAT_REG);
  58. val &= (DMTSTAT_BAD1 | DMTSTAT_BAD2 | DMTSTAT_EVENT);
  59. if (val)
  60. return -EAGAIN;
  61. return 0;
  62. }
  63. static inline int dmt_keepalive(struct pic32_dmt *dmt)
  64. {
  65. u32 v;
  66. u32 timeout = 500;
  67. /* set pre-clear key */
  68. writel(DMT_STEP1_KEY << 8, dmt->regs + DMTPRECLR_REG);
  69. /* wait for DMT window to open */
  70. while (--timeout) {
  71. v = readl(dmt->regs + DMTSTAT_REG) & DMTSTAT_WINOPN;
  72. if (v == DMTSTAT_WINOPN)
  73. break;
  74. }
  75. /* apply key2 */
  76. writel(DMT_STEP2_KEY, dmt->regs + DMTCLR_REG);
  77. /* check whether keys are latched correctly */
  78. return dmt_bad_status(dmt);
  79. }
  80. static inline u32 pic32_dmt_get_timeout_secs(struct pic32_dmt *dmt)
  81. {
  82. unsigned long rate;
  83. rate = clk_get_rate(dmt->clk);
  84. if (rate)
  85. return readl(dmt->regs + DMTPSCNT_REG) / rate;
  86. return 0;
  87. }
  88. static inline u32 pic32_dmt_bootstatus(struct pic32_dmt *dmt)
  89. {
  90. u32 v;
  91. void __iomem *rst_base;
  92. rst_base = ioremap(PIC32_BASE_RESET, 0x10);
  93. if (!rst_base)
  94. return 0;
  95. v = readl(rst_base);
  96. writel(RESETCON_DMT_TIMEOUT, PIC32_CLR(rst_base));
  97. iounmap(rst_base);
  98. return v & RESETCON_DMT_TIMEOUT;
  99. }
  100. static int pic32_dmt_start(struct watchdog_device *wdd)
  101. {
  102. struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
  103. dmt_enable(dmt);
  104. return dmt_keepalive(dmt);
  105. }
  106. static int pic32_dmt_stop(struct watchdog_device *wdd)
  107. {
  108. struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
  109. dmt_disable(dmt);
  110. return 0;
  111. }
  112. static int pic32_dmt_ping(struct watchdog_device *wdd)
  113. {
  114. struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
  115. return dmt_keepalive(dmt);
  116. }
  117. static const struct watchdog_ops pic32_dmt_fops = {
  118. .owner = THIS_MODULE,
  119. .start = pic32_dmt_start,
  120. .stop = pic32_dmt_stop,
  121. .ping = pic32_dmt_ping,
  122. };
  123. static const struct watchdog_info pic32_dmt_ident = {
  124. .options = WDIOF_KEEPALIVEPING |
  125. WDIOF_MAGICCLOSE,
  126. .identity = "PIC32 Deadman Timer",
  127. };
  128. static struct watchdog_device pic32_dmt_wdd = {
  129. .info = &pic32_dmt_ident,
  130. .ops = &pic32_dmt_fops,
  131. };
  132. static int pic32_dmt_probe(struct platform_device *pdev)
  133. {
  134. struct device *dev = &pdev->dev;
  135. int ret;
  136. struct pic32_dmt *dmt;
  137. struct watchdog_device *wdd = &pic32_dmt_wdd;
  138. dmt = devm_kzalloc(dev, sizeof(*dmt), GFP_KERNEL);
  139. if (!dmt)
  140. return -ENOMEM;
  141. dmt->regs = devm_platform_ioremap_resource(pdev, 0);
  142. if (IS_ERR(dmt->regs))
  143. return PTR_ERR(dmt->regs);
  144. dmt->clk = devm_clk_get_enabled(dev, NULL);
  145. if (IS_ERR(dmt->clk)) {
  146. dev_err(dev, "clk not found\n");
  147. return PTR_ERR(dmt->clk);
  148. }
  149. wdd->timeout = pic32_dmt_get_timeout_secs(dmt);
  150. if (!wdd->timeout) {
  151. dev_err(dev, "failed to read watchdog register timeout\n");
  152. return -EINVAL;
  153. }
  154. dev_info(dev, "timeout %d\n", wdd->timeout);
  155. wdd->bootstatus = pic32_dmt_bootstatus(dmt) ? WDIOF_CARDRESET : 0;
  156. watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
  157. watchdog_set_drvdata(wdd, dmt);
  158. ret = devm_watchdog_register_device(dev, wdd);
  159. if (ret)
  160. return ret;
  161. platform_set_drvdata(pdev, wdd);
  162. return 0;
  163. }
  164. static const struct of_device_id pic32_dmt_of_ids[] = {
  165. { .compatible = "microchip,pic32mzda-dmt",},
  166. { /* sentinel */ }
  167. };
  168. MODULE_DEVICE_TABLE(of, pic32_dmt_of_ids);
  169. static struct platform_driver pic32_dmt_driver = {
  170. .probe = pic32_dmt_probe,
  171. .driver = {
  172. .name = "pic32-dmt",
  173. .of_match_table = of_match_ptr(pic32_dmt_of_ids),
  174. }
  175. };
  176. module_platform_driver(pic32_dmt_driver);
  177. MODULE_AUTHOR("Purna Chandra Mandal <purna.mandal@microchip.com>");
  178. MODULE_DESCRIPTION("Microchip PIC32 DMT Driver");
  179. MODULE_LICENSE("GPL");