Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6375211ybi; Tue, 4 Jun 2019 23:42:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqx+2k/em9FgtTYjCJWjP7JbHIxAGgk6YT81XxwxdTfRqC/0u+OtTq1wXvP9lkWX/CWNwBa3 X-Received: by 2002:aa7:93bb:: with SMTP id x27mr43601279pff.104.1559716955065; Tue, 04 Jun 2019 23:42:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559716955; cv=none; d=google.com; s=arc-20160816; b=rbXcht/cPptDVjtNRJ5BcguZE7VKl62kyfmwyoLSC0n+lbOvlvnV10cvbawhjywbSS e/rWY8b1D8ymM94LczzdzDD/6P7oSlX++BBaCI7tOaODiw1RGBK0lBnuyvby6xvm0rhh yQksgOLTy+wHqfWJovyR7b6kKyu3Y9bewejkU1O9JWd/VifQOcaieBn+Dm109xfmiKhm qIGsIneA1hhZXmvZcjseF96vguD0Ug0PvjSJgZ4O4TAq8/G6SgXAF9XIRlLx7Cszj8EE +ApTIOFoIxOA7f9hGjPFwNmg9AJYbs2j24LZs8FNt3mQAZptcTmEloj0SsV+u7OPEOo4 X26g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:references :message-id:date:thread-index:thread-topic:subject:cc:to:from :dkim-signature; bh=4/EywsIVKRKoHyTgwA213UbVtBqPJzgp0+OhbhTYvPc=; b=PSroGkz2Q7uotqesRqOo52kQbt/W/vnDXKzHxgr0i9A/WMkdssHjdd6pp4zJjzuGxw CzyZsx5wjEDIg1EWSfO7djW7T5AHUKR7fpxCRzCL9YNqeAIIru/U/K5m4XWcv0icrWil gO27KMmyvugNfE2/ZiiI51uPvz/aiGHoRXtLJ66LYM3OO58yweounUHNAHc9sslTFgTf sf0A5RmOUjXqDBMj54VD9xrQw2JGWkIqckddVbPK9J21Xae/x+4Yc0/9NItO+9EUTUhw NlLguf7QStzI3YBgw/Rw39fIGYETfXKvzpaxcgFEm8HOPckZeI89Eyp/bwn88UAcFzmk uC2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nxp.com header.s=selector2 header.b=DHDS9lY8; 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=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k24si25728646pfk.195.2019.06.04.23.42.19; Tue, 04 Jun 2019 23:42:35 -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=@nxp.com header.s=selector2 header.b=DHDS9lY8; 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=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726595AbfFEGlF (ORCPT + 99 others); Wed, 5 Jun 2019 02:41:05 -0400 Received: from mail-eopbgr20067.outbound.protection.outlook.com ([40.107.2.67]:35584 "EHLO EUR02-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726086AbfFEGlE (ORCPT ); Wed, 5 Jun 2019 02:41:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4/EywsIVKRKoHyTgwA213UbVtBqPJzgp0+OhbhTYvPc=; b=DHDS9lY85zlrz3PcgudsqMR5YPVEMuzMMupDBNyYiDVWpi582QII5i3jzlbgRKaWue9iS/kCQhvS+toFD1npi6cuEVVeG5OAoHvp4eUqOgkto3ybigfCWi5uH6/3DuanL7Wy0PJ+y7ME/dwtaKdnOpW0D3F/WMNy++vpB2hPx0U= Received: from DB7PR04MB5178.eurprd04.prod.outlook.com (20.176.236.22) by DB7PR04MB4924.eurprd04.prod.outlook.com (20.176.234.31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1965.12; Wed, 5 Jun 2019 06:40:59 +0000 Received: from DB7PR04MB5178.eurprd04.prod.outlook.com ([fe80::24da:94bd:6034:4d45]) by DB7PR04MB5178.eurprd04.prod.outlook.com ([fe80::24da:94bd:6034:4d45%4]) with mapi id 15.20.1965.011; Wed, 5 Jun 2019 06:40:59 +0000 From: Jacky Bai To: "daniel.lezcano@linaro.org" , "tglx@linutronix.de" , "robh+dt@kernel.org" , "shawnguo@kernel.org" , "mark.rutland@arm.com" , Aisheng Dong CC: "linux-kernel@vger.kernel.org" , "devicetree@vger.kernel.org" , dl-linux-imx Subject: [PATCH v5 2/2] driver: clocksource: Add nxp system counter timer driver support Thread-Topic: [PATCH v5 2/2] driver: clocksource: Add nxp system counter timer driver support Thread-Index: AQHVG2miQGCCqxWZbUG/anskRBuVTw== Date: Wed, 5 Jun 2019 06:40:59 +0000 Message-ID: <20190605064546.29249-2-ping.bai@nxp.com> References: <20190605064546.29249-1-ping.bai@nxp.com> In-Reply-To: <20190605064546.29249-1-ping.bai@nxp.com> Accept-Language: zh-CN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.21.0 x-clientproxiedby: HK0P153CA0029.APCP153.PROD.OUTLOOK.COM (2603:1096:203:17::17) To DB7PR04MB5178.eurprd04.prod.outlook.com (2603:10a6:10:20::22) authentication-results: spf=none (sender IP is ) smtp.mailfrom=ping.bai@nxp.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [119.31.174.71] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 49a3630b-0344-4066-76a1-08d6e980c4dc x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020);SRVR:DB7PR04MB4924; x-ms-traffictypediagnostic: DB7PR04MB4924: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:6108; x-forefront-prvs: 00594E8DBA x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(376002)(39860400002)(136003)(366004)(396003)(346002)(189003)(199004)(4326008)(8936002)(2501003)(71200400001)(71190400001)(11346002)(6436002)(1076003)(68736007)(446003)(486006)(86362001)(6116002)(2616005)(6486002)(8676002)(81156014)(476003)(50226002)(36756003)(81166006)(66066001)(53936002)(305945005)(5660300002)(6506007)(110136005)(386003)(26005)(76176011)(186003)(66556008)(64756008)(99286004)(66946007)(66476007)(66446008)(73956011)(25786009)(478600001)(7736002)(256004)(6512007)(52116002)(6636002)(3846002)(14454004)(316002)(54906003)(102836004)(2906002);DIR:OUT;SFP:1101;SCL:1;SRVR:DB7PR04MB4924;H:DB7PR04MB5178.eurprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: nxp.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: l4wSSz9p4GyzOVecMQavuACgZ8UIBP94qj3l7Gz4c4z9Ot9cPIFQGLuQdXxy9unWW3qF6r4wKhFNGP+sNyPpPH9YhR+Z+31EstHiqY//jrzZ7uGdRQY9Y1RwiSpSGwpgpcy9T1N8YF8IWmMdbzVQ4imLAl6PPhFYsX2ipMUjzDIBUlHwTyRWUkxHLIw4mvFhYVmVteb6gHaY7JgTgTYXDMbaNX7wyiPpCYai5YxLIl97Txv7W+GK3m9hmvyWOycE5aK9JVmMtcR55v3/qizS0c7Wl8wWii9ejttiayS+5uAomdIN3E6UT5lC+XvQmfUqidKs/stBvTkmrhHerqvsnzi12wzYs97VborPiqP9OJmfVOo6czOZu8ZT2bhf7vI3W/P5fHhYY+rH/8MutseJqzZ00xg/gtyVNyYXcoy91fA= Content-Type: text/plain; charset="iso-8859-1" Content-ID: <3516069B4BCD3842943FFBA1B39F89DE@eurprd04.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 49a3630b-0344-4066-76a1-08d6e980c4dc X-MS-Exchange-CrossTenant-originalarrivaltime: 05 Jun 2019 06:40:59.2429 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: ping.bai@nxp.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR04MB4924 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bai Ping The system counter (sys_ctr) is a programmable system counter which provides a shared time base to the Cortex A15, A7, A53 etc cores. It is intended for use in applications where the counter is always powered on and supports multiple, unrelated clocks. The sys_ctr hardware supports: - 56-bit counter width (roll-over time greater than 40 years) - compare frame(64-bit compare value) contains programmable interrupt generation when compare value <=3D counter value. Signed-off-by: Bai Ping --- change v1->v2: - no change=20 change v2->v3: - remove the clocksource, we only need to use this module for timer purpos= e, so register it as clockevent is enough. - use the timer_of_init to init the irq, clock, etc. - remove some unnecessary comments. change v3->v4: - use cached value for CMPCR, - remove unnecessary timer enabe from set_state_oneshot function. change v4->v5: - remove the unnecessary config dependency - simplify the timer enable --- drivers/clocksource/Kconfig | 6 ++ drivers/clocksource/Makefile | 1 + drivers/clocksource/timer-imx-sysctr.c | 143 +++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 drivers/clocksource/timer-imx-sysctr.c diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig index 3300739edce4..8bcab0a008a8 100644 --- a/drivers/clocksource/Kconfig +++ b/drivers/clocksource/Kconfig @@ -617,6 +617,12 @@ config CLKSRC_IMX_TPM Enable this option to use IMX Timer/PWM Module (TPM) timer as clocksource. =20 +config TIMER_IMX_SYS_CTR + bool "i.MX system counter timer" if COMPILE_TEST + select TIMER_OF + help + Enable this option to use i.MX system counter timer for clockevent. + config CLKSRC_ST_LPC bool "Low power clocksource found in the LPC" if COMPILE_TEST select TIMER_OF if OF diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile index 236858fa7fbf..5fba39e81a40 100644 --- a/drivers/clocksource/Makefile +++ b/drivers/clocksource/Makefile @@ -74,6 +74,7 @@ obj-$(CONFIG_CLKSRC_MIPS_GIC) +=3D mips-gic-timer.o obj-$(CONFIG_CLKSRC_TANGO_XTAL) +=3D timer-tango-xtal.o obj-$(CONFIG_CLKSRC_IMX_GPT) +=3D timer-imx-gpt.o obj-$(CONFIG_CLKSRC_IMX_TPM) +=3D timer-imx-tpm.o +obj-$(CONFIG_TIMER_IMX_SYS_CTR) +=3D timer-imx-sysctr.o obj-$(CONFIG_ASM9260_TIMER) +=3D asm9260_timer.o obj-$(CONFIG_H8300_TMR8) +=3D h8300_timer8.o obj-$(CONFIG_H8300_TMR16) +=3D h8300_timer16.o diff --git a/drivers/clocksource/timer-imx-sysctr.c b/drivers/clocksource/t= imer-imx-sysctr.c new file mode 100644 index 000000000000..d3a049c1088c --- /dev/null +++ b/drivers/clocksource/timer-imx-sysctr.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0+ +// +// Copyright 2017-2019 NXP + +#include +#include +#include +#include + +#include "timer-of.h" + +#define CMP_OFFSET 0x10000 + +#define CNTCV_LO 0x8 +#define CNTCV_HI 0xc +#define CMPCV_LO (CMP_OFFSET + 0x20) +#define CMPCV_HI (CMP_OFFSET + 0x24) +#define CMPCR (CMP_OFFSET + 0x2c) + +#define SYS_CTR_EN 0x1 +#define SYS_CTR_IRQ_MASK 0x2 + +static void __iomem *sys_ctr_base; +static u32 cmpcr; + +static void sysctr_timer_enable(bool enable) +{ + writel(enable ? cmpcr | SYS_CTR_EN : cmpcr, sys_ctr_base + CMPCR); +} + +static void sysctr_irq_acknowledge(void) +{ + /* + * clear the enable bit(EN =3D0) will clear + * the status bit(ISTAT =3D 0), then the interrupt + * signal will be negated(acknowledged). + */ + sysctr_timer_enable(false); +} + +static inline u64 sysctr_read_counter(void) +{ + u32 cnt_hi, tmp_hi, cnt_lo; + + do { + cnt_hi =3D readl_relaxed(sys_ctr_base + CNTCV_HI); + cnt_lo =3D readl_relaxed(sys_ctr_base + CNTCV_LO); + tmp_hi =3D readl_relaxed(sys_ctr_base + CNTCV_HI); + } while (tmp_hi !=3D cnt_hi); + + return ((u64) cnt_hi << 32) | cnt_lo; +} + +static int sysctr_set_next_event(unsigned long delta, + struct clock_event_device *evt) +{ + u32 cmp_hi, cmp_lo; + u64 next; + + sysctr_timer_enable(false); + + next =3D sysctr_read_counter(); + + next +=3D delta; + + cmp_hi =3D (next >> 32) & 0x00fffff; + cmp_lo =3D next & 0xffffffff; + + writel_relaxed(cmp_hi, sys_ctr_base + CMPCV_HI); + writel_relaxed(cmp_lo, sys_ctr_base + CMPCV_LO); + + sysctr_timer_enable(true); + + return 0; +} + +static int sysctr_set_state_oneshot(struct clock_event_device *evt) +{ + return 0; +} + +static int sysctr_set_state_shutdown(struct clock_event_device *evt) +{ + sysctr_timer_enable(false); + + return 0; +} + +static irqreturn_t sysctr_timer_interrupt(int irq, void *dev_id) +{ + struct clock_event_device *evt =3D dev_id; + + sysctr_irq_acknowledge(); + + evt->event_handler(evt); + + return IRQ_HANDLED; +} + +static struct timer_of to_sysctr =3D { + .flags =3D TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE, + .clkevt =3D { + .name =3D "i.MX system counter timer", + .features =3D CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ, + .set_state_oneshot =3D sysctr_set_state_oneshot, + .set_next_event =3D sysctr_set_next_event, + .set_state_shutdown =3D sysctr_set_state_shutdown, + .rating =3D 200, + }, + .of_irq =3D { + .handler =3D sysctr_timer_interrupt, + .flags =3D IRQF_TIMER | IRQF_IRQPOLL, + }, + .of_clk =3D { + .name =3D "per", + }, +}; + +static void __init sysctr_clockevent_init(void) +{ + to_sysctr.clkevt.cpumask =3D cpumask_of(0); + + clockevents_config_and_register(&to_sysctr.clkevt, timer_of_rate(&to_sysc= tr), + 0xff, 0x7fffffff); +} + +static int __init sysctr_timer_init(struct device_node *np) +{ + int ret =3D 0; + + ret =3D timer_of_init(np, &to_sysctr); + if (ret) + return ret; + + sys_ctr_base =3D timer_of_base(&to_sysctr); + cmpcr =3D readl(sys_ctr_base + CMPCR); + cmpcr &=3D ~SYS_CTR_EN; + + sysctr_clockevent_init(); + + return 0; +} +TIMER_OF_DECLARE(sysctr_timer, "nxp,sysctr-timer", sysctr_timer_init); --=20 2.21.0