]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Sd/EmmcBlockIoPei/EmmcHci.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / EmmcBlockIoPei / EmmcHci.c
1 /** @file
2
3 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
5
6 **/
7
8 #include "EmmcBlockIoPei.h"
9
10 /**
11 Read/Write specified EMMC host controller mmio register.
12
13 @param[in] Address The address of the mmio register to be read/written.
14 @param[in] Read A boolean to indicate it's read or write operation.
15 @param[in] Count The width of the mmio register in bytes.
16 Must be 1, 2 , 4 or 8 bytes.
17 @param[in, out] Data For read operations, the destination buffer to store
18 the results. For write operations, the source buffer
19 to write data from. The caller is responsible for
20 having ownership of the data buffer and ensuring its
21 size not less than Count bytes.
22
23 @retval EFI_INVALID_PARAMETER The Address or the Data or the Count is not valid.
24 @retval EFI_SUCCESS The read/write operation succeeds.
25 @retval Others The read/write operation fails.
26
27 **/
28 EFI_STATUS
29 EFIAPI
30 EmmcPeimHcRwMmio (
31 IN UINTN Address,
32 IN BOOLEAN Read,
33 IN UINT8 Count,
34 IN OUT VOID *Data
35 )
36 {
37 if ((Address == 0) || (Data == NULL)) {
38 return EFI_INVALID_PARAMETER;
39 }
40
41 if ((Count != 1) && (Count != 2) && (Count != 4) && (Count != 8)) {
42 return EFI_INVALID_PARAMETER;
43 }
44
45 switch (Count) {
46 case 1:
47 if (Read) {
48 *(UINT8*)Data = MmioRead8 (Address);
49 } else {
50 MmioWrite8 (Address, *(UINT8*)Data);
51 }
52 break;
53 case 2:
54 if (Read) {
55 *(UINT16*)Data = MmioRead16 (Address);
56 } else {
57 MmioWrite16 (Address, *(UINT16*)Data);
58 }
59 break;
60 case 4:
61 if (Read) {
62 *(UINT32*)Data = MmioRead32 (Address);
63 } else {
64 MmioWrite32 (Address, *(UINT32*)Data);
65 }
66 break;
67 case 8:
68 if (Read) {
69 *(UINT64*)Data = MmioRead64 (Address);
70 } else {
71 MmioWrite64 (Address, *(UINT64*)Data);
72 }
73 break;
74 default:
75 ASSERT (FALSE);
76 return EFI_INVALID_PARAMETER;
77 }
78
79 return EFI_SUCCESS;
80 }
81
82 /**
83 Do OR operation with the value of the specified EMMC host controller mmio register.
84
85 @param[in] Address The address of the mmio register to be read/written.
86 @param[in] Count The width of the mmio register in bytes.
87 Must be 1, 2 , 4 or 8 bytes.
88 @param[in] OrData The pointer to the data used to do OR operation.
89 The caller is responsible for having ownership of
90 the data buffer and ensuring its size not less than
91 Count bytes.
92
93 @retval EFI_INVALID_PARAMETER The Address or the OrData or the Count is not valid.
94 @retval EFI_SUCCESS The OR operation succeeds.
95 @retval Others The OR operation fails.
96
97 **/
98 EFI_STATUS
99 EFIAPI
100 EmmcPeimHcOrMmio (
101 IN UINTN Address,
102 IN UINT8 Count,
103 IN VOID *OrData
104 )
105 {
106 EFI_STATUS Status;
107 UINT64 Data;
108 UINT64 Or;
109
110 Status = EmmcPeimHcRwMmio (Address, TRUE, Count, &Data);
111 if (EFI_ERROR (Status)) {
112 return Status;
113 }
114
115 if (Count == 1) {
116 Or = *(UINT8*) OrData;
117 } else if (Count == 2) {
118 Or = *(UINT16*) OrData;
119 } else if (Count == 4) {
120 Or = *(UINT32*) OrData;
121 } else if (Count == 8) {
122 Or = *(UINT64*) OrData;
123 } else {
124 return EFI_INVALID_PARAMETER;
125 }
126
127 Data |= Or;
128 Status = EmmcPeimHcRwMmio (Address, FALSE, Count, &Data);
129
130 return Status;
131 }
132
133 /**
134 Do AND operation with the value of the specified EMMC host controller mmio register.
135
136 @param[in] Address The address of the mmio register to be read/written.
137 @param[in] Count The width of the mmio register in bytes.
138 Must be 1, 2 , 4 or 8 bytes.
139 @param[in] AndData The pointer to the data used to do AND operation.
140 The caller is responsible for having ownership of
141 the data buffer and ensuring its size not less than
142 Count bytes.
143
144 @retval EFI_INVALID_PARAMETER The Address or the AndData or the Count is not valid.
145 @retval EFI_SUCCESS The AND operation succeeds.
146 @retval Others The AND operation fails.
147
148 **/
149 EFI_STATUS
150 EFIAPI
151 EmmcPeimHcAndMmio (
152 IN UINTN Address,
153 IN UINT8 Count,
154 IN VOID *AndData
155 )
156 {
157 EFI_STATUS Status;
158 UINT64 Data;
159 UINT64 And;
160
161 Status = EmmcPeimHcRwMmio (Address, TRUE, Count, &Data);
162 if (EFI_ERROR (Status)) {
163 return Status;
164 }
165
166 if (Count == 1) {
167 And = *(UINT8*) AndData;
168 } else if (Count == 2) {
169 And = *(UINT16*) AndData;
170 } else if (Count == 4) {
171 And = *(UINT32*) AndData;
172 } else if (Count == 8) {
173 And = *(UINT64*) AndData;
174 } else {
175 return EFI_INVALID_PARAMETER;
176 }
177
178 Data &= And;
179 Status = EmmcPeimHcRwMmio (Address, FALSE, Count, &Data);
180
181 return Status;
182 }
183
184 /**
185 Wait for the value of the specified MMIO register set to the test value.
186
187 @param[in] Address The address of the mmio register to be checked.
188 @param[in] Count The width of the mmio register in bytes.
189 Must be 1, 2, 4 or 8 bytes.
190 @param[in] MaskValue The mask value of memory.
191 @param[in] TestValue The test value of memory.
192
193 @retval EFI_NOT_READY The MMIO register hasn't set to the expected value.
194 @retval EFI_SUCCESS The MMIO register has expected value.
195 @retval Others The MMIO operation fails.
196
197 **/
198 EFI_STATUS
199 EFIAPI
200 EmmcPeimHcCheckMmioSet (
201 IN UINTN Address,
202 IN UINT8 Count,
203 IN UINT64 MaskValue,
204 IN UINT64 TestValue
205 )
206 {
207 EFI_STATUS Status;
208 UINT64 Value;
209
210 //
211 // Access PCI MMIO space to see if the value is the tested one.
212 //
213 Value = 0;
214 Status = EmmcPeimHcRwMmio (Address, TRUE, Count, &Value);
215 if (EFI_ERROR (Status)) {
216 return Status;
217 }
218
219 Value &= MaskValue;
220
221 if (Value == TestValue) {
222 return EFI_SUCCESS;
223 }
224
225 return EFI_NOT_READY;
226 }
227
228 /**
229 Wait for the value of the specified MMIO register set to the test value.
230
231 @param[in] Address The address of the mmio register to wait.
232 @param[in] Count The width of the mmio register in bytes.
233 Must be 1, 2, 4 or 8 bytes.
234 @param[in] MaskValue The mask value of memory.
235 @param[in] TestValue The test value of memory.
236 @param[in] Timeout The time out value for wait memory set, uses 1
237 microsecond as a unit.
238
239 @retval EFI_TIMEOUT The MMIO register hasn't expected value in timeout
240 range.
241 @retval EFI_SUCCESS The MMIO register has expected value.
242 @retval Others The MMIO operation fails.
243
244 **/
245 EFI_STATUS
246 EFIAPI
247 EmmcPeimHcWaitMmioSet (
248 IN UINTN Address,
249 IN UINT8 Count,
250 IN UINT64 MaskValue,
251 IN UINT64 TestValue,
252 IN UINT64 Timeout
253 )
254 {
255 EFI_STATUS Status;
256 BOOLEAN InfiniteWait;
257
258 if (Timeout == 0) {
259 InfiniteWait = TRUE;
260 } else {
261 InfiniteWait = FALSE;
262 }
263
264 while (InfiniteWait || (Timeout > 0)) {
265 Status = EmmcPeimHcCheckMmioSet (
266 Address,
267 Count,
268 MaskValue,
269 TestValue
270 );
271 if (Status != EFI_NOT_READY) {
272 return Status;
273 }
274
275 //
276 // Stall for 1 microsecond.
277 //
278 MicroSecondDelay (1);
279
280 Timeout--;
281 }
282
283 return EFI_TIMEOUT;
284 }
285
286 /**
287 Software reset the specified EMMC host controller and enable all interrupts.
288
289 @param[in] Bar The mmio base address of the slot to be accessed.
290
291 @retval EFI_SUCCESS The software reset executes successfully.
292 @retval Others The software reset fails.
293
294 **/
295 EFI_STATUS
296 EmmcPeimHcReset (
297 IN UINTN Bar
298 )
299 {
300 EFI_STATUS Status;
301 UINT8 SwReset;
302
303 SwReset = 0xFF;
304 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_SW_RST, FALSE, sizeof (SwReset), &SwReset);
305
306 if (EFI_ERROR (Status)) {
307 DEBUG ((EFI_D_ERROR, "EmmcPeimHcReset: write full 1 fails: %r\n", Status));
308 return Status;
309 }
310
311 Status = EmmcPeimHcWaitMmioSet (
312 Bar + EMMC_HC_SW_RST,
313 sizeof (SwReset),
314 0xFF,
315 0x00,
316 EMMC_TIMEOUT
317 );
318 if (EFI_ERROR (Status)) {
319 DEBUG ((EFI_D_INFO, "EmmcPeimHcReset: reset done with %r\n", Status));
320 return Status;
321 }
322 //
323 // Enable all interrupt after reset all.
324 //
325 Status = EmmcPeimHcEnableInterrupt (Bar);
326
327 return Status;
328 }
329
330 /**
331 Set all interrupt status bits in Normal and Error Interrupt Status Enable
332 register.
333
334 @param[in] Bar The mmio base address of the slot to be accessed.
335
336 @retval EFI_SUCCESS The operation executes successfully.
337 @retval Others The operation fails.
338
339 **/
340 EFI_STATUS
341 EmmcPeimHcEnableInterrupt (
342 IN UINTN Bar
343 )
344 {
345 EFI_STATUS Status;
346 UINT16 IntStatus;
347
348 //
349 // Enable all bits in Error Interrupt Status Enable Register
350 //
351 IntStatus = 0xFFFF;
352 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_ERR_INT_STS_EN, FALSE, sizeof (IntStatus), &IntStatus);
353 if (EFI_ERROR (Status)) {
354 return Status;
355 }
356 //
357 // Enable all bits in Normal Interrupt Status Enable Register
358 //
359 IntStatus = 0xFFFF;
360 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_NOR_INT_STS_EN, FALSE, sizeof (IntStatus), &IntStatus);
361
362 return Status;
363 }
364
365 /**
366 Get the capability data from the specified slot.
367
368 @param[in] Bar The mmio base address of the slot to be accessed.
369 @param[out] Capability The buffer to store the capability data.
370
371 @retval EFI_SUCCESS The operation executes successfully.
372 @retval Others The operation fails.
373
374 **/
375 EFI_STATUS
376 EmmcPeimHcGetCapability (
377 IN UINTN Bar,
378 OUT EMMC_HC_SLOT_CAP *Capability
379 )
380 {
381 EFI_STATUS Status;
382 UINT64 Cap;
383
384 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_CAP, TRUE, sizeof (Cap), &Cap);
385 if (EFI_ERROR (Status)) {
386 return Status;
387 }
388
389 CopyMem (Capability, &Cap, sizeof (Cap));
390
391 return EFI_SUCCESS;
392 }
393
394 /**
395 Detect whether there is a EMMC card attached at the specified EMMC host controller
396 slot.
397
398 Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.
399
400 @param[in] Bar The mmio base address of the slot to be accessed.
401
402 @retval EFI_SUCCESS There is a EMMC card attached.
403 @retval EFI_NO_MEDIA There is not a EMMC card attached.
404 @retval Others The detection fails.
405
406 **/
407 EFI_STATUS
408 EmmcPeimHcCardDetect (
409 IN UINTN Bar
410 )
411 {
412 EFI_STATUS Status;
413 UINT16 Data;
414 UINT32 PresentState;
415
416 //
417 // Check Normal Interrupt Status Register
418 //
419 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_NOR_INT_STS, TRUE, sizeof (Data), &Data);
420 if (EFI_ERROR (Status)) {
421 return Status;
422 }
423
424 if ((Data & (BIT6 | BIT7)) != 0) {
425 //
426 // Clear BIT6 and BIT7 by writing 1 to these two bits if set.
427 //
428 Data &= BIT6 | BIT7;
429 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_NOR_INT_STS, FALSE, sizeof (Data), &Data);
430 if (EFI_ERROR (Status)) {
431 return Status;
432 }
433 }
434
435 //
436 // Check Present State Register to see if there is a card presented.
437 //
438 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_PRESENT_STATE, TRUE, sizeof (PresentState), &PresentState);
439 if (EFI_ERROR (Status)) {
440 return Status;
441 }
442
443 if ((PresentState & BIT16) != 0) {
444 return EFI_SUCCESS;
445 } else {
446 return EFI_NO_MEDIA;
447 }
448 }
449
450 /**
451 Stop EMMC card clock.
452
453 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.
454
455 @param[in] Bar The mmio base address of the slot to be accessed.
456
457 @retval EFI_SUCCESS Succeed to stop EMMC clock.
458 @retval Others Fail to stop EMMC clock.
459
460 **/
461 EFI_STATUS
462 EmmcPeimHcStopClock (
463 IN UINTN Bar
464 )
465 {
466 EFI_STATUS Status;
467 UINT32 PresentState;
468 UINT16 ClockCtrl;
469
470 //
471 // Ensure no SD transactions are occurring on the SD Bus by
472 // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)
473 // in the Present State register to be 0.
474 //
475 Status = EmmcPeimHcWaitMmioSet (
476 Bar + EMMC_HC_PRESENT_STATE,
477 sizeof (PresentState),
478 BIT0 | BIT1,
479 0,
480 EMMC_TIMEOUT
481 );
482 if (EFI_ERROR (Status)) {
483 return Status;
484 }
485
486 //
487 // Set SD Clock Enable in the Clock Control register to 0
488 //
489 ClockCtrl = (UINT16)~BIT2;
490 Status = EmmcPeimHcAndMmio (Bar + EMMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);
491
492 return Status;
493 }
494
495 /**
496 EMMC card clock supply.
497
498 Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.
499
500 @param[in] Bar The mmio base address of the slot to be accessed.
501 @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.
502
503 @retval EFI_SUCCESS The clock is supplied successfully.
504 @retval Others The clock isn't supplied successfully.
505
506 **/
507 EFI_STATUS
508 EmmcPeimHcClockSupply (
509 IN UINTN Bar,
510 IN UINT64 ClockFreq
511 )
512 {
513 EFI_STATUS Status;
514 EMMC_HC_SLOT_CAP Capability;
515 UINT32 BaseClkFreq;
516 UINT32 SettingFreq;
517 UINT32 Divisor;
518 UINT32 Remainder;
519 UINT16 ControllerVer;
520 UINT16 ClockCtrl;
521
522 //
523 // Calculate a divisor for SD clock frequency
524 //
525 Status = EmmcPeimHcGetCapability (Bar, &Capability);
526 if (EFI_ERROR (Status)) {
527 return Status;
528 }
529 ASSERT (Capability.BaseClkFreq != 0);
530
531 BaseClkFreq = Capability.BaseClkFreq;
532
533 if (ClockFreq == 0) {
534 return EFI_INVALID_PARAMETER;
535 }
536
537 if (ClockFreq > (BaseClkFreq * 1000)) {
538 ClockFreq = BaseClkFreq * 1000;
539 }
540
541 //
542 // Calculate the divisor of base frequency.
543 //
544 Divisor = 0;
545 SettingFreq = BaseClkFreq * 1000;
546 while (ClockFreq < SettingFreq) {
547 Divisor++;
548
549 SettingFreq = (BaseClkFreq * 1000) / (2 * Divisor);
550 Remainder = (BaseClkFreq * 1000) % (2 * Divisor);
551 if ((ClockFreq == SettingFreq) && (Remainder == 0)) {
552 break;
553 }
554 if ((ClockFreq == SettingFreq) && (Remainder != 0)) {
555 SettingFreq ++;
556 }
557 }
558
559 DEBUG ((EFI_D_INFO, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq, Divisor, ClockFreq));
560
561 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_CTRL_VER, TRUE, sizeof (ControllerVer), &ControllerVer);
562 if (EFI_ERROR (Status)) {
563 return Status;
564 }
565 //
566 // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.
567 //
568 if ((ControllerVer & 0xFF) == 2) {
569 ASSERT (Divisor <= 0x3FF);
570 ClockCtrl = ((Divisor & 0xFF) << 8) | ((Divisor & 0x300) >> 2);
571 } else if (((ControllerVer & 0xFF) == 0) || ((ControllerVer & 0xFF) == 1)) {
572 //
573 // Only the most significant bit can be used as divisor.
574 //
575 if (((Divisor - 1) & Divisor) != 0) {
576 Divisor = 1 << (HighBitSet32 (Divisor) + 1);
577 }
578 ASSERT (Divisor <= 0x80);
579 ClockCtrl = (Divisor & 0xFF) << 8;
580 } else {
581 DEBUG ((EFI_D_ERROR, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer));
582 return EFI_UNSUPPORTED;
583 }
584
585 //
586 // Stop bus clock at first
587 //
588 Status = EmmcPeimHcStopClock (Bar);
589 if (EFI_ERROR (Status)) {
590 return Status;
591 }
592
593 //
594 // Supply clock frequency with specified divisor
595 //
596 ClockCtrl |= BIT0;
597 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_CLOCK_CTRL, FALSE, sizeof (ClockCtrl), &ClockCtrl);
598 if (EFI_ERROR (Status)) {
599 DEBUG ((EFI_D_ERROR, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));
600 return Status;
601 }
602
603 //
604 // Wait Internal Clock Stable in the Clock Control register to be 1
605 //
606 Status = EmmcPeimHcWaitMmioSet (
607 Bar + EMMC_HC_CLOCK_CTRL,
608 sizeof (ClockCtrl),
609 BIT1,
610 BIT1,
611 EMMC_TIMEOUT
612 );
613 if (EFI_ERROR (Status)) {
614 return Status;
615 }
616
617 //
618 // Set SD Clock Enable in the Clock Control register to 1
619 //
620 ClockCtrl = BIT2;
621 Status = EmmcPeimHcOrMmio (Bar + EMMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);
622
623 return Status;
624 }
625
626 /**
627 EMMC bus power control.
628
629 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
630
631 @param[in] Bar The mmio base address of the slot to be accessed.
632 @param[in] PowerCtrl The value setting to the power control register.
633
634 @retval TRUE There is a EMMC card attached.
635 @retval FALSE There is no a EMMC card attached.
636
637 **/
638 EFI_STATUS
639 EmmcPeimHcPowerControl (
640 IN UINTN Bar,
641 IN UINT8 PowerCtrl
642 )
643 {
644 EFI_STATUS Status;
645
646 //
647 // Clr SD Bus Power
648 //
649 PowerCtrl &= (UINT8)~BIT0;
650 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_POWER_CTRL, FALSE, sizeof (PowerCtrl), &PowerCtrl);
651 if (EFI_ERROR (Status)) {
652 return Status;
653 }
654
655 //
656 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
657 //
658 PowerCtrl |= BIT0;
659 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_POWER_CTRL, FALSE, sizeof (PowerCtrl), &PowerCtrl);
660
661 return Status;
662 }
663
664 /**
665 Set the EMMC bus width.
666
667 Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.
668
669 @param[in] Bar The mmio base address of the slot to be accessed.
670 @param[in] BusWidth The bus width used by the EMMC device, it must be 1, 4 or 8.
671
672 @retval EFI_SUCCESS The bus width is set successfully.
673 @retval Others The bus width isn't set successfully.
674
675 **/
676 EFI_STATUS
677 EmmcPeimHcSetBusWidth (
678 IN UINTN Bar,
679 IN UINT16 BusWidth
680 )
681 {
682 EFI_STATUS Status;
683 UINT8 HostCtrl1;
684
685 if (BusWidth == 1) {
686 HostCtrl1 = (UINT8)~(BIT5 | BIT1);
687 Status = EmmcPeimHcAndMmio (Bar + EMMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
688 } else if (BusWidth == 4) {
689 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_HOST_CTRL1, TRUE, sizeof (HostCtrl1), &HostCtrl1);
690 if (EFI_ERROR (Status)) {
691 return Status;
692 }
693 HostCtrl1 |= BIT1;
694 HostCtrl1 &= (UINT8)~BIT5;
695 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_HOST_CTRL1, FALSE, sizeof (HostCtrl1), &HostCtrl1);
696 } else if (BusWidth == 8) {
697 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_HOST_CTRL1, TRUE, sizeof (HostCtrl1), &HostCtrl1);
698 if (EFI_ERROR (Status)) {
699 return Status;
700 }
701 HostCtrl1 &= (UINT8)~BIT1;
702 HostCtrl1 |= BIT5;
703 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_HOST_CTRL1, FALSE, sizeof (HostCtrl1), &HostCtrl1);
704 } else {
705 ASSERT (FALSE);
706 return EFI_INVALID_PARAMETER;
707 }
708
709 return Status;
710 }
711
712 /**
713 Supply EMMC card with lowest clock frequency at initialization.
714
715 @param[in] Bar The mmio base address of the slot to be accessed.
716
717 @retval EFI_SUCCESS The clock is supplied successfully.
718 @retval Others The clock isn't supplied successfully.
719
720 **/
721 EFI_STATUS
722 EmmcPeimHcInitClockFreq (
723 IN UINTN Bar
724 )
725 {
726 EFI_STATUS Status;
727 EMMC_HC_SLOT_CAP Capability;
728 UINT32 InitFreq;
729
730 //
731 // Calculate a divisor for SD clock frequency
732 //
733 Status = EmmcPeimHcGetCapability (Bar, &Capability);
734 if (EFI_ERROR (Status)) {
735 return Status;
736 }
737
738 if (Capability.BaseClkFreq == 0) {
739 //
740 // Don't support get Base Clock Frequency information via another method
741 //
742 return EFI_UNSUPPORTED;
743 }
744 //
745 // Supply 400KHz clock frequency at initialization phase.
746 //
747 InitFreq = 400;
748 Status = EmmcPeimHcClockSupply (Bar, InitFreq);
749 return Status;
750 }
751
752 /**
753 Supply EMMC card with maximum voltage at initialization.
754
755 Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.
756
757 @param[in] Bar The mmio base address of the slot to be accessed.
758
759 @retval EFI_SUCCESS The voltage is supplied successfully.
760 @retval Others The voltage isn't supplied successfully.
761
762 **/
763 EFI_STATUS
764 EmmcPeimHcInitPowerVoltage (
765 IN UINTN Bar
766 )
767 {
768 EFI_STATUS Status;
769 EMMC_HC_SLOT_CAP Capability;
770 UINT8 MaxVoltage;
771 UINT8 HostCtrl2;
772
773 //
774 // Get the support voltage of the Host Controller
775 //
776 Status = EmmcPeimHcGetCapability (Bar, &Capability);
777 if (EFI_ERROR (Status)) {
778 return Status;
779 }
780 //
781 // Calculate supported maximum voltage according to SD Bus Voltage Select
782 //
783 if (Capability.Voltage33 != 0) {
784 //
785 // Support 3.3V
786 //
787 MaxVoltage = 0x0E;
788 } else if (Capability.Voltage30 != 0) {
789 //
790 // Support 3.0V
791 //
792 MaxVoltage = 0x0C;
793 } else if (Capability.Voltage18 != 0) {
794 //
795 // Support 1.8V
796 //
797 MaxVoltage = 0x0A;
798 HostCtrl2 = BIT3;
799 Status = EmmcPeimHcOrMmio (Bar + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
800 if (EFI_ERROR (Status)) {
801 return Status;
802 }
803 MicroSecondDelay (5000);
804 } else {
805 ASSERT (FALSE);
806 return EFI_DEVICE_ERROR;
807 }
808
809 //
810 // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register
811 //
812 Status = EmmcPeimHcPowerControl (Bar, MaxVoltage);
813
814 return Status;
815 }
816
817 /**
818 Initialize the Timeout Control register with most conservative value at initialization.
819
820 Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.
821
822 @param[in] Bar The mmio base address of the slot to be accessed.
823
824 @retval EFI_SUCCESS The timeout control register is configured successfully.
825 @retval Others The timeout control register isn't configured successfully.
826
827 **/
828 EFI_STATUS
829 EmmcPeimHcInitTimeoutCtrl (
830 IN UINTN Bar
831 )
832 {
833 EFI_STATUS Status;
834 UINT8 Timeout;
835
836 Timeout = 0x0E;
837 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_TIMEOUT_CTRL, FALSE, sizeof (Timeout), &Timeout);
838
839 return Status;
840 }
841
842 /**
843 Initial EMMC host controller with lowest clock frequency, max power and max timeout value
844 at initialization.
845
846 @param[in] Bar The mmio base address of the slot to be accessed.
847
848 @retval EFI_SUCCESS The host controller is initialized successfully.
849 @retval Others The host controller isn't initialized successfully.
850
851 **/
852 EFI_STATUS
853 EmmcPeimHcInitHost (
854 IN UINTN Bar
855 )
856 {
857 EFI_STATUS Status;
858
859 Status = EmmcPeimHcInitClockFreq (Bar);
860 if (EFI_ERROR (Status)) {
861 return Status;
862 }
863
864 Status = EmmcPeimHcInitPowerVoltage (Bar);
865 if (EFI_ERROR (Status)) {
866 return Status;
867 }
868
869 Status = EmmcPeimHcInitTimeoutCtrl (Bar);
870 return Status;
871 }
872
873 /**
874 Turn on/off LED.
875
876 @param[in] Bar The mmio base address of the slot to be accessed.
877 @param[in] On The boolean to turn on/off LED.
878
879 @retval EFI_SUCCESS The LED is turned on/off successfully.
880 @retval Others The LED isn't turned on/off successfully.
881
882 **/
883 EFI_STATUS
884 EmmcPeimHcLedOnOff (
885 IN UINTN Bar,
886 IN BOOLEAN On
887 )
888 {
889 EFI_STATUS Status;
890 UINT8 HostCtrl1;
891
892 if (On) {
893 HostCtrl1 = BIT0;
894 Status = EmmcPeimHcOrMmio (Bar + EMMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
895 } else {
896 HostCtrl1 = (UINT8)~BIT0;
897 Status = EmmcPeimHcAndMmio (Bar + EMMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
898 }
899
900 return Status;
901 }
902
903 /**
904 Build ADMA descriptor table for transfer.
905
906 Refer to SD Host Controller Simplified spec 3.0 Section 1.13 for details.
907
908 @param[in] Trb The pointer to the EMMC_TRB instance.
909
910 @retval EFI_SUCCESS The ADMA descriptor table is created successfully.
911 @retval Others The ADMA descriptor table isn't created successfully.
912
913 **/
914 EFI_STATUS
915 BuildAdmaDescTable (
916 IN EMMC_TRB *Trb
917 )
918 {
919 EFI_PHYSICAL_ADDRESS Data;
920 UINT64 DataLen;
921 UINT64 Entries;
922 UINT32 Index;
923 UINT64 Remaining;
924 UINT32 Address;
925
926 Data = Trb->DataPhy;
927 DataLen = Trb->DataLen;
928 //
929 // Only support 32bit ADMA Descriptor Table
930 //
931 if ((Data >= 0x100000000ul) || ((Data + DataLen) > 0x100000000ul)) {
932 return EFI_INVALID_PARAMETER;
933 }
934 //
935 // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)
936 // for 32-bit address descriptor table.
937 //
938 if ((Data & (BIT0 | BIT1)) != 0) {
939 DEBUG ((EFI_D_INFO, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data));
940 }
941
942 Entries = DivU64x32 ((DataLen + ADMA_MAX_DATA_PER_LINE - 1), ADMA_MAX_DATA_PER_LINE);
943
944 Trb->AdmaDescSize = (UINTN)MultU64x32 (Entries, sizeof (EMMC_HC_ADMA_DESC_LINE));
945 Trb->AdmaDesc = EmmcPeimAllocateMem (Trb->Slot->Private->Pool, Trb->AdmaDescSize);
946 if (Trb->AdmaDesc == NULL) {
947 return EFI_OUT_OF_RESOURCES;
948 }
949
950 Remaining = DataLen;
951 Address = (UINT32)Data;
952 for (Index = 0; Index < Entries; Index++) {
953 if (Remaining <= ADMA_MAX_DATA_PER_LINE) {
954 Trb->AdmaDesc[Index].Valid = 1;
955 Trb->AdmaDesc[Index].Act = 2;
956 Trb->AdmaDesc[Index].Length = (UINT16)Remaining;
957 Trb->AdmaDesc[Index].Address = Address;
958 break;
959 } else {
960 Trb->AdmaDesc[Index].Valid = 1;
961 Trb->AdmaDesc[Index].Act = 2;
962 Trb->AdmaDesc[Index].Length = 0;
963 Trb->AdmaDesc[Index].Address = Address;
964 }
965
966 Remaining -= ADMA_MAX_DATA_PER_LINE;
967 Address += ADMA_MAX_DATA_PER_LINE;
968 }
969
970 //
971 // Set the last descriptor line as end of descriptor table
972 //
973 Trb->AdmaDesc[Index].End = 1;
974 return EFI_SUCCESS;
975 }
976
977 /**
978 Create a new TRB for the EMMC cmd request.
979
980 @param[in] Slot The slot number of the EMMC card to send the command to.
981 @param[in] Packet A pointer to the SD command data structure.
982
983 @return Created Trb or NULL.
984
985 **/
986 EMMC_TRB *
987 EmmcPeimCreateTrb (
988 IN EMMC_PEIM_HC_SLOT *Slot,
989 IN EMMC_COMMAND_PACKET *Packet
990 )
991 {
992 EMMC_TRB *Trb;
993 EFI_STATUS Status;
994 EMMC_HC_SLOT_CAP Capability;
995 EDKII_IOMMU_OPERATION MapOp;
996 UINTN MapLength;
997
998 //
999 // Calculate a divisor for SD clock frequency
1000 //
1001 Status = EmmcPeimHcGetCapability (Slot->EmmcHcBase, &Capability);
1002 if (EFI_ERROR (Status)) {
1003 return NULL;
1004 }
1005
1006 Trb = AllocateZeroPool (sizeof (EMMC_TRB));
1007 if (Trb == NULL) {
1008 return NULL;
1009 }
1010
1011 Trb->Slot = Slot;
1012 Trb->BlockSize = 0x200;
1013 Trb->Packet = Packet;
1014 Trb->Timeout = Packet->Timeout;
1015
1016 if ((Packet->InTransferLength != 0) && (Packet->InDataBuffer != NULL)) {
1017 Trb->Data = Packet->InDataBuffer;
1018 Trb->DataLen = Packet->InTransferLength;
1019 Trb->Read = TRUE;
1020 } else if ((Packet->OutTransferLength != 0) && (Packet->OutDataBuffer != NULL)) {
1021 Trb->Data = Packet->OutDataBuffer;
1022 Trb->DataLen = Packet->OutTransferLength;
1023 Trb->Read = FALSE;
1024 } else if ((Packet->InTransferLength == 0) && (Packet->OutTransferLength == 0)) {
1025 Trb->Data = NULL;
1026 Trb->DataLen = 0;
1027 } else {
1028 goto Error;
1029 }
1030
1031 if ((Trb->DataLen != 0) && (Trb->DataLen < Trb->BlockSize)) {
1032 Trb->BlockSize = (UINT16)Trb->DataLen;
1033 }
1034
1035 if (Packet->EmmcCmdBlk->CommandIndex == EMMC_SEND_TUNING_BLOCK) {
1036 Trb->Mode = EmmcPioMode;
1037 } else {
1038 if (Trb->Read) {
1039 MapOp = EdkiiIoMmuOperationBusMasterWrite;
1040 } else {
1041 MapOp = EdkiiIoMmuOperationBusMasterRead;
1042 }
1043
1044 if (Trb->DataLen != 0) {
1045 MapLength = Trb->DataLen;
1046 Status = IoMmuMap (MapOp, Trb->Data, &MapLength, &Trb->DataPhy, &Trb->DataMap);
1047
1048 if (EFI_ERROR (Status) || (MapLength != Trb->DataLen)) {
1049 DEBUG ((DEBUG_ERROR, "EmmcPeimCreateTrb: Fail to map data buffer.\n"));
1050 goto Error;
1051 }
1052 }
1053
1054 if (Trb->DataLen == 0) {
1055 Trb->Mode = EmmcNoData;
1056 } else if (Capability.Adma2 != 0) {
1057 Trb->Mode = EmmcAdmaMode;
1058 Status = BuildAdmaDescTable (Trb);
1059 if (EFI_ERROR (Status)) {
1060 goto Error;
1061 }
1062 } else if (Capability.Sdma != 0) {
1063 Trb->Mode = EmmcSdmaMode;
1064 } else {
1065 Trb->Mode = EmmcPioMode;
1066 }
1067 }
1068 return Trb;
1069
1070 Error:
1071 EmmcPeimFreeTrb (Trb);
1072 return NULL;
1073 }
1074
1075 /**
1076 Free the resource used by the TRB.
1077
1078 @param[in] Trb The pointer to the EMMC_TRB instance.
1079
1080 **/
1081 VOID
1082 EmmcPeimFreeTrb (
1083 IN EMMC_TRB *Trb
1084 )
1085 {
1086 if ((Trb != NULL) && (Trb->DataMap != NULL)) {
1087 IoMmuUnmap (Trb->DataMap);
1088 }
1089
1090 if ((Trb != NULL) && (Trb->AdmaDesc != NULL)) {
1091 EmmcPeimFreeMem (Trb->Slot->Private->Pool, Trb->AdmaDesc, Trb->AdmaDescSize);
1092 }
1093
1094 if (Trb != NULL) {
1095 FreePool (Trb);
1096 }
1097 return;
1098 }
1099
1100 /**
1101 Check if the env is ready for execute specified TRB.
1102
1103 @param[in] Bar The mmio base address of the slot to be accessed.
1104 @param[in] Trb The pointer to the EMMC_TRB instance.
1105
1106 @retval EFI_SUCCESS The env is ready for TRB execution.
1107 @retval EFI_NOT_READY The env is not ready for TRB execution.
1108 @retval Others Some erros happen.
1109
1110 **/
1111 EFI_STATUS
1112 EmmcPeimCheckTrbEnv (
1113 IN UINTN Bar,
1114 IN EMMC_TRB *Trb
1115 )
1116 {
1117 EFI_STATUS Status;
1118 EMMC_COMMAND_PACKET *Packet;
1119 UINT32 PresentState;
1120
1121 Packet = Trb->Packet;
1122
1123 if ((Packet->EmmcCmdBlk->CommandType == EmmcCommandTypeAdtc) ||
1124 (Packet->EmmcCmdBlk->ResponseType == EmmcResponceTypeR1b) ||
1125 (Packet->EmmcCmdBlk->ResponseType == EmmcResponceTypeR5b)) {
1126 //
1127 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
1128 // the Present State register to be 0
1129 //
1130 PresentState = BIT0 | BIT1;
1131 } else {
1132 //
1133 // Wait Command Inhibit (CMD) in the Present State register
1134 // to be 0
1135 //
1136 PresentState = BIT0;
1137 }
1138
1139 Status = EmmcPeimHcCheckMmioSet (
1140 Bar + EMMC_HC_PRESENT_STATE,
1141 sizeof (PresentState),
1142 PresentState,
1143 0
1144 );
1145
1146 return Status;
1147 }
1148
1149 /**
1150 Wait for the env to be ready for execute specified TRB.
1151
1152 @param[in] Bar The mmio base address of the slot to be accessed.
1153 @param[in] Trb The pointer to the EMMC_TRB instance.
1154
1155 @retval EFI_SUCCESS The env is ready for TRB execution.
1156 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
1157 @retval Others Some erros happen.
1158
1159 **/
1160 EFI_STATUS
1161 EmmcPeimWaitTrbEnv (
1162 IN UINTN Bar,
1163 IN EMMC_TRB *Trb
1164 )
1165 {
1166 EFI_STATUS Status;
1167 EMMC_COMMAND_PACKET *Packet;
1168 UINT64 Timeout;
1169 BOOLEAN InfiniteWait;
1170
1171 //
1172 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1173 //
1174 Packet = Trb->Packet;
1175 Timeout = Packet->Timeout;
1176 if (Timeout == 0) {
1177 InfiniteWait = TRUE;
1178 } else {
1179 InfiniteWait = FALSE;
1180 }
1181
1182 while (InfiniteWait || (Timeout > 0)) {
1183 //
1184 // Check Trb execution result by reading Normal Interrupt Status register.
1185 //
1186 Status = EmmcPeimCheckTrbEnv (Bar, Trb);
1187 if (Status != EFI_NOT_READY) {
1188 return Status;
1189 }
1190 //
1191 // Stall for 1 microsecond.
1192 //
1193 MicroSecondDelay (1);
1194
1195 Timeout--;
1196 }
1197
1198 return EFI_TIMEOUT;
1199 }
1200
1201 /**
1202 Execute the specified TRB.
1203
1204 @param[in] Bar The mmio base address of the slot to be accessed.
1205 @param[in] Trb The pointer to the EMMC_TRB instance.
1206
1207 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
1208 @retval Others Some erros happen when sending this request to the host controller.
1209
1210 **/
1211 EFI_STATUS
1212 EmmcPeimExecTrb (
1213 IN UINTN Bar,
1214 IN EMMC_TRB *Trb
1215 )
1216 {
1217 EFI_STATUS Status;
1218 EMMC_COMMAND_PACKET *Packet;
1219 UINT16 Cmd;
1220 UINT16 IntStatus;
1221 UINT32 Argument;
1222 UINT16 BlkCount;
1223 UINT16 BlkSize;
1224 UINT16 TransMode;
1225 UINT8 HostCtrl1;
1226 UINT32 SdmaAddr;
1227 UINT64 AdmaAddr;
1228
1229 Packet = Trb->Packet;
1230 //
1231 // Clear all bits in Error Interrupt Status Register
1232 //
1233 IntStatus = 0xFFFF;
1234 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_ERR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);
1235 if (EFI_ERROR (Status)) {
1236 return Status;
1237 }
1238 //
1239 // Clear all bits in Normal Interrupt Status Register
1240 //
1241 IntStatus = 0xFFFF;
1242 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);
1243 if (EFI_ERROR (Status)) {
1244 return Status;
1245 }
1246 //
1247 // Set Host Control 1 register DMA Select field
1248 //
1249 if (Trb->Mode == EmmcAdmaMode) {
1250 HostCtrl1 = BIT4;
1251 Status = EmmcPeimHcOrMmio (Bar + EMMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
1252 if (EFI_ERROR (Status)) {
1253 return Status;
1254 }
1255 }
1256
1257 EmmcPeimHcLedOnOff (Bar, TRUE);
1258
1259 if (Trb->Mode == EmmcSdmaMode) {
1260 if ((UINT64)(UINTN)Trb->DataPhy >= 0x100000000ul) {
1261 return EFI_INVALID_PARAMETER;
1262 }
1263
1264 SdmaAddr = (UINT32)(UINTN)Trb->DataPhy;
1265 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_SDMA_ADDR, FALSE, sizeof (SdmaAddr), &SdmaAddr);
1266 if (EFI_ERROR (Status)) {
1267 return Status;
1268 }
1269 } else if (Trb->Mode == EmmcAdmaMode) {
1270 AdmaAddr = (UINT64)(UINTN)Trb->AdmaDesc;
1271 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_ADMA_SYS_ADDR, FALSE, sizeof (AdmaAddr), &AdmaAddr);
1272 if (EFI_ERROR (Status)) {
1273 return Status;
1274 }
1275 }
1276
1277 BlkSize = Trb->BlockSize;
1278 if (Trb->Mode == EmmcSdmaMode) {
1279 //
1280 // Set SDMA boundary to be 512K bytes.
1281 //
1282 BlkSize |= 0x7000;
1283 }
1284
1285 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_BLK_SIZE, FALSE, sizeof (BlkSize), &BlkSize);
1286 if (EFI_ERROR (Status)) {
1287 return Status;
1288 }
1289
1290 BlkCount = 0;
1291 if (Trb->Mode != EmmcNoData) {
1292 //
1293 // Calcuate Block Count.
1294 //
1295 BlkCount = (UINT16)(Trb->DataLen / Trb->BlockSize);
1296 }
1297
1298 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_BLK_COUNT, FALSE, sizeof (BlkCount), &BlkCount);
1299 if (EFI_ERROR (Status)) {
1300 return Status;
1301 }
1302
1303 Argument = Packet->EmmcCmdBlk->CommandArgument;
1304 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_ARG1, FALSE, sizeof (Argument), &Argument);
1305 if (EFI_ERROR (Status)) {
1306 return Status;
1307 }
1308
1309 TransMode = 0;
1310 if (Trb->Mode != EmmcNoData) {
1311 if (Trb->Mode != EmmcPioMode) {
1312 TransMode |= BIT0;
1313 }
1314 if (Trb->Read) {
1315 TransMode |= BIT4;
1316 }
1317 if (BlkCount > 1) {
1318 TransMode |= BIT5 | BIT1;
1319 }
1320 }
1321
1322 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_TRANS_MOD, FALSE, sizeof (TransMode), &TransMode);
1323 if (EFI_ERROR (Status)) {
1324 return Status;
1325 }
1326
1327 Cmd = (UINT16)LShiftU64(Packet->EmmcCmdBlk->CommandIndex, 8);
1328 if (Packet->EmmcCmdBlk->CommandType == EmmcCommandTypeAdtc) {
1329 Cmd |= BIT5;
1330 }
1331 //
1332 // Convert ResponseType to value
1333 //
1334 if (Packet->EmmcCmdBlk->CommandType != EmmcCommandTypeBc) {
1335 switch (Packet->EmmcCmdBlk->ResponseType) {
1336 case EmmcResponceTypeR1:
1337 case EmmcResponceTypeR5:
1338 case EmmcResponceTypeR6:
1339 case EmmcResponceTypeR7:
1340 Cmd |= (BIT1 | BIT3 | BIT4);
1341 break;
1342 case EmmcResponceTypeR2:
1343 Cmd |= (BIT0 | BIT3);
1344 break;
1345 case EmmcResponceTypeR3:
1346 case EmmcResponceTypeR4:
1347 Cmd |= BIT1;
1348 break;
1349 case EmmcResponceTypeR1b:
1350 case EmmcResponceTypeR5b:
1351 Cmd |= (BIT0 | BIT1 | BIT3 | BIT4);
1352 break;
1353 default:
1354 ASSERT (FALSE);
1355 break;
1356 }
1357 }
1358 //
1359 // Execute cmd
1360 //
1361 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_COMMAND, FALSE, sizeof (Cmd), &Cmd);
1362 return Status;
1363 }
1364
1365 /**
1366 Check the TRB execution result.
1367
1368 @param[in] Bar The mmio base address of the slot to be accessed.
1369 @param[in] Trb The pointer to the EMMC_TRB instance.
1370
1371 @retval EFI_SUCCESS The TRB is executed successfully.
1372 @retval EFI_NOT_READY The TRB is not completed for execution.
1373 @retval Others Some erros happen when executing this request.
1374
1375 **/
1376 EFI_STATUS
1377 EmmcPeimCheckTrbResult (
1378 IN UINTN Bar,
1379 IN EMMC_TRB *Trb
1380 )
1381 {
1382 EFI_STATUS Status;
1383 EMMC_COMMAND_PACKET *Packet;
1384 UINT16 IntStatus;
1385 UINT32 Response[4];
1386 UINT32 SdmaAddr;
1387 UINT8 Index;
1388 UINT8 SwReset;
1389 UINT32 PioLength;
1390
1391 SwReset = 0;
1392 Packet = Trb->Packet;
1393 //
1394 // Check Trb execution result by reading Normal Interrupt Status register.
1395 //
1396 Status = EmmcPeimHcRwMmio (
1397 Bar + EMMC_HC_NOR_INT_STS,
1398 TRUE,
1399 sizeof (IntStatus),
1400 &IntStatus
1401 );
1402 if (EFI_ERROR (Status)) {
1403 goto Done;
1404 }
1405 //
1406 // Check Transfer Complete bit is set or not.
1407 //
1408 if ((IntStatus & BIT1) == BIT1) {
1409 if ((IntStatus & BIT15) == BIT15) {
1410 //
1411 // Read Error Interrupt Status register to check if the error is
1412 // Data Timeout Error.
1413 // If yes, treat it as success as Transfer Complete has higher
1414 // priority than Data Timeout Error.
1415 //
1416 Status = EmmcPeimHcRwMmio (
1417 Bar + EMMC_HC_ERR_INT_STS,
1418 TRUE,
1419 sizeof (IntStatus),
1420 &IntStatus
1421 );
1422 if (!EFI_ERROR (Status)) {
1423 if ((IntStatus & BIT4) == BIT4) {
1424 Status = EFI_SUCCESS;
1425 } else {
1426 Status = EFI_DEVICE_ERROR;
1427 }
1428 }
1429 }
1430
1431 goto Done;
1432 }
1433 //
1434 // Check if there is a error happened during cmd execution.
1435 // If yes, then do error recovery procedure to follow SD Host Controller
1436 // Simplified Spec 3.0 section 3.10.1.
1437 //
1438 if ((IntStatus & BIT15) == BIT15) {
1439 Status = EmmcPeimHcRwMmio (
1440 Bar + EMMC_HC_ERR_INT_STS,
1441 TRUE,
1442 sizeof (IntStatus),
1443 &IntStatus
1444 );
1445 if (EFI_ERROR (Status)) {
1446 goto Done;
1447 }
1448
1449 if ((IntStatus & 0x0F) != 0) {
1450 SwReset |= BIT1;
1451 }
1452 if ((IntStatus & 0xF0) != 0) {
1453 SwReset |= BIT2;
1454 }
1455
1456 Status = EmmcPeimHcRwMmio (
1457 Bar + EMMC_HC_SW_RST,
1458 FALSE,
1459 sizeof (SwReset),
1460 &SwReset
1461 );
1462 if (EFI_ERROR (Status)) {
1463 goto Done;
1464 }
1465 Status = EmmcPeimHcWaitMmioSet (
1466 Bar + EMMC_HC_SW_RST,
1467 sizeof (SwReset),
1468 0xFF,
1469 0,
1470 EMMC_TIMEOUT
1471 );
1472 if (EFI_ERROR (Status)) {
1473 goto Done;
1474 }
1475
1476 Status = EFI_DEVICE_ERROR;
1477 goto Done;
1478 }
1479 //
1480 // Check if DMA interrupt is signalled for the SDMA transfer.
1481 //
1482 if ((Trb->Mode == EmmcSdmaMode) && ((IntStatus & BIT3) == BIT3)) {
1483 //
1484 // Clear DMA interrupt bit.
1485 //
1486 IntStatus = BIT3;
1487 Status = EmmcPeimHcRwMmio (
1488 Bar + EMMC_HC_NOR_INT_STS,
1489 FALSE,
1490 sizeof (IntStatus),
1491 &IntStatus
1492 );
1493 if (EFI_ERROR (Status)) {
1494 goto Done;
1495 }
1496 //
1497 // Update SDMA Address register.
1498 //
1499 SdmaAddr = EMMC_SDMA_ROUND_UP ((UINT32)(UINTN)Trb->DataPhy, EMMC_SDMA_BOUNDARY);
1500 Status = EmmcPeimHcRwMmio (
1501 Bar + EMMC_HC_SDMA_ADDR,
1502 FALSE,
1503 sizeof (UINT32),
1504 &SdmaAddr
1505 );
1506 if (EFI_ERROR (Status)) {
1507 goto Done;
1508 }
1509 Trb->DataPhy = (UINT32)(UINTN)SdmaAddr;
1510 }
1511
1512 if ((Packet->EmmcCmdBlk->CommandType != EmmcCommandTypeAdtc) &&
1513 (Packet->EmmcCmdBlk->ResponseType != EmmcResponceTypeR1b) &&
1514 (Packet->EmmcCmdBlk->ResponseType != EmmcResponceTypeR5b)) {
1515 if ((IntStatus & BIT0) == BIT0) {
1516 Status = EFI_SUCCESS;
1517 goto Done;
1518 }
1519 }
1520
1521 if (Packet->EmmcCmdBlk->CommandIndex == EMMC_SEND_TUNING_BLOCK) {
1522 //
1523 // When performing tuning procedure (Execute Tuning is set to 1) through PIO mode,
1524 // wait Buffer Read Ready bit of Normal Interrupt Status Register to be 1.
1525 // Refer to SD Host Controller Simplified Specification 3.0 figure 2-29 for details.
1526 //
1527 if ((IntStatus & BIT5) == BIT5) {
1528 //
1529 // Clear Buffer Read Ready interrupt at first.
1530 //
1531 IntStatus = BIT5;
1532 EmmcPeimHcRwMmio (Bar + EMMC_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);
1533 //
1534 // Read data out from Buffer Port register
1535 //
1536 for (PioLength = 0; PioLength < Trb->DataLen; PioLength += 4) {
1537 EmmcPeimHcRwMmio (Bar + EMMC_HC_BUF_DAT_PORT, TRUE, 4, (UINT8*)Trb->Data + PioLength);
1538 }
1539 Status = EFI_SUCCESS;
1540 goto Done;
1541 }
1542 }
1543
1544 Status = EFI_NOT_READY;
1545 Done:
1546 //
1547 // Get response data when the cmd is executed successfully.
1548 //
1549 if (!EFI_ERROR (Status)) {
1550 if (Packet->EmmcCmdBlk->CommandType != EmmcCommandTypeBc) {
1551 for (Index = 0; Index < 4; Index++) {
1552 Status = EmmcPeimHcRwMmio (
1553 Bar + EMMC_HC_RESPONSE + Index * 4,
1554 TRUE,
1555 sizeof (UINT32),
1556 &Response[Index]
1557 );
1558 if (EFI_ERROR (Status)) {
1559 EmmcPeimHcLedOnOff (Bar, FALSE);
1560 return Status;
1561 }
1562 }
1563 CopyMem (Packet->EmmcStatusBlk, Response, sizeof (Response));
1564 }
1565 }
1566
1567 if (Status != EFI_NOT_READY) {
1568 EmmcPeimHcLedOnOff (Bar, FALSE);
1569 }
1570
1571 return Status;
1572 }
1573
1574 /**
1575 Wait for the TRB execution result.
1576
1577 @param[in] Bar The mmio base address of the slot to be accessed.
1578 @param[in] Trb The pointer to the EMMC_TRB instance.
1579
1580 @retval EFI_SUCCESS The TRB is executed successfully.
1581 @retval Others Some erros happen when executing this request.
1582
1583 **/
1584 EFI_STATUS
1585 EmmcPeimWaitTrbResult (
1586 IN UINTN Bar,
1587 IN EMMC_TRB *Trb
1588 )
1589 {
1590 EFI_STATUS Status;
1591 EMMC_COMMAND_PACKET *Packet;
1592 UINT64 Timeout;
1593 BOOLEAN InfiniteWait;
1594
1595 Packet = Trb->Packet;
1596 //
1597 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1598 //
1599 Timeout = Packet->Timeout;
1600 if (Timeout == 0) {
1601 InfiniteWait = TRUE;
1602 } else {
1603 InfiniteWait = FALSE;
1604 }
1605
1606 while (InfiniteWait || (Timeout > 0)) {
1607 //
1608 // Check Trb execution result by reading Normal Interrupt Status register.
1609 //
1610 Status = EmmcPeimCheckTrbResult (Bar, Trb);
1611 if (Status != EFI_NOT_READY) {
1612 return Status;
1613 }
1614 //
1615 // Stall for 1 microsecond.
1616 //
1617 MicroSecondDelay (1);
1618
1619 Timeout--;
1620 }
1621
1622 return EFI_TIMEOUT;
1623 }
1624
1625 /**
1626 Sends EMMC command to an EMMC card that is attached to the EMMC controller.
1627
1628 If Packet is successfully sent to the EMMC card, then EFI_SUCCESS is returned.
1629
1630 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
1631
1632 If Slot is not in a valid range for the EMMC controller, then EFI_INVALID_PARAMETER
1633 is returned.
1634
1635 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
1636 EFI_INVALID_PARAMETER is returned.
1637
1638 @param[in] Slot The slot number of the Emmc card to send the command to.
1639 @param[in,out] Packet A pointer to the EMMC command data structure.
1640
1641 @retval EFI_SUCCESS The EMMC Command Packet was sent by the host.
1642 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
1643 command Packet.
1644 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
1645 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
1646 OutDataBuffer are NULL.
1647 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1648 @retval EFI_UNSUPPORTED The command described by the EMMC Command Packet is not
1649 supported by the host controller.
1650 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
1651 limit supported by EMMC card ( i.e. if the number of bytes
1652 exceed the Last LBA).
1653
1654 **/
1655 EFI_STATUS
1656 EFIAPI
1657 EmmcPeimExecCmd (
1658 IN EMMC_PEIM_HC_SLOT *Slot,
1659 IN OUT EMMC_COMMAND_PACKET *Packet
1660 )
1661 {
1662 EFI_STATUS Status;
1663 EMMC_TRB *Trb;
1664
1665 if (Packet == NULL) {
1666 return EFI_INVALID_PARAMETER;
1667 }
1668
1669 if ((Packet->EmmcCmdBlk == NULL) || (Packet->EmmcStatusBlk == NULL)) {
1670 return EFI_INVALID_PARAMETER;
1671 }
1672
1673 if ((Packet->OutDataBuffer == NULL) && (Packet->OutTransferLength != 0)) {
1674 return EFI_INVALID_PARAMETER;
1675 }
1676
1677 if ((Packet->InDataBuffer == NULL) && (Packet->InTransferLength != 0)) {
1678 return EFI_INVALID_PARAMETER;
1679 }
1680
1681 Trb = EmmcPeimCreateTrb (Slot, Packet);
1682 if (Trb == NULL) {
1683 return EFI_OUT_OF_RESOURCES;
1684 }
1685
1686 Status = EmmcPeimWaitTrbEnv (Slot->EmmcHcBase, Trb);
1687 if (EFI_ERROR (Status)) {
1688 goto Done;
1689 }
1690
1691 Status = EmmcPeimExecTrb (Slot->EmmcHcBase, Trb);
1692 if (EFI_ERROR (Status)) {
1693 goto Done;
1694 }
1695
1696 Status = EmmcPeimWaitTrbResult (Slot->EmmcHcBase, Trb);
1697 if (EFI_ERROR (Status)) {
1698 goto Done;
1699 }
1700
1701 Done:
1702 EmmcPeimFreeTrb (Trb);
1703
1704 return Status;
1705 }
1706
1707 /**
1708 Send command GO_IDLE_STATE (CMD0 with argument of 0x00000000) to the device to
1709 make it go to Idle State.
1710
1711 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1712
1713 @param[in] Slot The slot number of the Emmc card to send the command to.
1714
1715 @retval EFI_SUCCESS The EMMC device is reset correctly.
1716 @retval Others The device reset fails.
1717
1718 **/
1719 EFI_STATUS
1720 EmmcPeimReset (
1721 IN EMMC_PEIM_HC_SLOT *Slot
1722 )
1723 {
1724 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1725 EMMC_STATUS_BLOCK EmmcStatusBlk;
1726 EMMC_COMMAND_PACKET Packet;
1727 EFI_STATUS Status;
1728
1729 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1730 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1731 ZeroMem (&Packet, sizeof (Packet));
1732
1733 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1734 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1735 Packet.Timeout = EMMC_TIMEOUT;
1736
1737 EmmcCmdBlk.CommandIndex = EMMC_GO_IDLE_STATE;
1738 EmmcCmdBlk.CommandType = EmmcCommandTypeBc;
1739 EmmcCmdBlk.ResponseType = 0;
1740 EmmcCmdBlk.CommandArgument = 0;
1741
1742 Status = EmmcPeimExecCmd (Slot, &Packet);
1743
1744 return Status;
1745 }
1746
1747 /**
1748 Send command SEND_OP_COND to the EMMC device to get the data of the OCR register.
1749
1750 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1751
1752 @param[in] Slot The slot number of the Emmc card to send the command to.
1753 @param[in, out] Argument On input, the argument of SEND_OP_COND is to send to the device.
1754 On output, the argument is the value of OCR register.
1755
1756 @retval EFI_SUCCESS The operation is done correctly.
1757 @retval Others The operation fails.
1758
1759 **/
1760 EFI_STATUS
1761 EmmcPeimGetOcr (
1762 IN EMMC_PEIM_HC_SLOT *Slot,
1763 IN OUT UINT32 *Argument
1764 )
1765 {
1766 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1767 EMMC_STATUS_BLOCK EmmcStatusBlk;
1768 EMMC_COMMAND_PACKET Packet;
1769 EFI_STATUS Status;
1770
1771 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1772 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1773 ZeroMem (&Packet, sizeof (Packet));
1774
1775 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1776 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1777 Packet.Timeout = EMMC_TIMEOUT;
1778
1779 EmmcCmdBlk.CommandIndex = EMMC_SEND_OP_COND;
1780 EmmcCmdBlk.CommandType = EmmcCommandTypeBcr;
1781 EmmcCmdBlk.ResponseType = EmmcResponceTypeR3;
1782 EmmcCmdBlk.CommandArgument = *Argument;
1783
1784 Status = EmmcPeimExecCmd (Slot, &Packet);
1785 if (!EFI_ERROR (Status)) {
1786 //
1787 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
1788 //
1789 *Argument = EmmcStatusBlk.Resp0;
1790 }
1791
1792 return Status;
1793 }
1794
1795 /**
1796 Broadcast command ALL_SEND_CID to the bus to ask all the EMMC devices to send the
1797 data of their CID registers.
1798
1799 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1800
1801 @param[in] Slot The slot number of the Emmc card to send the command to.
1802
1803 @retval EFI_SUCCESS The operation is done correctly.
1804 @retval Others The operation fails.
1805
1806 **/
1807 EFI_STATUS
1808 EmmcPeimGetAllCid (
1809 IN EMMC_PEIM_HC_SLOT *Slot
1810 )
1811 {
1812 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1813 EMMC_STATUS_BLOCK EmmcStatusBlk;
1814 EMMC_COMMAND_PACKET Packet;
1815 EFI_STATUS Status;
1816
1817 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1818 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1819 ZeroMem (&Packet, sizeof (Packet));
1820
1821 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1822 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1823 Packet.Timeout = EMMC_TIMEOUT;
1824
1825 EmmcCmdBlk.CommandIndex = EMMC_ALL_SEND_CID;
1826 EmmcCmdBlk.CommandType = EmmcCommandTypeBcr;
1827 EmmcCmdBlk.ResponseType = EmmcResponceTypeR2;
1828 EmmcCmdBlk.CommandArgument = 0;
1829
1830 Status = EmmcPeimExecCmd (Slot, &Packet);
1831
1832 return Status;
1833 }
1834
1835 /**
1836 Send command SET_RELATIVE_ADDR to the EMMC device to assign a Relative device
1837 Address (RCA).
1838
1839 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1840
1841 @param[in] Slot The slot number of the Emmc card to send the command to.
1842 @param[in] Rca The relative device address to be assigned.
1843
1844 @retval EFI_SUCCESS The operation is done correctly.
1845 @retval Others The operation fails.
1846
1847 **/
1848 EFI_STATUS
1849 EmmcPeimSetRca (
1850 IN EMMC_PEIM_HC_SLOT *Slot,
1851 IN UINT32 Rca
1852 )
1853 {
1854 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1855 EMMC_STATUS_BLOCK EmmcStatusBlk;
1856 EMMC_COMMAND_PACKET Packet;
1857 EFI_STATUS Status;
1858
1859 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1860 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1861 ZeroMem (&Packet, sizeof (Packet));
1862
1863 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1864 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1865 Packet.Timeout = EMMC_TIMEOUT;
1866
1867 EmmcCmdBlk.CommandIndex = EMMC_SET_RELATIVE_ADDR;
1868 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
1869 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
1870 EmmcCmdBlk.CommandArgument = Rca << 16;
1871
1872 Status = EmmcPeimExecCmd (Slot, &Packet);
1873
1874 return Status;
1875 }
1876
1877 /**
1878 Send command SEND_CSD to the EMMC device to get the data of the CSD register.
1879
1880 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1881
1882 @param[in] Slot The slot number of the Emmc card to send the command to.
1883 @param[in] Rca The relative device address of selected device.
1884 @param[out] Csd The buffer to store the content of the CSD register.
1885 Note the caller should ignore the lowest byte of this
1886 buffer as the content of this byte is meaningless even
1887 if the operation succeeds.
1888
1889 @retval EFI_SUCCESS The operation is done correctly.
1890 @retval Others The operation fails.
1891
1892 **/
1893 EFI_STATUS
1894 EmmcPeimGetCsd (
1895 IN EMMC_PEIM_HC_SLOT *Slot,
1896 IN UINT32 Rca,
1897 OUT EMMC_CSD *Csd
1898 )
1899 {
1900 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1901 EMMC_STATUS_BLOCK EmmcStatusBlk;
1902 EMMC_COMMAND_PACKET Packet;
1903 EFI_STATUS Status;
1904
1905 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1906 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1907 ZeroMem (&Packet, sizeof (Packet));
1908
1909 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1910 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1911 Packet.Timeout = EMMC_TIMEOUT;
1912
1913 EmmcCmdBlk.CommandIndex = EMMC_SEND_CSD;
1914 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
1915 EmmcCmdBlk.ResponseType = EmmcResponceTypeR2;
1916 EmmcCmdBlk.CommandArgument = Rca << 16;
1917
1918 Status = EmmcPeimExecCmd (Slot, &Packet);
1919 if (!EFI_ERROR (Status)) {
1920 //
1921 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
1922 //
1923 CopyMem (((UINT8*)Csd) + 1, &EmmcStatusBlk.Resp0, sizeof (EMMC_CSD) - 1);
1924 }
1925
1926 return Status;
1927 }
1928
1929 /**
1930 Send command SELECT_DESELECT_CARD to the EMMC device to select/deselect it.
1931
1932 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1933
1934 @param[in] Slot The slot number of the Emmc card to send the command to.
1935 @param[in] Rca The relative device address of selected device.
1936
1937 @retval EFI_SUCCESS The operation is done correctly.
1938 @retval Others The operation fails.
1939
1940 **/
1941 EFI_STATUS
1942 EmmcPeimSelect (
1943 IN EMMC_PEIM_HC_SLOT *Slot,
1944 IN UINT32 Rca
1945 )
1946 {
1947 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1948 EMMC_STATUS_BLOCK EmmcStatusBlk;
1949 EMMC_COMMAND_PACKET Packet;
1950 EFI_STATUS Status;
1951
1952 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1953 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1954 ZeroMem (&Packet, sizeof (Packet));
1955
1956 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1957 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1958 Packet.Timeout = EMMC_TIMEOUT;
1959
1960 EmmcCmdBlk.CommandIndex = EMMC_SELECT_DESELECT_CARD;
1961 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
1962 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
1963 EmmcCmdBlk.CommandArgument = Rca << 16;
1964
1965 Status = EmmcPeimExecCmd (Slot, &Packet);
1966
1967 return Status;
1968 }
1969
1970 /**
1971 Send command SEND_EXT_CSD to the EMMC device to get the data of the EXT_CSD register.
1972
1973 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1974
1975 @param[in] Slot The slot number of the Emmc card to send the command to.
1976 @param[out] ExtCsd The buffer to store the content of the EXT_CSD register.
1977
1978 @retval EFI_SUCCESS The operation is done correctly.
1979 @retval Others The operation fails.
1980
1981 **/
1982 EFI_STATUS
1983 EmmcPeimGetExtCsd (
1984 IN EMMC_PEIM_HC_SLOT *Slot,
1985 OUT EMMC_EXT_CSD *ExtCsd
1986 )
1987 {
1988 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1989 EMMC_STATUS_BLOCK EmmcStatusBlk;
1990 EMMC_COMMAND_PACKET Packet;
1991 EFI_STATUS Status;
1992
1993 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1994 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1995 ZeroMem (&Packet, sizeof (Packet));
1996
1997 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1998 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1999 Packet.Timeout = EMMC_TIMEOUT;
2000
2001 EmmcCmdBlk.CommandIndex = EMMC_SEND_EXT_CSD;
2002 EmmcCmdBlk.CommandType = EmmcCommandTypeAdtc;
2003 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2004 EmmcCmdBlk.CommandArgument = 0x00000000;
2005
2006 Packet.InDataBuffer = ExtCsd;
2007 Packet.InTransferLength = sizeof (EMMC_EXT_CSD);
2008
2009 Status = EmmcPeimExecCmd (Slot, &Packet);
2010 return Status;
2011 }
2012
2013 /**
2014 Send command SWITCH to the EMMC device to switch the mode of operation of the
2015 selected Device or modifies the EXT_CSD registers.
2016
2017 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2018
2019 @param[in] Slot The slot number of the Emmc card to send the command to.
2020 @param[in] Access The access mode of SWTICH command.
2021 @param[in] Index The offset of the field to be access.
2022 @param[in] Value The value to be set to the specified field of EXT_CSD register.
2023 @param[in] CmdSet The value of CmdSet field of EXT_CSD register.
2024
2025 @retval EFI_SUCCESS The operation is done correctly.
2026 @retval Others The operation fails.
2027
2028 **/
2029 EFI_STATUS
2030 EmmcPeimSwitch (
2031 IN EMMC_PEIM_HC_SLOT *Slot,
2032 IN UINT8 Access,
2033 IN UINT8 Index,
2034 IN UINT8 Value,
2035 IN UINT8 CmdSet
2036 )
2037 {
2038 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2039 EMMC_STATUS_BLOCK EmmcStatusBlk;
2040 EMMC_COMMAND_PACKET Packet;
2041 EFI_STATUS Status;
2042
2043 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2044 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2045 ZeroMem (&Packet, sizeof (Packet));
2046
2047 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2048 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2049 Packet.Timeout = EMMC_TIMEOUT;
2050
2051 EmmcCmdBlk.CommandIndex = EMMC_SWITCH;
2052 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
2053 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1b;
2054 EmmcCmdBlk.CommandArgument = (Access << 24) | (Index << 16) | (Value << 8) | CmdSet;
2055
2056 Status = EmmcPeimExecCmd (Slot, &Packet);
2057
2058 return Status;
2059 }
2060
2061 /**
2062 Send command SEND_STATUS to the addressed EMMC device to get its status register.
2063
2064 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2065
2066 @param[in] Slot The slot number of the Emmc card to send the command to.
2067 @param[in] Rca The relative device address of addressed device.
2068 @param[out] DevStatus The returned device status.
2069
2070 @retval EFI_SUCCESS The operation is done correctly.
2071 @retval Others The operation fails.
2072
2073 **/
2074 EFI_STATUS
2075 EmmcPeimSendStatus (
2076 IN EMMC_PEIM_HC_SLOT *Slot,
2077 IN UINT32 Rca,
2078 OUT UINT32 *DevStatus
2079 )
2080 {
2081 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2082 EMMC_STATUS_BLOCK EmmcStatusBlk;
2083 EMMC_COMMAND_PACKET Packet;
2084 EFI_STATUS Status;
2085
2086 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2087 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2088 ZeroMem (&Packet, sizeof (Packet));
2089
2090 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2091 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2092 Packet.Timeout = EMMC_TIMEOUT;
2093
2094 EmmcCmdBlk.CommandIndex = EMMC_SEND_STATUS;
2095 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
2096 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2097 EmmcCmdBlk.CommandArgument = Rca << 16;
2098
2099 Status = EmmcPeimExecCmd (Slot, &Packet);
2100 if (!EFI_ERROR (Status)) {
2101 *DevStatus = EmmcStatusBlk.Resp0;
2102 }
2103
2104 return Status;
2105 }
2106
2107 /**
2108 Send command SET_BLOCK_COUNT to the addressed EMMC device to set the number of
2109 blocks for the following block read/write cmd.
2110
2111 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2112
2113 @param[in] Slot The slot number of the Emmc card to send the command to.
2114 @param[in] BlockCount The number of the logical block to access.
2115
2116 @retval EFI_SUCCESS The operation is done correctly.
2117 @retval Others The operation fails.
2118
2119 **/
2120 EFI_STATUS
2121 EmmcPeimSetBlkCount (
2122 IN EMMC_PEIM_HC_SLOT *Slot,
2123 IN UINT16 BlockCount
2124 )
2125 {
2126 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2127 EMMC_STATUS_BLOCK EmmcStatusBlk;
2128 EMMC_COMMAND_PACKET Packet;
2129 EFI_STATUS Status;
2130
2131 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2132 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2133 ZeroMem (&Packet, sizeof (Packet));
2134
2135 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2136 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2137 Packet.Timeout = EMMC_TIMEOUT;
2138
2139 EmmcCmdBlk.CommandIndex = EMMC_SET_BLOCK_COUNT;
2140 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
2141 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2142 EmmcCmdBlk.CommandArgument = BlockCount;
2143
2144 Status = EmmcPeimExecCmd (Slot, &Packet);
2145
2146 return Status;
2147 }
2148
2149 /**
2150 Send command READ_MULTIPLE_BLOCK/WRITE_MULTIPLE_BLOCK to the addressed EMMC device
2151 to read/write the specified number of blocks.
2152
2153 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2154
2155 @param[in] Slot The slot number of the Emmc card to send the command to.
2156 @param[in] Lba The logical block address of starting access.
2157 @param[in] BlockSize The block size of specified EMMC device partition.
2158 @param[in] Buffer The pointer to the transfer buffer.
2159 @param[in] BufferSize The size of transfer buffer.
2160 @param[in] IsRead Boolean to show the operation direction.
2161
2162 @retval EFI_SUCCESS The operation is done correctly.
2163 @retval Others The operation fails.
2164
2165 **/
2166 EFI_STATUS
2167 EmmcPeimRwMultiBlocks (
2168 IN EMMC_PEIM_HC_SLOT *Slot,
2169 IN EFI_LBA Lba,
2170 IN UINT32 BlockSize,
2171 IN VOID *Buffer,
2172 IN UINTN BufferSize,
2173 IN BOOLEAN IsRead
2174 )
2175 {
2176 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2177 EMMC_STATUS_BLOCK EmmcStatusBlk;
2178 EMMC_COMMAND_PACKET Packet;
2179 EFI_STATUS Status;
2180
2181 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2182 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2183 ZeroMem (&Packet, sizeof (Packet));
2184
2185 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2186 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2187 //
2188 // Calculate timeout value through the below formula.
2189 // Timeout = (transfer size) / (2MB/s).
2190 // Taking 2MB/s as divisor is because it's nearest to the eMMC lowest
2191 // transfer speed (2.4MB/s).
2192 // Refer to eMMC 5.0 spec section 6.9.1 for details.
2193 //
2194 Packet.Timeout = (BufferSize / (2 * 1024 * 1024) + 1) * 1000 * 1000;;
2195
2196 if (IsRead) {
2197 Packet.InDataBuffer = Buffer;
2198 Packet.InTransferLength = (UINT32)BufferSize;
2199
2200 EmmcCmdBlk.CommandIndex = EMMC_READ_MULTIPLE_BLOCK;
2201 EmmcCmdBlk.CommandType = EmmcCommandTypeAdtc;
2202 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2203 } else {
2204 Packet.OutDataBuffer = Buffer;
2205 Packet.OutTransferLength = (UINT32)BufferSize;
2206
2207 EmmcCmdBlk.CommandIndex = EMMC_WRITE_MULTIPLE_BLOCK;
2208 EmmcCmdBlk.CommandType = EmmcCommandTypeAdtc;
2209 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2210 }
2211
2212 if (Slot->SectorAddressing) {
2213 EmmcCmdBlk.CommandArgument = (UINT32)Lba;
2214 } else {
2215 EmmcCmdBlk.CommandArgument = (UINT32)MultU64x32 (Lba, BlockSize);
2216 }
2217
2218 Status = EmmcPeimExecCmd (Slot, &Packet);
2219
2220 return Status;
2221 }
2222
2223 /**
2224 Send command SEND_TUNING_BLOCK to the EMMC device for HS200 optimal sampling point
2225 detection.
2226
2227 It may be sent up to 40 times until the host finishes the tuning procedure.
2228
2229 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 for details.
2230
2231 @param[in] Slot The slot number of the Emmc card to send the command to.
2232 @param[in] BusWidth The bus width to work.
2233
2234 @retval EFI_SUCCESS The operation is done correctly.
2235 @retval Others The operation fails.
2236
2237 **/
2238 EFI_STATUS
2239 EmmcPeimSendTuningBlk (
2240 IN EMMC_PEIM_HC_SLOT *Slot,
2241 IN UINT8 BusWidth
2242 )
2243 {
2244 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2245 EMMC_STATUS_BLOCK EmmcStatusBlk;
2246 EMMC_COMMAND_PACKET Packet;
2247 EFI_STATUS Status;
2248 UINT8 TuningBlock[128];
2249
2250 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2251 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2252 ZeroMem (&Packet, sizeof (Packet));
2253
2254 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2255 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2256 Packet.Timeout = EMMC_TIMEOUT;
2257
2258 EmmcCmdBlk.CommandIndex = EMMC_SEND_TUNING_BLOCK;
2259 EmmcCmdBlk.CommandType = EmmcCommandTypeAdtc;
2260 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2261 EmmcCmdBlk.CommandArgument = 0;
2262
2263 Packet.InDataBuffer = TuningBlock;
2264 if (BusWidth == 8) {
2265 Packet.InTransferLength = sizeof (TuningBlock);
2266 } else {
2267 Packet.InTransferLength = 64;
2268 }
2269
2270 Status = EmmcPeimExecCmd (Slot, &Packet);
2271
2272 return Status;
2273 }
2274
2275 /**
2276 Tunning the clock to get HS200 optimal sampling point.
2277
2278 Command SEND_TUNING_BLOCK may be sent up to 40 times until the host finishes the
2279 tuning procedure.
2280
2281 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2282 Simplified Spec 3.0 section Figure 2-29 for details.
2283
2284 @param[in] Slot The slot number of the Emmc card to send the command to.
2285 @param[in] BusWidth The bus width to work.
2286
2287 @retval EFI_SUCCESS The operation is done correctly.
2288 @retval Others The operation fails.
2289
2290 **/
2291 EFI_STATUS
2292 EmmcPeimTuningClkForHs200 (
2293 IN EMMC_PEIM_HC_SLOT *Slot,
2294 IN UINT8 BusWidth
2295 )
2296 {
2297 EFI_STATUS Status;
2298 UINT8 HostCtrl2;
2299 UINT8 Retry;
2300
2301 //
2302 // Notify the host that the sampling clock tuning procedure starts.
2303 //
2304 HostCtrl2 = BIT6;
2305 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2306 if (EFI_ERROR (Status)) {
2307 return Status;
2308 }
2309 //
2310 // Ask the device to send a sequence of tuning blocks till the tuning procedure is done.
2311 //
2312 Retry = 0;
2313 do {
2314 Status = EmmcPeimSendTuningBlk (Slot, BusWidth);
2315 if (EFI_ERROR (Status)) {
2316 return Status;
2317 }
2318
2319 Status = EmmcPeimHcRwMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, TRUE, sizeof (HostCtrl2), &HostCtrl2);
2320 if (EFI_ERROR (Status)) {
2321 return Status;
2322 }
2323
2324 if ((HostCtrl2 & (BIT6 | BIT7)) == 0) {
2325 break;
2326 }
2327
2328 if ((HostCtrl2 & (BIT6 | BIT7)) == BIT7) {
2329 return EFI_SUCCESS;
2330 }
2331 } while (++Retry < 40);
2332
2333 DEBUG ((EFI_D_ERROR, "EmmcPeimTuningClkForHs200: Send tuning block fails at %d times with HostCtrl2 %02x\n", Retry, HostCtrl2));
2334 //
2335 // Abort the tuning procedure and reset the tuning circuit.
2336 //
2337 HostCtrl2 = (UINT8)~(BIT6 | BIT7);
2338 Status = EmmcPeimHcAndMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2339 if (EFI_ERROR (Status)) {
2340 return Status;
2341 }
2342 return EFI_DEVICE_ERROR;
2343 }
2344
2345 /**
2346 Switch the bus width to specified width.
2347
2348 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.9 and SD Host Controller
2349 Simplified Spec 3.0 section Figure 3-7 for details.
2350
2351 @param[in] Slot The slot number of the Emmc card to send the command to.
2352 @param[in] Rca The relative device address to be assigned.
2353 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
2354 use single data rate data simpling method.
2355 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2356
2357 @retval EFI_SUCCESS The operation is done correctly.
2358 @retval Others The operation fails.
2359
2360 **/
2361 EFI_STATUS
2362 EmmcPeimSwitchBusWidth (
2363 IN EMMC_PEIM_HC_SLOT *Slot,
2364 IN UINT32 Rca,
2365 IN BOOLEAN IsDdr,
2366 IN UINT8 BusWidth
2367 )
2368 {
2369 EFI_STATUS Status;
2370 UINT8 Access;
2371 UINT8 Index;
2372 UINT8 Value;
2373 UINT8 CmdSet;
2374 UINT32 DevStatus;
2375
2376 //
2377 // Write Byte, the Value field is written into the byte pointed by Index.
2378 //
2379 Access = 0x03;
2380 Index = OFFSET_OF (EMMC_EXT_CSD, BusWidth);
2381 if (BusWidth == 4) {
2382 Value = 1;
2383 } else if (BusWidth == 8) {
2384 Value = 2;
2385 } else {
2386 return EFI_INVALID_PARAMETER;
2387 }
2388
2389 if (IsDdr) {
2390 Value += 4;
2391 }
2392
2393 CmdSet = 0;
2394 Status = EmmcPeimSwitch (Slot, Access, Index, Value, CmdSet);
2395 if (EFI_ERROR (Status)) {
2396 return Status;
2397 }
2398
2399 Status = EmmcPeimSendStatus (Slot, Rca, &DevStatus);
2400 if (EFI_ERROR (Status)) {
2401 return Status;
2402 }
2403 //
2404 // Check the switch operation is really successful or not.
2405 //
2406 if ((DevStatus & BIT7) != 0) {
2407 return EFI_DEVICE_ERROR;
2408 }
2409
2410 Status = EmmcPeimHcSetBusWidth (Slot->EmmcHcBase, BusWidth);
2411
2412 return Status;
2413 }
2414
2415 /**
2416 Switch the clock frequency to the specified value.
2417
2418 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6 and SD Host Controller
2419 Simplified Spec 3.0 section Figure 3-3 for details.
2420
2421 @param[in] Slot The slot number of the Emmc card to send the command to.
2422 @param[in] Rca The relative device address to be assigned.
2423 @param[in] HsTiming The value to be written to HS_TIMING field of EXT_CSD register.
2424 @param[in] ClockFreq The max clock frequency to be set, the unit is MHz.
2425
2426 @retval EFI_SUCCESS The operation is done correctly.
2427 @retval Others The operation fails.
2428
2429 **/
2430 EFI_STATUS
2431 EmmcPeimSwitchClockFreq (
2432 IN EMMC_PEIM_HC_SLOT *Slot,
2433 IN UINT32 Rca,
2434 IN UINT8 HsTiming,
2435 IN UINT32 ClockFreq
2436 )
2437 {
2438 EFI_STATUS Status;
2439 UINT8 Access;
2440 UINT8 Index;
2441 UINT8 Value;
2442 UINT8 CmdSet;
2443 UINT32 DevStatus;
2444
2445 //
2446 // Write Byte, the Value field is written into the byte pointed by Index.
2447 //
2448 Access = 0x03;
2449 Index = OFFSET_OF (EMMC_EXT_CSD, HsTiming);
2450 Value = HsTiming;
2451 CmdSet = 0;
2452
2453 Status = EmmcPeimSwitch (Slot, Access, Index, Value, CmdSet);
2454 if (EFI_ERROR (Status)) {
2455 return Status;
2456 }
2457
2458 Status = EmmcPeimSendStatus (Slot, Rca, &DevStatus);
2459 if (EFI_ERROR (Status)) {
2460 return Status;
2461 }
2462 //
2463 // Check the switch operation is really successful or not.
2464 //
2465 if ((DevStatus & BIT7) != 0) {
2466 return EFI_DEVICE_ERROR;
2467 }
2468 //
2469 // Convert the clock freq unit from MHz to KHz.
2470 //
2471 Status = EmmcPeimHcClockSupply (Slot->EmmcHcBase, ClockFreq * 1000);
2472
2473 return Status;
2474 }
2475
2476 /**
2477 Switch to the High Speed timing according to request.
2478
2479 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2480 Simplified Spec 3.0 section Figure 2-29 for details.
2481
2482 @param[in] Slot The slot number of the Emmc card to send the command to.
2483 @param[in] Rca The relative device address to be assigned.
2484 @param[in] ClockFreq The max clock frequency to be set.
2485 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
2486 use single data rate data simpling method.
2487 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2488
2489 @retval EFI_SUCCESS The operation is done correctly.
2490 @retval Others The operation fails.
2491
2492 **/
2493 EFI_STATUS
2494 EmmcPeimSwitchToHighSpeed (
2495 IN EMMC_PEIM_HC_SLOT *Slot,
2496 IN UINT32 Rca,
2497 IN UINT32 ClockFreq,
2498 IN BOOLEAN IsDdr,
2499 IN UINT8 BusWidth
2500 )
2501 {
2502 EFI_STATUS Status;
2503 UINT8 HsTiming;
2504 UINT8 HostCtrl1;
2505 UINT8 HostCtrl2;
2506
2507 Status = EmmcPeimSwitchBusWidth (Slot, Rca, IsDdr, BusWidth);
2508 if (EFI_ERROR (Status)) {
2509 return Status;
2510 }
2511 //
2512 // Set to Hight Speed timing
2513 //
2514 HostCtrl1 = BIT2;
2515 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
2516 if (EFI_ERROR (Status)) {
2517 return Status;
2518 }
2519
2520 HostCtrl2 = (UINT8)~0x7;
2521 Status = EmmcPeimHcAndMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2522 if (EFI_ERROR (Status)) {
2523 return Status;
2524 }
2525 if (IsDdr) {
2526 HostCtrl2 = BIT2;
2527 } else if (ClockFreq == 52) {
2528 HostCtrl2 = BIT0;
2529 } else {
2530 HostCtrl2 = 0;
2531 }
2532 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2533 if (EFI_ERROR (Status)) {
2534 return Status;
2535 }
2536
2537 HsTiming = 1;
2538 Status = EmmcPeimSwitchClockFreq (Slot, Rca, HsTiming, ClockFreq);
2539
2540 return Status;
2541 }
2542
2543 /**
2544 Switch to the HS200 timing according to request.
2545
2546 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2547 Simplified Spec 3.0 section Figure 2-29 for details.
2548
2549 @param[in] Slot The slot number of the Emmc card to send the command to.
2550 @param[in] Rca The relative device address to be assigned.
2551 @param[in] ClockFreq The max clock frequency to be set.
2552 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2553
2554 @retval EFI_SUCCESS The operation is done correctly.
2555 @retval Others The operation fails.
2556
2557 **/
2558 EFI_STATUS
2559 EmmcPeimSwitchToHS200 (
2560 IN EMMC_PEIM_HC_SLOT *Slot,
2561 IN UINT32 Rca,
2562 IN UINT32 ClockFreq,
2563 IN UINT8 BusWidth
2564 )
2565 {
2566 EFI_STATUS Status;
2567 UINT8 HsTiming;
2568 UINT8 HostCtrl2;
2569 UINT16 ClockCtrl;
2570
2571 if ((BusWidth != 4) && (BusWidth != 8)) {
2572 return EFI_INVALID_PARAMETER;
2573 }
2574
2575 Status = EmmcPeimSwitchBusWidth (Slot, Rca, FALSE, BusWidth);
2576 if (EFI_ERROR (Status)) {
2577 return Status;
2578 }
2579 //
2580 // Set to HS200/SDR104 timing
2581 //
2582 //
2583 // Stop bus clock at first
2584 //
2585 Status = EmmcPeimHcStopClock (Slot->EmmcHcBase);
2586 if (EFI_ERROR (Status)) {
2587 return Status;
2588 }
2589
2590 HostCtrl2 = (UINT8)~0x7;
2591 Status = EmmcPeimHcAndMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2592 if (EFI_ERROR (Status)) {
2593 return Status;
2594 }
2595 HostCtrl2 = BIT0 | BIT1;
2596 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2597 if (EFI_ERROR (Status)) {
2598 return Status;
2599 }
2600
2601 //
2602 // Wait Internal Clock Stable in the Clock Control register to be 1 before set SD Clock Enable bit
2603 //
2604 Status = EmmcPeimHcWaitMmioSet (
2605 Slot->EmmcHcBase + EMMC_HC_CLOCK_CTRL,
2606 sizeof (ClockCtrl),
2607 BIT1,
2608 BIT1,
2609 EMMC_TIMEOUT
2610 );
2611 if (EFI_ERROR (Status)) {
2612 return Status;
2613 }
2614 //
2615 // Set SD Clock Enable in the Clock Control register to 1
2616 //
2617 ClockCtrl = BIT2;
2618 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);
2619
2620 HsTiming = 2;
2621 Status = EmmcPeimSwitchClockFreq (Slot, Rca, HsTiming, ClockFreq);
2622 if (EFI_ERROR (Status)) {
2623 return Status;
2624 }
2625
2626 Status = EmmcPeimTuningClkForHs200 (Slot, BusWidth);
2627
2628 return Status;
2629 }
2630
2631 /**
2632 Switch to the HS400 timing according to request.
2633
2634 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2635 Simplified Spec 3.0 section Figure 2-29 for details.
2636
2637 @param[in] Slot The slot number of the Emmc card to send the command to.
2638 @param[in] Rca The relative device address to be assigned.
2639 @param[in] ClockFreq The max clock frequency to be set.
2640
2641 @retval EFI_SUCCESS The operation is done correctly.
2642 @retval Others The operation fails.
2643
2644 **/
2645 EFI_STATUS
2646 EmmcPeimSwitchToHS400 (
2647 IN EMMC_PEIM_HC_SLOT *Slot,
2648 IN UINT32 Rca,
2649 IN UINT32 ClockFreq
2650 )
2651 {
2652 EFI_STATUS Status;
2653 UINT8 HsTiming;
2654 UINT8 HostCtrl2;
2655
2656 Status = EmmcPeimSwitchToHS200 (Slot, Rca, ClockFreq, 8);
2657 if (EFI_ERROR (Status)) {
2658 return Status;
2659 }
2660 //
2661 // Set to Hight Speed timing and set the clock frequency to a value less than 52MHz.
2662 //
2663 HsTiming = 1;
2664 Status = EmmcPeimSwitchClockFreq (Slot, Rca, HsTiming, 52);
2665 if (EFI_ERROR (Status)) {
2666 return Status;
2667 }
2668 //
2669 // HS400 mode must use 8 data lines.
2670 //
2671 Status = EmmcPeimSwitchBusWidth (Slot, Rca, TRUE, 8);
2672 if (EFI_ERROR (Status)) {
2673 return Status;
2674 }
2675 //
2676 // Set to HS400 timing
2677 //
2678 HostCtrl2 = (UINT8)~0x7;
2679 Status = EmmcPeimHcAndMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2680 if (EFI_ERROR (Status)) {
2681 return Status;
2682 }
2683 HostCtrl2 = BIT0 | BIT2;
2684 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2685 if (EFI_ERROR (Status)) {
2686 return Status;
2687 }
2688
2689 HsTiming = 3;
2690 Status = EmmcPeimSwitchClockFreq (Slot, Rca, HsTiming, ClockFreq);
2691
2692 return Status;
2693 }
2694
2695 /**
2696 Switch the high speed timing according to request.
2697
2698 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2699 Simplified Spec 3.0 section Figure 2-29 for details.
2700
2701 @param[in] Slot The slot number of the Emmc card to send the command to.
2702 @param[in] Rca The relative device address to be assigned.
2703
2704 @retval EFI_SUCCESS The operation is done correctly.
2705 @retval Others The operation fails.
2706
2707 **/
2708 EFI_STATUS
2709 EmmcPeimSetBusMode (
2710 IN EMMC_PEIM_HC_SLOT *Slot,
2711 IN UINT32 Rca
2712 )
2713 {
2714 EFI_STATUS Status;
2715 EMMC_HC_SLOT_CAP Capability;
2716 UINT8 HsTiming;
2717 BOOLEAN IsDdr;
2718 UINT32 ClockFreq;
2719 UINT8 BusWidth;
2720
2721 Status = EmmcPeimGetCsd (Slot, Rca, &Slot->Csd);
2722 if (EFI_ERROR (Status)) {
2723 DEBUG ((EFI_D_ERROR, "EmmcPeimSetBusMode: EmmcPeimGetCsd fails with %r\n", Status));
2724 return Status;
2725 }
2726
2727 if ((Slot->Csd.CSizeLow | Slot->Csd.CSizeHigh << 2) == 0xFFF) {
2728 Slot->SectorAddressing = TRUE;
2729 } else {
2730 Slot->SectorAddressing = FALSE;
2731 }
2732
2733 Status = EmmcPeimSelect (Slot, Rca);
2734 if (EFI_ERROR (Status)) {
2735 DEBUG ((EFI_D_ERROR, "EmmcPeimSetBusMode: EmmcPeimSelect fails with %r\n", Status));
2736 return Status;
2737 }
2738
2739 Status = EmmcPeimHcGetCapability (Slot->EmmcHcBase, &Capability);
2740 if (EFI_ERROR (Status)) {
2741 DEBUG ((EFI_D_ERROR, "EmmcPeimSetBusMode: EmmcPeimHcGetCapability fails with %r\n", Status));
2742 return Status;
2743 }
2744
2745 ASSERT (Capability.BaseClkFreq != 0);
2746 //
2747 // Check if the Host Controller support 8bits bus width.
2748 //
2749 if (Capability.BusWidth8 != 0) {
2750 BusWidth = 8;
2751 } else {
2752 BusWidth = 4;
2753 }
2754 //
2755 // Get Deivce_Type from EXT_CSD register.
2756 //
2757 Status = EmmcPeimGetExtCsd (Slot, &Slot->ExtCsd);
2758 if (EFI_ERROR (Status)) {
2759 DEBUG ((EFI_D_ERROR, "EmmcPeimSetBusMode: EmmcPeimGetExtCsd fails with %r\n", Status));
2760 return Status;
2761 }
2762 //
2763 // Calculate supported bus speed/bus width/clock frequency.
2764 //
2765 HsTiming = 0;
2766 IsDdr = FALSE;
2767 ClockFreq = 0;
2768 if (((Slot->ExtCsd.DeviceType & (BIT4 | BIT5)) != 0) && (Capability.Sdr104 != 0)) {
2769 HsTiming = 2;
2770 IsDdr = FALSE;
2771 ClockFreq = 200;
2772 } else if (((Slot->ExtCsd.DeviceType & (BIT2 | BIT3)) != 0) && (Capability.Ddr50 != 0)) {
2773 HsTiming = 1;
2774 IsDdr = TRUE;
2775 ClockFreq = 52;
2776 } else if (((Slot->ExtCsd.DeviceType & BIT1) != 0) && (Capability.HighSpeed != 0)) {
2777 HsTiming = 1;
2778 IsDdr = FALSE;
2779 ClockFreq = 52;
2780 } else if (((Slot->ExtCsd.DeviceType & BIT0) != 0) && (Capability.HighSpeed != 0)) {
2781 HsTiming = 1;
2782 IsDdr = FALSE;
2783 ClockFreq = 26;
2784 }
2785 //
2786 // Check if both of the device and the host controller support HS400 DDR mode.
2787 //
2788 if (((Slot->ExtCsd.DeviceType & (BIT6 | BIT7)) != 0) && (Capability.Hs400 != 0)) {
2789 //
2790 // The host controller supports 8bits bus.
2791 //
2792 ASSERT (BusWidth == 8);
2793 HsTiming = 3;
2794 IsDdr = TRUE;
2795 ClockFreq = 200;
2796 }
2797
2798 if ((ClockFreq == 0) || (HsTiming == 0)) {
2799 //
2800 // Continue using default setting.
2801 //
2802 return EFI_SUCCESS;
2803 }
2804
2805 DEBUG ((EFI_D_INFO, "HsTiming %d ClockFreq %d BusWidth %d Ddr %a\n", HsTiming, ClockFreq, BusWidth, IsDdr ? "TRUE":"FALSE"));
2806
2807 if (HsTiming == 3) {
2808 //
2809 // Execute HS400 timing switch procedure
2810 //
2811 Status = EmmcPeimSwitchToHS400 (Slot, Rca, ClockFreq);
2812 } else if (HsTiming == 2) {
2813 //
2814 // Execute HS200 timing switch procedure
2815 //
2816 Status = EmmcPeimSwitchToHS200 (Slot, Rca, ClockFreq, BusWidth);
2817 } else {
2818 //
2819 // Execute High Speed timing switch procedure
2820 //
2821 Status = EmmcPeimSwitchToHighSpeed (Slot, Rca, ClockFreq, IsDdr, BusWidth);
2822 }
2823
2824 return Status;
2825 }
2826
2827 /**
2828 Execute EMMC device identification procedure.
2829
2830 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
2831
2832 @param[in] Slot The slot number of the Emmc card to send the command to.
2833
2834 @retval EFI_SUCCESS There is a EMMC card.
2835 @retval Others There is not a EMMC card.
2836
2837 **/
2838 EFI_STATUS
2839 EmmcPeimIdentification (
2840 IN EMMC_PEIM_HC_SLOT *Slot
2841 )
2842 {
2843 EFI_STATUS Status;
2844 UINT32 Ocr;
2845 UINT32 Rca;
2846 UINTN Retry;
2847
2848 Status = EmmcPeimReset (Slot);
2849 if (EFI_ERROR (Status)) {
2850 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimReset fails with %r\n", Status));
2851 return Status;
2852 }
2853
2854 Ocr = 0;
2855 Retry = 0;
2856 do {
2857 Status = EmmcPeimGetOcr (Slot, &Ocr);
2858 if (EFI_ERROR (Status)) {
2859 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimGetOcr fails with %r\n", Status));
2860 return Status;
2861 }
2862
2863 if (Retry++ == 100) {
2864 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimGetOcr fails too many times\n"));
2865 return EFI_DEVICE_ERROR;
2866 }
2867 MicroSecondDelay (10 * 1000);
2868 } while ((Ocr & BIT31) == 0);
2869
2870 Status = EmmcPeimGetAllCid (Slot);
2871 if (EFI_ERROR (Status)) {
2872 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimGetAllCid fails with %r\n", Status));
2873 return Status;
2874 }
2875 //
2876 // Don't support multiple devices on the slot, that is
2877 // shared bus slot feature.
2878 //
2879 Rca = 1;
2880 Status = EmmcPeimSetRca (Slot, Rca);
2881 if (EFI_ERROR (Status)) {
2882 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimSetRca fails with %r\n", Status));
2883 return Status;
2884 }
2885 //
2886 // Enter Data Tranfer Mode.
2887 //
2888 DEBUG ((EFI_D_INFO, "Found a EMMC device at slot [%d], RCA [%d]\n", Slot, Rca));
2889
2890 Status = EmmcPeimSetBusMode (Slot, Rca);
2891
2892 return Status;
2893 }
2894