2010年1月18日 星期一

Linux Kernel(10.1)- drivers/mtd/devices/mtdram.c


MTD的基本介紹可以參考MTD - Memory Technology Devices,這篇文章要透過drivers/mtd/devices/mtdram.c來了解mtd的driver如何運作。
static unsigned long total_size = CONFIG_MTDRAM_TOTAL_SIZE;
#define MTDRAM_TOTAL_SIZE (total_size * 1024)

#ifdef MODULE
module_param(total_size, ulong, 0);
MODULE_PARM_DESC(total_size, "Total device size in KiB");
#endif

static int __init init_mtdram(void)
{
 void *addr;
 int err;

 if (!total_size)
  return -EINVAL;

 /* Allocate some memory */
 mtd_info = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
 if (!mtd_info)
  return -ENOMEM;

 addr = vmalloc(MTDRAM_TOTAL_SIZE);
 if (!addr) {
  kfree(mtd_info);
  mtd_info = NULL;
  return -ENOMEM;
 }
 err = mtdram_init_device(mtd_info, addr,
                   MTDRAM_TOTAL_SIZE, "mtdram test device");
 if (err) {
  vfree(addr);
  kfree(mtd_info);
  mtd_info = NULL;
  return err;
 }
 memset(mtd_info->priv, 0xff, MTDRAM_TOTAL_SIZE);
 return err;
}
首先,看到init_mtdram(),在該function中,我們分配一塊記憶體給mtd_info,以及一塊v-memory給稍候模擬的flash用,接著就呼叫mtdram_init_device()進行mtd的註冊動作,(一個mtd partition需要一個mtd_info來存放所需的資訊),init_mtdram()後面就將mtd_info->priv(即v-memory)的內容全部設成0xff,這是因為一個空的flash裡面預設就是0xff。

int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
  unsigned long size, char *name)
{
 memset(mtd, 0, sizeof(*mtd));

 /* Setup the MTD structure */
 mtd->name = name;
 mtd->type = MTD_RAM;
 mtd->flags = MTD_CAP_RAM;
 mtd->size = size;
 mtd->writesize = 1;
 mtd->erasesize = MTDRAM_ERASE_SIZE;
 mtd->priv = mapped_address;

 mtd->owner = THIS_MODULE;
 mtd->erase = ram_erase;
 mtd->point = ram_point;
 mtd->unpoint = ram_unpoint;
 mtd->get_unmapped_area = ram_get_unmapped_area;
 mtd->read = ram_read;
 mtd->write = ram_write;

 if (add_mtd_device(mtd)) {
  return -EIO;
 }

 return 0;
}
在mtdram_init_device()主要是填mtd_info相關資訊,然後呼叫add_mtd_device()進行註冊mtd的動作。呼叫del_mtd_device()進行移除mtd的工作。
point()/unpoint()可以參考http://www.linux-mtd.infradead.org/faq/general.html#L_point。

#ifdef MODULE
module_param(total_size, ulong, 0);
MODULE_PARM_DESC(total_size, "Total device size in KiB");
module_param(erase_size, ulong, 0);
MODULE_PARM_DESC(erase_size, "Device erase block size in KiB");
#endif

// We could store these in the mtd structure, but we only support 1 device.
static struct mtd_info *mtd_info;

static int ram_erase(struct mtd_info *mtd, struct erase_info *instr)
{
 if (instr->addr + instr->len > mtd->size)
  return -EINVAL;

 memset((char *)mtd->priv + instr->addr, 0xff, instr->len);

 instr->state = MTD_ERASE_DONE;
 mtd_erase_callback(instr);

 return 0;
}

static int ram_point(struct mtd_info *mtd, loff_t from, size_t len,
  size_t *retlen, void **virt, resource_size_t *phys)
{
 if (from + len > mtd->size)
  return -EINVAL;

 /* can we return a physical address with this driver? */
 if (phys)
  return -EINVAL;

 *virt = mtd->priv + from;
 *retlen = len;
 return 0;
}

static void ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
{
}

/*
 * Allow NOMMU mmap() to directly map the device (if not NULL)
 * - return the address to which the offset maps
 * - return -ENOSYS to indicate refusal to do the mapping
 */
static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
        unsigned long len,
        unsigned long offset,
        unsigned long flags)
{
 return (unsigned long) mtd->priv + offset;
}

static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
  size_t *retlen, u_char *buf)
{
 if (from + len > mtd->size)
  return -EINVAL;

 memcpy(buf, mtd->priv + from, len);

 *retlen = len;
 return 0;
}

static int ram_write(struct mtd_info *mtd, loff_t to, size_t len,
  size_t *retlen, const u_char *buf)
{
 if (to + len > mtd->size)
  return -EINVAL;

 memcpy((char *)mtd->priv + to, buf, len);

 *retlen = len;
 return 0;
}
剩下的read()/write()/erase()都是copy from/to memory和清成0xff,所以您可以發現讀寫NOR flash和讀寫memory差不多。



1 則留言:

  1. 想請問在kernel 在開機過程中,
    我要怎麼對 mtd 寫入一個 flag,
    在還沒有成功載入 filesystem 的條件之下,
    有辦法寫入和讀取嗎
    謝謝

    回覆刪除

熱門文章