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 MmioWrite32 (DMA4_CDFI (Channel
), DMA4
->DestinationFrameIndex
);
131 // Enable all the status bits since we are polling
132 MmioWrite32 (DMA4_CICR (Channel
), DMA4_CICR_ENABLE_ALL
);
133 MmioWrite32 (DMA4_CSR (Channel
), DMA4_CSR_RESET
);
135 /* 2) Start the DMA transfer by Setting the enable bit CCR[7]=1 */
136 /*--------------------------------------------------------------*/
138 MmioOr32 (DMA4_CCR(Channel
), DMA4_CCR_ENABLE
); //Launch transfer
144 Turn of DMA channel configured by EnableDma().
146 @param Channel DMA Channel to configure
147 @param SuccesMask Bits in DMA4_CSR register indicate EFI_SUCCESS
148 @param ErrorMask Bits in DMA4_CSR register indicate EFI_DEVICE_ERROR
150 @retval EFI_SUCCESS DMA hardware disabled
151 @retval EFI_INVALID_PARAMETER Channel is not valid
152 @retval EFI_DEVICE_ERROR The system hardware could not map the requested information.
159 IN UINT32 SuccessMask
,
163 EFI_STATUS Status
= EFI_SUCCESS
;
167 if (Channel
> DMA4_MAX_CHANNEL
) {
168 return EFI_INVALID_PARAMETER
;
172 Reg
= MmioRead32 (DMA4_CSR(Channel
));
173 if ((Reg
& ErrorMask
) != 0) {
174 Status
= EFI_DEVICE_ERROR
;
175 DEBUG ((EFI_D_ERROR
, "DMA Error (%d) %x\n", Channel
, Reg
));
178 } while ((Reg
& SuccessMask
) != SuccessMask
);
181 // Disable all status bits and clear them
182 MmioWrite32 (DMA4_CICR (Channel
), 0);
183 MmioWrite32 (DMA4_CSR (Channel
), DMA4_CSR_RESET
);
185 MmioAnd32 (DMA4_CCR(0), ~(DMA4_CCR_ENABLE
| DMA4_CCR_RD_ACTIVE
| DMA4_CCR_WR_ACTIVE
));
192 Provides the DMA controller-specific addresses needed to access system memory.
194 Operation is relative to the DMA bus master.
196 @param Operation Indicates if the bus master is going to read or write to system memory.
197 @param HostAddress The system memory address to map to the DMA controller.
198 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
200 @param DeviceAddress The resulting map address for the bus master controller to use to
201 access the hosts HostAddress.
202 @param Mapping A resulting value to pass to Unmap().
204 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
205 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
206 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
207 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
208 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
214 IN DMA_MAP_OPERATION Operation
,
215 IN VOID
*HostAddress
,
216 IN OUT UINTN
*NumberOfBytes
,
217 OUT PHYSICAL_ADDRESS
*DeviceAddress
,
221 MAP_INFO_INSTANCE
*Map
;
223 if ( HostAddress
== NULL
|| NumberOfBytes
== NULL
||
224 DeviceAddress
== NULL
|| Mapping
== NULL
) {
225 return EFI_INVALID_PARAMETER
;
229 if (Operation
>= MapOperationMaximum
) {
230 return EFI_INVALID_PARAMETER
;
233 *DeviceAddress
= ConvertToPhysicalAddress (HostAddress
);
235 // Remember range so we can flush on the other side
236 Map
= AllocatePool (sizeof (MAP_INFO_INSTANCE
));
238 return EFI_OUT_OF_RESOURCES
;
243 Map
->HostAddress
= (UINTN
)HostAddress
;
244 Map
->DeviceAddress
= *DeviceAddress
;
245 Map
->NumberOfBytes
= *NumberOfBytes
;
246 Map
->Operation
= Operation
;
248 // EfiCpuFlushTypeWriteBack, EfiCpuFlushTypeInvalidate
249 gCpu
->FlushDataCache (gCpu
, (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
, *NumberOfBytes
, EfiCpuFlushTypeWriteBackInvalidate
);
256 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
257 operation and releases any corresponding resources.
259 @param Mapping The mapping value returned from DmaMap*().
261 @retval EFI_SUCCESS The range was unmapped.
262 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
271 MAP_INFO_INSTANCE
*Map
;
273 if (Mapping
== NULL
) {
275 return EFI_INVALID_PARAMETER
;
278 Map
= (MAP_INFO_INSTANCE
*)Mapping
;
279 if (Map
->Operation
== MapOperationBusMasterWrite
) {
281 // Make sure we read buffer from uncached memory and not the cache
283 gCpu
->FlushDataCache (gCpu
, Map
->HostAddress
, Map
->NumberOfBytes
, EfiCpuFlushTypeInvalidate
);
292 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
295 @param MemoryType The type of memory to allocate, EfiBootServicesData or
296 EfiRuntimeServicesData.
297 @param Pages The number of pages to allocate.
298 @param HostAddress A pointer to store the base system memory address of the
301 @retval EFI_SUCCESS The requested memory pages were allocated.
302 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
303 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
304 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
305 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
310 IN EFI_MEMORY_TYPE MemoryType
,
312 OUT VOID
**HostAddress
315 if (HostAddress
== NULL
) {
316 return EFI_INVALID_PARAMETER
;
320 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
322 // We used uncached memory to keep coherency
324 if (MemoryType
== EfiBootServicesData
) {
325 *HostAddress
= UncachedAllocatePages (Pages
);
326 } else if (MemoryType
!= EfiRuntimeServicesData
) {
327 *HostAddress
= UncachedAllocateRuntimePages (Pages
);
329 return EFI_INVALID_PARAMETER
;
337 Frees memory that was allocated with DmaAllocateBuffer().
339 @param Pages The number of pages to free.
340 @param HostAddress The base system memory address of the allocated range.
342 @retval EFI_SUCCESS The requested memory pages were freed.
343 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
344 was not allocated with DmaAllocateBuffer().
354 if (HostAddress
== NULL
) {
355 return EFI_INVALID_PARAMETER
;
358 UncachedFreePages (HostAddress
, Pages
);
365 OmapDmaLibConstructor (
366 IN EFI_HANDLE ImageHandle
,
367 IN EFI_SYSTEM_TABLE
*SystemTable
372 // Get the Cpu protocol for later use
373 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&gCpu
);
374 ASSERT_EFI_ERROR(Status
);