3e397ebd6b70dd60f16fdfdef745f497cd06d354
[openocd.git] / src / flash / nand / fileio.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath <Dominic.Rath@gmx.de> *
3 * Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de> *
4 * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
5 * *
6 * Partially based on drivers/mtd/nand_ids.c from Linux. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "core.h"
28 #include "fileio.h"
29
30 static struct nand_ecclayout nand_oob_16 = {
31 .eccbytes = 6,
32 .eccpos = {0, 1, 2, 3, 6, 7},
33 .oobfree = {
34 {.offset = 8,
35 . length = 8}}
36 };
37
38 static struct nand_ecclayout nand_oob_64 = {
39 .eccbytes = 24,
40 .eccpos = {
41 40, 41, 42, 43, 44, 45, 46, 47,
42 48, 49, 50, 51, 52, 53, 54, 55,
43 56, 57, 58, 59, 60, 61, 62, 63},
44 .oobfree = {
45 {.offset = 2,
46 .length = 38}}
47 };
48
49 void nand_fileio_init(struct nand_fileio_state *state)
50 {
51 memset(state, 0, sizeof(*state));
52 state->oob_format = NAND_OOB_NONE;
53 }
54
55 int nand_fileio_start(struct command_context *cmd_ctx,
56 struct nand_device *nand, const char *filename, int filemode,
57 struct nand_fileio_state *state)
58 {
59 if (state->address % nand->page_size)
60 {
61 command_print(cmd_ctx, "only page-aligned addresses are supported");
62 return ERROR_COMMAND_SYNTAX_ERROR;
63 }
64
65 duration_start(&state->bench);
66
67 if (NULL != filename)
68 {
69 int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY);
70 if (ERROR_OK != retval)
71 {
72 const char *msg = (FILEIO_READ == filemode) ? "read" : "write";
73 command_print(cmd_ctx, "failed to open '%s' for %s access",
74 filename, msg);
75 return retval;
76 }
77 state->file_opened = true;
78 }
79
80 if (!(state->oob_format & NAND_OOB_ONLY))
81 {
82 state->page_size = nand->page_size;
83 state->page = malloc(nand->page_size);
84 }
85
86 if (state->oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW))
87 {
88 if (nand->page_size == 512)
89 {
90 state->oob_size = 16;
91 state->eccpos = nand_oob_16.eccpos;
92 }
93 else if (nand->page_size == 2048)
94 {
95 state->oob_size = 64;
96 state->eccpos = nand_oob_64.eccpos;
97 }
98 state->oob = malloc(state->oob_size);
99 }
100
101 return ERROR_OK;
102 }
103 int nand_fileio_cleanup(struct nand_fileio_state *state)
104 {
105 if (state->file_opened)
106 fileio_close(&state->fileio);
107
108 if (state->oob)
109 {
110 free(state->oob);
111 state->oob = NULL;
112 }
113 if (state->page)
114 {
115 free(state->page);
116 state->page = NULL;
117 }
118 return ERROR_OK;
119 }
120 int nand_fileio_finish(struct nand_fileio_state *state)
121 {
122 nand_fileio_cleanup(state);
123 return duration_measure(&state->bench);
124 }
125
126 COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
127 struct nand_device **dev, enum fileio_access filemode,
128 bool need_size, bool sw_ecc)
129 {
130 nand_fileio_init(state);
131
132 unsigned minargs = need_size ? 4 : 3;
133 if (CMD_ARGC < minargs)
134 return ERROR_COMMAND_SYNTAX_ERROR;
135
136 struct nand_device *nand;
137 int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &nand);
138 if (ERROR_OK != retval)
139 return retval;
140
141 if (NULL == nand->device)
142 {
143 command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
144 return ERROR_OK;
145 }
146
147 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], state->address);
148 if (need_size)
149 {
150 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], state->size);
151 if (state->size % nand->page_size)
152 {
153 command_print(CMD_CTX, "only page-aligned sizes are supported");
154 return ERROR_COMMAND_SYNTAX_ERROR;
155 }
156 }
157
158 if (CMD_ARGC > minargs)
159 {
160 for (unsigned i = minargs; i < CMD_ARGC; i++)
161 {
162 if (!strcmp(CMD_ARGV[i], "oob_raw"))
163 state->oob_format |= NAND_OOB_RAW;
164 else if (!strcmp(CMD_ARGV[i], "oob_only"))
165 state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
166 else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc"))
167 state->oob_format |= NAND_OOB_SW_ECC;
168 else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc_kw"))
169 state->oob_format |= NAND_OOB_SW_ECC_KW;
170 else
171 {
172 command_print(CMD_CTX, "unknown option: %s", CMD_ARGV[i]);
173 return ERROR_COMMAND_SYNTAX_ERROR;
174 }
175 }
176 }
177
178 retval = nand_fileio_start(CMD_CTX, nand, CMD_ARGV[1], filemode, state);
179 if (ERROR_OK != retval)
180 return retval;
181
182 if (!need_size)
183 state->size = state->fileio.size;
184
185 *dev = nand;
186
187 return ERROR_OK;
188 }
189
190 /**
191 * @returns If no error occurred, returns number of bytes consumed;
192 * otherwise, returns a negative error code.)
193 */
194 int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
195 {
196 size_t total_read = 0;
197 size_t one_read;
198
199 if (NULL != s->page)
200 {
201 fileio_read(&s->fileio, s->page_size, s->page, &one_read);
202 if (one_read < s->page_size)
203 memset(s->page + one_read, 0xff, s->page_size - one_read);
204 total_read += one_read;
205 }
206
207 if (s->oob_format & NAND_OOB_SW_ECC)
208 {
209 uint8_t ecc[3];
210 memset(s->oob, 0xff, s->oob_size);
211 for (uint32_t i = 0, j = 0; i < s->page_size; i += 256)
212 {
213 nand_calculate_ecc(nand, s->page + i, ecc);
214 s->oob[s->eccpos[j++]] = ecc[0];
215 s->oob[s->eccpos[j++]] = ecc[1];
216 s->oob[s->eccpos[j++]] = ecc[2];
217 }
218 }
219 else if (s->oob_format & NAND_OOB_SW_ECC_KW)
220 {
221 /*
222 * In this case eccpos is not used as
223 * the ECC data is always stored contigously
224 * at the end of the OOB area. It consists
225 * of 10 bytes per 512-byte data block.
226 */
227 uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
228 memset(s->oob, 0xff, s->oob_size);
229 for (uint32_t i = 0; i < s->page_size; i += 512)
230 {
231 nand_calculate_ecc_kw(nand, s->page + i, ecc);
232 ecc += 10;
233 }
234 }
235 else if (NULL != s->oob)
236 {
237 fileio_read(&s->fileio, s->oob_size, s->oob, &one_read);
238 if (one_read < s->oob_size)
239 memset(s->oob + one_read, 0xff, s->oob_size - one_read);
240 total_read += one_read;
241 }
242 return total_read;
243 }
244