blob: 435f42bc0ab06525effd5a116bdbe6f06665ced2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dinh Nguyen429642c2015-06-02 22:52:48 -05002/*
3 * Copyright Altera Corporation (C) 2014-2015
Dinh Nguyen429642c2015-06-02 22:52:48 -05004 */
5#include <common.h>
Simon Goldschmidt24910c32019-04-16 22:04:39 +02006#include <dm.h>
Marek Vasut1b1cc102015-08-01 22:25:29 +02007#include <errno.h>
Dinh Nguyen429642c2015-06-02 22:52:48 -05008#include <div64.h>
Simon Goldschmidt24910c32019-04-16 22:04:39 +02009#include <ram.h>
10#include <reset.h>
Dinh Nguyen429642c2015-06-02 22:52:48 -050011#include <watchdog.h>
12#include <asm/arch/fpga_manager.h>
Simon Goldschmidt24910c32019-04-16 22:04:39 +020013#include <asm/arch/reset_manager.h>
Dinh Nguyen429642c2015-06-02 22:52:48 -050014#include <asm/arch/sdram.h>
Dinh Nguyen429642c2015-06-02 22:52:48 -050015#include <asm/arch/system_manager.h>
16#include <asm/io.h>
17
Simon Goldschmidt24910c32019-04-16 22:04:39 +020018#include "sequencer.h"
19
20#ifdef CONFIG_SPL_BUILD
21
22struct altera_gen5_sdram_priv {
23 struct ram_info info;
24};
25
26struct altera_gen5_sdram_platdata {
27 struct socfpga_sdr *sdr;
28};
29
Marek Vasute08c5592015-07-26 10:37:54 +020030struct sdram_prot_rule {
Marek Vasut6772cd92015-08-01 23:12:11 +020031 u32 sdram_start; /* SDRAM start address */
32 u32 sdram_end; /* SDRAM end address */
Marek Vasute08c5592015-07-26 10:37:54 +020033 u32 rule; /* SDRAM protection rule number: 0-19 */
34 int valid; /* Rule valid or not? 1 - valid, 0 not*/
35
36 u32 security;
37 u32 portmask;
38 u32 result;
39 u32 lo_prot_id;
40 u32 hi_prot_id;
41};
42
Simon Goldschmidt24910c32019-04-16 22:04:39 +020043static unsigned long sdram_calculate_size(struct socfpga_sdr_ctrl *sdr_ctrl);
Dinh Nguyen429642c2015-06-02 22:52:48 -050044
Marek Vasut724c50f2015-08-01 19:20:19 +020045/**
46 * get_errata_rows() - Up the number of DRAM rows to cover entire address space
Marek Vasut3a079112015-08-01 21:16:20 +020047 * @cfg: SDRAM controller configuration data
Marek Vasut724c50f2015-08-01 19:20:19 +020048 *
49 * SDRAM Failure happens when accessing non-existent memory. Artificially
50 * increase the number of rows so that the memory controller thinks it has
51 * 4GB of RAM. This function returns such amount of rows.
52 */
Marek Vasut32ada572015-08-01 21:35:18 +020053static int get_errata_rows(const struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -050054{
Marek Vasut724c50f2015-08-01 19:20:19 +020055 /* Define constant for 4G memory - used for SDRAM errata workaround */
56#define MEMSIZE_4G (4ULL * 1024ULL * 1024ULL * 1024ULL)
57 const unsigned long long memsize = MEMSIZE_4G;
Marek Vasut3a079112015-08-01 21:16:20 +020058 const unsigned int cs =
59 ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
60 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
61 const unsigned int rows =
62 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
63 SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
64 const unsigned int banks =
65 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
66 SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
67 const unsigned int cols =
68 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
69 SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
Marek Vasut724c50f2015-08-01 19:20:19 +020070 const unsigned int width = 8;
71
Dinh Nguyen429642c2015-06-02 22:52:48 -050072 unsigned long long newrows;
Marek Vasut724c50f2015-08-01 19:20:19 +020073 int bits, inewrowslog2;
Dinh Nguyen429642c2015-06-02 22:52:48 -050074
75 debug("workaround rows - memsize %lld\n", memsize);
76 debug("workaround rows - cs %d\n", cs);
77 debug("workaround rows - width %d\n", width);
78 debug("workaround rows - rows %d\n", rows);
79 debug("workaround rows - banks %d\n", banks);
80 debug("workaround rows - cols %d\n", cols);
81
Marek Vasut186880e2015-08-01 18:54:34 +020082 newrows = lldiv(memsize, cs * (width / 8));
Dinh Nguyen429642c2015-06-02 22:52:48 -050083 debug("rows workaround - term1 %lld\n", newrows);
84
Marek Vasut186880e2015-08-01 18:54:34 +020085 newrows = lldiv(newrows, (1 << banks) * (1 << cols));
Dinh Nguyen429642c2015-06-02 22:52:48 -050086 debug("rows workaround - term2 %lld\n", newrows);
87
Marek Vasut186880e2015-08-01 18:54:34 +020088 /*
89 * Compute the hamming weight - same as number of bits set.
Dinh Nguyen429642c2015-06-02 22:52:48 -050090 * Need to see if result is ordinal power of 2 before
91 * attempting log2 of result.
92 */
Marek Vasut2fda5062015-08-01 18:46:55 +020093 bits = generic_hweight32(newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -050094
95 debug("rows workaround - bits %d\n", bits);
96
97 if (bits != 1) {
98 printf("SDRAM workaround failed, bits set %d\n", bits);
99 return rows;
100 }
101
102 if (newrows > UINT_MAX) {
103 printf("SDRAM workaround rangecheck failed, %lld\n", newrows);
104 return rows;
105 }
106
Marek Vasut186880e2015-08-01 18:54:34 +0200107 inewrowslog2 = __ilog2(newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500108
Marek Vasut186880e2015-08-01 18:54:34 +0200109 debug("rows workaround - ilog2 %d, %lld\n", inewrowslog2, newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500110
111 if (inewrowslog2 == -1) {
Marek Vasut186880e2015-08-01 18:54:34 +0200112 printf("SDRAM workaround failed, newrows %lld\n", newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500113 return rows;
114 }
115
116 return inewrowslog2;
117}
118
119/* SDRAM protection rules vary from 0-19, a total of 20 rules. */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200120static void sdram_set_rule(struct socfpga_sdr_ctrl *sdr_ctrl,
121 struct sdram_prot_rule *prule)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500122{
Marek Vasut6772cd92015-08-01 23:12:11 +0200123 u32 lo_addr_bits;
124 u32 hi_addr_bits;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500125 int ruleno = prule->rule;
126
127 /* Select the rule */
128 writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
129
130 /* Obtain the address bits */
Marek Vasut7fce5bc2015-08-01 22:40:48 +0200131 lo_addr_bits = prule->sdram_start >> 20ULL;
Marek Vasut12361a22016-04-04 17:52:21 +0200132 hi_addr_bits = (prule->sdram_end - 1) >> 20ULL;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500133
Marek Vasut6772cd92015-08-01 23:12:11 +0200134 debug("sdram set rule start %x, %d\n", lo_addr_bits,
Dinh Nguyen429642c2015-06-02 22:52:48 -0500135 prule->sdram_start);
Marek Vasut6772cd92015-08-01 23:12:11 +0200136 debug("sdram set rule end %x, %d\n", hi_addr_bits,
Dinh Nguyen429642c2015-06-02 22:52:48 -0500137 prule->sdram_end);
138
139 /* Set rule addresses */
140 writel(lo_addr_bits | (hi_addr_bits << 12), &sdr_ctrl->prot_rule_addr);
141
142 /* Set rule protection ids */
143 writel(prule->lo_prot_id | (prule->hi_prot_id << 12),
144 &sdr_ctrl->prot_rule_id);
145
146 /* Set the rule data */
147 writel(prule->security | (prule->valid << 2) |
148 (prule->portmask << 3) | (prule->result << 13),
149 &sdr_ctrl->prot_rule_data);
150
151 /* write the rule */
Marek Vasut7fce5bc2015-08-01 22:40:48 +0200152 writel(ruleno | (1 << 5), &sdr_ctrl->prot_rule_rdwr);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500153
154 /* Set rule number to 0 by default */
155 writel(0, &sdr_ctrl->prot_rule_rdwr);
156}
157
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200158static void sdram_get_rule(struct socfpga_sdr_ctrl *sdr_ctrl,
159 struct sdram_prot_rule *prule)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500160{
Marek Vasut91144072015-08-01 23:21:23 +0200161 u32 addr;
162 u32 id;
163 u32 data;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500164 int ruleno = prule->rule;
165
166 /* Read the rule */
167 writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
Marek Vasut91144072015-08-01 23:21:23 +0200168 writel(ruleno | (1 << 6), &sdr_ctrl->prot_rule_rdwr);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500169
170 /* Get the addresses */
171 addr = readl(&sdr_ctrl->prot_rule_addr);
172 prule->sdram_start = (addr & 0xFFF) << 20;
173 prule->sdram_end = ((addr >> 12) & 0xFFF) << 20;
174
175 /* Get the configured protection IDs */
176 id = readl(&sdr_ctrl->prot_rule_id);
177 prule->lo_prot_id = id & 0xFFF;
178 prule->hi_prot_id = (id >> 12) & 0xFFF;
179
180 /* Get protection data */
181 data = readl(&sdr_ctrl->prot_rule_data);
182
183 prule->security = data & 0x3;
184 prule->valid = (data >> 2) & 0x1;
185 prule->portmask = (data >> 3) & 0x3FF;
186 prule->result = (data >> 13) & 0x1;
187}
188
Marek Vasut6772cd92015-08-01 23:12:11 +0200189static void
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200190sdram_set_protection_config(struct socfpga_sdr_ctrl *sdr_ctrl,
191 const u32 sdram_start, const u32 sdram_end)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500192{
193 struct sdram_prot_rule rule;
194 int rules;
195
196 /* Start with accepting all SDRAM transaction */
197 writel(0x0, &sdr_ctrl->protport_default);
198
199 /* Clear all protection rules for warm boot case */
Marek Vasut7fce5bc2015-08-01 22:40:48 +0200200 memset(&rule, 0, sizeof(rule));
Dinh Nguyen429642c2015-06-02 22:52:48 -0500201
202 for (rules = 0; rules < 20; rules++) {
203 rule.rule = rules;
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200204 sdram_set_rule(sdr_ctrl, &rule);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500205 }
206
207 /* new rule: accept SDRAM */
208 rule.sdram_start = sdram_start;
209 rule.sdram_end = sdram_end;
210 rule.lo_prot_id = 0x0;
211 rule.hi_prot_id = 0xFFF;
212 rule.portmask = 0x3FF;
213 rule.security = 0x3;
214 rule.result = 0;
215 rule.valid = 1;
216 rule.rule = 0;
217
218 /* set new rule */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200219 sdram_set_rule(sdr_ctrl, &rule);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500220
221 /* default rule: reject everything */
222 writel(0x3ff, &sdr_ctrl->protport_default);
223}
224
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200225static void sdram_dump_protection_config(struct socfpga_sdr_ctrl *sdr_ctrl)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500226{
227 struct sdram_prot_rule rule;
228 int rules;
229
230 debug("SDRAM Prot rule, default %x\n",
231 readl(&sdr_ctrl->protport_default));
232
233 for (rules = 0; rules < 20; rules++) {
Marek Vasut42aa46d2015-12-29 09:38:52 +0100234 rule.rule = rules;
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200235 sdram_get_rule(sdr_ctrl, &rule);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500236 debug("Rule %d, rules ...\n", rules);
Marek Vasut6772cd92015-08-01 23:12:11 +0200237 debug(" sdram start %x\n", rule.sdram_start);
238 debug(" sdram end %x\n", rule.sdram_end);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500239 debug(" low prot id %d, hi prot id %d\n",
240 rule.lo_prot_id,
241 rule.hi_prot_id);
242 debug(" portmask %x\n", rule.portmask);
243 debug(" security %d\n", rule.security);
244 debug(" result %d\n", rule.result);
245 debug(" valid %d\n", rule.valid);
246 }
247}
248
Marek Vasut116d88f2015-08-01 22:26:11 +0200249/**
250 * sdram_write_verify() - write to register and verify the write.
251 * @addr: Register address
252 * @val: Value to be written and verified
253 *
254 * This function writes to a register, reads back the value and compares
255 * the result with the written value to check if the data match.
256 */
257static unsigned sdram_write_verify(const u32 *addr, const u32 val)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500258{
Marek Vasut116d88f2015-08-01 22:26:11 +0200259 u32 rval;
260
261 debug(" Write - Address 0x%p Data 0x%08x\n", addr, val);
262 writel(val, addr);
263
Dinh Nguyen429642c2015-06-02 22:52:48 -0500264 debug(" Read and verify...");
Marek Vasut116d88f2015-08-01 22:26:11 +0200265 rval = readl(addr);
266 if (rval != val) {
267 debug("FAIL - Address 0x%p Expected 0x%08x Data 0x%08x\n",
268 addr, val, rval);
269 return -EINVAL;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500270 }
Marek Vasut116d88f2015-08-01 22:26:11 +0200271
Dinh Nguyen429642c2015-06-02 22:52:48 -0500272 debug("correct!\n");
Dinh Nguyen429642c2015-06-02 22:52:48 -0500273 return 0;
274}
275
Marek Vasutb0d848c2015-08-01 22:28:30 +0200276/**
277 * sdr_get_ctrlcfg() - Get the value of DRAM CTRLCFG register
278 * @cfg: SDRAM controller configuration data
279 *
280 * Return the value of DRAM CTRLCFG register.
281 */
Marek Vasut32ada572015-08-01 21:35:18 +0200282static u32 sdr_get_ctrlcfg(const struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500283{
Marek Vasut3a079112015-08-01 21:16:20 +0200284 const u32 csbits =
285 ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
286 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
287 u32 addrorder =
288 (cfg->ctrl_cfg & SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK) >>
289 SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
290
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200291 u32 ctrl_cfg = cfg->ctrl_cfg;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500292
Marek Vasut82a27642015-08-01 19:33:40 +0200293 /*
294 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500295 * Set the addrorder field of the SDRAM control register
296 * based on the CSBITs setting.
297 */
Marek Vasut3a079112015-08-01 21:16:20 +0200298 if (csbits == 1) {
299 if (addrorder != 0)
Marek Vasut82a27642015-08-01 19:33:40 +0200300 debug("INFO: Changing address order to 0 (chip, row, bank, column)\n");
Marek Vasut3a079112015-08-01 21:16:20 +0200301 addrorder = 0;
302 } else if (csbits == 2) {
303 if (addrorder != 2)
Marek Vasut82a27642015-08-01 19:33:40 +0200304 debug("INFO: Changing address order to 2 (row, chip, bank, column)\n");
Marek Vasut3a079112015-08-01 21:16:20 +0200305 addrorder = 2;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500306 }
307
Marek Vasut3a079112015-08-01 21:16:20 +0200308 ctrl_cfg &= ~SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK;
Marek Vasut82a27642015-08-01 19:33:40 +0200309 ctrl_cfg |= addrorder << SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500310
Marek Vasut1e271e42015-08-01 21:24:31 +0200311 return ctrl_cfg;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500312}
313
Marek Vasutb0d848c2015-08-01 22:28:30 +0200314/**
315 * sdr_get_addr_rw() - Get the value of DRAM ADDRW register
316 * @cfg: SDRAM controller configuration data
317 *
318 * Return the value of DRAM ADDRW register.
319 */
Marek Vasut32ada572015-08-01 21:35:18 +0200320static u32 sdr_get_addr_rw(const struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500321{
Dinh Nguyen429642c2015-06-02 22:52:48 -0500322 /*
323 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500324 * Set SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB to
325 * log2(number of chip select bits). Since there's only
326 * 1 or 2 chip selects, log2(1) => 0, and log2(2) => 1,
327 * which is the same as "chip selects" - 1.
328 */
Marek Vasut3a079112015-08-01 21:16:20 +0200329 const int rows = get_errata_rows(cfg);
330 u32 dram_addrw = cfg->dram_addrw & ~SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK;
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200331
Marek Vasut1e271e42015-08-01 21:24:31 +0200332 return dram_addrw | (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500333}
334
Marek Vasutb81f11c2015-08-01 21:26:55 +0200335/**
336 * sdr_load_regs() - Load SDRAM controller registers
337 * @cfg: SDRAM controller configuration data
338 *
339 * This function loads the register values into the SDRAM controller block.
340 */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200341static void sdr_load_regs(struct socfpga_sdr_ctrl *sdr_ctrl,
342 const struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500343{
Marek Vasut1e271e42015-08-01 21:24:31 +0200344 const u32 ctrl_cfg = sdr_get_ctrlcfg(cfg);
345 const u32 dram_addrw = sdr_get_addr_rw(cfg);
346
Marek Vasut1e271e42015-08-01 21:24:31 +0200347 debug("\nConfiguring CTRLCFG\n");
348 writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
Marek Vasut71c1a002015-08-01 21:21:21 +0200349
350 debug("Configuring DRAMTIMING1\n");
351 writel(cfg->dram_timing1, &sdr_ctrl->dram_timing1);
352
353 debug("Configuring DRAMTIMING2\n");
354 writel(cfg->dram_timing2, &sdr_ctrl->dram_timing2);
355
356 debug("Configuring DRAMTIMING3\n");
357 writel(cfg->dram_timing3, &sdr_ctrl->dram_timing3);
358
359 debug("Configuring DRAMTIMING4\n");
360 writel(cfg->dram_timing4, &sdr_ctrl->dram_timing4);
361
362 debug("Configuring LOWPWRTIMING\n");
363 writel(cfg->lowpwr_timing, &sdr_ctrl->lowpwr_timing);
364
Marek Vasut1e271e42015-08-01 21:24:31 +0200365 debug("Configuring DRAMADDRW\n");
366 writel(dram_addrw, &sdr_ctrl->dram_addrw);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500367
368 debug("Configuring DRAMIFWIDTH\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200369 writel(cfg->dram_if_width, &sdr_ctrl->dram_if_width);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500370
371 debug("Configuring DRAMDEVWIDTH\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200372 writel(cfg->dram_dev_width, &sdr_ctrl->dram_dev_width);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500373
374 debug("Configuring LOWPWREQ\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200375 writel(cfg->lowpwr_eq, &sdr_ctrl->lowpwr_eq);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500376
377 debug("Configuring DRAMINTR\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200378 writel(cfg->dram_intr, &sdr_ctrl->dram_intr);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500379
Marek Vasut71c1a002015-08-01 21:21:21 +0200380 debug("Configuring STATICCFG\n");
381 writel(cfg->static_cfg, &sdr_ctrl->static_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500382
383 debug("Configuring CTRLWIDTH\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200384 writel(cfg->ctrl_width, &sdr_ctrl->ctrl_width);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500385
386 debug("Configuring PORTCFG\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200387 writel(cfg->port_cfg, &sdr_ctrl->port_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500388
Marek Vasut71c1a002015-08-01 21:21:21 +0200389 debug("Configuring FIFOCFG\n");
390 writel(cfg->fifo_cfg, &sdr_ctrl->fifo_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500391
392 debug("Configuring MPPRIORITY\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200393 writel(cfg->mp_priority, &sdr_ctrl->mp_priority);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500394
Marek Vasut71c1a002015-08-01 21:21:21 +0200395 debug("Configuring MPWEIGHT_MPWEIGHT_0\n");
396 writel(cfg->mp_weight0, &sdr_ctrl->mp_weight0);
397 writel(cfg->mp_weight1, &sdr_ctrl->mp_weight1);
398 writel(cfg->mp_weight2, &sdr_ctrl->mp_weight2);
399 writel(cfg->mp_weight3, &sdr_ctrl->mp_weight3);
400
401 debug("Configuring MPPACING_MPPACING_0\n");
402 writel(cfg->mp_pacing0, &sdr_ctrl->mp_pacing0);
403 writel(cfg->mp_pacing1, &sdr_ctrl->mp_pacing1);
404 writel(cfg->mp_pacing2, &sdr_ctrl->mp_pacing2);
405 writel(cfg->mp_pacing3, &sdr_ctrl->mp_pacing3);
406
407 debug("Configuring MPTHRESHOLDRST_MPTHRESHOLDRST_0\n");
408 writel(cfg->mp_threshold0, &sdr_ctrl->mp_threshold0);
409 writel(cfg->mp_threshold1, &sdr_ctrl->mp_threshold1);
410 writel(cfg->mp_threshold2, &sdr_ctrl->mp_threshold2);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500411
412 debug("Configuring PHYCTRL_PHYCTRL_0\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200413 writel(cfg->phy_ctrl0, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500414
415 debug("Configuring CPORTWIDTH\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200416 writel(cfg->cport_width, &sdr_ctrl->cport_width);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500417
418 debug("Configuring CPORTWMAP\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200419 writel(cfg->cport_wmap, &sdr_ctrl->cport_wmap);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500420
421 debug("Configuring CPORTRMAP\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200422 writel(cfg->cport_rmap, &sdr_ctrl->cport_rmap);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500423
424 debug("Configuring RFIFOCMAP\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200425 writel(cfg->rfifo_cmap, &sdr_ctrl->rfifo_cmap);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500426
427 debug("Configuring WFIFOCMAP\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200428 writel(cfg->wfifo_cmap, &sdr_ctrl->wfifo_cmap);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500429
430 debug("Configuring CPORTRDWR\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200431 writel(cfg->cport_rdwr, &sdr_ctrl->cport_rdwr);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500432
433 debug("Configuring DRAMODT\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200434 writel(cfg->dram_odt, &sdr_ctrl->dram_odt);
Chin Liang See3ea59512016-09-21 10:25:56 +0800435
436 debug("Configuring EXTRATIME1\n");
437 writel(cfg->extratime1, &sdr_ctrl->extratime1);
Marek Vasutb81f11c2015-08-01 21:26:55 +0200438}
439
Marek Vasut5a4e8ed2015-08-01 22:03:48 +0200440/**
441 * sdram_mmr_init_full() - Function to initialize SDRAM MMR
442 * @sdr_phy_reg: Value of the PHY control register 0
443 *
444 * Initialize the SDRAM MMR.
445 */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200446int sdram_mmr_init_full(struct socfpga_sdr_ctrl *sdr_ctrl,
447 unsigned int sdr_phy_reg)
Marek Vasutb81f11c2015-08-01 21:26:55 +0200448{
Marek Vasut32ada572015-08-01 21:35:18 +0200449 const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
Marek Vasutb81f11c2015-08-01 21:26:55 +0200450 const unsigned int rows =
451 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
452 SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
Marek Vasut116d88f2015-08-01 22:26:11 +0200453 int ret;
Marek Vasutb81f11c2015-08-01 21:26:55 +0200454
Ley Foon Tan3d3a8602019-11-08 10:38:20 +0800455 writel(rows,
456 socfpga_get_sysmgr_addr() + SYSMGR_ISWGRP_HANDOFF_OFFSET(4));
Marek Vasutb81f11c2015-08-01 21:26:55 +0200457
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200458 sdr_load_regs(sdr_ctrl, cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500459
460 /* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */
Ley Foon Tan3d3a8602019-11-08 10:38:20 +0800461 writel(cfg->fpgaport_rst,
462 socfpga_get_sysmgr_addr() + SYSMGR_ISWGRP_HANDOFF_OFFSET(3));
Dinh Nguyen429642c2015-06-02 22:52:48 -0500463
464 /* only enable if the FPGA is programmed */
465 if (fpgamgr_test_fpga_ready()) {
Marek Vasut116d88f2015-08-01 22:26:11 +0200466 ret = sdram_write_verify(&sdr_ctrl->fpgaport_rst,
467 cfg->fpgaport_rst);
468 if (ret)
469 return ret;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500470 }
471
472 /* Restore the SDR PHY Register if valid */
473 if (sdr_phy_reg != 0xffffffff)
474 writel(sdr_phy_reg, &sdr_ctrl->phy_ctrl0);
475
Marek Vasut7697ff72015-08-01 20:58:44 +0200476 /* Final step - apply configuration changes */
477 debug("Configuring STATICCFG\n");
478 clrsetbits_le32(&sdr_ctrl->static_cfg,
479 SDR_CTRLGRP_STATICCFG_APPLYCFG_MASK,
Dinh Nguyen429642c2015-06-02 22:52:48 -0500480 1 << SDR_CTRLGRP_STATICCFG_APPLYCFG_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500481
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200482 sdram_set_protection_config(sdr_ctrl, 0,
483 sdram_calculate_size(sdr_ctrl) - 1);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500484
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200485 sdram_dump_protection_config(sdr_ctrl);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500486
Marek Vasut116d88f2015-08-01 22:26:11 +0200487 return 0;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500488}
489
Marek Vasut1796a092015-08-01 21:47:16 +0200490/**
491 * sdram_calculate_size() - Calculate SDRAM size
Dinh Nguyen429642c2015-06-02 22:52:48 -0500492 *
Marek Vasut1796a092015-08-01 21:47:16 +0200493 * Calculate SDRAM device size based on SDRAM controller parameters.
494 * Size is specified in bytes.
Dinh Nguyen429642c2015-06-02 22:52:48 -0500495 */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200496static unsigned long sdram_calculate_size(struct socfpga_sdr_ctrl *sdr_ctrl)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500497{
498 unsigned long temp;
499 unsigned long row, bank, col, cs, width;
Marek Vasut6d6fbba2015-08-01 21:44:00 +0200500 const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
501 const unsigned int csbits =
502 ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
503 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
504 const unsigned int rowbits =
505 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
506 SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500507
508 temp = readl(&sdr_ctrl->dram_addrw);
509 col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
510 SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
511
Marek Vasut1796a092015-08-01 21:47:16 +0200512 /*
513 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500514 * Use ROWBITS from Quartus/QSys to calculate SDRAM size
515 * since the FB specifies we modify ROWBITs to work around SDRAM
516 * controller issue.
Dinh Nguyen429642c2015-06-02 22:52:48 -0500517 */
Ley Foon Tan3d3a8602019-11-08 10:38:20 +0800518 row = readl(socfpga_get_sysmgr_addr() +
519 SYSMGR_ISWGRP_HANDOFF_OFFSET(4));
Dinh Nguyen429642c2015-06-02 22:52:48 -0500520 if (row == 0)
Marek Vasut6d6fbba2015-08-01 21:44:00 +0200521 row = rowbits;
Marek Vasut1796a092015-08-01 21:47:16 +0200522 /*
523 * If the stored handoff value for rows is greater than
Dinh Nguyen429642c2015-06-02 22:52:48 -0500524 * the field width in the sdr.dramaddrw register then
525 * something is very wrong. Revert to using the the #define
526 * value handed off by the SOCEDS tool chain instead of
527 * using a broken value.
528 */
529 if (row > 31)
Marek Vasut6d6fbba2015-08-01 21:44:00 +0200530 row = rowbits;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500531
532 bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
533 SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
534
Marek Vasut1796a092015-08-01 21:47:16 +0200535 /*
536 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500537 * Use CSBITs from Quartus/QSys to calculate SDRAM size
538 * since the FB specifies we modify CSBITs to work around SDRAM
539 * controller issue.
540 */
Marek Vasut6d6fbba2015-08-01 21:44:00 +0200541 cs = csbits;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500542
543 width = readl(&sdr_ctrl->dram_if_width);
Marek Vasut1796a092015-08-01 21:47:16 +0200544
Dinh Nguyen429642c2015-06-02 22:52:48 -0500545 /* ECC would not be calculated as its not addressible */
546 if (width == SDRAM_WIDTH_32BIT_WITH_ECC)
547 width = 32;
548 if (width == SDRAM_WIDTH_16BIT_WITH_ECC)
549 width = 16;
550
551 /* calculate the SDRAM size base on this info */
552 temp = 1 << (row + bank + col);
553 temp = temp * cs * (width / 8);
554
Marek Vasut1796a092015-08-01 21:47:16 +0200555 debug("%s returns %ld\n", __func__, temp);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500556
557 return temp;
558}
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200559
560static int altera_gen5_sdram_ofdata_to_platdata(struct udevice *dev)
561{
562 struct altera_gen5_sdram_platdata *plat = dev->platdata;
563
564 plat->sdr = (struct socfpga_sdr *)devfdt_get_addr_index(dev, 0);
565 if (!plat->sdr)
566 return -ENODEV;
567
568 return 0;
569}
570
571static int altera_gen5_sdram_probe(struct udevice *dev)
572{
573 int ret;
574 unsigned long sdram_size;
575 struct altera_gen5_sdram_platdata *plat = dev->platdata;
576 struct altera_gen5_sdram_priv *priv = dev_get_priv(dev);
577 struct socfpga_sdr_ctrl *sdr_ctrl = &plat->sdr->sdr_ctrl;
578 struct reset_ctl_bulk resets;
579
580 ret = reset_get_bulk(dev, &resets);
581 if (ret) {
582 dev_err(dev, "Can't get reset: %d\n", ret);
583 return -ENODEV;
584 }
585 reset_deassert_bulk(&resets);
586
587 if (sdram_mmr_init_full(sdr_ctrl, 0xffffffff) != 0) {
588 puts("SDRAM init failed.\n");
589 goto failed;
590 }
591
592 debug("SDRAM: Calibrating PHY\n");
593 /* SDRAM calibration */
594 if (sdram_calibration_full(plat->sdr) == 0) {
595 puts("SDRAM calibration failed.\n");
596 goto failed;
597 }
598
599 sdram_size = sdram_calculate_size(sdr_ctrl);
600 debug("SDRAM: %ld MiB\n", sdram_size >> 20);
601
602 /* Sanity check ensure correct SDRAM size specified */
603 if (get_ram_size(0, sdram_size) != sdram_size) {
604 puts("SDRAM size check failed!\n");
605 goto failed;
606 }
607
608 priv->info.base = 0;
609 priv->info.size = sdram_size;
610
611 return 0;
612
613failed:
614 reset_release_bulk(&resets);
615 return -ENODEV;
616}
617
618static int altera_gen5_sdram_get_info(struct udevice *dev,
619 struct ram_info *info)
620{
621 struct altera_gen5_sdram_priv *priv = dev_get_priv(dev);
622
623 info->base = priv->info.base;
624 info->size = priv->info.size;
625
626 return 0;
627}
628
Simon Goldschmidte8744332019-10-23 22:19:37 +0200629static const struct ram_ops altera_gen5_sdram_ops = {
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200630 .get_info = altera_gen5_sdram_get_info,
631};
632
633static const struct udevice_id altera_gen5_sdram_ids[] = {
634 { .compatible = "altr,sdr-ctl" },
635 { /* sentinel */ }
636};
637
638U_BOOT_DRIVER(altera_gen5_sdram) = {
639 .name = "altr_sdr_ctl",
640 .id = UCLASS_RAM,
641 .of_match = altera_gen5_sdram_ids,
642 .ops = &altera_gen5_sdram_ops,
643 .ofdata_to_platdata = altera_gen5_sdram_ofdata_to_platdata,
644 .platdata_auto_alloc_size = sizeof(struct altera_gen5_sdram_platdata),
645 .probe = altera_gen5_sdram_probe,
646 .priv_auto_alloc_size = sizeof(struct altera_gen5_sdram_priv),
647};
648
649#endif /* CONFIG_SPL_BUILD */