blob: 41cfefbebc11d201c46ef766d2c95ae8513d7069 [file] [log] [blame]
andrewfish7f814ff2010-05-08 19:32:03 +00001/** @file
andrewfish81bc2052010-05-29 00:21:30 +00002 Abstractions for simple OMAP DMA channel.
Ronald Cron3402aac2014-08-19 13:29:52 +00003
andrewfish7f814ff2010-05-08 19:32:03 +00004
5 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
Ronald Cron3402aac2014-08-19 13:29:52 +00006
andrewfish7f814ff2010-05-08 19:32:03 +00007 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
11
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.
14
15**/
16
17#include <Base.h>
18#include <Library/DebugLib.h>
19#include <Library/OmapDmaLib.h>
andrewfish7f814ff2010-05-08 19:32:03 +000020#include <Library/IoLib.h>
andrewfish8e7c9e02010-05-28 00:31:53 +000021#include <Library/BaseMemoryLib.h>
andrewfish7f814ff2010-05-08 19:32:03 +000022#include <Omap3530/Omap3530.h>
23
andrewfish7f814ff2010-05-08 19:32:03 +000024
Ronald Cron3402aac2014-08-19 13:29:52 +000025/**
andrewfish7f814ff2010-05-08 19:32:03 +000026 Configure OMAP DMA Channel
Ronald Cron3402aac2014-08-19 13:29:52 +000027
andrewfish7f814ff2010-05-08 19:32:03 +000028 @param Channel DMA Channel to configure
Ronald Cron3402aac2014-08-19 13:29:52 +000029 @param Dma4 Pointer to structure used to initialize DMA registers for the Channel
30
andrewfish7f814ff2010-05-08 19:32:03 +000031 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
32 @retval EFI_INVALID_PARAMETER Channel is not valid
33 @retval EFI_DEVICE_ERROR The system hardware could not map the requested information.
Ronald Cron3402aac2014-08-19 13:29:52 +000034
andrewfish8e7c9e02010-05-28 00:31:53 +000035**/
36EFI_STATUS
37EFIAPI
38EnableDmaChannel (
39 IN UINTN Channel,
40 IN OMAP_DMA4 *DMA4
41 )
42{
43 UINT32 RegVal;
44
45
46 if (Channel > DMA4_MAX_CHANNEL) {
47 return EFI_INVALID_PARAMETER;
48 }
49
andrewfish7f814ff2010-05-08 19:32:03 +000050 /* 1) Configure the transfer parameters in the logical DMA registers */
51 /*-------------------------------------------------------------------*/
52
Ronald Cron3402aac2014-08-19 13:29:52 +000053 /* a) Set the data type CSDP[1:0], the Read/Write Port access type
54 CSDP[8:7]/[15:14], the Source/dest endianism CSDP[21]/CSDP[19],
andrewfish7f814ff2010-05-08 19:32:03 +000055 write mode CSDP[17:16], source/dest packed or nonpacked CSDP[6]/CSDP[13] */
Ronald Cron3402aac2014-08-19 13:29:52 +000056
andrewfish7f814ff2010-05-08 19:32:03 +000057 // Read CSDP
58 RegVal = MmioRead32 (DMA4_CSDP (Channel));
Ronald Cron3402aac2014-08-19 13:29:52 +000059
andrewfish7f814ff2010-05-08 19:32:03 +000060 // Build reg
61 RegVal = ((RegVal & ~ 0x3) | DMA4->DataType );
62 RegVal = ((RegVal & ~(0x3 << 7)) | (DMA4->ReadPortAccessType << 7));
63 RegVal = ((RegVal & ~(0x3 << 14)) | (DMA4->WritePortAccessType << 14));
64 RegVal = ((RegVal & ~(0x1 << 21)) | (DMA4->SourceEndiansim << 21));
65 RegVal = ((RegVal & ~(0x1 << 19)) | (DMA4->DestinationEndianism << 19));
66 RegVal = ((RegVal & ~(0x3 << 16)) | (DMA4->WriteMode << 16));
67 RegVal = ((RegVal & ~(0x1 << 6)) | (DMA4->SourcePacked << 6));
68 RegVal = ((RegVal & ~(0x1 << 13)) | (DMA4->DestinationPacked << 13));
69 // Write CSDP
70 MmioWrite32 (DMA4_CSDP (Channel), RegVal);
Ronald Cron3402aac2014-08-19 13:29:52 +000071
andrewfish7f814ff2010-05-08 19:32:03 +000072 /* b) Set the number of element per frame CEN[23:0]*/
73 MmioWrite32 (DMA4_CEN (Channel), DMA4->NumberOfElementPerFrame);
Ronald Cron3402aac2014-08-19 13:29:52 +000074
andrewfish7f814ff2010-05-08 19:32:03 +000075 /* c) Set the number of frame per block CFN[15:0]*/
76 MmioWrite32 (DMA4_CFN (Channel), DMA4->NumberOfFramePerTransferBlock);
Ronald Cron3402aac2014-08-19 13:29:52 +000077
andrewfish7f814ff2010-05-08 19:32:03 +000078 /* d) Set the Source/dest start address index CSSA[31:0]/CDSA[31:0]*/
79 MmioWrite32 (DMA4_CSSA (Channel), DMA4->SourceStartAddress);
80 MmioWrite32 (DMA4_CDSA (Channel), DMA4->DestinationStartAddress);
Ronald Cron3402aac2014-08-19 13:29:52 +000081
andrewfish7f814ff2010-05-08 19:32:03 +000082 /* e) Set the Read Port addressing mode CCR[13:12], the Write Port addressing mode CCR[15:14],
83 read/write priority CCR[6]/CCR[26]
Ronald Cron3402aac2014-08-19 13:29:52 +000084 I changed LCH CCR[20:19]=00 and CCR[4:0]=00000 to
andrewfish7f814ff2010-05-08 19:32:03 +000085 LCH CCR[20:19]= DMA4->WriteRequestNumber and CCR[4:0]=DMA4->ReadRequestNumber
86 */
Ronald Cron3402aac2014-08-19 13:29:52 +000087
andrewfish7f814ff2010-05-08 19:32:03 +000088 // Read CCR
89 RegVal = MmioRead32 (DMA4_CCR (Channel));
90
91 // Build reg
92 RegVal = ((RegVal & ~0x1f) | DMA4->ReadRequestNumber);
93 RegVal = ((RegVal & ~(BIT20 | BIT19)) | DMA4->WriteRequestNumber << 19);
94 RegVal = ((RegVal & ~(0x3 << 12)) | (DMA4->ReadPortAccessMode << 12));
95 RegVal = ((RegVal & ~(0x3 << 14)) | (DMA4->WritePortAccessMode << 14));
96 RegVal = ((RegVal & ~(0x1 << 6)) | (DMA4->ReadPriority << 6));
97 RegVal = ((RegVal & ~(0x1 << 26)) | (DMA4->WritePriority << 26));
Ronald Cron3402aac2014-08-19 13:29:52 +000098
andrewfish7f814ff2010-05-08 19:32:03 +000099 // Write CCR
100 MmioWrite32 (DMA4_CCR (Channel), RegVal);
Ronald Cron3402aac2014-08-19 13:29:52 +0000101
andrewfish7f814ff2010-05-08 19:32:03 +0000102 /* f)- Set the source element index CSEI[15:0]*/
103 MmioWrite32 (DMA4_CSEI (Channel), DMA4->SourceElementIndex);
Ronald Cron3402aac2014-08-19 13:29:52 +0000104
andrewfish7f814ff2010-05-08 19:32:03 +0000105 /* - Set the source frame index CSFI[15:0]*/
106 MmioWrite32 (DMA4_CSFI (Channel), DMA4->SourceFrameIndex);
107
108
109 /* - Set the destination element index CDEI[15:0]*/
110 MmioWrite32 (DMA4_CDEI (Channel), DMA4->DestinationElementIndex);
111
112 /* - Set the destination frame index CDFI[31:0]*/
113 MmioWrite32 (DMA4_CDFI (Channel), DMA4->DestinationFrameIndex);
Ronald Cron3402aac2014-08-19 13:29:52 +0000114
andrewfish9f6b9772010-05-11 00:06:47 +0000115 MmioWrite32 (DMA4_CDFI (Channel), DMA4->DestinationFrameIndex);
116
117 // Enable all the status bits since we are polling
118 MmioWrite32 (DMA4_CICR (Channel), DMA4_CICR_ENABLE_ALL);
119 MmioWrite32 (DMA4_CSR (Channel), DMA4_CSR_RESET);
120
andrewfish7f814ff2010-05-08 19:32:03 +0000121 /* 2) Start the DMA transfer by Setting the enable bit CCR[7]=1 */
122 /*--------------------------------------------------------------*/
123 //write enable bit
andrewfish8e7c9e02010-05-28 00:31:53 +0000124 MmioOr32 (DMA4_CCR(Channel), DMA4_CCR_ENABLE); //Launch transfer
125
126 return EFI_SUCCESS;
127}
128
Ronald Cron3402aac2014-08-19 13:29:52 +0000129/**
andrewfish7f814ff2010-05-08 19:32:03 +0000130 Turn of DMA channel configured by EnableDma().
Ronald Cron3402aac2014-08-19 13:29:52 +0000131
andrewfish7f814ff2010-05-08 19:32:03 +0000132 @param Channel DMA Channel to configure
andrewfish9f6b9772010-05-11 00:06:47 +0000133 @param SuccesMask Bits in DMA4_CSR register indicate EFI_SUCCESS
134 @param ErrorMask Bits in DMA4_CSR register indicate EFI_DEVICE_ERROR
Ronald Cron3402aac2014-08-19 13:29:52 +0000135
andrewfish7f814ff2010-05-08 19:32:03 +0000136 @retval EFI_SUCCESS DMA hardware disabled
137 @retval EFI_INVALID_PARAMETER Channel is not valid
138 @retval EFI_DEVICE_ERROR The system hardware could not map the requested information.
Ronald Cron3402aac2014-08-19 13:29:52 +0000139
andrewfish8e7c9e02010-05-28 00:31:53 +0000140**/
141EFI_STATUS
142EFIAPI
143DisableDmaChannel (
144 IN UINTN Channel,
145 IN UINT32 SuccessMask,
146 IN UINT32 ErrorMask
147 )
148{
149 EFI_STATUS Status = EFI_SUCCESS;
150 UINT32 Reg;
151
152
153 if (Channel > DMA4_MAX_CHANNEL) {
154 return EFI_INVALID_PARAMETER;
155 }
156
157 do {
158 Reg = MmioRead32 (DMA4_CSR(Channel));
159 if ((Reg & ErrorMask) != 0) {
160 Status = EFI_DEVICE_ERROR;
161 DEBUG ((EFI_D_ERROR, "DMA Error (%d) %x\n", Channel, Reg));
162 break;
163 }
164 } while ((Reg & SuccessMask) != SuccessMask);
165
166
167 // Disable all status bits and clear them
andrewfish9f6b9772010-05-11 00:06:47 +0000168 MmioWrite32 (DMA4_CICR (Channel), 0);
andrewfish8e7c9e02010-05-28 00:31:53 +0000169 MmioWrite32 (DMA4_CSR (Channel), DMA4_CSR_RESET);
170
Ronald Cron3402aac2014-08-19 13:29:52 +0000171 MmioAnd32 (DMA4_CCR(0), ~(DMA4_CCR_ENABLE | DMA4_CCR_RD_ACTIVE | DMA4_CCR_WR_ACTIVE));
andrewfish8e7c9e02010-05-28 00:31:53 +0000172 return Status;
173}
174
175
176