cfi_cmdset_0002.c

来自「linux嵌入式课程实践中的一个关于声卡驱动程序 。」· C语言 代码 · 共 1,143 行 · 第 1/2 页

C
1,143
字号
	DECLARE_WAITQUEUE(wait, current); retry:	cfi_spin_lock(chip->mutex);	if (chip->state != FL_READY){		set_current_state(TASK_UNINTERRUPTIBLE);		add_wait_queue(&chip->wq, &wait);                		cfi_spin_unlock(chip->mutex);		schedule();		remove_wait_queue(&chip->wq, &wait);#if 0		if(signal_pending(current))			return -EINTR;#endif		timeo = jiffies + HZ;		goto retry;	}		chip->state = FL_ERASING;		/* Handle devices with one erase region, that only implement	 * the chip erase command.	 */	ENABLE_VPP(map);	cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0x10, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	timeo = jiffies + (HZ*20);	adr = cfi->addr_unlock1;	/* Wait for the end of programing/erasure by using the toggle method.	 * As long as there is a programming procedure going on, bit 6 of the last	 * written byte is toggling it's state with each consectuve read.	 * The toggling stops as soon as the procedure is completed.	 *	 * If the process has gone on for too long on the chip bit 5 gets.	 * After bit5 is set you can kill the operation by sending a reset	 * command to the chip.	 */	dq6 = CMD(1<<6);	dq5 = CMD(1<<5);	oldstatus = cfi_read(map, adr);	status = cfi_read(map, adr);	while( ((status & dq6) != (oldstatus & dq6)) && 		((status & dq5) != dq5) &&		!time_after(jiffies, timeo)) {		int wait_reps;		/* an initial short sleep */		cfi_spin_unlock(chip->mutex);		schedule_timeout(HZ/100);		cfi_spin_lock(chip->mutex);				if (chip->state != FL_ERASING) {			/* Someone's suspended the erase. Sleep */			set_current_state(TASK_UNINTERRUPTIBLE);			add_wait_queue(&chip->wq, &wait);						cfi_spin_unlock(chip->mutex);			printk("erase suspended. Sleeping\n");						schedule();			remove_wait_queue(&chip->wq, &wait);#if 0						if (signal_pending(current))				return -EINTR;#endif						timeo = jiffies + (HZ*2); /* FIXME */			cfi_spin_lock(chip->mutex);			continue;		}		/* Busy wait for 1/10 of a milisecond */		for(wait_reps = 0;		    	(wait_reps < 100) &&			((status & dq6) != (oldstatus & dq6)) && 			((status & dq5) != dq5);			wait_reps++) {						/* Latency issues. Drop the lock, wait a while and retry */			cfi_spin_unlock(chip->mutex);						cfi_udelay(1);					cfi_spin_lock(chip->mutex);			oldstatus = cfi_read(map, adr);			status = cfi_read(map, adr);		}		oldstatus = cfi_read(map, adr);		status = cfi_read(map, adr);	}	if ((status & dq6) != (oldstatus & dq6)) {		/* The erasing didn't stop?? */		if ((status & dq5) == dq5) {			/* dq5 is active so we can do a reset and stop the erase */			cfi_write(map, CMD(0xF0), chip->start);		}		chip->state = FL_READY;		wake_up(&chip->wq);		cfi_spin_unlock(chip->mutex);		printk("waiting for erase to complete timed out.");		DISABLE_VPP(map);		return -EIO;	}	DISABLE_VPP(map);	chip->state = FL_READY;	wake_up(&chip->wq);	cfi_spin_unlock(chip->mutex);	return 0;	}static inline int do_erase_oneblock(struct map_info *map, struct flchip *chip, unsigned long adr){	unsigned int oldstatus, status;	unsigned int dq6, dq5;	unsigned long timeo = jiffies + HZ;	struct cfi_private *cfi = map->fldrv_priv;	DECLARE_WAITQUEUE(wait, current); retry:	cfi_spin_lock(chip->mutex);	if (chip->state != FL_READY){		set_current_state(TASK_UNINTERRUPTIBLE);		add_wait_queue(&chip->wq, &wait);                		cfi_spin_unlock(chip->mutex);		schedule();		remove_wait_queue(&chip->wq, &wait);#if 0		if(signal_pending(current))			return -EINTR;#endif		timeo = jiffies + HZ;		goto retry;	}		chip->state = FL_ERASING;	adr += chip->start;	ENABLE_VPP(map);	cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0x80, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, CFI_DEVICETYPE_X8, NULL);	cfi_write(map, CMD(0x30), adr);		timeo = jiffies + (HZ*20);	/* Wait for the end of programing/erasure by using the toggle method.	 * As long as there is a programming procedure going on, bit 6 of the last	 * written byte is toggling it's state with each consectuve read.	 * The toggling stops as soon as the procedure is completed.	 *	 * If the process has gone on for too long on the chip bit 5 gets.	 * After bit5 is set you can kill the operation by sending a reset	 * command to the chip.	 */	dq6 = CMD(1<<6);	dq5 = CMD(1<<5);	oldstatus = cfi_read(map, adr);	status = cfi_read(map, adr);	while( ((status & dq6) != (oldstatus & dq6)) && 		((status & dq5) != dq5) &&		!time_after(jiffies, timeo)) {		int wait_reps;		/* an initial short sleep */		cfi_spin_unlock(chip->mutex);		schedule_timeout(HZ/100);		cfi_spin_lock(chip->mutex);				if (chip->state != FL_ERASING) {			/* Someone's suspended the erase. Sleep */			set_current_state(TASK_UNINTERRUPTIBLE);			add_wait_queue(&chip->wq, &wait);						cfi_spin_unlock(chip->mutex);			printk(KERN_DEBUG "erase suspended. Sleeping\n");						schedule();			remove_wait_queue(&chip->wq, &wait);#if 0						if (signal_pending(current))				return -EINTR;#endif						timeo = jiffies + (HZ*2); /* FIXME */			cfi_spin_lock(chip->mutex);			continue;		}		/* Busy wait for 1/10 of a milisecond */		for(wait_reps = 0;		    	(wait_reps < 100) &&			((status & dq6) != (oldstatus & dq6)) && 			((status & dq5) != dq5);			wait_reps++) {						/* Latency issues. Drop the lock, wait a while and retry */			cfi_spin_unlock(chip->mutex);						cfi_udelay(1);					cfi_spin_lock(chip->mutex);			oldstatus = cfi_read(map, adr);			status = cfi_read(map, adr);		}		oldstatus = cfi_read(map, adr);		status = cfi_read(map, adr);	}	if ((status & dq6) != (oldstatus & dq6)) {		/* The erasing didn't stop?? */		if ((status & dq5) == dq5) {			/* dq5 is active so we can do a reset and stop the erase */			cfi_write(map, CMD(0xF0), chip->start);		}		chip->state = FL_READY;		wake_up(&chip->wq);		cfi_spin_unlock(chip->mutex);		printk("waiting for erase to complete timed out.");		DISABLE_VPP(map);		return -EIO;	}	DISABLE_VPP(map);	chip->state = FL_READY;	wake_up(&chip->wq);	cfi_spin_unlock(chip->mutex);	return 0;}static int cfi_amdstd_erase_varsize(struct mtd_info *mtd, struct erase_info *instr){	struct map_info *map = mtd->priv;	struct cfi_private *cfi = map->fldrv_priv;	unsigned long adr, len;	int chipnum, ret = 0;	int i, first;	struct mtd_erase_region_info *regions = mtd->eraseregions;	if (instr->addr > mtd->size)		return -EINVAL;	if ((instr->len + instr->addr) > mtd->size)		return -EINVAL;	/* Check that both start and end of the requested erase are	 * aligned with the erasesize at the appropriate addresses.	 */	i = 0;	/* Skip all erase regions which are ended before the start of 	   the requested erase. Actually, to save on the calculations,	   we skip to the first erase region which starts after the	   start of the requested erase, and then go back one.	*/		while (i < mtd->numeraseregions && instr->addr >= regions[i].offset)	       i++;	i--;	/* OK, now i is pointing at the erase region in which this 	   erase request starts. Check the start of the requested	   erase range is aligned with the erase size which is in	   effect here.	*/	if (instr->addr & (regions[i].erasesize-1))		return -EINVAL;	/* Remember the erase region we start on */	first = i;	/* Next, check that the end of the requested erase is aligned	 * with the erase region at that address.	 */	while (i<mtd->numeraseregions && (instr->addr + instr->len) >= regions[i].offset)		i++;	/* As before, drop back one to point at the region in which	   the address actually falls	*/	i--;		if ((instr->addr + instr->len) & (regions[i].erasesize-1))		return -EINVAL;		chipnum = instr->addr >> cfi->chipshift;	adr = instr->addr - (chipnum << cfi->chipshift);	len = instr->len;	i=first;	while(len) {		ret = do_erase_oneblock(map, &cfi->chips[chipnum], adr);		if (ret)			return ret;		adr += regions[i].erasesize;		len -= regions[i].erasesize;		if (adr % (1<< cfi->chipshift) == ((regions[i].offset + (regions[i].erasesize * regions[i].numblocks)) %( 1<< cfi->chipshift)))			i++;		if (adr >> cfi->chipshift) {			adr = 0;			chipnum++;						if (chipnum >= cfi->numchips)			break;		}	}	instr->state = MTD_ERASE_DONE;	if (instr->callback)		instr->callback(instr);		return 0;}static int cfi_amdstd_erase_onesize(struct mtd_info *mtd, struct erase_info *instr){	struct map_info *map = mtd->priv;	struct cfi_private *cfi = map->fldrv_priv;	unsigned long adr, len;	int chipnum, ret = 0;	if (instr->addr & (mtd->erasesize - 1))		return -EINVAL;	if (instr->len & (mtd->erasesize -1))		return -EINVAL;	if ((instr->len + instr->addr) > mtd->size)		return -EINVAL;	chipnum = instr->addr >> cfi->chipshift;	adr = instr->addr - (chipnum << cfi->chipshift);	len = instr->len;	while(len) {		ret = do_erase_oneblock(map, &cfi->chips[chipnum], adr);		if (ret)			return ret;		adr += mtd->erasesize;		len -= mtd->erasesize;		if (adr >> cfi->chipshift) {			adr = 0;			chipnum++;						if (chipnum >= cfi->numchips)			break;		}	}			instr->state = MTD_ERASE_DONE;	if (instr->callback)		instr->callback(instr);		return 0;}static int cfi_amdstd_erase_chip(struct mtd_info *mtd, struct erase_info *instr){	struct map_info *map = mtd->priv;	struct cfi_private *cfi = map->fldrv_priv;	int ret = 0;	if (instr->addr != 0)		return -EINVAL;	if (instr->len != mtd->size)		return -EINVAL;	ret = do_erase_chip(map, &cfi->chips[0]);	if (ret)		return ret;	instr->state = MTD_ERASE_DONE;	if (instr->callback)		instr->callback(instr);		return 0;}static void cfi_amdstd_sync (struct mtd_info *mtd){	struct map_info *map = mtd->priv;	struct cfi_private *cfi = map->fldrv_priv;	int i;	struct flchip *chip;	int ret = 0;	DECLARE_WAITQUEUE(wait, current);	for (i=0; !ret && i<cfi->numchips; i++) {		chip = &cfi->chips[i];	retry:		cfi_spin_lock(chip->mutex);		switch(chip->state) {		case FL_READY:		case FL_STATUS:		case FL_CFI_QUERY:		case FL_JEDEC_QUERY:			chip->oldstate = chip->state;			chip->state = FL_SYNCING;			/* No need to wake_up() on this state change - 			 * as the whole point is that nobody can do anything			 * with the chip now anyway.			 */		case FL_SYNCING:			cfi_spin_unlock(chip->mutex);			break;		default:			/* Not an idle state */			add_wait_queue(&chip->wq, &wait);						cfi_spin_unlock(chip->mutex);			schedule();		        remove_wait_queue(&chip->wq, &wait);						goto retry;		}	}	/* Unlock the chips again */	for (i--; i >=0; i--) {		chip = &cfi->chips[i];		cfi_spin_lock(chip->mutex);				if (chip->state == FL_SYNCING) {			chip->state = chip->oldstate;			wake_up(&chip->wq);		}		cfi_spin_unlock(chip->mutex);	}}static int cfi_amdstd_suspend(struct mtd_info *mtd){	struct map_info *map = mtd->priv;	struct cfi_private *cfi = map->fldrv_priv;	int i;	struct flchip *chip;	int ret = 0;//printk("suspend\n");	for (i=0; !ret && i<cfi->numchips; i++) {		chip = &cfi->chips[i];		cfi_spin_lock(chip->mutex);		switch(chip->state) {		case FL_READY:		case FL_STATUS:		case FL_CFI_QUERY:		case FL_JEDEC_QUERY:			chip->oldstate = chip->state;			chip->state = FL_PM_SUSPENDED;			/* No need to wake_up() on this state change - 			 * as the whole point is that nobody can do anything			 * with the chip now anyway.			 */		case FL_PM_SUSPENDED:			break;		default:			ret = -EAGAIN;			break;		}		cfi_spin_unlock(chip->mutex);	}	/* Unlock the chips again */	if (ret) {    		for (i--; i >=0; i--) {			chip = &cfi->chips[i];			cfi_spin_lock(chip->mutex);					if (chip->state == FL_PM_SUSPENDED) {				chip->state = chip->oldstate;				wake_up(&chip->wq);			}			cfi_spin_unlock(chip->mutex);		}	}		return ret;}static void cfi_amdstd_resume(struct mtd_info *mtd){	struct map_info *map = mtd->priv;	struct cfi_private *cfi = map->fldrv_priv;	int i;	struct flchip *chip;//printk("resume\n");	for (i=0; i<cfi->numchips; i++) {			chip = &cfi->chips[i];		cfi_spin_lock(chip->mutex);				if (chip->state == FL_PM_SUSPENDED) {			chip->state = FL_READY;			cfi_write(map, CMD(0xF0), chip->start);			wake_up(&chip->wq);		}		else			printk(KERN_ERR "Argh. Chip not in PM_SUSPENDED state upon resume()\n");		cfi_spin_unlock(chip->mutex);	}}static void cfi_amdstd_destroy(struct mtd_info *mtd){	struct map_info *map = mtd->priv;	struct cfi_private *cfi = map->fldrv_priv;	kfree(cfi->cmdset_priv);	kfree(cfi);}static char im_name[]="cfi_cmdset_0002";int __init cfi_amdstd_init(void){	inter_module_register(im_name, THIS_MODULE, &cfi_cmdset_0002);	return 0;}static void __exit cfi_amdstd_exit(void){	inter_module_unregister(im_name);}module_init(cfi_amdstd_init);module_exit(cfi_amdstd_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Crossnet Co. <info@crossnet.co.jp> et al.");MODULE_DESCRIPTION("MTD chip driver for AMD/Fujitsu flash chips");

⌨️ 快捷键说明

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