blob: f010f5e3ac5cdf1363093608eab9e61c4e3ccd40 [file] [log] [blame]
Kyungmin Parke29c22f2008-11-19 16:20:36 +01001/*
2 * Simple MTD partitioning layer
3 *
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
5 *
6 * This code is GPL
7 *
8 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
9 * added support for read_oob, write_oob
10 */
11
12#include <common.h>
13#include <malloc.h>
14#include <asm/errno.h>
15
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>
21
22/* Our partition linked list */
Stefan Roese9def12c2008-11-27 14:05:15 +010023struct list_head mtd_partitions;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010024
25/* Our partition node structure */
26struct mtd_part {
27 struct mtd_info mtd;
28 struct mtd_info *master;
29 u_int32_t offset;
30 int index;
31 struct list_head list;
32 int registered;
33};
34
35/*
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.
38 */
39#define PART(x) ((struct mtd_part *)(x))
40
41
42/*
43 * MTD methods which simply translate the effective address and pass through
44 * to the _real_ device.
45 */
46
47static int part_read (struct mtd_info *mtd, loff_t from, size_t len,
48 size_t *retlen, u_char *buf)
49{
50 struct mtd_part *part = PART(mtd);
51 int res;
52
53 if (from >= mtd->size)
54 len = 0;
55 else if (from + len > mtd->size)
56 len = mtd->size - from;
57 res = part->master->read (part->master, from + part->offset,
58 len, retlen, buf);
59 if (unlikely(res)) {
60 if (res == -EUCLEAN)
61 mtd->ecc_stats.corrected++;
62 if (res == -EBADMSG)
63 mtd->ecc_stats.failed++;
64 }
65 return res;
66}
67
68#ifdef MTD_LINUX
69static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
70 size_t *retlen, void **virt, resource_size_t *phys)
71{
72 struct mtd_part *part = PART(mtd);
73 if (from >= mtd->size)
74 len = 0;
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);
79}
80
81static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
82{
83 struct mtd_part *part = PART(mtd);
84
85 part->master->unpoint(part->master, from + part->offset, len);
86}
87#endif
88
89static int part_read_oob(struct mtd_info *mtd, loff_t from,
90 struct mtd_oob_ops *ops)
91{
92 struct mtd_part *part = PART(mtd);
93 int res;
94
95 if (from >= mtd->size)
96 return -EINVAL;
97 if (ops->datbuf && from + ops->len > mtd->size)
98 return -EINVAL;
99 res = part->master->read_oob(part->master, from + part->offset, ops);
100
101 if (unlikely(res)) {
102 if (res == -EUCLEAN)
103 mtd->ecc_stats.corrected++;
104 if (res == -EBADMSG)
105 mtd->ecc_stats.failed++;
106 }
107 return res;
108}
109
110static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
111 size_t *retlen, u_char *buf)
112{
113 struct mtd_part *part = PART(mtd);
114 return part->master->read_user_prot_reg (part->master, from,
115 len, retlen, buf);
116}
117
118static int part_get_user_prot_info (struct mtd_info *mtd,
119 struct otp_info *buf, size_t len)
120{
121 struct mtd_part *part = PART(mtd);
122 return part->master->get_user_prot_info (part->master, buf, len);
123}
124
125static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
126 size_t *retlen, u_char *buf)
127{
128 struct mtd_part *part = PART(mtd);
129 return part->master->read_fact_prot_reg (part->master, from,
130 len, retlen, buf);
131}
132
133static int part_get_fact_prot_info (struct mtd_info *mtd,
134 struct otp_info *buf, size_t len)
135{
136 struct mtd_part *part = PART(mtd);
137 return part->master->get_fact_prot_info (part->master, buf, len);
138}
139
140static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
141 size_t *retlen, const u_char *buf)
142{
143 struct mtd_part *part = PART(mtd);
144 if (!(mtd->flags & MTD_WRITEABLE))
145 return -EROFS;
146 if (to >= mtd->size)
147 len = 0;
148 else if (to + len > mtd->size)
149 len = mtd->size - to;
150 return part->master->write (part->master, to + part->offset,
151 len, retlen, buf);
152}
153
154#ifdef MTD_LINUX
155static int part_panic_write (struct mtd_info *mtd, loff_t to, size_t len,
156 size_t *retlen, const u_char *buf)
157{
158 struct mtd_part *part = PART(mtd);
159 if (!(mtd->flags & MTD_WRITEABLE))
160 return -EROFS;
161 if (to >= mtd->size)
162 len = 0;
163 else if (to + len > mtd->size)
164 len = mtd->size - to;
165 return part->master->panic_write (part->master, to + part->offset,
166 len, retlen, buf);
167}
168#endif
169
170static int part_write_oob(struct mtd_info *mtd, loff_t to,
171 struct mtd_oob_ops *ops)
172{
173 struct mtd_part *part = PART(mtd);
174
175 if (!(mtd->flags & MTD_WRITEABLE))
176 return -EROFS;
177
178 if (to >= mtd->size)
179 return -EINVAL;
180 if (ops->datbuf && to + ops->len > mtd->size)
181 return -EINVAL;
182 return part->master->write_oob(part->master, to + part->offset, ops);
183}
184
185static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
186 size_t *retlen, u_char *buf)
187{
188 struct mtd_part *part = PART(mtd);
189 return part->master->write_user_prot_reg (part->master, from,
190 len, retlen, buf);
191}
192
193static int part_lock_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len)
194{
195 struct mtd_part *part = PART(mtd);
196 return part->master->lock_user_prot_reg (part->master, from, len);
197}
198
199#ifdef MTD_LINUX
200static int part_writev (struct mtd_info *mtd, const struct kvec *vecs,
201 unsigned long count, loff_t to, size_t *retlen)
202{
203 struct mtd_part *part = PART(mtd);
204 if (!(mtd->flags & MTD_WRITEABLE))
205 return -EROFS;
206 return part->master->writev (part->master, vecs, count,
207 to + part->offset, retlen);
208}
209#endif
210
211static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
212{
213 struct mtd_part *part = PART(mtd);
214 int ret;
215 if (!(mtd->flags & MTD_WRITEABLE))
216 return -EROFS;
217 if (instr->addr >= mtd->size)
218 return -EINVAL;
219 instr->addr += part->offset;
220 ret = part->master->erase(part->master, instr);
221 if (ret) {
222 if (instr->fail_addr != 0xffffffff)
223 instr->fail_addr -= part->offset;
224 instr->addr -= part->offset;
225 }
226 return ret;
227}
228
229void mtd_erase_callback(struct erase_info *instr)
230{
231 if (instr->mtd->erase == part_erase) {
232 struct mtd_part *part = PART(instr->mtd);
233
234 if (instr->fail_addr != 0xffffffff)
235 instr->fail_addr -= part->offset;
236 instr->addr -= part->offset;
237 }
238 if (instr->callback)
239 instr->callback(instr);
240}
241#ifdef MTD_LINUX
242EXPORT_SYMBOL_GPL(mtd_erase_callback);
243#endif
244
245#ifdef MTD_LINUX
246static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
247{
248 struct mtd_part *part = PART(mtd);
249 if ((len + ofs) > mtd->size)
250 return -EINVAL;
251 return part->master->lock(part->master, ofs + part->offset, len);
252}
253
254static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
255{
256 struct mtd_part *part = PART(mtd);
257 if ((len + ofs) > mtd->size)
258 return -EINVAL;
259 return part->master->unlock(part->master, ofs + part->offset, len);
260}
261#endif
262
263static void part_sync(struct mtd_info *mtd)
264{
265 struct mtd_part *part = PART(mtd);
266 part->master->sync(part->master);
267}
268
269#ifdef MTD_LINUX
270static int part_suspend(struct mtd_info *mtd)
271{
272 struct mtd_part *part = PART(mtd);
273 return part->master->suspend(part->master);
274}
275
276static void part_resume(struct mtd_info *mtd)
277{
278 struct mtd_part *part = PART(mtd);
279 part->master->resume(part->master);
280}
281#endif
282
283static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
284{
285 struct mtd_part *part = PART(mtd);
286 if (ofs >= mtd->size)
287 return -EINVAL;
288 ofs += part->offset;
289 return part->master->block_isbad(part->master, ofs);
290}
291
292static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
293{
294 struct mtd_part *part = PART(mtd);
295 int res;
296
297 if (!(mtd->flags & MTD_WRITEABLE))
298 return -EROFS;
299 if (ofs >= mtd->size)
300 return -EINVAL;
301 ofs += part->offset;
302 res = part->master->block_markbad(part->master, ofs);
303#ifdef MTD_LINUX
304 if (!res)
305 mtd->ecc_stats.badblocks++;
306#endif
307 return res;
308}
309
310/*
311 * This function unregisters and destroy all slave MTD objects which are
312 * attached to the given master MTD object.
313 */
314
315int del_mtd_partitions(struct mtd_info *master)
316{
317 struct list_head *node;
318 struct mtd_part *slave;
319
320 for (node = mtd_partitions.next;
321 node != &mtd_partitions;
322 node = node->next) {
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);
329 kfree(slave);
330 node = prev;
331 }
332 }
333
334 return 0;
335}
336
337/*
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?)
342 */
343
344int add_mtd_partitions(struct mtd_info *master,
345 const struct mtd_partition *parts,
346 int nbparts)
347{
348 struct mtd_part *slave;
349 u_int32_t cur_offset = 0;
350 int i;
351
Stefan Roese9def12c2008-11-27 14:05:15 +0100352 /*
353 * Need to init the list here, since LIST_INIT() does not
354 * work on platforms where relocation has problems (like MIPS
355 * & PPC).
356 */
357 if (mtd_partitions.next == NULL)
358 INIT_LIST_HEAD(&mtd_partitions);
359
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100360 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
361
362 for (i = 0; i < nbparts; i++) {
363
364 /* allocate the partition structure */
365 slave = kzalloc (sizeof(*slave), GFP_KERNEL);
366 if (!slave) {
367 printk ("memory allocation error while creating partitions for \"%s\"\n",
368 master->name);
369 del_mtd_partitions(master);
370 return -ENOMEM;
371 }
372 list_add(&slave->list, &mtd_partitions);
373
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;
382
383 slave->mtd.name = parts[i].name;
384 slave->mtd.owner = master->owner;
385
386 slave->mtd.read = part_read;
387 slave->mtd.write = part_write;
388
389#ifdef MTD_LINUX
390 if (master->panic_write)
391 slave->mtd.panic_write = part_panic_write;
392
393 if(master->point && master->unpoint){
394 slave->mtd.point = part_point;
395 slave->mtd.unpoint = part_unpoint;
396 }
397#endif
398
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;
415 if (master->sync)
416 slave->mtd.sync = part_sync;
417#ifdef MTD_LINUX
418 if (!i && master->suspend && master->resume) {
419 slave->mtd.suspend = part_suspend;
420 slave->mtd.resume = part_resume;
421 }
422 if (master->writev)
423 slave->mtd.writev = part_writev;
424 if (master->lock)
425 slave->mtd.lock = part_lock;
426 if (master->unlock)
427 slave->mtd.unlock = part_unlock;
428#endif
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;
436 slave->index = i;
437
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);
448 }
449 }
450 if (slave->mtd.size == MTDPART_SIZ_FULL)
451 slave->mtd.size = master->size - slave->offset;
452 cur_offset = slave->offset + slave->mtd.size;
453
454 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
455 slave->offset + slave->mtd.size, slave->mtd.name);
456
457 /* let's do some sanity checks */
458 if (slave->offset >= master->size) {
459 /* let's register it anyway to preserve ordering */
460 slave->offset = 0;
461 slave->mtd.size = 0;
462 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
463 parts[i].name);
464 }
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);
469 }
470 if (master->numeraseregions>1) {
471 /* Deal with variable erase size stuff */
472 int i;
473 struct mtd_erase_region_info *regions = master->eraseregions;
474
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++)
477 ;
478
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;
482 }
483 }
484 } else {
485 /* Single erase size */
486 slave->mtd.erasesize = master->erasesize;
487 }
488
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",
495 parts[i].name);
496 }
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",
501 parts[i].name);
502 }
503
504 slave->mtd.ecclayout = master->ecclayout;
505 if (master->block_isbad) {
506 uint32_t offs = 0;
507
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;
513 }
514 }
515
516#ifdef MTD_LINUX
517 if (parts[i].mtdp) {
518 /* store the object pointer
519 * (caller may or may not register it */
520 *parts[i].mtdp = &slave->mtd;
521 slave->registered = 0;
522 } else {
523 /* register our partition */
524 add_mtd_device(&slave->mtd);
525 slave->registered = 1;
526 }
527#else
528 /* register our partition */
529 add_mtd_device(&slave->mtd);
530 slave->registered = 1;
531#endif
532 }
533
534 return 0;
535}
536
537#ifdef MTD_LINUX
538EXPORT_SYMBOL(add_mtd_partitions);
539EXPORT_SYMBOL(del_mtd_partitions);
540#endif