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