2 * Core registration and callback routines for MTD
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/mtd/mtd.h>
11 #include <linux/compat.h>
12 #include <ubi_uboot.h>
14 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
16 int add_mtd_device(struct mtd_info *mtd)
20 BUG_ON(mtd->writesize == 0);
22 for (i = 0; i < MAX_MTD_DEVICES; i++)
28 /* default value if not set by driver */
29 if (mtd->bitflip_threshold == 0)
30 mtd->bitflip_threshold = mtd->ecc_strength;
33 /* No need to get a refcount on the module containing
34 the notifier, since we hold the mtd_table_mutex */
36 /* We _know_ we aren't being removed, because
37 our caller is still holding us here. So none
38 of this try_ nonsense, and no bitching about it
47 * del_mtd_device - unregister an MTD device
48 * @mtd: pointer to MTD device info structure
50 * Remove a device from the list of MTD devices present in the system,
51 * and notify each currently active MTD 'user' of its departure.
52 * Returns zero on success or 1 on failure, which currently will happen
53 * if the requested device does not appear to be present in the list.
55 int del_mtd_device(struct mtd_info *mtd)
59 if (mtd_table[mtd->index] != mtd) {
61 } else if (mtd->usecount) {
62 printk(KERN_NOTICE "Removing MTD device #%d (%s)"
63 " with use count %d\n",
64 mtd->index, mtd->name, mtd->usecount);
67 /* No need to get a refcount on the module containing
68 * the notifier, since we hold the mtd_table_mutex */
69 mtd_table[mtd->index] = NULL;
78 * get_mtd_device - obtain a validated handle for an MTD device
79 * @mtd: last known address of the required MTD device
80 * @num: internal device number of the required MTD device
82 * Given a number and NULL address, return the num'th entry in the device
83 * table, if any. Given an address and num == -1, search the device table
84 * for a device with that address and return if it's still present. Given
85 * both, return the num'th driver only if its address matches. Return
88 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
90 struct mtd_info *ret = NULL;
94 for (i = 0; i < MAX_MTD_DEVICES; i++)
95 if (mtd_table[i] == mtd)
97 } else if (num < MAX_MTD_DEVICES) {
99 if (mtd && mtd != ret)
114 * get_mtd_device_nm - obtain a validated handle for an MTD device by
116 * @name: MTD device name to open
118 * This function returns MTD device description structure in case of
119 * success and an error code in case of failure.
121 struct mtd_info *get_mtd_device_nm(const char *name)
123 int i, err = -ENODEV;
124 struct mtd_info *mtd = NULL;
126 for (i = 0; i < MAX_MTD_DEVICES; i++) {
127 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
143 void put_mtd_device(struct mtd_info *mtd)
151 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
153 * mtd_get_len_incl_bad
155 * Check if length including bad blocks fits into device.
157 * @param mtd an MTD device
158 * @param offset offset in flash
159 * @param length image length
160 * @return image length including bad blocks in *len_incl_bad and whether or not
161 * the length returned was truncated in *truncated
163 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
164 const uint64_t length, uint64_t *len_incl_bad,
170 if (!mtd->block_isbad) {
171 *len_incl_bad = length;
175 uint64_t len_excl_bad = 0;
178 while (len_excl_bad < length) {
179 if (offset >= mtd->size) {
184 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
186 if (!mtd->block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
187 len_excl_bad += block_len;
189 *len_incl_bad += block_len;
193 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
196 * Erase is an asynchronous operation. Device drivers are supposed
197 * to call instr->callback() whenever the operation completes, even
198 * if it completes with a failure.
199 * Callers are supposed to pass a callback function and wait for it
200 * to be called before writing to the block.
202 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
204 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
206 if (!(mtd->flags & MTD_WRITEABLE))
208 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
210 instr->state = MTD_ERASE_DONE;
211 mtd_erase_callback(instr);
214 return mtd->_erase(mtd, instr);
217 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
221 if (from < 0 || from > mtd->size || len > mtd->size - from)
227 * In the absence of an error, drivers return a non-negative integer
228 * representing the maximum number of bitflips that were corrected on
229 * any one ecc region (if applicable; zero otherwise).
231 ret_code = mtd->_read(mtd, from, len, retlen, buf);
232 if (unlikely(ret_code < 0))
234 if (mtd->ecc_strength == 0)
235 return 0; /* device lacks ecc */
236 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
239 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
243 if (to < 0 || to > mtd->size || len > mtd->size - to)
245 if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
249 return mtd->_write(mtd, to, len, retlen, buf);
253 * In blackbox flight recorder like scenarios we want to make successful writes
254 * in interrupt context. panic_write() is only intended to be called when its
255 * known the kernel is about to panic and we need the write to succeed. Since
256 * the kernel is not going to be running for much longer, this function can
257 * break locks and delay to ensure the write succeeds (but not sleep).
259 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
263 if (!mtd->_panic_write)
265 if (to < 0 || to > mtd->size || len > mtd->size - to)
267 if (!(mtd->flags & MTD_WRITEABLE))
271 return mtd->_panic_write(mtd, to, len, retlen, buf);
274 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
276 ops->retlen = ops->oobretlen = 0;
279 return mtd->_read_oob(mtd, from, ops);
283 * Method to access the protection register area, present in some flash
284 * devices. The user data is one time programmable but the factory data is read
287 int mtd_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
290 if (!mtd->_get_fact_prot_info)
294 return mtd->_get_fact_prot_info(mtd, buf, len);
297 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
298 size_t *retlen, u_char *buf)
301 if (!mtd->_read_fact_prot_reg)
305 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
308 int mtd_get_user_prot_info(struct mtd_info *mtd, struct otp_info *buf,
311 if (!mtd->_get_user_prot_info)
315 return mtd->_get_user_prot_info(mtd, buf, len);
318 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
319 size_t *retlen, u_char *buf)
322 if (!mtd->_read_user_prot_reg)
326 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
329 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
330 size_t *retlen, u_char *buf)
333 if (!mtd->_write_user_prot_reg)
337 return mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
340 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
342 if (!mtd->_lock_user_prot_reg)
346 return mtd->_lock_user_prot_reg(mtd, from, len);
349 /* Chip-supported device locking */
350 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
354 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
358 return mtd->_lock(mtd, ofs, len);
361 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
365 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
369 return mtd->_unlock(mtd, ofs, len);
372 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
374 if (!mtd->_block_isbad)
376 if (ofs < 0 || ofs > mtd->size)
378 return mtd->_block_isbad(mtd, ofs);
381 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
383 if (!mtd->_block_markbad)
385 if (ofs < 0 || ofs > mtd->size)
387 if (!(mtd->flags & MTD_WRITEABLE))
389 return mtd->_block_markbad(mtd, ofs);