2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
8 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
9 * added support for read_oob, write_oob
14 #include <asm/errno.h>
16 #include <linux/types.h>
17 #include <linux/list.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/mtd/compat.h>
22 /* Our partition linked list */
23 struct list_head mtd_partitions;
25 /* Our partition node structure */
28 struct mtd_info *master;
31 struct list_head list;
36 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
37 * the pointer to that structure with this macro.
39 #define PART(x) ((struct mtd_part *)(x))
43 * MTD methods which simply translate the effective address and pass through
44 * to the _real_ device.
47 static int part_read (struct mtd_info *mtd, loff_t from, size_t len,
48 size_t *retlen, u_char *buf)
50 struct mtd_part *part = PART(mtd);
53 if (from >= mtd->size)
55 else if (from + len > mtd->size)
56 len = mtd->size - from;
57 res = part->master->read (part->master, from + part->offset,
61 mtd->ecc_stats.corrected++;
63 mtd->ecc_stats.failed++;
69 static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
70 size_t *retlen, void **virt, resource_size_t *phys)
72 struct mtd_part *part = PART(mtd);
73 if (from >= mtd->size)
75 else if (from + len > mtd->size)
76 len = mtd->size - from;
77 return part->master->point (part->master, from + part->offset,
78 len, retlen, virt, phys);
81 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
83 struct mtd_part *part = PART(mtd);
85 part->master->unpoint(part->master, from + part->offset, len);
89 static int part_read_oob(struct mtd_info *mtd, loff_t from,
90 struct mtd_oob_ops *ops)
92 struct mtd_part *part = PART(mtd);
95 if (from >= mtd->size)
97 if (ops->datbuf && from + ops->len > mtd->size)
99 res = part->master->read_oob(part->master, from + part->offset, ops);
103 mtd->ecc_stats.corrected++;
105 mtd->ecc_stats.failed++;
110 static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
111 size_t *retlen, u_char *buf)
113 struct mtd_part *part = PART(mtd);
114 return part->master->read_user_prot_reg (part->master, from,
118 static int part_get_user_prot_info (struct mtd_info *mtd,
119 struct otp_info *buf, size_t len)
121 struct mtd_part *part = PART(mtd);
122 return part->master->get_user_prot_info (part->master, buf, len);
125 static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
126 size_t *retlen, u_char *buf)
128 struct mtd_part *part = PART(mtd);
129 return part->master->read_fact_prot_reg (part->master, from,
133 static int part_get_fact_prot_info (struct mtd_info *mtd,
134 struct otp_info *buf, size_t len)
136 struct mtd_part *part = PART(mtd);
137 return part->master->get_fact_prot_info (part->master, buf, len);
140 static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
141 size_t *retlen, const u_char *buf)
143 struct mtd_part *part = PART(mtd);
144 if (!(mtd->flags & MTD_WRITEABLE))
148 else if (to + len > mtd->size)
149 len = mtd->size - to;
150 return part->master->write (part->master, to + part->offset,
155 static int part_panic_write (struct mtd_info *mtd, loff_t to, size_t len,
156 size_t *retlen, const u_char *buf)
158 struct mtd_part *part = PART(mtd);
159 if (!(mtd->flags & MTD_WRITEABLE))
163 else if (to + len > mtd->size)
164 len = mtd->size - to;
165 return part->master->panic_write (part->master, to + part->offset,
170 static int part_write_oob(struct mtd_info *mtd, loff_t to,
171 struct mtd_oob_ops *ops)
173 struct mtd_part *part = PART(mtd);
175 if (!(mtd->flags & MTD_WRITEABLE))
180 if (ops->datbuf && to + ops->len > mtd->size)
182 return part->master->write_oob(part->master, to + part->offset, ops);
185 static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
186 size_t *retlen, u_char *buf)
188 struct mtd_part *part = PART(mtd);
189 return part->master->write_user_prot_reg (part->master, from,
193 static int part_lock_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len)
195 struct mtd_part *part = PART(mtd);
196 return part->master->lock_user_prot_reg (part->master, from, len);
200 static int part_writev (struct mtd_info *mtd, const struct kvec *vecs,
201 unsigned long count, loff_t to, size_t *retlen)
203 struct mtd_part *part = PART(mtd);
204 if (!(mtd->flags & MTD_WRITEABLE))
206 return part->master->writev (part->master, vecs, count,
207 to + part->offset, retlen);
211 static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
213 struct mtd_part *part = PART(mtd);
215 if (!(mtd->flags & MTD_WRITEABLE))
217 if (instr->addr >= mtd->size)
219 instr->addr += part->offset;
220 ret = part->master->erase(part->master, instr);
222 if (instr->fail_addr != 0xffffffff)
223 instr->fail_addr -= part->offset;
224 instr->addr -= part->offset;
229 void mtd_erase_callback(struct erase_info *instr)
231 if (instr->mtd->erase == part_erase) {
232 struct mtd_part *part = PART(instr->mtd);
234 if (instr->fail_addr != 0xffffffff)
235 instr->fail_addr -= part->offset;
236 instr->addr -= part->offset;
239 instr->callback(instr);
242 EXPORT_SYMBOL_GPL(mtd_erase_callback);
246 static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
248 struct mtd_part *part = PART(mtd);
249 if ((len + ofs) > mtd->size)
251 return part->master->lock(part->master, ofs + part->offset, len);
254 static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
256 struct mtd_part *part = PART(mtd);
257 if ((len + ofs) > mtd->size)
259 return part->master->unlock(part->master, ofs + part->offset, len);
263 static void part_sync(struct mtd_info *mtd)
265 struct mtd_part *part = PART(mtd);
266 part->master->sync(part->master);
270 static int part_suspend(struct mtd_info *mtd)
272 struct mtd_part *part = PART(mtd);
273 return part->master->suspend(part->master);
276 static void part_resume(struct mtd_info *mtd)
278 struct mtd_part *part = PART(mtd);
279 part->master->resume(part->master);
283 static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
285 struct mtd_part *part = PART(mtd);
286 if (ofs >= mtd->size)
289 return part->master->block_isbad(part->master, ofs);
292 static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
294 struct mtd_part *part = PART(mtd);
297 if (!(mtd->flags & MTD_WRITEABLE))
299 if (ofs >= mtd->size)
302 res = part->master->block_markbad(part->master, ofs);
305 mtd->ecc_stats.badblocks++;
311 * This function unregisters and destroy all slave MTD objects which are
312 * attached to the given master MTD object.
315 int del_mtd_partitions(struct mtd_info *master)
317 struct list_head *node;
318 struct mtd_part *slave;
320 for (node = mtd_partitions.next;
321 node != &mtd_partitions;
323 slave = list_entry(node, struct mtd_part, list);
324 if (slave->master == master) {
325 struct list_head *prev = node->prev;
326 __list_del(prev, node->next);
327 if(slave->registered)
328 del_mtd_device(&slave->mtd);
338 * This function, given a master MTD object and a partition table, creates
339 * and registers slave MTD objects which are bound to the master according to
340 * the partition definitions.
341 * (Q: should we register the master MTD object as well?)
344 int add_mtd_partitions(struct mtd_info *master,
345 const struct mtd_partition *parts,
348 struct mtd_part *slave;
349 u_int32_t cur_offset = 0;
353 * Need to init the list here, since LIST_INIT() does not
354 * work on platforms where relocation has problems (like MIPS
357 if (mtd_partitions.next == NULL)
358 INIT_LIST_HEAD(&mtd_partitions);
360 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
362 for (i = 0; i < nbparts; i++) {
364 /* allocate the partition structure */
365 slave = kzalloc (sizeof(*slave), GFP_KERNEL);
367 printk ("memory allocation error while creating partitions for \"%s\"\n",
369 del_mtd_partitions(master);
372 list_add(&slave->list, &mtd_partitions);
374 /* set up the MTD object for this partition */
375 slave->mtd.type = master->type;
376 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
377 slave->mtd.size = parts[i].size;
378 slave->mtd.writesize = master->writesize;
379 slave->mtd.oobsize = master->oobsize;
380 slave->mtd.oobavail = master->oobavail;
381 slave->mtd.subpage_sft = master->subpage_sft;
383 slave->mtd.name = parts[i].name;
384 slave->mtd.owner = master->owner;
386 slave->mtd.read = part_read;
387 slave->mtd.write = part_write;
390 if (master->panic_write)
391 slave->mtd.panic_write = part_panic_write;
393 if(master->point && master->unpoint){
394 slave->mtd.point = part_point;
395 slave->mtd.unpoint = part_unpoint;
399 if (master->read_oob)
400 slave->mtd.read_oob = part_read_oob;
401 if (master->write_oob)
402 slave->mtd.write_oob = part_write_oob;
403 if(master->read_user_prot_reg)
404 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
405 if(master->read_fact_prot_reg)
406 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
407 if(master->write_user_prot_reg)
408 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
409 if(master->lock_user_prot_reg)
410 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
411 if(master->get_user_prot_info)
412 slave->mtd.get_user_prot_info = part_get_user_prot_info;
413 if(master->get_fact_prot_info)
414 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
416 slave->mtd.sync = part_sync;
418 if (!i && master->suspend && master->resume) {
419 slave->mtd.suspend = part_suspend;
420 slave->mtd.resume = part_resume;
423 slave->mtd.writev = part_writev;
425 slave->mtd.lock = part_lock;
427 slave->mtd.unlock = part_unlock;
429 if (master->block_isbad)
430 slave->mtd.block_isbad = part_block_isbad;
431 if (master->block_markbad)
432 slave->mtd.block_markbad = part_block_markbad;
433 slave->mtd.erase = part_erase;
434 slave->master = master;
435 slave->offset = parts[i].offset;
438 if (slave->offset == MTDPART_OFS_APPEND)
439 slave->offset = cur_offset;
440 if (slave->offset == MTDPART_OFS_NXTBLK) {
441 slave->offset = cur_offset;
442 if ((cur_offset % master->erasesize) != 0) {
443 /* Round up to next erasesize */
444 slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
445 printk(KERN_NOTICE "Moving partition %d: "
446 "0x%08x -> 0x%08x\n", i,
447 cur_offset, slave->offset);
450 if (slave->mtd.size == MTDPART_SIZ_FULL)
451 slave->mtd.size = master->size - slave->offset;
452 cur_offset = slave->offset + slave->mtd.size;
454 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
455 slave->offset + slave->mtd.size, slave->mtd.name);
457 /* let's do some sanity checks */
458 if (slave->offset >= master->size) {
459 /* let's register it anyway to preserve ordering */
462 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
465 if (slave->offset + slave->mtd.size > master->size) {
466 slave->mtd.size = master->size - slave->offset;
467 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
468 parts[i].name, master->name, slave->mtd.size);
470 if (master->numeraseregions>1) {
471 /* Deal with variable erase size stuff */
473 struct mtd_erase_region_info *regions = master->eraseregions;
475 /* Find the first erase regions which is part of this partition. */
476 for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
479 for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
480 if (slave->mtd.erasesize < regions[i].erasesize) {
481 slave->mtd.erasesize = regions[i].erasesize;
485 /* Single erase size */
486 slave->mtd.erasesize = master->erasesize;
489 if ((slave->mtd.flags & MTD_WRITEABLE) &&
490 (slave->offset % slave->mtd.erasesize)) {
491 /* Doesn't start on a boundary of major erase size */
492 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
493 slave->mtd.flags &= ~MTD_WRITEABLE;
494 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
497 if ((slave->mtd.flags & MTD_WRITEABLE) &&
498 (slave->mtd.size % slave->mtd.erasesize)) {
499 slave->mtd.flags &= ~MTD_WRITEABLE;
500 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
504 slave->mtd.ecclayout = master->ecclayout;
505 if (master->block_isbad) {
508 while(offs < slave->mtd.size) {
509 if (master->block_isbad(master,
510 offs + slave->offset))
511 slave->mtd.ecc_stats.badblocks++;
512 offs += slave->mtd.erasesize;
518 /* store the object pointer
519 * (caller may or may not register it */
520 *parts[i].mtdp = &slave->mtd;
521 slave->registered = 0;
523 /* register our partition */
524 add_mtd_device(&slave->mtd);
525 slave->registered = 1;
528 /* register our partition */
529 add_mtd_device(&slave->mtd);
530 slave->registered = 1;
538 EXPORT_SYMBOL(add_mtd_partitions);
539 EXPORT_SYMBOL(del_mtd_partitions);