From: Patrice CHOTARD <patrice.chotard@foss.st.com>
To: Thomas Perrot <thomas.perrot@bootlin.com>,
Fabrice Gasnier <fabrice.gasnier@foss.st.com>,
<uboot-stm32@st-md-mailman.stormreply.com>,
<u-boot@lists.u-boot-project.org>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
Patrick Delaunay <patrick.delaunay@foss.st.com>,
Tom Rini <trini@konsulko.com>,
Cheick Traore <cheick.traore@foss.st.com>
Subject: Re: [PATCH v2] pwm: stm32: Fix division by zero for 32-bit timers
Date: Thu, 26 Nov 2026 08:38:44 +0100 [thread overview]
Message-ID: <cffbd5df-9782-4faa-80e2-0aaa01fdaf7f@foss.st.com> (raw)
In-Reply-To: <20261102-tprrt-pwm-stm32-32bit-timer-div-v2-1-352fe74a27d7@tupi.fr>
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
prev parent reply other threads:[~2026-11-26 7:39 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
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=cffbd5df-9782-4faa-80e2-0aaa01fdaf7f@foss.st.com \
--to=patrice.chotard@foss.st.com \
--cc=cheick.traore@foss.st.com \
--cc=fabrice.gasnier@foss.st.com \
--cc=patrick.delaunay@foss.st.com \
--cc=thomas.perrot@bootlin.com \
--cc=thomas.petazzoni@bootlin.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.u-boot-project.org \
--cc=uboot-stm32@st-md-mailman.stormreply.com \
/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 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.