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

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)