cadence_wdt.c 14 KB

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