1 /***************************************************************************
2 * Copyright (C) 2007 by Pavel Chromy *
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. *
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. *
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 ***************************************************************************/
24 #include "replacements.h"
31 #include "binarybuffer.h"
33 #include "embeddedice.h"
34 #include "arm7_9_common.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 =
54 .register_commands = ocl_register_commands,
55 .flash_bank_command = ocl_flash_bank_command,
57 .protect = ocl_protect,
60 .erase_check = ocl_erase_check,
61 .protect_check = ocl_protect_check,
63 .auto_probe = ocl_auto_probe
67 typedef struct ocl_priv_s
69 arm_jtag_t *jtag_info;
75 int ocl_register_commands(struct command_context_s *cmd_ctx)
81 int ocl_erase_check(struct flash_bank_s *bank)
87 int ocl_protect_check(struct flash_bank_s *bank)
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)
97 armv4_5_common_t *armv4_5;
98 arm7_9_common_t *arm7_9;
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)
110 ocl = bank->driver_priv = malloc(sizeof(ocl_priv_t));
111 ocl->jtag_info = &arm7_9->jtag_info;
119 int ocl_erase(struct flash_bank_s *bank, int first, int last)
121 ocl_priv_t *ocl = bank->driver_priv;
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))
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))
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");
158 /* receive response */
159 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer+1, 1) != ERROR_OK))
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]);
167 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08lX", dcc_buffer[1]);
168 return ERROR_FLASH_OPERATION_FAILED;
175 int ocl_protect(struct flash_bank_s *bank, int set, int first, int last)
181 int ocl_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
183 ocl_priv_t *ocl = bank->driver_priv;
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));
208 if (count + (offset % ocl->bufalign) > ocl->buflen)
209 runlen = ocl->buflen - (offset % ocl->bufalign);
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++)
227 *dcc_bufptr &= *(buffer++) | 0xffffff00;
230 *dcc_bufptr &= ((*(buffer++))<<8) | 0xffff00ff;
233 *dcc_bufptr &= ((*(buffer++))<<16) | 0xff00ffff;
236 *dcc_bufptr &= ((*(buffer++))<<24) | 0x00ffffff;
237 chksum ^= *(dcc_bufptr++);
238 *dcc_bufptr = 0xffffffff;
244 /* add the remaining word to checksum */
246 chksum ^= *(dcc_bufptr++);
248 *(dcc_bufptr++) = chksum;
251 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
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");
266 /* receive response */
267 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
273 if (dcc_buffer[0] != OCL_CMD_DONE)
275 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08lX", dcc_buffer[0]);
277 return ERROR_FLASH_OPERATION_FAILED;
289 int ocl_probe(struct flash_bank_s *bank)
291 ocl_priv_t *ocl = bank->driver_priv;
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))
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");
312 /* receive response */
313 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
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))
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))
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))
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))
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)
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;
380 LOG_ERROR("buflen shall be divisible by 4");
381 return ERROR_FLASH_BANK_INVALID;
388 int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size)
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;