bcm2835_wdt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Watchdog driver for Broadcom BCM2835
  3. *
  4. * "bcm2708_wdog" driver written by Luke Diamand that was obtained from
  5. * branch "rpi-3.6.y" of git://github.com/raspberrypi/linux.git was used
  6. * as a hardware reference for the Broadcom BCM2835 watchdog timer.
  7. *
  8. * Copyright (C) 2013 Lubomir Rintel <lkundrak@v3.sk>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/reboot.h>
  17. #include <linux/types.h>
  18. #include <linux/module.h>
  19. #include <linux/io.h>
  20. #include <linux/watchdog.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_platform.h>
  24. #define PM_RSTC 0x1c
  25. #define PM_RSTS 0x20
  26. #define PM_WDOG 0x24
  27. #define PM_PASSWORD 0x5a000000
  28. #define PM_WDOG_TIME_SET 0x000fffff
  29. #define PM_RSTC_WRCFG_CLR 0xffffffcf
  30. #define PM_RSTS_HADWRH_SET 0x00000040
  31. #define PM_RSTC_WRCFG_SET 0x00000030
  32. #define PM_RSTC_WRCFG_FULL_RESET 0x00000020
  33. #define PM_RSTC_RESET 0x00000102
  34. /*
  35. * The Raspberry Pi firmware uses the RSTS register to know which partiton
  36. * to boot from. The partiton value is spread into bits 0, 2, 4, 6, 8, 10.
  37. * Partiton 63 is a special partition used by the firmware to indicate halt.
  38. */
  39. #define PM_RSTS_RASPBERRYPI_HALT 0x555
  40. #define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  41. #define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  42. struct bcm2835_wdt {
  43. void __iomem *base;
  44. spinlock_t lock;
  45. struct notifier_block restart_handler;
  46. };
  47. static unsigned int heartbeat;
  48. static bool nowayout = WATCHDOG_NOWAYOUT;
  49. static bool bcm2835_wdt_is_running(struct bcm2835_wdt *wdt)
  50. {
  51. uint32_t cur;
  52. cur = readl(wdt->base + PM_RSTC);
  53. return !!(cur & PM_RSTC_WRCFG_FULL_RESET);
  54. }
  55. static int bcm2835_wdt_start(struct watchdog_device *wdog)
  56. {
  57. struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
  58. uint32_t cur;
  59. unsigned long flags;
  60. spin_lock_irqsave(&wdt->lock, flags);
  61. writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
  62. PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
  63. cur = readl_relaxed(wdt->base + PM_RSTC);
  64. writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  65. PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
  66. spin_unlock_irqrestore(&wdt->lock, flags);
  67. return 0;
  68. }
  69. static int bcm2835_wdt_stop(struct watchdog_device *wdog)
  70. {
  71. struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
  72. writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
  73. return 0;
  74. }
  75. static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog)
  76. {
  77. struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
  78. uint32_t ret = readl_relaxed(wdt->base + PM_WDOG);
  79. return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
  80. }
  81. static const struct watchdog_ops bcm2835_wdt_ops = {
  82. .owner = THIS_MODULE,
  83. .start = bcm2835_wdt_start,
  84. .stop = bcm2835_wdt_stop,
  85. .get_timeleft = bcm2835_wdt_get_timeleft,
  86. };
  87. static const struct watchdog_info bcm2835_wdt_info = {
  88. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  89. WDIOF_KEEPALIVEPING,
  90. .identity = "Broadcom BCM2835 Watchdog timer",
  91. };
  92. static struct watchdog_device bcm2835_wdt_wdd = {
  93. .info = &bcm2835_wdt_info,
  94. .ops = &bcm2835_wdt_ops,
  95. .min_timeout = 1,
  96. .max_timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  97. .timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  98. };
  99. static int
  100. bcm2835_restart(struct notifier_block *this, unsigned long mode, void *cmd)
  101. {
  102. struct bcm2835_wdt *wdt = container_of(this, struct bcm2835_wdt,
  103. restart_handler);
  104. u32 val;
  105. /* use a timeout of 10 ticks (~150us) */
  106. writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
  107. val = readl_relaxed(wdt->base + PM_RSTC);
  108. val &= PM_RSTC_WRCFG_CLR;
  109. val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
  110. writel_relaxed(val, wdt->base + PM_RSTC);
  111. /* No sleeping, possibly atomic. */
  112. mdelay(1);
  113. return 0;
  114. }
  115. /*
  116. * We can't really power off, but if we do the normal reset scheme, and
  117. * indicate to bootcode.bin not to reboot, then most of the chip will be
  118. * powered off.
  119. */
  120. static void bcm2835_power_off(void)
  121. {
  122. struct device_node *np =
  123. of_find_compatible_node(NULL, NULL, "brcm,bcm2835-pm-wdt");
  124. struct platform_device *pdev = of_find_device_by_node(np);
  125. struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
  126. u32 val;
  127. /*
  128. * We set the watchdog hard reset bit here to distinguish this reset
  129. * from the normal (full) reset. bootcode.bin will not reboot after a
  130. * hard reset.
  131. */
  132. val = readl_relaxed(wdt->base + PM_RSTS);
  133. val |= PM_PASSWORD | PM_RSTS_RASPBERRYPI_HALT;
  134. writel_relaxed(val, wdt->base + PM_RSTS);
  135. /* Continue with normal reset mechanism */
  136. bcm2835_restart(&wdt->restart_handler, REBOOT_HARD, NULL);
  137. }
  138. static int bcm2835_wdt_probe(struct platform_device *pdev)
  139. {
  140. struct device *dev = &pdev->dev;
  141. struct device_node *np = dev->of_node;
  142. struct bcm2835_wdt *wdt;
  143. int err;
  144. wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
  145. if (!wdt)
  146. return -ENOMEM;
  147. platform_set_drvdata(pdev, wdt);
  148. spin_lock_init(&wdt->lock);
  149. wdt->base = of_iomap(np, 0);
  150. if (!wdt->base) {
  151. dev_err(dev, "Failed to remap watchdog regs");
  152. return -ENODEV;
  153. }
  154. watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
  155. watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
  156. watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
  157. bcm2835_wdt_wdd.parent = &pdev->dev;
  158. if (bcm2835_wdt_is_running(wdt)) {
  159. /*
  160. * The currently active timeout value (set by the
  161. * bootloader) may be different from the module
  162. * heartbeat parameter or the value in device
  163. * tree. But we just need to set WDOG_HW_RUNNING,
  164. * because then the framework will "immediately" ping
  165. * the device, updating the timeout.
  166. */
  167. set_bit(WDOG_HW_RUNNING, &bcm2835_wdt_wdd.status);
  168. }
  169. err = watchdog_register_device(&bcm2835_wdt_wdd);
  170. if (err) {
  171. dev_err(dev, "Failed to register watchdog device");
  172. iounmap(wdt->base);
  173. return err;
  174. }
  175. wdt->restart_handler.notifier_call = bcm2835_restart;
  176. wdt->restart_handler.priority = 128;
  177. register_restart_handler(&wdt->restart_handler);
  178. if (pm_power_off == NULL)
  179. pm_power_off = bcm2835_power_off;
  180. dev_info(dev, "Broadcom BCM2835 watchdog timer");
  181. return 0;
  182. }
  183. static int bcm2835_wdt_remove(struct platform_device *pdev)
  184. {
  185. struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
  186. unregister_restart_handler(&wdt->restart_handler);
  187. if (pm_power_off == bcm2835_power_off)
  188. pm_power_off = NULL;
  189. watchdog_unregister_device(&bcm2835_wdt_wdd);
  190. iounmap(wdt->base);
  191. return 0;
  192. }
  193. static void bcm2835_wdt_shutdown(struct platform_device *pdev)
  194. {
  195. bcm2835_wdt_stop(&bcm2835_wdt_wdd);
  196. }
  197. static const struct of_device_id bcm2835_wdt_of_match[] = {
  198. { .compatible = "brcm,bcm2835-pm-wdt", },
  199. {},
  200. };
  201. MODULE_DEVICE_TABLE(of, bcm2835_wdt_of_match);
  202. static struct platform_driver bcm2835_wdt_driver = {
  203. .probe = bcm2835_wdt_probe,
  204. .remove = bcm2835_wdt_remove,
  205. .shutdown = bcm2835_wdt_shutdown,
  206. .driver = {
  207. .name = "bcm2835-wdt",
  208. .of_match_table = bcm2835_wdt_of_match,
  209. },
  210. };
  211. module_platform_driver(bcm2835_wdt_driver);
  212. module_param(heartbeat, uint, 0);
  213. MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
  214. module_param(nowayout, bool, 0);
  215. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  216. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  217. MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
  218. MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
  219. MODULE_LICENSE("GPL");