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