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 ***************************************************************************/
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, u8 *buffer, u32 offset, u32 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 =
43 .register_commands = ocl_register_commands,
44 .flash_bank_command = ocl_flash_bank_command,
46 .protect = ocl_protect,
49 .erase_check = ocl_erase_check,
50 .protect_check = ocl_protect_check,
52 .auto_probe = ocl_auto_probe
55 typedef struct ocl_priv_s
57 arm_jtag_t *jtag_info;
59 unsigned int bufalign;
62 static int ocl_register_commands(struct command_context_s *cmd_ctx)
67 static int ocl_erase_check(struct flash_bank_s *bank)
72 static int ocl_protect_check(struct flash_bank_s *bank)
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)
81 armv4_5_common_t *armv4_5;
82 arm7_9_common_t *arm7_9;
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)
94 ocl = bank->driver_priv = malloc(sizeof(ocl_priv_t));
95 ocl->jtag_info = &arm7_9->jtag_info;
102 static int ocl_erase(struct flash_bank_s *bank, int first, int last)
104 ocl_priv_t *ocl = bank->driver_priv;
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))
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))
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");
141 /* receive response */
142 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer+1, 1) != ERROR_OK))
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%08X", dcc_buffer[1]);
150 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08X", dcc_buffer[1]);
151 return ERROR_FLASH_OPERATION_FAILED;
157 static int ocl_protect(struct flash_bank_s *bank, int set, int first, int last)
162 static int ocl_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
164 ocl_priv_t *ocl = bank->driver_priv;
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(u32)*(ocl->buflen/4+3));
189 if (count + (offset % ocl->bufalign) > ocl->buflen)
190 runlen = ocl->buflen - (offset % ocl->bufalign);
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++)
208 *dcc_bufptr &= *(buffer++) | 0xffffff00;
211 *dcc_bufptr &= ((*(buffer++))<<8) | 0xffff00ff;
214 *dcc_bufptr &= ((*(buffer++))<<16) | 0xff00ffff;
217 *dcc_bufptr &= ((*(buffer++))<<24) | 0x00ffffff;
218 chksum ^= *(dcc_bufptr++);
219 *dcc_bufptr = 0xffffffff;
225 /* add the remaining word to checksum */
227 chksum ^= *(dcc_bufptr++);
229 *(dcc_bufptr++) = chksum;
232 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
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");
247 /* receive response */
248 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
254 if (dcc_buffer[0] != OCL_CMD_DONE)
256 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08X", dcc_buffer[0]);
258 return ERROR_FLASH_OPERATION_FAILED;
269 static int ocl_probe(struct flash_bank_s *bank)
271 ocl_priv_t *ocl = bank->driver_priv;
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))
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");
292 /* receive response */
293 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
296 if (dcc_buffer[0] != OCL_CMD_DONE)
298 LOG_ERROR("loader response to OCL_PROBE 0x%08X", 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))
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))
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))
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))
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)
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;
360 LOG_ERROR("buflen shall be divisible by 4");
361 return ERROR_FLASH_BANK_INVALID;
367 static int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size)
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;