1414 * limitations under the License.
1515 */
1616
17+ #include " drivers/internal/SFDP.h"
18+ #include " platform/Callback.h"
1719#include " QSPIFBlockDevice.h"
1820#include < string.h>
1921#include " rtos/ThisThread.h"
@@ -48,8 +50,6 @@ using namespace mbed;
4850/* SFDP Header Parsing */
4951/* **********************/
5052#define QSPIF_RSFDP_DUMMY_CYCLES 8
51- #define QSPIF_SFDP_HEADER_SIZE 8
52- #define QSPIF_PARAM_HEADER_SIZE 8
5353
5454/* Basic Parameters Table Parsing */
5555/* *********************************/
@@ -204,10 +204,8 @@ int QSPIFBlockDevice::init()
204204 }
205205
206206 int status = QSPIF_BD_ERROR_OK;
207- uint32_t basic_table_addr = 0 ;
208- size_t basic_table_size = 0 ;
209- uint32_t sector_map_table_addr = 0 ;
210- size_t sector_map_table_size = 0 ;
207+ sfdp_hdr_info hdr_info;
208+ memset (&hdr_info, 0 , sizeof hdr_info);
211209
212210 _mutex.lock ();
213211
@@ -251,14 +249,14 @@ int QSPIFBlockDevice::init()
251249 }
252250
253251 /* *************************** Parse SFDP Header ***********************************/
254- if (0 != _sfdp_parse_sfdp_headers (basic_table_addr, basic_table_size, sector_map_table_addr, sector_map_table_size )) {
252+ if (0 != _sfdp_parse_sfdp_headers (hdr_info )) {
255253 tr_error (" Init - Parse SFDP Headers Failed" );
256254 status = QSPIF_BD_ERROR_PARSING_FAILED;
257255 goto exit_point;
258256 }
259257
260258 /* *************************** Parse Basic Parameters Table ***********************************/
261- if (0 != _sfdp_parse_basic_param_table (basic_table_addr, basic_table_size)) {
259+ if (0 != _sfdp_parse_basic_param_table (hdr_info. basic_table_addr , hdr_info. basic_table_size )) {
262260 tr_error (" Init - Parse Basic Param Table Failed" );
263261 status = QSPIF_BD_ERROR_PARSING_FAILED;
264262 goto exit_point;
@@ -269,10 +267,10 @@ int QSPIFBlockDevice::init()
269267 _device_size_bytes; // If there's no region map, we have a single region sized the entire device size
270268 _region_high_boundary[0 ] = _device_size_bytes - 1 ;
271269
272- if ((sector_map_table_addr != 0 ) && (0 != sector_map_table_size)) {
273- tr_debug (" Init - Parsing Sector Map Table - addr: 0x%lxh, Size: %d" , sector_map_table_addr,
274- sector_map_table_size);
275- if (0 != _sfdp_parse_sector_map_table (sector_map_table_addr, sector_map_table_size)) {
270+ if ((hdr_info. sector_map_table_addr != 0 ) && (0 != hdr_info. sector_map_table_size )) {
271+ tr_debug (" Init - Parsing Sector Map Table - addr: 0x%lxh, Size: %d" , hdr_info. sector_map_table_addr ,
272+ hdr_info. sector_map_table_size );
273+ if (0 != _sfdp_parse_sector_map_table (hdr_info. sector_map_table_addr , hdr_info. sector_map_table_size )) {
276274 tr_error (" Init - Parse Sector Map Table Failed" );
277275 status = QSPIF_BD_ERROR_PARSING_FAILED;
278276 goto exit_point;
@@ -629,75 +627,16 @@ int QSPIFBlockDevice::remove_csel_instance(PinName csel)
629627/* ********************************************************/
630628/* ********* SFDP Parsing and Detection Functions *********/
631629/* ********************************************************/
632- int QSPIFBlockDevice::_sfdp_parse_sfdp_headers (uint32_t &basic_table_addr, size_t &basic_table_size,
633- uint32_t §or_map_table_addr, size_t §or_map_table_size)
630+ int QSPIFBlockDevice::_sfdp_parse_sfdp_headers (mbed::sfdp_hdr_info &hdr_info)
634631{
635- uint8_t sfdp_header[QSPIF_SFDP_HEADER_SIZE];
636- uint8_t param_header[QSPIF_PARAM_HEADER_SIZE];
637- size_t data_length = QSPIF_SFDP_HEADER_SIZE;
638- bd_addr_t addr = 0x0 ;
639-
640- qspi_status_t status = _qspi_send_read_sfdp_command (addr, (char *) sfdp_header, data_length);
641- if (status != QSPI_STATUS_OK) {
642- tr_error (" Init - Read SFDP Failed" );
643- return -1 ;
644- }
645-
646- // Verify SFDP signature for sanity
647- // Also check that major/minor version is acceptable
648- if (!(memcmp (&sfdp_header[0 ], " SFDP" , 4 ) == 0 && sfdp_header[5 ] == 1 )) {
649- tr_error (" Init - Verification of SFDP signature and version failed" );
650- return -1 ;
651- } else {
652- tr_debug (" Init - Verification of SFDP signature and version succeeded" );
653- }
654-
655- // Discover Number of Parameter Headers
656- int number_of_param_headers = (int )(sfdp_header[6 ]) + 1 ;
657- tr_debug (" Number of Param Headers: %d" , number_of_param_headers);
658-
659-
660- addr += QSPIF_SFDP_HEADER_SIZE;
661- data_length = QSPIF_PARAM_HEADER_SIZE;
662-
663- // Loop over Param Headers and parse them (currently supports Basic Param Table and Sector Region Map Table)
664- for (int i_ind = 0 ; i_ind < number_of_param_headers; i_ind++) {
665- status = _qspi_send_read_sfdp_command (addr, (char *) param_header, data_length);
666- if (status != QSPI_STATUS_OK) {
667- tr_error (" Init - Read Param Table %d Failed" , i_ind + 1 );
668- return -1 ;
669- }
670-
671- // The SFDP spec indicates the standard table is always at offset 0
672- // in the parameter headers, we check just to be safe
673- if (param_header[2 ] != 1 ) {
674- tr_error (" Param Table %d - Major Version should be 1!" , i_ind + 1 );
675- return -1 ;
676- }
677-
678- if ((param_header[0 ] == 0 ) && (param_header[7 ] == 0xFF )) {
679- // Found Basic Params Table: LSB=0x00, MSB=0xFF
680- tr_debug (" Found Basic Param Table at Table: %d" , i_ind + 1 );
681- basic_table_addr = ((param_header[6 ] << 16 ) | (param_header[5 ] << 8 ) | (param_header[4 ]));
682- // Supporting up to 64 Bytes Table (16 DWORDS)
683- basic_table_size = ((param_header[3 ] * 4 ) < SFDP_DEFAULT_BASIC_PARAMS_TABLE_SIZE_BYTES) ? (param_header[3 ] * 4 ) : 64 ;
684- } else if ((param_header[0 ] == 81 ) && (param_header[7 ] == 0xFF )) {
685- // Found Sector Map Table: LSB=0x81, MSB=0xFF
686- tr_debug (" Found Sector Map Table at Table: %d" , i_ind + 1 );
687- sector_map_table_addr = ((param_header[6 ] << 16 ) | (param_header[5 ] << 8 ) | (param_header[4 ]));
688- sector_map_table_size = param_header[3 ] * 4 ;
689- }
690- addr += QSPIF_PARAM_HEADER_SIZE;
691- }
692-
693- return 0 ;
632+ return sfdp_parse_headers (callback (this , &QSPIFBlockDevice::_qspi_send_read_sfdp_command), hdr_info);
694633}
695634
696635int QSPIFBlockDevice::_sfdp_parse_basic_param_table (uint32_t basic_table_addr, size_t basic_table_size)
697636{
698637 uint8_t param_table[SFDP_DEFAULT_BASIC_PARAMS_TABLE_SIZE_BYTES]; /* Up To 16 DWORDS = 64 Bytes */
699638
700- qspi_status_t status = _qspi_send_read_sfdp_command (basic_table_addr, (char *) param_table, basic_table_size);
639+ int status = _qspi_send_read_sfdp_command (basic_table_addr, (char *)param_table, basic_table_size);
701640 if (status != QSPI_STATUS_OK) {
702641 tr_error (" Init - Read SFDP First Table Failed" );
703642 return -1 ;
@@ -1178,7 +1117,7 @@ int QSPIFBlockDevice::_sfdp_parse_sector_map_table(uint32_t sector_map_table_add
11781117 // Default set to all type bits 1-4 are common
11791118 int min_common_erase_type_bits = ERASE_BITMASK_ALL;
11801119
1181- qspi_status_t status = _qspi_send_read_sfdp_command (sector_map_table_addr, (char *) sector_map_table, sector_map_table_size);
1120+ int status = _qspi_send_read_sfdp_command (sector_map_table_addr, (char *)sector_map_table, sector_map_table_size);
11821121 if (status != QSPI_STATUS_OK) {
11831122 tr_error (" Init - Read SFDP First Table Failed" );
11841123 return -1 ;
@@ -1621,7 +1560,7 @@ qspi_status_t QSPIFBlockDevice::_qspi_send_general_command(qspi_inst_t instructi
16211560 return QSPI_STATUS_OK;
16221561}
16231562
1624- qspi_status_t QSPIFBlockDevice::_qspi_send_read_sfdp_command (bd_addr_t addr, void *rx_buffer, bd_size_t rx_length)
1563+ int QSPIFBlockDevice::_qspi_send_read_sfdp_command (bd_addr_t addr, void *rx_buffer, bd_size_t rx_length)
16251564{
16261565 size_t rx_len = rx_length;
16271566
0 commit comments