s3c2410_wdt.c 20 KB

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