moxart_wdt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/mod_devicetable.h>
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/moduleparam.h>
  21. #define REG_COUNT 0x4
  22. #define REG_MODE 0x8
  23. #define REG_ENABLE 0xC
  24. struct moxart_wdt_dev {
  25. struct watchdog_device dev;
  26. void __iomem *base;
  27. unsigned int clock_frequency;
  28. };
  29. static int heartbeat;
  30. static int moxart_wdt_restart(struct watchdog_device *wdt_dev,
  31. unsigned long action, void *data)
  32. {
  33. struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
  34. writel(1, moxart_wdt->base + REG_COUNT);
  35. writel(0x5ab9, moxart_wdt->base + REG_MODE);
  36. writel(0x03, moxart_wdt->base + REG_ENABLE);
  37. return 0;
  38. }
  39. static int moxart_wdt_stop(struct watchdog_device *wdt_dev)
  40. {
  41. struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
  42. writel(0, moxart_wdt->base + REG_ENABLE);
  43. return 0;
  44. }
  45. static int moxart_wdt_start(struct watchdog_device *wdt_dev)
  46. {
  47. struct moxart_wdt_dev *moxart_wdt = watchdog_get_drvdata(wdt_dev);
  48. writel(moxart_wdt->clock_frequency * wdt_dev->timeout,
  49. moxart_wdt->base + REG_COUNT);
  50. writel(0x5ab9, moxart_wdt->base + REG_MODE);
  51. writel(0x03, moxart_wdt->base + REG_ENABLE);
  52. return 0;
  53. }
  54. static int moxart_wdt_set_timeout(struct watchdog_device *wdt_dev,
  55. unsigned int timeout)
  56. {
  57. wdt_dev->timeout = timeout;
  58. return 0;
  59. }
  60. static const struct watchdog_info moxart_wdt_info = {
  61. .identity = "moxart-wdt",
  62. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
  63. WDIOF_MAGICCLOSE,
  64. };
  65. static const struct watchdog_ops moxart_wdt_ops = {
  66. .owner = THIS_MODULE,
  67. .start = moxart_wdt_start,
  68. .stop = moxart_wdt_stop,
  69. .set_timeout = moxart_wdt_set_timeout,
  70. .restart = moxart_wdt_restart,
  71. };
  72. static int moxart_wdt_probe(struct platform_device *pdev)
  73. {
  74. struct moxart_wdt_dev *moxart_wdt;
  75. struct device *dev = &pdev->dev;
  76. struct clk *clk;
  77. int err;
  78. unsigned int max_timeout;
  79. bool nowayout = WATCHDOG_NOWAYOUT;
  80. moxart_wdt = devm_kzalloc(dev, sizeof(*moxart_wdt), GFP_KERNEL);
  81. if (!moxart_wdt)
  82. return -ENOMEM;
  83. platform_set_drvdata(pdev, moxart_wdt);
  84. moxart_wdt->base = devm_platform_ioremap_resource(pdev, 0);
  85. if (IS_ERR(moxart_wdt->base))
  86. return PTR_ERR(moxart_wdt->base);
  87. clk = devm_clk_get(dev, NULL);
  88. if (IS_ERR(clk)) {
  89. pr_err("%s: of_clk_get failed\n", __func__);
  90. return PTR_ERR(clk);
  91. }
  92. moxart_wdt->clock_frequency = clk_get_rate(clk);
  93. if (moxart_wdt->clock_frequency == 0) {
  94. pr_err("%s: incorrect clock frequency\n", __func__);
  95. return -EINVAL;
  96. }
  97. max_timeout = UINT_MAX / moxart_wdt->clock_frequency;
  98. moxart_wdt->dev.info = &moxart_wdt_info;
  99. moxart_wdt->dev.ops = &moxart_wdt_ops;
  100. moxart_wdt->dev.timeout = max_timeout;
  101. moxart_wdt->dev.min_timeout = 1;
  102. moxart_wdt->dev.max_timeout = max_timeout;
  103. moxart_wdt->dev.parent = dev;
  104. watchdog_init_timeout(&moxart_wdt->dev, heartbeat, dev);
  105. watchdog_set_nowayout(&moxart_wdt->dev, nowayout);
  106. watchdog_set_restart_priority(&moxart_wdt->dev, 128);
  107. watchdog_set_drvdata(&moxart_wdt->dev, moxart_wdt);
  108. watchdog_stop_on_unregister(&moxart_wdt->dev);
  109. err = devm_watchdog_register_device(dev, &moxart_wdt->dev);
  110. if (err)
  111. return err;
  112. dev_dbg(dev, "Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n",
  113. moxart_wdt->dev.timeout, nowayout);
  114. return 0;
  115. }
  116. static const struct of_device_id moxart_watchdog_match[] = {
  117. { .compatible = "moxa,moxart-watchdog" },
  118. { },
  119. };
  120. MODULE_DEVICE_TABLE(of, moxart_watchdog_match);
  121. static struct platform_driver moxart_wdt_driver = {
  122. .probe = moxart_wdt_probe,
  123. .driver = {
  124. .name = "moxart-watchdog",
  125. .of_match_table = moxart_watchdog_match,
  126. },
  127. };
  128. module_platform_driver(moxart_wdt_driver);
  129. module_param(heartbeat, int, 0);
  130. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
  131. MODULE_DESCRIPTION("MOXART watchdog driver");
  132. MODULE_LICENSE("GPL");
  133. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");