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