SerialPortLib.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /** @file
  2. Serial I/O Port library functions with no library constructor/destructor
  3. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  4. Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Base.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/SerialPortLib.h>
  10. #include <Library/PcdLib.h>
  11. #include <Library/IoLib.h>
  12. #include <Library/OmapLib.h>
  13. #include <Omap3530/Omap3530.h>
  14. /*
  15. Programmed hardware of Serial port.
  16. @return Always return EFI_UNSUPPORTED.
  17. **/
  18. RETURN_STATUS
  19. EFIAPI
  20. SerialPortInitialize (
  21. VOID
  22. )
  23. {
  24. // assume assembly code at reset vector has setup UART
  25. return RETURN_SUCCESS;
  26. }
  27. /**
  28. Write data to serial device.
  29. @param Buffer Point of data buffer which need to be writed.
  30. @param NumberOfBytes Number of output bytes which are cached in Buffer.
  31. @retval 0 Write data failed.
  32. @retval !0 Actual number of bytes writed to serial device.
  33. **/
  34. UINTN
  35. EFIAPI
  36. SerialPortWrite (
  37. IN UINT8 *Buffer,
  38. IN UINTN NumberOfBytes
  39. )
  40. {
  41. UINT32 LSR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_LSR_REG;
  42. UINT32 THR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_THR_REG;
  43. UINTN Count;
  44. for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
  45. while ((MmioRead8(LSR) & UART_LSR_TX_FIFO_E_MASK) == UART_LSR_TX_FIFO_E_NOT_EMPTY);
  46. MmioWrite8(THR, *Buffer);
  47. }
  48. return NumberOfBytes;
  49. }
  50. /**
  51. Read data from serial device and save the datas in buffer.
  52. @param Buffer Point of data buffer which need to be writed.
  53. @param NumberOfBytes Number of output bytes which are cached in Buffer.
  54. @retval 0 Read data failed.
  55. @retval !0 Aactual number of bytes read from serial device.
  56. **/
  57. UINTN
  58. EFIAPI
  59. SerialPortRead (
  60. OUT UINT8 *Buffer,
  61. IN UINTN NumberOfBytes
  62. )
  63. {
  64. UINT32 LSR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_LSR_REG;
  65. UINT32 RBR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_RBR_REG;
  66. UINTN Count;
  67. for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
  68. while ((MmioRead8(LSR) & UART_LSR_RX_FIFO_E_MASK) == UART_LSR_RX_FIFO_E_EMPTY);
  69. *Buffer = MmioRead8(RBR);
  70. }
  71. return NumberOfBytes;
  72. }
  73. /**
  74. Check to see if any data is avaiable to be read from the debug device.
  75. @retval EFI_SUCCESS At least one byte of data is avaiable to be read
  76. @retval EFI_NOT_READY No data is avaiable to be read
  77. @retval EFI_DEVICE_ERROR The serial device is not functioning properly
  78. **/
  79. BOOLEAN
  80. EFIAPI
  81. SerialPortPoll (
  82. VOID
  83. )
  84. {
  85. UINT32 LSR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_LSR_REG;
  86. if ((MmioRead8(LSR) & UART_LSR_RX_FIFO_E_MASK) == UART_LSR_RX_FIFO_E_NOT_EMPTY) {
  87. return TRUE;
  88. } else {
  89. return FALSE;
  90. }
  91. }
  92. /**
  93. Sets the control bits on a serial device.
  94. @param[in] Control Sets the bits of Control that are settable.
  95. @retval RETURN_SUCCESS The new control bits were set on the serial device.
  96. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  97. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  98. **/
  99. RETURN_STATUS
  100. EFIAPI
  101. SerialPortSetControl (
  102. IN UINT32 Control
  103. )
  104. {
  105. return RETURN_UNSUPPORTED;
  106. }
  107. /**
  108. Retrieve the status of the control bits on a serial device.
  109. @param[out] Control A pointer to return the current control signals from the serial device.
  110. @retval RETURN_SUCCESS The control bits were read from the serial device.
  111. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  112. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  113. **/
  114. RETURN_STATUS
  115. EFIAPI
  116. SerialPortGetControl (
  117. OUT UINT32 *Control
  118. )
  119. {
  120. *Control = 0;
  121. if (!SerialPortPoll ()) {
  122. *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY;
  123. }
  124. return RETURN_SUCCESS;
  125. }
  126. /**
  127. Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
  128. data bits, and stop bits on a serial device.
  129. @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
  130. device's default interface speed.
  131. On output, the value actually set.
  132. @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
  133. serial interface. A ReceiveFifoDepth value of 0 will use
  134. the device's default FIFO depth.
  135. On output, the value actually set.
  136. @param Timeout The requested time out for a single character in microseconds.
  137. This timeout applies to both the transmit and receive side of the
  138. interface. A Timeout value of 0 will use the device's default time
  139. out value.
  140. On output, the value actually set.
  141. @param Parity The type of parity to use on this serial device. A Parity value of
  142. DefaultParity will use the device's default parity value.
  143. On output, the value actually set.
  144. @param DataBits The number of data bits to use on the serial device. A DataBits
  145. vaule of 0 will use the device's default data bit setting.
  146. On output, the value actually set.
  147. @param StopBits The number of stop bits to use on this serial device. A StopBits
  148. value of DefaultStopBits will use the device's default number of
  149. stop bits.
  150. On output, the value actually set.
  151. @retval RETURN_SUCCESS The new attributes were set on the serial device.
  152. @retval RETURN_UNSUPPORTED The serial device does not support this operation.
  153. @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
  154. @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
  155. **/
  156. RETURN_STATUS
  157. EFIAPI
  158. SerialPortSetAttributes (
  159. IN OUT UINT64 *BaudRate,
  160. IN OUT UINT32 *ReceiveFifoDepth,
  161. IN OUT UINT32 *Timeout,
  162. IN OUT EFI_PARITY_TYPE *Parity,
  163. IN OUT UINT8 *DataBits,
  164. IN OUT EFI_STOP_BITS_TYPE *StopBits
  165. )
  166. {
  167. return RETURN_UNSUPPORTED;
  168. }