]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
drivers: ddr: util.c: Fix divide by zero issue
authorPriyanka Singh <priyanka.singh@nxp.com>
Thu, 19 Aug 2021 06:09:01 +0000 (11:39 +0530)
committerPriyanka Jain <priyanka.jain@nxp.com>
Tue, 9 Nov 2021 09:13:24 +0000 (14:43 +0530)
Fix possible divide by zero issue in get_memory_clk_period_ps
by adding a check

Signed-off-by: Priyanka Singh <priyanka.singh@nxp.com>
Reviewed-by: Priyanka Jain <priyanka.jain@nxp.com>
drivers/ddr/fsl/util.c

index ac4f8d2732d770334e250aa86dc5d866ec80efed..43cb01804b71485f165fdfa641b633d9204aa48c 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright 2008-2014 Freescale Semiconductor, Inc.
+ * Copyright 2021 NXP
  */
 
 #include <common.h>
@@ -75,10 +76,13 @@ unsigned int get_memory_clk_period_ps(const unsigned int ctrl_num)
 
        /* Round to nearest 10ps, being careful about 64-bit multiply/divide */
        unsigned long long rem, mclk_ps = ULL_2E12;
-
-       /* Now perform the big divide, the result fits in 32-bits */
-       rem = do_div(mclk_ps, data_rate);
-       result = (rem >= (data_rate >> 1)) ? mclk_ps + 1 : mclk_ps;
+       if (data_rate) {
+               /* Now perform the big divide, the result fits in 32-bits */
+               rem = do_div(mclk_ps, data_rate);
+               result = (rem >= (data_rate >> 1)) ? mclk_ps + 1 : mclk_ps;
+       } else {
+               result = 0;
+       }
 
        return result;
 }