Lines Matching full:mtd

3  * Core registration and callback routines for MTD
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/partitions.h>
46 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_cls_suspend() local
48 return mtd ? mtd_suspend(mtd) : 0; in mtd_cls_suspend()
53 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_cls_resume() local
55 if (mtd) in mtd_cls_resume()
56 mtd_resume(mtd); in mtd_cls_resume()
67 .name = "mtd",
89 /* REVISIT once MTD uses the driver model better, whoever allocates
94 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_release() local
95 dev_t index = MTD_DEVT(mtd->index); in mtd_release()
97 idr_remove(&mtd_idr, mtd->index); in mtd_release()
98 of_node_put(mtd_get_of_node(mtd)); in mtd_release()
100 if (mtd_is_partition(mtd)) in mtd_release()
101 release_mtd_partition(mtd); in mtd_release()
109 struct mtd_info *mtd = container_of(kref, struct mtd_info, refcnt); in mtd_device_release() local
110 bool is_partition = mtd_is_partition(mtd); in mtd_device_release()
112 debugfs_remove_recursive(mtd->dbg.dfs_dir); in mtd_device_release()
115 nvmem_unregister(mtd->nvmem); in mtd_device_release()
117 device_unregister(&mtd->dev); in mtd_device_release()
120 * Clear dev so mtd can be safely re-registered later if desired. in mtd_device_release()
125 memset(&mtd->dev, 0, sizeof(mtd->dev)); in mtd_device_release()
139 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_type_show() local
142 switch (mtd->type) { in mtd_type_show()
178 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_flags_show() local
180 return sysfs_emit(buf, "0x%lx\n", (unsigned long)mtd->flags); in mtd_flags_show()
187 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_size_show() local
189 return sysfs_emit(buf, "%llu\n", (unsigned long long)mtd->size); in mtd_size_show()
196 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_erasesize_show() local
198 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->erasesize); in mtd_erasesize_show()
205 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_writesize_show() local
207 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->writesize); in mtd_writesize_show()
214 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_subpagesize_show() local
215 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; in mtd_subpagesize_show()
224 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_oobsize_show() local
226 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->oobsize); in mtd_oobsize_show()
233 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_oobavail_show() local
235 return sysfs_emit(buf, "%u\n", mtd->oobavail); in mtd_oobavail_show()
242 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_numeraseregions_show() local
244 return sysfs_emit(buf, "%u\n", mtd->numeraseregions); in mtd_numeraseregions_show()
251 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_name_show() local
253 return sysfs_emit(buf, "%s\n", mtd->name); in mtd_name_show()
260 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_strength_show() local
262 return sysfs_emit(buf, "%u\n", mtd->ecc_strength); in mtd_ecc_strength_show()
270 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bitflip_threshold_show() local
272 return sysfs_emit(buf, "%u\n", mtd->bitflip_threshold); in mtd_bitflip_threshold_show()
279 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bitflip_threshold_store() local
287 mtd->bitflip_threshold = bitflip_threshold; in mtd_bitflip_threshold_store()
295 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_step_size_show() local
297 return sysfs_emit(buf, "%u\n", mtd->ecc_step_size); in mtd_ecc_step_size_show()
305 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_corrected_bits_show() local
306 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_corrected_bits_show()
315 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_failures_show() local
316 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_ecc_failures_show()
325 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bad_blocks_show() local
326 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_bad_blocks_show()
335 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bbt_blocks_show() local
336 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_bbt_blocks_show()
362 ATTRIBUTE_GROUPS(mtd);
365 .name = "mtd",
387 static void mtd_debugfs_populate(struct mtd_info *mtd) in mtd_debugfs_populate() argument
389 struct device *dev = &mtd->dev; in mtd_debugfs_populate()
394 mtd->dbg.dfs_dir = debugfs_create_dir(dev_name(dev), dfs_dir_mtd); in mtd_debugfs_populate()
398 unsigned mtd_mmap_capabilities(struct mtd_info *mtd) in mtd_mmap_capabilities() argument
400 switch (mtd->type) { in mtd_mmap_capabilities()
417 struct mtd_info *mtd; in mtd_reboot_notifier() local
419 mtd = container_of(n, struct mtd_info, reboot_notifier); in mtd_reboot_notifier()
420 mtd->_reboot(mtd); in mtd_reboot_notifier()
427 * @mtd: pointer to new MTD device info structure
442 * From the pairing info the MTD user can find all the wunits paired with
445 * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
447 * mtd_pairing_info_to_wunit(mtd, &info);
451 int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit, in mtd_wunit_to_pairing_info() argument
454 struct mtd_info *master = mtd_get_master(mtd); in mtd_wunit_to_pairing_info()
472 * @mtd: pointer to new MTD device info structure
487 * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
489 * wunit = mtd_pairing_info_to_wunit(mtd, &info);
490 * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
491 * mtd->writesize, &retlen, buf + (i * mtd->writesize));
494 int mtd_pairing_info_to_wunit(struct mtd_info *mtd, in mtd_pairing_info_to_wunit() argument
497 struct mtd_info *master = mtd_get_master(mtd); in mtd_pairing_info_to_wunit()
506 return mtd->pairing->get_wunit(master, info); in mtd_pairing_info_to_wunit()
514 * @mtd: pointer to new MTD device info structure
522 int mtd_pairing_groups(struct mtd_info *mtd) in mtd_pairing_groups() argument
524 struct mtd_info *master = mtd_get_master(mtd); in mtd_pairing_groups()
536 struct mtd_info *mtd = priv; in mtd_nvmem_reg_read() local
540 err = mtd_read(mtd, offset, bytes, &retlen, val); in mtd_nvmem_reg_read()
547 static int mtd_nvmem_add(struct mtd_info *mtd) in mtd_nvmem_add() argument
549 struct device_node *node = mtd_get_of_node(mtd); in mtd_nvmem_add()
553 config.dev = &mtd->dev; in mtd_nvmem_add()
554 config.name = dev_name(&mtd->dev); in mtd_nvmem_add()
558 config.size = mtd->size; in mtd_nvmem_add()
564 config.priv = mtd; in mtd_nvmem_add()
566 mtd->nvmem = nvmem_register(&config); in mtd_nvmem_add()
567 if (IS_ERR(mtd->nvmem)) { in mtd_nvmem_add()
569 if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) in mtd_nvmem_add()
570 mtd->nvmem = NULL; in mtd_nvmem_add()
572 return dev_err_probe(&mtd->dev, PTR_ERR(mtd->nvmem), in mtd_nvmem_add()
579 static void mtd_check_of_node(struct mtd_info *mtd) in mtd_check_of_node() argument
585 /* Check if MTD already has a device node */ in mtd_check_of_node()
586 if (mtd_get_of_node(mtd)) in mtd_check_of_node()
589 if (!mtd_is_partition(mtd)) in mtd_check_of_node()
592 parent_dn = of_node_get(mtd_get_of_node(mtd->parent)); in mtd_check_of_node()
596 if (mtd_is_partition(mtd->parent)) in mtd_check_of_node()
604 mtd_name_len = strlen(mtd->name); in mtd_check_of_node()
622 !strncmp(mtd->name, pname + offset, plen)) { in mtd_check_of_node()
623 mtd_set_of_node(mtd, mtd_dn); in mtd_check_of_node()
635 * add_mtd_device - register an MTD device
636 * @mtd: pointer to new MTD device info structure
638 * Add a device to the list of MTD devices present in the system, and
639 * notify each currently active MTD 'user' of its arrival. Returns
643 int add_mtd_device(struct mtd_info *mtd) in add_mtd_device() argument
645 struct device_node *np = mtd_get_of_node(mtd); in add_mtd_device()
646 struct mtd_info *master = mtd_get_master(mtd); in add_mtd_device()
652 * mtd_device_parse_register() multiple times on the same master MTD, in add_mtd_device()
655 if (WARN_ONCE(mtd->dev.type, "MTD already registered\n")) in add_mtd_device()
658 BUG_ON(mtd->writesize == 0); in add_mtd_device()
661 * MTD drivers should implement ->_{write,read}() or in add_mtd_device()
664 if (WARN_ON((mtd->_write && mtd->_write_oob) || in add_mtd_device()
665 (mtd->_read && mtd->_read_oob))) in add_mtd_device()
668 if (WARN_ON((!mtd->erasesize || !master->_erase) && in add_mtd_device()
669 !(mtd->flags & MTD_NO_ERASE))) in add_mtd_device()
680 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION && in add_mtd_device()
681 (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH || in add_mtd_device()
689 ofidx = of_alias_get_id(np, "mtd"); in add_mtd_device()
691 i = idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL); in add_mtd_device()
693 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); in add_mtd_device()
699 mtd->index = i; in add_mtd_device()
700 kref_init(&mtd->refcnt); in add_mtd_device()
703 if (mtd->bitflip_threshold == 0) in add_mtd_device()
704 mtd->bitflip_threshold = mtd->ecc_strength; in add_mtd_device()
706 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in add_mtd_device()
709 mtd->erasesize /= ngroups; in add_mtd_device()
710 mtd->size = (u64)mtd_div_by_eb(mtd->size, master) * in add_mtd_device()
711 mtd->erasesize; in add_mtd_device()
714 if (is_power_of_2(mtd->erasesize)) in add_mtd_device()
715 mtd->erasesize_shift = ffs(mtd->erasesize) - 1; in add_mtd_device()
717 mtd->erasesize_shift = 0; in add_mtd_device()
719 if (is_power_of_2(mtd->writesize)) in add_mtd_device()
720 mtd->writesize_shift = ffs(mtd->writesize) - 1; in add_mtd_device()
722 mtd->writesize_shift = 0; in add_mtd_device()
724 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; in add_mtd_device()
725 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; in add_mtd_device()
728 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { in add_mtd_device()
729 error = mtd_unlock(mtd, 0, mtd->size); in add_mtd_device()
733 mtd->name); in add_mtd_device()
741 mtd->dev.type = &mtd_devtype; in add_mtd_device()
742 mtd->dev.class = &mtd_class; in add_mtd_device()
743 mtd->dev.devt = MTD_DEVT(i); in add_mtd_device()
744 dev_set_name(&mtd->dev, "mtd%d", i); in add_mtd_device()
745 dev_set_drvdata(&mtd->dev, mtd); in add_mtd_device()
746 mtd_check_of_node(mtd); in add_mtd_device()
747 of_node_get(mtd_get_of_node(mtd)); in add_mtd_device()
748 error = device_register(&mtd->dev); in add_mtd_device()
750 put_device(&mtd->dev); in add_mtd_device()
755 error = mtd_nvmem_add(mtd); in add_mtd_device()
759 mtd_debugfs_populate(mtd); in add_mtd_device()
761 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, in add_mtd_device()
762 "mtd%dro", i); in add_mtd_device()
764 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); in add_mtd_device()
768 not->add(mtd); in add_mtd_device()
772 if (of_property_read_bool(mtd_get_of_node(mtd), "linux,rootfs")) { in add_mtd_device()
774 pr_info("mtd: setting mtd%d (%s) as root device\n", mtd->index, mtd->name); in add_mtd_device()
775 ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, mtd->index); in add_mtd_device()
777 pr_warn("mtd: can't set mtd%d (%s) as root device - mtd must be builtin\n", in add_mtd_device()
778 mtd->index, mtd->name); in add_mtd_device()
790 device_unregister(&mtd->dev); in add_mtd_device()
792 of_node_put(mtd_get_of_node(mtd)); in add_mtd_device()
800 * del_mtd_device - unregister an MTD device
801 * @mtd: pointer to MTD device info structure
803 * Remove a device from the list of MTD devices present in the system,
804 * and notify each currently active MTD 'user' of its departure.
809 int del_mtd_device(struct mtd_info *mtd) in del_mtd_device() argument
816 if (idr_find(&mtd_idr, mtd->index) != mtd) { in del_mtd_device()
824 not->remove(mtd); in del_mtd_device()
826 kref_put(&mtd->refcnt, mtd_device_release); in del_mtd_device()
838 static void mtd_set_dev_defaults(struct mtd_info *mtd) in mtd_set_dev_defaults() argument
840 if (mtd->dev.parent) { in mtd_set_dev_defaults()
841 if (!mtd->owner && mtd->dev.parent->driver) in mtd_set_dev_defaults()
842 mtd->owner = mtd->dev.parent->driver->owner; in mtd_set_dev_defaults()
843 if (!mtd->name) in mtd_set_dev_defaults()
844 mtd->name = dev_name(mtd->dev.parent); in mtd_set_dev_defaults()
846 pr_debug("mtd device won't show a device symlink in sysfs\n"); in mtd_set_dev_defaults()
849 INIT_LIST_HEAD(&mtd->partitions); in mtd_set_dev_defaults()
850 mutex_init(&mtd->master.partitions_lock); in mtd_set_dev_defaults()
851 mutex_init(&mtd->master.chrdev_lock); in mtd_set_dev_defaults()
854 static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user) in mtd_otp_size() argument
867 ret = mtd_get_user_prot_info(mtd, PAGE_SIZE, &retlen, info); in mtd_otp_size()
869 ret = mtd_get_fact_prot_info(mtd, PAGE_SIZE, &retlen, info); in mtd_otp_size()
886 static struct nvmem_device *mtd_otp_nvmem_register(struct mtd_info *mtd, in mtd_otp_nvmem_register() argument
896 np = of_get_compatible_child(mtd->dev.of_node, compatible); in mtd_otp_nvmem_register()
899 config.dev = mtd->dev.parent; in mtd_otp_nvmem_register()
903 config.add_legacy_fixed_of_cells = !mtd_type_is_nand(mtd); in mtd_otp_nvmem_register()
910 config.priv = mtd; in mtd_otp_nvmem_register()
925 struct mtd_info *mtd = priv; in mtd_nvmem_user_otp_reg_read() local
929 ret = mtd_read_user_prot_reg(mtd, offset, bytes, &retlen, val); in mtd_nvmem_user_otp_reg_read()
939 struct mtd_info *mtd = priv; in mtd_nvmem_fact_otp_reg_read() local
943 ret = mtd_read_fact_prot_reg(mtd, offset, bytes, &retlen, val); in mtd_nvmem_fact_otp_reg_read()
950 static int mtd_otp_nvmem_add(struct mtd_info *mtd) in mtd_otp_nvmem_add() argument
952 struct device *dev = mtd->dev.parent; in mtd_otp_nvmem_add()
957 if (mtd->_get_user_prot_info && mtd->_read_user_prot_reg) { in mtd_otp_nvmem_add()
958 size = mtd_otp_size(mtd, true); in mtd_otp_nvmem_add()
965 nvmem = mtd_otp_nvmem_register(mtd, "user-otp", size, in mtd_otp_nvmem_add()
971 mtd->otp_user_nvmem = nvmem; in mtd_otp_nvmem_add()
975 if (mtd->_get_fact_prot_info && mtd->_read_fact_prot_reg) { in mtd_otp_nvmem_add()
976 size = mtd_otp_size(mtd, false); in mtd_otp_nvmem_add()
995 err = mtd_nvmem_fact_otp_reg_read(mtd, 0, otp, size); in mtd_otp_nvmem_add()
1003 nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size, in mtd_otp_nvmem_add()
1009 mtd->otp_factory_nvmem = nvmem; in mtd_otp_nvmem_add()
1016 nvmem_unregister(mtd->otp_user_nvmem); in mtd_otp_nvmem_add()
1024 * mtd_device_parse_register - parse partitions and register an MTD device.
1026 * @mtd: the MTD device to register
1027 * @types: the list of MTD partition probes to try, see
1029 * @parser_data: MTD partition parser-specific data
1033 * MTD device is registered if no partition info is found
1035 * This function aggregates MTD partitions parsing (done by
1036 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
1037 * basically follows the most common pattern found in many MTD drivers:
1041 * * Then It tries to probe partitions on MTD device @mtd using parsers
1046 * * If no partitions were found this function just registers the MTD device
1047 * @mtd and exits.
1051 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, in mtd_device_parse_register() argument
1058 mtd_set_dev_defaults(mtd); in mtd_device_parse_register()
1060 ret = mtd_otp_nvmem_add(mtd); in mtd_device_parse_register()
1065 ret = add_mtd_device(mtd); in mtd_device_parse_register()
1071 ret = parse_mtd_partitions(mtd, types, parser_data); in mtd_device_parse_register()
1078 ret = add_mtd_partitions(mtd, parts, nr_parts); in mtd_device_parse_register()
1079 else if (!device_is_registered(&mtd->dev)) in mtd_device_parse_register()
1080 ret = add_mtd_device(mtd); in mtd_device_parse_register()
1095 WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call, in mtd_device_parse_register()
1096 "MTD already registered\n"); in mtd_device_parse_register()
1097 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { in mtd_device_parse_register()
1098 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; in mtd_device_parse_register()
1099 register_reboot_notifier(&mtd->reboot_notifier); in mtd_device_parse_register()
1104 nvmem_unregister(mtd->otp_user_nvmem); in mtd_device_parse_register()
1105 nvmem_unregister(mtd->otp_factory_nvmem); in mtd_device_parse_register()
1108 if (ret && device_is_registered(&mtd->dev)) in mtd_device_parse_register()
1109 del_mtd_device(mtd); in mtd_device_parse_register()
1116 * mtd_device_unregister - unregister an existing MTD device.
1118 * @master: the MTD device to unregister. This will unregister both the master
1145 * register_mtd_user - register a 'user' of MTD devices.
1149 * or removal of MTD devices. Causes the 'add' callback to be immediately
1150 * invoked for each MTD device currently present in the system.
1154 struct mtd_info *mtd; in register_mtd_user() local
1162 mtd_for_each_device(mtd) in register_mtd_user()
1163 new->add(mtd); in register_mtd_user()
1170 * unregister_mtd_user - unregister a 'user' of MTD devices.
1174 * notified upon addition or removal of MTD devices. Causes the
1175 * 'remove' callback to be immediately invoked for each MTD device
1180 struct mtd_info *mtd; in unregister_mtd_user() local
1186 mtd_for_each_device(mtd) in unregister_mtd_user()
1187 old->remove(mtd); in unregister_mtd_user()
1196 * get_mtd_device - obtain a validated handle for an MTD device
1197 * @mtd: last known address of the required MTD device
1198 * @num: internal device number of the required MTD device
1206 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) in get_mtd_device() argument
1215 if (other == mtd) { in get_mtd_device()
1216 ret = mtd; in get_mtd_device()
1222 if (mtd && mtd != ret) in get_mtd_device()
1241 int __get_mtd_device(struct mtd_info *mtd) in __get_mtd_device() argument
1243 struct mtd_info *master = mtd_get_master(mtd); in __get_mtd_device()
1247 err = master->_get_device(mtd); in __get_mtd_device()
1258 while (mtd) { in __get_mtd_device()
1259 if (mtd != master) in __get_mtd_device()
1260 kref_get(&mtd->refcnt); in __get_mtd_device()
1261 mtd = mtd->parent; in __get_mtd_device()
1272 * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
1278 struct mtd_info *mtd = NULL; in of_get_mtd_device_by_node() local
1287 mtd = tmp; in of_get_mtd_device_by_node()
1288 err = __get_mtd_device(mtd); in of_get_mtd_device_by_node()
1295 return err ? ERR_PTR(err) : mtd; in of_get_mtd_device_by_node()
1300 * get_mtd_device_nm - obtain a validated handle for an MTD device by
1302 * @name: MTD device name to open
1304 * This function returns MTD device description structure in case of
1310 struct mtd_info *mtd = NULL, *other; in get_mtd_device_nm() local
1316 mtd = other; in get_mtd_device_nm()
1321 if (!mtd) in get_mtd_device_nm()
1324 err = __get_mtd_device(mtd); in get_mtd_device_nm()
1329 return mtd; in get_mtd_device_nm()
1337 void put_mtd_device(struct mtd_info *mtd) in put_mtd_device() argument
1340 __put_mtd_device(mtd); in put_mtd_device()
1346 void __put_mtd_device(struct mtd_info *mtd) in __put_mtd_device() argument
1348 struct mtd_info *master = mtd_get_master(mtd); in __put_mtd_device()
1350 while (mtd) { in __put_mtd_device()
1351 /* kref_put() can relese mtd, so keep a reference mtd->parent */ in __put_mtd_device()
1352 struct mtd_info *parent = mtd->parent; in __put_mtd_device()
1354 if (mtd != master) in __put_mtd_device()
1355 kref_put(&mtd->refcnt, mtd_device_release); in __put_mtd_device()
1356 mtd = parent; in __put_mtd_device()
1375 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) in mtd_erase() argument
1377 struct mtd_info *master = mtd_get_master(mtd); in mtd_erase()
1378 u64 mst_ofs = mtd_get_master_ofs(mtd, 0); in mtd_erase()
1385 if (!mtd->erasesize || !master->_erase) in mtd_erase()
1388 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) in mtd_erase()
1390 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_erase()
1398 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_erase()
1399 adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) * in mtd_erase()
1401 adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) * in mtd_erase()
1412 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_erase()
1415 instr->fail_addr *= mtd->erasesize; in mtd_erase()
1427 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, in mtd_point() argument
1430 struct mtd_info *master = mtd_get_master(mtd); in mtd_point()
1438 if (from < 0 || from >= mtd->size || len > mtd->size - from) in mtd_point()
1443 from = mtd_get_master_ofs(mtd, from); in mtd_point()
1449 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) in mtd_unpoint() argument
1451 struct mtd_info *master = mtd_get_master(mtd); in mtd_unpoint()
1455 if (from < 0 || from >= mtd->size || len > mtd->size - from) in mtd_unpoint()
1459 return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len); in mtd_unpoint()
1468 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, in mtd_get_unmapped_area() argument
1475 ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL); in mtd_get_unmapped_area()
1479 mtd_unpoint(mtd, offset, retlen); in mtd_get_unmapped_area()
1486 static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master, in mtd_update_ecc_stats() argument
1491 if (master == mtd) in mtd_update_ecc_stats()
1498 while (mtd->parent) { in mtd_update_ecc_stats()
1499 mtd->ecc_stats.failed += diff.failed; in mtd_update_ecc_stats()
1500 mtd->ecc_stats.corrected += diff.corrected; in mtd_update_ecc_stats()
1501 mtd = mtd->parent; in mtd_update_ecc_stats()
1505 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, in mtd_read() argument
1514 ret = mtd_read_oob(mtd, from, &ops); in mtd_read()
1524 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, in mtd_write() argument
1533 ret = mtd_write_oob(mtd, to, &ops); in mtd_write()
1548 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, in mtd_panic_write() argument
1551 struct mtd_info *master = mtd_get_master(mtd); in mtd_panic_write()
1556 if (to < 0 || to >= mtd->size || len > mtd->size - to) in mtd_panic_write()
1558 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_panic_write()
1565 return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len, in mtd_panic_write()
1570 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs, in mtd_check_oob_ops() argument
1584 if (offs < 0 || offs + ops->len > mtd->size) in mtd_check_oob_ops()
1590 if (ops->ooboffs >= mtd_oobavail(mtd, ops)) in mtd_check_oob_ops()
1593 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) - in mtd_check_oob_ops()
1594 mtd_div_by_ws(offs, mtd)) * in mtd_check_oob_ops()
1595 mtd_oobavail(mtd, ops)) - ops->ooboffs; in mtd_check_oob_ops()
1603 static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from, in mtd_read_oob_std() argument
1606 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_oob_std()
1609 from = mtd_get_master_ofs(mtd, from); in mtd_read_oob_std()
1619 static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to, in mtd_write_oob_std() argument
1622 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_oob_std()
1625 to = mtd_get_master_ofs(mtd, to); in mtd_write_oob_std()
1635 static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read, in mtd_io_emulated_slc() argument
1638 struct mtd_info *master = mtd_get_master(mtd); in mtd_io_emulated_slc()
1648 ebofs = mtd_mod_by_eb(start, mtd); in mtd_io_emulated_slc()
1649 base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize; in mtd_io_emulated_slc()
1651 info.pair = mtd_div_by_ws(ebofs, mtd); in mtd_io_emulated_slc()
1652 pageofs = mtd_mod_by_ws(ebofs, mtd); in mtd_io_emulated_slc()
1653 oobavail = mtd_oobavail(mtd, ops); in mtd_io_emulated_slc()
1664 pos = mtd_wunit_to_offset(mtd, base, wunit); in mtd_io_emulated_slc()
1667 if (adjops.len > mtd->writesize - pageofs) in mtd_io_emulated_slc()
1668 adjops.len = mtd->writesize - pageofs; in mtd_io_emulated_slc()
1675 ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops); in mtd_io_emulated_slc()
1679 ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops); in mtd_io_emulated_slc()
1698 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) in mtd_read_oob() argument
1700 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_oob()
1706 ret_code = mtd_check_oob_ops(mtd, from, ops); in mtd_read_oob()
1712 /* Check the validity of a potential fallback on mtd->_read */ in mtd_read_oob()
1719 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_read_oob()
1720 ret_code = mtd_io_emulated_slc(mtd, from, true, ops); in mtd_read_oob()
1722 ret_code = mtd_read_oob_std(mtd, from, ops); in mtd_read_oob()
1724 mtd_update_ecc_stats(mtd, master, &old_stats); in mtd_read_oob()
1727 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics in mtd_read_oob()
1728 * similar to mtd->_read(), returning a non-negative integer in mtd_read_oob()
1729 * representing max bitflips. In other cases, mtd->_read_oob() may in mtd_read_oob()
1734 if (mtd->ecc_strength == 0) in mtd_read_oob()
1738 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; in mtd_read_oob()
1742 int mtd_write_oob(struct mtd_info *mtd, loff_t to, in mtd_write_oob() argument
1745 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_oob()
1750 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_write_oob()
1753 ret = mtd_check_oob_ops(mtd, to, ops); in mtd_write_oob()
1759 /* Check the validity of a potential fallback on mtd->_write */ in mtd_write_oob()
1763 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_write_oob()
1764 return mtd_io_emulated_slc(mtd, to, false, ops); in mtd_write_oob()
1766 return mtd_write_oob_std(mtd, to, ops); in mtd_write_oob()
1772 * @mtd: MTD device structure
1782 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1786 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, in mtd_ooblayout_ecc() argument
1789 struct mtd_info *master = mtd_get_master(mtd); in mtd_ooblayout_ecc()
1806 * @mtd: MTD device structure
1816 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1820 int mtd_ooblayout_free(struct mtd_info *mtd, int section, in mtd_ooblayout_free() argument
1823 struct mtd_info *master = mtd_get_master(mtd); in mtd_ooblayout_free()
1839 * @mtd: mtd info structure
1850 * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1854 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, in mtd_ooblayout_find_region() argument
1865 ret = iter(mtd, section, oobregion); in mtd_ooblayout_find_region()
1890 * @mtd: mtd info structure
1900 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, in mtd_ooblayout_find_eccregion() argument
1904 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, in mtd_ooblayout_find_eccregion()
1911 * @mtd: mtd info structure
1923 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, in mtd_ooblayout_get_bytes() argument
1932 ret = mtd_ooblayout_find_region(mtd, start, &section, in mtd_ooblayout_get_bytes()
1946 ret = iter(mtd, ++section, &oobregion); in mtd_ooblayout_get_bytes()
1954 * @mtd: mtd info structure
1966 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, in mtd_ooblayout_set_bytes() argument
1975 ret = mtd_ooblayout_find_region(mtd, start, &section, in mtd_ooblayout_set_bytes()
1989 ret = iter(mtd, ++section, &oobregion); in mtd_ooblayout_set_bytes()
1997 * @mtd: mtd info structure
2004 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd, in mtd_ooblayout_count_bytes() argument
2013 ret = iter(mtd, section++, &oobregion); in mtd_ooblayout_count_bytes()
2028 * @mtd: mtd info structure
2038 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, in mtd_ooblayout_get_eccbytes() argument
2041 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, in mtd_ooblayout_get_eccbytes()
2048 * @mtd: mtd info structure
2058 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, in mtd_ooblayout_set_eccbytes() argument
2061 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, in mtd_ooblayout_set_eccbytes()
2068 * @mtd: mtd info structure
2078 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, in mtd_ooblayout_get_databytes() argument
2081 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, in mtd_ooblayout_get_databytes()
2088 * @mtd: mtd info structure
2098 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, in mtd_ooblayout_set_databytes() argument
2101 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, in mtd_ooblayout_set_databytes()
2108 * @mtd: mtd info structure
2114 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd) in mtd_ooblayout_count_freebytes() argument
2116 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); in mtd_ooblayout_count_freebytes()
2122 * @mtd: mtd info structure
2128 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) in mtd_ooblayout_count_eccbytes() argument
2130 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); in mtd_ooblayout_count_eccbytes()
2139 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, in mtd_get_fact_prot_info() argument
2142 struct mtd_info *master = mtd_get_master(mtd); in mtd_get_fact_prot_info()
2152 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, in mtd_read_fact_prot_reg() argument
2155 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_fact_prot_reg()
2166 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, in mtd_get_user_prot_info() argument
2169 struct mtd_info *master = mtd_get_master(mtd); in mtd_get_user_prot_info()
2179 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, in mtd_read_user_prot_reg() argument
2182 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_user_prot_reg()
2193 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, in mtd_write_user_prot_reg() argument
2196 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_user_prot_reg()
2216 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) in mtd_lock_user_prot_reg() argument
2218 struct mtd_info *master = mtd_get_master(mtd); in mtd_lock_user_prot_reg()
2228 int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) in mtd_erase_user_prot_reg() argument
2230 struct mtd_info *master = mtd_get_master(mtd); in mtd_erase_user_prot_reg()
2241 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_lock() argument
2243 struct mtd_info *master = mtd_get_master(mtd); in mtd_lock()
2247 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_lock()
2252 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_lock()
2253 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_lock()
2254 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_lock()
2257 return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_lock()
2261 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_unlock() argument
2263 struct mtd_info *master = mtd_get_master(mtd); in mtd_unlock()
2267 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_unlock()
2272 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_unlock()
2273 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_unlock()
2274 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_unlock()
2277 return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_unlock()
2281 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_is_locked() argument
2283 struct mtd_info *master = mtd_get_master(mtd); in mtd_is_locked()
2287 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_is_locked()
2292 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_is_locked()
2293 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_is_locked()
2294 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_is_locked()
2297 return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_is_locked()
2301 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) in mtd_block_isreserved() argument
2303 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_isreserved()
2305 if (ofs < 0 || ofs >= mtd->size) in mtd_block_isreserved()
2310 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_isreserved()
2311 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_isreserved()
2313 return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_isreserved()
2317 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) in mtd_block_isbad() argument
2319 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_isbad()
2321 if (ofs < 0 || ofs >= mtd->size) in mtd_block_isbad()
2326 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_isbad()
2327 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_isbad()
2329 return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_isbad()
2333 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) in mtd_block_markbad() argument
2335 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_markbad()
2340 if (ofs < 0 || ofs >= mtd->size) in mtd_block_markbad()
2342 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_block_markbad()
2345 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_markbad()
2346 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_markbad()
2348 ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_markbad()
2352 while (mtd->parent) { in mtd_block_markbad()
2353 mtd->ecc_stats.badblocks++; in mtd_block_markbad()
2354 mtd = mtd->parent; in mtd_block_markbad()
2364 * @mtd: mtd device description object pointer
2367 * @to: the MTD device offset to write to
2368 * @retlen: on exit contains the count of bytes written to the MTD device.
2373 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, in default_mtd_writev() argument
2383 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, in default_mtd_writev()
2395 * mtd_writev - the vector-based MTD write method
2396 * @mtd: mtd device description object pointer
2399 * @to: the MTD device offset to write to
2400 * @retlen: on exit contains the count of bytes written to the MTD device.
2405 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, in mtd_writev() argument
2408 struct mtd_info *master = mtd_get_master(mtd); in mtd_writev()
2411 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_writev()
2415 return default_mtd_writev(mtd, vecs, count, to, retlen); in mtd_writev()
2418 mtd_get_master_ofs(mtd, to), retlen); in mtd_writev()
2424 * @mtd: mtd device description object pointer
2437 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
2446 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) in mtd_kmalloc_up_to() argument
2449 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); in mtd_kmalloc_up_to()
2460 *size = ALIGN(*size, mtd->writesize); in mtd_kmalloc_up_to()
2474 /* Support for /proc/mtd */
2478 struct mtd_info *mtd; in mtd_proc_show() local
2482 mtd_for_each_device(mtd) { in mtd_proc_show()
2483 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", in mtd_proc_show()
2484 mtd->index, (unsigned long long)mtd->size, in mtd_proc_show()
2485 mtd->erasesize, mtd->name); in mtd_proc_show()
2527 mtd_bdi = mtd_bdi_init("mtd"); in init_mtd()
2533 proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show); in init_mtd()
2539 dfs_dir_mtd = debugfs_create_dir("mtd", NULL); in init_mtd()
2547 remove_proc_entry("mtd", NULL); in init_mtd()
2553 pr_err("Error registering mtd class or bdi: %d\n", ret); in init_mtd()
2562 remove_proc_entry("mtd", NULL); in cleanup_mtd()
2574 MODULE_DESCRIPTION("Core MTD registration and access routines");