2 * NAND Flash Controller Device Driver
3 * Copyright (c) 2009, Intel Corporation and its suppliers.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/slab.h>
31 #define BLK_FROM_ADDR(addr) ((u32)(addr >> DeviceInfo.nBitsInBlockDataSize))
32 #define PAGE_FROM_ADDR(addr, Block) ((u16)((addr - (u64)Block * \
33 DeviceInfo.wBlockDataSize) >> DeviceInfo.nBitsInPageDataSize))
35 #define IS_SPARE_BLOCK(blk) (BAD_BLOCK != (pbt[blk] &\
36 BAD_BLOCK) && SPARE_BLOCK == (pbt[blk] & SPARE_BLOCK))
38 #define IS_DATA_BLOCK(blk) (0 == (pbt[blk] & BAD_BLOCK))
40 #define IS_DISCARDED_BLOCK(blk) (BAD_BLOCK != (pbt[blk] &\
41 BAD_BLOCK) && DISCARD_BLOCK == (pbt[blk] & DISCARD_BLOCK))
43 #define IS_BAD_BLOCK(blk) (BAD_BLOCK == (pbt[blk] & BAD_BLOCK))
46 void debug_boundary_lineno_error(int chnl, int limit, int no,
47 int lineno, char *filename)
50 printk(KERN_ERR "Boundary Check Fail value %d >= limit %d, "
51 "at %s:%d. Other info:%d. Aborting...\n",
52 chnl, limit, filename, lineno, no);
54 /* static int globalmemsize; */
57 static u16 FTL_Cache_If_Hit(u64 dwPageAddr);
58 static int FTL_Cache_Read(u64 dwPageAddr);
59 static void FTL_Cache_Read_Page(u8 *pData, u64 dwPageAddr,
61 static void FTL_Cache_Write_Page(u8 *pData, u64 dwPageAddr,
62 u8 cache_blk, u16 flag);
63 static int FTL_Cache_Write(void);
64 static void FTL_Calculate_LRU(void);
65 static u32 FTL_Get_Block_Index(u32 wBlockNum);
67 static int FTL_Search_Block_Table_IN_Block(u32 BT_Block,
68 u8 BT_Tag, u16 *Page);
69 static int FTL_Read_Block_Table(void);
70 static int FTL_Write_Block_Table(int wForce);
71 static int FTL_Write_Block_Table_Data(void);
72 static int FTL_Check_Block_Table(int wOldTable);
73 static int FTL_Static_Wear_Leveling(void);
74 static u32 FTL_Replace_Block_Table(void);
75 static int FTL_Write_IN_Progress_Block_Table_Page(void);
77 static u32 FTL_Get_Page_Num(u64 length);
78 static u64 FTL_Get_Physical_Block_Addr(u64 blk_addr);
80 static u32 FTL_Replace_OneBlock(u32 wBlockNum,
82 static u32 FTL_Replace_LWBlock(u32 wBlockNum,
83 int *pGarbageCollect);
84 static u32 FTL_Replace_MWBlock(void);
85 static int FTL_Replace_Block(u64 blk_addr);
86 static int FTL_Adjust_Relative_Erase_Count(u32 Index_of_MAX);
88 struct device_info_tag DeviceInfo;
89 struct flash_cache_tag Cache;
90 static struct spectra_l2_cache_info cache_l2;
92 static u8 *cache_l2_page_buf;
93 static u8 *cache_l2_blk_buf;
99 static u16 g_wBlockTableOffset;
100 static u32 g_wBlockTableIndex;
101 static u8 g_cBlockTableStatus;
103 static u8 *g_pTempBuf;
104 static u8 *flag_check_blk_table;
105 static u8 *tmp_buf_search_bt_in_block;
106 static u8 *spare_buf_search_bt_in_block;
107 static u8 *spare_buf_bt_search_bt_in_block;
108 static u8 *tmp_buf1_read_blk_table;
109 static u8 *tmp_buf2_read_blk_table;
110 static u8 *flags_static_wear_leveling;
111 static u8 *tmp_buf_write_blk_table_data;
112 static u8 *tmp_buf_read_disturbance;
114 u8 *buf_read_page_main_spare;
115 u8 *buf_write_page_main_spare;
116 u8 *buf_read_page_spare;
117 u8 *buf_get_bad_block;
119 #if (RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE && CMD_DMA)
120 struct flash_cache_delta_list_tag int_cache[MAX_CHANS + MAX_DESCS];
121 struct flash_cache_tag cache_start_copy;
124 int g_wNumFreeBlocks;
128 static u8 bt_flag = FIRST_BT_ID;
129 static u8 bt_block_changed;
131 static u16 cache_block_to_write;
132 static u8 last_erased = FIRST_BT_ID;
135 static u8 BT_GC_Called;
138 #define COPY_BACK_BUF_NUM 10
140 static u8 ftl_cmd_cnt; /* Init value is 0 */
143 u8 *g_pBTStartingCopy;
144 u8 *g_pWearCounterCopy;
145 u16 *g_pReadCounterCopy;
146 u8 *g_pBlockTableCopies;
147 u8 *g_pNextBlockTable;
148 static u8 *cp_back_buf_copies[COPY_BACK_BUF_NUM];
149 static int cp_back_buf_idx;
151 static u8 *g_temp_buf;
153 #pragma pack(push, 1)
155 struct BTableChangesDelta {
158 u16 g_wBlockTableOffset;
159 u32 g_wBlockTableIndex;
170 struct BTableChangesDelta *p_BTableChangesDelta;
174 #define MARK_BLOCK_AS_BAD(blocknode) (blocknode |= BAD_BLOCK)
175 #define MARK_BLK_AS_DISCARD(blk) (blk = (blk & ~SPARE_BLOCK) | DISCARD_BLOCK)
177 #define FTL_Get_LBAPBA_Table_Mem_Size_Bytes() (DeviceInfo.wDataBlockNum *\
179 #define FTL_Get_WearCounter_Table_Mem_Size_Bytes() (DeviceInfo.wDataBlockNum *\
181 #define FTL_Get_ReadCounter_Table_Mem_Size_Bytes() (DeviceInfo.wDataBlockNum *\
183 #if SUPPORT_LARGE_BLOCKNUM
184 #define FTL_Get_LBAPBA_Table_Flash_Size_Bytes() (DeviceInfo.wDataBlockNum *\
187 #define FTL_Get_LBAPBA_Table_Flash_Size_Bytes() (DeviceInfo.wDataBlockNum *\
190 #define FTL_Get_WearCounter_Table_Flash_Size_Bytes \
191 FTL_Get_WearCounter_Table_Mem_Size_Bytes
192 #define FTL_Get_ReadCounter_Table_Flash_Size_Bytes \
193 FTL_Get_ReadCounter_Table_Mem_Size_Bytes
195 static u32 FTL_Get_Block_Table_Flash_Size_Bytes(void)
199 if (DeviceInfo.MLCDevice) {
200 byte_num = FTL_Get_LBAPBA_Table_Flash_Size_Bytes() +
201 DeviceInfo.wDataBlockNum * sizeof(u8) +
202 DeviceInfo.wDataBlockNum * sizeof(u16);
204 byte_num = FTL_Get_LBAPBA_Table_Flash_Size_Bytes() +
205 DeviceInfo.wDataBlockNum * sizeof(u8);
208 byte_num += 4 * sizeof(u8);
213 static u16 FTL_Get_Block_Table_Flash_Size_Pages(void)
215 return (u16)FTL_Get_Page_Num(FTL_Get_Block_Table_Flash_Size_Bytes());
218 static int FTL_Copy_Block_Table_To_Flash(u8 *flashBuf, u32 sizeToTx,
221 u32 wBytesCopied, blk_tbl_size, wBytes;
222 u32 *pbt = (u32 *)g_pBlockTable;
224 blk_tbl_size = FTL_Get_LBAPBA_Table_Flash_Size_Bytes();
226 (wBytes < sizeToTx) && ((wBytes + sizeTxed) < blk_tbl_size);
228 #if SUPPORT_LARGE_BLOCKNUM
229 flashBuf[wBytes] = (u8)(pbt[(wBytes + sizeTxed) / 3]
230 >> (((wBytes + sizeTxed) % 3) ?
231 ((((wBytes + sizeTxed) % 3) == 2) ? 0 : 8) : 16)) & 0xFF;
233 flashBuf[wBytes] = (u8)(pbt[(wBytes + sizeTxed) / 2]
234 >> (((wBytes + sizeTxed) % 2) ? 0 : 8)) & 0xFF;
238 sizeTxed = (sizeTxed > blk_tbl_size) ? (sizeTxed - blk_tbl_size) : 0;
239 blk_tbl_size = FTL_Get_WearCounter_Table_Flash_Size_Bytes();
240 wBytesCopied = wBytes;
241 wBytes = ((blk_tbl_size - sizeTxed) > (sizeToTx - wBytesCopied)) ?
242 (sizeToTx - wBytesCopied) : (blk_tbl_size - sizeTxed);
243 memcpy(flashBuf + wBytesCopied, g_pWearCounter + sizeTxed, wBytes);
245 sizeTxed = (sizeTxed > blk_tbl_size) ? (sizeTxed - blk_tbl_size) : 0;
247 if (DeviceInfo.MLCDevice) {
248 blk_tbl_size = FTL_Get_ReadCounter_Table_Flash_Size_Bytes();
249 wBytesCopied += wBytes;
250 for (wBytes = 0; ((wBytes + wBytesCopied) < sizeToTx) &&
251 ((wBytes + sizeTxed) < blk_tbl_size); wBytes++)
252 flashBuf[wBytes + wBytesCopied] =
253 (g_pReadCounter[(wBytes + sizeTxed) / 2] >>
254 (((wBytes + sizeTxed) % 2) ? 0 : 8)) & 0xFF;
257 return wBytesCopied + wBytes;
260 static int FTL_Copy_Block_Table_From_Flash(u8 *flashBuf,
261 u32 sizeToTx, u32 sizeTxed)
263 u32 wBytesCopied, blk_tbl_size, wBytes;
264 u32 *pbt = (u32 *)g_pBlockTable;
266 blk_tbl_size = FTL_Get_LBAPBA_Table_Flash_Size_Bytes();
267 for (wBytes = 0; (wBytes < sizeToTx) &&
268 ((wBytes + sizeTxed) < blk_tbl_size); wBytes++) {
269 #if SUPPORT_LARGE_BLOCKNUM
270 if (!((wBytes + sizeTxed) % 3))
271 pbt[(wBytes + sizeTxed) / 3] = 0;
272 pbt[(wBytes + sizeTxed) / 3] |=
273 (flashBuf[wBytes] << (((wBytes + sizeTxed) % 3) ?
274 ((((wBytes + sizeTxed) % 3) == 2) ? 0 : 8) : 16));
276 if (!((wBytes + sizeTxed) % 2))
277 pbt[(wBytes + sizeTxed) / 2] = 0;
278 pbt[(wBytes + sizeTxed) / 2] |=
279 (flashBuf[wBytes] << (((wBytes + sizeTxed) % 2) ?
284 sizeTxed = (sizeTxed > blk_tbl_size) ? (sizeTxed - blk_tbl_size) : 0;
285 blk_tbl_size = FTL_Get_WearCounter_Table_Flash_Size_Bytes();
286 wBytesCopied = wBytes;
287 wBytes = ((blk_tbl_size - sizeTxed) > (sizeToTx - wBytesCopied)) ?
288 (sizeToTx - wBytesCopied) : (blk_tbl_size - sizeTxed);
289 memcpy(g_pWearCounter + sizeTxed, flashBuf + wBytesCopied, wBytes);
290 sizeTxed = (sizeTxed > blk_tbl_size) ? (sizeTxed - blk_tbl_size) : 0;
292 if (DeviceInfo.MLCDevice) {
293 wBytesCopied += wBytes;
294 blk_tbl_size = FTL_Get_ReadCounter_Table_Flash_Size_Bytes();
295 for (wBytes = 0; ((wBytes + wBytesCopied) < sizeToTx) &&
296 ((wBytes + sizeTxed) < blk_tbl_size); wBytes++) {
297 if (((wBytes + sizeTxed) % 2))
298 g_pReadCounter[(wBytes + sizeTxed) / 2] = 0;
299 g_pReadCounter[(wBytes + sizeTxed) / 2] |=
301 (((wBytes + sizeTxed) % 2) ? 0 : 8));
305 return wBytesCopied+wBytes;
308 static int FTL_Insert_Block_Table_Signature(u8 *buf, u8 tag)
312 for (i = 0; i < BTSIG_BYTES; i++)
313 buf[BTSIG_OFFSET + i] =
314 ((tag + (i * BTSIG_DELTA) - FIRST_BT_ID) %
315 (1 + LAST_BT_ID-FIRST_BT_ID)) + FIRST_BT_ID;
320 static int FTL_Extract_Block_Table_Tag(u8 *buf, u8 **tagarray)
322 static u8 tag[BTSIG_BYTES >> 1];
323 int i, j, k, tagi, tagtemp, status;
325 *tagarray = (u8 *)tag;
328 for (i = 0; i < (BTSIG_BYTES - 1); i++) {
329 for (j = i + 1; (j < BTSIG_BYTES) &&
330 (tagi < (BTSIG_BYTES >> 1)); j++) {
331 tagtemp = buf[BTSIG_OFFSET + j] -
332 buf[BTSIG_OFFSET + i];
333 if (tagtemp && !(tagtemp % BTSIG_DELTA)) {
334 tagtemp = (buf[BTSIG_OFFSET + i] +
335 (1 + LAST_BT_ID - FIRST_BT_ID) -
337 (1 + LAST_BT_ID - FIRST_BT_ID);
339 for (k = 0; k < tagi; k++) {
340 if (tagtemp == tag[k])
344 if (status == FAIL) {
345 tag[tagi++] = tagtemp;
346 i = (j == (i + 1)) ? i + 1 : i;
347 j = (j == (i + 1)) ? i + 1 : i;
357 static int FTL_Execute_SPL_Recovery(void)
360 u32 *pbt = (u32 *)g_pBlockTable;
363 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
364 __FILE__, __LINE__, __func__);
366 blks = DeviceInfo.wSpectraEndBlock - DeviceInfo.wSpectraStartBlock;
367 for (j = 0; j <= blks; j++) {
369 if (((block & BAD_BLOCK) != BAD_BLOCK) &&
370 ((block & SPARE_BLOCK) == SPARE_BLOCK)) {
371 ret = GLOB_LLD_Erase_Block(block & ~BAD_BLOCK);
373 nand_dbg_print(NAND_DBG_WARN,
374 "NAND Program fail in %s, Line %d, "
375 "Function: %s, new Bad Block %d "
377 __FILE__, __LINE__, __func__,
378 (int)(block & ~BAD_BLOCK));
379 MARK_BLOCK_AS_BAD(pbt[j]);
387 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
388 * Function: GLOB_FTL_IdentifyDevice
389 * Inputs: pointer to identify data structure
390 * Outputs: PASS / FAIL
391 * Description: the identify data structure is filled in with
392 * information for the block driver.
393 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
394 int GLOB_FTL_IdentifyDevice(struct spectra_indentfy_dev_tag *dev_data)
396 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
397 __FILE__, __LINE__, __func__);
399 dev_data->NumBlocks = DeviceInfo.wTotalBlocks;
400 dev_data->PagesPerBlock = DeviceInfo.wPagesPerBlock;
401 dev_data->PageDataSize = DeviceInfo.wPageDataSize;
402 dev_data->wECCBytesPerSector = DeviceInfo.wECCBytesPerSector;
403 dev_data->wDataBlockNum = DeviceInfo.wDataBlockNum;
409 static int allocate_memory(void)
411 u32 block_table_size, page_size, block_size, mem_size;
418 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
419 __FILE__, __LINE__, __func__);
421 page_size = DeviceInfo.wPageSize;
422 block_size = DeviceInfo.wPagesPerBlock * DeviceInfo.wPageDataSize;
424 block_table_size = DeviceInfo.wDataBlockNum *
425 (sizeof(u32) + sizeof(u8) + sizeof(u16));
426 block_table_size += (DeviceInfo.wPageDataSize -
427 (block_table_size % DeviceInfo.wPageDataSize)) %
428 DeviceInfo.wPageDataSize;
430 /* Malloc memory for block tables */
431 g_pBlockTable = kmalloc(block_table_size, GFP_ATOMIC);
433 goto block_table_fail;
434 memset(g_pBlockTable, 0, block_table_size);
435 total_bytes += block_table_size;
437 g_pWearCounter = (u8 *)(g_pBlockTable +
438 DeviceInfo.wDataBlockNum * sizeof(u32));
440 if (DeviceInfo.MLCDevice)
441 g_pReadCounter = (u16 *)(g_pBlockTable +
442 DeviceInfo.wDataBlockNum *
443 (sizeof(u32) + sizeof(u8)));
445 /* Malloc memory and init for cache items */
446 for (i = 0; i < CACHE_ITEM_NUM; i++) {
447 Cache.array[i].address = NAND_CACHE_INIT_ADDR;
448 Cache.array[i].use_cnt = 0;
449 Cache.array[i].changed = CLEAR;
450 Cache.array[i].buf = kmalloc(Cache.cache_item_size,
452 if (!Cache.array[i].buf)
453 goto cache_item_fail;
454 memset(Cache.array[i].buf, 0, Cache.cache_item_size);
455 total_bytes += Cache.cache_item_size;
458 /* Malloc memory for IPF */
459 g_pIPF = kmalloc(page_size, GFP_ATOMIC);
462 memset(g_pIPF, 0, page_size);
463 total_bytes += page_size;
465 /* Malloc memory for data merging during Level2 Cache flush */
466 cache_l2_page_buf = kmalloc(page_size, GFP_ATOMIC);
467 if (!cache_l2_page_buf)
468 goto cache_l2_page_buf_fail;
469 memset(cache_l2_page_buf, 0xff, page_size);
470 total_bytes += page_size;
472 cache_l2_blk_buf = kmalloc(block_size, GFP_ATOMIC);
473 if (!cache_l2_blk_buf)
474 goto cache_l2_blk_buf_fail;
475 memset(cache_l2_blk_buf, 0xff, block_size);
476 total_bytes += block_size;
478 /* Malloc memory for temp buffer */
479 g_pTempBuf = kmalloc(Cache.cache_item_size, GFP_ATOMIC);
482 memset(g_pTempBuf, 0, Cache.cache_item_size);
483 total_bytes += Cache.cache_item_size;
485 /* Malloc memory for block table blocks */
486 mem_size = (1 + LAST_BT_ID - FIRST_BT_ID) * sizeof(u32);
487 g_pBTBlocks = kmalloc(mem_size, GFP_ATOMIC);
490 memset(g_pBTBlocks, 0xff, mem_size);
491 total_bytes += mem_size;
493 /* Malloc memory for function FTL_Check_Block_Table */
494 flag_check_blk_table = kmalloc(DeviceInfo.wDataBlockNum, GFP_ATOMIC);
495 if (!flag_check_blk_table)
496 goto flag_check_blk_table_fail;
497 total_bytes += DeviceInfo.wDataBlockNum;
499 /* Malloc memory for function FTL_Search_Block_Table_IN_Block */
500 tmp_buf_search_bt_in_block = kmalloc(page_size, GFP_ATOMIC);
501 if (!tmp_buf_search_bt_in_block)
502 goto tmp_buf_search_bt_in_block_fail;
503 memset(tmp_buf_search_bt_in_block, 0xff, page_size);
504 total_bytes += page_size;
506 mem_size = DeviceInfo.wPageSize - DeviceInfo.wPageDataSize;
507 spare_buf_search_bt_in_block = kmalloc(mem_size, GFP_ATOMIC);
508 if (!spare_buf_search_bt_in_block)
509 goto spare_buf_search_bt_in_block_fail;
510 memset(spare_buf_search_bt_in_block, 0xff, mem_size);
511 total_bytes += mem_size;
513 spare_buf_bt_search_bt_in_block = kmalloc(mem_size, GFP_ATOMIC);
514 if (!spare_buf_bt_search_bt_in_block)
515 goto spare_buf_bt_search_bt_in_block_fail;
516 memset(spare_buf_bt_search_bt_in_block, 0xff, mem_size);
517 total_bytes += mem_size;
519 /* Malloc memory for function FTL_Read_Block_Table */
520 tmp_buf1_read_blk_table = kmalloc(page_size, GFP_ATOMIC);
521 if (!tmp_buf1_read_blk_table)
522 goto tmp_buf1_read_blk_table_fail;
523 memset(tmp_buf1_read_blk_table, 0xff, page_size);
524 total_bytes += page_size;
526 tmp_buf2_read_blk_table = kmalloc(page_size, GFP_ATOMIC);
527 if (!tmp_buf2_read_blk_table)
528 goto tmp_buf2_read_blk_table_fail;
529 memset(tmp_buf2_read_blk_table, 0xff, page_size);
530 total_bytes += page_size;
532 /* Malloc memory for function FTL_Static_Wear_Leveling */
533 flags_static_wear_leveling = kmalloc(DeviceInfo.wDataBlockNum,
535 if (!flags_static_wear_leveling)
536 goto flags_static_wear_leveling_fail;
537 total_bytes += DeviceInfo.wDataBlockNum;
539 /* Malloc memory for function FTL_Write_Block_Table_Data */
540 if (FTL_Get_Block_Table_Flash_Size_Pages() > 3)
541 mem_size = FTL_Get_Block_Table_Flash_Size_Bytes() -
542 2 * DeviceInfo.wPageSize;
544 mem_size = DeviceInfo.wPageSize;
545 tmp_buf_write_blk_table_data = kmalloc(mem_size, GFP_ATOMIC);
546 if (!tmp_buf_write_blk_table_data)
547 goto tmp_buf_write_blk_table_data_fail;
548 memset(tmp_buf_write_blk_table_data, 0xff, mem_size);
549 total_bytes += mem_size;
551 /* Malloc memory for function FTL_Read_Disturbance */
552 tmp_buf_read_disturbance = kmalloc(block_size, GFP_ATOMIC);
553 if (!tmp_buf_read_disturbance)
554 goto tmp_buf_read_disturbance_fail;
555 memset(tmp_buf_read_disturbance, 0xff, block_size);
556 total_bytes += block_size;
558 /* Alloc mem for function NAND_Read_Page_Main_Spare of lld_nand.c */
559 buf_read_page_main_spare = kmalloc(DeviceInfo.wPageSize, GFP_ATOMIC);
560 if (!buf_read_page_main_spare)
561 goto buf_read_page_main_spare_fail;
562 total_bytes += DeviceInfo.wPageSize;
564 /* Alloc mem for function NAND_Write_Page_Main_Spare of lld_nand.c */
565 buf_write_page_main_spare = kmalloc(DeviceInfo.wPageSize, GFP_ATOMIC);
566 if (!buf_write_page_main_spare)
567 goto buf_write_page_main_spare_fail;
568 total_bytes += DeviceInfo.wPageSize;
570 /* Alloc mem for function NAND_Read_Page_Spare of lld_nand.c */
571 buf_read_page_spare = kmalloc(DeviceInfo.wPageSpareSize, GFP_ATOMIC);
572 if (!buf_read_page_spare)
573 goto buf_read_page_spare_fail;
574 memset(buf_read_page_spare, 0xff, DeviceInfo.wPageSpareSize);
575 total_bytes += DeviceInfo.wPageSpareSize;
577 /* Alloc mem for function NAND_Get_Bad_Block of lld_nand.c */
578 buf_get_bad_block = kmalloc(DeviceInfo.wPageSpareSize, GFP_ATOMIC);
579 if (!buf_get_bad_block)
580 goto buf_get_bad_block_fail;
581 memset(buf_get_bad_block, 0xff, DeviceInfo.wPageSpareSize);
582 total_bytes += DeviceInfo.wPageSpareSize;
585 g_temp_buf = kmalloc(block_size, GFP_ATOMIC);
588 memset(g_temp_buf, 0xff, block_size);
589 total_bytes += block_size;
591 /* Malloc memory for copy of block table used in CDMA mode */
592 g_pBTStartingCopy = kmalloc(block_table_size, GFP_ATOMIC);
593 if (!g_pBTStartingCopy)
594 goto bt_starting_copy;
595 memset(g_pBTStartingCopy, 0, block_table_size);
596 total_bytes += block_table_size;
598 g_pWearCounterCopy = (u8 *)(g_pBTStartingCopy +
599 DeviceInfo.wDataBlockNum * sizeof(u32));
601 if (DeviceInfo.MLCDevice)
602 g_pReadCounterCopy = (u16 *)(g_pBTStartingCopy +
603 DeviceInfo.wDataBlockNum *
604 (sizeof(u32) + sizeof(u8)));
606 /* Malloc memory for block table copies */
607 mem_size = 5 * DeviceInfo.wDataBlockNum * sizeof(u32) +
608 5 * DeviceInfo.wDataBlockNum * sizeof(u8);
609 if (DeviceInfo.MLCDevice)
610 mem_size += 5 * DeviceInfo.wDataBlockNum * sizeof(u16);
611 g_pBlockTableCopies = kmalloc(mem_size, GFP_ATOMIC);
612 if (!g_pBlockTableCopies)
613 goto blk_table_copies_fail;
614 memset(g_pBlockTableCopies, 0, mem_size);
615 total_bytes += mem_size;
616 g_pNextBlockTable = g_pBlockTableCopies;
618 /* Malloc memory for Block Table Delta */
619 mem_size = MAX_DESCS * sizeof(struct BTableChangesDelta);
620 g_pBTDelta = kmalloc(mem_size, GFP_ATOMIC);
623 memset(g_pBTDelta, 0, mem_size);
624 total_bytes += mem_size;
625 g_pBTDelta_Free = g_pBTDelta;
627 /* Malloc memory for Copy Back Buffers */
628 for (j = 0; j < COPY_BACK_BUF_NUM; j++) {
629 cp_back_buf_copies[j] = kmalloc(block_size, GFP_ATOMIC);
630 if (!cp_back_buf_copies[j])
631 goto cp_back_buf_copies_fail;
632 memset(cp_back_buf_copies[j], 0, block_size);
633 total_bytes += block_size;
637 /* Malloc memory for pending commands list */
638 mem_size = sizeof(struct pending_cmd) * MAX_DESCS;
639 info.pcmds = kzalloc(mem_size, GFP_KERNEL);
641 goto pending_cmds_buf_fail;
642 total_bytes += mem_size;
644 /* Malloc memory for CDMA descripter table */
645 mem_size = sizeof(struct cdma_descriptor) * MAX_DESCS;
646 info.cdma_desc_buf = kzalloc(mem_size, GFP_KERNEL);
647 if (!info.cdma_desc_buf)
648 goto cdma_desc_buf_fail;
649 total_bytes += mem_size;
651 /* Malloc memory for Memcpy descripter table */
652 mem_size = sizeof(struct memcpy_descriptor) * MAX_DESCS;
653 info.memcp_desc_buf = kzalloc(mem_size, GFP_KERNEL);
654 if (!info.memcp_desc_buf)
655 goto memcp_desc_buf_fail;
656 total_bytes += mem_size;
659 nand_dbg_print(NAND_DBG_WARN,
660 "Total memory allocated in FTL layer: %d\n", total_bytes);
666 kfree(info.cdma_desc_buf);
669 pending_cmds_buf_fail:
670 cp_back_buf_copies_fail:
673 kfree(cp_back_buf_copies[j]);
676 kfree(g_pBlockTableCopies);
677 blk_table_copies_fail:
678 kfree(g_pBTStartingCopy);
682 kfree(buf_get_bad_block);
685 buf_get_bad_block_fail:
686 kfree(buf_read_page_spare);
687 buf_read_page_spare_fail:
688 kfree(buf_write_page_main_spare);
689 buf_write_page_main_spare_fail:
690 kfree(buf_read_page_main_spare);
691 buf_read_page_main_spare_fail:
692 kfree(tmp_buf_read_disturbance);
693 tmp_buf_read_disturbance_fail:
694 kfree(tmp_buf_write_blk_table_data);
695 tmp_buf_write_blk_table_data_fail:
696 kfree(flags_static_wear_leveling);
697 flags_static_wear_leveling_fail:
698 kfree(tmp_buf2_read_blk_table);
699 tmp_buf2_read_blk_table_fail:
700 kfree(tmp_buf1_read_blk_table);
701 tmp_buf1_read_blk_table_fail:
702 kfree(spare_buf_bt_search_bt_in_block);
703 spare_buf_bt_search_bt_in_block_fail:
704 kfree(spare_buf_search_bt_in_block);
705 spare_buf_search_bt_in_block_fail:
706 kfree(tmp_buf_search_bt_in_block);
707 tmp_buf_search_bt_in_block_fail:
708 kfree(flag_check_blk_table);
709 flag_check_blk_table_fail:
714 kfree(cache_l2_blk_buf);
715 cache_l2_blk_buf_fail:
716 kfree(cache_l2_page_buf);
717 cache_l2_page_buf_fail:
723 kfree(Cache.array[i].buf);
724 kfree(g_pBlockTable);
726 printk(KERN_ERR "Failed to kmalloc memory in %s Line %d.\n",
733 static int free_memory(void)
738 kfree(info.memcp_desc_buf);
739 kfree(info.cdma_desc_buf);
741 for (i = COPY_BACK_BUF_NUM - 1; i >= 0; i--)
742 kfree(cp_back_buf_copies[i]);
744 kfree(g_pBlockTableCopies);
745 kfree(g_pBTStartingCopy);
747 kfree(buf_get_bad_block);
749 kfree(buf_read_page_spare);
750 kfree(buf_write_page_main_spare);
751 kfree(buf_read_page_main_spare);
752 kfree(tmp_buf_read_disturbance);
753 kfree(tmp_buf_write_blk_table_data);
754 kfree(flags_static_wear_leveling);
755 kfree(tmp_buf2_read_blk_table);
756 kfree(tmp_buf1_read_blk_table);
757 kfree(spare_buf_bt_search_bt_in_block);
758 kfree(spare_buf_search_bt_in_block);
759 kfree(tmp_buf_search_bt_in_block);
760 kfree(flag_check_blk_table);
764 for (i = CACHE_ITEM_NUM - 1; i >= 0; i--)
765 kfree(Cache.array[i].buf);
766 kfree(g_pBlockTable);
771 static void dump_cache_l2_table(void)
774 struct spectra_l2_cache_list *pnd;
778 list_for_each(p, &cache_l2.table.list) {
779 pnd = list_entry(p, struct spectra_l2_cache_list, list);
780 nand_dbg_print(NAND_DBG_WARN, "dump_cache_l2_table node: %d, logical_blk_num: %d\n", n, pnd->logical_blk_num);
782 for (i = 0; i < DeviceInfo.wPagesPerBlock; i++) {
783 if (pnd->pages_array[i] != MAX_U32_VALUE)
784 nand_dbg_print(NAND_DBG_WARN, " pages_array[%d]: 0x%x\n", i, pnd->pages_array[i]);
791 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
792 * Function: GLOB_FTL_Init
794 * Outputs: PASS=0 / FAIL=1
795 * Description: allocates the memory for cache array,
796 * important data structures
797 * clears the cache array
798 * reads the block table from flash into array
799 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
800 int GLOB_FTL_Init(void)
804 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
805 __FILE__, __LINE__, __func__);
807 Cache.pages_per_item = 1;
808 Cache.cache_item_size = 1 * DeviceInfo.wPageDataSize;
810 if (allocate_memory() != PASS)
814 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
815 memcpy((void *)&cache_start_copy, (void *)&Cache,
816 sizeof(struct flash_cache_tag));
817 memset((void *)&int_cache, -1,
818 sizeof(struct flash_cache_delta_list_tag) *
819 (MAX_CHANS + MAX_DESCS));
824 if (FTL_Read_Block_Table() != PASS)
827 /* Init the Level2 Cache data structure */
828 for (i = 0; i < BLK_NUM_FOR_L2_CACHE; i++)
829 cache_l2.blk_array[i] = MAX_U32_VALUE;
830 cache_l2.cur_blk_idx = 0;
831 cache_l2.cur_page_num = 0;
832 INIT_LIST_HEAD(&cache_l2.table.list);
833 cache_l2.table.logical_blk_num = MAX_U32_VALUE;
835 dump_cache_l2_table();
843 static void save_blk_table_changes(u16 idx)
846 u32 *pbt = (u32 *)g_pBTStartingCopy;
848 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
852 id = idx - MAX_CHANS;
853 if (int_cache[id].item != -1) {
854 cache_blks = int_cache[id].item;
855 cache_start_copy.array[cache_blks].address =
856 int_cache[id].cache.address;
857 cache_start_copy.array[cache_blks].changed =
858 int_cache[id].cache.changed;
862 ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
864 while (ftl_cmd <= PendingCMD[idx].Tag) {
865 if (p_BTableChangesDelta->ValidFields == 0x01) {
866 g_wBlockTableOffset =
867 p_BTableChangesDelta->g_wBlockTableOffset;
868 } else if (p_BTableChangesDelta->ValidFields == 0x0C) {
869 pbt[p_BTableChangesDelta->BT_Index] =
870 p_BTableChangesDelta->BT_Entry_Value;
871 debug_boundary_error(((
872 p_BTableChangesDelta->BT_Index)),
873 DeviceInfo.wDataBlockNum, 0);
874 } else if (p_BTableChangesDelta->ValidFields == 0x03) {
875 g_wBlockTableOffset =
876 p_BTableChangesDelta->g_wBlockTableOffset;
878 p_BTableChangesDelta->g_wBlockTableIndex;
879 } else if (p_BTableChangesDelta->ValidFields == 0x30) {
880 g_pWearCounterCopy[p_BTableChangesDelta->WC_Index] =
881 p_BTableChangesDelta->WC_Entry_Value;
882 } else if ((DeviceInfo.MLCDevice) &&
883 (p_BTableChangesDelta->ValidFields == 0xC0)) {
884 g_pReadCounterCopy[p_BTableChangesDelta->RC_Index] =
885 p_BTableChangesDelta->RC_Entry_Value;
886 nand_dbg_print(NAND_DBG_DEBUG,
887 "In event status setting read counter "
888 "GLOB_ftl_cmd_cnt %u Count %u Index %u\n",
890 p_BTableChangesDelta->RC_Entry_Value,
891 (unsigned int)p_BTableChangesDelta->RC_Index);
893 nand_dbg_print(NAND_DBG_DEBUG,
894 "This should never occur \n");
896 p_BTableChangesDelta += 1;
897 ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
901 static void discard_cmds(u16 n)
903 u32 *pbt = (u32 *)g_pBTStartingCopy;
906 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
911 if ((PendingCMD[n].CMD == WRITE_MAIN_CMD) ||
912 (PendingCMD[n].CMD == WRITE_MAIN_SPARE_CMD)) {
913 for (k = 0; k < DeviceInfo.wDataBlockNum; k++) {
914 if (PendingCMD[n].Block == (pbt[k] & (~BAD_BLOCK)))
915 MARK_BLK_AS_DISCARD(pbt[k]);
919 ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
920 while (ftl_cmd <= PendingCMD[n].Tag) {
921 p_BTableChangesDelta += 1;
922 ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
925 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
928 if (int_cache[id].item != -1) {
929 cache_blks = int_cache[id].item;
930 if (PendingCMD[n].CMD == MEMCOPY_CMD) {
931 if ((cache_start_copy.array[cache_blks].buf <=
932 PendingCMD[n].DataDestAddr) &&
933 ((cache_start_copy.array[cache_blks].buf +
934 Cache.cache_item_size) >
935 PendingCMD[n].DataDestAddr)) {
936 cache_start_copy.array[cache_blks].address =
937 NAND_CACHE_INIT_ADDR;
938 cache_start_copy.array[cache_blks].use_cnt =
940 cache_start_copy.array[cache_blks].changed =
944 cache_start_copy.array[cache_blks].address =
945 int_cache[id].cache.address;
946 cache_start_copy.array[cache_blks].changed =
947 int_cache[id].cache.changed;
953 static void process_cmd_pass(int *first_failed_cmd, u16 idx)
955 if (0 == *first_failed_cmd)
956 save_blk_table_changes(idx);
961 static void process_cmd_fail_abort(int *first_failed_cmd,
964 u32 *pbt = (u32 *)g_pBTStartingCopy;
967 int erase_fail, program_fail;
968 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
973 if (0 == *first_failed_cmd)
974 *first_failed_cmd = PendingCMD[idx].SBDCmdIndex;
976 nand_dbg_print(NAND_DBG_DEBUG, "Uncorrectable error has occured "
977 "while executing %u Command %u accesing Block %u\n",
978 (unsigned int)p_BTableChangesDelta->ftl_cmd_cnt,
980 (unsigned int)PendingCMD[idx].Block);
982 ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
983 while (ftl_cmd <= PendingCMD[idx].Tag) {
984 p_BTableChangesDelta += 1;
985 ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
988 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
989 id = idx - MAX_CHANS;
991 if (int_cache[id].item != -1) {
992 cache_blks = int_cache[id].item;
993 if ((PendingCMD[idx].CMD == WRITE_MAIN_CMD)) {
994 cache_start_copy.array[cache_blks].address =
995 int_cache[id].cache.address;
996 cache_start_copy.array[cache_blks].changed = SET;
997 } else if ((PendingCMD[idx].CMD == READ_MAIN_CMD)) {
998 cache_start_copy.array[cache_blks].address =
999 NAND_CACHE_INIT_ADDR;
1000 cache_start_copy.array[cache_blks].use_cnt = 0;
1001 cache_start_copy.array[cache_blks].changed =
1003 } else if (PendingCMD[idx].CMD == ERASE_CMD) {
1005 } else if (PendingCMD[idx].CMD == MEMCOPY_CMD) {
1011 erase_fail = (event == EVENT_ERASE_FAILURE) &&
1012 (PendingCMD[idx].CMD == ERASE_CMD);
1014 program_fail = (event == EVENT_PROGRAM_FAILURE) &&
1015 ((PendingCMD[idx].CMD == WRITE_MAIN_CMD) ||
1016 (PendingCMD[idx].CMD == WRITE_MAIN_SPARE_CMD));
1018 if (erase_fail || program_fail) {
1019 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
1020 if (PendingCMD[idx].Block ==
1021 (pbt[i] & (~BAD_BLOCK)))
1022 MARK_BLOCK_AS_BAD(pbt[i]);
1027 static void process_cmd(int *first_failed_cmd, u16 idx, int event)
1032 if (p_BTableChangesDelta->ftl_cmd_cnt == PendingCMD[idx].Tag)
1035 if (PendingCMD[idx].Status == CMD_PASS) {
1036 process_cmd_pass(first_failed_cmd, idx);
1037 } else if ((PendingCMD[idx].Status == CMD_FAIL) ||
1038 (PendingCMD[idx].Status == CMD_ABORT)) {
1039 process_cmd_fail_abort(first_failed_cmd, idx, event);
1040 } else if ((PendingCMD[idx].Status == CMD_NOT_DONE) &&
1041 PendingCMD[idx].Tag) {
1042 nand_dbg_print(NAND_DBG_DEBUG,
1043 " Command no. %hu is not executed\n",
1044 (unsigned int)PendingCMD[idx].Tag);
1045 ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
1046 while (ftl_cmd <= PendingCMD[idx].Tag) {
1047 p_BTableChangesDelta += 1;
1048 ftl_cmd = p_BTableChangesDelta->ftl_cmd_cnt;
1054 static void process_cmd(int *first_failed_cmd, u16 idx, int event)
1056 printk(KERN_ERR "temporary workaround function. "
1057 "Should not be called! \n");
1060 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1061 * Function: GLOB_FTL_Event_Status
1063 * Outputs: Event Code
1064 * Description: It is called by SBD after hardware interrupt signalling
1065 * completion of commands chain
1066 * It does following things
1067 * get event status from LLD
1068 * analyze command chain status
1069 * determine last command executed
1071 * rebuild the block table in case of uncorrectable error
1073 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1074 int GLOB_FTL_Event_Status(int *first_failed_cmd)
1076 int event_code = PASS;
1079 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1080 __FILE__, __LINE__, __func__);
1082 *first_failed_cmd = 0;
1084 event_code = GLOB_LLD_Event_Status();
1086 switch (event_code) {
1088 nand_dbg_print(NAND_DBG_DEBUG, "Handling EVENT_PASS\n");
1090 case EVENT_UNCORRECTABLE_DATA_ERROR:
1091 nand_dbg_print(NAND_DBG_DEBUG, "Handling Uncorrectable ECC!\n");
1093 case EVENT_PROGRAM_FAILURE:
1094 case EVENT_ERASE_FAILURE:
1095 nand_dbg_print(NAND_DBG_WARN, "Handling Ugly case. "
1096 "Event code: 0x%x\n", event_code);
1097 p_BTableChangesDelta =
1098 (struct BTableChangesDelta *)g_pBTDelta;
1099 for (i_P = MAX_CHANS; i_P < (ftl_cmd_cnt + MAX_CHANS);
1101 process_cmd(first_failed_cmd, i_P, event_code);
1102 memcpy(g_pBlockTable, g_pBTStartingCopy,
1103 DeviceInfo.wDataBlockNum * sizeof(u32));
1104 memcpy(g_pWearCounter, g_pWearCounterCopy,
1105 DeviceInfo.wDataBlockNum * sizeof(u8));
1106 if (DeviceInfo.MLCDevice)
1107 memcpy(g_pReadCounter, g_pReadCounterCopy,
1108 DeviceInfo.wDataBlockNum * sizeof(u16));
1110 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
1111 memcpy((void *)&Cache, (void *)&cache_start_copy,
1112 sizeof(struct flash_cache_tag));
1113 memset((void *)&int_cache, -1,
1114 sizeof(struct flash_cache_delta_list_tag) *
1115 (MAX_DESCS + MAX_CHANS));
1119 nand_dbg_print(NAND_DBG_WARN,
1120 "Handling unexpected event code - 0x%x\n",
1126 memcpy(g_pBTStartingCopy, g_pBlockTable,
1127 DeviceInfo.wDataBlockNum * sizeof(u32));
1128 memcpy(g_pWearCounterCopy, g_pWearCounter,
1129 DeviceInfo.wDataBlockNum * sizeof(u8));
1130 if (DeviceInfo.MLCDevice)
1131 memcpy(g_pReadCounterCopy, g_pReadCounter,
1132 DeviceInfo.wDataBlockNum * sizeof(u16));
1134 g_pBTDelta_Free = g_pBTDelta;
1136 g_pNextBlockTable = g_pBlockTableCopies;
1137 cp_back_buf_idx = 0;
1139 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
1140 memcpy((void *)&cache_start_copy, (void *)&Cache,
1141 sizeof(struct flash_cache_tag));
1142 memset((void *)&int_cache, -1,
1143 sizeof(struct flash_cache_delta_list_tag) *
1144 (MAX_DESCS + MAX_CHANS));
1150 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1151 * Function: glob_ftl_execute_cmds
1154 * Description: pass thru to LLD
1155 ***************************************************************/
1156 u16 glob_ftl_execute_cmds(void)
1158 nand_dbg_print(NAND_DBG_TRACE,
1159 "glob_ftl_execute_cmds: ftl_cmd_cnt %u\n",
1160 (unsigned int)ftl_cmd_cnt);
1162 return glob_lld_execute_cmds();
1168 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1169 * Function: GLOB_FTL_Read Immediate
1170 * Inputs: pointer to data
1172 * Outputs: PASS / FAIL
1173 * Description: Reads one page of data into RAM directly from flash without
1174 * using or disturbing cache.It is assumed this function is called
1175 * with CMD-DMA disabled.
1176 *****************************************************************/
1177 int GLOB_FTL_Read_Immediate(u8 *read_data, u64 addr)
1183 u32 *pbt = (u32 *)g_pBlockTable;
1185 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1186 __FILE__, __LINE__, __func__);
1188 Block = BLK_FROM_ADDR(addr);
1189 Page = PAGE_FROM_ADDR(addr, Block);
1191 if (!IS_SPARE_BLOCK(Block))
1194 phy_blk = pbt[Block];
1195 wResult = GLOB_LLD_Read_Page_Main(read_data, phy_blk, Page, 1);
1197 if (DeviceInfo.MLCDevice) {
1198 g_pReadCounter[phy_blk - DeviceInfo.wSpectraStartBlock]++;
1199 if (g_pReadCounter[phy_blk - DeviceInfo.wSpectraStartBlock]
1200 >= MAX_READ_COUNTER)
1201 FTL_Read_Disturbance(phy_blk);
1202 if (g_cBlockTableStatus != IN_PROGRESS_BLOCK_TABLE) {
1203 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
1204 FTL_Write_IN_Progress_Block_Table_Page();
1212 #ifdef SUPPORT_BIG_ENDIAN
1213 /*********************************************************************
1214 * Function: FTL_Invert_Block_Table
1217 * Description: Re-format the block table in ram based on BIG_ENDIAN and
1218 * LARGE_BLOCKNUM if necessary
1219 **********************************************************************/
1220 static void FTL_Invert_Block_Table(void)
1223 u32 *pbt = (u32 *)g_pBlockTable;
1225 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1226 __FILE__, __LINE__, __func__);
1228 #ifdef SUPPORT_LARGE_BLOCKNUM
1229 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
1230 pbt[i] = INVERTUINT32(pbt[i]);
1231 g_pWearCounter[i] = INVERTUINT32(g_pWearCounter[i]);
1234 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
1235 pbt[i] = INVERTUINT16(pbt[i]);
1236 g_pWearCounter[i] = INVERTUINT16(g_pWearCounter[i]);
1242 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1243 * Function: GLOB_FTL_Flash_Init
1245 * Outputs: PASS=0 / FAIL=0x01 (based on read ID)
1246 * Description: The flash controller is initialized
1247 * The flash device is reset
1248 * Perform a flash READ ID command to confirm that a
1249 * valid device is attached and active.
1250 * The DeviceInfo structure gets filled in
1251 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1252 int GLOB_FTL_Flash_Init(void)
1256 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1257 __FILE__, __LINE__, __func__);
1261 GLOB_LLD_Flash_Init();
1263 status = GLOB_LLD_Read_Device_ID();
1268 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1270 * Outputs: PASS=0 / FAIL=0x01 (based on read ID)
1271 * Description: The flash controller is released
1272 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1273 int GLOB_FTL_Flash_Release(void)
1275 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1276 __FILE__, __LINE__, __func__);
1278 return GLOB_LLD_Flash_Release();
1282 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1283 * Function: GLOB_FTL_Cache_Release
1286 * Description: release all allocated memory in GLOB_FTL_Init
1287 * (allocated in GLOB_FTL_Init)
1288 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1289 void GLOB_FTL_Cache_Release(void)
1291 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1292 __FILE__, __LINE__, __func__);
1297 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1298 * Function: FTL_Cache_If_Hit
1299 * Inputs: Page Address
1300 * Outputs: Block number/UNHIT BLOCK
1301 * Description: Determines if the addressed page is in cache
1302 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1303 static u16 FTL_Cache_If_Hit(u64 page_addr)
1309 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1310 __FILE__, __LINE__, __func__);
1312 item = UNHIT_CACHE_ITEM;
1313 for (i = 0; i < CACHE_ITEM_NUM; i++) {
1314 addr = Cache.array[i].address;
1315 if ((page_addr >= addr) &&
1316 (page_addr < (addr + Cache.cache_item_size))) {
1325 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1326 * Function: FTL_Calculate_LRU
1329 * Description: Calculate the least recently block in a cache and record its
1330 * index in LRU field.
1331 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1332 static void FTL_Calculate_LRU(void)
1334 u16 i, bCurrentLRU, bTempCount;
1336 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1337 __FILE__, __LINE__, __func__);
1340 bTempCount = MAX_WORD_VALUE;
1342 for (i = 0; i < CACHE_ITEM_NUM; i++) {
1343 if (Cache.array[i].use_cnt < bTempCount) {
1345 bTempCount = Cache.array[i].use_cnt;
1349 Cache.LRU = bCurrentLRU;
1352 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1353 * Function: FTL_Cache_Read_Page
1354 * Inputs: pointer to read buffer, logical address and cache item number
1356 * Description: Read the page from the cached block addressed by blocknumber
1357 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1358 static void FTL_Cache_Read_Page(u8 *data_buf, u64 logic_addr, u16 cache_item)
1362 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1363 __FILE__, __LINE__, __func__);
1365 start_addr = Cache.array[cache_item].buf;
1366 start_addr += (u32)(((logic_addr - Cache.array[cache_item].address) >>
1367 DeviceInfo.nBitsInPageDataSize) * DeviceInfo.wPageDataSize);
1370 GLOB_LLD_MemCopy_CMD(data_buf, start_addr,
1371 DeviceInfo.wPageDataSize, 0);
1374 memcpy(data_buf, start_addr, DeviceInfo.wPageDataSize);
1377 if (Cache.array[cache_item].use_cnt < MAX_WORD_VALUE)
1378 Cache.array[cache_item].use_cnt++;
1381 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1382 * Function: FTL_Cache_Read_All
1383 * Inputs: pointer to read buffer,block address
1384 * Outputs: PASS=0 / FAIL =1
1385 * Description: It reads pages in cache
1386 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1387 static int FTL_Cache_Read_All(u8 *pData, u64 phy_addr)
1394 u32 *pbt = (u32 *)g_pBlockTable;
1397 Block = BLK_FROM_ADDR(phy_addr);
1398 Page = PAGE_FROM_ADDR(phy_addr, Block);
1399 PageCount = Cache.pages_per_item;
1401 nand_dbg_print(NAND_DBG_DEBUG,
1402 "%s, Line %d, Function: %s, Block: 0x%x\n",
1403 __FILE__, __LINE__, __func__, Block);
1406 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
1407 if ((pbt[i] & (~BAD_BLOCK)) == Block) {
1409 if (IS_SPARE_BLOCK(i) || IS_BAD_BLOCK(i) ||
1410 IS_DISCARDED_BLOCK(i)) {
1411 /* Add by yunpeng -2008.12.3 */
1413 GLOB_LLD_MemCopy_CMD(pData, g_temp_buf,
1414 PageCount * DeviceInfo.wPageDataSize, 0);
1418 PageCount * DeviceInfo.wPageDataSize);
1422 continue; /* break ?? */
1427 if (0xffffffff == lba)
1428 printk(KERN_ERR "FTL_Cache_Read_All: Block is not found in BT\n");
1431 wResult = GLOB_LLD_Read_Page_Main_cdma(pData, Block, Page,
1432 PageCount, LLD_CMD_FLAG_MODE_CDMA);
1433 if (DeviceInfo.MLCDevice) {
1434 g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock]++;
1435 nand_dbg_print(NAND_DBG_DEBUG,
1436 "Read Counter modified in ftl_cmd_cnt %u"
1437 " Block %u Counter%u\n",
1438 ftl_cmd_cnt, (unsigned int)Block,
1439 g_pReadCounter[Block -
1440 DeviceInfo.wSpectraStartBlock]);
1442 p_BTableChangesDelta =
1443 (struct BTableChangesDelta *)g_pBTDelta_Free;
1444 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
1445 p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
1446 p_BTableChangesDelta->RC_Index =
1447 Block - DeviceInfo.wSpectraStartBlock;
1448 p_BTableChangesDelta->RC_Entry_Value =
1449 g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock];
1450 p_BTableChangesDelta->ValidFields = 0xC0;
1454 if (g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock] >=
1456 FTL_Read_Disturbance(Block);
1457 if (g_cBlockTableStatus != IN_PROGRESS_BLOCK_TABLE) {
1458 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
1459 FTL_Write_IN_Progress_Block_Table_Page();
1465 wResult = GLOB_LLD_Read_Page_Main(pData, Block, Page, PageCount);
1466 if (wResult == FAIL)
1469 if (DeviceInfo.MLCDevice) {
1470 g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock]++;
1471 if (g_pReadCounter[Block - DeviceInfo.wSpectraStartBlock] >=
1473 FTL_Read_Disturbance(Block);
1474 if (g_cBlockTableStatus != IN_PROGRESS_BLOCK_TABLE) {
1475 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
1476 FTL_Write_IN_Progress_Block_Table_Page();
1483 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1484 * Function: FTL_Cache_Write_All
1485 * Inputs: pointer to cache in sys memory
1486 * address of free block in flash
1487 * Outputs: PASS=0 / FAIL=1
1488 * Description: writes all the pages of the block in cache to flash
1490 * NOTE:need to make sure this works ok when cache is limited
1491 * to a partial block. This is where copy-back would be
1492 * activated. This would require knowing which pages in the
1493 * cached block are clean/dirty.Right now we only know if
1494 * the whole block is clean/dirty.
1495 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1496 static int FTL_Cache_Write_All(u8 *pData, u64 blk_addr)
1503 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1504 __FILE__, __LINE__, __func__);
1506 nand_dbg_print(NAND_DBG_DEBUG, "This block %d going to be written "
1507 "on %d\n", cache_block_to_write,
1508 (u32)(blk_addr >> DeviceInfo.nBitsInBlockDataSize));
1510 Block = BLK_FROM_ADDR(blk_addr);
1511 Page = PAGE_FROM_ADDR(blk_addr, Block);
1512 PageCount = Cache.pages_per_item;
1515 if (FAIL == GLOB_LLD_Write_Page_Main_cdma(pData,
1516 Block, Page, PageCount)) {
1517 nand_dbg_print(NAND_DBG_WARN,
1518 "NAND Program fail in %s, Line %d, "
1519 "Function: %s, new Bad Block %d generated! "
1520 "Need Bad Block replacing.\n",
1521 __FILE__, __LINE__, __func__, Block);
1526 if (FAIL == GLOB_LLD_Write_Page_Main(pData, Block, Page, PageCount)) {
1527 nand_dbg_print(NAND_DBG_WARN, "NAND Program fail in %s,"
1528 " Line %d, Function %s, new Bad Block %d generated!"
1529 "Need Bad Block replacing.\n",
1530 __FILE__, __LINE__, __func__, Block);
1537 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1538 * Function: FTL_Copy_Block
1539 * Inputs: source block address
1540 * Destination block address
1541 * Outputs: PASS=0 / FAIL=1
1542 * Description: used only for static wear leveling to move the block
1543 * containing static data to new blocks(more worn)
1544 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1545 int FTL_Copy_Block(u64 old_blk_addr, u64 blk_addr)
1547 int i, r1, r2, wResult = PASS;
1549 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1550 __FILE__, __LINE__, __func__);
1552 for (i = 0; i < DeviceInfo.wPagesPerBlock; i += Cache.pages_per_item) {
1553 r1 = FTL_Cache_Read_All(g_pTempBuf, old_blk_addr +
1554 i * DeviceInfo.wPageDataSize);
1555 r2 = FTL_Cache_Write_All(g_pTempBuf, blk_addr +
1556 i * DeviceInfo.wPageDataSize);
1557 if ((ERR == r1) || (FAIL == r2)) {
1566 /* Search the block table to find out the least wear block and then return it */
1567 static u32 find_least_worn_blk_for_l2_cache(void)
1570 u32 *pbt = (u32 *)g_pBlockTable;
1571 u8 least_wear_cnt = MAX_BYTE_VALUE;
1572 u32 least_wear_blk_idx = MAX_U32_VALUE;
1575 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
1576 if (IS_SPARE_BLOCK(i)) {
1577 phy_idx = (u32)((~BAD_BLOCK) & pbt[i]);
1578 if (phy_idx > DeviceInfo.wSpectraEndBlock)
1579 printk(KERN_ERR "find_least_worn_blk_for_l2_cache: "
1580 "Too big phy block num (%d)\n", phy_idx);
1581 if (g_pWearCounter[phy_idx -DeviceInfo.wSpectraStartBlock] < least_wear_cnt) {
1582 least_wear_cnt = g_pWearCounter[phy_idx - DeviceInfo.wSpectraStartBlock];
1583 least_wear_blk_idx = i;
1588 nand_dbg_print(NAND_DBG_WARN,
1589 "find_least_worn_blk_for_l2_cache: "
1590 "find block %d with least worn counter (%d)\n",
1591 least_wear_blk_idx, least_wear_cnt);
1593 return least_wear_blk_idx;
1598 /* Get blocks for Level2 Cache */
1599 static int get_l2_cache_blks(void)
1603 u32 *pbt = (u32 *)g_pBlockTable;
1605 for (n = 0; n < BLK_NUM_FOR_L2_CACHE; n++) {
1606 blk = find_least_worn_blk_for_l2_cache();
1607 if (blk > DeviceInfo.wDataBlockNum) {
1608 nand_dbg_print(NAND_DBG_WARN,
1609 "find_least_worn_blk_for_l2_cache: "
1610 "No enough free NAND blocks (n: %d) for L2 Cache!\n", n);
1613 /* Tag the free block as discard in block table */
1614 pbt[blk] = (pbt[blk] & (~BAD_BLOCK)) | DISCARD_BLOCK;
1615 /* Add the free block to the L2 Cache block array */
1616 cache_l2.blk_array[n] = pbt[blk] & (~BAD_BLOCK);
1622 static int erase_l2_cache_blocks(void)
1625 u32 pblk, lblk = BAD_BLOCK;
1627 u32 *pbt = (u32 *)g_pBlockTable;
1629 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
1630 __FILE__, __LINE__, __func__);
1632 for (i = 0; i < BLK_NUM_FOR_L2_CACHE; i++) {
1633 pblk = cache_l2.blk_array[i];
1635 /* If the L2 cache block is invalid, then just skip it */
1636 if (MAX_U32_VALUE == pblk)
1639 BUG_ON(pblk > DeviceInfo.wSpectraEndBlock);
1641 addr = (u64)pblk << DeviceInfo.nBitsInBlockDataSize;
1642 if (PASS == GLOB_FTL_Block_Erase(addr)) {
1643 /* Get logical block number of the erased block */
1644 lblk = FTL_Get_Block_Index(pblk);
1645 BUG_ON(BAD_BLOCK == lblk);
1646 /* Tag it as free in the block table */
1647 pbt[lblk] &= (u32)(~DISCARD_BLOCK);
1648 pbt[lblk] |= (u32)(SPARE_BLOCK);
1650 MARK_BLOCK_AS_BAD(pbt[lblk]);
1659 * Merge the valid data page in the L2 cache blocks into NAND.
1661 static int flush_l2_cache(void)
1663 struct list_head *p;
1664 struct spectra_l2_cache_list *pnd, *tmp_pnd;
1665 u32 *pbt = (u32 *)g_pBlockTable;
1666 u32 phy_blk, l2_blk;
1671 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
1672 __FILE__, __LINE__, __func__);
1674 if (list_empty(&cache_l2.table.list)) /* No data to flush */
1677 //dump_cache_l2_table();
1679 if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus) {
1680 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
1681 FTL_Write_IN_Progress_Block_Table_Page();
1684 list_for_each(p, &cache_l2.table.list) {
1685 pnd = list_entry(p, struct spectra_l2_cache_list, list);
1686 if (IS_SPARE_BLOCK(pnd->logical_blk_num) ||
1687 IS_BAD_BLOCK(pnd->logical_blk_num) ||
1688 IS_DISCARDED_BLOCK(pnd->logical_blk_num)) {
1689 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d\n", __FILE__, __LINE__);
1690 memset(cache_l2_blk_buf, 0xff, DeviceInfo.wPagesPerBlock * DeviceInfo.wPageDataSize);
1692 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d\n", __FILE__, __LINE__);
1693 phy_blk = pbt[pnd->logical_blk_num] & (~BAD_BLOCK);
1694 ret = GLOB_LLD_Read_Page_Main(cache_l2_blk_buf,
1695 phy_blk, 0, DeviceInfo.wPagesPerBlock);
1697 printk(KERN_ERR "Read NAND page fail in %s, Line %d\n", __FILE__, __LINE__);
1701 for (i = 0; i < DeviceInfo.wPagesPerBlock; i++) {
1702 if (pnd->pages_array[i] != MAX_U32_VALUE) {
1703 l2_blk = cache_l2.blk_array[(pnd->pages_array[i] >> 16) & 0xffff];
1704 l2_page = pnd->pages_array[i] & 0xffff;
1705 ret = GLOB_LLD_Read_Page_Main(cache_l2_page_buf, l2_blk, l2_page, 1);
1707 printk(KERN_ERR "Read NAND page fail in %s, Line %d\n", __FILE__, __LINE__);
1709 memcpy(cache_l2_blk_buf + i * DeviceInfo.wPageDataSize, cache_l2_page_buf, DeviceInfo.wPageDataSize);
1713 /* Find a free block and tag the original block as discarded */
1714 addr = (u64)pnd->logical_blk_num << DeviceInfo.nBitsInBlockDataSize;
1715 ret = FTL_Replace_Block(addr);
1717 printk(KERN_ERR "FTL_Replace_Block fail in %s, Line %d\n", __FILE__, __LINE__);
1720 /* Write back the updated data into NAND */
1721 phy_blk = pbt[pnd->logical_blk_num] & (~BAD_BLOCK);
1722 if (FAIL == GLOB_LLD_Write_Page_Main(cache_l2_blk_buf, phy_blk, 0, DeviceInfo.wPagesPerBlock)) {
1723 nand_dbg_print(NAND_DBG_WARN,
1724 "Program NAND block %d fail in %s, Line %d\n",
1725 phy_blk, __FILE__, __LINE__);
1726 /* This may not be really a bad block. So just tag it as discarded. */
1727 /* Then it has a chance to be erased when garbage collection. */
1728 /* If it is really bad, then the erase will fail and it will be marked */
1729 /* as bad then. Otherwise it will be marked as free and can be used again */
1730 MARK_BLK_AS_DISCARD(pbt[pnd->logical_blk_num]);
1731 /* Find another free block and write it again */
1732 FTL_Replace_Block(addr);
1733 phy_blk = pbt[pnd->logical_blk_num] & (~BAD_BLOCK);
1734 if (FAIL == GLOB_LLD_Write_Page_Main(cache_l2_blk_buf, phy_blk, 0, DeviceInfo.wPagesPerBlock)) {
1735 printk(KERN_ERR "Failed to write back block %d when flush L2 cache."
1736 "Some data will be lost!\n", phy_blk);
1737 MARK_BLOCK_AS_BAD(pbt[pnd->logical_blk_num]);
1740 /* tag the new free block as used block */
1741 pbt[pnd->logical_blk_num] &= (~SPARE_BLOCK);
1745 /* Destroy the L2 Cache table and free the memory of all nodes */
1746 list_for_each_entry_safe(pnd, tmp_pnd, &cache_l2.table.list, list) {
1747 list_del(&pnd->list);
1751 /* Erase discard L2 cache blocks */
1752 if (erase_l2_cache_blocks() != PASS)
1753 nand_dbg_print(NAND_DBG_WARN,
1754 " Erase L2 cache blocks error in %s, Line %d\n",
1755 __FILE__, __LINE__);
1757 /* Init the Level2 Cache data structure */
1758 for (i = 0; i < BLK_NUM_FOR_L2_CACHE; i++)
1759 cache_l2.blk_array[i] = MAX_U32_VALUE;
1760 cache_l2.cur_blk_idx = 0;
1761 cache_l2.cur_page_num = 0;
1762 INIT_LIST_HEAD(&cache_l2.table.list);
1763 cache_l2.table.logical_blk_num = MAX_U32_VALUE;
1769 * Write back a changed victim cache item to the Level2 Cache
1770 * and update the L2 Cache table to map the change.
1771 * If the L2 Cache is full, then start to do the L2 Cache flush.
1773 static int write_back_to_l2_cache(u8 *buf, u64 logical_addr)
1775 u32 logical_blk_num;
1776 u16 logical_page_num;
1777 struct list_head *p;
1778 struct spectra_l2_cache_list *pnd, *pnd_new;
1782 nand_dbg_print(NAND_DBG_DEBUG, "%s, Line %d, Function: %s\n",
1783 __FILE__, __LINE__, __func__);
1786 * If Level2 Cache table is empty, then it means either:
1787 * 1. This is the first time that the function called after FTL_init
1789 * 2. The Level2 Cache has just been flushed
1791 * So, 'steal' some free blocks from NAND for L2 Cache using
1792 * by just mask them as discard in the block table
1794 if (list_empty(&cache_l2.table.list)) {
1795 BUG_ON(cache_l2.cur_blk_idx != 0);
1796 BUG_ON(cache_l2.cur_page_num!= 0);
1797 BUG_ON(cache_l2.table.logical_blk_num != MAX_U32_VALUE);
1798 if (FAIL == get_l2_cache_blks()) {
1799 GLOB_FTL_Garbage_Collection();
1800 if (FAIL == get_l2_cache_blks()) {
1801 printk(KERN_ALERT "Fail to get L2 cache blks!\n");
1807 logical_blk_num = BLK_FROM_ADDR(logical_addr);
1808 logical_page_num = PAGE_FROM_ADDR(logical_addr, logical_blk_num);
1809 BUG_ON(logical_blk_num == MAX_U32_VALUE);
1811 /* Write the cache item data into the current position of L2 Cache */
1817 if (FAIL == GLOB_LLD_Write_Page_Main(buf,
1818 cache_l2.blk_array[cache_l2.cur_blk_idx],
1819 cache_l2.cur_page_num, 1)) {
1820 nand_dbg_print(NAND_DBG_WARN, "NAND Program fail in "
1821 "%s, Line %d, new Bad Block %d generated!\n",
1823 cache_l2.blk_array[cache_l2.cur_blk_idx]);
1825 /* TODO: tag the current block as bad and try again */
1832 * Update the L2 Cache table.
1834 * First seaching in the table to see whether the logical block
1835 * has been mapped. If not, then kmalloc a new node for the
1836 * logical block, fill data, and then insert it to the list.
1837 * Otherwise, just update the mapped node directly.
1840 list_for_each(p, &cache_l2.table.list) {
1841 pnd = list_entry(p, struct spectra_l2_cache_list, list);
1842 if (pnd->logical_blk_num == logical_blk_num) {
1843 pnd->pages_array[logical_page_num] =
1844 (cache_l2.cur_blk_idx << 16) |
1845 cache_l2.cur_page_num;
1850 if (!found) { /* Create new node for the logical block here */
1852 /* The logical pages to physical pages map array is
1853 * located at the end of struct spectra_l2_cache_list.
1855 node_size = sizeof(struct spectra_l2_cache_list) +
1856 sizeof(u32) * DeviceInfo.wPagesPerBlock;
1857 pnd_new = kmalloc(node_size, GFP_ATOMIC);
1859 printk(KERN_ERR "Failed to kmalloc in %s Line %d\n",
1860 __FILE__, __LINE__);
1862 * TODO: Need to flush all the L2 cache into NAND ASAP
1863 * since no memory available here
1866 pnd_new->logical_blk_num = logical_blk_num;
1867 for (i = 0; i < DeviceInfo.wPagesPerBlock; i++)
1868 pnd_new->pages_array[i] = MAX_U32_VALUE;
1869 pnd_new->pages_array[logical_page_num] =
1870 (cache_l2.cur_blk_idx << 16) | cache_l2.cur_page_num;
1871 list_add(&pnd_new->list, &cache_l2.table.list);
1874 /* Increasing the current position pointer of the L2 Cache */
1875 cache_l2.cur_page_num++;
1876 if (cache_l2.cur_page_num >= DeviceInfo.wPagesPerBlock) {
1877 cache_l2.cur_blk_idx++;
1878 if (cache_l2.cur_blk_idx >= BLK_NUM_FOR_L2_CACHE) {
1879 /* The L2 Cache is full. Need to flush it now */
1880 nand_dbg_print(NAND_DBG_WARN,
1881 "L2 Cache is full, will start to flush it\n");
1884 cache_l2.cur_page_num = 0;
1892 * Seach in the Level2 Cache table to find the cache item.
1893 * If find, read the data from the NAND page of L2 Cache,
1894 * Otherwise, return FAIL.
1896 static int search_l2_cache(u8 *buf, u64 logical_addr)
1898 u32 logical_blk_num;
1899 u16 logical_page_num;
1900 struct list_head *p;
1901 struct spectra_l2_cache_list *pnd;
1902 u32 tmp = MAX_U32_VALUE;
1907 logical_blk_num = BLK_FROM_ADDR(logical_addr);
1908 logical_page_num = PAGE_FROM_ADDR(logical_addr, logical_blk_num);
1910 list_for_each(p, &cache_l2.table.list) {
1911 pnd = list_entry(p, struct spectra_l2_cache_list, list);
1912 if (pnd->logical_blk_num == logical_blk_num) {
1913 tmp = pnd->pages_array[logical_page_num];
1918 if (tmp != MAX_U32_VALUE) { /* Found valid map */
1919 phy_blk = cache_l2.blk_array[(tmp >> 16) & 0xFFFF];
1920 phy_page = tmp & 0xFFFF;
1924 ret = GLOB_LLD_Read_Page_Main(buf, phy_blk, phy_page, 1);
1931 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1932 * Function: FTL_Cache_Write_Page
1933 * Inputs: Pointer to buffer, page address, cache block number
1934 * Outputs: PASS=0 / FAIL=1
1935 * Description: It writes the data in Cache Block
1936 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1937 static void FTL_Cache_Write_Page(u8 *pData, u64 page_addr,
1938 u8 cache_blk, u16 flag)
1943 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1944 __FILE__, __LINE__, __func__);
1946 addr = Cache.array[cache_blk].address;
1947 pDest = Cache.array[cache_blk].buf;
1949 pDest += (unsigned long)(page_addr - addr);
1950 Cache.array[cache_blk].changed = SET;
1952 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
1953 int_cache[ftl_cmd_cnt].item = cache_blk;
1954 int_cache[ftl_cmd_cnt].cache.address =
1955 Cache.array[cache_blk].address;
1956 int_cache[ftl_cmd_cnt].cache.changed =
1957 Cache.array[cache_blk].changed;
1959 GLOB_LLD_MemCopy_CMD(pDest, pData, DeviceInfo.wPageDataSize, flag);
1962 memcpy(pDest, pData, DeviceInfo.wPageDataSize);
1964 if (Cache.array[cache_blk].use_cnt < MAX_WORD_VALUE)
1965 Cache.array[cache_blk].use_cnt++;
1968 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
1969 * Function: FTL_Cache_Write
1971 * Outputs: PASS=0 / FAIL=1
1972 * Description: It writes least frequently used Cache block to flash if it
1974 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
1975 static int FTL_Cache_Write(void)
1977 int i, bResult = PASS;
1978 u16 bNO, least_count = 0xFFFF;
1980 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
1981 __FILE__, __LINE__, __func__);
1983 FTL_Calculate_LRU();
1986 nand_dbg_print(NAND_DBG_DEBUG, "FTL_Cache_Write: "
1987 "Least used cache block is %d\n", bNO);
1989 if (Cache.array[bNO].changed != SET)
1992 nand_dbg_print(NAND_DBG_DEBUG, "FTL_Cache_Write: Cache"
1993 " Block %d containing logical block %d is dirty\n",
1995 (u32)(Cache.array[bNO].address >>
1996 DeviceInfo.nBitsInBlockDataSize));
1998 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
1999 int_cache[ftl_cmd_cnt].item = bNO;
2000 int_cache[ftl_cmd_cnt].cache.address =
2001 Cache.array[bNO].address;
2002 int_cache[ftl_cmd_cnt].cache.changed = CLEAR;
2005 bResult = write_back_to_l2_cache(Cache.array[bNO].buf,
2006 Cache.array[bNO].address);
2008 Cache.array[bNO].changed = CLEAR;
2010 least_count = Cache.array[bNO].use_cnt;
2012 for (i = 0; i < CACHE_ITEM_NUM; i++) {
2015 if (Cache.array[i].use_cnt > 0)
2016 Cache.array[i].use_cnt -= least_count;
2022 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2023 * Function: FTL_Cache_Read
2024 * Inputs: Page address
2025 * Outputs: PASS=0 / FAIL=1
2026 * Description: It reads the block from device in Cache Block
2027 * Set the LRU count to 1
2028 * Mark the Cache Block as clean
2029 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2030 static int FTL_Cache_Read(u64 logical_addr)
2032 u64 item_addr, phy_addr;
2036 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
2037 __FILE__, __LINE__, __func__);
2039 num = Cache.LRU; /* The LRU cache item will be overwritten */
2041 item_addr = (u64)GLOB_u64_Div(logical_addr, Cache.cache_item_size) *
2042 Cache.cache_item_size;
2043 Cache.array[num].address = item_addr;
2044 Cache.array[num].use_cnt = 1;
2045 Cache.array[num].changed = CLEAR;
2048 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
2049 int_cache[ftl_cmd_cnt].item = num;
2050 int_cache[ftl_cmd_cnt].cache.address =
2051 Cache.array[num].address;
2052 int_cache[ftl_cmd_cnt].cache.changed =
2053 Cache.array[num].changed;
2057 * Search in L2 Cache. If hit, fill data into L1 Cache item buffer,
2058 * Otherwise, read it from NAND
2060 ret = search_l2_cache(Cache.array[num].buf, logical_addr);
2061 if (PASS == ret) /* Hit in L2 Cache */
2064 /* Compute the physical start address of NAND device according to */
2065 /* the logical start address of the cache item (LRU cache item) */
2066 phy_addr = FTL_Get_Physical_Block_Addr(item_addr) +
2067 GLOB_u64_Remainder(item_addr, 2);
2069 return FTL_Cache_Read_All(Cache.array[num].buf, phy_addr);
2072 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2073 * Function: FTL_Check_Block_Table
2075 * Outputs: PASS=0 / FAIL=1
2076 * Description: It checks the correctness of each block table entry
2077 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2078 static int FTL_Check_Block_Table(int wOldTable)
2083 u32 *pbt = (u32 *)g_pBlockTable;
2084 u8 *pFlag = flag_check_blk_table;
2086 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
2087 __FILE__, __LINE__, __func__);
2089 if (NULL != pFlag) {
2090 memset(pFlag, FAIL, DeviceInfo.wDataBlockNum);
2091 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
2092 blk_idx = (u32)(pbt[i] & (~BAD_BLOCK));
2095 * 20081006/KBV - Changed to pFlag[i] reference
2096 * to avoid buffer overflow
2100 * 2008-10-20 Yunpeng Note: This change avoid
2101 * buffer overflow, but changed function of
2102 * the code, so it should be re-write later
2104 if ((blk_idx > DeviceInfo.wSpectraEndBlock) ||
2118 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2119 * Function: FTL_Write_Block_Table
2121 * Outputs: 0=Block Table was updated. No write done. 1=Block write needs to
2123 * Description: It writes the block table
2124 * Block table always mapped to LBA 0 which inturn mapped
2125 * to any physical block
2126 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2127 static int FTL_Write_Block_Table(int wForce)
2129 u32 *pbt = (u32 *)g_pBlockTable;
2130 int wSuccess = PASS;
2131 u32 wTempBlockTableIndex;
2132 u16 bt_pages, new_bt_offset;
2133 u8 blockchangeoccured = 0;
2135 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
2136 __FILE__, __LINE__, __func__);
2138 bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
2140 if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus)
2143 if (PASS == wForce) {
2144 g_wBlockTableOffset =
2145 (u16)(DeviceInfo.wPagesPerBlock - bt_pages);
2147 p_BTableChangesDelta =
2148 (struct BTableChangesDelta *)g_pBTDelta_Free;
2149 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
2151 p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
2152 p_BTableChangesDelta->g_wBlockTableOffset =
2153 g_wBlockTableOffset;
2154 p_BTableChangesDelta->ValidFields = 0x01;
2158 nand_dbg_print(NAND_DBG_DEBUG,
2159 "Inside FTL_Write_Block_Table: block %d Page:%d\n",
2160 g_wBlockTableIndex, g_wBlockTableOffset);
2163 new_bt_offset = g_wBlockTableOffset + bt_pages + 1;
2164 if ((0 == (new_bt_offset % DeviceInfo.wPagesPerBlock)) ||
2165 (new_bt_offset > DeviceInfo.wPagesPerBlock) ||
2166 (FAIL == wSuccess)) {
2167 wTempBlockTableIndex = FTL_Replace_Block_Table();
2168 if (BAD_BLOCK == wTempBlockTableIndex)
2170 if (!blockchangeoccured) {
2171 bt_block_changed = 1;
2172 blockchangeoccured = 1;
2175 g_wBlockTableIndex = wTempBlockTableIndex;
2176 g_wBlockTableOffset = 0;
2177 pbt[BLOCK_TABLE_INDEX] = g_wBlockTableIndex;
2179 p_BTableChangesDelta =
2180 (struct BTableChangesDelta *)g_pBTDelta_Free;
2181 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
2183 p_BTableChangesDelta->ftl_cmd_cnt =
2185 p_BTableChangesDelta->g_wBlockTableOffset =
2186 g_wBlockTableOffset;
2187 p_BTableChangesDelta->g_wBlockTableIndex =
2189 p_BTableChangesDelta->ValidFields = 0x03;
2191 p_BTableChangesDelta =
2192 (struct BTableChangesDelta *)g_pBTDelta_Free;
2194 sizeof(struct BTableChangesDelta);
2196 p_BTableChangesDelta->ftl_cmd_cnt =
2198 p_BTableChangesDelta->BT_Index =
2200 p_BTableChangesDelta->BT_Entry_Value =
2201 pbt[BLOCK_TABLE_INDEX];
2202 p_BTableChangesDelta->ValidFields = 0x0C;
2206 wSuccess = FTL_Write_Block_Table_Data();
2207 if (FAIL == wSuccess)
2208 MARK_BLOCK_AS_BAD(pbt[BLOCK_TABLE_INDEX]);
2209 } while (FAIL == wSuccess);
2211 g_cBlockTableStatus = CURRENT_BLOCK_TABLE;
2216 static int force_format_nand(void)
2220 /* Force erase the whole unprotected physical partiton of NAND */
2221 printk(KERN_ALERT "Start to force erase whole NAND device ...\n");
2222 printk(KERN_ALERT "From phyical block %d to %d\n",
2223 DeviceInfo.wSpectraStartBlock, DeviceInfo.wSpectraEndBlock);
2224 for (i = DeviceInfo.wSpectraStartBlock; i <= DeviceInfo.wSpectraEndBlock; i++) {
2225 if (GLOB_LLD_Erase_Block(i))
2226 printk(KERN_ERR "Failed to force erase NAND block %d\n", i);
2228 printk(KERN_ALERT "Force Erase ends. Please reboot the system ...\n");
2234 int GLOB_FTL_Flash_Format(void)
2236 //return FTL_Format_Flash(1);
2237 return force_format_nand();
2241 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2242 * Function: FTL_Search_Block_Table_IN_Block
2243 * Inputs: Block Number
2245 * Outputs: PASS / FAIL
2246 * Page contatining the block table
2247 * Description: It searches the block table in the block
2248 * passed as an argument.
2250 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2251 static int FTL_Search_Block_Table_IN_Block(u32 BT_Block,
2252 u8 BT_Tag, u16 *Page)
2259 u8 *tempbuf = tmp_buf_search_bt_in_block;
2260 u8 *pSpareBuf = spare_buf_search_bt_in_block;
2261 u8 *pSpareBufBTLastPage = spare_buf_bt_search_bt_in_block;
2262 u8 bt_flag_last_page = 0xFF;
2263 u8 search_in_previous_pages = 0;
2266 nand_dbg_print(NAND_DBG_DEBUG, "%s, Line %d, Function: %s\n",
2267 __FILE__, __LINE__, __func__);
2269 nand_dbg_print(NAND_DBG_DEBUG,
2270 "Searching block table in %u block\n",
2271 (unsigned int)BT_Block);
2273 bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
2275 for (i = bt_pages; i < DeviceInfo.wPagesPerBlock;
2276 i += (bt_pages + 1)) {
2277 nand_dbg_print(NAND_DBG_DEBUG,
2278 "Searching last IPF: %d\n", i);
2279 Result = GLOB_LLD_Read_Page_Main_Polling(tempbuf,
2282 if (0 == memcmp(tempbuf, g_pIPF, DeviceInfo.wPageDataSize)) {
2283 if ((i + bt_pages + 1) < DeviceInfo.wPagesPerBlock) {
2286 search_in_previous_pages = 1;
2291 if (!search_in_previous_pages) {
2292 if (i != bt_pages) {
2293 i -= (bt_pages + 1);
2301 if (!search_in_previous_pages) {
2303 nand_dbg_print(NAND_DBG_DEBUG,
2304 "Reading the spare area of Block %u Page %u",
2305 (unsigned int)BT_Block, i);
2306 Result = GLOB_LLD_Read_Page_Spare(pSpareBuf,
2308 nand_dbg_print(NAND_DBG_DEBUG,
2309 "Reading the spare area of Block %u Page %u",
2310 (unsigned int)BT_Block, i + bt_pages - 1);
2311 Result = GLOB_LLD_Read_Page_Spare(pSpareBufBTLastPage,
2312 BT_Block, i + bt_pages - 1, 1);
2315 j = FTL_Extract_Block_Table_Tag(pSpareBuf, &tagarray);
2317 for (; k < j; k++) {
2318 if (tagarray[k] == BT_Tag)
2324 bt_flag = tagarray[k];
2328 if (Result == PASS) {
2330 j = FTL_Extract_Block_Table_Tag(
2331 pSpareBufBTLastPage, &tagarray);
2333 for (; k < j; k++) {
2334 if (tagarray[k] == BT_Tag)
2340 bt_flag_last_page = tagarray[k];
2344 if (Result == PASS) {
2345 if (bt_flag == bt_flag_last_page) {
2346 nand_dbg_print(NAND_DBG_DEBUG,
2347 "Block table is found"
2348 " in page after IPF "
2354 g_cBlockTableStatus =
2355 CURRENT_BLOCK_TABLE;
2364 if (search_in_previous_pages)
2367 i = i - (bt_pages + 1);
2371 nand_dbg_print(NAND_DBG_DEBUG,
2372 "Reading the spare area of Block %d Page %d",
2375 Result = GLOB_LLD_Read_Page_Spare(pSpareBuf, BT_Block, i, 1);
2376 nand_dbg_print(NAND_DBG_DEBUG,
2377 "Reading the spare area of Block %u Page %u",
2378 (unsigned int)BT_Block, i + bt_pages - 1);
2380 Result = GLOB_LLD_Read_Page_Spare(pSpareBufBTLastPage,
2381 BT_Block, i + bt_pages - 1, 1);
2384 j = FTL_Extract_Block_Table_Tag(pSpareBuf, &tagarray);
2386 for (; k < j; k++) {
2387 if (tagarray[k] == BT_Tag)
2393 bt_flag = tagarray[k];
2397 if (Result == PASS) {
2399 j = FTL_Extract_Block_Table_Tag(pSpareBufBTLastPage,
2402 for (; k < j; k++) {
2403 if (tagarray[k] == BT_Tag)
2409 bt_flag_last_page = tagarray[k];
2415 if (Result == PASS) {
2416 if (bt_flag == bt_flag_last_page) {
2417 nand_dbg_print(NAND_DBG_DEBUG,
2418 "Block table is found "
2419 "in page prior to IPF "
2420 "at block %u page %d\n",
2421 (unsigned int)BT_Block, i);
2424 g_cBlockTableStatus =
2425 IN_PROGRESS_BLOCK_TABLE;
2435 if (Result == FAIL) {
2436 if ((Last_IPF > bt_pages) && (i < Last_IPF) && (!BT_Found)) {
2438 *Page = i - (bt_pages + 1);
2440 if ((Last_IPF == bt_pages) && (i < Last_IPF) && (!BT_Found))
2444 if (Last_IPF == 0) {
2447 nand_dbg_print(NAND_DBG_DEBUG, "Reading the spare area of "
2448 "Block %u Page %u", (unsigned int)BT_Block, i);
2450 Result = GLOB_LLD_Read_Page_Spare(pSpareBuf, BT_Block, i, 1);
2451 nand_dbg_print(NAND_DBG_DEBUG,
2452 "Reading the spare area of Block %u Page %u",
2453 (unsigned int)BT_Block, i + bt_pages - 1);
2454 Result = GLOB_LLD_Read_Page_Spare(pSpareBufBTLastPage,
2455 BT_Block, i + bt_pages - 1, 1);
2458 j = FTL_Extract_Block_Table_Tag(pSpareBuf, &tagarray);
2460 for (; k < j; k++) {
2461 if (tagarray[k] == BT_Tag)
2467 bt_flag = tagarray[k];
2471 if (Result == PASS) {
2473 j = FTL_Extract_Block_Table_Tag(pSpareBufBTLastPage,
2476 for (; k < j; k++) {
2477 if (tagarray[k] == BT_Tag)
2483 bt_flag_last_page = tagarray[k];
2487 if (Result == PASS) {
2488 if (bt_flag == bt_flag_last_page) {
2489 nand_dbg_print(NAND_DBG_DEBUG,
2490 "Block table is found "
2491 "in page after IPF at "
2492 "block %u page %u\n",
2493 (unsigned int)BT_Block,
2497 g_cBlockTableStatus =
2498 CURRENT_BLOCK_TABLE;
2513 u8 *get_blk_table_start_addr(void)
2515 return g_pBlockTable;
2518 unsigned long get_blk_table_len(void)
2520 return DeviceInfo.wDataBlockNum * sizeof(u32);
2523 u8 *get_wear_leveling_table_start_addr(void)
2525 return g_pWearCounter;
2528 unsigned long get_wear_leveling_table_len(void)
2530 return DeviceInfo.wDataBlockNum * sizeof(u8);
2533 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2534 * Function: FTL_Read_Block_Table
2536 * Outputs: PASS / FAIL
2537 * Description: read the flash spare area and find a block containing the
2538 * most recent block table(having largest block_table_counter).
2539 * Find the last written Block table in this block.
2540 * Check the correctness of Block Table
2541 * If CDMA is enabled, this function is called in
2543 * We don't need to store changes in Block table in this
2544 * function as it is called only at initialization
2546 * Note: Currently this function is called at initialization
2547 * before any read/erase/write command issued to flash so,
2548 * there is no need to wait for CDMA list to complete as of now
2549 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2550 static int FTL_Read_Block_Table(void)
2554 u8 *tempBuf, *tagarray;
2557 u8 block_table_found = 0;
2563 int wBytesCopied = 0, tempvar;
2565 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
2566 __FILE__, __LINE__, __func__);
2568 tempBuf = tmp_buf1_read_blk_table;
2569 bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
2571 for (j = DeviceInfo.wSpectraStartBlock;
2572 j <= (int)DeviceInfo.wSpectraEndBlock;
2574 status = GLOB_LLD_Read_Page_Spare(tempBuf, j, 0, 1);
2576 i = FTL_Extract_Block_Table_Tag(tempBuf, &tagarray);
2578 status = GLOB_LLD_Read_Page_Main_Polling(tempBuf,
2580 for (; k < i; k++) {
2581 if (tagarray[k] == tempBuf[3])
2591 nand_dbg_print(NAND_DBG_DEBUG,
2592 "Block table is contained in Block %d %d\n",
2593 (unsigned int)j, (unsigned int)k);
2595 if (g_pBTBlocks[k-FIRST_BT_ID] == BTBLOCK_INVAL) {
2596 g_pBTBlocks[k-FIRST_BT_ID] = j;
2597 block_table_found = 1;
2599 printk(KERN_ERR "FTL_Read_Block_Table -"
2600 "This should never happens. "
2601 "Two block table have same counter %u!\n", k);
2605 if (block_table_found) {
2606 if (g_pBTBlocks[FIRST_BT_ID - FIRST_BT_ID] != BTBLOCK_INVAL &&
2607 g_pBTBlocks[LAST_BT_ID - FIRST_BT_ID] != BTBLOCK_INVAL) {
2609 while ((j > FIRST_BT_ID) &&
2610 (g_pBTBlocks[j - FIRST_BT_ID] != BTBLOCK_INVAL))
2612 if (j == FIRST_BT_ID) {
2614 last_erased = LAST_BT_ID;
2616 last_erased = (u8)j + 1;
2617 while ((j > FIRST_BT_ID) && (BTBLOCK_INVAL ==
2618 g_pBTBlocks[j - FIRST_BT_ID]))
2623 while (g_pBTBlocks[j - FIRST_BT_ID] == BTBLOCK_INVAL)
2625 last_erased = (u8)j;
2626 while ((j < LAST_BT_ID) && (BTBLOCK_INVAL !=
2627 g_pBTBlocks[j - FIRST_BT_ID]))
2629 if (g_pBTBlocks[j-FIRST_BT_ID] == BTBLOCK_INVAL)
2633 if (last_erased > j)
2634 j += (1 + LAST_BT_ID - FIRST_BT_ID);
2636 for (; (j >= last_erased) && (FAIL == wResult); j--) {
2637 i = (j - FIRST_BT_ID) %
2638 (1 + LAST_BT_ID - FIRST_BT_ID);
2640 FTL_Search_Block_Table_IN_Block(g_pBTBlocks[i],
2641 i + FIRST_BT_ID, &Page);
2642 if (g_cBlockTableStatus == IN_PROGRESS_BLOCK_TABLE)
2643 block_table_found = 0;
2645 while ((search_result == PASS) && (FAIL == wResult)) {
2646 nand_dbg_print(NAND_DBG_DEBUG,
2647 "FTL_Read_Block_Table:"
2648 "Block: %u Page: %u "
2649 "contains block table\n",
2650 (unsigned int)g_pBTBlocks[i],
2651 (unsigned int)Page);
2653 tempBuf = tmp_buf2_read_blk_table;
2655 for (k = 0; k < bt_pages; k++) {
2656 Block = g_pBTBlocks[i];
2660 GLOB_LLD_Read_Page_Main_Polling(
2661 tempBuf, Block, Page, PageCount);
2663 tempvar = k ? 0 : 4;
2666 FTL_Copy_Block_Table_From_Flash(
2668 DeviceInfo.wPageDataSize - tempvar,
2674 wResult = FTL_Check_Block_Table(FAIL);
2675 if (FAIL == wResult) {
2676 block_table_found = 0;
2677 if (Page > bt_pages)
2678 Page -= ((bt_pages<<1) + 1);
2680 search_result = FAIL;
2686 if (PASS == wResult) {
2687 if (!block_table_found)
2688 FTL_Execute_SPL_Recovery();
2690 if (g_cBlockTableStatus == IN_PROGRESS_BLOCK_TABLE)
2691 g_wBlockTableOffset = (u16)Page + 1;
2693 g_wBlockTableOffset = (u16)Page - bt_pages;
2695 g_wBlockTableIndex = (u32)g_pBTBlocks[i];
2698 if (DeviceInfo.MLCDevice)
2699 memcpy(g_pBTStartingCopy, g_pBlockTable,
2700 DeviceInfo.wDataBlockNum * sizeof(u32)
2701 + DeviceInfo.wDataBlockNum * sizeof(u8)
2702 + DeviceInfo.wDataBlockNum * sizeof(u16));
2704 memcpy(g_pBTStartingCopy, g_pBlockTable,
2705 DeviceInfo.wDataBlockNum * sizeof(u32)
2706 + DeviceInfo.wDataBlockNum * sizeof(u8));
2710 if (FAIL == wResult)
2711 printk(KERN_ERR "Yunpeng - "
2712 "Can not find valid spectra block table!\n");
2714 #if AUTO_FORMAT_FLASH
2715 if (FAIL == wResult) {
2716 nand_dbg_print(NAND_DBG_DEBUG, "doing auto-format\n");
2717 wResult = FTL_Format_Flash(0);
2724 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2725 * Function: FTL_Get_Page_Num
2726 * Inputs: Size in bytes
2727 * Outputs: Size in pages
2728 * Description: It calculates the pages required for the length passed
2729 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2730 static u32 FTL_Get_Page_Num(u64 length)
2732 return (u32)((length >> DeviceInfo.nBitsInPageDataSize) +
2733 (GLOB_u64_Remainder(length , 1) > 0 ? 1 : 0));
2736 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2737 * Function: FTL_Get_Physical_Block_Addr
2738 * Inputs: Block Address (byte format)
2739 * Outputs: Physical address of the block.
2740 * Description: It translates LBA to PBA by returning address stored
2741 * at the LBA location in the block table
2742 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2743 static u64 FTL_Get_Physical_Block_Addr(u64 logical_addr)
2748 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
2749 __FILE__, __LINE__, __func__);
2751 pbt = (u32 *)g_pBlockTable;
2752 physical_addr = (u64) DeviceInfo.wBlockDataSize *
2753 (pbt[BLK_FROM_ADDR(logical_addr)] & (~BAD_BLOCK));
2755 return physical_addr;
2758 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2759 * Function: FTL_Get_Block_Index
2760 * Inputs: Physical Block no.
2761 * Outputs: Logical block no. /BAD_BLOCK
2762 * Description: It returns the logical block no. for the PBA passed
2763 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2764 static u32 FTL_Get_Block_Index(u32 wBlockNum)
2766 u32 *pbt = (u32 *)g_pBlockTable;
2769 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
2770 __FILE__, __LINE__, __func__);
2772 for (i = 0; i < DeviceInfo.wDataBlockNum; i++)
2773 if (wBlockNum == (pbt[i] & (~BAD_BLOCK)))
2779 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2780 * Function: GLOB_FTL_Wear_Leveling
2783 * Description: This is static wear leveling (done by explicit call)
2784 * do complete static wear leveling
2785 * do complete garbage collection
2786 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2787 int GLOB_FTL_Wear_Leveling(void)
2789 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
2790 __FILE__, __LINE__, __func__);
2792 FTL_Static_Wear_Leveling();
2793 GLOB_FTL_Garbage_Collection();
2798 static void find_least_most_worn(u8 *chg,
2799 u32 *least_idx, u8 *least_cnt,
2800 u32 *most_idx, u8 *most_cnt)
2802 u32 *pbt = (u32 *)g_pBlockTable;
2807 for (i = BLOCK_TABLE_INDEX + 1; i < DeviceInfo.wDataBlockNum; i++) {
2808 if (IS_BAD_BLOCK(i) || PASS == chg[i])
2811 idx = (u32) ((~BAD_BLOCK) & pbt[i]);
2812 cnt = g_pWearCounter[idx - DeviceInfo.wSpectraStartBlock];
2814 if (IS_SPARE_BLOCK(i)) {
2815 if (cnt > *most_cnt) {
2821 if (IS_DATA_BLOCK(i)) {
2822 if (cnt < *least_cnt) {
2828 if (PASS == chg[*most_idx] || PASS == chg[*least_idx]) {
2829 debug_boundary_error(*most_idx,
2830 DeviceInfo.wDataBlockNum, 0);
2831 debug_boundary_error(*least_idx,
2832 DeviceInfo.wDataBlockNum, 0);
2838 static int move_blks_for_wear_leveling(u8 *chg,
2839 u32 *least_idx, u32 *rep_blk_num, int *result)
2841 u32 *pbt = (u32 *)g_pBlockTable;
2843 int j, ret_cp_blk, ret_erase;
2846 chg[*least_idx] = PASS;
2847 debug_boundary_error(*least_idx, DeviceInfo.wDataBlockNum, 0);
2849 rep_blk = FTL_Replace_MWBlock();
2850 if (rep_blk != BAD_BLOCK) {
2851 nand_dbg_print(NAND_DBG_DEBUG,
2852 "More than two spare blocks exist so do it\n");
2853 nand_dbg_print(NAND_DBG_DEBUG, "Block Replaced is %d\n",
2856 chg[rep_blk] = PASS;
2858 if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus) {
2859 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
2860 FTL_Write_IN_Progress_Block_Table_Page();
2863 for (j = 0; j < RETRY_TIMES; j++) {
2864 ret_cp_blk = FTL_Copy_Block((u64)(*least_idx) *
2865 DeviceInfo.wBlockDataSize,
2866 (u64)rep_blk * DeviceInfo.wBlockDataSize);
2867 if (FAIL == ret_cp_blk) {
2868 ret_erase = GLOB_FTL_Block_Erase((u64)rep_blk
2869 * DeviceInfo.wBlockDataSize);
2870 if (FAIL == ret_erase)
2871 MARK_BLOCK_AS_BAD(pbt[rep_blk]);
2873 nand_dbg_print(NAND_DBG_DEBUG,
2874 "FTL_Copy_Block == OK\n");
2879 if (j < RETRY_TIMES) {
2881 u32 old_idx = FTL_Get_Block_Index(*least_idx);
2882 u32 rep_idx = FTL_Get_Block_Index(rep_blk);
2883 tmp = (u32)(DISCARD_BLOCK | pbt[old_idx]);
2884 pbt[old_idx] = (u32)((~SPARE_BLOCK) &
2888 p_BTableChangesDelta = (struct BTableChangesDelta *)
2890 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
2891 p_BTableChangesDelta->ftl_cmd_cnt =
2893 p_BTableChangesDelta->BT_Index = old_idx;
2894 p_BTableChangesDelta->BT_Entry_Value = pbt[old_idx];
2895 p_BTableChangesDelta->ValidFields = 0x0C;
2897 p_BTableChangesDelta = (struct BTableChangesDelta *)
2899 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
2901 p_BTableChangesDelta->ftl_cmd_cnt =
2903 p_BTableChangesDelta->BT_Index = rep_idx;
2904 p_BTableChangesDelta->BT_Entry_Value = pbt[rep_idx];
2905 p_BTableChangesDelta->ValidFields = 0x0C;
2908 pbt[FTL_Get_Block_Index(rep_blk)] |= BAD_BLOCK;
2910 p_BTableChangesDelta = (struct BTableChangesDelta *)
2912 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
2914 p_BTableChangesDelta->ftl_cmd_cnt =
2916 p_BTableChangesDelta->BT_Index =
2917 FTL_Get_Block_Index(rep_blk);
2918 p_BTableChangesDelta->BT_Entry_Value =
2919 pbt[FTL_Get_Block_Index(rep_blk)];
2920 p_BTableChangesDelta->ValidFields = 0x0C;
2926 if (((*rep_blk_num)++) > WEAR_LEVELING_BLOCK_NUM)
2929 printk(KERN_ERR "Less than 3 spare blocks exist so quit\n");
2936 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2937 * Function: FTL_Static_Wear_Leveling
2939 * Outputs: PASS=0 / FAIL=1
2940 * Description: This is static wear leveling (done by explicit call)
2941 * search for most&least used
2942 * if difference < GATE:
2943 * update the block table with exhange
2944 * mark block table in flash as IN_PROGRESS
2946 * the caller should handle GC clean up after calling this function
2947 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
2948 int FTL_Static_Wear_Leveling(void)
2956 u32 replaced_blks = 0;
2957 u8 *chang_flag = flags_static_wear_leveling;
2959 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
2960 __FILE__, __LINE__, __func__);
2965 memset(chang_flag, FAIL, DeviceInfo.wDataBlockNum);
2966 while (go_on == PASS) {
2967 nand_dbg_print(NAND_DBG_DEBUG,
2968 "starting static wear leveling\n");
2970 least_worn_cnt = 0xFF;
2971 least_worn_idx = BLOCK_TABLE_INDEX;
2972 most_worn_idx = BLOCK_TABLE_INDEX;
2974 find_least_most_worn(chang_flag, &least_worn_idx,
2975 &least_worn_cnt, &most_worn_idx, &most_worn_cnt);
2977 nand_dbg_print(NAND_DBG_DEBUG,
2978 "Used and least worn is block %u, whos count is %u\n",
2979 (unsigned int)least_worn_idx,
2980 (unsigned int)least_worn_cnt);
2982 nand_dbg_print(NAND_DBG_DEBUG,
2983 "Free and most worn is block %u, whos count is %u\n",
2984 (unsigned int)most_worn_idx,
2985 (unsigned int)most_worn_cnt);
2987 if ((most_worn_cnt > least_worn_cnt) &&
2988 (most_worn_cnt - least_worn_cnt > WEAR_LEVELING_GATE))
2989 go_on = move_blks_for_wear_leveling(chang_flag,
2990 &least_worn_idx, &replaced_blks, &result);
2999 static int do_garbage_collection(u32 discard_cnt)
3001 u32 *pbt = (u32 *)g_pBlockTable;
3003 u8 bt_block_erased = 0;
3004 int i, cnt, ret = FAIL;
3008 while ((i < DeviceInfo.wDataBlockNum) && (discard_cnt > 0) &&
3009 ((ftl_cmd_cnt + 28) < 256)) {
3010 if (((pbt[i] & BAD_BLOCK) != BAD_BLOCK) &&
3011 (pbt[i] & DISCARD_BLOCK)) {
3012 if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus) {
3013 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
3014 FTL_Write_IN_Progress_Block_Table_Page();
3017 addr = FTL_Get_Physical_Block_Addr((u64)i *
3018 DeviceInfo.wBlockDataSize);
3019 pba = BLK_FROM_ADDR(addr);
3021 for (cnt = FIRST_BT_ID; cnt <= LAST_BT_ID; cnt++) {
3022 if (pba == g_pBTBlocks[cnt - FIRST_BT_ID]) {
3023 nand_dbg_print(NAND_DBG_DEBUG,
3024 "GC will erase BT block %u\n",
3028 bt_block_erased = 1;
3033 if (bt_block_erased) {
3034 bt_block_erased = 0;
3038 addr = FTL_Get_Physical_Block_Addr((u64)i *
3039 DeviceInfo.wBlockDataSize);
3041 if (PASS == GLOB_FTL_Block_Erase(addr)) {
3042 pbt[i] &= (u32)(~DISCARD_BLOCK);
3043 pbt[i] |= (u32)(SPARE_BLOCK);
3044 p_BTableChangesDelta =
3045 (struct BTableChangesDelta *)
3048 sizeof(struct BTableChangesDelta);
3049 p_BTableChangesDelta->ftl_cmd_cnt =
3051 p_BTableChangesDelta->BT_Index = i;
3052 p_BTableChangesDelta->BT_Entry_Value = pbt[i];
3053 p_BTableChangesDelta->ValidFields = 0x0C;
3057 MARK_BLOCK_AS_BAD(pbt[i]);
3068 static int do_garbage_collection(u32 discard_cnt)
3070 u32 *pbt = (u32 *)g_pBlockTable;
3072 u8 bt_block_erased = 0;
3073 int i, cnt, ret = FAIL;
3077 while ((i < DeviceInfo.wDataBlockNum) && (discard_cnt > 0)) {
3078 if (((pbt[i] & BAD_BLOCK) != BAD_BLOCK) &&
3079 (pbt[i] & DISCARD_BLOCK)) {
3080 if (IN_PROGRESS_BLOCK_TABLE != g_cBlockTableStatus) {
3081 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
3082 FTL_Write_IN_Progress_Block_Table_Page();
3085 addr = FTL_Get_Physical_Block_Addr((u64)i *
3086 DeviceInfo.wBlockDataSize);
3087 pba = BLK_FROM_ADDR(addr);
3089 for (cnt = FIRST_BT_ID; cnt <= LAST_BT_ID; cnt++) {
3090 if (pba == g_pBTBlocks[cnt - FIRST_BT_ID]) {
3091 nand_dbg_print(NAND_DBG_DEBUG,
3092 "GC will erase BT block %d\n",
3096 bt_block_erased = 1;
3101 if (bt_block_erased) {
3102 bt_block_erased = 0;
3106 /* If the discard block is L2 cache block, then just skip it */
3107 for (cnt = 0; cnt < BLK_NUM_FOR_L2_CACHE; cnt++) {
3108 if (cache_l2.blk_array[cnt] == pba) {
3109 nand_dbg_print(NAND_DBG_DEBUG,
3110 "GC will erase L2 cache blk %d\n",
3115 if (cnt < BLK_NUM_FOR_L2_CACHE) { /* Skip it */
3121 addr = FTL_Get_Physical_Block_Addr((u64)i *
3122 DeviceInfo.wBlockDataSize);
3124 if (PASS == GLOB_FTL_Block_Erase(addr)) {
3125 pbt[i] &= (u32)(~DISCARD_BLOCK);
3126 pbt[i] |= (u32)(SPARE_BLOCK);
3130 MARK_BLOCK_AS_BAD(pbt[i]);
3141 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3142 * Function: GLOB_FTL_Garbage_Collection
3144 * Outputs: PASS / FAIL (returns the number of un-erased blocks
3145 * Description: search the block table for all discarded blocks to erase
3146 * for each discarded block:
3147 * set the flash block to IN_PROGRESS
3149 * update the block table
3150 * write the block table to flash
3151 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3152 int GLOB_FTL_Garbage_Collection(void)
3157 u32 *pbt = (u32 *)g_pBlockTable;
3159 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
3160 __FILE__, __LINE__, __func__);
3163 printk(KERN_ALERT "GLOB_FTL_Garbage_Collection() "
3164 "has been re-entered! Exit.\n");
3170 GLOB_FTL_BT_Garbage_Collection();
3172 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
3173 if (IS_DISCARDED_BLOCK(i))
3177 if (wDiscard <= 0) {
3182 nand_dbg_print(NAND_DBG_DEBUG,
3183 "Found %d discarded blocks\n", wDiscard);
3185 FTL_Write_Block_Table(FAIL);
3187 wResult = do_garbage_collection(wDiscard);
3189 FTL_Write_Block_Table(FAIL);
3198 static int do_bt_garbage_collection(void)
3201 u32 *pbt = (u32 *)g_pBlockTable;
3202 u32 *pBTBlocksNode = (u32 *)g_pBTBlocks;
3206 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3207 __FILE__, __LINE__, __func__);
3214 for (i = last_erased; (i <= LAST_BT_ID) &&
3215 (g_pBTBlocks[((i + 2) % (1 + LAST_BT_ID - FIRST_BT_ID)) +
3216 FIRST_BT_ID - FIRST_BT_ID] != BTBLOCK_INVAL) &&
3217 ((ftl_cmd_cnt + 28)) < 256; i++) {
3218 pba = pBTBlocksNode[i - FIRST_BT_ID];
3219 lba = FTL_Get_Block_Index(pba);
3220 nand_dbg_print(NAND_DBG_DEBUG,
3221 "do_bt_garbage_collection: pba %d, lba %d\n",
3223 nand_dbg_print(NAND_DBG_DEBUG,
3224 "Block Table Entry: %d", pbt[lba]);
3226 if (((pbt[lba] & BAD_BLOCK) != BAD_BLOCK) &&
3227 (pbt[lba] & DISCARD_BLOCK)) {
3228 nand_dbg_print(NAND_DBG_DEBUG,
3229 "do_bt_garbage_collection_cdma: "
3230 "Erasing Block tables present in block %d\n",
3232 addr = FTL_Get_Physical_Block_Addr((u64)lba *
3233 DeviceInfo.wBlockDataSize);
3234 if (PASS == GLOB_FTL_Block_Erase(addr)) {
3235 pbt[lba] &= (u32)(~DISCARD_BLOCK);
3236 pbt[lba] |= (u32)(SPARE_BLOCK);
3238 p_BTableChangesDelta =
3239 (struct BTableChangesDelta *)
3242 sizeof(struct BTableChangesDelta);
3244 p_BTableChangesDelta->ftl_cmd_cnt =
3246 p_BTableChangesDelta->BT_Index = lba;
3247 p_BTableChangesDelta->BT_Entry_Value =
3250 p_BTableChangesDelta->ValidFields = 0x0C;
3253 pBTBlocksNode[last_erased - FIRST_BT_ID] =
3255 nand_dbg_print(NAND_DBG_DEBUG,
3256 "resetting bt entry at index %d "
3258 pBTBlocksNode[i - FIRST_BT_ID]);
3259 if (last_erased == LAST_BT_ID)
3260 last_erased = FIRST_BT_ID;
3264 MARK_BLOCK_AS_BAD(pbt[lba]);
3275 static int do_bt_garbage_collection(void)
3278 u32 *pbt = (u32 *)g_pBlockTable;
3279 u32 *pBTBlocksNode = (u32 *)g_pBTBlocks;
3283 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3284 __FILE__, __LINE__, __func__);
3291 for (i = last_erased; (i <= LAST_BT_ID) &&
3292 (g_pBTBlocks[((i + 2) % (1 + LAST_BT_ID - FIRST_BT_ID)) +
3293 FIRST_BT_ID - FIRST_BT_ID] != BTBLOCK_INVAL); i++) {
3294 pba = pBTBlocksNode[i - FIRST_BT_ID];
3295 lba = FTL_Get_Block_Index(pba);
3296 nand_dbg_print(NAND_DBG_DEBUG,
3297 "do_bt_garbage_collection_cdma: pba %d, lba %d\n",
3299 nand_dbg_print(NAND_DBG_DEBUG,
3300 "Block Table Entry: %d", pbt[lba]);
3302 if (((pbt[lba] & BAD_BLOCK) != BAD_BLOCK) &&
3303 (pbt[lba] & DISCARD_BLOCK)) {
3304 nand_dbg_print(NAND_DBG_DEBUG,
3305 "do_bt_garbage_collection: "
3306 "Erasing Block tables present in block %d\n",
3308 addr = FTL_Get_Physical_Block_Addr((u64)lba *
3309 DeviceInfo.wBlockDataSize);
3310 if (PASS == GLOB_FTL_Block_Erase(addr)) {
3311 pbt[lba] &= (u32)(~DISCARD_BLOCK);
3312 pbt[lba] |= (u32)(SPARE_BLOCK);
3314 pBTBlocksNode[last_erased - FIRST_BT_ID] =
3316 nand_dbg_print(NAND_DBG_DEBUG,
3317 "resetting bt entry at index %d "
3319 pBTBlocksNode[i - FIRST_BT_ID]);
3320 if (last_erased == LAST_BT_ID)
3321 last_erased = FIRST_BT_ID;
3325 MARK_BLOCK_AS_BAD(pbt[lba]);
3337 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3338 * Function: GLOB_FTL_BT_Garbage_Collection
3340 * Outputs: PASS / FAIL (returns the number of un-erased blocks
3341 * Description: Erases discarded blocks containing Block table
3343 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3344 int GLOB_FTL_BT_Garbage_Collection(void)
3346 return do_bt_garbage_collection();
3349 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3350 * Function: FTL_Replace_OneBlock
3351 * Inputs: Block number 1
3353 * Outputs: Replaced Block Number
3354 * Description: Interchange block table entries at wBlockNum and wReplaceNum
3356 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3357 static u32 FTL_Replace_OneBlock(u32 blk, u32 rep_blk)
3360 u32 replace_node = BAD_BLOCK;
3361 u32 *pbt = (u32 *)g_pBlockTable;
3363 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3364 __FILE__, __LINE__, __func__);
3366 if (rep_blk != BAD_BLOCK) {
3367 if (IS_BAD_BLOCK(blk))
3370 tmp_blk = DISCARD_BLOCK | (~SPARE_BLOCK & pbt[blk]);
3372 replace_node = (u32) ((~SPARE_BLOCK) & pbt[rep_blk]);
3373 pbt[blk] = replace_node;
3374 pbt[rep_blk] = tmp_blk;
3377 p_BTableChangesDelta =
3378 (struct BTableChangesDelta *)g_pBTDelta_Free;
3379 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
3381 p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
3382 p_BTableChangesDelta->BT_Index = blk;
3383 p_BTableChangesDelta->BT_Entry_Value = pbt[blk];
3385 p_BTableChangesDelta->ValidFields = 0x0C;
3387 p_BTableChangesDelta =
3388 (struct BTableChangesDelta *)g_pBTDelta_Free;
3389 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
3391 p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
3392 p_BTableChangesDelta->BT_Index = rep_blk;
3393 p_BTableChangesDelta->BT_Entry_Value = pbt[rep_blk];
3394 p_BTableChangesDelta->ValidFields = 0x0C;
3398 return replace_node;
3401 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3402 * Function: FTL_Write_Block_Table_Data
3403 * Inputs: Block table size in pages
3404 * Outputs: PASS=0 / FAIL=1
3405 * Description: Write block table data in flash
3406 * If first page and last page
3407 * Write data+BT flag
3410 * BT flag is a counter. Its value is incremented for block table
3411 * write in a new Block
3412 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3413 static int FTL_Write_Block_Table_Data(void)
3415 u64 dwBlockTableAddr, pTempAddr;
3417 u16 Page, PageCount;
3418 u8 *tempBuf = tmp_buf_write_blk_table_data;
3422 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3423 __FILE__, __LINE__, __func__);
3426 (u64)((u64)g_wBlockTableIndex * DeviceInfo.wBlockDataSize +
3427 (u64)g_wBlockTableOffset * DeviceInfo.wPageDataSize);
3428 pTempAddr = dwBlockTableAddr;
3430 bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
3432 nand_dbg_print(NAND_DBG_DEBUG, "FTL_Write_Block_Table_Data: "
3433 "page= %d BlockTableIndex= %d "
3434 "BlockTableOffset=%d\n", bt_pages,
3435 g_wBlockTableIndex, g_wBlockTableOffset);
3437 Block = BLK_FROM_ADDR(pTempAddr);
3438 Page = PAGE_FROM_ADDR(pTempAddr, Block);
3441 if (bt_block_changed) {
3442 if (bt_flag == LAST_BT_ID) {
3443 bt_flag = FIRST_BT_ID;
3444 g_pBTBlocks[bt_flag - FIRST_BT_ID] = Block;
3445 } else if (bt_flag < LAST_BT_ID) {
3447 g_pBTBlocks[bt_flag - FIRST_BT_ID] = Block;
3450 if ((bt_flag > (LAST_BT_ID-4)) &&
3451 g_pBTBlocks[FIRST_BT_ID - FIRST_BT_ID] !=
3453 bt_block_changed = 0;
3454 GLOB_FTL_BT_Garbage_Collection();
3457 bt_block_changed = 0;
3458 nand_dbg_print(NAND_DBG_DEBUG,
3459 "Block Table Counter is %u Block %u\n",
3460 bt_flag, (unsigned int)Block);
3463 memset(tempBuf, 0, 3);
3464 tempBuf[3] = bt_flag;
3465 wBytesCopied = FTL_Copy_Block_Table_To_Flash(tempBuf + 4,
3466 DeviceInfo.wPageDataSize - 4, 0);
3467 memset(&tempBuf[wBytesCopied + 4], 0xff,
3468 DeviceInfo.wPageSize - (wBytesCopied + 4));
3469 FTL_Insert_Block_Table_Signature(&tempBuf[DeviceInfo.wPageDataSize],
3473 memcpy(g_pNextBlockTable, tempBuf,
3474 DeviceInfo.wPageSize * sizeof(u8));
3475 nand_dbg_print(NAND_DBG_DEBUG, "Writing First Page of Block Table "
3476 "Block %u Page %u\n", (unsigned int)Block, Page);
3477 if (FAIL == GLOB_LLD_Write_Page_Main_Spare_cdma(g_pNextBlockTable,
3479 LLD_CMD_FLAG_MODE_CDMA | LLD_CMD_FLAG_ORDER_BEFORE_REST)) {
3480 nand_dbg_print(NAND_DBG_WARN, "NAND Program fail in "
3481 "%s, Line %d, Function: %s, "
3482 "new Bad Block %d generated!\n",
3483 __FILE__, __LINE__, __func__, Block);
3488 g_pNextBlockTable += ((DeviceInfo.wPageSize * sizeof(u8)));
3490 if (FAIL == GLOB_LLD_Write_Page_Main_Spare(tempBuf, Block, Page, 1)) {
3491 nand_dbg_print(NAND_DBG_WARN,
3492 "NAND Program fail in %s, Line %d, Function: %s, "
3493 "new Bad Block %d generated!\n",
3494 __FILE__, __LINE__, __func__, Block);
3500 PageCount = bt_pages - 1;
3501 if (PageCount > 1) {
3502 wBytesCopied += FTL_Copy_Block_Table_To_Flash(tempBuf,
3503 DeviceInfo.wPageDataSize * (PageCount - 1),
3507 memcpy(g_pNextBlockTable, tempBuf,
3508 (PageCount - 1) * DeviceInfo.wPageDataSize);
3509 if (FAIL == GLOB_LLD_Write_Page_Main_cdma(
3510 g_pNextBlockTable, Block, Page + 1,
3512 nand_dbg_print(NAND_DBG_WARN,
3513 "NAND Program fail in %s, Line %d, "
3515 "new Bad Block %d generated!\n",
3516 __FILE__, __LINE__, __func__,
3522 g_pNextBlockTable += (PageCount - 1) *
3523 DeviceInfo.wPageDataSize * sizeof(u8);
3525 if (FAIL == GLOB_LLD_Write_Page_Main(tempBuf,
3526 Block, Page + 1, PageCount - 1)) {
3527 nand_dbg_print(NAND_DBG_WARN,
3528 "NAND Program fail in %s, Line %d, "
3530 "new Bad Block %d generated!\n",
3531 __FILE__, __LINE__, __func__,
3538 wBytesCopied = FTL_Copy_Block_Table_To_Flash(tempBuf,
3539 DeviceInfo.wPageDataSize, wBytesCopied);
3540 memset(&tempBuf[wBytesCopied], 0xff,
3541 DeviceInfo.wPageSize-wBytesCopied);
3542 FTL_Insert_Block_Table_Signature(
3543 &tempBuf[DeviceInfo.wPageDataSize], bt_flag);
3545 memcpy(g_pNextBlockTable, tempBuf,
3546 DeviceInfo.wPageSize * sizeof(u8));
3547 nand_dbg_print(NAND_DBG_DEBUG,
3548 "Writing the last Page of Block Table "
3549 "Block %u Page %u\n",
3550 (unsigned int)Block, Page + bt_pages - 1);
3551 if (FAIL == GLOB_LLD_Write_Page_Main_Spare_cdma(
3552 g_pNextBlockTable, Block, Page + bt_pages - 1, 1,
3553 LLD_CMD_FLAG_MODE_CDMA |
3554 LLD_CMD_FLAG_ORDER_BEFORE_REST)) {
3555 nand_dbg_print(NAND_DBG_WARN,
3556 "NAND Program fail in %s, Line %d, "
3557 "Function: %s, new Bad Block %d generated!\n",
3558 __FILE__, __LINE__, __func__, Block);
3563 if (FAIL == GLOB_LLD_Write_Page_Main_Spare(tempBuf,
3564 Block, Page+bt_pages - 1, 1)) {
3565 nand_dbg_print(NAND_DBG_WARN,
3566 "NAND Program fail in %s, Line %d, "
3568 "new Bad Block %d generated!\n",
3569 __FILE__, __LINE__, __func__, Block);
3575 nand_dbg_print(NAND_DBG_DEBUG, "FTL_Write_Block_Table_Data: done\n");
3581 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3582 * Function: FTL_Replace_Block_Table
3584 * Outputs: PASS=0 / FAIL=1
3585 * Description: Get a new block to write block table
3586 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3587 static u32 FTL_Replace_Block_Table(void)
3592 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3593 __FILE__, __LINE__, __func__);
3595 blk = FTL_Replace_LWBlock(BLOCK_TABLE_INDEX, &gc);
3597 if ((BAD_BLOCK == blk) && (PASS == gc)) {
3598 GLOB_FTL_Garbage_Collection();
3599 blk = FTL_Replace_LWBlock(BLOCK_TABLE_INDEX, &gc);
3601 if (BAD_BLOCK == blk)
3602 printk(KERN_ERR "%s, %s: There is no spare block. "
3603 "It should never happen\n",
3604 __FILE__, __func__);
3606 nand_dbg_print(NAND_DBG_DEBUG, "New Block table Block is %d\n", blk);
3611 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3612 * Function: FTL_Replace_LWBlock
3613 * Inputs: Block number
3614 * Pointer to Garbage Collect flag
3616 * Description: Determine the least weared block by traversing
3618 * Set Garbage collection to be called if number of spare
3619 * block is less than Free Block Gate count
3620 * Change Block table entry to map least worn block for current
3622 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3623 static u32 FTL_Replace_LWBlock(u32 wBlockNum, int *pGarbageCollect)
3626 u32 *pbt = (u32 *)g_pBlockTable;
3627 u8 wLeastWornCounter = 0xFF;
3628 u32 wLeastWornIndex = BAD_BLOCK;
3629 u32 wSpareBlockNum = 0;
3630 u32 wDiscardBlockNum = 0;
3632 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3633 __FILE__, __LINE__, __func__);
3635 if (IS_SPARE_BLOCK(wBlockNum)) {
3636 *pGarbageCollect = FAIL;
3637 pbt[wBlockNum] = (u32)(pbt[wBlockNum] & (~SPARE_BLOCK));
3639 p_BTableChangesDelta =
3640 (struct BTableChangesDelta *)g_pBTDelta_Free;
3641 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
3642 p_BTableChangesDelta->ftl_cmd_cnt =
3644 p_BTableChangesDelta->BT_Index = (u32)(wBlockNum);
3645 p_BTableChangesDelta->BT_Entry_Value = pbt[wBlockNum];
3646 p_BTableChangesDelta->ValidFields = 0x0C;
3648 return pbt[wBlockNum];
3651 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
3652 if (IS_DISCARDED_BLOCK(i))
3655 if (IS_SPARE_BLOCK(i)) {
3656 u32 wPhysicalIndex = (u32)((~BAD_BLOCK) & pbt[i]);
3657 if (wPhysicalIndex > DeviceInfo.wSpectraEndBlock)
3658 printk(KERN_ERR "FTL_Replace_LWBlock: "
3659 "This should never occur!\n");
3660 if (g_pWearCounter[wPhysicalIndex -
3661 DeviceInfo.wSpectraStartBlock] <
3662 wLeastWornCounter) {
3664 g_pWearCounter[wPhysicalIndex -
3665 DeviceInfo.wSpectraStartBlock];
3666 wLeastWornIndex = i;
3672 nand_dbg_print(NAND_DBG_WARN,
3673 "FTL_Replace_LWBlock: Least Worn Counter %d\n",
3674 (int)wLeastWornCounter);
3676 if ((wDiscardBlockNum >= NUM_FREE_BLOCKS_GATE) ||
3677 (wSpareBlockNum <= NUM_FREE_BLOCKS_GATE))
3678 *pGarbageCollect = PASS;
3680 *pGarbageCollect = FAIL;
3682 nand_dbg_print(NAND_DBG_DEBUG,
3683 "FTL_Replace_LWBlock: Discarded Blocks %u Spare"
3685 (unsigned int)wDiscardBlockNum,
3686 (unsigned int)wSpareBlockNum);
3688 return FTL_Replace_OneBlock(wBlockNum, wLeastWornIndex);
3691 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3692 * Function: FTL_Replace_MWBlock
3694 * Outputs: most worn spare block no./BAD_BLOCK
3695 * Description: It finds most worn spare block.
3696 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3697 static u32 FTL_Replace_MWBlock(void)
3700 u32 *pbt = (u32 *)g_pBlockTable;
3701 u8 wMostWornCounter = 0;
3702 u32 wMostWornIndex = BAD_BLOCK;
3703 u32 wSpareBlockNum = 0;
3705 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3706 __FILE__, __LINE__, __func__);
3708 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
3709 if (IS_SPARE_BLOCK(i)) {
3710 u32 wPhysicalIndex = (u32)((~SPARE_BLOCK) & pbt[i]);
3711 if (g_pWearCounter[wPhysicalIndex -
3712 DeviceInfo.wSpectraStartBlock] >
3715 g_pWearCounter[wPhysicalIndex -
3716 DeviceInfo.wSpectraStartBlock];
3717 wMostWornIndex = wPhysicalIndex;
3723 if (wSpareBlockNum <= 2)
3726 return wMostWornIndex;
3729 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3730 * Function: FTL_Replace_Block
3731 * Inputs: Block Address
3732 * Outputs: PASS=0 / FAIL=1
3733 * Description: If block specified by blk_addr parameter is not free,
3734 * replace it with the least worn block.
3735 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3736 static int FTL_Replace_Block(u64 blk_addr)
3738 u32 current_blk = BLK_FROM_ADDR(blk_addr);
3739 u32 *pbt = (u32 *)g_pBlockTable;
3741 int GarbageCollect = FAIL;
3743 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3744 __FILE__, __LINE__, __func__);
3746 if (IS_SPARE_BLOCK(current_blk)) {
3747 pbt[current_blk] = (~SPARE_BLOCK) & pbt[current_blk];
3749 p_BTableChangesDelta =
3750 (struct BTableChangesDelta *)g_pBTDelta_Free;
3751 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
3752 p_BTableChangesDelta->ftl_cmd_cnt =
3754 p_BTableChangesDelta->BT_Index = current_blk;
3755 p_BTableChangesDelta->BT_Entry_Value = pbt[current_blk];
3756 p_BTableChangesDelta->ValidFields = 0x0C ;
3761 FTL_Replace_LWBlock(current_blk, &GarbageCollect);
3763 if (PASS == GarbageCollect)
3764 wResult = GLOB_FTL_Garbage_Collection();
3769 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3770 * Function: GLOB_FTL_Is_BadBlock
3771 * Inputs: block number to test
3772 * Outputs: PASS (block is BAD) / FAIL (block is not bad)
3773 * Description: test if this block number is flagged as bad
3774 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3775 int GLOB_FTL_Is_BadBlock(u32 wBlockNum)
3777 u32 *pbt = (u32 *)g_pBlockTable;
3779 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3780 __FILE__, __LINE__, __func__);
3782 if (wBlockNum >= DeviceInfo.wSpectraStartBlock
3783 && BAD_BLOCK == (pbt[wBlockNum] & BAD_BLOCK))
3789 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3790 * Function: GLOB_FTL_Flush_Cache
3792 * Outputs: PASS=0 / FAIL=1
3793 * Description: flush all the cache blocks to flash
3794 * if a cache block is not dirty, don't do anything with it
3795 * else, write the block and update the block table
3796 * Note: This function should be called at shutdown/power down.
3797 * to write important data into device
3798 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3799 int GLOB_FTL_Flush_Cache(void)
3803 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
3804 __FILE__, __LINE__, __func__);
3806 for (i = 0; i < CACHE_ITEM_NUM; i++) {
3807 if (SET == Cache.array[i].changed) {
3809 #if RESTORE_CACHE_ON_CDMA_CHAIN_FAILURE
3810 int_cache[ftl_cmd_cnt].item = i;
3811 int_cache[ftl_cmd_cnt].cache.address =
3812 Cache.array[i].address;
3813 int_cache[ftl_cmd_cnt].cache.changed = CLEAR;
3816 ret = write_back_to_l2_cache(Cache.array[i].buf, Cache.array[i].address);
3818 Cache.array[i].changed = CLEAR;
3820 printk(KERN_ALERT "Failed when write back to L2 cache!\n");
3821 /* TODO - How to handle this? */
3828 return FTL_Write_Block_Table(FAIL);
3831 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3832 * Function: GLOB_FTL_Page_Read
3833 * Inputs: pointer to data
3834 * logical address of data (u64 is LBA * Bytes/Page)
3835 * Outputs: PASS=0 / FAIL=1
3836 * Description: reads a page of data into RAM from the cache
3837 * if the data is not already in cache, read from flash to cache
3838 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3839 int GLOB_FTL_Page_Read(u8 *data, u64 logical_addr)
3844 nand_dbg_print(NAND_DBG_DEBUG, "GLOB_FTL_Page_Read - "
3845 "page_addr: %llu\n", logical_addr);
3847 cache_item = FTL_Cache_If_Hit(logical_addr);
3849 if (UNHIT_CACHE_ITEM == cache_item) {
3850 nand_dbg_print(NAND_DBG_DEBUG,
3851 "GLOB_FTL_Page_Read: Cache not hit\n");
3852 res = FTL_Cache_Write();
3853 if (ERR == FTL_Cache_Read(logical_addr))
3855 cache_item = Cache.LRU;
3858 FTL_Cache_Read_Page(data, logical_addr, cache_item);
3863 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3864 * Function: GLOB_FTL_Page_Write
3865 * Inputs: pointer to data
3866 * address of data (ADDRESSTYPE is LBA * Bytes/Page)
3867 * Outputs: PASS=0 / FAIL=1
3868 * Description: writes a page of data from RAM to the cache
3869 * if the data is not already in cache, write back the
3870 * least recently used block and read the addressed block
3871 * from flash to cache
3872 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3873 int GLOB_FTL_Page_Write(u8 *pData, u64 dwPageAddr)
3876 u32 *pbt = (u32 *)g_pBlockTable;
3879 nand_dbg_print(NAND_DBG_TRACE, "GLOB_FTL_Page_Write - "
3880 "dwPageAddr: %llu\n", dwPageAddr);
3882 cache_blk = FTL_Cache_If_Hit(dwPageAddr);
3884 if (UNHIT_CACHE_ITEM == cache_blk) {
3885 wResult = FTL_Cache_Write();
3886 if (IS_BAD_BLOCK(BLK_FROM_ADDR(dwPageAddr))) {
3887 wResult = FTL_Replace_Block(dwPageAddr);
3888 pbt[BLK_FROM_ADDR(dwPageAddr)] |= SPARE_BLOCK;
3889 if (wResult == FAIL)
3892 if (ERR == FTL_Cache_Read(dwPageAddr))
3894 cache_blk = Cache.LRU;
3895 FTL_Cache_Write_Page(pData, dwPageAddr, cache_blk, 0);
3898 FTL_Cache_Write_Page(pData, dwPageAddr, cache_blk,
3899 LLD_CMD_FLAG_ORDER_BEFORE_REST);
3901 FTL_Cache_Write_Page(pData, dwPageAddr, cache_blk, 0);
3908 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3909 * Function: GLOB_FTL_Block_Erase
3910 * Inputs: address of block to erase (now in byte format, should change to
3912 * Outputs: PASS=0 / FAIL=1
3913 * Description: erases the specified block
3914 * increments the erase count
3915 * If erase count reaches its upper limit,call function to
3916 * do the ajustment as per the relative erase count values
3917 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
3918 int GLOB_FTL_Block_Erase(u64 blk_addr)
3923 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
3924 __FILE__, __LINE__, __func__);
3926 BlkIdx = (u32)(blk_addr >> DeviceInfo.nBitsInBlockDataSize);
3928 if (BlkIdx < DeviceInfo.wSpectraStartBlock) {
3929 printk(KERN_ERR "GLOB_FTL_Block_Erase: "
3930 "This should never occur\n");
3935 status = GLOB_LLD_Erase_Block_cdma(BlkIdx, LLD_CMD_FLAG_MODE_CDMA);
3937 nand_dbg_print(NAND_DBG_WARN,
3938 "NAND Program fail in %s, Line %d, "
3939 "Function: %s, new Bad Block %d generated!\n",
3940 __FILE__, __LINE__, __func__, BlkIdx);
3942 status = GLOB_LLD_Erase_Block(BlkIdx);
3943 if (status == FAIL) {
3944 nand_dbg_print(NAND_DBG_WARN,
3945 "NAND Program fail in %s, Line %d, "
3946 "Function: %s, new Bad Block %d generated!\n",
3947 __FILE__, __LINE__, __func__, BlkIdx);
3952 if (DeviceInfo.MLCDevice) {
3953 g_pReadCounter[BlkIdx - DeviceInfo.wSpectraStartBlock] = 0;
3954 if (g_cBlockTableStatus != IN_PROGRESS_BLOCK_TABLE) {
3955 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
3956 FTL_Write_IN_Progress_Block_Table_Page();
3960 g_pWearCounter[BlkIdx - DeviceInfo.wSpectraStartBlock]++;
3963 p_BTableChangesDelta =
3964 (struct BTableChangesDelta *)g_pBTDelta_Free;
3965 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
3966 p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
3967 p_BTableChangesDelta->WC_Index =
3968 BlkIdx - DeviceInfo.wSpectraStartBlock;
3969 p_BTableChangesDelta->WC_Entry_Value =
3970 g_pWearCounter[BlkIdx - DeviceInfo.wSpectraStartBlock];
3971 p_BTableChangesDelta->ValidFields = 0x30;
3973 if (DeviceInfo.MLCDevice) {
3974 p_BTableChangesDelta =
3975 (struct BTableChangesDelta *)g_pBTDelta_Free;
3976 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
3977 p_BTableChangesDelta->ftl_cmd_cnt =
3979 p_BTableChangesDelta->RC_Index =
3980 BlkIdx - DeviceInfo.wSpectraStartBlock;
3981 p_BTableChangesDelta->RC_Entry_Value =
3982 g_pReadCounter[BlkIdx -
3983 DeviceInfo.wSpectraStartBlock];
3984 p_BTableChangesDelta->ValidFields = 0xC0;
3990 if (g_pWearCounter[BlkIdx - DeviceInfo.wSpectraStartBlock] == 0xFE)
3991 FTL_Adjust_Relative_Erase_Count(BlkIdx);
3997 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3998 * Function: FTL_Adjust_Relative_Erase_Count
3999 * Inputs: index to block that was just incremented and is at the max
4000 * Outputs: PASS=0 / FAIL=1
4001 * Description: If any erase counts at MAX, adjusts erase count of every
4002 * block by substracting least worn
4003 * counter from counter value of every entry in wear table
4004 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
4005 static int FTL_Adjust_Relative_Erase_Count(u32 Index_of_MAX)
4007 u8 wLeastWornCounter = MAX_BYTE_VALUE;
4010 u32 *pbt = (u32 *)g_pBlockTable;
4013 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
4014 __FILE__, __LINE__, __func__);
4016 for (i = 0; i < DeviceInfo.wDataBlockNum; i++) {
4017 if (IS_BAD_BLOCK(i))
4019 wWearIndex = (u32)(pbt[i] & (~BAD_BLOCK));
4021 if ((wWearIndex - DeviceInfo.wSpectraStartBlock) < 0)
4022 printk(KERN_ERR "FTL_Adjust_Relative_Erase_Count:"
4023 "This should never occur\n");
4024 wWearCounter = g_pWearCounter[wWearIndex -
4025 DeviceInfo.wSpectraStartBlock];
4026 if (wWearCounter < wLeastWornCounter)
4027 wLeastWornCounter = wWearCounter;
4030 if (wLeastWornCounter == 0) {
4031 nand_dbg_print(NAND_DBG_WARN,
4032 "Adjusting Wear Levelling Counters: Special Case\n");
4033 g_pWearCounter[Index_of_MAX -
4034 DeviceInfo.wSpectraStartBlock]--;
4036 p_BTableChangesDelta =
4037 (struct BTableChangesDelta *)g_pBTDelta_Free;
4038 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
4039 p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
4040 p_BTableChangesDelta->WC_Index =
4041 Index_of_MAX - DeviceInfo.wSpectraStartBlock;
4042 p_BTableChangesDelta->WC_Entry_Value =
4043 g_pWearCounter[Index_of_MAX -
4044 DeviceInfo.wSpectraStartBlock];
4045 p_BTableChangesDelta->ValidFields = 0x30;
4047 FTL_Static_Wear_Leveling();
4049 for (i = 0; i < DeviceInfo.wDataBlockNum; i++)
4050 if (!IS_BAD_BLOCK(i)) {
4051 wWearIndex = (u32)(pbt[i] & (~BAD_BLOCK));
4052 g_pWearCounter[wWearIndex -
4053 DeviceInfo.wSpectraStartBlock] =
4056 DeviceInfo.wSpectraStartBlock] -
4059 p_BTableChangesDelta =
4060 (struct BTableChangesDelta *)g_pBTDelta_Free;
4062 sizeof(struct BTableChangesDelta);
4064 p_BTableChangesDelta->ftl_cmd_cnt =
4066 p_BTableChangesDelta->WC_Index = wWearIndex -
4067 DeviceInfo.wSpectraStartBlock;
4068 p_BTableChangesDelta->WC_Entry_Value =
4069 g_pWearCounter[wWearIndex -
4070 DeviceInfo.wSpectraStartBlock];
4071 p_BTableChangesDelta->ValidFields = 0x30;
4079 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
4080 * Function: FTL_Write_IN_Progress_Block_Table_Page
4083 * Description: It writes in-progress flag page to the page next to
4085 ***********************************************************************/
4086 static int FTL_Write_IN_Progress_Block_Table_Page(void)
4093 u32 *pbt = (u32 *)g_pBlockTable;
4094 u32 wTempBlockTableIndex;
4097 nand_dbg_print(NAND_DBG_WARN, "%s, Line %d, Function: %s\n",
4098 __FILE__, __LINE__, __func__);
4100 bt_pages = FTL_Get_Block_Table_Flash_Size_Pages();
4102 dwIPFPageAddr = g_wBlockTableOffset + bt_pages;
4104 nand_dbg_print(NAND_DBG_DEBUG, "Writing IPF at "
4105 "Block %d Page %d\n",
4106 g_wBlockTableIndex, dwIPFPageAddr);
4109 wResult = GLOB_LLD_Write_Page_Main_Spare_cdma(g_pIPF,
4110 g_wBlockTableIndex, dwIPFPageAddr, 1,
4111 LLD_CMD_FLAG_MODE_CDMA | LLD_CMD_FLAG_ORDER_BEFORE_REST);
4112 if (wResult == FAIL) {
4113 nand_dbg_print(NAND_DBG_WARN,
4114 "NAND Program fail in %s, Line %d, "
4115 "Function: %s, new Bad Block %d generated!\n",
4116 __FILE__, __LINE__, __func__,
4117 g_wBlockTableIndex);
4119 g_wBlockTableOffset = dwIPFPageAddr + 1;
4120 p_BTableChangesDelta = (struct BTableChangesDelta *)g_pBTDelta_Free;
4121 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
4122 p_BTableChangesDelta->ftl_cmd_cnt = ftl_cmd_cnt;
4123 p_BTableChangesDelta->g_wBlockTableOffset = g_wBlockTableOffset;
4124 p_BTableChangesDelta->ValidFields = 0x01;
4127 wResult = GLOB_LLD_Write_Page_Main_Spare(g_pIPF,
4128 g_wBlockTableIndex, dwIPFPageAddr, 1);
4129 if (wResult == FAIL) {
4130 nand_dbg_print(NAND_DBG_WARN,
4131 "NAND Program fail in %s, Line %d, "
4132 "Function: %s, new Bad Block %d generated!\n",
4133 __FILE__, __LINE__, __func__,
4134 (int)g_wBlockTableIndex);
4135 MARK_BLOCK_AS_BAD(pbt[BLOCK_TABLE_INDEX]);
4136 wTempBlockTableIndex = FTL_Replace_Block_Table();
4137 bt_block_changed = 1;
4138 if (BAD_BLOCK == wTempBlockTableIndex)
4140 g_wBlockTableIndex = wTempBlockTableIndex;
4141 g_wBlockTableOffset = 0;
4142 /* Block table tag is '00'. Means it's used one */
4143 pbt[BLOCK_TABLE_INDEX] = g_wBlockTableIndex;
4146 g_wBlockTableOffset = dwIPFPageAddr + 1;
4151 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
4152 * Function: FTL_Read_Disturbance
4153 * Inputs: block address
4154 * Outputs: PASS=0 / FAIL=1
4155 * Description: used to handle read disturbance. Data in block that
4156 * reaches its read limit is moved to new block
4157 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
4158 int FTL_Read_Disturbance(u32 blk_addr)
4161 u32 *pbt = (u32 *) g_pBlockTable;
4162 u32 dwOldBlockAddr = blk_addr;
4165 u32 wLeastReadCounter = 0xFFFF;
4166 u32 wLeastReadIndex = BAD_BLOCK;
4167 u32 wSpareBlockNum = 0;
4172 nand_dbg_print(NAND_DBG_DEBUG, "%s, Line %d, Function: %s\n",
4173 __FILE__, __LINE__, __func__);
4176 g_pTempBuf = cp_back_buf_copies[cp_back_buf_idx];
4178 if (cp_back_buf_idx > COPY_BACK_BUF_NUM) {
4179 printk(KERN_ERR "cp_back_buf_copies overflow! Exit."
4180 "Maybe too many pending commands in your CDMA chain.\n");
4184 g_pTempBuf = tmp_buf_read_disturbance;
4187 wBlockNum = FTL_Get_Block_Index(blk_addr);
4190 /* This is a bug.Here 'i' should be logical block number
4191 * and start from 1 (0 is reserved for block table).
4192 * Have fixed it. - Yunpeng 2008. 12. 19
4194 for (i = 1; i < DeviceInfo.wDataBlockNum; i++) {
4195 if (IS_SPARE_BLOCK(i)) {
4196 u32 wPhysicalIndex =
4197 (u32)((~SPARE_BLOCK) & pbt[i]);
4198 if (g_pReadCounter[wPhysicalIndex -
4199 DeviceInfo.wSpectraStartBlock] <
4200 wLeastReadCounter) {
4202 g_pReadCounter[wPhysicalIndex -
4203 DeviceInfo.wSpectraStartBlock];
4204 wLeastReadIndex = i;
4210 if (wSpareBlockNum <= NUM_FREE_BLOCKS_GATE) {
4211 wResult = GLOB_FTL_Garbage_Collection();
4212 if (PASS == wResult)
4217 wTempNode = (u32)(DISCARD_BLOCK | pbt[wBlockNum]);
4218 wReplacedNode = (u32)((~SPARE_BLOCK) &
4219 pbt[wLeastReadIndex]);
4221 pbt[wBlockNum] = wReplacedNode;
4222 pbt[wLeastReadIndex] = wTempNode;
4223 p_BTableChangesDelta =
4224 (struct BTableChangesDelta *)g_pBTDelta_Free;
4225 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
4227 p_BTableChangesDelta->ftl_cmd_cnt =
4229 p_BTableChangesDelta->BT_Index = wBlockNum;
4230 p_BTableChangesDelta->BT_Entry_Value = pbt[wBlockNum];
4231 p_BTableChangesDelta->ValidFields = 0x0C;
4233 p_BTableChangesDelta =
4234 (struct BTableChangesDelta *)g_pBTDelta_Free;
4235 g_pBTDelta_Free += sizeof(struct BTableChangesDelta);
4237 p_BTableChangesDelta->ftl_cmd_cnt =
4239 p_BTableChangesDelta->BT_Index = wLeastReadIndex;
4240 p_BTableChangesDelta->BT_Entry_Value =
4241 pbt[wLeastReadIndex];
4242 p_BTableChangesDelta->ValidFields = 0x0C;
4244 wResult = GLOB_LLD_Read_Page_Main_cdma(g_pTempBuf,
4245 dwOldBlockAddr, 0, DeviceInfo.wPagesPerBlock,
4246 LLD_CMD_FLAG_MODE_CDMA);
4247 if (wResult == FAIL)
4252 if (wResult != FAIL) {
4253 if (FAIL == GLOB_LLD_Write_Page_Main_cdma(
4254 g_pTempBuf, pbt[wBlockNum], 0,
4255 DeviceInfo.wPagesPerBlock)) {
4256 nand_dbg_print(NAND_DBG_WARN,
4257 "NAND Program fail in "
4258 "%s, Line %d, Function: %s, "
4261 __FILE__, __LINE__, __func__,
4262 (int)pbt[wBlockNum]);
4264 MARK_BLOCK_AS_BAD(pbt[wBlockNum]);
4269 wResult = GLOB_LLD_Read_Page_Main(g_pTempBuf,
4270 dwOldBlockAddr, 0, DeviceInfo.wPagesPerBlock);
4271 if (wResult == FAIL)
4274 if (wResult != FAIL) {
4275 /* This is a bug. At this time, pbt[wBlockNum]
4276 is still the physical address of
4277 discard block, and should not be write.
4278 Have fixed it as below.
4279 -- Yunpeng 2008.12.19
4281 wResult = GLOB_LLD_Write_Page_Main(g_pTempBuf,
4283 DeviceInfo.wPagesPerBlock);
4284 if (wResult == FAIL) {
4285 nand_dbg_print(NAND_DBG_WARN,
4286 "NAND Program fail in "
4287 "%s, Line %d, Function: %s, "
4290 __FILE__, __LINE__, __func__,
4291 (int)wReplacedNode);
4292 MARK_BLOCK_AS_BAD(wReplacedNode);
4294 pbt[wBlockNum] = wReplacedNode;
4295 pbt[wLeastReadIndex] = wTempNode;
4299 if ((wResult == PASS) && (g_cBlockTableStatus !=
4300 IN_PROGRESS_BLOCK_TABLE)) {
4301 g_cBlockTableStatus = IN_PROGRESS_BLOCK_TABLE;
4302 FTL_Write_IN_Progress_Block_Table_Page();
4306 } while (wResult != PASS)