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 nand_erase_options_t opts;
72 memset(&opts, 0, sizeof(opts));
79 ret = nand_erase_opts(nand, &opts);
83 ret = nand_write_skip_bad(nand, start, &count, &actual,
88 printf("%s: nand_%s_skip_bad call failed at %llx!\n",
89 __func__, op == DFU_OP_READ ? "read" : "write",
95 * Find out where we stopped writing data. This can be deeper into
96 * the NAND than we expected due to having to skip bad blocks. So
97 * we must take this into account for the next write, if any.
100 dfu->bad_skip += actual - count;
105 static inline int nand_block_write(struct dfu_entity *dfu,
106 u64 offset, void *buf, long *len)
108 return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
111 static inline int nand_block_read(struct dfu_entity *dfu,
112 u64 offset, void *buf, long *len)
114 return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
117 static int dfu_write_medium_nand(struct dfu_entity *dfu,
118 u64 offset, void *buf, long *len)
122 switch (dfu->layout) {
124 ret = nand_block_write(dfu, offset, buf, len);
127 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
128 dfu_get_layout(dfu->layout));
134 static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
139 switch (dfu->layout) {
141 ret = nand_block_read(dfu, offset, buf, len);
144 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
145 dfu_get_layout(dfu->layout));
151 int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s)
156 dfu->dev_type = DFU_DEV_NAND;
157 st = strsep(&s, " ");
158 if (!strcmp(st, "raw")) {
159 dfu->layout = DFU_RAW_ADDR;
160 dfu->data.nand.start = simple_strtoul(s, &s, 16);
162 dfu->data.nand.size = simple_strtoul(s, &s, 16);
163 } else if (!strcmp(st, "part")) {
165 struct mtd_device *mtd_dev;
167 struct part_info *pi;
169 dfu->layout = DFU_RAW_ADDR;
171 dev = simple_strtoul(s, &s, 10);
173 part = simple_strtoul(s, &s, 10);
175 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
176 printf("using id '%s'\n", mtd_id);
180 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
182 printf("Could not locate '%s'\n", mtd_id);
186 dfu->data.nand.start = pi->offset;
187 dfu->data.nand.size = pi->size;
190 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
194 dfu->read_medium = dfu_read_medium_nand;
195 dfu->write_medium = dfu_write_medium_nand;