TianoCore EDK2 master
Loading...
Searching...
No Matches
GdbSerialLib.c
Go to the documentation of this file.
1
10#include <Uefi.h>
12#include <Library/PcdLib.h>
13#include <Library/IoLib.h>
14#include <Library/DebugLib.h>
15
16// ---------------------------------------------
17// UART Register Offsets
18// ---------------------------------------------
19#define BAUD_LOW_OFFSET 0x00
20#define BAUD_HIGH_OFFSET 0x01
21#define IER_OFFSET 0x01
22#define LCR_SHADOW_OFFSET 0x01
23#define FCR_SHADOW_OFFSET 0x02
24#define IR_CONTROL_OFFSET 0x02
25#define FCR_OFFSET 0x02
26#define EIR_OFFSET 0x02
27#define BSR_OFFSET 0x03
28#define LCR_OFFSET 0x03
29#define MCR_OFFSET 0x04
30#define LSR_OFFSET 0x05
31#define MSR_OFFSET 0x06
32
33// ---------------------------------------------
34// UART Register Bit Defines
35// ---------------------------------------------
36#define LSR_TXRDY 0x20U
37#define LSR_RXDA 0x01U
38#define DLAB 0x01U
39#define ENABLE_FIFO 0x01U
40#define CLEAR_FIFOS 0x06U
41
42// IO Port Base for the UART
43UINTN gPort;
44
54RETURN_STATUS
55EFIAPI
57 IN EFI_HANDLE ImageHandle,
58 IN EFI_SYSTEM_TABLE *SystemTable
59 )
60{
61 UINT64 BaudRate;
62 UINT8 DataBits;
63 UINT8 Parity;
64 UINT8 StopBits;
65
66 gPort = (UINTN)PcdGet32 (PcdGdbUartPort);
67
68 BaudRate = PcdGet64 (PcdGdbBaudRate);
69 Parity = PcdGet8 (PcdGdbParity);
70 DataBits = PcdGet8 (PcdGdbDataBits);
71 StopBits = PcdGet8 (PcdGdbStopBits);
72
73 return GdbSerialInit (BaudRate, Parity, DataBits, StopBits);
74}
75
95RETURN_STATUS
96EFIAPI
98 IN UINT64 BaudRate,
99 IN UINT8 Parity,
100 IN UINT8 DataBits,
101 IN UINT8 StopBits
102 )
103{
104 UINTN Divisor;
105 UINT8 OutputData;
106 UINT8 Data;
107 UINT8 BreakSet = 0;
108
109 //
110 // We assume the UART has been turned on to decode gPort address range
111 //
112
113 //
114 // Map 5..8 to 0..3
115 //
116 Data = (UINT8)(DataBits - (UINT8)5);
117
118 //
119 // Calculate divisor for baud generator
120 //
121 Divisor = 115200/(UINTN)BaudRate;
122
123 //
124 // Set communications format
125 //
126 OutputData = (UINT8)((DLAB << 7) | ((BreakSet << 6) | ((Parity << 3) | ((StopBits << 2) | Data))));
127 IoWrite8 (gPort + LCR_OFFSET, OutputData);
128
129 //
130 // Configure baud rate
131 //
132 IoWrite8 (gPort + BAUD_HIGH_OFFSET, (UINT8)(Divisor >> 8));
133 IoWrite8 (gPort + BAUD_LOW_OFFSET, (UINT8)(Divisor & 0xff));
134
135 //
136 // Switch back to bank 0
137 //
138 OutputData = (UINT8)((~DLAB<<7)|((BreakSet<<6)|((Parity<<3)|((StopBits<<2)| Data))));
139 IoWrite8 (gPort + LCR_OFFSET, OutputData);
140
141 // Not sure this is the right place to enable the FIFOs....
142 // We probably need the FIFO enabled to not drop input
143 IoWrite8 (gPort + FCR_SHADOW_OFFSET, ENABLE_FIFO);
144
145 // Configure the UART hardware here
146 return RETURN_SUCCESS;
147}
148
157BOOLEAN
158EFIAPI
160 VOID
161 )
162{
163 UINT8 Data;
164
165 Data = IoRead8 (gPort + LSR_OFFSET);
166
167 return ((Data & LSR_RXDA) == LSR_RXDA);
168}
169
176CHAR8
177EFIAPI
179 VOID
180 )
181{
182 UINT8 Data;
183 CHAR8 Char;
184
185 // Wait for the serial port to be ready
186 do {
187 Data = IoRead8 (gPort + LSR_OFFSET);
188 } while ((Data & LSR_RXDA) == 0);
189
190 Char = IoRead8 (gPort);
191
192 // Make this an DEBUG_INFO after we get everything debugged.
193 DEBUG ((DEBUG_ERROR, "<%c<", Char));
194 return Char;
195}
196
204VOID
205EFIAPI
207 IN CHAR8 Char
208 )
209{
210 UINT8 Data;
211
212 // Make this an DEBUG_INFO after we get everything debugged.
213 DEBUG ((DEBUG_ERROR, ">%c>", Char));
214
215 // Wait for the serial port to be ready
216 do {
217 Data = IoRead8 (gPort + LSR_OFFSET);
218 } while ((Data & LSR_TXRDY) == 0);
219
220 IoWrite8 (gPort, Char);
221}
222
230VOID
232 IN CHAR8 *String
233 )
234{
235 while (*String != '\0') {
236 GdbPutChar (*String);
237 String++;
238 }
239}
UINT64 UINTN
VOID GdbPutString(IN CHAR8 *String)
Definition: GdbSerialLib.c:231
CHAR8 EFIAPI GdbGetChar(VOID)
Definition: GdbSerialLib.c:178
BOOLEAN EFIAPI GdbIsCharAvailable(VOID)
Definition: GdbSerialLib.c:159
VOID EFIAPI GdbPutChar(IN CHAR8 Char)
Definition: GdbSerialLib.c:206
RETURN_STATUS EFIAPI GdbSerialLibConstructor(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: GdbSerialLib.c:56
RETURN_STATUS EFIAPI GdbSerialInit(IN UINT64 BaudRate, IN UINT8 Parity, IN UINT8 DataBits, IN UINT8 StopBits)
Definition: GdbSerialLib.c:97
UINT8 EFIAPI IoWrite8(IN UINTN Port, IN UINT8 Value)
Definition: IoLibArmVirt.c:200
UINT8 EFIAPI IoRead8(IN UINTN Port)
Definition: IoLibArmVirt.c:175
#define RETURN_SUCCESS
Definition: Base.h:1066
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet64(TokenName)
Definition: PcdLib.h:375
#define PcdGet8(TokenName)
Definition: PcdLib.h:336
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33