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