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

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)