NEWS: mention switch to git!
[openocd.git] / src / flash / ocl.c
bloba2dac50a31b363a35e62c9903ff05d9711788062
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
24 #include "ocl.h"
25 #include "flash.h"
26 #include "embeddedice.h"
29 static int ocl_register_commands(struct command_context_s *cmd_ctx);
30 static int ocl_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank);
31 static int ocl_erase(struct flash_bank_s *bank, int first, int last);
32 static int ocl_protect(struct flash_bank_s *bank, int set, int first, int last);
33 static int ocl_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count);
34 static int ocl_probe(struct flash_bank_s *bank);
35 static int ocl_erase_check(struct flash_bank_s *bank);
36 static int ocl_protect_check(struct flash_bank_s *bank);
37 static int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size);
38 static int ocl_auto_probe(struct flash_bank_s *bank);
40 flash_driver_t ocl_flash =
42 .name = "ocl",
43 .register_commands = ocl_register_commands,
44 .flash_bank_command = ocl_flash_bank_command,
45 .erase = ocl_erase,
46 .protect = ocl_protect,
47 .write = ocl_write,
48 .probe = ocl_probe,
49 .erase_check = ocl_erase_check,
50 .protect_check = ocl_protect_check,
51 .info = ocl_info,
52 .auto_probe = ocl_auto_probe
55 typedef struct ocl_priv_s
57 arm_jtag_t *jtag_info;
58 unsigned int buflen;
59 unsigned int bufalign;
60 } ocl_priv_t;
62 static int ocl_register_commands(struct command_context_s *cmd_ctx)
64 return ERROR_OK;
67 static int ocl_erase_check(struct flash_bank_s *bank)
69 return ERROR_OK;
72 static int ocl_protect_check(struct flash_bank_s *bank)
74 return ERROR_OK;
77 /* flash_bank ocl 0 0 0 0 <target#> */
78 static int ocl_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
80 int retval;
81 armv4_5_common_t *armv4_5;
82 arm7_9_common_t *arm7_9;
83 ocl_priv_t *ocl;
85 if (argc < 6)
87 LOG_WARNING("incomplete flash_bank ocl configuration");
88 return ERROR_FLASH_BANK_INVALID;
91 if ((retval = arm7_9_get_arch_pointers(bank->target, &armv4_5, &arm7_9)) != ERROR_OK)
92 return retval;
94 ocl = bank->driver_priv = malloc(sizeof(ocl_priv_t));
95 ocl->jtag_info = &arm7_9->jtag_info;
96 ocl->buflen = 0;
97 ocl->bufalign = 1;
99 return ERROR_OK;
102 static int ocl_erase(struct flash_bank_s *bank, int first, int last)
104 ocl_priv_t *ocl = bank->driver_priv;
105 int retval;
106 uint32_t dcc_buffer[3];
108 /* check preconditions */
109 if (bank->num_sectors == 0)
110 return ERROR_FLASH_BANK_NOT_PROBED;
112 if (bank->target->state != TARGET_RUNNING)
114 LOG_ERROR("target has to be running to communicate with the loader");
115 return ERROR_TARGET_NOT_RUNNING;
118 if ((first == 0) && (last == bank->num_sectors - 1))
120 dcc_buffer[0] = OCL_ERASE_ALL;
121 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
122 return retval;
124 else
126 dcc_buffer[0] = OCL_ERASE_BLOCK;
127 dcc_buffer[1] = first;
128 dcc_buffer[2] = last;
129 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3) != ERROR_OK))
130 return retval;
133 /* wait for response, fixed timeout of 1 s */
134 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
136 if (retval == ERROR_TARGET_TIMEOUT)
137 LOG_ERROR("loader not responding");
138 return retval;
141 /* receive response */
142 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer + 1, 1) != ERROR_OK))
143 return retval;
145 if (dcc_buffer[1] != OCL_CMD_DONE)
147 if (dcc_buffer[0] == OCL_ERASE_ALL)
148 LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
149 else
150 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
151 return ERROR_FLASH_OPERATION_FAILED;
154 return ERROR_OK;
157 static int ocl_protect(struct flash_bank_s *bank, int set, int first, int last)
159 return ERROR_OK;
162 static int ocl_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
164 ocl_priv_t *ocl = bank->driver_priv;
165 int retval;
166 uint32_t *dcc_buffer;
167 uint32_t *dcc_bufptr;
168 int byteofs;
169 int runlen;
170 uint32_t chksum;
172 int i;
174 /* check preconditions */
175 if (ocl->buflen == 0 || ocl->bufalign == 0)
176 return ERROR_FLASH_BANK_NOT_PROBED;
178 if (bank->target->state != TARGET_RUNNING)
180 LOG_ERROR("target has to be running to communicate with the loader");
181 return ERROR_TARGET_NOT_RUNNING;
184 /* allocate buffer for max. ocl buffer + overhead */
185 dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4 + 3));
187 while (count)
189 if (count + (offset % ocl->bufalign) > ocl->buflen)
190 runlen = ocl->buflen - (offset % ocl->bufalign);
191 else
192 runlen = count;
194 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
195 dcc_buffer[1] = offset;
196 dcc_bufptr = &dcc_buffer[2];
198 *dcc_bufptr = 0xffffffff;
199 byteofs = (offset % ocl->bufalign) % 4;
200 chksum = OCL_CHKS_INIT;
202 /* copy data to DCC buffer in proper byte order and properly aligned */
203 for (i = 0; i < runlen; i++)
205 switch (byteofs++)
207 case 0:
208 *dcc_bufptr &= *(buffer++) | 0xffffff00;
209 break;
210 case 1:
211 *dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
212 break;
213 case 2:
214 *dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
215 break;
216 case 3:
217 *dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
218 chksum ^= *(dcc_bufptr++);
219 *dcc_bufptr = 0xffffffff;
220 byteofs = 0;
221 break;
225 /* add the remaining word to checksum */
226 if (byteofs)
227 chksum ^= *(dcc_bufptr++);
229 *(dcc_bufptr++) = chksum;
231 /* send the data */
232 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
234 free(dcc_buffer);
235 return retval;
238 /* wait for response, fixed timeout of 1 s */
239 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
241 if (retval == ERROR_TARGET_TIMEOUT)
242 LOG_ERROR("loader not responding");
243 free(dcc_buffer);
244 return retval;
247 /* receive response */
248 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
250 free(dcc_buffer);
251 return retval;
254 if (dcc_buffer[0] != OCL_CMD_DONE)
256 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
257 free(dcc_buffer);
258 return ERROR_FLASH_OPERATION_FAILED;
261 count -= runlen;
262 offset += runlen;
265 free(dcc_buffer);
266 return ERROR_OK;
269 static int ocl_probe(struct flash_bank_s *bank)
271 ocl_priv_t *ocl = bank->driver_priv;
272 int retval;
273 uint32_t dcc_buffer[1];
274 int sectsize;
275 int i;
277 /* purge pending data in DCC */
278 embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
280 dcc_buffer[0] = OCL_PROBE;
281 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
282 return retval;
284 /* wait for response, fixed timeout of 1 s */
285 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
287 if (retval == ERROR_TARGET_TIMEOUT)
288 LOG_ERROR("loader not responding");
289 return retval;
292 /* receive response */
293 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
294 return retval;
296 if (dcc_buffer[0] != OCL_CMD_DONE)
298 LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
299 return ERROR_FLASH_OPERATION_FAILED;
302 /* receive and fill in parameters, detection of loader is important, receive it one by one */
303 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
304 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
305 return retval;
306 bank->base = dcc_buffer[0];
308 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
309 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
310 return retval;
311 bank->size = dcc_buffer[0];
313 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
314 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
315 return retval;
316 bank->num_sectors = dcc_buffer[0];
318 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
319 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
320 return retval;
321 ocl->buflen = dcc_buffer[0] & 0xffff;
322 ocl->bufalign = dcc_buffer[0] >> 16;
324 bank->sectors = realloc(bank->sectors, sizeof(flash_sector_t)*bank->num_sectors);
325 if (bank->num_sectors == 0)
327 LOG_ERROR("number of sectors shall be non zero value");
328 return ERROR_FLASH_BANK_INVALID;
330 if (bank->size % bank->num_sectors) {
331 LOG_ERROR("bank size not divisible by number of sectors");
332 return ERROR_FLASH_BANK_INVALID;
334 sectsize = bank->size / bank->num_sectors;
335 for (i = 0; i < bank->num_sectors; i++)
337 bank->sectors[i].offset = i * sectsize;
338 bank->sectors[i].size = sectsize;
339 bank->sectors[i].is_erased = -1;
340 bank->sectors[i].is_protected = -1;
343 if (ocl->bufalign == 0)
344 ocl->bufalign = 1;
346 if (ocl->buflen == 0)
348 LOG_ERROR("buflen shall be non zero value");
349 return ERROR_FLASH_BANK_INVALID;
352 if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign))
354 LOG_ERROR("buflen is not multiple of bufalign");
355 return ERROR_FLASH_BANK_INVALID;
358 if (ocl->buflen % 4)
360 LOG_ERROR("buflen shall be divisible by 4");
361 return ERROR_FLASH_BANK_INVALID;
364 return ERROR_OK;
367 static int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size)
369 return ERROR_OK;
372 static int ocl_auto_probe(struct flash_bank_s *bank)
374 ocl_priv_t *ocl = bank->driver_priv;
376 if (ocl->buflen == 0 || ocl->bufalign == 0)
377 return ERROR_FLASH_BANK_NOT_PROBED;
379 return ERROR_OK;