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