Merge branch 'next'
[u-boot/qq2440-u-boot.git] / drivers / dfu / dfu_nand.c
blob2d07097e850af70544d95f5481d6d5af9ef26f5c
1 /*
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+
13 #include <common.h>
14 #include <malloc.h>
15 #include <errno.h>
16 #include <div64.h>
17 #include <dfu.h>
18 #include <linux/mtd/mtd.h>
19 #include <jffs2/load_kernel.h>
20 #include <nand.h>
22 static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu,
23 u64 offset, void *buf, long *len)
25 loff_t start, lim;
26 size_t count, actual;
27 int ret;
28 nand_info_t *nand;
30 /* if buf == NULL return total size of the area */
31 if (buf == NULL) {
32 *len = dfu->data.nand.size;
33 return 0;
36 start = dfu->data.nand.start + offset + dfu->bad_skip;
37 lim = dfu->data.nand.start + dfu->data.nand.size - start;
38 count = *len;
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__);
44 return -1;
47 nand = &nand_info[nand_curr_device];
49 if (op == DFU_OP_READ) {
50 ret = nand_read_skip_bad(nand, start, &count, &actual,
51 lim, buf);
52 } else {
53 nand_erase_options_t opts;
55 memset(&opts, 0, sizeof(opts));
56 opts.offset = start;
57 opts.length = count;
58 opts.spread = 1;
59 opts.quiet = 1;
60 opts.lim = lim;
61 /* first erase */
62 ret = nand_erase_opts(nand, &opts);
63 if (ret)
64 return ret;
65 /* then write */
66 ret = nand_write_skip_bad(nand, start, &count, &actual,
67 lim, buf, 0);
70 if (ret != 0) {
71 printf("%s: nand_%s_skip_bad call failed at %llx!\n",
72 __func__, op == DFU_OP_READ ? "read" : "write",
73 start);
74 return ret;
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.
82 if (actual > count)
83 dfu->bad_skip += actual - count;
85 return ret;
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)
103 int ret = -1;
105 switch (dfu->layout) {
106 case DFU_RAW_ADDR:
107 ret = nand_block_write(dfu, offset, buf, len);
108 break;
109 default:
110 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
111 dfu_get_layout(dfu->layout));
114 return ret;
117 static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
118 long *len)
120 int ret = -1;
122 switch (dfu->layout) {
123 case DFU_RAW_ADDR:
124 *len = dfu->data.nand.size;
125 ret = nand_block_read(dfu, offset, buf, len);
126 break;
127 default:
128 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
129 dfu_get_layout(dfu->layout));
132 return ret;
135 static int dfu_flush_medium_nand(struct dfu_entity *dfu)
137 int ret = 0;
139 /* in case of ubi partition, erase rest of the partition */
140 if (dfu->data.nand.ubi) {
141 nand_info_t *nand;
142 nand_erase_options_t opts;
144 if (nand_curr_device < 0 ||
145 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
146 !nand_info[nand_curr_device].name) {
147 printf("%s: invalid nand device\n", __func__);
148 return -1;
151 nand = &nand_info[nand_curr_device];
153 memset(&opts, 0, sizeof(opts));
154 opts.offset = dfu->data.nand.start + dfu->offset +
155 dfu->bad_skip;
156 opts.length = dfu->data.nand.start +
157 dfu->data.nand.size - opts.offset;
158 ret = nand_erase_opts(nand, &opts);
159 if (ret != 0)
160 printf("Failure erase: %d\n", ret);
163 return ret;
166 int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s)
168 char *st;
169 int ret, dev, part;
171 dfu->data.nand.ubi = 0;
172 dfu->dev_type = DFU_DEV_NAND;
173 st = strsep(&s, " ");
174 if (!strcmp(st, "raw")) {
175 dfu->layout = DFU_RAW_ADDR;
176 dfu->data.nand.start = simple_strtoul(s, &s, 16);
177 s++;
178 dfu->data.nand.size = simple_strtoul(s, &s, 16);
179 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
180 char mtd_id[32];
181 struct mtd_device *mtd_dev;
182 u8 part_num;
183 struct part_info *pi;
185 dfu->layout = DFU_RAW_ADDR;
187 dev = simple_strtoul(s, &s, 10);
188 s++;
189 part = simple_strtoul(s, &s, 10);
191 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
192 printf("using id '%s'\n", mtd_id);
194 mtdparts_init();
196 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
197 if (ret != 0) {
198 printf("Could not locate '%s'\n", mtd_id);
199 return -1;
202 dfu->data.nand.start = pi->offset;
203 dfu->data.nand.size = pi->size;
204 if (!strcmp(st, "partubi"))
205 dfu->data.nand.ubi = 1;
206 } else {
207 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
208 return -1;
211 dfu->read_medium = dfu_read_medium_nand;
212 dfu->write_medium = dfu_write_medium_nand;
213 dfu->flush_medium = dfu_flush_medium_nand;
215 /* initial state */
216 dfu->inited = 0;
218 return 0;