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