EmbeddedPkg/MmcDxe: Fix protocols uninstallation in Stop() function
[mirror_edk2.git] / ArmPkg / Universal / MmcDxe / MmcBlockIo.c
CommitLineData
1bfda055 1/** @file\r
2*\r
3* Copyright (c) 2011, ARM Limited. All rights reserved.\r
4* \r
5* This program and the accompanying materials \r
6* are licensed and made available under the terms and conditions of the BSD License \r
7* which accompanies this distribution. The full text of the license may be found at \r
8* http://opensource.org/licenses/bsd-license.php \r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12*\r
13**/\r
14\r
15#include <Protocol/MmcHost.h>\r
16#include <Library/DebugLib.h>\r
17#include <Library/BaseMemoryLib.h>\r
18#include <Library/TimerLib.h>\r
19\r
20#include "Mmc.h"\r
21\r
22// Untested ...\r
23//#define USE_STREAM\r
24\r
25#define MAX_RETRY_COUNT 200\r
26\r
27EFI_STATUS\r
28MmcNotifyState (\r
29 MMC_HOST_INSTANCE *MmcHostInstance,\r
30 MMC_STATE State\r
31 ) {\r
32 MmcHostInstance->State = State;\r
33 return MmcHostInstance->MmcHost->NotifyState(State);\r
34}\r
35\r
36VOID PrintOCR(UINT32 ocr) {\r
507ebc1a 37 UINTN minv, maxv, volts;\r
38 UINTN loop;\r
1bfda055 39\r
507ebc1a 40 minv = 36; // 3.6\r
41 maxv = 20; // 2.0\r
42 volts = 20; // 2.0\r
1bfda055 43\r
44 // The MMC register bits [23:8] indicate the working range of the card\r
45 for (loop = 8; loop < 24; loop++) {\r
46 if (ocr & (1 << loop)) {\r
47 if (minv > volts) minv = volts;\r
507ebc1a 48 if (maxv < volts) maxv = volts + 1;\r
1bfda055 49 }\r
507ebc1a 50 volts = volts + 1;\r
1bfda055 51 }\r
52\r
53 DEBUG((EFI_D_ERROR, "- PrintOCR ocr (0x%X)\n",ocr));\r
507ebc1a 54 DEBUG((EFI_D_ERROR, "\t- Card operating voltage: %d.%d to %d.%d\n", minv/10, minv % 10, maxv/10, maxv % 10));\r
1bfda055 55 if (((ocr >> 29) & 3) == 0)\r
56 DEBUG((EFI_D_ERROR, "\t- AccessMode: Byte Mode\n"));\r
57 else\r
58 DEBUG((EFI_D_ERROR, "\t- AccessMode: Block Mode (0x%X)\n",((ocr >> 29) & 3)));\r
59\r
60 if (ocr & MMC_OCR_POWERUP)\r
61 DEBUG((EFI_D_ERROR, "\t- PowerUp\n"));\r
62 else\r
63 DEBUG((EFI_D_ERROR, "\t- Voltage Not Supported\n"));\r
64}\r
65\r
66VOID PrintCID(UINT32* cid) {\r
67 DEBUG((EFI_D_ERROR, "- PrintCID\n"));\r
68 DEBUG((EFI_D_ERROR, "\t- Manufacturing date: %d/%d\n",(cid[0] >> 8) & 0xF,(cid[0] >> 12) & 0xFF));\r
69 DEBUG((EFI_D_ERROR, "\t- Product serial number: 0x%X%X\n",cid[1] & 0xFFFFFF,(cid[0] >> 24) & 0xFF));\r
70 DEBUG((EFI_D_ERROR, "\t- Product revision: %d\n",cid[1] >> 24));\r
71 //DEBUG((EFI_D_ERROR, "\t- Product name: %s\n",(char*)(cid + 2)));\r
72 DEBUG((EFI_D_ERROR, "\t- OEM ID: %c%c\n",(cid[3] >> 8) & 0xFF,(cid[3] >> 16) & 0xFF));\r
73}\r
74\r
75VOID PrintCSD(UINT32* csd) {\r
76 UINTN val32;\r
77 CONST CHAR8* str_unit[] = { "100kbit/s","1Mbit/s","10Mbit/s","100MBit/s","Unkbown","Unkbown","Unkbown","Unkbown" };\r
78 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" };\r
79\r
80 if (((csd[2] >> 30) & 0x3) == 0)\r
81 DEBUG((EFI_D_ERROR, "- PrintCSD Version 1.01-1.10/Version 2.00/Standard Capacity\n"));\r
82 else if (((csd[2] >> 30) & 0x3) == 1)\r
83 DEBUG((EFI_D_ERROR, "- PrintCSD Version 2.00/High Capacity\n"));\r
84 else\r
85 DEBUG((EFI_D_ERROR, "- PrintCSD Version Higher than v3.3\n"));\r
86\r
87 DEBUG((EFI_D_ERROR, "\t- Supported card command class: 0x%X\n",MMC_CSD_GET_CCC(csd)));\r
88 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]));\r
89 DEBUG((EFI_D_ERROR, "\t- Maximum Read Data Block: %d\n",2 << (MMC_CSD_GET_READBLLEN(csd)-1)));\r
90 DEBUG((EFI_D_ERROR, "\t- Maximum Write Data Block: %d\n",2 << (MMC_CSD_GET_WRITEBLLEN(csd)-1)));\r
91 \r
92 if (!MMC_CSD_GET_FILEFORMATGRP(csd)) {\r
93 val32 = MMC_CSD_GET_FILEFORMAT(csd);\r
94 if (val32 == 0) DEBUG((EFI_D_ERROR, "\t- Format(0): Hard disk-like file system with partition table\n"));\r
95 else if (val32 == 1) DEBUG((EFI_D_ERROR, "\t- Format(1): DOS FAT (floppy-like) with boot sector only (no partition table)\n"));\r
96 else if (val32 == 2) DEBUG((EFI_D_ERROR, "\t- Format(2): Universal File Format\n"));\r
97 else DEBUG((EFI_D_ERROR, "\t- Format(3): Others/Unknown\n"));\r
98 } else {\r
99 DEBUG((EFI_D_ERROR, "\t- Format: Reserved\n"));\r
100 }\r
101}\r
102\r
103VOID PrintRCA(UINT32 rca) {\r
104 DEBUG((EFI_D_ERROR, "- PrintRCA: 0x%X\n",rca));\r
105 DEBUG((EFI_D_ERROR, "\t- Status: 0x%X\n",rca & 0xFFFF));\r
106 DEBUG((EFI_D_ERROR, "\t- RCA: 0x%X\n",(rca >> 16) & 0xFFFF));\r
107}\r
108\r
109VOID PrintResponseR1(UINT32 response) {\r
110 DEBUG((EFI_D_ERROR, "Response: 0x%X\n",response));\r
111 if (response & (1 << 8)) DEBUG((EFI_D_ERROR, "\t- READY_FOR_DATA\n"));\r
112\r
113 if (((response >> 9) & 0xF) == 0) DEBUG((EFI_D_ERROR, "\t- State: Idle\n"));\r
114 else if (((response >> 9) & 0xF) == 1) DEBUG((EFI_D_ERROR, "\t- State: Ready\n"));\r
115 else if (((response >> 9) & 0xF) == 2) DEBUG((EFI_D_ERROR, "\t- State: Ident\n"));\r
116 else if (((response >> 9) & 0xF) == 3) DEBUG((EFI_D_ERROR, "\t- State: StandBy\n"));\r
117 else if (((response >> 9) & 0xF) == 4) DEBUG((EFI_D_ERROR, "\t- State: Tran\n"));\r
118 else if (((response >> 9) & 0xF) == 5) DEBUG((EFI_D_ERROR, "\t- State: Data\n"));\r
119 else if (((response >> 9) & 0xF) == 6) DEBUG((EFI_D_ERROR, "\t- State: Rcv\n"));\r
120 else if (((response >> 9) & 0xF) == 7) DEBUG((EFI_D_ERROR, "\t- State: Prg\n"));\r
121 else if (((response >> 9) & 0xF) == 8) DEBUG((EFI_D_ERROR, "\t- State: Dis\n"));\r
122 else DEBUG((EFI_D_ERROR, "\t- State: Reserved\n"));\r
123}\r
124\r
125EFI_STATUS\r
126EFIAPI\r
127MmcIdentificationMode (\r
128 MMC_HOST_INSTANCE *MmcHostInstance\r
129 ) {\r
130 EFI_STATUS Status;\r
131 UINT32 Response[4];\r
132 UINTN timer;\r
133 UINTN CmdArg;\r
134 BOOLEAN bHCS;\r
135 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
136 \r
137 MmcHost = MmcHostInstance->MmcHost;\r
138 CmdArg = 0;\r
139 bHCS = FALSE;\r
140\r
141 if (MmcHost == NULL) {\r
142 return EFI_INVALID_PARAMETER;\r
143 }\r
144\r
145 // We can get into this function if we restart the identification mode\r
146 if (MmcHostInstance->State == MmcHwInitializationState) {\r
147 // Initialize the MMC Host HW\r
148 Status = MmcNotifyState (MmcHostInstance, MmcHwInitializationState);\r
149 if (EFI_ERROR(Status)) {\r
150 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcHwInitializationState\n"));\r
151 return Status;\r
152 }\r
153 } else {\r
154 //Note: Could even be used in all cases. But it looks this command could put the state machine into inactive for some cards\r
155 Status = MmcHost->SendCommand(MMC_CMD0, 0);\r
156 if (EFI_ERROR(Status)) {\r
157 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD0): Error\n"));\r
158 return Status;\r
159 }\r
160 }\r
161\r
162 Status = MmcNotifyState (MmcHostInstance, MmcIdleState);\r
163 if (EFI_ERROR(Status)) {\r
164 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdleState\n"));\r
165 return Status;\r
166 }\r
167\r
168 // Are we using SDIO ?\r
169 Status = MmcHost->SendCommand(MMC_CMD5, 0);\r
170 if (Status == EFI_SUCCESS) {\r
171 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported.\n"));\r
172 return EFI_UNSUPPORTED;\r
173 }\r
174\r
175 // Check which kind of card we are using. Ver2.00 or later SD Memory Card (PL180 is SD v1.1)\r
176 CmdArg = (0x0UL << 12 | BIT8 | 0xCEUL << 0);\r
177 Status = MmcHost->SendCommand(MMC_CMD8, CmdArg);\r
178 if (Status == EFI_SUCCESS) {\r
179 DEBUG ((EFI_D_ERROR, "Card is SD2.0 => Supports high capacity\n"));\r
180 bHCS = TRUE;\r
181 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R7,Response);\r
182 PrintResponseR1(Response[0]);\r
183 } else {\r
184 DEBUG ((EFI_D_ERROR, "Not a SD2.0 Card\n"));\r
185 }\r
186\r
187 // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.Busy == 1)\r
188 timer = MAX_RETRY_COUNT;\r
189 while (timer > 0) {\r
190 // SD Card or MMC Card ? CMD55 indicates to the card that the next command is an application specific command\r
191 Status = MmcHost->SendCommand(MMC_CMD55, 0);\r
192 if (Status == EFI_SUCCESS) {\r
193 DEBUG ((EFI_D_INFO, "Card should be SD\n"));\r
194 if (bHCS) {\r
195 MmcHostInstance->CardInfo.CardType = SD_CARD_2;\r
196 } else {\r
197 MmcHostInstance->CardInfo.CardType = SD_CARD;\r
198 }\r
199\r
200 // Note: The first time CmdArg will be zero\r
201 CmdArg = ((UINTN *) &(MmcHostInstance->CardInfo.OCRData))[0];\r
202 if (bHCS) {\r
203 CmdArg |= BIT30;\r
204 }\r
205 Status = MmcHost->SendCommand(MMC_ACMD41, CmdArg);\r
206 if (EFI_ERROR(Status)) {\r
207 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(ACMD41): Error\n"));\r
208 return Status;\r
209 }\r
210 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_OCR,Response);\r
211 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];\r
212 } else {\r
213 DEBUG ((EFI_D_INFO, "Card should be MMC\n"));\r
214 MmcHostInstance->CardInfo.CardType = MMC_CARD;\r
215\r
216 Status = MmcHost->SendCommand(MMC_CMD1, 0x800000);\r
217 if (EFI_ERROR(Status)) {\r
218 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(ACMD41): Error\n"));\r
219 return Status;\r
220 }\r
221 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_OCR,Response);\r
222 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];\r
223 }\r
224\r
225 if (MmcHostInstance->CardInfo.OCRData.Busy == 0) {\r
226 MicroSecondDelay(10*1000);\r
227 timer--;\r
228 } else {\r
229 if ((MmcHostInstance->CardInfo.CardType == SD_CARD_2) && (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1)) {\r
230 MmcHostInstance->CardInfo.CardType = SD_CARD_2_HIGH;\r
231 DEBUG ((EFI_D_ERROR, "High capacity card.\n"));\r
232 }\r
233 break; // The MMC/SD card is ready. Continue the Identification Mode\r
234 }\r
235 }\r
236\r
237 if (timer == 0) {\r
238 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(): No Card\n"));\r
239 ASSERT(0);\r
240 return EFI_NO_MEDIA;\r
241 } else {\r
242 PrintOCR(Response[0]);\r
243 }\r
244\r
245 Status = MmcNotifyState (MmcHostInstance, MmcReadyState);\r
246 if (EFI_ERROR(Status)) {\r
247 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcReadyState\n"));\r
248 return Status;\r
249 }\r
250\r
251 Status = MmcHost->SendCommand(MMC_CMD2, 0);\r
252 if (EFI_ERROR(Status)) {\r
253 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD2): Error\n"));\r
254 ASSERT(0);\r
255 return Status;\r
256 }\r
257 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_CID,Response);\r
258 PrintCID(Response);\r
259\r
260 Status = MmcNotifyState (MmcHostInstance, MmcIdentificationState);\r
261 if (EFI_ERROR(Status)) {\r
262 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdentificationState\n"));\r
263 return Status;\r
264 }\r
265\r
266 CmdArg = 0;\r
267 Status = MmcHost->SendCommand(MMC_CMD3, CmdArg);\r
268 if (EFI_ERROR(Status)) {\r
269 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n"));\r
270 return Status;\r
271 }\r
272 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_RCA,Response);\r
273 PrintRCA(Response[0]);\r
274\r
275 // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card\r
276 if (MmcHostInstance->CardInfo.CardType != MMC_CARD) {\r
277 MmcHostInstance->CardInfo.RCA = Response[0] >> 16;\r
278 } else {\r
279 MmcHostInstance->CardInfo.RCA = CmdArg;\r
280 }\r
281\r
282 Status = MmcNotifyState (MmcHostInstance, MmcStandByState);\r
283 if (EFI_ERROR(Status)) {\r
284 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcStandByState\n"));\r
285 return Status;\r
286 }\r
287\r
288 return EFI_SUCCESS;\r
289}\r
290\r
291EFI_STATUS\r
292EFIAPI\r
293MmcReset (\r
294 IN EFI_BLOCK_IO_PROTOCOL *This,\r
295 IN BOOLEAN ExtendedVerification\r
296 ) {\r
297 // Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn\r
298 // on power and restart Identification mode\r
299 return EFI_SUCCESS;\r
300}\r
301\r
302EFI_STATUS\r
303MmcDetectCard (\r
304 EFI_MMC_HOST_PROTOCOL *MmcHost\r
305 )\r
306{\r
307 if (!MmcHost->IsCardPresent()) {\r
308 return EFI_NO_MEDIA;\r
309 } else {\r
310 return EFI_SUCCESS;\r
311 }\r
312}\r
313\r
314#define MMCI0_BLOCKLEN 512\r
315#define MMCI0_TIMEOUT 10000\r
316\r
317EFI_STATUS MmcIoBlocks (\r
318 IN EFI_BLOCK_IO_PROTOCOL *This,\r
319 IN UINTN Transfer,\r
320 IN UINT32 MediaId,\r
321 IN EFI_LBA Lba,\r
322 IN UINTN BufferSize,\r
323 OUT VOID *Buffer\r
324 ) {\r
325 UINT32 Response[4];\r
326 EFI_STATUS Status;\r
327 UINTN CardSize, NumBlocks, BlockSize, CmdArg;\r
328 UINTN timer;\r
329 UINTN Cmd;\r
330 MMC_HOST_INSTANCE *MmcHostInstance;\r
331 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
332 UINTN BytesRemainingToBeTransfered;\r
333 UINTN BlockCount = 1;\r
334\r
335 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS(This);\r
336 ASSERT(MmcHostInstance != 0);\r
337 MmcHost = MmcHostInstance->MmcHost;\r
338 ASSERT(MmcHost);\r
339\r
340 if (MmcHost == 0) {\r
341 return EFI_INVALID_PARAMETER;\r
342 }\r
343\r
344 // Check if a Card is Present\r
345 if (!MmcHost->IsCardPresent()) {\r
346 MmcHostInstance->BlockIo.Media->MediaPresent = FALSE;\r
347 MmcHostInstance->BlockIo.Media->LastBlock = 0;\r
348 MmcHostInstance->BlockIo.Media->BlockSize = 512; // Should be zero but there is a bug in DiskIo\r
349 MmcHostInstance->BlockIo.Media->ReadOnly = FALSE; \r
350 return EFI_NO_MEDIA;\r
351 }\r
352\r
353 // If the driver has not been initialized yet then go into Iddentification Mode\r
354 if (MmcHostInstance->State == MmcHwInitializationState) {\r
355 MmcIdentificationMode (MmcHostInstance);\r
356\r
357 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
358 Status = MmcHost->SendCommand(MMC_CMD9, CmdArg);\r
359 if (EFI_ERROR(Status)) {\r
360 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD9): Error\n"));\r
361 ASSERT(0);\r
362 return Status;\r
363 }\r
364 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_CSD,Response);\r
365 PrintCSD(Response);\r
366\r
367\r
368 if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) {\r
369 ASSERT(0); //TODO: Implementation needed\r
370 CardSize = MMC_CSD_GET_DEVICESIZE(Response);\r
371 NumBlocks = ((CardSize + 1) * 1024);;\r
372 BlockSize = 1 << MMC_CSD_GET_READBLLEN(Response);\r
373 } else {\r
374 CardSize = MMC_CSD_GET_DEVICESIZE(Response);\r
375 NumBlocks = (CardSize + 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT(Response) + 2));\r
376 BlockSize = 1 << MMC_CSD_GET_READBLLEN(Response);\r
377 }\r
378\r
379 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.\r
380 if (BlockSize > 512) {\r
381 NumBlocks = MultU64x32(NumBlocks, BlockSize/512);\r
382 BlockSize = 512; \r
383 }\r
384\r
385 MmcHostInstance->BlockIo.Media->LastBlock = (NumBlocks - 1);\r
386 MmcHostInstance->BlockIo.Media->BlockSize = BlockSize;\r
387 MmcHostInstance->BlockIo.Media->ReadOnly = MmcHost->IsReadOnly();\r
388 MmcHostInstance->BlockIo.Media->MediaPresent = TRUE; \r
389 MmcHostInstance->BlockIo.Media->MediaId++; \r
390\r
391 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
392 Status = MmcHost->SendCommand(MMC_CMD7, CmdArg);\r
393 if (EFI_ERROR(Status)) {\r
394 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD7): Error\n"));\r
395 ASSERT(0);\r
396 return Status;\r
397 }\r
398 \r
399 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);\r
400 if (EFI_ERROR(Status)) {\r
401 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n"));\r
402 return Status;\r
403 }\r
404 } else {\r
405 // Maybe test if the card has changed to update gMmcMedia information\r
406 if (MmcHostInstance->State == MmcTransferState) {\r
407 //DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : MmcTransferState\n"));\r
408 } else if (MmcHostInstance->State == MmcStandByState) {\r
409 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : MmcStandByState\n"));\r
410 } else {\r
411 ASSERT(0);\r
412 }\r
413 }\r
414\r
415 if (Lba > This->Media->LastBlock) {\r
416 ASSERT(0);\r
417 return EFI_INVALID_PARAMETER;\r
418 }\r
419 \r
420 if ((BufferSize % This->Media->BlockSize) != 0) {\r
421 ASSERT(0);\r
422 return EFI_BAD_BUFFER_SIZE;\r
423 }\r
424\r
425 BytesRemainingToBeTransfered = BufferSize;\r
426 while (BytesRemainingToBeTransfered > 0) {\r
427 // Set Block Length\r
428 Status = MmcHost->SendCommand(MMC_CMD16, This->Media->BlockSize);\r
429 if (EFI_ERROR(Status)) {\r
430 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD16): Error This->Media->BlockSize:%d\n",This->Media->BlockSize));\r
431 ASSERT(0);\r
432 return Status;\r
433 }\r
434\r
435 // Block Count (not used). Could return an error for SD card\r
436 MmcHost->SendCommand(MMC_CMD23, BlockCount);\r
437\r
438 //Set command argument based on the card access mode (Byte mode or Block mode)\r
439 if (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1) {\r
440 CmdArg = Lba;\r
441 } else {\r
442 CmdArg = Lba * This->Media->BlockSize;\r
443 }\r
444\r
445 if (Transfer == MMC_IOBLOCKS_READ) {\r
446#ifndef USE_STREAM\r
447 // Read a single block\r
448 Cmd = MMC_CMD17;\r
449#else\r
450 //TODO: Should we support read stream (MMC_CMD11)\r
451#endif\r
452 } else {\r
453#ifndef USE_STREAM\r
454 // Write a single block\r
455 Cmd = MMC_CMD24;\r
456#else\r
457 //TODO: Should we support write stream (MMC_CMD20)\r
458#endif\r
459 }\r
460 Status = MmcHost->SendCommand(Cmd, CmdArg);\r
461 if (EFI_ERROR(Status)) {\r
462 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD%d): Error\n",Cmd));\r
463 ASSERT(0);\r
464 return Status;\r
465 }\r
466\r
467 if (Transfer == MMC_IOBLOCKS_READ) {\r
468#ifndef USE_STREAM\r
469 // Read one block of Data\r
470 Status = MmcHost->ReadBlockData(Lba,This->Media->BlockSize,Buffer);\r
471 if (EFI_ERROR(Status)) {\r
472 DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Read Block Data"));\r
473 ASSERT(0);\r
474 return Status;\r
475 }\r
476#else\r
477 //TODO: Read a steam\r
478 ASSERT(0);\r
479#endif\r
480 Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState);\r
481 if (EFI_ERROR(Status)) {\r
482 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcProgrammingState\n"));\r
483 return Status;\r
484 }\r
485 } else {\r
486#ifndef USE_STREAM\r
487 // Write one block of Data\r
488 Status = MmcHost->WriteBlockData(Lba,This->Media->BlockSize,Buffer);\r
489 if (EFI_ERROR(Status)) {\r
490 DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Write Block Data"));\r
491 ASSERT(0);\r
492 return Status;\r
493 }\r
494#else\r
495 //TODO: Write a steam\r
496 ASSERT(0);\r
497#endif\r
498 }\r
499\r
500 // Command 12 - Stop transmission (ends read)\r
501 Status = MmcHost->SendCommand(MMC_CMD12, 0);\r
502 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1b,Response);\r
503\r
504 // Command 13 - Read status and wait for programming to complete (return to tran)\r
505 timer = MMCI0_TIMEOUT;\r
506 while ((MMC_R0_CURRENTSTATE(Response) != MMC_R0_STATE_TRAN) && timer) {\r
507 MmcHost->SendCommand(MMC_CMD13, 0);\r
508 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response);\r
509 NanoSecondDelay(100);\r
510 timer--;\r
511 }\r
512\r
513 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);\r
514 if (EFI_ERROR(Status)) {\r
515 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n"));\r
516 return Status;\r
517 }\r
518\r
519 BytesRemainingToBeTransfered -= This->Media->BlockSize;\r
520 Lba += BlockCount;\r
521 Buffer = (UINT8 *)Buffer + This->Media->BlockSize;\r
522 }\r
523\r
524 return EFI_SUCCESS;\r
525}\r
526\r
527EFI_STATUS\r
528EFIAPI\r
529MmcReadBlocks (\r
530 IN EFI_BLOCK_IO_PROTOCOL *This,\r
531 IN UINT32 MediaId,\r
532 IN EFI_LBA Lba,\r
533 IN UINTN BufferSize,\r
534 OUT VOID *Buffer\r
535 ) {\r
536 return MmcIoBlocks (This, MMC_IOBLOCKS_READ, MediaId, Lba, BufferSize, Buffer);\r
537}\r
538\r
539EFI_STATUS\r
540EFIAPI\r
541MmcWriteBlocks (\r
542 IN EFI_BLOCK_IO_PROTOCOL *This,\r
543 IN UINT32 MediaId,\r
544 IN EFI_LBA Lba,\r
545 IN UINTN BufferSize,\r
546 IN VOID *Buffer\r
547 ) {\r
548 return MmcIoBlocks (This, MMC_IOBLOCKS_WRITE, MediaId, Lba, BufferSize, Buffer);\r
549}\r
550\r
551EFI_STATUS\r
552EFIAPI\r
553MmcFlushBlocks (\r
554 IN EFI_BLOCK_IO_PROTOCOL *This\r
555 ) {\r
556 return EFI_SUCCESS;\r
557}\r
558\r