2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-06-03 03:52:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright Altera Corporation (C) 2014-2015
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
2019-04-16 20:04:39 +00:00
|
|
|
#include <dm.h>
|
2015-08-01 20:25:29 +00:00
|
|
|
#include <errno.h>
|
2015-06-03 03:52:48 +00:00
|
|
|
#include <div64.h>
|
2019-12-28 17:45:05 +00:00
|
|
|
#include <init.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2019-04-16 20:04:39 +00:00
|
|
|
#include <ram.h>
|
|
|
|
#include <reset.h>
|
2015-06-03 03:52:48 +00:00
|
|
|
#include <watchdog.h>
|
|
|
|
#include <asm/arch/fpga_manager.h>
|
2019-04-16 20:04:39 +00:00
|
|
|
#include <asm/arch/reset_manager.h>
|
2015-06-03 03:52:48 +00:00
|
|
|
#include <asm/arch/sdram.h>
|
|
|
|
#include <asm/arch/system_manager.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <asm/bitops.h>
|
2015-06-03 03:52:48 +00:00
|
|
|
#include <asm/io.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2019-04-16 20:04:39 +00:00
|
|
|
#include "sequencer.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_SPL_BUILD
|
|
|
|
|
|
|
|
struct altera_gen5_sdram_priv {
|
|
|
|
struct ram_info info;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct altera_gen5_sdram_platdata {
|
|
|
|
struct socfpga_sdr *sdr;
|
|
|
|
};
|
|
|
|
|
2015-07-26 08:37:54 +00:00
|
|
|
struct sdram_prot_rule {
|
2015-08-01 21:12:11 +00:00
|
|
|
u32 sdram_start; /* SDRAM start address */
|
|
|
|
u32 sdram_end; /* SDRAM end address */
|
2015-07-26 08:37:54 +00:00
|
|
|
u32 rule; /* SDRAM protection rule number: 0-19 */
|
|
|
|
int valid; /* Rule valid or not? 1 - valid, 0 not*/
|
|
|
|
|
|
|
|
u32 security;
|
|
|
|
u32 portmask;
|
|
|
|
u32 result;
|
|
|
|
u32 lo_prot_id;
|
|
|
|
u32 hi_prot_id;
|
|
|
|
};
|
|
|
|
|
2019-04-16 20:04:39 +00:00
|
|
|
static unsigned long sdram_calculate_size(struct socfpga_sdr_ctrl *sdr_ctrl);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 17:20:19 +00:00
|
|
|
/**
|
|
|
|
* get_errata_rows() - Up the number of DRAM rows to cover entire address space
|
2015-08-01 19:16:20 +00:00
|
|
|
* @cfg: SDRAM controller configuration data
|
2015-08-01 17:20:19 +00:00
|
|
|
*
|
|
|
|
* SDRAM Failure happens when accessing non-existent memory. Artificially
|
|
|
|
* increase the number of rows so that the memory controller thinks it has
|
|
|
|
* 4GB of RAM. This function returns such amount of rows.
|
|
|
|
*/
|
2015-08-01 19:35:18 +00:00
|
|
|
static int get_errata_rows(const struct socfpga_sdram_config *cfg)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
2015-08-01 17:20:19 +00:00
|
|
|
/* Define constant for 4G memory - used for SDRAM errata workaround */
|
|
|
|
#define MEMSIZE_4G (4ULL * 1024ULL * 1024ULL * 1024ULL)
|
|
|
|
const unsigned long long memsize = MEMSIZE_4G;
|
2015-08-01 19:16:20 +00:00
|
|
|
const unsigned int cs =
|
|
|
|
((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
|
|
|
|
const unsigned int rows =
|
|
|
|
(cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
|
|
|
|
const unsigned int banks =
|
|
|
|
(cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
|
|
|
|
const unsigned int cols =
|
|
|
|
(cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
|
2015-08-01 17:20:19 +00:00
|
|
|
const unsigned int width = 8;
|
|
|
|
|
2015-06-03 03:52:48 +00:00
|
|
|
unsigned long long newrows;
|
2015-08-01 17:20:19 +00:00
|
|
|
int bits, inewrowslog2;
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("workaround rows - memsize %lld\n", memsize);
|
|
|
|
debug("workaround rows - cs %d\n", cs);
|
|
|
|
debug("workaround rows - width %d\n", width);
|
|
|
|
debug("workaround rows - rows %d\n", rows);
|
|
|
|
debug("workaround rows - banks %d\n", banks);
|
|
|
|
debug("workaround rows - cols %d\n", cols);
|
|
|
|
|
2015-08-01 16:54:34 +00:00
|
|
|
newrows = lldiv(memsize, cs * (width / 8));
|
2015-06-03 03:52:48 +00:00
|
|
|
debug("rows workaround - term1 %lld\n", newrows);
|
|
|
|
|
2015-08-01 16:54:34 +00:00
|
|
|
newrows = lldiv(newrows, (1 << banks) * (1 << cols));
|
2015-06-03 03:52:48 +00:00
|
|
|
debug("rows workaround - term2 %lld\n", newrows);
|
|
|
|
|
2015-08-01 16:54:34 +00:00
|
|
|
/*
|
|
|
|
* Compute the hamming weight - same as number of bits set.
|
2015-06-03 03:52:48 +00:00
|
|
|
* Need to see if result is ordinal power of 2 before
|
|
|
|
* attempting log2 of result.
|
|
|
|
*/
|
2015-08-01 16:46:55 +00:00
|
|
|
bits = generic_hweight32(newrows);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("rows workaround - bits %d\n", bits);
|
|
|
|
|
|
|
|
if (bits != 1) {
|
|
|
|
printf("SDRAM workaround failed, bits set %d\n", bits);
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newrows > UINT_MAX) {
|
|
|
|
printf("SDRAM workaround rangecheck failed, %lld\n", newrows);
|
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
2015-08-01 16:54:34 +00:00
|
|
|
inewrowslog2 = __ilog2(newrows);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 16:54:34 +00:00
|
|
|
debug("rows workaround - ilog2 %d, %lld\n", inewrowslog2, newrows);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
if (inewrowslog2 == -1) {
|
2015-08-01 16:54:34 +00:00
|
|
|
printf("SDRAM workaround failed, newrows %lld\n", newrows);
|
2015-06-03 03:52:48 +00:00
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inewrowslog2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SDRAM protection rules vary from 0-19, a total of 20 rules. */
|
2019-04-16 20:04:39 +00:00
|
|
|
static void sdram_set_rule(struct socfpga_sdr_ctrl *sdr_ctrl,
|
|
|
|
struct sdram_prot_rule *prule)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
2015-08-01 21:12:11 +00:00
|
|
|
u32 lo_addr_bits;
|
|
|
|
u32 hi_addr_bits;
|
2015-06-03 03:52:48 +00:00
|
|
|
int ruleno = prule->rule;
|
|
|
|
|
|
|
|
/* Select the rule */
|
|
|
|
writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
|
|
|
|
|
|
|
|
/* Obtain the address bits */
|
2015-08-01 20:40:48 +00:00
|
|
|
lo_addr_bits = prule->sdram_start >> 20ULL;
|
2016-04-04 15:52:21 +00:00
|
|
|
hi_addr_bits = (prule->sdram_end - 1) >> 20ULL;
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 21:12:11 +00:00
|
|
|
debug("sdram set rule start %x, %d\n", lo_addr_bits,
|
2015-06-03 03:52:48 +00:00
|
|
|
prule->sdram_start);
|
2015-08-01 21:12:11 +00:00
|
|
|
debug("sdram set rule end %x, %d\n", hi_addr_bits,
|
2015-06-03 03:52:48 +00:00
|
|
|
prule->sdram_end);
|
|
|
|
|
|
|
|
/* Set rule addresses */
|
|
|
|
writel(lo_addr_bits | (hi_addr_bits << 12), &sdr_ctrl->prot_rule_addr);
|
|
|
|
|
|
|
|
/* Set rule protection ids */
|
|
|
|
writel(prule->lo_prot_id | (prule->hi_prot_id << 12),
|
|
|
|
&sdr_ctrl->prot_rule_id);
|
|
|
|
|
|
|
|
/* Set the rule data */
|
|
|
|
writel(prule->security | (prule->valid << 2) |
|
|
|
|
(prule->portmask << 3) | (prule->result << 13),
|
|
|
|
&sdr_ctrl->prot_rule_data);
|
|
|
|
|
|
|
|
/* write the rule */
|
2015-08-01 20:40:48 +00:00
|
|
|
writel(ruleno | (1 << 5), &sdr_ctrl->prot_rule_rdwr);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
/* Set rule number to 0 by default */
|
|
|
|
writel(0, &sdr_ctrl->prot_rule_rdwr);
|
|
|
|
}
|
|
|
|
|
2019-04-16 20:04:39 +00:00
|
|
|
static void sdram_get_rule(struct socfpga_sdr_ctrl *sdr_ctrl,
|
|
|
|
struct sdram_prot_rule *prule)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
2015-08-01 21:21:23 +00:00
|
|
|
u32 addr;
|
|
|
|
u32 id;
|
|
|
|
u32 data;
|
2015-06-03 03:52:48 +00:00
|
|
|
int ruleno = prule->rule;
|
|
|
|
|
|
|
|
/* Read the rule */
|
|
|
|
writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
|
2015-08-01 21:21:23 +00:00
|
|
|
writel(ruleno | (1 << 6), &sdr_ctrl->prot_rule_rdwr);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
/* Get the addresses */
|
|
|
|
addr = readl(&sdr_ctrl->prot_rule_addr);
|
|
|
|
prule->sdram_start = (addr & 0xFFF) << 20;
|
|
|
|
prule->sdram_end = ((addr >> 12) & 0xFFF) << 20;
|
|
|
|
|
|
|
|
/* Get the configured protection IDs */
|
|
|
|
id = readl(&sdr_ctrl->prot_rule_id);
|
|
|
|
prule->lo_prot_id = id & 0xFFF;
|
|
|
|
prule->hi_prot_id = (id >> 12) & 0xFFF;
|
|
|
|
|
|
|
|
/* Get protection data */
|
|
|
|
data = readl(&sdr_ctrl->prot_rule_data);
|
|
|
|
|
|
|
|
prule->security = data & 0x3;
|
|
|
|
prule->valid = (data >> 2) & 0x1;
|
|
|
|
prule->portmask = (data >> 3) & 0x3FF;
|
|
|
|
prule->result = (data >> 13) & 0x1;
|
|
|
|
}
|
|
|
|
|
2015-08-01 21:12:11 +00:00
|
|
|
static void
|
2019-04-16 20:04:39 +00:00
|
|
|
sdram_set_protection_config(struct socfpga_sdr_ctrl *sdr_ctrl,
|
|
|
|
const u32 sdram_start, const u32 sdram_end)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
|
|
|
struct sdram_prot_rule rule;
|
|
|
|
int rules;
|
|
|
|
|
|
|
|
/* Start with accepting all SDRAM transaction */
|
|
|
|
writel(0x0, &sdr_ctrl->protport_default);
|
|
|
|
|
|
|
|
/* Clear all protection rules for warm boot case */
|
2015-08-01 20:40:48 +00:00
|
|
|
memset(&rule, 0, sizeof(rule));
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
for (rules = 0; rules < 20; rules++) {
|
|
|
|
rule.rule = rules;
|
2019-04-16 20:04:39 +00:00
|
|
|
sdram_set_rule(sdr_ctrl, &rule);
|
2015-06-03 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* new rule: accept SDRAM */
|
|
|
|
rule.sdram_start = sdram_start;
|
|
|
|
rule.sdram_end = sdram_end;
|
|
|
|
rule.lo_prot_id = 0x0;
|
|
|
|
rule.hi_prot_id = 0xFFF;
|
|
|
|
rule.portmask = 0x3FF;
|
|
|
|
rule.security = 0x3;
|
|
|
|
rule.result = 0;
|
|
|
|
rule.valid = 1;
|
|
|
|
rule.rule = 0;
|
|
|
|
|
|
|
|
/* set new rule */
|
2019-04-16 20:04:39 +00:00
|
|
|
sdram_set_rule(sdr_ctrl, &rule);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
/* default rule: reject everything */
|
|
|
|
writel(0x3ff, &sdr_ctrl->protport_default);
|
|
|
|
}
|
|
|
|
|
2019-04-16 20:04:39 +00:00
|
|
|
static void sdram_dump_protection_config(struct socfpga_sdr_ctrl *sdr_ctrl)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
|
|
|
struct sdram_prot_rule rule;
|
|
|
|
int rules;
|
|
|
|
|
|
|
|
debug("SDRAM Prot rule, default %x\n",
|
|
|
|
readl(&sdr_ctrl->protport_default));
|
|
|
|
|
|
|
|
for (rules = 0; rules < 20; rules++) {
|
2015-12-29 08:38:52 +00:00
|
|
|
rule.rule = rules;
|
2019-04-16 20:04:39 +00:00
|
|
|
sdram_get_rule(sdr_ctrl, &rule);
|
2015-06-03 03:52:48 +00:00
|
|
|
debug("Rule %d, rules ...\n", rules);
|
2015-08-01 21:12:11 +00:00
|
|
|
debug(" sdram start %x\n", rule.sdram_start);
|
|
|
|
debug(" sdram end %x\n", rule.sdram_end);
|
2015-06-03 03:52:48 +00:00
|
|
|
debug(" low prot id %d, hi prot id %d\n",
|
|
|
|
rule.lo_prot_id,
|
|
|
|
rule.hi_prot_id);
|
|
|
|
debug(" portmask %x\n", rule.portmask);
|
|
|
|
debug(" security %d\n", rule.security);
|
|
|
|
debug(" result %d\n", rule.result);
|
|
|
|
debug(" valid %d\n", rule.valid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-01 20:26:11 +00:00
|
|
|
/**
|
|
|
|
* sdram_write_verify() - write to register and verify the write.
|
|
|
|
* @addr: Register address
|
|
|
|
* @val: Value to be written and verified
|
|
|
|
*
|
|
|
|
* This function writes to a register, reads back the value and compares
|
|
|
|
* the result with the written value to check if the data match.
|
|
|
|
*/
|
|
|
|
static unsigned sdram_write_verify(const u32 *addr, const u32 val)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
2015-08-01 20:26:11 +00:00
|
|
|
u32 rval;
|
|
|
|
|
|
|
|
debug(" Write - Address 0x%p Data 0x%08x\n", addr, val);
|
|
|
|
writel(val, addr);
|
|
|
|
|
2015-06-03 03:52:48 +00:00
|
|
|
debug(" Read and verify...");
|
2015-08-01 20:26:11 +00:00
|
|
|
rval = readl(addr);
|
|
|
|
if (rval != val) {
|
|
|
|
debug("FAIL - Address 0x%p Expected 0x%08x Data 0x%08x\n",
|
|
|
|
addr, val, rval);
|
|
|
|
return -EINVAL;
|
2015-06-03 03:52:48 +00:00
|
|
|
}
|
2015-08-01 20:26:11 +00:00
|
|
|
|
2015-06-03 03:52:48 +00:00
|
|
|
debug("correct!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-01 20:28:30 +00:00
|
|
|
/**
|
|
|
|
* sdr_get_ctrlcfg() - Get the value of DRAM CTRLCFG register
|
|
|
|
* @cfg: SDRAM controller configuration data
|
|
|
|
*
|
|
|
|
* Return the value of DRAM CTRLCFG register.
|
|
|
|
*/
|
2015-08-01 19:35:18 +00:00
|
|
|
static u32 sdr_get_ctrlcfg(const struct socfpga_sdram_config *cfg)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
2015-08-01 19:16:20 +00:00
|
|
|
const u32 csbits =
|
|
|
|
((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
|
|
|
|
u32 addrorder =
|
|
|
|
(cfg->ctrl_cfg & SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK) >>
|
|
|
|
SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
|
|
|
|
|
2015-08-01 18:30:10 +00:00
|
|
|
u32 ctrl_cfg = cfg->ctrl_cfg;
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 17:33:40 +00:00
|
|
|
/*
|
|
|
|
* SDRAM Failure When Accessing Non-Existent Memory
|
2015-06-03 03:52:48 +00:00
|
|
|
* Set the addrorder field of the SDRAM control register
|
|
|
|
* based on the CSBITs setting.
|
|
|
|
*/
|
2015-08-01 19:16:20 +00:00
|
|
|
if (csbits == 1) {
|
|
|
|
if (addrorder != 0)
|
2015-08-01 17:33:40 +00:00
|
|
|
debug("INFO: Changing address order to 0 (chip, row, bank, column)\n");
|
2015-08-01 19:16:20 +00:00
|
|
|
addrorder = 0;
|
|
|
|
} else if (csbits == 2) {
|
|
|
|
if (addrorder != 2)
|
2015-08-01 17:33:40 +00:00
|
|
|
debug("INFO: Changing address order to 2 (row, chip, bank, column)\n");
|
2015-08-01 19:16:20 +00:00
|
|
|
addrorder = 2;
|
2015-06-03 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 19:16:20 +00:00
|
|
|
ctrl_cfg &= ~SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK;
|
2015-08-01 17:33:40 +00:00
|
|
|
ctrl_cfg |= addrorder << SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 19:24:31 +00:00
|
|
|
return ctrl_cfg;
|
2015-06-03 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 20:28:30 +00:00
|
|
|
/**
|
|
|
|
* sdr_get_addr_rw() - Get the value of DRAM ADDRW register
|
|
|
|
* @cfg: SDRAM controller configuration data
|
|
|
|
*
|
|
|
|
* Return the value of DRAM ADDRW register.
|
|
|
|
*/
|
2015-08-01 19:35:18 +00:00
|
|
|
static u32 sdr_get_addr_rw(const struct socfpga_sdram_config *cfg)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* SDRAM Failure When Accessing Non-Existent Memory
|
|
|
|
* Set SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB to
|
|
|
|
* log2(number of chip select bits). Since there's only
|
|
|
|
* 1 or 2 chip selects, log2(1) => 0, and log2(2) => 1,
|
|
|
|
* which is the same as "chip selects" - 1.
|
|
|
|
*/
|
2015-08-01 19:16:20 +00:00
|
|
|
const int rows = get_errata_rows(cfg);
|
|
|
|
u32 dram_addrw = cfg->dram_addrw & ~SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK;
|
2015-08-01 18:30:10 +00:00
|
|
|
|
2015-08-01 19:24:31 +00:00
|
|
|
return dram_addrw | (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB);
|
2015-06-03 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 19:26:55 +00:00
|
|
|
/**
|
|
|
|
* sdr_load_regs() - Load SDRAM controller registers
|
|
|
|
* @cfg: SDRAM controller configuration data
|
|
|
|
*
|
|
|
|
* This function loads the register values into the SDRAM controller block.
|
|
|
|
*/
|
2019-04-16 20:04:39 +00:00
|
|
|
static void sdr_load_regs(struct socfpga_sdr_ctrl *sdr_ctrl,
|
|
|
|
const struct socfpga_sdram_config *cfg)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
2015-08-01 19:24:31 +00:00
|
|
|
const u32 ctrl_cfg = sdr_get_ctrlcfg(cfg);
|
|
|
|
const u32 dram_addrw = sdr_get_addr_rw(cfg);
|
|
|
|
|
|
|
|
debug("\nConfiguring CTRLCFG\n");
|
|
|
|
writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
|
2015-08-01 19:21:21 +00:00
|
|
|
|
|
|
|
debug("Configuring DRAMTIMING1\n");
|
|
|
|
writel(cfg->dram_timing1, &sdr_ctrl->dram_timing1);
|
|
|
|
|
|
|
|
debug("Configuring DRAMTIMING2\n");
|
|
|
|
writel(cfg->dram_timing2, &sdr_ctrl->dram_timing2);
|
|
|
|
|
|
|
|
debug("Configuring DRAMTIMING3\n");
|
|
|
|
writel(cfg->dram_timing3, &sdr_ctrl->dram_timing3);
|
|
|
|
|
|
|
|
debug("Configuring DRAMTIMING4\n");
|
|
|
|
writel(cfg->dram_timing4, &sdr_ctrl->dram_timing4);
|
|
|
|
|
|
|
|
debug("Configuring LOWPWRTIMING\n");
|
|
|
|
writel(cfg->lowpwr_timing, &sdr_ctrl->lowpwr_timing);
|
|
|
|
|
2015-08-01 19:24:31 +00:00
|
|
|
debug("Configuring DRAMADDRW\n");
|
|
|
|
writel(dram_addrw, &sdr_ctrl->dram_addrw);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring DRAMIFWIDTH\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->dram_if_width, &sdr_ctrl->dram_if_width);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring DRAMDEVWIDTH\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->dram_dev_width, &sdr_ctrl->dram_dev_width);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring LOWPWREQ\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->lowpwr_eq, &sdr_ctrl->lowpwr_eq);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring DRAMINTR\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->dram_intr, &sdr_ctrl->dram_intr);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 19:21:21 +00:00
|
|
|
debug("Configuring STATICCFG\n");
|
|
|
|
writel(cfg->static_cfg, &sdr_ctrl->static_cfg);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring CTRLWIDTH\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->ctrl_width, &sdr_ctrl->ctrl_width);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring PORTCFG\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->port_cfg, &sdr_ctrl->port_cfg);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 19:21:21 +00:00
|
|
|
debug("Configuring FIFOCFG\n");
|
|
|
|
writel(cfg->fifo_cfg, &sdr_ctrl->fifo_cfg);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring MPPRIORITY\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->mp_priority, &sdr_ctrl->mp_priority);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 19:21:21 +00:00
|
|
|
debug("Configuring MPWEIGHT_MPWEIGHT_0\n");
|
|
|
|
writel(cfg->mp_weight0, &sdr_ctrl->mp_weight0);
|
|
|
|
writel(cfg->mp_weight1, &sdr_ctrl->mp_weight1);
|
|
|
|
writel(cfg->mp_weight2, &sdr_ctrl->mp_weight2);
|
|
|
|
writel(cfg->mp_weight3, &sdr_ctrl->mp_weight3);
|
|
|
|
|
|
|
|
debug("Configuring MPPACING_MPPACING_0\n");
|
|
|
|
writel(cfg->mp_pacing0, &sdr_ctrl->mp_pacing0);
|
|
|
|
writel(cfg->mp_pacing1, &sdr_ctrl->mp_pacing1);
|
|
|
|
writel(cfg->mp_pacing2, &sdr_ctrl->mp_pacing2);
|
|
|
|
writel(cfg->mp_pacing3, &sdr_ctrl->mp_pacing3);
|
|
|
|
|
|
|
|
debug("Configuring MPTHRESHOLDRST_MPTHRESHOLDRST_0\n");
|
|
|
|
writel(cfg->mp_threshold0, &sdr_ctrl->mp_threshold0);
|
|
|
|
writel(cfg->mp_threshold1, &sdr_ctrl->mp_threshold1);
|
|
|
|
writel(cfg->mp_threshold2, &sdr_ctrl->mp_threshold2);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring PHYCTRL_PHYCTRL_0\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->phy_ctrl0, &sdr_ctrl->phy_ctrl0);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring CPORTWIDTH\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->cport_width, &sdr_ctrl->cport_width);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring CPORTWMAP\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->cport_wmap, &sdr_ctrl->cport_wmap);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring CPORTRMAP\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->cport_rmap, &sdr_ctrl->cport_rmap);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring RFIFOCMAP\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->rfifo_cmap, &sdr_ctrl->rfifo_cmap);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring WFIFOCMAP\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->wfifo_cmap, &sdr_ctrl->wfifo_cmap);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring CPORTRDWR\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->cport_rdwr, &sdr_ctrl->cport_rdwr);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
debug("Configuring DRAMODT\n");
|
2015-08-01 18:58:44 +00:00
|
|
|
writel(cfg->dram_odt, &sdr_ctrl->dram_odt);
|
2016-09-21 02:25:56 +00:00
|
|
|
|
2019-10-17 22:22:31 +00:00
|
|
|
if (dram_is_ddr(3)) {
|
|
|
|
debug("Configuring EXTRATIME1\n");
|
|
|
|
writel(cfg->extratime1, &sdr_ctrl->extratime1);
|
|
|
|
}
|
2015-08-01 19:26:55 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 20:03:48 +00:00
|
|
|
/**
|
|
|
|
* sdram_mmr_init_full() - Function to initialize SDRAM MMR
|
|
|
|
* @sdr_phy_reg: Value of the PHY control register 0
|
|
|
|
*
|
|
|
|
* Initialize the SDRAM MMR.
|
|
|
|
*/
|
2019-04-16 20:04:39 +00:00
|
|
|
int sdram_mmr_init_full(struct socfpga_sdr_ctrl *sdr_ctrl,
|
|
|
|
unsigned int sdr_phy_reg)
|
2015-08-01 19:26:55 +00:00
|
|
|
{
|
2015-08-01 19:35:18 +00:00
|
|
|
const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
|
2015-08-01 19:26:55 +00:00
|
|
|
const unsigned int rows =
|
|
|
|
(cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
|
2015-08-01 20:26:11 +00:00
|
|
|
int ret;
|
2015-08-01 19:26:55 +00:00
|
|
|
|
2019-11-08 02:38:20 +00:00
|
|
|
writel(rows,
|
|
|
|
socfpga_get_sysmgr_addr() + SYSMGR_ISWGRP_HANDOFF_OFFSET(4));
|
2015-08-01 19:26:55 +00:00
|
|
|
|
2019-04-16 20:04:39 +00:00
|
|
|
sdr_load_regs(sdr_ctrl, cfg);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
/* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */
|
2019-11-08 02:38:20 +00:00
|
|
|
writel(cfg->fpgaport_rst,
|
|
|
|
socfpga_get_sysmgr_addr() + SYSMGR_ISWGRP_HANDOFF_OFFSET(3));
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
/* only enable if the FPGA is programmed */
|
|
|
|
if (fpgamgr_test_fpga_ready()) {
|
2015-08-01 20:26:11 +00:00
|
|
|
ret = sdram_write_verify(&sdr_ctrl->fpgaport_rst,
|
|
|
|
cfg->fpgaport_rst);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-06-03 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the SDR PHY Register if valid */
|
|
|
|
if (sdr_phy_reg != 0xffffffff)
|
|
|
|
writel(sdr_phy_reg, &sdr_ctrl->phy_ctrl0);
|
|
|
|
|
2015-08-01 18:58:44 +00:00
|
|
|
/* Final step - apply configuration changes */
|
|
|
|
debug("Configuring STATICCFG\n");
|
|
|
|
clrsetbits_le32(&sdr_ctrl->static_cfg,
|
|
|
|
SDR_CTRLGRP_STATICCFG_APPLYCFG_MASK,
|
2015-06-03 03:52:48 +00:00
|
|
|
1 << SDR_CTRLGRP_STATICCFG_APPLYCFG_LSB);
|
|
|
|
|
2019-04-16 20:04:39 +00:00
|
|
|
sdram_set_protection_config(sdr_ctrl, 0,
|
|
|
|
sdram_calculate_size(sdr_ctrl) - 1);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2019-04-16 20:04:39 +00:00
|
|
|
sdram_dump_protection_config(sdr_ctrl);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
2015-08-01 20:26:11 +00:00
|
|
|
return 0;
|
2015-06-03 03:52:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 19:47:16 +00:00
|
|
|
/**
|
|
|
|
* sdram_calculate_size() - Calculate SDRAM size
|
2015-06-03 03:52:48 +00:00
|
|
|
*
|
2015-08-01 19:47:16 +00:00
|
|
|
* Calculate SDRAM device size based on SDRAM controller parameters.
|
|
|
|
* Size is specified in bytes.
|
2015-06-03 03:52:48 +00:00
|
|
|
*/
|
2019-04-16 20:04:39 +00:00
|
|
|
static unsigned long sdram_calculate_size(struct socfpga_sdr_ctrl *sdr_ctrl)
|
2015-06-03 03:52:48 +00:00
|
|
|
{
|
|
|
|
unsigned long temp;
|
|
|
|
unsigned long row, bank, col, cs, width;
|
2015-08-01 19:44:00 +00:00
|
|
|
const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
|
|
|
|
const unsigned int csbits =
|
|
|
|
((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
|
|
|
|
const unsigned int rowbits =
|
|
|
|
(cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
temp = readl(&sdr_ctrl->dram_addrw);
|
|
|
|
col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
|
|
|
|
|
2015-08-01 19:47:16 +00:00
|
|
|
/*
|
|
|
|
* SDRAM Failure When Accessing Non-Existent Memory
|
2015-06-03 03:52:48 +00:00
|
|
|
* Use ROWBITS from Quartus/QSys to calculate SDRAM size
|
|
|
|
* since the FB specifies we modify ROWBITs to work around SDRAM
|
|
|
|
* controller issue.
|
|
|
|
*/
|
2019-11-08 02:38:20 +00:00
|
|
|
row = readl(socfpga_get_sysmgr_addr() +
|
|
|
|
SYSMGR_ISWGRP_HANDOFF_OFFSET(4));
|
2015-06-03 03:52:48 +00:00
|
|
|
if (row == 0)
|
2015-08-01 19:44:00 +00:00
|
|
|
row = rowbits;
|
2015-08-01 19:47:16 +00:00
|
|
|
/*
|
|
|
|
* If the stored handoff value for rows is greater than
|
2015-06-03 03:52:48 +00:00
|
|
|
* the field width in the sdr.dramaddrw register then
|
|
|
|
* something is very wrong. Revert to using the the #define
|
|
|
|
* value handed off by the SOCEDS tool chain instead of
|
|
|
|
* using a broken value.
|
|
|
|
*/
|
|
|
|
if (row > 31)
|
2015-08-01 19:44:00 +00:00
|
|
|
row = rowbits;
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
|
|
|
|
SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
|
|
|
|
|
2015-08-01 19:47:16 +00:00
|
|
|
/*
|
|
|
|
* SDRAM Failure When Accessing Non-Existent Memory
|
2015-06-03 03:52:48 +00:00
|
|
|
* Use CSBITs from Quartus/QSys to calculate SDRAM size
|
|
|
|
* since the FB specifies we modify CSBITs to work around SDRAM
|
|
|
|
* controller issue.
|
|
|
|
*/
|
2015-08-01 19:44:00 +00:00
|
|
|
cs = csbits;
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
width = readl(&sdr_ctrl->dram_if_width);
|
2015-08-01 19:47:16 +00:00
|
|
|
|
2015-06-03 03:52:48 +00:00
|
|
|
/* ECC would not be calculated as its not addressible */
|
|
|
|
if (width == SDRAM_WIDTH_32BIT_WITH_ECC)
|
|
|
|
width = 32;
|
|
|
|
if (width == SDRAM_WIDTH_16BIT_WITH_ECC)
|
|
|
|
width = 16;
|
|
|
|
|
|
|
|
/* calculate the SDRAM size base on this info */
|
|
|
|
temp = 1 << (row + bank + col);
|
|
|
|
temp = temp * cs * (width / 8);
|
|
|
|
|
2015-08-01 19:47:16 +00:00
|
|
|
debug("%s returns %ld\n", __func__, temp);
|
2015-06-03 03:52:48 +00:00
|
|
|
|
|
|
|
return temp;
|
|
|
|
}
|
2019-04-16 20:04:39 +00:00
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int altera_gen5_sdram_of_to_plat(struct udevice *dev)
|
2019-04-16 20:04:39 +00:00
|
|
|
{
|
2020-12-03 23:55:18 +00:00
|
|
|
struct altera_gen5_sdram_platdata *plat = dev->plat;
|
2019-04-16 20:04:39 +00:00
|
|
|
|
|
|
|
plat->sdr = (struct socfpga_sdr *)devfdt_get_addr_index(dev, 0);
|
|
|
|
if (!plat->sdr)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int altera_gen5_sdram_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long sdram_size;
|
2020-12-03 23:55:18 +00:00
|
|
|
struct altera_gen5_sdram_platdata *plat = dev->plat;
|
2019-04-16 20:04:39 +00:00
|
|
|
struct altera_gen5_sdram_priv *priv = dev_get_priv(dev);
|
|
|
|
struct socfpga_sdr_ctrl *sdr_ctrl = &plat->sdr->sdr_ctrl;
|
|
|
|
struct reset_ctl_bulk resets;
|
|
|
|
|
|
|
|
ret = reset_get_bulk(dev, &resets);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Can't get reset: %d\n", ret);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
reset_deassert_bulk(&resets);
|
|
|
|
|
|
|
|
if (sdram_mmr_init_full(sdr_ctrl, 0xffffffff) != 0) {
|
|
|
|
puts("SDRAM init failed.\n");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("SDRAM: Calibrating PHY\n");
|
|
|
|
/* SDRAM calibration */
|
|
|
|
if (sdram_calibration_full(plat->sdr) == 0) {
|
|
|
|
puts("SDRAM calibration failed.\n");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
sdram_size = sdram_calculate_size(sdr_ctrl);
|
|
|
|
debug("SDRAM: %ld MiB\n", sdram_size >> 20);
|
|
|
|
|
|
|
|
/* Sanity check ensure correct SDRAM size specified */
|
|
|
|
if (get_ram_size(0, sdram_size) != sdram_size) {
|
|
|
|
puts("SDRAM size check failed!\n");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->info.base = 0;
|
|
|
|
priv->info.size = sdram_size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
reset_release_bulk(&resets);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int altera_gen5_sdram_get_info(struct udevice *dev,
|
|
|
|
struct ram_info *info)
|
|
|
|
{
|
|
|
|
struct altera_gen5_sdram_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
info->base = priv->info.base;
|
|
|
|
info->size = priv->info.size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-10-23 20:19:37 +00:00
|
|
|
static const struct ram_ops altera_gen5_sdram_ops = {
|
2019-04-16 20:04:39 +00:00
|
|
|
.get_info = altera_gen5_sdram_get_info,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id altera_gen5_sdram_ids[] = {
|
|
|
|
{ .compatible = "altr,sdr-ctl" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(altera_gen5_sdram) = {
|
|
|
|
.name = "altr_sdr_ctl",
|
|
|
|
.id = UCLASS_RAM,
|
|
|
|
.of_match = altera_gen5_sdram_ids,
|
|
|
|
.ops = &altera_gen5_sdram_ops,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = altera_gen5_sdram_of_to_plat,
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct altera_gen5_sdram_platdata),
|
2019-04-16 20:04:39 +00:00
|
|
|
.probe = altera_gen5_sdram_probe,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct altera_gen5_sdram_priv),
|
2019-04-16 20:04:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* CONFIG_SPL_BUILD */
|