]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Sd/SdBlockIoPei/SdHci.c
MdeModulePkg/SdMmc: Add break to avoid dead loop when polling OCR Reg
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / SdBlockIoPei / SdHci.c
1 /** @file
2
3 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php.
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 #include "SdBlockIoPei.h"
15
16 /**
17 Read/Write specified SD 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 SdPeimHcRwMmio (
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 SD 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 SdPeimHcOrMmio (
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 = SdPeimHcRwMmio (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 = SdPeimHcRwMmio (Address, FALSE, Count, &Data);
135
136 return Status;
137 }
138
139 /**
140 Do AND operation with the value of the specified SD 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 SdPeimHcAndMmio (
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 = SdPeimHcRwMmio (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 = SdPeimHcRwMmio (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 SdPeimHcCheckMmioSet (
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 = SdPeimHcRwMmio (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 SdPeimHcWaitMmioSet (
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 = SdPeimHcCheckMmioSet (
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 SD 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 SdPeimHcReset (
303 IN UINTN Bar
304 )
305 {
306 EFI_STATUS Status;
307 UINT8 SwReset;
308
309 SwReset = 0xFF;
310 Status = SdPeimHcRwMmio (Bar + SD_HC_SW_RST, FALSE, sizeof (SwReset), &SwReset);
311
312 if (EFI_ERROR (Status)) {
313 DEBUG ((EFI_D_ERROR, "SdPeimHcReset: write full 1 fails: %r\n", Status));
314 return Status;
315 }
316
317 Status = SdPeimHcWaitMmioSet (
318 Bar + SD_HC_SW_RST,
319 sizeof (SwReset),
320 0xFF,
321 0x00,
322 SD_TIMEOUT
323 );
324 if (EFI_ERROR (Status)) {
325 DEBUG ((EFI_D_INFO, "SdPeimHcReset: reset done with %r\n", Status));
326 return Status;
327 }
328 //
329 // Enable all interrupt after reset all.
330 //
331 Status = SdPeimHcEnableInterrupt (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 SdPeimHcEnableInterrupt (
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 = SdPeimHcRwMmio (Bar + SD_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 = SdPeimHcRwMmio (Bar + SD_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 SdPeimHcGetCapability (
383 IN UINTN Bar,
384 OUT SD_HC_SLOT_CAP *Capability
385 )
386 {
387 EFI_STATUS Status;
388 UINT64 Cap;
389
390 Status = SdPeimHcRwMmio (Bar + SD_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 SD card attached at the specified SD 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 SD card attached.
409 @retval EFI_NO_MEDIA There is not a SD card attached.
410 @retval Others The detection fails.
411
412 **/
413 EFI_STATUS
414 SdPeimHcCardDetect (
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 = SdPeimHcRwMmio (Bar + SD_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 = SdPeimHcRwMmio (Bar + SD_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 = SdPeimHcRwMmio (Bar + SD_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 SD 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 SD clock.
464 @retval Others Fail to stop SD clock.
465
466 **/
467 EFI_STATUS
468 SdPeimHcStopClock (
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 = SdPeimHcWaitMmioSet (
482 Bar + SD_HC_PRESENT_STATE,
483 sizeof (PresentState),
484 BIT0 | BIT1,
485 0,
486 SD_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 = SdPeimHcAndMmio (Bar + SD_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);
497
498 return Status;
499 }
500
501 /**
502 SD 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 SdPeimHcClockSupply (
515 IN UINTN Bar,
516 IN UINT64 ClockFreq
517 )
518 {
519 EFI_STATUS Status;
520 SD_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 = SdPeimHcGetCapability (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 = SdPeimHcRwMmio (Bar + SD_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 = SdPeimHcStopClock (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 = SdPeimHcRwMmio (Bar + SD_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 = SdPeimHcWaitMmioSet (
613 Bar + SD_HC_CLOCK_CTRL,
614 sizeof (ClockCtrl),
615 BIT1,
616 BIT1,
617 SD_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 = SdPeimHcOrMmio (Bar + SD_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);
628
629 return Status;
630 }
631
632 /**
633 SD 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 SD card attached.
641 @retval FALSE There is no a SD card attached.
642
643 **/
644 EFI_STATUS
645 SdPeimHcPowerControl (
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 = SdPeimHcRwMmio (Bar + SD_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 = SdPeimHcRwMmio (Bar + SD_HC_POWER_CTRL, FALSE, sizeof (PowerCtrl), &PowerCtrl);
666
667 return Status;
668 }
669
670 /**
671 Set the SD 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 SD 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 SdPeimHcSetBusWidth (
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 = SdPeimHcAndMmio (Bar + SD_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
694 } else if (BusWidth == 4) {
695 Status = SdPeimHcRwMmio (Bar + SD_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 = SdPeimHcRwMmio (Bar + SD_HC_HOST_CTRL1, FALSE, sizeof (HostCtrl1), &HostCtrl1);
702 } else if (BusWidth == 8) {
703 Status = SdPeimHcRwMmio (Bar + SD_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 = SdPeimHcRwMmio (Bar + SD_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 SD 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 SdPeimHcInitClockFreq (
729 IN UINTN Bar
730 )
731 {
732 EFI_STATUS Status;
733 SD_HC_SLOT_CAP Capability;
734 UINT32 InitFreq;
735
736 //
737 // Calculate a divisor for SD clock frequency
738 //
739 Status = SdPeimHcGetCapability (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 = SdPeimHcClockSupply (Bar, InitFreq);
755 return Status;
756 }
757
758 /**
759 Supply SD 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 SdPeimHcInitPowerVoltage (
771 IN UINTN Bar
772 )
773 {
774 EFI_STATUS Status;
775 SD_HC_SLOT_CAP Capability;
776 UINT8 MaxVoltage;
777 UINT8 HostCtrl2;
778
779 //
780 // Get the support voltage of the Host Controller
781 //
782 Status = SdPeimHcGetCapability (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 = SdPeimHcOrMmio (Bar + SD_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 = SdPeimHcPowerControl (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 SdPeimHcInitTimeoutCtrl (
836 IN UINTN Bar
837 )
838 {
839 EFI_STATUS Status;
840 UINT8 Timeout;
841
842 Timeout = 0x0E;
843 Status = SdPeimHcRwMmio (Bar + SD_HC_TIMEOUT_CTRL, FALSE, sizeof (Timeout), &Timeout);
844
845 return Status;
846 }
847
848 /**
849 Initial SD 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 SdPeimHcInitHost (
860 IN UINTN Bar
861 )
862 {
863 EFI_STATUS Status;
864
865 Status = SdPeimHcInitClockFreq (Bar);
866 if (EFI_ERROR (Status)) {
867 return Status;
868 }
869
870 Status = SdPeimHcInitPowerVoltage (Bar);
871 if (EFI_ERROR (Status)) {
872 return Status;
873 }
874
875 Status = SdPeimHcInitTimeoutCtrl (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 SdPeimHcLedOnOff (
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 = SdPeimHcOrMmio (Bar + SD_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
901 } else {
902 HostCtrl1 = (UINT8)~BIT0;
903 Status = SdPeimHcAndMmio (Bar + SD_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 SD_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 SD_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 (SD_HC_ADMA_DESC_LINE));
951 Trb->AdmaDesc = SdPeimAllocateMem (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 SD cmd request.
985
986 @param[in] Slot The slot number of the SD 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 SD_TRB *
993 SdPeimCreateTrb (
994 IN SD_PEIM_HC_SLOT *Slot,
995 IN SD_COMMAND_PACKET *Packet
996 )
997 {
998 SD_TRB *Trb;
999 EFI_STATUS Status;
1000 SD_HC_SLOT_CAP Capability;
1001
1002 //
1003 // Calculate a divisor for SD clock frequency
1004 //
1005 Status = SdPeimHcGetCapability (Slot->SdHcBase, &Capability);
1006 if (EFI_ERROR (Status)) {
1007 return NULL;
1008 }
1009
1010 Trb = SdPeimAllocateMem (Slot->Private->Pool, sizeof (SD_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 != 0) && (Trb->DataLen < Trb->BlockSize)) {
1036 Trb->BlockSize = (UINT16)Trb->DataLen;
1037 }
1038
1039 if (Packet->SdCmdBlk->CommandIndex == SD_SEND_TUNING_BLOCK) {
1040 Trb->Mode = SdPioMode;
1041 } else {
1042 if (Trb->DataLen == 0) {
1043 Trb->Mode = SdNoData;
1044 } else if (Capability.Adma2 != 0) {
1045 Trb->Mode = SdAdmaMode;
1046 Status = BuildAdmaDescTable (Trb);
1047 if (EFI_ERROR (Status)) {
1048 goto Error;
1049 }
1050 } else if (Capability.Sdma != 0) {
1051 Trb->Mode = SdSdmaMode;
1052 } else {
1053 Trb->Mode = SdPioMode;
1054 }
1055 }
1056 return Trb;
1057
1058 Error:
1059 SdPeimFreeTrb (Trb);
1060 return NULL;
1061 }
1062
1063 /**
1064 Free the resource used by the TRB.
1065
1066 @param[in] Trb The pointer to the SD_TRB instance.
1067
1068 **/
1069 VOID
1070 SdPeimFreeTrb (
1071 IN SD_TRB *Trb
1072 )
1073 {
1074 if ((Trb != NULL) && (Trb->AdmaDesc != NULL)) {
1075 SdPeimFreeMem (Trb->Slot->Private->Pool, Trb->AdmaDesc, Trb->AdmaDescSize);
1076 }
1077
1078 if (Trb != NULL) {
1079 SdPeimFreeMem (Trb->Slot->Private->Pool, Trb, sizeof (SD_TRB));
1080 }
1081 return;
1082 }
1083
1084 /**
1085 Check if the env is ready for execute specified TRB.
1086
1087 @param[in] Bar The mmio base address of the slot to be accessed.
1088 @param[in] Trb The pointer to the SD_TRB instance.
1089
1090 @retval EFI_SUCCESS The env is ready for TRB execution.
1091 @retval EFI_NOT_READY The env is not ready for TRB execution.
1092 @retval Others Some erros happen.
1093
1094 **/
1095 EFI_STATUS
1096 SdPeimCheckTrbEnv (
1097 IN UINTN Bar,
1098 IN SD_TRB *Trb
1099 )
1100 {
1101 EFI_STATUS Status;
1102 SD_COMMAND_PACKET *Packet;
1103 UINT32 PresentState;
1104
1105 Packet = Trb->Packet;
1106
1107 if ((Packet->SdCmdBlk->CommandType == SdCommandTypeAdtc) ||
1108 (Packet->SdCmdBlk->ResponseType == SdResponseTypeR1b) ||
1109 (Packet->SdCmdBlk->ResponseType == SdResponseTypeR5b)) {
1110 //
1111 // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in
1112 // the Present State register to be 0
1113 //
1114 PresentState = BIT0 | BIT1;
1115 } else {
1116 //
1117 // Wait Command Inhibit (CMD) in the Present State register
1118 // to be 0
1119 //
1120 PresentState = BIT0;
1121 }
1122
1123 Status = SdPeimHcCheckMmioSet (
1124 Bar + SD_HC_PRESENT_STATE,
1125 sizeof (PresentState),
1126 PresentState,
1127 0
1128 );
1129
1130 return Status;
1131 }
1132
1133 /**
1134 Wait for the env to be ready for execute specified TRB.
1135
1136 @param[in] Bar The mmio base address of the slot to be accessed.
1137 @param[in] Trb The pointer to the SD_TRB instance.
1138
1139 @retval EFI_SUCCESS The env is ready for TRB execution.
1140 @retval EFI_TIMEOUT The env is not ready for TRB execution in time.
1141 @retval Others Some erros happen.
1142
1143 **/
1144 EFI_STATUS
1145 SdPeimWaitTrbEnv (
1146 IN UINTN Bar,
1147 IN SD_TRB *Trb
1148 )
1149 {
1150 EFI_STATUS Status;
1151 SD_COMMAND_PACKET *Packet;
1152 UINT64 Timeout;
1153 BOOLEAN InfiniteWait;
1154
1155 //
1156 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1157 //
1158 Packet = Trb->Packet;
1159 Timeout = Packet->Timeout;
1160 if (Timeout == 0) {
1161 InfiniteWait = TRUE;
1162 } else {
1163 InfiniteWait = FALSE;
1164 }
1165
1166 while (InfiniteWait || (Timeout > 0)) {
1167 //
1168 // Check Trb execution result by reading Normal Interrupt Status register.
1169 //
1170 Status = SdPeimCheckTrbEnv (Bar, Trb);
1171 if (Status != EFI_NOT_READY) {
1172 return Status;
1173 }
1174 //
1175 // Stall for 1 microsecond.
1176 //
1177 MicroSecondDelay (1);
1178
1179 Timeout--;
1180 }
1181
1182 return EFI_TIMEOUT;
1183 }
1184
1185 /**
1186 Execute the specified TRB.
1187
1188 @param[in] Bar The mmio base address of the slot to be accessed.
1189 @param[in] Trb The pointer to the SD_TRB instance.
1190
1191 @retval EFI_SUCCESS The TRB is sent to host controller successfully.
1192 @retval Others Some erros happen when sending this request to the host controller.
1193
1194 **/
1195 EFI_STATUS
1196 SdPeimExecTrb (
1197 IN UINTN Bar,
1198 IN SD_TRB *Trb
1199 )
1200 {
1201 EFI_STATUS Status;
1202 SD_COMMAND_PACKET *Packet;
1203 UINT16 Cmd;
1204 UINT16 IntStatus;
1205 UINT32 Argument;
1206 UINT16 BlkCount;
1207 UINT16 BlkSize;
1208 UINT16 TransMode;
1209 UINT8 HostCtrl1;
1210 UINT32 SdmaAddr;
1211 UINT64 AdmaAddr;
1212
1213 Packet = Trb->Packet;
1214 //
1215 // Clear all bits in Error Interrupt Status Register
1216 //
1217 IntStatus = 0xFFFF;
1218 Status = SdPeimHcRwMmio (Bar + SD_HC_ERR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);
1219 if (EFI_ERROR (Status)) {
1220 return Status;
1221 }
1222 //
1223 // Clear all bits in Normal Interrupt Status Register
1224 //
1225 IntStatus = 0xFFFF;
1226 Status = SdPeimHcRwMmio (Bar + SD_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);
1227 if (EFI_ERROR (Status)) {
1228 return Status;
1229 }
1230 //
1231 // Set Host Control 1 register DMA Select field
1232 //
1233 if (Trb->Mode == SdAdmaMode) {
1234 HostCtrl1 = BIT4;
1235 Status = SdPeimHcOrMmio (Bar + SD_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
1236 if (EFI_ERROR (Status)) {
1237 return Status;
1238 }
1239 }
1240
1241 SdPeimHcLedOnOff (Bar, TRUE);
1242
1243 if (Trb->Mode == SdSdmaMode) {
1244 if ((UINT64)(UINTN)Trb->Data >= 0x100000000ul) {
1245 return EFI_INVALID_PARAMETER;
1246 }
1247
1248 SdmaAddr = (UINT32)(UINTN)Trb->Data;
1249 Status = SdPeimHcRwMmio (Bar + SD_HC_SDMA_ADDR, FALSE, sizeof (SdmaAddr), &SdmaAddr);
1250 if (EFI_ERROR (Status)) {
1251 return Status;
1252 }
1253 } else if (Trb->Mode == SdAdmaMode) {
1254 AdmaAddr = (UINT64)(UINTN)Trb->AdmaDesc;
1255 Status = SdPeimHcRwMmio (Bar + SD_HC_ADMA_SYS_ADDR, FALSE, sizeof (AdmaAddr), &AdmaAddr);
1256 if (EFI_ERROR (Status)) {
1257 return Status;
1258 }
1259 }
1260
1261 BlkSize = Trb->BlockSize;
1262 if (Trb->Mode == SdSdmaMode) {
1263 //
1264 // Set SDMA boundary to be 512K bytes.
1265 //
1266 BlkSize |= 0x7000;
1267 }
1268
1269 Status = SdPeimHcRwMmio (Bar + SD_HC_BLK_SIZE, FALSE, sizeof (BlkSize), &BlkSize);
1270 if (EFI_ERROR (Status)) {
1271 return Status;
1272 }
1273
1274 BlkCount = 0;
1275 if (Trb->Mode != SdNoData) {
1276 //
1277 // Calcuate Block Count.
1278 //
1279 BlkCount = (UINT16)(Trb->DataLen / Trb->BlockSize);
1280 }
1281 Status = SdPeimHcRwMmio (Bar + SD_HC_BLK_COUNT, FALSE, sizeof (BlkCount), &BlkCount);
1282 if (EFI_ERROR (Status)) {
1283 return Status;
1284 }
1285
1286 Argument = Packet->SdCmdBlk->CommandArgument;
1287 Status = SdPeimHcRwMmio (Bar + SD_HC_ARG1, FALSE, sizeof (Argument), &Argument);
1288 if (EFI_ERROR (Status)) {
1289 return Status;
1290 }
1291
1292 TransMode = 0;
1293 if (Trb->Mode != SdNoData) {
1294 if (Trb->Mode != SdPioMode) {
1295 TransMode |= BIT0;
1296 }
1297 if (Trb->Read) {
1298 TransMode |= BIT4;
1299 }
1300 if (BlkCount > 1) {
1301 TransMode |= BIT5 | BIT1;
1302 }
1303 //
1304 // SD memory card needs to use AUTO CMD12 feature.
1305 //
1306 if (BlkCount > 1) {
1307 TransMode |= BIT2;
1308 }
1309 }
1310
1311 Status = SdPeimHcRwMmio (Bar + SD_HC_TRANS_MOD, FALSE, sizeof (TransMode), &TransMode);
1312 if (EFI_ERROR (Status)) {
1313 return Status;
1314 }
1315
1316 Cmd = (UINT16)LShiftU64(Packet->SdCmdBlk->CommandIndex, 8);
1317 if (Packet->SdCmdBlk->CommandType == SdCommandTypeAdtc) {
1318 Cmd |= BIT5;
1319 }
1320 //
1321 // Convert ResponseType to value
1322 //
1323 if (Packet->SdCmdBlk->CommandType != SdCommandTypeBc) {
1324 switch (Packet->SdCmdBlk->ResponseType) {
1325 case SdResponseTypeR1:
1326 case SdResponseTypeR5:
1327 case SdResponseTypeR6:
1328 case SdResponseTypeR7:
1329 Cmd |= (BIT1 | BIT3 | BIT4);
1330 break;
1331 case SdResponseTypeR2:
1332 Cmd |= (BIT0 | BIT3);
1333 break;
1334 case SdResponseTypeR3:
1335 case SdResponseTypeR4:
1336 Cmd |= BIT1;
1337 break;
1338 case SdResponseTypeR1b:
1339 case SdResponseTypeR5b:
1340 Cmd |= (BIT0 | BIT1 | BIT3 | BIT4);
1341 break;
1342 default:
1343 ASSERT (FALSE);
1344 break;
1345 }
1346 }
1347 //
1348 // Execute cmd
1349 //
1350 Status = SdPeimHcRwMmio (Bar + SD_HC_COMMAND, FALSE, sizeof (Cmd), &Cmd);
1351 return Status;
1352 }
1353
1354 /**
1355 Check the TRB execution result.
1356
1357 @param[in] Bar The mmio base address of the slot to be accessed.
1358 @param[in] Trb The pointer to the SD_TRB instance.
1359
1360 @retval EFI_SUCCESS The TRB is executed successfully.
1361 @retval EFI_NOT_READY The TRB is not completed for execution.
1362 @retval Others Some erros happen when executing this request.
1363
1364 **/
1365 EFI_STATUS
1366 SdPeimCheckTrbResult (
1367 IN UINTN Bar,
1368 IN SD_TRB *Trb
1369 )
1370 {
1371 EFI_STATUS Status;
1372 SD_COMMAND_PACKET *Packet;
1373 UINT16 IntStatus;
1374 UINT32 Response[4];
1375 UINT32 SdmaAddr;
1376 UINT8 Index;
1377 UINT8 SwReset;
1378 UINT32 PioLength;
1379
1380 SwReset = 0;
1381 Packet = Trb->Packet;
1382 //
1383 // Check Trb execution result by reading Normal Interrupt Status register.
1384 //
1385 Status = SdPeimHcRwMmio (
1386 Bar + SD_HC_NOR_INT_STS,
1387 TRUE,
1388 sizeof (IntStatus),
1389 &IntStatus
1390 );
1391 if (EFI_ERROR (Status)) {
1392 goto Done;
1393 }
1394 //
1395 // Check Transfer Complete bit is set or not.
1396 //
1397 if ((IntStatus & BIT1) == BIT1) {
1398 if ((IntStatus & BIT15) == BIT15) {
1399 //
1400 // Read Error Interrupt Status register to check if the error is
1401 // Data Timeout Error.
1402 // If yes, treat it as success as Transfer Complete has higher
1403 // priority than Data Timeout Error.
1404 //
1405 Status = SdPeimHcRwMmio (
1406 Bar + SD_HC_ERR_INT_STS,
1407 TRUE,
1408 sizeof (IntStatus),
1409 &IntStatus
1410 );
1411 if (!EFI_ERROR (Status)) {
1412 if ((IntStatus & BIT4) == BIT4) {
1413 Status = EFI_SUCCESS;
1414 } else {
1415 Status = EFI_DEVICE_ERROR;
1416 }
1417 }
1418 }
1419
1420 goto Done;
1421 }
1422 //
1423 // Check if there is a error happened during cmd execution.
1424 // If yes, then do error recovery procedure to follow SD Host Controller
1425 // Simplified Spec 3.0 section 3.10.1.
1426 //
1427 if ((IntStatus & BIT15) == BIT15) {
1428 Status = SdPeimHcRwMmio (
1429 Bar + SD_HC_ERR_INT_STS,
1430 TRUE,
1431 sizeof (IntStatus),
1432 &IntStatus
1433 );
1434 if (EFI_ERROR (Status)) {
1435 goto Done;
1436 }
1437
1438 if ((IntStatus & 0x0F) != 0) {
1439 SwReset |= BIT1;
1440 }
1441 if ((IntStatus & 0xF0) != 0) {
1442 SwReset |= BIT2;
1443 }
1444
1445 Status = SdPeimHcRwMmio (
1446 Bar + SD_HC_SW_RST,
1447 FALSE,
1448 sizeof (SwReset),
1449 &SwReset
1450 );
1451 if (EFI_ERROR (Status)) {
1452 goto Done;
1453 }
1454 Status = SdPeimHcWaitMmioSet (
1455 Bar + SD_HC_SW_RST,
1456 sizeof (SwReset),
1457 0xFF,
1458 0,
1459 SD_TIMEOUT
1460 );
1461 if (EFI_ERROR (Status)) {
1462 goto Done;
1463 }
1464
1465 Status = EFI_DEVICE_ERROR;
1466 goto Done;
1467 }
1468 //
1469 // Check if DMA interrupt is signalled for the SDMA transfer.
1470 //
1471 if ((Trb->Mode == SdSdmaMode) && ((IntStatus & BIT3) == BIT3)) {
1472 //
1473 // Clear DMA interrupt bit.
1474 //
1475 IntStatus = BIT3;
1476 Status = SdPeimHcRwMmio (
1477 Bar + SD_HC_NOR_INT_STS,
1478 FALSE,
1479 sizeof (IntStatus),
1480 &IntStatus
1481 );
1482 if (EFI_ERROR (Status)) {
1483 goto Done;
1484 }
1485 //
1486 // Update SDMA Address register.
1487 //
1488 SdmaAddr = SD_SDMA_ROUND_UP ((UINT32)(UINTN)Trb->Data, SD_SDMA_BOUNDARY);
1489 Status = SdPeimHcRwMmio (
1490 Bar + SD_HC_SDMA_ADDR,
1491 FALSE,
1492 sizeof (UINT32),
1493 &SdmaAddr
1494 );
1495 if (EFI_ERROR (Status)) {
1496 goto Done;
1497 }
1498 Trb->Data = (VOID*)(UINTN)SdmaAddr;
1499 }
1500
1501 if ((Packet->SdCmdBlk->CommandType != SdCommandTypeAdtc) &&
1502 (Packet->SdCmdBlk->ResponseType != SdResponseTypeR1b) &&
1503 (Packet->SdCmdBlk->ResponseType != SdResponseTypeR5b)) {
1504 if ((IntStatus & BIT0) == BIT0) {
1505 Status = EFI_SUCCESS;
1506 goto Done;
1507 }
1508 }
1509
1510 if (Packet->SdCmdBlk->CommandIndex == SD_SEND_TUNING_BLOCK) {
1511 //
1512 // When performing tuning procedure (Execute Tuning is set to 1) through PIO mode,
1513 // wait Buffer Read Ready bit of Normal Interrupt Status Register to be 1.
1514 // Refer to SD Host Controller Simplified Specification 3.0 figure 2-29 for details.
1515 //
1516 if ((IntStatus & BIT5) == BIT5) {
1517 //
1518 // Clear Buffer Read Ready interrupt at first.
1519 //
1520 IntStatus = BIT5;
1521 SdPeimHcRwMmio (Bar + SD_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);
1522 //
1523 // Read data out from Buffer Port register
1524 //
1525 for (PioLength = 0; PioLength < Trb->DataLen; PioLength += 4) {
1526 SdPeimHcRwMmio (Bar + SD_HC_BUF_DAT_PORT, TRUE, 4, (UINT8*)Trb->Data + PioLength);
1527 }
1528 Status = EFI_SUCCESS;
1529 goto Done;
1530 }
1531 }
1532
1533 Status = EFI_NOT_READY;
1534 Done:
1535 //
1536 // Get response data when the cmd is executed successfully.
1537 //
1538 if (!EFI_ERROR (Status)) {
1539 if (Packet->SdCmdBlk->CommandType != SdCommandTypeBc) {
1540 for (Index = 0; Index < 4; Index++) {
1541 Status = SdPeimHcRwMmio (
1542 Bar + SD_HC_RESPONSE + Index * 4,
1543 TRUE,
1544 sizeof (UINT32),
1545 &Response[Index]
1546 );
1547 if (EFI_ERROR (Status)) {
1548 SdPeimHcLedOnOff (Bar, FALSE);
1549 return Status;
1550 }
1551 }
1552 CopyMem (Packet->SdStatusBlk, Response, sizeof (Response));
1553 }
1554 }
1555
1556 if (Status != EFI_NOT_READY) {
1557 SdPeimHcLedOnOff (Bar, FALSE);
1558 }
1559
1560 return Status;
1561 }
1562
1563 /**
1564 Wait for the TRB execution result.
1565
1566 @param[in] Bar The mmio base address of the slot to be accessed.
1567 @param[in] Trb The pointer to the SD_TRB instance.
1568
1569 @retval EFI_SUCCESS The TRB is executed successfully.
1570 @retval Others Some erros happen when executing this request.
1571
1572 **/
1573 EFI_STATUS
1574 SdPeimWaitTrbResult (
1575 IN UINTN Bar,
1576 IN SD_TRB *Trb
1577 )
1578 {
1579 EFI_STATUS Status;
1580 SD_COMMAND_PACKET *Packet;
1581 UINT64 Timeout;
1582 BOOLEAN InfiniteWait;
1583
1584 Packet = Trb->Packet;
1585 //
1586 // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register
1587 //
1588 Timeout = Packet->Timeout;
1589 if (Timeout == 0) {
1590 InfiniteWait = TRUE;
1591 } else {
1592 InfiniteWait = FALSE;
1593 }
1594
1595 while (InfiniteWait || (Timeout > 0)) {
1596 //
1597 // Check Trb execution result by reading Normal Interrupt Status register.
1598 //
1599 Status = SdPeimCheckTrbResult (Bar, Trb);
1600 if (Status != EFI_NOT_READY) {
1601 return Status;
1602 }
1603 //
1604 // Stall for 1 microsecond.
1605 //
1606 MicroSecondDelay (1);
1607
1608 Timeout--;
1609 }
1610
1611 return EFI_TIMEOUT;
1612 }
1613
1614 /**
1615 Sends SD command to an SD card that is attached to the SD controller.
1616
1617 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
1618
1619 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
1620
1621 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
1622 is returned.
1623
1624 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
1625 EFI_INVALID_PARAMETER is returned.
1626
1627 @param[in] Slot The slot number of the Sd card to send the command to.
1628 @param[in,out] Packet A pointer to the SD command data structure.
1629
1630 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
1631 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
1632 command Packet.
1633 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
1634 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
1635 OutDataBuffer are NULL.
1636 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1637 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
1638 supported by the host controller.
1639 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
1640 limit supported by SD card ( i.e. if the number of bytes
1641 exceed the Last LBA).
1642
1643 **/
1644 EFI_STATUS
1645 EFIAPI
1646 SdPeimExecCmd (
1647 IN SD_PEIM_HC_SLOT *Slot,
1648 IN OUT SD_COMMAND_PACKET *Packet
1649 )
1650 {
1651 EFI_STATUS Status;
1652 SD_TRB *Trb;
1653
1654 if (Packet == NULL) {
1655 return EFI_INVALID_PARAMETER;
1656 }
1657
1658 if ((Packet->SdCmdBlk == NULL) || (Packet->SdStatusBlk == NULL)) {
1659 return EFI_INVALID_PARAMETER;
1660 }
1661
1662 if ((Packet->OutDataBuffer == NULL) && (Packet->OutTransferLength != 0)) {
1663 return EFI_INVALID_PARAMETER;
1664 }
1665
1666 if ((Packet->InDataBuffer == NULL) && (Packet->InTransferLength != 0)) {
1667 return EFI_INVALID_PARAMETER;
1668 }
1669
1670 Trb = SdPeimCreateTrb (Slot, Packet);
1671 if (Trb == NULL) {
1672 return EFI_OUT_OF_RESOURCES;
1673 }
1674
1675 Status = SdPeimWaitTrbEnv (Slot->SdHcBase, Trb);
1676 if (EFI_ERROR (Status)) {
1677 goto Done;
1678 }
1679
1680 Status = SdPeimExecTrb (Slot->SdHcBase, Trb);
1681 if (EFI_ERROR (Status)) {
1682 goto Done;
1683 }
1684
1685 Status = SdPeimWaitTrbResult (Slot->SdHcBase, Trb);
1686 if (EFI_ERROR (Status)) {
1687 goto Done;
1688 }
1689
1690 Done:
1691 SdPeimFreeTrb (Trb);
1692
1693 return Status;
1694 }
1695
1696 /**
1697 Send command GO_IDLE_STATE to the device to make it go to Idle State.
1698
1699 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
1700
1701 @param[in] Slot The slot number of the SD card to send the command to.
1702
1703 @retval EFI_SUCCESS The SD device is reset correctly.
1704 @retval Others The device reset fails.
1705
1706 **/
1707 EFI_STATUS
1708 SdPeimReset (
1709 IN SD_PEIM_HC_SLOT *Slot
1710 )
1711 {
1712 SD_COMMAND_BLOCK SdCmdBlk;
1713 SD_STATUS_BLOCK SdStatusBlk;
1714 SD_COMMAND_PACKET Packet;
1715 EFI_STATUS Status;
1716
1717 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
1718 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
1719 ZeroMem (&Packet, sizeof (Packet));
1720
1721 Packet.SdCmdBlk = &SdCmdBlk;
1722 Packet.SdStatusBlk = &SdStatusBlk;
1723 Packet.Timeout = SD_TIMEOUT;
1724
1725 SdCmdBlk.CommandIndex = SD_GO_IDLE_STATE;
1726 SdCmdBlk.CommandType = SdCommandTypeBc;
1727 SdCmdBlk.ResponseType = 0;
1728 SdCmdBlk.CommandArgument = 0;
1729
1730 Status = SdPeimExecCmd (Slot, &Packet);
1731
1732 return Status;
1733 }
1734
1735 /**
1736 Send command SEND_IF_COND to the device to inquiry the SD Memory Card interface
1737 condition.
1738
1739 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
1740
1741 @param[in] Slot The slot number of the SD card to send the command to.
1742 @param[in] SupplyVoltage The supplied voltage by the host.
1743 @param[in] CheckPattern The check pattern to be sent to the device.
1744
1745 @retval EFI_SUCCESS The operation is done correctly.
1746 @retval Others The operation fails.
1747
1748 **/
1749 EFI_STATUS
1750 SdPeimVoltageCheck (
1751 IN SD_PEIM_HC_SLOT *Slot,
1752 IN UINT8 SupplyVoltage,
1753 IN UINT8 CheckPattern
1754 )
1755 {
1756 SD_COMMAND_BLOCK SdCmdBlk;
1757 SD_STATUS_BLOCK SdStatusBlk;
1758 SD_COMMAND_PACKET Packet;
1759 EFI_STATUS Status;
1760
1761 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
1762 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
1763 ZeroMem (&Packet, sizeof (Packet));
1764
1765 Packet.SdCmdBlk = &SdCmdBlk;
1766 Packet.SdStatusBlk = &SdStatusBlk;
1767 Packet.Timeout = SD_TIMEOUT;
1768
1769 SdCmdBlk.CommandIndex = SD_SEND_IF_COND;
1770 SdCmdBlk.CommandType = SdCommandTypeBcr;
1771 SdCmdBlk.ResponseType = SdResponseTypeR7;
1772 SdCmdBlk.CommandArgument = (SupplyVoltage << 8) | CheckPattern;
1773
1774 Status = SdPeimExecCmd (Slot, &Packet);
1775 if (!EFI_ERROR (Status)) {
1776 if (SdStatusBlk.Resp0 != SdCmdBlk.CommandArgument) {
1777 return EFI_DEVICE_ERROR;
1778 }
1779 }
1780
1781 return Status;
1782 }
1783
1784 /**
1785 Send command SDIO_SEND_OP_COND to the device to see whether it is SDIO device.
1786
1787 Refer to SDIO Simplified Spec 3 Section 3.2 for details.
1788
1789 @param[in] Slot The slot number of the SD card to send the command to.
1790 @param[in] VoltageWindow The supply voltage window.
1791 @param[in] S18r The boolean to show if it should switch to 1.8v.
1792
1793 @retval EFI_SUCCESS The operation is done correctly.
1794 @retval Others The operation fails.
1795
1796 **/
1797 EFI_STATUS
1798 SdioSendOpCond (
1799 IN SD_PEIM_HC_SLOT *Slot,
1800 IN UINT32 VoltageWindow,
1801 IN BOOLEAN S18r
1802 )
1803 {
1804 SD_COMMAND_BLOCK SdCmdBlk;
1805 SD_STATUS_BLOCK SdStatusBlk;
1806 SD_COMMAND_PACKET Packet;
1807 EFI_STATUS Status;
1808 UINT32 Switch;
1809
1810 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
1811 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
1812 ZeroMem (&Packet, sizeof (Packet));
1813
1814 Packet.SdCmdBlk = &SdCmdBlk;
1815 Packet.SdStatusBlk = &SdStatusBlk;
1816 Packet.Timeout = SD_TIMEOUT;
1817
1818 SdCmdBlk.CommandIndex = SDIO_SEND_OP_COND;
1819 SdCmdBlk.CommandType = SdCommandTypeBcr;
1820 SdCmdBlk.ResponseType = SdResponseTypeR4;
1821
1822 Switch = S18r ? BIT24 : 0;
1823
1824 SdCmdBlk.CommandArgument = (VoltageWindow & 0xFFFFFF) | Switch;
1825
1826 Status = SdPeimExecCmd (Slot, &Packet);
1827
1828 return Status;
1829 }
1830
1831 /**
1832 Send command SD_SEND_OP_COND to the device to see whether it is SDIO device.
1833
1834 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
1835
1836 @param[in] Slot The slot number of the SD card to send the command to.
1837 @param[in] Rca The relative device address of addressed device.
1838 @param[in] VoltageWindow The supply voltage window.
1839 @param[in] S18r The boolean to show if it should switch to 1.8v.
1840 @param[in] Xpc The boolean to show if it should provide 0.36w power control.
1841 @param[in] Hcs The boolean to show if it support host capacity info.
1842 @param[out] Ocr The buffer to store returned OCR register value.
1843
1844
1845 @retval EFI_SUCCESS The operation is done correctly.
1846 @retval Others The operation fails.
1847
1848 **/
1849 EFI_STATUS
1850 SdPeimSendOpCond (
1851 IN SD_PEIM_HC_SLOT *Slot,
1852 IN UINT16 Rca,
1853 IN UINT32 VoltageWindow,
1854 IN BOOLEAN S18r,
1855 IN BOOLEAN Xpc,
1856 IN BOOLEAN Hcs,
1857 OUT UINT32 *Ocr
1858 )
1859 {
1860 SD_COMMAND_BLOCK SdCmdBlk;
1861 SD_STATUS_BLOCK SdStatusBlk;
1862 SD_COMMAND_PACKET Packet;
1863 EFI_STATUS Status;
1864 UINT32 Switch;
1865 UINT32 MaxPower;
1866 UINT32 HostCapacity;
1867
1868 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
1869 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
1870 ZeroMem (&Packet, sizeof (Packet));
1871
1872 Packet.SdCmdBlk = &SdCmdBlk;
1873 Packet.SdStatusBlk = &SdStatusBlk;
1874 Packet.Timeout = SD_TIMEOUT;
1875
1876 SdCmdBlk.CommandIndex = SD_APP_CMD;
1877 SdCmdBlk.CommandType = SdCommandTypeAc;
1878 SdCmdBlk.ResponseType = SdResponseTypeR1;
1879 SdCmdBlk.CommandArgument = (UINT32)Rca << 16;
1880
1881 Status = SdPeimExecCmd (Slot, &Packet);
1882 if (EFI_ERROR (Status)) {
1883 return Status;
1884 }
1885
1886 SdCmdBlk.CommandIndex = SD_SEND_OP_COND;
1887 SdCmdBlk.CommandType = SdCommandTypeBcr;
1888 SdCmdBlk.ResponseType = SdResponseTypeR3;
1889
1890 Switch = S18r ? BIT24 : 0;
1891 MaxPower = Xpc ? BIT28 : 0;
1892 HostCapacity = Hcs ? BIT30 : 0;
1893 SdCmdBlk.CommandArgument = (VoltageWindow & 0xFFFFFF) | Switch | MaxPower | HostCapacity;
1894
1895 Status = SdPeimExecCmd (Slot, &Packet);
1896 if (!EFI_ERROR (Status)) {
1897 //
1898 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
1899 //
1900 *Ocr = SdStatusBlk.Resp0;
1901 }
1902
1903 return Status;
1904 }
1905
1906 /**
1907 Broadcast command ALL_SEND_CID to the bus to ask all the SD devices to send the
1908 data of their CID registers.
1909
1910 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
1911
1912 @param[in] Slot The slot number of the SD card to send the command to.
1913
1914 @retval EFI_SUCCESS The operation is done correctly.
1915 @retval Others The operation fails.
1916
1917 **/
1918 EFI_STATUS
1919 SdPeimAllSendCid (
1920 IN SD_PEIM_HC_SLOT *Slot
1921 )
1922 {
1923 SD_COMMAND_BLOCK SdCmdBlk;
1924 SD_STATUS_BLOCK SdStatusBlk;
1925 SD_COMMAND_PACKET Packet;
1926 EFI_STATUS Status;
1927
1928 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
1929 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
1930 ZeroMem (&Packet, sizeof (Packet));
1931
1932 Packet.SdCmdBlk = &SdCmdBlk;
1933 Packet.SdStatusBlk = &SdStatusBlk;
1934 Packet.Timeout = SD_TIMEOUT;
1935
1936 SdCmdBlk.CommandIndex = SD_ALL_SEND_CID;
1937 SdCmdBlk.CommandType = SdCommandTypeBcr;
1938 SdCmdBlk.ResponseType = SdResponseTypeR2;
1939 SdCmdBlk.CommandArgument = 0;
1940
1941 Status = SdPeimExecCmd (Slot, &Packet);
1942
1943 return Status;
1944 }
1945
1946 /**
1947 Send command SET_RELATIVE_ADDR to the SD device to assign a Relative device
1948 Address (RCA).
1949
1950 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
1951
1952 @param[in] Slot The slot number of the SD card to send the command to.
1953 @param[out] Rca The relative device address to be assigned.
1954
1955 @retval EFI_SUCCESS The operation is done correctly.
1956 @retval Others The operation fails.
1957
1958 **/
1959 EFI_STATUS
1960 SdPeimSetRca (
1961 IN SD_PEIM_HC_SLOT *Slot,
1962 OUT UINT16 *Rca
1963 )
1964 {
1965 SD_COMMAND_BLOCK SdCmdBlk;
1966 SD_STATUS_BLOCK SdStatusBlk;
1967 SD_COMMAND_PACKET Packet;
1968 EFI_STATUS Status;
1969
1970 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
1971 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
1972 ZeroMem (&Packet, sizeof (Packet));
1973
1974 Packet.SdCmdBlk = &SdCmdBlk;
1975 Packet.SdStatusBlk = &SdStatusBlk;
1976 Packet.Timeout = SD_TIMEOUT;
1977
1978 SdCmdBlk.CommandIndex = SD_SET_RELATIVE_ADDR;
1979 SdCmdBlk.CommandType = SdCommandTypeBcr;
1980 SdCmdBlk.ResponseType = SdResponseTypeR6;
1981
1982 Status = SdPeimExecCmd (Slot, &Packet);
1983 if (!EFI_ERROR (Status)) {
1984 *Rca = (UINT16)(SdStatusBlk.Resp0 >> 16);
1985 }
1986
1987 return Status;
1988 }
1989
1990 /**
1991 Send command SEND_CSD to the SD device to get the data of the CSD register.
1992
1993 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
1994
1995 @param[in] Slot The slot number of the SD card to send the command to.
1996 @param[in] Rca The relative device address of selected device.
1997 @param[out] Csd The buffer to store the content of the CSD register.
1998 Note the caller should ignore the lowest byte of this
1999 buffer as the content of this byte is meaningless even
2000 if the operation succeeds.
2001
2002 @retval EFI_SUCCESS The operation is done correctly.
2003 @retval Others The operation fails.
2004
2005 **/
2006 EFI_STATUS
2007 SdPeimGetCsd (
2008 IN SD_PEIM_HC_SLOT *Slot,
2009 IN UINT16 Rca,
2010 OUT SD_CSD *Csd
2011 )
2012 {
2013 SD_COMMAND_BLOCK SdCmdBlk;
2014 SD_STATUS_BLOCK SdStatusBlk;
2015 SD_COMMAND_PACKET Packet;
2016 EFI_STATUS Status;
2017
2018 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2019 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2020 ZeroMem (&Packet, sizeof (Packet));
2021
2022 Packet.SdCmdBlk = &SdCmdBlk;
2023 Packet.SdStatusBlk = &SdStatusBlk;
2024 Packet.Timeout = SD_TIMEOUT;
2025
2026 SdCmdBlk.CommandIndex = SD_SEND_CSD;
2027 SdCmdBlk.CommandType = SdCommandTypeAc;
2028 SdCmdBlk.ResponseType = SdResponseTypeR2;
2029 SdCmdBlk.CommandArgument = (UINT32)Rca << 16;
2030
2031 Status = SdPeimExecCmd (Slot, &Packet);
2032 if (!EFI_ERROR (Status)) {
2033 //
2034 // For details, refer to SD Host Controller Simplified Spec 3.0 Table 2-12.
2035 //
2036 CopyMem (((UINT8*)Csd) + 1, &SdStatusBlk.Resp0, sizeof (SD_CSD) - 1);
2037 }
2038
2039 return Status;
2040 }
2041
2042 /**
2043 Send command SELECT_DESELECT_CARD to the SD device to select/deselect it.
2044
2045 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
2046
2047 @param[in] Slot The slot number of the SD card to send the command to.
2048 @param[in] Rca The relative device address of selected device.
2049
2050 @retval EFI_SUCCESS The operation is done correctly.
2051 @retval Others The operation fails.
2052
2053 **/
2054 EFI_STATUS
2055 SdPeimSelect (
2056 IN SD_PEIM_HC_SLOT *Slot,
2057 IN UINT16 Rca
2058 )
2059 {
2060 SD_COMMAND_BLOCK SdCmdBlk;
2061 SD_STATUS_BLOCK SdStatusBlk;
2062 SD_COMMAND_PACKET Packet;
2063 EFI_STATUS Status;
2064
2065 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2066 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2067 ZeroMem (&Packet, sizeof (Packet));
2068
2069 Packet.SdCmdBlk = &SdCmdBlk;
2070 Packet.SdStatusBlk = &SdStatusBlk;
2071 Packet.Timeout = SD_TIMEOUT;
2072
2073 SdCmdBlk.CommandIndex = SD_SELECT_DESELECT_CARD;
2074 SdCmdBlk.CommandType = SdCommandTypeAc;
2075 SdCmdBlk.ResponseType = SdResponseTypeR1b;
2076 SdCmdBlk.CommandArgument = (UINT32)Rca << 16;
2077
2078 Status = SdPeimExecCmd (Slot, &Packet);
2079
2080 return Status;
2081 }
2082
2083 /**
2084 Send command VOLTAGE_SWITCH to the SD device to switch the voltage of the device.
2085
2086 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
2087
2088 @param[in] Slot The slot number of the SD card to send the command to.
2089
2090 @retval EFI_SUCCESS The operation is done correctly.
2091 @retval Others The operation fails.
2092
2093 **/
2094 EFI_STATUS
2095 SdPeimVoltageSwitch (
2096 IN SD_PEIM_HC_SLOT *Slot
2097 )
2098 {
2099 SD_COMMAND_BLOCK SdCmdBlk;
2100 SD_STATUS_BLOCK SdStatusBlk;
2101 SD_COMMAND_PACKET Packet;
2102 EFI_STATUS Status;
2103
2104 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2105 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2106 ZeroMem (&Packet, sizeof (Packet));
2107
2108 Packet.SdCmdBlk = &SdCmdBlk;
2109 Packet.SdStatusBlk = &SdStatusBlk;
2110 Packet.Timeout = SD_TIMEOUT;
2111
2112 SdCmdBlk.CommandIndex = SD_VOLTAGE_SWITCH;
2113 SdCmdBlk.CommandType = SdCommandTypeAc;
2114 SdCmdBlk.ResponseType = SdResponseTypeR1;
2115 SdCmdBlk.CommandArgument = 0;
2116
2117 Status = SdPeimExecCmd (Slot, &Packet);
2118
2119 return Status;
2120 }
2121
2122 /**
2123 Send command SET_BUS_WIDTH to the SD device to set the bus width.
2124
2125 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
2126
2127 @param[in] Slot The slot number of the SD card to send the command to.
2128 @param[in] Rca The relative device address of addressed device.
2129 @param[in] BusWidth The bus width to be set, it could be 1 or 4.
2130
2131 @retval EFI_SUCCESS The operation is done correctly.
2132 @retval Others The operation fails.
2133
2134 **/
2135 EFI_STATUS
2136 SdPeimSetBusWidth (
2137 IN SD_PEIM_HC_SLOT *Slot,
2138 IN UINT16 Rca,
2139 IN UINT8 BusWidth
2140 )
2141 {
2142 SD_COMMAND_BLOCK SdCmdBlk;
2143 SD_STATUS_BLOCK SdStatusBlk;
2144 SD_COMMAND_PACKET Packet;
2145 EFI_STATUS Status;
2146 UINT8 Value;
2147
2148 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2149 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2150 ZeroMem (&Packet, sizeof (Packet));
2151
2152 Packet.SdCmdBlk = &SdCmdBlk;
2153 Packet.SdStatusBlk = &SdStatusBlk;
2154 Packet.Timeout = SD_TIMEOUT;
2155
2156 SdCmdBlk.CommandIndex = SD_APP_CMD;
2157 SdCmdBlk.CommandType = SdCommandTypeAc;
2158 SdCmdBlk.ResponseType = SdResponseTypeR1;
2159 SdCmdBlk.CommandArgument = (UINT32)Rca << 16;
2160
2161 Status = SdPeimExecCmd (Slot, &Packet);
2162 if (EFI_ERROR (Status)) {
2163 return Status;
2164 }
2165
2166 SdCmdBlk.CommandIndex = SD_SET_BUS_WIDTH;
2167 SdCmdBlk.CommandType = SdCommandTypeAc;
2168 SdCmdBlk.ResponseType = SdResponseTypeR1;
2169
2170 if (BusWidth == 1) {
2171 Value = 0;
2172 } else if (BusWidth == 4) {
2173 Value = 2;
2174 } else {
2175 return EFI_INVALID_PARAMETER;
2176 }
2177 SdCmdBlk.CommandArgument = Value & 0x3;
2178
2179 Status = SdPeimExecCmd (Slot, &Packet);
2180
2181 return Status;
2182 }
2183
2184 /**
2185 Send command SWITCH_FUNC to the SD device to check switchable function or switch card function.
2186
2187 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
2188
2189 @param[in] Slot The slot number of the SD card to send the command to.
2190 @param[in] AccessMode The value for access mode group.
2191 @param[in] CommandSystem The value for command set group.
2192 @param[in] DriveStrength The value for drive length group.
2193 @param[in] PowerLimit The value for power limit group.
2194 @param[in] Mode Switch or check function.
2195 @param[out] SwitchResp The return switch function status.
2196
2197 @retval EFI_SUCCESS The operation is done correctly.
2198 @retval Others The operation fails.
2199
2200 **/
2201 EFI_STATUS
2202 SdPeimSwitch (
2203 IN SD_PEIM_HC_SLOT *Slot,
2204 IN UINT8 AccessMode,
2205 IN UINT8 CommandSystem,
2206 IN UINT8 DriveStrength,
2207 IN UINT8 PowerLimit,
2208 IN BOOLEAN Mode,
2209 OUT UINT8 *SwitchResp
2210 )
2211 {
2212 SD_COMMAND_BLOCK SdCmdBlk;
2213 SD_STATUS_BLOCK SdStatusBlk;
2214 SD_COMMAND_PACKET Packet;
2215 EFI_STATUS Status;
2216 UINT32 ModeValue;
2217
2218 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2219 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2220 ZeroMem (&Packet, sizeof (Packet));
2221
2222 Packet.SdCmdBlk = &SdCmdBlk;
2223 Packet.SdStatusBlk = &SdStatusBlk;
2224 Packet.Timeout = SD_TIMEOUT;
2225
2226 SdCmdBlk.CommandIndex = SD_SWITCH_FUNC;
2227 SdCmdBlk.CommandType = SdCommandTypeAdtc;
2228 SdCmdBlk.ResponseType = SdResponseTypeR1;
2229
2230 ModeValue = Mode ? BIT31 : 0;
2231 SdCmdBlk.CommandArgument = (AccessMode & 0xF) | ((PowerLimit & 0xF) << 4) | \
2232 ((DriveStrength & 0xF) << 8) | ((DriveStrength & 0xF) << 12) | \
2233 ModeValue;
2234 Packet.InDataBuffer = SwitchResp;
2235 Packet.InTransferLength = 64;
2236
2237 Status = SdPeimExecCmd (Slot, &Packet);
2238
2239 return Status;
2240 }
2241
2242 /**
2243 Send command SEND_STATUS to the addressed SD device to get its status register.
2244
2245 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
2246
2247 @param[in] Slot The slot number of the SD card to send the command to.
2248 @param[in] Rca The relative device address of addressed device.
2249 @param[out] DevStatus The returned device status.
2250
2251 @retval EFI_SUCCESS The operation is done correctly.
2252 @retval Others The operation fails.
2253
2254 **/
2255 EFI_STATUS
2256 SdPeimSendStatus (
2257 IN SD_PEIM_HC_SLOT *Slot,
2258 IN UINT16 Rca,
2259 OUT UINT32 *DevStatus
2260 )
2261 {
2262 SD_COMMAND_BLOCK SdCmdBlk;
2263 SD_STATUS_BLOCK SdStatusBlk;
2264 SD_COMMAND_PACKET Packet;
2265 EFI_STATUS Status;
2266
2267 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2268 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2269 ZeroMem (&Packet, sizeof (Packet));
2270
2271 Packet.SdCmdBlk = &SdCmdBlk;
2272 Packet.SdStatusBlk = &SdStatusBlk;
2273 Packet.Timeout = SD_TIMEOUT;
2274
2275 SdCmdBlk.CommandIndex = SD_SEND_STATUS;
2276 SdCmdBlk.CommandType = SdCommandTypeAc;
2277 SdCmdBlk.ResponseType = SdResponseTypeR1;
2278 SdCmdBlk.CommandArgument = (UINT32)Rca << 16;
2279
2280 Status = SdPeimExecCmd (Slot, &Packet);
2281 if (!EFI_ERROR (Status)) {
2282 *DevStatus = SdStatusBlk.Resp0;
2283 }
2284
2285 return Status;
2286 }
2287
2288 /**
2289 Send command READ_SINGLE_BLOCK/WRITE_SINGLE_BLOCK to the addressed SD device
2290 to read/write the specified number of blocks.
2291
2292 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
2293
2294 @param[in] Slot The slot number of the SD card to send the command to.
2295 @param[in] Lba The logical block address of starting access.
2296 @param[in] BlockSize The block size of specified SD device partition.
2297 @param[in] Buffer The pointer to the transfer buffer.
2298 @param[in] BufferSize The size of transfer buffer.
2299 @param[in] IsRead Boolean to show the operation direction.
2300
2301 @retval EFI_SUCCESS The operation is done correctly.
2302 @retval Others The operation fails.
2303
2304 **/
2305 EFI_STATUS
2306 SdPeimRwSingleBlock (
2307 IN SD_PEIM_HC_SLOT *Slot,
2308 IN EFI_LBA Lba,
2309 IN UINT32 BlockSize,
2310 IN VOID *Buffer,
2311 IN UINTN BufferSize,
2312 IN BOOLEAN IsRead
2313 )
2314 {
2315 SD_COMMAND_BLOCK SdCmdBlk;
2316 SD_STATUS_BLOCK SdStatusBlk;
2317 SD_COMMAND_PACKET Packet;
2318 EFI_STATUS Status;
2319
2320 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2321 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2322 ZeroMem (&Packet, sizeof (Packet));
2323
2324 Packet.SdCmdBlk = &SdCmdBlk;
2325 Packet.SdStatusBlk = &SdStatusBlk;
2326 //
2327 // Calculate timeout value through the below formula.
2328 // Timeout = (transfer size) / (2MB/s).
2329 // Taking 2MB/s as divisor is because it's the lowest
2330 // transfer speed of class 2.
2331 //
2332 Packet.Timeout = (BufferSize / (2 * 1024 * 1024) + 1) * 1000 * 1000;;
2333
2334 if (IsRead) {
2335 Packet.InDataBuffer = Buffer;
2336 Packet.InTransferLength = (UINT32)BufferSize;
2337
2338 SdCmdBlk.CommandIndex = SD_READ_SINGLE_BLOCK;
2339 SdCmdBlk.CommandType = SdCommandTypeAdtc;
2340 SdCmdBlk.ResponseType = SdResponseTypeR1;
2341 } else {
2342 Packet.OutDataBuffer = Buffer;
2343 Packet.OutTransferLength = (UINT32)BufferSize;
2344
2345 SdCmdBlk.CommandIndex = SD_WRITE_SINGLE_BLOCK;
2346 SdCmdBlk.CommandType = SdCommandTypeAdtc;
2347 SdCmdBlk.ResponseType = SdResponseTypeR1;
2348 }
2349
2350 if (Slot->SectorAddressing) {
2351 SdCmdBlk.CommandArgument = (UINT32)Lba;
2352 } else {
2353 SdCmdBlk.CommandArgument = (UINT32)MultU64x32 (Lba, BlockSize);
2354 }
2355
2356 Status = SdPeimExecCmd (Slot, &Packet);
2357
2358 return Status;
2359 }
2360
2361 /**
2362 Send command READ_MULTIPLE_BLOCK/WRITE_MULTIPLE_BLOCK to the addressed SD device
2363 to read/write the specified number of blocks.
2364
2365 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
2366
2367 @param[in] Slot The slot number of the SD card to send the command to.
2368 @param[in] Lba The logical block address of starting access.
2369 @param[in] BlockSize The block size of specified SD device partition.
2370 @param[in] Buffer The pointer to the transfer buffer.
2371 @param[in] BufferSize The size of transfer buffer.
2372 @param[in] IsRead Boolean to show the operation direction.
2373
2374 @retval EFI_SUCCESS The operation is done correctly.
2375 @retval Others The operation fails.
2376
2377 **/
2378 EFI_STATUS
2379 SdPeimRwMultiBlocks (
2380 IN SD_PEIM_HC_SLOT *Slot,
2381 IN EFI_LBA Lba,
2382 IN UINT32 BlockSize,
2383 IN VOID *Buffer,
2384 IN UINTN BufferSize,
2385 IN BOOLEAN IsRead
2386 )
2387 {
2388 SD_COMMAND_BLOCK SdCmdBlk;
2389 SD_STATUS_BLOCK SdStatusBlk;
2390 SD_COMMAND_PACKET Packet;
2391 EFI_STATUS Status;
2392
2393 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2394 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2395 ZeroMem (&Packet, sizeof (Packet));
2396
2397 Packet.SdCmdBlk = &SdCmdBlk;
2398 Packet.SdStatusBlk = &SdStatusBlk;
2399 //
2400 // Calculate timeout value through the below formula.
2401 // Timeout = (transfer size) / (2MB/s).
2402 // Taking 2MB/s as divisor is because it's the lowest
2403 // transfer speed of class 2.
2404 //
2405 Packet.Timeout = (BufferSize / (2 * 1024 * 1024) + 1) * 1000 * 1000;;
2406
2407 if (IsRead) {
2408 Packet.InDataBuffer = Buffer;
2409 Packet.InTransferLength = (UINT32)BufferSize;
2410
2411 SdCmdBlk.CommandIndex = SD_READ_MULTIPLE_BLOCK;
2412 SdCmdBlk.CommandType = SdCommandTypeAdtc;
2413 SdCmdBlk.ResponseType = SdResponseTypeR1;
2414 } else {
2415 Packet.OutDataBuffer = Buffer;
2416 Packet.OutTransferLength = (UINT32)BufferSize;
2417
2418 SdCmdBlk.CommandIndex = SD_WRITE_MULTIPLE_BLOCK;
2419 SdCmdBlk.CommandType = SdCommandTypeAdtc;
2420 SdCmdBlk.ResponseType = SdResponseTypeR1;
2421 }
2422
2423 if (Slot->SectorAddressing) {
2424 SdCmdBlk.CommandArgument = (UINT32)Lba;
2425 } else {
2426 SdCmdBlk.CommandArgument = (UINT32)MultU64x32 (Lba, BlockSize);
2427 }
2428
2429 Status = SdPeimExecCmd (Slot, &Packet);
2430
2431 return Status;
2432 }
2433
2434 /**
2435 Send command SEND_TUNING_BLOCK to the SD device for SDR104/SDR50 optimal sampling point
2436 detection.
2437
2438 It may be sent up to 40 times until the host finishes the tuning procedure.
2439
2440 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 for details.
2441
2442 @param[in] Slot The slot number of the SD card to send the command to.
2443
2444 @retval EFI_SUCCESS The operation is done correctly.
2445 @retval Others The operation fails.
2446
2447 **/
2448 EFI_STATUS
2449 SdPeimSendTuningBlk (
2450 IN SD_PEIM_HC_SLOT *Slot
2451 )
2452 {
2453 SD_COMMAND_BLOCK SdCmdBlk;
2454 SD_STATUS_BLOCK SdStatusBlk;
2455 SD_COMMAND_PACKET Packet;
2456 EFI_STATUS Status;
2457 UINT8 TuningBlock[64];
2458
2459 ZeroMem (&SdCmdBlk, sizeof (SdCmdBlk));
2460 ZeroMem (&SdStatusBlk, sizeof (SdStatusBlk));
2461 ZeroMem (&Packet, sizeof (Packet));
2462
2463 Packet.SdCmdBlk = &SdCmdBlk;
2464 Packet.SdStatusBlk = &SdStatusBlk;
2465 Packet.Timeout = SD_TIMEOUT;
2466
2467 SdCmdBlk.CommandIndex = SD_SEND_TUNING_BLOCK;
2468 SdCmdBlk.CommandType = SdCommandTypeAdtc;
2469 SdCmdBlk.ResponseType = SdResponseTypeR1;
2470 SdCmdBlk.CommandArgument = 0;
2471
2472 Packet.InDataBuffer = TuningBlock;
2473 Packet.InTransferLength = sizeof (TuningBlock);
2474
2475 Status = SdPeimExecCmd (Slot, &Packet);
2476
2477 return Status;
2478 }
2479
2480 /**
2481 Tunning the sampling point of SDR104 or SDR50 bus speed mode.
2482
2483 Command SD_SEND_TUNING_BLOCK may be sent up to 40 times until the host finishes the
2484 tuning procedure.
2485
2486 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 and SD Host Controller
2487 Simplified Spec 3.0 Figure 2-29 for details.
2488
2489 @param[in] Slot The slot number of the SD card to send the command to.
2490
2491 @retval EFI_SUCCESS The operation is done correctly.
2492 @retval Others The operation fails.
2493
2494 **/
2495 EFI_STATUS
2496 SdPeimTuningClock (
2497 IN SD_PEIM_HC_SLOT *Slot
2498 )
2499 {
2500 EFI_STATUS Status;
2501 UINT8 HostCtrl2;
2502 UINT8 Retry;
2503
2504 //
2505 // Notify the host that the sampling clock tuning procedure starts.
2506 //
2507 HostCtrl2 = BIT6;
2508 Status = SdPeimHcOrMmio (Slot->SdHcBase + SD_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2509 if (EFI_ERROR (Status)) {
2510 return Status;
2511 }
2512 //
2513 // Ask the device to send a sequence of tuning blocks till the tuning procedure is done.
2514 //
2515 Retry = 0;
2516 do {
2517 Status = SdPeimSendTuningBlk (Slot);
2518 if (EFI_ERROR (Status)) {
2519 return Status;
2520 }
2521
2522 Status = SdPeimHcRwMmio (Slot->SdHcBase + SD_HC_HOST_CTRL2, TRUE, sizeof (HostCtrl2), &HostCtrl2);
2523 if (EFI_ERROR (Status)) {
2524 return Status;
2525 }
2526
2527 if ((HostCtrl2 & (BIT6 | BIT7)) == 0) {
2528 break;
2529 }
2530
2531 if ((HostCtrl2 & (BIT6 | BIT7)) == BIT7) {
2532 return EFI_SUCCESS;
2533 }
2534 } while (++Retry < 40);
2535
2536 DEBUG ((EFI_D_ERROR, "SdPeimTuningClock: Send tuning block fails at %d times with HostCtrl2 %02x\n", Retry, HostCtrl2));
2537 //
2538 // Abort the tuning procedure and reset the tuning circuit.
2539 //
2540 HostCtrl2 = (UINT8)~(BIT6 | BIT7);
2541 Status = SdPeimHcAndMmio (Slot->SdHcBase + SD_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2542 if (EFI_ERROR (Status)) {
2543 return Status;
2544 }
2545 return EFI_DEVICE_ERROR;
2546 }
2547
2548 /**
2549 Switch the bus width to specified width.
2550
2551 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 and
2552 SD Host Controller Simplified Spec 3.0 section Figure 3-7 for details.
2553
2554 @param[in] Slot The slot number of the SD card to send the command to.
2555 @param[in] Rca The relative device address to be assigned.
2556 @param[in] BusWidth The bus width to be set, it could be 4 or 8.
2557
2558 @retval EFI_SUCCESS The operation is done correctly.
2559 @retval Others The operation fails.
2560
2561 **/
2562 EFI_STATUS
2563 SdPeimSwitchBusWidth (
2564 IN SD_PEIM_HC_SLOT *Slot,
2565 IN UINT16 Rca,
2566 IN UINT8 BusWidth
2567 )
2568 {
2569 EFI_STATUS Status;
2570 UINT32 DevStatus;
2571
2572 Status = SdPeimSetBusWidth (Slot, Rca, BusWidth);
2573 if (EFI_ERROR (Status)) {
2574 return Status;
2575 }
2576
2577 Status = SdPeimSendStatus (Slot, Rca, &DevStatus);
2578 if (EFI_ERROR (Status)) {
2579 return Status;
2580 }
2581 //
2582 // Check the switch operation is really successful or not.
2583 //
2584 if ((DevStatus >> 16) != 0) {
2585 return EFI_DEVICE_ERROR;
2586 }
2587
2588 Status = SdPeimHcSetBusWidth (Slot->SdHcBase, BusWidth);
2589
2590 return Status;
2591 }
2592
2593 /**
2594 Switch the high speed timing according to request.
2595
2596 Refer to SD Physical Layer Simplified Spec 4.1 Section 4.7 and
2597 SD Host Controller Simplified Spec 3.0 section Figure 2-29 for details.
2598
2599 @param[in] Slot The slot number of the SD card to send the command to.
2600 @param[in] Rca The relative device address to be assigned.
2601 @param[in] S18a The boolean to show if it's a UHS-I SD card.
2602
2603 @retval EFI_SUCCESS The operation is done correctly.
2604 @retval Others The operation fails.
2605
2606 **/
2607 EFI_STATUS
2608 SdPeimSetBusMode (
2609 IN SD_PEIM_HC_SLOT *Slot,
2610 IN UINT16 Rca,
2611 IN BOOLEAN S18a
2612 )
2613 {
2614 EFI_STATUS Status;
2615 SD_HC_SLOT_CAP Capability;
2616 UINT32 ClockFreq;
2617 UINT8 BusWidth;
2618 UINT8 AccessMode;
2619 UINT8 HostCtrl1;
2620 UINT8 HostCtrl2;
2621 UINT8 SwitchResp[64];
2622
2623 Status = SdPeimGetCsd (Slot, Rca, &Slot->Csd);
2624 if (EFI_ERROR (Status)) {
2625 DEBUG ((EFI_D_ERROR, "SdPeimSetBusMode: SdPeimGetCsd fails with %r\n", Status));
2626 return Status;
2627 }
2628
2629 Status = SdPeimHcGetCapability (Slot->SdHcBase, &Capability);
2630 if (EFI_ERROR (Status)) {
2631 return Status;
2632 }
2633
2634 Status = SdPeimSelect (Slot, Rca);
2635 if (EFI_ERROR (Status)) {
2636 DEBUG ((EFI_D_ERROR, "SdPeimSetBusMode: SdPeimSelect fails with %r\n", Status));
2637 return Status;
2638 }
2639
2640 BusWidth = 4;
2641 Status = SdPeimSwitchBusWidth (Slot, Rca, BusWidth);
2642 if (EFI_ERROR (Status)) {
2643 DEBUG ((EFI_D_ERROR, "SdPeimSetBusMode: SdPeimSwitchBusWidth fails with %r\n", Status));
2644 return Status;
2645 }
2646
2647 //
2648 // Get the supported bus speed from SWITCH cmd return data group #1.
2649 //
2650 ZeroMem (SwitchResp, sizeof (SwitchResp));
2651 Status = SdPeimSwitch (Slot, 0xF, 0xF, 0xF, 0xF, FALSE, SwitchResp);
2652 if (EFI_ERROR (Status)) {
2653 return Status;
2654 }
2655 //
2656 // Calculate supported bus speed/bus width/clock frequency by host and device capability.
2657 //
2658 ClockFreq = 0;
2659 if (S18a && (Capability.Sdr104 != 0) && ((SwitchResp[13] & BIT3) != 0)) {
2660 ClockFreq = 208;
2661 AccessMode = 3;
2662 } else if (S18a && (Capability.Sdr50 != 0) && ((SwitchResp[13] & BIT2) != 0)) {
2663 ClockFreq = 100;
2664 AccessMode = 2;
2665 } else if (S18a && (Capability.Ddr50 != 0) && ((SwitchResp[13] & BIT4) != 0)) {
2666 ClockFreq = 50;
2667 AccessMode = 4;
2668 } else if ((SwitchResp[13] & BIT1) != 0) {
2669 ClockFreq = 50;
2670 AccessMode = 1;
2671 } else {
2672 ClockFreq = 25;
2673 AccessMode = 0;
2674 }
2675
2676 DEBUG ((EFI_D_INFO, "SdPeimSetBusMode: AccessMode %d ClockFreq %d BusWidth %d\n", AccessMode, ClockFreq, BusWidth));
2677
2678 Status = SdPeimSwitch (Slot, AccessMode, 0xF, 0xF, 0xF, TRUE, SwitchResp);
2679 if (EFI_ERROR (Status)) {
2680 DEBUG ((EFI_D_ERROR, "SdPeimSetBusMode: SdPeimSwitch fails with %r\n", Status));
2681 return Status;
2682 }
2683
2684 if ((SwitchResp[16] & 0xF) != AccessMode) {
2685 DEBUG ((EFI_D_ERROR, "SdPeimSetBusMode: SdPeimSwitch to AccessMode %d ClockFreq %d BusWidth %d fails! The Switch response is 0x%1x\n", AccessMode, ClockFreq, BusWidth, SwitchResp[16] & 0xF));
2686 return EFI_DEVICE_ERROR;
2687 }
2688 //
2689 // Set to Hight Speed timing
2690 //
2691 if (AccessMode == 1) {
2692 HostCtrl1 = BIT2;
2693 Status = SdPeimHcOrMmio (Slot->SdHcBase + SD_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);
2694 if (EFI_ERROR (Status)) {
2695 return Status;
2696 }
2697 }
2698
2699 HostCtrl2 = (UINT8)~0x7;
2700 Status = SdPeimHcAndMmio (Slot->SdHcBase + SD_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2701 if (EFI_ERROR (Status)) {
2702 return Status;
2703 }
2704 HostCtrl2 = AccessMode;
2705 Status = SdPeimHcOrMmio (Slot->SdHcBase + SD_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2706 if (EFI_ERROR (Status)) {
2707 return Status;
2708 }
2709
2710 Status = SdPeimHcClockSupply (Slot->SdHcBase, ClockFreq * 1000);
2711 if (EFI_ERROR (Status)) {
2712 DEBUG ((EFI_D_ERROR, "SdPeimSetBusMode: SdPeimHcClockSupply %r\n", Status));
2713 return Status;
2714 }
2715
2716 if ((AccessMode == 3) || ((AccessMode == 2) && (Capability.TuningSDR50 != 0))) {
2717 Status = SdPeimTuningClock (Slot);
2718 if (EFI_ERROR (Status)) {
2719 DEBUG ((EFI_D_ERROR, "SdPeimSetBusMode: SdPeimTuningClock fails with %r\n", Status));
2720 return Status;
2721 }
2722 }
2723
2724 DEBUG ((EFI_D_INFO, "SdPeimSetBusMode: SdPeimSetBusMode %r\n", Status));
2725
2726 return Status;
2727 }
2728
2729 /**
2730 Execute SD device identification procedure.
2731
2732 Refer to SD Physical Layer Simplified Spec 4.1 Section 3.6 for details.
2733
2734 @param[in] Slot The slot number of the SD card to send the command to.
2735
2736 @retval EFI_SUCCESS There is a SD card.
2737 @retval Others There is not a SD card.
2738
2739 **/
2740 EFI_STATUS
2741 SdPeimIdentification (
2742 IN SD_PEIM_HC_SLOT *Slot
2743 )
2744 {
2745 EFI_STATUS Status;
2746 UINT32 Ocr;
2747 UINT16 Rca;
2748 BOOLEAN Xpc;
2749 BOOLEAN S18r;
2750 UINT64 MaxCurrent;
2751 UINT64 Current;
2752 UINT16 ControllerVer;
2753 UINT8 PowerCtrl;
2754 UINT32 PresentState;
2755 UINT8 HostCtrl2;
2756 SD_HC_SLOT_CAP Capability;
2757 UINTN Retry;
2758 //
2759 // 1. Send Cmd0 to the device
2760 //
2761 Status = SdPeimReset (Slot);
2762 if (EFI_ERROR (Status)) {
2763 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: Executing Cmd0 fails with %r\n", Status));
2764 return Status;
2765 }
2766 //
2767 // 2. Send Cmd8 to the device
2768 //
2769 Status = SdPeimVoltageCheck (Slot, 0x1, 0xFF);
2770 if (EFI_ERROR (Status)) {
2771 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: Executing Cmd8 fails with %r\n", Status));
2772 return Status;
2773 }
2774 //
2775 // 3. Send SDIO Cmd5 to the device to the SDIO device OCR register.
2776 //
2777 Status = SdioSendOpCond (Slot, 0, FALSE);
2778 if (!EFI_ERROR (Status)) {
2779 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: Found SDIO device, ignore it as we don't support\n"));
2780 return EFI_DEVICE_ERROR;
2781 }
2782 //
2783 // 4. Send Acmd41 with voltage window 0 to the device
2784 //
2785 Status = SdPeimSendOpCond (Slot, 0, 0, FALSE, FALSE, FALSE, &Ocr);
2786 if (EFI_ERROR (Status)) {
2787 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: Executing SdPeimSendOpCond fails with %r\n", Status));
2788 return EFI_DEVICE_ERROR;
2789 }
2790
2791 Status = SdPeimHcGetCapability (Slot->SdHcBase, &Capability);
2792 if (EFI_ERROR (Status)) {
2793 return Status;
2794 }
2795
2796 Status = SdPeimHcRwMmio (Slot->SdHcBase + SD_HC_MAX_CURRENT_CAP, TRUE, sizeof (Current), &Current);
2797 if (EFI_ERROR (Status)) {
2798 return Status;
2799 }
2800
2801 if (Capability.Voltage33 != 0) {
2802 //
2803 // Support 3.3V
2804 //
2805 MaxCurrent = ((UINT32)Current & 0xFF) * 4;
2806 } else if (Capability.Voltage30 != 0) {
2807 //
2808 // Support 3.0V
2809 //
2810 MaxCurrent = (((UINT32)Current >> 8) & 0xFF) * 4;
2811 } else if (Capability.Voltage18 != 0) {
2812 //
2813 // Support 1.8V
2814 //
2815 MaxCurrent = (((UINT32)Current >> 16) & 0xFF) * 4;
2816 } else {
2817 ASSERT (FALSE);
2818 return EFI_DEVICE_ERROR;
2819 }
2820
2821 if (MaxCurrent >= 150) {
2822 Xpc = TRUE;
2823 } else {
2824 Xpc = FALSE;
2825 }
2826
2827 Status = SdPeimHcRwMmio (Slot->SdHcBase + SD_HC_CTRL_VER, TRUE, sizeof (ControllerVer), &ControllerVer);
2828 if (EFI_ERROR (Status)) {
2829 return Status;
2830 }
2831
2832 if ((ControllerVer & 0xFF) == 2) {
2833 S18r = TRUE;
2834 } else if (((ControllerVer & 0xFF) == 0) || ((ControllerVer & 0xFF) == 1)) {
2835 S18r = FALSE;
2836 } else {
2837 ASSERT (FALSE);
2838 return EFI_UNSUPPORTED;
2839 }
2840 //
2841 // 5. Repeatly send Acmd41 with supply voltage window to the device.
2842 // Note here we only support the cards complied with SD physical
2843 // layer simplified spec version 2.0 and version 3.0 and above.
2844 //
2845 Ocr = 0;
2846 Retry = 0;
2847 do {
2848 Status = SdPeimSendOpCond (Slot, 0, Ocr, S18r, Xpc, TRUE, &Ocr);
2849 if (EFI_ERROR (Status)) {
2850 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: SdPeimSendOpCond fails with %r Ocr %x, S18r %x, Xpc %x\n", Status, Ocr, S18r, Xpc));
2851 return EFI_DEVICE_ERROR;
2852 }
2853
2854 if (Retry++ == 100) {
2855 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: SdPeimSendOpCond fails too many times\n"));
2856 return EFI_DEVICE_ERROR;
2857 }
2858 MicroSecondDelay (10 * 1000);
2859 } while ((Ocr & BIT31) == 0);
2860
2861 //
2862 // 6. If the S18a bit is set and the Host Controller supports 1.8V signaling
2863 // (One of support bits is set to 1: SDR50, SDR104 or DDR50 in the
2864 // Capabilities register), switch its voltage to 1.8V.
2865 //
2866 if ((Capability.Sdr50 != 0 ||
2867 Capability.Sdr104 != 0 ||
2868 Capability.Ddr50 != 0) &&
2869 ((Ocr & BIT24) != 0)) {
2870 Status = SdPeimVoltageSwitch (Slot);
2871 if (EFI_ERROR (Status)) {
2872 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: Executing SdPeimVoltageSwitch fails with %r\n", Status));
2873 Status = EFI_DEVICE_ERROR;
2874 goto Error;
2875 } else {
2876 Status = SdPeimHcStopClock (Slot->SdHcBase);
2877 if (EFI_ERROR (Status)) {
2878 Status = EFI_DEVICE_ERROR;
2879 goto Error;
2880 }
2881
2882 SdPeimHcRwMmio (Slot->SdHcBase + SD_HC_PRESENT_STATE, TRUE, sizeof (PresentState), &PresentState);
2883 if (((PresentState >> 20) & 0xF) != 0) {
2884 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: SwitchVoltage fails with PresentState = 0x%x\n", PresentState));
2885 Status = EFI_DEVICE_ERROR;
2886 goto Error;
2887 }
2888 HostCtrl2 = BIT3;
2889 SdPeimHcOrMmio (Slot->SdHcBase + SD_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);
2890
2891 MicroSecondDelay (5000);
2892
2893 SdPeimHcRwMmio (Slot->SdHcBase + SD_HC_HOST_CTRL2, TRUE, sizeof (HostCtrl2), &HostCtrl2);
2894 if ((HostCtrl2 & BIT3) == 0) {
2895 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: SwitchVoltage fails with HostCtrl2 = 0x%x\n", HostCtrl2));
2896 Status = EFI_DEVICE_ERROR;
2897 goto Error;
2898 }
2899
2900 SdPeimHcInitClockFreq (Slot->SdHcBase);
2901
2902 MicroSecondDelay (1000);
2903
2904 SdPeimHcRwMmio (Slot->SdHcBase + SD_HC_PRESENT_STATE, TRUE, sizeof (PresentState), &PresentState);
2905 if (((PresentState >> 20) & 0xF) != 0xF) {
2906 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: SwitchVoltage fails with PresentState = 0x%x, It should be 0xF\n", PresentState));
2907 Status = EFI_DEVICE_ERROR;
2908 goto Error;
2909 }
2910 }
2911 DEBUG ((EFI_D_INFO, "SdPeimIdentification: Switch to 1.8v signal voltage success\n"));
2912 }
2913
2914 Status = SdPeimAllSendCid (Slot);
2915 if (EFI_ERROR (Status)) {
2916 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: Executing SdPeimAllSendCid fails with %r\n", Status));
2917 return Status;
2918 }
2919
2920 Status = SdPeimSetRca (Slot, &Rca);
2921 if (EFI_ERROR (Status)) {
2922 DEBUG ((EFI_D_ERROR, "SdPeimIdentification: Executing SdPeimSetRca fails with %r\n", Status));
2923 return Status;
2924 }
2925 //
2926 // Enter Data Tranfer Mode.
2927 //
2928 DEBUG ((EFI_D_INFO, "Found a SD device at slot [%d]\n", Slot));
2929
2930 Status = SdPeimSetBusMode (Slot, Rca, ((Ocr & BIT24) != 0));
2931
2932 return Status;
2933
2934 Error:
2935 //
2936 // Set SD Bus Power = 0
2937 //
2938 PowerCtrl = (UINT8)~BIT0;
2939 Status = SdPeimHcAndMmio (Slot->SdHcBase + SD_HC_POWER_CTRL, sizeof (PowerCtrl), &PowerCtrl);
2940 return EFI_DEVICE_ERROR;
2941 }