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

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)