intel-mid_wdt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * intel-mid_wdt: generic Intel MID SCU watchdog driver
  4. *
  5. * Platforms supported so far:
  6. * - Merrifield only
  7. *
  8. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  9. * Contact: David Cohen <david.a.cohen@linux.intel.com>
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/nmi.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/watchdog.h>
  16. #include <linux/platform_data/intel-mid_wdt.h>
  17. #include <asm/intel_scu_ipc.h>
  18. #include <asm/intel-mid.h>
  19. #define IPC_WATCHDOG 0xf8
  20. #define MID_WDT_PRETIMEOUT 15
  21. #define MID_WDT_TIMEOUT_MIN (1 + MID_WDT_PRETIMEOUT)
  22. #define MID_WDT_TIMEOUT_MAX 170
  23. #define MID_WDT_DEFAULT_TIMEOUT 90
  24. /* SCU watchdog messages */
  25. enum {
  26. SCU_WATCHDOG_START = 0,
  27. SCU_WATCHDOG_STOP,
  28. SCU_WATCHDOG_KEEPALIVE,
  29. };
  30. static inline int wdt_command(int sub, u32 *in, int inlen)
  31. {
  32. return intel_scu_ipc_command(IPC_WATCHDOG, sub, in, inlen, NULL, 0);
  33. }
  34. static int wdt_start(struct watchdog_device *wd)
  35. {
  36. struct device *dev = watchdog_get_drvdata(wd);
  37. int ret, in_size;
  38. int timeout = wd->timeout;
  39. struct ipc_wd_start {
  40. u32 pretimeout;
  41. u32 timeout;
  42. } ipc_wd_start = { timeout - MID_WDT_PRETIMEOUT, timeout };
  43. /*
  44. * SCU expects the input size for watchdog IPC to
  45. * be based on 4 bytes
  46. */
  47. in_size = DIV_ROUND_UP(sizeof(ipc_wd_start), 4);
  48. ret = wdt_command(SCU_WATCHDOG_START, (u32 *)&ipc_wd_start, in_size);
  49. if (ret)
  50. dev_crit(dev, "error starting watchdog: %d\n", ret);
  51. return ret;
  52. }
  53. static int wdt_ping(struct watchdog_device *wd)
  54. {
  55. struct device *dev = watchdog_get_drvdata(wd);
  56. int ret;
  57. ret = wdt_command(SCU_WATCHDOG_KEEPALIVE, NULL, 0);
  58. if (ret)
  59. dev_crit(dev, "Error executing keepalive: %d\n", ret);
  60. return ret;
  61. }
  62. static int wdt_stop(struct watchdog_device *wd)
  63. {
  64. struct device *dev = watchdog_get_drvdata(wd);
  65. int ret;
  66. ret = wdt_command(SCU_WATCHDOG_STOP, NULL, 0);
  67. if (ret)
  68. dev_crit(dev, "Error stopping watchdog: %d\n", ret);
  69. return ret;
  70. }
  71. static irqreturn_t mid_wdt_irq(int irq, void *dev_id)
  72. {
  73. panic("Kernel Watchdog");
  74. /* This code should not be reached */
  75. return IRQ_HANDLED;
  76. }
  77. static const struct watchdog_info mid_wdt_info = {
  78. .identity = "Intel MID SCU watchdog",
  79. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  80. };
  81. static const struct watchdog_ops mid_wdt_ops = {
  82. .owner = THIS_MODULE,
  83. .start = wdt_start,
  84. .stop = wdt_stop,
  85. .ping = wdt_ping,
  86. };
  87. static int mid_wdt_probe(struct platform_device *pdev)
  88. {
  89. struct device *dev = &pdev->dev;
  90. struct watchdog_device *wdt_dev;
  91. struct intel_mid_wdt_pdata *pdata = dev->platform_data;
  92. int ret;
  93. if (!pdata) {
  94. dev_err(dev, "missing platform data\n");
  95. return -EINVAL;
  96. }
  97. if (pdata->probe) {
  98. ret = pdata->probe(pdev);
  99. if (ret)
  100. return ret;
  101. }
  102. wdt_dev = devm_kzalloc(dev, sizeof(*wdt_dev), GFP_KERNEL);
  103. if (!wdt_dev)
  104. return -ENOMEM;
  105. wdt_dev->info = &mid_wdt_info;
  106. wdt_dev->ops = &mid_wdt_ops;
  107. wdt_dev->min_timeout = MID_WDT_TIMEOUT_MIN;
  108. wdt_dev->max_timeout = MID_WDT_TIMEOUT_MAX;
  109. wdt_dev->timeout = MID_WDT_DEFAULT_TIMEOUT;
  110. wdt_dev->parent = dev;
  111. watchdog_set_drvdata(wdt_dev, dev);
  112. ret = devm_request_irq(dev, pdata->irq, mid_wdt_irq,
  113. IRQF_SHARED | IRQF_NO_SUSPEND, "watchdog",
  114. wdt_dev);
  115. if (ret) {
  116. dev_err(dev, "error requesting warning irq %d\n", pdata->irq);
  117. return ret;
  118. }
  119. /*
  120. * The firmware followed by U-Boot leaves the watchdog running
  121. * with the default threshold which may vary. When we get here
  122. * we should make a decision to prevent any side effects before
  123. * user space daemon will take care of it. The best option,
  124. * taking into consideration that there is no way to read values
  125. * back from hardware, is to enforce watchdog being run with
  126. * deterministic values.
  127. */
  128. ret = wdt_start(wdt_dev);
  129. if (ret)
  130. return ret;
  131. /* Make sure the watchdog is serviced */
  132. set_bit(WDOG_HW_RUNNING, &wdt_dev->status);
  133. ret = devm_watchdog_register_device(dev, wdt_dev);
  134. if (ret)
  135. return ret;
  136. dev_info(dev, "Intel MID watchdog device probed\n");
  137. return 0;
  138. }
  139. static struct platform_driver mid_wdt_driver = {
  140. .probe = mid_wdt_probe,
  141. .driver = {
  142. .name = "intel_mid_wdt",
  143. },
  144. };
  145. module_platform_driver(mid_wdt_driver);
  146. MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
  147. MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
  148. MODULE_LICENSE("GPL");