moxart_wdt.c 4.1 KB

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