Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2574452imm; Thu, 16 Aug 2018 11:45:09 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzUtM6Ot1Z2ZtJn/C1THw21Na5y9La7llA82t79VVyxM+vskbNwebbvzEi2OX79TlUi0FLp X-Received: by 2002:a65:668f:: with SMTP id b15-v6mr3339115pgw.426.1534445109929; Thu, 16 Aug 2018 11:45:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534445109; cv=none; d=google.com; s=arc-20160816; b=eEONsEV5ozpDu4HeqmPQ0Pyn4Y660UpgONQEFB8nyQIDMfcpX0SnFBNTAwikvG1tau kkjx4b9NhM50yDwEF2gZWLIhYNl4+wxnVB8iyZFn3SctU4/SMmccAWTp5cOAQuNKDeed 5r3++Ql9cdPD/f+icEyUBOAo95MLiNf1AywXP8mNn3nFbyhF3wzR9BvuWrQmFgoMQtXQ ycAo4HkDaYpi9AeChc3hzran0Fr5rqfHoS9jGIhwyR5ymJuQZS+XSWRBkpSzm5fMQb0E 123IBRDTF0wVe4rQQb0JOqXGzEpX+eRm0Qd8j6tME03aWOk8OI0LKC2rSoh8mTxYY1yH HbnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:dkim-signature:content-transfer-encoding :mime-version:references:in-reply-to:message-id:cc:to:subject:from :date:arc-authentication-results; bh=yLBdLl/fjqoM179xl9EYX76KGqXnM+BBGClvoU9reZM=; b=Ll6pFNYxFb5T2+c8xfzJskMaTHVIT/mwtGG7o1AthrjqSZX1+h9u7y7bgOSnmpfny0 oUJX5Z8GaJqC8m2hrNr4QqF0MXiT0LzsHkJGhgE5x+PuFkDjU+PQH1qtzTRGENSTZ195 lYcOgR8wtGXJ0Nkkoy5p4Lz2p9Q/I4IeeoBkXPbNZJmOwrv00a2pQ3OmndAx8qpcTttt GSeVoM0Ia05qbCFnGhfNHL3XRVebYYXmvz6N2giJ2+V+P5r3KTA303InclxsZmViRQWp ozd7vsnREib6bnQaHuzJWaM8TToBDCaNl6eFEdfLdDi2JaPiJDv/qt3Q8xpBVwCIx7/T xXMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b="bL2/BGub"; 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=crapouillou.net Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x66-v6si44543pfx.129.2018.08.16.11.44.53; Thu, 16 Aug 2018 11:45:09 -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=@crapouillou.net header.s=mail header.b="bL2/BGub"; 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=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392208AbeHPTHX (ORCPT + 99 others); Thu, 16 Aug 2018 15:07:23 -0400 Received: from outils.crapouillou.net ([89.234.176.41]:45582 "EHLO crapouillou.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729150AbeHPTHX (ORCPT ); Thu, 16 Aug 2018 15:07:23 -0400 Date: Thu, 16 Aug 2018 18:07:44 +0200 From: Paul Cercueil Subject: Re: [PATCH v6 06/24] clocksource: Add driver for the Ingenic JZ47xx OST To: Mathieu Malaterre Cc: Rob Herring , Mark Rutland , thierry.reding@gmail.com, Daniel Lezcano , Thomas Gleixner , wim@linux-watchdog.org, Guenter Roeck , Ralf Baechle , Paul Burton , James Hogan , Jonathan Corbet , Lee Jones , Ezequiel Garcia , linux-pwm@vger.kernel.org, "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , LKML , linux-watchdog@vger.kernel.org, Linux-MIPS , linux-doc@vger.kernel.org, linux-clk@vger.kernel.org, Maarten ter Huurne Message-Id: <1534435664.5686.0@smtp.crapouillou.net> In-Reply-To: References: <20180809214414.20905-1-paul@crapouillou.net> <20180809214414.20905-7-paul@crapouillou.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1534435677; bh=yLBdLl/fjqoM179xl9EYX76KGqXnM+BBGClvoU9reZM=; h=Date:From:Subject:To:Cc:Message-Id:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding; b=bL2/BGubY35hCVuIuZqgqTUQ2IPYPHZmIHYSwgXchrB6VAm9QxK38K78k39G9oy9E8M/xg3uv94KPtrGO7JJfpbgqiSUfq1N1bY346ohSlX0N5W0LTj6U2ELYyLi7YxYfhUVyG2+xkMQEleUj7zZbUbd0KJ6L37UUiKqZXhcikw= Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Mathieu, Le mar. 14 ao=FBt 2018 =E0 11:42, Mathieu Malaterre a=20 =E9crit : > On Thu, Aug 9, 2018 at 11:44 PM Paul Cercueil =20 > wrote: >>=20 >> From: Maarten ter Huurne >>=20 >> OST is the OS Timer, a 64-bit timer/counter with buffered reading. >>=20 >> SoCs before the JZ4770 had (if any) a 32-bit OST; the JZ4770 and >> JZ4780 have a 64-bit OST. >>=20 >> This driver will register both a clocksource and a sched_clock to=20 >> the >> system. >=20 > With CONFIG_DEBUG_SECTION_MISMATCH=3Dy here is what I see: >=20 > MODPOST vmlinux.o > WARNING: vmlinux.o(.data+0x26600): Section mismatch in reference from > the variable ingenic_ost_driver to the function > .init.text:ingenic_ost_probe() > The variable ingenic_ost_driver references > the function __init ingenic_ost_probe() > If the reference is valid then annotate the > variable with __init* or __refdata (see linux/init.h) or name the=20 > variable: > *_template, *_timer, *_sht, *_ops, *_probe, *_probe_one, *_console >=20 > WARNING: vmlinux.o(.data+0x26660): Section mismatch in reference from > the variable ingenic_tcu_driver to the function > .init.text:ingenic_tcu_probe() > The variable ingenic_tcu_driver references > the function __init ingenic_tcu_probe() > If the reference is valid then annotate the > variable with __init* or __refdata (see linux/init.h) or name the=20 > variable: > *_template, *_timer, *_sht, *_ops, *_probe, *_probe_one, *_console >=20 > Thanks Right, I'll fix it in V7. Thanks! >> Signed-off-by: Maarten ter Huurne >> Signed-off-by: Paul Cercueil >> --- >> drivers/clocksource/Kconfig | 8 ++ >> drivers/clocksource/Makefile | 1 + >> drivers/clocksource/ingenic-ost.c | 208=20 >> ++++++++++++++++++++++++++++++++++++++ >> 3 files changed, 217 insertions(+) >> create mode 100644 drivers/clocksource/ingenic-ost.c >>=20 >> v5: New patch >>=20 >> v6: - Get rid of SoC IDs; pass pointer to ingenic_ost_soc_info as >> devicetree match data instead. >> - Use device_get_match_data() instead of the of_* variant >> - Handle error of dev_get_regmap() properly >>=20 >> diff --git a/drivers/clocksource/Kconfig=20 >> b/drivers/clocksource/Kconfig >> index 98f708208a8d..e855938c69f1 100644 >> --- a/drivers/clocksource/Kconfig >> +++ b/drivers/clocksource/Kconfig >> @@ -619,4 +619,12 @@ config INGENIC_TIMER >> help >> Support for the timer/counter unit of the Ingenic JZ SoCs. >>=20 >> +config INGENIC_OST >> + bool "Ingenic JZ47xx Operating System Timer" >> + depends on MIPS || COMPILE_TEST >> + depends on COMMON_CLK >> + select INGENIC_TIMER >> + help >> + Support for the OS Timer of the Ingenic JZ4770 or similar=20 >> SoC. >> + >> endmenu >> diff --git a/drivers/clocksource/Makefile=20 >> b/drivers/clocksource/Makefile >> index 26877505d400..56ce37252944 100644 >> --- a/drivers/clocksource/Makefile >> +++ b/drivers/clocksource/Makefile >> @@ -75,6 +75,7 @@ obj-$(CONFIG_ASM9260_TIMER) +=3D=20 >> asm9260_timer.o >> obj-$(CONFIG_H8300_TMR8) +=3D h8300_timer8.o >> obj-$(CONFIG_H8300_TMR16) +=3D h8300_timer16.o >> obj-$(CONFIG_H8300_TPU) +=3D h8300_tpu.o >> +obj-$(CONFIG_INGENIC_OST) +=3D ingenic-ost.o >> obj-$(CONFIG_INGENIC_TIMER) +=3D ingenic-timer.o >> obj-$(CONFIG_CLKSRC_ST_LPC) +=3D clksrc_st_lpc.o >> obj-$(CONFIG_X86_NUMACHIP) +=3D numachip.o >> diff --git a/drivers/clocksource/ingenic-ost.c=20 >> b/drivers/clocksource/ingenic-ost.c >> new file mode 100644 >> index 000000000000..400d30656790 >> --- /dev/null >> +++ b/drivers/clocksource/ingenic-ost.c >> @@ -0,0 +1,208 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> +/* >> + * JZ47xx SoCs TCU Operating System Timer driver >> + * >> + * Copyright (C) 2016 Maarten ter Huurne >> + * Copyright (C) 2018 Paul Cercueil >> + */ >> + >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> + >> +#include "ingenic-timer.h" >> + >> +#define TCU_OST_TCSR_MASK 0xc0 >> +#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 u64 notrace ingenic_ost_read_cntl(void) >> +{ >> + /* Bypass the regmap here as we must return as soon as=20 >> possible */ >> + return readl(ingenic_tcu_base + TCU_REG_OST_CNTL); >> +} >> + >> +static u64 notrace ingenic_ost_read_cnth(void) >> +{ >> + /* Bypass the regmap here as we must return as soon as=20 >> possible */ >> + return readl(ingenic_tcu_base + TCU_REG_OST_CNTH); >> +} >> + >> +static u64 notrace ingenic_ost_clocksource_read(struct clocksource=20 >> *cs) >> +{ >> + u32 val1, val2; >> + u64 count, recount; >> + s64 diff; >> + >> + /* >> + * The buffering of the upper 32 bits of the timer prevents=20 >> wrong >> + * results from the bottom 32 bits overflowing due to the=20 >> timer ticking >> + * along. However, it does not prevent wrong results from=20 >> simultaneous >> + * reads of the timer, which could reset the buffer=20 >> mid-read. >> + * Since this kind of wrong read can happen only when the=20 >> bottom bits >> + * overflow, there will be minutes between wrong reads, so=20 >> if we read >> + * twice in succession, at least one of the reads will be=20 >> correct. >> + */ >> + >> + /* Bypass the regmap here as we must return as soon as=20 >> possible */ >> + val1 =3D readl(ingenic_tcu_base + TCU_REG_OST_CNTL); >> + val2 =3D readl(ingenic_tcu_base + TCU_REG_OST_CNTHBUF); >> + count =3D (u64)val1 | (u64)val2 << 32; >> + >> + val1 =3D readl(ingenic_tcu_base + TCU_REG_OST_CNTL); >> + val2 =3D readl(ingenic_tcu_base + TCU_REG_OST_CNTHBUF); >> + recount =3D (u64)val1 | (u64)val2 << 32; >> + >> + /* >> + * A wrong read will produce a result that is 1<<32 too=20 >> high: the bottom >> + * part from before overflow and the upper part from after=20 >> overflow. >> + * Therefore, the lower value of the two reads is the=20 >> correct value. >> + */ >> + >> + diff =3D (s64)(recount - count); >> + if (unlikely(diff < 0)) >> + count =3D recount; >> + >> + return count; >> +} >> + >> +static int __init ingenic_ost_probe(struct platform_device *pdev) >> +{ >> + const struct ingenic_ost_soc_info *soc_info; >> + struct device *dev =3D &pdev->dev; >> + struct ingenic_ost *ost; >> + struct clocksource *cs; >> + unsigned long rate, flags; >> + int err; >> + >> + soc_info =3D device_get_match_data(dev); >> + if (!soc_info) >> + return -EINVAL; >> + >> + ost =3D devm_kzalloc(dev, sizeof(*ost), GFP_KERNEL); >> + if (!ost) >> + return -ENOMEM; >> + >> + ost->map =3D dev_get_regmap(dev->parent, NULL); >> + if (!ost->map) { >> + dev_err(dev, "regmap not found\n"); >> + return -EINVAL; >> + } >> + >> + ost->clk =3D devm_clk_get(dev, "ost"); >> + if (IS_ERR(ost->clk)) >> + return PTR_ERR(ost->clk); >> + >> + err =3D 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 =3D clk_get_rate(ost->clk); >> + >> + /* Enable OST TCU channel */ >> + regmap_write(ost->map, TCU_REG_TESR, BIT(TCU_OST_CHANNEL)); >> + >> + cs =3D &ost->cs; >> + cs->name =3D "ingenic-ost"; >> + cs->rating =3D 320; >> + cs->flags =3D CLOCK_SOURCE_IS_CONTINUOUS; >> + >> + if (soc_info->is64bit) { >> + cs->mask =3D CLOCKSOURCE_MASK(64); >> + cs->read =3D ingenic_ost_clocksource_read; >> + } else { >> + cs->mask =3D CLOCKSOURCE_MASK(32); >> + cs->read =3D (u64 (*)(struct clocksource=20 >> *))ingenic_ost_read_cnth; >> + } >> + >> + err =3D clocksource_register_hz(cs, rate); >> + if (err) { >> + dev_err(dev, "clocksource registration failed:=20 >> %d\n", err); >> + clk_disable_unprepare(ost->clk); >> + return err; >> + } >> + >> + /* Cannot register a sched_clock with interrupts on */ >> + local_irq_save(flags); >> + if (soc_info->is64bit) >> + sched_clock_register(ingenic_ost_read_cntl, 32,=20 >> rate); >> + else >> + sched_clock_register(ingenic_ost_read_cnth, 32,=20 >> rate); >> + local_irq_restore(flags); >> + >> + return 0; >> +} >> + >> +#ifdef CONFIG_PM_SLEEP >> +static int ingenic_ost_suspend(struct device *dev) >> +{ >> + struct ingenic_ost *ost =3D dev_get_drvdata(dev); >> + >> + clk_disable(ost->clk); >> + return 0; >> +} >> + >> +static int ingenic_ost_resume(struct device *dev) >> +{ >> + struct ingenic_ost *ost =3D dev_get_drvdata(dev); >> + >> + return clk_enable(ost->clk); >> +} >> + >> +static SIMPLE_DEV_PM_OPS(ingenic_ost_pm_ops, ingenic_ost_suspend, >> + ingenic_ost_resume); >> +#define INGENIC_OST_PM_OPS (&ingenic_ost_pm_ops) >> +#else >> +#define INGENIC_OST_PM_OPS NULL >> +#endif /* CONFIG_PM_SUSPEND */ >> + >> +static const struct ingenic_ost_soc_info jz4725b_ost_soc_info =3D { >> + .is64bit =3D false, >> +}; >> + >> +static const struct ingenic_ost_soc_info jz4770_ost_soc_info =3D { >> + .is64bit =3D true, >> +}; >> + >> +static const struct of_device_id ingenic_ost_of_match[] =3D { >> + { .compatible =3D "ingenic,jz4725b-ost", .data =3D=20 >> &jz4725b_ost_soc_info, }, >> + { .compatible =3D "ingenic,jz4770-ost", .data =3D=20 >> &jz4770_ost_soc_info, }, >> + { } >> +}; >> +MODULE_DEVICE_TABLE(of, ingenic_ost_of_match); >> + >> +static struct platform_driver ingenic_ost_driver =3D { >> + .probe =3D ingenic_ost_probe, >> + .driver =3D { >> + .name =3D "ingenic-ost", >> + .pm =3D INGENIC_OST_PM_OPS, >> + .of_match_table =3D ingenic_ost_of_match, >> + }, >> +}; >> +module_platform_driver(ingenic_ost_driver); >> -- >> 2.11.0 >>=20 =