]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/EmmcDevice.c
MdeModulePkg/SdMmcPciHcDxe: Add SDMMC HC v4 and above Support.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / SdMmcPciHcDxe / EmmcDevice.c
1 /** @file
2 This file provides some helper functions which are specific for EMMC device.
3
4 Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
5 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "SdMmcPciHcDxe.h"
17
18 /**
19 Send command GO_IDLE_STATE (CMD0 with argument of 0x00000000) to the device to
20 make it go to Idle State.
21
22 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
23
24 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
25 @param[in] Slot The slot number of the SD card to send the command to.
26
27 @retval EFI_SUCCESS The EMMC device is reset correctly.
28 @retval Others The device reset fails.
29
30 **/
31 EFI_STATUS
32 EmmcReset (
33 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
34 IN UINT8 Slot
35 )
36 {
37 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
38 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
39 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
40 EFI_STATUS Status;
41
42 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
43 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
44 ZeroMem (&Packet, sizeof (Packet));
45
46 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
47 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
48 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
49
50 SdMmcCmdBlk.CommandIndex = EMMC_GO_IDLE_STATE;
51 SdMmcCmdBlk.CommandType = SdMmcCommandTypeBc;
52 SdMmcCmdBlk.ResponseType = 0;
53 SdMmcCmdBlk.CommandArgument = 0;
54
55 gBS->Stall (1000);
56
57 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
58
59 return Status;
60 }
61
62 /**
63 Send command SEND_OP_COND to the EMMC device to get the data of the OCR register.
64
65 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
66
67 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
68 @param[in] Slot The slot number of the SD card to send the command to.
69 @param[in, out] Argument On input, the argument of SEND_OP_COND is to send to the device.
70 On output, the argument is the value of OCR register.
71
72 @retval EFI_SUCCESS The operation is done correctly.
73 @retval Others The operation fails.
74
75 **/
76 EFI_STATUS
77 EmmcGetOcr (
78 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
79 IN UINT8 Slot,
80 IN OUT UINT32 *Argument
81 )
82 {
83 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
84 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
85 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
86 EFI_STATUS Status;
87
88 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
89 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
90 ZeroMem (&Packet, sizeof (Packet));
91
92 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
93 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
94 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
95
96 SdMmcCmdBlk.CommandIndex = EMMC_SEND_OP_COND;
97 SdMmcCmdBlk.CommandType = SdMmcCommandTypeBcr;
98 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR3;
99 SdMmcCmdBlk.CommandArgument = *Argument;
100
101 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
102 if (!EFI_ERROR (Status)) {
103 //
104 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
105 //
106 *Argument = SdMmcStatusBlk.Resp0;
107 }
108
109 return Status;
110 }
111
112 /**
113 Broadcast command ALL_SEND_CID to the bus to ask all the EMMC devices to send the
114 data of their CID registers.
115
116 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
117
118 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
119 @param[in] Slot The slot number of the SD card to send the command to.
120
121 @retval EFI_SUCCESS The operation is done correctly.
122 @retval Others The operation fails.
123
124 **/
125 EFI_STATUS
126 EmmcGetAllCid (
127 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
128 IN UINT8 Slot
129 )
130 {
131 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
132 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
133 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
134 EFI_STATUS Status;
135
136 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
137 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
138 ZeroMem (&Packet, sizeof (Packet));
139
140 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
141 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
142 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
143
144 SdMmcCmdBlk.CommandIndex = EMMC_ALL_SEND_CID;
145 SdMmcCmdBlk.CommandType = SdMmcCommandTypeBcr;
146 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR2;
147 SdMmcCmdBlk.CommandArgument = 0;
148
149 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
150
151 return Status;
152 }
153
154 /**
155 Send command SET_RELATIVE_ADDR to the EMMC device to assign a Relative device
156 Address (RCA).
157
158 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
159
160 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
161 @param[in] Slot The slot number of the SD card to send the command to.
162 @param[in] Rca The relative device address to be assigned.
163
164 @retval EFI_SUCCESS The operation is done correctly.
165 @retval Others The operation fails.
166
167 **/
168 EFI_STATUS
169 EmmcSetRca (
170 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
171 IN UINT8 Slot,
172 IN UINT16 Rca
173 )
174 {
175 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
176 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
177 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
178 EFI_STATUS Status;
179
180 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
181 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
182 ZeroMem (&Packet, sizeof (Packet));
183
184 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
185 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
186 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
187
188 SdMmcCmdBlk.CommandIndex = EMMC_SET_RELATIVE_ADDR;
189 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;
190 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;
191 SdMmcCmdBlk.CommandArgument = (UINT32)Rca << 16;
192
193 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
194
195 return Status;
196 }
197
198 /**
199 Send command SEND_CSD to the EMMC device to get the data of the CSD register.
200
201 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
202
203 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
204 @param[in] Slot The slot number of the SD card to send the command to.
205 @param[in] Rca The relative device address of selected device.
206 @param[out] Csd The buffer to store the content of the CSD register.
207 Note the caller should ignore the lowest byte of this
208 buffer as the content of this byte is meaningless even
209 if the operation succeeds.
210
211 @retval EFI_SUCCESS The operation is done correctly.
212 @retval Others The operation fails.
213
214 **/
215 EFI_STATUS
216 EmmcGetCsd (
217 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
218 IN UINT8 Slot,
219 IN UINT16 Rca,
220 OUT EMMC_CSD *Csd
221 )
222 {
223 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
224 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
225 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
226 EFI_STATUS Status;
227
228 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
229 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
230 ZeroMem (&Packet, sizeof (Packet));
231
232 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
233 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
234 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
235
236 SdMmcCmdBlk.CommandIndex = EMMC_SEND_CSD;
237 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;
238 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR2;
239 SdMmcCmdBlk.CommandArgument = (UINT32)Rca << 16;
240
241 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
242 if (!EFI_ERROR (Status)) {
243 //
244 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
245 //
246 CopyMem (((UINT8*)Csd) + 1, &SdMmcStatusBlk.Resp0, sizeof (EMMC_CSD) - 1);
247 }
248
249 return Status;
250 }
251
252 /**
253 Send command SELECT_DESELECT_CARD to the EMMC device to select/deselect it.
254
255 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
256
257 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
258 @param[in] Slot The slot number of the SD card to send the command to.
259 @param[in] Rca The relative device address of selected device.
260
261 @retval EFI_SUCCESS The operation is done correctly.
262 @retval Others The operation fails.
263
264 **/
265 EFI_STATUS
266 EmmcSelect (
267 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
268 IN UINT8 Slot,
269 IN UINT16 Rca
270 )
271 {
272 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
273 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
274 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
275 EFI_STATUS Status;
276
277 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
278 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
279 ZeroMem (&Packet, sizeof (Packet));
280
281 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
282 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
283 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
284
285 SdMmcCmdBlk.CommandIndex = EMMC_SELECT_DESELECT_CARD;
286 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;
287 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;
288 SdMmcCmdBlk.CommandArgument = (UINT32)Rca << 16;
289
290 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
291
292 return Status;
293 }
294
295 /**
296 Send command SEND_EXT_CSD to the EMMC device to get the data of the EXT_CSD register.
297
298 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
299
300 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
301 @param[in] Slot The slot number of the SD card to send the command to.
302 @param[out] ExtCsd The buffer to store the content of the EXT_CSD register.
303
304 @retval EFI_SUCCESS The operation is done correctly.
305 @retval Others The operation fails.
306
307 **/
308 EFI_STATUS
309 EmmcGetExtCsd (
310 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
311 IN UINT8 Slot,
312 OUT EMMC_EXT_CSD *ExtCsd
313 )
314 {
315 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
316 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
317 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
318 EFI_STATUS Status;
319
320 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
321 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
322 ZeroMem (&Packet, sizeof (Packet));
323
324 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
325 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
326 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
327
328 SdMmcCmdBlk.CommandIndex = EMMC_SEND_EXT_CSD;
329 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAdtc;
330 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;
331 SdMmcCmdBlk.CommandArgument = 0x00000000;
332
333 Packet.InDataBuffer = ExtCsd;
334 Packet.InTransferLength = sizeof (EMMC_EXT_CSD);
335
336 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
337 return Status;
338 }
339
340 /**
341 Send command SWITCH to the EMMC device to switch the mode of operation of the
342 selected Device or modifies the EXT_CSD registers.
343
344 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
345
346 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
347 @param[in] Slot The slot number of the SD card to send the command to.
348 @param[in] Access The access mode of SWTICH command.
349 @param[in] Index The offset of the field to be access.
350 @param[in] Value The value to be set to the specified field of EXT_CSD register.
351 @param[in] CmdSet The value of CmdSet field of EXT_CSD register.
352
353 @retval EFI_SUCCESS The operation is done correctly.
354 @retval Others The operation fails.
355
356 **/
357 EFI_STATUS
358 EmmcSwitch (
359 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
360 IN UINT8 Slot,
361 IN UINT8 Access,
362 IN UINT8 Index,
363 IN UINT8 Value,
364 IN UINT8 CmdSet
365 )
366 {
367 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
368 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
369 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
370 EFI_STATUS Status;
371
372 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
373 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
374 ZeroMem (&Packet, sizeof (Packet));
375
376 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
377 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
378 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
379
380 SdMmcCmdBlk.CommandIndex = EMMC_SWITCH;
381 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;
382 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1b;
383 SdMmcCmdBlk.CommandArgument = (Access << 24) | (Index << 16) | (Value << 8) | CmdSet;
384
385 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
386
387 return Status;
388 }
389
390 /**
391 Send command SEND_STATUS to the addressed EMMC device to get its status register.
392
393 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
394
395 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
396 @param[in] Slot The slot number of the SD card to send the command to.
397 @param[in] Rca The relative device address of addressed device.
398 @param[out] DevStatus The returned device status.
399
400 @retval EFI_SUCCESS The operation is done correctly.
401 @retval Others The operation fails.
402
403 **/
404 EFI_STATUS
405 EmmcSendStatus (
406 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
407 IN UINT8 Slot,
408 IN UINT16 Rca,
409 OUT UINT32 *DevStatus
410 )
411 {
412 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
413 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
414 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
415 EFI_STATUS Status;
416
417 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
418 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
419 ZeroMem (&Packet, sizeof (Packet));
420
421 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
422 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
423 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
424
425 SdMmcCmdBlk.CommandIndex = EMMC_SEND_STATUS;
426 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAc;
427 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;
428 SdMmcCmdBlk.CommandArgument = (UINT32)Rca << 16;
429
430 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
431 if (!EFI_ERROR (Status)) {
432 *DevStatus = SdMmcStatusBlk.Resp0;
433 }
434
435 return Status;
436 }
437
438 /**
439 Send command SEND_TUNING_BLOCK to the EMMC device for HS200 optimal sampling point
440 detection.
441
442 It may be sent up to 40 times until the host finishes the tuning procedure.
443
444 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 for details.
445
446 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
447 @param[in] Slot The slot number of the SD card to send the command to.
448 @param[in] BusWidth The bus width to work.
449
450 @retval EFI_SUCCESS The operation is done correctly.
451 @retval Others The operation fails.
452
453 **/
454 EFI_STATUS
455 EmmcSendTuningBlk (
456 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
457 IN UINT8 Slot,
458 IN UINT8 BusWidth
459 )
460 {
461 EFI_SD_MMC_COMMAND_BLOCK SdMmcCmdBlk;
462 EFI_SD_MMC_STATUS_BLOCK SdMmcStatusBlk;
463 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET Packet;
464 EFI_STATUS Status;
465 UINT8 TuningBlock[128];
466
467 ZeroMem (&SdMmcCmdBlk, sizeof (SdMmcCmdBlk));
468 ZeroMem (&SdMmcStatusBlk, sizeof (SdMmcStatusBlk));
469 ZeroMem (&Packet, sizeof (Packet));
470
471 Packet.SdMmcCmdBlk = &SdMmcCmdBlk;
472 Packet.SdMmcStatusBlk = &SdMmcStatusBlk;
473 Packet.Timeout = SD_MMC_HC_GENERIC_TIMEOUT;
474
475 SdMmcCmdBlk.CommandIndex = EMMC_SEND_TUNING_BLOCK;
476 SdMmcCmdBlk.CommandType = SdMmcCommandTypeAdtc;
477 SdMmcCmdBlk.ResponseType = SdMmcResponseTypeR1;
478 SdMmcCmdBlk.CommandArgument = 0;
479
480 Packet.InDataBuffer = TuningBlock;
481 if (BusWidth == 8) {
482 Packet.InTransferLength = sizeof (TuningBlock);
483 } else {
484 Packet.InTransferLength = 64;
485 }
486
487 Status = SdMmcPassThruPassThru (PassThru, Slot, &Packet, NULL);
488
489 return Status;
490 }
491
492 /**
493 Tunning the clock to get HS200 optimal sampling point.
494
495 Command SEND_TUNING_BLOCK may be sent up to 40 times until the host finishes the
496 tuning procedure.
497
498 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
499 Simplified Spec 3.0 Figure 2-29 for details.
500
501 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
502 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
503 @param[in] Slot The slot number of the SD card to send the command to.
504 @param[in] BusWidth The bus width to work.
505
506 @retval EFI_SUCCESS The operation is done correctly.
507 @retval Others The operation fails.
508
509 **/
510 EFI_STATUS
511 EmmcTuningClkForHs200 (
512 IN EFI_PCI_IO_PROTOCOL *PciIo,
513 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
514 IN UINT8 Slot,
515 IN UINT8 BusWidth
516 )
517 {
518 EFI_STATUS Status;
519 UINT8 HostCtrl2;
520 UINT8 Retry;
521
522 //
523 // Notify the host that the sampling clock tuning procedure starts.
524 //
525 HostCtrl2 = BIT6;
526 Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
527 if (EFI_ERROR (Status)) {
528 return Status;
529 }
530 //
531 // Ask the device to send a sequence of tuning blocks till the tuning procedure is done.
532 //
533 Retry = 0;
534 do {
535 Status = EmmcSendTuningBlk (PassThru, Slot, BusWidth);
536 if (EFI_ERROR (Status)) {
537 DEBUG ((DEBUG_ERROR, "EmmcTuningClkForHs200: Send tuning block fails with %r\n", Status));
538 return Status;
539 }
540
541 Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, TRUE, sizeof (HostCtrl2), &HostCtrl2);
542 if (EFI_ERROR (Status)) {
543 return Status;
544 }
545
546 if ((HostCtrl2 & (BIT6 | BIT7)) == 0) {
547 break;
548 }
549
550 if ((HostCtrl2 & (BIT6 | BIT7)) == BIT7) {
551 return EFI_SUCCESS;
552 }
553 } while (++Retry < 40);
554
555 DEBUG ((DEBUG_ERROR, "EmmcTuningClkForHs200: Send tuning block fails at %d times with HostCtrl2 %02x\n", Retry, HostCtrl2));
556 //
557 // Abort the tuning procedure and reset the tuning circuit.
558 //
559 HostCtrl2 = (UINT8)~(BIT6 | BIT7);
560 Status = SdMmcHcAndMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
561 if (EFI_ERROR (Status)) {
562 return Status;
563 }
564 return EFI_DEVICE_ERROR;
565 }
566
567 /**
568 Switch the bus width to specified width.
569
570 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.9 and SD Host Controller
571 Simplified Spec 3.0 Figure 3-7 for details.
572
573 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
574 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
575 @param[in] Slot The slot number of the SD card to send the command to.
576 @param[in] Rca The relative device address to be assigned.
577 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
578 use single data rate data simpling method.
579 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
580
581 @retval EFI_SUCCESS The operation is done correctly.
582 @retval Others The operation fails.
583
584 **/
585 EFI_STATUS
586 EmmcSwitchBusWidth (
587 IN EFI_PCI_IO_PROTOCOL *PciIo,
588 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
589 IN UINT8 Slot,
590 IN UINT16 Rca,
591 IN BOOLEAN IsDdr,
592 IN UINT8 BusWidth
593 )
594 {
595 EFI_STATUS Status;
596 UINT8 Access;
597 UINT8 Index;
598 UINT8 Value;
599 UINT8 CmdSet;
600 UINT32 DevStatus;
601
602 //
603 // Write Byte, the Value field is written into the byte pointed by Index.
604 //
605 Access = 0x03;
606 Index = OFFSET_OF (EMMC_EXT_CSD, BusWidth);
607 if (BusWidth == 4) {
608 Value = 1;
609 } else if (BusWidth == 8) {
610 Value = 2;
611 } else {
612 return EFI_INVALID_PARAMETER;
613 }
614
615 if (IsDdr) {
616 Value += 4;
617 }
618
619 CmdSet = 0;
620 Status = EmmcSwitch (PassThru, Slot, Access, Index, Value, CmdSet);
621 if (EFI_ERROR (Status)) {
622 DEBUG ((DEBUG_ERROR, "EmmcSwitchBusWidth: Switch to bus width %d fails with %r\n", BusWidth, Status));
623 return Status;
624 }
625
626 Status = EmmcSendStatus (PassThru, Slot, Rca, &DevStatus);
627 if (EFI_ERROR (Status)) {
628 DEBUG ((DEBUG_ERROR, "EmmcSwitchBusWidth: Send status fails with %r\n", Status));
629 return Status;
630 }
631 //
632 // Check the switch operation is really successful or not.
633 //
634 if ((DevStatus & BIT7) != 0) {
635 DEBUG ((DEBUG_ERROR, "EmmcSwitchBusWidth: The switch operation fails as DevStatus is 0x%08x\n", DevStatus));
636 return EFI_DEVICE_ERROR;
637 }
638
639 Status = SdMmcHcSetBusWidth (PciIo, Slot, BusWidth);
640
641 return Status;
642 }
643
644 /**
645 Switch the clock frequency to the specified value.
646
647 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6 and SD Host Controller
648 Simplified Spec 3.0 Figure 3-3 for details.
649
650 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
651 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
652 @param[in] Slot The slot number of the SD card to send the command to.
653 @param[in] Rca The relative device address to be assigned.
654 @param[in] HsTiming The value to be written to HS_TIMING field of EXT_CSD register.
655 @param[in] Timing The bus mode timing indicator.
656 @param[in] ClockFreq The max clock frequency to be set, the unit is MHz.
657
658 @retval EFI_SUCCESS The operation is done correctly.
659 @retval Others The operation fails.
660
661 **/
662 EFI_STATUS
663 EmmcSwitchClockFreq (
664 IN EFI_PCI_IO_PROTOCOL *PciIo,
665 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
666 IN UINT8 Slot,
667 IN UINT16 Rca,
668 IN UINT8 HsTiming,
669 IN SD_MMC_BUS_MODE Timing,
670 IN UINT32 ClockFreq
671 )
672 {
673 EFI_STATUS Status;
674 UINT8 Access;
675 UINT8 Index;
676 UINT8 Value;
677 UINT8 CmdSet;
678 UINT32 DevStatus;
679 SD_MMC_HC_PRIVATE_DATA *Private;
680
681 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
682 //
683 // Write Byte, the Value field is written into the byte pointed by Index.
684 //
685 Access = 0x03;
686 Index = OFFSET_OF (EMMC_EXT_CSD, HsTiming);
687 Value = HsTiming;
688 CmdSet = 0;
689
690 Status = EmmcSwitch (PassThru, Slot, Access, Index, Value, CmdSet);
691 if (EFI_ERROR (Status)) {
692 DEBUG ((DEBUG_ERROR, "EmmcSwitchClockFreq: Switch to hstiming %d fails with %r\n", HsTiming, Status));
693 return Status;
694 }
695
696 Status = EmmcSendStatus (PassThru, Slot, Rca, &DevStatus);
697 if (EFI_ERROR (Status)) {
698 DEBUG ((DEBUG_ERROR, "EmmcSwitchClockFreq: Send status fails with %r\n", Status));
699 return Status;
700 }
701 //
702 // Check the switch operation is really successful or not.
703 //
704 if ((DevStatus & BIT7) != 0) {
705 DEBUG ((DEBUG_ERROR, "EmmcSwitchClockFreq: The switch operation fails as DevStatus is 0x%08x\n", DevStatus));
706 return EFI_DEVICE_ERROR;
707 }
708 //
709 // Convert the clock freq unit from MHz to KHz.
710 //
711 Status = SdMmcHcClockSupply (PciIo, Slot, ClockFreq * 1000, Private->BaseClkFreq[Slot], Private->ControllerVersion[Slot]);
712 if (EFI_ERROR (Status)) {
713 return Status;
714 }
715
716 if (mOverride != NULL && mOverride->NotifyPhase != NULL) {
717 Status = mOverride->NotifyPhase (
718 Private->ControllerHandle,
719 Slot,
720 EdkiiSdMmcSwitchClockFreqPost,
721 &Timing
722 );
723 if (EFI_ERROR (Status)) {
724 DEBUG ((
725 DEBUG_ERROR,
726 "%a: SD/MMC switch clock freq post notifier callback failed - %r\n",
727 __FUNCTION__,
728 Status
729 ));
730 return Status;
731 }
732 }
733
734 return Status;
735 }
736
737 /**
738 Switch to the High Speed timing according to request.
739
740 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
741 Simplified Spec 3.0 Figure 2-29 for details.
742
743 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
744 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
745 @param[in] Slot The slot number of the SD card to send the command to.
746 @param[in] Rca The relative device address to be assigned.
747 @param[in] ClockFreq The max clock frequency to be set.
748 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
749 use single data rate data simpling method.
750 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
751
752 @retval EFI_SUCCESS The operation is done correctly.
753 @retval Others The operation fails.
754
755 **/
756 EFI_STATUS
757 EmmcSwitchToHighSpeed (
758 IN EFI_PCI_IO_PROTOCOL *PciIo,
759 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
760 IN UINT8 Slot,
761 IN UINT16 Rca,
762 IN UINT32 ClockFreq,
763 IN BOOLEAN IsDdr,
764 IN UINT8 BusWidth
765 )
766 {
767 EFI_STATUS Status;
768 UINT8 HsTiming;
769 UINT8 HostCtrl1;
770 SD_MMC_BUS_MODE Timing;
771 SD_MMC_HC_PRIVATE_DATA *Private;
772
773 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
774
775 Status = EmmcSwitchBusWidth (PciIo, PassThru, Slot, Rca, IsDdr, BusWidth);
776 if (EFI_ERROR (Status)) {
777 return Status;
778 }
779 //
780 // Set to Hight Speed timing
781 //
782 HostCtrl1 = BIT2;
783 Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
784 if (EFI_ERROR (Status)) {
785 return Status;
786 }
787
788 if (IsDdr) {
789 Timing = SdMmcMmcHsDdr;
790 } else if (ClockFreq == 52) {
791 Timing = SdMmcMmcHsSdr;
792 } else {
793 Timing = SdMmcMmcLegacy;
794 }
795
796 Status = SdMmcHcUhsSignaling (Private->ControllerHandle, PciIo, Slot, Timing);
797 if (EFI_ERROR (Status)) {
798 return Status;
799 }
800
801 HsTiming = 1;
802 Status = EmmcSwitchClockFreq (PciIo, PassThru, Slot, Rca, HsTiming, Timing, ClockFreq);
803
804 return Status;
805 }
806
807 /**
808 Switch to the HS200 timing according to request.
809
810 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
811 Simplified Spec 3.0 Figure 2-29 for details.
812
813 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
814 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
815 @param[in] Slot The slot number of the SD card to send the command to.
816 @param[in] Rca The relative device address to be assigned.
817 @param[in] ClockFreq The max clock frequency to be set.
818 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
819
820 @retval EFI_SUCCESS The operation is done correctly.
821 @retval Others The operation fails.
822
823 **/
824 EFI_STATUS
825 EmmcSwitchToHS200 (
826 IN EFI_PCI_IO_PROTOCOL *PciIo,
827 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
828 IN UINT8 Slot,
829 IN UINT16 Rca,
830 IN UINT32 ClockFreq,
831 IN UINT8 BusWidth
832 )
833 {
834 EFI_STATUS Status;
835 UINT8 HsTiming;
836 UINT16 ClockCtrl;
837 SD_MMC_BUS_MODE Timing;
838 SD_MMC_HC_PRIVATE_DATA *Private;
839
840 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
841
842 if ((BusWidth != 4) && (BusWidth != 8)) {
843 return EFI_INVALID_PARAMETER;
844 }
845
846 Status = EmmcSwitchBusWidth (PciIo, PassThru, Slot, Rca, FALSE, BusWidth);
847 if (EFI_ERROR (Status)) {
848 return Status;
849 }
850 //
851 // Set to HS200/SDR104 timing
852 //
853 //
854 // Stop bus clock at first
855 //
856 Status = SdMmcHcStopClock (PciIo, Slot);
857 if (EFI_ERROR (Status)) {
858 return Status;
859 }
860
861 Timing = SdMmcMmcHs200;
862
863 Status = SdMmcHcUhsSignaling (Private->ControllerHandle, PciIo, Slot, Timing);
864 if (EFI_ERROR (Status)) {
865 return Status;
866 }
867
868 //
869 // Wait Internal Clock Stable in the Clock Control register to be 1 before set SD Clock Enable bit
870 //
871 Status = SdMmcHcWaitMmioSet (
872 PciIo,
873 Slot,
874 SD_MMC_HC_CLOCK_CTRL,
875 sizeof (ClockCtrl),
876 BIT1,
877 BIT1,
878 SD_MMC_HC_GENERIC_TIMEOUT
879 );
880 if (EFI_ERROR (Status)) {
881 return Status;
882 }
883 //
884 // Set SD Clock Enable in the Clock Control register to 1
885 //
886 ClockCtrl = BIT2;
887 Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);
888
889 HsTiming = 2;
890 Status = EmmcSwitchClockFreq (PciIo, PassThru, Slot, Rca, HsTiming, Timing, ClockFreq);
891 if (EFI_ERROR (Status)) {
892 return Status;
893 }
894
895 Status = EmmcTuningClkForHs200 (PciIo, PassThru, Slot, BusWidth);
896
897 return Status;
898 }
899
900 /**
901 Switch to the HS400 timing according to request.
902
903 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
904 Simplified Spec 3.0 Figure 2-29 for details.
905
906 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
907 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
908 @param[in] Slot The slot number of the SD card to send the command to.
909 @param[in] Rca The relative device address to be assigned.
910 @param[in] ClockFreq The max clock frequency to be set.
911
912 @retval EFI_SUCCESS The operation is done correctly.
913 @retval Others The operation fails.
914
915 **/
916 EFI_STATUS
917 EmmcSwitchToHS400 (
918 IN EFI_PCI_IO_PROTOCOL *PciIo,
919 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
920 IN UINT8 Slot,
921 IN UINT16 Rca,
922 IN UINT32 ClockFreq
923 )
924 {
925 EFI_STATUS Status;
926 UINT8 HsTiming;
927 SD_MMC_BUS_MODE Timing;
928 SD_MMC_HC_PRIVATE_DATA *Private;
929
930 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
931
932 Status = EmmcSwitchToHS200 (PciIo, PassThru, Slot, Rca, ClockFreq, 8);
933 if (EFI_ERROR (Status)) {
934 return Status;
935 }
936 //
937 // Set to Hight Speed timing and set the clock frequency to a value less than 52MHz.
938 //
939 HsTiming = 1;
940 Status = EmmcSwitchClockFreq (PciIo, PassThru, Slot, Rca, HsTiming, SdMmcMmcHsSdr, 52);
941 if (EFI_ERROR (Status)) {
942 return Status;
943 }
944 //
945 // HS400 mode must use 8 data lines.
946 //
947 Status = EmmcSwitchBusWidth (PciIo, PassThru, Slot, Rca, TRUE, 8);
948 if (EFI_ERROR (Status)) {
949 return Status;
950 }
951
952 Timing = SdMmcMmcHs400;
953
954 Status = SdMmcHcUhsSignaling (Private->ControllerHandle, PciIo, Slot, Timing);
955 if (EFI_ERROR (Status)) {
956 return Status;
957 }
958
959 HsTiming = 3;
960 Status = EmmcSwitchClockFreq (PciIo, PassThru, Slot, Rca, HsTiming, Timing, ClockFreq);
961
962 return Status;
963 }
964
965 /**
966 Switch the high speed timing according to request.
967
968 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
969 Simplified Spec 3.0 Figure 2-29 for details.
970
971 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
972 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
973 @param[in] Slot The slot number of the SD card to send the command to.
974 @param[in] Rca The relative device address to be assigned.
975
976 @retval EFI_SUCCESS The operation is done correctly.
977 @retval Others The operation fails.
978
979 **/
980 EFI_STATUS
981 EmmcSetBusMode (
982 IN EFI_PCI_IO_PROTOCOL *PciIo,
983 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
984 IN UINT8 Slot,
985 IN UINT16 Rca
986 )
987 {
988 EFI_STATUS Status;
989 EMMC_CSD Csd;
990 EMMC_EXT_CSD ExtCsd;
991 UINT8 HsTiming;
992 BOOLEAN IsDdr;
993 UINT32 ClockFreq;
994 UINT8 BusWidth;
995 SD_MMC_HC_PRIVATE_DATA *Private;
996
997 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
998
999 Status = EmmcGetCsd (PassThru, Slot, Rca, &Csd);
1000 if (EFI_ERROR (Status)) {
1001 DEBUG ((DEBUG_ERROR, "EmmcSetBusMode: GetCsd fails with %r\n", Status));
1002 return Status;
1003 }
1004
1005 Status = EmmcSelect (PassThru, Slot, Rca);
1006 if (EFI_ERROR (Status)) {
1007 DEBUG ((DEBUG_ERROR, "EmmcSetBusMode: Select fails with %r\n", Status));
1008 return Status;
1009 }
1010
1011 ASSERT (Private->BaseClkFreq[Slot] != 0);
1012 //
1013 // Check if the Host Controller support 8bits bus width.
1014 //
1015 if (Private->Capability[Slot].BusWidth8 != 0) {
1016 BusWidth = 8;
1017 } else {
1018 BusWidth = 4;
1019 }
1020 //
1021 // Get Deivce_Type from EXT_CSD register.
1022 //
1023 Status = EmmcGetExtCsd (PassThru, Slot, &ExtCsd);
1024 if (EFI_ERROR (Status)) {
1025 DEBUG ((DEBUG_ERROR, "EmmcSetBusMode: GetExtCsd fails with %r\n", Status));
1026 return Status;
1027 }
1028 //
1029 // Calculate supported bus speed/bus width/clock frequency.
1030 //
1031 HsTiming = 0;
1032 IsDdr = FALSE;
1033 ClockFreq = 0;
1034 if (((ExtCsd.DeviceType & (BIT4 | BIT5)) != 0) && (Private->Capability[Slot].Sdr104 != 0)) {
1035 HsTiming = 2;
1036 IsDdr = FALSE;
1037 ClockFreq = 200;
1038 } else if (((ExtCsd.DeviceType & (BIT2 | BIT3)) != 0) && (Private->Capability[Slot].Ddr50 != 0)) {
1039 HsTiming = 1;
1040 IsDdr = TRUE;
1041 ClockFreq = 52;
1042 } else if (((ExtCsd.DeviceType & BIT1) != 0) && (Private->Capability[Slot].HighSpeed != 0)) {
1043 HsTiming = 1;
1044 IsDdr = FALSE;
1045 ClockFreq = 52;
1046 } else if (((ExtCsd.DeviceType & BIT0) != 0) && (Private->Capability[Slot].HighSpeed != 0)) {
1047 HsTiming = 1;
1048 IsDdr = FALSE;
1049 ClockFreq = 26;
1050 }
1051 //
1052 // Check if both of the device and the host controller support HS400 DDR mode.
1053 //
1054 if (((ExtCsd.DeviceType & (BIT6 | BIT7)) != 0) && (Private->Capability[Slot].Hs400 != 0)) {
1055 //
1056 // The host controller supports 8bits bus.
1057 //
1058 ASSERT (BusWidth == 8);
1059 HsTiming = 3;
1060 IsDdr = TRUE;
1061 ClockFreq = 200;
1062 }
1063
1064 if ((ClockFreq == 0) || (HsTiming == 0)) {
1065 //
1066 // Continue using default setting.
1067 //
1068 return EFI_SUCCESS;
1069 }
1070
1071 DEBUG ((DEBUG_INFO, "EmmcSetBusMode: HsTiming %d ClockFreq %d BusWidth %d Ddr %a\n", HsTiming, ClockFreq, BusWidth, IsDdr ? "TRUE":"FALSE"));
1072
1073 if (HsTiming == 3) {
1074 //
1075 // Execute HS400 timing switch procedure
1076 //
1077 Status = EmmcSwitchToHS400 (PciIo, PassThru, Slot, Rca, ClockFreq);
1078 } else if (HsTiming == 2) {
1079 //
1080 // Execute HS200 timing switch procedure
1081 //
1082 Status = EmmcSwitchToHS200 (PciIo, PassThru, Slot, Rca, ClockFreq, BusWidth);
1083 } else {
1084 //
1085 // Execute High Speed timing switch procedure
1086 //
1087 Status = EmmcSwitchToHighSpeed (PciIo, PassThru, Slot, Rca, ClockFreq, IsDdr, BusWidth);
1088 }
1089
1090 DEBUG ((DEBUG_INFO, "EmmcSetBusMode: Switch to %a %r\n", (HsTiming == 3) ? "HS400" : ((HsTiming == 2) ? "HS200" : "HighSpeed"), Status));
1091
1092 return Status;
1093 }
1094
1095 /**
1096 Execute EMMC device identification procedure.
1097
1098 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1099
1100 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1101 @param[in] Slot The slot number of the SD card to send the command to.
1102
1103 @retval EFI_SUCCESS There is a EMMC card.
1104 @retval Others There is not a EMMC card.
1105
1106 **/
1107 EFI_STATUS
1108 EmmcIdentification (
1109 IN SD_MMC_HC_PRIVATE_DATA *Private,
1110 IN UINT8 Slot
1111 )
1112 {
1113 EFI_STATUS Status;
1114 EFI_PCI_IO_PROTOCOL *PciIo;
1115 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;
1116 UINT32 Ocr;
1117 UINT16 Rca;
1118 UINTN Retry;
1119
1120 PciIo = Private->PciIo;
1121 PassThru = &Private->PassThru;
1122
1123 Status = EmmcReset (PassThru, Slot);
1124 if (EFI_ERROR (Status)) {
1125 DEBUG ((DEBUG_VERBOSE, "EmmcIdentification: Executing Cmd0 fails with %r\n", Status));
1126 return Status;
1127 }
1128
1129 Ocr = 0;
1130 Retry = 0;
1131 do {
1132 Status = EmmcGetOcr (PassThru, Slot, &Ocr);
1133 if (EFI_ERROR (Status)) {
1134 DEBUG ((DEBUG_VERBOSE, "EmmcIdentification: Executing Cmd1 fails with %r\n", Status));
1135 return Status;
1136 }
1137 Ocr |= BIT30;
1138
1139 if (Retry++ == 100) {
1140 DEBUG ((DEBUG_VERBOSE, "EmmcIdentification: Executing Cmd1 fails too many times\n"));
1141 return EFI_DEVICE_ERROR;
1142 }
1143 gBS->Stall(10 * 1000);
1144 } while ((Ocr & BIT31) == 0);
1145
1146 Status = EmmcGetAllCid (PassThru, Slot);
1147 if (EFI_ERROR (Status)) {
1148 DEBUG ((DEBUG_VERBOSE, "EmmcIdentification: Executing Cmd2 fails with %r\n", Status));
1149 return Status;
1150 }
1151 //
1152 // Slot starts from 0 and valid RCA starts from 1.
1153 // Here we takes a simple formula to calculate the RCA.
1154 // Don't support multiple devices on the slot, that is
1155 // shared bus slot feature.
1156 //
1157 Rca = Slot + 1;
1158 Status = EmmcSetRca (PassThru, Slot, Rca);
1159 if (EFI_ERROR (Status)) {
1160 DEBUG ((DEBUG_ERROR, "EmmcIdentification: Executing Cmd3 fails with %r\n", Status));
1161 return Status;
1162 }
1163 //
1164 // Enter Data Tranfer Mode.
1165 //
1166 DEBUG ((DEBUG_INFO, "EmmcIdentification: Found a EMMC device at slot [%d], RCA [%d]\n", Slot, Rca));
1167 Private->Slot[Slot].CardType = EmmcCardType;
1168
1169 Status = EmmcSetBusMode (PciIo, PassThru, Slot, Rca);
1170
1171 return Status;
1172 }
1173