]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHcDxe.c
b474f8decdb40aacc72a8dc737906337c2ec1cfa
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / SdMmcPciHcDxe / SdMmcPciHcDxe.c
1 /** @file
2 This driver is used to manage SD/MMC PCI host controllers which are compliance
3 with SD Host Controller Simplified Specification version 3.00 plus the 64-bit
4 System Addressing support in SD Host Controller Simplified Specification version
5 4.20.
6
7 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.
8
9 Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
10 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21 #include "SdMmcPciHcDxe.h"
22
23 EDKII_SD_MMC_OVERRIDE *mOverride;
24
25 //
26 // Driver Global Variables
27 //
28 EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding = {
29 SdMmcPciHcDriverBindingSupported,
30 SdMmcPciHcDriverBindingStart,
31 SdMmcPciHcDriverBindingStop,
32 0x10,
33 NULL,
34 NULL
35 };
36
37 //
38 // Template for SD/MMC host controller private data.
39 //
40 SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate = {
41 SD_MMC_HC_PRIVATE_SIGNATURE, // Signature
42 NULL, // ControllerHandle
43 NULL, // PciIo
44 { // PassThru
45 sizeof (UINT32),
46 SdMmcPassThruPassThru,
47 SdMmcPassThruGetNextSlot,
48 SdMmcPassThruBuildDevicePath,
49 SdMmcPassThruGetSlotNumber,
50 SdMmcPassThruResetDevice
51 },
52 0, // PciAttributes
53 0, // PreviousSlot
54 NULL, // TimerEvent
55 NULL, // ConnectEvent
56 // Queue
57 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate.Queue),
58 { // Slot
59 {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0},
60 {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}
61 },
62 { // Capability
63 {0},
64 },
65 { // MaxCurrent
66 0,
67 },
68 {
69 0 // ControllerVersion
70 }
71 };
72
73 SD_DEVICE_PATH mSdDpTemplate = {
74 {
75 MESSAGING_DEVICE_PATH,
76 MSG_SD_DP,
77 {
78 (UINT8) (sizeof (SD_DEVICE_PATH)),
79 (UINT8) ((sizeof (SD_DEVICE_PATH)) >> 8)
80 }
81 },
82 0
83 };
84
85 EMMC_DEVICE_PATH mEmmcDpTemplate = {
86 {
87 MESSAGING_DEVICE_PATH,
88 MSG_EMMC_DP,
89 {
90 (UINT8) (sizeof (EMMC_DEVICE_PATH)),
91 (UINT8) ((sizeof (EMMC_DEVICE_PATH)) >> 8)
92 }
93 },
94 0
95 };
96
97 //
98 // Prioritized function list to detect card type.
99 // User could add other card detection logic here.
100 //
101 CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable[] = {
102 EmmcIdentification,
103 SdCardIdentification,
104 NULL
105 };
106
107 /**
108 The entry point for SD host controller driver, used to install this driver on the ImageHandle.
109
110 @param[in] ImageHandle The firmware allocated handle for this driver image.
111 @param[in] SystemTable Pointer to the EFI system table.
112
113 @retval EFI_SUCCESS Driver loaded.
114 @retval other Driver not loaded.
115
116 **/
117 EFI_STATUS
118 EFIAPI
119 InitializeSdMmcPciHcDxe (
120 IN EFI_HANDLE ImageHandle,
121 IN EFI_SYSTEM_TABLE *SystemTable
122 )
123 {
124 EFI_STATUS Status;
125
126 Status = EfiLibInstallDriverBindingComponentName2 (
127 ImageHandle,
128 SystemTable,
129 &gSdMmcPciHcDriverBinding,
130 ImageHandle,
131 &gSdMmcPciHcComponentName,
132 &gSdMmcPciHcComponentName2
133 );
134 ASSERT_EFI_ERROR (Status);
135
136 return Status;
137 }
138
139 /**
140 Call back function when the timer event is signaled.
141
142 @param[in] Event The Event this notify function registered to.
143 @param[in] Context Pointer to the context data registered to the
144 Event.
145
146 **/
147 VOID
148 EFIAPI
149 ProcessAsyncTaskList (
150 IN EFI_EVENT Event,
151 IN VOID* Context
152 )
153 {
154 SD_MMC_HC_PRIVATE_DATA *Private;
155 LIST_ENTRY *Link;
156 SD_MMC_HC_TRB *Trb;
157 EFI_STATUS Status;
158 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;
159 BOOLEAN InfiniteWait;
160 EFI_EVENT TrbEvent;
161
162 Private = (SD_MMC_HC_PRIVATE_DATA*)Context;
163
164 //
165 // Check if the first entry in the async I/O queue is done or not.
166 //
167 Status = EFI_SUCCESS;
168 Trb = NULL;
169 Link = GetFirstNode (&Private->Queue);
170 if (!IsNull (&Private->Queue, Link)) {
171 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);
172 if (!Private->Slot[Trb->Slot].MediaPresent) {
173 Status = EFI_NO_MEDIA;
174 goto Done;
175 }
176 if (!Trb->Started) {
177 //
178 // Check whether the cmd/data line is ready for transfer.
179 //
180 Status = SdMmcCheckTrbEnv (Private, Trb);
181 if (!EFI_ERROR (Status)) {
182 Trb->Started = TRUE;
183 Status = SdMmcExecTrb (Private, Trb);
184 if (EFI_ERROR (Status)) {
185 goto Done;
186 }
187 } else {
188 goto Done;
189 }
190 }
191 Status = SdMmcCheckTrbResult (Private, Trb);
192 }
193
194 Done:
195 if ((Trb != NULL) && (Status == EFI_NOT_READY)) {
196 Packet = Trb->Packet;
197 if (Packet->Timeout == 0) {
198 InfiniteWait = TRUE;
199 } else {
200 InfiniteWait = FALSE;
201 }
202 if ((!InfiniteWait) && (Trb->Timeout-- == 0)) {
203 RemoveEntryList (Link);
204 Trb->Packet->TransactionStatus = EFI_TIMEOUT;
205 TrbEvent = Trb->Event;
206 SdMmcFreeTrb (Trb);
207 DEBUG ((DEBUG_VERBOSE, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent));
208 gBS->SignalEvent (TrbEvent);
209 return;
210 }
211 }
212 if ((Trb != NULL) && (Status != EFI_NOT_READY)) {
213 RemoveEntryList (Link);
214 Trb->Packet->TransactionStatus = Status;
215 TrbEvent = Trb->Event;
216 SdMmcFreeTrb (Trb);
217 DEBUG ((DEBUG_VERBOSE, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent, Status));
218 gBS->SignalEvent (TrbEvent);
219 }
220 return;
221 }
222
223 /**
224 Sd removable device enumeration callback function when the timer event is signaled.
225
226 @param[in] Event The Event this notify function registered to.
227 @param[in] Context Pointer to the context data registered to the
228 Event.
229
230 **/
231 VOID
232 EFIAPI
233 SdMmcPciHcEnumerateDevice (
234 IN EFI_EVENT Event,
235 IN VOID* Context
236 )
237 {
238 SD_MMC_HC_PRIVATE_DATA *Private;
239 EFI_STATUS Status;
240 UINT8 Slot;
241 BOOLEAN MediaPresent;
242 UINT32 RoutineNum;
243 CARD_TYPE_DETECT_ROUTINE *Routine;
244 UINTN Index;
245 LIST_ENTRY *Link;
246 LIST_ENTRY *NextLink;
247 SD_MMC_HC_TRB *Trb;
248 EFI_TPL OldTpl;
249
250 Private = (SD_MMC_HC_PRIVATE_DATA*)Context;
251
252 for (Slot = 0; Slot < SD_MMC_HC_MAX_SLOT; Slot++) {
253 if ((Private->Slot[Slot].Enable) && (Private->Slot[Slot].SlotType == RemovableSlot)) {
254 Status = SdMmcHcCardDetect (Private->PciIo, Slot, &MediaPresent);
255 if ((Status == EFI_MEDIA_CHANGED) && !MediaPresent) {
256 DEBUG ((DEBUG_INFO, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot, Private->PciIo));
257 Private->Slot[Slot].MediaPresent = FALSE;
258 Private->Slot[Slot].Initialized = FALSE;
259 //
260 // Signal all async task events at the slot with EFI_NO_MEDIA status.
261 //
262 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
263 for (Link = GetFirstNode (&Private->Queue);
264 !IsNull (&Private->Queue, Link);
265 Link = NextLink) {
266 NextLink = GetNextNode (&Private->Queue, Link);
267 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);
268 if (Trb->Slot == Slot) {
269 RemoveEntryList (Link);
270 Trb->Packet->TransactionStatus = EFI_NO_MEDIA;
271 gBS->SignalEvent (Trb->Event);
272 SdMmcFreeTrb (Trb);
273 }
274 }
275 gBS->RestoreTPL (OldTpl);
276 //
277 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
278 //
279 gBS->ReinstallProtocolInterface (
280 Private->ControllerHandle,
281 &gEfiSdMmcPassThruProtocolGuid,
282 &Private->PassThru,
283 &Private->PassThru
284 );
285 }
286 if ((Status == EFI_MEDIA_CHANGED) && MediaPresent) {
287 DEBUG ((DEBUG_INFO, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot, Private->PciIo));
288 //
289 // Reset the specified slot of the SD/MMC Pci Host Controller
290 //
291 Status = SdMmcHcReset (Private, Slot);
292 if (EFI_ERROR (Status)) {
293 continue;
294 }
295 //
296 // Reinitialize slot and restart identification process for the new attached device
297 //
298 Status = SdMmcHcInitHost (Private, Slot);
299 if (EFI_ERROR (Status)) {
300 continue;
301 }
302
303 Private->Slot[Slot].MediaPresent = TRUE;
304 Private->Slot[Slot].Initialized = TRUE;
305 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);
306 for (Index = 0; Index < RoutineNum; Index++) {
307 Routine = &mCardTypeDetectRoutineTable[Index];
308 if (*Routine != NULL) {
309 Status = (*Routine) (Private, Slot);
310 if (!EFI_ERROR (Status)) {
311 break;
312 }
313 }
314 }
315 //
316 // This card doesn't get initialized correctly.
317 //
318 if (Index == RoutineNum) {
319 Private->Slot[Slot].Initialized = FALSE;
320 }
321
322 //
323 // Notify the upper layer the connect state change through ReinstallProtocolInterface.
324 //
325 gBS->ReinstallProtocolInterface (
326 Private->ControllerHandle,
327 &gEfiSdMmcPassThruProtocolGuid,
328 &Private->PassThru,
329 &Private->PassThru
330 );
331 }
332 }
333 }
334
335 return;
336 }
337 /**
338 Tests to see if this driver supports a given controller. If a child device is provided,
339 it further tests to see if this driver supports creating a handle for the specified child device.
340
341 This function checks to see if the driver specified by This supports the device specified by
342 ControllerHandle. Drivers will typically use the device path attached to
343 ControllerHandle and/or the services from the bus I/O abstraction attached to
344 ControllerHandle to determine if the driver supports ControllerHandle. This function
345 may be called many times during platform initialization. In order to reduce boot times, the tests
346 performed by this function must be very small, and take as little time as possible to execute. This
347 function must not change the state of any hardware devices, and this function must be aware that the
348 device specified by ControllerHandle may already be managed by the same driver or a
349 different driver. This function must match its calls to AllocatePages() with FreePages(),
350 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
351 Since ControllerHandle may have been previously started by the same driver, if a protocol is
352 already in the opened state, then it must not be closed with CloseProtocol(). This is required
353 to guarantee the state of ControllerHandle is not modified by this function.
354
355 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
356 @param[in] ControllerHandle The handle of the controller to test. This handle
357 must support a protocol interface that supplies
358 an I/O abstraction to the driver.
359 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
360 parameter is ignored by device drivers, and is optional for bus
361 drivers. For bus drivers, if this parameter is not NULL, then
362 the bus driver must determine if the bus controller specified
363 by ControllerHandle and the child controller specified
364 by RemainingDevicePath are both supported by this
365 bus driver.
366
367 @retval EFI_SUCCESS The device specified by ControllerHandle and
368 RemainingDevicePath is supported by the driver specified by This.
369 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
370 RemainingDevicePath is already being managed by the driver
371 specified by This.
372 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
373 RemainingDevicePath is already being managed by a different
374 driver or an application that requires exclusive access.
375 Currently not implemented.
376 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
377 RemainingDevicePath is not supported by the driver specified by This.
378 **/
379 EFI_STATUS
380 EFIAPI
381 SdMmcPciHcDriverBindingSupported (
382 IN EFI_DRIVER_BINDING_PROTOCOL *This,
383 IN EFI_HANDLE Controller,
384 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
385 )
386 {
387 EFI_STATUS Status;
388 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
389 EFI_PCI_IO_PROTOCOL *PciIo;
390 PCI_TYPE00 PciData;
391
392 PciIo = NULL;
393 ParentDevicePath = NULL;
394
395 //
396 // SdPciHcDxe is a device driver, and should ingore the
397 // "RemainingDevicePath" according to EFI spec.
398 //
399 Status = gBS->OpenProtocol (
400 Controller,
401 &gEfiDevicePathProtocolGuid,
402 (VOID *) &ParentDevicePath,
403 This->DriverBindingHandle,
404 Controller,
405 EFI_OPEN_PROTOCOL_BY_DRIVER
406 );
407 if (EFI_ERROR (Status)) {
408 //
409 // EFI_ALREADY_STARTED is also an error.
410 //
411 return Status;
412 }
413 //
414 // Close the protocol because we don't use it here.
415 //
416 gBS->CloseProtocol (
417 Controller,
418 &gEfiDevicePathProtocolGuid,
419 This->DriverBindingHandle,
420 Controller
421 );
422
423 //
424 // Now test the EfiPciIoProtocol.
425 //
426 Status = gBS->OpenProtocol (
427 Controller,
428 &gEfiPciIoProtocolGuid,
429 (VOID **) &PciIo,
430 This->DriverBindingHandle,
431 Controller,
432 EFI_OPEN_PROTOCOL_BY_DRIVER
433 );
434 if (EFI_ERROR (Status)) {
435 return Status;
436 }
437
438 //
439 // Now further check the PCI header: Base class (offset 0x08) and
440 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI
441 // Host Controller.
442 //
443 Status = PciIo->Pci.Read (
444 PciIo,
445 EfiPciIoWidthUint8,
446 0,
447 sizeof (PciData),
448 &PciData
449 );
450 if (EFI_ERROR (Status)) {
451 gBS->CloseProtocol (
452 Controller,
453 &gEfiPciIoProtocolGuid,
454 This->DriverBindingHandle,
455 Controller
456 );
457 return EFI_UNSUPPORTED;
458 }
459 //
460 // Since we already got the PciData, we can close protocol to avoid to carry it
461 // on for multiple exit points.
462 //
463 gBS->CloseProtocol (
464 Controller,
465 &gEfiPciIoProtocolGuid,
466 This->DriverBindingHandle,
467 Controller
468 );
469
470 //
471 // Examine SD PCI Host Controller PCI Configuration table fields.
472 //
473 if ((PciData.Hdr.ClassCode[2] == PCI_CLASS_SYSTEM_PERIPHERAL) &&
474 (PciData.Hdr.ClassCode[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER) &&
475 ((PciData.Hdr.ClassCode[0] == 0x00) || (PciData.Hdr.ClassCode[0] == 0x01))) {
476 return EFI_SUCCESS;
477 }
478
479 return EFI_UNSUPPORTED;
480 }
481
482 /**
483 Starts a device controller or a bus controller.
484
485 The Start() function is designed to be invoked from the EFI boot service ConnectController().
486 As a result, much of the error checking on the parameters to Start() has been moved into this
487 common boot service. It is legal to call Start() from other locations,
488 but the following calling restrictions must be followed or the system behavior will not be deterministic.
489 1. ControllerHandle must be a valid EFI_HANDLE.
490 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
491 EFI_DEVICE_PATH_PROTOCOL.
492 3. Prior to calling Start(), the Supported() function for the driver specified by This must
493 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
494
495 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
496 @param[in] ControllerHandle The handle of the controller to start. This handle
497 must support a protocol interface that supplies
498 an I/O abstraction to the driver.
499 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
500 parameter is ignored by device drivers, and is optional for bus
501 drivers. For a bus driver, if this parameter is NULL, then handles
502 for all the children of Controller are created by this driver.
503 If this parameter is not NULL and the first Device Path Node is
504 not the End of Device Path Node, then only the handle for the
505 child device specified by the first Device Path Node of
506 RemainingDevicePath is created by this driver.
507 If the first Device Path Node of RemainingDevicePath is
508 the End of Device Path Node, no child handle is created by this
509 driver.
510
511 @retval EFI_SUCCESS The device was started.
512 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
513 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
514 @retval Others The driver failded to start the device.
515
516 **/
517 EFI_STATUS
518 EFIAPI
519 SdMmcPciHcDriverBindingStart (
520 IN EFI_DRIVER_BINDING_PROTOCOL *This,
521 IN EFI_HANDLE Controller,
522 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
523 )
524 {
525 EFI_STATUS Status;
526 SD_MMC_HC_PRIVATE_DATA *Private;
527 EFI_PCI_IO_PROTOCOL *PciIo;
528 UINT64 Supports;
529 UINT64 PciAttributes;
530 UINT8 SlotNum;
531 UINT8 FirstBar;
532 UINT8 Slot;
533 UINT8 Index;
534 CARD_TYPE_DETECT_ROUTINE *Routine;
535 UINT32 RoutineNum;
536 BOOLEAN MediaPresent;
537 BOOLEAN Support64BitDma;
538
539 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStart: Start\n"));
540
541 //
542 // Open PCI I/O Protocol and save pointer to open protocol
543 // in private data area.
544 //
545 PciIo = NULL;
546 Status = gBS->OpenProtocol (
547 Controller,
548 &gEfiPciIoProtocolGuid,
549 (VOID **) &PciIo,
550 This->DriverBindingHandle,
551 Controller,
552 EFI_OPEN_PROTOCOL_BY_DRIVER
553 );
554 if (EFI_ERROR (Status)) {
555 return Status;
556 }
557
558 //
559 // Enable the SD Host Controller MMIO space
560 //
561 Private = NULL;
562 Status = PciIo->Attributes (
563 PciIo,
564 EfiPciIoAttributeOperationGet,
565 0,
566 &PciAttributes
567 );
568
569 if (EFI_ERROR (Status)) {
570 goto Done;
571 }
572
573 Status = PciIo->Attributes (
574 PciIo,
575 EfiPciIoAttributeOperationSupported,
576 0,
577 &Supports
578 );
579
580 if (!EFI_ERROR (Status)) {
581 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;
582 Status = PciIo->Attributes (
583 PciIo,
584 EfiPciIoAttributeOperationEnable,
585 Supports,
586 NULL
587 );
588 } else {
589 goto Done;
590 }
591
592 Private = AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA), &gSdMmcPciHcTemplate);
593 if (Private == NULL) {
594 Status = EFI_OUT_OF_RESOURCES;
595 goto Done;
596 }
597
598 Private->ControllerHandle = Controller;
599 Private->PciIo = PciIo;
600 Private->PciAttributes = PciAttributes;
601 InitializeListHead (&Private->Queue);
602
603 //
604 // Get SD/MMC Pci Host Controller Slot info
605 //
606 Status = SdMmcHcGetSlotInfo (PciIo, &FirstBar, &SlotNum);
607 if (EFI_ERROR (Status)) {
608 goto Done;
609 }
610
611 //
612 // Attempt to locate the singleton instance of the SD/MMC override protocol,
613 // which implements platform specific workarounds for non-standard SDHCI
614 // implementations.
615 //
616 if (mOverride == NULL) {
617 Status = gBS->LocateProtocol (&gEdkiiSdMmcOverrideProtocolGuid, NULL,
618 (VOID **)&mOverride);
619 if (!EFI_ERROR (Status)) {
620 DEBUG ((DEBUG_INFO, "%a: found SD/MMC override protocol\n",
621 __FUNCTION__));
622 }
623 }
624
625 Support64BitDma = TRUE;
626 for (Slot = FirstBar; Slot < (FirstBar + SlotNum); Slot++) {
627 Private->Slot[Slot].Enable = TRUE;
628
629 //
630 // Get SD/MMC Pci Host Controller Version
631 //
632 Status = SdMmcHcGetControllerVersion (PciIo, Slot, &Private->ControllerVersion[Slot]);
633 if (EFI_ERROR (Status)) {
634 continue;
635 }
636
637 Status = SdMmcHcGetCapability (PciIo, Slot, &Private->Capability[Slot]);
638 if (EFI_ERROR (Status)) {
639 continue;
640 }
641
642 Private->BaseClkFreq[Slot] = Private->Capability[Slot].BaseClkFreq;
643
644 if (mOverride != NULL && mOverride->Capability != NULL) {
645 Status = mOverride->Capability (
646 Controller,
647 Slot,
648 &Private->Capability[Slot],
649 &Private->BaseClkFreq[Slot]
650 );
651 if (EFI_ERROR (Status)) {
652 DEBUG ((DEBUG_WARN, "%a: Failed to override capability - %r\n",
653 __FUNCTION__, Status));
654 continue;
655 }
656 }
657 DumpCapabilityReg (Slot, &Private->Capability[Slot]);
658 DEBUG ((
659 DEBUG_INFO,
660 "Slot[%d] Base Clock Frequency: %dMHz\n",
661 Slot,
662 Private->BaseClkFreq[Slot]
663 ));
664
665 //
666 // If any of the slots does not support 64b system bus
667 // do not enable 64b DMA in the PCI layer.
668 //
669 if (Private->Capability[Slot].SysBus64V3 == 0 &&
670 Private->Capability[Slot].SysBus64V4 == 0) {
671 Support64BitDma = FALSE;
672 }
673
674 Status = SdMmcHcGetMaxCurrent (PciIo, Slot, &Private->MaxCurrent[Slot]);
675 if (EFI_ERROR (Status)) {
676 continue;
677 }
678
679 Private->Slot[Slot].SlotType = Private->Capability[Slot].SlotType;
680 if ((Private->Slot[Slot].SlotType != RemovableSlot) && (Private->Slot[Slot].SlotType != EmbeddedSlot)) {
681 DEBUG ((DEBUG_INFO, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private->Slot[Slot].SlotType));
682 continue;
683 }
684
685 //
686 // Reset the specified slot of the SD/MMC Pci Host Controller
687 //
688 Status = SdMmcHcReset (Private, Slot);
689 if (EFI_ERROR (Status)) {
690 continue;
691 }
692 //
693 // Check whether there is a SD/MMC card attached
694 //
695 if (Private->Slot[Slot].SlotType == RemovableSlot) {
696 Status = SdMmcHcCardDetect (PciIo, Slot, &MediaPresent);
697 if (EFI_ERROR (Status) && (Status != EFI_MEDIA_CHANGED)) {
698 continue;
699 } else if (!MediaPresent) {
700 DEBUG ((
701 DEBUG_INFO,
702 "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n",
703 Slot
704 ));
705 continue;
706 }
707 }
708
709 Status = SdMmcHcInitHost (Private, Slot);
710 if (EFI_ERROR (Status)) {
711 continue;
712 }
713
714 Private->Slot[Slot].MediaPresent = TRUE;
715 Private->Slot[Slot].Initialized = TRUE;
716 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);
717 for (Index = 0; Index < RoutineNum; Index++) {
718 Routine = &mCardTypeDetectRoutineTable[Index];
719 if (*Routine != NULL) {
720 Status = (*Routine) (Private, Slot);
721 if (!EFI_ERROR (Status)) {
722 break;
723 }
724 }
725 }
726 //
727 // This card doesn't get initialized correctly.
728 //
729 if (Index == RoutineNum) {
730 Private->Slot[Slot].Initialized = FALSE;
731 }
732 }
733
734 //
735 // Enable 64-bit DMA support in the PCI layer if this controller
736 // supports it.
737 //
738 if (Support64BitDma) {
739 Status = PciIo->Attributes (
740 PciIo,
741 EfiPciIoAttributeOperationEnable,
742 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE,
743 NULL
744 );
745 if (EFI_ERROR (Status)) {
746 DEBUG ((DEBUG_WARN, "SdMmcPciHcDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status));
747 }
748 }
749
750 //
751 // Start the asynchronous I/O monitor
752 //
753 Status = gBS->CreateEvent (
754 EVT_TIMER | EVT_NOTIFY_SIGNAL,
755 TPL_NOTIFY,
756 ProcessAsyncTaskList,
757 Private,
758 &Private->TimerEvent
759 );
760 if (EFI_ERROR (Status)) {
761 goto Done;
762 }
763
764 Status = gBS->SetTimer (Private->TimerEvent, TimerPeriodic, SD_MMC_HC_ASYNC_TIMER);
765 if (EFI_ERROR (Status)) {
766 goto Done;
767 }
768
769 //
770 // Start the Sd removable device connection enumeration
771 //
772 Status = gBS->CreateEvent (
773 EVT_TIMER | EVT_NOTIFY_SIGNAL,
774 TPL_CALLBACK,
775 SdMmcPciHcEnumerateDevice,
776 Private,
777 &Private->ConnectEvent
778 );
779 if (EFI_ERROR (Status)) {
780 goto Done;
781 }
782
783 Status = gBS->SetTimer (Private->ConnectEvent, TimerPeriodic, SD_MMC_HC_ENUM_TIMER);
784 if (EFI_ERROR (Status)) {
785 goto Done;
786 }
787
788 Status = gBS->InstallMultipleProtocolInterfaces (
789 &Controller,
790 &gEfiSdMmcPassThruProtocolGuid,
791 &(Private->PassThru),
792 NULL
793 );
794
795 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status, Controller));
796
797 Done:
798 if (EFI_ERROR (Status)) {
799 if ((Private != NULL) && (Private->PciAttributes != 0)) {
800 //
801 // Restore original PCI attributes
802 //
803 PciIo->Attributes (
804 PciIo,
805 EfiPciIoAttributeOperationSet,
806 Private->PciAttributes,
807 NULL
808 );
809 }
810 gBS->CloseProtocol (
811 Controller,
812 &gEfiPciIoProtocolGuid,
813 This->DriverBindingHandle,
814 Controller
815 );
816
817 if ((Private != NULL) && (Private->TimerEvent != NULL)) {
818 gBS->CloseEvent (Private->TimerEvent);
819 }
820
821 if ((Private != NULL) && (Private->ConnectEvent != NULL)) {
822 gBS->CloseEvent (Private->ConnectEvent);
823 }
824
825 if (Private != NULL) {
826 FreePool (Private);
827 }
828 }
829
830 return Status;
831 }
832
833 /**
834 Stops a device controller or a bus controller.
835
836 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
837 As a result, much of the error checking on the parameters to Stop() has been moved
838 into this common boot service. It is legal to call Stop() from other locations,
839 but the following calling restrictions must be followed or the system behavior will not be deterministic.
840 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
841 same driver's Start() function.
842 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
843 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
844 Start() function, and the Start() function must have called OpenProtocol() on
845 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
846
847 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
848 @param[in] ControllerHandle A handle to the device being stopped. The handle must
849 support a bus specific I/O protocol for the driver
850 to use to stop the device.
851 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
852 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
853 if NumberOfChildren is 0.
854
855 @retval EFI_SUCCESS The device was stopped.
856 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
857
858 **/
859 EFI_STATUS
860 EFIAPI
861 SdMmcPciHcDriverBindingStop (
862 IN EFI_DRIVER_BINDING_PROTOCOL *This,
863 IN EFI_HANDLE Controller,
864 IN UINTN NumberOfChildren,
865 IN EFI_HANDLE *ChildHandleBuffer
866 )
867 {
868 EFI_STATUS Status;
869 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;
870 SD_MMC_HC_PRIVATE_DATA *Private;
871 EFI_PCI_IO_PROTOCOL *PciIo;
872 LIST_ENTRY *Link;
873 LIST_ENTRY *NextLink;
874 SD_MMC_HC_TRB *Trb;
875
876 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStop: Start\n"));
877
878 Status = gBS->OpenProtocol (
879 Controller,
880 &gEfiSdMmcPassThruProtocolGuid,
881 (VOID**) &PassThru,
882 This->DriverBindingHandle,
883 Controller,
884 EFI_OPEN_PROTOCOL_GET_PROTOCOL
885 );
886 if (EFI_ERROR (Status)) {
887 return Status;
888 }
889
890 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);
891 //
892 // Close Non-Blocking timer and free Task list.
893 //
894 if (Private->TimerEvent != NULL) {
895 gBS->CloseEvent (Private->TimerEvent);
896 Private->TimerEvent = NULL;
897 }
898 if (Private->ConnectEvent != NULL) {
899 gBS->CloseEvent (Private->ConnectEvent);
900 Private->ConnectEvent = NULL;
901 }
902 //
903 // As the timer is closed, there is no needs to use TPL lock to
904 // protect the critical region "queue".
905 //
906 for (Link = GetFirstNode (&Private->Queue);
907 !IsNull (&Private->Queue, Link);
908 Link = NextLink) {
909 NextLink = GetNextNode (&Private->Queue, Link);
910 RemoveEntryList (Link);
911 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);
912 Trb->Packet->TransactionStatus = EFI_ABORTED;
913 gBS->SignalEvent (Trb->Event);
914 SdMmcFreeTrb (Trb);
915 }
916
917 //
918 // Uninstall Block I/O protocol from the device handle
919 //
920 Status = gBS->UninstallProtocolInterface (
921 Controller,
922 &gEfiSdMmcPassThruProtocolGuid,
923 &(Private->PassThru)
924 );
925
926 if (EFI_ERROR (Status)) {
927 return Status;
928 }
929
930 gBS->CloseProtocol (
931 Controller,
932 &gEfiPciIoProtocolGuid,
933 This->DriverBindingHandle,
934 Controller
935 );
936 //
937 // Restore original PCI attributes
938 //
939 PciIo = Private->PciIo;
940 Status = PciIo->Attributes (
941 PciIo,
942 EfiPciIoAttributeOperationSet,
943 Private->PciAttributes,
944 NULL
945 );
946 ASSERT_EFI_ERROR (Status);
947
948 FreePool (Private);
949
950 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStop: End with %r\n", Status));
951
952 return Status;
953 }
954
955 /**
956 Sends SD command to an SD card that is attached to the SD controller.
957
958 The PassThru() function sends the SD command specified by Packet to the SD card
959 specified by Slot.
960
961 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.
962
963 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.
964
965 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER
966 is returned.
967
968 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,
969 EFI_INVALID_PARAMETER is returned.
970
971 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
972 @param[in] Slot The slot number of the SD card to send the command to.
973 @param[in,out] Packet A pointer to the SD command data structure.
974 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is
975 not NULL, then nonblocking I/O is performed, and Event
976 will be signaled when the Packet completes.
977
978 @retval EFI_SUCCESS The SD Command Packet was sent by the host.
979 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD
980 command Packet.
981 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.
982 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and
983 OutDataBuffer are NULL.
984 @retval EFI_NO_MEDIA SD Device not present in the Slot.
985 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not
986 supported by the host controller.
987 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the
988 limit supported by SD card ( i.e. if the number of bytes
989 exceed the Last LBA).
990
991 **/
992 EFI_STATUS
993 EFIAPI
994 SdMmcPassThruPassThru (
995 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,
996 IN UINT8 Slot,
997 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet,
998 IN EFI_EVENT Event OPTIONAL
999 )
1000 {
1001 EFI_STATUS Status;
1002 SD_MMC_HC_PRIVATE_DATA *Private;
1003 SD_MMC_HC_TRB *Trb;
1004 EFI_TPL OldTpl;
1005
1006 if ((This == NULL) || (Packet == NULL)) {
1007 return EFI_INVALID_PARAMETER;
1008 }
1009
1010 if ((Packet->SdMmcCmdBlk == NULL) || (Packet->SdMmcStatusBlk == NULL)) {
1011 return EFI_INVALID_PARAMETER;
1012 }
1013
1014 if ((Packet->OutDataBuffer == NULL) && (Packet->OutTransferLength != 0)) {
1015 return EFI_INVALID_PARAMETER;
1016 }
1017
1018 if ((Packet->InDataBuffer == NULL) && (Packet->InTransferLength != 0)) {
1019 return EFI_INVALID_PARAMETER;
1020 }
1021
1022 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);
1023
1024 if (!Private->Slot[Slot].Enable) {
1025 return EFI_INVALID_PARAMETER;
1026 }
1027
1028 if (!Private->Slot[Slot].MediaPresent) {
1029 return EFI_NO_MEDIA;
1030 }
1031
1032 if (!Private->Slot[Slot].Initialized) {
1033 return EFI_DEVICE_ERROR;
1034 }
1035
1036 Trb = SdMmcCreateTrb (Private, Slot, Packet, Event);
1037 if (Trb == NULL) {
1038 return EFI_OUT_OF_RESOURCES;
1039 }
1040 //
1041 // Immediately return for async I/O.
1042 //
1043 if (Event != NULL) {
1044 return EFI_SUCCESS;
1045 }
1046
1047 //
1048 // Wait async I/O list is empty before execute sync I/O operation.
1049 //
1050 while (TRUE) {
1051 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1052 if (IsListEmpty (&Private->Queue)) {
1053 gBS->RestoreTPL (OldTpl);
1054 break;
1055 }
1056 gBS->RestoreTPL (OldTpl);
1057 }
1058
1059 Status = SdMmcWaitTrbEnv (Private, Trb);
1060 if (EFI_ERROR (Status)) {
1061 goto Done;
1062 }
1063
1064 Status = SdMmcExecTrb (Private, Trb);
1065 if (EFI_ERROR (Status)) {
1066 goto Done;
1067 }
1068
1069 Status = SdMmcWaitTrbResult (Private, Trb);
1070 if (EFI_ERROR (Status)) {
1071 goto Done;
1072 }
1073
1074 Done:
1075 SdMmcFreeTrb (Trb);
1076
1077 return Status;
1078 }
1079
1080 /**
1081 Used to retrieve next slot numbers supported by the SD controller. The function
1082 returns information about all available slots (populated or not-populated).
1083
1084 The GetNextSlot() function retrieves the next slot number on an SD controller.
1085 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller
1086 is returned.
1087
1088 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then
1089 the slot number of the next slot on the SD controller is returned.
1090
1091 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),
1092 EFI_INVALID_PARAMETER is returned.
1093
1094 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND
1095 is returned.
1096
1097 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1098 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.
1099 On output, a pointer to the next slot number on the SD controller.
1100 An input value of 0xFF retrieves the first slot number on the SD
1101 controller.
1102
1103 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.
1104 @retval EFI_NOT_FOUND There are no more slots on this SD controller.
1105 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call
1106 to GetNextSlot().
1107
1108 **/
1109 EFI_STATUS
1110 EFIAPI
1111 SdMmcPassThruGetNextSlot (
1112 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,
1113 IN OUT UINT8 *Slot
1114 )
1115 {
1116 SD_MMC_HC_PRIVATE_DATA *Private;
1117 UINT8 Index;
1118
1119 if ((This == NULL) || (Slot == NULL)) {
1120 return EFI_INVALID_PARAMETER;
1121 }
1122
1123 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);
1124
1125 if (*Slot == 0xFF) {
1126 for (Index = 0; Index < SD_MMC_HC_MAX_SLOT; Index++) {
1127 if (Private->Slot[Index].Enable) {
1128 *Slot = Index;
1129 Private->PreviousSlot = Index;
1130 return EFI_SUCCESS;
1131 }
1132 }
1133 return EFI_NOT_FOUND;
1134 } else if (*Slot == Private->PreviousSlot) {
1135 for (Index = *Slot + 1; Index < SD_MMC_HC_MAX_SLOT; Index++) {
1136 if (Private->Slot[Index].Enable) {
1137 *Slot = Index;
1138 Private->PreviousSlot = Index;
1139 return EFI_SUCCESS;
1140 }
1141 }
1142 return EFI_NOT_FOUND;
1143 } else {
1144 return EFI_INVALID_PARAMETER;
1145 }
1146 }
1147
1148 /**
1149 Used to allocate and build a device path node for an SD card on the SD controller.
1150
1151 The BuildDevicePath() function allocates and builds a single device node for the SD
1152 card specified by Slot.
1153
1154 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND
1155 is returned.
1156
1157 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.
1158
1159 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES
1160 is returned.
1161
1162 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1163 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is
1164 returned.
1165
1166 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.
1167 @param[in] Slot Specifies the slot number of the SD card for which a device
1168 path node is to be allocated and built.
1169 @param[in,out] DevicePath A pointer to a single device path node that describes the SD
1170 card specified by Slot. This function is responsible for
1171 allocating the buffer DevicePath with the boot service
1172 AllocatePool(). It is the caller's responsibility to free
1173 DevicePath when the caller is finished with DevicePath.
1174
1175 @retval EFI_SUCCESS The device path node that describes the SD card specified by
1176 Slot was allocated and returned in DevicePath.
1177 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.
1178 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1179 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1180
1181 **/
1182 EFI_STATUS
1183 EFIAPI
1184 SdMmcPassThruBuildDevicePath (
1185 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,
1186 IN UINT8 Slot,
1187 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
1188 )
1189 {
1190 SD_MMC_HC_PRIVATE_DATA *Private;
1191 SD_DEVICE_PATH *SdNode;
1192 EMMC_DEVICE_PATH *EmmcNode;
1193
1194 if ((This == NULL) || (DevicePath == NULL) || (Slot >= SD_MMC_HC_MAX_SLOT)) {
1195 return EFI_INVALID_PARAMETER;
1196 }
1197
1198 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);
1199
1200 if ((!Private->Slot[Slot].Enable) || (!Private->Slot[Slot].MediaPresent)) {
1201 return EFI_NOT_FOUND;
1202 }
1203
1204 if (Private->Slot[Slot].CardType == SdCardType) {
1205 SdNode = AllocateCopyPool (sizeof (SD_DEVICE_PATH), &mSdDpTemplate);
1206 if (SdNode == NULL) {
1207 return EFI_OUT_OF_RESOURCES;
1208 }
1209 SdNode->SlotNumber = Slot;
1210
1211 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) SdNode;
1212 } else if (Private->Slot[Slot].CardType == EmmcCardType) {
1213 EmmcNode = AllocateCopyPool (sizeof (EMMC_DEVICE_PATH), &mEmmcDpTemplate);
1214 if (EmmcNode == NULL) {
1215 return EFI_OUT_OF_RESOURCES;
1216 }
1217 EmmcNode->SlotNumber = Slot;
1218
1219 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) EmmcNode;
1220 } else {
1221 //
1222 // Currently we only support SD and EMMC two device nodes.
1223 //
1224 return EFI_NOT_FOUND;
1225 }
1226
1227 return EFI_SUCCESS;
1228 }
1229
1230 /**
1231 This function retrieves an SD card slot number based on the input device path.
1232
1233 The GetSlotNumber() function retrieves slot number for the SD card specified by
1234 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.
1235
1236 If DevicePath is not a device path node type that the SD Pass Thru driver supports,
1237 EFI_UNSUPPORTED is returned.
1238
1239 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1240 @param[in] DevicePath A pointer to the device path node that describes a SD
1241 card on the SD controller.
1242 @param[out] Slot On return, points to the slot number of an SD card on
1243 the SD controller.
1244
1245 @retval EFI_SUCCESS SD card slot number is returned in Slot.
1246 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.
1247 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD
1248 Pass Thru driver supports.
1249
1250 **/
1251 EFI_STATUS
1252 EFIAPI
1253 SdMmcPassThruGetSlotNumber (
1254 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,
1255 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
1256 OUT UINT8 *Slot
1257 )
1258 {
1259 SD_MMC_HC_PRIVATE_DATA *Private;
1260 SD_DEVICE_PATH *SdNode;
1261 EMMC_DEVICE_PATH *EmmcNode;
1262 UINT8 SlotNumber;
1263
1264 if ((This == NULL) || (DevicePath == NULL) || (Slot == NULL)) {
1265 return EFI_INVALID_PARAMETER;
1266 }
1267
1268 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);
1269
1270 //
1271 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH
1272 //
1273 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||
1274 ((DevicePath->SubType != MSG_SD_DP) &&
1275 (DevicePath->SubType != MSG_EMMC_DP)) ||
1276 (DevicePathNodeLength(DevicePath) != sizeof(SD_DEVICE_PATH)) ||
1277 (DevicePathNodeLength(DevicePath) != sizeof(EMMC_DEVICE_PATH))) {
1278 return EFI_UNSUPPORTED;
1279 }
1280
1281 if (DevicePath->SubType == MSG_SD_DP) {
1282 SdNode = (SD_DEVICE_PATH *) DevicePath;
1283 SlotNumber = SdNode->SlotNumber;
1284 } else {
1285 EmmcNode = (EMMC_DEVICE_PATH *) DevicePath;
1286 SlotNumber = EmmcNode->SlotNumber;
1287 }
1288
1289 if (SlotNumber >= SD_MMC_HC_MAX_SLOT) {
1290 return EFI_NOT_FOUND;
1291 }
1292
1293 if (Private->Slot[SlotNumber].Enable) {
1294 *Slot = SlotNumber;
1295 return EFI_SUCCESS;
1296 } else {
1297 return EFI_NOT_FOUND;
1298 }
1299 }
1300
1301 /**
1302 Resets an SD card that is connected to the SD controller.
1303
1304 The ResetDevice() function resets the SD card specified by Slot.
1305
1306 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is
1307 returned.
1308
1309 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER
1310 is returned.
1311
1312 If the device reset operation is completed, EFI_SUCCESS is returned.
1313
1314 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.
1315 @param[in] Slot Specifies the slot number of the SD card to be reset.
1316
1317 @retval EFI_SUCCESS The SD card specified by Slot was reset.
1318 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.
1319 @retval EFI_INVALID_PARAMETER Slot number is invalid.
1320 @retval EFI_NO_MEDIA SD Device not present in the Slot.
1321 @retval EFI_DEVICE_ERROR The reset command failed due to a device error
1322
1323 **/
1324 EFI_STATUS
1325 EFIAPI
1326 SdMmcPassThruResetDevice (
1327 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,
1328 IN UINT8 Slot
1329 )
1330 {
1331 SD_MMC_HC_PRIVATE_DATA *Private;
1332 LIST_ENTRY *Link;
1333 LIST_ENTRY *NextLink;
1334 SD_MMC_HC_TRB *Trb;
1335 EFI_TPL OldTpl;
1336
1337 if (This == NULL) {
1338 return EFI_INVALID_PARAMETER;
1339 }
1340
1341 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);
1342
1343 if (!Private->Slot[Slot].Enable) {
1344 return EFI_INVALID_PARAMETER;
1345 }
1346
1347 if (!Private->Slot[Slot].MediaPresent) {
1348 return EFI_NO_MEDIA;
1349 }
1350
1351 if (!Private->Slot[Slot].Initialized) {
1352 return EFI_DEVICE_ERROR;
1353 }
1354 //
1355 // Free all async I/O requests in the queue
1356 //
1357 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1358
1359 for (Link = GetFirstNode (&Private->Queue);
1360 !IsNull (&Private->Queue, Link);
1361 Link = NextLink) {
1362 NextLink = GetNextNode (&Private->Queue, Link);
1363 RemoveEntryList (Link);
1364 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);
1365 Trb->Packet->TransactionStatus = EFI_ABORTED;
1366 gBS->SignalEvent (Trb->Event);
1367 SdMmcFreeTrb (Trb);
1368 }
1369
1370 gBS->RestoreTPL (OldTpl);
1371
1372 return EFI_SUCCESS;
1373 }
1374