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