1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#ifndef __MTD_MTD_H__
21#define __MTD_MTD_H__
22
23#include <linux/types.h>
24#include <linux/uio.h>
25#include <linux/notifier.h>
26#include <linux/device.h>
27
28#include <mtd/mtd-abi.h>
29
30#include <asm/div64.h>
31
32#define MTD_CHAR_MAJOR 90
33#define MTD_BLOCK_MAJOR 31
34
35#define MTD_ERASE_PENDING 0x01
36#define MTD_ERASING 0x02
37#define MTD_ERASE_SUSPEND 0x04
38#define MTD_ERASE_DONE 0x08
39#define MTD_ERASE_FAILED 0x10
40
41#define MTD_FAIL_ADDR_UNKNOWN -1LL
42
43
44
45
46
47
48struct erase_info {
49 struct mtd_info *mtd;
50 uint64_t addr;
51 uint64_t len;
52 uint64_t fail_addr;
53 u_long time;
54 u_long retries;
55 unsigned dev;
56 unsigned cell;
57 void (*callback) (struct erase_info *self);
58 u_long priv;
59 u_char state;
60 struct erase_info *next;
61};
62
63struct mtd_erase_region_info {
64 uint64_t offset;
65 uint32_t erasesize;
66 uint32_t numblocks;
67 unsigned long *lockmap;
68};
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89struct mtd_oob_ops {
90 unsigned int mode;
91 size_t len;
92 size_t retlen;
93 size_t ooblen;
94 size_t oobretlen;
95 uint32_t ooboffs;
96 uint8_t *datbuf;
97 uint8_t *oobbuf;
98};
99
100#define MTD_MAX_OOBFREE_ENTRIES_LARGE 32
101#define MTD_MAX_ECCPOS_ENTRIES_LARGE 448
102
103
104
105
106
107
108struct nand_ecclayout {
109 __u32 eccbytes;
110 __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
111 __u32 oobavail;
112 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
113};
114
115struct module;
116
117struct mtd_info {
118 u_char type;
119 uint32_t flags;
120 uint64_t size;
121
122
123
124
125
126 uint32_t erasesize;
127
128
129
130
131
132
133
134 uint32_t writesize;
135
136
137
138
139
140
141
142
143
144
145 uint32_t writebufsize;
146
147 uint32_t oobsize;
148 uint32_t oobavail;
149
150
151
152
153
154 unsigned int erasesize_shift;
155 unsigned int writesize_shift;
156
157 unsigned int erasesize_mask;
158 unsigned int writesize_mask;
159
160
161 const char *name;
162 int index;
163
164
165 struct nand_ecclayout *ecclayout;
166
167
168 unsigned int ecc_strength;
169
170
171
172
173 int numeraseregions;
174 struct mtd_erase_region_info *eraseregions;
175
176
177
178
179
180 int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
181 int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
182 size_t *retlen, void **virt, resource_size_t *phys);
183 int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
184 unsigned long (*_get_unmapped_area) (struct mtd_info *mtd,
185 unsigned long len,
186 unsigned long offset,
187 unsigned long flags);
188 int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
189 size_t *retlen, u_char *buf);
190 int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
191 size_t *retlen, const u_char *buf);
192 int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
193 size_t *retlen, const u_char *buf);
194 int (*_read_oob) (struct mtd_info *mtd, loff_t from,
195 struct mtd_oob_ops *ops);
196 int (*_write_oob) (struct mtd_info *mtd, loff_t to,
197 struct mtd_oob_ops *ops);
198 int (*_get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf,
199 size_t len);
200 int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
201 size_t len, size_t *retlen, u_char *buf);
202 int (*_get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf,
203 size_t len);
204 int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
205 size_t len, size_t *retlen, u_char *buf);
206 int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
207 size_t len, size_t *retlen, u_char *buf);
208 int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
209 size_t len);
210 int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
211 unsigned long count, loff_t to, size_t *retlen);
212 void (*_sync) (struct mtd_info *mtd);
213 int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
214 int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
215 int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
216 int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
217 int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
218 int (*_suspend) (struct mtd_info *mtd);
219 void (*_resume) (struct mtd_info *mtd);
220
221
222
223
224 int (*_get_device) (struct mtd_info *mtd);
225 void (*_put_device) (struct mtd_info *mtd);
226
227
228
229
230 struct backing_dev_info *backing_dev_info;
231
232 struct notifier_block reboot_notifier;
233
234
235 struct mtd_ecc_stats ecc_stats;
236
237 int subpage_sft;
238
239 void *priv;
240
241 struct module *owner;
242 struct device dev;
243 int usecount;
244};
245
246int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
247int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
248 void **virt, resource_size_t *phys);
249int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
250unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
251 unsigned long offset, unsigned long flags);
252int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
253 u_char *buf);
254int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
255 const u_char *buf);
256int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
257 const u_char *buf);
258
259static inline int mtd_read_oob(struct mtd_info *mtd, loff_t from,
260 struct mtd_oob_ops *ops)
261{
262 ops->retlen = ops->oobretlen = 0;
263 if (!mtd->_read_oob)
264 return -EOPNOTSUPP;
265 return mtd->_read_oob(mtd, from, ops);
266}
267
268static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
269 struct mtd_oob_ops *ops)
270{
271 ops->retlen = ops->oobretlen = 0;
272 if (!mtd->_write_oob)
273 return -EOPNOTSUPP;
274 if (!(mtd->flags & MTD_WRITEABLE))
275 return -EROFS;
276 return mtd->_write_oob(mtd, to, ops);
277}
278
279int mtd_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
280 size_t len);
281int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
282 size_t *retlen, u_char *buf);
283int mtd_get_user_prot_info(struct mtd_info *mtd, struct otp_info *buf,
284 size_t len);
285int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
286 size_t *retlen, u_char *buf);
287int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
288 size_t *retlen, u_char *buf);
289int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
290
291int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
292 unsigned long count, loff_t to, size_t *retlen);
293
294static inline void mtd_sync(struct mtd_info *mtd)
295{
296 if (mtd->_sync)
297 mtd->_sync(mtd);
298}
299
300int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
301int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
302int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
303int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
304int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
305
306static inline int mtd_suspend(struct mtd_info *mtd)
307{
308 return mtd->_suspend ? mtd->_suspend(mtd) : 0;
309}
310
311static inline void mtd_resume(struct mtd_info *mtd)
312{
313 if (mtd->_resume)
314 mtd->_resume(mtd);
315}
316
317static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
318{
319 if (mtd->erasesize_shift)
320 return sz >> mtd->erasesize_shift;
321 do_div(sz, mtd->erasesize);
322 return sz;
323}
324
325static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
326{
327 if (mtd->erasesize_shift)
328 return sz & mtd->erasesize_mask;
329 return do_div(sz, mtd->erasesize);
330}
331
332static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
333{
334 if (mtd->writesize_shift)
335 return sz >> mtd->writesize_shift;
336 do_div(sz, mtd->writesize);
337 return sz;
338}
339
340static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
341{
342 if (mtd->writesize_shift)
343 return sz & mtd->writesize_mask;
344 return do_div(sz, mtd->writesize);
345}
346
347static inline int mtd_has_oob(const struct mtd_info *mtd)
348{
349 return mtd->_read_oob && mtd->_write_oob;
350}
351
352static inline int mtd_can_have_bb(const struct mtd_info *mtd)
353{
354 return !!mtd->_block_isbad;
355}
356
357
358
359struct mtd_partition;
360struct mtd_part_parser_data;
361
362extern int mtd_device_parse_register(struct mtd_info *mtd,
363 const char **part_probe_types,
364 struct mtd_part_parser_data *parser_data,
365 const struct mtd_partition *defparts,
366 int defnr_parts);
367#define mtd_device_register(master, parts, nr_parts) \
368 mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
369extern int mtd_device_unregister(struct mtd_info *master);
370extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
371extern int __get_mtd_device(struct mtd_info *mtd);
372extern void __put_mtd_device(struct mtd_info *mtd);
373extern struct mtd_info *get_mtd_device_nm(const char *name);
374extern void put_mtd_device(struct mtd_info *mtd);
375
376
377struct mtd_notifier {
378 void (*add)(struct mtd_info *mtd);
379 void (*remove)(struct mtd_info *mtd);
380 struct list_head list;
381};
382
383
384extern void register_mtd_user (struct mtd_notifier *new);
385extern int unregister_mtd_user (struct mtd_notifier *old);
386void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
387
388void mtd_erase_callback(struct erase_info *instr);
389
390static inline int mtd_is_bitflip(int err) {
391 return err == -EUCLEAN;
392}
393
394static inline int mtd_is_eccerr(int err) {
395 return err == -EBADMSG;
396}
397
398static inline int mtd_is_bitflip_or_eccerr(int err) {
399 return mtd_is_bitflip(err) || mtd_is_eccerr(err);
400}
401
402#endif
403