EcCommands.c 5.5 KB

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