EcCommands.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /** @file
  2. Board-specific EC commands.
  3. Copyright (c) 2021, Baruch Binyamin Doron
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Base.h>
  7. #include <Uefi.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/EcLib.h>
  10. #include <Library/IoLib.h>
  11. /* Notes:
  12. * - ACPI "CMDB": Writing to this offset is equivalent to sending commands.
  13. * The CMDx bytes contain the command parameters.
  14. *
  15. * TODO - Implement:
  16. * - Commands: 0x58, 0xE1 and 0xE2
  17. * - 0x51, 0x52: EC flash write?
  18. * - ACPI CMDB: 0x63 and 0x64, 0xC7
  19. * - 0x0B: Flash write (Boolean argument? Set in offset 0x0B?)
  20. *
  21. * Reversing vendor's protocols:
  22. * - Only read and write are used.
  23. * - Query, ACPI "CMDB" processing and command 58 are unused.
  24. * - Equivalent KbcPeim is an unused PPI.
  25. *
  26. * NB: Also look for potential EC library
  27. */
  28. #define EC_INDEX_IO_PORT 0x1200
  29. #define EC_INDEX_IO_HIGH_ADDR_PORT EC_INDEX_IO_PORT+1
  30. #define EC_INDEX_IO_LOW_ADDR_PORT EC_INDEX_IO_PORT+2
  31. #define EC_INDEX_IO_DATA_PORT EC_INDEX_IO_PORT+3
  32. /**
  33. Reads a byte of EC RAM.
  34. @param[in] Address Address to read
  35. @param[out] Data Data received
  36. @retval EFI_SUCCESS Command success
  37. @retval EFI_INVALID_PARAMETER Data is NULL
  38. @retval EFI_DEVICE_ERROR Command error
  39. @retval EFI_TIMEOUT Command timeout
  40. **/
  41. EFI_STATUS
  42. EcCmd90Read (
  43. IN UINT8 Address,
  44. OUT UINT8 *Data
  45. )
  46. {
  47. EFI_STATUS Status;
  48. if (Data == NULL) {
  49. return EFI_INVALID_PARAMETER;
  50. }
  51. Status = SendEcCommand (0x90);
  52. if (EFI_ERROR (Status)) {
  53. DEBUG((DEBUG_ERROR, "%a(): SendEcCommand(0x90) failed!\n", __FUNCTION__));
  54. return Status;
  55. }
  56. Status = SendEcData (Address);
  57. if (EFI_ERROR (Status)) {
  58. DEBUG((DEBUG_ERROR, "%a(): SendEcData(Address) failed!\n", __FUNCTION__));
  59. return Status;
  60. }
  61. Status = ReceiveEcData (Data);
  62. if (EFI_ERROR (Status)) {
  63. DEBUG((DEBUG_ERROR, "%a(): ReceiveEcData(Data) failed!\n", __FUNCTION__));
  64. return Status;
  65. }
  66. return EFI_SUCCESS;
  67. }
  68. /**
  69. Writes a byte of EC RAM.
  70. @param[in] Address Address to write
  71. @param[in] Data Data to write
  72. @retval EFI_SUCCESS Command success
  73. @retval EFI_DEVICE_ERROR Command error
  74. @retval EFI_TIMEOUT Command timeout
  75. **/
  76. EFI_STATUS
  77. EcCmd91Write (
  78. IN UINT8 Address,
  79. IN UINT8 Data
  80. )
  81. {
  82. EFI_STATUS Status;
  83. Status = SendEcCommand (0x91);
  84. if (EFI_ERROR (Status)) {
  85. DEBUG((DEBUG_ERROR, "%a(): SendEcCommand(0x91) failed!\n", __FUNCTION__));
  86. return Status;
  87. }
  88. Status = SendEcData (Address);
  89. if (EFI_ERROR (Status)) {
  90. DEBUG((DEBUG_ERROR, "%a(): SendEcData(Address) failed!\n", __FUNCTION__));
  91. return Status;
  92. }
  93. Status = SendEcData (Data);
  94. if (EFI_ERROR (Status)) {
  95. DEBUG((DEBUG_ERROR, "%a(): SendEcData(Data) failed!\n", __FUNCTION__));
  96. return Status;
  97. }
  98. return EFI_SUCCESS;
  99. }
  100. /**
  101. Query the EC status.
  102. @param[out] Status EC status byte
  103. @retval EFI_SUCCESS Command success
  104. @retval EFI_INVALID_PARAMETER Data is NULL
  105. @retval EFI_DEVICE_ERROR Command error
  106. @retval EFI_TIMEOUT Command timeout
  107. **/
  108. EFI_STATUS
  109. EcCmd94Query (
  110. OUT UINT8 *Data
  111. )
  112. {
  113. EFI_STATUS Status;
  114. if (Data == NULL) {
  115. return EFI_INVALID_PARAMETER;
  116. }
  117. Status = SendEcCommand (0x94);
  118. if (EFI_ERROR (Status)) {
  119. DEBUG((DEBUG_ERROR, "%a(): SendEcCommand(0x94) failed!\n", __FUNCTION__));
  120. return Status;
  121. }
  122. Status = ReceiveEcData (Data);
  123. if (EFI_ERROR (Status)) {
  124. DEBUG((DEBUG_ERROR, "%a(): ReceiveEcData(Data) failed!\n", __FUNCTION__));
  125. return Status;
  126. }
  127. return EFI_SUCCESS;
  128. }
  129. /**
  130. Reads a byte of EC (index) RAM.
  131. @param[in] Address Address to read
  132. @param[out] Data Data received
  133. **/
  134. VOID
  135. EcIdxRead (
  136. IN UINT16 Address,
  137. OUT UINT8 *Data
  138. )
  139. {
  140. if (Data == NULL) {
  141. return;
  142. }
  143. IoWrite8 (EC_INDEX_IO_HIGH_ADDR_PORT, Address >> 8);
  144. IoWrite8 (EC_INDEX_IO_LOW_ADDR_PORT, Address);
  145. *Data = IoRead8 (EC_INDEX_IO_DATA_PORT);
  146. }
  147. /**
  148. Writes a byte of EC (index) RAM.
  149. @param[in] Address Address to read
  150. @param[in] Data Data received
  151. **/
  152. VOID
  153. EcIdxWrite (
  154. IN UINT16 Address,
  155. IN UINT8 Data
  156. )
  157. {
  158. IoWrite8 (EC_INDEX_IO_HIGH_ADDR_PORT, Address >> 8);
  159. IoWrite8 (EC_INDEX_IO_LOW_ADDR_PORT, Address);
  160. IoWrite8 (EC_INDEX_IO_DATA_PORT, Data);
  161. }
  162. /**
  163. Read EC analog-digital converter.
  164. TODO: Check if ADC is valid.
  165. @param[in] Adc
  166. @param[out] DataBuffer
  167. **/
  168. VOID
  169. ReadEcAdcConverter (
  170. IN UINT8 Adc,
  171. OUT UINT16 *DataBuffer
  172. )
  173. {
  174. UINT8 AdcConvertersEnabled; // Contains some ADCs and some DACs
  175. UINT8 IdxData;
  176. if (DataBuffer == NULL) {
  177. return;
  178. }
  179. // Backup enabled ADCs
  180. EcIdxRead (0xff15, &AdcConvertersEnabled); // ADDAEN
  181. // Enable desired ADC in bitmask (not enabled by EC FW, not used by vendor FW)
  182. EcIdxWrite (0xff15, AdcConvertersEnabled | ((1 << Adc) & 0xf)); // ADDAEN
  183. // Sample the desired ADC in binary field; OR the start bit
  184. EcIdxWrite (0xff18, ((Adc << 1) & 0xf) | 1); // ADCTRL
  185. // Read the desired ADC
  186. EcIdxRead (0xff19, &IdxData); // ADCDAT
  187. *DataBuffer = (IdxData << 2);
  188. // Lower 2-bits of 10-bit ADC are in high bits of next register
  189. EcIdxRead (0xff1a, &IdxData); // ECIF
  190. *DataBuffer |= ((IdxData & 0xc0) >> 6);
  191. // Restore enabled ADCs
  192. EcIdxWrite (0xff15, AdcConvertersEnabled); // ADDAEN
  193. }