blob: 6f904d0984b10681c55b3485b7dd2ae132d3d324 [file] [log] [blame]
Vishal Bhoj82c80712015-12-15 21:13:33 +05301/** @file
2*
3* Copyright (c) 2011-2014, 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
20typedef 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)
53typedef 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
67UINT32 mEmmcRcaCount = 0;
68UINT32 CurrentMediaId = 0;
69
70STATIC
71EFI_STATUS
72EFIAPI
73EmmcGetDeviceState (
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 Rsp[4], RCA;
81
82 if (State == NULL)
83 return EFI_INVALID_PARAMETER;
84
85 Host = MmcHostInstance->MmcHost;
86 RCA = MmcHostInstance->CardInfo.RCA << RCA_SHIFT_OFFSET;
87 do {
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, (UINT32 *)&Rsp);
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 (Rsp[0] & 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 } while (!(Rsp[0] & MMC_R0_READY_FOR_DATA));
103 *State = MMC_R0_CURRENTSTATE(Rsp);
104 return EFI_SUCCESS;
105}
106
107STATIC
108EFI_STATUS
109EFIAPI
110EmmcSetEXTCSD (
111 IN MMC_HOST_INSTANCE *MmcHostInstance,
112 UINT32 ExtCmdIndex,
113 UINT32 Value
114 )
115{
116 EFI_MMC_HOST_PROTOCOL *Host;
117 EMMC_DEVICE_STATE State;
118 EFI_STATUS Status;
119 UINT32 Argument;
120
121 Host = MmcHostInstance->MmcHost;
122 Argument = (3 << 24) | ((ExtCmdIndex & 0xff) << 16) | ((Value & 0xff) << 8) | 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
139STATIC
140EFI_STATUS
141EFIAPI
142EmmcIdentificationMode (
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 EMMC_DEVICE_STATE State;
150 EFI_STATUS Status;
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 // 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 // MMC v4 specific
205 if (MmcHostInstance->CardInfo.CSDData.SPEC_VERS == 4) {
206 // Set 1-bit bus width for EXTCSD
207 Status = EmmcSetEXTCSD (MmcHostInstance, EXTCSD_BUS_WIDTH, EMMC_BUS_WIDTH_1BIT);
208 if (EFI_ERROR (Status)) {
209 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): Set extcsd bus width error, Status=%r.\n", Status));
210 return Status;
211 }
212
213 // Fetch ECSD
214 Status = Host->SendCommand (Host, MMC_CMD8, RCA);
215 if (EFI_ERROR (Status)) {
216 DEBUG ((EFI_D_ERROR, "EmmcIdentificationMode(): ECSD fetch error, Status=%r.\n", Status));
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 // Compute last block using bits [215:212] of the ECSD
234 Media->LastBlock = MmcHostInstance->CardInfo.ECSDData.SEC_COUNT - 1; // eMMC isn't supposed to report this for
235 // Cards <2GB in size, but the model does.
236
237 // Setup card type
238 MmcHostInstance->CardInfo.CardType = EMMC_CARD;
239 }
240 // Set up media
241 Media->BlockSize = EMMC_CARD_SIZE; // 512-byte support is mandatory for eMMC cards
242 Media->MediaId = MmcHostInstance->CardInfo.CIDData.PSN;
243 if (CurrentMediaId > Media->MediaId)
244 Media->MediaId = ++CurrentMediaId;
245 else
246 CurrentMediaId = Media->MediaId;
247 Media->ReadOnly = MmcHostInstance->CardInfo.CSDData.PERM_WRITE_PROTECT;
248 Media->LogicalBlocksPerPhysicalBlock = 1;
249 Media->IoAlign = 4;
250 return EFI_SUCCESS;
251}
252
253STATIC
254EFI_STATUS
255InitializeEmmcDevice (
256 IN MMC_HOST_INSTANCE *MmcHostInstance
257 )
258{
259 EFI_MMC_HOST_PROTOCOL *Host;
260 EFI_STATUS Status;
261 ECSD *ECSDData;
262 BOOLEAN Found = FALSE;
263 UINT32 BusClockFreq, Idx, Width;
264 UINT32 TimingMode[4] = {EMMCHS52DDR1V2, EMMCHS52DDR1V8, EMMCHS52, EMMCHS26};
265
266 Host = MmcHostInstance->MmcHost;
267 if (MmcHostInstance->CardInfo.CSDData.SPEC_VERS < 4)
268 return EFI_SUCCESS;
269 ECSDData = &MmcHostInstance->CardInfo.ECSDData;
270 if (ECSDData->DEVICE_TYPE == EMMCBACKWARD)
271 return EFI_SUCCESS;
272
273 Status = EmmcSetEXTCSD (MmcHostInstance, EXTCSD_HS_TIMING, EMMC_TIMING_HS);
274 if (EFI_ERROR (Status)) {
275 DEBUG ((DEBUG_ERROR, "InitializeEmmcDevice(): Failed to switch high speed mode, Status:%r.\n", Status));
276 return Status;
277 }
278
279 for (Idx = 0; Idx < 4; Idx++) {
280 switch (TimingMode[Idx]) {
281 case EMMCHS52DDR1V2:
282 case EMMCHS52DDR1V8:
283 Width = EMMC_BUS_WIDTH_DDR_8BIT;
284 BusClockFreq = 52000000;
285 break;
286 case EMMCHS52:
287 Width = EMMC_BUS_WIDTH_8BIT;
288 BusClockFreq = 52000000;
289 break;
290 case EMMCHS26:
291 Width = EMMC_BUS_WIDTH_8BIT;
292 BusClockFreq = 26000000;
293 break;
294 default:
295 return EFI_UNSUPPORTED;
296 }
297 Status = Host->SetIos (Host, BusClockFreq, 8, TimingMode[Idx]);
298 if (!EFI_ERROR (Status)) {
299 Found = TRUE;
300 break;
301 }
302 }
303 if (Found) {
304 Status = EmmcSetEXTCSD (MmcHostInstance, EXTCSD_BUS_WIDTH, Width);
305 if (EFI_ERROR (Status))
306 DEBUG ((DEBUG_ERROR, "InitializeEmmcDevice(): Failed to set EXTCSD bus width, Status:%r\n", Status));
307 }
308 return Status;
309}
310
311STATIC
312EFI_STATUS
313InitializeSdMmcDevice (
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 if (MmcHostInstance->CardInfo.CardType == SD_CARD_2_HIGH) {
351 CardSize = HC_MMC_CSD_GET_DEVICESIZE (Response);
352 NumBlocks = ((CardSize + 1) * 1024);
353 BlockSize = 1 << MMC_CSD_GET_READBLLEN (Response);
354 } else {
355 CardSize = MMC_CSD_GET_DEVICESIZE (Response);
356 NumBlocks = (CardSize + 1) * (1 << (MMC_CSD_GET_DEVICESIZEMULT (Response) + 2));
357 BlockSize = 1 << MMC_CSD_GET_READBLLEN (Response);
358 }
359
360 // For >=2G card, BlockSize may be 1K, but the transfer size is 512 bytes.
361 if (BlockSize > 512) {
362 NumBlocks = MultU64x32 (NumBlocks, BlockSize / 512);
363 BlockSize = 512;
364 }
365
366 MmcHostInstance->BlockIo.Media->LastBlock = (NumBlocks - 1);
367 MmcHostInstance->BlockIo.Media->BlockSize = BlockSize;
368 MmcHostInstance->BlockIo.Media->ReadOnly = MmcHost->IsReadOnly (MmcHost);
369 MmcHostInstance->BlockIo.Media->MediaPresent = TRUE;
370 MmcHostInstance->BlockIo.Media->MediaId = ++CurrentMediaId;
371
372 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
373 Status = MmcHost->SendCommand (MmcHost, MMC_CMD7, CmdArg);
374 if (EFI_ERROR (Status)) {
375 DEBUG((EFI_D_ERROR, "InitializeSdMmcDevice(MMC_CMD7): Error and Status = %r\n", Status));
376 return Status;
377 }
378
379 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, CmdArg);
380 if (EFI_ERROR (Status)) {
381 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD55): Error and Status = %r\n", Status));
382 return Status;
383 }
384 /* SCR */
385 Status = MmcHost->SendCommand (MmcHost, MMC_CMD51, 0);
386 if (EFI_ERROR (Status)) {
387 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD51): Error and Status = %r\n", Status));
388 return Status;
389 } else {
390 Status = MmcHost->ReadBlockData (MmcHost, 0, 8, Buffer);
391 if (EFI_ERROR (Status)) {
392 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD51): ReadBlockData Error and Status = %r\n", Status));
393 return Status;
394 }
395 CopyMem (&Scr, Buffer, 8);
396 if (Scr.SD_SPEC == 2) {
397 if (Scr.SD_SPEC3 == 1) {
398 if (Scr.SD_SPEC4 == 1) {
399 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 4.xx\n"));
400 } else {
401 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 3.0x\n"));
402 }
403 } else {
404 if (Scr.SD_SPEC4 == 0) {
405 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 2.0\n"));
406 } else {
407 DEBUG ((EFI_D_ERROR, "Found invalid SD Card\n"));
408 }
409 }
410 } else {
411 if ((Scr.SD_SPEC3 == 0) && (Scr.SD_SPEC4 == 0)) {
412 if (Scr.SD_SPEC == 1) {
413 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 1.10\n"));
414 } else {
415 DEBUG ((EFI_D_INFO, "Found SD Card for Spec Version 1.0\n"));
416 }
417 } else {
418 DEBUG ((EFI_D_ERROR, "Found invalid SD Card\n"));
419 }
420 }
421 }
422 if (CccSwitch) {
423 /* SD Switch, Mode:1, Group:0, Value:1 */
424 CmdArg = 1 << 31 | 0x00FFFFFF;
425 CmdArg &= ~(0xF << (0 * 4));
426 CmdArg |= 1 << (0 * 4);
427 Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, CmdArg);
428 if (EFI_ERROR (Status)) {
429 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): Error and Status = %r\n", Status));
430 return Status;
431 } else {
432 Status = MmcHost->ReadBlockData (MmcHost, 0, 64, Buffer);
433 if (EFI_ERROR (Status)) {
434 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): ReadBlockData Error and Status = %r\n", Status));
435 return Status;
436 }
437 }
438 }
439 if (Scr.SD_BUS_WIDTHS & SD_BUS_WIDTH_4BIT) {
440 CmdArg = MmcHostInstance->CardInfo.RCA << 16;
441 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, CmdArg);
442 if (EFI_ERROR (Status)) {
443 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD55): Error and Status = %r\n", Status));
444 return Status;
445 }
446 /* Width: 4 */
447 Status = MmcHost->SendCommand (MmcHost, MMC_CMD6, 2);
448 if (EFI_ERROR (Status)) {
449 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD6): Error and Status = %r\n", Status));
450 return Status;
451 }
452 }
453 Status = MmcHost->SetIos (MmcHost, 24 * 1000 * 1000, 4, EMMCBACKWARD);
454 if (EFI_ERROR (Status)) {
455 DEBUG ((EFI_D_ERROR, "%a(SetIos): Error and Status = %r\n", Status));
456 return Status;
457 }
458 return EFI_SUCCESS;
459}
460
461STATIC
462EFI_STATUS
463EFIAPI
464MmcIdentificationMode (
465 IN MMC_HOST_INSTANCE *MmcHostInstance
466 )
467{
468 EFI_STATUS Status;
469 UINT32 Response[4];
470 UINTN Timeout;
471 UINTN CmdArg;
472 BOOLEAN IsHCS;
473 EFI_MMC_HOST_PROTOCOL *MmcHost;
474 OCR_RESPONSE OcrResponse;
475
476 MmcHost = MmcHostInstance->MmcHost;
477 CmdArg = 0;
478 IsHCS = FALSE;
479
480 if (MmcHost == NULL) {
481 return EFI_INVALID_PARAMETER;
482 }
483
484 // We can get into this function if we restart the identification mode
485 if (MmcHostInstance->State == MmcHwInitializationState) {
486 // Initialize the MMC Host HW
487 Status = MmcNotifyState (MmcHostInstance, MmcHwInitializationState);
488 if (EFI_ERROR (Status)) {
489 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcHwInitializationState, Status=%r.\n", Status));
490 return Status;
491 }
492 }
493
494 Status = MmcHost->SendCommand (MmcHost, MMC_CMD0, 0);
495 if (EFI_ERROR (Status)) {
496 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD0): Error, Status=%r.\n", Status));
497 return Status;
498 }
499 Status = MmcNotifyState (MmcHostInstance, MmcIdleState);
500 if (EFI_ERROR (Status)) {
501 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdleState, Status=%r.\n", Status));
502 return Status;
503 }
504
505 // Send CMD1 to get OCR (MMC)
506 // This command only valid for MMC and eMMC
507 Timeout = MAX_RETRY_COUNT;
508 do {
509 Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, EMMC_CMD1_CAPACITY_GREATER_THAN_2GB);
510 if (EFI_ERROR (Status))
511 break;
512 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, (UINT32 *)&OcrResponse);
513 if (EFI_ERROR (Status)) {
514 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));
515 return Status;
516 }
517 Timeout--;
518 } while (!OcrResponse.Ocr.PowerUp && (Timeout > 0));
519 if (Status == EFI_SUCCESS) {
520 if (!OcrResponse.Ocr.PowerUp) {
521 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD1): Card initialisation failure, Status=%r.\n", Status));
522 return EFI_DEVICE_ERROR;
523 }
524 OcrResponse.Ocr.PowerUp = 0;
525 if (OcrResponse.Raw == EMMC_CMD1_CAPACITY_GREATER_THAN_2GB) {
526 MmcHostInstance->CardInfo.OCRData.AccessMode = BIT1;
527 }
528 else {
529 MmcHostInstance->CardInfo.OCRData.AccessMode = 0x0;
530 }
531 // Check whether MMC or eMMC
532 if (OcrResponse.Raw == EMMC_CMD1_CAPACITY_GREATER_THAN_2GB ||
533 OcrResponse.Raw == EMMC_CMD1_CAPACITY_LESS_THAN_2GB) {
534 return EmmcIdentificationMode (MmcHostInstance, OcrResponse);
535 }
536 }
537
538 // Are we using SDIO ?
539 Status = MmcHost->SendCommand (MmcHost, MMC_CMD5, 0);
540 if (Status == EFI_SUCCESS) {
541 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD5): Error - SDIO not supported, Status=%r.\n", Status));
542 return EFI_UNSUPPORTED;
543 }
544
545 // Check which kind of card we are using. Ver2.00 or later SD Memory Card (PL180 is SD v1.1)
546 CmdArg = (0x0UL << 12 | BIT8 | 0xCEUL << 0);
547 Status = MmcHost->SendCommand (MmcHost, MMC_CMD8, CmdArg);
548 if (Status == EFI_SUCCESS) {
549 DEBUG ((EFI_D_ERROR, "Card is SD2.0 => Supports high capacity\n"));
550 IsHCS = TRUE;
551 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R7, Response);
552 if (EFI_ERROR (Status)) {
553 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive response to CMD8, Status=%r.\n", Status));
554 return Status;
555 }
556 PrintResponseR1 (Response[0]);
557 // Check if it is valid response
558 if (Response[0] != CmdArg) {
559 DEBUG ((EFI_D_ERROR, "The Card is not usable\n"));
560 return EFI_UNSUPPORTED;
561 }
562 } else {
563 DEBUG ((EFI_D_ERROR, "Not a SD2.0 Card\n"));
564 }
565
566 // We need to wait for the MMC or SD card is ready => (gCardInfo.OCRData.PowerUp == 1)
567 Timeout = MAX_RETRY_COUNT;
568 while (Timeout > 0) {
569 // SD Card or MMC Card ? CMD55 indicates to the card that the next command is an application specific command
570 Status = MmcHost->SendCommand (MmcHost, MMC_CMD55, 0);
571 if (Status == EFI_SUCCESS) {
572 DEBUG ((EFI_D_INFO, "Card should be SD\n"));
573 if (IsHCS) {
574 MmcHostInstance->CardInfo.CardType = SD_CARD_2;
575 } else {
576 MmcHostInstance->CardInfo.CardType = SD_CARD;
577 }
578
579 // Note: The first time CmdArg will be zero
580 CmdArg = ((UINTN *) &(MmcHostInstance->CardInfo.OCRData))[0];
581 if (IsHCS) {
582 CmdArg |= BIT30;
583 }
584 Status = MmcHost->SendCommand (MmcHost, MMC_ACMD41, CmdArg);
585 if (!EFI_ERROR (Status)) {
586 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, Response);
587 if (EFI_ERROR (Status)) {
588 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));
589 return Status;
590 }
591 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];
592 }
593 } else {
594 DEBUG ((EFI_D_INFO, "Card should be MMC\n"));
595 MmcHostInstance->CardInfo.CardType = MMC_CARD;
596
597 Status = MmcHost->SendCommand (MmcHost, MMC_CMD1, 0x800000);
598 if (!EFI_ERROR (Status)) {
599 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_OCR, Response);
600 if (EFI_ERROR (Status)) {
601 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive OCR, Status=%r.\n", Status));
602 return Status;
603 }
604 ((UINT32 *) &(MmcHostInstance->CardInfo.OCRData))[0] = Response[0];
605 }
606 }
607
608 if (!EFI_ERROR (Status)) {
609 if (!MmcHostInstance->CardInfo.OCRData.PowerUp) {
610 MicroSecondDelay (1);
611 Timeout--;
612 } else {
613 if ((MmcHostInstance->CardInfo.CardType == SD_CARD_2) && (MmcHostInstance->CardInfo.OCRData.AccessMode & BIT1)) {
614 MmcHostInstance->CardInfo.CardType = SD_CARD_2_HIGH;
615 DEBUG ((EFI_D_ERROR, "High capacity card.\n"));
616 }
617 break; // The MMC/SD card is ready. Continue the Identification Mode
618 }
619 } else {
620 MicroSecondDelay (1);
621 Timeout--;
622 }
623 }
624
625 if (Timeout == 0) {
626 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(): No Card\n"));
627 return EFI_NO_MEDIA;
628 } else {
629 PrintOCR (Response[0]);
630 }
631
632 Status = MmcNotifyState (MmcHostInstance, MmcReadyState);
633 if (EFI_ERROR (Status)) {
634 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcReadyState\n"));
635 return Status;
636 }
637
638 Status = MmcHost->SendCommand (MmcHost, MMC_CMD2, 0);
639 if (EFI_ERROR (Status)) {
640 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD2): Error\n"));
641 return Status;
642 }
643 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_CID, Response);
644 if (EFI_ERROR (Status)) {
645 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive CID, Status=%r.\n", Status));
646 return Status;
647 }
648
649 PrintCID (Response);
650
651 Status = MmcHost->NotifyState (MmcHost, MmcIdentificationState);
652 if (EFI_ERROR (Status)) {
653 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcIdentificationState\n"));
654 return Status;
655 }
656
657 //
658 // Note, SD specifications say that "if the command execution causes a state change, it
659 // will be visible to the host in the response to the next command"
660 // The status returned for this CMD3 will be 2 - identification
661 //
662 CmdArg = 1;
663 Status = MmcHost->SendCommand (MmcHost, MMC_CMD3, CmdArg);
664 if (EFI_ERROR (Status)) {
665 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode(MMC_CMD3): Error\n"));
666 return Status;
667 }
668
669 Status = MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_RCA, Response);
670 if (EFI_ERROR (Status)) {
671 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Failed to receive RCA, Status=%r.\n", Status));
672 return Status;
673 }
674 PrintRCA (Response[0]);
675
676 // For MMC card, RCA is assigned by CMD3 while CMD3 dumps the RCA for SD card
677 if (MmcHostInstance->CardInfo.CardType != MMC_CARD) {
678 MmcHostInstance->CardInfo.RCA = Response[0] >> 16;
679 } else {
680 MmcHostInstance->CardInfo.RCA = CmdArg;
681 }
682 Status = MmcNotifyState (MmcHostInstance, MmcStandByState);
683 if (EFI_ERROR (Status)) {
684 DEBUG ((EFI_D_ERROR, "MmcIdentificationMode() : Error MmcStandByState\n"));
685 return Status;
686 }
687
688 return EFI_SUCCESS;
689}
690
691EFI_STATUS
692InitializeMmcDevice (
693 IN MMC_HOST_INSTANCE *MmcHostInstance
694 )
695{
696 EFI_STATUS Status;
697 EFI_MMC_HOST_PROTOCOL *MmcHost;
698 UINTN BlockCount;
699
700 BlockCount = 1;
701 MmcHost = MmcHostInstance->MmcHost;
702
703 Status = MmcIdentificationMode (MmcHostInstance);
704 if (EFI_ERROR (Status)) {
705 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(): Error in Identification Mode, Status=%r\n", Status));
706 return Status;
707 }
708
709 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);
710 if (EFI_ERROR (Status)) {
711 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(): Error MmcTransferState, Status=%r\n", Status));
712 return Status;
713 }
714
715 if (MmcHostInstance->CardInfo.CardType == EMMC_CARD) {
716 Status = InitializeEmmcDevice (MmcHostInstance);
717 } else {
718 Status = InitializeSdMmcDevice (MmcHostInstance);
719 }
720 if (EFI_ERROR (Status)) {
721 return Status;
722 }
723
724 // Set Block Length
725 Status = MmcHost->SendCommand (MmcHost, MMC_CMD16, MmcHostInstance->BlockIo.Media->BlockSize);
726 if (EFI_ERROR (Status)) {
727 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(MMC_CMD16): Error MmcHostInstance->BlockIo.Media->BlockSize: %d and Error = %r\n",
728 MmcHostInstance->BlockIo.Media->BlockSize, Status));
729 return Status;
730 }
731
732 // Block Count (not used). Could return an error for SD card
733 if (MmcHostInstance->CardInfo.CardType == MMC_CARD) {
734 Status = MmcHost->SendCommand (MmcHost, MMC_CMD23, BlockCount);
735 if (EFI_ERROR (Status)) {
736 DEBUG((EFI_D_ERROR, "InitializeMmcDevice(MMC_CMD23): Error, Status=%r\n", Status));
737 return Status;
738 }
739 }
740
741 return EFI_SUCCESS;
742}