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()
108 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_type_show() local
111 switch (mtd->type) { in mtd_type_show()
147 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_flags_show() local
149 return sysfs_emit(buf, "0x%lx\n", (unsigned long)mtd->flags); in mtd_flags_show()
156 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_size_show() local
158 return sysfs_emit(buf, "%llu\n", (unsigned long long)mtd->size); in mtd_size_show()
165 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_erasesize_show() local
167 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->erasesize); in mtd_erasesize_show()
174 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_writesize_show() local
176 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->writesize); in mtd_writesize_show()
183 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_subpagesize_show() local
184 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; in mtd_subpagesize_show()
193 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_oobsize_show() local
195 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->oobsize); in mtd_oobsize_show()
202 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_oobavail_show() local
204 return sysfs_emit(buf, "%u\n", mtd->oobavail); in mtd_oobavail_show()
211 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_numeraseregions_show() local
213 return sysfs_emit(buf, "%u\n", mtd->numeraseregions); in mtd_numeraseregions_show()
220 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_name_show() local
222 return sysfs_emit(buf, "%s\n", mtd->name); in mtd_name_show()
229 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_strength_show() local
231 return sysfs_emit(buf, "%u\n", mtd->ecc_strength); in mtd_ecc_strength_show()
239 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bitflip_threshold_show() local
241 return sysfs_emit(buf, "%u\n", mtd->bitflip_threshold); in mtd_bitflip_threshold_show()
248 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bitflip_threshold_store() local
256 mtd->bitflip_threshold = bitflip_threshold; in mtd_bitflip_threshold_store()
264 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_step_size_show() local
266 return sysfs_emit(buf, "%u\n", mtd->ecc_step_size); in mtd_ecc_step_size_show()
274 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_corrected_bits_show() local
275 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_corrected_bits_show()
284 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_ecc_failures_show() local
285 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_ecc_failures_show()
294 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bad_blocks_show() local
295 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_bad_blocks_show()
304 struct mtd_info *mtd = dev_get_drvdata(dev); in mtd_bbt_blocks_show() local
305 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; in mtd_bbt_blocks_show()
331 ATTRIBUTE_GROUPS(mtd);
334 .name = "mtd",
356 static void mtd_debugfs_populate(struct mtd_info *mtd) in mtd_debugfs_populate() argument
358 struct device *dev = &mtd->dev; in mtd_debugfs_populate()
363 mtd->dbg.dfs_dir = debugfs_create_dir(dev_name(dev), dfs_dir_mtd); in mtd_debugfs_populate()
367 unsigned mtd_mmap_capabilities(struct mtd_info *mtd) in mtd_mmap_capabilities() argument
369 switch (mtd->type) { in mtd_mmap_capabilities()
386 struct mtd_info *mtd; in mtd_reboot_notifier() local
388 mtd = container_of(n, struct mtd_info, reboot_notifier); in mtd_reboot_notifier()
389 mtd->_reboot(mtd); in mtd_reboot_notifier()
396 * @mtd: pointer to new MTD device info structure
411 * From the pairing info the MTD user can find all the wunits paired with
414 * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
416 * mtd_pairing_info_to_wunit(mtd, &info);
420 int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit, in mtd_wunit_to_pairing_info() argument
423 struct mtd_info *master = mtd_get_master(mtd); in mtd_wunit_to_pairing_info()
441 * @mtd: pointer to new MTD device info structure
456 * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
458 * wunit = mtd_pairing_info_to_wunit(mtd, &info);
459 * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
460 * mtd->writesize, &retlen, buf + (i * mtd->writesize));
463 int mtd_pairing_info_to_wunit(struct mtd_info *mtd, in mtd_pairing_info_to_wunit() argument
466 struct mtd_info *master = mtd_get_master(mtd); in mtd_pairing_info_to_wunit()
475 return mtd->pairing->get_wunit(master, info); in mtd_pairing_info_to_wunit()
483 * @mtd: pointer to new MTD device info structure
491 int mtd_pairing_groups(struct mtd_info *mtd) in mtd_pairing_groups() argument
493 struct mtd_info *master = mtd_get_master(mtd); in mtd_pairing_groups()
505 struct mtd_info *mtd = priv; in mtd_nvmem_reg_read() local
509 err = mtd_read(mtd, offset, bytes, &retlen, val); in mtd_nvmem_reg_read()
516 static int mtd_nvmem_add(struct mtd_info *mtd) in mtd_nvmem_add() argument
518 struct device_node *node = mtd_get_of_node(mtd); in mtd_nvmem_add()
522 config.dev = &mtd->dev; in mtd_nvmem_add()
523 config.name = dev_name(&mtd->dev); in mtd_nvmem_add()
526 config.size = mtd->size; in mtd_nvmem_add()
533 config.priv = mtd; in mtd_nvmem_add()
535 mtd->nvmem = nvmem_register(&config); in mtd_nvmem_add()
536 if (IS_ERR(mtd->nvmem)) { in mtd_nvmem_add()
538 if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) { in mtd_nvmem_add()
539 mtd->nvmem = NULL; in mtd_nvmem_add()
541 dev_err(&mtd->dev, "Failed to register NVMEM device\n"); in mtd_nvmem_add()
542 return PTR_ERR(mtd->nvmem); in mtd_nvmem_add()
549 static void mtd_check_of_node(struct mtd_info *mtd) in mtd_check_of_node() argument
557 /* Check if MTD already has a device node */ in mtd_check_of_node()
558 if (dev_of_node(&mtd->dev)) in mtd_check_of_node()
562 if (!mtd_is_partition(mtd)) in mtd_check_of_node()
564 parent = mtd->parent; in mtd_check_of_node()
574 mtd_name_len = strlen(mtd->name); in mtd_check_of_node()
592 !strncmp(mtd->name, pname + offset, plen)) { in mtd_check_of_node()
603 mtd_set_of_node(mtd, mtd_dn); in mtd_check_of_node()
612 * add_mtd_device - register an MTD device
613 * @mtd: pointer to new MTD device info structure
615 * Add a device to the list of MTD devices present in the system, and
616 * notify each currently active MTD 'user' of its arrival. Returns
620 int add_mtd_device(struct mtd_info *mtd) in add_mtd_device() argument
622 struct device_node *np = mtd_get_of_node(mtd); in add_mtd_device()
623 struct mtd_info *master = mtd_get_master(mtd); in add_mtd_device()
629 * mtd_device_parse_register() multiple times on the same master MTD, in add_mtd_device()
632 if (WARN_ONCE(mtd->dev.type, "MTD already registered\n")) in add_mtd_device()
635 BUG_ON(mtd->writesize == 0); in add_mtd_device()
638 * MTD drivers should implement ->_{write,read}() or in add_mtd_device()
641 if (WARN_ON((mtd->_write && mtd->_write_oob) || in add_mtd_device()
642 (mtd->_read && mtd->_read_oob))) in add_mtd_device()
645 if (WARN_ON((!mtd->erasesize || !master->_erase) && in add_mtd_device()
646 !(mtd->flags & MTD_NO_ERASE))) in add_mtd_device()
657 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION && in add_mtd_device()
658 (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH || in add_mtd_device()
666 ofidx = of_alias_get_id(np, "mtd"); in add_mtd_device()
668 i = idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL); in add_mtd_device()
670 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); in add_mtd_device()
676 mtd->index = i; in add_mtd_device()
677 mtd->usecount = 0; in add_mtd_device()
680 if (mtd->bitflip_threshold == 0) in add_mtd_device()
681 mtd->bitflip_threshold = mtd->ecc_strength; in add_mtd_device()
683 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in add_mtd_device()
686 mtd->erasesize /= ngroups; in add_mtd_device()
687 mtd->size = (u64)mtd_div_by_eb(mtd->size, master) * in add_mtd_device()
688 mtd->erasesize; in add_mtd_device()
691 if (is_power_of_2(mtd->erasesize)) in add_mtd_device()
692 mtd->erasesize_shift = ffs(mtd->erasesize) - 1; in add_mtd_device()
694 mtd->erasesize_shift = 0; in add_mtd_device()
696 if (is_power_of_2(mtd->writesize)) in add_mtd_device()
697 mtd->writesize_shift = ffs(mtd->writesize) - 1; in add_mtd_device()
699 mtd->writesize_shift = 0; in add_mtd_device()
701 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; in add_mtd_device()
702 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; in add_mtd_device()
705 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { in add_mtd_device()
706 error = mtd_unlock(mtd, 0, mtd->size); in add_mtd_device()
710 mtd->name); in add_mtd_device()
718 mtd->dev.type = &mtd_devtype; in add_mtd_device()
719 mtd->dev.class = &mtd_class; in add_mtd_device()
720 mtd->dev.devt = MTD_DEVT(i); in add_mtd_device()
721 dev_set_name(&mtd->dev, "mtd%d", i); in add_mtd_device()
722 dev_set_drvdata(&mtd->dev, mtd); in add_mtd_device()
723 mtd_check_of_node(mtd); in add_mtd_device()
724 of_node_get(mtd_get_of_node(mtd)); in add_mtd_device()
725 error = device_register(&mtd->dev); in add_mtd_device()
730 error = mtd_nvmem_add(mtd); in add_mtd_device()
734 mtd_debugfs_populate(mtd); in add_mtd_device()
736 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, in add_mtd_device()
737 "mtd%dro", i); in add_mtd_device()
739 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); in add_mtd_device()
743 not->add(mtd); in add_mtd_device()
754 device_unregister(&mtd->dev); in add_mtd_device()
756 of_node_put(mtd_get_of_node(mtd)); in add_mtd_device()
764 * del_mtd_device - unregister an MTD device
765 * @mtd: pointer to MTD device info structure
767 * Remove a device from the list of MTD devices present in the system,
768 * and notify each currently active MTD 'user' of its departure.
773 int del_mtd_device(struct mtd_info *mtd) in del_mtd_device() argument
780 if (idr_find(&mtd_idr, mtd->index) != mtd) { in del_mtd_device()
788 not->remove(mtd); in del_mtd_device()
790 if (mtd->usecount) { in del_mtd_device()
791 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", in del_mtd_device()
792 mtd->index, mtd->name, mtd->usecount); in del_mtd_device()
795 debugfs_remove_recursive(mtd->dbg.dfs_dir); in del_mtd_device()
798 nvmem_unregister(mtd->nvmem); in del_mtd_device()
800 device_unregister(&mtd->dev); in del_mtd_device()
802 /* Clear dev so mtd can be safely re-registered later if desired */ in del_mtd_device()
803 memset(&mtd->dev, 0, sizeof(mtd->dev)); in del_mtd_device()
805 idr_remove(&mtd_idr, mtd->index); in del_mtd_device()
806 of_node_put(mtd_get_of_node(mtd)); in del_mtd_device()
821 static void mtd_set_dev_defaults(struct mtd_info *mtd) in mtd_set_dev_defaults() argument
823 if (mtd->dev.parent) { in mtd_set_dev_defaults()
824 if (!mtd->owner && mtd->dev.parent->driver) in mtd_set_dev_defaults()
825 mtd->owner = mtd->dev.parent->driver->owner; in mtd_set_dev_defaults()
826 if (!mtd->name) in mtd_set_dev_defaults()
827 mtd->name = dev_name(mtd->dev.parent); in mtd_set_dev_defaults()
829 pr_debug("mtd device won't show a device symlink in sysfs\n"); in mtd_set_dev_defaults()
832 INIT_LIST_HEAD(&mtd->partitions); in mtd_set_dev_defaults()
833 mutex_init(&mtd->master.partitions_lock); in mtd_set_dev_defaults()
834 mutex_init(&mtd->master.chrdev_lock); in mtd_set_dev_defaults()
837 static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user) in mtd_otp_size() argument
850 ret = mtd_get_user_prot_info(mtd, PAGE_SIZE, &retlen, info); in mtd_otp_size()
852 ret = mtd_get_fact_prot_info(mtd, PAGE_SIZE, &retlen, info); in mtd_otp_size()
869 static struct nvmem_device *mtd_otp_nvmem_register(struct mtd_info *mtd, in mtd_otp_nvmem_register() argument
879 np = of_get_compatible_child(mtd->dev.of_node, compatible); in mtd_otp_nvmem_register()
882 config.dev = mtd->dev.parent; in mtd_otp_nvmem_register()
883 config.name = kasprintf(GFP_KERNEL, "%s-%s", dev_name(&mtd->dev), compatible); in mtd_otp_nvmem_register()
892 config.priv = mtd; in mtd_otp_nvmem_register()
908 struct mtd_info *mtd = priv; in mtd_nvmem_user_otp_reg_read() local
912 ret = mtd_read_user_prot_reg(mtd, offset, bytes, &retlen, val); in mtd_nvmem_user_otp_reg_read()
922 struct mtd_info *mtd = priv; in mtd_nvmem_fact_otp_reg_read() local
926 ret = mtd_read_fact_prot_reg(mtd, offset, bytes, &retlen, val); in mtd_nvmem_fact_otp_reg_read()
933 static int mtd_otp_nvmem_add(struct mtd_info *mtd) in mtd_otp_nvmem_add() argument
939 if (mtd->_get_user_prot_info && mtd->_read_user_prot_reg) { in mtd_otp_nvmem_add()
940 size = mtd_otp_size(mtd, true); in mtd_otp_nvmem_add()
945 nvmem = mtd_otp_nvmem_register(mtd, "user-otp", size, in mtd_otp_nvmem_add()
948 dev_err(&mtd->dev, "Failed to register OTP NVMEM device\n"); in mtd_otp_nvmem_add()
951 mtd->otp_user_nvmem = nvmem; in mtd_otp_nvmem_add()
955 if (mtd->_get_fact_prot_info && mtd->_read_fact_prot_reg) { in mtd_otp_nvmem_add()
956 size = mtd_otp_size(mtd, false); in mtd_otp_nvmem_add()
963 nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size, in mtd_otp_nvmem_add()
966 dev_err(&mtd->dev, "Failed to register OTP NVMEM device\n"); in mtd_otp_nvmem_add()
970 mtd->otp_factory_nvmem = nvmem; in mtd_otp_nvmem_add()
977 nvmem_unregister(mtd->otp_user_nvmem); in mtd_otp_nvmem_add()
982 * mtd_device_parse_register - parse partitions and register an MTD device.
984 * @mtd: the MTD device to register
985 * @types: the list of MTD partition probes to try, see
987 * @parser_data: MTD partition parser-specific data
991 * MTD device is registered if no partition info is found
993 * This function aggregates MTD partitions parsing (done by
994 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
995 * basically follows the most common pattern found in many MTD drivers:
999 * * Then It tries to probe partitions on MTD device @mtd using parsers
1004 * * If no partitions were found this function just registers the MTD device
1005 * @mtd and exits.
1009 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, in mtd_device_parse_register() argument
1016 mtd_set_dev_defaults(mtd); in mtd_device_parse_register()
1019 ret = add_mtd_device(mtd); in mtd_device_parse_register()
1025 ret = parse_mtd_partitions(mtd, types, parser_data); in mtd_device_parse_register()
1032 ret = add_mtd_partitions(mtd, parts, nr_parts); in mtd_device_parse_register()
1033 else if (!device_is_registered(&mtd->dev)) in mtd_device_parse_register()
1034 ret = add_mtd_device(mtd); in mtd_device_parse_register()
1049 WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call, in mtd_device_parse_register()
1050 "MTD already registered\n"); in mtd_device_parse_register()
1051 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { in mtd_device_parse_register()
1052 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; in mtd_device_parse_register()
1053 register_reboot_notifier(&mtd->reboot_notifier); in mtd_device_parse_register()
1056 ret = mtd_otp_nvmem_add(mtd); in mtd_device_parse_register()
1059 if (ret && device_is_registered(&mtd->dev)) in mtd_device_parse_register()
1060 del_mtd_device(mtd); in mtd_device_parse_register()
1067 * mtd_device_unregister - unregister an existing MTD device.
1069 * @master: the MTD device to unregister. This will unregister both the master
1096 * register_mtd_user - register a 'user' of MTD devices.
1100 * or removal of MTD devices. Causes the 'add' callback to be immediately
1101 * invoked for each MTD device currently present in the system.
1105 struct mtd_info *mtd; in register_mtd_user() local
1113 mtd_for_each_device(mtd) in register_mtd_user()
1114 new->add(mtd); in register_mtd_user()
1121 * unregister_mtd_user - unregister a 'user' of MTD devices.
1125 * notified upon addition or removal of MTD devices. Causes the
1126 * 'remove' callback to be immediately invoked for each MTD device
1131 struct mtd_info *mtd; in unregister_mtd_user() local
1137 mtd_for_each_device(mtd) in unregister_mtd_user()
1138 old->remove(mtd); in unregister_mtd_user()
1147 * get_mtd_device - obtain a validated handle for an MTD device
1148 * @mtd: last known address of the required MTD device
1149 * @num: internal device number of the required MTD device
1157 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) in get_mtd_device() argument
1166 if (other == mtd) { in get_mtd_device()
1167 ret = mtd; in get_mtd_device()
1173 if (mtd && mtd != ret) in get_mtd_device()
1192 int __get_mtd_device(struct mtd_info *mtd) in __get_mtd_device() argument
1194 struct mtd_info *master = mtd_get_master(mtd); in __get_mtd_device()
1201 err = master->_get_device(mtd); in __get_mtd_device()
1211 while (mtd->parent) { in __get_mtd_device()
1212 mtd->usecount++; in __get_mtd_device()
1213 mtd = mtd->parent; in __get_mtd_device()
1221 * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
1227 struct mtd_info *mtd = NULL; in of_get_mtd_device_by_node() local
1236 mtd = tmp; in of_get_mtd_device_by_node()
1237 err = __get_mtd_device(mtd); in of_get_mtd_device_by_node()
1244 return err ? ERR_PTR(err) : mtd; in of_get_mtd_device_by_node()
1249 * get_mtd_device_nm - obtain a validated handle for an MTD device by
1251 * @name: MTD device name to open
1253 * This function returns MTD device description structure in case of
1259 struct mtd_info *mtd = NULL, *other; in get_mtd_device_nm() local
1265 mtd = other; in get_mtd_device_nm()
1270 if (!mtd) in get_mtd_device_nm()
1273 err = __get_mtd_device(mtd); in get_mtd_device_nm()
1278 return mtd; in get_mtd_device_nm()
1286 void put_mtd_device(struct mtd_info *mtd) in put_mtd_device() argument
1289 __put_mtd_device(mtd); in put_mtd_device()
1295 void __put_mtd_device(struct mtd_info *mtd) in __put_mtd_device() argument
1297 struct mtd_info *master = mtd_get_master(mtd); in __put_mtd_device()
1299 while (mtd->parent) { in __put_mtd_device()
1300 --mtd->usecount; in __put_mtd_device()
1301 BUG_ON(mtd->usecount < 0); in __put_mtd_device()
1302 mtd = mtd->parent; in __put_mtd_device()
1319 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) in mtd_erase() argument
1321 struct mtd_info *master = mtd_get_master(mtd); in mtd_erase()
1322 u64 mst_ofs = mtd_get_master_ofs(mtd, 0); in mtd_erase()
1329 if (!mtd->erasesize || !master->_erase) in mtd_erase()
1332 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) in mtd_erase()
1334 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_erase()
1342 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_erase()
1343 adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) * in mtd_erase()
1345 adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) * in mtd_erase()
1356 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_erase()
1359 instr->fail_addr *= mtd->erasesize; in mtd_erase()
1370 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, in mtd_point() argument
1373 struct mtd_info *master = mtd_get_master(mtd); in mtd_point()
1381 if (from < 0 || from >= mtd->size || len > mtd->size - from) in mtd_point()
1386 from = mtd_get_master_ofs(mtd, from); in mtd_point()
1392 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) in mtd_unpoint() argument
1394 struct mtd_info *master = mtd_get_master(mtd); in mtd_unpoint()
1398 if (from < 0 || from >= mtd->size || len > mtd->size - from) in mtd_unpoint()
1402 return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len); in mtd_unpoint()
1411 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, in mtd_get_unmapped_area() argument
1418 ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL); in mtd_get_unmapped_area()
1422 mtd_unpoint(mtd, offset, retlen); in mtd_get_unmapped_area()
1429 static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master, in mtd_update_ecc_stats() argument
1434 if (master == mtd) in mtd_update_ecc_stats()
1441 while (mtd->parent) { in mtd_update_ecc_stats()
1442 mtd->ecc_stats.failed += diff.failed; in mtd_update_ecc_stats()
1443 mtd->ecc_stats.corrected += diff.corrected; in mtd_update_ecc_stats()
1444 mtd = mtd->parent; in mtd_update_ecc_stats()
1448 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, in mtd_read() argument
1457 ret = mtd_read_oob(mtd, from, &ops); in mtd_read()
1464 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, in mtd_write() argument
1473 ret = mtd_write_oob(mtd, to, &ops); in mtd_write()
1487 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, in mtd_panic_write() argument
1490 struct mtd_info *master = mtd_get_master(mtd); in mtd_panic_write()
1495 if (to < 0 || to >= mtd->size || len > mtd->size - to) in mtd_panic_write()
1497 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_panic_write()
1504 return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len, in mtd_panic_write()
1509 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs, in mtd_check_oob_ops() argument
1523 if (offs < 0 || offs + ops->len > mtd->size) in mtd_check_oob_ops()
1529 if (ops->ooboffs >= mtd_oobavail(mtd, ops)) in mtd_check_oob_ops()
1532 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) - in mtd_check_oob_ops()
1533 mtd_div_by_ws(offs, mtd)) * in mtd_check_oob_ops()
1534 mtd_oobavail(mtd, ops)) - ops->ooboffs; in mtd_check_oob_ops()
1542 static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from, in mtd_read_oob_std() argument
1545 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_oob_std()
1548 from = mtd_get_master_ofs(mtd, from); in mtd_read_oob_std()
1558 static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to, in mtd_write_oob_std() argument
1561 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_oob_std()
1564 to = mtd_get_master_ofs(mtd, to); in mtd_write_oob_std()
1574 static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read, in mtd_io_emulated_slc() argument
1577 struct mtd_info *master = mtd_get_master(mtd); in mtd_io_emulated_slc()
1587 ebofs = mtd_mod_by_eb(start, mtd); in mtd_io_emulated_slc()
1588 base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize; in mtd_io_emulated_slc()
1590 info.pair = mtd_div_by_ws(ebofs, mtd); in mtd_io_emulated_slc()
1591 pageofs = mtd_mod_by_ws(ebofs, mtd); in mtd_io_emulated_slc()
1592 oobavail = mtd_oobavail(mtd, ops); in mtd_io_emulated_slc()
1603 pos = mtd_wunit_to_offset(mtd, base, wunit); in mtd_io_emulated_slc()
1606 if (adjops.len > mtd->writesize - pageofs) in mtd_io_emulated_slc()
1607 adjops.len = mtd->writesize - pageofs; in mtd_io_emulated_slc()
1614 ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops); in mtd_io_emulated_slc()
1618 ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops); in mtd_io_emulated_slc()
1637 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) in mtd_read_oob() argument
1639 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_oob()
1645 ret_code = mtd_check_oob_ops(mtd, from, ops); in mtd_read_oob()
1651 /* Check the validity of a potential fallback on mtd->_read */ in mtd_read_oob()
1658 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_read_oob()
1659 ret_code = mtd_io_emulated_slc(mtd, from, true, ops); in mtd_read_oob()
1661 ret_code = mtd_read_oob_std(mtd, from, ops); in mtd_read_oob()
1663 mtd_update_ecc_stats(mtd, master, &old_stats); in mtd_read_oob()
1666 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics in mtd_read_oob()
1667 * similar to mtd->_read(), returning a non-negative integer in mtd_read_oob()
1668 * representing max bitflips. In other cases, mtd->_read_oob() may in mtd_read_oob()
1673 if (mtd->ecc_strength == 0) in mtd_read_oob()
1677 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; in mtd_read_oob()
1681 int mtd_write_oob(struct mtd_info *mtd, loff_t to, in mtd_write_oob() argument
1684 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_oob()
1689 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_write_oob()
1692 ret = mtd_check_oob_ops(mtd, to, ops); in mtd_write_oob()
1698 /* Check the validity of a potential fallback on mtd->_write */ in mtd_write_oob()
1702 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_write_oob()
1703 return mtd_io_emulated_slc(mtd, to, false, ops); in mtd_write_oob()
1705 return mtd_write_oob_std(mtd, to, ops); in mtd_write_oob()
1711 * @mtd: MTD device structure
1721 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1725 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, in mtd_ooblayout_ecc() argument
1728 struct mtd_info *master = mtd_get_master(mtd); in mtd_ooblayout_ecc()
1745 * @mtd: MTD device structure
1755 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1759 int mtd_ooblayout_free(struct mtd_info *mtd, int section, in mtd_ooblayout_free() argument
1762 struct mtd_info *master = mtd_get_master(mtd); in mtd_ooblayout_free()
1778 * @mtd: mtd info structure
1789 * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
1793 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, in mtd_ooblayout_find_region() argument
1804 ret = iter(mtd, section, oobregion); in mtd_ooblayout_find_region()
1829 * @mtd: mtd info structure
1839 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, in mtd_ooblayout_find_eccregion() argument
1843 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, in mtd_ooblayout_find_eccregion()
1850 * @mtd: mtd info structure
1862 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, in mtd_ooblayout_get_bytes() argument
1871 ret = mtd_ooblayout_find_region(mtd, start, &section, in mtd_ooblayout_get_bytes()
1885 ret = iter(mtd, ++section, &oobregion); in mtd_ooblayout_get_bytes()
1893 * @mtd: mtd info structure
1905 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, in mtd_ooblayout_set_bytes() argument
1914 ret = mtd_ooblayout_find_region(mtd, start, &section, in mtd_ooblayout_set_bytes()
1928 ret = iter(mtd, ++section, &oobregion); in mtd_ooblayout_set_bytes()
1936 * @mtd: mtd info structure
1943 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd, in mtd_ooblayout_count_bytes() argument
1952 ret = iter(mtd, section++, &oobregion); in mtd_ooblayout_count_bytes()
1967 * @mtd: mtd info structure
1977 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, in mtd_ooblayout_get_eccbytes() argument
1980 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, in mtd_ooblayout_get_eccbytes()
1987 * @mtd: mtd info structure
1997 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, in mtd_ooblayout_set_eccbytes() argument
2000 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, in mtd_ooblayout_set_eccbytes()
2007 * @mtd: mtd info structure
2017 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, in mtd_ooblayout_get_databytes() argument
2020 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, in mtd_ooblayout_get_databytes()
2027 * @mtd: mtd info structure
2037 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, in mtd_ooblayout_set_databytes() argument
2040 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, in mtd_ooblayout_set_databytes()
2047 * @mtd: mtd info structure
2053 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd) in mtd_ooblayout_count_freebytes() argument
2055 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); in mtd_ooblayout_count_freebytes()
2061 * @mtd: mtd info structure
2067 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) in mtd_ooblayout_count_eccbytes() argument
2069 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); in mtd_ooblayout_count_eccbytes()
2078 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, in mtd_get_fact_prot_info() argument
2081 struct mtd_info *master = mtd_get_master(mtd); in mtd_get_fact_prot_info()
2091 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, in mtd_read_fact_prot_reg() argument
2094 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_fact_prot_reg()
2105 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, in mtd_get_user_prot_info() argument
2108 struct mtd_info *master = mtd_get_master(mtd); in mtd_get_user_prot_info()
2118 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, in mtd_read_user_prot_reg() argument
2121 struct mtd_info *master = mtd_get_master(mtd); in mtd_read_user_prot_reg()
2132 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, in mtd_write_user_prot_reg() argument
2135 struct mtd_info *master = mtd_get_master(mtd); in mtd_write_user_prot_reg()
2155 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) in mtd_lock_user_prot_reg() argument
2157 struct mtd_info *master = mtd_get_master(mtd); in mtd_lock_user_prot_reg()
2167 int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) in mtd_erase_user_prot_reg() argument
2169 struct mtd_info *master = mtd_get_master(mtd); in mtd_erase_user_prot_reg()
2180 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_lock() argument
2182 struct mtd_info *master = mtd_get_master(mtd); in mtd_lock()
2186 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_lock()
2191 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_lock()
2192 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_lock()
2193 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_lock()
2196 return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_lock()
2200 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_unlock() argument
2202 struct mtd_info *master = mtd_get_master(mtd); in mtd_unlock()
2206 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_unlock()
2211 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_unlock()
2212 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_unlock()
2213 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_unlock()
2216 return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_unlock()
2220 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) in mtd_is_locked() argument
2222 struct mtd_info *master = mtd_get_master(mtd); in mtd_is_locked()
2226 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) in mtd_is_locked()
2231 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { in mtd_is_locked()
2232 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_is_locked()
2233 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; in mtd_is_locked()
2236 return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len); in mtd_is_locked()
2240 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) in mtd_block_isreserved() argument
2242 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_isreserved()
2244 if (ofs < 0 || ofs >= mtd->size) in mtd_block_isreserved()
2249 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_isreserved()
2250 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_isreserved()
2252 return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_isreserved()
2256 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) in mtd_block_isbad() argument
2258 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_isbad()
2260 if (ofs < 0 || ofs >= mtd->size) in mtd_block_isbad()
2265 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_isbad()
2266 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_isbad()
2268 return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_isbad()
2272 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) in mtd_block_markbad() argument
2274 struct mtd_info *master = mtd_get_master(mtd); in mtd_block_markbad()
2279 if (ofs < 0 || ofs >= mtd->size) in mtd_block_markbad()
2281 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_block_markbad()
2284 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) in mtd_block_markbad()
2285 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; in mtd_block_markbad()
2287 ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs)); in mtd_block_markbad()
2291 while (mtd->parent) { in mtd_block_markbad()
2292 mtd->ecc_stats.badblocks++; in mtd_block_markbad()
2293 mtd = mtd->parent; in mtd_block_markbad()
2302 * @mtd: mtd device description object pointer
2305 * @to: the MTD device offset to write to
2306 * @retlen: on exit contains the count of bytes written to the MTD device.
2311 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, in default_mtd_writev() argument
2321 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, in default_mtd_writev()
2333 * mtd_writev - the vector-based MTD write method
2334 * @mtd: mtd device description object pointer
2337 * @to: the MTD device offset to write to
2338 * @retlen: on exit contains the count of bytes written to the MTD device.
2343 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, in mtd_writev() argument
2346 struct mtd_info *master = mtd_get_master(mtd); in mtd_writev()
2349 if (!(mtd->flags & MTD_WRITEABLE)) in mtd_writev()
2353 return default_mtd_writev(mtd, vecs, count, to, retlen); in mtd_writev()
2356 mtd_get_master_ofs(mtd, to), retlen); in mtd_writev()
2362 * @mtd: mtd device description object pointer
2375 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
2384 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) in mtd_kmalloc_up_to() argument
2387 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); in mtd_kmalloc_up_to()
2398 *size = ALIGN(*size, mtd->writesize); in mtd_kmalloc_up_to()
2412 /* Support for /proc/mtd */
2416 struct mtd_info *mtd; in mtd_proc_show() local
2420 mtd_for_each_device(mtd) { in mtd_proc_show()
2421 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", in mtd_proc_show()
2422 mtd->index, (unsigned long long)mtd->size, in mtd_proc_show()
2423 mtd->erasesize, mtd->name); in mtd_proc_show()
2465 mtd_bdi = mtd_bdi_init("mtd"); in init_mtd()
2471 proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show); in init_mtd()
2477 dfs_dir_mtd = debugfs_create_dir("mtd", NULL); in init_mtd()
2485 remove_proc_entry("mtd", NULL); in init_mtd()
2490 pr_err("Error registering mtd class or bdi: %d\n", ret); in init_mtd()
2499 remove_proc_entry("mtd", NULL); in cleanup_mtd()
2511 MODULE_DESCRIPTION("Core MTD registration and access routines");