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