moxart_wdt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * MOXA ART SoCs watchdog driver.
  3. *
  4. * Copyright (C) 2013 Jonas Jensen
  5. *
  6. * Jonas Jensen <jonas.jensen@gmail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/err.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/moduleparam.h>
  20. #define REG_COUNT 0x4
  21. #define REG_MODE 0x8
  22. #define REG_ENABLE 0xC
  23. struct moxart_wdt_dev {
  24. struct watchdog_device dev;
  25. void __iomem *base;
  26. unsigned int clock_frequency;
  27. };
  28. static int heartbeat;
  29. static int moxart_wdt_stop(struct watchdog_device *wdt_dev)
  30. {
  31. struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
  32. writel(0, moxart_wdt->base + REG_ENABLE);
  33. return 0;
  34. }
  35. static int moxart_wdt_start(struct watchdog_device *wdt_dev)
  36. {
  37. struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
  38. writel(moxart_wdt->clock_frequency * wdt_dev->timeout,
  39. moxart_wdt->base + REG_COUNT);
  40. writel(0x5ab9, moxart_wdt->base + REG_MODE);
  41. writel(0x03, moxart_wdt->base + REG_ENABLE);
  42. return 0;
  43. }
  44. static int moxart_wdt_set_timeout(struct watchdog_device *wdt_dev,
  45. unsigned int timeout)
  46. {
  47. wdt_dev->timeout = timeout;
  48. return 0;
  49. }
  50. static const struct watchdog_info moxart_wdt_info = {
  51. .identity = "moxart-wdt",
  52. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  53. WDIOF_MAGICCLOSE,
  54. };
  55. static const struct watchdog_ops moxart_wdt_ops = {
  56. .owner = THIS_MODULE,
  57. .start = moxart_wdt_start,
  58. .stop = moxart_wdt_stop,
  59. .set_timeout = moxart_wdt_set_timeout,
  60. };
  61. static int moxart_wdt_probe(struct platform_device *pdev)
  62. {
  63. struct moxart_wdt_dev *moxart_wdt;
  64. struct device *dev = &pdev->dev;
  65. struct device_node *node = dev->of_node;
  66. struct resource *res;
  67. struct clk *clk;
  68. int err;
  69. unsigned int max_timeout;
  70. bool nowayout = WATCHDOG_NOWAYOUT;
  71. moxart_wdt = devm_kzalloc(dev, sizeof(*moxart_wdt), GFP_KERNEL);
  72. if (!moxart_wdt)
  73. return -ENOMEM;
  74. platform_set_drvdata(pdev, moxart_wdt);
  75. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  76. moxart_wdt->base = devm_ioremap_resource(dev, res);
  77. if (IS_ERR(moxart_wdt->base))
  78. return PTR_ERR(moxart_wdt->base);
  79. clk = of_clk_get(node, 0);
  80. if (IS_ERR(clk)) {
  81. pr_err("%s: of_clk_get failed\n", __func__);
  82. return PTR_ERR(clk);
  83. }
  84. moxart_wdt->clock_frequency = clk_get_rate(clk);
  85. if (moxart_wdt->clock_frequency == 0) {
  86. pr_err("%s: incorrect clock frequency\n", __func__);
  87. return -EINVAL;
  88. }
  89. max_timeout = UINT_MAX / moxart_wdt->clock_frequency;
  90. moxart_wdt->dev.info = &moxart_wdt_info;
  91. moxart_wdt->dev.ops = &moxart_wdt_ops;
  92. moxart_wdt->dev.timeout = max_timeout;
  93. moxart_wdt->dev.min_timeout = 1;
  94. moxart_wdt->dev.max_timeout = max_timeout;
  95. moxart_wdt->dev.parent = dev;
  96. watchdog_init_timeout(&moxart_wdt->dev, heartbeat, dev);
  97. watchdog_set_nowayout(&moxart_wdt->dev, nowayout);
  98. watchdog_set_drvdata(&moxart_wdt->dev, moxart_wdt);
  99. err = watchdog_register_device(&moxart_wdt->dev);
  100. if (err)
  101. return err;
  102. dev_dbg(dev, "Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n",
  103. moxart_wdt->dev.timeout, nowayout);
  104. return 0;
  105. }
  106. static int moxart_wdt_remove(struct platform_device *pdev)
  107. {
  108. struct moxart_wdt_dev *moxart_wdt = platform_get_drvdata(pdev);
  109. moxart_wdt_stop(&moxart_wdt->dev);
  110. watchdog_unregister_device(&moxart_wdt->dev);
  111. return 0;
  112. }
  113. static const struct of_device_id moxart_watchdog_match[] = {
  114. { .compatible = "moxa,moxart-watchdog" },
  115. { },
  116. };
  117. static struct platform_driver moxart_wdt_driver = {
  118. .probe = moxart_wdt_probe,
  119. .remove = moxart_wdt_remove,
  120. .driver = {
  121. .name = "moxart-watchdog",
  122. .owner = THIS_MODULE,
  123. .of_match_table = moxart_watchdog_match,
  124. },
  125. };
  126. module_platform_driver(moxart_wdt_driver);
  127. module_param(heartbeat, int, 0);
  128. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
  129. MODULE_DESCRIPTION("MOXART watchdog driver");
  130. MODULE_LICENSE("GPL");
  131. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");