- Fixes '==' whitespace
[fw/openocd] / src / flash / 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 "ocl.h"
25 #include "flash.h"
26 #include "embeddedice.h"
27
28
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, uint8_t *buffer, uint32_t offset, uint32_t 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);
39
40 flash_driver_t ocl_flash =
41 {
42         .name = "ocl",
43         .register_commands = ocl_register_commands,
44         .flash_bank_command = ocl_flash_bank_command,
45         .erase = ocl_erase,
46         .protect = ocl_protect,
47         .write = ocl_write,
48         .probe = ocl_probe,
49         .erase_check = ocl_erase_check,
50         .protect_check = ocl_protect_check,
51         .info = ocl_info,
52         .auto_probe = ocl_auto_probe
53 };
54
55 typedef struct ocl_priv_s
56 {
57         arm_jtag_t *jtag_info;
58         unsigned int buflen;
59         unsigned int bufalign;
60 } ocl_priv_t;
61
62 static int ocl_register_commands(struct command_context_s *cmd_ctx)
63 {
64         return ERROR_OK;
65 }
66
67 static int ocl_erase_check(struct flash_bank_s *bank)
68 {
69         return ERROR_OK;
70 }
71
72 static int ocl_protect_check(struct flash_bank_s *bank)
73 {
74         return ERROR_OK;
75 }
76
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)
79 {
80         int retval;
81         armv4_5_common_t *armv4_5;
82         arm7_9_common_t *arm7_9;
83         ocl_priv_t *ocl;
84
85         if (argc < 6)
86         {
87                 LOG_WARNING("incomplete flash_bank ocl configuration");
88                 return ERROR_FLASH_BANK_INVALID;
89         }
90
91         if ((retval = arm7_9_get_arch_pointers(bank->target, &armv4_5, &arm7_9)) != ERROR_OK)
92                 return retval;
93
94         ocl = bank->driver_priv = malloc(sizeof(ocl_priv_t));
95         ocl->jtag_info = &arm7_9->jtag_info;
96         ocl->buflen = 0;
97         ocl->bufalign = 1;
98
99         return ERROR_OK;
100 }
101
102 static int ocl_erase(struct flash_bank_s *bank, int first, int last)
103 {
104         ocl_priv_t *ocl = bank->driver_priv;
105         int retval;
106         uint32_t dcc_buffer[3];
107
108         /* check preconditions */
109         if (bank->num_sectors == 0)
110                 return ERROR_FLASH_BANK_NOT_PROBED;
111
112         if (bank->target->state != TARGET_RUNNING)
113         {
114                 LOG_ERROR("target has to be running to communicate with the loader");
115                 return ERROR_TARGET_NOT_RUNNING;
116         }
117
118         if ((first == 0) && (last == bank->num_sectors - 1))
119         {
120                 dcc_buffer[0] = OCL_ERASE_ALL;
121                 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
122                         return retval;
123         }
124         else
125         {
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))
130                         return retval;
131         }
132
133         /* wait for response, fixed timeout of 1 s */
134         if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
135         {
136                 if (retval == ERROR_TARGET_TIMEOUT)
137                         LOG_ERROR("loader not responding");
138                 return retval;
139         }
140
141         /* receive response */
142         if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer+1, 1) != ERROR_OK))
143                 return retval;
144
145         if (dcc_buffer[1] != OCL_CMD_DONE)
146         {
147                 if (dcc_buffer[0] == OCL_ERASE_ALL)
148                         LOG_ERROR("loader response to OCL_ERASE_ALL 0x%08" PRIx32 "", dcc_buffer[1]);
149                 else
150                         LOG_ERROR("loader response to OCL_ERASE_BLOCK 0x%08" PRIx32 "", dcc_buffer[1]);
151                 return ERROR_FLASH_OPERATION_FAILED;
152         }
153
154         return ERROR_OK;
155 }
156
157 static int ocl_protect(struct flash_bank_s *bank, int set, int first, int last)
158 {
159         return ERROR_OK;
160 }
161
162 static int ocl_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
163 {
164         ocl_priv_t *ocl = bank->driver_priv;
165         int retval;
166         uint32_t *dcc_buffer;
167         uint32_t *dcc_bufptr;
168         int byteofs;
169         int runlen;
170         uint32_t chksum;
171
172         int i;
173
174         /* check preconditions */
175         if (ocl->buflen == 0 || ocl->bufalign == 0)
176                 return ERROR_FLASH_BANK_NOT_PROBED;
177
178         if (bank->target->state != TARGET_RUNNING)
179         {
180                 LOG_ERROR("target has to be running to communicate with the loader");
181                 return ERROR_TARGET_NOT_RUNNING;
182         }
183
184         /* allocate buffer for max. ocl buffer + overhead */
185         dcc_buffer = malloc(sizeof(uint32_t)*(ocl->buflen/4+3));
186
187         while (count)
188         {
189                 if (count + (offset % ocl->bufalign) > ocl->buflen)
190                         runlen = ocl->buflen - (offset % ocl->bufalign);
191                 else
192                         runlen = count;
193
194                 dcc_buffer[0] = OCL_FLASH_BLOCK | runlen;
195                 dcc_buffer[1] = offset;
196                 dcc_bufptr = &dcc_buffer[2];
197
198                 *dcc_bufptr = 0xffffffff;
199                 byteofs = (offset % ocl->bufalign) % 4;
200                 chksum = OCL_CHKS_INIT;
201
202                 /* copy data to DCC buffer in proper byte order and properly aligned */
203                 for (i=0; i<runlen; i++)
204                 {
205                         switch (byteofs++)
206                         {
207                                 case 0:
208                                         *dcc_bufptr &= *(buffer++) | 0xffffff00;
209                                         break;
210                                 case 1:
211                                         *dcc_bufptr &= ((*(buffer++)) << 8) | 0xffff00ff;
212                                         break;
213                                 case 2:
214                                         *dcc_bufptr &= ((*(buffer++)) << 16) | 0xff00ffff;
215                                         break;
216                                 case 3:
217                                         *dcc_bufptr &= ((*(buffer++)) << 24) | 0x00ffffff;
218                                         chksum ^= *(dcc_bufptr++);
219                                         *dcc_bufptr = 0xffffffff;
220                                         byteofs = 0;
221                                         break;
222                         }
223                 }
224
225                 /* add the remaining word to checksum */
226                 if (byteofs)
227                         chksum ^= *(dcc_bufptr++);
228
229                 *(dcc_bufptr++) = chksum;
230
231                 /* send the data */
232                 if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, dcc_bufptr-dcc_buffer)) != ERROR_OK)
233                 {
234                         free(dcc_buffer);
235                   return retval;
236                 }
237
238                 /* wait for response, fixed timeout of 1 s */
239                 if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
240                 {
241                         if (retval == ERROR_TARGET_TIMEOUT)
242                                 LOG_ERROR("loader not responding");
243                         free(dcc_buffer);
244                         return retval;
245                 }
246
247                 /* receive response */
248                 if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
249                 {
250                         free(dcc_buffer);
251                         return retval;
252                 }
253
254                 if (dcc_buffer[0] != OCL_CMD_DONE)
255                 {
256                         LOG_ERROR("loader response to OCL_FLASH_BLOCK 0x%08" PRIx32 "", dcc_buffer[0]);
257                         free(dcc_buffer);
258                         return ERROR_FLASH_OPERATION_FAILED;
259                 }
260
261                 count -= runlen;
262                 offset += runlen;
263         }
264
265         free(dcc_buffer);
266         return ERROR_OK;
267 }
268
269 static int ocl_probe(struct flash_bank_s *bank)
270 {
271         ocl_priv_t *ocl = bank->driver_priv;
272         int retval;
273         uint32_t dcc_buffer[1];
274         int sectsize;
275         int i;
276
277         /* purge pending data in DCC */
278         embeddedice_receive(ocl->jtag_info, dcc_buffer, 1);
279
280         dcc_buffer[0] = OCL_PROBE;
281         if ((retval = embeddedice_send(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
282                 return retval;
283
284         /* wait for response, fixed timeout of 1 s */
285         if ((retval = embeddedice_handshake(ocl->jtag_info, EICE_COMM_CTRL_WBIT, 1000) != ERROR_OK))
286         {
287                 if (retval == ERROR_TARGET_TIMEOUT)
288                         LOG_ERROR("loader not responding");
289                 return retval;
290         }
291
292         /* receive response */
293         if ((retval = embeddedice_receive(ocl->jtag_info, dcc_buffer, 1) != ERROR_OK))
294                 return retval;
295
296         if (dcc_buffer[0] != OCL_CMD_DONE)
297         {
298                 LOG_ERROR("loader response to OCL_PROBE 0x%08" PRIx32 "", dcc_buffer[0]);
299                 return ERROR_FLASH_OPERATION_FAILED;
300         }
301
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))
305                 return retval;
306         bank->base = dcc_buffer[0];
307
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))
310                 return retval;
311         bank->size = dcc_buffer[0];
312
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))
315                 return retval;
316         bank->num_sectors = dcc_buffer[0];
317
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))
320                 return retval;
321         ocl->buflen = dcc_buffer[0] & 0xffff;
322         ocl->bufalign = dcc_buffer[0] >> 16;
323
324         bank->sectors = realloc(bank->sectors, sizeof(flash_sector_t)*bank->num_sectors);
325         if (bank->num_sectors == 0)
326         {
327                 LOG_ERROR("number of sectors shall be non zero value");
328                 return ERROR_FLASH_BANK_INVALID;
329         }
330         if (bank->size % bank->num_sectors) {
331                 LOG_ERROR("bank size not divisible by number of sectors");
332                 return ERROR_FLASH_BANK_INVALID;
333         }
334         sectsize = bank->size / bank->num_sectors;
335         for (i=0; i<bank->num_sectors; i++)
336         {
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;
341         }
342
343         if (ocl->bufalign == 0)
344                 ocl->bufalign = 1;
345
346         if (ocl->buflen == 0)
347         {
348                 LOG_ERROR("buflen shall be non zero value");
349                 return ERROR_FLASH_BANK_INVALID;
350         }
351
352         if ((ocl->bufalign > ocl->buflen) || (ocl->buflen % ocl->bufalign))
353         {
354                 LOG_ERROR("buflen is not multiple of bufalign");
355                 return ERROR_FLASH_BANK_INVALID;
356         }
357
358         if (ocl->buflen % 4)
359         {
360                 LOG_ERROR("buflen shall be divisible by 4");
361                 return ERROR_FLASH_BANK_INVALID;
362         }
363
364         return ERROR_OK;
365 }
366
367 static int ocl_info(struct flash_bank_s *bank, char *buf, int buf_size)
368 {
369         return ERROR_OK;
370 }
371
372 static int ocl_auto_probe(struct flash_bank_s *bank)
373 {
374         ocl_priv_t *ocl = bank->driver_priv;
375
376         if (ocl->buflen == 0 || ocl->bufalign == 0)
377                 return ERROR_FLASH_BANK_NOT_PROBED;
378
379         return ERROR_OK;
380 }