mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Biju Das <biju.das.jz@bp.renesas.com>
To: Cosmin-Gabriel Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>,
	William Breathitt Gray <wbg@kernel.org>
Cc: "linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>,
	"linux-renesas-soc@vger.kernel.org"
	<linux-renesas-soc@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Cosmin-Gabriel Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>
Subject: RE: [PATCH v3 12/14] counter: rz-mtu3-cnt: keep cascade_counts_enable in sync with usage
Date: Tue, 17 Nov 2026 14:28:00 +0000	[thread overview]
Message-ID: <TYYPR01MB6603AC9AD34C12A598166EC8867B2@TYYPR01MB6603.jpnprd01.prod.outlook.com> (raw)
In-Reply-To: <20261117102607.2504551-13-cosmin-gabriel.tanislav.xa@renesas.com>

Hi Cosmin,

Thanks for the patch.

> -----Original Message-----
> From: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>
> Sent: 17 November 2026 10:26
> Subject: [PATCH v3 12/14] counter: rz-mtu3-cnt: keep cascade_counts_enable in sync with usage
> 
> LWA is enabled/disabled by writing 1/0 to the cascade_counts_enable sysfs entry.
> 
> The 32-bit channel cannot function without LWA being enabled, and the 16-bit channels cannot function
> with LWA enabled.
> 
> PWM mode cannot function with LWA enabled.
> 
> It is possible to accidentally leave LWA enabled while trying to use PWM mode, leading to unintended
> access to the TGRA or TGRB registers of
> MTU1 and MTU2, which should not be accessed while LWA is enabled.
> 
> It is also possible to toggle LWA while PWM is enabled on MTU2, as the checks inside
> rz_mtu3_lock_if_ch0_is_enabled() do not check if MTU2 is busy, even if this setting affects MTU2.
> 
> To handle this, write 1 to the LWA bit when enabling the 32-bit channel, and automatically write 0 to
> it when disabling the 32-bit channel, to ensure that LWA is always in the correct state for any
> possible usage.
> 
> Remove rz_mtu3_is_counter_invalid() checks as it is now guaranteed that LWA has the proper value if a
> count is enabled.
> 
> This also drops the busy check that rz_mtu3_lock_if_counter_is_valid()
> carried. A count cannot be enabled without the underlying MTU channel being claimed as busy by the
> count driver itself, so the check is redundant.
> 
> Keep the cascade_counts_enable sysfs entry for ABI compatibility.
> 
> Signed-off-by: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@renesas.com>
> ---
> 
> V3:
>  * no changes
> 
> V2:
>  * add struct rz_mtu3_cnt::cascade_counts_enable documentation
>  * move rz_mtu3_set_lwa() call after rz_mtu3_disable()
>  * mention the removed busy check in the message
> 
>  drivers/counter/rz-mtu3-cnt.c | 95 +++++++----------------------------
>  1 file changed, 18 insertions(+), 77 deletions(-)
> 
> diff --git a/drivers/counter/rz-mtu3-cnt.c b/drivers/counter/rz-mtu3-cnt.c index
> e3578850f024..07dd7f588911 100644
> --- a/drivers/counter/rz-mtu3-cnt.c
> +++ b/drivers/counter/rz-mtu3-cnt.c
> @@ -71,6 +71,7 @@
>   * @timer_mode: Cache for timer mode values
>   * @mtclkc_mtclkd: Cache for external input phase clock select value
>   * @direction: Cache for direction values
> + * @cascade_enable: Cache for cascade_counts_enable value
>   */
>  struct rz_mtu3_cnt {
>  	struct mutex lock;
> @@ -81,6 +82,7 @@ struct rz_mtu3_cnt {
>  	bool count_is_enabled[RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS];
>  	bool direction[RZ_MTU3_MAX_LOGICAL_CNTR_CHANNELS];
>  	bool mtclkc_mtclkd;
> +	bool cascade_enable;
>  };
> 
>  static const enum counter_function rz_mtu3_count_functions[] = { @@ -102,44 +104,6 @@ static inline
> struct rz_mtu3_channel *rz_mtu3_get_ch(struct counter_device *coun
>  	return &priv->ch[ch_id];
>  }
> 
> -static bool rz_mtu3_is_counter_invalid(struct counter_device *counter, int id) -{
> -	struct rz_mtu3_cnt *const priv = counter_priv(counter);
> -	unsigned long tmdr;
> -
> -	pm_runtime_get_sync(counter->parent);
> -	tmdr = rz_mtu3_shared_reg_read(priv->ch, RZ_MTU3_TMDR3);
> -	pm_runtime_put(counter->parent);
> -
> -	if (id == RZ_MTU3_32_BIT_CH && test_bit(RZ_MTU3_TMDR3_LWA, &tmdr))
> -		return false;
> -
> -	if (id != RZ_MTU3_32_BIT_CH && !test_bit(RZ_MTU3_TMDR3_LWA, &tmdr))
> -		return false;
> -
> -	return true;
> -}
> -
> -static int rz_mtu3_lock_if_counter_is_valid(struct counter_device *counter,
> -					    struct rz_mtu3_channel *const ch,
> -					    struct rz_mtu3_cnt *const priv,
> -					    int id)
> -{
> -	mutex_lock(&priv->lock);
> -
> -	if (ch->is_busy && !priv->count_is_enabled[id]) {
> -		mutex_unlock(&priv->lock);
> -		return -EINVAL;
> -	}
> -
> -	if (rz_mtu3_is_counter_invalid(counter, id)) {
> -		mutex_unlock(&priv->lock);
> -		return -EBUSY;
> -	}
> -
> -	return 0;
> -}
> -
>  static int rz_mtu3_lock_if_count_is_enabled(struct rz_mtu3_channel *const ch,
>  					    struct rz_mtu3_cnt *const priv,
>  					    int id)
> @@ -195,17 +159,19 @@ static bool rz_mtu3_get_direction(struct rz_mtu3_channel *const ch)
>  	return !!(tsr & RZ_MTU3_TSR_TCFD);
>  }
> 
> +static void rz_mtu3_set_lwa(struct rz_mtu3_channel *const ch, bool
> +enable) {
> +	rz_mtu3_shared_reg_update_bit(ch, RZ_MTU3_TMDR3, RZ_MTU3_TMDR3_LWA,
> +				      enable);
> +}
> +
>  static int rz_mtu3_count_read(struct counter_device *counter,
>  			      struct counter_count *count, u64 *val)  {
>  	struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
> -	int ret;
> -
> -	ret = rz_mtu3_lock_if_counter_is_valid(counter, ch, priv, count->id);
> -	if (ret)
> -		return ret;
> 
> +	mutex_lock(&priv->lock);
>  	if (priv->count_is_enabled[count->id])
>  		priv->count[count->id] = rz_mtu3_get_count(ch, count->id);
>  	*val = priv->count[count->id];
> @@ -220,15 +186,11 @@ static int rz_mtu3_count_write(struct counter_device *counter,
>  	struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
>  	u32 count_val = val;
> -	int ret;
> 
>  	if (count->id != RZ_MTU3_32_BIT_CH)
>  		count_val &= U16_MAX;
> 
> -	ret = rz_mtu3_lock_if_counter_is_valid(counter, ch, priv, count->id);
> -	if (ret)
> -		return ret;
> -
> +	mutex_lock(&priv->lock);
>  	if (priv->count_is_enabled[count->id])
>  		rz_mtu3_set_count(ch, count->id, count_val);
>  	priv->count[count->id] = count_val;
> @@ -347,14 +309,9 @@ static int rz_mtu3_count_ceiling_read(struct counter_device *counter,
>  				      struct counter_count *count,
>  				      u64 *ceiling)
>  {
> -	struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
> -	int ret;
> -
> -	ret = rz_mtu3_lock_if_counter_is_valid(counter, ch, priv, count->id);
> -	if (ret)
> -		return ret;
> 
> +	mutex_lock(&priv->lock);
>  	*ceiling = priv->ceiling[count->id];
>  	mutex_unlock(&priv->lock);
>  	return 0;
> @@ -366,11 +323,8 @@ static int rz_mtu3_count_ceiling_write(struct counter_device *counter,  {
>  	struct rz_mtu3_channel *const ch = rz_mtu3_get_ch(counter, count->id);
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
> -	int ret;
> 
> -	ret = rz_mtu3_lock_if_counter_is_valid(counter, ch, priv, count->id);
> -	if (ret)
> -		return ret;
> +	mutex_lock(&priv->lock);
> 
>  	switch (count->id) {
>  	case RZ_MTU3_16_BIT_MTU1_CH:
> @@ -407,6 +361,7 @@ static void rz_mtu3_32bit_cnt_setting(struct counter_device *counter)
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
> 
>  	rz_mtu3_set_phcksel(priv->ch, priv->mtclkc_mtclkd);
> +	rz_mtu3_set_lwa(ch1, true);
> 
>  	rz_mtu3_8bit_ch_write(ch1, RZ_MTU3_TMDR1,
>  			      priv->timer_mode[RZ_MTU3_32_BIT_CH]);
> @@ -485,6 +440,7 @@ static void rz_mtu3_terminate_counter(struct counter_device *counter, int id)
>  	if (id == RZ_MTU3_32_BIT_CH) {
>  		rz_mtu3_disable(ch2);
>  		rz_mtu3_disable(ch1);
> +		rz_mtu3_set_lwa(ch1, false);
>  		rz_mtu3_release_channel(ch2);
>  		rz_mtu3_release_channel(ch1);
>  	} else {
> @@ -552,17 +508,9 @@ static int rz_mtu3_cascade_counts_enable_get(struct counter_device *counter,
>  					     u8 *cascade_enable)
>  {
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
> -	unsigned long tmdr;
> -	int ret;
> 
> -	ret = rz_mtu3_lock_if_ch0_is_enabled(priv);
> -	if (ret)
> -		return ret;
> -
> -	pm_runtime_get_sync(counter->parent);
> -	tmdr = rz_mtu3_shared_reg_read(priv->ch, RZ_MTU3_TMDR3);
> -	pm_runtime_put(counter->parent);
> -	*cascade_enable = test_bit(RZ_MTU3_TMDR3_LWA, &tmdr);
> +	mutex_lock(&priv->lock);
> +	*cascade_enable = priv->cascade_enable;


priv->cascade_enable is never consumed, so setting this value to 0 and 32-bit count enabling
still set LWA=1 may be return error value for this scenario.

Cheers,
Biju


>  	mutex_unlock(&priv->lock);
> 
>  	return 0;
> @@ -572,16 +520,9 @@ static int rz_mtu3_cascade_counts_enable_set(struct counter_device *counter,
>  					     u8 cascade_enable)
>  {
>  	struct rz_mtu3_cnt *const priv = counter_priv(counter);
> -	int ret;
> 
> -	ret = rz_mtu3_lock_if_ch0_is_enabled(priv);
> -	if (ret)
> -		return ret;
> -
> -	pm_runtime_get_sync(counter->parent);
> -	rz_mtu3_shared_reg_update_bit(priv->ch, RZ_MTU3_TMDR3,
> -				      RZ_MTU3_TMDR3_LWA, cascade_enable);
> -	pm_runtime_put(counter->parent);
> +	mutex_lock(&priv->lock);
> +	priv->cascade_enable = cascade_enable;
>  	mutex_unlock(&priv->lock);
> 
>  	return 0;
> --
> 2.56.0

  reply	other threads:[~2026-11-17 14:28 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 10:25 [PATCH v3 00/14] MTU3 counter fixes and improvements Cosmin Tanislav
2026-11-17 10:25 ` [PATCH v3 01/14] counter: rz-mtu3-cnt: do not assign struct rz_mtu3_channel::dev Cosmin Tanislav
2026-11-17 14:04   ` Biju Das
2026-11-17 10:25 ` [PATCH v3 02/14] counter: rz-mtu3-cnt: remove manual runtime PM handling Cosmin Tanislav
2026-11-17 14:04   ` Biju Das
2026-11-17 10:25 ` [PATCH v3 03/14] counter: rz-mtu3-cnt: use device-managed pm_runtime_enable() Cosmin Tanislav
2026-11-17 10:25 ` [PATCH v3 04/14] counter: rz-mtu3-cnt: read enable value from cache Cosmin Tanislav
2026-11-17 14:05   ` Biju Das
2026-11-17 10:25 ` [PATCH v3 05/14] counter: rz-mtu3-cnt: disable channel before releasing Cosmin Tanislav
2026-11-17 14:05   ` Biju Das
2026-11-17 10:25 ` [PATCH v3 06/14] counter: rz-mtu3-cnt: unify ceiling values Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 07/14] counter: rz-mtu3-cnt: cache " Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 08/14] counter: rz-mtu3-cnt: cache count values Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 09/14] counter: rz-mtu3-cnt: cache function values Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 10/14] counter: rz-mtu3-cnt: cache external input phase clock value Cosmin Tanislav
2026-11-17 10:26 ` [PATCH v3 11/14] counter: rz-mtu3-cnt: cache direction values Cosmin Tanislav
2026-11-17 13:49   ` Biju Das
2026-11-17 10:26 ` [PATCH v3 12/14] counter: rz-mtu3-cnt: keep cascade_counts_enable in sync with usage Cosmin Tanislav
2026-11-17 14:28   ` Biju Das [this message]
2026-11-17 14:42     ` Cosmin-Gabriel Tanislav
2026-11-17 14:57       ` Biju Das
2026-11-17 15:01         ` Biju Das
2026-11-17 10:26 ` [PATCH v3 13/14] counter: rz-mtu3-cnt: drop conditional locks Cosmin Tanislav
2026-11-17 17:10   ` Biju Das
2026-11-17 10:26 ` [PATCH v3 14/14] counter: rz-mtu3-cnt: use pm_runtime_resume_and_get() Cosmin Tanislav
2026-11-17 17:04   ` Biju Das

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=TYYPR01MB6603AC9AD34C12A598166EC8867B2@TYYPR01MB6603.jpnprd01.prod.outlook.com \
    --to=biju.das.jz@bp.renesas.com \
    --cc=cosmin-gabriel.tanislav.xa@renesas.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=wbg@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®