]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/mtd/mtdpart.c
2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
8 * $Id: mtdpart.c,v 1.55 2005/11/07 11:14:20 gleixner Exp $
10 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
11 * added support for read_oob, write_oob
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/list.h>
19 #include <linux/config.h>
20 #include <linux/kmod.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/compatmac.h>
25 /* Our partition linked list */
26 static LIST_HEAD(mtd_partitions
);
28 /* Our partition node structure */
31 struct mtd_info
*master
;
34 struct list_head list
;
39 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
40 * the pointer to that structure with this macro.
42 #define PART(x) ((struct mtd_part *)(x))
46 * MTD methods which simply translate the effective address and pass through
47 * to the _real_ device.
50 static int part_read (struct mtd_info
*mtd
, loff_t from
, size_t len
,
51 size_t *retlen
, u_char
*buf
)
53 struct mtd_part
*part
= PART(mtd
);
54 if (from
>= mtd
->size
)
56 else if (from
+ len
> mtd
->size
)
57 len
= mtd
->size
- from
;
58 if (part
->master
->read_ecc
== NULL
)
59 return part
->master
->read (part
->master
, from
+ part
->offset
,
62 return part
->master
->read_ecc (part
->master
, from
+ part
->offset
,
63 len
, retlen
, buf
, NULL
, &mtd
->oobinfo
);
66 static int part_point (struct mtd_info
*mtd
, loff_t from
, size_t len
,
67 size_t *retlen
, u_char
**buf
)
69 struct mtd_part
*part
= PART(mtd
);
70 if (from
>= mtd
->size
)
72 else if (from
+ len
> mtd
->size
)
73 len
= mtd
->size
- from
;
74 return part
->master
->point (part
->master
, from
+ part
->offset
,
77 static void part_unpoint (struct mtd_info
*mtd
, u_char
*addr
, loff_t from
, size_t len
)
79 struct mtd_part
*part
= PART(mtd
);
81 part
->master
->unpoint (part
->master
, addr
, from
+ part
->offset
, len
);
85 static int part_read_ecc (struct mtd_info
*mtd
, loff_t from
, size_t len
,
86 size_t *retlen
, u_char
*buf
, u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
88 struct mtd_part
*part
= PART(mtd
);
90 oobsel
= &mtd
->oobinfo
;
91 if (from
>= mtd
->size
)
93 else if (from
+ len
> mtd
->size
)
94 len
= mtd
->size
- from
;
95 return part
->master
->read_ecc (part
->master
, from
+ part
->offset
,
96 len
, retlen
, buf
, eccbuf
, oobsel
);
99 static int part_read_oob (struct mtd_info
*mtd
, loff_t from
, size_t len
,
100 size_t *retlen
, u_char
*buf
)
102 struct mtd_part
*part
= PART(mtd
);
103 if (from
>= mtd
->size
)
105 else if (from
+ len
> mtd
->size
)
106 len
= mtd
->size
- from
;
107 return part
->master
->read_oob (part
->master
, from
+ part
->offset
,
111 static int part_read_user_prot_reg (struct mtd_info
*mtd
, loff_t from
, size_t len
,
112 size_t *retlen
, u_char
*buf
)
114 struct mtd_part
*part
= PART(mtd
);
115 return part
->master
->read_user_prot_reg (part
->master
, from
,
119 static int part_get_user_prot_info (struct mtd_info
*mtd
,
120 struct otp_info
*buf
, size_t len
)
122 struct mtd_part
*part
= PART(mtd
);
123 return part
->master
->get_user_prot_info (part
->master
, buf
, len
);
126 static int part_read_fact_prot_reg (struct mtd_info
*mtd
, loff_t from
, size_t len
,
127 size_t *retlen
, u_char
*buf
)
129 struct mtd_part
*part
= PART(mtd
);
130 return part
->master
->read_fact_prot_reg (part
->master
, from
,
134 static int part_get_fact_prot_info (struct mtd_info
*mtd
,
135 struct otp_info
*buf
, size_t len
)
137 struct mtd_part
*part
= PART(mtd
);
138 return part
->master
->get_fact_prot_info (part
->master
, buf
, len
);
141 static int part_write (struct mtd_info
*mtd
, loff_t to
, size_t len
,
142 size_t *retlen
, const u_char
*buf
)
144 struct mtd_part
*part
= PART(mtd
);
145 if (!(mtd
->flags
& MTD_WRITEABLE
))
149 else if (to
+ len
> mtd
->size
)
150 len
= mtd
->size
- to
;
151 if (part
->master
->write_ecc
== NULL
)
152 return part
->master
->write (part
->master
, to
+ part
->offset
,
155 return part
->master
->write_ecc (part
->master
, to
+ part
->offset
,
156 len
, retlen
, buf
, NULL
, &mtd
->oobinfo
);
160 static int part_write_ecc (struct mtd_info
*mtd
, loff_t to
, size_t len
,
161 size_t *retlen
, const u_char
*buf
,
162 u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
164 struct mtd_part
*part
= PART(mtd
);
165 if (!(mtd
->flags
& MTD_WRITEABLE
))
168 oobsel
= &mtd
->oobinfo
;
171 else if (to
+ len
> mtd
->size
)
172 len
= mtd
->size
- to
;
173 return part
->master
->write_ecc (part
->master
, to
+ part
->offset
,
174 len
, retlen
, buf
, eccbuf
, oobsel
);
177 static int part_write_oob (struct mtd_info
*mtd
, loff_t to
, size_t len
,
178 size_t *retlen
, const u_char
*buf
)
180 struct mtd_part
*part
= PART(mtd
);
181 if (!(mtd
->flags
& MTD_WRITEABLE
))
185 else if (to
+ len
> mtd
->size
)
186 len
= mtd
->size
- to
;
187 return part
->master
->write_oob (part
->master
, to
+ part
->offset
,
191 static int part_write_user_prot_reg (struct mtd_info
*mtd
, loff_t from
, size_t len
,
192 size_t *retlen
, u_char
*buf
)
194 struct mtd_part
*part
= PART(mtd
);
195 return part
->master
->write_user_prot_reg (part
->master
, from
,
199 static int part_lock_user_prot_reg (struct mtd_info
*mtd
, loff_t from
, size_t len
)
201 struct mtd_part
*part
= PART(mtd
);
202 return part
->master
->lock_user_prot_reg (part
->master
, from
, len
);
205 static int part_writev (struct mtd_info
*mtd
, const struct kvec
*vecs
,
206 unsigned long count
, loff_t to
, size_t *retlen
)
208 struct mtd_part
*part
= PART(mtd
);
209 if (!(mtd
->flags
& MTD_WRITEABLE
))
211 if (part
->master
->writev_ecc
== NULL
)
212 return part
->master
->writev (part
->master
, vecs
, count
,
213 to
+ part
->offset
, retlen
);
215 return part
->master
->writev_ecc (part
->master
, vecs
, count
,
216 to
+ part
->offset
, retlen
,
217 NULL
, &mtd
->oobinfo
);
220 static int part_readv (struct mtd_info
*mtd
, struct kvec
*vecs
,
221 unsigned long count
, loff_t from
, size_t *retlen
)
223 struct mtd_part
*part
= PART(mtd
);
224 if (part
->master
->readv_ecc
== NULL
)
225 return part
->master
->readv (part
->master
, vecs
, count
,
226 from
+ part
->offset
, retlen
);
228 return part
->master
->readv_ecc (part
->master
, vecs
, count
,
229 from
+ part
->offset
, retlen
,
230 NULL
, &mtd
->oobinfo
);
233 static int part_writev_ecc (struct mtd_info
*mtd
, const struct kvec
*vecs
,
234 unsigned long count
, loff_t to
, size_t *retlen
,
235 u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
237 struct mtd_part
*part
= PART(mtd
);
238 if (!(mtd
->flags
& MTD_WRITEABLE
))
241 oobsel
= &mtd
->oobinfo
;
242 return part
->master
->writev_ecc (part
->master
, vecs
, count
,
243 to
+ part
->offset
, retlen
,
247 static int part_readv_ecc (struct mtd_info
*mtd
, struct kvec
*vecs
,
248 unsigned long count
, loff_t from
, size_t *retlen
,
249 u_char
*eccbuf
, struct nand_oobinfo
*oobsel
)
251 struct mtd_part
*part
= PART(mtd
);
253 oobsel
= &mtd
->oobinfo
;
254 return part
->master
->readv_ecc (part
->master
, vecs
, count
,
255 from
+ part
->offset
, retlen
,
259 static int part_erase (struct mtd_info
*mtd
, struct erase_info
*instr
)
261 struct mtd_part
*part
= PART(mtd
);
263 if (!(mtd
->flags
& MTD_WRITEABLE
))
265 if (instr
->addr
>= mtd
->size
)
267 instr
->addr
+= part
->offset
;
268 ret
= part
->master
->erase(part
->master
, instr
);
272 void mtd_erase_callback(struct erase_info
*instr
)
274 if (instr
->mtd
->erase
== part_erase
) {
275 struct mtd_part
*part
= PART(instr
->mtd
);
277 if (instr
->fail_addr
!= 0xffffffff)
278 instr
->fail_addr
-= part
->offset
;
279 instr
->addr
-= part
->offset
;
282 instr
->callback(instr
);
284 EXPORT_SYMBOL_GPL(mtd_erase_callback
);
286 static int part_lock (struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
288 struct mtd_part
*part
= PART(mtd
);
289 if ((len
+ ofs
) > mtd
->size
)
291 return part
->master
->lock(part
->master
, ofs
+ part
->offset
, len
);
294 static int part_unlock (struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
296 struct mtd_part
*part
= PART(mtd
);
297 if ((len
+ ofs
) > mtd
->size
)
299 return part
->master
->unlock(part
->master
, ofs
+ part
->offset
, len
);
302 static void part_sync(struct mtd_info
*mtd
)
304 struct mtd_part
*part
= PART(mtd
);
305 part
->master
->sync(part
->master
);
308 static int part_suspend(struct mtd_info
*mtd
)
310 struct mtd_part
*part
= PART(mtd
);
311 return part
->master
->suspend(part
->master
);
314 static void part_resume(struct mtd_info
*mtd
)
316 struct mtd_part
*part
= PART(mtd
);
317 part
->master
->resume(part
->master
);
320 static int part_block_isbad (struct mtd_info
*mtd
, loff_t ofs
)
322 struct mtd_part
*part
= PART(mtd
);
323 if (ofs
>= mtd
->size
)
326 return part
->master
->block_isbad(part
->master
, ofs
);
329 static int part_block_markbad (struct mtd_info
*mtd
, loff_t ofs
)
331 struct mtd_part
*part
= PART(mtd
);
332 if (!(mtd
->flags
& MTD_WRITEABLE
))
334 if (ofs
>= mtd
->size
)
337 return part
->master
->block_markbad(part
->master
, ofs
);
341 * This function unregisters and destroy all slave MTD objects which are
342 * attached to the given master MTD object.
345 int del_mtd_partitions(struct mtd_info
*master
)
347 struct list_head
*node
;
348 struct mtd_part
*slave
;
350 for (node
= mtd_partitions
.next
;
351 node
!= &mtd_partitions
;
353 slave
= list_entry(node
, struct mtd_part
, list
);
354 if (slave
->master
== master
) {
355 struct list_head
*prev
= node
->prev
;
356 __list_del(prev
, node
->next
);
357 if(slave
->registered
)
358 del_mtd_device(&slave
->mtd
);
368 * This function, given a master MTD object and a partition table, creates
369 * and registers slave MTD objects which are bound to the master according to
370 * the partition definitions.
371 * (Q: should we register the master MTD object as well?)
374 int add_mtd_partitions(struct mtd_info
*master
,
375 const struct mtd_partition
*parts
,
378 struct mtd_part
*slave
;
379 u_int32_t cur_offset
= 0;
382 printk (KERN_NOTICE
"Creating %d MTD partitions on \"%s\":\n", nbparts
, master
->name
);
384 for (i
= 0; i
< nbparts
; i
++) {
386 /* allocate the partition structure */
387 slave
= kmalloc (sizeof(*slave
), GFP_KERNEL
);
389 printk ("memory allocation error while creating partitions for \"%s\"\n",
391 del_mtd_partitions(master
);
394 memset(slave
, 0, sizeof(*slave
));
395 list_add(&slave
->list
, &mtd_partitions
);
397 /* set up the MTD object for this partition */
398 slave
->mtd
.type
= master
->type
;
399 slave
->mtd
.flags
= master
->flags
& ~parts
[i
].mask_flags
;
400 slave
->mtd
.size
= parts
[i
].size
;
401 slave
->mtd
.oobblock
= master
->oobblock
;
402 slave
->mtd
.oobsize
= master
->oobsize
;
403 slave
->mtd
.oobavail
= master
->oobavail
;
404 slave
->mtd
.ecctype
= master
->ecctype
;
405 slave
->mtd
.eccsize
= master
->eccsize
;
407 slave
->mtd
.name
= parts
[i
].name
;
408 slave
->mtd
.bank_size
= master
->bank_size
;
409 slave
->mtd
.owner
= master
->owner
;
411 slave
->mtd
.read
= part_read
;
412 slave
->mtd
.write
= part_write
;
414 if(master
->point
&& master
->unpoint
){
415 slave
->mtd
.point
= part_point
;
416 slave
->mtd
.unpoint
= part_unpoint
;
419 if (master
->read_ecc
)
420 slave
->mtd
.read_ecc
= part_read_ecc
;
421 if (master
->write_ecc
)
422 slave
->mtd
.write_ecc
= part_write_ecc
;
423 if (master
->read_oob
)
424 slave
->mtd
.read_oob
= part_read_oob
;
425 if (master
->write_oob
)
426 slave
->mtd
.write_oob
= part_write_oob
;
427 if(master
->read_user_prot_reg
)
428 slave
->mtd
.read_user_prot_reg
= part_read_user_prot_reg
;
429 if(master
->read_fact_prot_reg
)
430 slave
->mtd
.read_fact_prot_reg
= part_read_fact_prot_reg
;
431 if(master
->write_user_prot_reg
)
432 slave
->mtd
.write_user_prot_reg
= part_write_user_prot_reg
;
433 if(master
->lock_user_prot_reg
)
434 slave
->mtd
.lock_user_prot_reg
= part_lock_user_prot_reg
;
435 if(master
->get_user_prot_info
)
436 slave
->mtd
.get_user_prot_info
= part_get_user_prot_info
;
437 if(master
->get_fact_prot_info
)
438 slave
->mtd
.get_fact_prot_info
= part_get_fact_prot_info
;
440 slave
->mtd
.sync
= part_sync
;
441 if (!i
&& master
->suspend
&& master
->resume
) {
442 slave
->mtd
.suspend
= part_suspend
;
443 slave
->mtd
.resume
= part_resume
;
446 slave
->mtd
.writev
= part_writev
;
448 slave
->mtd
.readv
= part_readv
;
449 if (master
->writev_ecc
)
450 slave
->mtd
.writev_ecc
= part_writev_ecc
;
451 if (master
->readv_ecc
)
452 slave
->mtd
.readv_ecc
= part_readv_ecc
;
454 slave
->mtd
.lock
= part_lock
;
456 slave
->mtd
.unlock
= part_unlock
;
457 if (master
->block_isbad
)
458 slave
->mtd
.block_isbad
= part_block_isbad
;
459 if (master
->block_markbad
)
460 slave
->mtd
.block_markbad
= part_block_markbad
;
461 slave
->mtd
.erase
= part_erase
;
462 slave
->master
= master
;
463 slave
->offset
= parts
[i
].offset
;
466 if (slave
->offset
== MTDPART_OFS_APPEND
)
467 slave
->offset
= cur_offset
;
468 if (slave
->offset
== MTDPART_OFS_NXTBLK
) {
469 slave
->offset
= cur_offset
;
470 if ((cur_offset
% master
->erasesize
) != 0) {
471 /* Round up to next erasesize */
472 slave
->offset
= ((cur_offset
/ master
->erasesize
) + 1) * master
->erasesize
;
473 printk(KERN_NOTICE
"Moving partition %d: "
474 "0x%08x -> 0x%08x\n", i
,
475 cur_offset
, slave
->offset
);
478 if (slave
->mtd
.size
== MTDPART_SIZ_FULL
)
479 slave
->mtd
.size
= master
->size
- slave
->offset
;
480 cur_offset
= slave
->offset
+ slave
->mtd
.size
;
482 printk (KERN_NOTICE
"0x%08x-0x%08x : \"%s\"\n", slave
->offset
,
483 slave
->offset
+ slave
->mtd
.size
, slave
->mtd
.name
);
485 /* let's do some sanity checks */
486 if (slave
->offset
>= master
->size
) {
487 /* let's register it anyway to preserve ordering */
490 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
493 if (slave
->offset
+ slave
->mtd
.size
> master
->size
) {
494 slave
->mtd
.size
= master
->size
- slave
->offset
;
495 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
496 parts
[i
].name
, master
->name
, slave
->mtd
.size
);
498 if (master
->numeraseregions
>1) {
499 /* Deal with variable erase size stuff */
501 struct mtd_erase_region_info
*regions
= master
->eraseregions
;
503 /* Find the first erase regions which is part of this partition. */
504 for (i
=0; i
< master
->numeraseregions
&& slave
->offset
>= regions
[i
].offset
; i
++)
507 for (i
--; i
< master
->numeraseregions
&& slave
->offset
+ slave
->mtd
.size
> regions
[i
].offset
; i
++) {
508 if (slave
->mtd
.erasesize
< regions
[i
].erasesize
) {
509 slave
->mtd
.erasesize
= regions
[i
].erasesize
;
513 /* Single erase size */
514 slave
->mtd
.erasesize
= master
->erasesize
;
517 if ((slave
->mtd
.flags
& MTD_WRITEABLE
) &&
518 (slave
->offset
% slave
->mtd
.erasesize
)) {
519 /* Doesn't start on a boundary of major erase size */
520 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
521 slave
->mtd
.flags
&= ~MTD_WRITEABLE
;
522 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
525 if ((slave
->mtd
.flags
& MTD_WRITEABLE
) &&
526 (slave
->mtd
.size
% slave
->mtd
.erasesize
)) {
527 slave
->mtd
.flags
&= ~MTD_WRITEABLE
;
528 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
532 /* copy oobinfo from master */
533 memcpy(&slave
->mtd
.oobinfo
, &master
->oobinfo
, sizeof(slave
->mtd
.oobinfo
));
536 { /* store the object pointer (caller may or may not register it */
537 *parts
[i
].mtdp
= &slave
->mtd
;
538 slave
->registered
= 0;
542 /* register our partition */
543 add_mtd_device(&slave
->mtd
);
544 slave
->registered
= 1;
551 EXPORT_SYMBOL(add_mtd_partitions
);
552 EXPORT_SYMBOL(del_mtd_partitions
);
554 static DEFINE_SPINLOCK(part_parser_lock
);
555 static LIST_HEAD(part_parsers
);
557 static struct mtd_part_parser
*get_partition_parser(const char *name
)
559 struct list_head
*this;
561 spin_lock(&part_parser_lock
);
563 list_for_each(this, &part_parsers
) {
564 struct mtd_part_parser
*p
= list_entry(this, struct mtd_part_parser
, list
);
566 if (!strcmp(p
->name
, name
) && try_module_get(p
->owner
)) {
571 spin_unlock(&part_parser_lock
);
576 int register_mtd_parser(struct mtd_part_parser
*p
)
578 spin_lock(&part_parser_lock
);
579 list_add(&p
->list
, &part_parsers
);
580 spin_unlock(&part_parser_lock
);
585 int deregister_mtd_parser(struct mtd_part_parser
*p
)
587 spin_lock(&part_parser_lock
);
589 spin_unlock(&part_parser_lock
);
593 int parse_mtd_partitions(struct mtd_info
*master
, const char **types
,
594 struct mtd_partition
**pparts
, unsigned long origin
)
596 struct mtd_part_parser
*parser
;
599 for ( ; ret
<= 0 && *types
; types
++) {
600 parser
= get_partition_parser(*types
);
602 if (!parser
&& !request_module("%s", *types
))
603 parser
= get_partition_parser(*types
);
606 printk(KERN_NOTICE
"%s partition parsing not available\n",
610 ret
= (*parser
->parse_fn
)(master
, pparts
, origin
);
612 printk(KERN_NOTICE
"%d %s partitions found on MTD device %s\n",
613 ret
, parser
->name
, master
->name
);
615 put_partition_parser(parser
);
620 EXPORT_SYMBOL_GPL(parse_mtd_partitions
);
621 EXPORT_SYMBOL_GPL(register_mtd_parser
);
622 EXPORT_SYMBOL_GPL(deregister_mtd_parser
);
624 MODULE_LICENSE("GPL");
625 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org>");
626 MODULE_DESCRIPTION("Generic support for partitioning of MTD devices");