]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/EmmcDevice.c
MdeModulePkg/SdMmcPciHcDxe: Add V3 64b DMA 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 bus timing and clock frequency.
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 EmmcSwitchBusTiming (
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, "EmmcSwitchBusTiming: Switch to hstiming %d fails with %r\n", HsTiming, Status));
693 return Status;
694 }
695
696 //
697 // Convert the clock freq unit from MHz to KHz.
698 //
699 Status = SdMmcHcClockSupply (PciIo, Slot, ClockFreq * 1000, Private->BaseClkFreq[Slot], Private->ControllerVersion[Slot]);
700 if (EFI_ERROR (Status)) {
701 return Status;
702 }
703
704 Status = EmmcSendStatus (PassThru, Slot, Rca, &DevStatus);
705 if (EFI_ERROR (Status)) {
706 DEBUG ((DEBUG_ERROR, "EmmcSwitchBusTiming: Send status fails with %r\n", Status));
707 return Status;
708 }
709 //
710 // Check the switch operation is really successful or not.
711 //
712 if ((DevStatus & BIT7) != 0) {
713 DEBUG ((DEBUG_ERROR, "EmmcSwitchBusTiming: The switch operation fails as DevStatus is 0x%08x\n", DevStatus));
714 return EFI_DEVICE_ERROR;
715 }
716
717 if (mOverride != NULL && mOverride->NotifyPhase != NULL) {
718 Status = mOverride->NotifyPhase (
719 Private->ControllerHandle,
720 Slot,
721 EdkiiSdMmcSwitchClockFreqPost,
722 &Timing
723 );
724 if (EFI_ERROR (Status)) {
725 DEBUG ((
726 DEBUG_ERROR,
727 "%a: SD/MMC switch clock freq post notifier callback failed - %r\n",
728 __FUNCTION__,
729 Status
730 ));
731 return Status;
732 }
733 }
734
735 return Status;
736 }
737
738 /**
739 Switch to the High Speed timing according to request.
740
741 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
742 Simplified Spec 3.0 Figure 2-29 for details.
743
744 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
745 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
746 @param[in] Slot The slot number of the SD card to send the command to.
747 @param[in] Rca The relative device address to be assigned.
748 @param[in] ClockFreq The max clock frequency to be set.
749 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
750 use single data rate data simpling method.
751 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
752
753 @retval EFI_SUCCESS The operation is done correctly.
754 @retval Others The operation fails.
755
756 **/
757 EFI_STATUS
758 EmmcSwitchToHighSpeed (
759 IN EFI_PCI_IO_PROTOCOL *PciIo,
760 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
761 IN UINT8 Slot,
762 IN UINT16 Rca,
763 IN UINT32 ClockFreq,
764 IN BOOLEAN IsDdr,
765 IN UINT8 BusWidth
766 )
767 {
768 EFI_STATUS Status;
769 UINT8 HsTiming;
770 UINT8 HostCtrl1;
771 SD_MMC_BUS_MODE Timing;
772 SD_MMC_HC_PRIVATE_DATA *Private;
773
774 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
775
776 Status = EmmcSwitchBusWidth (PciIo, PassThru, Slot, Rca, IsDdr, BusWidth);
777 if (EFI_ERROR (Status)) {
778 return Status;
779 }
780 //
781 // Set to Hight Speed timing
782 //
783 HostCtrl1 = BIT2;
784 Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
785 if (EFI_ERROR (Status)) {
786 return Status;
787 }
788
789 if (IsDdr) {
790 Timing = SdMmcMmcHsDdr;
791 } else if (ClockFreq == 52) {
792 Timing = SdMmcMmcHsSdr;
793 } else {
794 Timing = SdMmcMmcLegacy;
795 }
796
797 Status = SdMmcHcUhsSignaling (Private->ControllerHandle, PciIo, Slot, Timing);
798 if (EFI_ERROR (Status)) {
799 return Status;
800 }
801
802 HsTiming = 1;
803 Status = EmmcSwitchBusTiming (PciIo, PassThru, Slot, Rca, HsTiming, Timing, ClockFreq);
804
805 return Status;
806 }
807
808 /**
809 Switch to the HS200 timing according to request.
810
811 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
812 Simplified Spec 3.0 Figure 2-29 for details.
813
814 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
815 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
816 @param[in] Slot The slot number of the SD card to send the command to.
817 @param[in] Rca The relative device address to be assigned.
818 @param[in] ClockFreq The max clock frequency to be set.
819 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
820
821 @retval EFI_SUCCESS The operation is done correctly.
822 @retval Others The operation fails.
823
824 **/
825 EFI_STATUS
826 EmmcSwitchToHS200 (
827 IN EFI_PCI_IO_PROTOCOL *PciIo,
828 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
829 IN UINT8 Slot,
830 IN UINT16 Rca,
831 IN UINT32 ClockFreq,
832 IN UINT8 BusWidth
833 )
834 {
835 EFI_STATUS Status;
836 UINT8 HsTiming;
837 UINT16 ClockCtrl;
838 SD_MMC_BUS_MODE Timing;
839 SD_MMC_HC_PRIVATE_DATA *Private;
840
841 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
842
843 if ((BusWidth != 4) && (BusWidth != 8)) {
844 return EFI_INVALID_PARAMETER;
845 }
846
847 Status = EmmcSwitchBusWidth (PciIo, PassThru, Slot, Rca, FALSE, BusWidth);
848 if (EFI_ERROR (Status)) {
849 return Status;
850 }
851 //
852 // Set to HS200/SDR104 timing
853 //
854 //
855 // Stop bus clock at first
856 //
857 Status = SdMmcHcStopClock (PciIo, Slot);
858 if (EFI_ERROR (Status)) {
859 return Status;
860 }
861
862 Timing = SdMmcMmcHs200;
863
864 Status = SdMmcHcUhsSignaling (Private->ControllerHandle, PciIo, Slot, Timing);
865 if (EFI_ERROR (Status)) {
866 return Status;
867 }
868
869 //
870 // Wait Internal Clock Stable in the Clock Control register to be 1 before set SD Clock Enable bit
871 //
872 Status = SdMmcHcWaitMmioSet (
873 PciIo,
874 Slot,
875 SD_MMC_HC_CLOCK_CTRL,
876 sizeof (ClockCtrl),
877 BIT1,
878 BIT1,
879 SD_MMC_HC_GENERIC_TIMEOUT
880 );
881 if (EFI_ERROR (Status)) {
882 return Status;
883 }
884 //
885 // Set SD Clock Enable in the Clock Control register to 1
886 //
887 ClockCtrl = BIT2;
888 Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);
889
890 HsTiming = 2;
891 Status = EmmcSwitchBusTiming (PciIo, PassThru, Slot, Rca, HsTiming, Timing, ClockFreq);
892 if (EFI_ERROR (Status)) {
893 return Status;
894 }
895
896 Status = EmmcTuningClkForHs200 (PciIo, PassThru, Slot, BusWidth);
897
898 return Status;
899 }
900
901 /**
902 Switch to the HS400 timing according to request.
903
904 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
905 Simplified Spec 3.0 Figure 2-29 for details.
906
907 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
908 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
909 @param[in] Slot The slot number of the SD card to send the command to.
910 @param[in] Rca The relative device address to be assigned.
911 @param[in] ClockFreq The max clock frequency to be set.
912
913 @retval EFI_SUCCESS The operation is done correctly.
914 @retval Others The operation fails.
915
916 **/
917 EFI_STATUS
918 EmmcSwitchToHS400 (
919 IN EFI_PCI_IO_PROTOCOL *PciIo,
920 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
921 IN UINT8 Slot,
922 IN UINT16 Rca,
923 IN UINT32 ClockFreq
924 )
925 {
926 EFI_STATUS Status;
927 UINT8 HsTiming;
928 SD_MMC_BUS_MODE Timing;
929 SD_MMC_HC_PRIVATE_DATA *Private;
930
931 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
932
933 Status = EmmcSwitchToHS200 (PciIo, PassThru, Slot, Rca, ClockFreq, 8);
934 if (EFI_ERROR (Status)) {
935 return Status;
936 }
937 //
938 // Set to Hight Speed timing and set the clock frequency to a value less than 52MHz.
939 //
940 HsTiming = 1;
941 Status = EmmcSwitchBusTiming (PciIo, PassThru, Slot, Rca, HsTiming, SdMmcMmcHsSdr, 52);
942 if (EFI_ERROR (Status)) {
943 return Status;
944 }
945 //
946 // HS400 mode must use 8 data lines.
947 //
948 Status = EmmcSwitchBusWidth (PciIo, PassThru, Slot, Rca, TRUE, 8);
949 if (EFI_ERROR (Status)) {
950 return Status;
951 }
952
953 Timing = SdMmcMmcHs400;
954
955 Status = SdMmcHcUhsSignaling (Private->ControllerHandle, PciIo, Slot, Timing);
956 if (EFI_ERROR (Status)) {
957 return Status;
958 }
959
960 HsTiming = 3;
961 Status = EmmcSwitchBusTiming (PciIo, PassThru, Slot, Rca, HsTiming, Timing, ClockFreq);
962
963 return Status;
964 }
965
966 /**
967 Switch the high speed timing according to request.
968
969 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
970 Simplified Spec 3.0 Figure 2-29 for details.
971
972 @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.
973 @param[in] PassThru A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
974 @param[in] Slot The slot number of the SD card to send the command to.
975 @param[in] Rca The relative device address to be assigned.
976
977 @retval EFI_SUCCESS The operation is done correctly.
978 @retval Others The operation fails.
979
980 **/
981 EFI_STATUS
982 EmmcSetBusMode (
983 IN EFI_PCI_IO_PROTOCOL *PciIo,
984 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru,
985 IN UINT8 Slot,
986 IN UINT16 Rca
987 )
988 {
989 EFI_STATUS Status;
990 EMMC_CSD Csd;
991 EMMC_EXT_CSD ExtCsd;
992 UINT8 HsTiming;
993 BOOLEAN IsDdr;
994 UINT32 ClockFreq;
995 UINT8 BusWidth;
996 SD_MMC_HC_PRIVATE_DATA *Private;
997
998 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
999
1000 Status = EmmcGetCsd (PassThru, Slot, Rca, &Csd);
1001 if (EFI_ERROR (Status)) {
1002 DEBUG ((DEBUG_ERROR, "EmmcSetBusMode: GetCsd fails with %r\n", Status));
1003 return Status;
1004 }
1005
1006 Status = EmmcSelect (PassThru, Slot, Rca);
1007 if (EFI_ERROR (Status)) {
1008 DEBUG ((DEBUG_ERROR, "EmmcSetBusMode: Select fails with %r\n", Status));
1009 return Status;
1010 }
1011
1012 ASSERT (Private->BaseClkFreq[Slot] != 0);
1013 //
1014 // Check if the Host Controller support 8bits bus width.
1015 //
1016 if (Private->Capability[Slot].BusWidth8 != 0) {
1017 BusWidth = 8;
1018 } else {
1019 BusWidth = 4;
1020 }
1021 //
1022 // Get Deivce_Type from EXT_CSD register.
1023 //
1024 Status = EmmcGetExtCsd (PassThru, Slot, &ExtCsd);
1025 if (EFI_ERROR (Status)) {
1026 DEBUG ((DEBUG_ERROR, "EmmcSetBusMode: GetExtCsd fails with %r\n", Status));
1027 return Status;
1028 }
1029 //
1030 // Calculate supported bus speed/bus width/clock frequency.
1031 //
1032 HsTiming = 0;
1033 IsDdr = FALSE;
1034 ClockFreq = 0;
1035 if (((ExtCsd.DeviceType & (BIT4 | BIT5)) != 0) && (Private->Capability[Slot].Sdr104 != 0)) {
1036 HsTiming = 2;
1037 IsDdr = FALSE;
1038 ClockFreq = 200;
1039 } else if (((ExtCsd.DeviceType & (BIT2 | BIT3)) != 0) && (Private->Capability[Slot].Ddr50 != 0)) {
1040 HsTiming = 1;
1041 IsDdr = TRUE;
1042 ClockFreq = 52;
1043 } else if (((ExtCsd.DeviceType & BIT1) != 0) && (Private->Capability[Slot].HighSpeed != 0)) {
1044 HsTiming = 1;
1045 IsDdr = FALSE;
1046 ClockFreq = 52;
1047 } else if (((ExtCsd.DeviceType & BIT0) != 0) && (Private->Capability[Slot].HighSpeed != 0)) {
1048 HsTiming = 1;
1049 IsDdr = FALSE;
1050 ClockFreq = 26;
1051 }
1052 //
1053 // Check if both of the device and the host controller support HS400 DDR mode.
1054 //
1055 if (((ExtCsd.DeviceType & (BIT6 | BIT7)) != 0) && (Private->Capability[Slot].Hs400 != 0)) {
1056 //
1057 // The host controller supports 8bits bus.
1058 //
1059 ASSERT (BusWidth == 8);
1060 HsTiming = 3;
1061 IsDdr = TRUE;
1062 ClockFreq = 200;
1063 }
1064
1065 if ((ClockFreq == 0) || (HsTiming == 0)) {
1066 //
1067 // Continue using default setting.
1068 //
1069 return EFI_SUCCESS;
1070 }
1071
1072 DEBUG ((DEBUG_INFO, "EmmcSetBusMode: HsTiming %d ClockFreq %d BusWidth %d Ddr %a\n", HsTiming, ClockFreq, BusWidth, IsDdr ? "TRUE":"FALSE"));
1073
1074 if (HsTiming == 3) {
1075 //
1076 // Execute HS400 timing switch procedure
1077 //
1078 Status = EmmcSwitchToHS400 (PciIo, PassThru, Slot, Rca, ClockFreq);
1079 } else if (HsTiming == 2) {
1080 //
1081 // Execute HS200 timing switch procedure
1082 //
1083 Status = EmmcSwitchToHS200 (PciIo, PassThru, Slot, Rca, ClockFreq, BusWidth);
1084 } else {
1085 //
1086 // Execute High Speed timing switch procedure
1087 //
1088 Status = EmmcSwitchToHighSpeed (PciIo, PassThru, Slot, Rca, ClockFreq, IsDdr, BusWidth);
1089 }
1090
1091 DEBUG ((DEBUG_INFO, "EmmcSetBusMode: Switch to %a %r\n", (HsTiming == 3) ? "HS400" : ((HsTiming == 2) ? "HS200" : "HighSpeed"), Status));
1092
1093 return Status;
1094 }
1095
1096 /**
1097 Execute EMMC device identification procedure.
1098
1099 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1100
1101 @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.
1102 @param[in] Slot The slot number of the SD card to send the command to.
1103
1104 @retval EFI_SUCCESS There is a EMMC card.
1105 @retval Others There is not a EMMC card.
1106
1107 **/
1108 EFI_STATUS
1109 EmmcIdentification (
1110 IN SD_MMC_HC_PRIVATE_DATA *Private,
1111 IN UINT8 Slot
1112 )
1113 {
1114 EFI_STATUS Status;
1115 EFI_PCI_IO_PROTOCOL *PciIo;
1116 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;
1117 UINT32 Ocr;
1118 UINT16 Rca;
1119 UINTN Retry;
1120
1121 PciIo = Private->PciIo;
1122 PassThru = &Private->PassThru;
1123
1124 Status = EmmcReset (PassThru, Slot);
1125 if (EFI_ERROR (Status)) {
1126 DEBUG ((DEBUG_VERBOSE, "EmmcIdentification: Executing Cmd0 fails with %r\n", Status));
1127 return Status;
1128 }
1129
1130 Ocr = 0;
1131 Retry = 0;
1132 do {
1133 Status = EmmcGetOcr (PassThru, Slot, &Ocr);
1134 if (EFI_ERROR (Status)) {
1135 DEBUG ((DEBUG_VERBOSE, "EmmcIdentification: Executing Cmd1 fails with %r\n", Status));
1136 return Status;
1137 }
1138 Ocr |= BIT30;
1139
1140 if (Retry++ == 100) {
1141 DEBUG ((DEBUG_VERBOSE, "EmmcIdentification: Executing Cmd1 fails too many times\n"));
1142 return EFI_DEVICE_ERROR;
1143 }
1144 gBS->Stall(10 * 1000);
1145 } while ((Ocr & BIT31) == 0);
1146
1147 Status = EmmcGetAllCid (PassThru, Slot);
1148 if (EFI_ERROR (Status)) {
1149 DEBUG ((DEBUG_VERBOSE, "EmmcIdentification: Executing Cmd2 fails with %r\n", Status));
1150 return Status;
1151 }
1152 //
1153 // Slot starts from 0 and valid RCA starts from 1.
1154 // Here we takes a simple formula to calculate the RCA.
1155 // Don't support multiple devices on the slot, that is
1156 // shared bus slot feature.
1157 //
1158 Rca = Slot + 1;
1159 Status = EmmcSetRca (PassThru, Slot, Rca);
1160 if (EFI_ERROR (Status)) {
1161 DEBUG ((DEBUG_ERROR, "EmmcIdentification: Executing Cmd3 fails with %r\n", Status));
1162 return Status;
1163 }
1164 //
1165 // Enter Data Tranfer Mode.
1166 //
1167 DEBUG ((DEBUG_INFO, "EmmcIdentification: Found a EMMC device at slot [%d], RCA [%d]\n", Slot, Rca));
1168 Private->Slot[Slot].CardType = EmmcCardType;
1169
1170 Status = EmmcSetBusMode (PciIo, PassThru, Slot, Rca);
1171
1172 return Status;
1173 }
1174