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 * SPDX-License-Identifier: GPL-2.0+
18 #include <linux/mtd/mtd.h>
19 #include <jffs2/load_kernel.h>
22 static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu,
23 u64 offset, void *buf, long *len)
30 /* if buf == NULL return total size of the area */
32 *len = dfu->data.nand.size;
36 start = dfu->data.nand.start + offset + dfu->bad_skip;
37 lim = dfu->data.nand.start + dfu->data.nand.size - start;
40 if (nand_curr_device < 0 ||
41 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
42 !nand_info[nand_curr_device].name) {
43 printf("%s: invalid nand device\n", __func__);
47 nand = &nand_info[nand_curr_device];
49 if (op == DFU_OP_READ) {
50 ret = nand_read_skip_bad(nand, start, &count, &actual,
53 nand_erase_options_t opts;
55 memset(&opts, 0, sizeof(opts));
62 ret = nand_erase_opts(nand, &opts);
66 ret = nand_write_skip_bad(nand, start, &count, &actual,
71 printf("%s: nand_%s_skip_bad call failed at %llx!\n",
72 __func__, op == DFU_OP_READ ? "read" : "write",
78 * Find out where we stopped writing data. This can be deeper into
79 * the NAND than we expected due to having to skip bad blocks. So
80 * we must take this into account for the next write, if any.
83 dfu->bad_skip += actual - count;
88 static inline int nand_block_write(struct dfu_entity *dfu,
89 u64 offset, void *buf, long *len)
91 return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
94 static inline int nand_block_read(struct dfu_entity *dfu,
95 u64 offset, void *buf, long *len)
97 return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
100 static int dfu_write_medium_nand(struct dfu_entity *dfu,
101 u64 offset, void *buf, long *len)
105 switch (dfu->layout) {
107 ret = nand_block_write(dfu, offset, buf, len);
110 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
111 dfu_get_layout(dfu->layout));
117 long dfu_get_medium_size_nand(struct dfu_entity *dfu)
119 return dfu->data.nand.size;
122 static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
127 switch (dfu->layout) {
129 ret = nand_block_read(dfu, offset, buf, len);
132 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
133 dfu_get_layout(dfu->layout));
139 static int dfu_flush_medium_nand(struct dfu_entity *dfu)
143 /* in case of ubi partition, erase rest of the partition */
144 if (dfu->data.nand.ubi) {
146 nand_erase_options_t opts;
148 if (nand_curr_device < 0 ||
149 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
150 !nand_info[nand_curr_device].name) {
151 printf("%s: invalid nand device\n", __func__);
155 nand = &nand_info[nand_curr_device];
157 memset(&opts, 0, sizeof(opts));
158 opts.offset = dfu->data.nand.start + dfu->offset +
160 opts.length = dfu->data.nand.start +
161 dfu->data.nand.size - opts.offset;
162 ret = nand_erase_opts(nand, &opts);
164 printf("Failure erase: %d\n", ret);
170 unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu)
173 * Currently, Poll Timeout != 0 is only needed on nand
174 * ubi partition, as the not used sectors need an erase
176 if (dfu->data.nand.ubi)
177 return DFU_MANIFEST_POLL_TIMEOUT;
179 return DFU_DEFAULT_POLL_TIMEOUT;
182 int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s)
187 dfu->data.nand.ubi = 0;
188 dfu->dev_type = DFU_DEV_NAND;
189 st = strsep(&s, " ");
190 if (!strcmp(st, "raw")) {
191 dfu->layout = DFU_RAW_ADDR;
192 dfu->data.nand.start = simple_strtoul(s, &s, 16);
194 dfu->data.nand.size = simple_strtoul(s, &s, 16);
195 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
197 struct mtd_device *mtd_dev;
199 struct part_info *pi;
201 dfu->layout = DFU_RAW_ADDR;
203 dev = simple_strtoul(s, &s, 10);
205 part = simple_strtoul(s, &s, 10);
207 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
208 printf("using id '%s'\n", mtd_id);
212 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
214 printf("Could not locate '%s'\n", mtd_id);
218 dfu->data.nand.start = pi->offset;
219 dfu->data.nand.size = pi->size;
220 if (!strcmp(st, "partubi"))
221 dfu->data.nand.ubi = 1;
223 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
227 dfu->get_medium_size = dfu_get_medium_size_nand;
228 dfu->read_medium = dfu_read_medium_nand;
229 dfu->write_medium = dfu_write_medium_nand;
230 dfu->flush_medium = dfu_flush_medium_nand;
231 dfu->poll_timeout = dfu_polltimeout_nand;