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: nrf_qspi_nor: refine alignment requirements #24128

Closed
wants to merge 1 commit into from

Conversation

pabigot
Copy link
Collaborator

@pabigot pabigot commented Apr 6, 2020

It's not sufficient that the read size be a multiple of four bytes;
the destination address for the read must also be properly aligned.

Relates to #24122

@@ -479,13 +479,13 @@ static inline int qspi_nor_read_id(struct device *dev,
static int qspi_nor_read(struct device *dev, off_t addr, void *dest,
size_t size)
{
if (!dest) {
/* destination must be aligned to a 32-bit word. */
if ((dest == NULL) || (((uintptr_t)dest % sizeof(uint32_t)) != 0)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: u32_t ? I see this is mostly used in this file as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, what is the gain of casting the dest to uintptr_t ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't void * wider pointre type than uintptr_t?

Copy link
Contributor

Choose a reason for hiding this comment

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

Would nrfx_is_word_aligned work as a check here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Switched to u32_t (original incorrectly had uint32_t, didn't notice).

dest is a pointer. Modulo operation on a pointer is not defined; the pointer must be cast to an integer. The only integral types to which pointers can legally be cast are intptr_t and uintptr_t, and we don't need signed modulo so uintptr_t is correct.

I was unaware of nrfx_is_word_aligned and don't see it used elsewhere in Zephyr.

@ball-hayden
Copy link
Contributor

Possibly one for a separate PR, but would it make sense to check the return code of nrfx_qspi_read before qspi_wait_for_completion?

https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/e23d8ac48dd0e5890559d618c048132dd4b0a697/drivers/flash/nrf_qspi_nor.c#L507-L510

nrfx_qspi_read returns NRFX_ERROR_INVALID_ADDR in the case the address isn't valid. I also wonder what the behaviour would be if the read returned NRFX_ERROR_BUSY - I guess that could also result in the read getting stuck waiting for completion?

@Laczen
Copy link
Collaborator

Laczen commented Apr 7, 2020

Hi @pabigot, IMO the flash read API should not put any limits on input address and read size or on output address. Only the flash write should do this.

It's not sufficient that the read size be a multiple of four bytes;
the destination address for the read must also be properly aligned.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
@pabigot pabigot force-pushed the nordic/20200406a branch from ba1bdca to 4caa587 Compare April 7, 2020 10:47
@pabigot
Copy link
Collaborator Author

pabigot commented Apr 7, 2020

nrfx_qspi_read returns NRFX_ERROR_INVALID_ADDR in the case the address isn't valid. I also wonder what the behaviour would be if the read returned NRFX_ERROR_BUSY - I guess that could also result in the read getting stuck waiting for completion?

Yes, that's probably the underlying cause of the behavior you see. It's a pervasive error throughout this driver. @kamlaz do you want to fix this or should I?

@pabigot
Copy link
Collaborator Author

pabigot commented Apr 7, 2020

Hi @pabigot, IMO the flash read API should not put any limits on input address and read size or on output address. Only the flash write should do this.

The existing API documents potential read alignment requirements for offset and size, and does not document any alignment requirements for write. I would expect both to be the same, and to include buffer alignment requirements.

@pabigot
Copy link
Collaborator Author

pabigot commented Apr 7, 2020

Closing this as not the right fix: the real problem is #24144, and the misalignment that revealed it is fixed by #24146.

@pabigot pabigot closed this Apr 7, 2020
@pabigot pabigot deleted the nordic/20200406a branch April 7, 2020 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants