-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathspi_flash.c
More file actions
2244 lines (1918 loc) · 63 KB
/
Copy pathspi_flash.c
File metadata and controls
2244 lines (1918 loc) · 63 KB
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include "spi_flash.h"
#include "flash/flash_common.h"
#include "flash/flash_logging.h"
#include "common/unused.h"
/* Status bits indicating when flash is operating in 4-byte address mode. */
#define MACRONIX_4BYTE_STATUS (1U << 5)
#define WINBOND_4BYTE_STATUS (1U << 0)
#define MICRON_4BYTE_STATES (1U << 0)
/* Config bits indicating address mode on reset. */
#define WINBOND_4BYTE_DEFAULT (1U << 1)
#define MICRON_4BYTE_DEFAULT (1U << 0)
/* Status bits indicating when flash has QSPI enabled. */
#define RESET_HOLD_ENABLE (1U << 4)
#define QSPI_ENABLE_BIT1 (1U << 1)
#define QSPI_ENABLE_BIT6 (1U << 6)
#define QSPI_ENABLE_BIT7 (1U << 7)
/**
* Check the requested operation to ensure it is valid for the device.
*/
#define SPI_FLASH_BOUNDS_CHECK(bytes, addr, len) \
if (addr >= bytes) { \
return SPI_FLASH_ADDRESS_OUT_OF_RANGE; \
} \
\
if ((addr + len) > bytes) { \
return SPI_FLASH_OPERATION_OUT_OF_RANGE; \
}
/**
* Configure the read command for the flash device.
*
* @param flash The flash interface to configure.
* @param command Read command information to use for configuration.
* @param opcode_4byte The read command to use in 4-byte mode.
* @param use_4byte Flag indicating if 4-byte mode is enabled.
* @param flags Transaction flags for the read.
*/
static void spi_flash_configure_read_command (const struct spi_flash *flash,
const struct spi_flash_sfdp_read_cmd *command, uint8_t opcode_4byte, bool use_4byte,
uint16_t flags)
{
flash->state->command.read_dummy = command->dummy_bytes;
flash->state->command.read_mode = command->mode_bytes;
flash->state->command.read_flags = flags;
if (use_4byte) {
flash->state->command.read = opcode_4byte;
flash->state->command.read_flags |= FLASH_FLAG_4BYTE_ADDRESS;
}
else {
flash->state->command.read = command->opcode;
}
}
/**
* Configure the program and erase commands for the flash device.
*
* @param flash The flash interface to configure.
*/
static void spi_flash_set_write_erase_commands (const struct spi_flash *flash)
{
if ((flash->state->capabilities & (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR)) ==
(FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR)) {
flash->state->command.write = FLASH_CMD_4BYTE_PP;
flash->state->command.write_flags = FLASH_FLAG_4BYTE_ADDRESS;
flash->state->command.erase_sector = FLASH_CMD_4BYTE_4K_ERASE;
flash->state->command.sector_flags = FLASH_FLAG_4BYTE_ADDRESS;
flash->state->command.erase_block = FLASH_CMD_4BYTE_64K_ERASE;
flash->state->command.block_flags = FLASH_FLAG_4BYTE_ADDRESS;
}
}
/**
* Configure the command set for the device based on its capabilities.
*
* @param flash The flash interface to configure.
* @param read Information from SFDP for read commands.
* @param sfdp SFDP tables for additional command information.
*/
static void spi_flash_set_device_commands (const struct spi_flash *flash,
const struct spi_flash_sfdp_read_commands *read, const struct spi_flash_sfdp_basic_table *sfdp)
{
bool use_4byte;
use_4byte = ((flash->state->capabilities & (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR)) ==
(FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR));
if (read && (flash->state->capabilities & FLASH_CAP_QUAD_1_4_4)) {
spi_flash_configure_read_command (flash, &read->quad_1_4_4, FLASH_CMD_4BYTE_QIO_READ,
use_4byte, FLASH_FLAG_QUAD_ADDR | FLASH_FLAG_QUAD_DATA);
}
else if (read && (flash->state->capabilities & FLASH_CAP_QUAD_1_1_4)) {
spi_flash_configure_read_command (flash, &read->quad_1_1_4, FLASH_CMD_4BYTE_QUAD_READ,
use_4byte, FLASH_FLAG_QUAD_DATA);
}
else if (read && (flash->state->capabilities & FLASH_CAP_DUAL_1_2_2)) {
spi_flash_configure_read_command (flash, &read->dual_1_2_2, FLASH_CMD_4BYTE_DIO_READ,
use_4byte, FLASH_FLAG_DUAL_ADDR | FLASH_FLAG_DUAL_DATA);
}
else if (read && (flash->state->capabilities & FLASH_CAP_DUAL_1_1_2)) {
spi_flash_configure_read_command (flash, &read->dual_1_1_2, FLASH_CMD_4BYTE_DUAL_READ,
use_4byte, FLASH_FLAG_DUAL_DATA);
}
else if (use_4byte) {
if (flash->state->use_fast_read) {
flash->state->command.read = FLASH_CMD_4BYTE_FAST_READ;
flash->state->command.read_dummy = 1;
}
else {
flash->state->command.read = FLASH_CMD_4BYTE_READ;
}
flash->state->command.read_flags = FLASH_FLAG_4BYTE_ADDRESS;
}
spi_flash_set_write_erase_commands (flash);
if (sfdp) {
spi_flash_sfdp_get_reset_command (sfdp, &flash->state->command.reset);
spi_flash_sfdp_get_deep_powerdown_commands (sfdp, &flash->state->command.enter_pwrdown,
&flash->state->command.release_pwrdown);
}
}
/**
* Configure a device for use and detect device properties. The device interface must be fully
* initialized prior finishing device and interface configuration.
*
* This will complete the steps outlined for spi_flash_initialize_device and
* spi_flash_initialize_device_state.
*
* @param flash The flash interface to configure.
* @param wake_device Flag indicating if the device should be removed from deep power down.
* @param reset_device Flag indicating if the device should be reset prior to initialization.
* @param drive_strength Flag indicating if the device output drive strength should be configured.
*
* @return 0 if the device and interface were successfully configured or an error code.
*/
static int spi_flash_configure_device (const struct spi_flash *flash, bool wake_device,
bool reset_device, bool drive_strength)
{
struct spi_flash_sfdp sfdp;
int status;
if (wake_device) {
status = spi_flash_deep_power_down (flash, 0);
if (status != 0) {
return status;
}
}
status = spi_flash_get_device_id (flash, NULL, NULL);
if (status != 0) {
return status;
}
if ((flash->state->device_id[0] == 0xff) || (flash->state->device_id[0] == 0x00)) {
status = SPI_FLASH_NO_DEVICE;
return status;
}
status = spi_flash_sfdp_init (&sfdp, flash->spi);
if (status != 0) {
return status;
}
status = spi_flash_discover_device_properties (flash, &sfdp);
if (status != 0) {
goto exit;
}
/* Make sure the device is not writing any data before we proceed. Resets will corrupt the
* flash and register writes will fail if a write is currently in progress. */
status = spi_flash_wait_for_write (flash, 30000);
if (status != 0) {
goto exit;
}
if (reset_device) {
status = spi_flash_reset_device (flash);
if ((status != 0) && (status != SPI_FLASH_RESET_NOT_SUPPORTED)) {
goto exit;
}
}
if (drive_strength) {
status = spi_flash_configure_drive_strength (flash);
if (status != 0) {
goto exit;
}
}
if ((flash->state->capabilities & (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR)) ==
(FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR)) {
status = spi_flash_detect_4byte_address_mode (flash);
if (status != 0) {
goto exit;
}
}
if (flash->state->command.read_flags & FLASH_FLAG_QUAD_DATA) {
status = spi_flash_enable_quad_spi (flash, 1);
if (status != 0) {
goto exit;
}
}
status = spi_flash_clear_block_protect (flash);
if (status != 0) {
goto exit;
}
exit:
spi_flash_sfdp_release (&sfdp);
return status;
}
/**
* Completely initialize a SPI flash interface and device so it is ready for use. This includes:
* - Initializing the SPI flash interface.
* - Configuring the interface and device based on discovered properties.
* - Detecting the address mode of the device.
*
* @param flash The flash interface to initialize.
* @param state Variable context for the flash interface. This must be uninitialized.
* @param spi The SPI master connected to the flash.
* @param fast_read Flag indicating if the FAST_READ command should be used for SPI reads.
* @param wake_device Flag indicating if the device should be removed from deep power down.
* @param reset_device Flag indicating if the device should be reset prior to initialization.
* @param drive_strength Flag indicating if the device output drive strength should be configured.
*
* @return 0 if the SPI flash was successfully initialized or an error code.
*/
int spi_flash_initialize_device (struct spi_flash *flash, struct spi_flash_state *state,
const struct flash_master *spi, bool fast_read, bool wake_device, bool reset_device,
bool drive_strength)
{
int status;
if (fast_read) {
status = spi_flash_init_fast_read (flash, state, spi);
}
else {
status = spi_flash_init (flash, state, spi);
}
if (status != 0) {
return status;
}
status = spi_flash_configure_device (flash, wake_device, reset_device, drive_strength);
if (status != 0) {
spi_flash_release (flash);
}
return status;
}
/**
* Completely initialize a SPI flash interface and device so it is ready for use. This includes:
* - Initializing the SPI flash interface state. The rest of the base interface is assumed to
* already be initialized, likely through static initialization.
* - Configuring the interface and device based on discovered properties.
* - Detecting the address mode of the device.
*
* @param flash The flash interface that contains the state to initialize.
* @param fast_read Flag indicating if the FAST_READ command should be used for SPI reads.
* @param wake_device Flag indicating if the device should be removed from deep power down.
* @param reset_device Flag indicating if the device should be reset prior to initialization.
* @param drive_strength Flag indicating if the device output drive strength should be configured.
*
* @return 0 if the SPI flash was successfully initialized or an error code.
*/
int spi_flash_initialize_device_state (const struct spi_flash *flash, bool fast_read,
bool wake_device, bool reset_device, bool drive_strength)
{
int status;
if (fast_read) {
status = spi_flash_init_state_fast_read (flash);
}
else {
status = spi_flash_init_state (flash);
}
if (status != 0) {
return status;
}
status = spi_flash_configure_device (flash, wake_device, reset_device, drive_strength);
if (status != 0) {
spi_flash_release (flash);
}
return status;
}
/**
* Complete the restore process for a the state of a flash interface that has already been
* initialized.
*
* @param flash The flash interface to restore.
* @param info The saved device information to restore interface state from.
*/
static void spi_flash_finish_device_restore (const struct spi_flash *flash,
const struct spi_flash_device_info *info)
{
memcpy (flash->state->device_id, info->device_id, sizeof (flash->state->device_id));
flash->state->device_size = info->device_size;
flash->state->capabilities = info->capabilities;
flash->state->use_busy_flag = !!(info->flags & SPI_FLASH_DEVICE_INFO_BUSY_FLAG);
flash->state->switch_4byte = (enum spi_flash_sfdp_4byte_addressing) info->switch_4byte;
flash->state->reset_3byte = !!(info->flags & SPI_FLASH_DEVICE_INFO_RESET_3BYTE);
flash->state->quad_enable = (enum spi_flash_sfdp_quad_enable) info->quad_enable;
flash->state->sr1_volatile = !!(info->flags & SPI_FLASH_DEVICE_INFO_SR1_VOLATILE);
flash->state->command.read = info->read_opcode;
flash->state->command.read_dummy = info->read_dummy;
flash->state->command.read_mode = info->read_mode;
flash->state->command.read_flags = info->read_flags;
spi_flash_set_write_erase_commands (flash);
flash->state->command.reset = info->reset_opcode;
flash->state->command.enter_pwrdown = info->enter_pwrdown;
flash->state->command.release_pwrdown = info->release_pwrdown;
}
/**
* Initialize a SPI flash device from a saved context. Upon completion, the interface will be ready
* to use, but no transaction with the flash device will be performed. This could leave the
* interface and device in an inconsistent state (e.g. the current address mode). It is recommended
* that the interface be synchronized with the flash when SPI accesses are possible.
*
* @param flash The flash interface to initialize.
* @param state Variable context for the flash interface. This must be uninitialized.
* @param spi The SPI master connected to the flash.
* @param info The saved device information to use for interface initialization.
*
* @return 0 if the flash interface was successfully initialized or an error code.
*/
int spi_flash_restore_device (struct spi_flash *flash, struct spi_flash_state *state,
const struct flash_master *spi, const struct spi_flash_device_info *info)
{
int status;
if (info == NULL) {
return SPI_FLASH_INVALID_ARGUMENT;
}
if (info->use_fast_read) {
status = spi_flash_init_fast_read (flash, state, spi);
}
else {
status = spi_flash_init (flash, state, spi);
}
if (status != 0) {
return status;
}
spi_flash_finish_device_restore (flash, info);
return 0;
}
/**
* Initialize a SPI flash device state from a saved context. Upon completion, the interface will be
* ready to use, but no transaction with the flash device will be performed. This could leave the
* interface and device in an inconsistent state (e.g. the current address mode). It is recommended
* that the interface be synchronized with the flash when SPI accesses are possible.
*
* Only the state will be initialized. The rest of the interface is assumed to already have been
* initialized, likely through static initialization.
*
* @param flash The flash interface that contains the state to initialize.
* @param info The saved device information to use for interface initialization.
*
* @return 0 if the flash interface was successfully initialized or an error code.
*/
int spi_flash_restore_device_state (const struct spi_flash *flash,
const struct spi_flash_device_info *info)
{
int status;
if (info == NULL) {
return SPI_FLASH_INVALID_ARGUMENT;
}
if (info->use_fast_read) {
status = spi_flash_init_state_fast_read (flash);
}
else {
status = spi_flash_init_state (flash);
}
if (status != 0) {
return status;
}
spi_flash_finish_device_restore (flash, info);
return 0;
}
/**
* Initialize the SPI flash interface API.
*
* @param flash The flash interface to initialize.
* @param state Variable context for the flash interface. This must be uninitialized.
* @param spi The SPI master connected to the flash.
*
* @return 0 if the flash API was initialized or an error code.
*/
static int spi_flash_init_api (struct spi_flash *flash, struct spi_flash_state *state,
const struct flash_master *spi)
{
if ((flash == NULL) || (state == NULL) || (spi == NULL)) {
return SPI_FLASH_INVALID_ARGUMENT;
}
memset (flash, 0, sizeof (struct spi_flash));
flash->base.get_device_size =
(int (*) (const struct flash*, uint32_t*)) spi_flash_get_device_size;
flash->base.read = (int (*) (const struct flash*, uint32_t, uint8_t*, size_t)) spi_flash_read;
flash->base.get_page_size = (int (*) (const struct flash*, uint32_t*)) spi_flash_get_page_size;
flash->base.minimum_write_per_page =
(int (*) (const struct flash*, uint32_t*)) spi_flash_minimum_write_per_page;
flash->base.write =
(int (*) (const struct flash*, uint32_t, const uint8_t*, size_t)) spi_flash_write;
flash->base.get_sector_size =
(int (*) (const struct flash*, uint32_t*)) spi_flash_get_sector_size;
flash->base.sector_erase = (int (*) (const struct flash*, uint32_t)) spi_flash_sector_erase;
flash->base.get_block_size =
(int (*) (const struct flash*, uint32_t*)) spi_flash_get_block_size;
flash->base.block_erase = (int (*) (const struct flash*, uint32_t)) spi_flash_block_erase;
flash->base.chip_erase = (int (*) (const struct flash*)) spi_flash_chip_erase;
flash->state = state;
flash->spi = spi;
return 0;
}
/**
* Initialize the SPI flash interface.
*
* This is not sufficient to be able to fully access the SPI flash device. Use
* {@link spi_flash_initialize_device} for complete device initialization.
*
* @param flash The flash interface to initialize.
* @param state Variable context for the flash interface. This must be uninitialized.
* @param spi The SPI master connected to the flash.
*
* @return 0 if the flash interface was initialized or an error code.
*/
int spi_flash_init (struct spi_flash *flash, struct spi_flash_state *state,
const struct flash_master *spi)
{
int status;
status = spi_flash_init_api (flash, state, spi);
if (status == 0) {
status = spi_flash_init_state (flash);
}
return status;
}
/**
* Initialize the SPI flash interface. The FAST_READ command will be used for SPI reads.
*
* This is not sufficient to be able to fully access the SPI flash device. Use
* {@link spi_flash_initialize_device} for complete device initialization.
*
* @param flash The flash interface to initialize.
* @param state Variable context for the flash interface. This must be uninitialized.
* @param spi The SPI master connected to the flash.
*
* @return 0 if the flash interface was initialized or an error code.
*/
int spi_flash_init_fast_read (struct spi_flash *flash, struct spi_flash_state *state,
const struct flash_master *spi)
{
int status;
status = spi_flash_init_api (flash, state, spi);
if (status == 0) {
status = spi_flash_init_state_fast_read (flash);
}
return status;
}
/**
* Initialize only the variable state for an SPI flash interface. The rest of the interface is
* assumed to have already been initialized.
*
* This would generally be used with a statically initialized instance.
*
* This is not sufficient to be able to fully access the SPI flash device. Use
* {@link spi_flash_initialize_device_state} for complete device initialization.
*
* @param flash The flash interface that contains the state to initialize.
*
* @return 0 if the state was successfully initialized or an error code.
*/
int spi_flash_init_state (const struct spi_flash *flash)
{
int status;
if ((flash == NULL) || (flash->state == NULL) || (flash->spi == NULL)) {
return SPI_FLASH_INVALID_ARGUMENT;
}
memset (flash->state, 0, sizeof (struct spi_flash_state));
status = platform_mutex_init (&flash->state->lock);
if (status != 0) {
return status;
}
flash->state->device_id[0] = 0xff;
/* Populate common command codes for basic flash operations. */
flash->state->command.read = FLASH_CMD_READ;
flash->state->command.write = FLASH_CMD_PP;
flash->state->command.erase_sector = FLASH_CMD_4K_ERASE;
flash->state->command.erase_block = FLASH_CMD_64K_ERASE;
/* Make assumptions about the power down command support to allow the overall device
* initialization sequence to wake devices up. If the device is powered down, it will not
* respond to any commands, so there is no way to query the device to determine command
* support. If scenarios arise where different commands are needed, the interface will need
* some additional information from the caller. */
flash->state->command.enter_pwrdown = FLASH_CMD_DP;
flash->state->command.release_pwrdown = FLASH_CMD_RDP;
/* Make an assumption in the default case that the flash device supports the common 66/99
* sequence for triggering a soft reset, and that the reset reverts the address mode to the
* default state. This allows some minimal scenarios where additional device discovery is not
* necessary to still reset the device, but most scenarios will never see thees default
* assumptions. */
flash->state->command.reset = FLASH_CMD_RST;
flash->state->reset_3byte = true;
/* Continuing with default assumptions, assume a device that supports both 3 and 4 byte address
* modes. */
flash->state->capabilities = (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR);
return 0;
}
/**
* Initialize only the variable state for an SPI flash interface. The rest of the interface is
* assumed to have already been initialized. The FAST_READ command will be used for SPI reads.
*
* This would generally be used with a statically initialized instance.
*
* This is not sufficient to be able to fully access the SPI flash device. Use
* {@link spi_flash_initialize_device_state} for complete device initialization.
*
* @param flash The flash interface that contains the state to initialize.
*
* @return 0 if the state was successfully initialized or an error code.
*/
int spi_flash_init_state_fast_read (const struct spi_flash *flash)
{
int status;
status = spi_flash_init_state (flash);
if (status == 0) {
flash->state->use_fast_read = true;
flash->state->command.read = FLASH_CMD_FAST_READ;
flash->state->command.read_dummy = 1;
}
return status;
}
/**
* Release the SPI flash interface.
*
* @param flash The flash interface to release.
*/
void spi_flash_release (const struct spi_flash *flash)
{
if (flash) {
platform_mutex_free (&flash->state->lock);
}
}
/**
* Save the SPI device context. This will allow a new SPI flash interface to be created to
* communicate with the flash device without needing to query the device.
*
* @param flash The flash interface to save.
* @param info The context that will be updated for the flash device.
*
* @return 0 if the flash device context was successfully saved or an error code.
*/
int spi_flash_save_device_info (const struct spi_flash *flash, struct spi_flash_device_info *info)
{
if ((flash == NULL) || (info == NULL)) {
return SPI_FLASH_INVALID_ARGUMENT;
}
info->version = SPI_FLASH_DEVICE_INFO_VERSION;
memcpy (info->device_id, flash->state->device_id, sizeof (info->device_id));
info->device_size = flash->state->device_size;
info->capabilities = flash->state->capabilities;
info->use_fast_read = flash->state->use_fast_read;
info->read_opcode = flash->state->command.read;
info->read_dummy = flash->state->command.read_dummy;
info->read_mode = flash->state->command.read_mode;
info->read_flags = flash->state->command.read_flags;
info->reset_opcode = flash->state->command.reset;
info->enter_pwrdown = flash->state->command.enter_pwrdown;
info->release_pwrdown = flash->state->command.release_pwrdown;
info->switch_4byte = flash->state->switch_4byte;
info->quad_enable = flash->state->quad_enable;
info->flags = 0;
if (flash->state->use_busy_flag) {
info->flags |= SPI_FLASH_DEVICE_INFO_BUSY_FLAG;
}
if (flash->state->reset_3byte) {
info->flags |= SPI_FLASH_DEVICE_INFO_RESET_3BYTE;
}
if (flash->state->sr1_volatile) {
info->flags |= SPI_FLASH_DEVICE_INFO_SR1_VOLATILE;
}
return 0;
}
/**
* Send a write command to the flash that only sends the command code.
*
* @param flash The flash instance to use to send the command.
* @param cmd The command code to send to the device.
*
* @return 0 if the command was successfully sent or an error code.
*/
static int spi_flash_simple_command (const struct spi_flash *flash, uint8_t cmd)
{
struct flash_xfer xfer;
FLASH_XFER_INIT_CMD_ONLY (xfer, cmd, 0);
return flash->spi->xfer (flash->spi, &xfer);
}
/**
* Send the write enable command to the flash device.
*
* @param flash The flash instance to use to send the command.
*
* @return 0 if the command was successfully sent or an error code.
*/
static int spi_flash_write_enable (const struct spi_flash *flash)
{
return spi_flash_simple_command (flash, FLASH_CMD_WREN);
}
/**
* Send the volatile write enable command to the flash device.
*
* @param flash The flash instance to use to send the command.
*
* @return 0 if the command was successfully sent or an error code.
*/
static int spi_flash_volatile_write_enable (const struct spi_flash *flash)
{
return spi_flash_simple_command (flash, FLASH_CMD_VOLATILE_WREN);
}
/**
* Determine if the flash is currently executing a write command.
*
* @param flash The flash instance to check.
*
* @return 0 if no write is in progress, 1 if there is, or an error code.
*/
static int spi_flash_is_wip_set (const struct spi_flash *flash)
{
struct flash_xfer xfer;
uint8_t reg;
int status;
if (!flash->state->use_busy_flag) {
FLASH_XFER_INIT_READ_REG (xfer, FLASH_CMD_RDSR, ®, 1, 0);
}
else {
FLASH_XFER_INIT_READ_REG (xfer, FLASH_CMD_RDSR_FLAG, ®, 1, 0);
}
status = flash->spi->xfer (flash->spi, &xfer);
if (status == 0) {
if (!flash->state->use_busy_flag) {
return ((reg & FLASH_STATUS_WIP) != 0);
}
else {
return ((reg & FLASH_FLAG_STATUS_READY) == 0);
}
}
else {
return status;
}
}
/**
* Wait for a write operation to complete.
*
* @param flash The flash instance that is executing a write operation.
* @param timeout The maximum number of milliseconds to wait for completion. A negative number will
* wait forever. 0 will return immediately.
* @param no_sleep Flag indicating no sleep should be inserted between status reads.
*
* @return 0 if the write was completed or an error code.
*/
static int spi_flash_wait_for_write_completion (const struct spi_flash *flash, int32_t timeout,
uint8_t no_sleep)
{
platform_clock timeout_val;
int done = 0;
int status;
if (timeout > 0) {
status = platform_init_timeout (timeout, &timeout_val);
if (status) {
return status;
}
}
do {
status = spi_flash_is_wip_set (flash);
if (status == 0) {
done = 1;
}
else if (status == 1) {
status = 0;
if ((timeout > 0) && (platform_has_timeout_expired (&timeout_val) == 1)) {
status = SPI_FLASH_WIP_TIMEOUT;
}
else if (timeout == 0) {
status = SPI_FLASH_WIP_TIMEOUT;
}
if (status == 0) {
if (!no_sleep) {
platform_msleep (10);
}
}
}
} while ((status == 0) && !done);
return status;
}
/**
* Send a write command that writes to register that requires no addressing. This will block until
* the register write has completed.
*
* @param flash The flash instance to use to send the command.
* @param cmd The command code that writes the register.
* @param data The data to write to the register.
* @param length The length of the data to write.
* @param volatile_wren Flag indicating the volatile write enable command should be used.
*
* @return 0 if the command was successfully completed or an error code.
*/
static int spi_flash_write_register (const struct spi_flash *flash, uint8_t cmd, uint8_t *data,
size_t length, bool volatile_wren)
{
struct flash_xfer xfer;
int status;
status = spi_flash_is_wip_set (flash);
if (status != 0) {
return (status == 1) ? SPI_FLASH_WRITE_IN_PROGRESS : status;
}
if (volatile_wren) {
status = spi_flash_volatile_write_enable (flash);
}
else {
status = spi_flash_write_enable (flash);
}
if (status != 0) {
return status;
}
FLASH_XFER_INIT_WRITE_REG (xfer, cmd, data, length, 0);
status = flash->spi->xfer (flash->spi, &xfer);
if (status != 0) {
return status;
}
return spi_flash_wait_for_write_completion (flash, -1, 1);
}
/**
* Discover device properties necessary for operation through SFDP. This must be done prior to
* using the interface to the device.
*
* This call supersedes {@link spi_flash_set_device_size}, which should no longer be used outside
* of unit testing. This call sets the device size and will also detect and configure many other
* relevant parameters.
*
* @param flash The flash interface to configure.
* @param sfdp The SFDP interface to use for property detection. The SFDP instance is not
* maintained internally and it can be managed independently of the SPI flash interface.
*
* @return 0 if the SPI flash properties were successfully detected or an error code.
*/
int spi_flash_discover_device_properties (const struct spi_flash *flash,
const struct spi_flash_sfdp *sfdp)
{
struct spi_flash_sfdp_basic_table parameters;
uint32_t spi_capabilities;
struct spi_flash_sfdp_read_commands read;
int status;
if ((flash == NULL) || (sfdp == NULL)) {
return SPI_FLASH_INVALID_ARGUMENT;
}
status = spi_flash_sfdp_basic_table_init (¶meters, sfdp);
if (status != 0) {
return status;
}
platform_mutex_lock (&flash->state->lock);
spi_flash_sfdp_get_device_capabilities (¶meters, &flash->state->capabilities);
spi_flash_sfdp_get_read_commands (¶meters, &read);
spi_capabilities = flash->spi->capabilities (flash->spi) & flash->state->capabilities;
if ((spi_capabilities & (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR)) !=
(flash->state->capabilities & (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR))) {
status = SPI_FLASH_INCOMPATIBLE_SPI_MASTER;
goto exit;
}
flash->state->capabilities = spi_capabilities;
flash->state->reset_3byte = false;
switch (flash->state->capabilities & (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR)) {
case (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR):
if (!spi_flash_sfdp_supports_4byte_commands (¶meters)) {
/* We expect the flash device to support explicit 4-byte address commands. If it
* does not, we can't support communicating with that flash. It is possible to add
* support for these devices using enter/exit 4-byte commands, EAR, etc., but that
* is a lot more complicated. Since devices that don't support these commands are
* a minority of devices or older, there is not much benefit to adding this now. */
status = SPI_FLASH_NO_4BYTE_CMDS;
goto exit;
}
flash->state->reset_3byte = spi_flash_sfdp_exit_4byte_mode_on_reset (¶meters);
break;
case FLASH_CAP_4BYTE_ADDR:
flash->state->addr_mode = FLASH_FLAG_4BYTE_ADDRESS;
break;
}
spi_flash_set_device_commands (flash, &read, ¶meters);
status = spi_flash_sfdp_get_4byte_mode_switch (¶meters, &flash->state->switch_4byte);
if (status != 0) {
goto exit;
}
status = spi_flash_sfdp_get_quad_enable (¶meters, &flash->state->quad_enable);
if (status != 0) {
goto exit;
}
status = spi_flash_sfdp_get_device_size (¶meters);
if (ROT_IS_ERROR (status)) {
goto exit;
}
flash->state->device_size = status;
flash->state->use_busy_flag = spi_flash_sfdp_use_busy_flag_status (¶meters);
flash->state->sr1_volatile = spi_flash_sfdp_use_volatile_write_enable (¶meters);
status = 0;
exit:
platform_mutex_unlock (&flash->state->lock);
spi_flash_sfdp_basic_table_release (¶meters);
return status;
}
/**
* Set the capacity of the flash device. This must be set before using the interface to the
* device.
*
* While this should not be used in most application code, there are scenarios where setting the
* flash device size in this way can be useful. Specifically, this works when the flash device is
* definitively known and the overhead of SFDP is not desirable. It is also useful for test code.
* If this function is used in any other scenario, it is possible to configure the driver in a way
* that is not compatible with the flash device.
*
* In general, {@link spi_flash_discover_device_properties} should be used to set the device size.
*
* @param flash The flash instance to configure.
* @param bytes The capacity of the physical flash device, in bytes.
*
* @return 0 if the interface was configured successfully or an error code.
*/
int spi_flash_set_device_size (const struct spi_flash *flash, uint32_t bytes)
{
if (flash == NULL) {
return SPI_FLASH_INVALID_ARGUMENT;
}
platform_mutex_lock (&flash->state->lock);
flash->state->device_size = bytes;
if (bytes > 0x1000000) {
/* Assume a device that can switch between address modes. */
flash->state->capabilities = (FLASH_CAP_3BYTE_ADDR | FLASH_CAP_4BYTE_ADDR);
spi_flash_set_device_commands (flash, NULL, NULL);
}
else {
/* Devices with 16MB or less of storage typically don't support 4-byte address mode, nor do
* they need it. */
flash->state->capabilities = FLASH_CAP_3BYTE_ADDR;
}
platform_mutex_unlock (&flash->state->lock);
return 0;
}
/**
* Set the opcode and parameters that should be used when reading data from flash.
*
* This will ignore any other properties of the device and/or SPI master and use exactly what is
* provided to this function. Given that, it is possible to configure the driver in a way that is
* not compatible with the flash device, SPI master, or both. Therefore, it should only be used in
* scenarios where the system configuration and state are definitively known.
*
* In general, {@link spi_flash_discover_device_properties} should be used to properly configure the
* driver state.
*
* @param flash Tha flash instance to configure.
* @param command Read command information that should be used by the driver.
* @param flags Transaction flags for read operations.
*
* @return 0 if the interface was configured successfully or an error code.
*/
int spi_flash_set_read_command (const struct spi_flash *flash,
const struct spi_flash_sfdp_read_cmd *command, uint16_t flags)
{
if ((flash == NULL) || (command == NULL)) {
return SPI_FLASH_INVALID_ARGUMENT;
}
platform_mutex_lock (&flash->state->lock);
spi_flash_configure_read_command (flash, command, 0, false, flags);
platform_mutex_unlock (&flash->state->lock);
return 0;
}
/**
* Set the opcode and parameters that should be used when writing data to flash. This does not
* affect erase commands.
*
* This will ignore any other properties of the device and/or SPI master and use exactly what is
* provided to this function. Given that, it is possible to configure the driver in a way that is
* not compatible with the flash device, SPI master, or both. Therefore, it should only be used in
* scenarios where the system configuration and state are definitively known.
*
* In general, {@link spi_flash_discover_device_properties} should be used to properly configure the
* driver state.
*
* @param flash Tha flash instance to configure.
* @param opcode Write command code that should be used by the driver.
* @param flags Transaction flags for write operations.
*
* @return 0 if the interface was configured successfully or an error code.
*/
int spi_flash_set_write_command (const struct spi_flash *flash, uint8_t opcode, uint16_t flags)
{