imgpdc_wdt.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Imagination Technologies PowerDown Controller Watchdog Timer.
  3. *
  4. * Copyright (c) 2014 Imagination Technologies Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * Based on drivers/watchdog/sunxi_wdt.c Copyright (c) 2013 Carlo Caione
  11. * 2012 Henrik Nordstrom
  12. *
  13. * Notes
  14. * -----
  15. * The timeout value is rounded to the next power of two clock cycles.
  16. * This is configured using the PDC_WDT_CONFIG register, according to this
  17. * formula:
  18. *
  19. * timeout = 2^(delay + 1) clock cycles
  20. *
  21. * Where 'delay' is the value written in PDC_WDT_CONFIG register.
  22. *
  23. * Therefore, the hardware only allows to program watchdog timeouts, expressed
  24. * as a power of two number of watchdog clock cycles. The current implementation
  25. * guarantees that the actual watchdog timeout will be _at least_ the value
  26. * programmed in the imgpdg_wdt driver.
  27. *
  28. * The following table shows how the user-configured timeout relates
  29. * to the actual hardware timeout (watchdog clock @ 40000 Hz):
  30. *
  31. * input timeout | WD_DELAY | actual timeout
  32. * -----------------------------------
  33. * 10 | 18 | 13 seconds
  34. * 20 | 19 | 26 seconds
  35. * 30 | 20 | 52 seconds
  36. * 60 | 21 | 104 seconds
  37. *
  38. * Albeit coarse, this granularity would suffice most watchdog uses.
  39. * If the platform allows it, the user should be able to change the watchdog
  40. * clock rate and achieve a finer timeout granularity.
  41. */
  42. #include <linux/clk.h>
  43. #include <linux/io.h>
  44. #include <linux/log2.h>
  45. #include <linux/module.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/reboot.h>
  48. #include <linux/slab.h>
  49. #include <linux/watchdog.h>
  50. /* registers */
  51. #define PDC_WDT_SOFT_RESET 0x00
  52. #define PDC_WDT_CONFIG 0x04
  53. #define PDC_WDT_CONFIG_ENABLE BIT(31)
  54. #define PDC_WDT_CONFIG_DELAY_MASK 0x1f
  55. #define PDC_WDT_TICKLE1 0x08
  56. #define PDC_WDT_TICKLE1_MAGIC 0xabcd1234
  57. #define PDC_WDT_TICKLE2 0x0c
  58. #define PDC_WDT_TICKLE2_MAGIC 0x4321dcba
  59. #define PDC_WDT_TICKLE_STATUS_MASK 0x7
  60. #define PDC_WDT_TICKLE_STATUS_SHIFT 0
  61. #define PDC_WDT_TICKLE_STATUS_HRESET 0x0 /* Hard reset */
  62. #define PDC_WDT_TICKLE_STATUS_TIMEOUT 0x1 /* Timeout */
  63. #define PDC_WDT_TICKLE_STATUS_TICKLE 0x2 /* Tickled incorrectly */
  64. #define PDC_WDT_TICKLE_STATUS_SRESET 0x3 /* Soft reset */
  65. #define PDC_WDT_TICKLE_STATUS_USER 0x4 /* User reset */
  66. /* Timeout values are in seconds */
  67. #define PDC_WDT_MIN_TIMEOUT 1
  68. #define PDC_WDT_DEF_TIMEOUT 64
  69. static int heartbeat;
  70. module_param(heartbeat, int, 0);
  71. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds "
  72. "(default=" __MODULE_STRING(PDC_WDT_DEF_TIMEOUT) ")");
  73. static bool nowayout = WATCHDOG_NOWAYOUT;
  74. module_param(nowayout, bool, 0);
  75. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
  76. "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  77. struct pdc_wdt_dev {
  78. struct watchdog_device wdt_dev;
  79. struct clk *wdt_clk;
  80. struct clk *sys_clk;
  81. void __iomem *base;
  82. struct notifier_block restart_handler;
  83. };
  84. static int pdc_wdt_keepalive(struct watchdog_device *wdt_dev)
  85. {
  86. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  87. writel(PDC_WDT_TICKLE1_MAGIC, wdt->base + PDC_WDT_TICKLE1);
  88. writel(PDC_WDT_TICKLE2_MAGIC, wdt->base + PDC_WDT_TICKLE2);
  89. return 0;
  90. }
  91. static int pdc_wdt_stop(struct watchdog_device *wdt_dev)
  92. {
  93. unsigned int val;
  94. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  95. val = readl(wdt->base + PDC_WDT_CONFIG);
  96. val &= ~PDC_WDT_CONFIG_ENABLE;
  97. writel(val, wdt->base + PDC_WDT_CONFIG);
  98. /* Must tickle to finish the stop */
  99. pdc_wdt_keepalive(wdt_dev);
  100. return 0;
  101. }
  102. static void __pdc_wdt_set_timeout(struct pdc_wdt_dev *wdt)
  103. {
  104. unsigned long clk_rate = clk_get_rate(wdt->wdt_clk);
  105. unsigned int val;
  106. val = readl(wdt->base + PDC_WDT_CONFIG) & ~PDC_WDT_CONFIG_DELAY_MASK;
  107. val |= order_base_2(wdt->wdt_dev.timeout * clk_rate) - 1;
  108. writel(val, wdt->base + PDC_WDT_CONFIG);
  109. }
  110. static int pdc_wdt_set_timeout(struct watchdog_device *wdt_dev,
  111. unsigned int new_timeout)
  112. {
  113. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  114. wdt->wdt_dev.timeout = new_timeout;
  115. __pdc_wdt_set_timeout(wdt);
  116. return 0;
  117. }
  118. /* Start the watchdog timer (delay should already be set) */
  119. static int pdc_wdt_start(struct watchdog_device *wdt_dev)
  120. {
  121. unsigned int val;
  122. struct pdc_wdt_dev *wdt = watchdog_get_drvdata(wdt_dev);
  123. __pdc_wdt_set_timeout(wdt);
  124. val = readl(wdt->base + PDC_WDT_CONFIG);
  125. val |= PDC_WDT_CONFIG_ENABLE;
  126. writel(val, wdt->base + PDC_WDT_CONFIG);
  127. return 0;
  128. }
  129. static struct watchdog_info pdc_wdt_info = {
  130. .identity = "IMG PDC Watchdog",
  131. .options = WDIOF_SETTIMEOUT |
  132. WDIOF_KEEPALIVEPING |
  133. WDIOF_MAGICCLOSE,
  134. };
  135. static const struct watchdog_ops pdc_wdt_ops = {
  136. .owner = THIS_MODULE,
  137. .start = pdc_wdt_start,
  138. .stop = pdc_wdt_stop,
  139. .ping = pdc_wdt_keepalive,
  140. .set_timeout = pdc_wdt_set_timeout,
  141. };
  142. static int pdc_wdt_restart(struct notifier_block *this, unsigned long mode,
  143. void *cmd)
  144. {
  145. struct pdc_wdt_dev *wdt = container_of(this, struct pdc_wdt_dev,
  146. restart_handler);
  147. /* Assert SOFT_RESET */
  148. writel(0x1, wdt->base + PDC_WDT_SOFT_RESET);
  149. return NOTIFY_OK;
  150. }
  151. static int pdc_wdt_probe(struct platform_device *pdev)
  152. {
  153. u64 div;
  154. int ret, val;
  155. unsigned long clk_rate;
  156. struct resource *res;
  157. struct pdc_wdt_dev *pdc_wdt;
  158. pdc_wdt = devm_kzalloc(&pdev->dev, sizeof(*pdc_wdt), GFP_KERNEL);
  159. if (!pdc_wdt)
  160. return -ENOMEM;
  161. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. pdc_wdt->base = devm_ioremap_resource(&pdev->dev, res);
  163. if (IS_ERR(pdc_wdt->base))
  164. return PTR_ERR(pdc_wdt->base);
  165. pdc_wdt->sys_clk = devm_clk_get(&pdev->dev, "sys");
  166. if (IS_ERR(pdc_wdt->sys_clk)) {
  167. dev_err(&pdev->dev, "failed to get the sys clock\n");
  168. return PTR_ERR(pdc_wdt->sys_clk);
  169. }
  170. pdc_wdt->wdt_clk = devm_clk_get(&pdev->dev, "wdt");
  171. if (IS_ERR(pdc_wdt->wdt_clk)) {
  172. dev_err(&pdev->dev, "failed to get the wdt clock\n");
  173. return PTR_ERR(pdc_wdt->wdt_clk);
  174. }
  175. ret = clk_prepare_enable(pdc_wdt->sys_clk);
  176. if (ret) {
  177. dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
  178. return ret;
  179. }
  180. ret = clk_prepare_enable(pdc_wdt->wdt_clk);
  181. if (ret) {
  182. dev_err(&pdev->dev, "could not prepare or enable wdt clock\n");
  183. goto disable_sys_clk;
  184. }
  185. /* We use the clock rate to calculate the max timeout */
  186. clk_rate = clk_get_rate(pdc_wdt->wdt_clk);
  187. if (clk_rate == 0) {
  188. dev_err(&pdev->dev, "failed to get clock rate\n");
  189. ret = -EINVAL;
  190. goto disable_wdt_clk;
  191. }
  192. if (order_base_2(clk_rate) > PDC_WDT_CONFIG_DELAY_MASK + 1) {
  193. dev_err(&pdev->dev, "invalid clock rate\n");
  194. ret = -EINVAL;
  195. goto disable_wdt_clk;
  196. }
  197. if (order_base_2(clk_rate) == 0)
  198. pdc_wdt->wdt_dev.min_timeout = PDC_WDT_MIN_TIMEOUT + 1;
  199. else
  200. pdc_wdt->wdt_dev.min_timeout = PDC_WDT_MIN_TIMEOUT;
  201. pdc_wdt->wdt_dev.info = &pdc_wdt_info;
  202. pdc_wdt->wdt_dev.ops = &pdc_wdt_ops;
  203. div = 1ULL << (PDC_WDT_CONFIG_DELAY_MASK + 1);
  204. do_div(div, clk_rate);
  205. pdc_wdt->wdt_dev.max_timeout = div;
  206. pdc_wdt->wdt_dev.timeout = PDC_WDT_DEF_TIMEOUT;
  207. pdc_wdt->wdt_dev.parent = &pdev->dev;
  208. watchdog_set_drvdata(&pdc_wdt->wdt_dev, pdc_wdt);
  209. watchdog_init_timeout(&pdc_wdt->wdt_dev, heartbeat, &pdev->dev);
  210. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  211. /* Find what caused the last reset */
  212. val = readl(pdc_wdt->base + PDC_WDT_TICKLE1);
  213. val = (val & PDC_WDT_TICKLE_STATUS_MASK) >> PDC_WDT_TICKLE_STATUS_SHIFT;
  214. switch (val) {
  215. case PDC_WDT_TICKLE_STATUS_TICKLE:
  216. case PDC_WDT_TICKLE_STATUS_TIMEOUT:
  217. pdc_wdt->wdt_dev.bootstatus |= WDIOF_CARDRESET;
  218. dev_info(&pdev->dev,
  219. "watchdog module last reset due to timeout\n");
  220. break;
  221. case PDC_WDT_TICKLE_STATUS_HRESET:
  222. dev_info(&pdev->dev,
  223. "watchdog module last reset due to hard reset\n");
  224. break;
  225. case PDC_WDT_TICKLE_STATUS_SRESET:
  226. dev_info(&pdev->dev,
  227. "watchdog module last reset due to soft reset\n");
  228. break;
  229. case PDC_WDT_TICKLE_STATUS_USER:
  230. dev_info(&pdev->dev,
  231. "watchdog module last reset due to user reset\n");
  232. break;
  233. default:
  234. dev_info(&pdev->dev,
  235. "contains an illegal status code (%08x)\n", val);
  236. break;
  237. }
  238. watchdog_set_nowayout(&pdc_wdt->wdt_dev, nowayout);
  239. platform_set_drvdata(pdev, pdc_wdt);
  240. ret = watchdog_register_device(&pdc_wdt->wdt_dev);
  241. if (ret)
  242. goto disable_wdt_clk;
  243. pdc_wdt->restart_handler.notifier_call = pdc_wdt_restart;
  244. pdc_wdt->restart_handler.priority = 128;
  245. ret = register_restart_handler(&pdc_wdt->restart_handler);
  246. if (ret)
  247. dev_warn(&pdev->dev, "failed to register restart handler: %d\n",
  248. ret);
  249. return 0;
  250. disable_wdt_clk:
  251. clk_disable_unprepare(pdc_wdt->wdt_clk);
  252. disable_sys_clk:
  253. clk_disable_unprepare(pdc_wdt->sys_clk);
  254. return ret;
  255. }
  256. static void pdc_wdt_shutdown(struct platform_device *pdev)
  257. {
  258. struct pdc_wdt_dev *pdc_wdt = platform_get_drvdata(pdev);
  259. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  260. }
  261. static int pdc_wdt_remove(struct platform_device *pdev)
  262. {
  263. struct pdc_wdt_dev *pdc_wdt = platform_get_drvdata(pdev);
  264. unregister_restart_handler(&pdc_wdt->restart_handler);
  265. pdc_wdt_stop(&pdc_wdt->wdt_dev);
  266. watchdog_unregister_device(&pdc_wdt->wdt_dev);
  267. clk_disable_unprepare(pdc_wdt->wdt_clk);
  268. clk_disable_unprepare(pdc_wdt->sys_clk);
  269. return 0;
  270. }
  271. static const struct of_device_id pdc_wdt_match[] = {
  272. { .compatible = "img,pdc-wdt" },
  273. {}
  274. };
  275. MODULE_DEVICE_TABLE(of, pdc_wdt_match);
  276. static struct platform_driver pdc_wdt_driver = {
  277. .driver = {
  278. .name = "imgpdc-wdt",
  279. .of_match_table = pdc_wdt_match,
  280. },
  281. .probe = pdc_wdt_probe,
  282. .remove = pdc_wdt_remove,
  283. .shutdown = pdc_wdt_shutdown,
  284. };
  285. module_platform_driver(pdc_wdt_driver);
  286. MODULE_AUTHOR("Jude Abraham <Jude.Abraham@imgtec.com>");
  287. MODULE_AUTHOR("Naidu Tellapati <Naidu.Tellapati@imgtec.com>");
  288. MODULE_DESCRIPTION("Imagination Technologies PDC Watchdog Timer Driver");
  289. MODULE_LICENSE("GPL v2");