cadence_wdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Cadence WDT driver - Used by Xilinx Zynq
  3. *
  4. * Copyright (C) 2010 - 2014 Xilinx, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/watchdog.h>
  21. #define CDNS_WDT_DEFAULT_TIMEOUT 10
  22. /* Supports 1 - 516 sec */
  23. #define CDNS_WDT_MIN_TIMEOUT 1
  24. #define CDNS_WDT_MAX_TIMEOUT 516
  25. /* Restart key */
  26. #define CDNS_WDT_RESTART_KEY 0x00001999
  27. /* Counter register access key */
  28. #define CDNS_WDT_REGISTER_ACCESS_KEY 0x00920000
  29. /* Counter value divisor */
  30. #define CDNS_WDT_COUNTER_VALUE_DIVISOR 0x1000
  31. /* Clock prescaler value and selection */
  32. #define CDNS_WDT_PRESCALE_64 64
  33. #define CDNS_WDT_PRESCALE_512 512
  34. #define CDNS_WDT_PRESCALE_4096 4096
  35. #define CDNS_WDT_PRESCALE_SELECT_64 1
  36. #define CDNS_WDT_PRESCALE_SELECT_512 2
  37. #define CDNS_WDT_PRESCALE_SELECT_4096 3
  38. /* Input clock frequency */
  39. #define CDNS_WDT_CLK_10MHZ 10000000
  40. #define CDNS_WDT_CLK_75MHZ 75000000
  41. /* Counter maximum value */
  42. #define CDNS_WDT_COUNTER_MAX 0xFFF
  43. static int wdt_timeout = CDNS_WDT_DEFAULT_TIMEOUT;
  44. static int nowayout = WATCHDOG_NOWAYOUT;
  45. module_param(wdt_timeout, int, 0);
  46. MODULE_PARM_DESC(wdt_timeout,
  47. "Watchdog time in seconds. (default="
  48. __MODULE_STRING(CDNS_WDT_DEFAULT_TIMEOUT) ")");
  49. module_param(nowayout, int, 0);
  50. MODULE_PARM_DESC(nowayout,
  51. "Watchdog cannot be stopped once started (default="
  52. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  53. /**
  54. * struct cdns_wdt - Watchdog device structure
  55. * @regs: baseaddress of device
  56. * @rst: reset flag
  57. * @clk: struct clk * of a clock source
  58. * @prescaler: for saving prescaler value
  59. * @ctrl_clksel: counter clock prescaler selection
  60. * @io_lock: spinlock for IO register access
  61. * @cdns_wdt_device: watchdog device structure
  62. *
  63. * Structure containing parameters specific to cadence watchdog.
  64. */
  65. struct cdns_wdt {
  66. void __iomem *regs;
  67. bool rst;
  68. struct clk *clk;
  69. u32 prescaler;
  70. u32 ctrl_clksel;
  71. spinlock_t io_lock;
  72. struct watchdog_device cdns_wdt_device;
  73. };
  74. /* Write access to Registers */
  75. static inline void cdns_wdt_writereg(struct cdns_wdt *wdt, u32 offset, u32 val)
  76. {
  77. writel_relaxed(val, wdt->regs + offset);
  78. }
  79. /*************************Register Map**************************************/
  80. /* Register Offsets for the WDT */
  81. #define CDNS_WDT_ZMR_OFFSET 0x0 /* Zero Mode Register */
  82. #define CDNS_WDT_CCR_OFFSET 0x4 /* Counter Control Register */
  83. #define CDNS_WDT_RESTART_OFFSET 0x8 /* Restart Register */
  84. #define CDNS_WDT_SR_OFFSET 0xC /* Status Register */
  85. /*
  86. * Zero Mode Register - This register controls how the time out is indicated
  87. * and also contains the access code to allow writes to the register (0xABC).
  88. */
  89. #define CDNS_WDT_ZMR_WDEN_MASK 0x00000001 /* Enable the WDT */
  90. #define CDNS_WDT_ZMR_RSTEN_MASK 0x00000002 /* Enable the reset output */
  91. #define CDNS_WDT_ZMR_IRQEN_MASK 0x00000004 /* Enable IRQ output */
  92. #define CDNS_WDT_ZMR_RSTLEN_16 0x00000030 /* Reset pulse of 16 pclk cycles */
  93. #define CDNS_WDT_ZMR_ZKEY_VAL 0x00ABC000 /* Access key, 0xABC << 12 */
  94. /*
  95. * Counter Control register - This register controls how fast the timer runs
  96. * and the reset value and also contains the access code to allow writes to
  97. * the register.
  98. */
  99. #define CDNS_WDT_CCR_CRV_MASK 0x00003FFC /* Counter reset value */
  100. /**
  101. * cdns_wdt_stop - Stop the watchdog.
  102. *
  103. * @wdd: watchdog device
  104. *
  105. * Read the contents of the ZMR register, clear the WDEN bit
  106. * in the register and set the access key for successful write.
  107. *
  108. * Return: always 0
  109. */
  110. static int cdns_wdt_stop(struct watchdog_device *wdd)
  111. {
  112. struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
  113. spin_lock(&wdt->io_lock);
  114. cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET,
  115. CDNS_WDT_ZMR_ZKEY_VAL & (~CDNS_WDT_ZMR_WDEN_MASK));
  116. spin_unlock(&wdt->io_lock);
  117. return 0;
  118. }
  119. /**
  120. * cdns_wdt_reload - Reload the watchdog timer (i.e. pat the watchdog).
  121. *
  122. * @wdd: watchdog device
  123. *
  124. * Write the restart key value (0x00001999) to the restart register.
  125. *
  126. * Return: always 0
  127. */
  128. static int cdns_wdt_reload(struct watchdog_device *wdd)
  129. {
  130. struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
  131. spin_lock(&wdt->io_lock);
  132. cdns_wdt_writereg(wdt, CDNS_WDT_RESTART_OFFSET,
  133. CDNS_WDT_RESTART_KEY);
  134. spin_unlock(&wdt->io_lock);
  135. return 0;
  136. }
  137. /**
  138. * cdns_wdt_start - Enable and start the watchdog.
  139. *
  140. * @wdd: watchdog device
  141. *
  142. * The counter value is calculated according to the formula:
  143. * calculated count = (timeout * clock) / prescaler + 1.
  144. * The calculated count is divided by 0x1000 to obtain the field value
  145. * to write to counter control register.
  146. * Clears the contents of prescaler and counter reset value. Sets the
  147. * prescaler to 4096 and the calculated count and access key
  148. * to write to CCR Register.
  149. * Sets the WDT (WDEN bit) and either the Reset signal(RSTEN bit)
  150. * or Interrupt signal(IRQEN) with a specified cycles and the access
  151. * key to write to ZMR Register.
  152. *
  153. * Return: always 0
  154. */
  155. static int cdns_wdt_start(struct watchdog_device *wdd)
  156. {
  157. struct cdns_wdt *wdt = watchdog_get_drvdata(wdd);
  158. unsigned int data = 0;
  159. unsigned short count;
  160. unsigned long clock_f = clk_get_rate(wdt->clk);
  161. /*
  162. * Counter value divisor to obtain the value of
  163. * counter reset to be written to control register.
  164. */
  165. count = (wdd->timeout * (clock_f / wdt->prescaler)) /
  166. CDNS_WDT_COUNTER_VALUE_DIVISOR + 1;
  167. if (count > CDNS_WDT_COUNTER_MAX)
  168. count = CDNS_WDT_COUNTER_MAX;
  169. spin_lock(&wdt->io_lock);
  170. cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET,
  171. CDNS_WDT_ZMR_ZKEY_VAL);
  172. count = (count << 2) & CDNS_WDT_CCR_CRV_MASK;
  173. /* Write counter access key first to be able write to register */
  174. data = count | CDNS_WDT_REGISTER_ACCESS_KEY | wdt->ctrl_clksel;
  175. cdns_wdt_writereg(wdt, CDNS_WDT_CCR_OFFSET, data);
  176. data = CDNS_WDT_ZMR_WDEN_MASK | CDNS_WDT_ZMR_RSTLEN_16 |
  177. CDNS_WDT_ZMR_ZKEY_VAL;
  178. /* Reset on timeout if specified in device tree. */
  179. if (wdt->rst) {
  180. data |= CDNS_WDT_ZMR_RSTEN_MASK;
  181. data &= ~CDNS_WDT_ZMR_IRQEN_MASK;
  182. } else {
  183. data &= ~CDNS_WDT_ZMR_RSTEN_MASK;
  184. data |= CDNS_WDT_ZMR_IRQEN_MASK;
  185. }
  186. cdns_wdt_writereg(wdt, CDNS_WDT_ZMR_OFFSET, data);
  187. cdns_wdt_writereg(wdt, CDNS_WDT_RESTART_OFFSET,
  188. CDNS_WDT_RESTART_KEY);
  189. spin_unlock(&wdt->io_lock);
  190. return 0;
  191. }
  192. /**
  193. * cdns_wdt_settimeout - Set a new timeout value for the watchdog device.
  194. *
  195. * @wdd: watchdog device
  196. * @new_time: new timeout value that needs to be set
  197. * Return: 0 on success
  198. *
  199. * Update the watchdog_device timeout with new value which is used when
  200. * cdns_wdt_start is called.
  201. */
  202. static int cdns_wdt_settimeout(struct watchdog_device *wdd,
  203. unsigned int new_time)
  204. {
  205. wdd->timeout = new_time;
  206. return cdns_wdt_start(wdd);
  207. }
  208. /**
  209. * cdns_wdt_irq_handler - Notifies of watchdog timeout.
  210. *
  211. * @irq: interrupt number
  212. * @dev_id: pointer to a platform device structure
  213. * Return: IRQ_HANDLED
  214. *
  215. * The handler is invoked when the watchdog times out and a
  216. * reset on timeout has not been enabled.
  217. */
  218. static irqreturn_t cdns_wdt_irq_handler(int irq, void *dev_id)
  219. {
  220. struct platform_device *pdev = dev_id;
  221. dev_info(&pdev->dev,
  222. "Watchdog timed out. Internal reset not enabled\n");
  223. return IRQ_HANDLED;
  224. }
  225. /*
  226. * Info structure used to indicate the features supported by the device
  227. * to the upper layers. This is defined in watchdog.h header file.
  228. */
  229. static struct watchdog_info cdns_wdt_info = {
  230. .identity = "cdns_wdt watchdog",
  231. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  232. WDIOF_MAGICCLOSE,
  233. };
  234. /* Watchdog Core Ops */
  235. static const struct watchdog_ops cdns_wdt_ops = {
  236. .owner = THIS_MODULE,
  237. .start = cdns_wdt_start,
  238. .stop = cdns_wdt_stop,
  239. .ping = cdns_wdt_reload,
  240. .set_timeout = cdns_wdt_settimeout,
  241. };
  242. /************************Platform Operations*****************************/
  243. /**
  244. * cdns_wdt_probe - Probe call for the device.
  245. *
  246. * @pdev: handle to the platform device structure.
  247. * Return: 0 on success, negative error otherwise.
  248. *
  249. * It does all the memory allocation and registration for the device.
  250. */
  251. static int cdns_wdt_probe(struct platform_device *pdev)
  252. {
  253. struct resource *res;
  254. int ret, irq;
  255. unsigned long clock_f;
  256. struct cdns_wdt *wdt;
  257. struct watchdog_device *cdns_wdt_device;
  258. wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
  259. if (!wdt)
  260. return -ENOMEM;
  261. cdns_wdt_device = &wdt->cdns_wdt_device;
  262. cdns_wdt_device->info = &cdns_wdt_info;
  263. cdns_wdt_device->ops = &cdns_wdt_ops;
  264. cdns_wdt_device->timeout = CDNS_WDT_DEFAULT_TIMEOUT;
  265. cdns_wdt_device->min_timeout = CDNS_WDT_MIN_TIMEOUT;
  266. cdns_wdt_device->max_timeout = CDNS_WDT_MAX_TIMEOUT;
  267. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  268. wdt->regs = devm_ioremap_resource(&pdev->dev, res);
  269. if (IS_ERR(wdt->regs))
  270. return PTR_ERR(wdt->regs);
  271. /* Register the interrupt */
  272. wdt->rst = of_property_read_bool(pdev->dev.of_node, "reset-on-timeout");
  273. irq = platform_get_irq(pdev, 0);
  274. if (!wdt->rst && irq >= 0) {
  275. ret = devm_request_irq(&pdev->dev, irq, cdns_wdt_irq_handler, 0,
  276. pdev->name, pdev);
  277. if (ret) {
  278. dev_err(&pdev->dev,
  279. "cannot register interrupt handler err=%d\n",
  280. ret);
  281. return ret;
  282. }
  283. }
  284. /* Initialize the members of cdns_wdt structure */
  285. cdns_wdt_device->parent = &pdev->dev;
  286. ret = watchdog_init_timeout(cdns_wdt_device, wdt_timeout, &pdev->dev);
  287. if (ret) {
  288. dev_err(&pdev->dev, "unable to set timeout value\n");
  289. return ret;
  290. }
  291. watchdog_set_nowayout(cdns_wdt_device, nowayout);
  292. watchdog_stop_on_reboot(cdns_wdt_device);
  293. watchdog_set_drvdata(cdns_wdt_device, wdt);
  294. wdt->clk = devm_clk_get(&pdev->dev, NULL);
  295. if (IS_ERR(wdt->clk)) {
  296. dev_err(&pdev->dev, "input clock not found\n");
  297. ret = PTR_ERR(wdt->clk);
  298. return ret;
  299. }
  300. ret = clk_prepare_enable(wdt->clk);
  301. if (ret) {
  302. dev_err(&pdev->dev, "unable to enable clock\n");
  303. return ret;
  304. }
  305. clock_f = clk_get_rate(wdt->clk);
  306. if (clock_f <= CDNS_WDT_CLK_75MHZ) {
  307. wdt->prescaler = CDNS_WDT_PRESCALE_512;
  308. wdt->ctrl_clksel = CDNS_WDT_PRESCALE_SELECT_512;
  309. } else {
  310. wdt->prescaler = CDNS_WDT_PRESCALE_4096;
  311. wdt->ctrl_clksel = CDNS_WDT_PRESCALE_SELECT_4096;
  312. }
  313. spin_lock_init(&wdt->io_lock);
  314. ret = watchdog_register_device(cdns_wdt_device);
  315. if (ret) {
  316. dev_err(&pdev->dev, "Failed to register wdt device\n");
  317. goto err_clk_disable;
  318. }
  319. platform_set_drvdata(pdev, wdt);
  320. dev_dbg(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds%s\n",
  321. wdt->regs, cdns_wdt_device->timeout,
  322. nowayout ? ", nowayout" : "");
  323. return 0;
  324. err_clk_disable:
  325. clk_disable_unprepare(wdt->clk);
  326. return ret;
  327. }
  328. /**
  329. * cdns_wdt_remove - Probe call for the device.
  330. *
  331. * @pdev: handle to the platform device structure.
  332. * Return: 0 on success, otherwise negative error.
  333. *
  334. * Unregister the device after releasing the resources.
  335. */
  336. static int cdns_wdt_remove(struct platform_device *pdev)
  337. {
  338. struct cdns_wdt *wdt = platform_get_drvdata(pdev);
  339. cdns_wdt_stop(&wdt->cdns_wdt_device);
  340. watchdog_unregister_device(&wdt->cdns_wdt_device);
  341. clk_disable_unprepare(wdt->clk);
  342. return 0;
  343. }
  344. /**
  345. * cdns_wdt_shutdown - Stop the device.
  346. *
  347. * @pdev: handle to the platform structure.
  348. *
  349. */
  350. static void cdns_wdt_shutdown(struct platform_device *pdev)
  351. {
  352. struct cdns_wdt *wdt = platform_get_drvdata(pdev);
  353. cdns_wdt_stop(&wdt->cdns_wdt_device);
  354. clk_disable_unprepare(wdt->clk);
  355. }
  356. /**
  357. * cdns_wdt_suspend - Stop the device.
  358. *
  359. * @dev: handle to the device structure.
  360. * Return: 0 always.
  361. */
  362. static int __maybe_unused cdns_wdt_suspend(struct device *dev)
  363. {
  364. struct platform_device *pdev = to_platform_device(dev);
  365. struct cdns_wdt *wdt = platform_get_drvdata(pdev);
  366. if (watchdog_active(&wdt->cdns_wdt_device)) {
  367. cdns_wdt_stop(&wdt->cdns_wdt_device);
  368. clk_disable_unprepare(wdt->clk);
  369. }
  370. return 0;
  371. }
  372. /**
  373. * cdns_wdt_resume - Resume the device.
  374. *
  375. * @dev: handle to the device structure.
  376. * Return: 0 on success, errno otherwise.
  377. */
  378. static int __maybe_unused cdns_wdt_resume(struct device *dev)
  379. {
  380. int ret;
  381. struct platform_device *pdev = to_platform_device(dev);
  382. struct cdns_wdt *wdt = platform_get_drvdata(pdev);
  383. if (watchdog_active(&wdt->cdns_wdt_device)) {
  384. ret = clk_prepare_enable(wdt->clk);
  385. if (ret) {
  386. dev_err(dev, "unable to enable clock\n");
  387. return ret;
  388. }
  389. cdns_wdt_start(&wdt->cdns_wdt_device);
  390. }
  391. return 0;
  392. }
  393. static SIMPLE_DEV_PM_OPS(cdns_wdt_pm_ops, cdns_wdt_suspend, cdns_wdt_resume);
  394. static struct of_device_id cdns_wdt_of_match[] = {
  395. { .compatible = "cdns,wdt-r1p2", },
  396. { /* end of table */ }
  397. };
  398. MODULE_DEVICE_TABLE(of, cdns_wdt_of_match);
  399. /* Driver Structure */
  400. static struct platform_driver cdns_wdt_driver = {
  401. .probe = cdns_wdt_probe,
  402. .remove = cdns_wdt_remove,
  403. .shutdown = cdns_wdt_shutdown,
  404. .driver = {
  405. .name = "cdns-wdt",
  406. .of_match_table = cdns_wdt_of_match,
  407. .pm = &cdns_wdt_pm_ops,
  408. },
  409. };
  410. module_platform_driver(cdns_wdt_driver);
  411. MODULE_AUTHOR("Xilinx, Inc.");
  412. MODULE_DESCRIPTION("Watchdog driver for Cadence WDT");
  413. MODULE_LICENSE("GPL");