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