Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright Altera Corporation (C) 2014-2015 |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 4 | */ |
| 5 | #include <common.h> |
Marek Vasut | 1b1cc10 | 2015-08-01 22:25:29 +0200 | [diff] [blame] | 6 | #include <errno.h> |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 7 | #include <div64.h> |
| 8 | #include <watchdog.h> |
| 9 | #include <asm/arch/fpga_manager.h> |
| 10 | #include <asm/arch/sdram.h> |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 11 | #include <asm/arch/system_manager.h> |
| 12 | #include <asm/io.h> |
| 13 | |
Marek Vasut | e08c559 | 2015-07-26 10:37:54 +0200 | [diff] [blame] | 14 | struct sdram_prot_rule { |
Marek Vasut | 6772cd9 | 2015-08-01 23:12:11 +0200 | [diff] [blame] | 15 | u32 sdram_start; /* SDRAM start address */ |
| 16 | u32 sdram_end; /* SDRAM end address */ |
Marek Vasut | e08c559 | 2015-07-26 10:37:54 +0200 | [diff] [blame] | 17 | u32 rule; /* SDRAM protection rule number: 0-19 */ |
| 18 | int valid; /* Rule valid or not? 1 - valid, 0 not*/ |
| 19 | |
| 20 | u32 security; |
| 21 | u32 portmask; |
| 22 | u32 result; |
| 23 | u32 lo_prot_id; |
| 24 | u32 hi_prot_id; |
| 25 | }; |
| 26 | |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 27 | static struct socfpga_system_manager *sysmgr_regs = |
| 28 | (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS; |
| 29 | static struct socfpga_sdr_ctrl *sdr_ctrl = |
Marek Vasut | 33acf0f | 2015-07-12 20:05:54 +0200 | [diff] [blame] | 30 | (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 31 | |
Marek Vasut | 724c50f | 2015-08-01 19:20:19 +0200 | [diff] [blame] | 32 | /** |
| 33 | * get_errata_rows() - Up the number of DRAM rows to cover entire address space |
Marek Vasut | 3a07911 | 2015-08-01 21:16:20 +0200 | [diff] [blame] | 34 | * @cfg: SDRAM controller configuration data |
Marek Vasut | 724c50f | 2015-08-01 19:20:19 +0200 | [diff] [blame] | 35 | * |
| 36 | * SDRAM Failure happens when accessing non-existent memory. Artificially |
| 37 | * increase the number of rows so that the memory controller thinks it has |
| 38 | * 4GB of RAM. This function returns such amount of rows. |
| 39 | */ |
Marek Vasut | 32ada57 | 2015-08-01 21:35:18 +0200 | [diff] [blame] | 40 | static int get_errata_rows(const struct socfpga_sdram_config *cfg) |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 41 | { |
Marek Vasut | 724c50f | 2015-08-01 19:20:19 +0200 | [diff] [blame] | 42 | /* Define constant for 4G memory - used for SDRAM errata workaround */ |
| 43 | #define MEMSIZE_4G (4ULL * 1024ULL * 1024ULL * 1024ULL) |
| 44 | const unsigned long long memsize = MEMSIZE_4G; |
Marek Vasut | 3a07911 | 2015-08-01 21:16:20 +0200 | [diff] [blame] | 45 | const unsigned int cs = |
| 46 | ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >> |
| 47 | SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1; |
| 48 | const unsigned int rows = |
| 49 | (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >> |
| 50 | SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB; |
| 51 | const unsigned int banks = |
| 52 | (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >> |
| 53 | SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB; |
| 54 | const unsigned int cols = |
| 55 | (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >> |
| 56 | SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB; |
Marek Vasut | 724c50f | 2015-08-01 19:20:19 +0200 | [diff] [blame] | 57 | const unsigned int width = 8; |
| 58 | |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 59 | unsigned long long newrows; |
Marek Vasut | 724c50f | 2015-08-01 19:20:19 +0200 | [diff] [blame] | 60 | int bits, inewrowslog2; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 61 | |
| 62 | debug("workaround rows - memsize %lld\n", memsize); |
| 63 | debug("workaround rows - cs %d\n", cs); |
| 64 | debug("workaround rows - width %d\n", width); |
| 65 | debug("workaround rows - rows %d\n", rows); |
| 66 | debug("workaround rows - banks %d\n", banks); |
| 67 | debug("workaround rows - cols %d\n", cols); |
| 68 | |
Marek Vasut | 186880e | 2015-08-01 18:54:34 +0200 | [diff] [blame] | 69 | newrows = lldiv(memsize, cs * (width / 8)); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 70 | debug("rows workaround - term1 %lld\n", newrows); |
| 71 | |
Marek Vasut | 186880e | 2015-08-01 18:54:34 +0200 | [diff] [blame] | 72 | newrows = lldiv(newrows, (1 << banks) * (1 << cols)); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 73 | debug("rows workaround - term2 %lld\n", newrows); |
| 74 | |
Marek Vasut | 186880e | 2015-08-01 18:54:34 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Compute the hamming weight - same as number of bits set. |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 77 | * Need to see if result is ordinal power of 2 before |
| 78 | * attempting log2 of result. |
| 79 | */ |
Marek Vasut | 2fda506 | 2015-08-01 18:46:55 +0200 | [diff] [blame] | 80 | bits = generic_hweight32(newrows); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 81 | |
| 82 | debug("rows workaround - bits %d\n", bits); |
| 83 | |
| 84 | if (bits != 1) { |
| 85 | printf("SDRAM workaround failed, bits set %d\n", bits); |
| 86 | return rows; |
| 87 | } |
| 88 | |
| 89 | if (newrows > UINT_MAX) { |
| 90 | printf("SDRAM workaround rangecheck failed, %lld\n", newrows); |
| 91 | return rows; |
| 92 | } |
| 93 | |
Marek Vasut | 186880e | 2015-08-01 18:54:34 +0200 | [diff] [blame] | 94 | inewrowslog2 = __ilog2(newrows); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 95 | |
Marek Vasut | 186880e | 2015-08-01 18:54:34 +0200 | [diff] [blame] | 96 | debug("rows workaround - ilog2 %d, %lld\n", inewrowslog2, newrows); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 97 | |
| 98 | if (inewrowslog2 == -1) { |
Marek Vasut | 186880e | 2015-08-01 18:54:34 +0200 | [diff] [blame] | 99 | printf("SDRAM workaround failed, newrows %lld\n", newrows); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 100 | return rows; |
| 101 | } |
| 102 | |
| 103 | return inewrowslog2; |
| 104 | } |
| 105 | |
| 106 | /* SDRAM protection rules vary from 0-19, a total of 20 rules. */ |
| 107 | static void sdram_set_rule(struct sdram_prot_rule *prule) |
| 108 | { |
Marek Vasut | 6772cd9 | 2015-08-01 23:12:11 +0200 | [diff] [blame] | 109 | u32 lo_addr_bits; |
| 110 | u32 hi_addr_bits; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 111 | int ruleno = prule->rule; |
| 112 | |
| 113 | /* Select the rule */ |
| 114 | writel(ruleno, &sdr_ctrl->prot_rule_rdwr); |
| 115 | |
| 116 | /* Obtain the address bits */ |
Marek Vasut | 7fce5bc | 2015-08-01 22:40:48 +0200 | [diff] [blame] | 117 | lo_addr_bits = prule->sdram_start >> 20ULL; |
Marek Vasut | 12361a2 | 2016-04-04 17:52:21 +0200 | [diff] [blame] | 118 | hi_addr_bits = (prule->sdram_end - 1) >> 20ULL; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 119 | |
Marek Vasut | 6772cd9 | 2015-08-01 23:12:11 +0200 | [diff] [blame] | 120 | debug("sdram set rule start %x, %d\n", lo_addr_bits, |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 121 | prule->sdram_start); |
Marek Vasut | 6772cd9 | 2015-08-01 23:12:11 +0200 | [diff] [blame] | 122 | debug("sdram set rule end %x, %d\n", hi_addr_bits, |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 123 | prule->sdram_end); |
| 124 | |
| 125 | /* Set rule addresses */ |
| 126 | writel(lo_addr_bits | (hi_addr_bits << 12), &sdr_ctrl->prot_rule_addr); |
| 127 | |
| 128 | /* Set rule protection ids */ |
| 129 | writel(prule->lo_prot_id | (prule->hi_prot_id << 12), |
| 130 | &sdr_ctrl->prot_rule_id); |
| 131 | |
| 132 | /* Set the rule data */ |
| 133 | writel(prule->security | (prule->valid << 2) | |
| 134 | (prule->portmask << 3) | (prule->result << 13), |
| 135 | &sdr_ctrl->prot_rule_data); |
| 136 | |
| 137 | /* write the rule */ |
Marek Vasut | 7fce5bc | 2015-08-01 22:40:48 +0200 | [diff] [blame] | 138 | writel(ruleno | (1 << 5), &sdr_ctrl->prot_rule_rdwr); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 139 | |
| 140 | /* Set rule number to 0 by default */ |
| 141 | writel(0, &sdr_ctrl->prot_rule_rdwr); |
| 142 | } |
| 143 | |
| 144 | static void sdram_get_rule(struct sdram_prot_rule *prule) |
| 145 | { |
Marek Vasut | 9114407 | 2015-08-01 23:21:23 +0200 | [diff] [blame] | 146 | u32 addr; |
| 147 | u32 id; |
| 148 | u32 data; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 149 | int ruleno = prule->rule; |
| 150 | |
| 151 | /* Read the rule */ |
| 152 | writel(ruleno, &sdr_ctrl->prot_rule_rdwr); |
Marek Vasut | 9114407 | 2015-08-01 23:21:23 +0200 | [diff] [blame] | 153 | writel(ruleno | (1 << 6), &sdr_ctrl->prot_rule_rdwr); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 154 | |
| 155 | /* Get the addresses */ |
| 156 | addr = readl(&sdr_ctrl->prot_rule_addr); |
| 157 | prule->sdram_start = (addr & 0xFFF) << 20; |
| 158 | prule->sdram_end = ((addr >> 12) & 0xFFF) << 20; |
| 159 | |
| 160 | /* Get the configured protection IDs */ |
| 161 | id = readl(&sdr_ctrl->prot_rule_id); |
| 162 | prule->lo_prot_id = id & 0xFFF; |
| 163 | prule->hi_prot_id = (id >> 12) & 0xFFF; |
| 164 | |
| 165 | /* Get protection data */ |
| 166 | data = readl(&sdr_ctrl->prot_rule_data); |
| 167 | |
| 168 | prule->security = data & 0x3; |
| 169 | prule->valid = (data >> 2) & 0x1; |
| 170 | prule->portmask = (data >> 3) & 0x3FF; |
| 171 | prule->result = (data >> 13) & 0x1; |
| 172 | } |
| 173 | |
Marek Vasut | 6772cd9 | 2015-08-01 23:12:11 +0200 | [diff] [blame] | 174 | static void |
| 175 | sdram_set_protection_config(const u32 sdram_start, const u32 sdram_end) |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 176 | { |
| 177 | struct sdram_prot_rule rule; |
| 178 | int rules; |
| 179 | |
| 180 | /* Start with accepting all SDRAM transaction */ |
| 181 | writel(0x0, &sdr_ctrl->protport_default); |
| 182 | |
| 183 | /* Clear all protection rules for warm boot case */ |
Marek Vasut | 7fce5bc | 2015-08-01 22:40:48 +0200 | [diff] [blame] | 184 | memset(&rule, 0, sizeof(rule)); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 185 | |
| 186 | for (rules = 0; rules < 20; rules++) { |
| 187 | rule.rule = rules; |
| 188 | sdram_set_rule(&rule); |
| 189 | } |
| 190 | |
| 191 | /* new rule: accept SDRAM */ |
| 192 | rule.sdram_start = sdram_start; |
| 193 | rule.sdram_end = sdram_end; |
| 194 | rule.lo_prot_id = 0x0; |
| 195 | rule.hi_prot_id = 0xFFF; |
| 196 | rule.portmask = 0x3FF; |
| 197 | rule.security = 0x3; |
| 198 | rule.result = 0; |
| 199 | rule.valid = 1; |
| 200 | rule.rule = 0; |
| 201 | |
| 202 | /* set new rule */ |
| 203 | sdram_set_rule(&rule); |
| 204 | |
| 205 | /* default rule: reject everything */ |
| 206 | writel(0x3ff, &sdr_ctrl->protport_default); |
| 207 | } |
| 208 | |
| 209 | static void sdram_dump_protection_config(void) |
| 210 | { |
| 211 | struct sdram_prot_rule rule; |
| 212 | int rules; |
| 213 | |
| 214 | debug("SDRAM Prot rule, default %x\n", |
| 215 | readl(&sdr_ctrl->protport_default)); |
| 216 | |
| 217 | for (rules = 0; rules < 20; rules++) { |
Marek Vasut | 42aa46d | 2015-12-29 09:38:52 +0100 | [diff] [blame] | 218 | rule.rule = rules; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 219 | sdram_get_rule(&rule); |
| 220 | debug("Rule %d, rules ...\n", rules); |
Marek Vasut | 6772cd9 | 2015-08-01 23:12:11 +0200 | [diff] [blame] | 221 | debug(" sdram start %x\n", rule.sdram_start); |
| 222 | debug(" sdram end %x\n", rule.sdram_end); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 223 | debug(" low prot id %d, hi prot id %d\n", |
| 224 | rule.lo_prot_id, |
| 225 | rule.hi_prot_id); |
| 226 | debug(" portmask %x\n", rule.portmask); |
| 227 | debug(" security %d\n", rule.security); |
| 228 | debug(" result %d\n", rule.result); |
| 229 | debug(" valid %d\n", rule.valid); |
| 230 | } |
| 231 | } |
| 232 | |
Marek Vasut | 116d88f | 2015-08-01 22:26:11 +0200 | [diff] [blame] | 233 | /** |
| 234 | * sdram_write_verify() - write to register and verify the write. |
| 235 | * @addr: Register address |
| 236 | * @val: Value to be written and verified |
| 237 | * |
| 238 | * This function writes to a register, reads back the value and compares |
| 239 | * the result with the written value to check if the data match. |
| 240 | */ |
| 241 | static unsigned sdram_write_verify(const u32 *addr, const u32 val) |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 242 | { |
Marek Vasut | 116d88f | 2015-08-01 22:26:11 +0200 | [diff] [blame] | 243 | u32 rval; |
| 244 | |
| 245 | debug(" Write - Address 0x%p Data 0x%08x\n", addr, val); |
| 246 | writel(val, addr); |
| 247 | |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 248 | debug(" Read and verify..."); |
Marek Vasut | 116d88f | 2015-08-01 22:26:11 +0200 | [diff] [blame] | 249 | rval = readl(addr); |
| 250 | if (rval != val) { |
| 251 | debug("FAIL - Address 0x%p Expected 0x%08x Data 0x%08x\n", |
| 252 | addr, val, rval); |
| 253 | return -EINVAL; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 254 | } |
Marek Vasut | 116d88f | 2015-08-01 22:26:11 +0200 | [diff] [blame] | 255 | |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 256 | debug("correct!\n"); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Marek Vasut | b0d848c | 2015-08-01 22:28:30 +0200 | [diff] [blame] | 260 | /** |
| 261 | * sdr_get_ctrlcfg() - Get the value of DRAM CTRLCFG register |
| 262 | * @cfg: SDRAM controller configuration data |
| 263 | * |
| 264 | * Return the value of DRAM CTRLCFG register. |
| 265 | */ |
Marek Vasut | 32ada57 | 2015-08-01 21:35:18 +0200 | [diff] [blame] | 266 | static u32 sdr_get_ctrlcfg(const struct socfpga_sdram_config *cfg) |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 267 | { |
Marek Vasut | 3a07911 | 2015-08-01 21:16:20 +0200 | [diff] [blame] | 268 | const u32 csbits = |
| 269 | ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >> |
| 270 | SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1; |
| 271 | u32 addrorder = |
| 272 | (cfg->ctrl_cfg & SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK) >> |
| 273 | SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB; |
| 274 | |
Marek Vasut | 4f3adbf | 2015-08-01 20:30:10 +0200 | [diff] [blame] | 275 | u32 ctrl_cfg = cfg->ctrl_cfg; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 276 | |
Marek Vasut | 82a2764 | 2015-08-01 19:33:40 +0200 | [diff] [blame] | 277 | /* |
| 278 | * SDRAM Failure When Accessing Non-Existent Memory |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 279 | * Set the addrorder field of the SDRAM control register |
| 280 | * based on the CSBITs setting. |
| 281 | */ |
Marek Vasut | 3a07911 | 2015-08-01 21:16:20 +0200 | [diff] [blame] | 282 | if (csbits == 1) { |
| 283 | if (addrorder != 0) |
Marek Vasut | 82a2764 | 2015-08-01 19:33:40 +0200 | [diff] [blame] | 284 | debug("INFO: Changing address order to 0 (chip, row, bank, column)\n"); |
Marek Vasut | 3a07911 | 2015-08-01 21:16:20 +0200 | [diff] [blame] | 285 | addrorder = 0; |
| 286 | } else if (csbits == 2) { |
| 287 | if (addrorder != 2) |
Marek Vasut | 82a2764 | 2015-08-01 19:33:40 +0200 | [diff] [blame] | 288 | debug("INFO: Changing address order to 2 (row, chip, bank, column)\n"); |
Marek Vasut | 3a07911 | 2015-08-01 21:16:20 +0200 | [diff] [blame] | 289 | addrorder = 2; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 290 | } |
| 291 | |
Marek Vasut | 3a07911 | 2015-08-01 21:16:20 +0200 | [diff] [blame] | 292 | ctrl_cfg &= ~SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK; |
Marek Vasut | 82a2764 | 2015-08-01 19:33:40 +0200 | [diff] [blame] | 293 | ctrl_cfg |= addrorder << SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 294 | |
Marek Vasut | 1e271e4 | 2015-08-01 21:24:31 +0200 | [diff] [blame] | 295 | return ctrl_cfg; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Marek Vasut | b0d848c | 2015-08-01 22:28:30 +0200 | [diff] [blame] | 298 | /** |
| 299 | * sdr_get_addr_rw() - Get the value of DRAM ADDRW register |
| 300 | * @cfg: SDRAM controller configuration data |
| 301 | * |
| 302 | * Return the value of DRAM ADDRW register. |
| 303 | */ |
Marek Vasut | 32ada57 | 2015-08-01 21:35:18 +0200 | [diff] [blame] | 304 | static u32 sdr_get_addr_rw(const struct socfpga_sdram_config *cfg) |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 305 | { |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 306 | /* |
| 307 | * SDRAM Failure When Accessing Non-Existent Memory |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 308 | * Set SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB to |
| 309 | * log2(number of chip select bits). Since there's only |
| 310 | * 1 or 2 chip selects, log2(1) => 0, and log2(2) => 1, |
| 311 | * which is the same as "chip selects" - 1. |
| 312 | */ |
Marek Vasut | 3a07911 | 2015-08-01 21:16:20 +0200 | [diff] [blame] | 313 | const int rows = get_errata_rows(cfg); |
| 314 | u32 dram_addrw = cfg->dram_addrw & ~SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK; |
Marek Vasut | 4f3adbf | 2015-08-01 20:30:10 +0200 | [diff] [blame] | 315 | |
Marek Vasut | 1e271e4 | 2015-08-01 21:24:31 +0200 | [diff] [blame] | 316 | return dram_addrw | (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Marek Vasut | b81f11c | 2015-08-01 21:26:55 +0200 | [diff] [blame] | 319 | /** |
| 320 | * sdr_load_regs() - Load SDRAM controller registers |
| 321 | * @cfg: SDRAM controller configuration data |
| 322 | * |
| 323 | * This function loads the register values into the SDRAM controller block. |
| 324 | */ |
Marek Vasut | 32ada57 | 2015-08-01 21:35:18 +0200 | [diff] [blame] | 325 | static void sdr_load_regs(const struct socfpga_sdram_config *cfg) |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 326 | { |
Marek Vasut | 1e271e4 | 2015-08-01 21:24:31 +0200 | [diff] [blame] | 327 | const u32 ctrl_cfg = sdr_get_ctrlcfg(cfg); |
| 328 | const u32 dram_addrw = sdr_get_addr_rw(cfg); |
| 329 | |
Marek Vasut | 1e271e4 | 2015-08-01 21:24:31 +0200 | [diff] [blame] | 330 | debug("\nConfiguring CTRLCFG\n"); |
| 331 | writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg); |
Marek Vasut | 71c1a00 | 2015-08-01 21:21:21 +0200 | [diff] [blame] | 332 | |
| 333 | debug("Configuring DRAMTIMING1\n"); |
| 334 | writel(cfg->dram_timing1, &sdr_ctrl->dram_timing1); |
| 335 | |
| 336 | debug("Configuring DRAMTIMING2\n"); |
| 337 | writel(cfg->dram_timing2, &sdr_ctrl->dram_timing2); |
| 338 | |
| 339 | debug("Configuring DRAMTIMING3\n"); |
| 340 | writel(cfg->dram_timing3, &sdr_ctrl->dram_timing3); |
| 341 | |
| 342 | debug("Configuring DRAMTIMING4\n"); |
| 343 | writel(cfg->dram_timing4, &sdr_ctrl->dram_timing4); |
| 344 | |
| 345 | debug("Configuring LOWPWRTIMING\n"); |
| 346 | writel(cfg->lowpwr_timing, &sdr_ctrl->lowpwr_timing); |
| 347 | |
Marek Vasut | 1e271e4 | 2015-08-01 21:24:31 +0200 | [diff] [blame] | 348 | debug("Configuring DRAMADDRW\n"); |
| 349 | writel(dram_addrw, &sdr_ctrl->dram_addrw); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 350 | |
| 351 | debug("Configuring DRAMIFWIDTH\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 352 | writel(cfg->dram_if_width, &sdr_ctrl->dram_if_width); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 353 | |
| 354 | debug("Configuring DRAMDEVWIDTH\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 355 | writel(cfg->dram_dev_width, &sdr_ctrl->dram_dev_width); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 356 | |
| 357 | debug("Configuring LOWPWREQ\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 358 | writel(cfg->lowpwr_eq, &sdr_ctrl->lowpwr_eq); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 359 | |
| 360 | debug("Configuring DRAMINTR\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 361 | writel(cfg->dram_intr, &sdr_ctrl->dram_intr); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 362 | |
Marek Vasut | 71c1a00 | 2015-08-01 21:21:21 +0200 | [diff] [blame] | 363 | debug("Configuring STATICCFG\n"); |
| 364 | writel(cfg->static_cfg, &sdr_ctrl->static_cfg); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 365 | |
| 366 | debug("Configuring CTRLWIDTH\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 367 | writel(cfg->ctrl_width, &sdr_ctrl->ctrl_width); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 368 | |
| 369 | debug("Configuring PORTCFG\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 370 | writel(cfg->port_cfg, &sdr_ctrl->port_cfg); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 371 | |
Marek Vasut | 71c1a00 | 2015-08-01 21:21:21 +0200 | [diff] [blame] | 372 | debug("Configuring FIFOCFG\n"); |
| 373 | writel(cfg->fifo_cfg, &sdr_ctrl->fifo_cfg); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 374 | |
| 375 | debug("Configuring MPPRIORITY\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 376 | writel(cfg->mp_priority, &sdr_ctrl->mp_priority); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 377 | |
Marek Vasut | 71c1a00 | 2015-08-01 21:21:21 +0200 | [diff] [blame] | 378 | debug("Configuring MPWEIGHT_MPWEIGHT_0\n"); |
| 379 | writel(cfg->mp_weight0, &sdr_ctrl->mp_weight0); |
| 380 | writel(cfg->mp_weight1, &sdr_ctrl->mp_weight1); |
| 381 | writel(cfg->mp_weight2, &sdr_ctrl->mp_weight2); |
| 382 | writel(cfg->mp_weight3, &sdr_ctrl->mp_weight3); |
| 383 | |
| 384 | debug("Configuring MPPACING_MPPACING_0\n"); |
| 385 | writel(cfg->mp_pacing0, &sdr_ctrl->mp_pacing0); |
| 386 | writel(cfg->mp_pacing1, &sdr_ctrl->mp_pacing1); |
| 387 | writel(cfg->mp_pacing2, &sdr_ctrl->mp_pacing2); |
| 388 | writel(cfg->mp_pacing3, &sdr_ctrl->mp_pacing3); |
| 389 | |
| 390 | debug("Configuring MPTHRESHOLDRST_MPTHRESHOLDRST_0\n"); |
| 391 | writel(cfg->mp_threshold0, &sdr_ctrl->mp_threshold0); |
| 392 | writel(cfg->mp_threshold1, &sdr_ctrl->mp_threshold1); |
| 393 | writel(cfg->mp_threshold2, &sdr_ctrl->mp_threshold2); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 394 | |
| 395 | debug("Configuring PHYCTRL_PHYCTRL_0\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 396 | writel(cfg->phy_ctrl0, &sdr_ctrl->phy_ctrl0); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 397 | |
| 398 | debug("Configuring CPORTWIDTH\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 399 | writel(cfg->cport_width, &sdr_ctrl->cport_width); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 400 | |
| 401 | debug("Configuring CPORTWMAP\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 402 | writel(cfg->cport_wmap, &sdr_ctrl->cport_wmap); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 403 | |
| 404 | debug("Configuring CPORTRMAP\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 405 | writel(cfg->cport_rmap, &sdr_ctrl->cport_rmap); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 406 | |
| 407 | debug("Configuring RFIFOCMAP\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 408 | writel(cfg->rfifo_cmap, &sdr_ctrl->rfifo_cmap); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 409 | |
| 410 | debug("Configuring WFIFOCMAP\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 411 | writel(cfg->wfifo_cmap, &sdr_ctrl->wfifo_cmap); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 412 | |
| 413 | debug("Configuring CPORTRDWR\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 414 | writel(cfg->cport_rdwr, &sdr_ctrl->cport_rdwr); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 415 | |
| 416 | debug("Configuring DRAMODT\n"); |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 417 | writel(cfg->dram_odt, &sdr_ctrl->dram_odt); |
Chin Liang See | 3ea5951 | 2016-09-21 10:25:56 +0800 | [diff] [blame] | 418 | |
| 419 | debug("Configuring EXTRATIME1\n"); |
| 420 | writel(cfg->extratime1, &sdr_ctrl->extratime1); |
Marek Vasut | b81f11c | 2015-08-01 21:26:55 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Marek Vasut | 5a4e8ed | 2015-08-01 22:03:48 +0200 | [diff] [blame] | 423 | /** |
| 424 | * sdram_mmr_init_full() - Function to initialize SDRAM MMR |
| 425 | * @sdr_phy_reg: Value of the PHY control register 0 |
| 426 | * |
| 427 | * Initialize the SDRAM MMR. |
| 428 | */ |
Marek Vasut | 1b1cc10 | 2015-08-01 22:25:29 +0200 | [diff] [blame] | 429 | int sdram_mmr_init_full(unsigned int sdr_phy_reg) |
Marek Vasut | b81f11c | 2015-08-01 21:26:55 +0200 | [diff] [blame] | 430 | { |
Marek Vasut | 32ada57 | 2015-08-01 21:35:18 +0200 | [diff] [blame] | 431 | const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config(); |
Marek Vasut | b81f11c | 2015-08-01 21:26:55 +0200 | [diff] [blame] | 432 | const unsigned int rows = |
| 433 | (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >> |
| 434 | SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB; |
Marek Vasut | 116d88f | 2015-08-01 22:26:11 +0200 | [diff] [blame] | 435 | int ret; |
Marek Vasut | b81f11c | 2015-08-01 21:26:55 +0200 | [diff] [blame] | 436 | |
| 437 | writel(rows, &sysmgr_regs->iswgrp_handoff[4]); |
| 438 | |
| 439 | sdr_load_regs(cfg); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 440 | |
| 441 | /* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */ |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 442 | writel(cfg->fpgaport_rst, &sysmgr_regs->iswgrp_handoff[3]); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 443 | |
| 444 | /* only enable if the FPGA is programmed */ |
| 445 | if (fpgamgr_test_fpga_ready()) { |
Marek Vasut | 116d88f | 2015-08-01 22:26:11 +0200 | [diff] [blame] | 446 | ret = sdram_write_verify(&sdr_ctrl->fpgaport_rst, |
| 447 | cfg->fpgaport_rst); |
| 448 | if (ret) |
| 449 | return ret; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* Restore the SDR PHY Register if valid */ |
| 453 | if (sdr_phy_reg != 0xffffffff) |
| 454 | writel(sdr_phy_reg, &sdr_ctrl->phy_ctrl0); |
| 455 | |
Marek Vasut | 7697ff7 | 2015-08-01 20:58:44 +0200 | [diff] [blame] | 456 | /* Final step - apply configuration changes */ |
| 457 | debug("Configuring STATICCFG\n"); |
| 458 | clrsetbits_le32(&sdr_ctrl->static_cfg, |
| 459 | SDR_CTRLGRP_STATICCFG_APPLYCFG_MASK, |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 460 | 1 << SDR_CTRLGRP_STATICCFG_APPLYCFG_LSB); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 461 | |
Marek Vasut | 6772cd9 | 2015-08-01 23:12:11 +0200 | [diff] [blame] | 462 | sdram_set_protection_config(0, sdram_calculate_size() - 1); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 463 | |
| 464 | sdram_dump_protection_config(); |
| 465 | |
Marek Vasut | 116d88f | 2015-08-01 22:26:11 +0200 | [diff] [blame] | 466 | return 0; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 467 | } |
| 468 | |
Marek Vasut | 1796a09 | 2015-08-01 21:47:16 +0200 | [diff] [blame] | 469 | /** |
| 470 | * sdram_calculate_size() - Calculate SDRAM size |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 471 | * |
Marek Vasut | 1796a09 | 2015-08-01 21:47:16 +0200 | [diff] [blame] | 472 | * Calculate SDRAM device size based on SDRAM controller parameters. |
| 473 | * Size is specified in bytes. |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 474 | */ |
| 475 | unsigned long sdram_calculate_size(void) |
| 476 | { |
| 477 | unsigned long temp; |
| 478 | unsigned long row, bank, col, cs, width; |
Marek Vasut | 6d6fbba | 2015-08-01 21:44:00 +0200 | [diff] [blame] | 479 | const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config(); |
| 480 | const unsigned int csbits = |
| 481 | ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >> |
| 482 | SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1; |
| 483 | const unsigned int rowbits = |
| 484 | (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >> |
| 485 | SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 486 | |
| 487 | temp = readl(&sdr_ctrl->dram_addrw); |
| 488 | col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >> |
| 489 | SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB; |
| 490 | |
Marek Vasut | 1796a09 | 2015-08-01 21:47:16 +0200 | [diff] [blame] | 491 | /* |
| 492 | * SDRAM Failure When Accessing Non-Existent Memory |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 493 | * Use ROWBITS from Quartus/QSys to calculate SDRAM size |
| 494 | * since the FB specifies we modify ROWBITs to work around SDRAM |
| 495 | * controller issue. |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 496 | */ |
| 497 | row = readl(&sysmgr_regs->iswgrp_handoff[4]); |
| 498 | if (row == 0) |
Marek Vasut | 6d6fbba | 2015-08-01 21:44:00 +0200 | [diff] [blame] | 499 | row = rowbits; |
Marek Vasut | 1796a09 | 2015-08-01 21:47:16 +0200 | [diff] [blame] | 500 | /* |
| 501 | * If the stored handoff value for rows is greater than |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 502 | * the field width in the sdr.dramaddrw register then |
| 503 | * something is very wrong. Revert to using the the #define |
| 504 | * value handed off by the SOCEDS tool chain instead of |
| 505 | * using a broken value. |
| 506 | */ |
| 507 | if (row > 31) |
Marek Vasut | 6d6fbba | 2015-08-01 21:44:00 +0200 | [diff] [blame] | 508 | row = rowbits; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 509 | |
| 510 | bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >> |
| 511 | SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB; |
| 512 | |
Marek Vasut | 1796a09 | 2015-08-01 21:47:16 +0200 | [diff] [blame] | 513 | /* |
| 514 | * SDRAM Failure When Accessing Non-Existent Memory |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 515 | * Use CSBITs from Quartus/QSys to calculate SDRAM size |
| 516 | * since the FB specifies we modify CSBITs to work around SDRAM |
| 517 | * controller issue. |
| 518 | */ |
Marek Vasut | 6d6fbba | 2015-08-01 21:44:00 +0200 | [diff] [blame] | 519 | cs = csbits; |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 520 | |
| 521 | width = readl(&sdr_ctrl->dram_if_width); |
Marek Vasut | 1796a09 | 2015-08-01 21:47:16 +0200 | [diff] [blame] | 522 | |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 523 | /* ECC would not be calculated as its not addressible */ |
| 524 | if (width == SDRAM_WIDTH_32BIT_WITH_ECC) |
| 525 | width = 32; |
| 526 | if (width == SDRAM_WIDTH_16BIT_WITH_ECC) |
| 527 | width = 16; |
| 528 | |
| 529 | /* calculate the SDRAM size base on this info */ |
| 530 | temp = 1 << (row + bank + col); |
| 531 | temp = temp * cs * (width / 8); |
| 532 | |
Marek Vasut | 1796a09 | 2015-08-01 21:47:16 +0200 | [diff] [blame] | 533 | debug("%s returns %ld\n", __func__, temp); |
Dinh Nguyen | 429642c | 2015-06-02 22:52:48 -0500 | [diff] [blame] | 534 | |
| 535 | return temp; |
| 536 | } |