[PATCH v3] mmc: mtk-sd: Improve data type in msdc_timeout_cal()

Thorsten Blum posted 1 patch 1 month ago
drivers/mmc/host/mtk-sd.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
[PATCH v3] mmc: mtk-sd: Improve data type in msdc_timeout_cal()
Posted by Thorsten Blum 1 month ago
The local variable clk_ns uses at most 32 bits and can be a u32.

Replace the 64-by-32 do_div() division with a standard divison.

Since do_div() casts the divisor to u32 anyway, changing the data type
of clk_ns to u32 also removes the following Coccinelle/coccicheck
warning reported by do_div.cocci:

  WARNING: do_div() does a 64-by-32 division, please consider using div64_u64 instead

Use min_t(u32,,) to simplify the code and improve its readability.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
Changes in v2:
- Address kernel test robot feedback and replace do_div() with a
 standard division
- Link to v1: https://lore.kernel.org/linux-kernel/20240815234602.59684-1-thorsten.blum@toblux.com/

Changes in v3:
- Use min_t() instead of max_t()
- Link to v2: https://lore.kernel.org/linux-kernel/20240817170726.350339-2-thorsten.blum@toblux.com/
---
 drivers/mmc/host/mtk-sd.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
index a94835b8ab93..edeab9a4a83b 100644
--- a/drivers/mmc/host/mtk-sd.c
+++ b/drivers/mmc/host/mtk-sd.c
@@ -795,14 +795,13 @@ static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
 static u64 msdc_timeout_cal(struct msdc_host *host, u64 ns, u64 clks)
 {
 	struct mmc_host *mmc = mmc_from_priv(host);
-	u64 timeout, clk_ns;
-	u32 mode = 0;
+	u64 timeout;
+	u32 clk_ns, mode = 0;
 
 	if (mmc->actual_clock == 0) {
 		timeout = 0;
 	} else {
-		clk_ns  = 1000000000ULL;
-		do_div(clk_ns, mmc->actual_clock);
+		clk_ns = 1000000000U / mmc->actual_clock;
 		timeout = ns + clk_ns - 1;
 		do_div(timeout, clk_ns);
 		timeout += clks;
@@ -831,7 +830,7 @@ static void msdc_set_timeout(struct msdc_host *host, u64 ns, u64 clks)
 
 	timeout = msdc_timeout_cal(host, ns, clks);
 	sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC,
-		      (u32)(timeout > 255 ? 255 : timeout));
+		      min_t(u32, timeout, 255));
 }
 
 static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks)
@@ -840,7 +839,7 @@ static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks)
 
 	timeout = msdc_timeout_cal(host, ns, clks);
 	sdr_set_field(host->base + SDC_CFG, SDC_CFG_WRDTOC,
-		      (u32)(timeout > 8191 ? 8191 : timeout));
+		      min_t(u32, timeout, 8191));
 }
 
 static void msdc_gate_clock(struct msdc_host *host)
-- 
2.46.0
Re: [PATCH v3] mmc: mtk-sd: Improve data type in msdc_timeout_cal()
Posted by Ulf Hansson 4 weeks, 1 day ago
On Sun, 18 Aug 2024 at 16:23, Thorsten Blum <thorsten.blum@toblux.com> wrote:
>
> The local variable clk_ns uses at most 32 bits and can be a u32.
>
> Replace the 64-by-32 do_div() division with a standard divison.
>
> Since do_div() casts the divisor to u32 anyway, changing the data type
> of clk_ns to u32 also removes the following Coccinelle/coccicheck
> warning reported by do_div.cocci:
>
>   WARNING: do_div() does a 64-by-32 division, please consider using div64_u64 instead
>
> Use min_t(u32,,) to simplify the code and improve its readability.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>

Applied for next, thanks!

Kind regards
Uffe


> ---
> Changes in v2:
> - Address kernel test robot feedback and replace do_div() with a
>  standard division
> - Link to v1: https://lore.kernel.org/linux-kernel/20240815234602.59684-1-thorsten.blum@toblux.com/
>
> Changes in v3:
> - Use min_t() instead of max_t()
> - Link to v2: https://lore.kernel.org/linux-kernel/20240817170726.350339-2-thorsten.blum@toblux.com/
> ---
>  drivers/mmc/host/mtk-sd.c | 11 +++++------
>  1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> index a94835b8ab93..edeab9a4a83b 100644
> --- a/drivers/mmc/host/mtk-sd.c
> +++ b/drivers/mmc/host/mtk-sd.c
> @@ -795,14 +795,13 @@ static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
>  static u64 msdc_timeout_cal(struct msdc_host *host, u64 ns, u64 clks)
>  {
>         struct mmc_host *mmc = mmc_from_priv(host);
> -       u64 timeout, clk_ns;
> -       u32 mode = 0;
> +       u64 timeout;
> +       u32 clk_ns, mode = 0;
>
>         if (mmc->actual_clock == 0) {
>                 timeout = 0;
>         } else {
> -               clk_ns  = 1000000000ULL;
> -               do_div(clk_ns, mmc->actual_clock);
> +               clk_ns = 1000000000U / mmc->actual_clock;
>                 timeout = ns + clk_ns - 1;
>                 do_div(timeout, clk_ns);
>                 timeout += clks;
> @@ -831,7 +830,7 @@ static void msdc_set_timeout(struct msdc_host *host, u64 ns, u64 clks)
>
>         timeout = msdc_timeout_cal(host, ns, clks);
>         sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC,
> -                     (u32)(timeout > 255 ? 255 : timeout));
> +                     min_t(u32, timeout, 255));
>  }
>
>  static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks)
> @@ -840,7 +839,7 @@ static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks)
>
>         timeout = msdc_timeout_cal(host, ns, clks);
>         sdr_set_field(host->base + SDC_CFG, SDC_CFG_WRDTOC,
> -                     (u32)(timeout > 8191 ? 8191 : timeout));
> +                     min_t(u32, timeout, 8191));
>  }
>
>  static void msdc_gate_clock(struct msdc_host *host)
> --
> 2.46.0
>