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>
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 mtd = get_nand_dev_by_index(nand_curr_device);
42 if (nand_curr_device < 0 ||
43 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
45 printf("%s: invalid nand device\n", __func__);
49 if (op == DFU_OP_READ) {
50 ret = nand_read_skip_bad(mtd, start, &count, &actual,
53 nand_erase_options_t opts;
54 int write_flags = WITH_WR_VERIFY;
56 memset(&opts, 0, sizeof(opts));
63 ret = nand_erase_opts(mtd, &opts);
67 #ifdef CONFIG_DFU_NAND_TRIMFFS
68 if (dfu->data.nand.ubi)
69 write_flags |= WITH_DROP_FFS;
71 ret = nand_write_skip_bad(mtd, start, &count, &actual,
72 lim, buf, write_flags);
76 printf("%s: nand_%s_skip_bad call failed at %llx!\n",
77 __func__, op == DFU_OP_READ ? "read" : "write",
83 * Find out where we stopped writing data. This can be deeper into
84 * the NAND than we expected due to having to skip bad blocks. So
85 * we must take this into account for the next write, if any.
88 dfu->bad_skip += actual - count;
93 static inline int nand_block_write(struct dfu_entity *dfu,
94 u64 offset, void *buf, long *len)
96 return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
99 static inline int nand_block_read(struct dfu_entity *dfu,
100 u64 offset, void *buf, long *len)
102 return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
105 static int dfu_write_medium_nand(struct dfu_entity *dfu,
106 u64 offset, void *buf, long *len)
110 switch (dfu->layout) {
112 ret = nand_block_write(dfu, offset, buf, len);
115 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
116 dfu_get_layout(dfu->layout));
122 int dfu_get_medium_size_nand(struct dfu_entity *dfu, u64 *size)
124 *size = dfu->data.nand.size;
129 static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
134 switch (dfu->layout) {
136 ret = nand_block_read(dfu, offset, buf, len);
139 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
140 dfu_get_layout(dfu->layout));
146 static int dfu_flush_medium_nand(struct dfu_entity *dfu)
151 /* in case of ubi partition, erase rest of the partition */
152 if (dfu->data.nand.ubi) {
153 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
154 nand_erase_options_t opts;
156 if (nand_curr_device < 0 ||
157 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
159 printf("%s: invalid nand device\n", __func__);
163 memset(&opts, 0, sizeof(opts));
165 if ((off & (mtd->erasesize - 1)) != 0) {
167 * last write ended with unaligned length
168 * sector is erased, jump to next
170 off = off & ~((mtd->erasesize - 1));
171 off += mtd->erasesize;
173 opts.offset = dfu->data.nand.start + off +
175 opts.length = dfu->data.nand.start +
176 dfu->data.nand.size - opts.offset;
177 ret = nand_erase_opts(mtd, &opts);
179 printf("Failure erase: %d\n", ret);
185 unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu)
188 * Currently, Poll Timeout != 0 is only needed on nand
189 * ubi partition, as the not used sectors need an erase
191 if (dfu->data.nand.ubi)
192 return DFU_MANIFEST_POLL_TIMEOUT;
194 return DFU_DEFAULT_POLL_TIMEOUT;
197 int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s)
202 dfu->data.nand.ubi = 0;
203 dfu->dev_type = DFU_DEV_NAND;
204 st = strsep(&s, " ");
205 if (!strcmp(st, "raw")) {
206 dfu->layout = DFU_RAW_ADDR;
207 dfu->data.nand.start = simple_strtoul(s, &s, 16);
209 dfu->data.nand.size = simple_strtoul(s, &s, 16);
210 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
212 struct mtd_device *mtd_dev;
214 struct part_info *pi;
216 dfu->layout = DFU_RAW_ADDR;
218 dev = simple_strtoul(s, &s, 10);
220 part = simple_strtoul(s, &s, 10);
222 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
223 debug("using id '%s'\n", mtd_id);
227 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
229 printf("Could not locate '%s'\n", mtd_id);
233 dfu->data.nand.start = pi->offset;
234 dfu->data.nand.size = pi->size;
235 if (!strcmp(st, "partubi"))
236 dfu->data.nand.ubi = 1;
238 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
242 dfu->get_medium_size = dfu_get_medium_size_nand;
243 dfu->read_medium = dfu_read_medium_nand;
244 dfu->write_medium = dfu_write_medium_nand;
245 dfu->flush_medium = dfu_flush_medium_nand;
246 dfu->poll_timeout = dfu_polltimeout_nand;