mcs814x_wdt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Moschip MCS814x Watchdog driver
  3. *
  4. * Copyright (C) 2012, Florian Fainelli <florian@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/init.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/watchdog.h>
  26. #include <linux/io.h>
  27. #include <linux/err.h>
  28. #include <linux/clk.h>
  29. #include <linux/of.h>
  30. #define WDT_COUNT 0x00
  31. #define WDT_CTRL 0x04
  32. #define WDT_CTRL_EN 0x1
  33. /* watchdog frequency */
  34. #define WDT_MAX_VALUE (0xffffffff)
  35. struct mcs814x_wdt {
  36. void __iomem *regs;
  37. spinlock_t lock;
  38. struct watchdog_device wdt_dev;
  39. struct clk *clk;
  40. };
  41. static int mcs814x_wdt_start(struct watchdog_device *dev)
  42. {
  43. struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);
  44. u32 reg;
  45. spin_lock(&wdt->lock);
  46. reg = readl_relaxed(wdt->regs + WDT_CTRL);
  47. reg |= WDT_CTRL_EN;
  48. writel_relaxed(reg, wdt->regs + WDT_CTRL);
  49. spin_unlock(&wdt->lock);
  50. return 0;
  51. }
  52. static int mcs814x_wdt_stop(struct watchdog_device *dev)
  53. {
  54. struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);
  55. u32 reg;
  56. spin_lock(&wdt->lock);
  57. reg = readl_relaxed(wdt->regs + WDT_CTRL);
  58. reg &= ~WDT_CTRL_EN;
  59. writel_relaxed(reg, wdt->regs + WDT_CTRL);
  60. spin_unlock(&wdt->lock);
  61. return 0;
  62. }
  63. static int mcs814x_wdt_set_timeout(struct watchdog_device *dev,
  64. unsigned int new_timeout)
  65. {
  66. struct mcs814x_wdt *wdt = watchdog_get_drvdata(dev);
  67. spin_lock(&wdt->lock);
  68. /* watchdog counts upward and rollover (0xfffffff -> 0)
  69. * triggers the reboot
  70. */
  71. writel_relaxed(WDT_MAX_VALUE - (new_timeout * clk_get_rate(wdt->clk)),
  72. wdt->regs + WDT_COUNT);
  73. spin_unlock(&wdt->lock);
  74. return 0;
  75. }
  76. static int mcs814x_wdt_ping(struct watchdog_device *dev)
  77. {
  78. /* restart the watchdog */
  79. mcs814x_wdt_stop(dev);
  80. mcs814x_wdt_set_timeout(dev, dev->timeout);
  81. mcs814x_wdt_start(dev);
  82. return 0;
  83. }
  84. static const struct watchdog_info mcs814x_wdt_ident = {
  85. .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
  86. WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  87. .identity = "MCS814x Watchdog",
  88. };
  89. static struct watchdog_ops mcs814x_wdt_ops = {
  90. .owner = THIS_MODULE,
  91. .start = mcs814x_wdt_start,
  92. .stop = mcs814x_wdt_stop,
  93. .set_timeout = mcs814x_wdt_set_timeout,
  94. .ping = mcs814x_wdt_ping,
  95. };
  96. static int mcs814x_wdt_probe(struct platform_device *pdev)
  97. {
  98. struct resource *res;
  99. struct mcs814x_wdt *wdt;
  100. int ret;
  101. struct clk *clk;
  102. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  103. if (!res)
  104. return -ENODEV;
  105. clk = clk_get(NULL, "wdt");
  106. if (IS_ERR_OR_NULL(clk)) {
  107. dev_err(&pdev->dev, "failed to get watchdog clock\n");
  108. return PTR_ERR(clk);
  109. }
  110. wdt = kzalloc(sizeof(*wdt), GFP_KERNEL);
  111. if (!wdt) {
  112. ret = -ENOMEM;
  113. goto out_clk;
  114. }
  115. spin_lock_init(&wdt->lock);
  116. wdt->clk = clk;
  117. wdt->wdt_dev.info = &mcs814x_wdt_ident;
  118. wdt->wdt_dev.ops = &mcs814x_wdt_ops;
  119. wdt->wdt_dev.min_timeout = 1;
  120. /* approximately 10995 secs */
  121. wdt->wdt_dev.max_timeout = (WDT_MAX_VALUE / clk_get_rate(clk));
  122. platform_set_drvdata(pdev, wdt);
  123. /* only ioremap registers, because the register is shared */
  124. wdt->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  125. if (!wdt->regs) {
  126. ret = -ENOMEM;
  127. goto out;
  128. }
  129. watchdog_set_drvdata(&wdt->wdt_dev, wdt);
  130. ret = watchdog_register_device(&wdt->wdt_dev);
  131. if (ret) {
  132. dev_err(&pdev->dev, "cannot register watchdog: %d\n", ret);
  133. goto out;
  134. }
  135. dev_info(&pdev->dev, "registered\n");
  136. return 0;
  137. out:
  138. platform_set_drvdata(pdev, NULL);
  139. kfree(wdt);
  140. out_clk:
  141. clk_put(clk);
  142. return ret;
  143. }
  144. static int mcs814x_wdt_remove(struct platform_device *pdev)
  145. {
  146. struct mcs814x_wdt *wdt = platform_get_drvdata(pdev);
  147. clk_put(wdt->clk);
  148. watchdog_unregister_device(&wdt->wdt_dev);
  149. watchdog_set_drvdata(&wdt->wdt_dev, NULL);
  150. kfree(wdt);
  151. platform_set_drvdata(pdev, NULL);
  152. return 0;
  153. }
  154. static const struct of_device_id mcs814x_wdt_ids[] = {
  155. { .compatible = "moschip,mcs814x-wdt", },
  156. { /* sentinel */ },
  157. };
  158. static struct platform_driver mcs814x_wdt_driver = {
  159. .driver = {
  160. .name = "mcs814x-wdt",
  161. .owner = THIS_MODULE,
  162. .of_match_table = mcs814x_wdt_ids,
  163. },
  164. .probe = mcs814x_wdt_probe,
  165. .remove = mcs814x_wdt_remove,
  166. };
  167. module_platform_driver(mcs814x_wdt_driver);
  168. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  169. MODULE_DESCRIPTION("Moschip MCS814x Watchdog driver");
  170. MODULE_LICENSE("GPL");
  171. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  172. MODULE_ALIAS("platform:mcs814x-wdt");