2 * MTD device concatenation layer
4 * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
6 * NAND support by Christian Gan <cgan@iders.ca>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/compat.h>
13 #include <linux/mtd/concat.h>
14 #include <ubi_uboot.h>
17 * Our storage structure:
18 * Subdev points to an array of pointers to struct mtd_info objects
19 * which is allocated along with this structure
25 struct mtd_info **subdev;
29 * how to calculate the size required for the above structure,
30 * including the pointer array subdev points to:
32 #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
33 ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
36 * Given a pointer to the MTD object in the mtd_concat structure,
37 * we can retrieve the pointer to that structure with this macro.
39 #define CONCAT(x) ((struct mtd_concat *)(x))
42 * MTD methods which look up the relevant subdevice, translate the
43 * effective address and pass through to the subdevice.
47 concat_read(struct mtd_info *mtd, loff_t from, size_t len,
48 size_t * retlen, u_char * buf)
50 struct mtd_concat *concat = CONCAT(mtd);
56 for (i = 0; i < concat->num_subdev; i++) {
57 struct mtd_info *subdev = concat->subdev[i];
60 if (from >= subdev->size) {
61 /* Not destined for this subdev */
66 if (from + len > subdev->size)
67 /* First part goes into this subdev */
68 size = subdev->size - from;
70 /* Entire transaction goes into this subdev */
73 err = subdev->read(subdev, from, size, &retsize, buf);
75 /* Save information about bitflips! */
77 if (err == -EBADMSG) {
78 mtd->ecc_stats.failed++;
80 } else if (err == -EUCLEAN) {
81 mtd->ecc_stats.corrected++;
82 /* Do not overwrite -EBADMSG !! */
101 concat_write(struct mtd_info *mtd, loff_t to, size_t len,
102 size_t * retlen, const u_char * buf)
104 struct mtd_concat *concat = CONCAT(mtd);
108 if (!(mtd->flags & MTD_WRITEABLE))
113 for (i = 0; i < concat->num_subdev; i++) {
114 struct mtd_info *subdev = concat->subdev[i];
115 size_t size, retsize;
117 if (to >= subdev->size) {
122 if (to + len > subdev->size)
123 size = subdev->size - to;
127 if (!(subdev->flags & MTD_WRITEABLE))
130 err = subdev->write(subdev, to, size, &retsize, buf);
148 concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
150 struct mtd_concat *concat = CONCAT(mtd);
151 struct mtd_oob_ops devops = *ops;
154 ops->retlen = ops->oobretlen = 0;
156 for (i = 0; i < concat->num_subdev; i++) {
157 struct mtd_info *subdev = concat->subdev[i];
159 if (from >= subdev->size) {
160 from -= subdev->size;
165 if (from + devops.len > subdev->size)
166 devops.len = subdev->size - from;
168 err = subdev->read_oob(subdev, from, &devops);
169 ops->retlen += devops.retlen;
170 ops->oobretlen += devops.oobretlen;
172 /* Save information about bitflips! */
174 if (err == -EBADMSG) {
175 mtd->ecc_stats.failed++;
177 } else if (err == -EUCLEAN) {
178 mtd->ecc_stats.corrected++;
179 /* Do not overwrite -EBADMSG !! */
187 devops.len = ops->len - ops->retlen;
190 devops.datbuf += devops.retlen;
193 devops.ooblen = ops->ooblen - ops->oobretlen;
196 devops.oobbuf += ops->oobretlen;
205 concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
207 struct mtd_concat *concat = CONCAT(mtd);
208 struct mtd_oob_ops devops = *ops;
211 if (!(mtd->flags & MTD_WRITEABLE))
216 for (i = 0; i < concat->num_subdev; i++) {
217 struct mtd_info *subdev = concat->subdev[i];
219 if (to >= subdev->size) {
224 /* partial write ? */
225 if (to + devops.len > subdev->size)
226 devops.len = subdev->size - to;
228 err = subdev->write_oob(subdev, to, &devops);
229 ops->retlen += devops.retlen;
234 devops.len = ops->len - ops->retlen;
237 devops.datbuf += devops.retlen;
240 devops.ooblen = ops->ooblen - ops->oobretlen;
243 devops.oobbuf += devops.oobretlen;
250 static void concat_erase_callback(struct erase_info *instr)
252 /* Nothing to do here in U-Boot */
255 static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
258 wait_queue_head_t waitq;
259 DECLARE_WAITQUEUE(wait, current);
262 * This code was stol^H^H^H^Hinspired by mtdchar.c
264 init_waitqueue_head(&waitq);
267 erase->callback = concat_erase_callback;
268 erase->priv = (unsigned long) &waitq;
271 * FIXME: Allow INTERRUPTIBLE. Which means
272 * not having the wait_queue head on the stack.
274 err = mtd->erase(mtd, erase);
276 set_current_state(TASK_UNINTERRUPTIBLE);
277 add_wait_queue(&waitq, &wait);
278 if (erase->state != MTD_ERASE_DONE
279 && erase->state != MTD_ERASE_FAILED)
281 remove_wait_queue(&waitq, &wait);
282 set_current_state(TASK_RUNNING);
284 err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
289 static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
291 struct mtd_concat *concat = CONCAT(mtd);
292 struct mtd_info *subdev;
294 uint64_t length, offset = 0;
295 struct erase_info *erase;
297 if (!(mtd->flags & MTD_WRITEABLE))
300 if (instr->addr > concat->mtd.size)
303 if (instr->len + instr->addr > concat->mtd.size)
307 * Check for proper erase block alignment of the to-be-erased area.
308 * It is easier to do this based on the super device's erase
309 * region info rather than looking at each particular sub-device
312 if (!concat->mtd.numeraseregions) {
313 /* the easy case: device has uniform erase block size */
314 if (instr->addr & (concat->mtd.erasesize - 1))
316 if (instr->len & (concat->mtd.erasesize - 1))
319 /* device has variable erase size */
320 struct mtd_erase_region_info *erase_regions =
321 concat->mtd.eraseregions;
324 * Find the erase region where the to-be-erased area begins:
326 for (i = 0; i < concat->mtd.numeraseregions &&
327 instr->addr >= erase_regions[i].offset; i++) ;
331 * Now erase_regions[i] is the region in which the
332 * to-be-erased area begins. Verify that the starting
333 * offset is aligned to this region's erase size:
335 if (instr->addr & (erase_regions[i].erasesize - 1))
339 * now find the erase region where the to-be-erased area ends:
341 for (; i < concat->mtd.numeraseregions &&
342 (instr->addr + instr->len) >= erase_regions[i].offset;
346 * check if the ending offset is aligned to this region's erase size
348 if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
353 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
355 /* make a local copy of instr to avoid modifying the caller's struct */
356 erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
365 * find the subdevice where the to-be-erased area begins, adjust
366 * starting offset to be relative to the subdevice start
368 for (i = 0; i < concat->num_subdev; i++) {
369 subdev = concat->subdev[i];
370 if (subdev->size <= erase->addr) {
371 erase->addr -= subdev->size;
372 offset += subdev->size;
378 /* must never happen since size limit has been verified above */
379 BUG_ON(i >= concat->num_subdev);
381 /* now do the erase: */
383 for (; length > 0; i++) {
384 /* loop for all subdevices affected by this request */
385 subdev = concat->subdev[i]; /* get current subdevice */
387 /* limit length to subdevice's size: */
388 if (erase->addr + length > subdev->size)
389 erase->len = subdev->size - erase->addr;
393 if (!(subdev->flags & MTD_WRITEABLE)) {
397 length -= erase->len;
398 if ((err = concat_dev_erase(subdev, erase))) {
399 /* sanity check: should never happen since
400 * block alignment has been checked above */
401 BUG_ON(err == -EINVAL);
402 if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
403 instr->fail_addr = erase->fail_addr + offset;
407 * erase->addr specifies the offset of the area to be
408 * erased *within the current subdevice*. It can be
409 * non-zero only the first time through this loop, i.e.
410 * for the first subdevice where blocks need to be erased.
411 * All the following erases must begin at the start of the
412 * current subdevice, i.e. at offset zero.
415 offset += subdev->size;
417 instr->state = erase->state;
423 instr->callback(instr);
427 static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
429 struct mtd_concat *concat = CONCAT(mtd);
430 int i, err = -EINVAL;
432 if ((len + ofs) > mtd->size)
435 for (i = 0; i < concat->num_subdev; i++) {
436 struct mtd_info *subdev = concat->subdev[i];
439 if (ofs >= subdev->size) {
444 if (ofs + len > subdev->size)
445 size = subdev->size - ofs;
449 err = subdev->lock(subdev, ofs, size);
465 static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
467 struct mtd_concat *concat = CONCAT(mtd);
470 if ((len + ofs) > mtd->size)
473 for (i = 0; i < concat->num_subdev; i++) {
474 struct mtd_info *subdev = concat->subdev[i];
477 if (ofs >= subdev->size) {
482 if (ofs + len > subdev->size)
483 size = subdev->size - ofs;
487 err = subdev->unlock(subdev, ofs, size);
503 static void concat_sync(struct mtd_info *mtd)
505 struct mtd_concat *concat = CONCAT(mtd);
508 for (i = 0; i < concat->num_subdev; i++) {
509 struct mtd_info *subdev = concat->subdev[i];
510 subdev->sync(subdev);
514 static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
516 struct mtd_concat *concat = CONCAT(mtd);
519 if (!concat->subdev[0]->block_isbad)
525 for (i = 0; i < concat->num_subdev; i++) {
526 struct mtd_info *subdev = concat->subdev[i];
528 if (ofs >= subdev->size) {
533 res = subdev->block_isbad(subdev, ofs);
540 static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
542 struct mtd_concat *concat = CONCAT(mtd);
543 int i, err = -EINVAL;
545 if (!concat->subdev[0]->block_markbad)
551 for (i = 0; i < concat->num_subdev; i++) {
552 struct mtd_info *subdev = concat->subdev[i];
554 if (ofs >= subdev->size) {
559 err = subdev->block_markbad(subdev, ofs);
561 mtd->ecc_stats.badblocks++;
569 * This function constructs a virtual MTD device by concatenating
570 * num_devs MTD devices. A pointer to the new device object is
571 * stored to *new_dev upon success. This function does _not_
572 * register any devices: this is the caller's responsibility.
574 struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
575 int num_devs, /* number of subdevices */
577 { /* name for the new device */
580 struct mtd_concat *concat;
581 uint32_t max_erasesize, curr_erasesize;
582 int num_erase_region;
584 debug("Concatenating MTD devices:\n");
585 for (i = 0; i < num_devs; i++)
586 debug("(%d): \"%s\"\n", i, subdev[i]->name);
587 debug("into device \"%s\"\n", name);
589 /* allocate the device structure */
590 size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
591 concat = kzalloc(size, GFP_KERNEL);
594 ("memory allocation error while creating concatenated device \"%s\"\n",
598 concat->subdev = (struct mtd_info **) (concat + 1);
601 * Set up the new "super" device's MTD object structure, check for
602 * incompatibilites between the subdevices.
604 concat->mtd.type = subdev[0]->type;
605 concat->mtd.flags = subdev[0]->flags;
606 concat->mtd.size = subdev[0]->size;
607 concat->mtd.erasesize = subdev[0]->erasesize;
608 concat->mtd.writesize = subdev[0]->writesize;
609 concat->mtd.subpage_sft = subdev[0]->subpage_sft;
610 concat->mtd.oobsize = subdev[0]->oobsize;
611 concat->mtd.oobavail = subdev[0]->oobavail;
612 if (subdev[0]->read_oob)
613 concat->mtd.read_oob = concat_read_oob;
614 if (subdev[0]->write_oob)
615 concat->mtd.write_oob = concat_write_oob;
616 if (subdev[0]->block_isbad)
617 concat->mtd.block_isbad = concat_block_isbad;
618 if (subdev[0]->block_markbad)
619 concat->mtd.block_markbad = concat_block_markbad;
621 concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
623 concat->subdev[0] = subdev[0];
625 for (i = 1; i < num_devs; i++) {
626 if (concat->mtd.type != subdev[i]->type) {
628 printk("Incompatible device type on \"%s\"\n",
632 if (concat->mtd.flags != subdev[i]->flags) {
634 * Expect all flags except MTD_WRITEABLE to be
635 * equal on all subdevices.
637 if ((concat->mtd.flags ^ subdev[i]->
638 flags) & ~MTD_WRITEABLE) {
640 printk("Incompatible device flags on \"%s\"\n",
644 /* if writeable attribute differs,
645 make super device writeable */
647 subdev[i]->flags & MTD_WRITEABLE;
650 concat->mtd.size += subdev[i]->size;
651 concat->mtd.ecc_stats.badblocks +=
652 subdev[i]->ecc_stats.badblocks;
653 if (concat->mtd.writesize != subdev[i]->writesize ||
654 concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
655 concat->mtd.oobsize != subdev[i]->oobsize ||
656 !concat->mtd.read_oob != !subdev[i]->read_oob ||
657 !concat->mtd.write_oob != !subdev[i]->write_oob) {
659 printk("Incompatible OOB or ECC data on \"%s\"\n",
663 concat->subdev[i] = subdev[i];
667 concat->mtd.ecclayout = subdev[0]->ecclayout;
669 concat->num_subdev = num_devs;
670 concat->mtd.name = name;
672 concat->mtd.erase = concat_erase;
673 concat->mtd.read = concat_read;
674 concat->mtd.write = concat_write;
675 concat->mtd.sync = concat_sync;
676 concat->mtd.lock = concat_lock;
677 concat->mtd.unlock = concat_unlock;
680 * Combine the erase block size info of the subdevices:
682 * first, walk the map of the new device and see how
683 * many changes in erase size we have
685 max_erasesize = curr_erasesize = subdev[0]->erasesize;
686 num_erase_region = 1;
687 for (i = 0; i < num_devs; i++) {
688 if (subdev[i]->numeraseregions == 0) {
689 /* current subdevice has uniform erase size */
690 if (subdev[i]->erasesize != curr_erasesize) {
691 /* if it differs from the last subdevice's erase size, count it */
693 curr_erasesize = subdev[i]->erasesize;
694 if (curr_erasesize > max_erasesize)
695 max_erasesize = curr_erasesize;
698 /* current subdevice has variable erase size */
700 for (j = 0; j < subdev[i]->numeraseregions; j++) {
702 /* walk the list of erase regions, count any changes */
703 if (subdev[i]->eraseregions[j].erasesize !=
707 subdev[i]->eraseregions[j].
709 if (curr_erasesize > max_erasesize)
710 max_erasesize = curr_erasesize;
716 if (num_erase_region == 1) {
718 * All subdevices have the same uniform erase size.
721 concat->mtd.erasesize = curr_erasesize;
722 concat->mtd.numeraseregions = 0;
727 * erase block size varies across the subdevices: allocate
728 * space to store the data describing the variable erase regions
730 struct mtd_erase_region_info *erase_region_p;
731 uint64_t begin, position;
733 concat->mtd.erasesize = max_erasesize;
734 concat->mtd.numeraseregions = num_erase_region;
735 concat->mtd.eraseregions = erase_region_p =
736 kmalloc(num_erase_region *
737 sizeof (struct mtd_erase_region_info), GFP_KERNEL);
738 if (!erase_region_p) {
741 ("memory allocation error while creating erase region list"
742 " for device \"%s\"\n", name);
747 * walk the map of the new device once more and fill in
748 * in erase region info:
750 curr_erasesize = subdev[0]->erasesize;
751 begin = position = 0;
752 for (i = 0; i < num_devs; i++) {
753 if (subdev[i]->numeraseregions == 0) {
754 /* current subdevice has uniform erase size */
755 if (subdev[i]->erasesize != curr_erasesize) {
757 * fill in an mtd_erase_region_info structure for the area
758 * we have walked so far:
760 erase_region_p->offset = begin;
761 erase_region_p->erasesize =
763 tmp64 = position - begin;
764 do_div(tmp64, curr_erasesize);
765 erase_region_p->numblocks = tmp64;
768 curr_erasesize = subdev[i]->erasesize;
771 position += subdev[i]->size;
773 /* current subdevice has variable erase size */
775 for (j = 0; j < subdev[i]->numeraseregions; j++) {
776 /* walk the list of erase regions, count any changes */
777 if (subdev[i]->eraseregions[j].
778 erasesize != curr_erasesize) {
779 erase_region_p->offset = begin;
780 erase_region_p->erasesize =
782 tmp64 = position - begin;
783 do_div(tmp64, curr_erasesize);
784 erase_region_p->numblocks = tmp64;
788 subdev[i]->eraseregions[j].
793 subdev[i]->eraseregions[j].
794 numblocks * (uint64_t)curr_erasesize;
798 /* Now write the final entry */
799 erase_region_p->offset = begin;
800 erase_region_p->erasesize = curr_erasesize;
801 tmp64 = position - begin;
802 do_div(tmp64, curr_erasesize);
803 erase_region_p->numblocks = tmp64;