s3c2410_wdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/types.h>
  28. #include <linux/timer.h>
  29. #include <linux/miscdevice.h> /* for MODULE_ALIAS_MISCDEV */
  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 <mach/map.h>
  41. #undef S3C_VA_WATCHDOG
  42. #define S3C_VA_WATCHDOG (0)
  43. #include <plat/regs-watchdog.h>
  44. #define PFX "s3c2410-wdt: "
  45. #define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
  46. #define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)
  47. static int nowayout = WATCHDOG_NOWAYOUT;
  48. static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
  49. static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
  50. static int soft_noboot;
  51. static int debug;
  52. module_param(tmr_margin, int, 0);
  53. module_param(tmr_atboot, int, 0);
  54. module_param(nowayout, int, 0);
  55. module_param(soft_noboot, int, 0);
  56. module_param(debug, int, 0);
  57. MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
  58. __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
  59. MODULE_PARM_DESC(tmr_atboot,
  60. "Watchdog is started at boot time if set to 1, default="
  61. __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
  62. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  63. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  64. MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
  65. "0 to reboot (default 0)");
  66. MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug (default 0)");
  67. static struct device *wdt_dev; /* platform device attached to */
  68. static struct resource *wdt_mem;
  69. static struct resource *wdt_irq;
  70. static struct clk *wdt_clock;
  71. static void __iomem *wdt_base;
  72. static unsigned int wdt_count;
  73. static DEFINE_SPINLOCK(wdt_lock);
  74. /* watchdog control routines */
  75. #define DBG(msg...) do { \
  76. if (debug) \
  77. printk(KERN_INFO msg); \
  78. } while (0)
  79. /* functions */
  80. static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
  81. {
  82. spin_lock(&wdt_lock);
  83. writel(wdt_count, wdt_base + S3C2410_WTCNT);
  84. spin_unlock(&wdt_lock);
  85. return 0;
  86. }
  87. static void __s3c2410wdt_stop(void)
  88. {
  89. unsigned long wtcon;
  90. wtcon = readl(wdt_base + S3C2410_WTCON);
  91. wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
  92. writel(wtcon, wdt_base + S3C2410_WTCON);
  93. }
  94. static int s3c2410wdt_stop(struct watchdog_device *wdd)
  95. {
  96. spin_lock(&wdt_lock);
  97. __s3c2410wdt_stop();
  98. spin_unlock(&wdt_lock);
  99. return 0;
  100. }
  101. static int s3c2410wdt_start(struct watchdog_device *wdd)
  102. {
  103. unsigned long wtcon;
  104. spin_lock(&wdt_lock);
  105. __s3c2410wdt_stop();
  106. wtcon = readl(wdt_base + S3C2410_WTCON);
  107. wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
  108. if (soft_noboot) {
  109. wtcon |= S3C2410_WTCON_INTEN;
  110. wtcon &= ~S3C2410_WTCON_RSTEN;
  111. } else {
  112. wtcon &= ~S3C2410_WTCON_INTEN;
  113. wtcon |= S3C2410_WTCON_RSTEN;
  114. }
  115. DBG("%s: wdt_count=0x%08x, wtcon=%08lx\n",
  116. __func__, wdt_count, wtcon);
  117. writel(wdt_count, wdt_base + S3C2410_WTDAT);
  118. writel(wdt_count, wdt_base + S3C2410_WTCNT);
  119. writel(wtcon, wdt_base + S3C2410_WTCON);
  120. spin_unlock(&wdt_lock);
  121. return 0;
  122. }
  123. static inline int s3c2410wdt_is_running(void)
  124. {
  125. return readl(wdt_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
  126. }
  127. static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeout)
  128. {
  129. unsigned long freq = clk_get_rate(wdt_clock);
  130. unsigned int count;
  131. unsigned int divisor = 1;
  132. unsigned long wtcon;
  133. if (timeout < 1)
  134. return -EINVAL;
  135. freq /= 128;
  136. count = timeout * freq;
  137. DBG("%s: count=%d, timeout=%d, freq=%lu\n",
  138. __func__, count, timeout, freq);
  139. /* if the count is bigger than the watchdog register,
  140. then work out what we need to do (and if) we can
  141. actually make this value
  142. */
  143. if (count >= 0x10000) {
  144. for (divisor = 1; divisor <= 0x100; divisor++) {
  145. if ((count / divisor) < 0x10000)
  146. break;
  147. }
  148. if ((count / divisor) >= 0x10000) {
  149. dev_err(wdt_dev, "timeout %d too big\n", timeout);
  150. return -EINVAL;
  151. }
  152. }
  153. DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n",
  154. __func__, timeout, divisor, count, count/divisor);
  155. count /= divisor;
  156. wdt_count = count;
  157. /* update the pre-scaler */
  158. wtcon = readl(wdt_base + S3C2410_WTCON);
  159. wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
  160. wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
  161. writel(count, wdt_base + S3C2410_WTDAT);
  162. writel(wtcon, wdt_base + S3C2410_WTCON);
  163. return 0;
  164. }
  165. #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
  166. static const struct watchdog_info s3c2410_wdt_ident = {
  167. .options = OPTIONS,
  168. .firmware_version = 0,
  169. .identity = "S3C2410 Watchdog",
  170. };
  171. static struct watchdog_ops s3c2410wdt_ops = {
  172. .owner = THIS_MODULE,
  173. .start = s3c2410wdt_start,
  174. .stop = s3c2410wdt_stop,
  175. .ping = s3c2410wdt_keepalive,
  176. .set_timeout = s3c2410wdt_set_heartbeat,
  177. };
  178. static struct watchdog_device s3c2410_wdd = {
  179. .info = &s3c2410_wdt_ident,
  180. .ops = &s3c2410wdt_ops,
  181. };
  182. /* interrupt handler code */
  183. static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
  184. {
  185. dev_info(wdt_dev, "watchdog timer expired (irq)\n");
  186. s3c2410wdt_keepalive(&s3c2410_wdd);
  187. return IRQ_HANDLED;
  188. }
  189. #ifdef CONFIG_CPU_FREQ
  190. static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
  191. unsigned long val, void *data)
  192. {
  193. int ret;
  194. if (!s3c2410wdt_is_running())
  195. goto done;
  196. if (val == CPUFREQ_PRECHANGE) {
  197. /* To ensure that over the change we don't cause the
  198. * watchdog to trigger, we perform an keep-alive if
  199. * the watchdog is running.
  200. */
  201. s3c2410wdt_keepalive(&s3c2410_wdd);
  202. } else if (val == CPUFREQ_POSTCHANGE) {
  203. s3c2410wdt_stop(&s3c2410_wdd);
  204. ret = s3c2410wdt_set_heartbeat(&s3c2410_wdd, s3c2410_wdd.timeout);
  205. if (ret >= 0)
  206. s3c2410wdt_start(&s3c2410_wdd);
  207. else
  208. goto err;
  209. }
  210. done:
  211. return 0;
  212. err:
  213. dev_err(wdt_dev, "cannot set new value for timeout %d\n",
  214. s3c2410_wdd.timeout);
  215. return ret;
  216. }
  217. static struct notifier_block s3c2410wdt_cpufreq_transition_nb = {
  218. .notifier_call = s3c2410wdt_cpufreq_transition,
  219. };
  220. static inline int s3c2410wdt_cpufreq_register(void)
  221. {
  222. return cpufreq_register_notifier(&s3c2410wdt_cpufreq_transition_nb,
  223. CPUFREQ_TRANSITION_NOTIFIER);
  224. }
  225. static inline void s3c2410wdt_cpufreq_deregister(void)
  226. {
  227. cpufreq_unregister_notifier(&s3c2410wdt_cpufreq_transition_nb,
  228. CPUFREQ_TRANSITION_NOTIFIER);
  229. }
  230. #else
  231. static inline int s3c2410wdt_cpufreq_register(void)
  232. {
  233. return 0;
  234. }
  235. static inline void s3c2410wdt_cpufreq_deregister(void)
  236. {
  237. }
  238. #endif
  239. static int __devinit s3c2410wdt_probe(struct platform_device *pdev)
  240. {
  241. struct device *dev;
  242. unsigned int wtcon;
  243. int started = 0;
  244. int ret;
  245. int size;
  246. DBG("%s: probe=%p\n", __func__, pdev);
  247. dev = &pdev->dev;
  248. wdt_dev = &pdev->dev;
  249. /* get the memory region for the watchdog timer */
  250. wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  251. if (wdt_mem == NULL) {
  252. dev_err(dev, "no memory resource specified\n");
  253. return -ENOENT;
  254. }
  255. size = resource_size(wdt_mem);
  256. if (!request_mem_region(wdt_mem->start, size, pdev->name)) {
  257. dev_err(dev, "failed to get memory region\n");
  258. return -EBUSY;
  259. }
  260. wdt_base = ioremap(wdt_mem->start, size);
  261. if (wdt_base == NULL) {
  262. dev_err(dev, "failed to ioremap() region\n");
  263. ret = -EINVAL;
  264. goto err_req;
  265. }
  266. DBG("probe: mapped wdt_base=%p\n", wdt_base);
  267. wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  268. if (wdt_irq == NULL) {
  269. dev_err(dev, "no irq resource specified\n");
  270. ret = -ENOENT;
  271. goto err_map;
  272. }
  273. ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
  274. if (ret != 0) {
  275. dev_err(dev, "failed to install irq (%d)\n", ret);
  276. goto err_map;
  277. }
  278. wdt_clock = clk_get(&pdev->dev, "watchdog");
  279. if (IS_ERR(wdt_clock)) {
  280. dev_err(dev, "failed to find watchdog clock source\n");
  281. ret = PTR_ERR(wdt_clock);
  282. goto err_irq;
  283. }
  284. clk_enable(wdt_clock);
  285. if (s3c2410wdt_cpufreq_register() < 0) {
  286. printk(KERN_ERR PFX "failed to register cpufreq\n");
  287. goto err_clk;
  288. }
  289. /* see if we can actually set the requested timer margin, and if
  290. * not, try the default value */
  291. if (s3c2410wdt_set_heartbeat(&s3c2410_wdd, tmr_margin)) {
  292. started = s3c2410wdt_set_heartbeat(&s3c2410_wdd,
  293. CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
  294. if (started == 0)
  295. dev_info(dev,
  296. "tmr_margin value out of range, default %d used\n",
  297. CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
  298. else
  299. dev_info(dev, "default timer value is out of range, "
  300. "cannot start\n");
  301. }
  302. ret = watchdog_register_device(&s3c2410_wdd);
  303. if (ret) {
  304. dev_err(dev, "cannot register watchdog (%d)\n", ret);
  305. goto err_cpufreq;
  306. }
  307. if (tmr_atboot && started == 0) {
  308. dev_info(dev, "starting watchdog timer\n");
  309. s3c2410wdt_start(&s3c2410_wdd);
  310. } else if (!tmr_atboot) {
  311. /* if we're not enabling the watchdog, then ensure it is
  312. * disabled if it has been left running from the bootloader
  313. * or other source */
  314. s3c2410wdt_stop(&s3c2410_wdd);
  315. }
  316. /* print out a statement of readiness */
  317. wtcon = readl(wdt_base + S3C2410_WTCON);
  318. dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
  319. (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
  320. (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
  321. (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");
  322. return 0;
  323. err_cpufreq:
  324. s3c2410wdt_cpufreq_deregister();
  325. err_clk:
  326. clk_disable(wdt_clock);
  327. clk_put(wdt_clock);
  328. err_irq:
  329. free_irq(wdt_irq->start, pdev);
  330. err_map:
  331. iounmap(wdt_base);
  332. err_req:
  333. release_mem_region(wdt_mem->start, size);
  334. wdt_mem = NULL;
  335. return ret;
  336. }
  337. static int __devexit s3c2410wdt_remove(struct platform_device *dev)
  338. {
  339. watchdog_unregister_device(&s3c2410_wdd);
  340. s3c2410wdt_cpufreq_deregister();
  341. clk_disable(wdt_clock);
  342. clk_put(wdt_clock);
  343. wdt_clock = NULL;
  344. free_irq(wdt_irq->start, dev);
  345. wdt_irq = NULL;
  346. iounmap(wdt_base);
  347. release_mem_region(wdt_mem->start, resource_size(wdt_mem));
  348. wdt_mem = NULL;
  349. return 0;
  350. }
  351. static void s3c2410wdt_shutdown(struct platform_device *dev)
  352. {
  353. s3c2410wdt_stop(&s3c2410_wdd);
  354. }
  355. #ifdef CONFIG_PM
  356. static unsigned long wtcon_save;
  357. static unsigned long wtdat_save;
  358. static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
  359. {
  360. /* Save watchdog state, and turn it off. */
  361. wtcon_save = readl(wdt_base + S3C2410_WTCON);
  362. wtdat_save = readl(wdt_base + S3C2410_WTDAT);
  363. /* Note that WTCNT doesn't need to be saved. */
  364. s3c2410wdt_stop(&s3c2410_wdd);
  365. return 0;
  366. }
  367. static int s3c2410wdt_resume(struct platform_device *dev)
  368. {
  369. /* Restore watchdog state. */
  370. writel(wtdat_save, wdt_base + S3C2410_WTDAT);
  371. writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
  372. writel(wtcon_save, wdt_base + S3C2410_WTCON);
  373. printk(KERN_INFO PFX "watchdog %sabled\n",
  374. (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
  375. return 0;
  376. }
  377. #else
  378. #define s3c2410wdt_suspend NULL
  379. #define s3c2410wdt_resume NULL
  380. #endif /* CONFIG_PM */
  381. #ifdef CONFIG_OF
  382. static const struct of_device_id s3c2410_wdt_match[] = {
  383. { .compatible = "samsung,s3c2410-wdt" },
  384. {},
  385. };
  386. MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
  387. #else
  388. #define s3c2410_wdt_match NULL
  389. #endif
  390. static struct platform_driver s3c2410wdt_driver = {
  391. .probe = s3c2410wdt_probe,
  392. .remove = __devexit_p(s3c2410wdt_remove),
  393. .shutdown = s3c2410wdt_shutdown,
  394. .suspend = s3c2410wdt_suspend,
  395. .resume = s3c2410wdt_resume,
  396. .driver = {
  397. .owner = THIS_MODULE,
  398. .name = "s3c2410-wdt",
  399. .of_match_table = s3c2410_wdt_match,
  400. },
  401. };
  402. static char banner[] __initdata =
  403. KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics\n";
  404. static int __init watchdog_init(void)
  405. {
  406. printk(banner);
  407. return platform_driver_register(&s3c2410wdt_driver);
  408. }
  409. static void __exit watchdog_exit(void)
  410. {
  411. platform_driver_unregister(&s3c2410wdt_driver);
  412. }
  413. module_init(watchdog_init);
  414. module_exit(watchdog_exit);
  415. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, "
  416. "Dimitry Andric <dimitry.andric@tomtom.com>");
  417. MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
  418. MODULE_LICENSE("GPL");
  419. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  420. MODULE_ALIAS("platform:s3c2410-wdt");