3 Copyright (c) 2006 - 2007, Intel Corporation<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 The implementation for EFI_ISA_IO_PROTOCOL.
23 // Include common header file for this module.
25 #include "InternalIsaIo.h"
28 // Driver Support Global Variables
30 EFI_ISA_IO_PROTOCOL IsaIoInterface
= {
50 static EFI_ISA_DMA_REGISTERS DmaRegisters
[8] = {
75 }, // Channel 4 is invalid
94 ReportErrorStatusCode (
95 EFI_STATUS_CODE_VALUE Code
101 report a error Status code of PCI bus driver controller
105 Code - The error status code.
109 EFI_SUCCESS - Success to report status code.
114 return REPORT_STATUS_CODE (
115 EFI_ERROR_CODE
| EFI_ERROR_MINOR
,
121 // Driver Support Functions
125 InitializeIsaIoInstance (
126 IN ISA_IO_DEVICE
*IsaIoDevice
,
127 IN EFI_ISA_ACPI_RESOURCE_LIST
*IsaDeviceResourceList
133 Initializes an ISA I/O Instance
137 IsaIoDevice - The iso device to be initialized.
138 IsaDeviceResourceList - The resource list.
142 EFI_SUCCESS - Initial success.
147 // Initializes an ISA I/O Instance
152 sizeof (EFI_ISA_IO_PROTOCOL
)
155 IsaIoDevice
->IsaIo
.ResourceList
= IsaDeviceResourceList
;
163 IN EFI_ISA_IO_PROTOCOL
*This
,
164 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
173 Performs an ISA I/O Read Cycle
177 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
178 Width - Signifies the width of the I/O operation.
179 Offset - The offset in ISA I/O space to start the I/O operation.
180 Count - The number of I/O operations to perform.
181 Buffer - The destination buffer to store the results
185 EFI_SUCCESS - The data was read from the device sucessfully.
186 EFI_UNSUPPORTED - The Offset is not valid for this device.
187 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
188 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
193 ISA_IO_DEVICE
*IsaIoDevice
;
195 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
198 // Verify Isa IO Access
200 Status
= IsaIoVerifyAccess (
207 if (EFI_ERROR (Status
)) {
211 // Call PciIo->Io.Read
213 Status
= IsaIoDevice
->PciIo
->Io
.Read (
215 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
216 EFI_PCI_IO_PASS_THROUGH_BAR
,
222 if (EFI_ERROR (Status
)) {
223 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
232 IN EFI_ISA_IO_PROTOCOL
*This
,
233 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
242 Performs an ISA I/O Write Cycle
246 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
247 Width - Signifies the width of the I/O operation.
248 Offset - The offset in ISA I/O space to start the I/O operation.
249 Count - The number of I/O operations to perform.
250 Buffer - The source buffer to write data from
254 EFI_SUCCESS - The data was writen to the device sucessfully.
255 EFI_UNSUPPORTED - The Offset is not valid for this device.
256 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
257 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
262 ISA_IO_DEVICE
*IsaIoDevice
;
264 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
267 // Verify Isa IO Access
269 Status
= IsaIoVerifyAccess (
276 if (EFI_ERROR (Status
)) {
280 // Call PciIo->Io.Write
282 Status
= IsaIoDevice
->PciIo
->Io
.Write (
284 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
285 EFI_PCI_IO_PASS_THROUGH_BAR
,
291 if (EFI_ERROR (Status
)) {
292 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
300 IN EFI_ISA_IO_PROTOCOL
*This
,
308 Writes an 8 bit I/O Port
312 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
313 Offset - The offset in ISA IO space to start the IO operation.
314 Value - The data to write port.
318 EFI_SUCCESS - Success.
319 EFI_INVALID_PARAMETER - Parameter is invalid.
320 EFI_UNSUPPORTED - The address range specified by Offset is not valid.
321 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
326 ISA_IO_DEVICE
*IsaIoDevice
;
328 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
331 // Call PciIo->Io.Write
333 Status
= IsaIoDevice
->PciIo
->Io
.Write (
336 EFI_PCI_IO_PASS_THROUGH_BAR
,
341 if (EFI_ERROR (Status
)) {
342 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
353 IN EFI_ISA_IO_PROTOCOL
*This
,
354 IN UINT32 AddrOffset
,
355 IN UINT32 PageOffset
,
356 IN UINT32 CountOffset
,
357 IN UINT32 BaseAddress
,
364 Writes I/O operation base address and count number to a 8 bit I/O Port.
368 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
369 AddrOffset - The address' offset.
370 PageOffset - The page's offest.
371 CountOffset - The count's offset.
372 BaseAddress - The base address.
373 Count - The number of I/O operations to perform.
377 EFI_SUCCESS - Success.
378 EFI_INVALID_PARAMETER - Parameter is invalid.
379 EFI_UNSUPPORTED - The address range specified by these Offsets and Count is not valid.
380 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
386 Status
= WritePort (This
, AddrOffset
, (UINT8
) (BaseAddress
& 0xff));
387 if (EFI_ERROR (Status
)) {
391 Status
= WritePort (This
, AddrOffset
, (UINT8
) ((BaseAddress
>> 8) & 0xff));
392 if (EFI_ERROR (Status
)) {
396 Status
= WritePort (This
, PageOffset
, (UINT8
) ((BaseAddress
>> 16) & 0xff));
397 if (EFI_ERROR (Status
)) {
401 Status
= WritePort (This
, CountOffset
, (UINT8
) (Count
& 0xff));
402 if (EFI_ERROR (Status
)) {
406 Status
= WritePort (This
, CountOffset
, (UINT8
) ((Count
>> 8) & 0xff));
407 if (EFI_ERROR (Status
)) {
417 IN EFI_ISA_IO_PROTOCOL
*This
,
424 Unmaps a memory region for DMA
428 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
429 Mapping - The mapping value returned from EFI_ISA_IO.Map().
433 EFI_SUCCESS - The range was unmapped.
434 EFI_DEVICE_ERROR - The data was not committed to the target system memory.
438 ISA_MAP_INFO
*IsaMapInfo
;
441 // Unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
443 if (!FeaturePcdGet (PcdIsaBusSupportDma
)) {
444 return EFI_UNSUPPORTED
;
448 // See if the Map() operation associated with this Unmap() required a mapping
449 // buffer.If a mapping buffer was not required, then this function simply
450 // returns EFI_SUCCESS.
452 if (Mapping
!= NULL
) {
454 // Get the MAP_INFO structure from Mapping
456 IsaMapInfo
= (ISA_MAP_INFO
*) Mapping
;
459 // If this is a write operation from the Agent's point of view,
460 // then copy the contents of the mapped buffer into the real buffer
461 // so the processor can read the contents of the real buffer.
463 if (IsaMapInfo
->Operation
== EfiIsaIoOperationBusMasterWrite
) {
465 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
466 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
467 IsaMapInfo
->NumberOfBytes
471 // Free the mapped buffer and the MAP_INFO structure.
473 gBS
->FreePages (IsaMapInfo
->MappedHostAddress
, IsaMapInfo
->NumberOfPages
);
474 gBS
->FreePool (IsaMapInfo
);
483 IN EFI_ISA_IO_PROTOCOL
*This
493 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
497 EFI_SUCCESS - The buffers were flushed.
498 EFI_DEVICE_ERROR - The buffers were not flushed due to a hardware error.
503 ISA_IO_DEVICE
*IsaIoDevice
;
505 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
510 Status
= IsaIoDevice
->PciIo
->Flush (IsaIoDevice
->PciIo
);
512 if (EFI_ERROR (Status
)) {
513 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
521 IN ISA_IO_DEVICE
*IsaIoDevice
,
522 IN ISA_ACCESS_TYPE Type
,
523 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
525 IN OUT UINT32
*Offset
531 Verifies access to an ISA device
535 IsaIoDevice - The ISA device to be verified.
536 Type - The Access type. The input must be either IsaAccessTypeMem or IsaAccessTypeIo.
537 Width - Signifies the width of the memory operation.
538 Count - The number of memory operations to perform.
539 Offset - The offset in ISA memory space to start the memory operation.
543 EFI_SUCCESS - Verify success.
544 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
545 EFI_UNSUPPORTED - The device ont support the access type.
549 EFI_ISA_ACPI_RESOURCE
*Item
;
552 if (Width
< EfiIsaIoWidthUint8
||
553 Width
>= EfiIsaIoWidthMaximum
||
554 Width
== EfiIsaIoWidthReserved
||
555 Width
== EfiIsaIoWidthFifoReserved
||
556 Width
== EfiIsaIoWidthFillReserved
558 return EFI_INVALID_PARAMETER
;
562 // If Width is EfiIsaIoWidthFifoUintX then convert to EfiIsaIoWidthUintX
563 // If Width is EfiIsaIoWidthFillUintX then convert to EfiIsaIoWidthUintX
565 if (Width
>= EfiIsaIoWidthFifoUint8
&& Width
<= EfiIsaIoWidthFifoReserved
) {
569 Width
= (EFI_ISA_IO_PROTOCOL_WIDTH
) (Width
& 0x03);
571 Status
= EFI_UNSUPPORTED
;
572 Item
= IsaIoDevice
->IsaIo
.ResourceList
->ResourceItem
;
573 while (Item
->Type
!= EfiIsaAcpiResourceEndOfList
) {
574 if ((Type
== IsaAccessTypeMem
&& Item
->Type
== EfiIsaAcpiResourceMemory
) ||
575 (Type
== IsaAccessTypeIo
&& Item
->Type
== EfiIsaAcpiResourceIo
)
577 if (*Offset
>= Item
->StartRange
&& (*Offset
+ Count
* (UINT32
)(1 << Width
)) - 1 <= Item
->EndRange
) {
581 if (*Offset
>= Item
->StartRange
&& *Offset
<= Item
->EndRange
) {
582 Status
= EFI_INVALID_PARAMETER
;
595 IN EFI_ISA_IO_PROTOCOL
*This
,
596 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
605 Performs an ISA Memory Read Cycle
609 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
610 Width - Signifies the width of the memory operation.
611 Offset - The offset in ISA memory space to start the memory operation.
612 Count - The number of memory operations to perform.
613 Buffer - The destination buffer to store the results
617 EFI_SUCCESS - The data was read from the device successfully.
618 EFI_UNSUPPORTED - The Offset is not valid for this device.
619 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
620 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
625 ISA_IO_DEVICE
*IsaIoDevice
;
628 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
631 // So we just return EFI_UNSUPPORTED for these functions.
633 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
634 return EFI_UNSUPPORTED
;
637 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
640 // Verify the Isa Io Access
642 Status
= IsaIoVerifyAccess (
649 if (EFI_ERROR (Status
)) {
653 // Call PciIo->Mem.Read
655 Status
= IsaIoDevice
->PciIo
->Mem
.Read (
657 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
658 EFI_PCI_IO_PASS_THROUGH_BAR
,
664 if (EFI_ERROR (Status
)) {
665 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
674 IN EFI_ISA_IO_PROTOCOL
*This
,
675 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
684 Performs an ISA Memory Write Cycle
688 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
689 Width - Signifies the width of the memory operation.
690 Offset - The offset in ISA memory space to start the memory operation.
691 Count - The number of memory operations to perform.
692 Buffer - The source buffer to write data from
696 EFI_SUCCESS - The data was written to the device sucessfully.
697 EFI_UNSUPPORTED - The Offset is not valid for this device.
698 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
699 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
704 ISA_IO_DEVICE
*IsaIoDevice
;
707 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
710 // So we just return EFI_UNSUPPORTED for these functions.
712 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
713 return EFI_UNSUPPORTED
;
716 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
719 // Verify Isa IO Access
721 Status
= IsaIoVerifyAccess (
728 if (EFI_ERROR (Status
)) {
732 // Call PciIo->Mem.Write
734 Status
= IsaIoDevice
->PciIo
->Mem
.Write (
736 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
737 EFI_PCI_IO_PASS_THROUGH_BAR
,
743 if (EFI_ERROR (Status
)) {
744 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
753 IN EFI_ISA_IO_PROTOCOL
*This
,
754 IN EFI_ISA_IO_PROTOCOL_WIDTH Width
,
755 IN UINT32 DestOffset
,
763 Performs an ISA I/O Copy Memory
767 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
768 Width - Signifies the width of the memory copy operation.
769 DestOffset - The offset of the destination
770 SrcOffset - The offset of the source
771 Count - The number of memory copy operations to perform
775 EFI_SUCCESS - The data was copied sucessfully.
776 EFI_UNSUPPORTED - The DestOffset or SrcOffset is not valid for this device.
777 EFI_INVALID_PARAMETER - Width or Count, or both, were invalid.
778 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of resources.
783 ISA_IO_DEVICE
*IsaIoDevice
;
786 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
789 // So we just return EFI_UNSUPPORTED for these functions.
791 if (!FeaturePcdGet (PcdIsaBusSupportIsaMemory
)) {
792 return EFI_UNSUPPORTED
;
795 IsaIoDevice
= ISA_IO_DEVICE_FROM_ISA_IO_THIS (This
);
798 // Verify Isa IO Access for destination and source
800 Status
= IsaIoVerifyAccess (
807 if (EFI_ERROR (Status
)) {
811 Status
= IsaIoVerifyAccess (
818 if (EFI_ERROR (Status
)) {
822 // Call PciIo->CopyMem
824 Status
= IsaIoDevice
->PciIo
->CopyMem (
826 (EFI_PCI_IO_PROTOCOL_WIDTH
) Width
,
827 EFI_PCI_IO_PASS_THROUGH_BAR
,
829 EFI_PCI_IO_PASS_THROUGH_BAR
,
834 if (EFI_ERROR (Status
)) {
835 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
843 IsaIoMap_OnlySupportSlaveReadWrite (
844 IN EFI_ISA_IO_PROTOCOL
*This
,
845 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
846 IN UINT8 ChannelNumber OPTIONAL
,
847 IN UINT32 ChannelAttributes
,
848 IN VOID
*HostAddress
,
849 IN OUT UINTN
*NumberOfBytes
,
850 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
857 Maps a memory region for DMA, note this implementation
858 only supports slave read/write operation to save code size.
862 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
863 Operation - Indicates the type of DMA (slave or bus master), and if
864 the DMA operation is going to read or write to system memory.
865 ChannelNumber - The slave channel number to use for this DMA operation.
866 If Operation and ChannelAttributes shows that this device
867 performs bus mastering DMA, then this field is ignored.
868 The legal range for this field is 0..7.
869 ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
870 HostAddress - The system memory address to map to the device.
871 NumberOfBytes - On input the number of bytes to map. On output the number
872 of bytes that were mapped.
873 DeviceAddress - The resulting map address for the bus master device to use
874 to access the hosts HostAddress.
875 Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
879 EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
880 EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
881 EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
882 EFI_DEVICE_ERROR - The system hardware could not map the requested address.
883 EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
888 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
889 ISA_MAP_INFO
*IsaMapInfo
;
891 UINTN MaxNumberOfBytes
;
897 UINT8 DmaChannelMode
;
899 if ((NULL
== This
) ||
900 (NULL
== HostAddress
) ||
901 (NULL
== NumberOfBytes
) ||
902 (NULL
== DeviceAddress
) ||
905 return EFI_INVALID_PARAMETER
;
910 // Initialize the return values to their defaults
915 // Make sure the Operation parameter is valid.
916 // Light IsaIo only supports two operations.
918 if (!(Operation
== EfiIsaIoOperationSlaveRead
||
919 Operation
== EfiIsaIoOperationSlaveWrite
)) {
920 return EFI_INVALID_PARAMETER
;
923 if (ChannelNumber
>= 4) {
925 // The Light IsaIo doesn't support channelNumber larger than 4.
927 return EFI_INVALID_PARAMETER
;
931 // Map the HostAddress to a DeviceAddress.
933 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
934 if ((PhysicalAddress
+*NumberOfBytes
) > ISA_MAX_MEMORY_ADDRESS
) {
936 // Common Buffer operations can not be remapped. If the common buffer
937 // is above 16MB, then it is not possible to generate a mapping, so return
940 if (Operation
== EfiIsaIoOperationBusMasterCommonBuffer
) {
941 return EFI_UNSUPPORTED
;
944 // Allocate an ISA_MAP_INFO structure to remember the mapping when Unmap()
947 IsaMapInfo
= AllocatePool (sizeof (ISA_MAP_INFO
));
948 if (IsaMapInfo
== NULL
) {
950 return EFI_OUT_OF_RESOURCES
;
953 // Return a pointer to the MAP_INFO structure in Mapping
955 *Mapping
= IsaMapInfo
;
958 // Initialize the MAP_INFO structure
960 IsaMapInfo
->Operation
= Operation
;
961 IsaMapInfo
->NumberOfBytes
= *NumberOfBytes
;
962 IsaMapInfo
->NumberOfPages
= EFI_SIZE_TO_PAGES (*NumberOfBytes
);
963 IsaMapInfo
->HostAddress
= PhysicalAddress
;
964 IsaMapInfo
->MappedHostAddress
= ISA_MAX_MEMORY_ADDRESS
- 1;
967 // Allocate a buffer below 16MB to map the transfer to.
969 Status
= gBS
->AllocatePages (
972 IsaMapInfo
->NumberOfPages
,
973 &IsaMapInfo
->MappedHostAddress
975 if (EFI_ERROR (Status
)) {
976 gBS
->FreePool (IsaMapInfo
);
982 // If this is a read operation from the DMA agents's point of view,
983 // then copy the contents of the real buffer into the mapped buffer
984 // so the DMA agent can read the contents of the real buffer.
986 if (Operation
== EfiIsaIoOperationSlaveRead
) {
988 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
989 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
990 IsaMapInfo
->NumberOfBytes
994 // The DeviceAddress is the address of the maped buffer below 16 MB
996 *DeviceAddress
= IsaMapInfo
->MappedHostAddress
;
999 // The transfer is below 16 MB, so the DeviceAddress is simply the
1002 *DeviceAddress
= PhysicalAddress
;
1006 // Figure out what to program into the DMA Channel Mode Register
1008 DmaMode
= (UINT8
) (B_8237_DMA_CHMODE_INCREMENT
| (ChannelNumber
& 0x03));
1009 if (Operation
== EfiIsaIoOperationSlaveRead
) {
1010 DmaMode
|= V_8237_DMA_CHMODE_MEM2IO
;
1012 DmaMode
|= V_8237_DMA_CHMODE_IO2MEM
;
1015 // We only support EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE in simplified IsaIo
1017 DmaMode
|= V_8237_DMA_CHMODE_SINGLE
;
1020 // A Slave DMA transfer can not cross a 64K boundary.
1021 // Compute *NumberOfBytes based on this restriction.
1023 MaxNumberOfBytes
= 0x10000 - ((UINT32
) (*DeviceAddress
) & 0xffff);
1024 if (*NumberOfBytes
> MaxNumberOfBytes
) {
1025 *NumberOfBytes
= MaxNumberOfBytes
;
1028 // Compute the values to program into the BaseAddress and Count registers
1029 // of the Slave DMA controller
1031 BaseAddress
= (UINT32
) (*DeviceAddress
);
1032 Count
= (UINT16
) (*NumberOfBytes
- 1);
1034 // Program the DMA Write Single Mask Register for ChannelNumber
1035 // Clear the DMA Byte Pointer Register
1037 DmaMask
= R_8237_DMA_WRSMSK_CH0_3
;
1038 DmaClear
= R_8237_DMA_CBPR_CH0_3
;
1039 DmaChannelMode
= R_8237_DMA_CHMODE_CH0_3
;
1041 Status
= WritePort (
1044 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1046 if (EFI_ERROR (Status
)) {
1050 Status
= WritePort (
1053 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1055 if (EFI_ERROR (Status
)) {
1059 Status
= WritePort (This
, DmaChannelMode
, DmaMode
);
1060 if (EFI_ERROR (Status
)) {
1064 Status
= WriteDmaPort (
1066 DmaRegisters
[ChannelNumber
].Address
,
1067 DmaRegisters
[ChannelNumber
].Page
,
1068 DmaRegisters
[ChannelNumber
].Count
,
1072 if (EFI_ERROR (Status
)) {
1076 Status
= WritePort (
1079 (UINT8
) (ChannelNumber
& 0x03)
1081 if (EFI_ERROR (Status
)) {
1090 IsaIoMap_FullSupport (
1091 IN EFI_ISA_IO_PROTOCOL
*This
,
1092 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
1093 IN UINT8 ChannelNumber OPTIONAL
,
1094 IN UINT32 ChannelAttributes
,
1095 IN VOID
*HostAddress
,
1096 IN OUT UINTN
*NumberOfBytes
,
1097 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
1102 Routine Description:
1104 Maps a memory region for DMA. This implementation implement the
1105 the full mapping support.
1109 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1110 Operation - Indicates the type of DMA (slave or bus master), and if
1111 the DMA operation is going to read or write to system memory.
1112 ChannelNumber - The slave channel number to use for this DMA operation.
1113 If Operation and ChannelAttributes shows that this device
1114 performs bus mastering DMA, then this field is ignored.
1115 The legal range for this field is 0..7.
1116 ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
1117 HostAddress - The system memory address to map to the device.
1118 NumberOfBytes - On input the number of bytes to map. On output the number
1119 of bytes that were mapped.
1120 DeviceAddress - The resulting map address for the bus master device to use
1121 - to access the hosts HostAddress.
1122 Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
1126 EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
1127 EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
1128 EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
1129 EFI_DEVICE_ERROR - The system hardware could not map the requested address.
1130 EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1137 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1138 ISA_MAP_INFO
*IsaMapInfo
;
1140 UINTN MaxNumberOfBytes
;
1146 UINT8 DmaChannelMode
;
1148 if ((NULL
== This
) ||
1149 (NULL
== HostAddress
) ||
1150 (NULL
== NumberOfBytes
) ||
1151 (NULL
== DeviceAddress
) ||
1154 return EFI_INVALID_PARAMETER
;
1159 // Initialize the return values to their defaults
1164 // Make sure the Operation parameter is valid
1166 if (Operation
< 0 || Operation
>= EfiIsaIoOperationMaximum
) {
1167 return EFI_INVALID_PARAMETER
;
1170 // See if this is a Slave DMA Operation
1174 if (Operation
== EfiIsaIoOperationSlaveRead
) {
1175 Operation
= EfiIsaIoOperationBusMasterRead
;
1180 if (Operation
== EfiIsaIoOperationSlaveWrite
) {
1181 Operation
= EfiIsaIoOperationBusMasterWrite
;
1188 // Make sure that ChannelNumber is a valid channel number
1189 // Channel 4 is used to cascade, so it is illegal.
1191 if (ChannelNumber
== 4 || ChannelNumber
> 7) {
1192 return EFI_INVALID_PARAMETER
;
1195 // This implementation only support COMPATIBLE DMA Transfers
1197 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_COMPATIBLE
)) {
1198 return EFI_INVALID_PARAMETER
;
1201 if (ChannelAttributes
&
1203 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_A
|
1204 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_B
|
1205 EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SPEED_C
1208 return EFI_INVALID_PARAMETER
;
1211 if (ChannelNumber
< 4) {
1213 // If this is Channel 0..3, then the width must be 8 bit
1215 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_8
) ||
1216 (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_16
)
1218 return EFI_INVALID_PARAMETER
;
1222 // If this is Channel 4..7, then the width must be 16 bit
1224 if ((ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_8
) ||
1225 (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_WIDTH_16
))
1227 return EFI_INVALID_PARAMETER
;
1231 // Either Demand Mode or Single Mode must be selected, but not both
1233 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE
) {
1234 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
) {
1235 return EFI_INVALID_PARAMETER
;
1238 if (!(ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
)) {
1239 return EFI_INVALID_PARAMETER
;
1244 // Map the HostAddress to a DeviceAddress.
1246 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
1247 if ((PhysicalAddress
+*NumberOfBytes
) > ISA_MAX_MEMORY_ADDRESS
) {
1249 // Common Buffer operations can not be remapped. If the common buffer
1250 // is above 16MB, then it is not possible to generate a mapping, so return
1253 if (Operation
== EfiIsaIoOperationBusMasterCommonBuffer
) {
1254 return EFI_UNSUPPORTED
;
1257 // Allocate an ISA_MAP_INFO structure to remember the mapping when Unmap()
1260 IsaMapInfo
= AllocatePool (sizeof (ISA_MAP_INFO
));
1261 if (IsaMapInfo
== NULL
) {
1263 return EFI_OUT_OF_RESOURCES
;
1266 // Return a pointer to the MAP_INFO structure in Mapping
1268 *Mapping
= IsaMapInfo
;
1271 // Initialize the MAP_INFO structure
1273 IsaMapInfo
->Operation
= Operation
;
1274 IsaMapInfo
->NumberOfBytes
= *NumberOfBytes
;
1275 IsaMapInfo
->NumberOfPages
= EFI_SIZE_TO_PAGES (*NumberOfBytes
);
1276 IsaMapInfo
->HostAddress
= PhysicalAddress
;
1277 IsaMapInfo
->MappedHostAddress
= ISA_MAX_MEMORY_ADDRESS
- 1;
1280 // Allocate a buffer below 16MB to map the transfer to.
1282 Status
= gBS
->AllocatePages (
1284 EfiBootServicesData
,
1285 IsaMapInfo
->NumberOfPages
,
1286 &IsaMapInfo
->MappedHostAddress
1288 if (EFI_ERROR (Status
)) {
1289 gBS
->FreePool (IsaMapInfo
);
1295 // If this is a read operation from the DMA agents's point of view,
1296 // then copy the contents of the real buffer into the mapped buffer
1297 // so the DMA agent can read the contents of the real buffer.
1299 if (Operation
== EfiIsaIoOperationBusMasterRead
) {
1301 (VOID
*) (UINTN
) IsaMapInfo
->MappedHostAddress
,
1302 (VOID
*) (UINTN
) IsaMapInfo
->HostAddress
,
1303 IsaMapInfo
->NumberOfBytes
1307 // The DeviceAddress is the address of the maped buffer below 16 MB
1309 *DeviceAddress
= IsaMapInfo
->MappedHostAddress
;
1312 // The transfer is below 16 MB, so the DeviceAddress is simply the
1315 *DeviceAddress
= PhysicalAddress
;
1318 // If this is a Bus Master operation then return
1324 // Figure out what to program into the DMA Channel Mode Register
1326 DmaMode
= (UINT8
) (B_8237_DMA_CHMODE_INCREMENT
| (ChannelNumber
& 0x03));
1328 DmaMode
|= V_8237_DMA_CHMODE_MEM2IO
;
1330 DmaMode
|= V_8237_DMA_CHMODE_IO2MEM
;
1333 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_AUTO_INITIALIZE
) {
1334 DmaMode
|= B_8237_DMA_CHMODE_AE
;
1337 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_DEMAND_MODE
) {
1338 DmaMode
|= V_8237_DMA_CHMODE_DEMAND
;
1341 if (ChannelAttributes
& EFI_ISA_IO_SLAVE_DMA_ATTRIBUTE_SINGLE_MODE
) {
1342 DmaMode
|= V_8237_DMA_CHMODE_SINGLE
;
1345 // A Slave DMA transfer can not cross a 64K boundary.
1346 // Compute *NumberOfBytes based on this restriction.
1348 MaxNumberOfBytes
= 0x10000 - ((UINT32
) (*DeviceAddress
) & 0xffff);
1349 if (*NumberOfBytes
> MaxNumberOfBytes
) {
1350 *NumberOfBytes
= MaxNumberOfBytes
;
1353 // Compute the values to program into the BaseAddress and Count registers
1354 // of the Slave DMA controller
1356 if (ChannelNumber
< 4) {
1357 BaseAddress
= (UINT32
) (*DeviceAddress
);
1358 Count
= (UINT16
) (*NumberOfBytes
- 1);
1360 BaseAddress
= (UINT32
) (((UINT32
) (*DeviceAddress
) & 0xff0000) | (((UINT32
) (*DeviceAddress
) & 0xffff) >> 1));
1361 Count
= (UINT16
) ((*NumberOfBytes
- 1) >> 1);
1364 // Program the DMA Write Single Mask Register for ChannelNumber
1365 // Clear the DMA Byte Pointer Register
1367 if (ChannelNumber
< 4) {
1368 DmaMask
= R_8237_DMA_WRSMSK_CH0_3
;
1369 DmaClear
= R_8237_DMA_CBPR_CH0_3
;
1370 DmaChannelMode
= R_8237_DMA_CHMODE_CH0_3
;
1372 DmaMask
= R_8237_DMA_WRSMSK_CH4_7
;
1373 DmaClear
= R_8237_DMA_CBPR_CH4_7
;
1374 DmaChannelMode
= R_8237_DMA_CHMODE_CH4_7
;
1377 Status
= WritePort (
1380 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1382 if (EFI_ERROR (Status
)) {
1386 Status
= WritePort (
1389 (UINT8
) (B_8237_DMA_WRSMSK_CMS
| (ChannelNumber
& 0x03))
1391 if (EFI_ERROR (Status
)) {
1395 Status
= WritePort (This
, DmaChannelMode
, DmaMode
);
1396 if (EFI_ERROR (Status
)) {
1400 Status
= WriteDmaPort (
1402 DmaRegisters
[ChannelNumber
].Address
,
1403 DmaRegisters
[ChannelNumber
].Page
,
1404 DmaRegisters
[ChannelNumber
].Count
,
1408 if (EFI_ERROR (Status
)) {
1412 Status
= WritePort (
1415 (UINT8
) (ChannelNumber
& 0x03)
1417 if (EFI_ERROR (Status
)) {
1427 IN EFI_ISA_IO_PROTOCOL
*This
,
1428 IN EFI_ISA_IO_PROTOCOL_OPERATION Operation
,
1429 IN UINT8 ChannelNumber OPTIONAL
,
1430 IN UINT32 ChannelAttributes
,
1431 IN VOID
*HostAddress
,
1432 IN OUT UINTN
*NumberOfBytes
,
1433 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
1438 Routine Description:
1440 Maps a memory region for DMA
1444 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1445 Operation - Indicates the type of DMA (slave or bus master), and if
1446 the DMA operation is going to read or write to system memory.
1447 ChannelNumber - The slave channel number to use for this DMA operation.
1448 If Operation and ChannelAttributes shows that this device
1449 performs bus mastering DMA, then this field is ignored.
1450 The legal range for this field is 0..7.
1451 ChannelAttributes - The attributes of the DMA channel to use for this DMA operation
1452 HostAddress - The system memory address to map to the device.
1453 NumberOfBytes - On input the number of bytes to map. On output the number
1454 of bytes that were mapped.
1455 DeviceAddress - The resulting map address for the bus master device to use
1456 - to access the hosts HostAddress.
1457 Mapping - A resulting value to pass to EFI_ISA_IO.Unmap().
1461 EFI_SUCCESS - The range was mapped for the returned NumberOfBytes.
1462 EFI_INVALID_PARAMETER - The Operation or HostAddress is undefined.
1463 EFI_UNSUPPORTED - The HostAddress can not be mapped as a common buffer.
1464 EFI_DEVICE_ERROR - The system hardware could not map the requested address.
1465 EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1470 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1472 if (!FeaturePcdGet (PcdIsaBusSupportDma
)) {
1473 return EFI_UNSUPPORTED
;
1476 // Set Feature Flag PcdIsaBusSupportBusMaster to FALSE to disable support for
1479 // So we just return EFI_UNSUPPORTED for these functions.
1481 if (FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1482 return IsaIoMap_OnlySupportSlaveReadWrite (
1494 return IsaIoMap_FullSupport (
1508 IsaIoAllocateBuffer (
1509 IN EFI_ISA_IO_PROTOCOL
*This
,
1510 IN EFI_ALLOCATE_TYPE Type
,
1511 IN EFI_MEMORY_TYPE MemoryType
,
1513 OUT VOID
**HostAddress
,
1514 IN UINT64 Attributes
1518 Routine Description:
1520 Allocates a common buffer for DMA
1524 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1525 Type - The type allocation to perform.
1526 MemoryType - The type of memory to allocate.
1527 Pages - The number of pages to allocate.
1528 HostAddress - A pointer to store the base address of the allocated range.
1529 Attributes - The requested bit mask of attributes for the allocated range.
1533 EFI_SUCCESS - The requested memory pages were allocated.
1534 EFI_INVALID_PARAMETER - Type is invalid or MemoryType is invalid or HostAddress is NULL
1535 EFI_UNSUPPORTED - Attributes is unsupported or the memory range specified
1536 by HostAddress, Pages, and Type is not available for common buffer use.
1537 EFI_OUT_OF_RESOURCES - The memory pages could not be allocated.
1542 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1545 // Set Feature Flag PcdIsaBusOnlySupportSlaveDma to FALSE to disable support for
1547 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1549 if (!FeaturePcdGet (PcdIsaBusSupportDma
) || FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1550 return EFI_UNSUPPORTED
;
1553 if (HostAddress
== NULL
) {
1554 return EFI_INVALID_PARAMETER
;
1557 if (Type
< AllocateAnyPages
|| Type
>= MaxAllocateType
) {
1558 return EFI_INVALID_PARAMETER
;
1561 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
1563 if (MemoryType
!= EfiBootServicesData
&& MemoryType
!= EfiRuntimeServicesData
) {
1564 return EFI_INVALID_PARAMETER
;
1567 if (Attributes
&~(EFI_ISA_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE
| EFI_ISA_IO_ATTRIBUTE_MEMORY_CACHED
)) {
1568 return EFI_UNSUPPORTED
;
1571 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (ISA_MAX_MEMORY_ADDRESS
- 1);
1572 if (Type
== AllocateAddress
) {
1573 if ((UINTN
) (*HostAddress
) >= ISA_MAX_MEMORY_ADDRESS
) {
1574 return EFI_UNSUPPORTED
;
1576 PhysicalAddress
= (UINTN
) (*HostAddress
);
1580 if (Type
== AllocateAnyPages
) {
1581 Type
= AllocateMaxAddress
;
1584 Status
= gBS
->AllocatePages (Type
, MemoryType
, Pages
, &PhysicalAddress
);
1585 if (EFI_ERROR (Status
)) {
1586 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);
1590 *HostAddress
= (VOID
*) (UINTN
) PhysicalAddress
;
1597 IN EFI_ISA_IO_PROTOCOL
*This
,
1599 IN VOID
*HostAddress
1603 Routine Description:
1605 Frees a common buffer
1609 This - A pointer to the EFI_ISA_IO_PROTOCOL instance.
1610 Pages - The number of pages to free.
1611 HostAddress - The base address of the allocated range.
1615 EFI_SUCCESS - The requested memory pages were freed.
1616 EFI_INVALID_PARAMETER - The memory was not allocated with EFI_ISA_IO.AllocateBufer().
1621 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
1624 // Set Feature Flag PcdIsaBusOnlySupportSlaveDma to FALSE to disable support for
1626 // Or unset Feature Flag PcdIsaBusSupportDma to disable support for ISA DMA.
1628 if (!FeaturePcdGet (PcdIsaBusSupportDma
) || FeaturePcdGet (PcdIsaBusOnlySupportSlaveDma
)) {
1629 return EFI_UNSUPPORTED
;
1632 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
1633 Status
= gBS
->FreePages (
1637 if (EFI_ERROR (Status
)) {
1638 ReportErrorStatusCode (EFI_IO_BUS_LPC
| EFI_IOB_EC_CONTROLLER_ERROR
);