Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E4B6BC433EF for ; Thu, 3 Mar 2022 10:01:22 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5AE2383811; Thu, 3 Mar 2022 11:01:20 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=bootlin.com header.i=@bootlin.com header.b="Of94FbOH"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B43FE83D22; Thu, 3 Mar 2022 11:01:18 +0100 (CET) Received: from relay11.mail.gandi.net (relay11.mail.gandi.net [217.70.178.231]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 104BB83510 for ; Thu, 3 Mar 2022 11:01:14 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=clement.leger@bootlin.com Received: (Authenticated sender: clement.leger@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id 092F410002D; Thu, 3 Mar 2022 10:00:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1646301673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=K+ncY8kk9XA5MFWZFXRxgWEsEitQyM4Cyl4N2y5Mke8=; b=Of94FbOHnfdHUC6Kw4+xv5wCjynyJoVOlof5lMAOJfGs0NHAEW5LYQqniJ4uQ2LgS3xc86 3nkOtXipaqXu33eEbnZA1NRWMZ3DjA/wX6KYbCHKs4d3IX/fz1X6nN7G3kHy62OVtwtqKO bZ6BAaHcul5wUW7bqVRPDTl3rzaCokT8fIwoQ93cqYf55y2tR09QDTA8bXs36i0FgFAppP fMrHFZryAHXjeLpEoIWm6eu5/CYRPyjtaKEsGAmUdfg8vJ9Jso8Y63VI9OgFs2fQHTpuqT RUsDZdPTMkSDMiH6e860r4lUJqCkEk/Ugfc4a8l5KjLFGx6y84zhd4KO0TjYHQ== Date: Thu, 3 Mar 2022 10:59:30 +0100 From: =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= To: Cc: , , , Subject: Re: [PATCH v2 1/3] timer: atmel_tcb_timer: add atmel_tcb driver Message-ID: <20220303105930.079c6644@fixe.home> In-Reply-To: <13d08250-1e38-d2a8-7b87-821fbc2d3b20@microchip.com> References: <20220202144301.3871490-1-clement.leger@bootlin.com> <20220202144301.3871490-2-clement.leger@bootlin.com> <13d08250-1e38-d2a8-7b87-821fbc2d3b20@microchip.com> Organization: Bootlin X-Mailer: Claws Mail 4.0.0 (GTK+ 3.24.31; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean Le Thu, 3 Mar 2022 09:31:14 +0000, a =C3=A9crit : > Hello Clement, >=20 > Thank you for your patch. >=20 > On 2/2/22 4:42 PM, Cl=C3=A9ment L=C3=A9ger wrote: > > Add a driver for the timer counter block that can be found on sama5d2. > > This driver will be used when booting under OP-TEE since the pit timer > > which is part of the SYSC is secured. Channel 1 & 2 are configured to > > be chained together which allows to have a 64bits counter. > >=20 > > Signed-off-by: Cl=C3=A9ment L=C3=A9ger > > --- > > MAINTAINERS | 1 + > > drivers/timer/Kconfig | 7 ++ > > drivers/timer/Makefile | 1 + > > drivers/timer/atmel_tcb_timer.c | 160 ++++++++++++++++++++++++++++++++ > > 4 files changed, 169 insertions(+) > > create mode 100644 drivers/timer/atmel_tcb_timer.c > >=20 > > diff --git a/MAINTAINERS b/MAINTAINERS > > index dcdd99e368..6ff95aea39 100644 > > --- a/MAINTAINERS > > +++ b/MAINTAINERS > > @@ -343,6 +343,7 @@ F: arch/arm/mach-at91/ > > F: board/atmel/ > > F: drivers/cpu/at91_cpu.c > > F: drivers/misc/microchip_flexcom.c > > +F: drivers/timer/atmel_tcb_timer.c > > F: include/dt-bindings/mfd/atmel-flexcom.h > > F: drivers/timer/mchp-pit64b-timer.c > >=20 > > diff --git a/drivers/timer/Kconfig b/drivers/timer/Kconfig > > index 8913142654..9b7bbec654 100644 > > --- a/drivers/timer/Kconfig > > +++ b/drivers/timer/Kconfig > > @@ -96,6 +96,13 @@ config ATMEL_PIT_TIMER > > it is designed to offer maximum accuracy and efficient manag= ement, > > even for systems with long response time. > >=20 > > +config ATMEL_TCB_TIMER > > + bool "Atmel timer counter support" > > + depends on TIMER > > + help > > + Select this to enable the use of the timer counter as a monot= onic > > + counter. =20 >=20 > maybe you should specify that this is specific to at91 architecture >=20 > > + > > config CADENCE_TTC_TIMER > > bool "Cadence TTC (Triple Timer Counter)" > > depends on TIMER > > diff --git a/drivers/timer/Makefile b/drivers/timer/Makefile > > index e2bd530eb0..58da6c1e84 100644 > > --- a/drivers/timer/Makefile > > +++ b/drivers/timer/Makefile > > @@ -10,6 +10,7 @@ obj-$(CONFIG_ARC_TIMER) +=3D arc_timer.o > > obj-$(CONFIG_AST_TIMER) +=3D ast_timer.o > > obj-$(CONFIG_ATCPIT100_TIMER) +=3D atcpit100_timer.o > > obj-$(CONFIG_ATMEL_PIT_TIMER) +=3D atmel_pit_timer.o > > +obj-$(CONFIG_ATMEL_TCB_TIMER) +=3D atmel_tcb_timer.o > > obj-$(CONFIG_CADENCE_TTC_TIMER) +=3D cadence-ttc.o > > obj-$(CONFIG_DESIGNWARE_APB_TIMER) +=3D dw-apb-timer.o > > obj-$(CONFIG_MPC83XX_TIMER) +=3D mpc83xx_timer.o > > diff --git a/drivers/timer/atmel_tcb_timer.c b/drivers/timer/atmel_tcb_= timer.c > > new file mode 100644 > > index 0000000000..6f2c054629 > > --- /dev/null > > +++ b/drivers/timer/atmel_tcb_timer.c > > @@ -0,0 +1,160 @@ > > +// SPDX-License-Identifier: GPL-2.0+ > > +/* > > + * Copyright (C) 2022 Microchip Corporation > > + * > > + * Author: Cl=C3=A9ment L=C3=A9ger > > + */ > > + > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > + > > +#define TCB_CHAN(chan) ((chan) * 0x40) > > + > > +#define TCB_CCR(chan) (0x0 + TCB_CHAN(chan)) > > +#define TCB_CCR_SWTRG 0x4 > > +#define TCB_CCR_CLKEN 0x1 > > + > > +#define TCB_CMR(chan) (0x4 + TCB_CHAN(chan)) > > +#define TCB_CMR_WAVE BIT(15) > > +#define TCB_CMR_TIMER_CLOCK0 0 > > +#define TCB_CMR_XC1 6 > > +#define TCB_CMR_ACPA_SET (1 << 16) > > +#define TCB_CMR_ACPC_CLEAR (2 << 18) > > + > > +#define TCB_CV(chan) (0x10 + TCB_CHAN(chan)) > > + > > +#define TCB_RA(chan) (0x14 + TCB_CHAN(chan)) > > +#define TCB_RB(chan) (0x18 + TCB_CHAN(chan)) > > +#define TCB_RC(chan) (0x1c + TCB_CHAN(chan)) > > + > > +#define TCB_IER(chan) (0x24 + TCB_CHAN(chan)) > > +#define TCB_IER_COVFS 0x1 > > + > > +#define TCB_SR(chan) (0x20 + TCB_CHAN(chan)) > > +#define TCB_SR_COVFS 0x1 =20 >=20 > Some of these defines are unused. It is better to remove them for simplic= ity Acked. >=20 > > + > > +#define TCB_IDR(chan) (0x28 + TCB_CHAN(chan)) > > + > > +#define TCB_BCR 0xc0 > > +#define TCB_BCR_SYNC 0x1 > > + > > +#define TCB_BMR 0xc4 > > +#define TCB_BMR_TC1XC1S_TIOA0 (2 << 2) > > + > > +#define TCB_WPMR 0xe4 > > +#define TCB_WPMR_WAKEY 0x54494d > > + > > +struct atmel_tcb_plat { > > + void __iomem *base; > > +}; > > + > > +static u64 atmel_tcb_get_count(struct udevice *dev) > > +{ > > + struct atmel_tcb_plat *plat =3D dev_get_plat(dev); > > + u64 cv0 =3D 0; > > + u64 cv1 =3D 0; > > + > > + do { > > + cv1 =3D readl(plat->base + TCB_CV(1)); > > + cv0 =3D readl(plat->base + TCB_CV(0)); > > + } while (readl(plat->base + TCB_CV(1)) !=3D cv1); =20 >=20 > Are you reading cv1 multiple times without storing the cv1 value ? >=20 > And I don't really like this loop. What happens if after reading again=20 > CV(1) it's always different from the stored cv1 value ? This loops allows to read a "coherent" 64 bits value from two 32 bits registers that are handled separatly by the hardware. This is just a precaution to avoid reading value while overflowing the 32 bits low value. Lets say the counter contains the following values: cv1 =3D 0 cv0 =3D 0xffffffff If we read cv1, and then cv0, we might potentially read cv1 =3D 0 and cv0 =3D 0 due to counter being incremented instead ov cv1 =3D 1, cv0 =3D 0. So actually, this check with cv1 will almost never be true and will potentially just happens if reading at the exact same time cv0 overflows. I can downgrade to using a 32 bits counter if you want. Please note that the same logic is used in Linux driver. >=20 > > + > > + cv0 |=3D cv1 << 32; > > + > > + return cv0; > > +} > > + > > +static void atmel_tcb_configure(void __iomem *base) > > +{ > > + /* Disable write protection */ > > + writel(TCB_WPMR_WAKEY, base + TCB_WPMR); > > + > > + /* Disable all irqs for both channel 0 & 1 */ > > + writel(0xff, base + TCB_IDR(0)); > > + writel(0xff, base + TCB_IDR(1)); > > + > > + /* > > + * In order to avoid wrapping, use a 64 bit counter by chaining > > + * two channels. > > + * Channel 0 is configured to generate a clock on TIOA0 which i= s cleared > > + * when reaching 0x80000000 and set when reaching 0. > > + */ > > + writel(TCB_CMR_TIMER_CLOCK0 | TCB_CMR_WAVE | TCB_CMR_ACPA_SET > > + | TCB_CMR_ACPC_CLEAR, base + TCB_CMR(0)); > > + writel(0x80000000, base + TCB_RC(0)); > > + writel(0x1, base + TCB_RA(0)); > > + writel(TCB_CCR_CLKEN, base + TCB_CCR(0)); > > + > > + /* Channel 1 is configured to use TIOA0 as input */ > > + writel(TCB_CMR_XC1 | TCB_CMR_WAVE, base + TCB_CMR(1)); > > + writel(TCB_CCR_CLKEN, base + TCB_CCR(1)); > > + > > + /* Set XC1 input to be TIOA0 (ie output of Channel 0) */ > > + writel(TCB_BMR_TC1XC1S_TIOA0, base + TCB_BMR); > > + > > + /* Sync & start all timers */ > > + writel(TCB_BCR_SYNC, base + TCB_BCR); > > +} > > + > > +static int atmel_tcb_probe(struct udevice *dev) > > +{ > > + struct atmel_tcb_plat *plat =3D dev_get_plat(dev); > > + struct timer_dev_priv *uc_priv =3D dev_get_uclass_priv(dev); > > + struct clk clk; > > + ulong clk_rate; > > + int ret; > > + > > + if (!device_is_compatible(dev->parent, "atmel,sama5d2-tcb")) > > + return -EINVAL; =20 >=20 > I find it odd that you check the compatible for a parent of this device=20 > when this device is compatible only with 'atmel,tcb-timer' . >=20 > I would expect to have a driver specific for atmel,sama5d2-tcb and probe= =20 > it accordingly. Since we "must" use the Linux bindings, this should be done this way. In linux, it is done the same way: static const struct of_device_id atmel_tcb_of_match[] =3D { ... { .compatible =3D "atmel,sama5d2-tcb", ...}, { /* sentinel */ } }; static int __init tcb_clksrc_init(struct device_node *node) { [...] tc.regs =3D of_iomap(node->parent, 0); if (!tc.regs) return -ENXIO; [...] match =3D of_match_node(atmel_tcb_of_match, node->parent); if (!match) return -ENODEV; [...] } >=20 > > + > > + /* Currently, we only support channel 0 and 1 to be chained */ > > + if (dev_read_addr_index(dev, 0) !=3D 0 && > > + dev_read_addr_index(dev, 1) !=3D 1) > > + return -EINVAL; =20 >=20 > In here maybe we should at least emit a debug message to log on the=20 > console, if we do not support different things. Ok, that would be a good thing indeed. >=20 > > + > > + ret =3D clk_get_by_name(dev->parent, "gclk", &clk); > > + if (ret) > > + return -EINVAL; > > + > > + clk_rate =3D clk_get_rate(&clk); > > + if (!clk_rate) > > + return -EINVAL; > > + > > + uc_priv->clock_rate =3D clk_rate; > > + > > + atmel_tcb_configure(plat->base); > > + > > + return 0; > > +} > > + > > +static int atmel_tcb_of_to_plat(struct udevice *dev) > > +{ > > + struct atmel_tcb_plat *plat =3D dev_get_plat(dev); > > + > > + plat->base =3D dev_read_addr_ptr(dev->parent); =20 >=20 > It looks like you want to handle the parent in fact. > This does not look very straight forward from my perspective. >=20 > Added Claudiu to this thread who knows better the timer blocks as they=20 > are designed in Linux The same thing is done in Linux also (see above). This is caused by the specific bindings. >=20 > > + > > + return 0; > > +} > > + > > +static const struct timer_ops atmel_tcb_ops =3D { > > + .get_count =3D atmel_tcb_get_count, > > +}; > > + > > +static const struct udevice_id atmel_tcb_ids[] =3D { > > + { .compatible =3D "atmel,tcb-timer" }, > > + { } > > +}; > > + > > +U_BOOT_DRIVER(atmel_tcb) =3D { > > + .name =3D "atmel_tcb", > > + .id =3D UCLASS_TIMER, > > + .of_match =3D atmel_tcb_ids, > > + .of_to_plat =3D atmel_tcb_of_to_plat, > > + .plat_auto =3D sizeof(struct atmel_tcb_plat), > > + .probe =3D atmel_tcb_probe, > > + .ops =3D &atmel_tcb_ops, > > +}; > > -- > > 2.34.1 > > =20 >=20 --=20 Cl=C3=A9ment L=C3=A9ger, Embedded Linux and Kernel engineer at Bootlin https://bootlin.com