Adding support for BeagleBoard.
[mirror_edk2.git] / EmbeddedPkg / Library / GdbSerialDebugPortLib / GdbSerialDebugPortLib.c
CommitLineData
2ef2b01e
A
1/** @file\r
2 Basic serial IO abstaction for GDB\r
3\r
4 Copyright (c) 2008-2009, Apple Inc. All rights reserved.\r
5 \r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <Uefi.h>\r
17#include <Library/GdbSerialLib.h>\r
18#include <Library/PcdLib.h>\r
19#include <Library/IoLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/UefiBootServicesTableLib.h>\r
22\r
23#include <Protocol/DebugPort.h>\r
24\r
25\r
26EFI_DEBUGPORT_PROTOCOL *gDebugPort = NULL;\r
27UINTN gTimeOut = 0;\r
28\r
29/**\r
30 The constructor function initializes the UART.\r
31 \r
32 @param ImageHandle The firmware allocated handle for the EFI image.\r
33 @param SystemTable A pointer to the EFI System Table.\r
34 \r
35 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
36\r
37**/\r
38RETURN_STATUS\r
39EFIAPI\r
40GdbSerialLibDebugPortConstructor (\r
41 IN EFI_HANDLE ImageHandle,\r
42 IN EFI_SYSTEM_TABLE *SystemTable\r
43 )\r
44{\r
45 EFI_STATUS Status;\r
46 \r
47 Status = gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **)&gDebugPort);\r
48 if (!EFI_ERROR (Status)) {\r
49 gTimeOut = PcdGet32 (PcdGdbMaxPacketRetryCount);\r
50 gDebugPort->Reset (gDebugPort);\r
51 }\r
52 \r
53 return Status;\r
54}\r
55\r
56\r
57\r
58/**\r
59 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity, \r
60 data buts, and stop bits on a serial device. This call is optional as the serial\r
61 port will be set up with defaults base on PCD values.\r
62\r
63 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
64 device's default interface speed.\r
65 @param Parity The type of parity to use on this serial device. A Parity value of\r
66 DefaultParity will use the device's default parity value.\r
67 @param DataBits The number of data bits to use on the serial device. A DataBits\r
68 vaule of 0 will use the device's default data bit setting.\r
69 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
70 value of DefaultStopBits will use the device's default number of\r
71 stop bits.\r
72\r
73 @retval EFI_SUCCESS The device was configured.\r
74 @retval EFI_DEVICE_ERROR The serial device could not be coonfigured.\r
75\r
76**/\r
77RETURN_STATUS\r
78EFIAPI\r
79GdbSerialInit (\r
80 IN UINT64 BaudRate, \r
81 IN UINT8 Parity, \r
82 IN UINT8 DataBits, \r
83 IN UINT8 StopBits \r
84 )\r
85{\r
86 EFI_STATUS Status;\r
87 \r
88 Status = gDebugPort->Reset (gDebugPort);\r
89 return Status;\r
90}\r
91\r
92\r
93/**\r
94 Check to see if a character is available from GDB. Do not read the character as that is\r
95 done via GdbGetChar().\r
96\r
97 @return TRUE - Character availible\r
98 @return FALSE - Character not availible\r
99 \r
100**/\r
101BOOLEAN\r
102EFIAPI\r
103GdbIsCharAvailable (\r
104 VOID\r
105 ) \r
106{\r
107 EFI_STATUS Status;\r
108 \r
109 Status = gDebugPort->Poll (gDebugPort);\r
110 \r
111 return (Status == EFI_SUCCESS ? TRUE : FALSE);\r
112}\r
113\r
114\r
115/**\r
116 Get a character from GDB. This function must be able to run in interrupt context.\r
117\r
118 @return A character from GDB\r
119 \r
120**/\r
121CHAR8\r
122EFIAPI\r
123GdbGetChar (\r
124 VOID\r
125 )\r
126{\r
127 EFI_STATUS Status;\r
128 CHAR8 Char;\r
129 UINTN BufferSize;\r
130 \r
131 do {\r
132 BufferSize = sizeof (Char);\r
133 Status = gDebugPort->Read (gDebugPort, gTimeOut, &BufferSize, &Char);\r
134 } while (EFI_ERROR (Status) || BufferSize != sizeof (Char));\r
135 \r
136 return Char;\r
137}\r
138\r
139\r
140/**\r
141 Send a character to GDB. This function must be able to run in interrupt context.\r
142\r
143\r
144 @param Char Send a character to GDB\r
145\r
146**/\r
147\r
148VOID\r
149EFIAPI\r
150GdbPutChar (\r
151 IN CHAR8 Char\r
152 )\r
153{\r
154 EFI_STATUS Status;\r
155 UINTN BufferSize;\r
156 \r
157 do {\r
158 BufferSize = sizeof (Char);\r
159 Status = gDebugPort->Write (gDebugPort, gTimeOut, &BufferSize, &Char);\r
160 } while (EFI_ERROR (Status) || BufferSize != sizeof (Char));\r
161 \r
162 return;\r
163}\r
164\r
165/**\r
166 Send an ASCII string to GDB. This function must be able to run in interrupt context.\r
167\r
168\r
169 @param String Send a string to GDB\r
170\r
171**/\r
172\r
173VOID\r
174GdbPutString (\r
175 IN CHAR8 *String\r
176 )\r
177{\r
178 // We could performance enhance this function by calling gDebugPort->Write ()\r
179 while (*String != '\0') {\r
180 GdbPutChar (*String);\r
181 String++;\r
182 }\r
183}\r
184\r
185\r
186\r
187\r