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, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
25 #include <target/embeddedice.h>
28 struct arm_jtag *jtag_info;
30 unsigned int bufalign;
33 static int ocl_erase_check(struct flash_bank *bank)
38 static int ocl_protect_check(struct flash_bank *bank)
43 /* flash_bank ocl 0 0 0 0 <target#> */
44 FLASH_BANK_COMMAND_HANDLER(ocl_flash_bank_command)
46 struct arm7_9_common *arm7_9;
50 return ERROR_COMMAND_SYNTAX_ERROR;
52 arm7_9 = target_to_arm7_9(bank->target);
53 if (!is_arm7_9(arm7_9))
54 return ERROR_TARGET_INVALID;
56 ocl = bank->driver_priv = malloc(sizeof(struct ocl_priv));
57 ocl->jtag_info = &arm7_9->jtag_info;
64 static int ocl_erase(struct flash_bank *bank, int first, int last)
66 struct ocl_priv *ocl = bank->driver_priv;
68 uint32_t dcc_buffer[3];
70 /* check preconditions */
71 if (bank->num_sectors == 0)
72 return ERROR_FLASH_BANK_NOT_PROBED;
74 if (bank->target->state != TARGET_RUNNING) {
75 LOG_ERROR("target has to be running to communicate with the loader");
76 return ERROR_TARGET_NOT_RUNNING;
79 if ((first == 0) && (last == bank->num_sectors - 1)) {
80 dcc_buffer[0] = OCL_ERASE_ALL;
81 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1);
82 if (retval != ERROR_OK)
85 dcc_buffer[0] = OCL_ERASE_BLOCK;
86 dcc_buffer[1] = first;
88 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3);
89 if (retval != ERROR_OK)
93 /* wait for response, fixed timeout of 1 s */
94 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
95 if (retval != ERROR_OK)
98 /* receive response */
99 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer + 1, 1);
100 if (retval != ERROR_OK)
103 if (dcc_buffer[1] != OCL_CMD_DONE) {
104 if (dcc_buffer[0] == OCL_ERASE_ALL)
105 LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
107 LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
108 return ERROR_FLASH_OPERATION_FAILED;
114 static int ocl_protect(struct flash_bank *bank, int set, int first, int last)
119 static int ocl_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
121 struct ocl_priv *ocl = bank->driver_priv;
123 uint32_t *dcc_buffer;
124 uint32_t *dcc_bufptr;
131 /* check preconditions */
132 if (ocl->buflen == 0 || ocl->bufalign == 0)
133 return ERROR_FLASH_BANK_NOT_PROBED;
135 if (bank->target->state != TARGET_RUNNING) {
136 LOG_ERROR("target has to be running to communicate with the loader");
137 return ERROR_TARGET_NOT_RUNNING;
140 /* allocate buffer for max. ocl buffer + overhead */
141 dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4 + 3));
144 if (count + (offset % ocl->bufalign) > ocl->buflen)
145 runlen = ocl->buflen - (offset % ocl->bufalign);
149 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
150 dcc_buffer[1] = offset;
151 dcc_bufptr = &dcc_buffer[2];
153 *dcc_bufptr = 0xffffffff;
154 byteofs = (offset % ocl->bufalign) % 4;
155 chksum = OCL_CHKS_INIT;
157 /* copy data to DCC buffer in proper byte order and properly aligned */
158 for (i = 0; i < runlen; i++) {
161 *dcc_bufptr &= *(buffer++) | 0xffffff00;
164 *dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
167 *dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
170 *dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
171 chksum ^= *(dcc_bufptr++);
172 *dcc_bufptr = 0xffffffff;
178 /* add the remaining word to checksum */
180 chksum ^= *(dcc_bufptr++);
182 *(dcc_bufptr++) = chksum;
185 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer);
186 if (retval != ERROR_OK) {
191 /* wait for response, fixed timeout of 1 s */
192 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
193 if (retval != ERROR_OK) {
198 /* receive response */
199 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
200 if (retval != ERROR_OK) {
205 if (dcc_buffer[0] != OCL_CMD_DONE) {
206 LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
208 return ERROR_FLASH_OPERATION_FAILED;
219 static int ocl_probe(struct flash_bank *bank)
221 struct ocl_priv *ocl = bank->driver_priv;
223 uint32_t dcc_buffer[1];
227 /* purge pending data in DCC */
228 embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
230 dcc_buffer[0] = OCL_PROBE;
231 retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1);
232 if (retval != ERROR_OK)
235 /* wait for response, fixed timeout of 1 s */
236 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000);
237 if (retval != ERROR_OK)
240 /* receive response */
241 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
242 if (retval != ERROR_OK)
245 if (dcc_buffer[0] != OCL_CMD_DONE) {
246 LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
247 return ERROR_FLASH_OPERATION_FAILED;
250 /* receive and fill in parameters, detection of loader is important, receive it one by one */
251 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
252 if (retval != ERROR_OK)
254 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
255 if (retval != ERROR_OK)
257 bank->base = dcc_buffer[0];
259 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
260 if (retval != ERROR_OK)
262 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
263 if (retval != ERROR_OK)
265 bank->size = dcc_buffer[0];
267 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
268 if (retval != ERROR_OK)
270 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
271 if (retval != ERROR_OK)
273 bank->num_sectors = dcc_buffer[0];
275 retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 0);
276 if (retval != ERROR_OK)
278 retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
279 if (retval != ERROR_OK)
281 ocl->buflen = dcc_buffer[0] & 0xffff;
282 ocl->bufalign = dcc_buffer[0] >> 16;
284 bank->sectors = realloc(bank->sectors, sizeof(struct flash_sector)*bank->num_sectors);
285 if (bank->num_sectors == 0) {
286 LOG_ERROR("number of sectors shall be non zero value");
287 return ERROR_FLASH_BANK_INVALID;
289 if (bank->size % bank->num_sectors) {
290 LOG_ERROR("bank size not divisible by number of sectors");
291 return ERROR_FLASH_BANK_INVALID;
293 sectsize = bank->size / bank->num_sectors;
294 for (i = 0; i < bank->num_sectors; i++) {
295 bank->sectors[i].offset = i * sectsize;
296 bank->sectors[i].size = sectsize;
297 bank->sectors[i].is_erased = -1;
298 bank->sectors[i].is_protected = -1;
301 if (ocl->bufalign == 0)
304 if (ocl->buflen == 0) {
305 LOG_ERROR("buflen shall be non zero value");
306 return ERROR_FLASH_BANK_INVALID;
309 if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign)) {
310 LOG_ERROR("buflen is not multiple of bufalign");
311 return ERROR_FLASH_BANK_INVALID;
314 if (ocl->buflen % 4) {
315 LOG_ERROR("buflen shall be divisible by 4");
316 return ERROR_FLASH_BANK_INVALID;
322 static int ocl_auto_probe(struct flash_bank *bank)
324 struct ocl_priv *ocl = bank->driver_priv;
326 if (ocl->buflen == 0 || ocl->bufalign == 0)
327 return ERROR_FLASH_BANK_NOT_PROBED;
332 struct flash_driver ocl_flash = {
334 .flash_bank_command = ocl_flash_bank_command,
336 .protect = ocl_protect,
338 .read = default_flash_read,
340 .erase_check = ocl_erase_check,
341 .protect_check = ocl_protect_check,
342 .auto_probe = ocl_auto_probe,
343 .free_driver_priv = default_flash_free_driver_priv,