BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / EmbeddedPkg / Universal / MmcDxe / MmcIdentification.c
CommitLineData
b4fdedc2
OM
1/** @file\r
2*\r
eff98cf9 3* Copyright (c) 2011-2015, ARM Limited. All rights reserved.\r
b4fdedc2
OM
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
e06253ba 15#include <Library/BaseMemoryLib.h>\r
7bb5fad5 16#include <Library/MemoryAllocationLib.h>\r
e06253ba
HZ
17#include <Library/TimerLib.h>\r
18\r
b4fdedc2
OM
19#include "Mmc.h"\r
20\r
21typedef union {\r
22 UINT32 Raw;\r
23 OCR Ocr;\r
24} OCR_RESPONSE;\r
25\r
26#define MAX_RETRY_COUNT 1000\r
27#define CMD_RETRY_COUNT 20\r
28#define RCA_SHIFT_OFFSET 16\r
29#define EMMC_CARD_SIZE 512\r
30#define EMMC_ECSD_SIZE_OFFSET 53\r
31\r
752ae805
HZ
32#define EXTCSD_BUS_WIDTH 183\r
33#define EXTCSD_HS_TIMING 185\r
34\r
35#define EMMC_TIMING_BACKWARD 0\r
36#define EMMC_TIMING_HS 1\r
37#define EMMC_TIMING_HS200 2\r
38#define EMMC_TIMING_HS400 3\r
39\r
40#define EMMC_BUS_WIDTH_1BIT 0\r
41#define EMMC_BUS_WIDTH_4BIT 1\r
42#define EMMC_BUS_WIDTH_8BIT 2\r
43#define EMMC_BUS_WIDTH_DDR_4BIT 5\r
44#define EMMC_BUS_WIDTH_DDR_8BIT 6\r
45\r
46#define EMMC_SWITCH_ERROR (1 << 7)\r
47\r
e06253ba
HZ
48#define SD_BUS_WIDTH_1BIT (1 << 0)\r
49#define SD_BUS_WIDTH_4BIT (1 << 2)\r
50\r
51#define SD_CCC_SWITCH (1 << 10)\r
52\r
752ae805
HZ
53#define DEVICE_STATE(x) (((x) >> 9) & 0xf)\r
54typedef enum _EMMC_DEVICE_STATE {\r
55 EMMC_IDLE_STATE = 0,\r
56 EMMC_READY_STATE,\r
57 EMMC_IDENT_STATE,\r
58 EMMC_STBY_STATE,\r
59 EMMC_TRAN_STATE,\r
60 EMMC_DATA_STATE,\r
61 EMMC_RCV_STATE,\r
62 EMMC_PRG_STATE,\r
63 EMMC_DIS_STATE,\r
64 EMMC_BTST_STATE,\r
65 EMMC_SLP_STATE\r
66} EMMC_DEVICE_STATE;\r
67\r
b4fdedc2
OM
68UINT32 mEmmcRcaCount = 0;\r
69\r
752ae805
HZ
70STATIC\r
71EFI_STATUS\r
72EFIAPI\r
73EmmcGetDeviceState (\r
74 IN MMC_HOST_INSTANCE *MmcHostInstance,\r
75 OUT EMMC_DEVICE_STATE *State\r
76 )\r
77{\r
78 EFI_MMC_HOST_PROTOCOL *Host;\r
79 EFI_STATUS Status;\r
80 UINT32 Data, RCA;\r
81\r
82 if (State == NULL) {\r
83 return EFI_INVALID_PARAMETER;\r
84 }\r
85\r
86 Host = MmcHostInstance->MmcHost;\r
87 RCA = MmcHostInstance->CardInfo.RCA << RCA_SHIFT_OFFSET;\r
88 Status = Host->SendCommand (Host, MMC_CMD13, RCA);\r
89 if (EFI_ERROR (Status)) {\r
90 DEBUG ((EFI_D_ERROR, "EmmcGetDeviceState(): Failed to get card status, Status=%r.\n", Status));\r
91 return Status;\r
92 }\r
93 Status = Host->ReceiveResponse (Host, MMC_RESPONSE_TYPE_R1, &Data);\r
94 if (EFI_ERROR (Status)) {\r
95 DEBUG ((EFI_D_ERROR, "EmmcGetDeviceState(): Failed to get response of CMD13, Status=%r.\n", Status));\r
96 return Status;\r
97 }\r
98 if (Data & EMMC_SWITCH_ERROR) {\r
99 DEBUG ((EFI_D_ERROR, "EmmcGetDeviceState(): Failed to switch expected mode, Status=%r.\n", Status));\r
100 return EFI_DEVICE_ERROR;\r
101 }\r
102 *State = DEVICE_STATE(Data);\r
103 return EFI_SUCCESS;\r
104}\r
105\r
106STATIC\r
107EFI_STATUS\r
108EFIAPI\r
109EmmcSetEXTCSD (\r
110 IN MMC_HOST_INSTANCE *MmcHostInstance,\r
111 UINT32 ExtCmdIndex,\r
112 UINT32 Value\r
113 )\r
114{\r
115 EFI_MMC_HOST_PROTOCOL *Host;\r
116 EMMC_DEVICE_STATE State;\r
117 EFI_STATUS Status;\r
118 UINT32 Argument;\r
119\r
120 Host = MmcHostInstance->MmcHost;\r
121 Argument = EMMC_CMD6_ARG_ACCESS(3) | EMMC_CMD6_ARG_INDEX(ExtCmdIndex) |\r
122 EMMC_CMD6_ARG_VALUE(Value) | EMMC_CMD6_ARG_CMD_SET(1);\r
123 Status = Host->SendCommand (Host, MMC_CMD6, Argument);\r
124 if (EFI_ERROR (Status)) {\r
125 DEBUG ((EFI_D_ERROR, "EmmcSetEXTCSD(): Failed to send CMD6, Status=%r.\n", Status));\r
126 return Status;\r
127 }\r
128 // Make sure device exiting prog mode\r
129 do {\r
130 Status = EmmcGetDeviceState (MmcHostInstance, &State);\r
131 if (EFI_ERROR (Status)) {\r
132 DEBUG ((EFI_D_ERROR, "EmmcSetEXTCSD(): Failed to get device state, Status=%r.\n", Status));\r
133 return Status;\r
134 }\r
135 } while (State == EMMC_PRG_STATE);\r
136 return EFI_SUCCESS;\r
137}\r
138\r
b4fdedc2
OM
139STATIC\r
140EFI_STATUS\r
141EFIAPI\r
142EmmcIdentificationMode (\r
143 IN MMC_HOST_INSTANCE *MmcHostInstance,\r
144 IN OCR_RESPONSE Response\r
145 )\r
146{\r
147 EFI_MMC_HOST_PROTOCOL *Host;\r
148 EFI_BLOCK_IO_MEDIA *Media;\r
149 EFI_STATUS Status;\r
752ae805 150 EMMC_DEVICE_STATE State;\r
b4fdedc2 151 UINT32 RCA;\r
b4fdedc2
OM
152\r
153 Host = MmcHostInstance->MmcHost;\r
154 Media = MmcHostInstance->BlockIo.Media;\r
155\r
156 // Fetch card identity register\r
157 Status = Host->SendCommand (Host, MMC_CMD2, 0);\r
158 if (EFI_ERROR (Status)) {\r
159 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Failed to send CMD2, Status=%r.\n", Status));\r
160 return Status;\r
161 }\r
162\r
163 Status = Host->ReceiveResponse (Host, MMC_RESPONSE_TYPE_R2, (UINT32 *)&(MmcHostInstance->CardInfo.CIDData));\r
164 if (EFI_ERROR (Status)) {\r
165 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): CID retrieval error, Status=%r.\n", Status));\r
166 return Status;\r
167 }\r
168\r
169 // Assign a relative address value to the card\r
170 MmcHostInstance->CardInfo.RCA = ++mEmmcRcaCount; // TODO: might need a more sophisticated way of doing this\r
171 RCA = MmcHostInstance->CardInfo.RCA << RCA_SHIFT_OFFSET;\r
172 Status = Host->SendCommand (Host, MMC_CMD3, RCA);\r
173 if (EFI_ERROR (Status)) {\r
174 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): RCA set error, Status=%r.\n", Status));\r
175 return Status;\r
176 }\r
177\r
178 // Fetch card specific data\r
179 Status = Host->SendCommand (Host, MMC_CMD9, RCA);\r
180 if (EFI_ERROR (Status)) {\r
181 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Failed to send CMD9, Status=%r.\n", Status));\r
182 return Status;\r
183 }\r
184\r
185 Status = Host->ReceiveResponse (Host, MMC_RESPONSE_TYPE_R2, (UINT32 *)&(MmcHostInstance->CardInfo.CSDData));\r
186 if (EFI_ERROR (Status)) {\r
187 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): CSD retrieval error, Status=%r.\n", Status));\r
188 return Status;\r
189 }\r
190\r
191 // Select the card\r
192 Status = Host->SendCommand (Host, MMC_CMD7, RCA);\r
193 if (EFI_ERROR (Status)) {\r
194 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Card selection error, Status=%r.\n", Status));\r
195 }\r
196\r
752ae805
HZ
197 if (MMC_HOST_HAS_SETIOS(Host)) {\r
198 // Set 1-bit bus width\r
199 Status = Host->SetIos (Host, 0, 1, EMMCBACKWARD);\r
200 if (EFI_ERROR (Status)) {\r
201 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Set 1-bit bus width error, Status=%r.\n", Status));\r
202 return Status;\r
203 }\r
204\r
205 // Set 1-bit bus width for EXTCSD\r
206 Status = EmmcSetEXTCSD (MmcHostInstance, EXTCSD_BUS_WIDTH, EMMC_BUS_WIDTH_1BIT);\r
207 if (EFI_ERROR (Status)) {\r
208 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Set extcsd bus width error, Status=%r.\n", Status));\r
209 return Status;\r
210 }\r
211 }\r
212\r
b4fdedc2 213 // Fetch ECSD\r
7bb5fad5
JN
214 MmcHostInstance->CardInfo.ECSDData = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (ECSD)));\r
215 if (MmcHostInstance->CardInfo.ECSDData == NULL) {\r
216 return EFI_OUT_OF_RESOURCES;\r
217 }\r
0ad564ff 218 Status = Host->SendCommand (Host, MMC_CMD8, 0);\r
b4fdedc2
OM
219 if (EFI_ERROR (Status)) {\r
220 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): ECSD fetch error, Status=%r.\n", Status));\r
221 }\r
222\r
7bb5fad5 223 Status = Host->ReadBlockData (Host, 0, 512, (UINT32 *)MmcHostInstance->CardInfo.ECSDData);\r
b4fdedc2
OM
224 if (EFI_ERROR (Status)) {\r
225 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): ECSD read error, Status=%r.\n", Status));\r
7bb5fad5 226 goto FreePageExit;\r
b4fdedc2
OM
227 }\r
228\r
752ae805
HZ
229 // Make sure device exiting data mode\r
230 do {\r
231 Status = EmmcGetDeviceState (MmcHostInstance, &State);\r
232 if (EFI_ERROR (Status)) {\r
233 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Failed to get device state, Status=%r.\n", Status));\r
7bb5fad5 234 goto FreePageExit;\r
752ae805
HZ
235 }\r
236 } while (State == EMMC_DATA_STATE);\r
237\r
b4fdedc2
OM
238 // Set up media\r
239 Media->BlockSize = EMMC_CARD_SIZE; // 512-byte support is mandatory for eMMC cards\r
240 Media->MediaId = MmcHostInstance->CardInfo.CIDData.PSN;\r
241 Media->ReadOnly = MmcHostInstance->CardInfo.CSDData.PERM_WRITE_PROTECT;\r
242 Media->LogicalBlocksPerPhysicalBlock = 1;\r
243 Media->IoAlign = 4;\r
244 // Compute last block using bits [215:212] of the ECSD\r
7bb5fad5 245 Media->LastBlock = MmcHostInstance->CardInfo.ECSDData->SECTOR_COUNT - 1; // eMMC isn't supposed to report this for\r
b4fdedc2
OM
246 // Cards <2GB in size, but the model does.\r
247\r
248 // Setup card type\r
249 MmcHostInstance->CardInfo.CardType = EMMC_CARD;\r
250 return EFI_SUCCESS;\r
7bb5fad5
JN
251\r
252FreePageExit:\r
253 FreePages (MmcHostInstance->CardInfo.ECSDData, EFI_SIZE_TO_PAGES (sizeof (ECSD)));\r
254 return Status;\r
b4fdedc2
OM
255}\r
256\r
752ae805
HZ
257STATIC\r
258EFI_STATUS\r
259InitializeEmmcDevice (\r
260 IN MMC_HOST_INSTANCE *MmcHostInstance\r
261 )\r
262{\r
263 EFI_MMC_HOST_PROTOCOL *Host;\r
264 EFI_STATUS Status = EFI_SUCCESS;\r
265 ECSD *ECSDData;\r
44f4ff62 266 UINT32 BusClockFreq, Idx, BusMode;\r
752ae805
HZ
267 UINT32 TimingMode[4] = {EMMCHS52DDR1V2, EMMCHS52DDR1V8, EMMCHS52, EMMCHS26};\r
268\r
269 Host = MmcHostInstance->MmcHost;\r
7bb5fad5 270 ECSDData = MmcHostInstance->CardInfo.ECSDData;\r
752ae805
HZ
271 if (ECSDData->DEVICE_TYPE == EMMCBACKWARD)\r
272 return EFI_SUCCESS;\r
273\r
274 if (!MMC_HOST_HAS_SETIOS(Host)) {\r
275 return EFI_SUCCESS;\r
276 }\r
277 Status = EmmcSetEXTCSD (MmcHostInstance, EXTCSD_HS_TIMING, EMMC_TIMING_HS);\r
278 if (EFI_ERROR (Status)) {\r
279 DEBUG ((DEBUG_ERROR, "InitializeEmmcDevice(): Failed to switch high speed mode, Status:%r.\n", Status));\r
280 return Status;\r
281 }\r
282\r
283 for (Idx = 0; Idx < 4; Idx++) {\r
284 switch (TimingMode[Idx]) {\r
285 case EMMCHS52DDR1V2:\r
286 case EMMCHS52DDR1V8:\r
287 case EMMCHS52:\r
288 BusClockFreq = 52000000;\r
289 break;\r
290 case EMMCHS26:\r
291 BusClockFreq = 26000000;\r
292 break;\r
293 default:\r
294 return EFI_UNSUPPORTED;\r
295 }\r
296 Status = Host->SetIos (Host, BusClockFreq, 8, TimingMode[Idx]);\r
297 if (!EFI_ERROR (Status)) {\r
44f4ff62
JN
298 switch (TimingMode[Idx]) {\r
299 case EMMCHS52DDR1V2:\r
300 case EMMCHS52DDR1V8:\r
301 BusMode = EMMC_BUS_WIDTH_DDR_8BIT;\r
302 break;\r
303 case EMMCHS52:\r
304 case EMMCHS26:\r
305 BusMode = EMMC_BUS_WIDTH_8BIT;\r
306 break;\r
307 default:\r
308 return EFI_UNSUPPORTED;\r
309 }\r
310 Status = EmmcSetEXTCSD (MmcHostInstance, EXTCSD_BUS_WIDTH, BusMode);\r
752ae805
HZ
311 if (EFI_ERROR (Status)) {\r
312 DEBUG ((DEBUG_ERROR, "InitializeEmmcDevice(): Failed to set EXTCSD bus width, Status:%r\n", Status));\r
313 }\r
314 return Status;\r
315 }\r
316 }\r
317 return Status;\r
318}\r
319\r
ea21f1d9
MA
320STATIC\r
321UINT32\r
322CreateSwitchCmdArgument (\r
323 IN UINT32 Mode,\r
324 IN UINT8 Group,\r
325 IN UINT8 Value\r
326 )\r
327{\r
328 UINT32 Argument;\r
329\r
330 Argument = Mode << 31 | 0x00FFFFFF;\r
331 Argument &= ~(0xF << (Group * 4));\r
332 Argument |= Value << (Group * 4);\r
333\r
334 return Argument;\r
335}\r
336\r
b4fdedc2
OM
337STATIC\r
338EFI_STATUS\r
339InitializeSdMmcDevice (\r
340 IN MMC_HOST_INSTANCE *MmcHostInstance\r
341 )\r
342{\r
343 UINT32 CmdArg;\r
344 UINT32 Response[4];\r
e06253ba 345 UINT32 Buffer[128];\r
ea21f1d9 346 UINT32 Speed;\r
b4fdedc2
OM
347 UINTN BlockSize;\r
348 UINTN CardSize;\r
349 UINTN NumBlocks;\r
e06253ba
HZ
350 BOOLEAN CccSwitch;\r
351 SCR Scr;\r
b4fdedc2
OM
352 EFI_STATUS Status;\r
353 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
354\r
ea21f1d9 355 Speed = SD_DEFAULT_SPEED;\r
b4fdedc2
OM
356 MmcHost = MmcHostInstance->MmcHost;\r
357\r
358 // Send a command to get Card specific data\r
359 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
360 Status = MmcHost->SendCommand (MmcHost, MMC_CMD9, CmdArg);\r
361 if (EFI_ERROR (Status)) {\r
362 DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(MMC_CMD9): Error, Status=%r\n", Status));\r
363 return Status;\r
364 }\r
365\r
366 // Read Response\r
367 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CSD, Response);\r
368 if (EFI_ERROR (Status)) {\r
369 DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(): Failed to receive CSD, Status=%r\n", Status));\r
370 return Status;\r
371 }\r
372 PrintCSD (Response);\r
e06253ba
HZ
373 if (MMC_CSD_GET_CCC(Response) & SD_CCC_SWITCH) {\r
374 CccSwitch = TRUE;\r
375 } else {\r
376 CccSwitch = FALSE;\r
377 }\r
b4fdedc2
OM
378\r
379 if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) {\r
380 CardSize = HC_MMC_CSD_GET_DEVICESIZE (Response);\r
381 NumBlocks = ((CardSize + 1) * 1024);\r
382 BlockSize = 1 << MMC_CSD_GET_READBLLEN (Response);\r
383 } else {\r
384 CardSize = MMC_CSD_GET_DEVICESIZE (Response);\r
385 NumBlocks = (CardSize + 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT (Response) + 2));\r
386 BlockSize = 1 << MMC_CSD_GET_READBLLEN (Response);\r
387 }\r
388\r
389 // For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.\r
390 if (BlockSize > 512) {\r
391 NumBlocks = MultU64x32 (NumBlocks, BlockSize / 512);\r
392 BlockSize = 512;\r
393 }\r
394\r
395 MmcHostInstance->BlockIo.Media->LastBlock = (NumBlocks - 1);\r
396 MmcHostInstance->BlockIo.Media->BlockSize = BlockSize;\r
397 MmcHostInstance->BlockIo.Media->ReadOnly = MmcHost->IsReadOnly (MmcHost);\r
398 MmcHostInstance->BlockIo.Media->MediaPresent = TRUE;\r
399 MmcHostInstance->BlockIo.Media->MediaId++;\r
400\r
401 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
402 Status = MmcHost->SendCommand (MmcHost, MMC_CMD7, CmdArg);\r
403 if (EFI_ERROR (Status)) {\r
404 DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(MMC_CMD7): Error and Status = %r\n", Status));\r
405 return Status;\r
406 }\r
407\r
e06253ba
HZ
408 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, CmdArg);\r
409 if (EFI_ERROR (Status)) {\r
3d436166 410 DEBUG ((DEBUG_ERROR, "%a (MMC_CMD55): Error and Status = %r\n", __FUNCTION__, Status));\r
e06253ba
HZ
411 return Status;\r
412 }\r
413 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);\r
414 if (EFI_ERROR (Status)) {\r
3d436166 415 DEBUG ((DEBUG_ERROR, "%a (MMC_CMD55): Error and Status = %r\n", __FUNCTION__, Status));\r
e06253ba
HZ
416 return Status;\r
417 }\r
418 if ((Response[0] & MMC_STATUS_APP_CMD) == 0) {\r
419 return EFI_SUCCESS;\r
420 }\r
421\r
422 /* SCR */\r
423 Status = MmcHost->SendCommand (MmcHost, MMC_ACMD51, 0);\r
424 if (EFI_ERROR (Status)) {\r
425 DEBUG ((EFI_D_ERROR, "%a(MMC_ACMD51): Error and Status = %r\n", __func__, Status));\r
426 return Status;\r
427 } else {\r
428 Status = MmcHost->ReadBlockData (MmcHost, 0, 8, Buffer);\r
429 if (EFI_ERROR (Status)) {\r
430 DEBUG ((EFI_D_ERROR, "%a(MMC_ACMD51): ReadBlockData Error and Status = %r\n", __func__, Status));\r
431 return Status;\r
432 }\r
433 CopyMem (&Scr, Buffer, 8);\r
434 if (Scr.SD_SPEC == 2) {\r
435 if (Scr.SD_SPEC3 == 1) {\r
436 if (Scr.SD_SPEC4 == 1) {\r
437 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 4.xx\n"));\r
438 } else {\r
439 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 3.0x\n"));\r
440 }\r
441 } else {\r
442 if (Scr.SD_SPEC4 == 0) {\r
443 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 2.0\n"));\r
444 } else {\r
445 DEBUG ((EFI_D_ERROR, "Found invalid SD Card\n"));\r
446 }\r
447 }\r
448 } else {\r
449 if ((Scr.SD_SPEC3 == 0) && (Scr.SD_SPEC4 == 0)) {\r
450 if (Scr.SD_SPEC == 1) {\r
451 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 1.10\n"));\r
452 } else {\r
453 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 1.0\n"));\r
454 }\r
455 } else {\r
456 DEBUG ((EFI_D_ERROR, "Found invalid SD Card\n"));\r
457 }\r
458 }\r
459 }\r
460 if (CccSwitch) {\r
ea21f1d9
MA
461 /* SD Switch, Mode:0, Group:0, Value:0 */\r
462 CmdArg = CreateSwitchCmdArgument(0, 0, 0);\r
463 Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, CmdArg);\r
464 if (EFI_ERROR (Status)) {\r
465 DEBUG ((DEBUG_ERROR, "%a (MMC_CMD6): Error and Status = %r\n", __FUNCTION__, Status));\r
466 return Status;\r
467 } else {\r
468 Status = MmcHost->ReadBlockData (MmcHost, 0, SWITCH_CMD_DATA_LENGTH, Buffer);\r
469 if (EFI_ERROR (Status)) {\r
470 DEBUG ((DEBUG_ERROR, "%a (MMC_CMD6): ReadBlockData Error and Status = %r\n", __FUNCTION__, Status));\r
471 return Status;\r
472 }\r
473 }\r
474\r
475 if (!(Buffer[3] & SD_HIGH_SPEED_SUPPORTED)) {\r
476 DEBUG ((DEBUG_ERROR, "%a : High Speed not supported by Card %r\n", __FUNCTION__, Status));\r
477 return Status;\r
478 }\r
479\r
480 Speed = SD_HIGH_SPEED;\r
481\r
e06253ba 482 /* SD Switch, Mode:1, Group:0, Value:1 */\r
ea21f1d9 483 CmdArg = CreateSwitchCmdArgument(1, 0, 1);\r
e06253ba
HZ
484 Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, CmdArg);\r
485 if (EFI_ERROR (Status)) {\r
3d436166 486 DEBUG ((DEBUG_ERROR, "%a (MMC_CMD6): Error and Status = %r\n", __FUNCTION__, Status));\r
e06253ba
HZ
487 return Status;\r
488 } else {\r
ea21f1d9 489 Status = MmcHost->ReadBlockData (MmcHost, 0, SWITCH_CMD_DATA_LENGTH, Buffer);\r
e06253ba 490 if (EFI_ERROR (Status)) {\r
3d436166 491 DEBUG ((DEBUG_ERROR, "%a (MMC_CMD6): ReadBlockData Error and Status = %r\n", __FUNCTION__, Status));\r
e06253ba
HZ
492 return Status;\r
493 }\r
ea21f1d9
MA
494\r
495 if ((Buffer[4] & SWITCH_CMD_SUCCESS_MASK) != 0x01000000) {\r
496 DEBUG((DEBUG_ERROR, "Problem switching SD card into high-speed mode\n"));\r
497 return Status;\r
498 }\r
e06253ba
HZ
499 }\r
500 }\r
501 if (Scr.SD_BUS_WIDTHS & SD_BUS_WIDTH_4BIT) {\r
502 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
503 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, CmdArg);\r
504 if (EFI_ERROR (Status)) {\r
3d436166 505 DEBUG ((DEBUG_ERROR, "%a (MMC_CMD55): Error and Status = %r\n", __FUNCTION__, Status));\r
e06253ba
HZ
506 return Status;\r
507 }\r
508 /* Width: 4 */\r
509 Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, 2);\r
510 if (EFI_ERROR (Status)) {\r
3d436166 511 DEBUG ((DEBUG_ERROR, "%a (MMC_CMD6): Error and Status = %r\n", __FUNCTION__, Status));\r
e06253ba
HZ
512 return Status;\r
513 }\r
514 }\r
515 if (MMC_HOST_HAS_SETIOS(MmcHost)) {\r
ea21f1d9 516 Status = MmcHost->SetIos (MmcHost, Speed, BUSWIDTH_4, EMMCBACKWARD);\r
e06253ba 517 if (EFI_ERROR (Status)) {\r
3d436166 518 DEBUG ((DEBUG_ERROR, "%a (SetIos): Error and Status = %r\n", __FUNCTION__, Status));\r
e06253ba
HZ
519 return Status;\r
520 }\r
521 }\r
b4fdedc2
OM
522 return EFI_SUCCESS;\r
523}\r
524\r
525STATIC\r
526EFI_STATUS\r
527EFIAPI\r
528MmcIdentificationMode (\r
529 IN MMC_HOST_INSTANCE *MmcHostInstance\r
530 )\r
531{\r
532 EFI_STATUS Status;\r
533 UINT32 Response[4];\r
534 UINTN Timeout;\r
535 UINTN CmdArg;\r
536 BOOLEAN IsHCS;\r
537 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
538 OCR_RESPONSE OcrResponse;\r
539\r
540 MmcHost = MmcHostInstance->MmcHost;\r
541 CmdArg = 0;\r
542 IsHCS = FALSE;\r
543\r
544 if (MmcHost == NULL) {\r
545 return EFI_INVALID_PARAMETER;\r
546 }\r
547\r
548 // We can get into this function if we restart the identification mode\r
549 if (MmcHostInstance->State == MmcHwInitializationState) {\r
550 // Initialize the MMC Host HW\r
551 Status = MmcNotifyState (MmcHostInstance, MmcHwInitializationState);\r
552 if (EFI_ERROR (Status)) {\r
553 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcHwInitializationState, Status=%r.\n", Status));\r
554 return Status;\r
555 }\r
556 }\r
557\r
558 Status = MmcHost->SendCommand (MmcHost, MMC_CMD0, 0);\r
559 if (EFI_ERROR (Status)) {\r
560 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD0): Error, Status=%r.\n", Status));\r
561 return Status;\r
562 }\r
563 Status = MmcNotifyState (MmcHostInstance, MmcIdleState);\r
564 if (EFI_ERROR (Status)) {\r
565 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdleState, Status=%r.\n", Status));\r
566 return Status;\r
567 }\r
568\r
139b5773
OM
569 // Send CMD1 to get OCR (MMC)\r
570 // This command only valid for MMC and eMMC\r
32010753
HZ
571 Timeout = MAX_RETRY_COUNT;\r
572 do {\r
573 Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, EMMC_CMD1_CAPACITY_GREATER_THAN_2GB);\r
574 if (EFI_ERROR (Status))\r
575 break;\r
b4fdedc2
OM
576 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, (UINT32 *)&OcrResponse);\r
577 if (EFI_ERROR (Status)) {\r
578 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));\r
579 return Status;\r
580 }\r
32010753
HZ
581 Timeout--;\r
582 } while (!OcrResponse.Ocr.PowerUp && (Timeout > 0));\r
583 if (Status == EFI_SUCCESS) {\r
b4fdedc2
OM
584 if (!OcrResponse.Ocr.PowerUp) {\r
585 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD1): Card initialisation failure, Status=%r.\n", Status));\r
586 return EFI_DEVICE_ERROR;\r
587 }\r
588 OcrResponse.Ocr.PowerUp = 0;\r
589 if (OcrResponse.Raw == EMMC_CMD1_CAPACITY_GREATER_THAN_2GB) {\r
590 MmcHostInstance->CardInfo.OCRData.AccessMode = BIT1;\r
591 }\r
592 else {\r
593 MmcHostInstance->CardInfo.OCRData.AccessMode = 0x0;\r
594 }\r
139b5773 595 // Check whether MMC or eMMC\r
b4fdedc2
OM
596 if (OcrResponse.Raw == EMMC_CMD1_CAPACITY_GREATER_THAN_2GB ||\r
597 OcrResponse.Raw == EMMC_CMD1_CAPACITY_LESS_THAN_2GB) {\r
598 return EmmcIdentificationMode (MmcHostInstance, OcrResponse);\r
599 }\r
b4fdedc2
OM
600 }\r
601\r
602 // Are we using SDIO ?\r
603 Status = MmcHost->SendCommand (MmcHost, MMC_CMD5, 0);\r
604 if (Status == EFI_SUCCESS) {\r
605 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported, Status=%r.\n", Status));\r
606 return EFI_UNSUPPORTED;\r
607 }\r
608\r
609 // Check which kind of card we are using. Ver2.00 or later SD Memory Card (PL180 is SD v1.1)\r
610 CmdArg = (0x0UL << 12 | BIT8 | 0xCEUL << 0);\r
611 Status = MmcHost->SendCommand (MmcHost, MMC_CMD8, CmdArg);\r
612 if (Status == EFI_SUCCESS) {\r
613 DEBUG ((EFI_D_ERROR, "Card is SD2.0 => Supports high capacity\n"));\r
614 IsHCS = TRUE;\r
615 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R7, Response);\r
616 if (EFI_ERROR (Status)) {\r
617 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive response to CMD8, Status=%r.\n", Status));\r
618 return Status;\r
619 }\r
620 PrintResponseR1 (Response[0]);\r
621 // Check if it is valid response\r
622 if (Response[0] != CmdArg) {\r
623 DEBUG ((EFI_D_ERROR, "The Card is not usable\n"));\r
624 return EFI_UNSUPPORTED;\r
625 }\r
626 } else {\r
627 DEBUG ((EFI_D_ERROR, "Not a SD2.0 Card\n"));\r
628 }\r
629\r
630 // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.PowerUp == 1)\r
631 Timeout = MAX_RETRY_COUNT;\r
632 while (Timeout > 0) {\r
633 // SD Card or MMC Card ? CMD55 indicates to the card that the next command is an application specific command\r
634 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, 0);\r
635 if (Status == EFI_SUCCESS) {\r
636 DEBUG ((EFI_D_INFO, "Card should be SD\n"));\r
637 if (IsHCS) {\r
638 MmcHostInstance->CardInfo.CardType = SD_CARD_2;\r
639 } else {\r
640 MmcHostInstance->CardInfo.CardType = SD_CARD;\r
641 }\r
642\r
643 // Note: The first time CmdArg will be zero\r
644 CmdArg = ((UINTN *) &(MmcHostInstance->CardInfo.OCRData))[0];\r
645 if (IsHCS) {\r
646 CmdArg |= BIT30;\r
647 }\r
648 Status = MmcHost->SendCommand (MmcHost, MMC_ACMD41, CmdArg);\r
649 if (!EFI_ERROR (Status)) {\r
650 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, Response);\r
651 if (EFI_ERROR (Status)) {\r
652 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));\r
653 return Status;\r
654 }\r
655 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];\r
656 }\r
657 } else {\r
658 DEBUG ((EFI_D_INFO, "Card should be MMC\n"));\r
659 MmcHostInstance->CardInfo.CardType = MMC_CARD;\r
660\r
661 Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, 0x800000);\r
662 if (!EFI_ERROR (Status)) {\r
663 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, Response);\r
664 if (EFI_ERROR (Status)) {\r
665 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));\r
666 return Status;\r
667 }\r
668 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];\r
669 }\r
670 }\r
671\r
672 if (!EFI_ERROR (Status)) {\r
673 if (!MmcHostInstance->CardInfo.OCRData.PowerUp) {\r
eff98cf9 674 gBS->Stall (1);\r
b4fdedc2
OM
675 Timeout--;\r
676 } else {\r
677 if ((MmcHostInstance->CardInfo.CardType == SD_CARD_2) && (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1)) {\r
678 MmcHostInstance->CardInfo.CardType = SD_CARD_2_HIGH;\r
679 DEBUG ((EFI_D_ERROR, "High capacity card.\n"));\r
680 }\r
681 break; // The MMC/SD card is ready. Continue the Identification Mode\r
682 }\r
683 } else {\r
eff98cf9 684 gBS->Stall (1);\r
b4fdedc2
OM
685 Timeout--;\r
686 }\r
687 }\r
688\r
689 if (Timeout == 0) {\r
690 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(): No Card\n"));\r
691 return EFI_NO_MEDIA;\r
692 } else {\r
693 PrintOCR (Response[0]);\r
694 }\r
695\r
696 Status = MmcNotifyState (MmcHostInstance, MmcReadyState);\r
697 if (EFI_ERROR (Status)) {\r
698 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcReadyState\n"));\r
699 return Status;\r
700 }\r
701\r
702 Status = MmcHost->SendCommand (MmcHost, MMC_CMD2, 0);\r
703 if (EFI_ERROR (Status)) {\r
704 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD2): Error\n"));\r
705 return Status;\r
706 }\r
707 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CID, Response);\r
708 if (EFI_ERROR (Status)) {\r
709 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive CID, Status=%r.\n", Status));\r
710 return Status;\r
711 }\r
712\r
713 PrintCID (Response);\r
714\r
715 Status = MmcHost->NotifyState (MmcHost, MmcIdentificationState);\r
716 if (EFI_ERROR (Status)) {\r
717 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdentificationState\n"));\r
718 return Status;\r
719 }\r
720\r
721 //\r
722 // Note, SD specifications say that "if the command execution causes a state change, it\r
723 // will be visible to the host in the response to the next command"\r
724 // The status returned for this CMD3 will be 2 - identification\r
725 //\r
726 CmdArg = 1;\r
727 Status = MmcHost->SendCommand (MmcHost, MMC_CMD3, CmdArg);\r
728 if (EFI_ERROR (Status)) {\r
729 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n"));\r
730 return Status;\r
731 }\r
732\r
733 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_RCA, Response);\r
734 if (EFI_ERROR (Status)) {\r
735 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive RCA, Status=%r.\n", Status));\r
736 return Status;\r
737 }\r
738 PrintRCA (Response[0]);\r
739\r
740 // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card\r
741 if (MmcHostInstance->CardInfo.CardType != MMC_CARD) {\r
742 MmcHostInstance->CardInfo.RCA = Response[0] >> 16;\r
743 } else {\r
744 MmcHostInstance->CardInfo.RCA = CmdArg;\r
745 }\r
746 Status = MmcNotifyState (MmcHostInstance, MmcStandByState);\r
747 if (EFI_ERROR (Status)) {\r
748 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcStandByState\n"));\r
749 return Status;\r
750 }\r
751\r
752 return EFI_SUCCESS;\r
753}\r
754\r
755EFI_STATUS\r
756InitializeMmcDevice (\r
757 IN MMC_HOST_INSTANCE *MmcHostInstance\r
758 )\r
759{\r
760 EFI_STATUS Status;\r
761 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
762 UINTN BlockCount;\r
763\r
764 BlockCount = 1;\r
765 MmcHost = MmcHostInstance->MmcHost;\r
766\r
767 Status = MmcIdentificationMode (MmcHostInstance);\r
768 if (EFI_ERROR (Status)) {\r
769 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(): Error in Identification Mode, Status=%r\n", Status));\r
770 return Status;\r
771 }\r
772\r
773 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);\r
774 if (EFI_ERROR (Status)) {\r
775 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(): Error MmcTransferState, Status=%r\n", Status));\r
776 return Status;\r
777 }\r
778\r
779 if (MmcHostInstance->CardInfo.CardType != EMMC_CARD) {\r
780 Status = InitializeSdMmcDevice (MmcHostInstance);\r
752ae805
HZ
781 } else {\r
782 Status = InitializeEmmcDevice (MmcHostInstance);\r
783 }\r
784 if (EFI_ERROR (Status)) {\r
785 return Status;\r
b4fdedc2
OM
786 }\r
787\r
788 // Set Block Length\r
789 Status = MmcHost->SendCommand (MmcHost, MMC_CMD16, MmcHostInstance->BlockIo.Media->BlockSize);\r
790 if (EFI_ERROR (Status)) {\r
791 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(MMC_CMD16): Error MmcHostInstance->BlockIo.Media->BlockSize: %d and Error = %r\n",\r
792 MmcHostInstance->BlockIo.Media->BlockSize, Status));\r
793 return Status;\r
794 }\r
795\r
796 // Block Count (not used). Could return an error for SD card\r
797 if (MmcHostInstance->CardInfo.CardType == MMC_CARD) {\r
798 Status = MmcHost->SendCommand (MmcHost, MMC_CMD23, BlockCount);\r
799 if (EFI_ERROR (Status)) {\r
800 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(MMC_CMD23): Error, Status=%r\n", Status));\r
801 return Status;\r
802 }\r
803 }\r
804\r
805 return EFI_SUCCESS;\r
806}\r