From patchwork Mon Jun 10 01:11:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Protsenko X-Patchwork-Id: 802937 Delivered-To: patch@linaro.org Received: by 2002:adf:f147:0:b0:35b:5a80:51b4 with SMTP id y7csp1734331wro; Sun, 9 Jun 2024 18:13:41 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWOhvsu8o4thXowGf3OjNyUBdGEd4kvhdIhL8k9iH+8N73+/wmPW2AZT2IBO+F+hzQfNQlqpWU+TbLymX4OzxCk X-Google-Smtp-Source: AGHT+IHe2QiiJrTjM/UIWeoKiLpM2p8HcTVhLPhiaZ6mGEtEEfp+A3VOHfELlYi9njnWysAMp4j/ X-Received: by 2002:a05:651c:150:b0:2e5:61f4:2c11 with SMTP id 38308e7fff4ca-2eadce9a12cmr57953591fa.45.1717982021530; Sun, 09 Jun 2024 18:13:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1717982021; cv=none; d=google.com; s=arc-20160816; b=zDdHgUDzV84TVWeZFacy8aZeOTnnHUit2BWrT2qNOAf6j4G75XPuh1nmkxssRbWy57 fWW/l2NeSqsLgKTOIEWyJvgMkinxZRdg1ZjyODiQ0PUZRErjpsvJlKUbmSSa+ThVWyz4 +TWknTUTCAX43FaFic2TmT7XrjvhoSP6IDJ63IfA/AJJRCZ6A0LobcvdygBSt9ohM74r wCzY9KtPI+0yCA0evY6V7xC60hTsURMvFWyqmck2CDpw8INt4zlRSgnYYzEGq4Y3eqZb mNVWT3ZHh/X2C25i4urkpI3ilaVvn+6QDb0VUDaofLwhL98hEdY3kAMQS/M5XI913XYD 42TQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9nd4em9gOiHJXkkv3B9uErPXnC9QG1DOG02CNBUp5pw=; fh=i1wSYAQ23HHndbpQcjH+KhDwWsgGeOB+dAiPmlNs0S0=; b=eZaI9ur39MbEbLqo5GN3XKdp69P7MDxoe0eP7sJA8Ap2lIq+NZAq8VNSxZah79h1gn iHi1hqHeztzeEzCGyFFkSpkecDz+1DFCxHeaLgAHEtkkDKV8mdgqOptHAIsmLPUC0qTZ URWe5FrwIxeypjMzYpcpDJ621iPXj9RCymtHR/NsKmmPoIoWQPkum/BkSdpsBS2uDxwQ /7S+1SGvR8pwtDR2dHkHO3TjsDTk+cwR3LzRpnoFO0eWw7OnEERQ/na5ENXEF50WFJaT jYn3fpAOw7rMfGxgDgIgkamApVMPwK9lN9kOBSLKp4h/N++zTKQYmOsSQBh75dId89qy yiFg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LKr1USlH; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57c7475e28asi1464319a12.395.2024.06.09.18.13.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jun 2024 18:13:41 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LKr1USlH; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8F02C87D46; Mon, 10 Jun 2024 03:12:38 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="LKr1USlH"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BB24088468; Mon, 10 Jun 2024 03:12:34 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 91120883DA for ; Mon, 10 Jun 2024 03:12:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=semen.protsenko@linaro.org Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-250e4c4d650so2008847fac.2 for ; Sun, 09 Jun 2024 18:12:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717981951; x=1718586751; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9nd4em9gOiHJXkkv3B9uErPXnC9QG1DOG02CNBUp5pw=; b=LKr1USlHpNMm36Qc1+isZMo5BWNlAHL4pq7bcrS67Sc9Fsd/rMKT/eWUohqieFrN9S iLaju9D6spvUHOFlDXLB4au/iCMSlpwUBXo1AtM6+f9nxwU6JqHaLChqHN3HQNmC2/nB mL4/hTUMVXHpYR7ks6KX8dDMrv7H3IiIQuJoSOG4HyuWYMUuZhyYp+lsnAW0No4yqRSg e4XCi90soIfo2LZLN3D2H997e9S8tJFQqYRs/pbQprwyR1xAZiU0sGpYWpKHpkbl/NNc lbCqexS9I4pCqAyIYea0x8R4I3K0mpsDSZeR4gwn1gSUEV71sus9b8fjO+dmfao5SlIE 5Uag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717981951; x=1718586751; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9nd4em9gOiHJXkkv3B9uErPXnC9QG1DOG02CNBUp5pw=; b=g7ZQMu1nwOmdXH00NPKbnFTC/omxiQmgycAnghKWVF4ULwcCG4sqDMy7NbGgO5Qmyj wScRM1VN6E4l0EpBAAMOWmwrP3Vg4WguQNnUKXf6ID2jsgaxDAyIARxr5B+td0x4GBYN RrhLGR7nv+8RgF9rcma1Tb5o8O8RLtyWy5fbCy3IitCSbb/QXhfGZFp3aflRqH9pe/V+ UEX+7bG4AFqP6n5tW7YzD9Bf0DeZzqd+84ZdAS9kmIUT75ChkJlGjM1wW0SQnSYN2Hrc Aobbvg07NVvlVIFUn10UpD3I5hZKc66Hy0OR2GkOSKYFaji61qlIsqmqOv9sLq7zJsQN fCFg== X-Forwarded-Encrypted: i=1; AJvYcCViJx6+NlBwESGjytn3QlJZbnnqktqIPrymju08CH5VwGZWV2OlyN+RMm0vrH+7f6xAzINsbtdHY3ahbjA7/vEJi+A9gg== X-Gm-Message-State: AOJu0YzdH53TRl7WZnsgMD20FUmzkpN+1obUi5gYnNADkpPraajUlDDw yEnGQI0UQzL/ffyBntOamR7feLQZjqNPM5k2L3YGHoqtzA5xXRPY9A3B5IYeRSA= X-Received: by 2002:a05:6870:d187:b0:254:bc92:9b97 with SMTP id 586e51a60fabf-254bc929e3dmr2682273fac.57.1717981951145; Sun, 09 Jun 2024 18:12:31 -0700 (PDT) Received: from localhost ([136.62.192.75]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-25448138e4asm2062822fac.57.2024.06.09.18.12.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jun 2024 18:12:30 -0700 (PDT) From: Sam Protsenko To: Tom Rini , Minkyu Kang , Peng Fan , Jaehoon Chung , Simon Glass Cc: Quentin Schulz , Philipp Tomsich , Kever Yang , Eugeniy Paltsev , Peter Robinson , Jonas Karlman , Yang Xiwen , Ferass El Hafidi , Sean Anderson , u-boot@lists.denx.de, uboot-snps-arc@synopsys.com Subject: [PATCH v2 06/40] mmc: dw_mmc: Extract divider update to a separate function Date: Sun, 9 Jun 2024 20:11:52 -0500 Message-Id: <20240610011226.4050-7-semen.protsenko@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240610011226.4050-1-semen.protsenko@linaro.org> References: <20240610011226.4050-1-semen.protsenko@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Extract the clock divider update into dwmci_update_div() function. It's a procedure recommended in TRM, so it's better to keep it in a dedicated function to make the code clearer. While at it also extract the clock control code into a separate routine to avoid code duplication in dwmci_setup_bus(). No functional change. Signed-off-by: Sam Protsenko --- Changes in v2: - Moved CLKSRC after disabling the clock as recommended in TRM, to avoid possible glitch - Extracted div update into a separate function with corresponding comment - Changed the patch subject and reworked the commit message drivers/mmc/dw_mmc.c | 81 +++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/drivers/mmc/dw_mmc.c b/drivers/mmc/dw_mmc.c index cbfb8d3b8683..7bf3279ee9ec 100644 --- a/drivers/mmc/dw_mmc.c +++ b/drivers/mmc/dw_mmc.c @@ -412,11 +412,56 @@ static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, return ret; } -static int dwmci_setup_bus(struct dwmci_host *host, u32 freq) +static int dwmci_control_clken(struct dwmci_host *host, bool on) { - u32 div, status; + const u32 val = on ? DWMCI_CLKEN_ENABLE | DWMCI_CLKEN_LOW_PWR : 0; + const u32 cmd_only_clk = DWMCI_CMD_PRV_DAT_WAIT | DWMCI_CMD_UPD_CLK; int timeout = 10000; + u32 status; + + dwmci_writel(host, DWMCI_CLKENA, val); + + /* Inform CIU */ + dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_START | cmd_only_clk); + do { + status = dwmci_readl(host, DWMCI_CMD); + if (timeout-- < 0) { + debug("%s: Timeout!\n", __func__); + return -ETIMEDOUT; + } + } while (status & DWMCI_CMD_START); + + return 0; +} + +/* + * Update the clock divider. + * + * To prevent a clock glitch keep the clock stopped during the update of + * clock divider and clock source. + */ +static int dwmci_update_div(struct dwmci_host *host, u32 div) +{ + int ret; + + /* Disable clock */ + ret = dwmci_control_clken(host, false); + if (ret) + return ret; + + /* Set clock to desired speed */ + dwmci_writel(host, DWMCI_CLKDIV, div); + dwmci_writel(host, DWMCI_CLKSRC, 0); + + /* Enable clock */ + return dwmci_control_clken(host, true); +} + +static int dwmci_setup_bus(struct dwmci_host *host, u32 freq) +{ + u32 div; unsigned long sclk; + int ret; if ((freq == host->clock) || (freq == 0)) return 0; @@ -439,35 +484,9 @@ static int dwmci_setup_bus(struct dwmci_host *host, u32 freq) else div = DIV_ROUND_UP(sclk, 2 * freq); - dwmci_writel(host, DWMCI_CLKENA, 0); - dwmci_writel(host, DWMCI_CLKSRC, 0); - - dwmci_writel(host, DWMCI_CLKDIV, div); - dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT | - DWMCI_CMD_UPD_CLK | DWMCI_CMD_START); - - do { - status = dwmci_readl(host, DWMCI_CMD); - if (timeout-- < 0) { - debug("%s: Timeout!\n", __func__); - return -ETIMEDOUT; - } - } while (status & DWMCI_CMD_START); - - dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE | - DWMCI_CLKEN_LOW_PWR); - - dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT | - DWMCI_CMD_UPD_CLK | DWMCI_CMD_START); - - timeout = 10000; - do { - status = dwmci_readl(host, DWMCI_CMD); - if (timeout-- < 0) { - debug("%s: Timeout!\n", __func__); - return -ETIMEDOUT; - } - } while (status & DWMCI_CMD_START); + ret = dwmci_update_div(host, div); + if (ret) + return ret; host->clock = freq;