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 <target/embeddedice.h>
31 struct arm_jtag *jtag_info;
33 unsigned int bufalign;
36 static int ocl_erase_check(struct flash_bank *bank)
41 static int ocl_protect_check(struct flash_bank *bank)
46 /* flash_bank ocl 0 0 0 0 <target#> */
47 FLASH_BANK_COMMAND_HANDLER(ocl_flash_bank_command)
49 struct arm7_9_common *arm7_9;
54 return ERROR_COMMAND_SYNTAX_ERROR;
57 arm7_9 = target_to_arm7_9(bank->target);
58 if (!is_arm7_9(arm7_9))
59 return ERROR_TARGET_INVALID;
61 ocl = bank->driver_priv = malloc(sizeof(struct ocl_priv));
62 ocl->jtag_info = &arm7_9->jtag_info;
69 static int ocl_erase(struct flash_bank *bank, int first, int last)
71 struct ocl_priv *ocl = bank->driver_priv;
73 uint32_t dcc_buffer[3];
75 /* check preconditions */
76 if (bank->num_sectors == 0)
77 return ERROR_FLASH_BANK_NOT_PROBED;
79 if (bank->target->state != TARGET_RUNNING)
81 LOG_ERROR("target has to be running to communicate with the loader");
82 return ERROR_TARGET_NOT_RUNNING;
85 if ((first == 0) && (last == bank->num_sectors - 1))
87 dcc_buffer[0] = OCL_ERASE_ALL;
88 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
93 dcc_buffer[0] = OCL_ERASE_BLOCK;
94 dcc_buffer[1] = first;
96 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3) != ERROR_OK))
100 /* wait for response, fixed timeout of 1 s */
101 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
106 /* receive response */
107 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer + 1, 1) != ERROR_OK))
110 if (dcc_buffer[1] != OCL_CMD_DONE)
112 if (dcc_buffer[0] == OCL_ERASE_ALL)
113 LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
115 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
116 return ERROR_FLASH_OPERATION_FAILED;
122 static int ocl_protect(struct flash_bank *bank, int set, int first, int last)
127 static int ocl_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
129 struct ocl_priv *ocl = bank->driver_priv;
131 uint32_t *dcc_buffer;
132 uint32_t *dcc_bufptr;
139 /* check preconditions */
140 if (ocl->buflen == 0 || ocl->bufalign == 0)
141 return ERROR_FLASH_BANK_NOT_PROBED;
143 if (bank->target->state != TARGET_RUNNING)
145 LOG_ERROR("target has to be running to communicate with the loader");
146 return ERROR_TARGET_NOT_RUNNING;
149 /* allocate buffer for max. ocl buffer + overhead */
150 dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4 + 3));
154 if (count + (offset % ocl->bufalign) > ocl->buflen)
155 runlen = ocl->buflen - (offset % ocl->bufalign);
159 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
160 dcc_buffer[1] = offset;
161 dcc_bufptr = &dcc_buffer[2];
163 *dcc_bufptr = 0xffffffff;
164 byteofs = (offset % ocl->bufalign) % 4;
165 chksum = OCL_CHKS_INIT;
167 /* copy data to DCC buffer in proper byte order and properly aligned */
168 for (i = 0; i < runlen; i++)
173 *dcc_bufptr &= *(buffer++) | 0xffffff00;
176 *dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
179 *dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
182 *dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
183 chksum ^= *(dcc_bufptr++);
184 *dcc_bufptr = 0xffffffff;
190 /* add the remaining word to checksum */
192 chksum ^= *(dcc_bufptr++);
194 *(dcc_bufptr++) = chksum;
197 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
203 /* wait for response, fixed timeout of 1 s */
204 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
210 /* receive response */
211 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
217 if (dcc_buffer[0] != OCL_CMD_DONE)
219 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
221 return ERROR_FLASH_OPERATION_FAILED;
232 static int ocl_probe(struct flash_bank *bank)
234 struct ocl_priv *ocl = bank->driver_priv;
236 uint32_t dcc_buffer[1];
240 /* purge pending data in DCC */
241 embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
243 dcc_buffer[0] = OCL_PROBE;
244 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
247 /* wait for response, fixed timeout of 1 s */
248 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
253 /* receive response */
254 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
257 if (dcc_buffer[0] != OCL_CMD_DONE)
259 LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
260 return ERROR_FLASH_OPERATION_FAILED;
263 /* receive and fill in parameters, detection of loader is important, receive it one by one */
264 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
265 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
267 bank->base = dcc_buffer[0];
269 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
270 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
272 bank->size = dcc_buffer[0];
274 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
275 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
277 bank->num_sectors = dcc_buffer[0];
279 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0) != ERROR_OK)
280 || (retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
282 ocl->buflen = dcc_buffer[0] & 0xffff;
283 ocl->bufalign = dcc_buffer[0] >> 16;
285 bank->sectors = realloc(bank->sectors, sizeof(struct flash_sector)*bank->num_sectors);
286 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++)
298 bank->sectors[i].offset = i * sectsize;
299 bank->sectors[i].size = sectsize;
300 bank->sectors[i].is_erased = -1;
301 bank->sectors[i].is_protected = -1;
304 if (ocl->bufalign == 0)
307 if (ocl->buflen == 0)
309 LOG_ERROR("buflen shall be non zero value");
310 return ERROR_FLASH_BANK_INVALID;
313 if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign))
315 LOG_ERROR("buflen is not multiple of bufalign");
316 return ERROR_FLASH_BANK_INVALID;
321 LOG_ERROR("buflen shall be divisible by 4");
322 return ERROR_FLASH_BANK_INVALID;
328 static int ocl_info(struct flash_bank *bank, char *buf, int buf_size)
333 static int ocl_auto_probe(struct flash_bank *bank)
335 struct ocl_priv *ocl = bank->driver_priv;
337 if (ocl->buflen == 0 || ocl->bufalign == 0)
338 return ERROR_FLASH_BANK_NOT_PROBED;
343 struct flash_driver ocl_flash = {
345 .flash_bank_command = ocl_flash_bank_command,
347 .protect = ocl_protect,
349 .read = default_flash_read,
351 .erase_check = ocl_erase_check,
352 .protect_check = ocl_protect_check,
354 .auto_probe = ocl_auto_probe,