moxart_wdt.c 4.5 KB

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