moxart_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include <asm/system_misc.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 struct moxart_wdt_dev *moxart_restart_ctx;
  30. static int heartbeat;
  31. static void moxart_wdt_restart(enum reboot_mode reboot_mode, const char *cmd)
  32. {
  33. writel(1, moxart_restart_ctx->base + REG_COUNT);
  34. writel(0x5ab9, moxart_restart_ctx->base + REG_MODE);
  35. writel(0x03, moxart_restart_ctx->base + REG_ENABLE);
  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. };
  69. static int moxart_wdt_probe(struct platform_device *pdev)
  70. {
  71. struct moxart_wdt_dev *moxart_wdt;
  72. struct device *dev = &pdev->dev;
  73. struct device_node *node = dev->of_node;
  74. struct resource *res;
  75. struct clk *clk;
  76. int err;
  77. unsigned int max_timeout;
  78. bool nowayout = WATCHDOG_NOWAYOUT;
  79. moxart_wdt = devm_kzalloc(dev, sizeof(*moxart_wdt), GFP_KERNEL);
  80. if (!moxart_wdt)
  81. return -ENOMEM;
  82. platform_set_drvdata(pdev, moxart_wdt);
  83. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  84. moxart_wdt->base = devm_ioremap_resource(dev, res);
  85. if (IS_ERR(moxart_wdt->base))
  86. return PTR_ERR(moxart_wdt->base);
  87. clk = of_clk_get(node, 0);
  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_drvdata(&moxart_wdt->dev, moxart_wdt);
  107. err = watchdog_register_device(&moxart_wdt->dev);
  108. if (err)
  109. return err;
  110. moxart_restart_ctx = moxart_wdt;
  111. arm_pm_restart = moxart_wdt_restart;
  112. dev_dbg(dev, "Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n",
  113. moxart_wdt->dev.timeout, nowayout);
  114. return 0;
  115. }
  116. static int moxart_wdt_remove(struct platform_device *pdev)
  117. {
  118. struct moxart_wdt_dev *moxart_wdt = platform_get_drvdata(pdev);
  119. arm_pm_restart = NULL;
  120. moxart_wdt_stop(&moxart_wdt->dev);
  121. watchdog_unregister_device(&moxart_wdt->dev);
  122. return 0;
  123. }
  124. static const struct of_device_id moxart_watchdog_match[] = {
  125. { .compatible = "moxa,moxart-watchdog" },
  126. { },
  127. };
  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. .owner = THIS_MODULE,
  134. .of_match_table = moxart_watchdog_match,
  135. },
  136. };
  137. module_platform_driver(moxart_wdt_driver);
  138. module_param(heartbeat, int, 0);
  139. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds");
  140. MODULE_DESCRIPTION("MOXART watchdog driver");
  141. MODULE_LICENSE("GPL");
  142. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");