bcm_kona_wdt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (C) 2013 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/debugfs.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of_address.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/watchdog.h>
  21. #define SECWDOG_CTRL_REG 0x00000000
  22. #define SECWDOG_COUNT_REG 0x00000004
  23. #define SECWDOG_RESERVED_MASK 0x1dffffff
  24. #define SECWDOG_WD_LOAD_FLAG 0x10000000
  25. #define SECWDOG_EN_MASK 0x08000000
  26. #define SECWDOG_SRSTEN_MASK 0x04000000
  27. #define SECWDOG_RES_MASK 0x00f00000
  28. #define SECWDOG_COUNT_MASK 0x000fffff
  29. #define SECWDOG_MAX_COUNT SECWDOG_COUNT_MASK
  30. #define SECWDOG_CLKS_SHIFT 20
  31. #define SECWDOG_MAX_RES 15
  32. #define SECWDOG_DEFAULT_RESOLUTION 4
  33. #define SECWDOG_MAX_TRY 1000
  34. #define SECS_TO_TICKS(x, w) ((x) << (w)->resolution)
  35. #define TICKS_TO_SECS(x, w) ((x) >> (w)->resolution)
  36. #define BCM_KONA_WDT_NAME "bcm_kona_wdt"
  37. struct bcm_kona_wdt {
  38. void __iomem *base;
  39. /*
  40. * One watchdog tick is 1/(2^resolution) seconds. Resolution can take
  41. * the values 0-15, meaning one tick can be 1s to 30.52us. Our default
  42. * resolution of 4 means one tick is 62.5ms.
  43. *
  44. * The watchdog counter is 20 bits. Depending on resolution, the maximum
  45. * counter value of 0xfffff expires after about 12 days (resolution 0)
  46. * down to only 32s (resolution 15). The default resolution of 4 gives
  47. * us a maximum of about 18 hours and 12 minutes before the watchdog
  48. * times out.
  49. */
  50. int resolution;
  51. spinlock_t lock;
  52. #ifdef CONFIG_BCM_KONA_WDT_DEBUG
  53. unsigned long busy_count;
  54. struct dentry *debugfs;
  55. #endif
  56. };
  57. static int secure_register_read(struct bcm_kona_wdt *wdt, uint32_t offset)
  58. {
  59. uint32_t val;
  60. unsigned count = 0;
  61. /*
  62. * If the WD_LOAD_FLAG is set, the watchdog counter field is being
  63. * updated in hardware. Once the WD timer is updated in hardware, it
  64. * gets cleared.
  65. */
  66. do {
  67. if (unlikely(count > 1))
  68. udelay(5);
  69. val = readl_relaxed(wdt->base + offset);
  70. count++;
  71. } while ((val & SECWDOG_WD_LOAD_FLAG) && count < SECWDOG_MAX_TRY);
  72. #ifdef CONFIG_BCM_KONA_WDT_DEBUG
  73. /* Remember the maximum number iterations due to WD_LOAD_FLAG */
  74. if (count > wdt->busy_count)
  75. wdt->busy_count = count;
  76. #endif
  77. /* This is the only place we return a negative value. */
  78. if (val & SECWDOG_WD_LOAD_FLAG)
  79. return -ETIMEDOUT;
  80. /* We always mask out reserved bits. */
  81. val &= SECWDOG_RESERVED_MASK;
  82. return val;
  83. }
  84. #ifdef CONFIG_BCM_KONA_WDT_DEBUG
  85. static int bcm_kona_wdt_dbg_show(struct seq_file *s, void *data)
  86. {
  87. int ctl_val, cur_val, ret;
  88. unsigned long flags;
  89. struct bcm_kona_wdt *wdt = s->private;
  90. if (!wdt)
  91. return seq_puts(s, "No device pointer\n");
  92. spin_lock_irqsave(&wdt->lock, flags);
  93. ctl_val = secure_register_read(wdt, SECWDOG_CTRL_REG);
  94. cur_val = secure_register_read(wdt, SECWDOG_COUNT_REG);
  95. spin_unlock_irqrestore(&wdt->lock, flags);
  96. if (ctl_val < 0 || cur_val < 0) {
  97. ret = seq_puts(s, "Error accessing hardware\n");
  98. } else {
  99. int ctl, cur, ctl_sec, cur_sec, res;
  100. ctl = ctl_val & SECWDOG_COUNT_MASK;
  101. res = (ctl_val & SECWDOG_RES_MASK) >> SECWDOG_CLKS_SHIFT;
  102. cur = cur_val & SECWDOG_COUNT_MASK;
  103. ctl_sec = TICKS_TO_SECS(ctl, wdt);
  104. cur_sec = TICKS_TO_SECS(cur, wdt);
  105. ret = seq_printf(s, "Resolution: %d / %d\n"
  106. "Control: %d s / %d (%#x) ticks\n"
  107. "Current: %d s / %d (%#x) ticks\n"
  108. "Busy count: %lu\n", res,
  109. wdt->resolution, ctl_sec, ctl, ctl, cur_sec,
  110. cur, cur, wdt->busy_count);
  111. }
  112. return ret;
  113. }
  114. static int bcm_kona_dbg_open(struct inode *inode, struct file *file)
  115. {
  116. return single_open(file, bcm_kona_wdt_dbg_show, inode->i_private);
  117. }
  118. static const struct file_operations bcm_kona_dbg_operations = {
  119. .open = bcm_kona_dbg_open,
  120. .read = seq_read,
  121. .llseek = seq_lseek,
  122. .release = single_release,
  123. };
  124. static void bcm_kona_wdt_debug_init(struct platform_device *pdev)
  125. {
  126. struct dentry *dir;
  127. struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
  128. if (!wdt)
  129. return;
  130. wdt->debugfs = NULL;
  131. dir = debugfs_create_dir(BCM_KONA_WDT_NAME, NULL);
  132. if (IS_ERR_OR_NULL(dir))
  133. return;
  134. if (debugfs_create_file("info", S_IFREG | S_IRUGO, dir, wdt,
  135. &bcm_kona_dbg_operations))
  136. wdt->debugfs = dir;
  137. else
  138. debugfs_remove_recursive(dir);
  139. }
  140. static void bcm_kona_wdt_debug_exit(struct platform_device *pdev)
  141. {
  142. struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
  143. if (wdt && wdt->debugfs) {
  144. debugfs_remove_recursive(wdt->debugfs);
  145. wdt->debugfs = NULL;
  146. }
  147. }
  148. #else
  149. static void bcm_kona_wdt_debug_init(struct platform_device *pdev) {}
  150. static void bcm_kona_wdt_debug_exit(struct platform_device *pdev) {}
  151. #endif /* CONFIG_BCM_KONA_WDT_DEBUG */
  152. static int bcm_kona_wdt_ctrl_reg_modify(struct bcm_kona_wdt *wdt,
  153. unsigned mask, unsigned newval)
  154. {
  155. int val;
  156. unsigned long flags;
  157. int ret = 0;
  158. spin_lock_irqsave(&wdt->lock, flags);
  159. val = secure_register_read(wdt, SECWDOG_CTRL_REG);
  160. if (val < 0) {
  161. ret = val;
  162. } else {
  163. val &= ~mask;
  164. val |= newval;
  165. writel_relaxed(val, wdt->base + SECWDOG_CTRL_REG);
  166. }
  167. spin_unlock_irqrestore(&wdt->lock, flags);
  168. return ret;
  169. }
  170. static int bcm_kona_wdt_set_resolution_reg(struct bcm_kona_wdt *wdt)
  171. {
  172. if (wdt->resolution > SECWDOG_MAX_RES)
  173. return -EINVAL;
  174. return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_RES_MASK,
  175. wdt->resolution << SECWDOG_CLKS_SHIFT);
  176. }
  177. static int bcm_kona_wdt_set_timeout_reg(struct watchdog_device *wdog,
  178. unsigned watchdog_flags)
  179. {
  180. struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
  181. return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_COUNT_MASK,
  182. SECS_TO_TICKS(wdog->timeout, wdt) |
  183. watchdog_flags);
  184. }
  185. static int bcm_kona_wdt_set_timeout(struct watchdog_device *wdog,
  186. unsigned int t)
  187. {
  188. wdog->timeout = t;
  189. return 0;
  190. }
  191. static unsigned int bcm_kona_wdt_get_timeleft(struct watchdog_device *wdog)
  192. {
  193. struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
  194. int val;
  195. unsigned long flags;
  196. spin_lock_irqsave(&wdt->lock, flags);
  197. val = secure_register_read(wdt, SECWDOG_COUNT_REG);
  198. spin_unlock_irqrestore(&wdt->lock, flags);
  199. if (val < 0)
  200. return val;
  201. return TICKS_TO_SECS(val & SECWDOG_COUNT_MASK, wdt);
  202. }
  203. static int bcm_kona_wdt_start(struct watchdog_device *wdog)
  204. {
  205. return bcm_kona_wdt_set_timeout_reg(wdog,
  206. SECWDOG_EN_MASK | SECWDOG_SRSTEN_MASK);
  207. }
  208. static int bcm_kona_wdt_stop(struct watchdog_device *wdog)
  209. {
  210. struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
  211. return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_EN_MASK |
  212. SECWDOG_SRSTEN_MASK, 0);
  213. }
  214. static struct watchdog_ops bcm_kona_wdt_ops = {
  215. .owner = THIS_MODULE,
  216. .start = bcm_kona_wdt_start,
  217. .stop = bcm_kona_wdt_stop,
  218. .set_timeout = bcm_kona_wdt_set_timeout,
  219. .get_timeleft = bcm_kona_wdt_get_timeleft,
  220. };
  221. static struct watchdog_info bcm_kona_wdt_info = {
  222. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  223. WDIOF_KEEPALIVEPING,
  224. .identity = "Broadcom Kona Watchdog Timer",
  225. };
  226. static struct watchdog_device bcm_kona_wdt_wdd = {
  227. .info = &bcm_kona_wdt_info,
  228. .ops = &bcm_kona_wdt_ops,
  229. .min_timeout = 1,
  230. .max_timeout = SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
  231. .timeout = SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
  232. };
  233. static void bcm_kona_wdt_shutdown(struct platform_device *pdev)
  234. {
  235. bcm_kona_wdt_stop(&bcm_kona_wdt_wdd);
  236. }
  237. static int bcm_kona_wdt_probe(struct platform_device *pdev)
  238. {
  239. struct device *dev = &pdev->dev;
  240. struct bcm_kona_wdt *wdt;
  241. struct resource *res;
  242. int ret;
  243. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  244. if (!wdt)
  245. return -ENOMEM;
  246. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  247. wdt->base = devm_ioremap_resource(dev, res);
  248. if (IS_ERR(wdt->base))
  249. return -ENODEV;
  250. wdt->resolution = SECWDOG_DEFAULT_RESOLUTION;
  251. ret = bcm_kona_wdt_set_resolution_reg(wdt);
  252. if (ret) {
  253. dev_err(dev, "Failed to set resolution (error: %d)", ret);
  254. return ret;
  255. }
  256. spin_lock_init(&wdt->lock);
  257. platform_set_drvdata(pdev, wdt);
  258. watchdog_set_drvdata(&bcm_kona_wdt_wdd, wdt);
  259. ret = bcm_kona_wdt_set_timeout_reg(&bcm_kona_wdt_wdd, 0);
  260. if (ret) {
  261. dev_err(dev, "Failed set watchdog timeout");
  262. return ret;
  263. }
  264. ret = watchdog_register_device(&bcm_kona_wdt_wdd);
  265. if (ret) {
  266. dev_err(dev, "Failed to register watchdog device");
  267. return ret;
  268. }
  269. bcm_kona_wdt_debug_init(pdev);
  270. dev_dbg(dev, "Broadcom Kona Watchdog Timer");
  271. return 0;
  272. }
  273. static int bcm_kona_wdt_remove(struct platform_device *pdev)
  274. {
  275. bcm_kona_wdt_debug_exit(pdev);
  276. bcm_kona_wdt_shutdown(pdev);
  277. watchdog_unregister_device(&bcm_kona_wdt_wdd);
  278. dev_dbg(&pdev->dev, "Watchdog driver disabled");
  279. return 0;
  280. }
  281. static const struct of_device_id bcm_kona_wdt_of_match[] = {
  282. { .compatible = "brcm,kona-wdt", },
  283. {},
  284. };
  285. MODULE_DEVICE_TABLE(of, bcm_kona_wdt_of_match);
  286. static struct platform_driver bcm_kona_wdt_driver = {
  287. .driver = {
  288. .name = BCM_KONA_WDT_NAME,
  289. .owner = THIS_MODULE,
  290. .of_match_table = bcm_kona_wdt_of_match,
  291. },
  292. .probe = bcm_kona_wdt_probe,
  293. .remove = bcm_kona_wdt_remove,
  294. .shutdown = bcm_kona_wdt_shutdown,
  295. };
  296. module_platform_driver(bcm_kona_wdt_driver);
  297. MODULE_ALIAS("platform:" BCM_KONA_WDT_NAME);
  298. MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
  299. MODULE_DESCRIPTION("Broadcom Kona Watchdog Driver");
  300. MODULE_LICENSE("GPL v2");