- updated docs for ft2232_vid_pid command
[openocd.git] / src / flash / ocl.c
blob4f3b506cbbb361ff48dd939fa287e3ffaf0f2369
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 "replacements.h"
26 #include "ocl.h"
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"
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
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);
51 flash_driver_t ocl_flash =
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
67 typedef struct ocl_priv_s
69 arm_jtag_t *jtag_info;
70 int buflen;
71 int bufalign;
72 } ocl_priv_t;
75 int ocl_register_commands(struct command_context_s *cmd_ctx)
77 return ERROR_OK;
81 int ocl_erase_check(struct flash_bank_s *bank)
83 return ERROR_OK;
87 int ocl_protect_check(struct flash_bank_s *bank)
89 return ERROR_OK;
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)
96 int retval;
97 armv4_5_common_t *armv4_5;
98 arm7_9_common_t *arm7_9;
99 ocl_priv_t *ocl;
101 if (argc < 6)
103 LOG_WARNING("incomplete flash_bank ocl configuration");
104 return ERROR_FLASH_BANK_INVALID;
107 if ((retval = arm7_9_get_arch_pointers(bank->target, &armv4_5, &arm7_9)) != ERROR_OK)
108 return retval;
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;
115 return ERROR_OK;
119 int ocl_erase(struct flash_bank_s *bank, int first, int last)
121 ocl_priv_t *ocl = bank->driver_priv;
122 int retval;
123 u32 dcc_buffer[3];
125 /* check preconditions */
126 if (bank->num_sectors == 0)
127 return ERROR_FLASH_BANK_NOT_PROBED;
129 if (bank->target->state != TARGET_RUNNING)
131 LOG_ERROR("target has to be running to communicate with the loader");
132 return ERROR_TARGET_NOT_RUNNING;
135 if ((first == 0) && (last == bank->num_sectors - 1))
137 dcc_buffer[0] = OCL_ERASE_ALL;
138 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
139 return retval;
141 else
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;
150 /* wait for response, fixed timeout of 1 s */
151 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
153 if (retval == ERROR_TARGET_TIMEOUT)
154 LOG_ERROR("loader not responding");
155 return retval;
158 /* receive response */
159 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer+1, 1) != ERROR_OK))
160 return retval;
162 if (dcc_buffer[1] != OCL_CMD_DONE)
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;
171 return ERROR_OK;
175 int ocl_protect(struct flash_bank_s *bank, int set, int first, int last)
177 return ERROR_OK;
181 int ocl_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
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;
191 int i;
193 /* check preconditions */
194 if (ocl->buflen == 0 || ocl->bufalign==0)
195 return ERROR_FLASH_BANK_NOT_PROBED;
197 if (bank->target->state != TARGET_RUNNING)
199 LOG_ERROR("target has to be running to communicate with the loader");
200 return ERROR_TARGET_NOT_RUNNING;
203 /* allocate buffer for max. ocl buffer + overhead */
204 dcc_buffer = malloc(sizeof(u32)*(ocl->buflen/4+3));
206 while (count)
208 if (count + (offset % ocl->bufalign) > ocl->buflen)
209 runlen = ocl->buflen - (offset % ocl->bufalign);
210 else
211 runlen = count;
213 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
214 dcc_buffer[1] = offset;
215 dcc_bufptr = &dcc_buffer[2];
217 *dcc_bufptr = 0xffffffff;
218 byteofs = (offset % ocl->bufalign) % 4;
219 chksum = OCL_CHKS_INIT;
221 /* copy data to DCC buffer in proper byte order and properly aligned */
222 for (i=0; i<runlen; i++)
224 switch (byteofs++)
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;
244 /* add the remaining word to checksum */
245 if (byteofs)
246 chksum ^= *(dcc_bufptr++);
248 *(dcc_bufptr++) = chksum;
250 /* send the data */
251 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
253 free(dcc_buffer);
254 return retval;
257 /* wait for response, fixed timeout of 1 s */
258 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
260 if (retval == ERROR_TARGET_TIMEOUT)
261 LOG_ERROR("loader not responding");
262 free(dcc_buffer);
263 return retval;
266 /* receive response */
267 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
269 free(dcc_buffer);
270 return retval;
273 if (dcc_buffer[0] != OCL_CMD_DONE)
275 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08lX", dcc_buffer[0]);
276 free(dcc_buffer);
277 return ERROR_FLASH_OPERATION_FAILED;
280 count -= runlen;
281 offset += runlen;
284 free(dcc_buffer);
285 return ERROR_OK;
289 int ocl_probe(struct flash_bank_s *bank)
291 ocl_priv_t *ocl = bank->driver_priv;
292 int retval;
293 u32 dcc_buffer[1];
294 int sectsize;
295 int i;
297 /* purge pending data in DCC */
298 embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
300 dcc_buffer[0] = OCL_PROBE;
301 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
302 return retval;
304 /* wait for response, fixed timeout of 1 s */
305 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
307 if (retval == ERROR_TARGET_TIMEOUT)
308 LOG_ERROR("loader not responding");
309 return retval;
312 /* receive response */
313 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
314 return retval;
316 if (dcc_buffer[0] != OCL_CMD_DONE)
318 LOG_ERROR("loader response to OCL_PROBE 0x%08lX", dcc_buffer[0]);
319 return ERROR_FLASH_OPERATION_FAILED;
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];
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];
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];
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;
344 bank->sectors = realloc(bank->sectors, sizeof(flash_sector_t)*bank->num_sectors);
345 if (bank->num_sectors == 0)
347 LOG_ERROR("number of sectors shall be non zero value");
348 return ERROR_FLASH_BANK_INVALID;
350 if (bank->size % bank->num_sectors) {
351 LOG_ERROR("bank size not divisible by number of sectors");
352 return ERROR_FLASH_BANK_INVALID;
354 sectsize = bank->size / bank->num_sectors;
355 for (i=0; i<bank->num_sectors; i++)
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;
363 if (ocl->bufalign == 0)
364 ocl->bufalign = 1;
366 if (ocl->buflen == 0)
368 LOG_ERROR("buflen shall be non zero value");
369 return ERROR_FLASH_BANK_INVALID;
372 if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign))
374 LOG_ERROR("buflen is not multiple of bufalign");
375 return ERROR_FLASH_BANK_INVALID;
378 if (ocl->buflen % 4)
380 LOG_ERROR("buflen shall be divisible by 4");
381 return ERROR_FLASH_BANK_INVALID;
384 return ERROR_OK;
388 int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size)
390 return ERROR_OK;
394 int ocl_auto_probe(struct flash_bank_s *bank)
396 ocl_priv_t *ocl = bank->driver_priv;
398 if (ocl->buflen == 0 || ocl->bufalign==0)
399 return ERROR_FLASH_BANK_NOT_PROBED;
401 return ERROR_OK;