From patchwork Mon Jul 22 17:57:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Protsenko X-Patchwork-Id: 813746 Delivered-To: patch@linaro.org Received: by 2002:adf:f288:0:b0:367:895a:4699 with SMTP id k8csp1985242wro; Mon, 22 Jul 2024 10:58:39 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCURrCRuRuWDSLEc6i8CZqL878UoQX/YgrN8eg1vXwP8QgPxrokAJTEo/JXOrKeVvVUC2sc8R/JRHe1ciWZl4nzs X-Google-Smtp-Source: AGHT+IFcvm2RZu8rcgh24XmBiS7DAbgrQNigvMNUl8w6f5aEZ8U49dRvVK50z9usgKYr04NhmsN7 X-Received: by 2002:a50:a453:0:b0:5a2:594b:be56 with SMTP id 4fb4d7f45d1cf-5a8ff210564mr940990a12.12.1721671119096; Mon, 22 Jul 2024 10:58:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721671119; cv=none; d=google.com; s=arc-20160816; b=aXZkK3EqqYHxrC2GeRRHesH4i+3SPVW/7YMqm+T80ulZ8F0d2AgU0zCCrJbmBYLSLz Jc7U/mteIBv+zQpJ5fPQR3LMDYAuDWidnIVfh5fnwdPIlwtz8A6Y26nhE3Q4DotkY6Fn IRJK0JI+k2ObosFhPCwx4E13ubb4IU1Q5KjKGg/G9wkOTqYdSLrEXFuPenGLY9UNZBBE gK4eteS7mCrQsd6J+gpfcDVgb/BArKmTEe6s7P42XvNgpcFhGRvz/pJ5/4YQF5ZUwo08 5tUCWuQkyqqjkjeHiH+o8adymH5V4PNjIxud46lnfuRAJDP36I2NQIMXGqzOqWTk1jih 0h4g== 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=0SOWexDhZKgy98qvHFC6VGW70pjGoZQlJAvCc0MDcpk=; fh=31wjvyyGWjJMFCXIkFpqmsFu3Vb+A+PSH+Fe3XHFFDs=; b=TV77EMJnYlxj2UTjO5JChNiK8NAlmTUL+y4qiXGlW90C7tzMBNnw0lpX8YRM+JWeB3 oLP6RQKd/CLQHvqaY6BfHQsy39HB0BbfXXuRQc8Dv5WTAGyGafQx6UoTqJn1RdrK500+ Hw7YfObvzSZ3ov6i/LcH34YSGHO/OIZV8+zaMieBDo3SXn7aoGs3CmfLv8ysoQSlwG4+ 1k1GMmCDgPkgjaSOrbsVqDlgnEun8aHjZ7gfO6W2hkDTfsqnCTTluxFWCmGNvkqKLUZ4 yrI3qMNSvcV+SR8ZIzWUhSlfqOZivF1UXngsTU6vQpyIWLyMsetIBOEunRAuu3fb7tlL wbYQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Cp+o7Z1T; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-5a30ce93ba1si4688948a12.688.2024.07.22.10.58.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 10:58:39 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Cp+o7Z1T; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 439C78855C; Mon, 22 Jul 2024 19:57:50 +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="Cp+o7Z1T"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 874E5884FD; Mon, 22 Jul 2024 19:57:48 +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 autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) (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 4C3A188453 for ; Mon, 22 Jul 2024 19:57:46 +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-ot1-x335.google.com with SMTP id 46e09a7af769-706dea920f3so2179527a34.1 for ; Mon, 22 Jul 2024 10:57:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1721671065; x=1722275865; 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=0SOWexDhZKgy98qvHFC6VGW70pjGoZQlJAvCc0MDcpk=; b=Cp+o7Z1TqsjGoGoWLaA7/2BiB9K18mnl5xZJMyDQtytWuq/5YjLrJYnk10ogZihz3h JitY3IdK7CT6TnA65sSJ9LrhR9I7izEc6K/H1P5dnD0wxq2zNabE08RLPaUOk2e103Wm jFtPu8ZfPnuQhKeH9a0QInvuNf8LFYIRCrgCMbvDoJEgpXQ1S74yBTGwrtVTHEhkPxMC xTG/TJTtCf9FjdCB3Cq2WYEITgvBdKH2g8d66KXAglx9rJDGwEPbzOj0oRrxga8MrABP m7GcmaQuoi8XA+Osr5ebi0pF3w+vtQsXxYdmDRjUAAXaMCLgAeHTS1IqKboVjLMJu3kk TH4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721671065; x=1722275865; 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=0SOWexDhZKgy98qvHFC6VGW70pjGoZQlJAvCc0MDcpk=; b=BnSWQKekeU2NQvLy2kdpCEKCfj/gqCY38upoNP/ZqfSwsq1DM3URYzZl1mxzjyI3R1 5CzUxZUSMfZRJaJQtTtVuvjUPpUgpPokCgWLQNOcj1W0Ml+lOTHh3AoGFczxSk4O1Kf9 jUnMcHFd7XVF9KA9AU3yXraB/11wLniuc39xQ6lxRuRTy6lwvIhSRgc/WVAoK4DaTISy yQ5Yjh6Zz1aTr2FXbfgYd6q4lwQ/IQ4lOmf86ZoA0ufZJak8LOdPkjkGfTLf5InhaB1O wCDMfj0MS5bOrGuumvdtq7SSMXFoieul9f2RQSJxypA010DjQPSYpzXxTn4IKz2ixccU ZWbg== X-Forwarded-Encrypted: i=1; AJvYcCWUBmxEtWjBeCyUYew7Zdva2KsdFjLrEv3x3V3nK9EtcMhiaGjpepDhSwPNASeEXVUUCxejNS7yPhp8/qlPJuClzNlNxg== X-Gm-Message-State: AOJu0YwcDvxkH2eyBvliJmg/DKBrs21EcMIEt1zALEGRwcZX/3GpLIZM gVth9+A+fNkFhcmoCQdV7g8ReH8rM3REFdwXlAYdkB2iD8XPcQsnCXFg7TU3SQ4= X-Received: by 2002:a05:6830:2aa7:b0:703:644c:84fc with SMTP id 46e09a7af769-708fdbe2457mr3225644a34.11.1721671065029; Mon, 22 Jul 2024 10:57:45 -0700 (PDT) Received: from localhost ([136.62.192.75]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-708f60c2be3sm1624885a34.23.2024.07.22.10.57.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 10:57:44 -0700 (PDT) From: Sam Protsenko To: Minkyu Kang , Minkyu Kang Cc: Tom Rini , Peng Fan , Jaehoon Chung , Simon Glass , 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 v3 05/38] mmc: dw_mmc: Extract divider update to a separate function Date: Mon, 22 Jul 2024 12:57:07 -0500 Message-Id: <20240722175740.6985-6-semen.protsenko@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240722175740.6985-1-semen.protsenko@linaro.org> References: <20240722175740.6985-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 v3: - (none) 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;