-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftl.c
executable file
·700 lines (541 loc) · 20.9 KB
/
ftl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Copyright 2011 INDILINX Co., Ltd.
//
// This file is part of Jasmine.
//
// Jasmine is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Jasmine is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Jasmine. See the file COPYING.
// If not, see <http://www.gnu.org/licenses/>.
#include "jasmine.h"
// TODO to delete after the implementation of bloc map addresing
#define PAGE_MAP_ADDR BLK_MAP_ADDR
#define PAGE_MAP_BYTES BLK_MAP_BYTES
#define VC_MAX 0xCDCD// means that bloc i is invalid (bad block )
//----------------------------------
// metadata structure
//----------------------------------
typedef struct _physical_addr
{
UINT32 blk;
UINT32 page;
}physical_addr;
static void sanity_check(void);
static BOOL32 is_bad_block(UINT32 const bank, UINT32 const vblk_offset);
static UINT32 get_physical_address(UINT32 const lpage_addr);
static void update_physical_address(UINT32 const lpage_addr, UINT32 const new_bank, UINT32 const new_row);
static UINT32 get_free_page(UINT32 const bank);
static BOOL32 check_format_mark(void);
static void write_format_mark(void);
static void format(void);
UINT32 g_ftl_read_buf_id;
UINT32 g_ftl_write_buf_id;
static UINT32 g_target_row[NUM_BANKS];
static UINT32 g_target_bank;
static volatile UINT32 g_read_fail_count;
static volatile UINT32 g_program_fail_count;
static volatile UINT32 g_erase_fail_count;
static UINT32 g_scan_list_entries[NUM_BANKS];
//************************ usefull macros ************************//
#define get_num_bank(lpn) ((lpn) / PAGES_PER_BANK)
//************************* my vars ****************************//
static UINT32 log_offset ;
//************************* Myfunctions *************************//
void showlogs();
static int is_in_log(UINT32 const lpage_addr); // < 0 if it doesent' exist
static physical_addr get_physical_data_addr(UINT32 const lpage_addr);
static void set_physical_data_addr(UINT32 const lpage_addr ,UINT32 const p_blk);
static BOOL32 check_if_exist(UINT32 lpage_addr);
static UINT32 get_new_blk();
static physical_addr get_new_log_page();
static void write_page(UINT32 const lpn, UINT32 const sect_offset, UINT32 const num_sectors);
//***************************************************************//
//************************* Myfunctions *************************//
static int is_in_log(UINT32 const lpage_addr) // if the page existe in the log block i >0
{int i;
for (i = log_offset ; i >= 0 ; i--)
{
if(read_dram_32(LOG_MAP_ADDR+sizeof(UINT32) * i) == lpage_addr )
break;
}
return i ;
}
static physical_addr get_physical_data_addr(UINT32 const lpage_addr)
{
physical_addr p_addr;
int offset = is_in_log(lpage_addr) ;
if ( offset >= 0 ) // the page exist in log blocks
{ int blk = offset / PAGES_PER_BLK ;
p_addr.page = offset % PAGES_PER_BLK;
p_addr.blk = read_dram_32(LOG_BLKS_MAP_ADDR+sizeof(UINT32) * blk);
}
else // the page exist in data block
{
UINT32 blk = lpage_addr / PAGES_PER_BLK ;
p_addr.page= lpage_addr % PAGES_PER_BLK ;
p_addr.blk = read_dram_32(BLK_MAP_ADDR + sizeof(UINT32)*blk);
}
return p_addr;
}
static void set_physical_data_addr(UINT32 const lpage_addr ,UINT32 const p_blk)
{
UINT32 l_blk = lpage_addr / PAGES_PER_BLK ;
write_dram_32(BLK_MAP_ADDR + sizeof(UINT32)*l_blk,p_blk);
}
static BOOL32 check_if_exist(UINT32 lpage_addr)
{
UINT32 blk = lpage_addr / PAGES_PER_BLK ;
return read_dram_32(BLK_MAP_ADDR + sizeof(UINT32)*blk) != NULL ;
}
//***************************************************************//
void ftl_open(void)
{
sanity_check();
showlogs();
// STEP 1 - read scan lists from NAND flash
scan_list_t* scan_list = (scan_list_t*) SCAN_LIST_ADDR;
UINT32 bank;
// Since we are going to check the flash interrupt flags within this function, ftl_isr() should not be called.
disable_irq();
flash_clear_irq(); // clear any flash interrupt flags that might have been set
for (bank = 0; bank < NUM_BANKS; bank++)
{
SETREG(FCP_CMD, FC_COL_ROW_READ_OUT); // FC_COL_ROW_READ_OUT = sensing and data output
SETREG(FCP_OPTION, FO_E); // scan list was written in 1-plane mode by install.exe, so there is no FO_P
SETREG(FCP_DMA_ADDR, scan_list + bank); // target address should be DRAM or SRAM (see flash.h for rules)
SETREG(FCP_DMA_CNT, SCAN_LIST_SIZE); // number of bytes for data output
SETREG(FCP_COL, 0);
SETREG(FCP_ROW_L(bank), SCAN_LIST_PAGE_OFFSET); // scan list was written to this position by install.exe
SETREG(FCP_ROW_H(bank), SCAN_LIST_PAGE_OFFSET); // Tutorial FTL always uses the same row addresses for high chip and low chip
flash_issue_cmd(bank, RETURN_ON_ISSUE); // Take a look at the source code of flash_issue_cmd() now.
}
// This while() statement waits the last issued command to be accepted.
// If bit #0 of WR_STAT is one, a flash command is in the Waiting Room, because the target bank has not accepted it yet.
while ((GETREG(WR_STAT) & 0x00000001) != 0);
// Now, FC_COL_ROW_READ_OUT commands are accepted by all the banks.
// Before checking whether scan lists are corrupted or not, we have to wait the completion of read operations.
// This code shows how to wait for ALL the banks to become idle.
while (GETREG(MON_CHABANKIDLE) != 0);
// Now we can check the flash interrupt flags.
for (bank = 0; bank < NUM_BANKS; bank++)
{
UINT32 num_entries = NULL;
UINT32 result = OK;
if (BSP_INTR(bank) & FIRQ_DATA_CORRUPT)
{
// Too many bits are corrupted so that they cannot be corrected by ECC.
result = FAIL;
}
else
{
// Even though the scan list is not corrupt, we have to check whether its contents make sense.
UINT32 i;
num_entries = read_dram_16(&(scan_list[bank].num_entries));
if (num_entries > SCAN_LIST_ITEMS)
{
result = FAIL; // We cannot trust this scan list. Perhaps a software bug.
}
else
{
for (i = 0; i < num_entries; i++)
{
UINT16 entry = read_dram_16(&(scan_list[bank].list[i]));
UINT16 pblk_offset = entry & 0x7FFF;
if (pblk_offset == 0 || pblk_offset >= PBLKS_PER_BANK)
{
#if OPTION_REDUCED_CAPACITY == FALSE
result = FAIL; // We cannot trust this scan list. Perhaps a software bug.
#endif
}
else
{
// Bit position 15 of scan list entry is high-chip/low-chip flag.
// Remove the flag in order to make is_bad_block() simple.
write_dram_16(&(scan_list[bank].list[i]), pblk_offset);
}
}
}
}
if (result == FAIL)
{
mem_set_dram(scan_list + bank, 0, SCAN_LIST_SIZE);
g_scan_list_entries[bank] = 0;
}
else
{
write_dram_16(&(scan_list[bank].num_entries), 0);
g_scan_list_entries[bank] = num_entries;
}
}
// STEP 2 - If necessary, do low-level format
// format() should be called after loading scan lists, because format() calls is_bad_block().
if (check_format_mark() == FALSE)
{
// When ftl_open() is called for the first time (i.e. the SSD is powered up the first time)
// format() is called.
format();
}
// STEP 3 - initialize page mapping table
// The page mapping table is too large to fit in SRAM.
mem_set_dram(BLK_MAP_ADDR,NULL,BLK_MAP_BYTES);
mem_set_dram(PAGE_MAP_ADDR,NULL,PAGE_MAP_BYTES);
mem_set_dram(PAGE_MAP_ADDR, NULL, PAGE_MAP_BYTES);
// STEP 4 - initialize global variables that belong to FTL
g_ftl_read_buf_id = 0;
g_ftl_write_buf_id = 0;
g_target_bank = 0;
for (bank = 0; bank < NUM_BANKS; bank++)
{
g_target_row[bank] = PAGES_PER_VBLK;
}
flash_clear_irq();
// This example FTL can handle runtime bad block interrupts and read fail (uncorrectable bit errors) interrupts
SETREG(INTR_MASK, FIRQ_DATA_CORRUPT | FIRQ_BADBLK_L | FIRQ_BADBLK_H);
SETREG(FCONF_PAUSE, FIRQ_DATA_CORRUPT | FIRQ_BADBLK_L | FIRQ_BADBLK_H);
enable_irq();
}
void ftl_read(UINT32 const lba, UINT32 const total_sectors)
{
UINT32 bank, row, num_sectors_to_read, temp;
UINT32 lpage_addr = lba / SECTORS_PER_PAGE; // logical page address
UINT32 sect_offset = lba % SECTORS_PER_PAGE; // sector offset within the page
UINT32 sectors_remain = total_sectors;
physical_addr p_addr ;
while (sectors_remain != 0) // one page per iteration
{
if (sect_offset + sectors_remain < SECTORS_PER_PAGE) // sect_offset ==0 or
{
num_sectors_to_read = sectors_remain;
}
else
{
num_sectors_to_read = SECTORS_PER_PAGE - sect_offset;
}
p_addr = get_physical_data_addr(lpage_addr);
temp = p_addr.blk;
if (p_addr.blk != NULL)
{
nand_page_ptread_to_host(bank,
p_addr.blk,
p_addr.page,
sect_offset,
num_sectors_to_read);
}
else // i really hate this party -_-"
{
UINT32 next_read_buf_id = (g_ftl_read_buf_id + 1) % NUM_RD_BUFFERS;
#if OPTION_FTL_TEST == 0
while (next_read_buf_id == GETREG(SATA_RBUF_PTR)); // wait if the read buffer is full (slow host)
#endif
// fix bug @ v.1.0.6
// Send 0xFF...FF to host when the host request to read the sector that has never been written.
// In old version, for example, if the host request to read unwritten sector 0 after programming in sector 1, Jasmine would send 0x00...00 to host.
// However, if the host already wrote to sector 1, Jasmine would send 0xFF...FF to host when host request to read sector 0. (ftl_read() in ftl_xxx/ftl.c)
mem_set_dram(RD_BUF_PTR(g_ftl_read_buf_id) + sect_offset*BYTES_PER_SECTOR,
0xFFFFFFFF, num_sectors_to_read*BYTES_PER_SECTOR);
flash_finish();
SETREG(BM_STACK_RDSET, next_read_buf_id); // change bm_read_limit
SETREG(BM_STACK_RESET, 0x02); // change bm_read_limit
g_ftl_read_buf_id = next_read_buf_id;
}
sect_offset = 0;
sectors_remain -= num_sectors_to_read;
lpage_addr++;
}
}
void ftl_write(UINT32 const lba, UINT32 const total_sectors)
{
uart_printf("lba %d \n total secotrs %d",lba,total_sectors);
UINT32 remain_sects, num_sectors_to_write;
UINT32 lpn, sect_offset;
lpn = lba / SECTORS_PER_PAGE;
sect_offset = lba % SECTORS_PER_PAGE;
remain_sects = total_sectors;
while (remain_sects != 0)
{
if ((sect_offset + remain_sects) < SECTORS_PER_PAGE)
{
num_sectors_to_write = remain_sects;
}
else
{
num_sectors_to_write = SECTORS_PER_PAGE - sect_offset;
}
// single page write individually
write_page(lpn, sect_offset, num_sectors_to_write);
sect_offset = 0;
remain_sects -= num_sectors_to_write;
lpn++;
}
}
static void write_page(UINT32 const lpn, UINT32 const sect_offset, UINT32 const num_sectors)
{
physical_addr p_addr ;
p_addr= get_physical_data_addr(lpn);
if(p_addr.blk == NULL) // new write operation
{
p_addr.blk = get_new_blk();
set_physical_addr(lpn,p_addr.blk);
}
else
{
p_addr = get_new_log_page();
}
}
static physical_addr get_new_log_page()
{ physical_addr p_addr;
if(log_offset < NUM_LOG_PAGES )
{ int blk = log_offset / PAGES_PER_BLK ;
p_addr.page = log_offset % PAGES_PER_BLK;
p_addr.blk = read_dram_32(LOG_BLKS_MAP_ADDR+sizeof(UINT32) * blk);
log_offset++;
}
else
{
uart_printf("no more free log blokcs ");
}
return p_addr ;
}
static UINT32 get_new_blk()
{
UINT32 vblock = mem_search_equ_dram(VCOUNT_ADDR + (bank * VBLKS_PER_BANK * sizeof(UINT16)),
sizeof(UINT16),
VBLKS_PER_BANK,
0);
if(vblock == NULL)
uart_printf("no more blks");
for(;;);
return vblock ;
}
void ftl_flush(void)
{
// do nothing
}
static BOOL32 is_bad_block(UINT32 const bank, UINT32 const vblk_offset)
{
// The scan list, which is installed by installer.c:install_block_zero(), contains physical block offsets of initial bad blocks.
// Since the parameter to is_bad_block() is not a pblk_offset but a vblk_offset, we have to do some conversion.
//
// When 1-plane mode is used, vblk_offset is equivalent to pblk_offset.
// When 2-plane mode is used, vblk_offset = pblk_offset / 2.
// Two physical blocks 0 and 1 are grouped into virtual block 0.
// Two physical blocks 2 and 3 are grouped into virtual block 1.
// Two physical blocks 4 and 5 are grouped into virtual block 2.
#if OPTION_2_PLANE
UINT32 pblk_offset;
scan_list_t* scan_list = (scan_list_t*) SCAN_LIST_ADDR;
pblk_offset = vblk_offset * NUM_PLANES;
if (mem_search_equ_dram(scan_list + bank, sizeof(UINT16), g_scan_list_entries[bank], pblk_offset) < g_scan_list_entries[bank])
{
return TRUE;
}
pblk_offset = vblk_offset * NUM_PLANES + 1;
if (mem_search_equ_dram(scan_list + bank, sizeof(UINT16), g_scan_list_entries[bank], pblk_offset) < g_scan_list_entries[bank])
{
return TRUE;
}
return FALSE;
#else
scan_list_t* scan_list = (scan_list_t*) SCAN_LIST_ADDR;
if (mem_search_equ_dram(scan_list + bank, sizeof(UINT16), g_scan_list_entries[bank], vblk_offset) < g_scan_list_entries[bank])
{
return TRUE;
}
return FALSE;
#endif
}
static UINT32 get_physical_address(UINT32 const lpage_addr)
{
// Page mapping table entry size is 4 byte.
return read_dram_32(PAGE_MAP_ADDR + lpage_addr * sizeof(UINT32));
}
static void update_physical_address(UINT32 const lpage_addr, UINT32 const new_bank, UINT32 const new_row)
{
write_dram_32(PAGE_MAP_ADDR + lpage_addr * sizeof(UINT32), new_bank * PAGES_PER_BANK + new_row);
}
static UINT32 get_free_page(UINT32 const bank)
{
// This function returns the row address for write operation.
UINT32 row;
UINT32 vblk_offset, page_offset;
row = g_target_row[bank];
vblk_offset = row / PAGES_PER_VBLK;
page_offset = row % PAGES_PER_VBLK;
if (page_offset == 0) // We are going to write to a new vblock.
{
while (is_bad_block(bank, vblk_offset) && vblk_offset < VBLKS_PER_BANK)
{
vblk_offset++; // We have to skip bad vblocks.
}
}
if (vblk_offset >= VBLKS_PER_BANK)
{
// Free vblocks are exhausted. Since this example FTL does not do garbage collection,
// no more data can be written to this SSD. The SSD stops working now.
led (1);
while (1);
}
row = vblk_offset * PAGES_PER_VBLK + page_offset;
g_target_row[bank] = row + 1;
return row;
}
static BOOL32 check_format_mark(void)
{
// This function reads a flash page from (bank #0, block #0) in order to check whether the SSD is formatted or not.
#ifdef __GNUC__
extern UINT32 size_of_firmware_image;
UINT32 firmware_image_pages = (((UINT32) (&size_of_firmware_image)) + BYTES_PER_FW_PAGE - 1) / BYTES_PER_FW_PAGE;
#else
extern UINT32 Image$$ER_CODE$$RO$$Length;
extern UINT32 Image$$ER_RW$$RW$$Length;
UINT32 firmware_image_bytes = ((UINT32) &Image$$ER_CODE$$RO$$Length) + ((UINT32) &Image$$ER_RW$$RW$$Length);
UINT32 firmware_image_pages = (firmware_image_bytes + BYTES_PER_FW_PAGE - 1) / BYTES_PER_FW_PAGE;
#endif
UINT32 format_mark_page_offset = FW_PAGE_OFFSET + firmware_image_pages;
UINT32 temp;
flash_clear_irq(); // clear any flash interrupt flags that might have been set
SETREG(FCP_CMD, FC_COL_ROW_READ_OUT);
SETREG(FCP_BANK, REAL_BANK(0));
SETREG(FCP_OPTION, FO_E);
SETREG(FCP_DMA_ADDR, FTL_BUF_ADDR); // flash -> DRAM
SETREG(FCP_DMA_CNT, BYTES_PER_SECTOR);
SETREG(FCP_COL, 0);
SETREG(FCP_ROW_L(0), format_mark_page_offset);
SETREG(FCP_ROW_H(0), format_mark_page_offset);
// At this point, we do not have to check Waiting Room status before issuing a command,
// because scan list loading has been completed just before this function is called.
SETREG(FCP_ISSUE, NULL);
// wait for the FC_COL_ROW_READ_OUT command to be accepted by bank #0
while ((GETREG(WR_STAT) & 0x00000001) != 0);
// wait until bank #0 finishes the read operation
while (BSP_FSM(0) != BANK_IDLE);
// Now that the read operation is complete, we can check interrupt flags.
temp = BSP_INTR(0) & FIRQ_ALL_FF;
// clear interrupt flags
CLR_BSP_INTR(0, 0xFF);
if (temp != 0)
{
return FALSE; // the page contains all-0xFF (the format mark does not exist.)
}
else
{
return TRUE; // the page contains something other than 0xFF (it must be the format mark)
}
}
static void write_format_mark(void)
{
// This function writes a format mark to a page at (bank #0, block #0).
#ifdef __GNUC__
extern UINT32 size_of_firmware_image;
UINT32 firmware_image_pages = (((UINT32) (&size_of_firmware_image)) + BYTES_PER_FW_PAGE - 1) / BYTES_PER_FW_PAGE;
#else
extern UINT32 Image$$ER_CODE$$RO$$Length;
extern UINT32 Image$$ER_RW$$RW$$Length;
UINT32 firmware_image_bytes = ((UINT32) &Image$$ER_CODE$$RO$$Length) + ((UINT32) &Image$$ER_RW$$RW$$Length);
UINT32 firmware_image_pages = (firmware_image_bytes + BYTES_PER_FW_PAGE - 1) / BYTES_PER_FW_PAGE;
#endif
UINT32 format_mark_page_offset = FW_PAGE_OFFSET + firmware_image_pages;
mem_set_dram(FTL_BUF_ADDR, 0, BYTES_PER_SECTOR);
SETREG(FCP_CMD, FC_COL_ROW_IN_PROG);
SETREG(FCP_BANK, REAL_BANK(0));
SETREG(FCP_OPTION, FO_E | FO_B_W_DRDY);
SETREG(FCP_DMA_ADDR, FTL_BUF_ADDR); // DRAM -> flash
SETREG(FCP_DMA_CNT, BYTES_PER_SECTOR);
SETREG(FCP_COL, 0);
SETREG(FCP_ROW_L(0), format_mark_page_offset);
SETREG(FCP_ROW_H(0), format_mark_page_offset);
// At this point, we do not have to check Waiting Room status before issuing a command,
// because we have waited for all the banks to become idle before returning from format().
SETREG(FCP_ISSUE, NULL);
// wait for the FC_COL_ROW_IN_PROG command to be accepted by bank #0
while ((GETREG(WR_STAT) & 0x00000001) != 0);
// wait until bank #0 finishes the write operation
while (BSP_FSM(0) != BANK_IDLE);
}
static void format(void)
{ led(0);
UINT32 bank, vblock, vcount_val;
uart_printf("Total FTL DRAM metadata size: %d KB", DRAM_BYTES_OTHER / 1024);
uart_printf("VBLKS_PER_BANK: %d", VBLKS_PER_BANK);
uart_printf("LBLKS_PER_BANK: %d", NUM_LPAGES / PAGES_PER_BLK / NUM_BANKS);
for (bank = 0; bank < NUM_BANKS; bank++)
{
for (vblock = 1; vblock < VBLKS_PER_BANK; vblock++)
{
vcount_val = VC_MAX;
if (is_bad_block(bank, vblock) == FALSE)
{
nand_block_erase(bank, vblock);
vcount_val = 0;
}
write_dram_16(VCOUNT_ADDR + ((bank * VBLKS_PER_BANK) + vblock) * sizeof(UINT16), vcount_val); // vcount_addr[i] ==0 means that the block i is valid
}
}
#if 0
write_format_mark();
#endif
led(1);
uart_printf("end format");
}
void ftl_isr(void)
{
UINT32 bank;
UINT32 bsp_intr_flag;
uart_printf("BSP interrupt occured...");
// interrupt pending clear (ICU)
SETREG(APB_INT_STS, INTR_FLASH);
for (bank = 0; bank < NUM_BANKS; bank++) {
while (BSP_FSM(bank) != BANK_IDLE);
// get interrupt flag from BSP
bsp_intr_flag = BSP_INTR(bank);
if (bsp_intr_flag == 0) {
continue;
}
UINT32 fc = GETREG(BSP_CMD(bank));
// BSP clear
CLR_BSP_INTR(bank, bsp_intr_flag);
// interrupt handling
if (bsp_intr_flag & FIRQ_DATA_CORRUPT) {
uart_printf("BSP interrupt at bank: 0x%x", bank);
uart_printf("FIRQ_DATA_CORRUPT occured...");
}
if (bsp_intr_flag & (FIRQ_BADBLK_H | FIRQ_BADBLK_L)) {
uart_printf("BSP interrupt at bank: 0x%x", bank);
if (fc == FC_COL_ROW_IN_PROG || fc == FC_IN_PROG || fc == FC_PROG) {
uart_printf("find runtime bad block when block program...");
}
else {
uart_printf("find runtime bad block when block erase...vblock #: %d", GETREG(BSP_ROW_H(bank)) / PAGES_PER_BLK);
ASSERT(fc == FC_ERASE);
}
}
}
}
static void sanity_check(void)
{
UINT32 dram_requirement = RD_BUF_BYTES + WR_BUF_BYTES + COPY_BUF_BYTES + FTL_BUF_BYTES
+ HIL_BUF_BYTES + TEMP_BUF_BYTES + SCAN_LIST_BYTES + PAGE_MAP_BYTES;
if (dram_requirement > DRAM_SIZE)
{
while (1);
}
}
/*********************log fcts**************/
void showlogs()
{
uart_printf("-----------logs--------------");
uart_printf("NUM_BANKS: %d",NUM_BANKS);
uart_printf("PAGES PER_BLK: %d",PAGES_PER_BLK);
uart_printf("total pages: %d , data pages %d",NUM_LPAGES ,NUM_DATA_BLKS *PAGES_PER_BLK);
uart_printf("NUM_DATA_BLKS: %d",NUM_DATA_BLKS);
uart_printf("NUM_LOG_BLKs: %d" , NUM_LOG_BLKS);
uart_printf("NUM BLKS %d -- %d",NUM_BLKS,NUM_LOG_BLKS+NUM_DATA_BLKS);
}