-
Notifications
You must be signed in to change notification settings - Fork 1
/
spi_flash.cpp
633 lines (490 loc) · 11.7 KB
/
spi_flash.cpp
1
2
3
4
5
6
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
32
33
34
35
36
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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
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
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
221
222
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
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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
479
480
481
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#include "stdafx.h"
#include <time.h>
#include <cstring>
#include "spi-op.h"
#include "spi_flash.h"
#define FLASH_SIZE_INCREASEMENT (32 << 10)
#define FLASH_SIZE_SAMPLE_INTERVAL (4 << 10)
#define DATA_READ_LENGTH 0x20000
#define min(a, b) (((a) > (b)) ? (b) : (a))
static int flash_probed;
static const spi_flash_id *flash_id;
static unsigned int erase_size;
static unsigned char erase_op;
static unsigned char addr_width;
static unsigned char sst_write;
struct JEDEC GetJedecId(void)
{
unsigned char op = SPI_CMD_RDID;
unsigned char id[5];
struct JEDEC a;
SPIWriteThenRead(&op, 1, id, 5);
a.man = id[0];
a.type = id[1];
a.capacity = id[2];
return a;
}
static inline void AddrToCmd3(unsigned int addr, unsigned char *cmd)
{
cmd[0] = (addr >> 16) & 0xff;
cmd[1] = (addr >> 8) & 0xff;
cmd[2] = (addr) & 0xff;
}
static inline void AddrToCmd4(unsigned int addr, unsigned char *cmd)
{
cmd[0] = (addr >> 24) & 0xff;
cmd[1] = (addr >> 16) & 0xff;
cmd[2] = (addr >> 8) & 0xff;
cmd[3] = (addr) & 0xff;
}
static inline void AddrToCmd(unsigned int addr, unsigned char *cmd)
{
if (addr_width == 4)
AddrToCmd4(addr, cmd);
else
AddrToCmd3(addr, cmd);
}
static inline unsigned int CmdSize(void)
{
if (flash_id->size > SIZE_16MB)
return 5;
else
return 4;
}
static bool WriteEnable(void)
{
unsigned char op = SPI_CMD_WREN;
return SPIWrite(&op, 1);
}
static bool WriteDisable(void)
{
unsigned char op = SPI_CMD_WRDI;
return SPIWrite(&op, 1);
}
static bool ReadStatusRegister(unsigned int &sr)
{
unsigned char op = SPI_CMD_RDSR;
unsigned char val;
if (!SPIWriteThenRead(&op, 1, &val, 1))
return false;
sr = val;
return true;
}
static bool WriteStatusRegister(unsigned char sr)
{
unsigned char op[2];
op[0] = SPI_CMD_WRSR;
op[1] = sr & 0xff;
return SPIWrite(op, 2);
}
static bool SetAddressMode(int enable4b)
{
unsigned char op[2];
int need_wren = 0;
if (addr_width != 4)
return true;
/* ??????? */
switch (JEDEC_MFR(flash_id->jedec_id))
{
case MFR_MICROM:
need_wren = 1;
case MFR_EON:
case MFR_MACRONIX:
case MFR_WINBOND:
case MFR_GIGADEVICE:
if (need_wren)
if (!WriteEnable())
return false;
op[0] = enable4b ? SPI_CMD_ENTER_4B_MODE : SPI_CMD_EXIT_4B_MODE;
if (!SPIWrite(op, 1))
return false;
if (need_wren)
if (!WriteDisable())
return false;
break;
case MFR_ISSI:
case MFR_SPANSION:
op[0] = SPI_CMD_WRBR;
op[1] = (!!enable4b) << 7;
if (!SPIWrite(op, 2))
return false;
break;
}
if (enable4b)
return true;
/* ???? 3 ?????????????? 16MB ???? */
switch (JEDEC_MFR(flash_id->jedec_id))
{
case MFR_EON:
op[0] = SPI_CMD_EXIT_HBL_MODE;
if (!SPIWrite(op, 1))
return false;
break;
case MFR_MACRONIX:
/* MX25L25655E ????? */
if (flash_id->jedec_id == 0xc22619)
break;
case MFR_MICROM:
case MFR_WINBOND:
case MFR_GIGADEVICE:
if (!WriteEnable())
return false;
op[0] = SPI_CMD_WREAR;
op[1] = 0;
if (!SPIWrite(op, 2))
return false;
if (!WriteDisable())
return false;
break;
case MFR_ISSI:
case MFR_SPANSION:
/* ?????????? */
break;
}
return true;
}
static bool FlashPoll(void)
{
unsigned int sr;
do
{
if (!ReadStatusRegister(sr))
return false;
} while (sr & 1);
return true;
}
/*
bool FlashProbe(void)
{
unsigned char op = SPI_CMD_RDID;
unsigned char id[5], mask = 0;
unsigned int jedec_id, ext_id, sr;
int pre_unlock = 0;
if (flash_probed)
return true;
SPIWriteThenRead(&op, 1, id, 5);
jedec_id = id[0];
jedec_id = jedec_id << 8;
jedec_id |= id[1];
jedec_id = jedec_id << 8;
jedec_id |= id[2];
ext_id = id[3] << 8 | id[4];
if (!jedec_id || (jedec_id == 0xffffff))
{
fprintf(stderr, "Error: no valid flash found.\n");
return false;
}
flash_id = spi_flash_id_lookup(jedec_id, ext_id);
if (flash_id)
{
if (flash_id->flags & SF_4K_SECTOR)
{
if (flash_id->flags & SF_4K_PMC)
erase_op = SPI_CMD_4KB_PMC_ERASE;
else
erase_op = SPI_CMD_SECTOR_ERASE;
erase_size = SECTOR_4KB;
}
else if (flash_id->flags & SF_32K_BLOCK)
{
erase_op = SPI_CMD_32KB_BLOCK_ERASE;
erase_size = SECTOR_32KB;
}
else if (flash_id->flags & SF_64K_BLOCK)
{
erase_op = SPI_CMD_64KB_BLOCK_ERASE;
erase_size = SECTOR_64KB;
}
else if (flash_id->flags & SF_256K_BLOCK)
{
erase_op = SPI_CMD_64KB_BLOCK_ERASE;
erase_size = SECTOR_256KB;
}
if (flash_id->flags & SF_INIT_SR)
pre_unlock = 1;
if (flash_id->flags & SF_SST)
sst_write = 1;
addr_width = flash_id->size > SIZE_16MB ? 4 : 3;
}
else
{
fprintf(stderr, "Error: unrecognised flash found.\n");
return false;
}
if (pre_unlock)
{
if (!WriteEnable())
return false;
if (!WriteStatusRegister(0))
return false;
}
else if (flash_id && flash_id->flags & SF_BP_ALL)
{
if (!ReadStatusRegister(sr))
return false;
if (flash_id->flags & SF_BP0_2)
mask |= SR_BP0_2_MASK;
if (flash_id->flags & SF_BP3)
mask |= SR_BP3_MASK;
if (flash_id->flags & SF_BP4)
mask |= SR_BP4_MASK;
if (sr & mask)
{
sr &= ~mask;
if (!WriteEnable())
return false;
if (!WriteStatusRegister(sr))
return false;
if (!FlashPoll())
return false;
}
}
printf("Flash: %s\n", flash_id->model);
printf("Capacity: %dKiB\n", flash_id->size >> 10);
printf("Sector size: %dKiB\n", erase_size >> 10);
printf("\n");
flash_probed = 1;
return true;
}
*/
unsigned int FlashGetSize(void)
{
return flash_id->size;
}
bool FlashRead(unsigned int addr, unsigned int len, unsigned char *buf)
{
unsigned int flash_offset, len_read, len_to_read, len_left;
struct timespec start, end;
double time_used;
unsigned char op[6];
if (!len)
return true;
if (!buf)
return false;
flash_offset = addr;// % flash_id->size;
//if (!SetAddressMode(1))
// return false;
op[0] = SPI_CMD_READ_FAST;
op[4] = 0;
op[5] = 0;
// ProgressInit();
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
len_read = 0;
len_left = len;
while (len_left)
{
len_to_read = len_left > DATA_READ_LENGTH ? DATA_READ_LENGTH : len_left;
AddrToCmd(flash_offset + len_read, &op[1]);
if (!SPIWriteThenRead(op, 5, buf + len_read, len_to_read)) //CmdSize() + 1
return false;
len_read += len_to_read;
len_left -= len_to_read;
// ProgressShow(len_read * 100 / len);
}
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
// ProgressDone();
time_used = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0;
printf("Time used: %.2fs\n", time_used);
printf("Speed: %.2fKiB/s\n", (double) len / time_used / 1024);
if (!SetAddressMode(0))
return false;
return true;
}
static bool FlashEraseSector(unsigned int addr)
{
unsigned char cmd[5];
cmd[0] = erase_op;
AddrToCmd(addr, &cmd[1]);
if (!WriteEnable())
return false;
if (!SPIWrite(cmd, CmdSize()))
return false;
return FlashPoll();
}
bool FlashErase(unsigned int addr, unsigned int len)
{
unsigned int num_sectors, sector_left, size_erased;
struct timespec start, end;
double time_used;
if (addr % erase_size)
{
fprintf(stderr, "Error: start address is not on erase boundary.\n");
return false;
}
if ((addr + len) % erase_size)
{
fprintf(stderr, "Error: end address is not on erase boundary.\n");
return false;
}
if ((addr > flash_id->size) || (addr + len > flash_id->size))
{
fprintf(stderr, "Error: end address exceeds flash capacity.\n");
return false;
}
if (!SetAddressMode(1))
return false;
// ProgressInit();
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
size_erased = 0;
/* ???????????????????? */
num_sectors = len / erase_size;
sector_left = num_sectors;
while (sector_left)
{
if (!FlashEraseSector(addr))
return false;
addr += erase_size;
size_erased += erase_size;
sector_left--;
// ProgressShow(size_erased * 100 / len);
}
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
time_used = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0;
// ProgressDone();
printf("Time used: %.2fs\n", time_used);
printf("Speed: %.2fKiB/s, %.2fsec/s\n", len / time_used / 1024, num_sectors / time_used);
if (!SetAddressMode(0))
return false;
return true;
}
bool FlashChipErase(void)
{
unsigned char cmd;
bool ret;
struct timespec start, end;
double time_used;
cmd = SPI_CMD_CHIP_ERASE;
if (!WriteEnable())
return false;
if (!SPIWrite(&cmd, 1))
return false;
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
ret = FlashPoll();
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
time_used = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0;
printf("Time used: %.2fs\n", time_used);
return ret;
}
bool FlashSinglePageProgram(unsigned int addr, unsigned char *buff, unsigned int len)
{
WriteEnable();
unsigned char op[5];
op[0] = SPI_CMD_PAGE_PROG;
AddrToCmd3(addr, &op[1]); //AddrToCmd
if(!SPIWriteTwo(op, 4, buff, len))
return false;
return FlashPoll();
}
static bool FlashPageProgram(unsigned int addr, unsigned char *buff, unsigned int len)
{
unsigned int bytes_written = 0, bytes_to_write, bytes_left;
unsigned int dst;
unsigned char *src;
struct timespec start, end;
double time_used;
//if (!SetAddressMode(1))
// return false;
// ProgressInit();
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
bytes_left = len;
while (bytes_written < len)
{
src = buff + bytes_written;
dst = addr + bytes_written;
bytes_to_write = len;//min(bytes_left, PAGE_SIZE - (dst % PAGE_SIZE));
if (!WriteEnable())
return false;
if (!FlashSinglePageProgram(dst, src, bytes_to_write))
return false;
bytes_left -= bytes_to_write;
bytes_written += bytes_to_write;
// ProgressShow(bytes_written * 100 / len);
}
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
time_used = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0;
// ProgressDone();
printf("Time used: %.2fs\n", time_used);
printf("Speed: %.2fKiB/s\n", len / time_used / 1024);
//if (!SetAddressMode(0))
// return false;
return true;
}
static bool FlashSSTAAIProgram(unsigned int addr, unsigned char *buff, unsigned int len)
{
unsigned char op[6];
unsigned int dst = 0, bytes_written = 0;
int addr_sent = 0;
struct timespec start, end;
double time_used;
// ProgressInit();
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
if (addr % 2)
{
if (!WriteEnable())
return false;
if (!FlashSinglePageProgram(addr, buff, 1))
return false;
dst++;
}
if (!WriteEnable())
return false;
op[0] = SPI_CMD_AAI_WP;
AddrToCmd3(addr + dst, &op[1]);
while (len - dst >= 2)
{
if (!addr_sent)
{
op[4] = buff[dst++];
op[5] = buff[dst++];
if (!SPIWrite(op, 6))
return false;
addr_sent = 1;
}
else
{
op[1] = buff[dst++];
op[2] = buff[dst++];
if (!SPIWrite(op, 3))
return false;
}
if (!FlashPoll())
return false;
bytes_written += 2;
// if (bytes_written % 256 == 0)
// ProgressShow(bytes_written * 100 / len);
}
if (!WriteDisable())
return false;
if (!FlashPoll())
return false;
if (dst < len)
{
if (!WriteEnable())
return false;
if (!FlashSinglePageProgram(addr + dst, buff + dst, 1))
return false;
dst++;
}
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
time_used = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0;
// ProgressDone();
printf("Time used: %.2fs\n", time_used);
printf("Speed: %.2fKiB/s\n", len / time_used / 1024);
if (!WriteDisable())
return false;
return true;
}
bool FlashWrite(unsigned int addr, unsigned char *buff, unsigned int len)
{
if (!buff)
return false;
//if ((addr > flash_id->size) || (addr + len > flash_id->size))
//{
// fprintf(stderr, "Error: write address exceeds flash capacity.\n");
// return false;
//}
if (1) //(sst_write)
return FlashSSTAAIProgram(addr, buff, len);
else
return FlashPageProgram(addr, buff, len);
}