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 int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s)
144 dfu->dev_type = DFU_DEV_NAND;
145 st = strsep(&s, " ");
146 if (!strcmp(st, "raw")) {
147 dfu->layout = DFU_RAW_ADDR;
148 dfu->data.nand.start = simple_strtoul(s, &s, 16);
150 dfu->data.nand.size = simple_strtoul(s, &s, 16);
151 } else if (!strcmp(st, "part")) {
153 struct mtd_device *mtd_dev;
155 struct part_info *pi;
157 dfu->layout = DFU_RAW_ADDR;
159 dev = simple_strtoul(s, &s, 10);
161 part = simple_strtoul(s, &s, 10);
163 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
164 printf("using id '%s'\n", mtd_id);
168 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
170 printf("Could not locate '%s'\n", mtd_id);
174 dfu->data.nand.start = pi->offset;
175 dfu->data.nand.size = pi->size;
178 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
182 dfu->read_medium = dfu_read_medium_nand;
183 dfu->write_medium = dfu_write_medium_nand;