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