📄 nand.c
字号:
} /* check, if we have a fs supplied oob-buffer */ if (oob_buf) { /* without autoplace. Legacy mode used by YAFFS1 */ if (oobsel->useecc != MTD_NANDECC_AUTOPLACE) { oob_data += mtd->oobsize + this->eccsteps * sizeof (int); } else { /* Walk through the autoplace chunks */ for (i = 0, j = 0; j < mtd->oobavail; i++) { int from = oobsel->oobfree[i][0]; int num = oobsel->oobfree[i][1]; memcpy(&oob_buf[oob], &oob_data[from], num); j+= num; } oob += mtd->oobavail; } }readdata: /* Partial page read, transfer data into fs buffer */ if (!aligned) { for (j = col; j < end && read < len; j++) buf[read++] = data_poi[j]; this->pagebuf = realpage; } else read += mtd->oobblock; if (read == len) break; /* For subsequent reads align to page boundary. */ col = 0; /* Increment page address */ realpage++; /* Apply delay or wait for ready/busy pin * Do this before the AUTOINCR check, so no problems * arise if a chip which does auto increment * is marked as NOAUTOINCR by the board driver. */ if (!this->dev_ready) udelay (this->chip_delay); else while (!this->dev_ready(mtd)); page = realpage & this->pagemask; /* Check, if we cross a chip boundary */ if (!page) { chipnr++; this->select_chip(mtd, -1); this->select_chip(mtd, chipnr); } /* Check, if the chip supports auto page increment * or if we have hit a block boundary. */ if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) sndcmd = 1; } /* Deselect and wake up anyone waiting on the device */ nand_release_chip(mtd); /* * Return success, if no ECC failures, else -EIO * fs driver will take care of that, because * retlen == desired len and result == -EIO */ *retlen = read; return ecc_failed ? -EIO : 0;}/* * NAND read out-of-band * * @mtd: MTD device structure * @from: offset to read from * @len: number of bytes to read * @retlen: pointer to variable to store the number of read bytes * @buf: the databuffer to put data */static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf){ int i, col, page, chipnr; int erase_state = 0; struct nand_chip *this = mtd->priv; int blockcheck = (mtd->erasesize >> this->page_shift) - 1; DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len); /* Shift to get page */ page = ((int) from) >> this->page_shift; chipnr = (int)((unsigned long)from / this->chipsize); /* Mask to get column */ col = from & (mtd->oobsize - 1); /* Initialize return length value */ *retlen = 0; /* Do not allow reads past end of device */ if ((from + len) > mtd->size) { DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n"); *retlen = 0; return -EINVAL; } /* Grab the lock and see if the device is available */ nand_get_chip (this, mtd , FL_READING, &erase_state); /* Select the NAND device */ this->select_chip(mtd, chipnr); /* Send the read command */ this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask); /* * Read the data, if we read more than one page * oob data, let the device transfer the data ! */ i = 0; while (i < len) { int thislen = (mtd->oobsize - col) & (mtd->oobsize - 1); if (!thislen) thislen = mtd->oobsize; thislen = min_t(int, thislen, len); this->read_buf(mtd, &buf[i], thislen); i += thislen; col += thislen; /* Read more ? */ if (i < len) { page++; /* Apply delay or wait for ready/busy pin * Do this before the AUTOINCR check, so no problems * arise if a chip which does auto increment * is marked as NOAUTOINCR by the board driver. */ if (!this->dev_ready) udelay (this->chip_delay); else while (!this->dev_ready(mtd)); /* Check, if we cross a chip boundary */ if (!(page & this->pagemask)) { chipnr++; this->select_chip(mtd, -1); this->select_chip(mtd, chipnr); } /* Check, if the chip supports auto page increment * or if we have hit a block boundary. */ if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) { /* For subsequent page reads set offset to 0 */ this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page); } } } /* Deselect and wake up anyone waiting on the device */ nand_release_chip(mtd); /* Return happy */ *retlen = len; return 0;}/* Prepare the out of band buffer * * Return: * 1. Filesystem buffer available and autoplacement is off, * return filesystem buffer * 2. No filesystem buffer or autoplace is off, return internal * buffer * 3. Filesystem buffer is given and autoplace selected * put data from fs buffer into internal buffer and * retrun internal buffer * * Note: The internal buffer is filled with 0xff. This must * be done only once, when no autoplacement happens * Autoplacement sets the buffer dirty flag, which * forces the 0xff fill before using the buffer again. * * @mtd: MTD device structure * @fsbuf: buffer given by fs driver * @oobsel: out of band selection structre * @autoplace: 1 = place given buffer into the oob bytes * @numpages: number of pages to prepare */static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel, int autoplace, int numpages){ struct nand_chip *this = mtd->priv; int i, len, ofs; /* Zero copy fs supplied buffer */ if (fsbuf && !autoplace) return fsbuf; /* Check, if the buffer must be filled with ff again */ if (this->oobdirty) { memset (this->oob_buf, 0xff, mtd->oobsize * (mtd->erasesize / mtd->oobblock)); this->oobdirty = 0; } /* If we have no autoplacement or no fs buffer use the internal one */ if (!autoplace || !fsbuf) return this->oob_buf; /* Walk through the pages and place the data */ this->oobdirty = 1; ofs = 0; while (numpages--) { for (i = 0, len = 0; len < mtd->oobavail; i++) { int to = ofs + oobsel->oobfree[i][0]; int num = oobsel->oobfree[i][1]; memcpy (&this->oob_buf[to], fsbuf, num); len += num; fsbuf += num; } ofs += mtd->oobavail; } return this->oob_buf;}#define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0/* * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL * * @mtd: MTD device structure * @to: offset to write to * @len: number of bytes to write * @retlen: pointer to variable to store the number of written bytes * @buf: the data to write*/static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf){ return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));} /* * NAND write with ECC * * @mtd: MTD device structure * @to: offset to write to * @len: number of bytes to write * @retlen: pointer to variable to store the number of written bytes * @buf: the data to write * @eccbuf: filesystem supplied oob data buffer * @oobsel: oob selection structure */static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel){ int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr; int autoplace = 0, numpages, totalpages; struct nand_chip *this = mtd->priv; u_char *oobbuf, *bufstart; int ppblock = mtd->erasesize >> this->page_shift; DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len); /* Initialize retlen, in case of early exit */ *retlen = 0; /* Do not allow write past end of device */ if ((to + len) > mtd->size) { DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n"); return -EINVAL; } /* reject writes, which are not page aligned */ if (NOTALIGNED (to) || NOTALIGNED(len)) { printk ("nand_write_ecc: Attempt to write not page aligned data\n"); return -EINVAL; } /* Grab the lock and see if the device is available */ nand_get_chip (this, mtd, FL_WRITING, NULL); /* Calculate chipnr */ chipnr = (int)((unsigned long) to / this->chipsize); /* Select the NAND device */ this->select_chip(mtd, chipnr); /* Check, if it is write protected */ if (nand_check_wp(mtd)) goto out; /* if oobsel is NULL, use chip defaults */ if (oobsel == NULL) oobsel = &mtd->oobinfo; /* Autoplace of oob data ? Use the default placement scheme */ if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) { oobsel = this->autooob; autoplace = 1; } /* Setup variables and oob buffer */ page = ((int) to) >> this->page_shift; /* Set it relative to chip */ page &= this->pagemask; startpage = page; totalpages = len >> this->page_shift; /* Calc number of pages we can write in one go */ numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages); oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages); bufstart = (u_char *)buf; /* Loop until all data is written */ while (written < len) { this->data_poi = (u_char*) &buf[written]; /* Write one page. If this is the last page to write * or the last page in this block, then use the * real pageprogram command, else select cached programming * if supported by the chip. */ ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0)); if (ret) { DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret); goto out; } /* Next oob page */ oob += mtd->oobsize; /* Update written bytes count */ written += mtd->oobblock; if (written == len) goto cmp; /* Increment page address */ page++; /* Have we hit a block boundary ? Then we have to verify and * if verify is ok, we have to setup the oob buffer for * the next pages. */ if (!(page & (ppblock - 1))){ int ofs; this->data_poi = bufstart; ret = nand_verify_pages (mtd, this, startpage, page - startpage, oobbuf, oobsel, chipnr, (eccbuf != NULL)); if (ret) { DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret); goto out; } *retlen = written; ofs = autoplace ? mtd->oobavail : mtd->oobsize; if (eccbuf) eccbuf += (page - startpage) * ofs; totalpages -= page - startpage; numpages = min (totalpages, ppblock); page &= this->pagemask; startpage = page; oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages); /* Check, if we cross a chip boundary */ if (!page) { chipnr++; this->select_chip(mtd, -1); this->select_chip(mtd, chipnr); } } } /* Verify the remaining pages */cmp: this->data_poi = bufstart; ret = nand_verify_pages (mtd, this, startpage, totalpages, oobbuf, oobsel, chipnr, (eccbuf != NULL)); if (!ret) *retlen = written; else DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret);out: /* Deselect and wake up anyone waiting on the device */ nand_release_chip(mtd); return ret;}/* * NAND write out-of-band * * @mtd: MTD device structure * @to: offset to write to * @len: number of bytes to write * @retlen: pointer to variable to store the number of written bytes * @buf: the data to write */static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf){ int column, page, status, ret = -EIO, chipnr; struct nand_chip *this = mtd->priv; DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len); /* Shift to get page */ page = ((int) to) >> this->page_shift; chipnr = (int)((unsigned long)to / this->chipsize); /* Mask to get column */ column = to & (mtd->oobsize - 1); /* Initialize return length value */ *retlen = 0; /* Do not allow write past end of page */ if ((column + len) > mtd->oobsize) { DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n"); return -EINVAL; } /* Grab the lock and see if the device is available */ nand_get_chip (this, mtd, FL_WRITING, NULL); /* Select the NAND device */ this->select_chip(mtd, chipnr); /* Reset the chip. Some chips (like the Toshiba TC5832DC found in one of my DiskOnChip 2000 test units) will clear the whole data page too if we don't do this. I have no clue why, but I seem to have 'fixed' it in the doc2000 driver in August 1999. dwmw2. */ this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); /* Check, if it is write protected */ if (nand_check_wp(mtd)) goto out; if (NAND_MUST_PAD(this)) { /* Write out desired data */ this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask); /* prepad 0xff for partial programming */ this->write_buf(mtd, ffchars, column); /* write data */ this->write_buf(mtd, buf, len); /* postpad 0xff for partial programming */ this->write_buf(mtd, ffchars, mtd->oobsize - (len+column)); } else { /* Write out desired data */ this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask); /* write data */ this->write_buf(mtd, buf, len); } /* Send command to program the OOB data */ this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1); status = this->waitfunc (mtd, this, FL_WRITING); /* See if device thinks it succeeded */ if (status & 0x01) { DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page); ret = -EIO; goto out; } /* Return happy */ *retlen = len;#ifdef CONFIG_MTD_NAND_VERIFY_WRITE#ifdef CONFIG_MTD_NAND_MP2520F
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -