s3c2410_wdt.c 19 KB

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