1
2
3
7
8
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
38
39
40
41
42
43
44
46
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
102
103
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
123
124
125
126
127
131
132
133
134
135
136
137
138
139
140
141
148
155
156
157
158
159
160
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
188
189
190
191
192
193
194
195
196
197
203
210
211
212
213
214
215
216
217
221
222
223
224
225
226
227
228
229
230
231
232
233
234
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
293
294
295
299
300
301
302
303
304
305
306
307
308
309
320
321
322
323
324
325
326
327
328
329
330
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* ... */
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#define LOAD_TIMER_ERASE 0
#define LOAD_TIMER_WRITE 1
#define FLASH_PAGE_SIZE 512
#define DBGU_CIDR 0x8000507C
#define F_CTRL 0x80102000
#define F_STAT 0x80102004
#define F_PROG_TIME 0x80102008
#define F_WAIT 0x80102010
#define F_CLK_TIME 0x8010201C
/* ... */
#define F_INTEN_CLR 0x80102FD8
#define F_INTEN_SET 0x80102FDC
#define F_INT_STAT 0x80102FE0
#define F_INTEN 0x80102FE4
#define F_INT_CLR 0x80102FE8
#define F_INT_SET 0x80102FEC
#define FLASH_PD 0x80005030
/* ... */
#define FLASH_INIT 0x80005034
/* ... */
#define FC_CS 0x0001
#define FC_FUNC 0x0002
#define FC_WEN 0x0004
#define FC_RD_LATCH 0x0020
#define FC_PROTECT 0x0080
#define FC_SET_DATA 0x0400
#define FC_RSSL 0x0800
#define FC_PROG_REQ 0x1000
#define FC_CLR_BUF 0x4000
#define FC_LOAD_REQ 0x8000
#define FS_DONE 0x0001
#define FS_PROGGNT 0x0002
#define FS_RDY 0x0004
#define FS_ERR 0x0020
#define FPT_TIME_MASK 0x7FFF
#define FPT_ENABLE 0x8000
#define FW_WAIT_STATES_MASK 0x00FF
#define FW_SET_MASK 0xC000
#define FCT_CLK_DIV_MASK 0x0FFF
36 defines
struct lpc288x_flash_bank {
uint32_t working_area;
uint32_t working_area_size;
uint32_t cidr;
const char *target_name;
uint32_t cclk;
uint32_t sector_size_break;
...};
static uint32_t lpc288x_wait_status_busy(struct flash_bank *bank, int timeout);
static void lpc288x_load_timer(int erase, struct target *target);
static void lpc288x_set_flash_clk(struct flash_bank *bank);
static uint32_t lpc288x_system_ready(struct flash_bank *bank);
static uint32_t lpc288x_wait_status_busy(struct flash_bank *bank, int timeout)
{
uint32_t status;
struct target *target = bank->target;
do {
alive_sleep(1);
timeout--;
target_read_u32(target, F_STAT, &status);
...} while (((status & FS_DONE) == 0) && timeout);
if (timeout == 0) {
LOG_DEBUG("Timedout!");
return ERROR_FLASH_OPERATION_FAILED;
}if (timeout == 0) { ... }
return ERROR_OK;
}{ ... }
static int lpc288x_read_part_info(struct flash_bank *bank)
{
struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
struct target *target = bank->target;
uint32_t cidr;
int i = 0;
uint32_t offset;
if (lpc288x_info->cidr == 0x0102100A)
return ERROR_OK;
/* ... */
target_read_u32(target, DBGU_CIDR, &cidr);
if (cidr != 0x0102100A) {
LOG_WARNING("Cannot identify target as an LPC288X (%08" PRIx32 ")", cidr);
return ERROR_FLASH_OPERATION_FAILED;
}if (cidr != 0x0102100A) { ... }
lpc288x_info->cidr = cidr;
lpc288x_info->sector_size_break = 0x000F0000;
lpc288x_info->target_name = "LPC288x";
offset = bank->base;
bank->num_sectors = 23;
bank->sectors = malloc(sizeof(struct flash_sector) * 23);
for (i = 0; i < 15; i++) {
bank->sectors[i].offset = offset;
bank->sectors[i].size = 64 * 1024;
offset += bank->sectors[i].size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 1;
}for (i = 0; i < 15; i++) { ... }
for (i = 15; i < 23; i++) {
bank->sectors[i].offset = offset;
bank->sectors[i].size = 8 * 1024;
offset += bank->sectors[i].size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 1;
}for (i = 15; i < 23; i++) { ... }
return ERROR_OK;
}{ ... }
static int lpc288x_protect_check(struct flash_bank *bank)
{
return ERROR_OK;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(lpc288x_flash_bank_command)
{
struct lpc288x_flash_bank *lpc288x_info;
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
lpc288x_info = malloc(sizeof(struct lpc288x_flash_bank));
bank->driver_priv = lpc288x_info;
lpc288x_info->cidr = 0;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], lpc288x_info->cclk);
return ERROR_OK;
}{ ... }
/* ... */
static void lpc288x_set_flash_clk(struct flash_bank *bank)
{
uint32_t clk_time;
struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
clk_time = (lpc288x_info->cclk / 66000) / 3;
target_write_u32(bank->target, F_CTRL, FC_CS | FC_WEN);
target_write_u32(bank->target, F_CLK_TIME, clk_time);
}{ ... }
/* ... */
static void lpc288x_load_timer(int erase, struct target *target)
{
if (erase == LOAD_TIMER_ERASE)
target_write_u32(target, F_PROG_TIME, FPT_ENABLE | 9500);
else
target_write_u32(target, F_PROG_TIME, FPT_ENABLE | 75);
}{ ... }
static uint32_t lpc288x_system_ready(struct flash_bank *bank)
{
struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
if (lpc288x_info->cidr == 0)
return ERROR_FLASH_BANK_NOT_PROBED;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
return ERROR_OK;
}{ ... }
static int lpc288x_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
uint32_t status;
struct target *target = bank->target;
status = lpc288x_system_ready(bank);
if (status != ERROR_OK)
return status;
if ((last < first) || (last >= bank->num_sectors)) {
LOG_INFO("Bad sector range");
return ERROR_FLASH_SECTOR_INVALID;
}if ((last < first) || (last >= bank->num_sectors)) { ... }
lpc288x_set_flash_clk(bank);
for (unsigned int sector = first; sector <= last; sector++) {
if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
lpc288x_load_timer(LOAD_TIMER_ERASE, target);
target_write_u32(target, bank->sectors[sector].offset, 0x00);
target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_CS);
}for (unsigned int sector = first; sector <= last; sector++) { ... }
if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
return ERROR_OK;
}{ ... }
static int lpc288x_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
{
uint8_t page_buffer[FLASH_PAGE_SIZE];
uint32_t status, source_offset, dest_offset;
struct target *target = bank->target;
uint32_t bytes_remaining = count;
uint32_t first_sector, last_sector, sector, page;
status = lpc288x_system_ready(bank);
if (status != ERROR_OK)
return status;
first_sector = last_sector = 0xffffffff;
for (unsigned int i = 0; i < bank->num_sectors; i++) {
if ((offset >= bank->sectors[i].offset) &&
(offset < (bank->sectors[i].offset + bank->sectors[i].size)) &&
(first_sector == 0xffffffff)) {
first_sector = i;
if (offset % bank->sectors[i].size) {
LOG_INFO(
"offset 0x%" PRIx32 " breaks required alignment 0x%" PRIx32 "",
offset,
bank->sectors[i].size);
return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
}if (offset % bank->sectors[i].size) { ... }
}if ((offset >= bank->sectors[i].offset) && (offset < (bank->sectors[i].offset + bank->sectors[i].size)) && (first_sector == 0xffffffff)) { ... }
if (((offset + count) > bank->sectors[i].offset) &&
((offset + count) <= (bank->sectors[i].offset + bank->sectors[i].size)) &&
(last_sector == 0xffffffff))
last_sector = i;
}for (unsigned int i = 0; i < bank->num_sectors; i++) { ... }
if (first_sector == 0xffffffff || last_sector == 0xffffffff) {
LOG_INFO("Range check failed %" PRIx32 " %" PRIx32 "", offset, count);
return ERROR_FLASH_DST_OUT_OF_BANK;
}if (first_sector == 0xffffffff || last_sector == 0xffffffff) { ... }
lpc288x_set_flash_clk(bank);
source_offset = 0;
dest_offset = 0;
for (sector = first_sector; sector <= last_sector; sector++) {
for (page = 0; page < bank->sectors[sector].size / FLASH_PAGE_SIZE; page++) {
if (bytes_remaining == 0) {
count = 0;
memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
}if (bytes_remaining == 0) { ... } else if (bytes_remaining < FLASH_PAGE_SIZE) {
count = bytes_remaining;
memset(page_buffer, 0xFF, FLASH_PAGE_SIZE);
memcpy(page_buffer, &buffer[source_offset], count);
}else if (bytes_remaining < FLASH_PAGE_SIZE) { ... } else {
count = FLASH_PAGE_SIZE;
memcpy(page_buffer, &buffer[source_offset], count);
}else { ... }
if (lpc288x_wait_status_busy(bank, 1000) != ERROR_OK)
return ERROR_FLASH_OPERATION_FAILED;
target_write_u32(target, F_CTRL, FC_CS | FC_SET_DATA | FC_WEN | FC_FUNC);
target_write_u32(target, F_CTRL, FC_CS | FC_WEN | FC_FUNC);
if (target_write_buffer(target, offset + dest_offset, FLASH_PAGE_SIZE,
page_buffer) != ERROR_OK) {
LOG_INFO("Write to flash buffer failed");
return ERROR_FLASH_OPERATION_FAILED;
}if (target_write_buffer(target, offset + dest_offset, FLASH_PAGE_SIZE, page_buffer) != ERROR_OK) { ... }
dest_offset += FLASH_PAGE_SIZE;
source_offset += count;
bytes_remaining -= count;
lpc288x_load_timer(LOAD_TIMER_WRITE, target);
target_write_u32(target, F_CTRL, FC_PROG_REQ | FC_PROTECT | FC_FUNC |
FC_CS);
}for (page = 0; page < bank->sectors[sector].size / FLASH_PAGE_SIZE; page++) { ... }
}for (sector = first_sector; sector <= last_sector; sector++) { ... }
return ERROR_OK;
}{ ... }
static int lpc288x_probe(struct flash_bank *bank)
{
struct lpc288x_flash_bank *lpc288x_info = bank->driver_priv;
int retval;
if (lpc288x_info->cidr != 0)
return ERROR_OK;
if (bank->target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (bank->target->state != TARGET_HALTED) { ... }
retval = lpc288x_read_part_info(bank);
if (retval != ERROR_OK)
return retval;
return ERROR_OK;
}{ ... }
static int lpc288x_protect(struct flash_bank *bank, int set, unsigned int first,
unsigned int last)
{
int status;
uint32_t value;
struct target *target = bank->target;
status = lpc288x_system_ready(bank);
if (status != ERROR_OK)
return status;
if ((last < first) || (last >= bank->num_sectors))
return ERROR_FLASH_SECTOR_INVALID;
lpc288x_set_flash_clk(bank);
for (unsigned int lockregion = first; lockregion <= last; lockregion++) {
if (set) {
value = 0x01;
}if (set) { ... } else {
value = 0x00;
}else { ... }
target_write_u32(target, bank->sectors[lockregion].offset, value);
target_write_u32(target, F_CTRL, FC_LOAD_REQ | FC_PROTECT | FC_WEN | FC_FUNC |
FC_CS);
}for (unsigned int lockregion = first; lockregion <= last; lockregion++) { ... }
return ERROR_OK;
}{ ... }
const struct flash_driver lpc288x_flash = {
.name = "lpc288x",
.flash_bank_command = lpc288x_flash_bank_command,
.erase = lpc288x_erase,
.protect = lpc288x_protect,
.write = lpc288x_write,
.read = default_flash_read,
.probe = lpc288x_probe,
.auto_probe = lpc288x_probe,
.erase_check = default_flash_blank_check,
.protect_check = lpc288x_protect_check,
.free_driver_priv = default_flash_free_driver_priv,
...};