Flash.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /** @file
  2. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "Flash.h"
  6. NAND_PART_INFO_TABLE gNandPartInfoTable[1] = {
  7. { 0x2C, 0xBA, 17, 11 }
  8. };
  9. NAND_FLASH_INFO *gNandFlashInfo = NULL;
  10. UINT8 *gEccCode;
  11. UINTN gNum512BytesChunks = 0;
  12. //
  13. // Device path for SemiHosting. It contains our autogened Caller ID GUID.
  14. //
  15. typedef struct {
  16. VENDOR_DEVICE_PATH Guid;
  17. EFI_DEVICE_PATH_PROTOCOL End;
  18. } FLASH_DEVICE_PATH;
  19. FLASH_DEVICE_PATH gDevicePath = {
  20. {
  21. { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0 } },
  22. EFI_CALLER_ID_GUID
  23. },
  24. { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0} }
  25. };
  26. //Actual page address = Column address + Page address + Block address.
  27. UINTN
  28. GetActualPageAddressInBytes (
  29. UINTN BlockIndex,
  30. UINTN PageIndex
  31. )
  32. {
  33. //BlockAddressStart = Start of the Block address in actual NAND
  34. //PageAddressStart = Start of the Page address in actual NAND
  35. return ((BlockIndex << gNandFlashInfo->BlockAddressStart) + (PageIndex << gNandFlashInfo->PageAddressStart));
  36. }
  37. VOID
  38. NandSendCommand (
  39. UINT8 Command
  40. )
  41. {
  42. MmioWrite16(GPMC_NAND_COMMAND_0, Command);
  43. }
  44. VOID
  45. NandSendAddress (
  46. UINT8 Address
  47. )
  48. {
  49. MmioWrite16(GPMC_NAND_ADDRESS_0, Address);
  50. }
  51. UINT16
  52. NandReadStatus (
  53. VOID
  54. )
  55. {
  56. //Send READ STATUS command
  57. NandSendCommand(READ_STATUS_CMD);
  58. //Read status.
  59. return MmioRead16(GPMC_NAND_DATA_0);
  60. }
  61. VOID
  62. NandSendAddressCycles (
  63. UINTN Address
  64. )
  65. {
  66. //Column address
  67. NandSendAddress(Address & 0xff);
  68. Address >>= 8;
  69. //Column address
  70. NandSendAddress(Address & 0x07);
  71. Address >>= 3;
  72. //Page and Block address
  73. NandSendAddress(Address & 0xff);
  74. Address >>= 8;
  75. //Block address
  76. NandSendAddress(Address & 0xff);
  77. Address >>= 8;
  78. //Block address
  79. NandSendAddress(Address & 0x01);
  80. }
  81. VOID
  82. GpmcInit (
  83. VOID
  84. )
  85. {
  86. //Enable Smart-idle mode.
  87. MmioWrite32 (GPMC_SYSCONFIG, SMARTIDLEMODE);
  88. //Set IRQSTATUS and IRQENABLE to the reset value
  89. MmioWrite32 (GPMC_IRQSTATUS, 0x0);
  90. MmioWrite32 (GPMC_IRQENABLE, 0x0);
  91. //Disable GPMC timeout control.
  92. MmioWrite32 (GPMC_TIMEOUT_CONTROL, TIMEOUTDISABLE);
  93. //Set WRITEPROTECT bit to enable write access.
  94. MmioWrite32 (GPMC_CONFIG, WRITEPROTECT_HIGH);
  95. //NOTE: Following GPMC_CONFIGi_0 register settings are taken from u-boot memory dump.
  96. MmioWrite32 (GPMC_CONFIG1_0, DEVICETYPE_NAND | DEVICESIZE_X16);
  97. MmioWrite32 (GPMC_CONFIG2_0, CSRDOFFTIME | CSWROFFTIME);
  98. MmioWrite32 (GPMC_CONFIG3_0, ADVRDOFFTIME | ADVWROFFTIME);
  99. MmioWrite32 (GPMC_CONFIG4_0, OEONTIME | OEOFFTIME | WEONTIME | WEOFFTIME);
  100. MmioWrite32 (GPMC_CONFIG5_0, RDCYCLETIME | WRCYCLETIME | RDACCESSTIME | PAGEBURSTACCESSTIME);
  101. MmioWrite32 (GPMC_CONFIG6_0, WRACCESSTIME | WRDATAONADMUXBUS | CYCLE2CYCLEDELAY | CYCLE2CYCLESAMECSEN);
  102. MmioWrite32 (GPMC_CONFIG7_0, MASKADDRESS_128MB | CSVALID | BASEADDRESS);
  103. }
  104. EFI_STATUS
  105. NandDetectPart (
  106. VOID
  107. )
  108. {
  109. UINT8 NandInfo = 0;
  110. UINT8 PartInfo[5];
  111. UINTN Index;
  112. BOOLEAN Found = FALSE;
  113. //Send READ ID command
  114. NandSendCommand(READ_ID_CMD);
  115. //Send one address cycle.
  116. NandSendAddress(0);
  117. //Read 5-bytes to idenfity code programmed into the NAND flash devices.
  118. //BYTE 0 = Manufacture ID
  119. //Byte 1 = Device ID
  120. //Byte 2, 3, 4 = Nand part specific information (Page size, Block size etc)
  121. for (Index = 0; Index < sizeof(PartInfo); Index++) {
  122. PartInfo[Index] = MmioRead16(GPMC_NAND_DATA_0);
  123. }
  124. //Check if the ManufactureId and DeviceId are part of the currently supported nand parts.
  125. for (Index = 0; Index < sizeof(gNandPartInfoTable)/sizeof(NAND_PART_INFO_TABLE); Index++) {
  126. if (gNandPartInfoTable[Index].ManufactureId == PartInfo[0] && gNandPartInfoTable[Index].DeviceId == PartInfo[1]) {
  127. gNandFlashInfo->BlockAddressStart = gNandPartInfoTable[Index].BlockAddressStart;
  128. gNandFlashInfo->PageAddressStart = gNandPartInfoTable[Index].PageAddressStart;
  129. Found = TRUE;
  130. break;
  131. }
  132. }
  133. if (Found == FALSE) {
  134. DEBUG ((EFI_D_ERROR, "Nand part is not currently supported. Manufacture id: %x, Device id: %x\n", PartInfo[0], PartInfo[1]));
  135. return EFI_NOT_FOUND;
  136. }
  137. //Populate NAND_FLASH_INFO based on the result of READ ID command.
  138. gNandFlashInfo->ManufactureId = PartInfo[0];
  139. gNandFlashInfo->DeviceId = PartInfo[1];
  140. NandInfo = PartInfo[3];
  141. if (PAGE_SIZE(NandInfo) == PAGE_SIZE_2K_VAL) {
  142. gNandFlashInfo->PageSize = PAGE_SIZE_2K;
  143. } else {
  144. DEBUG ((EFI_D_ERROR, "Unknown Page size.\n"));
  145. return EFI_DEVICE_ERROR;
  146. }
  147. if (SPARE_AREA_SIZE(NandInfo) == SPARE_AREA_SIZE_64B_VAL) {
  148. gNandFlashInfo->SparePageSize = SPARE_AREA_SIZE_64B;
  149. } else {
  150. DEBUG ((EFI_D_ERROR, "Unknown Spare area size.\n"));
  151. return EFI_DEVICE_ERROR;
  152. }
  153. if (BLOCK_SIZE(NandInfo) == BLOCK_SIZE_128K_VAL) {
  154. gNandFlashInfo->BlockSize = BLOCK_SIZE_128K;
  155. } else {
  156. DEBUG ((EFI_D_ERROR, "Unknown Block size.\n"));
  157. return EFI_DEVICE_ERROR;
  158. }
  159. if (ORGANIZATION(NandInfo) == ORGANIZATION_X8) {
  160. gNandFlashInfo->Organization = 0;
  161. } else if (ORGANIZATION(NandInfo) == ORGANIZATION_X16) {
  162. gNandFlashInfo->Organization = 1;
  163. }
  164. //Calculate total number of blocks.
  165. gNandFlashInfo->NumPagesPerBlock = DivU64x32(gNandFlashInfo->BlockSize, gNandFlashInfo->PageSize);
  166. return EFI_SUCCESS;
  167. }
  168. VOID
  169. NandConfigureEcc (
  170. VOID
  171. )
  172. {
  173. //Define ECC size 0 and size 1 to 512 bytes
  174. MmioWrite32 (GPMC_ECC_SIZE_CONFIG, (ECCSIZE0_512BYTES | ECCSIZE1_512BYTES));
  175. }
  176. VOID
  177. NandEnableEcc (
  178. VOID
  179. )
  180. {
  181. //Clear all the ECC result registers and select ECC result register 1
  182. MmioWrite32 (GPMC_ECC_CONTROL, (ECCCLEAR | ECCPOINTER_REG1));
  183. //Enable ECC engine on CS0
  184. MmioWrite32 (GPMC_ECC_CONFIG, (ECCENABLE | ECCCS_0 | ECC16B));
  185. }
  186. VOID
  187. NandDisableEcc (
  188. VOID
  189. )
  190. {
  191. //Turn off ECC engine.
  192. MmioWrite32 (GPMC_ECC_CONFIG, ECCDISABLE);
  193. }
  194. VOID
  195. NandCalculateEcc (
  196. VOID
  197. )
  198. {
  199. UINTN Index;
  200. UINTN EccResultRegister;
  201. UINTN EccResult;
  202. //Capture 32-bit ECC result for each 512-bytes chunk.
  203. //In our case PageSize is 2K so read ECC1-ECC4 result registers and
  204. //generate total of 12-bytes of ECC code for the particular page.
  205. EccResultRegister = GPMC_ECC1_RESULT;
  206. for (Index = 0; Index < gNum512BytesChunks; Index++) {
  207. EccResult = MmioRead32 (EccResultRegister);
  208. //Calculate ECC code from 32-bit ECC result value.
  209. //NOTE: Following calculation is not part of TRM. We got this information
  210. //from Beagleboard mailing list.
  211. gEccCode[Index * 3] = EccResult & 0xFF;
  212. gEccCode[(Index * 3) + 1] = (EccResult >> 16) & 0xFF;
  213. gEccCode[(Index * 3) + 2] = (((EccResult >> 20) & 0xF0) | ((EccResult >> 8) & 0x0F));
  214. //Point to next ECC result register.
  215. EccResultRegister += 4;
  216. }
  217. }
  218. EFI_STATUS
  219. NandReadPage (
  220. IN UINTN BlockIndex,
  221. IN UINTN PageIndex,
  222. OUT VOID *Buffer,
  223. OUT UINT8 *SpareBuffer
  224. )
  225. {
  226. UINTN Address;
  227. UINTN Index;
  228. UINTN NumMainAreaWords = (gNandFlashInfo->PageSize/2);
  229. UINTN NumSpareAreaWords = (gNandFlashInfo->SparePageSize/2);
  230. UINT16 *MainAreaWordBuffer = Buffer;
  231. UINT16 *SpareAreaWordBuffer = (UINT16 *)SpareBuffer;
  232. UINTN Timeout = MAX_RETRY_COUNT;
  233. //Generate device address in bytes to access specific block and page index
  234. Address = GetActualPageAddressInBytes(BlockIndex, PageIndex);
  235. //Send READ command
  236. NandSendCommand(PAGE_READ_CMD);
  237. //Send 5 Address cycles to access specific device address
  238. NandSendAddressCycles(Address);
  239. //Send READ CONFIRM command
  240. NandSendCommand(PAGE_READ_CONFIRM_CMD);
  241. //Poll till device is busy.
  242. while (Timeout) {
  243. if ((NandReadStatus() & NAND_READY) == NAND_READY) {
  244. break;
  245. }
  246. Timeout--;
  247. }
  248. if (Timeout == 0) {
  249. DEBUG ((EFI_D_ERROR, "Read page timed out.\n"));
  250. return EFI_TIMEOUT;
  251. }
  252. //Reissue READ command
  253. NandSendCommand(PAGE_READ_CMD);
  254. //Enable ECC engine.
  255. NandEnableEcc();
  256. //Read data into the buffer.
  257. for (Index = 0; Index < NumMainAreaWords; Index++) {
  258. *MainAreaWordBuffer++ = MmioRead16(GPMC_NAND_DATA_0);
  259. }
  260. //Read spare area into the buffer.
  261. for (Index = 0; Index < NumSpareAreaWords; Index++) {
  262. *SpareAreaWordBuffer++ = MmioRead16(GPMC_NAND_DATA_0);
  263. }
  264. //Calculate ECC.
  265. NandCalculateEcc();
  266. //Turn off ECC engine.
  267. NandDisableEcc();
  268. //Perform ECC correction.
  269. //Need to implement..
  270. return EFI_SUCCESS;
  271. }
  272. EFI_STATUS
  273. NandWritePage (
  274. IN UINTN BlockIndex,
  275. IN UINTN PageIndex,
  276. OUT VOID *Buffer,
  277. IN UINT8 *SpareBuffer
  278. )
  279. {
  280. UINTN Address;
  281. UINT16 *MainAreaWordBuffer = Buffer;
  282. UINT16 *SpareAreaWordBuffer = (UINT16 *)SpareBuffer;
  283. UINTN Index;
  284. UINTN NandStatus;
  285. UINTN Timeout = MAX_RETRY_COUNT;
  286. //Generate device address in bytes to access specific block and page index
  287. Address = GetActualPageAddressInBytes(BlockIndex, PageIndex);
  288. //Send SERIAL DATA INPUT command
  289. NandSendCommand(PROGRAM_PAGE_CMD);
  290. //Send 5 Address cycles to access specific device address
  291. NandSendAddressCycles(Address);
  292. //Enable ECC engine.
  293. NandEnableEcc();
  294. //Data input from Buffer
  295. for (Index = 0; Index < (gNandFlashInfo->PageSize/2); Index++) {
  296. MmioWrite16(GPMC_NAND_DATA_0, *MainAreaWordBuffer++);
  297. //After each write access, device has to wait to accept data.
  298. //Currently we may not be programming proper timing parameters to
  299. //the GPMC_CONFIGi_0 registers and we would need to figure that out.
  300. //Without following delay, page programming fails.
  301. gBS->Stall(1);
  302. }
  303. //Calculate ECC.
  304. NandCalculateEcc();
  305. //Turn off ECC engine.
  306. NandDisableEcc();
  307. //Prepare Spare area buffer with ECC codes.
  308. SetMem(SpareBuffer, gNandFlashInfo->SparePageSize, 0xFF);
  309. CopyMem(&SpareBuffer[ECC_POSITION], gEccCode, gNum512BytesChunks * 3);
  310. //Program spare area with calculated ECC.
  311. for (Index = 0; Index < (gNandFlashInfo->SparePageSize/2); Index++) {
  312. MmioWrite16(GPMC_NAND_DATA_0, *SpareAreaWordBuffer++);
  313. }
  314. //Send PROGRAM command
  315. NandSendCommand(PROGRAM_PAGE_CONFIRM_CMD);
  316. //Poll till device is busy.
  317. NandStatus = 0;
  318. while (Timeout) {
  319. NandStatus = NandReadStatus();
  320. if ((NandStatus & NAND_READY) == NAND_READY) {
  321. break;
  322. }
  323. Timeout--;
  324. }
  325. if (Timeout == 0) {
  326. DEBUG ((EFI_D_ERROR, "Program page timed out.\n"));
  327. return EFI_TIMEOUT;
  328. }
  329. //Bit0 indicates Pass/Fail status
  330. if (NandStatus & NAND_FAILURE) {
  331. return EFI_DEVICE_ERROR;
  332. }
  333. return EFI_SUCCESS;
  334. }
  335. EFI_STATUS
  336. NandEraseBlock (
  337. IN UINTN BlockIndex
  338. )
  339. {
  340. UINTN Address;
  341. UINTN NandStatus;
  342. UINTN Timeout = MAX_RETRY_COUNT;
  343. //Generate device address in bytes to access specific block and page index
  344. Address = GetActualPageAddressInBytes(BlockIndex, 0);
  345. //Send ERASE SETUP command
  346. NandSendCommand(BLOCK_ERASE_CMD);
  347. //Send 3 address cycles to device to access Page address and Block address
  348. Address >>= 11; //Ignore column addresses
  349. NandSendAddress(Address & 0xff);
  350. Address >>= 8;
  351. NandSendAddress(Address & 0xff);
  352. Address >>= 8;
  353. NandSendAddress(Address & 0xff);
  354. //Send ERASE CONFIRM command
  355. NandSendCommand(BLOCK_ERASE_CONFIRM_CMD);
  356. //Poll till device is busy.
  357. NandStatus = 0;
  358. while (Timeout) {
  359. NandStatus = NandReadStatus();
  360. if ((NandStatus & NAND_READY) == NAND_READY) {
  361. break;
  362. }
  363. Timeout--;
  364. gBS->Stall(1);
  365. }
  366. if (Timeout == 0) {
  367. DEBUG ((EFI_D_ERROR, "Erase block timed out for Block: %d.\n", BlockIndex));
  368. return EFI_TIMEOUT;
  369. }
  370. //Bit0 indicates Pass/Fail status
  371. if (NandStatus & NAND_FAILURE) {
  372. return EFI_DEVICE_ERROR;
  373. }
  374. return EFI_SUCCESS;
  375. }
  376. EFI_STATUS
  377. NandReadBlock (
  378. IN UINTN StartBlockIndex,
  379. IN UINTN EndBlockIndex,
  380. OUT VOID *Buffer,
  381. OUT VOID *SpareBuffer
  382. )
  383. {
  384. UINTN BlockIndex;
  385. UINTN PageIndex;
  386. EFI_STATUS Status = EFI_SUCCESS;
  387. for (BlockIndex = StartBlockIndex; BlockIndex <= EndBlockIndex; BlockIndex++) {
  388. //For each block read number of pages
  389. for (PageIndex = 0; PageIndex < gNandFlashInfo->NumPagesPerBlock; PageIndex++) {
  390. Status = NandReadPage(BlockIndex, PageIndex, Buffer, SpareBuffer);
  391. if (EFI_ERROR(Status)) {
  392. return Status;
  393. }
  394. Buffer = ((UINT8 *)Buffer + gNandFlashInfo->PageSize);
  395. }
  396. }
  397. return Status;
  398. }
  399. EFI_STATUS
  400. NandWriteBlock (
  401. IN UINTN StartBlockIndex,
  402. IN UINTN EndBlockIndex,
  403. OUT VOID *Buffer,
  404. OUT VOID *SpareBuffer
  405. )
  406. {
  407. UINTN BlockIndex;
  408. UINTN PageIndex;
  409. EFI_STATUS Status = EFI_SUCCESS;
  410. for (BlockIndex = StartBlockIndex; BlockIndex <= EndBlockIndex; BlockIndex++) {
  411. //Page programming.
  412. for (PageIndex = 0; PageIndex < gNandFlashInfo->NumPagesPerBlock; PageIndex++) {
  413. Status = NandWritePage(BlockIndex, PageIndex, Buffer, SpareBuffer);
  414. if (EFI_ERROR(Status)) {
  415. return Status;
  416. }
  417. Buffer = ((UINT8 *)Buffer + gNandFlashInfo->PageSize);
  418. }
  419. }
  420. return Status;
  421. }
  422. EFI_STATUS
  423. EFIAPI
  424. NandFlashReset (
  425. IN EFI_BLOCK_IO_PROTOCOL *This,
  426. IN BOOLEAN ExtendedVerification
  427. )
  428. {
  429. UINTN BusyStall = 50; // microSeconds
  430. UINTN ResetBusyTimeout = (1000000 / BusyStall); // 1 Second
  431. //Send RESET command to device.
  432. NandSendCommand(RESET_CMD);
  433. //Wait for 1ms before we check status register.
  434. gBS->Stall(1000);
  435. //Check BIT#5 & BIT#6 in Status register to make sure RESET is done.
  436. while ((NandReadStatus() & NAND_RESET_STATUS) != NAND_RESET_STATUS) {
  437. //In case of extended verification, wait for extended amount of time
  438. //to make sure device is reset.
  439. if (ExtendedVerification) {
  440. if (ResetBusyTimeout == 0) {
  441. return EFI_DEVICE_ERROR;
  442. }
  443. gBS->Stall(BusyStall);
  444. ResetBusyTimeout--;
  445. }
  446. }
  447. return EFI_SUCCESS;
  448. }
  449. EFI_STATUS
  450. EFIAPI
  451. NandFlashReadBlocks (
  452. IN EFI_BLOCK_IO_PROTOCOL *This,
  453. IN UINT32 MediaId,
  454. IN EFI_LBA Lba,
  455. IN UINTN BufferSize,
  456. OUT VOID *Buffer
  457. )
  458. {
  459. UINTN NumBlocks;
  460. UINTN EndBlockIndex;
  461. EFI_STATUS Status;
  462. UINT8 *SpareBuffer = NULL;
  463. if (Buffer == NULL) {
  464. Status = EFI_INVALID_PARAMETER;
  465. goto exit;
  466. }
  467. if (Lba > LAST_BLOCK) {
  468. Status = EFI_INVALID_PARAMETER;
  469. goto exit;
  470. }
  471. if ((BufferSize % gNandFlashInfo->BlockSize) != 0) {
  472. Status = EFI_BAD_BUFFER_SIZE;
  473. goto exit;
  474. }
  475. NumBlocks = DivU64x32(BufferSize, gNandFlashInfo->BlockSize);
  476. EndBlockIndex = ((UINTN)Lba + NumBlocks) - 1;
  477. SpareBuffer = (UINT8 *)AllocatePool(gNandFlashInfo->SparePageSize);
  478. if (SpareBuffer == NULL) {
  479. Status = EFI_OUT_OF_RESOURCES;
  480. goto exit;
  481. }
  482. //Read block
  483. Status = NandReadBlock((UINTN)Lba, EndBlockIndex, Buffer, SpareBuffer);
  484. if (EFI_ERROR(Status)) {
  485. DEBUG((EFI_D_ERROR, "Read block fails: %x\n", Status));
  486. goto exit;
  487. }
  488. exit:
  489. if (SpareBuffer != NULL) {
  490. FreePool (SpareBuffer);
  491. }
  492. return Status;
  493. }
  494. EFI_STATUS
  495. EFIAPI
  496. NandFlashWriteBlocks (
  497. IN EFI_BLOCK_IO_PROTOCOL *This,
  498. IN UINT32 MediaId,
  499. IN EFI_LBA Lba,
  500. IN UINTN BufferSize,
  501. IN VOID *Buffer
  502. )
  503. {
  504. UINTN BlockIndex;
  505. UINTN NumBlocks;
  506. UINTN EndBlockIndex;
  507. EFI_STATUS Status;
  508. UINT8 *SpareBuffer = NULL;
  509. if (Buffer == NULL) {
  510. Status = EFI_INVALID_PARAMETER;
  511. goto exit;
  512. }
  513. if (Lba > LAST_BLOCK) {
  514. Status = EFI_INVALID_PARAMETER;
  515. goto exit;
  516. }
  517. if ((BufferSize % gNandFlashInfo->BlockSize) != 0) {
  518. Status = EFI_BAD_BUFFER_SIZE;
  519. goto exit;
  520. }
  521. NumBlocks = DivU64x32(BufferSize, gNandFlashInfo->BlockSize);
  522. EndBlockIndex = ((UINTN)Lba + NumBlocks) - 1;
  523. SpareBuffer = (UINT8 *)AllocatePool(gNandFlashInfo->SparePageSize);
  524. if (SpareBuffer == NULL) {
  525. Status = EFI_OUT_OF_RESOURCES;
  526. goto exit;
  527. }
  528. // Erase block
  529. for (BlockIndex = (UINTN)Lba; BlockIndex <= EndBlockIndex; BlockIndex++) {
  530. Status = NandEraseBlock(BlockIndex);
  531. if (EFI_ERROR(Status)) {
  532. DEBUG((EFI_D_ERROR, "Erase block failed. Status: %x\n", Status));
  533. goto exit;
  534. }
  535. }
  536. // Program data
  537. Status = NandWriteBlock((UINTN)Lba, EndBlockIndex, Buffer, SpareBuffer);
  538. if (EFI_ERROR(Status)) {
  539. DEBUG((EFI_D_ERROR, "Block write fails: %x\n", Status));
  540. goto exit;
  541. }
  542. exit:
  543. if (SpareBuffer != NULL) {
  544. FreePool (SpareBuffer);
  545. }
  546. return Status;
  547. }
  548. EFI_STATUS
  549. EFIAPI
  550. NandFlashFlushBlocks (
  551. IN EFI_BLOCK_IO_PROTOCOL *This
  552. )
  553. {
  554. return EFI_SUCCESS;
  555. }
  556. EFI_BLOCK_IO_MEDIA gNandFlashMedia = {
  557. SIGNATURE_32('n','a','n','d'), // MediaId
  558. FALSE, // RemovableMedia
  559. TRUE, // MediaPresent
  560. FALSE, // LogicalPartition
  561. FALSE, // ReadOnly
  562. FALSE, // WriteCaching
  563. 0, // BlockSize
  564. 2, // IoAlign
  565. 0, // Pad
  566. 0 // LastBlock
  567. };
  568. EFI_BLOCK_IO_PROTOCOL BlockIo =
  569. {
  570. EFI_BLOCK_IO_INTERFACE_REVISION, // Revision
  571. &gNandFlashMedia, // *Media
  572. NandFlashReset, // Reset
  573. NandFlashReadBlocks, // ReadBlocks
  574. NandFlashWriteBlocks, // WriteBlocks
  575. NandFlashFlushBlocks // FlushBlocks
  576. };
  577. EFI_STATUS
  578. NandFlashInitialize (
  579. IN EFI_HANDLE ImageHandle,
  580. IN EFI_SYSTEM_TABLE *SystemTable
  581. )
  582. {
  583. EFI_STATUS Status;
  584. gNandFlashInfo = (NAND_FLASH_INFO *)AllocateZeroPool (sizeof(NAND_FLASH_INFO));
  585. //Initialize GPMC module.
  586. GpmcInit();
  587. //Reset NAND part
  588. NandFlashReset(&BlockIo, FALSE);
  589. //Detect NAND part and populate gNandFlashInfo structure
  590. Status = NandDetectPart ();
  591. if (EFI_ERROR(Status)) {
  592. DEBUG((EFI_D_ERROR, "Nand part id detection failure: Status: %x\n", Status));
  593. return Status;
  594. }
  595. //Count total number of 512Bytes chunk based on the page size.
  596. if (gNandFlashInfo->PageSize == PAGE_SIZE_512B) {
  597. gNum512BytesChunks = 1;
  598. } else if (gNandFlashInfo->PageSize == PAGE_SIZE_2K) {
  599. gNum512BytesChunks = 4;
  600. } else if (gNandFlashInfo->PageSize == PAGE_SIZE_4K) {
  601. gNum512BytesChunks = 8;
  602. }
  603. gEccCode = (UINT8 *)AllocatePool(gNum512BytesChunks * 3);
  604. if (gEccCode == NULL) {
  605. return EFI_OUT_OF_RESOURCES;
  606. }
  607. //Configure ECC
  608. NandConfigureEcc ();
  609. //Patch EFI_BLOCK_IO_MEDIA structure.
  610. gNandFlashMedia.BlockSize = gNandFlashInfo->BlockSize;
  611. gNandFlashMedia.LastBlock = LAST_BLOCK;
  612. //Publish BlockIO.
  613. Status = gBS->InstallMultipleProtocolInterfaces (
  614. &ImageHandle,
  615. &gEfiBlockIoProtocolGuid, &BlockIo,
  616. &gEfiDevicePathProtocolGuid, &gDevicePath,
  617. NULL
  618. );
  619. return Status;
  620. }