s3c2410_wdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* linux/drivers/char/watchdog/s3c2410_wdt.c
  2. *
  3. * Copyright (c) 2004 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C2410 Watchdog Timer Support
  7. *
  8. * Based on, softdog.c by Alan Cox,
  9. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/types.h>
  29. #include <linux/timer.h>
  30. #include <linux/watchdog.h>
  31. #include <linux/init.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/clk.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/io.h>
  37. #include <linux/cpufreq.h>
  38. #include <linux/slab.h>
  39. #include <linux/err.h>
  40. #include <linux/of.h>
  41. #define S3C2410_WTCON 0x00
  42. #define S3C2410_WTDAT 0x04
  43. #define S3C2410_WTCNT 0x08
  44. #define S3C2410_WTCON_RSTEN (1 << 0)
  45. #define S3C2410_WTCON_INTEN (1 << 2)
  46. #define S3C2410_WTCON_ENABLE (1 << 5)
  47. #define S3C2410_WTCON_DIV16 (0 << 3)
  48. #define S3C2410_WTCON_DIV32 (1 << 3)
  49. #define S3C2410_WTCON_DIV64 (2 << 3)
  50. #define S3C2410_WTCON_DIV128 (3 << 3)
  51. #define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
  52. #define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
  53. #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
  54. #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
  55. static bool nowayout = WATCHDOG_NOWAYOUT;
  56. static int tmr_margin;
  57. static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
  58. static int soft_noboot;
  59. static int debug;
  60. module_param(tmr_margin, int, 0);
  61. module_param(tmr_atboot, int, 0);
  62. module_param(nowayout, bool, 0);
  63. module_param(soft_noboot, int, 0);
  64. module_param(debug, int, 0);
  65. MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
  66. __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
  67. MODULE_PARM_DESC(tmr_atboot,
  68. "Watchdog is started at boot time if set to 1, default="
  69. __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
  70. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  71. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  72. MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
  73. "0 to reboot (default 0)");
  74. MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
  75. struct s3c2410_wdt {
  76. struct device *dev;
  77. struct clk *clock;
  78. void __iomem *reg_base;
  79. unsigned int count;
  80. spinlock_t lock;
  81. unsigned long wtcon_save;
  82. unsigned long wtdat_save;
  83. struct watchdog_device wdt_device;
  84. struct notifier_block freq_transition;
  85. };
  86. /* watchdog control routines */
  87. #define DBG(fmt, ...) \
  88. do { \
  89. if (debug) \
  90. pr_info(fmt, ##__VA_ARGS__); \
  91. } while (0)
  92. /* functions */
  93. static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
  94. {
  95. return container_of(nb, struct s3c2410_wdt, freq_transition);
  96. }
  97. static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
  98. {
  99. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  100. spin_lock(&wdt->lock);
  101. writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
  102. spin_unlock(&wdt->lock);
  103. return 0;
  104. }
  105. static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
  106. {
  107. unsigned long wtcon;
  108. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  109. wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
  110. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  111. }
  112. static int s3c2410wdt_stop(struct watchdog_device *wdd)
  113. {
  114. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  115. spin_lock(&wdt->lock);
  116. __s3c2410wdt_stop(wdt);
  117. spin_unlock(&wdt->lock);
  118. return 0;
  119. }
  120. static int s3c2410wdt_start(struct watchdog_device *wdd)
  121. {
  122. unsigned long wtcon;
  123. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  124. spin_lock(&wdt->lock);
  125. __s3c2410wdt_stop(wdt);
  126. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  127. wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
  128. if (soft_noboot) {
  129. wtcon |= S3C2410_WTCON_INTEN;
  130. wtcon &= ~S3C2410_WTCON_RSTEN;
  131. } else {
  132. wtcon &= ~S3C2410_WTCON_INTEN;
  133. wtcon |= S3C2410_WTCON_RSTEN;
  134. }
  135. DBG("%s: count=0x%08x, wtcon=%08lx\n",
  136. __func__, wdt->count, wtcon);
  137. writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
  138. writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
  139. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  140. spin_unlock(&wdt->lock);
  141. return 0;
  142. }
  143. static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
  144. {
  145. return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
  146. }
  147. static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
  148. {
  149. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  150. unsigned long freq = clk_get_rate(wdt->clock);
  151. unsigned int count;
  152. unsigned int divisor = 1;
  153. unsigned long wtcon;
  154. if (timeout < 1)
  155. return -EINVAL;
  156. freq /= 128;
  157. count = timeout * freq;
  158. DBG("%s: count=%d, timeout=%d, freq=%lu\n",
  159. __func__, count, timeout, freq);
  160. /* if the count is bigger than the watchdog register,
  161. then work out what we need to do (and if) we can
  162. actually make this value
  163. */
  164. if (count >= 0x10000) {
  165. for (divisor = 1; divisor <= 0x100; divisor++) {
  166. if ((count / divisor) < 0x10000)
  167. break;
  168. }
  169. if ((count / divisor) >= 0x10000) {
  170. dev_err(wdt->dev, "timeout %d too big\n", timeout);
  171. return -EINVAL;
  172. }
  173. }
  174. DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
  175. __func__, timeout, divisor, count, count/divisor);
  176. count /= divisor;
  177. wdt->count = count;
  178. /* update the pre-scaler */
  179. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  180. wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
  181. wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
  182. writel(count, wdt->reg_base + S3C2410_WTDAT);
  183. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  184. wdd->timeout = (count * divisor) / freq;
  185. return 0;
  186. }
  187. #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
  188. static const struct watchdog_info s3c2410_wdt_ident = {
  189. .options = OPTIONS,
  190. .firmware_version = 0,
  191. .identity = "S3C2410 Watchdog",
  192. };
  193. static struct watchdog_ops s3c2410wdt_ops = {
  194. .owner = THIS_MODULE,
  195. .start = s3c2410wdt_start,
  196. .stop = s3c2410wdt_stop,
  197. .ping = s3c2410wdt_keepalive,
  198. .set_timeout = s3c2410wdt_set_heartbeat,
  199. };
  200. static struct watchdog_device s3c2410_wdd = {
  201. .info = &s3c2410_wdt_ident,
  202. .ops = &s3c2410wdt_ops,
  203. .timeout = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME,
  204. };
  205. /* interrupt handler code */
  206. static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
  207. {
  208. struct s3c2410_wdt *wdt = platform_get_drvdata(param);
  209. dev_info(wdt->dev, "watchdog timer expired (irq)\n");
  210. s3c2410wdt_keepalive(&wdt->wdt_device);
  211. return IRQ_HANDLED;
  212. }
  213. #ifdef CONFIG_CPU_FREQ
  214. static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
  215. unsigned long val, void *data)
  216. {
  217. int ret;
  218. struct s3c2410_wdt *wdt = freq_to_wdt(nb);
  219. if (!s3c2410wdt_is_running(wdt))
  220. goto done;
  221. if (val == CPUFREQ_PRECHANGE) {
  222. /* To ensure that over the change we don't cause the
  223. * watchdog to trigger, we perform an keep-alive if
  224. * the watchdog is running.
  225. */
  226. s3c2410wdt_keepalive(&wdt->wdt_device);
  227. } else if (val == CPUFREQ_POSTCHANGE) {
  228. s3c2410wdt_stop(&wdt->wdt_device);
  229. ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  230. wdt->wdt_device.timeout);
  231. if (ret >= 0)
  232. s3c2410wdt_start(&wdt->wdt_device);
  233. else
  234. goto err;
  235. }
  236. done:
  237. return 0;
  238. err:
  239. dev_err(wdt->dev, "cannot set new value for timeout %d\n",
  240. wdt->wdt_device.timeout);
  241. return ret;
  242. }
  243. static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
  244. {
  245. wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
  246. return cpufreq_register_notifier(&wdt->freq_transition,
  247. CPUFREQ_TRANSITION_NOTIFIER);
  248. }
  249. static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
  250. {
  251. wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
  252. cpufreq_unregister_notifier(&wdt->freq_transition,
  253. CPUFREQ_TRANSITION_NOTIFIER);
  254. }
  255. #else
  256. static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
  257. {
  258. return 0;
  259. }
  260. static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
  261. {
  262. }
  263. #endif
  264. static int s3c2410wdt_probe(struct platform_device *pdev)
  265. {
  266. struct device *dev;
  267. struct s3c2410_wdt *wdt;
  268. struct resource *wdt_mem;
  269. struct resource *wdt_irq;
  270. unsigned int wtcon;
  271. int started = 0;
  272. int ret;
  273. DBG("%s: probe=%p\n", __func__, pdev);
  274. dev = &pdev->dev;
  275. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  276. if (!wdt)
  277. return -ENOMEM;
  278. wdt->dev = &pdev->dev;
  279. spin_lock_init(&wdt->lock);
  280. wdt->wdt_device = s3c2410_wdd;
  281. wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  282. if (wdt_irq == NULL) {
  283. dev_err(dev, "no irq resource specified\n");
  284. ret = -ENOENT;
  285. goto err;
  286. }
  287. /* get the memory region for the watchdog timer */
  288. wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  289. wdt->reg_base = devm_ioremap_resource(dev, wdt_mem);
  290. if (IS_ERR(wdt->reg_base)) {
  291. ret = PTR_ERR(wdt->reg_base);
  292. goto err;
  293. }
  294. DBG("probe: mapped reg_base=%p\n", wdt->reg_base);
  295. wdt->clock = devm_clk_get(dev, "watchdog");
  296. if (IS_ERR(wdt->clock)) {
  297. dev_err(dev, "failed to find watchdog clock source\n");
  298. ret = PTR_ERR(wdt->clock);
  299. goto err;
  300. }
  301. clk_prepare_enable(wdt->clock);
  302. ret = s3c2410wdt_cpufreq_register(wdt);
  303. if (ret < 0) {
  304. dev_err(dev, "failed to register cpufreq\n");
  305. goto err_clk;
  306. }
  307. watchdog_set_drvdata(&wdt->wdt_device, wdt);
  308. /* see if we can actually set the requested timer margin, and if
  309. * not, try the default value */
  310. watchdog_init_timeout(&wdt->wdt_device, tmr_margin, &pdev->dev);
  311. ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  312. wdt->wdt_device.timeout);
  313. if (ret) {
  314. started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  315. CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
  316. if (started == 0)
  317. dev_info(dev,
  318. "tmr_margin value out of range, default %d used\n",
  319. CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
  320. else
  321. dev_info(dev, "default timer value is out of range, "
  322. "cannot start\n");
  323. }
  324. ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
  325. pdev->name, pdev);
  326. if (ret != 0) {
  327. dev_err(dev, "failed to install irq (%d)\n", ret);
  328. goto err_cpufreq;
  329. }
  330. watchdog_set_nowayout(&wdt->wdt_device, nowayout);
  331. ret = watchdog_register_device(&wdt->wdt_device);
  332. if (ret) {
  333. dev_err(dev, "cannot register watchdog (%d)\n", ret);
  334. goto err_cpufreq;
  335. }
  336. if (tmr_atboot && started == 0) {
  337. dev_info(dev, "starting watchdog timer\n");
  338. s3c2410wdt_start(&wdt->wdt_device);
  339. } else if (!tmr_atboot) {
  340. /* if we're not enabling the watchdog, then ensure it is
  341. * disabled if it has been left running from the bootloader
  342. * or other source */
  343. s3c2410wdt_stop(&wdt->wdt_device);
  344. }
  345. platform_set_drvdata(pdev, wdt);
  346. /* print out a statement of readiness */
  347. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  348. dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
  349. (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
  350. (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
  351. (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
  352. return 0;
  353. err_cpufreq:
  354. s3c2410wdt_cpufreq_deregister(wdt);
  355. err_clk:
  356. clk_disable_unprepare(wdt->clock);
  357. wdt->clock = NULL;
  358. err:
  359. return ret;
  360. }
  361. static int s3c2410wdt_remove(struct platform_device *dev)
  362. {
  363. struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
  364. watchdog_unregister_device(&wdt->wdt_device);
  365. s3c2410wdt_cpufreq_deregister(wdt);
  366. clk_disable_unprepare(wdt->clock);
  367. wdt->clock = NULL;
  368. return 0;
  369. }
  370. static void s3c2410wdt_shutdown(struct platform_device *dev)
  371. {
  372. struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
  373. s3c2410wdt_stop(&wdt->wdt_device);
  374. }
  375. #ifdef CONFIG_PM_SLEEP
  376. static int s3c2410wdt_suspend(struct device *dev)
  377. {
  378. struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
  379. /* Save watchdog state, and turn it off. */
  380. wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
  381. wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
  382. /* Note that WTCNT doesn't need to be saved. */
  383. s3c2410wdt_stop(&wdt->wdt_device);
  384. return 0;
  385. }
  386. static int s3c2410wdt_resume(struct device *dev)
  387. {
  388. struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
  389. /* Restore watchdog state. */
  390. writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
  391. writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
  392. writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
  393. dev_info(dev, "watchdog %sabled\n",
  394. (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
  395. return 0;
  396. }
  397. #endif
  398. static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
  399. s3c2410wdt_resume);
  400. #ifdef CONFIG_OF
  401. static const struct of_device_id s3c2410_wdt_match[] = {
  402. { .compatible = "samsung,s3c2410-wdt" },
  403. {},
  404. };
  405. MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
  406. #endif
  407. static struct platform_driver s3c2410wdt_driver = {
  408. .probe = s3c2410wdt_probe,
  409. .remove = s3c2410wdt_remove,
  410. .shutdown = s3c2410wdt_shutdown,
  411. .driver = {
  412. .owner = THIS_MODULE,
  413. .name = "s3c2410-wdt",
  414. .pm = &s3c2410wdt_pm_ops,
  415. .of_match_table = of_match_ptr(s3c2410_wdt_match),
  416. },
  417. };
  418. module_platform_driver(s3c2410wdt_driver);
  419. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
  420. "Dimitry Andric <dimitry.andric@tomtom.com>");
  421. MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
  422. MODULE_LICENSE("GPL");
  423. MODULE_ALIAS("platform:s3c2410-wdt");