s3c2410_wdt.c 19 KB

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