mmc_spi: Fix cd-gpios for SPI SD card insertion/removal detection
PATCH 1/1 mmc_spi: Fix cd-gpios for SPI SD card insertion/removal detection
Previously, SD cards using pure SPI protocol with mmc_spi could not reliably detect card removal via cd-gpios. This patch fixes the issue by extending the card alive check in _mmc_detect_card_removed() to include cd-gpios detection.
Additionally, mmc_rescan() now includes a status probing function specific to SPI SD cards to ensure the card state is correctly updated on insertion and removal events.
--- drivers/mmc/core/core.c | 15 ++++++++++++--- drivers/mmc/core/core.h | 1 + drivers/mmc/core/mmc.c | 8 ++++++++ 3 files changed, 21 insertions(+), 3 deletions(-)
drivers/mmc/core/core.c +12 -3
@@ -2123,6 +2123,11 @@ int _mmc_detect_card_removed(struct mmc_host *host) ret = host->bus_ops->alive (host); +if (mmc_host_is_spi (host)) { +if (host->ops->get_cd){ + ret = !host->ops->get_cd (host); + } + }/* * Card detect status and alive check may be out of sync if card is * removed slowly, when card detect switch changes while card/slot
@@ -2238,9 +2243,13 @@ void mmc_rescan(struct work_struct *work) }/* Verify a registered card to be functional, else remove it. */ -if (host->bus_ops) - host->bus_ops->detect (host); - +if (host->bus_ops){ +if (mmc_host_is_spi (host)){ +mmc_detect_mmc (host); + }else { + host->bus_ops->detect (host); + } + } host->detect_change =0 ;/* if there still is a card present, stop here */
drivers/mmc/core/core.h +1 -0
@@ -78,6 +78,7 @@ void _mmc_detect_change(struct mmc_host *host, unsigned long delay,int _mmc_detect_card_removed (struct mmc_host *host);int mmc_detect_card_removed (struct mmc_host *host); +int mmc_detect_mmc (struct mmc_host *host);int mmc_attach_mmc (struct mmc_host *host);int mmc_attach_sd (struct mmc_host *host);int mmc_attach_sdio (struct mmc_host *host);
drivers/mmc/core/mmc.c +8 -0
@@ -2316,6 +2316,14 @@ static const struct mmc_bus_ops mmc_ops = { .flush_cache = _mmc_flush_cache, }; +/* +* Export function for mmc detection +*/ +int mmc_detect_mmc (struct mmc_host *host){ +mmc_detect (host); +return 0 ; +} +/* * Starting point for MMC card init. */
Signature · 1 line
2.34.1
Quoted 9 lines · cuu
Previously, SD cards using pure SPI protocol with
mmc_spicould not reliably detect card removal via cd-gpios. This patch fixes the issue by extending the card alive check in_mmc_detect_card_removed()to include cd-gpios detection.Additionally,
mmc_rescan()now includes a status probing function specific to SPI SD cards to ensure the card state is correctly updated on insertion and removal events.
Sorry, I don't get this.
What is so special with SPI cards here that makes the regular card detection/removal procedures not to work?
Is there something wrong with mmc_sd_alive()?
Quoted 2 lines
Signed-off-by cuu <[email protected]>
Please use a proper name for your sob line. I assume "cuu" is an alias, no?
Kind regards
Uffe
Quoted 73 lines
--- drivers/mmc/core/core.c | 15 ++++++++++++--- drivers/mmc/core/core.h | 1 + drivers/mmc/core/mmc.c | 8 ++++++++ 3 files changed, 21 insertions(+), 3 deletions(-)drivers/mmc/core/core.c +12
-3@@ -2123,6 +2123,11 @@ int _mmc_detect_card_removed(struct mmc_host *host) ret = host->bus_ops->alive (host); +if (mmc_host_is_spi (host)) { +if (host->ops->get_cd){ + ret = !host->ops->get_cd (host); + } + }/* * Card detect status and alive check may be out of sync if card is * removed slowly, when card detect switch changes while card/slot @@ -2238,9 +2243,13 @@ void mmc_rescan(struct work_struct *work) }/* Verify a registered card to be functional, else remove it. */ -if (host->bus_ops) - host->bus_ops->detect (host); - +if (host->bus_ops){ +if (mmc_host_is_spi (host)){ +mmc_detect_mmc (host); + }else { + host->bus_ops->detect (host); + } + } host->detect_change =0 ;/* if there still is a card present, stop here */ drivers/mmc/core/core.h +1
-0@@ -78,6 +78,7 @@ void _mmc_detect_change(struct mmc_host *host, unsigned long delay,int _mmc_detect_card_removed (struct mmc_host *host);int mmc_detect_card_removed (struct mmc_host *host); +int mmc_detect_mmc (struct mmc_host *host);int mmc_attach_mmc (struct mmc_host *host);int mmc_attach_sd (struct mmc_host *host);int mmc_attach_sdio (struct mmc_host *host);drivers/mmc/core/mmc.c +8
-1@@ -2316,6 +2316,14 @@ static const struct mmc_bus_ops mmc_ops = { .flush_cache = _mmc_flush_cache, }; +/* +* Export function for mmc detection +*/ +int mmc_detect_mmc (struct mmc_host *host){ +mmc_detect (host); +return 0 ; +} +/* * Starting point for MMC card init. */ --2.34.1
A message that is not in the archive (sent off-list or not yet received): nyiodgyzyb6f2kkjlvjdlfflfkdccr2lgnij2uxkdm4mrpznf5@srcxlmdh47da
Quoted 161 lines · cuu
Quoted 85 lines · Ulf Hansson
Quoted 9 lines · cuu
Previously, SD cards using pure SPI protocol with
mmc_spicould not reliably detect card removal via cd-gpios. This patch fixes the issue by extending the card alive check in_mmc_detect_card_removed()to include cd-gpios detection.Additionally,
mmc_rescan()now includes a status probing function specific to SPI SD cards to ensure the card state is correctly updated on insertion and removal events.Sorry, I don't get this.
What is so special with SPI cards here that makes the regular card detection/removal procedures not to work?
Is there something wrong with
mmc_sd_alive()?Quoted 2 lines
Signed-off-by cuu <[email protected]>Please use a proper name for your sob line. I assume "cuu" is an alias, no?
Kind regards
UffeQuoted 59 lines
--- drivers/mmc/core/core.c | 15 ++++++++++++--- drivers/mmc/core/core.h | 1 + drivers/mmc/core/mmc.c | 8 ++++++++ 3 files changed, 21 insertions(+), 3 deletions(-)drivers/mmc/core/core.c +12
-3@@ -2123,6 +2123,11 @@ int _mmc_detect_card_removed(struct mmc_host *host) ret = host->bus_ops->alive (host); +if (mmc_host_is_spi (host)) { +if (host->ops->get_cd){ + ret = !host->ops->get_cd (host); + } + }/* * Card detect status and alive check may be out of sync if card is * removed slowly, when card detect switch changes while card/slot @@ -2238,9 +2243,13 @@ void mmc_rescan(struct work_struct *work) }/* Verify a registered card to be functional, else remove it. */ -if (host->bus_ops) - host->bus_ops->detect (host); - +if (host->bus_ops){ +if (mmc_host_is_spi (host)){ +mmc_detect_mmc (host); + }else { + host->bus_ops->detect (host); + } + } host->detect_change =0 ;/* if there still is a card present, stop here */ drivers/mmc/core/core.h +1
-0@@ -78,6 +78,7 @@ void _mmc_detect_change(struct mmc_host *host, unsigned long delay,int _mmc_detect_card_removed (struct mmc_host *host);int mmc_detect_card_removed (struct mmc_host *host); +int mmc_detect_mmc (struct mmc_host *host);int mmc_attach_mmc (struct mmc_host *host);int mmc_attach_sd (struct mmc_host *host);int mmc_attach_sdio (struct mmc_host *host);drivers/mmc/core/mmc.c +0
-0@@ -2316,6 +2316,14 @@ static const struct mmc_bus_ops mmc_ops = { .flush_cache = _mmc_flush_cache, };Quoted 14 lines
Quoted 12 lines
drivers/mmc/core/mmc.c +8
-1+/* +* Export function for mmc detection +*/ +int mmc_detect_mmc (struct mmc_host *host){ +mmc_detect (host); +return 0 ; +} +/* * Starting point for MMC card init. */ --Quoted 2 lines
2.34.1
Hi Ulf
I worked On a PicoCalc board I added an extension board for the Raspberry Pi Zero 2W, while still wanting the Zero 2W to access the PicoCalc’s own SPI-connected SD card.
Here is the DTS I wrote:
``` /dts-v1/; /plugin/; / { compatible = "brcm,bcm2835"; fragment@0 { target = <&spidev0>; __overlay__ { status = "disabled"; }; }; fragment@1 { target = <&spi1>; __overlay__ { status = "okay"; #address-cells = <1>; #size-cells = <0>; sdslot@0 { compatible = "mmc-spi-slot"; reg = <0>; // SPI CS0 voltage-ranges = <3300 3300>; spi-max-frequency = <8000000>; gpios = <&gpio 6 1>; cd-gpios = <&gpio 6 1>; }; }; }; }; ```With this setup, card insertion is detected, but removal is not. From what I can tell,
mmc_sd_alive()itself is fine, but it seems does not check the state ofcd-gpios.For removable SPI SD cards that provide
cd-gpios, I believe relying on thecd-gpiosstate may be a more direct and accurate way to determine card presence.Therefore, I added a check in
_mmc_detect_card_removed()to handle the case of SPI-protocol cards withcd-gpios, so that removal detection can work properly.I am not deeply familiar with the Linux kernel, so if I am wrong please correct me. Thanks a lot.
Ps:
You are right and I am sorry, my sob line was wrong the correct version is
Quoted 9 lines
``` Signed-off-by: dexter kidd <[email protected]> ```I don't know how to fix the sob line, re-send a patch to you again?
Kind regards
Resending my previous reply with the full CC list this time. Apologies for the noise.
Quoted 131 lines · cuu
Quoted 129 lines · cuu
Quoted 85 lines · Ulf Hansson
Quoted 9 lines · cuu
Previously, SD cards using pure SPI protocol with
mmc_spicould not reliably detect card removal via cd-gpios. This patch fixes the issue by extending the card alive check in_mmc_detect_card_removed()to include cd-gpios detection.Additionally,
mmc_rescan()now includes a status probing function specific to SPI SD cards to ensure the card state is correctly updated on insertion and removal events.Sorry, I don't get this.
What is so special with SPI cards here that makes the regular card detection/removal procedures not to work?
Is there something wrong with
mmc_sd_alive()?Quoted 2 lines
Signed-off-by cuu <[email protected]>Please use a proper name for your sob line. I assume "cuu" is an alias, no?
Kind regards
UffeQuoted 59 lines
--- drivers/mmc/core/core.c | 15 ++++++++++++--- drivers/mmc/core/core.h | 1 + drivers/mmc/core/mmc.c | 8 ++++++++ 3 files changed, 21 insertions(+), 3 deletions(-)drivers/mmc/core/core.c +12
-3@@ -2123,6 +2123,11 @@ int _mmc_detect_card_removed(struct mmc_host *host) ret = host->bus_ops->alive (host); +if (mmc_host_is_spi (host)) { +if (host->ops->get_cd){ + ret = !host->ops->get_cd (host); + } + }/* * Card detect status and alive check may be out of sync if card is * removed slowly, when card detect switch changes while card/slot @@ -2238,9 +2243,13 @@ void mmc_rescan(struct work_struct *work) }/* Verify a registered card to be functional, else remove it. */ -if (host->bus_ops) - host->bus_ops->detect (host); - +if (host->bus_ops){ +if (mmc_host_is_spi (host)){ +mmc_detect_mmc (host); + }else { + host->bus_ops->detect (host); + } + } host->detect_change =0 ;/* if there still is a card present, stop here */ drivers/mmc/core/core.h +1
-0@@ -78,6 +78,7 @@ void _mmc_detect_change(struct mmc_host *host, unsigned long delay,int _mmc_detect_card_removed (struct mmc_host *host);int mmc_detect_card_removed (struct mmc_host *host); +int mmc_detect_mmc (struct mmc_host *host);int mmc_attach_mmc (struct mmc_host *host);int mmc_attach_sd (struct mmc_host *host);int mmc_attach_sdio (struct mmc_host *host);drivers/mmc/core/mmc.c +0
-0@@ -2316,6 +2316,14 @@ static const struct mmc_bus_ops mmc_ops = { .flush_cache = _mmc_flush_cache, };Quoted 14 lines
Quoted 12 lines
drivers/mmc/core/mmc.c +8
-1+/* +* Export function for mmc detection +*/ +int mmc_detect_mmc (struct mmc_host *host){ +mmc_detect (host); +return 0 ; +} +/* * Starting point for MMC card init. */ --Quoted 2 lines
2.34.1
Hi Ulf
I worked On a PicoCalc board I added an extension board for the Raspberry Pi Zero 2W, while still wanting the Zero 2W to access the PicoCalc’s own SPI-connected SD card.
Here is the DTS I wrote:
```
/dts-v1/;
/plugin/;/ { compatible = "brcm,bcm2835"; fragment@0 { target = <&spidev0>; __overlay__ { status = "disabled"; }; }; fragment@1 { target = <&spi1>; __overlay__ { status = "okay"; #address-cells = <1>; #size-cells = <0>;
Quoted 12 lines
Quoted 11 lines
sdslot@0 { compatible = "mmc-spi-slot"; reg = <0>; // SPI CS0 voltage-ranges = <3300 3300>; spi-max-frequency = <8000000>; gpios = <&gpio 6 1>; cd-gpios = <&gpio 6 1>; }; }; }; };Quoted 1 line
```
Thanks for sharing this information!
Quoted 8 lines
Quoted 8 lines
With this setup, card insertion is detected, but removal is not. From what I can tell,
mmc_sd_alive()itself is fine, but it seems does not check the state ofcd-gpios.For removable SPI SD cards that provide
cd-gpios, I believe relying on thecd-gpiosstate may be a more direct and accurate way to determine card presence.
Sending a CMD13 to poll for the card should work as well, I think.
Or maybe there is something specific with SPI here?
Quoted 4 lines
Quoted 4 lines
Therefore, I added a check in
_mmc_detect_card_removed()to handle the case of SPI-protocol cards withcd-gpios, so that removal detection can work properly.
I see. I am not sure (yet), that this is really the problem.
I had a look at the mmc_spi_probe() and there are problems with the order of how things are being registered. In general, mmc_gpiod_request_cd() should be called *before* calling mmc_add_host(), but I assume there are some legacy reasons as to why this isn't the case here.
That said, can you check if you are receiving card detect/removal interrupts and thus mmc_spi_detect_irq() should be invoked, when you insert/remove the card?
Does the card get detected if you start without a card in the slot when you boot and insert it later?
If you are not using the card detect interrupt, MMC_CAP_NEEDS_POLL must be set. Can you check if this is the case?
Quoted 3 lines
Quoted 3 lines
I am not deeply familiar with the Linux kernel, so if I am wrong please correct me. Thanks a lot.
No worries, I will try to help you as much as I can.
[...]
Kind regards
Uffe
Quoted 153 lines · Ulf Hansson
Quoted 131 lines · cuu
Quoted 129 lines · cuu
Quoted 85 lines · Ulf Hansson
Quoted 9 lines · cuu
Previously, SD cards using pure SPI protocol with
mmc_spicould not reliably detect card removal via cd-gpios. This patch fixes the issue by extending the card alive check in_mmc_detect_card_removed()to include cd-gpios detection.Additionally,
mmc_rescan()now includes a status probing function specific to SPI SD cards to ensure the card state is correctly updated on insertion and removal events.Sorry, I don't get this.
What is so special with SPI cards here that makes the regular card detection/removal procedures not to work?
Is there something wrong with
mmc_sd_alive()?Quoted 2 lines
Signed-off-by cuu <[email protected]>Please use a proper name for your sob line. I assume "cuu" is an alias, no?
Kind regards
UffeQuoted 59 lines
--- drivers/mmc/core/core.c | 15 ++++++++++++--- drivers/mmc/core/core.h | 1 + drivers/mmc/core/mmc.c | 8 ++++++++ 3 files changed, 21 insertions(+), 3 deletions(-)drivers/mmc/core/core.c +12
-3@@ -2123,6 +2123,11 @@ int _mmc_detect_card_removed(struct mmc_host *host) ret = host->bus_ops->alive (host); +if (mmc_host_is_spi (host)) { +if (host->ops->get_cd){ + ret = !host->ops->get_cd (host); + } + }/* * Card detect status and alive check may be out of sync if card is * removed slowly, when card detect switch changes while card/slot @@ -2238,9 +2243,13 @@ void mmc_rescan(struct work_struct *work) }/* Verify a registered card to be functional, else remove it. */ -if (host->bus_ops) - host->bus_ops->detect (host); - +if (host->bus_ops){ +if (mmc_host_is_spi (host)){ +mmc_detect_mmc (host); + }else { + host->bus_ops->detect (host); + } + } host->detect_change =0 ;/* if there still is a card present, stop here */ drivers/mmc/core/core.h +1
-0@@ -78,6 +78,7 @@ void _mmc_detect_change(struct mmc_host *host, unsigned long delay,int _mmc_detect_card_removed (struct mmc_host *host);int mmc_detect_card_removed (struct mmc_host *host); +int mmc_detect_mmc (struct mmc_host *host);int mmc_attach_mmc (struct mmc_host *host);int mmc_attach_sd (struct mmc_host *host);int mmc_attach_sdio (struct mmc_host *host);drivers/mmc/core/mmc.c +0
-0@@ -2316,6 +2316,14 @@ static const struct mmc_bus_ops mmc_ops = { .flush_cache = _mmc_flush_cache, };Quoted 14 lines
Quoted 12 lines
drivers/mmc/core/mmc.c +8
-1+/* +* Export function for mmc detection +*/ +int mmc_detect_mmc (struct mmc_host *host){ +mmc_detect (host); +return 0 ; +} +/* * Starting point for MMC card init. */ --Quoted 2 lines
2.34.1
Hi Ulf
I worked On a PicoCalc board I added an extension board for the Raspberry Pi Zero 2W, while still wanting the Zero 2W to access the PicoCalc’s own SPI-connected SD card.
Here is the DTS I wrote:
```
/dts-v1/;
/plugin/;/ { compatible = "brcm,bcm2835"; fragment@0 { target = <&spidev0>; __overlay__ { status = "disabled"; }; }; fragment@1 { target = <&spi1>; __overlay__ { status = "okay"; #address-cells = <1>; #size-cells = <0>;Quoted 12 lines
Quoted 11 lines
sdslot@0 { compatible = "mmc-spi-slot"; reg = <0>; // SPI CS0 voltage-ranges = <3300 3300>; spi-max-frequency = <8000000>; gpios = <&gpio 6 1>; cd-gpios = <&gpio 6 1>; }; }; }; };Quoted 1 line
```
Thanks for sharing this information!
Quoted 5 lines
Quoted 5 lines
With this setup, card insertion is detected, but removal is not. From what I can tell,
mmc_sd_alive()itself is fine, but it seems does not check the state ofcd-gpios.
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
For removable SPI SD cards that provide
cd-gpios, I believe relying on thecd-gpiosstate may be a more direct and accurate way to determine card presence.Sending a CMD13 to poll for the card should work as well, I think.
Or maybe there is something specific with SPI here?
There is nothing specific with SPI here.
I mentioned pure SPI because I don't think any SDIO high speed based sd card will have problems with linux mmc drivers since we used it for a long time.
But I am facing a simple SPI (4 wire, SDA SCL VCC GND) based card slot problem with detecting card removal.
I think CMD13 is not enough since we have cd-gpios here , I guess CMD13 is like to wait for it to be timeout so that the system will think the card is gone,right ?
but if we configured cd-gpios,then why wait for CMD13 ?
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Therefore, I added a check in
_mmc_detect_card_removed()to handle the case of SPI-protocol cards withcd-gpios, so that removal detection can work properly.
Quoted 7 lines
I see. I am not sure (yet), that this is really the problem.
I had a look at the
mmc_spi_probe()and there are problems with the order of how things are being registered. In general,mmc_gpiod_request_cd()should be called *before* callingmmc_add_host(), but I assume there are some legacy reasons as to why this isn't the case here.
yeah, I guess so , so my first intention is not to break these legacy codes.
Quoted 3 lines
That said, can you check if you are receiving card detect/removal interrupts and thus
mmc_spi_detect_irq()should be invoked, when you insert/remove the card?
In default , mmc_spi_detect_irq() won't be invoked here I've changed a little of my dts to force IRQ happen
```
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2835";
fragment@0 {
target = <&spidev0>;
__overlay__ {
status = "disabled";
};
};
fragment@1 {
target = <&spi1>;
__overlay__ {
status = "okay";
#address-cells = <1>;
#size-cells = <0>;
sdslot@0 {
compatible = "mmc-spi-slot";
reg = <0>; // SPI CS0
voltage-ranges = <3300 3300>;
spi-max-frequency = <8000000>;
gpios = <&gpio 6 1>;
cd-gpios = <&gpio 6 1>;
interrupts-extended = <&gpio 6 1>;
};
};
};
};
```added one line interrupts-extended
so that we can see interrupts to invoke mmc_spi_detect_irq with different conditions
in dmesg | grep mmc ( I added a pr_info() in mmc_spi_detect_irq )
* interrupts-extended = <&gpio 6 2>;
insert invoked mmc_spi_detect_irq()
eject did not invoke mmc_spi_detect_irq()
* interrupts-extended = <&gpio 6 1>;
insert invoked mmc_spi_detect_irq()
eject the first time, won't invoke mmc_spi_detect_irq()
insert again and eject
now every time when inserting or ejecting, we can get a mmc_spi_detect_irq() invoked
It is a little bit weird,but that is what happened here
but still, linux did not remove the node /dev/mmcblk2( my spi sd card node)
* interrupts-extended = <&gpio 6 3>;
same as above
Quoted 2 lines
Does the card get detected if you start without a card in the slot when you boot and insert it later?
Yes, the sd card got detected when I inserted it after the system booted up.
Quoted 2 lines
If you are not using the card detect interrupt,
MMC_CAP_NEEDS_POLLmust be set. Can you check if this is the case?
By default MMC_CAP_NEEDS_POLL is not enabled here so I added one line
```
@@ -1281,6 +1282,8 @@ static int mmc_spi_probe(struct spi_device *spi)
mmc->caps &= ~MMC_CAP_NEEDS_POLL;
mmc_gpiod_request_cd_irq(mmc);
}
+ mmc->caps |= MMC_CAP_NEEDS_POLL;
+
mmc_detect_change(mmc, 0);
/* Index 1 is write protect/read only */
```in mmc_spi_probe() to force it to be enabled.
and tested it without IRQ modified dts
dmesg | grep mmc shows
``` [ 9.186471] mmc_spi spi1.0: SD/MMC host mmc2, no WP, no poweroff, cd polling [ 9.234981] mmc2: host does not support reading read-only switch, assuming write-enable [ 9.235039] mmc2: new SDHC card on SPI [ 9.238720] mmcblk2: mmc2:0000 LX32G 29.5 GiB [ 9.256975] mmcblk2: p1 p2 [ 9.263982] mmcblk2: mmc2:0000 LX32G 29.5 GiB (quirks 0x40000000) ```
as we can see , cd polling showed up
but polling is not working as we expected
nothing happened when I popped out my card, /dev/mmcblk2p* still remains in linux.
no more other lines in dmesg ,too
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
I am not deeply familiar with the Linux kernel, so if I am wrong please correct me. Thanks a lot.
No worries, I will try to help you as much as I can.
[...]
Kind regards
Uffe
All my testings are under rpi zero 2w with
Linux PicoCalc 6.17.0-rc6-v7+ #2 SMP Wed Sep 17 15:21:08 CST 2025 armv7l GNU/Linux
Kind regards
dexter kidd
Quoted 169 lines · dexter K
Quoted 153 lines · Ulf Hansson
Quoted 131 lines · cuu
Quoted 129 lines · cuu
Quoted 85 lines · Ulf Hansson
Quoted 9 lines · cuu
Previously, SD cards using pure SPI protocol with
mmc_spicould not reliably detect card removal via cd-gpios. This patch fixes the issue by extending the card alive check in_mmc_detect_card_removed()to include cd-gpios detection.Additionally,
mmc_rescan()now includes a status probing function specific to SPI SD cards to ensure the card state is correctly updated on insertion and removal events.Sorry, I don't get this.
What is so special with SPI cards here that makes the regular card detection/removal procedures not to work?
Is there something wrong with
mmc_sd_alive()?Quoted 2 lines
Signed-off-by cuu <[email protected]>Please use a proper name for your sob line. I assume "cuu" is an alias, no?
Kind regards
UffeQuoted 59 lines
--- drivers/mmc/core/core.c | 15 ++++++++++++--- drivers/mmc/core/core.h | 1 + drivers/mmc/core/mmc.c | 8 ++++++++ 3 files changed, 21 insertions(+), 3 deletions(-)drivers/mmc/core/core.c +12
-3@@ -2123,6 +2123,11 @@ int _mmc_detect_card_removed(struct mmc_host *host) ret = host->bus_ops->alive (host); +if (mmc_host_is_spi (host)) { +if (host->ops->get_cd){ + ret = !host->ops->get_cd (host); + } + }/* * Card detect status and alive check may be out of sync if card is * removed slowly, when card detect switch changes while card/slot @@ -2238,9 +2243,13 @@ void mmc_rescan(struct work_struct *work) }/* Verify a registered card to be functional, else remove it. */ -if (host->bus_ops) - host->bus_ops->detect (host); - +if (host->bus_ops){ +if (mmc_host_is_spi (host)){ +mmc_detect_mmc (host); + }else { + host->bus_ops->detect (host); + } + } host->detect_change =0 ;/* if there still is a card present, stop here */ drivers/mmc/core/core.h +1
-0@@ -78,6 +78,7 @@ void _mmc_detect_change(struct mmc_host *host, unsigned long delay,int _mmc_detect_card_removed (struct mmc_host *host);int mmc_detect_card_removed (struct mmc_host *host); +int mmc_detect_mmc (struct mmc_host *host);int mmc_attach_mmc (struct mmc_host *host);int mmc_attach_sd (struct mmc_host *host);int mmc_attach_sdio (struct mmc_host *host);drivers/mmc/core/mmc.c +0
-0@@ -2316,6 +2316,14 @@ static const struct mmc_bus_ops mmc_ops = { .flush_cache = _mmc_flush_cache, };Quoted 14 lines
Quoted 12 lines
drivers/mmc/core/mmc.c +8
-1+/* +* Export function for mmc detection +*/ +int mmc_detect_mmc (struct mmc_host *host){ +mmc_detect (host); +return 0 ; +} +/* * Starting point for MMC card init. */ --Quoted 2 lines
2.34.1
Hi Ulf
I worked On a PicoCalc board I added an extension board for the Raspberry Pi Zero 2W, while still wanting the Zero 2W to access the PicoCalc’s own SPI-connected SD card.
Here is the DTS I wrote:
```
/dts-v1/;
/plugin/;/ { compatible = "brcm,bcm2835"; fragment@0 { target = <&spidev0>; __overlay__ { status = "disabled"; }; }; fragment@1 { target = <&spi1>; __overlay__ { status = "okay"; #address-cells = <1>; #size-cells = <0>;Quoted 12 lines
Quoted 11 lines
sdslot@0 { compatible = "mmc-spi-slot"; reg = <0>; // SPI CS0 voltage-ranges = <3300 3300>; spi-max-frequency = <8000000>; gpios = <&gpio 6 1>; cd-gpios = <&gpio 6 1>; }; }; }; };Quoted 1 line
```
Thanks for sharing this information!
Quoted 5 lines
Quoted 5 lines
With this setup, card insertion is detected, but removal is not. From what I can tell,
mmc_sd_alive()itself is fine, but it seems does not check the state ofcd-gpios.Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
For removable SPI SD cards that provide
cd-gpios, I believe relying on thecd-gpiosstate may be a more direct and accurate way to determine card presence.Sending a CMD13 to poll for the card should work as well, I think.
Or maybe there is something specific with SPI here?
There is nothing specific with SPI here.
I mentioned pure SPI because I don't think any SDIO high speed based sd card will have problems with linux mmc drivers since we used it for a long time.
Quoted 6 lines
But I am facing a simple SPI (4 wire, SDA SCL VCC GND) based card slot problem with detecting card removal.
I think CMD13 is not enough since we have cd-gpios here , I guess CMD13 is like to wait for it to be timeout so that the system will think the card is gone,right ?
Correct!
Quoted 1 line
but if we configured cd-gpios,then why wait for CMD13 ?
CMD13 should be really quick, even in the timeout case we should only wait for a few us.
Of course, if there is a problem with the mmc host controller/driver, the timeout could be longer.
Quoted 67 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Therefore, I added a check in
_mmc_detect_card_removed()to handle the case of SPI-protocol cards withcd-gpios, so that removal detection can work properly.Quoted 7 lines
I see. I am not sure (yet), that this is really the problem.
I had a look at the
mmc_spi_probe()and there are problems with the order of how things are being registered. In general,mmc_gpiod_request_cd()should be called *before* callingmmc_add_host(), but I assume there are some legacy reasons as to why this isn't the case here.yeah, I guess so , so my first intention is not to break these legacy codes.
Quoted 3 lines
That said, can you check if you are receiving card detect/removal interrupts and thus
mmc_spi_detect_irq()should be invoked, when you insert/remove the card?In default ,
mmc_spi_detect_irq()won't be invoked here I've changed a little of my dts to force IRQ happen``` /dts-v1/; /plugin/; / { compatible = "brcm,bcm2835"; fragment@0 { target = <&spidev0>; __overlay__ { status = "disabled"; }; }; fragment@1 { target = <&spi1>; __overlay__ { status = "okay"; #address-cells = <1>; #size-cells = <0>; sdslot@0 { compatible = "mmc-spi-slot"; reg = <0>; // SPI CS0 voltage-ranges = <3300 3300>; spi-max-frequency = <8000000>; gpios = <&gpio 6 1>; cd-gpios = <&gpio 6 1>; interrupts-extended = <&gpio 6 1>; }; }; }; }; ```added one line
interrupts-extended
so that we can see interrupts to invokemmc_spi_detect_irqwith different conditions
in dmesg | grep mmc ( I added apr_info()inmmc_spi_detect_irq)* interrupts-extended = <&gpio 6 2>;
insert invokedmmc_spi_detect_irq()
eject did not invokemmc_spi_detect_irq()
Quoted 8 lines
* interrupts-extended = <&gpio 6 1>;
insert invokedmmc_spi_detect_irq()
eject the first time, won't invokemmc_spi_detect_irq()
insert again and eject
now every time when inserting or ejecting, we can get ammc_spi_detect_irq()invoked
It is a little bit weird,but that is what happened here
but still, linux did not remove the node /dev/mmcblk2( my spi sd card node)
Okay, so if we are not receiving an irq at card-removal this becomes a problem. If it's because of GPIO or pinctrl settings needs to be adjusted, I can't tell, because of limited HW knowledge in this case.
Nevertheless, if there are no irq available, we should fallback to using the polling mode with CMD13 (MMC_CAP_NEEDS_POLL).
That said, to me, it looks like we should start by trying to make the polling mode work first. If it doesn't, we need to understand why.
Quoted 16 lines
* interrupts-extended = <&gpio 6 3>;
same as aboveQuoted 2 lines
Does the card get detected if you start without a card in the slot when you boot and insert it later?
Yes, the sd card got detected when I inserted it after the system booted up.
Quoted 2 lines
If you are not using the card detect interrupt,
MMC_CAP_NEEDS_POLLmust be set. Can you check if this is the case?By default
MMC_CAP_NEEDS_POLLis not enabled here so I added one line ```
Review static int mmc_spi_probe(struct spi_device *spi) · line 1286
Quoted 5 lines
(quoted diff) +2
-0@@ -1281,6 +1282,8 @@ static int mmc_spi_probe(struct spi_device *spi) mmc->caps &= ~MMC_CAP_NEEDS_POLL; mmc_gpiod_request_cd_irq(mmc); } + mmc->caps |= MMC_CAP_NEEDS_POLL; +
This needs to be set before calling mmc_add_host(). And beyond that, make sure we don't request for GPIO irq (don't call mmc_gpiod_request_cd_irq()), to make sure we only test polling mode separately.
Moreover, you may need to make changes in drivers/mmc/host/of_mmc_spi.c to avoid this too. Huh, this code is messy, sorry!
Quoted 16 lines
(quoted diff) +0
-0mmc_detect_change(mmc, 0); /* Index 1 is write protect/read only */``` in mmc_spi_probe() to force it to be enabled. and tested it without IRQ modified dts dmesg | grep mmc shows ``` [ 9.186471] mmc_spi spi1.0: SD/MMC host mmc2, no WP, no poweroff, cd polling [ 9.234981] mmc2: host does not support reading read-only switch, assuming write-enable [ 9.235039] mmc2: new SDHC card on SPI [ 9.238720] mmcblk2: mmc2:0000 LX32G 29.5 GiB [ 9.256975] mmcblk2: p1 p2 [ 9.263982] mmcblk2: mmc2:0000 LX32G 29.5 GiB (quirks 0x40000000)
Quoted 7 lines
```
as we can see , cd polling showed upbut polling is not working as we expected
nothing happened when I popped out my card,/dev/mmcblk2p*still remains in linux.
no more other lines in dmesg ,too
Could it be that the CMD13 is hanging when there is no card inserted in the spi-slot?
Quoted 12 lines
Quoted 11 lines
Quoted 4 lines
Quoted 4 lines
I am not deeply familiar with the Linux kernel, so if I am wrong please correct me. Thanks a lot.
No worries, I will try to help you as much as I can.
[...]
Kind regards
Uffe
Quoted 7 lines
All my testings are under rpi zero 2w with
Linux PicoCalc 6.17.0-rc6-v7+ #2 SMP Wed Sep 17 15:21:08 CST 2025 armv7l GNU/LinuxKind regards
dexter kidd
Apologize for the delay!
Kind regards
Uffe