loongson1_wdt.c 4.0 KB

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