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