[PATCH] ddr: socfpga: Fix integer overflow that caused DDR size mismatched

tien.fong.chee at intel.com tien.fong.chee at intel.com
Wed Aug 5 05:58:40 CEST 2020


From: Tien Fong Chee <tien.fong.chee at intel.com>

Convert the constant integer to 'phys_size_t' to avoid overflow
when calculating the SDRAM size.

Signed-off-by: Tien Fong Chee <tien.fong.chee at intel.com>
---
 drivers/ddr/altera/sdram_soc64.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/ddr/altera/sdram_soc64.c b/drivers/ddr/altera/sdram_soc64.c
index 8cb75fe02a..a862ac8092 100644
--- a/drivers/ddr/altera/sdram_soc64.c
+++ b/drivers/ddr/altera/sdram_soc64.c
@@ -218,13 +218,14 @@ phys_size_t sdram_calculate_size(struct altera_sdram_platdata *plat)
 {
 	u32 dramaddrw = hmc_readl(plat, DRAMADDRW);
 
-	phys_size_t size = 1 << (DRAMADDRW_CFG_CS_ADDR_WIDTH(dramaddrw) +
-			 DRAMADDRW_CFG_BANK_GRP_ADDR_WIDTH(dramaddrw) +
-			 DRAMADDRW_CFG_BANK_ADDR_WIDTH(dramaddrw) +
-			 DRAMADDRW_CFG_ROW_ADDR_WIDTH(dramaddrw) +
-			 DRAMADDRW_CFG_COL_ADDR_WIDTH(dramaddrw));
-
-	size *= (2 << (hmc_ecc_readl(plat, DDRIOCTRL) &
+	phys_size_t size = (phys_size_t)1 <<
+			   (DRAMADDRW_CFG_CS_ADDR_WIDTH(dramaddrw) +
+			    DRAMADDRW_CFG_BANK_GRP_ADDR_WIDTH(dramaddrw) +
+			    DRAMADDRW_CFG_BANK_ADDR_WIDTH(dramaddrw) +
+			    DRAMADDRW_CFG_ROW_ADDR_WIDTH(dramaddrw) +
+			    DRAMADDRW_CFG_COL_ADDR_WIDTH(dramaddrw));
+
+	size *= ((phys_size_t)2 << (hmc_ecc_readl(plat, DDRIOCTRL) &
 			DDR_HMC_DDRIOCTRL_IOSIZE_MSK));
 
 	return size;
-- 
2.13.0



More information about the U-Boot mailing list