]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/SerialDxe/SerialIo.c
ARM Packages: Removed trailing spaces
[mirror_edk2.git] / EmbeddedPkg / SerialDxe / SerialIo.c
CommitLineData
2ef2b01e 1/** @file\r
3402aac7 2 Serial IO Abstraction for GDB stub. This allows an EFI consoles that shows up on the system\r
7ca9e5a4 3 running GDB. One console for error information and another console for user input/output.\r
b0fdce95 4\r
7ca9e5a4 5 Basic packet format is $packet-data#checksum. So every command has 4 bytes of overhead: $,\r
3402aac7 6 #, 0, 0. The 0 and 0 are the ascii characters for the checksum.\r
2ef2b01e 7\r
60274cca 8 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
b0fdce95
OM
9 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\r
10\r
60274cca 11 This program and the accompanying materials\r
2ef2b01e
A
12 are licensed and made available under the terms and conditions of the BSD License\r
13 which accompanies this distribution. The full text of the license may be found at\r
14 http://opensource.org/licenses/bsd-license.php\r
15\r
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18\r
19**/\r
20\r
21#include <PiDxe.h>\r
22#include <Library/UefiLib.h>\r
23#include <Library/UefiBootServicesTableLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/SerialPortLib.h>\r
7d49ced0 26#include <Library/SerialPortExtLib.h>\r
026e30c4 27#include <Library/PcdLib.h>\r
2ef2b01e
A
28\r
29#include <Protocol/SerialIo.h>\r
30\r
7d49ced0 31typedef struct {\r
32 VENDOR_DEVICE_PATH Guid;\r
33 UART_DEVICE_PATH Uart;\r
34 EFI_DEVICE_PATH_PROTOCOL End;\r
35} SIMPLE_TEXT_OUT_DEVICE_PATH;\r
36\r
37SIMPLE_TEXT_OUT_DEVICE_PATH mDevicePath = {\r
38 {\r
b0fdce95 39 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0} },\r
7d49ced0 40 EFI_CALLER_ID_GUID // Use the drivers GUID\r
41 },\r
42 {\r
b0fdce95 43 { MESSAGING_DEVICE_PATH, MSG_UART_DP, { sizeof (UART_DEVICE_PATH), 0} },\r
7d49ced0 44 0, // Reserved\r
45 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate\r
46 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits\r
47 FixedPcdGet8 (PcdUartDefaultParity), // Parity (N)\r
48 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits\r
49 },\r
b0fdce95 50 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } }\r
7d49ced0 51};\r
52\r
53EFI_HANDLE gHandle = NULL;\r
54\r
2ef2b01e
A
55/**\r
56 Reset the serial device.\r
57\r
58 @param This Protocol instance pointer.\r
3402aac7 59\r
2ef2b01e
A
60 @retval EFI_SUCCESS The device was reset.\r
61 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
62\r
63**/\r
64EFI_STATUS\r
65EFIAPI\r
66SerialReset (\r
67 IN EFI_SERIAL_IO_PROTOCOL *This\r
68 )\r
69{\r
7d49ced0 70 EFI_STATUS Status;\r
71 EFI_TPL Tpl;\r
72\r
73 Status = SerialPortInitialize ();\r
74 if (EFI_ERROR(Status)) {\r
75 return Status;\r
76 }\r
77\r
78 //\r
79 // Set the Serial I/O mode and update the device path\r
80 //\r
81\r
82 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
83\r
84 //\r
85 // Set the Serial I/O mode\r
86 //\r
87 This->Mode->ReceiveFifoDepth = 0;\r
88 This->Mode->Timeout = 1000000;\r
89 This->Mode->BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
90 This->Mode->DataBits = (UINT32)PcdGet8 (PcdUartDefaultDataBits);\r
91 This->Mode->Parity = (UINT32)PcdGet8 (PcdUartDefaultParity);\r
92 This->Mode->StopBits = (UINT32)PcdGet8 (PcdUartDefaultStopBits);\r
93\r
94 //\r
95 // Check if the device path has actually changed\r
96 //\r
97 if (mDevicePath.Uart.BaudRate == This->Mode->BaudRate &&\r
98 mDevicePath.Uart.DataBits == (UINT8)This->Mode->DataBits &&\r
99 mDevicePath.Uart.Parity == (UINT8)This->Mode->Parity &&\r
100 mDevicePath.Uart.StopBits == (UINT8)This->Mode->StopBits\r
101 ) {\r
102 gBS->RestoreTPL (Tpl);\r
103 return EFI_SUCCESS;\r
104 }\r
105\r
106 //\r
107 // Update the device path\r
108 //\r
109 mDevicePath.Uart.BaudRate = This->Mode->BaudRate;\r
110 mDevicePath.Uart.DataBits = (UINT8)This->Mode->DataBits;\r
111 mDevicePath.Uart.Parity = (UINT8)This->Mode->Parity;\r
112 mDevicePath.Uart.StopBits = (UINT8)This->Mode->StopBits;\r
113\r
114 Status = gBS->ReinstallProtocolInterface (\r
115 gHandle,\r
116 &gEfiDevicePathProtocolGuid,\r
117 &mDevicePath,\r
118 &mDevicePath\r
119 );\r
120\r
121 gBS->RestoreTPL (Tpl);\r
122\r
123 return Status;\r
2ef2b01e
A
124}\r
125\r
126\r
127/**\r
7ca9e5a4 128 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
2ef2b01e
A
129 data buts, and stop bits on a serial device.\r
130\r
131 @param This Protocol instance pointer.\r
132 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
133 device's default interface speed.\r
7ca9e5a4 134 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
2ef2b01e 135 serial interface. A ReceiveFifoDepth value of 0 will use\r
7ca9e5a4 136 the device's default FIFO depth.\r
2ef2b01e
A
137 @param Timeout The requested time out for a single character in microseconds.\r
138 This timeout applies to both the transmit and receive side of the\r
139 interface. A Timeout value of 0 will use the device's default time\r
140 out value.\r
141 @param Parity The type of parity to use on this serial device. A Parity value of\r
142 DefaultParity will use the device's default parity value.\r
143 @param DataBits The number of data bits to use on the serial device. A DataBits\r
7ca9e5a4 144 value of 0 will use the device's default data bit setting.\r
2ef2b01e
A
145 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
146 value of DefaultStopBits will use the device's default number of\r
147 stop bits.\r
148\r
149 @retval EFI_SUCCESS The device was reset.\r
150 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
151\r
152**/\r
153EFI_STATUS\r
154EFIAPI\r
155SerialSetAttributes (\r
156 IN EFI_SERIAL_IO_PROTOCOL *This,\r
157 IN UINT64 BaudRate,\r
158 IN UINT32 ReceiveFifoDepth,\r
159 IN UINT32 Timeout,\r
160 IN EFI_PARITY_TYPE Parity,\r
161 IN UINT8 DataBits,\r
162 IN EFI_STOP_BITS_TYPE StopBits\r
163 )\r
164{\r
52659efb
OM
165 RETURN_STATUS ReturnStatus;\r
166 EFI_STATUS Status;\r
167 EFI_TPL Tpl;\r
7d49ced0 168\r
52659efb
OM
169 ReturnStatus = SerialPortSetAttributes (&BaudRate, &ReceiveFifoDepth, &Timeout, &Parity, &DataBits, &StopBits);\r
170 if (RETURN_ERROR (ReturnStatus)) {\r
171 return EFI_DEVICE_ERROR;\r
7d49ced0 172 }\r
173\r
174 //\r
175 // Set the Serial I/O mode and update the device path\r
176 //\r
177\r
178 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
179\r
180 //\r
181 // Set the Serial I/O mode\r
182 //\r
183 This->Mode->BaudRate = BaudRate;\r
184 This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;\r
185 This->Mode->Timeout = Timeout;\r
186 This->Mode->Parity = (UINT32)Parity;\r
187 This->Mode->DataBits = (UINT32)DataBits;\r
188 This->Mode->StopBits = (UINT32)StopBits;\r
189\r
190 //\r
191 // Check if the device path has actually changed\r
192 //\r
193 if (mDevicePath.Uart.BaudRate == BaudRate &&\r
194 mDevicePath.Uart.Parity == (UINT8)Parity &&\r
195 mDevicePath.Uart.DataBits == DataBits &&\r
196 mDevicePath.Uart.StopBits == (UINT8)StopBits\r
197 ) {\r
198 gBS->RestoreTPL (Tpl);\r
199 return EFI_SUCCESS;\r
200 }\r
201\r
202 //\r
203 // Update the device path\r
204 //\r
205 mDevicePath.Uart.BaudRate = BaudRate;\r
206 mDevicePath.Uart.DataBits = DataBits;\r
207 mDevicePath.Uart.Parity = (UINT8) Parity;\r
208 mDevicePath.Uart.StopBits = (UINT8) StopBits;\r
209\r
210 Status = gBS->ReinstallProtocolInterface (\r
211 gHandle,\r
212 &gEfiDevicePathProtocolGuid,\r
213 &mDevicePath,\r
214 &mDevicePath\r
215 );\r
216\r
217 gBS->RestoreTPL (Tpl);\r
218\r
219 return Status;\r
2ef2b01e
A
220}\r
221\r
222\r
223/**\r
224 Set the control bits on a serial device\r
225\r
226 @param This Protocol instance pointer.\r
227 @param Control Set the bits of Control that are settable.\r
228\r
229 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
230 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
231 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
232\r
233**/\r
234EFI_STATUS\r
235EFIAPI\r
236SerialSetControl (\r
237 IN EFI_SERIAL_IO_PROTOCOL *This,\r
238 IN UINT32 Control\r
239 )\r
240{\r
7d49ced0 241 return SerialPortSetControl(Control);\r
2ef2b01e
A
242}\r
243\r
244\r
245/**\r
7ca9e5a4 246 Retrieves the status of the control bits on a serial device\r
2ef2b01e
A
247\r
248 @param This Protocol instance pointer.\r
249 @param Control A pointer to return the current Control signals from the serial device.\r
3402aac7 250\r
2ef2b01e
A
251 @retval EFI_SUCCESS The control bits were read from the serial device.\r
252 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
253\r
254**/\r
255EFI_STATUS\r
256EFIAPI\r
257SerialGetControl (\r
258 IN EFI_SERIAL_IO_PROTOCOL *This,\r
259 OUT UINT32 *Control\r
260 )\r
261{\r
7d49ced0 262 return SerialPortGetControl(Control);\r
2ef2b01e
A
263}\r
264\r
265\r
266/**\r
267 Writes data to a serial device.\r
268\r
269 @param This Protocol instance pointer.\r
270 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
271 data actually written.\r
272 @param Buffer The buffer of data to write\r
273\r
274 @retval EFI_SUCCESS The data was written.\r
275 @retval EFI_DEVICE_ERROR The device reported an error.\r
276 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
277\r
278**/\r
279EFI_STATUS\r
280EFIAPI\r
281SerialWrite (\r
282 IN EFI_SERIAL_IO_PROTOCOL *This,\r
283 IN OUT UINTN *BufferSize,\r
284 IN VOID *Buffer\r
285 )\r
286{\r
287 UINTN Count;\r
3402aac7 288\r
2ef2b01e 289 Count = SerialPortWrite (Buffer, *BufferSize);\r
7d49ced0 290\r
291 if (Count != *BufferSize) {\r
292 *BufferSize = Count;\r
293 return EFI_TIMEOUT;\r
294 }\r
295\r
296 return EFI_SUCCESS;\r
2ef2b01e
A
297}\r
298\r
299/**\r
40ab42dd 300 Reads data from a serial device.\r
2ef2b01e
A
301\r
302 @param This Protocol instance pointer.\r
303 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
304 data returned in Buffer.\r
305 @param Buffer The buffer to return the data into.\r
306\r
307 @retval EFI_SUCCESS The data was read.\r
308 @retval EFI_DEVICE_ERROR The device reported an error.\r
309 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
310\r
311**/\r
312\r
313EFI_STATUS\r
314EFIAPI\r
315SerialRead (\r
316 IN EFI_SERIAL_IO_PROTOCOL *This,\r
317 IN OUT UINTN *BufferSize,\r
318 OUT VOID *Buffer\r
319 )\r
320{\r
010bb3d2 321 UINTN Count = 0;\r
40ab42dd 322\r
010bb3d2 323 if (SerialPortPoll()) {\r
324 Count = SerialPortRead (Buffer, *BufferSize);\r
40ab42dd 325 }\r
326\r
327 if (Count != *BufferSize) {\r
010bb3d2 328 *BufferSize = Count;\r
40ab42dd 329 return EFI_TIMEOUT;\r
010bb3d2 330 }\r
40ab42dd 331\r
f65dc3be 332 return EFI_SUCCESS;\r
2ef2b01e
A
333}\r
334\r
3402aac7 335//\r
7ca9e5a4 336// Template used to initialize the GDB Serial IO protocols\r
2ef2b01e
A
337//\r
338EFI_SERIAL_IO_MODE gSerialIoMode = {\r
026e30c4 339 0, // ControlMask\r
340 0, // Timeout\r
341 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate\r
7ca9e5a4 342 1, // ReceiveFifoDepth\r
026e30c4 343 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits\r
344 FixedPcdGet8 (PcdUartDefaultParity), // Parity\r
345 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits\r
2ef2b01e
A
346};\r
347\r
348\r
349EFI_SERIAL_IO_PROTOCOL gSerialIoTemplate = {\r
350 SERIAL_IO_INTERFACE_REVISION,\r
351 SerialReset,\r
352 SerialSetAttributes,\r
353 SerialSetControl,\r
354 SerialGetControl,\r
355 SerialWrite,\r
356 SerialRead,\r
357 &gSerialIoMode\r
358};\r
2ef2b01e
A
359\r
360/**\r
361 Initialize the state information for the Serial Io Protocol\r
362\r
363 @param ImageHandle of the loaded driver\r
364 @param SystemTable Pointer to the System Table\r
365\r
366 @retval EFI_SUCCESS Protocol registered\r
367 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure\r
368 @retval EFI_DEVICE_ERROR Hardware problems\r
369\r
370**/\r
371EFI_STATUS\r
372EFIAPI\r
373SerialDxeInitialize (\r
374 IN EFI_HANDLE ImageHandle,\r
375 IN EFI_SYSTEM_TABLE *SystemTable\r
376 )\r
377{\r
378 EFI_STATUS Status;\r
379\r
2ef2b01e
A
380 // Make a new handle with Serial IO protocol and its device path on it.\r
381 Status = gBS->InstallMultipleProtocolInterfaces (\r
3402aac7 382 &gHandle,\r
2ef2b01e 383 &gEfiSerialIoProtocolGuid, &gSerialIoTemplate,\r
3402aac7 384 &gEfiDevicePathProtocolGuid, &mDevicePath,\r
2ef2b01e
A
385 NULL\r
386 );\r
387 ASSERT_EFI_ERROR (Status);\r
3402aac7 388\r
2ef2b01e
A
389 return Status;\r
390}\r
391\r