1
2
3
6
7
16
17
23
24
25
26
27
28
29
30
31
32
33
34
43
44
52
53
61
62
64
66
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
98
99
100
101
102
103
104
105
111
112
118
119
125
126
127
128
129
130
131
132
133
134
135
136
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
162
163
164
165
166
167
170
171
172
173
174
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
212
213
214
215
216
217
218
219
220
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
253
254
255
256
257
258
269
270
271
272
273
274
275
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
304
305
306
307
308
309
310
311
312
313
314
318
319
323
324
328
329
335
336
337
338
339
345
346
347
348
349
350
351
352
358
359
360
361
362
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
389
390
391
392
393
394
395
396
400
401
405
406
407
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
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
532
533
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
568
569
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
596
597
603
604
605
606
607
608
609
616
617
622
623
624
625
626
627
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* ... */
/* ... */
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include "spi.h"
#include <jtag/jtag.h>
#include <helper/time_support.h>
#define SMI_READ_REG(a) \
({ \
int _ret; \
uint32_t _value; \
\
_ret = target_read_u32(target, io_base + (a), &_value); \
if (_ret != ERROR_OK) \
return _ret; \
_value; \
...})...
#define SMI_WRITE_REG(a, v) \
{ \
int _retval; \
\
_retval = target_write_u32(target, io_base + (a), (v)); \
if (_retval != ERROR_OK) \
return _retval; \
...}...
#define SMI_POLL_TFF(timeout) \
{ \
int _retval; \
\
_retval = poll_tff(target, io_base, timeout); \
if (_retval != ERROR_OK) \
return _retval; \
...}...
#define SMI_SET_SW_MODE() SMI_WRITE_REG(SMI_CR1, \
SMI_READ_REG(SMI_CR1) | SMI_SW_MODE)...
#define SMI_SET_HWWB_MODE() SMI_WRITE_REG(SMI_CR1, \
(SMI_READ_REG(SMI_CR1) | SMI_WB_MODE) & ~SMI_SW_MODE)...
#define SMI_SET_HW_MODE() SMI_WRITE_REG(SMI_CR1, \
SMI_READ_REG(SMI_CR1) & ~(SMI_SW_MODE | SMI_WB_MODE))...
#define SMI_CLEAR_TFF() SMI_WRITE_REG(SMI_SR, ~SMI_TFF)
#define SMI_BANK_SIZE (0x01000000)
#define SMI_CR1 (0x00)
#define SMI_CR2 (0x04)
#define SMI_SR (0x08)
#define SMI_TR (0x0c)
#define SMI_RR (0x10)
#define SMI_SW_MODE 0x10000000
#define SMI_WB_MODE 0x20000000
#define SMI_TX_LEN_1 0x00000001
#define SMI_TX_LEN_4 0x00000004
#define SMI_RX_LEN_3 0x00000030
#define SMI_SEND 0x00000080
#define SMI_RSR 0x00000400
#define SMI_WE 0x00000800
#define SMI_SEL_BANK0 0x00000000
#define SMI_SEL_BANK1 0x00001000
#define SMI_SEL_BANK2 0x00002000
#define SMI_SEL_BANK3 0x00003000
#define SMI_TFF 0x00000100
#define SMI_READ_ID 0x0000009F
#define SMI_CMD_TIMEOUT (100)
#define SMI_PROBE_TIMEOUT (100)
#define SMI_MAX_TIMEOUT (3000)
30 defines
struct stmsmi_flash_bank {
bool probed;
uint32_t io_base;
uint32_t bank_num;
const struct flash_device *dev;
...};
struct stmsmi_target {
char *name;
uint32_t tap_idcode;
uint32_t smi_base;
uint32_t io_base;
...};
static const struct stmsmi_target target_devices[] = {
{ "SPEAr3xx/6xx", 0x07926041, 0xf8000000, 0xfc000000 },
{ "STR75x", 0x4f1f0041, 0x80000000, 0x90000000 },
{ NULL, 0, 0, 0 }
...};
FLASH_BANK_COMMAND_HANDLER(stmsmi_flash_bank_command)
{
struct stmsmi_flash_bank *stmsmi_info;
LOG_DEBUG("%s", __func__);
if (CMD_ARGC < 6)
return ERROR_COMMAND_SYNTAX_ERROR;
stmsmi_info = malloc(sizeof(struct stmsmi_flash_bank));
if (!stmsmi_info) {
LOG_ERROR("not enough memory");
return ERROR_FAIL;
}if (!stmsmi_info) { ... }
bank->driver_priv = stmsmi_info;
stmsmi_info->probed = false;
return ERROR_OK;
}{ ... }
static int poll_tff(struct target *target, uint32_t io_base, int timeout)
{
int64_t endtime;
if (SMI_READ_REG(SMI_SR) & SMI_TFF)
return ERROR_OK;
endtime = timeval_ms() + timeout;
do {
alive_sleep(1);
if (SMI_READ_REG(SMI_SR) & SMI_TFF)
return ERROR_OK;
...} while (timeval_ms() < endtime);
LOG_ERROR("Timeout while polling TFF");
return ERROR_FLASH_OPERATION_FAILED;
}{ ... }
/* ... */
static int read_status_reg(struct flash_bank *bank, uint32_t *status)
{
struct target *target = bank->target;
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
uint32_t io_base = stmsmi_info->io_base;
SMI_CLEAR_TFF();
SMI_WRITE_REG(SMI_CR2, stmsmi_info->bank_num | SMI_RSR);
SMI_POLL_TFF(SMI_CMD_TIMEOUT);
SMI_CLEAR_TFF();
*status = SMI_READ_REG(SMI_SR) & 0x0000ffff;
SMI_WRITE_REG(SMI_CR2, 0);
return ERROR_OK;
}{ ... }
static int wait_till_ready(struct flash_bank *bank, int timeout)
{
uint32_t status;
int retval;
int64_t endtime;
endtime = timeval_ms() + timeout;
do {
retval = read_status_reg(bank, &status);
if (retval != ERROR_OK)
return retval;
if ((status & SPIFLASH_BSY_BIT) == 0)
return ERROR_OK;
alive_sleep(1);
...} while (timeval_ms() < endtime);
LOG_ERROR("timeout");
return ERROR_FAIL;
}{ ... }
/* ... */
static int smi_write_enable(struct flash_bank *bank)
{
struct target *target = bank->target;
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
uint32_t io_base = stmsmi_info->io_base;
uint32_t status;
int retval;
SMI_SET_HW_MODE();
SMI_CLEAR_TFF();
SMI_WRITE_REG(SMI_CR2, stmsmi_info->bank_num | SMI_WE);
SMI_POLL_TFF(SMI_CMD_TIMEOUT);
retval = read_status_reg(bank, &status);
if (retval != ERROR_OK)
return retval;
if ((status & SPIFLASH_WE_BIT) == 0) {
LOG_ERROR("Cannot enable write to flash. Status=0x%08" PRIx32, status);
return ERROR_FAIL;
}if ((status & SPIFLASH_WE_BIT) == 0) { ... }
return ERROR_OK;
}{ ... }
static uint32_t erase_command(struct stmsmi_flash_bank *stmsmi_info,
uint32_t offset)
{
uint8_t cmd_bytes[] = {
stmsmi_info->dev->erase_cmd,
offset >> 16,
offset >> 8,
offset
...};
return le_to_h_u32(cmd_bytes);
}{ ... }
static int smi_erase_sector(struct flash_bank *bank, int sector)
{
struct target *target = bank->target;
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
uint32_t io_base = stmsmi_info->io_base;
uint32_t cmd;
int retval;
retval = smi_write_enable(bank);
if (retval != ERROR_OK)
return retval;
SMI_SET_SW_MODE();
SMI_CLEAR_TFF();
cmd = erase_command(stmsmi_info, bank->sectors[sector].offset);
SMI_WRITE_REG(SMI_TR, cmd);
SMI_WRITE_REG(SMI_CR2, stmsmi_info->bank_num | SMI_SEND | SMI_TX_LEN_4);
SMI_POLL_TFF(SMI_CMD_TIMEOUT);
retval = wait_till_ready(bank, SMI_MAX_TIMEOUT);
if (retval != ERROR_OK)
return retval;
return ERROR_OK;
}{ ... }
static int stmsmi_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct target *target = bank->target;
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
uint32_t io_base = stmsmi_info->io_base;
int retval = ERROR_OK;
LOG_DEBUG("%s: from sector %u to sector %u", __func__, first, last);
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if ((last < first) || (last >= bank->num_sectors)) {
LOG_ERROR("Flash sector invalid");
return ERROR_FLASH_SECTOR_INVALID;
}if ((last < first) || (last >= bank->num_sectors)) { ... }
if (!(stmsmi_info->probed)) {
LOG_ERROR("Flash bank not probed");
return ERROR_FLASH_BANK_NOT_PROBED;
}if (!(stmsmi_info->probed)) { ... }
for (unsigned int sector = first; sector <= last; sector++) {
if (bank->sectors[sector].is_protected) {
LOG_ERROR("Flash sector %u protected", sector);
return ERROR_FAIL;
}if (bank->sectors[sector].is_protected) { ... }
}for (unsigned int sector = first; sector <= last; sector++) { ... }
if (stmsmi_info->dev->erase_cmd == 0x00)
return ERROR_FLASH_OPER_UNSUPPORTED;
for (unsigned int sector = first; sector <= last; sector++) {
retval = smi_erase_sector(bank, sector);
if (retval != ERROR_OK)
break;
keep_alive();
}for (unsigned int sector = first; sector <= last; sector++) { ... }
SMI_SET_HW_MODE();
return retval;
}{ ... }
static int stmsmi_protect(struct flash_bank *bank, int set,
unsigned int first, unsigned int last)
{
for (unsigned int sector = first; sector <= last; sector++)
bank->sectors[sector].is_protected = set;
return ERROR_OK;
}{ ... }
static int smi_write_buffer(struct flash_bank *bank, const uint8_t *buffer,
uint32_t address, uint32_t len)
{
struct target *target = bank->target;
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
uint32_t io_base = stmsmi_info->io_base;
int retval;
LOG_DEBUG("%s: address=0x%08" PRIx32 " len=0x%08" PRIx32,
__func__, address, len);
retval = smi_write_enable(bank);
if (retval != ERROR_OK)
return retval;
SMI_SET_HWWB_MODE();
retval = target_write_buffer(target, address, len, buffer);
if (retval != ERROR_OK)
return retval;
return ERROR_OK;
}{ ... }
static int stmsmi_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct target *target = bank->target;
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
uint32_t io_base = stmsmi_info->io_base;
uint32_t cur_count, page_size, page_offset;
int retval = ERROR_OK;
LOG_DEBUG("%s: offset=0x%08" PRIx32 " count=0x%08" PRIx32,
__func__, offset, count);
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if (offset + count > stmsmi_info->dev->size_in_bytes) {
LOG_WARNING("Write pasts end of flash. Extra data discarded.");
count = stmsmi_info->dev->size_in_bytes - offset;
}if (offset + count > stmsmi_info->dev->size_in_bytes) { ... }
for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
if ((offset <
(bank->sectors[sector].offset + bank->sectors[sector].size))
&& ((offset + count - 1) >= bank->sectors[sector].offset)
&& bank->sectors[sector].is_protected) {
LOG_ERROR("Flash sector %u protected", sector);
return ERROR_FAIL;
}if ((offset < (bank->sectors[sector].offset + bank->sectors[sector].size)) && ((offset + count - 1) >= bank->sectors[sector].offset) && bank->sectors[sector].is_protected) { ... }
}for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ... }
page_size = stmsmi_info->dev->pagesize ?
stmsmi_info->dev->pagesize : SPIFLASH_DEF_PAGESIZE;
if (count > 0 && (offset & 3) != 0) {
cur_count = 4 - (offset & 3);
if (cur_count > count)
cur_count = count;
retval = smi_write_buffer(bank, buffer, bank->base + offset,
cur_count);
if (retval != ERROR_OK)
goto err;
offset += cur_count;
buffer += cur_count;
count -= cur_count;
}if (count > 0 && (offset & 3) != 0) { ... }
page_offset = offset % page_size;
while (count >= 4) {
if (page_offset + count > page_size)
cur_count = page_size - page_offset;
else
cur_count = count & ~3;
retval = smi_write_buffer(bank, buffer, bank->base + offset,
cur_count);
if (retval != ERROR_OK)
goto err;
page_offset = 0;
buffer += cur_count;
offset += cur_count;
count -= cur_count;
keep_alive();
}while (count >= 4) { ... }
if (count > 0)
retval = smi_write_buffer(bank, buffer, bank->base + offset, count);
err:
SMI_SET_HW_MODE();
return retval;
}{ ... }
static int read_flash_id(struct flash_bank *bank, uint32_t *id)
{
struct target *target = bank->target;
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
uint32_t io_base = stmsmi_info->io_base;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
retval = wait_till_ready(bank, SMI_PROBE_TIMEOUT);
if (retval != ERROR_OK)
return retval;
SMI_SET_SW_MODE();
SMI_CLEAR_TFF();
SMI_WRITE_REG(SMI_TR, SMI_READ_ID);
SMI_WRITE_REG(SMI_CR2,
stmsmi_info->bank_num | SMI_SEND | SMI_RX_LEN_3 | SMI_TX_LEN_1);
SMI_POLL_TFF(SMI_CMD_TIMEOUT);
SMI_CLEAR_TFF();
*id = SMI_READ_REG(SMI_RR) & 0x00ffffff;
return ERROR_OK;
}{ ... }
static int stmsmi_probe(struct flash_bank *bank)
{
struct target *target = bank->target;
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
uint32_t io_base, sectorsize;
struct flash_sector *sectors;
uint32_t id = 0;
const struct stmsmi_target *target_device;
int retval;
if (stmsmi_info->probed)
free(bank->sectors);
stmsmi_info->probed = false;
for (target_device = target_devices ; target_device->name ; ++target_device)
if (target_device->tap_idcode == target->tap->idcode)
break;
if (!target_device->name) {
LOG_ERROR("Device ID 0x%" PRIx32 " is not known as SMI capable",
target->tap->idcode);
return ERROR_FAIL;
}if (!target_device->name) { ... }
switch (bank->base - target_device->smi_base) {
case 0:
stmsmi_info->bank_num = SMI_SEL_BANK0;
break;case 0:
case SMI_BANK_SIZE:
stmsmi_info->bank_num = SMI_SEL_BANK1;
break;case SMI_BANK_SIZE:
case 2*SMI_BANK_SIZE:
stmsmi_info->bank_num = SMI_SEL_BANK2;
break;case 2*SMI_BANK_SIZE:
case 3*SMI_BANK_SIZE:
stmsmi_info->bank_num = SMI_SEL_BANK3;
break;case 3*SMI_BANK_SIZE:
default:
LOG_ERROR("Invalid SMI base address " TARGET_ADDR_FMT, bank->base);
return ERROR_FAIL;default
}switch (bank->base - target_device->smi_base) { ... }
io_base = target_device->io_base;
stmsmi_info->io_base = io_base;
LOG_DEBUG("Valid SMI on device %s at address " TARGET_ADDR_FMT,
target_device->name, bank->base);
retval = read_flash_id(bank, &id);
SMI_SET_HW_MODE();
if (retval != ERROR_OK)
return retval;
stmsmi_info->dev = NULL;
for (const struct flash_device *p = flash_devices; p->name ; p++)
if (p->device_id == id) {
stmsmi_info->dev = p;
break;
}if (p->device_id == id) { ... }
if (!stmsmi_info->dev) {
LOG_ERROR("Unknown flash device (ID 0x%08" PRIx32 ")", id);
return ERROR_FAIL;
}if (!stmsmi_info->dev) { ... }
LOG_INFO("Found flash device \'%s\' (ID 0x%08" PRIx32 ")",
stmsmi_info->dev->name, stmsmi_info->dev->device_id);
bank->size = stmsmi_info->dev->size_in_bytes;
if (bank->size <= (1UL << 16))
LOG_WARNING("device needs 2-byte addresses - not implemented");
if (bank->size > (1UL << 24))
LOG_WARNING("device needs paging or 4-byte addresses - not implemented");
sectorsize = stmsmi_info->dev->sectorsize ?
stmsmi_info->dev->sectorsize : stmsmi_info->dev->size_in_bytes;
bank->num_sectors =
stmsmi_info->dev->size_in_bytes / sectorsize;
sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
if (!sectors) {
LOG_ERROR("not enough memory");
return ERROR_FAIL;
}if (!sectors) { ... }
for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
sectors[sector].offset = sector * sectorsize;
sectors[sector].size = sectorsize;
sectors[sector].is_erased = -1;
sectors[sector].is_protected = 1;
}for (unsigned int sector = 0; sector < bank->num_sectors; sector++) { ... }
bank->sectors = sectors;
stmsmi_info->probed = true;
return ERROR_OK;
}{ ... }
static int stmsmi_auto_probe(struct flash_bank *bank)
{
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
if (stmsmi_info->probed)
return ERROR_OK;
return stmsmi_probe(bank);
}{ ... }
static int stmsmi_protect_check(struct flash_bank *bank)
{
return ERROR_OK;
}{ ... }
static int get_stmsmi_info(struct flash_bank *bank, struct command_invocation *cmd)
{
struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
if (!(stmsmi_info->probed)) {
command_print_sameline(cmd, "\nSMI flash bank not probed yet\n");
return ERROR_OK;
}if (!(stmsmi_info->probed)) { ... }
command_print_sameline(cmd, "\nSMI flash information:\n"
" Device \'%s\' (ID 0x%08" PRIx32 ")\n",
stmsmi_info->dev->name, stmsmi_info->dev->device_id);
return ERROR_OK;
}{ ... }
const struct flash_driver stmsmi_flash = {
.name = "stmsmi",
.flash_bank_command = stmsmi_flash_bank_command,
.erase = stmsmi_erase,
.protect = stmsmi_protect,
.write = stmsmi_write,
.read = default_flash_read,
.probe = stmsmi_probe,
.auto_probe = stmsmi_auto_probe,
.erase_check = default_flash_blank_check,
.protect_check = stmsmi_protect_check,
.info = get_stmsmi_info,
.free_driver_priv = default_flash_free_driver_priv,
...};