1 // SPDX-License-Identifier: GPL-2.0+
3 * dfu_nand.c -- DFU for NAND routines.
5 * Copyright (C) 2012-2013 Texas Instruments, Inc.
7 * Based on dfu_mmc.c which is:
8 * Copyright (C) 2012 Samsung Electronics
9 * author: Lukasz Majewski <l.majewski@samsung.com>
17 #include <linux/mtd/mtd.h>
18 #include <jffs2/load_kernel.h>
21 static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu,
22 u64 offset, void *buf, long *len)
29 /* if buf == NULL return total size of the area */
31 *len = dfu->data.nand.size;
35 start = dfu->data.nand.start + offset + dfu->bad_skip;
36 lim = dfu->data.nand.start + dfu->data.nand.size - start;
39 mtd = get_nand_dev_by_index(nand_curr_device);
41 if (nand_curr_device < 0 ||
42 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
44 printf("%s: invalid nand device\n", __func__);
48 if (op == DFU_OP_READ) {
49 ret = nand_read_skip_bad(mtd, start, &count, &actual,
52 nand_erase_options_t opts;
54 memset(&opts, 0, sizeof(opts));
61 ret = nand_erase_opts(mtd, &opts);
65 ret = nand_write_skip_bad(mtd, start, &count, &actual,
66 lim, buf, WITH_WR_VERIFY);
70 printf("%s: nand_%s_skip_bad call failed at %llx!\n",
71 __func__, op == DFU_OP_READ ? "read" : "write",
77 * Find out where we stopped writing data. This can be deeper into
78 * the NAND than we expected due to having to skip bad blocks. So
79 * we must take this into account for the next write, if any.
82 dfu->bad_skip += actual - count;
87 static inline int nand_block_write(struct dfu_entity *dfu,
88 u64 offset, void *buf, long *len)
90 return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
93 static inline int nand_block_read(struct dfu_entity *dfu,
94 u64 offset, void *buf, long *len)
96 return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
99 static int dfu_write_medium_nand(struct dfu_entity *dfu,
100 u64 offset, void *buf, long *len)
104 switch (dfu->layout) {
106 ret = nand_block_write(dfu, offset, buf, len);
109 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
110 dfu_get_layout(dfu->layout));
116 int dfu_get_medium_size_nand(struct dfu_entity *dfu, u64 *size)
118 *size = dfu->data.nand.size;
123 static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
128 switch (dfu->layout) {
130 ret = nand_block_read(dfu, offset, buf, len);
133 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
134 dfu_get_layout(dfu->layout));
140 static int dfu_flush_medium_nand(struct dfu_entity *dfu)
145 /* in case of ubi partition, erase rest of the partition */
146 if (dfu->data.nand.ubi) {
147 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
148 nand_erase_options_t opts;
150 if (nand_curr_device < 0 ||
151 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
153 printf("%s: invalid nand device\n", __func__);
157 memset(&opts, 0, sizeof(opts));
159 if ((off & (mtd->erasesize - 1)) != 0) {
161 * last write ended with unaligned length
162 * sector is erased, jump to next
164 off = off & ~((mtd->erasesize - 1));
165 off += mtd->erasesize;
167 opts.offset = dfu->data.nand.start + off +
169 opts.length = dfu->data.nand.start +
170 dfu->data.nand.size - opts.offset;
171 ret = nand_erase_opts(mtd, &opts);
173 printf("Failure erase: %d\n", ret);
179 unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu)
182 * Currently, Poll Timeout != 0 is only needed on nand
183 * ubi partition, as the not used sectors need an erase
185 if (dfu->data.nand.ubi)
186 return DFU_MANIFEST_POLL_TIMEOUT;
188 return DFU_DEFAULT_POLL_TIMEOUT;
191 int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s)
196 dfu->data.nand.ubi = 0;
197 dfu->dev_type = DFU_DEV_NAND;
198 st = strsep(&s, " ");
199 if (!strcmp(st, "raw")) {
200 dfu->layout = DFU_RAW_ADDR;
201 dfu->data.nand.start = simple_strtoul(s, &s, 16);
203 dfu->data.nand.size = simple_strtoul(s, &s, 16);
204 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
206 struct mtd_device *mtd_dev;
208 struct part_info *pi;
210 dfu->layout = DFU_RAW_ADDR;
212 dev = simple_strtoul(s, &s, 10);
214 part = simple_strtoul(s, &s, 10);
216 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
217 printf("using id '%s'\n", mtd_id);
221 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
223 printf("Could not locate '%s'\n", mtd_id);
227 dfu->data.nand.start = pi->offset;
228 dfu->data.nand.size = pi->size;
229 if (!strcmp(st, "partubi"))
230 dfu->data.nand.ubi = 1;
232 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
236 dfu->get_medium_size = dfu_get_medium_size_nand;
237 dfu->read_medium = dfu_read_medium_nand;
238 dfu->write_medium = dfu_write_medium_nand;
239 dfu->flush_medium = dfu_flush_medium_nand;
240 dfu->poll_timeout = dfu_polltimeout_nand;