EmbeddedPkg/MmcDxe: Fix the CMD3 sequence
[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
d8ad4736 336 //\r
337 // Note, SD specifications say that "if the command execution causes a state change, it\r
338 // will be visible to the host in the response to the next command"\r
339 // The status returned for this CMD3 will be 2 - identification\r
340 //\r
341 CmdArg = 1;\r
342 Status = MmcHost->SendCommand(MMC_CMD3, CmdArg);\r
343 if (EFI_ERROR(Status)) {\r
344 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n"));\r
345 return Status;\r
e8e95df4 346 }\r
347\r
d8ad4736 348 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_RCA,Response);\r
349 PrintRCA(Response[0]);\r
350\r
e8e95df4 351 // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card\r
352 if (MmcHostInstance->CardInfo.CardType != MMC_CARD) {\r
353 MmcHostInstance->CardInfo.RCA = Response[0] >> 16;\r
354 } else {\r
355 MmcHostInstance->CardInfo.RCA = CmdArg;\r
356 }\r
357\r
358 Status = MmcNotifyState (MmcHostInstance, MmcStandByState);\r
359 if (EFI_ERROR(Status)) {\r
360 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcStandByState\n"));\r
361 return Status;\r
362 }\r
363\r
364 return EFI_SUCCESS;\r
1bfda055 365}\r
366\r
367EFI_STATUS\r
368EFIAPI\r
369MmcReset (\r
370 IN EFI_BLOCK_IO_PROTOCOL *This,\r
371 IN BOOLEAN ExtendedVerification\r
e8e95df4 372 )\r
373{\r
3de99375 374 MMC_HOST_INSTANCE *MmcHostInstance;\r
375\r
376 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS(This);\r
377\r
378 if (MmcHostInstance->MmcHost == NULL) {\r
379 // Nothing to do\r
380 return EFI_SUCCESS;\r
381 }\r
382\r
383 // If a card is not present then clear all media settings\r
384 if (!MmcHostInstance->MmcHost->IsCardPresent()) {\r
385 MmcHostInstance->BlockIo.Media->MediaPresent = FALSE;\r
386 MmcHostInstance->BlockIo.Media->LastBlock = 0;\r
387 MmcHostInstance->BlockIo.Media->BlockSize = 512; // Should be zero but there is a bug in DiskIo\r
388 MmcHostInstance->BlockIo.Media->ReadOnly = FALSE;\r
389\r
390 // Indicate that the driver requires initialization\r
391 MmcHostInstance->State = MmcHwInitializationState;\r
392\r
393 return EFI_SUCCESS;\r
394 }\r
395\r
e8e95df4 396 // Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn\r
397 // on power and restart Identification mode\r
398 return EFI_SUCCESS;\r
1bfda055 399}\r
400\r
401EFI_STATUS\r
402MmcDetectCard (\r
403 EFI_MMC_HOST_PROTOCOL *MmcHost\r
404 )\r
405{\r
e8e95df4 406 if (!MmcHost->IsCardPresent()) {\r
407 return EFI_NO_MEDIA;\r
408 } else {\r
409 return EFI_SUCCESS;\r
410 }\r
1bfda055 411}\r
412\r
413#define MMCI0_BLOCKLEN 512\r
414#define MMCI0_TIMEOUT 10000\r
415\r
e8e95df4 416EFI_STATUS\r
417MmcIoBlocks (\r
1bfda055 418 IN EFI_BLOCK_IO_PROTOCOL *This,\r
419 IN UINTN Transfer,\r
420 IN UINT32 MediaId,\r
421 IN EFI_LBA Lba,\r
422 IN UINTN BufferSize,\r
423 OUT VOID *Buffer\r
e8e95df4 424 )\r
425{\r
426 UINT32 Response[4];\r
427 EFI_STATUS Status;\r
428 UINTN CardSize, NumBlocks, BlockSize, CmdArg;\r
969ece79 429 INTN Timeout;\r
e8e95df4 430 UINTN Cmd;\r
431 MMC_HOST_INSTANCE *MmcHostInstance;\r
432 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
433 UINTN BytesRemainingToBeTransfered;\r
434 UINTN BlockCount = 1;\r
435\r
436 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS(This);\r
437 ASSERT(MmcHostInstance != 0);\r
438 MmcHost = MmcHostInstance->MmcHost;\r
439 ASSERT(MmcHost);\r
440\r
969ece79 441 if ((MmcHost == 0)|| (Buffer == NULL)) {\r
e8e95df4 442 return EFI_INVALID_PARAMETER;\r
443 }\r
444\r
445 // Check if a Card is Present\r
3de99375 446 if (!MmcHostInstance->BlockIo.Media->MediaPresent) {\r
e8e95df4 447 return EFI_NO_MEDIA;\r
448 }\r
449\r
969ece79 450 // If the driver has not been initialized yet then go into Identification Mode\r
e8e95df4 451 if (MmcHostInstance->State == MmcHwInitializationState) {\r
452 MmcIdentificationMode (MmcHostInstance);\r
453\r
454 //Send a command to get Card specific data\r
455 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
456 Status = MmcHost->SendCommand(MMC_CMD9, CmdArg);\r
457 if (EFI_ERROR(Status)) {\r
458 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD9): Error, Status=%r\n", Status));\r
459 ASSERT(0);\r
460 return Status;\r
1bfda055 461 }\r
e8e95df4 462 //Read Response\r
463 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_CSD,Response);\r
464 PrintCSD(Response);\r
465\r
466 if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) {\r
467 ASSERT(0); //TODO: Implementation needed\r
468 CardSize = MMC_CSD_GET_DEVICESIZE(Response);\r
469 NumBlocks = ((CardSize + 1) * 1024);;\r
470 BlockSize = 1 << MMC_CSD_GET_READBLLEN(Response);\r
471 } else {\r
472 CardSize = MMC_CSD_GET_DEVICESIZE(Response);\r
473 NumBlocks = (CardSize + 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT(Response) + 2));\r
474 BlockSize = 1 << MMC_CSD_GET_READBLLEN(Response);\r
1bfda055 475 }\r
476\r
e8e95df4 477 //For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.\r
478 if (BlockSize > 512) {\r
479 NumBlocks = MultU64x32(NumBlocks, BlockSize/512);\r
480 BlockSize = 512;\r
481 }\r
1bfda055 482\r
e8e95df4 483 MmcHostInstance->BlockIo.Media->LastBlock = (NumBlocks - 1);\r
484 MmcHostInstance->BlockIo.Media->BlockSize = BlockSize;\r
485 MmcHostInstance->BlockIo.Media->ReadOnly = MmcHost->IsReadOnly();\r
486 MmcHostInstance->BlockIo.Media->MediaPresent = TRUE;\r
487 MmcHostInstance->BlockIo.Media->MediaId++;\r
a1ab9143 488\r
e8e95df4 489 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
490 Status = MmcHost->SendCommand(MMC_CMD7, CmdArg);\r
491 if (EFI_ERROR(Status)) {\r
492 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD7): Error and Status = %r\n", Status));\r
493 ASSERT(0);\r
494 return Status;\r
1bfda055 495 }\r
496\r
e8e95df4 497 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);\r
498 if (EFI_ERROR(Status)) {\r
499 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n"));\r
500 return Status;\r
501 }\r
502 } else {\r
503 // Maybe test if the card has changed to update gMmcMedia information\r
504 if (MmcHostInstance->State == MmcTransferState) {\r
505 //DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : MmcTransferState\n"));\r
506 } else if (MmcHostInstance->State == MmcStandByState) {\r
507 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : MmcStandByState\n"));\r
508 } else {\r
509 ASSERT(0);\r
1bfda055 510 }\r
e8e95df4 511 }\r
512\r
969ece79 513 // All blocks must be within the device\r
514 if ((Lba + (BufferSize / This->Media->BlockSize)) > (This->Media->LastBlock + 1)){\r
515 ASSERT(0);\r
516 return EFI_INVALID_PARAMETER;\r
e8e95df4 517 }\r
518\r
969ece79 519 // The buffer size must not be zero and it must be an exact multiple of the block size\r
520 if ((BufferSize == 0) || ((BufferSize % This->Media->BlockSize) != 0)) {\r
521 ASSERT(0);\r
522 return EFI_BAD_BUFFER_SIZE;\r
523 }\r
524\r
525 if (This->Media->MediaId != MediaId) {\r
526 return EFI_MEDIA_CHANGED;\r
527 }\r
528\r
529 if((Transfer == MMC_IOBLOCKS_WRITE) && (This->Media->ReadOnly == TRUE)) {\r
530 return EFI_WRITE_PROTECTED;\r
e8e95df4 531 }\r
532\r
533 BytesRemainingToBeTransfered = BufferSize;\r
534 while (BytesRemainingToBeTransfered > 0) {\r
535\r
536 // Check if the Card is in Ready status\r
537 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
538 Response[0] = 0;\r
539 Timeout = 20;\r
540 while(!(Response[0] & MMC_R0_READY_FOR_DATA) && (MMC_R0_CURRENTSTATE(Response) != MMC_R0_STATE_TRAN) && Timeout--) {\r
541 Status = MmcHost->SendCommand(MMC_CMD13, CmdArg);\r
542 if (!EFI_ERROR(Status)) {\r
543 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response);\r
544 }\r
1bfda055 545 }\r
546\r
e8e95df4 547 if (0 == Timeout) {\r
548 DEBUG((EFI_D_ERROR, "The Card is busy\n"));\r
549 return EFI_NOT_READY;\r
b9d5fe03 550 }\r
551\r
e8e95df4 552 // Set Block Length\r
553 Status = MmcHost->SendCommand(MMC_CMD16, This->Media->BlockSize);\r
554 if (EFI_ERROR(Status)) {\r
555 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD16): Error This->Media->BlockSize:%d and Error = %r\n",This->Media->BlockSize, Status));\r
556 return Status;\r
557 }\r
1bfda055 558\r
e8e95df4 559 // Block Count (not used). Could return an error for SD card\r
560 MmcHost->SendCommand(MMC_CMD23, BlockCount);\r
1bfda055 561\r
e8e95df4 562 //Set command argument based on the card access mode (Byte mode or Block mode)\r
563 if (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1) {\r
564 CmdArg = Lba;\r
565 } else {\r
566 CmdArg = Lba * This->Media->BlockSize;\r
567 }\r
1bfda055 568\r
e8e95df4 569 if (Transfer == MMC_IOBLOCKS_READ) {\r
1bfda055 570#ifndef USE_STREAM\r
e8e95df4 571 // Read a single block\r
572 Cmd = MMC_CMD17;\r
1bfda055 573#else\r
e8e95df4 574 //TODO: Should we support read stream (MMC_CMD11)\r
1bfda055 575#endif\r
e8e95df4 576 } else {\r
1bfda055 577#ifndef USE_STREAM\r
e8e95df4 578 // Write a single block\r
579 Cmd = MMC_CMD24;\r
1bfda055 580#else\r
e8e95df4 581 //TODO: Should we support write stream (MMC_CMD20)\r
1bfda055 582#endif\r
e8e95df4 583 }\r
584 Status = MmcHost->SendCommand(Cmd, CmdArg);\r
585 if (EFI_ERROR(Status)) {\r
586 DEBUG((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD%d): Error %r\n",Cmd, Status));\r
587 return Status;\r
588 }\r
1bfda055 589\r
e8e95df4 590 if (Transfer == MMC_IOBLOCKS_READ) {\r
1bfda055 591#ifndef USE_STREAM\r
e8e95df4 592 // Read one block of Data\r
593 Status = MmcHost->ReadBlockData(Lba,This->Media->BlockSize,Buffer);\r
594 if (EFI_ERROR(Status)) {\r
595 DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Read Block Data and Status = %r\n", Status));\r
596 return Status;\r
597 }\r
1bfda055 598#else\r
e8e95df4 599 //TODO: Read a steam\r
600 ASSERT(0);\r
1bfda055 601#endif\r
e8e95df4 602 Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState);\r
603 if (EFI_ERROR(Status)) {\r
604 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcProgrammingState\n"));\r
605 return Status;\r
606 }\r
607 } else {\r
1bfda055 608#ifndef USE_STREAM\r
e8e95df4 609 // Write one block of Data\r
610 Status = MmcHost->WriteBlockData(Lba,This->Media->BlockSize,Buffer);\r
611 if (EFI_ERROR(Status)) {\r
612 DEBUG((EFI_D_BLKIO, "MmcIdentificationMode(): Error Write Block Data and Status = %r\n", Status));\r
613 return Status;\r
614 }\r
1bfda055 615#else\r
e8e95df4 616 //TODO: Write a steam\r
617 ASSERT(0);\r
1bfda055 618#endif\r
e8e95df4 619 }\r
1bfda055 620\r
e8e95df4 621 // Command 12 - Stop transmission (ends read)\r
622 Status = MmcHost->SendCommand(MMC_CMD12, 0);\r
623 if (!EFI_ERROR(Status)) {\r
624 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1b,Response);\r
625 }\r
1bfda055 626\r
e8e95df4 627 // Command 13 - Read status and wait for programming to complete (return to tran)\r
628 Timeout = MMCI0_TIMEOUT;\r
629 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
630 Response[0] = 0;\r
631 while(!(Response[0] & MMC_R0_READY_FOR_DATA) && (MMC_R0_CURRENTSTATE(Response) != MMC_R0_STATE_TRAN) && Timeout--) {\r
632 Status = MmcHost->SendCommand(MMC_CMD13, CmdArg);\r
633 if (!EFI_ERROR(Status)) {\r
634 MmcHost->ReceiveResponse(MMC_RESPONSE_TYPE_R1,Response);\r
635 }\r
636 NanoSecondDelay(100);\r
637 Timeout--;\r
638 }\r
1bfda055 639\r
e8e95df4 640 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);\r
641 if (EFI_ERROR(Status)) {\r
642 DEBUG((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcTransferState\n"));\r
643 return Status;\r
1bfda055 644 }\r
645\r
e8e95df4 646 BytesRemainingToBeTransfered -= This->Media->BlockSize;\r
647 Lba += BlockCount;\r
648 Buffer = (UINT8 *)Buffer + This->Media->BlockSize;\r
649 }\r
650\r
651 return EFI_SUCCESS;\r
1bfda055 652}\r
653\r
654EFI_STATUS\r
655EFIAPI\r
656MmcReadBlocks (\r
657 IN EFI_BLOCK_IO_PROTOCOL *This,\r
658 IN UINT32 MediaId,\r
659 IN EFI_LBA Lba,\r
660 IN UINTN BufferSize,\r
661 OUT VOID *Buffer\r
e8e95df4 662 )\r
663{\r
664 return MmcIoBlocks (This, MMC_IOBLOCKS_READ, MediaId, Lba, BufferSize, Buffer);\r
1bfda055 665}\r
666\r
667EFI_STATUS\r
668EFIAPI\r
669MmcWriteBlocks (\r
670 IN EFI_BLOCK_IO_PROTOCOL *This,\r
671 IN UINT32 MediaId,\r
672 IN EFI_LBA Lba,\r
673 IN UINTN BufferSize,\r
674 IN VOID *Buffer\r
e8e95df4 675 )\r
676{\r
677 return MmcIoBlocks (This, MMC_IOBLOCKS_WRITE, MediaId, Lba, BufferSize, Buffer);\r
1bfda055 678}\r
679\r
680EFI_STATUS\r
681EFIAPI\r
682MmcFlushBlocks (\r
683 IN EFI_BLOCK_IO_PROTOCOL *This\r
e8e95df4 684 )\r
685{\r
686 return EFI_SUCCESS;\r
1bfda055 687}\r
688\r