blob: 6f44abdc16102d345e97845f17bc624e5c37eb44 [file] [log] [blame]
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001/*
Wolfgang Denk932394a2005-08-17 12:55:25 +02002 * Copyright (C) 1999-2003 David Woodhouse <dwmw2@infradead.org> et al.
3 *
4 * Released under GPL
5 */
6
7#ifndef __MTD_MTD_H__
8#define __MTD_MTD_H__
William Juulcfa460a2007-10-31 13:53:06 +01009
Wolfgang Denk932394a2005-08-17 12:55:25 +020010#include <linux/types.h>
Stefan Roese8d2effe2009-05-11 16:03:55 +020011#include <div64.h>
Sergey Lapindfe64e22013-01-14 03:46:50 +000012#include <mtd/mtd-abi.h>
13#include <asm/errno.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020014
William Juulcfa460a2007-10-31 13:53:06 +010015#define MTD_CHAR_MAJOR 90
16#define MTD_BLOCK_MAJOR 31
17#define MAX_MTD_DEVICES 32
Wolfgang Denk932394a2005-08-17 12:55:25 +020018
Wolfgang Denk53677ef2008-05-20 16:00:29 +020019#define MTD_ERASE_PENDING 0x01
Wolfgang Denk932394a2005-08-17 12:55:25 +020020#define MTD_ERASING 0x02
21#define MTD_ERASE_SUSPEND 0x04
22#define MTD_ERASE_DONE 0x08
23#define MTD_ERASE_FAILED 0x10
24
Stefan Roese8d2effe2009-05-11 16:03:55 +020025#define MTD_FAIL_ADDR_UNKNOWN -1LL
26
Kyungmin Parkd438d502008-08-13 09:11:02 +090027/*
28 * Enumeration for NAND/OneNAND flash chip state
29 */
30enum {
31 FL_READY,
32 FL_READING,
33 FL_WRITING,
34 FL_ERASING,
35 FL_SYNCING,
36 FL_CACHEDPRG,
37 FL_RESETING,
38 FL_UNLOCKING,
39 FL_LOCKING,
40 FL_PM_SUSPENDED,
41};
42
Wolfgang Denk932394a2005-08-17 12:55:25 +020043/* If the erase fails, fail_addr might indicate exactly which block failed. If
Stefan Roese8d2effe2009-05-11 16:03:55 +020044 fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level or was not
Wolfgang Denk932394a2005-08-17 12:55:25 +020045 specific to any particular block. */
46struct erase_info {
47 struct mtd_info *mtd;
Stefan Roese8d2effe2009-05-11 16:03:55 +020048 uint64_t addr;
49 uint64_t len;
50 uint64_t fail_addr;
Wolfgang Denk932394a2005-08-17 12:55:25 +020051 u_long time;
52 u_long retries;
53 u_int dev;
54 u_int cell;
55 void (*callback) (struct erase_info *self);
56 u_long priv;
57 u_char state;
58 struct erase_info *next;
Marek Vasut6d414192011-09-12 06:04:06 +020059 int scrub;
Wolfgang Denk932394a2005-08-17 12:55:25 +020060};
61
62struct mtd_erase_region_info {
Stefan Roese8d2effe2009-05-11 16:03:55 +020063 uint64_t offset; /* At which this region starts, from the beginning of the MTD */
Wolfgang Denk932394a2005-08-17 12:55:25 +020064 u_int32_t erasesize; /* For this region */
65 u_int32_t numblocks; /* Number of blocks of erasesize in this region */
William Juulcfa460a2007-10-31 13:53:06 +010066 unsigned long *lockmap; /* If keeping bitmap of locks */
67};
68
William Juulcfa460a2007-10-31 13:53:06 +010069/**
70 * struct mtd_oob_ops - oob operation operands
71 * @mode: operation mode
72 *
73 * @len: number of data bytes to write/read
74 *
75 * @retlen: number of data bytes written/read
76 *
77 * @ooblen: number of oob bytes to write/read
78 * @oobretlen: number of oob bytes written/read
79 * @ooboffs: offset of oob data in the oob area (only relevant when
Sergey Lapindfe64e22013-01-14 03:46:50 +000080 * mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
William Juulcfa460a2007-10-31 13:53:06 +010081 * @datbuf: data buffer - if NULL only oob data are read/written
82 * @oobbuf: oob data buffer
83 *
84 * Note, it is allowed to read more then one OOB area at one go, but not write.
85 * The interface assumes that the OOB write requests program only one page's
86 * OOB area.
87 */
88struct mtd_oob_ops {
Sergey Lapindfe64e22013-01-14 03:46:50 +000089 unsigned int mode;
William Juulcfa460a2007-10-31 13:53:06 +010090 size_t len;
91 size_t retlen;
92 size_t ooblen;
93 size_t oobretlen;
94 uint32_t ooboffs;
95 uint8_t *datbuf;
96 uint8_t *oobbuf;
Wolfgang Denk932394a2005-08-17 12:55:25 +020097};
98
99struct mtd_info {
100 u_char type;
101 u_int32_t flags;
Wolfgang Denk8e5e9b92009-07-07 22:35:02 +0200102 uint64_t size; /* Total size of the MTD */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200103
Albert ARIBAUDfa82f872011-08-04 18:45:45 +0200104 /* "Major" erase size for the device. Naïve users may take this
Wolfgang Denk932394a2005-08-17 12:55:25 +0200105 * to be the only erase size available, or may use the more detailed
106 * information below if they desire
107 */
108 u_int32_t erasesize;
William Juulcfa460a2007-10-31 13:53:06 +0100109 /* Minimal writable flash unit size. In case of NOR flash it is 1 (even
110 * though individual bits can be cleared), in case of NAND flash it is
111 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
112 * it is of ECC block size, etc. It is illegal to have writesize = 0.
113 * Any driver registering a struct mtd_info must ensure a writesize of
114 * 1 or larger.
115 */
116 u_int32_t writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200117
Wolfgang Denk4b070802008-08-14 14:41:06 +0200118 u_int32_t oobsize; /* Amount of OOB data per block (e.g. 16) */
119 u_int32_t oobavail; /* Available OOB bytes per block */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200120
Sergey Lapindfe64e22013-01-14 03:46:50 +0000121 /*
122 * read ops return -EUCLEAN if max number of bitflips corrected on any
123 * one region comprising an ecc step equals or exceeds this value.
124 * Settable by driver, else defaults to ecc_strength. User can override
125 * in sysfs. N.B. The meaning of the -EUCLEAN return code has changed;
126 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
127 */
128 unsigned int bitflip_threshold;
129
Wolfgang Denk4b070802008-08-14 14:41:06 +0200130 /* Kernel-only stuff starts here. */
Scott Woodc45912d2008-10-24 16:20:43 -0500131 const char *name;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200132 int index;
133
Sergey Lapindfe64e22013-01-14 03:46:50 +0000134 /* ECC layout structure pointer - read only! */
William Juulcfa460a2007-10-31 13:53:06 +0100135 struct nand_ecclayout *ecclayout;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200136
Sergey Lapindfe64e22013-01-14 03:46:50 +0000137 /* max number of correctible bit errors per ecc step */
138 unsigned int ecc_strength;
139
Wolfgang Denk932394a2005-08-17 12:55:25 +0200140 /* Data for variable erase regions. If numeraseregions is zero,
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200141 * it means that the whole device has erasesize as given above.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200142 */
143 int numeraseregions;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200144 struct mtd_erase_region_info *eraseregions;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200145
Scott Woodc45912d2008-10-24 16:20:43 -0500146 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000147 * Do not call via these pointers, use corresponding mtd_*()
148 * wrappers instead.
Scott Woodc45912d2008-10-24 16:20:43 -0500149 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000150 int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
151 int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
Scott Woodc45912d2008-10-24 16:20:43 -0500152 size_t *retlen, void **virt, phys_addr_t *phys);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000153 void (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
154 int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
155 size_t *retlen, u_char *buf);
156 int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
157 size_t *retlen, const u_char *buf);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200158
Scott Woodc45912d2008-10-24 16:20:43 -0500159 /* In blackbox flight recorder like scenarios we want to make successful
160 writes in interrupt context. panic_write() is only intended to be
161 called when its known the kernel is about to panic and we need the
162 write to succeed. Since the kernel is not going to be running for much
163 longer, this function can break locks and delay to ensure the write
164 succeeds (but not sleep). */
165
Sergey Lapindfe64e22013-01-14 03:46:50 +0000166 int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
Scott Woodc45912d2008-10-24 16:20:43 -0500167
Sergey Lapindfe64e22013-01-14 03:46:50 +0000168 int (*_read_oob) (struct mtd_info *mtd, loff_t from,
William Juulcfa460a2007-10-31 13:53:06 +0100169 struct mtd_oob_ops *ops);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000170 int (*_write_oob) (struct mtd_info *mtd, loff_t to,
William Juulcfa460a2007-10-31 13:53:06 +0100171 struct mtd_oob_ops *ops);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000172 int (*_get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf,
173 size_t len);
174 int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
175 size_t len, size_t *retlen, u_char *buf);
176 int (*_get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf,
177 size_t len);
178 int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
179 size_t len, size_t *retlen, u_char *buf);
180 int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to, size_t len,
181 size_t *retlen, u_char *buf);
182 int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
183 size_t len);
184 void (*_sync) (struct mtd_info *mtd);
185 int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
186 int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
187 int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
188 int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200189 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000190 * If the driver is something smart, like UBI, it may need to maintain
191 * its own reference counting. The below functions are only for driver.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200192 */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000193 int (*_get_device) (struct mtd_info *mtd);
194 void (*_put_device) (struct mtd_info *mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100195
196/* XXX U-BOOT XXX */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200197#if 0
William Juulcfa460a2007-10-31 13:53:06 +0100198 /* kvec-based read/write methods.
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200199 NB: The 'count' parameter is the number of _vectors_, each of
Wolfgang Denk932394a2005-08-17 12:55:25 +0200200 which contains an (ofs, len) tuple.
201 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200202 int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200203#endif
William Juulcfa460a2007-10-31 13:53:06 +0100204/* XXX U-BOOT XXX */
205#if 0
206 struct notifier_block reboot_notifier; /* default mode before reboot */
207#endif
208
209 /* ECC status information */
210 struct mtd_ecc_stats ecc_stats;
211 /* Subpage shift (NAND) */
212 int subpage_sft;
213
Wolfgang Denk932394a2005-08-17 12:55:25 +0200214 void *priv;
215
216 struct module *owner;
217 int usecount;
218};
219
Sergey Lapindfe64e22013-01-14 03:46:50 +0000220int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
221int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
222 u_char *buf);
223int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
224 const u_char *buf);
225int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
226 const u_char *buf);
227
228int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
229
230static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
231 struct mtd_oob_ops *ops)
232{
233 ops->retlen = ops->oobretlen = 0;
234 if (!mtd->_write_oob)
235 return -EOPNOTSUPP;
236 if (!(mtd->flags & MTD_WRITEABLE))
237 return -EROFS;
238 return mtd->_write_oob(mtd, to, ops);
239}
240
241int mtd_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
242 size_t len);
243int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
244 size_t *retlen, u_char *buf);
245int mtd_get_user_prot_info(struct mtd_info *mtd, struct otp_info *buf,
246 size_t len);
247int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
248 size_t *retlen, u_char *buf);
249int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
250 size_t *retlen, u_char *buf);
251int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
252
253/* XXX U-BOOT XXX */
254#if 0
255int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
256 unsigned long count, loff_t to, size_t *retlen);
257#endif
258
259static inline void mtd_sync(struct mtd_info *mtd)
260{
261 if (mtd->_sync)
262 mtd->_sync(mtd);
263}
264
265int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
266int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
267int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
268int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
269int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
270
Stefan Roese8d2effe2009-05-11 16:03:55 +0200271static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
272{
273 do_div(sz, mtd->erasesize);
274 return sz;
275}
276
277static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
278{
279 return do_div(sz, mtd->erasesize);
280}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200281
Sergey Lapindfe64e22013-01-14 03:46:50 +0000282static inline int mtd_has_oob(const struct mtd_info *mtd)
283{
284 return mtd->_read_oob && mtd->_write_oob;
285}
286
287static inline int mtd_can_have_bb(const struct mtd_info *mtd)
288{
289 return !!mtd->_block_isbad;
290}
291
Wolfgang Denk932394a2005-08-17 12:55:25 +0200292 /* Kernel-side ioctl definitions */
293
294extern int add_mtd_device(struct mtd_info *mtd);
295extern int del_mtd_device (struct mtd_info *mtd);
296
297extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
William Juulcfa460a2007-10-31 13:53:06 +0100298extern struct mtd_info *get_mtd_device_nm(const char *name);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200299
300extern void put_mtd_device(struct mtd_info *mtd);
Ben Gardiner4ba692f2010-08-31 17:48:01 -0400301extern void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
302 const uint64_t length, uint64_t *len_incl_bad,
303 int *truncated);
William Juulcfa460a2007-10-31 13:53:06 +0100304/* XXX U-BOOT XXX */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200305#if 0
306struct mtd_notifier {
307 void (*add)(struct mtd_info *mtd);
308 void (*remove)(struct mtd_info *mtd);
309 struct list_head list;
310};
311
Wolfgang Denk932394a2005-08-17 12:55:25 +0200312extern void register_mtd_user (struct mtd_notifier *new);
313extern int unregister_mtd_user (struct mtd_notifier *old);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200314#endif
315
Wolfgang Denk932394a2005-08-17 12:55:25 +0200316#ifdef CONFIG_MTD_PARTITIONS
317void mtd_erase_callback(struct erase_info *instr);
318#else
319static inline void mtd_erase_callback(struct erase_info *instr)
320{
321 if (instr->callback)
322 instr->callback(instr);
323}
324#endif
325
326/*
327 * Debugging macro and defines
328 */
329#define MTD_DEBUG_LEVEL0 (0) /* Quiet */
330#define MTD_DEBUG_LEVEL1 (1) /* Audible */
331#define MTD_DEBUG_LEVEL2 (2) /* Loud */
332#define MTD_DEBUG_LEVEL3 (3) /* Noisy */
333
334#ifdef CONFIG_MTD_DEBUG
Sergey Lapindfe64e22013-01-14 03:46:50 +0000335#define pr_debug(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
Scott Wood3167c532008-06-20 12:38:57 -0500336#define MTDDEBUG(n, args...) \
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200337 do { \
Wolfgang Denk932394a2005-08-17 12:55:25 +0200338 if (n <= CONFIG_MTD_DEBUG_VERBOSE) \
339 printk(KERN_INFO args); \
340 } while(0)
341#else /* CONFIG_MTD_DEBUG */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000342#define pr_debug(args...)
Scott Woodc45912d2008-10-24 16:20:43 -0500343#define MTDDEBUG(n, args...) \
344 do { \
345 if (0) \
346 printk(KERN_INFO args); \
347 } while(0)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200348#endif /* CONFIG_MTD_DEBUG */
Sergey Lapindfe64e22013-01-14 03:46:50 +0000349#define pr_info(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
350#define pr_warn(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
351#define pr_err(args...) MTDDEBUG(MTD_DEBUG_LEVEL0, args)
352
353static inline int mtd_is_bitflip(int err) {
354 return err == -EUCLEAN;
355}
356
357static inline int mtd_is_eccerr(int err) {
358 return err == -EBADMSG;
359}
360
361static inline int mtd_is_bitflip_or_eccerr(int err) {
362 return mtd_is_bitflip(err) || mtd_is_eccerr(err);
363}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200364
365#endif /* __MTD_MTD_H__ */