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