befa4e102ca79186a0bcfecd814eb36451d01a6b
[openocd.git] / src / flash / 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, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25
26 #include "ocl.h"
27
28 #include "flash.h"
29 #include "target.h"
30 #include "log.h"
31 #include "binarybuffer.h"
32 #include "types.h"
33 #include "embeddedice.h"
34 #include "arm7_9_common.h"
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 int ocl_register_commands(struct command_context_s *cmd_ctx);
41 int ocl_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
42 int ocl_erase(struct flash_bank_s *bank, int first, int last);
43 int ocl_protect(struct flash_bank_s *bank, int set, int first, int last);
44 int ocl_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count);
45 int ocl_probe(struct flash_bank_s *bank);
46 int ocl_erase_check(struct flash_bank_s *bank);
47 int ocl_protect_check(struct flash_bank_s *bank);
48 int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size);
49 int ocl_auto_probe(struct flash_bank_s *bank);
50
51 flash_driver_t ocl_flash =
52 {
53 .name = "ocl",
54 .register_commands = ocl_register_commands,
55 .flash_bank_command = ocl_flash_bank_command,
56 .erase = ocl_erase,
57 .protect = ocl_protect,
58 .write = ocl_write,
59 .probe = ocl_probe,
60 .erase_check = ocl_erase_check,
61 .protect_check = ocl_protect_check,
62 .info = ocl_info,
63 .auto_probe = ocl_auto_probe
64 };
65
66
67 typedef struct ocl_priv_s
68 {
69 arm_jtag_t *jtag_info;
70 int buflen;
71 int bufalign;
72 } ocl_priv_t;
73
74
75 int ocl_register_commands(struct command_context_s *cmd_ctx)
76 {
77 return ERROR_OK;
78 }
79
80
81 int ocl_erase_check(struct flash_bank_s *bank)
82 {
83 return ERROR_OK;
84 }
85
86
87 int ocl_protect_check(struct flash_bank_s *bank)
88 {
89 return ERROR_OK;
90 }
91
92
93 /* flash_bank ocl 0 0 0 0 <target#> */
94 int ocl_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
95 {
96 int retval;
97 armv4_5_common_t *armv4_5;
98 arm7_9_common_t *arm7_9;
99 ocl_priv_t *ocl;
100
101 if (argc < 6)
102 {
103 LOG_WARNING("incomplete flash_bank ocl configuration");
104 return ERROR_FLASH_BANK_INVALID;
105 }
106
107 if ((retval = arm7_9_get_arch_pointers(bank->target, &armv4_5, &arm7_9)) != ERROR_OK)
108 return retval;
109
110 ocl = bank->driver_priv = malloc(sizeof(ocl_priv_t));
111 ocl->jtag_info = &arm7_9->jtag_info;
112 ocl->buflen = 0;
113 ocl->bufalign = 1;
114
115 return ERROR_OK;
116 }
117
118
119 int ocl_erase(struct flash_bank_s *bank, int first, int last)
120 {
121 ocl_priv_t *ocl = bank->driver_priv;
122 int retval;
123 u32 dcc_buffer[3];
124
125 /* check preconditions */
126 if (bank->num_sectors == 0)
127 return ERROR_FLASH_BANK_NOT_PROBED;
128
129 if (bank->target->state != TARGET_RUNNING)
130 {
131 LOG_ERROR("target has to be running to communicate with the loader");
132 return ERROR_TARGET_NOT_RUNNING;
133 }
134
135 if ((first == 0) && (last == bank->num_sectors - 1))
136 {
137 dcc_buffer[0] = OCL_ERASE_ALL;
138 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
139 return retval;
140 }
141 else
142 {
143 dcc_buffer[0] = OCL_ERASE_BLOCK;
144 dcc_buffer[1] = first;
145 dcc_buffer[2] = last;
146 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3) != ERROR_OK))
147 return retval;
148 }
149
150 /* wait for response, fixed timeout of 1 s */
151 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
152 {
153 if (retval == ERROR_TARGET_TIMEOUT)
154 LOG_ERROR("loader not responding");
155 return retval;
156 }
157
158 /* receive response */
159 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer+1, 1) != ERROR_OK))
160 return retval;
161
162 if (dcc_buffer[1] != OCL_CMD_DONE)
163 {
164 if (dcc_buffer[0] == OCL_ERASE_ALL)
165 LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08lX", dcc_buffer[1]);
166 else
167 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08lX", dcc_buffer[1]);
168 return ERROR_FLASH_OPERATION_FAILED;
169 }
170
171 return ERROR_OK;
172 }
173
174
175 int ocl_protect(struct flash_bank_s *bank, int set, int first, int last)
176 {
177 return ERROR_OK;
178 }
179
180
181 int ocl_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
182 {
183 ocl_priv_t *ocl = bank->driver_priv;
184 int retval;
185 u32 *dcc_buffer;
186 u32 *dcc_bufptr;
187 int byteofs;
188 int runlen;
189 u32 chksum;
190
191 int i;
192
193 /* check preconditions */
194 if (ocl->buflen == 0 || ocl->bufalign==0)
195 return ERROR_FLASH_BANK_NOT_PROBED;
196
197 if (bank->target->state != TARGET_RUNNING)
198 {
199 LOG_ERROR("target has to be running to communicate with the loader");
200 return ERROR_TARGET_NOT_RUNNING;
201 }
202
203 /* allocate buffer for max. ocl buffer + overhead */
204 dcc_buffer = malloc(sizeof(u32)*(ocl->buflen/4+3));
205
206 while (count)
207 {
208 if (count + (offset % ocl->bufalign) > ocl->buflen)
209 runlen = ocl->buflen - (offset % ocl->bufalign);
210 else
211 runlen = count;
212
213 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
214 dcc_buffer[1] = offset;
215 dcc_bufptr = &dcc_buffer[2];
216
217 *dcc_bufptr = 0xffffffff;
218 byteofs = (offset % ocl->bufalign) % 4;
219 chksum = OCL_CHKS_INIT;
220
221 /* copy data to DCC buffer in proper byte order and properly aligned */
222 for (i=0; i<runlen; i++)
223 {
224 switch (byteofs++)
225 {
226 case 0:
227 *dcc_bufptr &= *(buffer++) | 0xffffff00;
228 break;
229 case 1:
230 *dcc_bufptr &= ((*(buffer++))<<8) | 0xffff00ff;
231 break;
232 case 2:
233 *dcc_bufptr &= ((*(buffer++))<<16) | 0xff00ffff;
234 break;
235 case 3:
236 *dcc_bufptr &= ((*(buffer++))<<24) | 0x00ffffff;
237 chksum ^= *(dcc_bufptr++);
238 *dcc_bufptr = 0xffffffff;
239 byteofs = 0;
240 break;
241 }
242 }
243
244 /* add the remaining word to checksum */
245 if (byteofs)
246 chksum ^= *(dcc_bufptr++);
247
248 *(dcc_bufptr++) = chksum;
249
250 /* send the data */
251 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
252 {
253 free(dcc_buffer);
254 return retval;
255 }
256
257 /* wait for response, fixed timeout of 1 s */
258 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
259 {
260 if (retval == ERROR_TARGET_TIMEOUT)
261 LOG_ERROR("loader not responding");
262 free(dcc_buffer);
263 return retval;
264 }
265
266 /* receive response */
267 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
268 {
269 free(dcc_buffer);
270 return retval;
271 }
272
273 if (dcc_buffer[0] != OCL_CMD_DONE)
274 {
275 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08lX", dcc_buffer[0]);
276 free(dcc_buffer);
277 return ERROR_FLASH_OPERATION_FAILED;
278 }
279
280 count -= runlen;
281 offset += runlen;
282 }
283
284 free(dcc_buffer);
285 return ERROR_OK;
286 }
287
288
289 int ocl_probe(struct flash_bank_s *bank)
290 {
291 ocl_priv_t *ocl = bank->driver_priv;
292 int retval;
293 u32 dcc_buffer[1];
294 int sectsize;
295 int i;
296
297 /* purge pending data in DCC */
298 embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
299
300 dcc_buffer[0] = OCL_PROBE;
301 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
302 return retval;
303
304 /* wait for response, fixed timeout of 1 s */
305 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
306 {
307 if (retval == ERROR_TARGET_TIMEOUT)
308 LOG_ERROR("loader not responding");
309 return retval;
310 }
311
312 /* receive response */
313 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
314 return retval;
315
316 if (dcc_buffer[0] != OCL_CMD_DONE)
317 {
318 LOG_ERROR("loader response to OCL_PROBE 0x%08lX", dcc_buffer[0]);
319 return ERROR_FLASH_OPERATION_FAILED;
320 }
321
322 /* receive and fill in parameters, detection of loader is important, receive it one by one */
323 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
324 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
325 return retval;
326 bank->base = dcc_buffer[0];
327
328 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
329 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
330 return retval;
331 bank->size = dcc_buffer[0];
332
333 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
334 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
335 return retval;
336 bank->num_sectors = dcc_buffer[0];
337
338 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
339 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
340 return retval;
341 ocl->buflen = dcc_buffer[0] & 0xffff;
342 ocl->bufalign = dcc_buffer[0] >> 16;
343
344 bank->sectors = realloc(bank->sectors, sizeof(flash_sector_t)*bank->num_sectors);
345 if (bank->num_sectors == 0)
346 {
347 LOG_ERROR("number of sectors shall be non zero value");
348 return ERROR_FLASH_BANK_INVALID;
349 }
350 if (bank->size % bank->num_sectors) {
351 LOG_ERROR("bank size not divisible by number of sectors");
352 return ERROR_FLASH_BANK_INVALID;
353 }
354 sectsize = bank->size / bank->num_sectors;
355 for (i=0; i<bank->num_sectors; i++)
356 {
357 bank->sectors[i].offset = i * sectsize;
358 bank->sectors[i].size = sectsize;
359 bank->sectors[i].is_erased = -1;
360 bank->sectors[i].is_protected = -1;
361 }
362
363 if (ocl->bufalign == 0)
364 ocl->bufalign = 1;
365
366 if (ocl->buflen == 0)
367 {
368 LOG_ERROR("buflen shall be non zero value");
369 return ERROR_FLASH_BANK_INVALID;
370 }
371
372 if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign))
373 {
374 LOG_ERROR("buflen is not multiple of bufalign");
375 return ERROR_FLASH_BANK_INVALID;
376 }
377
378 if (ocl->buflen % 4)
379 {
380 LOG_ERROR("buflen shall be divisible by 4");
381 return ERROR_FLASH_BANK_INVALID;
382 }
383
384 return ERROR_OK;
385 }
386
387
388 int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size)
389 {
390 return ERROR_OK;
391 }
392
393
394 int ocl_auto_probe(struct flash_bank_s *bank)
395 {
396 ocl_priv_t *ocl = bank->driver_priv;
397
398 if (ocl->buflen == 0 || ocl->bufalign==0)
399 return ERROR_FLASH_BANK_NOT_PROBED;
400
401 return ERROR_OK;
402 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)