diff mbox

[U-Boot] bugfix i.mx6 pwm: prevent overflow of period_c * duty_ns by casting duty_ns to ull first. This bug came up when trying to create a 200 Hz PWM.

Message ID 1430136666-18626-1-git-send-email-bnrn@psicontrol.com
State Changes Requested
Delegated to: Stefano Babic
Headers show

Commit Message

Brecht Neyrinck April 27, 2015, 12:11 p.m. UTC
---
 drivers/pwm/pwm-imx-util.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
 mode change 100644 => 100755 drivers/pwm/pwm-imx-util.c

Comments

Heiko Schocher April 28, 2015, 5:32 a.m. UTC | #1
Hello Brecht Neyrinck,

Am 27.04.2015 14:11, schrieb Brecht Neyrinck:
> ---
>   drivers/pwm/pwm-imx-util.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>   mode change 100644 => 100755 drivers/pwm/pwm-imx-util.c
>
> diff --git a/drivers/pwm/pwm-imx-util.c b/drivers/pwm/pwm-imx-util.c
> index f1d0b35..777a8bf 100644
> --- a/drivers/pwm/pwm-imx-util.c
> +++ b/drivers/pwm/pwm-imx-util.c
> @@ -56,7 +56,7 @@ int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c,
>   	*prescale = *period_c / 0x10000 + 1;
>
>   	*period_c /= *prescale;
> -	c = (unsigned long long)(*period_c * duty_ns);
> +	c = *period_c * (unsigned long long) duty_ns;

Thanks for this ... Hmm... this code is directly from linux
drivers/pwm/pwm-imx.c ... Do you have running a linux on your hw?
Could you verify this in linux too?

Thanks!

Acked-by: Heiko Schocher <hs@denx.de>

bye,
Heiko
>   	do_div(c, period_ns);
>   	*duty_c = c;
>
>
NEYRINCK Brecht May 5, 2015, 8:40 a.m. UTC | #2
Hi Heiko,

We didn't need the 200 Hz PWM in Linux, so I didn't test it right away.
I did now, and the problem isn't present there, which I found weird given the fact that it is indeed similar code. So I dug into the kernel:
The devil is in the details. :)

Kernel: 
c = (unsigned long long)period_cycles * duty_ns;

U-boot:
c = (unsigned long long)(*period_c * duty_ns);

The braces cause the casting to be performed after the (truncated) multiplication, while without braces, the casting is done upfront.

Thank you for your reply and giving attention to this small bug.

Kind regards,
Brecht

-----Original Message-----
From: Heiko Schocher [mailto:hs@denx.de] 
Sent: dinsdag 28 april 2015 7:32
To: NEYRINCK Brecht
Cc: u-boot@lists.denx.de
Subject: Re: [U-Boot] [PATCH] bugfix i.mx6 pwm: prevent overflow of period_c * duty_ns by casting duty_ns to ull first. This bug came up when trying to create a 200 Hz PWM.

Hello Brecht Neyrinck,

Am 27.04.2015 14:11, schrieb Brecht Neyrinck:
> ---
>   drivers/pwm/pwm-imx-util.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>   mode change 100644 => 100755 drivers/pwm/pwm-imx-util.c
>
> diff --git a/drivers/pwm/pwm-imx-util.c b/drivers/pwm/pwm-imx-util.c 
> index f1d0b35..777a8bf 100644
> --- a/drivers/pwm/pwm-imx-util.c
> +++ b/drivers/pwm/pwm-imx-util.c
> @@ -56,7 +56,7 @@ int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c,
>   	*prescale = *period_c / 0x10000 + 1;
>
>   	*period_c /= *prescale;
> -	c = (unsigned long long)(*period_c * duty_ns);
> +	c = *period_c * (unsigned long long) duty_ns;

Thanks for this ... Hmm... this code is directly from linux drivers/pwm/pwm-imx.c ... Do you have running a linux on your hw?
Could you verify this in linux too?

Thanks!

Acked-by: Heiko Schocher <hs@denx.de>

bye,
Heiko
>   	do_div(c, period_ns);
>   	*duty_c = c;
>
>
Fabio Estevam May 5, 2015, 2:15 p.m. UTC | #3
Hi Brecht,

On Mon, Apr 27, 2015 at 9:11 AM, Brecht Neyrinck <bnrn@psicontrol.com> wrote:
> ---
>  drivers/pwm/pwm-imx-util.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>  mode change 100644 => 100755 drivers/pwm/pwm-imx-util.c
>
> diff --git a/drivers/pwm/pwm-imx-util.c b/drivers/pwm/pwm-imx-util.c
> index f1d0b35..777a8bf 100644
> --- a/drivers/pwm/pwm-imx-util.c
> +++ b/drivers/pwm/pwm-imx-util.c
> @@ -56,7 +56,7 @@ int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c,
>         *prescale = *period_c / 0x10000 + 1;
>
>         *period_c /= *prescale;
> -       c = (unsigned long long)(*period_c * duty_ns);
> +       c = *period_c * (unsigned long long) duty_ns;
>         do_div(c, period_ns);
>         *duty_c = c;

Your fix looks good, but your patch misses a Signed-off-by.

Also the commit log seems to be mixed with the subject.

Please check:
http://www.denx.de/wiki/U-Boot/Patches

You can also send it to the list using git send-email.

Regards,

Fabio Estevam
diff mbox

Patch

diff --git a/drivers/pwm/pwm-imx-util.c b/drivers/pwm/pwm-imx-util.c
index f1d0b35..777a8bf 100644
--- a/drivers/pwm/pwm-imx-util.c
+++ b/drivers/pwm/pwm-imx-util.c
@@ -56,7 +56,7 @@  int pwm_imx_get_parms(int period_ns, int duty_ns, unsigned long *period_c,
 	*prescale = *period_c / 0x10000 + 1;
 
 	*period_c /= *prescale;
-	c = (unsigned long long)(*period_c * duty_ns);
+	c = *period_c * (unsigned long long) duty_ns;
 	do_div(c, period_ns);
 	*duty_c = c;