max63xx_wdt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/watchdog.h>
  23. #include <linux/init.h>
  24. #include <linux/bitops.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/io.h>
  29. #include <linux/device.h>
  30. #include <linux/slab.h>
  31. #define DEFAULT_HEARTBEAT 60
  32. #define MAX_HEARTBEAT 60
  33. static int heartbeat = DEFAULT_HEARTBEAT;
  34. static int nowayout = WATCHDOG_NOWAYOUT;
  35. /*
  36. * Memory mapping: a single byte, 3 first lower bits to select bit 3
  37. * to ping the watchdog.
  38. */
  39. #define MAX6369_WDSET (7 << 0)
  40. #define MAX6369_WDI (1 << 3)
  41. static DEFINE_SPINLOCK(io_lock);
  42. static unsigned long wdt_status;
  43. #define WDT_IN_USE 0
  44. #define WDT_RUNNING 1
  45. #define WDT_OK_TO_CLOSE 2
  46. static int nodelay;
  47. static struct resource *wdt_mem;
  48. static void __iomem *wdt_base;
  49. static struct platform_device *max63xx_pdev;
  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. u8 wdset;
  65. u8 tdelay;
  66. u8 twd;
  67. };
  68. static struct max63xx_timeout max6369_table[] = {
  69. { 5, 1, 1 },
  70. { 6, 10, 10 },
  71. { 7, 60, 60 },
  72. { },
  73. };
  74. static struct max63xx_timeout max6371_table[] = {
  75. { 6, 60, 3 },
  76. { 7, 60, 60 },
  77. { },
  78. };
  79. static 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 *current_timeout;
  88. static struct max63xx_timeout *
  89. max63xx_select_timeout(struct max63xx_timeout *table, int value)
  90. {
  91. while (table->twd) {
  92. if (value <= table->twd) {
  93. if (nodelay && table->tdelay == 0)
  94. return table;
  95. if (!nodelay)
  96. return table;
  97. }
  98. table++;
  99. }
  100. return NULL;
  101. }
  102. static void max63xx_wdt_ping(void)
  103. {
  104. u8 val;
  105. spin_lock(&io_lock);
  106. val = __raw_readb(wdt_base);
  107. __raw_writeb(val | MAX6369_WDI, wdt_base);
  108. __raw_writeb(val & ~MAX6369_WDI, wdt_base);
  109. spin_unlock(&io_lock);
  110. }
  111. static void max63xx_wdt_enable(struct max63xx_timeout *entry)
  112. {
  113. u8 val;
  114. if (test_and_set_bit(WDT_RUNNING, &wdt_status))
  115. return;
  116. spin_lock(&io_lock);
  117. val = __raw_readb(wdt_base);
  118. val &= ~MAX6369_WDSET;
  119. val |= entry->wdset;
  120. __raw_writeb(val, wdt_base);
  121. spin_unlock(&io_lock);
  122. /* check for a edge triggered startup */
  123. if (entry->tdelay == 0)
  124. max63xx_wdt_ping();
  125. }
  126. static void max63xx_wdt_disable(void)
  127. {
  128. u8 val;
  129. spin_lock(&io_lock);
  130. val = __raw_readb(wdt_base);
  131. val &= ~MAX6369_WDSET;
  132. val |= 3;
  133. __raw_writeb(val, wdt_base);
  134. spin_unlock(&io_lock);
  135. clear_bit(WDT_RUNNING, &wdt_status);
  136. }
  137. static int max63xx_wdt_open(struct inode *inode, struct file *file)
  138. {
  139. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  140. return -EBUSY;
  141. max63xx_wdt_enable(current_timeout);
  142. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  143. return nonseekable_open(inode, file);
  144. }
  145. static ssize_t max63xx_wdt_write(struct file *file, const char *data,
  146. size_t len, loff_t *ppos)
  147. {
  148. if (len) {
  149. if (!nowayout) {
  150. size_t i;
  151. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  152. for (i = 0; i != len; i++) {
  153. char c;
  154. if (get_user(c, data + i))
  155. return -EFAULT;
  156. if (c == 'V')
  157. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  158. }
  159. }
  160. max63xx_wdt_ping();
  161. }
  162. return len;
  163. }
  164. static const struct watchdog_info ident = {
  165. .options = WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
  166. .identity = "max63xx Watchdog",
  167. };
  168. static long max63xx_wdt_ioctl(struct file *file, unsigned int cmd,
  169. unsigned long arg)
  170. {
  171. int ret = -ENOTTY;
  172. switch (cmd) {
  173. case WDIOC_GETSUPPORT:
  174. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  175. sizeof(ident)) ? -EFAULT : 0;
  176. break;
  177. case WDIOC_GETSTATUS:
  178. case WDIOC_GETBOOTSTATUS:
  179. ret = put_user(0, (int *)arg);
  180. break;
  181. case WDIOC_KEEPALIVE:
  182. max63xx_wdt_ping();
  183. ret = 0;
  184. break;
  185. case WDIOC_GETTIMEOUT:
  186. ret = put_user(heartbeat, (int *)arg);
  187. break;
  188. }
  189. return ret;
  190. }
  191. static int max63xx_wdt_release(struct inode *inode, struct file *file)
  192. {
  193. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
  194. max63xx_wdt_disable();
  195. else
  196. dev_crit(&max63xx_pdev->dev,
  197. "device closed unexpectedly - timer will not stop\n");
  198. clear_bit(WDT_IN_USE, &wdt_status);
  199. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  200. return 0;
  201. }
  202. static const struct file_operations max63xx_wdt_fops = {
  203. .owner = THIS_MODULE,
  204. .llseek = no_llseek,
  205. .write = max63xx_wdt_write,
  206. .unlocked_ioctl = max63xx_wdt_ioctl,
  207. .open = max63xx_wdt_open,
  208. .release = max63xx_wdt_release,
  209. };
  210. static struct miscdevice max63xx_wdt_miscdev = {
  211. .minor = WATCHDOG_MINOR,
  212. .name = "watchdog",
  213. .fops = &max63xx_wdt_fops,
  214. };
  215. static int __devinit max63xx_wdt_probe(struct platform_device *pdev)
  216. {
  217. int ret = 0;
  218. int size;
  219. struct device *dev = &pdev->dev;
  220. struct max63xx_timeout *table;
  221. table = (struct max63xx_timeout *)pdev->id_entry->driver_data;
  222. if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
  223. heartbeat = DEFAULT_HEARTBEAT;
  224. dev_info(dev, "requesting %ds heartbeat\n", heartbeat);
  225. current_timeout = max63xx_select_timeout(table, heartbeat);
  226. if (!current_timeout) {
  227. dev_err(dev, "unable to satisfy heartbeat request\n");
  228. return -EINVAL;
  229. }
  230. dev_info(dev, "using %ds heartbeat with %ds initial delay\n",
  231. current_timeout->twd, current_timeout->tdelay);
  232. heartbeat = current_timeout->twd;
  233. max63xx_pdev = pdev;
  234. wdt_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  235. if (wdt_mem == NULL) {
  236. dev_err(dev, "failed to get memory region resource\n");
  237. return -ENOENT;
  238. }
  239. size = resource_size(wdt_mem);
  240. if (!request_mem_region(wdt_mem->start, size, pdev->name)) {
  241. dev_err(dev, "failed to get memory region\n");
  242. return -ENOENT;
  243. }
  244. wdt_base = ioremap(wdt_mem->start, size);
  245. if (!wdt_base) {
  246. dev_err(dev, "failed to map memory region\n");
  247. ret = -ENOMEM;
  248. goto out_request;
  249. }
  250. ret = misc_register(&max63xx_wdt_miscdev);
  251. if (ret < 0) {
  252. dev_err(dev, "cannot register misc device\n");
  253. goto out_unmap;
  254. }
  255. return 0;
  256. out_unmap:
  257. iounmap(wdt_base);
  258. out_request:
  259. release_mem_region(wdt_mem->start, size);
  260. wdt_mem = NULL;
  261. return ret;
  262. }
  263. static int __devexit max63xx_wdt_remove(struct platform_device *pdev)
  264. {
  265. misc_deregister(&max63xx_wdt_miscdev);
  266. if (wdt_mem) {
  267. release_mem_region(wdt_mem->start, resource_size(wdt_mem));
  268. wdt_mem = NULL;
  269. }
  270. if (wdt_base)
  271. iounmap(wdt_base);
  272. return 0;
  273. }
  274. static struct platform_device_id max63xx_id_table[] = {
  275. { "max6369_wdt", (kernel_ulong_t)max6369_table, },
  276. { "max6370_wdt", (kernel_ulong_t)max6369_table, },
  277. { "max6371_wdt", (kernel_ulong_t)max6371_table, },
  278. { "max6372_wdt", (kernel_ulong_t)max6371_table, },
  279. { "max6373_wdt", (kernel_ulong_t)max6373_table, },
  280. { "max6374_wdt", (kernel_ulong_t)max6373_table, },
  281. { },
  282. };
  283. MODULE_DEVICE_TABLE(platform, max63xx_id_table);
  284. static struct platform_driver max63xx_wdt_driver = {
  285. .probe = max63xx_wdt_probe,
  286. .remove = __devexit_p(max63xx_wdt_remove),
  287. .id_table = max63xx_id_table,
  288. .driver = {
  289. .name = "max63xx_wdt",
  290. .owner = THIS_MODULE,
  291. },
  292. };
  293. static int __init max63xx_wdt_init(void)
  294. {
  295. return platform_driver_register(&max63xx_wdt_driver);
  296. }
  297. static void __exit max63xx_wdt_exit(void)
  298. {
  299. platform_driver_unregister(&max63xx_wdt_driver);
  300. }
  301. module_init(max63xx_wdt_init);
  302. module_exit(max63xx_wdt_exit);
  303. MODULE_AUTHOR("Marc Zyngier <maz@misterjones.org>");
  304. MODULE_DESCRIPTION("max63xx Watchdog Driver");
  305. module_param(heartbeat, int, 0);
  306. MODULE_PARM_DESC(heartbeat,
  307. "Watchdog heartbeat period in seconds from 1 to "
  308. __MODULE_STRING(MAX_HEARTBEAT) ", default "
  309. __MODULE_STRING(DEFAULT_HEARTBEAT));
  310. module_param(nowayout, int, 0);
  311. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  312. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  313. module_param(nodelay, int, 0);
  314. MODULE_PARM_DESC(nodelay,
  315. "Force selection of a timeout setting without initial delay "
  316. "(max6373/74 only, default=0)");
  317. MODULE_LICENSE("GPL");
  318. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);