menf21bmc_wdt.c 5.4 KB

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