]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/GdbSerialLib/GdbSerialLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / Library / GdbSerialLib / GdbSerialLib.c
CommitLineData
2ef2b01e 1/** @file\r
c6a72cd7 2 Basic serial IO abstraction for GDB\r
2ef2b01e 3\r
60274cca 4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
3402aac7 5\r
878b807a 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
2ef2b01e
A
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
e7108d0e 16// ---------------------------------------------\r
2ef2b01e 17// UART Register Offsets\r
e7108d0e
MK
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
2ef2b01e 34// UART Register Bit Defines\r
e7108d0e
MK
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
2ef2b01e
A
41\r
42// IO Port Base for the UART\r
e7108d0e 43UINTN gPort;\r
2ef2b01e
A
44\r
45/**\r
46 The constructor function initializes the UART.\r
3402aac7 47\r
2ef2b01e
A
48 @param ImageHandle The firmware allocated handle for the EFI image.\r
49 @param SystemTable A pointer to the EFI System Table.\r
3402aac7 50\r
2ef2b01e
A
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
e7108d0e
MK
61 UINT64 BaudRate;\r
62 UINT8 DataBits;\r
63 UINT8 Parity;\r
64 UINT8 StopBits;\r
3402aac7 65\r
2ef2b01e 66 gPort = (UINTN)PcdGet32 (PcdGdbUartPort);\r
3402aac7 67\r
2ef2b01e
A
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
2ef2b01e 76/**\r
c6a72cd7 77 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
2ef2b01e
A
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
a4037690 81 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the\r
2ef2b01e
A
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
c6a72cd7 86 value of 0 will use the device's default data bit setting.\r
2ef2b01e
A
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
c6a72cd7 92 @retval EFI_DEVICE_ERROR The serial device could not be configured.\r
2ef2b01e
A
93\r
94**/\r
95RETURN_STATUS\r
96EFIAPI\r
97GdbSerialInit (\r
e7108d0e
MK
98 IN UINT64 BaudRate,\r
99 IN UINT8 Parity,\r
100 IN UINT8 DataBits,\r
101 IN UINT8 StopBits\r
2ef2b01e
A
102 )\r
103{\r
e7108d0e
MK
104 UINTN Divisor;\r
105 UINT8 OutputData;\r
106 UINT8 Data;\r
107 UINT8 BreakSet = 0;\r
2ef2b01e
A
108\r
109 //\r
110 // We assume the UART has been turned on to decode gPort address range\r
111 //\r
3402aac7 112\r
2ef2b01e
A
113 //\r
114 // Map 5..8 to 0..3\r
115 //\r
e7108d0e 116 Data = (UINT8)(DataBits - (UINT8)5);\r
2ef2b01e
A
117\r
118 //\r
119 // Calculate divisor for baud generator\r
120 //\r
3402aac7
RC
121 Divisor = 115200/(UINTN)BaudRate;\r
122\r
2ef2b01e
A
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
2ef2b01e
A
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
2ef2b01e
A
145 // Configure the UART hardware here\r
146 return RETURN_SUCCESS;\r
147}\r
148\r
2ef2b01e
A
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
4f0624e8
GL
153 @return TRUE - Character available\r
154 @return FALSE - Character not available\r
3402aac7 155\r
2ef2b01e
A
156**/\r
157BOOLEAN\r
158EFIAPI\r
159GdbIsCharAvailable (\r
160 VOID\r
3402aac7 161 )\r
2ef2b01e 162{\r
e7108d0e 163 UINT8 Data;\r
3402aac7 164\r
2ef2b01e 165 Data = IoRead8 (gPort + LSR_OFFSET);\r
3402aac7 166\r
2ef2b01e
A
167 return ((Data & LSR_RXDA) == LSR_RXDA);\r
168}\r
169\r
2ef2b01e
A
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
3402aac7 174\r
2ef2b01e
A
175**/\r
176CHAR8\r
177EFIAPI\r
178GdbGetChar (\r
179 VOID\r
180 )\r
181{\r
e7108d0e
MK
182 UINT8 Data;\r
183 CHAR8 Char;\r
2ef2b01e
A
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
3402aac7 191\r
a1878955
MK
192 // Make this an DEBUG_INFO after we get everything debugged.\r
193 DEBUG ((DEBUG_ERROR, "<%c<", Char));\r
2ef2b01e
A
194 return Char;\r
195}\r
196\r
2ef2b01e
A
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
2ef2b01e
A
204VOID\r
205EFIAPI\r
206GdbPutChar (\r
e7108d0e 207 IN CHAR8 Char\r
2ef2b01e
A
208 )\r
209{\r
e7108d0e 210 UINT8 Data;\r
3402aac7 211\r
a1878955
MK
212 // Make this an DEBUG_INFO after we get everything debugged.\r
213 DEBUG ((DEBUG_ERROR, ">%c>", Char));\r
3402aac7 214\r
2ef2b01e
A
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
3402aac7 219\r
2ef2b01e
A
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
2ef2b01e
A
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