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: 3244711 Return-Path: X-Original-To: patchwork-linux-samsung-soc@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 E2C0EC045B for ; Wed, 27 Nov 2013 01:41:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 147002053F for ; Wed, 27 Nov 2013 01:41:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2826120503 for ; Wed, 27 Nov 2013 01:41:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753947Ab3K0Blx (ORCPT ); Tue, 26 Nov 2013 20:41:53 -0500 Received: from mail-yh0-f74.google.com ([209.85.213.74]:35800 "EHLO mail-yh0-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758233Ab3K0A6G (ORCPT ); Tue, 26 Nov 2013 19:58:06 -0500 Received: by mail-yh0-f74.google.com with SMTP id a41so604068yho.5 for ; Tue, 26 Nov 2013 16:58:06 -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=Xyn4mVIIChhhyphsYiO6OzUPgYWmcgbtHLzJmR/uz55cbIwfDb195mLrbcBfiowmxe FddhZWUki5wZQFSn+oGzB5AaP7Enx7M+wGJkUwb3D8CZzBgG3iaUNmaZ7SBHxe+XaJpP ci/sIHKwv5oYBRFkZEEl+fnFB3YZY0QQiShPuoG1TLRJA9XLt7CRuDCodUf8WDlkJRg8 OvB/wFNU0dAL6MSHRFBCQJV5l+pX09m7Qa9RC/mOCUo8E0TqZ1yG/kNIhJbkkbqOy0St bnPnuceBB+4JFcZCd2IJEpVX+x1IJF9ZSublKvWeJxyYiOx+B58HmMwc7IApLHbDa8AU JZxg== X-Gm-Message-State: ALoCoQlr81V05r9IgR9mU/f21GsRBThJnKXW3GMxCWIa4XVYQAhf1Gc8g5gV5gs7eqJlkxoZC9siawjbfKX0qHvWVBmUBZKHk9CJOLIYXJLf2WN6IIFdpDpbX+zb+hH0eqUDCaJzjZB5DyJcH3z0F0S4/Rb7ojypfH6Io3YFk8KDOlKornIP2meuIEai2q3vY9Q59F3AGraM8cWcj2Axgzlu0I8IE/L5sA== 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 Cc: Leela Krishna Amudala , Olof Johansson , Tomasz Figa , Kukjin Kim , Doug Anderson , Ben Dooks , linux-arm-kernel@lists.infradead.org, linux-samsung-soc@vger.kernel.org, linux-watchdog@vger.kernel.org, linux-kernel@vger.kernel.org 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> Sender: linux-samsung-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-samsung-soc@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 */