menf21bmc_wdt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MEN 14F021P00 Board Management Controller (BMC) Watchdog Driver.
  4. *
  5. * Copyright (C) 2014 MEN Mikro Elektronik Nuernberg GmbH
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/watchdog.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/i2c.h>
  13. #define DEVNAME "menf21bmc_wdt"
  14. #define BMC_CMD_WD_ON 0x11
  15. #define BMC_CMD_WD_OFF 0x12
  16. #define BMC_CMD_WD_TRIG 0x13
  17. #define BMC_CMD_WD_TIME 0x14
  18. #define BMC_CMD_WD_STATE 0x17
  19. #define BMC_WD_OFF_VAL 0x69
  20. #define BMC_CMD_RST_RSN 0x92
  21. #define BMC_WD_TIMEOUT_MIN 1 /* in sec */
  22. #define BMC_WD_TIMEOUT_MAX 6553 /* in sec */
  23. static bool nowayout = WATCHDOG_NOWAYOUT;
  24. module_param(nowayout, bool, 0);
  25. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  26. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  27. struct menf21bmc_wdt {
  28. struct watchdog_device wdt;
  29. struct i2c_client *i2c_client;
  30. };
  31. static int menf21bmc_wdt_set_bootstatus(struct menf21bmc_wdt *data)
  32. {
  33. int rst_rsn;
  34. rst_rsn = i2c_smbus_read_byte_data(data->i2c_client, BMC_CMD_RST_RSN);
  35. if (rst_rsn < 0)
  36. return rst_rsn;
  37. if (rst_rsn == 0x02)
  38. data->wdt.bootstatus |= WDIOF_CARDRESET;
  39. else if (rst_rsn == 0x05)
  40. data->wdt.bootstatus |= WDIOF_EXTERN1;
  41. else if (rst_rsn == 0x06)
  42. data->wdt.bootstatus |= WDIOF_EXTERN2;
  43. else if (rst_rsn == 0x0A)
  44. data->wdt.bootstatus |= WDIOF_POWERUNDER;
  45. return 0;
  46. }
  47. static int menf21bmc_wdt_start(struct watchdog_device *wdt)
  48. {
  49. struct menf21bmc_wdt *drv_data = watchdog_get_drvdata(wdt);
  50. return i2c_smbus_write_byte(drv_data->i2c_client, BMC_CMD_WD_ON);
  51. }
  52. static int menf21bmc_wdt_stop(struct watchdog_device *wdt)
  53. {
  54. struct menf21bmc_wdt *drv_data = watchdog_get_drvdata(wdt);
  55. return i2c_smbus_write_byte_data(drv_data->i2c_client,
  56. BMC_CMD_WD_OFF, BMC_WD_OFF_VAL);
  57. }
  58. static int
  59. menf21bmc_wdt_settimeout(struct watchdog_device *wdt, unsigned int timeout)
  60. {
  61. int ret;
  62. struct menf21bmc_wdt *drv_data = watchdog_get_drvdata(wdt);
  63. /*
  64. * BMC Watchdog does have a resolution of 100ms.
  65. * Watchdog API defines the timeout in seconds, so we have to
  66. * multiply the value.
  67. */
  68. ret = i2c_smbus_write_word_data(drv_data->i2c_client,
  69. BMC_CMD_WD_TIME, timeout * 10);
  70. if (ret < 0)
  71. return ret;
  72. wdt->timeout = timeout;
  73. return 0;
  74. }
  75. static int menf21bmc_wdt_ping(struct watchdog_device *wdt)
  76. {
  77. struct menf21bmc_wdt *drv_data = watchdog_get_drvdata(wdt);
  78. return i2c_smbus_write_byte(drv_data->i2c_client, BMC_CMD_WD_TRIG);
  79. }
  80. static const struct watchdog_info menf21bmc_wdt_info = {
  81. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  82. .identity = DEVNAME,
  83. };
  84. static const struct watchdog_ops menf21bmc_wdt_ops = {
  85. .owner = THIS_MODULE,
  86. .start = menf21bmc_wdt_start,
  87. .stop = menf21bmc_wdt_stop,
  88. .ping = menf21bmc_wdt_ping,
  89. .set_timeout = menf21bmc_wdt_settimeout,
  90. };
  91. static int menf21bmc_wdt_probe(struct platform_device *pdev)
  92. {
  93. struct device *dev = &pdev->dev;
  94. int ret, bmc_timeout;
  95. struct menf21bmc_wdt *drv_data;
  96. struct i2c_client *i2c_client = to_i2c_client(dev->parent);
  97. drv_data = devm_kzalloc(dev, sizeof(struct menf21bmc_wdt), GFP_KERNEL);
  98. if (!drv_data)
  99. return -ENOMEM;
  100. drv_data->wdt.ops = &menf21bmc_wdt_ops;
  101. drv_data->wdt.info = &menf21bmc_wdt_info;
  102. drv_data->wdt.min_timeout = BMC_WD_TIMEOUT_MIN;
  103. drv_data->wdt.max_timeout = BMC_WD_TIMEOUT_MAX;
  104. drv_data->wdt.parent = dev;
  105. drv_data->i2c_client = i2c_client;
  106. /*
  107. * Get the current wdt timeout value from the BMC because
  108. * the BMC will save the value set before if the system restarts.
  109. */
  110. bmc_timeout = i2c_smbus_read_word_data(drv_data->i2c_client,
  111. BMC_CMD_WD_TIME);
  112. if (bmc_timeout < 0) {
  113. dev_err(dev, "failed to get current WDT timeout\n");
  114. return bmc_timeout;
  115. }
  116. watchdog_init_timeout(&drv_data->wdt, bmc_timeout / 10, dev);
  117. watchdog_set_nowayout(&drv_data->wdt, nowayout);
  118. watchdog_set_drvdata(&drv_data->wdt, drv_data);
  119. platform_set_drvdata(pdev, drv_data);
  120. ret = menf21bmc_wdt_set_bootstatus(drv_data);
  121. if (ret < 0) {
  122. dev_err(dev, "failed to set Watchdog bootstatus\n");
  123. return ret;
  124. }
  125. ret = devm_watchdog_register_device(dev, &drv_data->wdt);
  126. if (ret)
  127. return ret;
  128. dev_info(dev, "MEN 14F021P00 BMC Watchdog device enabled\n");
  129. return 0;
  130. }
  131. static void menf21bmc_wdt_shutdown(struct platform_device *pdev)
  132. {
  133. struct menf21bmc_wdt *drv_data = platform_get_drvdata(pdev);
  134. i2c_smbus_write_word_data(drv_data->i2c_client,
  135. BMC_CMD_WD_OFF, BMC_WD_OFF_VAL);
  136. }
  137. static struct platform_driver menf21bmc_wdt = {
  138. .driver = {
  139. .name = DEVNAME,
  140. },
  141. .probe = menf21bmc_wdt_probe,
  142. .shutdown = menf21bmc_wdt_shutdown,
  143. };
  144. module_platform_driver(menf21bmc_wdt);
  145. MODULE_DESCRIPTION("MEN 14F021P00 BMC Watchdog driver");
  146. MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
  147. MODULE_LICENSE("GPL v2");
  148. MODULE_ALIAS("platform:menf21bmc_wdt");