]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/ddr/altera/sdram.c
ddr: altera: sdram: Clean up sdram_write_verify()
[karo-tx-uboot.git] / drivers / ddr / altera / sdram.c
index 1d9324a9ab776219597ce4c708e64835ec0668dd..fe2e753e5a4344bcee4947c47bfd819657bc4fbe 100644 (file)
@@ -4,6 +4,7 @@
  * SPDX-License-Identifier:    GPL-2.0+
  */
 #include <common.h>
+#include <errno.h>
 #include <div64.h>
 #include <watchdog.h>
 #include <asm/arch/fpga_manager.h>
 #include <asm/arch/system_manager.h>
 #include <asm/io.h>
 
-/*
- * FIXME: This path is temporary until the SDRAM driver gets
- *        a proper thorough cleanup.
- */
-#include "../../../board/altera/socfpga/qts/sdram_config.h"
-
 DECLARE_GLOBAL_DATA_PTR;
 
 struct sdram_prot_rule {
@@ -37,212 +32,6 @@ static struct socfpga_system_manager *sysmgr_regs =
 static struct socfpga_sdr_ctrl *sdr_ctrl =
        (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
 
-static struct socfpga_sdram_config {
-       u32     ctrl_cfg;
-       u32     dram_timing1;
-       u32     dram_timing2;
-       u32     dram_timing3;
-       u32     dram_timing4;
-       u32     lowpwr_timing;
-       u32     dram_odt;
-       u32     dram_addrw;
-       u32     dram_if_width;
-       u32     dram_dev_width;
-       u32     dram_intr;
-       u32     lowpwr_eq;
-       u32     static_cfg;
-       u32     ctrl_width;
-       u32     cport_width;
-       u32     cport_wmap;
-       u32     cport_rmap;
-       u32     rfifo_cmap;
-       u32     wfifo_cmap;
-       u32     cport_rdwr;
-       u32     port_cfg;
-       u32     fpgaport_rst;
-       u32     fifo_cfg;
-       u32     mp_priority;
-       u32     mp_weight0;
-       u32     mp_weight1;
-       u32     mp_weight2;
-       u32     mp_weight3;
-       u32     mp_pacing0;
-       u32     mp_pacing1;
-       u32     mp_pacing2;
-       u32     mp_pacing3;
-       u32     mp_threshold0;
-       u32     mp_threshold1;
-       u32     mp_threshold2;
-       u32     phy_ctrl0;
-} sdram_config = {
-       .ctrl_cfg =
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_MEMTYPE <<
-                       SDR_CTRLGRP_CTRLCFG_MEMTYPE_LSB)                |
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_MEMBL <<
-                       SDR_CTRLGRP_CTRLCFG_MEMBL_LSB)                  |
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER <<
-                       SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB)              |
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ECCEN <<
-                       SDR_CTRLGRP_CTRLCFG_ECCEN_LSB)                  |
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ECCCORREN <<
-                       SDR_CTRLGRP_CTRLCFG_ECCCORREN_LSB)              |
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_REORDEREN <<
-                       SDR_CTRLGRP_CTRLCFG_REORDEREN_LSB)              |
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_STARVELIMIT <<
-                       SDR_CTRLGRP_CTRLCFG_STARVELIMIT_LSB)            |
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_DQSTRKEN <<
-                       SDR_CTRLGRP_CTRLCFG_DQSTRKEN_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_NODMPINS <<
-                       SDR_CTRLGRP_CTRLCFG_NODMPINS_LSB),
-       .dram_timing1 =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TCWL <<
-                       SDR_CTRLGRP_DRAMTIMING1_TCWL_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_AL <<
-                       SDR_CTRLGRP_DRAMTIMING1_TAL_LSB)                |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TCL <<
-                       SDR_CTRLGRP_DRAMTIMING1_TCL_LSB)                |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TRRD <<
-                       SDR_CTRLGRP_DRAMTIMING1_TRRD_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TFAW <<
-                       SDR_CTRLGRP_DRAMTIMING1_TFAW_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TRFC <<
-                       SDR_CTRLGRP_DRAMTIMING1_TRFC_LSB),
-       .dram_timing2 =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TREFI <<
-                       SDR_CTRLGRP_DRAMTIMING2_TREFI_LSB)              |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TRCD <<
-                       SDR_CTRLGRP_DRAMTIMING2_TRCD_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TRP <<
-                       SDR_CTRLGRP_DRAMTIMING2_TRP_LSB)                |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TWR <<
-                       SDR_CTRLGRP_DRAMTIMING2_TWR_LSB)                |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TWTR <<
-                       SDR_CTRLGRP_DRAMTIMING2_TWTR_LSB),
-       .dram_timing3 =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRTP <<
-                       SDR_CTRLGRP_DRAMTIMING3_TRTP_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRAS <<
-                       SDR_CTRLGRP_DRAMTIMING3_TRAS_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRC <<
-                       SDR_CTRLGRP_DRAMTIMING3_TRC_LSB)                |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TMRD <<
-                       SDR_CTRLGRP_DRAMTIMING3_TMRD_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TCCD <<
-                       SDR_CTRLGRP_DRAMTIMING3_TCCD_LSB),
-       .dram_timing4 =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING4_SELFRFSHEXIT <<
-                       SDR_CTRLGRP_DRAMTIMING4_SELFRFSHEXIT_LSB)       |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING4_PWRDOWNEXIT <<
-                       SDR_CTRLGRP_DRAMTIMING4_PWRDOWNEXIT_LSB),
-       .lowpwr_timing =
-               (CONFIG_HPS_SDR_CTRLCFG_LOWPWRTIMING_AUTOPDCYCLES <<
-                       SDR_CTRLGRP_LOWPWRTIMING_AUTOPDCYCLES_LSB)      |
-               (CONFIG_HPS_SDR_CTRLCFG_LOWPWRTIMING_CLKDISABLECYCLES <<
-                       SDR_CTRLGRP_LOWPWRTIMING_CLKDISABLECYCLES_LSB),
-       .dram_odt =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMODT_READ <<
-                       SDR_CTRLGRP_DRAMODT_READ_LSB)                   |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMODT_WRITE <<
-                       SDR_CTRLGRP_DRAMODT_WRITE_LSB),
-       .dram_addrw =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS <<
-                       SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB)              |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS <<
-                       SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB)              |
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS <<
-                       SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB)             |
-               ((CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS - 1) <<
-                       SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB),
-       .dram_if_width =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMIFWIDTH_IFWIDTH <<
-                       SDR_CTRLGRP_DRAMIFWIDTH_IFWIDTH_LSB),
-       .dram_dev_width =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMDEVWIDTH_DEVWIDTH <<
-                       SDR_CTRLGRP_DRAMDEVWIDTH_DEVWIDTH_LSB),
-       .dram_intr =
-               (CONFIG_HPS_SDR_CTRLCFG_DRAMINTR_INTREN <<
-                       SDR_CTRLGRP_DRAMINTR_INTREN_LSB),
-       .lowpwr_eq =
-               (CONFIG_HPS_SDR_CTRLCFG_LOWPWREQ_SELFRFSHMASK <<
-                       SDR_CTRLGRP_LOWPWREQ_SELFRFSHMASK_LSB),
-       .static_cfg =
-               (CONFIG_HPS_SDR_CTRLCFG_STATICCFG_MEMBL <<
-                       SDR_CTRLGRP_STATICCFG_MEMBL_LSB)                |
-               (CONFIG_HPS_SDR_CTRLCFG_STATICCFG_USEECCASDATA <<
-                       SDR_CTRLGRP_STATICCFG_USEECCASDATA_LSB),
-       .ctrl_width =
-               (CONFIG_HPS_SDR_CTRLCFG_CTRLWIDTH_CTRLWIDTH <<
-                       SDR_CTRLGRP_CTRLWIDTH_CTRLWIDTH_LSB),
-       .cport_width =
-               (CONFIG_HPS_SDR_CTRLCFG_CPORTWIDTH_CPORTWIDTH <<
-                       SDR_CTRLGRP_CPORTWIDTH_CMDPORTWIDTH_LSB),
-       .cport_wmap =
-               (CONFIG_HPS_SDR_CTRLCFG_CPORTWMAP_CPORTWMAP <<
-                       SDR_CTRLGRP_CPORTWMAP_CPORTWFIFOMAP_LSB),
-       .cport_rmap =
-               (CONFIG_HPS_SDR_CTRLCFG_CPORTRMAP_CPORTRMAP <<
-                       SDR_CTRLGRP_CPORTRMAP_CPORTRFIFOMAP_LSB),
-       .rfifo_cmap =
-               (CONFIG_HPS_SDR_CTRLCFG_RFIFOCMAP_RFIFOCMAP <<
-                       SDR_CTRLGRP_RFIFOCMAP_RFIFOCPORTMAP_LSB),
-       .wfifo_cmap =
-               (CONFIG_HPS_SDR_CTRLCFG_WFIFOCMAP_WFIFOCMAP <<
-                       SDR_CTRLGRP_WFIFOCMAP_WFIFOCPORTMAP_LSB),
-       .cport_rdwr =
-               (CONFIG_HPS_SDR_CTRLCFG_CPORTRDWR_CPORTRDWR <<
-                       SDR_CTRLGRP_CPORTRDWR_CPORTRDWR_LSB),
-       .port_cfg =
-               (CONFIG_HPS_SDR_CTRLCFG_PORTCFG_AUTOPCHEN <<
-                       SDR_CTRLGRP_PORTCFG_AUTOPCHEN_LSB),
-       .fpgaport_rst = CONFIG_HPS_SDR_CTRLCFG_FPGAPORTRST,
-       .fifo_cfg =
-               (CONFIG_HPS_SDR_CTRLCFG_FIFOCFG_SYNCMODE <<
-                       SDR_CTRLGRP_FIFOCFG_SYNCMODE_LSB)               |
-               (CONFIG_HPS_SDR_CTRLCFG_FIFOCFG_INCSYNC <<
-                       SDR_CTRLGRP_FIFOCFG_INCSYNC_LSB),
-       .mp_priority =
-               (CONFIG_HPS_SDR_CTRLCFG_MPPRIORITY_USERPRIORITY <<
-                       SDR_CTRLGRP_MPPRIORITY_USERPRIORITY_LSB),
-       .mp_weight0 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_0_STATICWEIGHT_31_0 <<
-                       SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_0_STATICWEIGHT_31_0_LSB),
-       .mp_weight1 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_1_STATICWEIGHT_49_32 <<
-                       SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_STATICWEIGHT_49_32_LSB) |
-               (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_1_SUMOFWEIGHT_13_0 <<
-                       SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_SUMOFWEIGHTS_13_0_LSB),
-       .mp_weight2 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_2_SUMOFWEIGHT_45_14 <<
-                       SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_2_SUMOFWEIGHTS_45_14_LSB),
-       .mp_weight3 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_3_SUMOFWEIGHT_63_46 <<
-                       SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_3_SUMOFWEIGHTS_63_46_LSB),
-       .mp_pacing0 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPPACING_0_THRESHOLD1_31_0 <<
-                       SDR_CTRLGRP_MPPACING_MPPACING_0_THRESHOLD1_31_0_LSB),
-       .mp_pacing1 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPPACING_1_THRESHOLD1_59_32 <<
-                       SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD1_59_32_LSB) |
-               (CONFIG_HPS_SDR_CTRLCFG_MPPACING_1_THRESHOLD2_3_0 <<
-                       SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD2_3_0_LSB),
-       .mp_pacing2 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPPACING_2_THRESHOLD2_35_4 <<
-                       SDR_CTRLGRP_MPPACING_MPPACING_2_THRESHOLD2_35_4_LSB),
-       .mp_pacing3 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPPACING_3_THRESHOLD2_59_36 <<
-                       SDR_CTRLGRP_MPPACING_MPPACING_3_THRESHOLD2_59_36_LSB),
-       .mp_threshold0 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0 <<
-                       SDR_CTRLGRP_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0_LSB),
-       .mp_threshold1 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32 <<
-                       SDR_CTRLGRP_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32_LSB),
-       .mp_threshold2 =
-               (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64 <<
-                       SDR_CTRLGRP_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64_LSB),
-       .phy_ctrl0 = CONFIG_HPS_SDR_CTRLCFG_PHYCTRL_PHYCTRL_0,
-};
-
 /**
  * get_errata_rows() - Up the number of DRAM rows to cover entire address space
  * @cfg:       SDRAM controller configuration data
@@ -251,7 +40,7 @@ static struct socfpga_sdram_config {
  * increase the number of rows so that the memory controller thinks it has
  * 4GB of RAM. This function returns such amount of rows.
  */
-static int get_errata_rows(struct socfpga_sdram_config *cfg)
+static int get_errata_rows(const struct socfpga_sdram_config *cfg)
 {
        /* Define constant for 4G memory - used for SDRAM errata workaround */
 #define MEMSIZE_4G     (4ULL * 1024ULL * 1024ULL * 1024ULL)
@@ -442,32 +231,34 @@ static void sdram_dump_protection_config(void)
        }
 }
 
-/* Function to write to register and verify the write */
-static unsigned sdram_write_verify(unsigned int *addr, unsigned reg_value)
+/**
+ * sdram_write_verify() - write to register and verify the write.
+ * @addr:      Register address
+ * @val:       Value to be written and verified
+ *
+ * This function writes to a register, reads back the value and compares
+ * the result with the written value to check if the data match.
+ */
+static unsigned sdram_write_verify(const u32 *addr, const u32 val)
 {
-#ifndef SDRAM_MMR_SKIP_VERIFY
-       unsigned reg_value1;
-#endif
-       debug("   Write - Address ");
-       debug("0x%08x Data 0x%08x\n", (u32)addr, reg_value);
-       /* Write to register */
-       writel(reg_value, addr);
-#ifndef SDRAM_MMR_SKIP_VERIFY
+       u32 rval;
+
+       debug("   Write - Address 0x%p Data 0x%08x\n", addr, val);
+       writel(val, addr);
+
        debug("   Read and verify...");
-       /* Read back the wrote value */
-       reg_value1 = readl(addr);
-       /* Indicate failure if value not matched */
-       if (reg_value1 != reg_value) {
-               debug("FAIL - Address 0x%08x Expected 0x%08x Data 0x%08x\n",
-                     (u32)addr, reg_value, reg_value1);
-               return 1;
+       rval = readl(addr);
+       if (rval != val) {
+               debug("FAIL - Address 0x%p Expected 0x%08x Data 0x%08x\n",
+                     addr, val, rval);
+               return -EINVAL;
        }
+
        debug("correct!\n");
-#endif /* SDRAM_MMR_SKIP_VERIFY */
        return 0;
 }
 
-static u32 sdr_get_ctrlcfg(struct socfpga_sdram_config *cfg)
+static u32 sdr_get_ctrlcfg(const struct socfpga_sdram_config *cfg)
 {
        const u32 csbits =
                ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
@@ -499,7 +290,7 @@ static u32 sdr_get_ctrlcfg(struct socfpga_sdram_config *cfg)
        return ctrl_cfg;
 }
 
-static u32 sdr_get_addr_rw(struct socfpga_sdram_config *cfg)
+static u32 sdr_get_addr_rw(const struct socfpga_sdram_config *cfg)
 {
        /*
         * SDRAM Failure When Accessing Non-Existent Memory
@@ -514,20 +305,17 @@ static u32 sdr_get_addr_rw(struct socfpga_sdram_config *cfg)
        return dram_addrw | (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB);
 }
 
-/* Function to initialize SDRAM MMR */
-unsigned sdram_mmr_init_full(unsigned int sdr_phy_reg)
+/**
+ * sdr_load_regs() - Load SDRAM controller registers
+ * @cfg:       SDRAM controller configuration data
+ *
+ * This function loads the register values into the SDRAM controller block.
+ */
+static void sdr_load_regs(const struct socfpga_sdram_config *cfg)
 {
-       unsigned long status = 0;
-       struct socfpga_sdram_config *cfg = &sdram_config;
-       const unsigned int rows =
-               (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
-                       SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
-
        const u32 ctrl_cfg = sdr_get_ctrlcfg(cfg);
        const u32 dram_addrw = sdr_get_addr_rw(cfg);
 
-       writel(rows, &sysmgr_regs->iswgrp_handoff[4]);
-
        debug("\nConfiguring CTRLCFG\n");
        writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
 
@@ -616,17 +404,35 @@ unsigned sdram_mmr_init_full(unsigned int sdr_phy_reg)
 
        debug("Configuring DRAMODT\n");
        writel(cfg->dram_odt, &sdr_ctrl->dram_odt);
+}
+
+/**
+ * sdram_mmr_init_full() - Function to initialize SDRAM MMR
+ * @sdr_phy_reg:       Value of the PHY control register 0
+ *
+ * Initialize the SDRAM MMR.
+ */
+int sdram_mmr_init_full(unsigned int sdr_phy_reg)
+{
+       const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
+       const unsigned int rows =
+               (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
+                       SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
+       int ret;
+
+       writel(rows, &sysmgr_regs->iswgrp_handoff[4]);
+
+       sdr_load_regs(cfg);
 
        /* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */
        writel(cfg->fpgaport_rst, &sysmgr_regs->iswgrp_handoff[3]);
 
        /* only enable if the FPGA is programmed */
        if (fpgamgr_test_fpga_ready()) {
-               if (sdram_write_verify(&sdr_ctrl->fpgaport_rst,
-                   cfg->fpgaport_rst) == 1) {
-                       status = 1;
-                       return 1;
-               }
+               ret = sdram_write_verify(&sdr_ctrl->fpgaport_rst,
+                                        cfg->fpgaport_rst);
+               if (ret)
+                       return ret;
        }
 
        /* Restore the SDR PHY Register if valid */
@@ -643,67 +449,63 @@ unsigned sdram_mmr_init_full(unsigned int sdr_phy_reg)
 
        sdram_dump_protection_config();
 
-       return status;
+       return 0;
 }
 
-/*
- * To calculate SDRAM device size based on SDRAM controller parameters.
- * Size is specified in bytes.
+/**
+ * sdram_calculate_size() - Calculate SDRAM size
  *
- * NOTE:
- * This function is compiled and linked into the preloader and
- * Uboot (there may be others). So if this function changes, the Preloader
- * and UBoot must be updated simultaneously.
+ * Calculate SDRAM device size based on SDRAM controller parameters.
+ * Size is specified in bytes.
  */
 unsigned long sdram_calculate_size(void)
 {
        unsigned long temp;
        unsigned long row, bank, col, cs, width;
+       const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
+       const unsigned int csbits =
+               ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
+                       SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
+       const unsigned int rowbits =
+               (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
+                       SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
 
        temp = readl(&sdr_ctrl->dram_addrw);
        col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
                SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
 
-       /* SDRAM Failure When Accessing Non-Existent Memory
+       /*
+        * SDRAM Failure When Accessing Non-Existent Memory
         * Use ROWBITS from Quartus/QSys to calculate SDRAM size
         * since the FB specifies we modify ROWBITs to work around SDRAM
         * controller issue.
-        *
-        * If the stored handoff value for rows is 0, it probably means
-        * the preloader is older than UBoot. Use the
-        * #define from the SOCEDS Tools per Crucible review
-        * uboot-socfpga-204. Note that this is not a supported
-        * configuration and is not tested. The customer
-        * should be using preloader and uboot built from the
-        * same tag.
         */
        row = readl(&sysmgr_regs->iswgrp_handoff[4]);
        if (row == 0)
-               row = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
-       /* If the stored handoff value for rows is greater than
+               row = rowbits;
+       /*
+        * If the stored handoff value for rows is greater than
         * the field width in the sdr.dramaddrw register then
         * something is very wrong. Revert to using the the #define
         * value handed off by the SOCEDS tool chain instead of
         * using a broken value.
         */
        if (row > 31)
-               row = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
+               row = rowbits;
 
        bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
                SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
 
-       /* SDRAM Failure When Accessing Non-Existent Memory
+       /*
+        * SDRAM Failure When Accessing Non-Existent Memory
         * Use CSBITs from Quartus/QSys to calculate SDRAM size
         * since the FB specifies we modify CSBITs to work around SDRAM
         * controller issue.
         */
-       cs = (temp & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
-             SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB;
-       cs += 1;
-
-       cs = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS;
+       cs = csbits;
 
        width = readl(&sdr_ctrl->dram_if_width);
+
        /* ECC would not be calculated as its not addressible */
        if (width == SDRAM_WIDTH_32BIT_WITH_ECC)
                width = 32;
@@ -714,7 +516,7 @@ unsigned long sdram_calculate_size(void)
        temp = 1 << (row + bank + col);
        temp = temp * cs * (width  / 8);
 
-       debug("sdram_calculate_memory returns %ld\n", temp);
+       debug("%s returns %ld\n", __func__, temp);
 
        return temp;
 }