digicolor_wdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/reboot.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/of_address.h>
  20. #define TIMER_A_CONTROL 0
  21. #define TIMER_A_COUNT 4
  22. #define TIMER_A_ENABLE_COUNT BIT(0)
  23. #define TIMER_A_ENABLE_WATCHDOG BIT(1)
  24. struct dc_wdt {
  25. void __iomem *base;
  26. struct clk *clk;
  27. struct notifier_block restart_handler;
  28. spinlock_t lock;
  29. };
  30. static unsigned timeout;
  31. module_param(timeout, uint, 0);
  32. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
  33. static void dc_wdt_set(struct dc_wdt *wdt, u32 ticks)
  34. {
  35. unsigned long flags;
  36. spin_lock_irqsave(&wdt->lock, flags);
  37. writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
  38. writel_relaxed(ticks, wdt->base + TIMER_A_COUNT);
  39. writel_relaxed(TIMER_A_ENABLE_COUNT | TIMER_A_ENABLE_WATCHDOG,
  40. wdt->base + TIMER_A_CONTROL);
  41. spin_unlock_irqrestore(&wdt->lock, flags);
  42. }
  43. static int dc_restart_handler(struct notifier_block *this, unsigned long mode,
  44. void *cmd)
  45. {
  46. struct dc_wdt *wdt = container_of(this, struct dc_wdt, restart_handler);
  47. dc_wdt_set(wdt, 1);
  48. /* wait for reset to assert... */
  49. mdelay(500);
  50. return NOTIFY_DONE;
  51. }
  52. static int dc_wdt_start(struct watchdog_device *wdog)
  53. {
  54. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  55. dc_wdt_set(wdt, wdog->timeout * clk_get_rate(wdt->clk));
  56. return 0;
  57. }
  58. static int dc_wdt_stop(struct watchdog_device *wdog)
  59. {
  60. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  61. writel_relaxed(0, wdt->base + TIMER_A_CONTROL);
  62. return 0;
  63. }
  64. static int dc_wdt_set_timeout(struct watchdog_device *wdog, unsigned int t)
  65. {
  66. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  67. dc_wdt_set(wdt, t * clk_get_rate(wdt->clk));
  68. wdog->timeout = t;
  69. return 0;
  70. }
  71. static unsigned int dc_wdt_get_timeleft(struct watchdog_device *wdog)
  72. {
  73. struct dc_wdt *wdt = watchdog_get_drvdata(wdog);
  74. uint32_t count = readl_relaxed(wdt->base + TIMER_A_COUNT);
  75. return count / clk_get_rate(wdt->clk);
  76. }
  77. static struct watchdog_ops dc_wdt_ops = {
  78. .owner = THIS_MODULE,
  79. .start = dc_wdt_start,
  80. .stop = dc_wdt_stop,
  81. .set_timeout = dc_wdt_set_timeout,
  82. .get_timeleft = dc_wdt_get_timeleft,
  83. };
  84. static struct watchdog_info dc_wdt_info = {
  85. .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE
  86. | WDIOF_KEEPALIVEPING,
  87. .identity = "Conexant Digicolor Watchdog",
  88. };
  89. static struct watchdog_device dc_wdt_wdd = {
  90. .info = &dc_wdt_info,
  91. .ops = &dc_wdt_ops,
  92. .min_timeout = 1,
  93. };
  94. static int dc_wdt_probe(struct platform_device *pdev)
  95. {
  96. struct device *dev = &pdev->dev;
  97. struct device_node *np = dev->of_node;
  98. struct dc_wdt *wdt;
  99. int ret;
  100. wdt = devm_kzalloc(dev, sizeof(struct dc_wdt), GFP_KERNEL);
  101. if (!wdt)
  102. return -ENOMEM;
  103. platform_set_drvdata(pdev, wdt);
  104. wdt->base = of_iomap(np, 0);
  105. if (!wdt->base) {
  106. dev_err(dev, "Failed to remap watchdog regs");
  107. return -ENODEV;
  108. }
  109. wdt->clk = devm_clk_get(&pdev->dev, NULL);
  110. if (IS_ERR(wdt->clk)) {
  111. ret = PTR_ERR(wdt->clk);
  112. goto err_iounmap;
  113. }
  114. dc_wdt_wdd.max_timeout = U32_MAX / clk_get_rate(wdt->clk);
  115. dc_wdt_wdd.timeout = dc_wdt_wdd.max_timeout;
  116. dc_wdt_wdd.parent = &pdev->dev;
  117. spin_lock_init(&wdt->lock);
  118. watchdog_set_drvdata(&dc_wdt_wdd, wdt);
  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. wdt->restart_handler.notifier_call = dc_restart_handler;
  126. wdt->restart_handler.priority = 128;
  127. ret = register_restart_handler(&wdt->restart_handler);
  128. if (ret)
  129. dev_warn(&pdev->dev, "cannot register restart handler\n");
  130. return 0;
  131. err_iounmap:
  132. iounmap(wdt->base);
  133. return ret;
  134. }
  135. static int dc_wdt_remove(struct platform_device *pdev)
  136. {
  137. struct dc_wdt *wdt = platform_get_drvdata(pdev);
  138. unregister_restart_handler(&wdt->restart_handler);
  139. watchdog_unregister_device(&dc_wdt_wdd);
  140. iounmap(wdt->base);
  141. return 0;
  142. }
  143. static void dc_wdt_shutdown(struct platform_device *pdev)
  144. {
  145. dc_wdt_stop(&dc_wdt_wdd);
  146. }
  147. static const struct of_device_id dc_wdt_of_match[] = {
  148. { .compatible = "cnxt,cx92755-wdt", },
  149. {},
  150. };
  151. MODULE_DEVICE_TABLE(of, dc_wdt_of_match);
  152. static struct platform_driver dc_wdt_driver = {
  153. .probe = dc_wdt_probe,
  154. .remove = dc_wdt_remove,
  155. .shutdown = dc_wdt_shutdown,
  156. .driver = {
  157. .name = "digicolor-wdt",
  158. .of_match_table = dc_wdt_of_match,
  159. },
  160. };
  161. module_platform_driver(dc_wdt_driver);
  162. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  163. MODULE_DESCRIPTION("Driver for Conexant Digicolor watchdog timer");
  164. MODULE_LICENSE("GPL");