From patchwork Mon Oct 23 09:58:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 116666 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp4452498qgn; Mon, 23 Oct 2017 03:00:31 -0700 (PDT) X-Received: by 10.101.81.70 with SMTP id g6mr11279155pgq.97.1508752831845; Mon, 23 Oct 2017 03:00:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1508752831; cv=none; d=google.com; s=arc-20160816; b=n6sGgYeDBKoD3jbZG6rriE0go3RRLFhY8dC9l/9RWjCU3vVnsAecViQhuevfwTHSKX KOJZJmRfyu4+xLkmM687+Ze3JJUwcH6mRsnFYubELPbMeZlphzXw6D/27FaelbCIBENH fZ+TJBm83drky/6VJjykCcEaLcpDBEsW51T6jVQPUse89DrPqFzqWWAtjQyi9gjvD6e5 v1NMUxlpQqprCZCEFPLwla+JiOI7aRDTFekTQa7ixS6IEbP5D0s+rHz7KteR8rVtlH9i 7Ti3xVzCuJIMb152dFBok8Qk8n5Lk8cdd2w5CDLF+30nIqOKvHSIAbNlTP0yUz47ql82 C0uA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Amjj2EpUS0lJXSOjZlPug9dtUiVl8SGDRu/zS2XCM2k=; b=OpcVBXlFzbzbl9j9akyuxSakV7Ot9Nfx49IvnYEfGnz5P1/ajccDP7Ye61Lkx85uXQ 40EAhnypL/n2gLRKnokmVOuoPfYxkx+wLLBwZ0gJivWhaakNp3+4bq+FBjKH39aLQUoN D2m/yc42dS/+max671rLoQ3tFKSkNG161XRzBzAIAp1sqeIufzq9TKh2lEy+PZSLxuuB /zqUVYoTMavl8Efa2sv6L0aGWr1P9Ru77DaLg6MmD1d9NsVX1AKIEnJJi72LhWPVNdWc GSghCAnlKIgyyYz8G+6fJdZAViRMQ24GAdVOALwr6/tKQMLKQe+Gll+tJiGbZOdm09QZ /Cwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=joohgQSc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q18si4709080pge.257.2017.10.23.03.00.31; Mon, 23 Oct 2017 03:00:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=joohgQSc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751616AbdJWKAa (ORCPT + 27 others); Mon, 23 Oct 2017 06:00:30 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:55572 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751407AbdJWJ7t (ORCPT ); Mon, 23 Oct 2017 05:59:49 -0400 Received: by mail-wm0-f66.google.com with SMTP id u138so8668600wmu.4 for ; Mon, 23 Oct 2017 02:59:48 -0700 (PDT) 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=Amjj2EpUS0lJXSOjZlPug9dtUiVl8SGDRu/zS2XCM2k=; b=joohgQSc+EZ8STB58VbJsdBSaa3uou9sZ78qaAvNbai1IYVTyTqFE+2Z1a/0hzZlJF BKPVt8YganCHWQVJACG40LCGlf0T69QN+ac7kJrTk1GCbbCuLmqoOxmyOfcBcJPyTn2q tVLrWbx/ywBQijSuJBTEgsnabB8HkbO+fzEaY= 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=Amjj2EpUS0lJXSOjZlPug9dtUiVl8SGDRu/zS2XCM2k=; b=N4/ys95TewTjFpoPg0Mryuf30QRHGx4tuasXYGsvL9qF6F/bux1EoNA/JkT0SZIAZJ KfN4cfvQNpZ4pazAxcDWG91NvNz3+AnQGOtuwRlx+qW16guHHr9V9mk95WR2SRHOdjHV Jvht6nXrYiAtTVhQWckYnnP3y5/nTBQ6uUQWMEvokR0EeHOClzNUTNrSn1ItoGFlFrz2 WByqRBq0BLOOGJJWSasHI5VZzkI616j3UpIvPQWCZRmXXvj/hI17yi/kTqT0se0GKXNF IRto0IH/mU6Ud706D/E+npgoJExrtFbO7LAoyqKQpIz8DVGlr31ttEfLjlRZV7ljmCow tTMA== X-Gm-Message-State: AMCzsaVxP3MSZz1w6WgYZiV9Hf5EE2u3y8AYA6URVcS6o+hTXDqjQ3CF W1v6mXbkqY3K3z7Hm2HbpcDokw== X-Google-Smtp-Source: ABhQp+RTuldHQ1PzOMyRSfpU4HaLqSy8fJ/QUMvkl1JU9VSuHjtvdGpmYpGmlCIDijS78HEHdH2RvQ== X-Received: by 10.28.238.193 with SMTP id j62mr5121672wmi.44.1508752787529; Mon, 23 Oct 2017 02:59:47 -0700 (PDT) Received: from lmecxl0911.lme.st.com ([80.214.127.33]) by smtp.gmail.com with ESMTPSA id q188sm3626900wmb.43.2017.10.23.02.59.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Oct 2017 02:59:47 -0700 (PDT) From: Benjamin Gaignard To: robh+dt@kernel.org, mark.rutland@arm.com, linux@armlinux.org.uk, mcoquelin.stm32@gmail.com, alexandre.torgue@st.com, daniel.lezcano@linaro.org, tglx@linutronix.de, ludovic.barre@st.com, julien.thierry@arm.com Cc: devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Benjamin Gaignard Subject: [PATCH v7 5/6] clocksource: stm32: add clocksource support Date: Mon, 23 Oct 2017 11:58:41 +0200 Message-Id: <1508752722-4489-6-git-send-email-benjamin.gaignard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1508752722-4489-1-git-send-email-benjamin.gaignard@linaro.org> References: <1508752722-4489-1-git-send-email-benjamin.gaignard@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The stm32 timer hardware is currently only used as a clock event device, but it can be utilized as a clocksource as well. Implement this by enabling the free running counter in the hardware block and converting the clock event part from a count down event timer to a comparator based timer. Signed-off-by: Benjamin Gaignard --- drivers/clocksource/timer-stm32.c | 115 ++++++++++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 28 deletions(-) -- 2.7.4 diff --git a/drivers/clocksource/timer-stm32.c b/drivers/clocksource/timer-stm32.c index d8e5636..b00987f 100644 --- a/drivers/clocksource/timer-stm32.c +++ b/drivers/clocksource/timer-stm32.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include "timer-of.h" @@ -23,16 +25,16 @@ #define TIM_DIER 0x0c #define TIM_SR 0x10 #define TIM_EGR 0x14 +#define TIM_CNT 0x24 #define TIM_PSC 0x28 #define TIM_ARR 0x2c +#define TIM_CCR1 0x34 #define TIM_CR1_CEN BIT(0) -#define TIM_CR1_OPM BIT(3) +#define TIM_CR1_UDIS BIT(1) #define TIM_CR1_ARPE BIT(7) -#define TIM_DIER_UIE BIT(0) - -#define TIM_SR_UIF BIT(0) +#define TIM_DIER_CC1IE BIT(1) #define TIM_EGR_UG BIT(0) @@ -42,28 +44,44 @@ static int stm32_clock_event_shutdown(struct clock_event_device *evt) { struct timer_of *to = to_timer_of(evt); - writel_relaxed(0, timer_of_base(to) + TIM_CR1); + writel_relaxed(0, timer_of_base(to) + TIM_DIER); + return 0; } -static int stm32_clock_event_set_periodic(struct clock_event_device *evt) +static int stm32_clock_event_set_next_event(unsigned long evt, + struct clock_event_device *clkevt) { - struct timer_of *to = to_timer_of(evt); + struct timer_of *to = to_timer_of(clkevt); + unsigned long now, next; - writel_relaxed(timer_of_period(to), timer_of_base(to) + TIM_ARR); - writel_relaxed(TIM_CR1_ARPE | TIM_CR1_CEN, timer_of_base(to) + TIM_CR1); + next = readl_relaxed(timer_of_base(to) + TIM_CNT) + evt; + writel_relaxed(next, timer_of_base(to) + TIM_CCR1); + now = readl_relaxed(timer_of_base(to) + TIM_CNT); + + if (next - now > evt) + return -ETIME; + + writel_relaxed(TIM_DIER_CC1IE, timer_of_base(to) + TIM_DIER); return 0; } -static int stm32_clock_event_set_next_event(unsigned long evt, - struct clock_event_device *clkevt) +static int stm32_clock_event_set_periodic(struct clock_event_device *evt) { - struct timer_of *to = to_timer_of(clkevt); + struct timer_of *to = to_timer_of(evt); - writel_relaxed(evt, timer_of_base(to) + TIM_ARR); - writel_relaxed(TIM_CR1_ARPE | TIM_CR1_OPM | TIM_CR1_CEN, - timer_of_base(to) + TIM_CR1); + return stm32_clock_event_set_next_event(timer_of_period(to), evt); +} + +static int stm32_clock_event_set_oneshot(struct clock_event_device *evt) +{ + struct timer_of *to = to_timer_of(evt); + unsigned long val; + + val = readl_relaxed(timer_of_base(to) + TIM_CNT); + writel_relaxed(val, timer_of_base(to) + TIM_CCR1); + writel_relaxed(TIM_DIER_CC1IE, timer_of_base(to) + TIM_DIER); return 0; } @@ -75,12 +93,57 @@ static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id) writel_relaxed(0, timer_of_base(to) + TIM_SR); + if (clockevent_state_periodic(evt)) + stm32_clock_event_set_periodic(evt); + else + stm32_clock_event_shutdown(evt); + evt->event_handler(evt); return IRQ_HANDLED; } -static int __init stm32_clockevent_init(struct device_node *node) +static void __init stm32_clockevent_init(struct timer_of *to) +{ + writel_relaxed(0, timer_of_base(to) + TIM_DIER); + writel_relaxed(0, timer_of_base(to) + TIM_SR); + + clockevents_config_and_register(&to->clkevt, + timer_of_rate(to), MIN_DELTA, ~0U); +} + +static void __iomem *stm32_timer_cnt __read_mostly; +static u64 notrace stm32_read_sched_clock(void) +{ + return readl_relaxed(stm32_timer_cnt); +} + +static int __init stm32_clocksource_init(struct timer_of *to) +{ + writel_relaxed(~0U, timer_of_base(to) + TIM_ARR); + writel_relaxed(0, timer_of_base(to) + TIM_PSC); + + writel_relaxed(0, timer_of_base(to) + TIM_SR); + writel_relaxed(0, timer_of_base(to) + TIM_DIER); + writel_relaxed(TIM_CR1_ARPE | TIM_CR1_UDIS, + timer_of_base(to) + TIM_CR1); + + /* Make sure that registers are updated */ + writel_relaxed(TIM_EGR_UG, timer_of_base(to) + TIM_EGR); + + /* Enable controller */ + writel_relaxed(TIM_CR1_ARPE | TIM_CR1_UDIS | TIM_CR1_CEN, + timer_of_base(to) + TIM_CR1); + + stm32_timer_cnt = timer_of_base(to) + TIM_CNT; + sched_clock_register(stm32_read_sched_clock, 32, timer_of_rate(to)); + + return clocksource_mmio_init(stm32_timer_cnt, "stm32_timer", + timer_of_rate(to), 250, 32, + clocksource_mmio_readl_up); +} + +static int __init stm32_timer_init(struct device_node *node) { struct reset_control *rstc; unsigned long max_arr; @@ -92,12 +155,13 @@ static int __init stm32_clockevent_init(struct device_node *node) return -ENOMEM; to->flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE; + to->clkevt.name = "stm32_clockevent"; to->clkevt.rating = 200; - to->clkevt.features = CLOCK_EVT_FEAT_PERIODIC; + to->clkevt.features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC; to->clkevt.set_state_shutdown = stm32_clock_event_shutdown; to->clkevt.set_state_periodic = stm32_clock_event_set_periodic; - to->clkevt.set_state_oneshot = stm32_clock_event_shutdown; + to->clkevt.set_state_oneshot = stm32_clock_event_set_oneshot; to->clkevt.tick_resume = stm32_clock_event_shutdown; to->clkevt.set_next_event = stm32_clock_event_set_next_event; @@ -122,16 +186,11 @@ static int __init stm32_clockevent_init(struct device_node *node) goto deinit; } - writel_relaxed(0, timer_of_base(to) + TIM_ARR); - - writel_relaxed(0, timer_of_base(to) + TIM_PSC); - writel_relaxed(TIM_EGR_UG, timer_of_base(to) + TIM_EGR); - writel_relaxed(TIM_DIER_UIE, timer_of_base(to) + TIM_DIER); - writel_relaxed(0, timer_of_base(to) + TIM_SR); + ret = stm32_clocksource_init(to); + if (ret) + goto deinit; - clockevents_config_and_register(&to->clkevt, - timer_of_period(to), - MIN_DELTA, ~0U); + stm32_clockevent_init(to); return 0; @@ -143,4 +202,4 @@ static int __init stm32_clockevent_init(struct device_node *node) return ret; } -TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_clockevent_init); +TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_timer_init);