8940f519b749ac1c43aa9fd8b544180cf5caaf46
[mirror_edk2.git] / EmbeddedPkg / Universal / MmcDxe / MmcBlockIo.c
1 /** @file
2 *
3 * Copyright (c) 2011, 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 <Protocol/MmcHost.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/TimerLib.h>
19
20 #include "Mmc.h"
21
22 // Untested ...
23 //#define USE_STREAM
24
25 #define MAX_RETRY_COUNT 1000
26 #define CMD_RETRY_COUNT 20
27
28 EFI_STATUS
29 MmcNotifyState (
30 IN MMC_HOST_INSTANCE *MmcHostInstance,
31 IN MMC_STATE State
32 )
33 {
34 MmcHostInstance->State = State;
35 return MmcHostInstance->MmcHost->NotifyState (MmcHostInstance->MmcHost, State);
36 }
37
38 VOID
39 PrintOCR (
40 IN UINT32 Ocr
41 )
42 {
43 UINTN minv, maxv, volts;
44 UINTN loop;
45
46 minv = 36; // 3.6
47 maxv = 20; // 2.0
48 volts = 20; // 2.0
49
50 // The MMC register bits [23:8] indicate the working range of the card
51 for (loop = 8; loop < 24; loop++) {
52 if (Ocr & (1 << loop)) {
53 if (minv > volts) minv = volts;
54 if (maxv < volts) maxv = volts + 1;
55 }
56 volts = volts + 1;
57 }
58
59 DEBUG((EFI_D_ERROR, "- PrintOCR Ocr (0x%X)\n",Ocr));
60 DEBUG((EFI_D_ERROR, "\t- Card operating voltage: %d.%d to %d.%d\n", minv/10, minv % 10, maxv/10, maxv % 10));
61 if (((Ocr >> 29) & 3) == 0) {
62 DEBUG((EFI_D_ERROR, "\t- AccessMode: Byte Mode\n"));
63 } else {
64 DEBUG((EFI_D_ERROR, "\t- AccessMode: Block Mode (0x%X)\n",((Ocr >> 29) & 3)));
65 }
66
67 if (Ocr & MMC_OCR_POWERUP) {
68 DEBUG((EFI_D_ERROR, "\t- PowerUp\n"));
69 } else {
70 DEBUG((EFI_D_ERROR, "\t- Voltage Not Supported\n"));
71 }
72 }
73
74 VOID PrintCID (
75 IN UINT32* Cid
76 )
77 {
78 DEBUG((EFI_D_ERROR, "- PrintCID\n"));
79 DEBUG((EFI_D_ERROR, "\t- Manufacturing date: %d/%d\n",(Cid[0] >> 8) & 0xF,(Cid[0] >> 12) & 0xFF));
80 DEBUG((EFI_D_ERROR, "\t- Product serial number: 0x%X%X\n",Cid[1] & 0xFFFFFF,(Cid[0] >> 24) & 0xFF));
81 DEBUG((EFI_D_ERROR, "\t- Product revision: %d\n",Cid[1] >> 24));
82 //DEBUG((EFI_D_ERROR, "\t- Product name: %s\n",(char*)(Cid + 2)));
83 DEBUG((EFI_D_ERROR, "\t- OEM ID: %c%c\n",(Cid[3] >> 8) & 0xFF,(Cid[3] >> 16) & 0xFF));
84 }
85
86 #if !defined(MDEPKG_NDEBUG)
87 CONST CHAR8* mStrUnit[] = { "100kbit/s","1Mbit/s","10Mbit/s","100MBit/s","Unkbown","Unkbown","Unkbown","Unkbown" };
88 CONST CHAR8* mStrValue[] = { "1.0","1.2","1.3","1.5","2.0","2.5","3.0","3.5","4.0","4.5","5.0","Unknown","Unknown","Unknown","Unknown" };
89 #endif
90
91 VOID
92 PrintCSD (
93 IN UINT32* Csd
94 )
95 {
96 UINTN Value;
97
98 if (((Csd[2] >> 30) & 0x3) == 0) {
99 DEBUG((EFI_D_ERROR, "- PrintCSD Version 1.01-1.10/Version 2.00/Standard Capacity\n"));
100 } else if (((Csd[2] >> 30) & 0x3) == 1) {
101 DEBUG((EFI_D_ERROR, "- PrintCSD Version 2.00/High Capacity\n"));
102 } else {
103 DEBUG((EFI_D_ERROR, "- PrintCSD Version Higher than v3.3\n"));
104 }
105
106 DEBUG((EFI_D_ERROR, "\t- Supported card command class: 0x%X\n",MMC_CSD_GET_CCC(Csd)));
107 DEBUG((EFI_D_ERROR, "\t- Speed: %a %a\n",mStrValue[(MMC_CSD_GET_TRANSPEED(Csd) >> 3) & 0xF],mStrUnit[MMC_CSD_GET_TRANSPEED(Csd) & 7]));
108 DEBUG((EFI_D_ERROR, "\t- Maximum Read Data Block: %d\n",2 << (MMC_CSD_GET_READBLLEN(Csd)-1)));
109 DEBUG((EFI_D_ERROR, "\t- Maximum Write Data Block: %d\n",2 << (MMC_CSD_GET_WRITEBLLEN(Csd)-1)));
110
111 if (!MMC_CSD_GET_FILEFORMATGRP(Csd)) {
112 Value = MMC_CSD_GET_FILEFORMAT(Csd);
113 if (Value == 0) DEBUG((EFI_D_ERROR, "\t- Format(0): Hard disk-like file system with partition table\n"));
114 else if (Value == 1) DEBUG((EFI_D_ERROR, "\t- Format(1): DOS FAT (floppy-like) with boot sector only (no partition table)\n"));
115 else if (Value == 2) DEBUG((EFI_D_ERROR, "\t- Format(2): Universal File Format\n"));
116 else DEBUG((EFI_D_ERROR, "\t- Format(3): Others/Unknown\n"));
117 } else {
118 DEBUG((EFI_D_ERROR, "\t- Format: Reserved\n"));
119 }
120 }
121
122 VOID
123 PrintRCA (
124 IN UINT32 Rca
125 )
126 {
127 DEBUG((EFI_D_ERROR, "- PrintRCA: 0x%X\n",Rca));
128 DEBUG((EFI_D_ERROR, "\t- Status: 0x%X\n",Rca & 0xFFFF));
129 DEBUG((EFI_D_ERROR, "\t- RCA: 0x%X\n",(Rca >> 16) & 0xFFFF));
130 }
131
132 VOID
133 PrintResponseR1 (
134 IN UINT32 Response
135 )
136 {
137 DEBUG((EFI_D_INFO, "Response: 0x%X\n",Response));
138 if (Response & (1 << 8)) DEBUG((EFI_D_INFO, "\t- READY_FOR_DATA\n"));
139
140 if (((Response >> 9) & 0xF) == 0) DEBUG((EFI_D_INFO, "\t- State: Idle\n"));
141 else if (((Response >> 9) & 0xF) == 1) DEBUG((EFI_D_INFO, "\t- State: Ready\n"));
142 else if (((Response >> 9) & 0xF) == 2) DEBUG((EFI_D_INFO, "\t- State: Ident\n"));
143 else if (((Response >> 9) & 0xF) == 3) DEBUG((EFI_D_INFO, "\t- State: StandBy\n"));
144 else if (((Response >> 9) & 0xF) == 4) DEBUG((EFI_D_INFO, "\t- State: Tran\n"));
145 else if (((Response >> 9) & 0xF) == 5) DEBUG((EFI_D_INFO, "\t- State: Data\n"));
146 else if (((Response >> 9) & 0xF) == 6) DEBUG((EFI_D_INFO, "\t- State: Rcv\n"));
147 else if (((Response >> 9) & 0xF) == 7) DEBUG((EFI_D_INFO, "\t- State: Prg\n"));
148 else if (((Response >> 9) & 0xF) == 8) DEBUG((EFI_D_INFO, "\t- State: Dis\n"));
149 else DEBUG((EFI_D_INFO, "\t- State: Reserved\n"));
150 }
151
152 EFI_STATUS
153 EFIAPI
154 MmcGetCardStatus(
155 IN MMC_HOST_INSTANCE *MmcHostInstance
156 )
157 {
158 EFI_STATUS Status;
159 UINT32 Response[4];
160 UINTN CmdArg;
161 EFI_MMC_HOST_PROTOCOL *MmcHost;
162
163 Status = EFI_SUCCESS;
164 MmcHost = MmcHostInstance->MmcHost;
165 CmdArg = 0;
166
167 if (MmcHost == NULL) {
168 return EFI_INVALID_PARAMETER;
169 }
170 if(MmcHostInstance->State != MmcHwInitializationState){
171 //Get the Status of the card.
172 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
173 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
174 if (EFI_ERROR(Status)) {
175 DEBUG((EFI_D_ERROR, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status));
176 return Status;
177 }
178
179 //Read Response
180 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1,Response);
181 PrintResponseR1(Response[0]);
182 }
183
184 return Status;
185 }
186
187 EFI_STATUS
188 EFIAPI
189 MmcIdentificationMode (
190 IN MMC_HOST_INSTANCE *MmcHostInstance
191 )
192 {
193 EFI_STATUS Status;
194 UINT32 Response[4];
195 UINTN Timeout;
196 UINTN CmdArg;
197 BOOLEAN IsHCS;
198 EFI_MMC_HOST_PROTOCOL *MmcHost;
199
200 MmcHost = MmcHostInstance->MmcHost;
201 CmdArg = 0;
202 IsHCS = FALSE;
203
204 if (MmcHost == NULL) {
205 return EFI_INVALID_PARAMETER;
206 }
207
208 // We can get into this function if we restart the identification mode
209 if (MmcHostInstance->State == MmcHwInitializationState) {
210 // Initialize the MMC Host HW
211 Status = MmcNotifyState (MmcHostInstance, MmcHwInitializationState);
212 if (EFI_ERROR(Status)) {
213 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcHwInitializationState\n"));
214 return Status;
215 }
216 } else {
217 //Note: Could even be used in all cases. But it looks this command could put the state machine into inactive for some cards
218 Status = MmcHost->SendCommand (MmcHost, MMC_CMD0, 0);
219 if (EFI_ERROR(Status)) {
220 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD0): Error\n"));
221 return Status;
222 }
223 }
224
225 Status = MmcNotifyState (MmcHostInstance, MmcIdleState);
226 if (EFI_ERROR(Status)) {
227 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdleState\n"));
228 return Status;
229 }
230
231 // Are we using SDIO ?
232 Status = MmcHost->SendCommand (MmcHost, MMC_CMD5, 0);
233 if (Status == EFI_SUCCESS) {
234 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported.\n"));
235 return EFI_UNSUPPORTED;
236 }
237
238 // Check which kind of card we are using. Ver2.00 or later SD Memory Card (PL180 is SD v1.1)
239 CmdArg = (0x0UL << 12 | BIT8 | 0xCEUL << 0);
240 Status = MmcHost->SendCommand (MmcHost, MMC_CMD8, CmdArg);
241 if (Status == EFI_SUCCESS) {
242 DEBUG ((EFI_D_ERROR, "Card is SD2.0 => Supports high capacity\n"));
243 IsHCS = TRUE;
244 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R7,Response);
245 PrintResponseR1(Response[0]);
246 //check if it is valid response
247 if(Response[0] != CmdArg){
248 DEBUG ((EFI_D_ERROR, "The Card is not usable\n"));
249 return EFI_UNSUPPORTED;
250 }
251 } else {
252 DEBUG ((EFI_D_ERROR, "Not a SD2.0 Card\n"));
253 }
254
255 // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.Busy == 1)
256 Timeout = MAX_RETRY_COUNT;
257 while (Timeout > 0) {
258 // SD Card or MMC Card ? CMD55 indicates to the card that the next command is an application specific command
259 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, 0);
260 if (Status == EFI_SUCCESS) {
261 DEBUG ((EFI_D_INFO, "Card should be SD\n"));
262 if (IsHCS) {
263 MmcHostInstance->CardInfo.CardType = SD_CARD_2;
264 } else {
265 MmcHostInstance->CardInfo.CardType = SD_CARD;
266 }
267
268 // Note: The first time CmdArg will be zero
269 CmdArg = ((UINTN *) &(MmcHostInstance->CardInfo.OCRData))[0];
270 if (IsHCS) {
271 CmdArg |= BIT30;
272 }
273 Status = MmcHost->SendCommand (MmcHost, MMC_ACMD41, CmdArg);
274 if (!EFI_ERROR(Status)) {
275 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR,Response);
276 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];
277 }
278 } else {
279 DEBUG ((EFI_D_INFO, "Card should be MMC\n"));
280 MmcHostInstance->CardInfo.CardType = MMC_CARD;
281
282 Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, 0x800000);
283 if (!EFI_ERROR(Status)) {
284 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR,Response);
285 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];
286 }
287 }
288
289 if (!EFI_ERROR(Status)) {
290 if (MmcHostInstance->CardInfo.OCRData.Busy == 0) {
291 MicroSecondDelay(1);
292 Timeout--;
293 } else {
294 if ((MmcHostInstance->CardInfo.CardType == SD_CARD_2) && (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1)) {
295 MmcHostInstance->CardInfo.CardType = SD_CARD_2_HIGH;
296 DEBUG ((EFI_D_ERROR, "High capacity card.\n"));
297 }
298 break; // The MMC/SD card is ready. Continue the Identification Mode
299 }
300 } else {
301 MicroSecondDelay(1);
302 Timeout--;
303 }
304 }
305
306 if (Timeout == 0) {
307 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(): No Card\n"));
308 return EFI_NO_MEDIA;
309 } else {
310 PrintOCR(Response[0]);
311 }
312
313 Status = MmcNotifyState (MmcHostInstance, MmcReadyState);
314 if (EFI_ERROR(Status)) {
315 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcReadyState\n"));
316 return Status;
317 }
318
319 Status = MmcHost->SendCommand (MmcHost, MMC_CMD2, 0);
320 if (EFI_ERROR(Status)) {
321 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD2): Error\n"));
322 return Status;
323 }
324 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CID,Response);
325 PrintCID(Response);
326
327 Status = MmcNotifyState (MmcHostInstance, MmcIdentificationState);
328 if (EFI_ERROR(Status)) {
329 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdentificationState\n"));
330 return Status;
331 }
332
333 //
334 // Note, SD specifications say that "if the command execution causes a state change, it
335 // will be visible to the host in the response to the next command"
336 // The status returned for this CMD3 will be 2 - identification
337 //
338 CmdArg = 1;
339 Status = MmcHost->SendCommand (MmcHost, MMC_CMD3, CmdArg);
340 if (EFI_ERROR(Status)) {
341 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n"));
342 return Status;
343 }
344
345 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_RCA,Response);
346 PrintRCA(Response[0]);
347
348 // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card
349 if (MmcHostInstance->CardInfo.CardType != MMC_CARD) {
350 MmcHostInstance->CardInfo.RCA = Response[0] >> 16;
351 } else {
352 MmcHostInstance->CardInfo.RCA = CmdArg;
353 }
354
355 Status = MmcNotifyState (MmcHostInstance, MmcStandByState);
356 if (EFI_ERROR(Status)) {
357 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcStandByState\n"));
358 return Status;
359 }
360
361 return EFI_SUCCESS;
362 }
363
364 EFI_STATUS InitializeMmcDevice (
365 IN MMC_HOST_INSTANCE *MmcHostInstance
366 )
367 {
368 UINT32 Response[4];
369 EFI_STATUS Status;
370 UINTN CardSize, NumBlocks, BlockSize, CmdArg;
371 EFI_MMC_HOST_PROTOCOL *MmcHost;
372 UINTN BlockCount = 1;
373
374 MmcHost = MmcHostInstance->MmcHost;
375
376 MmcIdentificationMode (MmcHostInstance);
377
378 //Send a command to get Card specific data
379 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
380 Status = MmcHost->SendCommand (MmcHost, MMC_CMD9, CmdArg);
381 if (EFI_ERROR(Status)) {
382 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD9): Error, Status=%r\n", Status));
383 return Status;
384 }
385 //Read Response
386 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CSD,Response);
387 PrintCSD(Response);
388
389 if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) {
390 CardSize = HC_MMC_CSD_GET_DEVICESIZE(Response);
391 NumBlocks = ((CardSize + 1) * 1024);
392 BlockSize = 1 << MMC_CSD_GET_READBLLEN(Response);
393 } else {
394 CardSize = MMC_CSD_GET_DEVICESIZE(Response);
395 NumBlocks = (CardSize + 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT(Response) + 2));
396 BlockSize = 1 << MMC_CSD_GET_READBLLEN(Response);
397 }
398
399 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
400 if (BlockSize > 512) {
401 NumBlocks = MultU64x32(NumBlocks, BlockSize/512);
402 BlockSize = 512;
403 }
404
405 MmcHostInstance->BlockIo.Media->LastBlock = (NumBlocks - 1);
406 MmcHostInstance->BlockIo.Media->BlockSize = BlockSize;
407 MmcHostInstance->BlockIo.Media->ReadOnly = MmcHost->IsReadOnly (MmcHost);
408 MmcHostInstance->BlockIo.Media->MediaPresent = TRUE;
409 MmcHostInstance->BlockIo.Media->MediaId++;
410
411 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
412 Status = MmcHost->SendCommand (MmcHost, MMC_CMD7, CmdArg);
413 if (EFI_ERROR(Status)) {
414 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD7): Error and Status = %r\n", Status));
415 return Status;
416 }
417
418 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);
419 if (EFI_ERROR(Status)) {
420 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n"));
421 return Status;
422 }
423
424 // Set Block Length
425 Status = MmcHost->SendCommand (MmcHost, MMC_CMD16, MmcHostInstance->BlockIo.Media->BlockSize);
426 if (EFI_ERROR(Status)) {
427 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD16): Error MmcHostInstance->BlockIo.Media->BlockSize: %d and Error = %r\n",MmcHostInstance->BlockIo.Media->BlockSize, Status));
428 return Status;
429 }
430
431 // Block Count (not used). Could return an error for SD card
432 if (MmcHostInstance->CardInfo.CardType == MMC_CARD) {
433 MmcHost->SendCommand (MmcHost, MMC_CMD23, BlockCount);
434 }
435
436 return EFI_SUCCESS;
437 }
438
439 EFI_STATUS
440 EFIAPI
441 MmcReset (
442 IN EFI_BLOCK_IO_PROTOCOL *This,
443 IN BOOLEAN ExtendedVerification
444 )
445 {
446 MMC_HOST_INSTANCE *MmcHostInstance;
447
448 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS(This);
449
450 if (MmcHostInstance->MmcHost == NULL) {
451 // Nothing to do
452 return EFI_SUCCESS;
453 }
454
455 // If a card is not present then clear all media settings
456 if (!MmcHostInstance->MmcHost->IsCardPresent (MmcHostInstance->MmcHost)) {
457 MmcHostInstance->BlockIo.Media->MediaPresent = FALSE;
458 MmcHostInstance->BlockIo.Media->LastBlock = 0;
459 MmcHostInstance->BlockIo.Media->BlockSize = 512; // Should be zero but there is a bug in DiskIo
460 MmcHostInstance->BlockIo.Media->ReadOnly = FALSE;
461
462 // Indicate that the driver requires initialization
463 MmcHostInstance->State = MmcHwInitializationState;
464
465 return EFI_SUCCESS;
466 }
467
468 // Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn
469 // on power and restart Identification mode
470 return EFI_SUCCESS;
471 }
472
473 EFI_STATUS
474 MmcDetectCard (
475 EFI_MMC_HOST_PROTOCOL *MmcHost
476 )
477 {
478 if (!MmcHost->IsCardPresent (MmcHost)) {
479 return EFI_NO_MEDIA;
480 } else {
481 return EFI_SUCCESS;
482 }
483 }
484
485 #define MMCI0_BLOCKLEN 512
486 #define MMCI0_TIMEOUT 10000
487
488 EFI_STATUS
489 MmcIoBlocks (
490 IN EFI_BLOCK_IO_PROTOCOL *This,
491 IN UINTN Transfer,
492 IN UINT32 MediaId,
493 IN EFI_LBA Lba,
494 IN UINTN BufferSize,
495 OUT VOID *Buffer
496 )
497 {
498 UINT32 Response[4];
499 EFI_STATUS Status;
500 UINTN CmdArg;
501 INTN Timeout;
502 UINTN Cmd;
503 MMC_HOST_INSTANCE *MmcHostInstance;
504 EFI_MMC_HOST_PROTOCOL *MmcHost;
505 UINTN BytesRemainingToBeTransfered;
506 UINTN BlockCount = 1;
507
508 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS(This);
509 ASSERT(MmcHostInstance != 0);
510 MmcHost = MmcHostInstance->MmcHost;
511 ASSERT(MmcHost);
512
513 if ((MmcHost == 0)|| (Buffer == NULL)) {
514 return EFI_INVALID_PARAMETER;
515 }
516
517 // Check if a Card is Present
518 if (!MmcHostInstance->BlockIo.Media->MediaPresent) {
519 return EFI_NO_MEDIA;
520 }
521
522 // All blocks must be within the device
523 if ((Lba + (BufferSize / This->Media->BlockSize)) > (This->Media->LastBlock + 1)){
524 return EFI_INVALID_PARAMETER;
525 }
526
527 // The buffer size must not be zero and it must be an exact multiple of the block size
528 if ((BufferSize == 0) || ((BufferSize % This->Media->BlockSize) != 0)) {
529 return EFI_BAD_BUFFER_SIZE;
530 }
531
532 if (This->Media->MediaId != MediaId) {
533 return EFI_MEDIA_CHANGED;
534 }
535
536 if((Transfer == MMC_IOBLOCKS_WRITE) && (This->Media->ReadOnly == TRUE)) {
537 return EFI_WRITE_PROTECTED;
538 }
539
540 BytesRemainingToBeTransfered = BufferSize;
541 while (BytesRemainingToBeTransfered > 0) {
542
543 // Check if the Card is in Ready status
544 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
545 Response[0] = 0;
546 Timeout = 20;
547 while(!(Response[0] & MMC_R0_READY_FOR_DATA) && (MMC_R0_CURRENTSTATE(Response) != MMC_R0_STATE_TRAN) && Timeout--) {
548 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
549 if (!EFI_ERROR(Status)) {
550 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1,Response);
551 }
552 }
553
554 if (0 == Timeout) {
555 DEBUG((EFI_D_ERROR, "The Card is busy\n"));
556 return EFI_NOT_READY;
557 }
558
559 //Set command argument based on the card access mode (Byte mode or Block mode)
560 if (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1) {
561 CmdArg = Lba;
562 } else {
563 CmdArg = Lba * This->Media->BlockSize;
564 }
565
566 if (Transfer == MMC_IOBLOCKS_READ) {
567 #ifndef USE_STREAM
568 // Read a single block
569 Cmd = MMC_CMD17;
570 #else
571 //TODO: Should we support read stream (MMC_CMD11)
572 #endif
573 } else {
574 #ifndef USE_STREAM
575 // Write a single block
576 Cmd = MMC_CMD24;
577 #else
578 //TODO: Should we support write stream (MMC_CMD20)
579 #endif
580 }
581 Status = MmcHost->SendCommand (MmcHost, Cmd, CmdArg);
582 if (EFI_ERROR(Status)) {
583 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD%d): Error %r\n",Cmd, Status));
584 return Status;
585 }
586
587 if (Transfer == MMC_IOBLOCKS_READ) {
588 #ifndef USE_STREAM
589 // Read one block of Data
590 Status = MmcHost->ReadBlockData (MmcHost, Lba,This->Media->BlockSize,Buffer);
591 if (EFI_ERROR(Status)) {
592 DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Read Block Data and Status = %r\n", Status));
593 return Status;
594 }
595 #else
596 //TODO: Read a steam
597 ASSERT(0);
598 #endif
599 Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState);
600 if (EFI_ERROR(Status)) {
601 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcProgrammingState\n"));
602 return Status;
603 }
604 } else {
605 #ifndef USE_STREAM
606 // Write one block of Data
607 Status = MmcHost->WriteBlockData (MmcHost, Lba,This->Media->BlockSize,Buffer);
608 if (EFI_ERROR(Status)) {
609 DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Write Block Data and Status = %r\n", Status));
610 return Status;
611 }
612 #else
613 //TODO: Write a steam
614 ASSERT(0);
615 #endif
616 }
617
618 // Command 12 - Stop transmission (ends read)
619 Status = MmcHost->SendCommand (MmcHost, MMC_CMD12, 0);
620 if (!EFI_ERROR(Status)) {
621 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1b,Response);
622 }
623
624 // Command 13 - Read status and wait for programming to complete (return to tran)
625 Timeout = MMCI0_TIMEOUT;
626 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
627 Response[0] = 0;
628 while(!(Response[0] & MMC_R0_READY_FOR_DATA) && (MMC_R0_CURRENTSTATE(Response) != MMC_R0_STATE_TRAN) && Timeout--) {
629 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);
630 if (!EFI_ERROR(Status)) {
631 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1,Response);
632 }
633 NanoSecondDelay(100);
634 Timeout--;
635 }
636
637 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);
638 if (EFI_ERROR(Status)) {
639 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n"));
640 return Status;
641 }
642
643 BytesRemainingToBeTransfered -= This->Media->BlockSize;
644 Lba += BlockCount;
645 Buffer = (UINT8 *)Buffer + This->Media->BlockSize;
646 }
647
648 return EFI_SUCCESS;
649 }
650
651 EFI_STATUS
652 EFIAPI
653 MmcReadBlocks (
654 IN EFI_BLOCK_IO_PROTOCOL *This,
655 IN UINT32 MediaId,
656 IN EFI_LBA Lba,
657 IN UINTN BufferSize,
658 OUT VOID *Buffer
659 )
660 {
661 return MmcIoBlocks (This, MMC_IOBLOCKS_READ, MediaId, Lba, BufferSize, Buffer);
662 }
663
664 EFI_STATUS
665 EFIAPI
666 MmcWriteBlocks (
667 IN EFI_BLOCK_IO_PROTOCOL *This,
668 IN UINT32 MediaId,
669 IN EFI_LBA Lba,
670 IN UINTN BufferSize,
671 IN VOID *Buffer
672 )
673 {
674 return MmcIoBlocks (This, MMC_IOBLOCKS_WRITE, MediaId, Lba, BufferSize, Buffer);
675 }
676
677 EFI_STATUS
678 EFIAPI
679 MmcFlushBlocks (
680 IN EFI_BLOCK_IO_PROTOCOL *This
681 )
682 {
683 return EFI_SUCCESS;
684 }
685