Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752107AbdFMH6x (ORCPT ); Tue, 13 Jun 2017 03:58:53 -0400 Received: from mail-bl2nam02on0076.outbound.protection.outlook.com ([104.47.38.76]:33664 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751765AbdFMH6s (ORCPT ); Tue, 13 Jun 2017 03:58:48 -0400 Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=nxp.com; nxp.com; dkim=none (message not signed) header.d=none;nxp.com; dmarc=fail action=none header.from=nxp.com; From: Dong Aisheng To: CC: , , , , , , , , , Dong Aisheng , Anson Huang Subject: [PATCH V2 2/2] timer: imx-tpm: add imx tpm timer support Date: Tue, 13 Jun 2017 15:58:45 +0800 Message-ID: <1497340725-26594-3-git-send-email-aisheng.dong@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1497340725-26594-1-git-send-email-aisheng.dong@nxp.com> References: <1497340725-26594-1-git-send-email-aisheng.dong@nxp.com> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131418143241888035;(91ab9b29-cfa4-454e-5278-08d120cd25b8);() X-Forefront-Antispam-Report: CIP:192.88.168.50;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(336005)(39410400002)(39400400002)(39850400002)(39380400002)(39860400002)(39840400002)(39450400003)(2980300002)(1109001)(1110001)(339900001)(199003)(54534003)(189002)(2906002)(189998001)(8656002)(110136004)(97736004)(54906002)(38730400002)(50466002)(85426001)(68736007)(50226002)(5003940100001)(104016004)(81156014)(105606002)(106466001)(33646002)(77096006)(2351001)(36756003)(81166006)(47776003)(305945005)(2950100002)(7416002)(575784001)(86362001)(5660300001)(356003)(48376002)(8936002)(53936002)(76176999)(50986999)(8676002)(4326008)(39060400002)(6916009)(6666003)(498600001)(2004002);DIR:OUT;SFP:1101;SCL:1;SRVR:CY1PR0301MB0908;H:tx30smr01.am.freescale.net;FPR:;SPF:Fail;PTR:InfoDomainNonexistent;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;BL2FFO11FD029;1:OZrK4ersI/Gdgo3KbACMSxrubUudhHjxEP/IGcFMhBpOcymA/AO5MfpfPPDoPZos/D3fs/4OG9cKpbi9bUtDzO1JvfOb/9CYHx+uyNZISnM8g77uM+JS6zg5uQZ5Jl1Ib4bymin5CuHptSK7feYcYgCnS4grlByquPsb8CGeqSPC336pkWAkTVoNyJ5M6zuu2oxur5oTzb8+MVW6avXYF57asnNmNOpLFgP9y4d7eVuMpVLj96QTVs9rMs0iB8jEOJMbt9rb3DVrGjdw26g4JH7CGTbWHfBVEEXDqh2Rgf2QFlP6eyOqqQB0ms4lEVppz0Cey3BCGo34J4zkzOn9U+mCLLz0g3vDw9HD01a+ml3AxX0ulHYaliP/bTVmIwLDUE75minIDQUrvikmXYSWF55Z6EXDuD5E00NElJQWRsCmtoaBt25mevp0I2rBDGWZ5sRamoXzjx9tsrxgRNpqVrg2nwiuyBmu0fSODp3zNBx++T91jGjefOHMZ/8zbTdVBJOeuRvitc27cH5VucSbkBe50jKjeIleC/sXQFaJ3q+PG7fQeEDWbW3m/sGaOAKcgCvUEnwB/uZjvzKaNbdP+VCKZfvYcPkBG3J1q3zgXwRv9pkbh0o8zStHoB+6YCELJfMcSYwFvwd1ndWv/I8MSCVnThqrbUkgEt9II4jJy9xdfYzB0iJmAYRHYcVvZkC2 MIME-Version: 1.0 Content-Type: text/plain X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY1PR0301MB0908: X-MS-Office365-Filtering-Correlation-Id: 944cd6be-dd0c-482c-561b-08d4b232035d X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(201703131430075)(201703131517081);SRVR:CY1PR0301MB0908; X-Microsoft-Exchange-Diagnostics: 1;CY1PR0301MB0908;3:hIOm/ef6J/hcEq6QScr6KqeopDYB6tnSVNDSN5FK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3Rda4KzZDa3gtwbWllM+OLsxY4vH7mFppX66/W2/0uKYNIJDhNSiTW+zLX2w4XDyc6PmGVxd4unXDJJwNV/uI28aHL8L79F5hOKj2VtNLJS6e96sn4d6jQadGgTP8y6eRaY/o4w7A== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6095135)(601004)(2401047)(5005006)(13016025)(8121501046)(13018025)(10201501046)(3002001)(100000703101)(100105400095)(93006095)(93001095)(6055026)(6096035)(20161123559100)(20161123563025)(20161123565025)(20161123556025)(201703131430075)(201703131441075)(201703131448075)(201703131433075)(201703161259150)(20161123561025)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:CY1PR0301MB0908;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(400006)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:CY1PR0301MB0908; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CY1PR0301MB0908;4:22RmyifZ9GIZFEXV9lU9Vnm7j9ioO/Nl2tEoccm6?= =?us-ascii?Q?6EFOCyRUdItdWATSQRO0IZXgjZAQNTCMHll2NY6j+kvECz2uId4azRipdL8Y?= =?us-ascii?Q?lD7MtOgRoAV7s9WbRUOWZbo4muV7vI5cA7xXOhJcOIhVhno0mSd0KNA0ysn5?= =?us-ascii?Q?tpWsYyzD5qJ+fcKt8vaE2lBk8fXVwHmoQKoAR+v8jIq64t+0ywk2A6iVmLxQ?= =?us-ascii?Q?1DoeN9DWN1f/RhF49/RXJ6xloOk6zjwYsSK2Jpeceu8oJgxZZE8exx5ANlBk?= =?us-ascii?Q?qYUDDYNYpWYhl+AQWssSKXpvOF8TcpIvOYT/GaGSc+UFuBpNZWyDYJ3pdGW9?= =?us-ascii?Q?sRrnVeb7MBRZD1iG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ascii?Q?jUv7ojrMdw0+gW8fy2zev3US0F9RjncZiwD5w9xb?= X-Forefront-PRVS: 0337AFFE9A X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CY1PR0301MB0908;23:9SjeWVRhX7c8DOI6cRUP8ipakrCI83k9Yeiz4xW?= =?us-ascii?Q?Pnb6mOC9vqq3ROUc/0iWsqhTModBhDJRtpsOZWWGrLc/oYUvHd3n+FP750Cn?= =?us-ascii?Q?nkcf08ZKhan2MhD2S1srOlD+wUy5YqA2hkVpPBSzQnfvtnYpnZiBiyEa94nU?= =?us-ascii?Q?vPXf9o7U+MFtKaMJKMpS92MHGMypdEFmF6ambg3tMgRWH44jCWv/dF6vuPUJ?= =?us-ascii?Q?QflynUt7WN8tcVCgZ1ZPFOQkG9CDteqs61x56dHxcHPseVcoZbtWPIy/ts2M?= =?us-ascii?Q?0woO/SPnOjqfXNT5G/sdMLO3Cj512Y28xTj0ehgJqyh8pamhsIoM9x3IXvSI?= =?us-ascii?Q?XDGpz95JeR7K6ahZicHfKgb5xIgCls/IuVouf/57G9fkKd8fYP6Yl2jNV1Hz?= =?us-ascii?Q?5cF6kuRUGglQ2znCd9gfrGuid7hw8u1H+cvoJ7S/MEUl3HG09lvWwdjA3CzF?= =?us-ascii?Q?OIZIxER51Tw7ss/ULegzQleP2TfI/myQHugEz6Bte/FBZbvvK87NFf4GIriQ?= =?us-ascii?Q?e3bn3HByGEPiQGaKnbIa9tK/pfjVP24VAVUT9qVwRekpxsAK6DUwmGMyZDws?= =?us-ascii?Q?cSDe85J8WKRdMVmEFz6AJSk58XXfL1J04QrVwQ4/AQGa9BYYWq/0kKMLyX1Y?= =?us-ascii?Q?3B+JipzTLnYnenZG2qSXVIfJbtQdCWWKtjlhsuWnjCHQ/qFnmPRSSl8gc554?= =?us-ascii?Q?ilZ+RteNybJ61C6zBkN7YXbWkbFmiu1/Pj1RhRGKODmrSZ5MhF/D9wj4qGd3?= =?us-ascii?Q?/lvzeOyScq9ZbNEZsZ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ShvSrm4aqYo/wRHw9Nrb3VGl08N5jIdCSHkxL4oLdfQOer1byHSwGNX232dsTX+el4tkwO3cSF4o1mmsSklJeK1p4USCQL6G+kPi5tNvFA8BW1ALWdoFTOeq1oWcYdhIpJdBf74+DEDdliJXiiVBzZp/Omy/mguEmCBycpf3Jl/cXA/I5HqsKHA3U96lUmr26BBaV7f3rrLbtLOxSVm1yhhhw0oBoAA9jOHzFcYnJCmMpGuGCQ7nJ7guHwNsObDhxirnH4y9oKd3rOHpdGQRNATNZCIpYyz39Z+ozrg== X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jun 2017 07:58:38.9003 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e;Ip=[192.88.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR0301MB0908 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 8024 Lines: 294 IMX Timer/PWM Module (TPM) supports both timer and pwm function while this patch only adds the timer support. PWM would be added later. The TPM counter, compare and capture registers are clocked by an asynchronous clock that can remain enabled in low power modes. Cc: Daniel Lezcano Cc: Arnd Bergmann Cc: Thomas Gleixner Cc: Shawn Guo Cc: Anson Huang Cc: Bai Ping Signed-off-by: Dong Aisheng --- ChangeLog: v1->v2: * change to readl/writel from __raw_readl/writel according to Arnd's suggestion to avoid endian issue * add help information in Kconfig * add more error checking --- drivers/clocksource/Kconfig | 8 ++ drivers/clocksource/Makefile | 1 + drivers/clocksource/timer-imx-tpm.c | 227 ++++++++++++++++++++++++++++++++++++ 3 files changed, 236 insertions(+) create mode 100644 drivers/clocksource/timer-imx-tpm.c diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig index 545d541..33b4d31 100644 --- a/drivers/clocksource/Kconfig +++ b/drivers/clocksource/Kconfig @@ -603,6 +603,14 @@ config CLKSRC_IMX_GPT depends on ARM && CLKDEV_LOOKUP select CLKSRC_MMIO +config CLKSRC_IMX_TPM + bool "Clocksource using i.MX TPM" if COMPILE_TEST + depends on ARM && CLKDEV_LOOKUP && GENERIC_CLOCKEVENTS + select CLKSRC_MMIO + help + Enable this option to use IMX Timer/PWM Module (TPM) timer as + clocksource. + config CLKSRC_ST_LPC bool "Low power clocksource found in the LPC" if COMPILE_TEST select CLKSRC_OF if OF diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile index 2b5b56a..9fdf8da0 100644 --- a/drivers/clocksource/Makefile +++ b/drivers/clocksource/Makefile @@ -67,6 +67,7 @@ obj-$(CONFIG_CLKSRC_VERSATILE) += versatile.o obj-$(CONFIG_CLKSRC_MIPS_GIC) += mips-gic-timer.o obj-$(CONFIG_CLKSRC_TANGO_XTAL) += tango_xtal.o obj-$(CONFIG_CLKSRC_IMX_GPT) += timer-imx-gpt.o +obj-$(CONFIG_CLKSRC_IMX_TPM) += timer-imx-tpm.o obj-$(CONFIG_ASM9260_TIMER) += asm9260_timer.o obj-$(CONFIG_H8300_TMR8) += h8300_timer8.o obj-$(CONFIG_H8300_TMR16) += h8300_timer16.o diff --git a/drivers/clocksource/timer-imx-tpm.c b/drivers/clocksource/timer-imx-tpm.c new file mode 100644 index 0000000..940a4f75 --- /dev/null +++ b/drivers/clocksource/timer-imx-tpm.c @@ -0,0 +1,227 @@ +/* + * Copyright 2016 Freescale Semiconductor, Inc. + * Copyright 2017 NXP + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define TPM_SC 0x10 +#define TPM_SC_CMOD_INC_PER_CNT (0x1 << 3) +#define TPM_SC_CMOD_DIV_DEFAULT 0x3 +#define TPM_CNT 0x14 +#define TPM_MOD 0x18 +#define TPM_STATUS 0x1c +#define TPM_STATUS_CH0F BIT(0) +#define TPM_C0SC 0x20 +#define TPM_C0SC_CHIE BIT(6) +#define TPM_C0SC_MODE_SHIFT 2 +#define TPM_C0SC_MODE_MASK 0x3c +#define TPM_C0SC_MODE_SW_COMPARE 0x4 +#define TPM_C0V 0x24 + +static void __iomem *timer_base; +static struct clock_event_device clockevent_tpm; + +static inline void tpm_timer_disable(void) +{ + unsigned int val; + + /* channel disable */ + val = readl(timer_base + TPM_C0SC); + val &= ~(TPM_C0SC_MODE_MASK | TPM_C0SC_CHIE); + writel(val, timer_base + TPM_C0SC); +} + +static inline void tpm_timer_enable(void) +{ + unsigned int val; + + /* channel enabled in sw compare mode */ + val = readl(timer_base + TPM_C0SC); + val |= (TPM_C0SC_MODE_SW_COMPARE << TPM_C0SC_MODE_SHIFT) | + TPM_C0SC_CHIE; + writel(val, timer_base + TPM_C0SC); +} + +static inline void tpm_irq_acknowledge(void) +{ + writel(TPM_STATUS_CH0F, timer_base + TPM_STATUS); +} + +static struct delay_timer tpm_delay_timer; + +static unsigned long tpm_read_current_timer(void) +{ + return readl(timer_base + TPM_CNT); +} + +static u64 notrace tpm_read_sched_clock(void) +{ + return readl(timer_base + TPM_CNT); +} + +static int __init tpm_clocksource_init(unsigned long rate) +{ + tpm_delay_timer.read_current_timer = &tpm_read_current_timer; + tpm_delay_timer.freq = rate; + register_current_timer_delay(&tpm_delay_timer); + + sched_clock_register(tpm_read_sched_clock, 32, rate); + + return clocksource_mmio_init(timer_base + TPM_CNT, "imx-tpm", + rate, 200, 32, clocksource_mmio_readl_up); +} + +static int tpm_set_next_event(unsigned long delta, + struct clock_event_device *evt) +{ + unsigned long next, now; + + next = readl(timer_base + TPM_CNT) + delta; + writel(next, timer_base + TPM_C0V); + now = readl(timer_base + TPM_CNT); + + return (int)((next - now) <= 0) ? -ETIME : 0; +} + +static int tpm_set_state_oneshot(struct clock_event_device *evt) +{ + tpm_timer_enable(); + + return 0; +} + +static int tpm_set_state_shutdown(struct clock_event_device *evt) +{ + tpm_timer_disable(); + + return 0; +} + +static irqreturn_t tpm_timer_interrupt(int irq, void *dev_id) +{ + struct clock_event_device *evt = &clockevent_tpm; + + tpm_irq_acknowledge(); + + evt->event_handler(evt); + + return IRQ_HANDLED; +} + +static struct clock_event_device clockevent_tpm = { + .name = "i.MX7ULP TPM Timer", + .features = CLOCK_EVT_FEAT_ONESHOT, + .set_state_oneshot = tpm_set_state_oneshot, + .set_next_event = tpm_set_next_event, + .set_state_shutdown = tpm_set_state_shutdown, + .rating = 200, +}; + +static struct irqaction tpm_timer_irq = { + .name = "i.MX7ULP TPM Timer", + .flags = IRQF_TIMER | IRQF_IRQPOLL, + .handler = tpm_timer_interrupt, + .dev_id = &clockevent_tpm, +}; + +static int __init tpm_clockevent_init(unsigned long rate, int irq) +{ + setup_irq(irq, &tpm_timer_irq); + + clockevent_tpm.cpumask = cpumask_of(0); + clockevent_tpm.irq = irq; + clockevents_config_and_register(&clockevent_tpm, + rate, 0xff, 0xfffffffe); + + return 0; +} + +static int __init tpm_timer_init(struct device_node *np) +{ + struct clk *ipg, *per; + int irq, ret; + u32 rate; + + timer_base = of_iomap(np, 0); + if (!timer_base) { + pr_err("tpm: failed to get base address\n"); + return -ENXIO; + } + + irq = irq_of_parse_and_map(np, 0); + if (!irq) { + pr_err("tpm: failed to get irq\n"); + ret = -ENOENT; + goto err_iomap; + } + + ipg = of_clk_get_by_name(np, "ipg"); + per = of_clk_get_by_name(np, "per"); + if (IS_ERR(ipg) || IS_ERR(per)) { + pr_err("tpm: failed to get igp or per clk\n"); + ret = -ENODEV; + goto err_clk_get; + } + + /* enable clk before accessing registers */ + ret = clk_prepare_enable(ipg); + if (ret) { + pr_err("tpm: ipg clock enable failed (%d)\n", ret); + goto err_ipg_clk_enable; + } + + ret = clk_prepare_enable(per); + if (ret) { + pr_err("tpm: per clock enable failed (%d)\n", ret); + goto err_per_clk_enable; + } + + /* + * Initialize tpm module to a known state + * 1) Counter disabled + * 2) TPM counter operates in up counting mode + * 3) Timer Overflow Interrupt disabled + * 4) Channel0 disabled + * 5) DMA transfers disabled + */ + writel(0, timer_base + TPM_SC); + writel(0, timer_base + TPM_CNT); + writel(0, timer_base + TPM_C0SC); + + /* increase per cnt, div 8 by default */ + writel(TPM_SC_CMOD_INC_PER_CNT | TPM_SC_CMOD_DIV_DEFAULT, + timer_base + TPM_SC); + + /* set MOD register to maximum for free running mode */ + writel(0xffffffff, timer_base + TPM_MOD); + + rate = clk_get_rate(per) / 8; + tpm_clocksource_init(rate); + tpm_clockevent_init(rate, irq); + + return 0; + +err_per_clk_enable: + clk_disable_unprepare(ipg); +err_ipg_clk_enable: +err_clk_get: + clk_put(per); + clk_put(ipg); +err_iomap: + iounmap(timer_base); + return ret; +} +CLOCKSOURCE_OF_DECLARE(imx7ulp, "fsl,imx7ulp-tpm", tpm_timer_init); -- 2.7.4