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