intel-mid_wdt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. struct device *dev = watchdog_get_drvdata(wd);
  40. int ret, in_size;
  41. int timeout = wd->timeout;
  42. struct ipc_wd_start {
  43. u32 pretimeout;
  44. u32 timeout;
  45. } ipc_wd_start = { timeout - MID_WDT_PRETIMEOUT, timeout };
  46. /*
  47. * SCU expects the input size for watchdog IPC to
  48. * be based on 4 bytes
  49. */
  50. in_size = DIV_ROUND_UP(sizeof(ipc_wd_start), 4);
  51. ret = wdt_command(SCU_WATCHDOG_START, (u32 *)&ipc_wd_start, in_size);
  52. if (ret)
  53. dev_crit(dev, "error starting watchdog: %d\n", ret);
  54. return ret;
  55. }
  56. static int wdt_ping(struct watchdog_device *wd)
  57. {
  58. struct device *dev = watchdog_get_drvdata(wd);
  59. int ret;
  60. ret = wdt_command(SCU_WATCHDOG_KEEPALIVE, NULL, 0);
  61. if (ret)
  62. dev_crit(dev, "Error executing keepalive: %d\n", ret);
  63. return ret;
  64. }
  65. static int wdt_stop(struct watchdog_device *wd)
  66. {
  67. struct device *dev = watchdog_get_drvdata(wd);
  68. int ret;
  69. ret = wdt_command(SCU_WATCHDOG_STOP, NULL, 0);
  70. if (ret)
  71. dev_crit(dev, "Error stopping watchdog: %d\n", ret);
  72. return ret;
  73. }
  74. static irqreturn_t mid_wdt_irq(int irq, void *dev_id)
  75. {
  76. panic("Kernel Watchdog");
  77. /* This code should not be reached */
  78. return IRQ_HANDLED;
  79. }
  80. static const struct watchdog_info mid_wdt_info = {
  81. .identity = "Intel MID SCU watchdog",
  82. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  83. };
  84. static const struct watchdog_ops mid_wdt_ops = {
  85. .owner = THIS_MODULE,
  86. .start = wdt_start,
  87. .stop = wdt_stop,
  88. .ping = wdt_ping,
  89. };
  90. static int mid_wdt_probe(struct platform_device *pdev)
  91. {
  92. struct watchdog_device *wdt_dev;
  93. struct intel_mid_wdt_pdata *pdata = pdev->dev.platform_data;
  94. int ret;
  95. if (!pdata) {
  96. dev_err(&pdev->dev, "missing platform data\n");
  97. return -EINVAL;
  98. }
  99. if (pdata->probe) {
  100. ret = pdata->probe(pdev);
  101. if (ret)
  102. return ret;
  103. }
  104. wdt_dev = devm_kzalloc(&pdev->dev, sizeof(*wdt_dev), GFP_KERNEL);
  105. if (!wdt_dev)
  106. return -ENOMEM;
  107. wdt_dev->info = &mid_wdt_info;
  108. wdt_dev->ops = &mid_wdt_ops;
  109. wdt_dev->min_timeout = MID_WDT_TIMEOUT_MIN;
  110. wdt_dev->max_timeout = MID_WDT_TIMEOUT_MAX;
  111. wdt_dev->timeout = MID_WDT_DEFAULT_TIMEOUT;
  112. wdt_dev->parent = &pdev->dev;
  113. watchdog_set_drvdata(wdt_dev, &pdev->dev);
  114. ret = devm_request_irq(&pdev->dev, pdata->irq, mid_wdt_irq,
  115. IRQF_SHARED | IRQF_NO_SUSPEND, "watchdog",
  116. wdt_dev);
  117. if (ret) {
  118. dev_err(&pdev->dev, "error requesting warning irq %d\n",
  119. pdata->irq);
  120. return ret;
  121. }
  122. /*
  123. * The firmware followed by U-Boot leaves the watchdog running
  124. * with the default threshold which may vary. When we get here
  125. * we should make a decision to prevent any side effects before
  126. * user space daemon will take care of it. The best option,
  127. * taking into consideration that there is no way to read values
  128. * back from hardware, is to enforce watchdog being run with
  129. * deterministic values.
  130. */
  131. ret = wdt_start(wdt_dev);
  132. if (ret)
  133. return ret;
  134. /* Make sure the watchdog is serviced */
  135. set_bit(WDOG_HW_RUNNING, &wdt_dev->status);
  136. ret = devm_watchdog_register_device(&pdev->dev, wdt_dev);
  137. if (ret) {
  138. dev_err(&pdev->dev, "error registering watchdog device\n");
  139. return ret;
  140. }
  141. dev_info(&pdev->dev, "Intel MID watchdog device probed\n");
  142. return 0;
  143. }
  144. static struct platform_driver mid_wdt_driver = {
  145. .probe = mid_wdt_probe,
  146. .driver = {
  147. .name = "intel_mid_wdt",
  148. },
  149. };
  150. module_platform_driver(mid_wdt_driver);
  151. MODULE_AUTHOR("David Cohen <david.a.cohen@linux.intel.com>");
  152. MODULE_DESCRIPTION("Watchdog Driver for Intel MID platform");
  153. MODULE_LICENSE("GPL");