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