debug.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. debug.c
  9. Abstract:
  10. This module implements debug UART support for BIOS platforms.
  11. Author:
  12. Evan Green 26-Feb-2014
  13. Environment:
  14. Firmware
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <uefifw.h>
  20. #include <dev/ns16550.h>
  21. //
  22. // --------------------------------------------------------------------- Macros
  23. //
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. //
  28. // Define the hard-coded debug serial port.
  29. //
  30. #define EFI_BIOS_DEBUG_SERIAL_PORT 1
  31. //
  32. // Define the number of serial ports that exist in a PC.
  33. //
  34. #define SERIAL_PORT_COUNT 4
  35. //
  36. // Define the bits for the PC UART Line Status register.
  37. //
  38. #define PC_UART_LINE_STATUS_DATA_READY 0x01
  39. #define PC_UART_LINE_STATUS_TRANSMIT_EMPTY 0x20
  40. #define PC_UART_LINE_STATUS_ERRORS 0x8E
  41. //
  42. // Define the base baud rate for the PC UART. This corresponds to a divisor of
  43. // 1.
  44. //
  45. #define PC_UART_BASE_BAUD 115200
  46. //
  47. // ------------------------------------------------------ Data Type Definitions
  48. //
  49. /*++
  50. Structure Description:
  51. This structure defines a baud rate for the PC UART.
  52. Members:
  53. BaudRate - Stores the baud rate.
  54. Divisor - Stores the divisor to set for this baud rate.
  55. --*/
  56. typedef struct _BAUD_RATE {
  57. UINT32 BaudRate;
  58. UINT16 Divisor;
  59. } BAUD_RATE, *PBAUD_RATE;
  60. typedef enum _COM_REGISTER {
  61. ComDataBuffer = 0,
  62. ComDivisorLow = 0,
  63. ComInterruptEnable = 1,
  64. ComDivisorHigh = 1,
  65. ComInterruptStatus = 2,
  66. ComFifoControl = 2,
  67. ComLineControl = 3,
  68. ComModemControl = 4,
  69. ComLineStatus = 5,
  70. ComModemStatus = 6,
  71. ComScratch = 7
  72. } COM_REGISTER, *PCOM_REGISTER;
  73. //
  74. // ----------------------------------------------- Internal Function Prototypes
  75. //
  76. //
  77. // -------------------------------------------------------------------- Globals
  78. //
  79. NS16550_CONTEXT EfiPcDebugUart;
  80. UINT16 EfiPcSerialIoPortBase[SERIAL_PORT_COUNT] = {
  81. 0x3F8,
  82. 0x2F8,
  83. 0x3E8,
  84. 0x2E8
  85. };
  86. //
  87. // ------------------------------------------------------------------ Functions
  88. //
  89. EFI_STATUS
  90. EfiPlatformDebugDeviceReset (
  91. UINT32 BaudRate
  92. )
  93. /*++
  94. Routine Description:
  95. This routine attempts to initialize the serial UART used for debugging.
  96. Arguments:
  97. BaudRate - Supplies the desired baud rate.
  98. Return Value:
  99. EFI_SUCCESS on success.
  100. EFI_DEVICE_ERROR if a device error occurred while resetting the device.
  101. EFI_UNSUPPORTED if the given baud rate cannot be achieved.
  102. --*/
  103. {
  104. UINT16 PortBase;
  105. EFI_STATUS Status;
  106. Status = EfipNs16550ComputeDivisor(PC_UART_BASE_BAUD,
  107. BaudRate,
  108. &(EfiPcDebugUart.BaudRateDivisor));
  109. if (EFI_ERROR(Status)) {
  110. return Status;
  111. }
  112. PortBase = EfiPcSerialIoPortBase[EFI_BIOS_DEBUG_SERIAL_PORT - 1];
  113. EfiPcDebugUart.MemoryBase = NULL;
  114. EfiPcDebugUart.IoBase = PortBase;
  115. EfiPcDebugUart.RegisterOffset = 0;
  116. EfiPcDebugUart.RegisterShift = 0;
  117. EfiPcDebugUart.Flags = NS16550_FLAG_64_BYTE_FIFO;
  118. return EfipNs16550Initialize(&EfiPcDebugUart);
  119. }
  120. EFI_STATUS
  121. EfiPlatformDebugDeviceTransmit (
  122. VOID *Data,
  123. UINTN Size
  124. )
  125. /*++
  126. Routine Description:
  127. This routine transmits data from the host out through the debug device.
  128. Arguments:
  129. Data - Supplies a pointer to the data to write.
  130. Size - Supplies the size to write, in bytes.
  131. Return Value:
  132. EFI_SUCCESS on success.
  133. EFI_DEVICE_ERROR if a device error occurred.
  134. --*/
  135. {
  136. return EfipNs16550Transmit(&EfiPcDebugUart, Data, Size);
  137. }
  138. EFI_STATUS
  139. EfiPlatformDebugDeviceReceive (
  140. VOID *Data,
  141. UINTN *Size
  142. )
  143. /*++
  144. Routine Description:
  145. This routine receives incoming data from the debug device.
  146. Arguments:
  147. Data - Supplies a pointer where the read data will be returned on success.
  148. Size - Supplies a pointer that on input contains the size of the receive
  149. buffer. On output, returns the number of bytes read.
  150. Return Value:
  151. EFI_SUCCESS on success.
  152. EFI_NOT_READY if there was no data to be read at the current time.
  153. EFI_DEVICE_ERROR if a device error occurred.
  154. --*/
  155. {
  156. return EfipNs16550Receive(&EfiPcDebugUart, Data, Size);
  157. }
  158. EFI_STATUS
  159. EfiPlatformDebugDeviceGetStatus (
  160. BOOLEAN *ReceiveDataAvailable
  161. )
  162. /*++
  163. Routine Description:
  164. This routine returns the current device status.
  165. Arguments:
  166. ReceiveDataAvailable - Supplies a pointer where a boolean will be returned
  167. indicating whether or not receive data is available.
  168. Return Value:
  169. EFI_SUCCESS on success.
  170. EFI_DEVICE_ERROR if a device error occurred.
  171. --*/
  172. {
  173. return EfipNs16550GetStatus(&EfiPcDebugUart, ReceiveDataAvailable);
  174. }
  175. VOID
  176. EfiPlatformDebugDeviceDisconnect (
  177. VOID
  178. )
  179. /*++
  180. Routine Description:
  181. This routine disconnects a device, taking it offline.
  182. Arguments:
  183. None.
  184. Return Value:
  185. None.
  186. --*/
  187. {
  188. return;
  189. }
  190. //
  191. // --------------------------------------------------------- Internal Functions
  192. //