Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1253893
  • 博文数量: 479
  • 博客积分: 12240
  • 博客等级: 上将
  • 技术积分: 4999
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-24 17:12
文章分类

全部博文(479)

文章存档

2014年(1)

2013年(1)

2012年(1)

2011年(95)

2010年(177)

2009年(167)

2008年(16)

2007年(21)

分类: LINUX

2008-01-02 20:39:59


U-Boot 1.3.1是U-Boot目前的最新版本,和以前的版本相比,增加了对大页面NAND  FLASH 的支持。早期版本基本上只能支持到512字节的页面大小,而现在市面上大多是2K字节页面大小的NAND FLASH,这样的FLASH容量大,价格低,性价比很高,很受电子设计工程师的青睐。

看源代码board目录下,有很多板子的配置。可以使用源代码阅读工具,搜索一下,可以看到其中有很多板子的目录下都有一个nand.c文件,文件中有一些nand flash必须的借口函数。下面逐一介绍。

board_nand_init函数

在这个函数可以定义NAND  FLASH 命令/数据的地址,典型的可以看board/sc3/sc3nand.c文件:

    sc3_io_base = (void *) CFG_NAND_BASE;
    /* Set address of NAND IO lines (Using Linear Data Access Region) */
    nand->IO_ADDR_R = (void __iomem *) sc3_io_base;
    nand->IO_ADDR_W = (void __iomem *) sc3_io_base;


还有一些接口也是必须的,如设置用户自定义的一些接口函数,比较典型的可以看board/prodrive/pdnp3/nand.c文件:

int board_nand_init(struct nand_chip *nand)
{
    pdnb3_ndfc = (struct pdnb3_ndfc_regs *)CFG_NAND_BASE;

    nand->eccmode = NAND_ECC_SOFT;

    /* Set address of NAND IO lines (Using Linear Data Access Region) */
    nand->IO_ADDR_R = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4);
    nand->IO_ADDR_W = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4);
    /* Reference hardware control function */
    nand->hwcontrol = pdnb3_nand_hwcontrol;
    /* Set command delay time */
    nand->hwcontrol = pdnb3_nand_hwcontrol;
    nand->write_byte = pdnb3_nand_write_byte;
    nand->read_byte = pdnb3_nand_read_byte;
    nand->write_buf = pdnb3_nand_write_buf;
    nand->read_buf = pdnb3_nand_read_buf;
    nand->verify_buf = pdnb3_nand_verify_buf;
    nand->dev_ready = pdnb3_nand_dev_ready;
    return 0;
}


如果在这里定义了这些接口函数,则必须实现,用于替换u-boot的默认函数实现。

nand_chip数据结构


其实,这里面最重要的是nand_chip数据结构,请看文件include/linux/mtd/nand.h:


/**
 * struct nand_chip - NAND Private Flash Chip Data
 * @IO_ADDR_R:                [BOARDSPECIFIC] address to read the 8 I/O lines of the flash device
 * @IO_ADDR_W:              [BOARDSPECIFIC] address to write the 8 I/O lines of the flash device
 * @read_byte:                [REPLACEABLE] read one byte from the chip
 * @write_byte:        [REPLACEABLE] write one byte to the chip
 * @read_word:        [REPLACEABLE] read one word from the chip
 * @write_word:        [REPLACEABLE] write one word to the chip
 * @write_buf:            [REPLACEABLE] write data from the buffer to the chip
 * @read_buf:            [REPLACEABLE] read data from the chip into the buffer
 * @verify_buf:            [REPLACEABLE] verify buffer contents against the chip data
 * @select_chip:        [REPLACEABLE] select chip nr
 * @block_bad:            [REPLACEABLE] check, if the block is bad
 * @block_markbad:    [REPLACEABLE] mark the block bad
 * @hwcontrol:           [BOARDSPECIFIC] hardwarespecific function for accesing control-lines
 * @dev_ready:          [BOARDSPECIFIC] hardwarespecific function for accesing device ready/busy line
 *            If set to NULL no access to ready/busy is available and the ready/busy information
 *            is read from the chip status register
 * @cmdfunc:        [REPLACEABLE] hardwarespecific function for writing commands to the chip
 * @waitfunc:        [REPLACEABLE] hardwarespecific function for wait on ready
 * @calculate_ecc:    [REPLACEABLE] function for ecc calculation or readback from ecc hardware
 * @correct_data:    [REPLACEABLE] function for ecc correction, matching to ecc generator (sw/hw)
 * @enable_hwecc:    [BOARDSPECIFIC] function to enable (reset) hardware ecc generator. Must only
 *            be provided if a hardware ECC is available
 * @erase_cmd:        [INTERN] erase command write function, selectable due to AND support
 * @scan_bbt:        [REPLACEABLE] function to scan bad block table
 * @eccmode:        [BOARDSPECIFIC] mode of ecc, see defines
 * @eccsize:        [INTERN] databytes used per ecc-calculation
 * @eccbytes:        [INTERN] number of ecc bytes per ecc-calculation step
 * @eccsteps:        [INTERN] number of ecc calculation steps per page
 * @chip_delay:        [BOARDSPECIFIC] chip dependent delay for transfering data from array to read regs (tR)
 * @chip_lock:        [INTERN] spinlock used to protect access to this structure and the chip
 * @wq:                    [INTERN] wait queue to sleep on if a NAND operation is in progress
 * @state:                [INTERN] the current state of the NAND device
 * @page_shift:        [INTERN] number of address bits in a page (column address bits)
 * @phys_erase_shift:    [INTERN] number of address bits in a physical eraseblock
 * @bbt_erase_shift:    [INTERN] number of address bits in a bbt entry
 * @chip_shift:        [INTERN] number of address bits in one chip
 * @data_buf:        [INTERN] internal buffer for one page + oob
 * @oob_buf:        [INTERN] oob buffer for one eraseblock
 * @oobdirty:        [INTERN] indicates that oob_buf must be reinitialized
 * @data_poi:        [INTERN] pointer to a data buffer
 * @options:        [BOARDSPECIFIC] various chip options. They can partly be set to inform nand_scan about
 *            special functionality. See the defines for further explanation
 * @badblockpos:    [INTERN] position of the bad block marker in the oob area
 * @numchips:        [INTERN] number of physical chips
 * @chipsize:        [INTERN] the size of one chip for multichip arrays
 * @pagemask:        [INTERN] page number mask = number of (pages / chip) - 1
 * @pagebuf:        [INTERN] holds the pagenumber which is currently in data_buf
 * @autooob:        [REPLACEABLE] the default (auto)placement scheme
 * @bbt:        [INTERN] bad block table pointer
 * @bbt_td:        [REPLACEABLE] bad block table descriptor for flash lookup
 * @bbt_md:        [REPLACEABLE] bad block table mirror descriptor
 * @badblock_pattern:    [REPLACEABLE] bad block scan pattern used for initial bad block scan
 * @controller:        [OPTIONAL] a pointer to a hardware controller structure which is shared among multiple independend devices
 * @priv:        [OPTIONAL] pointer to private chip date
 */


struct nand_chip {
    void __iomem    *IO_ADDR_R;
    void __iomem    *IO_ADDR_W;

    u_char        (*read_byte)(struct mtd_info *mtd);
    void            (*write_byte)(struct mtd_info *mtd, u_char byte);
    u16            (*read_word)(struct mtd_info *mtd);
    void            (*write_word)(struct mtd_info *mtd, u16 word);

    void        (*write_buf)(struct mtd_info *mtd, const u_char *buf, int len);
    void        (*read_buf)(struct mtd_info *mtd, u_char *buf, int len);
    int        (*verify_buf)(struct mtd_info *mtd, const u_char *buf, int len);
    void        (*select_chip)(struct mtd_info *mtd, int chip);
    int        (*block_bad)(struct mtd_info *mtd, loff_t ofs, int getchip);
    int        (*block_markbad)(struct mtd_info *mtd, loff_t ofs);
    void        (*hwcontrol)(struct mtd_info *mtd, int cmd);
    int        (*dev_ready)(struct mtd_info *mtd);
    void        (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column, int page_addr);
    int        (*waitfunc)(struct mtd_info *mtd, struct nand_chip *this, int state);
    int        (*calculate_ecc)(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code);
    int        (*correct_data)(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc);
    void        (*enable_hwecc)(struct mtd_info *mtd, int mode);
    void        (*erase_cmd)(struct mtd_info *mtd, int page);
    int        (*scan_bbt)(struct mtd_info *mtd);
    int        eccmode;
    int        eccsize;
    int        eccbytes;
    int        eccsteps;
    int        chip_delay;
#if 0
    spinlock_t    chip_lock;
    wait_queue_head_t wq;
    nand_state_t    state;
#endif
    int                                page_shift;
    int                                phys_erase_shift;
    int                                bbt_erase_shift;
    int                                chip_shift;
    u_char                            *data_buf;
    u_char                            *oob_buf;
    int                                    oobdirty;
    u_char                            *data_poi;
    unsigned int                options;
    int                                        badblockpos;
    int                                        numchips;
    unsigned long                    chipsize;
    int                                        pagemask;
    int                                        pagebuf;
    struct nand_oobinfo        *autooob;
    uint8_t                                *bbt;
    struct nand_bbt_descr    *bbt_td;
    struct nand_bbt_descr    *bbt_md;
    struct nand_bbt_descr    *badblock_pattern;
    struct nand_hw_control    *controller;
    void                                        *priv;
};


这个数据结构的注视非常明了,指出了那些函数可以被替换,可被替换的函数,用户可以根据自己的需求实现,一般在上面所讲的nand.c中实现。当然,在其他地方实现也可以,U-Boot源代码中也有这样的例子。

nand_base.c文件


nand_chip数据结构定义的函数,U-Boot的默认实现在drivers/mtd/nand/nand_base.c文件中。可以仔细看看源码。如果要进行替换,这些函数有一定的参考价值。

如果要为nand_chip数据结构增加某些接口函数或者某些成员,可以修改这两个文件。

nand Flash和nand_legacy


nand目录下的文件是能够支持2K大页面的NAND  FLASH的实现,而nand_legacy目录下的文件只能支持到512字节的页面,在使用的时候需要加以区分。

阅读(4156) | 评论(0) | 转发(0) |
0

上一篇:ubuntu 系统配置

下一篇:U-Boot写yaffs2文件

给主人留下些什么吧!~~