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,
220 if (from < 0 || from > mtd->size || len > mtd->size - from)
224 return mtd->_read(mtd, from, len, retlen, buf);
227 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
231 if (to < 0 || to > mtd->size || len > mtd->size - to)
233 if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
237 return mtd->_write(mtd, to, len, retlen, buf);
241 * In blackbox flight recorder like scenarios we want to make successful writes
242 * in interrupt context. panic_write() is only intended to be called when its
243 * known the kernel is about to panic and we need the write to succeed. Since
244 * the kernel is not going to be running for much longer, this function can
245 * break locks and delay to ensure the write succeeds (but not sleep).
247 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
251 if (!mtd->_panic_write)
253 if (to < 0 || to > mtd->size || len > mtd->size - to)
255 if (!(mtd->flags & MTD_WRITEABLE))
259 return mtd->_panic_write(mtd, to, len, retlen, buf);
262 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
264 ops->retlen = ops->oobretlen = 0;
267 return mtd->_read_oob(mtd, from, ops);
271 * Method to access the protection register area, present in some flash
272 * devices. The user data is one time programmable but the factory data is read
275 int mtd_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
278 if (!mtd->_get_fact_prot_info)
282 return mtd->_get_fact_prot_info(mtd, buf, len);
285 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
286 size_t *retlen, u_char *buf)
289 if (!mtd->_read_fact_prot_reg)
293 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
296 int mtd_get_user_prot_info(struct mtd_info *mtd, struct otp_info *buf,
299 if (!mtd->_get_user_prot_info)
303 return mtd->_get_user_prot_info(mtd, buf, len);
306 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
307 size_t *retlen, u_char *buf)
310 if (!mtd->_read_user_prot_reg)
314 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
317 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
318 size_t *retlen, u_char *buf)
321 if (!mtd->_write_user_prot_reg)
325 return mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
328 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
330 if (!mtd->_lock_user_prot_reg)
334 return mtd->_lock_user_prot_reg(mtd, from, len);
337 /* Chip-supported device locking */
338 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
342 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
346 return mtd->_lock(mtd, ofs, len);
349 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
353 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
357 return mtd->_unlock(mtd, ofs, len);
360 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
362 if (!mtd->_block_isbad)
364 if (ofs < 0 || ofs > mtd->size)
366 return mtd->_block_isbad(mtd, ofs);
369 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
371 if (!mtd->_block_markbad)
373 if (ofs < 0 || ofs > mtd->size)
375 if (!(mtd->flags & MTD_WRITEABLE))
377 return mtd->_block_markbad(mtd, ofs);