]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/Drivers/NorFlashDxe/NorFlashDxe.c
MdePkg/BaseLib AARCH64: terminate stack frame list on stack switch
[mirror_edk2.git] / ArmPlatformPkg / Drivers / NorFlashDxe / NorFlashDxe.c
CommitLineData
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
1dbbfc17 23STATIC EFI_EVENT mNorFlashVirtualAddrChangeEvent;\r
1e57a462 24\r
25//\r
26// Global variable declarations\r
27//\r
28NOR_FLASH_INSTANCE **mNorFlashInstances;\r
1dbbfc17 29UINT32 mNorFlashDeviceCount;\r
1e57a462 30\r
31NOR_FLASH_INSTANCE mNorFlashInstanceTemplate = {\r
32 NOR_FLASH_SIGNATURE, // Signature\r
33 NULL, // Handle ... NEED TO BE FILLED\r
34\r
35 FALSE, // Initialized\r
36 NULL, // Initialize\r
37\r
38 0, // DeviceBaseAddress ... NEED TO BE FILLED\r
39 0, // RegionBaseAddress ... NEED TO BE FILLED\r
40 0, // Size ... NEED TO BE FILLED\r
41 0, // StartLba\r
42\r
1d5d0ae9 43 {\r
44 EFI_BLOCK_IO_PROTOCOL_REVISION2, // Revision\r
45 NULL, // Media ... NEED TO BE FILLED\r
46 NorFlashBlockIoReset, // Reset;\r
1e57a462 47 NorFlashBlockIoReadBlocks, // ReadBlocks\r
48 NorFlashBlockIoWriteBlocks, // WriteBlocks\r
1d5d0ae9 49 NorFlashBlockIoFlushBlocks // FlushBlocks\r
1e57a462 50 }, // BlockIoProtocol\r
51\r
1d5d0ae9 52 {\r
53 0, // MediaId ... NEED TO BE FILLED\r
54 FALSE, // RemovableMedia\r
55 TRUE, // MediaPresent\r
56 FALSE, // LogicalPartition\r
57 FALSE, // ReadOnly\r
58 FALSE, // WriteCaching;\r
59 0, // BlockSize ... NEED TO BE FILLED\r
60 4, // IoAlign\r
61 0, // LastBlock ... NEED TO BE FILLED\r
62 0, // LowestAlignedLba\r
63 1, // LogicalBlocksPerPhysicalBlock\r
1e57a462 64 }, //Media;\r
65\r
452a9ee1
BJ
66 {\r
67 EFI_DISK_IO_PROTOCOL_REVISION, // Revision\r
68 NorFlashDiskIoReadDisk, // ReadDisk\r
69 NorFlashDiskIoWriteDisk // WriteDisk\r
70 },\r
71\r
1e57a462 72 FALSE, // SupportFvb ... NEED TO BE FILLED\r
1d5d0ae9 73 {\r
1e57a462 74 FvbGetAttributes, // GetAttributes\r
75 FvbSetAttributes, // SetAttributes\r
76 FvbGetPhysicalAddress, // GetPhysicalAddress\r
77 FvbGetBlockSize, // GetBlockSize\r
78 FvbRead, // Read\r
79 FvbWrite, // Write\r
1d5d0ae9 80 FvbEraseBlocks, // EraseBlocks\r
81 NULL, //ParentHandle\r
1e57a462 82 }, // FvbProtoccol;\r
452a9ee1 83 NULL, // ShadowBuffer\r
1e57a462 84 {\r
85 {\r
86 {\r
87 HARDWARE_DEVICE_PATH,\r
88 HW_VENDOR_DP,\r
b0fdce95 89 { (UINT8)sizeof(VENDOR_DEVICE_PATH), (UINT8)((sizeof(VENDOR_DEVICE_PATH)) >> 8) }\r
1e57a462 90 },\r
b0fdce95 91 { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } }, // GUID ... NEED TO BE FILLED\r
1e57a462 92 },\r
93 {\r
94 END_DEVICE_PATH_TYPE,\r
95 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
b0fdce95 96 { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 }\r
1e57a462 97 }\r
98 } // DevicePath\r
99};\r
100\r
101EFI_STATUS\r
102NorFlashCreateInstance (\r
103 IN UINTN NorFlashDeviceBase,\r
104 IN UINTN NorFlashRegionBase,\r
105 IN UINTN NorFlashSize,\r
106 IN UINT32 MediaId,\r
107 IN UINT32 BlockSize,\r
108 IN BOOLEAN SupportFvb,\r
109 IN CONST GUID *NorFlashGuid,\r
110 OUT NOR_FLASH_INSTANCE** NorFlashInstance\r
111 )\r
112{\r
113 EFI_STATUS Status;\r
114 NOR_FLASH_INSTANCE* Instance;\r
115\r
116 ASSERT(NorFlashInstance != NULL);\r
117\r
2dff0c1a 118 Instance = AllocateRuntimeCopyPool (sizeof(NOR_FLASH_INSTANCE),&mNorFlashInstanceTemplate);\r
1e57a462 119 if (Instance == NULL) {\r
120 return EFI_OUT_OF_RESOURCES;\r
121 }\r
122\r
123 Instance->DeviceBaseAddress = NorFlashDeviceBase;\r
124 Instance->RegionBaseAddress = NorFlashRegionBase;\r
125 Instance->Size = NorFlashSize;\r
126\r
127 Instance->BlockIoProtocol.Media = &Instance->Media;\r
128 Instance->Media.MediaId = MediaId;\r
129 Instance->Media.BlockSize = BlockSize;\r
130 Instance->Media.LastBlock = (NorFlashSize / BlockSize)-1;\r
1d5d0ae9 131\r
2dff0c1a 132 CopyGuid (&Instance->DevicePath.Vendor.Guid, NorFlashGuid);\r
1e57a462 133\r
452a9ee1
BJ
134 Instance->ShadowBuffer = AllocateRuntimePool (BlockSize);;\r
135 if (Instance->ShadowBuffer == NULL) {\r
136 return EFI_OUT_OF_RESOURCES;\r
137 }\r
138\r
1e57a462 139 if (SupportFvb) {\r
140 Instance->SupportFvb = TRUE;\r
141 Instance->Initialize = NorFlashFvbInitialize;\r
142\r
143 Status = gBS->InstallMultipleProtocolInterfaces (\r
144 &Instance->Handle,\r
145 &gEfiDevicePathProtocolGuid, &Instance->DevicePath,\r
146 &gEfiBlockIoProtocolGuid, &Instance->BlockIoProtocol,\r
147 &gEfiFirmwareVolumeBlockProtocolGuid, &Instance->FvbProtocol,\r
148 NULL\r
149 );\r
150 if (EFI_ERROR(Status)) {\r
2dff0c1a 151 FreePool (Instance);\r
1e57a462 152 return Status;\r
153 }\r
154 } else {\r
155 Instance->Initialized = TRUE;\r
156\r
157 Status = gBS->InstallMultipleProtocolInterfaces (\r
158 &Instance->Handle,\r
159 &gEfiDevicePathProtocolGuid, &Instance->DevicePath,\r
160 &gEfiBlockIoProtocolGuid, &Instance->BlockIoProtocol,\r
452a9ee1 161 &gEfiDiskIoProtocolGuid, &Instance->DiskIoProtocol,\r
1e57a462 162 NULL\r
163 );\r
164 if (EFI_ERROR(Status)) {\r
2dff0c1a 165 FreePool (Instance);\r
1e57a462 166 return Status;\r
167 }\r
168 }\r
1d5d0ae9 169\r
170 *NorFlashInstance = Instance;\r
1e57a462 171 return Status;\r
172}\r
173\r
174UINT32\r
175NorFlashReadStatusRegister (\r
176 IN NOR_FLASH_INSTANCE *Instance,\r
177 IN UINTN SR_Address\r
178 )\r
179{\r
180 // Prepare to read the status register\r
181 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_STATUS_REGISTER);\r
182 return MmioRead32 (Instance->DeviceBaseAddress);\r
183}\r
184\r
22044caa 185STATIC\r
1e57a462 186BOOLEAN\r
187NorFlashBlockIsLocked (\r
188 IN NOR_FLASH_INSTANCE *Instance,\r
189 IN UINTN BlockAddress\r
190 )\r
191{\r
192 UINT32 LockStatus;\r
1e57a462 193\r
194 // Send command for reading device id\r
195 SEND_NOR_COMMAND (BlockAddress, 2, P30_CMD_READ_DEVICE_ID);\r
196\r
197 // Read block lock status\r
198 LockStatus = MmioRead32 (CREATE_NOR_ADDRESS(BlockAddress, 2));\r
199\r
200 // Decode block lock status\r
201 LockStatus = FOLD_32BIT_INTO_16BIT(LockStatus);\r
202\r
203 if ((LockStatus & 0x2) != 0) {\r
204 DEBUG((EFI_D_ERROR, "NorFlashBlockIsLocked: WARNING: Block LOCKED DOWN\n"));\r
205 }\r
206\r
22044caa 207 return ((LockStatus & 0x1) != 0);\r
1e57a462 208}\r
209\r
22044caa 210STATIC\r
1e57a462 211EFI_STATUS\r
212NorFlashUnlockSingleBlock (\r
213 IN NOR_FLASH_INSTANCE *Instance,\r
214 IN UINTN BlockAddress\r
215 )\r
216{\r
1e57a462 217 UINT32 LockStatus;\r
218\r
219 // Raise the Task Priority Level to TPL_NOTIFY to serialise all its operations\r
220 // and to protect shared data structures.\r
221\r
222 if (FeaturePcdGet (PcdNorFlashCheckBlockLocked) == TRUE) {\r
223 do {\r
224 // Request a lock setup\r
225 SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_LOCK_BLOCK_SETUP);\r
226\r
227 // Request an unlock\r
228 SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_UNLOCK_BLOCK);\r
229\r
230 // Send command for reading device id\r
231 SEND_NOR_COMMAND (BlockAddress, 2, P30_CMD_READ_DEVICE_ID);\r
232\r
233 // Read block lock status\r
234 LockStatus = MmioRead32 (CREATE_NOR_ADDRESS(BlockAddress, 2));\r
235\r
236 // Decode block lock status\r
237 LockStatus = FOLD_32BIT_INTO_16BIT(LockStatus);\r
238 } while ((LockStatus & 0x1) == 1);\r
239 } else {\r
240 // Request a lock setup\r
241 SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_LOCK_BLOCK_SETUP);\r
242\r
243 // Request an unlock\r
244 SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_UNLOCK_BLOCK);\r
245\r
246 // Wait until the status register gives us the all clear\r
247 do {\r
248 LockStatus = NorFlashReadStatusRegister (Instance, BlockAddress);\r
249 } while ((LockStatus & P30_SR_BIT_WRITE) != P30_SR_BIT_WRITE);\r
250 }\r
251\r
252 // Put device back into Read Array mode\r
253 SEND_NOR_COMMAND (BlockAddress, 0, P30_CMD_READ_ARRAY);\r
254\r
22044caa 255 DEBUG((DEBUG_BLKIO, "UnlockSingleBlock: BlockAddress=0x%08x\n", BlockAddress));\r
1e57a462 256\r
22044caa 257 return EFI_SUCCESS;\r
1e57a462 258}\r
259\r
22044caa 260STATIC\r
1e57a462 261EFI_STATUS\r
262NorFlashUnlockSingleBlockIfNecessary (\r
263 IN NOR_FLASH_INSTANCE *Instance,\r
264 IN UINTN BlockAddress\r
265 )\r
266{\r
22044caa
OM
267 EFI_STATUS Status;\r
268\r
269 Status = EFI_SUCCESS;\r
1e57a462 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
22044caa 282STATIC\r
1e57a462 283EFI_STATUS\r
284NorFlashEraseSingleBlock (\r
285 IN NOR_FLASH_INSTANCE *Instance,\r
286 IN UINTN BlockAddress\r
287 )\r
288{\r
289 EFI_STATUS Status;\r
290 UINT32 StatusRegister;\r
291\r
292 Status = EFI_SUCCESS;\r
293\r
294 // Request a block erase and then confirm it\r
295 SEND_NOR_COMMAND(BlockAddress, 0, P30_CMD_BLOCK_ERASE_SETUP);\r
296 SEND_NOR_COMMAND(BlockAddress, 0, P30_CMD_BLOCK_ERASE_CONFIRM);\r
297\r
298 // Wait until the status register gives us the all clear\r
299 do {\r
300 StatusRegister = NorFlashReadStatusRegister (Instance, BlockAddress);\r
301 } while ((StatusRegister & P30_SR_BIT_WRITE) != P30_SR_BIT_WRITE);\r
302\r
303 if (StatusRegister & P30_SR_BIT_VPP) {\r
304 DEBUG((EFI_D_ERROR,"EraseSingleBlock(BlockAddress=0x%08x: VPP Range Error\n", BlockAddress));\r
305 Status = EFI_DEVICE_ERROR;\r
306 }\r
307\r
308 if ((StatusRegister & (P30_SR_BIT_ERASE | P30_SR_BIT_PROGRAM)) == (P30_SR_BIT_ERASE | P30_SR_BIT_PROGRAM)) {\r
309 DEBUG((EFI_D_ERROR,"EraseSingleBlock(BlockAddress=0x%08x: Command Sequence Error\n", BlockAddress));\r
310 Status = EFI_DEVICE_ERROR;\r
311 }\r
312\r
313 if (StatusRegister & P30_SR_BIT_ERASE) {\r
314 DEBUG((EFI_D_ERROR,"EraseSingleBlock(BlockAddress=0x%08x: Block Erase Error StatusRegister:0x%X\n", BlockAddress, StatusRegister));\r
315 Status = EFI_DEVICE_ERROR;\r
316 }\r
317\r
318 if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {\r
319 // The debug level message has been reduced because a device lock might happen. In this case we just retry it ...\r
320 DEBUG((EFI_D_INFO,"EraseSingleBlock(BlockAddress=0x%08x: Block Locked Error\n", BlockAddress));\r
321 Status = EFI_WRITE_PROTECTED;\r
322 }\r
323\r
324 if (EFI_ERROR(Status)) {\r
325 // Clear the Status Register\r
326 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS_REGISTER);\r
327 }\r
328\r
329 // Put device back into Read Array mode\r
330 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
331\r
332 return Status;\r
333}\r
334\r
335/**\r
22044caa 336 * This function unlock and erase an entire NOR Flash block.\r
1e57a462 337 **/\r
338EFI_STATUS\r
339NorFlashUnlockAndEraseSingleBlock (\r
340 IN NOR_FLASH_INSTANCE *Instance,\r
341 IN UINTN BlockAddress\r
342 )\r
343{\r
344 EFI_STATUS Status;\r
345 UINTN Index;\r
346 EFI_TPL OriginalTPL;\r
347\r
2dff0c1a
OM
348 if (!EfiAtRuntime ()) {\r
349 // Raise TPL to TPL_HIGH to stop anyone from interrupting us.\r
350 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
351 } else {\r
352 // This initialization is only to prevent the compiler to complain about the\r
353 // use of uninitialized variables\r
354 OriginalTPL = TPL_HIGH_LEVEL;\r
355 }\r
1e57a462 356\r
357 Index = 0;\r
358 // The block erase might fail a first time (SW bug ?). Retry it ...\r
359 do {\r
360 // Unlock the block if we have to\r
361 Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);\r
22044caa
OM
362 if (EFI_ERROR (Status)) {\r
363 break;\r
1e57a462 364 }\r
22044caa 365 Status = NorFlashEraseSingleBlock (Instance, BlockAddress);\r
1e57a462 366 Index++;\r
367 } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status == EFI_WRITE_PROTECTED));\r
368\r
369 if (Index == NOR_FLASH_ERASE_RETRY) {\r
370 DEBUG((EFI_D_ERROR,"EraseSingleBlock(BlockAddress=0x%08x: Block Locked Error (try to erase %d times)\n", BlockAddress,Index));\r
371 }\r
372\r
2dff0c1a
OM
373 if (!EfiAtRuntime ()) {\r
374 // Interruptions can resume.\r
375 gBS->RestoreTPL (OriginalTPL);\r
376 }\r
1e57a462 377\r
378 return Status;\r
379}\r
380\r
381\r
452a9ee1 382STATIC\r
1e57a462 383EFI_STATUS\r
384NorFlashWriteSingleWord (\r
385 IN NOR_FLASH_INSTANCE *Instance,\r
386 IN UINTN WordAddress,\r
387 IN UINT32 WriteData\r
388 )\r
389{\r
390 EFI_STATUS Status;\r
391 UINT32 StatusRegister;\r
392\r
393 Status = EFI_SUCCESS;\r
394\r
395 // Request a write single word command\r
396 SEND_NOR_COMMAND(WordAddress, 0, P30_CMD_WORD_PROGRAM_SETUP);\r
397\r
398 // Store the word into NOR Flash;\r
399 MmioWrite32 (WordAddress, WriteData);\r
400\r
401 // Wait for the write to complete and then check for any errors; i.e. check the Status Register\r
402 do {\r
403 // Prepare to read the status register\r
404 StatusRegister = NorFlashReadStatusRegister (Instance, WordAddress);\r
405 // The chip is busy while the WRITE bit is not asserted\r
406 } while ((StatusRegister & P30_SR_BIT_WRITE) != P30_SR_BIT_WRITE);\r
407\r
408\r
409 // Perform a full status check:\r
410 // Mask the relevant bits of Status Register.\r
411 // Everything should be zero, if not, we have a problem\r
412\r
413 if (StatusRegister & P30_SR_BIT_VPP) {\r
414 DEBUG((EFI_D_ERROR,"NorFlashWriteSingleWord(WordAddress:0x%X): VPP Range Error\n",WordAddress));\r
415 Status = EFI_DEVICE_ERROR;\r
416 }\r
417\r
418 if (StatusRegister & P30_SR_BIT_PROGRAM) {\r
419 DEBUG((EFI_D_ERROR,"NorFlashWriteSingleWord(WordAddress:0x%X): Program Error\n",WordAddress));\r
420 Status = EFI_DEVICE_ERROR;\r
421 }\r
422\r
423 if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {\r
424 DEBUG((EFI_D_ERROR,"NorFlashWriteSingleWord(WordAddress:0x%X): Device Protect Error\n",WordAddress));\r
425 Status = EFI_DEVICE_ERROR;\r
426 }\r
427\r
428 if (!EFI_ERROR(Status)) {\r
429 // Clear the Status Register\r
430 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS_REGISTER);\r
431 }\r
432\r
433 // Put device back into Read Array mode\r
434 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
435\r
436 return Status;\r
437}\r
438\r
439/*\r
440 * Writes data to the NOR Flash using the Buffered Programming method.\r
441 *\r
442 * The maximum size of the on-chip buffer is 32-words, because of hardware restrictions.\r
443 * Therefore this function will only handle buffers up to 32 words or 128 bytes.\r
444 * To deal with larger buffers, call this function again.\r
445 *\r
446 * This function presumes that both the TargetAddress and the TargetAddress+BufferSize\r
447 * exist entirely within the NOR Flash. Therefore these conditions will not be checked here.\r
448 *\r
449 * In buffered programming, if the target address not at the beginning of a 32-bit word boundary,\r
450 * then programming time is doubled and power consumption is increased.\r
451 * Therefore, it is a requirement to align buffer writes to 32-bit word boundaries.\r
452 * i.e. the last 4 bits of the target start address must be zero: 0x......00\r
453 */\r
454EFI_STATUS\r
455NorFlashWriteBuffer (\r
456 IN NOR_FLASH_INSTANCE *Instance,\r
457 IN UINTN TargetAddress,\r
458 IN UINTN BufferSizeInBytes,\r
459 IN UINT32 *Buffer\r
460 )\r
461{\r
462 EFI_STATUS Status;\r
463 UINTN BufferSizeInWords;\r
464 UINTN Count;\r
465 volatile UINT32 *Data;\r
466 UINTN WaitForBuffer;\r
467 BOOLEAN BufferAvailable;\r
468 UINT32 StatusRegister;\r
469\r
470 WaitForBuffer = MAX_BUFFERED_PROG_ITERATIONS;\r
471 BufferAvailable = FALSE;\r
472\r
473 // Check that the target address does not cross a 32-word boundary.\r
474 if ((TargetAddress & BOUNDARY_OF_32_WORDS) != 0) {\r
475 return EFI_INVALID_PARAMETER;\r
476 }\r
477\r
478 // Check there are some data to program\r
479 if (BufferSizeInBytes == 0) {\r
480 return EFI_BUFFER_TOO_SMALL;\r
481 }\r
482\r
483 // Check that the buffer size does not exceed the maximum hardware buffer size on chip.\r
484 if (BufferSizeInBytes > P30_MAX_BUFFER_SIZE_IN_BYTES) {\r
485 return EFI_BAD_BUFFER_SIZE;\r
486 }\r
487\r
488 // Check that the buffer size is a multiple of 32-bit words\r
489 if ((BufferSizeInBytes % 4) != 0) {\r
490 return EFI_BAD_BUFFER_SIZE;\r
491 }\r
492\r
493 // Pre-programming conditions checked, now start the algorithm.\r
494\r
495 // Prepare the data destination address\r
496 Data = (UINT32 *)TargetAddress;\r
497\r
498 // Check the availability of the buffer\r
499 do {\r
500 // Issue the Buffered Program Setup command\r
501 SEND_NOR_COMMAND(TargetAddress, 0, P30_CMD_BUFFERED_PROGRAM_SETUP);\r
502\r
503 // Read back the status register bit#7 from the same address\r
504 if (((*Data) & P30_SR_BIT_WRITE) == P30_SR_BIT_WRITE) {\r
505 BufferAvailable = TRUE;\r
506 }\r
507\r
508 // Update the loop counter\r
509 WaitForBuffer--;\r
510\r
511 } while ((WaitForBuffer > 0) && (BufferAvailable == FALSE));\r
512\r
513 // The buffer was not available for writing\r
514 if (WaitForBuffer == 0) {\r
515 Status = EFI_DEVICE_ERROR;\r
516 goto EXIT;\r
517 }\r
518\r
519 // From now on we work in 32-bit words\r
520 BufferSizeInWords = BufferSizeInBytes / (UINTN)4;\r
521\r
522 // Write the word count, which is (buffer_size_in_words - 1),\r
523 // because word count 0 means one word.\r
524 SEND_NOR_COMMAND(TargetAddress, 0, (BufferSizeInWords - 1));\r
525\r
526 // Write the data to the NOR Flash, advancing each address by 4 bytes\r
527 for(Count=0; Count < BufferSizeInWords; Count++, Data++, Buffer++) {\r
2efbf710 528 MmioWrite32 ((UINTN)Data, *Buffer);\r
1e57a462 529 }\r
530\r
531 // Issue the Buffered Program Confirm command, to start the programming operation\r
532 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_BUFFERED_PROGRAM_CONFIRM);\r
533\r
534 // Wait for the write to complete and then check for any errors; i.e. check the Status Register\r
535 do {\r
536 StatusRegister = NorFlashReadStatusRegister (Instance, TargetAddress);\r
537 // The chip is busy while the WRITE bit is not asserted\r
538 } while ((StatusRegister & P30_SR_BIT_WRITE) != P30_SR_BIT_WRITE);\r
539\r
540\r
541 // Perform a full status check:\r
542 // Mask the relevant bits of Status Register.\r
543 // Everything should be zero, if not, we have a problem\r
544\r
545 Status = EFI_SUCCESS;\r
546\r
547 if (StatusRegister & P30_SR_BIT_VPP) {\r
548 DEBUG((EFI_D_ERROR,"NorFlashWriteBuffer(TargetAddress:0x%X): VPP Range Error\n", TargetAddress));\r
549 Status = EFI_DEVICE_ERROR;\r
550 }\r
551\r
552 if (StatusRegister & P30_SR_BIT_PROGRAM) {\r
553 DEBUG((EFI_D_ERROR,"NorFlashWriteBuffer(TargetAddress:0x%X): Program Error\n", TargetAddress));\r
554 Status = EFI_DEVICE_ERROR;\r
555 }\r
556\r
557 if (StatusRegister & P30_SR_BIT_BLOCK_LOCKED) {\r
558 DEBUG((EFI_D_ERROR,"NorFlashWriteBuffer(TargetAddress:0x%X): Device Protect Error\n",TargetAddress));\r
559 Status = EFI_DEVICE_ERROR;\r
560 }\r
561\r
562 if (!EFI_ERROR(Status)) {\r
563 // Clear the Status Register\r
564 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_CLEAR_STATUS_REGISTER);\r
565 }\r
566\r
567EXIT:\r
568 // Put device back into Read Array mode\r
569 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
570\r
571 return Status;\r
572}\r
573\r
452a9ee1 574STATIC\r
1e57a462 575EFI_STATUS\r
452a9ee1 576NorFlashWriteFullBlock (\r
1e57a462 577 IN NOR_FLASH_INSTANCE *Instance,\r
578 IN EFI_LBA Lba,\r
579 IN UINT32 *DataBuffer,\r
580 IN UINT32 BlockSizeInWords\r
581 )\r
582{\r
583 EFI_STATUS Status;\r
584 UINTN WordAddress;\r
585 UINT32 WordIndex;\r
586 UINTN BufferIndex;\r
587 UINTN BlockAddress;\r
588 UINTN BuffersInBlock;\r
589 UINTN RemainingWords;\r
590 EFI_TPL OriginalTPL;\r
518c243d 591 UINTN Cnt;\r
1e57a462 592\r
593 Status = EFI_SUCCESS;\r
594\r
595 // Get the physical address of the block\r
596 BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSizeInWords * 4);\r
597\r
598 // Start writing from the first address at the start of the block\r
599 WordAddress = BlockAddress;\r
600\r
2dff0c1a
OM
601 if (!EfiAtRuntime ()) {\r
602 // Raise TPL to TPL_HIGH to stop anyone from interrupting us.\r
603 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
604 } else {\r
605 // This initialization is only to prevent the compiler to complain about the\r
606 // use of uninitialized variables\r
607 OriginalTPL = TPL_HIGH_LEVEL;\r
608 }\r
1e57a462 609\r
610 Status = NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress);\r
611 if (EFI_ERROR(Status)) {\r
612 DEBUG((EFI_D_ERROR, "WriteSingleBlock: ERROR - Failed to Unlock and Erase the single block at 0x%X\n", BlockAddress));\r
613 goto EXIT;\r
614 }\r
615\r
616 // To speed up the programming operation, NOR Flash is programmed using the Buffered Programming method.\r
617\r
618 // Check that the address starts at a 32-word boundary, i.e. last 7 bits must be zero\r
619 if ((WordAddress & BOUNDARY_OF_32_WORDS) == 0x00) {\r
620\r
621 // First, break the entire block into buffer-sized chunks.\r
82745213 622 BuffersInBlock = (UINTN)(BlockSizeInWords * 4) / P30_MAX_BUFFER_SIZE_IN_BYTES;\r
1e57a462 623\r
624 // Then feed each buffer chunk to the NOR Flash\r
518c243d 625 // If a buffer does not contain any data, don't write it.\r
1e57a462 626 for(BufferIndex=0;\r
627 BufferIndex < BuffersInBlock;\r
628 BufferIndex++, WordAddress += P30_MAX_BUFFER_SIZE_IN_BYTES, DataBuffer += P30_MAX_BUFFER_SIZE_IN_WORDS\r
629 ) {\r
518c243d
HL
630 // Check the buffer to see if it contains any data (not set all 1s).\r
631 for (Cnt = 0; Cnt < P30_MAX_BUFFER_SIZE_IN_WORDS; Cnt++) {\r
632 if (~DataBuffer[Cnt] != 0 ) {\r
633 // Some data found, write the buffer.\r
634 Status = NorFlashWriteBuffer (Instance, WordAddress, P30_MAX_BUFFER_SIZE_IN_BYTES,\r
635 DataBuffer);\r
636 if (EFI_ERROR(Status)) {\r
637 goto EXIT;\r
638 }\r
639 break;\r
640 }\r
1e57a462 641 }\r
642 }\r
643\r
644 // Finally, finish off any remaining words that are less than the maximum size of the buffer\r
645 RemainingWords = BlockSizeInWords % P30_MAX_BUFFER_SIZE_IN_WORDS;\r
646\r
647 if(RemainingWords != 0) {\r
648 Status = NorFlashWriteBuffer (Instance, WordAddress, (RemainingWords * 4), DataBuffer);\r
649 if (EFI_ERROR(Status)) {\r
650 goto EXIT;\r
651 }\r
652 }\r
653\r
654 } else {\r
655 // For now, use the single word programming algorithm\r
656 // It is unlikely that the NOR Flash will exist in an address which falls within a 32 word boundary range,\r
657 // i.e. which ends in the range 0x......01 - 0x......7F.\r
658 for(WordIndex=0; WordIndex<BlockSizeInWords; WordIndex++, DataBuffer++, WordAddress = WordAddress + 4) {\r
659 Status = NorFlashWriteSingleWord (Instance, WordAddress, *DataBuffer);\r
660 if (EFI_ERROR(Status)) {\r
661 goto EXIT;\r
662 }\r
663 }\r
664 }\r
665\r
666EXIT:\r
2dff0c1a
OM
667 if (!EfiAtRuntime ()) {\r
668 // Interruptions can resume.\r
669 gBS->RestoreTPL (OriginalTPL);\r
670 }\r
1e57a462 671\r
672 if (EFI_ERROR(Status)) {\r
673 DEBUG((EFI_D_ERROR, "NOR FLASH Programming [WriteSingleBlock] failed at address 0x%08x. Exit Status = \"%r\".\n", WordAddress, Status));\r
674 }\r
675 return Status;\r
676}\r
677\r
678\r
679EFI_STATUS\r
680NorFlashWriteBlocks (\r
681 IN NOR_FLASH_INSTANCE *Instance,\r
682 IN EFI_LBA Lba,\r
683 IN UINTN BufferSizeInBytes,\r
684 IN VOID *Buffer\r
685 )\r
686{\r
687 UINT32 *pWriteBuffer;\r
688 EFI_STATUS Status = EFI_SUCCESS;\r
689 EFI_LBA CurrentBlock;\r
690 UINT32 BlockSizeInWords;\r
691 UINT32 NumBlocks;\r
692 UINT32 BlockCount;\r
693\r
694 // The buffer must be valid\r
695 if (Buffer == NULL) {\r
696 return EFI_INVALID_PARAMETER;\r
697 }\r
698\r
699 if(Instance->Media.ReadOnly == TRUE) {\r
700 return EFI_WRITE_PROTECTED;\r
701 }\r
702\r
703 // We must have some bytes to read\r
704 DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: BufferSizeInBytes=0x%x\n", BufferSizeInBytes));\r
705 if(BufferSizeInBytes == 0) {\r
706 return EFI_BAD_BUFFER_SIZE;\r
707 }\r
708\r
709 // The size of the buffer must be a multiple of the block size\r
710 DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: BlockSize in bytes =0x%x\n", Instance->Media.BlockSize));\r
711 if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) {\r
712 return EFI_BAD_BUFFER_SIZE;\r
713 }\r
714\r
715 // All blocks must be within the device\r
716 NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ;\r
717\r
718 DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: NumBlocks=%d, LastBlock=%ld, Lba=%ld.\n", NumBlocks, Instance->Media.LastBlock, Lba));\r
719\r
720 if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) {\r
721 DEBUG((EFI_D_ERROR, "NorFlashWriteBlocks: ERROR - Write will exceed last block.\n"));\r
722 return EFI_INVALID_PARAMETER;\r
723 }\r
724\r
725 BlockSizeInWords = Instance->Media.BlockSize / 4;\r
726\r
727 // Because the target *Buffer is a pointer to VOID, we must put all the data into a pointer\r
728 // to a proper data type, so use *ReadBuffer\r
729 pWriteBuffer = (UINT32 *)Buffer;\r
730\r
731 CurrentBlock = Lba;\r
732 for (BlockCount=0; BlockCount < NumBlocks; BlockCount++, CurrentBlock++, pWriteBuffer = pWriteBuffer + BlockSizeInWords) {\r
733\r
734 DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: Writing block #%d\n", (UINTN)CurrentBlock));\r
735\r
452a9ee1 736 Status = NorFlashWriteFullBlock (Instance, CurrentBlock, pWriteBuffer, BlockSizeInWords);\r
1e57a462 737\r
738 if (EFI_ERROR(Status)) {\r
739 break;\r
740 }\r
741 }\r
742\r
743 DEBUG((DEBUG_BLKIO, "NorFlashWriteBlocks: Exit Status = \"%r\".\n", Status));\r
744 return Status;\r
745}\r
746\r
747EFI_STATUS\r
748NorFlashReadBlocks (\r
749 IN NOR_FLASH_INSTANCE *Instance,\r
750 IN EFI_LBA Lba,\r
751 IN UINTN BufferSizeInBytes,\r
752 OUT VOID *Buffer\r
753 )\r
754{\r
755 UINT32 NumBlocks;\r
756 UINTN StartAddress;\r
757\r
3d783074 758 DEBUG((DEBUG_BLKIO, "NorFlashReadBlocks: BufferSize=0x%xB BlockSize=0x%xB LastBlock=%ld, Lba=%ld.\n",\r
759 BufferSizeInBytes, Instance->Media.BlockSize, Instance->Media.LastBlock, Lba));\r
760\r
1e57a462 761 // The buffer must be valid\r
762 if (Buffer == NULL) {\r
763 return EFI_INVALID_PARAMETER;\r
764 }\r
765\r
452a9ee1 766 // Return if we have not any byte to read\r
3d783074 767 if (BufferSizeInBytes == 0) {\r
768 return EFI_SUCCESS;\r
1e57a462 769 }\r
770\r
771 // The size of the buffer must be a multiple of the block size\r
1e57a462 772 if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) {\r
773 return EFI_BAD_BUFFER_SIZE;\r
774 }\r
775\r
776 // All blocks must be within the device\r
777 NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize ;\r
778\r
1e57a462 779 if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) {\r
780 DEBUG((EFI_D_ERROR, "NorFlashReadBlocks: ERROR - Read will exceed last block\n"));\r
781 return EFI_INVALID_PARAMETER;\r
782 }\r
783\r
784 // Get the address to start reading from\r
785 StartAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress,\r
786 Lba,\r
787 Instance->Media.BlockSize\r
788 );\r
789\r
790 // Put the device into Read Array mode\r
791 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
792\r
793 // Readout the data\r
794 CopyMem(Buffer, (UINTN *)StartAddress, BufferSizeInBytes);\r
795\r
796 return EFI_SUCCESS;\r
797}\r
798\r
518c243d
HL
799EFI_STATUS\r
800NorFlashRead (\r
801 IN NOR_FLASH_INSTANCE *Instance,\r
802 IN EFI_LBA Lba,\r
803 IN UINTN Offset,\r
804 IN UINTN BufferSizeInBytes,\r
805 OUT VOID *Buffer\r
806 )\r
807{\r
ac83357a 808 UINTN StartAddress;\r
518c243d
HL
809\r
810 // The buffer must be valid\r
811 if (Buffer == NULL) {\r
812 return EFI_INVALID_PARAMETER;\r
813 }\r
814\r
815 // Return if we have not any byte to read\r
816 if (BufferSizeInBytes == 0) {\r
817 return EFI_SUCCESS;\r
818 }\r
819\r
ac83357a 820 if (((Lba * Instance->Media.BlockSize) + Offset + BufferSizeInBytes) > Instance->Size) {\r
518c243d
HL
821 DEBUG ((EFI_D_ERROR, "NorFlashRead: ERROR - Read will exceed device size.\n"));\r
822 return EFI_INVALID_PARAMETER;\r
823 }\r
824\r
825 // Get the address to start reading from\r
826 StartAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress,\r
827 Lba,\r
828 Instance->Media.BlockSize\r
829 );\r
830\r
831 // Put the device into Read Array mode\r
832 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
833\r
834 // Readout the data\r
835 CopyMem (Buffer, (UINTN *)(StartAddress + Offset), BufferSizeInBytes);\r
836\r
837 return EFI_SUCCESS;\r
838}\r
839\r
452a9ee1
BJ
840/*\r
841 Write a full or portion of a block. It must not span block boundaries; that is,\r
842 Offset + *NumBytes <= Instance->Media.BlockSize.\r
843*/\r
844EFI_STATUS\r
845NorFlashWriteSingleBlock (\r
846 IN NOR_FLASH_INSTANCE *Instance,\r
847 IN EFI_LBA Lba,\r
848 IN UINTN Offset,\r
849 IN OUT UINTN *NumBytes,\r
850 IN UINT8 *Buffer\r
851 )\r
852{\r
853 EFI_STATUS TempStatus;\r
854 UINT32 Tmp;\r
855 UINT32 TmpBuf;\r
856 UINT32 WordToWrite;\r
857 UINT32 Mask;\r
858 BOOLEAN DoErase;\r
859 UINTN BytesToWrite;\r
860 UINTN CurOffset;\r
861 UINTN WordAddr;\r
862 UINTN BlockSize;\r
863 UINTN BlockAddress;\r
864 UINTN PrevBlockAddress;\r
865\r
866 PrevBlockAddress = 0;\r
867\r
868 if (!Instance->Initialized && Instance->Initialize) {\r
869 Instance->Initialize(Instance);\r
870 }\r
871\r
36d66acf 872 DEBUG ((DEBUG_BLKIO, "NorFlashWriteSingleBlock(Parameters: Lba=%ld, Offset=0x%x, *NumBytes=0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer));\r
452a9ee1
BJ
873\r
874 // Detect WriteDisabled state\r
875 if (Instance->Media.ReadOnly == TRUE) {\r
876 DEBUG ((EFI_D_ERROR, "NorFlashWriteSingleBlock: ERROR - Can not write: Device is in WriteDisabled state.\n"));\r
877 // It is in WriteDisabled state, return an error right away\r
878 return EFI_ACCESS_DENIED;\r
879 }\r
880\r
881 // Cache the block size to avoid de-referencing pointers all the time\r
882 BlockSize = Instance->Media.BlockSize;\r
883\r
884 // The write must not span block boundaries.\r
885 // We need to check each variable individually because adding two large values together overflows.\r
886 if ( ( Offset >= BlockSize ) ||\r
887 ( *NumBytes > BlockSize ) ||\r
888 ( (Offset + *NumBytes) > BlockSize ) ) {\r
889 DEBUG ((EFI_D_ERROR, "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r
890 return EFI_BAD_BUFFER_SIZE;\r
891 }\r
892\r
893 // We must have some bytes to write\r
894 if (*NumBytes == 0) {\r
895 DEBUG ((EFI_D_ERROR, "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", Offset, *NumBytes, BlockSize ));\r
896 return EFI_BAD_BUFFER_SIZE;\r
897 }\r
898\r
899 // Pick 128bytes as a good start for word operations as opposed to erasing the\r
900 // block and writing the data regardless if an erase is really needed.\r
901 // It looks like most individual NV variable writes are smaller than 128bytes.\r
902 if (*NumBytes <= 128) {\r
903 // Check to see if we need to erase before programming the data into NOR.\r
904 // If the destination bits are only changing from 1s to 0s we can just write.\r
905 // After a block is erased all bits in the block is set to 1.\r
906 // If any byte requires us to erase we just give up and rewrite all of it.\r
907 DoErase = FALSE;\r
908 BytesToWrite = *NumBytes;\r
909 CurOffset = Offset;\r
910\r
911 while (BytesToWrite > 0) {\r
912 // Read full word from NOR, splice as required. A word is the smallest\r
913 // unit we can write.\r
36d66acf 914 TempStatus = NorFlashRead (Instance, Lba, CurOffset & ~(0x3), sizeof(Tmp), &Tmp);\r
452a9ee1
BJ
915 if (EFI_ERROR (TempStatus)) {\r
916 return EFI_DEVICE_ERROR;\r
917 }\r
918\r
919 // Physical address of word in NOR to write.\r
920 WordAddr = (CurOffset & ~(0x3)) + GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress,\r
921 Lba, BlockSize);\r
922 // The word of data that is to be written.\r
923 TmpBuf = *((UINT32*)(Buffer + (*NumBytes - BytesToWrite)));\r
924\r
925 // First do word aligned chunks.\r
926 if ((CurOffset & 0x3) == 0) {\r
927 if (BytesToWrite >= 4) {\r
928 // Is the destination still in 'erased' state?\r
929 if (~Tmp != 0) {\r
930 // Check to see if we are only changing bits to zero.\r
931 if ((Tmp ^ TmpBuf) & TmpBuf) {\r
932 DoErase = TRUE;\r
933 break;\r
934 }\r
935 }\r
936 // Write this word to NOR\r
937 WordToWrite = TmpBuf;\r
938 CurOffset += sizeof(TmpBuf);\r
939 BytesToWrite -= sizeof(TmpBuf);\r
940 } else {\r
941 // BytesToWrite < 4. Do small writes and left-overs\r
942 Mask = ~((~0) << (BytesToWrite * 8));\r
943 // Mask out the bytes we want.\r
944 TmpBuf &= Mask;\r
945 // Is the destination still in 'erased' state?\r
946 if ((Tmp & Mask) != Mask) {\r
947 // Check to see if we are only changing bits to zero.\r
948 if ((Tmp ^ TmpBuf) & TmpBuf) {\r
949 DoErase = TRUE;\r
950 break;\r
951 }\r
952 }\r
953 // Merge old and new data. Write merged word to NOR\r
954 WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
955 CurOffset += BytesToWrite;\r
956 BytesToWrite = 0;\r
957 }\r
958 } else {\r
959 // Do multiple words, but starting unaligned.\r
960 if (BytesToWrite > (4 - (CurOffset & 0x3))) {\r
961 Mask = ((~0) << ((CurOffset & 0x3) * 8));\r
962 // Mask out the bytes we want.\r
963 TmpBuf &= Mask;\r
964 // Is the destination still in 'erased' state?\r
965 if ((Tmp & Mask) != Mask) {\r
966 // Check to see if we are only changing bits to zero.\r
967 if ((Tmp ^ TmpBuf) & TmpBuf) {\r
968 DoErase = TRUE;\r
969 break;\r
970 }\r
971 }\r
972 // Merge old and new data. Write merged word to NOR\r
973 WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
974 BytesToWrite -= (4 - (CurOffset & 0x3));\r
975 CurOffset += (4 - (CurOffset & 0x3));\r
976 } else {\r
977 // Unaligned and fits in one word.\r
978 Mask = (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) * 8);\r
979 // Mask out the bytes we want.\r
980 TmpBuf = (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask;\r
981 // Is the destination still in 'erased' state?\r
982 if ((Tmp & Mask) != Mask) {\r
983 // Check to see if we are only changing bits to zero.\r
984 if ((Tmp ^ TmpBuf) & TmpBuf) {\r
985 DoErase = TRUE;\r
986 break;\r
987 }\r
988 }\r
989 // Merge old and new data. Write merged word to NOR\r
990 WordToWrite = (Tmp & ~Mask) | TmpBuf;\r
991 CurOffset += BytesToWrite;\r
992 BytesToWrite = 0;\r
993 }\r
994 }\r
995\r
996 //\r
997 // Write the word to NOR.\r
998 //\r
999\r
1000 BlockAddress = GET_NOR_BLOCK_ADDRESS (Instance->RegionBaseAddress, Lba, BlockSize);\r
1001 if (BlockAddress != PrevBlockAddress) {\r
1002 TempStatus = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);\r
1003 if (EFI_ERROR (TempStatus)) {\r
1004 return EFI_DEVICE_ERROR;\r
1005 }\r
1006 PrevBlockAddress = BlockAddress;\r
1007 }\r
1008 TempStatus = NorFlashWriteSingleWord (Instance, WordAddr, WordToWrite);\r
1009 if (EFI_ERROR (TempStatus)) {\r
1010 return EFI_DEVICE_ERROR;\r
1011 }\r
1012 }\r
1013 // Exit if we got here and could write all the data. Otherwise do the\r
1014 // Erase-Write cycle.\r
1015 if (!DoErase) {\r
1016 return EFI_SUCCESS;\r
1017 }\r
1018 }\r
1019\r
1020 // Check we did get some memory. Buffer is BlockSize.\r
1021 if (Instance->ShadowBuffer == NULL) {\r
1022 DEBUG ((EFI_D_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));\r
1023 return EFI_DEVICE_ERROR;\r
1024 }\r
1025\r
1026 // Read NOR Flash data into shadow buffer\r
36d66acf 1027 TempStatus = NorFlashReadBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer);\r
452a9ee1
BJ
1028 if (EFI_ERROR (TempStatus)) {\r
1029 // Return one of the pre-approved error statuses\r
1030 return EFI_DEVICE_ERROR;\r
1031 }\r
1032\r
1033 // Put the data at the appropriate location inside the buffer area\r
1034 CopyMem ((VOID*)((UINTN)Instance->ShadowBuffer + Offset), Buffer, *NumBytes);\r
1035\r
1036 // Write the modified buffer back to the NorFlash\r
36d66acf 1037 TempStatus = NorFlashWriteBlocks (Instance, Lba, BlockSize, Instance->ShadowBuffer);\r
452a9ee1
BJ
1038 if (EFI_ERROR (TempStatus)) {\r
1039 // Return one of the pre-approved error statuses\r
1040 return EFI_DEVICE_ERROR;\r
1041 }\r
1042\r
1043 return EFI_SUCCESS;\r
1044}\r
1045\r
1046/*\r
1047 Although DiskIoDxe will automatically install the DiskIO protocol whenever\r
1048 we install the BlockIO protocol, its implementation is sub-optimal as it reads\r
1049 and writes entire blocks using the BlockIO protocol. In fact we can access\r
1050 NOR flash with a finer granularity than that, so we can improve performance\r
1051 by directly producing the DiskIO protocol.\r
1052*/\r
1053\r
1054/**\r
1055 Read BufferSize bytes from Offset into Buffer.\r
1056\r
1057 @param This Protocol instance pointer.\r
1058 @param MediaId Id of the media, changes every time the media is replaced.\r
1059 @param Offset The starting byte offset to read from\r
1060 @param BufferSize Size of Buffer\r
1061 @param Buffer Buffer containing read data\r
1062\r
1063 @retval EFI_SUCCESS The data was read correctly from the device.\r
1064 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.\r
1065 @retval EFI_NO_MEDIA There is no media in the device.\r
1066 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
1067 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not\r
1068 valid for the device.\r
1069\r
1070**/\r
1071EFI_STATUS\r
1072EFIAPI\r
1073NorFlashDiskIoReadDisk (\r
1074 IN EFI_DISK_IO_PROTOCOL *This,\r
1075 IN UINT32 MediaId,\r
1076 IN UINT64 DiskOffset,\r
1077 IN UINTN BufferSize,\r
1078 OUT VOID *Buffer\r
1079 )\r
1080{\r
1081 NOR_FLASH_INSTANCE *Instance;\r
1082 UINT32 BlockSize;\r
1083 UINT32 BlockOffset;\r
1084 EFI_LBA Lba;\r
1085\r
1086 Instance = INSTANCE_FROM_DISKIO_THIS(This);\r
1087\r
1088 if (MediaId != Instance->Media.MediaId) {\r
1089 return EFI_MEDIA_CHANGED;\r
1090 }\r
1091\r
1092 BlockSize = Instance->Media.BlockSize;\r
1093 Lba = (EFI_LBA) DivU64x32Remainder (DiskOffset, BlockSize, &BlockOffset);\r
1094\r
1095 return NorFlashRead (Instance, Lba, BlockOffset, BufferSize, Buffer);\r
1096}\r
1097\r
1098/**\r
1099 Writes a specified number of bytes to a device.\r
1100\r
1101 @param This Indicates a pointer to the calling context.\r
1102 @param MediaId ID of the medium to be written.\r
1103 @param Offset The starting byte offset on the logical block I/O device to write.\r
1104 @param BufferSize The size in bytes of Buffer. The number of bytes to write to the device.\r
1105 @param Buffer A pointer to the buffer containing the data to be written.\r
1106\r
1107 @retval EFI_SUCCESS The data was written correctly to the device.\r
1108 @retval EFI_WRITE_PROTECTED The device can not be written to.\r
1109 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r
1110 @retval EFI_NO_MEDIA There is no media in the device.\r
1111 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
1112 @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not\r
1113 valid for the device.\r
1114\r
1115**/\r
1116EFI_STATUS\r
1117EFIAPI\r
1118NorFlashDiskIoWriteDisk (\r
1119 IN EFI_DISK_IO_PROTOCOL *This,\r
1120 IN UINT32 MediaId,\r
1121 IN UINT64 DiskOffset,\r
1122 IN UINTN BufferSize,\r
1123 IN VOID *Buffer\r
1124 )\r
1125{\r
1126 NOR_FLASH_INSTANCE *Instance;\r
1127 UINT32 BlockSize;\r
1128 UINT32 BlockOffset;\r
1129 EFI_LBA Lba;\r
1130 UINTN RemainingBytes;\r
1131 UINTN WriteSize;\r
1132 EFI_STATUS Status;\r
1133\r
1134 Instance = INSTANCE_FROM_DISKIO_THIS(This);\r
1135\r
1136 if (MediaId != Instance->Media.MediaId) {\r
1137 return EFI_MEDIA_CHANGED;\r
1138 }\r
1139\r
1140 BlockSize = Instance->Media.BlockSize;\r
1141 Lba = (EFI_LBA) DivU64x32Remainder (DiskOffset, BlockSize, &BlockOffset);\r
1142\r
1143 RemainingBytes = BufferSize;\r
1144\r
1145 // Write either all the remaining bytes, or the number of bytes that bring\r
1146 // us up to a block boundary, whichever is less.\r
1147 // (DiskOffset | (BlockSize - 1)) + 1) rounds DiskOffset up to the next\r
1148 // block boundary (even if it is already on one).\r
1149 WriteSize = MIN (RemainingBytes, ((DiskOffset | (BlockSize - 1)) + 1) - DiskOffset);\r
1150\r
1151 do {\r
1152 if (WriteSize == BlockSize) {\r
1153 // Write a full block\r
1154 Status = NorFlashWriteFullBlock (Instance, Lba, Buffer, BlockSize / sizeof (UINT32));\r
1155 } else {\r
1156 // Write a partial block\r
1157 Status = NorFlashWriteSingleBlock (Instance, Lba, BlockOffset, &WriteSize, Buffer);\r
1158 }\r
1159 if (EFI_ERROR (Status)) {\r
1160 return Status;\r
1161 }\r
1162 // Now continue writing either all the remaining bytes or single blocks.\r
1163 RemainingBytes -= WriteSize;\r
1164 Buffer = (UINT8 *) Buffer + WriteSize;\r
1165 Lba++;\r
1166 BlockOffset = 0;\r
1167 WriteSize = MIN (RemainingBytes, BlockSize);\r
1168 } while (RemainingBytes);\r
1169\r
1170 return Status;\r
1171}\r
518c243d 1172\r
1e57a462 1173EFI_STATUS\r
1174NorFlashReset (\r
1175 IN NOR_FLASH_INSTANCE *Instance\r
1176 )\r
1177{\r
1178 // As there is no specific RESET to perform, ensure that the devices is in the default Read Array mode\r
1179 SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
1180 return EFI_SUCCESS;\r
1181}\r
1182\r
1dbbfc17
OM
1183/**\r
1184 Fixup internal data so that EFI can be call in virtual mode.\r
1185 Call the passed in Child Notify event and convert any pointers in\r
1186 lib to virtual mode.\r
1187\r
1188 @param[in] Event The Event that is being processed\r
1189 @param[in] Context Event Context\r
1190**/\r
1191VOID\r
1192EFIAPI\r
1193NorFlashVirtualNotifyEvent (\r
1194 IN EFI_EVENT Event,\r
1195 IN VOID *Context\r
1196 )\r
1197{\r
1198 UINTN Index;\r
1199\r
1200 for (Index = 0; Index < mNorFlashDeviceCount; Index++) {\r
1201 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->DeviceBaseAddress);\r
1202 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->RegionBaseAddress);\r
1203\r
1204 // Convert BlockIo protocol\r
1205 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->BlockIoProtocol.FlushBlocks);\r
1206 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->BlockIoProtocol.ReadBlocks);\r
1207 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->BlockIoProtocol.Reset);\r
1208 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->BlockIoProtocol.WriteBlocks);\r
1209\r
1210 // Convert Fvb\r
1211 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->FvbProtocol.EraseBlocks);\r
1212 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->FvbProtocol.GetAttributes);\r
1213 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->FvbProtocol.GetBlockSize);\r
1214 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->FvbProtocol.GetPhysicalAddress);\r
1215 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->FvbProtocol.Read);\r
1216 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->FvbProtocol.SetAttributes);\r
1217 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->FvbProtocol.Write);\r
1218\r
452a9ee1
BJ
1219 if (mNorFlashInstances[Index]->ShadowBuffer != NULL) {\r
1220 EfiConvertPointer (0x0, (VOID**)&mNorFlashInstances[Index]->ShadowBuffer);\r
1dbbfc17
OM
1221 }\r
1222 }\r
1223\r
1224 return;\r
1225}\r
1226\r
1e57a462 1227EFI_STATUS\r
1228EFIAPI\r
1229NorFlashInitialise (\r
1230 IN EFI_HANDLE ImageHandle,\r
1231 IN EFI_SYSTEM_TABLE *SystemTable\r
1232 )\r
1233{\r
1234 EFI_STATUS Status;\r
1235 UINT32 Index;\r
1236 NOR_FLASH_DESCRIPTION* NorFlashDevices;\r
1e57a462 1237 BOOLEAN ContainVariableStorage;\r
1238\r
1239 Status = NorFlashPlatformInitialization ();\r
1240 if (EFI_ERROR(Status)) {\r
1241 DEBUG((EFI_D_ERROR,"NorFlashInitialise: Fail to initialize Nor Flash devices\n"));\r
1242 return Status;\r
1243 }\r
1244\r
1dbbfc17 1245 Status = NorFlashPlatformGetDevices (&NorFlashDevices, &mNorFlashDeviceCount);\r
1e57a462 1246 if (EFI_ERROR(Status)) {\r
1247 DEBUG((EFI_D_ERROR,"NorFlashInitialise: Fail to get Nor Flash devices\n"));\r
1248 return Status;\r
1249 }\r
1250\r
1dbbfc17 1251 mNorFlashInstances = AllocateRuntimePool (sizeof(NOR_FLASH_INSTANCE*) * mNorFlashDeviceCount);\r
1e57a462 1252\r
1dbbfc17 1253 for (Index = 0; Index < mNorFlashDeviceCount; Index++) {\r
1e57a462 1254 // Check if this NOR Flash device contain the variable storage region\r
1255 ContainVariableStorage =\r
1256 (NorFlashDevices[Index].RegionBaseAddress <= PcdGet32 (PcdFlashNvStorageVariableBase)) &&\r
1257 (PcdGet32 (PcdFlashNvStorageVariableBase) + PcdGet32 (PcdFlashNvStorageVariableSize) <= NorFlashDevices[Index].RegionBaseAddress + NorFlashDevices[Index].Size);\r
1258\r
1259 Status = NorFlashCreateInstance (\r
1260 NorFlashDevices[Index].DeviceBaseAddress,\r
1261 NorFlashDevices[Index].RegionBaseAddress,\r
1262 NorFlashDevices[Index].Size,\r
1263 Index,\r
1264 NorFlashDevices[Index].BlockSize,\r
1265 ContainVariableStorage,\r
1266 &NorFlashDevices[Index].Guid,\r
1267 &mNorFlashInstances[Index]\r
1268 );\r
1269 if (EFI_ERROR(Status)) {\r
1270 DEBUG((EFI_D_ERROR,"NorFlashInitialise: Fail to create instance for NorFlash[%d]\n",Index));\r
1271 }\r
1272 }\r
1273\r
1dbbfc17
OM
1274 //\r
1275 // Register for the virtual address change event\r
1276 //\r
1277 Status = gBS->CreateEventEx (\r
1278 EVT_NOTIFY_SIGNAL,\r
1279 TPL_NOTIFY,\r
1280 NorFlashVirtualNotifyEvent,\r
1281 NULL,\r
1282 &gEfiEventVirtualAddressChangeGuid,\r
1283 &mNorFlashVirtualAddrChangeEvent\r
1284 );\r
1285 ASSERT_EFI_ERROR (Status);\r
1286\r
1e57a462 1287 return Status;\r
1288}\r