mmc: core/sdhci: enhance card detect code
cover mmc: core/sdhci: enhance card detect code
Patch 1 enhanced card removal judgement code to resolve the missing removal event issue when card is removed slowly.
Patch 2 added callback function get_cd accordingly in sdhci.c. Patch 3 update some card detect related code in sdhci.c and used patch 2.
patch 1 and 2 are same as previous version except adding Reviewed-by/Acked-by/Tested-by.
patch 3 is added to this patchset in this version.
Kevin Liu (3)
[PATCH v8 1/3] mmc: core: enhance card removal judgement for slow [PATCH v4 2/3] mmc: sdhci: add function get_cd
[PATCH v3 3/3] mmc: sdhci: update check code for cd-broken or
drivers/mmc/core/core.c | 13 ++++++++++++ drivers/mmc/host/sdhci.c | 51 ++++++++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 17 deletions(-)
PATCH v8 1/3 mmc: core: enhance card removal judgement for slow removal
Function _mmc_detect_card_removed will be called to know whether the card is still present when host->bus_ops->detect is called. In current code, the return value of this function generally only depend on the result of sending cmd13 to card, which may not safe for card with detection support like slot gpio detection. Because the communication status between host and card may out of sync with the detect status if remove the card slowly or hands shake during the process. The direct reason is the async between card detect switch and card/slot pad contaction in hardware, which is defined by spec.
The spec define card insert/remove sequence as below (both standard size SD card and MicroSD card have the same sequence):
"Part 1 Standard Size SD Card Mechanical Addendum Ver4.00 Final, Appendix C: Card Detection Switch" (Take normally open type as example) a)SD card insertion sequence:
The card detection switch should be turned on after all SD card contact pads are connected to the host connector contact pads.
b)SD removal sequence:
The card detection switch should be turned off when the SD card is just going to be removed and before any SD card contact pad is disconnected from the host connector contact pad.
Below is the sequence when this issue occur (Take slot gpio detection as example and remove the card slowly during the process):
1. gpio level changed and card detect interrupt triggered.
2. mmc_rescan was launched.
3. the card pads were still contacted with the slot pads because of slow removal. So _mmc_detect_card_removed and mmc_rescan think card was still present (cmd13 succeed).
4. card pads were discontacted from the card slot pads.
So the card was actually removed finally but the card removal event has been missed by system.
The interval length between step 1 and step 4 depends on the card removal speed. If it's longer than the detect work schedule delay which is 200ms, this issue will likely happen.
This patch add the card detect status check in function _mmc_detect_card_removed if cmd13 check succeed and host->ops->get_cd provided. If get_cd detect no card present then schedule another detect work 200ms later.
--- drivers/mmc/core/core.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/mmc/core/core.c +13 -0
@@ -2289,6 +2289,19 @@ int _mmc_detect_card_removed(struct mmc_host *host)return 1 ; ret = host->bus_ops->alive (host); +/* +* Card detect status and alive check may out of sync if card is +* removed slowly when card detect switch changed while card/slot +* pads still contacted in hardware.(refer to "SD Card Mechanical +* Addendum, Appendix C: Card Detection Switch") So reschedule a +* detect work 200ms later for this case. +*/ +if (!ret && host->ops->get_cd && !host->ops->get_cd (host)) { +mmc_detect_change (host,msecs_to_jiffies (200 )); +pr_debug ("%s: card remove too slowly\n" , +mmc_hostname (host)); + } +if (ret) {mmc_card_set_removed (host->card);pr_debug ("%s: card remove detected\n" ,mmc_hostname (host));
Signature · 1 line
1.7.9.5
Re: [PATCH v8 1/3] mmc: core: enhance card removal judgement for slow removal
Quoted 4 lines · Kevin Liu
This patch add the card detect status check in function
_mmc_detect_card_removedif cmd13 check succeed andhost->ops->get_cdprovided. Ifget_cddetect no card present then schedule another detect work 200ms later.
(On the Nomadik)
Yours,
Linus Walleij
PATCH v4 2/3 mmc: sdhci: add function get_cd
In reply to Kevin Liu1. mmc_rescan will call get_cd to know whether the card is present before mmc_rescan_try_freq to avoid useless trials during card removal or start host is called when card is not present.
2. get_cd need to be checked to resolve slow card removal issue.
--- drivers/mmc/host/sdhci.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
drivers/mmc/host/sdhci.c +30 -0
@@ -1581,6 +1581,35 @@ static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)sdhci_runtime_pm_put (host); } +static int sdhci_do_get_cd (struct sdhci_host *host) +{ +int gpio_cd; + +if (host->flags &SDHCI_DEVICE_DEAD ) +return 0 ; +/* If polling/nonremovable, assume that the card is always present. */ +if ((host->quirks &SDHCI_QUIRK_BROKEN_CARD_DETECTION ) || + (host->mmc->caps &MMC_CAP_NONREMOVABLE )) +return 1 ; +/* Try slot gpio detect */ + gpio_cd =mmc_gpio_get_cd (host->mmc); +if (!IS_ERR_VALUE (gpio_cd)) +return !!gpio_cd; +/* Host native card detect */ +return !!(sdhci_readl (host,SDHCI_PRESENT_STATE ) &SDHCI_CARD_PRESENT ); +} + +static int sdhci_get_cd (struct mmc_host *mmc) +{ +struct sdhci_host *host =mmc_priv (mmc); +int ret; + +sdhci_runtime_pm_get (host); + ret =sdhci_do_get_cd (host); +sdhci_runtime_pm_put (host); +return ret; +} +static int sdhci_check_ro (struct sdhci_host *host) {unsigned long flags;
@@ -2038,6 +2067,7 @@ static void sdhci_card_event(struct mmc_host *mmc)static const struct mmc_host_ops sdhci_ops = { .request = sdhci_request, .set_ios = sdhci_set_ios, + .get_cd = sdhci_get_cd, .get_ro = sdhci_get_ro, .hw_reset = sdhci_hw_reset, .enable_sdio_irq = sdhci_enable_sdio_irq,
Signature · 1 line
1.7.9.5
PATCH v3 3/3 mmc: sdhci: update check code for cd-broken or nonremovable card
In reply to Kevin Liucard present check in function sdhci_request:
1. If quirk SDHCI_QUIRK_BROKEN_CARD_DETECTION is selected, there is no card detection available and polling must be used. Gpio detect must _not_ be supported under this case.
Change the sequence to check SDHCI_QUIRK_BROKEN_CARD_DETECTION first, which make code more clear.
2. nonremovable card should be also assumed always present.
3. change to call sdhci_do_get_cd directly
check in function sdhci_enable_irq_wakeups:
1. nonremovable card should be also assumed _no_ insert/remove wakeup needed like some sdio cards which is nonremovable and can wakeup host only by card int.
--- drivers/mmc/host/sdhci.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-)
drivers/mmc/host/sdhci.c +4 -17
@@ -54,6 +54,7 @@ static void sdhci_finish_command(struct sdhci_host *);static int sdhci_execute_tuning (struct mmc_host *mmc,u32 opcode);static void sdhci_tuning_timer (unsigned long data);static void sdhci_enable_preset_value (struct sdhci_host *host,bool enable); +static int sdhci_do_get_cd (struct sdhci_host *host);#ifdef CONFIG_PM_RUNTIME static int sdhci_runtime_pm_get (struct sdhci_host *host);
@@ -1337,22 +1338,7 @@ static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq) host->mrq = mrq; -/* -* Firstly check card presence from cd-gpio. The return could -* be one of the following possibilities: -* negative: cd-gpio is not available -* zero: cd-gpio is used, and card is removed -* one: cd-gpio is used, and card is present -*/ - present =mmc_gpio_get_cd (host->mmc); -if (present <0 ) { -/* If polling, assume that the card is always present. */ -if (host->quirks &SDHCI_QUIRK_BROKEN_CARD_DETECTION ) - present =1 ; -else - present =sdhci_readl (host,SDHCI_PRESENT_STATE ) & -SDHCI_CARD_PRESENT ; - } + present =sdhci_do_get_cd (host);if (!present || host->flags &SDHCI_DEVICE_DEAD ) { host->mrq->cmd->error = -ENOMEDIUM ;
@@ -2505,7 +2491,8 @@ void sdhci_enable_irq_wakeups(struct sdhci_host *host) val =sdhci_readb (host,SDHCI_WAKE_UP_CONTROL ); val |= mask ;/* Avoid fake wake up */ -if (host->quirks &SDHCI_QUIRK_BROKEN_CARD_DETECTION ) +if ((host->quirks &SDHCI_QUIRK_BROKEN_CARD_DETECTION ) || + (host->mmc->caps &MMC_CAP_NONREMOVABLE )) val &= ~(SDHCI_WAKE_ON_INSERT |SDHCI_WAKE_ON_REMOVE );sdhci_writeb (host, val,SDHCI_WAKE_UP_CONTROL ); }
Signature · 1 line
1.7.9.5