digicolor_wdt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Watchdog driver for Conexant Digicolor
  3. *
  4. * Copyright (C) 2015 Paradox Innovation Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/module.h>
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/clk.h>
  16. #include <linux/watchdog.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of_address.h>
  19. #define TIMER_A_CONTROL 0
  20. #define TIMER_A_COUNT 4
  21. #define TIMER_A_ENABLE_COUNT BIT(0)
  22. #define TIMER_A_ENABLE_WATCHDOG BIT(1)
  23. struct dc_wdt {
  24. void __iomem *base;
  25. struct clk *clk;
  26. spinlock_t lock;
  27. };
  28. static unsigned timeout;
  29. module_param(timeout, uint, 0);
  30. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
  31. static void dc_wdt_set(struct dc_wdt *wdt, u32 ticks)
  32. {
  33. unsigned long flags;
  34. spin_lock_irqsave(&wdt->lock, flags);
  35. writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
  36. writel_relaxed(ticks, wdt->base + TIMER_A_COUNT);
  37. writel_relaxed(TIMER_A_ENABLE_COUNT | TIMER_A_ENABLE_WATCHDOG,
  38. wdt->base + TIMER_A_CONTROL);
  39. spin_unlock_irqrestore(&wdt->lock, flags);
  40. }
  41. static int dc_wdt_restart(struct watchdog_device *wdog, unsigned long action,
  42. void *data)
  43. {
  44. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  45. dc_wdt_set(wdt, 1);
  46. /* wait for reset to assert... */
  47. mdelay(500);
  48. return 0;
  49. }
  50. static int dc_wdt_start(struct watchdog_device *wdog)
  51. {
  52. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  53. dc_wdt_set(wdt, wdog->timeout * clk_get_rate(wdt->clk));
  54. return 0;
  55. }
  56. static int dc_wdt_stop(struct watchdog_device *wdog)
  57. {
  58. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  59. writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
  60. return 0;
  61. }
  62. static int dc_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
  63. {
  64. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  65. dc_wdt_set(wdt, t * clk_get_rate(wdt->clk));
  66. wdog->timeout = t;
  67. return 0;
  68. }
  69. static unsigned int dc_wdt_get_timeleft(struct watchdog_device *wdog)
  70. {
  71. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  72. uint32_t count = readl_relaxed(wdt->base + TIMER_A_COUNT);
  73. return count / clk_get_rate(wdt->clk);
  74. }
  75. static struct watchdog_ops dc_wdt_ops = {
  76. .owner = THIS_MODULE,
  77. .start = dc_wdt_start,
  78. .stop = dc_wdt_stop,
  79. .set_timeout = dc_wdt_set_timeout,
  80. .get_timeleft = dc_wdt_get_timeleft,
  81. .restart = dc_wdt_restart,
  82. };
  83. static struct watchdog_info dc_wdt_info = {
  84. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE
  85. | WDIOF_KEEPALIVEPING,
  86. .identity = "Conexant Digicolor Watchdog",
  87. };
  88. static struct watchdog_device dc_wdt_wdd = {
  89. .info = &dc_wdt_info,
  90. .ops = &dc_wdt_ops,
  91. .min_timeout = 1,
  92. };
  93. static int dc_wdt_probe(struct platform_device *pdev)
  94. {
  95. struct device *dev = &pdev->dev;
  96. struct device_node *np = dev->of_node;
  97. struct dc_wdt *wdt;
  98. int ret;
  99. wdt = devm_kzalloc(dev, sizeof(struct dc_wdt), GFP_KERNEL);
  100. if (!wdt)
  101. return -ENOMEM;
  102. platform_set_drvdata(pdev, wdt);
  103. wdt->base = of_iomap(np, 0);
  104. if (!wdt->base) {
  105. dev_err(dev, "Failed to remap watchdog regs");
  106. return -ENODEV;
  107. }
  108. wdt->clk = devm_clk_get(&pdev->dev, NULL);
  109. if (IS_ERR(wdt->clk)) {
  110. ret = PTR_ERR(wdt->clk);
  111. goto err_iounmap;
  112. }
  113. dc_wdt_wdd.max_timeout = U32_MAX / clk_get_rate(wdt->clk);
  114. dc_wdt_wdd.timeout = dc_wdt_wdd.max_timeout;
  115. dc_wdt_wdd.parent = &pdev->dev;
  116. spin_lock_init(&wdt->lock);
  117. watchdog_set_drvdata(&dc_wdt_wdd, wdt);
  118. watchdog_set_restart_priority(&dc_wdt_wdd, 128);
  119. watchdog_init_timeout(&dc_wdt_wdd, timeout, dev);
  120. ret = watchdog_register_device(&dc_wdt_wdd);
  121. if (ret) {
  122. dev_err(dev, "Failed to register watchdog device");
  123. goto err_iounmap;
  124. }
  125. return 0;
  126. err_iounmap:
  127. iounmap(wdt->base);
  128. return ret;
  129. }
  130. static int dc_wdt_remove(struct platform_device *pdev)
  131. {
  132. struct dc_wdt *wdt = platform_get_drvdata(pdev);
  133. watchdog_unregister_device(&dc_wdt_wdd);
  134. iounmap(wdt->base);
  135. return 0;
  136. }
  137. static void dc_wdt_shutdown(struct platform_device *pdev)
  138. {
  139. dc_wdt_stop(&dc_wdt_wdd);
  140. }
  141. static const struct of_device_id dc_wdt_of_match[] = {
  142. { .compatible = "cnxt,cx92755-wdt", },
  143. {},
  144. };
  145. MODULE_DEVICE_TABLE(of, dc_wdt_of_match);
  146. static struct platform_driver dc_wdt_driver = {
  147. .probe = dc_wdt_probe,
  148. .remove = dc_wdt_remove,
  149. .shutdown = dc_wdt_shutdown,
  150. .driver = {
  151. .name = "digicolor-wdt",
  152. .of_match_table = dc_wdt_of_match,
  153. },
  154. };
  155. module_platform_driver(dc_wdt_driver);
  156. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  157. MODULE_DESCRIPTION("Driver for Conexant Digicolor watchdog timer");
  158. MODULE_LICENSE("GPL");