EmbeddedPkg/MmcDxe: Align the ExtCSD buffer
[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 EFI_STATUS
322 InitializeSdMmcDevice (
323 IN MMC_HOST_INSTANCE *MmcHostInstance
324 )
325 {
326 UINT32 CmdArg;
327 UINT32 Response[4];
328 UINT32 Buffer[128];
329 UINTN BlockSize;
330 UINTN CardSize;
331 UINTN NumBlocks;
332 BOOLEAN CccSwitch;
333 SCR Scr;
334 EFI_STATUS Status;
335 EFI_MMC_HOST_PROTOCOL *MmcHost;
336
337 MmcHost = MmcHostInstance->MmcHost;
338
339 // Send a command to get Card specific data
340 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
341 Status = MmcHost->SendCommand (MmcHost, MMC_CMD9, CmdArg);
342 if (EFI_ERROR (Status)) {
343 DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(MMC_CMD9): Error, Status=%r\n", Status));
344 return Status;
345 }
346
347 // Read Response
348 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CSD, Response);
349 if (EFI_ERROR (Status)) {
350 DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(): Failed to receive CSD, Status=%r\n", Status));
351 return Status;
352 }
353 PrintCSD (Response);
354 if (MMC_CSD_GET_CCC(Response) & SD_CCC_SWITCH) {
355 CccSwitch = TRUE;
356 } else {
357 CccSwitch = FALSE;
358 }
359
360 if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) {
361 CardSize = HC_MMC_CSD_GET_DEVICESIZE (Response);
362 NumBlocks = ((CardSize + 1) * 1024);
363 BlockSize = 1 << MMC_CSD_GET_READBLLEN (Response);
364 } else {
365 CardSize = MMC_CSD_GET_DEVICESIZE (Response);
366 NumBlocks = (CardSize + 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT (Response) + 2));
367 BlockSize = 1 << MMC_CSD_GET_READBLLEN (Response);
368 }
369
370 // For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
371 if (BlockSize > 512) {
372 NumBlocks = MultU64x32 (NumBlocks, BlockSize / 512);
373 BlockSize = 512;
374 }
375
376 MmcHostInstance->BlockIo.Media->LastBlock = (NumBlocks - 1);
377 MmcHostInstance->BlockIo.Media->BlockSize = BlockSize;
378 MmcHostInstance->BlockIo.Media->ReadOnly = MmcHost->IsReadOnly (MmcHost);
379 MmcHostInstance->BlockIo.Media->MediaPresent = TRUE;
380 MmcHostInstance->BlockIo.Media->MediaId++;
381
382 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
383 Status = MmcHost->SendCommand (MmcHost, MMC_CMD7, CmdArg);
384 if (EFI_ERROR (Status)) {
385 DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(MMC_CMD7): Error and Status = %r\n", Status));
386 return Status;
387 }
388
389 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, CmdArg);
390 if (EFI_ERROR (Status)) {
391 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD55): Error and Status = %r\n", Status));
392 return Status;
393 }
394 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);
395 if (EFI_ERROR (Status)) {
396 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD55): Error and Status = %r\n", Status));
397 return Status;
398 }
399 if ((Response[0] & MMC_STATUS_APP_CMD) == 0) {
400 return EFI_SUCCESS;
401 }
402
403 /* SCR */
404 Status = MmcHost->SendCommand (MmcHost, MMC_ACMD51, 0);
405 if (EFI_ERROR (Status)) {
406 DEBUG ((EFI_D_ERROR, "%a(MMC_ACMD51): Error and Status = %r\n", __func__, Status));
407 return Status;
408 } else {
409 Status = MmcHost->ReadBlockData (MmcHost, 0, 8, Buffer);
410 if (EFI_ERROR (Status)) {
411 DEBUG ((EFI_D_ERROR, "%a(MMC_ACMD51): ReadBlockData Error and Status = %r\n", __func__, Status));
412 return Status;
413 }
414 CopyMem (&Scr, Buffer, 8);
415 if (Scr.SD_SPEC == 2) {
416 if (Scr.SD_SPEC3 == 1) {
417 if (Scr.SD_SPEC4 == 1) {
418 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 4.xx\n"));
419 } else {
420 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 3.0x\n"));
421 }
422 } else {
423 if (Scr.SD_SPEC4 == 0) {
424 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 2.0\n"));
425 } else {
426 DEBUG ((EFI_D_ERROR, "Found invalid SD Card\n"));
427 }
428 }
429 } else {
430 if ((Scr.SD_SPEC3 == 0) && (Scr.SD_SPEC4 == 0)) {
431 if (Scr.SD_SPEC == 1) {
432 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 1.10\n"));
433 } else {
434 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 1.0\n"));
435 }
436 } else {
437 DEBUG ((EFI_D_ERROR, "Found invalid SD Card\n"));
438 }
439 }
440 }
441 if (CccSwitch) {
442 /* SD Switch, Mode:1, Group:0, Value:1 */
443 CmdArg = 1 << 31 | 0x00FFFFFF;
444 CmdArg &= ~(0xF << (0 * 4));
445 CmdArg |= 1 << (0 * 4);
446 Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, CmdArg);
447 if (EFI_ERROR (Status)) {
448 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): Error and Status = %r\n", Status));
449 return Status;
450 } else {
451 Status = MmcHost->ReadBlockData (MmcHost, 0, 64, Buffer);
452 if (EFI_ERROR (Status)) {
453 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): ReadBlockData Error and Status = %r\n", Status));
454 return Status;
455 }
456 }
457 }
458 if (Scr.SD_BUS_WIDTHS & SD_BUS_WIDTH_4BIT) {
459 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
460 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, CmdArg);
461 if (EFI_ERROR (Status)) {
462 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD55): Error and Status = %r\n", Status));
463 return Status;
464 }
465 /* Width: 4 */
466 Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, 2);
467 if (EFI_ERROR (Status)) {
468 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): Error and Status = %r\n", Status));
469 return Status;
470 }
471 }
472 if (MMC_HOST_HAS_SETIOS(MmcHost)) {
473 Status = MmcHost->SetIos (MmcHost, 26 * 1000 * 1000, 4, EMMCBACKWARD);
474 if (EFI_ERROR (Status)) {
475 DEBUG ((EFI_D_ERROR, "%a(SetIos): Error and Status = %r\n", Status));
476 return Status;
477 }
478 }
479 return EFI_SUCCESS;
480 }
481
482 STATIC
483 EFI_STATUS
484 EFIAPI
485 MmcIdentificationMode (
486 IN MMC_HOST_INSTANCE *MmcHostInstance
487 )
488 {
489 EFI_STATUS Status;
490 UINT32 Response[4];
491 UINTN Timeout;
492 UINTN CmdArg;
493 BOOLEAN IsHCS;
494 EFI_MMC_HOST_PROTOCOL *MmcHost;
495 OCR_RESPONSE OcrResponse;
496
497 MmcHost = MmcHostInstance->MmcHost;
498 CmdArg = 0;
499 IsHCS = FALSE;
500
501 if (MmcHost == NULL) {
502 return EFI_INVALID_PARAMETER;
503 }
504
505 // We can get into this function if we restart the identification mode
506 if (MmcHostInstance->State == MmcHwInitializationState) {
507 // Initialize the MMC Host HW
508 Status = MmcNotifyState (MmcHostInstance, MmcHwInitializationState);
509 if (EFI_ERROR (Status)) {
510 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcHwInitializationState, Status=%r.\n", Status));
511 return Status;
512 }
513 }
514
515 Status = MmcHost->SendCommand (MmcHost, MMC_CMD0, 0);
516 if (EFI_ERROR (Status)) {
517 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD0): Error, Status=%r.\n", Status));
518 return Status;
519 }
520 Status = MmcNotifyState (MmcHostInstance, MmcIdleState);
521 if (EFI_ERROR (Status)) {
522 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdleState, Status=%r.\n", Status));
523 return Status;
524 }
525
526 // Send CMD1 to get OCR (MMC)
527 // This command only valid for MMC and eMMC
528 Timeout = MAX_RETRY_COUNT;
529 do {
530 Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, EMMC_CMD1_CAPACITY_GREATER_THAN_2GB);
531 if (EFI_ERROR (Status))
532 break;
533 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, (UINT32 *)&OcrResponse);
534 if (EFI_ERROR (Status)) {
535 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));
536 return Status;
537 }
538 Timeout--;
539 } while (!OcrResponse.Ocr.PowerUp && (Timeout > 0));
540 if (Status == EFI_SUCCESS) {
541 if (!OcrResponse.Ocr.PowerUp) {
542 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD1): Card initialisation failure, Status=%r.\n", Status));
543 return EFI_DEVICE_ERROR;
544 }
545 OcrResponse.Ocr.PowerUp = 0;
546 if (OcrResponse.Raw == EMMC_CMD1_CAPACITY_GREATER_THAN_2GB) {
547 MmcHostInstance->CardInfo.OCRData.AccessMode = BIT1;
548 }
549 else {
550 MmcHostInstance->CardInfo.OCRData.AccessMode = 0x0;
551 }
552 // Check whether MMC or eMMC
553 if (OcrResponse.Raw == EMMC_CMD1_CAPACITY_GREATER_THAN_2GB ||
554 OcrResponse.Raw == EMMC_CMD1_CAPACITY_LESS_THAN_2GB) {
555 return EmmcIdentificationMode (MmcHostInstance, OcrResponse);
556 }
557 }
558
559 // Are we using SDIO ?
560 Status = MmcHost->SendCommand (MmcHost, MMC_CMD5, 0);
561 if (Status == EFI_SUCCESS) {
562 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported, Status=%r.\n", Status));
563 return EFI_UNSUPPORTED;
564 }
565
566 // Check which kind of card we are using. Ver2.00 or later SD Memory Card (PL180 is SD v1.1)
567 CmdArg = (0x0UL << 12 | BIT8 | 0xCEUL << 0);
568 Status = MmcHost->SendCommand (MmcHost, MMC_CMD8, CmdArg);
569 if (Status == EFI_SUCCESS) {
570 DEBUG ((EFI_D_ERROR, "Card is SD2.0 => Supports high capacity\n"));
571 IsHCS = TRUE;
572 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R7, Response);
573 if (EFI_ERROR (Status)) {
574 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive response to CMD8, Status=%r.\n", Status));
575 return Status;
576 }
577 PrintResponseR1 (Response[0]);
578 // Check if it is valid response
579 if (Response[0] != CmdArg) {
580 DEBUG ((EFI_D_ERROR, "The Card is not usable\n"));
581 return EFI_UNSUPPORTED;
582 }
583 } else {
584 DEBUG ((EFI_D_ERROR, "Not a SD2.0 Card\n"));
585 }
586
587 // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.PowerUp == 1)
588 Timeout = MAX_RETRY_COUNT;
589 while (Timeout > 0) {
590 // SD Card or MMC Card ? CMD55 indicates to the card that the next command is an application specific command
591 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, 0);
592 if (Status == EFI_SUCCESS) {
593 DEBUG ((EFI_D_INFO, "Card should be SD\n"));
594 if (IsHCS) {
595 MmcHostInstance->CardInfo.CardType = SD_CARD_2;
596 } else {
597 MmcHostInstance->CardInfo.CardType = SD_CARD;
598 }
599
600 // Note: The first time CmdArg will be zero
601 CmdArg = ((UINTN *) &(MmcHostInstance->CardInfo.OCRData))[0];
602 if (IsHCS) {
603 CmdArg |= BIT30;
604 }
605 Status = MmcHost->SendCommand (MmcHost, MMC_ACMD41, CmdArg);
606 if (!EFI_ERROR (Status)) {
607 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, Response);
608 if (EFI_ERROR (Status)) {
609 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));
610 return Status;
611 }
612 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];
613 }
614 } else {
615 DEBUG ((EFI_D_INFO, "Card should be MMC\n"));
616 MmcHostInstance->CardInfo.CardType = MMC_CARD;
617
618 Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, 0x800000);
619 if (!EFI_ERROR (Status)) {
620 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, Response);
621 if (EFI_ERROR (Status)) {
622 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));
623 return Status;
624 }
625 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];
626 }
627 }
628
629 if (!EFI_ERROR (Status)) {
630 if (!MmcHostInstance->CardInfo.OCRData.PowerUp) {
631 gBS->Stall (1);
632 Timeout--;
633 } else {
634 if ((MmcHostInstance->CardInfo.CardType == SD_CARD_2) && (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1)) {
635 MmcHostInstance->CardInfo.CardType = SD_CARD_2_HIGH;
636 DEBUG ((EFI_D_ERROR, "High capacity card.\n"));
637 }
638 break; // The MMC/SD card is ready. Continue the Identification Mode
639 }
640 } else {
641 gBS->Stall (1);
642 Timeout--;
643 }
644 }
645
646 if (Timeout == 0) {
647 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(): No Card\n"));
648 return EFI_NO_MEDIA;
649 } else {
650 PrintOCR (Response[0]);
651 }
652
653 Status = MmcNotifyState (MmcHostInstance, MmcReadyState);
654 if (EFI_ERROR (Status)) {
655 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcReadyState\n"));
656 return Status;
657 }
658
659 Status = MmcHost->SendCommand (MmcHost, MMC_CMD2, 0);
660 if (EFI_ERROR (Status)) {
661 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD2): Error\n"));
662 return Status;
663 }
664 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CID, Response);
665 if (EFI_ERROR (Status)) {
666 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive CID, Status=%r.\n", Status));
667 return Status;
668 }
669
670 PrintCID (Response);
671
672 Status = MmcHost->NotifyState (MmcHost, MmcIdentificationState);
673 if (EFI_ERROR (Status)) {
674 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdentificationState\n"));
675 return Status;
676 }
677
678 //
679 // Note, SD specifications say that "if the command execution causes a state change, it
680 // will be visible to the host in the response to the next command"
681 // The status returned for this CMD3 will be 2 - identification
682 //
683 CmdArg = 1;
684 Status = MmcHost->SendCommand (MmcHost, MMC_CMD3, CmdArg);
685 if (EFI_ERROR (Status)) {
686 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n"));
687 return Status;
688 }
689
690 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_RCA, Response);
691 if (EFI_ERROR (Status)) {
692 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive RCA, Status=%r.\n", Status));
693 return Status;
694 }
695 PrintRCA (Response[0]);
696
697 // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card
698 if (MmcHostInstance->CardInfo.CardType != MMC_CARD) {
699 MmcHostInstance->CardInfo.RCA = Response[0] >> 16;
700 } else {
701 MmcHostInstance->CardInfo.RCA = CmdArg;
702 }
703 Status = MmcNotifyState (MmcHostInstance, MmcStandByState);
704 if (EFI_ERROR (Status)) {
705 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcStandByState\n"));
706 return Status;
707 }
708
709 return EFI_SUCCESS;
710 }
711
712 EFI_STATUS
713 InitializeMmcDevice (
714 IN MMC_HOST_INSTANCE *MmcHostInstance
715 )
716 {
717 EFI_STATUS Status;
718 EFI_MMC_HOST_PROTOCOL *MmcHost;
719 UINTN BlockCount;
720
721 BlockCount = 1;
722 MmcHost = MmcHostInstance->MmcHost;
723
724 Status = MmcIdentificationMode (MmcHostInstance);
725 if (EFI_ERROR (Status)) {
726 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(): Error in Identification Mode, Status=%r\n", Status));
727 return Status;
728 }
729
730 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);
731 if (EFI_ERROR (Status)) {
732 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(): Error MmcTransferState, Status=%r\n", Status));
733 return Status;
734 }
735
736 if (MmcHostInstance->CardInfo.CardType != EMMC_CARD) {
737 Status = InitializeSdMmcDevice (MmcHostInstance);
738 } else {
739 Status = InitializeEmmcDevice (MmcHostInstance);
740 }
741 if (EFI_ERROR (Status)) {
742 return Status;
743 }
744
745 // Set Block Length
746 Status = MmcHost->SendCommand (MmcHost, MMC_CMD16, MmcHostInstance->BlockIo.Media->BlockSize);
747 if (EFI_ERROR (Status)) {
748 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(MMC_CMD16): Error MmcHostInstance->BlockIo.Media->BlockSize: %d and Error = %r\n",
749 MmcHostInstance->BlockIo.Media->BlockSize, Status));
750 return Status;
751 }
752
753 // Block Count (not used). Could return an error for SD card
754 if (MmcHostInstance->CardInfo.CardType == MMC_CARD) {
755 Status = MmcHost->SendCommand (MmcHost, MMC_CMD23, BlockCount);
756 if (EFI_ERROR (Status)) {
757 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(MMC_CMD23): Error, Status=%r\n", Status));
758 return Status;
759 }
760 }
761
762 return EFI_SUCCESS;
763 }