max63xx_wdt.c 7.0 KB

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