max63xx_wdt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * drivers/char/watchdog/max63xx_wdt.c
  3. *
  4. * Driver for max63{69,70,71,72,73,74} watchdog timers
  5. *
  6. * Copyright (C) 2009 Marc Zyngier <maz@misterjones.org>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. *
  12. * This driver assumes the watchdog pins are memory mapped (as it is
  13. * the case for the Arcom Zeus). Should it be connected over GPIOs or
  14. * another interface, some abstraction will have to be introduced.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/watchdog.h>
  23. #include <linux/bitops.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/io.h>
  27. #include <linux/slab.h>
  28. #define DEFAULT_HEARTBEAT 60
  29. #define MAX_HEARTBEAT 60
  30. static unsigned int heartbeat = DEFAULT_HEARTBEAT;
  31. static bool nowayout = WATCHDOG_NOWAYOUT;
  32. /*
  33. * Memory mapping: a single byte, 3 first lower bits to select bit 3
  34. * to ping the watchdog.
  35. */
  36. #define MAX6369_WDSET (7 << 0)
  37. #define MAX6369_WDI (1 << 3)
  38. #define MAX6369_WDSET_DISABLED 3
  39. static int nodelay;
  40. struct max63xx_wdt {
  41. struct watchdog_device wdd;
  42. const struct max63xx_timeout *timeout;
  43. /* memory mapping */
  44. void __iomem *base;
  45. spinlock_t lock;
  46. /* WDI and WSET bits write access routines */
  47. void (*ping)(struct max63xx_wdt *wdt);
  48. void (*set)(struct max63xx_wdt *wdt, u8 set);
  49. };
  50. /*
  51. * The timeout values used are actually the absolute minimum the chip
  52. * offers. Typical values on my board are slightly over twice as long
  53. * (10s setting ends up with a 25s timeout), and can be up to 3 times
  54. * the nominal setting (according to the datasheet). So please take
  55. * these values with a grain of salt. Same goes for the initial delay
  56. * "feature". Only max6373/74 have a few settings without this initial
  57. * delay (selected with the "nodelay" parameter).
  58. *
  59. * I also decided to remove from the tables any timeout smaller than a
  60. * second, as it looked completly overkill...
  61. */
  62. /* Timeouts in second */
  63. struct max63xx_timeout {
  64. const u8 wdset;
  65. const u8 tdelay;
  66. const u8 twd;
  67. };
  68. static const struct max63xx_timeout max6369_table[] = {
  69. { 5, 1, 1 },
  70. { 6, 10, 10 },
  71. { 7, 60, 60 },
  72. { },
  73. };
  74. static const struct max63xx_timeout max6371_table[] = {
  75. { 6, 60, 3 },
  76. { 7, 60, 60 },
  77. { },
  78. };
  79. static const struct max63xx_timeout max6373_table[] = {
  80. { 2, 60, 1 },
  81. { 5, 0, 1 },
  82. { 1, 3, 3 },
  83. { 7, 60, 10 },
  84. { 6, 0, 10 },
  85. { },
  86. };
  87. static struct max63xx_timeout *
  88. max63xx_select_timeout(struct max63xx_timeout *table, int value)
  89. {
  90. while (table->twd) {
  91. if (value <= table->twd) {
  92. if (nodelay && table->tdelay == 0)
  93. return table;
  94. if (!nodelay)
  95. return table;
  96. }
  97. table++;
  98. }
  99. return NULL;
  100. }
  101. static int max63xx_wdt_ping(struct watchdog_device *wdd)
  102. {
  103. struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
  104. wdt->ping(wdt);
  105. return 0;
  106. }
  107. static int max63xx_wdt_start(struct watchdog_device *wdd)
  108. {
  109. struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
  110. wdt->set(wdt, wdt->timeout->wdset);
  111. /* check for a edge triggered startup */
  112. if (wdt->timeout->tdelay == 0)
  113. wdt->ping(wdt);
  114. return 0;
  115. }
  116. static int max63xx_wdt_stop(struct watchdog_device *wdd)
  117. {
  118. struct max63xx_wdt *wdt = watchdog_get_drvdata(wdd);
  119. wdt->set(wdt, MAX6369_WDSET_DISABLED);
  120. return 0;
  121. }
  122. static const struct watchdog_ops max63xx_wdt_ops = {
  123. .owner = THIS_MODULE,
  124. .start = max63xx_wdt_start,
  125. .stop = max63xx_wdt_stop,
  126. .ping = max63xx_wdt_ping,
  127. };
  128. static const struct watchdog_info max63xx_wdt_info = {
  129. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  130. .identity = "max63xx Watchdog",
  131. };
  132. static void max63xx_mmap_ping(struct max63xx_wdt *wdt)
  133. {
  134. u8 val;
  135. spin_lock(&wdt->lock);
  136. val = __raw_readb(wdt->base);
  137. __raw_writeb(val | MAX6369_WDI, wdt->base);
  138. __raw_writeb(val & ~MAX6369_WDI, wdt->base);
  139. spin_unlock(&wdt->lock);
  140. }
  141. static void max63xx_mmap_set(struct max63xx_wdt *wdt, u8 set)
  142. {
  143. u8 val;
  144. spin_lock(&wdt->lock);
  145. val = __raw_readb(wdt->base);
  146. val &= ~MAX6369_WDSET;
  147. val |= set & MAX6369_WDSET;
  148. __raw_writeb(val, wdt->base);
  149. spin_unlock(&wdt->lock);
  150. }
  151. static int max63xx_mmap_init(struct platform_device *p, struct max63xx_wdt *wdt)
  152. {
  153. wdt->base = devm_platform_ioremap_resource(p, 0);
  154. if (IS_ERR(wdt->base))
  155. return PTR_ERR(wdt->base);
  156. spin_lock_init(&wdt->lock);
  157. wdt->ping = max63xx_mmap_ping;
  158. wdt->set = max63xx_mmap_set;
  159. return 0;
  160. }
  161. static int max63xx_wdt_probe(struct platform_device *pdev)
  162. {
  163. struct device *dev = &pdev->dev;
  164. struct max63xx_wdt *wdt;
  165. struct max63xx_timeout *table;
  166. int err;
  167. wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  168. if (!wdt)
  169. return -ENOMEM;
  170. table = (struct max63xx_timeout *)pdev->id_entry->driver_data;
  171. if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
  172. heartbeat = DEFAULT_HEARTBEAT;
  173. wdt->timeout = max63xx_select_timeout(table, heartbeat);
  174. if (!wdt->timeout) {
  175. dev_err(dev, "unable to satisfy %ds heartbeat request\n",
  176. heartbeat);
  177. return -EINVAL;
  178. }
  179. err = max63xx_mmap_init(pdev, wdt);
  180. if (err)
  181. return err;
  182. platform_set_drvdata(pdev, &wdt->wdd);
  183. watchdog_set_drvdata(&wdt->wdd, wdt);
  184. wdt->wdd.parent = dev;
  185. wdt->wdd.timeout = wdt->timeout->twd;
  186. wdt->wdd.info = &max63xx_wdt_info;
  187. wdt->wdd.ops = &max63xx_wdt_ops;
  188. watchdog_set_nowayout(&wdt->wdd, nowayout);
  189. err = devm_watchdog_register_device(dev, &wdt->wdd);
  190. if (err)
  191. return err;
  192. dev_info(dev, "using %ds heartbeat with %ds initial delay\n",
  193. wdt->timeout->twd, wdt->timeout->tdelay);
  194. return 0;
  195. }
  196. static const struct platform_device_id max63xx_id_table[] = {
  197. { "max6369_wdt", (kernel_ulong_t)max6369_table, },
  198. { "max6370_wdt", (kernel_ulong_t)max6369_table, },
  199. { "max6371_wdt", (kernel_ulong_t)max6371_table, },
  200. { "max6372_wdt", (kernel_ulong_t)max6371_table, },
  201. { "max6373_wdt", (kernel_ulong_t)max6373_table, },
  202. { "max6374_wdt", (kernel_ulong_t)max6373_table, },
  203. { },
  204. };
  205. MODULE_DEVICE_TABLE(platform, max63xx_id_table);
  206. static struct platform_driver max63xx_wdt_driver = {
  207. .probe = max63xx_wdt_probe,
  208. .id_table = max63xx_id_table,
  209. .driver = {
  210. .name = "max63xx_wdt",
  211. },
  212. };
  213. module_platform_driver(max63xx_wdt_driver);
  214. MODULE_AUTHOR("Marc Zyngier <maz@misterjones.org>");
  215. MODULE_DESCRIPTION("max63xx Watchdog Driver");
  216. module_param(heartbeat, int, 0);
  217. MODULE_PARM_DESC(heartbeat,
  218. "Watchdog heartbeat period in seconds from 1 to "
  219. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  220. __MODULE_STRING(DEFAULT_HEARTBEAT));
  221. module_param(nowayout, bool, 0);
  222. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  223. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  224. module_param(nodelay, int, 0);
  225. MODULE_PARM_DESC(nodelay,
  226. "Force selection of a timeout setting without initial delay "
  227. "(max6373/74 only, default=0)");
  228. MODULE_LICENSE("GPL v2");