]>
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 | |
49c99534 MW |
447 | EdkiiSdMmcResetPre,\r |
448 | NULL);\r | |
b23fc39c AB |
449 | if (EFI_ERROR (Status)) {\r |
450 | DEBUG ((DEBUG_WARN,\r | |
451 | "%a: SD/MMC pre reset notifier callback failed - %r\n",\r | |
452 | __FUNCTION__, Status));\r | |
453 | return Status;\r | |
454 | }\r | |
455 | }\r | |
456 | \r | |
457 | PciIo = Private->PciIo;\r | |
064d301f TM |
458 | SwReset = BIT0;\r |
459 | Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_SW_RST, sizeof (SwReset), &SwReset);\r | |
48555339 FT |
460 | \r |
461 | if (EFI_ERROR (Status)) {\r | |
064d301f | 462 | DEBUG ((DEBUG_ERROR, "SdMmcHcReset: write SW Reset for All fails: %r\n", Status));\r |
48555339 FT |
463 | return Status;\r |
464 | }\r | |
465 | \r | |
466 | Status = SdMmcHcWaitMmioSet (\r | |
467 | PciIo,\r | |
468 | Slot,\r | |
469 | SD_MMC_HC_SW_RST,\r | |
470 | sizeof (SwReset),\r | |
064d301f | 471 | BIT0,\r |
48555339 FT |
472 | 0x00,\r |
473 | SD_MMC_HC_GENERIC_TIMEOUT\r | |
474 | );\r | |
475 | if (EFI_ERROR (Status)) {\r | |
e27ccaba | 476 | DEBUG ((DEBUG_INFO, "SdMmcHcReset: reset done with %r\n", Status));\r |
48555339 FT |
477 | return Status;\r |
478 | }\r | |
b23fc39c | 479 | \r |
48555339 FT |
480 | //\r |
481 | // Enable all interrupt after reset all.\r | |
482 | //\r | |
483 | Status = SdMmcHcEnableInterrupt (PciIo, Slot);\r | |
b23fc39c AB |
484 | if (EFI_ERROR (Status)) {\r |
485 | DEBUG ((DEBUG_INFO, "SdMmcHcReset: SdMmcHcEnableInterrupt done with %r\n",\r | |
486 | Status));\r | |
487 | return Status;\r | |
488 | }\r | |
489 | \r | |
490 | //\r | |
491 | // Notify the SD/MMC override protocol that we have just reset\r | |
492 | // the SD/MMC host controller.\r | |
493 | //\r | |
494 | if (mOverride != NULL && mOverride->NotifyPhase != NULL) {\r | |
495 | Status = mOverride->NotifyPhase (\r | |
496 | Private->ControllerHandle,\r | |
497 | Slot,\r | |
49c99534 MW |
498 | EdkiiSdMmcResetPost,\r |
499 | NULL);\r | |
b23fc39c AB |
500 | if (EFI_ERROR (Status)) {\r |
501 | DEBUG ((DEBUG_WARN,\r | |
502 | "%a: SD/MMC post reset notifier callback failed - %r\n",\r | |
503 | __FUNCTION__, Status));\r | |
504 | }\r | |
505 | }\r | |
48555339 FT |
506 | \r |
507 | return Status;\r | |
508 | }\r | |
509 | \r | |
510 | /**\r | |
511 | Set all interrupt status bits in Normal and Error Interrupt Status Enable\r | |
512 | register.\r | |
513 | \r | |
514 | @param[in] PciIo The PCI IO protocol instance.\r | |
515 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
516 | \r | |
517 | @retval EFI_SUCCESS The operation executes successfully.\r | |
518 | @retval Others The operation fails.\r | |
519 | \r | |
520 | **/\r | |
521 | EFI_STATUS\r | |
522 | SdMmcHcEnableInterrupt (\r | |
523 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
524 | IN UINT8 Slot\r | |
525 | )\r | |
526 | {\r | |
527 | EFI_STATUS Status;\r | |
528 | UINT16 IntStatus;\r | |
529 | \r | |
530 | //\r | |
531 | // Enable all bits in Error Interrupt Status Enable Register\r | |
532 | //\r | |
533 | IntStatus = 0xFFFF;\r | |
534 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_ERR_INT_STS_EN, FALSE, sizeof (IntStatus), &IntStatus);\r | |
535 | if (EFI_ERROR (Status)) {\r | |
536 | return Status;\r | |
537 | }\r | |
538 | //\r | |
539 | // Enable all bits in Normal Interrupt Status Enable Register\r | |
540 | //\r | |
541 | IntStatus = 0xFFFF;\r | |
542 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_NOR_INT_STS_EN, FALSE, sizeof (IntStatus), &IntStatus);\r | |
543 | \r | |
544 | return Status;\r | |
545 | }\r | |
546 | \r | |
547 | /**\r | |
548 | Get the capability data from the specified slot.\r | |
549 | \r | |
550 | @param[in] PciIo The PCI IO protocol instance.\r | |
551 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
552 | @param[out] Capability The buffer to store the capability data.\r | |
553 | \r | |
554 | @retval EFI_SUCCESS The operation executes successfully.\r | |
555 | @retval Others The operation fails.\r | |
556 | \r | |
557 | **/\r | |
558 | EFI_STATUS\r | |
559 | SdMmcHcGetCapability (\r | |
560 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
561 | IN UINT8 Slot,\r | |
562 | OUT SD_MMC_HC_SLOT_CAP *Capability\r | |
563 | )\r | |
564 | {\r | |
565 | EFI_STATUS Status;\r | |
566 | UINT64 Cap;\r | |
567 | \r | |
568 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_CAP, TRUE, sizeof (Cap), &Cap);\r | |
569 | if (EFI_ERROR (Status)) {\r | |
570 | return Status;\r | |
571 | }\r | |
572 | \r | |
573 | CopyMem (Capability, &Cap, sizeof (Cap));\r | |
574 | \r | |
575 | return EFI_SUCCESS;\r | |
576 | }\r | |
577 | \r | |
578 | /**\r | |
579 | Get the maximum current capability data from the specified slot.\r | |
580 | \r | |
581 | @param[in] PciIo The PCI IO protocol instance.\r | |
582 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
583 | @param[out] MaxCurrent The buffer to store the maximum current capability data.\r | |
584 | \r | |
585 | @retval EFI_SUCCESS The operation executes successfully.\r | |
586 | @retval Others The operation fails.\r | |
587 | \r | |
588 | **/\r | |
589 | EFI_STATUS\r | |
590 | SdMmcHcGetMaxCurrent (\r | |
591 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
592 | IN UINT8 Slot,\r | |
593 | OUT UINT64 *MaxCurrent\r | |
594 | )\r | |
595 | {\r | |
596 | EFI_STATUS Status;\r | |
597 | \r | |
598 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_MAX_CURRENT_CAP, TRUE, sizeof (UINT64), MaxCurrent);\r | |
599 | \r | |
600 | return Status;\r | |
601 | }\r | |
602 | \r | |
603 | /**\r | |
604 | Detect whether there is a SD/MMC card attached at the specified SD/MMC host controller\r | |
605 | slot.\r | |
606 | \r | |
607 | Refer to SD Host Controller Simplified spec 3.0 Section 3.1 for details.\r | |
608 | \r | |
609 | @param[in] PciIo The PCI IO protocol instance.\r | |
610 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
611 | @param[out] MediaPresent The pointer to the media present boolean value.\r | |
612 | \r | |
613 | @retval EFI_SUCCESS There is no media change happened.\r | |
614 | @retval EFI_MEDIA_CHANGED There is media change happened.\r | |
615 | @retval Others The detection fails.\r | |
616 | \r | |
617 | **/\r | |
618 | EFI_STATUS\r | |
619 | SdMmcHcCardDetect (\r | |
620 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
621 | IN UINT8 Slot,\r | |
622 | OUT BOOLEAN *MediaPresent\r | |
623 | )\r | |
624 | {\r | |
625 | EFI_STATUS Status;\r | |
626 | UINT16 Data;\r | |
627 | UINT32 PresentState;\r | |
628 | \r | |
2e9107b8 FT |
629 | //\r |
630 | // Check Present State Register to see if there is a card presented.\r | |
631 | //\r | |
632 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_PRESENT_STATE, TRUE, sizeof (PresentState), &PresentState);\r | |
633 | if (EFI_ERROR (Status)) {\r | |
634 | return Status;\r | |
635 | }\r | |
636 | \r | |
637 | if ((PresentState & BIT16) != 0) {\r | |
638 | *MediaPresent = TRUE;\r | |
639 | } else {\r | |
640 | *MediaPresent = FALSE;\r | |
641 | }\r | |
642 | \r | |
48555339 FT |
643 | //\r |
644 | // Check Normal Interrupt Status Register\r | |
645 | //\r | |
646 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_NOR_INT_STS, TRUE, sizeof (Data), &Data);\r | |
647 | if (EFI_ERROR (Status)) {\r | |
648 | return Status;\r | |
649 | }\r | |
650 | \r | |
651 | if ((Data & (BIT6 | BIT7)) != 0) {\r | |
652 | //\r | |
653 | // Clear BIT6 and BIT7 by writing 1 to these two bits if set.\r | |
654 | //\r | |
655 | Data &= BIT6 | BIT7;\r | |
656 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_NOR_INT_STS, FALSE, sizeof (Data), &Data);\r | |
657 | if (EFI_ERROR (Status)) {\r | |
658 | return Status;\r | |
659 | }\r | |
660 | \r | |
48555339 FT |
661 | return EFI_MEDIA_CHANGED;\r |
662 | }\r | |
663 | \r | |
664 | return EFI_SUCCESS;\r | |
665 | }\r | |
666 | \r | |
667 | /**\r | |
668 | Stop SD/MMC card clock.\r | |
669 | \r | |
670 | Refer to SD Host Controller Simplified spec 3.0 Section 3.2.2 for details.\r | |
671 | \r | |
672 | @param[in] PciIo The PCI IO protocol instance.\r | |
673 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
674 | \r | |
675 | @retval EFI_SUCCESS Succeed to stop SD/MMC clock.\r | |
676 | @retval Others Fail to stop SD/MMC clock.\r | |
677 | \r | |
678 | **/\r | |
679 | EFI_STATUS\r | |
680 | SdMmcHcStopClock (\r | |
681 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
682 | IN UINT8 Slot\r | |
683 | )\r | |
684 | {\r | |
685 | EFI_STATUS Status;\r | |
686 | UINT32 PresentState;\r | |
687 | UINT16 ClockCtrl;\r | |
688 | \r | |
689 | //\r | |
690 | // Ensure no SD transactions are occurring on the SD Bus by\r | |
691 | // waiting for Command Inhibit (DAT) and Command Inhibit (CMD)\r | |
692 | // in the Present State register to be 0.\r | |
693 | //\r | |
694 | Status = SdMmcHcWaitMmioSet (\r | |
695 | PciIo,\r | |
696 | Slot,\r | |
697 | SD_MMC_HC_PRESENT_STATE,\r | |
698 | sizeof (PresentState),\r | |
699 | BIT0 | BIT1,\r | |
700 | 0,\r | |
701 | SD_MMC_HC_GENERIC_TIMEOUT\r | |
702 | );\r | |
703 | if (EFI_ERROR (Status)) {\r | |
704 | return Status;\r | |
705 | }\r | |
706 | \r | |
707 | //\r | |
708 | // Set SD Clock Enable in the Clock Control register to 0\r | |
709 | //\r | |
710 | ClockCtrl = (UINT16)~BIT2;\r | |
711 | Status = SdMmcHcAndMmio (PciIo, Slot, SD_MMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);\r | |
712 | \r | |
713 | return Status;\r | |
714 | }\r | |
715 | \r | |
716 | /**\r | |
717 | SD/MMC card clock supply.\r | |
718 | \r | |
719 | Refer to SD Host Controller Simplified spec 3.0 Section 3.2.1 for details.\r | |
720 | \r | |
721 | @param[in] PciIo The PCI IO protocol instance.\r | |
722 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
723 | @param[in] ClockFreq The max clock frequency to be set. The unit is KHz.\r | |
724 | @param[in] Capability The capability of the slot.\r | |
725 | \r | |
726 | @retval EFI_SUCCESS The clock is supplied successfully.\r | |
727 | @retval Others The clock isn't supplied successfully.\r | |
728 | \r | |
729 | **/\r | |
730 | EFI_STATUS\r | |
731 | SdMmcHcClockSupply (\r | |
732 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
733 | IN UINT8 Slot,\r | |
734 | IN UINT64 ClockFreq,\r | |
735 | IN SD_MMC_HC_SLOT_CAP Capability\r | |
736 | )\r | |
737 | {\r | |
738 | EFI_STATUS Status;\r | |
739 | UINT32 BaseClkFreq;\r | |
740 | UINT32 SettingFreq;\r | |
741 | UINT32 Divisor;\r | |
742 | UINT32 Remainder;\r | |
743 | UINT16 ControllerVer;\r | |
744 | UINT16 ClockCtrl;\r | |
745 | \r | |
746 | //\r | |
747 | // Calculate a divisor for SD clock frequency\r | |
748 | //\r | |
749 | ASSERT (Capability.BaseClkFreq != 0);\r | |
750 | \r | |
751 | BaseClkFreq = Capability.BaseClkFreq;\r | |
cb9cb9e2 | 752 | if (ClockFreq == 0) {\r |
48555339 FT |
753 | return EFI_INVALID_PARAMETER;\r |
754 | }\r | |
cb9cb9e2 FT |
755 | \r |
756 | if (ClockFreq > (BaseClkFreq * 1000)) {\r | |
757 | ClockFreq = BaseClkFreq * 1000;\r | |
758 | }\r | |
759 | \r | |
48555339 FT |
760 | //\r |
761 | // Calculate the divisor of base frequency.\r | |
762 | //\r | |
763 | Divisor = 0;\r | |
764 | SettingFreq = BaseClkFreq * 1000;\r | |
765 | while (ClockFreq < SettingFreq) {\r | |
766 | Divisor++;\r | |
767 | \r | |
768 | SettingFreq = (BaseClkFreq * 1000) / (2 * Divisor);\r | |
769 | Remainder = (BaseClkFreq * 1000) % (2 * Divisor);\r | |
770 | if ((ClockFreq == SettingFreq) && (Remainder == 0)) {\r | |
771 | break;\r | |
772 | }\r | |
773 | if ((ClockFreq == SettingFreq) && (Remainder != 0)) {\r | |
774 | SettingFreq ++;\r | |
775 | }\r | |
776 | }\r | |
777 | \r | |
e27ccaba | 778 | DEBUG ((DEBUG_INFO, "BaseClkFreq %dMHz Divisor %d ClockFreq %dKhz\n", BaseClkFreq, Divisor, ClockFreq));\r |
48555339 FT |
779 | \r |
780 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_CTRL_VER, TRUE, sizeof (ControllerVer), &ControllerVer);\r | |
781 | if (EFI_ERROR (Status)) {\r | |
782 | return Status;\r | |
783 | }\r | |
784 | //\r | |
785 | // Set SDCLK Frequency Select and Internal Clock Enable fields in Clock Control register.\r | |
786 | //\r | |
bbce0015 JB |
787 | if (((ControllerVer & 0xFF) >= SD_MMC_HC_CTRL_VER_300) &&\r |
788 | ((ControllerVer & 0xFF) <= SD_MMC_HC_CTRL_VER_420)) {\r | |
48555339 FT |
789 | ASSERT (Divisor <= 0x3FF);\r |
790 | ClockCtrl = ((Divisor & 0xFF) << 8) | ((Divisor & 0x300) >> 2);\r | |
791 | } else if (((ControllerVer & 0xFF) == 0) || ((ControllerVer & 0xFF) == 1)) {\r | |
792 | //\r | |
793 | // Only the most significant bit can be used as divisor.\r | |
794 | //\r | |
795 | if (((Divisor - 1) & Divisor) != 0) {\r | |
796 | Divisor = 1 << (HighBitSet32 (Divisor) + 1);\r | |
797 | }\r | |
798 | ASSERT (Divisor <= 0x80);\r | |
799 | ClockCtrl = (Divisor & 0xFF) << 8;\r | |
800 | } else {\r | |
e27ccaba | 801 | DEBUG ((DEBUG_ERROR, "Unknown SD Host Controller Spec version [0x%x]!!!\n", ControllerVer));\r |
48555339 FT |
802 | return EFI_UNSUPPORTED;\r |
803 | }\r | |
804 | \r | |
805 | //\r | |
806 | // Stop bus clock at first\r | |
807 | //\r | |
808 | Status = SdMmcHcStopClock (PciIo, Slot);\r | |
809 | if (EFI_ERROR (Status)) {\r | |
810 | return Status;\r | |
811 | }\r | |
812 | \r | |
813 | //\r | |
814 | // Supply clock frequency with specified divisor\r | |
815 | //\r | |
816 | ClockCtrl |= BIT0;\r | |
817 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_CLOCK_CTRL, FALSE, sizeof (ClockCtrl), &ClockCtrl);\r | |
818 | if (EFI_ERROR (Status)) {\r | |
e27ccaba | 819 | DEBUG ((DEBUG_ERROR, "Set SDCLK Frequency Select and Internal Clock Enable fields fails\n"));\r |
48555339 FT |
820 | return Status;\r |
821 | }\r | |
822 | \r | |
823 | //\r | |
824 | // Wait Internal Clock Stable in the Clock Control register to be 1\r | |
825 | //\r | |
826 | Status = SdMmcHcWaitMmioSet (\r | |
827 | PciIo,\r | |
828 | Slot,\r | |
829 | SD_MMC_HC_CLOCK_CTRL,\r | |
830 | sizeof (ClockCtrl),\r | |
831 | BIT1,\r | |
832 | BIT1,\r | |
833 | SD_MMC_HC_GENERIC_TIMEOUT\r | |
834 | );\r | |
835 | if (EFI_ERROR (Status)) {\r | |
836 | return Status;\r | |
837 | }\r | |
838 | \r | |
839 | //\r | |
840 | // Set SD Clock Enable in the Clock Control register to 1\r | |
841 | //\r | |
842 | ClockCtrl = BIT2;\r | |
843 | Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_CLOCK_CTRL, sizeof (ClockCtrl), &ClockCtrl);\r | |
844 | \r | |
845 | return Status;\r | |
846 | }\r | |
847 | \r | |
848 | /**\r | |
849 | SD/MMC bus power control.\r | |
850 | \r | |
851 | Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.\r | |
852 | \r | |
853 | @param[in] PciIo The PCI IO protocol instance.\r | |
854 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
855 | @param[in] PowerCtrl The value setting to the power control register.\r | |
856 | \r | |
857 | @retval TRUE There is a SD/MMC card attached.\r | |
858 | @retval FALSE There is no a SD/MMC card attached.\r | |
859 | \r | |
860 | **/\r | |
861 | EFI_STATUS\r | |
862 | SdMmcHcPowerControl (\r | |
863 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
864 | IN UINT8 Slot,\r | |
865 | IN UINT8 PowerCtrl\r | |
866 | )\r | |
867 | {\r | |
868 | EFI_STATUS Status;\r | |
869 | \r | |
870 | //\r | |
871 | // Clr SD Bus Power\r | |
872 | //\r | |
873 | PowerCtrl &= (UINT8)~BIT0;\r | |
874 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_POWER_CTRL, FALSE, sizeof (PowerCtrl), &PowerCtrl);\r | |
875 | if (EFI_ERROR (Status)) {\r | |
876 | return Status;\r | |
877 | }\r | |
878 | \r | |
879 | //\r | |
880 | // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register\r | |
881 | //\r | |
882 | PowerCtrl |= BIT0;\r | |
883 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_POWER_CTRL, FALSE, sizeof (PowerCtrl), &PowerCtrl);\r | |
884 | \r | |
885 | return Status;\r | |
886 | }\r | |
887 | \r | |
888 | /**\r | |
889 | Set the SD/MMC bus width.\r | |
890 | \r | |
891 | Refer to SD Host Controller Simplified spec 3.0 Section 3.4 for details.\r | |
892 | \r | |
893 | @param[in] PciIo The PCI IO protocol instance.\r | |
894 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
895 | @param[in] BusWidth The bus width used by the SD/MMC device, it must be 1, 4 or 8.\r | |
896 | \r | |
897 | @retval EFI_SUCCESS The bus width is set successfully.\r | |
898 | @retval Others The bus width isn't set successfully.\r | |
899 | \r | |
900 | **/\r | |
901 | EFI_STATUS\r | |
902 | SdMmcHcSetBusWidth (\r | |
903 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
904 | IN UINT8 Slot,\r | |
905 | IN UINT16 BusWidth\r | |
906 | )\r | |
907 | {\r | |
908 | EFI_STATUS Status;\r | |
909 | UINT8 HostCtrl1;\r | |
910 | \r | |
911 | if (BusWidth == 1) {\r | |
912 | HostCtrl1 = (UINT8)~(BIT5 | BIT1);\r | |
913 | Status = SdMmcHcAndMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);\r | |
914 | } else if (BusWidth == 4) {\r | |
915 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, TRUE, sizeof (HostCtrl1), &HostCtrl1);\r | |
916 | if (EFI_ERROR (Status)) {\r | |
917 | return Status;\r | |
918 | }\r | |
919 | HostCtrl1 |= BIT1;\r | |
920 | HostCtrl1 &= (UINT8)~BIT5;\r | |
921 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, FALSE, sizeof (HostCtrl1), &HostCtrl1);\r | |
922 | } else if (BusWidth == 8) {\r | |
923 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, TRUE, sizeof (HostCtrl1), &HostCtrl1);\r | |
924 | if (EFI_ERROR (Status)) {\r | |
925 | return Status;\r | |
926 | }\r | |
927 | HostCtrl1 &= (UINT8)~BIT1;\r | |
928 | HostCtrl1 |= BIT5;\r | |
929 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, FALSE, sizeof (HostCtrl1), &HostCtrl1);\r | |
930 | } else {\r | |
931 | ASSERT (FALSE);\r | |
932 | return EFI_INVALID_PARAMETER;\r | |
933 | }\r | |
934 | \r | |
935 | return Status;\r | |
936 | }\r | |
937 | \r | |
938 | /**\r | |
939 | Supply SD/MMC card with lowest clock frequency at initialization.\r | |
940 | \r | |
941 | @param[in] PciIo The PCI IO protocol instance.\r | |
942 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
943 | @param[in] Capability The capability of the slot.\r | |
944 | \r | |
945 | @retval EFI_SUCCESS The clock is supplied successfully.\r | |
946 | @retval Others The clock isn't supplied successfully.\r | |
947 | \r | |
948 | **/\r | |
949 | EFI_STATUS\r | |
950 | SdMmcHcInitClockFreq (\r | |
951 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
952 | IN UINT8 Slot,\r | |
953 | IN SD_MMC_HC_SLOT_CAP Capability\r | |
954 | )\r | |
955 | {\r | |
956 | EFI_STATUS Status;\r | |
957 | UINT32 InitFreq;\r | |
958 | \r | |
959 | //\r | |
960 | // Calculate a divisor for SD clock frequency\r | |
961 | //\r | |
962 | if (Capability.BaseClkFreq == 0) {\r | |
963 | //\r | |
964 | // Don't support get Base Clock Frequency information via another method\r | |
965 | //\r | |
966 | return EFI_UNSUPPORTED;\r | |
967 | }\r | |
968 | //\r | |
969 | // Supply 400KHz clock frequency at initialization phase.\r | |
970 | //\r | |
971 | InitFreq = 400;\r | |
972 | Status = SdMmcHcClockSupply (PciIo, Slot, InitFreq, Capability);\r | |
973 | return Status;\r | |
974 | }\r | |
975 | \r | |
976 | /**\r | |
977 | Supply SD/MMC card with maximum voltage at initialization.\r | |
978 | \r | |
979 | Refer to SD Host Controller Simplified spec 3.0 Section 3.3 for details.\r | |
980 | \r | |
981 | @param[in] PciIo The PCI IO protocol instance.\r | |
982 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
983 | @param[in] Capability The capability of the slot.\r | |
984 | \r | |
985 | @retval EFI_SUCCESS The voltage is supplied successfully.\r | |
986 | @retval Others The voltage isn't supplied successfully.\r | |
987 | \r | |
988 | **/\r | |
989 | EFI_STATUS\r | |
990 | SdMmcHcInitPowerVoltage (\r | |
991 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
992 | IN UINT8 Slot,\r | |
993 | IN SD_MMC_HC_SLOT_CAP Capability\r | |
994 | )\r | |
995 | {\r | |
996 | EFI_STATUS Status;\r | |
997 | UINT8 MaxVoltage;\r | |
998 | UINT8 HostCtrl2;\r | |
999 | \r | |
1000 | //\r | |
1001 | // Calculate supported maximum voltage according to SD Bus Voltage Select\r | |
1002 | //\r | |
1003 | if (Capability.Voltage33 != 0) {\r | |
1004 | //\r | |
1005 | // Support 3.3V\r | |
1006 | //\r | |
1007 | MaxVoltage = 0x0E;\r | |
1008 | } else if (Capability.Voltage30 != 0) {\r | |
1009 | //\r | |
1010 | // Support 3.0V\r | |
1011 | //\r | |
1012 | MaxVoltage = 0x0C;\r | |
1013 | } else if (Capability.Voltage18 != 0) {\r | |
1014 | //\r | |
1015 | // Support 1.8V\r | |
1016 | //\r | |
1017 | MaxVoltage = 0x0A;\r | |
1018 | HostCtrl2 = BIT3;\r | |
1019 | Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL2, sizeof (HostCtrl2), &HostCtrl2);\r | |
1020 | gBS->Stall (5000);\r | |
1021 | if (EFI_ERROR (Status)) {\r | |
1022 | return Status;\r | |
1023 | }\r | |
1024 | } else {\r | |
1025 | ASSERT (FALSE);\r | |
1026 | return EFI_DEVICE_ERROR;\r | |
1027 | }\r | |
1028 | \r | |
1029 | //\r | |
1030 | // Set SD Bus Voltage Select and SD Bus Power fields in Power Control Register\r | |
1031 | //\r | |
1032 | Status = SdMmcHcPowerControl (PciIo, Slot, MaxVoltage);\r | |
1033 | \r | |
1034 | return Status;\r | |
1035 | }\r | |
1036 | \r | |
1037 | /**\r | |
1038 | Initialize the Timeout Control register with most conservative value at initialization.\r | |
1039 | \r | |
1040 | Refer to SD Host Controller Simplified spec 3.0 Section 2.2.15 for details.\r | |
1041 | \r | |
1042 | @param[in] PciIo The PCI IO protocol instance.\r | |
1043 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
1044 | \r | |
1045 | @retval EFI_SUCCESS The timeout control register is configured successfully.\r | |
1046 | @retval Others The timeout control register isn't configured successfully.\r | |
1047 | \r | |
1048 | **/\r | |
1049 | EFI_STATUS\r | |
1050 | SdMmcHcInitTimeoutCtrl (\r | |
1051 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
1052 | IN UINT8 Slot\r | |
1053 | )\r | |
1054 | {\r | |
1055 | EFI_STATUS Status;\r | |
1056 | UINT8 Timeout;\r | |
1057 | \r | |
1058 | Timeout = 0x0E;\r | |
1059 | Status = SdMmcHcRwMmio (PciIo, Slot, SD_MMC_HC_TIMEOUT_CTRL, FALSE, sizeof (Timeout), &Timeout);\r | |
1060 | \r | |
1061 | return Status;\r | |
1062 | }\r | |
1063 | \r | |
1064 | /**\r | |
1065 | Initial SD/MMC host controller with lowest clock frequency, max power and max timeout value\r | |
1066 | at initialization.\r | |
1067 | \r | |
b23fc39c | 1068 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r |
48555339 | 1069 | @param[in] Slot The slot number of the SD card to send the command to.\r |
48555339 FT |
1070 | \r |
1071 | @retval EFI_SUCCESS The host controller is initialized successfully.\r | |
1072 | @retval Others The host controller isn't initialized successfully.\r | |
1073 | \r | |
1074 | **/\r | |
1075 | EFI_STATUS\r | |
1076 | SdMmcHcInitHost (\r | |
b23fc39c AB |
1077 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r |
1078 | IN UINT8 Slot\r | |
48555339 FT |
1079 | )\r |
1080 | {\r | |
b23fc39c AB |
1081 | EFI_STATUS Status;\r |
1082 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1083 | SD_MMC_HC_SLOT_CAP Capability;\r | |
1084 | \r | |
1085 | //\r | |
1086 | // Notify the SD/MMC override protocol that we are about to initialize\r | |
1087 | // the SD/MMC host controller.\r | |
1088 | //\r | |
1089 | if (mOverride != NULL && mOverride->NotifyPhase != NULL) {\r | |
1090 | Status = mOverride->NotifyPhase (\r | |
1091 | Private->ControllerHandle,\r | |
1092 | Slot,\r | |
49c99534 MW |
1093 | EdkiiSdMmcInitHostPre,\r |
1094 | NULL);\r | |
b23fc39c AB |
1095 | if (EFI_ERROR (Status)) {\r |
1096 | DEBUG ((DEBUG_WARN,\r | |
1097 | "%a: SD/MMC pre init notifier callback failed - %r\n",\r | |
1098 | __FUNCTION__, Status));\r | |
1099 | return Status;\r | |
1100 | }\r | |
1101 | }\r | |
1102 | \r | |
1103 | PciIo = Private->PciIo;\r | |
1104 | Capability = Private->Capability[Slot];\r | |
48555339 FT |
1105 | \r |
1106 | Status = SdMmcHcInitClockFreq (PciIo, Slot, Capability);\r | |
1107 | if (EFI_ERROR (Status)) {\r | |
1108 | return Status;\r | |
1109 | }\r | |
1110 | \r | |
1111 | Status = SdMmcHcInitPowerVoltage (PciIo, Slot, Capability);\r | |
1112 | if (EFI_ERROR (Status)) {\r | |
1113 | return Status;\r | |
1114 | }\r | |
1115 | \r | |
1116 | Status = SdMmcHcInitTimeoutCtrl (PciIo, Slot);\r | |
b23fc39c AB |
1117 | if (EFI_ERROR (Status)) {\r |
1118 | return Status;\r | |
1119 | }\r | |
1120 | \r | |
1121 | //\r | |
1122 | // Notify the SD/MMC override protocol that we are have just initialized\r | |
1123 | // the SD/MMC host controller.\r | |
1124 | //\r | |
1125 | if (mOverride != NULL && mOverride->NotifyPhase != NULL) {\r | |
1126 | Status = mOverride->NotifyPhase (\r | |
1127 | Private->ControllerHandle,\r | |
1128 | Slot,\r | |
49c99534 MW |
1129 | EdkiiSdMmcInitHostPost,\r |
1130 | NULL);\r | |
b23fc39c AB |
1131 | if (EFI_ERROR (Status)) {\r |
1132 | DEBUG ((DEBUG_WARN,\r | |
1133 | "%a: SD/MMC post init notifier callback failed - %r\n",\r | |
1134 | __FUNCTION__, Status));\r | |
1135 | }\r | |
1136 | }\r | |
48555339 FT |
1137 | return Status;\r |
1138 | }\r | |
1139 | \r | |
1140 | /**\r | |
1141 | Turn on/off LED.\r | |
1142 | \r | |
1143 | @param[in] PciIo The PCI IO protocol instance.\r | |
1144 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
1145 | @param[in] On The boolean to turn on/off LED.\r | |
1146 | \r | |
1147 | @retval EFI_SUCCESS The LED is turned on/off successfully.\r | |
1148 | @retval Others The LED isn't turned on/off successfully.\r | |
1149 | \r | |
1150 | **/\r | |
1151 | EFI_STATUS\r | |
1152 | SdMmcHcLedOnOff (\r | |
1153 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
1154 | IN UINT8 Slot,\r | |
1155 | IN BOOLEAN On\r | |
1156 | )\r | |
1157 | {\r | |
1158 | EFI_STATUS Status;\r | |
1159 | UINT8 HostCtrl1;\r | |
1160 | \r | |
1161 | if (On) {\r | |
1162 | HostCtrl1 = BIT0;\r | |
1163 | Status = SdMmcHcOrMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);\r | |
1164 | } else {\r | |
1165 | HostCtrl1 = (UINT8)~BIT0;\r | |
1166 | Status = SdMmcHcAndMmio (PciIo, Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);\r | |
1167 | }\r | |
1168 | \r | |
1169 | return Status;\r | |
1170 | }\r | |
1171 | \r | |
1172 | /**\r | |
1173 | Build ADMA descriptor table for transfer.\r | |
1174 | \r | |
1175 | Refer to SD Host Controller Simplified spec 3.0 Section 1.13 for details.\r | |
1176 | \r | |
1177 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1178 | \r | |
1179 | @retval EFI_SUCCESS The ADMA descriptor table is created successfully.\r | |
1180 | @retval Others The ADMA descriptor table isn't created successfully.\r | |
1181 | \r | |
1182 | **/\r | |
1183 | EFI_STATUS\r | |
1184 | BuildAdmaDescTable (\r | |
1185 | IN SD_MMC_HC_TRB *Trb\r | |
1186 | )\r | |
1187 | {\r | |
1188 | EFI_PHYSICAL_ADDRESS Data;\r | |
1189 | UINT64 DataLen;\r | |
1190 | UINT64 Entries;\r | |
1191 | UINT32 Index;\r | |
1192 | UINT64 Remaining;\r | |
1193 | UINT32 Address;\r | |
1194 | UINTN TableSize;\r | |
1195 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1196 | EFI_STATUS Status;\r | |
1197 | UINTN Bytes;\r | |
1198 | \r | |
1199 | Data = Trb->DataPhy;\r | |
1200 | DataLen = Trb->DataLen;\r | |
1201 | PciIo = Trb->Private->PciIo;\r | |
1202 | //\r | |
1203 | // Only support 32bit ADMA Descriptor Table\r | |
1204 | //\r | |
1205 | if ((Data >= 0x100000000ul) || ((Data + DataLen) > 0x100000000ul)) {\r | |
1206 | return EFI_INVALID_PARAMETER;\r | |
1207 | }\r | |
1208 | //\r | |
1209 | // Address field shall be set on 32-bit boundary (Lower 2-bit is always set to 0)\r | |
1210 | // for 32-bit address descriptor table.\r | |
1211 | //\r | |
1212 | if ((Data & (BIT0 | BIT1)) != 0) {\r | |
e27ccaba | 1213 | DEBUG ((DEBUG_INFO, "The buffer [0x%x] to construct ADMA desc is not aligned to 4 bytes boundary!\n", Data));\r |
48555339 FT |
1214 | }\r |
1215 | \r | |
1216 | Entries = DivU64x32 ((DataLen + ADMA_MAX_DATA_PER_LINE - 1), ADMA_MAX_DATA_PER_LINE);\r | |
1217 | TableSize = (UINTN)MultU64x32 (Entries, sizeof (SD_MMC_HC_ADMA_DESC_LINE));\r | |
1218 | Trb->AdmaPages = (UINT32)EFI_SIZE_TO_PAGES (TableSize);\r | |
1219 | Status = PciIo->AllocateBuffer (\r | |
1220 | PciIo,\r | |
1221 | AllocateAnyPages,\r | |
1222 | EfiBootServicesData,\r | |
1223 | EFI_SIZE_TO_PAGES (TableSize),\r | |
1224 | (VOID **)&Trb->AdmaDesc,\r | |
1225 | 0\r | |
1226 | );\r | |
1227 | if (EFI_ERROR (Status)) {\r | |
1228 | return EFI_OUT_OF_RESOURCES;\r | |
1229 | }\r | |
1230 | ZeroMem (Trb->AdmaDesc, TableSize);\r | |
1231 | Bytes = TableSize;\r | |
1232 | Status = PciIo->Map (\r | |
1233 | PciIo,\r | |
1234 | EfiPciIoOperationBusMasterCommonBuffer,\r | |
1235 | Trb->AdmaDesc,\r | |
1236 | &Bytes,\r | |
1237 | &Trb->AdmaDescPhy,\r | |
1238 | &Trb->AdmaMap\r | |
1239 | );\r | |
1240 | \r | |
1241 | if (EFI_ERROR (Status) || (Bytes != TableSize)) {\r | |
1242 | //\r | |
1243 | // Map error or unable to map the whole RFis buffer into a contiguous region.\r | |
1244 | //\r | |
1245 | PciIo->FreeBuffer (\r | |
1246 | PciIo,\r | |
1247 | EFI_SIZE_TO_PAGES (TableSize),\r | |
1248 | Trb->AdmaDesc\r | |
1249 | );\r | |
1250 | return EFI_OUT_OF_RESOURCES;\r | |
1251 | }\r | |
1252 | \r | |
1253 | if ((UINT64)(UINTN)Trb->AdmaDescPhy > 0x100000000ul) {\r | |
1254 | //\r | |
1255 | // The ADMA doesn't support 64bit addressing.\r | |
1256 | //\r | |
1257 | PciIo->Unmap (\r | |
1258 | PciIo,\r | |
1259 | Trb->AdmaMap\r | |
1260 | );\r | |
1261 | PciIo->FreeBuffer (\r | |
1262 | PciIo,\r | |
1263 | EFI_SIZE_TO_PAGES (TableSize),\r | |
1264 | Trb->AdmaDesc\r | |
1265 | );\r | |
1266 | return EFI_DEVICE_ERROR;\r | |
1267 | }\r | |
1268 | \r | |
1269 | Remaining = DataLen;\r | |
1270 | Address = (UINT32)Data;\r | |
1271 | for (Index = 0; Index < Entries; Index++) {\r | |
1272 | if (Remaining <= ADMA_MAX_DATA_PER_LINE) {\r | |
1273 | Trb->AdmaDesc[Index].Valid = 1;\r | |
1274 | Trb->AdmaDesc[Index].Act = 2;\r | |
1275 | Trb->AdmaDesc[Index].Length = (UINT16)Remaining;\r | |
1276 | Trb->AdmaDesc[Index].Address = Address;\r | |
1277 | break;\r | |
1278 | } else {\r | |
1279 | Trb->AdmaDesc[Index].Valid = 1;\r | |
1280 | Trb->AdmaDesc[Index].Act = 2;\r | |
1281 | Trb->AdmaDesc[Index].Length = 0;\r | |
1282 | Trb->AdmaDesc[Index].Address = Address;\r | |
1283 | }\r | |
1284 | \r | |
1285 | Remaining -= ADMA_MAX_DATA_PER_LINE;\r | |
1286 | Address += ADMA_MAX_DATA_PER_LINE;\r | |
1287 | }\r | |
1288 | \r | |
1289 | //\r | |
1290 | // Set the last descriptor line as end of descriptor table\r | |
1291 | //\r | |
1292 | Trb->AdmaDesc[Index].End = 1;\r | |
1293 | return EFI_SUCCESS;\r | |
1294 | }\r | |
1295 | \r | |
1296 | /**\r | |
1297 | Create a new TRB for the SD/MMC cmd request.\r | |
1298 | \r | |
1299 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1300 | @param[in] Slot The slot number of the SD card to send the command to.\r | |
1301 | @param[in] Packet A pointer to the SD command data structure.\r | |
1302 | @param[in] Event If Event is NULL, blocking I/O is performed. If Event is\r | |
1303 | not NULL, then nonblocking I/O is performed, and Event\r | |
1304 | will be signaled when the Packet completes.\r | |
1305 | \r | |
1306 | @return Created Trb or NULL.\r | |
1307 | \r | |
1308 | **/\r | |
1309 | SD_MMC_HC_TRB *\r | |
1310 | SdMmcCreateTrb (\r | |
1311 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1312 | IN UINT8 Slot,\r | |
1313 | IN EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet,\r | |
1314 | IN EFI_EVENT Event\r | |
1315 | )\r | |
1316 | {\r | |
1317 | SD_MMC_HC_TRB *Trb;\r | |
1318 | EFI_STATUS Status;\r | |
1319 | EFI_TPL OldTpl;\r | |
1320 | EFI_PCI_IO_PROTOCOL_OPERATION Flag;\r | |
1321 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1322 | UINTN MapLength;\r | |
1323 | \r | |
1324 | Trb = AllocateZeroPool (sizeof (SD_MMC_HC_TRB));\r | |
1325 | if (Trb == NULL) {\r | |
1326 | return NULL;\r | |
1327 | }\r | |
1328 | \r | |
1329 | Trb->Signature = SD_MMC_HC_TRB_SIG;\r | |
1330 | Trb->Slot = Slot;\r | |
1331 | Trb->BlockSize = 0x200;\r | |
1332 | Trb->Packet = Packet;\r | |
1333 | Trb->Event = Event;\r | |
1334 | Trb->Started = FALSE;\r | |
1335 | Trb->Timeout = Packet->Timeout;\r | |
1336 | Trb->Private = Private;\r | |
1337 | \r | |
1338 | if ((Packet->InTransferLength != 0) && (Packet->InDataBuffer != NULL)) {\r | |
1339 | Trb->Data = Packet->InDataBuffer;\r | |
1340 | Trb->DataLen = Packet->InTransferLength;\r | |
1341 | Trb->Read = TRUE;\r | |
1342 | } else if ((Packet->OutTransferLength != 0) && (Packet->OutDataBuffer != NULL)) {\r | |
1343 | Trb->Data = Packet->OutDataBuffer;\r | |
1344 | Trb->DataLen = Packet->OutTransferLength;\r | |
1345 | Trb->Read = FALSE;\r | |
1346 | } else if ((Packet->InTransferLength == 0) && (Packet->OutTransferLength == 0)) {\r | |
1347 | Trb->Data = NULL;\r | |
1348 | Trb->DataLen = 0;\r | |
1349 | } else {\r | |
1350 | goto Error;\r | |
1351 | }\r | |
1352 | \r | |
54228046 | 1353 | if ((Trb->DataLen != 0) && (Trb->DataLen < Trb->BlockSize)) {\r |
e7e89b08 FT |
1354 | Trb->BlockSize = (UINT16)Trb->DataLen;\r |
1355 | }\r | |
1356 | \r | |
1357 | if (((Private->Slot[Trb->Slot].CardType == EmmcCardType) &&\r | |
1358 | (Packet->SdMmcCmdBlk->CommandIndex == EMMC_SEND_TUNING_BLOCK)) ||\r | |
1359 | ((Private->Slot[Trb->Slot].CardType == SdCardType) &&\r | |
1360 | (Packet->SdMmcCmdBlk->CommandIndex == SD_SEND_TUNING_BLOCK))) {\r | |
1361 | Trb->Mode = SdMmcPioMode;\r | |
48555339 | 1362 | } else {\r |
e7e89b08 FT |
1363 | if (Trb->Read) {\r |
1364 | Flag = EfiPciIoOperationBusMasterWrite;\r | |
1365 | } else {\r | |
1366 | Flag = EfiPciIoOperationBusMasterRead;\r | |
48555339 | 1367 | }\r |
48555339 | 1368 | \r |
e7e89b08 FT |
1369 | PciIo = Private->PciIo;\r |
1370 | if (Trb->DataLen != 0) {\r | |
1371 | MapLength = Trb->DataLen;\r | |
1372 | Status = PciIo->Map (\r | |
1373 | PciIo,\r | |
1374 | Flag,\r | |
1375 | Trb->Data,\r | |
1376 | &MapLength,\r | |
1377 | &Trb->DataPhy,\r | |
1378 | &Trb->DataMap\r | |
1379 | );\r | |
1380 | if (EFI_ERROR (Status) || (Trb->DataLen != MapLength)) {\r | |
1381 | Status = EFI_BAD_BUFFER_SIZE;\r | |
1382 | goto Error;\r | |
1383 | }\r | |
48555339 | 1384 | }\r |
48555339 | 1385 | \r |
e7e89b08 FT |
1386 | if (Trb->DataLen == 0) {\r |
1387 | Trb->Mode = SdMmcNoData;\r | |
1388 | } else if (Private->Capability[Slot].Adma2 != 0) {\r | |
1389 | Trb->Mode = SdMmcAdmaMode;\r | |
1390 | Status = BuildAdmaDescTable (Trb);\r | |
1391 | if (EFI_ERROR (Status)) {\r | |
1392 | PciIo->Unmap (PciIo, Trb->DataMap);\r | |
1393 | goto Error;\r | |
1394 | }\r | |
1395 | } else if (Private->Capability[Slot].Sdma != 0) {\r | |
1396 | Trb->Mode = SdMmcSdmaMode;\r | |
1397 | } else {\r | |
1398 | Trb->Mode = SdMmcPioMode;\r | |
48555339 | 1399 | }\r |
48555339 FT |
1400 | }\r |
1401 | \r | |
1402 | if (Event != NULL) {\r | |
3b1d8241 | 1403 | OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r |
48555339 FT |
1404 | InsertTailList (&Private->Queue, &Trb->TrbList);\r |
1405 | gBS->RestoreTPL (OldTpl);\r | |
1406 | }\r | |
1407 | \r | |
1408 | return Trb;\r | |
1409 | \r | |
1410 | Error:\r | |
1411 | SdMmcFreeTrb (Trb);\r | |
1412 | return NULL;\r | |
1413 | }\r | |
1414 | \r | |
1415 | /**\r | |
1416 | Free the resource used by the TRB.\r | |
1417 | \r | |
1418 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1419 | \r | |
1420 | **/\r | |
1421 | VOID\r | |
1422 | SdMmcFreeTrb (\r | |
1423 | IN SD_MMC_HC_TRB *Trb\r | |
1424 | )\r | |
1425 | {\r | |
1426 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1427 | \r | |
1428 | PciIo = Trb->Private->PciIo;\r | |
1429 | \r | |
1430 | if (Trb->AdmaMap != NULL) {\r | |
1431 | PciIo->Unmap (\r | |
1432 | PciIo,\r | |
1433 | Trb->AdmaMap\r | |
1434 | );\r | |
1435 | }\r | |
1436 | if (Trb->AdmaDesc != NULL) {\r | |
1437 | PciIo->FreeBuffer (\r | |
1438 | PciIo,\r | |
1439 | Trb->AdmaPages,\r | |
1440 | Trb->AdmaDesc\r | |
1441 | );\r | |
1442 | }\r | |
1443 | if (Trb->DataMap != NULL) {\r | |
1444 | PciIo->Unmap (\r | |
1445 | PciIo,\r | |
1446 | Trb->DataMap\r | |
1447 | );\r | |
1448 | }\r | |
1449 | FreePool (Trb);\r | |
1450 | return;\r | |
1451 | }\r | |
1452 | \r | |
1453 | /**\r | |
1454 | Check if the env is ready for execute specified TRB.\r | |
1455 | \r | |
1456 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1457 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1458 | \r | |
1459 | @retval EFI_SUCCESS The env is ready for TRB execution.\r | |
1460 | @retval EFI_NOT_READY The env is not ready for TRB execution.\r | |
1461 | @retval Others Some erros happen.\r | |
1462 | \r | |
1463 | **/\r | |
1464 | EFI_STATUS\r | |
1465 | SdMmcCheckTrbEnv (\r | |
1466 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1467 | IN SD_MMC_HC_TRB *Trb\r | |
1468 | )\r | |
1469 | {\r | |
1470 | EFI_STATUS Status;\r | |
1471 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1472 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1473 | UINT32 PresentState;\r | |
1474 | \r | |
1475 | Packet = Trb->Packet;\r | |
1476 | \r | |
1477 | if ((Packet->SdMmcCmdBlk->CommandType == SdMmcCommandTypeAdtc) ||\r | |
1478 | (Packet->SdMmcCmdBlk->ResponseType == SdMmcResponseTypeR1b) ||\r | |
1479 | (Packet->SdMmcCmdBlk->ResponseType == SdMmcResponseTypeR5b)) {\r | |
1480 | //\r | |
1481 | // Wait Command Inhibit (CMD) and Command Inhibit (DAT) in\r | |
1482 | // the Present State register to be 0\r | |
1483 | //\r | |
1484 | PresentState = BIT0 | BIT1;\r | |
48555339 FT |
1485 | } else {\r |
1486 | //\r | |
1487 | // Wait Command Inhibit (CMD) in the Present State register\r | |
1488 | // to be 0\r | |
1489 | //\r | |
1490 | PresentState = BIT0;\r | |
1491 | }\r | |
1492 | \r | |
1493 | PciIo = Private->PciIo;\r | |
1494 | Status = SdMmcHcCheckMmioSet (\r | |
1495 | PciIo,\r | |
1496 | Trb->Slot,\r | |
1497 | SD_MMC_HC_PRESENT_STATE,\r | |
1498 | sizeof (PresentState),\r | |
1499 | PresentState,\r | |
1500 | 0\r | |
1501 | );\r | |
1502 | \r | |
1503 | return Status;\r | |
1504 | }\r | |
1505 | \r | |
1506 | /**\r | |
1507 | Wait for the env to be ready for execute specified TRB.\r | |
1508 | \r | |
1509 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1510 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1511 | \r | |
1512 | @retval EFI_SUCCESS The env is ready for TRB execution.\r | |
1513 | @retval EFI_TIMEOUT The env is not ready for TRB execution in time.\r | |
1514 | @retval Others Some erros happen.\r | |
1515 | \r | |
1516 | **/\r | |
1517 | EFI_STATUS\r | |
1518 | SdMmcWaitTrbEnv (\r | |
1519 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1520 | IN SD_MMC_HC_TRB *Trb\r | |
1521 | )\r | |
1522 | {\r | |
1523 | EFI_STATUS Status;\r | |
1524 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1525 | UINT64 Timeout;\r | |
1526 | BOOLEAN InfiniteWait;\r | |
1527 | \r | |
1528 | //\r | |
1529 | // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register\r | |
1530 | //\r | |
1531 | Packet = Trb->Packet;\r | |
1532 | Timeout = Packet->Timeout;\r | |
1533 | if (Timeout == 0) {\r | |
1534 | InfiniteWait = TRUE;\r | |
1535 | } else {\r | |
1536 | InfiniteWait = FALSE;\r | |
1537 | }\r | |
1538 | \r | |
1539 | while (InfiniteWait || (Timeout > 0)) {\r | |
1540 | //\r | |
1541 | // Check Trb execution result by reading Normal Interrupt Status register.\r | |
1542 | //\r | |
1543 | Status = SdMmcCheckTrbEnv (Private, Trb);\r | |
1544 | if (Status != EFI_NOT_READY) {\r | |
1545 | return Status;\r | |
1546 | }\r | |
1547 | //\r | |
1548 | // Stall for 1 microsecond.\r | |
1549 | //\r | |
1550 | gBS->Stall (1);\r | |
1551 | \r | |
1552 | Timeout--;\r | |
1553 | }\r | |
1554 | \r | |
1555 | return EFI_TIMEOUT;\r | |
1556 | }\r | |
1557 | \r | |
1558 | /**\r | |
1559 | Execute the specified TRB.\r | |
1560 | \r | |
1561 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1562 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1563 | \r | |
1564 | @retval EFI_SUCCESS The TRB is sent to host controller successfully.\r | |
1565 | @retval Others Some erros happen when sending this request to the host controller.\r | |
1566 | \r | |
1567 | **/\r | |
1568 | EFI_STATUS\r | |
1569 | SdMmcExecTrb (\r | |
1570 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1571 | IN SD_MMC_HC_TRB *Trb\r | |
1572 | )\r | |
1573 | {\r | |
1574 | EFI_STATUS Status;\r | |
1575 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1576 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
1577 | UINT16 Cmd;\r | |
1578 | UINT16 IntStatus;\r | |
1579 | UINT32 Argument;\r | |
1580 | UINT16 BlkCount;\r | |
1581 | UINT16 BlkSize;\r | |
1582 | UINT16 TransMode;\r | |
1583 | UINT8 HostCtrl1;\r | |
1584 | UINT32 SdmaAddr;\r | |
1585 | UINT64 AdmaAddr;\r | |
1586 | \r | |
1587 | Packet = Trb->Packet;\r | |
1588 | PciIo = Trb->Private->PciIo;\r | |
1589 | //\r | |
1590 | // Clear all bits in Error Interrupt Status Register\r | |
1591 | //\r | |
1592 | IntStatus = 0xFFFF;\r | |
1593 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_ERR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);\r | |
1594 | if (EFI_ERROR (Status)) {\r | |
1595 | return Status;\r | |
1596 | }\r | |
1597 | //\r | |
1598 | // Clear all bits in Normal Interrupt Status Register excepts for Card Removal & Card Insertion bits.\r | |
1599 | //\r | |
1600 | IntStatus = 0xFF3F;\r | |
1601 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);\r | |
1602 | if (EFI_ERROR (Status)) {\r | |
1603 | return Status;\r | |
1604 | }\r | |
1605 | //\r | |
1606 | // Set Host Control 1 register DMA Select field\r | |
1607 | //\r | |
1608 | if (Trb->Mode == SdMmcAdmaMode) {\r | |
1609 | HostCtrl1 = BIT4;\r | |
1610 | Status = SdMmcHcOrMmio (PciIo, Trb->Slot, SD_MMC_HC_HOST_CTRL1, sizeof (HostCtrl1), &HostCtrl1);\r | |
1611 | if (EFI_ERROR (Status)) {\r | |
1612 | return Status;\r | |
1613 | }\r | |
1614 | }\r | |
1615 | \r | |
1616 | SdMmcHcLedOnOff (PciIo, Trb->Slot, TRUE);\r | |
1617 | \r | |
1618 | if (Trb->Mode == SdMmcSdmaMode) {\r | |
1619 | if ((UINT64)(UINTN)Trb->DataPhy >= 0x100000000ul) {\r | |
1620 | return EFI_INVALID_PARAMETER;\r | |
1621 | }\r | |
1622 | \r | |
1623 | SdmaAddr = (UINT32)(UINTN)Trb->DataPhy;\r | |
1624 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_SDMA_ADDR, FALSE, sizeof (SdmaAddr), &SdmaAddr);\r | |
1625 | if (EFI_ERROR (Status)) {\r | |
1626 | return Status;\r | |
1627 | }\r | |
1628 | } else if (Trb->Mode == SdMmcAdmaMode) {\r | |
1629 | AdmaAddr = (UINT64)(UINTN)Trb->AdmaDescPhy;\r | |
1630 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_ADMA_SYS_ADDR, FALSE, sizeof (AdmaAddr), &AdmaAddr);\r | |
1631 | if (EFI_ERROR (Status)) {\r | |
1632 | return Status;\r | |
1633 | }\r | |
1634 | }\r | |
1635 | \r | |
1636 | BlkSize = Trb->BlockSize;\r | |
1637 | if (Trb->Mode == SdMmcSdmaMode) {\r | |
1638 | //\r | |
1639 | // Set SDMA boundary to be 512K bytes.\r | |
1640 | //\r | |
1641 | BlkSize |= 0x7000;\r | |
1642 | }\r | |
1643 | \r | |
1644 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_BLK_SIZE, FALSE, sizeof (BlkSize), &BlkSize);\r | |
1645 | if (EFI_ERROR (Status)) {\r | |
1646 | return Status;\r | |
1647 | }\r | |
1648 | \r | |
e7e89b08 FT |
1649 | BlkCount = 0;\r |
1650 | if (Trb->Mode != SdMmcNoData) {\r | |
1651 | //\r | |
1652 | // Calcuate Block Count.\r | |
1653 | //\r | |
1654 | BlkCount = (UINT16)(Trb->DataLen / Trb->BlockSize);\r | |
1655 | }\r | |
48555339 FT |
1656 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_BLK_COUNT, FALSE, sizeof (BlkCount), &BlkCount);\r |
1657 | if (EFI_ERROR (Status)) {\r | |
1658 | return Status;\r | |
1659 | }\r | |
1660 | \r | |
1661 | Argument = Packet->SdMmcCmdBlk->CommandArgument;\r | |
1662 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_ARG1, FALSE, sizeof (Argument), &Argument);\r | |
1663 | if (EFI_ERROR (Status)) {\r | |
1664 | return Status;\r | |
1665 | }\r | |
1666 | \r | |
1667 | TransMode = 0;\r | |
1668 | if (Trb->Mode != SdMmcNoData) {\r | |
1669 | if (Trb->Mode != SdMmcPioMode) {\r | |
1670 | TransMode |= BIT0;\r | |
1671 | }\r | |
1672 | if (Trb->Read) {\r | |
1673 | TransMode |= BIT4;\r | |
1674 | }\r | |
e7e89b08 | 1675 | if (BlkCount > 1) {\r |
48555339 FT |
1676 | TransMode |= BIT5 | BIT1;\r |
1677 | }\r | |
1678 | //\r | |
1679 | // Only SD memory card needs to use AUTO CMD12 feature.\r | |
1680 | //\r | |
1681 | if (Private->Slot[Trb->Slot].CardType == SdCardType) {\r | |
1682 | if (BlkCount > 1) {\r | |
1683 | TransMode |= BIT2;\r | |
1684 | }\r | |
1685 | }\r | |
1686 | }\r | |
1687 | \r | |
1688 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_TRANS_MOD, FALSE, sizeof (TransMode), &TransMode);\r | |
1689 | if (EFI_ERROR (Status)) {\r | |
1690 | return Status;\r | |
1691 | }\r | |
1692 | \r | |
1693 | Cmd = (UINT16)LShiftU64(Packet->SdMmcCmdBlk->CommandIndex, 8);\r | |
1694 | if (Packet->SdMmcCmdBlk->CommandType == SdMmcCommandTypeAdtc) {\r | |
1695 | Cmd |= BIT5;\r | |
1696 | }\r | |
1697 | //\r | |
1698 | // Convert ResponseType to value\r | |
1699 | //\r | |
1700 | if (Packet->SdMmcCmdBlk->CommandType != SdMmcCommandTypeBc) {\r | |
1701 | switch (Packet->SdMmcCmdBlk->ResponseType) {\r | |
1702 | case SdMmcResponseTypeR1:\r | |
1703 | case SdMmcResponseTypeR5:\r | |
1704 | case SdMmcResponseTypeR6:\r | |
1705 | case SdMmcResponseTypeR7:\r | |
1706 | Cmd |= (BIT1 | BIT3 | BIT4);\r | |
1707 | break;\r | |
1708 | case SdMmcResponseTypeR2:\r | |
1709 | Cmd |= (BIT0 | BIT3);\r | |
1710 | break;\r | |
1711 | case SdMmcResponseTypeR3:\r | |
1712 | case SdMmcResponseTypeR4:\r | |
1713 | Cmd |= BIT1;\r | |
1714 | break;\r | |
1715 | case SdMmcResponseTypeR1b:\r | |
1716 | case SdMmcResponseTypeR5b:\r | |
1717 | Cmd |= (BIT0 | BIT1 | BIT3 | BIT4);\r | |
1718 | break;\r | |
1719 | default:\r | |
1720 | ASSERT (FALSE);\r | |
1721 | break;\r | |
1722 | }\r | |
1723 | }\r | |
1724 | //\r | |
1725 | // Execute cmd\r | |
1726 | //\r | |
1727 | Status = SdMmcHcRwMmio (PciIo, Trb->Slot, SD_MMC_HC_COMMAND, FALSE, sizeof (Cmd), &Cmd);\r | |
1728 | return Status;\r | |
1729 | }\r | |
1730 | \r | |
1731 | /**\r | |
1732 | Check the TRB execution result.\r | |
1733 | \r | |
1734 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1735 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1736 | \r | |
1737 | @retval EFI_SUCCESS The TRB is executed successfully.\r | |
1738 | @retval EFI_NOT_READY The TRB is not completed for execution.\r | |
1739 | @retval Others Some erros happen when executing this request.\r | |
1740 | \r | |
1741 | **/\r | |
1742 | EFI_STATUS\r | |
1743 | SdMmcCheckTrbResult (\r | |
1744 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1745 | IN SD_MMC_HC_TRB *Trb\r | |
1746 | )\r | |
1747 | {\r | |
1748 | EFI_STATUS Status;\r | |
1749 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1750 | UINT16 IntStatus;\r | |
1751 | UINT32 Response[4];\r | |
1752 | UINT32 SdmaAddr;\r | |
1753 | UINT8 Index;\r | |
1754 | UINT8 SwReset;\r | |
e7e89b08 | 1755 | UINT32 PioLength;\r |
48555339 FT |
1756 | \r |
1757 | SwReset = 0;\r | |
1758 | Packet = Trb->Packet;\r | |
1759 | //\r | |
1760 | // Check Trb execution result by reading Normal Interrupt Status register.\r | |
1761 | //\r | |
1762 | Status = SdMmcHcRwMmio (\r | |
1763 | Private->PciIo,\r | |
1764 | Trb->Slot,\r | |
1765 | SD_MMC_HC_NOR_INT_STS,\r | |
1766 | TRUE,\r | |
1767 | sizeof (IntStatus),\r | |
1768 | &IntStatus\r | |
1769 | );\r | |
1770 | if (EFI_ERROR (Status)) {\r | |
1771 | goto Done;\r | |
1772 | }\r | |
1773 | //\r | |
1774 | // Check Transfer Complete bit is set or not.\r | |
1775 | //\r | |
1776 | if ((IntStatus & BIT1) == BIT1) {\r | |
1777 | if ((IntStatus & BIT15) == BIT15) {\r | |
1778 | //\r | |
1779 | // Read Error Interrupt Status register to check if the error is\r | |
1780 | // Data Timeout Error.\r | |
1781 | // If yes, treat it as success as Transfer Complete has higher\r | |
1782 | // priority than Data Timeout Error.\r | |
1783 | //\r | |
1784 | Status = SdMmcHcRwMmio (\r | |
1785 | Private->PciIo,\r | |
1786 | Trb->Slot,\r | |
1787 | SD_MMC_HC_ERR_INT_STS,\r | |
1788 | TRUE,\r | |
1789 | sizeof (IntStatus),\r | |
1790 | &IntStatus\r | |
1791 | );\r | |
1792 | if (!EFI_ERROR (Status)) {\r | |
1793 | if ((IntStatus & BIT4) == BIT4) {\r | |
1794 | Status = EFI_SUCCESS;\r | |
1795 | } else {\r | |
1796 | Status = EFI_DEVICE_ERROR;\r | |
1797 | }\r | |
1798 | }\r | |
1799 | }\r | |
1800 | \r | |
1801 | goto Done;\r | |
1802 | }\r | |
1803 | //\r | |
1804 | // Check if there is a error happened during cmd execution.\r | |
1805 | // If yes, then do error recovery procedure to follow SD Host Controller\r | |
1806 | // Simplified Spec 3.0 section 3.10.1.\r | |
1807 | //\r | |
1808 | if ((IntStatus & BIT15) == BIT15) {\r | |
1809 | Status = SdMmcHcRwMmio (\r | |
1810 | Private->PciIo,\r | |
1811 | Trb->Slot,\r | |
1812 | SD_MMC_HC_ERR_INT_STS,\r | |
1813 | TRUE,\r | |
1814 | sizeof (IntStatus),\r | |
1815 | &IntStatus\r | |
1816 | );\r | |
1817 | if (EFI_ERROR (Status)) {\r | |
1818 | goto Done;\r | |
1819 | }\r | |
1820 | if ((IntStatus & 0x0F) != 0) {\r | |
1821 | SwReset |= BIT1;\r | |
1822 | }\r | |
1823 | if ((IntStatus & 0xF0) != 0) {\r | |
1824 | SwReset |= BIT2;\r | |
1825 | }\r | |
1826 | \r | |
1827 | Status = SdMmcHcRwMmio (\r | |
1828 | Private->PciIo,\r | |
1829 | Trb->Slot,\r | |
1830 | SD_MMC_HC_SW_RST,\r | |
1831 | FALSE,\r | |
1832 | sizeof (SwReset),\r | |
1833 | &SwReset\r | |
1834 | );\r | |
1835 | if (EFI_ERROR (Status)) {\r | |
1836 | goto Done;\r | |
1837 | }\r | |
1838 | Status = SdMmcHcWaitMmioSet (\r | |
1839 | Private->PciIo,\r | |
1840 | Trb->Slot,\r | |
1841 | SD_MMC_HC_SW_RST,\r | |
1842 | sizeof (SwReset),\r | |
1843 | 0xFF,\r | |
1844 | 0,\r | |
1845 | SD_MMC_HC_GENERIC_TIMEOUT\r | |
1846 | );\r | |
1847 | if (EFI_ERROR (Status)) {\r | |
1848 | goto Done;\r | |
1849 | }\r | |
1850 | \r | |
1851 | Status = EFI_DEVICE_ERROR;\r | |
1852 | goto Done;\r | |
1853 | }\r | |
1854 | //\r | |
1855 | // Check if DMA interrupt is signalled for the SDMA transfer.\r | |
1856 | //\r | |
1857 | if ((Trb->Mode == SdMmcSdmaMode) && ((IntStatus & BIT3) == BIT3)) {\r | |
1858 | //\r | |
1859 | // Clear DMA interrupt bit.\r | |
1860 | //\r | |
1861 | IntStatus = BIT3;\r | |
1862 | Status = SdMmcHcRwMmio (\r | |
1863 | Private->PciIo,\r | |
1864 | Trb->Slot,\r | |
1865 | SD_MMC_HC_NOR_INT_STS,\r | |
1866 | FALSE,\r | |
1867 | sizeof (IntStatus),\r | |
1868 | &IntStatus\r | |
1869 | );\r | |
1870 | if (EFI_ERROR (Status)) {\r | |
1871 | goto Done;\r | |
1872 | }\r | |
1873 | //\r | |
1874 | // Update SDMA Address register.\r | |
1875 | //\r | |
1876 | SdmaAddr = SD_MMC_SDMA_ROUND_UP ((UINT32)(UINTN)Trb->DataPhy, SD_MMC_SDMA_BOUNDARY);\r | |
1877 | Status = SdMmcHcRwMmio (\r | |
1878 | Private->PciIo,\r | |
1879 | Trb->Slot,\r | |
1880 | SD_MMC_HC_SDMA_ADDR,\r | |
1881 | FALSE,\r | |
1882 | sizeof (UINT32),\r | |
1883 | &SdmaAddr\r | |
1884 | );\r | |
1885 | if (EFI_ERROR (Status)) {\r | |
1886 | goto Done;\r | |
1887 | }\r | |
1888 | Trb->DataPhy = (UINT32)(UINTN)SdmaAddr;\r | |
1889 | }\r | |
1890 | \r | |
1891 | if ((Packet->SdMmcCmdBlk->CommandType != SdMmcCommandTypeAdtc) &&\r | |
1892 | (Packet->SdMmcCmdBlk->ResponseType != SdMmcResponseTypeR1b) &&\r | |
1893 | (Packet->SdMmcCmdBlk->ResponseType != SdMmcResponseTypeR5b)) {\r | |
1894 | if ((IntStatus & BIT0) == BIT0) {\r | |
1895 | Status = EFI_SUCCESS;\r | |
1896 | goto Done;\r | |
1897 | }\r | |
1898 | }\r | |
1899 | \r | |
1900 | if (((Private->Slot[Trb->Slot].CardType == EmmcCardType) &&\r | |
1901 | (Packet->SdMmcCmdBlk->CommandIndex == EMMC_SEND_TUNING_BLOCK)) ||\r | |
1902 | ((Private->Slot[Trb->Slot].CardType == SdCardType) &&\r | |
1903 | (Packet->SdMmcCmdBlk->CommandIndex == SD_SEND_TUNING_BLOCK))) {\r | |
1904 | //\r | |
e7e89b08 FT |
1905 | // When performing tuning procedure (Execute Tuning is set to 1) through PIO mode,\r |
1906 | // wait Buffer Read Ready bit of Normal Interrupt Status Register to be 1.\r | |
1907 | // Refer to SD Host Controller Simplified Specification 3.0 figure 2-29 for details.\r | |
48555339 | 1908 | //\r |
e7e89b08 FT |
1909 | if ((IntStatus & BIT5) == BIT5) {\r |
1910 | //\r | |
1911 | // Clear Buffer Read Ready interrupt at first.\r | |
1912 | //\r | |
1913 | IntStatus = BIT5;\r | |
1914 | SdMmcHcRwMmio (Private->PciIo, Trb->Slot, SD_MMC_HC_NOR_INT_STS, FALSE, sizeof (IntStatus), &IntStatus);\r | |
1915 | //\r | |
1916 | // Read data out from Buffer Port register\r | |
1917 | //\r | |
1918 | for (PioLength = 0; PioLength < Trb->DataLen; PioLength += 4) {\r | |
1919 | SdMmcHcRwMmio (Private->PciIo, Trb->Slot, SD_MMC_HC_BUF_DAT_PORT, TRUE, 4, (UINT8*)Trb->Data + PioLength);\r | |
1920 | }\r | |
1921 | Status = EFI_SUCCESS;\r | |
1922 | goto Done;\r | |
1923 | }\r | |
48555339 FT |
1924 | }\r |
1925 | \r | |
1926 | Status = EFI_NOT_READY;\r | |
1927 | Done:\r | |
1928 | //\r | |
1929 | // Get response data when the cmd is executed successfully.\r | |
1930 | //\r | |
1931 | if (!EFI_ERROR (Status)) {\r | |
1932 | if (Packet->SdMmcCmdBlk->CommandType != SdMmcCommandTypeBc) {\r | |
1933 | for (Index = 0; Index < 4; Index++) {\r | |
1934 | Status = SdMmcHcRwMmio (\r | |
1935 | Private->PciIo,\r | |
1936 | Trb->Slot,\r | |
1937 | SD_MMC_HC_RESPONSE + Index * 4,\r | |
1938 | TRUE,\r | |
1939 | sizeof (UINT32),\r | |
1940 | &Response[Index]\r | |
1941 | );\r | |
1942 | if (EFI_ERROR (Status)) {\r | |
1943 | SdMmcHcLedOnOff (Private->PciIo, Trb->Slot, FALSE);\r | |
1944 | return Status;\r | |
1945 | }\r | |
1946 | }\r | |
1947 | CopyMem (Packet->SdMmcStatusBlk, Response, sizeof (Response));\r | |
1948 | }\r | |
1949 | }\r | |
1950 | \r | |
1951 | if (Status != EFI_NOT_READY) {\r | |
1952 | SdMmcHcLedOnOff (Private->PciIo, Trb->Slot, FALSE);\r | |
1953 | }\r | |
1954 | \r | |
1955 | return Status;\r | |
1956 | }\r | |
1957 | \r | |
1958 | /**\r | |
1959 | Wait for the TRB execution result.\r | |
1960 | \r | |
1961 | @param[in] Private A pointer to the SD_MMC_HC_PRIVATE_DATA instance.\r | |
1962 | @param[in] Trb The pointer to the SD_MMC_HC_TRB instance.\r | |
1963 | \r | |
1964 | @retval EFI_SUCCESS The TRB is executed successfully.\r | |
1965 | @retval Others Some erros happen when executing this request.\r | |
1966 | \r | |
1967 | **/\r | |
1968 | EFI_STATUS\r | |
1969 | SdMmcWaitTrbResult (\r | |
1970 | IN SD_MMC_HC_PRIVATE_DATA *Private,\r | |
1971 | IN SD_MMC_HC_TRB *Trb\r | |
1972 | )\r | |
1973 | {\r | |
1974 | EFI_STATUS Status;\r | |
1975 | EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r | |
1976 | UINT64 Timeout;\r | |
1977 | BOOLEAN InfiniteWait;\r | |
1978 | \r | |
1979 | Packet = Trb->Packet;\r | |
1980 | //\r | |
1981 | // Wait Command Complete Interrupt Status bit in Normal Interrupt Status Register\r | |
1982 | //\r | |
1983 | Timeout = Packet->Timeout;\r | |
1984 | if (Timeout == 0) {\r | |
1985 | InfiniteWait = TRUE;\r | |
1986 | } else {\r | |
1987 | InfiniteWait = FALSE;\r | |
1988 | }\r | |
1989 | \r | |
1990 | while (InfiniteWait || (Timeout > 0)) {\r | |
1991 | //\r | |
1992 | // Check Trb execution result by reading Normal Interrupt Status register.\r | |
1993 | //\r | |
1994 | Status = SdMmcCheckTrbResult (Private, Trb);\r | |
1995 | if (Status != EFI_NOT_READY) {\r | |
1996 | return Status;\r | |
1997 | }\r | |
1998 | //\r | |
1999 | // Stall for 1 microsecond.\r | |
2000 | //\r | |
2001 | gBS->Stall (1);\r | |
2002 | \r | |
2003 | Timeout--;\r | |
2004 | }\r | |
2005 | \r | |
2006 | return EFI_TIMEOUT;\r | |
2007 | }\r | |
2008 | \r |