blob: c10e3fd732b0d24e796acf92574c1e5d223f8e70 [file] [log] [blame]
Dinh Nguyen429642c2015-06-02 22:52:48 -05001/*
2 * Copyright Altera Corporation (C) 2014-2015
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6#include <common.h>
7#include <div64.h>
8#include <watchdog.h>
9#include <asm/arch/fpga_manager.h>
10#include <asm/arch/sdram.h>
Dinh Nguyen429642c2015-06-02 22:52:48 -050011#include <asm/arch/system_manager.h>
12#include <asm/io.h>
13
Marek Vasut43bb47e2015-07-12 15:59:10 +020014/*
15 * FIXME: This path is temporary until the SDRAM driver gets
16 * a proper thorough cleanup.
17 */
18#include "../../../board/altera/socfpga/qts/sdram_config.h"
19
Dinh Nguyen429642c2015-06-02 22:52:48 -050020DECLARE_GLOBAL_DATA_PTR;
21
Marek Vasute08c5592015-07-26 10:37:54 +020022struct sdram_prot_rule {
23 u64 sdram_start; /* SDRAM start address */
24 u64 sdram_end; /* SDRAM end address */
25 u32 rule; /* SDRAM protection rule number: 0-19 */
26 int valid; /* Rule valid or not? 1 - valid, 0 not*/
27
28 u32 security;
29 u32 portmask;
30 u32 result;
31 u32 lo_prot_id;
32 u32 hi_prot_id;
33};
34
Dinh Nguyen429642c2015-06-02 22:52:48 -050035static struct socfpga_system_manager *sysmgr_regs =
36 (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
37static struct socfpga_sdr_ctrl *sdr_ctrl =
Marek Vasut33acf0f2015-07-12 20:05:54 +020038 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
Dinh Nguyen429642c2015-06-02 22:52:48 -050039
Marek Vasut4f3adbf2015-08-01 20:30:10 +020040static struct socfpga_sdram_config {
41 u32 ctrl_cfg;
42 u32 dram_timing1;
43 u32 dram_timing2;
44 u32 dram_timing3;
45 u32 dram_timing4;
46 u32 lowpwr_timing;
47 u32 dram_addrw;
48 u32 static_cfg;
49 u32 fifo_cfg;
50 u32 mp_weight0;
51 u32 mp_weight1;
52 u32 mp_weight2;
53 u32 mp_weight3;
54 u32 mp_pacing0;
55 u32 mp_pacing1;
56 u32 mp_pacing2;
57 u32 mp_pacing3;
58 u32 mp_threshold0;
59 u32 mp_threshold1;
60 u32 mp_threshold2;
61} sdram_config = {
62 .ctrl_cfg =
63 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_MEMTYPE <<
64 SDR_CTRLGRP_CTRLCFG_MEMTYPE_LSB) |
65 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_MEMBL <<
66 SDR_CTRLGRP_CTRLCFG_MEMBL_LSB) |
67 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ECCEN <<
68 SDR_CTRLGRP_CTRLCFG_ECCEN_LSB) |
69 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ECCCORREN <<
70 SDR_CTRLGRP_CTRLCFG_ECCCORREN_LSB) |
71 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_REORDEREN <<
72 SDR_CTRLGRP_CTRLCFG_REORDEREN_LSB) |
73 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_STARVELIMIT <<
74 SDR_CTRLGRP_CTRLCFG_STARVELIMIT_LSB) |
75 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_DQSTRKEN <<
76 SDR_CTRLGRP_CTRLCFG_DQSTRKEN_LSB) |
77 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_NODMPINS <<
78 SDR_CTRLGRP_CTRLCFG_NODMPINS_LSB),
79 .dram_timing1 =
80 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TCWL <<
81 SDR_CTRLGRP_DRAMTIMING1_TCWL_LSB) |
82 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_AL <<
83 SDR_CTRLGRP_DRAMTIMING1_TAL_LSB) |
84 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TCL <<
85 SDR_CTRLGRP_DRAMTIMING1_TCL_LSB) |
86 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TRRD <<
87 SDR_CTRLGRP_DRAMTIMING1_TRRD_LSB) |
88 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TFAW <<
89 SDR_CTRLGRP_DRAMTIMING1_TFAW_LSB) |
90 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TRFC <<
91 SDR_CTRLGRP_DRAMTIMING1_TRFC_LSB),
92 .dram_timing2 =
93 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TREFI <<
94 SDR_CTRLGRP_DRAMTIMING2_TREFI_LSB) |
95 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TRCD <<
96 SDR_CTRLGRP_DRAMTIMING2_TRCD_LSB) |
97 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TRP <<
98 SDR_CTRLGRP_DRAMTIMING2_TRP_LSB) |
99 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TWR <<
100 SDR_CTRLGRP_DRAMTIMING2_TWR_LSB) |
101 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TWTR <<
102 SDR_CTRLGRP_DRAMTIMING2_TWTR_LSB),
103 .dram_timing3 =
104 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRTP <<
105 SDR_CTRLGRP_DRAMTIMING3_TRTP_LSB) |
106 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRAS <<
107 SDR_CTRLGRP_DRAMTIMING3_TRAS_LSB) |
108 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRC <<
109 SDR_CTRLGRP_DRAMTIMING3_TRC_LSB) |
110 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TMRD <<
111 SDR_CTRLGRP_DRAMTIMING3_TMRD_LSB) |
112 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TCCD <<
113 SDR_CTRLGRP_DRAMTIMING3_TCCD_LSB),
114 .dram_timing4 =
115 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING4_SELFRFSHEXIT <<
116 SDR_CTRLGRP_DRAMTIMING4_SELFRFSHEXIT_LSB) |
117 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING4_PWRDOWNEXIT <<
118 SDR_CTRLGRP_DRAMTIMING4_PWRDOWNEXIT_LSB),
119 .lowpwr_timing =
120 (CONFIG_HPS_SDR_CTRLCFG_LOWPWRTIMING_AUTOPDCYCLES <<
121 SDR_CTRLGRP_LOWPWRTIMING_AUTOPDCYCLES_LSB) |
122 (CONFIG_HPS_SDR_CTRLCFG_LOWPWRTIMING_CLKDISABLECYCLES <<
123 SDR_CTRLGRP_LOWPWRTIMING_CLKDISABLECYCLES_LSB),
124 .dram_addrw =
125 (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS <<
126 SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB) |
127 (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS <<
128 SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB) |
129 ((CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS - 1) <<
130 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB),
131 .static_cfg =
132 (CONFIG_HPS_SDR_CTRLCFG_STATICCFG_MEMBL <<
133 SDR_CTRLGRP_STATICCFG_MEMBL_LSB) |
134 (CONFIG_HPS_SDR_CTRLCFG_STATICCFG_USEECCASDATA <<
135 SDR_CTRLGRP_STATICCFG_USEECCASDATA_LSB),
136 .fifo_cfg =
137 (CONFIG_HPS_SDR_CTRLCFG_FIFOCFG_SYNCMODE <<
138 SDR_CTRLGRP_FIFOCFG_SYNCMODE_LSB) |
139 (CONFIG_HPS_SDR_CTRLCFG_FIFOCFG_INCSYNC <<
140 SDR_CTRLGRP_FIFOCFG_INCSYNC_LSB),
141 .mp_weight0 =
142 (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_0_STATICWEIGHT_31_0 <<
143 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_0_STATICWEIGHT_31_0_LSB),
144 .mp_weight1 =
145 (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_1_STATICWEIGHT_49_32 <<
146 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_STATICWEIGHT_49_32_LSB) |
147 (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_1_SUMOFWEIGHT_13_0 <<
148 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_SUMOFWEIGHTS_13_0_LSB),
149 .mp_weight2 =
150 (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_2_SUMOFWEIGHT_45_14 <<
151 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_2_SUMOFWEIGHTS_45_14_LSB),
152 .mp_weight3 =
153 (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_3_SUMOFWEIGHT_63_46 <<
154 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_3_SUMOFWEIGHTS_63_46_LSB),
155 .mp_pacing0 =
156 (CONFIG_HPS_SDR_CTRLCFG_MPPACING_0_THRESHOLD1_31_0 <<
157 SDR_CTRLGRP_MPPACING_MPPACING_0_THRESHOLD1_31_0_LSB),
158 .mp_pacing1 =
159 (CONFIG_HPS_SDR_CTRLCFG_MPPACING_1_THRESHOLD1_59_32 <<
160 SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD1_59_32_LSB) |
161 (CONFIG_HPS_SDR_CTRLCFG_MPPACING_1_THRESHOLD2_3_0 <<
162 SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD2_3_0_LSB),
163 .mp_pacing2 =
164 (CONFIG_HPS_SDR_CTRLCFG_MPPACING_2_THRESHOLD2_35_4 <<
165 SDR_CTRLGRP_MPPACING_MPPACING_2_THRESHOLD2_35_4_LSB),
166 .mp_pacing3 =
167 (CONFIG_HPS_SDR_CTRLCFG_MPPACING_3_THRESHOLD2_59_36 <<
168 SDR_CTRLGRP_MPPACING_MPPACING_3_THRESHOLD2_59_36_LSB),
169 .mp_threshold0 =
170 (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0 <<
171 SDR_CTRLGRP_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0_LSB),
172 .mp_threshold1 =
173 (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32 <<
174 SDR_CTRLGRP_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32_LSB),
175 .mp_threshold2 =
176 (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64 <<
177 SDR_CTRLGRP_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64_LSB),
178};
179
Marek Vasut724c50f2015-08-01 19:20:19 +0200180/**
181 * get_errata_rows() - Up the number of DRAM rows to cover entire address space
182 *
183 * SDRAM Failure happens when accessing non-existent memory. Artificially
184 * increase the number of rows so that the memory controller thinks it has
185 * 4GB of RAM. This function returns such amount of rows.
186 */
187static int get_errata_rows(void)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500188{
Marek Vasut724c50f2015-08-01 19:20:19 +0200189 /* Define constant for 4G memory - used for SDRAM errata workaround */
190#define MEMSIZE_4G (4ULL * 1024ULL * 1024ULL * 1024ULL)
191 const unsigned long long memsize = MEMSIZE_4G;
192 const unsigned int cs = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS;
193 const unsigned int rows = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
194 const unsigned int banks = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS;
195 const unsigned int cols = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS;
196 const unsigned int width = 8;
197
Dinh Nguyen429642c2015-06-02 22:52:48 -0500198 unsigned long long newrows;
Marek Vasut724c50f2015-08-01 19:20:19 +0200199 int bits, inewrowslog2;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500200
201 debug("workaround rows - memsize %lld\n", memsize);
202 debug("workaround rows - cs %d\n", cs);
203 debug("workaround rows - width %d\n", width);
204 debug("workaround rows - rows %d\n", rows);
205 debug("workaround rows - banks %d\n", banks);
206 debug("workaround rows - cols %d\n", cols);
207
Marek Vasut186880e2015-08-01 18:54:34 +0200208 newrows = lldiv(memsize, cs * (width / 8));
Dinh Nguyen429642c2015-06-02 22:52:48 -0500209 debug("rows workaround - term1 %lld\n", newrows);
210
Marek Vasut186880e2015-08-01 18:54:34 +0200211 newrows = lldiv(newrows, (1 << banks) * (1 << cols));
Dinh Nguyen429642c2015-06-02 22:52:48 -0500212 debug("rows workaround - term2 %lld\n", newrows);
213
Marek Vasut186880e2015-08-01 18:54:34 +0200214 /*
215 * Compute the hamming weight - same as number of bits set.
Dinh Nguyen429642c2015-06-02 22:52:48 -0500216 * Need to see if result is ordinal power of 2 before
217 * attempting log2 of result.
218 */
Marek Vasut2fda5062015-08-01 18:46:55 +0200219 bits = generic_hweight32(newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500220
221 debug("rows workaround - bits %d\n", bits);
222
223 if (bits != 1) {
224 printf("SDRAM workaround failed, bits set %d\n", bits);
225 return rows;
226 }
227
228 if (newrows > UINT_MAX) {
229 printf("SDRAM workaround rangecheck failed, %lld\n", newrows);
230 return rows;
231 }
232
Marek Vasut186880e2015-08-01 18:54:34 +0200233 inewrowslog2 = __ilog2(newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500234
Marek Vasut186880e2015-08-01 18:54:34 +0200235 debug("rows workaround - ilog2 %d, %lld\n", inewrowslog2, newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500236
237 if (inewrowslog2 == -1) {
Marek Vasut186880e2015-08-01 18:54:34 +0200238 printf("SDRAM workaround failed, newrows %lld\n", newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500239 return rows;
240 }
241
242 return inewrowslog2;
243}
244
245/* SDRAM protection rules vary from 0-19, a total of 20 rules. */
246static void sdram_set_rule(struct sdram_prot_rule *prule)
247{
248 uint32_t lo_addr_bits;
249 uint32_t hi_addr_bits;
250 int ruleno = prule->rule;
251
252 /* Select the rule */
253 writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
254
255 /* Obtain the address bits */
256 lo_addr_bits = (uint32_t)(((prule->sdram_start) >> 20ULL) & 0xFFF);
257 hi_addr_bits = (uint32_t)((((prule->sdram_end-1) >> 20ULL)) & 0xFFF);
258
259 debug("sdram set rule start %x, %lld\n", lo_addr_bits,
260 prule->sdram_start);
261 debug("sdram set rule end %x, %lld\n", hi_addr_bits,
262 prule->sdram_end);
263
264 /* Set rule addresses */
265 writel(lo_addr_bits | (hi_addr_bits << 12), &sdr_ctrl->prot_rule_addr);
266
267 /* Set rule protection ids */
268 writel(prule->lo_prot_id | (prule->hi_prot_id << 12),
269 &sdr_ctrl->prot_rule_id);
270
271 /* Set the rule data */
272 writel(prule->security | (prule->valid << 2) |
273 (prule->portmask << 3) | (prule->result << 13),
274 &sdr_ctrl->prot_rule_data);
275
276 /* write the rule */
277 writel(ruleno | (1L << 5), &sdr_ctrl->prot_rule_rdwr);
278
279 /* Set rule number to 0 by default */
280 writel(0, &sdr_ctrl->prot_rule_rdwr);
281}
282
283static void sdram_get_rule(struct sdram_prot_rule *prule)
284{
285 uint32_t addr;
286 uint32_t id;
287 uint32_t data;
288 int ruleno = prule->rule;
289
290 /* Read the rule */
291 writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
292 writel(ruleno | (1L << 6), &sdr_ctrl->prot_rule_rdwr);
293
294 /* Get the addresses */
295 addr = readl(&sdr_ctrl->prot_rule_addr);
296 prule->sdram_start = (addr & 0xFFF) << 20;
297 prule->sdram_end = ((addr >> 12) & 0xFFF) << 20;
298
299 /* Get the configured protection IDs */
300 id = readl(&sdr_ctrl->prot_rule_id);
301 prule->lo_prot_id = id & 0xFFF;
302 prule->hi_prot_id = (id >> 12) & 0xFFF;
303
304 /* Get protection data */
305 data = readl(&sdr_ctrl->prot_rule_data);
306
307 prule->security = data & 0x3;
308 prule->valid = (data >> 2) & 0x1;
309 prule->portmask = (data >> 3) & 0x3FF;
310 prule->result = (data >> 13) & 0x1;
311}
312
313static void sdram_set_protection_config(uint64_t sdram_start, uint64_t sdram_end)
314{
315 struct sdram_prot_rule rule;
316 int rules;
317
318 /* Start with accepting all SDRAM transaction */
319 writel(0x0, &sdr_ctrl->protport_default);
320
321 /* Clear all protection rules for warm boot case */
322 memset(&rule, 0, sizeof(struct sdram_prot_rule));
323
324 for (rules = 0; rules < 20; rules++) {
325 rule.rule = rules;
326 sdram_set_rule(&rule);
327 }
328
329 /* new rule: accept SDRAM */
330 rule.sdram_start = sdram_start;
331 rule.sdram_end = sdram_end;
332 rule.lo_prot_id = 0x0;
333 rule.hi_prot_id = 0xFFF;
334 rule.portmask = 0x3FF;
335 rule.security = 0x3;
336 rule.result = 0;
337 rule.valid = 1;
338 rule.rule = 0;
339
340 /* set new rule */
341 sdram_set_rule(&rule);
342
343 /* default rule: reject everything */
344 writel(0x3ff, &sdr_ctrl->protport_default);
345}
346
347static void sdram_dump_protection_config(void)
348{
349 struct sdram_prot_rule rule;
350 int rules;
351
352 debug("SDRAM Prot rule, default %x\n",
353 readl(&sdr_ctrl->protport_default));
354
355 for (rules = 0; rules < 20; rules++) {
356 sdram_get_rule(&rule);
357 debug("Rule %d, rules ...\n", rules);
358 debug(" sdram start %llx\n", rule.sdram_start);
359 debug(" sdram end %llx\n", rule.sdram_end);
360 debug(" low prot id %d, hi prot id %d\n",
361 rule.lo_prot_id,
362 rule.hi_prot_id);
363 debug(" portmask %x\n", rule.portmask);
364 debug(" security %d\n", rule.security);
365 debug(" result %d\n", rule.result);
366 debug(" valid %d\n", rule.valid);
367 }
368}
369
370/* Function to write to register and verify the write */
371static unsigned sdram_write_verify(unsigned int *addr, unsigned reg_value)
372{
373#ifndef SDRAM_MMR_SKIP_VERIFY
374 unsigned reg_value1;
375#endif
376 debug(" Write - Address ");
377 debug("0x%08x Data 0x%08x\n", (u32)addr, reg_value);
378 /* Write to register */
379 writel(reg_value, addr);
380#ifndef SDRAM_MMR_SKIP_VERIFY
381 debug(" Read and verify...");
382 /* Read back the wrote value */
383 reg_value1 = readl(addr);
384 /* Indicate failure if value not matched */
385 if (reg_value1 != reg_value) {
386 debug("FAIL - Address 0x%08x Expected 0x%08x Data 0x%08x\n",
387 (u32)addr, reg_value, reg_value1);
388 return 1;
389 }
390 debug("correct!\n");
391#endif /* SDRAM_MMR_SKIP_VERIFY */
392 return 0;
393}
394
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200395static void set_sdr_ctrlcfg(struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500396{
Marek Vasut82a27642015-08-01 19:33:40 +0200397 u32 addrorder;
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200398 u32 ctrl_cfg = cfg->ctrl_cfg;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500399
400 debug("\nConfiguring CTRLCFG\n");
Dinh Nguyen429642c2015-06-02 22:52:48 -0500401
Marek Vasut82a27642015-08-01 19:33:40 +0200402 /*
403 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500404 * Set the addrorder field of the SDRAM control register
405 * based on the CSBITs setting.
406 */
407 switch (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS) {
408 case 1:
409 addrorder = 0; /* chip, row, bank, column */
410 if (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER != 0)
Marek Vasut82a27642015-08-01 19:33:40 +0200411 debug("INFO: Changing address order to 0 (chip, row, bank, column)\n");
Dinh Nguyen429642c2015-06-02 22:52:48 -0500412 break;
413 case 2:
414 addrorder = 2; /* row, chip, bank, column */
415 if (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER != 2)
Marek Vasut82a27642015-08-01 19:33:40 +0200416 debug("INFO: Changing address order to 2 (row, chip, bank, column)\n");
Dinh Nguyen429642c2015-06-02 22:52:48 -0500417 break;
418 default:
419 addrorder = CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER;
420 break;
421 }
422
Marek Vasut82a27642015-08-01 19:33:40 +0200423 ctrl_cfg |= addrorder << SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500424
Marek Vasut82a27642015-08-01 19:33:40 +0200425 writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500426}
427
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200428static void set_sdr_dram_timing(struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500429{
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200430 debug("Configuring DRAMTIMING1\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200431 writel(cfg->dram_timing1, &sdr_ctrl->dram_timing1);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500432
Dinh Nguyen429642c2015-06-02 22:52:48 -0500433 debug("Configuring DRAMTIMING2\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200434 writel(cfg->dram_timing2, &sdr_ctrl->dram_timing2);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500435
Dinh Nguyen429642c2015-06-02 22:52:48 -0500436 debug("Configuring DRAMTIMING3\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200437 writel(cfg->dram_timing3, &sdr_ctrl->dram_timing3);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500438
Dinh Nguyen429642c2015-06-02 22:52:48 -0500439 debug("Configuring DRAMTIMING4\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200440 writel(cfg->dram_timing4, &sdr_ctrl->dram_timing4);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500441
Dinh Nguyen429642c2015-06-02 22:52:48 -0500442 debug("Configuring LOWPWRTIMING\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200443 writel(cfg->lowpwr_timing, &sdr_ctrl->lowpwr_timing);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500444}
445
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200446static void set_sdr_addr_rw(struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500447{
Dinh Nguyen429642c2015-06-02 22:52:48 -0500448 /*
449 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500450 * Set SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB to
451 * log2(number of chip select bits). Since there's only
452 * 1 or 2 chip selects, log2(1) => 0, and log2(2) => 1,
453 * which is the same as "chip selects" - 1.
454 */
Marek Vasut820b0d92015-08-01 19:50:56 +0200455 const int rows = get_errata_rows();
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200456
Marek Vasut820b0d92015-08-01 19:50:56 +0200457 debug("Configuring DRAMADDRW\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200458 writel(cfg->dram_addrw | (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB),
459 &sdr_ctrl->dram_addrw);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500460}
461
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200462static void set_sdr_static_cfg(struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500463{
Marek Vasut9d64f192015-08-01 20:04:19 +0200464 debug("Configuring STATICCFG\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200465 writel(cfg->static_cfg, &sdr_ctrl->static_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500466}
467
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200468static void set_sdr_fifo_cfg(struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500469{
Marek Vasutf904a862015-08-01 20:04:33 +0200470 debug("Configuring FIFOCFG\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200471 writel(cfg->fifo_cfg, &sdr_ctrl->fifo_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500472}
473
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200474static void set_sdr_mp_weight(struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500475{
Marek Vasutb933b192015-08-01 20:10:23 +0200476 debug("Configuring MPWEIGHT_MPWEIGHT_0\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200477 writel(cfg->mp_weight0, &sdr_ctrl->mp_weight0);
478 writel(cfg->mp_weight1, &sdr_ctrl->mp_weight1);
479 writel(cfg->mp_weight2, &sdr_ctrl->mp_weight2);
480 writel(cfg->mp_weight3, &sdr_ctrl->mp_weight3);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500481}
482
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200483static void set_sdr_mp_pacing(struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500484{
Marek Vasut44f09cc2015-08-01 20:12:31 +0200485 debug("Configuring MPPACING_MPPACING_0\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200486 writel(cfg->mp_pacing0, &sdr_ctrl->mp_pacing0);
487 writel(cfg->mp_pacing1, &sdr_ctrl->mp_pacing1);
488 writel(cfg->mp_pacing2, &sdr_ctrl->mp_pacing2);
489 writel(cfg->mp_pacing3, &sdr_ctrl->mp_pacing3);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500490}
491
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200492static void set_sdr_mp_threshold(struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500493{
Marek Vasut92e8e6f2015-08-01 20:14:11 +0200494 debug("Configuring MPTHRESHOLDRST_MPTHRESHOLDRST_0\n");
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200495 writel(cfg->mp_threshold0, &sdr_ctrl->mp_threshold0);
496 writel(cfg->mp_threshold1, &sdr_ctrl->mp_threshold1);
497 writel(cfg->mp_threshold2, &sdr_ctrl->mp_threshold2);
Marek Vasut92e8e6f2015-08-01 20:14:11 +0200498}
Dinh Nguyen429642c2015-06-02 22:52:48 -0500499
500/* Function to initialize SDRAM MMR */
501unsigned sdram_mmr_init_full(unsigned int sdr_phy_reg)
502{
503 unsigned long reg_value;
504 unsigned long status = 0;
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200505 struct socfpga_sdram_config *cfg = &sdram_config;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500506
507#if defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS) && \
508defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS) && \
509defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS) && \
510defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS) && \
511defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS)
512
513 writel(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS,
514 &sysmgr_regs->iswgrp_handoff[4]);
515#endif
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200516 set_sdr_ctrlcfg(cfg);
517 set_sdr_dram_timing(cfg);
518 set_sdr_addr_rw(cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500519
520 debug("Configuring DRAMIFWIDTH\n");
521 clrsetbits_le32(&sdr_ctrl->dram_if_width,
522 SDR_CTRLGRP_DRAMIFWIDTH_IFWIDTH_MASK,
523 CONFIG_HPS_SDR_CTRLCFG_DRAMIFWIDTH_IFWIDTH <<
524 SDR_CTRLGRP_DRAMIFWIDTH_IFWIDTH_LSB);
525
526 debug("Configuring DRAMDEVWIDTH\n");
527 clrsetbits_le32(&sdr_ctrl->dram_dev_width,
528 SDR_CTRLGRP_DRAMDEVWIDTH_DEVWIDTH_MASK,
529 CONFIG_HPS_SDR_CTRLCFG_DRAMDEVWIDTH_DEVWIDTH <<
530 SDR_CTRLGRP_DRAMDEVWIDTH_DEVWIDTH_LSB);
531
532 debug("Configuring LOWPWREQ\n");
533 clrsetbits_le32(&sdr_ctrl->lowpwr_eq,
534 SDR_CTRLGRP_LOWPWREQ_SELFRFSHMASK_MASK,
535 CONFIG_HPS_SDR_CTRLCFG_LOWPWREQ_SELFRFSHMASK <<
536 SDR_CTRLGRP_LOWPWREQ_SELFRFSHMASK_LSB);
537
538 debug("Configuring DRAMINTR\n");
539 clrsetbits_le32(&sdr_ctrl->dram_intr, SDR_CTRLGRP_DRAMINTR_INTREN_MASK,
540 CONFIG_HPS_SDR_CTRLCFG_DRAMINTR_INTREN <<
541 SDR_CTRLGRP_DRAMINTR_INTREN_LSB);
542
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200543 set_sdr_static_cfg(cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500544
545 debug("Configuring CTRLWIDTH\n");
546 clrsetbits_le32(&sdr_ctrl->ctrl_width,
547 SDR_CTRLGRP_CTRLWIDTH_CTRLWIDTH_MASK,
548 CONFIG_HPS_SDR_CTRLCFG_CTRLWIDTH_CTRLWIDTH <<
549 SDR_CTRLGRP_CTRLWIDTH_CTRLWIDTH_LSB);
550
551 debug("Configuring PORTCFG\n");
552 clrsetbits_le32(&sdr_ctrl->port_cfg, SDR_CTRLGRP_PORTCFG_AUTOPCHEN_MASK,
553 CONFIG_HPS_SDR_CTRLCFG_PORTCFG_AUTOPCHEN <<
554 SDR_CTRLGRP_PORTCFG_AUTOPCHEN_LSB);
555
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200556 set_sdr_fifo_cfg(cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500557
558 debug("Configuring MPPRIORITY\n");
559 clrsetbits_le32(&sdr_ctrl->mp_priority,
560 SDR_CTRLGRP_MPPRIORITY_USERPRIORITY_MASK,
561 CONFIG_HPS_SDR_CTRLCFG_MPPRIORITY_USERPRIORITY <<
562 SDR_CTRLGRP_MPPRIORITY_USERPRIORITY_LSB);
563
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200564 set_sdr_mp_weight(cfg);
565 set_sdr_mp_pacing(cfg);
566 set_sdr_mp_threshold(cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500567
568 debug("Configuring PHYCTRL_PHYCTRL_0\n");
569 setbits_le32(&sdr_ctrl->phy_ctrl0,
570 CONFIG_HPS_SDR_CTRLCFG_PHYCTRL_PHYCTRL_0);
571
572 debug("Configuring CPORTWIDTH\n");
573 clrsetbits_le32(&sdr_ctrl->cport_width,
574 SDR_CTRLGRP_CPORTWIDTH_CMDPORTWIDTH_MASK,
575 CONFIG_HPS_SDR_CTRLCFG_CPORTWIDTH_CPORTWIDTH <<
576 SDR_CTRLGRP_CPORTWIDTH_CMDPORTWIDTH_LSB);
577 debug(" Write - Address ");
578 debug("0x%08x Data 0x%08x\n",
579 (unsigned)(&sdr_ctrl->cport_width),
580 (unsigned)reg_value);
581 reg_value = readl(&sdr_ctrl->cport_width);
582 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
583
584 debug("Configuring CPORTWMAP\n");
585 clrsetbits_le32(&sdr_ctrl->cport_wmap,
586 SDR_CTRLGRP_CPORTWMAP_CPORTWFIFOMAP_MASK,
587 CONFIG_HPS_SDR_CTRLCFG_CPORTWMAP_CPORTWMAP <<
588 SDR_CTRLGRP_CPORTWMAP_CPORTWFIFOMAP_LSB);
589 debug(" Write - Address ");
590 debug("0x%08x Data 0x%08x\n",
591 (unsigned)(&sdr_ctrl->cport_wmap),
592 (unsigned)reg_value);
593 reg_value = readl(&sdr_ctrl->cport_wmap);
594 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
595
596 debug("Configuring CPORTRMAP\n");
597 clrsetbits_le32(&sdr_ctrl->cport_rmap,
598 SDR_CTRLGRP_CPORTRMAP_CPORTRFIFOMAP_MASK,
599 CONFIG_HPS_SDR_CTRLCFG_CPORTRMAP_CPORTRMAP <<
600 SDR_CTRLGRP_CPORTRMAP_CPORTRFIFOMAP_LSB);
601 debug(" Write - Address ");
602 debug("0x%08x Data 0x%08x\n",
603 (unsigned)(&sdr_ctrl->cport_rmap),
604 (unsigned)reg_value);
605 reg_value = readl(&sdr_ctrl->cport_rmap);
606 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
607
608 debug("Configuring RFIFOCMAP\n");
609 clrsetbits_le32(&sdr_ctrl->rfifo_cmap,
610 SDR_CTRLGRP_RFIFOCMAP_RFIFOCPORTMAP_MASK,
611 CONFIG_HPS_SDR_CTRLCFG_RFIFOCMAP_RFIFOCMAP <<
612 SDR_CTRLGRP_RFIFOCMAP_RFIFOCPORTMAP_LSB);
613 debug(" Write - Address ");
614 debug("0x%08x Data 0x%08x\n",
615 (unsigned)(&sdr_ctrl->rfifo_cmap),
616 (unsigned)reg_value);
617 reg_value = readl(&sdr_ctrl->rfifo_cmap);
618 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
619
620 debug("Configuring WFIFOCMAP\n");
621 reg_value = readl(&sdr_ctrl->wfifo_cmap);
622 clrsetbits_le32(&sdr_ctrl->wfifo_cmap,
623 SDR_CTRLGRP_WFIFOCMAP_WFIFOCPORTMAP_MASK,
624 CONFIG_HPS_SDR_CTRLCFG_WFIFOCMAP_WFIFOCMAP <<
625 SDR_CTRLGRP_WFIFOCMAP_WFIFOCPORTMAP_LSB);
626 debug(" Write - Address ");
627 debug("0x%08x Data 0x%08x\n",
628 (unsigned)(&sdr_ctrl->wfifo_cmap),
629 (unsigned)reg_value);
630 reg_value = readl(&sdr_ctrl->wfifo_cmap);
631 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
632
633 debug("Configuring CPORTRDWR\n");
634 clrsetbits_le32(&sdr_ctrl->cport_rdwr,
635 SDR_CTRLGRP_CPORTRDWR_CPORTRDWR_MASK,
636 CONFIG_HPS_SDR_CTRLCFG_CPORTRDWR_CPORTRDWR <<
637 SDR_CTRLGRP_CPORTRDWR_CPORTRDWR_LSB);
638 debug(" Write - Address ");
639 debug("0x%08x Data 0x%08x\n",
640 (unsigned)(&sdr_ctrl->cport_rdwr),
641 (unsigned)reg_value);
642 reg_value = readl(&sdr_ctrl->cport_rdwr);
643 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
644
645 debug("Configuring DRAMODT\n");
646 clrsetbits_le32(&sdr_ctrl->dram_odt,
647 SDR_CTRLGRP_DRAMODT_READ_MASK,
648 CONFIG_HPS_SDR_CTRLCFG_DRAMODT_READ <<
649 SDR_CTRLGRP_DRAMODT_READ_LSB);
650
651 clrsetbits_le32(&sdr_ctrl->dram_odt,
652 SDR_CTRLGRP_DRAMODT_WRITE_MASK,
653 CONFIG_HPS_SDR_CTRLCFG_DRAMODT_WRITE <<
654 SDR_CTRLGRP_DRAMODT_WRITE_LSB);
655
656 /* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */
657 writel(CONFIG_HPS_SDR_CTRLCFG_FPGAPORTRST,
658 &sysmgr_regs->iswgrp_handoff[3]);
659
660 /* only enable if the FPGA is programmed */
661 if (fpgamgr_test_fpga_ready()) {
662 if (sdram_write_verify(&sdr_ctrl->fpgaport_rst,
663 CONFIG_HPS_SDR_CTRLCFG_FPGAPORTRST) == 1) {
664 status = 1;
665 return 1;
666 }
667 }
668
669 /* Restore the SDR PHY Register if valid */
670 if (sdr_phy_reg != 0xffffffff)
671 writel(sdr_phy_reg, &sdr_ctrl->phy_ctrl0);
672
673/***** Final step - apply configuration changes *****/
674 debug("Configuring STATICCFG_\n");
675 clrsetbits_le32(&sdr_ctrl->static_cfg, SDR_CTRLGRP_STATICCFG_APPLYCFG_MASK,
676 1 << SDR_CTRLGRP_STATICCFG_APPLYCFG_LSB);
677 debug(" Write - Address ");
678 debug("0x%08x Data 0x%08x\n",
679 (unsigned)(&sdr_ctrl->static_cfg),
680 (unsigned)reg_value);
681 reg_value = readl(&sdr_ctrl->static_cfg);
682 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
683
684 sdram_set_protection_config(0, sdram_calculate_size());
685
686 sdram_dump_protection_config();
687
688 return status;
689}
690
691/*
692 * To calculate SDRAM device size based on SDRAM controller parameters.
693 * Size is specified in bytes.
694 *
695 * NOTE:
696 * This function is compiled and linked into the preloader and
697 * Uboot (there may be others). So if this function changes, the Preloader
698 * and UBoot must be updated simultaneously.
699 */
700unsigned long sdram_calculate_size(void)
701{
702 unsigned long temp;
703 unsigned long row, bank, col, cs, width;
704
705 temp = readl(&sdr_ctrl->dram_addrw);
706 col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
707 SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
708
709 /* SDRAM Failure When Accessing Non-Existent Memory
710 * Use ROWBITS from Quartus/QSys to calculate SDRAM size
711 * since the FB specifies we modify ROWBITs to work around SDRAM
712 * controller issue.
713 *
714 * If the stored handoff value for rows is 0, it probably means
715 * the preloader is older than UBoot. Use the
716 * #define from the SOCEDS Tools per Crucible review
717 * uboot-socfpga-204. Note that this is not a supported
718 * configuration and is not tested. The customer
719 * should be using preloader and uboot built from the
720 * same tag.
721 */
722 row = readl(&sysmgr_regs->iswgrp_handoff[4]);
723 if (row == 0)
724 row = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
725 /* If the stored handoff value for rows is greater than
726 * the field width in the sdr.dramaddrw register then
727 * something is very wrong. Revert to using the the #define
728 * value handed off by the SOCEDS tool chain instead of
729 * using a broken value.
730 */
731 if (row > 31)
732 row = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
733
734 bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
735 SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
736
737 /* SDRAM Failure When Accessing Non-Existent Memory
738 * Use CSBITs from Quartus/QSys to calculate SDRAM size
739 * since the FB specifies we modify CSBITs to work around SDRAM
740 * controller issue.
741 */
742 cs = (temp & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
743 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB;
744 cs += 1;
745
746 cs = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS;
747
748 width = readl(&sdr_ctrl->dram_if_width);
749 /* ECC would not be calculated as its not addressible */
750 if (width == SDRAM_WIDTH_32BIT_WITH_ECC)
751 width = 32;
752 if (width == SDRAM_WIDTH_16BIT_WITH_ECC)
753 width = 16;
754
755 /* calculate the SDRAM size base on this info */
756 temp = 1 << (row + bank + col);
757 temp = temp * cs * (width / 8);
758
759 debug("sdram_calculate_memory returns %ld\n", temp);
760
761 return temp;
762}