bcm2835_wdt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/types.h>
  16. #include <linux/module.h>
  17. #include <linux/io.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/of_address.h>
  21. #include <linux/miscdevice.h>
  22. #define PM_RSTC 0x1c
  23. #define PM_WDOG 0x24
  24. #define PM_PASSWORD 0x5a000000
  25. #define PM_WDOG_TIME_SET 0x000fffff
  26. #define PM_RSTC_WRCFG_CLR 0xffffffcf
  27. #define PM_RSTC_WRCFG_SET 0x00000030
  28. #define PM_RSTC_WRCFG_FULL_RESET 0x00000020
  29. #define PM_RSTC_RESET 0x00000102
  30. #define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  31. #define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  32. struct bcm2835_wdt {
  33. void __iomem *base;
  34. spinlock_t lock;
  35. };
  36. static unsigned int heartbeat;
  37. static bool nowayout = WATCHDOG_NOWAYOUT;
  38. static int bcm2835_wdt_start(struct watchdog_device *wdog)
  39. {
  40. struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
  41. uint32_t cur;
  42. unsigned long flags;
  43. spin_lock_irqsave(&wdt->lock, flags);
  44. writel_relaxed(PM_PASSWORD | (SECS_TO_WDOG_TICKS(wdog->timeout) &
  45. PM_WDOG_TIME_SET), wdt->base + PM_WDOG);
  46. cur = readl_relaxed(wdt->base + PM_RSTC);
  47. writel_relaxed(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  48. PM_RSTC_WRCFG_FULL_RESET, wdt->base + PM_RSTC);
  49. spin_unlock_irqrestore(&wdt->lock, flags);
  50. return 0;
  51. }
  52. static int bcm2835_wdt_stop(struct watchdog_device *wdog)
  53. {
  54. struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
  55. writel_relaxed(PM_PASSWORD | PM_RSTC_RESET, wdt->base + PM_RSTC);
  56. dev_info(wdog->dev, "Watchdog timer stopped");
  57. return 0;
  58. }
  59. static int bcm2835_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
  60. {
  61. wdog->timeout = t;
  62. return 0;
  63. }
  64. static unsigned int bcm2835_wdt_get_timeleft(struct watchdog_device *wdog)
  65. {
  66. struct bcm2835_wdt *wdt = watchdog_get_drvdata(wdog);
  67. uint32_t ret = readl_relaxed(wdt->base + PM_WDOG);
  68. return WDOG_TICKS_TO_SECS(ret & PM_WDOG_TIME_SET);
  69. }
  70. static struct watchdog_ops bcm2835_wdt_ops = {
  71. .owner = THIS_MODULE,
  72. .start = bcm2835_wdt_start,
  73. .stop = bcm2835_wdt_stop,
  74. .set_timeout = bcm2835_wdt_set_timeout,
  75. .get_timeleft = bcm2835_wdt_get_timeleft,
  76. };
  77. static struct watchdog_info bcm2835_wdt_info = {
  78. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE |
  79. WDIOF_KEEPALIVEPING,
  80. .identity = "Broadcom BCM2835 Watchdog timer",
  81. };
  82. static struct watchdog_device bcm2835_wdt_wdd = {
  83. .info = &bcm2835_wdt_info,
  84. .ops = &bcm2835_wdt_ops,
  85. .min_timeout = 1,
  86. .max_timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  87. .timeout = WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  88. };
  89. static int bcm2835_wdt_probe(struct platform_device *pdev)
  90. {
  91. struct device *dev = &pdev->dev;
  92. struct device_node *np = dev->of_node;
  93. struct bcm2835_wdt *wdt;
  94. int err;
  95. wdt = devm_kzalloc(dev, sizeof(struct bcm2835_wdt), GFP_KERNEL);
  96. if (!wdt) {
  97. dev_err(dev, "Failed to allocate memory for watchdog device");
  98. return -ENOMEM;
  99. }
  100. platform_set_drvdata(pdev, wdt);
  101. spin_lock_init(&wdt->lock);
  102. wdt->base = of_iomap(np, 0);
  103. if (!wdt->base) {
  104. dev_err(dev, "Failed to remap watchdog regs");
  105. return -ENODEV;
  106. }
  107. watchdog_set_drvdata(&bcm2835_wdt_wdd, wdt);
  108. watchdog_init_timeout(&bcm2835_wdt_wdd, heartbeat, dev);
  109. watchdog_set_nowayout(&bcm2835_wdt_wdd, nowayout);
  110. err = watchdog_register_device(&bcm2835_wdt_wdd);
  111. if (err) {
  112. dev_err(dev, "Failed to register watchdog device");
  113. iounmap(wdt->base);
  114. return err;
  115. }
  116. dev_info(dev, "Broadcom BCM2835 watchdog timer");
  117. return 0;
  118. }
  119. static int bcm2835_wdt_remove(struct platform_device *pdev)
  120. {
  121. struct bcm2835_wdt *wdt = platform_get_drvdata(pdev);
  122. watchdog_unregister_device(&bcm2835_wdt_wdd);
  123. iounmap(wdt->base);
  124. return 0;
  125. }
  126. static void bcm2835_wdt_shutdown(struct platform_device *pdev)
  127. {
  128. bcm2835_wdt_stop(&bcm2835_wdt_wdd);
  129. }
  130. static const struct of_device_id bcm2835_wdt_of_match[] = {
  131. { .compatible = "brcm,bcm2835-pm-wdt", },
  132. {},
  133. };
  134. MODULE_DEVICE_TABLE(of, bcm2835_wdt_of_match);
  135. static struct platform_driver bcm2835_wdt_driver = {
  136. .probe = bcm2835_wdt_probe,
  137. .remove = bcm2835_wdt_remove,
  138. .shutdown = bcm2835_wdt_shutdown,
  139. .driver = {
  140. .name = "bcm2835-wdt",
  141. .owner = THIS_MODULE,
  142. .of_match_table = bcm2835_wdt_of_match,
  143. },
  144. };
  145. module_platform_driver(bcm2835_wdt_driver);
  146. module_param(heartbeat, uint, 0);
  147. MODULE_PARM_DESC(heartbeat, "Initial watchdog heartbeat in seconds");
  148. module_param(nowayout, bool, 0);
  149. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  150. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  151. MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
  152. MODULE_DESCRIPTION("Driver for Broadcom BCM2835 watchdog timer");
  153. MODULE_LICENSE("GPL");
  154. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);