1e29c22f5SKyungmin Park /* 2e29c22f5SKyungmin Park * Core registration and callback routines for MTD 3e29c22f5SKyungmin Park * drivers and users. 4e29c22f5SKyungmin Park * 5ff94bc40SHeiko Schocher * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> 6ff94bc40SHeiko Schocher * Copyright © 2006 Red Hat UK Limited 7ff94bc40SHeiko Schocher * 8ff94bc40SHeiko Schocher * SPDX-License-Identifier: GPL-2.0+ 9ff94bc40SHeiko Schocher * 10e29c22f5SKyungmin Park */ 11e29c22f5SKyungmin Park 12ff94bc40SHeiko Schocher #define __UBOOT__ 13ff94bc40SHeiko Schocher #ifndef __UBOOT__ 14ff94bc40SHeiko Schocher #include <linux/module.h> 15ff94bc40SHeiko Schocher #include <linux/kernel.h> 16ff94bc40SHeiko Schocher #include <linux/ptrace.h> 17ff94bc40SHeiko Schocher #include <linux/seq_file.h> 18ff94bc40SHeiko Schocher #include <linux/string.h> 19ff94bc40SHeiko Schocher #include <linux/timer.h> 20ff94bc40SHeiko Schocher #include <linux/major.h> 21ff94bc40SHeiko Schocher #include <linux/fs.h> 22ff94bc40SHeiko Schocher #include <linux/err.h> 23ff94bc40SHeiko Schocher #include <linux/ioctl.h> 24ff94bc40SHeiko Schocher #include <linux/init.h> 25ff94bc40SHeiko Schocher #include <linux/proc_fs.h> 26ff94bc40SHeiko Schocher #include <linux/idr.h> 27ff94bc40SHeiko Schocher #include <linux/backing-dev.h> 28ff94bc40SHeiko Schocher #include <linux/gfp.h> 29ff94bc40SHeiko Schocher #include <linux/slab.h> 30ff94bc40SHeiko Schocher #else 317b15e2bbSMike Frysinger #include <linux/compat.h> 32ff94bc40SHeiko Schocher #include <linux/err.h> 33e29c22f5SKyungmin Park #include <ubi_uboot.h> 34ff94bc40SHeiko Schocher #endif 35e29c22f5SKyungmin Park 36ff94bc40SHeiko Schocher #include <linux/mtd/mtd.h> 37ff94bc40SHeiko Schocher #include <linux/mtd/partitions.h> 38ff94bc40SHeiko Schocher 39ff94bc40SHeiko Schocher #include "mtdcore.h" 40ff94bc40SHeiko Schocher 41ff94bc40SHeiko Schocher #ifndef __UBOOT__ 42ff94bc40SHeiko Schocher /* 43ff94bc40SHeiko Schocher * backing device capabilities for non-mappable devices (such as NAND flash) 44ff94bc40SHeiko Schocher * - permits private mappings, copies are taken of the data 45ff94bc40SHeiko Schocher */ 46ff94bc40SHeiko Schocher static struct backing_dev_info mtd_bdi_unmappable = { 47ff94bc40SHeiko Schocher .capabilities = BDI_CAP_MAP_COPY, 48ff94bc40SHeiko Schocher }; 49ff94bc40SHeiko Schocher 50ff94bc40SHeiko Schocher /* 51ff94bc40SHeiko Schocher * backing device capabilities for R/O mappable devices (such as ROM) 52ff94bc40SHeiko Schocher * - permits private mappings, copies are taken of the data 53ff94bc40SHeiko Schocher * - permits non-writable shared mappings 54ff94bc40SHeiko Schocher */ 55ff94bc40SHeiko Schocher static struct backing_dev_info mtd_bdi_ro_mappable = { 56ff94bc40SHeiko Schocher .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT | 57ff94bc40SHeiko Schocher BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP), 58ff94bc40SHeiko Schocher }; 59ff94bc40SHeiko Schocher 60ff94bc40SHeiko Schocher /* 61ff94bc40SHeiko Schocher * backing device capabilities for writable mappable devices (such as RAM) 62ff94bc40SHeiko Schocher * - permits private mappings, copies are taken of the data 63ff94bc40SHeiko Schocher * - permits non-writable shared mappings 64ff94bc40SHeiko Schocher */ 65ff94bc40SHeiko Schocher static struct backing_dev_info mtd_bdi_rw_mappable = { 66ff94bc40SHeiko Schocher .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT | 67ff94bc40SHeiko Schocher BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP | 68ff94bc40SHeiko Schocher BDI_CAP_WRITE_MAP), 69ff94bc40SHeiko Schocher }; 70ff94bc40SHeiko Schocher 71ff94bc40SHeiko Schocher static int mtd_cls_suspend(struct device *dev, pm_message_t state); 72ff94bc40SHeiko Schocher static int mtd_cls_resume(struct device *dev); 73ff94bc40SHeiko Schocher 74ff94bc40SHeiko Schocher static struct class mtd_class = { 75ff94bc40SHeiko Schocher .name = "mtd", 76ff94bc40SHeiko Schocher .owner = THIS_MODULE, 77ff94bc40SHeiko Schocher .suspend = mtd_cls_suspend, 78ff94bc40SHeiko Schocher .resume = mtd_cls_resume, 79ff94bc40SHeiko Schocher }; 80ff94bc40SHeiko Schocher #else 81e29c22f5SKyungmin Park struct mtd_info *mtd_table[MAX_MTD_DEVICES]; 82e29c22f5SKyungmin Park 83ff94bc40SHeiko Schocher #define MAX_IDR_ID 64 84ff94bc40SHeiko Schocher 85ff94bc40SHeiko Schocher struct idr_layer { 86ff94bc40SHeiko Schocher int used; 87ff94bc40SHeiko Schocher void *ptr; 88ff94bc40SHeiko Schocher }; 89ff94bc40SHeiko Schocher 90ff94bc40SHeiko Schocher struct idr { 91ff94bc40SHeiko Schocher struct idr_layer id[MAX_IDR_ID]; 92ff94bc40SHeiko Schocher }; 93ff94bc40SHeiko Schocher 94ff94bc40SHeiko Schocher #define DEFINE_IDR(name) struct idr name; 95ff94bc40SHeiko Schocher 96ff94bc40SHeiko Schocher void idr_remove(struct idr *idp, int id) 97ff94bc40SHeiko Schocher { 98ff94bc40SHeiko Schocher if (idp->id[id].used) 99ff94bc40SHeiko Schocher idp->id[id].used = 0; 100ff94bc40SHeiko Schocher 101ff94bc40SHeiko Schocher return; 102ff94bc40SHeiko Schocher } 103ff94bc40SHeiko Schocher void *idr_find(struct idr *idp, int id) 104ff94bc40SHeiko Schocher { 105ff94bc40SHeiko Schocher if (idp->id[id].used) 106ff94bc40SHeiko Schocher return idp->id[id].ptr; 107ff94bc40SHeiko Schocher 108ff94bc40SHeiko Schocher return NULL; 109ff94bc40SHeiko Schocher } 110ff94bc40SHeiko Schocher 111ff94bc40SHeiko Schocher void *idr_get_next(struct idr *idp, int *next) 112ff94bc40SHeiko Schocher { 113ff94bc40SHeiko Schocher void *ret; 114ff94bc40SHeiko Schocher int id = *next; 115ff94bc40SHeiko Schocher 116ff94bc40SHeiko Schocher ret = idr_find(idp, id); 117ff94bc40SHeiko Schocher if (ret) { 118ff94bc40SHeiko Schocher id ++; 119ff94bc40SHeiko Schocher if (!idp->id[id].used) 120ff94bc40SHeiko Schocher id = 0; 121ff94bc40SHeiko Schocher *next = id; 122ff94bc40SHeiko Schocher } else { 123ff94bc40SHeiko Schocher *next = 0; 124ff94bc40SHeiko Schocher } 125ff94bc40SHeiko Schocher 126ff94bc40SHeiko Schocher return ret; 127ff94bc40SHeiko Schocher } 128ff94bc40SHeiko Schocher 129ff94bc40SHeiko Schocher int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask) 130ff94bc40SHeiko Schocher { 131ff94bc40SHeiko Schocher struct idr_layer *idl; 132ff94bc40SHeiko Schocher int i = 0; 133ff94bc40SHeiko Schocher 134ff94bc40SHeiko Schocher while (i < MAX_IDR_ID) { 135ff94bc40SHeiko Schocher idl = &idp->id[i]; 136ff94bc40SHeiko Schocher if (idl->used == 0) { 137ff94bc40SHeiko Schocher idl->used = 1; 138ff94bc40SHeiko Schocher idl->ptr = ptr; 139ff94bc40SHeiko Schocher return i; 140ff94bc40SHeiko Schocher } 141ff94bc40SHeiko Schocher i++; 142ff94bc40SHeiko Schocher } 143ff94bc40SHeiko Schocher return -ENOSPC; 144ff94bc40SHeiko Schocher } 145ff94bc40SHeiko Schocher #endif 146ff94bc40SHeiko Schocher 147ff94bc40SHeiko Schocher static DEFINE_IDR(mtd_idr); 148ff94bc40SHeiko Schocher 149ff94bc40SHeiko Schocher /* These are exported solely for the purpose of mtd_blkdevs.c. You 150ff94bc40SHeiko Schocher should not use them for _anything_ else */ 151ff94bc40SHeiko Schocher DEFINE_MUTEX(mtd_table_mutex); 152ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_table_mutex); 153ff94bc40SHeiko Schocher 154ff94bc40SHeiko Schocher struct mtd_info *__mtd_next_device(int i) 155ff94bc40SHeiko Schocher { 156ff94bc40SHeiko Schocher return idr_get_next(&mtd_idr, &i); 157ff94bc40SHeiko Schocher } 158ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(__mtd_next_device); 159ff94bc40SHeiko Schocher 160ff94bc40SHeiko Schocher #ifndef __UBOOT__ 161ff94bc40SHeiko Schocher static LIST_HEAD(mtd_notifiers); 162ff94bc40SHeiko Schocher 163ff94bc40SHeiko Schocher 164ff94bc40SHeiko Schocher #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) 165ff94bc40SHeiko Schocher 166ff94bc40SHeiko Schocher /* REVISIT once MTD uses the driver model better, whoever allocates 167ff94bc40SHeiko Schocher * the mtd_info will probably want to use the release() hook... 168ff94bc40SHeiko Schocher */ 169ff94bc40SHeiko Schocher static void mtd_release(struct device *dev) 170ff94bc40SHeiko Schocher { 171ff94bc40SHeiko Schocher struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev); 172ff94bc40SHeiko Schocher dev_t index = MTD_DEVT(mtd->index); 173ff94bc40SHeiko Schocher 174ff94bc40SHeiko Schocher /* remove /dev/mtdXro node if needed */ 175ff94bc40SHeiko Schocher if (index) 176ff94bc40SHeiko Schocher device_destroy(&mtd_class, index + 1); 177ff94bc40SHeiko Schocher } 178ff94bc40SHeiko Schocher 179ff94bc40SHeiko Schocher static int mtd_cls_suspend(struct device *dev, pm_message_t state) 180ff94bc40SHeiko Schocher { 181ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 182ff94bc40SHeiko Schocher 183ff94bc40SHeiko Schocher return mtd ? mtd_suspend(mtd) : 0; 184ff94bc40SHeiko Schocher } 185ff94bc40SHeiko Schocher 186ff94bc40SHeiko Schocher static int mtd_cls_resume(struct device *dev) 187ff94bc40SHeiko Schocher { 188ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 189ff94bc40SHeiko Schocher 190ff94bc40SHeiko Schocher if (mtd) 191ff94bc40SHeiko Schocher mtd_resume(mtd); 192ff94bc40SHeiko Schocher return 0; 193ff94bc40SHeiko Schocher } 194ff94bc40SHeiko Schocher 195ff94bc40SHeiko Schocher static ssize_t mtd_type_show(struct device *dev, 196ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 197ff94bc40SHeiko Schocher { 198ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 199ff94bc40SHeiko Schocher char *type; 200ff94bc40SHeiko Schocher 201ff94bc40SHeiko Schocher switch (mtd->type) { 202ff94bc40SHeiko Schocher case MTD_ABSENT: 203ff94bc40SHeiko Schocher type = "absent"; 204ff94bc40SHeiko Schocher break; 205ff94bc40SHeiko Schocher case MTD_RAM: 206ff94bc40SHeiko Schocher type = "ram"; 207ff94bc40SHeiko Schocher break; 208ff94bc40SHeiko Schocher case MTD_ROM: 209ff94bc40SHeiko Schocher type = "rom"; 210ff94bc40SHeiko Schocher break; 211ff94bc40SHeiko Schocher case MTD_NORFLASH: 212ff94bc40SHeiko Schocher type = "nor"; 213ff94bc40SHeiko Schocher break; 214ff94bc40SHeiko Schocher case MTD_NANDFLASH: 215ff94bc40SHeiko Schocher type = "nand"; 216ff94bc40SHeiko Schocher break; 217ff94bc40SHeiko Schocher case MTD_DATAFLASH: 218ff94bc40SHeiko Schocher type = "dataflash"; 219ff94bc40SHeiko Schocher break; 220ff94bc40SHeiko Schocher case MTD_UBIVOLUME: 221ff94bc40SHeiko Schocher type = "ubi"; 222ff94bc40SHeiko Schocher break; 223ff94bc40SHeiko Schocher case MTD_MLCNANDFLASH: 224ff94bc40SHeiko Schocher type = "mlc-nand"; 225ff94bc40SHeiko Schocher break; 226ff94bc40SHeiko Schocher default: 227ff94bc40SHeiko Schocher type = "unknown"; 228ff94bc40SHeiko Schocher } 229ff94bc40SHeiko Schocher 230ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%s\n", type); 231ff94bc40SHeiko Schocher } 232ff94bc40SHeiko Schocher static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); 233ff94bc40SHeiko Schocher 234ff94bc40SHeiko Schocher static ssize_t mtd_flags_show(struct device *dev, 235ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 236ff94bc40SHeiko Schocher { 237ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 238ff94bc40SHeiko Schocher 239ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); 240ff94bc40SHeiko Schocher 241ff94bc40SHeiko Schocher } 242ff94bc40SHeiko Schocher static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); 243ff94bc40SHeiko Schocher 244ff94bc40SHeiko Schocher static ssize_t mtd_size_show(struct device *dev, 245ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 246ff94bc40SHeiko Schocher { 247ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 248ff94bc40SHeiko Schocher 249ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%llu\n", 250ff94bc40SHeiko Schocher (unsigned long long)mtd->size); 251ff94bc40SHeiko Schocher 252ff94bc40SHeiko Schocher } 253ff94bc40SHeiko Schocher static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); 254ff94bc40SHeiko Schocher 255ff94bc40SHeiko Schocher static ssize_t mtd_erasesize_show(struct device *dev, 256ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 257ff94bc40SHeiko Schocher { 258ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 259ff94bc40SHeiko Schocher 260ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); 261ff94bc40SHeiko Schocher 262ff94bc40SHeiko Schocher } 263ff94bc40SHeiko Schocher static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); 264ff94bc40SHeiko Schocher 265ff94bc40SHeiko Schocher static ssize_t mtd_writesize_show(struct device *dev, 266ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 267ff94bc40SHeiko Schocher { 268ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 269ff94bc40SHeiko Schocher 270ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); 271ff94bc40SHeiko Schocher 272ff94bc40SHeiko Schocher } 273ff94bc40SHeiko Schocher static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); 274ff94bc40SHeiko Schocher 275ff94bc40SHeiko Schocher static ssize_t mtd_subpagesize_show(struct device *dev, 276ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 277ff94bc40SHeiko Schocher { 278ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 279ff94bc40SHeiko Schocher unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; 280ff94bc40SHeiko Schocher 281ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); 282ff94bc40SHeiko Schocher 283ff94bc40SHeiko Schocher } 284ff94bc40SHeiko Schocher static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); 285ff94bc40SHeiko Schocher 286ff94bc40SHeiko Schocher static ssize_t mtd_oobsize_show(struct device *dev, 287ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 288ff94bc40SHeiko Schocher { 289ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 290ff94bc40SHeiko Schocher 291ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); 292ff94bc40SHeiko Schocher 293ff94bc40SHeiko Schocher } 294ff94bc40SHeiko Schocher static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); 295ff94bc40SHeiko Schocher 296ff94bc40SHeiko Schocher static ssize_t mtd_numeraseregions_show(struct device *dev, 297ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 298ff94bc40SHeiko Schocher { 299ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 300ff94bc40SHeiko Schocher 301ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); 302ff94bc40SHeiko Schocher 303ff94bc40SHeiko Schocher } 304ff94bc40SHeiko Schocher static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, 305ff94bc40SHeiko Schocher NULL); 306ff94bc40SHeiko Schocher 307ff94bc40SHeiko Schocher static ssize_t mtd_name_show(struct device *dev, 308ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 309ff94bc40SHeiko Schocher { 310ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 311ff94bc40SHeiko Schocher 312ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); 313ff94bc40SHeiko Schocher 314ff94bc40SHeiko Schocher } 315ff94bc40SHeiko Schocher static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); 316ff94bc40SHeiko Schocher 317ff94bc40SHeiko Schocher static ssize_t mtd_ecc_strength_show(struct device *dev, 318ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 319ff94bc40SHeiko Schocher { 320ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 321ff94bc40SHeiko Schocher 322ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); 323ff94bc40SHeiko Schocher } 324ff94bc40SHeiko Schocher static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL); 325ff94bc40SHeiko Schocher 326ff94bc40SHeiko Schocher static ssize_t mtd_bitflip_threshold_show(struct device *dev, 327ff94bc40SHeiko Schocher struct device_attribute *attr, 328ff94bc40SHeiko Schocher char *buf) 329ff94bc40SHeiko Schocher { 330ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 331ff94bc40SHeiko Schocher 332ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); 333ff94bc40SHeiko Schocher } 334ff94bc40SHeiko Schocher 335ff94bc40SHeiko Schocher static ssize_t mtd_bitflip_threshold_store(struct device *dev, 336ff94bc40SHeiko Schocher struct device_attribute *attr, 337ff94bc40SHeiko Schocher const char *buf, size_t count) 338ff94bc40SHeiko Schocher { 339ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 340ff94bc40SHeiko Schocher unsigned int bitflip_threshold; 341ff94bc40SHeiko Schocher int retval; 342ff94bc40SHeiko Schocher 343ff94bc40SHeiko Schocher retval = kstrtouint(buf, 0, &bitflip_threshold); 344ff94bc40SHeiko Schocher if (retval) 345ff94bc40SHeiko Schocher return retval; 346ff94bc40SHeiko Schocher 347ff94bc40SHeiko Schocher mtd->bitflip_threshold = bitflip_threshold; 348ff94bc40SHeiko Schocher return count; 349ff94bc40SHeiko Schocher } 350ff94bc40SHeiko Schocher static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR, 351ff94bc40SHeiko Schocher mtd_bitflip_threshold_show, 352ff94bc40SHeiko Schocher mtd_bitflip_threshold_store); 353ff94bc40SHeiko Schocher 354ff94bc40SHeiko Schocher static ssize_t mtd_ecc_step_size_show(struct device *dev, 355ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf) 356ff94bc40SHeiko Schocher { 357ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev); 358ff94bc40SHeiko Schocher 359ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); 360ff94bc40SHeiko Schocher 361ff94bc40SHeiko Schocher } 362ff94bc40SHeiko Schocher static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL); 363ff94bc40SHeiko Schocher 364ff94bc40SHeiko Schocher static struct attribute *mtd_attrs[] = { 365ff94bc40SHeiko Schocher &dev_attr_type.attr, 366ff94bc40SHeiko Schocher &dev_attr_flags.attr, 367ff94bc40SHeiko Schocher &dev_attr_size.attr, 368ff94bc40SHeiko Schocher &dev_attr_erasesize.attr, 369ff94bc40SHeiko Schocher &dev_attr_writesize.attr, 370ff94bc40SHeiko Schocher &dev_attr_subpagesize.attr, 371ff94bc40SHeiko Schocher &dev_attr_oobsize.attr, 372ff94bc40SHeiko Schocher &dev_attr_numeraseregions.attr, 373ff94bc40SHeiko Schocher &dev_attr_name.attr, 374ff94bc40SHeiko Schocher &dev_attr_ecc_strength.attr, 375ff94bc40SHeiko Schocher &dev_attr_ecc_step_size.attr, 376ff94bc40SHeiko Schocher &dev_attr_bitflip_threshold.attr, 377ff94bc40SHeiko Schocher NULL, 378ff94bc40SHeiko Schocher }; 379ff94bc40SHeiko Schocher ATTRIBUTE_GROUPS(mtd); 380ff94bc40SHeiko Schocher 381ff94bc40SHeiko Schocher static struct device_type mtd_devtype = { 382ff94bc40SHeiko Schocher .name = "mtd", 383ff94bc40SHeiko Schocher .groups = mtd_groups, 384ff94bc40SHeiko Schocher .release = mtd_release, 385ff94bc40SHeiko Schocher }; 386ff94bc40SHeiko Schocher #endif 387ff94bc40SHeiko Schocher 388ff94bc40SHeiko Schocher /** 389ff94bc40SHeiko Schocher * add_mtd_device - register an MTD device 390ff94bc40SHeiko Schocher * @mtd: pointer to new MTD device info structure 391ff94bc40SHeiko Schocher * 392ff94bc40SHeiko Schocher * Add a device to the list of MTD devices present in the system, and 393ff94bc40SHeiko Schocher * notify each currently active MTD 'user' of its arrival. Returns 394ff94bc40SHeiko Schocher * zero on success or 1 on failure, which currently will only happen 395ff94bc40SHeiko Schocher * if there is insufficient memory or a sysfs error. 396ff94bc40SHeiko Schocher */ 397ff94bc40SHeiko Schocher 398e29c22f5SKyungmin Park int add_mtd_device(struct mtd_info *mtd) 399e29c22f5SKyungmin Park { 400ff94bc40SHeiko Schocher #ifndef __UBOOT__ 401ff94bc40SHeiko Schocher struct mtd_notifier *not; 402ff94bc40SHeiko Schocher #endif 403ff94bc40SHeiko Schocher int i, error; 404ff94bc40SHeiko Schocher 405ff94bc40SHeiko Schocher #ifndef __UBOOT__ 406ff94bc40SHeiko Schocher if (!mtd->backing_dev_info) { 407ff94bc40SHeiko Schocher switch (mtd->type) { 408ff94bc40SHeiko Schocher case MTD_RAM: 409ff94bc40SHeiko Schocher mtd->backing_dev_info = &mtd_bdi_rw_mappable; 410ff94bc40SHeiko Schocher break; 411ff94bc40SHeiko Schocher case MTD_ROM: 412ff94bc40SHeiko Schocher mtd->backing_dev_info = &mtd_bdi_ro_mappable; 413ff94bc40SHeiko Schocher break; 414ff94bc40SHeiko Schocher default: 415ff94bc40SHeiko Schocher mtd->backing_dev_info = &mtd_bdi_unmappable; 416ff94bc40SHeiko Schocher break; 417ff94bc40SHeiko Schocher } 418ff94bc40SHeiko Schocher } 419ff94bc40SHeiko Schocher #endif 420e29c22f5SKyungmin Park 421e29c22f5SKyungmin Park BUG_ON(mtd->writesize == 0); 422ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex); 423e29c22f5SKyungmin Park 424ff94bc40SHeiko Schocher i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); 425ff94bc40SHeiko Schocher if (i < 0) 426ff94bc40SHeiko Schocher goto fail_locked; 427ff94bc40SHeiko Schocher 428e29c22f5SKyungmin Park mtd->index = i; 429e29c22f5SKyungmin Park mtd->usecount = 0; 430e29c22f5SKyungmin Park 431dfe64e2cSSergey Lapin /* default value if not set by driver */ 432dfe64e2cSSergey Lapin if (mtd->bitflip_threshold == 0) 433dfe64e2cSSergey Lapin mtd->bitflip_threshold = mtd->ecc_strength; 434dfe64e2cSSergey Lapin 435ff94bc40SHeiko Schocher if (is_power_of_2(mtd->erasesize)) 436ff94bc40SHeiko Schocher mtd->erasesize_shift = ffs(mtd->erasesize) - 1; 437ff94bc40SHeiko Schocher else 438ff94bc40SHeiko Schocher mtd->erasesize_shift = 0; 439dfe64e2cSSergey Lapin 440ff94bc40SHeiko Schocher if (is_power_of_2(mtd->writesize)) 441ff94bc40SHeiko Schocher mtd->writesize_shift = ffs(mtd->writesize) - 1; 442ff94bc40SHeiko Schocher else 443ff94bc40SHeiko Schocher mtd->writesize_shift = 0; 444ff94bc40SHeiko Schocher 445ff94bc40SHeiko Schocher mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; 446ff94bc40SHeiko Schocher mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; 447ff94bc40SHeiko Schocher 448ff94bc40SHeiko Schocher /* Some chips always power up locked. Unlock them now */ 449ff94bc40SHeiko Schocher if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { 450ff94bc40SHeiko Schocher error = mtd_unlock(mtd, 0, mtd->size); 451ff94bc40SHeiko Schocher if (error && error != -EOPNOTSUPP) 452ff94bc40SHeiko Schocher printk(KERN_WARNING 453ff94bc40SHeiko Schocher "%s: unlock failed, writes may not work\n", 454ff94bc40SHeiko Schocher mtd->name); 455ff94bc40SHeiko Schocher } 456ff94bc40SHeiko Schocher 457ff94bc40SHeiko Schocher #ifndef __UBOOT__ 458ff94bc40SHeiko Schocher /* Caller should have set dev.parent to match the 459ff94bc40SHeiko Schocher * physical device. 460ff94bc40SHeiko Schocher */ 461ff94bc40SHeiko Schocher mtd->dev.type = &mtd_devtype; 462ff94bc40SHeiko Schocher mtd->dev.class = &mtd_class; 463ff94bc40SHeiko Schocher mtd->dev.devt = MTD_DEVT(i); 464ff94bc40SHeiko Schocher dev_set_name(&mtd->dev, "mtd%d", i); 465ff94bc40SHeiko Schocher dev_set_drvdata(&mtd->dev, mtd); 466ff94bc40SHeiko Schocher if (device_register(&mtd->dev) != 0) 467ff94bc40SHeiko Schocher goto fail_added; 468ff94bc40SHeiko Schocher 469ff94bc40SHeiko Schocher if (MTD_DEVT(i)) 470ff94bc40SHeiko Schocher device_create(&mtd_class, mtd->dev.parent, 471ff94bc40SHeiko Schocher MTD_DEVT(i) + 1, 472ff94bc40SHeiko Schocher NULL, "mtd%dro", i); 473ff94bc40SHeiko Schocher 474ff94bc40SHeiko Schocher pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); 475e29c22f5SKyungmin Park /* No need to get a refcount on the module containing 476e29c22f5SKyungmin Park the notifier, since we hold the mtd_table_mutex */ 477ff94bc40SHeiko Schocher list_for_each_entry(not, &mtd_notifiers, list) 478ff94bc40SHeiko Schocher not->add(mtd); 479*ddf7bcfaSHeiko Schocher #else 480*ddf7bcfaSHeiko Schocher pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); 481ff94bc40SHeiko Schocher #endif 482e29c22f5SKyungmin Park 483ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 484e29c22f5SKyungmin Park /* We _know_ we aren't being removed, because 485e29c22f5SKyungmin Park our caller is still holding us here. So none 486e29c22f5SKyungmin Park of this try_ nonsense, and no bitching about it 487e29c22f5SKyungmin Park either. :) */ 488ff94bc40SHeiko Schocher __module_get(THIS_MODULE); 489e29c22f5SKyungmin Park return 0; 490e29c22f5SKyungmin Park 491ff94bc40SHeiko Schocher #ifndef __UBOOT__ 492ff94bc40SHeiko Schocher fail_added: 493ff94bc40SHeiko Schocher idr_remove(&mtd_idr, i); 494ff94bc40SHeiko Schocher #endif 495ff94bc40SHeiko Schocher fail_locked: 496ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 497e29c22f5SKyungmin Park return 1; 498e29c22f5SKyungmin Park } 499e29c22f5SKyungmin Park 500e29c22f5SKyungmin Park /** 501e29c22f5SKyungmin Park * del_mtd_device - unregister an MTD device 502e29c22f5SKyungmin Park * @mtd: pointer to MTD device info structure 503e29c22f5SKyungmin Park * 504e29c22f5SKyungmin Park * Remove a device from the list of MTD devices present in the system, 505e29c22f5SKyungmin Park * and notify each currently active MTD 'user' of its departure. 506e29c22f5SKyungmin Park * Returns zero on success or 1 on failure, which currently will happen 507e29c22f5SKyungmin Park * if the requested device does not appear to be present in the list. 508e29c22f5SKyungmin Park */ 509ff94bc40SHeiko Schocher 510e29c22f5SKyungmin Park int del_mtd_device(struct mtd_info *mtd) 511e29c22f5SKyungmin Park { 512e29c22f5SKyungmin Park int ret; 513ff94bc40SHeiko Schocher #ifndef __UBOOT__ 514ff94bc40SHeiko Schocher struct mtd_notifier *not; 515ff94bc40SHeiko Schocher #endif 516e29c22f5SKyungmin Park 517ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex); 518ff94bc40SHeiko Schocher 519ff94bc40SHeiko Schocher if (idr_find(&mtd_idr, mtd->index) != mtd) { 520e29c22f5SKyungmin Park ret = -ENODEV; 521ff94bc40SHeiko Schocher goto out_error; 522ff94bc40SHeiko Schocher } 523ff94bc40SHeiko Schocher 524ff94bc40SHeiko Schocher #ifndef __UBOOT__ 525ff94bc40SHeiko Schocher /* No need to get a refcount on the module containing 526ff94bc40SHeiko Schocher the notifier, since we hold the mtd_table_mutex */ 527ff94bc40SHeiko Schocher list_for_each_entry(not, &mtd_notifiers, list) 528ff94bc40SHeiko Schocher not->remove(mtd); 529ff94bc40SHeiko Schocher #endif 530ff94bc40SHeiko Schocher 531ff94bc40SHeiko Schocher if (mtd->usecount) { 532ff94bc40SHeiko Schocher printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", 533e29c22f5SKyungmin Park mtd->index, mtd->name, mtd->usecount); 534e29c22f5SKyungmin Park ret = -EBUSY; 535e29c22f5SKyungmin Park } else { 536ff94bc40SHeiko Schocher #ifndef __UBOOT__ 537ff94bc40SHeiko Schocher device_unregister(&mtd->dev); 538ff94bc40SHeiko Schocher #endif 539e29c22f5SKyungmin Park 540ff94bc40SHeiko Schocher idr_remove(&mtd_idr, mtd->index); 541ff94bc40SHeiko Schocher 542ff94bc40SHeiko Schocher module_put(THIS_MODULE); 543e29c22f5SKyungmin Park ret = 0; 544e29c22f5SKyungmin Park } 545e29c22f5SKyungmin Park 546ff94bc40SHeiko Schocher out_error: 547ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 548e29c22f5SKyungmin Park return ret; 549e29c22f5SKyungmin Park } 550e29c22f5SKyungmin Park 551ff94bc40SHeiko Schocher #ifndef __UBOOT__ 552ff94bc40SHeiko Schocher /** 553ff94bc40SHeiko Schocher * mtd_device_parse_register - parse partitions and register an MTD device. 554ff94bc40SHeiko Schocher * 555ff94bc40SHeiko Schocher * @mtd: the MTD device to register 556ff94bc40SHeiko Schocher * @types: the list of MTD partition probes to try, see 557ff94bc40SHeiko Schocher * 'parse_mtd_partitions()' for more information 558ff94bc40SHeiko Schocher * @parser_data: MTD partition parser-specific data 559ff94bc40SHeiko Schocher * @parts: fallback partition information to register, if parsing fails; 560ff94bc40SHeiko Schocher * only valid if %nr_parts > %0 561ff94bc40SHeiko Schocher * @nr_parts: the number of partitions in parts, if zero then the full 562ff94bc40SHeiko Schocher * MTD device is registered if no partition info is found 563ff94bc40SHeiko Schocher * 564ff94bc40SHeiko Schocher * This function aggregates MTD partitions parsing (done by 565ff94bc40SHeiko Schocher * 'parse_mtd_partitions()') and MTD device and partitions registering. It 566ff94bc40SHeiko Schocher * basically follows the most common pattern found in many MTD drivers: 567ff94bc40SHeiko Schocher * 568ff94bc40SHeiko Schocher * * It first tries to probe partitions on MTD device @mtd using parsers 569ff94bc40SHeiko Schocher * specified in @types (if @types is %NULL, then the default list of parsers 570ff94bc40SHeiko Schocher * is used, see 'parse_mtd_partitions()' for more information). If none are 571ff94bc40SHeiko Schocher * found this functions tries to fallback to information specified in 572ff94bc40SHeiko Schocher * @parts/@nr_parts. 573ff94bc40SHeiko Schocher * * If any partitioning info was found, this function registers the found 574ff94bc40SHeiko Schocher * partitions. 575ff94bc40SHeiko Schocher * * If no partitions were found this function just registers the MTD device 576ff94bc40SHeiko Schocher * @mtd and exits. 577ff94bc40SHeiko Schocher * 578ff94bc40SHeiko Schocher * Returns zero in case of success and a negative error code in case of failure. 579ff94bc40SHeiko Schocher */ 580ff94bc40SHeiko Schocher int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, 581ff94bc40SHeiko Schocher struct mtd_part_parser_data *parser_data, 582ff94bc40SHeiko Schocher const struct mtd_partition *parts, 583ff94bc40SHeiko Schocher int nr_parts) 584ff94bc40SHeiko Schocher { 585ff94bc40SHeiko Schocher int err; 586ff94bc40SHeiko Schocher struct mtd_partition *real_parts; 587ff94bc40SHeiko Schocher 588ff94bc40SHeiko Schocher err = parse_mtd_partitions(mtd, types, &real_parts, parser_data); 589ff94bc40SHeiko Schocher if (err <= 0 && nr_parts && parts) { 590ff94bc40SHeiko Schocher real_parts = kmemdup(parts, sizeof(*parts) * nr_parts, 591ff94bc40SHeiko Schocher GFP_KERNEL); 592ff94bc40SHeiko Schocher if (!real_parts) 593ff94bc40SHeiko Schocher err = -ENOMEM; 594ff94bc40SHeiko Schocher else 595ff94bc40SHeiko Schocher err = nr_parts; 596ff94bc40SHeiko Schocher } 597ff94bc40SHeiko Schocher 598ff94bc40SHeiko Schocher if (err > 0) { 599ff94bc40SHeiko Schocher err = add_mtd_partitions(mtd, real_parts, err); 600ff94bc40SHeiko Schocher kfree(real_parts); 601ff94bc40SHeiko Schocher } else if (err == 0) { 602ff94bc40SHeiko Schocher err = add_mtd_device(mtd); 603ff94bc40SHeiko Schocher if (err == 1) 604ff94bc40SHeiko Schocher err = -ENODEV; 605ff94bc40SHeiko Schocher } 606ff94bc40SHeiko Schocher 607ff94bc40SHeiko Schocher return err; 608ff94bc40SHeiko Schocher } 609ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_device_parse_register); 610ff94bc40SHeiko Schocher 611ff94bc40SHeiko Schocher /** 612ff94bc40SHeiko Schocher * mtd_device_unregister - unregister an existing MTD device. 613ff94bc40SHeiko Schocher * 614ff94bc40SHeiko Schocher * @master: the MTD device to unregister. This will unregister both the master 615ff94bc40SHeiko Schocher * and any partitions if registered. 616ff94bc40SHeiko Schocher */ 617ff94bc40SHeiko Schocher int mtd_device_unregister(struct mtd_info *master) 618ff94bc40SHeiko Schocher { 619ff94bc40SHeiko Schocher int err; 620ff94bc40SHeiko Schocher 621ff94bc40SHeiko Schocher err = del_mtd_partitions(master); 622ff94bc40SHeiko Schocher if (err) 623ff94bc40SHeiko Schocher return err; 624ff94bc40SHeiko Schocher 625ff94bc40SHeiko Schocher if (!device_is_registered(&master->dev)) 626ff94bc40SHeiko Schocher return 0; 627ff94bc40SHeiko Schocher 628ff94bc40SHeiko Schocher return del_mtd_device(master); 629ff94bc40SHeiko Schocher } 630ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_device_unregister); 631ff94bc40SHeiko Schocher 632ff94bc40SHeiko Schocher /** 633ff94bc40SHeiko Schocher * register_mtd_user - register a 'user' of MTD devices. 634ff94bc40SHeiko Schocher * @new: pointer to notifier info structure 635ff94bc40SHeiko Schocher * 636ff94bc40SHeiko Schocher * Registers a pair of callbacks function to be called upon addition 637ff94bc40SHeiko Schocher * or removal of MTD devices. Causes the 'add' callback to be immediately 638ff94bc40SHeiko Schocher * invoked for each MTD device currently present in the system. 639ff94bc40SHeiko Schocher */ 640ff94bc40SHeiko Schocher void register_mtd_user (struct mtd_notifier *new) 641ff94bc40SHeiko Schocher { 642ff94bc40SHeiko Schocher struct mtd_info *mtd; 643ff94bc40SHeiko Schocher 644ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex); 645ff94bc40SHeiko Schocher 646ff94bc40SHeiko Schocher list_add(&new->list, &mtd_notifiers); 647ff94bc40SHeiko Schocher 648ff94bc40SHeiko Schocher __module_get(THIS_MODULE); 649ff94bc40SHeiko Schocher 650ff94bc40SHeiko Schocher mtd_for_each_device(mtd) 651ff94bc40SHeiko Schocher new->add(mtd); 652ff94bc40SHeiko Schocher 653ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 654ff94bc40SHeiko Schocher } 655ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(register_mtd_user); 656ff94bc40SHeiko Schocher 657ff94bc40SHeiko Schocher /** 658ff94bc40SHeiko Schocher * unregister_mtd_user - unregister a 'user' of MTD devices. 659ff94bc40SHeiko Schocher * @old: pointer to notifier info structure 660ff94bc40SHeiko Schocher * 661ff94bc40SHeiko Schocher * Removes a callback function pair from the list of 'users' to be 662ff94bc40SHeiko Schocher * notified upon addition or removal of MTD devices. Causes the 663ff94bc40SHeiko Schocher * 'remove' callback to be immediately invoked for each MTD device 664ff94bc40SHeiko Schocher * currently present in the system. 665ff94bc40SHeiko Schocher */ 666ff94bc40SHeiko Schocher int unregister_mtd_user (struct mtd_notifier *old) 667ff94bc40SHeiko Schocher { 668ff94bc40SHeiko Schocher struct mtd_info *mtd; 669ff94bc40SHeiko Schocher 670ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex); 671ff94bc40SHeiko Schocher 672ff94bc40SHeiko Schocher module_put(THIS_MODULE); 673ff94bc40SHeiko Schocher 674ff94bc40SHeiko Schocher mtd_for_each_device(mtd) 675ff94bc40SHeiko Schocher old->remove(mtd); 676ff94bc40SHeiko Schocher 677ff94bc40SHeiko Schocher list_del(&old->list); 678ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 679ff94bc40SHeiko Schocher return 0; 680ff94bc40SHeiko Schocher } 681ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(unregister_mtd_user); 682ff94bc40SHeiko Schocher #endif 683ff94bc40SHeiko Schocher 684e29c22f5SKyungmin Park /** 685e29c22f5SKyungmin Park * get_mtd_device - obtain a validated handle for an MTD device 686e29c22f5SKyungmin Park * @mtd: last known address of the required MTD device 687e29c22f5SKyungmin Park * @num: internal device number of the required MTD device 688e29c22f5SKyungmin Park * 689e29c22f5SKyungmin Park * Given a number and NULL address, return the num'th entry in the device 690e29c22f5SKyungmin Park * table, if any. Given an address and num == -1, search the device table 691e29c22f5SKyungmin Park * for a device with that address and return if it's still present. Given 692e29c22f5SKyungmin Park * both, return the num'th driver only if its address matches. Return 693e29c22f5SKyungmin Park * error code if not. 694e29c22f5SKyungmin Park */ 695e29c22f5SKyungmin Park struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) 696e29c22f5SKyungmin Park { 697ff94bc40SHeiko Schocher struct mtd_info *ret = NULL, *other; 698ff94bc40SHeiko Schocher int err = -ENODEV; 699ff94bc40SHeiko Schocher 700ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex); 701e29c22f5SKyungmin Park 702e29c22f5SKyungmin Park if (num == -1) { 703ff94bc40SHeiko Schocher mtd_for_each_device(other) { 704ff94bc40SHeiko Schocher if (other == mtd) { 705ff94bc40SHeiko Schocher ret = mtd; 706ff94bc40SHeiko Schocher break; 707ff94bc40SHeiko Schocher } 708ff94bc40SHeiko Schocher } 709ff94bc40SHeiko Schocher } else if (num >= 0) { 710ff94bc40SHeiko Schocher ret = idr_find(&mtd_idr, num); 711e29c22f5SKyungmin Park if (mtd && mtd != ret) 712e29c22f5SKyungmin Park ret = NULL; 713e29c22f5SKyungmin Park } 714e29c22f5SKyungmin Park 715ff94bc40SHeiko Schocher if (!ret) { 716ff94bc40SHeiko Schocher ret = ERR_PTR(err); 717ff94bc40SHeiko Schocher goto out; 718e29c22f5SKyungmin Park } 719e29c22f5SKyungmin Park 720ff94bc40SHeiko Schocher err = __get_mtd_device(ret); 721ff94bc40SHeiko Schocher if (err) 722ff94bc40SHeiko Schocher ret = ERR_PTR(err); 723ff94bc40SHeiko Schocher out: 724ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 725ff94bc40SHeiko Schocher return ret; 726ff94bc40SHeiko Schocher } 727ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(get_mtd_device); 728ff94bc40SHeiko Schocher 729ff94bc40SHeiko Schocher 730ff94bc40SHeiko Schocher int __get_mtd_device(struct mtd_info *mtd) 731ff94bc40SHeiko Schocher { 732ff94bc40SHeiko Schocher int err; 733ff94bc40SHeiko Schocher 734ff94bc40SHeiko Schocher if (!try_module_get(mtd->owner)) 735ff94bc40SHeiko Schocher return -ENODEV; 736ff94bc40SHeiko Schocher 737ff94bc40SHeiko Schocher if (mtd->_get_device) { 738ff94bc40SHeiko Schocher err = mtd->_get_device(mtd); 739ff94bc40SHeiko Schocher 740ff94bc40SHeiko Schocher if (err) { 741ff94bc40SHeiko Schocher module_put(mtd->owner); 742ff94bc40SHeiko Schocher return err; 743ff94bc40SHeiko Schocher } 744ff94bc40SHeiko Schocher } 745ff94bc40SHeiko Schocher mtd->usecount++; 746ff94bc40SHeiko Schocher return 0; 747ff94bc40SHeiko Schocher } 748ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(__get_mtd_device); 749ff94bc40SHeiko Schocher 750e29c22f5SKyungmin Park /** 751e29c22f5SKyungmin Park * get_mtd_device_nm - obtain a validated handle for an MTD device by 752e29c22f5SKyungmin Park * device name 753e29c22f5SKyungmin Park * @name: MTD device name to open 754e29c22f5SKyungmin Park * 755e29c22f5SKyungmin Park * This function returns MTD device description structure in case of 756e29c22f5SKyungmin Park * success and an error code in case of failure. 757e29c22f5SKyungmin Park */ 758e29c22f5SKyungmin Park struct mtd_info *get_mtd_device_nm(const char *name) 759e29c22f5SKyungmin Park { 760ff94bc40SHeiko Schocher int err = -ENODEV; 761ff94bc40SHeiko Schocher struct mtd_info *mtd = NULL, *other; 762e29c22f5SKyungmin Park 763ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex); 764ff94bc40SHeiko Schocher 765ff94bc40SHeiko Schocher mtd_for_each_device(other) { 766ff94bc40SHeiko Schocher if (!strcmp(name, other->name)) { 767ff94bc40SHeiko Schocher mtd = other; 768e29c22f5SKyungmin Park break; 769e29c22f5SKyungmin Park } 770e29c22f5SKyungmin Park } 771e29c22f5SKyungmin Park 772e29c22f5SKyungmin Park if (!mtd) 773e29c22f5SKyungmin Park goto out_unlock; 774e29c22f5SKyungmin Park 775ff94bc40SHeiko Schocher err = __get_mtd_device(mtd); 776ff94bc40SHeiko Schocher if (err) 777ff94bc40SHeiko Schocher goto out_unlock; 778ff94bc40SHeiko Schocher 779ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 780e29c22f5SKyungmin Park return mtd; 781e29c22f5SKyungmin Park 782e29c22f5SKyungmin Park out_unlock: 783ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 784e29c22f5SKyungmin Park return ERR_PTR(err); 785e29c22f5SKyungmin Park } 786ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(get_mtd_device_nm); 7874ba692fbSBen Gardiner 7884ba692fbSBen Gardiner #if defined(CONFIG_CMD_MTDPARTS_SPREAD) 7894ba692fbSBen Gardiner /** 7904ba692fbSBen Gardiner * mtd_get_len_incl_bad 7914ba692fbSBen Gardiner * 7924ba692fbSBen Gardiner * Check if length including bad blocks fits into device. 7934ba692fbSBen Gardiner * 7944ba692fbSBen Gardiner * @param mtd an MTD device 7954ba692fbSBen Gardiner * @param offset offset in flash 7964ba692fbSBen Gardiner * @param length image length 7974ba692fbSBen Gardiner * @return image length including bad blocks in *len_incl_bad and whether or not 7984ba692fbSBen Gardiner * the length returned was truncated in *truncated 7994ba692fbSBen Gardiner */ 8004ba692fbSBen Gardiner void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset, 8014ba692fbSBen Gardiner const uint64_t length, uint64_t *len_incl_bad, 8024ba692fbSBen Gardiner int *truncated) 8034ba692fbSBen Gardiner { 8044ba692fbSBen Gardiner *truncated = 0; 8054ba692fbSBen Gardiner *len_incl_bad = 0; 8064ba692fbSBen Gardiner 8074ba692fbSBen Gardiner if (!mtd->block_isbad) { 8084ba692fbSBen Gardiner *len_incl_bad = length; 8094ba692fbSBen Gardiner return; 8104ba692fbSBen Gardiner } 8114ba692fbSBen Gardiner 8124ba692fbSBen Gardiner uint64_t len_excl_bad = 0; 8134ba692fbSBen Gardiner uint64_t block_len; 8144ba692fbSBen Gardiner 8154ba692fbSBen Gardiner while (len_excl_bad < length) { 81636650ca9SScott Wood if (offset >= mtd->size) { 81736650ca9SScott Wood *truncated = 1; 81836650ca9SScott Wood return; 81936650ca9SScott Wood } 82036650ca9SScott Wood 8214ba692fbSBen Gardiner block_len = mtd->erasesize - (offset & (mtd->erasesize - 1)); 8224ba692fbSBen Gardiner 8234ba692fbSBen Gardiner if (!mtd->block_isbad(mtd, offset & ~(mtd->erasesize - 1))) 8244ba692fbSBen Gardiner len_excl_bad += block_len; 8254ba692fbSBen Gardiner 8264ba692fbSBen Gardiner *len_incl_bad += block_len; 8274ba692fbSBen Gardiner offset += block_len; 8284ba692fbSBen Gardiner } 8294ba692fbSBen Gardiner } 8304ba692fbSBen Gardiner #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */ 831dfe64e2cSSergey Lapin 832ff94bc40SHeiko Schocher void put_mtd_device(struct mtd_info *mtd) 833ff94bc40SHeiko Schocher { 834ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex); 835ff94bc40SHeiko Schocher __put_mtd_device(mtd); 836ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 837ff94bc40SHeiko Schocher 838ff94bc40SHeiko Schocher } 839ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(put_mtd_device); 840ff94bc40SHeiko Schocher 841ff94bc40SHeiko Schocher void __put_mtd_device(struct mtd_info *mtd) 842ff94bc40SHeiko Schocher { 843ff94bc40SHeiko Schocher --mtd->usecount; 844ff94bc40SHeiko Schocher BUG_ON(mtd->usecount < 0); 845ff94bc40SHeiko Schocher 846ff94bc40SHeiko Schocher if (mtd->_put_device) 847ff94bc40SHeiko Schocher mtd->_put_device(mtd); 848ff94bc40SHeiko Schocher 849ff94bc40SHeiko Schocher module_put(mtd->owner); 850ff94bc40SHeiko Schocher } 851ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(__put_mtd_device); 852ff94bc40SHeiko Schocher 853dfe64e2cSSergey Lapin /* 854dfe64e2cSSergey Lapin * Erase is an asynchronous operation. Device drivers are supposed 855dfe64e2cSSergey Lapin * to call instr->callback() whenever the operation completes, even 856dfe64e2cSSergey Lapin * if it completes with a failure. 857dfe64e2cSSergey Lapin * Callers are supposed to pass a callback function and wait for it 858dfe64e2cSSergey Lapin * to be called before writing to the block. 859dfe64e2cSSergey Lapin */ 860dfe64e2cSSergey Lapin int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) 861dfe64e2cSSergey Lapin { 862dfe64e2cSSergey Lapin if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr) 863dfe64e2cSSergey Lapin return -EINVAL; 864dfe64e2cSSergey Lapin if (!(mtd->flags & MTD_WRITEABLE)) 865dfe64e2cSSergey Lapin return -EROFS; 866dfe64e2cSSergey Lapin instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 867dfe64e2cSSergey Lapin if (!instr->len) { 868dfe64e2cSSergey Lapin instr->state = MTD_ERASE_DONE; 869dfe64e2cSSergey Lapin mtd_erase_callback(instr); 870dfe64e2cSSergey Lapin return 0; 871dfe64e2cSSergey Lapin } 872dfe64e2cSSergey Lapin return mtd->_erase(mtd, instr); 873dfe64e2cSSergey Lapin } 874ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_erase); 875ff94bc40SHeiko Schocher 876ff94bc40SHeiko Schocher #ifndef __UBOOT__ 877ff94bc40SHeiko Schocher /* 878ff94bc40SHeiko Schocher * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. 879ff94bc40SHeiko Schocher */ 880ff94bc40SHeiko Schocher int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 881ff94bc40SHeiko Schocher void **virt, resource_size_t *phys) 882ff94bc40SHeiko Schocher { 883ff94bc40SHeiko Schocher *retlen = 0; 884ff94bc40SHeiko Schocher *virt = NULL; 885ff94bc40SHeiko Schocher if (phys) 886ff94bc40SHeiko Schocher *phys = 0; 887ff94bc40SHeiko Schocher if (!mtd->_point) 888ff94bc40SHeiko Schocher return -EOPNOTSUPP; 889ff94bc40SHeiko Schocher if (from < 0 || from > mtd->size || len > mtd->size - from) 890ff94bc40SHeiko Schocher return -EINVAL; 891ff94bc40SHeiko Schocher if (!len) 892ff94bc40SHeiko Schocher return 0; 893ff94bc40SHeiko Schocher return mtd->_point(mtd, from, len, retlen, virt, phys); 894ff94bc40SHeiko Schocher } 895ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_point); 896ff94bc40SHeiko Schocher 897ff94bc40SHeiko Schocher /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ 898ff94bc40SHeiko Schocher int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 899ff94bc40SHeiko Schocher { 900ff94bc40SHeiko Schocher if (!mtd->_point) 901ff94bc40SHeiko Schocher return -EOPNOTSUPP; 902ff94bc40SHeiko Schocher if (from < 0 || from > mtd->size || len > mtd->size - from) 903ff94bc40SHeiko Schocher return -EINVAL; 904ff94bc40SHeiko Schocher if (!len) 905ff94bc40SHeiko Schocher return 0; 906ff94bc40SHeiko Schocher return mtd->_unpoint(mtd, from, len); 907ff94bc40SHeiko Schocher } 908ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_unpoint); 909ff94bc40SHeiko Schocher #endif 910ff94bc40SHeiko Schocher 911ff94bc40SHeiko Schocher /* 912ff94bc40SHeiko Schocher * Allow NOMMU mmap() to directly map the device (if not NULL) 913ff94bc40SHeiko Schocher * - return the address to which the offset maps 914ff94bc40SHeiko Schocher * - return -ENOSYS to indicate refusal to do the mapping 915ff94bc40SHeiko Schocher */ 916ff94bc40SHeiko Schocher unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, 917ff94bc40SHeiko Schocher unsigned long offset, unsigned long flags) 918ff94bc40SHeiko Schocher { 919ff94bc40SHeiko Schocher if (!mtd->_get_unmapped_area) 920ff94bc40SHeiko Schocher return -EOPNOTSUPP; 921ff94bc40SHeiko Schocher if (offset > mtd->size || len > mtd->size - offset) 922ff94bc40SHeiko Schocher return -EINVAL; 923ff94bc40SHeiko Schocher return mtd->_get_unmapped_area(mtd, len, offset, flags); 924ff94bc40SHeiko Schocher } 925ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_get_unmapped_area); 926dfe64e2cSSergey Lapin 927dfe64e2cSSergey Lapin int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 928dfe64e2cSSergey Lapin u_char *buf) 929dfe64e2cSSergey Lapin { 93040462e54SPaul Burton int ret_code; 931ff94bc40SHeiko Schocher *retlen = 0; 932dfe64e2cSSergey Lapin if (from < 0 || from > mtd->size || len > mtd->size - from) 933dfe64e2cSSergey Lapin return -EINVAL; 934dfe64e2cSSergey Lapin if (!len) 935dfe64e2cSSergey Lapin return 0; 93640462e54SPaul Burton 93740462e54SPaul Burton /* 93840462e54SPaul Burton * In the absence of an error, drivers return a non-negative integer 93940462e54SPaul Burton * representing the maximum number of bitflips that were corrected on 94040462e54SPaul Burton * any one ecc region (if applicable; zero otherwise). 94140462e54SPaul Burton */ 94240462e54SPaul Burton ret_code = mtd->_read(mtd, from, len, retlen, buf); 94340462e54SPaul Burton if (unlikely(ret_code < 0)) 94440462e54SPaul Burton return ret_code; 94540462e54SPaul Burton if (mtd->ecc_strength == 0) 94640462e54SPaul Burton return 0; /* device lacks ecc */ 94740462e54SPaul Burton return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; 948dfe64e2cSSergey Lapin } 949ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_read); 950dfe64e2cSSergey Lapin 951dfe64e2cSSergey Lapin int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 952dfe64e2cSSergey Lapin const u_char *buf) 953dfe64e2cSSergey Lapin { 954dfe64e2cSSergey Lapin *retlen = 0; 955dfe64e2cSSergey Lapin if (to < 0 || to > mtd->size || len > mtd->size - to) 956dfe64e2cSSergey Lapin return -EINVAL; 957dfe64e2cSSergey Lapin if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE)) 958dfe64e2cSSergey Lapin return -EROFS; 959dfe64e2cSSergey Lapin if (!len) 960dfe64e2cSSergey Lapin return 0; 961dfe64e2cSSergey Lapin return mtd->_write(mtd, to, len, retlen, buf); 962dfe64e2cSSergey Lapin } 963ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_write); 964dfe64e2cSSergey Lapin 965dfe64e2cSSergey Lapin /* 966dfe64e2cSSergey Lapin * In blackbox flight recorder like scenarios we want to make successful writes 967dfe64e2cSSergey Lapin * in interrupt context. panic_write() is only intended to be called when its 968dfe64e2cSSergey Lapin * known the kernel is about to panic and we need the write to succeed. Since 969dfe64e2cSSergey Lapin * the kernel is not going to be running for much longer, this function can 970dfe64e2cSSergey Lapin * break locks and delay to ensure the write succeeds (but not sleep). 971dfe64e2cSSergey Lapin */ 972dfe64e2cSSergey Lapin int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 973dfe64e2cSSergey Lapin const u_char *buf) 974dfe64e2cSSergey Lapin { 975dfe64e2cSSergey Lapin *retlen = 0; 976dfe64e2cSSergey Lapin if (!mtd->_panic_write) 977dfe64e2cSSergey Lapin return -EOPNOTSUPP; 978dfe64e2cSSergey Lapin if (to < 0 || to > mtd->size || len > mtd->size - to) 979dfe64e2cSSergey Lapin return -EINVAL; 980dfe64e2cSSergey Lapin if (!(mtd->flags & MTD_WRITEABLE)) 981dfe64e2cSSergey Lapin return -EROFS; 982dfe64e2cSSergey Lapin if (!len) 983dfe64e2cSSergey Lapin return 0; 984dfe64e2cSSergey Lapin return mtd->_panic_write(mtd, to, len, retlen, buf); 985dfe64e2cSSergey Lapin } 986ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_panic_write); 987dfe64e2cSSergey Lapin 988dfe64e2cSSergey Lapin int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) 989dfe64e2cSSergey Lapin { 990ff94bc40SHeiko Schocher int ret_code; 991dfe64e2cSSergey Lapin ops->retlen = ops->oobretlen = 0; 992dfe64e2cSSergey Lapin if (!mtd->_read_oob) 993dfe64e2cSSergey Lapin return -EOPNOTSUPP; 994ff94bc40SHeiko Schocher /* 995ff94bc40SHeiko Schocher * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics 996ff94bc40SHeiko Schocher * similar to mtd->_read(), returning a non-negative integer 997ff94bc40SHeiko Schocher * representing max bitflips. In other cases, mtd->_read_oob() may 998ff94bc40SHeiko Schocher * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). 999ff94bc40SHeiko Schocher */ 1000ff94bc40SHeiko Schocher ret_code = mtd->_read_oob(mtd, from, ops); 1001ff94bc40SHeiko Schocher if (unlikely(ret_code < 0)) 1002ff94bc40SHeiko Schocher return ret_code; 1003ff94bc40SHeiko Schocher if (mtd->ecc_strength == 0) 1004ff94bc40SHeiko Schocher return 0; /* device lacks ecc */ 1005ff94bc40SHeiko Schocher return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; 1006dfe64e2cSSergey Lapin } 1007ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_read_oob); 1008dfe64e2cSSergey Lapin 1009dfe64e2cSSergey Lapin /* 1010dfe64e2cSSergey Lapin * Method to access the protection register area, present in some flash 1011dfe64e2cSSergey Lapin * devices. The user data is one time programmable but the factory data is read 1012dfe64e2cSSergey Lapin * only. 1013dfe64e2cSSergey Lapin */ 1014dfe64e2cSSergey Lapin int mtd_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf, 1015dfe64e2cSSergey Lapin size_t len) 1016dfe64e2cSSergey Lapin { 1017dfe64e2cSSergey Lapin if (!mtd->_get_fact_prot_info) 1018dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1019dfe64e2cSSergey Lapin if (!len) 1020dfe64e2cSSergey Lapin return 0; 1021dfe64e2cSSergey Lapin return mtd->_get_fact_prot_info(mtd, buf, len); 1022dfe64e2cSSergey Lapin } 1023ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); 1024dfe64e2cSSergey Lapin 1025dfe64e2cSSergey Lapin int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 1026dfe64e2cSSergey Lapin size_t *retlen, u_char *buf) 1027dfe64e2cSSergey Lapin { 1028dfe64e2cSSergey Lapin *retlen = 0; 1029dfe64e2cSSergey Lapin if (!mtd->_read_fact_prot_reg) 1030dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1031dfe64e2cSSergey Lapin if (!len) 1032dfe64e2cSSergey Lapin return 0; 1033dfe64e2cSSergey Lapin return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf); 1034dfe64e2cSSergey Lapin } 1035ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); 1036dfe64e2cSSergey Lapin 1037dfe64e2cSSergey Lapin int mtd_get_user_prot_info(struct mtd_info *mtd, struct otp_info *buf, 1038dfe64e2cSSergey Lapin size_t len) 1039dfe64e2cSSergey Lapin { 1040dfe64e2cSSergey Lapin if (!mtd->_get_user_prot_info) 1041dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1042dfe64e2cSSergey Lapin if (!len) 1043dfe64e2cSSergey Lapin return 0; 1044dfe64e2cSSergey Lapin return mtd->_get_user_prot_info(mtd, buf, len); 1045dfe64e2cSSergey Lapin } 1046ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_get_user_prot_info); 1047dfe64e2cSSergey Lapin 1048dfe64e2cSSergey Lapin int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 1049dfe64e2cSSergey Lapin size_t *retlen, u_char *buf) 1050dfe64e2cSSergey Lapin { 1051dfe64e2cSSergey Lapin *retlen = 0; 1052dfe64e2cSSergey Lapin if (!mtd->_read_user_prot_reg) 1053dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1054dfe64e2cSSergey Lapin if (!len) 1055dfe64e2cSSergey Lapin return 0; 1056dfe64e2cSSergey Lapin return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf); 1057dfe64e2cSSergey Lapin } 1058ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); 1059dfe64e2cSSergey Lapin 1060dfe64e2cSSergey Lapin int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, 1061dfe64e2cSSergey Lapin size_t *retlen, u_char *buf) 1062dfe64e2cSSergey Lapin { 1063dfe64e2cSSergey Lapin *retlen = 0; 1064dfe64e2cSSergey Lapin if (!mtd->_write_user_prot_reg) 1065dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1066dfe64e2cSSergey Lapin if (!len) 1067dfe64e2cSSergey Lapin return 0; 1068dfe64e2cSSergey Lapin return mtd->_write_user_prot_reg(mtd, to, len, retlen, buf); 1069dfe64e2cSSergey Lapin } 1070ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); 1071dfe64e2cSSergey Lapin 1072dfe64e2cSSergey Lapin int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) 1073dfe64e2cSSergey Lapin { 1074dfe64e2cSSergey Lapin if (!mtd->_lock_user_prot_reg) 1075dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1076dfe64e2cSSergey Lapin if (!len) 1077dfe64e2cSSergey Lapin return 0; 1078dfe64e2cSSergey Lapin return mtd->_lock_user_prot_reg(mtd, from, len); 1079dfe64e2cSSergey Lapin } 1080ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); 1081dfe64e2cSSergey Lapin 1082dfe64e2cSSergey Lapin /* Chip-supported device locking */ 1083dfe64e2cSSergey Lapin int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 1084dfe64e2cSSergey Lapin { 1085dfe64e2cSSergey Lapin if (!mtd->_lock) 1086dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1087dfe64e2cSSergey Lapin if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) 1088dfe64e2cSSergey Lapin return -EINVAL; 1089dfe64e2cSSergey Lapin if (!len) 1090dfe64e2cSSergey Lapin return 0; 1091dfe64e2cSSergey Lapin return mtd->_lock(mtd, ofs, len); 1092dfe64e2cSSergey Lapin } 1093ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_lock); 1094dfe64e2cSSergey Lapin 1095dfe64e2cSSergey Lapin int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 1096dfe64e2cSSergey Lapin { 1097dfe64e2cSSergey Lapin if (!mtd->_unlock) 1098dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1099dfe64e2cSSergey Lapin if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) 1100dfe64e2cSSergey Lapin return -EINVAL; 1101dfe64e2cSSergey Lapin if (!len) 1102dfe64e2cSSergey Lapin return 0; 1103dfe64e2cSSergey Lapin return mtd->_unlock(mtd, ofs, len); 1104dfe64e2cSSergey Lapin } 1105ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_unlock); 1106ff94bc40SHeiko Schocher 1107ff94bc40SHeiko Schocher int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 1108ff94bc40SHeiko Schocher { 1109ff94bc40SHeiko Schocher if (!mtd->_is_locked) 1110ff94bc40SHeiko Schocher return -EOPNOTSUPP; 1111ff94bc40SHeiko Schocher if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) 1112ff94bc40SHeiko Schocher return -EINVAL; 1113ff94bc40SHeiko Schocher if (!len) 1114ff94bc40SHeiko Schocher return 0; 1115ff94bc40SHeiko Schocher return mtd->_is_locked(mtd, ofs, len); 1116ff94bc40SHeiko Schocher } 1117ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_is_locked); 1118dfe64e2cSSergey Lapin 1119dfe64e2cSSergey Lapin int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) 1120dfe64e2cSSergey Lapin { 1121dfe64e2cSSergey Lapin if (!mtd->_block_isbad) 1122dfe64e2cSSergey Lapin return 0; 1123dfe64e2cSSergey Lapin if (ofs < 0 || ofs > mtd->size) 1124dfe64e2cSSergey Lapin return -EINVAL; 1125dfe64e2cSSergey Lapin return mtd->_block_isbad(mtd, ofs); 1126dfe64e2cSSergey Lapin } 1127ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_block_isbad); 1128dfe64e2cSSergey Lapin 1129dfe64e2cSSergey Lapin int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) 1130dfe64e2cSSergey Lapin { 1131dfe64e2cSSergey Lapin if (!mtd->_block_markbad) 1132dfe64e2cSSergey Lapin return -EOPNOTSUPP; 1133dfe64e2cSSergey Lapin if (ofs < 0 || ofs > mtd->size) 1134dfe64e2cSSergey Lapin return -EINVAL; 1135dfe64e2cSSergey Lapin if (!(mtd->flags & MTD_WRITEABLE)) 1136dfe64e2cSSergey Lapin return -EROFS; 1137dfe64e2cSSergey Lapin return mtd->_block_markbad(mtd, ofs); 1138dfe64e2cSSergey Lapin } 1139ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_block_markbad); 1140ff94bc40SHeiko Schocher 1141ff94bc40SHeiko Schocher #ifndef __UBOOT__ 1142ff94bc40SHeiko Schocher /* 1143ff94bc40SHeiko Schocher * default_mtd_writev - the default writev method 1144ff94bc40SHeiko Schocher * @mtd: mtd device description object pointer 1145ff94bc40SHeiko Schocher * @vecs: the vectors to write 1146ff94bc40SHeiko Schocher * @count: count of vectors in @vecs 1147ff94bc40SHeiko Schocher * @to: the MTD device offset to write to 1148ff94bc40SHeiko Schocher * @retlen: on exit contains the count of bytes written to the MTD device. 1149ff94bc40SHeiko Schocher * 1150ff94bc40SHeiko Schocher * This function returns zero in case of success and a negative error code in 1151ff94bc40SHeiko Schocher * case of failure. 1152ff94bc40SHeiko Schocher */ 1153ff94bc40SHeiko Schocher static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 1154ff94bc40SHeiko Schocher unsigned long count, loff_t to, size_t *retlen) 1155ff94bc40SHeiko Schocher { 1156ff94bc40SHeiko Schocher unsigned long i; 1157ff94bc40SHeiko Schocher size_t totlen = 0, thislen; 1158ff94bc40SHeiko Schocher int ret = 0; 1159ff94bc40SHeiko Schocher 1160ff94bc40SHeiko Schocher for (i = 0; i < count; i++) { 1161ff94bc40SHeiko Schocher if (!vecs[i].iov_len) 1162ff94bc40SHeiko Schocher continue; 1163ff94bc40SHeiko Schocher ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, 1164ff94bc40SHeiko Schocher vecs[i].iov_base); 1165ff94bc40SHeiko Schocher totlen += thislen; 1166ff94bc40SHeiko Schocher if (ret || thislen != vecs[i].iov_len) 1167ff94bc40SHeiko Schocher break; 1168ff94bc40SHeiko Schocher to += vecs[i].iov_len; 1169ff94bc40SHeiko Schocher } 1170ff94bc40SHeiko Schocher *retlen = totlen; 1171ff94bc40SHeiko Schocher return ret; 1172ff94bc40SHeiko Schocher } 1173ff94bc40SHeiko Schocher 1174ff94bc40SHeiko Schocher /* 1175ff94bc40SHeiko Schocher * mtd_writev - the vector-based MTD write method 1176ff94bc40SHeiko Schocher * @mtd: mtd device description object pointer 1177ff94bc40SHeiko Schocher * @vecs: the vectors to write 1178ff94bc40SHeiko Schocher * @count: count of vectors in @vecs 1179ff94bc40SHeiko Schocher * @to: the MTD device offset to write to 1180ff94bc40SHeiko Schocher * @retlen: on exit contains the count of bytes written to the MTD device. 1181ff94bc40SHeiko Schocher * 1182ff94bc40SHeiko Schocher * This function returns zero in case of success and a negative error code in 1183ff94bc40SHeiko Schocher * case of failure. 1184ff94bc40SHeiko Schocher */ 1185ff94bc40SHeiko Schocher int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 1186ff94bc40SHeiko Schocher unsigned long count, loff_t to, size_t *retlen) 1187ff94bc40SHeiko Schocher { 1188ff94bc40SHeiko Schocher *retlen = 0; 1189ff94bc40SHeiko Schocher if (!(mtd->flags & MTD_WRITEABLE)) 1190ff94bc40SHeiko Schocher return -EROFS; 1191ff94bc40SHeiko Schocher if (!mtd->_writev) 1192ff94bc40SHeiko Schocher return default_mtd_writev(mtd, vecs, count, to, retlen); 1193ff94bc40SHeiko Schocher return mtd->_writev(mtd, vecs, count, to, retlen); 1194ff94bc40SHeiko Schocher } 1195ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_writev); 1196ff94bc40SHeiko Schocher 1197ff94bc40SHeiko Schocher /** 1198ff94bc40SHeiko Schocher * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size 1199ff94bc40SHeiko Schocher * @mtd: mtd device description object pointer 1200ff94bc40SHeiko Schocher * @size: a pointer to the ideal or maximum size of the allocation, points 1201ff94bc40SHeiko Schocher * to the actual allocation size on success. 1202ff94bc40SHeiko Schocher * 1203ff94bc40SHeiko Schocher * This routine attempts to allocate a contiguous kernel buffer up to 1204ff94bc40SHeiko Schocher * the specified size, backing off the size of the request exponentially 1205ff94bc40SHeiko Schocher * until the request succeeds or until the allocation size falls below 1206ff94bc40SHeiko Schocher * the system page size. This attempts to make sure it does not adversely 1207ff94bc40SHeiko Schocher * impact system performance, so when allocating more than one page, we 1208ff94bc40SHeiko Schocher * ask the memory allocator to avoid re-trying, swapping, writing back 1209ff94bc40SHeiko Schocher * or performing I/O. 1210ff94bc40SHeiko Schocher * 1211ff94bc40SHeiko Schocher * Note, this function also makes sure that the allocated buffer is aligned to 1212ff94bc40SHeiko Schocher * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. 1213ff94bc40SHeiko Schocher * 1214ff94bc40SHeiko Schocher * This is called, for example by mtd_{read,write} and jffs2_scan_medium, 1215ff94bc40SHeiko Schocher * to handle smaller (i.e. degraded) buffer allocations under low- or 1216ff94bc40SHeiko Schocher * fragmented-memory situations where such reduced allocations, from a 1217ff94bc40SHeiko Schocher * requested ideal, are allowed. 1218ff94bc40SHeiko Schocher * 1219ff94bc40SHeiko Schocher * Returns a pointer to the allocated buffer on success; otherwise, NULL. 1220ff94bc40SHeiko Schocher */ 1221ff94bc40SHeiko Schocher void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) 1222ff94bc40SHeiko Schocher { 1223ff94bc40SHeiko Schocher gfp_t flags = __GFP_NOWARN | __GFP_WAIT | 1224ff94bc40SHeiko Schocher __GFP_NORETRY | __GFP_NO_KSWAPD; 1225ff94bc40SHeiko Schocher size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); 1226ff94bc40SHeiko Schocher void *kbuf; 1227ff94bc40SHeiko Schocher 1228ff94bc40SHeiko Schocher *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); 1229ff94bc40SHeiko Schocher 1230ff94bc40SHeiko Schocher while (*size > min_alloc) { 1231ff94bc40SHeiko Schocher kbuf = kmalloc(*size, flags); 1232ff94bc40SHeiko Schocher if (kbuf) 1233ff94bc40SHeiko Schocher return kbuf; 1234ff94bc40SHeiko Schocher 1235ff94bc40SHeiko Schocher *size >>= 1; 1236ff94bc40SHeiko Schocher *size = ALIGN(*size, mtd->writesize); 1237ff94bc40SHeiko Schocher } 1238ff94bc40SHeiko Schocher 1239ff94bc40SHeiko Schocher /* 1240ff94bc40SHeiko Schocher * For the last resort allocation allow 'kmalloc()' to do all sorts of 1241ff94bc40SHeiko Schocher * things (write-back, dropping caches, etc) by using GFP_KERNEL. 1242ff94bc40SHeiko Schocher */ 1243ff94bc40SHeiko Schocher return kmalloc(*size, GFP_KERNEL); 1244ff94bc40SHeiko Schocher } 1245ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); 1246ff94bc40SHeiko Schocher #endif 1247ff94bc40SHeiko Schocher 1248ff94bc40SHeiko Schocher #ifdef CONFIG_PROC_FS 1249ff94bc40SHeiko Schocher 1250ff94bc40SHeiko Schocher /*====================================================================*/ 1251ff94bc40SHeiko Schocher /* Support for /proc/mtd */ 1252ff94bc40SHeiko Schocher 1253ff94bc40SHeiko Schocher static int mtd_proc_show(struct seq_file *m, void *v) 1254ff94bc40SHeiko Schocher { 1255ff94bc40SHeiko Schocher struct mtd_info *mtd; 1256ff94bc40SHeiko Schocher 1257ff94bc40SHeiko Schocher seq_puts(m, "dev: size erasesize name\n"); 1258ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex); 1259ff94bc40SHeiko Schocher mtd_for_each_device(mtd) { 1260ff94bc40SHeiko Schocher seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", 1261ff94bc40SHeiko Schocher mtd->index, (unsigned long long)mtd->size, 1262ff94bc40SHeiko Schocher mtd->erasesize, mtd->name); 1263ff94bc40SHeiko Schocher } 1264ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex); 1265ff94bc40SHeiko Schocher return 0; 1266ff94bc40SHeiko Schocher } 1267ff94bc40SHeiko Schocher 1268ff94bc40SHeiko Schocher static int mtd_proc_open(struct inode *inode, struct file *file) 1269ff94bc40SHeiko Schocher { 1270ff94bc40SHeiko Schocher return single_open(file, mtd_proc_show, NULL); 1271ff94bc40SHeiko Schocher } 1272ff94bc40SHeiko Schocher 1273ff94bc40SHeiko Schocher static const struct file_operations mtd_proc_ops = { 1274ff94bc40SHeiko Schocher .open = mtd_proc_open, 1275ff94bc40SHeiko Schocher .read = seq_read, 1276ff94bc40SHeiko Schocher .llseek = seq_lseek, 1277ff94bc40SHeiko Schocher .release = single_release, 1278ff94bc40SHeiko Schocher }; 1279ff94bc40SHeiko Schocher #endif /* CONFIG_PROC_FS */ 1280ff94bc40SHeiko Schocher 1281ff94bc40SHeiko Schocher /*====================================================================*/ 1282ff94bc40SHeiko Schocher /* Init code */ 1283ff94bc40SHeiko Schocher 1284ff94bc40SHeiko Schocher #ifndef __UBOOT__ 1285ff94bc40SHeiko Schocher static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name) 1286ff94bc40SHeiko Schocher { 1287ff94bc40SHeiko Schocher int ret; 1288ff94bc40SHeiko Schocher 1289ff94bc40SHeiko Schocher ret = bdi_init(bdi); 1290ff94bc40SHeiko Schocher if (!ret) 1291ff94bc40SHeiko Schocher ret = bdi_register(bdi, NULL, "%s", name); 1292ff94bc40SHeiko Schocher 1293ff94bc40SHeiko Schocher if (ret) 1294ff94bc40SHeiko Schocher bdi_destroy(bdi); 1295ff94bc40SHeiko Schocher 1296ff94bc40SHeiko Schocher return ret; 1297ff94bc40SHeiko Schocher } 1298ff94bc40SHeiko Schocher 1299ff94bc40SHeiko Schocher static struct proc_dir_entry *proc_mtd; 1300ff94bc40SHeiko Schocher 1301ff94bc40SHeiko Schocher static int __init init_mtd(void) 1302ff94bc40SHeiko Schocher { 1303ff94bc40SHeiko Schocher int ret; 1304ff94bc40SHeiko Schocher 1305ff94bc40SHeiko Schocher ret = class_register(&mtd_class); 1306ff94bc40SHeiko Schocher if (ret) 1307ff94bc40SHeiko Schocher goto err_reg; 1308ff94bc40SHeiko Schocher 1309ff94bc40SHeiko Schocher ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap"); 1310ff94bc40SHeiko Schocher if (ret) 1311ff94bc40SHeiko Schocher goto err_bdi1; 1312ff94bc40SHeiko Schocher 1313ff94bc40SHeiko Schocher ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap"); 1314ff94bc40SHeiko Schocher if (ret) 1315ff94bc40SHeiko Schocher goto err_bdi2; 1316ff94bc40SHeiko Schocher 1317ff94bc40SHeiko Schocher ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap"); 1318ff94bc40SHeiko Schocher if (ret) 1319ff94bc40SHeiko Schocher goto err_bdi3; 1320ff94bc40SHeiko Schocher 1321ff94bc40SHeiko Schocher proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops); 1322ff94bc40SHeiko Schocher 1323ff94bc40SHeiko Schocher ret = init_mtdchar(); 1324ff94bc40SHeiko Schocher if (ret) 1325ff94bc40SHeiko Schocher goto out_procfs; 1326ff94bc40SHeiko Schocher 1327ff94bc40SHeiko Schocher return 0; 1328ff94bc40SHeiko Schocher 1329ff94bc40SHeiko Schocher out_procfs: 1330ff94bc40SHeiko Schocher if (proc_mtd) 1331ff94bc40SHeiko Schocher remove_proc_entry("mtd", NULL); 1332ff94bc40SHeiko Schocher err_bdi3: 1333ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_ro_mappable); 1334ff94bc40SHeiko Schocher err_bdi2: 1335ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_unmappable); 1336ff94bc40SHeiko Schocher err_bdi1: 1337ff94bc40SHeiko Schocher class_unregister(&mtd_class); 1338ff94bc40SHeiko Schocher err_reg: 1339ff94bc40SHeiko Schocher pr_err("Error registering mtd class or bdi: %d\n", ret); 1340ff94bc40SHeiko Schocher return ret; 1341ff94bc40SHeiko Schocher } 1342ff94bc40SHeiko Schocher 1343ff94bc40SHeiko Schocher static void __exit cleanup_mtd(void) 1344ff94bc40SHeiko Schocher { 1345ff94bc40SHeiko Schocher cleanup_mtdchar(); 1346ff94bc40SHeiko Schocher if (proc_mtd) 1347ff94bc40SHeiko Schocher remove_proc_entry("mtd", NULL); 1348ff94bc40SHeiko Schocher class_unregister(&mtd_class); 1349ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_unmappable); 1350ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_ro_mappable); 1351ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_rw_mappable); 1352ff94bc40SHeiko Schocher } 1353ff94bc40SHeiko Schocher 1354ff94bc40SHeiko Schocher module_init(init_mtd); 1355ff94bc40SHeiko Schocher module_exit(cleanup_mtd); 1356ff94bc40SHeiko Schocher #endif 1357ff94bc40SHeiko Schocher 1358ff94bc40SHeiko Schocher MODULE_LICENSE("GPL"); 1359ff94bc40SHeiko Schocher MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 1360ff94bc40SHeiko Schocher MODULE_DESCRIPTION("Core MTD registration and access routines"); 1361