]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHcDxe.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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
48190274
HW
3 with SD Host Controller Simplified Specification version 3.00 plus the 64-bit\r
4 System Addressing support in SD Host Controller Simplified Specification version\r
5 4.20.\r
48555339
FT
6\r
7 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.\r
8\r
690d60c0 9 Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.\r
48190274 10 Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>\r
9d510e61 11 SPDX-License-Identifier: BSD-2-Clause-Patent\r
48555339
FT
12\r
13**/\r
14\r
15#include "SdMmcPciHcDxe.h"\r
16\r
b23fc39c
AB
17EDKII_SD_MMC_OVERRIDE *mOverride;\r
18\r
48555339
FT
19//\r
20// Driver Global Variables\r
21//\r
22EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding = {\r
23 SdMmcPciHcDriverBindingSupported,\r
24 SdMmcPciHcDriverBindingStart,\r
25 SdMmcPciHcDriverBindingStop,\r
26 0x10,\r
27 NULL,\r
28 NULL\r
29};\r
30\r
31//\r
32// Template for SD/MMC host controller private data.\r
33//\r
34SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate = {\r
35 SD_MMC_HC_PRIVATE_SIGNATURE, // Signature\r
36 NULL, // ControllerHandle\r
37 NULL, // PciIo\r
38 { // PassThru\r
39 sizeof (UINT32),\r
40 SdMmcPassThruPassThru,\r
41 SdMmcPassThruGetNextSlot,\r
42 SdMmcPassThruBuildDevicePath,\r
43 SdMmcPassThruGetSlotNumber,\r
44 SdMmcPassThruResetDevice\r
45 },\r
46 0, // PciAttributes\r
47 0, // PreviousSlot\r
48 NULL, // TimerEvent\r
49 NULL, // ConnectEvent\r
50 // Queue\r
51 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate.Queue),\r
52 { // Slot\r
c25ddd01
FT
53 {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0},\r
54 {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}, {0, UnknownSlot, 0, 0, 0}\r
48555339
FT
55 },\r
56 { // Capability\r
57 {0},\r
58 },\r
59 { // MaxCurrent\r
60 0,\r
61 },\r
b5547b9c
AS
62 {\r
63 0 // ControllerVersion\r
64 }\r
48555339
FT
65};\r
66\r
67SD_DEVICE_PATH mSdDpTemplate = {\r
68 {\r
69 MESSAGING_DEVICE_PATH,\r
70 MSG_SD_DP,\r
71 {\r
72 (UINT8) (sizeof (SD_DEVICE_PATH)),\r
73 (UINT8) ((sizeof (SD_DEVICE_PATH)) >> 8)\r
74 }\r
75 },\r
76 0\r
77};\r
78\r
79EMMC_DEVICE_PATH mEmmcDpTemplate = {\r
80 {\r
81 MESSAGING_DEVICE_PATH,\r
82 MSG_EMMC_DP,\r
83 {\r
84 (UINT8) (sizeof (EMMC_DEVICE_PATH)),\r
85 (UINT8) ((sizeof (EMMC_DEVICE_PATH)) >> 8)\r
86 }\r
87 },\r
88 0\r
89};\r
90\r
91//\r
92// Prioritized function list to detect card type.\r
93// User could add other card detection logic here.\r
94//\r
95CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable[] = {\r
96 EmmcIdentification,\r
97 SdCardIdentification,\r
98 NULL\r
99};\r
100\r
101/**\r
102 The entry point for SD host controller driver, used to install this driver on the ImageHandle.\r
103\r
104 @param[in] ImageHandle The firmware allocated handle for this driver image.\r
105 @param[in] SystemTable Pointer to the EFI system table.\r
106\r
107 @retval EFI_SUCCESS Driver loaded.\r
108 @retval other Driver not loaded.\r
109\r
110**/\r
111EFI_STATUS\r
112EFIAPI\r
113InitializeSdMmcPciHcDxe (\r
114 IN EFI_HANDLE ImageHandle,\r
115 IN EFI_SYSTEM_TABLE *SystemTable\r
116 )\r
117{\r
118 EFI_STATUS Status;\r
119\r
120 Status = EfiLibInstallDriverBindingComponentName2 (\r
121 ImageHandle,\r
122 SystemTable,\r
123 &gSdMmcPciHcDriverBinding,\r
124 ImageHandle,\r
125 &gSdMmcPciHcComponentName,\r
126 &gSdMmcPciHcComponentName2\r
127 );\r
128 ASSERT_EFI_ERROR (Status);\r
129\r
130 return Status;\r
131}\r
132\r
133/**\r
134 Call back function when the timer event is signaled.\r
135\r
136 @param[in] Event The Event this notify function registered to.\r
137 @param[in] Context Pointer to the context data registered to the\r
138 Event.\r
139\r
140**/\r
141VOID\r
142EFIAPI\r
143ProcessAsyncTaskList (\r
144 IN EFI_EVENT Event,\r
145 IN VOID* Context\r
146 )\r
147{\r
148 SD_MMC_HC_PRIVATE_DATA *Private;\r
149 LIST_ENTRY *Link;\r
150 SD_MMC_HC_TRB *Trb;\r
151 EFI_STATUS Status;\r
152 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r
153 BOOLEAN InfiniteWait;\r
154 EFI_EVENT TrbEvent;\r
155\r
156 Private = (SD_MMC_HC_PRIVATE_DATA*)Context;\r
157\r
158 //\r
159 // Check if the first entry in the async I/O queue is done or not.\r
160 //\r
161 Status = EFI_SUCCESS;\r
162 Trb = NULL;\r
163 Link = GetFirstNode (&Private->Queue);\r
164 if (!IsNull (&Private->Queue, Link)) {\r
165 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
e26575f7 166 if (!Private->Slot[Trb->Slot].MediaPresent) {\r
48555339
FT
167 Status = EFI_NO_MEDIA;\r
168 goto Done;\r
169 }\r
170 if (!Trb->Started) {\r
171 //\r
172 // Check whether the cmd/data line is ready for transfer.\r
173 //\r
174 Status = SdMmcCheckTrbEnv (Private, Trb);\r
175 if (!EFI_ERROR (Status)) {\r
176 Trb->Started = TRUE;\r
177 Status = SdMmcExecTrb (Private, Trb);\r
178 if (EFI_ERROR (Status)) {\r
179 goto Done;\r
180 }\r
181 } else {\r
182 goto Done;\r
183 }\r
184 }\r
185 Status = SdMmcCheckTrbResult (Private, Trb);\r
186 }\r
187\r
188Done:\r
189 if ((Trb != NULL) && (Status == EFI_NOT_READY)) {\r
190 Packet = Trb->Packet;\r
191 if (Packet->Timeout == 0) {\r
192 InfiniteWait = TRUE;\r
193 } else {\r
194 InfiniteWait = FALSE;\r
195 }\r
196 if ((!InfiniteWait) && (Trb->Timeout-- == 0)) {\r
197 RemoveEntryList (Link);\r
198 Trb->Packet->TransactionStatus = EFI_TIMEOUT;\r
199 TrbEvent = Trb->Event;\r
200 SdMmcFreeTrb (Trb);\r
e27ccaba 201 DEBUG ((DEBUG_VERBOSE, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent));\r
48555339
FT
202 gBS->SignalEvent (TrbEvent);\r
203 return;\r
204 }\r
205 }\r
206 if ((Trb != NULL) && (Status != EFI_NOT_READY)) {\r
207 RemoveEntryList (Link);\r
208 Trb->Packet->TransactionStatus = Status;\r
209 TrbEvent = Trb->Event;\r
210 SdMmcFreeTrb (Trb);\r
e27ccaba 211 DEBUG ((DEBUG_VERBOSE, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent, Status));\r
48555339
FT
212 gBS->SignalEvent (TrbEvent);\r
213 }\r
214 return;\r
215}\r
216\r
217/**\r
218 Sd removable device enumeration callback function when the timer event is signaled.\r
219\r
220 @param[in] Event The Event this notify function registered to.\r
221 @param[in] Context Pointer to the context data registered to the\r
222 Event.\r
223\r
224**/\r
225VOID\r
226EFIAPI\r
227SdMmcPciHcEnumerateDevice (\r
228 IN EFI_EVENT Event,\r
229 IN VOID* Context\r
230 )\r
231{\r
232 SD_MMC_HC_PRIVATE_DATA *Private;\r
233 EFI_STATUS Status;\r
234 UINT8 Slot;\r
235 BOOLEAN MediaPresent;\r
236 UINT32 RoutineNum;\r
237 CARD_TYPE_DETECT_ROUTINE *Routine;\r
238 UINTN Index;\r
239 LIST_ENTRY *Link;\r
240 LIST_ENTRY *NextLink;\r
241 SD_MMC_HC_TRB *Trb;\r
3b1d8241 242 EFI_TPL OldTpl;\r
48555339
FT
243\r
244 Private = (SD_MMC_HC_PRIVATE_DATA*)Context;\r
245\r
246 for (Slot = 0; Slot < SD_MMC_HC_MAX_SLOT; Slot++) {\r
247 if ((Private->Slot[Slot].Enable) && (Private->Slot[Slot].SlotType == RemovableSlot)) {\r
248 Status = SdMmcHcCardDetect (Private->PciIo, Slot, &MediaPresent);\r
e26575f7 249 if ((Status == EFI_MEDIA_CHANGED) && !MediaPresent) {\r
e27ccaba 250 DEBUG ((DEBUG_INFO, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot, Private->PciIo));\r
48555339 251 Private->Slot[Slot].MediaPresent = FALSE;\r
c25ddd01 252 Private->Slot[Slot].Initialized = FALSE;\r
48555339
FT
253 //\r
254 // Signal all async task events at the slot with EFI_NO_MEDIA status.\r
255 //\r
3b1d8241 256 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
48555339
FT
257 for (Link = GetFirstNode (&Private->Queue);\r
258 !IsNull (&Private->Queue, Link);\r
259 Link = NextLink) {\r
260 NextLink = GetNextNode (&Private->Queue, Link);\r
261 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
262 if (Trb->Slot == Slot) {\r
263 RemoveEntryList (Link);\r
264 Trb->Packet->TransactionStatus = EFI_NO_MEDIA;\r
265 gBS->SignalEvent (Trb->Event);\r
266 SdMmcFreeTrb (Trb);\r
267 }\r
268 }\r
3b1d8241 269 gBS->RestoreTPL (OldTpl);\r
48555339
FT
270 //\r
271 // Notify the upper layer the connect state change through ReinstallProtocolInterface.\r
272 //\r
273 gBS->ReinstallProtocolInterface (\r
274 Private->ControllerHandle,\r
275 &gEfiSdMmcPassThruProtocolGuid,\r
276 &Private->PassThru,\r
277 &Private->PassThru\r
278 );\r
279 }\r
e26575f7 280 if ((Status == EFI_MEDIA_CHANGED) && MediaPresent) {\r
e27ccaba 281 DEBUG ((DEBUG_INFO, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot, Private->PciIo));\r
48555339 282 //\r
83ceccab
FT
283 // Reset the specified slot of the SD/MMC Pci Host Controller\r
284 //\r
b23fc39c 285 Status = SdMmcHcReset (Private, Slot);\r
83ceccab
FT
286 if (EFI_ERROR (Status)) {\r
287 continue;\r
288 }\r
289 //\r
48555339
FT
290 // Reinitialize slot and restart identification process for the new attached device\r
291 //\r
b23fc39c 292 Status = SdMmcHcInitHost (Private, Slot);\r
48555339
FT
293 if (EFI_ERROR (Status)) {\r
294 continue;\r
295 }\r
296\r
297 Private->Slot[Slot].MediaPresent = TRUE;\r
c25ddd01 298 Private->Slot[Slot].Initialized = TRUE;\r
48555339
FT
299 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);\r
300 for (Index = 0; Index < RoutineNum; Index++) {\r
301 Routine = &mCardTypeDetectRoutineTable[Index];\r
302 if (*Routine != NULL) {\r
303 Status = (*Routine) (Private, Slot);\r
304 if (!EFI_ERROR (Status)) {\r
305 break;\r
306 }\r
307 }\r
308 }\r
c25ddd01
FT
309 //\r
310 // This card doesn't get initialized correctly.\r
311 //\r
312 if (Index == RoutineNum) {\r
313 Private->Slot[Slot].Initialized = FALSE;\r
314 }\r
48555339
FT
315\r
316 //\r
317 // Notify the upper layer the connect state change through ReinstallProtocolInterface.\r
318 //\r
319 gBS->ReinstallProtocolInterface (\r
320 Private->ControllerHandle,\r
321 &gEfiSdMmcPassThruProtocolGuid,\r
322 &Private->PassThru,\r
323 &Private->PassThru\r
324 );\r
325 }\r
326 }\r
327 }\r
328\r
329 return;\r
330}\r
331/**\r
332 Tests to see if this driver supports a given controller. If a child device is provided,\r
333 it further tests to see if this driver supports creating a handle for the specified child device.\r
334\r
335 This function checks to see if the driver specified by This supports the device specified by\r
336 ControllerHandle. Drivers will typically use the device path attached to\r
337 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
338 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
339 may be called many times during platform initialization. In order to reduce boot times, the tests\r
340 performed by this function must be very small, and take as little time as possible to execute. This\r
341 function must not change the state of any hardware devices, and this function must be aware that the\r
342 device specified by ControllerHandle may already be managed by the same driver or a\r
343 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
344 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
345 Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
346 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
347 to guarantee the state of ControllerHandle is not modified by this function.\r
348\r
349 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
350 @param[in] ControllerHandle The handle of the controller to test. This handle\r
351 must support a protocol interface that supplies\r
352 an I/O abstraction to the driver.\r
353 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
354 parameter is ignored by device drivers, and is optional for bus\r
355 drivers. For bus drivers, if this parameter is not NULL, then\r
356 the bus driver must determine if the bus controller specified\r
357 by ControllerHandle and the child controller specified\r
358 by RemainingDevicePath are both supported by this\r
359 bus driver.\r
360\r
361 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
362 RemainingDevicePath is supported by the driver specified by This.\r
363 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
364 RemainingDevicePath is already being managed by the driver\r
365 specified by This.\r
366 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
367 RemainingDevicePath is already being managed by a different\r
368 driver or an application that requires exclusive access.\r
369 Currently not implemented.\r
370 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
371 RemainingDevicePath is not supported by the driver specified by This.\r
372**/\r
373EFI_STATUS\r
374EFIAPI\r
375SdMmcPciHcDriverBindingSupported (\r
376 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
377 IN EFI_HANDLE Controller,\r
378 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
379 )\r
380{\r
381 EFI_STATUS Status;\r
382 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
383 EFI_PCI_IO_PROTOCOL *PciIo;\r
384 PCI_TYPE00 PciData;\r
385\r
386 PciIo = NULL;\r
387 ParentDevicePath = NULL;\r
388\r
389 //\r
390 // SdPciHcDxe is a device driver, and should ingore the\r
391 // "RemainingDevicePath" according to EFI spec.\r
392 //\r
393 Status = gBS->OpenProtocol (\r
394 Controller,\r
395 &gEfiDevicePathProtocolGuid,\r
396 (VOID *) &ParentDevicePath,\r
397 This->DriverBindingHandle,\r
398 Controller,\r
399 EFI_OPEN_PROTOCOL_BY_DRIVER\r
400 );\r
401 if (EFI_ERROR (Status)) {\r
402 //\r
403 // EFI_ALREADY_STARTED is also an error.\r
404 //\r
405 return Status;\r
406 }\r
407 //\r
408 // Close the protocol because we don't use it here.\r
409 //\r
410 gBS->CloseProtocol (\r
411 Controller,\r
412 &gEfiDevicePathProtocolGuid,\r
413 This->DriverBindingHandle,\r
414 Controller\r
415 );\r
416\r
417 //\r
418 // Now test the EfiPciIoProtocol.\r
419 //\r
420 Status = gBS->OpenProtocol (\r
421 Controller,\r
422 &gEfiPciIoProtocolGuid,\r
423 (VOID **) &PciIo,\r
424 This->DriverBindingHandle,\r
425 Controller,\r
426 EFI_OPEN_PROTOCOL_BY_DRIVER\r
427 );\r
428 if (EFI_ERROR (Status)) {\r
429 return Status;\r
430 }\r
431\r
432 //\r
433 // Now further check the PCI header: Base class (offset 0x08) and\r
434 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI\r
435 // Host Controller.\r
436 //\r
437 Status = PciIo->Pci.Read (\r
438 PciIo,\r
439 EfiPciIoWidthUint8,\r
440 0,\r
441 sizeof (PciData),\r
442 &PciData\r
443 );\r
444 if (EFI_ERROR (Status)) {\r
445 gBS->CloseProtocol (\r
446 Controller,\r
447 &gEfiPciIoProtocolGuid,\r
448 This->DriverBindingHandle,\r
449 Controller\r
450 );\r
451 return EFI_UNSUPPORTED;\r
452 }\r
453 //\r
454 // Since we already got the PciData, we can close protocol to avoid to carry it\r
455 // on for multiple exit points.\r
456 //\r
457 gBS->CloseProtocol (\r
458 Controller,\r
459 &gEfiPciIoProtocolGuid,\r
460 This->DriverBindingHandle,\r
461 Controller\r
462 );\r
463\r
464 //\r
465 // Examine SD PCI Host Controller PCI Configuration table fields.\r
466 //\r
467 if ((PciData.Hdr.ClassCode[2] == PCI_CLASS_SYSTEM_PERIPHERAL) &&\r
468 (PciData.Hdr.ClassCode[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER) &&\r
469 ((PciData.Hdr.ClassCode[0] == 0x00) || (PciData.Hdr.ClassCode[0] == 0x01))) {\r
470 return EFI_SUCCESS;\r
471 }\r
472\r
473 return EFI_UNSUPPORTED;\r
474}\r
475\r
476/**\r
477 Starts a device controller or a bus controller.\r
478\r
479 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
480 As a result, much of the error checking on the parameters to Start() has been moved into this\r
481 common boot service. It is legal to call Start() from other locations,\r
482 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
483 1. ControllerHandle must be a valid EFI_HANDLE.\r
484 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
485 EFI_DEVICE_PATH_PROTOCOL.\r
486 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
487 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
488\r
489 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
490 @param[in] ControllerHandle The handle of the controller to start. This handle\r
491 must support a protocol interface that supplies\r
492 an I/O abstraction to the driver.\r
493 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
494 parameter is ignored by device drivers, and is optional for bus\r
495 drivers. For a bus driver, if this parameter is NULL, then handles\r
496 for all the children of Controller are created by this driver.\r
497 If this parameter is not NULL and the first Device Path Node is\r
498 not the End of Device Path Node, then only the handle for the\r
499 child device specified by the first Device Path Node of\r
500 RemainingDevicePath is created by this driver.\r
501 If the first Device Path Node of RemainingDevicePath is\r
502 the End of Device Path Node, no child handle is created by this\r
503 driver.\r
504\r
505 @retval EFI_SUCCESS The device was started.\r
506 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
507 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
508 @retval Others The driver failded to start the device.\r
509\r
510**/\r
511EFI_STATUS\r
512EFIAPI\r
513SdMmcPciHcDriverBindingStart (\r
514 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
515 IN EFI_HANDLE Controller,\r
516 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
517 )\r
518{\r
519 EFI_STATUS Status;\r
520 SD_MMC_HC_PRIVATE_DATA *Private;\r
521 EFI_PCI_IO_PROTOCOL *PciIo;\r
522 UINT64 Supports;\r
523 UINT64 PciAttributes;\r
524 UINT8 SlotNum;\r
525 UINT8 FirstBar;\r
526 UINT8 Slot;\r
527 UINT8 Index;\r
528 CARD_TYPE_DETECT_ROUTINE *Routine;\r
529 UINT32 RoutineNum;\r
530 BOOLEAN MediaPresent;\r
df0a0e4b 531 BOOLEAN Support64BitDma;\r
48555339 532\r
e27ccaba 533 DEBUG ((DEBUG_INFO, "SdMmcPciHcDriverBindingStart: Start\n"));\r
48555339
FT
534\r
535 //\r
536 // Open PCI I/O Protocol and save pointer to open protocol\r
537 // in private data area.\r
538 //\r
539 PciIo = NULL;\r
540 Status = gBS->OpenProtocol (\r
541 Controller,\r
542 &gEfiPciIoProtocolGuid,\r
543 (VOID **) &PciIo,\r
544 This->DriverBindingHandle,\r
545 Controller,\r
546 EFI_OPEN_PROTOCOL_BY_DRIVER\r
547 );\r
548 if (EFI_ERROR (Status)) {\r
549 return Status;\r
550 }\r
551\r
552 //\r
553 // Enable the SD Host Controller MMIO space\r
554 //\r
555 Private = NULL;\r
556 Status = PciIo->Attributes (\r
557 PciIo,\r
558 EfiPciIoAttributeOperationGet,\r
559 0,\r
560 &PciAttributes\r
561 );\r
562\r
563 if (EFI_ERROR (Status)) {\r
564 goto Done;\r
565 }\r
566\r
567 Status = PciIo->Attributes (\r
568 PciIo,\r
569 EfiPciIoAttributeOperationSupported,\r
570 0,\r
571 &Supports\r
572 );\r
573\r
574 if (!EFI_ERROR (Status)) {\r
575 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r
576 Status = PciIo->Attributes (\r
577 PciIo,\r
578 EfiPciIoAttributeOperationEnable,\r
579 Supports,\r
580 NULL\r
581 );\r
582 } else {\r
583 goto Done;\r
584 }\r
585\r
586 Private = AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA), &gSdMmcPciHcTemplate);\r
587 if (Private == NULL) {\r
588 Status = EFI_OUT_OF_RESOURCES;\r
589 goto Done;\r
590 }\r
591\r
592 Private->ControllerHandle = Controller;\r
593 Private->PciIo = PciIo;\r
594 Private->PciAttributes = PciAttributes;\r
595 InitializeListHead (&Private->Queue);\r
596\r
597 //\r
598 // Get SD/MMC Pci Host Controller Slot info\r
599 //\r
600 Status = SdMmcHcGetSlotInfo (PciIo, &FirstBar, &SlotNum);\r
601 if (EFI_ERROR (Status)) {\r
602 goto Done;\r
603 }\r
604\r
b23fc39c
AB
605 //\r
606 // Attempt to locate the singleton instance of the SD/MMC override protocol,\r
607 // which implements platform specific workarounds for non-standard SDHCI\r
608 // implementations.\r
609 //\r
610 if (mOverride == NULL) {\r
611 Status = gBS->LocateProtocol (&gEdkiiSdMmcOverrideProtocolGuid, NULL,\r
612 (VOID **)&mOverride);\r
613 if (!EFI_ERROR (Status)) {\r
614 DEBUG ((DEBUG_INFO, "%a: found SD/MMC override protocol\n",\r
615 __FUNCTION__));\r
616 }\r
617 }\r
618\r
df0a0e4b 619 Support64BitDma = TRUE;\r
48555339
FT
620 for (Slot = FirstBar; Slot < (FirstBar + SlotNum); Slot++) {\r
621 Private->Slot[Slot].Enable = TRUE;\r
622\r
b5547b9c
AS
623 //\r
624 // Get SD/MMC Pci Host Controller Version\r
625 //\r
626 Status = SdMmcHcGetControllerVersion (PciIo, Slot, &Private->ControllerVersion[Slot]);\r
627 if (EFI_ERROR (Status)) {\r
628 continue;\r
629 }\r
630\r
48555339
FT
631 Status = SdMmcHcGetCapability (PciIo, Slot, &Private->Capability[Slot]);\r
632 if (EFI_ERROR (Status)) {\r
633 continue;\r
634 }\r
7f3b0bad
MW
635\r
636 Private->BaseClkFreq[Slot] = Private->Capability[Slot].BaseClkFreq;\r
637\r
b23fc39c
AB
638 if (mOverride != NULL && mOverride->Capability != NULL) {\r
639 Status = mOverride->Capability (\r
640 Controller,\r
641 Slot,\r
7f3b0bad
MW
642 &Private->Capability[Slot],\r
643 &Private->BaseClkFreq[Slot]\r
644 );\r
b23fc39c
AB
645 if (EFI_ERROR (Status)) {\r
646 DEBUG ((DEBUG_WARN, "%a: Failed to override capability - %r\n",\r
647 __FUNCTION__, Status));\r
648 continue;\r
649 }\r
650 }\r
48555339 651 DumpCapabilityReg (Slot, &Private->Capability[Slot]);\r
7f3b0bad
MW
652 DEBUG ((\r
653 DEBUG_INFO,\r
654 "Slot[%d] Base Clock Frequency: %dMHz\n",\r
655 Slot,\r
656 Private->BaseClkFreq[Slot]\r
657 ));\r
48555339 658\r
b5547b9c
AS
659 //\r
660 // If any of the slots does not support 64b system bus\r
661 // do not enable 64b DMA in the PCI layer.\r
662 //\r
690d60c0
AS
663 if ((Private->ControllerVersion[Slot] == SD_MMC_HC_CTRL_VER_300 &&\r
664 Private->Capability[Slot].SysBus64V3 == 0) ||\r
665 (Private->ControllerVersion[Slot] == SD_MMC_HC_CTRL_VER_400 &&\r
666 Private->Capability[Slot].SysBus64V3 == 0) ||\r
667 (Private->ControllerVersion[Slot] >= SD_MMC_HC_CTRL_VER_410 &&\r
668 Private->Capability[Slot].SysBus64V4 == 0)) {\r
b5547b9c
AS
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