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