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

📄 lart.c

📁 基于linux-2.6.28的mtd驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
   /* was the erase successfull? */   if ((status & STATUS_ERASE_ERR))	 {		printk (KERN_WARNING "%s: erase error at address 0x%.8x.\n",module_name,offset);		return (0);	 }   return (1);}static int flash_erase (struct mtd_info *mtd,struct erase_info *instr){   __u32 addr,len;   int i,first;#ifdef LART_DEBUG   printk (KERN_DEBUG "%s(addr = 0x%.8x, len = %d)\n", __func__, instr->addr, instr->len);#endif   /* sanity checks */   if (instr->addr + instr->len > mtd->size) return (-EINVAL);   /*	* check that both start and end of the requested erase are	* aligned with the erasesize at the appropriate addresses.	*	* 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.	*/   for (i = 0; i < mtd->numeraseregions && instr->addr >= mtd->eraseregions[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 & (mtd->eraseregions[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.	*	* as before, drop back one to point at the region in which	* the address actually falls	*/   for (; i < mtd->numeraseregions && instr->addr + instr->len >= mtd->eraseregions[i].offset; i++) ;   i--;   /* is the end aligned on a block boundary? */   if ((instr->addr + instr->len) & (mtd->eraseregions[i].erasesize - 1)) return (-EINVAL);   addr = instr->addr;   len = instr->len;   i = first;   /* now erase those blocks */   while (len)	 {		if (!erase_block (addr))		  {			 instr->state = MTD_ERASE_FAILED;			 return (-EIO);		  }		addr += mtd->eraseregions[i].erasesize;		len -= mtd->eraseregions[i].erasesize;		if (addr == mtd->eraseregions[i].offset + (mtd->eraseregions[i].erasesize * mtd->eraseregions[i].numblocks)) i++;	 }   instr->state = MTD_ERASE_DONE;   mtd_erase_callback(instr);   return (0);}static int flash_read (struct mtd_info *mtd,loff_t from,size_t len,size_t *retlen,u_char *buf){#ifdef LART_DEBUG   printk (KERN_DEBUG "%s(from = 0x%.8x, len = %d)\n", __func__, (__u32)from, len);#endif   /* sanity checks */   if (!len) return (0);   if (from + len > mtd->size) return (-EINVAL);   /* we always read len bytes */   *retlen = len;   /* first, we read bytes until we reach a dword boundary */   if (from & (BUSWIDTH - 1))	 {		int gap = BUSWIDTH - (from & (BUSWIDTH - 1));		while (len && gap--) *buf++ = read8 (from++), len--;	 }   /* now we read dwords until we reach a non-dword boundary */   while (len >= BUSWIDTH)	 {		*((__u32 *) buf) = read32 (from);		buf += BUSWIDTH;		from += BUSWIDTH;		len -= BUSWIDTH;	 }   /* top up the last unaligned bytes */   if (len & (BUSWIDTH - 1))	 while (len--) *buf++ = read8 (from++);   return (0);}/* * Write one dword ``x'' to flash memory at offset ``offset''. ``offset'' * must be 32 bits, i.e. it must be on a dword boundary. * * Returns 1 if successful, 0 otherwise. */static inline int write_dword (__u32 offset,__u32 x){   __u32 status;#ifdef LART_DEBUG   printk (KERN_DEBUG "%s(): 0x%.8x <- 0x%.8x\n", __func__, offset, x);#endif   /* setup writing */   write32 (DATA_TO_FLASH (PGM_SETUP),offset);   /* write the data */   write32 (x,offset);   /* wait for the write to finish */   do	 {		write32 (DATA_TO_FLASH (STATUS_READ),offset);		status = FLASH_TO_DATA (read32 (offset));	 }   while ((~status & STATUS_BUSY) != 0);   /* put the flash back into command mode */   write32 (DATA_TO_FLASH (READ_ARRAY),offset);   /* was the write successfull? */   if ((status & STATUS_PGM_ERR) || read32 (offset) != x)	 {		printk (KERN_WARNING "%s: write error at address 0x%.8x.\n",module_name,offset);		return (0);	 }   return (1);}static int flash_write (struct mtd_info *mtd,loff_t to,size_t len,size_t *retlen,const u_char *buf){   __u8 tmp[4];   int i,n;#ifdef LART_DEBUG   printk (KERN_DEBUG "%s(to = 0x%.8x, len = %d)\n", __func__, (__u32)to, len);#endif   *retlen = 0;   /* sanity checks */   if (!len) return (0);   if (to + len > mtd->size) return (-EINVAL);   /* first, we write a 0xFF.... padded byte until we reach a dword boundary */   if (to & (BUSWIDTH - 1))	 {		__u32 aligned = to & ~(BUSWIDTH - 1);		int gap = to - aligned;		i = n = 0;		while (gap--) tmp[i++] = 0xFF;		while (len && i < BUSWIDTH) tmp[i++] = buf[n++], len--;		while (i < BUSWIDTH) tmp[i++] = 0xFF;		if (!write_dword (aligned,*((__u32 *) tmp))) return (-EIO);		to += n;		buf += n;		*retlen += n;	 }   /* now we write dwords until we reach a non-dword boundary */   while (len >= BUSWIDTH)	 {		if (!write_dword (to,*((__u32 *) buf))) return (-EIO);		to += BUSWIDTH;		buf += BUSWIDTH;		*retlen += BUSWIDTH;		len -= BUSWIDTH;	 }   /* top up the last unaligned bytes, padded with 0xFF.... */   if (len & (BUSWIDTH - 1))	 {		i = n = 0;		while (len--) tmp[i++] = buf[n++];		while (i < BUSWIDTH) tmp[i++] = 0xFF;		if (!write_dword (to,*((__u32 *) tmp))) return (-EIO);		*retlen += n;	 }   return (0);}/***************************************************************************************************/static struct mtd_info mtd;static struct mtd_erase_region_info erase_regions[] = {	/* parameter blocks */	{		.offset		= 0x00000000,		.erasesize	= FLASH_BLOCKSIZE_PARAM,		.numblocks	= FLASH_NUMBLOCKS_16m_PARAM,	},	/* main blocks */	{		.offset	 = FLASH_BLOCKSIZE_PARAM * FLASH_NUMBLOCKS_16m_PARAM,		.erasesize	= FLASH_BLOCKSIZE_MAIN,		.numblocks	= FLASH_NUMBLOCKS_16m_MAIN,	}};#ifdef HAVE_PARTITIONSstatic struct mtd_partition lart_partitions[] = {	/* blob */	{		.name	= "blob",		.offset	= BLOB_START,		.size	= BLOB_LEN,	},	/* kernel */	{		.name	= "kernel",		.offset	= KERNEL_START,		/* MTDPART_OFS_APPEND */		.size	= KERNEL_LEN,	},	/* initial ramdisk / file system */	{		.name	= "file system",		.offset	= INITRD_START,		/* MTDPART_OFS_APPEND */		.size	= INITRD_LEN,		/* MTDPART_SIZ_FULL */	}};#endifint __init lart_flash_init (void){   int result;   memset (&mtd,0,sizeof (mtd));   printk ("MTD driver for LART. Written by Abraham vd Merwe <abraham@2d3d.co.za>\n");   printk ("%s: Probing for 28F160x3 flash on LART...\n",module_name);   if (!flash_probe ())	 {		printk (KERN_WARNING "%s: Found no LART compatible flash device\n",module_name);		return (-ENXIO);	 }   printk ("%s: This looks like a LART board to me.\n",module_name);   mtd.name = module_name;   mtd.type = MTD_NORFLASH;   mtd.writesize = 1;   mtd.flags = MTD_CAP_NORFLASH;   mtd.size = FLASH_BLOCKSIZE_PARAM * FLASH_NUMBLOCKS_16m_PARAM + FLASH_BLOCKSIZE_MAIN * FLASH_NUMBLOCKS_16m_MAIN;   mtd.erasesize = FLASH_BLOCKSIZE_MAIN;   mtd.numeraseregions = ARRAY_SIZE(erase_regions);   mtd.eraseregions = erase_regions;   mtd.erase = flash_erase;   mtd.read = flash_read;   mtd.write = flash_write;   mtd.owner = THIS_MODULE;#ifdef LART_DEBUG   printk (KERN_DEBUG		   "mtd.name = %s\n"		   "mtd.size = 0x%.8x (%uM)\n"		   "mtd.erasesize = 0x%.8x (%uK)\n"		   "mtd.numeraseregions = %d\n",		   mtd.name,		   mtd.size,mtd.size / (1024*1024),		   mtd.erasesize,mtd.erasesize / 1024,		   mtd.numeraseregions);   if (mtd.numeraseregions)	 for (result = 0; result < mtd.numeraseregions; result++)	   printk (KERN_DEBUG			   "\n\n"			   "mtd.eraseregions[%d].offset = 0x%.8x\n"			   "mtd.eraseregions[%d].erasesize = 0x%.8x (%uK)\n"			   "mtd.eraseregions[%d].numblocks = %d\n",			   result,mtd.eraseregions[result].offset,			   result,mtd.eraseregions[result].erasesize,mtd.eraseregions[result].erasesize / 1024,			   result,mtd.eraseregions[result].numblocks);#ifdef HAVE_PARTITIONS   printk ("\npartitions = %d\n", ARRAY_SIZE(lart_partitions));   for (result = 0; result < ARRAY_SIZE(lart_partitions); result++)	 printk (KERN_DEBUG			 "\n\n"			 "lart_partitions[%d].name = %s\n"			 "lart_partitions[%d].offset = 0x%.8x\n"			 "lart_partitions[%d].size = 0x%.8x (%uK)\n",			 result,lart_partitions[result].name,			 result,lart_partitions[result].offset,			 result,lart_partitions[result].size,lart_partitions[result].size / 1024);#endif#endif#ifndef HAVE_PARTITIONS   result = add_mtd_device (&mtd);#else   result = add_mtd_partitions (&mtd,lart_partitions, ARRAY_SIZE(lart_partitions));#endif   return (result);}void __exit lart_flash_exit (void){#ifndef HAVE_PARTITIONS   del_mtd_device (&mtd);#else   del_mtd_partitions (&mtd);#endif}module_init (lart_flash_init);module_exit (lart_flash_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Abraham vd Merwe <abraham@2d3d.co.za>");MODULE_DESCRIPTION("MTD driver for Intel 28F160F3 on LART board");

⌨️ 快捷键说明

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