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