MODULE_PARM_DESC(id_bytes, "The ID bytes returned by NAND Flash 'read ID' command");
MODULE_PARM_DESC(first_id_byte, "The first byte returned by NAND Flash 'read ID' command (manufacturer ID) (obsolete)");
MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID) (obsolete)");
MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command (obsolete)");
MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command (obsolete)");
MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)");
MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanoseconds)");
MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanoseconds)");
MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
MODULE_PARM_DESC(log, "Perform logging if not zero");
MODULE_PARM_DESC(dbg, "Output debug information if not zero");
MODULE_PARM_DESC(parts, "Partition sizes (in erase blocks) separated by commas"); /* Page and erase block positions for the following parameters are independent of any partitions */
MODULE_PARM_DESC(badblocks, "Erase blocks that are initially marked bad, separated by commas");
MODULE_PARM_DESC(weakblocks, "Weak erase blocks [: remaining erase cycles (defaults to 3)]" " separated by commas e.g. 113:2 means eb 113" " can be erased only twice before failing");
MODULE_PARM_DESC(weakpages, "Weak pages [: maximum writes (defaults to 3)]" " separated by commas e.g. 1401:2 means page 1401" " can be written only twice before failing");
MODULE_PARM_DESC(bitflips, "Maximum number of random bit flips per page (zero by default)");
MODULE_PARM_DESC(gravepages, "Pages that lose data [: maximum reads (defaults to 3)]" " separated by commas e.g. 1401:2 means page 1401" " can be read only twice before failing");
MODULE_PARM_DESC(overridesize, "Specifies the NAND Flash size overriding the ID bytes. " "The size is specified in erase blocks and as the exponent of a power of two" " e.g. 5 means a size of 32 erase blocks");
MODULE_PARM_DESC(cache_file, "File to use to cache nand pages instead of memory");
MODULE_PARM_DESC(bbt, "0 OOB, 1 BBT with marker in OOB, 2 BBT with marker in data area");
MODULE_PARM_DESC(bch, "Enable BCH ecc and set how many bits should " "be correctable in 512-byte blocks");
/* The largest possible page size */ #define NS_LARGEST_PAGE_SIZE 4096
/* Simulator's output macros (logging, debugging, warning, error) */ #define NS_LOG(args...) \ do { if (log) pr_debug(" log: " args); } while(0) #define NS_DBG(args...) \ do { if (dbg) pr_debug(" debug: " args); } while(0) #define NS_WARN(args...) \ do { pr_warn(" warning: " args); } while(0) #define NS_ERR(args...) \ do { pr_err(" error: " args); } while(0) #define NS_INFO(args...) \ do { pr_info(" " args); } while(0)
/* Busy-wait delay macros (microseconds, milliseconds) */ #define NS_UDELAY(us) \ do { if (do_delays) udelay(us); } while(0) #define NS_MDELAY(us) \ do { if (do_delays) mdelay(us); } while(0)
/* Is the nandsim structure initialized ? */ #define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
/* Good operation completion status */ #define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
/* Calculate the page offset in flash RAM image by (row, column) address */ #define NS_RAW_OFFSET(ns) \
(((ns)->regs.row * (ns)->geom.pgszoob) + (ns)->regs.column)
/* Calculate the OOB offset in flash RAM image by (row, column) address */ #define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
/* Calculate the byte shift in the next page to access */ #define NS_PAGE_BYTE_SHIFT(ns) ((ns)->regs.column + (ns)->regs.off)
/* After a command is input, the simulator goes to one of the following states */ #define STATE_CMD_READ0 0x00000001 /* read data from the beginning of page */ #define STATE_CMD_READ1 0x00000002 /* read data from the second half of page */ #define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */ #define STATE_CMD_PAGEPROG 0x00000004 /* start page program */ #define STATE_CMD_READOOB 0x00000005 /* read OOB area */ #define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */ #define STATE_CMD_STATUS 0x00000007 /* read status */ #define STATE_CMD_SEQIN 0x00000009 /* sequential data input */ #define STATE_CMD_READID 0x0000000A /* read ID */ #define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */ #define STATE_CMD_RESET 0x0000000C /* reset */ #define STATE_CMD_RNDOUT 0x0000000D /* random output command */ #define STATE_CMD_RNDOUTSTART 0x0000000E /* random output start command */ #define STATE_CMD_MASK 0x0000000F /* command states mask */
/* After an address is input, the simulator goes to one of these states */ #define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */ #define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */ #define STATE_ADDR_COLUMN 0x00000030 /* column address was accepted */ #define STATE_ADDR_ZERO 0x00000040 /* one byte zero address was accepted */ #define STATE_ADDR_MASK 0x00000070 /* address states mask */
/* During data input/output the simulator is in these states */ #define STATE_DATAIN 0x00000100 /* waiting for data input */ #define STATE_DATAIN_MASK 0x00000100 /* data input states mask */
#define STATE_DATAOUT 0x00001000 /* waiting for page data output */ #define STATE_DATAOUT_ID 0x00002000 /* waiting for ID bytes output */ #define STATE_DATAOUT_STATUS 0x00003000 /* waiting for status output */ #define STATE_DATAOUT_MASK 0x00007000 /* data output states mask */
/* Previous operation is done, ready to accept new requests */ #define STATE_READY 0x00000000
/* This state is used to mark that the next state isn't known yet */ #define STATE_UNKNOWN 0x10000000
/* Simulator's actions bit masks */ #define ACTION_CPY 0x00100000 /* copy page/OOB to the internal buffer */ #define ACTION_PRGPAGE 0x00200000 /* program the internal buffer to flash */ #define ACTION_SECERASE 0x00300000 /* erase sector */ #define ACTION_ZEROOFF 0x00400000 /* don't add any offset to address */ #define ACTION_HALFOFF 0x00500000 /* add to address half of page */ #define ACTION_OOBOFF 0x00600000 /* add to address OOB offset */ #define ACTION_MASK 0x00700000 /* action mask */
#define NS_OPER_NUM 13/* Number of operations supported by the simulator */ #define NS_OPER_STATES 6/* Maximum number of states in operation */
uint busw; /* flash chip bus width (8 or 16) */
u_char ids[8]; /* chip's ID bytes */
uint32_t options; /* chip's characteristic bits */
uint32_t state; /* current chip state */
uint32_t nxstate; /* next expected state */
uint32_t *op; /* current operation, NULL operations isn't known yet */
uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
uint16_t npstates; /* number of previous states saved */
uint16_t stateidx; /* current state index */
/* The simulated NAND flash pages array */ union ns_mem *pages;
/* Slab allocator for nand pages */ struct kmem_cache *nand_pages_slab;
/* Internal buffer of page + OOB size bytes */ union ns_mem buf;
/* NAND flash "geometry" */ struct {
uint64_t totsz; /* total flash size, bytes */
uint32_t secsz; /* flash sector (erase block) size, bytes */
uint pgsz; /* NAND flash page size, bytes */
uint oobsz; /* page OOB area size, bytes */
uint64_t totszoob; /* total flash size including OOB, bytes */
uint pgszoob; /* page size including OOB , bytes*/
uint secszoob; /* sector size including OOB, bytes */
uint pgnum; /* total number of pages */
uint pgsec; /* number of pages per sector */
uint secshift; /* bits number in sector size */
uint pgshift; /* bits number in page size */
uint pgaddrbytes; /* bytes per page address */
uint secaddrbytes; /* bytes per sector address */
uint idbytes; /* the number ID bytes that this chip outputs */
} geom;
/* NAND flash internal registers */ struct { unsigned command; /* the command register */
u_char status; /* the status register */
uint row; /* the page number */
uint column; /* the offset within page */
uint count; /* internal counter */
uint num; /* number of bytes which must be processed */
uint off; /* fixed page offset */
} regs;
/* NAND flash lines state */ struct { int ce; /* chip Enable */ int cle; /* command Latch Enable */ int ale; /* address Latch Enable */ int wp; /* write Protect */
} lines;
/* Fields needed when using a cache file */ struct file *cfile; /* Open file */ unsignedlong *pages_written; /* Which pages have been written */ void *file_buf; struct page *held_pages[NS_MAX_HELD_PAGES]; int held_cnt;
/* debugfs entry */ struct dentry *dent;
};
/* *Operationsarray.Toperformanyoperationthesimulatormustpass *throughthecorrespondentstateschain.
*/ staticstruct nandsim_operations {
uint32_t reqopts; /* options which are required to perform the operation */
uint32_t states[NS_OPER_STATES]; /* operation's states */
} ops[NS_OPER_NUM] = { /* Read page + OOB from the beginning */
{OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY,
STATE_DATAOUT, STATE_READY}}, /* Read page + OOB from the second half */
{OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY,
STATE_DATAOUT, STATE_READY}}, /* Read OOB */
{OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY,
STATE_DATAOUT, STATE_READY}}, /* Program page starting from the beginning */
{OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}}, /* Program page starting from the beginning */
{OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}}, /* Program page starting from the second half */
{OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}}, /* Program OOB */
{OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE,
STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}}, /* Erase sector */
{OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}}, /* Read status */
{OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}}, /* Read ID */
{OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}}, /* Large page devices read page */
{OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
STATE_DATAOUT, STATE_READY}}, /* Large page devices random page read */
{OPT_LARGEPAGE, {STATE_CMD_RNDOUT, STATE_ADDR_COLUMN, STATE_CMD_RNDOUTSTART | ACTION_CPY,
STATE_DATAOUT, STATE_READY}},
};
/* Calc wear stats */ for (i = 0; i < wear_eb_count; ++i) { unsignedlong wear = erase_block_wear[i]; if (wear < wmin)
wmin = wear; if (wear > wmax)
wmax = wear;
tot += wear;
}
for (i = 0; i < 9; ++i) {
deciles[i] = 0;
decile_max[i] = (wmax * (i + 1) + 5) / 10;
}
deciles[9] = 0;
decile_max[9] = wmax; for (i = 0; i < wear_eb_count; ++i) { int d; unsignedlong wear = erase_block_wear[i]; for (d = 0; d < 10; ++d) if (wear <= decile_max[d]) {
deciles[d] += 1; break;
}
}
avg = tot / wear_eb_count;
/* Output wear report */
seq_printf(m, "Total numbers of erases: %lu\n", tot);
seq_printf(m, "Number of erase blocks: %u\n", wear_eb_count);
seq_printf(m, "Average number of erases: %lu\n", avg);
seq_printf(m, "Maximum number of erases: %lu\n", wmax);
seq_printf(m, "Minimum number of erases: %lu\n", wmin); for (i = 0; i < 10; ++i) { unsignedlong from = (i ? decile_max[i - 1] + 1 : 0); if (from > decile_max[i]) continue;
seq_printf(m, "Number of ebs with erase counts from %lu to %lu : %lu\n",
from,
decile_max[i],
deciles[i]);
}
/* *Justskipdebugfsinitializationwhenthedebugfsdirectoryis *missing.
*/ if (IS_ERR_OR_NULL(root)) { if (IS_ENABLED(CONFIG_DEBUG_FS) &&
!IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
NS_WARN("CONFIG_MTD_PARTITIONED_MASTER must be enabled to expose debugfs stuff\n"); return0;
}
ns->pages = vmalloc(array_size(sizeof(union ns_mem), ns->geom.pgnum)); if (!ns->pages) {
NS_ERR("alloc_device: unable to allocate page array\n"); return -ENOMEM;
} for (i = 0; i < ns->geom.pgnum; i++) {
ns->pages[i].byte = NULL;
}
ns->nand_pages_slab = kmem_cache_create("nandsim",
ns->geom.pgszoob, 0, 0, NULL); if (!ns->nand_pages_slab) {
NS_ERR("cache_create: unable to create kmem_cache\n");
err = -ENOMEM; goto err_free_pg;
}
return0;
err_free_pg:
vfree(ns->pages);
return err;
}
/* *Freeanyallocatedpages,andfreethearrayofpagepointers.
*/ staticvoid ns_free_device(struct nandsim *ns)
{ int i;
if (ns->cfile) {
kfree(ns->file_buf);
vfree(ns->pages_written);
filp_close(ns->cfile, NULL); return;
}
if (ns->pages) { for (i = 0; i < ns->geom.pgnum; i++) { if (ns->pages[i].byte)
kmem_cache_free(ns->nand_pages_slab,
ns->pages[i].byte);
}
kmem_cache_destroy(ns->nand_pages_slab);
vfree(ns->pages);
}
}
list_for_each_entry(gp, &grave_pages, list) if (gp->page_no == page_no) { if (gp->reads_done >= gp->max_reads) return1;
gp->reads_done += 1; return0;
} return0;
}
staticint ns_setup_wear_reporting(struct mtd_info *mtd)
{
wear_eb_count = div_u64(mtd->size, mtd->erasesize);
erase_block_wear = kcalloc(wear_eb_count, sizeof(unsignedlong), GFP_KERNEL); if (!erase_block_wear) {
NS_ERR("Too many erase blocks for wear reporting\n"); return -ENOMEM;
} return0;
}
staticvoid ns_update_wear(unsignedint erase_block_no)
{ if (!erase_block_wear) return;
total_wear += 1; /* *TODO:Notifythisthroughadebugfsentry, *insteadofshowinganerrormessage.
*/ if (total_wear == 0)
NS_ERR("Erase counter total overflow\n");
erase_block_wear[erase_block_no] += 1; if (erase_block_wear[erase_block_no] == 0)
NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
}
/* *Returnsthestringrepresentationof'state'state.
*/ staticchar *ns_get_state_name(uint32_t state)
{ switch (NS_STATE(state)) { case STATE_CMD_READ0: return"STATE_CMD_READ0"; case STATE_CMD_READ1: return"STATE_CMD_READ1"; case STATE_CMD_PAGEPROG: return"STATE_CMD_PAGEPROG"; case STATE_CMD_READOOB: return"STATE_CMD_READOOB"; case STATE_CMD_READSTART: return"STATE_CMD_READSTART"; case STATE_CMD_ERASE1: return"STATE_CMD_ERASE1"; case STATE_CMD_STATUS: return"STATE_CMD_STATUS"; case STATE_CMD_SEQIN: return"STATE_CMD_SEQIN"; case STATE_CMD_READID: return"STATE_CMD_READID"; case STATE_CMD_ERASE2: return"STATE_CMD_ERASE2"; case STATE_CMD_RESET: return"STATE_CMD_RESET"; case STATE_CMD_RNDOUT: return"STATE_CMD_RNDOUT"; case STATE_CMD_RNDOUTSTART: return"STATE_CMD_RNDOUTSTART"; case STATE_ADDR_PAGE: return"STATE_ADDR_PAGE"; case STATE_ADDR_SEC: return"STATE_ADDR_SEC"; case STATE_ADDR_ZERO: return"STATE_ADDR_ZERO"; case STATE_ADDR_COLUMN: return"STATE_ADDR_COLUMN"; case STATE_DATAIN: return"STATE_DATAIN"; case STATE_DATAOUT: return"STATE_DATAOUT"; case STATE_DATAOUT_ID: return"STATE_DATAOUT_ID"; case STATE_DATAOUT_STATUS: return"STATE_DATAOUT_STATUS"; case STATE_READY: return"STATE_READY"; case STATE_UNKNOWN: return"STATE_UNKNOWN";
}
case NAND_CMD_READ0: case NAND_CMD_READ1: case NAND_CMD_READSTART: case NAND_CMD_PAGEPROG: case NAND_CMD_READOOB: case NAND_CMD_ERASE1: case NAND_CMD_STATUS: case NAND_CMD_SEQIN: case NAND_CMD_READID: case NAND_CMD_ERASE2: case NAND_CMD_RESET: case NAND_CMD_RNDOUT: case NAND_CMD_RNDOUTSTART: return0;
default: return1;
}
}
/* *Returnsstateaftercommandisacceptedbycommandnumber.
*/ static uint32_t ns_get_state_by_command(unsigned command)
{ switch (command) { case NAND_CMD_READ0: return STATE_CMD_READ0; case NAND_CMD_READ1: return STATE_CMD_READ1; case NAND_CMD_PAGEPROG: return STATE_CMD_PAGEPROG; case NAND_CMD_READSTART: return STATE_CMD_READSTART; case NAND_CMD_READOOB: return STATE_CMD_READOOB; case NAND_CMD_ERASE1: return STATE_CMD_ERASE1; case NAND_CMD_STATUS: return STATE_CMD_STATUS; case NAND_CMD_SEQIN: return STATE_CMD_SEQIN; case NAND_CMD_READID: return STATE_CMD_READID; case NAND_CMD_ERASE2: return STATE_CMD_ERASE2; case NAND_CMD_RESET: return STATE_CMD_RESET; case NAND_CMD_RNDOUT: return STATE_CMD_RNDOUT; case NAND_CMD_RNDOUTSTART: return STATE_CMD_RNDOUTSTART;
}
if (opsfound == 0) { /* Nothing was found. Try to ignore previous commands (if any) and search again */ if (ns->npstates != 0) {
NS_DBG("find_operation: no operation found, try again with state %s\n",
ns_get_state_name(ns->state));
ns->npstates = 0; return ns_find_operation(ns, 0);
}
NS_DBG("find_operation: no operations found\n");
ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); return -2;
}
if (flag) { /* This shouldn't happen */
NS_DBG("find_operation: BUG, operation must be known if address is input\n"); return -2;
}
NS_DBG("find_operation: there is still ambiguity\n");
ns->pstates[ns->npstates++] = ns->state;
return -1;
}
staticvoid ns_put_pages(struct nandsim *ns)
{ int i;
for (i = 0; i < ns->held_cnt; i++)
put_page(ns->held_pages[i]);
}
/* Get page cache pages in advance to provide NOFS memory allocation */ staticint ns_get_pages(struct nandsim *ns, struct file *file, size_t count,
loff_t pos)
{
pgoff_t index, start_index, end_index; struct page *page; struct address_space *mapping = file->f_mapping;
if (ns_read_error(page_no)) {
get_random_bytes(ns->buf.byte, num);
NS_WARN("simulating read error in page %u\n", page_no); return1;
} return0;
}
staticvoid ns_do_bit_flips(struct nandsim *ns, int num)
{ if (bitflips && get_random_u16() < (1 << 6)) { int flips = 1; if (bitflips > 1)
flips = get_random_u32_inclusive(1, bitflips); while (flips--) { int pos = get_random_u32_below(num * 8);
ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
NS_WARN("read_page: flipping bit %d in page %d " "reading from %d ecc: corrected=%u failed=%u\n",
pos, ns->regs.row, NS_PAGE_BYTE_SHIFT(ns),
nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
}
}
}
/* *FilltheNANDbufferwithdatareadfromthespecifiedpage.
*/ staticvoid ns_read_page(struct nandsim *ns, int num)
{ union ns_mem *mypage;
if (ns->cfile) { if (!test_bit(ns->regs.row, ns->pages_written)) {
NS_DBG("read_page: page %d not written\n", ns->regs.row);
memset(ns->buf.byte, 0xFF, num);
} else {
loff_t pos;
ssize_t tx;
NS_DBG("read_page: page %d written, reading from %d\n",
ns->regs.row, NS_PAGE_BYTE_SHIFT(ns)); if (ns_do_read_error(ns, num)) return;
pos = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
tx = ns_read_file(ns, ns->cfile, ns->buf.byte, num,
pos); if (tx != num) {
NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx); return;
}
ns_do_bit_flips(ns, num);
} return;
}
mypage = NS_GET_PAGE(ns); if (mypage->byte == NULL) {
NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
memset(ns->buf.byte, 0xFF, num);
} else {
NS_DBG("read_page: page %d allocated, reading from %d\n",
ns->regs.row, NS_PAGE_BYTE_SHIFT(ns)); if (ns_do_read_error(ns, num)) return;
memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
ns_do_bit_flips(ns, num);
}
}
/* *Eraseallpagesinthespecifiedsector.
*/ staticvoid ns_erase_sector(struct nandsim *ns)
{ union ns_mem *mypage; int i;
if (ns->cfile) { for (i = 0; i < ns->geom.pgsec; i++) if (__test_and_clear_bit(ns->regs.row + i,
ns->pages_written)) {
NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
} return;
}
mypage = NS_GET_PAGE(ns); for (i = 0; i < ns->geom.pgsec; i++) { if (mypage->byte != NULL) {
NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
kmem_cache_free(ns->nand_pages_slab, mypage->byte);
mypage->byte = NULL;
}
mypage++;
}
}
/* *ProgramthespecifiedpagewiththecontentsfromtheNANDbuffer.
*/ staticint ns_prog_page(struct nandsim *ns, int num)
{ int i; union ns_mem *mypage;
u_char *pg_off;
if (ns->cfile) {
loff_t off;
ssize_t tx; int all;
NS_DBG("prog_page: writing page %d\n", ns->regs.row);
pg_off = ns->file_buf + NS_PAGE_BYTE_SHIFT(ns);
off = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off; if (!test_bit(ns->regs.row, ns->pages_written)) {
all = 1;
memset(ns->file_buf, 0xff, ns->geom.pgszoob);
} else {
all = 0;
tx = ns_read_file(ns, ns->cfile, pg_off, num, off); if (tx != num) {
NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx); return -1;
}
} for (i = 0; i < num; i++)
pg_off[i] &= ns->buf.byte[i]; if (all) {
loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
tx = ns_write_file(ns, ns->cfile, ns->file_buf,
ns->geom.pgszoob, pos); if (tx != ns->geom.pgszoob) {
NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx); return -1;
}
__set_bit(ns->regs.row, ns->pages_written);
} else {
tx = ns_write_file(ns, ns->cfile, pg_off, num, off); if (tx != num) {
NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx); return -1;
}
} return0;
}
/* Check that page address input is correct */ if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row); return -1;
}
switch (action) {
case ACTION_CPY: /* *Copypagedatatotheinternalbuffer.
*/
/* Column shouldn't be very large */ if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
NS_ERR("do_state_action: column number is too large\n"); break;
}
num = ns->geom.pgszoob - NS_PAGE_BYTE_SHIFT(ns);
ns_read_page(ns, num);
NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
num, NS_RAW_OFFSET(ns) + ns->regs.off);
NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
ns->regs.row, NS_RAW_OFFSET(ns));
NS_LOG("erase sector %u\n", erase_block_no);
ns_erase_sector(ns);
NS_MDELAY(erase_delay);
if (erase_block_wear)
ns_update_wear(erase_block_no);
if (ns_erase_error(erase_block_no)) {
NS_WARN("simulating erase failure in erase block %u\n", erase_block_no); return -1;
}
break;
case ACTION_PRGPAGE: /* *Programpage-moveinternalbufferdatatothepage.
*/
if (ns->lines.wp) {
NS_WARN("do_state_action: device is write-protected, programm\n"); return -1;
}
num = ns->geom.pgszoob - NS_PAGE_BYTE_SHIFT(ns); if (num != ns->regs.count) {
NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
ns->regs.count, num); return -1;
}
if (ns_prog_page(ns, num) == -1) return -1;
page_no = ns->regs.row;
NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
NS_LOG("programm page %d\n", ns->regs.row);
NS_DBG("switch_state: operation is unknown, try to find it\n");
if (ns_find_operation(ns, 0)) return;
}
/* See, whether we need to do some action */ if ((ns->state & ACTION_MASK) &&
ns_do_state_action(ns, ns->state) < 0) {
ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); return;
}
/* For 16x devices column means the page offset in words */ if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
NS_DBG("switch_state: double the column number for 16x device\n");
ns->regs.column <<= 1;
}
if (NS_STATE(ns->nxstate) == STATE_READY) { /* *Thecurrentstateisthelast.ReturntoSTATE_READY
*/
u_char status = NS_STATUS_OK(ns);
/* In case of data states, see if all bytes were input/output */ if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
&& ns->regs.count != ns->regs.num) {
NS_WARN("switch_state: not all bytes were processed, %d left\n",
ns->regs.num - ns->regs.count);
status = NS_STATUS_FAILED(ns);
}
NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
/* Sanity and correctness checks */ if (!ns->lines.ce) {
NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb); return outb;
} if (ns->lines.ale || ns->lines.cle) {
NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb); return outb;
} if (!(ns->state & STATE_DATAOUT_MASK)) {
NS_WARN("read_byte: unexpected data output cycle, state is %s return %#x\n",
ns_get_state_name(ns->state), (uint)outb); return outb;
}
/* Status register may be read as many times as it is wanted */ if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
NS_DBG("read_byte: return %#x status\n", ns->regs.status); return ns->regs.status;
}
/* Check if there is any data in the internal buffer which may be read */ if (ns->regs.count == ns->regs.num) {
NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb); return outb;
}
/* Sanity and correctness checks */ if (!ns->lines.ce) {
NS_ERR("write_byte: chip is disabled, ignore write\n"); return;
} if (ns->lines.ale && ns->lines.cle) {
NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n"); return;
}
if (ns->lines.cle == 1) { /* *Thebytewrittenisacommand.
*/
ns->regs.count = 0; switch (NS_STATE(ns->nxstate)) { case STATE_ADDR_PAGE:
ns->regs.num = ns->geom.pgaddrbytes; break; case STATE_ADDR_SEC:
ns->regs.num = ns->geom.secaddrbytes; break; case STATE_ADDR_ZERO:
ns->regs.num = 1; break; default:
BUG();
}
}
/* Check that chip is expecting address */ if (!(ns->nxstate & STATE_ADDR_MASK)) {
NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, switch to STATE_READY\n",
(uint)byte, ns_get_state_name(ns->nxstate));
ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); return;
}
/* Check if this is expected byte */ if (ns->regs.count == ns->regs.num) {
NS_ERR("write_byte: no more address bytes expected\n");
ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); return;
}
if (ns->regs.count == ns->regs.num) {
NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
ns_switch_state(ns);
}
} else { /* *Thebytewrittenisaninputdata.
*/
/* Check that chip is expecting data input */ if (!(ns->state & STATE_DATAIN_MASK)) {
NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, switch to %s\n",
(uint)byte, ns_get_state_name(ns->state),
ns_get_state_name(STATE_READY));
ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); return;
}
/* Check if this is expected byte */ if (ns->regs.count == ns->regs.num) {
NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
ns->regs.num); return;
}
/* Check that chip is expecting data input */ if (!(ns->state & STATE_DATAIN_MASK)) {
NS_ERR("write_buf: data input isn't expected, state is %s, switch to STATE_READY\n",
ns_get_state_name(ns->state));
ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); return;
}
/* Check if these are expected bytes */ if (ns->regs.count + len > ns->regs.num) {
NS_ERR("write_buf: too many input bytes\n");
ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); return;
}
/* Sanity and correctness checks */ if (!ns->lines.ce) {
NS_ERR("read_buf: chip is disabled\n"); return;
} if (ns->lines.ale || ns->lines.cle) {
NS_ERR("read_buf: ALE or CLE pin is high\n"); return;
} if (!(ns->state & STATE_DATAOUT_MASK)) {
NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
ns_get_state_name(ns->state)); return;
}
if (NS_STATE(ns->state) != STATE_DATAOUT) { int i;
for (i = 0; i < len; i++)
buf[i] = ns_nand_read_byte(chip);
return;
}
/* Check if these are expected bytes */ if (ns->regs.count + len > ns->regs.num) {
NS_ERR("read_buf: too many bytes to read\n");
ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); return;
}
switch (instr->type) { case NAND_OP_CMD_INSTR:
ns->lines.cle = 1;
ns_nand_write_byte(chip, instr->ctx.cmd.opcode); break; case NAND_OP_ADDR_INSTR:
ns->lines.ale = 1; for (i = 0; i < instr->ctx.addr.naddrs; i++)
ns_nand_write_byte(chip, instr->ctx.addr.addrs[i]); break; case NAND_OP_DATA_IN_INSTR:
ns_nand_read_buf(chip, instr->ctx.data.buf.in, instr->ctx.data.len); break; case NAND_OP_DATA_OUT_INSTR:
ns_nand_write_buf(chip, instr->ctx.data.buf.out, instr->ctx.data.len); break; case NAND_OP_WAITRDY_INSTR: /* we are always ready */ break;
}
}
/* Do not bother supporting small page devices */ if (nsmtd->oobsize < 64 || !eccsteps) {
NS_ERR("BCH not available on small page devices\n"); return -EINVAL;
}
if (((eccbytes * eccsteps) + 2) > nsmtd->oobsize) {
NS_ERR("Invalid BCH value %u\n", bch); return -EINVAL;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.