Change return value on error.
[fw/openocd] / src / flash / nor / ocl.c
1 /***************************************************************************
2  *   Copyright (C) 2007 by Pavel Chromy                                    *
3  *   chromy@asix.cz                                                        *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "imp.h"
25 #include "ocl.h"
26 #include <target/embeddedice.h>
27
28
29 struct ocl_priv
30 {
31         struct arm_jtag *jtag_info;
32         unsigned int buflen;
33         unsigned int bufalign;
34 };
35
36 static int ocl_erase_check(struct flash_bank *bank)
37 {
38         return ERROR_OK;
39 }
40
41 static int ocl_protect_check(struct flash_bank *bank)
42 {
43         return ERROR_OK;
44 }
45
46 /* flash_bank ocl 0 0 0 0 <target#> */
47 FLASH_BANK_COMMAND_HANDLER(ocl_flash_bank_command)
48 {
49         struct arm7_9_common *arm7_9;
50         struct ocl_priv *ocl;
51
52         if (CMD_ARGC < 6)
53         {
54                 return ERROR_COMMAND_SYNTAX_ERROR;
55         }
56
57         arm7_9 = target_to_arm7_9(bank->target);
58         if (!is_arm7_9(arm7_9))
59                 return ERROR_TARGET_INVALID;
60
61         ocl = bank->driver_priv = malloc(sizeof(struct ocl_priv));
62         ocl->jtag_info = &arm7_9->jtag_info;
63         ocl->buflen = 0;
64         ocl->bufalign = 1;
65
66         return ERROR_OK;
67 }
68
69 static int ocl_erase(struct flash_bank *bank, int first, int last)
70 {
71         struct ocl_priv *ocl = bank->driver_priv;
72         int retval;
73         uint32_t dcc_buffer[3];
74
75         /* check preconditions */
76         if (bank->num_sectors == 0)
77                 return ERROR_FLASH_BANK_NOT_PROBED;
78
79         if (bank->target->state != TARGET_RUNNING)
80         {
81                 LOG_ERROR("target has to be running to communicate with the loader");
82                 return ERROR_TARGET_NOT_RUNNING;
83         }
84
85         if ((first == 0) && (last == bank->num_sectors - 1))
86         {
87                 dcc_buffer[0] = OCL_ERASE_ALL;
88                 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
89                         return retval;
90         }
91         else
92         {
93                 dcc_buffer[0] = OCL_ERASE_BLOCK;
94                 dcc_buffer[1] = first;
95                 dcc_buffer[2] = last;
96                 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 3) != ERROR_OK))
97                         return retval;
98         }
99
100         /* wait for response, fixed timeout of 1 s */
101         if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
102         {
103                 return retval;
104         }
105
106         /* receive response */
107         if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer + 1, 1) != ERROR_OK))
108                 return retval;
109
110         if (dcc_buffer[1] != OCL_CMD_DONE)
111         {
112                 if (dcc_buffer[0] == OCL_ERASE_ALL)
113                         LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
114                 else
115                         LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
116                 return ERROR_FLASH_OPERATION_FAILED;
117         }
118
119         return ERROR_OK;
120 }
121
122 static int ocl_protect(struct flash_bank *bank, int set, int first, int last)
123 {
124         return ERROR_OK;
125 }
126
127 static int ocl_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
128 {
129         struct ocl_priv *ocl = bank->driver_priv;
130         int retval;
131         uint32_t *dcc_buffer;
132         uint32_t *dcc_bufptr;
133         int byteofs;
134         int runlen;
135         uint32_t chksum;
136
137         int i;
138
139         /* check preconditions */
140         if (ocl->buflen == 0 || ocl->bufalign == 0)
141                 return ERROR_FLASH_BANK_NOT_PROBED;
142
143         if (bank->target->state != TARGET_RUNNING)
144         {
145                 LOG_ERROR("target has to be running to communicate with the loader");
146                 return ERROR_TARGET_NOT_RUNNING;
147         }
148
149         /* allocate buffer for max. ocl buffer + overhead */
150         dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4 + 3));
151
152         while (count)
153         {
154                 if (count + (offset % ocl->bufalign) > ocl->buflen)
155                         runlen = ocl->buflen - (offset % ocl->bufalign);
156                 else
157                         runlen = count;
158
159                 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
160                 dcc_buffer[1] = offset;
161                 dcc_bufptr = &dcc_buffer[2];
162
163                 *dcc_bufptr = 0xffffffff;
164                 byteofs = (offset % ocl->bufalign) % 4;
165                 chksum = OCL_CHKS_INIT;
166
167                 /* copy data to DCC buffer in proper byte order and properly aligned */
168                 for (i = 0; i < runlen; i++)
169                 {
170                         switch (byteofs++)
171                         {
172                                 case 0:
173                                         *dcc_bufptr &= *(buffer++) | 0xffffff00;
174                                         break;
175                                 case 1:
176                                         *dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
177                                         break;
178                                 case 2:
179                                         *dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
180                                         break;
181                                 case 3:
182                                         *dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
183                                         chksum ^= *(dcc_bufptr++);
184                                         *dcc_bufptr = 0xffffffff;
185                                         byteofs = 0;
186                                         break;
187                         }
188                 }
189
190                 /* add the remaining word to checksum */
191                 if (byteofs)
192                         chksum ^= *(dcc_bufptr++);
193
194                 *(dcc_bufptr++) = chksum;
195
196                 /* send the data */
197                 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
198                 {
199                         free(dcc_buffer);
200                   return retval;
201                 }
202
203                 /* wait for response, fixed timeout of 1 s */
204                 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
205                 {
206                         free(dcc_buffer);
207                         return retval;
208                 }
209
210                 /* receive response */
211                 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
212                 {
213                         free(dcc_buffer);
214                         return retval;
215                 }
216
217                 if (dcc_buffer[0] != OCL_CMD_DONE)
218                 {
219                         LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
220                         free(dcc_buffer);
221                         return ERROR_FLASH_OPERATION_FAILED;
222                 }
223
224                 count -= runlen;
225                 offset += runlen;
226         }
227
228         free(dcc_buffer);
229         return ERROR_OK;
230 }
231
232 static int ocl_probe(struct flash_bank *bank)
233 {
234         struct ocl_priv *ocl = bank->driver_priv;
235         int retval;
236         uint32_t dcc_buffer[1];
237         int sectsize;
238         int i;
239
240         /* purge pending data in DCC */
241         embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
242
243         dcc_buffer[0] = OCL_PROBE;
244         if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
245                 return retval;
246
247         /* wait for response, fixed timeout of 1 s */
248         if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
249         {
250                 return retval;
251         }
252
253         /* receive response */
254         if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
255                 return retval;
256
257         if (dcc_buffer[0] != OCL_CMD_DONE)
258         {
259                 LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
260                 return ERROR_FLASH_OPERATION_FAILED;
261         }
262
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))
266                 return retval;
267         bank->base = dcc_buffer[0];
268
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))
271                 return retval;
272         bank->size = dcc_buffer[0];
273
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))
276                 return retval;
277         bank->num_sectors = dcc_buffer[0];
278
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))
281                 return retval;
282         ocl->buflen = dcc_buffer[0] & 0xffff;
283         ocl->bufalign = dcc_buffer[0] >> 16;
284
285         bank->sectors = realloc(bank->sectors, sizeof(struct flash_sector)*bank->num_sectors);
286         if (bank->num_sectors == 0)
287         {
288                 LOG_ERROR("number of sectors shall be non zero value");
289                 return ERROR_FLASH_BANK_INVALID;
290         }
291         if (bank->size % bank->num_sectors) {
292                 LOG_ERROR("bank size not divisible by number of sectors");
293                 return ERROR_FLASH_BANK_INVALID;
294         }
295         sectsize = bank->size / bank->num_sectors;
296         for (i = 0; i < bank->num_sectors; i++)
297         {
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;
302         }
303
304         if (ocl->bufalign == 0)
305                 ocl->bufalign = 1;
306
307         if (ocl->buflen == 0)
308         {
309                 LOG_ERROR("buflen shall be non zero value");
310                 return ERROR_FLASH_BANK_INVALID;
311         }
312
313         if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign))
314         {
315                 LOG_ERROR("buflen is not multiple of bufalign");
316                 return ERROR_FLASH_BANK_INVALID;
317         }
318
319         if (ocl->buflen % 4)
320         {
321                 LOG_ERROR("buflen shall be divisible by 4");
322                 return ERROR_FLASH_BANK_INVALID;
323         }
324
325         return ERROR_OK;
326 }
327
328 static int ocl_info(struct flash_bank *bank, char *buf, int buf_size)
329 {
330         return ERROR_OK;
331 }
332
333 static int ocl_auto_probe(struct flash_bank *bank)
334 {
335         struct ocl_priv *ocl = bank->driver_priv;
336
337         if (ocl->buflen == 0 || ocl->bufalign == 0)
338                 return ERROR_FLASH_BANK_NOT_PROBED;
339
340         return ERROR_OK;
341 }
342
343 struct flash_driver ocl_flash = {
344         .name = "ocl",
345         .flash_bank_command = ocl_flash_bank_command,
346         .erase = ocl_erase,
347         .protect = ocl_protect,
348         .write = ocl_write,
349         .read = default_flash_read,
350         .probe = ocl_probe,
351         .erase_check = ocl_erase_check,
352         .protect_check = ocl_protect_check,
353         .info = ocl_info,
354         .auto_probe = ocl_auto_probe,
355 };