Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: flash: spi nor: Add MultiInstance support #55735

Closed
wants to merge 1,137 commits into from

Conversation

ashiroji
Copy link
Contributor

Modify the SPI Nor driver to be able to have multiple instances at the same time. This patch is heavily inspired by the at45 driver. It was tested on the nRF5340 DK by using the external spi memory two times.

drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
@ashiroji ashiroji force-pushed the Improve_spi_nor_driver branch from e2c2ef5 to 83727aa Compare March 14, 2023 08:23
@Laczen
Copy link
Collaborator

Laczen commented Mar 14, 2023

@ashiroji, thank you for the proposal. Although I haven't gone trough the complete PR I have two general remarks:

  1. I was suspecting changes to the Kconfig file as multi-instance is not easy supported by Kconfig options that are for all instances,
  2. The use of ANY_INST_..., instead you should create a structure that fits the instance while using the FOR_EACH macro construct.

@ashiroji ashiroji force-pushed the Improve_spi_nor_driver branch from 83727aa to 26bef72 Compare March 14, 2023 08:48
@ashiroji
Copy link
Contributor Author

ashiroji commented Mar 14, 2023

Hi @Laczen,

  1. I was suspecting changes to the Kconfig file as multi-instance is not easy supported by Kconfig options that are for all instances,

Can you please expand more?

  1. The use of ANY_INST_..., instead you should create a structure that fits the instance while using the FOR_EACH macro construct.

The modifications I made are inspired by the at45 flash driver without trying to redesign the SPI NOR driver. In the original implementation, there is conditional code that gets compiled (or not) depending the existence of a feature or not (such as dpd). If each driver instance has a fitting structure, the compiler will complain that a data field is missing.

@ashiroji ashiroji force-pushed the Improve_spi_nor_driver branch from 26bef72 to 06a4e98 Compare March 14, 2023 09:17
@Laczen
Copy link
Collaborator

Laczen commented Mar 14, 2023

Hi @Laczen,

  1. I was suspecting changes to the Kconfig file as multi-instance is not easy supported by Kconfig options that are for all instances,

Can you please expand more?

As an example the Kconfig has an option: SPI_NOR_SFDP_MINIMAL, this will be enabled globally for all instances and does not match the multi-instance solution.

  1. The use of ANY_INST_..., instead you should create a structure that fits the instance while using the FOR_EACH macro construct.

The modifications I made are inspired by the at45 flash driver without trying to redesign the SPI NOR driver. In the original implementation, there is conditional code that gets compiled (or not) depending the existence of a feature or not (such as dpd). If each driver instance has a fitting structure, the compiler will complain that a data field is missing.

Multi-instance support requires supporting a mix of devices that have and don't have a feature, the use of ANY_INST_... could be used to add/eliminate certain parts of the code (or to make it do nothing/not access unavailable elements when it is not used), but it does not seem appropiate to define the structures based upon ANY_INST_.... But I repeat I haven't gone trough the complete PR.

@ashiroji
Copy link
Contributor Author

ashiroji commented Mar 14, 2023

Hi @Laczen,

  1. I was suspecting changes to the Kconfig file as multi-instance is not easy supported by Kconfig options that are for all instances,

Can you please expand more?

As an example the Kconfig has an option: SPI_NOR_SFDP_MINIMAL, this will be enabled globally for all instances and does not match the multi-instance solution.

I see your point, I did not think about this actually.

  1. The use of ANY_INST_..., instead you should create a structure that fits the instance while using the FOR_EACH macro construct.

The modifications I made are inspired by the at45 flash driver without trying to redesign the SPI NOR driver. In the original implementation, there is conditional code that gets compiled (or not) depending the existence of a feature or not (such as dpd). If each driver instance has a fitting structure, the compiler will complain that a data field is missing.

Multi-instance support requires supporting a mix of devices that have and don't have a feature, the use of ANY_INST_... could be used to add/eliminate certain parts of the code (or to make it do nothing/not access unavailable elements when it is not used), but it does not seem appropiate to define the structures based upon ANY_INST_.... But I repeat I haven't gone trough the complete PR.

The general idea I went with, is if an instance has a field, enable the needed data field for all instances and initialise it with proper value from tree (for those who have) and with 0( for those who don't). This is specifically used for deep power down mode parameters. That way the code that uses these parameters will find a valid value even if the feature is not defined.
Please take your time to read the PR and indicate how to improve this

@ashiroji
Copy link
Contributor Author

@Laczen , does it make sense to transform the Kconfigs to device tree properties? To have the possibility to choose different values for different instances?

@Laczen
Copy link
Collaborator

Laczen commented Mar 15, 2023

@Laczen , does it make sense to transform the Kconfigs to device tree properties? To have the possibility to choose different values for different instances?

In general yes, but you might leave some default parameters that are used in case they cannot be derived from the dts.

@ashiroji
Copy link
Contributor Author

ashiroji commented Mar 15, 2023

@Laczen , does it make sense to transform the Kconfigs to device tree properties? To have the possibility to choose different values for different instances?

In general yes, but you might leave some default parameters that are used in case they cannot be derived from the dts.

I think I can modify the following Kconfigs:
SPI_NOR_SFDP into an enum parameter:

zephyr,spi-nor-sfdp:
type : string
enum:
- "minimal"
- "devicetree"
- "runtime"

SPI_NOR_CS_WAIT_DELAY, SPI_NOR_FLASH_LAYOUT_PAGE_SIZE can be transformed into an int parameter

SPI_NOR_IDLE_IN_DPD into a bool parameter

@Laczen
Copy link
Collaborator

Laczen commented Mar 15, 2023

@ashiroji, I propose you leave the Kconfig as is, it is not really fit for multi-instance, but it will require so much changes that the PR will grow too much.

Regarding the use of the ANY_INST_... macro: @de-nordic could you comment what should be the best way to handle this: if we have multiple instances of a driver is it acceptable to create one driver config struct with all options enabled or should the driver config struct be split up in several (optional) parts that are added only when required ?

@ashiroji
Copy link
Contributor Author

@ashiroji, I propose you leave the Kconfig as is, it is not really fit for multi-instance, but it will require so much changes that the PR will grow too much.

Okay.

Regarding the use of the ANY_INST_... macro: @de-nordic could you comment what should be the best way to handle this: if we have multiple instances of a driver is it acceptable to create one driver config struct with all options enabled or should the driver config struct be split up in several (optional) parts that are added only when required ?

I'll be waiting for further comments

@ashiroji
Copy link
Contributor Author

@nordicjm , @de-nordic , @nashif can you please review the PR?

drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
@ashiroji ashiroji force-pushed the Improve_spi_nor_driver branch 3 times, most recently from 4bd55fe to 3cc95c2 Compare March 23, 2023 08:44
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
drivers/flash/spi_nor.c Outdated Show resolved Hide resolved
@nordicjm nordicjm added the dev-review To be discussed in dev-review meeting label Mar 23, 2023
@de-nordic
Copy link
Collaborator

@ashiroji, I propose you leave the Kconfig as is, it is not really fit for multi-instance, but it will require so much changes that the PR will grow too much.

Regarding the use of the ANY_INST_... macro: @de-nordic could you comment what should be the best way to handle this: if we have multiple instances of a driver is it acceptable to create one driver config struct with all options enabled or should the driver config struct be split up in several (optional) parts that are added only when required ?

I think the current solution with enabling struct elements when they are needed for all instances is OK.
If we figure better way to do that, and dynamically add these fields to some buffer we may think it over.

@MaureenHelm MaureenHelm removed the dev-review To be discussed in dev-review meeting label Mar 23, 2023
@ashiroji ashiroji force-pushed the Improve_spi_nor_driver branch 3 times, most recently from 83df2eb to 61d5c14 Compare March 30, 2023 11:19
@ashiroji
Copy link
Contributor Author

ashiroji commented Mar 19, 2024

Sorry, I guess I did something wrong when I force-pushed the update.
@de-nordic ,
I opened a new PR
#70415

@ashiroji ashiroji closed this Mar 19, 2024
@de-nordic
Copy link
Collaborator

Sorry, I guess I did something wrong when I force-pushed the update. @de-nordic , I opened a new PR #70415

Happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.