BaseTools/BinToPcd: Fix Python 2.7.x compatibility issue
[mirror_edk2.git] / EmbeddedPkg / Universal / MmcDxe / MmcBlockIo.c
CommitLineData
1bfda055 1/** @file\r
2*\r
eff98cf9 3* Copyright (c) 2011-2015, ARM Limited. All rights reserved.\r
1bfda055 4*\r
842b02d8
OM
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
1bfda055 12*\r
13**/\r
14\r
1bfda055 15#include <Library/BaseMemoryLib.h>\r
1bfda055 16\r
17#include "Mmc.h"\r
18\r
1bfda055 19EFI_STATUS\r
20MmcNotifyState (\r
e8e95df4 21 IN MMC_HOST_INSTANCE *MmcHostInstance,\r
22 IN MMC_STATE State\r
23 )\r
24{\r
25 MmcHostInstance->State = State;\r
16d88c2d 26 return MmcHostInstance->MmcHost->NotifyState (MmcHostInstance->MmcHost, State);\r
1bfda055 27}\r
28\r
a1ab9143 29EFI_STATUS\r
30EFIAPI\r
842b02d8 31MmcGetCardStatus (\r
4ca3c688 32 IN MMC_HOST_INSTANCE *MmcHostInstance\r
e8e95df4 33 )\r
34{\r
35 EFI_STATUS Status;\r
36 UINT32 Response[4];\r
37 UINTN CmdArg;\r
38 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
39\r
40 Status = EFI_SUCCESS;\r
41 MmcHost = MmcHostInstance->MmcHost;\r
42 CmdArg = 0;\r
43\r
44 if (MmcHost == NULL) {\r
45 return EFI_INVALID_PARAMETER;\r
46 }\r
842b02d8 47 if (MmcHostInstance->State != MmcHwInitializationState) {\r
e8e95df4 48 //Get the Status of the card.\r
49 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
16d88c2d 50 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);\r
6f711615 51 if (EFI_ERROR (Status)) {\r
52 DEBUG ((EFI_D_ERROR, "MmcGetCardStatus(MMC_CMD13): Error and Status = %r\n", Status));\r
e8e95df4 53 return Status;\r
a1ab9143 54 }\r
55\r
e8e95df4 56 //Read Response\r
6f711615 57 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);\r
58 PrintResponseR1 (Response[0]);\r
e8e95df4 59 }\r
60\r
61 return Status;\r
1bfda055 62}\r
63\r
1bfda055 64EFI_STATUS\r
65EFIAPI\r
66MmcReset (\r
67 IN EFI_BLOCK_IO_PROTOCOL *This,\r
68 IN BOOLEAN ExtendedVerification\r
e8e95df4 69 )\r
70{\r
3de99375 71 MMC_HOST_INSTANCE *MmcHostInstance;\r
72\r
6f711615 73 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);\r
3de99375 74\r
75 if (MmcHostInstance->MmcHost == NULL) {\r
76 // Nothing to do\r
77 return EFI_SUCCESS;\r
78 }\r
79\r
80 // If a card is not present then clear all media settings\r
16d88c2d 81 if (!MmcHostInstance->MmcHost->IsCardPresent (MmcHostInstance->MmcHost)) {\r
3de99375 82 MmcHostInstance->BlockIo.Media->MediaPresent = FALSE;\r
83 MmcHostInstance->BlockIo.Media->LastBlock = 0;\r
84 MmcHostInstance->BlockIo.Media->BlockSize = 512; // Should be zero but there is a bug in DiskIo\r
85 MmcHostInstance->BlockIo.Media->ReadOnly = FALSE;\r
86\r
87 // Indicate that the driver requires initialization\r
88 MmcHostInstance->State = MmcHwInitializationState;\r
89\r
90 return EFI_SUCCESS;\r
91 }\r
92\r
e8e95df4 93 // Implement me. Either send a CMD0 (could not work for some MMC host) or just turn off/turn\r
94 // on power and restart Identification mode\r
95 return EFI_SUCCESS;\r
1bfda055 96}\r
97\r
98EFI_STATUS\r
99MmcDetectCard (\r
100 EFI_MMC_HOST_PROTOCOL *MmcHost\r
101 )\r
102{\r
16d88c2d 103 if (!MmcHost->IsCardPresent (MmcHost)) {\r
e8e95df4 104 return EFI_NO_MEDIA;\r
105 } else {\r
106 return EFI_SUCCESS;\r
107 }\r
1bfda055 108}\r
109\r
9532373b
OM
110EFI_STATUS\r
111MmcStopTransmission (\r
112 EFI_MMC_HOST_PROTOCOL *MmcHost\r
113 )\r
114{\r
115 EFI_STATUS Status;\r
116 UINT32 Response[4];\r
117 // Command 12 - Stop transmission (ends read or write)\r
118 // Normally only needed for streaming transfers or after error.\r
119 Status = MmcHost->SendCommand (MmcHost, MMC_CMD12, 0);\r
120 if (!EFI_ERROR (Status)) {\r
121 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1b, Response);\r
122 }\r
123 return Status;\r
124}\r
125\r
1bfda055 126#define MMCI0_BLOCKLEN 512\r
127#define MMCI0_TIMEOUT 10000\r
128\r
339c6e90
HZ
129STATIC\r
130EFI_STATUS\r
131MmcTransferBlock (\r
132 IN EFI_BLOCK_IO_PROTOCOL *This,\r
133 IN UINTN Cmd,\r
134 IN UINTN Transfer,\r
135 IN UINT32 MediaId,\r
136 IN EFI_LBA Lba,\r
137 IN UINTN BufferSize,\r
138 OUT VOID *Buffer\r
139 )\r
140{\r
141 EFI_STATUS Status;\r
142 UINTN CmdArg;\r
143 INTN Timeout;\r
144 UINT32 Response[4];\r
145 MMC_HOST_INSTANCE *MmcHostInstance;\r
146 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
147\r
148 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);\r
149 MmcHost = MmcHostInstance->MmcHost;\r
150\r
151 //Set command argument based on the card access mode (Byte mode or Block mode)\r
152 if ((MmcHostInstance->CardInfo.OCRData.AccessMode & MMC_OCR_ACCESS_MASK) ==\r
153 MMC_OCR_ACCESS_SECTOR) {\r
154 CmdArg = Lba;\r
155 } else {\r
156 CmdArg = Lba * This->Media->BlockSize;\r
157 }\r
158\r
159 Status = MmcHost->SendCommand (MmcHost, Cmd, CmdArg);\r
160 if (EFI_ERROR (Status)) {\r
161 DEBUG ((EFI_D_ERROR, "%a(MMC_CMD%d): Error %r\n", __func__, Cmd, Status));\r
162 return Status;\r
163 }\r
164\r
165 if (Transfer == MMC_IOBLOCKS_READ) {\r
166 // Read Data\r
167 Status = MmcHost->ReadBlockData (MmcHost, Lba, BufferSize, Buffer);\r
168 if (EFI_ERROR (Status)) {\r
169 DEBUG ((EFI_D_BLKIO, "%a(): Error Read Block Data and Status = %r\n", __func__, Status));\r
170 MmcStopTransmission (MmcHost);\r
171 return Status;\r
172 }\r
173 Status = MmcNotifyState (MmcHostInstance, MmcProgrammingState);\r
174 if (EFI_ERROR (Status)) {\r
175 DEBUG ((EFI_D_ERROR, "%a() : Error MmcProgrammingState\n", __func__));\r
176 return Status;\r
177 }\r
178 } else {\r
179 // Write Data\r
180 Status = MmcHost->WriteBlockData (MmcHost, Lba, BufferSize, Buffer);\r
181 if (EFI_ERROR (Status)) {\r
182 DEBUG ((EFI_D_BLKIO, "%a(): Error Write Block Data and Status = %r\n", __func__, Status));\r
183 MmcStopTransmission (MmcHost);\r
184 return Status;\r
185 }\r
186 }\r
187\r
188 // Command 13 - Read status and wait for programming to complete (return to tran)\r
189 Timeout = MMCI0_TIMEOUT;\r
190 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
191 Response[0] = 0;\r
192 while(!(Response[0] & MMC_R0_READY_FOR_DATA)\r
193 && (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)\r
194 && Timeout--) {\r
195 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);\r
196 if (!EFI_ERROR (Status)) {\r
197 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);\r
198 if (Response[0] & MMC_R0_READY_FOR_DATA) {\r
199 break; // Prevents delay once finished\r
200 }\r
201 }\r
202 }\r
203\r
204 if (BufferSize > This->Media->BlockSize) {\r
205 Status = MmcHost->SendCommand (MmcHost, MMC_CMD12, 0);\r
206 if (EFI_ERROR (Status)) {\r
207 DEBUG ((EFI_D_BLKIO, "%a(): Error and Status:%r\n", __func__, Status));\r
208 }\r
1cc0f69b 209 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1b, Response);\r
339c6e90
HZ
210 }\r
211\r
212 Status = MmcNotifyState (MmcHostInstance, MmcTransferState);\r
213 if (EFI_ERROR (Status)) {\r
214 DEBUG ((EFI_D_ERROR, "MmcIoBlocks() : Error MmcTransferState\n"));\r
215 return Status;\r
216 }\r
217 return Status;\r
218}\r
219\r
e8e95df4 220EFI_STATUS\r
221MmcIoBlocks (\r
1bfda055 222 IN EFI_BLOCK_IO_PROTOCOL *This,\r
223 IN UINTN Transfer,\r
224 IN UINT32 MediaId,\r
225 IN EFI_LBA Lba,\r
226 IN UINTN BufferSize,\r
227 OUT VOID *Buffer\r
e8e95df4 228 )\r
229{\r
230 UINT32 Response[4];\r
231 EFI_STATUS Status;\r
40842a5e 232 UINTN CmdArg;\r
969ece79 233 INTN Timeout;\r
e8e95df4 234 UINTN Cmd;\r
235 MMC_HOST_INSTANCE *MmcHostInstance;\r
236 EFI_MMC_HOST_PROTOCOL *MmcHost;\r
237 UINTN BytesRemainingToBeTransfered;\r
842b02d8 238 UINTN BlockCount;\r
339c6e90 239 UINTN ConsumeSize;\r
e8e95df4 240\r
842b02d8 241 BlockCount = 1;\r
6f711615 242 MmcHostInstance = MMC_HOST_INSTANCE_FROM_BLOCK_IO_THIS (This);\r
842b02d8 243 ASSERT (MmcHostInstance != NULL);\r
e8e95df4 244 MmcHost = MmcHostInstance->MmcHost;\r
6f711615 245 ASSERT (MmcHost);\r
e8e95df4 246\r
5ab765a7 247 if (This->Media->MediaId != MediaId) {\r
248 return EFI_MEDIA_CHANGED;\r
249 }\r
250\r
842b02d8 251 if ((MmcHost == NULL) || (Buffer == NULL)) {\r
e8e95df4 252 return EFI_INVALID_PARAMETER;\r
253 }\r
254\r
255 // Check if a Card is Present\r
3de99375 256 if (!MmcHostInstance->BlockIo.Media->MediaPresent) {\r
e8e95df4 257 return EFI_NO_MEDIA;\r
258 }\r
259\r
339c6e90
HZ
260 if (MMC_HOST_HAS_ISMULTIBLOCK(MmcHost) && MmcHost->IsMultiBlock(MmcHost)) {\r
261 BlockCount = (BufferSize + This->Media->BlockSize - 1) / This->Media->BlockSize;\r
262 }\r
263\r
969ece79 264 // All blocks must be within the device\r
842b02d8 265 if ((Lba + (BufferSize / This->Media->BlockSize)) > (This->Media->LastBlock + 1)) {\r
969ece79 266 return EFI_INVALID_PARAMETER;\r
e8e95df4 267 }\r
268\r
842b02d8 269 if ((Transfer == MMC_IOBLOCKS_WRITE) && (This->Media->ReadOnly == TRUE)) {\r
5ab765a7 270 return EFI_WRITE_PROTECTED;\r
969ece79 271 }\r
272\r
5ab765a7 273 // Reading 0 Byte is valid\r
274 if (BufferSize == 0) {\r
275 return EFI_SUCCESS;\r
969ece79 276 }\r
277\r
5ab765a7 278 // The buffer size must be an exact multiple of the block size\r
279 if ((BufferSize % This->Media->BlockSize) != 0) {\r
280 return EFI_BAD_BUFFER_SIZE;\r
281 }\r
282\r
283 // Check the alignment\r
284 if ((This->Media->IoAlign > 2) && (((UINTN)Buffer & (This->Media->IoAlign - 1)) != 0)) {\r
285 return EFI_INVALID_PARAMETER;\r
e8e95df4 286 }\r
287\r
288 BytesRemainingToBeTransfered = BufferSize;\r
289 while (BytesRemainingToBeTransfered > 0) {\r
290\r
291 // Check if the Card is in Ready status\r
292 CmdArg = MmcHostInstance->CardInfo.RCA << 16;\r
293 Response[0] = 0;\r
294 Timeout = 20;\r
260675b0 295 while( (!(Response[0] & MMC_R0_READY_FOR_DATA))\r
6f711615 296 && (MMC_R0_CURRENTSTATE (Response) != MMC_R0_STATE_TRAN)\r
260675b0 297 && Timeout--) {\r
16d88c2d 298 Status = MmcHost->SendCommand (MmcHost, MMC_CMD13, CmdArg);\r
6f711615 299 if (!EFI_ERROR (Status)) {\r
300 MmcHost->ReceiveResponse (MmcHost, MMC_RESPONSE_TYPE_R1, Response);\r
e8e95df4 301 }\r
1bfda055 302 }\r
303\r
e8e95df4 304 if (0 == Timeout) {\r
6f711615 305 DEBUG ((EFI_D_ERROR, "The Card is busy\n"));\r
e8e95df4 306 return EFI_NOT_READY;\r
b9d5fe03 307 }\r
308\r
e8e95df4 309 if (Transfer == MMC_IOBLOCKS_READ) {\r
339c6e90
HZ
310 if (BlockCount == 1) {\r
311 // Read a single block\r
312 Cmd = MMC_CMD17;\r
313 } else {\r
314 // Read multiple blocks\r
315 Cmd = MMC_CMD18;\r
e8e95df4 316 }\r
317 } else {\r
339c6e90
HZ
318 if (BlockCount == 1) {\r
319 // Write a single block\r
320 Cmd = MMC_CMD24;\r
321 } else {\r
322 // Write multiple blocks\r
323 Cmd = MMC_CMD25;\r
e8e95df4 324 }\r
e8e95df4 325 }\r
1bfda055 326\r
339c6e90
HZ
327 ConsumeSize = BlockCount * This->Media->BlockSize;\r
328 if (BytesRemainingToBeTransfered < ConsumeSize) {\r
329 ConsumeSize = BytesRemainingToBeTransfered;\r
e8e95df4 330 }\r
339c6e90 331 Status = MmcTransferBlock (This, Cmd, Transfer, MediaId, Lba, ConsumeSize, Buffer);\r
6f711615 332 if (EFI_ERROR (Status)) {\r
339c6e90 333 DEBUG ((EFI_D_ERROR, "%a(): Failed to transfer block and Status:%r\n", __func__, Status));\r
1bfda055 334 }\r
335\r
339c6e90
HZ
336 BytesRemainingToBeTransfered -= ConsumeSize;\r
337 if (BytesRemainingToBeTransfered > 0) {\r
338 Lba += BlockCount;\r
339 Buffer = (UINT8 *)Buffer + ConsumeSize;\r
340 }\r
e8e95df4 341 }\r
342\r
343 return EFI_SUCCESS;\r
1bfda055 344}\r
345\r
346EFI_STATUS\r
347EFIAPI\r
348MmcReadBlocks (\r
349 IN EFI_BLOCK_IO_PROTOCOL *This,\r
350 IN UINT32 MediaId,\r
351 IN EFI_LBA Lba,\r
352 IN UINTN BufferSize,\r
353 OUT VOID *Buffer\r
e8e95df4 354 )\r
355{\r
356 return MmcIoBlocks (This, MMC_IOBLOCKS_READ, MediaId, Lba, BufferSize, Buffer);\r
1bfda055 357}\r
358\r
359EFI_STATUS\r
360EFIAPI\r
361MmcWriteBlocks (\r
362 IN EFI_BLOCK_IO_PROTOCOL *This,\r
363 IN UINT32 MediaId,\r
364 IN EFI_LBA Lba,\r
365 IN UINTN BufferSize,\r
366 IN VOID *Buffer\r
e8e95df4 367 )\r
368{\r
369 return MmcIoBlocks (This, MMC_IOBLOCKS_WRITE, MediaId, Lba, BufferSize, Buffer);\r
1bfda055 370}\r
371\r
372EFI_STATUS\r
373EFIAPI\r
374MmcFlushBlocks (\r
375 IN EFI_BLOCK_IO_PROTOCOL *This\r
e8e95df4 376 )\r
377{\r
378 return EFI_SUCCESS;\r
1bfda055 379}\r