xref: /openbmc/linux/include/linux/mtd/mtd.h (revision b8bb76713ec50df2f11efee386e16f93d51e1076)
1 /*
2  * 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__
9 
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/uio.h>
13 #include <linux/notifier.h>
14 
15 #include <linux/mtd/compatmac.h>
16 #include <mtd/mtd-abi.h>
17 
18 #include <asm/div64.h>
19 
20 #define MTD_CHAR_MAJOR 90
21 #define MTD_BLOCK_MAJOR 31
22 #define MAX_MTD_DEVICES 32
23 
24 #define MTD_ERASE_PENDING      	0x01
25 #define MTD_ERASING		0x02
26 #define MTD_ERASE_SUSPEND	0x04
27 #define MTD_ERASE_DONE          0x08
28 #define MTD_ERASE_FAILED        0x10
29 
30 #define MTD_FAIL_ADDR_UNKNOWN -1LL
31 
32 /* If the erase fails, fail_addr might indicate exactly which block failed.  If
33    fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level or was not
34    specific to any particular block. */
35 struct erase_info {
36 	struct mtd_info *mtd;
37 	uint64_t addr;
38 	uint64_t len;
39 	uint64_t fail_addr;
40 	u_long time;
41 	u_long retries;
42 	unsigned dev;
43 	unsigned cell;
44 	void (*callback) (struct erase_info *self);
45 	u_long priv;
46 	u_char state;
47 	struct erase_info *next;
48 };
49 
50 struct mtd_erase_region_info {
51 	uint64_t offset;			/* At which this region starts, from the beginning of the MTD */
52 	uint32_t erasesize;		/* For this region */
53 	uint32_t numblocks;		/* Number of blocks of erasesize in this region */
54 	unsigned long *lockmap;		/* If keeping bitmap of locks */
55 };
56 
57 /*
58  * oob operation modes
59  *
60  * MTD_OOB_PLACE:	oob data are placed at the given offset
61  * MTD_OOB_AUTO:	oob data are automatically placed at the free areas
62  *			which are defined by the ecclayout
63  * MTD_OOB_RAW:		mode to read raw data+oob in one chunk. The oob data
64  *			is inserted into the data. Thats a raw image of the
65  *			flash contents.
66  */
67 typedef enum {
68 	MTD_OOB_PLACE,
69 	MTD_OOB_AUTO,
70 	MTD_OOB_RAW,
71 } mtd_oob_mode_t;
72 
73 /**
74  * struct mtd_oob_ops - oob operation operands
75  * @mode:	operation mode
76  *
77  * @len:	number of data bytes to write/read
78  *
79  * @retlen:	number of data bytes written/read
80  *
81  * @ooblen:	number of oob bytes to write/read
82  * @oobretlen:	number of oob bytes written/read
83  * @ooboffs:	offset of oob data in the oob area (only relevant when
84  *		mode = MTD_OOB_PLACE)
85  * @datbuf:	data buffer - if NULL only oob data are read/written
86  * @oobbuf:	oob data buffer
87  *
88  * Note, it is allowed to read more than one OOB area at one go, but not write.
89  * The interface assumes that the OOB write requests program only one page's
90  * OOB area.
91  */
92 struct mtd_oob_ops {
93 	mtd_oob_mode_t	mode;
94 	size_t		len;
95 	size_t		retlen;
96 	size_t		ooblen;
97 	size_t		oobretlen;
98 	uint32_t	ooboffs;
99 	uint8_t		*datbuf;
100 	uint8_t		*oobbuf;
101 };
102 
103 struct mtd_info {
104 	u_char type;
105 	uint32_t flags;
106 	uint64_t size;	 // Total size of the MTD
107 
108 	/* "Major" erase size for the device. Naïve users may take this
109 	 * to be the only erase size available, or may use the more detailed
110 	 * information below if they desire
111 	 */
112 	uint32_t erasesize;
113 	/* Minimal writable flash unit size. In case of NOR flash it is 1 (even
114 	 * though individual bits can be cleared), in case of NAND flash it is
115 	 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
116 	 * it is of ECC block size, etc. It is illegal to have writesize = 0.
117 	 * Any driver registering a struct mtd_info must ensure a writesize of
118 	 * 1 or larger.
119 	 */
120 	uint32_t writesize;
121 
122 	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
123 	uint32_t oobavail;  // Available OOB bytes per block
124 
125 	/*
126 	 * If erasesize is a power of 2 then the shift is stored in
127 	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
128 	 */
129 	unsigned int erasesize_shift;
130 	unsigned int writesize_shift;
131 	/* Masks based on erasesize_shift and writesize_shift */
132 	unsigned int erasesize_mask;
133 	unsigned int writesize_mask;
134 
135 	// Kernel-only stuff starts here.
136 	const char *name;
137 	int index;
138 
139 	/* ecc layout structure pointer - read only ! */
140 	struct nand_ecclayout *ecclayout;
141 
142 	/* Data for variable erase regions. If numeraseregions is zero,
143 	 * it means that the whole device has erasesize as given above.
144 	 */
145 	int numeraseregions;
146 	struct mtd_erase_region_info *eraseregions;
147 
148 	/*
149 	 * Erase is an asynchronous operation.  Device drivers are supposed
150 	 * to call instr->callback() whenever the operation completes, even
151 	 * if it completes with a failure.
152 	 * Callers are supposed to pass a callback function and wait for it
153 	 * to be called before writing to the block.
154 	 */
155 	int (*erase) (struct mtd_info *mtd, struct erase_info *instr);
156 
157 	/* This stuff for eXecute-In-Place */
158 	/* phys is optional and may be set to NULL */
159 	int (*point) (struct mtd_info *mtd, loff_t from, size_t len,
160 			size_t *retlen, void **virt, resource_size_t *phys);
161 
162 	/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
163 	void (*unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
164 
165 
166 	int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
167 	int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
168 
169 	/* In blackbox flight recorder like scenarios we want to make successful
170 	   writes in interrupt context. panic_write() is only intended to be
171 	   called when its known the kernel is about to panic and we need the
172 	   write to succeed. Since the kernel is not going to be running for much
173 	   longer, this function can break locks and delay to ensure the write
174 	   succeeds (but not sleep). */
175 
176 	int (*panic_write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
177 
178 	int (*read_oob) (struct mtd_info *mtd, loff_t from,
179 			 struct mtd_oob_ops *ops);
180 	int (*write_oob) (struct mtd_info *mtd, loff_t to,
181 			 struct mtd_oob_ops *ops);
182 
183 	/*
184 	 * Methods to access the protection register area, present in some
185 	 * flash devices. The user data is one time programmable but the
186 	 * factory data is read only.
187 	 */
188 	int (*get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len);
189 	int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
190 	int (*get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len);
191 	int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
192 	int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
193 	int (*lock_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len);
194 
195 	/* kvec-based read/write methods.
196 	   NB: The 'count' parameter is the number of _vectors_, each of
197 	   which contains an (ofs, len) tuple.
198 	*/
199 	int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen);
200 
201 	/* Sync */
202 	void (*sync) (struct mtd_info *mtd);
203 
204 	/* Chip-supported device locking */
205 	int (*lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
206 	int (*unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
207 
208 	/* Power Management functions */
209 	int (*suspend) (struct mtd_info *mtd);
210 	void (*resume) (struct mtd_info *mtd);
211 
212 	/* Bad block management functions */
213 	int (*block_isbad) (struct mtd_info *mtd, loff_t ofs);
214 	int (*block_markbad) (struct mtd_info *mtd, loff_t ofs);
215 
216 	struct notifier_block reboot_notifier;  /* default mode before reboot */
217 
218 	/* ECC status information */
219 	struct mtd_ecc_stats ecc_stats;
220 	/* Subpage shift (NAND) */
221 	int subpage_sft;
222 
223 	void *priv;
224 
225 	struct module *owner;
226 	int usecount;
227 
228 	/* If the driver is something smart, like UBI, it may need to maintain
229 	 * its own reference counting. The below functions are only for driver.
230 	 * The driver may register its callbacks. These callbacks are not
231 	 * supposed to be called by MTD users */
232 	int (*get_device) (struct mtd_info *mtd);
233 	void (*put_device) (struct mtd_info *mtd);
234 };
235 
236 static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
237 {
238 	if (mtd->erasesize_shift)
239 		return sz >> mtd->erasesize_shift;
240 	do_div(sz, mtd->erasesize);
241 	return sz;
242 }
243 
244 static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
245 {
246 	if (mtd->erasesize_shift)
247 		return sz & mtd->erasesize_mask;
248 	return do_div(sz, mtd->erasesize);
249 }
250 
251 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
252 {
253 	if (mtd->writesize_shift)
254 		return sz >> mtd->writesize_shift;
255 	do_div(sz, mtd->writesize);
256 	return sz;
257 }
258 
259 static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
260 {
261 	if (mtd->writesize_shift)
262 		return sz & mtd->writesize_mask;
263 	return do_div(sz, mtd->writesize);
264 }
265 
266 	/* Kernel-side ioctl definitions */
267 
268 extern int add_mtd_device(struct mtd_info *mtd);
269 extern int del_mtd_device (struct mtd_info *mtd);
270 
271 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
272 extern struct mtd_info *get_mtd_device_nm(const char *name);
273 
274 extern void put_mtd_device(struct mtd_info *mtd);
275 
276 
277 struct mtd_notifier {
278 	void (*add)(struct mtd_info *mtd);
279 	void (*remove)(struct mtd_info *mtd);
280 	struct list_head list;
281 };
282 
283 
284 extern void register_mtd_user (struct mtd_notifier *new);
285 extern int unregister_mtd_user (struct mtd_notifier *old);
286 
287 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
288 		       unsigned long count, loff_t to, size_t *retlen);
289 
290 int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs,
291 		      unsigned long count, loff_t from, size_t *retlen);
292 
293 #ifdef CONFIG_MTD_PARTITIONS
294 void mtd_erase_callback(struct erase_info *instr);
295 #else
296 static inline void mtd_erase_callback(struct erase_info *instr)
297 {
298 	if (instr->callback)
299 		instr->callback(instr);
300 }
301 #endif
302 
303 /*
304  * Debugging macro and defines
305  */
306 #define MTD_DEBUG_LEVEL0	(0)	/* Quiet   */
307 #define MTD_DEBUG_LEVEL1	(1)	/* Audible */
308 #define MTD_DEBUG_LEVEL2	(2)	/* Loud    */
309 #define MTD_DEBUG_LEVEL3	(3)	/* Noisy   */
310 
311 #ifdef CONFIG_MTD_DEBUG
312 #define DEBUG(n, args...)				\
313 	do {						\
314 		if (n <= CONFIG_MTD_DEBUG_VERBOSE)	\
315 			printk(KERN_INFO args);		\
316 	} while(0)
317 #else /* CONFIG_MTD_DEBUG */
318 #define DEBUG(n, args...)				\
319 	do {						\
320 		if (0)					\
321 			printk(KERN_INFO args);		\
322 	} while(0)
323 
324 #endif /* CONFIG_MTD_DEBUG */
325 
326 #endif /* __MTD_MTD_H__ */
327