s3c2410_wdt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2004 Simtec Electronics
  4. * Ben Dooks <[email protected]>
  5. *
  6. * S3C2410 Watchdog Timer Support
  7. *
  8. * Based on, softdog.c by Alan Cox,
  9. * (c) Copyright 1996 Alan Cox <[email protected]>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/types.h>
  14. #include <linux/timer.h>
  15. #include <linux/watchdog.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/clk.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/io.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/slab.h>
  23. #include <linux/err.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <linux/regmap.h>
  28. #include <linux/delay.h>
  29. #define S3C2410_WTCON 0x00
  30. #define S3C2410_WTDAT 0x04
  31. #define S3C2410_WTCNT 0x08
  32. #define S3C2410_WTCLRINT 0x0c
  33. #define S3C2410_WTCNT_MAXCNT 0xffff
  34. #define S3C2410_WTCON_RSTEN (1 << 0)
  35. #define S3C2410_WTCON_INTEN (1 << 2)
  36. #define S3C2410_WTCON_ENABLE (1 << 5)
  37. #define S3C2410_WTCON_DIV16 (0 << 3)
  38. #define S3C2410_WTCON_DIV32 (1 << 3)
  39. #define S3C2410_WTCON_DIV64 (2 << 3)
  40. #define S3C2410_WTCON_DIV128 (3 << 3)
  41. #define S3C2410_WTCON_MAXDIV 0x80
  42. #define S3C2410_WTCON_PRESCALE(x) ((x) << 8)
  43. #define S3C2410_WTCON_PRESCALE_MASK (0xff << 8)
  44. #define S3C2410_WTCON_PRESCALE_MAX 0xff
  45. #define S3C2410_WATCHDOG_ATBOOT (0)
  46. #define S3C2410_WATCHDOG_DEFAULT_TIME (15)
  47. #define EXYNOS5_RST_STAT_REG_OFFSET 0x0404
  48. #define EXYNOS5_WDT_DISABLE_REG_OFFSET 0x0408
  49. #define EXYNOS5_WDT_MASK_RESET_REG_OFFSET 0x040c
  50. #define EXYNOS850_CLUSTER0_NONCPU_OUT 0x1220
  51. #define EXYNOS850_CLUSTER0_NONCPU_INT_EN 0x1244
  52. #define EXYNOS850_CLUSTER1_NONCPU_OUT 0x1620
  53. #define EXYNOS850_CLUSTER1_NONCPU_INT_EN 0x1644
  54. #define EXYNOSAUTOV9_CLUSTER1_NONCPU_OUT 0x1520
  55. #define EXYNOSAUTOV9_CLUSTER1_NONCPU_INT_EN 0x1544
  56. #define EXYNOS850_CLUSTER0_WDTRESET_BIT 24
  57. #define EXYNOS850_CLUSTER1_WDTRESET_BIT 23
  58. #define EXYNOSAUTOV9_CLUSTER0_WDTRESET_BIT 25
  59. #define EXYNOSAUTOV9_CLUSTER1_WDTRESET_BIT 24
  60. /**
  61. * DOC: Quirk flags for different Samsung watchdog IP-cores
  62. *
  63. * This driver supports multiple Samsung SoCs, each of which might have
  64. * different set of registers and features supported. As watchdog block
  65. * sometimes requires modifying PMU registers for proper functioning, register
  66. * differences in both watchdog and PMU IP-cores should be accounted for. Quirk
  67. * flags described below serve the purpose of telling the driver about mentioned
  68. * SoC traits, and can be specified in driver data for each particular supported
  69. * device.
  70. *
  71. * %QUIRK_HAS_WTCLRINT_REG: Watchdog block has WTCLRINT register. It's used to
  72. * clear the interrupt once the interrupt service routine is complete. It's
  73. * write-only, writing any values to this register clears the interrupt, but
  74. * reading is not permitted.
  75. *
  76. * %QUIRK_HAS_PMU_MASK_RESET: PMU block has the register for disabling/enabling
  77. * WDT reset request. On old SoCs it's usually called MASK_WDT_RESET_REQUEST,
  78. * new SoCs have CLUSTERx_NONCPU_INT_EN register, which 'mask_bit' value is
  79. * inverted compared to the former one.
  80. *
  81. * %QUIRK_HAS_PMU_RST_STAT: PMU block has RST_STAT (reset status) register,
  82. * which contains bits indicating the reason for most recent CPU reset. If
  83. * present, driver will use this register to check if previous reboot was due to
  84. * watchdog timer reset.
  85. *
  86. * %QUIRK_HAS_PMU_AUTO_DISABLE: PMU block has AUTOMATIC_WDT_RESET_DISABLE
  87. * register. If 'mask_bit' bit is set, PMU will disable WDT reset when
  88. * corresponding processor is in reset state.
  89. *
  90. * %QUIRK_HAS_PMU_CNT_EN: PMU block has some register (e.g. CLUSTERx_NONCPU_OUT)
  91. * with "watchdog counter enable" bit. That bit should be set to make watchdog
  92. * counter running.
  93. */
  94. #define QUIRK_HAS_WTCLRINT_REG (1 << 0)
  95. #define QUIRK_HAS_PMU_MASK_RESET (1 << 1)
  96. #define QUIRK_HAS_PMU_RST_STAT (1 << 2)
  97. #define QUIRK_HAS_PMU_AUTO_DISABLE (1 << 3)
  98. #define QUIRK_HAS_PMU_CNT_EN (1 << 4)
  99. /* These quirks require that we have a PMU register map */
  100. #define QUIRKS_HAVE_PMUREG \
  101. (QUIRK_HAS_PMU_MASK_RESET | QUIRK_HAS_PMU_RST_STAT | \
  102. QUIRK_HAS_PMU_AUTO_DISABLE | QUIRK_HAS_PMU_CNT_EN)
  103. static bool nowayout = WATCHDOG_NOWAYOUT;
  104. static int tmr_margin;
  105. static int tmr_atboot = S3C2410_WATCHDOG_ATBOOT;
  106. static int soft_noboot;
  107. module_param(tmr_margin, int, 0);
  108. module_param(tmr_atboot, int, 0);
  109. module_param(nowayout, bool, 0);
  110. module_param(soft_noboot, int, 0);
  111. MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. (default="
  112. __MODULE_STRING(S3C2410_WATCHDOG_DEFAULT_TIME) ")");
  113. MODULE_PARM_DESC(tmr_atboot,
  114. "Watchdog is started at boot time if set to 1, default="
  115. __MODULE_STRING(S3C2410_WATCHDOG_ATBOOT));
  116. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  117. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  118. MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, 0 to reboot (default 0)");
  119. /**
  120. * struct s3c2410_wdt_variant - Per-variant config data
  121. *
  122. * @disable_reg: Offset in pmureg for the register that disables the watchdog
  123. * timer reset functionality.
  124. * @mask_reset_reg: Offset in pmureg for the register that masks the watchdog
  125. * timer reset functionality.
  126. * @mask_reset_inv: If set, mask_reset_reg value will have inverted meaning.
  127. * @mask_bit: Bit number for the watchdog timer in the disable register and the
  128. * mask reset register.
  129. * @rst_stat_reg: Offset in pmureg for the register that has the reset status.
  130. * @rst_stat_bit: Bit number in the rst_stat register indicating a watchdog
  131. * reset.
  132. * @cnt_en_reg: Offset in pmureg for the register that enables WDT counter.
  133. * @cnt_en_bit: Bit number for "watchdog counter enable" in cnt_en register.
  134. * @quirks: A bitfield of quirks.
  135. */
  136. struct s3c2410_wdt_variant {
  137. int disable_reg;
  138. int mask_reset_reg;
  139. bool mask_reset_inv;
  140. int mask_bit;
  141. int rst_stat_reg;
  142. int rst_stat_bit;
  143. int cnt_en_reg;
  144. int cnt_en_bit;
  145. u32 quirks;
  146. };
  147. struct s3c2410_wdt {
  148. struct device *dev;
  149. struct clk *bus_clk; /* for register interface (PCLK) */
  150. struct clk *src_clk; /* for WDT counter */
  151. void __iomem *reg_base;
  152. unsigned int count;
  153. spinlock_t lock;
  154. unsigned long wtcon_save;
  155. unsigned long wtdat_save;
  156. struct watchdog_device wdt_device;
  157. struct notifier_block freq_transition;
  158. const struct s3c2410_wdt_variant *drv_data;
  159. struct regmap *pmureg;
  160. };
  161. static const struct s3c2410_wdt_variant drv_data_s3c2410 = {
  162. .quirks = 0
  163. };
  164. #ifdef CONFIG_OF
  165. static const struct s3c2410_wdt_variant drv_data_s3c6410 = {
  166. .quirks = QUIRK_HAS_WTCLRINT_REG,
  167. };
  168. static const struct s3c2410_wdt_variant drv_data_exynos5250 = {
  169. .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
  170. .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
  171. .mask_bit = 20,
  172. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  173. .rst_stat_bit = 20,
  174. .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
  175. QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
  176. };
  177. static const struct s3c2410_wdt_variant drv_data_exynos5420 = {
  178. .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
  179. .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
  180. .mask_bit = 0,
  181. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  182. .rst_stat_bit = 9,
  183. .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
  184. QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
  185. };
  186. static const struct s3c2410_wdt_variant drv_data_exynos7 = {
  187. .disable_reg = EXYNOS5_WDT_DISABLE_REG_OFFSET,
  188. .mask_reset_reg = EXYNOS5_WDT_MASK_RESET_REG_OFFSET,
  189. .mask_bit = 23,
  190. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  191. .rst_stat_bit = 23, /* A57 WDTRESET */
  192. .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
  193. QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_AUTO_DISABLE,
  194. };
  195. static const struct s3c2410_wdt_variant drv_data_exynos850_cl0 = {
  196. .mask_reset_reg = EXYNOS850_CLUSTER0_NONCPU_INT_EN,
  197. .mask_bit = 2,
  198. .mask_reset_inv = true,
  199. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  200. .rst_stat_bit = EXYNOS850_CLUSTER0_WDTRESET_BIT,
  201. .cnt_en_reg = EXYNOS850_CLUSTER0_NONCPU_OUT,
  202. .cnt_en_bit = 7,
  203. .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
  204. QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
  205. };
  206. static const struct s3c2410_wdt_variant drv_data_exynos850_cl1 = {
  207. .mask_reset_reg = EXYNOS850_CLUSTER1_NONCPU_INT_EN,
  208. .mask_bit = 2,
  209. .mask_reset_inv = true,
  210. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  211. .rst_stat_bit = EXYNOS850_CLUSTER1_WDTRESET_BIT,
  212. .cnt_en_reg = EXYNOS850_CLUSTER1_NONCPU_OUT,
  213. .cnt_en_bit = 7,
  214. .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET | \
  215. QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
  216. };
  217. static const struct s3c2410_wdt_variant drv_data_exynosautov9_cl0 = {
  218. .mask_reset_reg = EXYNOS850_CLUSTER0_NONCPU_INT_EN,
  219. .mask_bit = 2,
  220. .mask_reset_inv = true,
  221. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  222. .rst_stat_bit = EXYNOSAUTOV9_CLUSTER0_WDTRESET_BIT,
  223. .cnt_en_reg = EXYNOS850_CLUSTER0_NONCPU_OUT,
  224. .cnt_en_bit = 7,
  225. .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET |
  226. QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
  227. };
  228. static const struct s3c2410_wdt_variant drv_data_exynosautov9_cl1 = {
  229. .mask_reset_reg = EXYNOSAUTOV9_CLUSTER1_NONCPU_INT_EN,
  230. .mask_bit = 2,
  231. .mask_reset_inv = true,
  232. .rst_stat_reg = EXYNOS5_RST_STAT_REG_OFFSET,
  233. .rst_stat_bit = EXYNOSAUTOV9_CLUSTER1_WDTRESET_BIT,
  234. .cnt_en_reg = EXYNOSAUTOV9_CLUSTER1_NONCPU_OUT,
  235. .cnt_en_bit = 7,
  236. .quirks = QUIRK_HAS_WTCLRINT_REG | QUIRK_HAS_PMU_MASK_RESET |
  237. QUIRK_HAS_PMU_RST_STAT | QUIRK_HAS_PMU_CNT_EN,
  238. };
  239. static const struct of_device_id s3c2410_wdt_match[] = {
  240. { .compatible = "samsung,s3c2410-wdt",
  241. .data = &drv_data_s3c2410 },
  242. { .compatible = "samsung,s3c6410-wdt",
  243. .data = &drv_data_s3c6410 },
  244. { .compatible = "samsung,exynos5250-wdt",
  245. .data = &drv_data_exynos5250 },
  246. { .compatible = "samsung,exynos5420-wdt",
  247. .data = &drv_data_exynos5420 },
  248. { .compatible = "samsung,exynos7-wdt",
  249. .data = &drv_data_exynos7 },
  250. { .compatible = "samsung,exynos850-wdt",
  251. .data = &drv_data_exynos850_cl0 },
  252. { .compatible = "samsung,exynosautov9-wdt",
  253. .data = &drv_data_exynosautov9_cl0 },
  254. {},
  255. };
  256. MODULE_DEVICE_TABLE(of, s3c2410_wdt_match);
  257. #endif
  258. static const struct platform_device_id s3c2410_wdt_ids[] = {
  259. {
  260. .name = "s3c2410-wdt",
  261. .driver_data = (unsigned long)&drv_data_s3c2410,
  262. },
  263. {}
  264. };
  265. MODULE_DEVICE_TABLE(platform, s3c2410_wdt_ids);
  266. /* functions */
  267. static inline unsigned long s3c2410wdt_get_freq(struct s3c2410_wdt *wdt)
  268. {
  269. return clk_get_rate(wdt->src_clk ? wdt->src_clk : wdt->bus_clk);
  270. }
  271. static inline unsigned int s3c2410wdt_max_timeout(struct s3c2410_wdt *wdt)
  272. {
  273. const unsigned long freq = s3c2410wdt_get_freq(wdt);
  274. return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
  275. / S3C2410_WTCON_MAXDIV);
  276. }
  277. static inline struct s3c2410_wdt *freq_to_wdt(struct notifier_block *nb)
  278. {
  279. return container_of(nb, struct s3c2410_wdt, freq_transition);
  280. }
  281. static int s3c2410wdt_disable_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
  282. {
  283. const u32 mask_val = BIT(wdt->drv_data->mask_bit);
  284. const u32 val = mask ? mask_val : 0;
  285. int ret;
  286. ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->disable_reg,
  287. mask_val, val);
  288. if (ret < 0)
  289. dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
  290. return ret;
  291. }
  292. static int s3c2410wdt_mask_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
  293. {
  294. const u32 mask_val = BIT(wdt->drv_data->mask_bit);
  295. const bool val_inv = wdt->drv_data->mask_reset_inv;
  296. const u32 val = (mask ^ val_inv) ? mask_val : 0;
  297. int ret;
  298. ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->mask_reset_reg,
  299. mask_val, val);
  300. if (ret < 0)
  301. dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
  302. return ret;
  303. }
  304. static int s3c2410wdt_enable_counter(struct s3c2410_wdt *wdt, bool en)
  305. {
  306. const u32 mask_val = BIT(wdt->drv_data->cnt_en_bit);
  307. const u32 val = en ? mask_val : 0;
  308. int ret;
  309. ret = regmap_update_bits(wdt->pmureg, wdt->drv_data->cnt_en_reg,
  310. mask_val, val);
  311. if (ret < 0)
  312. dev_err(wdt->dev, "failed to update reg(%d)\n", ret);
  313. return ret;
  314. }
  315. static int s3c2410wdt_enable(struct s3c2410_wdt *wdt, bool en)
  316. {
  317. int ret;
  318. if (wdt->drv_data->quirks & QUIRK_HAS_PMU_AUTO_DISABLE) {
  319. ret = s3c2410wdt_disable_wdt_reset(wdt, !en);
  320. if (ret < 0)
  321. return ret;
  322. }
  323. if (wdt->drv_data->quirks & QUIRK_HAS_PMU_MASK_RESET) {
  324. ret = s3c2410wdt_mask_wdt_reset(wdt, !en);
  325. if (ret < 0)
  326. return ret;
  327. }
  328. if (wdt->drv_data->quirks & QUIRK_HAS_PMU_CNT_EN) {
  329. ret = s3c2410wdt_enable_counter(wdt, en);
  330. if (ret < 0)
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. static int s3c2410wdt_keepalive(struct watchdog_device *wdd)
  336. {
  337. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  338. spin_lock(&wdt->lock);
  339. writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
  340. spin_unlock(&wdt->lock);
  341. return 0;
  342. }
  343. static void __s3c2410wdt_stop(struct s3c2410_wdt *wdt)
  344. {
  345. unsigned long wtcon;
  346. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  347. wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
  348. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  349. }
  350. static int s3c2410wdt_stop(struct watchdog_device *wdd)
  351. {
  352. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  353. spin_lock(&wdt->lock);
  354. __s3c2410wdt_stop(wdt);
  355. spin_unlock(&wdt->lock);
  356. return 0;
  357. }
  358. static int s3c2410wdt_start(struct watchdog_device *wdd)
  359. {
  360. unsigned long wtcon;
  361. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  362. spin_lock(&wdt->lock);
  363. __s3c2410wdt_stop(wdt);
  364. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  365. wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;
  366. if (soft_noboot) {
  367. wtcon |= S3C2410_WTCON_INTEN;
  368. wtcon &= ~S3C2410_WTCON_RSTEN;
  369. } else {
  370. wtcon &= ~S3C2410_WTCON_INTEN;
  371. wtcon |= S3C2410_WTCON_RSTEN;
  372. }
  373. dev_dbg(wdt->dev, "Starting watchdog: count=0x%08x, wtcon=%08lx\n",
  374. wdt->count, wtcon);
  375. writel(wdt->count, wdt->reg_base + S3C2410_WTDAT);
  376. writel(wdt->count, wdt->reg_base + S3C2410_WTCNT);
  377. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  378. spin_unlock(&wdt->lock);
  379. return 0;
  380. }
  381. static inline int s3c2410wdt_is_running(struct s3c2410_wdt *wdt)
  382. {
  383. return readl(wdt->reg_base + S3C2410_WTCON) & S3C2410_WTCON_ENABLE;
  384. }
  385. static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd,
  386. unsigned int timeout)
  387. {
  388. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  389. unsigned long freq = s3c2410wdt_get_freq(wdt);
  390. unsigned int count;
  391. unsigned int divisor = 1;
  392. unsigned long wtcon;
  393. if (timeout < 1)
  394. return -EINVAL;
  395. freq = DIV_ROUND_UP(freq, 128);
  396. count = timeout * freq;
  397. dev_dbg(wdt->dev, "Heartbeat: count=%d, timeout=%d, freq=%lu\n",
  398. count, timeout, freq);
  399. /* if the count is bigger than the watchdog register,
  400. then work out what we need to do (and if) we can
  401. actually make this value
  402. */
  403. if (count >= 0x10000) {
  404. divisor = DIV_ROUND_UP(count, 0xffff);
  405. if (divisor > 0x100) {
  406. dev_err(wdt->dev, "timeout %d too big\n", timeout);
  407. return -EINVAL;
  408. }
  409. }
  410. dev_dbg(wdt->dev, "Heartbeat: timeout=%d, divisor=%d, count=%d (%08x)\n",
  411. timeout, divisor, count, DIV_ROUND_UP(count, divisor));
  412. count = DIV_ROUND_UP(count, divisor);
  413. wdt->count = count;
  414. /* update the pre-scaler */
  415. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  416. wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
  417. wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);
  418. writel(count, wdt->reg_base + S3C2410_WTDAT);
  419. writel(wtcon, wdt->reg_base + S3C2410_WTCON);
  420. wdd->timeout = (count * divisor) / freq;
  421. return 0;
  422. }
  423. static int s3c2410wdt_restart(struct watchdog_device *wdd, unsigned long action,
  424. void *data)
  425. {
  426. struct s3c2410_wdt *wdt = watchdog_get_drvdata(wdd);
  427. void __iomem *wdt_base = wdt->reg_base;
  428. /* disable watchdog, to be safe */
  429. writel(0, wdt_base + S3C2410_WTCON);
  430. /* put initial values into count and data */
  431. writel(0x80, wdt_base + S3C2410_WTCNT);
  432. writel(0x80, wdt_base + S3C2410_WTDAT);
  433. /* set the watchdog to go and reset... */
  434. writel(S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV16 |
  435. S3C2410_WTCON_RSTEN | S3C2410_WTCON_PRESCALE(0x20),
  436. wdt_base + S3C2410_WTCON);
  437. /* wait for reset to assert... */
  438. mdelay(500);
  439. return 0;
  440. }
  441. #define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
  442. static const struct watchdog_info s3c2410_wdt_ident = {
  443. .options = OPTIONS,
  444. .firmware_version = 0,
  445. .identity = "S3C2410 Watchdog",
  446. };
  447. static const struct watchdog_ops s3c2410wdt_ops = {
  448. .owner = THIS_MODULE,
  449. .start = s3c2410wdt_start,
  450. .stop = s3c2410wdt_stop,
  451. .ping = s3c2410wdt_keepalive,
  452. .set_timeout = s3c2410wdt_set_heartbeat,
  453. .restart = s3c2410wdt_restart,
  454. };
  455. static const struct watchdog_device s3c2410_wdd = {
  456. .info = &s3c2410_wdt_ident,
  457. .ops = &s3c2410wdt_ops,
  458. .timeout = S3C2410_WATCHDOG_DEFAULT_TIME,
  459. };
  460. /* interrupt handler code */
  461. static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
  462. {
  463. struct s3c2410_wdt *wdt = platform_get_drvdata(param);
  464. dev_info(wdt->dev, "watchdog timer expired (irq)\n");
  465. s3c2410wdt_keepalive(&wdt->wdt_device);
  466. if (wdt->drv_data->quirks & QUIRK_HAS_WTCLRINT_REG)
  467. writel(0x1, wdt->reg_base + S3C2410_WTCLRINT);
  468. return IRQ_HANDLED;
  469. }
  470. #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
  471. static int s3c2410wdt_cpufreq_transition(struct notifier_block *nb,
  472. unsigned long val, void *data)
  473. {
  474. int ret;
  475. struct s3c2410_wdt *wdt = freq_to_wdt(nb);
  476. if (!s3c2410wdt_is_running(wdt))
  477. goto done;
  478. if (val == CPUFREQ_PRECHANGE) {
  479. /* To ensure that over the change we don't cause the
  480. * watchdog to trigger, we perform an keep-alive if
  481. * the watchdog is running.
  482. */
  483. s3c2410wdt_keepalive(&wdt->wdt_device);
  484. } else if (val == CPUFREQ_POSTCHANGE) {
  485. s3c2410wdt_stop(&wdt->wdt_device);
  486. ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  487. wdt->wdt_device.timeout);
  488. if (ret >= 0)
  489. s3c2410wdt_start(&wdt->wdt_device);
  490. else
  491. goto err;
  492. }
  493. done:
  494. return 0;
  495. err:
  496. dev_err(wdt->dev, "cannot set new value for timeout %d\n",
  497. wdt->wdt_device.timeout);
  498. return ret;
  499. }
  500. static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
  501. {
  502. wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
  503. return cpufreq_register_notifier(&wdt->freq_transition,
  504. CPUFREQ_TRANSITION_NOTIFIER);
  505. }
  506. static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
  507. {
  508. wdt->freq_transition.notifier_call = s3c2410wdt_cpufreq_transition;
  509. cpufreq_unregister_notifier(&wdt->freq_transition,
  510. CPUFREQ_TRANSITION_NOTIFIER);
  511. }
  512. #else
  513. static inline int s3c2410wdt_cpufreq_register(struct s3c2410_wdt *wdt)
  514. {
  515. return 0;
  516. }
  517. static inline void s3c2410wdt_cpufreq_deregister(struct s3c2410_wdt *wdt)
  518. {
  519. }
  520. #endif
  521. static inline unsigned int s3c2410wdt_get_bootstatus(struct s3c2410_wdt *wdt)
  522. {
  523. unsigned int rst_stat;
  524. int ret;
  525. if (!(wdt->drv_data->quirks & QUIRK_HAS_PMU_RST_STAT))
  526. return 0;
  527. ret = regmap_read(wdt->pmureg, wdt->drv_data->rst_stat_reg, &rst_stat);
  528. if (ret)
  529. dev_warn(wdt->dev, "Couldn't get RST_STAT register\n");
  530. else if (rst_stat & BIT(wdt->drv_data->rst_stat_bit))
  531. return WDIOF_CARDRESET;
  532. return 0;
  533. }
  534. static inline const struct s3c2410_wdt_variant *
  535. s3c2410_get_wdt_drv_data(struct platform_device *pdev)
  536. {
  537. const struct s3c2410_wdt_variant *variant;
  538. struct device *dev = &pdev->dev;
  539. variant = of_device_get_match_data(dev);
  540. if (!variant) {
  541. /* Device matched by platform_device_id */
  542. variant = (struct s3c2410_wdt_variant *)
  543. platform_get_device_id(pdev)->driver_data;
  544. }
  545. #ifdef CONFIG_OF
  546. /* Choose Exynos850/ExynosAutov9 driver data w.r.t. cluster index */
  547. if (variant == &drv_data_exynos850_cl0 ||
  548. variant == &drv_data_exynosautov9_cl0) {
  549. u32 index;
  550. int err;
  551. err = of_property_read_u32(dev->of_node,
  552. "samsung,cluster-index", &index);
  553. if (err) {
  554. dev_err(dev, "failed to get cluster index\n");
  555. return NULL;
  556. }
  557. switch (index) {
  558. case 0:
  559. return variant;
  560. case 1:
  561. return (variant == &drv_data_exynos850_cl0) ?
  562. &drv_data_exynos850_cl1 :
  563. &drv_data_exynosautov9_cl1;
  564. default:
  565. dev_err(dev, "wrong cluster index: %u\n", index);
  566. return NULL;
  567. }
  568. }
  569. #endif
  570. return variant;
  571. }
  572. static int s3c2410wdt_probe(struct platform_device *pdev)
  573. {
  574. struct device *dev = &pdev->dev;
  575. struct s3c2410_wdt *wdt;
  576. unsigned int wtcon;
  577. int wdt_irq;
  578. int ret;
  579. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  580. if (!wdt)
  581. return -ENOMEM;
  582. wdt->dev = dev;
  583. spin_lock_init(&wdt->lock);
  584. wdt->wdt_device = s3c2410_wdd;
  585. wdt->drv_data = s3c2410_get_wdt_drv_data(pdev);
  586. if (!wdt->drv_data)
  587. return -EINVAL;
  588. if (wdt->drv_data->quirks & QUIRKS_HAVE_PMUREG) {
  589. wdt->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
  590. "samsung,syscon-phandle");
  591. if (IS_ERR(wdt->pmureg)) {
  592. dev_err(dev, "syscon regmap lookup failed.\n");
  593. return PTR_ERR(wdt->pmureg);
  594. }
  595. }
  596. wdt_irq = platform_get_irq(pdev, 0);
  597. if (wdt_irq < 0)
  598. return wdt_irq;
  599. /* get the memory region for the watchdog timer */
  600. wdt->reg_base = devm_platform_ioremap_resource(pdev, 0);
  601. if (IS_ERR(wdt->reg_base))
  602. return PTR_ERR(wdt->reg_base);
  603. wdt->bus_clk = devm_clk_get(dev, "watchdog");
  604. if (IS_ERR(wdt->bus_clk)) {
  605. dev_err(dev, "failed to find bus clock\n");
  606. return PTR_ERR(wdt->bus_clk);
  607. }
  608. ret = clk_prepare_enable(wdt->bus_clk);
  609. if (ret < 0) {
  610. dev_err(dev, "failed to enable bus clock\n");
  611. return ret;
  612. }
  613. /*
  614. * "watchdog_src" clock is optional; if it's not present -- just skip it
  615. * and use "watchdog" clock as both bus and source clock.
  616. */
  617. wdt->src_clk = devm_clk_get_optional(dev, "watchdog_src");
  618. if (IS_ERR(wdt->src_clk)) {
  619. dev_err_probe(dev, PTR_ERR(wdt->src_clk),
  620. "failed to get source clock\n");
  621. ret = PTR_ERR(wdt->src_clk);
  622. goto err_bus_clk;
  623. }
  624. ret = clk_prepare_enable(wdt->src_clk);
  625. if (ret) {
  626. dev_err(dev, "failed to enable source clock\n");
  627. goto err_bus_clk;
  628. }
  629. wdt->wdt_device.min_timeout = 1;
  630. wdt->wdt_device.max_timeout = s3c2410wdt_max_timeout(wdt);
  631. ret = s3c2410wdt_cpufreq_register(wdt);
  632. if (ret < 0) {
  633. dev_err(dev, "failed to register cpufreq\n");
  634. goto err_src_clk;
  635. }
  636. watchdog_set_drvdata(&wdt->wdt_device, wdt);
  637. /* see if we can actually set the requested timer margin, and if
  638. * not, try the default value */
  639. watchdog_init_timeout(&wdt->wdt_device, tmr_margin, dev);
  640. ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  641. wdt->wdt_device.timeout);
  642. if (ret) {
  643. ret = s3c2410wdt_set_heartbeat(&wdt->wdt_device,
  644. S3C2410_WATCHDOG_DEFAULT_TIME);
  645. if (ret == 0) {
  646. dev_warn(dev, "tmr_margin value out of range, default %d used\n",
  647. S3C2410_WATCHDOG_DEFAULT_TIME);
  648. } else {
  649. dev_err(dev, "failed to use default timeout\n");
  650. goto err_cpufreq;
  651. }
  652. }
  653. ret = devm_request_irq(dev, wdt_irq, s3c2410wdt_irq, 0,
  654. pdev->name, pdev);
  655. if (ret != 0) {
  656. dev_err(dev, "failed to install irq (%d)\n", ret);
  657. goto err_cpufreq;
  658. }
  659. watchdog_set_nowayout(&wdt->wdt_device, nowayout);
  660. watchdog_set_restart_priority(&wdt->wdt_device, 128);
  661. wdt->wdt_device.bootstatus = s3c2410wdt_get_bootstatus(wdt);
  662. wdt->wdt_device.parent = dev;
  663. /*
  664. * If "tmr_atboot" param is non-zero, start the watchdog right now. Also
  665. * set WDOG_HW_RUNNING bit, so that watchdog core can kick the watchdog.
  666. *
  667. * If we're not enabling the watchdog, then ensure it is disabled if it
  668. * has been left running from the bootloader or other source.
  669. */
  670. if (tmr_atboot) {
  671. dev_info(dev, "starting watchdog timer\n");
  672. s3c2410wdt_start(&wdt->wdt_device);
  673. set_bit(WDOG_HW_RUNNING, &wdt->wdt_device.status);
  674. } else {
  675. s3c2410wdt_stop(&wdt->wdt_device);
  676. }
  677. ret = watchdog_register_device(&wdt->wdt_device);
  678. if (ret)
  679. goto err_cpufreq;
  680. ret = s3c2410wdt_enable(wdt, true);
  681. if (ret < 0)
  682. goto err_unregister;
  683. platform_set_drvdata(pdev, wdt);
  684. /* print out a statement of readiness */
  685. wtcon = readl(wdt->reg_base + S3C2410_WTCON);
  686. dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabled\n",
  687. (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
  688. (wtcon & S3C2410_WTCON_RSTEN) ? "en" : "dis",
  689. (wtcon & S3C2410_WTCON_INTEN) ? "en" : "dis");
  690. return 0;
  691. err_unregister:
  692. watchdog_unregister_device(&wdt->wdt_device);
  693. err_cpufreq:
  694. s3c2410wdt_cpufreq_deregister(wdt);
  695. err_src_clk:
  696. clk_disable_unprepare(wdt->src_clk);
  697. err_bus_clk:
  698. clk_disable_unprepare(wdt->bus_clk);
  699. return ret;
  700. }
  701. static int s3c2410wdt_remove(struct platform_device *dev)
  702. {
  703. int ret;
  704. struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
  705. ret = s3c2410wdt_enable(wdt, false);
  706. if (ret < 0)
  707. return ret;
  708. watchdog_unregister_device(&wdt->wdt_device);
  709. s3c2410wdt_cpufreq_deregister(wdt);
  710. clk_disable_unprepare(wdt->src_clk);
  711. clk_disable_unprepare(wdt->bus_clk);
  712. return 0;
  713. }
  714. static void s3c2410wdt_shutdown(struct platform_device *dev)
  715. {
  716. struct s3c2410_wdt *wdt = platform_get_drvdata(dev);
  717. s3c2410wdt_enable(wdt, false);
  718. s3c2410wdt_stop(&wdt->wdt_device);
  719. }
  720. static int s3c2410wdt_suspend(struct device *dev)
  721. {
  722. int ret;
  723. struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
  724. /* Save watchdog state, and turn it off. */
  725. wdt->wtcon_save = readl(wdt->reg_base + S3C2410_WTCON);
  726. wdt->wtdat_save = readl(wdt->reg_base + S3C2410_WTDAT);
  727. ret = s3c2410wdt_enable(wdt, false);
  728. if (ret < 0)
  729. return ret;
  730. /* Note that WTCNT doesn't need to be saved. */
  731. s3c2410wdt_stop(&wdt->wdt_device);
  732. return 0;
  733. }
  734. static int s3c2410wdt_resume(struct device *dev)
  735. {
  736. int ret;
  737. struct s3c2410_wdt *wdt = dev_get_drvdata(dev);
  738. /* Restore watchdog state. */
  739. writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTDAT);
  740. writel(wdt->wtdat_save, wdt->reg_base + S3C2410_WTCNT);/* Reset count */
  741. writel(wdt->wtcon_save, wdt->reg_base + S3C2410_WTCON);
  742. ret = s3c2410wdt_enable(wdt, true);
  743. if (ret < 0)
  744. return ret;
  745. dev_info(dev, "watchdog %sabled\n",
  746. (wdt->wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");
  747. return 0;
  748. }
  749. static DEFINE_SIMPLE_DEV_PM_OPS(s3c2410wdt_pm_ops,
  750. s3c2410wdt_suspend, s3c2410wdt_resume);
  751. static struct platform_driver s3c2410wdt_driver = {
  752. .probe = s3c2410wdt_probe,
  753. .remove = s3c2410wdt_remove,
  754. .shutdown = s3c2410wdt_shutdown,
  755. .id_table = s3c2410_wdt_ids,
  756. .driver = {
  757. .name = "s3c2410-wdt",
  758. .pm = pm_sleep_ptr(&s3c2410wdt_pm_ops),
  759. .of_match_table = of_match_ptr(s3c2410_wdt_match),
  760. },
  761. };
  762. module_platform_driver(s3c2410wdt_driver);
  763. MODULE_AUTHOR("Ben Dooks <[email protected]>, Dimitry Andric <[email protected]>");
  764. MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
  765. MODULE_LICENSE("GPL");