I2cHdmiDebugSerialPortLib.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /** @file
  2. Serial I/O Port library implementation for the HDMI I2C Debug Port
  3. Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Base.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/SerialPortLib.h>
  9. #include <Library/PcdLib.h>
  10. #include <Library/TimerLib.h>
  11. #include <IgfxI2c.h>
  12. #include <I2cDebugPortProtocol.h>
  13. /**
  14. Initialize the serial device hardware.
  15. If no initialization is required, then return RETURN_SUCCESS.
  16. If the serial device was successfully initialized, then return RETURN_SUCCESS.
  17. If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
  18. @retval RETURN_SUCCESS The serial device was initialized.
  19. @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
  20. **/
  21. RETURN_STATUS
  22. EFIAPI
  23. SerialPortInitialize (
  24. VOID
  25. )
  26. {
  27. return RETURN_SUCCESS;
  28. }
  29. /**
  30. Write data from buffer to serial device.
  31. Writes NumberOfBytes data bytes from Buffer to the serial device.
  32. The number of bytes actually written to the serial device is returned.
  33. If the return value is less than NumberOfBytes, then the write operation failed.
  34. If Buffer is NULL, then ASSERT().
  35. If NumberOfBytes is zero, then return 0.
  36. @param Buffer Pointer to the data buffer to be written.
  37. @param NumberOfBytes Number of bytes to written to the serial device.
  38. @retval 0 NumberOfBytes is 0.
  39. @retval >0 The number of bytes written to the serial device.
  40. If this value is less than NumberOfBytes, then the write operation failed.
  41. **/
  42. UINTN
  43. EFIAPI
  44. SerialPortWrite (
  45. IN UINT8 *Buffer,
  46. IN UINTN NumberOfBytes
  47. )
  48. {
  49. EFI_STATUS Status;
  50. /******************************************************/
  51. /* WARNING: The GPIOs Need to be programmed first. */
  52. /* Make sure HdmiDebugGpioInit() runs before */
  53. /* this function is called!!! */
  54. /******************************************************/
  55. if (Buffer == NULL) {
  56. return 0;
  57. }
  58. if (NumberOfBytes == 0) {
  59. return 0;
  60. }
  61. Status = I2cDebugPortWrite (Buffer, (UINT32) NumberOfBytes);
  62. if (EFI_ERROR (Status)) {
  63. return 0;
  64. } else {
  65. return NumberOfBytes;
  66. }
  67. }
  68. /**
  69. Read data from serial device and save the datas in buffer.
  70. Reads NumberOfBytes data bytes from a serial device into the buffer
  71. specified by Buffer. The number of bytes actually read is returned.
  72. If the return value is less than NumberOfBytes, then the rest operation failed.
  73. If Buffer is NULL, then ASSERT().
  74. If NumberOfBytes is zero, then return 0.
  75. @param Buffer Pointer to the data buffer to store the data read from the serial device.
  76. @param NumberOfBytes Number of bytes which will be read.
  77. @retval 0 Read data failed, no data is to be read.
  78. @retval >0 Actual number of bytes read from serial device.
  79. **/
  80. UINTN
  81. EFIAPI
  82. SerialPortRead (
  83. OUT UINT8 *Buffer,
  84. IN UINTN NumberOfBytes
  85. )
  86. {
  87. EFI_STATUS Status;
  88. UINT32 BytesRead;
  89. if (Buffer == NULL) {
  90. return 0;
  91. }
  92. if (NumberOfBytes == 0) {
  93. return 0;
  94. }
  95. BytesRead = (UINT32) NumberOfBytes;
  96. Status = I2cDebugPortRead (Buffer, &BytesRead);
  97. if (EFI_ERROR (Status)) {
  98. return 0;
  99. } else {
  100. return (UINTN) BytesRead;
  101. }
  102. }
  103. /**
  104. Polls a serial device to see if there is any data waiting to be read.
  105. Polls a serial device to see if there is any data waiting to be read.
  106. If there is data waiting to be read from the serial device, then TRUE is returned.
  107. If there is no data waiting to be read from the serial device, then FALSE is returned.
  108. @retval TRUE Data is waiting to be read from the serial device.
  109. @retval FALSE There is no data waiting to be read from the serial device.
  110. **/
  111. BOOLEAN
  112. EFIAPI
  113. SerialPortPoll (
  114. VOID
  115. )
  116. {
  117. EFI_STATUS Status;
  118. UINT8 NumberOfBytesInFifoBuffer;
  119. Status = I2cDebugPortReadyToRead (&NumberOfBytesInFifoBuffer);
  120. if (EFI_ERROR (Status)) {
  121. return FALSE;
  122. }
  123. if (NumberOfBytesInFifoBuffer <= 0) {
  124. return FALSE;
  125. }
  126. return TRUE;
  127. }
  128. /**
  129. Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
  130. data bits, and stop bits on a serial device.
  131. @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
  132. device's default interface speed.
  133. On output, the value actually set.
  134. @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
  135. serial interface. A ReceiveFifoDepth value of 0 will use
  136. the device's default FIFO depth.
  137. On output, the value actually set.
  138. @param Timeout The requested time out for a single character in microseconds.
  139. This timeout applies to both the transmit and receive side of the
  140. interface. A Timeout value of 0 will use the device's default time
  141. out value.
  142. On output, the value actually set.
  143. @param Parity The type of parity to use on this serial device. A Parity value of
  144. DefaultParity will use the device's default parity value.
  145. On output, the value actually set.
  146. @param DataBits The number of data bits to use on the serial device. A DataBits
  147. vaule of 0 will use the device's default data bit setting.
  148. On output, the value actually set.
  149. @param StopBits The number of stop bits to use on this serial device. A StopBits
  150. value of DefaultStopBits will use the device's default number of
  151. stop bits.
  152. On output, the value actually set.
  153. @retval RETURN_SUCCESS The new attributes were set on the serial device.
  154. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  155. @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
  156. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  157. **/
  158. RETURN_STATUS
  159. EFIAPI
  160. SerialPortSetAttributes (
  161. IN OUT UINT64 *BaudRate,
  162. IN OUT UINT32 *ReceiveFifoDepth,
  163. IN OUT UINT32 *Timeout,
  164. IN OUT EFI_PARITY_TYPE *Parity,
  165. IN OUT UINT8 *DataBits,
  166. IN OUT EFI_STOP_BITS_TYPE *StopBits
  167. )
  168. {
  169. return EFI_UNSUPPORTED;
  170. }