PeiI2cHdmiDebugSerialPortLib.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /** @file
  2. Serial I/O Port library implementation for the HDMI I2C Debug Port
  3. PEI Library implementation
  4. Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Base.h>
  8. #include <PiPei.h>
  9. #include <Library/BaseLib.h>
  10. #include <Library/BaseMemoryLib.h>
  11. #include <Library/SerialPortLib.h>
  12. #include <Library/HobLib.h>
  13. #include <Library/PcdLib.h>
  14. #include <IgfxI2c.h>
  15. #include <I2cDebugPortProtocol.h>
  16. extern EFI_GUID gI2cHdmiDebugHobGuid;
  17. typedef struct {
  18. PCH_TYPE PchType;
  19. UINT32 ControlBits;
  20. UINT8 I2cHdmiDebugDdcBusPinPair;
  21. BOOLEAN IgdBusMasterReset;
  22. } I2C_HDMI_DEBUG_PORT_CONTEXT;
  23. I2C_HDMI_DEBUG_PORT_CONTEXT*
  24. GetI2cHdmiDebugPortContext (
  25. VOID
  26. )
  27. {
  28. EFI_STATUS Status;
  29. EFI_HOB_GUID_TYPE *GuidHob;
  30. I2C_HDMI_DEBUG_PORT_CONTEXT *Context;
  31. UINT8 I2cHdmiDebugDdcBusPinPair;
  32. I2cHdmiDebugDdcBusPinPair = 0;
  33. GuidHob = GetFirstGuidHob (&gI2cHdmiDebugHobGuid);
  34. if (GuidHob == NULL) {
  35. Context = (I2C_HDMI_DEBUG_PORT_CONTEXT *) BuildGuidHob (&gI2cHdmiDebugHobGuid, sizeof (I2C_HDMI_DEBUG_PORT_CONTEXT));
  36. if (Context == NULL) {
  37. return NULL;
  38. }
  39. ZeroMem ((VOID *) Context, sizeof (I2C_HDMI_DEBUG_PORT_CONTEXT));
  40. Context->PchType = GetPchTypeInternal ();
  41. Status = GetGmbusBusPinPair (
  42. (IGFX_I2C_CHANNEL) PcdGet32 (PcdI2cHdmiDebugPortDdcI2cChannel),
  43. &I2cHdmiDebugDdcBusPinPair
  44. );
  45. if (EFI_ERROR (Status)) {
  46. I2cHdmiDebugDdcBusPinPair = 0;
  47. }
  48. Context->I2cHdmiDebugDdcBusPinPair = I2cHdmiDebugDdcBusPinPair;
  49. return Context;
  50. } else {
  51. return GET_GUID_HOB_DATA (GuidHob);
  52. }
  53. }
  54. /**
  55. Sets the control bits on a serial device.
  56. @param Control Sets the bits of Control that are settable.
  57. @retval RETURN_SUCCESS The new control bits were set on the serial device.
  58. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  59. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  60. **/
  61. RETURN_STATUS
  62. EFIAPI
  63. SerialPortSetControl (
  64. IN UINT32 Control
  65. )
  66. {
  67. I2C_HDMI_DEBUG_PORT_CONTEXT *Context;
  68. //
  69. // check for invalid control parameters
  70. //
  71. if ((Control & (~(EFI_SERIAL_REQUEST_TO_SEND |
  72. EFI_SERIAL_DATA_TERMINAL_READY |
  73. EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE |
  74. EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE |
  75. EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE))) != 0 ) {
  76. return EFI_UNSUPPORTED;
  77. }
  78. Context = GetI2cHdmiDebugPortContext ();
  79. if (Context == NULL) {
  80. return EFI_UNSUPPORTED;
  81. }
  82. Control &= (UINT32) ~(EFI_SERIAL_INPUT_BUFFER_EMPTY);
  83. Context->ControlBits = Control;
  84. return EFI_SUCCESS;
  85. }
  86. /**
  87. Retrieve the status of the control bits on a serial device.
  88. @param Control A pointer to return the current control signals from the serial device.
  89. @retval RETURN_SUCCESS The control bits were read from the serial device.
  90. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  91. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  92. **/
  93. RETURN_STATUS
  94. EFIAPI
  95. SerialPortGetControl (
  96. OUT UINT32 *Control
  97. )
  98. {
  99. EFI_STATUS Status;
  100. I2C_HDMI_DEBUG_PORT_CONTEXT *Context;
  101. UINT8 NumberOfBytesInFifoBuffer;
  102. Context = GetI2cHdmiDebugPortContext ();
  103. if (Context == NULL) {
  104. return EFI_UNSUPPORTED;
  105. }
  106. Status = I2cDebugPortReadyToRead (&NumberOfBytesInFifoBuffer);
  107. if (EFI_ERROR (Status)) {
  108. return Status;
  109. }
  110. *Control = (EFI_SERIAL_CLEAR_TO_SEND | EFI_SERIAL_DATA_SET_READY |
  111. EFI_SERIAL_CARRIER_DETECT | EFI_SERIAL_OUTPUT_BUFFER_EMPTY);
  112. if (NumberOfBytesInFifoBuffer <= 0) {
  113. *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
  114. }
  115. *Control |= Context->ControlBits;
  116. return Status;
  117. }
  118. /**
  119. Returns the type of PCH on the system
  120. @retval The PCH type.
  121. **/
  122. PCH_TYPE
  123. GetPchType (
  124. VOID
  125. )
  126. {
  127. I2C_HDMI_DEBUG_PORT_CONTEXT *Context;
  128. Context = GetI2cHdmiDebugPortContext ();
  129. if (Context == NULL) {
  130. return PchTypeUnknown;
  131. }
  132. return Context->PchType;
  133. }
  134. /**
  135. Returns the GPIO pin pair to use for the I2C HDMI debug port
  136. @param[out] DdcBusPinPair - The GPIO pin pair for the I2C HDMI debug port.
  137. @retval EFI_SUCCESS - The GPIO pin pair was successfully determined
  138. @retval EFI_INVALID_PARAMETER - The given DDC I2C channel does not exist.
  139. @retval EFI_UNSUPPORTED - The platform is using a PCH that is not supported yet.
  140. **/
  141. EFI_STATUS
  142. GetGmbusBusPinPairForI2cDebugPort (
  143. OUT UINT8 *DdcBusPinPair
  144. )
  145. {
  146. I2C_HDMI_DEBUG_PORT_CONTEXT *Context;
  147. Context = GetI2cHdmiDebugPortContext ();
  148. if (Context == NULL) {
  149. return EFI_UNSUPPORTED;
  150. }
  151. *DdcBusPinPair = Context->I2cHdmiDebugDdcBusPinPair;
  152. return EFI_SUCCESS;
  153. }
  154. /**
  155. Returns a flag indicating whether the IGD device bus master enable needs to
  156. be disabled at the end of the current transaction
  157. @retval TRUE - IGD Bus Master Enable needs to be reset
  158. @retval FALSE - IGD Bus Master Enable does not need to be reset
  159. **/
  160. BOOLEAN
  161. GetIgdBusMasterReset (
  162. VOID
  163. )
  164. {
  165. I2C_HDMI_DEBUG_PORT_CONTEXT *Context;
  166. Context = GetI2cHdmiDebugPortContext ();
  167. if (Context == NULL) {
  168. return TRUE;
  169. }
  170. return Context->IgdBusMasterReset;
  171. }
  172. /**
  173. Sets a flag indicating whether the IGD device bus master enable needs to
  174. be disabled at the end of the current transaction
  175. @param[in] IgdBusMasterReset - IGD device bus master enable flag
  176. **/
  177. VOID
  178. SetIgdBusMasterReset (
  179. BOOLEAN IgdBusMasterReset
  180. )
  181. {
  182. I2C_HDMI_DEBUG_PORT_CONTEXT *Context;
  183. Context = GetI2cHdmiDebugPortContext ();
  184. if (Context == NULL) {
  185. return;
  186. }
  187. Context->IgdBusMasterReset = IgdBusMasterReset;
  188. }