183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2e29c22f5SKyungmin Park /*
3e29c22f5SKyungmin Park * Core registration and callback routines for MTD
4e29c22f5SKyungmin Park * drivers and users.
5e29c22f5SKyungmin Park *
6ff94bc40SHeiko Schocher * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
7ff94bc40SHeiko Schocher * Copyright © 2006 Red Hat UK Limited
8ff94bc40SHeiko Schocher *
9e29c22f5SKyungmin Park */
10e29c22f5SKyungmin Park
11ff94bc40SHeiko Schocher #ifndef __UBOOT__
12ff94bc40SHeiko Schocher #include <linux/module.h>
13ff94bc40SHeiko Schocher #include <linux/kernel.h>
14ff94bc40SHeiko Schocher #include <linux/ptrace.h>
15ff94bc40SHeiko Schocher #include <linux/seq_file.h>
16ff94bc40SHeiko Schocher #include <linux/string.h>
17ff94bc40SHeiko Schocher #include <linux/timer.h>
18ff94bc40SHeiko Schocher #include <linux/major.h>
19ff94bc40SHeiko Schocher #include <linux/fs.h>
20ff94bc40SHeiko Schocher #include <linux/err.h>
21ff94bc40SHeiko Schocher #include <linux/ioctl.h>
22ff94bc40SHeiko Schocher #include <linux/init.h>
23ff94bc40SHeiko Schocher #include <linux/proc_fs.h>
24ff94bc40SHeiko Schocher #include <linux/idr.h>
25ff94bc40SHeiko Schocher #include <linux/backing-dev.h>
26ff94bc40SHeiko Schocher #include <linux/gfp.h>
27ff94bc40SHeiko Schocher #include <linux/slab.h>
28ff94bc40SHeiko Schocher #else
29ff94bc40SHeiko Schocher #include <linux/err.h>
30e29c22f5SKyungmin Park #include <ubi_uboot.h>
31ff94bc40SHeiko Schocher #endif
32e29c22f5SKyungmin Park
33f8fdb81fSFabio Estevam #include <linux/log2.h>
34ff94bc40SHeiko Schocher #include <linux/mtd/mtd.h>
35ff94bc40SHeiko Schocher #include <linux/mtd/partitions.h>
36ff94bc40SHeiko Schocher
37ff94bc40SHeiko Schocher #include "mtdcore.h"
38ff94bc40SHeiko Schocher
39ff94bc40SHeiko Schocher #ifndef __UBOOT__
40ff94bc40SHeiko Schocher /*
41ff94bc40SHeiko Schocher * backing device capabilities for non-mappable devices (such as NAND flash)
42ff94bc40SHeiko Schocher * - permits private mappings, copies are taken of the data
43ff94bc40SHeiko Schocher */
44ff94bc40SHeiko Schocher static struct backing_dev_info mtd_bdi_unmappable = {
45ff94bc40SHeiko Schocher .capabilities = BDI_CAP_MAP_COPY,
46ff94bc40SHeiko Schocher };
47ff94bc40SHeiko Schocher
48ff94bc40SHeiko Schocher /*
49ff94bc40SHeiko Schocher * backing device capabilities for R/O mappable devices (such as ROM)
50ff94bc40SHeiko Schocher * - permits private mappings, copies are taken of the data
51ff94bc40SHeiko Schocher * - permits non-writable shared mappings
52ff94bc40SHeiko Schocher */
53ff94bc40SHeiko Schocher static struct backing_dev_info mtd_bdi_ro_mappable = {
54ff94bc40SHeiko Schocher .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
55ff94bc40SHeiko Schocher BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
56ff94bc40SHeiko Schocher };
57ff94bc40SHeiko Schocher
58ff94bc40SHeiko Schocher /*
59ff94bc40SHeiko Schocher * backing device capabilities for writable mappable devices (such as RAM)
60ff94bc40SHeiko Schocher * - permits private mappings, copies are taken of the data
61ff94bc40SHeiko Schocher * - permits non-writable shared mappings
62ff94bc40SHeiko Schocher */
63ff94bc40SHeiko Schocher static struct backing_dev_info mtd_bdi_rw_mappable = {
64ff94bc40SHeiko Schocher .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
65ff94bc40SHeiko Schocher BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
66ff94bc40SHeiko Schocher BDI_CAP_WRITE_MAP),
67ff94bc40SHeiko Schocher };
68ff94bc40SHeiko Schocher
69ff94bc40SHeiko Schocher static int mtd_cls_suspend(struct device *dev, pm_message_t state);
70ff94bc40SHeiko Schocher static int mtd_cls_resume(struct device *dev);
71ff94bc40SHeiko Schocher
72ff94bc40SHeiko Schocher static struct class mtd_class = {
73ff94bc40SHeiko Schocher .name = "mtd",
74ff94bc40SHeiko Schocher .owner = THIS_MODULE,
75ff94bc40SHeiko Schocher .suspend = mtd_cls_suspend,
76ff94bc40SHeiko Schocher .resume = mtd_cls_resume,
77ff94bc40SHeiko Schocher };
78ff94bc40SHeiko Schocher #else
79e29c22f5SKyungmin Park struct mtd_info *mtd_table[MAX_MTD_DEVICES];
80e29c22f5SKyungmin Park
81ff94bc40SHeiko Schocher #define MAX_IDR_ID 64
82ff94bc40SHeiko Schocher
83ff94bc40SHeiko Schocher struct idr_layer {
84ff94bc40SHeiko Schocher int used;
85ff94bc40SHeiko Schocher void *ptr;
86ff94bc40SHeiko Schocher };
87ff94bc40SHeiko Schocher
88ff94bc40SHeiko Schocher struct idr {
89ff94bc40SHeiko Schocher struct idr_layer id[MAX_IDR_ID];
904c47fd0bSBoris Brezillon bool updated;
91ff94bc40SHeiko Schocher };
92ff94bc40SHeiko Schocher
93ff94bc40SHeiko Schocher #define DEFINE_IDR(name) struct idr name;
94ff94bc40SHeiko Schocher
idr_remove(struct idr * idp,int id)95ff94bc40SHeiko Schocher void idr_remove(struct idr *idp, int id)
96ff94bc40SHeiko Schocher {
974c47fd0bSBoris Brezillon if (idp->id[id].used) {
98ff94bc40SHeiko Schocher idp->id[id].used = 0;
994c47fd0bSBoris Brezillon idp->updated = true;
1004c47fd0bSBoris Brezillon }
101ff94bc40SHeiko Schocher
102ff94bc40SHeiko Schocher return;
103ff94bc40SHeiko Schocher }
idr_find(struct idr * idp,int id)104ff94bc40SHeiko Schocher void *idr_find(struct idr *idp, int id)
105ff94bc40SHeiko Schocher {
106ff94bc40SHeiko Schocher if (idp->id[id].used)
107ff94bc40SHeiko Schocher return idp->id[id].ptr;
108ff94bc40SHeiko Schocher
109ff94bc40SHeiko Schocher return NULL;
110ff94bc40SHeiko Schocher }
111ff94bc40SHeiko Schocher
idr_get_next(struct idr * idp,int * next)112ff94bc40SHeiko Schocher void *idr_get_next(struct idr *idp, int *next)
113ff94bc40SHeiko Schocher {
114ff94bc40SHeiko Schocher void *ret;
115ff94bc40SHeiko Schocher int id = *next;
116ff94bc40SHeiko Schocher
117ff94bc40SHeiko Schocher ret = idr_find(idp, id);
118ff94bc40SHeiko Schocher if (ret) {
119ff94bc40SHeiko Schocher id ++;
120ff94bc40SHeiko Schocher if (!idp->id[id].used)
121ff94bc40SHeiko Schocher id = 0;
122ff94bc40SHeiko Schocher *next = id;
123ff94bc40SHeiko Schocher } else {
124ff94bc40SHeiko Schocher *next = 0;
125ff94bc40SHeiko Schocher }
126ff94bc40SHeiko Schocher
127ff94bc40SHeiko Schocher return ret;
128ff94bc40SHeiko Schocher }
129ff94bc40SHeiko Schocher
idr_alloc(struct idr * idp,void * ptr,int start,int end,gfp_t gfp_mask)130ff94bc40SHeiko Schocher int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
131ff94bc40SHeiko Schocher {
132ff94bc40SHeiko Schocher struct idr_layer *idl;
133ff94bc40SHeiko Schocher int i = 0;
134ff94bc40SHeiko Schocher
135ff94bc40SHeiko Schocher while (i < MAX_IDR_ID) {
136ff94bc40SHeiko Schocher idl = &idp->id[i];
137ff94bc40SHeiko Schocher if (idl->used == 0) {
138ff94bc40SHeiko Schocher idl->used = 1;
139ff94bc40SHeiko Schocher idl->ptr = ptr;
1404c47fd0bSBoris Brezillon idp->updated = true;
141ff94bc40SHeiko Schocher return i;
142ff94bc40SHeiko Schocher }
143ff94bc40SHeiko Schocher i++;
144ff94bc40SHeiko Schocher }
145ff94bc40SHeiko Schocher return -ENOSPC;
146ff94bc40SHeiko Schocher }
147ff94bc40SHeiko Schocher #endif
148ff94bc40SHeiko Schocher
149ff94bc40SHeiko Schocher static DEFINE_IDR(mtd_idr);
150ff94bc40SHeiko Schocher
151ff94bc40SHeiko Schocher /* These are exported solely for the purpose of mtd_blkdevs.c. You
152ff94bc40SHeiko Schocher should not use them for _anything_ else */
153ff94bc40SHeiko Schocher DEFINE_MUTEX(mtd_table_mutex);
154ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_table_mutex);
155ff94bc40SHeiko Schocher
__mtd_next_device(int i)156ff94bc40SHeiko Schocher struct mtd_info *__mtd_next_device(int i)
157ff94bc40SHeiko Schocher {
158ff94bc40SHeiko Schocher return idr_get_next(&mtd_idr, &i);
159ff94bc40SHeiko Schocher }
160ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(__mtd_next_device);
161ff94bc40SHeiko Schocher
mtd_dev_list_updated(void)1624c47fd0bSBoris Brezillon bool mtd_dev_list_updated(void)
1634c47fd0bSBoris Brezillon {
1644c47fd0bSBoris Brezillon if (mtd_idr.updated) {
1654c47fd0bSBoris Brezillon mtd_idr.updated = false;
1664c47fd0bSBoris Brezillon return true;
1674c47fd0bSBoris Brezillon }
1684c47fd0bSBoris Brezillon
1694c47fd0bSBoris Brezillon return false;
1704c47fd0bSBoris Brezillon }
1714c47fd0bSBoris Brezillon
172ff94bc40SHeiko Schocher #ifndef __UBOOT__
173ff94bc40SHeiko Schocher static LIST_HEAD(mtd_notifiers);
174ff94bc40SHeiko Schocher
175ff94bc40SHeiko Schocher
176ff94bc40SHeiko Schocher #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
177ff94bc40SHeiko Schocher
178ff94bc40SHeiko Schocher /* REVISIT once MTD uses the driver model better, whoever allocates
179ff94bc40SHeiko Schocher * the mtd_info will probably want to use the release() hook...
180ff94bc40SHeiko Schocher */
mtd_release(struct device * dev)181ff94bc40SHeiko Schocher static void mtd_release(struct device *dev)
182ff94bc40SHeiko Schocher {
183ff94bc40SHeiko Schocher struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
184ff94bc40SHeiko Schocher dev_t index = MTD_DEVT(mtd->index);
185ff94bc40SHeiko Schocher
186ff94bc40SHeiko Schocher /* remove /dev/mtdXro node if needed */
187ff94bc40SHeiko Schocher if (index)
188ff94bc40SHeiko Schocher device_destroy(&mtd_class, index + 1);
189ff94bc40SHeiko Schocher }
190ff94bc40SHeiko Schocher
mtd_cls_suspend(struct device * dev,pm_message_t state)191ff94bc40SHeiko Schocher static int mtd_cls_suspend(struct device *dev, pm_message_t state)
192ff94bc40SHeiko Schocher {
193ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
194ff94bc40SHeiko Schocher
195ff94bc40SHeiko Schocher return mtd ? mtd_suspend(mtd) : 0;
196ff94bc40SHeiko Schocher }
197ff94bc40SHeiko Schocher
mtd_cls_resume(struct device * dev)198ff94bc40SHeiko Schocher static int mtd_cls_resume(struct device *dev)
199ff94bc40SHeiko Schocher {
200ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
201ff94bc40SHeiko Schocher
202ff94bc40SHeiko Schocher if (mtd)
203ff94bc40SHeiko Schocher mtd_resume(mtd);
204ff94bc40SHeiko Schocher return 0;
205ff94bc40SHeiko Schocher }
206ff94bc40SHeiko Schocher
mtd_type_show(struct device * dev,struct device_attribute * attr,char * buf)207ff94bc40SHeiko Schocher static ssize_t mtd_type_show(struct device *dev,
208ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
209ff94bc40SHeiko Schocher {
210ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
211ff94bc40SHeiko Schocher char *type;
212ff94bc40SHeiko Schocher
213ff94bc40SHeiko Schocher switch (mtd->type) {
214ff94bc40SHeiko Schocher case MTD_ABSENT:
215ff94bc40SHeiko Schocher type = "absent";
216ff94bc40SHeiko Schocher break;
217ff94bc40SHeiko Schocher case MTD_RAM:
218ff94bc40SHeiko Schocher type = "ram";
219ff94bc40SHeiko Schocher break;
220ff94bc40SHeiko Schocher case MTD_ROM:
221ff94bc40SHeiko Schocher type = "rom";
222ff94bc40SHeiko Schocher break;
223ff94bc40SHeiko Schocher case MTD_NORFLASH:
224ff94bc40SHeiko Schocher type = "nor";
225ff94bc40SHeiko Schocher break;
226ff94bc40SHeiko Schocher case MTD_NANDFLASH:
227ff94bc40SHeiko Schocher type = "nand";
228ff94bc40SHeiko Schocher break;
229ff94bc40SHeiko Schocher case MTD_DATAFLASH:
230ff94bc40SHeiko Schocher type = "dataflash";
231ff94bc40SHeiko Schocher break;
232ff94bc40SHeiko Schocher case MTD_UBIVOLUME:
233ff94bc40SHeiko Schocher type = "ubi";
234ff94bc40SHeiko Schocher break;
235ff94bc40SHeiko Schocher case MTD_MLCNANDFLASH:
236ff94bc40SHeiko Schocher type = "mlc-nand";
237ff94bc40SHeiko Schocher break;
238ff94bc40SHeiko Schocher default:
239ff94bc40SHeiko Schocher type = "unknown";
240ff94bc40SHeiko Schocher }
241ff94bc40SHeiko Schocher
242ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%s\n", type);
243ff94bc40SHeiko Schocher }
244ff94bc40SHeiko Schocher static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
245ff94bc40SHeiko Schocher
mtd_flags_show(struct device * dev,struct device_attribute * attr,char * buf)246ff94bc40SHeiko Schocher static ssize_t mtd_flags_show(struct device *dev,
247ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
248ff94bc40SHeiko Schocher {
249ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
250ff94bc40SHeiko Schocher
251ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
252ff94bc40SHeiko Schocher
253ff94bc40SHeiko Schocher }
254ff94bc40SHeiko Schocher static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
255ff94bc40SHeiko Schocher
mtd_size_show(struct device * dev,struct device_attribute * attr,char * buf)256ff94bc40SHeiko Schocher static ssize_t mtd_size_show(struct device *dev,
257ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
258ff94bc40SHeiko Schocher {
259ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
260ff94bc40SHeiko Schocher
261ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%llu\n",
262ff94bc40SHeiko Schocher (unsigned long long)mtd->size);
263ff94bc40SHeiko Schocher
264ff94bc40SHeiko Schocher }
265ff94bc40SHeiko Schocher static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
266ff94bc40SHeiko Schocher
mtd_erasesize_show(struct device * dev,struct device_attribute * attr,char * buf)267ff94bc40SHeiko Schocher static ssize_t mtd_erasesize_show(struct device *dev,
268ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
269ff94bc40SHeiko Schocher {
270ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
271ff94bc40SHeiko Schocher
272ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
273ff94bc40SHeiko Schocher
274ff94bc40SHeiko Schocher }
275ff94bc40SHeiko Schocher static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
276ff94bc40SHeiko Schocher
mtd_writesize_show(struct device * dev,struct device_attribute * attr,char * buf)277ff94bc40SHeiko Schocher static ssize_t mtd_writesize_show(struct device *dev,
278ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
279ff94bc40SHeiko Schocher {
280ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
281ff94bc40SHeiko Schocher
282ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
283ff94bc40SHeiko Schocher
284ff94bc40SHeiko Schocher }
285ff94bc40SHeiko Schocher static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
286ff94bc40SHeiko Schocher
mtd_subpagesize_show(struct device * dev,struct device_attribute * attr,char * buf)287ff94bc40SHeiko Schocher static ssize_t mtd_subpagesize_show(struct device *dev,
288ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
289ff94bc40SHeiko Schocher {
290ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
291ff94bc40SHeiko Schocher unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
292ff94bc40SHeiko Schocher
293ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
294ff94bc40SHeiko Schocher
295ff94bc40SHeiko Schocher }
296ff94bc40SHeiko Schocher static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
297ff94bc40SHeiko Schocher
mtd_oobsize_show(struct device * dev,struct device_attribute * attr,char * buf)298ff94bc40SHeiko Schocher static ssize_t mtd_oobsize_show(struct device *dev,
299ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
300ff94bc40SHeiko Schocher {
301ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
302ff94bc40SHeiko Schocher
303ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
304ff94bc40SHeiko Schocher
305ff94bc40SHeiko Schocher }
306ff94bc40SHeiko Schocher static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
307ff94bc40SHeiko Schocher
mtd_numeraseregions_show(struct device * dev,struct device_attribute * attr,char * buf)308ff94bc40SHeiko Schocher static ssize_t mtd_numeraseregions_show(struct device *dev,
309ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
310ff94bc40SHeiko Schocher {
311ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
312ff94bc40SHeiko Schocher
313ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
314ff94bc40SHeiko Schocher
315ff94bc40SHeiko Schocher }
316ff94bc40SHeiko Schocher static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
317ff94bc40SHeiko Schocher NULL);
318ff94bc40SHeiko Schocher
mtd_name_show(struct device * dev,struct device_attribute * attr,char * buf)319ff94bc40SHeiko Schocher static ssize_t mtd_name_show(struct device *dev,
320ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
321ff94bc40SHeiko Schocher {
322ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
323ff94bc40SHeiko Schocher
324ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
325ff94bc40SHeiko Schocher
326ff94bc40SHeiko Schocher }
327ff94bc40SHeiko Schocher static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
328ff94bc40SHeiko Schocher
mtd_ecc_strength_show(struct device * dev,struct device_attribute * attr,char * buf)329ff94bc40SHeiko Schocher static ssize_t mtd_ecc_strength_show(struct device *dev,
330ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
331ff94bc40SHeiko Schocher {
332ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
333ff94bc40SHeiko Schocher
334ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
335ff94bc40SHeiko Schocher }
336ff94bc40SHeiko Schocher static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
337ff94bc40SHeiko Schocher
mtd_bitflip_threshold_show(struct device * dev,struct device_attribute * attr,char * buf)338ff94bc40SHeiko Schocher static ssize_t mtd_bitflip_threshold_show(struct device *dev,
339ff94bc40SHeiko Schocher struct device_attribute *attr,
340ff94bc40SHeiko Schocher char *buf)
341ff94bc40SHeiko Schocher {
342ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
343ff94bc40SHeiko Schocher
344ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
345ff94bc40SHeiko Schocher }
346ff94bc40SHeiko Schocher
mtd_bitflip_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)347ff94bc40SHeiko Schocher static ssize_t mtd_bitflip_threshold_store(struct device *dev,
348ff94bc40SHeiko Schocher struct device_attribute *attr,
349ff94bc40SHeiko Schocher const char *buf, size_t count)
350ff94bc40SHeiko Schocher {
351ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
352ff94bc40SHeiko Schocher unsigned int bitflip_threshold;
353ff94bc40SHeiko Schocher int retval;
354ff94bc40SHeiko Schocher
355ff94bc40SHeiko Schocher retval = kstrtouint(buf, 0, &bitflip_threshold);
356ff94bc40SHeiko Schocher if (retval)
357ff94bc40SHeiko Schocher return retval;
358ff94bc40SHeiko Schocher
359ff94bc40SHeiko Schocher mtd->bitflip_threshold = bitflip_threshold;
360ff94bc40SHeiko Schocher return count;
361ff94bc40SHeiko Schocher }
362ff94bc40SHeiko Schocher static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
363ff94bc40SHeiko Schocher mtd_bitflip_threshold_show,
364ff94bc40SHeiko Schocher mtd_bitflip_threshold_store);
365ff94bc40SHeiko Schocher
mtd_ecc_step_size_show(struct device * dev,struct device_attribute * attr,char * buf)366ff94bc40SHeiko Schocher static ssize_t mtd_ecc_step_size_show(struct device *dev,
367ff94bc40SHeiko Schocher struct device_attribute *attr, char *buf)
368ff94bc40SHeiko Schocher {
369ff94bc40SHeiko Schocher struct mtd_info *mtd = dev_get_drvdata(dev);
370ff94bc40SHeiko Schocher
371ff94bc40SHeiko Schocher return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
372ff94bc40SHeiko Schocher
373ff94bc40SHeiko Schocher }
374ff94bc40SHeiko Schocher static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
375ff94bc40SHeiko Schocher
376ff94bc40SHeiko Schocher static struct attribute *mtd_attrs[] = {
377ff94bc40SHeiko Schocher &dev_attr_type.attr,
378ff94bc40SHeiko Schocher &dev_attr_flags.attr,
379ff94bc40SHeiko Schocher &dev_attr_size.attr,
380ff94bc40SHeiko Schocher &dev_attr_erasesize.attr,
381ff94bc40SHeiko Schocher &dev_attr_writesize.attr,
382ff94bc40SHeiko Schocher &dev_attr_subpagesize.attr,
383ff94bc40SHeiko Schocher &dev_attr_oobsize.attr,
384ff94bc40SHeiko Schocher &dev_attr_numeraseregions.attr,
385ff94bc40SHeiko Schocher &dev_attr_name.attr,
386ff94bc40SHeiko Schocher &dev_attr_ecc_strength.attr,
387ff94bc40SHeiko Schocher &dev_attr_ecc_step_size.attr,
388ff94bc40SHeiko Schocher &dev_attr_bitflip_threshold.attr,
389ff94bc40SHeiko Schocher NULL,
390ff94bc40SHeiko Schocher };
391ff94bc40SHeiko Schocher ATTRIBUTE_GROUPS(mtd);
392ff94bc40SHeiko Schocher
393ff94bc40SHeiko Schocher static struct device_type mtd_devtype = {
394ff94bc40SHeiko Schocher .name = "mtd",
395ff94bc40SHeiko Schocher .groups = mtd_groups,
396ff94bc40SHeiko Schocher .release = mtd_release,
397ff94bc40SHeiko Schocher };
398ff94bc40SHeiko Schocher #endif
399ff94bc40SHeiko Schocher
400ff94bc40SHeiko Schocher /**
401ff94bc40SHeiko Schocher * add_mtd_device - register an MTD device
402ff94bc40SHeiko Schocher * @mtd: pointer to new MTD device info structure
403ff94bc40SHeiko Schocher *
404ff94bc40SHeiko Schocher * Add a device to the list of MTD devices present in the system, and
405ff94bc40SHeiko Schocher * notify each currently active MTD 'user' of its arrival. Returns
406ff94bc40SHeiko Schocher * zero on success or 1 on failure, which currently will only happen
407ff94bc40SHeiko Schocher * if there is insufficient memory or a sysfs error.
408ff94bc40SHeiko Schocher */
409ff94bc40SHeiko Schocher
add_mtd_device(struct mtd_info * mtd)410e29c22f5SKyungmin Park int add_mtd_device(struct mtd_info *mtd)
411e29c22f5SKyungmin Park {
412ff94bc40SHeiko Schocher #ifndef __UBOOT__
413ff94bc40SHeiko Schocher struct mtd_notifier *not;
414ff94bc40SHeiko Schocher #endif
415ff94bc40SHeiko Schocher int i, error;
416ff94bc40SHeiko Schocher
417ff94bc40SHeiko Schocher #ifndef __UBOOT__
418ff94bc40SHeiko Schocher if (!mtd->backing_dev_info) {
419ff94bc40SHeiko Schocher switch (mtd->type) {
420ff94bc40SHeiko Schocher case MTD_RAM:
421ff94bc40SHeiko Schocher mtd->backing_dev_info = &mtd_bdi_rw_mappable;
422ff94bc40SHeiko Schocher break;
423ff94bc40SHeiko Schocher case MTD_ROM:
424ff94bc40SHeiko Schocher mtd->backing_dev_info = &mtd_bdi_ro_mappable;
425ff94bc40SHeiko Schocher break;
426ff94bc40SHeiko Schocher default:
427ff94bc40SHeiko Schocher mtd->backing_dev_info = &mtd_bdi_unmappable;
428ff94bc40SHeiko Schocher break;
429ff94bc40SHeiko Schocher }
430ff94bc40SHeiko Schocher }
431ff94bc40SHeiko Schocher #endif
432e29c22f5SKyungmin Park
433e29c22f5SKyungmin Park BUG_ON(mtd->writesize == 0);
434ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex);
435e29c22f5SKyungmin Park
436ff94bc40SHeiko Schocher i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
437ff94bc40SHeiko Schocher if (i < 0)
438ff94bc40SHeiko Schocher goto fail_locked;
439ff94bc40SHeiko Schocher
440e29c22f5SKyungmin Park mtd->index = i;
441e29c22f5SKyungmin Park mtd->usecount = 0;
442e29c22f5SKyungmin Park
4432a74930dSMiquel Raynal INIT_LIST_HEAD(&mtd->partitions);
4442a74930dSMiquel Raynal
445dfe64e2cSSergey Lapin /* default value if not set by driver */
446dfe64e2cSSergey Lapin if (mtd->bitflip_threshold == 0)
447dfe64e2cSSergey Lapin mtd->bitflip_threshold = mtd->ecc_strength;
448dfe64e2cSSergey Lapin
449ff94bc40SHeiko Schocher if (is_power_of_2(mtd->erasesize))
450ff94bc40SHeiko Schocher mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
451ff94bc40SHeiko Schocher else
452ff94bc40SHeiko Schocher mtd->erasesize_shift = 0;
453dfe64e2cSSergey Lapin
454ff94bc40SHeiko Schocher if (is_power_of_2(mtd->writesize))
455ff94bc40SHeiko Schocher mtd->writesize_shift = ffs(mtd->writesize) - 1;
456ff94bc40SHeiko Schocher else
457ff94bc40SHeiko Schocher mtd->writesize_shift = 0;
458ff94bc40SHeiko Schocher
459ff94bc40SHeiko Schocher mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
460ff94bc40SHeiko Schocher mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
461ff94bc40SHeiko Schocher
462ff94bc40SHeiko Schocher /* Some chips always power up locked. Unlock them now */
463ff94bc40SHeiko Schocher if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
464ff94bc40SHeiko Schocher error = mtd_unlock(mtd, 0, mtd->size);
465ff94bc40SHeiko Schocher if (error && error != -EOPNOTSUPP)
466ff94bc40SHeiko Schocher printk(KERN_WARNING
467ff94bc40SHeiko Schocher "%s: unlock failed, writes may not work\n",
468ff94bc40SHeiko Schocher mtd->name);
469ff94bc40SHeiko Schocher }
470ff94bc40SHeiko Schocher
471ff94bc40SHeiko Schocher #ifndef __UBOOT__
472ff94bc40SHeiko Schocher /* Caller should have set dev.parent to match the
473ff94bc40SHeiko Schocher * physical device.
474ff94bc40SHeiko Schocher */
475ff94bc40SHeiko Schocher mtd->dev.type = &mtd_devtype;
476ff94bc40SHeiko Schocher mtd->dev.class = &mtd_class;
477ff94bc40SHeiko Schocher mtd->dev.devt = MTD_DEVT(i);
478ff94bc40SHeiko Schocher dev_set_name(&mtd->dev, "mtd%d", i);
479ff94bc40SHeiko Schocher dev_set_drvdata(&mtd->dev, mtd);
480ff94bc40SHeiko Schocher if (device_register(&mtd->dev) != 0)
481ff94bc40SHeiko Schocher goto fail_added;
482ff94bc40SHeiko Schocher
483ff94bc40SHeiko Schocher if (MTD_DEVT(i))
484ff94bc40SHeiko Schocher device_create(&mtd_class, mtd->dev.parent,
485ff94bc40SHeiko Schocher MTD_DEVT(i) + 1,
486ff94bc40SHeiko Schocher NULL, "mtd%dro", i);
487ff94bc40SHeiko Schocher
488ff94bc40SHeiko Schocher pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
489e29c22f5SKyungmin Park /* No need to get a refcount on the module containing
490e29c22f5SKyungmin Park the notifier, since we hold the mtd_table_mutex */
491ff94bc40SHeiko Schocher list_for_each_entry(not, &mtd_notifiers, list)
492ff94bc40SHeiko Schocher not->add(mtd);
493ddf7bcfaSHeiko Schocher #else
494ddf7bcfaSHeiko Schocher pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
495ff94bc40SHeiko Schocher #endif
496e29c22f5SKyungmin Park
497ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
498e29c22f5SKyungmin Park /* We _know_ we aren't being removed, because
499e29c22f5SKyungmin Park our caller is still holding us here. So none
500e29c22f5SKyungmin Park of this try_ nonsense, and no bitching about it
501e29c22f5SKyungmin Park either. :) */
502ff94bc40SHeiko Schocher __module_get(THIS_MODULE);
503e29c22f5SKyungmin Park return 0;
504e29c22f5SKyungmin Park
505ff94bc40SHeiko Schocher #ifndef __UBOOT__
506ff94bc40SHeiko Schocher fail_added:
507ff94bc40SHeiko Schocher idr_remove(&mtd_idr, i);
508ff94bc40SHeiko Schocher #endif
509ff94bc40SHeiko Schocher fail_locked:
510ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
511e29c22f5SKyungmin Park return 1;
512e29c22f5SKyungmin Park }
513e29c22f5SKyungmin Park
514e29c22f5SKyungmin Park /**
515e29c22f5SKyungmin Park * del_mtd_device - unregister an MTD device
516e29c22f5SKyungmin Park * @mtd: pointer to MTD device info structure
517e29c22f5SKyungmin Park *
518e29c22f5SKyungmin Park * Remove a device from the list of MTD devices present in the system,
519e29c22f5SKyungmin Park * and notify each currently active MTD 'user' of its departure.
520e29c22f5SKyungmin Park * Returns zero on success or 1 on failure, which currently will happen
521e29c22f5SKyungmin Park * if the requested device does not appear to be present in the list.
522e29c22f5SKyungmin Park */
523ff94bc40SHeiko Schocher
del_mtd_device(struct mtd_info * mtd)524e29c22f5SKyungmin Park int del_mtd_device(struct mtd_info *mtd)
525e29c22f5SKyungmin Park {
526e29c22f5SKyungmin Park int ret;
527ff94bc40SHeiko Schocher #ifndef __UBOOT__
528ff94bc40SHeiko Schocher struct mtd_notifier *not;
529ff94bc40SHeiko Schocher #endif
530e29c22f5SKyungmin Park
531*a02820fcSBoris Brezillon ret = del_mtd_partitions(mtd);
532*a02820fcSBoris Brezillon if (ret) {
533*a02820fcSBoris Brezillon debug("Failed to delete MTD partitions attached to %s (err %d)\n",
534*a02820fcSBoris Brezillon mtd->name, ret);
535*a02820fcSBoris Brezillon return ret;
536*a02820fcSBoris Brezillon }
537*a02820fcSBoris Brezillon
538ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex);
539ff94bc40SHeiko Schocher
540ff94bc40SHeiko Schocher if (idr_find(&mtd_idr, mtd->index) != mtd) {
541e29c22f5SKyungmin Park ret = -ENODEV;
542ff94bc40SHeiko Schocher goto out_error;
543ff94bc40SHeiko Schocher }
544ff94bc40SHeiko Schocher
545ff94bc40SHeiko Schocher #ifndef __UBOOT__
546ff94bc40SHeiko Schocher /* No need to get a refcount on the module containing
547ff94bc40SHeiko Schocher the notifier, since we hold the mtd_table_mutex */
548ff94bc40SHeiko Schocher list_for_each_entry(not, &mtd_notifiers, list)
549ff94bc40SHeiko Schocher not->remove(mtd);
550ff94bc40SHeiko Schocher #endif
551ff94bc40SHeiko Schocher
552ff94bc40SHeiko Schocher if (mtd->usecount) {
553ff94bc40SHeiko Schocher printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
554e29c22f5SKyungmin Park mtd->index, mtd->name, mtd->usecount);
555e29c22f5SKyungmin Park ret = -EBUSY;
556e29c22f5SKyungmin Park } else {
557ff94bc40SHeiko Schocher #ifndef __UBOOT__
558ff94bc40SHeiko Schocher device_unregister(&mtd->dev);
559ff94bc40SHeiko Schocher #endif
560e29c22f5SKyungmin Park
561ff94bc40SHeiko Schocher idr_remove(&mtd_idr, mtd->index);
562ff94bc40SHeiko Schocher
563ff94bc40SHeiko Schocher module_put(THIS_MODULE);
564e29c22f5SKyungmin Park ret = 0;
565e29c22f5SKyungmin Park }
566e29c22f5SKyungmin Park
567ff94bc40SHeiko Schocher out_error:
568ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
569e29c22f5SKyungmin Park return ret;
570e29c22f5SKyungmin Park }
571e29c22f5SKyungmin Park
572ff94bc40SHeiko Schocher #ifndef __UBOOT__
573ff94bc40SHeiko Schocher /**
574ff94bc40SHeiko Schocher * mtd_device_parse_register - parse partitions and register an MTD device.
575ff94bc40SHeiko Schocher *
576ff94bc40SHeiko Schocher * @mtd: the MTD device to register
577ff94bc40SHeiko Schocher * @types: the list of MTD partition probes to try, see
578ff94bc40SHeiko Schocher * 'parse_mtd_partitions()' for more information
579ff94bc40SHeiko Schocher * @parser_data: MTD partition parser-specific data
580ff94bc40SHeiko Schocher * @parts: fallback partition information to register, if parsing fails;
581ff94bc40SHeiko Schocher * only valid if %nr_parts > %0
582ff94bc40SHeiko Schocher * @nr_parts: the number of partitions in parts, if zero then the full
583ff94bc40SHeiko Schocher * MTD device is registered if no partition info is found
584ff94bc40SHeiko Schocher *
585ff94bc40SHeiko Schocher * This function aggregates MTD partitions parsing (done by
586ff94bc40SHeiko Schocher * 'parse_mtd_partitions()') and MTD device and partitions registering. It
587ff94bc40SHeiko Schocher * basically follows the most common pattern found in many MTD drivers:
588ff94bc40SHeiko Schocher *
589ff94bc40SHeiko Schocher * * It first tries to probe partitions on MTD device @mtd using parsers
590ff94bc40SHeiko Schocher * specified in @types (if @types is %NULL, then the default list of parsers
591ff94bc40SHeiko Schocher * is used, see 'parse_mtd_partitions()' for more information). If none are
592ff94bc40SHeiko Schocher * found this functions tries to fallback to information specified in
593ff94bc40SHeiko Schocher * @parts/@nr_parts.
594ff94bc40SHeiko Schocher * * If any partitioning info was found, this function registers the found
595ff94bc40SHeiko Schocher * partitions.
596ff94bc40SHeiko Schocher * * If no partitions were found this function just registers the MTD device
597ff94bc40SHeiko Schocher * @mtd and exits.
598ff94bc40SHeiko Schocher *
599ff94bc40SHeiko Schocher * Returns zero in case of success and a negative error code in case of failure.
600ff94bc40SHeiko Schocher */
mtd_device_parse_register(struct mtd_info * mtd,const char * const * types,struct mtd_part_parser_data * parser_data,const struct mtd_partition * parts,int nr_parts)601ff94bc40SHeiko Schocher int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
602ff94bc40SHeiko Schocher struct mtd_part_parser_data *parser_data,
603ff94bc40SHeiko Schocher const struct mtd_partition *parts,
604ff94bc40SHeiko Schocher int nr_parts)
605ff94bc40SHeiko Schocher {
606ff94bc40SHeiko Schocher int err;
607ff94bc40SHeiko Schocher struct mtd_partition *real_parts;
608ff94bc40SHeiko Schocher
609ff94bc40SHeiko Schocher err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
610ff94bc40SHeiko Schocher if (err <= 0 && nr_parts && parts) {
611ff94bc40SHeiko Schocher real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
612ff94bc40SHeiko Schocher GFP_KERNEL);
613ff94bc40SHeiko Schocher if (!real_parts)
614ff94bc40SHeiko Schocher err = -ENOMEM;
615ff94bc40SHeiko Schocher else
616ff94bc40SHeiko Schocher err = nr_parts;
617ff94bc40SHeiko Schocher }
618ff94bc40SHeiko Schocher
619ff94bc40SHeiko Schocher if (err > 0) {
620ff94bc40SHeiko Schocher err = add_mtd_partitions(mtd, real_parts, err);
621ff94bc40SHeiko Schocher kfree(real_parts);
622ff94bc40SHeiko Schocher } else if (err == 0) {
623ff94bc40SHeiko Schocher err = add_mtd_device(mtd);
624ff94bc40SHeiko Schocher if (err == 1)
625ff94bc40SHeiko Schocher err = -ENODEV;
626ff94bc40SHeiko Schocher }
627ff94bc40SHeiko Schocher
628ff94bc40SHeiko Schocher return err;
629ff94bc40SHeiko Schocher }
630ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_device_parse_register);
631ff94bc40SHeiko Schocher
632ff94bc40SHeiko Schocher /**
633ff94bc40SHeiko Schocher * mtd_device_unregister - unregister an existing MTD device.
634ff94bc40SHeiko Schocher *
635ff94bc40SHeiko Schocher * @master: the MTD device to unregister. This will unregister both the master
636ff94bc40SHeiko Schocher * and any partitions if registered.
637ff94bc40SHeiko Schocher */
mtd_device_unregister(struct mtd_info * master)638ff94bc40SHeiko Schocher int mtd_device_unregister(struct mtd_info *master)
639ff94bc40SHeiko Schocher {
640ff94bc40SHeiko Schocher int err;
641ff94bc40SHeiko Schocher
642ff94bc40SHeiko Schocher err = del_mtd_partitions(master);
643ff94bc40SHeiko Schocher if (err)
644ff94bc40SHeiko Schocher return err;
645ff94bc40SHeiko Schocher
646ff94bc40SHeiko Schocher if (!device_is_registered(&master->dev))
647ff94bc40SHeiko Schocher return 0;
648ff94bc40SHeiko Schocher
649ff94bc40SHeiko Schocher return del_mtd_device(master);
650ff94bc40SHeiko Schocher }
651ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_device_unregister);
652ff94bc40SHeiko Schocher
653ff94bc40SHeiko Schocher /**
654ff94bc40SHeiko Schocher * register_mtd_user - register a 'user' of MTD devices.
655ff94bc40SHeiko Schocher * @new: pointer to notifier info structure
656ff94bc40SHeiko Schocher *
657ff94bc40SHeiko Schocher * Registers a pair of callbacks function to be called upon addition
658ff94bc40SHeiko Schocher * or removal of MTD devices. Causes the 'add' callback to be immediately
659ff94bc40SHeiko Schocher * invoked for each MTD device currently present in the system.
660ff94bc40SHeiko Schocher */
register_mtd_user(struct mtd_notifier * new)661ff94bc40SHeiko Schocher void register_mtd_user (struct mtd_notifier *new)
662ff94bc40SHeiko Schocher {
663ff94bc40SHeiko Schocher struct mtd_info *mtd;
664ff94bc40SHeiko Schocher
665ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex);
666ff94bc40SHeiko Schocher
667ff94bc40SHeiko Schocher list_add(&new->list, &mtd_notifiers);
668ff94bc40SHeiko Schocher
669ff94bc40SHeiko Schocher __module_get(THIS_MODULE);
670ff94bc40SHeiko Schocher
671ff94bc40SHeiko Schocher mtd_for_each_device(mtd)
672ff94bc40SHeiko Schocher new->add(mtd);
673ff94bc40SHeiko Schocher
674ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
675ff94bc40SHeiko Schocher }
676ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(register_mtd_user);
677ff94bc40SHeiko Schocher
678ff94bc40SHeiko Schocher /**
679ff94bc40SHeiko Schocher * unregister_mtd_user - unregister a 'user' of MTD devices.
680ff94bc40SHeiko Schocher * @old: pointer to notifier info structure
681ff94bc40SHeiko Schocher *
682ff94bc40SHeiko Schocher * Removes a callback function pair from the list of 'users' to be
683ff94bc40SHeiko Schocher * notified upon addition or removal of MTD devices. Causes the
684ff94bc40SHeiko Schocher * 'remove' callback to be immediately invoked for each MTD device
685ff94bc40SHeiko Schocher * currently present in the system.
686ff94bc40SHeiko Schocher */
unregister_mtd_user(struct mtd_notifier * old)687ff94bc40SHeiko Schocher int unregister_mtd_user (struct mtd_notifier *old)
688ff94bc40SHeiko Schocher {
689ff94bc40SHeiko Schocher struct mtd_info *mtd;
690ff94bc40SHeiko Schocher
691ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex);
692ff94bc40SHeiko Schocher
693ff94bc40SHeiko Schocher module_put(THIS_MODULE);
694ff94bc40SHeiko Schocher
695ff94bc40SHeiko Schocher mtd_for_each_device(mtd)
696ff94bc40SHeiko Schocher old->remove(mtd);
697ff94bc40SHeiko Schocher
698ff94bc40SHeiko Schocher list_del(&old->list);
699ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
700ff94bc40SHeiko Schocher return 0;
701ff94bc40SHeiko Schocher }
702ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(unregister_mtd_user);
703ff94bc40SHeiko Schocher #endif
704ff94bc40SHeiko Schocher
705e29c22f5SKyungmin Park /**
706e29c22f5SKyungmin Park * get_mtd_device - obtain a validated handle for an MTD device
707e29c22f5SKyungmin Park * @mtd: last known address of the required MTD device
708e29c22f5SKyungmin Park * @num: internal device number of the required MTD device
709e29c22f5SKyungmin Park *
710e29c22f5SKyungmin Park * Given a number and NULL address, return the num'th entry in the device
711e29c22f5SKyungmin Park * table, if any. Given an address and num == -1, search the device table
712e29c22f5SKyungmin Park * for a device with that address and return if it's still present. Given
713e29c22f5SKyungmin Park * both, return the num'th driver only if its address matches. Return
714e29c22f5SKyungmin Park * error code if not.
715e29c22f5SKyungmin Park */
get_mtd_device(struct mtd_info * mtd,int num)716e29c22f5SKyungmin Park struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
717e29c22f5SKyungmin Park {
718ff94bc40SHeiko Schocher struct mtd_info *ret = NULL, *other;
719ff94bc40SHeiko Schocher int err = -ENODEV;
720ff94bc40SHeiko Schocher
721ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex);
722e29c22f5SKyungmin Park
723e29c22f5SKyungmin Park if (num == -1) {
724ff94bc40SHeiko Schocher mtd_for_each_device(other) {
725ff94bc40SHeiko Schocher if (other == mtd) {
726ff94bc40SHeiko Schocher ret = mtd;
727ff94bc40SHeiko Schocher break;
728ff94bc40SHeiko Schocher }
729ff94bc40SHeiko Schocher }
730ff94bc40SHeiko Schocher } else if (num >= 0) {
731ff94bc40SHeiko Schocher ret = idr_find(&mtd_idr, num);
732e29c22f5SKyungmin Park if (mtd && mtd != ret)
733e29c22f5SKyungmin Park ret = NULL;
734e29c22f5SKyungmin Park }
735e29c22f5SKyungmin Park
736ff94bc40SHeiko Schocher if (!ret) {
737ff94bc40SHeiko Schocher ret = ERR_PTR(err);
738ff94bc40SHeiko Schocher goto out;
739e29c22f5SKyungmin Park }
740e29c22f5SKyungmin Park
741ff94bc40SHeiko Schocher err = __get_mtd_device(ret);
742ff94bc40SHeiko Schocher if (err)
743ff94bc40SHeiko Schocher ret = ERR_PTR(err);
744ff94bc40SHeiko Schocher out:
745ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
746ff94bc40SHeiko Schocher return ret;
747ff94bc40SHeiko Schocher }
748ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(get_mtd_device);
749ff94bc40SHeiko Schocher
750ff94bc40SHeiko Schocher
__get_mtd_device(struct mtd_info * mtd)751ff94bc40SHeiko Schocher int __get_mtd_device(struct mtd_info *mtd)
752ff94bc40SHeiko Schocher {
753ff94bc40SHeiko Schocher int err;
754ff94bc40SHeiko Schocher
755ff94bc40SHeiko Schocher if (!try_module_get(mtd->owner))
756ff94bc40SHeiko Schocher return -ENODEV;
757ff94bc40SHeiko Schocher
758ff94bc40SHeiko Schocher if (mtd->_get_device) {
759ff94bc40SHeiko Schocher err = mtd->_get_device(mtd);
760ff94bc40SHeiko Schocher
761ff94bc40SHeiko Schocher if (err) {
762ff94bc40SHeiko Schocher module_put(mtd->owner);
763ff94bc40SHeiko Schocher return err;
764ff94bc40SHeiko Schocher }
765ff94bc40SHeiko Schocher }
766ff94bc40SHeiko Schocher mtd->usecount++;
767ff94bc40SHeiko Schocher return 0;
768ff94bc40SHeiko Schocher }
769ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(__get_mtd_device);
770ff94bc40SHeiko Schocher
771e29c22f5SKyungmin Park /**
772e29c22f5SKyungmin Park * get_mtd_device_nm - obtain a validated handle for an MTD device by
773e29c22f5SKyungmin Park * device name
774e29c22f5SKyungmin Park * @name: MTD device name to open
775e29c22f5SKyungmin Park *
776e29c22f5SKyungmin Park * This function returns MTD device description structure in case of
777e29c22f5SKyungmin Park * success and an error code in case of failure.
778e29c22f5SKyungmin Park */
get_mtd_device_nm(const char * name)779e29c22f5SKyungmin Park struct mtd_info *get_mtd_device_nm(const char *name)
780e29c22f5SKyungmin Park {
781ff94bc40SHeiko Schocher int err = -ENODEV;
782ff94bc40SHeiko Schocher struct mtd_info *mtd = NULL, *other;
783e29c22f5SKyungmin Park
784ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex);
785ff94bc40SHeiko Schocher
786ff94bc40SHeiko Schocher mtd_for_each_device(other) {
787ff94bc40SHeiko Schocher if (!strcmp(name, other->name)) {
788ff94bc40SHeiko Schocher mtd = other;
789e29c22f5SKyungmin Park break;
790e29c22f5SKyungmin Park }
791e29c22f5SKyungmin Park }
792e29c22f5SKyungmin Park
793e29c22f5SKyungmin Park if (!mtd)
794e29c22f5SKyungmin Park goto out_unlock;
795e29c22f5SKyungmin Park
796ff94bc40SHeiko Schocher err = __get_mtd_device(mtd);
797ff94bc40SHeiko Schocher if (err)
798ff94bc40SHeiko Schocher goto out_unlock;
799ff94bc40SHeiko Schocher
800ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
801e29c22f5SKyungmin Park return mtd;
802e29c22f5SKyungmin Park
803e29c22f5SKyungmin Park out_unlock:
804ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
805e29c22f5SKyungmin Park return ERR_PTR(err);
806e29c22f5SKyungmin Park }
807ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(get_mtd_device_nm);
8084ba692fbSBen Gardiner
8094ba692fbSBen Gardiner #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
8104ba692fbSBen Gardiner /**
8114ba692fbSBen Gardiner * mtd_get_len_incl_bad
8124ba692fbSBen Gardiner *
8134ba692fbSBen Gardiner * Check if length including bad blocks fits into device.
8144ba692fbSBen Gardiner *
8154ba692fbSBen Gardiner * @param mtd an MTD device
8164ba692fbSBen Gardiner * @param offset offset in flash
8174ba692fbSBen Gardiner * @param length image length
8184ba692fbSBen Gardiner * @return image length including bad blocks in *len_incl_bad and whether or not
8194ba692fbSBen Gardiner * the length returned was truncated in *truncated
8204ba692fbSBen Gardiner */
mtd_get_len_incl_bad(struct mtd_info * mtd,uint64_t offset,const uint64_t length,uint64_t * len_incl_bad,int * truncated)8214ba692fbSBen Gardiner void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
8224ba692fbSBen Gardiner const uint64_t length, uint64_t *len_incl_bad,
8234ba692fbSBen Gardiner int *truncated)
8244ba692fbSBen Gardiner {
8254ba692fbSBen Gardiner *truncated = 0;
8264ba692fbSBen Gardiner *len_incl_bad = 0;
8274ba692fbSBen Gardiner
8285da163d6Smaxin.john@enea.com if (!mtd->_block_isbad) {
8294ba692fbSBen Gardiner *len_incl_bad = length;
8304ba692fbSBen Gardiner return;
8314ba692fbSBen Gardiner }
8324ba692fbSBen Gardiner
8334ba692fbSBen Gardiner uint64_t len_excl_bad = 0;
8344ba692fbSBen Gardiner uint64_t block_len;
8354ba692fbSBen Gardiner
8364ba692fbSBen Gardiner while (len_excl_bad < length) {
83736650ca9SScott Wood if (offset >= mtd->size) {
83836650ca9SScott Wood *truncated = 1;
83936650ca9SScott Wood return;
84036650ca9SScott Wood }
84136650ca9SScott Wood
8424ba692fbSBen Gardiner block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
8434ba692fbSBen Gardiner
8445da163d6Smaxin.john@enea.com if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
8454ba692fbSBen Gardiner len_excl_bad += block_len;
8464ba692fbSBen Gardiner
8474ba692fbSBen Gardiner *len_incl_bad += block_len;
8484ba692fbSBen Gardiner offset += block_len;
8494ba692fbSBen Gardiner }
8504ba692fbSBen Gardiner }
8514ba692fbSBen Gardiner #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
852dfe64e2cSSergey Lapin
put_mtd_device(struct mtd_info * mtd)853ff94bc40SHeiko Schocher void put_mtd_device(struct mtd_info *mtd)
854ff94bc40SHeiko Schocher {
855ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex);
856ff94bc40SHeiko Schocher __put_mtd_device(mtd);
857ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
858ff94bc40SHeiko Schocher
859ff94bc40SHeiko Schocher }
860ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(put_mtd_device);
861ff94bc40SHeiko Schocher
__put_mtd_device(struct mtd_info * mtd)862ff94bc40SHeiko Schocher void __put_mtd_device(struct mtd_info *mtd)
863ff94bc40SHeiko Schocher {
864ff94bc40SHeiko Schocher --mtd->usecount;
865ff94bc40SHeiko Schocher BUG_ON(mtd->usecount < 0);
866ff94bc40SHeiko Schocher
867ff94bc40SHeiko Schocher if (mtd->_put_device)
868ff94bc40SHeiko Schocher mtd->_put_device(mtd);
869ff94bc40SHeiko Schocher
870ff94bc40SHeiko Schocher module_put(mtd->owner);
871ff94bc40SHeiko Schocher }
872ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(__put_mtd_device);
873ff94bc40SHeiko Schocher
874dfe64e2cSSergey Lapin /*
875dfe64e2cSSergey Lapin * Erase is an asynchronous operation. Device drivers are supposed
876dfe64e2cSSergey Lapin * to call instr->callback() whenever the operation completes, even
877dfe64e2cSSergey Lapin * if it completes with a failure.
878dfe64e2cSSergey Lapin * Callers are supposed to pass a callback function and wait for it
879dfe64e2cSSergey Lapin * to be called before writing to the block.
880dfe64e2cSSergey Lapin */
mtd_erase(struct mtd_info * mtd,struct erase_info * instr)881dfe64e2cSSergey Lapin int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
882dfe64e2cSSergey Lapin {
883dfe64e2cSSergey Lapin if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
884dfe64e2cSSergey Lapin return -EINVAL;
885dfe64e2cSSergey Lapin if (!(mtd->flags & MTD_WRITEABLE))
886dfe64e2cSSergey Lapin return -EROFS;
887dfe64e2cSSergey Lapin instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
888dfe64e2cSSergey Lapin if (!instr->len) {
889dfe64e2cSSergey Lapin instr->state = MTD_ERASE_DONE;
890dfe64e2cSSergey Lapin mtd_erase_callback(instr);
891dfe64e2cSSergey Lapin return 0;
892dfe64e2cSSergey Lapin }
893dfe64e2cSSergey Lapin return mtd->_erase(mtd, instr);
894dfe64e2cSSergey Lapin }
895ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_erase);
896ff94bc40SHeiko Schocher
897ff94bc40SHeiko Schocher #ifndef __UBOOT__
898ff94bc40SHeiko Schocher /*
899ff94bc40SHeiko Schocher * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
900ff94bc40SHeiko Schocher */
mtd_point(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,void ** virt,resource_size_t * phys)901ff94bc40SHeiko Schocher int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
902ff94bc40SHeiko Schocher void **virt, resource_size_t *phys)
903ff94bc40SHeiko Schocher {
904ff94bc40SHeiko Schocher *retlen = 0;
905ff94bc40SHeiko Schocher *virt = NULL;
906ff94bc40SHeiko Schocher if (phys)
907ff94bc40SHeiko Schocher *phys = 0;
908ff94bc40SHeiko Schocher if (!mtd->_point)
909ff94bc40SHeiko Schocher return -EOPNOTSUPP;
910ff94bc40SHeiko Schocher if (from < 0 || from > mtd->size || len > mtd->size - from)
911ff94bc40SHeiko Schocher return -EINVAL;
912ff94bc40SHeiko Schocher if (!len)
913ff94bc40SHeiko Schocher return 0;
914ff94bc40SHeiko Schocher return mtd->_point(mtd, from, len, retlen, virt, phys);
915ff94bc40SHeiko Schocher }
916ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_point);
917ff94bc40SHeiko Schocher
918ff94bc40SHeiko Schocher /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
mtd_unpoint(struct mtd_info * mtd,loff_t from,size_t len)919ff94bc40SHeiko Schocher int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
920ff94bc40SHeiko Schocher {
921ff94bc40SHeiko Schocher if (!mtd->_point)
922ff94bc40SHeiko Schocher return -EOPNOTSUPP;
923ff94bc40SHeiko Schocher if (from < 0 || from > mtd->size || len > mtd->size - from)
924ff94bc40SHeiko Schocher return -EINVAL;
925ff94bc40SHeiko Schocher if (!len)
926ff94bc40SHeiko Schocher return 0;
927ff94bc40SHeiko Schocher return mtd->_unpoint(mtd, from, len);
928ff94bc40SHeiko Schocher }
929ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_unpoint);
930ff94bc40SHeiko Schocher #endif
931ff94bc40SHeiko Schocher
932ff94bc40SHeiko Schocher /*
933ff94bc40SHeiko Schocher * Allow NOMMU mmap() to directly map the device (if not NULL)
934ff94bc40SHeiko Schocher * - return the address to which the offset maps
935ff94bc40SHeiko Schocher * - return -ENOSYS to indicate refusal to do the mapping
936ff94bc40SHeiko Schocher */
mtd_get_unmapped_area(struct mtd_info * mtd,unsigned long len,unsigned long offset,unsigned long flags)937ff94bc40SHeiko Schocher unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
938ff94bc40SHeiko Schocher unsigned long offset, unsigned long flags)
939ff94bc40SHeiko Schocher {
940ff94bc40SHeiko Schocher if (!mtd->_get_unmapped_area)
941ff94bc40SHeiko Schocher return -EOPNOTSUPP;
942ff94bc40SHeiko Schocher if (offset > mtd->size || len > mtd->size - offset)
943ff94bc40SHeiko Schocher return -EINVAL;
944ff94bc40SHeiko Schocher return mtd->_get_unmapped_area(mtd, len, offset, flags);
945ff94bc40SHeiko Schocher }
946ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
947dfe64e2cSSergey Lapin
mtd_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)948dfe64e2cSSergey Lapin int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
949dfe64e2cSSergey Lapin u_char *buf)
950dfe64e2cSSergey Lapin {
95140462e54SPaul Burton int ret_code;
952ff94bc40SHeiko Schocher *retlen = 0;
953dfe64e2cSSergey Lapin if (from < 0 || from > mtd->size || len > mtd->size - from)
954dfe64e2cSSergey Lapin return -EINVAL;
955dfe64e2cSSergey Lapin if (!len)
956dfe64e2cSSergey Lapin return 0;
95740462e54SPaul Burton
95840462e54SPaul Burton /*
95940462e54SPaul Burton * In the absence of an error, drivers return a non-negative integer
96040462e54SPaul Burton * representing the maximum number of bitflips that were corrected on
96140462e54SPaul Burton * any one ecc region (if applicable; zero otherwise).
96240462e54SPaul Burton */
963596cf083SBoris Brezillon if (mtd->_read) {
96440462e54SPaul Burton ret_code = mtd->_read(mtd, from, len, retlen, buf);
965596cf083SBoris Brezillon } else if (mtd->_read_oob) {
966596cf083SBoris Brezillon struct mtd_oob_ops ops = {
967596cf083SBoris Brezillon .len = len,
968596cf083SBoris Brezillon .datbuf = buf,
969596cf083SBoris Brezillon };
970596cf083SBoris Brezillon
971596cf083SBoris Brezillon ret_code = mtd->_read_oob(mtd, from, &ops);
972596cf083SBoris Brezillon *retlen = ops.retlen;
973596cf083SBoris Brezillon } else {
974596cf083SBoris Brezillon return -ENOTSUPP;
975596cf083SBoris Brezillon }
976596cf083SBoris Brezillon
97740462e54SPaul Burton if (unlikely(ret_code < 0))
97840462e54SPaul Burton return ret_code;
97940462e54SPaul Burton if (mtd->ecc_strength == 0)
98040462e54SPaul Burton return 0; /* device lacks ecc */
98140462e54SPaul Burton return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
982dfe64e2cSSergey Lapin }
983ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_read);
984dfe64e2cSSergey Lapin
mtd_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)985dfe64e2cSSergey Lapin int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
986dfe64e2cSSergey Lapin const u_char *buf)
987dfe64e2cSSergey Lapin {
988dfe64e2cSSergey Lapin *retlen = 0;
989dfe64e2cSSergey Lapin if (to < 0 || to > mtd->size || len > mtd->size - to)
990dfe64e2cSSergey Lapin return -EINVAL;
991596cf083SBoris Brezillon if ((!mtd->_write && !mtd->_write_oob) ||
992596cf083SBoris Brezillon !(mtd->flags & MTD_WRITEABLE))
993dfe64e2cSSergey Lapin return -EROFS;
994dfe64e2cSSergey Lapin if (!len)
995dfe64e2cSSergey Lapin return 0;
996596cf083SBoris Brezillon
997596cf083SBoris Brezillon if (!mtd->_write) {
998596cf083SBoris Brezillon struct mtd_oob_ops ops = {
999596cf083SBoris Brezillon .len = len,
1000596cf083SBoris Brezillon .datbuf = (u8 *)buf,
1001596cf083SBoris Brezillon };
1002596cf083SBoris Brezillon int ret;
1003596cf083SBoris Brezillon
1004596cf083SBoris Brezillon ret = mtd->_write_oob(mtd, to, &ops);
1005596cf083SBoris Brezillon *retlen = ops.retlen;
1006596cf083SBoris Brezillon return ret;
1007596cf083SBoris Brezillon }
1008596cf083SBoris Brezillon
1009dfe64e2cSSergey Lapin return mtd->_write(mtd, to, len, retlen, buf);
1010dfe64e2cSSergey Lapin }
1011ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_write);
1012dfe64e2cSSergey Lapin
1013dfe64e2cSSergey Lapin /*
1014dfe64e2cSSergey Lapin * In blackbox flight recorder like scenarios we want to make successful writes
1015dfe64e2cSSergey Lapin * in interrupt context. panic_write() is only intended to be called when its
1016dfe64e2cSSergey Lapin * known the kernel is about to panic and we need the write to succeed. Since
1017dfe64e2cSSergey Lapin * the kernel is not going to be running for much longer, this function can
1018dfe64e2cSSergey Lapin * break locks and delay to ensure the write succeeds (but not sleep).
1019dfe64e2cSSergey Lapin */
mtd_panic_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)1020dfe64e2cSSergey Lapin int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1021dfe64e2cSSergey Lapin const u_char *buf)
1022dfe64e2cSSergey Lapin {
1023dfe64e2cSSergey Lapin *retlen = 0;
1024dfe64e2cSSergey Lapin if (!mtd->_panic_write)
1025dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1026dfe64e2cSSergey Lapin if (to < 0 || to > mtd->size || len > mtd->size - to)
1027dfe64e2cSSergey Lapin return -EINVAL;
1028dfe64e2cSSergey Lapin if (!(mtd->flags & MTD_WRITEABLE))
1029dfe64e2cSSergey Lapin return -EROFS;
1030dfe64e2cSSergey Lapin if (!len)
1031dfe64e2cSSergey Lapin return 0;
1032dfe64e2cSSergey Lapin return mtd->_panic_write(mtd, to, len, retlen, buf);
1033dfe64e2cSSergey Lapin }
1034ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_panic_write);
1035dfe64e2cSSergey Lapin
mtd_check_oob_ops(struct mtd_info * mtd,loff_t offs,struct mtd_oob_ops * ops)10368fad769fSBoris Brezillon static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
10378fad769fSBoris Brezillon struct mtd_oob_ops *ops)
10388fad769fSBoris Brezillon {
10398fad769fSBoris Brezillon /*
10408fad769fSBoris Brezillon * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
10418fad769fSBoris Brezillon * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
10428fad769fSBoris Brezillon * this case.
10438fad769fSBoris Brezillon */
10448fad769fSBoris Brezillon if (!ops->datbuf)
10458fad769fSBoris Brezillon ops->len = 0;
10468fad769fSBoris Brezillon
10478fad769fSBoris Brezillon if (!ops->oobbuf)
10488fad769fSBoris Brezillon ops->ooblen = 0;
10498fad769fSBoris Brezillon
10508fad769fSBoris Brezillon if (offs < 0 || offs + ops->len > mtd->size)
10518fad769fSBoris Brezillon return -EINVAL;
10528fad769fSBoris Brezillon
10538fad769fSBoris Brezillon if (ops->ooblen) {
10548fad769fSBoris Brezillon u64 maxooblen;
10558fad769fSBoris Brezillon
10568fad769fSBoris Brezillon if (ops->ooboffs >= mtd_oobavail(mtd, ops))
10578fad769fSBoris Brezillon return -EINVAL;
10588fad769fSBoris Brezillon
10598fad769fSBoris Brezillon maxooblen = ((mtd_div_by_ws(mtd->size, mtd) -
10608fad769fSBoris Brezillon mtd_div_by_ws(offs, mtd)) *
10618fad769fSBoris Brezillon mtd_oobavail(mtd, ops)) - ops->ooboffs;
10628fad769fSBoris Brezillon if (ops->ooblen > maxooblen)
10638fad769fSBoris Brezillon return -EINVAL;
10648fad769fSBoris Brezillon }
10658fad769fSBoris Brezillon
10668fad769fSBoris Brezillon return 0;
10678fad769fSBoris Brezillon }
10688fad769fSBoris Brezillon
mtd_read_oob(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)1069dfe64e2cSSergey Lapin int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1070dfe64e2cSSergey Lapin {
1071ff94bc40SHeiko Schocher int ret_code;
1072dfe64e2cSSergey Lapin ops->retlen = ops->oobretlen = 0;
10738fad769fSBoris Brezillon
10748fad769fSBoris Brezillon ret_code = mtd_check_oob_ops(mtd, from, ops);
10758fad769fSBoris Brezillon if (ret_code)
10768fad769fSBoris Brezillon return ret_code;
10778fad769fSBoris Brezillon
1078ca040d85SMiquel Raynal /* Check the validity of a potential fallback on mtd->_read */
1079ca040d85SMiquel Raynal if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1080ca040d85SMiquel Raynal return -EOPNOTSUPP;
1081ca040d85SMiquel Raynal
1082ca040d85SMiquel Raynal if (mtd->_read_oob)
1083ca040d85SMiquel Raynal ret_code = mtd->_read_oob(mtd, from, ops);
1084ca040d85SMiquel Raynal else
1085ca040d85SMiquel Raynal ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1086ca040d85SMiquel Raynal ops->datbuf);
1087ca040d85SMiquel Raynal
1088ff94bc40SHeiko Schocher /*
1089ff94bc40SHeiko Schocher * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1090ff94bc40SHeiko Schocher * similar to mtd->_read(), returning a non-negative integer
1091ff94bc40SHeiko Schocher * representing max bitflips. In other cases, mtd->_read_oob() may
1092ff94bc40SHeiko Schocher * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1093ff94bc40SHeiko Schocher */
1094ff94bc40SHeiko Schocher if (unlikely(ret_code < 0))
1095ff94bc40SHeiko Schocher return ret_code;
1096ff94bc40SHeiko Schocher if (mtd->ecc_strength == 0)
1097ff94bc40SHeiko Schocher return 0; /* device lacks ecc */
1098ff94bc40SHeiko Schocher return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1099dfe64e2cSSergey Lapin }
1100ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_read_oob);
1101dfe64e2cSSergey Lapin
mtd_write_oob(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)11025f50d82dSEzequiel Garcia int mtd_write_oob(struct mtd_info *mtd, loff_t to,
11035f50d82dSEzequiel Garcia struct mtd_oob_ops *ops)
11045f50d82dSEzequiel Garcia {
11058fad769fSBoris Brezillon int ret;
11068fad769fSBoris Brezillon
11075f50d82dSEzequiel Garcia ops->retlen = ops->oobretlen = 0;
1108ca040d85SMiquel Raynal
11095f50d82dSEzequiel Garcia if (!(mtd->flags & MTD_WRITEABLE))
11105f50d82dSEzequiel Garcia return -EROFS;
11118fad769fSBoris Brezillon
11128fad769fSBoris Brezillon ret = mtd_check_oob_ops(mtd, to, ops);
11138fad769fSBoris Brezillon if (ret)
11148fad769fSBoris Brezillon return ret;
11158fad769fSBoris Brezillon
1116ca040d85SMiquel Raynal /* Check the validity of a potential fallback on mtd->_write */
1117ca040d85SMiquel Raynal if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1118ca040d85SMiquel Raynal return -EOPNOTSUPP;
1119ca040d85SMiquel Raynal
1120ca040d85SMiquel Raynal if (mtd->_write_oob)
11215f50d82dSEzequiel Garcia return mtd->_write_oob(mtd, to, ops);
1122ca040d85SMiquel Raynal else
1123ca040d85SMiquel Raynal return mtd->_write(mtd, to, ops->len, &ops->retlen,
1124ca040d85SMiquel Raynal ops->datbuf);
11255f50d82dSEzequiel Garcia }
11265f50d82dSEzequiel Garcia EXPORT_SYMBOL_GPL(mtd_write_oob);
11275f50d82dSEzequiel Garcia
112813f3b04fSBoris Brezillon /**
112913f3b04fSBoris Brezillon * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
113013f3b04fSBoris Brezillon * @mtd: MTD device structure
113113f3b04fSBoris Brezillon * @section: ECC section. Depending on the layout you may have all the ECC
113213f3b04fSBoris Brezillon * bytes stored in a single contiguous section, or one section
113313f3b04fSBoris Brezillon * per ECC chunk (and sometime several sections for a single ECC
113413f3b04fSBoris Brezillon * ECC chunk)
113513f3b04fSBoris Brezillon * @oobecc: OOB region struct filled with the appropriate ECC position
113613f3b04fSBoris Brezillon * information
113713f3b04fSBoris Brezillon *
113813f3b04fSBoris Brezillon * This function returns ECC section information in the OOB area. If you want
113913f3b04fSBoris Brezillon * to get all the ECC bytes information, then you should call
114013f3b04fSBoris Brezillon * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
114113f3b04fSBoris Brezillon *
114213f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
114313f3b04fSBoris Brezillon */
mtd_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobecc)114413f3b04fSBoris Brezillon int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
114513f3b04fSBoris Brezillon struct mtd_oob_region *oobecc)
114613f3b04fSBoris Brezillon {
114713f3b04fSBoris Brezillon memset(oobecc, 0, sizeof(*oobecc));
114813f3b04fSBoris Brezillon
114913f3b04fSBoris Brezillon if (!mtd || section < 0)
115013f3b04fSBoris Brezillon return -EINVAL;
115113f3b04fSBoris Brezillon
115213f3b04fSBoris Brezillon if (!mtd->ooblayout || !mtd->ooblayout->ecc)
115313f3b04fSBoris Brezillon return -ENOTSUPP;
115413f3b04fSBoris Brezillon
115513f3b04fSBoris Brezillon return mtd->ooblayout->ecc(mtd, section, oobecc);
115613f3b04fSBoris Brezillon }
115713f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
115813f3b04fSBoris Brezillon
115913f3b04fSBoris Brezillon /**
116013f3b04fSBoris Brezillon * mtd_ooblayout_free - Get the OOB region definition of a specific free
116113f3b04fSBoris Brezillon * section
116213f3b04fSBoris Brezillon * @mtd: MTD device structure
116313f3b04fSBoris Brezillon * @section: Free section you are interested in. Depending on the layout
116413f3b04fSBoris Brezillon * you may have all the free bytes stored in a single contiguous
116513f3b04fSBoris Brezillon * section, or one section per ECC chunk plus an extra section
116613f3b04fSBoris Brezillon * for the remaining bytes (or other funky layout).
116713f3b04fSBoris Brezillon * @oobfree: OOB region struct filled with the appropriate free position
116813f3b04fSBoris Brezillon * information
116913f3b04fSBoris Brezillon *
117013f3b04fSBoris Brezillon * This function returns free bytes position in the OOB area. If you want
117113f3b04fSBoris Brezillon * to get all the free bytes information, then you should call
117213f3b04fSBoris Brezillon * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
117313f3b04fSBoris Brezillon *
117413f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
117513f3b04fSBoris Brezillon */
mtd_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobfree)117613f3b04fSBoris Brezillon int mtd_ooblayout_free(struct mtd_info *mtd, int section,
117713f3b04fSBoris Brezillon struct mtd_oob_region *oobfree)
117813f3b04fSBoris Brezillon {
117913f3b04fSBoris Brezillon memset(oobfree, 0, sizeof(*oobfree));
118013f3b04fSBoris Brezillon
118113f3b04fSBoris Brezillon if (!mtd || section < 0)
118213f3b04fSBoris Brezillon return -EINVAL;
118313f3b04fSBoris Brezillon
118413f3b04fSBoris Brezillon if (!mtd->ooblayout || !mtd->ooblayout->free)
118513f3b04fSBoris Brezillon return -ENOTSUPP;
118613f3b04fSBoris Brezillon
118713f3b04fSBoris Brezillon return mtd->ooblayout->free(mtd, section, oobfree);
118813f3b04fSBoris Brezillon }
118913f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
119013f3b04fSBoris Brezillon
119113f3b04fSBoris Brezillon /**
119213f3b04fSBoris Brezillon * mtd_ooblayout_find_region - Find the region attached to a specific byte
119313f3b04fSBoris Brezillon * @mtd: mtd info structure
119413f3b04fSBoris Brezillon * @byte: the byte we are searching for
119513f3b04fSBoris Brezillon * @sectionp: pointer where the section id will be stored
119613f3b04fSBoris Brezillon * @oobregion: used to retrieve the ECC position
119713f3b04fSBoris Brezillon * @iter: iterator function. Should be either mtd_ooblayout_free or
119813f3b04fSBoris Brezillon * mtd_ooblayout_ecc depending on the region type you're searching for
119913f3b04fSBoris Brezillon *
120013f3b04fSBoris Brezillon * This function returns the section id and oobregion information of a
120113f3b04fSBoris Brezillon * specific byte. For example, say you want to know where the 4th ECC byte is
120213f3b04fSBoris Brezillon * stored, you'll use:
120313f3b04fSBoris Brezillon *
120413f3b04fSBoris Brezillon * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
120513f3b04fSBoris Brezillon *
120613f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
120713f3b04fSBoris Brezillon */
mtd_ooblayout_find_region(struct mtd_info * mtd,int byte,int * sectionp,struct mtd_oob_region * oobregion,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * oobregion))120813f3b04fSBoris Brezillon static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
120913f3b04fSBoris Brezillon int *sectionp, struct mtd_oob_region *oobregion,
121013f3b04fSBoris Brezillon int (*iter)(struct mtd_info *,
121113f3b04fSBoris Brezillon int section,
121213f3b04fSBoris Brezillon struct mtd_oob_region *oobregion))
121313f3b04fSBoris Brezillon {
121413f3b04fSBoris Brezillon int pos = 0, ret, section = 0;
121513f3b04fSBoris Brezillon
121613f3b04fSBoris Brezillon memset(oobregion, 0, sizeof(*oobregion));
121713f3b04fSBoris Brezillon
121813f3b04fSBoris Brezillon while (1) {
121913f3b04fSBoris Brezillon ret = iter(mtd, section, oobregion);
122013f3b04fSBoris Brezillon if (ret)
122113f3b04fSBoris Brezillon return ret;
122213f3b04fSBoris Brezillon
122313f3b04fSBoris Brezillon if (pos + oobregion->length > byte)
122413f3b04fSBoris Brezillon break;
122513f3b04fSBoris Brezillon
122613f3b04fSBoris Brezillon pos += oobregion->length;
122713f3b04fSBoris Brezillon section++;
122813f3b04fSBoris Brezillon }
122913f3b04fSBoris Brezillon
123013f3b04fSBoris Brezillon /*
123113f3b04fSBoris Brezillon * Adjust region info to make it start at the beginning at the
123213f3b04fSBoris Brezillon * 'start' ECC byte.
123313f3b04fSBoris Brezillon */
123413f3b04fSBoris Brezillon oobregion->offset += byte - pos;
123513f3b04fSBoris Brezillon oobregion->length -= byte - pos;
123613f3b04fSBoris Brezillon *sectionp = section;
123713f3b04fSBoris Brezillon
123813f3b04fSBoris Brezillon return 0;
123913f3b04fSBoris Brezillon }
124013f3b04fSBoris Brezillon
124113f3b04fSBoris Brezillon /**
124213f3b04fSBoris Brezillon * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
124313f3b04fSBoris Brezillon * ECC byte
124413f3b04fSBoris Brezillon * @mtd: mtd info structure
124513f3b04fSBoris Brezillon * @eccbyte: the byte we are searching for
124613f3b04fSBoris Brezillon * @sectionp: pointer where the section id will be stored
124713f3b04fSBoris Brezillon * @oobregion: OOB region information
124813f3b04fSBoris Brezillon *
124913f3b04fSBoris Brezillon * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
125013f3b04fSBoris Brezillon * byte.
125113f3b04fSBoris Brezillon *
125213f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
125313f3b04fSBoris Brezillon */
mtd_ooblayout_find_eccregion(struct mtd_info * mtd,int eccbyte,int * section,struct mtd_oob_region * oobregion)125413f3b04fSBoris Brezillon int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
125513f3b04fSBoris Brezillon int *section,
125613f3b04fSBoris Brezillon struct mtd_oob_region *oobregion)
125713f3b04fSBoris Brezillon {
125813f3b04fSBoris Brezillon return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
125913f3b04fSBoris Brezillon mtd_ooblayout_ecc);
126013f3b04fSBoris Brezillon }
126113f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
126213f3b04fSBoris Brezillon
126313f3b04fSBoris Brezillon /**
126413f3b04fSBoris Brezillon * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
126513f3b04fSBoris Brezillon * @mtd: mtd info structure
126613f3b04fSBoris Brezillon * @buf: destination buffer to store OOB bytes
126713f3b04fSBoris Brezillon * @oobbuf: OOB buffer
126813f3b04fSBoris Brezillon * @start: first byte to retrieve
126913f3b04fSBoris Brezillon * @nbytes: number of bytes to retrieve
127013f3b04fSBoris Brezillon * @iter: section iterator
127113f3b04fSBoris Brezillon *
127213f3b04fSBoris Brezillon * Extract bytes attached to a specific category (ECC or free)
127313f3b04fSBoris Brezillon * from the OOB buffer and copy them into buf.
127413f3b04fSBoris Brezillon *
127513f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
127613f3b04fSBoris Brezillon */
mtd_ooblayout_get_bytes(struct mtd_info * mtd,u8 * buf,const u8 * oobbuf,int start,int nbytes,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * oobregion))127713f3b04fSBoris Brezillon static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
127813f3b04fSBoris Brezillon const u8 *oobbuf, int start, int nbytes,
127913f3b04fSBoris Brezillon int (*iter)(struct mtd_info *,
128013f3b04fSBoris Brezillon int section,
128113f3b04fSBoris Brezillon struct mtd_oob_region *oobregion))
128213f3b04fSBoris Brezillon {
128313f3b04fSBoris Brezillon struct mtd_oob_region oobregion;
128413f3b04fSBoris Brezillon int section, ret;
128513f3b04fSBoris Brezillon
128613f3b04fSBoris Brezillon ret = mtd_ooblayout_find_region(mtd, start, §ion,
128713f3b04fSBoris Brezillon &oobregion, iter);
128813f3b04fSBoris Brezillon
128913f3b04fSBoris Brezillon while (!ret) {
129013f3b04fSBoris Brezillon int cnt;
129113f3b04fSBoris Brezillon
129213f3b04fSBoris Brezillon cnt = min_t(int, nbytes, oobregion.length);
129313f3b04fSBoris Brezillon memcpy(buf, oobbuf + oobregion.offset, cnt);
129413f3b04fSBoris Brezillon buf += cnt;
129513f3b04fSBoris Brezillon nbytes -= cnt;
129613f3b04fSBoris Brezillon
129713f3b04fSBoris Brezillon if (!nbytes)
129813f3b04fSBoris Brezillon break;
129913f3b04fSBoris Brezillon
130013f3b04fSBoris Brezillon ret = iter(mtd, ++section, &oobregion);
130113f3b04fSBoris Brezillon }
130213f3b04fSBoris Brezillon
130313f3b04fSBoris Brezillon return ret;
130413f3b04fSBoris Brezillon }
130513f3b04fSBoris Brezillon
130613f3b04fSBoris Brezillon /**
130713f3b04fSBoris Brezillon * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
130813f3b04fSBoris Brezillon * @mtd: mtd info structure
130913f3b04fSBoris Brezillon * @buf: source buffer to get OOB bytes from
131013f3b04fSBoris Brezillon * @oobbuf: OOB buffer
131113f3b04fSBoris Brezillon * @start: first OOB byte to set
131213f3b04fSBoris Brezillon * @nbytes: number of OOB bytes to set
131313f3b04fSBoris Brezillon * @iter: section iterator
131413f3b04fSBoris Brezillon *
131513f3b04fSBoris Brezillon * Fill the OOB buffer with data provided in buf. The category (ECC or free)
131613f3b04fSBoris Brezillon * is selected by passing the appropriate iterator.
131713f3b04fSBoris Brezillon *
131813f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
131913f3b04fSBoris Brezillon */
mtd_ooblayout_set_bytes(struct mtd_info * mtd,const u8 * buf,u8 * oobbuf,int start,int nbytes,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * oobregion))132013f3b04fSBoris Brezillon static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
132113f3b04fSBoris Brezillon u8 *oobbuf, int start, int nbytes,
132213f3b04fSBoris Brezillon int (*iter)(struct mtd_info *,
132313f3b04fSBoris Brezillon int section,
132413f3b04fSBoris Brezillon struct mtd_oob_region *oobregion))
132513f3b04fSBoris Brezillon {
132613f3b04fSBoris Brezillon struct mtd_oob_region oobregion;
132713f3b04fSBoris Brezillon int section, ret;
132813f3b04fSBoris Brezillon
132913f3b04fSBoris Brezillon ret = mtd_ooblayout_find_region(mtd, start, §ion,
133013f3b04fSBoris Brezillon &oobregion, iter);
133113f3b04fSBoris Brezillon
133213f3b04fSBoris Brezillon while (!ret) {
133313f3b04fSBoris Brezillon int cnt;
133413f3b04fSBoris Brezillon
133513f3b04fSBoris Brezillon cnt = min_t(int, nbytes, oobregion.length);
133613f3b04fSBoris Brezillon memcpy(oobbuf + oobregion.offset, buf, cnt);
133713f3b04fSBoris Brezillon buf += cnt;
133813f3b04fSBoris Brezillon nbytes -= cnt;
133913f3b04fSBoris Brezillon
134013f3b04fSBoris Brezillon if (!nbytes)
134113f3b04fSBoris Brezillon break;
134213f3b04fSBoris Brezillon
134313f3b04fSBoris Brezillon ret = iter(mtd, ++section, &oobregion);
134413f3b04fSBoris Brezillon }
134513f3b04fSBoris Brezillon
134613f3b04fSBoris Brezillon return ret;
134713f3b04fSBoris Brezillon }
134813f3b04fSBoris Brezillon
134913f3b04fSBoris Brezillon /**
135013f3b04fSBoris Brezillon * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
135113f3b04fSBoris Brezillon * @mtd: mtd info structure
135213f3b04fSBoris Brezillon * @iter: category iterator
135313f3b04fSBoris Brezillon *
135413f3b04fSBoris Brezillon * Count the number of bytes in a given category.
135513f3b04fSBoris Brezillon *
135613f3b04fSBoris Brezillon * Returns a positive value on success, a negative error code otherwise.
135713f3b04fSBoris Brezillon */
mtd_ooblayout_count_bytes(struct mtd_info * mtd,int (* iter)(struct mtd_info *,int section,struct mtd_oob_region * oobregion))135813f3b04fSBoris Brezillon static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
135913f3b04fSBoris Brezillon int (*iter)(struct mtd_info *,
136013f3b04fSBoris Brezillon int section,
136113f3b04fSBoris Brezillon struct mtd_oob_region *oobregion))
136213f3b04fSBoris Brezillon {
136313f3b04fSBoris Brezillon struct mtd_oob_region oobregion;
136413f3b04fSBoris Brezillon int section = 0, ret, nbytes = 0;
136513f3b04fSBoris Brezillon
136613f3b04fSBoris Brezillon while (1) {
136713f3b04fSBoris Brezillon ret = iter(mtd, section++, &oobregion);
136813f3b04fSBoris Brezillon if (ret) {
136913f3b04fSBoris Brezillon if (ret == -ERANGE)
137013f3b04fSBoris Brezillon ret = nbytes;
137113f3b04fSBoris Brezillon break;
137213f3b04fSBoris Brezillon }
137313f3b04fSBoris Brezillon
137413f3b04fSBoris Brezillon nbytes += oobregion.length;
137513f3b04fSBoris Brezillon }
137613f3b04fSBoris Brezillon
137713f3b04fSBoris Brezillon return ret;
137813f3b04fSBoris Brezillon }
137913f3b04fSBoris Brezillon
138013f3b04fSBoris Brezillon /**
138113f3b04fSBoris Brezillon * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
138213f3b04fSBoris Brezillon * @mtd: mtd info structure
138313f3b04fSBoris Brezillon * @eccbuf: destination buffer to store ECC bytes
138413f3b04fSBoris Brezillon * @oobbuf: OOB buffer
138513f3b04fSBoris Brezillon * @start: first ECC byte to retrieve
138613f3b04fSBoris Brezillon * @nbytes: number of ECC bytes to retrieve
138713f3b04fSBoris Brezillon *
138813f3b04fSBoris Brezillon * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
138913f3b04fSBoris Brezillon *
139013f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
139113f3b04fSBoris Brezillon */
mtd_ooblayout_get_eccbytes(struct mtd_info * mtd,u8 * eccbuf,const u8 * oobbuf,int start,int nbytes)139213f3b04fSBoris Brezillon int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
139313f3b04fSBoris Brezillon const u8 *oobbuf, int start, int nbytes)
139413f3b04fSBoris Brezillon {
139513f3b04fSBoris Brezillon return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
139613f3b04fSBoris Brezillon mtd_ooblayout_ecc);
139713f3b04fSBoris Brezillon }
139813f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
139913f3b04fSBoris Brezillon
140013f3b04fSBoris Brezillon /**
140113f3b04fSBoris Brezillon * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
140213f3b04fSBoris Brezillon * @mtd: mtd info structure
140313f3b04fSBoris Brezillon * @eccbuf: source buffer to get ECC bytes from
140413f3b04fSBoris Brezillon * @oobbuf: OOB buffer
140513f3b04fSBoris Brezillon * @start: first ECC byte to set
140613f3b04fSBoris Brezillon * @nbytes: number of ECC bytes to set
140713f3b04fSBoris Brezillon *
140813f3b04fSBoris Brezillon * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
140913f3b04fSBoris Brezillon *
141013f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
141113f3b04fSBoris Brezillon */
mtd_ooblayout_set_eccbytes(struct mtd_info * mtd,const u8 * eccbuf,u8 * oobbuf,int start,int nbytes)141213f3b04fSBoris Brezillon int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
141313f3b04fSBoris Brezillon u8 *oobbuf, int start, int nbytes)
141413f3b04fSBoris Brezillon {
141513f3b04fSBoris Brezillon return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
141613f3b04fSBoris Brezillon mtd_ooblayout_ecc);
141713f3b04fSBoris Brezillon }
141813f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
141913f3b04fSBoris Brezillon
142013f3b04fSBoris Brezillon /**
142113f3b04fSBoris Brezillon * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
142213f3b04fSBoris Brezillon * @mtd: mtd info structure
142313f3b04fSBoris Brezillon * @databuf: destination buffer to store ECC bytes
142413f3b04fSBoris Brezillon * @oobbuf: OOB buffer
142513f3b04fSBoris Brezillon * @start: first ECC byte to retrieve
142613f3b04fSBoris Brezillon * @nbytes: number of ECC bytes to retrieve
142713f3b04fSBoris Brezillon *
142813f3b04fSBoris Brezillon * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
142913f3b04fSBoris Brezillon *
143013f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
143113f3b04fSBoris Brezillon */
mtd_ooblayout_get_databytes(struct mtd_info * mtd,u8 * databuf,const u8 * oobbuf,int start,int nbytes)143213f3b04fSBoris Brezillon int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
143313f3b04fSBoris Brezillon const u8 *oobbuf, int start, int nbytes)
143413f3b04fSBoris Brezillon {
143513f3b04fSBoris Brezillon return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
143613f3b04fSBoris Brezillon mtd_ooblayout_free);
143713f3b04fSBoris Brezillon }
143813f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
143913f3b04fSBoris Brezillon
144013f3b04fSBoris Brezillon /**
144113f3b04fSBoris Brezillon * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
144213f3b04fSBoris Brezillon * @mtd: mtd info structure
144313f3b04fSBoris Brezillon * @eccbuf: source buffer to get data bytes from
144413f3b04fSBoris Brezillon * @oobbuf: OOB buffer
144513f3b04fSBoris Brezillon * @start: first ECC byte to set
144613f3b04fSBoris Brezillon * @nbytes: number of ECC bytes to set
144713f3b04fSBoris Brezillon *
144813f3b04fSBoris Brezillon * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
144913f3b04fSBoris Brezillon *
145013f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
145113f3b04fSBoris Brezillon */
mtd_ooblayout_set_databytes(struct mtd_info * mtd,const u8 * databuf,u8 * oobbuf,int start,int nbytes)145213f3b04fSBoris Brezillon int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
145313f3b04fSBoris Brezillon u8 *oobbuf, int start, int nbytes)
145413f3b04fSBoris Brezillon {
145513f3b04fSBoris Brezillon return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
145613f3b04fSBoris Brezillon mtd_ooblayout_free);
145713f3b04fSBoris Brezillon }
145813f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
145913f3b04fSBoris Brezillon
146013f3b04fSBoris Brezillon /**
146113f3b04fSBoris Brezillon * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
146213f3b04fSBoris Brezillon * @mtd: mtd info structure
146313f3b04fSBoris Brezillon *
146413f3b04fSBoris Brezillon * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
146513f3b04fSBoris Brezillon *
146613f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
146713f3b04fSBoris Brezillon */
mtd_ooblayout_count_freebytes(struct mtd_info * mtd)146813f3b04fSBoris Brezillon int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
146913f3b04fSBoris Brezillon {
147013f3b04fSBoris Brezillon return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
147113f3b04fSBoris Brezillon }
147213f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
147313f3b04fSBoris Brezillon
147413f3b04fSBoris Brezillon /**
147513f3b04fSBoris Brezillon * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
147613f3b04fSBoris Brezillon * @mtd: mtd info structure
147713f3b04fSBoris Brezillon *
147813f3b04fSBoris Brezillon * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
147913f3b04fSBoris Brezillon *
148013f3b04fSBoris Brezillon * Returns zero on success, a negative error code otherwise.
148113f3b04fSBoris Brezillon */
mtd_ooblayout_count_eccbytes(struct mtd_info * mtd)148213f3b04fSBoris Brezillon int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
148313f3b04fSBoris Brezillon {
148413f3b04fSBoris Brezillon return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
148513f3b04fSBoris Brezillon }
148613f3b04fSBoris Brezillon EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
148713f3b04fSBoris Brezillon
1488dfe64e2cSSergey Lapin /*
1489dfe64e2cSSergey Lapin * Method to access the protection register area, present in some flash
1490dfe64e2cSSergey Lapin * devices. The user data is one time programmable but the factory data is read
1491dfe64e2cSSergey Lapin * only.
1492dfe64e2cSSergey Lapin */
mtd_get_fact_prot_info(struct mtd_info * mtd,size_t len,size_t * retlen,struct otp_info * buf)14934e67c571SHeiko Schocher int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
14944e67c571SHeiko Schocher struct otp_info *buf)
1495dfe64e2cSSergey Lapin {
1496dfe64e2cSSergey Lapin if (!mtd->_get_fact_prot_info)
1497dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1498dfe64e2cSSergey Lapin if (!len)
1499dfe64e2cSSergey Lapin return 0;
15004e67c571SHeiko Schocher return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1501dfe64e2cSSergey Lapin }
1502ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1503dfe64e2cSSergey Lapin
mtd_read_fact_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)1504dfe64e2cSSergey Lapin int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1505dfe64e2cSSergey Lapin size_t *retlen, u_char *buf)
1506dfe64e2cSSergey Lapin {
1507dfe64e2cSSergey Lapin *retlen = 0;
1508dfe64e2cSSergey Lapin if (!mtd->_read_fact_prot_reg)
1509dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1510dfe64e2cSSergey Lapin if (!len)
1511dfe64e2cSSergey Lapin return 0;
1512dfe64e2cSSergey Lapin return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1513dfe64e2cSSergey Lapin }
1514ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1515dfe64e2cSSergey Lapin
mtd_get_user_prot_info(struct mtd_info * mtd,size_t len,size_t * retlen,struct otp_info * buf)15164e67c571SHeiko Schocher int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
15174e67c571SHeiko Schocher struct otp_info *buf)
1518dfe64e2cSSergey Lapin {
1519dfe64e2cSSergey Lapin if (!mtd->_get_user_prot_info)
1520dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1521dfe64e2cSSergey Lapin if (!len)
1522dfe64e2cSSergey Lapin return 0;
15234e67c571SHeiko Schocher return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1524dfe64e2cSSergey Lapin }
1525ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1526dfe64e2cSSergey Lapin
mtd_read_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)1527dfe64e2cSSergey Lapin int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1528dfe64e2cSSergey Lapin size_t *retlen, u_char *buf)
1529dfe64e2cSSergey Lapin {
1530dfe64e2cSSergey Lapin *retlen = 0;
1531dfe64e2cSSergey Lapin if (!mtd->_read_user_prot_reg)
1532dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1533dfe64e2cSSergey Lapin if (!len)
1534dfe64e2cSSergey Lapin return 0;
1535dfe64e2cSSergey Lapin return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1536dfe64e2cSSergey Lapin }
1537ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1538dfe64e2cSSergey Lapin
mtd_write_user_prot_reg(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,u_char * buf)1539dfe64e2cSSergey Lapin int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1540dfe64e2cSSergey Lapin size_t *retlen, u_char *buf)
1541dfe64e2cSSergey Lapin {
15424e67c571SHeiko Schocher int ret;
15434e67c571SHeiko Schocher
1544dfe64e2cSSergey Lapin *retlen = 0;
1545dfe64e2cSSergey Lapin if (!mtd->_write_user_prot_reg)
1546dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1547dfe64e2cSSergey Lapin if (!len)
1548dfe64e2cSSergey Lapin return 0;
15494e67c571SHeiko Schocher ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
15504e67c571SHeiko Schocher if (ret)
15514e67c571SHeiko Schocher return ret;
15524e67c571SHeiko Schocher
15534e67c571SHeiko Schocher /*
15544e67c571SHeiko Schocher * If no data could be written at all, we are out of memory and
15554e67c571SHeiko Schocher * must return -ENOSPC.
15564e67c571SHeiko Schocher */
15574e67c571SHeiko Schocher return (*retlen) ? 0 : -ENOSPC;
1558dfe64e2cSSergey Lapin }
1559ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1560dfe64e2cSSergey Lapin
mtd_lock_user_prot_reg(struct mtd_info * mtd,loff_t from,size_t len)1561dfe64e2cSSergey Lapin int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1562dfe64e2cSSergey Lapin {
1563dfe64e2cSSergey Lapin if (!mtd->_lock_user_prot_reg)
1564dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1565dfe64e2cSSergey Lapin if (!len)
1566dfe64e2cSSergey Lapin return 0;
1567dfe64e2cSSergey Lapin return mtd->_lock_user_prot_reg(mtd, from, len);
1568dfe64e2cSSergey Lapin }
1569ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1570dfe64e2cSSergey Lapin
1571dfe64e2cSSergey Lapin /* Chip-supported device locking */
mtd_lock(struct mtd_info * mtd,loff_t ofs,uint64_t len)1572dfe64e2cSSergey Lapin int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1573dfe64e2cSSergey Lapin {
1574dfe64e2cSSergey Lapin if (!mtd->_lock)
1575dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1576dfe64e2cSSergey Lapin if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1577dfe64e2cSSergey Lapin return -EINVAL;
1578dfe64e2cSSergey Lapin if (!len)
1579dfe64e2cSSergey Lapin return 0;
1580dfe64e2cSSergey Lapin return mtd->_lock(mtd, ofs, len);
1581dfe64e2cSSergey Lapin }
1582ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_lock);
1583dfe64e2cSSergey Lapin
mtd_unlock(struct mtd_info * mtd,loff_t ofs,uint64_t len)1584dfe64e2cSSergey Lapin int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1585dfe64e2cSSergey Lapin {
1586dfe64e2cSSergey Lapin if (!mtd->_unlock)
1587dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1588dfe64e2cSSergey Lapin if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1589dfe64e2cSSergey Lapin return -EINVAL;
1590dfe64e2cSSergey Lapin if (!len)
1591dfe64e2cSSergey Lapin return 0;
1592dfe64e2cSSergey Lapin return mtd->_unlock(mtd, ofs, len);
1593dfe64e2cSSergey Lapin }
1594ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_unlock);
1595ff94bc40SHeiko Schocher
mtd_is_locked(struct mtd_info * mtd,loff_t ofs,uint64_t len)1596ff94bc40SHeiko Schocher int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1597ff94bc40SHeiko Schocher {
1598ff94bc40SHeiko Schocher if (!mtd->_is_locked)
1599ff94bc40SHeiko Schocher return -EOPNOTSUPP;
1600ff94bc40SHeiko Schocher if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1601ff94bc40SHeiko Schocher return -EINVAL;
1602ff94bc40SHeiko Schocher if (!len)
1603ff94bc40SHeiko Schocher return 0;
1604ff94bc40SHeiko Schocher return mtd->_is_locked(mtd, ofs, len);
1605ff94bc40SHeiko Schocher }
1606ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_is_locked);
1607dfe64e2cSSergey Lapin
mtd_block_isreserved(struct mtd_info * mtd,loff_t ofs)160886a720aaSEzequiel Garcia int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1609dfe64e2cSSergey Lapin {
1610dfe64e2cSSergey Lapin if (ofs < 0 || ofs > mtd->size)
1611dfe64e2cSSergey Lapin return -EINVAL;
161286a720aaSEzequiel Garcia if (!mtd->_block_isreserved)
161386a720aaSEzequiel Garcia return 0;
161486a720aaSEzequiel Garcia return mtd->_block_isreserved(mtd, ofs);
161586a720aaSEzequiel Garcia }
161686a720aaSEzequiel Garcia EXPORT_SYMBOL_GPL(mtd_block_isreserved);
161786a720aaSEzequiel Garcia
mtd_block_isbad(struct mtd_info * mtd,loff_t ofs)161886a720aaSEzequiel Garcia int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
161986a720aaSEzequiel Garcia {
162086a720aaSEzequiel Garcia if (ofs < 0 || ofs > mtd->size)
162186a720aaSEzequiel Garcia return -EINVAL;
162286a720aaSEzequiel Garcia if (!mtd->_block_isbad)
162386a720aaSEzequiel Garcia return 0;
1624dfe64e2cSSergey Lapin return mtd->_block_isbad(mtd, ofs);
1625dfe64e2cSSergey Lapin }
1626ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_block_isbad);
1627dfe64e2cSSergey Lapin
mtd_block_markbad(struct mtd_info * mtd,loff_t ofs)1628dfe64e2cSSergey Lapin int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1629dfe64e2cSSergey Lapin {
1630dfe64e2cSSergey Lapin if (!mtd->_block_markbad)
1631dfe64e2cSSergey Lapin return -EOPNOTSUPP;
1632dfe64e2cSSergey Lapin if (ofs < 0 || ofs > mtd->size)
1633dfe64e2cSSergey Lapin return -EINVAL;
1634dfe64e2cSSergey Lapin if (!(mtd->flags & MTD_WRITEABLE))
1635dfe64e2cSSergey Lapin return -EROFS;
1636dfe64e2cSSergey Lapin return mtd->_block_markbad(mtd, ofs);
1637dfe64e2cSSergey Lapin }
1638ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_block_markbad);
1639ff94bc40SHeiko Schocher
1640ff94bc40SHeiko Schocher #ifndef __UBOOT__
1641ff94bc40SHeiko Schocher /*
1642ff94bc40SHeiko Schocher * default_mtd_writev - the default writev method
1643ff94bc40SHeiko Schocher * @mtd: mtd device description object pointer
1644ff94bc40SHeiko Schocher * @vecs: the vectors to write
1645ff94bc40SHeiko Schocher * @count: count of vectors in @vecs
1646ff94bc40SHeiko Schocher * @to: the MTD device offset to write to
1647ff94bc40SHeiko Schocher * @retlen: on exit contains the count of bytes written to the MTD device.
1648ff94bc40SHeiko Schocher *
1649ff94bc40SHeiko Schocher * This function returns zero in case of success and a negative error code in
1650ff94bc40SHeiko Schocher * case of failure.
1651ff94bc40SHeiko Schocher */
default_mtd_writev(struct mtd_info * mtd,const struct kvec * vecs,unsigned long count,loff_t to,size_t * retlen)1652ff94bc40SHeiko Schocher static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1653ff94bc40SHeiko Schocher unsigned long count, loff_t to, size_t *retlen)
1654ff94bc40SHeiko Schocher {
1655ff94bc40SHeiko Schocher unsigned long i;
1656ff94bc40SHeiko Schocher size_t totlen = 0, thislen;
1657ff94bc40SHeiko Schocher int ret = 0;
1658ff94bc40SHeiko Schocher
1659ff94bc40SHeiko Schocher for (i = 0; i < count; i++) {
1660ff94bc40SHeiko Schocher if (!vecs[i].iov_len)
1661ff94bc40SHeiko Schocher continue;
1662ff94bc40SHeiko Schocher ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1663ff94bc40SHeiko Schocher vecs[i].iov_base);
1664ff94bc40SHeiko Schocher totlen += thislen;
1665ff94bc40SHeiko Schocher if (ret || thislen != vecs[i].iov_len)
1666ff94bc40SHeiko Schocher break;
1667ff94bc40SHeiko Schocher to += vecs[i].iov_len;
1668ff94bc40SHeiko Schocher }
1669ff94bc40SHeiko Schocher *retlen = totlen;
1670ff94bc40SHeiko Schocher return ret;
1671ff94bc40SHeiko Schocher }
1672ff94bc40SHeiko Schocher
1673ff94bc40SHeiko Schocher /*
1674ff94bc40SHeiko Schocher * mtd_writev - the vector-based MTD write method
1675ff94bc40SHeiko Schocher * @mtd: mtd device description object pointer
1676ff94bc40SHeiko Schocher * @vecs: the vectors to write
1677ff94bc40SHeiko Schocher * @count: count of vectors in @vecs
1678ff94bc40SHeiko Schocher * @to: the MTD device offset to write to
1679ff94bc40SHeiko Schocher * @retlen: on exit contains the count of bytes written to the MTD device.
1680ff94bc40SHeiko Schocher *
1681ff94bc40SHeiko Schocher * This function returns zero in case of success and a negative error code in
1682ff94bc40SHeiko Schocher * case of failure.
1683ff94bc40SHeiko Schocher */
mtd_writev(struct mtd_info * mtd,const struct kvec * vecs,unsigned long count,loff_t to,size_t * retlen)1684ff94bc40SHeiko Schocher int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1685ff94bc40SHeiko Schocher unsigned long count, loff_t to, size_t *retlen)
1686ff94bc40SHeiko Schocher {
1687ff94bc40SHeiko Schocher *retlen = 0;
1688ff94bc40SHeiko Schocher if (!(mtd->flags & MTD_WRITEABLE))
1689ff94bc40SHeiko Schocher return -EROFS;
1690ff94bc40SHeiko Schocher if (!mtd->_writev)
1691ff94bc40SHeiko Schocher return default_mtd_writev(mtd, vecs, count, to, retlen);
1692ff94bc40SHeiko Schocher return mtd->_writev(mtd, vecs, count, to, retlen);
1693ff94bc40SHeiko Schocher }
1694ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_writev);
1695ff94bc40SHeiko Schocher
1696ff94bc40SHeiko Schocher /**
1697ff94bc40SHeiko Schocher * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1698ff94bc40SHeiko Schocher * @mtd: mtd device description object pointer
1699ff94bc40SHeiko Schocher * @size: a pointer to the ideal or maximum size of the allocation, points
1700ff94bc40SHeiko Schocher * to the actual allocation size on success.
1701ff94bc40SHeiko Schocher *
1702ff94bc40SHeiko Schocher * This routine attempts to allocate a contiguous kernel buffer up to
1703ff94bc40SHeiko Schocher * the specified size, backing off the size of the request exponentially
1704ff94bc40SHeiko Schocher * until the request succeeds or until the allocation size falls below
1705ff94bc40SHeiko Schocher * the system page size. This attempts to make sure it does not adversely
1706ff94bc40SHeiko Schocher * impact system performance, so when allocating more than one page, we
1707ff94bc40SHeiko Schocher * ask the memory allocator to avoid re-trying, swapping, writing back
1708ff94bc40SHeiko Schocher * or performing I/O.
1709ff94bc40SHeiko Schocher *
1710ff94bc40SHeiko Schocher * Note, this function also makes sure that the allocated buffer is aligned to
1711ff94bc40SHeiko Schocher * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1712ff94bc40SHeiko Schocher *
1713ff94bc40SHeiko Schocher * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1714ff94bc40SHeiko Schocher * to handle smaller (i.e. degraded) buffer allocations under low- or
1715ff94bc40SHeiko Schocher * fragmented-memory situations where such reduced allocations, from a
1716ff94bc40SHeiko Schocher * requested ideal, are allowed.
1717ff94bc40SHeiko Schocher *
1718ff94bc40SHeiko Schocher * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1719ff94bc40SHeiko Schocher */
mtd_kmalloc_up_to(const struct mtd_info * mtd,size_t * size)1720ff94bc40SHeiko Schocher void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1721ff94bc40SHeiko Schocher {
1722ff94bc40SHeiko Schocher gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1723ff94bc40SHeiko Schocher __GFP_NORETRY | __GFP_NO_KSWAPD;
1724ff94bc40SHeiko Schocher size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1725ff94bc40SHeiko Schocher void *kbuf;
1726ff94bc40SHeiko Schocher
1727ff94bc40SHeiko Schocher *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1728ff94bc40SHeiko Schocher
1729ff94bc40SHeiko Schocher while (*size > min_alloc) {
1730ff94bc40SHeiko Schocher kbuf = kmalloc(*size, flags);
1731ff94bc40SHeiko Schocher if (kbuf)
1732ff94bc40SHeiko Schocher return kbuf;
1733ff94bc40SHeiko Schocher
1734ff94bc40SHeiko Schocher *size >>= 1;
1735ff94bc40SHeiko Schocher *size = ALIGN(*size, mtd->writesize);
1736ff94bc40SHeiko Schocher }
1737ff94bc40SHeiko Schocher
1738ff94bc40SHeiko Schocher /*
1739ff94bc40SHeiko Schocher * For the last resort allocation allow 'kmalloc()' to do all sorts of
1740ff94bc40SHeiko Schocher * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1741ff94bc40SHeiko Schocher */
1742ff94bc40SHeiko Schocher return kmalloc(*size, GFP_KERNEL);
1743ff94bc40SHeiko Schocher }
1744ff94bc40SHeiko Schocher EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1745ff94bc40SHeiko Schocher #endif
1746ff94bc40SHeiko Schocher
1747ff94bc40SHeiko Schocher #ifdef CONFIG_PROC_FS
1748ff94bc40SHeiko Schocher
1749ff94bc40SHeiko Schocher /*====================================================================*/
1750ff94bc40SHeiko Schocher /* Support for /proc/mtd */
1751ff94bc40SHeiko Schocher
mtd_proc_show(struct seq_file * m,void * v)1752ff94bc40SHeiko Schocher static int mtd_proc_show(struct seq_file *m, void *v)
1753ff94bc40SHeiko Schocher {
1754ff94bc40SHeiko Schocher struct mtd_info *mtd;
1755ff94bc40SHeiko Schocher
1756ff94bc40SHeiko Schocher seq_puts(m, "dev: size erasesize name\n");
1757ff94bc40SHeiko Schocher mutex_lock(&mtd_table_mutex);
1758ff94bc40SHeiko Schocher mtd_for_each_device(mtd) {
1759ff94bc40SHeiko Schocher seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1760ff94bc40SHeiko Schocher mtd->index, (unsigned long long)mtd->size,
1761ff94bc40SHeiko Schocher mtd->erasesize, mtd->name);
1762ff94bc40SHeiko Schocher }
1763ff94bc40SHeiko Schocher mutex_unlock(&mtd_table_mutex);
1764ff94bc40SHeiko Schocher return 0;
1765ff94bc40SHeiko Schocher }
1766ff94bc40SHeiko Schocher
mtd_proc_open(struct inode * inode,struct file * file)1767ff94bc40SHeiko Schocher static int mtd_proc_open(struct inode *inode, struct file *file)
1768ff94bc40SHeiko Schocher {
1769ff94bc40SHeiko Schocher return single_open(file, mtd_proc_show, NULL);
1770ff94bc40SHeiko Schocher }
1771ff94bc40SHeiko Schocher
1772ff94bc40SHeiko Schocher static const struct file_operations mtd_proc_ops = {
1773ff94bc40SHeiko Schocher .open = mtd_proc_open,
1774ff94bc40SHeiko Schocher .read = seq_read,
1775ff94bc40SHeiko Schocher .llseek = seq_lseek,
1776ff94bc40SHeiko Schocher .release = single_release,
1777ff94bc40SHeiko Schocher };
1778ff94bc40SHeiko Schocher #endif /* CONFIG_PROC_FS */
1779ff94bc40SHeiko Schocher
1780ff94bc40SHeiko Schocher /*====================================================================*/
1781ff94bc40SHeiko Schocher /* Init code */
1782ff94bc40SHeiko Schocher
1783ff94bc40SHeiko Schocher #ifndef __UBOOT__
mtd_bdi_init(struct backing_dev_info * bdi,const char * name)1784ff94bc40SHeiko Schocher static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1785ff94bc40SHeiko Schocher {
1786ff94bc40SHeiko Schocher int ret;
1787ff94bc40SHeiko Schocher
1788ff94bc40SHeiko Schocher ret = bdi_init(bdi);
1789ff94bc40SHeiko Schocher if (!ret)
1790ff94bc40SHeiko Schocher ret = bdi_register(bdi, NULL, "%s", name);
1791ff94bc40SHeiko Schocher
1792ff94bc40SHeiko Schocher if (ret)
1793ff94bc40SHeiko Schocher bdi_destroy(bdi);
1794ff94bc40SHeiko Schocher
1795ff94bc40SHeiko Schocher return ret;
1796ff94bc40SHeiko Schocher }
1797ff94bc40SHeiko Schocher
1798ff94bc40SHeiko Schocher static struct proc_dir_entry *proc_mtd;
1799ff94bc40SHeiko Schocher
init_mtd(void)1800ff94bc40SHeiko Schocher static int __init init_mtd(void)
1801ff94bc40SHeiko Schocher {
1802ff94bc40SHeiko Schocher int ret;
1803ff94bc40SHeiko Schocher
1804ff94bc40SHeiko Schocher ret = class_register(&mtd_class);
1805ff94bc40SHeiko Schocher if (ret)
1806ff94bc40SHeiko Schocher goto err_reg;
1807ff94bc40SHeiko Schocher
1808ff94bc40SHeiko Schocher ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1809ff94bc40SHeiko Schocher if (ret)
1810ff94bc40SHeiko Schocher goto err_bdi1;
1811ff94bc40SHeiko Schocher
1812ff94bc40SHeiko Schocher ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1813ff94bc40SHeiko Schocher if (ret)
1814ff94bc40SHeiko Schocher goto err_bdi2;
1815ff94bc40SHeiko Schocher
1816ff94bc40SHeiko Schocher ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1817ff94bc40SHeiko Schocher if (ret)
1818ff94bc40SHeiko Schocher goto err_bdi3;
1819ff94bc40SHeiko Schocher
1820ff94bc40SHeiko Schocher proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1821ff94bc40SHeiko Schocher
1822ff94bc40SHeiko Schocher ret = init_mtdchar();
1823ff94bc40SHeiko Schocher if (ret)
1824ff94bc40SHeiko Schocher goto out_procfs;
1825ff94bc40SHeiko Schocher
1826ff94bc40SHeiko Schocher return 0;
1827ff94bc40SHeiko Schocher
1828ff94bc40SHeiko Schocher out_procfs:
1829ff94bc40SHeiko Schocher if (proc_mtd)
1830ff94bc40SHeiko Schocher remove_proc_entry("mtd", NULL);
1831ff94bc40SHeiko Schocher err_bdi3:
1832ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_ro_mappable);
1833ff94bc40SHeiko Schocher err_bdi2:
1834ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_unmappable);
1835ff94bc40SHeiko Schocher err_bdi1:
1836ff94bc40SHeiko Schocher class_unregister(&mtd_class);
1837ff94bc40SHeiko Schocher err_reg:
1838ff94bc40SHeiko Schocher pr_err("Error registering mtd class or bdi: %d\n", ret);
1839ff94bc40SHeiko Schocher return ret;
1840ff94bc40SHeiko Schocher }
1841ff94bc40SHeiko Schocher
cleanup_mtd(void)1842ff94bc40SHeiko Schocher static void __exit cleanup_mtd(void)
1843ff94bc40SHeiko Schocher {
1844ff94bc40SHeiko Schocher cleanup_mtdchar();
1845ff94bc40SHeiko Schocher if (proc_mtd)
1846ff94bc40SHeiko Schocher remove_proc_entry("mtd", NULL);
1847ff94bc40SHeiko Schocher class_unregister(&mtd_class);
1848ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_unmappable);
1849ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_ro_mappable);
1850ff94bc40SHeiko Schocher bdi_destroy(&mtd_bdi_rw_mappable);
1851ff94bc40SHeiko Schocher }
1852ff94bc40SHeiko Schocher
1853ff94bc40SHeiko Schocher module_init(init_mtd);
1854ff94bc40SHeiko Schocher module_exit(cleanup_mtd);
1855ff94bc40SHeiko Schocher #endif
1856ff94bc40SHeiko Schocher
1857ff94bc40SHeiko Schocher MODULE_LICENSE("GPL");
1858ff94bc40SHeiko Schocher MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1859ff94bc40SHeiko Schocher MODULE_DESCRIPTION("Core MTD registration and access routines");
1860