blob: a3b914fdfc0632a66967b15bd92c39a4feff2cad [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 Glass8e16b1e2019-12-28 10:45:05 -07009#include <init.h>
Simon Goldschmidt24910c32019-04-16 22:04:39 +020010#include <ram.h>
11#include <reset.h>
Dinh Nguyen429642c2015-06-02 22:52:48 -050012#include <watchdog.h>
13#include <asm/arch/fpga_manager.h>
Simon Goldschmidt24910c32019-04-16 22:04:39 +020014#include <asm/arch/reset_manager.h>
Dinh Nguyen429642c2015-06-02 22:52:48 -050015#include <asm/arch/sdram.h>
Dinh Nguyen429642c2015-06-02 22:52:48 -050016#include <asm/arch/system_manager.h>
17#include <asm/io.h>
18
Simon Goldschmidt24910c32019-04-16 22:04:39 +020019#include "sequencer.h"
20
21#ifdef CONFIG_SPL_BUILD
22
23struct altera_gen5_sdram_priv {
24 struct ram_info info;
25};
26
27struct altera_gen5_sdram_platdata {
28 struct socfpga_sdr *sdr;
29};
30
Marek Vasute08c5592015-07-26 10:37:54 +020031struct sdram_prot_rule {
Marek Vasut6772cd92015-08-01 23:12:11 +020032 u32 sdram_start; /* SDRAM start address */
33 u32 sdram_end; /* SDRAM end address */
Marek Vasute08c5592015-07-26 10:37:54 +020034 u32 rule; /* SDRAM protection rule number: 0-19 */
35 int valid; /* Rule valid or not? 1 - valid, 0 not*/
36
37 u32 security;
38 u32 portmask;
39 u32 result;
40 u32 lo_prot_id;
41 u32 hi_prot_id;
42};
43
Simon Goldschmidt24910c32019-04-16 22:04:39 +020044static unsigned long sdram_calculate_size(struct socfpga_sdr_ctrl *sdr_ctrl);
Dinh Nguyen429642c2015-06-02 22:52:48 -050045
Marek Vasut724c50f2015-08-01 19:20:19 +020046/**
47 * get_errata_rows() - Up the number of DRAM rows to cover entire address space
Marek Vasut3a079112015-08-01 21:16:20 +020048 * @cfg: SDRAM controller configuration data
Marek Vasut724c50f2015-08-01 19:20:19 +020049 *
50 * SDRAM Failure happens when accessing non-existent memory. Artificially
51 * increase the number of rows so that the memory controller thinks it has
52 * 4GB of RAM. This function returns such amount of rows.
53 */
Marek Vasut32ada572015-08-01 21:35:18 +020054static int get_errata_rows(const struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -050055{
Marek Vasut724c50f2015-08-01 19:20:19 +020056 /* Define constant for 4G memory - used for SDRAM errata workaround */
57#define MEMSIZE_4G (4ULL * 1024ULL * 1024ULL * 1024ULL)
58 const unsigned long long memsize = MEMSIZE_4G;
Marek Vasut3a079112015-08-01 21:16:20 +020059 const unsigned int cs =
60 ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
61 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
62 const unsigned int rows =
63 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
64 SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
65 const unsigned int banks =
66 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
67 SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
68 const unsigned int cols =
69 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
70 SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
Marek Vasut724c50f2015-08-01 19:20:19 +020071 const unsigned int width = 8;
72
Dinh Nguyen429642c2015-06-02 22:52:48 -050073 unsigned long long newrows;
Marek Vasut724c50f2015-08-01 19:20:19 +020074 int bits, inewrowslog2;
Dinh Nguyen429642c2015-06-02 22:52:48 -050075
76 debug("workaround rows - memsize %lld\n", memsize);
77 debug("workaround rows - cs %d\n", cs);
78 debug("workaround rows - width %d\n", width);
79 debug("workaround rows - rows %d\n", rows);
80 debug("workaround rows - banks %d\n", banks);
81 debug("workaround rows - cols %d\n", cols);
82
Marek Vasut186880e2015-08-01 18:54:34 +020083 newrows = lldiv(memsize, cs * (width / 8));
Dinh Nguyen429642c2015-06-02 22:52:48 -050084 debug("rows workaround - term1 %lld\n", newrows);
85
Marek Vasut186880e2015-08-01 18:54:34 +020086 newrows = lldiv(newrows, (1 << banks) * (1 << cols));
Dinh Nguyen429642c2015-06-02 22:52:48 -050087 debug("rows workaround - term2 %lld\n", newrows);
88
Marek Vasut186880e2015-08-01 18:54:34 +020089 /*
90 * Compute the hamming weight - same as number of bits set.
Dinh Nguyen429642c2015-06-02 22:52:48 -050091 * Need to see if result is ordinal power of 2 before
92 * attempting log2 of result.
93 */
Marek Vasut2fda5062015-08-01 18:46:55 +020094 bits = generic_hweight32(newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -050095
96 debug("rows workaround - bits %d\n", bits);
97
98 if (bits != 1) {
99 printf("SDRAM workaround failed, bits set %d\n", bits);
100 return rows;
101 }
102
103 if (newrows > UINT_MAX) {
104 printf("SDRAM workaround rangecheck failed, %lld\n", newrows);
105 return rows;
106 }
107
Marek Vasut186880e2015-08-01 18:54:34 +0200108 inewrowslog2 = __ilog2(newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500109
Marek Vasut186880e2015-08-01 18:54:34 +0200110 debug("rows workaround - ilog2 %d, %lld\n", inewrowslog2, newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500111
112 if (inewrowslog2 == -1) {
Marek Vasut186880e2015-08-01 18:54:34 +0200113 printf("SDRAM workaround failed, newrows %lld\n", newrows);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500114 return rows;
115 }
116
117 return inewrowslog2;
118}
119
120/* SDRAM protection rules vary from 0-19, a total of 20 rules. */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200121static void sdram_set_rule(struct socfpga_sdr_ctrl *sdr_ctrl,
122 struct sdram_prot_rule *prule)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500123{
Marek Vasut6772cd92015-08-01 23:12:11 +0200124 u32 lo_addr_bits;
125 u32 hi_addr_bits;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500126 int ruleno = prule->rule;
127
128 /* Select the rule */
129 writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
130
131 /* Obtain the address bits */
Marek Vasut7fce5bc2015-08-01 22:40:48 +0200132 lo_addr_bits = prule->sdram_start >> 20ULL;
Marek Vasut12361a22016-04-04 17:52:21 +0200133 hi_addr_bits = (prule->sdram_end - 1) >> 20ULL;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500134
Marek Vasut6772cd92015-08-01 23:12:11 +0200135 debug("sdram set rule start %x, %d\n", lo_addr_bits,
Dinh Nguyen429642c2015-06-02 22:52:48 -0500136 prule->sdram_start);
Marek Vasut6772cd92015-08-01 23:12:11 +0200137 debug("sdram set rule end %x, %d\n", hi_addr_bits,
Dinh Nguyen429642c2015-06-02 22:52:48 -0500138 prule->sdram_end);
139
140 /* Set rule addresses */
141 writel(lo_addr_bits | (hi_addr_bits << 12), &sdr_ctrl->prot_rule_addr);
142
143 /* Set rule protection ids */
144 writel(prule->lo_prot_id | (prule->hi_prot_id << 12),
145 &sdr_ctrl->prot_rule_id);
146
147 /* Set the rule data */
148 writel(prule->security | (prule->valid << 2) |
149 (prule->portmask << 3) | (prule->result << 13),
150 &sdr_ctrl->prot_rule_data);
151
152 /* write the rule */
Marek Vasut7fce5bc2015-08-01 22:40:48 +0200153 writel(ruleno | (1 << 5), &sdr_ctrl->prot_rule_rdwr);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500154
155 /* Set rule number to 0 by default */
156 writel(0, &sdr_ctrl->prot_rule_rdwr);
157}
158
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200159static void sdram_get_rule(struct socfpga_sdr_ctrl *sdr_ctrl,
160 struct sdram_prot_rule *prule)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500161{
Marek Vasut91144072015-08-01 23:21:23 +0200162 u32 addr;
163 u32 id;
164 u32 data;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500165 int ruleno = prule->rule;
166
167 /* Read the rule */
168 writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
Marek Vasut91144072015-08-01 23:21:23 +0200169 writel(ruleno | (1 << 6), &sdr_ctrl->prot_rule_rdwr);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500170
171 /* Get the addresses */
172 addr = readl(&sdr_ctrl->prot_rule_addr);
173 prule->sdram_start = (addr & 0xFFF) << 20;
174 prule->sdram_end = ((addr >> 12) & 0xFFF) << 20;
175
176 /* Get the configured protection IDs */
177 id = readl(&sdr_ctrl->prot_rule_id);
178 prule->lo_prot_id = id & 0xFFF;
179 prule->hi_prot_id = (id >> 12) & 0xFFF;
180
181 /* Get protection data */
182 data = readl(&sdr_ctrl->prot_rule_data);
183
184 prule->security = data & 0x3;
185 prule->valid = (data >> 2) & 0x1;
186 prule->portmask = (data >> 3) & 0x3FF;
187 prule->result = (data >> 13) & 0x1;
188}
189
Marek Vasut6772cd92015-08-01 23:12:11 +0200190static void
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200191sdram_set_protection_config(struct socfpga_sdr_ctrl *sdr_ctrl,
192 const u32 sdram_start, const u32 sdram_end)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500193{
194 struct sdram_prot_rule rule;
195 int rules;
196
197 /* Start with accepting all SDRAM transaction */
198 writel(0x0, &sdr_ctrl->protport_default);
199
200 /* Clear all protection rules for warm boot case */
Marek Vasut7fce5bc2015-08-01 22:40:48 +0200201 memset(&rule, 0, sizeof(rule));
Dinh Nguyen429642c2015-06-02 22:52:48 -0500202
203 for (rules = 0; rules < 20; rules++) {
204 rule.rule = rules;
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200205 sdram_set_rule(sdr_ctrl, &rule);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500206 }
207
208 /* new rule: accept SDRAM */
209 rule.sdram_start = sdram_start;
210 rule.sdram_end = sdram_end;
211 rule.lo_prot_id = 0x0;
212 rule.hi_prot_id = 0xFFF;
213 rule.portmask = 0x3FF;
214 rule.security = 0x3;
215 rule.result = 0;
216 rule.valid = 1;
217 rule.rule = 0;
218
219 /* set new rule */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200220 sdram_set_rule(sdr_ctrl, &rule);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500221
222 /* default rule: reject everything */
223 writel(0x3ff, &sdr_ctrl->protport_default);
224}
225
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200226static void sdram_dump_protection_config(struct socfpga_sdr_ctrl *sdr_ctrl)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500227{
228 struct sdram_prot_rule rule;
229 int rules;
230
231 debug("SDRAM Prot rule, default %x\n",
232 readl(&sdr_ctrl->protport_default));
233
234 for (rules = 0; rules < 20; rules++) {
Marek Vasut42aa46d2015-12-29 09:38:52 +0100235 rule.rule = rules;
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200236 sdram_get_rule(sdr_ctrl, &rule);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500237 debug("Rule %d, rules ...\n", rules);
Marek Vasut6772cd92015-08-01 23:12:11 +0200238 debug(" sdram start %x\n", rule.sdram_start);
239 debug(" sdram end %x\n", rule.sdram_end);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500240 debug(" low prot id %d, hi prot id %d\n",
241 rule.lo_prot_id,
242 rule.hi_prot_id);
243 debug(" portmask %x\n", rule.portmask);
244 debug(" security %d\n", rule.security);
245 debug(" result %d\n", rule.result);
246 debug(" valid %d\n", rule.valid);
247 }
248}
249
Marek Vasut116d88f2015-08-01 22:26:11 +0200250/**
251 * sdram_write_verify() - write to register and verify the write.
252 * @addr: Register address
253 * @val: Value to be written and verified
254 *
255 * This function writes to a register, reads back the value and compares
256 * the result with the written value to check if the data match.
257 */
258static unsigned sdram_write_verify(const u32 *addr, const u32 val)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500259{
Marek Vasut116d88f2015-08-01 22:26:11 +0200260 u32 rval;
261
262 debug(" Write - Address 0x%p Data 0x%08x\n", addr, val);
263 writel(val, addr);
264
Dinh Nguyen429642c2015-06-02 22:52:48 -0500265 debug(" Read and verify...");
Marek Vasut116d88f2015-08-01 22:26:11 +0200266 rval = readl(addr);
267 if (rval != val) {
268 debug("FAIL - Address 0x%p Expected 0x%08x Data 0x%08x\n",
269 addr, val, rval);
270 return -EINVAL;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500271 }
Marek Vasut116d88f2015-08-01 22:26:11 +0200272
Dinh Nguyen429642c2015-06-02 22:52:48 -0500273 debug("correct!\n");
Dinh Nguyen429642c2015-06-02 22:52:48 -0500274 return 0;
275}
276
Marek Vasutb0d848c2015-08-01 22:28:30 +0200277/**
278 * sdr_get_ctrlcfg() - Get the value of DRAM CTRLCFG register
279 * @cfg: SDRAM controller configuration data
280 *
281 * Return the value of DRAM CTRLCFG register.
282 */
Marek Vasut32ada572015-08-01 21:35:18 +0200283static u32 sdr_get_ctrlcfg(const struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500284{
Marek Vasut3a079112015-08-01 21:16:20 +0200285 const u32 csbits =
286 ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
287 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
288 u32 addrorder =
289 (cfg->ctrl_cfg & SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK) >>
290 SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
291
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200292 u32 ctrl_cfg = cfg->ctrl_cfg;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500293
Marek Vasut82a27642015-08-01 19:33:40 +0200294 /*
295 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500296 * Set the addrorder field of the SDRAM control register
297 * based on the CSBITs setting.
298 */
Marek Vasut3a079112015-08-01 21:16:20 +0200299 if (csbits == 1) {
300 if (addrorder != 0)
Marek Vasut82a27642015-08-01 19:33:40 +0200301 debug("INFO: Changing address order to 0 (chip, row, bank, column)\n");
Marek Vasut3a079112015-08-01 21:16:20 +0200302 addrorder = 0;
303 } else if (csbits == 2) {
304 if (addrorder != 2)
Marek Vasut82a27642015-08-01 19:33:40 +0200305 debug("INFO: Changing address order to 2 (row, chip, bank, column)\n");
Marek Vasut3a079112015-08-01 21:16:20 +0200306 addrorder = 2;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500307 }
308
Marek Vasut3a079112015-08-01 21:16:20 +0200309 ctrl_cfg &= ~SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK;
Marek Vasut82a27642015-08-01 19:33:40 +0200310 ctrl_cfg |= addrorder << SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500311
Marek Vasut1e271e42015-08-01 21:24:31 +0200312 return ctrl_cfg;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500313}
314
Marek Vasutb0d848c2015-08-01 22:28:30 +0200315/**
316 * sdr_get_addr_rw() - Get the value of DRAM ADDRW register
317 * @cfg: SDRAM controller configuration data
318 *
319 * Return the value of DRAM ADDRW register.
320 */
Marek Vasut32ada572015-08-01 21:35:18 +0200321static u32 sdr_get_addr_rw(const struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500322{
Dinh Nguyen429642c2015-06-02 22:52:48 -0500323 /*
324 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500325 * Set SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB to
326 * log2(number of chip select bits). Since there's only
327 * 1 or 2 chip selects, log2(1) => 0, and log2(2) => 1,
328 * which is the same as "chip selects" - 1.
329 */
Marek Vasut3a079112015-08-01 21:16:20 +0200330 const int rows = get_errata_rows(cfg);
331 u32 dram_addrw = cfg->dram_addrw & ~SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK;
Marek Vasut4f3adbf2015-08-01 20:30:10 +0200332
Marek Vasut1e271e42015-08-01 21:24:31 +0200333 return dram_addrw | (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500334}
335
Marek Vasutb81f11c2015-08-01 21:26:55 +0200336/**
337 * sdr_load_regs() - Load SDRAM controller registers
338 * @cfg: SDRAM controller configuration data
339 *
340 * This function loads the register values into the SDRAM controller block.
341 */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200342static void sdr_load_regs(struct socfpga_sdr_ctrl *sdr_ctrl,
343 const struct socfpga_sdram_config *cfg)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500344{
Marek Vasut1e271e42015-08-01 21:24:31 +0200345 const u32 ctrl_cfg = sdr_get_ctrlcfg(cfg);
346 const u32 dram_addrw = sdr_get_addr_rw(cfg);
347
Marek Vasut1e271e42015-08-01 21:24:31 +0200348 debug("\nConfiguring CTRLCFG\n");
349 writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
Marek Vasut71c1a002015-08-01 21:21:21 +0200350
351 debug("Configuring DRAMTIMING1\n");
352 writel(cfg->dram_timing1, &sdr_ctrl->dram_timing1);
353
354 debug("Configuring DRAMTIMING2\n");
355 writel(cfg->dram_timing2, &sdr_ctrl->dram_timing2);
356
357 debug("Configuring DRAMTIMING3\n");
358 writel(cfg->dram_timing3, &sdr_ctrl->dram_timing3);
359
360 debug("Configuring DRAMTIMING4\n");
361 writel(cfg->dram_timing4, &sdr_ctrl->dram_timing4);
362
363 debug("Configuring LOWPWRTIMING\n");
364 writel(cfg->lowpwr_timing, &sdr_ctrl->lowpwr_timing);
365
Marek Vasut1e271e42015-08-01 21:24:31 +0200366 debug("Configuring DRAMADDRW\n");
367 writel(dram_addrw, &sdr_ctrl->dram_addrw);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500368
369 debug("Configuring DRAMIFWIDTH\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200370 writel(cfg->dram_if_width, &sdr_ctrl->dram_if_width);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500371
372 debug("Configuring DRAMDEVWIDTH\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200373 writel(cfg->dram_dev_width, &sdr_ctrl->dram_dev_width);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500374
375 debug("Configuring LOWPWREQ\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200376 writel(cfg->lowpwr_eq, &sdr_ctrl->lowpwr_eq);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500377
378 debug("Configuring DRAMINTR\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200379 writel(cfg->dram_intr, &sdr_ctrl->dram_intr);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500380
Marek Vasut71c1a002015-08-01 21:21:21 +0200381 debug("Configuring STATICCFG\n");
382 writel(cfg->static_cfg, &sdr_ctrl->static_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500383
384 debug("Configuring CTRLWIDTH\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200385 writel(cfg->ctrl_width, &sdr_ctrl->ctrl_width);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500386
387 debug("Configuring PORTCFG\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200388 writel(cfg->port_cfg, &sdr_ctrl->port_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500389
Marek Vasut71c1a002015-08-01 21:21:21 +0200390 debug("Configuring FIFOCFG\n");
391 writel(cfg->fifo_cfg, &sdr_ctrl->fifo_cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500392
393 debug("Configuring MPPRIORITY\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200394 writel(cfg->mp_priority, &sdr_ctrl->mp_priority);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500395
Marek Vasut71c1a002015-08-01 21:21:21 +0200396 debug("Configuring MPWEIGHT_MPWEIGHT_0\n");
397 writel(cfg->mp_weight0, &sdr_ctrl->mp_weight0);
398 writel(cfg->mp_weight1, &sdr_ctrl->mp_weight1);
399 writel(cfg->mp_weight2, &sdr_ctrl->mp_weight2);
400 writel(cfg->mp_weight3, &sdr_ctrl->mp_weight3);
401
402 debug("Configuring MPPACING_MPPACING_0\n");
403 writel(cfg->mp_pacing0, &sdr_ctrl->mp_pacing0);
404 writel(cfg->mp_pacing1, &sdr_ctrl->mp_pacing1);
405 writel(cfg->mp_pacing2, &sdr_ctrl->mp_pacing2);
406 writel(cfg->mp_pacing3, &sdr_ctrl->mp_pacing3);
407
408 debug("Configuring MPTHRESHOLDRST_MPTHRESHOLDRST_0\n");
409 writel(cfg->mp_threshold0, &sdr_ctrl->mp_threshold0);
410 writel(cfg->mp_threshold1, &sdr_ctrl->mp_threshold1);
411 writel(cfg->mp_threshold2, &sdr_ctrl->mp_threshold2);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500412
413 debug("Configuring PHYCTRL_PHYCTRL_0\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200414 writel(cfg->phy_ctrl0, &sdr_ctrl->phy_ctrl0);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500415
416 debug("Configuring CPORTWIDTH\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200417 writel(cfg->cport_width, &sdr_ctrl->cport_width);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500418
419 debug("Configuring CPORTWMAP\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200420 writel(cfg->cport_wmap, &sdr_ctrl->cport_wmap);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500421
422 debug("Configuring CPORTRMAP\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200423 writel(cfg->cport_rmap, &sdr_ctrl->cport_rmap);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500424
425 debug("Configuring RFIFOCMAP\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200426 writel(cfg->rfifo_cmap, &sdr_ctrl->rfifo_cmap);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500427
428 debug("Configuring WFIFOCMAP\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200429 writel(cfg->wfifo_cmap, &sdr_ctrl->wfifo_cmap);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500430
431 debug("Configuring CPORTRDWR\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200432 writel(cfg->cport_rdwr, &sdr_ctrl->cport_rdwr);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500433
434 debug("Configuring DRAMODT\n");
Marek Vasut7697ff72015-08-01 20:58:44 +0200435 writel(cfg->dram_odt, &sdr_ctrl->dram_odt);
Chin Liang See3ea59512016-09-21 10:25:56 +0800436
437 debug("Configuring EXTRATIME1\n");
438 writel(cfg->extratime1, &sdr_ctrl->extratime1);
Marek Vasutb81f11c2015-08-01 21:26:55 +0200439}
440
Marek Vasut5a4e8ed2015-08-01 22:03:48 +0200441/**
442 * sdram_mmr_init_full() - Function to initialize SDRAM MMR
443 * @sdr_phy_reg: Value of the PHY control register 0
444 *
445 * Initialize the SDRAM MMR.
446 */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200447int sdram_mmr_init_full(struct socfpga_sdr_ctrl *sdr_ctrl,
448 unsigned int sdr_phy_reg)
Marek Vasutb81f11c2015-08-01 21:26:55 +0200449{
Marek Vasut32ada572015-08-01 21:35:18 +0200450 const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
Marek Vasutb81f11c2015-08-01 21:26:55 +0200451 const unsigned int rows =
452 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
453 SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
Marek Vasut116d88f2015-08-01 22:26:11 +0200454 int ret;
Marek Vasutb81f11c2015-08-01 21:26:55 +0200455
Ley Foon Tan3d3a8602019-11-08 10:38:20 +0800456 writel(rows,
457 socfpga_get_sysmgr_addr() + SYSMGR_ISWGRP_HANDOFF_OFFSET(4));
Marek Vasutb81f11c2015-08-01 21:26:55 +0200458
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200459 sdr_load_regs(sdr_ctrl, cfg);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500460
461 /* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */
Ley Foon Tan3d3a8602019-11-08 10:38:20 +0800462 writel(cfg->fpgaport_rst,
463 socfpga_get_sysmgr_addr() + SYSMGR_ISWGRP_HANDOFF_OFFSET(3));
Dinh Nguyen429642c2015-06-02 22:52:48 -0500464
465 /* only enable if the FPGA is programmed */
466 if (fpgamgr_test_fpga_ready()) {
Marek Vasut116d88f2015-08-01 22:26:11 +0200467 ret = sdram_write_verify(&sdr_ctrl->fpgaport_rst,
468 cfg->fpgaport_rst);
469 if (ret)
470 return ret;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500471 }
472
473 /* Restore the SDR PHY Register if valid */
474 if (sdr_phy_reg != 0xffffffff)
475 writel(sdr_phy_reg, &sdr_ctrl->phy_ctrl0);
476
Marek Vasut7697ff72015-08-01 20:58:44 +0200477 /* Final step - apply configuration changes */
478 debug("Configuring STATICCFG\n");
479 clrsetbits_le32(&sdr_ctrl->static_cfg,
480 SDR_CTRLGRP_STATICCFG_APPLYCFG_MASK,
Dinh Nguyen429642c2015-06-02 22:52:48 -0500481 1 << SDR_CTRLGRP_STATICCFG_APPLYCFG_LSB);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500482
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200483 sdram_set_protection_config(sdr_ctrl, 0,
484 sdram_calculate_size(sdr_ctrl) - 1);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500485
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200486 sdram_dump_protection_config(sdr_ctrl);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500487
Marek Vasut116d88f2015-08-01 22:26:11 +0200488 return 0;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500489}
490
Marek Vasut1796a092015-08-01 21:47:16 +0200491/**
492 * sdram_calculate_size() - Calculate SDRAM size
Dinh Nguyen429642c2015-06-02 22:52:48 -0500493 *
Marek Vasut1796a092015-08-01 21:47:16 +0200494 * Calculate SDRAM device size based on SDRAM controller parameters.
495 * Size is specified in bytes.
Dinh Nguyen429642c2015-06-02 22:52:48 -0500496 */
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200497static unsigned long sdram_calculate_size(struct socfpga_sdr_ctrl *sdr_ctrl)
Dinh Nguyen429642c2015-06-02 22:52:48 -0500498{
499 unsigned long temp;
500 unsigned long row, bank, col, cs, width;
Marek Vasut6d6fbba2015-08-01 21:44:00 +0200501 const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
502 const unsigned int csbits =
503 ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
504 SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
505 const unsigned int rowbits =
506 (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
507 SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500508
509 temp = readl(&sdr_ctrl->dram_addrw);
510 col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
511 SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
512
Marek Vasut1796a092015-08-01 21:47:16 +0200513 /*
514 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500515 * Use ROWBITS from Quartus/QSys to calculate SDRAM size
516 * since the FB specifies we modify ROWBITs to work around SDRAM
517 * controller issue.
Dinh Nguyen429642c2015-06-02 22:52:48 -0500518 */
Ley Foon Tan3d3a8602019-11-08 10:38:20 +0800519 row = readl(socfpga_get_sysmgr_addr() +
520 SYSMGR_ISWGRP_HANDOFF_OFFSET(4));
Dinh Nguyen429642c2015-06-02 22:52:48 -0500521 if (row == 0)
Marek Vasut6d6fbba2015-08-01 21:44:00 +0200522 row = rowbits;
Marek Vasut1796a092015-08-01 21:47:16 +0200523 /*
524 * If the stored handoff value for rows is greater than
Dinh Nguyen429642c2015-06-02 22:52:48 -0500525 * the field width in the sdr.dramaddrw register then
526 * something is very wrong. Revert to using the the #define
527 * value handed off by the SOCEDS tool chain instead of
528 * using a broken value.
529 */
530 if (row > 31)
Marek Vasut6d6fbba2015-08-01 21:44:00 +0200531 row = rowbits;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500532
533 bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
534 SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
535
Marek Vasut1796a092015-08-01 21:47:16 +0200536 /*
537 * SDRAM Failure When Accessing Non-Existent Memory
Dinh Nguyen429642c2015-06-02 22:52:48 -0500538 * Use CSBITs from Quartus/QSys to calculate SDRAM size
539 * since the FB specifies we modify CSBITs to work around SDRAM
540 * controller issue.
541 */
Marek Vasut6d6fbba2015-08-01 21:44:00 +0200542 cs = csbits;
Dinh Nguyen429642c2015-06-02 22:52:48 -0500543
544 width = readl(&sdr_ctrl->dram_if_width);
Marek Vasut1796a092015-08-01 21:47:16 +0200545
Dinh Nguyen429642c2015-06-02 22:52:48 -0500546 /* ECC would not be calculated as its not addressible */
547 if (width == SDRAM_WIDTH_32BIT_WITH_ECC)
548 width = 32;
549 if (width == SDRAM_WIDTH_16BIT_WITH_ECC)
550 width = 16;
551
552 /* calculate the SDRAM size base on this info */
553 temp = 1 << (row + bank + col);
554 temp = temp * cs * (width / 8);
555
Marek Vasut1796a092015-08-01 21:47:16 +0200556 debug("%s returns %ld\n", __func__, temp);
Dinh Nguyen429642c2015-06-02 22:52:48 -0500557
558 return temp;
559}
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200560
561static int altera_gen5_sdram_ofdata_to_platdata(struct udevice *dev)
562{
563 struct altera_gen5_sdram_platdata *plat = dev->platdata;
564
565 plat->sdr = (struct socfpga_sdr *)devfdt_get_addr_index(dev, 0);
566 if (!plat->sdr)
567 return -ENODEV;
568
569 return 0;
570}
571
572static int altera_gen5_sdram_probe(struct udevice *dev)
573{
574 int ret;
575 unsigned long sdram_size;
576 struct altera_gen5_sdram_platdata *plat = dev->platdata;
577 struct altera_gen5_sdram_priv *priv = dev_get_priv(dev);
578 struct socfpga_sdr_ctrl *sdr_ctrl = &plat->sdr->sdr_ctrl;
579 struct reset_ctl_bulk resets;
580
581 ret = reset_get_bulk(dev, &resets);
582 if (ret) {
583 dev_err(dev, "Can't get reset: %d\n", ret);
584 return -ENODEV;
585 }
586 reset_deassert_bulk(&resets);
587
588 if (sdram_mmr_init_full(sdr_ctrl, 0xffffffff) != 0) {
589 puts("SDRAM init failed.\n");
590 goto failed;
591 }
592
593 debug("SDRAM: Calibrating PHY\n");
594 /* SDRAM calibration */
595 if (sdram_calibration_full(plat->sdr) == 0) {
596 puts("SDRAM calibration failed.\n");
597 goto failed;
598 }
599
600 sdram_size = sdram_calculate_size(sdr_ctrl);
601 debug("SDRAM: %ld MiB\n", sdram_size >> 20);
602
603 /* Sanity check ensure correct SDRAM size specified */
604 if (get_ram_size(0, sdram_size) != sdram_size) {
605 puts("SDRAM size check failed!\n");
606 goto failed;
607 }
608
609 priv->info.base = 0;
610 priv->info.size = sdram_size;
611
612 return 0;
613
614failed:
615 reset_release_bulk(&resets);
616 return -ENODEV;
617}
618
619static int altera_gen5_sdram_get_info(struct udevice *dev,
620 struct ram_info *info)
621{
622 struct altera_gen5_sdram_priv *priv = dev_get_priv(dev);
623
624 info->base = priv->info.base;
625 info->size = priv->info.size;
626
627 return 0;
628}
629
Simon Goldschmidte8744332019-10-23 22:19:37 +0200630static const struct ram_ops altera_gen5_sdram_ops = {
Simon Goldschmidt24910c32019-04-16 22:04:39 +0200631 .get_info = altera_gen5_sdram_get_info,
632};
633
634static const struct udevice_id altera_gen5_sdram_ids[] = {
635 { .compatible = "altr,sdr-ctl" },
636 { /* sentinel */ }
637};
638
639U_BOOT_DRIVER(altera_gen5_sdram) = {
640 .name = "altr_sdr_ctl",
641 .id = UCLASS_RAM,
642 .of_match = altera_gen5_sdram_ids,
643 .ops = &altera_gen5_sdram_ops,
644 .ofdata_to_platdata = altera_gen5_sdram_ofdata_to_platdata,
645 .platdata_auto_alloc_size = sizeof(struct altera_gen5_sdram_platdata),
646 .probe = altera_gen5_sdram_probe,
647 .priv_auto_alloc_size = sizeof(struct altera_gen5_sdram_priv),
648};
649
650#endif /* CONFIG_SPL_BUILD */