]> Git Repo - J-u-boot.git/blame - drivers/dfu/dfu_nand.c
global: Convert simple_strtoul() with decimal to dectoul()
[J-u-boot.git] / drivers / dfu / dfu_nand.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
c6631764
PA
2/*
3 * dfu_nand.c -- DFU for NAND routines.
4 *
5 * Copyright (C) 2012-2013 Texas Instruments, Inc.
6 *
7 * Based on dfu_mmc.c which is:
8 * Copyright (C) 2012 Samsung Electronics
9 * author: Lukasz Majewski <[email protected]>
c6631764
PA
10 */
11
12#include <common.h>
f7ae49fc 13#include <log.h>
c6631764
PA
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>
21
5a127c84 22static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu,
c6631764
PA
23 u64 offset, void *buf, long *len)
24{
25 loff_t start, lim;
26 size_t count, actual;
27 int ret;
151c06ec 28 struct mtd_info *mtd;
c6631764
PA
29
30 /* if buf == NULL return total size of the area */
31 if (buf == NULL) {
32 *len = dfu->data.nand.size;
33 return 0;
34 }
35
36 start = dfu->data.nand.start + offset + dfu->bad_skip;
37 lim = dfu->data.nand.start + dfu->data.nand.size - start;
38 count = *len;
39
16520189
GS
40 mtd = get_nand_dev_by_index(nand_curr_device);
41
c6631764
PA
42 if (nand_curr_device < 0 ||
43 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
16520189 44 !mtd) {
c6631764
PA
45 printf("%s: invalid nand device\n", __func__);
46 return -1;
47 }
48
a67cc37e 49 if (op == DFU_OP_READ) {
151c06ec
SW
50 ret = nand_read_skip_bad(mtd, start, &count, &actual,
51 lim, buf);
a67cc37e
HS
52 } else {
53 nand_erase_options_t opts;
13cb7cc9 54 int write_flags = WITH_WR_VERIFY;
a67cc37e
HS
55
56 memset(&opts, 0, sizeof(opts));
57 opts.offset = start;
58 opts.length = count;
59 opts.spread = 1;
60 opts.quiet = 1;
61 opts.lim = lim;
62 /* first erase */
151c06ec 63 ret = nand_erase_opts(mtd, &opts);
a67cc37e
HS
64 if (ret)
65 return ret;
66 /* then write */
13cb7cc9
GR
67#ifdef CONFIG_DFU_NAND_TRIMFFS
68 if (dfu->data.nand.ubi)
69 write_flags |= WITH_DROP_FFS;
70#endif
151c06ec 71 ret = nand_write_skip_bad(mtd, start, &count, &actual,
13cb7cc9 72 lim, buf, write_flags);
a67cc37e 73 }
c6631764
PA
74
75 if (ret != 0) {
76 printf("%s: nand_%s_skip_bad call failed at %llx!\n",
77 __func__, op == DFU_OP_READ ? "read" : "write",
78 start);
79 return ret;
80 }
81
82 /*
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.
86 */
87 if (actual > count)
88 dfu->bad_skip += actual - count;
89
90 return ret;
91}
92
93static inline int nand_block_write(struct dfu_entity *dfu,
94 u64 offset, void *buf, long *len)
95{
96 return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
97}
98
99static inline int nand_block_read(struct dfu_entity *dfu,
100 u64 offset, void *buf, long *len)
101{
102 return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
103}
104
105static int dfu_write_medium_nand(struct dfu_entity *dfu,
106 u64 offset, void *buf, long *len)
107{
108 int ret = -1;
109
110 switch (dfu->layout) {
111 case DFU_RAW_ADDR:
112 ret = nand_block_write(dfu, offset, buf, len);
113 break;
114 default:
115 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
116 dfu_get_layout(dfu->layout));
117 }
118
119 return ret;
120}
121
15970d87 122int dfu_get_medium_size_nand(struct dfu_entity *dfu, u64 *size)
0e285b50 123{
4de51201
PD
124 *size = dfu->data.nand.size;
125
126 return 0;
0e285b50
SW
127}
128
c6631764
PA
129static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
130 long *len)
131{
132 int ret = -1;
133
134 switch (dfu->layout) {
135 case DFU_RAW_ADDR:
136 ret = nand_block_read(dfu, offset, buf, len);
137 break;
138 default:
139 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
140 dfu_get_layout(dfu->layout));
141 }
142
143 return ret;
144}
145
815c30b2
HS
146static int dfu_flush_medium_nand(struct dfu_entity *dfu)
147{
148 int ret = 0;
9ae63f46 149 u64 off;
815c30b2
HS
150
151 /* in case of ubi partition, erase rest of the partition */
152 if (dfu->data.nand.ubi) {
16520189 153 struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
815c30b2
HS
154 nand_erase_options_t opts;
155
156 if (nand_curr_device < 0 ||
157 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
16520189 158 !mtd) {
815c30b2
HS
159 printf("%s: invalid nand device\n", __func__);
160 return -1;
161 }
162
815c30b2 163 memset(&opts, 0, sizeof(opts));
9ae63f46
HS
164 off = dfu->offset;
165 if ((off & (mtd->erasesize - 1)) != 0) {
166 /*
167 * last write ended with unaligned length
168 * sector is erased, jump to next
169 */
170 off = off & ~((mtd->erasesize - 1));
171 off += mtd->erasesize;
172 }
173 opts.offset = dfu->data.nand.start + off +
815c30b2
HS
174 dfu->bad_skip;
175 opts.length = dfu->data.nand.start +
176 dfu->data.nand.size - opts.offset;
151c06ec 177 ret = nand_erase_opts(mtd, &opts);
815c30b2
HS
178 if (ret != 0)
179 printf("Failure erase: %d\n", ret);
180 }
181
182 return ret;
183}
184
fc25fa27
HS
185unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu)
186{
187 /*
188 * Currently, Poll Timeout != 0 is only needed on nand
189 * ubi partition, as the not used sectors need an erase
190 */
191 if (dfu->data.nand.ubi)
192 return DFU_MANIFEST_POLL_TIMEOUT;
193
194 return DFU_DEFAULT_POLL_TIMEOUT;
195}
196
dd64827e 197int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s)
c6631764
PA
198{
199 char *st;
200 int ret, dev, part;
201
815c30b2 202 dfu->data.nand.ubi = 0;
c6631764
PA
203 dfu->dev_type = DFU_DEV_NAND;
204 st = strsep(&s, " ");
205 if (!strcmp(st, "raw")) {
206 dfu->layout = DFU_RAW_ADDR;
7e5f460e 207 dfu->data.nand.start = hextoul(s, &s);
c6631764 208 s++;
7e5f460e 209 dfu->data.nand.size = hextoul(s, &s);
815c30b2 210 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
c6631764
PA
211 char mtd_id[32];
212 struct mtd_device *mtd_dev;
213 u8 part_num;
214 struct part_info *pi;
215
216 dfu->layout = DFU_RAW_ADDR;
217
0b1284eb 218 dev = dectoul(s, &s);
c6631764 219 s++;
0b1284eb 220 part = dectoul(s, &s);
c6631764
PA
221
222 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
0a815ff7 223 debug("using id '%s'\n", mtd_id);
c6631764
PA
224
225 mtdparts_init();
226
227 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
228 if (ret != 0) {
229 printf("Could not locate '%s'\n", mtd_id);
230 return -1;
231 }
232
233 dfu->data.nand.start = pi->offset;
234 dfu->data.nand.size = pi->size;
815c30b2
HS
235 if (!strcmp(st, "partubi"))
236 dfu->data.nand.ubi = 1;
c6631764
PA
237 } else {
238 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
239 return -1;
240 }
241
0e285b50 242 dfu->get_medium_size = dfu_get_medium_size_nand;
c6631764
PA
243 dfu->read_medium = dfu_read_medium_nand;
244 dfu->write_medium = dfu_write_medium_nand;
815c30b2 245 dfu->flush_medium = dfu_flush_medium_nand;
fc25fa27 246 dfu->poll_timeout = dfu_polltimeout_nand;
c6631764
PA
247
248 /* initial state */
249 dfu->inited = 0;
250
251 return 0;
252}
This page took 0.366132 seconds and 4 git commands to generate.