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