⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nand_base.c

📁 项目源码察看工具---- MTD工具
💻 C
📖 第 1 页 / 共 5 页
字号:
	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);			oob = 0;			/* 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_device(mtd);	return ret;}/** * nand_write_oob - [MTD Interface] 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 * * NAND write out-of-band */static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * oob_buf){	int column, page, status, ret = -EIO, chipnr, eccsteps; 	struct nand_chip *this = mtd->priv;	DEBUG (MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n", __FUNCTION__, (unsigned int) to, (int) len);	/* Shift to get page */	page = (int) (to >> this->page_shift);	chipnr = (int) (to >> this->chip_shift);	/* 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, "%s: Attempt to write past end of page\n", __FUNCTION__);		return -EINVAL;	}	/* Grab the lock and see if the device is available */	nand_get_device (this, mtd, FL_WRITING);	/* 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;	/* Invalidate the page cache, if we write to the cached page */	if (page == this->pagebuf)		this->pagebuf = -1;	if (this->eccmode == NAND_ECC_SOFT || this->eccmode == NAND_ECC_NONE) {		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, oob_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, oob_buf, len);		}	} else {		int i = 0, j = 0;		int fflen = 0, old_fflen = 0, ooblen = 0;		/* Write out desired data */		this->cmdfunc (mtd, NAND_CMD_SEQIN, 0, page & this->pagemask);		eccsteps = this->eccsteps;		for (j = 0; this->layout[j].length; j++) {			switch (this->layout[j].type) {			case ITEM_TYPE_DATA:				if (this->options & NAND_COMPLEX_OOB_WRITE) {					this->enable_hwecc(mtd, NAND_ECC_WRITE);					this->write_buf(mtd, ffchars, this->layout[j].length);					fflen += this->layout[j].length;				} else {					if (old_fflen < fflen) {						this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1);						status = this->waitfunc (mtd, this, FL_WRITING);						if (status & NAND_STATUS_FAIL) {							DEBUG (MTD_DEBUG_LEVEL0, "%s: Failed write, page 0x%08x\n", __FUNCTION__, page);							ret = -EIO;							goto out;						}					}					fflen += this->layout[j].length;					if (this->options & NAND_BUSWIDTH_16 && (fflen + ooblen) & 1)						this->cmdfunc (mtd, NAND_CMD_SEQIN, fflen + ooblen - 1, page & this->pagemask);					else						this->cmdfunc (mtd, NAND_CMD_SEQIN, fflen + ooblen, page & this->pagemask);					old_fflen = fflen;				}				break;			case ITEM_TYPE_ECC:			case ITEM_TYPE_OOB:				if (this->layout[j].type == ITEM_TYPE_ECC)					this->enable_hwecc(mtd, NAND_ECC_WRITESYN);				else					this->enable_hwecc(mtd, NAND_ECC_WRITEOOB);				i = min_t(int, column, this->layout[j].length);				if (i) {					if (this->options & NAND_BUSWIDTH_16 && i & 1)						i--;					if (i == 0) {						this->write_word(mtd, cpu_to_le16((oob_buf[0] << 8) | 0xff));						i++;						ooblen++;					} else						this->write_buf(mtd, ffchars, i);				}				column -= i;				fflen += i;				i = min_t(int, len + column - ooblen, this->layout[j].length - i);				if (i) {					if (column) {						this->write_word(mtd, cpu_to_le16((oob_buf[0] << 8) | 0xff));						i--;						ooblen++;					}					if (i & 1)						i--;					this->write_buf(mtd, &oob_buf[ooblen], i);				}				ooblen += i;				if (ooblen == len - 1) {					this->write_word(mtd, cpu_to_le16(oob_buf[ooblen]) | 0xff00);					ooblen += 2;				}				if (ooblen >= len) {					if (NAND_MUST_PAD(this))						this->write_buf(mtd, ffchars, mtd->oobsize + mtd->oobblock - fflen - ooblen);					goto finish;				}				break;			}		}	}finish:	/* 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 & NAND_STATUS_FAIL) {		DEBUG (MTD_DEBUG_LEVEL0, "%s: Failed write, page 0x%08x\n", __FUNCTION__, page);		ret = -EIO;		goto out;	}	/* Return happy */	*retlen = len;#ifdef CONFIG_MTD_NAND_VERIFY_WRITE	if (this->eccmode == NAND_ECC_SOFT || this->eccmode == NAND_ECC_NONE) {		/* Send command to read back the data */		this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask);		if (this->verify_buf(mtd, oob_buf, len)) {			DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page);			ret = -EIO;			goto out;		}	}#warning "Verify for OOB data in HW ECC case is NOT YET implemented"#endif	ret = 0;out:	/* Deselect and wake up anyone waiting on the device */	nand_release_device(mtd);	return ret;}/** * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc * @mtd:	MTD device structure * @vecs:	the iovectors to write * @count:	number of vectors * @to:		offset to write to * @retlen:	pointer to variable to store the number of written bytes * * NAND write with kvec. This just calls the ecc function */static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,		loff_t to, size_t * retlen){	return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL));}/** * nand_writev_ecc - [MTD Interface] write with iovec with ecc * @mtd:	MTD device structure * @vecs:	the iovectors to write * @count:	number of vectors * @to:		offset to write to * @retlen:	pointer to variable to store the number of written bytes * @eccbuf:	filesystem supplied oob data buffer * @oobsel:	oob selection structure * * NAND write with iovec with ecc */static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,		loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel){	int i, page, len, total_len, ret = -EIO, written = 0, chipnr;	int oob, numpages, autoplace = 0, startpage;	struct nand_chip *this = mtd->priv;	int	ppblock = (1 << (this->phys_erase_shift - this->page_shift));	u_char *oobbuf, *bufstart;	/* Preset written len for early exit */	*retlen = 0;	/* Calculate total length of data */	total_len = 0;	for (i = 0; i < count; i++)		total_len += (int) vecs[i].iov_len;	DEBUG (MTD_DEBUG_LEVEL3,	       "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);	/* Do not allow write past end of page */	if ((to + total_len) > mtd->size) {		DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");		return -EINVAL;	}	/* reject writes, which are not page aligned */	if (NOTALIGNED (to) || NOTALIGNED(total_len)) {		printk (KERN_NOTICE "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_device (this, mtd, FL_WRITING);	/* Get the current chip-nr */	chipnr = (int) (to >> this->chip_shift);	/* 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;	}	if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR)		autoplace = 1;	/* Setup start page */	page = (int) (to >> this->page_shift);	/* Invalidate the page cache, if we write to the cached page */	if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift))		this->pagebuf = -1;	startpage = page & this->pagemask;	/* Loop until all kvec' data has been written */	len = 0;	while (count) {		/* If the given tuple is >= pagesize then		 * write it out from the iov		 */		if ((vecs->iov_len - len) >= mtd->oobblock) {			/* Calc number of pages we can write			 * out of this iov in one go */			numpages = (vecs->iov_len - len) >> this->page_shift;			/* Do not cross block boundaries */			numpages = min (ppblock - (startpage & (ppblock - 1)), numpages);			oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);			bufstart = (u_char *)vecs->iov_base;			bufstart += len;			this->data_poi = bufstart;			oob = 0;			for (i = 1; i <= numpages; i++) {				/* Write one page. If this is the last page to write				 * then use the real pageprogram command, else select				 * cached programming if supported by the chip.				 */				ret = nand_write_page (mtd, this, page & this->pagemask,					&oobbuf[oob], oobsel, i != numpages);				if (ret)					goto out;				this->data_poi += mtd->oobblock;				len += mtd->oobblock;				oob += mtd->oobsize;				page++;			}			/* Check, if we have to switch to the next tuple */			if (len >= (int) vecs->iov_len) {				vecs++;				len = 0;				count--;			}		} else {			/* We must use the internal buffer, read data out of each			 * tuple until we have a full page to write			 */			int cnt = 0;			while (cnt < mtd->oobblock) {				if (vecs->iov_base != NULL && vecs->iov_len)					this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];				/* Check, if we have to switch to the next tuple */				if (len >= (int) vecs->iov_len) {					vecs++;					len = 0;					count--;				}			}			this->pagebuf = page;			this->data_poi = this->data_buf;			bufstart = this->data_poi;			numpages = 1;			oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages);			ret = nand_write_page (mtd, this, page & this->pagemask,				oobbuf, oobsel, 0);			if (ret)				goto out;			page++;		}		this->data_poi = bufstart;		ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0);		if (ret)			goto out;		written += mtd->oobblock * numpages;		/* All done ? */		if (!count)			break;		startpage = page & this->pagemask;		/* Check, if we cross a chip boundary */		if (!startpage) {			chipnr++;			this->select_chip(mtd, -1);			this->select_chip(mtd, chipnr);		}	}	ret = 0;out:	/* Deselect and wake up anyone waiting on the device */	nand_release_device(mtd);	*retlen = written;	return ret;}/** * single_erease_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -