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