BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / EmbeddedPkg / Universal / MmcDxe / MmcBlockIo.c
1 /** @file
2 *
3 * Copyright (c) 2011-2015, ARM Limited. All rights reserved.
4 *
5 * 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 <Library/BaseMemoryLib.h>
16
17 #include "Mmc.h"
18
19 EFI_STATUS
20 MmcNotifyState (
21 IN MMC_HOST_INSTANCE *MmcHostInstance,
22 IN MMC_STATE State
23 )
24 {
25 MmcHostInstance->State = State;
26 return MmcHostInstance->MmcHost->NotifyState (MmcHostInstance->MmcHost, State);
27 }
28
29 EFI_STATUS
30 EFIAPI
31 MmcGetCardStatus (
32 IN MMC_HOST_INSTANCE *MmcHostInstance
33 )
34 {
35 EFI_STATUS Status;
36 UINT32 Response[4];
37 UINTN CmdArg;
38 EFI_MMC_HOST_PROTOCOL *MmcHost;
39
40 Status = EFI_SUCCESS;
41 MmcHost = MmcHostInstance->MmcHost;
42 CmdArg = 0;
43
44 if (MmcHost == NULL) {
45 return EFI_INVALID_PARAMETER;
46 }
47 if (MmcHostInstance->State != MmcHwInitializationState) {
48 //Get the Status of the card.
49 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
50 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
51 if (EFI_ERROR (Status)) {
52 DEBUG ((EFI_D_ERROR, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status));
53 return Status;
54 }
55
56 //Read Response
57 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
58 PrintResponseR1 (Response[0]);
59 }
60
61 return Status;
62 }
63
64 EFI_STATUS
65 EFIAPI
66 MmcReset (
67 IN EFI_BLOCK_IO_PROTOCOL *This,
68 IN BOOLEAN ExtendedVerification
69 )
70 {
71 MMC_HOST_INSTANCE *MmcHostInstance;
72
73 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);
74
75 if (MmcHostInstance->MmcHost == NULL) {
76 // Nothing to do
77 return EFI_SUCCESS;
78 }
79
80 // If a card is not present then clear all media settings
81 if (!MmcHostInstance->MmcHost->IsCardPresent (MmcHostInstance->MmcHost)) {
82 MmcHostInstance->BlockIo.Media->MediaPresent = FALSE;
83 MmcHostInstance->BlockIo.Media->LastBlock = 0;
84 MmcHostInstance->BlockIo.Media->BlockSize = 512; // Should be zero but there is a bug in DiskIo
85 MmcHostInstance->BlockIo.Media->ReadOnly = FALSE;
86
87 // Indicate that the driver requires initialization
88 MmcHostInstance->State = MmcHwInitializationState;
89
90 return EFI_SUCCESS;
91 }
92
93 // Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn
94 // on power and restart Identification mode
95 return EFI_SUCCESS;
96 }
97
98 EFI_STATUS
99 MmcDetectCard (
100 EFI_MMC_HOST_PROTOCOL *MmcHost
101 )
102 {
103 if (!MmcHost->IsCardPresent (MmcHost)) {
104 return EFI_NO_MEDIA;
105 } else {
106 return EFI_SUCCESS;
107 }
108 }
109
110 EFI_STATUS
111 MmcStopTransmission (
112 EFI_MMC_HOST_PROTOCOL *MmcHost
113 )
114 {
115 EFI_STATUS Status;
116 UINT32 Response[4];
117 // Command 12 - Stop transmission (ends read or write)
118 // Normally only needed for streaming transfers or after error.
119 Status = MmcHost->SendCommand (MmcHost, MMC_CMD12, 0);
120 if (!EFI_ERROR (Status)) {
121 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1b, Response);
122 }
123 return Status;
124 }
125
126 #define MMCI0_BLOCKLEN 512
127 #define MMCI0_TIMEOUT 10000
128
129 STATIC
130 EFI_STATUS
131 MmcTransferBlock (
132 IN EFI_BLOCK_IO_PROTOCOL *This,
133 IN UINTN Cmd,
134 IN UINTN Transfer,
135 IN UINT32 MediaId,
136 IN EFI_LBA Lba,
137 IN UINTN BufferSize,
138 OUT VOID *Buffer
139 )
140 {
141 EFI_STATUS Status;
142 UINTN CmdArg;
143 INTN Timeout;
144 UINT32 Response[4];
145 MMC_HOST_INSTANCE *MmcHostInstance;
146 EFI_MMC_HOST_PROTOCOL *MmcHost;
147
148 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);
149 MmcHost = MmcHostInstance->MmcHost;
150
151 //Set command argument based on the card access mode (Byte mode or Block mode)
152 if ((MmcHostInstance->CardInfo.OCRData.AccessMode & MMC_OCR_ACCESS_MASK) ==
153 MMC_OCR_ACCESS_SECTOR) {
154 CmdArg = Lba;
155 } else {
156 CmdArg = Lba * This->Media->BlockSize;
157 }
158
159 Status = MmcHost->SendCommand (MmcHost, Cmd, CmdArg);
160 if (EFI_ERROR (Status)) {
161 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD%d): Error %r\n", __func__, Cmd, Status));
162 return Status;
163 }
164
165 if (Transfer == MMC_IOBLOCKS_READ) {
166 // Read Data
167 Status = MmcHost->ReadBlockData (MmcHost, Lba, BufferSize, Buffer);
168 if (EFI_ERROR (Status)) {
169 DEBUG ((EFI_D_BLKIO, "%a(): Error Read Block Data and Status = %r\n", __func__, Status));
170 MmcStopTransmission (MmcHost);
171 return Status;
172 }
173 Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState);
174 if (EFI_ERROR (Status)) {
175 DEBUG ((EFI_D_ERROR, "%a() : Error MmcProgrammingState\n", __func__));
176 return Status;
177 }
178 } else {
179 // Write Data
180 Status = MmcHost->WriteBlockData (MmcHost, Lba, BufferSize, Buffer);
181 if (EFI_ERROR (Status)) {
182 DEBUG ((EFI_D_BLKIO, "%a(): Error Write Block Data and Status = %r\n", __func__, Status));
183 MmcStopTransmission (MmcHost);
184 return Status;
185 }
186 }
187
188 // Command 13 - Read status and wait for programming to complete (return to tran)
189 Timeout = MMCI0_TIMEOUT;
190 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
191 Response[0] = 0;
192 while(!(Response[0] & MMC_R0_READY_FOR_DATA)
193 && (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)
194 && Timeout--) {
195 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
196 if (!EFI_ERROR (Status)) {
197 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
198 if (Response[0] & MMC_R0_READY_FOR_DATA) {
199 break; // Prevents delay once finished
200 }
201 }
202 }
203
204 if (BufferSize > This->Media->BlockSize) {
205 Status = MmcHost->SendCommand (MmcHost, MMC_CMD12, 0);
206 if (EFI_ERROR (Status)) {
207 DEBUG ((EFI_D_BLKIO, "%a(): Error and Status:%r\n", __func__, Status));
208 }
209 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1b, Response);
210 }
211
212 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);
213 if (EFI_ERROR (Status)) {
214 DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcTransferState\n"));
215 return Status;
216 }
217 return Status;
218 }
219
220 EFI_STATUS
221 MmcIoBlocks (
222 IN EFI_BLOCK_IO_PROTOCOL *This,
223 IN UINTN Transfer,
224 IN UINT32 MediaId,
225 IN EFI_LBA Lba,
226 IN UINTN BufferSize,
227 OUT VOID *Buffer
228 )
229 {
230 UINT32 Response[4];
231 EFI_STATUS Status;
232 UINTN CmdArg;
233 INTN Timeout;
234 UINTN Cmd;
235 MMC_HOST_INSTANCE *MmcHostInstance;
236 EFI_MMC_HOST_PROTOCOL *MmcHost;
237 UINTN BytesRemainingToBeTransfered;
238 UINTN BlockCount;
239 UINTN ConsumeSize;
240
241 BlockCount = 1;
242 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);
243 ASSERT (MmcHostInstance != NULL);
244 MmcHost = MmcHostInstance->MmcHost;
245 ASSERT (MmcHost);
246
247 if (This->Media->MediaId != MediaId) {
248 return EFI_MEDIA_CHANGED;
249 }
250
251 if ((MmcHost == NULL) || (Buffer == NULL)) {
252 return EFI_INVALID_PARAMETER;
253 }
254
255 // Check if a Card is Present
256 if (!MmcHostInstance->BlockIo.Media->MediaPresent) {
257 return EFI_NO_MEDIA;
258 }
259
260 if (MMC_HOST_HAS_ISMULTIBLOCK(MmcHost) && MmcHost->IsMultiBlock(MmcHost)) {
261 BlockCount = (BufferSize + This->Media->BlockSize - 1) / This->Media->BlockSize;
262 }
263
264 // All blocks must be within the device
265 if ((Lba + (BufferSize / This->Media->BlockSize)) > (This->Media->LastBlock + 1)) {
266 return EFI_INVALID_PARAMETER;
267 }
268
269 if ((Transfer == MMC_IOBLOCKS_WRITE) && (This->Media->ReadOnly == TRUE)) {
270 return EFI_WRITE_PROTECTED;
271 }
272
273 // Reading 0 Byte is valid
274 if (BufferSize == 0) {
275 return EFI_SUCCESS;
276 }
277
278 // The buffer size must be an exact multiple of the block size
279 if ((BufferSize % This->Media->BlockSize) != 0) {
280 return EFI_BAD_BUFFER_SIZE;
281 }
282
283 // Check the alignment
284 if ((This->Media->IoAlign > 2) && (((UINTN)Buffer & (This->Media->IoAlign - 1)) != 0)) {
285 return EFI_INVALID_PARAMETER;
286 }
287
288 BytesRemainingToBeTransfered = BufferSize;
289 while (BytesRemainingToBeTransfered > 0) {
290
291 // Check if the Card is in Ready status
292 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
293 Response[0] = 0;
294 Timeout = 20;
295 while( (!(Response[0] & MMC_R0_READY_FOR_DATA))
296 && (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)
297 && Timeout--) {
298 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
299 if (!EFI_ERROR (Status)) {
300 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
301 }
302 }
303
304 if (0 == Timeout) {
305 DEBUG ((EFI_D_ERROR, "The Card is busy\n"));
306 return EFI_NOT_READY;
307 }
308
309 if (Transfer == MMC_IOBLOCKS_READ) {
310 if (BlockCount == 1) {
311 // Read a single block
312 Cmd = MMC_CMD17;
313 } else {
314 // Read multiple blocks
315 Cmd = MMC_CMD18;
316 }
317 } else {
318 if (BlockCount == 1) {
319 // Write a single block
320 Cmd = MMC_CMD24;
321 } else {
322 // Write multiple blocks
323 Cmd = MMC_CMD25;
324 }
325 }
326
327 ConsumeSize = BlockCount * This->Media->BlockSize;
328 if (BytesRemainingToBeTransfered < ConsumeSize) {
329 ConsumeSize = BytesRemainingToBeTransfered;
330 }
331 Status = MmcTransferBlock (This, Cmd, Transfer, MediaId, Lba, ConsumeSize, Buffer);
332 if (EFI_ERROR (Status)) {
333 DEBUG ((EFI_D_ERROR, "%a(): Failed to transfer block and Status:%r\n", __func__, Status));
334 }
335
336 BytesRemainingToBeTransfered -= ConsumeSize;
337 if (BytesRemainingToBeTransfered > 0) {
338 Lba += BlockCount;
339 Buffer = (UINT8 *)Buffer + ConsumeSize;
340 }
341 }
342
343 return EFI_SUCCESS;
344 }
345
346 EFI_STATUS
347 EFIAPI
348 MmcReadBlocks (
349 IN EFI_BLOCK_IO_PROTOCOL *This,
350 IN UINT32 MediaId,
351 IN EFI_LBA Lba,
352 IN UINTN BufferSize,
353 OUT VOID *Buffer
354 )
355 {
356 return MmcIoBlocks (This, MMC_IOBLOCKS_READ, MediaId, Lba, BufferSize, Buffer);
357 }
358
359 EFI_STATUS
360 EFIAPI
361 MmcWriteBlocks (
362 IN EFI_BLOCK_IO_PROTOCOL *This,
363 IN UINT32 MediaId,
364 IN EFI_LBA Lba,
365 IN UINTN BufferSize,
366 IN VOID *Buffer
367 )
368 {
369 return MmcIoBlocks (This, MMC_IOBLOCKS_WRITE, MediaId, Lba, BufferSize, Buffer);
370 }
371
372 EFI_STATUS
373 EFIAPI
374 MmcFlushBlocks (
375 IN EFI_BLOCK_IO_PROTOCOL *This
376 )
377 {
378 return EFI_SUCCESS;
379 }