From patchwork Fri Dec 15 08:52:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 10114219 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EB5CD60231 for ; Fri, 15 Dec 2017 09:00:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD44028B63 for ; Fri, 15 Dec 2017 09:00:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CFB8E29F27; Fri, 15 Dec 2017 09:00:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5215228B63 for ; Fri, 15 Dec 2017 09:00:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=nK+pRAdje4BA3wYfY46x5rnDd+Nm3E7IF4BoeTQGTko=; b=rcZ1JNjVz10jnUt88Eq2G1957u 12syOqQVMBpmg9o9adYbr/Bvsr7rpnV/oHG2x4T7/tb6/yf3f0v4TiYW6t8ABML22YmpQQaFE4fTE J+EknrZlL7GnPeFijxx7JDTST6Y/ERs2YSr1oFyG8zsQOJEC4ia67sF00z+O3JlaSJLBDSqltlG1p OcGgMUNEZEUWPtMXGa4ymHCjitvYmDRUqk1YWfkZvllT/cEYLsQXG9aQol5U6BgSa7YbSm2bWI3u0 HseZxyNA9tqxYBHIp1hPhPXIfOTK7X7M9A2bSLX/e2HOxbOIjpJXOlyHWX1DesOBW/XbFBCQK6/WV cqFd4CXQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1ePlqm-0005iZ-44; Fri, 15 Dec 2017 09:00:12 +0000 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1ePlkK-0001Cy-1W for linux-arm-kernel@lists.infradead.org; Fri, 15 Dec 2017 08:53:39 +0000 Received: by mail-wm0-x243.google.com with SMTP id n138so15940463wmg.2 for ; Fri, 15 Dec 2017 00:53:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7UYSmJ//9Mfom7wNt9+xW9mt5SqRdz1LBQvLuanRgz0=; b=H8MfPQhIj0f7Q3kSiaGLwur9RaQ+bVA/5/8ZCOUzf5mDnEyyZekYhB349AEeug9yHB 88f9t3RlYt3ohcrQ1EvKPWCmGNqhXw2UzTJ7d3+gAvm42tIbnpOKYlRt24CZRV2jBUUZ sh/qK5UHdWkas4vY99GdLKjr0sIdigoZjHuZI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7UYSmJ//9Mfom7wNt9+xW9mt5SqRdz1LBQvLuanRgz0=; b=TR/G4MW+dF7dKt4oD61NYDu7xEAxqpN0OrtJgzNgWg7mq/PSN7Qxj9NqDKW54+HIOx Kzovf8dE2m57/USyElVUI1Hjq1alGhVTX+c0V7MI9qAwD6QM4uPMRvPu0zf2Um6keouu UYjMQsKlh/qJlr9fOmV0fz8H5tRV0uH3VBlsD3IKcA1nyLbiH0EdNDPjLqZjzDpYK7cu CuoaIrd1er3a31H7gXan28WSSMKgnJ8r9J31AE6uOYnwoyOGU5yIkMFjC+y4cqG+myyO EOyf8H7xCWmEShQ4atRMKk1Kvbhk/ocdYC6P4b0kouvfpUhWVc+FxZ9nwK3gO8AmdyvZ yUxg== X-Gm-Message-State: AKGB3mLesRD0+9lBqoVE4mVD/Ugm9SdN0OmuLqZr6D4/B18sXByjxSMJ cqZOrmB6qvm9lnER9HCoGa23kg== X-Google-Smtp-Source: ACJfBotzP9FMMjXuE9WHA3rfUzvoaTkaVaU4X4vJeBiehtfa0pUbaRdRplcjFlkMFMX5gH3bp8KefQ== X-Received: by 10.28.106.16 with SMTP id f16mr4892031wmc.58.1513327989970; Fri, 15 Dec 2017 00:53:09 -0800 (PST) Received: from lmecxl0911.lme.st.com ([80.215.40.214]) by smtp.gmail.com with ESMTPSA id r14sm3211803wra.71.2017.12.15.00.53.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Dec 2017 00:53:09 -0800 (PST) From: Benjamin Gaignard X-Google-Original-From: Benjamin Gaignard To: mark.rutland@arm.com, linux@armlinux.org.uk, mcoquelin.stm32@gmail.com, alexandre.torgue@st.com, daniel.lezcano@linaro.org, tglx@linutronix.de Subject: [PATCH 2/4] clocksource: stm32: use prescaler to adjust the resolution Date: Fri, 15 Dec 2017 09:52:45 +0100 Message-Id: <20171215085247.14946-3-benjamin.gaignard@st.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171215085247.14946-1-benjamin.gaignard@st.com> References: <20171215085247.14946-1-benjamin.gaignard@st.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171215_005333_229335_6B99A69C X-CRM114-Status: GOOD ( 13.58 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Benjamin Gaignard MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Rather than use fixed prescaler values compute it to get a clock as close as possible of 10KHz and a resolution of 0.1ms. Signed-off-by: Benjamin Gaignard --- drivers/clocksource/timer-stm32.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/drivers/clocksource/timer-stm32.c b/drivers/clocksource/timer-stm32.c index 23a321cca45b..de721d318065 100644 --- a/drivers/clocksource/timer-stm32.c +++ b/drivers/clocksource/timer-stm32.c @@ -37,6 +37,11 @@ #define TIM_EGR_UG BIT(0) +#define MAX_TIM_PSC 0xFFFF + +/* Target a 10KHz clock to get a resolution of 0.1 ms */ +#define TARGETED_CLK_RATE 10000 + static int stm32_clock_event_shutdown(struct clock_event_device *evt) { struct timer_of *to = to_timer_of(evt); @@ -83,7 +88,7 @@ static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id) static void __init stm32_clockevent_init(struct timer_of *to) { unsigned long max_delta; - int prescaler; + unsigned long prescaler; to->clkevt.name = "stm32_clockevent"; to->clkevt.features = CLOCK_EVT_FEAT_PERIODIC; @@ -96,13 +101,17 @@ static void __init stm32_clockevent_init(struct timer_of *to) /* Detect whether the timer is 16 or 32 bits */ writel_relaxed(~0U, timer_of_base(to) + TIM_ARR); max_delta = readl_relaxed(timer_of_base(to) + TIM_ARR); - if (max_delta == ~0U) { - prescaler = 1; + to->clkevt.rating = 50; + if (max_delta == ~0U) to->clkevt.rating = 250; - } else { - prescaler = 1024; - to->clkevt.rating = 50; - } + + /* + * Get the highest possible prescaler value to be as close + * as possible of TARGETED_CLK_RATE + */ + prescaler = DIV_ROUND_CLOSEST(timer_of_rate(to), TARGETED_CLK_RATE); + if (prescaler > MAX_TIM_PSC) + prescaler = MAX_TIM_PSC; writel_relaxed(0, timer_of_base(to) + TIM_ARR); writel_relaxed(prescaler - 1, timer_of_base(to) + TIM_PSC);