2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* NAND flash simulator.
|
|
|
|
*
|
|
|
|
* Author: Artem B. Bityuckiy <dedekind@oktetlabs.ru>, <dedekind@infradead.org>
|
|
|
|
*
|
2005-11-07 19:15:49 +08:00
|
|
|
* Copyright (C) 2004 Nokia Corporation
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Note: NS means "NAND Simulator".
|
|
|
|
* Note: Input means input TO flash chip, output means output FROM chip.
|
|
|
|
*
|
|
|
|
* This program 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 2, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/vmalloc.h>
|
mtd: nandsim: don't open code a do_div helper
We don't need to open code the divide function, just use div_u64 that
already exists and do the same job. While this is a straightforward
clean up, there is more to that, the real motivation for this.
While building on a cross compiling environment in armel, using gcc
4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5), I was getting the following build
error:
ERROR: "__aeabi_uldivmod" [drivers/mtd/nand/nandsim.ko] undefined!
After investigating with objdump and hand built assembly version
generated with the compiler, I narrowed __aeabi_uldivmod as being
generated from the divide function. When nandsim.c is built with
-fno-inline-functions-called-once, that happens when
CONFIG_DEBUG_SECTION_MISMATCH is enabled, the do_div optimization in
arch/arm/include/asm/div64.h doesn't work as expected with the open
coded divide function: even if the do_div we are using doesn't have a
constant divisor, the compiler still includes the else parts of the
optimized do_div macro, and translates the divisions there to use
__aeabi_uldivmod, instead of only calling __do_div_asm -> __do_div64 and
optimizing/removing everything else out.
So to reproduce, gcc 4.6 plus CONFIG_DEBUG_SECTION_MISMATCH=y and
CONFIG_MTD_NAND_NANDSIM=m should do it, building on armel.
After this change, the compiler does the intended thing even with
-fno-inline-functions-called-once, and optimizes out as expected the
constant handling in the optimized do_div on arm. As this also avoids a
build issue, I'm marking for Stable, as I think is applicable for this
case.
Signed-off-by: Herton Ronaldo Krzesinski <herton.krzesinski@canonical.com>
Cc: stable@vger.kernel.org
Acked-by: Nicolas Pitre <nico@linaro.org>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2012-05-17 03:21:52 +08:00
|
|
|
#include <linux/math64.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/nand.h>
|
2011-03-11 18:05:34 +08:00
|
|
|
#include <linux/mtd/nand_bch.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/mtd/partitions.h>
|
|
|
|
#include <linux/delay.h>
|
2007-03-19 18:46:43 +08:00
|
|
|
#include <linux/list.h>
|
2007-03-19 18:47:45 +08:00
|
|
|
#include <linux/random.h>
|
2008-12-18 04:46:17 +08:00
|
|
|
#include <linux/sched.h>
|
2008-11-12 22:06:07 +08:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/pagemap.h>
|
2012-12-03 21:31:40 +08:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/debugfs.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Default simulator parameters values */
|
|
|
|
#if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
|
|
|
|
!defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \
|
|
|
|
!defined(CONFIG_NANDSIM_THIRD_ID_BYTE) || \
|
|
|
|
!defined(CONFIG_NANDSIM_FOURTH_ID_BYTE)
|
|
|
|
#define CONFIG_NANDSIM_FIRST_ID_BYTE 0x98
|
|
|
|
#define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39
|
|
|
|
#define CONFIG_NANDSIM_THIRD_ID_BYTE 0xFF /* No byte */
|
|
|
|
#define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF /* No byte */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NANDSIM_ACCESS_DELAY
|
|
|
|
#define CONFIG_NANDSIM_ACCESS_DELAY 25
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NANDSIM_PROGRAMM_DELAY
|
|
|
|
#define CONFIG_NANDSIM_PROGRAMM_DELAY 200
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NANDSIM_ERASE_DELAY
|
|
|
|
#define CONFIG_NANDSIM_ERASE_DELAY 2
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NANDSIM_OUTPUT_CYCLE
|
|
|
|
#define CONFIG_NANDSIM_OUTPUT_CYCLE 40
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NANDSIM_INPUT_CYCLE
|
|
|
|
#define CONFIG_NANDSIM_INPUT_CYCLE 50
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NANDSIM_BUS_WIDTH
|
|
|
|
#define CONFIG_NANDSIM_BUS_WIDTH 8
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NANDSIM_DO_DELAYS
|
|
|
|
#define CONFIG_NANDSIM_DO_DELAYS 0
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NANDSIM_LOG
|
|
|
|
#define CONFIG_NANDSIM_LOG 0
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_NANDSIM_DBG
|
|
|
|
#define CONFIG_NANDSIM_DBG 0
|
|
|
|
#endif
|
2010-01-30 04:58:08 +08:00
|
|
|
#ifndef CONFIG_NANDSIM_MAX_PARTS
|
|
|
|
#define CONFIG_NANDSIM_MAX_PARTS 32
|
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static uint first_id_byte = CONFIG_NANDSIM_FIRST_ID_BYTE;
|
|
|
|
static uint second_id_byte = CONFIG_NANDSIM_SECOND_ID_BYTE;
|
|
|
|
static uint third_id_byte = CONFIG_NANDSIM_THIRD_ID_BYTE;
|
|
|
|
static uint fourth_id_byte = CONFIG_NANDSIM_FOURTH_ID_BYTE;
|
|
|
|
static uint access_delay = CONFIG_NANDSIM_ACCESS_DELAY;
|
|
|
|
static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY;
|
|
|
|
static uint erase_delay = CONFIG_NANDSIM_ERASE_DELAY;
|
|
|
|
static uint output_cycle = CONFIG_NANDSIM_OUTPUT_CYCLE;
|
|
|
|
static uint input_cycle = CONFIG_NANDSIM_INPUT_CYCLE;
|
|
|
|
static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
|
|
|
|
static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
|
|
|
|
static uint log = CONFIG_NANDSIM_LOG;
|
|
|
|
static uint dbg = CONFIG_NANDSIM_DBG;
|
2010-01-30 04:58:08 +08:00
|
|
|
static unsigned long parts[CONFIG_NANDSIM_MAX_PARTS];
|
2007-03-19 18:46:43 +08:00
|
|
|
static unsigned int parts_num;
|
2007-03-19 18:47:45 +08:00
|
|
|
static char *badblocks = NULL;
|
|
|
|
static char *weakblocks = NULL;
|
|
|
|
static char *weakpages = NULL;
|
|
|
|
static unsigned int bitflips = 0;
|
|
|
|
static char *gravepages = NULL;
|
2007-03-19 18:49:11 +08:00
|
|
|
static unsigned int overridesize = 0;
|
2008-11-12 22:06:07 +08:00
|
|
|
static char *cache_file = NULL;
|
2010-09-30 01:43:54 +08:00
|
|
|
static unsigned int bbt;
|
2011-03-11 18:05:34 +08:00
|
|
|
static unsigned int bch;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
module_param(first_id_byte, uint, 0400);
|
|
|
|
module_param(second_id_byte, uint, 0400);
|
|
|
|
module_param(third_id_byte, uint, 0400);
|
|
|
|
module_param(fourth_id_byte, uint, 0400);
|
|
|
|
module_param(access_delay, uint, 0400);
|
|
|
|
module_param(programm_delay, uint, 0400);
|
|
|
|
module_param(erase_delay, uint, 0400);
|
|
|
|
module_param(output_cycle, uint, 0400);
|
|
|
|
module_param(input_cycle, uint, 0400);
|
|
|
|
module_param(bus_width, uint, 0400);
|
|
|
|
module_param(do_delays, uint, 0400);
|
|
|
|
module_param(log, uint, 0400);
|
|
|
|
module_param(dbg, uint, 0400);
|
2007-03-19 18:46:43 +08:00
|
|
|
module_param_array(parts, ulong, &parts_num, 0400);
|
2007-03-19 18:47:45 +08:00
|
|
|
module_param(badblocks, charp, 0400);
|
|
|
|
module_param(weakblocks, charp, 0400);
|
|
|
|
module_param(weakpages, charp, 0400);
|
|
|
|
module_param(bitflips, uint, 0400);
|
|
|
|
module_param(gravepages, charp, 0400);
|
2007-03-19 18:49:11 +08:00
|
|
|
module_param(overridesize, uint, 0400);
|
2008-11-12 22:06:07 +08:00
|
|
|
module_param(cache_file, charp, 0400);
|
2010-09-30 01:43:54 +08:00
|
|
|
module_param(bbt, uint, 0400);
|
2011-03-11 18:05:34 +08:00
|
|
|
module_param(bch, uint, 0400);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-19 18:49:11 +08:00
|
|
|
MODULE_PARM_DESC(first_id_byte, "The first byte returned by NAND Flash 'read ID' command (manufacturer ID)");
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID)");
|
|
|
|
MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command");
|
|
|
|
MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command");
|
2008-11-12 22:06:07 +08:00
|
|
|
MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)");
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
|
|
|
|
MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
|
2009-12-18 04:31:20 +08:00
|
|
|
MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanoseconds)");
|
|
|
|
MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanoseconds)");
|
2005-04-17 06:20:36 +08:00
|
|
|
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");
|
2007-03-19 18:46:43 +08:00
|
|
|
MODULE_PARM_DESC(parts, "Partition sizes (in erase blocks) separated by commas");
|
2007-03-19 18:47:45 +08:00
|
|
|
/* 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");
|
2007-03-19 18:49:11 +08:00
|
|
|
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");
|
2008-11-12 22:06:07 +08:00
|
|
|
MODULE_PARM_DESC(cache_file, "File to use to cache nand pages instead of memory");
|
2010-09-30 01:43:54 +08:00
|
|
|
MODULE_PARM_DESC(bbt, "0 OOB, 1 BBT with marker in OOB, 2 BBT with marker in data area");
|
2011-03-11 18:05:34 +08:00
|
|
|
MODULE_PARM_DESC(bch, "Enable BCH ecc and set how many bits should "
|
|
|
|
"be correctable in 512-byte blocks");
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* The largest possible page size */
|
2009-11-30 02:07:57 +08:00
|
|
|
#define NS_LARGEST_PAGE_SIZE 4096
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* The prefix for simulator output */
|
|
|
|
#define NS_OUTPUT_PREFIX "[nandsim]"
|
|
|
|
|
|
|
|
/* Simulator's output macros (logging, debugging, warning, error) */
|
|
|
|
#define NS_LOG(args...) \
|
|
|
|
do { if (log) printk(KERN_DEBUG NS_OUTPUT_PREFIX " log: " args); } while(0)
|
|
|
|
#define NS_DBG(args...) \
|
|
|
|
do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0)
|
|
|
|
#define NS_WARN(args...) \
|
2007-03-19 18:46:43 +08:00
|
|
|
do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warning: " args); } while(0)
|
2005-04-17 06:20:36 +08:00
|
|
|
#define NS_ERR(args...) \
|
2007-03-19 18:46:43 +08:00
|
|
|
do { printk(KERN_ERR NS_OUTPUT_PREFIX " error: " args); } while(0)
|
2007-03-19 18:40:41 +08:00
|
|
|
#define NS_INFO(args...) \
|
|
|
|
do { printk(KERN_INFO NS_OUTPUT_PREFIX " " args); } while(0)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* 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)
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 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)))
|
|
|
|
|
|
|
|
/* Operation failed completion status */
|
2005-11-07 19:15:49 +08:00
|
|
|
#define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Calculate the page offset in flash RAM image by (row, column) address */
|
|
|
|
#define NS_RAW_OFFSET(ns) \
|
|
|
|
(((ns)->regs.row << (ns)->geom.pgshift) + ((ns)->regs.row * (ns)->geom.oobsz) + (ns)->regs.column)
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 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)
|
|
|
|
|
|
|
|
/* 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 */
|
2006-12-07 03:52:32 +08:00
|
|
|
#define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */
|
2010-11-13 18:46:05 +08:00
|
|
|
#define STATE_CMD_PAGEPROG 0x00000004 /* start page program */
|
2005-04-17 06:20:36 +08:00
|
|
|
#define STATE_CMD_READOOB 0x00000005 /* read OOB area */
|
|
|
|
#define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */
|
|
|
|
#define STATE_CMD_STATUS 0x00000007 /* read status */
|
2010-11-13 18:46:05 +08:00
|
|
|
#define STATE_CMD_SEQIN 0x00000009 /* sequential data input */
|
2005-04-17 06:20:36 +08:00
|
|
|
#define STATE_CMD_READID 0x0000000A /* read ID */
|
|
|
|
#define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */
|
|
|
|
#define STATE_CMD_RESET 0x0000000C /* reset */
|
2008-07-30 16:18:42 +08:00
|
|
|
#define STATE_CMD_RNDOUT 0x0000000D /* random output command */
|
|
|
|
#define STATE_CMD_RNDOUTSTART 0x0000000E /* random output start command */
|
2005-04-17 06:20:36 +08:00
|
|
|
#define STATE_CMD_MASK 0x0000000F /* command states mask */
|
|
|
|
|
2008-02-03 23:22:34 +08:00
|
|
|
/* After an address is input, the simulator goes to one of these states */
|
2005-04-17 06:20:36 +08:00
|
|
|
#define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */
|
|
|
|
#define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */
|
2008-07-30 16:18:42 +08:00
|
|
|
#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 */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-11-13 18:46:05 +08:00
|
|
|
/* During data input/output the simulator is in these states */
|
2005-04-17 06:20:36 +08:00
|
|
|
#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_STATUS_M 0x00004000 /* waiting for multi-plane 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 */
|
2010-11-13 18:46:05 +08:00
|
|
|
#define ACTION_PRGPAGE 0x00200000 /* program the internal buffer to flash */
|
2005-04-17 06:20:36 +08:00
|
|
|
#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 */
|
|
|
|
|
2008-07-30 16:18:42 +08:00
|
|
|
#define NS_OPER_NUM 13 /* Number of operations supported by the simulator */
|
2005-04-17 06:20:36 +08:00
|
|
|
#define NS_OPER_STATES 6 /* Maximum number of states in operation */
|
|
|
|
|
|
|
|
#define OPT_ANY 0xFFFFFFFF /* any chip supports this operation */
|
|
|
|
#define OPT_PAGE512 0x00000002 /* 512-byte page chips */
|
|
|
|
#define OPT_PAGE2048 0x00000008 /* 2048-byte page chips */
|
|
|
|
#define OPT_SMARTMEDIA 0x00000010 /* SmartMedia technology chips */
|
|
|
|
#define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */
|
2009-11-30 02:07:57 +08:00
|
|
|
#define OPT_PAGE4096 0x00000080 /* 4096-byte page chips */
|
|
|
|
#define OPT_LARGEPAGE (OPT_PAGE2048 | OPT_PAGE4096) /* 2048 & 4096-byte page chips */
|
2013-03-05 21:00:51 +08:00
|
|
|
#define OPT_SMALLPAGE (OPT_PAGE512) /* 512-byte page chips */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-11-13 18:46:05 +08:00
|
|
|
/* Remove action bits from state */
|
2005-04-17 06:20:36 +08:00
|
|
|
#define NS_STATE(x) ((x) & ~ACTION_MASK)
|
2005-11-07 19:15:49 +08:00
|
|
|
|
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Maximum previous states which need to be saved. Currently saving is
|
2010-11-13 18:46:05 +08:00
|
|
|
* only needed for page program operation with preceded read command
|
2005-04-17 06:20:36 +08:00
|
|
|
* (which is only valid for 512-byte pages).
|
|
|
|
*/
|
|
|
|
#define NS_MAX_PREVSTATES 1
|
|
|
|
|
2008-11-12 22:06:07 +08:00
|
|
|
/* Maximum page cache pages needed to read or write a NAND page to the cache_file */
|
|
|
|
#define NS_MAX_HELD_PAGES 16
|
|
|
|
|
2012-12-03 21:31:40 +08:00
|
|
|
struct nandsim_debug_info {
|
|
|
|
struct dentry *dfs_root;
|
|
|
|
struct dentry *dfs_wear_report;
|
|
|
|
};
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
/*
|
|
|
|
* A union to represent flash memory contents and flash buffer.
|
|
|
|
*/
|
|
|
|
union ns_mem {
|
|
|
|
u_char *byte; /* for byte access */
|
|
|
|
uint16_t *word; /* for 16-bit word access */
|
|
|
|
};
|
|
|
|
|
2005-11-07 19:15:49 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* The structure which describes all the internal simulator data.
|
|
|
|
*/
|
|
|
|
struct nandsim {
|
2010-01-30 04:58:08 +08:00
|
|
|
struct mtd_partition partitions[CONFIG_NANDSIM_MAX_PARTS];
|
2007-03-19 18:46:43 +08:00
|
|
|
unsigned int nbparts;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
uint busw; /* flash chip bus width (8 or 16) */
|
|
|
|
u_char ids[4]; /* chip's ID bytes */
|
|
|
|
uint32_t options; /* chip's characteristic bits */
|
|
|
|
uint32_t state; /* current chip state */
|
|
|
|
uint32_t nxstate; /* next expected state */
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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 */
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
/* The simulated NAND flash pages array */
|
|
|
|
union ns_mem *pages;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-11-13 21:40:38 +08:00
|
|
|
/* Slab allocator for nand pages */
|
|
|
|
struct kmem_cache *nand_pages_slab;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Internal buffer of page + OOB size bytes */
|
2006-10-09 00:32:31 +08:00
|
|
|
union ns_mem buf;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* NAND flash "geometry" */
|
2010-04-01 20:22:50 +08:00
|
|
|
struct {
|
2008-05-30 20:56:26 +08:00
|
|
|
uint64_t totsz; /* total flash size, bytes */
|
2005-04-17 06:20:36 +08:00
|
|
|
uint32_t secsz; /* flash sector (erase block) size, bytes */
|
|
|
|
uint pgsz; /* NAND flash page size, bytes */
|
|
|
|
uint oobsz; /* page OOB area size, bytes */
|
2008-05-30 20:56:26 +08:00
|
|
|
uint64_t totszoob; /* total flash size including OOB, bytes */
|
2005-04-17 06:20:36 +08:00
|
|
|
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 oobshift; /* bits number in OOB 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 */
|
2010-04-01 20:22:50 +08:00
|
|
|
struct {
|
2005-04-17 06:20:36 +08:00
|
|
|
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 */
|
2010-04-01 20:22:50 +08:00
|
|
|
struct {
|
2005-04-17 06:20:36 +08:00
|
|
|
int ce; /* chip Enable */
|
|
|
|
int cle; /* command Latch Enable */
|
|
|
|
int ale; /* address Latch Enable */
|
|
|
|
int wp; /* write Protect */
|
|
|
|
} lines;
|
2008-11-12 22:06:07 +08:00
|
|
|
|
|
|
|
/* Fields needed when using a cache file */
|
|
|
|
struct file *cfile; /* Open file */
|
2013-07-28 10:21:53 +08:00
|
|
|
unsigned long *pages_written; /* Which pages have been written */
|
2008-11-12 22:06:07 +08:00
|
|
|
void *file_buf;
|
|
|
|
struct page *held_pages[NS_MAX_HELD_PAGES];
|
|
|
|
int held_cnt;
|
2012-12-03 21:31:40 +08:00
|
|
|
|
|
|
|
struct nandsim_debug_info dbg;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Operations array. To perform any operation the simulator must pass
|
|
|
|
* through the correspondent states chain.
|
|
|
|
*/
|
|
|
|
static struct 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}},
|
2010-11-13 18:46:05 +08:00
|
|
|
/* Program page starting from the beginning */
|
2005-04-17 06:20:36 +08:00
|
|
|
{OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
|
|
|
|
STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
|
2010-11-13 18:46:05 +08:00
|
|
|
/* Program page starting from the beginning */
|
2005-04-17 06:20:36 +08:00
|
|
|
{OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
|
|
|
|
STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
|
2010-11-13 18:46:05 +08:00
|
|
|
/* Program page starting from the second half */
|
2005-04-17 06:20:36 +08:00
|
|
|
{OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
|
|
|
|
STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
|
2010-11-13 18:46:05 +08:00
|
|
|
/* Program OOB */
|
2005-04-17 06:20:36 +08:00
|
|
|
{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,
|
2008-07-30 16:18:42 +08:00
|
|
|
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}},
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2007-03-19 18:47:45 +08:00
|
|
|
struct weak_block {
|
|
|
|
struct list_head list;
|
|
|
|
unsigned int erase_block_no;
|
|
|
|
unsigned int max_erases;
|
|
|
|
unsigned int erases_done;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(weak_blocks);
|
|
|
|
|
|
|
|
struct weak_page {
|
|
|
|
struct list_head list;
|
|
|
|
unsigned int page_no;
|
|
|
|
unsigned int max_writes;
|
|
|
|
unsigned int writes_done;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(weak_pages);
|
|
|
|
|
|
|
|
struct grave_page {
|
|
|
|
struct list_head list;
|
|
|
|
unsigned int page_no;
|
|
|
|
unsigned int max_reads;
|
|
|
|
unsigned int reads_done;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(grave_pages);
|
|
|
|
|
2007-03-19 18:40:41 +08:00
|
|
|
static unsigned long *erase_block_wear = NULL;
|
|
|
|
static unsigned int wear_eb_count = 0;
|
|
|
|
static unsigned long total_wear = 0;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* MTD structure for NAND controller */
|
|
|
|
static struct mtd_info *nsmtd;
|
|
|
|
|
2012-12-03 21:31:40 +08:00
|
|
|
static int nandsim_debugfs_show(struct seq_file *m, void *private)
|
|
|
|
{
|
|
|
|
unsigned long wmin = -1, wmax = 0, avg;
|
|
|
|
unsigned long deciles[10], decile_max[10], tot = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Calc wear stats */
|
|
|
|
for (i = 0; i < wear_eb_count; ++i) {
|
|
|
|
unsigned long 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;
|
|
|
|
unsigned long 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) {
|
|
|
|
unsigned long 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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nandsim_debugfs_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return single_open(file, nandsim_debugfs_show, inode->i_private);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations dfs_fops = {
|
|
|
|
.open = nandsim_debugfs_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = single_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nandsim_debugfs_create - initialize debugfs
|
|
|
|
* @dev: nandsim device description object
|
|
|
|
*
|
|
|
|
* This function creates all debugfs files for UBI device @ubi. Returns zero in
|
|
|
|
* case of success and a negative error code in case of failure.
|
|
|
|
*/
|
|
|
|
static int nandsim_debugfs_create(struct nandsim *dev)
|
|
|
|
{
|
|
|
|
struct nandsim_debug_info *dbg = &dev->dbg;
|
|
|
|
struct dentry *dent;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_DEBUG_FS))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dent = debugfs_create_dir("nandsim", NULL);
|
|
|
|
if (IS_ERR_OR_NULL(dent)) {
|
|
|
|
int err = dent ? -ENODEV : PTR_ERR(dent);
|
|
|
|
|
|
|
|
NS_ERR("cannot create \"nandsim\" debugfs directory, err %d\n",
|
|
|
|
err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
dbg->dfs_root = dent;
|
|
|
|
|
|
|
|
dent = debugfs_create_file("wear_report", S_IRUSR,
|
|
|
|
dbg->dfs_root, dev, &dfs_fops);
|
|
|
|
if (IS_ERR_OR_NULL(dent))
|
|
|
|
goto out_remove;
|
|
|
|
dbg->dfs_wear_report = dent;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_remove:
|
|
|
|
debugfs_remove_recursive(dbg->dfs_root);
|
|
|
|
err = dent ? PTR_ERR(dent) : -ENODEV;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nandsim_debugfs_remove - destroy all debugfs files
|
|
|
|
*/
|
|
|
|
static void nandsim_debugfs_remove(struct nandsim *ns)
|
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_DEBUG_FS))
|
|
|
|
debugfs_remove_recursive(ns->dbg.dfs_root);
|
|
|
|
}
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
/*
|
2008-11-13 21:40:38 +08:00
|
|
|
* Allocate array of page pointers, create slab allocation for an array
|
|
|
|
* and initialize the array by NULL pointers.
|
2006-10-09 00:32:31 +08:00
|
|
|
*
|
|
|
|
* RETURNS: 0 if success, -ENOMEM if memory alloc fails.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static int alloc_device(struct nandsim *ns)
|
2006-10-09 00:32:31 +08:00
|
|
|
{
|
2008-11-12 22:06:07 +08:00
|
|
|
struct file *cfile;
|
|
|
|
int i, err;
|
|
|
|
|
|
|
|
if (cache_file) {
|
|
|
|
cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
|
|
|
|
if (IS_ERR(cfile))
|
|
|
|
return PTR_ERR(cfile);
|
|
|
|
if (!cfile->f_op || (!cfile->f_op->read && !cfile->f_op->aio_read)) {
|
|
|
|
NS_ERR("alloc_device: cache file not readable\n");
|
|
|
|
err = -EINVAL;
|
|
|
|
goto err_close;
|
|
|
|
}
|
|
|
|
if (!cfile->f_op->write && !cfile->f_op->aio_write) {
|
|
|
|
NS_ERR("alloc_device: cache file not writeable\n");
|
|
|
|
err = -EINVAL;
|
|
|
|
goto err_close;
|
|
|
|
}
|
2013-07-28 10:21:53 +08:00
|
|
|
ns->pages_written = vzalloc(BITS_TO_LONGS(ns->geom.pgnum) *
|
|
|
|
sizeof(unsigned long));
|
2008-11-12 22:06:07 +08:00
|
|
|
if (!ns->pages_written) {
|
|
|
|
NS_ERR("alloc_device: unable to allocate pages written array\n");
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_close;
|
|
|
|
}
|
|
|
|
ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
|
|
|
|
if (!ns->file_buf) {
|
|
|
|
NS_ERR("alloc_device: unable to allocate file buf\n");
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
ns->cfile = cfile;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-09 00:32:31 +08:00
|
|
|
|
|
|
|
ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
|
|
|
|
if (!ns->pages) {
|
2008-11-12 22:06:07 +08:00
|
|
|
NS_ERR("alloc_device: unable to allocate page array\n");
|
2006-10-09 00:32:31 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ns->geom.pgnum; i++) {
|
|
|
|
ns->pages[i].byte = NULL;
|
|
|
|
}
|
2008-11-13 21:40:38 +08:00
|
|
|
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");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2006-10-09 00:32:31 +08:00
|
|
|
|
|
|
|
return 0;
|
2008-11-12 22:06:07 +08:00
|
|
|
|
|
|
|
err_free:
|
|
|
|
vfree(ns->pages_written);
|
|
|
|
err_close:
|
|
|
|
filp_close(cfile, NULL);
|
|
|
|
return err;
|
2006-10-09 00:32:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free any allocated pages, and free the array of page pointers.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static void free_device(struct nandsim *ns)
|
2006-10-09 00:32:31 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-11-12 22:06:07 +08:00
|
|
|
if (ns->cfile) {
|
|
|
|
kfree(ns->file_buf);
|
|
|
|
vfree(ns->pages_written);
|
|
|
|
filp_close(ns->cfile, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
if (ns->pages) {
|
|
|
|
for (i = 0; i < ns->geom.pgnum; i++) {
|
|
|
|
if (ns->pages[i].byte)
|
2008-11-13 21:40:38 +08:00
|
|
|
kmem_cache_free(ns->nand_pages_slab,
|
|
|
|
ns->pages[i].byte);
|
2006-10-09 00:32:31 +08:00
|
|
|
}
|
2008-11-13 21:40:38 +08:00
|
|
|
kmem_cache_destroy(ns->nand_pages_slab);
|
2006-10-09 00:32:31 +08:00
|
|
|
vfree(ns->pages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-19 18:46:43 +08:00
|
|
|
static char *get_partition_name(int i)
|
|
|
|
{
|
2013-07-28 10:21:54 +08:00
|
|
|
return kasprintf(GFP_KERNEL, "NAND simulator partition %d", i);
|
2007-03-19 18:46:43 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Initialize the nandsim structure.
|
|
|
|
*
|
|
|
|
* RETURNS: 0 if success, -ERRNO if failure.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static int init_nandsim(struct mtd_info *mtd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-06-29 18:15:17 +08:00
|
|
|
struct nand_chip *chip = mtd->priv;
|
|
|
|
struct nandsim *ns = chip->priv;
|
2007-03-19 18:46:43 +08:00
|
|
|
int i, ret = 0;
|
2008-12-10 22:01:46 +08:00
|
|
|
uint64_t remains;
|
|
|
|
uint64_t next_offset;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (NS_IS_INITIALIZED(ns)) {
|
|
|
|
NS_ERR("init_nandsim: nandsim is already initialized\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Force mtd to not do delays */
|
|
|
|
chip->chip_delay = 0;
|
|
|
|
|
|
|
|
/* Initialize the NAND flash parameters */
|
|
|
|
ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
|
|
|
|
ns->geom.totsz = mtd->size;
|
2006-05-23 05:18:05 +08:00
|
|
|
ns->geom.pgsz = mtd->writesize;
|
2005-04-17 06:20:36 +08:00
|
|
|
ns->geom.oobsz = mtd->oobsize;
|
|
|
|
ns->geom.secsz = mtd->erasesize;
|
|
|
|
ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
|
mtd: nandsim: don't open code a do_div helper
We don't need to open code the divide function, just use div_u64 that
already exists and do the same job. While this is a straightforward
clean up, there is more to that, the real motivation for this.
While building on a cross compiling environment in armel, using gcc
4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5), I was getting the following build
error:
ERROR: "__aeabi_uldivmod" [drivers/mtd/nand/nandsim.ko] undefined!
After investigating with objdump and hand built assembly version
generated with the compiler, I narrowed __aeabi_uldivmod as being
generated from the divide function. When nandsim.c is built with
-fno-inline-functions-called-once, that happens when
CONFIG_DEBUG_SECTION_MISMATCH is enabled, the do_div optimization in
arch/arm/include/asm/div64.h doesn't work as expected with the open
coded divide function: even if the do_div we are using doesn't have a
constant divisor, the compiler still includes the else parts of the
optimized do_div macro, and translates the divisions there to use
__aeabi_uldivmod, instead of only calling __do_div_asm -> __do_div64 and
optimizing/removing everything else out.
So to reproduce, gcc 4.6 plus CONFIG_DEBUG_SECTION_MISMATCH=y and
CONFIG_MTD_NAND_NANDSIM=m should do it, building on armel.
After this change, the compiler does the intended thing even with
-fno-inline-functions-called-once, and optimizes out as expected the
constant handling in the optimized do_div on arm. As this also avoids a
build issue, I'm marking for Stable, as I think is applicable for this
case.
Signed-off-by: Herton Ronaldo Krzesinski <herton.krzesinski@canonical.com>
Cc: stable@vger.kernel.org
Acked-by: Nicolas Pitre <nico@linaro.org>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2012-05-17 03:21:52 +08:00
|
|
|
ns->geom.pgnum = div_u64(ns->geom.totsz, ns->geom.pgsz);
|
2008-05-30 20:56:26 +08:00
|
|
|
ns->geom.totszoob = ns->geom.totsz + (uint64_t)ns->geom.pgnum * ns->geom.oobsz;
|
2005-04-17 06:20:36 +08:00
|
|
|
ns->geom.secshift = ffs(ns->geom.secsz) - 1;
|
|
|
|
ns->geom.pgshift = chip->page_shift;
|
|
|
|
ns->geom.oobshift = ffs(ns->geom.oobsz) - 1;
|
|
|
|
ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
|
|
|
|
ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
|
|
|
|
ns->options = 0;
|
|
|
|
|
2013-03-05 21:00:51 +08:00
|
|
|
if (ns->geom.pgsz == 512) {
|
2012-05-02 08:12:53 +08:00
|
|
|
ns->options |= OPT_PAGE512;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->busw == 8)
|
|
|
|
ns->options |= OPT_PAGE512_8BIT;
|
|
|
|
} else if (ns->geom.pgsz == 2048) {
|
|
|
|
ns->options |= OPT_PAGE2048;
|
2009-11-30 02:07:57 +08:00
|
|
|
} else if (ns->geom.pgsz == 4096) {
|
|
|
|
ns->options |= OPT_PAGE4096;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
|
|
|
NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ns->options & OPT_SMALLPAGE) {
|
2008-05-30 20:56:18 +08:00
|
|
|
if (ns->geom.totsz <= (32 << 20)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
ns->geom.pgaddrbytes = 3;
|
|
|
|
ns->geom.secaddrbytes = 2;
|
|
|
|
} else {
|
|
|
|
ns->geom.pgaddrbytes = 4;
|
|
|
|
ns->geom.secaddrbytes = 3;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ns->geom.totsz <= (128 << 20)) {
|
2006-12-07 03:52:32 +08:00
|
|
|
ns->geom.pgaddrbytes = 4;
|
2005-04-17 06:20:36 +08:00
|
|
|
ns->geom.secaddrbytes = 2;
|
|
|
|
} else {
|
|
|
|
ns->geom.pgaddrbytes = 5;
|
|
|
|
ns->geom.secaddrbytes = 3;
|
|
|
|
}
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2007-03-19 18:46:43 +08:00
|
|
|
/* Fill the partition_info structure */
|
|
|
|
if (parts_num > ARRAY_SIZE(ns->partitions)) {
|
|
|
|
NS_ERR("too many partitions.\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
remains = ns->geom.totsz;
|
|
|
|
next_offset = 0;
|
|
|
|
for (i = 0; i < parts_num; ++i) {
|
2008-12-10 22:01:46 +08:00
|
|
|
uint64_t part_sz = (uint64_t)parts[i] * ns->geom.secsz;
|
2008-05-30 20:56:26 +08:00
|
|
|
|
|
|
|
if (!part_sz || part_sz > remains) {
|
2007-03-19 18:46:43 +08:00
|
|
|
NS_ERR("bad partition size.\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
ns->partitions[i].name = get_partition_name(i);
|
|
|
|
ns->partitions[i].offset = next_offset;
|
2008-05-30 20:56:26 +08:00
|
|
|
ns->partitions[i].size = part_sz;
|
2007-03-19 18:46:43 +08:00
|
|
|
next_offset += ns->partitions[i].size;
|
|
|
|
remains -= ns->partitions[i].size;
|
|
|
|
}
|
|
|
|
ns->nbparts = parts_num;
|
|
|
|
if (remains) {
|
|
|
|
if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
|
|
|
|
NS_ERR("too many partitions.\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
ns->partitions[i].name = get_partition_name(i);
|
|
|
|
ns->partitions[i].offset = next_offset;
|
|
|
|
ns->partitions[i].size = remains;
|
|
|
|
ns->nbparts += 1;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Detect how many ID bytes the NAND chip outputs */
|
2013-03-04 22:05:00 +08:00
|
|
|
for (i = 0; nand_flash_ids[i].name != NULL; i++) {
|
|
|
|
if (second_id_byte != nand_flash_ids[i].dev_id)
|
|
|
|
continue;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ns->busw == 16)
|
|
|
|
NS_WARN("16-bit flashes support wasn't tested\n");
|
|
|
|
|
2008-07-31 03:35:04 +08:00
|
|
|
printk("flash size: %llu MiB\n",
|
|
|
|
(unsigned long long)ns->geom.totsz >> 20);
|
2005-04-17 06:20:36 +08:00
|
|
|
printk("page size: %u bytes\n", ns->geom.pgsz);
|
|
|
|
printk("OOB area size: %u bytes\n", ns->geom.oobsz);
|
|
|
|
printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
|
|
|
|
printk("pages number: %u\n", ns->geom.pgnum);
|
|
|
|
printk("pages per sector: %u\n", ns->geom.pgsec);
|
|
|
|
printk("bus width: %u\n", ns->busw);
|
|
|
|
printk("bits in sector size: %u\n", ns->geom.secshift);
|
|
|
|
printk("bits in page size: %u\n", ns->geom.pgshift);
|
2008-07-31 03:35:04 +08:00
|
|
|
printk("bits in OOB size: %u\n", ns->geom.oobshift);
|
|
|
|
printk("flash size with OOB: %llu KiB\n",
|
|
|
|
(unsigned long long)ns->geom.totszoob >> 10);
|
2005-04-17 06:20:36 +08:00
|
|
|
printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
|
|
|
|
printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
|
|
|
|
printk("options: %#x\n", ns->options);
|
|
|
|
|
2007-03-19 18:46:43 +08:00
|
|
|
if ((ret = alloc_device(ns)) != 0)
|
2006-10-09 00:32:31 +08:00
|
|
|
goto error;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Allocate / initialize the internal buffer */
|
|
|
|
ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
|
|
|
|
if (!ns->buf.byte) {
|
|
|
|
NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
|
|
|
|
ns->geom.pgszoob);
|
2007-03-19 18:46:43 +08:00
|
|
|
ret = -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2006-10-09 00:32:31 +08:00
|
|
|
free_device(ns);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-19 18:46:43 +08:00
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the nandsim structure.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static void free_nandsim(struct nandsim *ns)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
kfree(ns->buf.byte);
|
2006-10-09 00:32:31 +08:00
|
|
|
free_device(ns);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-19 18:47:45 +08:00
|
|
|
static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
char *w;
|
|
|
|
int zero_ok;
|
|
|
|
unsigned int erase_block_no;
|
|
|
|
loff_t offset;
|
|
|
|
|
|
|
|
if (!badblocks)
|
|
|
|
return 0;
|
|
|
|
w = badblocks;
|
|
|
|
do {
|
|
|
|
zero_ok = (*w == '0' ? 1 : 0);
|
|
|
|
erase_block_no = simple_strtoul(w, &w, 0);
|
|
|
|
if (!zero_ok && !erase_block_no) {
|
|
|
|
NS_ERR("invalid badblocks.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
offset = erase_block_no * ns->geom.secsz;
|
2011-12-24 01:37:38 +08:00
|
|
|
if (mtd_block_markbad(mtd, offset)) {
|
2007-03-19 18:47:45 +08:00
|
|
|
NS_ERR("invalid badblocks.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (*w == ',')
|
|
|
|
w += 1;
|
|
|
|
} while (*w);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_weakblocks(void)
|
|
|
|
{
|
|
|
|
char *w;
|
|
|
|
int zero_ok;
|
|
|
|
unsigned int erase_block_no;
|
|
|
|
unsigned int max_erases;
|
|
|
|
struct weak_block *wb;
|
|
|
|
|
|
|
|
if (!weakblocks)
|
|
|
|
return 0;
|
|
|
|
w = weakblocks;
|
|
|
|
do {
|
|
|
|
zero_ok = (*w == '0' ? 1 : 0);
|
|
|
|
erase_block_no = simple_strtoul(w, &w, 0);
|
|
|
|
if (!zero_ok && !erase_block_no) {
|
|
|
|
NS_ERR("invalid weakblocks.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
max_erases = 3;
|
|
|
|
if (*w == ':') {
|
|
|
|
w += 1;
|
|
|
|
max_erases = simple_strtoul(w, &w, 0);
|
|
|
|
}
|
|
|
|
if (*w == ',')
|
|
|
|
w += 1;
|
|
|
|
wb = kzalloc(sizeof(*wb), GFP_KERNEL);
|
|
|
|
if (!wb) {
|
|
|
|
NS_ERR("unable to allocate memory.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
wb->erase_block_no = erase_block_no;
|
|
|
|
wb->max_erases = max_erases;
|
|
|
|
list_add(&wb->list, &weak_blocks);
|
|
|
|
} while (*w);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int erase_error(unsigned int erase_block_no)
|
|
|
|
{
|
|
|
|
struct weak_block *wb;
|
|
|
|
|
|
|
|
list_for_each_entry(wb, &weak_blocks, list)
|
|
|
|
if (wb->erase_block_no == erase_block_no) {
|
|
|
|
if (wb->erases_done >= wb->max_erases)
|
|
|
|
return 1;
|
|
|
|
wb->erases_done += 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_weakpages(void)
|
|
|
|
{
|
|
|
|
char *w;
|
|
|
|
int zero_ok;
|
|
|
|
unsigned int page_no;
|
|
|
|
unsigned int max_writes;
|
|
|
|
struct weak_page *wp;
|
|
|
|
|
|
|
|
if (!weakpages)
|
|
|
|
return 0;
|
|
|
|
w = weakpages;
|
|
|
|
do {
|
|
|
|
zero_ok = (*w == '0' ? 1 : 0);
|
|
|
|
page_no = simple_strtoul(w, &w, 0);
|
|
|
|
if (!zero_ok && !page_no) {
|
|
|
|
NS_ERR("invalid weakpagess.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
max_writes = 3;
|
|
|
|
if (*w == ':') {
|
|
|
|
w += 1;
|
|
|
|
max_writes = simple_strtoul(w, &w, 0);
|
|
|
|
}
|
|
|
|
if (*w == ',')
|
|
|
|
w += 1;
|
|
|
|
wp = kzalloc(sizeof(*wp), GFP_KERNEL);
|
|
|
|
if (!wp) {
|
|
|
|
NS_ERR("unable to allocate memory.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
wp->page_no = page_no;
|
|
|
|
wp->max_writes = max_writes;
|
|
|
|
list_add(&wp->list, &weak_pages);
|
|
|
|
} while (*w);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_error(unsigned int page_no)
|
|
|
|
{
|
|
|
|
struct weak_page *wp;
|
|
|
|
|
|
|
|
list_for_each_entry(wp, &weak_pages, list)
|
|
|
|
if (wp->page_no == page_no) {
|
|
|
|
if (wp->writes_done >= wp->max_writes)
|
|
|
|
return 1;
|
|
|
|
wp->writes_done += 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_gravepages(void)
|
|
|
|
{
|
|
|
|
char *g;
|
|
|
|
int zero_ok;
|
|
|
|
unsigned int page_no;
|
|
|
|
unsigned int max_reads;
|
|
|
|
struct grave_page *gp;
|
|
|
|
|
|
|
|
if (!gravepages)
|
|
|
|
return 0;
|
|
|
|
g = gravepages;
|
|
|
|
do {
|
|
|
|
zero_ok = (*g == '0' ? 1 : 0);
|
|
|
|
page_no = simple_strtoul(g, &g, 0);
|
|
|
|
if (!zero_ok && !page_no) {
|
|
|
|
NS_ERR("invalid gravepagess.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
max_reads = 3;
|
|
|
|
if (*g == ':') {
|
|
|
|
g += 1;
|
|
|
|
max_reads = simple_strtoul(g, &g, 0);
|
|
|
|
}
|
|
|
|
if (*g == ',')
|
|
|
|
g += 1;
|
|
|
|
gp = kzalloc(sizeof(*gp), GFP_KERNEL);
|
|
|
|
if (!gp) {
|
|
|
|
NS_ERR("unable to allocate memory.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
gp->page_no = page_no;
|
|
|
|
gp->max_reads = max_reads;
|
|
|
|
list_add(&gp->list, &grave_pages);
|
|
|
|
} while (*g);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_error(unsigned int page_no)
|
|
|
|
{
|
|
|
|
struct grave_page *gp;
|
|
|
|
|
|
|
|
list_for_each_entry(gp, &grave_pages, list)
|
|
|
|
if (gp->page_no == page_no) {
|
|
|
|
if (gp->reads_done >= gp->max_reads)
|
|
|
|
return 1;
|
|
|
|
gp->reads_done += 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_lists(void)
|
|
|
|
{
|
|
|
|
struct list_head *pos, *n;
|
|
|
|
list_for_each_safe(pos, n, &weak_blocks) {
|
|
|
|
list_del(pos);
|
|
|
|
kfree(list_entry(pos, struct weak_block, list));
|
|
|
|
}
|
|
|
|
list_for_each_safe(pos, n, &weak_pages) {
|
|
|
|
list_del(pos);
|
|
|
|
kfree(list_entry(pos, struct weak_page, list));
|
|
|
|
}
|
|
|
|
list_for_each_safe(pos, n, &grave_pages) {
|
|
|
|
list_del(pos);
|
|
|
|
kfree(list_entry(pos, struct grave_page, list));
|
|
|
|
}
|
2007-03-19 18:40:41 +08:00
|
|
|
kfree(erase_block_wear);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int setup_wear_reporting(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
size_t mem;
|
|
|
|
|
mtd: nandsim: don't open code a do_div helper
We don't need to open code the divide function, just use div_u64 that
already exists and do the same job. While this is a straightforward
clean up, there is more to that, the real motivation for this.
While building on a cross compiling environment in armel, using gcc
4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5), I was getting the following build
error:
ERROR: "__aeabi_uldivmod" [drivers/mtd/nand/nandsim.ko] undefined!
After investigating with objdump and hand built assembly version
generated with the compiler, I narrowed __aeabi_uldivmod as being
generated from the divide function. When nandsim.c is built with
-fno-inline-functions-called-once, that happens when
CONFIG_DEBUG_SECTION_MISMATCH is enabled, the do_div optimization in
arch/arm/include/asm/div64.h doesn't work as expected with the open
coded divide function: even if the do_div we are using doesn't have a
constant divisor, the compiler still includes the else parts of the
optimized do_div macro, and translates the divisions there to use
__aeabi_uldivmod, instead of only calling __do_div_asm -> __do_div64 and
optimizing/removing everything else out.
So to reproduce, gcc 4.6 plus CONFIG_DEBUG_SECTION_MISMATCH=y and
CONFIG_MTD_NAND_NANDSIM=m should do it, building on armel.
After this change, the compiler does the intended thing even with
-fno-inline-functions-called-once, and optimizes out as expected the
constant handling in the optimized do_div on arm. As this also avoids a
build issue, I'm marking for Stable, as I think is applicable for this
case.
Signed-off-by: Herton Ronaldo Krzesinski <herton.krzesinski@canonical.com>
Cc: stable@vger.kernel.org
Acked-by: Nicolas Pitre <nico@linaro.org>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2012-05-17 03:21:52 +08:00
|
|
|
wear_eb_count = div_u64(mtd->size, mtd->erasesize);
|
2007-03-19 18:40:41 +08:00
|
|
|
mem = wear_eb_count * sizeof(unsigned long);
|
|
|
|
if (mem / sizeof(unsigned long) != wear_eb_count) {
|
|
|
|
NS_ERR("Too many erase blocks for wear reporting\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
erase_block_wear = kzalloc(mem, GFP_KERNEL);
|
|
|
|
if (!erase_block_wear) {
|
|
|
|
NS_ERR("Too many erase blocks for wear reporting\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_wear(unsigned int erase_block_no)
|
|
|
|
{
|
|
|
|
if (!erase_block_wear)
|
|
|
|
return;
|
|
|
|
total_wear += 1;
|
2012-12-03 21:31:40 +08:00
|
|
|
/*
|
|
|
|
* TODO: Notify this through a debugfs entry,
|
|
|
|
* instead of showing an error message.
|
|
|
|
*/
|
2007-03-19 18:40:41 +08:00
|
|
|
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);
|
2007-03-19 18:47:45 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Returns the string representation of 'state' state.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static char *get_state_name(uint32_t state)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
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";
|
2008-07-30 16:18:42 +08:00
|
|
|
case STATE_CMD_RNDOUT:
|
|
|
|
return "STATE_CMD_RNDOUT";
|
|
|
|
case STATE_CMD_RNDOUTSTART:
|
|
|
|
return "STATE_CMD_RNDOUTSTART";
|
2005-04-17 06:20:36 +08:00
|
|
|
case STATE_ADDR_PAGE:
|
|
|
|
return "STATE_ADDR_PAGE";
|
|
|
|
case STATE_ADDR_SEC:
|
|
|
|
return "STATE_ADDR_SEC";
|
|
|
|
case STATE_ADDR_ZERO:
|
|
|
|
return "STATE_ADDR_ZERO";
|
2008-07-30 16:18:42 +08:00
|
|
|
case STATE_ADDR_COLUMN:
|
|
|
|
return "STATE_ADDR_COLUMN";
|
2005-04-17 06:20:36 +08:00
|
|
|
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_DATAOUT_STATUS_M:
|
|
|
|
return "STATE_DATAOUT_STATUS_M";
|
|
|
|
case STATE_READY:
|
|
|
|
return "STATE_READY";
|
|
|
|
case STATE_UNKNOWN:
|
|
|
|
return "STATE_UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ERR("get_state_name: unknown state, BUG\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if command is valid.
|
|
|
|
*
|
|
|
|
* RETURNS: 1 if wrong command, 0 if right.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static int check_command(int cmd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
switch (cmd) {
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
case NAND_CMD_READ0:
|
2008-07-30 16:18:42 +08:00
|
|
|
case NAND_CMD_READ1:
|
2005-04-17 06:20:36 +08:00
|
|
|
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:
|
2008-07-30 16:18:42 +08:00
|
|
|
case NAND_CMD_RNDOUT:
|
|
|
|
case NAND_CMD_RNDOUTSTART:
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns state after command is accepted by command number.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static uint32_t get_state_by_command(unsigned command)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
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;
|
2008-07-30 16:18:42 +08:00
|
|
|
case NAND_CMD_RNDOUT:
|
|
|
|
return STATE_CMD_RNDOUT;
|
|
|
|
case NAND_CMD_RNDOUTSTART:
|
|
|
|
return STATE_CMD_RNDOUTSTART;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ERR("get_state_by_command: unknown command, BUG\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move an address byte to the correspondent internal register.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
uint byte = (uint)bt;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
|
|
|
|
ns->regs.column |= (byte << 8 * ns->regs.count);
|
|
|
|
else {
|
|
|
|
ns->regs.row |= (byte << 8 * (ns->regs.count -
|
|
|
|
ns->geom.pgaddrbytes +
|
|
|
|
ns->geom.secaddrbytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Switch to STATE_READY state.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
|
|
|
|
|
|
|
|
ns->state = STATE_READY;
|
|
|
|
ns->nxstate = STATE_UNKNOWN;
|
|
|
|
ns->op = NULL;
|
|
|
|
ns->npstates = 0;
|
|
|
|
ns->stateidx = 0;
|
|
|
|
ns->regs.num = 0;
|
|
|
|
ns->regs.count = 0;
|
|
|
|
ns->regs.off = 0;
|
|
|
|
ns->regs.row = 0;
|
|
|
|
ns->regs.column = 0;
|
|
|
|
ns->regs.status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the operation isn't known yet, try to find it in the global array
|
|
|
|
* of supported operations.
|
|
|
|
*
|
|
|
|
* Operation can be unknown because of the following.
|
2010-11-13 18:46:05 +08:00
|
|
|
* 1. New command was accepted and this is the first call to find the
|
2005-04-17 06:20:36 +08:00
|
|
|
* correspondent states chain. In this case ns->npstates = 0;
|
2010-11-13 18:46:05 +08:00
|
|
|
* 2. There are several operations which begin with the same command(s)
|
2005-04-17 06:20:36 +08:00
|
|
|
* (for example program from the second half and read from the
|
|
|
|
* second half operations both begin with the READ1 command). In this
|
|
|
|
* case the ns->pstates[] array contains previous states.
|
2005-11-07 19:15:49 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Thus, the function tries to find operation containing the following
|
|
|
|
* states (if the 'flag' parameter is 0):
|
|
|
|
* ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
|
|
|
|
*
|
|
|
|
* If (one and only one) matching operation is found, it is accepted (
|
|
|
|
* ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
|
|
|
|
* zeroed).
|
2005-11-07 19:15:49 +08:00
|
|
|
*
|
2010-11-13 18:46:05 +08:00
|
|
|
* If there are several matches, the current state is pushed to the
|
2005-04-17 06:20:36 +08:00
|
|
|
* ns->pstates.
|
|
|
|
*
|
|
|
|
* The operation can be unknown only while commands are input to the chip.
|
|
|
|
* As soon as address command is accepted, the operation must be known.
|
|
|
|
* In such situation the function is called with 'flag' != 0, and the
|
|
|
|
* operation is searched using the following pattern:
|
|
|
|
* ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
|
2005-11-07 19:15:49 +08:00
|
|
|
*
|
2010-11-13 18:46:05 +08:00
|
|
|
* It is supposed that this pattern must either match one operation or
|
2005-04-17 06:20:36 +08:00
|
|
|
* none. There can't be ambiguity in that case.
|
|
|
|
*
|
2010-11-13 18:46:05 +08:00
|
|
|
* If no matches found, the function does the following:
|
2005-04-17 06:20:36 +08:00
|
|
|
* 1. if there are saved states present, try to ignore them and search
|
|
|
|
* again only using the last command. If nothing was found, switch
|
|
|
|
* to the STATE_READY state.
|
|
|
|
* 2. if there are no saved states, switch to the STATE_READY state.
|
|
|
|
*
|
|
|
|
* RETURNS: -2 - no matched operations found.
|
|
|
|
* -1 - several matches.
|
|
|
|
* 0 - operation is found.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static int find_operation(struct nandsim *ns, uint32_t flag)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int opsfound = 0;
|
|
|
|
int i, j, idx = 0;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
for (i = 0; i < NS_OPER_NUM; i++) {
|
|
|
|
|
|
|
|
int found = 1;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!(ns->options & ops[i].reqopts))
|
|
|
|
/* Ignore operations we can't perform */
|
|
|
|
continue;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (flag) {
|
|
|
|
if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-11-07 19:15:49 +08:00
|
|
|
for (j = 0; j < ns->npstates; j++)
|
2005-04-17 06:20:36 +08:00
|
|
|
if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
|
|
|
|
&& (ns->options & ops[idx].reqopts)) {
|
|
|
|
found = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
idx = i;
|
|
|
|
opsfound += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opsfound == 1) {
|
|
|
|
/* Exact match */
|
|
|
|
ns->op = &ops[idx].states[0];
|
|
|
|
if (flag) {
|
2005-11-07 19:15:49 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* In this case the find_operation function was
|
|
|
|
* called when address has just began input. But it isn't
|
|
|
|
* yet fully input and the current state must
|
|
|
|
* not be one of STATE_ADDR_*, but the STATE_ADDR_*
|
|
|
|
* state must be the next state (ns->nxstate).
|
|
|
|
*/
|
|
|
|
ns->stateidx = ns->npstates - 1;
|
|
|
|
} else {
|
|
|
|
ns->stateidx = ns->npstates;
|
|
|
|
}
|
|
|
|
ns->npstates = 0;
|
|
|
|
ns->state = ns->op[ns->stateidx];
|
|
|
|
ns->nxstate = ns->op[ns->stateidx + 1];
|
|
|
|
NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
|
|
|
|
idx, get_state_name(ns->state), get_state_name(ns->nxstate));
|
|
|
|
return 0;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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",
|
|
|
|
get_state_name(ns->state));
|
|
|
|
ns->npstates = 0;
|
|
|
|
return find_operation(ns, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
NS_DBG("find_operation: no operations found\n");
|
|
|
|
switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
|
|
|
|
return -2;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (flag) {
|
|
|
|
/* This shouldn't happen */
|
|
|
|
NS_DBG("find_operation: BUG, operation must be known if address is input\n");
|
|
|
|
return -2;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
NS_DBG("find_operation: there is still ambiguity\n");
|
|
|
|
|
|
|
|
ns->pstates[ns->npstates++] = ns->state;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-11-12 22:06:07 +08:00
|
|
|
static void put_pages(struct nandsim *ns)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ns->held_cnt; i++)
|
|
|
|
page_cache_release(ns->held_pages[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get page cache pages in advance to provide NOFS memory allocation */
|
|
|
|
static int 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;
|
|
|
|
|
|
|
|
start_index = pos >> PAGE_CACHE_SHIFT;
|
|
|
|
end_index = (pos + count - 1) >> PAGE_CACHE_SHIFT;
|
|
|
|
if (end_index - start_index + 1 > NS_MAX_HELD_PAGES)
|
|
|
|
return -EINVAL;
|
|
|
|
ns->held_cnt = 0;
|
|
|
|
for (index = start_index; index <= end_index; index++) {
|
|
|
|
page = find_get_page(mapping, index);
|
|
|
|
if (page == NULL) {
|
|
|
|
page = find_or_create_page(mapping, index, GFP_NOFS);
|
|
|
|
if (page == NULL) {
|
|
|
|
write_inode_now(mapping->host, 1);
|
|
|
|
page = find_or_create_page(mapping, index, GFP_NOFS);
|
|
|
|
}
|
|
|
|
if (page == NULL) {
|
|
|
|
put_pages(ns);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
unlock_page(page);
|
|
|
|
}
|
|
|
|
ns->held_pages[ns->held_cnt++] = page;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_memalloc(void)
|
|
|
|
{
|
|
|
|
if (current->flags & PF_MEMALLOC)
|
|
|
|
return 0;
|
|
|
|
current->flags |= PF_MEMALLOC;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_memalloc(int memalloc)
|
|
|
|
{
|
|
|
|
if (memalloc)
|
|
|
|
current->flags &= ~PF_MEMALLOC;
|
|
|
|
}
|
|
|
|
|
2013-02-24 03:51:48 +08:00
|
|
|
static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
|
2008-11-12 22:06:07 +08:00
|
|
|
{
|
|
|
|
ssize_t tx;
|
|
|
|
int err, memalloc;
|
|
|
|
|
2013-02-24 03:51:48 +08:00
|
|
|
err = get_pages(ns, file, count, pos);
|
2008-11-12 22:06:07 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
memalloc = set_memalloc();
|
2013-02-24 03:51:48 +08:00
|
|
|
tx = kernel_read(file, pos, buf, count);
|
2008-11-12 22:06:07 +08:00
|
|
|
clear_memalloc(memalloc);
|
|
|
|
put_pages(ns);
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
2013-02-24 03:51:48 +08:00
|
|
|
static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
|
2008-11-12 22:06:07 +08:00
|
|
|
{
|
|
|
|
ssize_t tx;
|
|
|
|
int err, memalloc;
|
|
|
|
|
2013-02-24 03:51:48 +08:00
|
|
|
err = get_pages(ns, file, count, pos);
|
2008-11-12 22:06:07 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
memalloc = set_memalloc();
|
2013-02-24 03:51:48 +08:00
|
|
|
tx = kernel_write(file, buf, count, pos);
|
2008-11-12 22:06:07 +08:00
|
|
|
clear_memalloc(memalloc);
|
|
|
|
put_pages(ns);
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
/*
|
|
|
|
* Returns a pointer to the current page.
|
|
|
|
*/
|
|
|
|
static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
|
|
|
|
{
|
|
|
|
return &(ns->pages[ns->regs.row]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retuns a pointer to the current byte, within the current page.
|
|
|
|
*/
|
|
|
|
static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
|
|
|
|
{
|
|
|
|
return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
|
|
|
|
}
|
|
|
|
|
2008-11-12 22:06:07 +08:00
|
|
|
int do_read_error(struct nandsim *ns, int num)
|
|
|
|
{
|
|
|
|
unsigned int page_no = ns->regs.row;
|
|
|
|
|
|
|
|
if (read_error(page_no)) {
|
2012-12-18 08:04:32 +08:00
|
|
|
prandom_bytes(ns->buf.byte, num);
|
2008-11-12 22:06:07 +08:00
|
|
|
NS_WARN("simulating read error in page %u\n", page_no);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_bit_flips(struct nandsim *ns, int num)
|
|
|
|
{
|
2013-01-03 20:19:13 +08:00
|
|
|
if (bitflips && prandom_u32() < (1 << 22)) {
|
2008-11-12 22:06:07 +08:00
|
|
|
int flips = 1;
|
|
|
|
if (bitflips > 1)
|
2013-01-03 20:19:13 +08:00
|
|
|
flips = (prandom_u32() % (int) bitflips) + 1;
|
2008-11-12 22:06:07 +08:00
|
|
|
while (flips--) {
|
2013-01-03 20:19:13 +08:00
|
|
|
int pos = prandom_u32() % (num * 8);
|
2008-11-12 22:06:07 +08:00
|
|
|
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->regs.column + ns->regs.off,
|
|
|
|
nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
/*
|
|
|
|
* Fill the NAND buffer with data read from the specified page.
|
|
|
|
*/
|
|
|
|
static void read_page(struct nandsim *ns, int num)
|
|
|
|
{
|
|
|
|
union ns_mem *mypage;
|
|
|
|
|
2008-11-12 22:06:07 +08:00
|
|
|
if (ns->cfile) {
|
2013-07-28 10:21:53 +08:00
|
|
|
if (!test_bit(ns->regs.row, ns->pages_written)) {
|
2008-11-12 22:06:07 +08:00
|
|
|
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->regs.column + ns->regs.off);
|
|
|
|
if (do_read_error(ns, num))
|
|
|
|
return;
|
|
|
|
pos = (loff_t)ns->regs.row * ns->geom.pgszoob + ns->regs.column + ns->regs.off;
|
2013-02-24 03:51:48 +08:00
|
|
|
tx = read_file(ns, ns->cfile, ns->buf.byte, num, pos);
|
2008-11-12 22:06:07 +08:00
|
|
|
if (tx != num) {
|
|
|
|
NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do_bit_flips(ns, num);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
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->regs.column + ns->regs.off);
|
2008-11-12 22:06:07 +08:00
|
|
|
if (do_read_error(ns, num))
|
2007-03-19 18:47:45 +08:00
|
|
|
return;
|
2006-10-09 00:32:31 +08:00
|
|
|
memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
|
2008-11-12 22:06:07 +08:00
|
|
|
do_bit_flips(ns, num);
|
2006-10-09 00:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Erase all pages in the specified sector.
|
|
|
|
*/
|
|
|
|
static void erase_sector(struct nandsim *ns)
|
|
|
|
{
|
|
|
|
union ns_mem *mypage;
|
|
|
|
int i;
|
|
|
|
|
2008-11-12 22:06:07 +08:00
|
|
|
if (ns->cfile) {
|
|
|
|
for (i = 0; i < ns->geom.pgsec; i++)
|
2013-07-28 10:21:53 +08:00
|
|
|
if (__test_and_clear_bit(ns->regs.row + i,
|
|
|
|
ns->pages_written)) {
|
2008-11-12 22:06:07 +08:00
|
|
|
NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
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);
|
2008-11-13 21:40:38 +08:00
|
|
|
kmem_cache_free(ns->nand_pages_slab, mypage->byte);
|
2006-10-09 00:32:31 +08:00
|
|
|
mypage->byte = NULL;
|
|
|
|
}
|
|
|
|
mypage++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Program the specified page with the contents from the NAND buffer.
|
|
|
|
*/
|
|
|
|
static int prog_page(struct nandsim *ns, int num)
|
|
|
|
{
|
2006-10-20 16:23:56 +08:00
|
|
|
int i;
|
2006-10-09 00:32:31 +08:00
|
|
|
union ns_mem *mypage;
|
|
|
|
u_char *pg_off;
|
|
|
|
|
2008-11-12 22:06:07 +08:00
|
|
|
if (ns->cfile) {
|
2013-02-24 03:51:48 +08:00
|
|
|
loff_t off;
|
2008-11-12 22:06:07 +08:00
|
|
|
ssize_t tx;
|
|
|
|
int all;
|
|
|
|
|
|
|
|
NS_DBG("prog_page: writing page %d\n", ns->regs.row);
|
|
|
|
pg_off = ns->file_buf + ns->regs.column + ns->regs.off;
|
|
|
|
off = (loff_t)ns->regs.row * ns->geom.pgszoob + ns->regs.column + ns->regs.off;
|
2013-07-28 10:21:53 +08:00
|
|
|
if (!test_bit(ns->regs.row, ns->pages_written)) {
|
2008-11-12 22:06:07 +08:00
|
|
|
all = 1;
|
|
|
|
memset(ns->file_buf, 0xff, ns->geom.pgszoob);
|
|
|
|
} else {
|
|
|
|
all = 0;
|
2013-02-24 03:51:48 +08:00
|
|
|
tx = read_file(ns, ns->cfile, pg_off, num, off);
|
2008-11-12 22:06:07 +08:00
|
|
|
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) {
|
2013-02-24 03:51:48 +08:00
|
|
|
loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
|
|
|
|
tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, pos);
|
2008-11-12 22:06:07 +08:00
|
|
|
if (tx != ns->geom.pgszoob) {
|
|
|
|
NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-28 10:21:53 +08:00
|
|
|
__set_bit(ns->regs.row, ns->pages_written);
|
2008-11-12 22:06:07 +08:00
|
|
|
} else {
|
2013-02-24 03:51:48 +08:00
|
|
|
tx = write_file(ns, ns->cfile, pg_off, num, off);
|
2008-11-12 22:06:07 +08:00
|
|
|
if (tx != num) {
|
|
|
|
NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
mypage = NS_GET_PAGE(ns);
|
|
|
|
if (mypage->byte == NULL) {
|
|
|
|
NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
|
[MTD] [NAND] nandsim: avoid deadlocking FS
Make nandsim use GFP_NOFS when allocating memory, because it might
be used by a file-system (e.g. UBIFS2) which means, if we are short
of memory, we may deadlock. Indee, UBIFS is holding a lock, writes
to the media, reaches this place in NANDsim, kmalloc does not find
the requested amount of RAM, calls memory shrinker, which decides
to writeback inodes, calls FS, and it deadlocks on the lock which
is already being held. Below is the UBIFS backtrace which
demonstrates that:
[<c03717dc>] __mutex_lock_slowpath+0xc8/0x2e6
[<c0371a16>] mutex_lock+0x1c/0x1f
[<f8b9d076>] reserve_space+0x3d/0xa9 [ubifs]
[<f8b9d1bd>] make_one_reservation+0x2b/0x86 [ubifs]
[<f8b9d3fc>] ubifs_jrn_write_block+0xda/0x12f [ubifs]
[<f8b9ff3a>] ubifs_writepage+0x11d/0x1ec [ubifs]
[<c015d6ab>] shrink_inactive_list+0x7fa/0x969
[<c015d8c8>] shrink_zone+0xae/0x10c
[<c015e3b4>] try_to_free_pages+0x159/0x251
[<c015980a>] __alloc_pages+0x125/0x2f0
[<c016ff6a>] cache_alloc_refill+0x380/0x6ba
[<c01703f3>] __kmalloc+0x14f/0x157
[<f885722a>] do_state_action+0xab7/0xc74 [nandsim]
[<f885760c>] switch_state+0x225/0x402 [nandsim]
[<f8857e7e>] ns_hwcontrol+0x3e2/0x620 [nandsim]
[<f8862f53>] nand_command+0x2e/0x1a5 [nand]
[<f8861ad8>] nand_write_page+0x4a/0x9a [nand]
[<f88617b4>] nand_do_write_ops+0x1cf/0x343 [nand]
[<f8861a70>] nand_write+0x88/0xa6 [nand]
[<f8850b0e>] part_write+0x72/0x8b [mtd]
[<f88e19c5>] ubi_io_write+0x189/0x29c [ubi]
[<f88dfb98>] ubi_eba_write_leb+0xb6/0x699 [ubi]
[<f88def93>] ubi_leb_write+0xe4/0xe9 [ubi]
[<f8ba3b82>] ubifs_wbuf_write_nolock+0x333/0x4c9 [ubifs]
[<f8b9d28c>] write_node+0x74/0x8e [ubifs]
[<f8b9d422>] ubifs_jrn_write_block+0x100/0x12f [ubifs]
[<f8b9ff3a>] ubifs_writepage+0x11d/0x1ec [ubifs]
[<c0159e5b>] __writepage+0xb/0x26
[<c015a318>] write_cache_pages+0x203/0x2d9
[<c015a411>] generic_writepages+0x23/0x2d
[<c015a452>] do_writepages+0x37/0x39
[<c018e24a>] __writeback_single_inode+0x96/0x399
[<c018e903>] sync_sb_inodes+0x1a3/0x274
[<c018ebf3>] writeback_inodes+0xa6/0xd8
[<c015a9dd>] background_writeout+0x86/0x9e
[<c015ae9c>] pdflush+0xfb/0x1b6
[<c01387d7>] kthread+0x37/0x59
[<c0104dc3>] kernel_thread_helper+0x7/0x14
The deadlock is funny because it starts in pdflush/writeback,
and comes back to writeback, then deadlocks. It seems we should look
carefully for other places in UBI and MTD and use GFP_NOFS instead
of GFP_KERNEL.
Caught-by: Adrian Hunter <ext-adrian.hunter@nokia.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2007-08-29 01:33:32 +08:00
|
|
|
/*
|
|
|
|
* We allocate memory with GFP_NOFS because a flash FS may
|
|
|
|
* utilize this. If it is holding an FS lock, then gets here,
|
2008-11-13 21:40:38 +08:00
|
|
|
* then kernel memory alloc runs writeback which goes to the FS
|
|
|
|
* again and deadlocks. This was seen in practice.
|
[MTD] [NAND] nandsim: avoid deadlocking FS
Make nandsim use GFP_NOFS when allocating memory, because it might
be used by a file-system (e.g. UBIFS2) which means, if we are short
of memory, we may deadlock. Indee, UBIFS is holding a lock, writes
to the media, reaches this place in NANDsim, kmalloc does not find
the requested amount of RAM, calls memory shrinker, which decides
to writeback inodes, calls FS, and it deadlocks on the lock which
is already being held. Below is the UBIFS backtrace which
demonstrates that:
[<c03717dc>] __mutex_lock_slowpath+0xc8/0x2e6
[<c0371a16>] mutex_lock+0x1c/0x1f
[<f8b9d076>] reserve_space+0x3d/0xa9 [ubifs]
[<f8b9d1bd>] make_one_reservation+0x2b/0x86 [ubifs]
[<f8b9d3fc>] ubifs_jrn_write_block+0xda/0x12f [ubifs]
[<f8b9ff3a>] ubifs_writepage+0x11d/0x1ec [ubifs]
[<c015d6ab>] shrink_inactive_list+0x7fa/0x969
[<c015d8c8>] shrink_zone+0xae/0x10c
[<c015e3b4>] try_to_free_pages+0x159/0x251
[<c015980a>] __alloc_pages+0x125/0x2f0
[<c016ff6a>] cache_alloc_refill+0x380/0x6ba
[<c01703f3>] __kmalloc+0x14f/0x157
[<f885722a>] do_state_action+0xab7/0xc74 [nandsim]
[<f885760c>] switch_state+0x225/0x402 [nandsim]
[<f8857e7e>] ns_hwcontrol+0x3e2/0x620 [nandsim]
[<f8862f53>] nand_command+0x2e/0x1a5 [nand]
[<f8861ad8>] nand_write_page+0x4a/0x9a [nand]
[<f88617b4>] nand_do_write_ops+0x1cf/0x343 [nand]
[<f8861a70>] nand_write+0x88/0xa6 [nand]
[<f8850b0e>] part_write+0x72/0x8b [mtd]
[<f88e19c5>] ubi_io_write+0x189/0x29c [ubi]
[<f88dfb98>] ubi_eba_write_leb+0xb6/0x699 [ubi]
[<f88def93>] ubi_leb_write+0xe4/0xe9 [ubi]
[<f8ba3b82>] ubifs_wbuf_write_nolock+0x333/0x4c9 [ubifs]
[<f8b9d28c>] write_node+0x74/0x8e [ubifs]
[<f8b9d422>] ubifs_jrn_write_block+0x100/0x12f [ubifs]
[<f8b9ff3a>] ubifs_writepage+0x11d/0x1ec [ubifs]
[<c0159e5b>] __writepage+0xb/0x26
[<c015a318>] write_cache_pages+0x203/0x2d9
[<c015a411>] generic_writepages+0x23/0x2d
[<c015a452>] do_writepages+0x37/0x39
[<c018e24a>] __writeback_single_inode+0x96/0x399
[<c018e903>] sync_sb_inodes+0x1a3/0x274
[<c018ebf3>] writeback_inodes+0xa6/0xd8
[<c015a9dd>] background_writeout+0x86/0x9e
[<c015ae9c>] pdflush+0xfb/0x1b6
[<c01387d7>] kthread+0x37/0x59
[<c0104dc3>] kernel_thread_helper+0x7/0x14
The deadlock is funny because it starts in pdflush/writeback,
and comes back to writeback, then deadlocks. It seems we should look
carefully for other places in UBI and MTD and use GFP_NOFS instead
of GFP_KERNEL.
Caught-by: Adrian Hunter <ext-adrian.hunter@nokia.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2007-08-29 01:33:32 +08:00
|
|
|
*/
|
2008-11-13 21:40:38 +08:00
|
|
|
mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS);
|
2006-10-09 00:32:31 +08:00
|
|
|
if (mypage->byte == NULL) {
|
|
|
|
NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(mypage->byte, 0xFF, ns->geom.pgszoob);
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_off = NS_PAGE_BYTE_OFF(ns);
|
2006-10-20 16:23:56 +08:00
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
pg_off[i] &= ns->buf.byte[i];
|
2006-10-09 00:32:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* If state has any action bit, perform this action.
|
|
|
|
*
|
|
|
|
* RETURNS: 0 if success, -1 if error.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static int do_state_action(struct nandsim *ns, uint32_t action)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-10-09 00:32:31 +08:00
|
|
|
int num;
|
2005-04-17 06:20:36 +08:00
|
|
|
int busdiv = ns->busw == 8 ? 1 : 2;
|
2007-03-19 18:47:45 +08:00
|
|
|
unsigned int erase_block_no, page_no;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
action &= ACTION_MASK;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 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:
|
|
|
|
/*
|
|
|
|
* Copy page data to the internal buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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->regs.off - ns->regs.column;
|
2006-10-09 00:32:31 +08:00
|
|
|
read_page(ns, num);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
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);
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->regs.off == 0)
|
|
|
|
NS_LOG("read page %d\n", ns->regs.row);
|
|
|
|
else if (ns->regs.off < ns->geom.pgsz)
|
|
|
|
NS_LOG("read page %d (second half)\n", ns->regs.row);
|
|
|
|
else
|
|
|
|
NS_LOG("read OOB of page %d\n", ns->regs.row);
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
NS_UDELAY(access_delay);
|
|
|
|
NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_SECERASE:
|
|
|
|
/*
|
|
|
|
* Erase sector.
|
|
|
|
*/
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->lines.wp) {
|
|
|
|
NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
|
|
|
|
|| (ns->regs.row & ~(ns->geom.secsz - 1))) {
|
|
|
|
NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ns->regs.row = (ns->regs.row <<
|
|
|
|
8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
|
|
|
|
ns->regs.column = 0;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2007-03-19 18:47:45 +08:00
|
|
|
erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
|
|
|
|
ns->regs.row, NS_RAW_OFFSET(ns));
|
2007-03-19 18:47:45 +08:00
|
|
|
NS_LOG("erase sector %u\n", erase_block_no);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
erase_sector(ns);
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
NS_MDELAY(erase_delay);
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2007-03-19 18:40:41 +08:00
|
|
|
if (erase_block_wear)
|
|
|
|
update_wear(erase_block_no);
|
|
|
|
|
2007-03-19 18:47:45 +08:00
|
|
|
if (erase_error(erase_block_no)) {
|
|
|
|
NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_PRGPAGE:
|
|
|
|
/*
|
2010-11-13 18:46:05 +08:00
|
|
|
* Program page - move internal buffer data to the page.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (ns->lines.wp) {
|
|
|
|
NS_WARN("do_state_action: device is write-protected, programm\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-10-09 00:32:31 +08:00
|
|
|
if (prog_page(ns, num) == -1)
|
|
|
|
return -1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-19 18:47:45 +08:00
|
|
|
page_no = ns->regs.row;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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);
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
NS_UDELAY(programm_delay);
|
|
|
|
NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2007-03-19 18:47:45 +08:00
|
|
|
if (write_error(page_no)) {
|
|
|
|
NS_WARN("simulating write failure in page %u\n", page_no);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
case ACTION_ZEROOFF:
|
|
|
|
NS_DBG("do_state_action: set internal offset to 0\n");
|
|
|
|
ns->regs.off = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_HALFOFF:
|
|
|
|
if (!(ns->options & OPT_PAGE512_8BIT)) {
|
|
|
|
NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
|
|
|
|
"byte page size 8x chips\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
|
|
|
|
ns->regs.off = ns->geom.pgsz/2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_OOBOFF:
|
|
|
|
NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
|
|
|
|
ns->regs.off = ns->geom.pgsz;
|
|
|
|
break;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
|
|
|
NS_DBG("do_state_action: BUG! unknown action\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Switch simulator's state.
|
|
|
|
*/
|
2006-10-15 00:03:34 +08:00
|
|
|
static void switch_state(struct nandsim *ns)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
if (ns->op) {
|
|
|
|
/*
|
|
|
|
* The current operation have already been identified.
|
|
|
|
* Just follow the states chain.
|
|
|
|
*/
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ns->stateidx += 1;
|
|
|
|
ns->state = ns->nxstate;
|
|
|
|
ns->nxstate = ns->op[ns->stateidx + 1];
|
|
|
|
|
|
|
|
NS_DBG("switch_state: operation is known, switch to the next state, "
|
|
|
|
"state: %s, nxstate: %s\n",
|
|
|
|
get_state_name(ns->state), get_state_name(ns->nxstate));
|
|
|
|
|
|
|
|
/* See, whether we need to do some action */
|
|
|
|
if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
|
|
|
|
switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
|
|
|
|
return;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We don't yet know which operation we perform.
|
|
|
|
* Try to identify it.
|
|
|
|
*/
|
|
|
|
|
2005-11-07 19:15:49 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* The only event causing the switch_state function to
|
|
|
|
* be called with yet unknown operation is new command.
|
|
|
|
*/
|
|
|
|
ns->state = get_state_by_command(ns->regs.command);
|
|
|
|
|
|
|
|
NS_DBG("switch_state: operation is unknown, try to find it\n");
|
|
|
|
|
|
|
|
if (find_operation(ns, 0) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
|
|
|
|
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) {
|
|
|
|
/*
|
|
|
|
* The current state is the last. Return to STATE_READY
|
|
|
|
*/
|
|
|
|
|
|
|
|
u_char status = NS_STATUS_OK(ns);
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 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);
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
|
|
|
|
|
|
|
|
switch_to_ready_state(ns, status);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
|
2005-11-07 19:15:49 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* If the next state is data input/output, switch to it now
|
|
|
|
*/
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ns->state = ns->nxstate;
|
|
|
|
ns->nxstate = ns->op[++ns->stateidx + 1];
|
|
|
|
ns->regs.num = ns->regs.count = 0;
|
|
|
|
|
|
|
|
NS_DBG("switch_state: the next state is data I/O, switch, "
|
|
|
|
"state: %s, nxstate: %s\n",
|
|
|
|
get_state_name(ns->state), get_state_name(ns->nxstate));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the internal register to the count of bytes which
|
|
|
|
* are expected to be input or output
|
|
|
|
*/
|
|
|
|
switch (NS_STATE(ns->state)) {
|
|
|
|
case STATE_DATAIN:
|
|
|
|
case STATE_DATAOUT:
|
|
|
|
ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
|
|
|
|
break;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
case STATE_DATAOUT_ID:
|
|
|
|
ns->regs.num = ns->geom.idbytes;
|
|
|
|
break;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
case STATE_DATAOUT_STATUS:
|
|
|
|
case STATE_DATAOUT_STATUS_M:
|
|
|
|
ns->regs.count = ns->regs.num = 0;
|
|
|
|
break;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
|
|
|
NS_ERR("switch_state: BUG! unknown data state\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (ns->nxstate & STATE_ADDR_MASK) {
|
|
|
|
/*
|
|
|
|
* If the next state is address input, set the internal
|
|
|
|
* register to the number of expected address bytes
|
|
|
|
*/
|
|
|
|
|
|
|
|
ns->regs.count = 0;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
switch (NS_STATE(ns->nxstate)) {
|
|
|
|
case STATE_ADDR_PAGE:
|
|
|
|
ns->regs.num = ns->geom.pgaddrbytes;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
case STATE_ADDR_SEC:
|
|
|
|
ns->regs.num = ns->geom.secaddrbytes;
|
|
|
|
break;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
case STATE_ADDR_ZERO:
|
|
|
|
ns->regs.num = 1;
|
|
|
|
break;
|
|
|
|
|
2008-07-30 16:18:42 +08:00
|
|
|
case STATE_ADDR_COLUMN:
|
|
|
|
/* Column address is always 2 bytes */
|
|
|
|
ns->regs.num = ns->geom.pgaddrbytes - ns->geom.secaddrbytes;
|
|
|
|
break;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
default:
|
|
|
|
NS_ERR("switch_state: BUG! unknown address state\n");
|
|
|
|
}
|
|
|
|
} else {
|
2005-11-07 19:15:49 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Just reset internal counters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ns->regs.num = 0;
|
|
|
|
ns->regs.count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-15 00:03:34 +08:00
|
|
|
static u_char ns_nand_read_byte(struct mtd_info *mtd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-06-29 18:15:17 +08:00
|
|
|
struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
|
2005-04-17 06:20:36 +08:00
|
|
|
u_char outb = 0x00;
|
|
|
|
|
|
|
|
/* 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", 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (NS_STATE(ns->state)) {
|
|
|
|
case STATE_DATAOUT:
|
|
|
|
if (ns->busw == 8) {
|
|
|
|
outb = ns->buf.byte[ns->regs.count];
|
|
|
|
ns->regs.count += 1;
|
|
|
|
} else {
|
|
|
|
outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
|
|
|
|
ns->regs.count += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case STATE_DATAOUT_ID:
|
|
|
|
NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
|
|
|
|
outb = ns->ids[ns->regs.count];
|
|
|
|
ns->regs.count += 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->regs.count == ns->regs.num) {
|
|
|
|
NS_DBG("read_byte: all bytes were read\n");
|
|
|
|
|
2012-05-02 08:12:53 +08:00
|
|
|
if (NS_STATE(ns->nxstate) == STATE_READY)
|
2005-04-17 06:20:36 +08:00
|
|
|
switch_state(ns);
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return outb;
|
|
|
|
}
|
|
|
|
|
2006-10-15 00:03:34 +08:00
|
|
|
static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-06-29 18:15:17 +08:00
|
|
|
struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->lines.cle == 1) {
|
|
|
|
/*
|
|
|
|
* The byte written is a command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (byte == NAND_CMD_RESET) {
|
|
|
|
NS_LOG("reset chip\n");
|
|
|
|
switch_to_ready_state(ns, NS_STATUS_OK(ns));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-30 16:18:42 +08:00
|
|
|
/* Check that the command byte is correct */
|
|
|
|
if (check_command(byte)) {
|
|
|
|
NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
|
|
|
|
|| NS_STATE(ns->state) == STATE_DATAOUT_STATUS_M
|
2008-07-30 16:18:42 +08:00
|
|
|
|| NS_STATE(ns->state) == STATE_DATAOUT) {
|
|
|
|
int row = ns->regs.row;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
switch_state(ns);
|
2008-07-30 16:18:42 +08:00
|
|
|
if (byte == NAND_CMD_RNDOUT)
|
|
|
|
ns->regs.row = row;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Check if chip is expecting command */
|
|
|
|
if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
|
2008-11-12 22:06:40 +08:00
|
|
|
/* Do not warn if only 2 id bytes are read */
|
|
|
|
if (!(ns->regs.command == NAND_CMD_READID &&
|
|
|
|
NS_STATE(ns->state) == STATE_DATAOUT_ID && ns->regs.count == 2)) {
|
|
|
|
/*
|
|
|
|
* We are in situation when something else (not command)
|
|
|
|
* was expected but command was input. In this case ignore
|
|
|
|
* previous command(s)/state(s) and accept the last one.
|
|
|
|
*/
|
|
|
|
NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
|
|
|
|
"ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
NS_DBG("command byte corresponding to %s state accepted\n",
|
|
|
|
get_state_name(get_state_by_command(byte)));
|
|
|
|
ns->regs.command = byte;
|
|
|
|
switch_state(ns);
|
|
|
|
|
|
|
|
} else if (ns->lines.ale == 1) {
|
|
|
|
/*
|
|
|
|
* The byte written is an address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
|
|
|
|
|
|
|
|
NS_DBG("write_byte: operation isn't known yet, identify it\n");
|
|
|
|
|
|
|
|
if (find_operation(ns, 1) < 0)
|
|
|
|
return;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
|
|
|
|
switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
|
|
|
|
return;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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, get_state_name(ns->nxstate));
|
|
|
|
switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
|
|
|
|
return;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Check if this is expected byte */
|
|
|
|
if (ns->regs.count == ns->regs.num) {
|
|
|
|
NS_ERR("write_byte: no more address bytes expected\n");
|
|
|
|
switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
accept_addr_byte(ns, byte);
|
|
|
|
|
|
|
|
ns->regs.count += 1;
|
|
|
|
|
|
|
|
NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
|
|
|
|
(uint)byte, ns->regs.count, ns->regs.num);
|
|
|
|
|
|
|
|
if (ns->regs.count == ns->regs.num) {
|
|
|
|
NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
|
|
|
|
switch_state(ns);
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The byte written is an input data.
|
|
|
|
*/
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 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,
|
|
|
|
get_state_name(ns->state), get_state_name(STATE_READY));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ns->busw == 8) {
|
|
|
|
ns->buf.byte[ns->regs.count] = byte;
|
|
|
|
ns->regs.count += 1;
|
|
|
|
} else {
|
|
|
|
ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
|
|
|
|
ns->regs.count += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-05-24 05:25:53 +08:00
|
|
|
static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
|
|
|
|
{
|
|
|
|
struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
|
|
|
|
|
|
|
|
ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
|
|
|
|
ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
|
|
|
|
ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
|
|
|
|
|
|
|
|
if (cmd != NAND_CMD_NONE)
|
|
|
|
ns_nand_write_byte(mtd, cmd);
|
|
|
|
}
|
|
|
|
|
2006-10-15 00:03:34 +08:00
|
|
|
static int ns_device_ready(struct mtd_info *mtd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
NS_DBG("device_ready\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-10-15 00:03:34 +08:00
|
|
|
static uint16_t ns_nand_read_word(struct mtd_info *mtd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct nand_chip *chip = (struct nand_chip *)mtd->priv;
|
|
|
|
|
|
|
|
NS_DBG("read_word\n");
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
|
|
|
|
}
|
|
|
|
|
2006-10-15 00:03:34 +08:00
|
|
|
static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-06-29 18:15:17 +08:00
|
|
|
struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* 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", get_state_name(ns->state));
|
|
|
|
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");
|
|
|
|
switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ns->buf.byte + ns->regs.count, buf, len);
|
|
|
|
ns->regs.count += len;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->regs.count == ns->regs.num) {
|
|
|
|
NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-15 00:03:34 +08:00
|
|
|
static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-06-29 18:15:17 +08:00
|
|
|
struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* 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",
|
|
|
|
get_state_name(ns->state));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_STATE(ns->state) != STATE_DATAOUT) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
|
|
|
|
|
|
|
|
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");
|
|
|
|
switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf, ns->buf.byte + ns->regs.count, len);
|
|
|
|
ns->regs.count += len;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ns->regs.count == ns->regs.num) {
|
2012-05-02 08:12:53 +08:00
|
|
|
if (NS_STATE(ns->nxstate) == STATE_READY)
|
2005-04-17 06:20:36 +08:00
|
|
|
switch_state(ns);
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module initialization function
|
|
|
|
*/
|
2005-11-29 22:49:38 +08:00
|
|
|
static int __init ns_init_module(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct nand_chip *chip;
|
|
|
|
struct nandsim *nand;
|
2007-03-19 18:46:43 +08:00
|
|
|
int retval = -ENOMEM, i;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (bus_width != 8 && bus_width != 16) {
|
|
|
|
NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Allocate and initialize mtd_info, nand_chip and nandsim structures */
|
2006-11-16 03:10:29 +08:00
|
|
|
nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
|
2005-04-17 06:20:36 +08:00
|
|
|
+ sizeof(struct nandsim), GFP_KERNEL);
|
|
|
|
if (!nsmtd) {
|
|
|
|
NS_ERR("unable to allocate core structures.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
chip = (struct nand_chip *)(nsmtd + 1);
|
|
|
|
nsmtd->priv = (void *)chip;
|
|
|
|
nand = (struct nandsim *)(chip + 1);
|
2005-11-07 19:15:49 +08:00
|
|
|
chip->priv = (void *)nand;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Register simulator's callbacks.
|
|
|
|
*/
|
2006-05-24 05:25:53 +08:00
|
|
|
chip->cmd_ctrl = ns_hwcontrol;
|
2005-04-17 06:20:36 +08:00
|
|
|
chip->read_byte = ns_nand_read_byte;
|
|
|
|
chip->dev_ready = ns_device_ready;
|
|
|
|
chip->write_buf = ns_nand_write_buf;
|
|
|
|
chip->read_buf = ns_nand_read_buf;
|
|
|
|
chip->read_word = ns_nand_read_word;
|
2006-05-23 18:00:46 +08:00
|
|
|
chip->ecc.mode = NAND_ECC_SOFT;
|
2007-03-19 18:49:11 +08:00
|
|
|
/* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
|
|
|
|
/* and 'badblocks' parameters to work */
|
2005-03-19 23:33:59 +08:00
|
|
|
chip->options |= NAND_SKIP_BBTSCAN;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-09-30 01:43:54 +08:00
|
|
|
switch (bbt) {
|
|
|
|
case 2:
|
2011-06-01 07:31:22 +08:00
|
|
|
chip->bbt_options |= NAND_BBT_NO_OOB;
|
2010-09-30 01:43:54 +08:00
|
|
|
case 1:
|
2011-06-01 07:31:23 +08:00
|
|
|
chip->bbt_options |= NAND_BBT_USE_FLASH;
|
2010-09-30 01:43:54 +08:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERR("bbt has to be 0..2\n");
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
2005-11-07 19:15:49 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Perform minimum nandsim structure initialization to handle
|
2005-11-07 19:15:49 +08:00
|
|
|
* the initial ID read command correctly
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
if (third_id_byte != 0xFF || fourth_id_byte != 0xFF)
|
|
|
|
nand->geom.idbytes = 4;
|
|
|
|
else
|
|
|
|
nand->geom.idbytes = 2;
|
|
|
|
nand->regs.status = NS_STATUS_OK(nand);
|
|
|
|
nand->nxstate = STATE_UNKNOWN;
|
2013-03-05 21:00:51 +08:00
|
|
|
nand->options |= OPT_PAGE512; /* temporary value */
|
2005-04-17 06:20:36 +08:00
|
|
|
nand->ids[0] = first_id_byte;
|
|
|
|
nand->ids[1] = second_id_byte;
|
|
|
|
nand->ids[2] = third_id_byte;
|
|
|
|
nand->ids[3] = fourth_id_byte;
|
|
|
|
if (bus_width == 16) {
|
|
|
|
nand->busw = 16;
|
|
|
|
chip->options |= NAND_BUSWIDTH_16;
|
|
|
|
}
|
|
|
|
|
2006-05-14 08:20:46 +08:00
|
|
|
nsmtd->owner = THIS_MODULE;
|
|
|
|
|
2007-03-19 18:47:45 +08:00
|
|
|
if ((retval = parse_weakblocks()) != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((retval = parse_weakpages()) != 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((retval = parse_gravepages()) != 0)
|
|
|
|
goto error;
|
|
|
|
|
2011-03-11 18:05:34 +08:00
|
|
|
retval = nand_scan_ident(nsmtd, 1, NULL);
|
|
|
|
if (retval) {
|
|
|
|
NS_ERR("cannot scan NAND Simulator device\n");
|
|
|
|
if (retval > 0)
|
|
|
|
retval = -ENXIO;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bch) {
|
|
|
|
unsigned int eccsteps, eccbytes;
|
|
|
|
if (!mtd_nand_has_bch()) {
|
|
|
|
NS_ERR("BCH ECC support is disabled\n");
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* use 512-byte ecc blocks */
|
|
|
|
eccsteps = nsmtd->writesize/512;
|
|
|
|
eccbytes = (bch*13+7)/8;
|
|
|
|
/* do not bother supporting small page devices */
|
|
|
|
if ((nsmtd->oobsize < 64) || !eccsteps) {
|
|
|
|
NS_ERR("bch not available on small page devices\n");
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if ((eccbytes*eccsteps+2) > nsmtd->oobsize) {
|
|
|
|
NS_ERR("invalid bch value %u\n", bch);
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
chip->ecc.mode = NAND_ECC_SOFT_BCH;
|
|
|
|
chip->ecc.size = 512;
|
|
|
|
chip->ecc.bytes = eccbytes;
|
|
|
|
NS_INFO("using %u-bit/%u bytes BCH ECC\n", bch, chip->ecc.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = nand_scan_tail(nsmtd);
|
|
|
|
if (retval) {
|
2005-04-17 06:20:36 +08:00
|
|
|
NS_ERR("can't register NAND Simulator\n");
|
|
|
|
if (retval > 0)
|
|
|
|
retval = -ENXIO;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2007-03-19 18:49:11 +08:00
|
|
|
if (overridesize) {
|
2008-12-10 22:01:46 +08:00
|
|
|
uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
|
2007-03-19 18:49:11 +08:00
|
|
|
if (new_size >> overridesize != nsmtd->erasesize) {
|
|
|
|
NS_ERR("overridesize is too big\n");
|
2012-09-12 20:26:26 +08:00
|
|
|
retval = -EINVAL;
|
2007-03-19 18:49:11 +08:00
|
|
|
goto err_exit;
|
|
|
|
}
|
|
|
|
/* N.B. This relies on nand_scan not doing anything with the size before we change it */
|
|
|
|
nsmtd->size = new_size;
|
|
|
|
chip->chipsize = new_size;
|
2008-05-30 20:56:26 +08:00
|
|
|
chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
|
2008-05-30 20:56:23 +08:00
|
|
|
chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
|
2007-03-19 18:49:11 +08:00
|
|
|
}
|
|
|
|
|
2007-03-19 18:40:41 +08:00
|
|
|
if ((retval = setup_wear_reporting(nsmtd)) != 0)
|
|
|
|
goto err_exit;
|
|
|
|
|
2012-12-03 21:31:40 +08:00
|
|
|
if ((retval = nandsim_debugfs_create(nand)) != 0)
|
|
|
|
goto err_exit;
|
|
|
|
|
2007-03-19 18:46:43 +08:00
|
|
|
if ((retval = init_nandsim(nsmtd)) != 0)
|
|
|
|
goto err_exit;
|
2005-11-07 19:15:49 +08:00
|
|
|
|
2010-09-30 01:43:54 +08:00
|
|
|
if ((retval = nand_default_bbt(nsmtd)) != 0)
|
2007-03-19 18:47:45 +08:00
|
|
|
goto err_exit;
|
|
|
|
|
2010-09-30 01:43:54 +08:00
|
|
|
if ((retval = parse_badblocks(nand, nsmtd)) != 0)
|
2007-03-19 18:46:43 +08:00
|
|
|
goto err_exit;
|
2005-03-19 23:33:59 +08:00
|
|
|
|
2007-03-19 18:46:43 +08:00
|
|
|
/* Register NAND partitions */
|
2011-05-23 17:23:40 +08:00
|
|
|
retval = mtd_device_register(nsmtd, &nand->partitions[0],
|
|
|
|
nand->nbparts);
|
|
|
|
if (retval != 0)
|
2007-03-19 18:46:43 +08:00
|
|
|
goto err_exit;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2007-03-19 18:46:43 +08:00
|
|
|
err_exit:
|
|
|
|
free_nandsim(nand);
|
|
|
|
nand_release(nsmtd);
|
|
|
|
for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
|
|
|
|
kfree(nand->partitions[i].name);
|
2005-04-17 06:20:36 +08:00
|
|
|
error:
|
|
|
|
kfree(nsmtd);
|
2007-03-19 18:47:45 +08:00
|
|
|
free_lists();
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(ns_init_module);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module clean-up function
|
|
|
|
*/
|
|
|
|
static void __exit ns_cleanup_module(void)
|
|
|
|
{
|
2010-06-29 18:15:17 +08:00
|
|
|
struct nandsim *ns = ((struct nand_chip *)nsmtd->priv)->priv;
|
2007-03-19 18:46:43 +08:00
|
|
|
int i;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-12-03 21:31:40 +08:00
|
|
|
nandsim_debugfs_remove(ns);
|
2005-04-17 06:20:36 +08:00
|
|
|
free_nandsim(ns); /* Free nandsim private resources */
|
2007-03-19 18:46:43 +08:00
|
|
|
nand_release(nsmtd); /* Unregister driver */
|
|
|
|
for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
|
|
|
|
kfree(ns->partitions[i].name);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(nsmtd); /* Free other structures */
|
2007-03-19 18:47:45 +08:00
|
|
|
free_lists();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_exit(ns_cleanup_module);
|
|
|
|
|
|
|
|
MODULE_LICENSE ("GPL");
|
|
|
|
MODULE_AUTHOR ("Artem B. Bityuckiy");
|
|
|
|
MODULE_DESCRIPTION ("The NAND flash simulator");
|