{"id":816915,"url":"http://patchwork.ozlabs.org/api/patches/816915/?format=json","web_url":"http://patchwork.ozlabs.org/project/uboot/patch/1506004213-22620-11-git-send-email-jjhiblot@ti.com/","project":{"id":18,"url":"http://patchwork.ozlabs.org/api/projects/18/?format=json","name":"U-Boot","link_name":"uboot","list_id":"u-boot.lists.denx.de","list_email":"u-boot@lists.denx.de","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<1506004213-22620-11-git-send-email-jjhiblot@ti.com>","list_archive_url":null,"date":"2017-09-21T14:29:57","name":"[U-Boot,v2,10/26] mmc: refactor SD startup to make it easier to support new modes","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"eb399c8b77de7d0727d73d7838f50c440b82408a","submitter":{"id":70508,"url":"http://patchwork.ozlabs.org/api/people/70508/?format=json","name":"Jean-Jacques Hiblot","email":"jjhiblot@ti.com"},"delegate":{"id":12423,"url":"http://patchwork.ozlabs.org/api/users/12423/?format=json","username":"Jaehoon","first_name":"Jaehoon","last_name":"Chung","email":"jh80.chung@samsung.com"},"mbox":"http://patchwork.ozlabs.org/project/uboot/patch/1506004213-22620-11-git-send-email-jjhiblot@ti.com/mbox/","series":[{"id":4400,"url":"http://patchwork.ozlabs.org/api/series/4400/?format=json","web_url":"http://patchwork.ozlabs.org/project/uboot/list/?series=4400","date":"2017-09-21T14:29:47","name":"mmc: Add support for HS200 and UHS modes","version":2,"mbox":"http://patchwork.ozlabs.org/series/4400/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/816915/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/816915/checks/","tags":{},"related":[],"headers":{"Return-Path":"<u-boot-bounces@lists.denx.de>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=lists.denx.de\n\t(client-ip=81.169.180.215; helo=lists.denx.de;\n\tenvelope-from=u-boot-bounces@lists.denx.de;\n\treceiver=<UNKNOWN>)","ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ti.com header.i=@ti.com header.b=\"N96Myg/n\";\n\tdkim-atps=neutral"],"Received":["from lists.denx.de (dione.denx.de [81.169.180.215])\n\tby ozlabs.org (Postfix) with ESMTP id 3xyfWP5QXTz9t3C\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 22 Sep 2017 00:44:01 +1000 (AEST)","by lists.denx.de (Postfix, from userid 105)\n\tid 6C86AC2207A; Thu, 21 Sep 2017 14:35:01 +0000 (UTC)","from lists.denx.de (localhost [IPv6:::1])\n\tby lists.denx.de (Postfix) with ESMTP id 17E17C22020;\n\tThu, 21 Sep 2017 14:32:36 +0000 (UTC)","by lists.denx.de (Postfix, from userid 105)\n\tid 90D13C21FF9; Thu, 21 Sep 2017 14:31:51 +0000 (UTC)","from fllnx209.ext.ti.com (fllnx209.ext.ti.com [198.47.19.16])\n\tby lists.denx.de (Postfix) with ESMTPS id 1679FC21F9D\n\tfor <u-boot@lists.denx.de>; Thu, 21 Sep 2017 14:31:46 +0000 (UTC)","from dlelxv90.itg.ti.com ([172.17.2.17])\n\tby fllnx209.ext.ti.com (8.15.1/8.15.1) with ESMTP id v8LEUiCA032466; \n\tThu, 21 Sep 2017 09:30:44 -0500","from DFLE105.ent.ti.com (dfle105.ent.ti.com [10.64.6.26])\n\tby dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id v8LEUdWd019729; \n\tThu, 21 Sep 2017 09:30:39 -0500","from DFLE102.ent.ti.com (10.64.6.23) by DFLE105.ent.ti.com\n\t(10.64.6.26) with Microsoft SMTP Server (version=TLS1_2,\n\tcipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.845.34;\n\tThu, 21 Sep 2017 09:30:39 -0500","from dlep32.itg.ti.com (157.170.170.100) by DFLE102.ent.ti.com\n\t(10.64.6.23) with Microsoft SMTP Server (version=TLS1_0,\n\tcipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.845.34 via Frontend\n\tTransport; Thu, 21 Sep 2017 09:30:39 -0500","from localhost (ileax41-snat.itg.ti.com [10.172.224.153])\n\tby dlep32.itg.ti.com (8.14.3/8.13.8) with ESMTP id v8LEUcsO030775;\n\tThu, 21 Sep 2017 09:30:38 -0500"],"X-Spam-Checker-Version":"SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de","X-Spam-Level":"","X-Spam-Status":"No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE,\n\tT_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ti.com;\n\ts=ti-com-17Q1; t=1506004244;\n\tbh=ntugfkNgKbVRnHuu6gEMtfDS6o+ZXEa7O2HpgZ1GoCs=;\n\th=From:To:CC:Subject:Date:In-Reply-To:References;\n\tb=N96Myg/n3ozhl1ZjuL7w8fG3BQsyBFxPT3t9aOTmAMqwhqR3NtwUq06hol0rZsiP9\n\te/yuyxuyi7UVyaffAesYttQtoIx1JTcwLZyNxnlT/KzVkJ2+QvULdlpz1ExNxZBHJF\n\taobDfu9SlfyW+7AKfYSLOk/pU/zMPVC7dHmKt304=","From":"Jean-Jacques Hiblot <jjhiblot@ti.com>","To":"<jh80.chung@samsung.com>, <trini@konsulko.com>, <kishon@ti.com>,\n\t<sjg@chromium.org>","Date":"Thu, 21 Sep 2017 16:29:57 +0200","Message-ID":"<1506004213-22620-11-git-send-email-jjhiblot@ti.com>","X-Mailer":"git-send-email 1.9.1","In-Reply-To":"<1506004213-22620-1-git-send-email-jjhiblot@ti.com>","References":"<1506004213-22620-1-git-send-email-jjhiblot@ti.com>","MIME-Version":"1.0","X-EXCLAIMER-MD-CONFIG":"e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180","Cc":"u-boot@lists.denx.de","Subject":"[U-Boot] [PATCH v2 10/26] mmc: refactor SD startup to make it\n\teasier to support new modes","X-BeenThere":"u-boot@lists.denx.de","X-Mailman-Version":"2.1.18","Precedence":"list","List-Id":"U-Boot discussion <u-boot.lists.denx.de>","List-Unsubscribe":"<https://lists.denx.de/options/u-boot>,\n\t<mailto:u-boot-request@lists.denx.de?subject=unsubscribe>","List-Archive":"<http://lists.denx.de/pipermail/u-boot/>","List-Post":"<mailto:u-boot@lists.denx.de>","List-Help":"<mailto:u-boot-request@lists.denx.de?subject=help>","List-Subscribe":"<https://lists.denx.de/listinfo/u-boot>,\n\t<mailto:u-boot-request@lists.denx.de?subject=subscribe>","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"u-boot-bounces@lists.denx.de","Sender":"\"U-Boot\" <u-boot-bounces@lists.denx.de>"},"content":"The SDcard startup process currently handles only 2 modes. To make it\neasier to add support for more modes, let's make the process more generic\nand use a list of the modes to try.\nThe major functional change is that when a mode fails we try the next one.\nNot all modes are tried, only those supported by the card and the host.\n\nSigned-off-by: Jean-Jacques Hiblot <jjhiblot@ti.com>\nReviewed-by: Simon Glass <sjg@chromium.org>\n---\n drivers/mmc/mmc.c | 183 ++++++++++++++++++++++++++++++++++++++----------------\n include/mmc.h     |   1 +\n 2 files changed, 130 insertions(+), 54 deletions(-)","diff":"diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c\nindex 382a9cf..b8e726a 100644\n--- a/drivers/mmc/mmc.c\n+++ b/drivers/mmc/mmc.c\n@@ -237,7 +237,8 @@ int mmc_send_status(struct mmc *mmc, int timeout)\n \t\t\t    (cmd.response[0] & MMC_STATUS_CURR_STATE) !=\n \t\t\t     MMC_STATE_PRG)\n \t\t\t\tbreak;\n-\t\t\telse if (cmd.response[0] & MMC_STATUS_MASK) {\n+\n+\t\t\tif (cmd.response[0] & MMC_STATUS_MASK) {\n #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)\n \t\t\t\tprintf(\"Status Error: 0x%08X\\n\",\n \t\t\t\t\tcmd.response[0]);\n@@ -610,7 +611,7 @@ static int mmc_change_freq(struct mmc *mmc)\n \tchar cardtype;\n \tint err;\n \n-\tmmc->card_caps = 0;\n+\tmmc->card_caps = MMC_MODE_1BIT;\n \n \tif (mmc_host_is_spi(mmc))\n \t\treturn 0;\n@@ -936,7 +937,7 @@ static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)\n }\n \n \n-static int sd_change_freq(struct mmc *mmc)\n+static int sd_get_capabilities(struct mmc *mmc)\n {\n \tint err;\n \tstruct mmc_cmd cmd;\n@@ -945,7 +946,7 @@ static int sd_change_freq(struct mmc *mmc)\n \tstruct mmc_data data;\n \tint timeout;\n \n-\tmmc->card_caps = 0;\n+\tmmc->card_caps = MMC_MODE_1BIT;\n \n \tif (mmc_host_is_spi(mmc))\n \t\treturn 0;\n@@ -1022,26 +1023,53 @@ retry_scr:\n \t}\n \n \t/* If high-speed isn't supported, we return */\n-\tif (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))\n-\t\treturn 0;\n+\tif (__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED)\n+\t\tmmc->card_caps |= MMC_CAP(SD_HS);\n \n-\t/*\n-\t * If the host doesn't support SD_HIGHSPEED, do not switch card to\n-\t * HIGHSPEED mode even if the card support SD_HIGHSPPED.\n-\t * This can avoid furthur problem when the card runs in different\n-\t * mode between the host.\n-\t */\n-\tif (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&\n-\t\t(mmc->cfg->host_caps & MMC_MODE_HS)))\n-\t\treturn 0;\n+\treturn 0;\n+}\n+\n+static int sd_set_card_speed(struct mmc *mmc, enum bus_mode mode)\n+{\n+\tint err;\n+\n+\tALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);\n \n \terr = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);\n+\tif (err)\n+\t\treturn err;\n+\n+\tif ((__be32_to_cpu(switch_status[4]) & 0x0f000000) != 0x01000000)\n+\t\treturn -ENOTSUPP;\n+\n+\treturn 0;\n+}\n \n+int sd_select_bus_width(struct mmc *mmc, int w)\n+{\n+\tint err;\n+\tstruct mmc_cmd cmd;\n+\n+\tif ((w != 4) && (w != 1))\n+\t\treturn -EINVAL;\n+\n+\tcmd.cmdidx = MMC_CMD_APP_CMD;\n+\tcmd.resp_type = MMC_RSP_R1;\n+\tcmd.cmdarg = mmc->rca << 16;\n+\n+\terr = mmc_send_cmd(mmc, &cmd, NULL);\n \tif (err)\n \t\treturn err;\n \n-\tif ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)\n-\t\tmmc->card_caps |= MMC_MODE_HS;\n+\tcmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;\n+\tcmd.resp_type = MMC_RSP_R1;\n+\tif (w == 4)\n+\t\tcmd.cmdarg = 2;\n+\telse if (w == 1)\n+\t\tcmd.cmdarg = 0;\n+\terr = mmc_send_cmd(mmc, &cmd, NULL);\n+\tif (err)\n+\t\treturn err;\n \n \treturn 0;\n }\n@@ -1133,6 +1161,18 @@ static const u8 multipliers[] = {\n \t80,\n };\n \n+static inline int bus_width(uint cap)\n+{\n+\tif (cap == MMC_MODE_8BIT)\n+\t\treturn 8;\n+\tif (cap == MMC_MODE_4BIT)\n+\t\treturn 4;\n+\tif (cap == MMC_MODE_1BIT)\n+\t\treturn 1;\n+\terror(\"invalid bus witdh capability 0x%x\\n\", cap);\n+\treturn 0;\n+}\n+\n #if !CONFIG_IS_ENABLED(DM_MMC)\n static void mmc_set_ios(struct mmc *mmc)\n {\n@@ -1176,8 +1216,9 @@ void mmc_dump_capabilities(const char *text, uint caps)\n \t\tprintf(\"8, \");\n \tif (caps & MMC_MODE_4BIT)\n \t\tprintf(\"4, \");\n-\tprintf(\"1] modes [\");\n-\n+\tif (caps & MMC_MODE_1BIT)\n+\t\tprintf(\"1, \");\n+\tprintf(\"\\b\\b] modes [\");\n \tfor (mode = MMC_LEGACY; mode < MMC_MODES_END; mode++)\n \t\tif (MMC_CAP(mode) & caps)\n \t\t\tprintf(\"%s, \", mmc_mode_name(mode));\n@@ -1185,47 +1226,81 @@ void mmc_dump_capabilities(const char *text, uint caps)\n }\n #endif\n \n-static int sd_select_bus_freq_width(struct mmc *mmc)\n+struct mode_width_tuning {\n+\tenum bus_mode mode;\n+\tuint widths;\n+};\n+\n+static const struct mode_width_tuning sd_modes_by_pref[] = {\n+\t{\n+\t\t.mode = SD_HS,\n+\t\t.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,\n+\t},\n+\t{\n+\t\t.mode = SD_LEGACY,\n+\t\t.widths = MMC_MODE_4BIT | MMC_MODE_1BIT,\n+\t}\n+};\n+\n+#define for_each_sd_mode_by_pref(caps, mwt) \\\n+\tfor (mwt = sd_modes_by_pref;\\\n+\t     mwt < sd_modes_by_pref + ARRAY_SIZE(sd_modes_by_pref);\\\n+\t     mwt++) \\\n+\t\tif (caps & MMC_CAP(mwt->mode))\n+\n+static int sd_select_mode_and_width(struct mmc *mmc)\n {\n \tint err;\n-\tstruct mmc_cmd cmd;\n+\tuint widths[] = {MMC_MODE_4BIT, MMC_MODE_1BIT};\n+\tconst struct mode_width_tuning *mwt;\n \n-\terr = sd_change_freq(mmc);\n+\terr = sd_get_capabilities(mmc);\n \tif (err)\n \t\treturn err;\n-\n \t/* Restrict card's capabilities by what the host can do */\n-\tmmc->card_caps &= mmc->cfg->host_caps;\n-\n-\tif (mmc->card_caps & MMC_MODE_4BIT) {\n-\t\tcmd.cmdidx = MMC_CMD_APP_CMD;\n-\t\tcmd.resp_type = MMC_RSP_R1;\n-\t\tcmd.cmdarg = mmc->rca << 16;\n-\n-\t\terr = mmc_send_cmd(mmc, &cmd, NULL);\n-\t\tif (err)\n-\t\t\treturn err;\n-\n-\t\tcmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;\n-\t\tcmd.resp_type = MMC_RSP_R1;\n-\t\tcmd.cmdarg = 2;\n-\t\terr = mmc_send_cmd(mmc, &cmd, NULL);\n-\t\tif (err)\n-\t\t\treturn err;\n-\n-\t\tmmc_set_bus_width(mmc, 4);\n+\tmmc->card_caps &= (mmc->cfg->host_caps | MMC_MODE_1BIT);\n+\n+\tfor_each_sd_mode_by_pref(mmc->card_caps, mwt) {\n+\t\tuint *w;\n+\n+\t\tfor (w = widths; w < widths + ARRAY_SIZE(widths); w++) {\n+\t\t\tif (*w & mmc->card_caps & mwt->widths) {\n+\t\t\t\tdebug(\"trying mode %s width %d (at %d MHz)\\n\",\n+\t\t\t\t      mmc_mode_name(mwt->mode),\n+\t\t\t\t      bus_width(*w),\n+\t\t\t\t      mmc_mode2freq(mmc, mwt->mode) / 1000000);\n+\n+\t\t\t\t/* configure the bus width (card + host) */\n+\t\t\t\terr = sd_select_bus_width(mmc, bus_width(*w));\n+\t\t\t\tif (err)\n+\t\t\t\t\tgoto error;\n+\t\t\t\tmmc_set_bus_width(mmc, bus_width(*w));\n+\n+\t\t\t\t/* configure the bus mode (card) */\n+\t\t\t\terr = sd_set_card_speed(mmc, mwt->mode);\n+\t\t\t\tif (err)\n+\t\t\t\t\tgoto error;\n+\n+\t\t\t\t/* configure the bus mode (host) */\n+\t\t\t\tmmc_select_mode(mmc, mwt->mode);\n+\t\t\t\tmmc_set_clock(mmc, mmc->tran_speed);\n+\n+\t\t\t\terr = sd_read_ssr(mmc);\n+\t\t\t\tif (!err)\n+\t\t\t\t\treturn 0;\n+\n+\t\t\t\tprintf(\"bad ssr\\n\");\n+\n+error:\n+\t\t\t\t/* revert to a safer bus speed */\n+\t\t\t\tmmc_select_mode(mmc, SD_LEGACY);\n+\t\t\t\tmmc_set_clock(mmc, mmc->tran_speed);\n+\t\t\t}\n+\t\t}\n \t}\n \n-\terr = sd_read_ssr(mmc);\n-\tif (err)\n-\t\treturn err;\n-\n-\tif (mmc->card_caps & MMC_MODE_HS)\n-\t\tmmc_select_mode(mmc, SD_HS);\n-\telse\n-\t\tmmc_select_mode(mmc, SD_LEGACY);\n-\n-\treturn 0;\n+\terror(\"unable to select a mode\\n\");\n+\treturn -ENOTSUPP;\n }\n \n /*\n@@ -1290,7 +1365,7 @@ static int mmc_select_bus_freq_width(struct mmc *mmc)\n \t\treturn err;\n \n \t/* Restrict card's capabilities by what the host can do */\n-\tmmc->card_caps &= mmc->cfg->host_caps;\n+\tmmc->card_caps &= (mmc->cfg->host_caps | MMC_MODE_1BIT);\n \n \t/* Only version 4 of MMC supports wider bus widths */\n \tif (mmc->version < MMC_VERSION_4)\n@@ -1685,7 +1760,7 @@ static int mmc_startup(struct mmc *mmc)\n \t\treturn err;\n \n \tif (IS_SD(mmc))\n-\t\terr = sd_select_bus_freq_width(mmc);\n+\t\terr = sd_select_mode_and_width(mmc);\n \telse\n \t\terr = mmc_select_bus_freq_width(mmc);\n \ndiff --git a/include/mmc.h b/include/mmc.h\nindex dd83f14..9fe6a87 100644\n--- a/include/mmc.h\n+++ b/include/mmc.h\n@@ -59,6 +59,7 @@\n \n #define MMC_MODE_8BIT\t\tBIT(30)\n #define MMC_MODE_4BIT\t\tBIT(29)\n+#define MMC_MODE_1BIT\t\tBIT(28)\n #define MMC_MODE_SPI\t\tBIT(27)\n \n \n","prefixes":["U-Boot","v2","10/26"]}