]>
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 | |
54228046 | 7 | Copyright (c) 2015 - 2017, 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 | |
20 | /**\r | |
21 | Dump the content of SD/MMC host controller's Capability Register.\r | |
22 | \r | |
23 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
24 | @param[in] Capability The buffer to store the capability data.\r | |
25 | \r | |
26 | **/\r | |
27 | VOID\r | |
28 | DumpCapabilityReg (\r | |
29 | IN UINT8 Slot,\r | |
30 | IN SD_MMC_HC_SLOT_CAP *Capability\r | |
31 | )\r | |
32 | {\r | |
33 | //\r | |
34 | // Dump Capability Data\r | |
35 | //\r | |
e27ccaba FT |
36 | DEBUG ((DEBUG_INFO, " == Slot [%d] Capability is 0x%x ==\n", Slot, Capability));\r |
37 | DEBUG ((DEBUG_INFO, " Timeout Clk Freq %d%a\n", Capability->TimeoutFreq, (Capability->TimeoutUnit) ? "MHz" : "KHz"));\r | |
38 | DEBUG ((DEBUG_INFO, " Base Clk Freq %dMHz\n", Capability->BaseClkFreq));\r | |
39 | DEBUG ((DEBUG_INFO, " Max Blk Len %dbytes\n", 512 * (1 << Capability->MaxBlkLen)));\r | |
40 | DEBUG ((DEBUG_INFO, " 8-bit Support %a\n", Capability->BusWidth8 ? "TRUE" : "FALSE"));\r | |
41 | DEBUG ((DEBUG_INFO, " ADMA2 Support %a\n", Capability->Adma2 ? "TRUE" : "FALSE"));\r | |
42 | DEBUG ((DEBUG_INFO, " HighSpeed Support %a\n", Capability->HighSpeed ? "TRUE" : "FALSE"));\r | |
43 | DEBUG ((DEBUG_INFO, " SDMA Support %a\n", Capability->Sdma ? "TRUE" : "FALSE"));\r | |
44 | DEBUG ((DEBUG_INFO, " Suspend/Resume %a\n", Capability->SuspRes ? "TRUE" : "FALSE"));\r | |
45 | DEBUG ((DEBUG_INFO, " Voltage 3.3 %a\n", Capability->Voltage33 ? "TRUE" : "FALSE"));\r | |
46 | DEBUG ((DEBUG_INFO, " Voltage 3.0 %a\n", Capability->Voltage30 ? "TRUE" : "FALSE"));\r | |
47 | DEBUG ((DEBUG_INFO, " Voltage 1.8 %a\n", Capability->Voltage18 ? "TRUE" : "FALSE"));\r | |
48 | DEBUG ((DEBUG_INFO, " 64-bit Sys Bus %a\n", Capability->SysBus64 ? "TRUE" : "FALSE"));\r | |
49 | DEBUG ((DEBUG_INFO, " Async Interrupt %a\n", Capability->AsyncInt ? "TRUE" : "FALSE"));\r | |
50 | DEBUG ((DEBUG_INFO, " SlotType "));\r | |
48555339 | 51 | if (Capability->SlotType == 0x00) {\r |
e27ccaba | 52 | DEBUG ((DEBUG_INFO, "%a\n", "Removable Slot"));\r |
48555339 | 53 | } else if (Capability->SlotType == 0x01) {\r |
e27ccaba | 54 | DEBUG ((DEBUG_INFO, "%a\n", "Embedded Slot"));\r |
48555339 | 55 | } else if (Capability->SlotType == 0x02) {\r |
e27ccaba | 56 | DEBUG ((DEBUG_INFO, "%a\n", "Shared Bus Slot"));\r |
48555339 | 57 | } else {\r |
e27ccaba | 58 | DEBUG ((DEBUG_INFO, "%a\n", "Reserved"));\r |
48555339 | 59 | }\r |
e27ccaba FT |
60 | DEBUG ((DEBUG_INFO, " SDR50 Support %a\n", Capability->Sdr50 ? "TRUE" : "FALSE"));\r |
61 | DEBUG ((DEBUG_INFO, " SDR104 Support %a\n", Capability->Sdr104 ? "TRUE" : "FALSE"));\r | |
62 | DEBUG ((DEBUG_INFO, " DDR50 Support %a\n", Capability->Ddr50 ? "TRUE" : "FALSE"));\r | |
63 | DEBUG ((DEBUG_INFO, " Driver Type A %a\n", Capability->DriverTypeA ? "TRUE" : "FALSE"));\r | |
64 | DEBUG ((DEBUG_INFO, " Driver Type C %a\n", Capability->DriverTypeC ? "TRUE" : "FALSE"));\r | |
65 | DEBUG ((DEBUG_INFO, " Driver Type D %a\n", Capability->DriverTypeD ? "TRUE" : "FALSE"));\r | |
66 | DEBUG ((DEBUG_INFO, " Driver Type 4 %a\n", Capability->DriverType4 ? "TRUE" : "FALSE"));\r | |
48555339 | 67 | if (Capability->TimerCount == 0) {\r |
e27ccaba | 68 | DEBUG ((DEBUG_INFO, " Retuning TimerCnt Disabled\n", 2 * (Capability->TimerCount - 1)));\r |
48555339 | 69 | } else {\r |
e27ccaba | 70 | DEBUG ((DEBUG_INFO, " Retuning TimerCnt %dseconds\n", 2 * (Capability->TimerCount - 1)));\r |
48555339 | 71 | }\r |
e27ccaba FT |
72 | DEBUG ((DEBUG_INFO, " SDR50 Tuning %a\n", Capability->TuningSDR50 ? "TRUE" : "FALSE"));\r |
73 | DEBUG ((DEBUG_INFO, " Retuning Mode Mode %d\n", Capability->RetuningMod + 1));\r | |
74 | DEBUG ((DEBUG_INFO, " Clock Multiplier M = %d\n", Capability->ClkMultiplier + 1));\r | |
75 | DEBUG ((DEBUG_INFO, " HS 400 %a\n", Capability->Hs400 ? "TRUE" : "FALSE"));\r | |
48555339 FT |
76 | return;\r |
77 | }\r | |
78 | \r | |
79 | /**\r | |
80 | Read SlotInfo register from SD/MMC host controller pci config space.\r | |
81 | \r | |
82 | @param[in] PciIo The PCI IO protocol instance.\r | |
83 | @param[out] FirstBar The buffer to store the first BAR value.\r | |
84 | @param[out] SlotNum The buffer to store the supported slot number.\r | |
85 | \r | |
86 | @retval EFI_SUCCESS The operation succeeds.\r | |
87 | @retval Others The operation fails.\r | |
88 | \r | |
89 | **/\r | |
90 | EFI_STATUS\r | |
91 | EFIAPI\r | |
92 | SdMmcHcGetSlotInfo (\r | |
93 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
94 | OUT UINT8 *FirstBar,\r | |
95 | OUT UINT8 *SlotNum\r | |
96 | )\r | |
97 | {\r | |
98 | EFI_STATUS Status;\r | |
99 | SD_MMC_HC_SLOT_INFO SlotInfo;\r | |
100 | \r | |
101 | Status = PciIo->Pci.Read (\r | |
102 | PciIo,\r | |
103 | EfiPciIoWidthUint8,\r | |
104 | SD_MMC_HC_SLOT_OFFSET,\r | |
105 | sizeof (SlotInfo),\r | |
106 | &SlotInfo\r | |
107 | );\r | |
108 | if (EFI_ERROR (Status)) {\r | |
109 | return Status;\r | |
110 | }\r | |
111 | \r | |
112 | *FirstBar = SlotInfo.FirstBar;\r | |
113 | *SlotNum = SlotInfo.SlotNum + 1;\r | |
114 | ASSERT ((*FirstBar + *SlotNum) < SD_MMC_HC_MAX_SLOT);\r | |
115 | return EFI_SUCCESS;\r | |
116 | }\r | |
117 | \r | |
118 | /**\r | |
119 | Read/Write specified SD/MMC host controller mmio register.\r | |
120 | \r | |
121 | @param[in] PciIo The PCI IO protocol instance.\r | |
122 | @param[in] BarIndex The BAR index of the standard PCI Configuration\r | |
123 | header to use as the base address for the memory\r | |
124 | operation to perform.\r | |
125 | @param[in] Offset The offset within the selected BAR to start the\r | |
126 | memory operation.\r | |
127 | @param[in] Read A boolean to indicate it's read or write operation.\r | |
128 | @param[in] Count The width of the mmio register in bytes.\r | |
129 | Must be 1, 2 , 4 or 8 bytes.\r | |
130 | @param[in, out] Data For read operations, the destination buffer to store\r | |
131 | the results. For write operations, the source buffer\r | |
132 | to write data from. The caller is responsible for\r | |
133 | having ownership of the data buffer and ensuring its\r | |
134 | size not less than Count bytes.\r | |
135 | \r | |
136 | @retval EFI_INVALID_PARAMETER The PciIo or Data is NULL or the Count is not valid.\r | |
137 | @retval EFI_SUCCESS The read/write operation succeeds.\r | |
138 | @retval Others The read/write operation fails.\r | |
139 | \r | |
140 | **/\r | |
141 | EFI_STATUS\r | |
142 | EFIAPI\r | |
143 | SdMmcHcRwMmio (\r | |
144 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
145 | IN UINT8 BarIndex,\r | |
146 | IN UINT32 Offset,\r | |
147 | IN BOOLEAN Read,\r | |
148 | IN UINT8 Count,\r | |
149 | IN OUT VOID *Data\r | |
150 | )\r | |
151 | {\r | |
152 | EFI_STATUS Status;\r | |
153 | \r | |
154 | if ((PciIo == NULL) || (Data == NULL)) {\r | |
155 | return EFI_INVALID_PARAMETER;\r | |
156 | }\r | |
157 | \r | |
158 | if ((Count != 1) && (Count != 2) && (Count != 4) && (Count != 8)) {\r | |
159 | return EFI_INVALID_PARAMETER;\r | |
160 | }\r | |
161 | \r | |
162 | if (Read) {\r | |
163 | Status = PciIo->Mem.Read (\r | |
164 | PciIo,\r | |
165 | EfiPciIoWidthUint8,\r | |
166 | BarIndex,\r | |
167 | (UINT64) Offset,\r | |
168 | Count,\r | |
169 | Data\r | |
170 | );\r | |
171 | } else {\r | |
172 | Status = PciIo->Mem.Write (\r | |
173 | PciIo,\r | |
174 | EfiPciIoWidthUint8,\r | |
175 | BarIndex,\r | |
176 | (UINT64) Offset,\r | |
177 | Count,\r | |
178 | Data\r | |
179 | );\r | |
180 | }\r | |
181 | \r | |
182 | return Status;\r | |
183 | }\r | |
184 | \r | |
185 | /**\r | |
186 | Do OR operation with the value of the specified SD/MMC host controller mmio register.\r | |
187 | \r | |
188 | @param[in] PciIo The PCI IO protocol instance.\r | |
189 | @param[in] BarIndex The BAR index of the standard PCI Configuration\r | |
190 | header to use as the base address for the memory\r | |
191 | operation to perform.\r | |
192 | @param[in] Offset The offset within the selected BAR to start the\r | |
193 | memory operation.\r | |
194 | @param[in] Count The width of the mmio register in bytes.\r | |
195 | Must be 1, 2 , 4 or 8 bytes.\r | |
196 | @param[in] OrData The pointer to the data used to do OR operation.\r | |
197 | The caller is responsible for having ownership of\r | |
198 | the data buffer and ensuring its size not less than\r | |
199 | Count bytes.\r | |
200 | \r | |
201 | @retval EFI_INVALID_PARAMETER The PciIo or OrData is NULL or the Count is not valid.\r | |
202 | @retval EFI_SUCCESS The OR operation succeeds.\r | |
203 | @retval Others The OR operation fails.\r | |
204 | \r | |
205 | **/\r | |
206 | EFI_STATUS\r | |
207 | EFIAPI\r | |
208 | SdMmcHcOrMmio (\r | |
209 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
210 | IN UINT8 BarIndex,\r | |
211 | IN UINT32 Offset,\r | |
212 | IN UINT8 Count,\r | |
213 | IN VOID *OrData\r | |
214 | )\r | |
215 | {\r | |
216 | EFI_STATUS Status;\r | |
217 | UINT64 Data;\r | |
218 | UINT64 Or;\r | |
219 | \r | |
220 | Status = SdMmcHcRwMmio (PciIo, BarIndex, Offset, TRUE, Count, &Data);\r | |
221 | if (EFI_ERROR (Status)) {\r | |
222 | return Status;\r | |
223 | }\r | |
224 | \r | |
225 | if (Count == 1) {\r | |
226 | Or = *(UINT8*) OrData;\r | |
227 | } else if (Count == 2) {\r | |
228 | Or = *(UINT16*) OrData;\r | |
229 | } else if (Count == 4) {\r | |
230 | Or = *(UINT32*) OrData;\r | |
231 | } else if (Count == 8) {\r | |
232 | Or = *(UINT64*) OrData;\r | |
233 | } else {\r | |
234 | return EFI_INVALID_PARAMETER;\r | |
235 | }\r | |
236 | \r | |
237 | Data |= Or;\r | |
238 | Status = SdMmcHcRwMmio (PciIo, BarIndex, Offset, FALSE, Count, &Data);\r | |
239 | \r | |
240 | return Status;\r | |
241 | }\r | |
242 | \r | |
243 | /**\r | |
244 | Do AND operation with the value of the specified SD/MMC host controller mmio register.\r | |
245 | \r | |
246 | @param[in] PciIo The PCI IO protocol instance.\r | |
247 | @param[in] BarIndex The BAR index of the standard PCI Configuration\r | |
248 | header to use as the base address for the memory\r | |
249 | operation to perform.\r | |
250 | @param[in] Offset The offset within the selected BAR to start the\r | |
251 | memory operation.\r | |
252 | @param[in] Count The width of the mmio register in bytes.\r | |
253 | Must be 1, 2 , 4 or 8 bytes.\r | |
254 | @param[in] AndData The pointer to the data used to do AND operation.\r | |
255 | The caller is responsible for having ownership of\r | |
256 | the data buffer and ensuring its size not less than\r | |
257 | Count bytes.\r | |
258 | \r | |
259 | @retval EFI_INVALID_PARAMETER The PciIo or AndData is NULL or the Count is not valid.\r | |
260 | @retval EFI_SUCCESS The AND operation succeeds.\r | |
261 | @retval Others The AND operation fails.\r | |
262 | \r | |
263 | **/\r | |
264 | EFI_STATUS\r | |
265 | EFIAPI\r | |
266 | SdMmcHcAndMmio (\r | |
267 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
268 | IN UINT8 BarIndex,\r | |
269 | IN UINT32 Offset,\r | |
270 | IN UINT8 Count,\r | |
271 | IN VOID *AndData\r | |
272 | )\r | |
273 | {\r | |
274 | EFI_STATUS Status;\r | |
275 | UINT64 Data;\r | |
276 | UINT64 And;\r | |
277 | \r | |
278 | Status = SdMmcHcRwMmio (PciIo, BarIndex, Offset, TRUE, Count, &Data);\r | |
279 | if (EFI_ERROR (Status)) {\r | |
280 | return Status;\r | |
281 | }\r | |
282 | \r | |
283 | if (Count == 1) {\r | |
284 | And = *(UINT8*) AndData;\r | |
285 | } else if (Count == 2) {\r | |
286 | And = *(UINT16*) AndData;\r | |
287 | } else if (Count == 4) {\r | |
288 | And = *(UINT32*) AndData;\r | |
289 | } else if (Count == 8) {\r | |
290 | And = *(UINT64*) AndData;\r | |
291 | } else {\r | |
292 | return EFI_INVALID_PARAMETER;\r | |
293 | }\r | |
294 | \r | |
295 | Data &= And;\r | |
296 | Status = SdMmcHcRwMmio (PciIo, BarIndex, Offset, FALSE, Count, &Data);\r | |
297 | \r | |
298 | return Status;\r | |
299 | }\r | |
300 | \r | |
301 | /**\r | |
302 | Wait for the value of the specified MMIO register set to the test value.\r | |
303 | \r | |
304 | @param[in] PciIo The PCI IO protocol instance.\r | |
305 | @param[in] BarIndex The BAR index of the standard PCI Configuration\r | |
306 | header to use as the base address for the memory\r | |
307 | operation to perform.\r | |
308 | @param[in] Offset The offset within the selected BAR to start the\r | |
309 | memory operation.\r | |
310 | @param[in] Count The width of the mmio register in bytes.\r | |
311 | Must be 1, 2, 4 or 8 bytes.\r | |
312 | @param[in] MaskValue The mask value of memory.\r | |
313 | @param[in] TestValue The test value of memory.\r | |
314 | \r | |
315 | @retval EFI_NOT_READY The MMIO register hasn't set to the expected value.\r | |
316 | @retval EFI_SUCCESS The MMIO register has expected value.\r | |
317 | @retval Others The MMIO operation fails.\r | |
318 | \r | |
319 | **/\r | |
320 | EFI_STATUS\r | |
321 | EFIAPI\r | |
322 | SdMmcHcCheckMmioSet (\r | |
323 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
324 | IN UINT8 BarIndex,\r | |
325 | IN UINT32 Offset,\r | |
326 | IN UINT8 Count,\r | |
327 | IN UINT64 MaskValue,\r | |
328 | IN UINT64 TestValue\r | |
329 | )\r | |
330 | {\r | |
331 | EFI_STATUS Status;\r | |
332 | UINT64 Value;\r | |
333 | \r | |
334 | //\r | |
335 | // Access PCI MMIO space to see if the value is the tested one.\r | |
336 | //\r | |
337 | Value = 0;\r | |
338 | Status = SdMmcHcRwMmio (PciIo, BarIndex, Offset, TRUE, Count, &Value);\r | |
339 | if (EFI_ERROR (Status)) {\r | |
340 | return Status;\r | |
341 | }\r | |
342 | \r | |
343 | Value &= MaskValue;\r | |
344 | \r | |
345 | if (Value == TestValue) {\r | |
346 | return EFI_SUCCESS;\r | |
347 | }\r | |
348 | \r | |
349 | return EFI_NOT_READY;\r | |
350 | }\r | |
351 | \r | |
352 | /**\r | |
353 | Wait for the value of the specified MMIO register set to the test value.\r | |
354 | \r | |
355 | @param[in] PciIo The PCI IO protocol instance.\r | |
356 | @param[in] BarIndex The BAR index of the standard PCI Configuration\r | |
357 | header to use as the base address for the memory\r | |
358 | operation to perform.\r | |
359 | @param[in] Offset The offset within the selected BAR to start the\r | |
360 | memory operation.\r | |
361 | @param[in] Count The width of the mmio register in bytes.\r | |
362 | Must be 1, 2, 4 or 8 bytes.\r | |
363 | @param[in] MaskValue The mask value of memory.\r | |
364 | @param[in] TestValue The test value of memory.\r | |
365 | @param[in] Timeout The time out value for wait memory set, uses 1\r | |
366 | microsecond as a unit.\r | |
367 | \r | |
368 | @retval EFI_TIMEOUT The MMIO register hasn't expected value in timeout\r | |
369 | range.\r | |
370 | @retval EFI_SUCCESS The MMIO register has expected value.\r | |
371 | @retval Others The MMIO operation fails.\r | |
372 | \r | |
373 | **/\r | |
374 | EFI_STATUS\r | |
375 | EFIAPI\r | |
376 | SdMmcHcWaitMmioSet (\r | |
377 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
378 | IN UINT8 BarIndex,\r | |
379 | IN UINT32 Offset,\r | |
380 | IN UINT8 Count,\r | |
381 | IN UINT64 MaskValue,\r | |
382 | IN UINT64 TestValue,\r | |
383 | IN UINT64 Timeout\r | |
384 | )\r | |
385 | {\r | |
386 | EFI_STATUS Status;\r | |
387 | BOOLEAN InfiniteWait;\r | |
388 | \r | |
389 | if (Timeout == 0) {\r | |
390 | InfiniteWait = TRUE;\r | |
391 | } else {\r | |
392 | InfiniteWait = FALSE;\r | |
393 | }\r | |
394 | \r | |
395 | while (InfiniteWait || (Timeout > 0)) {\r | |
396 | Status = SdMmcHcCheckMmioSet (\r | |
397 | PciIo,\r | |
398 | BarIndex,\r | |
399 | Offset,\r | |
400 | Count,\r | |
401 | MaskValue,\r | |
402 | TestValue\r | |
403 | );\r | |
404 | if (Status != EFI_NOT_READY) {\r | |
405 | return Status;\r | |
406 | }\r | |
407 | \r | |
408 | //\r | |
409 | // Stall for 1 microsecond.\r | |
410 | //\r | |
411 | gBS->Stall (1);\r | |
412 | \r | |
413 | Timeout--;\r | |
414 | }\r | |
415 | \r | |
416 | return EFI_TIMEOUT;\r | |
417 | }\r | |
418 | \r | |
419 | /**\r | |
420 | Software reset the specified SD/MMC host controller and enable all interrupts.\r | |
421 | \r | |
b23fc39c | 422 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r |
48555339 FT |
423 | @param[in] Slot The slot number of the SD card to send the command to.\r |
424 | \r | |
425 | @retval EFI_SUCCESS The software reset executes successfully.\r | |
426 | @retval Others The software reset fails.\r | |
427 | \r | |
428 | **/\r | |
429 | EFI_STATUS\r | |
430 | SdMmcHcReset (\r | |
b23fc39c | 431 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r |
48555339 FT |
432 | IN UINT8 Slot\r |
433 | )\r | |
434 | {\r | |
435 | EFI_STATUS Status;\r | |
436 | UINT8 SwReset;\r | |
b23fc39c | 437 | EFI_PCI_IO_PROTOCOL *PciIo;\r |
48555339 | 438 | \r |
b23fc39c AB |
439 | //\r |
440 | // Notify the SD/MMC override protocol that we are about to reset\r | |
441 | // the SD/MMC host controller.\r | |
442 | //\r | |
443 | if (mOverride != NULL && mOverride->NotifyPhase != NULL) {\r | |
444 | Status = mOverride->NotifyPhase (\r | |
445 | Private->ControllerHandle,\r | |
446 | Slot,\r | |
447 | EdkiiSdMmcResetPre);\r | |
448 | if (EFI_ERROR (Status)) {\r | |
449 | DEBUG ((DEBUG_WARN,\r | |
450 | "%a: SD/MMC pre reset notifier callback failed - %r\n",\r | |
451 | __FUNCTION__, Status));\r | |
452 | return Status;\r | |
453 | }\r | |
454 | }\r | |
455 | \r | |
456 | PciIo = Private->PciIo;\r | |
48555339 FT |
457 | SwReset = 0xFF;\r |
458 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_SW_RST, FALSE, sizeof (SwReset), &SwReset);\r | |
459 | \r | |
460 | if (EFI_ERROR (Status)) {\r | |
e27ccaba | 461 | DEBUG ((DEBUG_ERROR, "SdMmcHcReset: write full 1 fails: %r\n", Status));\r |
48555339 FT |
462 | return Status;\r |
463 | }\r | |
464 | \r | |
465 | Status = SdMmcHcWaitMmioSet (\r | |
466 | PciIo,\r | |
467 | Slot,\r | |
468 | SD_MMC_HC_SW_RST,\r | |
469 | sizeof (SwReset),\r | |
470 | 0xFF,\r | |
471 | 0x00,\r | |
472 | SD_MMC_HC_GENERIC_TIMEOUT\r | |
473 | );\r | |
474 | if (EFI_ERROR (Status)) {\r | |
e27ccaba | 475 | DEBUG ((DEBUG_INFO, "SdMmcHcReset: reset done with %r\n", Status));\r |
48555339 FT |
476 | return Status;\r |
477 | }\r | |
b23fc39c | 478 | \r |
48555339 FT |
479 | //\r |
480 | // Enable all interrupt after reset all.\r | |
481 | //\r | |
482 | Status = SdMmcHcEnableInterrupt (PciIo, Slot);\r | |
b23fc39c AB |
483 | if (EFI_ERROR (Status)) {\r |
484 | DEBUG ((DEBUG_INFO, "SdMmcHcReset: SdMmcHcEnableInterrupt done with %r\n",\r | |
485 | Status));\r | |
486 | return Status;\r | |
487 | }\r | |
488 | \r | |
489 | //\r | |
490 | // Notify the SD/MMC override protocol that we have just reset\r | |
491 | // the SD/MMC host controller.\r | |
492 | //\r | |
493 | if (mOverride != NULL && mOverride->NotifyPhase != NULL) {\r | |
494 | Status = mOverride->NotifyPhase (\r | |
495 | Private->ControllerHandle,\r | |
496 | Slot,\r | |
497 | EdkiiSdMmcResetPost);\r | |
498 | if (EFI_ERROR (Status)) {\r | |
499 | DEBUG ((DEBUG_WARN,\r | |
500 | "%a: SD/MMC post reset notifier callback failed - %r\n",\r | |
501 | __FUNCTION__, Status));\r | |
502 | }\r | |
503 | }\r | |
48555339 FT |
504 | \r |
505 | return Status;\r | |
506 | }\r | |
507 | \r | |
508 | /**\r | |
509 | Set all interrupt status bits in Normal and Error Interrupt Status Enable\r | |
510 | register.\r | |
511 | \r | |
512 | @param[in] PciIo The PCI IO protocol instance.\r | |
513 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
514 | \r | |
515 | @retval EFI_SUCCESS The operation executes successfully.\r | |
516 | @retval Others The operation fails.\r | |
517 | \r | |
518 | **/\r | |
519 | EFI_STATUS\r | |
520 | SdMmcHcEnableInterrupt (\r | |
521 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
522 | IN UINT8 Slot\r | |
523 | )\r | |
524 | {\r | |
525 | EFI_STATUS Status;\r | |
526 | UINT16 IntStatus;\r | |
527 | \r | |
528 | //\r | |
529 | // Enable all bits in Error Interrupt Status Enable Register\r | |
530 | //\r | |
531 | IntStatus = 0xFFFF;\r | |
532 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_ERR_INT_STS_EN, FALSE, sizeof (IntStatus), &IntStatus);\r | |
533 | if (EFI_ERROR (Status)) {\r | |
534 | return Status;\r | |
535 | }\r | |
536 | //\r | |
537 | // Enable all bits in Normal Interrupt Status Enable Register\r | |
538 | //\r | |
539 | IntStatus = 0xFFFF;\r | |
540 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_NOR_INT_STS_EN, FALSE, sizeof (IntStatus), &IntStatus);\r | |
541 | \r | |
542 | return Status;\r | |
543 | }\r | |
544 | \r | |
545 | /**\r | |
546 | Get the capability data from the specified slot.\r | |
547 | \r | |
548 | @param[in] PciIo The PCI IO protocol instance.\r | |
549 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
550 | @param[out] Capability The buffer to store the capability data.\r | |
551 | \r | |
552 | @retval EFI_SUCCESS The operation executes successfully.\r | |
553 | @retval Others The operation fails.\r | |
554 | \r | |
555 | **/\r | |
556 | EFI_STATUS\r | |
557 | SdMmcHcGetCapability (\r | |
558 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
559 | IN UINT8 Slot,\r | |
560 | OUT SD_MMC_HC_SLOT_CAP *Capability\r | |
561 | )\r | |
562 | {\r | |
563 | EFI_STATUS Status;\r | |
564 | UINT64 Cap;\r | |
565 | \r | |
566 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_CAP, TRUE, sizeof (Cap), &Cap);\r | |
567 | if (EFI_ERROR (Status)) {\r | |
568 | return Status;\r | |
569 | }\r | |
570 | \r | |
571 | CopyMem (Capability, &Cap, sizeof (Cap));\r | |
572 | \r | |
573 | return EFI_SUCCESS;\r | |
574 | }\r | |
575 | \r | |
576 | /**\r | |
577 | Get the maximum current capability data from the specified slot.\r | |
578 | \r | |
579 | @param[in] PciIo The PCI IO protocol instance.\r | |
580 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
581 | @param[out] MaxCurrent The buffer to store the maximum current capability data.\r | |
582 | \r | |
583 | @retval EFI_SUCCESS The operation executes successfully.\r | |
584 | @retval Others The operation fails.\r | |
585 | \r | |
586 | **/\r | |
587 | EFI_STATUS\r | |
588 | SdMmcHcGetMaxCurrent (\r | |
589 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
590 | IN UINT8 Slot,\r | |
591 | OUT UINT64 *MaxCurrent\r | |
592 | )\r | |
593 | {\r | |
594 | EFI_STATUS Status;\r | |
595 | \r | |
596 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_MAX_CURRENT_CAP, TRUE, sizeof (UINT64), MaxCurrent);\r | |
597 | \r | |
598 | return Status;\r | |
599 | }\r | |
600 | \r | |
601 | /**\r | |
602 | Detect whether there is a SD/MMC card attached at the specified SD/MMC host controller\r | |
603 | slot.\r | |
604 | \r | |
605 | Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.\r | |
606 | \r | |
607 | @param[in] PciIo The PCI IO protocol instance.\r | |
608 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
609 | @param[out] MediaPresent The pointer to the media present boolean value.\r | |
610 | \r | |
611 | @retval EFI_SUCCESS There is no media change happened.\r | |
612 | @retval EFI_MEDIA_CHANGED There is media change happened.\r | |
613 | @retval Others The detection fails.\r | |
614 | \r | |
615 | **/\r | |
616 | EFI_STATUS\r | |
617 | SdMmcHcCardDetect (\r | |
618 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
619 | IN UINT8 Slot,\r | |
620 | OUT BOOLEAN *MediaPresent\r | |
621 | )\r | |
622 | {\r | |
623 | EFI_STATUS Status;\r | |
624 | UINT16 Data;\r | |
625 | UINT32 PresentState;\r | |
626 | \r | |
2e9107b8 FT |
627 | //\r |
628 | // Check Present State Register to see if there is a card presented.\r | |
629 | //\r | |
630 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_PRESENT_STATE, TRUE, sizeof (PresentState), &PresentState);\r | |
631 | if (EFI_ERROR (Status)) {\r | |
632 | return Status;\r | |
633 | }\r | |
634 | \r | |
635 | if ((PresentState & BIT16) != 0) {\r | |
636 | *MediaPresent = TRUE;\r | |
637 | } else {\r | |
638 | *MediaPresent = FALSE;\r | |
639 | }\r | |
640 | \r | |
48555339 FT |
641 | //\r |
642 | // Check Normal Interrupt Status Register\r | |
643 | //\r | |
644 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_NOR_INT_STS, TRUE, sizeof (Data), &Data);\r | |
645 | if (EFI_ERROR (Status)) {\r | |
646 | return Status;\r | |
647 | }\r | |
648 | \r | |
649 | if ((Data & (BIT6 | BIT7)) != 0) {\r | |
650 | //\r | |
651 | // Clear BIT6 and BIT7 by writing 1 to these two bits if set.\r | |
652 | //\r | |
653 | Data &= BIT6 | BIT7;\r | |
654 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_NOR_INT_STS, FALSE, sizeof (Data), &Data);\r | |
655 | if (EFI_ERROR (Status)) {\r | |
656 | return Status;\r | |
657 | }\r | |
658 | \r | |
48555339 FT |
659 | return EFI_MEDIA_CHANGED;\r |
660 | }\r | |
661 | \r | |
662 | return EFI_SUCCESS;\r | |
663 | }\r | |
664 | \r | |
665 | /**\r | |
666 | Stop SD/MMC card clock.\r | |
667 | \r | |
668 | Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.\r | |
669 | \r | |
670 | @param[in] PciIo The PCI IO protocol instance.\r | |
671 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
672 | \r | |
673 | @retval EFI_SUCCESS Succeed to stop SD/MMC clock.\r | |
674 | @retval Others Fail to stop SD/MMC clock.\r | |
675 | \r | |
676 | **/\r | |
677 | EFI_STATUS\r | |
678 | SdMmcHcStopClock (\r | |
679 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
680 | IN UINT8 Slot\r | |
681 | )\r | |
682 | {\r | |
683 | EFI_STATUS Status;\r | |
684 | UINT32 PresentState;\r | |
685 | UINT16 ClockCtrl;\r | |
686 | \r | |
687 | //\r | |
688 | // Ensure no SD transactions are occurring on the SD Bus by\r | |
689 | // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)\r | |
690 | // in the Present State register to be 0.\r | |
691 | //\r | |
692 | Status = SdMmcHcWaitMmioSet (\r | |
693 | PciIo,\r | |
694 | Slot,\r | |
695 | SD_MMC_HC_PRESENT_STATE,\r | |
696 | sizeof (PresentState),\r | |
697 | BIT0 | BIT1,\r | |
698 | 0,\r | |
699 | SD_MMC_HC_GENERIC_TIMEOUT\r | |
700 | );\r | |
701 | if (EFI_ERROR (Status)) {\r | |
702 | return Status;\r | |
703 | }\r | |
704 | \r | |
705 | //\r | |
706 | // Set SD Clock Enable in the Clock Control register to 0\r | |
707 | //\r | |
708 | ClockCtrl = (UINT16)~BIT2;\r | |
709 | Status = SdMmcHcAndMmio (PciIo, Slot, SD_MMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);\r | |
710 | \r | |
711 | return Status;\r | |
712 | }\r | |
713 | \r | |
714 | /**\r | |
715 | SD/MMC card clock supply.\r | |
716 | \r | |
717 | Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.\r | |
718 | \r | |
719 | @param[in] PciIo The PCI IO protocol instance.\r | |
720 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
721 | @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.\r | |
722 | @param[in] Capability The capability of the slot.\r | |
723 | \r | |
724 | @retval EFI_SUCCESS The clock is supplied successfully.\r | |
725 | @retval Others The clock isn't supplied successfully.\r | |
726 | \r | |
727 | **/\r | |
728 | EFI_STATUS\r | |
729 | SdMmcHcClockSupply (\r | |
730 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
731 | IN UINT8 Slot,\r | |
732 | IN UINT64 ClockFreq,\r | |
733 | IN SD_MMC_HC_SLOT_CAP Capability\r | |
734 | )\r | |
735 | {\r | |
736 | EFI_STATUS Status;\r | |
737 | UINT32 BaseClkFreq;\r | |
738 | UINT32 SettingFreq;\r | |
739 | UINT32 Divisor;\r | |
740 | UINT32 Remainder;\r | |
741 | UINT16 ControllerVer;\r | |
742 | UINT16 ClockCtrl;\r | |
743 | \r | |
744 | //\r | |
745 | // Calculate a divisor for SD clock frequency\r | |
746 | //\r | |
747 | ASSERT (Capability.BaseClkFreq != 0);\r | |
748 | \r | |
749 | BaseClkFreq = Capability.BaseClkFreq;\r | |
cb9cb9e2 | 750 | if (ClockFreq == 0) {\r |
48555339 FT |
751 | return EFI_INVALID_PARAMETER;\r |
752 | }\r | |
cb9cb9e2 FT |
753 | \r |
754 | if (ClockFreq > (BaseClkFreq * 1000)) {\r | |
755 | ClockFreq = BaseClkFreq * 1000;\r | |
756 | }\r | |
757 | \r | |
48555339 FT |
758 | //\r |
759 | // Calculate the divisor of base frequency.\r | |
760 | //\r | |
761 | Divisor = 0;\r | |
762 | SettingFreq = BaseClkFreq * 1000;\r | |
763 | while (ClockFreq < SettingFreq) {\r | |
764 | Divisor++;\r | |
765 | \r | |
766 | SettingFreq = (BaseClkFreq * 1000) / (2 * Divisor);\r | |
767 | Remainder = (BaseClkFreq * 1000) % (2 * Divisor);\r | |
768 | if ((ClockFreq == SettingFreq) && (Remainder == 0)) {\r | |
769 | break;\r | |
770 | }\r | |
771 | if ((ClockFreq == SettingFreq) && (Remainder != 0)) {\r | |
772 | SettingFreq ++;\r | |
773 | }\r | |
774 | }\r | |
775 | \r | |
e27ccaba | 776 | DEBUG ((DEBUG_INFO, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq, Divisor, ClockFreq));\r |
48555339 FT |
777 | \r |
778 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_CTRL_VER, TRUE, sizeof (ControllerVer), &ControllerVer);\r | |
779 | if (EFI_ERROR (Status)) {\r | |
780 | return Status;\r | |
781 | }\r | |
782 | //\r | |
783 | // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.\r | |
784 | //\r | |
785 | if ((ControllerVer & 0xFF) == 2) {\r | |
786 | ASSERT (Divisor <= 0x3FF);\r | |
787 | ClockCtrl = ((Divisor & 0xFF) << 8) | ((Divisor & 0x300) >> 2);\r | |
788 | } else if (((ControllerVer & 0xFF) == 0) || ((ControllerVer & 0xFF) == 1)) {\r | |
789 | //\r | |
790 | // Only the most significant bit can be used as divisor.\r | |
791 | //\r | |
792 | if (((Divisor - 1) & Divisor) != 0) {\r | |
793 | Divisor = 1 << (HighBitSet32 (Divisor) + 1);\r | |
794 | }\r | |
795 | ASSERT (Divisor <= 0x80);\r | |
796 | ClockCtrl = (Divisor & 0xFF) << 8;\r | |
797 | } else {\r | |
e27ccaba | 798 | DEBUG ((DEBUG_ERROR, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer));\r |
48555339 FT |
799 | return EFI_UNSUPPORTED;\r |
800 | }\r | |
801 | \r | |
802 | //\r | |
803 | // Stop bus clock at first\r | |
804 | //\r | |
805 | Status = SdMmcHcStopClock (PciIo, Slot);\r | |
806 | if (EFI_ERROR (Status)) {\r | |
807 | return Status;\r | |
808 | }\r | |
809 | \r | |
810 | //\r | |
811 | // Supply clock frequency with specified divisor\r | |
812 | //\r | |
813 | ClockCtrl |= BIT0;\r | |
814 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_CLOCK_CTRL, FALSE, sizeof (ClockCtrl), &ClockCtrl);\r | |
815 | if (EFI_ERROR (Status)) {\r | |
e27ccaba | 816 | DEBUG ((DEBUG_ERROR, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));\r |
48555339 FT |
817 | return Status;\r |
818 | }\r | |
819 | \r | |
820 | //\r | |
821 | // Wait Internal Clock Stable in the Clock Control register to be 1\r | |
822 | //\r | |
823 | Status = SdMmcHcWaitMmioSet (\r | |
824 | PciIo,\r | |
825 | Slot,\r | |
826 | SD_MMC_HC_CLOCK_CTRL,\r | |
827 | sizeof (ClockCtrl),\r | |
828 | BIT1,\r | |
829 | BIT1,\r | |
830 | SD_MMC_HC_GENERIC_TIMEOUT\r | |
831 | );\r | |
832 | if (EFI_ERROR (Status)) {\r | |
833 | return Status;\r | |
834 | }\r | |
835 | \r | |
836 | //\r | |
837 | // Set SD Clock Enable in the Clock Control register to 1\r | |
838 | //\r | |
839 | ClockCtrl = BIT2;\r | |
840 | Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);\r | |
841 | \r | |
842 | return Status;\r | |
843 | }\r | |
844 | \r | |
845 | /**\r | |
846 | SD/MMC bus power control.\r | |
847 | \r | |
848 | Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.\r | |
849 | \r | |
850 | @param[in] PciIo The PCI IO protocol instance.\r | |
851 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
852 | @param[in] PowerCtrl The value setting to the power control register.\r | |
853 | \r | |
854 | @retval TRUE There is a SD/MMC card attached.\r | |
855 | @retval FALSE There is no a SD/MMC card attached.\r | |
856 | \r | |
857 | **/\r | |
858 | EFI_STATUS\r | |
859 | SdMmcHcPowerControl (\r | |
860 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
861 | IN UINT8 Slot,\r | |
862 | IN UINT8 PowerCtrl\r | |
863 | )\r | |
864 | {\r | |
865 | EFI_STATUS Status;\r | |
866 | \r | |
867 | //\r | |
868 | // Clr SD Bus Power\r | |
869 | //\r | |
870 | PowerCtrl &= (UINT8)~BIT0;\r | |
871 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_POWER_CTRL, FALSE, sizeof (PowerCtrl), &PowerCtrl);\r | |
872 | if (EFI_ERROR (Status)) {\r | |
873 | return Status;\r | |
874 | }\r | |
875 | \r | |
876 | //\r | |
877 | // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register\r | |
878 | //\r | |
879 | PowerCtrl |= BIT0;\r | |
880 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_POWER_CTRL, FALSE, sizeof (PowerCtrl), &PowerCtrl);\r | |
881 | \r | |
882 | return Status;\r | |
883 | }\r | |
884 | \r | |
885 | /**\r | |
886 | Set the SD/MMC bus width.\r | |
887 | \r | |
888 | Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.\r | |
889 | \r | |
890 | @param[in] PciIo The PCI IO protocol instance.\r | |
891 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
892 | @param[in] BusWidth The bus width used by the SD/MMC device, it must be 1, 4 or 8.\r | |
893 | \r | |
894 | @retval EFI_SUCCESS The bus width is set successfully.\r | |
895 | @retval Others The bus width isn't set successfully.\r | |
896 | \r | |
897 | **/\r | |
898 | EFI_STATUS\r | |
899 | SdMmcHcSetBusWidth (\r | |
900 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
901 | IN UINT8 Slot,\r | |
902 | IN UINT16 BusWidth\r | |
903 | )\r | |
904 | {\r | |
905 | EFI_STATUS Status;\r | |
906 | UINT8 HostCtrl1;\r | |
907 | \r | |
908 | if (BusWidth == 1) {\r | |
909 | HostCtrl1 = (UINT8)~(BIT5 | BIT1);\r | |
910 | Status = SdMmcHcAndMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);\r | |
911 | } else if (BusWidth == 4) {\r | |
912 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, TRUE, sizeof (HostCtrl1), &HostCtrl1);\r | |
913 | if (EFI_ERROR (Status)) {\r | |
914 | return Status;\r | |
915 | }\r | |
916 | HostCtrl1 |= BIT1;\r | |
917 | HostCtrl1 &= (UINT8)~BIT5;\r | |
918 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, FALSE, sizeof (HostCtrl1), &HostCtrl1);\r | |
919 | } else if (BusWidth == 8) {\r | |
920 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, TRUE, sizeof (HostCtrl1), &HostCtrl1);\r | |
921 | if (EFI_ERROR (Status)) {\r | |
922 | return Status;\r | |
923 | }\r | |
924 | HostCtrl1 &= (UINT8)~BIT1;\r | |
925 | HostCtrl1 |= BIT5;\r | |
926 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, FALSE, sizeof (HostCtrl1), &HostCtrl1);\r | |
927 | } else {\r | |
928 | ASSERT (FALSE);\r | |
929 | return EFI_INVALID_PARAMETER;\r | |
930 | }\r | |
931 | \r | |
932 | return Status;\r | |
933 | }\r | |
934 | \r | |
935 | /**\r | |
936 | Supply SD/MMC card with lowest clock frequency at initialization.\r | |
937 | \r | |
938 | @param[in] PciIo The PCI IO protocol instance.\r | |
939 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
940 | @param[in] Capability The capability of the slot.\r | |
941 | \r | |
942 | @retval EFI_SUCCESS The clock is supplied successfully.\r | |
943 | @retval Others The clock isn't supplied successfully.\r | |
944 | \r | |
945 | **/\r | |
946 | EFI_STATUS\r | |
947 | SdMmcHcInitClockFreq (\r | |
948 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
949 | IN UINT8 Slot,\r | |
950 | IN SD_MMC_HC_SLOT_CAP Capability\r | |
951 | )\r | |
952 | {\r | |
953 | EFI_STATUS Status;\r | |
954 | UINT32 InitFreq;\r | |
955 | \r | |
956 | //\r | |
957 | // Calculate a divisor for SD clock frequency\r | |
958 | //\r | |
959 | if (Capability.BaseClkFreq == 0) {\r | |
960 | //\r | |
961 | // Don't support get Base Clock Frequency information via another method\r | |
962 | //\r | |
963 | return EFI_UNSUPPORTED;\r | |
964 | }\r | |
965 | //\r | |
966 | // Supply 400KHz clock frequency at initialization phase.\r | |
967 | //\r | |
968 | InitFreq = 400;\r | |
969 | Status = SdMmcHcClockSupply (PciIo, Slot, InitFreq, Capability);\r | |
970 | return Status;\r | |
971 | }\r | |
972 | \r | |
973 | /**\r | |
974 | Supply SD/MMC card with maximum voltage at initialization.\r | |
975 | \r | |
976 | Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.\r | |
977 | \r | |
978 | @param[in] PciIo The PCI IO protocol instance.\r | |
979 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
980 | @param[in] Capability The capability of the slot.\r | |
981 | \r | |
982 | @retval EFI_SUCCESS The voltage is supplied successfully.\r | |
983 | @retval Others The voltage isn't supplied successfully.\r | |
984 | \r | |
985 | **/\r | |
986 | EFI_STATUS\r | |
987 | SdMmcHcInitPowerVoltage (\r | |
988 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
989 | IN UINT8 Slot,\r | |
990 | IN SD_MMC_HC_SLOT_CAP Capability\r | |
991 | )\r | |
992 | {\r | |
993 | EFI_STATUS Status;\r | |
994 | UINT8 MaxVoltage;\r | |
995 | UINT8 HostCtrl2;\r | |
996 | \r | |
997 | //\r | |
998 | // Calculate supported maximum voltage according to SD Bus Voltage Select\r | |
999 | //\r | |
1000 | if (Capability.Voltage33 != 0) {\r | |
1001 | //\r | |
1002 | // Support 3.3V\r | |
1003 | //\r | |
1004 | MaxVoltage = 0x0E;\r | |
1005 | } else if (Capability.Voltage30 != 0) {\r | |
1006 | //\r | |
1007 | // Support 3.0V\r | |
1008 | //\r | |
1009 | MaxVoltage = 0x0C;\r | |
1010 | } else if (Capability.Voltage18 != 0) {\r | |
1011 | //\r | |
1012 | // Support 1.8V\r | |
1013 | //\r | |
1014 | MaxVoltage = 0x0A;\r | |
1015 | HostCtrl2 = BIT3;\r | |
1016 | Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);\r | |
1017 | gBS->Stall (5000);\r | |
1018 | if (EFI_ERROR (Status)) {\r | |
1019 | return Status;\r | |
1020 | }\r | |
1021 | } else {\r | |
1022 | ASSERT (FALSE);\r | |
1023 | return EFI_DEVICE_ERROR;\r | |
1024 | }\r | |
1025 | \r | |
1026 | //\r | |
1027 | // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register\r | |
1028 | //\r | |
1029 | Status = SdMmcHcPowerControl (PciIo, Slot, MaxVoltage);\r | |
1030 | \r | |
1031 | return Status;\r | |
1032 | }\r | |
1033 | \r | |
1034 | /**\r | |
1035 | Initialize the Timeout Control register with most conservative value at initialization.\r | |
1036 | \r | |
1037 | Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.\r | |
1038 | \r | |
1039 | @param[in] PciIo The PCI IO protocol instance.\r | |
1040 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
1041 | \r | |
1042 | @retval EFI_SUCCESS The timeout control register is configured successfully.\r | |
1043 | @retval Others The timeout control register isn't configured successfully.\r | |
1044 | \r | |
1045 | **/\r | |
1046 | EFI_STATUS\r | |
1047 | SdMmcHcInitTimeoutCtrl (\r | |
1048 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
1049 | IN UINT8 Slot\r | |
1050 | )\r | |
1051 | {\r | |
1052 | EFI_STATUS Status;\r | |
1053 | UINT8 Timeout;\r | |
1054 | \r | |
1055 | Timeout = 0x0E;\r | |
1056 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_TIMEOUT_CTRL, FALSE, sizeof (Timeout), &Timeout);\r | |
1057 | \r | |
1058 | return Status;\r | |
1059 | }\r | |
1060 | \r | |
1061 | /**\r | |
1062 | Initial SD/MMC host controller with lowest clock frequency, max power and max timeout value\r | |
1063 | at initialization.\r | |
1064 | \r | |
b23fc39c | 1065 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r |
48555339 | 1066 | @param[in] Slot The slot number of the SD card to send the command to.\r |
48555339 FT |
1067 | \r |
1068 | @retval EFI_SUCCESS The host controller is initialized successfully.\r | |
1069 | @retval Others The host controller isn't initialized successfully.\r | |
1070 | \r | |
1071 | **/\r | |
1072 | EFI_STATUS\r | |
1073 | SdMmcHcInitHost (\r | |
b23fc39c AB |
1074 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r |
1075 | IN UINT8 Slot\r | |
48555339 FT |
1076 | )\r |
1077 | {\r | |
b23fc39c AB |
1078 | EFI_STATUS Status;\r |
1079 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1080 | SD_MMC_HC_SLOT_CAP Capability;\r | |
1081 | \r | |
1082 | //\r | |
1083 | // Notify the SD/MMC override protocol that we are about to initialize\r | |
1084 | // the SD/MMC host controller.\r | |
1085 | //\r | |
1086 | if (mOverride != NULL && mOverride->NotifyPhase != NULL) {\r | |
1087 | Status = mOverride->NotifyPhase (\r | |
1088 | Private->ControllerHandle,\r | |
1089 | Slot,\r | |
1090 | EdkiiSdMmcInitHostPre);\r | |
1091 | if (EFI_ERROR (Status)) {\r | |
1092 | DEBUG ((DEBUG_WARN,\r | |
1093 | "%a: SD/MMC pre init notifier callback failed - %r\n",\r | |
1094 | __FUNCTION__, Status));\r | |
1095 | return Status;\r | |
1096 | }\r | |
1097 | }\r | |
1098 | \r | |
1099 | PciIo = Private->PciIo;\r | |
1100 | Capability = Private->Capability[Slot];\r | |
48555339 FT |
1101 | \r |
1102 | Status = SdMmcHcInitClockFreq (PciIo, Slot, Capability);\r | |
1103 | if (EFI_ERROR (Status)) {\r | |
1104 | return Status;\r | |
1105 | }\r | |
1106 | \r | |
1107 | Status = SdMmcHcInitPowerVoltage (PciIo, Slot, Capability);\r | |
1108 | if (EFI_ERROR (Status)) {\r | |
1109 | return Status;\r | |
1110 | }\r | |
1111 | \r | |
1112 | Status = SdMmcHcInitTimeoutCtrl (PciIo, Slot);\r | |
b23fc39c AB |
1113 | if (EFI_ERROR (Status)) {\r |
1114 | return Status;\r | |
1115 | }\r | |
1116 | \r | |
1117 | //\r | |
1118 | // Notify the SD/MMC override protocol that we are have just initialized\r | |
1119 | // the SD/MMC host controller.\r | |
1120 | //\r | |
1121 | if (mOverride != NULL && mOverride->NotifyPhase != NULL) {\r | |
1122 | Status = mOverride->NotifyPhase (\r | |
1123 | Private->ControllerHandle,\r | |
1124 | Slot,\r | |
1125 | EdkiiSdMmcInitHostPost);\r | |
1126 | if (EFI_ERROR (Status)) {\r | |
1127 | DEBUG ((DEBUG_WARN,\r | |
1128 | "%a: SD/MMC post init notifier callback failed - %r\n",\r | |
1129 | __FUNCTION__, Status));\r | |
1130 | }\r | |
1131 | }\r | |
48555339 FT |
1132 | return Status;\r |
1133 | }\r | |
1134 | \r | |
1135 | /**\r | |
1136 | Turn on/off LED.\r | |
1137 | \r | |
1138 | @param[in] PciIo The PCI IO protocol instance.\r | |
1139 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
1140 | @param[in] On The boolean to turn on/off LED.\r | |
1141 | \r | |
1142 | @retval EFI_SUCCESS The LED is turned on/off successfully.\r | |
1143 | @retval Others The LED isn't turned on/off successfully.\r | |
1144 | \r | |
1145 | **/\r | |
1146 | EFI_STATUS\r | |
1147 | SdMmcHcLedOnOff (\r | |
1148 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
1149 | IN UINT8 Slot,\r | |
1150 | IN BOOLEAN On\r | |
1151 | )\r | |
1152 | {\r | |
1153 | EFI_STATUS Status;\r | |
1154 | UINT8 HostCtrl1;\r | |
1155 | \r | |
1156 | if (On) {\r | |
1157 | HostCtrl1 = BIT0;\r | |
1158 | Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);\r | |
1159 | } else {\r | |
1160 | HostCtrl1 = (UINT8)~BIT0;\r | |
1161 | Status = SdMmcHcAndMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);\r | |
1162 | }\r | |
1163 | \r | |
1164 | return Status;\r | |
1165 | }\r | |
1166 | \r | |
1167 | /**\r | |
1168 | Build ADMA descriptor table for transfer.\r | |
1169 | \r | |
1170 | Refer to SD Host Controller Simplified spec 3.0 Section 1.13 for details.\r | |
1171 | \r | |
1172 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1173 | \r | |
1174 | @retval EFI_SUCCESS The ADMA descriptor table is created successfully.\r | |
1175 | @retval Others The ADMA descriptor table isn't created successfully.\r | |
1176 | \r | |
1177 | **/\r | |
1178 | EFI_STATUS\r | |
1179 | BuildAdmaDescTable (\r | |
1180 | IN SD_MMC_HC_TRB *Trb\r | |
1181 | )\r | |
1182 | {\r | |
1183 | EFI_PHYSICAL_ADDRESS Data;\r | |
1184 | UINT64 DataLen;\r | |
1185 | UINT64 Entries;\r | |
1186 | UINT32 Index;\r | |
1187 | UINT64 Remaining;\r | |
1188 | UINT32 Address;\r | |
1189 | UINTN TableSize;\r | |
1190 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1191 | EFI_STATUS Status;\r | |
1192 | UINTN Bytes;\r | |
1193 | \r | |
1194 | Data = Trb->DataPhy;\r | |
1195 | DataLen = Trb->DataLen;\r | |
1196 | PciIo = Trb->Private->PciIo;\r | |
1197 | //\r | |
1198 | // Only support 32bit ADMA Descriptor Table\r | |
1199 | //\r | |
1200 | if ((Data >= 0x100000000ul) || ((Data + DataLen) > 0x100000000ul)) {\r | |
1201 | return EFI_INVALID_PARAMETER;\r | |
1202 | }\r | |
1203 | //\r | |
1204 | // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)\r | |
1205 | // for 32-bit address descriptor table.\r | |
1206 | //\r | |
1207 | if ((Data & (BIT0 | BIT1)) != 0) {\r | |
e27ccaba | 1208 | DEBUG ((DEBUG_INFO, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data));\r |
48555339 FT |
1209 | }\r |
1210 | \r | |
1211 | Entries = DivU64x32 ((DataLen + ADMA_MAX_DATA_PER_LINE - 1), ADMA_MAX_DATA_PER_LINE);\r | |
1212 | TableSize = (UINTN)MultU64x32 (Entries, sizeof (SD_MMC_HC_ADMA_DESC_LINE));\r | |
1213 | Trb->AdmaPages = (UINT32)EFI_SIZE_TO_PAGES (TableSize);\r | |
1214 | Status = PciIo->AllocateBuffer (\r | |
1215 | PciIo,\r | |
1216 | AllocateAnyPages,\r | |
1217 | EfiBootServicesData,\r | |
1218 | EFI_SIZE_TO_PAGES (TableSize),\r | |
1219 | (VOID **)&Trb->AdmaDesc,\r | |
1220 | 0\r | |
1221 | );\r | |
1222 | if (EFI_ERROR (Status)) {\r | |
1223 | return EFI_OUT_OF_RESOURCES;\r | |
1224 | }\r | |
1225 | ZeroMem (Trb->AdmaDesc, TableSize);\r | |
1226 | Bytes = TableSize;\r | |
1227 | Status = PciIo->Map (\r | |
1228 | PciIo,\r | |
1229 | EfiPciIoOperationBusMasterCommonBuffer,\r | |
1230 | Trb->AdmaDesc,\r | |
1231 | &Bytes,\r | |
1232 | &Trb->AdmaDescPhy,\r | |
1233 | &Trb->AdmaMap\r | |
1234 | );\r | |
1235 | \r | |
1236 | if (EFI_ERROR (Status) || (Bytes != TableSize)) {\r | |
1237 | //\r | |
1238 | // Map error or unable to map the whole RFis buffer into a contiguous region.\r | |
1239 | //\r | |
1240 | PciIo->FreeBuffer (\r | |
1241 | PciIo,\r | |
1242 | EFI_SIZE_TO_PAGES (TableSize),\r | |
1243 | Trb->AdmaDesc\r | |
1244 | );\r | |
1245 | return EFI_OUT_OF_RESOURCES;\r | |
1246 | }\r | |
1247 | \r | |
1248 | if ((UINT64)(UINTN)Trb->AdmaDescPhy > 0x100000000ul) {\r | |
1249 | //\r | |
1250 | // The ADMA doesn't support 64bit addressing.\r | |
1251 | //\r | |
1252 | PciIo->Unmap (\r | |
1253 | PciIo,\r | |
1254 | Trb->AdmaMap\r | |
1255 | );\r | |
1256 | PciIo->FreeBuffer (\r | |
1257 | PciIo,\r | |
1258 | EFI_SIZE_TO_PAGES (TableSize),\r | |
1259 | Trb->AdmaDesc\r | |
1260 | );\r | |
1261 | return EFI_DEVICE_ERROR;\r | |
1262 | }\r | |
1263 | \r | |
1264 | Remaining = DataLen;\r | |
1265 | Address = (UINT32)Data;\r | |
1266 | for (Index = 0; Index < Entries; Index++) {\r | |
1267 | if (Remaining <= ADMA_MAX_DATA_PER_LINE) {\r | |
1268 | Trb->AdmaDesc[Index].Valid = 1;\r | |
1269 | Trb->AdmaDesc[Index].Act = 2;\r | |
1270 | Trb->AdmaDesc[Index].Length = (UINT16)Remaining;\r | |
1271 | Trb->AdmaDesc[Index].Address = Address;\r | |
1272 | break;\r | |
1273 | } else {\r | |
1274 | Trb->AdmaDesc[Index].Valid = 1;\r | |
1275 | Trb->AdmaDesc[Index].Act = 2;\r | |
1276 | Trb->AdmaDesc[Index].Length = 0;\r | |
1277 | Trb->AdmaDesc[Index].Address = Address;\r | |
1278 | }\r | |
1279 | \r | |
1280 | Remaining -= ADMA_MAX_DATA_PER_LINE;\r | |
1281 | Address += ADMA_MAX_DATA_PER_LINE;\r | |
1282 | }\r | |
1283 | \r | |
1284 | //\r | |
1285 | // Set the last descriptor line as end of descriptor table\r | |
1286 | //\r | |
1287 | Trb->AdmaDesc[Index].End = 1;\r | |
1288 | return EFI_SUCCESS;\r | |
1289 | }\r | |
1290 | \r | |
1291 | /**\r | |
1292 | Create a new TRB for the SD/MMC cmd request.\r | |
1293 | \r | |
1294 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1295 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
1296 | @param[in] Packet A pointer to the SD command data structure.\r | |
1297 | @param[in] Event If Event is NULL, blocking I/O is performed. If Event is\r | |
1298 | not NULL, then nonblocking I/O is performed, and Event\r | |
1299 | will be signaled when the Packet completes.\r | |
1300 | \r | |
1301 | @return Created Trb or NULL.\r | |
1302 | \r | |
1303 | **/\r | |
1304 | SD_MMC_HC_TRB *\r | |
1305 | SdMmcCreateTrb (\r | |
1306 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1307 | IN UINT8 Slot,\r | |
1308 | IN EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet,\r | |
1309 | IN EFI_EVENT Event\r | |
1310 | )\r | |
1311 | {\r | |
1312 | SD_MMC_HC_TRB *Trb;\r | |
1313 | EFI_STATUS Status;\r | |
1314 | EFI_TPL OldTpl;\r | |
1315 | EFI_PCI_IO_PROTOCOL_OPERATION Flag;\r | |
1316 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1317 | UINTN MapLength;\r | |
1318 | \r | |
1319 | Trb = AllocateZeroPool (sizeof (SD_MMC_HC_TRB));\r | |
1320 | if (Trb == NULL) {\r | |
1321 | return NULL;\r | |
1322 | }\r | |
1323 | \r | |
1324 | Trb->Signature = SD_MMC_HC_TRB_SIG;\r | |
1325 | Trb->Slot = Slot;\r | |
1326 | Trb->BlockSize = 0x200;\r | |
1327 | Trb->Packet = Packet;\r | |
1328 | Trb->Event = Event;\r | |
1329 | Trb->Started = FALSE;\r | |
1330 | Trb->Timeout = Packet->Timeout;\r | |
1331 | Trb->Private = Private;\r | |
1332 | \r | |
1333 | if ((Packet->InTransferLength != 0) && (Packet->InDataBuffer != NULL)) {\r | |
1334 | Trb->Data = Packet->InDataBuffer;\r | |
1335 | Trb->DataLen = Packet->InTransferLength;\r | |
1336 | Trb->Read = TRUE;\r | |
1337 | } else if ((Packet->OutTransferLength != 0) && (Packet->OutDataBuffer != NULL)) {\r | |
1338 | Trb->Data = Packet->OutDataBuffer;\r | |
1339 | Trb->DataLen = Packet->OutTransferLength;\r | |
1340 | Trb->Read = FALSE;\r | |
1341 | } else if ((Packet->InTransferLength == 0) && (Packet->OutTransferLength == 0)) {\r | |
1342 | Trb->Data = NULL;\r | |
1343 | Trb->DataLen = 0;\r | |
1344 | } else {\r | |
1345 | goto Error;\r | |
1346 | }\r | |
1347 | \r | |
54228046 | 1348 | if ((Trb->DataLen != 0) && (Trb->DataLen < Trb->BlockSize)) {\r |
e7e89b08 FT |
1349 | Trb->BlockSize = (UINT16)Trb->DataLen;\r |
1350 | }\r | |
1351 | \r | |
1352 | if (((Private->Slot[Trb->Slot].CardType == EmmcCardType) &&\r | |
1353 | (Packet->SdMmcCmdBlk->CommandIndex == EMMC_SEND_TUNING_BLOCK)) ||\r | |
1354 | ((Private->Slot[Trb->Slot].CardType == SdCardType) &&\r | |
1355 | (Packet->SdMmcCmdBlk->CommandIndex == SD_SEND_TUNING_BLOCK))) {\r | |
1356 | Trb->Mode = SdMmcPioMode;\r | |
48555339 | 1357 | } else {\r |
e7e89b08 FT |
1358 | if (Trb->Read) {\r |
1359 | Flag = EfiPciIoOperationBusMasterWrite;\r | |
1360 | } else {\r | |
1361 | Flag = EfiPciIoOperationBusMasterRead;\r | |
48555339 | 1362 | }\r |
48555339 | 1363 | \r |
e7e89b08 FT |
1364 | PciIo = Private->PciIo;\r |
1365 | if (Trb->DataLen != 0) {\r | |
1366 | MapLength = Trb->DataLen;\r | |
1367 | Status = PciIo->Map (\r | |
1368 | PciIo,\r | |
1369 | Flag,\r | |
1370 | Trb->Data,\r | |
1371 | &MapLength,\r | |
1372 | &Trb->DataPhy,\r | |
1373 | &Trb->DataMap\r | |
1374 | );\r | |
1375 | if (EFI_ERROR (Status) || (Trb->DataLen != MapLength)) {\r | |
1376 | Status = EFI_BAD_BUFFER_SIZE;\r | |
1377 | goto Error;\r | |
1378 | }\r | |
48555339 | 1379 | }\r |
48555339 | 1380 | \r |
e7e89b08 FT |
1381 | if (Trb->DataLen == 0) {\r |
1382 | Trb->Mode = SdMmcNoData;\r | |
1383 | } else if (Private->Capability[Slot].Adma2 != 0) {\r | |
1384 | Trb->Mode = SdMmcAdmaMode;\r | |
1385 | Status = BuildAdmaDescTable (Trb);\r | |
1386 | if (EFI_ERROR (Status)) {\r | |
1387 | PciIo->Unmap (PciIo, Trb->DataMap);\r | |
1388 | goto Error;\r | |
1389 | }\r | |
1390 | } else if (Private->Capability[Slot].Sdma != 0) {\r | |
1391 | Trb->Mode = SdMmcSdmaMode;\r | |
1392 | } else {\r | |
1393 | Trb->Mode = SdMmcPioMode;\r | |
48555339 | 1394 | }\r |
48555339 FT |
1395 | }\r |
1396 | \r | |
1397 | if (Event != NULL) {\r | |
3b1d8241 | 1398 | OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r |
48555339 FT |
1399 | InsertTailList (&Private->Queue, &Trb->TrbList);\r |
1400 | gBS->RestoreTPL (OldTpl);\r | |
1401 | }\r | |
1402 | \r | |
1403 | return Trb;\r | |
1404 | \r | |
1405 | Error:\r | |
1406 | SdMmcFreeTrb (Trb);\r | |
1407 | return NULL;\r | |
1408 | }\r | |
1409 | \r | |
1410 | /**\r | |
1411 | Free the resource used by the TRB.\r | |
1412 | \r | |
1413 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1414 | \r | |
1415 | **/\r | |
1416 | VOID\r | |
1417 | SdMmcFreeTrb (\r | |
1418 | IN SD_MMC_HC_TRB *Trb\r | |
1419 | )\r | |
1420 | {\r | |
1421 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1422 | \r | |
1423 | PciIo = Trb->Private->PciIo;\r | |
1424 | \r | |
1425 | if (Trb->AdmaMap != NULL) {\r | |
1426 | PciIo->Unmap (\r | |
1427 | PciIo,\r | |
1428 | Trb->AdmaMap\r | |
1429 | );\r | |
1430 | }\r | |
1431 | if (Trb->AdmaDesc != NULL) {\r | |
1432 | PciIo->FreeBuffer (\r | |
1433 | PciIo,\r | |
1434 | Trb->AdmaPages,\r | |
1435 | Trb->AdmaDesc\r | |
1436 | );\r | |
1437 | }\r | |
1438 | if (Trb->DataMap != NULL) {\r | |
1439 | PciIo->Unmap (\r | |
1440 | PciIo,\r | |
1441 | Trb->DataMap\r | |
1442 | );\r | |
1443 | }\r | |
1444 | FreePool (Trb);\r | |
1445 | return;\r | |
1446 | }\r | |
1447 | \r | |
1448 | /**\r | |
1449 | Check if the env is ready for execute specified TRB.\r | |
1450 | \r | |
1451 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1452 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1453 | \r | |
1454 | @retval EFI_SUCCESS The env is ready for TRB execution.\r | |
1455 | @retval EFI_NOT_READY The env is not ready for TRB execution.\r | |
1456 | @retval Others Some erros happen.\r | |
1457 | \r | |
1458 | **/\r | |
1459 | EFI_STATUS\r | |
1460 | SdMmcCheckTrbEnv (\r | |
1461 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1462 | IN SD_MMC_HC_TRB *Trb\r | |
1463 | )\r | |
1464 | {\r | |
1465 | EFI_STATUS Status;\r | |
1466 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1467 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1468 | UINT32 PresentState;\r | |
1469 | \r | |
1470 | Packet = Trb->Packet;\r | |
1471 | \r | |
1472 | if ((Packet->SdMmcCmdBlk->CommandType == SdMmcCommandTypeAdtc) ||\r | |
1473 | (Packet->SdMmcCmdBlk->ResponseType == SdMmcResponseTypeR1b) ||\r | |
1474 | (Packet->SdMmcCmdBlk->ResponseType == SdMmcResponseTypeR5b)) {\r | |
1475 | //\r | |
1476 | // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in\r | |
1477 | // the Present State register to be 0\r | |
1478 | //\r | |
1479 | PresentState = BIT0 | BIT1;\r | |
48555339 FT |
1480 | } else {\r |
1481 | //\r | |
1482 | // Wait Command Inhibit (CMD) in the Present State register\r | |
1483 | // to be 0\r | |
1484 | //\r | |
1485 | PresentState = BIT0;\r | |
1486 | }\r | |
1487 | \r | |
1488 | PciIo = Private->PciIo;\r | |
1489 | Status = SdMmcHcCheckMmioSet (\r | |
1490 | PciIo,\r | |
1491 | Trb->Slot,\r | |
1492 | SD_MMC_HC_PRESENT_STATE,\r | |
1493 | sizeof (PresentState),\r | |
1494 | PresentState,\r | |
1495 | 0\r | |
1496 | );\r | |
1497 | \r | |
1498 | return Status;\r | |
1499 | }\r | |
1500 | \r | |
1501 | /**\r | |
1502 | Wait for the env to be ready for execute specified TRB.\r | |
1503 | \r | |
1504 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1505 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1506 | \r | |
1507 | @retval EFI_SUCCESS The env is ready for TRB execution.\r | |
1508 | @retval EFI_TIMEOUT The env is not ready for TRB execution in time.\r | |
1509 | @retval Others Some erros happen.\r | |
1510 | \r | |
1511 | **/\r | |
1512 | EFI_STATUS\r | |
1513 | SdMmcWaitTrbEnv (\r | |
1514 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1515 | IN SD_MMC_HC_TRB *Trb\r | |
1516 | )\r | |
1517 | {\r | |
1518 | EFI_STATUS Status;\r | |
1519 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1520 | UINT64 Timeout;\r | |
1521 | BOOLEAN InfiniteWait;\r | |
1522 | \r | |
1523 | //\r | |
1524 | // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register\r | |
1525 | //\r | |
1526 | Packet = Trb->Packet;\r | |
1527 | Timeout = Packet->Timeout;\r | |
1528 | if (Timeout == 0) {\r | |
1529 | InfiniteWait = TRUE;\r | |
1530 | } else {\r | |
1531 | InfiniteWait = FALSE;\r | |
1532 | }\r | |
1533 | \r | |
1534 | while (InfiniteWait || (Timeout > 0)) {\r | |
1535 | //\r | |
1536 | // Check Trb execution result by reading Normal Interrupt Status register.\r | |
1537 | //\r | |
1538 | Status = SdMmcCheckTrbEnv (Private, Trb);\r | |
1539 | if (Status != EFI_NOT_READY) {\r | |
1540 | return Status;\r | |
1541 | }\r | |
1542 | //\r | |
1543 | // Stall for 1 microsecond.\r | |
1544 | //\r | |
1545 | gBS->Stall (1);\r | |
1546 | \r | |
1547 | Timeout--;\r | |
1548 | }\r | |
1549 | \r | |
1550 | return EFI_TIMEOUT;\r | |
1551 | }\r | |
1552 | \r | |
1553 | /**\r | |
1554 | Execute the specified TRB.\r | |
1555 | \r | |
1556 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1557 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1558 | \r | |
1559 | @retval EFI_SUCCESS The TRB is sent to host controller successfully.\r | |
1560 | @retval Others Some erros happen when sending this request to the host controller.\r | |
1561 | \r | |
1562 | **/\r | |
1563 | EFI_STATUS\r | |
1564 | SdMmcExecTrb (\r | |
1565 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1566 | IN SD_MMC_HC_TRB *Trb\r | |
1567 | )\r | |
1568 | {\r | |
1569 | EFI_STATUS Status;\r | |
1570 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1571 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1572 | UINT16 Cmd;\r | |
1573 | UINT16 IntStatus;\r | |
1574 | UINT32 Argument;\r | |
1575 | UINT16 BlkCount;\r | |
1576 | UINT16 BlkSize;\r | |
1577 | UINT16 TransMode;\r | |
1578 | UINT8 HostCtrl1;\r | |
1579 | UINT32 SdmaAddr;\r | |
1580 | UINT64 AdmaAddr;\r | |
1581 | \r | |
1582 | Packet = Trb->Packet;\r | |
1583 | PciIo = Trb->Private->PciIo;\r | |
1584 | //\r | |
1585 | // Clear all bits in Error Interrupt Status Register\r | |
1586 | //\r | |
1587 | IntStatus = 0xFFFF;\r | |
1588 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_ERR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);\r | |
1589 | if (EFI_ERROR (Status)) {\r | |
1590 | return Status;\r | |
1591 | }\r | |
1592 | //\r | |
1593 | // Clear all bits in Normal Interrupt Status Register excepts for Card Removal & Card Insertion bits.\r | |
1594 | //\r | |
1595 | IntStatus = 0xFF3F;\r | |
1596 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);\r | |
1597 | if (EFI_ERROR (Status)) {\r | |
1598 | return Status;\r | |
1599 | }\r | |
1600 | //\r | |
1601 | // Set Host Control 1 register DMA Select field\r | |
1602 | //\r | |
1603 | if (Trb->Mode == SdMmcAdmaMode) {\r | |
1604 | HostCtrl1 = BIT4;\r | |
1605 | Status = SdMmcHcOrMmio (PciIo, Trb->Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);\r | |
1606 | if (EFI_ERROR (Status)) {\r | |
1607 | return Status;\r | |
1608 | }\r | |
1609 | }\r | |
1610 | \r | |
1611 | SdMmcHcLedOnOff (PciIo, Trb->Slot, TRUE);\r | |
1612 | \r | |
1613 | if (Trb->Mode == SdMmcSdmaMode) {\r | |
1614 | if ((UINT64)(UINTN)Trb->DataPhy >= 0x100000000ul) {\r | |
1615 | return EFI_INVALID_PARAMETER;\r | |
1616 | }\r | |
1617 | \r | |
1618 | SdmaAddr = (UINT32)(UINTN)Trb->DataPhy;\r | |
1619 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_SDMA_ADDR, FALSE, sizeof (SdmaAddr), &SdmaAddr);\r | |
1620 | if (EFI_ERROR (Status)) {\r | |
1621 | return Status;\r | |
1622 | }\r | |
1623 | } else if (Trb->Mode == SdMmcAdmaMode) {\r | |
1624 | AdmaAddr = (UINT64)(UINTN)Trb->AdmaDescPhy;\r | |
1625 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_ADMA_SYS_ADDR, FALSE, sizeof (AdmaAddr), &AdmaAddr);\r | |
1626 | if (EFI_ERROR (Status)) {\r | |
1627 | return Status;\r | |
1628 | }\r | |
1629 | }\r | |
1630 | \r | |
1631 | BlkSize = Trb->BlockSize;\r | |
1632 | if (Trb->Mode == SdMmcSdmaMode) {\r | |
1633 | //\r | |
1634 | // Set SDMA boundary to be 512K bytes.\r | |
1635 | //\r | |
1636 | BlkSize |= 0x7000;\r | |
1637 | }\r | |
1638 | \r | |
1639 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_BLK_SIZE, FALSE, sizeof (BlkSize), &BlkSize);\r | |
1640 | if (EFI_ERROR (Status)) {\r | |
1641 | return Status;\r | |
1642 | }\r | |
1643 | \r | |
e7e89b08 FT |
1644 | BlkCount = 0;\r |
1645 | if (Trb->Mode != SdMmcNoData) {\r | |
1646 | //\r | |
1647 | // Calcuate Block Count.\r | |
1648 | //\r | |
1649 | BlkCount = (UINT16)(Trb->DataLen / Trb->BlockSize);\r | |
1650 | }\r | |
48555339 FT |
1651 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_BLK_COUNT, FALSE, sizeof (BlkCount), &BlkCount);\r |
1652 | if (EFI_ERROR (Status)) {\r | |
1653 | return Status;\r | |
1654 | }\r | |
1655 | \r | |
1656 | Argument = Packet->SdMmcCmdBlk->CommandArgument;\r | |
1657 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_ARG1, FALSE, sizeof (Argument), &Argument);\r | |
1658 | if (EFI_ERROR (Status)) {\r | |
1659 | return Status;\r | |
1660 | }\r | |
1661 | \r | |
1662 | TransMode = 0;\r | |
1663 | if (Trb->Mode != SdMmcNoData) {\r | |
1664 | if (Trb->Mode != SdMmcPioMode) {\r | |
1665 | TransMode |= BIT0;\r | |
1666 | }\r | |
1667 | if (Trb->Read) {\r | |
1668 | TransMode |= BIT4;\r | |
1669 | }\r | |
e7e89b08 | 1670 | if (BlkCount > 1) {\r |
48555339 FT |
1671 | TransMode |= BIT5 | BIT1;\r |
1672 | }\r | |
1673 | //\r | |
1674 | // Only SD memory card needs to use AUTO CMD12 feature.\r | |
1675 | //\r | |
1676 | if (Private->Slot[Trb->Slot].CardType == SdCardType) {\r | |
1677 | if (BlkCount > 1) {\r | |
1678 | TransMode |= BIT2;\r | |
1679 | }\r | |
1680 | }\r | |
1681 | }\r | |
1682 | \r | |
1683 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_TRANS_MOD, FALSE, sizeof (TransMode), &TransMode);\r | |
1684 | if (EFI_ERROR (Status)) {\r | |
1685 | return Status;\r | |
1686 | }\r | |
1687 | \r | |
1688 | Cmd = (UINT16)LShiftU64(Packet->SdMmcCmdBlk->CommandIndex, 8);\r | |
1689 | if (Packet->SdMmcCmdBlk->CommandType == SdMmcCommandTypeAdtc) {\r | |
1690 | Cmd |= BIT5;\r | |
1691 | }\r | |
1692 | //\r | |
1693 | // Convert ResponseType to value\r | |
1694 | //\r | |
1695 | if (Packet->SdMmcCmdBlk->CommandType != SdMmcCommandTypeBc) {\r | |
1696 | switch (Packet->SdMmcCmdBlk->ResponseType) {\r | |
1697 | case SdMmcResponseTypeR1:\r | |
1698 | case SdMmcResponseTypeR5:\r | |
1699 | case SdMmcResponseTypeR6:\r | |
1700 | case SdMmcResponseTypeR7:\r | |
1701 | Cmd |= (BIT1 | BIT3 | BIT4);\r | |
1702 | break;\r | |
1703 | case SdMmcResponseTypeR2:\r | |
1704 | Cmd |= (BIT0 | BIT3);\r | |
1705 | break;\r | |
1706 | case SdMmcResponseTypeR3:\r | |
1707 | case SdMmcResponseTypeR4:\r | |
1708 | Cmd |= BIT1;\r | |
1709 | break;\r | |
1710 | case SdMmcResponseTypeR1b:\r | |
1711 | case SdMmcResponseTypeR5b:\r | |
1712 | Cmd |= (BIT0 | BIT1 | BIT3 | BIT4);\r | |
1713 | break;\r | |
1714 | default:\r | |
1715 | ASSERT (FALSE);\r | |
1716 | break;\r | |
1717 | }\r | |
1718 | }\r | |
1719 | //\r | |
1720 | // Execute cmd\r | |
1721 | //\r | |
1722 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_COMMAND, FALSE, sizeof (Cmd), &Cmd);\r | |
1723 | return Status;\r | |
1724 | }\r | |
1725 | \r | |
1726 | /**\r | |
1727 | Check the TRB execution result.\r | |
1728 | \r | |
1729 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1730 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1731 | \r | |
1732 | @retval EFI_SUCCESS The TRB is executed successfully.\r | |
1733 | @retval EFI_NOT_READY The TRB is not completed for execution.\r | |
1734 | @retval Others Some erros happen when executing this request.\r | |
1735 | \r | |
1736 | **/\r | |
1737 | EFI_STATUS\r | |
1738 | SdMmcCheckTrbResult (\r | |
1739 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1740 | IN SD_MMC_HC_TRB *Trb\r | |
1741 | )\r | |
1742 | {\r | |
1743 | EFI_STATUS Status;\r | |
1744 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1745 | UINT16 IntStatus;\r | |
1746 | UINT32 Response[4];\r | |
1747 | UINT32 SdmaAddr;\r | |
1748 | UINT8 Index;\r | |
1749 | UINT8 SwReset;\r | |
e7e89b08 | 1750 | UINT32 PioLength;\r |
48555339 FT |
1751 | \r |
1752 | SwReset = 0;\r | |
1753 | Packet = Trb->Packet;\r | |
1754 | //\r | |
1755 | // Check Trb execution result by reading Normal Interrupt Status register.\r | |
1756 | //\r | |
1757 | Status = SdMmcHcRwMmio (\r | |
1758 | Private->PciIo,\r | |
1759 | Trb->Slot,\r | |
1760 | SD_MMC_HC_NOR_INT_STS,\r | |
1761 | TRUE,\r | |
1762 | sizeof (IntStatus),\r | |
1763 | &IntStatus\r | |
1764 | );\r | |
1765 | if (EFI_ERROR (Status)) {\r | |
1766 | goto Done;\r | |
1767 | }\r | |
1768 | //\r | |
1769 | // Check Transfer Complete bit is set or not.\r | |
1770 | //\r | |
1771 | if ((IntStatus & BIT1) == BIT1) {\r | |
1772 | if ((IntStatus & BIT15) == BIT15) {\r | |
1773 | //\r | |
1774 | // Read Error Interrupt Status register to check if the error is\r | |
1775 | // Data Timeout Error.\r | |
1776 | // If yes, treat it as success as Transfer Complete has higher\r | |
1777 | // priority than Data Timeout Error.\r | |
1778 | //\r | |
1779 | Status = SdMmcHcRwMmio (\r | |
1780 | Private->PciIo,\r | |
1781 | Trb->Slot,\r | |
1782 | SD_MMC_HC_ERR_INT_STS,\r | |
1783 | TRUE,\r | |
1784 | sizeof (IntStatus),\r | |
1785 | &IntStatus\r | |
1786 | );\r | |
1787 | if (!EFI_ERROR (Status)) {\r | |
1788 | if ((IntStatus & BIT4) == BIT4) {\r | |
1789 | Status = EFI_SUCCESS;\r | |
1790 | } else {\r | |
1791 | Status = EFI_DEVICE_ERROR;\r | |
1792 | }\r | |
1793 | }\r | |
1794 | }\r | |
1795 | \r | |
1796 | goto Done;\r | |
1797 | }\r | |
1798 | //\r | |
1799 | // Check if there is a error happened during cmd execution.\r | |
1800 | // If yes, then do error recovery procedure to follow SD Host Controller\r | |
1801 | // Simplified Spec 3.0 section 3.10.1.\r | |
1802 | //\r | |
1803 | if ((IntStatus & BIT15) == BIT15) {\r | |
1804 | Status = SdMmcHcRwMmio (\r | |
1805 | Private->PciIo,\r | |
1806 | Trb->Slot,\r | |
1807 | SD_MMC_HC_ERR_INT_STS,\r | |
1808 | TRUE,\r | |
1809 | sizeof (IntStatus),\r | |
1810 | &IntStatus\r | |
1811 | );\r | |
1812 | if (EFI_ERROR (Status)) {\r | |
1813 | goto Done;\r | |
1814 | }\r | |
1815 | if ((IntStatus & 0x0F) != 0) {\r | |
1816 | SwReset |= BIT1;\r | |
1817 | }\r | |
1818 | if ((IntStatus & 0xF0) != 0) {\r | |
1819 | SwReset |= BIT2;\r | |
1820 | }\r | |
1821 | \r | |
1822 | Status = SdMmcHcRwMmio (\r | |
1823 | Private->PciIo,\r | |
1824 | Trb->Slot,\r | |
1825 | SD_MMC_HC_SW_RST,\r | |
1826 | FALSE,\r | |
1827 | sizeof (SwReset),\r | |
1828 | &SwReset\r | |
1829 | );\r | |
1830 | if (EFI_ERROR (Status)) {\r | |
1831 | goto Done;\r | |
1832 | }\r | |
1833 | Status = SdMmcHcWaitMmioSet (\r | |
1834 | Private->PciIo,\r | |
1835 | Trb->Slot,\r | |
1836 | SD_MMC_HC_SW_RST,\r | |
1837 | sizeof (SwReset),\r | |
1838 | 0xFF,\r | |
1839 | 0,\r | |
1840 | SD_MMC_HC_GENERIC_TIMEOUT\r | |
1841 | );\r | |
1842 | if (EFI_ERROR (Status)) {\r | |
1843 | goto Done;\r | |
1844 | }\r | |
1845 | \r | |
1846 | Status = EFI_DEVICE_ERROR;\r | |
1847 | goto Done;\r | |
1848 | }\r | |
1849 | //\r | |
1850 | // Check if DMA interrupt is signalled for the SDMA transfer.\r | |
1851 | //\r | |
1852 | if ((Trb->Mode == SdMmcSdmaMode) && ((IntStatus & BIT3) == BIT3)) {\r | |
1853 | //\r | |
1854 | // Clear DMA interrupt bit.\r | |
1855 | //\r | |
1856 | IntStatus = BIT3;\r | |
1857 | Status = SdMmcHcRwMmio (\r | |
1858 | Private->PciIo,\r | |
1859 | Trb->Slot,\r | |
1860 | SD_MMC_HC_NOR_INT_STS,\r | |
1861 | FALSE,\r | |
1862 | sizeof (IntStatus),\r | |
1863 | &IntStatus\r | |
1864 | );\r | |
1865 | if (EFI_ERROR (Status)) {\r | |
1866 | goto Done;\r | |
1867 | }\r | |
1868 | //\r | |
1869 | // Update SDMA Address register.\r | |
1870 | //\r | |
1871 | SdmaAddr = SD_MMC_SDMA_ROUND_UP ((UINT32)(UINTN)Trb->DataPhy, SD_MMC_SDMA_BOUNDARY);\r | |
1872 | Status = SdMmcHcRwMmio (\r | |
1873 | Private->PciIo,\r | |
1874 | Trb->Slot,\r | |
1875 | SD_MMC_HC_SDMA_ADDR,\r | |
1876 | FALSE,\r | |
1877 | sizeof (UINT32),\r | |
1878 | &SdmaAddr\r | |
1879 | );\r | |
1880 | if (EFI_ERROR (Status)) {\r | |
1881 | goto Done;\r | |
1882 | }\r | |
1883 | Trb->DataPhy = (UINT32)(UINTN)SdmaAddr;\r | |
1884 | }\r | |
1885 | \r | |
1886 | if ((Packet->SdMmcCmdBlk->CommandType != SdMmcCommandTypeAdtc) &&\r | |
1887 | (Packet->SdMmcCmdBlk->ResponseType != SdMmcResponseTypeR1b) &&\r | |
1888 | (Packet->SdMmcCmdBlk->ResponseType != SdMmcResponseTypeR5b)) {\r | |
1889 | if ((IntStatus & BIT0) == BIT0) {\r | |
1890 | Status = EFI_SUCCESS;\r | |
1891 | goto Done;\r | |
1892 | }\r | |
1893 | }\r | |
1894 | \r | |
1895 | if (((Private->Slot[Trb->Slot].CardType == EmmcCardType) &&\r | |
1896 | (Packet->SdMmcCmdBlk->CommandIndex == EMMC_SEND_TUNING_BLOCK)) ||\r | |
1897 | ((Private->Slot[Trb->Slot].CardType == SdCardType) &&\r | |
1898 | (Packet->SdMmcCmdBlk->CommandIndex == SD_SEND_TUNING_BLOCK))) {\r | |
1899 | //\r | |
e7e89b08 FT |
1900 | // When performing tuning procedure (Execute Tuning is set to 1) through PIO mode,\r |
1901 | // wait Buffer Read Ready bit of Normal Interrupt Status Register to be 1.\r | |
1902 | // Refer to SD Host Controller Simplified Specification 3.0 figure 2-29 for details.\r | |
48555339 | 1903 | //\r |
e7e89b08 FT |
1904 | if ((IntStatus & BIT5) == BIT5) {\r |
1905 | //\r | |
1906 | // Clear Buffer Read Ready interrupt at first.\r | |
1907 | //\r | |
1908 | IntStatus = BIT5;\r | |
1909 | SdMmcHcRwMmio (Private->PciIo, Trb->Slot, SD_MMC_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);\r | |
1910 | //\r | |
1911 | // Read data out from Buffer Port register\r | |
1912 | //\r | |
1913 | for (PioLength = 0; PioLength < Trb->DataLen; PioLength += 4) {\r | |
1914 | SdMmcHcRwMmio (Private->PciIo, Trb->Slot, SD_MMC_HC_BUF_DAT_PORT, TRUE, 4, (UINT8*)Trb->Data + PioLength);\r | |
1915 | }\r | |
1916 | Status = EFI_SUCCESS;\r | |
1917 | goto Done;\r | |
1918 | }\r | |
48555339 FT |
1919 | }\r |
1920 | \r | |
1921 | Status = EFI_NOT_READY;\r | |
1922 | Done:\r | |
1923 | //\r | |
1924 | // Get response data when the cmd is executed successfully.\r | |
1925 | //\r | |
1926 | if (!EFI_ERROR (Status)) {\r | |
1927 | if (Packet->SdMmcCmdBlk->CommandType != SdMmcCommandTypeBc) {\r | |
1928 | for (Index = 0; Index < 4; Index++) {\r | |
1929 | Status = SdMmcHcRwMmio (\r | |
1930 | Private->PciIo,\r | |
1931 | Trb->Slot,\r | |
1932 | SD_MMC_HC_RESPONSE + Index * 4,\r | |
1933 | TRUE,\r | |
1934 | sizeof (UINT32),\r | |
1935 | &Response[Index]\r | |
1936 | );\r | |
1937 | if (EFI_ERROR (Status)) {\r | |
1938 | SdMmcHcLedOnOff (Private->PciIo, Trb->Slot, FALSE);\r | |
1939 | return Status;\r | |
1940 | }\r | |
1941 | }\r | |
1942 | CopyMem (Packet->SdMmcStatusBlk, Response, sizeof (Response));\r | |
1943 | }\r | |
1944 | }\r | |
1945 | \r | |
1946 | if (Status != EFI_NOT_READY) {\r | |
1947 | SdMmcHcLedOnOff (Private->PciIo, Trb->Slot, FALSE);\r | |
1948 | }\r | |
1949 | \r | |
1950 | return Status;\r | |
1951 | }\r | |
1952 | \r | |
1953 | /**\r | |
1954 | Wait for the TRB execution result.\r | |
1955 | \r | |
1956 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1957 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1958 | \r | |
1959 | @retval EFI_SUCCESS The TRB is executed successfully.\r | |
1960 | @retval Others Some erros happen when executing this request.\r | |
1961 | \r | |
1962 | **/\r | |
1963 | EFI_STATUS\r | |
1964 | SdMmcWaitTrbResult (\r | |
1965 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1966 | IN SD_MMC_HC_TRB *Trb\r | |
1967 | )\r | |
1968 | {\r | |
1969 | EFI_STATUS Status;\r | |
1970 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1971 | UINT64 Timeout;\r | |
1972 | BOOLEAN InfiniteWait;\r | |
1973 | \r | |
1974 | Packet = Trb->Packet;\r | |
1975 | //\r | |
1976 | // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register\r | |
1977 | //\r | |
1978 | Timeout = Packet->Timeout;\r | |
1979 | if (Timeout == 0) {\r | |
1980 | InfiniteWait = TRUE;\r | |
1981 | } else {\r | |
1982 | InfiniteWait = FALSE;\r | |
1983 | }\r | |
1984 | \r | |
1985 | while (InfiniteWait || (Timeout > 0)) {\r | |
1986 | //\r | |
1987 | // Check Trb execution result by reading Normal Interrupt Status register.\r | |
1988 | //\r | |
1989 | Status = SdMmcCheckTrbResult (Private, Trb);\r | |
1990 | if (Status != EFI_NOT_READY) {\r | |
1991 | return Status;\r | |
1992 | }\r | |
1993 | //\r | |
1994 | // Stall for 1 microsecond.\r | |
1995 | //\r | |
1996 | gBS->Stall (1);\r | |
1997 | \r | |
1998 | Timeout--;\r | |
1999 | }\r | |
2000 | \r | |
2001 | return EFI_TIMEOUT;\r | |
2002 | }\r | |
2003 | \r |