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