intel-mid_wdt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * intel-mid_wdt: generic Intel MID SCU watchdog driver
  3. *
  4. * Platforms supported so far:
  5. * - Merrifield only
  6. *
  7. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  8. * Contact: David Cohen <david.a.cohen@linux.intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of version 2 of the GNU General
  12. * Public License as published by the Free Software Foundation.
  13. */
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/nmi.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/watchdog.h>
  19. #include <linux/platform_data/intel-mid_wdt.h>
  20. #include <asm/intel_scu_ipc.h>
  21. #include <asm/intel-mid.h>
  22. #define IPC_WATCHDOG 0xf8
  23. #define MID_WDT_PRETIMEOUT 15
  24. #define MID_WDT_TIMEOUT_MIN (1 + MID_WDT_PRETIMEOUT)
  25. #define MID_WDT_TIMEOUT_MAX 170
  26. #define MID_WDT_DEFAULT_TIMEOUT 90
  27. /* SCU watchdog messages */
  28. enum {
  29. SCU_WATCHDOG_START = 0,
  30. SCU_WATCHDOG_STOP,
  31. SCU_WATCHDOG_KEEPALIVE,
  32. };
  33. static inline int wdt_command(int sub, u32 *in, int inlen)
  34. {
  35. return intel_scu_ipc_command(IPC_WATCHDOG, sub, in, inlen, NULL, 0);
  36. }
  37. static int wdt_start(struct watchdog_device *wd)
  38. {
  39. int ret, in_size;
  40. int timeout = wd->timeout;
  41. struct ipc_wd_start {
  42. u32 pretimeout;
  43. u32 timeout;
  44. } ipc_wd_start = { timeout - MID_WDT_PRETIMEOUT, timeout };
  45. /*
  46. * SCU expects the input size for watchdog IPC to
  47. * be based on 4 bytes
  48. */
  49. in_size = DIV_ROUND_UP(sizeof(ipc_wd_start), 4);
  50. ret = wdt_command(SCU_WATCHDOG_START, (u32 *)&ipc_wd_start, in_size);
  51. if (ret) {
  52. struct device *dev = watchdog_get_drvdata(wd);
  53. dev_crit(dev, "error starting watchdog: %d\n", ret);
  54. }
  55. return ret;
  56. }
  57. static int wdt_ping(struct watchdog_device *wd)
  58. {
  59. int ret;
  60. ret = wdt_command(SCU_WATCHDOG_KEEPALIVE, NULL, 0);
  61. if (ret) {
  62. struct device *dev = watchdog_get_drvdata(wd);
  63. dev_crit(dev, "Error executing keepalive: 0x%x\n", ret);
  64. }
  65. return ret;
  66. }
  67. static int wdt_stop(struct watchdog_device *wd)
  68. {
  69. int ret;
  70. ret = wdt_command(SCU_WATCHDOG_STOP, NULL, 0);
  71. if (ret) {
  72. struct device *dev = watchdog_get_drvdata(wd);
  73. dev_crit(dev, "Error stopping watchdog: 0x%x\n", ret);
  74. }
  75. return ret;
  76. }
  77. static irqreturn_t mid_wdt_irq(int irq, void *dev_id)
  78. {
  79. panic("Kernel Watchdog");
  80. /* This code should not be reached */
  81. return IRQ_HANDLED;
  82. }
  83. static const struct watchdog_info mid_wdt_info = {
  84. .identity = "Intel MID SCU watchdog",
  85. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  86. };
  87. static const struct watchdog_ops mid_wdt_ops = {
  88. .owner = THIS_MODULE,
  89. .start = wdt_start,
  90. .stop = wdt_stop,
  91. .ping = wdt_ping,
  92. };
  93. static int mid_wdt_probe(struct platform_device *pdev)
  94. {
  95. struct watchdog_device *wdt_dev;
  96. struct intel_mid_wdt_pdata *pdata = pdev->dev.platform_data;
  97. int ret;
  98. if (!pdata) {
  99. dev_err(&pdev->dev, "missing platform data\n");
  100. return -EINVAL;
  101. }
  102. if (pdata->probe) {
  103. ret = pdata->probe(pdev);
  104. if (ret)
  105. return ret;
  106. }
  107. wdt_dev = devm_kzalloc(&pdev->dev, sizeof(*wdt_dev), GFP_KERNEL);
  108. if (!wdt_dev)
  109. return -ENOMEM;
  110. wdt_dev->info = &mid_wdt_info;
  111. wdt_dev->ops = &mid_wdt_ops;
  112. wdt_dev->min_timeout = MID_WDT_TIMEOUT_MIN;
  113. wdt_dev->max_timeout = MID_WDT_TIMEOUT_MAX;
  114. wdt_dev->timeout = MID_WDT_DEFAULT_TIMEOUT;
  115. wdt_dev->parent = &pdev->dev;
  116. watchdog_set_drvdata(wdt_dev, &pdev->dev);
  117. platform_set_drvdata(pdev, wdt_dev);
  118. ret = devm_request_irq(&pdev->dev, pdata->irq, mid_wdt_irq,
  119. IRQF_SHARED | IRQF_NO_SUSPEND, "watchdog",
  120. wdt_dev);
  121. if (ret) {
  122. dev_err(&pdev->dev, "error requesting warning irq %d\n",
  123. pdata->irq);
  124. return ret;
  125. }
  126. ret = watchdog_register_device(wdt_dev);
  127. if (ret) {
  128. dev_err(&pdev->dev, "error registering watchdog device\n");
  129. return ret;
  130. }
  131. dev_info(&pdev->dev, "Intel MID watchdog device probed\n");
  132. return 0;
  133. }
  134. static int mid_wdt_remove(struct platform_device *pdev)
  135. {
  136. struct watchdog_device *wd = platform_get_drvdata(pdev);
  137. watchdog_unregister_device(wd);
  138. return 0;
  139. }
  140. static struct platform_driver mid_wdt_driver = {
  141. .probe = mid_wdt_probe,
  142. .remove = mid_wdt_remove,
  143. .driver = {
  144. .name = "intel_mid_wdt",
  145. },
  146. };
  147. module_platform_driver(mid_wdt_driver);
  148. MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
  149. MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
  150. MODULE_LICENSE("GPL");