Commit graph

38 commits

Author SHA1 Message Date
Daniel DeGrasse 0767fd97f8 sd: sd_ops: fix DISK_IOCTL_CTRL_SYNC return code
SD IOCTL handling for DISK_IOCTL_CTRL_SYNC was falling through to the
default return statement, and returning an error when disk sync
succeeded. Fix this issue by properly breaking in IOCTL handler.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2024-05-29 07:31:39 +02:00
Daniel DeGrasse 05c84ccb2d sd: sd_ops: take card lock when issuing IOCTL command
Take card lock when running IOCTL command, to avoid race conditions that
could occur within the lower SDHC transfer implementations (as these
will be called by sdmmc_wait_ready)

Fixes #72368

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2024-05-16 18:54:21 -04:00
Daniel DeGrasse 6d7a6ea8a8 sd: sd_ops: remove additional unlock call within card_read()
SD ops card_read() implementation does not need to unlock mutex, as this
is managed by the calling function card_write_blocks. Remove this stray
k_mutex_unlock() call.

Fixes #72287

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2024-05-16 09:21:18 +02:00
Daniel DeGrasse ed64818565 sd: sdio: rework frequency and timing selection logic
Reword frequency and timing selection logic for SDIO framework so that
the maximum supported timing mode between the card and host will be
selected, even if the maximum frequency supported by the host is not
equal to the maximum frequency for that mode.

Also, resolve an issue where a host and card combo only supporting HS
mode or worse would still use legacy timing mode, because the CCCR flags
were not being checked when checking if HS bus speed could be used.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2024-05-16 09:21:03 +02:00
Daniel DeGrasse 82ce2b412d sd: sdmmc: rework frequency and timing selection logic
SDMMC framework frequency and timing selection logic has several
longstanding issues, including:
- requiring that SD hosts support the maximum frequency possible for a
  given UHS mode in order to apply that timing
- selecting SDHC_TIMING_SDR25 for high speed mode, when SDHC_TIMING_HS
  would be correct

Rework the frequency and timing selection logic within the SD framework
to resolve these issues.

Fixes #52589
Fixes #67943

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2024-05-16 09:21:03 +02:00
Jakob Genßler 3d1a4039ea sd: sdmmc: fix formatting
Formats according to latest .clang-format rules.

Signed-off-by: Jakob Genßler <jakob.genssler@gin.de>
2024-03-20 10:27:02 -05:00
Jakob Genßler 64fd289b2f sd: sdmmc: initialize cmd retries
Sets the retries in cmd struct to their Kconfig value. This fixes
an issue in imx_usdhc.c where retries would be used uninitialized. That
leads to a loop not being entered and subsequently no data being read
from the controller.
Tested on mimxrt1170_evk.

Signed-off-by: Jakob Genßler <jakob.genssler@gin.de>
2024-03-20 10:27:02 -05:00
Pisit Sawangvonganan 4889c9ca17 sd: update comment to reference 'SD host controller specification'
In the code comments referring to section 3.6.1, changed the reference
from 'SD specification' to 'SD host controller specification'
for a clearer and more accurate context.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-01-25 20:13:25 +01:00
Pisit Sawangvonganan 1829fc33cb sd: omit zero-initialization for csd[4] and cid[4]
After a review of the usage of csd[4] and cid[4],
it has been determined that zero-initialization can be omitted.

It can be expected from successful operations that data will be
appropriately written back from the underlying layer.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-01-25 20:13:25 +01:00
Pisit Sawangvonganan 8534c38232 sd: omit zero-initialization for sdhc_command and sdhc_data structures
The decision to omit zero-initialization is driven by a desire to enhance
Zephyr's compactness and efficiency.

This is achieved by omitting zero-initialization, thereby reducing the
instruction count and, as a byproduct, the code size.

After a thorough review of the usage of struct sdhc_command and sdhc_data,
it has been determined that zero-initialization can be omitted.
Only a portion of the fields need to be manually initialized.
(e.g. cmd.retries, data.block_addr)

For the uninitialized fields, it can be expected from successful
operations that data will be appropriately written back from
the underlying layer.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-01-25 20:13:25 +01:00
Pisit Sawangvonganan 612aa7169a sd: add config SD_CMD_RETRIES
Introduced the config SD_CMD_RETRIES option as a build-time parameter.
This allows the assignment of a specific value to cmd.retries.
The default value has been set to 0 to preserve the previous setting.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-01-25 20:13:25 +01:00
Daniel DeGrasse c1b7b817cc sd: modify sdmmc_wait_ready to always decrement timeout
As described in issue:
https://github.com/zephyrproject-rtos/zephyr/issues/65027,
sdmmc_wait_ready can enter an infinite loop if the card is removed while
waiting for it to report an idle status. Fix this by always decrementing
the timeout in sdmmc_wait_ready, regardless of whether the SD card is
busy.

Fixes #65027

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2024-01-24 16:37:21 +00:00
Daniel DeGrasse 1c0b114bf8 sd: add timeout when taking SD card lock for data operations
Previously, if an SD I/O operation was attempted while another thread
held the mutex for the SD card, the I/O operation would simply fail. Add
a timeout to k_mutex_lock calls within the SD subsystem, so that
multithreaded access to the SD card instead blocks until the SD card is
available for I/O

Fixes #66211

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2024-01-09 15:46:54 -06:00
Daniel DeGrasse 726128f810 sd: add SDIO subsystem
add SDIO subsystem code. SDIO subsystem currently supports
card initialization, SDIO read/write, SDIO extended
read/write, and SDIO card interrupts.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2023-11-06 19:01:08 -05:00
Najumon B.A c5094776e9 sd: add check for maximum supported voltage by host controller
add check for maximum voltage supported by hc before apply card voltage.

Signed-off-by: Najumon B.A <najumon.ba@intel.com>
2023-09-29 16:29:00 +02:00
Mourad Kharrazi a85ffa8130 drivers: sdhc: allow bandwidth selection
The current implementation uses both, host and card capabilites to derive
the maximum bus width to be used. However, in cases where a MMC device is
not connected to the host via shdc using the full bus width of 8 lines,
device initialization fails. Introducing the `bus-width` property
circumvents this by reducing the host bus capabilites and forcing
communication with the MMC device using 1, 4 or 8 lines.

Signed-off-by: Mourad Kharrazi <mourad.kharrazi@ithinx.io>
2023-09-13 16:20:59 +02:00
Daniel DeGrasse b2172a97d8 sd: clean up sizes of some SD data fields
Clean up sizes of some SD data fields, as the max value of these fields
is limited by the SD specification

Specifically, the limits are as follows:
num_io: 0-7, 3 bits (SDIO only)
relative_addr: 16 bits (SDMMC/MMC)
block_size: 12 bits (Max of 2KB, uint16_t used)
sd_version: 8 bits (currently at version 3)
card_speed: 8 bits (could potentially be reduced in size)

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2023-08-31 10:19:37 +02:00
Daniel DeGrasse 06984353bf sd: ensure card variables are initialized in sd_ops
Ensure card CSD and CID variables are initialized in sd_ops functions
for reading card CSD and CID data.

Fixes #59562

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2023-08-30 11:37:04 +02:00
Alexander Svensen 674e23820f sd: Return ret if error in sdmmc_spi_read_cxd
- Check OCR when sending CMD58, reject if 0

Signed-off-by: Alexander Svensen <alexander.svensen@nordicsemi.no>
2023-03-28 09:23:47 +02:00
Declan Snyder bb41f7dc9d sd: Enable eMMC cache
Use cache on eMMC

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-01-14 09:22:22 +01:00
Declan Snyder a0bfcb737c sd: Support eMMC HS200/400 Timing Mode
Support for eMMC HS200 and HS400 Timing Modes.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-01-14 09:22:22 +01:00
Declan Snyder 496ded03f1 sd: Enables High Speed Timing for MMC protocol
- Enables HS Timing Mode for MMC protocol.
- Reads data about HS modes from EXT_CSD for MMC.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-01-14 09:22:22 +01:00
Declan Snyder c89b2b08ce sd: Enables eMMC in Zephyr
Enables eMMC protocol in Zephyr

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-01-14 09:22:22 +01:00
Declan Snyder fea01b4254 tests: sd: Adds MMC test and Kconfigs
Adds MMC test and Kconfigs for MMC
Note: MMC not yet implemented as of this commit

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-01-14 09:22:22 +01:00
Declan Snyder cad243d59e sd: Changes framework to support MMC
- Adds mmc.c
- Edits sd.c to init and probe MMC
- Adds mmc init to sd_init
- Some functions from sdmmc.c should be in sd_ops because
they can be used by both sdmmc and mmc.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-01-14 09:22:22 +01:00
Declan Snyder 9fe18e05b4 sd: Changed Init Header Structure
In sd subsystem, made one header for all the init functions
instead of having a bunch of header files with one function.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-01-14 09:22:22 +01:00
Declan Snyder a838eef33b sd: Changed KConfig Structure
Changed KConfig structure for SD:
- Better Menu Interface
- Changed symbol dependency structure

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2023-01-14 09:22:22 +01:00
Daniel DeGrasse 94c858ed86 sd: split sdmmc common functions into sd_ops.c
split reusable portions of SDMMC protocol code into sd_ops.c, so other
SD protocols can use these functions directly without compiling in the
SDMMC subsystem.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2023-01-14 09:22:22 +01:00
Daniel DeGrasse b7cd970493 sd: add stub for SDIO support
Add stub code for SDIO support, capable of verifying card responds to CMD5.
This commit also changes the architecture of the SDIO probe step to make
adding new protocol support more streamlined, and enable compiling out
support for undesired protocols.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2023-01-14 09:22:22 +01:00
Daniel DeGrasse 9a63abc5c6 sd: utilize sdmmc_wait_ready with SPI mode.
Update sdmmc framework to use sdmmc_wait_ready when accessing card in
SPI mode. this will allow cards that do not return to ready to be polled
for busy status until the SD data timeout expires

Fixes #52931

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2023-01-10 13:29:32 +01:00
Daniel DeGrasse 11e3344909 sd: enable 4 bit bus width for high speed cards
Enable 4 bit bus width for high speed cards, so that host and card
combinations that cannot use UHS mode will still benefit from the
speed increase of using 4 data lines.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2022-10-03 14:22:33 +02:00
Gerard Marull-Paretas 79e6b0e0f6 includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.

The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.

NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-09-05 16:31:47 +02:00
Daniel DeGrasse 1a9d470fab sd: fix spelling in sdmmc_decode_csd
Fix spelling in sdmmc_decode_csd to use count instead of cout. Shortening
the variable name here was needlessly confusing.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2022-08-23 11:12:37 -05:00
Daniel DeGrasse ba9220c5bd sd: sdmmc: wait before sending second OCR
introduce a small wait between probing and initialization OCR. This
delay fixes a failure to initialize seen on some SD cards

Fixes #47238

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2022-08-18 12:49:03 +02:00
Daniel DeGrasse fdefd873fc sd: add proper handling for DISK_IOCTL_CTRL_SYNC
Add handling for DISK_IOCTL_CTRL_SYNC to SD subsystem. Note that
SD caching is not enabled by the SD stack, so the only required
operation to sync the disk is to wait for any active data programming
to complete.

Fixes #46689

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2022-07-04 14:25:33 +00:00
Daniel DeGrasse 7840b07121 sd: increase SDMMC busy wait timeout
SDMMC busy wait timeout was incorrectly waiting for
CONFIG_SD_DATA_TIMEOUT microseconds, but should be waiting for
CONFIG_SD_DATA_TIMEOUT milliseconds. Multiply wait value by 1000.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2022-05-09 08:46:29 -05:00
Daniel DeGrasse 64c5b93d5c sd: Add sdmmc protocol stack
Add SDMMC driver to subsystem. SDMMC driver will handle initialization,
as well as SDMMC I/O. SD mode support is currently supported, SPI mode
support is not.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2022-04-29 14:21:36 -05:00
Daniel DeGrasse 3e8bbee9ed sd: Add common SD initialization code
All SD cards require SD CMD0 (reset) and CMD8 (send IF cond) at boot.
Add this portion of the initialization flow to SD subsystem, as well as
query command to check if card is SDIO.

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2022-04-29 14:21:36 -05:00