moxart_wdt.c 4.8 KB

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