From patchwork Wed Nov 27 00:57:19 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 3243261 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 5A28EC045B for ; Wed, 27 Nov 2013 01:05:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 72B5220549 for ; Wed, 27 Nov 2013 01:05:23 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 767A4204EC for ; Wed, 27 Nov 2013 01:05:22 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VlTYl-0002P7-Tq; Wed, 27 Nov 2013 01:04:56 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VlTYd-0003tl-Iz; Wed, 27 Nov 2013 01:04:47 +0000 Received: from mail-vc0-f202.google.com ([209.85.220.202]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VlTYX-0003ro-3O for linux-arm-kernel@lists.infradead.org; Wed, 27 Nov 2013 01:04:42 +0000 Received: by mail-vc0-f202.google.com with SMTP id id10so702341vcb.5 for ; Tue, 26 Nov 2013 17:04:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=tZw9BX6c/hUVIWG/TmSSS8CP980LC4qVzHzJ1U2A9Ag=; b=bdrvUYtL/K5eFOpT6IsCQv8fJ1VSVcxlELRKhHZ/RAO2ypRlcNDssaSGAOBpMNt5nx kx0Uxh8bo1iTbUdoU5NF0xNzEAdKK+5VmnZ5MwGAppM1VsN4DEKNWw0gm52XzLJ8MkXH tf+DtMdm+L9S3wjq27U/LXlXc+RVI7wywCwhfjHOagZBcton+MQ9gJnrhHRycIwSGfgY XbrDsiI6/UGBodu5E0bP7pAAIQnRz2vn1fpVSnJOQWmdUCVoHinkbruiGij818jpHSUp tZ8+sz/N8lR4MUVy9kDmVN0gSpVYT/jATcN/XOpV6yC0Z7JcrNzD6m6tP1SSmG2PnAjC 6FJQ== X-Gm-Message-State: ALoCoQkT67EtSWlAFjDtrzOXMx5/DltBhIT2kf0yBkCOfh3zehS21b0MVIsEB9qewNthsX5uLaZIIxTvT6Pp/wzfSRk0J6PfIhCgdGBowX18eF6/ejsqz4TETE/iyhPeNj8Xa1DT0YF42QsBQVtQF3NIQk+Z0xjY9zVN2SGtPRE7g5xv91AEj6YNlyYlNVIR6nAvxhHvKkyH3oxV6a5dSandp+lLIcjQbw== X-Received: by 10.58.230.5 with SMTP id su5mr11573277vec.23.1385513886073; Tue, 26 Nov 2013 16:58:06 -0800 (PST) Received: from corp2gmr1-2.hot.corp.google.com (corp2gmr1-2.hot.corp.google.com [172.24.189.93]) by gmr-mx.google.com with ESMTPS id h47si3758217yhn.0.2013.11.26.16.58.06 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Nov 2013 16:58:06 -0800 (PST) Received: from tictac.mtv.corp.google.com (tictac.mtv.corp.google.com [172.22.72.141]) by corp2gmr1-2.hot.corp.google.com (Postfix) with ESMTP id D73265A4273; Tue, 26 Nov 2013 16:58:05 -0800 (PST) Received: by tictac.mtv.corp.google.com (Postfix, from userid 121310) id 7D22980785; Tue, 26 Nov 2013 16:58:05 -0800 (PST) From: Doug Anderson To: Wim Van Sebroeck , Guenter Roeck Subject: [PATCH v2] watchdog: s3c2410_wdt: Handle rounding a little better for timeout Date: Tue, 26 Nov 2013 16:57:19 -0800 Message-Id: <1385513839-17181-1-git-send-email-dianders@chromium.org> X-Mailer: git-send-email 1.8.4.1 In-Reply-To: <1385490637-10306-1-git-send-email-dianders@chromium.org> References: <1385490637-10306-1-git-send-email-dianders@chromium.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20131126_200441_257953_CD02767E X-CRM114-Status: GOOD ( 13.48 ) X-Spam-Score: -2.6 (--) Cc: Kukjin Kim , linux-watchdog@vger.kernel.org, Leela Krishna Amudala , linux-kernel@vger.kernel.org, Tomasz Figa , Doug Anderson , linux-samsung-soc@vger.kernel.org, Ben Dooks , Olof Johansson , linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The existing watchdog timeout worked OK but didn't deal with rounding in an ideal way when dividing out all of its clocks. Specifically if you had a timeout of 32 seconds and an input clock of 66666666, you'd end up setting a timeout of 31.9998 seconds and reporting a timeout of 31 seconds. Specifically DBG printouts showed: s3c2410wdt_set_heartbeat: count=16666656, timeout=32, freq=520833 s3c2410wdt_set_heartbeat: timeout=32, divisor=255, count=16666656 (0000ff4f) and the final timeout reported to the user was: ((count / divisor) * divisor) / freq (0xff4f * 255) / 520833 = 31 (truncated from 31.9998) the technically "correct" value is: (0xff4f * 255) / (66666666.0 / 128) = 31.9998 By using "DIV_ROUND_UP" we can be a little more correct. s3c2410wdt_set_heartbeat: count=16666688, timeout=32, freq=520834 s3c2410wdt_set_heartbeat: timeout=32, divisor=255, count=16666688 (0000ff50) and the final timeout reported to the user: (0xff50 * 255) / 520834 = 32 the technically "correct" value is: (0xff50 * 255) / (66666666.0 / 128) = 32.0003 We'll use a DIV_ROUND_UP to solve this, generally erroring on the side of reporting shorter values to the user and setting the watchdog to slightly longer than requested: * Round input frequency up to assume watchdog is counting faster. * Round divisions by divisor up to give us extra time. At the same time we can avoid a for loop by just doing the right math. Signed-off-by: Doug Anderson --- Changes in v2: - Avoid a for loop as per Guenter. drivers/watchdog/s3c2410_wdt.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c index 7d8fd04..d9bcd6e 100644 --- a/drivers/watchdog/s3c2410_wdt.c +++ b/drivers/watchdog/s3c2410_wdt.c @@ -188,7 +188,7 @@ static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeou if (timeout < 1) return -EINVAL; - freq /= 128; + freq = DIV_ROUND_UP(freq, 128); count = timeout * freq; DBG("%s: count=%d, timeout=%d, freq=%lu\n", @@ -200,21 +200,18 @@ static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeou */ if (count >= 0x10000) { - for (divisor = 1; divisor <= 0x100; divisor++) { - if ((count / divisor) < 0x10000) - break; - } + divisor = DIV_ROUND_UP(count, 0xffff); - if ((count / divisor) >= 0x10000) { + if (divisor > 0x100) { dev_err(wdt->dev, "timeout %d too big\n", timeout); return -EINVAL; } } DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n", - __func__, timeout, divisor, count, count/divisor); + __func__, timeout, divisor, count, DIV_ROUND_UP(count, divisor)); - count /= divisor; + count = DIV_ROUND_UP(count, divisor); wdt->count = count; /* update the pre-scaler */