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

📄 onenand_base.c

📁 老版本的mtd-snap
💻 C
📖 第 1 页 / 共 3 页
字号:
			}		}		this->command(mtd, ONENAND_CMD_BUFFERRAM, to, mtd->oobblock);		this->write_bufferram(mtd, ONENAND_DATARAM, pbuf, 0, mtd->oobblock);		this->write_bufferram(mtd, ONENAND_SPARERAM, ffchars, 0, mtd->oobsize);		this->command(mtd, ONENAND_CMD_PROG, to, mtd->oobblock);		onenand_update_bufferram(mtd, to, 1);		ret = this->wait(mtd, FL_WRITING);		if (ret) {			DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: write failed %d\n", ret);			goto out;		}		/* Only check verify write turn on */		ret = onenand_verify_page(mtd, (u_char *) pbuf, to, block, page);		if (ret) {			DEBUG(MTD_DEBUG_LEVEL0, "onenand_writev_ecc: verify failed %d\n", ret);			goto out;		}		written += mtd->oobblock;		to += mtd->oobblock;	}out:	/* Deselect and wakt up anyone waiting on the device */	onenand_release_device(mtd);	*retlen = written;	return 0;}/** * onenand_writev - [MTD Interface] compabilty function for onenand_writev_ecc * @param mtd		MTD device structure * @param vecs		the iovectors to write * @param count		number of vectors * @param to		offset to write to * @param retlen	pointer to variable to store the number of written bytes * * OneNAND write with kvec. This just calls the ecc function */static int onenand_writev(struct mtd_info *mtd, const struct kvec *vecs,	unsigned long count, loff_t to, size_t *retlen){	return onenand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);}/** * onenand_erase - [MTD Interface] erase block(s) * @param mtd		MTD device structure * @param instr		erase instruction * * Erase one ore more blocks */static int onenand_erase(struct mtd_info *mtd, struct erase_info *instr){	struct onenand_chip *this = mtd->priv;	unsigned int block_size;	loff_t addr;	int len;	int ret = 0;	DEBUG(MTD_DEBUG_LEVEL3, "onenand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);	block_size = (1 << this->erase_shift);	/* Start address must align on block boundary */	if (unlikely(instr->addr & (block_size - 1))) {		DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Unaligned address\n");		return -EINVAL;	}	/* Length must align on block boundary */	if (unlikely(instr->len & (block_size - 1))) {		DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Length not block aligned\n");		return -EINVAL;	}	/* Do not allow erase past end of device */	if (unlikely((instr->len + instr->addr) > mtd->size)) {		DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Erase past end of device\n");		return -EINVAL;	}	instr->fail_addr = 0xffffffff;	/* Grab the lock and see if the device is available */	onenand_get_device(mtd, FL_ERASING);	/* Loop throught the pages */	len = instr->len;	addr = instr->addr;	instr->state = MTD_ERASING;	while (len) {		/* TODO Check badblock */		this->command(mtd, ONENAND_CMD_ERASE, addr, block_size);		ret = this->wait(mtd, FL_ERASING);		/* Check, if it is write protected */		if (ret) {			if (ret == -EPERM)				DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Device is write protected!!!\n");			else				DEBUG(MTD_DEBUG_LEVEL0, "onenand_erase: Failed erase, block %d\n", (unsigned) (addr >> this->erase_shift));			instr->state = MTD_ERASE_FAILED;			instr->fail_addr = addr;			goto erase_exit;		}		len -= block_size;		addr += block_size;	}	instr->state = MTD_ERASE_DONE;erase_exit:	ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;	/* Do call back function */	if (!ret)		mtd_erase_callback(instr);	/* Deselect and wake up anyone waiting on the device */	onenand_release_device(mtd);	return ret;}/** * onenand_sync - [MTD Interface] sync * @param mtd		MTD device structure * * Sync is actually a wait for chip ready function */static void onenand_sync(struct mtd_info *mtd){	DEBUG(MTD_DEBUG_LEVEL3, "onenand_sync: called\n");	/* Grab the lock and see if the device is available */	onenand_get_device(mtd, FL_SYNCING);	/* Release it and go back */	onenand_release_device(mtd);}/** * onenand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad * @param mtd		MTD device structure * @param ofs		offset relative to mtd start */static int onenand_block_isbad(struct mtd_info *mtd, loff_t ofs){	/*	 * TODO 	 * 1. Bad block table (BBT)	 *   -> using NAND BBT to support JFFS2	 * 2. Bad block management (BBM)	 *   -> bad block replace scheme	 *	 * Currently we do nothing	 */	return 0;}/** * onenand_block_markbad - [MTD Interface] Mark the block at the given offset as bad * @param mtd		MTD device structure * @param ofs		offset relative to mtd start */static int onenand_block_markbad(struct mtd_info *mtd, loff_t ofs){	/* see above */	return 0;}/** * onenand_unlock - [MTD Interface] Unlock block(s) * @param mtd		MTD device structure * @param ofs		offset relative to mtd start * @param len		number of bytes to unlock * * Unlock one or more blocks */static int onenand_unlock(struct mtd_info *mtd, loff_t ofs, size_t len){	struct onenand_chip *this = mtd->priv;	int start, end, block, value, status;	start = ofs >> this->erase_shift;	end = len >> this->erase_shift;	/* Continuous lock scheme */	if (this->options & ONENAND_CONT_LOCK) {		/* Set start block address */		this->write_word(start, this->base + ONENAND_REG_START_BLOCK_ADDRESS);		/* Set end block address */		this->write_word(end - 1, this->base + ONENAND_REG_END_BLOCK_ADDRESS);		/* Write unlock command */		this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);		/* There's no return value */		this->wait(mtd, FL_UNLOCKING);		/* Sanity check */		while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)		    & ONENAND_CTRL_ONGO)			continue;		/* Check lock status */		status = this->read_word(this->base + ONENAND_REG_WP_STATUS);		if (!(status & ONENAND_WP_US))			printk(KERN_ERR "wp status = 0x%x\n", status);		return 0;	}	/* Block lock scheme */	for (block = start; block < end; block++) {		/* Set start block address */		this->write_word(block, this->base + ONENAND_REG_START_BLOCK_ADDRESS);		/* Write unlock command */		this->command(mtd, ONENAND_CMD_UNLOCK, 0, 0);		/* There's no return value */		this->wait(mtd, FL_UNLOCKING);		/* Sanity check */		while (this->read_word(this->base + ONENAND_REG_CTRL_STATUS)		    & ONENAND_CTRL_ONGO)			continue;		/* Set block address for read block status */		value = onenand_block_address(this->device_id, block);		this->write_word(value, this->base + ONENAND_REG_START_ADDRESS1);		/* Check lock status */		status = this->read_word(this->base + ONENAND_REG_WP_STATUS);		if (!(status & ONENAND_WP_US))			printk(KERN_ERR "block = %d, wp status = 0x%x\n", block, status);	}		return 0;}/** * onenand_print_device_info - Print device ID * @param device        device ID * * Print device ID */static void onenand_print_device_info(int device){        int vcc, demuxed, ddp, density;        vcc = device & ONENAND_DEVICE_VCC_MASK;        demuxed = device & ONENAND_DEVICE_IS_DEMUX;        ddp = device & ONENAND_DEVICE_IS_DDP;        density = device >> ONENAND_DEVICE_DENSITY_SHIFT;        printk(KERN_INFO "%sOneNAND%s %dMB %sV 16-bit (0x%02x)\n",                demuxed ? "" : "Muxed ",                ddp ? "(DDP)" : "",                (16 << density),                vcc ? "2.65/3.3" : "1.8",                device);}static const struct onenand_manufacturers onenand_manuf_ids[] = {        {ONENAND_MFR_SAMSUNG, "Samsung"},        {ONENAND_MFR_UNKNOWN, "Unknown"}};/** * onenand_check_maf - Check manufacturer ID * @param manuf         manufacturer ID * * Check manufacturer ID */static int onenand_check_maf(int manuf){        int i;        for (i = 0; onenand_manuf_ids[i].id; i++) {                if (manuf == onenand_manuf_ids[i].id)                        break;        }        printk(KERN_DEBUG "OneNAND Manufacturer: %s\n",                onenand_manuf_ids[i].name);        return (i != ONENAND_MFR_UNKNOWN);}/** * onenand_probe - [OneNAND Interface] Probe the OneNAND device * @param mtd		MTD device structure * * OneNAND detection method: *   Compare the the values from command with ones from register */static int onenand_probe(struct mtd_info *mtd){	struct onenand_chip *this = mtd->priv;	int bram_maf_id, bram_dev_id, maf_id, dev_id;	int version_id;	int density;	/* Send the command for reading device ID from BootRAM */	this->write_word(ONENAND_CMD_READID, this->base + ONENAND_BOOTRAM);	/* Read manufacturer and device IDs from BootRAM */	bram_maf_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x0);	bram_dev_id = this->read_word(this->base + ONENAND_BOOTRAM + 0x2);	/* Check manufacturer ID */	if (onenand_check_maf(bram_maf_id))		return -ENXIO;	/* Reset OneNAND to read default register values */	this->write_word(ONENAND_CMD_RESET, this->base + ONENAND_BOOTRAM);	/* Read manufacturer and device IDs from Register */	maf_id = this->read_word(this->base + ONENAND_REG_MANUFACTURER_ID);	dev_id = this->read_word(this->base + ONENAND_REG_DEVICE_ID);	/* Check OneNAND device */	if (maf_id != bram_maf_id || dev_id != bram_dev_id)		return -ENXIO;	/* Flash device information */	onenand_print_device_info(dev_id);	this->device_id = dev_id;	density = dev_id >> ONENAND_DEVICE_DENSITY_SHIFT;	this->chipsize = (16 << density) << 20;	/* OneNAND page size & block size */	/* The data buffer size is equal to page size */	mtd->oobblock = this->read_word(this->base + ONENAND_REG_DATA_BUFFER_SIZE);	mtd->oobsize = mtd->oobblock >> 5;	/* Pagers per block is always 64 in OneNAND */	mtd->erasesize = mtd->oobblock << 6;	this->erase_shift = ffs(mtd->erasesize) - 1;	this->page_shift = ffs(mtd->oobblock) - 1;	this->ppb_shift = (this->erase_shift - this->page_shift);	this->page_mask = (mtd->erasesize / mtd->oobblock) - 1;	/* REVIST: Multichip handling */	mtd->size = this->chipsize;	/* Version ID */	version_id = this->read_word(this->base + ONENAND_REG_VERSION_ID);	printk(KERN_DEBUG "OneNAND version = 0x%04x\n", version_id);	/* Lock scheme */	if (density <= ONENAND_DEVICE_DENSITY_512Mb &&	    !(version_id >> ONENAND_VERSION_PROCESS_SHIFT)) {		printk(KERN_INFO "Lock scheme is Continues Lock\n");		this->options |= ONENAND_CONT_LOCK;	}		return 0;}/** * onenand_scan - [OneNAND Interface] Scan for the OneNAND device * @param mtd		MTD device structure * @param maxchips	Number of chips to scan for * * This fills out all the not initialized function pointers * with the defaults. * The flash ID is read and the mtd/chip structures are * filled with the appropriate values. */int onenand_scan(struct mtd_info *mtd, int maxchips){	struct onenand_chip *this = mtd->priv;	if (!this->read_word)		this->read_word = onenand_readw;	if (!this->write_word)		this->write_word = onenand_writew;	if (!this->command)		this->command = onenand_command;	if (!this->wait)		this->wait = onenand_wait;	if (!this->read_bufferram)		this->read_bufferram = onenand_read_bufferram;	if (!this->write_bufferram)		this->write_bufferram = onenand_write_bufferram;	if (onenand_probe(mtd))		return -ENXIO;	this->state = FL_READY;	init_waitqueue_head(&this->wq);	spin_lock_init(&this->chip_lock);	switch (mtd->oobsize) {	case 64:		this->autooob = &onenand_oob_64;		break;	case 32:		this->autooob = &onenand_oob_32;		break;	default:		printk(KERN_WARNING "No OOB scheme defined for oobsize %d\n",			mtd->oobsize);		/* To prevent kernel oops */		this->autooob = &onenand_oob_32;		break;	}	memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));		/* Fill in remaining MTD driver data */	mtd->type = MTD_NANDFLASH;	mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;	mtd->ecctype = MTD_ECC_SW;	mtd->erase = onenand_erase;	mtd->point = NULL;	mtd->unpoint = NULL;	mtd->read = onenand_read;	mtd->write = onenand_write;	mtd->read_ecc = onenand_read_ecc;	mtd->write_ecc = onenand_write_ecc;	mtd->read_oob = onenand_read_oob;	mtd->write_oob = onenand_write_oob;	mtd->readv = NULL;	mtd->readv_ecc = NULL;	mtd->writev = onenand_writev;	mtd->writev_ecc = onenand_writev_ecc;	mtd->sync = onenand_sync;	mtd->lock = NULL;	mtd->unlock = onenand_unlock;	mtd->suspend = NULL;	mtd->resume = NULL;	mtd->block_isbad = onenand_block_isbad;	mtd->block_markbad = onenand_block_markbad;	mtd->owner = THIS_MODULE;	/* Unlock whole block */	mtd->unlock(mtd, 0x0, this->chipsize);	return 0;}/** * onenand_release - [OneNAND Interface] Free resources held by the OneNAND device * @param mtd		MTD device structure */void onenand_release(struct mtd_info *mtd){#ifdef CONFIG_MTD_PARTITIONS	/* Deregister partitions */	del_mtd_partitions (mtd);#endif	/* Deregister the device */	del_mtd_device (mtd);}EXPORT_SYMBOL_GPL(onenand_scan);EXPORT_SYMBOL_GPL(onenand_release);MODULE_LICENSE("GPL");MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");MODULE_DESCRIPTION("Generic OneNAND flash driver code");

⌨️ 快捷键说明

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