Skip to content

Commit 6143579

Browse files
FRASTMfabiobaltieri
authored andcommitted
drivers: flash: stm32 ospi driver with read ID from octoflash
This commit adds the jedec216 read JEDEC ID function API. The ospi commands are issued to the octo flash device or by the DTS jedec-id property. Availability of this API is conditional on selecting CONFIG_FLASH_JESD216_API. Signed-off-by: Francois Ramu <[email protected]>
1 parent 9fbe27b commit 6143579

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

drivers/flash/flash_stm32_ospi.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ struct flash_stm32_ospi_data {
146146
uint32_t write_opcode;
147147
enum jesd216_mode_type read_mode;
148148
enum jesd216_dw15_qer_type qer_type;
149+
#if defined(CONFIG_FLASH_JESD216_API)
150+
/* Table to hold the jedec Read ID given by the octoFlash or the DTS */
151+
uint8_t jedec_id[JESD216_READ_ID_LEN];
152+
#endif /* CONFIG_FLASH_JESD216_API */
149153
int cmd_status;
150154
#if STM32_OSPI_USE_DMA
151155
struct stream dma;
@@ -319,6 +323,74 @@ static OSPI_RegularCmdTypeDef ospi_prepare_cmd(uint8_t transfer_mode, uint8_t tr
319323
return cmd_tmp;
320324
}
321325

326+
#if defined(CONFIG_FLASH_JESD216_API)
327+
/*
328+
* Read the JEDEC ID data from the octoFlash at init or DTS
329+
* and store in the jedec_id Table of the flash_stm32_ospi_data
330+
*/
331+
static int stm32_ospi_read_jedec_id(const struct device *dev)
332+
{
333+
struct flash_stm32_ospi_data *dev_data = dev->data;
334+
335+
#if DT_NODE_HAS_PROP(DT_INST(0, st_stm32_ospi_nor), jedec_id)
336+
/* If DTS has the jedec_id property, check its length */
337+
if (DT_INST_PROP_LEN(0, jedec_id) != JESD216_READ_ID_LEN) {
338+
LOG_ERR("Read ID length is wrong (%d)", DT_INST_PROP_LEN(0, jedec_id));
339+
return -EIO;
340+
}
341+
342+
/* The dev_data->jedec_id if filled from the DTS property */
343+
#else
344+
/* This is a SPI/STR command to issue to the octoFlash device */
345+
OSPI_RegularCmdTypeDef cmd = ospi_prepare_cmd(OSPI_SPI_MODE, OSPI_STR_TRANSFER);
346+
347+
cmd.Instruction = JESD216_CMD_READ_ID;
348+
cmd.DummyCycles = 8U;
349+
cmd.AddressSize = HAL_OSPI_ADDRESS_NONE;
350+
cmd.NbData = JESD216_READ_ID_LEN; /* 3 bytes in the READ ID */
351+
352+
HAL_StatusTypeDef hal_ret;
353+
354+
hal_ret = HAL_OSPI_Command(&dev_data->hospi, &cmd,
355+
HAL_OSPI_TIMEOUT_DEFAULT_VALUE);
356+
357+
if (hal_ret != HAL_OK) {
358+
LOG_ERR("%d: Failed to send OSPI instruction", hal_ret);
359+
return -EIO;
360+
}
361+
362+
/* Place the received data directly into the jedec Table */
363+
hal_ret = HAL_OSPI_Receive(&dev_data->hospi, dev_data->jedec_id,
364+
HAL_OSPI_TIMEOUT_DEFAULT_VALUE);
365+
if (hal_ret != HAL_OK) {
366+
LOG_ERR("%d: Failed to read data", hal_ret);
367+
return -EIO;
368+
}
369+
#endif /* jedec_id */
370+
371+
dev_data->cmd_status = 0;
372+
373+
return 0;
374+
}
375+
376+
/*
377+
* Read Serial Flash ID :
378+
* just gives the values received by the octoFlash or from the DTS
379+
*/
380+
static int ospi_read_jedec_id(const struct device *dev, uint8_t *id)
381+
{
382+
struct flash_stm32_ospi_data *dev_data = dev->data;
383+
384+
/* Take jedec Id values from the table (issued from the octoFlash) */
385+
memcpy(id, dev_data->jedec_id, JESD216_READ_ID_LEN);
386+
387+
LOG_INF("Manuf ID = %02x Memory Type = %02x Memory Density = %02x",
388+
id[0], id[1], id[2]);
389+
390+
return 0;
391+
}
392+
#endif /* CONFIG_FLASH_JESD216_API */
393+
322394
/*
323395
* Read Serial Flash Discovery Parameter :
324396
* perform a read access over SPI bus for SDFP (DataMode is already set)
@@ -1281,6 +1353,9 @@ static const struct flash_driver_api flash_stm32_ospi_driver_api = {
12811353
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
12821354
.page_layout = flash_stm32_ospi_pages_layout,
12831355
#endif
1356+
#if defined(CONFIG_FLASH_JESD216_API)
1357+
.read_jedec_id = ospi_read_jedec_id,
1358+
#endif /* CONFIG_FLASH_JESD216_API */
12841359
};
12851360

12861361
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
@@ -1903,6 +1978,15 @@ static int flash_stm32_ospi_init(const struct device *dev)
19031978
return -EIO;
19041979
}
19051980

1981+
#if defined(CONFIG_FLASH_JESD216_API)
1982+
/* Process with the RDID (jedec read ID) instruction at init and fill jedec_id Table */
1983+
ret = stm32_ospi_read_jedec_id(dev);
1984+
if (ret != 0) {
1985+
LOG_ERR("Read ID failed: %d", ret);
1986+
return ret;
1987+
}
1988+
#endif /* CONFIG_FLASH_JESD216_API */
1989+
19061990
if (stm32_ospi_config_mem(dev) != 0) {
19071991
LOG_ERR("OSPI mode not config'd (%u rate %u)",
19081992
dev_cfg->data_mode, dev_cfg->data_rate);
@@ -2071,6 +2155,9 @@ static struct flash_stm32_ospi_data flash_stm32_ospi_dev_data = {
20712155
},
20722156
.qer_type = DT_QER_PROP_OR(0, JESD216_DW15_QER_VAL_S1B6),
20732157
.write_opcode = DT_WRITEOC_PROP_OR(0, SPI_NOR_WRITEOC_NONE),
2158+
#if DT_NODE_HAS_PROP(DT_INST(0, st_stm32_ospi_nor), jedec_id)
2159+
.jedec_id = DT_INST_PROP(0, jedec_id),
2160+
#endif /* jedec_id */
20742161
OSPI_DMA_CHANNEL(STM32_OSPI_NODE, tx_rx)
20752162
};
20762163

drivers/flash/jesd216.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
#include <zephyr/sys/util.h>
1313
#include <zephyr/types.h>
1414

15+
/* JEDEC Read identification */
16+
#define JESD216_CMD_READ_ID SPI_NOR_CMD_RDID
17+
#define JESD216_OCMD_READ_ID 0x9F60
18+
#define JESD216_READ_ID_LEN 3
19+
1520
/* Following are structures and constants supporting the JEDEC Serial
1621
* Flash Discoverable Parameters standard, JESD216 and its successors,
1722
* available at

0 commit comments

Comments
 (0)