bcm2835_wdt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 int bcm2835_wdt_start(struct watchdog_device *wdog)
  50. {
  51. struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
  52. uint32_t cur;
  53. unsigned long flags;
  54. spin_lock_irqsave(&wdt->lock, flags);
  55. writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
  56. PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
  57. cur = readl_relaxed(wdt->base + PM_RSTC);
  58. writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  59. PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
  60. spin_unlock_irqrestore(&wdt->lock, flags);
  61. return 0;
  62. }
  63. static int bcm2835_wdt_stop(struct watchdog_device *wdog)
  64. {
  65. struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
  66. writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
  67. dev_info(wdog->dev, "Watchdog timer stopped");
  68. return 0;
  69. }
  70. static int bcm2835_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
  71. {
  72. wdog->timeout = t;
  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 struct watchdog_ops bcm2835_wdt_ops = {
  82. .owner = THIS_MODULE,
  83. .start = bcm2835_wdt_start,
  84. .stop = bcm2835_wdt_stop,
  85. .set_timeout = bcm2835_wdt_set_timeout,
  86. .get_timeleft = bcm2835_wdt_get_timeleft,
  87. };
  88. static struct watchdog_info bcm2835_wdt_info = {
  89. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  90. WDIOF_KEEPALIVEPING,
  91. .identity = "Broadcom BCM2835 Watchdog timer",
  92. };
  93. static struct watchdog_device bcm2835_wdt_wdd = {
  94. .info = &bcm2835_wdt_info,
  95. .ops = &bcm2835_wdt_ops,
  96. .min_timeout = 1,
  97. .max_timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  98. .timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  99. };
  100. static int
  101. bcm2835_restart(struct notifier_block *this, unsigned long mode, void *cmd)
  102. {
  103. struct bcm2835_wdt *wdt = container_of(this, struct bcm2835_wdt,
  104. restart_handler);
  105. u32 val;
  106. /* use a timeout of 10 ticks (~150us) */
  107. writel_relaxed(10 | PM_PASSWORD, wdt->base + PM_WDOG);
  108. val = readl_relaxed(wdt->base + PM_RSTC);
  109. val &= PM_RSTC_WRCFG_CLR;
  110. val |= PM_PASSWORD | PM_RSTC_WRCFG_FULL_RESET;
  111. writel_relaxed(val, wdt->base + PM_RSTC);
  112. /* No sleeping, possibly atomic. */
  113. mdelay(1);
  114. return 0;
  115. }
  116. /*
  117. * We can't really power off, but if we do the normal reset scheme, and
  118. * indicate to bootcode.bin not to reboot, then most of the chip will be
  119. * powered off.
  120. */
  121. static void bcm2835_power_off(void)
  122. {
  123. struct device_node *np =
  124. of_find_compatible_node(NULL, NULL, "brcm,bcm2835-pm-wdt");
  125. struct platform_device *pdev = of_find_device_by_node(np);
  126. struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
  127. u32 val;
  128. /*
  129. * We set the watchdog hard reset bit here to distinguish this reset
  130. * from the normal (full) reset. bootcode.bin will not reboot after a
  131. * hard reset.
  132. */
  133. val = readl_relaxed(wdt->base + PM_RSTS);
  134. val |= PM_PASSWORD | PM_RSTS_RASPBERRYPI_HALT;
  135. writel_relaxed(val, wdt->base + PM_RSTS);
  136. /* Continue with normal reset mechanism */
  137. bcm2835_restart(&wdt->restart_handler, REBOOT_HARD, NULL);
  138. }
  139. static int bcm2835_wdt_probe(struct platform_device *pdev)
  140. {
  141. struct device *dev = &pdev->dev;
  142. struct device_node *np = dev->of_node;
  143. struct bcm2835_wdt *wdt;
  144. int err;
  145. wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
  146. if (!wdt)
  147. return -ENOMEM;
  148. platform_set_drvdata(pdev, wdt);
  149. spin_lock_init(&wdt->lock);
  150. wdt->base = of_iomap(np, 0);
  151. if (!wdt->base) {
  152. dev_err(dev, "Failed to remap watchdog regs");
  153. return -ENODEV;
  154. }
  155. watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
  156. watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
  157. watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
  158. bcm2835_wdt_wdd.parent = &pdev->dev;
  159. err = watchdog_register_device(&bcm2835_wdt_wdd);
  160. if (err) {
  161. dev_err(dev, "Failed to register watchdog device");
  162. iounmap(wdt->base);
  163. return err;
  164. }
  165. wdt->restart_handler.notifier_call = bcm2835_restart;
  166. wdt->restart_handler.priority = 128;
  167. register_restart_handler(&wdt->restart_handler);
  168. if (pm_power_off == NULL)
  169. pm_power_off = bcm2835_power_off;
  170. dev_info(dev, "Broadcom BCM2835 watchdog timer");
  171. return 0;
  172. }
  173. static int bcm2835_wdt_remove(struct platform_device *pdev)
  174. {
  175. struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
  176. unregister_restart_handler(&wdt->restart_handler);
  177. if (pm_power_off == bcm2835_power_off)
  178. pm_power_off = NULL;
  179. watchdog_unregister_device(&bcm2835_wdt_wdd);
  180. iounmap(wdt->base);
  181. return 0;
  182. }
  183. static void bcm2835_wdt_shutdown(struct platform_device *pdev)
  184. {
  185. bcm2835_wdt_stop(&bcm2835_wdt_wdd);
  186. }
  187. static const struct of_device_id bcm2835_wdt_of_match[] = {
  188. { .compatible = "brcm,bcm2835-pm-wdt", },
  189. {},
  190. };
  191. MODULE_DEVICE_TABLE(of, bcm2835_wdt_of_match);
  192. static struct platform_driver bcm2835_wdt_driver = {
  193. .probe = bcm2835_wdt_probe,
  194. .remove = bcm2835_wdt_remove,
  195. .shutdown = bcm2835_wdt_shutdown,
  196. .driver = {
  197. .name = "bcm2835-wdt",
  198. .of_match_table = bcm2835_wdt_of_match,
  199. },
  200. };
  201. module_platform_driver(bcm2835_wdt_driver);
  202. module_param(heartbeat, uint, 0);
  203. MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
  204. module_param(nowayout, bool, 0);
  205. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  206. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  207. MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
  208. MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
  209. MODULE_LICENSE("GPL");