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>
27 static int nand_block_op(enum dfu_nand_op op, struct dfu_entity *dfu,
28 u64 offset, void *buf, long *len)
35 /* if buf == NULL return total size of the area */
37 *len = dfu->data.nand.size;
41 start = dfu->data.nand.start + offset + dfu->bad_skip;
42 lim = dfu->data.nand.start + dfu->data.nand.size - start;
45 if (nand_curr_device < 0 ||
46 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
47 !nand_info[nand_curr_device].name) {
48 printf("%s: invalid nand device\n", __func__);
52 nand = &nand_info[nand_curr_device];
54 if (op == DFU_OP_READ) {
55 ret = nand_read_skip_bad(nand, start, &count, &actual,
58 nand_erase_options_t opts;
60 memset(&opts, 0, sizeof(opts));
67 ret = nand_erase_opts(nand, &opts);
71 ret = nand_write_skip_bad(nand, start, &count, &actual,
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 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 int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s)
175 dfu->data.nand.ubi = 0;
176 dfu->dev_type = DFU_DEV_NAND;
177 st = strsep(&s, " ");
178 if (!strcmp(st, "raw")) {
179 dfu->layout = DFU_RAW_ADDR;
180 dfu->data.nand.start = simple_strtoul(s, &s, 16);
182 dfu->data.nand.size = simple_strtoul(s, &s, 16);
183 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
185 struct mtd_device *mtd_dev;
187 struct part_info *pi;
189 dfu->layout = DFU_RAW_ADDR;
191 dev = simple_strtoul(s, &s, 10);
193 part = simple_strtoul(s, &s, 10);
195 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
196 printf("using id '%s'\n", mtd_id);
200 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
202 printf("Could not locate '%s'\n", mtd_id);
206 dfu->data.nand.start = pi->offset;
207 dfu->data.nand.size = pi->size;
208 if (!strcmp(st, "partubi"))
209 dfu->data.nand.ubi = 1;
211 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
215 dfu->read_medium = dfu_read_medium_nand;
216 dfu->write_medium = dfu_write_medium_nand;
217 dfu->flush_medium = dfu_flush_medium_nand;