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