From patchwork Wed Apr 9 19:45:33 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Brugger X-Patchwork-Id: 3957501 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CA5209F387 for ; Wed, 9 Apr 2014 19:48:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B0E01206B7 for ; Wed, 9 Apr 2014 19:48:56 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 83637206B1 for ; Wed, 9 Apr 2014 19:48:55 +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 1WXyTH-0004vr-7t; Wed, 09 Apr 2014 19:47:44 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1WXyT2-0002Bw-7B; Wed, 09 Apr 2014 19:47:28 +0000 Received: from mail-we0-x22c.google.com ([2a00:1450:400c:c03::22c]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1WXyS1-00024b-UG for linux-arm-kernel@lists.infradead.org; Wed, 09 Apr 2014 19:46:29 +0000 Received: by mail-we0-f172.google.com with SMTP id t61so2990393wes.3 for ; Wed, 09 Apr 2014 12:46:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hsYcjmR1TFgTpflsfxJOWILjJF/OGYsKsvKOhm8W3qA=; b=FJWOzdcghm/JrLsd9ttHpnUbz/gjuQjaiZrx3iVhUeVXxzUiwgaR6cQlU42iMIcG/B dub5GXYpJB/eF6yOHf+AH5tdX0yEgVsFNtutjYO8RRcEEvDHjFf/XNVuCG/bKtjvbKCO NeR/snXkzcFoLn1lFj60GJtofArH+MJ8Tpte1Cq4aROGjEzwHUvWRQbs2eaEU5+kfOZB D1Z7cZutEF9zRpkOcxuGf9qs3lqeLLbV79wPDaXpK/OGBap6TdjdcBwss49eDI/LmIVK JnkMUv1WxH7d8dwpJKFf1WAbBxzftR9Ssptqn+cxChanQGet8uHgT4EFD4xQXPqbViG/ vQmA== X-Received: by 10.194.173.193 with SMTP id bm1mr4646481wjc.55.1397072763763; Wed, 09 Apr 2014 12:46:03 -0700 (PDT) Received: from localhost.localdomain (87.Red-88-0-22.dynamicIP.rima-tde.net. [88.0.22.87]) by mx.google.com with ESMTPSA id vh3sm3069256wjc.18.2014.04.09.12.46.01 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 09 Apr 2014 12:46:03 -0700 (PDT) From: Matthias Brugger To: linux-kernel@vger.kernel.org Subject: [PATCH 1/4] clocksource: Add support for the Mediatek SoCs Date: Wed, 9 Apr 2014 21:45:33 +0200 Message-Id: <1397072736-10793-2-git-send-email-matthias.bgg@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1397072736-10793-1-git-send-email-matthias.bgg@gmail.com> References: <1397072736-10793-1-git-send-email-matthias.bgg@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140409_154627_649231_86FDAF19 X-CRM114-Status: GOOD ( 25.30 ) X-Spam-Score: -2.0 (--) Cc: mark.rutland@arm.com, andrew@lunn.ch, linux-doc@vger.kernel.org, thierry.reding@gmail.com, heiko.stuebner@bq.com, linux@arm.linux.org.uk, daniel.lezcano@linaro.org, florian.vaussard@epfl.ch, sebastian.hesselbarth@gmail.com, devicetree@vger.kernel.org, jason@lakedaemon.net, pawel.moll@arm.com, ijc+devicetree@hellion.org.uk, robh+dt@kernel.org, matthias.bgg@gmail.com, tglx@linutronix.de, linux-arm-kernel@lists.infradead.org, rdunlap@infradead.org, silvio.fricke@gmail.com, galak@codeaurora.org, olof@lixom.net, jic23@kernel.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.4 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, T_DKIM_INVALID, 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 This patch adds a clock source and clock event for the timer found on the Mediatek SoCs. The Mediatek General Porpose Timer block provides five 32 bit timers and one 64 bit timer. Two 32 bit timers are used: TIMER1: clock events supporting periodic and oneshot events TIMER2: clock source configured as a free running counter The General Porpose Timer block can be run with two clocks. A 13 MHz system clock and the RTC clock running at 32 KHz. This implementation uses the system clock. Signed-off-by: Matthias Brugger --- drivers/clocksource/Kconfig | 4 + drivers/clocksource/Makefile | 1 + drivers/clocksource/mtk_timer.c | 248 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 253 insertions(+) create mode 100644 drivers/clocksource/mtk_timer.c diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig index 96918e1..bb29321 100644 --- a/drivers/clocksource/Kconfig +++ b/drivers/clocksource/Kconfig @@ -144,6 +144,10 @@ config VF_PIT_TIMER config SYS_SUPPORTS_SH_CMT bool +config MTK_TIMER + bool + + config SYS_SUPPORTS_SH_MTU2 bool diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile index 98cb6c5..619d302 100644 --- a/drivers/clocksource/Makefile +++ b/drivers/clocksource/Makefile @@ -33,6 +33,7 @@ obj-$(CONFIG_CLKSRC_EXYNOS_MCT) += exynos_mct.o obj-$(CONFIG_CLKSRC_SAMSUNG_PWM) += samsung_pwm_timer.o obj-$(CONFIG_VF_PIT_TIMER) += vf_pit_timer.o obj-$(CONFIG_CLKSRC_QCOM) += qcom-timer.o +obj-$(CONFIG_MTK_TIMER) += mtk_timer.o obj-$(CONFIG_ARM_ARCH_TIMER) += arm_arch_timer.o obj-$(CONFIG_ARM_GLOBAL_TIMER) += arm_global_timer.o diff --git a/drivers/clocksource/mtk_timer.c b/drivers/clocksource/mtk_timer.c new file mode 100644 index 0000000..bf901e3 --- /dev/null +++ b/drivers/clocksource/mtk_timer.c @@ -0,0 +1,248 @@ +/* + * Mediatek SoCs General-Purpose Timer handling. + * + * Copyright (C) 2014 Matthias Brugger + * + * Matthias Brugger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define GPT_IRQ_EN_REG 0x00 +#define GPT_IRQ_ENABLE(val) BIT(val-1) +#define GPT_IRQ_ST_REG 0x04 +#define GPT_IRQ_ACK_REG 0x08 +#define GPT_IRQ_ACK(val) BIT(val-1) + +#define TIMER_CTRL_REG(val) (0x10 * val) +#define TIMER_CTRL_OP(val) (((val) & 0x3) << 4) +#define TIMER_CTRL_OP_ONESHOT (0) +#define TIMER_CTRL_OP_REPEAT (1) +#define TIMER_CTRL_OP_KEEPGO (2) +#define TIMER_CTRL_OP_FREERUN (3) +#define TIMER_CTRL_CLEAR (2) +#define TIMER_CTRL_ENABLE (1) +#define TIMER_CTRL_DISABLE (0) + +#define TIMER_CLK_REG(val) (0x04 + (0x10 * val)) +#define TIMER_CLK_SRC(val) (((val) & 0x1) << 4) +#define TIMER_CLK_SRC_SYS13M (0) +#define TIMER_CLK_SRC_RTC32K (1) +#define TIMER_CLK_DIV1 (0x0) +#define TIMER_CLK_DIV2 (0x1) +#define TIMER_CLK_DIV3 (0x2) +#define TIMER_CLK_DIV4 (0x3) +#define TIMER_CLK_DIV5 (0x4) +#define TIMER_CLK_DIV6 (0x5) +#define TIMER_CLK_DIV7 (0x6) +#define TIMER_CLK_DIV8 (0x7) +#define TIMER_CLK_DIV9 (0x8) +#define TIMER_CLK_DIV10 (0x9) +#define TIMER_CLK_DIV11 (0xA) +#define TIMER_CLK_DIV12 (0xB) +#define TIMER_CLK_DIV13 (0xC) +#define TIMER_CLK_DIV16 (0xD) +#define TIMER_CLK_DIV32 (0xE) +#define TIMER_CLK_DIV64 (0xF) + +#define TIMER_CNT_REG(val) (0x08 + (0x10 * val)) +#define TIMER_CMP_REG(val) (0x0C + (0x10 * val)) + +#define GPT_CLK_EVT 1 +#define GPT_CLK_SRC 2 + +static void __iomem *gpt_base; +static u32 ticks_per_jiffy; + +static void mtk_clkevt_time_stop(u8 timer) +{ + u32 val = readl(gpt_base + TIMER_CTRL_REG(timer)); + writel(val & ~TIMER_CTRL_ENABLE, gpt_base + TIMER_CTRL_REG(timer)); +} + +static void mtk_clkevt_time_setup(unsigned long delay, u8 timer) +{ + writel(delay, gpt_base + TIMER_CMP_REG(timer)); +} + +static void mtk_clkevt_time_start(bool periodic, u8 timer) +{ + u32 val; + + /* Acknowledge interrupt */ + writel(GPT_IRQ_ACK(timer), gpt_base + GPT_IRQ_ACK_REG); + + val = readl(gpt_base + TIMER_CTRL_REG(timer)); + + /* Clear 2 bit timer operation mode field */ + val &= ~TIMER_CTRL_OP(0x3); + + if (periodic) + val |= TIMER_CTRL_OP(TIMER_CTRL_OP_REPEAT); + else + val |= TIMER_CTRL_OP(TIMER_CTRL_OP_ONESHOT); + + writel(val | TIMER_CTRL_ENABLE | TIMER_CTRL_CLEAR, + gpt_base + TIMER_CTRL_REG(timer)); +} + +static void mtk_clkevt_mode(enum clock_event_mode mode, + struct clock_event_device *clk) +{ + mtk_clkevt_time_stop(GPT_CLK_EVT); + + switch (mode) { + case CLOCK_EVT_MODE_PERIODIC: + mtk_clkevt_time_setup(ticks_per_jiffy, GPT_CLK_EVT); + mtk_clkevt_time_start(true, GPT_CLK_EVT); + break; + case CLOCK_EVT_MODE_ONESHOT: + mtk_clkevt_time_start(false, GPT_CLK_EVT); + break; + case CLOCK_EVT_MODE_UNUSED: + case CLOCK_EVT_MODE_SHUTDOWN: + default: + /* No more interrupts will occur as source is disabled */ + break; + } +} + +static int mtk_clkevt_next_event(unsigned long evt, + struct clock_event_device *unused) +{ + mtk_clkevt_time_stop(GPT_CLK_EVT); + mtk_clkevt_time_setup(evt, GPT_CLK_EVT); + mtk_clkevt_time_start(false, GPT_CLK_EVT); + + return 0; +} + +static struct clock_event_device mtk_clockevent = { + .name = "mtk_tick", + .rating = 300, + .shift = 32, + .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, + .set_mode = mtk_clkevt_mode, + .set_next_event = mtk_clkevt_next_event, +}; + + +static irqreturn_t mtk_timer_interrupt(int irq, void *dev_id) +{ + struct clock_event_device *evt = (struct clock_event_device *)dev_id; + + /* Acknowledge timer0 irq */ + writel(GPT_IRQ_ACK(GPT_CLK_EVT), gpt_base + GPT_IRQ_ACK_REG); + evt->event_handler(evt); + + return IRQ_HANDLED; +} + +static void mtk_timer_global_reset(void) +{ + /* Disable all interrupts */ + writel(0x0, gpt_base + GPT_IRQ_EN_REG); + /* Acknowledge all interrupts */ + writel(0x3f, gpt_base + GPT_IRQ_ACK_REG); +} + +static void mtk_timer_reset(u8 timer) +{ + writel(TIMER_CTRL_CLEAR | TIMER_CTRL_DISABLE, + gpt_base + TIMER_CTRL_REG(timer)); + writel(0x0, gpt_base + TIMER_CMP_REG(timer)); +} + +static struct irqaction mtk_timer_irq = { + .name = "mtk_timer0", + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL, + .handler = mtk_timer_interrupt, + .dev_id = &mtk_clockevent, +}; + +static u32 mtk_timer_sched_read(void) +{ + return readl(gpt_base + TIMER_CNT_REG(GPT_CLK_SRC)); +} + +static void __init mtk_timer_init(struct device_node *node) +{ + unsigned long rate = 0; + struct clk *clk; + int ret, irq; + u32 val; + + gpt_base = of_iomap(node, 0); + if (!gpt_base) + panic("Can't map registers"); + + irq = irq_of_parse_and_map(node, 0); + if (irq <= 0) + panic("Can't parse IRQ"); + + clk = of_clk_get_by_name(node, "sys_clk"); + if (IS_ERR(clk)) + panic("Can't get timer clock"); + clk_prepare_enable(clk); + + rate = clk_get_rate(clk); + + mtk_timer_global_reset(); + + /* Configure clock source */ + mtk_timer_reset(GPT_CLK_SRC); + + writel(TIMER_CLK_SRC(TIMER_CLK_SRC_SYS13M) | TIMER_CLK_DIV1, + gpt_base + TIMER_CLK_REG(GPT_CLK_SRC)); + + writel(TIMER_CTRL_OP(TIMER_CTRL_OP_FREERUN) | TIMER_CTRL_ENABLE, + gpt_base + TIMER_CTRL_REG(GPT_CLK_SRC)); + + clocksource_mmio_init(gpt_base + TIMER_CNT_REG(GPT_CLK_SRC), node->name, + rate, 300, 32, clocksource_mmio_readl_up); + + ticks_per_jiffy = DIV_ROUND_UP(rate, HZ); + + /* Configure clock event */ + mtk_timer_reset(GPT_CLK_EVT); + + writel(TIMER_CLK_SRC(TIMER_CLK_SRC_SYS13M) | TIMER_CLK_DIV1, + gpt_base + TIMER_CLK_REG(GPT_CLK_EVT)); + writel(0, gpt_base + TIMER_CMP_REG(GPT_CLK_EVT)); + + writel(TIMER_CTRL_OP(TIMER_CTRL_OP_REPEAT) | TIMER_CTRL_ENABLE, + gpt_base + TIMER_CTRL_REG(GPT_CLK_EVT)); + + ret = setup_irq(irq, &mtk_timer_irq); + if (ret) + pr_warn("failed to setup irq %d\n", irq); + + /* Enable timer0 interrupt */ + val = readl(gpt_base + GPT_IRQ_EN_REG); + writel(val | GPT_IRQ_ENABLE(GPT_CLK_EVT), gpt_base + GPT_IRQ_EN_REG); + + mtk_clockevent.cpumask = cpumask_of(0); + + clockevents_config_and_register(&mtk_clockevent, rate, 0x3, + 0xffffffff); +} +CLOCKSOURCE_OF_DECLARE(mtk_mt6589, "mediatek,mtk6589-timer", mtk_timer_init); +