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
66 typedef struct ocl_priv_s
68 arm_jtag_t *jtag_info;
73 int ocl_register_commands(struct command_context_s *cmd_ctx)
78 int ocl_erase_check(struct flash_bank_s *bank)
83 int ocl_protect_check(struct flash_bank_s *bank)
88 /* flash_bank ocl 0 0 0 0 <target#> */
89 int ocl_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct flash_bank_s *bank)
92 armv4_5_common_t *armv4_5;
93 arm7_9_common_t *arm7_9;
98 LOG_WARNING("incomplete flash_bank ocl configuration");
99 return ERROR_FLASH_BANK_INVALID;
102 if ((retval = arm7_9_get_arch_pointers(bank->target, &armv4_5, &arm7_9)) != ERROR_OK)
105 ocl = bank->driver_priv = malloc(sizeof(ocl_priv_t));
106 ocl->jtag_info = &arm7_9->jtag_info;
113 int ocl_erase(struct flash_bank_s *bank, int first, int last)
115 ocl_priv_t *ocl = bank->driver_priv;
119 /* check preconditions */
120 if (bank->num_sectors == 0)
121 return ERROR_FLASH_BANK_NOT_PROBED;
123 if (bank->target->state != TARGET_RUNNING)
125 LOG_ERROR("target has to be running to communicate with the loader");
126 return ERROR_TARGET_NOT_RUNNING;
129 if ((first == 0) && (last == bank->num_sectors - 1))
131 dcc_buffer[0] = OCL_ERASE_ALL;
132 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
137 dcc_buffer[0] = OCL_ERASE_BLOCK;
138 dcc_buffer[1] = first;
139 dcc_buffer[2] = last;
140 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3) != ERROR_OK))
144 /* wait for response, fixed timeout of 1 s */
145 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
147 if (retval == ERROR_TARGET_TIMEOUT)
148 LOG_ERROR("loader not responding");
152 /* receive response */
153 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer+1, 1) != ERROR_OK))
156 if (dcc_buffer[1] != OCL_CMD_DONE)
158 if (dcc_buffer[0] == OCL_ERASE_ALL)
159 LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08X", dcc_buffer[1]);
161 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08X", dcc_buffer[1]);
162 return ERROR_FLASH_OPERATION_FAILED;
168 int ocl_protect(struct flash_bank_s *bank, int set, int first, int last)
173 int ocl_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
175 ocl_priv_t *ocl = bank->driver_priv;
185 /* check preconditions */
186 if (ocl->buflen == 0 || ocl->bufalign==0)
187 return ERROR_FLASH_BANK_NOT_PROBED;
189 if (bank->target->state != TARGET_RUNNING)
191 LOG_ERROR("target has to be running to communicate with the loader");
192 return ERROR_TARGET_NOT_RUNNING;
195 /* allocate buffer for max. ocl buffer + overhead */
196 dcc_buffer = malloc(sizeof(u32)*(ocl->buflen/4+3));
200 if (count + (offset % ocl->bufalign) > ocl->buflen)
201 runlen = ocl->buflen - (offset % ocl->bufalign);
205 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
206 dcc_buffer[1] = offset;
207 dcc_bufptr = &dcc_buffer[2];
209 *dcc_bufptr = 0xffffffff;
210 byteofs = (offset % ocl->bufalign) % 4;
211 chksum = OCL_CHKS_INIT;
213 /* copy data to DCC buffer in proper byte order and properly aligned */
214 for (i=0; i<runlen; i++)
219 *dcc_bufptr &= *(buffer++) | 0xffffff00;
222 *dcc_bufptr &= ((*(buffer++))<<8) | 0xffff00ff;
225 *dcc_bufptr &= ((*(buffer++))<<16) | 0xff00ffff;
228 *dcc_bufptr &= ((*(buffer++))<<24) | 0x00ffffff;
229 chksum ^= *(dcc_bufptr++);
230 *dcc_bufptr = 0xffffffff;
236 /* add the remaining word to checksum */
238 chksum ^= *(dcc_bufptr++);
240 *(dcc_bufptr++) = chksum;
243 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
249 /* wait for response, fixed timeout of 1 s */
250 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
252 if (retval == ERROR_TARGET_TIMEOUT)
253 LOG_ERROR("loader not responding");
258 /* receive response */
259 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
265 if (dcc_buffer[0] != OCL_CMD_DONE)
267 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08X", dcc_buffer[0]);
269 return ERROR_FLASH_OPERATION_FAILED;
280 int ocl_probe(struct flash_bank_s *bank)
282 ocl_priv_t *ocl = bank->driver_priv;
288 /* purge pending data in DCC */
289 embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
291 dcc_buffer[0] = OCL_PROBE;
292 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
295 /* wait for response, fixed timeout of 1 s */
296 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
298 if (retval == ERROR_TARGET_TIMEOUT)
299 LOG_ERROR("loader not responding");
303 /* receive response */
304 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
307 if (dcc_buffer[0] != OCL_CMD_DONE)
309 LOG_ERROR("loader response to OCL_PROBE 0x%08X", dcc_buffer[0]);
310 return ERROR_FLASH_OPERATION_FAILED;
313 /* receive and fill in parameters, detection of loader is important, receive it one by one */
314 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
315 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
317 bank->base = dcc_buffer[0];
319 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
320 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
322 bank->size = dcc_buffer[0];
324 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
325 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
327 bank->num_sectors = dcc_buffer[0];
329 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
330 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
332 ocl->buflen = dcc_buffer[0] & 0xffff;
333 ocl->bufalign = dcc_buffer[0] >> 16;
335 bank->sectors = realloc(bank->sectors, sizeof(flash_sector_t)*bank->num_sectors);
336 if (bank->num_sectors == 0)
338 LOG_ERROR("number of sectors shall be non zero value");
339 return ERROR_FLASH_BANK_INVALID;
341 if (bank->size % bank->num_sectors) {
342 LOG_ERROR("bank size not divisible by number of sectors");
343 return ERROR_FLASH_BANK_INVALID;
345 sectsize = bank->size / bank->num_sectors;
346 for (i=0; i<bank->num_sectors; i++)
348 bank->sectors[i].offset = i * sectsize;
349 bank->sectors[i].size = sectsize;
350 bank->sectors[i].is_erased = -1;
351 bank->sectors[i].is_protected = -1;
354 if (ocl->bufalign == 0)
357 if (ocl->buflen == 0)
359 LOG_ERROR("buflen shall be non zero value");
360 return ERROR_FLASH_BANK_INVALID;
363 if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign))
365 LOG_ERROR("buflen is not multiple of bufalign");
366 return ERROR_FLASH_BANK_INVALID;
371 LOG_ERROR("buflen shall be divisible by 4");
372 return ERROR_FLASH_BANK_INVALID;
378 int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size)
383 int ocl_auto_probe(struct flash_bank_s *bank)
385 ocl_priv_t *ocl = bank->driver_priv;
387 if (ocl->buflen == 0 || ocl->bufalign==0)
388 return ERROR_FLASH_BANK_NOT_PROBED;