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