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