mmc: core: hw_reset changes
v3 cover mmc: core: hw_reset changes
Make the mmc_hw_reset routines more generic, so we can easily add a power cycle of SD cards as well. Also simplify the (e)MMC specific parts of the reset code.
As I don't have an eMMC device myself, much less one with a reset line, I'd be very happy if someone could help me test the code with an eMMC?
Please have a look at the handling of bus_mode in mmc_set_initial_state(), we set it to MMC_BUSMODE_PUSHPULL also when powering off a device. If this is not OK, we'll just have to leave bus_mode out of it.
v3:
- Keep mmc_can_reset
- Always set bus_mode = MMC_BUSMODE_PUSHPULL in mmc_set_initial_state()
v2:
- Call the new bus_ops member hw_reset instead of power_reset
- Create mmc_set_initial_state and call it from mmc_mmc_hw_reset instead of mmc_power_up
- Keep "mmc_hw_reset" naming
Johan Rudholm (3):
mmc: core: consistent handling of initial values
mmc: core: turn hw_reset into a bus_ops
mmc: sd: add hw_reset callback
drivers/mmc/core/core.c | 99 ++++++++++++++++++----------------------------- drivers/mmc/core/core.h | 6 +++ drivers/mmc/core/mmc.c | 40 +++++++++++++++++++ drivers/mmc/core/sd.c | 13 ++++++ 4 files changed, 97 insertions(+), 61 deletions(-)
Signature · 1 line
1.7.2.5
PATCH v3 1/3 mmc: core: consistent handling of initial values
mmc_do_hw_reset(), mmc_power_up() and mmc_power_off() all set similar initial values for bus_mode, bus_width, chip_select and timing. Let's make this handling simpler and more consistent by sticking them together in a common function. This will introduce small changes in behavior in the following places:
mmc_power_off():
For SPI hosts, explicitly set bus_mode = MMC_BUSMODE_PUSHPULL and chip_select = MMC_CS_HIGH, before we left them as they were.
For non-SPI hosts, set bus_mode = MMC_BUSMODE_PUSHPULL instead of MMC_BUSMODE_OPENDRAIN as before.
These two changes should not be a problem since the device will be powered off anyway.
mmc_do_hw_reset():
Always set bus_mode = MMC_BUSMODE_PUSHPULL, as required by SD/SDIO cards. MMC cards require MMC_BUSMODE_OPENDRAIN, but this is taken care of by mmc_init_card() and mmc_attach_mmc().
--- drivers/mmc/core/core.c | 47 ++++++++++++++++++++++------------------------- drivers/mmc/core/core.h | 1 + 2 files changed, 23 insertions(+), 25 deletions(-)
drivers/mmc/core/core.c +22 -25
@@ -1099,6 +1099,22 @@ void mmc_set_bus_width(struct mmc_host *host, unsigned int width)mmc_host_clk_release (host); } +/* +* Set initial state after a power cycle or a hw_reset. +*/ +void mmc_set_initial_state (struct mmc_host *host) +{ +if (mmc_host_is_spi (host)) + host->ios.chip_select =MMC_CS_HIGH ; +else + host->ios.chip_select =MMC_CS_DONTCARE ; + host->ios.bus_mode =MMC_BUSMODE_PUSHPULL ; + host->ios.bus_width =MMC_BUS_WIDTH_1 ; + host->ios.timing =MMC_TIMING_LEGACY ; + +mmc_set_ios (host); +} +/** * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number * @vdd: voltage (mV)
@@ -1537,15 +1553,9 @@ void mmc_power_up(struct mmc_host *host, u32 ocr)mmc_host_clk_hold (host); host->ios.vdd =fls (ocr) -1 ; -if (mmc_host_is_spi (host)) - host->ios.chip_select =MMC_CS_HIGH ; -else - host->ios.chip_select =MMC_CS_DONTCARE ; - host->ios.bus_mode =MMC_BUSMODE_PUSHPULL ; host->ios.power_mode =MMC_POWER_UP ; - host->ios.bus_width =MMC_BUS_WIDTH_1 ; - host->ios.timing =MMC_TIMING_LEGACY ; -mmc_set_ios (host); +/* Set initial state and call mmc_set_ios */ +mmc_set_initial_state (host);/* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */ if (__mmc_set_signal_voltage (host,MMC_SIGNAL_VOLTAGE_330 ) ==0 )
@@ -1585,14 +1595,9 @@ void mmc_power_off(struct mmc_host *host) host->ios.clock =0 ; host->ios.vdd =0 ; -if (!mmc_host_is_spi (host)) { - host->ios.bus_mode =MMC_BUSMODE_OPENDRAIN ; - host->ios.chip_select =MMC_CS_DONTCARE ; - } host->ios.power_mode =MMC_POWER_OFF ; - host->ios.bus_width =MMC_BUS_WIDTH_1 ; - host->ios.timing =MMC_TIMING_LEGACY ; -mmc_set_ios (host); +/* Set initial state and call mmc_set_ios */ +mmc_set_initial_state (host);/* * Some configurations, such as the 802.11 SDIO card in the OLPC
@@ -2278,16 +2283,8 @@ static int mmc_do_hw_reset(struct mmc_host *host, int check) } } -if (mmc_host_is_spi (host)) { - host->ios.chip_select =MMC_CS_HIGH ; - host->ios.bus_mode =MMC_BUSMODE_PUSHPULL ; - }else { - host->ios.chip_select =MMC_CS_DONTCARE ; - host->ios.bus_mode =MMC_BUSMODE_OPENDRAIN ; - } - host->ios.bus_width =MMC_BUS_WIDTH_1 ; - host->ios.timing =MMC_TIMING_LEGACY ; -mmc_set_ios (host); +/* Set initial state and call mmc_set_ios */ +mmc_set_initial_state (host);mmc_host_clk_release (host);
drivers/mmc/core/core.h +1 -0
@@ -49,6 +49,7 @@ void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type);void mmc_power_up (struct mmc_host *host,u32 ocr);void mmc_power_off (struct mmc_host *host);void mmc_power_cycle (struct mmc_host *host,u32 ocr); +void mmc_set_initial_state (struct mmc_host *host);static inline void mmc_delay (unsigned int ms) {
Signature · 1 line
1.7.2.5
Re: [PATCH v3 1/3] mmc: core: consistent handling of initial values
Quoted 17 lines · Johan Rudholm
mmc_do_hw_reset(),mmc_power_up()andmmc_power_off()all set similar initial values forbus_mode,bus_width,chip_selectand timing. Let's make this handling simpler and more consistent by sticking them together in a common function. This will introduce small changes in behavior in the following places:
mmc_power_off():For SPI hosts, explicitly set
bus_mode=MMC_BUSMODE_PUSHPULLandchip_select=MMC_CS_HIGH, before we left them as they were.For non-SPI hosts, set
bus_mode=MMC_BUSMODE_PUSHPULLinstead ofMMC_BUSMODE_OPENDRAINas before.These two changes should not be a problem since the device will be powered off anyway.
Quoted 7 lines
mmc_do_hw_reset():Always set
bus_mode=MMC_BUSMODE_PUSHPULL, as required by SD/SDIO cards. MMC cards requireMMC_BUSMODE_OPENDRAIN, but this is taken care of bymmc_init_card()andmmc_attach_mmc().Signed-off-by Johan Rudholm <[email protected]>
I decided to apply this one now for my next branch, thus we also get some testing of it in linux-next.
If it turns out that it's causing issues, we need to come back to it. For the rest of the patches I need some more time to review.
Thanks and kind regards
Uffe
Quoted 101 lines
--- drivers/mmc/core/core.c | 47 ++++++++++++++++++++++------------------------- drivers/mmc/core/core.h | 1 + 2 files changed, 23 insertions(+), 25 deletions(-)drivers/mmc/core/core.c +22
-25@@ -1099,6 +1099,22 @@ void mmc_set_bus_width(struct mmc_host *host, unsigned int width)mmc_host_clk_release (host); } +/* +* Set initial state after a power cycle or a hw_reset. +*/ +void mmc_set_initial_state (struct mmc_host *host) +{ +if (mmc_host_is_spi (host)) + host->ios.chip_select =MMC_CS_HIGH ; +else + host->ios.chip_select =MMC_CS_DONTCARE ; + host->ios.bus_mode =MMC_BUSMODE_PUSHPULL ; + host->ios.bus_width =MMC_BUS_WIDTH_1 ; + host->ios.timing =MMC_TIMING_LEGACY ; + +mmc_set_ios (host); +} +/** * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number * @vdd: voltage (mV) @@ -1537,15 +1553,9 @@ void mmc_power_up(struct mmc_host *host, u32 ocr)mmc_host_clk_hold (host); host->ios.vdd =fls (ocr) -1 ; -if (mmc_host_is_spi (host)) - host->ios.chip_select =MMC_CS_HIGH ; -else - host->ios.chip_select =MMC_CS_DONTCARE ; - host->ios.bus_mode =MMC_BUSMODE_PUSHPULL ; host->ios.power_mode =MMC_POWER_UP ; - host->ios.bus_width =MMC_BUS_WIDTH_1 ; - host->ios.timing =MMC_TIMING_LEGACY ; -mmc_set_ios (host); +/* Set initial state and call mmc_set_ios */ +mmc_set_initial_state (host);/* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */ if (__mmc_set_signal_voltage (host,MMC_SIGNAL_VOLTAGE_330 ) ==0 )@@ -1585,14 +1595,9 @@ void mmc_power_off(struct mmc_host *host) host->ios.clock =0 ; host->ios.vdd =0 ; -if (!mmc_host_is_spi (host)) { - host->ios.bus_mode =MMC_BUSMODE_OPENDRAIN ; - host->ios.chip_select =MMC_CS_DONTCARE ; - } host->ios.power_mode =MMC_POWER_OFF ; - host->ios.bus_width =MMC_BUS_WIDTH_1 ; - host->ios.timing =MMC_TIMING_LEGACY ; -mmc_set_ios (host); +/* Set initial state and call mmc_set_ios */ +mmc_set_initial_state (host);/* * Some configurations, such as the 802.11 SDIO card in the OLPC @@ -2278,16 +2283,8 @@ static int mmc_do_hw_reset(struct mmc_host *host, int check) } } -if (mmc_host_is_spi (host)) { - host->ios.chip_select =MMC_CS_HIGH ; - host->ios.bus_mode =MMC_BUSMODE_PUSHPULL ; - }else { - host->ios.chip_select =MMC_CS_DONTCARE ; - host->ios.bus_mode =MMC_BUSMODE_OPENDRAIN ; - } - host->ios.bus_width =MMC_BUS_WIDTH_1 ; - host->ios.timing =MMC_TIMING_LEGACY ; -mmc_set_ios (host); +/* Set initial state and call mmc_set_ios */ +mmc_set_initial_state (host);mmc_host_clk_release (host);drivers/mmc/core/core.h +1
-1@@ -49,6 +49,7 @@ void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type);void mmc_power_up (struct mmc_host *host,u32 ocr);void mmc_power_off (struct mmc_host *host);void mmc_power_cycle (struct mmc_host *host,u32 ocr); +void mmc_set_initial_state (struct mmc_host *host);static inline void mmc_delay (unsigned int ms) { --1.7.2.5
PATCH v3 2/3 mmc: core: turn hw_reset into a bus_ops
In reply to Johan RudholmMove the (e)MMC specific hw_reset code from core.c into mmc.c and call it from the new bus_ops member hw_reset. This also lets us add code for resetting SD cards as well.
--- drivers/mmc/core/core.c | 56 +++++++++++++++------------------------------- drivers/mmc/core/core.h | 5 ++++ drivers/mmc/core/mmc.c | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 38 deletions(-)
drivers/mmc/core/core.c +18 -38
@@ -2242,67 +2242,47 @@ static void mmc_hw_reset_for_init(struct mmc_host *host)mmc_host_clk_release (host); } -int mmc_can_reset (struct mmc_card *card) -{ -u8 rst_n_function; - -if (!mmc_card_mmc (card)) -return 0 ; - rst_n_function = card->ext_csd.rst_n_function; -if ((rst_n_function &EXT_CSD_RST_N_EN_MASK ) !=EXT_CSD_RST_N_ENABLED ) -return 0 ; -return 1 ; -} -EXPORT_SYMBOL (mmc_can_reset); - +/* Reset card in a bus-specific way */ static int mmc_do_hw_reset (struct mmc_host *host,int check) { -struct mmc_card *card = host->card; - -if (!(host->caps &MMC_CAP_HW_RESET ) || !host->ops->hw_reset) -return -EOPNOTSUPP ; +int ret; -if (!card) +if (!host->card)return -EINVAL ; -if (!mmc_can_reset (card)) +if (!host->bus_ops || !host->bus_ops->hw_reset || + host->bus_ops->hw_reset (host,MMC_HW_RESET_TEST ))return -EOPNOTSUPP ; -mmc_host_clk_hold (host); -mmc_set_clock (host, host->f_init); + ret = host->bus_ops->hw_reset (host, check); - host->ops->hw_reset (host); - -/* If the reset has happened, then a status command will fail */ -if (check) { -u32 status; - -if (!mmc_send_status (card, &status)) { -mmc_host_clk_release (host); -return -ENOSYS ; - } - } - -/* Set initial state and call mmc_set_ios */ -mmc_set_initial_state (host); +if (check ==MMC_HW_RESET_TEST_CARD ) +return ret; -mmc_host_clk_release (host); +pr_warn ("%s: tried to reset card (status %d)\n" , +mmc_hostname (host), ret);return host->bus_ops->power_restore (host); }int mmc_hw_reset (struct mmc_host *host) { -return mmc_do_hw_reset (host,0 ); +return mmc_do_hw_reset (host,MMC_HW_RESET_RESET ); }EXPORT_SYMBOL (mmc_hw_reset);int mmc_hw_reset_check (struct mmc_host *host) { -return mmc_do_hw_reset (host,1 ); +return mmc_do_hw_reset (host,MMC_HW_RESET_CHECK ); }EXPORT_SYMBOL (mmc_hw_reset_check); +int mmc_can_reset (struct mmc_card *card) +{ +return mmc_do_hw_reset (card->host,MMC_HW_RESET_TEST_CARD ); +} +EXPORT_SYMBOL (mmc_can_reset); +static int mmc_rescan_try_freq (struct mmc_host *host,unsigned freq) { host->f_init = freq;
drivers/mmc/core/core.h +5 -0
@@ -27,6 +27,11 @@ struct mmc_bus_ops {int (*power_restore)(struct mmc_host *);int (*alive)(struct mmc_host *);int (*shutdown)(struct mmc_host *); +int (*hw_reset)(struct mmc_host *,int ); +#define MMC_HW_RESET_RESET 0 +#define MMC_HW_RESET_TEST 1 +#define MMC_HW_RESET_CHECK 2 +#define MMC_HW_RESET_TEST_CARD 3 };void mmc_attach_bus (struct mmc_host *host,const struct mmc_bus_ops *ops);
drivers/mmc/core/mmc.c +40 -0
@@ -1821,6 +1821,45 @@ static int mmc_power_restore(struct mmc_host *host)return ret; } +static int mmc_mmc_hw_reset (struct mmc_host *host,int test) +{ +struct mmc_card *card = host->card; +u8 rst_n_function; + + rst_n_function = card->ext_csd.rst_n_function; +if ((rst_n_function &EXT_CSD_RST_N_EN_MASK ) !=EXT_CSD_RST_N_ENABLED ) +return -EOPNOTSUPP ; + +if (test ==MMC_HW_RESET_TEST_CARD ) +return 0 ; + +if (!(host->caps &MMC_CAP_HW_RESET ) || !host->ops->hw_reset) +return -EOPNOTSUPP ; + +if (test ==MMC_HW_RESET_TEST ) +return 0 ; + +mmc_host_clk_hold (host); +mmc_set_clock (host, host->f_init); + + host->ops->hw_reset (host); + +if (test ==MMC_HW_RESET_CHECK ) { +u32 status; + +if (!mmc_send_status (card, &status)) { +mmc_host_clk_release (host); +return -ENOSYS ; + } + } + +mmc_set_initial_state (host); + +mmc_host_clk_release (host); + +return 0 ; +} +static const struct mmc_bus_ops mmc_ops = { .remove = mmc_remove, .detect = mmc_detect,
@@ -1831,6 +1870,7 @@ static const struct mmc_bus_ops mmc_ops = { .power_restore = mmc_power_restore, .alive = mmc_alive, .shutdown = mmc_shutdown, + .hw_reset = mmc_mmc_hw_reset, };/*
Signature · 1 line
1.7.2.5
PATCH v3 3/3 mmc: sd: add hw_reset callback
In reply to Johan RudholmEnable power cycle and re-initialization of SD cards via the hw_reset bus_ops. Power cycling a buggy SD card sometimes helps it get back on track.
--- drivers/mmc/core/sd.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-)
drivers/mmc/core/sd.c +13 -0
@@ -1180,6 +1180,18 @@ static int mmc_sd_runtime_resume(struct mmc_host *host)return 0 ; } +static int mmc_sd_hw_reset (struct mmc_host *host,int test) +{ +struct mmc_card *card = host->card; + +if (test ==MMC_HW_RESET_TEST || test ==MMC_HW_RESET_TEST_CARD ) +return 0 ; + +mmc_power_cycle (host, card->ocr); + +return 0 ; +} +static int mmc_sd_power_restore (struct mmc_host *host) {int ret;
@@ -1201,6 +1213,7 @@ static const struct mmc_bus_ops mmc_sd_ops = { .power_restore = mmc_sd_power_restore, .alive = mmc_sd_alive, .shutdown = mmc_sd_suspend, + .hw_reset = mmc_sd_hw_reset, };/*
Signature · 1 line
1.7.2.5