RE: [PATCH v3 12/14] counter: rz-mtu3-cnt: keep cascade_counts_enable in sync with usage
From: Biju Das
Date: Tue Nov 17 2026 - 09:58:26 EST
Hi Cosmin,
> -----Original Message-----
> From: Cosmin-Gabriel Tanislav <cosmin-gabriel.tanislav.xa@xxxxxxxxxxx>
> Sent: 17 November 2026 14:43
> Subject: RE: [PATCH v3 12/14] counter: rz-mtu3-cnt: keep cascade_counts_enable in sync with usage
>
> > From: Biju Das <biju.das.jz@xxxxxxxxxxxxxx>
> > Sent: Tuesday, November 17, 2026 4:28 PM
> >
> > Hi Cosmin,
> >
> > Thanks for the patch.
> >
> > > -----Original Message-----
> > > From: Cosmin Tanislav <cosmin-gabriel.tanislav.xa@xxxxxxxxxxx>
> > > 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@xxxxxxxxxxx>
> > > ---
> > >
> > > 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
> > priv->32-bit count enabling
> > still set LWA=1 may be return error value for this scenario.
> >
>
> We only keep priv->cascade_enable so that the user-visible file reports a stable value, to preserve
> ABI compatibility. This patch merely allows usage of the 32-bit counter whatever the cascade_enable
> value may be.
>
> Existing user scenarios keep working, unless they relied on specifically writing 0 to cascade_enable
> and then trying to use the 32-bit counter, expecting to get errors.
>
> Please keep in mind that before this series the error wouldn't appear on enable either, just when
> accessing the ceiling or count values.
>
> This does not seem like a real use-case to me as it does not make sense for users to depend on an
> error being generated.
>
> The benefit is that new users will not have to touch the cascade_enable at all and the logic gets much
> simpler.
>
OK.
Maybe add a explanation in the patch commit message.
"The cascade_counts_enable attribute is now only a stored value and no
longer affects driver behaviour. The 32-bit count is available whether
the attribute is set to 0 or 1.
The only scenario that changes is writing 0 and then expecting errors."
Also update ABI description(Documentation/ABI/testing/sysfs-bus-counter)
What: /sys/bus/counter/devices/counterX/cascade_counts_enable
Description:
Indicates whether the counts are cascaded. This value is
stored only and does not affect behaviour: the 32-bit
count works regardless of whether this attribute is set
to 0 or 1.
Cheers,
Biju