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