]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EmbeddedPkg/Library/GdbSerialLib/GdbSerialLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / Library / GdbSerialLib / GdbSerialLib.c
... / ...
CommitLineData
1/** @file\r
2 Basic serial IO abstraction for GDB\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
5\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include <Uefi.h>\r
11#include <Library/GdbSerialLib.h>\r
12#include <Library/PcdLib.h>\r
13#include <Library/IoLib.h>\r
14#include <Library/DebugLib.h>\r
15\r
16// ---------------------------------------------\r
17// UART Register Offsets\r
18// ---------------------------------------------\r
19#define BAUD_LOW_OFFSET 0x00\r
20#define BAUD_HIGH_OFFSET 0x01\r
21#define IER_OFFSET 0x01\r
22#define LCR_SHADOW_OFFSET 0x01\r
23#define FCR_SHADOW_OFFSET 0x02\r
24#define IR_CONTROL_OFFSET 0x02\r
25#define FCR_OFFSET 0x02\r
26#define EIR_OFFSET 0x02\r
27#define BSR_OFFSET 0x03\r
28#define LCR_OFFSET 0x03\r
29#define MCR_OFFSET 0x04\r
30#define LSR_OFFSET 0x05\r
31#define MSR_OFFSET 0x06\r
32\r
33// ---------------------------------------------\r
34// UART Register Bit Defines\r
35// ---------------------------------------------\r
36#define LSR_TXRDY 0x20U\r
37#define LSR_RXDA 0x01U\r
38#define DLAB 0x01U\r
39#define ENABLE_FIFO 0x01U\r
40#define CLEAR_FIFOS 0x06U\r
41\r
42// IO Port Base for the UART\r
43UINTN gPort;\r
44\r
45/**\r
46 The constructor function initializes the UART.\r
47\r
48 @param ImageHandle The firmware allocated handle for the EFI image.\r
49 @param SystemTable A pointer to the EFI System Table.\r
50\r
51 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
52\r
53**/\r
54RETURN_STATUS\r
55EFIAPI\r
56GdbSerialLibConstructor (\r
57 IN EFI_HANDLE ImageHandle,\r
58 IN EFI_SYSTEM_TABLE *SystemTable\r
59 )\r
60{\r
61 UINT64 BaudRate;\r
62 UINT8 DataBits;\r
63 UINT8 Parity;\r
64 UINT8 StopBits;\r
65\r
66 gPort = (UINTN)PcdGet32 (PcdGdbUartPort);\r
67\r
68 BaudRate = PcdGet64 (PcdGdbBaudRate);\r
69 Parity = PcdGet8 (PcdGdbParity);\r
70 DataBits = PcdGet8 (PcdGdbDataBits);\r
71 StopBits = PcdGet8 (PcdGdbStopBits);\r
72\r
73 return GdbSerialInit (BaudRate, Parity, DataBits, StopBits);\r
74}\r
75\r
76/**\r
77 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
78 data buts, and stop bits on a serial device. This call is optional as the serial\r
79 port will be set up with defaults base on PCD values.\r
80\r
81 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the\r
82 device's default interface speed.\r
83 @param Parity The type of parity to use on this serial device. A Parity value of\r
84 DefaultParity will use the device's default parity value.\r
85 @param DataBits The number of data bits to use on the serial device. A DataBits\r
86 value of 0 will use the device's default data bit setting.\r
87 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
88 value of DefaultStopBits will use the device's default number of\r
89 stop bits.\r
90\r
91 @retval EFI_SUCCESS The device was configured.\r
92 @retval EFI_DEVICE_ERROR The serial device could not be configured.\r
93\r
94**/\r
95RETURN_STATUS\r
96EFIAPI\r
97GdbSerialInit (\r
98 IN UINT64 BaudRate,\r
99 IN UINT8 Parity,\r
100 IN UINT8 DataBits,\r
101 IN UINT8 StopBits\r
102 )\r
103{\r
104 UINTN Divisor;\r
105 UINT8 OutputData;\r
106 UINT8 Data;\r
107 UINT8 BreakSet = 0;\r
108\r
109 //\r
110 // We assume the UART has been turned on to decode gPort address range\r
111 //\r
112\r
113 //\r
114 // Map 5..8 to 0..3\r
115 //\r
116 Data = (UINT8)(DataBits - (UINT8)5);\r
117\r
118 //\r
119 // Calculate divisor for baud generator\r
120 //\r
121 Divisor = 115200/(UINTN)BaudRate;\r
122\r
123 //\r
124 // Set communications format\r
125 //\r
126 OutputData = (UINT8)((DLAB << 7) | ((BreakSet << 6) | ((Parity << 3) | ((StopBits << 2) | Data))));\r
127 IoWrite8 (gPort + LCR_OFFSET, OutputData);\r
128\r
129 //\r
130 // Configure baud rate\r
131 //\r
132 IoWrite8 (gPort + BAUD_HIGH_OFFSET, (UINT8)(Divisor >> 8));\r
133 IoWrite8 (gPort + BAUD_LOW_OFFSET, (UINT8)(Divisor & 0xff));\r
134\r
135 //\r
136 // Switch back to bank 0\r
137 //\r
138 OutputData = (UINT8)((~DLAB<<7)|((BreakSet<<6)|((Parity<<3)|((StopBits<<2)| Data))));\r
139 IoWrite8 (gPort + LCR_OFFSET, OutputData);\r
140\r
141 // Not sure this is the right place to enable the FIFOs....\r
142 // We probably need the FIFO enabled to not drop input\r
143 IoWrite8 (gPort + FCR_SHADOW_OFFSET, ENABLE_FIFO);\r
144\r
145 // Configure the UART hardware here\r
146 return RETURN_SUCCESS;\r
147}\r
148\r
149/**\r
150 Check to see if a character is available from GDB. Do not read the character as that is\r
151 done via GdbGetChar().\r
152\r
153 @return TRUE - Character available\r
154 @return FALSE - Character not available\r
155\r
156**/\r
157BOOLEAN\r
158EFIAPI\r
159GdbIsCharAvailable (\r
160 VOID\r
161 )\r
162{\r
163 UINT8 Data;\r
164\r
165 Data = IoRead8 (gPort + LSR_OFFSET);\r
166\r
167 return ((Data & LSR_RXDA) == LSR_RXDA);\r
168}\r
169\r
170/**\r
171 Get a character from GDB. This function must be able to run in interrupt context.\r
172\r
173 @return A character from GDB\r
174\r
175**/\r
176CHAR8\r
177EFIAPI\r
178GdbGetChar (\r
179 VOID\r
180 )\r
181{\r
182 UINT8 Data;\r
183 CHAR8 Char;\r
184\r
185 // Wait for the serial port to be ready\r
186 do {\r
187 Data = IoRead8 (gPort + LSR_OFFSET);\r
188 } while ((Data & LSR_RXDA) == 0);\r
189\r
190 Char = IoRead8 (gPort);\r
191\r
192 // Make this an DEBUG_INFO after we get everything debugged.\r
193 DEBUG ((DEBUG_ERROR, "<%c<", Char));\r
194 return Char;\r
195}\r
196\r
197/**\r
198 Send a character to GDB. This function must be able to run in interrupt context.\r
199\r
200\r
201 @param Char Send a character to GDB\r
202\r
203**/\r
204VOID\r
205EFIAPI\r
206GdbPutChar (\r
207 IN CHAR8 Char\r
208 )\r
209{\r
210 UINT8 Data;\r
211\r
212 // Make this an DEBUG_INFO after we get everything debugged.\r
213 DEBUG ((DEBUG_ERROR, ">%c>", Char));\r
214\r
215 // Wait for the serial port to be ready\r
216 do {\r
217 Data = IoRead8 (gPort + LSR_OFFSET);\r
218 } while ((Data & LSR_TXRDY) == 0);\r
219\r
220 IoWrite8 (gPort, Char);\r
221}\r
222\r
223/**\r
224 Send an ASCII string to GDB. This function must be able to run in interrupt context.\r
225\r
226\r
227 @param String Send a string to GDB\r
228\r
229**/\r
230VOID\r
231GdbPutString (\r
232 IN CHAR8 *String\r
233 )\r
234{\r
235 while (*String != '\0') {\r
236 GdbPutChar (*String);\r
237 String++;\r
238 }\r
239}\r