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:28:14 EST
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 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