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