blob: 146ce11eb1bafcfbb8f0add516c5675d0f166bc7 [file] [log] [blame]
Kyungmin Parkf6d5e252008-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>
Mike Frysinger11d1a092012-04-09 13:39:55 +000020#include <linux/compat.h>
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010021
22/* Our partition linked list */
Stefan Roese61379202008-11-27 14:05:15 +010023struct list_head mtd_partitions;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010024
25/* Our partition node structure */
26struct mtd_part {
27 struct mtd_info mtd;
28 struct mtd_info *master;
Stefan Roese586b3a62009-05-11 16:03:55 +020029 uint64_t offset;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010030 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
Stefan Roese586b3a62009-05-11 16:03:55 +020047static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
48 size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010049{
50 struct mtd_part *part = PART(mtd);
Stefan Roese586b3a62009-05-11 16:03:55 +020051 struct mtd_ecc_stats stats;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010052 int res;
53
Stefan Roese586b3a62009-05-11 16:03:55 +020054 stats = part->master->ecc_stats;
Sergey Lapin3a38a552013-01-14 03:46:50 +000055 res = mtd_read(part->master, from + part->offset, len, retlen, buf);
Paul Burton700a76c2013-09-04 15:16:56 +010056 if (unlikely(mtd_is_eccerr(res)))
57 mtd->ecc_stats.failed +=
58 part->master->ecc_stats.failed - stats.failed;
59 else
60 mtd->ecc_stats.corrected +=
61 part->master->ecc_stats.corrected - stats.corrected;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010062 return res;
63}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010064
65static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese586b3a62009-05-11 16:03:55 +020066 struct mtd_oob_ops *ops)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010067{
68 struct mtd_part *part = PART(mtd);
69 int res;
70
71 if (from >= mtd->size)
72 return -EINVAL;
73 if (ops->datbuf && from + ops->len > mtd->size)
74 return -EINVAL;
Sergey Lapin3a38a552013-01-14 03:46:50 +000075 res = mtd_read_oob(part->master, from + part->offset, ops);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010076
77 if (unlikely(res)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +000078 if (mtd_is_bitflip(res))
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010079 mtd->ecc_stats.corrected++;
Sergey Lapin3a38a552013-01-14 03:46:50 +000080 if (mtd_is_eccerr(res))
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010081 mtd->ecc_stats.failed++;
82 }
83 return res;
84}
85
Stefan Roese586b3a62009-05-11 16:03:55 +020086static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
87 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010088{
89 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +000090 return mtd_read_user_prot_reg(part->master, from, len, retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010091}
92
Stefan Roese586b3a62009-05-11 16:03:55 +020093static int part_get_user_prot_info(struct mtd_info *mtd,
94 struct otp_info *buf, size_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010095{
96 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +000097 return mtd_get_user_prot_info(part->master, buf, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010098}
99
Stefan Roese586b3a62009-05-11 16:03:55 +0200100static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
101 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100102{
103 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000104 return mtd_read_fact_prot_reg(part->master, from, len, retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100105}
106
Stefan Roese586b3a62009-05-11 16:03:55 +0200107static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
108 size_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100109{
110 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000111 return mtd_get_fact_prot_info(part->master, buf, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100112}
113
Stefan Roese586b3a62009-05-11 16:03:55 +0200114static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
115 size_t *retlen, const u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100116{
117 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000118 return mtd_write(part->master, to + part->offset, len, retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100119}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100120
121static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese586b3a62009-05-11 16:03:55 +0200122 struct mtd_oob_ops *ops)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100123{
124 struct mtd_part *part = PART(mtd);
125
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100126 if (to >= mtd->size)
127 return -EINVAL;
128 if (ops->datbuf && to + ops->len > mtd->size)
129 return -EINVAL;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000130 return mtd_write_oob(part->master, to + part->offset, ops);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100131}
132
Stefan Roese586b3a62009-05-11 16:03:55 +0200133static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
134 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100135{
136 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000137 return mtd_write_user_prot_reg(part->master, from, len, retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100138}
139
Stefan Roese586b3a62009-05-11 16:03:55 +0200140static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
141 size_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100142{
143 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000144 return mtd_lock_user_prot_reg(part->master, from, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100145}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100146
Stefan Roese586b3a62009-05-11 16:03:55 +0200147static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100148{
149 struct mtd_part *part = PART(mtd);
150 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000151
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100152 instr->addr += part->offset;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000153 ret = mtd_erase(part->master, instr);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100154 if (ret) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200155 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100156 instr->fail_addr -= part->offset;
157 instr->addr -= part->offset;
158 }
159 return ret;
160}
161
162void mtd_erase_callback(struct erase_info *instr)
163{
Sergey Lapin3a38a552013-01-14 03:46:50 +0000164 if (instr->mtd->_erase == part_erase) {
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100165 struct mtd_part *part = PART(instr->mtd);
166
Stefan Roese586b3a62009-05-11 16:03:55 +0200167 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100168 instr->fail_addr -= part->offset;
169 instr->addr -= part->offset;
170 }
171 if (instr->callback)
172 instr->callback(instr);
173}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100174
Stefan Roese586b3a62009-05-11 16:03:55 +0200175static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100176{
177 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000178 return mtd_lock(part->master, ofs + part->offset, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100179}
180
Stefan Roese586b3a62009-05-11 16:03:55 +0200181static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100182{
183 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000184 return mtd_unlock(part->master, ofs + part->offset, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100185}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100186
187static void part_sync(struct mtd_info *mtd)
188{
189 struct mtd_part *part = PART(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000190 mtd_sync(part->master);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100191}
192
Stefan Roese586b3a62009-05-11 16:03:55 +0200193static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100194{
195 struct mtd_part *part = PART(mtd);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100196 ofs += part->offset;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000197 return mtd_block_isbad(part->master, ofs);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100198}
199
Stefan Roese586b3a62009-05-11 16:03:55 +0200200static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100201{
202 struct mtd_part *part = PART(mtd);
203 int res;
204
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100205 ofs += part->offset;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000206 res = mtd_block_markbad(part->master, ofs);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100207 if (!res)
208 mtd->ecc_stats.badblocks++;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100209 return res;
210}
211
212/*
213 * This function unregisters and destroy all slave MTD objects which are
214 * attached to the given master MTD object.
215 */
216
217int del_mtd_partitions(struct mtd_info *master)
218{
Stefan Roese586b3a62009-05-11 16:03:55 +0200219 struct mtd_part *slave, *next;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100220
Stefan Roese586b3a62009-05-11 16:03:55 +0200221 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100222 if (slave->master == master) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200223 list_del(&slave->list);
224 if (slave->registered)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100225 del_mtd_device(&slave->mtd);
226 kfree(slave);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100227 }
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100228
229 return 0;
230}
231
Stefan Roese586b3a62009-05-11 16:03:55 +0200232static struct mtd_part *add_one_partition(struct mtd_info *master,
233 const struct mtd_partition *part, int partno,
234 uint64_t cur_offset)
235{
236 struct mtd_part *slave;
237
238 /* allocate the partition structure */
239 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
240 if (!slave) {
241 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
242 master->name);
243 del_mtd_partitions(master);
244 return NULL;
245 }
246 list_add(&slave->list, &mtd_partitions);
247
248 /* set up the MTD object for this partition */
249 slave->mtd.type = master->type;
250 slave->mtd.flags = master->flags & ~part->mask_flags;
251 slave->mtd.size = part->size;
252 slave->mtd.writesize = master->writesize;
253 slave->mtd.oobsize = master->oobsize;
254 slave->mtd.oobavail = master->oobavail;
255 slave->mtd.subpage_sft = master->subpage_sft;
256
257 slave->mtd.name = part->name;
258 slave->mtd.owner = master->owner;
259
Sergey Lapin3a38a552013-01-14 03:46:50 +0000260 slave->mtd._read = part_read;
261 slave->mtd._write = part_write;
Stefan Roese586b3a62009-05-11 16:03:55 +0200262
Sergey Lapin3a38a552013-01-14 03:46:50 +0000263 if (master->_read_oob)
264 slave->mtd._read_oob = part_read_oob;
265 if (master->_write_oob)
266 slave->mtd._write_oob = part_write_oob;
267 if (master->_read_user_prot_reg)
268 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
269 if (master->_read_fact_prot_reg)
270 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
271 if (master->_write_user_prot_reg)
272 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
273 if (master->_lock_user_prot_reg)
274 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
275 if (master->_get_user_prot_info)
276 slave->mtd._get_user_prot_info = part_get_user_prot_info;
277 if (master->_get_fact_prot_info)
278 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
279 if (master->_sync)
280 slave->mtd._sync = part_sync;
281 if (master->_lock)
282 slave->mtd._lock = part_lock;
283 if (master->_unlock)
284 slave->mtd._unlock = part_unlock;
285 if (master->_block_isbad)
286 slave->mtd._block_isbad = part_block_isbad;
287 if (master->_block_markbad)
288 slave->mtd._block_markbad = part_block_markbad;
289 slave->mtd._erase = part_erase;
Stefan Roese586b3a62009-05-11 16:03:55 +0200290 slave->master = master;
291 slave->offset = part->offset;
292 slave->index = partno;
293
294 if (slave->offset == MTDPART_OFS_APPEND)
295 slave->offset = cur_offset;
296 if (slave->offset == MTDPART_OFS_NXTBLK) {
297 slave->offset = cur_offset;
298 if (mtd_mod_by_eb(cur_offset, master) != 0) {
299 /* Round up to next erasesize */
300 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Joe Hershberger47550fc2013-04-08 10:32:49 +0000301 debug("Moving partition %d: 0x%012llx -> 0x%012llx\n",
302 partno, (unsigned long long)cur_offset,
303 (unsigned long long)slave->offset);
Stefan Roese586b3a62009-05-11 16:03:55 +0200304 }
305 }
306 if (slave->mtd.size == MTDPART_SIZ_FULL)
307 slave->mtd.size = master->size - slave->offset;
308
Joe Hershberger47550fc2013-04-08 10:32:49 +0000309 debug("0x%012llx-0x%012llx : \"%s\"\n",
310 (unsigned long long)slave->offset,
311 (unsigned long long)(slave->offset + slave->mtd.size),
312 slave->mtd.name);
Stefan Roese586b3a62009-05-11 16:03:55 +0200313
314 /* let's do some sanity checks */
315 if (slave->offset >= master->size) {
316 /* let's register it anyway to preserve ordering */
317 slave->offset = 0;
318 slave->mtd.size = 0;
319 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
320 part->name);
321 goto out_register;
322 }
323 if (slave->offset + slave->mtd.size > master->size) {
324 slave->mtd.size = master->size - slave->offset;
325 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
326 part->name, master->name, (unsigned long long)slave->mtd.size);
327 }
328 if (master->numeraseregions > 1) {
329 /* Deal with variable erase size stuff */
330 int i, max = master->numeraseregions;
331 u64 end = slave->offset + slave->mtd.size;
332 struct mtd_erase_region_info *regions = master->eraseregions;
333
334 /* Find the first erase regions which is part of this
335 * partition. */
336 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
337 ;
338 /* The loop searched for the region _behind_ the first one */
339 i--;
340
341 /* Pick biggest erasesize */
342 for (; i < max && regions[i].offset < end; i++) {
343 if (slave->mtd.erasesize < regions[i].erasesize) {
344 slave->mtd.erasesize = regions[i].erasesize;
345 }
346 }
347 BUG_ON(slave->mtd.erasesize == 0);
348 } else {
349 /* Single erase size */
350 slave->mtd.erasesize = master->erasesize;
351 }
352
353 if ((slave->mtd.flags & MTD_WRITEABLE) &&
354 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
355 /* Doesn't start on a boundary of major erase size */
356 /* FIXME: Let it be writable if it is on a boundary of
357 * _minor_ erase size though */
358 slave->mtd.flags &= ~MTD_WRITEABLE;
359 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
360 part->name);
361 }
362 if ((slave->mtd.flags & MTD_WRITEABLE) &&
363 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
364 slave->mtd.flags &= ~MTD_WRITEABLE;
365 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
366 part->name);
367 }
368
369 slave->mtd.ecclayout = master->ecclayout;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000370 if (master->_block_isbad) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200371 uint64_t offs = 0;
372
373 while (offs < slave->mtd.size) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000374 if (mtd_block_isbad(master, offs + slave->offset))
Stefan Roese586b3a62009-05-11 16:03:55 +0200375 slave->mtd.ecc_stats.badblocks++;
376 offs += slave->mtd.erasesize;
377 }
378 }
379
380out_register:
381 if (part->mtdp) {
382 /* store the object pointer (caller may or may not register it*/
383 *part->mtdp = &slave->mtd;
384 slave->registered = 0;
385 } else {
386 /* register our partition */
387 add_mtd_device(&slave->mtd);
388 slave->registered = 1;
389 }
390 return slave;
391}
392
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100393/*
394 * This function, given a master MTD object and a partition table, creates
395 * and registers slave MTD objects which are bound to the master according to
396 * the partition definitions.
Stefan Roese586b3a62009-05-11 16:03:55 +0200397 *
398 * We don't register the master, or expect the caller to have done so,
399 * for reasons of data integrity.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100400 */
401
402int add_mtd_partitions(struct mtd_info *master,
403 const struct mtd_partition *parts,
404 int nbparts)
405{
406 struct mtd_part *slave;
Stefan Roese586b3a62009-05-11 16:03:55 +0200407 uint64_t cur_offset = 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100408 int i;
409
Stefan Roese61379202008-11-27 14:05:15 +0100410 /*
411 * Need to init the list here, since LIST_INIT() does not
412 * work on platforms where relocation has problems (like MIPS
413 * & PPC).
414 */
415 if (mtd_partitions.next == NULL)
416 INIT_LIST_HEAD(&mtd_partitions);
417
Joe Hershberger47550fc2013-04-08 10:32:49 +0000418 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100419
420 for (i = 0; i < nbparts; i++) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200421 slave = add_one_partition(master, parts + i, i, cur_offset);
422 if (!slave)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100423 return -ENOMEM;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100424 cur_offset = slave->offset + slave->mtd.size;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100425 }
426
427 return 0;
428}