s3c2410_wdt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * Copyright (c) 2004 Simtec Electronics
  3. * Ben Dooks <ben@simtec.co.uk>
  4. *
  5. * S3C2410 Watchdog Timer Support
  6. *
  7. * Based on, softdog.c by Alan Cox,
  8. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/types.h>
  23. #include <linux/timer.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/clk.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/io.h>
  30. #include <linux/cpufreq.h>
  31. #include <linux/slab.h>
  32. #include <linux/err.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/mfd/syscon.h>
  36. #include <linux/regmap.h>
  37. #include <linux/delay.h>
  38. #define S3C2410_WTCON 0x00
  39. #define S3C2410_WTDAT 0x04
  40. #define S3C2410_WTCNT 0x08
  41. #define S3C2410_WTCLRINT 0x0c
  42. #define S3C2410_WTCNT_MAXCNT 0xffff
  43. #define S3C2410_WTCON_RSTEN (1 << 0)
  44. #define S3C2410_WTCON_INTEN (1 << 2)
  45. #define S3C2410_WTCON_ENABLE (1 << 5)
  46. #define S3C2410_WTCON_DIV16 (0 << 3)
  47. #define S3C2410_WTCON_DIV32 (1 << 3)
  48. #define S3C2410_WTCON_DIV64 (2 << 3)
  49. #define S3C2410_WTCON_DIV128 (3 << 3)
  50. #define S3C2410_WTCON_MAXDIV 0x80
  51. #define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
  52. #define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
  53. #define S3C2410_WTCON_PRESCALE_MAX 0xff
  54. #define S3C2410_WATCHDOG_ATBOOT (0)
  55. #define S3C2410_WATCHDOG_DEFAULT_TIME (15)
  56. #define EXYNOS5_RST_STAT_REG_OFFSET 0x0404
  57. #define EXYNOS5_WDT_DISABLE_REG_OFFSET 0x0408
  58. #define EXYNOS5_WDT_MASK_RESET_REG_OFFSET 0x040c
  59. #define QUIRK_HAS_PMU_CONFIG (1 << 0)
  60. #define QUIRK_HAS_RST_STAT (1 << 1)
  61. #define QUIRK_HAS_WTCLRINT_REG (1 << 2)
  62. /* These quirks require that we have a PMU register map */
  63. #define QUIRKS_HAVE_PMUREG (QUIRK_HAS_PMU_CONFIG | \
  64. QUIRK_HAS_RST_STAT)
  65. static bool nowayout = WATCHDOG_NOWAYOUT;
  66. static int tmr_margin;
  67. static int tmr_atboot = S3C2410_WATCHDOG_ATBOOT;
  68. static int soft_noboot;
  69. module_param(tmr_margin, int, 0);
  70. module_param(tmr_atboot, int, 0);
  71. module_param(nowayout, bool, 0);
  72. module_param(soft_noboot, int, 0);
  73. MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
  74. __MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
  75. MODULE_PARM_DESC(tmr_atboot,
  76. "Watchdog is started at boot time if set to 1, default="
  77. __MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
  78. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  79. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  80. MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
  81. /**
  82. * struct s3c2410_wdt_variant - Per-variant config data
  83. *
  84. * @disable_reg: Offset in pmureg for the register that disables the watchdog
  85. * timer reset functionality.
  86. * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
  87. * timer reset functionality.
  88. * @mask_bit: Bit number for the watchdog timer in the disable register and the
  89. * mask reset register.
  90. * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
  91. * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
  92. * reset.
  93. * @quirks: A bitfield of quirks.
  94. */
  95. struct s3c2410_wdt_variant {
  96. int disable_reg;
  97. int mask_reset_reg;
  98. int mask_bit;
  99. int rst_stat_reg;
  100. int rst_stat_bit;
  101. u32 quirks;
  102. };
  103. struct s3c2410_wdt {
  104. struct device *dev;
  105. struct clk *clock;
  106. void __iomem *reg_base;
  107. unsigned int count;
  108. spinlock_t lock;
  109. unsigned long wtcon_save;
  110. unsigned long wtdat_save;
  111. struct watchdog_device wdt_device;
  112. struct notifier_block freq_transition;
  113. const struct s3c2410_wdt_variant *drv_data;
  114. struct regmap *pmureg;
  115. };
  116. static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
  117. .quirks = 0
  118. };
  119. #ifdef CONFIG_OF
  120. static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
  121. .quirks = QUIRK_HAS_WTCLRINT_REG,
  122. };
  123. static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
  124. .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
  125. .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
  126. .mask_bit = 20,
  127. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  128. .rst_stat_bit = 20,
  129. .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
  130. | QUIRK_HAS_WTCLRINT_REG,
  131. };
  132. static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
  133. .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
  134. .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
  135. .mask_bit = 0,
  136. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  137. .rst_stat_bit = 9,
  138. .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
  139. | QUIRK_HAS_WTCLRINT_REG,
  140. };
  141. static const struct s3c2410_wdt_variant drv_data_exynos7 = {
  142. .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
  143. .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
  144. .mask_bit = 23,
  145. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  146. .rst_stat_bit = 23, /* A57 WDTRESET */
  147. .quirks = QUIRK_HAS_PMU_CONFIG | QUIRK_HAS_RST_STAT \
  148. | QUIRK_HAS_WTCLRINT_REG,
  149. };
  150. static const struct of_device_id s3c2410_wdt_match[] = {
  151. { .compatible = "samsung,s3c2410-wdt",
  152. .data = &drv_data_s3c2410 },
  153. { .compatible = "samsung,s3c6410-wdt",
  154. .data = &drv_data_s3c6410 },
  155. { .compatible = "samsung,exynos5250-wdt",
  156. .data = &drv_data_exynos5250 },
  157. { .compatible = "samsung,exynos5420-wdt",
  158. .data = &drv_data_exynos5420 },
  159. { .compatible = "samsung,exynos7-wdt",
  160. .data = &drv_data_exynos7 },
  161. {},
  162. };
  163. MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
  164. #endif
  165. static const struct platform_device_id s3c2410_wdt_ids[] = {
  166. {
  167. .name = "s3c2410-wdt",
  168. .driver_data = (unsigned long)&drv_data_s3c2410,
  169. },
  170. {}
  171. };
  172. MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
  173. /* functions */
  174. static inline unsigned int s3c2410wdt_max_timeout(struct clk *clock)
  175. {
  176. unsigned long freq = clk_get_rate(clock);
  177. return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
  178. / S3C2410_WTCON_MAXDIV);
  179. }
  180. static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
  181. {
  182. return container_of(nb, struct s3c2410_wdt, freq_transition);
  183. }
  184. static int s3c2410wdt_mask_and_disable_reset(struct s3c2410_wdt *wdt, bool mask)
  185. {
  186. int ret;
  187. u32 mask_val = 1 << wdt->drv_data->mask_bit;
  188. u32 val = 0;
  189. /* No need to do anything if no PMU CONFIG needed */
  190. if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_CONFIG))
  191. return 0;
  192. if (mask)
  193. val = mask_val;
  194. ret = regmap_update_bits(wdt->pmureg,
  195. wdt->drv_data->disable_reg,
  196. mask_val, val);
  197. if (ret < 0)
  198. goto error;
  199. ret = regmap_update_bits(wdt->pmureg,
  200. wdt->drv_data->mask_reset_reg,
  201. mask_val, val);
  202. error:
  203. if (ret < 0)
  204. dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
  205. return ret;
  206. }
  207. static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
  208. {
  209. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  210. spin_lock(&wdt->lock);
  211. writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
  212. spin_unlock(&wdt->lock);
  213. return 0;
  214. }
  215. static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
  216. {
  217. unsigned long wtcon;
  218. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  219. wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
  220. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  221. }
  222. static int s3c2410wdt_stop(struct watchdog_device *wdd)
  223. {
  224. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  225. spin_lock(&wdt->lock);
  226. __s3c2410wdt_stop(wdt);
  227. spin_unlock(&wdt->lock);
  228. return 0;
  229. }
  230. static int s3c2410wdt_start(struct watchdog_device *wdd)
  231. {
  232. unsigned long wtcon;
  233. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  234. spin_lock(&wdt->lock);
  235. __s3c2410wdt_stop(wdt);
  236. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  237. wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
  238. if (soft_noboot) {
  239. wtcon |= S3C2410_WTCON_INTEN;
  240. wtcon &= ~S3C2410_WTCON_RSTEN;
  241. } else {
  242. wtcon &= ~S3C2410_WTCON_INTEN;
  243. wtcon |= S3C2410_WTCON_RSTEN;
  244. }
  245. dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
  246. wdt->count, wtcon);
  247. writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
  248. writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
  249. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  250. spin_unlock(&wdt->lock);
  251. return 0;
  252. }
  253. static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
  254. {
  255. return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
  256. }
  257. static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
  258. unsigned int timeout)
  259. {
  260. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  261. unsigned long freq = clk_get_rate(wdt->clock);
  262. unsigned int count;
  263. unsigned int divisor = 1;
  264. unsigned long wtcon;
  265. if (timeout < 1)
  266. return -EINVAL;
  267. freq = DIV_ROUND_UP(freq, 128);
  268. count = timeout * freq;
  269. dev_dbg(wdt->dev, "Heartbeat: count=%d, timeout=%d, freq=%lu\n",
  270. count, timeout, freq);
  271. /* if the count is bigger than the watchdog register,
  272. then work out what we need to do (and if) we can
  273. actually make this value
  274. */
  275. if (count >= 0x10000) {
  276. divisor = DIV_ROUND_UP(count, 0xffff);
  277. if (divisor > 0x100) {
  278. dev_err(wdt->dev, "timeout %d too big\n", timeout);
  279. return -EINVAL;
  280. }
  281. }
  282. dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%d (%08x)\n",
  283. timeout, divisor, count, DIV_ROUND_UP(count, divisor));
  284. count = DIV_ROUND_UP(count, divisor);
  285. wdt->count = count;
  286. /* update the pre-scaler */
  287. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  288. wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
  289. wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
  290. writel(count, wdt->reg_base + S3C2410_WTDAT);
  291. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  292. wdd->timeout = (count * divisor) / freq;
  293. return 0;
  294. }
  295. static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
  296. void *data)
  297. {
  298. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  299. void __iomem *wdt_base = wdt->reg_base;
  300. /* disable watchdog, to be safe */
  301. writel(0, wdt_base + S3C2410_WTCON);
  302. /* put initial values into count and data */
  303. writel(0x80, wdt_base + S3C2410_WTCNT);
  304. writel(0x80, wdt_base + S3C2410_WTDAT);
  305. /* set the watchdog to go and reset... */
  306. writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
  307. S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
  308. wdt_base + S3C2410_WTCON);
  309. /* wait for reset to assert... */
  310. mdelay(500);
  311. return 0;
  312. }
  313. #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
  314. static const struct watchdog_info s3c2410_wdt_ident = {
  315. .options = OPTIONS,
  316. .firmware_version = 0,
  317. .identity = "S3C2410 Watchdog",
  318. };
  319. static const struct watchdog_ops s3c2410wdt_ops = {
  320. .owner = THIS_MODULE,
  321. .start = s3c2410wdt_start,
  322. .stop = s3c2410wdt_stop,
  323. .ping = s3c2410wdt_keepalive,
  324. .set_timeout = s3c2410wdt_set_heartbeat,
  325. .restart = s3c2410wdt_restart,
  326. };
  327. static const struct watchdog_device s3c2410_wdd = {
  328. .info = &s3c2410_wdt_ident,
  329. .ops = &s3c2410wdt_ops,
  330. .timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
  331. };
  332. /* interrupt handler code */
  333. static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
  334. {
  335. struct s3c2410_wdt *wdt = platform_get_drvdata(param);
  336. dev_info(wdt->dev, "watchdog timer expired (irq)\n");
  337. s3c2410wdt_keepalive(&wdt->wdt_device);
  338. if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
  339. writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
  340. return IRQ_HANDLED;
  341. }
  342. #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
  343. static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
  344. unsigned long val, void *data)
  345. {
  346. int ret;
  347. struct s3c2410_wdt *wdt = freq_to_wdt(nb);
  348. if (!s3c2410wdt_is_running(wdt))
  349. goto done;
  350. if (val == CPUFREQ_PRECHANGE) {
  351. /* To ensure that over the change we don't cause the
  352. * watchdog to trigger, we perform an keep-alive if
  353. * the watchdog is running.
  354. */
  355. s3c2410wdt_keepalive(&wdt->wdt_device);
  356. } else if (val == CPUFREQ_POSTCHANGE) {
  357. s3c2410wdt_stop(&wdt->wdt_device);
  358. ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  359. wdt->wdt_device.timeout);
  360. if (ret >= 0)
  361. s3c2410wdt_start(&wdt->wdt_device);
  362. else
  363. goto err;
  364. }
  365. done:
  366. return 0;
  367. err:
  368. dev_err(wdt->dev, "cannot set new value for timeout %d\n",
  369. wdt->wdt_device.timeout);
  370. return ret;
  371. }
  372. static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
  373. {
  374. wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
  375. return cpufreq_register_notifier(&wdt->freq_transition,
  376. CPUFREQ_TRANSITION_NOTIFIER);
  377. }
  378. static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
  379. {
  380. wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
  381. cpufreq_unregister_notifier(&wdt->freq_transition,
  382. CPUFREQ_TRANSITION_NOTIFIER);
  383. }
  384. #else
  385. static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
  386. {
  387. return 0;
  388. }
  389. static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
  390. {
  391. }
  392. #endif
  393. static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
  394. {
  395. unsigned int rst_stat;
  396. int ret;
  397. if (!(wdt->drv_data->quirks & QUIRK_HAS_RST_STAT))
  398. return 0;
  399. ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
  400. if (ret)
  401. dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
  402. else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
  403. return WDIOF_CARDRESET;
  404. return 0;
  405. }
  406. static inline const struct s3c2410_wdt_variant *
  407. s3c2410_get_wdt_drv_data(struct platform_device *pdev)
  408. {
  409. const struct s3c2410_wdt_variant *variant;
  410. variant = of_device_get_match_data(&pdev->dev);
  411. if (!variant) {
  412. /* Device matched by platform_device_id */
  413. variant = (struct s3c2410_wdt_variant *)
  414. platform_get_device_id(pdev)->driver_data;
  415. }
  416. return variant;
  417. }
  418. static int s3c2410wdt_probe(struct platform_device *pdev)
  419. {
  420. struct device *dev = &pdev->dev;
  421. struct s3c2410_wdt *wdt;
  422. struct resource *wdt_mem;
  423. struct resource *wdt_irq;
  424. unsigned int wtcon;
  425. int started = 0;
  426. int ret;
  427. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  428. if (!wdt)
  429. return -ENOMEM;
  430. wdt->dev = dev;
  431. spin_lock_init(&wdt->lock);
  432. wdt->wdt_device = s3c2410_wdd;
  433. wdt->drv_data = s3c2410_get_wdt_drv_data(pdev);
  434. if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
  435. wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
  436. "samsung,syscon-phandle");
  437. if (IS_ERR(wdt->pmureg)) {
  438. dev_err(dev, "syscon regmap lookup failed.\n");
  439. return PTR_ERR(wdt->pmureg);
  440. }
  441. }
  442. wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  443. if (wdt_irq == NULL) {
  444. dev_err(dev, "no irq resource specified\n");
  445. ret = -ENOENT;
  446. goto err;
  447. }
  448. /* get the memory region for the watchdog timer */
  449. wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  450. wdt->reg_base = devm_ioremap_resource(dev, wdt_mem);
  451. if (IS_ERR(wdt->reg_base)) {
  452. ret = PTR_ERR(wdt->reg_base);
  453. goto err;
  454. }
  455. wdt->clock = devm_clk_get(dev, "watchdog");
  456. if (IS_ERR(wdt->clock)) {
  457. dev_err(dev, "failed to find watchdog clock source\n");
  458. ret = PTR_ERR(wdt->clock);
  459. goto err;
  460. }
  461. ret = clk_prepare_enable(wdt->clock);
  462. if (ret < 0) {
  463. dev_err(dev, "failed to enable clock\n");
  464. return ret;
  465. }
  466. wdt->wdt_device.min_timeout = 1;
  467. wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt->clock);
  468. ret = s3c2410wdt_cpufreq_register(wdt);
  469. if (ret < 0) {
  470. dev_err(dev, "failed to register cpufreq\n");
  471. goto err_clk;
  472. }
  473. watchdog_set_drvdata(&wdt->wdt_device, wdt);
  474. /* see if we can actually set the requested timer margin, and if
  475. * not, try the default value */
  476. watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
  477. ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  478. wdt->wdt_device.timeout);
  479. if (ret) {
  480. started = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  481. S3C2410_WATCHDOG_DEFAULT_TIME);
  482. if (started == 0)
  483. dev_info(dev,
  484. "tmr_margin value out of range, default %d used\n",
  485. S3C2410_WATCHDOG_DEFAULT_TIME);
  486. else
  487. dev_info(dev, "default timer value is out of range, cannot start\n");
  488. }
  489. ret = devm_request_irq(dev, wdt_irq->start, s3c2410wdt_irq, 0,
  490. pdev->name, pdev);
  491. if (ret != 0) {
  492. dev_err(dev, "failed to install irq (%d)\n", ret);
  493. goto err_cpufreq;
  494. }
  495. watchdog_set_nowayout(&wdt->wdt_device, nowayout);
  496. watchdog_set_restart_priority(&wdt->wdt_device, 128);
  497. wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
  498. wdt->wdt_device.parent = dev;
  499. ret = watchdog_register_device(&wdt->wdt_device);
  500. if (ret) {
  501. dev_err(dev, "cannot register watchdog (%d)\n", ret);
  502. goto err_cpufreq;
  503. }
  504. ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
  505. if (ret < 0)
  506. goto err_unregister;
  507. if (tmr_atboot && started == 0) {
  508. dev_info(dev, "starting watchdog timer\n");
  509. s3c2410wdt_start(&wdt->wdt_device);
  510. } else if (!tmr_atboot) {
  511. /* if we're not enabling the watchdog, then ensure it is
  512. * disabled if it has been left running from the bootloader
  513. * or other source */
  514. s3c2410wdt_stop(&wdt->wdt_device);
  515. }
  516. platform_set_drvdata(pdev, wdt);
  517. /* print out a statement of readiness */
  518. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  519. dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
  520. (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
  521. (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
  522. (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
  523. return 0;
  524. err_unregister:
  525. watchdog_unregister_device(&wdt->wdt_device);
  526. err_cpufreq:
  527. s3c2410wdt_cpufreq_deregister(wdt);
  528. err_clk:
  529. clk_disable_unprepare(wdt->clock);
  530. err:
  531. return ret;
  532. }
  533. static int s3c2410wdt_remove(struct platform_device *dev)
  534. {
  535. int ret;
  536. struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
  537. ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
  538. if (ret < 0)
  539. return ret;
  540. watchdog_unregister_device(&wdt->wdt_device);
  541. s3c2410wdt_cpufreq_deregister(wdt);
  542. clk_disable_unprepare(wdt->clock);
  543. return 0;
  544. }
  545. static void s3c2410wdt_shutdown(struct platform_device *dev)
  546. {
  547. struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
  548. s3c2410wdt_mask_and_disable_reset(wdt, true);
  549. s3c2410wdt_stop(&wdt->wdt_device);
  550. }
  551. #ifdef CONFIG_PM_SLEEP
  552. static int s3c2410wdt_suspend(struct device *dev)
  553. {
  554. int ret;
  555. struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
  556. /* Save watchdog state, and turn it off. */
  557. wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
  558. wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
  559. ret = s3c2410wdt_mask_and_disable_reset(wdt, true);
  560. if (ret < 0)
  561. return ret;
  562. /* Note that WTCNT doesn't need to be saved. */
  563. s3c2410wdt_stop(&wdt->wdt_device);
  564. return 0;
  565. }
  566. static int s3c2410wdt_resume(struct device *dev)
  567. {
  568. int ret;
  569. struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
  570. /* Restore watchdog state. */
  571. writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
  572. writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
  573. writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
  574. ret = s3c2410wdt_mask_and_disable_reset(wdt, false);
  575. if (ret < 0)
  576. return ret;
  577. dev_info(dev, "watchdog %sabled\n",
  578. (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
  579. return 0;
  580. }
  581. #endif
  582. static SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops, s3c2410wdt_suspend,
  583. s3c2410wdt_resume);
  584. static struct platform_driver s3c2410wdt_driver = {
  585. .probe = s3c2410wdt_probe,
  586. .remove = s3c2410wdt_remove,
  587. .shutdown = s3c2410wdt_shutdown,
  588. .id_table = s3c2410_wdt_ids,
  589. .driver = {
  590. .name = "s3c2410-wdt",
  591. .pm = &s3c2410wdt_pm_ops,
  592. .of_match_table = of_match_ptr(s3c2410_wdt_match),
  593. },
  594. };
  595. module_platform_driver(s3c2410wdt_driver);
  596. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Dimitry Andric <dimitry.andric@tomtom.com>");
  597. MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
  598. MODULE_LICENSE("GPL");