Skip to content

Commit 0da1323

Browse files
FRASTMfabiobaltieri
authored andcommitted
drivers: flash: stm32 ospi can read the SFDP table from the DTS
When the SFDP table is provided by the DeviceTree with sfdp-bfp property It is NOT read from the octoFlash. If no table exists in the DTS and flash read SPDP fails, the device init fails. Availability of this sfdp_read API is conditional on selecting CONFIG_FLASH_JESD216_API. Signed-off-by: Francois Ramu <[email protected]>
1 parent 6143579 commit 0da1323

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

drivers/flash/flash_stm32_ospi.c

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -391,26 +391,18 @@ static int ospi_read_jedec_id(const struct device *dev, uint8_t *id)
391391
}
392392
#endif /* CONFIG_FLASH_JESD216_API */
393393

394+
#if !DT_NODE_HAS_PROP(DT_INST(0, st_stm32_ospi_nor), sfdp_bfp)
394395
/*
395-
* Read Serial Flash Discovery Parameter :
396+
* Read Serial Flash Discovery Parameter from the octoFlash at init :
396397
* perform a read access over SPI bus for SDFP (DataMode is already set)
397-
* or get it from the sdfp table (in the DTS)
398398
*/
399-
static int ospi_read_sfdp(const struct device *dev, off_t addr, uint8_t *data,
400-
size_t size)
399+
static int stm32_ospi_read_sfdp(const struct device *dev, off_t addr,
400+
void *data,
401+
size_t size)
401402
{
402403
const struct flash_stm32_ospi_config *dev_cfg = dev->config;
403404
struct flash_stm32_ospi_data *dev_data = dev->data;
404405

405-
#if DT_NODE_HAS_PROP(DT_INST(0, st_stm32_ospi_nor), sfdp_bfp)
406-
/* simulate the SDFP */
407-
ARG_UNUSED(addr); /* addr is 0 */
408-
409-
for (uint8_t i_ind = 0; i_ind < MIN(size, ARRAY_SIZE(dev_cfg->sfdp_bfp)); i_ind++) {
410-
*(data + i_ind) = dev_cfg->sfdp_bfp[i_ind];
411-
}
412-
#else /* sfdp_bfp */
413-
414406
OSPI_RegularCmdTypeDef cmd = ospi_prepare_cmd(dev_cfg->data_mode,
415407
dev_cfg->data_rate);
416408
if (dev_cfg->data_mode == OSPI_OPI_MODE) {
@@ -436,17 +428,52 @@ static int ospi_read_sfdp(const struct device *dev, off_t addr, uint8_t *data,
436428
return -EIO;
437429
}
438430

439-
hal_ret = HAL_OSPI_Receive(&dev_data->hospi, data, HAL_OSPI_TIMEOUT_DEFAULT_VALUE);
431+
hal_ret = HAL_OSPI_Receive(&dev_data->hospi, (uint8_t *)data,
432+
HAL_OSPI_TIMEOUT_DEFAULT_VALUE);
440433
if (hal_ret != HAL_OK) {
441434
LOG_ERR("%d: Failed to read data", hal_ret);
442435
return -EIO;
443436
}
444437

445-
#endif /* sfdp_bfp */
446438
dev_data->cmd_status = 0;
447439

448440
return 0;
449441
}
442+
#endif /* ! sfdp_bfp */
443+
444+
/*
445+
* Read Serial Flash Discovery Parameter :
446+
* perform a read access over SPI bus for SDFP (DataMode is already set)
447+
* or get it from the sdfp table (in the DTS)
448+
*/
449+
static int ospi_read_sfdp(const struct device *dev, off_t addr, void *data,
450+
size_t size)
451+
{
452+
#if DT_NODE_HAS_PROP(DT_INST(0, st_stm32_ospi_nor), sfdp_bfp)
453+
/* There is a sfdp-bfp property in the deviceTree : do not read the flash */
454+
const struct flash_stm32_ospi_config *dev_cfg = dev->config;
455+
456+
LOG_INF("Read SFDP from DTS property");
457+
/* If DTS has the sdfp table property, check its length */
458+
if (size > DT_INST_PROP_LEN(0, sfdp_bfp)) {
459+
LOG_ERR("SDFP bdfp length is wrong (%d)", DT_INST_PROP_LEN(0, sfdp_bfp));
460+
return -EIO;
461+
}
462+
/* The dev_cfg->sfdp_bfp if filled from the DTS property */
463+
memcpy(data, dev_cfg->sfdp_bfp + addr, size);
464+
465+
return 0;
466+
#else
467+
LOG_INF("Read SFDP from octoFlash");
468+
/* Get the SFDP from the octoFlash (no sfdp-bfp table in the DeviceTree) */
469+
if (stm32_ospi_read_sfdp(dev, addr, data, size) == 0) {
470+
/* If valid, then ignore any table from the DTS */
471+
return 0;
472+
}
473+
LOG_INF("Error reading SFDP from octoFlash and none in the DTS");
474+
return -EINVAL;
475+
#endif /* sfdp_bfp */
476+
}
450477

451478
static bool ospi_address_is_valid(const struct device *dev, off_t addr,
452479
size_t size)
@@ -1354,6 +1381,7 @@ static const struct flash_driver_api flash_stm32_ospi_driver_api = {
13541381
.page_layout = flash_stm32_ospi_pages_layout,
13551382
#endif
13561383
#if defined(CONFIG_FLASH_JESD216_API)
1384+
.sfdp_read = ospi_read_sfdp,
13571385
.read_jedec_id = ospi_read_jedec_id,
13581386
#endif /* CONFIG_FLASH_JESD216_API */
13591387
};
@@ -2155,6 +2183,7 @@ static struct flash_stm32_ospi_data flash_stm32_ospi_dev_data = {
21552183
},
21562184
.qer_type = DT_QER_PROP_OR(0, JESD216_DW15_QER_VAL_S1B6),
21572185
.write_opcode = DT_WRITEOC_PROP_OR(0, SPI_NOR_WRITEOC_NONE),
2186+
.page_size = SPI_NOR_PAGE_SIZE, /* by default, to be updated by sfdp */
21582187
#if DT_NODE_HAS_PROP(DT_INST(0, st_stm32_ospi_nor), jedec_id)
21592188
.jedec_id = DT_INST_PROP(0, jedec_id),
21602189
#endif /* jedec_id */

0 commit comments

Comments
 (0)