@@ -387,13 +387,27 @@ static int qspi_write_unprotect(const struct device *dev)
387
387
static int qspi_read_sfdp (const struct device * dev , off_t addr , void * data ,
388
388
size_t size )
389
389
{
390
+ int ret = 0 ;
390
391
struct flash_stm32_qspi_data * dev_data = dev -> data ;
391
392
HAL_StatusTypeDef hal_ret ;
392
393
393
394
__ASSERT (data != NULL , "null destination" );
394
395
395
396
LOG_INF ("Reading SFDP" );
396
397
398
+ #if DT_PROP (DT_NODELABEL (quadspi ), dual_flash ) && defined(QUADSPI_CR_DFM )
399
+ /*
400
+ * In dual flash mode, reading the SFDP table would cause the parameters from both flash
401
+ * memories to be read (first byte read would be the first SFDP byte from the first flash,
402
+ * second byte read would be the first SFDP byte from the second flash, ...). Both flash
403
+ * memories are expected to be identical so to have identical SFDP. Therefore, the dual
404
+ * flash mode is disabled during the reading to obtain the SFDP from a single flash memory
405
+ * only.
406
+ */
407
+ MODIFY_REG (dev_data -> hqspi .Instance -> CR , QUADSPI_CR_DFM , QSPI_DUALFLASH_DISABLE );
408
+ LOG_DBG ("Dual flash mode disabled while reading SFDP" );
409
+ #endif /* dual_flash */
410
+
397
411
QSPI_CommandTypeDef cmd = {
398
412
.Instruction = JESD216_CMD_READ_SFDP ,
399
413
.Address = addr ,
@@ -409,19 +423,27 @@ static int qspi_read_sfdp(const struct device *dev, off_t addr, void *data,
409
423
HAL_QSPI_TIMEOUT_DEFAULT_VALUE );
410
424
if (hal_ret != HAL_OK ) {
411
425
LOG_ERR ("%d: Failed to send SFDP instruction" , hal_ret );
412
- return - EIO ;
426
+ ret = - EIO ;
427
+ goto end ;
413
428
}
414
429
415
430
hal_ret = HAL_QSPI_Receive (& dev_data -> hqspi , (uint8_t * )data ,
416
431
HAL_QSPI_TIMEOUT_DEFAULT_VALUE );
417
432
if (hal_ret != HAL_OK ) {
418
433
LOG_ERR ("%d: Failed to read SFDP" , hal_ret );
419
- return - EIO ;
434
+ ret = - EIO ;
435
+ goto end ;
420
436
}
421
437
422
438
dev_data -> cmd_status = 0 ;
423
439
424
- return 0 ;
440
+ end :
441
+ #if DT_PROP (DT_NODELABEL (quadspi ), dual_flash ) && defined(QUADSPI_CR_DFM )
442
+ /* Re-enable the dual flash mode */
443
+ MODIFY_REG (dev_data -> hqspi .Instance -> CR , QUADSPI_CR_DFM , QSPI_DUALFLASH_ENABLE );
444
+ #endif /* dual_flash */
445
+
446
+ return ret ;
425
447
}
426
448
427
449
static bool qspi_address_is_valid (const struct device * dev , off_t addr ,
@@ -1511,16 +1533,19 @@ static int flash_stm32_qspi_init(const struct device *dev)
1511
1533
/* Give a bit position from 0 to 31 to the HAL init minus 1 for the DCR1 reg */
1512
1534
dev_data -> hqspi .Init .FlashSize = find_lsb_set (dev_cfg -> flash_size ) - 2 ;
1513
1535
#if DT_PROP (DT_NODELABEL (quadspi ), dual_flash ) && defined(QUADSPI_CR_DFM )
1536
+ dev_data -> hqspi .Init .SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE ;
1537
+ dev_data -> hqspi .Init .ChipSelectHighTime = QSPI_CS_HIGH_TIME_3_CYCLE ;
1538
+ dev_data -> hqspi .Init .DualFlash = QSPI_DUALFLASH_ENABLE ;
1539
+
1514
1540
/*
1515
1541
* When the DTS has <dual-flash>, it means Dual Flash Mode
1516
1542
* Even in DUAL flash config, the SDFP is read from one single quad-NOR
1517
1543
* else the magic nb is wrong (0x46465353)
1518
- * That means that the Dual Flash config is set after the SFDP sequence
1544
+ * So configure the driver to read from the first flash when dual flash
1545
+ * mode is temporarily disabled. Note that if BK2_NCS is not connected,
1546
+ * it is not possible to read from the second flash when dual flash mode
1547
+ * is disabled.
1519
1548
*/
1520
- dev_data -> hqspi .Init .SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE ;
1521
- dev_data -> hqspi .Init .ChipSelectHighTime = QSPI_CS_HIGH_TIME_3_CYCLE ;
1522
- dev_data -> hqspi .Init .DualFlash = QSPI_DUALFLASH_DISABLE ;
1523
- /* Set Dual Flash Mode only on MemoryMapped */
1524
1549
dev_data -> hqspi .Init .FlashID = QSPI_FLASH_ID_1 ;
1525
1550
#endif /* dual_flash */
1526
1551
@@ -1623,16 +1648,6 @@ static int flash_stm32_qspi_init(const struct device *dev)
1623
1648
LOG_DBG ("Write Un-protected" );
1624
1649
1625
1650
#ifdef CONFIG_STM32_MEMMAP
1626
- #if DT_PROP (DT_NODELABEL (quadspi ), dual_flash ) && defined(QUADSPI_CR_DFM )
1627
- /*
1628
- * When the DTS has dual_flash, it means Dual Flash Mode for Memory MAPPED
1629
- * Force Dual Flash mode now, after the SFDP sequence which is reading
1630
- * one quad-NOR only
1631
- */
1632
- MODIFY_REG (dev_data -> hqspi .Instance -> CR , (QUADSPI_CR_DFM ), QSPI_DUALFLASH_ENABLE );
1633
- LOG_DBG ("Dual Flash Mode" );
1634
- #endif /* dual_flash */
1635
-
1636
1651
ret = stm32_qspi_set_memory_mapped (dev );
1637
1652
if (ret != 0 ) {
1638
1653
LOG_ERR ("Failed to enable memory-mapped mode: %d" , ret );
0 commit comments