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