flash/nor/lpc2000: free allocated working area when target_write fails
[openocd.git] / src / flash / nor / lpc2000.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * LPC1700 support Copyright (C) 2009 by Audrius Urmanavicius *
6 * didele.deze@gmail.com *
7 * *
8 * LPC1100 variant and auto-probing support Copyright (C) 2014 *
9 * by Cosmin Gorgovan cosmin [at] linux-geek [dot] org *
10 * *
11 * LPC800/LPC1500/LPC54100 support Copyright (C) 2013/2014 *
12 * by Nemui Trinomius *
13 * nemuisan_kawausogasuki@live.jp *
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
29 ***************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "imp.h"
36 #include <helper/binarybuffer.h>
37 #include <target/algorithm.h>
38 #include <target/arm_opcodes.h>
39 #include <target/armv7m.h>
40
41 /**
42 * @file
43 * flash programming support for NXP LPC8xx,LPC1xxx,LPC4xxx,LP5410x and LPC2xxx devices.
44 *
45 * @todo Provide a way to update CCLK after declaring the flash bank. The value which is correct after chip reset will
46 * rarely still work right after the clocks switch to use the PLL (e.g. 4MHz --> 100 MHz).
47 */
48 /*
49 * currently supported devices:
50 * variant 1 (lpc2000_v1):
51 * - 2104 | 5 | 6
52 * - 2114 | 9
53 * - 2124 | 9
54 * - 2194
55 * - 2212 | 4
56 * - 2292 | 4
57 *
58 * variant 2 (lpc2000_v2):
59 * - 213x
60 * - 214x
61 * - 2101 | 2 | 3
62 * - 2364 | 6 | 8
63 * - 2378
64 *
65 * lpc1700:
66 * - 175x
67 * - 176x (tested with LPC1768)
68 * - 177x
69 * - 178x (tested with LPC1788)
70 *
71 * lpc4000: (lpc1700's alias)
72 * - 407x
73 * - 408x (tested with LPC4088)
74 *
75 * lpc4300: (also available as lpc1800 - alias)
76 * - 43x2 | 3 | 5 | 7 (tested with LPC4337/LPC4357)
77 * - 18x2 | 3 | 5 | 7
78 *
79 * lpc800:
80 * - 810 | 1 | 2 (tested with LPC810/LPC811/LPC812)
81 * - 822 | 4 (tested with LPC824)
82 *
83 * lpc1100:
84 * - 11xx
85 * - 11Axx
86 * - 11Cxx
87 * - 11Dxx
88 * - 11Exx
89 * - 11Uxx (tested with LPC11U34)
90 * - 131x
91 * - 134x
92 *
93 * lpc1500:
94 * - 15x7 | 8 | 9 (tested with LPC1549)
95 *
96 * lpc54100:
97 * - 54101 | 2 (tested with LPC54102)
98 *
99 * The auto variant auto-detects parts from the following series:
100 * - 11xx
101 * - 11Axx
102 * - 11Cxx
103 * - 11Dxx
104 * - 11Exx
105 * - 11Uxx
106 * - 131x
107 * - 134x
108 * - 175x
109 * - 176x
110 * - 177x
111 * - 178x
112 * - 407x
113 * - 408x
114 * - 81x
115 * - 82x
116 */
117
118 /* Part IDs for autodetection */
119 /* A script which can automatically extract part ids from user manuals is available here:
120 * https://github.com/lgeek/lpc_part_ids
121 */
122 #define LPC1110_1 0x0A07102B
123 #define LPC1110_2 0x1A07102B
124 #define LPC1111_002_1 0x0A16D02B
125 #define LPC1111_002_2 0x1A16D02B
126 #define LPC1111_101_1 0x041E502B
127 #define LPC1111_101_2 0x2516D02B
128 #define LPC1111_103_1 0x00010013
129 #define LPC1111_201_1 0x0416502B
130 #define LPC1111_201_2 0x2516902B
131 #define LPC1111_203_1 0x00010012
132 #define LPC1112_101_1 0x042D502B
133 #define LPC1112_101_2 0x2524D02B
134 #define LPC1112_102_1 0x0A24902B
135 #define LPC1112_102_2 0x1A24902B
136 #define LPC1112_103_1 0x00020023
137 #define LPC1112_201_1 0x0425502B
138 #define LPC1112_201_2 0x2524902B
139 #define LPC1112_203_1 0x00020022
140 #define LPC1113_201_1 0x0434502B
141 #define LPC1113_201_2 0x2532902B
142 #define LPC1113_203_1 0x00030032
143 #define LPC1113_301_1 0x0434102B
144 #define LPC1113_301_2 0x2532102B
145 #define LPC1113_303_1 0x00030030
146 #define LPC1114_102_1 0x0A40902B
147 #define LPC1114_102_2 0x1A40902B
148 #define LPC1114_201_1 0x0444502B
149 #define LPC1114_201_2 0x2540902B
150 #define LPC1114_203_1 0x00040042
151 #define LPC1114_301_1 0x0444102B
152 #define LPC1114_301_2 0x2540102B
153 #define LPC1114_303_1 0x00040040
154 #define LPC1114_323_1 0x00040060
155 #define LPC1114_333_1 0x00040070
156 #define LPC1115_303_1 0x00050080
157
158 #define LPC11A02_1 0x4D4C802B
159 #define LPC11A04_1 0x4D80002B
160 #define LPC11A11_001_1 0x455EC02B
161 #define LPC11A12_101_1 0x4574802B
162 #define LPC11A13_201_1 0x458A402B
163 #define LPC11A14_301_1 0x35A0002B
164 #define LPC11A14_301_2 0x45A0002B
165
166 #define LPC11C12_301_1 0x1421102B
167 #define LPC11C14_301_1 0x1440102B
168 #define LPC11C22_301_1 0x1431102B
169 #define LPC11C24_301_1 0x1430102B
170
171 #define LPC11E11_101 0x293E902B
172 #define LPC11E12_201 0x2954502B
173 #define LPC11E13_301 0x296A102B
174 #define LPC11E14_401 0x2980102B
175 #define LPC11E36_501 0x00009C41
176 #define LPC11E37_401 0x00007C45
177 #define LPC11E37_501 0x00007C41
178
179 #define LPC11U12_201_1 0x095C802B
180 #define LPC11U12_201_2 0x295C802B
181 #define LPC11U13_201_1 0x097A802B
182 #define LPC11U13_201_2 0x297A802B
183 #define LPC11U14_201_1 0x0998802B
184 #define LPC11U14_201_2 0x2998802B
185 #define LPC11U23_301 0x2972402B
186 #define LPC11U24_301 0x2988402B
187 #define LPC11U24_401 0x2980002B
188 #define LPC11U34_311 0x0003D440
189 #define LPC11U34_421 0x0001CC40
190 #define LPC11U35_401 0x0001BC40
191 #define LPC11U35_501 0x0000BC40
192 #define LPC11U36_401 0x00019C40
193 #define LPC11U37_401 0x00017C40
194 #define LPC11U37H_401 0x00007C44
195 #define LPC11U37_501 0x00007C40
196
197 #define LPC11E66 0x0000DCC1
198 #define LPC11E67 0x0000BC81
199 #define LPC11E68 0x00007C01
200
201 #define LPC11U66 0x0000DCC8
202 #define LPC11U67_1 0x0000BC88
203 #define LPC11U67_2 0x0000BC80
204 #define LPC11U68_1 0x00007C08
205 #define LPC11U68_2 0x00007C00
206
207 #define LPC1311 0x2C42502B
208 #define LPC1311_1 0x1816902B
209 #define LPC1313 0x2C40102B
210 #define LPC1313_1 0x1830102B
211 #define LPC1315 0x3A010523
212 #define LPC1316 0x1A018524
213 #define LPC1317 0x1A020525
214 #define LPC1342 0x3D01402B
215 #define LPC1343 0x3D00002B
216 #define LPC1345 0x28010541
217 #define LPC1346 0x08018542
218 #define LPC1347 0x08020543
219
220 #define LPC1751_1 0x25001110
221 #define LPC1751_2 0x25001118
222 #define LPC1752 0x25001121
223 #define LPC1754 0x25011722
224 #define LPC1756 0x25011723
225 #define LPC1758 0x25013F37
226 #define LPC1759 0x25113737
227 #define LPC1763 0x26012033
228 #define LPC1764 0x26011922
229 #define LPC1765 0x26013733
230 #define LPC1766 0x26013F33
231 #define LPC1767 0x26012837
232 #define LPC1768 0x26013F37
233 #define LPC1769 0x26113F37
234 #define LPC1774 0x27011132
235 #define LPC1776 0x27191F43
236 #define LPC1777 0x27193747
237 #define LPC1778 0x27193F47
238 #define LPC1785 0x281D1743
239 #define LPC1786 0x281D1F43
240 #define LPC1787 0x281D3747
241 #define LPC1788 0x281D3F47
242
243 #define LPC4072 0x47011121
244 #define LPC4074 0x47011132
245 #define LPC4076 0x47191F43
246 #define LPC4078 0x47193F47
247 #define LPC4088 0x481D3F47
248
249 #define LPC810_021 0x00008100
250 #define LPC811_001 0x00008110
251 #define LPC812_101 0x00008120
252 #define LPC812_101_1 0x00008121
253 #define LPC812_101_2 0x00008122
254 #define LPC812_101_3 0x00008123
255
256 #define LPC822_101 0x00008221
257 #define LPC822_101_1 0x00008222
258 #define LPC824_201 0x00008241
259 #define LPC824_201_1 0x00008242
260
261 #define IAP_CODE_LEN 0x34
262
263 typedef enum {
264 lpc2000_v1,
265 lpc2000_v2,
266 lpc1700,
267 lpc4300,
268 lpc800,
269 lpc1100,
270 lpc1500,
271 lpc54100,
272 lpc_auto,
273 } lpc2000_variant;
274
275 struct lpc2000_flash_bank {
276 lpc2000_variant variant;
277 uint32_t cclk;
278 int cmd51_dst_boundary;
279 int calc_checksum;
280 uint32_t cmd51_max_buffer;
281 int checksum_vector;
282 uint32_t iap_max_stack;
283 uint32_t lpc4300_bank;
284 bool probed;
285 };
286
287 enum lpc2000_status_codes {
288 LPC2000_CMD_SUCCESS = 0,
289 LPC2000_INVALID_COMMAND = 1,
290 LPC2000_SRC_ADDR_ERROR = 2,
291 LPC2000_DST_ADDR_ERROR = 3,
292 LPC2000_SRC_ADDR_NOT_MAPPED = 4,
293 LPC2000_DST_ADDR_NOT_MAPPED = 5,
294 LPC2000_COUNT_ERROR = 6,
295 LPC2000_INVALID_SECTOR = 7,
296 LPC2000_SECTOR_NOT_BLANK = 8,
297 LPC2000_SECTOR_NOT_PREPARED = 9,
298 LPC2000_COMPARE_ERROR = 10,
299 LPC2000_BUSY = 11,
300 LPC2000_PARAM_ERROR = 12,
301 LPC2000_ADDR_ERROR = 13,
302 LPC2000_ADDR_NOT_MAPPED = 14,
303 LPC2000_CMD_NOT_LOCKED = 15,
304 LPC2000_INVALID_CODE = 16,
305 LPC2000_INVALID_BAUD_RATE = 17,
306 LPC2000_INVALID_STOP_BIT = 18,
307 LPC2000_CRP_ENABLED = 19,
308 LPC2000_INVALID_FLASH_UNIT = 20,
309 LPC2000_USER_CODE_CHECKSUM = 21,
310 LCP2000_ERROR_SETTING_ACTIVE_PARTITION = 22,
311 };
312
313 static int lpc2000_build_sector_list(struct flash_bank *bank)
314 {
315 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
316 uint32_t offset = 0;
317
318 /* default to a 4096 write buffer */
319 lpc2000_info->cmd51_max_buffer = 4096;
320
321 if (lpc2000_info->variant == lpc2000_v1) {
322 lpc2000_info->cmd51_dst_boundary = 512;
323 lpc2000_info->checksum_vector = 5;
324 lpc2000_info->iap_max_stack = 128;
325
326 /* variant 1 has different layout for 128kb and 256kb flashes */
327 if (bank->size == 128 * 1024) {
328 bank->num_sectors = 16;
329 bank->sectors = malloc(sizeof(struct flash_sector) * 16);
330 for (int i = 0; i < 16; i++) {
331 bank->sectors[i].offset = offset;
332 bank->sectors[i].size = 8 * 1024;
333 offset += bank->sectors[i].size;
334 bank->sectors[i].is_erased = -1;
335 bank->sectors[i].is_protected = 1;
336 }
337 } else if (bank->size == 256 * 1024) {
338 bank->num_sectors = 18;
339 bank->sectors = malloc(sizeof(struct flash_sector) * 18);
340
341 for (int i = 0; i < 8; i++) {
342 bank->sectors[i].offset = offset;
343 bank->sectors[i].size = 8 * 1024;
344 offset += bank->sectors[i].size;
345 bank->sectors[i].is_erased = -1;
346 bank->sectors[i].is_protected = 1;
347 }
348 for (int i = 8; i < 10; i++) {
349 bank->sectors[i].offset = offset;
350 bank->sectors[i].size = 64 * 1024;
351 offset += bank->sectors[i].size;
352 bank->sectors[i].is_erased = -1;
353 bank->sectors[i].is_protected = 1;
354 }
355 for (int i = 10; i < 18; i++) {
356 bank->sectors[i].offset = offset;
357 bank->sectors[i].size = 8 * 1024;
358 offset += bank->sectors[i].size;
359 bank->sectors[i].is_erased = -1;
360 bank->sectors[i].is_protected = 1;
361 }
362 } else {
363 LOG_ERROR("BUG: unknown bank->size encountered");
364 exit(-1);
365 }
366 } else if (lpc2000_info->variant == lpc2000_v2) {
367 lpc2000_info->cmd51_dst_boundary = 256;
368 lpc2000_info->checksum_vector = 5;
369 lpc2000_info->iap_max_stack = 128;
370
371 /* variant 2 has a uniform layout, only number of sectors differs */
372 switch (bank->size) {
373 case 4 * 1024:
374 lpc2000_info->cmd51_max_buffer = 1024;
375 bank->num_sectors = 1;
376 break;
377 case 8 * 1024:
378 lpc2000_info->cmd51_max_buffer = 1024;
379 bank->num_sectors = 2;
380 break;
381 case 16 * 1024:
382 bank->num_sectors = 4;
383 break;
384 case 32 * 1024:
385 bank->num_sectors = 8;
386 break;
387 case 64 * 1024:
388 bank->num_sectors = 9;
389 break;
390 case 128 * 1024:
391 bank->num_sectors = 11;
392 break;
393 case 256 * 1024:
394 bank->num_sectors = 15;
395 break;
396 case 500 * 1024:
397 bank->num_sectors = 27;
398 break;
399 case 512 * 1024:
400 case 504 * 1024:
401 bank->num_sectors = 28;
402 break;
403 default:
404 LOG_ERROR("BUG: unknown bank->size encountered");
405 exit(-1);
406 break;
407 }
408
409 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
410
411 for (int i = 0; i < bank->num_sectors; i++) {
412 if (i < 8) {
413 bank->sectors[i].offset = offset;
414 bank->sectors[i].size = 4 * 1024;
415 offset += bank->sectors[i].size;
416 bank->sectors[i].is_erased = -1;
417 bank->sectors[i].is_protected = 1;
418 } else if (i < 22) {
419 bank->sectors[i].offset = offset;
420 bank->sectors[i].size = 32 * 1024;
421 offset += bank->sectors[i].size;
422 bank->sectors[i].is_erased = -1;
423 bank->sectors[i].is_protected = 1;
424 } else if (i < 28) {
425 bank->sectors[i].offset = offset;
426 bank->sectors[i].size = 4 * 1024;
427 offset += bank->sectors[i].size;
428 bank->sectors[i].is_erased = -1;
429 bank->sectors[i].is_protected = 1;
430 }
431 }
432 } else if (lpc2000_info->variant == lpc1700) {
433 lpc2000_info->cmd51_dst_boundary = 256;
434 lpc2000_info->checksum_vector = 7;
435 lpc2000_info->iap_max_stack = 128;
436
437 switch (bank->size) {
438 case 4 * 1024:
439 lpc2000_info->cmd51_max_buffer = 256;
440 bank->num_sectors = 1;
441 break;
442 case 8 * 1024:
443 lpc2000_info->cmd51_max_buffer = 512;
444 bank->num_sectors = 2;
445 break;
446 case 16 * 1024:
447 lpc2000_info->cmd51_max_buffer = 512;
448 bank->num_sectors = 4;
449 break;
450 case 32 * 1024:
451 lpc2000_info->cmd51_max_buffer = 1024;
452 bank->num_sectors = 8;
453 break;
454 case 64 * 1024:
455 bank->num_sectors = 16;
456 break;
457 case 128 * 1024:
458 bank->num_sectors = 18;
459 break;
460 case 256 * 1024:
461 bank->num_sectors = 22;
462 break;
463 case 512 * 1024:
464 bank->num_sectors = 30;
465 break;
466 default:
467 LOG_ERROR("BUG: unknown bank->size encountered");
468 exit(-1);
469 }
470
471 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
472
473 for (int i = 0; i < bank->num_sectors; i++) {
474 bank->sectors[i].offset = offset;
475 /* sectors 0-15 are 4kB-sized, 16 and above are 32kB-sized for LPC17xx/LPC40xx devices */
476 bank->sectors[i].size = (i < 16) ? 4 * 1024 : 32 * 1024;
477 offset += bank->sectors[i].size;
478 bank->sectors[i].is_erased = -1;
479 bank->sectors[i].is_protected = 1;
480 }
481 } else if (lpc2000_info->variant == lpc4300) {
482 lpc2000_info->cmd51_dst_boundary = 512;
483 lpc2000_info->checksum_vector = 7;
484 lpc2000_info->iap_max_stack = 208;
485
486 switch (bank->size) {
487 case 256 * 1024:
488 bank->num_sectors = 11;
489 break;
490 case 384 * 1024:
491 bank->num_sectors = 13;
492 break;
493 case 512 * 1024:
494 bank->num_sectors = 15;
495 break;
496 default:
497 LOG_ERROR("BUG: unknown bank->size encountered");
498 exit(-1);
499 }
500
501 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
502
503 for (int i = 0; i < bank->num_sectors; i++) {
504 bank->sectors[i].offset = offset;
505 /* sectors 0-7 are 8kB-sized, 8 and above are 64kB-sized for LPC43xx devices */
506 bank->sectors[i].size = (i < 8) ? 8 * 1024 : 64 * 1024;
507 offset += bank->sectors[i].size;
508 bank->sectors[i].is_erased = -1;
509 bank->sectors[i].is_protected = 1;
510 }
511
512 } else if (lpc2000_info->variant == lpc800) {
513 lpc2000_info->cmd51_dst_boundary = 64;
514 lpc2000_info->checksum_vector = 7;
515 lpc2000_info->iap_max_stack = 208; /* 148byte for LPC81x,208byte for LPC82x. */
516 lpc2000_info->cmd51_max_buffer = 256; /* smallest MCU in the series, LPC810, has 1 kB of SRAM */
517
518 switch (bank->size) {
519 case 4 * 1024:
520 bank->num_sectors = 4;
521 break;
522 case 8 * 1024:
523 bank->num_sectors = 8;
524 break;
525 case 16 * 1024:
526 bank->num_sectors = 16;
527 break;
528 case 32 * 1024:
529 lpc2000_info->cmd51_max_buffer = 1024; /* For LPC824, has 8kB of SRAM */
530 bank->num_sectors = 32;
531 break;
532 default:
533 LOG_ERROR("BUG: unknown bank->size encountered");
534 exit(-1);
535 }
536
537 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
538
539 for (int i = 0; i < bank->num_sectors; i++) {
540 bank->sectors[i].offset = offset;
541 /* all sectors are 1kB-sized for LPC8xx devices */
542 bank->sectors[i].size = 1 * 1024;
543 offset += bank->sectors[i].size;
544 bank->sectors[i].is_erased = -1;
545 bank->sectors[i].is_protected = 1;
546 }
547
548 } else if (lpc2000_info->variant == lpc1100) {
549 lpc2000_info->cmd51_dst_boundary = 256;
550 lpc2000_info->checksum_vector = 7;
551 lpc2000_info->iap_max_stack = 128;
552
553 if ((bank->size % (4 * 1024)) != 0) {
554 LOG_ERROR("BUG: unknown bank->size encountered,\nLPC1100 flash size must be a multiple of 4096");
555 exit(-1);
556 }
557 lpc2000_info->cmd51_max_buffer = 512; /* smallest MCU in the series, LPC1110, has 1 kB of SRAM */
558 bank->num_sectors = bank->size / 4096;
559
560 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
561
562 for (int i = 0; i < bank->num_sectors; i++) {
563 bank->sectors[i].offset = offset;
564 /* all sectors are 4kB-sized */
565 bank->sectors[i].size = 4 * 1024;
566 offset += bank->sectors[i].size;
567 bank->sectors[i].is_erased = -1;
568 bank->sectors[i].is_protected = 1;
569 }
570
571 } else if (lpc2000_info->variant == lpc1500) {
572 lpc2000_info->cmd51_dst_boundary = 256;
573 lpc2000_info->checksum_vector = 7;
574 lpc2000_info->iap_max_stack = 128;
575
576 switch (bank->size) {
577 case 64 * 1024:
578 bank->num_sectors = 16;
579 break;
580 case 128 * 1024:
581 bank->num_sectors = 32;
582 break;
583 case 256 * 1024:
584 bank->num_sectors = 64;
585 break;
586 default:
587 LOG_ERROR("BUG: unknown bank->size encountered");
588 exit(-1);
589 }
590
591 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
592
593 for (int i = 0; i < bank->num_sectors; i++) {
594 bank->sectors[i].offset = offset;
595 /* all sectors are 4kB-sized */
596 bank->sectors[i].size = 4 * 1024;
597 offset += bank->sectors[i].size;
598 bank->sectors[i].is_erased = -1;
599 bank->sectors[i].is_protected = 1;
600 }
601
602 } else if (lpc2000_info->variant == lpc54100) {
603 lpc2000_info->cmd51_dst_boundary = 256;
604 lpc2000_info->checksum_vector = 7;
605 lpc2000_info->iap_max_stack = 128;
606
607 switch (bank->size) {
608 case 256 * 1024:
609 bank->num_sectors = 8;
610 break;
611 case 512 * 1024:
612 bank->num_sectors = 16;
613 break;
614 default:
615 LOG_ERROR("BUG: unknown bank->size encountered");
616 exit(-1);
617 }
618
619 bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
620
621 for (int i = 0; i < bank->num_sectors; i++) {
622 bank->sectors[i].offset = offset;
623 /* all sectors are 32kB-sized */
624 bank->sectors[i].size = 32 * 1024;
625 offset += bank->sectors[i].size;
626 bank->sectors[i].is_erased = -1;
627 bank->sectors[i].is_protected = 1;
628 }
629
630 } else {
631 LOG_ERROR("BUG: unknown lpc2000_info->variant encountered");
632 exit(-1);
633 }
634
635 return ERROR_OK;
636 }
637
638 /* this function allocates and initializes working area used for IAP algorithm
639 * uses 52 + max IAP stack bytes working area
640 * 0x0 to 0x7: jump gate (BX to thumb state, b -2 to wait)
641 * 0x8 to 0x1f: command parameter table (1+5 words)
642 * 0x20 to 0x33: command result table (1+4 words)
643 * 0x34 to 0xb3|0x104: stack
644 * (128b needed for lpc1xxx/2000/5410x, 208b for lpc43xx/lpc82x and 148b for lpc81x)
645 */
646
647 static int lpc2000_iap_working_area_init(struct flash_bank *bank, struct working_area **iap_working_area)
648 {
649 struct target *target = bank->target;
650 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
651
652 if (target_alloc_working_area(target, IAP_CODE_LEN + lpc2000_info->iap_max_stack, iap_working_area) != ERROR_OK) {
653 LOG_ERROR("no working area specified, can't write LPC2000 internal flash");
654 return ERROR_FLASH_OPERATION_FAILED;
655 }
656
657 uint8_t jump_gate[8];
658
659 /* write IAP code to working area */
660 switch (lpc2000_info->variant) {
661 case lpc800:
662 case lpc1100:
663 case lpc1500:
664 case lpc1700:
665 case lpc4300:
666 case lpc54100:
667 case lpc_auto:
668 target_buffer_set_u32(target, jump_gate, ARMV4_5_T_BX(12));
669 target_buffer_set_u32(target, jump_gate + 4, ARMV5_T_BKPT(0));
670 break;
671 case lpc2000_v1:
672 case lpc2000_v2:
673 target_buffer_set_u32(target, jump_gate, ARMV4_5_BX(12));
674 target_buffer_set_u32(target, jump_gate + 4, ARMV4_5_B(0xfffffe, 0));
675 break;
676 default:
677 LOG_ERROR("BUG: unknown lpc2000_info->variant encountered");
678 exit(-1);
679 }
680
681 int retval = target_write_memory(target, (*iap_working_area)->address, 4, 2, jump_gate);
682 if (retval != ERROR_OK) {
683 LOG_ERROR("Write memory at address 0x%8.8" PRIx32 " failed (check work_area definition)",
684 (*iap_working_area)->address);
685 target_free_working_area(target, *iap_working_area);
686 }
687
688 return retval;
689 }
690
691 /* call LPC8xx/LPC1xxx/LPC4xxx/LPC5410x/LPC2000 IAP function */
692
693 static int lpc2000_iap_call(struct flash_bank *bank, struct working_area *iap_working_area, int code,
694 uint32_t param_table[5], uint32_t result_table[4])
695 {
696 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
697 struct target *target = bank->target;
698
699 struct arm_algorithm arm_algo; /* for LPC2000 */
700 struct armv7m_algorithm armv7m_info; /* for LPC8xx/LPC1xxx/LPC4xxx/LPC5410x */
701 uint32_t iap_entry_point = 0; /* to make compiler happier */
702
703 switch (lpc2000_info->variant) {
704 case lpc800:
705 case lpc1100:
706 case lpc1700:
707 case lpc_auto:
708 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
709 armv7m_info.core_mode = ARM_MODE_THREAD;
710 iap_entry_point = 0x1fff1ff1;
711 break;
712 case lpc1500:
713 case lpc54100:
714 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
715 armv7m_info.core_mode = ARM_MODE_THREAD;
716 iap_entry_point = 0x03000205;
717 break;
718 case lpc2000_v1:
719 case lpc2000_v2:
720 arm_algo.common_magic = ARM_COMMON_MAGIC;
721 arm_algo.core_mode = ARM_MODE_SVC;
722 arm_algo.core_state = ARM_STATE_ARM;
723 iap_entry_point = 0x7ffffff1;
724 break;
725 case lpc4300:
726 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
727 armv7m_info.core_mode = ARM_MODE_THREAD;
728 /* read out IAP entry point from ROM driver table at 0x10400100 */
729 target_read_u32(target, 0x10400100, &iap_entry_point);
730 break;
731 default:
732 LOG_ERROR("BUG: unknown lpc2000->variant encountered");
733 exit(-1);
734 }
735
736 struct mem_param mem_params[2];
737
738 /* command parameter table */
739 init_mem_param(&mem_params[0], iap_working_area->address + 8, 6 * 4, PARAM_OUT);
740 target_buffer_set_u32(target, mem_params[0].value, code);
741 target_buffer_set_u32(target, mem_params[0].value + 0x04, param_table[0]);
742 target_buffer_set_u32(target, mem_params[0].value + 0x08, param_table[1]);
743 target_buffer_set_u32(target, mem_params[0].value + 0x0c, param_table[2]);
744 target_buffer_set_u32(target, mem_params[0].value + 0x10, param_table[3]);
745 target_buffer_set_u32(target, mem_params[0].value + 0x14, param_table[4]);
746
747 struct reg_param reg_params[5];
748
749 init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
750 buf_set_u32(reg_params[0].value, 0, 32, iap_working_area->address + 0x08);
751
752 /* command result table */
753 init_mem_param(&mem_params[1], iap_working_area->address + 0x20, 5 * 4, PARAM_IN);
754
755 init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
756 buf_set_u32(reg_params[1].value, 0, 32, iap_working_area->address + 0x20);
757
758 /* IAP entry point */
759 init_reg_param(&reg_params[2], "r12", 32, PARAM_OUT);
760 buf_set_u32(reg_params[2].value, 0, 32, iap_entry_point);
761
762 switch (lpc2000_info->variant) {
763 case lpc800:
764 case lpc1100:
765 case lpc1500:
766 case lpc1700:
767 case lpc4300:
768 case lpc54100:
769 case lpc_auto:
770 /* IAP stack */
771 init_reg_param(&reg_params[3], "sp", 32, PARAM_OUT);
772 buf_set_u32(reg_params[3].value, 0, 32,
773 iap_working_area->address + IAP_CODE_LEN + lpc2000_info->iap_max_stack);
774
775 /* return address */
776 init_reg_param(&reg_params[4], "lr", 32, PARAM_OUT);
777 buf_set_u32(reg_params[4].value, 0, 32, (iap_working_area->address + 0x04) | 1);
778 /* bit0 of LR = 1 to return in Thumb mode */
779
780 target_run_algorithm(target, 2, mem_params, 5, reg_params, iap_working_area->address, 0, 10000,
781 &armv7m_info);
782 break;
783 case lpc2000_v1:
784 case lpc2000_v2:
785 /* IAP stack */
786 init_reg_param(&reg_params[3], "sp_svc", 32, PARAM_OUT);
787 buf_set_u32(reg_params[3].value, 0, 32,
788 iap_working_area->address + IAP_CODE_LEN + lpc2000_info->iap_max_stack);
789
790 /* return address */
791 init_reg_param(&reg_params[4], "lr_svc", 32, PARAM_OUT);
792 buf_set_u32(reg_params[4].value, 0, 32, iap_working_area->address + 0x04);
793
794 target_run_algorithm(target, 2, mem_params, 5, reg_params, iap_working_area->address,
795 iap_working_area->address + 0x4, 10000, &arm_algo);
796 break;
797 default:
798 LOG_ERROR("BUG: unknown lpc2000->variant encountered");
799 exit(-1);
800 }
801
802 int status_code = target_buffer_get_u32(target, mem_params[1].value);
803 result_table[0] = target_buffer_get_u32(target, mem_params[1].value + 0x04);
804 result_table[1] = target_buffer_get_u32(target, mem_params[1].value + 0x08);
805 result_table[2] = target_buffer_get_u32(target, mem_params[1].value + 0x0c);
806 result_table[3] = target_buffer_get_u32(target, mem_params[1].value + 0x10);
807
808 LOG_DEBUG("IAP command = %i (0x%8.8" PRIx32 ", 0x%8.8" PRIx32 ", 0x%8.8" PRIx32 ", 0x%8.8" PRIx32 ", 0x%8.8" PRIx32
809 ") completed with result = %8.8x",
810 code, param_table[0], param_table[1], param_table[2], param_table[3], param_table[4], status_code);
811
812 destroy_mem_param(&mem_params[0]);
813 destroy_mem_param(&mem_params[1]);
814
815 destroy_reg_param(&reg_params[0]);
816 destroy_reg_param(&reg_params[1]);
817 destroy_reg_param(&reg_params[2]);
818 destroy_reg_param(&reg_params[3]);
819 destroy_reg_param(&reg_params[4]);
820
821 return status_code;
822 }
823
824 static int lpc2000_iap_blank_check(struct flash_bank *bank, int first, int last)
825 {
826 if ((first < 0) || (last >= bank->num_sectors))
827 return ERROR_FLASH_SECTOR_INVALID;
828
829 uint32_t param_table[5] = {0};
830 uint32_t result_table[4];
831 struct working_area *iap_working_area;
832
833 int retval = lpc2000_iap_working_area_init(bank, &iap_working_area);
834
835 if (retval != ERROR_OK)
836 return retval;
837
838 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
839 if (lpc2000_info->variant == lpc4300)
840 param_table[2] = lpc2000_info->lpc4300_bank;
841
842 for (int i = first; i <= last && retval == ERROR_OK; i++) {
843 /* check single sector */
844 param_table[0] = param_table[1] = i;
845 int status_code = lpc2000_iap_call(bank, iap_working_area, 53, param_table, result_table);
846
847 switch (status_code) {
848 case ERROR_FLASH_OPERATION_FAILED:
849 retval = ERROR_FLASH_OPERATION_FAILED;
850 break;
851 case LPC2000_CMD_SUCCESS:
852 bank->sectors[i].is_erased = 1;
853 break;
854 case LPC2000_SECTOR_NOT_BLANK:
855 bank->sectors[i].is_erased = 0;
856 break;
857 case LPC2000_INVALID_SECTOR:
858 bank->sectors[i].is_erased = 0;
859 break;
860 case LPC2000_BUSY:
861 retval = ERROR_FLASH_BUSY;
862 break;
863 default:
864 LOG_ERROR("BUG: unknown LPC2000 status code %i", status_code);
865 exit(-1);
866 }
867 }
868
869 struct target *target = bank->target;
870 target_free_working_area(target, iap_working_area);
871
872 return retval;
873 }
874
875 /*
876 * flash bank lpc2000 <base> <size> 0 0 <target#> <lpc_variant> <cclk> [calc_checksum]
877 */
878 FLASH_BANK_COMMAND_HANDLER(lpc2000_flash_bank_command)
879 {
880 if (CMD_ARGC < 8)
881 return ERROR_COMMAND_SYNTAX_ERROR;
882
883 struct lpc2000_flash_bank *lpc2000_info = calloc(1, sizeof(*lpc2000_info));
884 lpc2000_info->probed = false;
885
886 bank->driver_priv = lpc2000_info;
887
888 if (strcmp(CMD_ARGV[6], "lpc2000_v1") == 0) {
889 lpc2000_info->variant = lpc2000_v1;
890 } else if (strcmp(CMD_ARGV[6], "lpc2000_v2") == 0) {
891 lpc2000_info->variant = lpc2000_v2;
892 } else if (strcmp(CMD_ARGV[6], "lpc1700") == 0 || strcmp(CMD_ARGV[6], "lpc4000") == 0) {
893 lpc2000_info->variant = lpc1700;
894 } else if (strcmp(CMD_ARGV[6], "lpc1800") == 0 || strcmp(CMD_ARGV[6], "lpc4300") == 0) {
895 lpc2000_info->variant = lpc4300;
896 } else if (strcmp(CMD_ARGV[6], "lpc800") == 0) {
897 lpc2000_info->variant = lpc800;
898 } else if (strcmp(CMD_ARGV[6], "lpc1100") == 0) {
899 lpc2000_info->variant = lpc1100;
900 } else if (strcmp(CMD_ARGV[6], "lpc1500") == 0) {
901 lpc2000_info->variant = lpc1500;
902 } else if (strcmp(CMD_ARGV[6], "lpc54100") == 0) {
903 lpc2000_info->variant = lpc54100;
904 } else if (strcmp(CMD_ARGV[6], "auto") == 0) {
905 lpc2000_info->variant = lpc_auto;
906 } else {
907 LOG_ERROR("unknown LPC2000 variant: %s", CMD_ARGV[6]);
908 free(lpc2000_info);
909 return ERROR_FLASH_BANK_INVALID;
910 }
911
912 /* Maximum size required for the IAP stack.
913 This value only gets used when probing, only for auto, lpc1100 and lpc1700.
914 We use the maximum size for any part supported by the driver(!) to be safe
915 in case the auto variant is mistakenly used on a MCU from one of the series
916 for which we don't support auto-probing. */
917 lpc2000_info->iap_max_stack = 208;
918
919 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[7], lpc2000_info->cclk);
920 lpc2000_info->calc_checksum = 0;
921
922 uint32_t temp_base = 0;
923 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], temp_base);
924 if (temp_base >= 0x1B000000)
925 lpc2000_info->lpc4300_bank = 1; /* bank B */
926 else
927 lpc2000_info->lpc4300_bank = 0; /* bank A */
928
929 if (CMD_ARGC >= 9) {
930 if (strcmp(CMD_ARGV[8], "calc_checksum") == 0)
931 lpc2000_info->calc_checksum = 1;
932 }
933
934 return ERROR_OK;
935 }
936
937 static int lpc2000_erase(struct flash_bank *bank, int first, int last)
938 {
939 if (bank->target->state != TARGET_HALTED) {
940 LOG_ERROR("Target not halted");
941 return ERROR_TARGET_NOT_HALTED;
942 }
943
944 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
945 uint32_t param_table[5] = {0};
946
947 param_table[0] = first;
948 param_table[1] = last;
949
950 if (lpc2000_info->variant == lpc4300)
951 param_table[2] = lpc2000_info->lpc4300_bank;
952 else
953 param_table[2] = lpc2000_info->cclk;
954
955 uint32_t result_table[4];
956 struct working_area *iap_working_area;
957
958 int retval = lpc2000_iap_working_area_init(bank, &iap_working_area);
959
960 if (retval != ERROR_OK)
961 return retval;
962
963 if (lpc2000_info->variant == lpc4300)
964 /* Init IAP Anyway */
965 lpc2000_iap_call(bank, iap_working_area, 49, param_table, result_table);
966
967 /* Prepare sectors */
968 int status_code = lpc2000_iap_call(bank, iap_working_area, 50, param_table, result_table);
969 switch (status_code) {
970 case ERROR_FLASH_OPERATION_FAILED:
971 retval = ERROR_FLASH_OPERATION_FAILED;
972 break;
973 case LPC2000_CMD_SUCCESS:
974 break;
975 case LPC2000_INVALID_SECTOR:
976 retval = ERROR_FLASH_SECTOR_INVALID;
977 break;
978 default:
979 LOG_WARNING("lpc2000 prepare sectors returned %i", status_code);
980 retval = ERROR_FLASH_OPERATION_FAILED;
981 break;
982 }
983
984 if (retval == ERROR_OK) {
985 /* Erase sectors */
986 param_table[2] = lpc2000_info->cclk;
987 if (lpc2000_info->variant == lpc4300)
988 param_table[3] = lpc2000_info->lpc4300_bank;
989
990 status_code = lpc2000_iap_call(bank, iap_working_area, 52, param_table, result_table);
991 switch (status_code) {
992 case ERROR_FLASH_OPERATION_FAILED:
993 retval = ERROR_FLASH_OPERATION_FAILED;
994 break;
995 case LPC2000_CMD_SUCCESS:
996 break;
997 case LPC2000_INVALID_SECTOR:
998 retval = ERROR_FLASH_SECTOR_INVALID;
999 break;
1000 default:
1001 LOG_WARNING("lpc2000 erase sectors returned %i", status_code);
1002 retval = ERROR_FLASH_OPERATION_FAILED;
1003 break;
1004 }
1005 }
1006
1007 struct target *target = bank->target;
1008 target_free_working_area(target, iap_working_area);
1009
1010 return retval;
1011 }
1012
1013 static int lpc2000_protect(struct flash_bank *bank, int set, int first, int last)
1014 {
1015 /* can't protect/unprotect on the lpc2000 */
1016 return ERROR_OK;
1017 }
1018
1019 static int lpc2000_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
1020 {
1021 struct target *target = bank->target;
1022
1023 if (bank->target->state != TARGET_HALTED) {
1024 LOG_ERROR("Target not halted");
1025 return ERROR_TARGET_NOT_HALTED;
1026 }
1027
1028 if (offset + count > bank->size)
1029 return ERROR_FLASH_DST_OUT_OF_BANK;
1030
1031 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
1032
1033 uint32_t dst_min_alignment = lpc2000_info->cmd51_dst_boundary;
1034
1035 if (offset % dst_min_alignment) {
1036 LOG_WARNING("offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32, offset, dst_min_alignment);
1037 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
1038 }
1039
1040 int first_sector = 0;
1041 int last_sector = 0;
1042
1043 for (int i = 0; i < bank->num_sectors; i++) {
1044 if (offset >= bank->sectors[i].offset)
1045 first_sector = i;
1046 if (offset + DIV_ROUND_UP(count, dst_min_alignment) * dst_min_alignment > bank->sectors[i].offset)
1047 last_sector = i;
1048 }
1049
1050 LOG_DEBUG("first_sector: %i, last_sector: %i", first_sector, last_sector);
1051
1052 /* check if exception vectors should be flashed */
1053 if ((offset == 0) && (count >= 0x20) && lpc2000_info->calc_checksum) {
1054 assert(lpc2000_info->checksum_vector < 8);
1055 uint32_t checksum = 0;
1056 for (int i = 0; i < 8; i++) {
1057 LOG_DEBUG("Vector 0x%2.2x: 0x%8.8" PRIx32, i * 4, buf_get_u32(buffer + (i * 4), 0, 32));
1058 if (i != lpc2000_info->checksum_vector)
1059 checksum += buf_get_u32(buffer + (i * 4), 0, 32);
1060 }
1061 checksum = 0 - checksum;
1062 LOG_DEBUG("checksum: 0x%8.8" PRIx32, checksum);
1063
1064 uint32_t original_value = buf_get_u32(buffer + (lpc2000_info->checksum_vector * 4), 0, 32);
1065 if (original_value != checksum) {
1066 LOG_WARNING("Verification will fail since checksum in image (0x%8.8" PRIx32 ") to be written to flash is "
1067 "different from calculated vector checksum (0x%8.8" PRIx32 ").", original_value, checksum);
1068 LOG_WARNING("To remove this warning modify build tools on developer PC to inject correct LPC vector "
1069 "checksum.");
1070 }
1071
1072 /* FIXME: WARNING! This code is broken because it modifies the callers buffer in place. */
1073 buf_set_u32((uint8_t *)buffer + (lpc2000_info->checksum_vector * 4), 0, 32, checksum);
1074 }
1075
1076 struct working_area *iap_working_area;
1077
1078 int retval = lpc2000_iap_working_area_init(bank, &iap_working_area);
1079
1080 if (retval != ERROR_OK)
1081 return retval;
1082
1083 struct working_area *download_area;
1084
1085 /* allocate a working area */
1086 if (target_alloc_working_area(target, lpc2000_info->cmd51_max_buffer, &download_area) != ERROR_OK) {
1087 LOG_ERROR("no working area specified, can't write LPC2000 internal flash");
1088 target_free_working_area(target, iap_working_area);
1089 return ERROR_FLASH_OPERATION_FAILED;
1090 }
1091
1092 uint32_t bytes_remaining = count;
1093 uint32_t bytes_written = 0;
1094 uint32_t param_table[5] = {0};
1095 uint32_t result_table[4];
1096
1097 if (lpc2000_info->variant == lpc4300)
1098 /* Init IAP Anyway */
1099 lpc2000_iap_call(bank, iap_working_area, 49, param_table, result_table);
1100
1101 while (bytes_remaining > 0) {
1102 uint32_t thisrun_bytes;
1103 if (bytes_remaining >= lpc2000_info->cmd51_max_buffer)
1104 thisrun_bytes = lpc2000_info->cmd51_max_buffer;
1105 else
1106 thisrun_bytes = lpc2000_info->cmd51_dst_boundary;
1107
1108 /* Prepare sectors */
1109 param_table[0] = first_sector;
1110 param_table[1] = last_sector;
1111
1112 if (lpc2000_info->variant == lpc4300)
1113 param_table[2] = lpc2000_info->lpc4300_bank;
1114 else
1115 param_table[2] = lpc2000_info->cclk;
1116
1117 int status_code = lpc2000_iap_call(bank, iap_working_area, 50, param_table, result_table);
1118 switch (status_code) {
1119 case ERROR_FLASH_OPERATION_FAILED:
1120 retval = ERROR_FLASH_OPERATION_FAILED;
1121 break;
1122 case LPC2000_CMD_SUCCESS:
1123 break;
1124 case LPC2000_INVALID_SECTOR:
1125 retval = ERROR_FLASH_SECTOR_INVALID;
1126 break;
1127 default:
1128 LOG_WARNING("lpc2000 prepare sectors returned %i", status_code);
1129 retval = ERROR_FLASH_OPERATION_FAILED;
1130 break;
1131 }
1132
1133 /* Exit if error occured */
1134 if (retval != ERROR_OK)
1135 break;
1136
1137 if (bytes_remaining >= thisrun_bytes) {
1138 retval = target_write_buffer(bank->target, download_area->address, thisrun_bytes, buffer + bytes_written);
1139 if (retval != ERROR_OK) {
1140 retval = ERROR_FLASH_OPERATION_FAILED;
1141 break;
1142 }
1143 } else {
1144 uint8_t *last_buffer = malloc(thisrun_bytes);
1145 memcpy(last_buffer, buffer + bytes_written, bytes_remaining);
1146 memset(last_buffer + bytes_remaining, 0xff, thisrun_bytes - bytes_remaining);
1147 target_write_buffer(bank->target, download_area->address, thisrun_bytes, last_buffer);
1148 free(last_buffer);
1149 }
1150
1151 LOG_DEBUG("writing 0x%" PRIx32 " bytes to address 0x%" PRIx32, thisrun_bytes,
1152 bank->base + offset + bytes_written);
1153
1154 /* Write data */
1155 param_table[0] = bank->base + offset + bytes_written;
1156 param_table[1] = download_area->address;
1157 param_table[2] = thisrun_bytes;
1158 param_table[3] = lpc2000_info->cclk;
1159 status_code = lpc2000_iap_call(bank, iap_working_area, 51, param_table, result_table);
1160 switch (status_code) {
1161 case ERROR_FLASH_OPERATION_FAILED:
1162 retval = ERROR_FLASH_OPERATION_FAILED;
1163 break;
1164 case LPC2000_CMD_SUCCESS:
1165 break;
1166 case LPC2000_INVALID_SECTOR:
1167 retval = ERROR_FLASH_SECTOR_INVALID;
1168 break;
1169 default:
1170 LOG_WARNING("lpc2000 returned %i", status_code);
1171 retval = ERROR_FLASH_OPERATION_FAILED;
1172 break;
1173 }
1174
1175 /* Exit if error occured */
1176 if (retval != ERROR_OK)
1177 break;
1178
1179 if (bytes_remaining > thisrun_bytes)
1180 bytes_remaining -= thisrun_bytes;
1181 else
1182 bytes_remaining = 0;
1183 bytes_written += thisrun_bytes;
1184 }
1185
1186 target_free_working_area(target, iap_working_area);
1187 target_free_working_area(target, download_area);
1188
1189 return retval;
1190 }
1191
1192 static int get_lpc2000_part_id(struct flash_bank *bank, uint32_t *part_id)
1193 {
1194 if (bank->target->state != TARGET_HALTED) {
1195 LOG_ERROR("Target not halted");
1196 return ERROR_TARGET_NOT_HALTED;
1197 }
1198
1199 uint32_t param_table[5] = {0};
1200 uint32_t result_table[4];
1201 struct working_area *iap_working_area;
1202
1203 int retval = lpc2000_iap_working_area_init(bank, &iap_working_area);
1204
1205 if (retval != ERROR_OK)
1206 return retval;
1207
1208 /* The status seems to be bogus with the part ID command on some IAP
1209 firmwares, so ignore it. */
1210 lpc2000_iap_call(bank, iap_working_area, 54, param_table, result_table);
1211
1212 struct target *target = bank->target;
1213 target_free_working_area(target, iap_working_area);
1214
1215 /* If the result is zero, the command probably didn't work out. */
1216 if (result_table[0] == 0)
1217 return LPC2000_INVALID_COMMAND;
1218
1219 *part_id = result_table[0];
1220 return LPC2000_CMD_SUCCESS;
1221 }
1222
1223 static int lpc2000_auto_probe_flash(struct flash_bank *bank)
1224 {
1225 uint32_t part_id;
1226 int retval;
1227 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
1228
1229 if (bank->target->state != TARGET_HALTED) {
1230 LOG_ERROR("Target not halted");
1231 return ERROR_TARGET_NOT_HALTED;
1232 }
1233
1234 retval = get_lpc2000_part_id(bank, &part_id);
1235 if (retval != LPC2000_CMD_SUCCESS) {
1236 LOG_ERROR("Could not get part ID");
1237 return retval;
1238 }
1239
1240 switch (part_id) {
1241 case LPC1110_1:
1242 case LPC1110_2:
1243 lpc2000_info->variant = lpc1100;
1244 bank->size = 4 * 1024;
1245 break;
1246
1247 case LPC1111_002_1:
1248 case LPC1111_002_2:
1249 case LPC1111_101_1:
1250 case LPC1111_101_2:
1251 case LPC1111_103_1:
1252 case LPC1111_201_1:
1253 case LPC1111_201_2:
1254 case LPC1111_203_1:
1255 case LPC11A11_001_1:
1256 case LPC11E11_101:
1257 case LPC1311:
1258 case LPC1311_1:
1259 lpc2000_info->variant = lpc1100;
1260 bank->size = 8 * 1024;
1261 break;
1262
1263 case LPC1112_101_1:
1264 case LPC1112_101_2:
1265 case LPC1112_102_1:
1266 case LPC1112_102_2:
1267 case LPC1112_103_1:
1268 case LPC1112_201_1:
1269 case LPC1112_201_2:
1270 case LPC1112_203_1:
1271 case LPC11A02_1:
1272 case LPC11C12_301_1:
1273 case LPC11C22_301_1:
1274 case LPC11A12_101_1:
1275 case LPC11E12_201:
1276 case LPC11U12_201_1:
1277 case LPC11U12_201_2:
1278 case LPC1342:
1279 lpc2000_info->variant = lpc1100;
1280 bank->size = 16 * 1024;
1281 break;
1282
1283 case LPC1113_201_1:
1284 case LPC1113_201_2:
1285 case LPC1113_203_1:
1286 case LPC1113_301_1:
1287 case LPC1113_301_2:
1288 case LPC1113_303_1:
1289 case LPC11A13_201_1:
1290 case LPC11E13_301:
1291 case LPC11U13_201_1:
1292 case LPC11U13_201_2:
1293 case LPC11U23_301:
1294 lpc2000_info->variant = lpc1100;
1295 bank->size = 24 * 1024;
1296 break;
1297
1298 case LPC1114_102_1:
1299 case LPC1114_102_2:
1300 case LPC1114_201_1:
1301 case LPC1114_201_2:
1302 case LPC1114_203_1:
1303 case LPC1114_301_1:
1304 case LPC1114_301_2:
1305 case LPC1114_303_1:
1306 case LPC11A04_1:
1307 case LPC11A14_301_1:
1308 case LPC11A14_301_2:
1309 case LPC11C14_301_1:
1310 case LPC11C24_301_1:
1311 case LPC11E14_401:
1312 case LPC11U14_201_1:
1313 case LPC11U14_201_2:
1314 case LPC11U24_301:
1315 case LPC11U24_401:
1316 case LPC1313:
1317 case LPC1313_1:
1318 case LPC1315:
1319 case LPC1343:
1320 case LPC1345:
1321 lpc2000_info->variant = lpc1100;
1322 bank->size = 32 * 1024;
1323 break;
1324
1325 case LPC1751_1:
1326 case LPC1751_2:
1327 lpc2000_info->variant = lpc1700;
1328 bank->size = 32 * 1024;
1329 break;
1330
1331 case LPC11U34_311:
1332 lpc2000_info->variant = lpc1100;
1333 bank->size = 40 * 1024;
1334 break;
1335
1336 case LPC1114_323_1:
1337 case LPC11U34_421:
1338 case LPC1316:
1339 case LPC1346:
1340 lpc2000_info->variant = lpc1100;
1341 bank->size = 48 * 1024;
1342 break;
1343
1344 case LPC1114_333_1:
1345 lpc2000_info->variant = lpc1100;
1346 bank->size = 56 * 1024;
1347 break;
1348
1349 case LPC1115_303_1:
1350 case LPC11U35_401:
1351 case LPC11U35_501:
1352 case LPC11E66:
1353 case LPC11U66:
1354 case LPC1317:
1355 case LPC1347:
1356 lpc2000_info->variant = lpc1100;
1357 bank->size = 64 * 1024;
1358 break;
1359
1360 case LPC1752:
1361 case LPC4072:
1362 lpc2000_info->variant = lpc1700;
1363 bank->size = 64 * 1024;
1364 break;
1365
1366 case LPC11E36_501:
1367 case LPC11U36_401:
1368 lpc2000_info->variant = lpc1100;
1369 bank->size = 96 * 1024;
1370 break;
1371
1372 case LPC11E37_401:
1373 case LPC11E37_501:
1374 case LPC11U37_401:
1375 case LPC11U37H_401:
1376 case LPC11U37_501:
1377 case LPC11E67:
1378 case LPC11E68:
1379 case LPC11U67_1:
1380 case LPC11U67_2:
1381 lpc2000_info->variant = lpc1100;
1382 bank->size = 128 * 1024;
1383 break;
1384
1385 case LPC1754:
1386 case LPC1764:
1387 case LPC1774:
1388 case LPC4074:
1389 lpc2000_info->variant = lpc1700;
1390 bank->size = 128 * 1024;
1391 break;
1392
1393 case LPC11U68_1:
1394 case LPC11U68_2:
1395 lpc2000_info->variant = lpc1100;
1396 bank->size = 256 * 1024;
1397 break;
1398
1399 case LPC1756:
1400 case LPC1763:
1401 case LPC1765:
1402 case LPC1766:
1403 case LPC1776:
1404 case LPC1785:
1405 case LPC1786:
1406 case LPC4076:
1407 lpc2000_info->variant = lpc1700;
1408 bank->size = 256 * 1024;
1409 break;
1410
1411 case LPC1758:
1412 case LPC1759:
1413 case LPC1767:
1414 case LPC1768:
1415 case LPC1769:
1416 case LPC1777:
1417 case LPC1778:
1418 case LPC1787:
1419 case LPC1788:
1420 case LPC4078:
1421 case LPC4088:
1422 lpc2000_info->variant = lpc1700;
1423 bank->size = 512 * 1024;
1424 break;
1425
1426 case LPC810_021:
1427 lpc2000_info->variant = lpc800;
1428 bank->size = 4 * 1024;
1429 break;
1430
1431 case LPC811_001:
1432 lpc2000_info->variant = lpc800;
1433 bank->size = 8 * 1024;
1434 break;
1435
1436 case LPC812_101:
1437 case LPC812_101_1:
1438 case LPC812_101_2:
1439 case LPC812_101_3:
1440 case LPC822_101:
1441 case LPC822_101_1:
1442 lpc2000_info->variant = lpc800;
1443 bank->size = 16 * 1024;
1444 break;
1445
1446 case LPC824_201:
1447 case LPC824_201_1:
1448 lpc2000_info->variant = lpc800;
1449 bank->size = 32 * 1024;
1450 break;
1451
1452 default:
1453 LOG_ERROR("BUG: unknown Part ID encountered: 0x%x", part_id);
1454 exit(-1);
1455 }
1456
1457 return ERROR_OK;
1458 }
1459
1460 static int lpc2000_probe(struct flash_bank *bank)
1461 {
1462 int status;
1463 uint32_t part_id;
1464 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
1465
1466 if (!lpc2000_info->probed) {
1467 if (lpc2000_info->variant == lpc_auto) {
1468 status = lpc2000_auto_probe_flash(bank);
1469 if (status != ERROR_OK)
1470 return status;
1471 } else if (lpc2000_info->variant == lpc1100 || lpc2000_info->variant == lpc1700) {
1472 status = get_lpc2000_part_id(bank, &part_id);
1473 if (status == LPC2000_CMD_SUCCESS)
1474 LOG_INFO("If auto-detection fails for this part, please email "
1475 "openocd-devel@lists.sourceforge.net, citing part id 0x%x.\n", part_id);
1476 }
1477
1478 lpc2000_build_sector_list(bank);
1479 lpc2000_info->probed = true;
1480 }
1481
1482 return ERROR_OK;
1483 }
1484
1485 static int lpc2000_erase_check(struct flash_bank *bank)
1486 {
1487 if (bank->target->state != TARGET_HALTED) {
1488 LOG_ERROR("Target not halted");
1489 return ERROR_TARGET_NOT_HALTED;
1490 }
1491
1492 return lpc2000_iap_blank_check(bank, 0, bank->num_sectors - 1);
1493 }
1494
1495 static int lpc2000_protect_check(struct flash_bank *bank)
1496 {
1497 /* sectors are always protected */
1498 return ERROR_OK;
1499 }
1500
1501 static int get_lpc2000_info(struct flash_bank *bank, char *buf, int buf_size)
1502 {
1503 struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
1504
1505 snprintf(buf, buf_size, "lpc2000 flash driver variant: %i, clk: %" PRIi32 "kHz", lpc2000_info->variant,
1506 lpc2000_info->cclk);
1507
1508 return ERROR_OK;
1509 }
1510
1511 COMMAND_HANDLER(lpc2000_handle_part_id_command)
1512 {
1513 if (CMD_ARGC < 1)
1514 return ERROR_COMMAND_SYNTAX_ERROR;
1515
1516 struct flash_bank *bank;
1517 int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1518 if (ERROR_OK != retval)
1519 return retval;
1520
1521 if (bank->target->state != TARGET_HALTED) {
1522 LOG_ERROR("Target not halted");
1523 return ERROR_TARGET_NOT_HALTED;
1524 }
1525
1526 uint32_t part_id;
1527 int status_code = get_lpc2000_part_id(bank, &part_id);
1528 if (status_code != 0x0) {
1529 if (status_code == ERROR_FLASH_OPERATION_FAILED) {
1530 command_print(CMD_CTX, "no sufficient working area specified, can't access LPC2000 IAP interface");
1531 } else
1532 command_print(CMD_CTX, "lpc2000 IAP returned status code %i", status_code);
1533 } else
1534 command_print(CMD_CTX, "lpc2000 part id: 0x%8.8" PRIx32, part_id);
1535
1536 return retval;
1537 }
1538
1539 static const struct command_registration lpc2000_exec_command_handlers[] = {
1540 {
1541 .name = "part_id",
1542 .handler = lpc2000_handle_part_id_command,
1543 .mode = COMMAND_EXEC,
1544 .help = "print part id of lpc2000 flash bank <num>",
1545 .usage = "<bank>",
1546 },
1547 COMMAND_REGISTRATION_DONE
1548 };
1549 static const struct command_registration lpc2000_command_handlers[] = {
1550 {
1551 .name = "lpc2000",
1552 .mode = COMMAND_ANY,
1553 .help = "lpc2000 flash command group",
1554 .usage = "",
1555 .chain = lpc2000_exec_command_handlers,
1556 },
1557 COMMAND_REGISTRATION_DONE
1558 };
1559
1560 struct flash_driver lpc2000_flash = {
1561 .name = "lpc2000",
1562 .commands = lpc2000_command_handlers,
1563 .flash_bank_command = lpc2000_flash_bank_command,
1564 .erase = lpc2000_erase,
1565 .protect = lpc2000_protect,
1566 .write = lpc2000_write,
1567 .read = default_flash_read,
1568 .probe = lpc2000_probe,
1569 .auto_probe = lpc2000_probe,
1570 .erase_check = lpc2000_erase_check,
1571 .protect_check = lpc2000_protect_check,
1572 .info = get_lpc2000_info,
1573 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)