blob: ccdbef6b75f26fe78fe5fc52438d797bad5b3b46 [file] [log] [blame]
Pantelis Antoniouc6631762013-03-14 05:32:52 +00001/*
2 * dfu_nand.c -- DFU for NAND routines.
3 *
4 * Copyright (C) 2012-2013 Texas Instruments, Inc.
5 *
6 * Based on dfu_mmc.c which is:
7 * Copyright (C) 2012 Samsung Electronics
8 * author: Lukasz Majewski <l.majewski@samsung.com>
9 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Pantelis Antoniouc6631762013-03-14 05:32:52 +000011 */
12
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>
21
Afzal Mohammed5a127c82013-09-18 01:14:50 +053022static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antoniouc6631762013-03-14 05:32:52 +000023 u64 offset, void *buf, long *len)
24{
25 loff_t start, lim;
26 size_t count, actual;
27 int ret;
28 nand_info_t *nand;
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
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;
45 }
46
47 nand = &nand_info[nand_curr_device];
48
Heiko Schochera67cc372013-06-24 18:50:40 +020049 if (op == DFU_OP_READ) {
Pantelis Antoniouc6631762013-03-14 05:32:52 +000050 ret = nand_read_skip_bad(nand, start, &count, &actual,
51 lim, buf);
Heiko Schochera67cc372013-06-24 18:50:40 +020052 } else {
53 nand_erase_options_t opts;
54
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 */
Pantelis Antoniouc6631762013-03-14 05:32:52 +000066 ret = nand_write_skip_bad(nand, start, &count, &actual,
67 lim, buf, 0);
Heiko Schochera67cc372013-06-24 18:50:40 +020068 }
Pantelis Antoniouc6631762013-03-14 05:32:52 +000069
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;
75 }
76
77 /*
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.
81 */
82 if (actual > count)
83 dfu->bad_skip += actual - count;
84
85 return ret;
86}
87
88static inline int nand_block_write(struct dfu_entity *dfu,
89 u64 offset, void *buf, long *len)
90{
91 return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
92}
93
94static inline int nand_block_read(struct dfu_entity *dfu,
95 u64 offset, void *buf, long *len)
96{
97 return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
98}
99
100static int dfu_write_medium_nand(struct dfu_entity *dfu,
101 u64 offset, void *buf, long *len)
102{
103 int ret = -1;
104
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));
112 }
113
114 return ret;
115}
116
117static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
118 long *len)
119{
120 int ret = -1;
121
122 switch (dfu->layout) {
123 case DFU_RAW_ADDR:
Bo Shen47d79de2013-11-07 14:29:41 +0800124 *len = dfu->data.nand.size;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000125 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));
130 }
131
132 return ret;
133}
134
Heiko Schocher815c30b2013-07-25 06:43:11 +0200135static int dfu_flush_medium_nand(struct dfu_entity *dfu)
136{
137 int ret = 0;
138
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;
143
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;
149 }
150
151 nand = &nand_info[nand_curr_device];
152
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);
161 }
162
163 return ret;
164}
165
Heiko Schocherfc25fa22014-04-11 07:59:47 +0200166unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu)
167{
168 /*
169 * Currently, Poll Timeout != 0 is only needed on nand
170 * ubi partition, as the not used sectors need an erase
171 */
172 if (dfu->data.nand.ubi)
173 return DFU_MANIFEST_POLL_TIMEOUT;
174
175 return DFU_DEFAULT_POLL_TIMEOUT;
176}
177
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000178int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s)
179{
180 char *st;
181 int ret, dev, part;
182
Heiko Schocher815c30b2013-07-25 06:43:11 +0200183 dfu->data.nand.ubi = 0;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000184 dfu->dev_type = DFU_DEV_NAND;
185 st = strsep(&s, " ");
186 if (!strcmp(st, "raw")) {
187 dfu->layout = DFU_RAW_ADDR;
188 dfu->data.nand.start = simple_strtoul(s, &s, 16);
189 s++;
190 dfu->data.nand.size = simple_strtoul(s, &s, 16);
Heiko Schocher815c30b2013-07-25 06:43:11 +0200191 } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000192 char mtd_id[32];
193 struct mtd_device *mtd_dev;
194 u8 part_num;
195 struct part_info *pi;
196
197 dfu->layout = DFU_RAW_ADDR;
198
199 dev = simple_strtoul(s, &s, 10);
200 s++;
201 part = simple_strtoul(s, &s, 10);
202
203 sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
204 printf("using id '%s'\n", mtd_id);
205
206 mtdparts_init();
207
208 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
209 if (ret != 0) {
210 printf("Could not locate '%s'\n", mtd_id);
211 return -1;
212 }
213
214 dfu->data.nand.start = pi->offset;
215 dfu->data.nand.size = pi->size;
Heiko Schocher815c30b2013-07-25 06:43:11 +0200216 if (!strcmp(st, "partubi"))
217 dfu->data.nand.ubi = 1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000218 } else {
219 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
220 return -1;
221 }
222
223 dfu->read_medium = dfu_read_medium_nand;
224 dfu->write_medium = dfu_write_medium_nand;
Heiko Schocher815c30b2013-07-25 06:43:11 +0200225 dfu->flush_medium = dfu_flush_medium_nand;
Heiko Schocherfc25fa22014-04-11 07:59:47 +0200226 dfu->poll_timeout = dfu_polltimeout_nand;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000227
228 /* initial state */
229 dfu->inited = 0;
230
231 return 0;
232}