]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SerialDxe/SerialIo.c
MdeModulePkg/FaultTolerantWriteDxe: factor out boot service accesses
[mirror_edk2.git] / MdeModulePkg / Universal / SerialDxe / SerialIo.c
CommitLineData
bf6b810f
SZ
1/** @file\r
2 Serial driver that layers on top of a Serial Port Library instance.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\r
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
7\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/SerialPortLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/PcdLib.h>\r
22\r
23#include <Protocol/SerialIo.h>\r
24#include <Protocol/DevicePath.h>\r
25\r
26typedef struct {\r
27 VENDOR_DEVICE_PATH Guid;\r
28 UART_DEVICE_PATH Uart;\r
29 EFI_DEVICE_PATH_PROTOCOL End;\r
30} SERIAL_DEVICE_PATH;\r
31\r
32/**\r
33 Reset the serial device.\r
34\r
35 @param This Protocol instance pointer.\r
36\r
37 @retval EFI_SUCCESS The device was reset.\r
38 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
39\r
40**/\r
41EFI_STATUS\r
42EFIAPI\r
43SerialReset (\r
44 IN EFI_SERIAL_IO_PROTOCOL *This\r
45 );\r
46\r
47/**\r
48 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
49 data bits, and stop bits on a serial device.\r
50\r
51 @param This Protocol instance pointer.\r
52 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
53 device's default interface speed.\r
54 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
55 serial interface. A ReceiveFifoDepth value of 0 will use\r
56 the device's default FIFO depth.\r
57 @param Timeout The requested time out for a single character in microseconds.\r
58 This timeout applies to both the transmit and receive side of the\r
59 interface. A Timeout value of 0 will use the device's default time\r
60 out value.\r
61 @param Parity The type of parity to use on this serial device. A Parity value of\r
62 DefaultParity will use the device's default parity value.\r
63 @param DataBits The number of data bits to use on the serial device. A DataBits\r
64 value of 0 will use the device's default data bit setting.\r
65 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
66 value of DefaultStopBits will use the device's default number of\r
67 stop bits.\r
68\r
13d378fc
JG
69 @retval EFI_SUCCESS The device was reset.\r
70 @retval EFI_INVALID_PARAMETER One or more attributes has an unsupported value.\r
71 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
bf6b810f
SZ
72\r
73**/\r
74EFI_STATUS\r
75EFIAPI\r
76SerialSetAttributes (\r
77 IN EFI_SERIAL_IO_PROTOCOL *This,\r
78 IN UINT64 BaudRate,\r
79 IN UINT32 ReceiveFifoDepth,\r
80 IN UINT32 Timeout,\r
81 IN EFI_PARITY_TYPE Parity,\r
82 IN UINT8 DataBits,\r
83 IN EFI_STOP_BITS_TYPE StopBits\r
84 );\r
85\r
86/**\r
87 Set the control bits on a serial device\r
88\r
89 @param This Protocol instance pointer.\r
90 @param Control Set the bits of Control that are settable.\r
91\r
92 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
93 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
94 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
95\r
96**/\r
97EFI_STATUS\r
98EFIAPI\r
99SerialSetControl (\r
100 IN EFI_SERIAL_IO_PROTOCOL *This,\r
101 IN UINT32 Control\r
102 );\r
103\r
104/**\r
105 Retrieves the status of the control bits on a serial device\r
106\r
107 @param This Protocol instance pointer.\r
108 @param Control A pointer to return the current Control signals from the serial device.\r
109\r
110 @retval EFI_SUCCESS The control bits were read from the serial device.\r
111 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
112\r
113**/\r
114EFI_STATUS\r
115EFIAPI\r
116SerialGetControl (\r
117 IN EFI_SERIAL_IO_PROTOCOL *This,\r
118 OUT UINT32 *Control\r
119 );\r
120\r
121/**\r
122 Writes data to a serial device.\r
123\r
124 @param This Protocol instance pointer.\r
125 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
126 data actually written.\r
127 @param Buffer The buffer of data to write\r
128\r
129 @retval EFI_SUCCESS The data was written.\r
130 @retval EFI_DEVICE_ERROR The device reported an error.\r
131 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
132\r
133**/\r
134EFI_STATUS\r
135EFIAPI\r
136SerialWrite (\r
137 IN EFI_SERIAL_IO_PROTOCOL *This,\r
138 IN OUT UINTN *BufferSize,\r
139 IN VOID *Buffer\r
140 );\r
141\r
142/**\r
143 Reads data from a serial device.\r
144\r
145 @param This Protocol instance pointer.\r
146 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
147 data returned in Buffer.\r
148 @param Buffer The buffer to return the data into.\r
149\r
150 @retval EFI_SUCCESS The data was read.\r
151 @retval EFI_DEVICE_ERROR The device reported an error.\r
152 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
153\r
154**/\r
155EFI_STATUS\r
156EFIAPI\r
157SerialRead (\r
158 IN EFI_SERIAL_IO_PROTOCOL *This,\r
159 IN OUT UINTN *BufferSize,\r
160 OUT VOID *Buffer\r
161 );\r
162\r
163EFI_HANDLE mSerialHandle = NULL;\r
164\r
165SERIAL_DEVICE_PATH mSerialDevicePath = {\r
166 {\r
167 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0} },\r
168 EFI_CALLER_ID_GUID // Use the driver's GUID\r
169 },\r
170 {\r
171 { MESSAGING_DEVICE_PATH, MSG_UART_DP, { sizeof (UART_DEVICE_PATH), 0} },\r
172 0, // Reserved\r
173 0, // BaudRate\r
174 0, // DataBits\r
175 0, // Parity\r
176 0 // StopBits\r
177 },\r
178 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } }\r
179};\r
180\r
181//\r
182// Template used to initialize the Serial IO protocols.\r
183//\r
184EFI_SERIAL_IO_MODE mSerialIoMode = {\r
c0beed6d
LE
185 //\r
186 // value field set in SerialDxeInitialize()?\r
187 //--------- ------------------- -----------------------------\r
188 0, // ControlMask\r
ea012619 189 1000 * 1000, // Timeout\r
c0beed6d
LE
190 0, // BaudRate yes\r
191 1, // ReceiveFifoDepth\r
192 0, // DataBits yes\r
193 0, // Parity yes\r
194 0 // StopBits yes\r
bf6b810f
SZ
195};\r
196\r
197EFI_SERIAL_IO_PROTOCOL mSerialIoTemplate = {\r
198 SERIAL_IO_INTERFACE_REVISION,\r
199 SerialReset,\r
200 SerialSetAttributes,\r
201 SerialSetControl,\r
202 SerialGetControl,\r
203 SerialWrite,\r
204 SerialRead,\r
205 &mSerialIoMode\r
206};\r
207\r
208/**\r
209 Reset the serial device.\r
210\r
211 @param This Protocol instance pointer.\r
212\r
213 @retval EFI_SUCCESS The device was reset.\r
214 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
215\r
216**/\r
217EFI_STATUS\r
218EFIAPI\r
219SerialReset (\r
220 IN EFI_SERIAL_IO_PROTOCOL *This\r
221 )\r
222{\r
223 EFI_STATUS Status;\r
bf6b810f
SZ
224\r
225 Status = SerialPortInitialize ();\r
226 if (EFI_ERROR (Status)) {\r
227 return Status;\r
228 }\r
229\r
230 //\r
91cc526b 231 // Go set the current attributes\r
bf6b810f 232 //\r
91cc526b
PB
233 Status = This->SetAttributes (\r
234 This,\r
235 This->Mode->BaudRate,\r
236 This->Mode->ReceiveFifoDepth,\r
237 This->Mode->Timeout,\r
238 (EFI_PARITY_TYPE) This->Mode->Parity,\r
239 (UINT8) This->Mode->DataBits,\r
240 (EFI_STOP_BITS_TYPE) This->Mode->StopBits\r
241 );\r
7ce5af40
JG
242\r
243 //\r
244 // The serial device may not support some of the attributes. To prevent\r
245 // later failure, always return EFI_SUCCESS when SetAttributes is returning\r
246 // EFI_INVALID_PARAMETER.\r
247 //\r
248 if (Status == EFI_INVALID_PARAMETER) {\r
249 return EFI_SUCCESS;\r
250 }\r
bf6b810f
SZ
251\r
252 return Status;\r
253}\r
254\r
255/**\r
256 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
257 data bits, and stop bits on a serial device.\r
258\r
259 @param This Protocol instance pointer.\r
260 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
261 device's default interface speed.\r
262 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
263 serial interface. A ReceiveFifoDepth value of 0 will use\r
264 the device's default FIFO depth.\r
265 @param Timeout The requested time out for a single character in microseconds.\r
266 This timeout applies to both the transmit and receive side of the\r
267 interface. A Timeout value of 0 will use the device's default time\r
268 out value.\r
269 @param Parity The type of parity to use on this serial device. A Parity value of\r
270 DefaultParity will use the device's default parity value.\r
271 @param DataBits The number of data bits to use on the serial device. A DataBits\r
272 value of 0 will use the device's default data bit setting.\r
273 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
274 value of DefaultStopBits will use the device's default number of\r
275 stop bits.\r
276\r
13d378fc
JG
277 @retval EFI_SUCCESS The device was reset.\r
278 @retval EFI_INVALID_PARAMETER One or more attributes has an unsupported value.\r
279 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
bf6b810f
SZ
280\r
281**/\r
282EFI_STATUS\r
283EFIAPI\r
284SerialSetAttributes (\r
285 IN EFI_SERIAL_IO_PROTOCOL *This,\r
286 IN UINT64 BaudRate,\r
287 IN UINT32 ReceiveFifoDepth,\r
288 IN UINT32 Timeout,\r
289 IN EFI_PARITY_TYPE Parity,\r
290 IN UINT8 DataBits,\r
291 IN EFI_STOP_BITS_TYPE StopBits\r
292 )\r
293{\r
a7fd8452
SZ
294 EFI_STATUS Status;\r
295 EFI_TPL Tpl;\r
296 UINT64 OriginalBaudRate;\r
297 UINT32 OriginalReceiveFifoDepth;\r
298 UINT32 OriginalTimeout;\r
299 EFI_PARITY_TYPE OriginalParity;\r
300 UINT8 OriginalDataBits;\r
301 EFI_STOP_BITS_TYPE OriginalStopBits;\r
bf6b810f 302\r
a7fd8452
SZ
303 //\r
304 // Preserve the original input values in case\r
305 // SerialPortSetAttributes() updates the input/output\r
306 // parameters even on error.\r
307 //\r
308 OriginalBaudRate = BaudRate;\r
309 OriginalReceiveFifoDepth = ReceiveFifoDepth;\r
310 OriginalTimeout = Timeout;\r
311 OriginalParity = Parity;\r
312 OriginalDataBits = DataBits;\r
313 OriginalStopBits = StopBits;\r
bf6b810f
SZ
314 Status = SerialPortSetAttributes (&BaudRate, &ReceiveFifoDepth, &Timeout, &Parity, &DataBits, &StopBits);\r
315 if (EFI_ERROR (Status)) {\r
a7fd8452
SZ
316 //\r
317 // If it is just to set Timeout value and unsupported is returned,\r
318 // do not return error.\r
319 //\r
320 if ((Status == EFI_UNSUPPORTED) &&\r
321 (This->Mode->Timeout != OriginalTimeout) &&\r
322 (This->Mode->ReceiveFifoDepth == OriginalReceiveFifoDepth) &&\r
323 (This->Mode->BaudRate == OriginalBaudRate) &&\r
324 (This->Mode->DataBits == (UINT32) OriginalDataBits) &&\r
325 (This->Mode->Parity == (UINT32) OriginalParity) &&\r
326 (This->Mode->StopBits == (UINT32) OriginalStopBits)) {\r
327 //\r
328 // Restore to the original input values.\r
329 //\r
330 BaudRate = OriginalBaudRate;\r
331 ReceiveFifoDepth = OriginalReceiveFifoDepth;\r
332 Timeout = OriginalTimeout;\r
333 Parity = OriginalParity;\r
334 DataBits = OriginalDataBits;\r
335 StopBits = OriginalStopBits;\r
336 Status = EFI_SUCCESS;\r
13d378fc
JG
337 } else if (Status == EFI_INVALID_PARAMETER || Status == EFI_UNSUPPORTED) {\r
338 return EFI_INVALID_PARAMETER;\r
a7fd8452 339 } else {\r
13d378fc 340 return EFI_DEVICE_ERROR;\r
a7fd8452 341 }\r
bf6b810f
SZ
342 }\r
343\r
344 //\r
345 // Set the Serial I/O mode and update the device path\r
346 //\r
347\r
348 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
349\r
350 //\r
351 // Set the Serial I/O mode\r
352 //\r
353 This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;\r
354 This->Mode->Timeout = Timeout;\r
355 This->Mode->BaudRate = BaudRate;\r
356 This->Mode->DataBits = (UINT32) DataBits;\r
357 This->Mode->Parity = (UINT32) Parity;\r
358 This->Mode->StopBits = (UINT32) StopBits;\r
359\r
360 //\r
361 // Check if the device path has actually changed\r
362 //\r
363 if (mSerialDevicePath.Uart.BaudRate == BaudRate &&\r
364 mSerialDevicePath.Uart.DataBits == DataBits &&\r
365 mSerialDevicePath.Uart.Parity == (UINT8) Parity &&\r
366 mSerialDevicePath.Uart.StopBits == (UINT8) StopBits\r
367 ) {\r
368 gBS->RestoreTPL (Tpl);\r
369 return EFI_SUCCESS;\r
370 }\r
371\r
372 //\r
373 // Update the device path\r
374 //\r
375 mSerialDevicePath.Uart.BaudRate = BaudRate;\r
376 mSerialDevicePath.Uart.DataBits = DataBits;\r
377 mSerialDevicePath.Uart.Parity = (UINT8) Parity;\r
378 mSerialDevicePath.Uart.StopBits = (UINT8) StopBits;\r
379\r
380 Status = gBS->ReinstallProtocolInterface (\r
381 mSerialHandle,\r
382 &gEfiDevicePathProtocolGuid,\r
383 &mSerialDevicePath,\r
384 &mSerialDevicePath\r
385 );\r
386\r
387 gBS->RestoreTPL (Tpl);\r
388\r
389 return Status;\r
390}\r
391\r
392/**\r
393 Set the control bits on a serial device\r
394\r
395 @param This Protocol instance pointer.\r
396 @param Control Set the bits of Control that are settable.\r
397\r
398 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
399 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
400 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
401\r
402**/\r
403EFI_STATUS\r
404EFIAPI\r
405SerialSetControl (\r
406 IN EFI_SERIAL_IO_PROTOCOL *This,\r
407 IN UINT32 Control\r
408 )\r
409{\r
410 return SerialPortSetControl (Control);\r
411}\r
412\r
413/**\r
414 Retrieves the status of the control bits on a serial device\r
415\r
416 @param This Protocol instance pointer.\r
417 @param Control A pointer to return the current Control signals from the serial device.\r
418\r
419 @retval EFI_SUCCESS The control bits were read from the serial device.\r
420 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
421\r
422**/\r
423EFI_STATUS\r
424EFIAPI\r
425SerialGetControl (\r
426 IN EFI_SERIAL_IO_PROTOCOL *This,\r
427 OUT UINT32 *Control\r
428 )\r
429{\r
430 return SerialPortGetControl (Control);\r
431}\r
432\r
433/**\r
434 Writes data to a serial device.\r
435\r
436 @param This Protocol instance pointer.\r
437 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
438 data actually written.\r
439 @param Buffer The buffer of data to write\r
440\r
441 @retval EFI_SUCCESS The data was written.\r
442 @retval EFI_DEVICE_ERROR The device reported an error.\r
443 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
444\r
445**/\r
446EFI_STATUS\r
447EFIAPI\r
448SerialWrite (\r
449 IN EFI_SERIAL_IO_PROTOCOL *This,\r
450 IN OUT UINTN *BufferSize,\r
451 IN VOID *Buffer\r
452 )\r
453{\r
454 UINTN Count;\r
455\r
456 Count = SerialPortWrite (Buffer, *BufferSize);\r
457\r
458 if (Count != *BufferSize) {\r
459 *BufferSize = Count;\r
460 return EFI_TIMEOUT;\r
461 }\r
462\r
463 return EFI_SUCCESS;\r
464}\r
465\r
466/**\r
467 Reads data from a serial device.\r
468\r
469 @param This Protocol instance pointer.\r
470 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
471 data returned in Buffer.\r
472 @param Buffer The buffer to return the data into.\r
473\r
474 @retval EFI_SUCCESS The data was read.\r
475 @retval EFI_DEVICE_ERROR The device reported an error.\r
476 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
477\r
478**/\r
479EFI_STATUS\r
480EFIAPI\r
481SerialRead (\r
482 IN EFI_SERIAL_IO_PROTOCOL *This,\r
483 IN OUT UINTN *BufferSize,\r
484 OUT VOID *Buffer\r
485 )\r
486{\r
487 UINTN Count;\r
4cf3f37c 488 UINTN TimeOut;\r
bf6b810f
SZ
489\r
490 Count = 0;\r
491\r
4cf3f37c
SZ
492 while (Count < *BufferSize) {\r
493 TimeOut = 0;\r
494 while (TimeOut < mSerialIoMode.Timeout) {\r
495 if (SerialPortPoll ()) {\r
496 break;\r
497 }\r
498 gBS->Stall (10);\r
499 TimeOut += 10;\r
500 }\r
501 if (TimeOut >= mSerialIoMode.Timeout) {\r
502 break;\r
503 }\r
504 SerialPortRead (Buffer, 1);\r
505 Count++;\r
506 Buffer = (VOID *) ((UINT8 *) Buffer + 1);\r
bf6b810f
SZ
507 }\r
508\r
509 if (Count != *BufferSize) {\r
510 *BufferSize = Count;\r
511 return EFI_TIMEOUT;\r
512 }\r
513\r
514 return EFI_SUCCESS;\r
515}\r
516\r
517/**\r
518 Initialization for the Serial Io Protocol.\r
519\r
520 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
521 @param[in] SystemTable A pointer to the EFI System Table.\r
522\r
523 @retval EFI_SUCCESS The entry point is executed successfully.\r
524 @retval other Some error occurs when executing this entry point.\r
525\r
526**/\r
527EFI_STATUS\r
528EFIAPI\r
529SerialDxeInitialize (\r
530 IN EFI_HANDLE ImageHandle,\r
531 IN EFI_SYSTEM_TABLE *SystemTable\r
532 )\r
533{\r
534 EFI_STATUS Status;\r
535\r
bf6b810f
SZ
536 mSerialIoMode.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
537 mSerialIoMode.DataBits = (UINT32) PcdGet8 (PcdUartDefaultDataBits);\r
538 mSerialIoMode.Parity = (UINT32) PcdGet8 (PcdUartDefaultParity);\r
539 mSerialIoMode.StopBits = (UINT32) PcdGet8 (PcdUartDefaultStopBits);\r
f423d760 540 mSerialIoMode.ReceiveFifoDepth = PcdGet16 (PcdUartDefaultReceiveFifoDepth);\r
bf6b810f
SZ
541 mSerialDevicePath.Uart.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
542 mSerialDevicePath.Uart.DataBits = PcdGet8 (PcdUartDefaultDataBits);\r
543 mSerialDevicePath.Uart.Parity = PcdGet8 (PcdUartDefaultParity);\r
544 mSerialDevicePath.Uart.StopBits = PcdGet8 (PcdUartDefaultStopBits);\r
545\r
91cc526b
PB
546 //\r
547 // Issue a reset to initialize the Serial Port\r
548 //\r
549 Status = mSerialIoTemplate.Reset (&mSerialIoTemplate);\r
550 if (EFI_ERROR (Status)) {\r
551 return Status;\r
552 }\r
553\r
bf6b810f
SZ
554 //\r
555 // Make a new handle with Serial IO protocol and its device path on it.\r
556 //\r
557 Status = gBS->InstallMultipleProtocolInterfaces (\r
558 &mSerialHandle,\r
559 &gEfiSerialIoProtocolGuid, &mSerialIoTemplate,\r
560 &gEfiDevicePathProtocolGuid, &mSerialDevicePath,\r
561 NULL\r
562 );\r
563 ASSERT_EFI_ERROR (Status);\r
564\r
565 return Status;\r
566}\r
567\r