]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Sd/EmmcBlockIoPei/EmmcHci.c
MdeModulePkg/SdMmcPciHcDxe: Use BaseClk if the target clock is larger
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / EmmcBlockIoPei / EmmcHci.c
1 /** @file
2
3 Copyright (c) 2015 - 2016, 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 = (EFI_PHYSICAL_ADDRESS)(UINTN)Trb->Data;
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
1002 //
1003 // Calculate a divisor for SD clock frequency
1004 //
1005 Status = EmmcPeimHcGetCapability (Slot->EmmcHcBase, &Capability);
1006 if (EFI_ERROR (Status)) {
1007 return NULL;
1008 }
1009
1010 Trb = EmmcPeimAllocateMem (Slot->Private->Pool, sizeof (EMMC_TRB));
1011 if (Trb == NULL) {
1012 return NULL;
1013 }
1014
1015 Trb->Slot = Slot;
1016 Trb->BlockSize = 0x200;
1017 Trb->Packet = Packet;
1018 Trb->Timeout = Packet->Timeout;
1019
1020 if ((Packet->InTransferLength != 0) && (Packet->InDataBuffer != NULL)) {
1021 Trb->Data = Packet->InDataBuffer;
1022 Trb->DataLen = Packet->InTransferLength;
1023 Trb->Read = TRUE;
1024 } else if ((Packet->OutTransferLength != 0) && (Packet->OutDataBuffer != NULL)) {
1025 Trb->Data = Packet->OutDataBuffer;
1026 Trb->DataLen = Packet->OutTransferLength;
1027 Trb->Read = FALSE;
1028 } else if ((Packet->InTransferLength == 0) && (Packet->OutTransferLength == 0)) {
1029 Trb->Data = NULL;
1030 Trb->DataLen = 0;
1031 } else {
1032 goto Error;
1033 }
1034
1035 if ((Trb->DataLen % Trb->BlockSize) != 0) {
1036 if (Trb->DataLen < Trb->BlockSize) {
1037 Trb->BlockSize = (UINT16)Trb->DataLen;
1038 }
1039 }
1040
1041 if (Trb->DataLen == 0) {
1042 Trb->Mode = EmmcNoData;
1043 } else if (Capability.Adma2 != 0) {
1044 Trb->Mode = EmmcAdmaMode;
1045 Status = BuildAdmaDescTable (Trb);
1046 if (EFI_ERROR (Status)) {
1047 goto Error;
1048 }
1049 } else if (Capability.Sdma != 0) {
1050 Trb->Mode = EmmcSdmaMode;
1051 } else {
1052 Trb->Mode = EmmcPioMode;
1053 }
1054
1055 return Trb;
1056
1057 Error:
1058 EmmcPeimFreeTrb (Trb);
1059 return NULL;
1060 }
1061
1062 /**
1063 Free the resource used by the TRB.
1064
1065 @param[in] Trb The pointer to the EMMC_TRB instance.
1066
1067 **/
1068 VOID
1069 EmmcPeimFreeTrb (
1070 IN EMMC_TRB *Trb
1071 )
1072 {
1073 if ((Trb != NULL) && (Trb->AdmaDesc != NULL)) {
1074 EmmcPeimFreeMem (Trb->Slot->Private->Pool, Trb->AdmaDesc, Trb->AdmaDescSize);
1075 }
1076
1077 if (Trb != NULL) {
1078 EmmcPeimFreeMem (Trb->Slot->Private->Pool, Trb, sizeof (EMMC_TRB));
1079 }
1080 return;
1081 }
1082
1083 /**
1084 Check if the env is ready for execute specified TRB.
1085
1086 @param[in] Bar The mmio base address of the slot to be accessed.
1087 @param[in] Trb The pointer to the EMMC_TRB instance.
1088
1089 @retval EFI_SUCCESS The env is ready for TRB execution.
1090 @retval EFI_NOT_READY The env is not ready for TRB execution.
1091 @retval Others Some erros happen.
1092
1093 **/
1094 EFI_STATUS
1095 EmmcPeimCheckTrbEnv (
1096 IN UINTN Bar,
1097 IN EMMC_TRB *Trb
1098 )
1099 {
1100 EFI_STATUS Status;
1101 EMMC_COMMAND_PACKET *Packet;
1102 UINT32 PresentState;
1103
1104 Packet = Trb->Packet;
1105
1106 if ((Packet->EmmcCmdBlk->CommandType == EmmcCommandTypeAdtc) ||
1107 (Packet->EmmcCmdBlk->ResponseType == EmmcResponceTypeR1b) ||
1108 (Packet->EmmcCmdBlk->ResponseType == EmmcResponceTypeR5b)) {
1109 //
1110 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
1111 // the Present State register to be 0
1112 //
1113 PresentState = BIT0 | BIT1;
1114 if (Packet->EmmcCmdBlk->CommandIndex == EMMC_SEND_TUNING_BLOCK) {
1115 PresentState = BIT0;
1116 }
1117 } else {
1118 //
1119 // Wait Command Inhibit (CMD) in the Present State register
1120 // to be 0
1121 //
1122 PresentState = BIT0;
1123 }
1124
1125 Status = EmmcPeimHcCheckMmioSet (
1126 Bar + EMMC_HC_PRESENT_STATE,
1127 sizeof (PresentState),
1128 PresentState,
1129 0
1130 );
1131
1132 return Status;
1133 }
1134
1135 /**
1136 Wait for the env to be ready for execute specified TRB.
1137
1138 @param[in] Bar The mmio base address of the slot to be accessed.
1139 @param[in] Trb The pointer to the EMMC_TRB instance.
1140
1141 @retval EFI_SUCCESS The env is ready for TRB execution.
1142 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
1143 @retval Others Some erros happen.
1144
1145 **/
1146 EFI_STATUS
1147 EmmcPeimWaitTrbEnv (
1148 IN UINTN Bar,
1149 IN EMMC_TRB *Trb
1150 )
1151 {
1152 EFI_STATUS Status;
1153 EMMC_COMMAND_PACKET *Packet;
1154 UINT64 Timeout;
1155 BOOLEAN InfiniteWait;
1156
1157 //
1158 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1159 //
1160 Packet = Trb->Packet;
1161 Timeout = Packet->Timeout;
1162 if (Timeout == 0) {
1163 InfiniteWait = TRUE;
1164 } else {
1165 InfiniteWait = FALSE;
1166 }
1167
1168 while (InfiniteWait || (Timeout > 0)) {
1169 //
1170 // Check Trb execution result by reading Normal Interrupt Status register.
1171 //
1172 Status = EmmcPeimCheckTrbEnv (Bar, Trb);
1173 if (Status != EFI_NOT_READY) {
1174 return Status;
1175 }
1176 //
1177 // Stall for 1 microsecond.
1178 //
1179 MicroSecondDelay (1);
1180
1181 Timeout--;
1182 }
1183
1184 return EFI_TIMEOUT;
1185 }
1186
1187 /**
1188 Execute the specified TRB.
1189
1190 @param[in] Bar The mmio base address of the slot to be accessed.
1191 @param[in] Trb The pointer to the EMMC_TRB instance.
1192
1193 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
1194 @retval Others Some erros happen when sending this request to the host controller.
1195
1196 **/
1197 EFI_STATUS
1198 EmmcPeimExecTrb (
1199 IN UINTN Bar,
1200 IN EMMC_TRB *Trb
1201 )
1202 {
1203 EFI_STATUS Status;
1204 EMMC_COMMAND_PACKET *Packet;
1205 UINT16 Cmd;
1206 UINT16 IntStatus;
1207 UINT32 Argument;
1208 UINT16 BlkCount;
1209 UINT16 BlkSize;
1210 UINT16 TransMode;
1211 UINT8 HostCtrl1;
1212 UINT32 SdmaAddr;
1213 UINT64 AdmaAddr;
1214
1215 Packet = Trb->Packet;
1216 //
1217 // Clear all bits in Error Interrupt Status Register
1218 //
1219 IntStatus = 0xFFFF;
1220 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_ERR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);
1221 if (EFI_ERROR (Status)) {
1222 return Status;
1223 }
1224 //
1225 // Clear all bits in Normal Interrupt Status Register
1226 //
1227 IntStatus = 0xFFFF;
1228 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);
1229 if (EFI_ERROR (Status)) {
1230 return Status;
1231 }
1232 //
1233 // Set Host Control 1 register DMA Select field
1234 //
1235 if (Trb->Mode == EmmcAdmaMode) {
1236 HostCtrl1 = BIT4;
1237 Status = EmmcPeimHcOrMmio (Bar + EMMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
1238 if (EFI_ERROR (Status)) {
1239 return Status;
1240 }
1241 }
1242
1243 EmmcPeimHcLedOnOff (Bar, TRUE);
1244
1245 if (Trb->Mode == EmmcSdmaMode) {
1246 if ((UINT64)(UINTN)Trb->Data >= 0x100000000ul) {
1247 return EFI_INVALID_PARAMETER;
1248 }
1249
1250 SdmaAddr = (UINT32)(UINTN)Trb->Data;
1251 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_SDMA_ADDR, FALSE, sizeof (SdmaAddr), &SdmaAddr);
1252 if (EFI_ERROR (Status)) {
1253 return Status;
1254 }
1255 } else if (Trb->Mode == EmmcAdmaMode) {
1256 AdmaAddr = (UINT64)(UINTN)Trb->AdmaDesc;
1257 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_ADMA_SYS_ADDR, FALSE, sizeof (AdmaAddr), &AdmaAddr);
1258 if (EFI_ERROR (Status)) {
1259 return Status;
1260 }
1261 }
1262
1263 BlkSize = Trb->BlockSize;
1264 if (Trb->Mode == EmmcSdmaMode) {
1265 //
1266 // Set SDMA boundary to be 512K bytes.
1267 //
1268 BlkSize |= 0x7000;
1269 }
1270
1271 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_BLK_SIZE, FALSE, sizeof (BlkSize), &BlkSize);
1272 if (EFI_ERROR (Status)) {
1273 return Status;
1274 }
1275
1276 BlkCount = (UINT16)(Trb->DataLen / Trb->BlockSize);
1277 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_BLK_COUNT, FALSE, sizeof (BlkCount), &BlkCount);
1278 if (EFI_ERROR (Status)) {
1279 return Status;
1280 }
1281
1282 Argument = Packet->EmmcCmdBlk->CommandArgument;
1283 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_ARG1, FALSE, sizeof (Argument), &Argument);
1284 if (EFI_ERROR (Status)) {
1285 return Status;
1286 }
1287
1288 TransMode = 0;
1289 if (Trb->Mode != EmmcNoData) {
1290 if (Trb->Mode != EmmcPioMode) {
1291 TransMode |= BIT0;
1292 }
1293 if (Trb->Read) {
1294 TransMode |= BIT4;
1295 }
1296 if (BlkCount != 0) {
1297 TransMode |= BIT5 | BIT1;
1298 }
1299 }
1300
1301 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_TRANS_MOD, FALSE, sizeof (TransMode), &TransMode);
1302 if (EFI_ERROR (Status)) {
1303 return Status;
1304 }
1305
1306 Cmd = (UINT16)LShiftU64(Packet->EmmcCmdBlk->CommandIndex, 8);
1307 if (Packet->EmmcCmdBlk->CommandType == EmmcCommandTypeAdtc) {
1308 Cmd |= BIT5;
1309 }
1310 //
1311 // Convert ResponseType to value
1312 //
1313 if (Packet->EmmcCmdBlk->CommandType != EmmcCommandTypeBc) {
1314 switch (Packet->EmmcCmdBlk->ResponseType) {
1315 case EmmcResponceTypeR1:
1316 case EmmcResponceTypeR5:
1317 case EmmcResponceTypeR6:
1318 case EmmcResponceTypeR7:
1319 Cmd |= (BIT1 | BIT3 | BIT4);
1320 break;
1321 case EmmcResponceTypeR2:
1322 Cmd |= (BIT0 | BIT3);
1323 break;
1324 case EmmcResponceTypeR3:
1325 case EmmcResponceTypeR4:
1326 Cmd |= BIT1;
1327 break;
1328 case EmmcResponceTypeR1b:
1329 case EmmcResponceTypeR5b:
1330 Cmd |= (BIT0 | BIT1 | BIT3 | BIT4);
1331 break;
1332 default:
1333 ASSERT (FALSE);
1334 break;
1335 }
1336 }
1337 //
1338 // Execute cmd
1339 //
1340 Status = EmmcPeimHcRwMmio (Bar + EMMC_HC_COMMAND, FALSE, sizeof (Cmd), &Cmd);
1341 return Status;
1342 }
1343
1344 /**
1345 Check the TRB execution result.
1346
1347 @param[in] Bar The mmio base address of the slot to be accessed.
1348 @param[in] Trb The pointer to the EMMC_TRB instance.
1349
1350 @retval EFI_SUCCESS The TRB is executed successfully.
1351 @retval EFI_NOT_READY The TRB is not completed for execution.
1352 @retval Others Some erros happen when executing this request.
1353
1354 **/
1355 EFI_STATUS
1356 EmmcPeimCheckTrbResult (
1357 IN UINTN Bar,
1358 IN EMMC_TRB *Trb
1359 )
1360 {
1361 EFI_STATUS Status;
1362 EMMC_COMMAND_PACKET *Packet;
1363 UINT16 IntStatus;
1364 UINT32 Response[4];
1365 UINT32 SdmaAddr;
1366 UINT8 Index;
1367 UINT8 SwReset;
1368
1369 SwReset = 0;
1370 Packet = Trb->Packet;
1371 //
1372 // Check Trb execution result by reading Normal Interrupt Status register.
1373 //
1374 Status = EmmcPeimHcRwMmio (
1375 Bar + EMMC_HC_NOR_INT_STS,
1376 TRUE,
1377 sizeof (IntStatus),
1378 &IntStatus
1379 );
1380 if (EFI_ERROR (Status)) {
1381 goto Done;
1382 }
1383 //
1384 // Check Transfer Complete bit is set or not.
1385 //
1386 if ((IntStatus & BIT1) == BIT1) {
1387 if ((IntStatus & BIT15) == BIT15) {
1388 //
1389 // Read Error Interrupt Status register to check if the error is
1390 // Data Timeout Error.
1391 // If yes, treat it as success as Transfer Complete has higher
1392 // priority than Data Timeout Error.
1393 //
1394 Status = EmmcPeimHcRwMmio (
1395 Bar + EMMC_HC_ERR_INT_STS,
1396 TRUE,
1397 sizeof (IntStatus),
1398 &IntStatus
1399 );
1400 if (!EFI_ERROR (Status)) {
1401 if ((IntStatus & BIT4) == BIT4) {
1402 Status = EFI_SUCCESS;
1403 } else {
1404 Status = EFI_DEVICE_ERROR;
1405 }
1406 }
1407 }
1408
1409 goto Done;
1410 }
1411 //
1412 // Check if there is a error happened during cmd execution.
1413 // If yes, then do error recovery procedure to follow SD Host Controller
1414 // Simplified Spec 3.0 section 3.10.1.
1415 //
1416 if ((IntStatus & BIT15) == BIT15) {
1417 Status = EmmcPeimHcRwMmio (
1418 Bar + EMMC_HC_ERR_INT_STS,
1419 TRUE,
1420 sizeof (IntStatus),
1421 &IntStatus
1422 );
1423 if (EFI_ERROR (Status)) {
1424 goto Done;
1425 }
1426
1427 if ((IntStatus & 0x0F) != 0) {
1428 SwReset |= BIT1;
1429 }
1430 if ((IntStatus & 0xF0) != 0) {
1431 SwReset |= BIT2;
1432 }
1433
1434 Status = EmmcPeimHcRwMmio (
1435 Bar + EMMC_HC_SW_RST,
1436 FALSE,
1437 sizeof (SwReset),
1438 &SwReset
1439 );
1440 if (EFI_ERROR (Status)) {
1441 goto Done;
1442 }
1443 Status = EmmcPeimHcWaitMmioSet (
1444 Bar + EMMC_HC_SW_RST,
1445 sizeof (SwReset),
1446 0xFF,
1447 0,
1448 EMMC_TIMEOUT
1449 );
1450 if (EFI_ERROR (Status)) {
1451 goto Done;
1452 }
1453
1454 Status = EFI_DEVICE_ERROR;
1455 goto Done;
1456 }
1457 //
1458 // Check if DMA interrupt is signalled for the SDMA transfer.
1459 //
1460 if ((Trb->Mode == EmmcSdmaMode) && ((IntStatus & BIT3) == BIT3)) {
1461 //
1462 // Clear DMA interrupt bit.
1463 //
1464 IntStatus = BIT3;
1465 Status = EmmcPeimHcRwMmio (
1466 Bar + EMMC_HC_NOR_INT_STS,
1467 FALSE,
1468 sizeof (IntStatus),
1469 &IntStatus
1470 );
1471 if (EFI_ERROR (Status)) {
1472 goto Done;
1473 }
1474 //
1475 // Update SDMA Address register.
1476 //
1477 SdmaAddr = EMMC_SDMA_ROUND_UP ((UINT32)(UINTN)Trb->Data, EMMC_SDMA_BOUNDARY);
1478 Status = EmmcPeimHcRwMmio (
1479 Bar + EMMC_HC_SDMA_ADDR,
1480 FALSE,
1481 sizeof (UINT32),
1482 &SdmaAddr
1483 );
1484 if (EFI_ERROR (Status)) {
1485 goto Done;
1486 }
1487 Trb->Data = (VOID*)(UINTN)SdmaAddr;
1488 }
1489
1490 if ((Packet->EmmcCmdBlk->CommandType != EmmcCommandTypeAdtc) &&
1491 (Packet->EmmcCmdBlk->ResponseType != EmmcResponceTypeR1b) &&
1492 (Packet->EmmcCmdBlk->ResponseType != EmmcResponceTypeR5b)) {
1493 if ((IntStatus & BIT0) == BIT0) {
1494 Status = EFI_SUCCESS;
1495 goto Done;
1496 }
1497 }
1498
1499 if (Packet->EmmcCmdBlk->CommandIndex == EMMC_SEND_TUNING_BLOCK) {
1500 Status = EFI_SUCCESS;
1501 goto Done;
1502 }
1503
1504 Status = EFI_NOT_READY;
1505 Done:
1506 //
1507 // Get response data when the cmd is executed successfully.
1508 //
1509 if (!EFI_ERROR (Status)) {
1510 if (Packet->EmmcCmdBlk->CommandType != EmmcCommandTypeBc) {
1511 for (Index = 0; Index < 4; Index++) {
1512 Status = EmmcPeimHcRwMmio (
1513 Bar + EMMC_HC_RESPONSE + Index * 4,
1514 TRUE,
1515 sizeof (UINT32),
1516 &Response[Index]
1517 );
1518 if (EFI_ERROR (Status)) {
1519 EmmcPeimHcLedOnOff (Bar, FALSE);
1520 return Status;
1521 }
1522 }
1523 CopyMem (Packet->EmmcStatusBlk, Response, sizeof (Response));
1524 }
1525 }
1526
1527 if (Status != EFI_NOT_READY) {
1528 EmmcPeimHcLedOnOff (Bar, FALSE);
1529 }
1530
1531 return Status;
1532 }
1533
1534 /**
1535 Wait for the TRB execution result.
1536
1537 @param[in] Bar The mmio base address of the slot to be accessed.
1538 @param[in] Trb The pointer to the EMMC_TRB instance.
1539
1540 @retval EFI_SUCCESS The TRB is executed successfully.
1541 @retval Others Some erros happen when executing this request.
1542
1543 **/
1544 EFI_STATUS
1545 EmmcPeimWaitTrbResult (
1546 IN UINTN Bar,
1547 IN EMMC_TRB *Trb
1548 )
1549 {
1550 EFI_STATUS Status;
1551 EMMC_COMMAND_PACKET *Packet;
1552 UINT64 Timeout;
1553 BOOLEAN InfiniteWait;
1554
1555 Packet = Trb->Packet;
1556 //
1557 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1558 //
1559 Timeout = Packet->Timeout;
1560 if (Timeout == 0) {
1561 InfiniteWait = TRUE;
1562 } else {
1563 InfiniteWait = FALSE;
1564 }
1565
1566 while (InfiniteWait || (Timeout > 0)) {
1567 //
1568 // Check Trb execution result by reading Normal Interrupt Status register.
1569 //
1570 Status = EmmcPeimCheckTrbResult (Bar, Trb);
1571 if (Status != EFI_NOT_READY) {
1572 return Status;
1573 }
1574 //
1575 // Stall for 1 microsecond.
1576 //
1577 MicroSecondDelay (1);
1578
1579 Timeout--;
1580 }
1581
1582 return EFI_TIMEOUT;
1583 }
1584
1585 /**
1586 Sends EMMC command to an EMMC card that is attached to the EMMC controller.
1587
1588 If Packet is successfully sent to the EMMC card, then EFI_SUCCESS is returned.
1589
1590 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
1591
1592 If Slot is not in a valid range for the EMMC controller, then EFI_INVALID_PARAMETER
1593 is returned.
1594
1595 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
1596 EFI_INVALID_PARAMETER is returned.
1597
1598 @param[in] Slot The slot number of the Emmc card to send the command to.
1599 @param[in,out] Packet A pointer to the EMMC command data structure.
1600
1601 @retval EFI_SUCCESS The EMMC Command Packet was sent by the host.
1602 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
1603 command Packet.
1604 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
1605 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
1606 OutDataBuffer are NULL.
1607 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1608 @retval EFI_UNSUPPORTED The command described by the EMMC Command Packet is not
1609 supported by the host controller.
1610 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
1611 limit supported by EMMC card ( i.e. if the number of bytes
1612 exceed the Last LBA).
1613
1614 **/
1615 EFI_STATUS
1616 EFIAPI
1617 EmmcPeimExecCmd (
1618 IN EMMC_PEIM_HC_SLOT *Slot,
1619 IN OUT EMMC_COMMAND_PACKET *Packet
1620 )
1621 {
1622 EFI_STATUS Status;
1623 EMMC_TRB *Trb;
1624
1625 if (Packet == NULL) {
1626 return EFI_INVALID_PARAMETER;
1627 }
1628
1629 if ((Packet->EmmcCmdBlk == NULL) || (Packet->EmmcStatusBlk == NULL)) {
1630 return EFI_INVALID_PARAMETER;
1631 }
1632
1633 if ((Packet->OutDataBuffer == NULL) && (Packet->OutTransferLength != 0)) {
1634 return EFI_INVALID_PARAMETER;
1635 }
1636
1637 if ((Packet->InDataBuffer == NULL) && (Packet->InTransferLength != 0)) {
1638 return EFI_INVALID_PARAMETER;
1639 }
1640
1641 Trb = EmmcPeimCreateTrb (Slot, Packet);
1642 if (Trb == NULL) {
1643 return EFI_OUT_OF_RESOURCES;
1644 }
1645
1646 Status = EmmcPeimWaitTrbEnv (Slot->EmmcHcBase, Trb);
1647 if (EFI_ERROR (Status)) {
1648 goto Done;
1649 }
1650
1651 Status = EmmcPeimExecTrb (Slot->EmmcHcBase, Trb);
1652 if (EFI_ERROR (Status)) {
1653 goto Done;
1654 }
1655
1656 Status = EmmcPeimWaitTrbResult (Slot->EmmcHcBase, Trb);
1657 if (EFI_ERROR (Status)) {
1658 goto Done;
1659 }
1660
1661 Done:
1662 EmmcPeimFreeTrb (Trb);
1663
1664 return Status;
1665 }
1666
1667 /**
1668 Send command GO_IDLE_STATE (CMD0 with argument of 0x00000000) to the device to
1669 make it go to Idle State.
1670
1671 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1672
1673 @param[in] Slot The slot number of the Emmc card to send the command to.
1674
1675 @retval EFI_SUCCESS The EMMC device is reset correctly.
1676 @retval Others The device reset fails.
1677
1678 **/
1679 EFI_STATUS
1680 EmmcPeimReset (
1681 IN EMMC_PEIM_HC_SLOT *Slot
1682 )
1683 {
1684 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1685 EMMC_STATUS_BLOCK EmmcStatusBlk;
1686 EMMC_COMMAND_PACKET Packet;
1687 EFI_STATUS Status;
1688
1689 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1690 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1691 ZeroMem (&Packet, sizeof (Packet));
1692
1693 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1694 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1695 Packet.Timeout = EMMC_TIMEOUT;
1696
1697 EmmcCmdBlk.CommandIndex = EMMC_GO_IDLE_STATE;
1698 EmmcCmdBlk.CommandType = EmmcCommandTypeBc;
1699 EmmcCmdBlk.ResponseType = 0;
1700 EmmcCmdBlk.CommandArgument = 0;
1701
1702 Status = EmmcPeimExecCmd (Slot, &Packet);
1703
1704 return Status;
1705 }
1706
1707 /**
1708 Send command SEND_OP_COND to the EMMC device to get the data of the OCR register.
1709
1710 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1711
1712 @param[in] Slot The slot number of the Emmc card to send the command to.
1713 @param[in, out] Argument On input, the argument of SEND_OP_COND is to send to the device.
1714 On output, the argument is the value of OCR register.
1715
1716 @retval EFI_SUCCESS The operation is done correctly.
1717 @retval Others The operation fails.
1718
1719 **/
1720 EFI_STATUS
1721 EmmcPeimGetOcr (
1722 IN EMMC_PEIM_HC_SLOT *Slot,
1723 IN OUT UINT32 *Argument
1724 )
1725 {
1726 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1727 EMMC_STATUS_BLOCK EmmcStatusBlk;
1728 EMMC_COMMAND_PACKET Packet;
1729 EFI_STATUS Status;
1730
1731 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1732 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1733 ZeroMem (&Packet, sizeof (Packet));
1734
1735 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1736 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1737 Packet.Timeout = EMMC_TIMEOUT;
1738
1739 EmmcCmdBlk.CommandIndex = EMMC_SEND_OP_COND;
1740 EmmcCmdBlk.CommandType = EmmcCommandTypeBcr;
1741 EmmcCmdBlk.ResponseType = EmmcResponceTypeR3;
1742 EmmcCmdBlk.CommandArgument = *Argument;
1743
1744 Status = EmmcPeimExecCmd (Slot, &Packet);
1745 if (!EFI_ERROR (Status)) {
1746 //
1747 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
1748 //
1749 *Argument = EmmcStatusBlk.Resp0;
1750 }
1751
1752 return Status;
1753 }
1754
1755 /**
1756 Broadcast command ALL_SEND_CID to the bus to ask all the EMMC devices to send the
1757 data of their CID registers.
1758
1759 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
1760
1761 @param[in] Slot The slot number of the Emmc card to send the command to.
1762
1763 @retval EFI_SUCCESS The operation is done correctly.
1764 @retval Others The operation fails.
1765
1766 **/
1767 EFI_STATUS
1768 EmmcPeimGetAllCid (
1769 IN EMMC_PEIM_HC_SLOT *Slot
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_ALL_SEND_CID;
1786 EmmcCmdBlk.CommandType = EmmcCommandTypeBcr;
1787 EmmcCmdBlk.ResponseType = EmmcResponceTypeR2;
1788 EmmcCmdBlk.CommandArgument = 0;
1789
1790 Status = EmmcPeimExecCmd (Slot, &Packet);
1791
1792 return Status;
1793 }
1794
1795 /**
1796 Send command SET_RELATIVE_ADDR to the EMMC device to assign a Relative device
1797 Address (RCA).
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 @param[in] Rca The relative device address to be assigned.
1803
1804 @retval EFI_SUCCESS The operation is done correctly.
1805 @retval Others The operation fails.
1806
1807 **/
1808 EFI_STATUS
1809 EmmcPeimSetRca (
1810 IN EMMC_PEIM_HC_SLOT *Slot,
1811 IN UINT32 Rca
1812 )
1813 {
1814 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1815 EMMC_STATUS_BLOCK EmmcStatusBlk;
1816 EMMC_COMMAND_PACKET Packet;
1817 EFI_STATUS Status;
1818
1819 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1820 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1821 ZeroMem (&Packet, sizeof (Packet));
1822
1823 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1824 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1825 Packet.Timeout = EMMC_TIMEOUT;
1826
1827 EmmcCmdBlk.CommandIndex = EMMC_SET_RELATIVE_ADDR;
1828 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
1829 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
1830 EmmcCmdBlk.CommandArgument = Rca << 16;
1831
1832 Status = EmmcPeimExecCmd (Slot, &Packet);
1833
1834 return Status;
1835 }
1836
1837 /**
1838 Send command SEND_CSD to the EMMC device to get the data of the CSD register.
1839
1840 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1841
1842 @param[in] Slot The slot number of the Emmc card to send the command to.
1843 @param[in] Rca The relative device address of selected device.
1844 @param[out] Csd The buffer to store the content of the CSD register.
1845 Note the caller should ignore the lowest byte of this
1846 buffer as the content of this byte is meaningless even
1847 if the operation succeeds.
1848
1849 @retval EFI_SUCCESS The operation is done correctly.
1850 @retval Others The operation fails.
1851
1852 **/
1853 EFI_STATUS
1854 EmmcPeimGetCsd (
1855 IN EMMC_PEIM_HC_SLOT *Slot,
1856 IN UINT32 Rca,
1857 OUT EMMC_CSD *Csd
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_SEND_CSD;
1874 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
1875 EmmcCmdBlk.ResponseType = EmmcResponceTypeR2;
1876 EmmcCmdBlk.CommandArgument = Rca << 16;
1877
1878 Status = EmmcPeimExecCmd (Slot, &Packet);
1879 if (!EFI_ERROR (Status)) {
1880 //
1881 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
1882 //
1883 CopyMem (((UINT8*)Csd) + 1, &EmmcStatusBlk.Resp0, sizeof (EMMC_CSD) - 1);
1884 }
1885
1886 return Status;
1887 }
1888
1889 /**
1890 Send command SELECT_DESELECT_CARD to the EMMC device to select/deselect it.
1891
1892 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1893
1894 @param[in] Slot The slot number of the Emmc card to send the command to.
1895 @param[in] Rca The relative device address of selected device.
1896
1897 @retval EFI_SUCCESS The operation is done correctly.
1898 @retval Others The operation fails.
1899
1900 **/
1901 EFI_STATUS
1902 EmmcPeimSelect (
1903 IN EMMC_PEIM_HC_SLOT *Slot,
1904 IN UINT32 Rca
1905 )
1906 {
1907 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1908 EMMC_STATUS_BLOCK EmmcStatusBlk;
1909 EMMC_COMMAND_PACKET Packet;
1910 EFI_STATUS Status;
1911
1912 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1913 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1914 ZeroMem (&Packet, sizeof (Packet));
1915
1916 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1917 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1918 Packet.Timeout = EMMC_TIMEOUT;
1919
1920 EmmcCmdBlk.CommandIndex = EMMC_SELECT_DESELECT_CARD;
1921 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
1922 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
1923 EmmcCmdBlk.CommandArgument = Rca << 16;
1924
1925 Status = EmmcPeimExecCmd (Slot, &Packet);
1926
1927 return Status;
1928 }
1929
1930 /**
1931 Send command SEND_EXT_CSD to the EMMC device to get the data of the EXT_CSD register.
1932
1933 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1934
1935 @param[in] Slot The slot number of the Emmc card to send the command to.
1936 @param[out] ExtCsd The buffer to store the content of the EXT_CSD register.
1937
1938 @retval EFI_SUCCESS The operation is done correctly.
1939 @retval Others The operation fails.
1940
1941 **/
1942 EFI_STATUS
1943 EmmcPeimGetExtCsd (
1944 IN EMMC_PEIM_HC_SLOT *Slot,
1945 OUT EMMC_EXT_CSD *ExtCsd
1946 )
1947 {
1948 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1949 EMMC_STATUS_BLOCK EmmcStatusBlk;
1950 EMMC_COMMAND_PACKET Packet;
1951 EFI_STATUS Status;
1952
1953 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
1954 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
1955 ZeroMem (&Packet, sizeof (Packet));
1956
1957 Packet.EmmcCmdBlk = &EmmcCmdBlk;
1958 Packet.EmmcStatusBlk = &EmmcStatusBlk;
1959 Packet.Timeout = EMMC_TIMEOUT;
1960
1961 EmmcCmdBlk.CommandIndex = EMMC_SEND_EXT_CSD;
1962 EmmcCmdBlk.CommandType = EmmcCommandTypeAdtc;
1963 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
1964 EmmcCmdBlk.CommandArgument = 0x00000000;
1965
1966 Packet.InDataBuffer = ExtCsd;
1967 Packet.InTransferLength = sizeof (EMMC_EXT_CSD);
1968
1969 Status = EmmcPeimExecCmd (Slot, &Packet);
1970 return Status;
1971 }
1972
1973 /**
1974 Send command SWITCH to the EMMC device to switch the mode of operation of the
1975 selected Device or modifies the EXT_CSD registers.
1976
1977 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
1978
1979 @param[in] Slot The slot number of the Emmc card to send the command to.
1980 @param[in] Access The access mode of SWTICH command.
1981 @param[in] Index The offset of the field to be access.
1982 @param[in] Value The value to be set to the specified field of EXT_CSD register.
1983 @param[in] CmdSet The value of CmdSet field of EXT_CSD register.
1984
1985 @retval EFI_SUCCESS The operation is done correctly.
1986 @retval Others The operation fails.
1987
1988 **/
1989 EFI_STATUS
1990 EmmcPeimSwitch (
1991 IN EMMC_PEIM_HC_SLOT *Slot,
1992 IN UINT8 Access,
1993 IN UINT8 Index,
1994 IN UINT8 Value,
1995 IN UINT8 CmdSet
1996 )
1997 {
1998 EMMC_COMMAND_BLOCK EmmcCmdBlk;
1999 EMMC_STATUS_BLOCK EmmcStatusBlk;
2000 EMMC_COMMAND_PACKET Packet;
2001 EFI_STATUS Status;
2002
2003 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2004 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2005 ZeroMem (&Packet, sizeof (Packet));
2006
2007 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2008 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2009 Packet.Timeout = EMMC_TIMEOUT;
2010
2011 EmmcCmdBlk.CommandIndex = EMMC_SWITCH;
2012 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
2013 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1b;
2014 EmmcCmdBlk.CommandArgument = (Access << 24) | (Index << 16) | (Value << 8) | CmdSet;
2015
2016 Status = EmmcPeimExecCmd (Slot, &Packet);
2017
2018 return Status;
2019 }
2020
2021 /**
2022 Send command SEND_STATUS to the addressed EMMC device to get its status register.
2023
2024 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2025
2026 @param[in] Slot The slot number of the Emmc card to send the command to.
2027 @param[in] Rca The relative device address of addressed device.
2028 @param[out] DevStatus The returned device status.
2029
2030 @retval EFI_SUCCESS The operation is done correctly.
2031 @retval Others The operation fails.
2032
2033 **/
2034 EFI_STATUS
2035 EmmcPeimSendStatus (
2036 IN EMMC_PEIM_HC_SLOT *Slot,
2037 IN UINT32 Rca,
2038 OUT UINT32 *DevStatus
2039 )
2040 {
2041 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2042 EMMC_STATUS_BLOCK EmmcStatusBlk;
2043 EMMC_COMMAND_PACKET Packet;
2044 EFI_STATUS Status;
2045
2046 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2047 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2048 ZeroMem (&Packet, sizeof (Packet));
2049
2050 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2051 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2052 Packet.Timeout = EMMC_TIMEOUT;
2053
2054 EmmcCmdBlk.CommandIndex = EMMC_SEND_STATUS;
2055 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
2056 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2057 EmmcCmdBlk.CommandArgument = Rca << 16;
2058
2059 Status = EmmcPeimExecCmd (Slot, &Packet);
2060 if (!EFI_ERROR (Status)) {
2061 *DevStatus = EmmcStatusBlk.Resp0;
2062 }
2063
2064 return Status;
2065 }
2066
2067 /**
2068 Send command SET_BLOCK_COUNT to the addressed EMMC device to set the number of
2069 blocks for the following block read/write cmd.
2070
2071 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2072
2073 @param[in] Slot The slot number of the Emmc card to send the command to.
2074 @param[in] BlockCount The number of the logical block to access.
2075
2076 @retval EFI_SUCCESS The operation is done correctly.
2077 @retval Others The operation fails.
2078
2079 **/
2080 EFI_STATUS
2081 EmmcPeimSetBlkCount (
2082 IN EMMC_PEIM_HC_SLOT *Slot,
2083 IN UINT16 BlockCount
2084 )
2085 {
2086 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2087 EMMC_STATUS_BLOCK EmmcStatusBlk;
2088 EMMC_COMMAND_PACKET Packet;
2089 EFI_STATUS Status;
2090
2091 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2092 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2093 ZeroMem (&Packet, sizeof (Packet));
2094
2095 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2096 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2097 Packet.Timeout = EMMC_TIMEOUT;
2098
2099 EmmcCmdBlk.CommandIndex = EMMC_SET_BLOCK_COUNT;
2100 EmmcCmdBlk.CommandType = EmmcCommandTypeAc;
2101 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2102 EmmcCmdBlk.CommandArgument = BlockCount;
2103
2104 Status = EmmcPeimExecCmd (Slot, &Packet);
2105
2106 return Status;
2107 }
2108
2109 /**
2110 Send command READ_MULTIPLE_BLOCK/WRITE_MULTIPLE_BLOCK to the addressed EMMC device
2111 to read/write the specified number of blocks.
2112
2113 Refer to EMMC Electrical Standard Spec 5.1 Section 6.10.4 for details.
2114
2115 @param[in] Slot The slot number of the Emmc card to send the command to.
2116 @param[in] Lba The logical block address of starting access.
2117 @param[in] BlockSize The block size of specified EMMC device partition.
2118 @param[in] Buffer The pointer to the transfer buffer.
2119 @param[in] BufferSize The size of transfer buffer.
2120 @param[in] IsRead Boolean to show the operation direction.
2121
2122 @retval EFI_SUCCESS The operation is done correctly.
2123 @retval Others The operation fails.
2124
2125 **/
2126 EFI_STATUS
2127 EmmcPeimRwMultiBlocks (
2128 IN EMMC_PEIM_HC_SLOT *Slot,
2129 IN EFI_LBA Lba,
2130 IN UINT32 BlockSize,
2131 IN VOID *Buffer,
2132 IN UINTN BufferSize,
2133 IN BOOLEAN IsRead
2134 )
2135 {
2136 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2137 EMMC_STATUS_BLOCK EmmcStatusBlk;
2138 EMMC_COMMAND_PACKET Packet;
2139 EFI_STATUS Status;
2140
2141 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2142 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2143 ZeroMem (&Packet, sizeof (Packet));
2144
2145 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2146 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2147 //
2148 // Calculate timeout value through the below formula.
2149 // Timeout = (transfer size) / (2MB/s).
2150 // Taking 2MB/s as divisor is because it's nearest to the eMMC lowest
2151 // transfer speed (2.4MB/s).
2152 // Refer to eMMC 5.0 spec section 6.9.1 for details.
2153 //
2154 Packet.Timeout = (BufferSize / (2 * 1024 * 1024) + 1) * 1000 * 1000;;
2155
2156 if (IsRead) {
2157 Packet.InDataBuffer = Buffer;
2158 Packet.InTransferLength = (UINT32)BufferSize;
2159
2160 EmmcCmdBlk.CommandIndex = EMMC_READ_MULTIPLE_BLOCK;
2161 EmmcCmdBlk.CommandType = EmmcCommandTypeAdtc;
2162 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2163 } else {
2164 Packet.OutDataBuffer = Buffer;
2165 Packet.OutTransferLength = (UINT32)BufferSize;
2166
2167 EmmcCmdBlk.CommandIndex = EMMC_WRITE_MULTIPLE_BLOCK;
2168 EmmcCmdBlk.CommandType = EmmcCommandTypeAdtc;
2169 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2170 }
2171
2172 if (Slot->SectorAddressing) {
2173 EmmcCmdBlk.CommandArgument = (UINT32)Lba;
2174 } else {
2175 EmmcCmdBlk.CommandArgument = (UINT32)MultU64x32 (Lba, BlockSize);
2176 }
2177
2178 Status = EmmcPeimExecCmd (Slot, &Packet);
2179
2180 return Status;
2181 }
2182
2183 /**
2184 Send command SEND_TUNING_BLOCK to the EMMC device for HS200 optimal sampling point
2185 detection.
2186
2187 It may be sent up to 40 times until the host finishes the tuning procedure.
2188
2189 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 for details.
2190
2191 @param[in] Slot The slot number of the Emmc card to send the command to.
2192 @param[in] BusWidth The bus width to work.
2193
2194 @retval EFI_SUCCESS The operation is done correctly.
2195 @retval Others The operation fails.
2196
2197 **/
2198 EFI_STATUS
2199 EmmcPeimSendTuningBlk (
2200 IN EMMC_PEIM_HC_SLOT *Slot,
2201 IN UINT8 BusWidth
2202 )
2203 {
2204 EMMC_COMMAND_BLOCK EmmcCmdBlk;
2205 EMMC_STATUS_BLOCK EmmcStatusBlk;
2206 EMMC_COMMAND_PACKET Packet;
2207 EFI_STATUS Status;
2208 UINT8 TuningBlock[128];
2209
2210 ZeroMem (&EmmcCmdBlk, sizeof (EmmcCmdBlk));
2211 ZeroMem (&EmmcStatusBlk, sizeof (EmmcStatusBlk));
2212 ZeroMem (&Packet, sizeof (Packet));
2213
2214 Packet.EmmcCmdBlk = &EmmcCmdBlk;
2215 Packet.EmmcStatusBlk = &EmmcStatusBlk;
2216 Packet.Timeout = EMMC_TIMEOUT;
2217
2218 EmmcCmdBlk.CommandIndex = EMMC_SEND_TUNING_BLOCK;
2219 EmmcCmdBlk.CommandType = EmmcCommandTypeAdtc;
2220 EmmcCmdBlk.ResponseType = EmmcResponceTypeR1;
2221 EmmcCmdBlk.CommandArgument = 0;
2222
2223 Packet.InDataBuffer = TuningBlock;
2224 if (BusWidth == 8) {
2225 Packet.InTransferLength = sizeof (TuningBlock);
2226 } else {
2227 Packet.InTransferLength = 64;
2228 }
2229
2230 Status = EmmcPeimExecCmd (Slot, &Packet);
2231
2232 return Status;
2233 }
2234
2235 /**
2236 Tunning the clock to get HS200 optimal sampling point.
2237
2238 Command SEND_TUNING_BLOCK may be sent up to 40 times until the host finishes the
2239 tuning procedure.
2240
2241 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2242 Simplified Spec 3.0 section Figure 2-29 for details.
2243
2244 @param[in] Slot The slot number of the Emmc card to send the command to.
2245 @param[in] BusWidth The bus width to work.
2246
2247 @retval EFI_SUCCESS The operation is done correctly.
2248 @retval Others The operation fails.
2249
2250 **/
2251 EFI_STATUS
2252 EmmcPeimTuningClkForHs200 (
2253 IN EMMC_PEIM_HC_SLOT *Slot,
2254 IN UINT8 BusWidth
2255 )
2256 {
2257 EFI_STATUS Status;
2258 UINT8 HostCtrl2;
2259 UINT8 Retry;
2260
2261 //
2262 // Notify the host that the sampling clock tuning procedure starts.
2263 //
2264 HostCtrl2 = BIT6;
2265 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2266 if (EFI_ERROR (Status)) {
2267 return Status;
2268 }
2269 //
2270 // Ask the device to send a sequence of tuning blocks till the tuning procedure is done.
2271 //
2272 Retry = 0;
2273 do {
2274 Status = EmmcPeimSendTuningBlk (Slot, BusWidth);
2275 if (EFI_ERROR (Status)) {
2276 return Status;
2277 }
2278
2279 Status = EmmcPeimHcRwMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, TRUE, sizeof (HostCtrl2), &HostCtrl2);
2280 if (EFI_ERROR (Status)) {
2281 return Status;
2282 }
2283
2284 if ((HostCtrl2 & (BIT6 | BIT7)) == BIT7) {
2285 break;
2286 }
2287 } while (++Retry < 40);
2288
2289 if (Retry == 40) {
2290 Status = EFI_TIMEOUT;
2291 }
2292 return Status;
2293 }
2294
2295 /**
2296 Switch the bus width to specified width.
2297
2298 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.9 and SD Host Controller
2299 Simplified Spec 3.0 section Figure 3-7 for details.
2300
2301 @param[in] Slot The slot number of the Emmc card to send the command to.
2302 @param[in] Rca The relative device address to be assigned.
2303 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
2304 use single data rate data simpling method.
2305 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2306
2307 @retval EFI_SUCCESS The operation is done correctly.
2308 @retval Others The operation fails.
2309
2310 **/
2311 EFI_STATUS
2312 EmmcPeimSwitchBusWidth (
2313 IN EMMC_PEIM_HC_SLOT *Slot,
2314 IN UINT32 Rca,
2315 IN BOOLEAN IsDdr,
2316 IN UINT8 BusWidth
2317 )
2318 {
2319 EFI_STATUS Status;
2320 UINT8 Access;
2321 UINT8 Index;
2322 UINT8 Value;
2323 UINT8 CmdSet;
2324 UINT32 DevStatus;
2325
2326 //
2327 // Write Byte, the Value field is written into the byte pointed by Index.
2328 //
2329 Access = 0x03;
2330 Index = OFFSET_OF (EMMC_EXT_CSD, BusWidth);
2331 if (BusWidth == 4) {
2332 Value = 1;
2333 } else if (BusWidth == 8) {
2334 Value = 2;
2335 } else {
2336 return EFI_INVALID_PARAMETER;
2337 }
2338
2339 if (IsDdr) {
2340 Value += 4;
2341 }
2342
2343 CmdSet = 0;
2344 Status = EmmcPeimSwitch (Slot, Access, Index, Value, CmdSet);
2345 if (EFI_ERROR (Status)) {
2346 return Status;
2347 }
2348
2349 Status = EmmcPeimSendStatus (Slot, Rca, &DevStatus);
2350 if (EFI_ERROR (Status)) {
2351 return Status;
2352 }
2353 //
2354 // Check the switch operation is really successful or not.
2355 //
2356 if ((DevStatus & BIT7) != 0) {
2357 return EFI_DEVICE_ERROR;
2358 }
2359
2360 Status = EmmcPeimHcSetBusWidth (Slot->EmmcHcBase, BusWidth);
2361
2362 return Status;
2363 }
2364
2365 /**
2366 Switch the clock frequency to the specified value.
2367
2368 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6 and SD Host Controller
2369 Simplified Spec 3.0 section Figure 3-3 for details.
2370
2371 @param[in] Slot The slot number of the Emmc card to send the command to.
2372 @param[in] Rca The relative device address to be assigned.
2373 @param[in] HsTiming The value to be written to HS_TIMING field of EXT_CSD register.
2374 @param[in] ClockFreq The max clock frequency to be set, the unit is MHz.
2375
2376 @retval EFI_SUCCESS The operation is done correctly.
2377 @retval Others The operation fails.
2378
2379 **/
2380 EFI_STATUS
2381 EmmcPeimSwitchClockFreq (
2382 IN EMMC_PEIM_HC_SLOT *Slot,
2383 IN UINT32 Rca,
2384 IN UINT8 HsTiming,
2385 IN UINT32 ClockFreq
2386 )
2387 {
2388 EFI_STATUS Status;
2389 UINT8 Access;
2390 UINT8 Index;
2391 UINT8 Value;
2392 UINT8 CmdSet;
2393 UINT32 DevStatus;
2394
2395 //
2396 // Write Byte, the Value field is written into the byte pointed by Index.
2397 //
2398 Access = 0x03;
2399 Index = OFFSET_OF (EMMC_EXT_CSD, HsTiming);
2400 Value = HsTiming;
2401 CmdSet = 0;
2402
2403 Status = EmmcPeimSwitch (Slot, Access, Index, Value, CmdSet);
2404 if (EFI_ERROR (Status)) {
2405 return Status;
2406 }
2407
2408 Status = EmmcPeimSendStatus (Slot, Rca, &DevStatus);
2409 if (EFI_ERROR (Status)) {
2410 return Status;
2411 }
2412 //
2413 // Check the switch operation is really successful or not.
2414 //
2415 if ((DevStatus & BIT7) != 0) {
2416 return EFI_DEVICE_ERROR;
2417 }
2418 //
2419 // Convert the clock freq unit from MHz to KHz.
2420 //
2421 Status = EmmcPeimHcClockSupply (Slot->EmmcHcBase, ClockFreq * 1000);
2422
2423 return Status;
2424 }
2425
2426 /**
2427 Switch to the High Speed timing according to request.
2428
2429 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2430 Simplified Spec 3.0 section Figure 2-29 for details.
2431
2432 @param[in] Slot The slot number of the Emmc card to send the command to.
2433 @param[in] Rca The relative device address to be assigned.
2434 @param[in] ClockFreq The max clock frequency to be set.
2435 @param[in] IsDdr If TRUE, use dual data rate data simpling method. Otherwise
2436 use single data rate data simpling method.
2437 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2438
2439 @retval EFI_SUCCESS The operation is done correctly.
2440 @retval Others The operation fails.
2441
2442 **/
2443 EFI_STATUS
2444 EmmcPeimSwitchToHighSpeed (
2445 IN EMMC_PEIM_HC_SLOT *Slot,
2446 IN UINT32 Rca,
2447 IN UINT32 ClockFreq,
2448 IN BOOLEAN IsDdr,
2449 IN UINT8 BusWidth
2450 )
2451 {
2452 EFI_STATUS Status;
2453 UINT8 HsTiming;
2454 UINT8 HostCtrl1;
2455 UINT8 HostCtrl2;
2456
2457 Status = EmmcPeimSwitchBusWidth (Slot, Rca, IsDdr, BusWidth);
2458 if (EFI_ERROR (Status)) {
2459 return Status;
2460 }
2461 //
2462 // Set to Hight Speed timing
2463 //
2464 HostCtrl1 = BIT2;
2465 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
2466 if (EFI_ERROR (Status)) {
2467 return Status;
2468 }
2469
2470 HostCtrl2 = (UINT8)~0x7;
2471 Status = EmmcPeimHcAndMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2472 if (EFI_ERROR (Status)) {
2473 return Status;
2474 }
2475 if (IsDdr) {
2476 HostCtrl2 = BIT2;
2477 } else if (ClockFreq == 52) {
2478 HostCtrl2 = BIT0;
2479 } else {
2480 HostCtrl2 = 0;
2481 }
2482 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2483 if (EFI_ERROR (Status)) {
2484 return Status;
2485 }
2486
2487 HsTiming = 1;
2488 Status = EmmcPeimSwitchClockFreq (Slot, Rca, HsTiming, ClockFreq);
2489 if (EFI_ERROR (Status)) {
2490 return Status;
2491 }
2492
2493 return Status;
2494 }
2495
2496 /**
2497 Switch to the HS200 timing according to request.
2498
2499 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2500 Simplified Spec 3.0 section Figure 2-29 for details.
2501
2502 @param[in] Slot The slot number of the Emmc card to send the command to.
2503 @param[in] Rca The relative device address to be assigned.
2504 @param[in] ClockFreq The max clock frequency to be set.
2505 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2506
2507 @retval EFI_SUCCESS The operation is done correctly.
2508 @retval Others The operation fails.
2509
2510 **/
2511 EFI_STATUS
2512 EmmcPeimSwitchToHS200 (
2513 IN EMMC_PEIM_HC_SLOT *Slot,
2514 IN UINT32 Rca,
2515 IN UINT32 ClockFreq,
2516 IN UINT8 BusWidth
2517 )
2518 {
2519 EFI_STATUS Status;
2520 UINT8 HsTiming;
2521 UINT8 HostCtrl2;
2522 UINT16 ClockCtrl;
2523
2524 if ((BusWidth != 4) && (BusWidth != 8)) {
2525 return EFI_INVALID_PARAMETER;
2526 }
2527
2528 Status = EmmcPeimSwitchBusWidth (Slot, Rca, FALSE, BusWidth);
2529 if (EFI_ERROR (Status)) {
2530 return Status;
2531 }
2532 //
2533 // Set to HS200/SDR104 timing
2534 //
2535 //
2536 // Stop bus clock at first
2537 //
2538 Status = EmmcPeimHcStopClock (Slot->EmmcHcBase);
2539 if (EFI_ERROR (Status)) {
2540 return Status;
2541 }
2542
2543 HostCtrl2 = (UINT8)~0x7;
2544 Status = EmmcPeimHcAndMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2545 if (EFI_ERROR (Status)) {
2546 return Status;
2547 }
2548 HostCtrl2 = BIT0 | BIT1;
2549 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2550 if (EFI_ERROR (Status)) {
2551 return Status;
2552 }
2553
2554 //
2555 // Wait Internal Clock Stable in the Clock Control register to be 1 before set SD Clock Enable bit
2556 //
2557 Status = EmmcPeimHcWaitMmioSet (
2558 Slot->EmmcHcBase + EMMC_HC_CLOCK_CTRL,
2559 sizeof (ClockCtrl),
2560 BIT1,
2561 BIT1,
2562 EMMC_TIMEOUT
2563 );
2564 if (EFI_ERROR (Status)) {
2565 return Status;
2566 }
2567 //
2568 // Set SD Clock Enable in the Clock Control register to 1
2569 //
2570 ClockCtrl = BIT2;
2571 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);
2572
2573 HsTiming = 2;
2574 Status = EmmcPeimSwitchClockFreq (Slot, Rca, HsTiming, ClockFreq);
2575 if (EFI_ERROR (Status)) {
2576 return Status;
2577 }
2578
2579 Status = EmmcPeimTuningClkForHs200 (Slot, BusWidth);
2580
2581 return Status;
2582 }
2583
2584 /**
2585 Switch to the HS400 timing according to request.
2586
2587 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2588 Simplified Spec 3.0 section Figure 2-29 for details.
2589
2590 @param[in] Slot The slot number of the Emmc card to send the command to.
2591 @param[in] Rca The relative device address to be assigned.
2592 @param[in] ClockFreq The max clock frequency to be set.
2593
2594 @retval EFI_SUCCESS The operation is done correctly.
2595 @retval Others The operation fails.
2596
2597 **/
2598 EFI_STATUS
2599 EmmcPeimSwitchToHS400 (
2600 IN EMMC_PEIM_HC_SLOT *Slot,
2601 IN UINT32 Rca,
2602 IN UINT32 ClockFreq
2603 )
2604 {
2605 EFI_STATUS Status;
2606 UINT8 HsTiming;
2607 UINT8 HostCtrl2;
2608
2609 Status = EmmcPeimSwitchToHS200 (Slot, Rca, ClockFreq, 8);
2610 if (EFI_ERROR (Status)) {
2611 return Status;
2612 }
2613 //
2614 // Set to Hight Speed timing and set the clock frequency to a value less than 52MHz.
2615 //
2616 HsTiming = 1;
2617 Status = EmmcPeimSwitchClockFreq (Slot, Rca, HsTiming, 52);
2618 if (EFI_ERROR (Status)) {
2619 return Status;
2620 }
2621 //
2622 // HS400 mode must use 8 data lines.
2623 //
2624 Status = EmmcPeimSwitchBusWidth (Slot, Rca, TRUE, 8);
2625 if (EFI_ERROR (Status)) {
2626 return Status;
2627 }
2628 //
2629 // Set to HS400 timing
2630 //
2631 HostCtrl2 = (UINT8)~0x7;
2632 Status = EmmcPeimHcAndMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2633 if (EFI_ERROR (Status)) {
2634 return Status;
2635 }
2636 HostCtrl2 = BIT0 | BIT2;
2637 Status = EmmcPeimHcOrMmio (Slot->EmmcHcBase + EMMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2638 if (EFI_ERROR (Status)) {
2639 return Status;
2640 }
2641
2642 HsTiming = 3;
2643 Status = EmmcPeimSwitchClockFreq (Slot, Rca, HsTiming, ClockFreq);
2644
2645 return Status;
2646 }
2647
2648 /**
2649 Switch the high speed timing according to request.
2650
2651 Refer to EMMC Electrical Standard Spec 5.1 Section 6.6.8 and SD Host Controller
2652 Simplified Spec 3.0 section Figure 2-29 for details.
2653
2654 @param[in] Slot The slot number of the Emmc card to send the command to.
2655 @param[in] Rca The relative device address to be assigned.
2656
2657 @retval EFI_SUCCESS The operation is done correctly.
2658 @retval Others The operation fails.
2659
2660 **/
2661 EFI_STATUS
2662 EmmcPeimSetBusMode (
2663 IN EMMC_PEIM_HC_SLOT *Slot,
2664 IN UINT32 Rca
2665 )
2666 {
2667 EFI_STATUS Status;
2668 EMMC_HC_SLOT_CAP Capability;
2669 UINT8 HsTiming;
2670 BOOLEAN IsDdr;
2671 UINT32 ClockFreq;
2672 UINT8 BusWidth;
2673
2674 Status = EmmcPeimGetCsd (Slot, Rca, &Slot->Csd);
2675 if (EFI_ERROR (Status)) {
2676 DEBUG ((EFI_D_ERROR, "EmmcPeimSetBusMode: EmmcPeimGetCsd fails with %r\n", Status));
2677 return Status;
2678 }
2679
2680 if ((Slot->Csd.CSizeLow | Slot->Csd.CSizeHigh << 2) == 0xFFF) {
2681 Slot->SectorAddressing = TRUE;
2682 } else {
2683 Slot->SectorAddressing = FALSE;
2684 }
2685
2686 Status = EmmcPeimSelect (Slot, Rca);
2687 if (EFI_ERROR (Status)) {
2688 DEBUG ((EFI_D_ERROR, "EmmcPeimSetBusMode: EmmcPeimSelect fails with %r\n", Status));
2689 return Status;
2690 }
2691
2692 Status = EmmcPeimHcGetCapability (Slot->EmmcHcBase, &Capability);
2693 if (EFI_ERROR (Status)) {
2694 DEBUG ((EFI_D_ERROR, "EmmcPeimSetBusMode: EmmcPeimHcGetCapability fails with %r\n", Status));
2695 return Status;
2696 }
2697
2698 ASSERT (Capability.BaseClkFreq != 0);
2699 //
2700 // Check if the Host Controller support 8bits bus width.
2701 //
2702 if (Capability.BusWidth8 != 0) {
2703 BusWidth = 8;
2704 } else {
2705 BusWidth = 4;
2706 }
2707 //
2708 // Get Deivce_Type from EXT_CSD register.
2709 //
2710 Status = EmmcPeimGetExtCsd (Slot, &Slot->ExtCsd);
2711 if (EFI_ERROR (Status)) {
2712 DEBUG ((EFI_D_ERROR, "EmmcPeimSetBusMode: EmmcPeimGetExtCsd fails with %r\n", Status));
2713 return Status;
2714 }
2715 //
2716 // Calculate supported bus speed/bus width/clock frequency.
2717 //
2718 HsTiming = 0;
2719 IsDdr = FALSE;
2720 ClockFreq = 0;
2721 if (((Slot->ExtCsd.DeviceType & (BIT4 | BIT5)) != 0) && (Capability.Sdr104 != 0)) {
2722 HsTiming = 2;
2723 IsDdr = FALSE;
2724 ClockFreq = 200;
2725 } else if (((Slot->ExtCsd.DeviceType & (BIT2 | BIT3)) != 0) && (Capability.Ddr50 != 0)) {
2726 HsTiming = 1;
2727 IsDdr = TRUE;
2728 ClockFreq = 52;
2729 } else if (((Slot->ExtCsd.DeviceType & BIT1) != 0) && (Capability.HighSpeed != 0)) {
2730 HsTiming = 1;
2731 IsDdr = FALSE;
2732 ClockFreq = 52;
2733 } else if (((Slot->ExtCsd.DeviceType & BIT0) != 0) && (Capability.HighSpeed != 0)) {
2734 HsTiming = 1;
2735 IsDdr = FALSE;
2736 ClockFreq = 26;
2737 }
2738 //
2739 // Check if both of the device and the host controller support HS400 DDR mode.
2740 //
2741 if (((Slot->ExtCsd.DeviceType & (BIT6 | BIT7)) != 0) && (Capability.Hs400 != 0)) {
2742 //
2743 // The host controller supports 8bits bus.
2744 //
2745 ASSERT (BusWidth == 8);
2746 HsTiming = 3;
2747 IsDdr = TRUE;
2748 ClockFreq = 200;
2749 }
2750
2751 if ((ClockFreq == 0) || (HsTiming == 0)) {
2752 //
2753 // Continue using default setting.
2754 //
2755 return EFI_SUCCESS;
2756 }
2757
2758 DEBUG ((EFI_D_INFO, "HsTiming %d ClockFreq %d BusWidth %d Ddr %a\n", HsTiming, ClockFreq, BusWidth, IsDdr ? "TRUE":"FALSE"));
2759
2760 if (HsTiming == 3) {
2761 //
2762 // Execute HS400 timing switch procedure
2763 //
2764 Status = EmmcPeimSwitchToHS400 (Slot, Rca, ClockFreq);
2765 } else if (HsTiming == 2) {
2766 //
2767 // Execute HS200 timing switch procedure
2768 //
2769 Status = EmmcPeimSwitchToHS200 (Slot, Rca, ClockFreq, BusWidth);
2770 } else {
2771 //
2772 // Execute High Speed timing switch procedure
2773 //
2774 Status = EmmcPeimSwitchToHighSpeed (Slot, Rca, ClockFreq, BusWidth, IsDdr);
2775 }
2776
2777 return Status;
2778 }
2779
2780 /**
2781 Execute EMMC device identification procedure.
2782
2783 Refer to EMMC Electrical Standard Spec 5.1 Section 6.4 for details.
2784
2785 @param[in] Slot The slot number of the Emmc card to send the command to.
2786
2787 @retval EFI_SUCCESS There is a EMMC card.
2788 @retval Others There is not a EMMC card.
2789
2790 **/
2791 EFI_STATUS
2792 EmmcPeimIdentification (
2793 IN EMMC_PEIM_HC_SLOT *Slot
2794 )
2795 {
2796 EFI_STATUS Status;
2797 UINT32 Ocr;
2798 UINT32 Rca;
2799
2800 Status = EmmcPeimReset (Slot);
2801 if (EFI_ERROR (Status)) {
2802 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimReset fails with %r\n", Status));
2803 return Status;
2804 }
2805
2806 Ocr = 0;
2807 do {
2808 Status = EmmcPeimGetOcr (Slot, &Ocr);
2809 if (EFI_ERROR (Status)) {
2810 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimGetOcr fails with %r\n", Status));
2811 return Status;
2812 }
2813 } while ((Ocr & BIT31) == 0);
2814
2815 Status = EmmcPeimGetAllCid (Slot);
2816 if (EFI_ERROR (Status)) {
2817 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimGetAllCid fails with %r\n", Status));
2818 return Status;
2819 }
2820 //
2821 // Don't support multiple devices on the slot, that is
2822 // shared bus slot feature.
2823 //
2824 Rca = 1;
2825 Status = EmmcPeimSetRca (Slot, Rca);
2826 if (EFI_ERROR (Status)) {
2827 DEBUG ((EFI_D_ERROR, "EmmcPeimIdentification: EmmcPeimSetRca fails with %r\n", Status));
2828 return Status;
2829 }
2830 //
2831 // Enter Data Tranfer Mode.
2832 //
2833 DEBUG ((EFI_D_INFO, "Found a EMMC device at slot [%d], RCA [%d]\n", Slot, Rca));
2834
2835 Status = EmmcPeimSetBusMode (Slot, Rca);
2836
2837 return Status;
2838 }
2839