-
Notifications
You must be signed in to change notification settings - Fork 1
/
QSPIFBlockDeviceAsas.cpp
1634 lines (1396 loc) · 58.7 KB
/
QSPIFBlockDeviceAsas.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
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
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Arduino.h"
#include "blockdevice/internal/SFDP.h"
#include "platform/Callback.h"
#include "QSPIFBlockDeviceAsas.h"
#include <string.h>
#include "rtos/ThisThread.h"
#ifndef MBED_CONF_MBED_TRACE_ENABLE
#define MBED_CONF_MBED_TRACE_ENABLE 0
#endif
#include "mbed-trace/mbed_trace.h"
#define TRACE_GROUP "QSPIF"
using namespace std::chrono;
using namespace mbed;
/* Default QSPIF Parameters */
/****************************/
#define QSPIF_DEFAULT_SE_SIZE 4096
// The SFDP spec only defines two status registers. But some devices,
// have three "status-like" registers (one status, two config)
#define QSPI_MAX_STATUS_REGISTERS 3
#define QSPI_DEFAULT_STATUS_REGISTERS 2
#ifndef UINT64_MAX
#define UINT64_MAX -1
#endif
#define QSPI_NO_ADDRESS_COMMAND UINT64_MAX
#define QSPI_ALT_DEFAULT_VALUE 0
// Status Register Bits
#define QSPIF_STATUS_BIT_WIP 0x1 // Write In Progress
#define QSPIF_STATUS_BIT_WEL 0x2 // Write Enable Latch
#define QSPIF_NO_QUAD_ENABLE (-1)
/* SFDP Header Parsing */
/***********************/
#define QSPIF_RSFDP_DUMMY_CYCLES 8
/* Basic Parameters Table Parsing */
/**********************************/
//READ Instruction support according to BUS Configuration
#define QSPIF_BASIC_PARAM_TABLE_FAST_READ_SUPPORT_BYTE 2
#define QSPIF_BASIC_PARAM_TABLE_QPI_READ_SUPPORT_BYTE 16
#define QSPIF_BASIC_PARAM_TABLE_444_READ_INST_BYTE 27
#define QSPIF_BASIC_PARAM_TABLE_144_READ_INST_BYTE 9
#define QSPIF_BASIC_PARAM_TABLE_114_READ_INST_BYTE 11
#define QSPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE 23
#define QSPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE 15
#define QSPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE 13
// Quad Enable Params
#define QSPIF_BASIC_PARAM_TABLE_QER_BYTE 58
#define QSPIF_BASIC_PARAM_TABLE_444_MODE_EN_SEQ_BYTE 56
#define QSPIF_BASIC_PARAM_TABLE_SOFT_RESET_BYTE 61
#define QSPIF_BASIC_PARAM_TABLE_4BYTE_ADDR_BYTE 63
#define SOFT_RESET_RESET_INST_BITMASK 0b001000
#define SOFT_RESET_ENABLE_AND_RESET_INST_BITMASK 0b010000
// 4-Byte Addressing Support Bitmasks
#define FOURBYTE_ADDR_B7_BITMASK 0b00000001
#define FOURBYTE_ADDR_B7_WREN_BITMASK 0b00000010
#define FOURBYTE_ADDR_EXT_ADDR_REG_BITMASK 0b00000100
#define FOURBYTE_ADDR_BANK_REG_BITMASK 0b00001000
#define FOURBYTE_ADDR_CONF_REG_BITMASK 0b00010000
#define FOURBYTE_ADDR_INSTS_BITMASK 0b00100000
#define FOURBYTE_ADDR_ALWAYS_BITMASK 0b01000000
#define IS_MEM_READY_MAX_RETRIES 10000
// General QSPI instructions
#define QSPIF_INST_WSR1 0x01 // Write status register 1
#define QSPIF_INST_PROG 0x02 // Page program
#define QSPIF_INST_WRDI 0x04 // Write disable
#define QSPIF_INST_RSR1 0x05 // Read status register 1
#define QSPIF_INST_WREN 0x06 // Write enable
#define QSPIF_INST_RSFDP 0x5A // Read SFDP
#define QSPIF_INST_RDID 0x9F // Read Manufacturer and JDEC Device ID
// Device-specific instructions
#define QSPIF_INST_ULBPR 0x98 // Clear all write-protection bits in the Block-Protection register
#define QSPIF_INST_RDCR 0x15 // Read the two control registers
// Default read/legacy erase instructions
#define QSPIF_INST_READ_DEFAULT 0x03
#define QSPIF_INST_LEGACY_ERASE_DEFAULT (-1)
// Default status register 2 read/write instructions
#define QSPIF_INST_WSR2_DEFAULT QSPI_NO_INST
#define QSPIF_INST_RSR2_DEFAULT 0x35
// Default 4-byte extended addressing register write instruction
#define QSPIF_INST_4BYTE_REG_WRITE_DEFAULT QSPI_NO_INST
// Length of data returned from RDID instruction
#define QSPI_RDID_DATA_LENGTH 3
/* Init function to initialize Different Devices CS static list */
static PinName *generate_initialized_active_qspif_csel_arr();
// Static Members for different devices csel
// _devices_mutex is used to lock csel list - only one QSPIFBlockDeviceAsas instance per csel is allowed
SingletonPtr<PlatformMutex> QSPIFBlockDeviceAsas::_devices_mutex;
int QSPIFBlockDeviceAsas::_number_of_active_qspif_flash_csel = 0;
PinName *QSPIFBlockDeviceAsas::_active_qspif_flash_csel_arr = generate_initialized_active_qspif_csel_arr();
/********* Public API Functions *********/
/****************************************/
QSPIFBlockDeviceAsas::QSPIFBlockDeviceAsas(PinName io0, PinName io1, PinName io2, PinName io3, PinName sclk, PinName csel,
int clock_mode,
int freq)
: _qspi(io0, io1, io2, io3, sclk, csel, clock_mode), _csel(csel), _freq(freq),
_init_ref_count(0),
_is_initialized(false) {
_unique_device_status = add_new_csel_instance(csel);
if (_unique_device_status == 0) {
tr_debug("Adding a new QSPIFBlockDeviceAsas csel: %d", (int)csel);
} else if (_unique_device_status == -1) {
tr_error("QSPIFBlockDeviceAsas with the same csel(%d) already exists", (int)csel);
} else {
tr_error("Too many different QSPIFBlockDeviceAsas devices - max allowed: %d", QSPIF_MAX_ACTIVE_FLASH_DEVICES);
}
// Initialize parameters
_sfdp_info.bptbl.legacy_erase_instruction = QSPIF_INST_LEGACY_ERASE_DEFAULT;
_sfdp_info.bptbl.device_size_bytes = 0;
_sfdp_info.smptbl.regions_min_common_erase_size = 0;
_sfdp_info.smptbl.region_cnt = 1;
_sfdp_info.smptbl.region_erase_types_bitfld[0] = SFDP_ERASE_BITMASK_NONE;
// Until proven otherwise, assume no quad enable
_quad_enable_register_idx = QSPIF_NO_QUAD_ENABLE;
_quad_enable_bit = QSPIF_NO_QUAD_ENABLE;
_needs_fast_mode = false;
// Default Bus Setup 1_1_1 with 0 dummy and mode cycles
_inst_width = QSPI_CFG_BUS_SINGLE;
_address_width = QSPI_CFG_BUS_SINGLE;
_address_size = QSPI_CFG_ADDR_SIZE_24;
_alt_size = 0;
_dummy_cycles = 0;
_data_width = QSPI_CFG_BUS_SINGLE;
// Set default read/erase instructions
_read_instruction = QSPIF_INST_READ_DEFAULT;
_num_status_registers = QSPI_DEFAULT_STATUS_REGISTERS;
// Set default status register 2 write/read instructions
_write_status_reg_2_inst = QSPIF_INST_WSR2_DEFAULT;
_read_status_reg_2_inst = QSPIF_INST_RSR2_DEFAULT;
_clear_protection_method = QSPIF_BP_CLEAR_SR;
// Set default 4-byte addressing extension register write instruction
_attempt_4_byte_addressing = true;
_4byte_msb_reg_write_inst = QSPIF_INST_4BYTE_REG_WRITE_DEFAULT;
// Quirk for Cypress S25FS512S
_S25FS512S_quirk = false;
// Quirk for AT25SF128A
_AT25SF128A_quirk = false;
}
int QSPIFBlockDeviceAsas::init() {
int status = A_QSPIF_BD_ERROR_OK;
if (_unique_device_status == 0) {
tr_debug("QSPIFBlockDeviceAsas csel: %d", (int)_csel);
} else if (_unique_device_status == -1) {
tr_error("QSPIFBlockDeviceAsas with the same csel(%d) already exists", (int)_csel);
return A_QSPIF_BD_ERROR_DEVICE_NOT_UNIQUE;
} else {
tr_error("Too many different QSPIFBlockDeviceAsas devices - max allowed: %d", QSPIF_MAX_ACTIVE_FLASH_DEVICES);
return A_QSPIF_BD_ERROR_DEVICE_MAX_EXCEED;
}
_mutex.lock();
// All commands other than Read and RSFDP use default 1-1-1 bus mode (Program/Erase are constrained by flash memory performance more than bus performance)
if (QSPI_STATUS_OK != _qspi.configure_format(QSPI_CFG_BUS_SINGLE, QSPI_CFG_BUS_SINGLE, _address_size, QSPI_CFG_BUS_SINGLE, 0, QSPI_CFG_BUS_SINGLE, 0)) {
tr_error("_qspi_configure_format failed");
status = A_QSPIF_BD_ERROR_DEVICE_ERROR;
goto exit_point;
}
if (!_is_initialized) {
_init_ref_count = 0;
}
_init_ref_count++;
if (_init_ref_count != 1) {
goto exit_point;
}
_alt_size = 0;
_dummy_cycles = 0;
if (QSPI_STATUS_OK != _qspi_set_frequency(_freq)) {
tr_error("QSPI Set Frequency Failed");
status = A_QSPIF_BD_ERROR_DEVICE_ERROR;
goto exit_point;
}
// Synchronize Device
if (false == _is_mem_ready()) {
tr_error("Init - _is_mem_ready Failed");
status = A_QSPIF_BD_ERROR_READY_FAILED;
goto exit_point;
}
if (_handle_vendor_quirks() < 0) {
tr_error("Init - Could not read vendor id");
status = A_QSPIF_BD_ERROR_DEVICE_ERROR;
goto exit_point;
}
/**************************** Parse SFDP data ***********************************/
{
_sfdp_info.bptbl.addr = 0x0;
_sfdp_info.bptbl.size = 0;
_sfdp_info.smptbl.addr = 0x0;
_sfdp_info.smptbl.size = 0;
if (sfdp_parse_headers(callback(this, &QSPIFBlockDeviceAsas::_qspi_send_read_sfdp_command), _sfdp_info) < 0) {
tr_error("Init - Parse SFDP Headers Failed");
status = A_QSPIF_BD_ERROR_PARSING_FAILED;
goto exit_point;
}
if (_sfdp_parse_basic_param_table(callback(this, &QSPIFBlockDeviceAsas::_qspi_send_read_sfdp_command),
_sfdp_info)
< 0) {
tr_error("Init - Parse Basic Param Table Failed");
status = A_QSPIF_BD_ERROR_PARSING_FAILED;
goto exit_point;
}
if (sfdp_parse_sector_map_table(callback(this, &QSPIFBlockDeviceAsas::_qspi_send_read_sfdp_command), _sfdp_info) < 0) {
tr_error("Init - Parse Sector Map Table Failed");
status = A_QSPIF_BD_ERROR_PARSING_FAILED;
goto exit_point;
}
}
if (0 != _clear_block_protection()) {
tr_error("Init - clearing block protection failed");
status = A_QSPIF_BD_ERROR_PARSING_FAILED;
goto exit_point;
}
_is_initialized = true;
exit_point:
_mutex.unlock();
return status;
}
int QSPIFBlockDeviceAsas::deinit() {
int result = A_QSPIF_BD_ERROR_OK;
_mutex.lock();
if (!_is_initialized) {
_init_ref_count = 0;
_mutex.unlock();
return result;
}
_init_ref_count--;
if (_init_ref_count) {
_mutex.unlock();
return result;
}
// Disable Device for Writing
qspi_status_t status = _qspi_send_general_command(QSPIF_INST_WRDI, QSPI_NO_ADDRESS_COMMAND, NULL, 0, NULL, 0);
if (status != QSPI_STATUS_OK) {
tr_error("Write Disable failed");
result = A_QSPIF_BD_ERROR_DEVICE_ERROR;
}
_is_initialized = false;
_mutex.unlock();
if (_unique_device_status == 0) {
remove_csel_instance(_csel);
}
return result;
}
int QSPIFBlockDeviceAsas::read(void *buffer, bd_addr_t addr, bd_size_t size) {
int status = A_QSPIF_BD_ERROR_OK;
// For Some reason the quad read doesn't work well in my device (Arduino Giga WiFi with AT25SF128A Flash)
// So I used conventional reading to help me in using WiFi as it depends on Firmware stored in Flash
// tr_debug("Read Inst: 0x%xh", _read_instruction);
// _mutex.lock();
// if (QSPI_STATUS_OK != _qspi_send_read_command(_read_instruction, buffer, addr, size)) {
// tr_error("Read Command failed");
// status = A_QSPIF_BD_ERROR_DEVICE_ERROR;
// }
// _mutex.unlock();
// Serial.print("Address ");
// Serial.println(addr);
// A trivial solution for now
// Todo: Enable Dual reading because it's fast, I guess....
size_t sz = size;
char *bf = reinterpret_cast<char *>(buffer);
int address = addr;
return _qspi.read(0x03, -1, address, bf, &sz);
return status;
}
int QSPIFBlockDeviceAsas::program(const void *buffer, bd_addr_t addr, bd_size_t size) {
qspi_status_t result = QSPI_STATUS_OK;
bool program_failed = false;
int status = A_QSPIF_BD_ERROR_OK;
uint32_t offset = 0;
uint32_t chunk = 0;
bd_size_t written_bytes = 0;
tr_debug("Program - Buff: %p, addr: %llu, size: %llu", buffer, addr, size);
while (size > 0) {
// Write on _page_size_bytes boundaries (Default 256 bytes a page)
offset = addr % _page_size_bytes;
chunk = (offset + size < _page_size_bytes) ? size : (_page_size_bytes - offset);
written_bytes = chunk;
_mutex.lock();
//Send WREN
if (_set_write_enable() != 0) {
tr_error("Write Enabe failed");
program_failed = true;
status = A_QSPIF_BD_ERROR_WREN_FAILED;
goto exit_point;
}
result = _qspi_send_program_command(QSPIF_INST_PROG, buffer, addr, &written_bytes);
if ((result != QSPI_STATUS_OK) || (chunk != written_bytes)) {
tr_error("Write failed");
program_failed = true;
status = A_QSPIF_BD_ERROR_DEVICE_ERROR;
goto exit_point;
}
buffer = static_cast<const uint8_t *>(buffer) + chunk;
addr += chunk;
size -= chunk;
if (false == _is_mem_ready()) {
tr_error("Device not ready after write, failed");
program_failed = true;
status = A_QSPIF_BD_ERROR_READY_FAILED;
goto exit_point;
}
_mutex.unlock();
}
exit_point:
if (program_failed) {
_mutex.unlock();
}
return status;
}
int QSPIFBlockDeviceAsas::erase(bd_addr_t addr, bd_size_t size) {
int type = 0;
qspi_inst_t cur_erase_inst = QSPI_NO_INST;
bool erase_failed = false;
int status = A_QSPIF_BD_ERROR_OK;
// Find region of erased address
int region = sfdp_find_addr_region(addr, _sfdp_info);
// Erase Types of selected region
uint8_t bitfield = _sfdp_info.smptbl.region_erase_types_bitfld[region];
tr_debug("Erase - addr: %llu, size: %llu", addr, size);
if ((addr + size) > _sfdp_info.bptbl.device_size_bytes) {
tr_error("Erase exceeds flash device size");
return A_QSPIF_BD_ERROR_INVALID_ERASE_PARAMS;
}
if (((addr % get_erase_size(addr)) != 0) || (((addr + size) % get_erase_size(addr + size - 1)) != 0)) {
tr_error("Invalid erase - unaligned address and size");
return A_QSPIF_BD_ERROR_INVALID_ERASE_PARAMS;
}
// For each iteration erase the largest section supported by current region
while (size > 0) {
unsigned int eu_size;
if (_sfdp_info.bptbl.legacy_erase_instruction == QSPI_NO_INST) {
// Iterate to find next largest erase type that is a) supported by region, and b) smaller than size.
// Find the matching instruction and erase size chunk for that type.
type = sfdp_iterate_next_largest_erase_type(bitfield, size, addr,
region,
_sfdp_info.smptbl);
cur_erase_inst = _sfdp_info.smptbl.erase_type_inst_arr[type];
eu_size = _sfdp_info.smptbl.erase_type_size_arr[type];
} else {
// Must use legacy 4k erase instruction
cur_erase_inst = _sfdp_info.bptbl.legacy_erase_instruction;
eu_size = QSPIF_DEFAULT_SE_SIZE;
}
if (addr % eu_size != 0 || addr + size < eu_size) {
// Should not happen if the erase table parsing
// and alignment checks were performed correctly
tr_error("internal error: address %llu not aligned to erase size %u (type %d)",
addr, eu_size, type);
}
tr_debug("Erase - addr: %llu, size:%llu, Inst: 0x%xh, erase size: %u",
addr, size, cur_erase_inst, eu_size);
tr_debug("Erase - Region: %d, Type:%d ",
region, type);
_mutex.lock();
if (_set_write_enable() != 0) {
tr_error("QSPI Erase Device not ready - failed");
erase_failed = true;
status = A_QSPIF_BD_ERROR_WREN_FAILED;
goto exit_point;
}
if (QSPI_STATUS_OK != _qspi_send_erase_command(cur_erase_inst, addr, size)) {
tr_error("QSPI Erase command failed!");
erase_failed = true;
status = A_QSPIF_BD_ERROR_DEVICE_ERROR;
goto exit_point;
}
addr += eu_size;
size -= eu_size;
if ((size > 0) && (addr > _sfdp_info.smptbl.region_high_boundary[region])) {
// erase crossed to next region
region++;
bitfield = _sfdp_info.smptbl.region_erase_types_bitfld[region];
}
if (false == _is_mem_ready()) {
tr_error("QSPI After Erase Device not ready - failed");
erase_failed = true;
status = A_QSPIF_BD_ERROR_READY_FAILED;
goto exit_point;
}
_mutex.unlock();
}
exit_point:
if (erase_failed) {
_mutex.unlock();
}
return status;
}
bd_size_t QSPIFBlockDeviceAsas::get_read_size() const {
// Return minimum read size in bytes for the device
return MBED_CONF_QSPIF_QSPI_MIN_READ_SIZE;
}
bd_size_t QSPIFBlockDeviceAsas::get_program_size() const {
// Return minimum program/write size in bytes for the device
return MBED_CONF_QSPIF_QSPI_MIN_PROG_SIZE;
}
bd_size_t QSPIFBlockDeviceAsas::get_erase_size() const {
// return minimal erase size supported by all regions (0 if none exists)
return _sfdp_info.smptbl.regions_min_common_erase_size;
}
const char *QSPIFBlockDeviceAsas::get_type() const {
return "QSPIF";
}
// Find minimal erase size supported by the region to which the address belongs to
bd_size_t QSPIFBlockDeviceAsas::get_erase_size(bd_addr_t addr) const {
// If the legacy erase instruction is in use, the erase size is uniformly 4k
if (_sfdp_info.bptbl.legacy_erase_instruction != QSPI_NO_INST) {
return QSPIF_DEFAULT_SE_SIZE;
}
// Find region of current address
int region = sfdp_find_addr_region(addr, _sfdp_info);
int min_region_erase_size = _sfdp_info.smptbl.regions_min_common_erase_size;
int8_t type_mask = SFDP_ERASE_BITMASK_TYPE1;
int i_ind = 0;
if (region != -1) {
type_mask = 0x01;
for (i_ind = 0; i_ind < 4; i_ind++) {
// loop through erase types bitfield supported by region
if (_sfdp_info.smptbl.region_erase_types_bitfld[region] & type_mask) {
min_region_erase_size = _sfdp_info.smptbl.erase_type_size_arr[i_ind];
break;
}
type_mask = type_mask << 1;
}
if (i_ind == 4) {
tr_error("No erase type was found for region addr");
}
}
return (bd_size_t)min_region_erase_size;
}
bd_size_t QSPIFBlockDeviceAsas::size() const {
return _sfdp_info.bptbl.device_size_bytes;
}
int QSPIFBlockDeviceAsas::get_erase_value() const {
return 0xFF;
}
/********************************/
/* Different Device Csel Mgmt */
/********************************/
static PinName *generate_initialized_active_qspif_csel_arr() {
PinName *init_arr = new PinName[QSPIF_MAX_ACTIVE_FLASH_DEVICES];
for (int i_ind = 0; i_ind < QSPIF_MAX_ACTIVE_FLASH_DEVICES; i_ind++) {
init_arr[i_ind] = NC;
}
return init_arr;
}
int QSPIFBlockDeviceAsas::add_new_csel_instance(PinName csel) {
int status = 0;
_devices_mutex->lock();
if (_number_of_active_qspif_flash_csel >= QSPIF_MAX_ACTIVE_FLASH_DEVICES) {
status = -2;
goto exit_point;
}
// verify the device is unique(no identical csel already exists)
for (int i_ind = 0; i_ind < QSPIF_MAX_ACTIVE_FLASH_DEVICES; i_ind++) {
if (_active_qspif_flash_csel_arr[i_ind] == csel) {
status = -1;
goto exit_point;
}
}
// Insert new csel into existing device list
for (int i_ind = 0; i_ind < QSPIF_MAX_ACTIVE_FLASH_DEVICES; i_ind++) {
if (_active_qspif_flash_csel_arr[i_ind] == NC) {
_active_qspif_flash_csel_arr[i_ind] = csel;
break;
}
}
_number_of_active_qspif_flash_csel++;
exit_point:
_devices_mutex->unlock();
return status;
}
int QSPIFBlockDeviceAsas::remove_csel_instance(PinName csel) {
int status = -1;
_devices_mutex->lock();
// remove the csel from existing device list
for (int i_ind = 0; i_ind < QSPIF_MAX_ACTIVE_FLASH_DEVICES; i_ind++) {
if (_active_qspif_flash_csel_arr[i_ind] == csel) {
_active_qspif_flash_csel_arr[i_ind] = NC;
if (_number_of_active_qspif_flash_csel > 0) {
_number_of_active_qspif_flash_csel--;
}
status = 0;
break;
}
}
_devices_mutex->unlock();
return status;
}
/*********************************************************/
/********** SFDP Parsing and Detection Functions *********/
/*********************************************************/
int QSPIFBlockDeviceAsas::_sfdp_parse_basic_param_table(Callback<int(mbed::bd_addr_t, mbed::sfdp_cmd_addr_size_t, uint8_t, uint8_t, void *, bd_size_t)> sfdp_reader,
sfdp_hdr_info &sfdp_info) {
uint8_t param_table[SFDP_BASIC_PARAMS_TBL_SIZE]; /* Up To 20 DWORDS = 80 Bytes */
int status = -1;
if (_AT25SF128A_quirk) {
/*
* Skip registers from address 0x54 to 0x5F.
* Reading those registers will corrupt future reads.
*/
status = sfdp_reader(
sfdp_info.bptbl.addr,
SFDP_READ_CMD_ADDR_TYPE,
SFDP_READ_CMD_INST,
SFDP_READ_CMD_DUMMY_CYCLES,
param_table,
0x24 // from 0x30 to 0x53
);
if (status != QSPI_STATUS_OK) {
tr_error("Init - Read SFDP First Table Failed");
return -1;
}
status = sfdp_reader(
0x60,
SFDP_READ_CMD_ADDR_TYPE,
SFDP_READ_CMD_INST,
SFDP_READ_CMD_DUMMY_CYCLES,
¶m_table[0x30],
0x0C // from 0x60 to 0x6B
);
} else {
status = sfdp_reader(
sfdp_info.bptbl.addr,
SFDP_READ_CMD_ADDR_TYPE,
SFDP_READ_CMD_INST,
SFDP_READ_CMD_DUMMY_CYCLES,
param_table,
sfdp_info.bptbl.size);
}
if (status != QSPI_STATUS_OK) {
tr_error("Init - Read SFDP First Table Failed");
return -1;
}
// Check that density is not greater than 4 gigabits (i.e. that addressing beyond 4 bytes is not required)
if (sfdp_detect_addressability(param_table, _sfdp_info.bptbl) < 0) {
tr_error("Verify 4byte addressing failed");
return -1;
}
if (sfdp_detect_device_density(param_table, _sfdp_info.bptbl) < 0) {
tr_error("Detecting device density failed");
return -1;
}
// Set Page Size (QSPI write must be done on Page limits)
_page_size_bytes = sfdp_detect_page_size(param_table, sfdp_info.bptbl.size);
if (_sfdp_detect_reset_protocol_and_reset(param_table) != A_QSPIF_BD_ERROR_OK) {
tr_error("Init - Detecting reset protocol/resetting failed");
return -1;
}
// Detect and Set Erase Types
bool shouldSetQuadEnable = false;
bool is_qpi_mode = false;
if (sfdp_detect_erase_types_inst_and_size(param_table, _sfdp_info) < 0) {
tr_error("Init - Detecting erase types instructions/sizes failed");
return -1;
}
// Detect and Set fastest Bus mode (default 1-1-1)
_sfdp_detect_best_bus_read_mode(param_table, sfdp_info.bptbl.size, shouldSetQuadEnable, is_qpi_mode);
if (true == shouldSetQuadEnable) {
if (_needs_fast_mode) {
_enable_fast_mode();
}
// Set Quad Enable and QPI Bus modes if Supported
tr_debug("Init - Setting Quad Enable");
if (0 != _sfdp_set_quad_enabled(param_table)) {
tr_error("Device supports Quad bus, but Quad Enable Failed");
return -1;
}
if (true == is_qpi_mode) {
tr_debug("Init - Setting QPI mode");
_sfdp_set_qpi_enabled(param_table);
}
}
#ifndef TARGET_NORDIC
// 4 byte addressing is not currently supported with the Nordic QSPI controller
if (_attempt_4_byte_addressing) {
if (_sfdp_detect_and_enable_4byte_addressing(param_table, sfdp_info.bptbl.size) != A_QSPIF_BD_ERROR_OK) {
tr_error("Init - Detecting/enabling 4-byte addressing failed");
return -1;
}
}
#endif
if (false == _is_mem_ready()) {
tr_error("Init - _is_mem_ready Failed");
return -1;
}
return 0;
}
int QSPIFBlockDeviceAsas::_sfdp_set_quad_enabled(uint8_t *basic_param_table_ptr) {
uint8_t status_reg_setup[QSPI_MAX_STATUS_REGISTERS] = { 0 };
uint8_t status_regs[QSPI_MAX_STATUS_REGISTERS] = { 0 };
uint8_t qer_value = 0;
if (_AT25SF128A_quirk) {
qer_value = 1;
} else {
// QUAD Enable procedure is specified by 3 bits
qer_value = (basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_QER_BYTE] & 0x70) >> 4;
}
switch (qer_value) {
case 0:
tr_debug("Device Does not Have a QE Bit, continue based on Read Inst");
return 0;
case 1:
case 4:
// Bit 1 of Status Reg 2
_quad_enable_register_idx = 1;
_quad_enable_bit = 1;
tr_debug("Setting QE Bit, Bit 1 of Status Reg 2");
break;
case 2:
// Bit 6 of Status Reg 1
_quad_enable_register_idx = 0;
_quad_enable_bit = 6;
tr_debug("Setting QE Bit, Bit 6 of Status Reg 1");
break;
case 3:
// Bit 7 of Status Reg 1
_quad_enable_register_idx = 0;
_quad_enable_bit = 7;
_write_status_reg_2_inst = 0x3E;
_read_status_reg_2_inst = 0x3F;
tr_debug("Setting QE Bit, Bit 7 of Status Reg 1");
break;
case 5:
// Bit 1 of status Reg 2
_quad_enable_register_idx = 1;
_quad_enable_bit = 1;
tr_debug("Setting QE Bit, Bit 1 of Status Reg 2");
break;
default:
tr_warning("Unsupported QER configuration");
return 0;
}
if (_quad_enable_register_idx != QSPIF_NO_QUAD_ENABLE && _quad_enable_bit != QSPIF_NO_QUAD_ENABLE) {
status_reg_setup[_quad_enable_register_idx] = 1 << _quad_enable_bit;
}
// Read existing status register values
_qspi_read_status_registers(status_regs);
// Set Bits for Quad Enable
for (int i = 0; i < QSPI_MAX_STATUS_REGISTERS; i++) {
status_regs[i] |= status_reg_setup[i];
}
// Write new Status Register Setup
_qspi_write_status_registers(status_regs);
if (false == _is_mem_ready()) {
tr_error("Device not ready after write, failed");
return -1;
}
// For Debug
memset(status_regs, 0, QSPI_MAX_STATUS_REGISTERS);
_qspi_read_status_registers(status_regs);
if (((status_regs[0] & status_reg_setup[0]) | (status_regs[1] & status_reg_setup[1])) == 0) {
tr_error("Status register not set correctly");
return -1;
}
return 0;
}
int QSPIFBlockDeviceAsas::_sfdp_set_qpi_enabled(uint8_t *basic_param_table_ptr) {
uint8_t config_reg[1];
// QPI 4-4-4 Enable Procedure is specified in 5 Bits
uint8_t en_seq_444_value = (((basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_444_MODE_EN_SEQ_BYTE] & 0xF0) >> 4) | ((basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_444_MODE_EN_SEQ_BYTE + 1] & 0x01) << 4));
switch (en_seq_444_value) {
case 1:
case 2:
tr_debug("_sfdp_set_qpi_enabled - send command 38h");
if (QSPI_STATUS_OK != _qspi_send_general_command(0x38, QSPI_NO_ADDRESS_COMMAND, NULL, 0, NULL, 0)) {
tr_error("_sfdp_set_qpi_enabled - send command 38h Failed");
}
break;
case 4:
tr_debug("_sfdp_set_qpi_enabled - send command 35h");
if (QSPI_STATUS_OK != _qspi_send_general_command(0x35, QSPI_NO_ADDRESS_COMMAND, NULL, 0, NULL, 0)) {
tr_error("_sfdp_set_qpi_enabled - send command 35h Failed");
}
break;
case 8:
tr_debug("_sfdp_set_qpi_enabled - set config bit 6 and send command 71h");
if (QSPI_STATUS_OK != _qspi_send_general_command(0x65, 0x800003, NULL, 0, (char *)config_reg, 1)) {
tr_error("_sfdp_set_qpi_enabled - set config bit 6 command 65h Failed");
}
config_reg[0] |= 0x40; //Set Bit 6
if (QSPI_STATUS_OK != _qspi_send_general_command(0x71, 0x800003, NULL, 0, (char *)config_reg, 1)) {
tr_error("_sfdp_set_qpi_enabled - send command 71h Failed");
}
break;
case 16:
tr_debug("_sfdp_set_qpi_enabled - reset config bits 0-7 and send command 61h");
if (QSPI_STATUS_OK != _qspi_send_general_command(0x65, QSPI_NO_ADDRESS_COMMAND, NULL, 0, (char *)config_reg, 1)) {
tr_error("_sfdp_set_qpi_enabled - send command 65h Failed");
}
config_reg[0] &= 0x7F; //Reset Bit 7 of CR
if (QSPI_STATUS_OK != _qspi_send_general_command(0x61, QSPI_NO_ADDRESS_COMMAND, NULL, 0, (char *)config_reg, 1)) {
tr_error("_sfdp_set_qpi_enabled - send command 61 Failed");
}
break;
default:
tr_warning("_sfdp_set_qpi_enabled - Unsupported En Seq 444 configuration");
break;
}
return 0;
}
int QSPIFBlockDeviceAsas::_sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table_ptr, int basic_param_table_size,
bool &set_quad_enable, bool &is_qpi_mode) {
set_quad_enable = false;
is_qpi_mode = false;
uint8_t examined_byte;
do { // compound statement is the loop body
examined_byte = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_FAST_READ_SUPPORT_BYTE];
if (examined_byte & 0x20) {
// Fast Read 1-4-4 Supported
_read_instruction = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_144_READ_INST_BYTE];
set_quad_enable = true;
_dummy_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_144_READ_INST_BYTE - 1] & 0x1F;
uint8_t mode_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_144_READ_INST_BYTE - 1] >> 5;
_alt_size = mode_cycles * 4;
_address_width = QSPI_CFG_BUS_QUAD;
_data_width = QSPI_CFG_BUS_QUAD;
tr_debug("Read Bus Mode set to 1-4-4, Instruction: 0x%xh", _read_instruction);
break;
}
// QPI is checked as second option.
if (basic_param_table_size > QSPIF_BASIC_PARAM_TABLE_QPI_READ_SUPPORT_BYTE) {
examined_byte = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_QPI_READ_SUPPORT_BYTE];
if (examined_byte & 0x10) {
// QPI 4-4-4 Supported
_read_instruction = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_444_READ_INST_BYTE];
set_quad_enable = true;
is_qpi_mode = true;
_dummy_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_444_READ_INST_BYTE - 1] & 0x1F;
uint8_t mode_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_444_READ_INST_BYTE - 1] >> 5;
_alt_size = mode_cycles * 4;
tr_debug("Read Bus Mode set to 4-4-4, Instruction: 0x%xh", _read_instruction);
_address_width = QSPI_CFG_BUS_QUAD;
_data_width = QSPI_CFG_BUS_QUAD;
break;
}
}
examined_byte = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_FAST_READ_SUPPORT_BYTE];
if (examined_byte & 0x40) {
// Fast Read 1-1-4 Supported
_read_instruction = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_114_READ_INST_BYTE];
set_quad_enable = true;
_dummy_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_114_READ_INST_BYTE - 1] & 0x1F;
uint8_t mode_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_114_READ_INST_BYTE - 1] >> 5;
_alt_size = mode_cycles;
_data_width = QSPI_CFG_BUS_QUAD;
tr_debug("Read Bus Mode set to 1-1-4, Instruction: 0x%xh", _read_instruction);
break;
}
examined_byte = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_QPI_READ_SUPPORT_BYTE];
if (examined_byte & 0x01) {
// Fast Read 2-2-2 Supported
_read_instruction = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE];
_dummy_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE - 1] & 0x1F;
uint8_t mode_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE - 1] >> 5;
_alt_size = mode_cycles * 2;
_address_width = QSPI_CFG_BUS_DUAL;
_data_width = QSPI_CFG_BUS_DUAL;
tr_debug("Read Bus Mode set to 2-2-2, Instruction: 0x%xh", _read_instruction);
break;
}
examined_byte = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_FAST_READ_SUPPORT_BYTE];
if (examined_byte & 0x10) {
// Fast Read 1-2-2 Supported
_read_instruction = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE];
_dummy_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE - 1] & 0x1F;
uint8_t mode_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE - 1] >> 5;
_alt_size = mode_cycles * 2;
_address_width = QSPI_CFG_BUS_DUAL;
_data_width = QSPI_CFG_BUS_DUAL;
tr_debug("Read Bus Mode set to 1-2-2, Instruction: 0x%xh", _read_instruction);
break;
}
if (examined_byte & 0x01) {
// Fast Read 1-1-2 Supported
_read_instruction = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE];
_dummy_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE - 1] & 0x1F;
uint8_t mode_cycles = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE - 1] >> 5;
_alt_size = mode_cycles;
_data_width = QSPI_CFG_BUS_DUAL;
tr_debug("Read Bus Mode set to 1-1-2, Instruction: 0x%xh", _read_instruction);
break;
}
_read_instruction = QSPIF_INST_READ_DEFAULT;
tr_debug("Read Bus Mode set to 1-1-1, Instruction: 0x%xh", _read_instruction);
} while (false);
return 0;
}
int QSPIFBlockDeviceAsas::_sfdp_detect_and_enable_4byte_addressing(uint8_t *basic_param_table_ptr, int basic_param_table_size) {
int status = A_QSPIF_BD_ERROR_OK;
qspi_status_t qspi_status = QSPI_STATUS_OK;
// Always enable 4-byte addressing if possible
if (basic_param_table_size > QSPIF_BASIC_PARAM_TABLE_4BYTE_ADDR_BYTE) {
uint8_t examined_byte = basic_param_table_ptr[QSPIF_BASIC_PARAM_TABLE_4BYTE_ADDR_BYTE];
if (examined_byte & FOURBYTE_ADDR_ALWAYS_BITMASK) {
// No need to do anything if 4-byte addressing is always enabled
_address_size = QSPI_CFG_ADDR_SIZE_32;
} else if (examined_byte & FOURBYTE_ADDR_B7_BITMASK) {
// Issue instruction B7h to enable 4-byte addressing
qspi_status = _qspi_send_general_command(0xB7, QSPI_NO_ADDRESS_COMMAND, NULL, 0, NULL, 0);
status = (qspi_status == QSPI_STATUS_OK) ? A_QSPIF_BD_ERROR_OK : A_QSPIF_BD_ERROR_PARSING_FAILED;
if (status == A_QSPIF_BD_ERROR_OK) {
_address_size = QSPI_CFG_ADDR_SIZE_32;
}
} else if (examined_byte & FOURBYTE_ADDR_B7_WREN_BITMASK) {
// Issue WREN and then instruction B7h to enable 4-byte addressing
if (_set_write_enable() == 0) {
qspi_status = _qspi_send_general_command(0xB7, QSPI_NO_ADDRESS_COMMAND, NULL, 0, NULL, 0);
status = (qspi_status == QSPI_STATUS_OK) ? A_QSPIF_BD_ERROR_OK : A_QSPIF_BD_ERROR_PARSING_FAILED;
if (status == A_QSPIF_BD_ERROR_OK) {
_address_size = QSPI_CFG_ADDR_SIZE_32;
}
} else {
tr_error("Write enable failed");
status = A_QSPIF_BD_ERROR_WREN_FAILED;
}
} else if (examined_byte & FOURBYTE_ADDR_CONF_REG_BITMASK) {
// Write 1 to bit 0 of a configuration register to enable 4-byte addressing
// Write to register with instruction B1h, read from register with instruction B5h
uint8_t conf_register = 0;
qspi_status = _qspi_send_general_command(0xB5, QSPI_NO_ADDRESS_COMMAND, NULL, 0, (char *)&conf_register, 1);
status = (qspi_status == QSPI_STATUS_OK) ? A_QSPIF_BD_ERROR_OK : A_QSPIF_BD_ERROR_PARSING_FAILED;
if (status == A_QSPIF_BD_ERROR_OK) {
conf_register |= 0b00000001;
if (_set_write_enable() == 0) {
qspi_status_t qspi_status = _qspi_send_general_command(0xB1, QSPI_NO_ADDRESS_COMMAND, (char *)&conf_register, 1, NULL, 0);
status = (qspi_status == QSPI_STATUS_OK) ? A_QSPIF_BD_ERROR_OK : A_QSPIF_BD_ERROR_PARSING_FAILED;
if (status == A_QSPIF_BD_ERROR_OK) {
_address_size = QSPI_CFG_ADDR_SIZE_32;
}
} else {
tr_error("Write enable failed");
status = A_QSPIF_BD_ERROR_WREN_FAILED;