For use by drivers that support eMMC standard tuning, add a new mmc_send_tuning helper that sends 64-128 bytes and checks the result for bit errors.
Signed-off-by: Ahmad Fatoum <a.fat...@pengutronix.de> --- drivers/mci/mci-core.c | 78 ++++++++++++++++++++++++++++++++++++++++++ include/mci.h | 1 + 2 files changed, 79 insertions(+) diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c index 2645f9240d93..204143011e30 100644 --- a/drivers/mci/mci-core.c +++ b/drivers/mci/mci-core.c @@ -1607,6 +1607,84 @@ int mci_execute_tuning(struct mci *mci) return host->ops.execute_tuning(host, opcode); } +static const u8 tuning_blk_pattern_4bit[] = { + 0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc, + 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef, + 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb, + 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef, + 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c, + 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee, + 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff, + 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde, +}; + +static const u8 tuning_blk_pattern_8bit[] = { + 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, + 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, 0xcc, + 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, 0xff, + 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, 0xff, + 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, 0xdd, + 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, + 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, 0xff, + 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, 0xff, + 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, + 0x00, 0xff, 0xff, 0xcc, 0xcc, 0xcc, 0x33, 0xcc, + 0xcc, 0xcc, 0x33, 0x33, 0xcc, 0xcc, 0xcc, 0xff, + 0xff, 0xff, 0xee, 0xff, 0xff, 0xff, 0xee, 0xee, + 0xff, 0xff, 0xff, 0xdd, 0xff, 0xff, 0xff, 0xdd, + 0xdd, 0xff, 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, + 0xbb, 0xbb, 0xff, 0xff, 0xff, 0x77, 0xff, 0xff, + 0xff, 0x77, 0x77, 0xff, 0x77, 0xbb, 0xdd, 0xee, +}; + +int mmc_send_tuning(struct mci *mci, u32 opcode) +{ + struct mci_cmd cmd = {}; + struct mci_data data = {}; + const u8 *tuning_block_pattern; + int size, err = 0; + u8 *data_buf; + + if (mci->host->ios.bus_width == MMC_BUS_WIDTH_8) { + tuning_block_pattern = tuning_blk_pattern_8bit; + size = sizeof(tuning_blk_pattern_8bit); + } else if (mci->host->ios.bus_width == MMC_BUS_WIDTH_4) { + tuning_block_pattern = tuning_blk_pattern_4bit; + size = sizeof(tuning_blk_pattern_4bit); + } else + return -EINVAL; + + data_buf = calloc(size, 1); + if (!data_buf) + return -ENOMEM; + + mci_setup_cmd(&cmd, opcode, 0, MMC_RSP_R1 | MMC_CMD_ADTC); + + + data.blocksize = size; + data.blocks = 1; + data.flags = MMC_DATA_READ; + + /* + * According to the tuning specs, Tuning process + * is normally shorter 40 executions of CMD19, + * and timeout value should be shorter than 150 ms + */ + data.timeout_ns = 150 * NSEC_PER_MSEC; + + err = mci_send_cmd(mci, &cmd, &data); + if (err) + goto out; + + if (memcmp(data_buf, tuning_block_pattern, size)) + err = -EIO; + +out: + free(data_buf); + return err; +} +EXPORT_SYMBOL_GPL(mmc_send_tuning); + int mci_send_abort_tuning(struct mci *mci, u32 opcode) { struct mci_cmd cmd = {}; diff --git a/include/mci.h b/include/mci.h index 29aaecb46305..207fb9779a14 100644 --- a/include/mci.h +++ b/include/mci.h @@ -773,6 +773,7 @@ static inline int mmc_card_hs(struct mci *mci) */ int mmc_hs200_tuning(struct mci *mci); int mci_execute_tuning(struct mci *mci); +int mmc_send_tuning(struct mci *mci, u32 opcode); int mci_send_abort_tuning(struct mci *mci, u32 opcode); int mmc_select_timing(struct mci *mci); int mci_set_blockcount(struct mci *mci, unsigned int cmdarg); -- 2.39.5