blob: 9a3bf6f39d236107b19afb68bc0c8822aa08aa55 [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 */
23static LIST_HEAD(mtd_partitions);
24
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
352 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
353
354 for (i = 0; i < nbparts; i++) {
355
356 /* allocate the partition structure */
357 slave = kzalloc (sizeof(*slave), GFP_KERNEL);
358 if (!slave) {
359 printk ("memory allocation error while creating partitions for \"%s\"\n",
360 master->name);
361 del_mtd_partitions(master);
362 return -ENOMEM;
363 }
364 list_add(&slave->list, &mtd_partitions);
365
366 /* set up the MTD object for this partition */
367 slave->mtd.type = master->type;
368 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
369 slave->mtd.size = parts[i].size;
370 slave->mtd.writesize = master->writesize;
371 slave->mtd.oobsize = master->oobsize;
372 slave->mtd.oobavail = master->oobavail;
373 slave->mtd.subpage_sft = master->subpage_sft;
374
375 slave->mtd.name = parts[i].name;
376 slave->mtd.owner = master->owner;
377
378 slave->mtd.read = part_read;
379 slave->mtd.write = part_write;
380
381#ifdef MTD_LINUX
382 if (master->panic_write)
383 slave->mtd.panic_write = part_panic_write;
384
385 if(master->point && master->unpoint){
386 slave->mtd.point = part_point;
387 slave->mtd.unpoint = part_unpoint;
388 }
389#endif
390
391 if (master->read_oob)
392 slave->mtd.read_oob = part_read_oob;
393 if (master->write_oob)
394 slave->mtd.write_oob = part_write_oob;
395 if(master->read_user_prot_reg)
396 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
397 if(master->read_fact_prot_reg)
398 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
399 if(master->write_user_prot_reg)
400 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
401 if(master->lock_user_prot_reg)
402 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
403 if(master->get_user_prot_info)
404 slave->mtd.get_user_prot_info = part_get_user_prot_info;
405 if(master->get_fact_prot_info)
406 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
407 if (master->sync)
408 slave->mtd.sync = part_sync;
409#ifdef MTD_LINUX
410 if (!i && master->suspend && master->resume) {
411 slave->mtd.suspend = part_suspend;
412 slave->mtd.resume = part_resume;
413 }
414 if (master->writev)
415 slave->mtd.writev = part_writev;
416 if (master->lock)
417 slave->mtd.lock = part_lock;
418 if (master->unlock)
419 slave->mtd.unlock = part_unlock;
420#endif
421 if (master->block_isbad)
422 slave->mtd.block_isbad = part_block_isbad;
423 if (master->block_markbad)
424 slave->mtd.block_markbad = part_block_markbad;
425 slave->mtd.erase = part_erase;
426 slave->master = master;
427 slave->offset = parts[i].offset;
428 slave->index = i;
429
430 if (slave->offset == MTDPART_OFS_APPEND)
431 slave->offset = cur_offset;
432 if (slave->offset == MTDPART_OFS_NXTBLK) {
433 slave->offset = cur_offset;
434 if ((cur_offset % master->erasesize) != 0) {
435 /* Round up to next erasesize */
436 slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
437 printk(KERN_NOTICE "Moving partition %d: "
438 "0x%08x -> 0x%08x\n", i,
439 cur_offset, slave->offset);
440 }
441 }
442 if (slave->mtd.size == MTDPART_SIZ_FULL)
443 slave->mtd.size = master->size - slave->offset;
444 cur_offset = slave->offset + slave->mtd.size;
445
446 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
447 slave->offset + slave->mtd.size, slave->mtd.name);
448
449 /* let's do some sanity checks */
450 if (slave->offset >= master->size) {
451 /* let's register it anyway to preserve ordering */
452 slave->offset = 0;
453 slave->mtd.size = 0;
454 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
455 parts[i].name);
456 }
457 if (slave->offset + slave->mtd.size > master->size) {
458 slave->mtd.size = master->size - slave->offset;
459 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
460 parts[i].name, master->name, slave->mtd.size);
461 }
462 if (master->numeraseregions>1) {
463 /* Deal with variable erase size stuff */
464 int i;
465 struct mtd_erase_region_info *regions = master->eraseregions;
466
467 /* Find the first erase regions which is part of this partition. */
468 for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
469 ;
470
471 for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
472 if (slave->mtd.erasesize < regions[i].erasesize) {
473 slave->mtd.erasesize = regions[i].erasesize;
474 }
475 }
476 } else {
477 /* Single erase size */
478 slave->mtd.erasesize = master->erasesize;
479 }
480
481 if ((slave->mtd.flags & MTD_WRITEABLE) &&
482 (slave->offset % slave->mtd.erasesize)) {
483 /* Doesn't start on a boundary of major erase size */
484 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
485 slave->mtd.flags &= ~MTD_WRITEABLE;
486 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
487 parts[i].name);
488 }
489 if ((slave->mtd.flags & MTD_WRITEABLE) &&
490 (slave->mtd.size % slave->mtd.erasesize)) {
491 slave->mtd.flags &= ~MTD_WRITEABLE;
492 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
493 parts[i].name);
494 }
495
496 slave->mtd.ecclayout = master->ecclayout;
497 if (master->block_isbad) {
498 uint32_t offs = 0;
499
500 while(offs < slave->mtd.size) {
501 if (master->block_isbad(master,
502 offs + slave->offset))
503 slave->mtd.ecc_stats.badblocks++;
504 offs += slave->mtd.erasesize;
505 }
506 }
507
508#ifdef MTD_LINUX
509 if (parts[i].mtdp) {
510 /* store the object pointer
511 * (caller may or may not register it */
512 *parts[i].mtdp = &slave->mtd;
513 slave->registered = 0;
514 } else {
515 /* register our partition */
516 add_mtd_device(&slave->mtd);
517 slave->registered = 1;
518 }
519#else
520 /* register our partition */
521 add_mtd_device(&slave->mtd);
522 slave->registered = 1;
523#endif
524 }
525
526 return 0;
527}
528
529#ifdef MTD_LINUX
530EXPORT_SYMBOL(add_mtd_partitions);
531EXPORT_SYMBOL(del_mtd_partitions);
532#endif