BaseSerialPortLibHob.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /** @file
  2. UART Serial Port library functions.
  3. Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <Base.h>
  8. #include <Library/PcdLib.h>
  9. #include <Library/IoLib.h>
  10. #include <Library/BaseLib.h>
  11. #include <Library/HobLib.h>
  12. #include <Library/TimerLib.h>
  13. #include <Library/DebugLib.h>
  14. #include <Protocol/SerialIo.h>
  15. #include <UniversalPayload/SerialPortInfo.h>
  16. //
  17. // 16550 UART register offsets and bitfields
  18. //
  19. #define R_UART_RXBUF 0 // LCR_DLAB = 0
  20. #define R_UART_TXBUF 0 // LCR_DLAB = 0
  21. #define R_UART_BAUD_LOW 0 // LCR_DLAB = 1
  22. #define R_UART_BAUD_HIGH 1 // LCR_DLAB = 1
  23. #define R_UART_IER 1 // LCR_DLAB = 0
  24. #define R_UART_FCR 2
  25. #define B_UART_FCR_FIFOE BIT0
  26. #define B_UART_FCR_FIFO64 BIT5
  27. #define R_UART_LCR 3
  28. #define B_UART_LCR_DLAB BIT7
  29. #define R_UART_MCR 4
  30. #define B_UART_MCR_DTRC BIT0
  31. #define B_UART_MCR_RTS BIT1
  32. #define R_UART_LSR 5
  33. #define B_UART_LSR_RXRDY BIT0
  34. #define B_UART_LSR_TXRDY BIT5
  35. #define B_UART_LSR_TEMT BIT6
  36. #define R_UART_MSR 6
  37. #define B_UART_MSR_CTS BIT4
  38. #define B_UART_MSR_DSR BIT5
  39. #define B_UART_MSR_RI BIT6
  40. #define B_UART_MSR_DCD BIT7
  41. #define MAX_SIZE 16
  42. typedef struct {
  43. UINTN BaseAddress;
  44. BOOLEAN UseMmio;
  45. UINT32 BaudRate;
  46. UINT8 RegisterStride;
  47. } UART_INFO;
  48. UART_INFO mUartInfo[MAX_SIZE];
  49. UINT8 mUartCount = 0;
  50. /**
  51. Reads an 8-bit register. If UseMmio is TRUE, then the value is read from
  52. MMIO space. If UseMmio is FALSE, then the value is read from I/O space. The
  53. parameter Offset is added to the base address of the register.
  54. @param Base The base address register of UART device.
  55. @param Offset The offset of the register to read.
  56. @param UseMmio Check if value has to be read from MMIO space or IO space.
  57. @param RegisterStride Number of bytes between registers in serial device.
  58. @return The value read from the register.
  59. **/
  60. UINT8
  61. SerialPortReadRegister (
  62. UINTN Base,
  63. UINTN Offset,
  64. BOOLEAN UseMmio,
  65. UINT8 RegisterStride
  66. )
  67. {
  68. if (UseMmio) {
  69. return MmioRead8 (Base + Offset * RegisterStride);
  70. } else {
  71. return IoRead8 (Base + Offset * RegisterStride);
  72. }
  73. }
  74. /**
  75. Writes an 8-bit register.. If UseMmio is TRUE, then the value is written to
  76. MMIO space. If UseMmio is FALSE, then the value is written to I/O space. The
  77. parameter Offset is added to the base address of the registers.
  78. @param Base The base address register of UART device.
  79. @param Offset The offset of the register to write.
  80. @param Value Value to be written.
  81. @param UseMmio Check if value has to be written to MMIO space or IO space.
  82. @param RegisterStride Number of bytes between registers in serial device.
  83. @return The value written to the register.
  84. **/
  85. UINT8
  86. SerialPortWriteRegister (
  87. UINTN Base,
  88. UINTN Offset,
  89. UINT8 Value,
  90. BOOLEAN UseMmio,
  91. UINT8 RegisterStride
  92. )
  93. {
  94. if (UseMmio) {
  95. return MmioWrite8 (Base + Offset * RegisterStride, Value);
  96. } else {
  97. return IoWrite8 (Base + Offset * RegisterStride, Value);
  98. }
  99. }
  100. /**
  101. Initialize the serial device hardware.
  102. If no initialization is required, then return RETURN_SUCCESS.
  103. If the serial device was successfully initialized, then return RETURN_SUCCESS.
  104. @retval RETURN_SUCCESS The serial device was initialized.
  105. **/
  106. RETURN_STATUS
  107. EFIAPI
  108. SerialPortInitialize (
  109. VOID
  110. )
  111. {
  112. UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO *SerialPortInfo;
  113. EFI_HOB_GUID_TYPE *GuidHob;
  114. UINTN SerialRegisterBase;
  115. UINT8 RegisterStride;
  116. UINT32 Divisor;
  117. UINT32 CurrentDivisor;
  118. UINT32 BaudRate;
  119. BOOLEAN Initialized;
  120. BOOLEAN MmioEnable;
  121. UINT8 Value;
  122. GuidHob = GetFirstGuidHob (&gUniversalPayloadSerialPortInfoGuid);
  123. while (GuidHob != NULL) {
  124. SerialPortInfo = (UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO *)GET_GUID_HOB_DATA (GuidHob);
  125. SerialRegisterBase = SerialPortInfo->RegisterBase;
  126. MmioEnable = SerialPortInfo->UseMmio;
  127. BaudRate = SerialPortInfo->BaudRate;
  128. RegisterStride = SerialPortInfo->RegisterStride;
  129. if (SerialRegisterBase == 0) {
  130. GuidHob = GET_NEXT_HOB (GuidHob);
  131. GuidHob = GetNextGuidHob (&gUniversalPayloadSerialPortInfoGuid, GuidHob);
  132. continue;
  133. }
  134. mUartInfo[mUartCount].BaseAddress = SerialRegisterBase;
  135. mUartInfo[mUartCount].UseMmio = MmioEnable;
  136. mUartInfo[mUartCount].BaudRate = BaudRate;
  137. mUartInfo[mUartCount].RegisterStride = RegisterStride;
  138. mUartCount++;
  139. Divisor = PcdGet32 (PcdSerialClockRate) / (BaudRate * 16);
  140. if ((PcdGet32 (PcdSerialClockRate) % (BaudRate * 16)) >= BaudRate * 8) {
  141. Divisor++;
  142. }
  143. //
  144. // See if the serial port is already initialized
  145. //
  146. Initialized = TRUE;
  147. if ((SerialPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStride) & 0x3F) != (PcdGet8 (PcdSerialLineControl) & 0x3F)) {
  148. Initialized = FALSE;
  149. }
  150. Value = (UINT8)(SerialPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStride) | B_UART_LCR_DLAB);
  151. SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, Value, MmioEnable, RegisterStride);
  152. CurrentDivisor = SerialPortReadRegister (SerialRegisterBase, R_UART_BAUD_HIGH, MmioEnable, RegisterStride) << 8;
  153. CurrentDivisor |= (UINT32)SerialPortReadRegister (SerialRegisterBase, R_UART_BAUD_LOW, MmioEnable, RegisterStride);
  154. Value = (UINT8)(SerialPortReadRegister (SerialRegisterBase, R_UART_LCR, MmioEnable, RegisterStride) & ~B_UART_LCR_DLAB);
  155. SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, Value, MmioEnable, RegisterStride);
  156. if (CurrentDivisor != Divisor) {
  157. Initialized = FALSE;
  158. }
  159. if (Initialized) {
  160. GuidHob = GET_NEXT_HOB (GuidHob);
  161. GuidHob = GetNextGuidHob (&gUniversalPayloadSerialPortInfoGuid, GuidHob);
  162. continue;
  163. }
  164. //
  165. // Configure baud rate
  166. //
  167. SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, B_UART_LCR_DLAB, MmioEnable, RegisterStride);
  168. SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_HIGH, (UINT8)(Divisor >> 8), MmioEnable, RegisterStride);
  169. SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_LOW, (UINT8)(Divisor & 0xff), MmioEnable, RegisterStride);
  170. //
  171. // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
  172. // Strip reserved bits from PcdSerialLineControl
  173. //
  174. SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3F), MmioEnable, RegisterStride);
  175. //
  176. // Enable and reset FIFOs
  177. // Strip reserved bits from PcdSerialFifoControl
  178. //
  179. SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, 0x00, MmioEnable, RegisterStride);
  180. SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE | B_UART_FCR_FIFO64)), MmioEnable, RegisterStride);
  181. //
  182. // Set FIFO Polled Mode by clearing IER after setting FCR
  183. //
  184. SerialPortWriteRegister (SerialRegisterBase, R_UART_IER, 0x00, MmioEnable, RegisterStride);
  185. //
  186. // Put Modem Control Register(MCR) into its reset state of 0x00.
  187. //
  188. SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, 0x00, MmioEnable, RegisterStride);
  189. GuidHob = GET_NEXT_HOB (GuidHob);
  190. GuidHob = GetNextGuidHob (&gUniversalPayloadSerialPortInfoGuid, GuidHob);
  191. }
  192. return RETURN_SUCCESS;
  193. }
  194. /**
  195. Write data from buffer to serial device.
  196. Writes NumberOfBytes data bytes from Buffer to the serial device.
  197. The number of bytes actually written to the serial device is returned.
  198. If the return value is less than NumberOfBytes, then the write operation failed.
  199. If Buffer is NULL, then return 0.
  200. If NumberOfBytes is zero, then return 0.
  201. @param Buffer Pointer to the data buffer to be written.
  202. @param NumberOfBytes Number of bytes to written to the serial device.
  203. @retval 0 NumberOfBytes is 0.
  204. @retval >0 The number of bytes written to the serial device.
  205. If this value is less than NumberOfBytes, then the write operation failed.
  206. **/
  207. UINTN
  208. EFIAPI
  209. SerialPortWrite (
  210. IN UINT8 *Buffer,
  211. IN UINTN NumberOfBytes
  212. )
  213. {
  214. UINTN BaseAddress;
  215. BOOLEAN UseMmio;
  216. UINTN BytesLeft;
  217. UINTN Index;
  218. UINTN FifoSize;
  219. UINT8 *DataBuffer;
  220. UINT8 Count;
  221. UINT8 Stride;
  222. if ((Buffer == NULL) || (NumberOfBytes == 0) || (mUartCount == 0)) {
  223. return 0;
  224. }
  225. //
  226. // Compute the maximum size of the Tx FIFO
  227. //
  228. FifoSize = 1;
  229. if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) != 0) {
  230. if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) == 0) {
  231. FifoSize = 16;
  232. } else {
  233. FifoSize = PcdGet32 (PcdSerialExtendedTxFifoSize);
  234. }
  235. }
  236. Count = 0;
  237. while (Count < mUartCount) {
  238. BaseAddress = mUartInfo[Count].BaseAddress;
  239. UseMmio = mUartInfo[Count].UseMmio;
  240. Stride = mUartInfo[Count].RegisterStride;
  241. if (BaseAddress == 0) {
  242. Count++;
  243. continue;
  244. }
  245. DataBuffer = Buffer;
  246. BytesLeft = NumberOfBytes;
  247. while (BytesLeft != 0) {
  248. //
  249. // Fill the entire Tx FIFO
  250. //
  251. for (Index = 0; Index < FifoSize && BytesLeft != 0; Index++, BytesLeft--, DataBuffer++) {
  252. //
  253. // Write byte to the transmit buffer.
  254. //
  255. SerialPortWriteRegister (BaseAddress, R_UART_TXBUF, *DataBuffer, UseMmio, Stride);
  256. }
  257. MicroSecondDelay (20);
  258. }
  259. Count++;
  260. }
  261. return NumberOfBytes;
  262. }
  263. /**
  264. Reads data from a serial device into a buffer.
  265. @param Buffer Pointer to the data buffer to store the data read from the serial device.
  266. @param NumberOfBytes Number of bytes to read from the serial device.
  267. @retval 0 NumberOfBytes is 0.
  268. @retval >0 The number of bytes read from the serial device.
  269. If this value is less than NumberOfBytes, then the read operation failed.
  270. **/
  271. UINTN
  272. EFIAPI
  273. SerialPortRead (
  274. OUT UINT8 *Buffer,
  275. IN UINTN NumberOfBytes
  276. )
  277. {
  278. UINTN BaseAddress;
  279. BOOLEAN UseMmio;
  280. UINT8 *DataBuffer;
  281. UINTN BytesLeft;
  282. UINTN Result;
  283. UINT8 Mcr;
  284. UINT8 Count;
  285. UINT8 Stride;
  286. if (Buffer == NULL) {
  287. return 0;
  288. }
  289. Count = 0;
  290. while (Count < mUartCount) {
  291. BaseAddress = mUartInfo[Count].BaseAddress;
  292. UseMmio = mUartInfo[Count].UseMmio;
  293. Stride = mUartInfo[Count].RegisterStride;
  294. if (BaseAddress == 0) {
  295. Count++;
  296. continue;
  297. }
  298. DataBuffer = Buffer;
  299. BytesLeft = NumberOfBytes;
  300. Mcr = (UINT8)(SerialPortReadRegister (BaseAddress, R_UART_MCR, UseMmio, Stride) & ~B_UART_MCR_RTS);
  301. for (Result = 0; BytesLeft-- != 0; Result++, DataBuffer++) {
  302. //
  303. // Wait for the serial port to have some data.
  304. //
  305. while ((SerialPortReadRegister (BaseAddress, R_UART_LSR, UseMmio, Stride) & B_UART_LSR_RXRDY) == 0) {
  306. if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
  307. //
  308. // Set RTS to let the peer send some data
  309. //
  310. SerialPortWriteRegister (BaseAddress, R_UART_MCR, (UINT8)(Mcr | B_UART_MCR_RTS), UseMmio, Stride);
  311. }
  312. }
  313. if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
  314. //
  315. // Clear RTS to prevent peer from sending data
  316. //
  317. SerialPortWriteRegister (BaseAddress, R_UART_MCR, Mcr, UseMmio, Stride);
  318. }
  319. //
  320. // Read byte from the receive buffer.
  321. //
  322. *DataBuffer = SerialPortReadRegister (BaseAddress, R_UART_RXBUF, UseMmio, Stride);
  323. }
  324. Count++;
  325. }
  326. return Result;
  327. }
  328. /**
  329. Polls a serial device to see if there is any data waiting to be read.
  330. Polls a serial device to see if there is any data waiting to be read.
  331. If there is data waiting to be read from the serial device, then TRUE is returned.
  332. If there is no data waiting to be read from the serial device, then FALSE is returned.
  333. @retval TRUE Data is waiting to be read from the serial device.
  334. @retval FALSE There is no data waiting to be read from the serial device.
  335. **/
  336. BOOLEAN
  337. EFIAPI
  338. SerialPortPoll (
  339. VOID
  340. )
  341. {
  342. UINTN BaseAddress;
  343. BOOLEAN UseMmio;
  344. UINT8 Stride;
  345. UINT8 Count;
  346. BOOLEAN Status;
  347. Count = 0;
  348. Status = FALSE;
  349. while (Count < mUartCount) {
  350. BaseAddress = mUartInfo[Count].BaseAddress;
  351. UseMmio = mUartInfo[Count].UseMmio;
  352. Stride = mUartInfo[Count].RegisterStride;
  353. if (BaseAddress == 0) {
  354. Count++;
  355. continue;
  356. }
  357. Status = FALSE;
  358. //
  359. // Read the serial port status
  360. //
  361. if ((SerialPortReadRegister (BaseAddress, R_UART_LSR, UseMmio, Stride) & B_UART_LSR_RXRDY) != 0) {
  362. if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
  363. //
  364. // Clear RTS to prevent peer from sending data
  365. //
  366. SerialPortWriteRegister (BaseAddress, R_UART_MCR, (UINT8)(SerialPortReadRegister (BaseAddress, R_UART_MCR, UseMmio, Stride) & ~B_UART_MCR_RTS), UseMmio, Stride);
  367. }
  368. Status = TRUE;
  369. }
  370. if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
  371. //
  372. // Set RTS to let the peer send some data
  373. //
  374. SerialPortWriteRegister (BaseAddress, R_UART_MCR, (UINT8)(SerialPortReadRegister (BaseAddress, R_UART_MCR, UseMmio, Stride) | B_UART_MCR_RTS), UseMmio, Stride);
  375. }
  376. Count++;
  377. }
  378. return Status;
  379. }
  380. /**
  381. Sets the control bits on a serial device.
  382. @param Control Sets the bits of Control that are settable.
  383. @retval RETURN_SUCCESS The new control bits were set on the serial device.
  384. **/
  385. RETURN_STATUS
  386. EFIAPI
  387. SerialPortSetControl (
  388. IN UINT32 Control
  389. )
  390. {
  391. UINTN BaseAddress;
  392. BOOLEAN UseMmio;
  393. UINT8 Mcr;
  394. UINT8 Count;
  395. UINT8 Stride;
  396. Count = 0;
  397. while (Count < mUartCount) {
  398. BaseAddress = mUartInfo[Count].BaseAddress;
  399. UseMmio = mUartInfo[Count].UseMmio;
  400. Stride = mUartInfo[Count].RegisterStride;
  401. if (BaseAddress == 0) {
  402. Count++;
  403. continue;
  404. }
  405. //
  406. // First determine the parameter is invalid.
  407. //
  408. if ((Control & (~(EFI_SERIAL_REQUEST_TO_SEND | EFI_SERIAL_DATA_TERMINAL_READY |
  409. EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE))) != 0)
  410. {
  411. Count++;
  412. continue;
  413. }
  414. //
  415. // Read the Modem Control Register.
  416. //
  417. Mcr = SerialPortReadRegister (BaseAddress, R_UART_MCR, UseMmio, Stride);
  418. Mcr &= (~(B_UART_MCR_DTRC | B_UART_MCR_RTS));
  419. if ((Control & EFI_SERIAL_DATA_TERMINAL_READY) == EFI_SERIAL_DATA_TERMINAL_READY) {
  420. Mcr |= B_UART_MCR_DTRC;
  421. }
  422. if ((Control & EFI_SERIAL_REQUEST_TO_SEND) == EFI_SERIAL_REQUEST_TO_SEND) {
  423. Mcr |= B_UART_MCR_RTS;
  424. }
  425. //
  426. // Write the Modem Control Register.
  427. //
  428. SerialPortWriteRegister (BaseAddress, R_UART_MCR, Mcr, UseMmio, Stride);
  429. Count++;
  430. }
  431. return RETURN_SUCCESS;
  432. }
  433. /**
  434. Retrieve the status of the control bits on a serial device.
  435. @param Control A pointer to return the current control signals from the serial device.
  436. @retval RETURN_SUCCESS The control bits were read from the serial device.
  437. **/
  438. RETURN_STATUS
  439. EFIAPI
  440. SerialPortGetControl (
  441. OUT UINT32 *Control
  442. )
  443. {
  444. UINTN BaseAddress;
  445. BOOLEAN UseMmio;
  446. UINT8 Msr;
  447. UINT8 Mcr;
  448. UINT8 Lsr;
  449. UINT8 Count;
  450. UINT8 Stride;
  451. Count = 0;
  452. while (Count < mUartCount) {
  453. BaseAddress = mUartInfo[Count].BaseAddress;
  454. UseMmio = mUartInfo[Count].UseMmio;
  455. Stride = mUartInfo[Count].RegisterStride;
  456. if (BaseAddress == 0) {
  457. Count++;
  458. continue;
  459. }
  460. *Control = 0;
  461. //
  462. // Read the Modem Status Register.
  463. //
  464. Msr = SerialPortReadRegister (BaseAddress, R_UART_MSR, UseMmio, Stride);
  465. if ((Msr & B_UART_MSR_CTS) == B_UART_MSR_CTS) {
  466. *Control |= EFI_SERIAL_CLEAR_TO_SEND;
  467. }
  468. if ((Msr & B_UART_MSR_DSR) == B_UART_MSR_DSR) {
  469. *Control |= EFI_SERIAL_DATA_SET_READY;
  470. }
  471. if ((Msr & B_UART_MSR_RI) == B_UART_MSR_RI) {
  472. *Control |= EFI_SERIAL_RING_INDICATE;
  473. }
  474. if ((Msr & B_UART_MSR_DCD) == B_UART_MSR_DCD) {
  475. *Control |= EFI_SERIAL_CARRIER_DETECT;
  476. }
  477. //
  478. // Read the Modem Control Register.
  479. //
  480. Mcr = SerialPortReadRegister (BaseAddress, R_UART_MCR, UseMmio, Stride);
  481. if ((Mcr & B_UART_MCR_DTRC) == B_UART_MCR_DTRC) {
  482. *Control |= EFI_SERIAL_DATA_TERMINAL_READY;
  483. }
  484. if ((Mcr & B_UART_MCR_RTS) == B_UART_MCR_RTS) {
  485. *Control |= EFI_SERIAL_REQUEST_TO_SEND;
  486. }
  487. if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
  488. *Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;
  489. }
  490. //
  491. // Read the Line Status Register.
  492. //
  493. Lsr = SerialPortReadRegister (BaseAddress, R_UART_LSR, UseMmio, Stride);
  494. if ((Lsr & (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) == (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) {
  495. *Control |= EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
  496. }
  497. if ((Lsr & B_UART_LSR_RXRDY) == 0) {
  498. *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
  499. }
  500. Count++;
  501. }
  502. return RETURN_SUCCESS;
  503. }
  504. /**
  505. Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
  506. data bits, and stop bits on a serial device.
  507. @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
  508. device's default interface speed.
  509. On output, the value actually set.
  510. @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
  511. serial interface. A ReceiveFifoDepth value of 0 will use
  512. the device's default FIFO depth.
  513. On output, the value actually set.
  514. @param Timeout The requested time out for a single character in microseconds.
  515. This timeout applies to both the transmit and receive side of the
  516. interface. A Timeout value of 0 will use the device's default time
  517. out value.
  518. On output, the value actually set.
  519. @param Parity The type of parity to use on this serial device. A Parity value of
  520. DefaultParity will use the device's default parity value.
  521. On output, the value actually set.
  522. @param DataBits The number of data bits to use on the serial device. A DataBits
  523. vaule of 0 will use the device's default data bit setting.
  524. On output, the value actually set.
  525. @param StopBits The number of stop bits to use on this serial device. A StopBits
  526. value of DefaultStopBits will use the device's default number of
  527. stop bits.
  528. On output, the value actually set.
  529. @retval RETURN_SUCCESS The new attributes were set on the serial device.
  530. **/
  531. RETURN_STATUS
  532. EFIAPI
  533. SerialPortSetAttributes (
  534. IN OUT UINT64 *BaudRate,
  535. IN OUT UINT32 *ReceiveFifoDepth,
  536. IN OUT UINT32 *Timeout,
  537. IN OUT EFI_PARITY_TYPE *Parity,
  538. IN OUT UINT8 *DataBits,
  539. IN OUT EFI_STOP_BITS_TYPE *StopBits
  540. )
  541. {
  542. UINTN BaseAddress;
  543. BOOLEAN UseMmio;
  544. UINT32 SerialBaudRate;
  545. UINTN Divisor;
  546. UINT8 Lcr;
  547. UINT8 LcrData;
  548. UINT8 LcrParity;
  549. UINT8 LcrStop;
  550. UINT8 Count;
  551. UINT8 Stride;
  552. Count = 0;
  553. while (Count < mUartCount) {
  554. BaseAddress = mUartInfo[Count].BaseAddress;
  555. UseMmio = mUartInfo[Count].UseMmio;
  556. Stride = mUartInfo[Count].RegisterStride;
  557. if (BaseAddress == 0) {
  558. Count++;
  559. continue;
  560. }
  561. //
  562. // Check for default settings and fill in actual values.
  563. //
  564. if (*BaudRate == 0) {
  565. *BaudRate = mUartInfo[Count].BaudRate;
  566. }
  567. SerialBaudRate = (UINT32)*BaudRate;
  568. if (*DataBits == 0) {
  569. LcrData = (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3);
  570. *DataBits = LcrData + 5;
  571. } else {
  572. if ((*DataBits < 5) || (*DataBits > 8)) {
  573. Count++;
  574. continue;
  575. }
  576. //
  577. // Map 5..8 to 0..3
  578. //
  579. LcrData = (UINT8)(*DataBits - (UINT8)5);
  580. }
  581. if (*Parity == DefaultParity) {
  582. LcrParity = (UINT8)((PcdGet8 (PcdSerialLineControl) >> 3) & 0x7);
  583. switch (LcrParity) {
  584. case 0:
  585. *Parity = NoParity;
  586. break;
  587. case 3:
  588. *Parity = EvenParity;
  589. break;
  590. case 1:
  591. *Parity = OddParity;
  592. break;
  593. case 7:
  594. *Parity = SpaceParity;
  595. break;
  596. case 5:
  597. *Parity = MarkParity;
  598. break;
  599. default:
  600. break;
  601. }
  602. } else {
  603. switch (*Parity) {
  604. case NoParity:
  605. LcrParity = 0;
  606. break;
  607. case EvenParity:
  608. LcrParity = 3;
  609. break;
  610. case OddParity:
  611. LcrParity = 1;
  612. break;
  613. case SpaceParity:
  614. LcrParity = 7;
  615. break;
  616. case MarkParity:
  617. LcrParity = 5;
  618. break;
  619. default:
  620. Count++;
  621. continue;
  622. }
  623. }
  624. if (*StopBits == DefaultStopBits) {
  625. LcrStop = (UINT8)((PcdGet8 (PcdSerialLineControl) >> 2) & 0x1);
  626. switch (LcrStop) {
  627. case 0:
  628. *StopBits = OneStopBit;
  629. break;
  630. case 1:
  631. if (*DataBits == 5) {
  632. *StopBits = OneFiveStopBits;
  633. } else {
  634. *StopBits = TwoStopBits;
  635. }
  636. break;
  637. default:
  638. break;
  639. }
  640. } else {
  641. switch (*StopBits) {
  642. case OneStopBit:
  643. LcrStop = 0;
  644. break;
  645. case OneFiveStopBits:
  646. case TwoStopBits:
  647. LcrStop = 1;
  648. break;
  649. default:
  650. Count++;
  651. continue;
  652. }
  653. }
  654. //
  655. // Calculate divisor for baud generator
  656. // Ref_Clk_Rate / Baud_Rate / 16
  657. //
  658. Divisor = PcdGet32 (PcdSerialClockRate) / (SerialBaudRate * 16);
  659. if ((PcdGet32 (PcdSerialClockRate) % (SerialBaudRate * 16)) >= SerialBaudRate * 8) {
  660. Divisor++;
  661. }
  662. //
  663. // Configure baud rate
  664. //
  665. SerialPortWriteRegister (BaseAddress, R_UART_LCR, B_UART_LCR_DLAB, UseMmio, Stride);
  666. SerialPortWriteRegister (BaseAddress, R_UART_BAUD_HIGH, (UINT8)(Divisor >> 8), UseMmio, Stride);
  667. SerialPortWriteRegister (BaseAddress, R_UART_BAUD_LOW, (UINT8)(Divisor & 0xff), UseMmio, Stride);
  668. //
  669. // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
  670. // Strip reserved bits from line control value
  671. //
  672. Lcr = (UINT8)((LcrParity << 3) | (LcrStop << 2) | LcrData);
  673. SerialPortWriteRegister (BaseAddress, R_UART_LCR, (UINT8)(Lcr & 0x3F), UseMmio, Stride);
  674. Count++;
  675. }
  676. return RETURN_SUCCESS;
  677. }