zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
9.92k stars 6.11k forks source link

[RFC] Introduction to MSPI (Multi-bit SPI) driver API #70723 #71769

Closed swift-tk closed 2 hours ago

swift-tk commented 1 month ago

Description

This PR primarily forces on creating a generic MSPI(multi-bit SPI) controller API that typically is used when interfacing with high speed pSRAM/flash memory, displays, and sensors devices.

I have finished below items from https://github.com/zephyrproject-rtos/zephyr/issues/70723#issuecomment-2045544160. Also see this issue for more detailed RFC.

Q & A

Q: Why include sensors?

A: The MSPI would still be compatible with generic serial SPI interface and the API is expandable to support RTIO.

Q: Use of Kconfig MSPI_COMPLETION_TIMEOUT_TOLERANCE

A: This is a maximum time allowed for a transfer regardless of error or large data size. The unit is not yet determined, maybe in milli-seconds.

Q: Why won't we include a parameter for the maximum time to wait for the next transfer.

A: MSPI only cares about the transfer, it is all about the transfer. It does not care what happens in between transfers. Unlike SDIO, the protocol dictates a controlled flow and that there is a response for every command. This is not true for SPI. The MSPI controller can't be responsible for knowing the internal state of specific devices. This is the responsibility of the specific device driver.

Q: Purpose of struct mspi_dev_cfg

A: This contains the device specific hardware controller settings. However, they are device properties and should be derived from device datasheets. While often time the value of its member may frequently change, the settings derived from DTS that filled within the struct should represent the most frequently used settings during run-time. When value of its member changes, the mspi_dev_config API should be used for device operation mode switching.

For future enhancement, enhanced XIP or DMA that requires sending only cmd and address one time may be supported but will not be in the context of this PR.

Q: Support for memory mapped device

A: The XIP address should be obtained from devicetree memory regions. For multi-slave scenario, each device's mapped address may be determined by the start address along with device index and its size. This can be done within the controller or pass by struct mspi_xip_config

Going to defer code linking from this PR as it is more of kernel enhancement and also for the fact that we may need more time to think about it.

Q: Reasons when choosing the member of struct mspi_xfer

A:

Q: MSPI slave usage

A: When an MSPI controller act as a slave, it is essentially an MSPI device. A slave device driver could be created for this purpose to interface with the existing controller driver and the APIs would still apply. But there is no plan to show an implementation of a mspi slave in this PR and the details may subject to change.

If you think there might be a conflict with the current proposal, please point it out. At this point, the use of MSPI slave may be very specific and uncommon.

Action items:

github-actions[bot] commented 1 month ago

Hello @swift-tk, 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. 😊

swift-tk commented 1 month ago

Hi @carlescufi Could you please add appropriate labels to the PR and possibly link to https://github.com/zephyrproject-rtos/zephyr/issues/70723. Thanks. Please let me know if it needs to be discussed again in AG meeting.

swift-tk commented 1 month ago

will make another pass on the docs when other folks actually knowledgeable about the topic will have provided their own reviews. It is looking pretty good and thorough though, so thanks for that!

@kartben Many thanks!

RichardSWheatley commented 1 month ago

@danieldegrasse I should have posted my replies in general instead of individual comments.

You can change many parameters on the fly from the chipset driver, prior to sending the command or data as each external chipset will have different configurations.

I am going to suggest we post some code here and hopefully that will help clear things up.

swift-tk commented 1 month ago

There is going to be real hardware drivers soon once the prerequisites are merged to main.

swift-tk commented 1 month ago

I clicked the wrong button… For those that want to take a quick peek on the real hardware implementation please see here https://github.com/AmbiqMicro/ambiqzephyr/pull/13

RichardSWheatley commented 4 weeks ago

Glad to see the latest commits. Including the Ambiq driver and the to chip drivers.

I think this should help clarify some questions and help move this ticket towards completion.

Is it possible to possibly discuss this in the next Architecture meeting to ensure everyone is on the same page?

zephyrbot commented 3 weeks ago

The following west manifest projects have been modified in this Pull Request:

Name Old Revision New Revision Diff
hal_ambiq https://github.com/zephyrproject-rtos/hal_ambiq/commit/fcbbd99e20db1432196f4aec92678bd1f5b19c96 https://github.com/zephyrproject-rtos/hal_ambiq/commit/367ae6a0396e3074bebbd55ef72f8e577168e567 zephyrproject-rtos/hal_ambiq@fcbbd99e..367ae6a0

Note: This message is automatically posted and updated by the Manifest GitHub Action.

swift-tk commented 2 weeks ago

Hi all, Does anyone have further comments? If would like to discuss it, I'm available on discord or the architecture meeting. There is one last thing and I would to direct your attention to the return values of the API that I have listed in the description. If everthing looks ok, I would like to move forward. It may be the time for the reviewers start to looked at the affected areas such as HW emulation, MEMC and FLASH.

emulation: test: @aaronemassey @jeremybettis @yperess @tristan-google @alevkoy @asemjonovs flash: @de-nordic MEMC: ? Devicetree/Devicetree Bindings: @galak @decsny @gmarull API: @tbursztyka @erwango @danieldegrasse @teburd

Thanks!!

AlessandroLuo commented 1 week ago

LGTM!

aaronyegx commented 1 week ago

Hi @carlescufi @erwango @danieldegrasse , could you take another look and see if any other discussion or changes are needed? Thank you.

FRASTM commented 1 week ago

That's an interesting abstraction especially for the the multi-SPI NOR flash memories. In some situation, the driver is getting the flash parameters from by the SFDP table of device memory itself, --> could values from the SFDP conflict with the values hold by the DTS of the device ?

carlescufi commented 1 week ago

Hi @carlescufi @erwango @danieldegrasse , could you take another look and see if any other discussion or changes are needed? Thank you.

I defer the Nordic review to @anangl. It would be great to get additional reviews from @erwango and @danieldegrasse

swift-tk commented 1 week ago

That's an interesting abstraction especially for the the multi-SPI NOR flash memories. In some situation, the driver is getting the flash parameters from by the SFDP table of device memory itself, --> could values from the SFDP conflict with the values hold by the DTS of the device ?

The short answer is it could be conflicting to the SFDP. If there is a conflict, then there will probably be problems during run time. Notice that the values hold by device DTS are brought into the device driver, so the device initialization can be optimized by including a SFDP read operation and then check against the target settings in the DTS to catch incompatibility. The base settings for reading the SFDP should be hardcoded in the device driver and match the factory default settings of the device after power up. In my case, the flash atxp032 driver, there is a const serial_cfg that stores the default settings. In which case that a flash device powers up from their OTP state, one could introduce another otp_cfg variable.

In any case, it is left to the device drivers to decide that and manage their internal states.

erwango commented 1 week ago

I defer the Nordic review to @anangl. It would be great to get additional reviews from @erwango and @danieldegrasse

Defering review to @FRASTM and @GeorgeCGV for STM32

swift-tk commented 14 hours ago

@anangl @FRASTM @GeorgeCGV @danieldegrasse Any additional comments? If not, could we move forward? @carlescufi

swift-tk commented 11 hours ago

@kartben @dleach02 I agree with nashif. To be fair, the SPI API has not adapted that and the terms in MSPI are taken from SPI. If it is not blocking and not absolutely necessary, then I prefer not to change anything at this time.

RichardSWheatley commented 10 hours ago

@kartben @dleach02 I agree with nashif. To be fair, the SPI API has not adapted that and the terms in MSPI are taken from SPI. If it is not blocking and not absolutely necessary, then I prefer not to change anything at this time.

@swift-tk one of @dleach02 last comments was that this could be done in a follow-up PR.

@dleach02 ia that ok?

dleach02 commented 10 hours ago

@kartben @dleach02 I agree with nashif. To be fair, the SPI API has not adapted that and the terms in MSPI are taken from SPI. If it is not blocking and not absolutely necessary, then I prefer not to change anything at this time.

@swift-tk one of @dleach02 last comments was that this could be done in a follow-up PR.

@dleach02 ia that ok?

we are discussing that in discord. The suggestion was that it was okay but we would enter a blocking bug against the API to be addressed during the v3.7 stabilization period.

swift-tk commented 9 hours ago

@kartben There should be no master/slave phrase anymore. If you see any, please point it out. Thanks. @RichardSWheatley @AlessandroLuo @aaronyegx Ambiq people please +1 approve again as there are some changes related to the code.

danieldegrasse commented 9 hours ago

Hi @carlescufi @erwango @danieldegrasse , could you take another look and see if any other discussion or changes are needed? Thank you.

Taking another look now- apologies, I was not aware we were trying to cut this in before 3.7

RichardSWheatley commented 9 hours ago

Hi @carlescufi @erwango @danieldegrasse , could you take another look and see if any other discussion or changes are needed? Thank you.

Taking another look now- apologies, I was not aware we were trying to cut this in before 3.7

Where can we look for dates on 3.7 LTS fr ze and release?

swift-tk commented 9 hours ago

Hi @carlescufi @erwango @danieldegrasse , could you take another look and see if any other discussion or changes are needed? Thank you.

Taking another look now- apologies, I was not aware we were trying to cut this in before 3.7

Where can we look for dates on 3.7 LTS fr ze and release?

https://github.com/zephyrproject-rtos/zephyr/wiki/Release-Management

danieldegrasse commented 6 hours ago

Running tests/subsys/settings/functional/file on native_sim//64 seems to also fail on main, so I think the issue is not related to this PR specifically

swift-tk commented 2 hours ago

The API seems to be designed to transition the device into QSPI or OSPI mode, and then send all commands in that mode. I guess for devices that do not support QSPI/OSPI, we would set them to 1-4-4 or 1-8-8 mode?

What mode to be set should be based on the device datasheet, i.e. transfer waveforms. And yes, the transcieve API is designed to send everything in one particular mode.

Are there devices you are aware of that use something like 1-4-4 mode for flash reads and 1-2-2 mode for flash writes? From my understanding, that would result in frequent reconfiguration with this API. I suppose the idea in that case would be that read commands would be handled via the mspi_dev_config, since that configuration is static and does not need to change?

I'm aware of one particular device that could only write memory with 1-1-4 mode and able to read memory using 1-1-4 and 1-4-4. But I think this is rare and poorly designed hardware in attempt to bump read speed. In this case, whether it result in frequent reconfiguration or not will depend on the usage. i.e. the percentages of write and read memory commands. If it is a flash device we are talking about, write may already be very costly with erase and page layout are involved. Everything will still need to go through mspi_dev_config, as transceive API is not aware of io-mode and etc. If case by case analysis that the configuration becomes very frequent, then I suggest to use a software queue like RTIO to manage calls to MSPI API to bascially pipelining the requests. I understand that the hardware command queue are powerful and capable of handling switching every settable things. But I'd prefer that the controller driver in general shouldn't need to manage the internal states of its device as I think it is better handled at device drivers. If we attempt to do that, we would probably end of having similar spi_cfg with all configurable parameters when calling transceive API. It would become very unfriendly to use as the number of parameters would be quite large for MSPI.

When using 1-4-4 or 1-8-8 mode, is my understanding correct that the first byte of a transfer would be sent using 1 pad, and future bytes of the same transfer would use 4/8 pads?

1-4-4 correspond to sending command with 1 pad, sending address with 4 pad and sending data with 4 pad.

How will this API work with SFDP? It seems like it would function by probing all data in serial mode, then reconfiguring the device based on what was discovered, is that correct?

For that particular flash ATXP032 driver I did, if I understand things correctly in the datasheet, the SFDP can only be read in serial mode. The device also always power up into serial mode. But this may not be true for other devices.

Constructing the SFDP API would be device driver specific and it would just follow the same flow as with write and read memory commands. If the device need to be switched to certain io mode or certain state in order to read SFDP, then do the switch with mspi_dev_config, once read is finished, it should switch itself back to normal operation mode.

swift-tk commented 2 hours ago

I thank everyone for their effort to make the merge happen!! But it is not done, there are rooms to improve for sure. I'm happy to further improve the API or review any PR to improve it. Cheers!!