flash: nor: ath79: remove base calculation
[openocd.git] / src / flash / nor / ocl.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Pavel Chromy *
3 * chromy@asix.cz *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "imp.h"
24 #include "ocl.h"
25 #include <target/embeddedice.h>
26
27 struct ocl_priv {
28 struct arm_jtag *jtag_info;
29 unsigned int buflen;
30 unsigned int bufalign;
31 };
32
33 /* flash_bank ocl 0 0 0 0 <target#> */
34 FLASH_BANK_COMMAND_HANDLER(ocl_flash_bank_command)
35 {
36 struct arm7_9_common *arm7_9;
37 struct ocl_priv *ocl;
38
39 if (CMD_ARGC < 6)
40 return ERROR_COMMAND_SYNTAX_ERROR;
41
42 arm7_9 = target_to_arm7_9(bank->target);
43 if (!is_arm7_9(arm7_9))
44 return ERROR_TARGET_INVALID;
45
46 ocl = bank->driver_priv = malloc(sizeof(struct ocl_priv));
47 ocl->jtag_info = &arm7_9->jtag_info;
48 ocl->buflen = 0;
49 ocl->bufalign = 1;
50
51 return ERROR_OK;
52 }
53
54 static int ocl_erase(struct flash_bank *bank, int first, int last)
55 {
56 struct ocl_priv *ocl = bank->driver_priv;
57 int retval;
58 uint32_t dcc_buffer[3];
59
60 /* check preconditions */
61 if (bank->num_sectors == 0)
62 return ERROR_FLASH_BANK_NOT_PROBED;
63
64 if (bank->target->state != TARGET_RUNNING) {
65 LOG_ERROR("target has to be running to communicate with the loader");
66 return ERROR_TARGET_NOT_RUNNING;
67 }
68
69 if ((first == 0) && (last == bank->num_sectors - 1)) {
70 dcc_buffer[0] = OCL_ERASE_ALL;
71 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1);
72 if (retval != ERROR_OK)
73 return retval;
74 } else {
75 dcc_buffer[0] = OCL_ERASE_BLOCK;
76 dcc_buffer[1] = first;
77 dcc_buffer[2] = last;
78 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3);
79 if (retval != ERROR_OK)
80 return retval;
81 }
82
83 /* wait for response, fixed timeout of 1 s */
84 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
85 if (retval != ERROR_OK)
86 return retval;
87
88 /* receive response */
89 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer + 1, 1);
90 if (retval != ERROR_OK)
91 return retval;
92
93 if (dcc_buffer[1] != OCL_CMD_DONE) {
94 if (dcc_buffer[0] == OCL_ERASE_ALL)
95 LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
96 else
97 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
98 return ERROR_FLASH_OPERATION_FAILED;
99 }
100
101 return ERROR_OK;
102 }
103
104 static int ocl_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
105 {
106 struct ocl_priv *ocl = bank->driver_priv;
107 int retval;
108 uint32_t *dcc_buffer;
109 uint32_t *dcc_bufptr;
110 int byteofs;
111 int runlen;
112 uint32_t chksum;
113
114 int i;
115
116 /* check preconditions */
117 if (ocl->buflen == 0 || ocl->bufalign == 0)
118 return ERROR_FLASH_BANK_NOT_PROBED;
119
120 if (bank->target->state != TARGET_RUNNING) {
121 LOG_ERROR("target has to be running to communicate with the loader");
122 return ERROR_TARGET_NOT_RUNNING;
123 }
124
125 /* allocate buffer for max. ocl buffer + overhead */
126 dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4 + 3));
127
128 while (count) {
129 if (count + (offset % ocl->bufalign) > ocl->buflen)
130 runlen = ocl->buflen - (offset % ocl->bufalign);
131 else
132 runlen = count;
133
134 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
135 dcc_buffer[1] = offset;
136 dcc_bufptr = &dcc_buffer[2];
137
138 *dcc_bufptr = 0xffffffff;
139 byteofs = (offset % ocl->bufalign) % 4;
140 chksum = OCL_CHKS_INIT;
141
142 /* copy data to DCC buffer in proper byte order and properly aligned */
143 for (i = 0; i < runlen; i++) {
144 switch (byteofs++) {
145 case 0:
146 *dcc_bufptr &= *(buffer++) | 0xffffff00;
147 break;
148 case 1:
149 *dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
150 break;
151 case 2:
152 *dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
153 break;
154 case 3:
155 *dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
156 chksum ^= *(dcc_bufptr++);
157 *dcc_bufptr = 0xffffffff;
158 byteofs = 0;
159 break;
160 }
161 }
162
163 /* add the remaining word to checksum */
164 if (byteofs)
165 chksum ^= *(dcc_bufptr++);
166
167 *(dcc_bufptr++) = chksum;
168
169 /* send the data */
170 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer);
171 if (retval != ERROR_OK) {
172 free(dcc_buffer);
173 return retval;
174 }
175
176 /* wait for response, fixed timeout of 1 s */
177 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
178 if (retval != ERROR_OK) {
179 free(dcc_buffer);
180 return retval;
181 }
182
183 /* receive response */
184 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
185 if (retval != ERROR_OK) {
186 free(dcc_buffer);
187 return retval;
188 }
189
190 if (dcc_buffer[0] != OCL_CMD_DONE) {
191 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
192 free(dcc_buffer);
193 return ERROR_FLASH_OPERATION_FAILED;
194 }
195
196 count -= runlen;
197 offset += runlen;
198 }
199
200 free(dcc_buffer);
201 return ERROR_OK;
202 }
203
204 static int ocl_probe(struct flash_bank *bank)
205 {
206 struct ocl_priv *ocl = bank->driver_priv;
207 int retval;
208 uint32_t dcc_buffer[1];
209 int sectsize;
210 int i;
211
212 /* purge pending data in DCC */
213 embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
214
215 dcc_buffer[0] = OCL_PROBE;
216 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1);
217 if (retval != ERROR_OK)
218 return retval;
219
220 /* wait for response, fixed timeout of 1 s */
221 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
222 if (retval != ERROR_OK)
223 return retval;
224
225 /* receive response */
226 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
227 if (retval != ERROR_OK)
228 return retval;
229
230 if (dcc_buffer[0] != OCL_CMD_DONE) {
231 LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
232 return ERROR_FLASH_OPERATION_FAILED;
233 }
234
235 /* receive and fill in parameters, detection of loader is important, receive it one by one */
236 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
237 if (retval != ERROR_OK)
238 return retval;
239 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
240 if (retval != ERROR_OK)
241 return retval;
242 bank->base = dcc_buffer[0];
243
244 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
245 if (retval != ERROR_OK)
246 return retval;
247 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
248 if (retval != ERROR_OK)
249 return retval;
250 bank->size = dcc_buffer[0];
251
252 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
253 if (retval != ERROR_OK)
254 return retval;
255 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
256 if (retval != ERROR_OK)
257 return retval;
258 bank->num_sectors = dcc_buffer[0];
259
260 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
261 if (retval != ERROR_OK)
262 return retval;
263 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
264 if (retval != ERROR_OK)
265 return retval;
266 ocl->buflen = dcc_buffer[0] & 0xffff;
267 ocl->bufalign = dcc_buffer[0] >> 16;
268
269 bank->sectors = realloc(bank->sectors, sizeof(struct flash_sector)*bank->num_sectors);
270 if (bank->num_sectors == 0) {
271 LOG_ERROR("number of sectors shall be non zero value");
272 return ERROR_FLASH_BANK_INVALID;
273 }
274 if (bank->size % bank->num_sectors) {
275 LOG_ERROR("bank size not divisible by number of sectors");
276 return ERROR_FLASH_BANK_INVALID;
277 }
278 sectsize = bank->size / bank->num_sectors;
279 for (i = 0; i < bank->num_sectors; i++) {
280 bank->sectors[i].offset = i * sectsize;
281 bank->sectors[i].size = sectsize;
282 bank->sectors[i].is_erased = -1;
283 bank->sectors[i].is_protected = -1;
284 }
285
286 if (ocl->bufalign == 0)
287 ocl->bufalign = 1;
288
289 if (ocl->buflen == 0) {
290 LOG_ERROR("buflen shall be non zero value");
291 return ERROR_FLASH_BANK_INVALID;
292 }
293
294 if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign)) {
295 LOG_ERROR("buflen is not multiple of bufalign");
296 return ERROR_FLASH_BANK_INVALID;
297 }
298
299 if (ocl->buflen % 4) {
300 LOG_ERROR("buflen shall be divisible by 4");
301 return ERROR_FLASH_BANK_INVALID;
302 }
303
304 return ERROR_OK;
305 }
306
307 static int ocl_auto_probe(struct flash_bank *bank)
308 {
309 struct ocl_priv *ocl = bank->driver_priv;
310
311 if (ocl->buflen == 0 || ocl->bufalign == 0)
312 return ERROR_FLASH_BANK_NOT_PROBED;
313
314 return ERROR_OK;
315 }
316
317 struct flash_driver ocl_flash = {
318 .name = "ocl",
319 .flash_bank_command = ocl_flash_bank_command,
320 .erase = ocl_erase,
321 .write = ocl_write,
322 .read = default_flash_read,
323 .probe = ocl_probe,
324 .erase_check = default_flash_blank_check,
325 .auto_probe = ocl_auto_probe,
326 .free_driver_priv = default_flash_free_driver_priv,
327 };