Skip to content

Conversation

@daniel-0723
Copy link

This PR is to support SPI extended modes, like 1-1-2/1-2-2/1-1-4/1-4-4/1-4D-4D/4-4-4/8-8-8/8D-8D-8D.

1-4D-4D :
1 means command is sent on 1 line in STR mode;
the first 4D means address is sent on 4 lines in DTR mode;
the second 4D means data is sent on 4 lines in DTD mode;

8D-8D-8D :
the first 8D means command is sent on 8 lines in DTR mode;
the second 8D means address is sent on 8 lines in DTR mode;
the third 8D means data is sent on 8 lines in DTD mode;

@zephyrbot zephyrbot added area: SPI SPI bus area: Devicetree Binding PR modifies or adds a Device Tree binding platform: STM32 ST Micro STM32 area: Flash labels Nov 23, 2023
@github-actions
Copy link

Hello @daniel-0723, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@erwango
Copy link
Member

erwango commented Nov 23, 2023

Thanks for this contribution.

Please:

  • Check your commit message. It should conform to the following template:
<dir>: <dir>: <Title summarizing the change>

<A description useful to the reader>

Signed-off-by: foo <[email protected]>

Also, your PR introduces a lot of changes that would deserve an individual explanation. So please split your change into multiple commits with proper justification for each of them.

@@ -0,0 +1,388 @@
/*
Copy link
Member

@erwango erwango Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a OSPI STM32 driver (see drivers/flash/flash_stm32_ospi.c), please explain the added value of this one and why the existing one can't be fixed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can refer to this link for more information.

Copy link
Author

@daniel-0723 daniel-0723 Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a OSPI STM32 driver (see drivers/flash/flash_stm32_ospi.c)

What we wanted to do before was to make spi_nor.c compatible with ospi controller, so we took STM32 ospi as an example. And implemented ospi_ll_stm32.c which is partially porting from (drivers/flash/flash_stm32_ospi.c), provided interface for spi_nor.c to call.

@@ -0,0 +1,388 @@
/*
* Copyright (c) 2022 Macronix.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use you own copyright.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why "ll", you're using HAL API

bool "STM32 MCU OSPI controller driver"
default y
depends on SOC_FAMILY_STM32
select USE_STM32_LL_OSPI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not using ll API, not required

Comment on lines 19 to 40
#if SPI_STM32

#config SPI_STM32_INTERRUPT
# bool "STM32 MCU SPI Interrupt Support"
# help
# Enable Interrupt support for the SPI Driver of STM32 family.

#config SPI_STM32_DMA
# bool "STM32 MCU SPI DMA Support"
# select DMA
# help
# Enable the SPI DMA mode for SPI instances
# that enable dma channels in their device tree node.

#config SPI_STM32_USE_HW_SS
# bool "STM32 Hardware Slave Select support"
# default y
# help
# Use Slave Select pin instead of software Slave Select.


#endif # SPI_STM32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not needed, please clean it.


include: [base.yaml, power.yaml]

on-bus: spi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be properly explained as it will have many impacts.

Copy link
Author

@daniel-0723 daniel-0723 Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we want to do is to make spi_nor.c compatible with ospi and qspi controllers ("bus: qspi" and "bus: ospi") such as STM32 qspi/ospi controller, not just limited to spi controller ("bus: spi"). But "on-bus: spi" property bings the compatible "jedec_spi_nor" with "bus: spi".

Copy link
Author

@daniel-0723 daniel-0723 Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also , I found dts/bindings/mtd/nxp,xspi-device.yaml includes spi-device.yaml. In other words, NXP xspi nodes are binding to spi controller ("bus: spi") because the "on-bus: spi" property. This can work now is because the NXP flexspi controller has no "bus: ospi" or "bus: qspi" property (dts/bindings/spi/nxp,imx-flexspi.yaml).

struct spi_buf {
void *buf;
size_t len;
#ifdef CONFIG_SPI_EXTENDED_MODES
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done in a dedicated commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done in a dedicated commit.

Do we need to submit a new pull request for it? Or can there be multiple commits in a pull request?

Comment on lines 140 to 147
compatible = "st,stm32-ospi-nor";
compatible = "jedec,spi-nor";
label = "MX25LM51245";
reg = <0>;
ospi-max-frequency = <DT_FREQ_M(50)>;
size = <DT_SIZE_M(512)>; /* 64 MBytes */
spi-bus-width = <OSPI_OPI_MODE>;
data-rate = <OSPI_DTR_TRANSFER>;
four-byte-opcodes;
spi-max-frequency = <DT_FREQ_M(50)>;
size = <DT_SIZE_M(64)>;
jedec-id = [c2 85 3a];
duplex = <0>;
frame-format = <0>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain these changes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we wanted to do before was to make spi_nor.c compatible with ospi controller and nodes. If this is possible in the future, these nodes such as mx25lm51245g will be compatible with "jedec,spi-nor".

Copy link
Contributor

@de-nordic de-nordic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO there is more work needed to cut down what is compiled in and allow to select which modes are supported.
As far as I can tell the bits that state SPI line configuration are set at compile time, for example with SPI_CONFIG_DT_INST, which means that most of mode switching code will never be used as the design of hardware exactly specifies by how many lines the SPI device is connected.
A designer of hardware should be allowed to pre-select supported modes and cut support to only these, to cut on flash usage by the driver.

#ifdef CONFIG_SPI_EXTENDED_MODES

rc = spi_nor_change_protocol(dev, spi_config_get_lines(spi_cfg));
//rc = spi_nor_change_protocol(dev, PROTO_8_8_8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftovers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solved

Copy link
Contributor

@tbursztyka tbursztyka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you really need quad/octal modes, please refer to #39991

Which is a more generic SPI API update than yours. (ALL generic SPI specific configurations bits should land in spi.h, but it should stay generic and provide no specialization towards a use case such as nor flash for instance).

I would be interested to know if you could use that API update instead.

@de-nordic
Copy link
Contributor

If you really need quad/octal modes, please refer to #39991

Which is a more generic SPI API update than yours. (ALL generic SPI specific configurations bits should land in spi.h, but it should stay generic and provide no specialization towards a use case such as nor flash for instance).

I would be interested to know if you could use that API update instead.

Planning to reopen the PR?

@tbursztyka
Copy link
Contributor

Planning to reopen the PR?

Sure

@github-actions
Copy link

github-actions bot commented Apr 2, 2024

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@rruuaanng
Copy link
Contributor

@daniel-0723 You can continue your work. Based on the discussion, there are still some unresolved issues.

@github-actions github-actions bot removed the Stale label Jan 14, 2025
@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Mar 15, 2025
@daniel-0723
Copy link
Author

Hi @de-nordic @rruuaanng , I have updated this PR's branch, please remove the "stale" label and review it.

@github-actions github-actions bot removed the Stale label Mar 17, 2025
@rruuaanng
Copy link
Contributor

@daniel-0723 You may temporarily adopt the suggestions made by the other maintainers mentioned above, as they appear to be incomplete. Once they are completed, I will review them again.

@daniel-0723 daniel-0723 force-pushed the macronix_octal_quad_mode branch from 1464b46 to eb75cb9 Compare March 17, 2025 02:21
@daniel-0723
Copy link
Author

which means that most of mode switching code will never be used as the design of hardware exactly specifies by how many lines the SPI device is connected.

Many Flash have low power and high performance modes. Also, Flash in 8D-8D-8D mode consumes more power than in 1S-1S-1S but of high performance, which requires the Flash mode can be switched in driver code to meet low power and high performance scenario.

@daniel-0723 daniel-0723 force-pushed the macronix_octal_quad_mode branch 2 times, most recently from 95284a5 to 9e9b4a8 Compare March 17, 2025 15:12
Enable quad and octal mode in spi_nor.c

Signed-off-by: Daniel Zhang <[email protected]>
@daniel-0723 daniel-0723 force-pushed the macronix_octal_quad_mode branch from 9e9b4a8 to 9cc87b2 Compare March 18, 2025 07:11
@tbursztyka
Copy link
Contributor

I already told you this needs to be redone almost completely.

  • It should be generic (dual/quad/octal modes are generic and not specifically meant for flash NOR only)
  • It should not bloat the thing up. (the amount of attributes you add in spi_buf is unbelievably too large)
  • Your changes on spi_buf is a direct no-go anyway: unaligned struct, bitfields on a public API, etc etc...

Again, I proposed years ago #39991 , nobody backed it up (sure it would have needed changes and refinements). In meetings it was decided that it was unnecessary, that qspi and all could get a direct flash driver, and no other device would ever need it. And then this happened: #71769

@daniel-0723
Copy link
Author

daniel-0723 commented Mar 19, 2025

And then this happened: #71769

Thank you for your comment. I think maybe I should rework it and move these work to drivers/flash/flash_mspi_nor.c.

@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label May 19, 2025
cr2 = 0x02;
ret = spi_nor_wrcr2(dev, cr2);
data->nor_protocol = PROTO_8D_8D_8D;
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default case missing

static int spi_nor_read(const struct device *dev, off_t addr, void *dest,
size_t size)
{
struct spi_nor_data *data = dev->data;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will trigger a build warning on data being unused when CONFIG_SPI_EXTENDED_MODES is disabled.

Suggestion: at line 1052:

-	struct spi_nor_data *data = dev->data;
+	uint8_t read_cmd = SPI_NOR_CMD_READ;
 	const size_t flash_size = dev_flash_size(dev);
 	int ret;
 
+	if (IS_ENABLED(CONFIG_SPI_EXTENDED_MODES))
+		read_cmd = ((struct spi_nor_data *)(dev->data))->read_cmd;

Ditto in spi_nor_write().

nor_protocol = PROTO_1_1_1;
}

return nor_protocol;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking suggestion:

static enum spi_nor_protocol spi_config_get_lines(const struct spi_config *config)
{
	switch (config->operation & SPI_LINES_MASK) {
	case SPI_LINES_SINGLE:
		return PROTO_1_1_1;
	case SPI_LINES_DUAL:
		return PROTO_1_2_2;
	case SPI_LINES_QUAD:
		return PROTO_4_4_4;
	case SPI_LINES_OCTAL:
		return PROTO_8_8_8;
	default:
		return PROTO_1_1_1;
	}
}

#endif

#ifdef CONFIG_SPI_EXTENDED_MODES

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this empty line?

*/
size_t len;

#ifdef CONFIG_SPI_EXTENDED_MODES
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer without this tabulation indentation.

data->read_cmd = SPI_NOR_CMD_READ;
data->program_cmd = SPI_NOR_CMD_PP;
data->erase_cmd = SPI_NOR_CMD_SE;
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could shrink the base cases:

	case PROTO_1_1_1:
	case PROTO_1_1_2:
	case PROTO_1_2_2:
	case PROTO_1_1_4:
	case PROTO_1_4_4:
	case PROTO_1_4D_4D:
		data->nor_protocol = nor_protocol;
		data->flag_access_32bit = false;
		data->read_cmd = SPI_NOR_CMD_READ;
		data->program_cmd = SPI_NOR_CMD_PP;
		data->erase_cmd = SPI_NOR_CMD_SE;
		break;

int ret = spi_nor_cmd_write(dev, SPI_NOR_CMD_WREN);

if (ret == 0) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this empty line?

* function.
*
* @param dev Device struct
* @param sr The new value of the configuration register2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace sr with cr.

@github-actions github-actions bot removed the Stale label May 20, 2025
@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Jul 19, 2025
@github-actions github-actions bot closed this Aug 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants