* [PATCH v2] pwm: stm32: Fix division by zero for 32-bit timers
@ 2026-11-02 10:32 Thomas Perrot
2026-11-26 7:38 ` Patrice CHOTARD
0 siblings, 1 reply; 2+ messages in thread
From: Thomas Perrot @ 2026-11-02 10:32 UTC (permalink / raw)
To: Fabrice Gasnier, Patrice Chotard, uboot-stm32, u-boot
Cc: Thomas Petazzoni, Patrick Delaunay, Tom Rini, Cheick Traore,
Thomas Perrot
stm32_pwm_set_config() derives the prescaler with
do_div(div, priv->max_arr + 1). max_arr is a u32, so on a 32-bit timer
(max_arr = 0xFFFFFFFF, e.g. TIM5 on STM32MP15) the sum wraps to 0 and
do_div() divides by zero. Configuring a PWM on such a timer, for
instance for a backlight, resets the board.
do_div() takes a 32-bit divisor, so widening the operand is not enough.
Use div64_u64() with a 64-bit divisor. The prescaler then comes out as
0, which is right since the period fits in ARR without prescaling.
Fixes: be5523d38296 ("pwm: stm32: add driver to support pwm with timer")
Signed-off-by: Thomas Perrot <thomas.perrot@bootlin.com>
Tested-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
Reviewed-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
On a 32-bit timer such as TIM5 on STM32MP15, stm32_pwm_set_config()
divides by max_arr + 1, which wraps to 0 in a u32. Seen when a
backlight on TIM5 is configured from U-Boot.
I first implemented this fix on ST's U-Boot v2023.10-stm32mp tree.
---
Changes in v2:
- Drop the div variable and derive the prescaler from prd directly
- Collect Tested-by and Reviewed-by from Fabrice
- Reword the note on where the fix was first implemented
- Link to v1: https://lore.kernel.org/r/20261030-tprrt-pwm-stm32-32bit-timer-div-v1-1-f6f93d7923e4@bootlin.com
---
drivers/pwm/pwm-stm32.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
index a691f75e4a71..212389088f74 100644
--- a/drivers/pwm/pwm-stm32.c
+++ b/drivers/pwm/pwm-stm32.c
@@ -13,6 +13,7 @@
#include <asm/arch/timers.h>
#include <dm/device_compat.h>
#include <linux/bitfield.h>
+#include <linux/math64.h>
#include <linux/time.h>
#define CCMR_CHANNEL_SHIFT 8
@@ -33,7 +34,7 @@ static int stm32_pwm_set_config(struct udevice *dev, uint channel,
{
struct stm32_timers_plat *plat = dev_get_plat(dev_get_parent(dev));
struct stm32_timers_priv *priv = dev_get_priv(dev_get_parent(dev));
- unsigned long long prd, div, dty;
+ unsigned long long prd, dty;
unsigned int prescaler = 0;
u32 ccmr, mask, shift;
@@ -60,12 +61,10 @@ static int stm32_pwm_set_config(struct udevice *dev, uint channel,
* (period_ns * clkrate) // (NSEC_PER_SEC * (max_arr + 1)) ≤ prescaler
*/
- div = (unsigned long long)priv->rate * period_ns;
- do_div(div, NSEC_PER_SEC);
- prd = div;
+ prd = (unsigned long long)priv->rate * period_ns;
+ do_div(prd, NSEC_PER_SEC);
- do_div(div, priv->max_arr + 1);
- prescaler = div;
+ prescaler = div64_u64(prd, (u64)priv->max_arr + 1);
if (prescaler > MAX_TIM_PSC)
return -EINVAL;
---
base-commit: 03e45557e1f38a39a0c80fe30483130ff376cf92
change-id: 20261029-tprrt-pwm-stm32-32bit-timer-div-eeb070ead061
Best regards,
--
Thomas Perrot <thomas.perrot@tupi.fr>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] pwm: stm32: Fix division by zero for 32-bit timers
2026-11-02 10:32 [PATCH v2] pwm: stm32: Fix division by zero for 32-bit timers Thomas Perrot
@ 2026-11-26 7:38 ` Patrice CHOTARD
0 siblings, 0 replies; 2+ messages in thread
From: Patrice CHOTARD @ 2026-11-26 7:38 UTC (permalink / raw)
To: Thomas Perrot, Fabrice Gasnier, uboot-stm32, u-boot
Cc: Thomas Petazzoni, Patrick Delaunay, Tom Rini, Cheick Traore
On 11/2/26 11:32, Thomas Perrot wrote:
> stm32_pwm_set_config() derives the prescaler with
> do_div(div, priv->max_arr + 1). max_arr is a u32, so on a 32-bit timer
> (max_arr = 0xFFFFFFFF, e.g. TIM5 on STM32MP15) the sum wraps to 0 and
> do_div() divides by zero. Configuring a PWM on such a timer, for
> instance for a backlight, resets the board.
>
> do_div() takes a 32-bit divisor, so widening the operand is not enough.
> Use div64_u64() with a 64-bit divisor. The prescaler then comes out as
> 0, which is right since the period fits in ARR without prescaling.
>
> Fixes: be5523d38296 ("pwm: stm32: add driver to support pwm with timer")
> Signed-off-by: Thomas Perrot <thomas.perrot@bootlin.com>
> Tested-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
> Reviewed-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
> ---
> On a 32-bit timer such as TIM5 on STM32MP15, stm32_pwm_set_config()
> divides by max_arr + 1, which wraps to 0 in a u32. Seen when a
> backlight on TIM5 is configured from U-Boot.
>
> I first implemented this fix on ST's U-Boot v2023.10-stm32mp tree.
> ---
> Changes in v2:
> - Drop the div variable and derive the prescaler from prd directly
> - Collect Tested-by and Reviewed-by from Fabrice
> - Reword the note on where the fix was first implemented
> - Link to v1: https://lore.kernel.org/r/20261030-tprrt-pwm-stm32-32bit-timer-div-v1-1-f6f93d7923e4@bootlin.com
> ---
> drivers/pwm/pwm-stm32.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
> index a691f75e4a71..212389088f74 100644
> --- a/drivers/pwm/pwm-stm32.c
> +++ b/drivers/pwm/pwm-stm32.c
> @@ -13,6 +13,7 @@
> #include <asm/arch/timers.h>
> #include <dm/device_compat.h>
> #include <linux/bitfield.h>
> +#include <linux/math64.h>
> #include <linux/time.h>
>
> #define CCMR_CHANNEL_SHIFT 8
> @@ -33,7 +34,7 @@ static int stm32_pwm_set_config(struct udevice *dev, uint channel,
> {
> struct stm32_timers_plat *plat = dev_get_plat(dev_get_parent(dev));
> struct stm32_timers_priv *priv = dev_get_priv(dev_get_parent(dev));
> - unsigned long long prd, div, dty;
> + unsigned long long prd, dty;
> unsigned int prescaler = 0;
> u32 ccmr, mask, shift;
>
> @@ -60,12 +61,10 @@ static int stm32_pwm_set_config(struct udevice *dev, uint channel,
> * (period_ns * clkrate) // (NSEC_PER_SEC * (max_arr + 1)) ≤ prescaler
> */
>
> - div = (unsigned long long)priv->rate * period_ns;
> - do_div(div, NSEC_PER_SEC);
> - prd = div;
> + prd = (unsigned long long)priv->rate * period_ns;
> + do_div(prd, NSEC_PER_SEC);
>
> - do_div(div, priv->max_arr + 1);
> - prescaler = div;
> + prescaler = div64_u64(prd, (u64)priv->max_arr + 1);
> if (prescaler > MAX_TIM_PSC)
> return -EINVAL;
>
>
> ---
> base-commit: 03e45557e1f38a39a0c80fe30483130ff376cf92
> change-id: 20261029-tprrt-pwm-stm32-32bit-timer-div-eeb070ead061
>
> Best regards,
Hi Thomas
Applied to u-boot-stm32/main
Thanks
Patrice
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-26 7:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-02 10:32 [PATCH v2] pwm: stm32: Fix division by zero for 32-bit timers Thomas Perrot
2026-11-26 7:38 ` Patrice CHOTARD
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.