]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/IsaSerialDxe/Serial.c
Coding style modification.
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / IsaSerialDxe / Serial.c
1 /**@file
2 Serial driver for standard UARTS on an ISA bus.
3
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Serial.h"
16
17 //
18 // ISA Serial Driver Global Variables
19 //
20 EFI_DRIVER_BINDING_PROTOCOL gSerialControllerDriver = {
21 SerialControllerDriverSupported,
22 SerialControllerDriverStart,
23 SerialControllerDriverStop,
24 0xa,
25 NULL,
26 NULL
27 };
28
29
30 SERIAL_DEV gSerialDevTempate = {
31 SERIAL_DEV_SIGNATURE,
32 NULL,
33 { // SerialIo
34 SERIAL_IO_INTERFACE_REVISION,
35 IsaSerialReset,
36 IsaSerialSetAttributes,
37 IsaSerialSetControl,
38 IsaSerialGetControl,
39 IsaSerialWrite,
40 IsaSerialRead,
41 NULL
42 },
43 { // SerialMode
44 SERIAL_PORT_DEFAULT_CONTROL_MASK,
45 SERIAL_PORT_DEFAULT_TIMEOUT,
46 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
47 SERIAL_PORT_DEFAULT_RECEIVE_FIFO_DEPTH,
48 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
49 FixedPcdGet8 (PcdUartDefaultParity), // Parity
50 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
51 },
52 NULL,
53 NULL,
54 { // UartDevicePath
55 {
56 MESSAGING_DEVICE_PATH,
57 MSG_UART_DP,
58 {
59 (UINT8) (sizeof (UART_DEVICE_PATH)),
60 (UINT8) ((sizeof (UART_DEVICE_PATH)) >> 8)
61 }
62 },
63 0,
64 FixedPcdGet64 (PcdUartDefaultBaudRate),
65 FixedPcdGet8 (PcdUartDefaultDataBits),
66 FixedPcdGet8 (PcdUartDefaultParity),
67 FixedPcdGet8 (PcdUartDefaultStopBits)
68 },
69 NULL,
70 0, //BaseAddress
71 {
72 0,
73 0,
74 SERIAL_MAX_BUFFER_SIZE,
75 { 0 }
76 },
77 {
78 0,
79 0,
80 SERIAL_MAX_BUFFER_SIZE,
81 { 0 }
82 },
83 FALSE,
84 FALSE,
85 UART16550A,
86 NULL
87 };
88
89 /**
90 The user Entry Point for module IsaSerial. The user code starts with this function.
91
92 @param[in] ImageHandle The firmware allocated handle for the EFI image.
93 @param[in] SystemTable A pointer to the EFI System Table.
94
95 @retval EFI_SUCCESS The entry point is executed successfully.
96 @retval other Some error occurs when executing this entry point.
97
98 **/
99 EFI_STATUS
100 EFIAPI
101 InitializeIsaSerial (
102 IN EFI_HANDLE ImageHandle,
103 IN EFI_SYSTEM_TABLE *SystemTable
104 )
105 {
106 EFI_STATUS Status;
107
108 //
109 // Install driver model protocol(s).
110 //
111 Status = EfiLibInstallDriverBindingComponentName2 (
112 ImageHandle,
113 SystemTable,
114 &gSerialControllerDriver,
115 ImageHandle,
116 &gIsaSerialComponentName,
117 &gIsaSerialComponentName2
118 );
119 ASSERT_EFI_ERROR (Status);
120
121
122 return Status;
123 }
124
125 /**
126 Check to see if this driver supports the given controller
127
128 @param This - A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
129 @param Controller - The handle of the controller to test.
130 @param RemainingDevicePath - A pointer to the remaining portion of a device path.
131
132 @return EFI_SUCCESS - This driver can support the given controller
133
134 **/
135 EFI_STATUS
136 EFIAPI
137 SerialControllerDriverSupported (
138 IN EFI_DRIVER_BINDING_PROTOCOL *This,
139 IN EFI_HANDLE Controller,
140 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
141 )
142
143 {
144 EFI_STATUS Status;
145 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
146 EFI_ISA_IO_PROTOCOL *IsaIo;
147 UART_DEVICE_PATH UartNode;
148
149 //
150 // Ignore the RemainingDevicePath
151 //
152 //
153 // Open the IO Abstraction(s) needed to perform the supported test
154 //
155 Status = gBS->OpenProtocol (
156 Controller,
157 &gEfiDevicePathProtocolGuid,
158 (VOID **) &ParentDevicePath,
159 This->DriverBindingHandle,
160 Controller,
161 EFI_OPEN_PROTOCOL_BY_DRIVER
162 );
163 if (Status == EFI_ALREADY_STARTED) {
164 return EFI_SUCCESS;
165 }
166
167 if (EFI_ERROR (Status)) {
168 return Status;
169 }
170
171 gBS->CloseProtocol (
172 Controller,
173 &gEfiDevicePathProtocolGuid,
174 This->DriverBindingHandle,
175 Controller
176 );
177
178 Status = gBS->OpenProtocol (
179 Controller,
180 &gEfiIsaIoProtocolGuid,
181 (VOID **) &IsaIo,
182 This->DriverBindingHandle,
183 Controller,
184 EFI_OPEN_PROTOCOL_BY_DRIVER
185 );
186
187 if (Status == EFI_ALREADY_STARTED) {
188 return EFI_SUCCESS;
189 }
190
191 if (EFI_ERROR (Status)) {
192 return Status;
193 }
194 //
195 // Use the ISA I/O Protocol to see if Controller is standard ISA UART that
196 // can be managed by this driver.
197 //
198 Status = EFI_SUCCESS;
199 if (IsaIo->ResourceList->Device.HID != EISA_PNP_ID (0x501)) {
200 Status = EFI_UNSUPPORTED;
201 goto Error;
202 }
203 //
204 // Make sure RemainingDevicePath is valid
205 //
206 if (RemainingDevicePath != NULL) {
207 Status = EFI_UNSUPPORTED;
208 CopyMem (
209 &UartNode,
210 (UART_DEVICE_PATH *) RemainingDevicePath,
211 sizeof (UART_DEVICE_PATH)
212 );
213 if (UartNode.Header.Type != MESSAGING_DEVICE_PATH ||
214 UartNode.Header.SubType != MSG_UART_DP ||
215 sizeof (UART_DEVICE_PATH) != DevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL *) &UartNode)
216 ) {
217 goto Error;
218 }
219
220 if (UartNode.BaudRate > SERIAL_PORT_MAX_BAUD_RATE) {
221 goto Error;
222 }
223
224 if (UartNode.Parity < NoParity || UartNode.Parity > SpaceParity) {
225 goto Error;
226 }
227
228 if (UartNode.DataBits < 5 || UartNode.DataBits > 8) {
229 goto Error;
230 }
231
232 if (UartNode.StopBits < OneStopBit || UartNode.StopBits > TwoStopBits) {
233 goto Error;
234 }
235
236 if ((UartNode.DataBits == 5) && (UartNode.StopBits == TwoStopBits)) {
237 goto Error;
238 }
239
240 if ((UartNode.DataBits >= 6) && (UartNode.DataBits <= 8) && (UartNode.StopBits == OneFiveStopBits)) {
241 goto Error;
242 }
243
244 Status = EFI_SUCCESS;
245 }
246
247 Error:
248 //
249 // Close the I/O Abstraction(s) used to perform the supported test
250 //
251 gBS->CloseProtocol (
252 Controller,
253 &gEfiIsaIoProtocolGuid,
254 This->DriverBindingHandle,
255 Controller
256 );
257
258 return Status;
259 }
260
261 /**
262 Start to management the controller passed in
263
264 @param This - A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
265 @param Controller - The handle of the controller to test.
266 @param RemainingDevicePath - A pointer to the remaining portion of a device path.
267
268 @return EFI_SUCCESS - Driver is started successfully
269
270 **/
271 EFI_STATUS
272 EFIAPI
273 SerialControllerDriverStart (
274 IN EFI_DRIVER_BINDING_PROTOCOL *This,
275 IN EFI_HANDLE Controller,
276 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
277 )
278
279 {
280 EFI_STATUS Status;
281 EFI_ISA_IO_PROTOCOL *IsaIo;
282 SERIAL_DEV *SerialDevice;
283 UINTN Index;
284 UART_DEVICE_PATH Node;
285 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
286 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
287 UINTN EntryCount;
288 EFI_SERIAL_IO_PROTOCOL *SerialIo;
289
290 SerialDevice = NULL;
291 //
292 // Get the Parent Device Path
293 //
294 Status = gBS->OpenProtocol (
295 Controller,
296 &gEfiDevicePathProtocolGuid,
297 (VOID **) &ParentDevicePath,
298 This->DriverBindingHandle,
299 Controller,
300 EFI_OPEN_PROTOCOL_BY_DRIVER
301 );
302 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
303 return Status;
304 }
305 //
306 // Report status code enable the serial
307 //
308 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
309 EFI_PROGRESS_CODE,
310 EFI_P_PC_ENABLE | EFI_PERIPHERAL_SERIAL_PORT,
311 ParentDevicePath
312 );
313
314 //
315 // Grab the IO abstraction we need to get any work done
316 //
317 Status = gBS->OpenProtocol (
318 Controller,
319 &gEfiIsaIoProtocolGuid,
320 (VOID **) &IsaIo,
321 This->DriverBindingHandle,
322 Controller,
323 EFI_OPEN_PROTOCOL_BY_DRIVER
324 );
325 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
326 goto Error;
327 }
328
329 if (Status == EFI_ALREADY_STARTED) {
330
331 if (RemainingDevicePath == NULL) {
332 return EFI_SUCCESS;
333 }
334 //
335 // Make sure a child handle does not already exist. This driver can only
336 // produce one child per serial port.
337 //
338 Status = gBS->OpenProtocolInformation (
339 Controller,
340 &gEfiIsaIoProtocolGuid,
341 &OpenInfoBuffer,
342 &EntryCount
343 );
344 if (EFI_ERROR (Status)) {
345 return Status;
346 }
347
348 Status = EFI_ALREADY_STARTED;
349 for (Index = 0; Index < EntryCount; Index++) {
350 if (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
351 Status = gBS->OpenProtocol (
352 OpenInfoBuffer[Index].ControllerHandle,
353 &gEfiSerialIoProtocolGuid,
354 (VOID **) &SerialIo,
355 This->DriverBindingHandle,
356 Controller,
357 EFI_OPEN_PROTOCOL_GET_PROTOCOL
358 );
359 if (!EFI_ERROR (Status)) {
360 CopyMem (&Node, RemainingDevicePath, sizeof (UART_DEVICE_PATH));
361 Status = SerialIo->SetAttributes (
362 SerialIo,
363 Node.BaudRate,
364 SerialIo->Mode->ReceiveFifoDepth,
365 SerialIo->Mode->Timeout,
366 (EFI_PARITY_TYPE) Node.Parity,
367 Node.DataBits,
368 (EFI_STOP_BITS_TYPE) Node.StopBits
369 );
370 }
371 break;
372 }
373 }
374
375 gBS->FreePool (OpenInfoBuffer);
376 return Status;
377 }
378 //
379 // Initialize the serial device instance
380 //
381 SerialDevice = AllocateCopyPool (sizeof (SERIAL_DEV), &gSerialDevTempate);
382 if (SerialDevice == NULL) {
383 Status = EFI_OUT_OF_RESOURCES;
384 goto Error;
385 }
386
387 SerialDevice->SerialIo.Mode = &(SerialDevice->SerialMode);
388 SerialDevice->IsaIo = IsaIo;
389 SerialDevice->ParentDevicePath = ParentDevicePath;
390
391 ADD_SERIAL_NAME (SerialDevice, IsaIo);
392
393 for (Index = 0; SerialDevice->IsaIo->ResourceList->ResourceItem[Index].Type != EfiIsaAcpiResourceEndOfList; Index++) {
394 if (SerialDevice->IsaIo->ResourceList->ResourceItem[Index].Type == EfiIsaAcpiResourceIo) {
395 SerialDevice->BaseAddress = (UINT16) SerialDevice->IsaIo->ResourceList->ResourceItem[Index].StartRange;
396 }
397 }
398 //
399 // Report status code the serial present
400 //
401 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
402 EFI_PROGRESS_CODE,
403 EFI_P_PC_PRESENCE_DETECT | EFI_PERIPHERAL_SERIAL_PORT,
404 ParentDevicePath
405 );
406
407 if (!IsaSerialPortPresent (SerialDevice)) {
408 Status = EFI_DEVICE_ERROR;
409 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
410 EFI_ERROR_CODE,
411 EFI_P_EC_NOT_DETECTED | EFI_PERIPHERAL_SERIAL_PORT,
412 ParentDevicePath
413 );
414 goto Error;
415 }
416
417 if (RemainingDevicePath != NULL) {
418 //
419 // Match the configuration of the RemainingDevicePath. IsHandleSupported()
420 // already checked to make sure the RemainingDevicePath contains settings
421 // that we can support.
422 //
423 CopyMem (&SerialDevice->UartDevicePath, RemainingDevicePath, sizeof (UART_DEVICE_PATH));
424 } else {
425 //
426 // Use the values from the gSerialDevTempate as no remaining device path was
427 // passed in.
428 //
429 }
430 //
431 // Build the device path by appending the UART node to the ParentDevicePath
432 // from the WinNtIo handle. The Uart setings are zero here, since
433 // SetAttribute() will update them to match the current setings.
434 //
435 SerialDevice->DevicePath = AppendDevicePathNode (
436 ParentDevicePath,
437 (EFI_DEVICE_PATH_PROTOCOL *)&SerialDevice->UartDevicePath
438 );
439 if (SerialDevice->DevicePath == NULL) {
440 Status = EFI_DEVICE_ERROR;
441 goto Error;
442 }
443
444 //
445 // Fill in Serial I/O Mode structure based on either the RemainingDevicePath or defaults.
446 //
447 SerialDevice->SerialMode.BaudRate = SerialDevice->UartDevicePath.BaudRate;
448 SerialDevice->SerialMode.DataBits = SerialDevice->UartDevicePath.DataBits;
449 SerialDevice->SerialMode.Parity = SerialDevice->UartDevicePath.Parity;
450 SerialDevice->SerialMode.StopBits = SerialDevice->UartDevicePath.StopBits;
451
452 //
453 // Issue a reset to initialize the COM port
454 //
455 Status = SerialDevice->SerialIo.Reset (&SerialDevice->SerialIo);
456 if (EFI_ERROR (Status)) {
457 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
458 EFI_ERROR_CODE,
459 EFI_P_EC_CONTROLLER_ERROR | EFI_PERIPHERAL_SERIAL_PORT,
460 ParentDevicePath
461 );
462 goto Error;
463 }
464 //
465 // Install protocol interfaces for the serial device.
466 //
467 Status = gBS->InstallMultipleProtocolInterfaces (
468 &SerialDevice->Handle,
469 &gEfiDevicePathProtocolGuid,
470 SerialDevice->DevicePath,
471 &gEfiSerialIoProtocolGuid,
472 &SerialDevice->SerialIo,
473 NULL
474 );
475 if (EFI_ERROR (Status)) {
476 goto Error;
477 }
478 //
479 // Open For Child Device
480 //
481 Status = gBS->OpenProtocol (
482 Controller,
483 &gEfiIsaIoProtocolGuid,
484 (VOID **) &IsaIo,
485 This->DriverBindingHandle,
486 SerialDevice->Handle,
487 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
488 );
489
490 Error:
491 if (EFI_ERROR (Status)) {
492 gBS->CloseProtocol (
493 Controller,
494 &gEfiDevicePathProtocolGuid,
495 This->DriverBindingHandle,
496 Controller
497 );
498 gBS->CloseProtocol (
499 Controller,
500 &gEfiIsaIoProtocolGuid,
501 This->DriverBindingHandle,
502 Controller
503 );
504 if (SerialDevice) {
505 if (SerialDevice->DevicePath) {
506 gBS->FreePool (SerialDevice->DevicePath);
507 }
508
509 FreeUnicodeStringTable (SerialDevice->ControllerNameTable);
510 gBS->FreePool (SerialDevice);
511 }
512 }
513
514 return Status;
515 }
516
517 /**
518 Disconnect this driver with the controller, uninstall related protocol instance
519
520 @param This - A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
521 @param Controller - The handle of the controller to test.
522 @param NumberOfChildren - Number of child device.
523 @param RemainingDevicePath - A pointer to the remaining portion of a device path.
524
525 @retval EFI_SUCCESS - Operation successfully
526 @retval EFI_DEVICE_ERROR - Cannot stop the driver successfully
527
528 **/
529 EFI_STATUS
530 EFIAPI
531 SerialControllerDriverStop (
532 IN EFI_DRIVER_BINDING_PROTOCOL *This,
533 IN EFI_HANDLE Controller,
534 IN UINTN NumberOfChildren,
535 IN EFI_HANDLE *ChildHandleBuffer
536 )
537
538 {
539 EFI_STATUS Status;
540 UINTN Index;
541 BOOLEAN AllChildrenStopped;
542 EFI_SERIAL_IO_PROTOCOL *SerialIo;
543 SERIAL_DEV *SerialDevice;
544 EFI_ISA_IO_PROTOCOL *IsaIo;
545 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
546
547 Status = gBS->HandleProtocol (
548 Controller,
549 &gEfiDevicePathProtocolGuid,
550 (VOID **) &DevicePath
551 );
552
553 //
554 // Report the status code disable the serial
555 //
556 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
557 EFI_PROGRESS_CODE,
558 EFI_P_PC_DISABLE | EFI_PERIPHERAL_SERIAL_PORT,
559 DevicePath
560 );
561
562 //
563 // Complete all outstanding transactions to Controller.
564 // Don't allow any new transaction to Controller to be started.
565 //
566 if (NumberOfChildren == 0) {
567 //
568 // Close the bus driver
569 //
570 Status = gBS->CloseProtocol (
571 Controller,
572 &gEfiIsaIoProtocolGuid,
573 This->DriverBindingHandle,
574 Controller
575 );
576
577 Status = gBS->CloseProtocol (
578 Controller,
579 &gEfiDevicePathProtocolGuid,
580 This->DriverBindingHandle,
581 Controller
582 );
583 return Status;
584 }
585
586 AllChildrenStopped = TRUE;
587
588 for (Index = 0; Index < NumberOfChildren; Index++) {
589
590 Status = gBS->OpenProtocol (
591 ChildHandleBuffer[Index],
592 &gEfiSerialIoProtocolGuid,
593 (VOID **) &SerialIo,
594 This->DriverBindingHandle,
595 Controller,
596 EFI_OPEN_PROTOCOL_GET_PROTOCOL
597 );
598 if (!EFI_ERROR (Status)) {
599
600 SerialDevice = SERIAL_DEV_FROM_THIS (SerialIo);
601
602 Status = gBS->CloseProtocol (
603 Controller,
604 &gEfiIsaIoProtocolGuid,
605 This->DriverBindingHandle,
606 ChildHandleBuffer[Index]
607 );
608
609 Status = gBS->UninstallMultipleProtocolInterfaces (
610 ChildHandleBuffer[Index],
611 &gEfiDevicePathProtocolGuid,
612 SerialDevice->DevicePath,
613 &gEfiSerialIoProtocolGuid,
614 &SerialDevice->SerialIo,
615 NULL
616 );
617 if (EFI_ERROR (Status)) {
618 gBS->OpenProtocol (
619 Controller,
620 &gEfiIsaIoProtocolGuid,
621 (VOID **) &IsaIo,
622 This->DriverBindingHandle,
623 ChildHandleBuffer[Index],
624 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
625 );
626 } else {
627 if (SerialDevice->DevicePath) {
628 gBS->FreePool (SerialDevice->DevicePath);
629 }
630
631 FreeUnicodeStringTable (SerialDevice->ControllerNameTable);
632 gBS->FreePool (SerialDevice);
633 }
634 }
635
636 if (EFI_ERROR (Status)) {
637 AllChildrenStopped = FALSE;
638 }
639 }
640
641 if (!AllChildrenStopped) {
642 return EFI_DEVICE_ERROR;
643 }
644
645 return EFI_SUCCESS;
646 }
647
648 /**
649 Detect whether specific FIFO is full or not
650
651 @param Fifo - A pointer to the Data Structure SERIAL_DEV_FIFO
652
653 @return whether specific FIFO is full or not
654
655 **/
656 BOOLEAN
657 IsaSerialFifoFull (
658 IN SERIAL_DEV_FIFO *Fifo
659 )
660
661 {
662 if (Fifo->Surplus == 0) {
663 return TRUE;
664 }
665
666 return FALSE;
667 }
668
669 /**
670 Detect whether specific FIFO is empty or not
671
672
673 @param Fifo - A pointer to the Data Structure SERIAL_DEV_FIFO
674
675 @return whether specific FIFO is empty or not
676
677 **/
678 BOOLEAN
679 IsaSerialFifoEmpty (
680 IN SERIAL_DEV_FIFO *Fifo
681 )
682
683 {
684 if (Fifo->Surplus == SERIAL_MAX_BUFFER_SIZE) {
685 return TRUE;
686 }
687
688 return FALSE;
689 }
690
691 /**
692 Add data to specific FIFO
693
694 @param Fifo - A pointer to the Data Structure SERIAL_DEV_FIFO
695 @param Data - the data added to FIFO
696
697 @retval EFI_SUCCESS - Add data to specific FIFO successfully
698 @retval EFI_OUT_OF_RESOURCE - Failed to add data because FIFO is already full
699
700 **/
701 EFI_STATUS
702 IsaSerialFifoAdd (
703 IN SERIAL_DEV_FIFO *Fifo,
704 IN UINT8 Data
705 )
706
707 {
708 //
709 // if FIFO full can not add data
710 //
711 if (IsaSerialFifoFull (Fifo)) {
712 return EFI_OUT_OF_RESOURCES;
713 }
714 //
715 // FIFO is not full can add data
716 //
717 Fifo->Data[Fifo->Last] = Data;
718 Fifo->Surplus--;
719 Fifo->Last++;
720 if (Fifo->Last == SERIAL_MAX_BUFFER_SIZE) {
721 Fifo->Last = 0;
722 }
723
724 return EFI_SUCCESS;
725 }
726
727 /**
728 Remove data from specific FIFO
729
730 @param Fifo - A pointer to the Data Structure SERIAL_DEV_FIFO
731 @param Data - the data removed from FIFO
732
733 @retval EFI_SUCCESS - Remove data from specific FIFO successfully
734 @retval EFI_OUT_OF_RESOURCE - Failed to remove data because FIFO is empty
735
736 **/
737 EFI_STATUS
738 IsaSerialFifoRemove (
739 IN SERIAL_DEV_FIFO *Fifo,
740 OUT UINT8 *Data
741 )
742
743 {
744 //
745 // if FIFO is empty, no data can remove
746 //
747 if (IsaSerialFifoEmpty (Fifo)) {
748 return EFI_OUT_OF_RESOURCES;
749 }
750 //
751 // FIFO is not empty, can remove data
752 //
753 *Data = Fifo->Data[Fifo->First];
754 Fifo->Surplus++;
755 Fifo->First++;
756 if (Fifo->First == SERIAL_MAX_BUFFER_SIZE) {
757 Fifo->First = 0;
758 }
759
760 return EFI_SUCCESS;
761 }
762
763 /**
764 Reads and writes all avaliable data.
765
766 @param SerialDevice - The device to flush
767
768 @retval EFI_SUCCESS - Data was read/written successfully.
769 @retval EFI_OUT_OF_RESOURCE - Failed because software receive FIFO is full. Note, when
770 this happens, pending writes are not done.
771
772 **/
773 EFI_STATUS
774 IsaSerialReceiveTransmit (
775 IN SERIAL_DEV *SerialDevice
776 )
777
778 {
779 SERIAL_PORT_LSR Lsr;
780 UINT8 Data;
781 BOOLEAN ReceiveFifoFull;
782 SERIAL_PORT_MSR Msr;
783 SERIAL_PORT_MCR Mcr;
784 UINTN TimeOut;
785
786 Data = 0;
787
788 //
789 // Begin the read or write
790 //
791 if (SerialDevice->SoftwareLoopbackEnable) {
792 do {
793 ReceiveFifoFull = IsaSerialFifoFull (&SerialDevice->Receive);
794 if (!IsaSerialFifoEmpty (&SerialDevice->Transmit)) {
795 IsaSerialFifoRemove (&SerialDevice->Transmit, &Data);
796 if (ReceiveFifoFull) {
797 return EFI_OUT_OF_RESOURCES;
798 }
799
800 IsaSerialFifoAdd (&SerialDevice->Receive, Data);
801 }
802 } while (!IsaSerialFifoEmpty (&SerialDevice->Transmit));
803 } else {
804 ReceiveFifoFull = IsaSerialFifoFull (&SerialDevice->Receive);
805 do {
806 Lsr.Data = READ_LSR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
807
808 if (FeaturePcdGet (PcdNtEmulatorEnable)) {
809 //
810 // This is required for NT to avoid a forever-spin...
811 // This would be better if READ_LSR was a polling operation
812 // that would timeout.
813 //
814 Lsr.Bits.THRE = 1;
815 }
816 //
817 // Flush incomming data to prevent a an overrun during a long write
818 //
819 if (Lsr.Bits.DR && !ReceiveFifoFull) {
820 ReceiveFifoFull = IsaSerialFifoFull (&SerialDevice->Receive);
821 if (!ReceiveFifoFull) {
822 if (Lsr.Bits.FIFOE || Lsr.Bits.OE || Lsr.Bits.PE || Lsr.Bits.FE || Lsr.Bits.BI) {
823 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
824 EFI_ERROR_CODE,
825 EFI_P_EC_INPUT_ERROR | EFI_PERIPHERAL_SERIAL_PORT,
826 SerialDevice->DevicePath
827 );
828 if (Lsr.Bits.FIFOE || Lsr.Bits.PE || Lsr.Bits.FE || Lsr.Bits.BI) {
829 Data = READ_RBR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
830 continue;
831 }
832 }
833 //
834 // Make sure the receive data will not be missed, Assert DTR
835 //
836 if (SerialDevice->HardwareFlowControl) {
837 Mcr.Data = READ_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
838 Mcr.Bits.DTRC &= 0;
839 WRITE_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Mcr.Data);
840 }
841
842 Data = READ_RBR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
843
844 //
845 // Deassert DTR
846 //
847 if (SerialDevice->HardwareFlowControl) {
848 Mcr.Data = READ_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
849 Mcr.Bits.DTRC |= 1;
850 WRITE_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Mcr.Data);
851 }
852
853 IsaSerialFifoAdd (&SerialDevice->Receive, Data);
854
855 continue;
856 } else {
857 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
858 EFI_PROGRESS_CODE,
859 EFI_P_SERIAL_PORT_PC_CLEAR_BUFFER | EFI_PERIPHERAL_SERIAL_PORT,
860 SerialDevice->DevicePath
861 );
862 }
863 }
864 //
865 // Do the write
866 //
867 if (Lsr.Bits.THRE && !IsaSerialFifoEmpty (&SerialDevice->Transmit)) {
868 //
869 // Make sure the transmit data will not be missed
870 //
871 if (SerialDevice->HardwareFlowControl) {
872 //
873 // Send RTS
874 //
875 Mcr.Data = READ_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
876 Mcr.Bits.RTS |= 1;
877 WRITE_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Mcr.Data);
878 //
879 // Wait for CTS
880 //
881 TimeOut = 0;
882 Msr.Data = READ_MSR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
883 while (!Msr.Bits.CTS) {
884 gBS->Stall (TIMEOUT_STALL_INTERVAL);
885 TimeOut++;
886 if (TimeOut > 5) {
887 break;
888 }
889
890 Msr.Data = READ_MSR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
891 }
892
893 if (Msr.Bits.CTS) {
894 IsaSerialFifoRemove (&SerialDevice->Transmit, &Data);
895 WRITE_THR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Data);
896 }
897 }
898 //
899 // write the data out
900 //
901 if (!SerialDevice->HardwareFlowControl) {
902 IsaSerialFifoRemove (&SerialDevice->Transmit, &Data);
903 WRITE_THR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Data);
904 }
905 //
906 // Make sure the transmit data will not be missed
907 //
908 if (SerialDevice->HardwareFlowControl) {
909 //
910 // Assert RTS
911 //
912 Mcr.Data = READ_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
913 Mcr.Bits.RTS &= 0;
914 WRITE_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Mcr.Data);
915 }
916 }
917 } while (Lsr.Bits.THRE && !IsaSerialFifoEmpty (&SerialDevice->Transmit));
918 }
919
920 return EFI_SUCCESS;
921 }
922
923 //
924 // Interface Functions
925 //
926 /**
927 Reset serial device
928
929 @param This - Pointer to EFI_SERIAL_IO_PROTOCOL
930
931 @retval EFI_SUCCESS - Reset successfully
932 @retval EFI_DEVICE_ERROR - Failed to reset
933
934 **/
935 EFI_STATUS
936 EFIAPI
937 IsaSerialReset (
938 IN EFI_SERIAL_IO_PROTOCOL *This
939 )
940 {
941 EFI_STATUS Status;
942 SERIAL_DEV *SerialDevice;
943 SERIAL_PORT_LCR Lcr;
944 SERIAL_PORT_IER Ier;
945 SERIAL_PORT_MCR Mcr;
946 SERIAL_PORT_FCR Fcr;
947 EFI_TPL Tpl;
948
949 SerialDevice = SERIAL_DEV_FROM_THIS (This);
950
951 //
952 // Report the status code reset the serial
953 //
954 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
955 EFI_PROGRESS_CODE,
956 EFI_P_PC_RESET | EFI_PERIPHERAL_SERIAL_PORT,
957 SerialDevice->DevicePath
958 );
959
960 Tpl = gBS->RaiseTPL (TPL_NOTIFY);
961
962 //
963 // Make sure DLAB is 0.
964 //
965 Lcr.Data = READ_LCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
966 Lcr.Bits.DLAB = 0;
967 WRITE_LCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Lcr.Data);
968
969 //
970 // Turn off all interrupts
971 //
972 Ier.Data = READ_IER (SerialDevice->IsaIo, SerialDevice->BaseAddress);
973 Ier.Bits.RAVIE = 0;
974 Ier.Bits.THEIE = 0;
975 Ier.Bits.RIE = 0;
976 Ier.Bits.MIE = 0;
977 WRITE_IER (SerialDevice->IsaIo, SerialDevice->BaseAddress, Ier.Data);
978
979 //
980 // Disable the FIFO.
981 //
982 Fcr.Bits.TRFIFOE = 0;
983 WRITE_FCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Fcr.Data);
984
985 //
986 // Turn off loopback and disable device interrupt.
987 //
988 Mcr.Data = READ_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
989 Mcr.Bits.OUT1 = 0;
990 Mcr.Bits.OUT2 = 0;
991 Mcr.Bits.LME = 0;
992 WRITE_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Mcr.Data);
993
994 //
995 // Clear the scratch pad register
996 //
997 WRITE_SCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, 0);
998
999 //
1000 // Go set the current attributes
1001 //
1002 Status = This->SetAttributes (
1003 This,
1004 This->Mode->BaudRate,
1005 This->Mode->ReceiveFifoDepth,
1006 This->Mode->Timeout,
1007 (EFI_PARITY_TYPE) This->Mode->Parity,
1008 (UINT8) This->Mode->DataBits,
1009 (EFI_STOP_BITS_TYPE) This->Mode->StopBits
1010 );
1011
1012 if (EFI_ERROR (Status)) {
1013 gBS->RestoreTPL (Tpl);
1014 return EFI_DEVICE_ERROR;
1015 }
1016 //
1017 // Go set the current control bits
1018 //
1019 Status = This->SetControl (
1020 This,
1021 This->Mode->ControlMask
1022 );
1023
1024 if (EFI_ERROR (Status)) {
1025 gBS->RestoreTPL (Tpl);
1026 return EFI_DEVICE_ERROR;
1027 }
1028 //
1029 // for 16550A enable FIFO, 16550 disable FIFO
1030 //
1031 Fcr.Bits.TRFIFOE = 1;
1032 Fcr.Bits.RESETRF = 1;
1033 Fcr.Bits.RESETTF = 1;
1034 WRITE_FCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Fcr.Data);
1035
1036 //
1037 // Reset the software FIFO
1038 //
1039 SerialDevice->Receive.First = 0;
1040 SerialDevice->Receive.Last = 0;
1041 SerialDevice->Receive.Surplus = SERIAL_MAX_BUFFER_SIZE;
1042 SerialDevice->Transmit.First = 0;
1043 SerialDevice->Transmit.Last = 0;
1044 SerialDevice->Transmit.Surplus = SERIAL_MAX_BUFFER_SIZE;
1045
1046 gBS->RestoreTPL (Tpl);
1047
1048 //
1049 // Device reset is complete
1050 //
1051 return EFI_SUCCESS;
1052 }
1053
1054 /**
1055 Set new attributes to a serial device
1056
1057 @param This - Pointer to EFI_SERIAL_IO_PROTOCOL
1058 @param BaudRate - The baudrate of the serial device
1059 @param ReceiveFifoDepth - The depth of receive FIFO buffer
1060 @param Timeout - The request timeout for a single char
1061 @param Parity - The type of parity used in serial device
1062 @param DataBits - Number of databits used in serial device
1063 @param StopBits - Number of stopbits used in serial device
1064
1065 @retval EFI_SUCCESS - The new attributes were set
1066 @retval EFI_INVALID_PARAMETERS - One or more attributes have an unsupported value
1067 @retval EFI_UNSUPPORTED - Data Bits can not set to 5 or 6
1068 @retval EFI_DEVICE_ERROR - The serial device is not functioning correctly (no return)
1069
1070 **/
1071 EFI_STATUS
1072 EFIAPI
1073 IsaSerialSetAttributes (
1074 IN EFI_SERIAL_IO_PROTOCOL *This,
1075 IN UINT64 BaudRate,
1076 IN UINT32 ReceiveFifoDepth,
1077 IN UINT32 Timeout,
1078 IN EFI_PARITY_TYPE Parity,
1079 IN UINT8 DataBits,
1080 IN EFI_STOP_BITS_TYPE StopBits
1081 )
1082 {
1083 EFI_STATUS Status;
1084 SERIAL_DEV *SerialDevice;
1085 UINT32 Divisor;
1086 UINT32 Remained;
1087 SERIAL_PORT_LCR Lcr;
1088 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
1089 EFI_TPL Tpl;
1090
1091 SerialDevice = SERIAL_DEV_FROM_THIS (This);
1092
1093 //
1094 // Check for default settings and fill in actual values.
1095 //
1096 if (BaudRate == 0) {
1097 BaudRate = FixedPcdGet64 (PcdUartDefaultBaudRate);
1098 }
1099
1100 if (ReceiveFifoDepth == 0) {
1101 ReceiveFifoDepth = SERIAL_PORT_DEFAULT_RECEIVE_FIFO_DEPTH;
1102 }
1103
1104 if (Timeout == 0) {
1105 Timeout = SERIAL_PORT_DEFAULT_TIMEOUT;
1106 }
1107
1108 if (Parity == DefaultParity) {
1109 Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);
1110 }
1111
1112 if (DataBits == 0) {
1113 DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);
1114 }
1115
1116 if (StopBits == DefaultStopBits) {
1117 StopBits = (EFI_STOP_BITS_TYPE) FixedPcdGet8 (PcdUartDefaultStopBits);
1118 }
1119 //
1120 // 5 and 6 data bits can not be verified on a 16550A UART
1121 // Return EFI_INVALID_PARAMETER if an attempt is made to use these settings.
1122 //
1123 if ((DataBits == 5) || (DataBits == 6)) {
1124 return EFI_INVALID_PARAMETER;
1125 }
1126 //
1127 // Make sure all parameters are valid
1128 //
1129 if ((BaudRate > SERIAL_PORT_MAX_BAUD_RATE) || (BaudRate < SERIAL_PORT_MIN_BAUD_RATE)) {
1130 return EFI_INVALID_PARAMETER;
1131 }
1132 //
1133 // 50,75,110,134,150,300,600,1200,1800,2000,2400,3600,4800,7200,9600,19200,
1134 // 38400,57600,115200
1135 //
1136 if (BaudRate < 75) {
1137 BaudRate = 50;
1138 } else if (BaudRate < 110) {
1139 BaudRate = 75;
1140 } else if (BaudRate < 134) {
1141 BaudRate = 110;
1142 } else if (BaudRate < 150) {
1143 BaudRate = 134;
1144 } else if (BaudRate < 300) {
1145 BaudRate = 150;
1146 } else if (BaudRate < 600) {
1147 BaudRate = 300;
1148 } else if (BaudRate < 1200) {
1149 BaudRate = 600;
1150 } else if (BaudRate < 1800) {
1151 BaudRate = 1200;
1152 } else if (BaudRate < 2000) {
1153 BaudRate = 1800;
1154 } else if (BaudRate < 2400) {
1155 BaudRate = 2000;
1156 } else if (BaudRate < 3600) {
1157 BaudRate = 2400;
1158 } else if (BaudRate < 4800) {
1159 BaudRate = 3600;
1160 } else if (BaudRate < 7200) {
1161 BaudRate = 4800;
1162 } else if (BaudRate < 9600) {
1163 BaudRate = 7200;
1164 } else if (BaudRate < 19200) {
1165 BaudRate = 9600;
1166 } else if (BaudRate < 38400) {
1167 BaudRate = 19200;
1168 } else if (BaudRate < 57600) {
1169 BaudRate = 38400;
1170 } else if (BaudRate < 115200) {
1171 BaudRate = 57600;
1172 } else if (BaudRate <= SERIAL_PORT_MAX_BAUD_RATE) {
1173 BaudRate = 115200;
1174 }
1175
1176 if ((ReceiveFifoDepth < 1) || (ReceiveFifoDepth > SERIAL_PORT_MAX_RECEIVE_FIFO_DEPTH)) {
1177 return EFI_INVALID_PARAMETER;
1178 }
1179
1180 if ((Timeout < SERIAL_PORT_MIN_TIMEOUT) || (Timeout > SERIAL_PORT_MAX_TIMEOUT)) {
1181 return EFI_INVALID_PARAMETER;
1182 }
1183
1184 if ((Parity < NoParity) || (Parity > SpaceParity)) {
1185 return EFI_INVALID_PARAMETER;
1186 }
1187
1188 if ((DataBits < 5) || (DataBits > 8)) {
1189 return EFI_INVALID_PARAMETER;
1190 }
1191
1192 if ((StopBits < OneStopBit) || (StopBits > TwoStopBits)) {
1193 return EFI_INVALID_PARAMETER;
1194 }
1195 //
1196 // for DataBits = 5, StopBits can not set TwoStopBits
1197 //
1198 // if ((DataBits == 5) && (StopBits == TwoStopBits)) {
1199 // return EFI_INVALID_PARAMETER;
1200 // }
1201 //
1202 // for DataBits = 6,7,8, StopBits can not set OneFiveStopBits
1203 //
1204 if ((DataBits >= 6) && (DataBits <= 8) && (StopBits == OneFiveStopBits)) {
1205 return EFI_INVALID_PARAMETER;
1206 }
1207
1208 //
1209 // Compute divisor use to program the baud rate using a round determination
1210 //
1211 Divisor = (UINT32) DivU64x32Remainder (
1212 SERIAL_PORT_INPUT_CLOCK,
1213 ((UINT32) BaudRate * 16),
1214 &Remained
1215 );
1216 if (Remained) {
1217 Divisor += 1;
1218 }
1219
1220 if ((Divisor == 0) || (Divisor & 0xffff0000)) {
1221 return EFI_INVALID_PARAMETER;
1222 }
1223
1224 Tpl = gBS->RaiseTPL (TPL_NOTIFY);
1225
1226 //
1227 // Compute the actual baud rate that the serial port will be programmed for.
1228 //
1229 BaudRate = SERIAL_PORT_INPUT_CLOCK / Divisor / 16;
1230
1231 //
1232 // Put serial port on Divisor Latch Mode
1233 //
1234 Lcr.Data = READ_LCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
1235 Lcr.Bits.DLAB = 1;
1236 WRITE_LCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Lcr.Data);
1237
1238 //
1239 // Write the divisor to the serial port
1240 //
1241 WRITE_DLL (SerialDevice->IsaIo, SerialDevice->BaseAddress, (UINT8) (Divisor & 0xff));
1242 WRITE_DLM (SerialDevice->IsaIo, SerialDevice->BaseAddress, (UINT8) ((Divisor >> 8) & 0xff));
1243
1244 //
1245 // Put serial port back in normal mode and set remaining attributes.
1246 //
1247 Lcr.Bits.DLAB = 0;
1248
1249 switch (Parity) {
1250 case NoParity:
1251 Lcr.Bits.PAREN = 0;
1252 Lcr.Bits.EVENPAR = 0;
1253 Lcr.Bits.STICPAR = 0;
1254 break;
1255
1256 case EvenParity:
1257 Lcr.Bits.PAREN = 1;
1258 Lcr.Bits.EVENPAR = 1;
1259 Lcr.Bits.STICPAR = 0;
1260 break;
1261
1262 case OddParity:
1263 Lcr.Bits.PAREN = 1;
1264 Lcr.Bits.EVENPAR = 0;
1265 Lcr.Bits.STICPAR = 0;
1266 break;
1267
1268 case SpaceParity:
1269 Lcr.Bits.PAREN = 1;
1270 Lcr.Bits.EVENPAR = 1;
1271 Lcr.Bits.STICPAR = 1;
1272 break;
1273
1274 case MarkParity:
1275 Lcr.Bits.PAREN = 1;
1276 Lcr.Bits.EVENPAR = 0;
1277 Lcr.Bits.STICPAR = 1;
1278 break;
1279
1280 default:
1281 break;
1282 }
1283
1284 switch (StopBits) {
1285 case OneStopBit:
1286 Lcr.Bits.STOPB = 0;
1287 break;
1288
1289 case OneFiveStopBits:
1290 case TwoStopBits:
1291 Lcr.Bits.STOPB = 1;
1292 break;
1293
1294 default:
1295 break;
1296 }
1297 //
1298 // DataBits
1299 //
1300 Lcr.Bits.SERIALDB = (UINT8) ((DataBits - 5) & 0x03);
1301 WRITE_LCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Lcr.Data);
1302
1303 //
1304 // Set the Serial I/O mode
1305 //
1306 This->Mode->BaudRate = BaudRate;
1307 This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;
1308 This->Mode->Timeout = Timeout;
1309 This->Mode->Parity = Parity;
1310 This->Mode->DataBits = DataBits;
1311 This->Mode->StopBits = StopBits;
1312
1313 //
1314 // See if Device Path Node has actually changed
1315 //
1316 if (SerialDevice->UartDevicePath.BaudRate == BaudRate &&
1317 SerialDevice->UartDevicePath.DataBits == DataBits &&
1318 SerialDevice->UartDevicePath.Parity == Parity &&
1319 SerialDevice->UartDevicePath.StopBits == StopBits
1320 ) {
1321 gBS->RestoreTPL (Tpl);
1322 return EFI_SUCCESS;
1323 }
1324 //
1325 // Update the device path
1326 //
1327 SerialDevice->UartDevicePath.BaudRate = BaudRate;
1328 SerialDevice->UartDevicePath.DataBits = DataBits;
1329 SerialDevice->UartDevicePath.Parity = (UINT8) Parity;
1330 SerialDevice->UartDevicePath.StopBits = (UINT8) StopBits;
1331
1332 NewDevicePath = AppendDevicePathNode (
1333 SerialDevice->ParentDevicePath,
1334 (EFI_DEVICE_PATH_PROTOCOL *) &SerialDevice->UartDevicePath
1335 );
1336 if (NewDevicePath == NULL) {
1337 gBS->RestoreTPL (Tpl);
1338 return EFI_DEVICE_ERROR;
1339 }
1340
1341 if (SerialDevice->Handle != NULL) {
1342 Status = gBS->ReinstallProtocolInterface (
1343 SerialDevice->Handle,
1344 &gEfiDevicePathProtocolGuid,
1345 SerialDevice->DevicePath,
1346 NewDevicePath
1347 );
1348 if (EFI_ERROR (Status)) {
1349 gBS->RestoreTPL (Tpl);
1350 return Status;
1351 }
1352 }
1353
1354 if (SerialDevice->DevicePath) {
1355 gBS->FreePool (SerialDevice->DevicePath);
1356 }
1357
1358 SerialDevice->DevicePath = NewDevicePath;
1359
1360 gBS->RestoreTPL (Tpl);
1361
1362 return EFI_SUCCESS;
1363 }
1364
1365 /**
1366 Set Control Bits
1367
1368 @param This - Pointer to EFI_SERIAL_IO_PROTOCOL
1369 @param Control - Control bits that can be settable
1370
1371 @retval EFI_SUCCESS - New Control bits were set successfully
1372 @retval EFI_UNSUPPORTED - The Control bits wanted to set are not supported
1373
1374 **/
1375 EFI_STATUS
1376 EFIAPI
1377 IsaSerialSetControl (
1378 IN EFI_SERIAL_IO_PROTOCOL *This,
1379 IN UINT32 Control
1380 )
1381 {
1382 SERIAL_DEV *SerialDevice;
1383 SERIAL_PORT_MCR Mcr;
1384 EFI_TPL Tpl;
1385
1386 //
1387 // The control bits that can be set are :
1388 // EFI_SERIAL_DATA_TERMINAL_READY: 0x0001 // WO
1389 // EFI_SERIAL_REQUEST_TO_SEND: 0x0002 // WO
1390 // EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE: 0x1000 // RW
1391 // EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE: 0x2000 // RW
1392 //
1393 SerialDevice = SERIAL_DEV_FROM_THIS (This);
1394
1395 //
1396 // first determine the parameter is invalid
1397 //
1398 if (Control & 0xffff8ffc) {
1399 return EFI_UNSUPPORTED;
1400 }
1401
1402 Tpl = gBS->RaiseTPL (TPL_NOTIFY);
1403
1404 Mcr.Data = READ_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
1405 Mcr.Bits.DTRC = 0;
1406 Mcr.Bits.RTS = 0;
1407 Mcr.Bits.LME = 0;
1408 SerialDevice->SoftwareLoopbackEnable = FALSE;
1409 SerialDevice->HardwareFlowControl = FALSE;
1410
1411 if (Control & EFI_SERIAL_DATA_TERMINAL_READY) {
1412 Mcr.Bits.DTRC = 1;
1413 }
1414
1415 if (Control & EFI_SERIAL_REQUEST_TO_SEND) {
1416 Mcr.Bits.RTS = 1;
1417 }
1418
1419 if (Control & EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE) {
1420 Mcr.Bits.LME = 1;
1421 }
1422
1423 if (Control & EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE) {
1424 SerialDevice->HardwareFlowControl = TRUE;
1425 }
1426
1427 WRITE_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Mcr.Data);
1428
1429 if (Control & EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE) {
1430 SerialDevice->SoftwareLoopbackEnable = TRUE;
1431 }
1432
1433 gBS->RestoreTPL (Tpl);
1434
1435 return EFI_SUCCESS;
1436 }
1437
1438 /**
1439 Get ControlBits
1440
1441 @param This - Pointer to EFI_SERIAL_IO_PROTOCOL
1442 @param Control - Control signals of the serial device
1443
1444 @retval EFI_SUCCESS - Get Control signals successfully
1445
1446 **/
1447 EFI_STATUS
1448 EFIAPI
1449 IsaSerialGetControl (
1450 IN EFI_SERIAL_IO_PROTOCOL *This,
1451 OUT UINT32 *Control
1452 )
1453 {
1454 SERIAL_DEV *SerialDevice;
1455 SERIAL_PORT_MSR Msr;
1456 SERIAL_PORT_MCR Mcr;
1457 EFI_TPL Tpl;
1458
1459 Tpl = gBS->RaiseTPL (TPL_NOTIFY);
1460
1461 SerialDevice = SERIAL_DEV_FROM_THIS (This);
1462
1463 *Control = 0;
1464
1465 //
1466 // Read the Modem Status Register
1467 //
1468 Msr.Data = READ_MSR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
1469
1470 if (Msr.Bits.CTS) {
1471 *Control |= EFI_SERIAL_CLEAR_TO_SEND;
1472 }
1473
1474 if (Msr.Bits.DSR) {
1475 *Control |= EFI_SERIAL_DATA_SET_READY;
1476 }
1477
1478 if (Msr.Bits.RI) {
1479 *Control |= EFI_SERIAL_RING_INDICATE;
1480 }
1481
1482 if (Msr.Bits.DCD) {
1483 *Control |= EFI_SERIAL_CARRIER_DETECT;
1484 }
1485 //
1486 // Read the Modem Control Register
1487 //
1488 Mcr.Data = READ_MCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
1489
1490 if (Mcr.Bits.DTRC) {
1491 *Control |= EFI_SERIAL_DATA_TERMINAL_READY;
1492 }
1493
1494 if (Mcr.Bits.RTS) {
1495 *Control |= EFI_SERIAL_REQUEST_TO_SEND;
1496 }
1497
1498 if (Mcr.Bits.LME) {
1499 *Control |= EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE;
1500 }
1501
1502 if (SerialDevice->HardwareFlowControl) {
1503 *Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;
1504 }
1505 //
1506 // See if the Transmit FIFO is empty
1507 //
1508 IsaSerialReceiveTransmit (SerialDevice);
1509
1510 if (IsaSerialFifoEmpty (&SerialDevice->Transmit)) {
1511 *Control |= EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
1512 }
1513 //
1514 // See if the Receive FIFO is empty.
1515 //
1516 IsaSerialReceiveTransmit (SerialDevice);
1517
1518 if (IsaSerialFifoEmpty (&SerialDevice->Receive)) {
1519 *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
1520 }
1521
1522 if (SerialDevice->SoftwareLoopbackEnable) {
1523 *Control |= EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE;
1524 }
1525
1526 gBS->RestoreTPL (Tpl);
1527
1528 return EFI_SUCCESS;
1529 }
1530
1531 /**
1532 Write the specified number of bytes to serial device
1533
1534 @param This - Pointer to EFI_SERIAL_IO_PROTOCOL
1535 @param BufferSize - On input the size of Buffer, on output the amount of
1536 data actually written
1537 @param Buffer - The buffer of data to write
1538
1539 @retval EFI_SUCCESS - The data were written successfully
1540 @retval EFI_DEVICE_ERROR - The device reported an error
1541 @retval EFI_TIMEOUT - The write operation was stopped due to timeout
1542
1543 **/
1544 EFI_STATUS
1545 EFIAPI
1546 IsaSerialWrite (
1547 IN EFI_SERIAL_IO_PROTOCOL *This,
1548 IN OUT UINTN *BufferSize,
1549 IN VOID *Buffer
1550 )
1551 {
1552 SERIAL_DEV *SerialDevice;
1553 UINT8 *CharBuffer;
1554 UINT32 Index;
1555 UINTN Elapsed;
1556 UINTN ActualWrite;
1557 EFI_TPL Tpl;
1558
1559 SerialDevice = SERIAL_DEV_FROM_THIS (This);
1560 Elapsed = 0;
1561 ActualWrite = 0;
1562
1563 if (*BufferSize == 0) {
1564 return EFI_SUCCESS;
1565 }
1566
1567 if (!Buffer) {
1568 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1569 EFI_ERROR_CODE,
1570 EFI_P_EC_OUTPUT_ERROR | EFI_PERIPHERAL_SERIAL_PORT,
1571 SerialDevice->DevicePath
1572 );
1573
1574 return EFI_DEVICE_ERROR;
1575 }
1576
1577 Tpl = gBS->RaiseTPL (TPL_NOTIFY);
1578
1579 CharBuffer = (UINT8 *) Buffer;
1580
1581 for (Index = 0; Index < *BufferSize; Index++) {
1582 IsaSerialFifoAdd (&SerialDevice->Transmit, CharBuffer[Index]);
1583
1584 while (IsaSerialReceiveTransmit (SerialDevice) != EFI_SUCCESS || !IsaSerialFifoEmpty (&SerialDevice->Transmit)) {
1585 //
1586 // Unsuccessful write so check if timeout has expired, if not,
1587 // stall for a bit, increment time elapsed, and try again
1588 //
1589 if (Elapsed >= This->Mode->Timeout) {
1590 *BufferSize = ActualWrite;
1591 gBS->RestoreTPL (Tpl);
1592 return EFI_TIMEOUT;
1593 }
1594
1595 gBS->Stall (TIMEOUT_STALL_INTERVAL);
1596
1597 Elapsed += TIMEOUT_STALL_INTERVAL;
1598 }
1599
1600 ActualWrite++;
1601 //
1602 // Successful write so reset timeout
1603 //
1604 Elapsed = 0;
1605 }
1606
1607 gBS->RestoreTPL (Tpl);
1608
1609 return EFI_SUCCESS;
1610 }
1611
1612 /**
1613 Read the specified number of bytes from serial device
1614
1615 @param This - Pointer to EFI_SERIAL_IO_PROTOCOL
1616 @param BufferSize - On input the size of Buffer, on output the amount of
1617 data returned in buffer
1618 @param Buffer - The buffer to return the data into
1619
1620 @retval EFI_SUCCESS - The data were read successfully
1621 @retval EFI_DEVICE_ERROR - The device reported an error
1622 @retval EFI_TIMEOUT - The read operation was stopped due to timeout
1623
1624 **/
1625 EFI_STATUS
1626 EFIAPI
1627 IsaSerialRead (
1628 IN EFI_SERIAL_IO_PROTOCOL *This,
1629 IN OUT UINTN *BufferSize,
1630 OUT VOID *Buffer
1631 )
1632 {
1633 SERIAL_DEV *SerialDevice;
1634 UINT32 Index;
1635 UINT8 *CharBuffer;
1636 UINTN Elapsed;
1637 EFI_STATUS Status;
1638 EFI_TPL Tpl;
1639
1640 SerialDevice = SERIAL_DEV_FROM_THIS (This);
1641 Elapsed = 0;
1642
1643 if (*BufferSize == 0) {
1644 return EFI_SUCCESS;
1645 }
1646
1647 if (!Buffer) {
1648 return EFI_DEVICE_ERROR;
1649 }
1650
1651 Tpl = gBS->RaiseTPL (TPL_NOTIFY);
1652
1653 Status = IsaSerialReceiveTransmit (SerialDevice);
1654
1655 if (EFI_ERROR (Status)) {
1656 *BufferSize = 0;
1657
1658 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1659 EFI_ERROR_CODE,
1660 EFI_P_EC_INPUT_ERROR | EFI_PERIPHERAL_SERIAL_PORT,
1661 SerialDevice->DevicePath
1662 );
1663
1664 gBS->RestoreTPL (Tpl);
1665
1666 return EFI_DEVICE_ERROR;
1667 }
1668
1669 CharBuffer = (UINT8 *) Buffer;
1670 for (Index = 0; Index < *BufferSize; Index++) {
1671 while (IsaSerialFifoRemove (&SerialDevice->Receive, &(CharBuffer[Index])) != EFI_SUCCESS) {
1672 //
1673 // Unsuccessful read so check if timeout has expired, if not,
1674 // stall for a bit, increment time elapsed, and try again
1675 // Need this time out to get conspliter to work.
1676 //
1677 if (Elapsed >= This->Mode->Timeout) {
1678 *BufferSize = Index;
1679 gBS->RestoreTPL (Tpl);
1680 return EFI_TIMEOUT;
1681 }
1682
1683 gBS->Stall (TIMEOUT_STALL_INTERVAL);
1684 Elapsed += TIMEOUT_STALL_INTERVAL;
1685
1686 Status = IsaSerialReceiveTransmit (SerialDevice);
1687 if (Status == EFI_DEVICE_ERROR) {
1688 *BufferSize = Index;
1689 gBS->RestoreTPL (Tpl);
1690 return EFI_DEVICE_ERROR;
1691 }
1692 }
1693 //
1694 // Successful read so reset timeout
1695 //
1696 Elapsed = 0;
1697 }
1698
1699 IsaSerialReceiveTransmit (SerialDevice);
1700
1701 gBS->RestoreTPL (Tpl);
1702
1703 return EFI_SUCCESS;
1704 }
1705
1706 /**
1707 Use scratchpad register to test if this serial port is present
1708
1709 @param SerialDevice - Pointer to serial device structure
1710
1711 @return if this serial port is present
1712 **/
1713 BOOLEAN
1714 IsaSerialPortPresent (
1715 IN SERIAL_DEV *SerialDevice
1716 )
1717
1718 {
1719 UINT8 Temp;
1720 BOOLEAN Status;
1721
1722 Status = TRUE;
1723
1724 //
1725 // Save SCR reg
1726 //
1727 Temp = READ_SCR (SerialDevice->IsaIo, SerialDevice->BaseAddress);
1728 WRITE_SCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, 0xAA);
1729
1730 if (READ_SCR (SerialDevice->IsaIo, SerialDevice->BaseAddress) != 0xAA) {
1731 if (!FeaturePcdGet (PcdNtEmulatorEnable)) {
1732 Status = FALSE;
1733 }
1734 }
1735
1736 WRITE_SCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, 0x55);
1737
1738 if (READ_SCR (SerialDevice->IsaIo, SerialDevice->BaseAddress) != 0x55) {
1739 if (!FeaturePcdGet (PcdNtEmulatorEnable)) {
1740 Status = FALSE;
1741 }
1742 }
1743 //
1744 // Restore SCR
1745 //
1746 WRITE_SCR (SerialDevice->IsaIo, SerialDevice->BaseAddress, Temp);
1747 return Status;
1748 }
1749
1750 /**
1751 Use IsaIo protocol to read serial port
1752
1753 @param IsaIo - Pointer to EFI_ISA_IO_PROTOCOL instance
1754 @param BaseAddress - Serial port register group base address
1755 @param Offset - Offset in register group
1756
1757 @return Data read from serial port
1758
1759 **/
1760 UINT8
1761 IsaSerialReadPort (
1762 IN EFI_ISA_IO_PROTOCOL *IsaIo,
1763 IN UINT16 BaseAddress,
1764 IN UINT32 Offset
1765 )
1766 {
1767 UINT8 Data;
1768
1769 //
1770 // Use IsaIo to access IO
1771 //
1772 IsaIo->Io.Read (
1773 IsaIo,
1774 EfiIsaIoWidthUint8,
1775 BaseAddress + Offset,
1776 1,
1777 &Data
1778 );
1779 return Data;
1780 }
1781
1782 /**
1783 Use IsaIo protocol to write serial port
1784
1785 @param IsaIo - Pointer to EFI_ISA_IO_PROTOCOL instance
1786 @param BaseAddress - Serial port register group base address
1787 @param Offset - Offset in register group
1788 @param Data - data which is to be written to some serial port register
1789
1790 **/
1791 VOID
1792 IsaSerialWritePort (
1793 IN EFI_ISA_IO_PROTOCOL *IsaIo,
1794 IN UINT16 BaseAddress,
1795 IN UINT32 Offset,
1796 IN UINT8 Data
1797 )
1798 {
1799 //
1800 // Use IsaIo to access IO
1801 //
1802 IsaIo->Io.Write (
1803 IsaIo,
1804 EfiIsaIoWidthUint8,
1805 BaseAddress + Offset,
1806 1,
1807 &Data
1808 );
1809 }
1810