asm9260_wdt.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Watchdog driver for Alphascale ASM9260.
  3. *
  4. * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>
  5. *
  6. * Licensed under GPLv2 or later.
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/reset.h>
  17. #include <linux/watchdog.h>
  18. #define CLOCK_FREQ 1000000
  19. /* Watchdog Mode register */
  20. #define HW_WDMOD 0x00
  21. /* Wake interrupt. Set by HW, can't be cleared. */
  22. #define BM_MOD_WDINT BIT(3)
  23. /* This bit set if timeout reached. Cleared by SW. */
  24. #define BM_MOD_WDTOF BIT(2)
  25. /* HW Reset on timeout */
  26. #define BM_MOD_WDRESET BIT(1)
  27. /* WD enable */
  28. #define BM_MOD_WDEN BIT(0)
  29. /*
  30. * Watchdog Timer Constant register
  31. * Minimal value is 0xff, the meaning of this value
  32. * depends on used clock: T = WDCLK * (0xff + 1) * 4
  33. */
  34. #define HW_WDTC 0x04
  35. #define BM_WDTC_MAX(freq) (0x7fffffff / (freq))
  36. /* Watchdog Feed register */
  37. #define HW_WDFEED 0x08
  38. /* Watchdog Timer Value register */
  39. #define HW_WDTV 0x0c
  40. #define ASM9260_WDT_DEFAULT_TIMEOUT 30
  41. enum asm9260_wdt_mode {
  42. HW_RESET,
  43. SW_RESET,
  44. DEBUG,
  45. };
  46. struct asm9260_wdt_priv {
  47. struct device *dev;
  48. struct watchdog_device wdd;
  49. struct clk *clk;
  50. struct clk *clk_ahb;
  51. struct reset_control *rst;
  52. void __iomem *iobase;
  53. int irq;
  54. unsigned long wdt_freq;
  55. enum asm9260_wdt_mode mode;
  56. };
  57. static int asm9260_wdt_feed(struct watchdog_device *wdd)
  58. {
  59. struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
  60. iowrite32(0xaa, priv->iobase + HW_WDFEED);
  61. iowrite32(0x55, priv->iobase + HW_WDFEED);
  62. return 0;
  63. }
  64. static unsigned int asm9260_wdt_gettimeleft(struct watchdog_device *wdd)
  65. {
  66. struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
  67. u32 counter;
  68. counter = ioread32(priv->iobase + HW_WDTV);
  69. return counter / priv->wdt_freq;
  70. }
  71. static int asm9260_wdt_updatetimeout(struct watchdog_device *wdd)
  72. {
  73. struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
  74. u32 counter;
  75. counter = wdd->timeout * priv->wdt_freq;
  76. iowrite32(counter, priv->iobase + HW_WDTC);
  77. return 0;
  78. }
  79. static int asm9260_wdt_enable(struct watchdog_device *wdd)
  80. {
  81. struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
  82. u32 mode = 0;
  83. if (priv->mode == HW_RESET)
  84. mode = BM_MOD_WDRESET;
  85. iowrite32(BM_MOD_WDEN | mode, priv->iobase + HW_WDMOD);
  86. asm9260_wdt_updatetimeout(wdd);
  87. asm9260_wdt_feed(wdd);
  88. return 0;
  89. }
  90. static int asm9260_wdt_disable(struct watchdog_device *wdd)
  91. {
  92. struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
  93. /* The only way to disable WD is to reset it. */
  94. reset_control_assert(priv->rst);
  95. reset_control_deassert(priv->rst);
  96. return 0;
  97. }
  98. static int asm9260_wdt_settimeout(struct watchdog_device *wdd, unsigned int to)
  99. {
  100. wdd->timeout = to;
  101. asm9260_wdt_updatetimeout(wdd);
  102. return 0;
  103. }
  104. static void asm9260_wdt_sys_reset(struct asm9260_wdt_priv *priv)
  105. {
  106. /* init WD if it was not started */
  107. iowrite32(BM_MOD_WDEN | BM_MOD_WDRESET, priv->iobase + HW_WDMOD);
  108. iowrite32(0xff, priv->iobase + HW_WDTC);
  109. /* first pass correct sequence */
  110. asm9260_wdt_feed(&priv->wdd);
  111. /*
  112. * Then write wrong pattern to the feed to trigger reset
  113. * ASAP.
  114. */
  115. iowrite32(0xff, priv->iobase + HW_WDFEED);
  116. mdelay(1000);
  117. }
  118. static irqreturn_t asm9260_wdt_irq(int irq, void *devid)
  119. {
  120. struct asm9260_wdt_priv *priv = devid;
  121. u32 stat;
  122. stat = ioread32(priv->iobase + HW_WDMOD);
  123. if (!(stat & BM_MOD_WDINT))
  124. return IRQ_NONE;
  125. if (priv->mode == DEBUG) {
  126. dev_info(priv->dev, "Watchdog Timeout. Do nothing.\n");
  127. } else {
  128. dev_info(priv->dev, "Watchdog Timeout. Doing SW Reset.\n");
  129. asm9260_wdt_sys_reset(priv);
  130. }
  131. return IRQ_HANDLED;
  132. }
  133. static int asm9260_restart(struct watchdog_device *wdd, unsigned long action,
  134. void *data)
  135. {
  136. struct asm9260_wdt_priv *priv = watchdog_get_drvdata(wdd);
  137. asm9260_wdt_sys_reset(priv);
  138. return 0;
  139. }
  140. static const struct watchdog_info asm9260_wdt_ident = {
  141. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
  142. | WDIOF_MAGICCLOSE,
  143. .identity = "Alphascale asm9260 Watchdog",
  144. };
  145. static const struct watchdog_ops asm9260_wdt_ops = {
  146. .owner = THIS_MODULE,
  147. .start = asm9260_wdt_enable,
  148. .stop = asm9260_wdt_disable,
  149. .get_timeleft = asm9260_wdt_gettimeleft,
  150. .ping = asm9260_wdt_feed,
  151. .set_timeout = asm9260_wdt_settimeout,
  152. .restart = asm9260_restart,
  153. };
  154. static int asm9260_wdt_get_dt_clks(struct asm9260_wdt_priv *priv)
  155. {
  156. int err;
  157. unsigned long clk;
  158. priv->clk = devm_clk_get(priv->dev, "mod");
  159. if (IS_ERR(priv->clk)) {
  160. dev_err(priv->dev, "Failed to get \"mod\" clk\n");
  161. return PTR_ERR(priv->clk);
  162. }
  163. /* configure AHB clock */
  164. priv->clk_ahb = devm_clk_get(priv->dev, "ahb");
  165. if (IS_ERR(priv->clk_ahb)) {
  166. dev_err(priv->dev, "Failed to get \"ahb\" clk\n");
  167. return PTR_ERR(priv->clk_ahb);
  168. }
  169. err = clk_prepare_enable(priv->clk_ahb);
  170. if (err) {
  171. dev_err(priv->dev, "Failed to enable ahb_clk!\n");
  172. return err;
  173. }
  174. err = clk_set_rate(priv->clk, CLOCK_FREQ);
  175. if (err) {
  176. clk_disable_unprepare(priv->clk_ahb);
  177. dev_err(priv->dev, "Failed to set rate!\n");
  178. return err;
  179. }
  180. err = clk_prepare_enable(priv->clk);
  181. if (err) {
  182. clk_disable_unprepare(priv->clk_ahb);
  183. dev_err(priv->dev, "Failed to enable clk!\n");
  184. return err;
  185. }
  186. /* wdt has internal divider */
  187. clk = clk_get_rate(priv->clk);
  188. if (!clk) {
  189. clk_disable_unprepare(priv->clk);
  190. clk_disable_unprepare(priv->clk_ahb);
  191. dev_err(priv->dev, "Failed, clk is 0!\n");
  192. return -EINVAL;
  193. }
  194. priv->wdt_freq = clk / 2;
  195. return 0;
  196. }
  197. static void asm9260_wdt_get_dt_mode(struct asm9260_wdt_priv *priv)
  198. {
  199. const char *tmp;
  200. int ret;
  201. /* default mode */
  202. priv->mode = HW_RESET;
  203. ret = of_property_read_string(priv->dev->of_node,
  204. "alphascale,mode", &tmp);
  205. if (ret < 0)
  206. return;
  207. if (!strcmp(tmp, "hw"))
  208. priv->mode = HW_RESET;
  209. else if (!strcmp(tmp, "sw"))
  210. priv->mode = SW_RESET;
  211. else if (!strcmp(tmp, "debug"))
  212. priv->mode = DEBUG;
  213. else
  214. dev_warn(priv->dev, "unknown reset-type: %s. Using default \"hw\" mode.",
  215. tmp);
  216. }
  217. static int asm9260_wdt_probe(struct platform_device *pdev)
  218. {
  219. struct asm9260_wdt_priv *priv;
  220. struct watchdog_device *wdd;
  221. struct resource *res;
  222. int ret;
  223. const char * const mode_name[] = { "hw", "sw", "debug", };
  224. priv = devm_kzalloc(&pdev->dev, sizeof(struct asm9260_wdt_priv),
  225. GFP_KERNEL);
  226. if (!priv)
  227. return -ENOMEM;
  228. priv->dev = &pdev->dev;
  229. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  230. priv->iobase = devm_ioremap_resource(&pdev->dev, res);
  231. if (IS_ERR(priv->iobase))
  232. return PTR_ERR(priv->iobase);
  233. priv->rst = devm_reset_control_get_exclusive(&pdev->dev, "wdt_rst");
  234. if (IS_ERR(priv->rst))
  235. return PTR_ERR(priv->rst);
  236. ret = asm9260_wdt_get_dt_clks(priv);
  237. if (ret)
  238. return ret;
  239. wdd = &priv->wdd;
  240. wdd->info = &asm9260_wdt_ident;
  241. wdd->ops = &asm9260_wdt_ops;
  242. wdd->min_timeout = 1;
  243. wdd->max_timeout = BM_WDTC_MAX(priv->wdt_freq);
  244. wdd->parent = &pdev->dev;
  245. watchdog_set_drvdata(wdd, priv);
  246. /*
  247. * If 'timeout-sec' unspecified in devicetree, assume a 30 second
  248. * default, unless the max timeout is less than 30 seconds, then use
  249. * the max instead.
  250. */
  251. wdd->timeout = ASM9260_WDT_DEFAULT_TIMEOUT;
  252. watchdog_init_timeout(wdd, 0, &pdev->dev);
  253. asm9260_wdt_get_dt_mode(priv);
  254. if (priv->mode != HW_RESET)
  255. priv->irq = platform_get_irq(pdev, 0);
  256. if (priv->irq > 0) {
  257. /*
  258. * Not all supported platforms specify an interrupt for the
  259. * watchdog, so let's make it optional.
  260. */
  261. ret = devm_request_irq(&pdev->dev, priv->irq,
  262. asm9260_wdt_irq, 0, pdev->name, priv);
  263. if (ret < 0)
  264. dev_warn(&pdev->dev, "failed to request IRQ\n");
  265. }
  266. watchdog_set_restart_priority(wdd, 128);
  267. ret = watchdog_register_device(wdd);
  268. if (ret)
  269. goto clk_off;
  270. platform_set_drvdata(pdev, priv);
  271. dev_info(&pdev->dev, "Watchdog enabled (timeout: %d sec, mode: %s)\n",
  272. wdd->timeout, mode_name[priv->mode]);
  273. return 0;
  274. clk_off:
  275. clk_disable_unprepare(priv->clk);
  276. clk_disable_unprepare(priv->clk_ahb);
  277. return ret;
  278. }
  279. static void asm9260_wdt_shutdown(struct platform_device *pdev)
  280. {
  281. struct asm9260_wdt_priv *priv = platform_get_drvdata(pdev);
  282. asm9260_wdt_disable(&priv->wdd);
  283. }
  284. static int asm9260_wdt_remove(struct platform_device *pdev)
  285. {
  286. struct asm9260_wdt_priv *priv = platform_get_drvdata(pdev);
  287. asm9260_wdt_disable(&priv->wdd);
  288. watchdog_unregister_device(&priv->wdd);
  289. clk_disable_unprepare(priv->clk);
  290. clk_disable_unprepare(priv->clk_ahb);
  291. return 0;
  292. }
  293. static const struct of_device_id asm9260_wdt_of_match[] = {
  294. { .compatible = "alphascale,asm9260-wdt"},
  295. {},
  296. };
  297. MODULE_DEVICE_TABLE(of, asm9260_wdt_of_match);
  298. static struct platform_driver asm9260_wdt_driver = {
  299. .driver = {
  300. .name = "asm9260-wdt",
  301. .of_match_table = asm9260_wdt_of_match,
  302. },
  303. .probe = asm9260_wdt_probe,
  304. .remove = asm9260_wdt_remove,
  305. .shutdown = asm9260_wdt_shutdown,
  306. };
  307. module_platform_driver(asm9260_wdt_driver);
  308. MODULE_DESCRIPTION("asm9260 WatchDog Timer Driver");
  309. MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>");
  310. MODULE_LICENSE("GPL");