loongson1_wdt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016 Yang Ling <gnaygnil@gmail.com>
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/watchdog.h>
  9. #include <loongson1.h>
  10. #define DEFAULT_HEARTBEAT 30
  11. static bool nowayout = WATCHDOG_NOWAYOUT;
  12. module_param(nowayout, bool, 0444);
  13. static unsigned int heartbeat;
  14. module_param(heartbeat, uint, 0444);
  15. struct ls1x_wdt_drvdata {
  16. void __iomem *base;
  17. struct clk *clk;
  18. unsigned long clk_rate;
  19. struct watchdog_device wdt;
  20. };
  21. static int ls1x_wdt_ping(struct watchdog_device *wdt_dev)
  22. {
  23. struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  24. writel(0x1, drvdata->base + WDT_SET);
  25. return 0;
  26. }
  27. static int ls1x_wdt_set_timeout(struct watchdog_device *wdt_dev,
  28. unsigned int timeout)
  29. {
  30. struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  31. unsigned int max_hw_heartbeat = wdt_dev->max_hw_heartbeat_ms / 1000;
  32. unsigned int counts;
  33. wdt_dev->timeout = timeout;
  34. counts = drvdata->clk_rate * min(timeout, max_hw_heartbeat);
  35. writel(counts, drvdata->base + WDT_TIMER);
  36. return 0;
  37. }
  38. static int ls1x_wdt_start(struct watchdog_device *wdt_dev)
  39. {
  40. struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  41. writel(0x1, drvdata->base + WDT_EN);
  42. return 0;
  43. }
  44. static int ls1x_wdt_stop(struct watchdog_device *wdt_dev)
  45. {
  46. struct ls1x_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  47. writel(0x0, drvdata->base + WDT_EN);
  48. return 0;
  49. }
  50. static const struct watchdog_info ls1x_wdt_info = {
  51. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  52. .identity = "Loongson1 Watchdog",
  53. };
  54. static const struct watchdog_ops ls1x_wdt_ops = {
  55. .owner = THIS_MODULE,
  56. .start = ls1x_wdt_start,
  57. .stop = ls1x_wdt_stop,
  58. .ping = ls1x_wdt_ping,
  59. .set_timeout = ls1x_wdt_set_timeout,
  60. };
  61. static void ls1x_clk_disable_unprepare(void *data)
  62. {
  63. clk_disable_unprepare(data);
  64. }
  65. static int ls1x_wdt_probe(struct platform_device *pdev)
  66. {
  67. struct device *dev = &pdev->dev;
  68. struct ls1x_wdt_drvdata *drvdata;
  69. struct watchdog_device *ls1x_wdt;
  70. unsigned long clk_rate;
  71. int err;
  72. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  73. if (!drvdata)
  74. return -ENOMEM;
  75. drvdata->base = devm_platform_ioremap_resource(pdev, 0);
  76. if (IS_ERR(drvdata->base))
  77. return PTR_ERR(drvdata->base);
  78. drvdata->clk = devm_clk_get(dev, pdev->name);
  79. if (IS_ERR(drvdata->clk))
  80. return PTR_ERR(drvdata->clk);
  81. err = clk_prepare_enable(drvdata->clk);
  82. if (err) {
  83. dev_err(dev, "clk enable failed\n");
  84. return err;
  85. }
  86. err = devm_add_action_or_reset(dev, ls1x_clk_disable_unprepare,
  87. drvdata->clk);
  88. if (err)
  89. return err;
  90. clk_rate = clk_get_rate(drvdata->clk);
  91. if (!clk_rate)
  92. return -EINVAL;
  93. drvdata->clk_rate = clk_rate;
  94. ls1x_wdt = &drvdata->wdt;
  95. ls1x_wdt->info = &ls1x_wdt_info;
  96. ls1x_wdt->ops = &ls1x_wdt_ops;
  97. ls1x_wdt->timeout = DEFAULT_HEARTBEAT;
  98. ls1x_wdt->min_timeout = 1;
  99. ls1x_wdt->max_hw_heartbeat_ms = U32_MAX / clk_rate * 1000;
  100. ls1x_wdt->parent = dev;
  101. watchdog_init_timeout(ls1x_wdt, heartbeat, dev);
  102. watchdog_set_nowayout(ls1x_wdt, nowayout);
  103. watchdog_set_drvdata(ls1x_wdt, drvdata);
  104. err = devm_watchdog_register_device(dev, &drvdata->wdt);
  105. if (err)
  106. return err;
  107. platform_set_drvdata(pdev, drvdata);
  108. dev_info(dev, "Loongson1 Watchdog driver registered\n");
  109. return 0;
  110. }
  111. static struct platform_driver ls1x_wdt_driver = {
  112. .probe = ls1x_wdt_probe,
  113. .driver = {
  114. .name = "ls1x-wdt",
  115. },
  116. };
  117. module_platform_driver(ls1x_wdt_driver);
  118. MODULE_AUTHOR("Yang Ling <gnaygnil@gmail.com>");
  119. MODULE_DESCRIPTION("Loongson1 Watchdog Driver");
  120. MODULE_LICENSE("GPL");