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