1
2
3
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
71
72
73
74
75
76
77
78
79
80
81
82
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
215
216
217
222
223
224
225
226
227
228
229
230
231
232
233
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
274
275
276
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
337
338
339
340
345
346
347
348
349
350
351
352
353
354
355
356
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
416
417
418
419
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
465
466
467
468
469
470
471
472
473
474
475
476
477
490
491
492
493
494
498
499
500
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/* ... */
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#include <helper/time_support.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
5 includes
static int aducm360_build_sector_list(struct flash_bank *bank);
static int aducm360_check_flash_completion(struct target *target, unsigned int timeout_ms);
static int aducm360_set_write_enable(struct target *target, int enable);
#define ADUCM360_FLASH_BASE 0x40002800
#define ADUCM360_FLASH_FEESTA 0x0000
#define ADUCM360_FLASH_FEECON0 0x0004
#define ADUCM360_FLASH_FEECMD 0x0008
#define ADUCM360_FLASH_FEEADR0L 0x0010
#define ADUCM360_FLASH_FEEADR0H 0x0014
#define ADUCM360_FLASH_FEEADR1L 0x0018
#define ADUCM360_FLASH_FEEADR1H 0x001C
#define ADUCM360_FLASH_FEEKEY 0x0020
#define ADUCM360_FLASH_FEEPROL 0x0028
#define ADUCM360_FLASH_FEEPROH 0x002C
#define ADUCM360_FLASH_FEESIGL 0x0030
#define ADUCM360_FLASH_FEESIGH 0x0034
#define ADUCM360_FLASH_FEECON1 0x0038
#define ADUCM360_FLASH_FEEADRAL 0x0048
#define ADUCM360_FLASH_FEEADRAH 0x004C
#define ADUCM360_FLASH_FEEAEN0 0x0078
#define ADUCM360_FLASH_FEEAEN1 0x007C
#define ADUCM360_FLASH_FEEAEN2 0x0080
19 defines
FLASH_BANK_COMMAND_HANDLER(aducm360_flash_bank_command)
{
bank->base = 0x00000000;
bank->size = 0x00020000;
aducm360_build_sector_list(bank);
return ERROR_OK;
}{ ... }
#define FLASH_SECTOR_SIZE 512
static int aducm360_build_sector_list(struct flash_bank *bank)
{
uint32_t offset = 0;
bank->num_sectors = bank->size / FLASH_SECTOR_SIZE;
bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
for (unsigned i = 0; i < bank->num_sectors; ++i) {
bank->sectors[i].offset = offset;
bank->sectors[i].size = FLASH_SECTOR_SIZE;
offset += bank->sectors[i].size;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 0;
}for (unsigned i = 0; i < bank->num_sectors; ++i) { ... }
return ERROR_OK;
}{ ... }
static int aducm360_mass_erase(struct target *target)
{
uint32_t value;
int res = ERROR_OK;
target_read_u32(target, ADUCM360_FLASH_BASE + ADUCM360_FLASH_FEESTA, &value);
aducm360_set_write_enable(target, 1);
target_write_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEEKEY, 0x0000F456);
target_write_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEEKEY, 0x0000F123);
target_write_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEECMD, 0x00000003);
res = aducm360_check_flash_completion(target, 3500);
if (res != ERROR_OK) {
LOG_ERROR("mass erase failed.");
aducm360_set_write_enable(target, 0);
res = ERROR_FLASH_OPERATION_FAILED;
}if (res != ERROR_OK) { ... }
return res;
}{ ... }
static int aducm360_page_erase(struct target *target, uint32_t padd)
{
uint32_t value;
int res = ERROR_OK;
target_read_u32(target, ADUCM360_FLASH_BASE + ADUCM360_FLASH_FEESTA, &value);
aducm360_set_write_enable(target, 1);
target_write_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEEKEY, 0x0000F456);
target_write_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEEKEY, 0x0000F123);
target_write_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEEADR0L, padd & 0xFFFF);
target_write_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEEADR0H, (padd>>16) & 0xFFFF);
target_write_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEECMD, 0x00000001);
res = aducm360_check_flash_completion(target, 50);
if (res != ERROR_OK) {
LOG_ERROR("page erase failed at 0x%08" PRIx32, padd);
aducm360_set_write_enable(target, 0);
res = ERROR_FLASH_OPERATION_FAILED;
}if (res != ERROR_OK) { ... }
return res;
}{ ... }
static int aducm360_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
int res = ERROR_OK;
int i;
int count;
struct target *target = bank->target;
uint32_t padd;
if (((first | last) == 0) || ((first == 0) && (last >= bank->num_sectors))) {
res = aducm360_mass_erase(target);
}if (((first | last) == 0) || ((first == 0) && (last >= bank->num_sectors))) { ... } else {
count = last - first + 1;
for (i = 0; i < count; ++i) {
padd = bank->base + ((first+i)*FLASH_SECTOR_SIZE);
res = aducm360_page_erase(target, padd);
if (res != ERROR_OK)
break;
}for (i = 0; i < count; ++i) { ... }
}else { ... }
return res;
}{ ... }
static int aducm360_write_block_sync(
struct flash_bank *bank,
const uint8_t *buffer,
uint32_t offset,
uint32_t count)
{
struct target *target = bank->target;
uint32_t target_buffer_size = 8192;
struct working_area *helper;
struct working_area *target_buffer;
uint32_t address = bank->base + offset;
struct reg_param reg_params[8];
int retval = ERROR_OK;
uint32_t entry_point = 0, exit_point = 0;
uint32_t res;
struct armv7m_algorithm armv7m_algo;
static const uint32_t aducm360_flash_write_code[] = {
0x88AF4D10, 0x0704F047, 0x682F80AF, 0x600E6806,
0xF017882F, 0xF43F0F08, 0xF851AFFB, 0x42B77B04,
0x800DF040, 0x0004F100, 0xF47F3A04, 0x686FAFEF,
0x0704F027, 0xF04F80AF, 0xF0000400, 0xF04FB802,
0xBE000480, 0x40002800, 0x00015000, 0x20000000,
0x00013000
...};
LOG_DEBUG("'aducm360_write_block_sync' requested, dst:0x%08" PRIx32 ", count:0x%08" PRIx32 "bytes.",
address, count);
if (((count%4) != 0) || ((offset%4) != 0)) {
LOG_ERROR("write block must be multiple of four bytes in offset & length");
return ERROR_FAIL;
}if (((count%4) != 0) || ((offset%4) != 0)) { ... }
if (target_alloc_working_area(target, sizeof(aducm360_flash_write_code),
&helper) != ERROR_OK) {
LOG_WARNING("no working area available, can't do block memory writes");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (target_alloc_working_area(target, sizeof(aducm360_flash_write_code), &helper) != ERROR_OK) { ... }
uint8_t code[sizeof(aducm360_flash_write_code)];
target_buffer_set_u32_array(target, code, ARRAY_SIZE(aducm360_flash_write_code),
aducm360_flash_write_code);
retval = target_write_buffer(target, helper->address, sizeof(code), code);
if (retval != ERROR_OK)
return retval;
entry_point = helper->address;
while (target_alloc_working_area_try(target, target_buffer_size, &target_buffer) != ERROR_OK) {
LOG_WARNING("couldn't allocate a buffer space of 0x%08" PRIx32 "bytes in the target's SRAM.",
target_buffer_size);
target_buffer_size /= 2;
if (target_buffer_size <= 256) {
LOG_WARNING("no large enough working area available, can't do block memory writes");
target_free_working_area(target, helper);
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (target_buffer_size <= 256) { ... }
}while (target_alloc_working_area_try(target, target_buffer_size, &target_buffer) != ERROR_OK) { ... }
armv7m_algo.common_magic = ARMV7M_COMMON_MAGIC;
armv7m_algo.core_mode = ARM_MODE_THREAD;
init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
init_reg_param(®_params[4], "r4", 32, PARAM_IN);
while (count > 0) {
uint32_t thisrun_count = (count > target_buffer_size) ? target_buffer_size : count;
retval = target_write_buffer(target, target_buffer->address, thisrun_count, buffer);
if (retval != ERROR_OK)
break;
buf_set_u32(reg_params[0].value, 0, 32, target_buffer->address);
buf_set_u32(reg_params[1].value, 0, 32, address);
buf_set_u32(reg_params[2].value, 0, 32, thisrun_count);
buf_set_u32(reg_params[3].value, 0, 32, 0);
retval = target_run_algorithm(target, 0, NULL, 5,
reg_params, entry_point, exit_point, 10000, &armv7m_algo);
if (retval != ERROR_OK) {
LOG_ERROR("error executing aducm360 flash write algorithm");
break;
}if (retval != ERROR_OK) { ... }
res = buf_get_u32(reg_params[4].value, 0, 32);
if (res) {
LOG_ERROR("aducm360 fast sync algorithm reports an error (%02" PRIX32 ")", res);
retval = ERROR_FAIL;
break;
}if (res) { ... }
buffer += thisrun_count;
address += thisrun_count;
count -= thisrun_count;
}while (count > 0) { ... }
target_free_working_area(target, target_buffer);
target_free_working_area(target, helper);
destroy_reg_param(®_params[0]);
destroy_reg_param(®_params[1]);
destroy_reg_param(®_params[2]);
destroy_reg_param(®_params[3]);
destroy_reg_param(®_params[4]);
return retval;
}{ ... }
static int aducm360_write_block_async(
struct flash_bank *bank,
const uint8_t *buffer,
uint32_t offset,
uint32_t count)
{
struct target *target = bank->target;
uint32_t target_buffer_size = 1024;
struct working_area *helper;
struct working_area *target_buffer;
uint32_t address = bank->base + offset;
struct reg_param reg_params[9];
int retval = ERROR_OK;
uint32_t entry_point = 0, exit_point = 0;
uint32_t res;
uint32_t wcount;
struct armv7m_algorithm armv7m_algo;
static const uint32_t aducm360_flash_write_code[] = {
0x4050F8DF, 0xF04588A5, 0x80A50504, 0x8000F8D0,
0x0F00F1B8, 0x8016F000, 0x45476847, 0xAFF6F43F,
0x6B04F857, 0x6B04F842, 0xF0158825, 0xF43F0F08,
0x428FAFFB, 0xF100BF28, 0x60470708, 0xB10B3B01,
0xBFE4F7FF, 0xF02588A5, 0x80A50504, 0x0900F04F,
0xBE00BF00, 0x40002800, 0x20000000, 0x20000100,
0x00013000
...};
LOG_DEBUG("'aducm360_write_block_async' requested, dst:0x%08" PRIx32 ", count:0x%08" PRIx32 "bytes.",
address, count);
if (((count%4) != 0) || ((offset%4) != 0)) {
LOG_ERROR("write block must be multiple of four bytes in offset & length");
return ERROR_FAIL;
}if (((count%4) != 0) || ((offset%4) != 0)) { ... }
wcount = count/4;
if (target_alloc_working_area(target, sizeof(aducm360_flash_write_code),
&helper) != ERROR_OK) {
LOG_WARNING("no working area available, can't do block memory writes");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (target_alloc_working_area(target, sizeof(aducm360_flash_write_code), &helper) != ERROR_OK) { ... }
uint8_t code[sizeof(aducm360_flash_write_code)];
target_buffer_set_u32_array(target, code, ARRAY_SIZE(aducm360_flash_write_code),
aducm360_flash_write_code);
retval = target_write_buffer(target, helper->address, sizeof(code), code);
if (retval != ERROR_OK)
return retval;
entry_point = helper->address;
while (target_alloc_working_area_try(target, target_buffer_size, &target_buffer) != ERROR_OK) {
LOG_WARNING("couldn't allocate a buffer space of 0x%08" PRIx32 "bytes in the target's SRAM.",
target_buffer_size);
target_buffer_size /= 2;
if (target_buffer_size <= 256) {
LOG_WARNING("no large enough working area available, can't do block memory writes");
target_free_working_area(target, helper);
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (target_buffer_size <= 256) { ... }
}while (target_alloc_working_area_try(target, target_buffer_size, &target_buffer) != ERROR_OK) { ... }
armv7m_algo.common_magic = ARMV7M_COMMON_MAGIC;
armv7m_algo.core_mode = ARM_MODE_THREAD;
init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
init_reg_param(®_params[4], "r9", 32, PARAM_IN);
buf_set_u32(reg_params[0].value, 0, 32, target_buffer->address);
buf_set_u32(reg_params[1].value, 0, 32, target_buffer->address + target_buffer->size);
buf_set_u32(reg_params[2].value, 0, 32, address);
buf_set_u32(reg_params[3].value, 0, 32, wcount);
retval = target_run_flash_async_algorithm(target, buffer, wcount, 4,
0, NULL,
5, reg_params,
target_buffer->address, target_buffer->size,
entry_point, exit_point,
&armv7m_algo);
if (retval != ERROR_OK) {
LOG_ERROR("error executing aducm360 flash write algorithm");
}if (retval != ERROR_OK) { ... } else {
res = buf_get_u32(reg_params[4].value, 0, 32);
if (res) {
LOG_ERROR("aducm360 fast async algorithm reports an error (%02" PRIX32 ")", res);
retval = ERROR_FAIL;
}if (res) { ... }
}else { ... }
target_free_working_area(target, target_buffer);
target_free_working_area(target, helper);
destroy_reg_param(®_params[0]);
destroy_reg_param(®_params[1]);
destroy_reg_param(®_params[2]);
destroy_reg_param(®_params[3]);
destroy_reg_param(®_params[4]);
return retval;
}{ ... }
/* ... */
static int aducm360_write_block(struct flash_bank *bank,
const uint8_t *buffer,
uint32_t offset,
uint32_t count)
{
int choice = 0;
switch (choice) {
case 0:
return aducm360_write_block_sync(bank, buffer, offset, count);case 0:
case 1:
return aducm360_write_block_async(bank, buffer, offset, count);case 1:
default:
LOG_ERROR("aducm360_write_block was cancelled (no writing method was chosen)!");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;default
}switch (choice) { ... }
}{ ... }
#define FEESTA_WRDONE 0x00000008
static int aducm360_write_modified(struct flash_bank *bank,
const uint8_t *buffer,
uint32_t offset,
uint32_t count)
{
uint32_t value;
int res = ERROR_OK;
uint32_t i, j, a, d;
struct target *target = bank->target;
LOG_DEBUG("performing slow write (offset=0x%08" PRIx32 ", count=0x%08" PRIx32 ")...",
offset, count);
aducm360_set_write_enable(target, 1);
target_read_u32(target, ADUCM360_FLASH_BASE + ADUCM360_FLASH_FEESTA, &value);
for (i = 0; i < count; i += 4) {
a = offset+i;
for (j = 0; i < 4; i += 1)
*((uint8_t *)(&d) + j) = buffer[i+j];
target_write_u32(target, a, d);
do {
target_read_u32(target, ADUCM360_FLASH_BASE + ADUCM360_FLASH_FEESTA, &value);
...} while (!(value & FEESTA_WRDONE));
}for (i = 0; i < count; i += 4) { ... }
aducm360_set_write_enable(target, 0);
return res;
}{ ... }
static int aducm360_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
{
int retval;
retval = aducm360_write_block(bank, buffer, offset, count);
if (retval != ERROR_OK) {
if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
/* ... */
LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
retval = aducm360_write_modified(bank, buffer, offset, count);
if (retval != ERROR_OK) {
LOG_ERROR("slow write failed");
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) { ... }
}if (retval != ERROR_OK) { ... }
return retval;
}{ ... }
static int aducm360_probe(struct flash_bank *bank)
{
return ERROR_OK;
}{ ... }
/* ... */
static int aducm360_set_write_enable(struct target *target, int enable)
{
uint32_t value;
target_read_u32(target, ADUCM360_FLASH_BASE + ADUCM360_FLASH_FEECON0, &value);
if (enable)
value |= 0x00000004;
else
value &= ~0x00000004;
target_write_u32(target, ADUCM360_FLASH_BASE + ADUCM360_FLASH_FEECON0, value);
return ERROR_OK;
}{ ... }
/* ... */
static int aducm360_check_flash_completion(struct target *target, unsigned int timeout_ms)
{
uint32_t v = 1;
int64_t endtime = timeval_ms() + timeout_ms;
while (1) {
target_read_u32(target, ADUCM360_FLASH_BASE+ADUCM360_FLASH_FEESTA, &v);
if ((v & 0x00000001) == 0)
break;
alive_sleep(1);
if (timeval_ms() >= endtime)
break;
}while (1) { ... }
if (!(v & 0x00000004))
return ERROR_FAIL;
return ERROR_OK;
}{ ... }
const struct flash_driver aducm360_flash = {
.name = "aducm360",
.flash_bank_command = aducm360_flash_bank_command,
.erase = aducm360_erase,
.write = aducm360_write,
.read = default_flash_read,
.probe = aducm360_probe,
.auto_probe = aducm360_probe,
.erase_check = default_flash_blank_check,
...};