5504841b544a5a71deef2082ce075b7857168d22
[fw/openocd] / 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, see <http://www.gnu.org/licenses/>. *
20  ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "core.h"
27 #include "fileio.h"
28
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}
35         }
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         },
45         .oobfree = {
46                 {.offset = 2,
47                  .length = 38}
48         }
49 };
50
51 void nand_fileio_init(struct nand_fileio_state *state)
52 {
53         memset(state, 0, sizeof(*state));
54         state->oob_format = NAND_OOB_NONE;
55 }
56
57 int nand_fileio_start(struct command_invocation *cmd,
58         struct nand_device *nand, const char *filename, int filemode,
59         struct nand_fileio_state *state)
60 {
61         if (state->address % nand->page_size) {
62                 command_print(cmd, "only page-aligned addresses are supported");
63                 return ERROR_COMMAND_SYNTAX_ERROR;
64         }
65
66         duration_start(&state->bench);
67
68         if (NULL != filename) {
69                 int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY);
70                 if (retval != ERROR_OK) {
71                         const char *msg = (filemode == FILEIO_READ) ? "read" : "write";
72                         command_print(cmd, "failed to open '%s' for %s access",
73                                 filename, msg);
74                         return retval;
75                 }
76                 state->file_opened = true;
77         }
78
79         if (!(state->oob_format & NAND_OOB_ONLY)) {
80                 state->page_size = nand->page_size;
81                 state->page = malloc(nand->page_size);
82         }
83
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;
91                 }
92                 state->oob = malloc(state->oob_size);
93         }
94
95         return ERROR_OK;
96 }
97 int nand_fileio_cleanup(struct nand_fileio_state *state)
98 {
99         if (state->file_opened)
100                 fileio_close(state->fileio);
101
102         free(state->oob);
103         state->oob = NULL;
104
105         free(state->page);
106         state->page = NULL;
107         return ERROR_OK;
108 }
109 int nand_fileio_finish(struct nand_fileio_state *state)
110 {
111         nand_fileio_cleanup(state);
112         return duration_measure(&state->bench);
113 }
114
115 COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
116         struct nand_device **dev, enum fileio_access filemode,
117         bool need_size, bool sw_ecc)
118 {
119         nand_fileio_init(state);
120
121         unsigned minargs = need_size ? 4 : 3;
122         if (CMD_ARGC < minargs)
123                 return ERROR_COMMAND_SYNTAX_ERROR;
124
125         struct nand_device *nand;
126         int retval = CALL_COMMAND_HANDLER(nand_command_get_device, 0, &nand);
127         if (retval != ERROR_OK)
128                 return retval;
129
130         if (NULL == nand->device) {
131                 command_print(CMD, "#%s: not probed", CMD_ARGV[0]);
132                 return ERROR_NAND_DEVICE_NOT_PROBED;
133         }
134
135         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], state->address);
136         if (need_size) {
137                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], state->size);
138                 if (state->size % nand->page_size) {
139                         command_print(CMD, "only page-aligned sizes are supported");
140                         return ERROR_COMMAND_SYNTAX_ERROR;
141                 }
142         }
143
144         if (CMD_ARGC > minargs) {
145                 for (unsigned i = minargs; i < CMD_ARGC; i++) {
146                         if (!strcmp(CMD_ARGV[i], "oob_raw"))
147                                 state->oob_format |= NAND_OOB_RAW;
148                         else if (!strcmp(CMD_ARGV[i], "oob_only"))
149                                 state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
150                         else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc"))
151                                 state->oob_format |= NAND_OOB_SW_ECC;
152                         else if (sw_ecc && !strcmp(CMD_ARGV[i], "oob_softecc_kw"))
153                                 state->oob_format |= NAND_OOB_SW_ECC_KW;
154                         else {
155                                 command_print(CMD, "unknown option: %s", CMD_ARGV[i]);
156                                 return ERROR_COMMAND_SYNTAX_ERROR;
157                         }
158                 }
159         }
160
161         retval = nand_fileio_start(CMD, nand, CMD_ARGV[1], filemode, state);
162         if (retval != ERROR_OK)
163                 return retval;
164
165         if (!need_size) {
166                 size_t filesize;
167                 retval = fileio_size(state->fileio, &filesize);
168                 if (retval != ERROR_OK)
169                         return retval;
170                 state->size = filesize;
171         }
172
173         *dev = nand;
174
175         return ERROR_OK;
176 }
177
178 /**
179  * @returns If no error occurred, returns number of bytes consumed;
180  * otherwise, returns a negative error code.)
181  */
182 int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
183 {
184         size_t total_read = 0;
185         size_t one_read;
186
187         if (NULL != s->page) {
188                 fileio_read(s->fileio, s->page_size, s->page, &one_read);
189                 if (one_read < s->page_size)
190                         memset(s->page + one_read, 0xff, s->page_size - one_read);
191                 total_read += one_read;
192         }
193
194         if (s->oob_format & NAND_OOB_SW_ECC) {
195                 uint8_t ecc[3];
196                 memset(s->oob, 0xff, s->oob_size);
197                 for (uint32_t i = 0, j = 0; i < s->page_size; i += 256) {
198                         nand_calculate_ecc(nand, s->page + i, ecc);
199                         s->oob[s->eccpos[j++]] = ecc[0];
200                         s->oob[s->eccpos[j++]] = ecc[1];
201                         s->oob[s->eccpos[j++]] = ecc[2];
202                 }
203         } else if (s->oob_format & NAND_OOB_SW_ECC_KW)   {
204                 /*
205                  * In this case eccpos is not used as
206                  * the ECC data is always stored contiguously
207                  * at the end of the OOB area.  It consists
208                  * of 10 bytes per 512-byte data block.
209                  */
210                 uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
211                 memset(s->oob, 0xff, s->oob_size);
212                 for (uint32_t i = 0; i < s->page_size; i += 512) {
213                         nand_calculate_ecc_kw(nand, s->page + i, ecc);
214                         ecc += 10;
215                 }
216         } else if (NULL != s->oob)   {
217                 fileio_read(s->fileio, s->oob_size, s->oob, &one_read);
218                 if (one_read < s->oob_size)
219                         memset(s->oob + one_read, 0xff, s->oob_size - one_read);
220                 total_read += one_read;
221         }
222         return total_read;
223 }