bcm_kona_wdt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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;
  88. unsigned long flags;
  89. struct bcm_kona_wdt *wdt = s->private;
  90. if (!wdt) {
  91. seq_puts(s, "No device pointer\n");
  92. return 0;
  93. }
  94. spin_lock_irqsave(&wdt->lock, flags);
  95. ctl_val = secure_register_read(wdt, SECWDOG_CTRL_REG);
  96. cur_val = secure_register_read(wdt, SECWDOG_COUNT_REG);
  97. spin_unlock_irqrestore(&wdt->lock, flags);
  98. if (ctl_val < 0 || cur_val < 0) {
  99. seq_puts(s, "Error accessing hardware\n");
  100. } else {
  101. int ctl, cur, ctl_sec, cur_sec, res;
  102. ctl = ctl_val & SECWDOG_COUNT_MASK;
  103. res = (ctl_val & SECWDOG_RES_MASK) >> SECWDOG_CLKS_SHIFT;
  104. cur = cur_val & SECWDOG_COUNT_MASK;
  105. ctl_sec = TICKS_TO_SECS(ctl, wdt);
  106. cur_sec = TICKS_TO_SECS(cur, wdt);
  107. seq_printf(s,
  108. "Resolution: %d / %d\n"
  109. "Control: %d s / %d (%#x) ticks\n"
  110. "Current: %d s / %d (%#x) ticks\n"
  111. "Busy count: %lu\n",
  112. res, wdt->resolution,
  113. ctl_sec, ctl, ctl,
  114. cur_sec, cur, cur,
  115. wdt->busy_count);
  116. }
  117. return 0;
  118. }
  119. static int bcm_kona_dbg_open(struct inode *inode, struct file *file)
  120. {
  121. return single_open(file, bcm_kona_wdt_dbg_show, inode->i_private);
  122. }
  123. static const struct file_operations bcm_kona_dbg_operations = {
  124. .open = bcm_kona_dbg_open,
  125. .read = seq_read,
  126. .llseek = seq_lseek,
  127. .release = single_release,
  128. };
  129. static void bcm_kona_wdt_debug_init(struct platform_device *pdev)
  130. {
  131. struct dentry *dir;
  132. struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
  133. if (!wdt)
  134. return;
  135. wdt->debugfs = NULL;
  136. dir = debugfs_create_dir(BCM_KONA_WDT_NAME, NULL);
  137. if (IS_ERR_OR_NULL(dir))
  138. return;
  139. if (debugfs_create_file("info", S_IFREG | S_IRUGO, dir, wdt,
  140. &bcm_kona_dbg_operations))
  141. wdt->debugfs = dir;
  142. else
  143. debugfs_remove_recursive(dir);
  144. }
  145. static void bcm_kona_wdt_debug_exit(struct platform_device *pdev)
  146. {
  147. struct bcm_kona_wdt *wdt = platform_get_drvdata(pdev);
  148. if (wdt && wdt->debugfs) {
  149. debugfs_remove_recursive(wdt->debugfs);
  150. wdt->debugfs = NULL;
  151. }
  152. }
  153. #else
  154. static void bcm_kona_wdt_debug_init(struct platform_device *pdev) {}
  155. static void bcm_kona_wdt_debug_exit(struct platform_device *pdev) {}
  156. #endif /* CONFIG_BCM_KONA_WDT_DEBUG */
  157. static int bcm_kona_wdt_ctrl_reg_modify(struct bcm_kona_wdt *wdt,
  158. unsigned mask, unsigned newval)
  159. {
  160. int val;
  161. unsigned long flags;
  162. int ret = 0;
  163. spin_lock_irqsave(&wdt->lock, flags);
  164. val = secure_register_read(wdt, SECWDOG_CTRL_REG);
  165. if (val < 0) {
  166. ret = val;
  167. } else {
  168. val &= ~mask;
  169. val |= newval;
  170. writel_relaxed(val, wdt->base + SECWDOG_CTRL_REG);
  171. }
  172. spin_unlock_irqrestore(&wdt->lock, flags);
  173. return ret;
  174. }
  175. static int bcm_kona_wdt_set_resolution_reg(struct bcm_kona_wdt *wdt)
  176. {
  177. if (wdt->resolution > SECWDOG_MAX_RES)
  178. return -EINVAL;
  179. return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_RES_MASK,
  180. wdt->resolution << SECWDOG_CLKS_SHIFT);
  181. }
  182. static int bcm_kona_wdt_set_timeout_reg(struct watchdog_device *wdog,
  183. unsigned watchdog_flags)
  184. {
  185. struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
  186. return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_COUNT_MASK,
  187. SECS_TO_TICKS(wdog->timeout, wdt) |
  188. watchdog_flags);
  189. }
  190. static int bcm_kona_wdt_set_timeout(struct watchdog_device *wdog,
  191. unsigned int t)
  192. {
  193. wdog->timeout = t;
  194. return 0;
  195. }
  196. static unsigned int bcm_kona_wdt_get_timeleft(struct watchdog_device *wdog)
  197. {
  198. struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
  199. int val;
  200. unsigned long flags;
  201. spin_lock_irqsave(&wdt->lock, flags);
  202. val = secure_register_read(wdt, SECWDOG_COUNT_REG);
  203. spin_unlock_irqrestore(&wdt->lock, flags);
  204. if (val < 0)
  205. return val;
  206. return TICKS_TO_SECS(val & SECWDOG_COUNT_MASK, wdt);
  207. }
  208. static int bcm_kona_wdt_start(struct watchdog_device *wdog)
  209. {
  210. return bcm_kona_wdt_set_timeout_reg(wdog,
  211. SECWDOG_EN_MASK | SECWDOG_SRSTEN_MASK);
  212. }
  213. static int bcm_kona_wdt_stop(struct watchdog_device *wdog)
  214. {
  215. struct bcm_kona_wdt *wdt = watchdog_get_drvdata(wdog);
  216. return bcm_kona_wdt_ctrl_reg_modify(wdt, SECWDOG_EN_MASK |
  217. SECWDOG_SRSTEN_MASK, 0);
  218. }
  219. static struct watchdog_ops bcm_kona_wdt_ops = {
  220. .owner = THIS_MODULE,
  221. .start = bcm_kona_wdt_start,
  222. .stop = bcm_kona_wdt_stop,
  223. .set_timeout = bcm_kona_wdt_set_timeout,
  224. .get_timeleft = bcm_kona_wdt_get_timeleft,
  225. };
  226. static struct watchdog_info bcm_kona_wdt_info = {
  227. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  228. WDIOF_KEEPALIVEPING,
  229. .identity = "Broadcom Kona Watchdog Timer",
  230. };
  231. static struct watchdog_device bcm_kona_wdt_wdd = {
  232. .info = &bcm_kona_wdt_info,
  233. .ops = &bcm_kona_wdt_ops,
  234. .min_timeout = 1,
  235. .max_timeout = SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
  236. .timeout = SECWDOG_MAX_COUNT >> SECWDOG_DEFAULT_RESOLUTION,
  237. };
  238. static void bcm_kona_wdt_shutdown(struct platform_device *pdev)
  239. {
  240. bcm_kona_wdt_stop(&bcm_kona_wdt_wdd);
  241. }
  242. static int bcm_kona_wdt_probe(struct platform_device *pdev)
  243. {
  244. struct device *dev = &pdev->dev;
  245. struct bcm_kona_wdt *wdt;
  246. struct resource *res;
  247. int ret;
  248. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  249. if (!wdt)
  250. return -ENOMEM;
  251. spin_lock_init(&wdt->lock);
  252. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  253. wdt->base = devm_ioremap_resource(dev, res);
  254. if (IS_ERR(wdt->base))
  255. return -ENODEV;
  256. wdt->resolution = SECWDOG_DEFAULT_RESOLUTION;
  257. ret = bcm_kona_wdt_set_resolution_reg(wdt);
  258. if (ret) {
  259. dev_err(dev, "Failed to set resolution (error: %d)", ret);
  260. return ret;
  261. }
  262. platform_set_drvdata(pdev, wdt);
  263. watchdog_set_drvdata(&bcm_kona_wdt_wdd, wdt);
  264. bcm_kona_wdt_wdd.parent = &pdev->dev;
  265. ret = bcm_kona_wdt_set_timeout_reg(&bcm_kona_wdt_wdd, 0);
  266. if (ret) {
  267. dev_err(dev, "Failed set watchdog timeout");
  268. return ret;
  269. }
  270. ret = watchdog_register_device(&bcm_kona_wdt_wdd);
  271. if (ret) {
  272. dev_err(dev, "Failed to register watchdog device");
  273. return ret;
  274. }
  275. bcm_kona_wdt_debug_init(pdev);
  276. dev_dbg(dev, "Broadcom Kona Watchdog Timer");
  277. return 0;
  278. }
  279. static int bcm_kona_wdt_remove(struct platform_device *pdev)
  280. {
  281. bcm_kona_wdt_debug_exit(pdev);
  282. bcm_kona_wdt_shutdown(pdev);
  283. watchdog_unregister_device(&bcm_kona_wdt_wdd);
  284. dev_dbg(&pdev->dev, "Watchdog driver disabled");
  285. return 0;
  286. }
  287. static const struct of_device_id bcm_kona_wdt_of_match[] = {
  288. { .compatible = "brcm,kona-wdt", },
  289. {},
  290. };
  291. MODULE_DEVICE_TABLE(of, bcm_kona_wdt_of_match);
  292. static struct platform_driver bcm_kona_wdt_driver = {
  293. .driver = {
  294. .name = BCM_KONA_WDT_NAME,
  295. .of_match_table = bcm_kona_wdt_of_match,
  296. },
  297. .probe = bcm_kona_wdt_probe,
  298. .remove = bcm_kona_wdt_remove,
  299. .shutdown = bcm_kona_wdt_shutdown,
  300. };
  301. module_platform_driver(bcm_kona_wdt_driver);
  302. MODULE_ALIAS("platform:" BCM_KONA_WDT_NAME);
  303. MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
  304. MODULE_DESCRIPTION("Broadcom Kona Watchdog Driver");
  305. MODULE_LICENSE("GPL v2");