linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V2] mmc: dw_mmc: rewrite CLKDIV computation
@ 2013-03-27 20:38 Grant Grundler
  0 siblings, 0 replies; only message in thread
From: Grant Grundler @ 2013-03-27 20:38 UTC (permalink / raw)
  To: Chris Ball
  Cc: Doug Anderson, Will Newton, Seungwon Jeon, Bing Zhao,
	Jaehoon Chung, Ashok Nagarajan, Olof Johansson, linux-mmc,
	linux-kernel, Grant Grundler

When backporting Commit e419990b5e8 ("mmc: dw_mmc: correct the
calculation for CLKDIV") to 3.4 kernel and debugging
a FW issue, I found the code unreadable.  This rewrite simplifies
the computation and explains each step.

Signed-off-by: Grant Grundler <grundler@chromium.org>
---
V2: rewrote commit msg per feedback

Tested on Samsung Series 3 Chromebook (exynos 5250 chipset) using
ChromeOS 3.4 kernel (not 3.9-rc3 which this patch is based against).

I've posted the test_dw_mmc.c source to confirm this patch generates
the same correct values.

The CLKDIV issue I was trying to resolve in ChromeOS 3.4 kernel:
    http://crbug.com/221828

 drivers/mmc/host/dw_mmc.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)


diff --git a/drivers/mmc/host/dw_mmc.c b/drivers/mmc/host/dw_mmc.c
index 9834221..6fdf55b 100644
--- a/drivers/mmc/host/dw_mmc.c
+++ b/drivers/mmc/host/dw_mmc.c
@@ -631,14 +631,22 @@ static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
 
 	if (slot->clock != host->current_speed || force_clkinit) {
 		div = host->bus_hz / slot->clock;
-		if (host->bus_hz % slot->clock && host->bus_hz > slot->clock)
-			/*
-			 * move the + 1 after the divide to prevent
-			 * over-clocking the card.
+		if (host->bus_hz > slot->clock) {
+			/* don't overclock due to integer math losses */
+			if ((div * slot->clock) < host->bus_hz)
+				div++;
+
+			/* don't overclock due to resolution of HW */
+			if (div & 1)
+				div++;
+
+			/* See 6.2.3 CLKDIV in "Mobile Storage Host Databook"
+			 * Look for dwc_mobile_storage_db.pdf from Synopsys.
+			 * CLKDIV value 0 means divisor 1, val 1 -> 2, ...
 			 */
-			div += 1;
-
-		div = (host->bus_hz != slot->clock) ? DIV_ROUND_UP(div, 2) : 0;
+			div /= 2;
+		} else
+			div = 0;        /* use bus_hz */
 
 		dev_info(&slot->mmc->class_dev,
 			 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ"
-- 
1.8.1.3


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2013-03-27 20:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-27 20:38 [PATCH V2] mmc: dw_mmc: rewrite CLKDIV computation Grant Grundler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).