1
2
3
9
10
11
12
13
14
20
21
24
25
26
27
28
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
54
55
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
100
101
102
103
104
110
111
112
113
114
115
116
117
118
119
125
126
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
175
176
177
178
179
180
181
182
183
184
185
189
190
191
192
193
194
195
196
197
198
199
200
201
202
206
207
208
209
210
211
212
217
218
219
220
221
222
223
224
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
263
264
265
266
267
268
269
270
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
307
308
309
313
314
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
397
398
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#include <helper/time_support.h>
#include <target/armv7m.h>
#include "../../../contrib/loaders/flash/npcx/npcx_flash.h"
5 includes
static const uint8_t npcx_algo[] = {
#include "../../../contrib/loaders/flash/npcx/npcx_algo.inc"
...};
#define NPCX_FLASH_TIMEOUT_MS 8000
enum npcx_flash_device_index {
NPCX_FLASH_256KB = 0,
NPCX_FLASH_512KB = 1,
NPCX_FLASH_1MB = 2,
NPCX_FLASH_UNKNOWN,
...};
struct npcx_flash_bank {
uint32_t sector_length;
bool probed;
enum npcx_flash_device_index flash;
struct working_area *working_area;
struct armv7m_algorithm armv7m_info;
const uint8_t *algo_code;
uint32_t algo_size;
uint32_t algo_working_size;
uint32_t buffer_addr;
uint32_t params_addr;
uint32_t fiu_ver;
...};
struct npcx_flash_info {
char *name;
uint32_t id;
uint32_t size;
...};
static const struct npcx_flash_info flash_info[] = {
[NPCX_FLASH_256KB] = {
.name = "256KB Flash",
.id = 0xEF4012,
.size = 256 * 1024,
...},
[NPCX_FLASH_512KB] = {
.name = "512KB Flash",
.id = 0xEF4013,
.size = 512 * 1024,
...},
[NPCX_FLASH_1MB] = {
.name = "1MB Flash",
.id = 0xEF4014,
.size = 1024 * 1024,
...},
[NPCX_FLASH_UNKNOWN] = {
.name = "Unknown Flash",
.size = 0xFFFFFFFF,
...},
...};
static int npcx_init(struct flash_bank *bank)
{
struct target *target = bank->target;
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
target_free_working_area(target, npcx_bank->working_area);
npcx_bank->working_area = NULL;
int retval = target_alloc_working_area(target, npcx_bank->algo_working_size,
&npcx_bank->working_area);
if (retval != ERROR_OK)
return retval;
if (npcx_bank->working_area->address != NPCX_FLASH_LOADER_WORKING_ADDR) {
LOG_TARGET_ERROR(target, "Invalid working address");
LOG_INFO("Hint: Use '-work-area-phys 0x%" PRIx32 "' in your target configuration",
NPCX_FLASH_LOADER_WORKING_ADDR);
target_free_working_area(target, npcx_bank->working_area);
npcx_bank->working_area = NULL;
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (npcx_bank->working_area->address != NPCX_FLASH_LOADER_WORKING_ADDR) { ... }
retval = target_write_buffer(target, NPCX_FLASH_LOADER_PROGRAM_ADDR,
npcx_bank->algo_size, npcx_bank->algo_code);
if (retval != ERROR_OK) {
LOG_TARGET_ERROR(target, "Failed to load flash helper algorithm");
target_free_working_area(target, npcx_bank->working_area);
npcx_bank->working_area = NULL;
return retval;
}if (retval != ERROR_OK) { ... }
npcx_bank->armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
npcx_bank->armv7m_info.core_mode = ARM_MODE_THREAD;
retval = target_start_algorithm(target, 0, NULL, 0, NULL,
NPCX_FLASH_LOADER_PROGRAM_ADDR, 0,
&npcx_bank->armv7m_info);
if (retval != ERROR_OK) {
LOG_TARGET_ERROR(target, "Failed to start flash helper algorithm");
target_free_working_area(target, npcx_bank->working_area);
npcx_bank->working_area = NULL;
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
return retval;
}{ ... }
static int npcx_quit(struct flash_bank *bank)
{
struct target *target = bank->target;
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
(void)target_halt(target);
int retval = target_wait_algorithm(target, 0, NULL, 0, NULL, 0,
NPCX_FLASH_TIMEOUT_MS, &npcx_bank->armv7m_info);
target_free_working_area(target, npcx_bank->working_area);
npcx_bank->working_area = NULL;
return retval;
}{ ... }
static int npcx_wait_algo_done(struct flash_bank *bank, uint32_t params_addr)
{
struct target *target = bank->target;
uint32_t status_addr = params_addr + offsetof(struct npcx_flash_params, sync);
uint32_t status;
int64_t start_ms = timeval_ms();
do {
int retval = target_read_u32(target, status_addr, &status);
if (retval != ERROR_OK)
return retval;
keep_alive();
int64_t elapsed_ms = timeval_ms() - start_ms;
if (elapsed_ms > NPCX_FLASH_TIMEOUT_MS)
break;
...} while (status == NPCX_FLASH_LOADER_EXECUTE);
if (status != NPCX_FLASH_LOADER_WAIT) {
LOG_TARGET_ERROR(target, "Flash operation failed, status (%0x" PRIX32 ") ", status);
return ERROR_FAIL;
}if (status != NPCX_FLASH_LOADER_WAIT) { ... }
return ERROR_OK;
}{ ... }
static enum npcx_flash_device_index npcx_get_flash_id(struct flash_bank *bank, uint32_t *flash_id)
{
struct target *target = bank->target;
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
struct npcx_flash_params algo_params;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
int retval = npcx_init(bank);
if (retval != ERROR_OK)
return retval;
target_buffer_set_u32(target, (uint8_t *)&algo_params.fiu_ver, npcx_bank->fiu_ver);
target_buffer_set_u32(target, (uint8_t *)&algo_params.cmd, NPCX_FLASH_CMD_GET_FLASH_ID);
target_buffer_set_u32(target, (uint8_t *)&algo_params.sync, NPCX_FLASH_LOADER_WAIT);
retval = target_write_buffer(target, npcx_bank->params_addr,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval != ERROR_OK) {
(void)npcx_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
target_buffer_set_u32(target, (uint8_t *)&algo_params.sync, NPCX_FLASH_LOADER_EXECUTE);
retval = target_write_buffer(target, npcx_bank->params_addr,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval == ERROR_OK) {
retval = npcx_wait_algo_done(bank, npcx_bank->params_addr);
if (retval == ERROR_OK)
target_read_u32(target, NPCX_FLASH_LOADER_BUFFER_ADDR, flash_id);
}if (retval == ERROR_OK) { ... }
(void)npcx_quit(bank);
return retval;
}{ ... }
static int npcx_get_flash(uint32_t flash_id)
{
for (uint32_t i = 0; i < ARRAY_SIZE(flash_info) - 1; i++) {
if (flash_info[i].id == flash_id)
return i;
}for (uint32_t i = 0; i < ARRAY_SIZE(flash_info) - 1; i++) { ... }
return NPCX_FLASH_UNKNOWN;
}{ ... }
static int npcx_probe(struct flash_bank *bank)
{
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
uint32_t sector_length = NPCX_FLASH_ERASE_SIZE;
uint32_t flash_id;
npcx_bank->algo_code = npcx_algo;
npcx_bank->algo_size = sizeof(npcx_algo);
npcx_bank->algo_working_size = NPCX_FLASH_LOADER_PARAMS_SIZE +
NPCX_FLASH_LOADER_BUFFER_SIZE +
NPCX_FLASH_LOADER_PROGRAM_SIZE;
npcx_bank->buffer_addr = NPCX_FLASH_LOADER_BUFFER_ADDR;
npcx_bank->params_addr = NPCX_FLASH_LOADER_PARAMS_ADDR;
int retval = npcx_get_flash_id(bank, &flash_id);
if (retval != ERROR_OK)
return retval;
npcx_bank->flash = npcx_get_flash(flash_id);
unsigned int num_sectors = flash_info[npcx_bank->flash].size / sector_length;
bank->sectors = calloc(num_sectors, sizeof(struct flash_sector));
if (!bank->sectors) {
LOG_ERROR("Out of memory");
return ERROR_FAIL;
}if (!bank->sectors) { ... }
bank->num_sectors = num_sectors;
bank->size = num_sectors * sector_length;
bank->write_start_alignment = 0;
bank->write_end_alignment = 0;
npcx_bank->sector_length = sector_length;
for (unsigned int i = 0; i < num_sectors; i++) {
bank->sectors[i].offset = i * sector_length;
bank->sectors[i].size = sector_length;
bank->sectors[i].is_erased = -1;
bank->sectors[i].is_protected = 0;
}for (unsigned int i = 0; i < num_sectors; i++) { ... }
npcx_bank->probed = true;
return ERROR_OK;
}{ ... }
static int npcx_auto_probe(struct flash_bank *bank)
{
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
int retval = ERROR_OK;
if (!npcx_bank->probed)
retval = npcx_probe(bank);
return retval;
}{ ... }
FLASH_BANK_COMMAND_HANDLER(npcx_flash_bank_command)
{
struct npcx_flash_bank *npcx_bank;
if (CMD_ARGC < 6 || CMD_ARGC > 7)
return ERROR_COMMAND_SYNTAX_ERROR;
npcx_bank = calloc(1, sizeof(struct npcx_flash_bank));
if (!npcx_bank) {
LOG_ERROR("Out of memory");
return ERROR_FAIL;
}if (!npcx_bank) { ... }
const char *fiu;
if (CMD_ARGC == 6) {
LOG_WARNING("No FIU is selection, using default.");
npcx_bank->fiu_ver = NPCX_FIU_NPCX;
}if (CMD_ARGC == 6) { ... }
if (CMD_ARGC == 7) {
fiu = CMD_ARGV[6];
if (strcmp(fiu, "npcx.fiu") == 0) {
npcx_bank->fiu_ver = NPCX_FIU_NPCX;
}if (strcmp(fiu, "npcx.fiu") == 0) { ... } else if (strcmp(fiu, "npcx_v2.fiu") == 0) {
npcx_bank->fiu_ver = NPCX_FIU_NPCX_V2;
}else if (strcmp(fiu, "npcx_v2.fiu") == 0) { ... } else if (strcmp(fiu, "npck.fiu") == 0) {
npcx_bank->fiu_ver = NPCX_FIU_NPCK;
}else if (strcmp(fiu, "npck.fiu") == 0) { ... } else {
LOG_ERROR("%s is not a valid fiu", fiu);
free(npcx_bank);
return ERROR_TARGET_INVALID;
}else { ... }
}if (CMD_ARGC == 7) { ... }
npcx_bank->sector_length = NPCX_FLASH_ERASE_SIZE;
bank->driver_priv = npcx_bank;
return ERROR_OK;
}{ ... }
static int npcx_chip_erase(struct flash_bank *bank)
{
struct target *target = bank->target;
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
struct npcx_flash_params algo_params;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
int retval = npcx_auto_probe(bank);
if (retval != ERROR_OK)
return retval;
retval = npcx_init(bank);
if (retval != ERROR_OK)
return retval;
target_buffer_set_u32(target, (uint8_t *)&algo_params.fiu_ver, npcx_bank->fiu_ver);
target_buffer_set_u32(target, (uint8_t *)&algo_params.cmd, NPCX_FLASH_CMD_ERASE_ALL);
target_buffer_set_u32(target, (uint8_t *)&algo_params.sync, NPCX_FLASH_LOADER_WAIT);
retval = target_write_buffer(target, npcx_bank->params_addr,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval != ERROR_OK) {
(void)npcx_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
target_buffer_set_u32(target, (uint8_t *)&algo_params.sync, NPCX_FLASH_LOADER_EXECUTE);
retval = target_write_buffer(target, npcx_bank->params_addr,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval == ERROR_OK)
retval = npcx_wait_algo_done(bank, npcx_bank->params_addr);
(void)npcx_quit(bank);
return retval;
}{ ... }
static int npcx_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
struct npcx_flash_params algo_params;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if ((first == 0) && (last == (bank->num_sectors - 1))) {
return npcx_chip_erase(bank);
}if ((first == 0) && (last == (bank->num_sectors - 1))) { ... }
uint32_t address = first * npcx_bank->sector_length;
uint32_t length = (last - first + 1) * npcx_bank->sector_length;
int retval = npcx_auto_probe(bank);
if (retval != ERROR_OK)
return retval;
retval = npcx_init(bank);
if (retval != ERROR_OK)
return retval;
target_buffer_set_u32(target, (uint8_t *)&algo_params.fiu_ver, npcx_bank->fiu_ver);
target_buffer_set_u32(target, (uint8_t *)&algo_params.addr, address);
target_buffer_set_u32(target, (uint8_t *)&algo_params.len, length);
target_buffer_set_u32(target, (uint8_t *)&algo_params.cmd, NPCX_FLASH_CMD_ERASE_SECTORS);
target_buffer_set_u32(target, (uint8_t *)&algo_params.sync, NPCX_FLASH_LOADER_WAIT);
retval = target_write_buffer(target, npcx_bank->params_addr,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval != ERROR_OK) {
(void)npcx_quit(bank);
return retval;
}if (retval != ERROR_OK) { ... }
target_buffer_set_u32(target, (uint8_t *)&algo_params.sync, NPCX_FLASH_LOADER_EXECUTE);
retval = target_write_buffer(target, npcx_bank->params_addr,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval == ERROR_OK)
retval = npcx_wait_algo_done(bank, npcx_bank->params_addr);
(void)npcx_quit(bank);
return retval;
}{ ... }
static int npcx_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct target *target = bank->target;
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
struct npcx_flash_params algo_params;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
int retval = npcx_auto_probe(bank);
if (retval != ERROR_OK)
return retval;
retval = npcx_init(bank);
if (retval != ERROR_OK)
return retval;
target_buffer_set_u32(target, (uint8_t *)&algo_params.cmd, NPCX_FLASH_CMD_PROGRAM);
uint32_t address = offset;
while (count > 0) {
uint32_t size = (count > NPCX_FLASH_LOADER_BUFFER_SIZE) ?
NPCX_FLASH_LOADER_BUFFER_SIZE : count;
retval = target_write_buffer(target, npcx_bank->buffer_addr,
size, buffer);
if (retval != ERROR_OK) {
LOG_ERROR("Unable to write data to target memory");
break;
}if (retval != ERROR_OK) { ... }
target_buffer_set_u32(target, (uint8_t *)&algo_params.fiu_ver, npcx_bank->fiu_ver);
target_buffer_set_u32(target, (uint8_t *)&algo_params.addr, address);
target_buffer_set_u32(target, (uint8_t *)&algo_params.len, size);
target_buffer_set_u32(target, (uint8_t *)&algo_params.sync, NPCX_FLASH_LOADER_WAIT);
retval = target_write_buffer(target, npcx_bank->params_addr,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval != ERROR_OK)
break;
target_buffer_set_u32(target, (uint8_t *)&algo_params.sync, NPCX_FLASH_LOADER_EXECUTE);
retval = target_write_buffer(target, npcx_bank->params_addr,
sizeof(algo_params), (uint8_t *)&algo_params);
if (retval != ERROR_OK)
break;
retval = npcx_wait_algo_done(bank, npcx_bank->params_addr);
if (retval != ERROR_OK)
break;
count -= size;
buffer += size;
address += size;
}while (count > 0) { ... }
(void)npcx_quit(bank);
return retval;
}{ ... }
static int npcx_info(struct flash_bank *bank, struct command_invocation *cmd)
{
struct npcx_flash_bank *npcx_bank = bank->driver_priv;
command_print_sameline(cmd, "%s flash: %s\n",
target_name(bank->target),
flash_info[npcx_bank->flash].name);
return ERROR_OK;
}{ ... }
const struct flash_driver npcx_flash = {
.name = "npcx",
.flash_bank_command = npcx_flash_bank_command,
.erase = npcx_erase,
.write = npcx_write,
.read = default_flash_read,
.probe = npcx_probe,
.auto_probe = npcx_auto_probe,
.erase_check = default_flash_blank_check,
.info = npcx_info,
.free_driver_priv = default_flash_free_driver_priv,
...};