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