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