bmt_tlc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <platform/bmt.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. typedef struct {
  35. char signature[3];
  36. u8 version;
  37. u8 bad_count; // bad block count in pool
  38. u8 mapped_count; // mapped block count in pool
  39. u8 checksum;
  40. u8 reseverd[13];
  41. } phys_bmt_header;
  42. typedef struct {
  43. phys_bmt_header header;
  44. bmt_entry table[MAX_BMT_SIZE];
  45. } phys_bmt_struct;
  46. typedef struct {
  47. char signature[3];
  48. } bmt_oob_data;
  49. static char MAIN_SIGNATURE[] = "BMT";
  50. static char OOB_SIGNATURE[] = "bmt";
  51. #define SIGNATURE_SIZE (3)
  52. #define MAX_DAT_SIZE 0x4000
  53. #define MAX_OOB_SIZE 0x800
  54. extern flashdev_info devinfo;
  55. #if defined(MTK_TLC_NAND_SUPPORT)
  56. extern bool mtk_nand_write_tlc_block_hw(struct nand_chip *chip,
  57. uint8_t *buf, u32 mapped_block);
  58. extern bool mtk_block_istlc(u64 addr);
  59. extern unsigned char g_spare_buf[MAX_OOB_SIZE];
  60. #endif
  61. static struct nand_chip *nand_chip_bmt;
  62. #define PAGE_SIZE_BMT (nand_chip_bmt->page_size)
  63. #if !defined(MTK_TLC_NAND_SUPPORT)
  64. #define BLOCK_SIZE_BMT (nand_chip_bmt->sector_size == 512?nand_chip_bmt->erasesize : nand_chip_bmt->erasesize*2)
  65. #define OFFSET(block) (((u64)block) * BLOCK_SIZE_BMT)
  66. #define PAGE_ADDR(block) ((block) * (BLOCK_SIZE_BMT / PAGE_SIZE_BMT))
  67. #else
  68. #define BLOCK_SIZE_BMT (devinfo.blocksize * 1024)
  69. #define PAGE_PER_SIZE_BMT ((devinfo.blocksize * 1024) / devinfo.pagesize)
  70. //#define OFFSET(block) (((u64)block) * BLOCK_SIZE_BMT)
  71. #define PAGE_ADDR(block) ((block) * PAGE_PER_SIZE_BMT)
  72. extern u64 OFFSET(u32 block);//return logical address
  73. #endif
  74. /*********************************************************************
  75. * Flash is splited into 2 parts, system part is for normal system *
  76. * system usage, size is system_block_count, another is replace pool *
  77. * +-------------------------------------------------+ *
  78. * | system_block_count | bmt_block_count | *
  79. * +-------------------------------------------------+ *
  80. *********************************************************************/
  81. static u32 total_block_count; // block number in flash
  82. u32 system_block_count;
  83. static int bmt_block_count; // bmt table size
  84. static int page_per_block; // page per count
  85. static u32 bmt_block_index; // bmt block index
  86. static bmt_struct bmt; // dynamic created global bmt table
  87. __attribute__((aligned(64))) static u8 dat_buf[MAX_DAT_SIZE];
  88. static u8 oob_buf[MAX_OOB_SIZE];
  89. static bool pool_erased;
  90. extern bool nand_erase_hw (u64 offset);
  91. extern bool mark_block_bad_hw(u64 offset);
  92. extern int nand_write_page_hw(u32 page, u8 *dat, u8 *oob);
  93. /***************************************************************
  94. *
  95. * Interface adaptor for preloader/uboot/kernel
  96. * These interfaces operate on physical address, read/write
  97. * physical data.
  98. *
  99. ***************************************************************/
  100. int nand_read_page_bmt(u32 page, u8 * dat, u8 * oob)
  101. {
  102. return nand_exec_read_page_hw(nand_chip_bmt, page, PAGE_SIZE_BMT, dat, oob);
  103. }
  104. bool nand_block_bad_bmt(u64 offset)
  105. {
  106. return nand_block_bad_hw(nand_chip_bmt, offset);
  107. }
  108. // actually uboot should never use the following 3 functions Fix me kai
  109. bool nand_erase_bmt(u64 offset)
  110. {
  111. return nand_erase_hw(offset);
  112. }
  113. int mark_block_bad_bmt(u64 offset)
  114. {
  115. #if !defined(MTK_TLC_NAND_SUPPORT)
  116. return mark_block_bad_hw(offset);
  117. #endif
  118. }
  119. bool nand_write_page_bmt(u32 page, u8 * dat, u8 * oob)
  120. {
  121. return nand_write_page_hw(page, dat, oob);
  122. }
  123. /***************************************************************
  124. * *
  125. * static internal function *
  126. * *
  127. ***************************************************************/
  128. static void dump_bmt_info(bmt_struct * bmt)
  129. {
  130. int i;
  131. dprintf(INFO, "BMT v%d. total %d mapping:\n", bmt->version, bmt->mapped_count);
  132. for (i = 0; i < bmt->mapped_count; i++) {
  133. dprintf(INFO, "\tbad block (0x%x) has been mapped to block (0x%x)\n", bmt->table[i].bad_index, bmt->table[i].mapped_index);
  134. }
  135. }
  136. static bool match_bmt_signature(u8 * dat, u8 * oob)
  137. {
  138. if (memcmp(dat + MAIN_SIGNATURE_OFFSET, MAIN_SIGNATURE, SIGNATURE_SIZE)) {
  139. dprintf(INFO, "[%s]0x%x,0x%x,0x%x,0x%x \n", __FUNCTION__, *((UINT32 *) dat), *(((UINT32 *) dat) + 1), *(((UINT32 *) dat) + 2), *(((UINT32 *) dat) + 3));
  140. return false;
  141. }
  142. if (memcmp(oob + OOB_SIGNATURE_OFFSET, OOB_SIGNATURE, SIGNATURE_SIZE)) {
  143. dprintf(INFO, "main signature match, oob signature doesn't match, but ignore\n");
  144. }
  145. return true;
  146. }
  147. static u8 cal_bmt_checksum(phys_bmt_struct * phys_table, int bmt_size)
  148. {
  149. u32 i;
  150. u8 checksum = 0;
  151. u8 *dat = (u8 *) phys_table;
  152. checksum += phys_table->header.version;
  153. checksum += phys_table->header.mapped_count;
  154. dat += sizeof(phys_bmt_header);
  155. for (i = 0; i < bmt_size * sizeof(bmt_entry); i++) {
  156. checksum += dat[i];
  157. }
  158. return checksum;
  159. }
  160. // return -1 for unmapped block, and bad block index if mapped.
  161. static int is_block_mapped(int index)
  162. {
  163. int i;
  164. for (i = 0; i < bmt.mapped_count; i++) {
  165. if (index == bmt.table[i].mapped_index)
  166. return i;
  167. }
  168. return -1;
  169. }
  170. static bool is_page_used(u8 * dat, u8 * oob)
  171. {
  172. if (2048 == PAGE_SIZE_BMT) {
  173. return ((oob[13] != 0xFF) || (oob[14] != 0xFF));
  174. } else {
  175. return ((oob[OOB_INDEX_OFFSET] != 0xFF) || (oob[OOB_INDEX_OFFSET + 1] != 0xFF));
  176. }
  177. }
  178. static bool valid_bmt_data(phys_bmt_struct * phys_table)
  179. {
  180. int i;
  181. u8 checksum = cal_bmt_checksum(phys_table, bmt_block_count);
  182. if (phys_table->header.checksum != checksum) {
  183. dprintf(INFO, "BMT Data checksum error: %x %x\n", phys_table->header.checksum, checksum);
  184. return false;
  185. }
  186. dprintf(INFO, "BMT Checksum is: 0x%x\n", phys_table->header.checksum);
  187. for (i = 0; i < phys_table->header.mapped_count; i++) {
  188. if (phys_table->table[i].bad_index >= total_block_count || phys_table->table[i].mapped_index >= total_block_count || phys_table->table[i].mapped_index < system_block_count) {
  189. dprintf(INFO, "index error: bad_index: %d, mapped_index: %d\n", phys_table->table[i].bad_index, phys_table->table[i].mapped_index);
  190. return false;
  191. }
  192. }
  193. dprintf(INFO, "Valid BMT, version v%d\n", phys_table->header.version);
  194. return true;
  195. }
  196. static void fill_nand_bmt_buffer(bmt_struct * bmt, u8 * dat, u8 * oob)
  197. {
  198. phys_bmt_struct *phys_bmt = NULL;
  199. phys_bmt = (phys_bmt_struct *)malloc(sizeof(phys_bmt_struct));
  200. if (!phys_bmt) {
  201. ASSERT(0);
  202. }
  203. dump_bmt_info(bmt);
  204. // fill phys_bmt_struct structure with bmt_struct
  205. memset(phys_bmt, 0xFF, sizeof(phys_bmt_struct));
  206. memcpy(phys_bmt->header.signature, MAIN_SIGNATURE, SIGNATURE_SIZE);
  207. phys_bmt->header.version = BMT_VERSION;
  208. // phys_bmt.header.bad_count = bmt->bad_count;
  209. phys_bmt->header.mapped_count = bmt->mapped_count;
  210. memcpy(phys_bmt->table, bmt->table, sizeof(bmt_entry) * bmt_block_count);
  211. phys_bmt->header.checksum = cal_bmt_checksum(phys_bmt, bmt_block_count);
  212. memcpy(dat + MAIN_SIGNATURE_OFFSET, phys_bmt, sizeof(phys_bmt_struct));
  213. memcpy(oob + OOB_SIGNATURE_OFFSET, OOB_SIGNATURE, SIGNATURE_SIZE);
  214. free(phys_bmt);
  215. }
  216. // return valid index if found BMT, else return 0
  217. static int load_bmt_data(int start, int pool_size)
  218. {
  219. int bmt_index = start + pool_size - 1;
  220. phys_bmt_struct *phys_table = NULL;
  221. int i;
  222. phys_table = (phys_bmt_struct *)malloc(sizeof(phys_bmt_struct));
  223. if (!phys_table) {
  224. ASSERT(0);
  225. }
  226. dprintf(INFO, "[%s]: begin to search BMT from block 0x%x\n", __FUNCTION__, bmt_index);
  227. dprintf(INFO, "[%s]: bmt_index=0x%x, start=0x%x, pool_size=0x%x, \n", __FUNCTION__, bmt_index, start, pool_size);
  228. for (bmt_index = start + pool_size - 1; bmt_index >= start; bmt_index--) {
  229. dprintf(INFO, "[%s]: bmt_index=0x%x-- ", __FUNCTION__, bmt_index);
  230. if (nand_block_bad_bmt(OFFSET(bmt_index))) {
  231. dprintf(INFO, "Skip bad block: %d\n", bmt_index);
  232. continue;
  233. }
  234. #if defined(MTK_TLC_NAND_SUPPORT)
  235. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  236. && (devinfo.tlcControl.normaltlc))
  237. devinfo.tlcControl.slcopmodeEn = TRUE; //change to slc mode
  238. #endif
  239. memset(dat_buf, 0xAA, sizeof(dat_buf));
  240. memset(oob_buf, 0xAA, sizeof(oob_buf));
  241. if (!nand_read_page_bmt(PAGE_ADDR(bmt_index), dat_buf, oob_buf)) {
  242. dprintf(INFO, "Error found when read block %d\n", bmt_index);
  243. continue;
  244. }
  245. if (!match_bmt_signature(dat_buf, oob_buf)) {
  246. dprintf(INFO, "[%s]: match_bmt_signature out! \n", __FUNCTION__);
  247. continue;
  248. }
  249. dprintf(INFO, "Match bmt signature @ block: 0x%x\n", bmt_index);
  250. memcpy(phys_table, dat_buf + MAIN_SIGNATURE_OFFSET, sizeof(phys_bmt_struct));
  251. if (!valid_bmt_data(phys_table)) {
  252. dprintf(INFO, "BMT data is not correct %d\n", bmt_index);
  253. continue;
  254. } else {
  255. bmt.mapped_count = phys_table->header.mapped_count;
  256. bmt.version = phys_table->header.version;
  257. memcpy(bmt.table, phys_table->table, bmt.mapped_count * sizeof(bmt_entry));
  258. dprintf(INFO, "bmt found at block: %d, mapped block: %d\n", bmt_index, bmt.mapped_count);
  259. #if !defined(MTK_TLC_NAND_SUPPORT)
  260. for (i = 0; i < bmt.mapped_count; i++) {
  261. if (!nand_block_bad_bmt(OFFSET(bmt.table[i].bad_index))) {
  262. dprintf(INFO, "block 0x%x is not mark bad, should be power lost last time\n", bmt.table[i].bad_index);
  263. mark_block_bad_bmt(OFFSET(bmt.table[i].bad_index));
  264. }
  265. }
  266. #endif
  267. free(phys_table);
  268. return bmt_index;
  269. }
  270. }
  271. free(phys_table);
  272. dprintf(INFO, "bmt block not found!\n");
  273. return 0;
  274. }
  275. /*************************************************************************
  276. * Find an available block and erase. *
  277. * start_from_end: if true, find available block from end of flash. *
  278. * else, find from the beginning of the pool *
  279. * need_erase: if true, all unmapped blocks in the pool will be erased *
  280. *************************************************************************/
  281. static int find_available_block(bool start_from_end)
  282. {
  283. int i; // , j;
  284. u32 block = system_block_count;
  285. int direction;
  286. dprintf(INFO, "Try to find_available_block, pool_erase: %d\n", pool_erased);
  287. #if defined(MTK_TLC_NAND_SUPPORT)
  288. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  289. && (devinfo.tlcControl.normaltlc))
  290. devinfo.tlcControl.slcopmodeEn = TRUE;
  291. #endif
  292. #if !defined(MTK_TLC_NAND_SUPPORT)
  293. if (!pool_erased) {
  294. dprintf(INFO, "Erase all un-mapped blocks in pool\n");
  295. for (i = 0; i < bmt_block_count; i++) {
  296. if (block == bmt_block_index) {
  297. dprintf(INFO, "Skip bmt block 0x%x\n", block);
  298. continue;
  299. }
  300. if (nand_block_bad_bmt(OFFSET(block + i))) {
  301. dprintf(INFO, "Skip bad block 0x%x\n", block + i);
  302. continue;
  303. }
  304. if (is_block_mapped(block + i) >= 0) {
  305. dprintf(INFO, "Skip mapped block 0x%x\n", block + i);
  306. continue;
  307. }
  308. //xiaolei: erase non-mapped block by SLC mode firstly
  309. //then, before using as each Mode, should erase by that Mode first.
  310. if (!nand_erase_bmt(((u64)(block + i)) * (devinfo.blocksize * 1024))) {
  311. dprintf(INFO, "Erase block 0x%x failed\n", block + i);
  312. mark_block_bad_bmt(OFFSET(block + i));
  313. }
  314. }
  315. pool_erased = 1;
  316. }
  317. #endif
  318. if (start_from_end) {
  319. block = total_block_count - 1;
  320. direction = -1;
  321. } else {
  322. block = system_block_count;
  323. direction = 1;
  324. }
  325. for (i = 0; i < bmt_block_count; i++, block += direction) {
  326. if (block == bmt_block_index) {
  327. dprintf(INFO, "Skip bmt block 0x%x\n", block);
  328. continue;
  329. }
  330. if (nand_block_bad_bmt(OFFSET(block))) {
  331. dprintf(INFO, "Skip bad block 0x%x\n", block);
  332. continue;
  333. }
  334. if (is_block_mapped(block) >= 0) {
  335. dprintf(INFO, "Skip mapped block 0x%x\n", block);
  336. continue;
  337. }
  338. #if defined(MTK_TLC_NAND_SUPPORT)
  339. if (!nand_erase_bmt(((u64)block) * (devinfo.blocksize * 1024))) {
  340. dprintf(INFO, "Erase block 0x%x failed\n", block);
  341. mark_block_bad_bmt(OFFSET(block));
  342. }
  343. #endif
  344. dprintf(INFO, "Find block 0x%x available\n", block);
  345. return block;
  346. }
  347. return 0;
  348. }
  349. static unsigned short get_bad_index_from_oob(u8 * oob_buf)
  350. {
  351. unsigned short index;
  352. if (2048 == PAGE_SIZE_BMT) { // sector 1024 FDM size = 16, mark location moved
  353. memcpy(&index, oob_buf + 13, OOB_INDEX_SIZE);
  354. } else {
  355. memcpy(&index, oob_buf + OOB_INDEX_OFFSET, OOB_INDEX_SIZE);
  356. }
  357. return index;
  358. }
  359. void set_bad_index_to_oob(u8 * oob, u16 index)
  360. {
  361. if (2048 == PAGE_SIZE_BMT) {
  362. memcpy(oob + 13, &index, sizeof(index));
  363. } else {
  364. memcpy(oob + OOB_INDEX_OFFSET, &index, sizeof(index));
  365. }
  366. }
  367. static int migrate_from_bad(u64 offset, u8 * write_dat, u8 * write_oob)
  368. {
  369. int page;
  370. #if defined(MTK_TLC_NAND_SUPPORT)
  371. u32 error_block = (u32)(offset / (devinfo.blocksize * 1024));
  372. u32 error_page = (u32)(offset >> nand_chip_bmt->page_shift) % page_per_block;
  373. u32 orig_block = error_block;
  374. u32 idx;
  375. bool tlc_mode_block = FALSE;
  376. int bRet;
  377. #else
  378. u32 error_block = (u32)(offset / BLOCK_SIZE_BMT);
  379. u32 error_page = (u32)(offset / PAGE_SIZE_BMT) % page_per_block;
  380. #endif
  381. int to_index;
  382. u32 tick = 1;
  383. memcpy(oob_buf, write_oob, MAX_OOB_SIZE);
  384. to_index = find_available_block(false);
  385. if (!to_index) {
  386. dprintf(INFO, "Cannot find an available block for BMT\n");
  387. return 0;
  388. }
  389. #if defined(MTK_TLC_NAND_SUPPORT)
  390. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  391. && (devinfo.tlcControl.normaltlc)) {
  392. if (error_block >= system_block_count) { // mapped block become bad, find original bad block
  393. for (idx = 0; idx < bmt_block_count; idx++) {
  394. if (bmt.table[idx].mapped_index == error_block) {
  395. orig_block = bmt.table[idx].bad_index;
  396. break;
  397. }
  398. }
  399. }
  400. tlc_mode_block = mtk_block_istlc(orig_block * (devinfo.blocksize * 1024));
  401. if (!tlc_mode_block) {
  402. devinfo.tlcControl.slcopmodeEn = TRUE; //slc mode
  403. tick = 3;
  404. } else {
  405. devinfo.tlcControl.slcopmodeEn = false;
  406. }
  407. }
  408. #endif
  409. #if defined(MTK_TLC_NAND_SUPPORT)
  410. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  411. && (devinfo.tlcControl.normaltlc) && tlc_mode_block) {
  412. if (error_block >= system_block_count)
  413. set_bad_index_to_oob(oob_buf, orig_block);
  414. else
  415. set_bad_index_to_oob(oob_buf, error_block);
  416. //erase block by slc mode in find_available_block
  417. //should erase block by tlc mode for tlc block
  418. memcpy(g_spare_buf, oob_buf, MAX_OOB_SIZE);
  419. nand_erase_bmt(((u64)to_index) * (devinfo.blocksize * 1024));
  420. bRet = mtk_nand_write_tlc_block_hw(nand_chip_bmt, write_dat, to_index);
  421. if (bRet != 0) {
  422. dprintf(INFO, "Write to page 0x%x fail\n", PAGE_ADDR(to_index) + error_page);
  423. mark_block_bad_bmt(OFFSET(to_index));
  424. return migrate_from_bad(offset, write_dat, write_oob);
  425. }
  426. } else
  427. #endif
  428. {
  429. {
  430. // migrate error page first
  431. dprintf(INFO, "Write error page: 0x%x\n", error_page);
  432. if (!write_dat) {
  433. nand_read_page_bmt(PAGE_ADDR(error_block) + error_page, dat_buf, NULL);
  434. write_dat = dat_buf;
  435. }
  436. // memcpy(oob_buf, write_oob, MAX_OOB_SIZE);
  437. if (error_block < system_block_count)
  438. set_bad_index_to_oob(oob_buf, error_block); // if error_block is already a mapped block, original mapping index is in OOB.
  439. if (!nand_write_page_bmt(PAGE_ADDR(to_index) + error_page, write_dat, oob_buf)) {
  440. dprintf(INFO, "Write to page 0x%x fail\n", PAGE_ADDR(to_index) + error_page);
  441. mark_block_bad_bmt(OFFSET(to_index));
  442. return migrate_from_bad(offset, write_dat, write_oob);
  443. }
  444. }
  445. for (page = 0; page < page_per_block; page+=tick) {
  446. if ((u32)page != error_page) {
  447. nand_read_page_bmt(PAGE_ADDR(error_block) + page, dat_buf, oob_buf);
  448. if (is_page_used(dat_buf, oob_buf)) {
  449. if (error_block < system_block_count) {
  450. set_bad_index_to_oob(oob_buf, error_block);
  451. }
  452. dprintf(INFO, "\tmigrate page 0x%x to page 0x%x\n",PAGE_ADDR(error_block) + page, PAGE_ADDR(to_index) + page);
  453. if (!nand_write_page_bmt(PAGE_ADDR(to_index) + page, dat_buf, oob_buf)) {
  454. dprintf(INFO, "Write to page 0x%x fail\n", PAGE_ADDR(to_index) + page);
  455. mark_block_bad_bmt(OFFSET(to_index));
  456. return migrate_from_bad(offset, write_dat, write_oob);
  457. }
  458. }
  459. }
  460. }
  461. }
  462. dprintf(INFO, "Migrate from 0x%x to 0x%x done!\n", error_block, to_index);
  463. return to_index;
  464. }
  465. static bool write_bmt_to_flash(u8 * dat, u8 * oob)
  466. {
  467. bool need_erase = true;
  468. dprintf(INFO, "Try to write BMT\n");
  469. if (bmt_block_index == 0) {
  470. // if we don't have index, we don't need to erase found block as it has been erased in find_available_block()
  471. need_erase = false;
  472. if (!(bmt_block_index = find_available_block(true))) {
  473. dprintf(INFO, "Cannot find an available block for BMT\n");
  474. return false;
  475. }
  476. }
  477. dprintf(INFO, "Find BMT block: 0x%x\n", bmt_block_index);
  478. #if defined(MTK_TLC_NAND_SUPPORT)
  479. if ((devinfo.NAND_FLASH_TYPE == NAND_FLASH_TLC)
  480. && (devinfo.tlcControl.normaltlc))
  481. devinfo.tlcControl.slcopmodeEn = TRUE; //change to slc mode
  482. #endif
  483. if (need_erase) {
  484. if (!nand_erase_bmt(((u64)bmt_block_index) * (devinfo.blocksize * 1024))) {
  485. dprintf(INFO, "BMT block erase fail, mark bad: 0x%x\n", bmt_block_index);
  486. mark_block_bad_bmt(OFFSET(bmt_block_index));
  487. bmt_block_index = 0;
  488. return write_bmt_to_flash(dat, oob); // recursive call
  489. }
  490. }
  491. if (!nand_write_page_bmt(PAGE_ADDR(bmt_block_index), dat, oob)) {
  492. dprintf(INFO, "Write BMT data fail, need to write again\n");
  493. mark_block_bad_bmt(OFFSET(bmt_block_index));
  494. // bmt.bad_count++;
  495. bmt_block_index = 0;
  496. return write_bmt_to_flash(dat, oob); // recursive call
  497. }
  498. dprintf(INFO, "Write BMT data to block 0x%x success\n", bmt_block_index);
  499. return true;
  500. }
  501. /*******************************************************************
  502. * Reconstruct bmt, called when found bmt info doesn't match bad
  503. * block info in flash.
  504. *
  505. * Return NULL for failure
  506. *******************************************************************/
  507. bmt_struct *reconstruct_bmt(bmt_struct * bmt)
  508. {
  509. int i;
  510. int index = system_block_count;
  511. unsigned short bad_index;
  512. int mapped;
  513. bmt->version = BMT_VERSION;
  514. bmt->bad_count = 0;
  515. bmt->mapped_count = 0;
  516. memset(bmt->table, 0, bmt_block_count * sizeof(bmt_entry));
  517. for (i = 0; i < bmt_block_count; i++, index++) {
  518. if (nand_block_bad_bmt(OFFSET(index))) {
  519. dprintf(INFO, "Skip bad block: 0x%x\n", index);
  520. continue;
  521. }
  522. nand_read_page_bmt(PAGE_ADDR(index), dat_buf, oob_buf);
  523. if ((bad_index = get_bad_index_from_oob(oob_buf)) >= system_block_count) {
  524. dprintf(INFO, "get bad index: 0x%x\n", bad_index);
  525. if (bad_index != 0xFFFF)
  526. dprintf(INFO, "Invalid bad index found in block 0x%x, bad index 0x%x\n", index, bad_index);
  527. continue;
  528. }
  529. dprintf(INFO, "Block 0x%x is mapped to bad block: 0x%x\n", index, bad_index);
  530. if (!nand_block_bad_bmt(OFFSET(bad_index))) {
  531. dprintf(INFO, "\tbut block 0x%x is not marked as bad, invalid mapping\n", bad_index);
  532. continue; // no need to erase here, it will be erased later when trying to write BMT
  533. }
  534. if ((mapped = is_block_mapped(bad_index)) >= 0) {
  535. dprintf(INFO, "bad block 0x%x is mapped to 0x%x, should be caused by power lost, replace with one\n", bmt->table[mapped].bad_index, bmt->table[mapped].mapped_index);
  536. bmt->table[mapped].mapped_index = index; // use new one instead.
  537. } else {
  538. bmt->table[bmt->mapped_count].bad_index = bad_index;
  539. bmt->table[bmt->mapped_count].mapped_index = index;
  540. bmt->mapped_count++;
  541. }
  542. dprintf(INFO, "Add mapping: 0x%x -> 0x%x to BMT\n", bad_index, index);
  543. }
  544. dprintf(INFO, "Scan replace pool done, mapped block: %d\n", bmt->mapped_count);
  545. memset(oob_buf, 0xFF, sizeof(oob_buf));
  546. fill_nand_bmt_buffer(bmt, dat_buf, oob_buf);
  547. if (!write_bmt_to_flash(dat_buf, oob_buf)) {
  548. dprintf(INFO, "TRAGEDY: cannot find a place to write BMT!!!!\n");
  549. }
  550. return bmt;
  551. }
  552. /*******************************************************************
  553. * [BMT Interface]
  554. *
  555. * Description:
  556. * Init bmt from nand. Reconstruct if not found or data error
  557. *
  558. * Parameter:
  559. * size: size of bmt and replace pool
  560. *
  561. * Return:
  562. * NULL for failure, and a bmt struct for success
  563. *******************************************************************/
  564. bmt_struct *init_bmt(struct nand_chip * chip, int size)
  565. {
  566. if (size > 0 && size < MAX_BMT_SIZE) {
  567. dprintf(INFO, "Init bmt table, size: %d\n", size);
  568. bmt_block_count = size;
  569. } else {
  570. dprintf(INFO, "Invalid bmt table size: %d\n", size);
  571. return NULL;
  572. }
  573. nand_chip_bmt = chip;
  574. system_block_count = (u32)(chip->chipsize / (devinfo.blocksize * 1024));
  575. total_block_count = bmt_block_count + system_block_count;
  576. page_per_block = (devinfo.blocksize * 1024) / devinfo.pagesize;
  577. dprintf(INFO, "bmt count: %d, system count: %d\n", bmt_block_count, system_block_count);
  578. pool_erased = 0;
  579. memset(bmt.table, 0, size * sizeof(bmt_entry));
  580. if ((bmt_block_index = load_bmt_data(system_block_count, size))) {
  581. dprintf(INFO, "Load bmt data success @ block 0x%x\n", bmt_block_index);
  582. //dump_bmt_info(&bmt);
  583. return &bmt;
  584. } else {
  585. dprintf(INFO, "Load bmt data fail, need re-construct!\n");
  586. #if defined(MTK_TLC_NAND_SUPPORT)
  587. //cannot go here by normal flow on TLC NAND. bmt table can not be restructed by reading oob of each bmt pool block.
  588. //because it is invisiable of SLC/TLC MODE in bmt pool.
  589. return NULL;
  590. #else
  591. if (reconstruct_bmt(&bmt))
  592. return &bmt;
  593. else
  594. return NULL;
  595. #endif
  596. }
  597. }
  598. /*******************************************************************
  599. * [BMT Interface]
  600. *
  601. * Description:
  602. * Update BMT.
  603. *
  604. * Parameter:
  605. * offset: update block/page offset.
  606. * reason: update reason, see update_reason_t for reason.
  607. * dat/oob: data and oob buffer for write fail.
  608. *
  609. * Return:
  610. * Return true for success, and false for failure.
  611. *******************************************************************/
  612. bool update_bmt(u64 offset, update_reason_t reason, u8 * dat, u8 * oob)
  613. {
  614. int map_index;
  615. int orig_bad_block = -1;
  616. int i;
  617. u32 bad_index = (u32)(offset / (devinfo.blocksize * 1024));
  618. if (reason == UPDATE_WRITE_FAIL) {
  619. dprintf(INFO, "Write fail, need to migrate\n");
  620. if (!(map_index = migrate_from_bad(offset, dat, oob))) {
  621. dprintf(INFO, "migrate fail\n");
  622. return false;
  623. }
  624. } else {
  625. if (!(map_index = find_available_block(false))) {
  626. dprintf(INFO, "Cannot find block in pool\n");
  627. return false;
  628. }
  629. }
  630. if ((u32)bad_index >= system_block_count) {
  631. for (i = 0; i < bmt_block_count; i++) {
  632. if (bmt.table[i].mapped_index == bad_index) {
  633. orig_bad_block = bmt.table[i].bad_index;
  634. break;
  635. }
  636. }
  637. dprintf(INFO, "Mapped block becomes bad, orig bad block is 0x%x\n", orig_bad_block);
  638. bmt.table[i].mapped_index = map_index;
  639. } else {
  640. bmt.table[bmt.mapped_count].mapped_index = map_index;
  641. bmt.table[bmt.mapped_count].bad_index = bad_index;
  642. bmt.mapped_count++;
  643. }
  644. memset(oob_buf, 0xFF, sizeof(oob_buf));
  645. fill_nand_bmt_buffer(&bmt, dat_buf, oob_buf);
  646. if (!write_bmt_to_flash(dat_buf, oob_buf))
  647. return false;
  648. mark_block_bad_bmt(offset);
  649. return true;
  650. }
  651. /*******************************************************************
  652. * [BMT Interface]
  653. *
  654. * Description:
  655. * Given an block index, return mapped index if it's mapped, else
  656. * return given index.
  657. *
  658. * Parameter:
  659. * index: given an block index. This value cannot exceed
  660. * system_block_count.
  661. *
  662. * Return NULL for failure
  663. *******************************************************************/
  664. u16 get_mapping_block_index(u32 index)
  665. {
  666. int i;
  667. if (index > system_block_count) {
  668. dprintf(INFO, "Given index exceed: 0x%x > 0x%x\n", index, system_block_count);
  669. return index;
  670. }
  671. for (i = 0; i < bmt.mapped_count; i++) {
  672. if (bmt.table[i].bad_index == index) {
  673. dprintf(INFO, "Redirect 0x%x to 0x%x\n", index, bmt.table[i].mapped_index);
  674. return bmt.table[i].mapped_index;
  675. }
  676. }
  677. return index;
  678. }