From patchwork Tue Mar 26 22:50:02 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Grant Grundler X-Patchwork-Id: 2344661 Return-Path: X-Original-To: patchwork-linux-mmc@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id ACEE5E014B for ; Tue, 26 Mar 2013 22:50:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755823Ab3CZWuO (ORCPT ); Tue, 26 Mar 2013 18:50:14 -0400 Received: from mail-pa0-f45.google.com ([209.85.220.45]:55318 "EHLO mail-pa0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755240Ab3CZWuK (ORCPT ); Tue, 26 Mar 2013 18:50:10 -0400 Received: by mail-pa0-f45.google.com with SMTP id kl13so1859917pab.4 for ; Tue, 26 Mar 2013 15:50:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=x-received:from:to:cc:subject:date:message-id:x-mailer; bh=NlIkQ/JpUmZy5ySbjuHNzbIIxyGfgVL/zWUtQSnUliI=; b=Yv5L2pnuHXCMOqhZfUdL2nsAMQbAX3VqNbhkyZx8YHtGkOizFta9RxVBY0YIpotDuj KExMaOYm/Yd5fayy0g7AzNGp/7FhXc+EFmAJDNXTcfK4Z9DT2GEZlfcNSQs3z1zpe2zN O8txDMsVJkhgxx3Sjk91yyNe3D7fLwQsVzvfU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer :x-gm-message-state; bh=NlIkQ/JpUmZy5ySbjuHNzbIIxyGfgVL/zWUtQSnUliI=; b=LCECAc04Kaa2lAwuwzqh+y98aFqwwmb/zD75+JrqtDFj4CPTczPpqEOF3cSmt9bHH1 qnq5mPTdL6bDlEwEEm9UH0hJQydtfsfy+ERVtLtwSycKs88yuW778yTpeI+WWOS8LFw6 kbLIyLi68rHeAFgBPMHYvsNyn6F8MoKwIWgL/bU+LCf4EIJ0VITj6AE5JqLW+/+srhp9 XBGsPx3LWe12nETJ6O3MS6MhqESOVBUzCjlIBoX1I6gF57aIgD7ZmJrNxezTaIUzJlAJ SV/tXlr/j50Vck/BXuoI1w/pr61LmHQgDUcj86nPA/3EwMq3PDWVJCXarlz80sweQ8Dy ujNg== X-Received: by 10.68.106.165 with SMTP id gv5mr3167962pbb.143.1364338210126; Tue, 26 Mar 2013 15:50:10 -0700 (PDT) Received: from firesword.mtv.corp.google.com (firesword.mtv.corp.google.com [172.22.73.90]) by mx.google.com with ESMTPS id z1sm18974253pbw.19.2013.03.26.15.50.08 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 26 Mar 2013 15:50:09 -0700 (PDT) From: Grant Grundler To: Chris Ball Cc: Doug Anderson , Will Newton , Seungwon Jeon , Bing Zhao , Jaehoon Chung , Ashok Nagarajan , Olof Johansson , linux-mmc@vger.kernel.org, linux-kernel@vger.kernel.org, Grant Grundler Subject: [PATCH] mmc: dw_mmc: rewrite CLKDIV computation Date: Tue, 26 Mar 2013 15:50:02 -0700 Message-Id: <1364338202-10311-1-git-send-email-grundler@chromium.org> X-Mailer: git-send-email 1.8.1.3 X-Gm-Message-State: ALoCoQmngDwjtqi6fRYCKbp3cmPJRxzOx4bddLe0jWv0Kxid+gRQWHPfKInUPqznfNGFnjQACjdH Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org Last year Seungwon Jeon (Samsung) fixed a bug in CLKDIV computation. But when debugging a related issue (http://crbug.com/221828) I found the code unreadable. This rewrite simplifies the computation and explains each step. Signed-off-by: Grant Grundler --- 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 written a test program to confirm this patch generates the same correct values and will share that separately. 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"