Commit | Line | Data |
---|---|---|
1e57a462 | 1 | /** @file NorFlashDxe.c\r |
2 | \r | |
2dff0c1a | 3 | Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>\r |
1e57a462 | 4 | \r |
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 | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include <Library/UefiLib.h>\r | |
16 | #include <Library/BaseMemoryLib.h>\r | |
17 | #include <Library/MemoryAllocationLib.h>\r | |
18 | #include <Library/UefiBootServicesTableLib.h>\r | |
19 | #include <Library/PcdLib.h>\r | |
20 | \r | |
21 | #include "NorFlashDxe.h"\r | |
22 | \r | |
23 | \r | |
24 | //\r | |
25 | // Global variable declarations\r | |
26 | //\r | |
27 | NOR_FLASH_INSTANCE **mNorFlashInstances;\r | |
28 | \r | |
29 | NOR_FLASH_INSTANCE mNorFlashInstanceTemplate = {\r | |
30 | NOR_FLASH_SIGNATURE, // Signature\r | |
31 | NULL, // Handle ... NEED TO BE FILLED\r | |
32 | \r | |
33 | FALSE, // Initialized\r | |
34 | NULL, // Initialize\r | |
35 | \r | |
36 | 0, // DeviceBaseAddress ... NEED TO BE FILLED\r | |
37 | 0, // RegionBaseAddress ... NEED TO BE FILLED\r | |
38 | 0, // Size ... NEED TO BE FILLED\r | |
39 | 0, // StartLba\r | |
40 | \r | |
1d5d0ae9 | 41 | {\r |
42 | EFI_BLOCK_IO_PROTOCOL_REVISION2, // Revision\r | |
43 | NULL, // Media ... NEED TO BE FILLED\r | |
44 | NorFlashBlockIoReset, // Reset;\r | |
1e57a462 | 45 | NorFlashBlockIoReadBlocks, // ReadBlocks\r |
46 | NorFlashBlockIoWriteBlocks, // WriteBlocks\r | |
1d5d0ae9 | 47 | NorFlashBlockIoFlushBlocks // FlushBlocks\r |
1e57a462 | 48 | }, // BlockIoProtocol\r |
49 | \r | |
1d5d0ae9 | 50 | {\r |
51 | 0, // MediaId ... NEED TO BE FILLED\r | |
52 | FALSE, // RemovableMedia\r | |
53 | TRUE, // MediaPresent\r | |
54 | FALSE, // LogicalPartition\r | |
55 | FALSE, // ReadOnly\r | |
56 | FALSE, // WriteCaching;\r | |
57 | 0, // BlockSize ... NEED TO BE FILLED\r | |
58 | 4, // IoAlign\r | |
59 | 0, // LastBlock ... NEED TO BE FILLED\r | |
60 | 0, // LowestAlignedLba\r | |
61 | 1, // LogicalBlocksPerPhysicalBlock\r | |
1e57a462 | 62 | }, //Media;\r |
63 | \r | |
64 | FALSE, // SupportFvb ... NEED TO BE FILLED\r | |
1d5d0ae9 | 65 | {\r |
1e57a462 | 66 | FvbGetAttributes, // GetAttributes\r |
67 | FvbSetAttributes, // SetAttributes\r | |
68 | FvbGetPhysicalAddress, // GetPhysicalAddress\r | |
69 | FvbGetBlockSize, // GetBlockSize\r | |
70 | FvbRead, // Read\r | |
71 | FvbWrite, // Write\r | |
1d5d0ae9 | 72 | FvbEraseBlocks, // EraseBlocks\r |
73 | NULL, //ParentHandle\r | |
1e57a462 | 74 | }, // FvbProtoccol;\r |
2dff0c1a | 75 | NULL, // FvbBuffer\r |
1e57a462 | 76 | {\r |
77 | {\r | |
78 | {\r | |
79 | HARDWARE_DEVICE_PATH,\r | |
80 | HW_VENDOR_DP,\r | |
81 | (UINT8)( sizeof(VENDOR_DEVICE_PATH) ),\r | |
82 | (UINT8)((sizeof(VENDOR_DEVICE_PATH)) >> 8),\r | |
83 | },\r | |
84 | { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, // GUID ... NEED TO BE FILLED\r | |
85 | },\r | |
86 | {\r | |
87 | END_DEVICE_PATH_TYPE,\r | |
88 | END_ENTIRE_DEVICE_PATH_SUBTYPE,\r | |
89 | sizeof (EFI_DEVICE_PATH_PROTOCOL),\r | |
90 | 0\r | |
91 | }\r | |
92 | } // DevicePath\r | |
93 | };\r | |
94 | \r | |
95 | EFI_STATUS\r | |
96 | NorFlashCreateInstance (\r | |
97 | IN UINTN NorFlashDeviceBase,\r | |
98 | IN UINTN NorFlashRegionBase,\r | |
99 | IN UINTN NorFlashSize,\r | |
100 | IN UINT32 MediaId,\r | |
101 | IN UINT32 BlockSize,\r | |
102 | IN BOOLEAN SupportFvb,\r | |
103 | IN CONST GUID *NorFlashGuid,\r | |
104 | OUT NOR_FLASH_INSTANCE** NorFlashInstance\r | |
105 | )\r | |
106 | {\r | |
107 | EFI_STATUS Status;\r | |
108 | NOR_FLASH_INSTANCE* Instance;\r | |
109 | \r | |
110 | ASSERT(NorFlashInstance != NULL);\r | |
111 | \r | |
2dff0c1a | 112 | Instance = AllocateRuntimeCopyPool (sizeof(NOR_FLASH_INSTANCE),&mNorFlashInstanceTemplate);\r |
1e57a462 | 113 | if (Instance == NULL) {\r |
114 | return EFI_OUT_OF_RESOURCES;\r | |
115 | }\r | |
116 | \r | |
117 | Instance->DeviceBaseAddress = NorFlashDeviceBase;\r | |
118 | Instance->RegionBaseAddress = NorFlashRegionBase;\r | |
119 | Instance->Size = NorFlashSize;\r | |
120 | \r | |
121 | Instance->BlockIoProtocol.Media = &Instance->Media;\r | |
122 | Instance->Media.MediaId = MediaId;\r | |
123 | Instance->Media.BlockSize = BlockSize;\r | |
124 | Instance->Media.LastBlock = (NorFlashSize / BlockSize)-1;\r | |
1d5d0ae9 | 125 | \r |
2dff0c1a | 126 | CopyGuid (&Instance->DevicePath.Vendor.Guid, NorFlashGuid);\r |
1e57a462 | 127 | \r |
128 | if (SupportFvb) {\r | |
129 | Instance->SupportFvb = TRUE;\r | |
130 | Instance->Initialize = NorFlashFvbInitialize;\r | |
2dff0c1a OM |
131 | Instance->FvbBuffer = AllocateRuntimePool (BlockSize);;\r |
132 | if (Instance->FvbBuffer == NULL) {\r | |
133 | return EFI_OUT_OF_RESOURCES;\r | |
134 | }\r | |
1e57a462 | 135 | \r |
136 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
137 | &Instance->Handle,\r | |
138 | &gEfiDevicePathProtocolGuid, &Instance->DevicePath,\r | |
139 | &gEfiBlockIoProtocolGuid, &Instance->BlockIoProtocol,\r | |
140 | &gEfiFirmwareVolumeBlockProtocolGuid, &Instance->FvbProtocol,\r | |
141 | NULL\r | |
142 | );\r | |
143 | if (EFI_ERROR(Status)) {\r | |
2dff0c1a | 144 | FreePool (Instance);\r |
1e57a462 | 145 | return Status;\r |
146 | }\r | |
147 | } else {\r | |
148 | Instance->Initialized = TRUE;\r | |
149 | \r | |
150 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
151 | &Instance->Handle,\r | |
152 | &gEfiDevicePathProtocolGuid, &Instance->DevicePath,\r | |
153 | &gEfiBlockIoProtocolGuid, &Instance->BlockIoProtocol,\r | |
154 | NULL\r | |
155 | );\r | |
156 | if (EFI_ERROR(Status)) {\r | |
2dff0c1a | 157 | FreePool (Instance);\r |
1e57a462 | 158 | return Status;\r |
159 | }\r | |
160 | }\r | |
1d5d0ae9 | 161 | \r |
162 | *NorFlashInstance = Instance;\r | |
1e57a462 | 163 | return Status;\r |
164 | }\r | |
165 | \r | |
166 | UINT32\r | |
167 | NorFlashReadStatusRegister (\r | |
168 | IN NOR_FLASH_INSTANCE *Instance,\r | |
169 | IN UINTN SR_Address\r | |
170 | )\r | |
171 | {\r | |
172 | // Prepare to read the status register\r | |
173 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_STATUS_REGISTER);\r | |
174 | return MmioRead32 (Instance->DeviceBaseAddress);\r | |
175 | }\r | |
176 | \r | |
177 | \r | |
178 | BOOLEAN\r | |
179 | NorFlashBlockIsLocked (\r | |
180 | IN NOR_FLASH_INSTANCE *Instance,\r | |
181 | IN UINTN BlockAddress\r | |
182 | )\r | |
183 | {\r | |
184 | UINT32 LockStatus;\r | |
185 | BOOLEAN BlockIsLocked;\r | |
186 | \r | |
187 | BlockIsLocked = TRUE;\r | |
188 | \r | |
189 | // Send command for reading device id\r | |
190 | SEND_NOR_COMMAND (BlockAddress, 2, P30_CMD_READ_DEVICE_ID);\r | |
191 | \r | |
192 | // Read block lock status\r | |
193 | LockStatus = MmioRead32 (CREATE_NOR_ADDRESS(BlockAddress, 2));\r | |
194 | \r | |
195 | // Decode block lock status\r | |
196 | LockStatus = FOLD_32BIT_INTO_16BIT(LockStatus);\r | |
197 | \r | |
198 | if ((LockStatus & 0x2) != 0) {\r | |
199 | DEBUG((EFI_D_ERROR, "NorFlashBlockIsLocked: WARNING: Block LOCKED DOWN\n"));\r | |
200 | }\r | |
201 | \r | |
202 | if ((LockStatus & 0x1) == 0) {\r | |
203 | // This means the block is unlocked\r | |
204 | DEBUG((DEBUG_BLKIO, "UnlockSingleBlock: Block 0x%08x unlocked\n", BlockAddress));\r | |
205 | BlockIsLocked = FALSE;\r | |
206 | }\r | |
207 | \r | |
208 | return BlockIsLocked;\r | |
209 | }\r | |
210 | \r | |
211 | \r | |
212 | EFI_STATUS\r | |
213 | NorFlashUnlockSingleBlock (\r | |
214 | IN NOR_FLASH_INSTANCE *Instance,\r | |
215 | IN UINTN BlockAddress\r | |
216 | )\r | |
217 | {\r | |
218 | EFI_STATUS Status = EFI_SUCCESS;\r | |
219 | UINT32 LockStatus;\r | |
220 | \r | |
221 | // Raise the Task Priority Level to TPL_NOTIFY to serialise all its operations\r | |
222 | // and to protect shared data structures.\r | |
223 | \r | |
224 | if (FeaturePcdGet (PcdNorFlashCheckBlockLocked) == TRUE) {\r | |
225 | do {\r | |
226 | // Request a lock setup\r | |
227 | SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_LOCK_BLOCK_SETUP);\r | |
228 | \r | |
229 | // Request an unlock\r | |
230 | SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_UNLOCK_BLOCK);\r | |
231 | \r | |
232 | // Send command for reading device id\r | |
233 | SEND_NOR_COMMAND (BlockAddress, 2, P30_CMD_READ_DEVICE_ID);\r | |
234 | \r | |
235 | // Read block lock status\r | |
236 | LockStatus = MmioRead32 (CREATE_NOR_ADDRESS(BlockAddress, 2));\r | |
237 | \r | |
238 | // Decode block lock status\r | |
239 | LockStatus = FOLD_32BIT_INTO_16BIT(LockStatus);\r | |
240 | } while ((LockStatus & 0x1) == 1);\r | |
241 | } else {\r | |
242 | // Request a lock setup\r | |
243 | SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_LOCK_BLOCK_SETUP);\r | |
244 | \r | |
245 | // Request an unlock\r | |
246 | SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_UNLOCK_BLOCK);\r | |
247 | \r | |
248 | // Wait until the status register gives us the all clear\r | |
249 | do {\r | |
250 | LockStatus = NorFlashReadStatusRegister (Instance, BlockAddress);\r | |
251 | } while ((LockStatus & P30_SR_BIT_WRITE) != P30_SR_BIT_WRITE);\r | |
252 | }\r | |
253 | \r | |
254 | // Put device back into Read Array mode\r | |
255 | SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_READ_ARRAY);\r | |
256 | \r | |
257 | DEBUG((DEBUG_BLKIO, "UnlockSingleBlock: BlockAddress=0x%08x, Exit Status = \"%r\".\n", BlockAddress, Status));\r | |
258 | \r | |
259 | return Status;\r | |
260 | }\r | |
261 | \r | |
262 | \r | |
263 | EFI_STATUS\r | |
264 | NorFlashUnlockSingleBlockIfNecessary (\r | |
265 | IN NOR_FLASH_INSTANCE *Instance,\r | |
266 | IN UINTN BlockAddress\r | |
267 | )\r | |
268 | {\r | |
269 | EFI_STATUS Status = EFI_SUCCESS;\r | |
270 | \r | |
271 | if (NorFlashBlockIsLocked (Instance, BlockAddress) == TRUE) {\r | |
272 | Status = NorFlashUnlockSingleBlock (Instance, BlockAddress);\r | |
273 | }\r | |
274 | \r | |
275 | return Status;\r | |
276 | }\r | |
277 | \r | |
278 | \r | |
279 | /**\r | |
280 | * The following function presumes that the block has already been unlocked.\r | |
281 | **/\r | |
282 | EFI_STATUS\r | |
283 | NorFlashEraseSingleBlock (\r | |
284 | IN NOR_FLASH_INSTANCE *Instance,\r | |
285 | IN UINTN BlockAddress\r | |
286 | )\r | |
287 | {\r | |
288 | EFI_STATUS Status;\r | |
289 | UINT32 StatusRegister;\r | |
290 | \r | |
291 | Status = EFI_SUCCESS;\r | |
292 | \r | |
293 | // Request a block erase and then confirm it\r | |
294 | SEND_NOR_COMMAND(BlockAddress, 0, P30_CMD_BLOCK_ERASE_SETUP);\r | |
295 | SEND_NOR_COMMAND(BlockAddress, 0, P30_CMD_BLOCK_ERASE_CONFIRM);\r | |
296 | \r | |
297 | // Wait until the status register gives us the all clear\r | |
298 | do {\r | |
299 | StatusRegister = NorFlashReadStatusRegister (Instance, BlockAddress);\r | |
300 | } while ((StatusRegister & P30_SR_BIT_WRITE) != P30_SR_BIT_WRITE);\r | |
301 | \r | |
302 | if (StatusRegister & P30_SR_BIT_VPP) {\r | |
303 | DEBUG((EFI_D_ERROR,"EraseSingleBlock(BlockAddress=0x%08x: VPP Range Error\n", BlockAddress));\r | |
304 | Status = EFI_DEVICE_ERROR;\r | |
305 | }\r | |
306 | \r | |
307 | if ((StatusRegister & (P30_SR_BIT_ERASE | P30_SR_BIT_PROGRAM)) == (P30_SR_BIT_ERASE | P30_SR_BIT_PROGRAM)) {\r | |
308 | DEBUG((EFI_D_ERROR,"EraseSingleBlock(BlockAddress=0x%08x: Command Sequence Error\n", BlockAddress));\r | |
309 | Status = EFI_DEVICE_ERROR;\r | |
310 | }\r | |
311 | \r | |
312 | if (StatusRegister & P30_SR_BIT_ERASE) {\r | |
313 | DEBUG((EFI_D_ERROR,"EraseSingleBlock(BlockAddress=0x%08x: Block Erase Error StatusRegister:0x%X\n", BlockAddress, StatusRegister));\r | |
314 | Status = EFI_DEVICE_ERROR;\r | |
315 | }\r | |
316 | \r | |
317 | if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {\r | |
318 | // The debug level message has been reduced because a device lock might happen. In this case we just retry it ...\r | |
319 | DEBUG((EFI_D_INFO,"EraseSingleBlock(BlockAddress=0x%08x: Block Locked Error\n", BlockAddress));\r | |
320 | Status = EFI_WRITE_PROTECTED;\r | |
321 | }\r | |
322 | \r | |
323 | if (EFI_ERROR(Status)) {\r | |
324 | // Clear the Status Register\r | |
325 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS_REGISTER);\r | |
326 | }\r | |
327 | \r | |
328 | // Put device back into Read Array mode\r | |
329 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r | |
330 | \r | |
331 | return Status;\r | |
332 | }\r | |
333 | \r | |
334 | /**\r | |
335 | * The following function presumes that the block has already been unlocked.\r | |
336 | **/\r | |
337 | EFI_STATUS\r | |
338 | NorFlashUnlockAndEraseSingleBlock (\r | |
339 | IN NOR_FLASH_INSTANCE *Instance,\r | |
340 | IN UINTN BlockAddress\r | |
341 | )\r | |
342 | {\r | |
343 | EFI_STATUS Status;\r | |
344 | UINTN Index;\r | |
345 | EFI_TPL OriginalTPL;\r | |
346 | \r | |
2dff0c1a OM |
347 | if (!EfiAtRuntime ()) {\r |
348 | // Raise TPL to TPL_HIGH to stop anyone from interrupting us.\r | |
349 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
350 | } else {\r | |
351 | // This initialization is only to prevent the compiler to complain about the\r | |
352 | // use of uninitialized variables\r | |
353 | OriginalTPL = TPL_HIGH_LEVEL;\r | |
354 | }\r | |
1e57a462 | 355 | \r |
356 | Index = 0;\r | |
357 | // The block erase might fail a first time (SW bug ?). Retry it ...\r | |
358 | do {\r | |
359 | // Unlock the block if we have to\r | |
360 | Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);\r | |
361 | if (!EFI_ERROR(Status)) {\r | |
362 | Status = NorFlashEraseSingleBlock (Instance, BlockAddress);\r | |
363 | }\r | |
364 | Index++;\r | |
365 | } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status == EFI_WRITE_PROTECTED));\r | |
366 | \r | |
367 | if (Index == NOR_FLASH_ERASE_RETRY) {\r | |
368 | DEBUG((EFI_D_ERROR,"EraseSingleBlock(BlockAddress=0x%08x: Block Locked Error (try to erase %d times)\n", BlockAddress,Index));\r | |
369 | }\r | |
370 | \r | |
2dff0c1a OM |
371 | if (!EfiAtRuntime ()) {\r |
372 | // Interruptions can resume.\r | |
373 | gBS->RestoreTPL (OriginalTPL);\r | |
374 | }\r | |
1e57a462 | 375 | \r |
376 | return Status;\r | |
377 | }\r | |
378 | \r | |
379 | \r | |
380 | EFI_STATUS\r | |
381 | NorFlashWriteSingleWord (\r | |
382 | IN NOR_FLASH_INSTANCE *Instance,\r | |
383 | IN UINTN WordAddress,\r | |
384 | IN UINT32 WriteData\r | |
385 | )\r | |
386 | {\r | |
387 | EFI_STATUS Status;\r | |
388 | UINT32 StatusRegister;\r | |
389 | \r | |
390 | Status = EFI_SUCCESS;\r | |
391 | \r | |
392 | // Request a write single word command\r | |
393 | SEND_NOR_COMMAND(WordAddress, 0, P30_CMD_WORD_PROGRAM_SETUP);\r | |
394 | \r | |
395 | // Store the word into NOR Flash;\r | |
396 | MmioWrite32 (WordAddress, WriteData);\r | |
397 | \r | |
398 | // Wait for the write to complete and then check for any errors; i.e. check the Status Register\r | |
399 | do {\r | |
400 | // Prepare to read the status register\r | |
401 | StatusRegister = NorFlashReadStatusRegister (Instance, WordAddress);\r | |
402 | // The chip is busy while the WRITE bit is not asserted\r | |
403 | } while ((StatusRegister & P30_SR_BIT_WRITE) != P30_SR_BIT_WRITE);\r | |
404 | \r | |
405 | \r | |
406 | // Perform a full status check:\r | |
407 | // Mask the relevant bits of Status Register.\r | |
408 | // Everything should be zero, if not, we have a problem\r | |
409 | \r | |
410 | if (StatusRegister & P30_SR_BIT_VPP) {\r | |
411 | DEBUG((EFI_D_ERROR,"NorFlashWriteSingleWord(WordAddress:0x%X): VPP Range Error\n",WordAddress));\r | |
412 | Status = EFI_DEVICE_ERROR;\r | |
413 | }\r | |
414 | \r | |
415 | if (StatusRegister & P30_SR_BIT_PROGRAM) {\r | |
416 | DEBUG((EFI_D_ERROR,"NorFlashWriteSingleWord(WordAddress:0x%X): Program Error\n",WordAddress));\r | |
417 | Status = EFI_DEVICE_ERROR;\r | |
418 | }\r | |
419 | \r | |
420 | if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {\r | |
421 | DEBUG((EFI_D_ERROR,"NorFlashWriteSingleWord(WordAddress:0x%X): Device Protect Error\n",WordAddress));\r | |
422 | Status = EFI_DEVICE_ERROR;\r | |
423 | }\r | |
424 | \r | |
425 | if (!EFI_ERROR(Status)) {\r | |
426 | // Clear the Status Register\r | |
427 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS_REGISTER);\r | |
428 | }\r | |
429 | \r | |
430 | // Put device back into Read Array mode\r | |
431 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r | |
432 | \r | |
433 | return Status;\r | |
434 | }\r | |
435 | \r | |
436 | /*\r | |
437 | * Writes data to the NOR Flash using the Buffered Programming method.\r | |
438 | *\r | |
439 | * The maximum size of the on-chip buffer is 32-words, because of hardware restrictions.\r | |
440 | * Therefore this function will only handle buffers up to 32 words or 128 bytes.\r | |
441 | * To deal with larger buffers, call this function again.\r | |
442 | *\r | |
443 | * This function presumes that both the TargetAddress and the TargetAddress+BufferSize\r | |
444 | * exist entirely within the NOR Flash. Therefore these conditions will not be checked here.\r | |
445 | *\r | |
446 | * In buffered programming, if the target address not at the beginning of a 32-bit word boundary,\r | |
447 | * then programming time is doubled and power consumption is increased.\r | |
448 | * Therefore, it is a requirement to align buffer writes to 32-bit word boundaries.\r | |
449 | * i.e. the last 4 bits of the target start address must be zero: 0x......00\r | |
450 | */\r | |
451 | EFI_STATUS\r | |
452 | NorFlashWriteBuffer (\r | |
453 | IN NOR_FLASH_INSTANCE *Instance,\r | |
454 | IN UINTN TargetAddress,\r | |
455 | IN UINTN BufferSizeInBytes,\r | |
456 | IN UINT32 *Buffer\r | |
457 | )\r | |
458 | {\r | |
459 | EFI_STATUS Status;\r | |
460 | UINTN BufferSizeInWords;\r | |
461 | UINTN Count;\r | |
462 | volatile UINT32 *Data;\r | |
463 | UINTN WaitForBuffer;\r | |
464 | BOOLEAN BufferAvailable;\r | |
465 | UINT32 StatusRegister;\r | |
466 | \r | |
467 | WaitForBuffer = MAX_BUFFERED_PROG_ITERATIONS;\r | |
468 | BufferAvailable = FALSE;\r | |
469 | \r | |
470 | // Check that the target address does not cross a 32-word boundary.\r | |
471 | if ((TargetAddress & BOUNDARY_OF_32_WORDS) != 0) {\r | |
472 | return EFI_INVALID_PARAMETER;\r | |
473 | }\r | |
474 | \r | |
475 | // Check there are some data to program\r | |
476 | if (BufferSizeInBytes == 0) {\r | |
477 | return EFI_BUFFER_TOO_SMALL;\r | |
478 | }\r | |
479 | \r | |
480 | // Check that the buffer size does not exceed the maximum hardware buffer size on chip.\r | |
481 | if (BufferSizeInBytes > P30_MAX_BUFFER_SIZE_IN_BYTES) {\r | |
482 | return EFI_BAD_BUFFER_SIZE;\r | |
483 | }\r | |
484 | \r | |
485 | // Check that the buffer size is a multiple of 32-bit words\r | |
486 | if ((BufferSizeInBytes % 4) != 0) {\r | |
487 | return EFI_BAD_BUFFER_SIZE;\r | |
488 | }\r | |
489 | \r | |
490 | // Pre-programming conditions checked, now start the algorithm.\r | |
491 | \r | |
492 | // Prepare the data destination address\r | |
493 | Data = (UINT32 *)TargetAddress;\r | |
494 | \r | |
495 | // Check the availability of the buffer\r | |
496 | do {\r | |
497 | // Issue the Buffered Program Setup command\r | |
498 | SEND_NOR_COMMAND(TargetAddress, 0, P30_CMD_BUFFERED_PROGRAM_SETUP);\r | |
499 | \r | |
500 | // Read back the status register bit#7 from the same address\r | |
501 | if (((*Data) & P30_SR_BIT_WRITE) == P30_SR_BIT_WRITE) {\r | |
502 | BufferAvailable = TRUE;\r | |
503 | }\r | |
504 | \r | |
505 | // Update the loop counter\r | |
506 | WaitForBuffer--;\r | |
507 | \r | |
508 | } while ((WaitForBuffer > 0) && (BufferAvailable == FALSE));\r | |
509 | \r | |
510 | // The buffer was not available for writing\r | |
511 | if (WaitForBuffer == 0) {\r | |
512 | Status = EFI_DEVICE_ERROR;\r | |
513 | goto EXIT;\r | |
514 | }\r | |
515 | \r | |
516 | // From now on we work in 32-bit words\r | |
517 | BufferSizeInWords = BufferSizeInBytes / (UINTN)4;\r | |
518 | \r | |
519 | // Write the word count, which is (buffer_size_in_words - 1),\r | |
520 | // because word count 0 means one word.\r | |
521 | SEND_NOR_COMMAND(TargetAddress, 0, (BufferSizeInWords - 1));\r | |
522 | \r | |
523 | // Write the data to the NOR Flash, advancing each address by 4 bytes\r | |
524 | for(Count=0; Count < BufferSizeInWords; Count++, Data++, Buffer++) {\r | |
525 | *Data = *Buffer;\r | |
526 | }\r | |
527 | \r | |
528 | // Issue the Buffered Program Confirm command, to start the programming operation\r | |
529 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_BUFFERED_PROGRAM_CONFIRM);\r | |
530 | \r | |
531 | // Wait for the write to complete and then check for any errors; i.e. check the Status Register\r | |
532 | do {\r | |
533 | StatusRegister = NorFlashReadStatusRegister (Instance, TargetAddress);\r | |
534 | // The chip is busy while the WRITE bit is not asserted\r | |
535 | } while ((StatusRegister & P30_SR_BIT_WRITE) != P30_SR_BIT_WRITE);\r | |
536 | \r | |
537 | \r | |
538 | // Perform a full status check:\r | |
539 | // Mask the relevant bits of Status Register.\r | |
540 | // Everything should be zero, if not, we have a problem\r | |
541 | \r | |
542 | Status = EFI_SUCCESS;\r | |
543 | \r | |
544 | if (StatusRegister & P30_SR_BIT_VPP) {\r | |
545 | DEBUG((EFI_D_ERROR,"NorFlashWriteBuffer(TargetAddress:0x%X): VPP Range Error\n", TargetAddress));\r | |
546 | Status = EFI_DEVICE_ERROR;\r | |
547 | }\r | |
548 | \r | |
549 | if (StatusRegister & P30_SR_BIT_PROGRAM) {\r | |
550 | DEBUG((EFI_D_ERROR,"NorFlashWriteBuffer(TargetAddress:0x%X): Program Error\n", TargetAddress));\r | |
551 | Status = EFI_DEVICE_ERROR;\r | |
552 | }\r | |
553 | \r | |
554 | if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {\r | |
555 | DEBUG((EFI_D_ERROR,"NorFlashWriteBuffer(TargetAddress:0x%X): Device Protect Error\n",TargetAddress));\r | |
556 | Status = EFI_DEVICE_ERROR;\r | |
557 | }\r | |
558 | \r | |
559 | if (!EFI_ERROR(Status)) {\r | |
560 | // Clear the Status Register\r | |
561 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS_REGISTER);\r | |
562 | }\r | |
563 | \r | |
564 | EXIT:\r | |
565 | // Put device back into Read Array mode\r | |
566 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r | |
567 | \r | |
568 | return Status;\r | |
569 | }\r | |
570 | \r | |
571 | EFI_STATUS\r | |
572 | NorFlashWriteSingleBlock (\r | |
573 | IN NOR_FLASH_INSTANCE *Instance,\r | |
574 | IN EFI_LBA Lba,\r | |
575 | IN UINT32 *DataBuffer,\r | |
576 | IN UINT32 BlockSizeInWords\r | |
577 | )\r | |
578 | {\r | |
579 | EFI_STATUS Status;\r | |
580 | UINTN WordAddress;\r | |
581 | UINT32 WordIndex;\r | |
582 | UINTN BufferIndex;\r | |
583 | UINTN BlockAddress;\r | |
584 | UINTN BuffersInBlock;\r | |
585 | UINTN RemainingWords;\r | |
586 | EFI_TPL OriginalTPL;\r | |
587 | \r | |
588 | Status = EFI_SUCCESS;\r | |
589 | \r | |
590 | // Get the physical address of the block\r | |
591 | BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSizeInWords * 4);\r | |
592 | \r | |
593 | // Start writing from the first address at the start of the block\r | |
594 | WordAddress = BlockAddress;\r | |
595 | \r | |
2dff0c1a OM |
596 | if (!EfiAtRuntime ()) {\r |
597 | // Raise TPL to TPL_HIGH to stop anyone from interrupting us.\r | |
598 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
599 | } else {\r | |
600 | // This initialization is only to prevent the compiler to complain about the\r | |
601 | // use of uninitialized variables\r | |
602 | OriginalTPL = TPL_HIGH_LEVEL;\r | |
603 | }\r | |
1e57a462 | 604 | \r |
605 | Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);\r | |
606 | if (EFI_ERROR(Status)) {\r | |
607 | DEBUG((EFI_D_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and Erase the single block at 0x%X\n", BlockAddress));\r | |
608 | goto EXIT;\r | |
609 | }\r | |
610 | \r | |
611 | // To speed up the programming operation, NOR Flash is programmed using the Buffered Programming method.\r | |
612 | \r | |
613 | // Check that the address starts at a 32-word boundary, i.e. last 7 bits must be zero\r | |
614 | if ((WordAddress & BOUNDARY_OF_32_WORDS) == 0x00) {\r | |
615 | \r | |
616 | // First, break the entire block into buffer-sized chunks.\r | |
82745213 | 617 | BuffersInBlock = (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE_IN_BYTES;\r |
1e57a462 | 618 | \r |
619 | // Then feed each buffer chunk to the NOR Flash\r | |
620 | for(BufferIndex=0;\r | |
621 | BufferIndex < BuffersInBlock;\r | |
622 | BufferIndex++, WordAddress += P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer += P30_MAX_BUFFER_SIZE_IN_WORDS\r | |
623 | ) {\r | |
624 | Status = NorFlashWriteBuffer (Instance, WordAddress, P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer);\r | |
625 | if (EFI_ERROR(Status)) {\r | |
626 | goto EXIT;\r | |
627 | }\r | |
628 | }\r | |
629 | \r | |
630 | // Finally, finish off any remaining words that are less than the maximum size of the buffer\r | |
631 | RemainingWords = BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;\r | |
632 | \r | |
633 | if(RemainingWords != 0) {\r | |
634 | Status = NorFlashWriteBuffer (Instance, WordAddress, (RemainingWords * 4), DataBuffer);\r | |
635 | if (EFI_ERROR(Status)) {\r | |
636 | goto EXIT;\r | |
637 | }\r | |
638 | }\r | |
639 | \r | |
640 | } else {\r | |
641 | // For now, use the single word programming algorithm\r | |
642 | // It is unlikely that the NOR Flash will exist in an address which falls within a 32 word boundary range,\r | |
643 | // i.e. which ends in the range 0x......01 - 0x......7F.\r | |
644 | for(WordIndex=0; WordIndex<BlockSizeInWords; WordIndex++, DataBuffer++, WordAddress = WordAddress + 4) {\r | |
645 | Status = NorFlashWriteSingleWord (Instance, WordAddress, *DataBuffer);\r | |
646 | if (EFI_ERROR(Status)) {\r | |
647 | goto EXIT;\r | |
648 | }\r | |
649 | }\r | |
650 | }\r | |
651 | \r | |
652 | EXIT:\r | |
2dff0c1a OM |
653 | if (!EfiAtRuntime ()) {\r |
654 | // Interruptions can resume.\r | |
655 | gBS->RestoreTPL (OriginalTPL);\r | |
656 | }\r | |
1e57a462 | 657 | \r |
658 | if (EFI_ERROR(Status)) {\r | |
659 | DEBUG((EFI_D_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed at address 0x%08x. Exit Status = \"%r\".\n", WordAddress, Status));\r | |
660 | }\r | |
661 | return Status;\r | |
662 | }\r | |
663 | \r | |
664 | \r | |
665 | EFI_STATUS\r | |
666 | NorFlashWriteBlocks (\r | |
667 | IN NOR_FLASH_INSTANCE *Instance,\r | |
668 | IN EFI_LBA Lba,\r | |
669 | IN UINTN BufferSizeInBytes,\r | |
670 | IN VOID *Buffer\r | |
671 | )\r | |
672 | {\r | |
673 | UINT32 *pWriteBuffer;\r | |
674 | EFI_STATUS Status = EFI_SUCCESS;\r | |
675 | EFI_LBA CurrentBlock;\r | |
676 | UINT32 BlockSizeInWords;\r | |
677 | UINT32 NumBlocks;\r | |
678 | UINT32 BlockCount;\r | |
679 | \r | |
680 | // The buffer must be valid\r | |
681 | if (Buffer == NULL) {\r | |
682 | return EFI_INVALID_PARAMETER;\r | |
683 | }\r | |
684 | \r | |
685 | if(Instance->Media.ReadOnly == TRUE) {\r | |
686 | return EFI_WRITE_PROTECTED;\r | |
687 | }\r | |
688 | \r | |
689 | // We must have some bytes to read\r | |
690 | DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: BufferSizeInBytes=0x%x\n", BufferSizeInBytes));\r | |
691 | if(BufferSizeInBytes == 0) {\r | |
692 | return EFI_BAD_BUFFER_SIZE;\r | |
693 | }\r | |
694 | \r | |
695 | // The size of the buffer must be a multiple of the block size\r | |
696 | DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: BlockSize in bytes =0x%x\n", Instance->Media.BlockSize));\r | |
697 | if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) {\r | |
698 | return EFI_BAD_BUFFER_SIZE;\r | |
699 | }\r | |
700 | \r | |
701 | // All blocks must be within the device\r | |
702 | NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ;\r | |
703 | \r | |
704 | DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: NumBlocks=%d, LastBlock=%ld, Lba=%ld.\n", NumBlocks, Instance->Media.LastBlock, Lba));\r | |
705 | \r | |
706 | if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) {\r | |
707 | DEBUG((EFI_D_ERROR, "NorFlashWriteBlocks: ERROR - Write will exceed last block.\n"));\r | |
708 | return EFI_INVALID_PARAMETER;\r | |
709 | }\r | |
710 | \r | |
711 | BlockSizeInWords = Instance->Media.BlockSize / 4;\r | |
712 | \r | |
713 | // Because the target *Buffer is a pointer to VOID, we must put all the data into a pointer\r | |
714 | // to a proper data type, so use *ReadBuffer\r | |
715 | pWriteBuffer = (UINT32 *)Buffer;\r | |
716 | \r | |
717 | CurrentBlock = Lba;\r | |
718 | for (BlockCount=0; BlockCount < NumBlocks; BlockCount++, CurrentBlock++, pWriteBuffer = pWriteBuffer + BlockSizeInWords) {\r | |
719 | \r | |
720 | DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: Writing block #%d\n", (UINTN)CurrentBlock));\r | |
721 | \r | |
722 | Status = NorFlashWriteSingleBlock (Instance, CurrentBlock, pWriteBuffer, BlockSizeInWords);\r | |
723 | \r | |
724 | if (EFI_ERROR(Status)) {\r | |
725 | break;\r | |
726 | }\r | |
727 | }\r | |
728 | \r | |
729 | DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: Exit Status = \"%r\".\n", Status));\r | |
730 | return Status;\r | |
731 | }\r | |
732 | \r | |
733 | EFI_STATUS\r | |
734 | NorFlashReadBlocks (\r | |
735 | IN NOR_FLASH_INSTANCE *Instance,\r | |
736 | IN EFI_LBA Lba,\r | |
737 | IN UINTN BufferSizeInBytes,\r | |
738 | OUT VOID *Buffer\r | |
739 | )\r | |
740 | {\r | |
741 | UINT32 NumBlocks;\r | |
742 | UINTN StartAddress;\r | |
743 | \r | |
3d783074 | 744 | DEBUG((DEBUG_BLKIO, "NorFlashReadBlocks: BufferSize=0x%xB BlockSize=0x%xB LastBlock=%ld, Lba=%ld.\n",\r |
745 | BufferSizeInBytes, Instance->Media.BlockSize, Instance->Media.LastBlock, Lba));\r | |
746 | \r | |
1e57a462 | 747 | // The buffer must be valid\r |
748 | if (Buffer == NULL) {\r | |
749 | return EFI_INVALID_PARAMETER;\r | |
750 | }\r | |
751 | \r | |
3d783074 | 752 | // Return if we have not any byte to read \r |
753 | if (BufferSizeInBytes == 0) {\r | |
754 | return EFI_SUCCESS;\r | |
1e57a462 | 755 | }\r |
756 | \r | |
757 | // The size of the buffer must be a multiple of the block size\r | |
1e57a462 | 758 | if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) {\r |
759 | return EFI_BAD_BUFFER_SIZE;\r | |
760 | }\r | |
761 | \r | |
762 | // All blocks must be within the device\r | |
763 | NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ;\r | |
764 | \r | |
1e57a462 | 765 | if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) {\r |
766 | DEBUG((EFI_D_ERROR, "NorFlashReadBlocks: ERROR - Read will exceed last block\n"));\r | |
767 | return EFI_INVALID_PARAMETER;\r | |
768 | }\r | |
769 | \r | |
770 | // Get the address to start reading from\r | |
771 | StartAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress,\r | |
772 | Lba,\r | |
773 | Instance->Media.BlockSize\r | |
774 | );\r | |
775 | \r | |
776 | // Put the device into Read Array mode\r | |
777 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r | |
778 | \r | |
779 | // Readout the data\r | |
780 | CopyMem(Buffer, (UINTN *)StartAddress, BufferSizeInBytes);\r | |
781 | \r | |
782 | return EFI_SUCCESS;\r | |
783 | }\r | |
784 | \r | |
785 | EFI_STATUS\r | |
786 | NorFlashReset (\r | |
787 | IN NOR_FLASH_INSTANCE *Instance\r | |
788 | )\r | |
789 | {\r | |
790 | // As there is no specific RESET to perform, ensure that the devices is in the default Read Array mode\r | |
791 | SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r | |
792 | return EFI_SUCCESS;\r | |
793 | }\r | |
794 | \r | |
795 | EFI_STATUS\r | |
796 | EFIAPI\r | |
797 | NorFlashInitialise (\r | |
798 | IN EFI_HANDLE ImageHandle,\r | |
799 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
800 | )\r | |
801 | {\r | |
802 | EFI_STATUS Status;\r | |
803 | UINT32 Index;\r | |
804 | NOR_FLASH_DESCRIPTION* NorFlashDevices;\r | |
805 | UINT32 NorFlashDeviceCount;\r | |
806 | BOOLEAN ContainVariableStorage;\r | |
807 | \r | |
808 | Status = NorFlashPlatformInitialization ();\r | |
809 | if (EFI_ERROR(Status)) {\r | |
810 | DEBUG((EFI_D_ERROR,"NorFlashInitialise: Fail to initialize Nor Flash devices\n"));\r | |
811 | return Status;\r | |
812 | }\r | |
813 | \r | |
814 | Status = NorFlashPlatformGetDevices (&NorFlashDevices,&NorFlashDeviceCount);\r | |
815 | if (EFI_ERROR(Status)) {\r | |
816 | DEBUG((EFI_D_ERROR,"NorFlashInitialise: Fail to get Nor Flash devices\n"));\r | |
817 | return Status;\r | |
818 | }\r | |
819 | \r | |
2dff0c1a | 820 | mNorFlashInstances = AllocateRuntimePool (sizeof(NOR_FLASH_INSTANCE*) * NorFlashDeviceCount);\r |
1e57a462 | 821 | \r |
822 | for (Index = 0; Index < NorFlashDeviceCount; Index++) {\r | |
823 | // Check if this NOR Flash device contain the variable storage region\r | |
824 | ContainVariableStorage =\r | |
825 | (NorFlashDevices[Index].RegionBaseAddress <= PcdGet32 (PcdFlashNvStorageVariableBase)) &&\r | |
826 | (PcdGet32 (PcdFlashNvStorageVariableBase) + PcdGet32 (PcdFlashNvStorageVariableSize) <= NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);\r | |
827 | \r | |
828 | Status = NorFlashCreateInstance (\r | |
829 | NorFlashDevices[Index].DeviceBaseAddress,\r | |
830 | NorFlashDevices[Index].RegionBaseAddress,\r | |
831 | NorFlashDevices[Index].Size,\r | |
832 | Index,\r | |
833 | NorFlashDevices[Index].BlockSize,\r | |
834 | ContainVariableStorage,\r | |
835 | &NorFlashDevices[Index].Guid,\r | |
836 | &mNorFlashInstances[Index]\r | |
837 | );\r | |
838 | if (EFI_ERROR(Status)) {\r | |
839 | DEBUG((EFI_D_ERROR,"NorFlashInitialise: Fail to create instance for NorFlash[%d]\n",Index));\r | |
840 | }\r | |
841 | }\r | |
842 | \r | |
843 | return Status;\r | |
844 | }\r |