Lines Matching full:mtd

3  * Core registration and callback routines for MTD
32 #include <linux/mtd/mtd.h>
33 #include <linux/mtd/partitions.h>
43 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_cls_suspend() local
45 return mtd ? mtd_suspend(mtd) : 0; in mtd_cls_suspend()
50 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_cls_resume() local
52 if (mtd) in mtd_cls_resume()
53 mtd_resume(mtd); in mtd_cls_resume()
64 .name = "mtd",
87 /* REVISIT once MTD uses the driver model better, whoever allocates
92 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_release() local
93 dev_t index = MTD_DEVT(mtd->index); in mtd_release()
102 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_type_show() local
105 switch (mtd->type) { in mtd_type_show()
141 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_flags_show() local
143 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); in mtd_flags_show()
150 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_size_show() local
153 (unsigned long long)mtd->size); in mtd_size_show()
160 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_erasesize_show() local
162 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); in mtd_erasesize_show()
169 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_writesize_show() local
171 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); in mtd_writesize_show()
178 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_subpagesize_show() local
179 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; in mtd_subpagesize_show()
188 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_oobsize_show() local
190 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); in mtd_oobsize_show()
197 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_oobavail_show() local
199 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->oobavail); in mtd_oobavail_show()
206 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_numeraseregions_show() local
208 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); in mtd_numeraseregions_show()
216 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_name_show() local
218 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); in mtd_name_show()
225 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_strength_show() local
227 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); in mtd_ecc_strength_show()
235 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bitflip_threshold_show() local
237 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); in mtd_bitflip_threshold_show()
244 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bitflip_threshold_store() local
252 mtd->bitflip_threshold = bitflip_threshold; in mtd_bitflip_threshold_store()
262 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_step_size_show() local
264 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); in mtd_ecc_step_size_show()
272 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_stats_corrected_show() local
273 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_ecc_stats_corrected_show()
283 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_stats_errors_show() local
284 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_ecc_stats_errors_show()
293 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_badblocks_show() local
294 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_badblocks_show()
303 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bbtblocks_show() local
304 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_bbtblocks_show()
330 ATTRIBUTE_GROUPS(mtd);
333 .name = "mtd",
340 struct mtd_info *mtd = s->private; in mtd_partid_debug_show() local
342 seq_printf(s, "%s\n", mtd->dbg.partid); in mtd_partid_debug_show()
351 struct mtd_info *mtd = s->private; in mtd_partname_debug_show() local
353 seq_printf(s, "%s\n", mtd->dbg.partname); in mtd_partname_debug_show()
362 static void mtd_debugfs_populate(struct mtd_info *mtd) in mtd_debugfs_populate() argument
364 struct device *dev = &mtd->dev; in mtd_debugfs_populate()
371 mtd->dbg.dfs_dir = root; in mtd_debugfs_populate()
373 if (mtd->dbg.partid) in mtd_debugfs_populate()
374 debugfs_create_file("partid", 0400, root, mtd, in mtd_debugfs_populate()
377 if (mtd->dbg.partname) in mtd_debugfs_populate()
378 debugfs_create_file("partname", 0400, root, mtd, in mtd_debugfs_populate()
383 unsigned mtd_mmap_capabilities(struct mtd_info *mtd) in mtd_mmap_capabilities() argument
385 switch (mtd->type) { in mtd_mmap_capabilities()
402 struct mtd_info *mtd; in mtd_reboot_notifier() local
404 mtd = container_of(n, struct mtd_info, reboot_notifier); in mtd_reboot_notifier()
405 mtd->_reboot(mtd); in mtd_reboot_notifier()
412 * @mtd: pointer to new MTD device info structure
427 * From the pairing info the MTD user can find all the wunits paired with
430 * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
432 * mtd_pairing_info_to_wunit(mtd, &info);
436 int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit, in mtd_wunit_to_pairing_info() argument
439 struct mtd_info *master = mtd_get_master(mtd); in mtd_wunit_to_pairing_info()
457 * @mtd: pointer to new MTD device info structure
472 * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
474 * wunit = mtd_pairing_info_to_wunit(mtd, &info);
475 * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
476 * mtd->writesize, &retlen, buf + (i * mtd->writesize));
479 int mtd_pairing_info_to_wunit(struct mtd_info *mtd, in mtd_pairing_info_to_wunit() argument
482 struct mtd_info *master = mtd_get_master(mtd); in mtd_pairing_info_to_wunit()
491 return mtd->pairing->get_wunit(master, info); in mtd_pairing_info_to_wunit()
499 * @mtd: pointer to new MTD device info structure
507 int mtd_pairing_groups(struct mtd_info *mtd) in mtd_pairing_groups() argument
509 struct mtd_info *master = mtd_get_master(mtd); in mtd_pairing_groups()
521 struct mtd_info *mtd = priv; in mtd_nvmem_reg_read() local
525 err = mtd_read(mtd, offset, bytes, &retlen, val); in mtd_nvmem_reg_read()
532 static int mtd_nvmem_add(struct mtd_info *mtd) in mtd_nvmem_add() argument
537 config.dev = &mtd->dev; in mtd_nvmem_add()
538 config.name = dev_name(&mtd->dev); in mtd_nvmem_add()
541 config.size = mtd->size; in mtd_nvmem_add()
547 config.priv = mtd; in mtd_nvmem_add()
549 mtd->nvmem = nvmem_register(&config); in mtd_nvmem_add()
550 if (IS_ERR(mtd->nvmem)) { in mtd_nvmem_add()
552 if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) { in mtd_nvmem_add()
553 mtd->nvmem = NULL; in mtd_nvmem_add()
555 dev_err(&mtd->dev, "Failed to register NVMEM device\n"); in mtd_nvmem_add()
556 return PTR_ERR(mtd->nvmem); in mtd_nvmem_add()
564 * add_mtd_device - register an MTD device
565 * @mtd: pointer to new MTD device info structure
567 * Add a device to the list of MTD devices present in the system, and
568 * notify each currently active MTD 'user' of its arrival. Returns
572 int add_mtd_device(struct mtd_info *mtd) in add_mtd_device() argument
574 struct mtd_info *master = mtd_get_master(mtd); in add_mtd_device()
580 * mtd_device_parse_register() multiple times on the same master MTD, in add_mtd_device()
583 if (WARN_ONCE(mtd->dev.type, "MTD already registered\n")) in add_mtd_device()
586 BUG_ON(mtd->writesize == 0); in add_mtd_device()
589 * MTD drivers should implement ->_{write,read}() or in add_mtd_device()
592 if (WARN_ON((mtd->_write && mtd->_write_oob) || in add_mtd_device()
593 (mtd->_read && mtd->_read_oob))) in add_mtd_device()
596 if (WARN_ON((!mtd->erasesize || !master->_erase) && in add_mtd_device()
597 !(mtd->flags & MTD_NO_ERASE))) in add_mtd_device()
608 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION && in add_mtd_device()
609 (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH || in add_mtd_device()
615 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); in add_mtd_device()
621 mtd->index = i; in add_mtd_device()
622 mtd->usecount = 0; in add_mtd_device()
625 if (mtd->bitflip_threshold == 0) in add_mtd_device()
626 mtd->bitflip_threshold = mtd->ecc_strength; in add_mtd_device()
628 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in add_mtd_device()
631 mtd->erasesize /= ngroups; in add_mtd_device()
632 mtd->size = (u64)mtd_div_by_eb(mtd->size, master) * in add_mtd_device()
633 mtd->erasesize; in add_mtd_device()
636 if (is_power_of_2(mtd->erasesize)) in add_mtd_device()
637 mtd->erasesize_shift = ffs(mtd->erasesize) - 1; in add_mtd_device()
639 mtd->erasesize_shift = 0; in add_mtd_device()
641 if (is_power_of_2(mtd->writesize)) in add_mtd_device()
642 mtd->writesize_shift = ffs(mtd->writesize) - 1; in add_mtd_device()
644 mtd->writesize_shift = 0; in add_mtd_device()
646 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; in add_mtd_device()
647 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; in add_mtd_device()
650 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { in add_mtd_device()
651 error = mtd_unlock(mtd, 0, mtd->size); in add_mtd_device()
655 mtd->name); in add_mtd_device()
663 mtd->dev.type = &mtd_devtype; in add_mtd_device()
664 mtd->dev.class = &mtd_class; in add_mtd_device()
665 mtd->dev.devt = MTD_DEVT(i); in add_mtd_device()
666 dev_set_name(&mtd->dev, "mtd%d", i); in add_mtd_device()
667 dev_set_drvdata(&mtd->dev, mtd); in add_mtd_device()
668 of_node_get(mtd_get_of_node(mtd)); in add_mtd_device()
669 error = device_register(&mtd->dev); in add_mtd_device()
674 error = mtd_nvmem_add(mtd); in add_mtd_device()
678 mtd_debugfs_populate(mtd); in add_mtd_device()
680 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, in add_mtd_device()
681 "mtd%dro", i); in add_mtd_device()
683 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); in add_mtd_device()
687 not->add(mtd); in add_mtd_device()
698 device_unregister(&mtd->dev); in add_mtd_device()
700 of_node_put(mtd_get_of_node(mtd)); in add_mtd_device()
708 * del_mtd_device - unregister an MTD device
709 * @mtd: pointer to MTD device info structure
711 * Remove a device from the list of MTD devices present in the system,
712 * and notify each currently active MTD 'user' of its departure.
717 int del_mtd_device(struct mtd_info *mtd) in del_mtd_device() argument
724 debugfs_remove_recursive(mtd->dbg.dfs_dir); in del_mtd_device()
726 if (idr_find(&mtd_idr, mtd->index) != mtd) { in del_mtd_device()
734 not->remove(mtd); in del_mtd_device()
736 if (mtd->usecount) { in del_mtd_device()
737 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", in del_mtd_device()
738 mtd->index, mtd->name, mtd->usecount); in del_mtd_device()
742 if (mtd->nvmem) in del_mtd_device()
743 nvmem_unregister(mtd->nvmem); in del_mtd_device()
745 device_unregister(&mtd->dev); in del_mtd_device()
747 idr_remove(&mtd_idr, mtd->index); in del_mtd_device()
748 of_node_put(mtd_get_of_node(mtd)); in del_mtd_device()
763 static void mtd_set_dev_defaults(struct mtd_info *mtd) in mtd_set_dev_defaults() argument
765 if (mtd->dev.parent) { in mtd_set_dev_defaults()
766 if (!mtd->owner && mtd->dev.parent->driver) in mtd_set_dev_defaults()
767 mtd->owner = mtd->dev.parent->driver->owner; in mtd_set_dev_defaults()
768 if (!mtd->name) in mtd_set_dev_defaults()
769 mtd->name = dev_name(mtd->dev.parent); in mtd_set_dev_defaults()
771 pr_debug("mtd device won't show a device symlink in sysfs\n"); in mtd_set_dev_defaults()
774 INIT_LIST_HEAD(&mtd->partitions); in mtd_set_dev_defaults()
775 mutex_init(&mtd->master.partitions_lock); in mtd_set_dev_defaults()
779 * mtd_device_parse_register - parse partitions and register an MTD device.
781 * @mtd: the MTD device to register
782 * @types: the list of MTD partition probes to try, see
784 * @parser_data: MTD partition parser-specific data
788 * MTD device is registered if no partition info is found
790 * This function aggregates MTD partitions parsing (done by
791 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
792 * basically follows the most common pattern found in many MTD drivers:
796 * * Then It tries to probe partitions on MTD device @mtd using parsers
801 * * If no partitions were found this function just registers the MTD device
802 * @mtd and exits.
806 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, in mtd_device_parse_register() argument
813 mtd_set_dev_defaults(mtd); in mtd_device_parse_register()
816 ret = add_mtd_device(mtd); in mtd_device_parse_register()
822 ret = parse_mtd_partitions(mtd, types, parser_data); in mtd_device_parse_register()
826 ret = add_mtd_partitions(mtd, parts, nr_parts); in mtd_device_parse_register()
827 else if (!device_is_registered(&mtd->dev)) in mtd_device_parse_register()
828 ret = add_mtd_device(mtd); in mtd_device_parse_register()
843 WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call, in mtd_device_parse_register()
844 "MTD already registered\n"); in mtd_device_parse_register()
845 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { in mtd_device_parse_register()
846 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; in mtd_device_parse_register()
847 register_reboot_notifier(&mtd->reboot_notifier); in mtd_device_parse_register()
851 if (ret && device_is_registered(&mtd->dev)) in mtd_device_parse_register()
852 del_mtd_device(mtd); in mtd_device_parse_register()
859 * mtd_device_unregister - unregister an existing MTD device.
861 * @master: the MTD device to unregister. This will unregister both the master
883 * register_mtd_user - register a 'user' of MTD devices.
887 * or removal of MTD devices. Causes the 'add' callback to be immediately
888 * invoked for each MTD device currently present in the system.
892 struct mtd_info *mtd; in register_mtd_user() local
900 mtd_for_each_device(mtd) in register_mtd_user()
901 new->add(mtd); in register_mtd_user()
908 * unregister_mtd_user - unregister a 'user' of MTD devices.
912 * notified upon addition or removal of MTD devices. Causes the
913 * 'remove' callback to be immediately invoked for each MTD device
918 struct mtd_info *mtd; in unregister_mtd_user() local
924 mtd_for_each_device(mtd) in unregister_mtd_user()
925 old->remove(mtd); in unregister_mtd_user()
934 * get_mtd_device - obtain a validated handle for an MTD device
935 * @mtd: last known address of the required MTD device
936 * @num: internal device number of the required MTD device
944 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) in get_mtd_device() argument
953 if (other == mtd) { in get_mtd_device()
954 ret = mtd; in get_mtd_device()
960 if (mtd && mtd != ret) in get_mtd_device()
979 int __get_mtd_device(struct mtd_info *mtd) in __get_mtd_device() argument
981 struct mtd_info *master = mtd_get_master(mtd); in __get_mtd_device()
988 err = master->_get_device(mtd); in __get_mtd_device()
996 while (mtd->parent) { in __get_mtd_device()
997 mtd->usecount++; in __get_mtd_device()
998 mtd = mtd->parent; in __get_mtd_device()
1006 * get_mtd_device_nm - obtain a validated handle for an MTD device by
1008 * @name: MTD device name to open
1010 * This function returns MTD device description structure in case of
1016 struct mtd_info *mtd = NULL, *other; in get_mtd_device_nm() local
1022 mtd = other; in get_mtd_device_nm()
1027 if (!mtd) in get_mtd_device_nm()
1030 err = __get_mtd_device(mtd); in get_mtd_device_nm()
1035 return mtd; in get_mtd_device_nm()
1043 void put_mtd_device(struct mtd_info *mtd) in put_mtd_device() argument
1046 __put_mtd_device(mtd); in put_mtd_device()
1052 void __put_mtd_device(struct mtd_info *mtd) in __put_mtd_device() argument
1054 struct mtd_info *master = mtd_get_master(mtd); in __put_mtd_device()
1056 while (mtd->parent) { in __put_mtd_device()
1057 --mtd->usecount; in __put_mtd_device()
1058 BUG_ON(mtd->usecount < 0); in __put_mtd_device()
1059 mtd = mtd->parent; in __put_mtd_device()
1074 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) in mtd_erase() argument
1076 struct mtd_info *master = mtd_get_master(mtd); in mtd_erase()
1077 u64 mst_ofs = mtd_get_master_ofs(mtd, 0); in mtd_erase()
1084 if (!mtd->erasesize || !master->_erase) in mtd_erase()
1087 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) in mtd_erase()
1089 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_erase()
1097 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_erase()
1098 adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) * in mtd_erase()
1100 adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) * in mtd_erase()
1111 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_erase()
1114 instr->fail_addr *= mtd->erasesize; in mtd_erase()
1125 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, in mtd_point() argument
1128 struct mtd_info *master = mtd_get_master(mtd); in mtd_point()
1136 if (from < 0 || from >= mtd->size || len > mtd->size - from) in mtd_point()
1141 from = mtd_get_master_ofs(mtd, from); in mtd_point()
1147 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) in mtd_unpoint() argument
1149 struct mtd_info *master = mtd_get_master(mtd); in mtd_unpoint()
1153 if (from < 0 || from >= mtd->size || len > mtd->size - from) in mtd_unpoint()
1157 return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len); in mtd_unpoint()
1166 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, in mtd_get_unmapped_area() argument
1173 ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL); in mtd_get_unmapped_area()
1177 mtd_unpoint(mtd, offset, retlen); in mtd_get_unmapped_area()
1184 static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master, in mtd_update_ecc_stats() argument
1189 if (master == mtd) in mtd_update_ecc_stats()
1196 while (mtd->parent) { in mtd_update_ecc_stats()
1197 mtd->ecc_stats.failed += diff.failed; in mtd_update_ecc_stats()
1198 mtd->ecc_stats.corrected += diff.corrected; in mtd_update_ecc_stats()
1199 mtd = mtd->parent; in mtd_update_ecc_stats()
1203 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, in mtd_read() argument
1212 ret = mtd_read_oob(mtd, from, &ops); in mtd_read()
1219 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, in mtd_write() argument
1228 ret = mtd_write_oob(mtd, to, &ops); in mtd_write()
1242 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, in mtd_panic_write() argument
1245 struct mtd_info *master = mtd_get_master(mtd); in mtd_panic_write()
1250 if (to < 0 || to >= mtd->size || len > mtd->size - to) in mtd_panic_write()
1252 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_panic_write()
1259 return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len, in mtd_panic_write()
1264 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs, in mtd_check_oob_ops() argument
1278 if (offs < 0 || offs + ops->len > mtd->size) in mtd_check_oob_ops()
1284 if (ops->ooboffs >= mtd_oobavail(mtd, ops)) in mtd_check_oob_ops()
1287 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) - in mtd_check_oob_ops()
1288 mtd_div_by_ws(offs, mtd)) * in mtd_check_oob_ops()
1289 mtd_oobavail(mtd, ops)) - ops->ooboffs; in mtd_check_oob_ops()
1297 static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from, in mtd_read_oob_std() argument
1300 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_oob_std()
1303 from = mtd_get_master_ofs(mtd, from); in mtd_read_oob_std()
1313 static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to, in mtd_write_oob_std() argument
1316 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_oob_std()
1319 to = mtd_get_master_ofs(mtd, to); in mtd_write_oob_std()
1329 static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read, in mtd_io_emulated_slc() argument
1332 struct mtd_info *master = mtd_get_master(mtd); in mtd_io_emulated_slc()
1342 ebofs = mtd_mod_by_eb(start, mtd); in mtd_io_emulated_slc()
1343 base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize; in mtd_io_emulated_slc()
1345 info.pair = mtd_div_by_ws(ebofs, mtd); in mtd_io_emulated_slc()
1346 pageofs = mtd_mod_by_ws(ebofs, mtd); in mtd_io_emulated_slc()
1347 oobavail = mtd_oobavail(mtd, ops); in mtd_io_emulated_slc()
1358 pos = mtd_wunit_to_offset(mtd, base, wunit); in mtd_io_emulated_slc()
1361 if (adjops.len > mtd->writesize - pageofs) in mtd_io_emulated_slc()
1362 adjops.len = mtd->writesize - pageofs; in mtd_io_emulated_slc()
1369 ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops); in mtd_io_emulated_slc()
1373 ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops); in mtd_io_emulated_slc()
1392 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) in mtd_read_oob() argument
1394 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_oob()
1400 ret_code = mtd_check_oob_ops(mtd, from, ops); in mtd_read_oob()
1406 /* Check the validity of a potential fallback on mtd->_read */ in mtd_read_oob()
1410 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_read_oob()
1411 ret_code = mtd_io_emulated_slc(mtd, from, true, ops); in mtd_read_oob()
1413 ret_code = mtd_read_oob_std(mtd, from, ops); in mtd_read_oob()
1415 mtd_update_ecc_stats(mtd, master, &old_stats); in mtd_read_oob()
1418 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics in mtd_read_oob()
1419 * similar to mtd->_read(), returning a non-negative integer in mtd_read_oob()
1420 * representing max bitflips. In other cases, mtd->_read_oob() may in mtd_read_oob()
1425 if (mtd->ecc_strength == 0) in mtd_read_oob()
1427 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; in mtd_read_oob()
1431 int mtd_write_oob(struct mtd_info *mtd, loff_t to, in mtd_write_oob() argument
1434 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_oob()
1439 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_write_oob()
1442 ret = mtd_check_oob_ops(mtd, to, ops); in mtd_write_oob()
1448 /* Check the validity of a potential fallback on mtd->_write */ in mtd_write_oob()
1452 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_write_oob()
1453 return mtd_io_emulated_slc(mtd, to, false, ops); in mtd_write_oob()
1455 return mtd_write_oob_std(mtd, to, ops); in mtd_write_oob()
1461 * @mtd: MTD device structure
1471 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1475 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, in mtd_ooblayout_ecc() argument
1478 struct mtd_info *master = mtd_get_master(mtd); in mtd_ooblayout_ecc()
1495 * @mtd: MTD device structure
1505 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1509 int mtd_ooblayout_free(struct mtd_info *mtd, int section, in mtd_ooblayout_free() argument
1512 struct mtd_info *master = mtd_get_master(mtd); in mtd_ooblayout_free()
1528 * @mtd: mtd info structure
1539 * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1543 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, in mtd_ooblayout_find_region() argument
1554 ret = iter(mtd, section, oobregion); in mtd_ooblayout_find_region()
1579 * @mtd: mtd info structure
1589 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, in mtd_ooblayout_find_eccregion() argument
1593 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, in mtd_ooblayout_find_eccregion()
1600 * @mtd: mtd info structure
1612 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, in mtd_ooblayout_get_bytes() argument
1621 ret = mtd_ooblayout_find_region(mtd, start, &section, in mtd_ooblayout_get_bytes()
1635 ret = iter(mtd, ++section, &oobregion); in mtd_ooblayout_get_bytes()
1643 * @mtd: mtd info structure
1655 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, in mtd_ooblayout_set_bytes() argument
1664 ret = mtd_ooblayout_find_region(mtd, start, &section, in mtd_ooblayout_set_bytes()
1678 ret = iter(mtd, ++section, &oobregion); in mtd_ooblayout_set_bytes()
1686 * @mtd: mtd info structure
1693 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd, in mtd_ooblayout_count_bytes() argument
1702 ret = iter(mtd, section++, &oobregion); in mtd_ooblayout_count_bytes()
1717 * @mtd: mtd info structure
1727 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, in mtd_ooblayout_get_eccbytes() argument
1730 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, in mtd_ooblayout_get_eccbytes()
1737 * @mtd: mtd info structure
1747 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, in mtd_ooblayout_set_eccbytes() argument
1750 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, in mtd_ooblayout_set_eccbytes()
1757 * @mtd: mtd info structure
1767 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, in mtd_ooblayout_get_databytes() argument
1770 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, in mtd_ooblayout_get_databytes()
1777 * @mtd: mtd info structure
1787 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, in mtd_ooblayout_set_databytes() argument
1790 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, in mtd_ooblayout_set_databytes()
1797 * @mtd: mtd info structure
1803 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd) in mtd_ooblayout_count_freebytes() argument
1805 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); in mtd_ooblayout_count_freebytes()
1811 * @mtd: mtd info structure
1817 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) in mtd_ooblayout_count_eccbytes() argument
1819 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); in mtd_ooblayout_count_eccbytes()
1828 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, in mtd_get_fact_prot_info() argument
1831 struct mtd_info *master = mtd_get_master(mtd); in mtd_get_fact_prot_info()
1841 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, in mtd_read_fact_prot_reg() argument
1844 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_fact_prot_reg()
1855 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, in mtd_get_user_prot_info() argument
1858 struct mtd_info *master = mtd_get_master(mtd); in mtd_get_user_prot_info()
1868 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, in mtd_read_user_prot_reg() argument
1871 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_user_prot_reg()
1882 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, in mtd_write_user_prot_reg() argument
1885 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_user_prot_reg()
1905 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) in mtd_lock_user_prot_reg() argument
1907 struct mtd_info *master = mtd_get_master(mtd); in mtd_lock_user_prot_reg()
1918 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_lock() argument
1920 struct mtd_info *master = mtd_get_master(mtd); in mtd_lock()
1924 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_lock()
1929 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_lock()
1930 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_lock()
1931 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_lock()
1934 return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_lock()
1938 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_unlock() argument
1940 struct mtd_info *master = mtd_get_master(mtd); in mtd_unlock()
1944 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_unlock()
1949 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_unlock()
1950 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_unlock()
1951 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_unlock()
1954 return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_unlock()
1958 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_is_locked() argument
1960 struct mtd_info *master = mtd_get_master(mtd); in mtd_is_locked()
1964 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_is_locked()
1969 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_is_locked()
1970 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_is_locked()
1971 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_is_locked()
1974 return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_is_locked()
1978 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) in mtd_block_isreserved() argument
1980 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_isreserved()
1982 if (ofs < 0 || ofs >= mtd->size) in mtd_block_isreserved()
1987 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_isreserved()
1988 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_isreserved()
1990 return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_isreserved()
1994 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) in mtd_block_isbad() argument
1996 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_isbad()
1998 if (ofs < 0 || ofs >= mtd->size) in mtd_block_isbad()
2003 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_isbad()
2004 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_isbad()
2006 return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_isbad()
2010 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) in mtd_block_markbad() argument
2012 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_markbad()
2017 if (ofs < 0 || ofs >= mtd->size) in mtd_block_markbad()
2019 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_block_markbad()
2022 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_markbad()
2023 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_markbad()
2025 ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_markbad()
2029 while (mtd->parent) { in mtd_block_markbad()
2030 mtd->ecc_stats.badblocks++; in mtd_block_markbad()
2031 mtd = mtd->parent; in mtd_block_markbad()
2040 * @mtd: mtd device description object pointer
2043 * @to: the MTD device offset to write to
2044 * @retlen: on exit contains the count of bytes written to the MTD device.
2049 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, in default_mtd_writev() argument
2059 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, in default_mtd_writev()
2071 * mtd_writev - the vector-based MTD write method
2072 * @mtd: mtd device description object pointer
2075 * @to: the MTD device offset to write to
2076 * @retlen: on exit contains the count of bytes written to the MTD device.
2081 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, in mtd_writev() argument
2084 struct mtd_info *master = mtd_get_master(mtd); in mtd_writev()
2087 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_writev()
2091 return default_mtd_writev(mtd, vecs, count, to, retlen); in mtd_writev()
2094 mtd_get_master_ofs(mtd, to), retlen); in mtd_writev()
2100 * @mtd: mtd device description object pointer
2113 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
2122 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) in mtd_kmalloc_up_to() argument
2125 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); in mtd_kmalloc_up_to()
2136 *size = ALIGN(*size, mtd->writesize); in mtd_kmalloc_up_to()
2150 /* Support for /proc/mtd */
2154 struct mtd_info *mtd; in mtd_proc_show() local
2158 mtd_for_each_device(mtd) { in mtd_proc_show()
2159 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", in mtd_proc_show()
2160 mtd->index, (unsigned long long)mtd->size, in mtd_proc_show()
2161 mtd->erasesize, mtd->name); in mtd_proc_show()
2203 mtd_bdi = mtd_bdi_init("mtd"); in init_mtd()
2209 proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show); in init_mtd()
2215 dfs_dir_mtd = debugfs_create_dir("mtd", NULL); in init_mtd()
2221 remove_proc_entry("mtd", NULL); in init_mtd()
2226 pr_err("Error registering mtd class or bdi: %d\n", ret); in init_mtd()
2235 remove_proc_entry("mtd", NULL); in cleanup_mtd()
2246 MODULE_DESCRIPTION("Core MTD registration and access routines");