微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > ARM-Linux驱动--MTD驱动分析(一)

ARM-Linux驱动--MTD驱动分析(一)

时间:11-20 来源:互联网 点击:
主机:Gentoo Linux 11.2 with linux kernel 3.0.6

硬件平台:FL2440(S3C2440)with linux kernel 2.6.35

MTD(memory technology device内存技术设备) 在硬件和文件系统层之间的提供了一个抽象的接口,MTD是用来访问内存设备(如:ROM、flash)的中间层,它将内存设备的共有特性抽取出来,从而使增加新的内存设备驱动程序变得更简单。MTD的源代码都在/drivers/mtd目录中。

MTD中间层细分为四层,按从上到下依次为:设备节点、MTD设备层、MTD原始设备层和硬件驱动层。MTD中间层层次结构图如下:

从上图可以看出,原始设备是MTD字符设备和MTD块设备的抽象。

MTD设备层、MTD原始设备层和Flash硬件驱动层之间的接口关系如下图:

下面首先分析下MTD原始层设备

1、mtd_info数据结构

  1. structmtd_info{
  2. u_chartype;//内存技术类型,例如MTD_RAM,MTD_ROM,MTD_NORFLASH,MTD_NAND_FLASH,MTD_PEROM等
  3. uint32_tflags;//标志位
  4. uint64_tsize;//TotalsizeoftheMTD//MTD设备的大小
  5. /*"Major"erasesizeforthedevice.Naïveusersmaytakethis
  6. *tobetheonlyerasesizeavailable,ormayusethemoredetailed
  7. *informationbelowiftheydesire
  8. */
  9. uint32_terasesize;//最小的擦除块大小
  10. /*Minimalwritableflashunitsize.IncaseofNORflashitis1(even
  11. *thoughindividualbitscanbecleared),incaseofNANDflashitis
  12. *oneNANDpage(orhalf,orone-fourthsofit),incaseofECC-edNOR
  13. *itisofECCblocksize,etc.Itisillegaltohavewritesize=0.
  14. *Anydriverregisteringastructmtd_infomustensureawritesizeof
  15. *1orlarger.
  16. */
  17. uint32_twritesize;//编程块大小
  18. uint32_toobsize;//AmountofOOBdataperblock(e.g.16)//oob(Outofband)块大小
  19. uint32_toobavail;//AvailableOOBbytesperblock//每块的可用的oob字节
  20. /*
  21. *Iferasesizeisapowerof2thentheshiftisstoredin
  22. *erasesize_shiftotherwiseerasesize_shiftiszero.Dittowritesize.
  23. */
  24. unsignedinterasesize_shift;
  25. unsignedintwritesize_shift;
  26. /*Masksbasedonerasesize_shiftandwritesize_shift*/
  27. unsignedinterasesize_mask;
  28. unsignedintwritesize_mask;
  29. //Kernel-onlystuffstartshere.
  30. constchar*name;
  31. intindex;
  32. /*ecclayoutstructurepointer-readonly!*/
  33. structnand_ecclayout*ecclayout;//eec布局结构
  34. /*Dataforvariableeraseregions.Ifnumeraseregionsiszero,
  35. *itmeansthatthewholedevicehaserasesizeasgivenabove.
  36. */
  37. intnumeraseregions;//擦除区域个数,通常为1
  38. structmtd_erase_region_info*eraseregions;//擦除区域的区域信息地址
  39. /*
  40. *Eraseisanasynchronousoperation.Devicedriversaresupposed
  41. *tocallinstr->callback()whenevertheoperationcompletes,even
  42. *ifitcompleteswithafailure.
  43. *Callersaresupposedtopassacallbackfunctionandwaitforit
  44. *tobecalledbeforewritingtotheblock.
  45. */
  46. int(*erase)(structmtd_info*mtd,structerase_info*instr);//函数指针,erase函数的功能是将一个erase_info加入擦除队列
  47. /*ThisstuffforeXecute-In-Place*/
  48. /*physisoptionalandmaybesettoNULL*/
  49. int(*point)(structmtd_info*mtd,loff_tfrom,size_tlen,
  50. size_t*retlen,void**virt,resource_size_t*phys);//point函数功能是允许片内执行(XIP)
  51. /*WeprobablyshouldntallowXIPiftheunpointisntaNULL*/
  52. void(*unpoint)(structmtd_info*mtd,loff_tfrom,size_tlen);//unpoint函数与point函数相反,是禁止片内执行(XIP)
  53. /*AllowNOMMUmmap()todirectlymapthedevice(ifnotNULL)
  54. *-returntheaddresstowhichtheoffsetmaps
  55. *-return-ENOSYStoindicaterefusaltodothemapping
  56. */
  57. //如果不是NULL,则允许无MMU单元的地址映射,返回偏移地址
  58. unsignedlong(*get_unmapped_area)(structmtd_info*mtd,
  59. unsignedlonglen,
  60. unsignedlongoffset,
  61. unsignedlongflags);
  62. /*Backingdevicecapabilitiesforthisdevice
  63. *-providesmmapcapabilities
  64. */
  65. structbacking_dev_info*backing_dev_info;
  66. //MTD设备的读写函数
  67. int(*read)(structmtd_info*mtd,loff_tfrom,size_tlen,size_t*retlen,u_char*buf);
  68. int(*write)(structmtd_info*mtd,loff_tto,size_tlen,size_t*retlen,constu_char*buf);
  69. /*Inblackboxflightrecorderlikescenarioswewanttomakesuccessful
  70. writesininterruptcontext.panic_write()isonlyintendedtobe
  71. calledwhenitsknownthekernelisabouttopanicandweneedthe
  72. writetosucceed.Sincethekernelisnotgoingtoberunningformuch
  73. longer,thisfunctioncanbreaklock

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top