blob: 8db8dde5dd90abeae835427d26096ed51db626b2 [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 Vasut724c50f2015-08-01 19:20:19 +020040/**
41 * get_errata_rows() - Up the number of DRAM rows to cover entire address space
42 *
43 * SDRAM Failure happens when accessing non-existent memory. Artificially
44 * increase the number of rows so that the memory controller thinks it has
45 * 4GB of RAM. This function returns such amount of rows.
46 */
47static int get_errata_rows(void)
Dinh Nguyen429642c2015-06-02 22:52:48 -050048{
Marek Vasut724c50f2015-08-01 19:20:19 +020049 /* Define constant for 4G memory - used for SDRAM errata workaround */
50#define MEMSIZE_4G (4ULL * 1024ULL * 1024ULL * 1024ULL)
51 const unsigned long long memsize = MEMSIZE_4G;
52 const unsigned int cs = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS;
53 const unsigned int rows = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
54 const unsigned int banks = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS;
55 const unsigned int cols = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS;
56 const unsigned int width = 8;
57
Dinh Nguyen429642c2015-06-02 22:52:48 -050058 unsigned long long newrows;
Marek Vasut724c50f2015-08-01 19:20:19 +020059 int bits, inewrowslog2;
Dinh Nguyen429642c2015-06-02 22:52:48 -050060
61 debug("workaround rows - memsize %lld\n", memsize);
62 debug("workaround rows - cs %d\n", cs);
63 debug("workaround rows - width %d\n", width);
64 debug("workaround rows - rows %d\n", rows);
65 debug("workaround rows - banks %d\n", banks);
66 debug("workaround rows - cols %d\n", cols);
67
Marek Vasut186880e2015-08-01 18:54:34 +020068 newrows = lldiv(memsize, cs * (width / 8));
Dinh Nguyen429642c2015-06-02 22:52:48 -050069 debug("rows workaround - term1 %lld\n", newrows);
70
Marek Vasut186880e2015-08-01 18:54:34 +020071 newrows = lldiv(newrows, (1 << banks) * (1 << cols));
Dinh Nguyen429642c2015-06-02 22:52:48 -050072 debug("rows workaround - term2 %lld\n", newrows);
73
Marek Vasut186880e2015-08-01 18:54:34 +020074 /*
75 * Compute the hamming weight - same as number of bits set.
Dinh Nguyen429642c2015-06-02 22:52:48 -050076 * Need to see if result is ordinal power of 2 before
77 * attempting log2 of result.
78 */
Marek Vasut2fda5062015-08-01 18:46:55 +020079 bits = generic_hweight32(newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -050080
81 debug("rows workaround - bits %d\n", bits);
82
83 if (bits != 1) {
84 printf("SDRAM workaround failed, bits set %d\n", bits);
85 return rows;
86 }
87
88 if (newrows > UINT_MAX) {
89 printf("SDRAM workaround rangecheck failed, %lld\n", newrows);
90 return rows;
91 }
92
Marek Vasut186880e2015-08-01 18:54:34 +020093 inewrowslog2 = __ilog2(newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -050094
Marek Vasut186880e2015-08-01 18:54:34 +020095 debug("rows workaround - ilog2 %d, %lld\n", inewrowslog2, newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -050096
97 if (inewrowslog2 == -1) {
Marek Vasut186880e2015-08-01 18:54:34 +020098 printf("SDRAM workaround failed, newrows %lld\n", newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -050099 return rows;
100 }
101
102 return inewrowslog2;
103}
104
105/* SDRAM protection rules vary from 0-19, a total of 20 rules. */
106static void sdram_set_rule(struct sdram_prot_rule *prule)
107{
108 uint32_t lo_addr_bits;
109 uint32_t hi_addr_bits;
110 int ruleno = prule->rule;
111
112 /* Select the rule */
113 writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
114
115 /* Obtain the address bits */
116 lo_addr_bits = (uint32_t)(((prule->sdram_start) >> 20ULL) & 0xFFF);
117 hi_addr_bits = (uint32_t)((((prule->sdram_end-1) >> 20ULL)) & 0xFFF);
118
119 debug("sdram set rule start %x, %lld\n", lo_addr_bits,
120 prule->sdram_start);
121 debug("sdram set rule end %x, %lld\n", hi_addr_bits,
122 prule->sdram_end);
123
124 /* Set rule addresses */
125 writel(lo_addr_bits | (hi_addr_bits << 12), &sdr_ctrl->prot_rule_addr);
126
127 /* Set rule protection ids */
128 writel(prule->lo_prot_id | (prule->hi_prot_id << 12),
129 &sdr_ctrl->prot_rule_id);
130
131 /* Set the rule data */
132 writel(prule->security | (prule->valid << 2) |
133 (prule->portmask << 3) | (prule->result << 13),
134 &sdr_ctrl->prot_rule_data);
135
136 /* write the rule */
137 writel(ruleno | (1L << 5), &sdr_ctrl->prot_rule_rdwr);
138
139 /* Set rule number to 0 by default */
140 writel(0, &sdr_ctrl->prot_rule_rdwr);
141}
142
143static void sdram_get_rule(struct sdram_prot_rule *prule)
144{
145 uint32_t addr;
146 uint32_t id;
147 uint32_t data;
148 int ruleno = prule->rule;
149
150 /* Read the rule */
151 writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
152 writel(ruleno | (1L << 6), &sdr_ctrl->prot_rule_rdwr);
153
154 /* Get the addresses */
155 addr = readl(&sdr_ctrl->prot_rule_addr);
156 prule->sdram_start = (addr & 0xFFF) << 20;
157 prule->sdram_end = ((addr >> 12) & 0xFFF) << 20;
158
159 /* Get the configured protection IDs */
160 id = readl(&sdr_ctrl->prot_rule_id);
161 prule->lo_prot_id = id & 0xFFF;
162 prule->hi_prot_id = (id >> 12) & 0xFFF;
163
164 /* Get protection data */
165 data = readl(&sdr_ctrl->prot_rule_data);
166
167 prule->security = data & 0x3;
168 prule->valid = (data >> 2) & 0x1;
169 prule->portmask = (data >> 3) & 0x3FF;
170 prule->result = (data >> 13) & 0x1;
171}
172
173static void sdram_set_protection_config(uint64_t sdram_start, uint64_t sdram_end)
174{
175 struct sdram_prot_rule rule;
176 int rules;
177
178 /* Start with accepting all SDRAM transaction */
179 writel(0x0, &sdr_ctrl->protport_default);
180
181 /* Clear all protection rules for warm boot case */
182 memset(&rule, 0, sizeof(struct sdram_prot_rule));
183
184 for (rules = 0; rules < 20; rules++) {
185 rule.rule = rules;
186 sdram_set_rule(&rule);
187 }
188
189 /* new rule: accept SDRAM */
190 rule.sdram_start = sdram_start;
191 rule.sdram_end = sdram_end;
192 rule.lo_prot_id = 0x0;
193 rule.hi_prot_id = 0xFFF;
194 rule.portmask = 0x3FF;
195 rule.security = 0x3;
196 rule.result = 0;
197 rule.valid = 1;
198 rule.rule = 0;
199
200 /* set new rule */
201 sdram_set_rule(&rule);
202
203 /* default rule: reject everything */
204 writel(0x3ff, &sdr_ctrl->protport_default);
205}
206
207static void sdram_dump_protection_config(void)
208{
209 struct sdram_prot_rule rule;
210 int rules;
211
212 debug("SDRAM Prot rule, default %x\n",
213 readl(&sdr_ctrl->protport_default));
214
215 for (rules = 0; rules < 20; rules++) {
216 sdram_get_rule(&rule);
217 debug("Rule %d, rules ...\n", rules);
218 debug(" sdram start %llx\n", rule.sdram_start);
219 debug(" sdram end %llx\n", rule.sdram_end);
220 debug(" low prot id %d, hi prot id %d\n",
221 rule.lo_prot_id,
222 rule.hi_prot_id);
223 debug(" portmask %x\n", rule.portmask);
224 debug(" security %d\n", rule.security);
225 debug(" result %d\n", rule.result);
226 debug(" valid %d\n", rule.valid);
227 }
228}
229
230/* Function to write to register and verify the write */
231static unsigned sdram_write_verify(unsigned int *addr, unsigned reg_value)
232{
233#ifndef SDRAM_MMR_SKIP_VERIFY
234 unsigned reg_value1;
235#endif
236 debug(" Write - Address ");
237 debug("0x%08x Data 0x%08x\n", (u32)addr, reg_value);
238 /* Write to register */
239 writel(reg_value, addr);
240#ifndef SDRAM_MMR_SKIP_VERIFY
241 debug(" Read and verify...");
242 /* Read back the wrote value */
243 reg_value1 = readl(addr);
244 /* Indicate failure if value not matched */
245 if (reg_value1 != reg_value) {
246 debug("FAIL - Address 0x%08x Expected 0x%08x Data 0x%08x\n",
247 (u32)addr, reg_value, reg_value1);
248 return 1;
249 }
250 debug("correct!\n");
251#endif /* SDRAM_MMR_SKIP_VERIFY */
252 return 0;
253}
254
255static void set_sdr_ctrlcfg(void)
256{
Marek Vasut82a27642015-08-01 19:33:40 +0200257 u32 addrorder;
258 u32 ctrl_cfg =
259 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_MEMTYPE <<
260 SDR_CTRLGRP_CTRLCFG_MEMTYPE_LSB) |
261 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_MEMBL <<
262 SDR_CTRLGRP_CTRLCFG_MEMBL_LSB) |
263 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ECCEN <<
264 SDR_CTRLGRP_CTRLCFG_ECCEN_LSB) |
265 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ECCCORREN <<
266 SDR_CTRLGRP_CTRLCFG_ECCCORREN_LSB) |
267 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_REORDEREN <<
268 SDR_CTRLGRP_CTRLCFG_REORDEREN_LSB) |
269 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_STARVELIMIT <<
270 SDR_CTRLGRP_CTRLCFG_STARVELIMIT_LSB) |
271 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_DQSTRKEN <<
272 SDR_CTRLGRP_CTRLCFG_DQSTRKEN_LSB) |
273 (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_NODMPINS <<
274 SDR_CTRLGRP_CTRLCFG_NODMPINS_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500275
276 debug("\nConfiguring CTRLCFG\n");
Dinh Nguyen429642c2015-06-02 22:52:48 -0500277
Marek Vasut82a27642015-08-01 19:33:40 +0200278 /*
279 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500280 * Set the addrorder field of the SDRAM control register
281 * based on the CSBITs setting.
282 */
283 switch (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS) {
284 case 1:
285 addrorder = 0; /* chip, row, bank, column */
286 if (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER != 0)
Marek Vasut82a27642015-08-01 19:33:40 +0200287 debug("INFO: Changing address order to 0 (chip, row, bank, column)\n");
Dinh Nguyen429642c2015-06-02 22:52:48 -0500288 break;
289 case 2:
290 addrorder = 2; /* row, chip, bank, column */
291 if (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER != 2)
Marek Vasut82a27642015-08-01 19:33:40 +0200292 debug("INFO: Changing address order to 2 (row, chip, bank, column)\n");
Dinh Nguyen429642c2015-06-02 22:52:48 -0500293 break;
294 default:
295 addrorder = CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER;
296 break;
297 }
298
Marek Vasut82a27642015-08-01 19:33:40 +0200299 ctrl_cfg |= addrorder << SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500300
Marek Vasut82a27642015-08-01 19:33:40 +0200301 writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500302}
303
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200304static void set_sdr_dram_timing(void)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500305{
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200306 const u32 dram_timing1 =
307 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TCWL <<
308 SDR_CTRLGRP_DRAMTIMING1_TCWL_LSB) |
309 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_AL <<
310 SDR_CTRLGRP_DRAMTIMING1_TAL_LSB) |
311 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TCL <<
312 SDR_CTRLGRP_DRAMTIMING1_TCL_LSB) |
313 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TRRD <<
314 SDR_CTRLGRP_DRAMTIMING1_TRRD_LSB) |
315 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TFAW <<
316 SDR_CTRLGRP_DRAMTIMING1_TFAW_LSB) |
317 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TRFC <<
318 SDR_CTRLGRP_DRAMTIMING1_TRFC_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500319
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200320 const u32 dram_timing2 =
321 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TREFI <<
322 SDR_CTRLGRP_DRAMTIMING2_TREFI_LSB) |
323 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TRCD <<
324 SDR_CTRLGRP_DRAMTIMING2_TRCD_LSB) |
325 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TRP <<
326 SDR_CTRLGRP_DRAMTIMING2_TRP_LSB) |
327 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TWR <<
328 SDR_CTRLGRP_DRAMTIMING2_TWR_LSB) |
329 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TWTR <<
330 SDR_CTRLGRP_DRAMTIMING2_TWTR_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500331
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200332 const u32 dram_timing3 =
333 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRTP <<
334 SDR_CTRLGRP_DRAMTIMING3_TRTP_LSB) |
335 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRAS <<
336 SDR_CTRLGRP_DRAMTIMING3_TRAS_LSB) |
337 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRC <<
338 SDR_CTRLGRP_DRAMTIMING3_TRC_LSB) |
339 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TMRD <<
340 SDR_CTRLGRP_DRAMTIMING3_TMRD_LSB) |
341 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TCCD <<
342 SDR_CTRLGRP_DRAMTIMING3_TCCD_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500343
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200344 const u32 dram_timing4 =
345 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING4_SELFRFSHEXIT <<
346 SDR_CTRLGRP_DRAMTIMING4_SELFRFSHEXIT_LSB) |
347 (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING4_PWRDOWNEXIT <<
348 SDR_CTRLGRP_DRAMTIMING4_PWRDOWNEXIT_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500349
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200350 const u32 lowpwr_timing =
351 (CONFIG_HPS_SDR_CTRLCFG_LOWPWRTIMING_AUTOPDCYCLES <<
352 SDR_CTRLGRP_LOWPWRTIMING_AUTOPDCYCLES_LSB) |
353 (CONFIG_HPS_SDR_CTRLCFG_LOWPWRTIMING_CLKDISABLECYCLES <<
354 SDR_CTRLGRP_LOWPWRTIMING_CLKDISABLECYCLES_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500355
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200356 debug("Configuring DRAMTIMING1\n");
357 writel(dram_timing1, &sdr_ctrl->dram_timing1);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500358
Dinh Nguyen429642c2015-06-02 22:52:48 -0500359 debug("Configuring DRAMTIMING2\n");
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200360 writel(dram_timing2, &sdr_ctrl->dram_timing2);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500361
Dinh Nguyen429642c2015-06-02 22:52:48 -0500362 debug("Configuring DRAMTIMING3\n");
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200363 writel(dram_timing3, &sdr_ctrl->dram_timing3);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500364
Dinh Nguyen429642c2015-06-02 22:52:48 -0500365 debug("Configuring DRAMTIMING4\n");
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200366 writel(dram_timing4, &sdr_ctrl->dram_timing4);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500367
Dinh Nguyen429642c2015-06-02 22:52:48 -0500368 debug("Configuring LOWPWRTIMING\n");
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200369 writel(lowpwr_timing, &sdr_ctrl->lowpwr_timing);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500370}
371
372static void set_sdr_addr_rw(void)
373{
Dinh Nguyen429642c2015-06-02 22:52:48 -0500374 /*
375 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500376 * Set SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB to
377 * log2(number of chip select bits). Since there's only
378 * 1 or 2 chip selects, log2(1) => 0, and log2(2) => 1,
379 * which is the same as "chip selects" - 1.
380 */
Marek Vasut820b0d92015-08-01 19:50:56 +0200381 const int rows = get_errata_rows();
382 const u32 dram_addrw =
383 (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS <<
384 SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB) |
385 (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB) |
386 (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS <<
387 SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB) |
388 ((CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS - 1) <<
Dinh Nguyen429642c2015-06-02 22:52:48 -0500389 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB);
Marek Vasut820b0d92015-08-01 19:50:56 +0200390 debug("Configuring DRAMADDRW\n");
391 writel(dram_addrw, &sdr_ctrl->dram_addrw);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500392}
393
394static void set_sdr_static_cfg(void)
395{
Marek Vasut9d64f192015-08-01 20:04:19 +0200396 const u32 static_cfg =
397 (CONFIG_HPS_SDR_CTRLCFG_STATICCFG_MEMBL <<
398 SDR_CTRLGRP_STATICCFG_MEMBL_LSB) |
399 (CONFIG_HPS_SDR_CTRLCFG_STATICCFG_USEECCASDATA <<
Dinh Nguyen429642c2015-06-02 22:52:48 -0500400 SDR_CTRLGRP_STATICCFG_USEECCASDATA_LSB);
Marek Vasut9d64f192015-08-01 20:04:19 +0200401
402 debug("Configuring STATICCFG\n");
403 writel(static_cfg, &sdr_ctrl->static_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500404}
405
406static void set_sdr_fifo_cfg(void)
407{
Marek Vasutf904a862015-08-01 20:04:33 +0200408 const u32 fifo_cfg =
409 (CONFIG_HPS_SDR_CTRLCFG_FIFOCFG_SYNCMODE <<
410 SDR_CTRLGRP_FIFOCFG_SYNCMODE_LSB) |
411 (CONFIG_HPS_SDR_CTRLCFG_FIFOCFG_INCSYNC <<
Dinh Nguyen429642c2015-06-02 22:52:48 -0500412 SDR_CTRLGRP_FIFOCFG_INCSYNC_LSB);
Marek Vasutf904a862015-08-01 20:04:33 +0200413
414 debug("Configuring FIFOCFG\n");
415 writel(fifo_cfg, &sdr_ctrl->fifo_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500416}
417
418static void set_sdr_mp_weight(void)
419{
420 debug("Configuring MPWEIGHT_MPWEIGHT_0\n");
421 clrsetbits_le32(&sdr_ctrl->mp_weight0,
422 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_0_STATICWEIGHT_31_0_MASK,
423 CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_0_STATICWEIGHT_31_0 <<
424 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_0_STATICWEIGHT_31_0_LSB);
425
426 clrsetbits_le32(&sdr_ctrl->mp_weight1,
427 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_STATICWEIGHT_49_32_MASK,
428 CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_1_STATICWEIGHT_49_32 <<
429 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_STATICWEIGHT_49_32_LSB);
430
431 clrsetbits_le32(&sdr_ctrl->mp_weight1,
432 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_SUMOFWEIGHTS_13_0_MASK,
433 CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_1_SUMOFWEIGHT_13_0 <<
434 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_SUMOFWEIGHTS_13_0_LSB);
435
436 clrsetbits_le32(&sdr_ctrl->mp_weight2,
437 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_2_SUMOFWEIGHTS_45_14_MASK,
438 CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_2_SUMOFWEIGHT_45_14 <<
439 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_2_SUMOFWEIGHTS_45_14_LSB);
440
441 clrsetbits_le32(&sdr_ctrl->mp_weight3,
442 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_3_SUMOFWEIGHTS_63_46_MASK,
443 CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_3_SUMOFWEIGHT_63_46 <<
444 SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_3_SUMOFWEIGHTS_63_46_LSB);
445}
446
447static void set_sdr_mp_pacing(void)
448{
449 debug("Configuring MPPACING_MPPACING_0\n");
450 clrsetbits_le32(&sdr_ctrl->mp_pacing0,
451 SDR_CTRLGRP_MPPACING_MPPACING_0_THRESHOLD1_31_0_MASK,
452 CONFIG_HPS_SDR_CTRLCFG_MPPACING_0_THRESHOLD1_31_0 <<
453 SDR_CTRLGRP_MPPACING_MPPACING_0_THRESHOLD1_31_0_LSB);
454
455 clrsetbits_le32(&sdr_ctrl->mp_pacing1,
456 SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD1_59_32_MASK,
457 CONFIG_HPS_SDR_CTRLCFG_MPPACING_1_THRESHOLD1_59_32 <<
458 SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD1_59_32_LSB);
459
460 clrsetbits_le32(&sdr_ctrl->mp_pacing1,
461 SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD2_3_0_MASK,
462 CONFIG_HPS_SDR_CTRLCFG_MPPACING_1_THRESHOLD2_3_0 <<
463 SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD2_3_0_LSB);
464
465 clrsetbits_le32(&sdr_ctrl->mp_pacing2,
466 SDR_CTRLGRP_MPPACING_MPPACING_2_THRESHOLD2_35_4_MASK,
467 CONFIG_HPS_SDR_CTRLCFG_MPPACING_2_THRESHOLD2_35_4 <<
468 SDR_CTRLGRP_MPPACING_MPPACING_2_THRESHOLD2_35_4_LSB);
469
470 clrsetbits_le32(&sdr_ctrl->mp_pacing3,
471 SDR_CTRLGRP_MPPACING_MPPACING_3_THRESHOLD2_59_36_MASK,
472 CONFIG_HPS_SDR_CTRLCFG_MPPACING_3_THRESHOLD2_59_36 <<
473 SDR_CTRLGRP_MPPACING_MPPACING_3_THRESHOLD2_59_36_LSB);
474}
475
476static void set_sdr_mp_threshold(void)
477{
478 debug("Configuring MPTHRESHOLDRST_MPTHRESHOLDRST_0\n");
479 clrsetbits_le32(&sdr_ctrl->mp_threshold0,
480 SDR_CTRLGRP_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0_MASK,
481 CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0 <<
482 SDR_CTRLGRP_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0_LSB);
483
484 clrsetbits_le32(&sdr_ctrl->mp_threshold1,
485 SDR_CTRLGRP_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32_MASK,
Marek Vasut452d6392015-07-09 01:47:56 +0200486 CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32 <<
Dinh Nguyen429642c2015-06-02 22:52:48 -0500487 SDR_CTRLGRP_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32_LSB);
488
489 clrsetbits_le32(&sdr_ctrl->mp_threshold2,
490 SDR_CTRLGRP_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64_MASK,
491 CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64 <<
492 SDR_CTRLGRP_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64_LSB);
493}
494
495
496/* Function to initialize SDRAM MMR */
497unsigned sdram_mmr_init_full(unsigned int sdr_phy_reg)
498{
499 unsigned long reg_value;
500 unsigned long status = 0;
501
502#if defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS) && \
503defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS) && \
504defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS) && \
505defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS) && \
506defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS)
507
508 writel(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS,
509 &sysmgr_regs->iswgrp_handoff[4]);
510#endif
511 set_sdr_ctrlcfg();
Marek Vasut6e9af9b2015-08-01 19:45:24 +0200512 set_sdr_dram_timing();
Dinh Nguyen429642c2015-06-02 22:52:48 -0500513 set_sdr_addr_rw();
514
515 debug("Configuring DRAMIFWIDTH\n");
516 clrsetbits_le32(&sdr_ctrl->dram_if_width,
517 SDR_CTRLGRP_DRAMIFWIDTH_IFWIDTH_MASK,
518 CONFIG_HPS_SDR_CTRLCFG_DRAMIFWIDTH_IFWIDTH <<
519 SDR_CTRLGRP_DRAMIFWIDTH_IFWIDTH_LSB);
520
521 debug("Configuring DRAMDEVWIDTH\n");
522 clrsetbits_le32(&sdr_ctrl->dram_dev_width,
523 SDR_CTRLGRP_DRAMDEVWIDTH_DEVWIDTH_MASK,
524 CONFIG_HPS_SDR_CTRLCFG_DRAMDEVWIDTH_DEVWIDTH <<
525 SDR_CTRLGRP_DRAMDEVWIDTH_DEVWIDTH_LSB);
526
527 debug("Configuring LOWPWREQ\n");
528 clrsetbits_le32(&sdr_ctrl->lowpwr_eq,
529 SDR_CTRLGRP_LOWPWREQ_SELFRFSHMASK_MASK,
530 CONFIG_HPS_SDR_CTRLCFG_LOWPWREQ_SELFRFSHMASK <<
531 SDR_CTRLGRP_LOWPWREQ_SELFRFSHMASK_LSB);
532
533 debug("Configuring DRAMINTR\n");
534 clrsetbits_le32(&sdr_ctrl->dram_intr, SDR_CTRLGRP_DRAMINTR_INTREN_MASK,
535 CONFIG_HPS_SDR_CTRLCFG_DRAMINTR_INTREN <<
536 SDR_CTRLGRP_DRAMINTR_INTREN_LSB);
537
538 set_sdr_static_cfg();
539
540 debug("Configuring CTRLWIDTH\n");
541 clrsetbits_le32(&sdr_ctrl->ctrl_width,
542 SDR_CTRLGRP_CTRLWIDTH_CTRLWIDTH_MASK,
543 CONFIG_HPS_SDR_CTRLCFG_CTRLWIDTH_CTRLWIDTH <<
544 SDR_CTRLGRP_CTRLWIDTH_CTRLWIDTH_LSB);
545
546 debug("Configuring PORTCFG\n");
547 clrsetbits_le32(&sdr_ctrl->port_cfg, SDR_CTRLGRP_PORTCFG_AUTOPCHEN_MASK,
548 CONFIG_HPS_SDR_CTRLCFG_PORTCFG_AUTOPCHEN <<
549 SDR_CTRLGRP_PORTCFG_AUTOPCHEN_LSB);
550
551 set_sdr_fifo_cfg();
552
553 debug("Configuring MPPRIORITY\n");
554 clrsetbits_le32(&sdr_ctrl->mp_priority,
555 SDR_CTRLGRP_MPPRIORITY_USERPRIORITY_MASK,
556 CONFIG_HPS_SDR_CTRLCFG_MPPRIORITY_USERPRIORITY <<
557 SDR_CTRLGRP_MPPRIORITY_USERPRIORITY_LSB);
558
559 set_sdr_mp_weight();
560 set_sdr_mp_pacing();
561 set_sdr_mp_threshold();
562
563 debug("Configuring PHYCTRL_PHYCTRL_0\n");
564 setbits_le32(&sdr_ctrl->phy_ctrl0,
565 CONFIG_HPS_SDR_CTRLCFG_PHYCTRL_PHYCTRL_0);
566
567 debug("Configuring CPORTWIDTH\n");
568 clrsetbits_le32(&sdr_ctrl->cport_width,
569 SDR_CTRLGRP_CPORTWIDTH_CMDPORTWIDTH_MASK,
570 CONFIG_HPS_SDR_CTRLCFG_CPORTWIDTH_CPORTWIDTH <<
571 SDR_CTRLGRP_CPORTWIDTH_CMDPORTWIDTH_LSB);
572 debug(" Write - Address ");
573 debug("0x%08x Data 0x%08x\n",
574 (unsigned)(&sdr_ctrl->cport_width),
575 (unsigned)reg_value);
576 reg_value = readl(&sdr_ctrl->cport_width);
577 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
578
579 debug("Configuring CPORTWMAP\n");
580 clrsetbits_le32(&sdr_ctrl->cport_wmap,
581 SDR_CTRLGRP_CPORTWMAP_CPORTWFIFOMAP_MASK,
582 CONFIG_HPS_SDR_CTRLCFG_CPORTWMAP_CPORTWMAP <<
583 SDR_CTRLGRP_CPORTWMAP_CPORTWFIFOMAP_LSB);
584 debug(" Write - Address ");
585 debug("0x%08x Data 0x%08x\n",
586 (unsigned)(&sdr_ctrl->cport_wmap),
587 (unsigned)reg_value);
588 reg_value = readl(&sdr_ctrl->cport_wmap);
589 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
590
591 debug("Configuring CPORTRMAP\n");
592 clrsetbits_le32(&sdr_ctrl->cport_rmap,
593 SDR_CTRLGRP_CPORTRMAP_CPORTRFIFOMAP_MASK,
594 CONFIG_HPS_SDR_CTRLCFG_CPORTRMAP_CPORTRMAP <<
595 SDR_CTRLGRP_CPORTRMAP_CPORTRFIFOMAP_LSB);
596 debug(" Write - Address ");
597 debug("0x%08x Data 0x%08x\n",
598 (unsigned)(&sdr_ctrl->cport_rmap),
599 (unsigned)reg_value);
600 reg_value = readl(&sdr_ctrl->cport_rmap);
601 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
602
603 debug("Configuring RFIFOCMAP\n");
604 clrsetbits_le32(&sdr_ctrl->rfifo_cmap,
605 SDR_CTRLGRP_RFIFOCMAP_RFIFOCPORTMAP_MASK,
606 CONFIG_HPS_SDR_CTRLCFG_RFIFOCMAP_RFIFOCMAP <<
607 SDR_CTRLGRP_RFIFOCMAP_RFIFOCPORTMAP_LSB);
608 debug(" Write - Address ");
609 debug("0x%08x Data 0x%08x\n",
610 (unsigned)(&sdr_ctrl->rfifo_cmap),
611 (unsigned)reg_value);
612 reg_value = readl(&sdr_ctrl->rfifo_cmap);
613 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
614
615 debug("Configuring WFIFOCMAP\n");
616 reg_value = readl(&sdr_ctrl->wfifo_cmap);
617 clrsetbits_le32(&sdr_ctrl->wfifo_cmap,
618 SDR_CTRLGRP_WFIFOCMAP_WFIFOCPORTMAP_MASK,
619 CONFIG_HPS_SDR_CTRLCFG_WFIFOCMAP_WFIFOCMAP <<
620 SDR_CTRLGRP_WFIFOCMAP_WFIFOCPORTMAP_LSB);
621 debug(" Write - Address ");
622 debug("0x%08x Data 0x%08x\n",
623 (unsigned)(&sdr_ctrl->wfifo_cmap),
624 (unsigned)reg_value);
625 reg_value = readl(&sdr_ctrl->wfifo_cmap);
626 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
627
628 debug("Configuring CPORTRDWR\n");
629 clrsetbits_le32(&sdr_ctrl->cport_rdwr,
630 SDR_CTRLGRP_CPORTRDWR_CPORTRDWR_MASK,
631 CONFIG_HPS_SDR_CTRLCFG_CPORTRDWR_CPORTRDWR <<
632 SDR_CTRLGRP_CPORTRDWR_CPORTRDWR_LSB);
633 debug(" Write - Address ");
634 debug("0x%08x Data 0x%08x\n",
635 (unsigned)(&sdr_ctrl->cport_rdwr),
636 (unsigned)reg_value);
637 reg_value = readl(&sdr_ctrl->cport_rdwr);
638 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
639
640 debug("Configuring DRAMODT\n");
641 clrsetbits_le32(&sdr_ctrl->dram_odt,
642 SDR_CTRLGRP_DRAMODT_READ_MASK,
643 CONFIG_HPS_SDR_CTRLCFG_DRAMODT_READ <<
644 SDR_CTRLGRP_DRAMODT_READ_LSB);
645
646 clrsetbits_le32(&sdr_ctrl->dram_odt,
647 SDR_CTRLGRP_DRAMODT_WRITE_MASK,
648 CONFIG_HPS_SDR_CTRLCFG_DRAMODT_WRITE <<
649 SDR_CTRLGRP_DRAMODT_WRITE_LSB);
650
651 /* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */
652 writel(CONFIG_HPS_SDR_CTRLCFG_FPGAPORTRST,
653 &sysmgr_regs->iswgrp_handoff[3]);
654
655 /* only enable if the FPGA is programmed */
656 if (fpgamgr_test_fpga_ready()) {
657 if (sdram_write_verify(&sdr_ctrl->fpgaport_rst,
658 CONFIG_HPS_SDR_CTRLCFG_FPGAPORTRST) == 1) {
659 status = 1;
660 return 1;
661 }
662 }
663
664 /* Restore the SDR PHY Register if valid */
665 if (sdr_phy_reg != 0xffffffff)
666 writel(sdr_phy_reg, &sdr_ctrl->phy_ctrl0);
667
668/***** Final step - apply configuration changes *****/
669 debug("Configuring STATICCFG_\n");
670 clrsetbits_le32(&sdr_ctrl->static_cfg, SDR_CTRLGRP_STATICCFG_APPLYCFG_MASK,
671 1 << SDR_CTRLGRP_STATICCFG_APPLYCFG_LSB);
672 debug(" Write - Address ");
673 debug("0x%08x Data 0x%08x\n",
674 (unsigned)(&sdr_ctrl->static_cfg),
675 (unsigned)reg_value);
676 reg_value = readl(&sdr_ctrl->static_cfg);
677 debug(" Read value without verify 0x%08x\n", (unsigned)reg_value);
678
679 sdram_set_protection_config(0, sdram_calculate_size());
680
681 sdram_dump_protection_config();
682
683 return status;
684}
685
686/*
687 * To calculate SDRAM device size based on SDRAM controller parameters.
688 * Size is specified in bytes.
689 *
690 * NOTE:
691 * This function is compiled and linked into the preloader and
692 * Uboot (there may be others). So if this function changes, the Preloader
693 * and UBoot must be updated simultaneously.
694 */
695unsigned long sdram_calculate_size(void)
696{
697 unsigned long temp;
698 unsigned long row, bank, col, cs, width;
699
700 temp = readl(&sdr_ctrl->dram_addrw);
701 col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
702 SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
703
704 /* SDRAM Failure When Accessing Non-Existent Memory
705 * Use ROWBITS from Quartus/QSys to calculate SDRAM size
706 * since the FB specifies we modify ROWBITs to work around SDRAM
707 * controller issue.
708 *
709 * If the stored handoff value for rows is 0, it probably means
710 * the preloader is older than UBoot. Use the
711 * #define from the SOCEDS Tools per Crucible review
712 * uboot-socfpga-204. Note that this is not a supported
713 * configuration and is not tested. The customer
714 * should be using preloader and uboot built from the
715 * same tag.
716 */
717 row = readl(&sysmgr_regs->iswgrp_handoff[4]);
718 if (row == 0)
719 row = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
720 /* If the stored handoff value for rows is greater than
721 * the field width in the sdr.dramaddrw register then
722 * something is very wrong. Revert to using the the #define
723 * value handed off by the SOCEDS tool chain instead of
724 * using a broken value.
725 */
726 if (row > 31)
727 row = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
728
729 bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
730 SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
731
732 /* SDRAM Failure When Accessing Non-Existent Memory
733 * Use CSBITs from Quartus/QSys to calculate SDRAM size
734 * since the FB specifies we modify CSBITs to work around SDRAM
735 * controller issue.
736 */
737 cs = (temp & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
738 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB;
739 cs += 1;
740
741 cs = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS;
742
743 width = readl(&sdr_ctrl->dram_if_width);
744 /* ECC would not be calculated as its not addressible */
745 if (width == SDRAM_WIDTH_32BIT_WITH_ECC)
746 width = 32;
747 if (width == SDRAM_WIDTH_16BIT_WITH_ECC)
748 width = 16;
749
750 /* calculate the SDRAM size base on this info */
751 temp = 1 << (row + bank + col);
752 temp = temp * cs * (width / 8);
753
754 debug("sdram_calculate_memory returns %ld\n", temp);
755
756 return temp;
757}