2 * dfu_nand.c -- DFU for NAND routines.
4 * Copyright (C) 2012-2013 Texas Instruments, Inc.
6 * Based on dfu_mmc.c which is:
7 * Copyright (C) 2012 Samsung Electronics
8 * author: Lukasz Majewski <l.majewski@samsung.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <linux/mtd/mtd.h>
31 #include <jffs2/load_kernel.h>
39 static int nand_block_op(enum dfu_nand_op op, struct dfu_entity *dfu,
40 u64 offset, void *buf, long *len)
47 /* if buf == NULL return total size of the area */
49 *len = dfu->data.nand.size;
53 start = dfu->data.nand.start + offset + dfu->bad_skip;
54 lim = dfu->data.nand.start + dfu->data.nand.size - start;
57 if (nand_curr_device < 0 ||
58 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
59 !nand_info[nand_curr_device].name) {
60 printf("%s: invalid nand device\n", __func__);
64 nand = &nand_info[nand_curr_device];
66 if (op == DFU_OP_READ)
67 ret = nand_read_skip_bad(nand, start, &count, &actual,
70 ret = nand_write_skip_bad(nand, start, &count, &actual,
74 printf("%s: nand_%s_skip_bad call failed at %llx!\n",
75 __func__, op == DFU_OP_READ ? "read" : "write",
81 * Find out where we stopped writing data. This can be deeper into
82 * the NAND than we expected due to having to skip bad blocks. So
83 * we must take this into account for the next write, if any.
86 dfu->bad_skip += actual - count;
91 static inline int nand_block_write(struct dfu_entity *dfu,
92 u64 offset, void *buf, long *len)
94 return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
97 static inline int nand_block_read(struct dfu_entity *dfu,
98 u64 offset, void *buf, long *len)
100 return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
103 static int dfu_write_medium_nand(struct dfu_entity *dfu,
104 u64 offset, void *buf, long *len)
108 switch (dfu->layout) {
110 ret = nand_block_write(dfu, offset, buf, len);
113 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
114 dfu_get_layout(dfu->layout));
120 static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
125 switch (dfu->layout) {
127 ret = nand_block_read(dfu, offset, buf, len);
130 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
131 dfu_get_layout(dfu->layout));
137 int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s)
142 dfu->dev_type = DFU_DEV_NAND;
143 st = strsep(&s, " ");
144 if (!strcmp(st, "raw")) {
145 dfu->layout = DFU_RAW_ADDR;
146 dfu->data.nand.start = simple_strtoul(s, &s, 16);
148 dfu->data.nand.size = simple_strtoul(s, &s, 16);
149 } else if (!strcmp(st, "part")) {
151 struct mtd_device *mtd_dev;
153 struct part_info *pi;
155 dfu->layout = DFU_RAW_ADDR;
157 dev = simple_strtoul(s, &s, 10);
159 part = simple_strtoul(s, &s, 10);
161 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
162 printf("using id '%s'\n", mtd_id);
166 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
168 printf("Could not locate '%s'\n", mtd_id);
172 dfu->data.nand.start = pi->offset;
173 dfu->data.nand.size = pi->size;
176 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
180 dfu->read_medium = dfu_read_medium_nand;
181 dfu->write_medium = dfu_write_medium_nand;