从模型分析到Flash驱动模板(3)

内核管理分区的链表节点,通过它来实现对mtd_info对象的管理。

41 struct mtd_part { 42 struct mtd_info mtd; 43 struct mtd_info *master; 44 uint64_t offset; 45 struct list_head list; 46 };

struct mtd_part
--42-->对应的mtd_info对象
--43-->父对象指针
--44-->偏移量
--45-->链表节点

mtd_partition

描述一个分区

39 struct mtd_partition { 40 const char *name; /* identifier string */ 41 uint64_t size; /* partition size */ 42 uint64_t offset; /* offset within the master MTD space */ 43 uint32_t mask_flags; /* master MTD flags to mask out for this partition */ 44 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */ 45 };

mtd_partition
--40-->分区名
--41-->分区大小,使用MTDPART_SIZ_FULL表示使用全部空间
--42-->分区在master设备中的偏移量。MTDPART_OFS_APPEND表示从上一个分区结束的地方开始,MTDPART_OFS_NXTBLK表示从下一个擦除块开始; MTDPART_OFS_RETAIN表示尽可能向后偏,把size大小的空间留下即可
--43-->权限掩码,MTD_WRITEABLE表示将父设备的只读选项变成可写(可写分区要求size和offset要erasesize对齐,eg MTDPART_OFS_NEXTBLK)
--44-->NANDFlash的OOB布局,OOB是NANDFlash中很有用空间,比如yaffs2就需要将坏块信息存储在OOB区域

mtd_partitions

链表头,将所有的mtd_partition连接起来。

36 /* Our partition linked list */ 37 static LIST_HEAD(mtd_partitions);

下图是关键API的调用关系。

mtd_add_partition()
  └── add_mtd_device()
add_mtd_partitions()
  └── add_mtd_device()

add_mtd_device()

分配并初始化一个mtd对象。

367 334 int add_mtd_device(struct mtd_info *mtd) 335 { 336 struct mtd_notifier *not; 337 int i, error; 338 339 if (!mtd->backing_dev_info) { 340 switch (mtd->type) { 341 case MTD_RAM: 342 mtd->backing_dev_info = &mtd_bdi_rw_mappable; 343 break; 344 case MTD_ROM: 345 mtd->backing_dev_info = &mtd_bdi_ro_mappable; 346 break; 347 default: 348 mtd->backing_dev_info = &mtd_bdi_unmappable; 349 break; 350 } 351 } 355 356 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); 357 if (i < 0) 358 goto fail_locked; 359 360 mtd->index = i; 361 mtd->usecount = 0; 362 363 /* default value if not set by driver */ 364 if (mtd->bitflip_threshold == 0) 365 mtd->bitflip_threshold = mtd->ecc_strength; 366 367 if (is_power_of_2(mtd->erasesize)) 368 mtd->erasesize_shift = ffs(mtd->erasesize) - 1; 369 else 370 mtd->erasesize_shift = 0; 371 372 if (is_power_of_2(mtd->writesize)) 373 mtd->writesize_shift = ffs(mtd->writesize) - 1; 374 else 375 mtd->writesize_shift = 0; 376 377 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; 378 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; 379 380 /* Some chips always power up locked. Unlock them now */ 381 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { 382 error = mtd_unlock(mtd, 0, mtd->size); 387 } 388 392 mtd->dev.type = &mtd_devtype; 393 mtd->dev.class = &mtd_class; 394 mtd->dev.devt = MTD_DEVT(i); 395 dev_set_name(&mtd->dev, "mtd%d", i); 396 dev_set_drvdata(&mtd->dev, mtd); 397 if (device_register(&mtd->dev) != 0) 399 400 if (MTD_DEVT(i)) 401 device_create(&mtd_class, mtd->dev.parent, 402 MTD_DEVT(i) + 1, 403 NULL, "mtd%dro", i); 408 list_for_each_entry(not, &mtd_notifiers, list) 409 not->add(mtd); 417 return 0; 424 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8840177b6c5801da96185883ebeb051f.html