flash/nor: at91samd modified to use real erase sector size
[openocd.git] / src / flash / nand / fileio.c
blob64c32c0a8a025e3b5568ab52605db861a88a196d
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, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include "core.h"
27 #include "fileio.h"
29 static struct nand_ecclayout nand_oob_16 = {
30 .eccbytes = 6,
31 .eccpos = {0, 1, 2, 3, 6, 7},
32 .oobfree = {
33 {.offset = 8,
34 .length = 8}
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
45 .oobfree = {
46 {.offset = 2,
47 .length = 38}
51 void nand_fileio_init(struct nand_fileio_state *state)
53 memset(state, 0, sizeof(*state));
54 state->oob_format = NAND_OOB_NONE;
57 int nand_fileio_start(struct command_context *cmd_ctx,
58 struct nand_device *nand, const char *filename, int filemode,
59 struct nand_fileio_state *state)
61 if (state->address % nand->page_size) {
62 command_print(cmd_ctx, "only page-aligned addresses are supported");
63 return ERROR_COMMAND_SYNTAX_ERROR;
66 duration_start(&state->bench);
68 if (NULL != filename) {
69 int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY);
70 if (ERROR_OK != retval) {
71 const char *msg = (FILEIO_READ == filemode) ? "read" : "write";
72 command_print(cmd_ctx, "failed to open '%s' for %s access",
73 filename, msg);
74 return retval;
76 state->file_opened = true;
79 if (!(state->oob_format & NAND_OOB_ONLY)) {
80 state->page_size = nand->page_size;
81 state->page = malloc(nand->page_size);
84 if (state->oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW)) {
85 if (nand->page_size == 512) {
86 state->oob_size = 16;
87 state->eccpos = nand_oob_16.eccpos;
88 } else if (nand->page_size == 2048) {
89 state->oob_size = 64;
90 state->eccpos = nand_oob_64.eccpos;
92 state->oob = malloc(state->oob_size);
95 return ERROR_OK;
97 int nand_fileio_cleanup(struct nand_fileio_state *state)
99 if (state->file_opened)
100 fileio_close(state->fileio);
102 if (state->oob) {
103 free(state->oob);
104 state->oob = NULL;
106 if (state->page) {
107 free(state->page);
108 state->page = NULL;
110 return ERROR_OK;
112 int nand_fileio_finish(struct nand_fileio_state *state)
114 nand_fileio_cleanup(state);
115 return duration_measure(&state->bench);
118 COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
119 struct nand_device **dev, enum fileio_access filemode,
120 bool need_size, bool sw_ecc)
122 nand_fileio_init(state);
124 unsigned minargs = need_size ? 4 : 3;
125 if (CMD_ARGC < minargs)
126 return ERROR_COMMAND_SYNTAX_ERROR;
128 struct nand_device *nand;
129 int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &nand);
130 if (ERROR_OK != retval)
131 return retval;
133 if (NULL == nand->device) {
134 command_print(CMD_CTX, "#%s: not probed", CMD_ARGV[0]);
135 return ERROR_NAND_DEVICE_NOT_PROBED;
138 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], state->address);
139 if (need_size) {
140 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], state->size);
141 if (state->size % nand->page_size) {
142 command_print(CMD_CTX, "only page-aligned sizes are supported");
143 return ERROR_COMMAND_SYNTAX_ERROR;
147 if (CMD_ARGC > minargs) {
148 for (unsigned i = minargs; i < CMD_ARGC; i++) {
149 if (!strcmp(CMD_ARGV[i], "oob_raw"))
150 state->oob_format |= NAND_OOB_RAW;
151 else if (!strcmp(CMD_ARGV[i], "oob_only"))
152 state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
153 else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc"))
154 state->oob_format |= NAND_OOB_SW_ECC;
155 else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc_kw"))
156 state->oob_format |= NAND_OOB_SW_ECC_KW;
157 else {
158 command_print(CMD_CTX, "unknown option: %s", CMD_ARGV[i]);
159 return ERROR_COMMAND_SYNTAX_ERROR;
164 retval = nand_fileio_start(CMD_CTX, nand, CMD_ARGV[1], filemode, state);
165 if (ERROR_OK != retval)
166 return retval;
168 if (!need_size) {
169 size_t filesize;
170 retval = fileio_size(state->fileio, &filesize);
171 if (retval != ERROR_OK)
172 return retval;
173 state->size = filesize;
176 *dev = nand;
178 return ERROR_OK;
182 * @returns If no error occurred, returns number of bytes consumed;
183 * otherwise, returns a negative error code.)
185 int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
187 size_t total_read = 0;
188 size_t one_read;
190 if (NULL != s->page) {
191 fileio_read(s->fileio, s->page_size, s->page, &one_read);
192 if (one_read < s->page_size)
193 memset(s->page + one_read, 0xff, s->page_size - one_read);
194 total_read += one_read;
197 if (s->oob_format & NAND_OOB_SW_ECC) {
198 uint8_t ecc[3];
199 memset(s->oob, 0xff, s->oob_size);
200 for (uint32_t i = 0, j = 0; i < s->page_size; i += 256) {
201 nand_calculate_ecc(nand, s->page + i, ecc);
202 s->oob[s->eccpos[j++]] = ecc[0];
203 s->oob[s->eccpos[j++]] = ecc[1];
204 s->oob[s->eccpos[j++]] = ecc[2];
206 } else if (s->oob_format & NAND_OOB_SW_ECC_KW) {
208 * In this case eccpos is not used as
209 * the ECC data is always stored contigously
210 * at the end of the OOB area. It consists
211 * of 10 bytes per 512-byte data block.
213 uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
214 memset(s->oob, 0xff, s->oob_size);
215 for (uint32_t i = 0; i < s->page_size; i += 512) {
216 nand_calculate_ecc_kw(nand, s->page + i, ecc);
217 ecc += 10;
219 } else if (NULL != s->oob) {
220 fileio_read(s->fileio, s->oob_size, s->oob, &one_read);
221 if (one_read < s->oob_size)
222 memset(s->oob + one_read, 0xff, s->oob_size - one_read);
223 total_read += one_read;
225 return total_read;