📄 u-boot代码记录.c
字号:
int floor, chip;
int numchips[NAND_MAX_FLOORS];
int maxchips = NAND_MAX_CHIPS;
int ret = 1;
nand->numchips = 0; //nandflash 芯片个数
nand->mfr = 0; //厂商ID
nand->id = 0; //模式ID
/* For each floor, find the number of valid chips it contains */
for (floor = 0; floor < NAND_MAX_FLOORS; floor++) {
ret = 1;
numchips[floor] = 0;
for (chip = 0; chip < maxchips && ret != 0; chip++) {
ret = NanD_IdentChip(nand, floor, chip);
if (ret) //成功
{
numchips[floor]++;
nand->numchips++;
}
}
}
/* If there are none at all that we recognise, bail */
if (!nand->numchips) //不执行
{
#ifdef NAND_DEBUG
puts ("No NAND flash chips recognised.\n");
#endif
return;
}
/* Allocate an array to hold the information for each chip */
nand->chips = malloc(sizeof(struct Nand) * nand->numchips);
if (!nand->chips) //上面语句成功则不执行
{
puts ("No memory for allocating chip info structures\n");
return;
}
ret = 0;
/* Fill out the chip array with {floor, chipno} for each
* detected chip in the device. */
for (floor = 0; floor < NAND_MAX_FLOORS; floor++)
{
for (chip = 0; chip < numchips[floor]; chip++)
{
nand->chips[ret].floor = floor;
nand->chips[ret].chip = chip;
nand->chips[ret].curadr = 0;
nand->chips[ret].curmode = 0x50;
ret++;
}
}
/* Calculate and print the total size of the device */
nand->totlen = nand->numchips * (1 << nand->chipshift); //nandflash的容量为64Mb
#ifdef NAND_DEBUG
printf("%d flash chips found. Total nand_chip size: %ld MB\n",
nand->numchips, nand->totlen >> 20);
#endif
}
//********************************探测nandflssh******************************
unsigned long nand_probe(unsigned long physadr)
{
struct nand_chip *nand = NULL;
int i = 0, ChipID = 1;
#ifdef CONFIG_MTD_NAND_ECC_JFFS2
oob_config.ecc_pos[0] = NAND_JFFS2_OOB_ECCPOS0;
oob_config.ecc_pos[1] = NAND_JFFS2_OOB_ECCPOS1;
oob_config.ecc_pos[2] = NAND_JFFS2_OOB_ECCPOS2;
oob_config.ecc_pos[3] = NAND_JFFS2_OOB_ECCPOS3;
oob_config.ecc_pos[4] = NAND_JFFS2_OOB_ECCPOS4;
oob_config.ecc_pos[5] = NAND_JFFS2_OOB_ECCPOS5;
oob_config.eccvalid_pos = 4;
#else
oob_config.ecc_pos[0] = NAND_NOOB_ECCPOS0;
oob_config.ecc_pos[1] = NAND_NOOB_ECCPOS1;
oob_config.ecc_pos[2] = NAND_NOOB_ECCPOS2;
oob_config.ecc_pos[3] = NAND_NOOB_ECCPOS3;
oob_config.ecc_pos[4] = NAND_NOOB_ECCPOS4;
oob_config.ecc_pos[5] = NAND_NOOB_ECCPOS5;
oob_config.eccvalid_pos = NAND_NOOB_ECCVPOS;
#endif
oob_config.badblock_pos = 5;
for (i=0; i<CFG_MAX_NAND_DEVICE; i++)
{
if (nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN) //这个是成立的
{
nand = &nand_dev_desc[i];
break;
}
}
if (!nand) //这里不成立
return (0);
memset((char *)nand, 0, sizeof(struct nand_chip)); //将nand结构用0来填充
nand->IO_ADDR = physadr; //放的是基地址
nand->cache_page = -1; /* init the cache page */
NanD_ScanChips(nand);
if (nand->totlen == 0) //这里不成立
{
/* no chips found, clean up and quit */
memset((char *)nand, 0, sizeof(struct nand_chip));
nand->ChipID = NAND_ChipID_UNKNOWN;
return (0);
}
nand->ChipID = ChipID;
if (curr_device == -1) //成立
curr_device = i; //当前设备为1
nand->data_buf = malloc (nand->oobblock + nand->oobsize);//512+16
if (!nand->data_buf) {
puts ("Cannot allocate memory for data structures.\n");
return (0);
}
return (nand->totlen); //返回nandflash的大小(字节)
}
//////////////////////////////////////////////////////来自cmd_nand中的一部分,只有定义了CFG_NAND_LEGACY后才有/////////////////////////////////////////////////////////////////////
#include <command.h>
#include <malloc.h>
#include <asm/io.h>
#include <watchdog.h>
# define SHOW_BOOT_PROGRESS(arg)
#if (CONFIG_COMMANDS & CFG_CMD_NAND)
#include <linux/mtd/nand_legacy.h>
#if 0
#include <linux/mtd/nand_ids.h>
#include <jffs2/jffs2.h>
#endif
#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
#undef NAND_DEBUG
#undef PSYCHO_DEBUG
/* ****************** WARNING *********************
* When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
* erase (or at least attempt to erase) blocks that are marked
* bad. This can be very handy if you are _sure_ that the block
* is OK, say because you marked a good block bad to test bad
* block handling and you are done testing, or if you have
* accidentally marked blocks bad.
*
* Erasing factory marked bad blocks is a _bad_ idea. If the
* erase succeeds there is no reliable way to find them again,
* and attempting to program or erase bad blocks can affect
* the data in _other_ (good) blocks.
*/
#define ALLOW_ERASE_BAD_DEBUG 0
#define CONFIG_MTD_NAND_ECC /* enable ECC */
#define CONFIG_MTD_NAND_ECC_JFFS2
/* bits for nand_legacy_rw() `cmd'; or together as needed */
#define NANDRW_READ 0x01
#define NANDRW_WRITE 0x00
#define NANDRW_JFFS2 0x02
#define NANDRW_JFFS2_SKIP 0x04
/*
* Imports from nand_legacy.c
*/
extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
extern int curr_device;
extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
size_t len, int clean);
extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
size_t len, size_t *retlen, u_char *buf);
extern void nand_print(struct nand_chip *nand);
extern void nand_print_bad(struct nand_chip *nand);
extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
size_t len, size_t *retlen, u_char *buf);
extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
size_t len, size_t *retlen, const u_char *buf);
//打印nandflash信息
void nand_print(struct nand_chip *nand)
{
if (nand->numchips > 1) //这里不执行
{
printf("%s at 0x%lx,\n"
"\t %d chips %s, size %d MB, \n"
"\t total size %ld MB, sector size %ld kB\n",
nand->name, nand->IO_ADDR, nand->numchips,
nand->chips_name, 1 << (nand->chipshift - 20),
nand->totlen >> 20, nand->erasesize >> 10);
}
else //这里执行
{
printf("%s at 0x%lx (", nand->chips_name, nand->IO_ADDR);
print_size(nand->totlen, ", ");
print_size(nand->erasesize, " sector)\n");
}
}
//检测是否是坏块
/* read from the 16 bytes of oob data that correspond to a 512 byte
* page or 2 256-byte pages.
*/
//NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
static int NanD_Address(struct nand_chip *nand, int numbytes, unsigned long ofs)
{
unsigned long nandptr;
int i;
nandptr = nand->IO_ADDR;
/* Assert the ALE (Address Latch Enable) line to the flash chip */
NAND_CTL_SETALE(nandptr);
/* Send the address */
/* Devices with 256-byte page are addressed as:
* Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
* there is no device on the market with page256
* and more than 24 bits.
* Devices with 512-byte page are addressed as:
* Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
* 25-31 is sent only if the chip support it.
* bit 8 changes the read command to be sent
* (NAND_CMD_READ0 or NAND_CMD_READ1).
*/
///////////////////////////////只有在块擦除的时候地址是3个字节其他都是四个字节/////////////////////////////////////////////
if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE)//注意点:如果是只按页方式写则要少一个周期,按页方式写的时候这里不执行
WRITE_NAND_ADDRESS(ofs, nandptr); //由于该宏只接受最低8位,即只接受最低8位为0x00
ofs = ofs >> nand->page_shift;//得到页地址
if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) //如果只按照字节方式写这里不执行
{
for (i = 0; i < nand->pageadrlen; i++, ofs = ofs >> 8) {
WRITE_NAND_ADDRESS(ofs, nandptr);
}
}
/* Lower the ALE line */
NAND_CTL_CLRALE(nandptr);
/* Wait for the chip to respond */
return NanD_WaitReady(nand, 1);
}
nand_read_oob(nand, page0 + badpos, 1, &retlen, (unsigned char *)&oob_data);
int nand_read_oob(struct nand_chip* nand, size_t ofs, size_t len,size_t * retlen, u_char * buf)
{
int len256 = 0;
struct Nand *mychip;
int ret = 0;
mychip = &nand->chips[ofs >> nand->chipshift];
/* update address for 2M x 8bit devices. OOB starts on the second */
/* page to maintain compatibility with nand_read_ecc. */
if (nand->page256) //不执行
{
if (!(ofs & 0x8))
ofs += 0x100;
else
ofs -= 0x8;
}
NAND_ENABLE_CE(nand); /* set pin low */
NanD_Command(nand, NAND_CMD_READOOB); //0x50,读OOB数据
if (nand->bus16) //不执行
{
NanD_Address(nand, ADDR_COLUMN_PAGE,
((ofs >> nand->page_shift) << nand->page_shift) +
((ofs & (nand->oobblock - 1)) >> 1));
}
else //执行
{
NanD_Address(nand, ADDR_COLUMN_PAGE, ofs); //写入oob地址分四次写
}
/* treat crossing 8-byte OOB data for 2M x 8bit devices */
/* Note: datasheet says it should automaticaly wrap to the */
/* next OOB block, but it didn't work here. mf. */
if (nand->page256 && ofs + len > (ofs | 0x7) + 1) //不执行
{
len256 = (ofs | 0x7) + 1 - ofs;
NanD_ReadBuf(nand, buf, len256);
NanD_Command(nand, NAND_CMD_READOOB);
NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
}
NanD_ReadBuf(nand, &buf[len256], len - len256);//从当前地址读出len - len256字节的数据
*retlen = len;
/* Reading the full OOB data drops us off of the end of the page,
* causing the flash device to go into busy mode, so we need
* to wait until ready 11.4.1 and Toshiba TC58256FT nands */
ret = NanD_WaitReady(nand, 1);
NAND_DISABLE_CE(nand); /* set pin high */
return ret;
}
/* print bad blocks in NAND flash */
void nand_print_bad(struct nand_chip* nand)
{
unsigned long pos;
for (pos = 0; pos < nand->totlen; pos += nand->erasesize)
{
if (check_block(nand, pos))
printf(" 0x%8.8lx\n", pos);
}
puts("\n");
}
static int check_block (struct nand_chip *nand, unsigned long pos)
{
size_t retlen;
uint8_t oob_data;
uint16_t oob_data16[6];
int page0 = pos & (-nand->erasesize);
int page1 = page0 + nand->oobblock;
int badpos = oob_config.badblock_pos; //oob_config.badblock_pos = 5;在probe在给了值了
if (pos >= nand->totlen)
return 1;
if (badpos < 0)
return 0; /* no way to check, assume OK */
if (nand->bus16) //不执行
{
if (nand_read_oob(nand, (page0 + 0), 12, &retlen, (uint8_t *)oob_data16)
|| (oob_data16[2] & 0xff00) != 0xff00)
return 1;
if (nand_read_oob(nand, (page1 + 0), 12, &retlen, (uint8_t *)oob_data16)
|| (oob_data16[2] & 0xff00) != 0xff00)
return 1;
}
else //执行
{
/* Note - bad block marker can be on first or second page */
if (nand_read_oob(nand, page0 + badpos, 1, &retlen, (unsigned char *)&oob_data)|| oob_data != 0xff|| nand_read_oob (nand, page1 + badpos, 1, &retlen, (unsigned char *)&oob_data)|| oob_data != 0xff)
return 1;
}
return 0;
}
ret = nand_legacy_erase (nand, off, size, 1);
int nand_legacy_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean)
{
/* This is defined as a structure so it will work on any system
* using native endian jffs2 (the default).
*/
static struct jffs2_unknown_node clean_marker =
{
JFFS2_MAGIC_BITMASK,
JFFS2_NODETYPE_CLEANMARKER,
8 /* 8 bytes in this node */
};
unsigned long nandptr;
struct Nand *mychip;
int ret = 0;
if (ofs & (nand->erasesize-1) || len & (nand->erasesize-1)) //ofs和len 必须为最低9位为0否则要抱错
{
printf ("Offset and size must be sector aligned, erasesize = %d\n",
(int) nand->erasesize);
return -1;
}
nandptr = nand->IO_ADDR;
/* Select the NAND device */
NAND_ENABLE_CE(nand); /* set pin low */
/* Check the WP bit */
NanD_Command(nand, NAND_CMD_STATUS);
if (!(READ_NAND(nand->IO_ADDR) & 0x80))
{
printf ("nand_write_ecc: Device is write protected!!!\n");
ret = -1;
goto out;
}
/* Check the WP bit */
NanD_Command(nand, NAND_CMD_STATUS);//0x70
if (!(READ_NAND(nand->IO_ADDR) & 0x80))
{
printf ("%s: Device is write protected!!!\n", __FUNCTION__);
ret = -1;
goto out;
}
/* FIXME: Do nand in the background. Use timers or schedule_task() */
while(len)
{
/*mychip = &nand->chips[shr(ofs, nand->chipshift)];*/
mychip = &nand->chips[ofs >> nand->chipshift];
/* always check for bad block first, genuine bad blocks
* should _never_ be erased.
*/
if (ALLOW_ERASE_BAD_DEBUG || !check_block(nand, ofs)) //check_block如果成功表明这个块是好的返回为0
{
/* Select the NAND device */
NAND_ENABLE_CE(nand); /* set pin low */
NanD_Command(nand, NAND_CMD_ERASE1);//0x60
NanD_Address(nand, ADDR_PAGE, ofs); //地址
NanD_Command(nand, NAND_CMD_ERASE2);//0xd0
NanD_Command(nand, NAND_CMD_STATUS);
if (READ_NAND(nandptr) & 1)
{
printf ("%s: Error erasing at 0x%lx\n",
__FUNCTION__, (long)ofs);
/* There was an error */
ret = -1;
goto out;
}
if (clean)
{
int n; /* return value not used */
int p, l;
/* clean marker position and size depend
* on the page size, since 256 byte pages
* only have 8 bytes of oob data
*/
if (nand->page256) //不执行
{
p = NAND_JFFS2_OOB8_FSDAPOS;
l = NAND_JFFS2_OOB8_FSDALEN;
}
else ///执行
{
p = NAND_JFFS2_OOB16_FSDAPOS;
l = NAND_JFFS2_OOB16_FSDALEN;
}
ret = nand_write_oob(nand, ofs + p, l, (size_t *)&n,
(u_char *)&clean_marker);
/* quit here if write failed */
if (ret)
goto out;
}
}
ofs += nand->erasesize;
len -= nand->erasesize;
}
out:
/* De-select the NAND device */
NAND_DISABLE_CE(nand); /* set pin high */
return ret;
}
////////////////////////////////关键函数////////////////////////////////////
int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
{
int rcode = 0;
switch (argc) //参数的个数
{
case 0:
case 1:
printf ("Usage:\n%s\n", cmdtp->usage);
return 1;
case 2: //如果参数的个数为2
if (strcmp (argv[1], "info") == 0) //如果第二个参数为info则打印出nandflash信息
{
int i;
putc ('\n');
for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i)
{
if (nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN)
continue; /* list only known devices */
printf ("Device %d: ", i);
nand_print (&nand_dev_desc[i]);
}
return 0;
}
else if (strcmp (argv[1], "device") == 0) //如果第二个参数为device也打印出nandflash信息
{
if ((curr_device < 0)|| (curr_device >= CFG_MAX_NAND_DEVICE))
{
puts ("\nno devices available\n");
return 1;
}
print
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -