2 OMAP35xx DMA abstractions modeled on PCI IO protocol. EnableDma()/DisableDma()
5 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/DebugLib.h>
19 #include <Library/OmapDmaLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UncachedMemoryAllocationLib.h>
23 #include <Library/IoLib.h>
24 #include <Omap3530/Omap3530.h>
26 #include <Protocol/Cpu.h>
29 EFI_PHYSICAL_ADDRESS HostAddress
;
30 EFI_PHYSICAL_ADDRESS DeviceAddress
;
32 DMA_MAP_OPERATION Operation
;
37 EFI_CPU_ARCH_PROTOCOL
*gCpu
;
40 Configure OMAP DMA Channel
42 @param Channel DMA Channel to configure
43 @param Dma4 Pointer to structure used to initialize DMA registers for the Channel
45 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
46 @retval EFI_INVALID_PARAMETER Channel is not valid
47 @retval EFI_DEVICE_ERROR The system hardware could not map the requested information.
60 if (Channel
> DMA4_MAX_CHANNEL
) {
61 return EFI_INVALID_PARAMETER
;
64 /* 1) Configure the transfer parameters in the logical DMA registers */
65 /*-------------------------------------------------------------------*/
67 /* a) Set the data type CSDP[1:0], the Read/Write Port access type
68 CSDP[8:7]/[15:14], the Source/dest endianism CSDP[21]/CSDP[19],
69 write mode CSDP[17:16], source/dest packed or nonpacked CSDP[6]/CSDP[13] */
72 RegVal
= MmioRead32 (DMA4_CSDP (Channel
));
75 RegVal
= ((RegVal
& ~ 0x3) | DMA4
->DataType
);
76 RegVal
= ((RegVal
& ~(0x3 << 7)) | (DMA4
->ReadPortAccessType
<< 7));
77 RegVal
= ((RegVal
& ~(0x3 << 14)) | (DMA4
->WritePortAccessType
<< 14));
78 RegVal
= ((RegVal
& ~(0x1 << 21)) | (DMA4
->SourceEndiansim
<< 21));
79 RegVal
= ((RegVal
& ~(0x1 << 19)) | (DMA4
->DestinationEndianism
<< 19));
80 RegVal
= ((RegVal
& ~(0x3 << 16)) | (DMA4
->WriteMode
<< 16));
81 RegVal
= ((RegVal
& ~(0x1 << 6)) | (DMA4
->SourcePacked
<< 6));
82 RegVal
= ((RegVal
& ~(0x1 << 13)) | (DMA4
->DestinationPacked
<< 13));
84 MmioWrite32 (DMA4_CSDP (Channel
), RegVal
);
86 /* b) Set the number of element per frame CEN[23:0]*/
87 MmioWrite32 (DMA4_CEN (Channel
), DMA4
->NumberOfElementPerFrame
);
89 /* c) Set the number of frame per block CFN[15:0]*/
90 MmioWrite32 (DMA4_CFN (Channel
), DMA4
->NumberOfFramePerTransferBlock
);
92 /* d) Set the Source/dest start address index CSSA[31:0]/CDSA[31:0]*/
93 MmioWrite32 (DMA4_CSSA (Channel
), DMA4
->SourceStartAddress
);
94 MmioWrite32 (DMA4_CDSA (Channel
), DMA4
->DestinationStartAddress
);
96 /* e) Set the Read Port addressing mode CCR[13:12], the Write Port addressing mode CCR[15:14],
97 read/write priority CCR[6]/CCR[26]
98 I changed LCH CCR[20:19]=00 and CCR[4:0]=00000 to
99 LCH CCR[20:19]= DMA4->WriteRequestNumber and CCR[4:0]=DMA4->ReadRequestNumber
103 RegVal
= MmioRead32 (DMA4_CCR (Channel
));
106 RegVal
= ((RegVal
& ~0x1f) | DMA4
->ReadRequestNumber
);
107 RegVal
= ((RegVal
& ~(BIT20
| BIT19
)) | DMA4
->WriteRequestNumber
<< 19);
108 RegVal
= ((RegVal
& ~(0x3 << 12)) | (DMA4
->ReadPortAccessMode
<< 12));
109 RegVal
= ((RegVal
& ~(0x3 << 14)) | (DMA4
->WritePortAccessMode
<< 14));
110 RegVal
= ((RegVal
& ~(0x1 << 6)) | (DMA4
->ReadPriority
<< 6));
111 RegVal
= ((RegVal
& ~(0x1 << 26)) | (DMA4
->WritePriority
<< 26));
114 MmioWrite32 (DMA4_CCR (Channel
), RegVal
);
116 /* f)- Set the source element index CSEI[15:0]*/
117 MmioWrite32 (DMA4_CSEI (Channel
), DMA4
->SourceElementIndex
);
119 /* - Set the source frame index CSFI[15:0]*/
120 MmioWrite32 (DMA4_CSFI (Channel
), DMA4
->SourceFrameIndex
);
123 /* - Set the destination element index CDEI[15:0]*/
124 MmioWrite32 (DMA4_CDEI (Channel
), DMA4
->DestinationElementIndex
);
126 /* - Set the destination frame index CDFI[31:0]*/
127 MmioWrite32 (DMA4_CDFI (Channel
), DMA4
->DestinationFrameIndex
);
129 /* 2) Start the DMA transfer by Setting the enable bit CCR[7]=1 */
130 /*--------------------------------------------------------------*/
132 MmioOr32 (DMA4_CCR(0), DMA4_CCR_ENABLE
); //Launch transfer
138 Turn of DMA channel configured by EnableDma().
140 @param Channel DMA Channel to configure
142 @retval EFI_SUCCESS DMA hardware disabled
143 @retval EFI_INVALID_PARAMETER Channel is not valid
144 @retval EFI_DEVICE_ERROR The system hardware could not map the requested information.
153 if (Channel
> DMA4_MAX_CHANNEL
) {
154 return EFI_INVALID_PARAMETER
;
157 MmioAnd32 (DMA4_CCR(0), ~(DMA4_CCR_ENABLE
| DMA4_CCR_RD_ACTIVE
| DMA4_CCR_WR_ACTIVE
));
162 Provides the DMA controller-specific addresses needed to access system memory.
164 Operation is relative to the DMA bus master.
166 @param Operation Indicates if the bus master is going to read or write to system memory.
167 @param HostAddress The system memory address to map to the DMA controller.
168 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
170 @param DeviceAddress The resulting map address for the bus master controller to use to
171 access the hosts HostAddress.
172 @param Mapping A resulting value to pass to Unmap().
174 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
175 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
176 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
177 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
178 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
184 IN DMA_MAP_OPERATION Operation
,
185 IN VOID
*HostAddress
,
186 IN OUT UINTN
*NumberOfBytes
,
187 OUT PHYSICAL_ADDRESS
*DeviceAddress
,
191 MAP_INFO_INSTANCE
*Map
;
193 if ( HostAddress
== NULL
|| NumberOfBytes
== NULL
||
194 DeviceAddress
== NULL
|| Mapping
== NULL
) {
195 return EFI_INVALID_PARAMETER
;
199 if (Operation
>= MapOperationMaximum
) {
200 return EFI_INVALID_PARAMETER
;
203 *DeviceAddress
= ConvertToPhysicalAddress (HostAddress
);
205 // Remember range so we can flush on the other side
206 Map
= AllocatePool (sizeof (MAP_INFO_INSTANCE
));
208 return EFI_OUT_OF_RESOURCES
;
213 Map
->HostAddress
= (UINTN
)HostAddress
;
214 Map
->DeviceAddress
= *DeviceAddress
;
215 Map
->NumberOfBytes
= *NumberOfBytes
;
216 Map
->Operation
= Operation
;
218 // EfiCpuFlushTypeWriteBack, EfiCpuFlushTypeInvalidate
219 gCpu
->FlushDataCache (gCpu
, (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
, *NumberOfBytes
, EfiCpuFlushTypeWriteBackInvalidate
);
226 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
227 operation and releases any corresponding resources.
229 @param Mapping The mapping value returned from DmaMap*().
231 @retval EFI_SUCCESS The range was unmapped.
232 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
241 MAP_INFO_INSTANCE
*Map
;
243 if (Mapping
== NULL
) {
245 return EFI_INVALID_PARAMETER
;
248 Map
= (MAP_INFO_INSTANCE
*)Mapping
;
249 if (Map
->Operation
== MapOperationBusMasterWrite
) {
251 // Make sure we read buffer from uncached memory and not the cache
253 gCpu
->FlushDataCache (gCpu
, Map
->HostAddress
, Map
->NumberOfBytes
, EfiCpuFlushTypeInvalidate
);
262 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
265 @param MemoryType The type of memory to allocate, EfiBootServicesData or
266 EfiRuntimeServicesData.
267 @param Pages The number of pages to allocate.
268 @param HostAddress A pointer to store the base system memory address of the
271 @retval EFI_SUCCESS The requested memory pages were allocated.
272 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
273 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
274 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
275 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
280 IN EFI_MEMORY_TYPE MemoryType
,
282 OUT VOID
**HostAddress
285 if (HostAddress
== NULL
) {
286 return EFI_INVALID_PARAMETER
;
290 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
292 // We used uncached memory to keep coherency
294 if (MemoryType
== EfiBootServicesData
) {
295 *HostAddress
= UncachedAllocatePages (Pages
);
296 } else if (MemoryType
!= EfiRuntimeServicesData
) {
297 *HostAddress
= UncachedAllocateRuntimePages (Pages
);
299 return EFI_INVALID_PARAMETER
;
307 Frees memory that was allocated with DmaAllocateBuffer().
309 @param Pages The number of pages to free.
310 @param HostAddress The base system memory address of the allocated range.
312 @retval EFI_SUCCESS The requested memory pages were freed.
313 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
314 was not allocated with DmaAllocateBuffer().
324 if (HostAddress
== NULL
) {
325 return EFI_INVALID_PARAMETER
;
328 UncachedFreePages (HostAddress
, Pages
);
335 OmapDmaLibConstructor (
336 IN EFI_HANDLE ImageHandle
,
337 IN EFI_SYSTEM_TABLE
*SystemTable
342 // Get the Cpu protocol for later use
343 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&gCpu
);
344 ASSERT_EFI_ERROR(Status
);