blob: ffbb79aaec1482285804e30b088528c3ee551f2d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dave Liu19b247e2008-01-11 18:48:24 +08002/*
3 * Copyright (C) 2007 Freescale Semiconductor, Inc.
4 *
5 * Authors: Nick.Spence@freescale.com
6 * Wilson.Lo@freescale.com
7 * scottwood@freescale.com
Dave Liu19b247e2008-01-11 18:48:24 +08008 */
9
10#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Dave Liu19b247e2008-01-11 18:48:24 +080012#include <mpc83xx.h>
13#include <spd_sdram.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Dave Liu19b247e2008-01-11 18:48:24 +080016
17#include <asm/bitops.h>
18#include <asm/io.h>
19
20#include <asm/processor.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24static void resume_from_sleep(void)
25{
26 u32 magic = *(u32 *)0;
27
28 typedef void (*func_t)(void);
29 func_t resume = *(func_t *)4;
30
31 if (magic == 0xf5153ae5)
32 resume();
33
34 gd->flags &= ~GD_FLG_SILENT;
35 puts("\nResume from sleep failed: bad magic word\n");
36}
37
38/* Fixed sdram init -- doesn't use serial presence detect.
39 *
40 * This is useful for faster booting in configs where the RAM is unlikely
41 * to be changed, or for things like NAND booting where space is tight.
42 */
Anton Vorontsovec821752009-11-24 20:12:12 +030043#ifndef CONFIG_SYS_RAMBOOT
Dave Liu19b247e2008-01-11 18:48:24 +080044static long fixed_sdram(void)
45{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
47 u32 msize = CONFIG_SYS_DDR_SIZE * 1024 * 1024;
Dave Liu19b247e2008-01-11 18:48:24 +080048 u32 msize_log2 = __ilog2(msize);
49
Mario Six805cac12019-01-21 09:18:16 +010050 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE & 0xfffff000;
Dave Liu19b247e2008-01-11 18:48:24 +080051 im->sysconf.ddrlaw[0].ar = LBLAWAR_EN | (msize_log2 - 1);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020052 im->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE;
Dave Liu19b247e2008-01-11 18:48:24 +080053
54 /*
55 * Erratum DDR3 requires a 50ms delay after clearing DDRCDR[DDR_cfg],
56 * or the DDR2 controller may fail to initialize correctly.
57 */
Anton Vorontsovec821752009-11-24 20:12:12 +030058 __udelay(50000);
Dave Liu19b247e2008-01-11 18:48:24 +080059
60 im->ddr.csbnds[0].csbnds = (msize - 1) >> 24;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020061 im->ddr.cs_config[0] = CONFIG_SYS_DDR_CS0_CONFIG;
Dave Liu19b247e2008-01-11 18:48:24 +080062
63 /* Currently we use only one CS, so disable the other bank. */
64 im->ddr.cs_config[1] = 0;
65
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020066 im->ddr.sdram_clk_cntl = CONFIG_SYS_DDR_SDRAM_CLK_CNTL;
67 im->ddr.timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3;
68 im->ddr.timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1;
69 im->ddr.timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2;
70 im->ddr.timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0;
Dave Liu19b247e2008-01-11 18:48:24 +080071
72 if (im->pmc.pmccr1 & PMCCR1_POWER_OFF)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020073 im->ddr.sdram_cfg = CONFIG_SYS_DDR_SDRAM_CFG | SDRAM_CFG_BI;
Dave Liu19b247e2008-01-11 18:48:24 +080074 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020075 im->ddr.sdram_cfg = CONFIG_SYS_DDR_SDRAM_CFG;
Dave Liu19b247e2008-01-11 18:48:24 +080076
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020077 im->ddr.sdram_cfg2 = CONFIG_SYS_DDR_SDRAM_CFG2;
78 im->ddr.sdram_mode = CONFIG_SYS_DDR_MODE;
79 im->ddr.sdram_mode2 = CONFIG_SYS_DDR_MODE2;
Dave Liu19b247e2008-01-11 18:48:24 +080080
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081 im->ddr.sdram_interval = CONFIG_SYS_DDR_INTERVAL;
Dave Liu19b247e2008-01-11 18:48:24 +080082 sync();
83
84 /* enable DDR controller */
85 im->ddr.sdram_cfg |= SDRAM_CFG_MEM_EN;
86 sync();
87
88 return msize;
89}
Anton Vorontsovec821752009-11-24 20:12:12 +030090#else
91static long fixed_sdram(void)
92{
93 return CONFIG_SYS_DDR_SIZE * 1024 * 1024;
94}
95#endif /* CONFIG_SYS_RAMBOOT */
Dave Liu19b247e2008-01-11 18:48:24 +080096
Simon Glassd35f3382017-04-06 12:47:05 -060097int dram_init(void)
Dave Liu19b247e2008-01-11 18:48:24 +080098{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020099 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
Dave Liu19b247e2008-01-11 18:48:24 +0800100 u32 msize;
101
102 if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
Simon Glass39f90ba2017-03-31 08:40:25 -0600103 return -ENXIO;
Dave Liu19b247e2008-01-11 18:48:24 +0800104
105 /* DDR SDRAM */
106 msize = fixed_sdram();
107
108 if (im->pmc.pmccr1 & PMCCR1_POWER_OFF)
109 resume_from_sleep();
110
Simon Glass39f90ba2017-03-31 08:40:25 -0600111 /* set total bus SDRAM size(bytes) -- DDR */
112 gd->ram_size = msize;
113
114 return 0;
Dave Liu19b247e2008-01-11 18:48:24 +0800115}