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

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)