From patchwork Thu Sep 14 07:56:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Gaignard X-Patchwork-Id: 9952559 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 C2DCB60230 for ; Thu, 14 Sep 2017 08:09:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B3D5828B30 for ; Thu, 14 Sep 2017 08:09:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A889F28C6F; Thu, 14 Sep 2017 08:09:46 +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=unavailable 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 C2EFD28BF8 for ; Thu, 14 Sep 2017 08:09:45 +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=porvk3++XJRxR8UUVxS0Vitdg1SDOhg9sG70L5VDDg8=; b=I5A6ct4TM6PmlIG8AM5bggviUm yJVuY/ZYjOWR+ERybdzOoK6Uw7hRc58wTkgwdKxpQLHhl989NYa/xfkfcXCDFgzX/2oHjP+d+0Wwi y+4uNCCikXKldvC9PU0oqcFoetWIzsqS2gi/TMDWKGRwOBJH0ZfzIoVjjCAVIeJuiOPggL+YUEi0w RE5MpKgDNZTpaUXeC6JiQ4i6sgbZIX364P3Vbz5WKC31ixZbPj7xvx562lWC7D0q8lxqFF7oIG9i4 ARSKbWBNPTFfrN1pWb0lHefy5JMUgD8/ROC1UcmEDW1ovts+BzL34TsasxM87SH+X9BXOe/NjG/nB AN5w9NFA==; 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 1dsPDH-0003eR-U4; Thu, 14 Sep 2017 08:09:31 +0000 Received: from mail-wr0-x22d.google.com ([2a00:1450:400c:c0c::22d]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dsP2F-00064f-Nu for linux-arm-kernel@lists.infradead.org; Thu, 14 Sep 2017 07:58:10 +0000 Received: by mail-wr0-x22d.google.com with SMTP id 108so4577586wra.5 for ; Thu, 14 Sep 2017 00:57:47 -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=WtL6qbRFihroGrr0tIynAgCFYyusKeFTZ/NEiXPSbSc=; b=AQU7BkCBNGg701a+TLAsPTcjUJSuQ8F3IbLW/H28LKHco8AoSRW3Xt4LgvjCE3mON1 eav/rzTHg9UnagbGsxGrwHE7QnHZmBBmWp12YE/BeOT+fwaXz0Ks4NVaxwJVx9EqExoH 1WyI3qMXhrIzpizSRBiRlnfFmCY1uasAZoscA= 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=WtL6qbRFihroGrr0tIynAgCFYyusKeFTZ/NEiXPSbSc=; b=gpp6v9irBqbuktYeJbtE5uTBNeC4Gjlhsk+PtQ8YZ/ofeWuvoO3QDDXX5IUDXlrCWo 3yWajYfnbCe/X+UpLUIG5hDhTmMTbTYTvyCtRfxrw44MJqJMPVQpBJXSikEOtRqA5yyo LDJt17LgeJjQdSnVs7v1vpypHYoxFVky+TYzk3ouKY8gyuvyz6RjFGtZ4VAy/pNJlbob zKAHiC7U5mvsS6SWrQcvnhZ+WCeko5j8V17KYcJHNUmpShseT6ENiw342ebKixBFAkIC 9J9snHdATtJwsZAaJnMLBb8uqO0N6J+feXXkCwUFMuyXNi9fTSJF4VcDkTwPsj7BbXpz BqGg== X-Gm-Message-State: AHPjjUiHlZk8f/2G+EEZX9308CeOXAipuhjOVsmi4W6Ewmq1+GcoGcP4 tah9xoHJpw7syj35 X-Google-Smtp-Source: ADKCNb6b0jjR3xSb/IEmlsgTDrIU5/DPB3uY7G6AR3bWQzIyO1tTN1Slu3NYv4JpZ7WCNwC3eT15ng== X-Received: by 10.223.193.71 with SMTP id w7mr18982420wre.158.1505375865817; Thu, 14 Sep 2017 00:57:45 -0700 (PDT) Received: from lmecxl0911.lme.st.com ([80.214.22.183]) by smtp.gmail.com with ESMTPSA id c56sm15362709wrc.1.2017.09.14.00.57.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 14 Sep 2017 00:57:45 -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 Subject: [PATCH 1/2] clocksource: stm32: rework driver to use only one timer Date: Thu, 14 Sep 2017 09:56:51 +0200 Message-Id: <1505375812-19037-2-git-send-email-benjamin.gaignard@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1505375812-19037-1-git-send-email-benjamin.gaignard@linaro.org> References: <1505375812-19037-1-git-send-email-benjamin.gaignard@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170914_005808_164221_EF099702 X-CRM114-Status: GOOD ( 17.26 ) 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: devicetree@vger.kernel.org, Benjamin Gaignard , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org 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 Rework driver code to use only one timer for both clocksource and clockevent. This patch also forbids to use 16 bits timers because they are not enough accurate. Do some clean up in structures and functions names too. Signed-off-by: Ludovic Barre Signed-off-by: Benjamin Gaignard --- drivers/clocksource/timer-stm32.c | 259 +++++++++++++++++++++++--------------- 1 file changed, 155 insertions(+), 104 deletions(-) diff --git a/drivers/clocksource/timer-stm32.c b/drivers/clocksource/timer-stm32.c index 8f24237..648c10a 100644 --- a/drivers/clocksource/timer-stm32.c +++ b/drivers/clocksource/timer-stm32.c @@ -16,175 +16,226 @@ #include #include #include +#include +#include #define TIM_CR1 0x00 #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) -struct stm32_clock_event_ddata { +struct stm32_clock_event { struct clock_event_device evtdev; unsigned periodic_top; - void __iomem *base; + void __iomem *regs; }; static int stm32_clock_event_shutdown(struct clock_event_device *evtdev) { - struct stm32_clock_event_ddata *data = - container_of(evtdev, struct stm32_clock_event_ddata, evtdev); - void *base = data->base; + struct stm32_clock_event *ce = + container_of(evtdev, struct stm32_clock_event, evtdev); + + writel_relaxed(0, ce->regs + TIM_DIER); - writel_relaxed(0, base + TIM_CR1); return 0; } -static int stm32_clock_event_set_periodic(struct clock_event_device *evtdev) +static int stm32_clock_event_set_next_event(unsigned long evt, + struct clock_event_device *evtdev) { - struct stm32_clock_event_ddata *data = - container_of(evtdev, struct stm32_clock_event_ddata, evtdev); - void *base = data->base; + struct stm32_clock_event *ce = + container_of(evtdev, struct stm32_clock_event, evtdev); + unsigned long cnt; + + cnt = readl_relaxed(ce->regs + TIM_CNT); + writel_relaxed(cnt + evt, ce->regs + TIM_CCR1); + writel_relaxed(TIM_DIER_CC1IE, ce->regs + TIM_DIER); - writel_relaxed(data->periodic_top, base + TIM_ARR); - writel_relaxed(TIM_CR1_ARPE | TIM_CR1_CEN, base + TIM_CR1); return 0; } -static int stm32_clock_event_set_next_event(unsigned long evt, - struct clock_event_device *evtdev) +static int stm32_clock_event_set_periodic(struct clock_event_device *evtdev) { - struct stm32_clock_event_ddata *data = - container_of(evtdev, struct stm32_clock_event_ddata, evtdev); + struct stm32_clock_event *ce = + container_of(evtdev, struct stm32_clock_event, evtdev); - writel_relaxed(evt, data->base + TIM_ARR); - writel_relaxed(TIM_CR1_ARPE | TIM_CR1_OPM | TIM_CR1_CEN, - data->base + TIM_CR1); + return stm32_clock_event_set_next_event(ce->periodic_top, evtdev); +} - return 0; +static int stm32_clock_event_set_oneshot(struct clock_event_device *evtdev) +{ + return stm32_clock_event_set_next_event(0, evtdev); } static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id) { - struct stm32_clock_event_ddata *data = dev_id; + struct stm32_clock_event *ce = dev_id; + + writel_relaxed(0, ce->regs + TIM_SR); - writel_relaxed(0, data->base + TIM_SR); + if (clockevent_state_periodic(&ce->evtdev)) + stm32_clock_event_set_periodic(&ce->evtdev); - data->evtdev.event_handler(&data->evtdev); + if (clockevent_state_oneshot(&ce->evtdev)) + stm32_clock_event_shutdown(&ce->evtdev); + + ce->evtdev.event_handler(&ce->evtdev); return IRQ_HANDLED; } -static struct stm32_clock_event_ddata clock_event_ddata = { - .evtdev = { - .name = "stm32 clockevent", - .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC, - .set_state_shutdown = stm32_clock_event_shutdown, - .set_state_periodic = stm32_clock_event_set_periodic, - .set_state_oneshot = stm32_clock_event_shutdown, - .tick_resume = stm32_clock_event_shutdown, - .set_next_event = stm32_clock_event_set_next_event, - .rating = 200, - }, -}; +static int __init stm32_clockevent_init(struct device_node *np, + void __iomem *base, + struct clk *clk, int irq) +{ + struct stm32_clock_event *ce; + unsigned long rate; + int err; + + ce = kzalloc(sizeof(*ce), GFP_KERNEL); + if (!ce) + return -ENOMEM; + + ce->regs = base; + ce->evtdev.name = "stm32_clockevent"; + ce->evtdev.features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC; + ce->evtdev.set_state_shutdown = stm32_clock_event_shutdown; + ce->evtdev.set_state_periodic = stm32_clock_event_set_periodic; + ce->evtdev.set_state_oneshot = stm32_clock_event_set_oneshot; + ce->evtdev.tick_resume = stm32_clock_event_shutdown; + ce->evtdev.set_next_event = stm32_clock_event_set_next_event; + ce->evtdev.rating = 200; -static int __init stm32_clockevent_init(struct device_node *np) + rate = clk_get_rate(clk); + ce->periodic_top = DIV_ROUND_CLOSEST(rate, HZ); + + writel_relaxed(0, ce->regs + TIM_DIER); + writel_relaxed(0, ce->regs + TIM_SR); + + err = request_irq(irq, stm32_clock_event_handler, IRQF_TIMER, + "stm32 clockevent", ce); + if (err) { + kfree(ce); + return err; + } + + clockevents_config_and_register(&ce->evtdev, rate, 0x60, ~0U); + + return 0; +} + +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 device_node *node, + void __iomem *regs, + struct clk *clk) +{ + unsigned long rate; + + rate = clk_get_rate(clk); + + writel_relaxed(~0U, regs + TIM_ARR); + writel_relaxed(0, regs + TIM_PSC); + writel_relaxed(0, regs + TIM_SR); + writel_relaxed(0, regs + TIM_DIER); + writel_relaxed(0, regs + TIM_SR); + writel_relaxed(TIM_CR1_ARPE | TIM_CR1_UDIS, regs + TIM_CR1); + + /* Make sure that registers are updated */ + writel_relaxed(TIM_EGR_UG, regs + TIM_EGR); + + /* Enable controller */ + writel_relaxed(TIM_CR1_ARPE | TIM_CR1_UDIS | TIM_CR1_CEN, + regs + TIM_CR1); + + stm32_timer_cnt = regs + TIM_CNT; + sched_clock_register(stm32_read_sched_clock, 32, rate); + + return clocksource_mmio_init(stm32_timer_cnt, "stm32_timer", + rate, 250, 32, clocksource_mmio_readl_up); +} + +static int __init stm32_timer_init(struct device_node *node) { - struct stm32_clock_event_ddata *data = &clock_event_ddata; - struct clk *clk; struct reset_control *rstc; - unsigned long rate, max_delta; - int irq, ret, bits, prescaler = 1; + void __iomem *timer_base; + unsigned long max_arr; + struct clk *clk; + int irq, err; - clk = of_clk_get(np, 0); - if (IS_ERR(clk)) { - ret = PTR_ERR(clk); - pr_err("failed to get clock for clockevent (%d)\n", ret); - goto err_clk_get; + timer_base = of_io_request_and_map(node, 0, of_node_full_name(node)); + if (IS_ERR(timer_base)) { + pr_err("Can't map registers\n"); + goto out; } - ret = clk_prepare_enable(clk); - if (ret) { - pr_err("failed to enable timer clock for clockevent (%d)\n", - ret); - goto err_clk_enable; + irq = irq_of_parse_and_map(node, 0); + if (irq <= 0) { + pr_err("Can't parse IRQ\n"); + goto out_unmap; } - rate = clk_get_rate(clk); + clk = of_clk_get(node, 0); + if (IS_ERR(clk)) { + pr_err("Can't get timer clock\n"); + goto out_unmap; + } - rstc = of_reset_control_get(np, NULL); + rstc = of_reset_control_get(node, NULL); if (!IS_ERR(rstc)) { reset_control_assert(rstc); reset_control_deassert(rstc); } - data->base = of_iomap(np, 0); - if (!data->base) { - ret = -ENXIO; - pr_err("failed to map registers for clockevent\n"); - goto err_iomap; - } - - irq = irq_of_parse_and_map(np, 0); - if (!irq) { - ret = -EINVAL; - pr_err("%pOF: failed to get irq.\n", np); - goto err_get_irq; + err = clk_prepare_enable(clk); + if (err) { + pr_err("Couldn't enable parent clock\n"); + goto out_clk; } /* Detect whether the timer is 16 or 32 bits */ - writel_relaxed(~0U, data->base + TIM_ARR); - max_delta = readl_relaxed(data->base + TIM_ARR); - if (max_delta == ~0U) { - prescaler = 1; - bits = 32; - } else { - prescaler = 1024; - bits = 16; + writel_relaxed(~0U, timer_base + TIM_ARR); + max_arr = readl_relaxed(timer_base + TIM_ARR); + if (max_arr != ~0U) { + err = -EINVAL; + pr_err("32 bits timer is needed\n"); + goto out_unprepare; } - writel_relaxed(0, data->base + TIM_ARR); - - writel_relaxed(prescaler - 1, data->base + TIM_PSC); - writel_relaxed(TIM_EGR_UG, data->base + TIM_EGR); - writel_relaxed(TIM_DIER_UIE, data->base + TIM_DIER); - writel_relaxed(0, data->base + TIM_SR); - - data->periodic_top = DIV_ROUND_CLOSEST(rate, prescaler * HZ); - clockevents_config_and_register(&data->evtdev, - DIV_ROUND_CLOSEST(rate, prescaler), - 0x1, max_delta); + err = stm32_clocksource_init(node, timer_base, clk); + if (err) + goto out_unprepare; - ret = request_irq(irq, stm32_clock_event_handler, IRQF_TIMER, - "stm32 clockevent", data); - if (ret) { - pr_err("%pOF: failed to request irq.\n", np); - goto err_get_irq; - } - - pr_info("%pOF: STM32 clockevent driver initialized (%d bits)\n", - np, bits); + err = stm32_clockevent_init(node, timer_base, clk, irq); + if (err) + goto out_unprepare; - return ret; + return 0; -err_get_irq: - iounmap(data->base); -err_iomap: +out_unprepare: clk_disable_unprepare(clk); -err_clk_enable: +out_clk: clk_put(clk); -err_clk_get: - return ret; +out_unmap: + iounmap(timer_base); +out: + return err; } -TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_clockevent_init); +CLOCKSOURCE_OF_DECLARE(stm32, "st,stm32-timer", stm32_timer_init);