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 ***************************************************************************/
27 #include <target/embeddedice.h>
30 struct arm_jtag *jtag_info;
32 unsigned int bufalign;
35 static int ocl_erase_check(struct flash_bank *bank)
40 static int ocl_protect_check(struct flash_bank *bank)
45 /* flash_bank ocl 0 0 0 0 <target#> */
46 FLASH_BANK_COMMAND_HANDLER(ocl_flash_bank_command)
48 struct arm7_9_common *arm7_9;
52 return ERROR_COMMAND_SYNTAX_ERROR;
54 arm7_9 = target_to_arm7_9(bank->target);
55 if (!is_arm7_9(arm7_9))
56 return ERROR_TARGET_INVALID;
58 ocl = bank->driver_priv = malloc(sizeof(struct ocl_priv));
59 ocl->jtag_info = &arm7_9->jtag_info;
66 static int ocl_erase(struct flash_bank *bank, int first, int last)
68 struct ocl_priv *ocl = bank->driver_priv;
70 uint32_t dcc_buffer[3];
72 /* check preconditions */
73 if (bank->num_sectors == 0)
74 return ERROR_FLASH_BANK_NOT_PROBED;
76 if (bank->target->state != TARGET_RUNNING) {
77 LOG_ERROR("target has to be running to communicate with the loader");
78 return ERROR_TARGET_NOT_RUNNING;
81 if ((first == 0) && (last == bank->num_sectors - 1)) {
82 dcc_buffer[0] = OCL_ERASE_ALL;
83 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1);
84 if (retval != ERROR_OK)
87 dcc_buffer[0] = OCL_ERASE_BLOCK;
88 dcc_buffer[1] = first;
90 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3);
91 if (retval != ERROR_OK)
95 /* wait for response, fixed timeout of 1 s */
96 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
97 if (retval != ERROR_OK)
100 /* receive response */
101 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer + 1, 1);
102 if (retval != ERROR_OK)
105 if (dcc_buffer[1] != OCL_CMD_DONE) {
106 if (dcc_buffer[0] == OCL_ERASE_ALL)
107 LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
109 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
110 return ERROR_FLASH_OPERATION_FAILED;
116 static int ocl_protect(struct flash_bank *bank, int set, int first, int last)
121 static int ocl_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
123 struct ocl_priv *ocl = bank->driver_priv;
125 uint32_t *dcc_buffer;
126 uint32_t *dcc_bufptr;
133 /* check preconditions */
134 if (ocl->buflen == 0 || ocl->bufalign == 0)
135 return ERROR_FLASH_BANK_NOT_PROBED;
137 if (bank->target->state != TARGET_RUNNING) {
138 LOG_ERROR("target has to be running to communicate with the loader");
139 return ERROR_TARGET_NOT_RUNNING;
142 /* allocate buffer for max. ocl buffer + overhead */
143 dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4 + 3));
146 if (count + (offset % ocl->bufalign) > ocl->buflen)
147 runlen = ocl->buflen - (offset % ocl->bufalign);
151 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
152 dcc_buffer[1] = offset;
153 dcc_bufptr = &dcc_buffer[2];
155 *dcc_bufptr = 0xffffffff;
156 byteofs = (offset % ocl->bufalign) % 4;
157 chksum = OCL_CHKS_INIT;
159 /* copy data to DCC buffer in proper byte order and properly aligned */
160 for (i = 0; i < runlen; i++) {
163 *dcc_bufptr &= *(buffer++) | 0xffffff00;
166 *dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
169 *dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
172 *dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
173 chksum ^= *(dcc_bufptr++);
174 *dcc_bufptr = 0xffffffff;
180 /* add the remaining word to checksum */
182 chksum ^= *(dcc_bufptr++);
184 *(dcc_bufptr++) = chksum;
187 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer);
188 if (retval != ERROR_OK) {
193 /* wait for response, fixed timeout of 1 s */
194 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
195 if (retval != ERROR_OK) {
200 /* receive response */
201 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
202 if (retval != ERROR_OK) {
207 if (dcc_buffer[0] != OCL_CMD_DONE) {
208 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
210 return ERROR_FLASH_OPERATION_FAILED;
221 static int ocl_probe(struct flash_bank *bank)
223 struct ocl_priv *ocl = bank->driver_priv;
225 uint32_t dcc_buffer[1];
229 /* purge pending data in DCC */
230 embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
232 dcc_buffer[0] = OCL_PROBE;
233 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1);
234 if (retval != ERROR_OK)
237 /* wait for response, fixed timeout of 1 s */
238 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
239 if (retval != ERROR_OK)
242 /* receive response */
243 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
244 if (retval != ERROR_OK)
247 if (dcc_buffer[0] != OCL_CMD_DONE) {
248 LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
249 return ERROR_FLASH_OPERATION_FAILED;
252 /* receive and fill in parameters, detection of loader is important, receive it one by one */
253 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
254 if (retval != ERROR_OK)
256 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
257 if (retval != ERROR_OK)
259 bank->base = dcc_buffer[0];
261 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
262 if (retval != ERROR_OK)
264 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
265 if (retval != ERROR_OK)
267 bank->size = dcc_buffer[0];
269 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
270 if (retval != ERROR_OK)
272 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
273 if (retval != ERROR_OK)
275 bank->num_sectors = dcc_buffer[0];
277 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
278 if (retval != ERROR_OK)
280 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
281 if (retval != ERROR_OK)
283 ocl->buflen = dcc_buffer[0] & 0xffff;
284 ocl->bufalign = dcc_buffer[0] >> 16;
286 bank->sectors = realloc(bank->sectors, sizeof(struct flash_sector)*bank->num_sectors);
287 if (bank->num_sectors == 0) {
288 LOG_ERROR("number of sectors shall be non zero value");
289 return ERROR_FLASH_BANK_INVALID;
291 if (bank->size % bank->num_sectors) {
292 LOG_ERROR("bank size not divisible by number of sectors");
293 return ERROR_FLASH_BANK_INVALID;
295 sectsize = bank->size / bank->num_sectors;
296 for (i = 0; i < bank->num_sectors; i++) {
297 bank->sectors[i].offset = i * sectsize;
298 bank->sectors[i].size = sectsize;
299 bank->sectors[i].is_erased = -1;
300 bank->sectors[i].is_protected = -1;
303 if (ocl->bufalign == 0)
306 if (ocl->buflen == 0) {
307 LOG_ERROR("buflen shall be non zero value");
308 return ERROR_FLASH_BANK_INVALID;
311 if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign)) {
312 LOG_ERROR("buflen is not multiple of bufalign");
313 return ERROR_FLASH_BANK_INVALID;
316 if (ocl->buflen % 4) {
317 LOG_ERROR("buflen shall be divisible by 4");
318 return ERROR_FLASH_BANK_INVALID;
324 static int ocl_info(struct flash_bank *bank, char *buf, int buf_size)
329 static int ocl_auto_probe(struct flash_bank *bank)
331 struct ocl_priv *ocl = bank->driver_priv;
333 if (ocl->buflen == 0 || ocl->bufalign == 0)
334 return ERROR_FLASH_BANK_NOT_PROBED;
339 struct flash_driver ocl_flash = {
341 .flash_bank_command = ocl_flash_bank_command,
343 .protect = ocl_protect,
345 .read = default_flash_read,
347 .erase_check = ocl_erase_check,
348 .protect_check = ocl_protect_check,
350 .auto_probe = ocl_auto_probe,