jz4740_wdt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
  3. * JZ4740 Watchdog driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/watchdog.h>
  21. #include <linux/init.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/io.h>
  24. #include <linux/device.h>
  25. #include <linux/clk.h>
  26. #include <linux/slab.h>
  27. #include <linux/err.h>
  28. #include <asm/mach-jz4740/timer.h>
  29. #define JZ_REG_WDT_TIMER_DATA 0x0
  30. #define JZ_REG_WDT_COUNTER_ENABLE 0x4
  31. #define JZ_REG_WDT_TIMER_COUNTER 0x8
  32. #define JZ_REG_WDT_TIMER_CONTROL 0xC
  33. #define JZ_WDT_CLOCK_PCLK 0x1
  34. #define JZ_WDT_CLOCK_RTC 0x2
  35. #define JZ_WDT_CLOCK_EXT 0x4
  36. #define JZ_WDT_CLOCK_DIV_SHIFT 3
  37. #define JZ_WDT_CLOCK_DIV_1 (0 << JZ_WDT_CLOCK_DIV_SHIFT)
  38. #define JZ_WDT_CLOCK_DIV_4 (1 << JZ_WDT_CLOCK_DIV_SHIFT)
  39. #define JZ_WDT_CLOCK_DIV_16 (2 << JZ_WDT_CLOCK_DIV_SHIFT)
  40. #define JZ_WDT_CLOCK_DIV_64 (3 << JZ_WDT_CLOCK_DIV_SHIFT)
  41. #define JZ_WDT_CLOCK_DIV_256 (4 << JZ_WDT_CLOCK_DIV_SHIFT)
  42. #define JZ_WDT_CLOCK_DIV_1024 (5 << JZ_WDT_CLOCK_DIV_SHIFT)
  43. #define DEFAULT_HEARTBEAT 5
  44. #define MAX_HEARTBEAT 2048
  45. static bool nowayout = WATCHDOG_NOWAYOUT;
  46. module_param(nowayout, bool, 0);
  47. MODULE_PARM_DESC(nowayout,
  48. "Watchdog cannot be stopped once started (default="
  49. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  50. static unsigned int heartbeat = DEFAULT_HEARTBEAT;
  51. module_param(heartbeat, uint, 0);
  52. MODULE_PARM_DESC(heartbeat,
  53. "Watchdog heartbeat period in seconds from 1 to "
  54. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  55. __MODULE_STRING(DEFAULT_HEARTBEAT));
  56. struct jz4740_wdt_drvdata {
  57. struct watchdog_device wdt;
  58. void __iomem *base;
  59. struct clk *rtc_clk;
  60. };
  61. static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
  62. {
  63. struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  64. writew(0x0, drvdata->base + JZ_REG_WDT_TIMER_COUNTER);
  65. return 0;
  66. }
  67. static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
  68. unsigned int new_timeout)
  69. {
  70. struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  71. unsigned int rtc_clk_rate;
  72. unsigned int timeout_value;
  73. unsigned short clock_div = JZ_WDT_CLOCK_DIV_1;
  74. rtc_clk_rate = clk_get_rate(drvdata->rtc_clk);
  75. timeout_value = rtc_clk_rate * new_timeout;
  76. while (timeout_value > 0xffff) {
  77. if (clock_div == JZ_WDT_CLOCK_DIV_1024) {
  78. /* Requested timeout too high;
  79. * use highest possible value. */
  80. timeout_value = 0xffff;
  81. break;
  82. }
  83. timeout_value >>= 2;
  84. clock_div += (1 << JZ_WDT_CLOCK_DIV_SHIFT);
  85. }
  86. writeb(0x0, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
  87. writew(clock_div, drvdata->base + JZ_REG_WDT_TIMER_CONTROL);
  88. writew((u16)timeout_value, drvdata->base + JZ_REG_WDT_TIMER_DATA);
  89. writew(0x0, drvdata->base + JZ_REG_WDT_TIMER_COUNTER);
  90. writew(clock_div | JZ_WDT_CLOCK_RTC,
  91. drvdata->base + JZ_REG_WDT_TIMER_CONTROL);
  92. writeb(0x1, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
  93. wdt_dev->timeout = new_timeout;
  94. return 0;
  95. }
  96. static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
  97. {
  98. jz4740_timer_enable_watchdog();
  99. jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
  100. return 0;
  101. }
  102. static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
  103. {
  104. struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
  105. jz4740_timer_disable_watchdog();
  106. writeb(0x0, drvdata->base + JZ_REG_WDT_COUNTER_ENABLE);
  107. return 0;
  108. }
  109. static const struct watchdog_info jz4740_wdt_info = {
  110. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  111. .identity = "jz4740 Watchdog",
  112. };
  113. static const struct watchdog_ops jz4740_wdt_ops = {
  114. .owner = THIS_MODULE,
  115. .start = jz4740_wdt_start,
  116. .stop = jz4740_wdt_stop,
  117. .ping = jz4740_wdt_ping,
  118. .set_timeout = jz4740_wdt_set_timeout,
  119. };
  120. static int __devinit jz4740_wdt_probe(struct platform_device *pdev)
  121. {
  122. struct jz4740_wdt_drvdata *drvdata;
  123. struct watchdog_device *jz4740_wdt;
  124. struct resource *res;
  125. int ret;
  126. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct jz4740_wdt_drvdata),
  127. GFP_KERNEL);
  128. if (!drvdata) {
  129. dev_err(&pdev->dev, "Unable to alloacate watchdog device\n");
  130. return -ENOMEM;
  131. }
  132. if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
  133. heartbeat = DEFAULT_HEARTBEAT;
  134. jz4740_wdt = &drvdata->wdt;
  135. jz4740_wdt->info = &jz4740_wdt_info;
  136. jz4740_wdt->ops = &jz4740_wdt_ops;
  137. jz4740_wdt->timeout = heartbeat;
  138. jz4740_wdt->min_timeout = 1;
  139. jz4740_wdt->max_timeout = MAX_HEARTBEAT;
  140. watchdog_set_nowayout(jz4740_wdt, nowayout);
  141. watchdog_set_drvdata(jz4740_wdt, drvdata);
  142. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  143. drvdata->base = devm_request_and_ioremap(&pdev->dev, res);
  144. if (drvdata->base == NULL) {
  145. ret = -EBUSY;
  146. goto err_out;
  147. }
  148. drvdata->rtc_clk = clk_get(NULL, "rtc");
  149. if (IS_ERR(drvdata->rtc_clk)) {
  150. dev_err(&pdev->dev, "cannot find RTC clock\n");
  151. ret = PTR_ERR(drvdata->rtc_clk);
  152. goto err_out;
  153. }
  154. ret = watchdog_register_device(&drvdata->wdt);
  155. if (ret < 0)
  156. goto err_disable_clk;
  157. platform_set_drvdata(pdev, drvdata);
  158. return 0;
  159. err_disable_clk:
  160. clk_put(drvdata->rtc_clk);
  161. err_out:
  162. return ret;
  163. }
  164. static int __devexit jz4740_wdt_remove(struct platform_device *pdev)
  165. {
  166. struct jz4740_wdt_drvdata *drvdata = platform_get_drvdata(pdev);
  167. jz4740_wdt_stop(&drvdata->wdt);
  168. watchdog_unregister_device(&drvdata->wdt);
  169. clk_put(drvdata->rtc_clk);
  170. return 0;
  171. }
  172. static struct platform_driver jz4740_wdt_driver = {
  173. .probe = jz4740_wdt_probe,
  174. .remove = __devexit_p(jz4740_wdt_remove),
  175. .driver = {
  176. .name = "jz4740-wdt",
  177. .owner = THIS_MODULE,
  178. },
  179. };
  180. module_platform_driver(jz4740_wdt_driver);
  181. MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
  182. MODULE_DESCRIPTION("jz4740 Watchdog Driver");
  183. MODULE_LICENSE("GPL");
  184. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  185. MODULE_ALIAS("platform:jz4740-wdt");