cfi_cmdset_0001.c

来自「linux-2.4.29操作系统的源码」· C语言 代码 · 共 1,160 行 · 第 1/3 页

C
1,160
字号
			case FL_CFI_QUERY:	case FL_JEDEC_QUERY:		cfi_write(map, CMD(0x70), adr);		chip->state = FL_STATUS;	case FL_STATUS:		status = cfi_read(map, adr);		if ((status & status_OK) == status_OK)			break;				/* Urgh. Chip not yet ready to talk to us. */		if (time_after(jiffies, timeo)) {			spin_unlock(chip->mutex);			printk(KERN_ERR "waiting for chip to be ready timed out in read\n");			return -EIO;		}		/* Latency issues. Drop the lock, wait a while and retry */		spin_unlock(chip->mutex);		cfi_udelay(1);		goto retry;	case FL_ERASING:		if (!extp || 		    !((extp->FeatureSupport & 2) && (extp->SuspendCmdSupport & 1)))			goto sleep; /* We don't support erase suspend */				cfi_write (map, CMD(0xb0), adr);		/* If the flash has finished erasing, then 'erase suspend'		 * appears to make some (28F320) flash devices switch to		 * 'read' mode.  Make sure that we switch to 'read status'		 * mode so we get the right data. --rmk		 */		cfi_write(map, CMD(0x70), adr);		chip->oldstate = FL_ERASING;		chip->state = FL_ERASE_SUSPENDING;		for (;;) {			status = cfi_read(map, adr);			if ((status & status_OK) == status_OK)				break;						if (time_after(jiffies, timeo)) {				/* Urgh */				cfi_write(map, CMD(0xd0), adr);				/* make sure we're in 'read status' mode */				cfi_write(map, CMD(0x70), adr);				chip->state = FL_ERASING;				spin_unlock(chip->mutex);				printk(KERN_ERR "Chip not ready after erase "				       "suspended: status = 0x%x\n", status);				return -EIO;			}						spin_unlock(chip->mutex);			cfi_udelay(1);			spin_lock(chip->mutex);		}		suspended = 1;		chip->state = FL_STATUS;		break;	default:	sleep:		/* Stick ourselves on a wait queue to be woken when		   someone changes the status */		set_current_state(TASK_UNINTERRUPTIBLE);		add_wait_queue(&chip->wq, &wait);		spin_unlock(chip->mutex);		schedule();		remove_wait_queue(&chip->wq, &wait);		timeo = jiffies + HZ;		goto retry;	}	ENABLE_VPP(map);	cfi_write(map, CMD(0x40), adr);	cfi_write(map, datum, adr);	chip->state = FL_WRITING;	spin_unlock(chip->mutex);	cfi_udelay(chip->word_write_time);	spin_lock(chip->mutex);	timeo = jiffies + (HZ/2);	z = 0;	for (;;) {		if (chip->state != FL_WRITING) {			/* Someone's suspended the write. Sleep */			set_current_state(TASK_UNINTERRUPTIBLE);			add_wait_queue(&chip->wq, &wait);			spin_unlock(chip->mutex);			schedule();			remove_wait_queue(&chip->wq, &wait);			timeo = jiffies + (HZ / 2); /* FIXME */			spin_lock(chip->mutex);			continue;		}		status = cfi_read(map, adr);		if ((status & status_OK) == status_OK)			break;				/* OK Still waiting */		if (time_after(jiffies, timeo)) {			chip->state = FL_STATUS;			DISABLE_VPP(map);			printk(KERN_ERR "waiting for chip to be ready timed out in word write\n");			ret = -EIO;			goto out;		}		/* Latency issues. Drop the lock, wait a while and retry */		spin_unlock(chip->mutex);		z++;		cfi_udelay(1);		spin_lock(chip->mutex);	}	if (!z) {		chip->word_write_time--;		if (!chip->word_write_time)			chip->word_write_time++;	}	if (z > 1) 		chip->word_write_time++;	/* Done and happy. */	chip->state = FL_STATUS;	/* check for lock bit */	if (status & CMD(0x02)) {		/* clear status */		cfi_write(map, CMD(0x50), adr);		/* put back into read status register mode */		cfi_write(map, CMD(0x70), adr);		ret = -EROFS;		goto out;	} out:	if (suspended) {		chip->state = chip->oldstate;		/* What if one interleaved chip has finished and the 		   other hasn't? The old code would leave the finished		   one in READY mode. That's bad, and caused -EROFS 		   errors to be returned from do_erase_oneblock because		   that's the only bit it checked for at the time.		   As the state machine appears to explicitly allow 		   sending the 0x70 (Read Status) command to an erasing		   chip and expecting it to be ignored, that's what we 		   do. */		cfi_write(map, CMD(0xd0), adr);		cfi_write(map, CMD(0x70), adr);			} else		DISABLE_VPP(map); /* must not clear the VPP if there is a suspended erase to be resumed */	wake_up(&chip->wq);	spin_unlock(chip->mutex);	return ret;}static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf){	struct map_info *map = mtd->priv;	struct cfi_private *cfi = map->fldrv_priv;	int ret = 0;	int chipnum;	unsigned long ofs;	*retlen = 0;	if (!len)		return 0;	chipnum = to >> cfi->chipshift;	ofs = to  - (chipnum << cfi->chipshift);	/* If it's not bus-aligned, do the first byte write */	if (ofs & (CFIDEV_BUSWIDTH-1)) {		unsigned long bus_ofs = ofs & ~(CFIDEV_BUSWIDTH-1);		int gap = ofs - bus_ofs;		int i = 0, n = 0;		u_char tmp_buf[8];		cfi_word datum;		while (gap--)			tmp_buf[i++] = 0xff;		while (len && i < CFIDEV_BUSWIDTH)			tmp_buf[i++] = buf[n++], len--;		while (i < CFIDEV_BUSWIDTH)			tmp_buf[i++] = 0xff;		if (cfi_buswidth_is_2()) {			datum = *(__u16*)tmp_buf;		} else if (cfi_buswidth_is_4()) {			datum = *(__u32*)tmp_buf;		} else if (cfi_buswidth_is_8()) {			datum = *(__u64*)tmp_buf;		} else {			return -EINVAL;  /* should never happen, but be safe */		}		ret = do_write_oneword(map, &cfi->chips[chipnum],					       bus_ofs, datum);		if (ret) 			return ret;				ofs += n;		buf += n;		(*retlen) += n;		if (ofs >> cfi->chipshift) {			chipnum ++; 			ofs = 0;			if (chipnum == cfi->numchips)				return 0;		}	}		while(len >= CFIDEV_BUSWIDTH) {		cfi_word datum;		if (cfi_buswidth_is_1()) {			datum = *(__u8*)buf;		} else if (cfi_buswidth_is_2()) {			datum = *(__u16*)buf;		} else if (cfi_buswidth_is_4()) {			datum = *(__u32*)buf;		} else if (cfi_buswidth_is_8()) {			datum = *(__u64*)buf;		} else {			return -EINVAL;		}		ret = do_write_oneword(map, &cfi->chips[chipnum],				ofs, datum);		if (ret)			return ret;		ofs += CFIDEV_BUSWIDTH;		buf += CFIDEV_BUSWIDTH;		(*retlen) += CFIDEV_BUSWIDTH;		len -= CFIDEV_BUSWIDTH;		if (ofs >> cfi->chipshift) {			chipnum ++; 			ofs = 0;			if (chipnum == cfi->numchips)				return 0;		}	}	if (len & (CFIDEV_BUSWIDTH-1)) {		int i = 0, n = 0;		u_char tmp_buf[8];		cfi_word datum;		while (len--)			tmp_buf[i++] = buf[n++];		while (i < CFIDEV_BUSWIDTH)			tmp_buf[i++] = 0xff;		if (cfi_buswidth_is_2()) {			datum = *(__u16*)tmp_buf;		} else if (cfi_buswidth_is_4()) {			datum = *(__u32*)tmp_buf;		} else if (cfi_buswidth_is_8()) {			datum = *(__u64*)tmp_buf;		} else {			return -EINVAL;  /* should never happen, but be safe */		}		ret = do_write_oneword(map, &cfi->chips[chipnum],					       ofs, datum);		if (ret) 			return ret;				(*retlen) += n;	}	return 0;}static inline int do_write_buffer(struct map_info *map, struct flchip *chip, 				  unsigned long adr, const u_char *buf, int len){	struct cfi_private *cfi = map->fldrv_priv;	struct cfi_pri_intelext *extp = cfi->cmdset_priv;	cfi_word status, status_OK;	unsigned long cmd_adr, timeo;	DECLARE_WAITQUEUE(wait, current);	int wbufsize, z, suspended=0, ret=0;	wbufsize = CFIDEV_INTERLEAVE << cfi->cfiq->MaxBufWriteSize;	adr += chip->start;	cmd_adr = adr & ~(wbufsize-1);		/* Let's determine this according to the interleave only once */	status_OK = CMD(0x80);	timeo = jiffies + HZ; retry:	spin_lock(chip->mutex);	/* Check that the chip's ready to talk to us.	 * Later, we can actually think about interrupting it	 * if it's in FL_ERASING state.	 * Not just yet, though.	 */	switch (chip->state) {	case FL_READY:	case FL_CFI_QUERY:	case FL_JEDEC_QUERY:		cfi_write(map, CMD(0x70), cmd_adr);		chip->state = FL_STATUS;	case FL_STATUS:		status = cfi_read(map, cmd_adr);		if ((status & status_OK) == status_OK)			break;		/* Urgh. Chip not yet ready to talk to us. */		if (time_after(jiffies, timeo)) {			spin_unlock(chip->mutex);			printk(KERN_ERR "waiting for chip to be ready timed out in buffer write\n");			return -EIO;		}		/* Latency issues. Drop the lock, wait a while and retry */		spin_unlock(chip->mutex);		cfi_udelay(1);		goto retry;	case FL_ERASING:		if (!extp || 		    !((extp->FeatureSupport & 2) && (extp->SuspendCmdSupport & 1)))			goto sleep; /* We don't support erase suspend */				cfi_write (map, CMD(0xb0), adr);		/* If the flash has finished erasing, then 'erase suspend'		 * appears to make some (28F320) flash devices switch to		 * 'read' mode.  Make sure that we switch to 'read status'		 * mode so we get the right data. --rmk		 */		cfi_write(map, CMD(0x70), adr);		chip->oldstate = FL_ERASING;		chip->state = FL_ERASE_SUSPENDING;		for (;;) {			status = cfi_read(map, adr);			if ((status & status_OK) == status_OK)				break;						if (time_after(jiffies, timeo)) {				/* Urgh */				cfi_write(map, CMD(0xd0), adr);				/* make sure we're in 'read status' mode */				cfi_write(map, CMD(0x70), adr);				chip->state = FL_ERASING;				spin_unlock(chip->mutex);				printk(KERN_ERR "Chip not ready after erase "				       "suspended: status = 0x%x\n", status);				return -EIO;			}						spin_unlock(chip->mutex);			cfi_udelay(1);			spin_lock(chip->mutex);		}		suspended = 1;		chip->state = FL_STATUS;		break;	default:	sleep:		/* Stick ourselves on a wait queue to be woken when		   someone changes the status */		set_current_state(TASK_UNINTERRUPTIBLE);		add_wait_queue(&chip->wq, &wait);		spin_unlock(chip->mutex);		schedule();		remove_wait_queue(&chip->wq, &wait);		timeo = jiffies + HZ;		goto retry;	}	/* We know we're now in FL_STATUS mode, and 'status' is current */	/* 

⌨️ 快捷键说明

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