meson_wdt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Meson Watchdog Driver
  3. *
  4. * Copyright (c) 2014 Carlo Caione
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/types.h>
  23. #include <linux/watchdog.h>
  24. #define DRV_NAME "meson_wdt"
  25. #define MESON_WDT_TC 0x00
  26. #define MESON_WDT_DC_RESET (3 << 24)
  27. #define MESON_WDT_RESET 0x04
  28. #define MESON_WDT_TIMEOUT 30
  29. #define MESON_WDT_MIN_TIMEOUT 1
  30. #define MESON_SEC_TO_TC(s, c) ((s) * (c))
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. static unsigned int timeout;
  33. struct meson_wdt_data {
  34. unsigned int enable;
  35. unsigned int terminal_count_mask;
  36. unsigned int count_unit;
  37. };
  38. static struct meson_wdt_data meson6_wdt_data = {
  39. .enable = BIT(22),
  40. .terminal_count_mask = 0x3fffff,
  41. .count_unit = 100000, /* 10 us */
  42. };
  43. static struct meson_wdt_data meson8b_wdt_data = {
  44. .enable = BIT(19),
  45. .terminal_count_mask = 0xffff,
  46. .count_unit = 7812, /* 128 us */
  47. };
  48. struct meson_wdt_dev {
  49. struct watchdog_device wdt_dev;
  50. void __iomem *wdt_base;
  51. const struct meson_wdt_data *data;
  52. };
  53. static int meson_wdt_restart(struct watchdog_device *wdt_dev,
  54. unsigned long action, void *data)
  55. {
  56. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  57. u32 tc_reboot = MESON_WDT_DC_RESET;
  58. tc_reboot |= meson_wdt->data->enable;
  59. while (1) {
  60. writel(tc_reboot, meson_wdt->wdt_base + MESON_WDT_TC);
  61. mdelay(5);
  62. }
  63. return 0;
  64. }
  65. static int meson_wdt_ping(struct watchdog_device *wdt_dev)
  66. {
  67. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  68. writel(0, meson_wdt->wdt_base + MESON_WDT_RESET);
  69. return 0;
  70. }
  71. static void meson_wdt_change_timeout(struct watchdog_device *wdt_dev,
  72. unsigned int timeout)
  73. {
  74. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  75. u32 reg;
  76. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  77. reg &= ~meson_wdt->data->terminal_count_mask;
  78. reg |= MESON_SEC_TO_TC(timeout, meson_wdt->data->count_unit);
  79. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  80. }
  81. static int meson_wdt_set_timeout(struct watchdog_device *wdt_dev,
  82. unsigned int timeout)
  83. {
  84. wdt_dev->timeout = timeout;
  85. meson_wdt_change_timeout(wdt_dev, timeout);
  86. meson_wdt_ping(wdt_dev);
  87. return 0;
  88. }
  89. static int meson_wdt_stop(struct watchdog_device *wdt_dev)
  90. {
  91. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  92. u32 reg;
  93. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  94. reg &= ~meson_wdt->data->enable;
  95. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  96. return 0;
  97. }
  98. static int meson_wdt_start(struct watchdog_device *wdt_dev)
  99. {
  100. struct meson_wdt_dev *meson_wdt = watchdog_get_drvdata(wdt_dev);
  101. u32 reg;
  102. meson_wdt_change_timeout(wdt_dev, meson_wdt->wdt_dev.timeout);
  103. meson_wdt_ping(wdt_dev);
  104. reg = readl(meson_wdt->wdt_base + MESON_WDT_TC);
  105. reg |= meson_wdt->data->enable;
  106. writel(reg, meson_wdt->wdt_base + MESON_WDT_TC);
  107. return 0;
  108. }
  109. static const struct watchdog_info meson_wdt_info = {
  110. .identity = DRV_NAME,
  111. .options = WDIOF_SETTIMEOUT |
  112. WDIOF_KEEPALIVEPING |
  113. WDIOF_MAGICCLOSE,
  114. };
  115. static const struct watchdog_ops meson_wdt_ops = {
  116. .owner = THIS_MODULE,
  117. .start = meson_wdt_start,
  118. .stop = meson_wdt_stop,
  119. .ping = meson_wdt_ping,
  120. .set_timeout = meson_wdt_set_timeout,
  121. .restart = meson_wdt_restart,
  122. };
  123. static const struct of_device_id meson_wdt_dt_ids[] = {
  124. { .compatible = "amlogic,meson6-wdt", .data = &meson6_wdt_data },
  125. { .compatible = "amlogic,meson8-wdt", .data = &meson6_wdt_data },
  126. { .compatible = "amlogic,meson8b-wdt", .data = &meson8b_wdt_data },
  127. { .compatible = "amlogic,meson8m2-wdt", .data = &meson8b_wdt_data },
  128. { /* sentinel */ }
  129. };
  130. MODULE_DEVICE_TABLE(of, meson_wdt_dt_ids);
  131. static int meson_wdt_probe(struct platform_device *pdev)
  132. {
  133. struct resource *res;
  134. struct meson_wdt_dev *meson_wdt;
  135. const struct of_device_id *of_id;
  136. int err;
  137. meson_wdt = devm_kzalloc(&pdev->dev, sizeof(*meson_wdt), GFP_KERNEL);
  138. if (!meson_wdt)
  139. return -ENOMEM;
  140. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  141. meson_wdt->wdt_base = devm_ioremap_resource(&pdev->dev, res);
  142. if (IS_ERR(meson_wdt->wdt_base))
  143. return PTR_ERR(meson_wdt->wdt_base);
  144. of_id = of_match_device(meson_wdt_dt_ids, &pdev->dev);
  145. if (!of_id) {
  146. dev_err(&pdev->dev, "Unable to initialize WDT data\n");
  147. return -ENODEV;
  148. }
  149. meson_wdt->data = of_id->data;
  150. meson_wdt->wdt_dev.parent = &pdev->dev;
  151. meson_wdt->wdt_dev.info = &meson_wdt_info;
  152. meson_wdt->wdt_dev.ops = &meson_wdt_ops;
  153. meson_wdt->wdt_dev.max_timeout =
  154. meson_wdt->data->terminal_count_mask / meson_wdt->data->count_unit;
  155. meson_wdt->wdt_dev.min_timeout = MESON_WDT_MIN_TIMEOUT;
  156. meson_wdt->wdt_dev.timeout = min_t(unsigned int,
  157. MESON_WDT_TIMEOUT,
  158. meson_wdt->wdt_dev.max_timeout);
  159. watchdog_set_drvdata(&meson_wdt->wdt_dev, meson_wdt);
  160. watchdog_init_timeout(&meson_wdt->wdt_dev, timeout, &pdev->dev);
  161. watchdog_set_nowayout(&meson_wdt->wdt_dev, nowayout);
  162. watchdog_set_restart_priority(&meson_wdt->wdt_dev, 128);
  163. meson_wdt_stop(&meson_wdt->wdt_dev);
  164. watchdog_stop_on_reboot(&meson_wdt->wdt_dev);
  165. err = devm_watchdog_register_device(&pdev->dev, &meson_wdt->wdt_dev);
  166. if (err)
  167. return err;
  168. dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
  169. meson_wdt->wdt_dev.timeout, nowayout);
  170. return 0;
  171. }
  172. static struct platform_driver meson_wdt_driver = {
  173. .probe = meson_wdt_probe,
  174. .driver = {
  175. .name = DRV_NAME,
  176. .of_match_table = meson_wdt_dt_ids,
  177. },
  178. };
  179. module_platform_driver(meson_wdt_driver);
  180. module_param(timeout, uint, 0);
  181. MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
  182. module_param(nowayout, bool, 0);
  183. MODULE_PARM_DESC(nowayout,
  184. "Watchdog cannot be stopped once started (default="
  185. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  186. MODULE_LICENSE("GPL");
  187. MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  188. MODULE_DESCRIPTION("Meson Watchdog Timer Driver");