Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5323474ybl; Tue, 14 Jan 2020 07:08:20 -0800 (PST) X-Google-Smtp-Source: APXvYqwKLEyx4G9vE9lMzyW5eREPN3VMeLQoMM48jvsLDCyN7wGw3KCUdLtv0uX+25vfRbqmzHi6 X-Received: by 2002:a9d:6301:: with SMTP id q1mr16428972otk.70.1579014500488; Tue, 14 Jan 2020 07:08:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579014500; cv=none; d=google.com; s=arc-20160816; b=LsHm4G8tjsgi05sgRuBrc5gFfOn2fkpz3EM1nWPaE5vyIbwGIrt5LjlFIjfPDR6TWe Fva6lFKIzgCZU/SdUrnD0XqbZDxpo7W55xK9Ba/u/vsYB95pLEBbhH2z6d7ZuI8Ij9Em xreJ64z0XYDN7m2Ll0XJP7M6jeum2HyjyueZswf50dcEwjOEaVpG1zmCawPDvFqwmU2O XCUyhHDUBlrSwZhhsebTl7mQVYlAzVMWLgABZUsQYWzOim/mgy39myljdZ67YgNH4RoM zVxNqZWGHc76EfEHKSrswJ8r0ByiZr+L3wjvDEcCC+HqfV1dE17FD24o32sQP2lJx/sP icWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=BvjxXIepNx0mvNesr/9RNNIfuKuiwR0fPltZa2H6Xhg=; b=jeP7jNrdkIuV6vFZt9nQ8PVb9jtRoO8o4ZY1Zhvef52N4s62ElBXaiigUpmI9+qhqs tqSKCQsvY29SWaLT1ZANIhoUJarngX8BfTz6I0gS/tEnu1O60Gn1p0f2Dn99tkmXX+V0 zsXXci4hrrjoEeEaityrdqO7gJbrGW/c3uUkywlV8UfRSo2Ipnmm91/6Bpi4lg+NCIyu rm6gWd81j63c2gCvO0kTcTiSXa2K4ECpYamGKu+5Lp9cK2I3Wit00xhll922Q81aNZ+N 9mMvGt2iyoCiP+Kl73QaZ6CGMuPHhXV9mO/R85O7AeYnhx8KqN6JJBpftRzvcyAEocxV CzlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@crapouillou.net header.s=mail header.b=fhkoDSuR; 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=fail (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w24si7103142oic.260.2020.01.14.07.08.00; Tue, 14 Jan 2020 07:08:20 -0800 (PST) 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=fail header.i=@crapouillou.net header.s=mail header.b=fhkoDSuR; 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=fail (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729187AbgANPGd (ORCPT + 99 others); Tue, 14 Jan 2020 10:06:33 -0500 Received: from outils.crapouillou.net ([89.234.176.41]:46228 "EHLO crapouillou.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725904AbgANPGd (ORCPT ); Tue, 14 Jan 2020 10:06:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1579014390; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=BvjxXIepNx0mvNesr/9RNNIfuKuiwR0fPltZa2H6Xhg=; b=fhkoDSuRwh+LXfXPVfRhqr12UIMtlJkqYCYIMQahdwi69mjKPY6C+xJoNsJzyweN9Rxr4m /TQ5lvFpLNg0uQUYHvs2vOy/re2HNQuEybnlwGRMDdyCaYUb+JEld5NEYFllL6KdwElHNq EpeaeDJemdlfN99Pz7712Yx/Ko+4/9E= From: Paul Cercueil To: Daniel Lezcano , Thomas Gleixner Cc: od@zcrc.me, linux-kernel@vger.kernel.org, Maarten ter Huurne , Paul Cercueil , Mathieu Malaterre , Artur Rojek Subject: [PATCH v3] clocksource: Add driver for the Ingenic JZ47xx OST Date: Tue, 14 Jan 2020 12:06:19 -0300 Message-Id: <20200114150619.14611-1-paul@crapouillou.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Maarten ter Huurne OST is the OS Timer, a 64-bit timer/counter with buffered reading. SoCs before the JZ4770 had (if any) a 32-bit OST; the JZ4770 and JZ4780 have a 64-bit OST. This driver will register both a clocksource and a sched_clock to the system. Signed-off-by: Maarten ter Huurne Signed-off-by: Paul Cercueil Tested-by: Mathieu Malaterre Tested-by: Artur Rojek --- Notes: v2: local_irq_save/restore were moved within sched_clock_register v3: Only register as 32-bit clocksource to simplify things drivers/clocksource/Kconfig | 8 ++ drivers/clocksource/Makefile | 1 + drivers/clocksource/ingenic-ost.c | 183 ++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 drivers/clocksource/ingenic-ost.c diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig index 5fdd76cb1768..5be2f876f64f 100644 --- a/drivers/clocksource/Kconfig +++ b/drivers/clocksource/Kconfig @@ -697,4 +697,12 @@ config INGENIC_TIMER help Support for the timer/counter unit of the Ingenic JZ SoCs. +config INGENIC_OST + bool "Ingenic JZ47xx Operating System Timer" + depends on MIPS || COMPILE_TEST + depends on COMMON_CLK + select MFD_SYSCON + help + Support for the OS Timer of the Ingenic JZ4770 or similar SoC. + endmenu diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile index 4dfe4225ece7..6bc97a6fd229 100644 --- a/drivers/clocksource/Makefile +++ b/drivers/clocksource/Makefile @@ -80,6 +80,7 @@ obj-$(CONFIG_ASM9260_TIMER) += asm9260_timer.o obj-$(CONFIG_H8300_TMR8) += h8300_timer8.o obj-$(CONFIG_H8300_TMR16) += h8300_timer16.o obj-$(CONFIG_H8300_TPU) += h8300_tpu.o +obj-$(CONFIG_INGENIC_OST) += ingenic-ost.o obj-$(CONFIG_INGENIC_TIMER) += ingenic-timer.o obj-$(CONFIG_CLKSRC_ST_LPC) += clksrc_st_lpc.o obj-$(CONFIG_X86_NUMACHIP) += numachip.o diff --git a/drivers/clocksource/ingenic-ost.c b/drivers/clocksource/ingenic-ost.c new file mode 100644 index 000000000000..2c91e3b34572 --- /dev/null +++ b/drivers/clocksource/ingenic-ost.c @@ -0,0 +1,183 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * JZ47xx SoCs TCU Operating System Timer driver + * + * Copyright (C) 2016 Maarten ter Huurne + * Copyright (C) 2020 Paul Cercueil + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define TCU_OST_TCSR_MASK 0xffc0 +#define TCU_OST_TCSR_CNT_MD BIT(15) + +#define TCU_OST_CHANNEL 15 + +struct ingenic_ost_soc_info { + bool is64bit; +}; + +struct ingenic_ost { + struct regmap *map; + struct clk *clk; + + struct clocksource cs; +}; + +static struct ingenic_ost *ingenic_ost; + +static u64 notrace ingenic_ost_read_cntl(void) +{ + u32 val; + + regmap_read(ingenic_ost->map, TCU_REG_OST_CNTL, &val); + + return val; +} + +static u64 notrace ingenic_ost_read_cnth(void) +{ + u32 val; + + regmap_read(ingenic_ost->map, TCU_REG_OST_CNTH, &val); + + return val; +} + +static u64 notrace ingenic_ost_clocksource_readl(struct clocksource *cs) +{ + return ingenic_ost_read_cntl(); +} + +static u64 notrace ingenic_ost_clocksource_readh(struct clocksource *cs) +{ + return ingenic_ost_read_cnth(); +} + +static int __init ingenic_ost_probe(struct platform_device *pdev) +{ + const struct ingenic_ost_soc_info *soc_info; + struct device *dev = &pdev->dev; + struct ingenic_ost *ost; + struct clocksource *cs; + unsigned long rate; + int err; + + soc_info = device_get_match_data(dev); + if (!soc_info) + return -EINVAL; + + ost = devm_kzalloc(dev, sizeof(*ost), GFP_KERNEL); + if (!ost) + return -ENOMEM; + + ingenic_ost = ost; + + ost->map = device_node_to_regmap(dev->parent->of_node); + if (!ost->map) { + dev_err(dev, "regmap not found\n"); + return -EINVAL; + } + + ost->clk = devm_clk_get(dev, "ost"); + if (IS_ERR(ost->clk)) + return PTR_ERR(ost->clk); + + err = clk_prepare_enable(ost->clk); + if (err) + return err; + + /* Clear counter high/low registers */ + if (soc_info->is64bit) + regmap_write(ost->map, TCU_REG_OST_CNTL, 0); + regmap_write(ost->map, TCU_REG_OST_CNTH, 0); + + /* Don't reset counter at compare value. */ + regmap_update_bits(ost->map, TCU_REG_OST_TCSR, + TCU_OST_TCSR_MASK, TCU_OST_TCSR_CNT_MD); + + rate = clk_get_rate(ost->clk); + + /* Enable OST TCU channel */ + regmap_write(ost->map, TCU_REG_TESR, BIT(TCU_OST_CHANNEL)); + + cs = &ost->cs; + cs->name = "ingenic-ost"; + cs->rating = 320; + cs->flags = CLOCK_SOURCE_IS_CONTINUOUS; + cs->mask = CLOCKSOURCE_MASK(32); + + if (soc_info->is64bit) + cs->read = ingenic_ost_clocksource_readl; + else + cs->read = ingenic_ost_clocksource_readh; + + err = clocksource_register_hz(cs, rate); + if (err) { + dev_err(dev, "clocksource registration failed: %d\n", err); + clk_disable_unprepare(ost->clk); + return err; + } + + if (soc_info->is64bit) + sched_clock_register(ingenic_ost_read_cntl, 32, rate); + else + sched_clock_register(ingenic_ost_read_cnth, 32, rate); + + return 0; +} + +static int __maybe_unused ingenic_ost_suspend(struct device *dev) +{ + struct ingenic_ost *ost = dev_get_drvdata(dev); + + clk_disable(ost->clk); + + return 0; +} + +static int __maybe_unused ingenic_ost_resume(struct device *dev) +{ + struct ingenic_ost *ost = dev_get_drvdata(dev); + + return clk_enable(ost->clk); +} + +static const struct dev_pm_ops __maybe_unused ingenic_ost_pm_ops = { + /* _noirq: We want the OST clock to be gated last / ungated first */ + .suspend_noirq = ingenic_ost_suspend, + .resume_noirq = ingenic_ost_resume, +}; + +static const struct ingenic_ost_soc_info jz4725b_ost_soc_info = { + .is64bit = false, +}; + +static const struct ingenic_ost_soc_info jz4770_ost_soc_info = { + .is64bit = true, +}; + +static const struct of_device_id ingenic_ost_of_match[] = { + { .compatible = "ingenic,jz4725b-ost", .data = &jz4725b_ost_soc_info, }, + { .compatible = "ingenic,jz4770-ost", .data = &jz4770_ost_soc_info, }, + { } +}; + +static struct platform_driver ingenic_ost_driver = { + .driver = { + .name = "ingenic-ost", +#ifdef CONFIG_PM_SUSPEND + .pm = &ingenic_ost_pm_ops, +#endif + .of_match_table = ingenic_ost_of_match, + }, +}; +builtin_platform_driver_probe(ingenic_ost_driver, ingenic_ost_probe); -- 2.24.1