blob: 8a828207f9356eed3c11f0403e6fd4a813b4031d [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 Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Dave Liu19b247e2008-01-11 18:48:24 +080015
16#include <asm/bitops.h>
17#include <asm/io.h>
18
19#include <asm/processor.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23static void resume_from_sleep(void)
24{
25 u32 magic = *(u32 *)0;
26
27 typedef void (*func_t)(void);
28 func_t resume = *(func_t *)4;
29
30 if (magic == 0xf5153ae5)
31 resume();
32
33 gd->flags &= ~GD_FLG_SILENT;
34 puts("\nResume from sleep failed: bad magic word\n");
35}
36
37/* Fixed sdram init -- doesn't use serial presence detect.
38 *
39 * This is useful for faster booting in configs where the RAM is unlikely
40 * to be changed, or for things like NAND booting where space is tight.
41 */
Anton Vorontsovec821752009-11-24 20:12:12 +030042#ifndef CONFIG_SYS_RAMBOOT
Dave Liu19b247e2008-01-11 18:48:24 +080043static long fixed_sdram(void)
44{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020045 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
46 u32 msize = CONFIG_SYS_DDR_SIZE * 1024 * 1024;
Dave Liu19b247e2008-01-11 18:48:24 +080047 u32 msize_log2 = __ilog2(msize);
48
Mario Six805cac12019-01-21 09:18:16 +010049 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE & 0xfffff000;
Dave Liu19b247e2008-01-11 18:48:24 +080050 im->sysconf.ddrlaw[0].ar = LBLAWAR_EN | (msize_log2 - 1);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020051 im->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE;
Dave Liu19b247e2008-01-11 18:48:24 +080052
53 /*
54 * Erratum DDR3 requires a 50ms delay after clearing DDRCDR[DDR_cfg],
55 * or the DDR2 controller may fail to initialize correctly.
56 */
Anton Vorontsovec821752009-11-24 20:12:12 +030057 __udelay(50000);
Dave Liu19b247e2008-01-11 18:48:24 +080058
59 im->ddr.csbnds[0].csbnds = (msize - 1) >> 24;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020060 im->ddr.cs_config[0] = CONFIG_SYS_DDR_CS0_CONFIG;
Dave Liu19b247e2008-01-11 18:48:24 +080061
62 /* Currently we use only one CS, so disable the other bank. */
63 im->ddr.cs_config[1] = 0;
64
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020065 im->ddr.sdram_clk_cntl = CONFIG_SYS_DDR_SDRAM_CLK_CNTL;
66 im->ddr.timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3;
67 im->ddr.timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1;
68 im->ddr.timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2;
69 im->ddr.timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0;
Dave Liu19b247e2008-01-11 18:48:24 +080070
71 if (im->pmc.pmccr1 & PMCCR1_POWER_OFF)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020072 im->ddr.sdram_cfg = CONFIG_SYS_DDR_SDRAM_CFG | SDRAM_CFG_BI;
Dave Liu19b247e2008-01-11 18:48:24 +080073 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020074 im->ddr.sdram_cfg = CONFIG_SYS_DDR_SDRAM_CFG;
Dave Liu19b247e2008-01-11 18:48:24 +080075
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020076 im->ddr.sdram_cfg2 = CONFIG_SYS_DDR_SDRAM_CFG2;
77 im->ddr.sdram_mode = CONFIG_SYS_DDR_MODE;
78 im->ddr.sdram_mode2 = CONFIG_SYS_DDR_MODE2;
Dave Liu19b247e2008-01-11 18:48:24 +080079
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020080 im->ddr.sdram_interval = CONFIG_SYS_DDR_INTERVAL;
Dave Liu19b247e2008-01-11 18:48:24 +080081 sync();
82
83 /* enable DDR controller */
84 im->ddr.sdram_cfg |= SDRAM_CFG_MEM_EN;
85 sync();
86
87 return msize;
88}
Anton Vorontsovec821752009-11-24 20:12:12 +030089#else
90static long fixed_sdram(void)
91{
92 return CONFIG_SYS_DDR_SIZE * 1024 * 1024;
93}
94#endif /* CONFIG_SYS_RAMBOOT */
Dave Liu19b247e2008-01-11 18:48:24 +080095
Simon Glassd35f3382017-04-06 12:47:05 -060096int dram_init(void)
Dave Liu19b247e2008-01-11 18:48:24 +080097{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020098 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
Dave Liu19b247e2008-01-11 18:48:24 +080099 u32 msize;
100
101 if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
Simon Glass39f90ba2017-03-31 08:40:25 -0600102 return -ENXIO;
Dave Liu19b247e2008-01-11 18:48:24 +0800103
104 /* DDR SDRAM */
105 msize = fixed_sdram();
106
107 if (im->pmc.pmccr1 & PMCCR1_POWER_OFF)
108 resume_from_sleep();
109
Simon Glass39f90ba2017-03-31 08:40:25 -0600110 /* set total bus SDRAM size(bytes) -- DDR */
111 gd->ram_size = msize;
112
113 return 0;
Dave Liu19b247e2008-01-11 18:48:24 +0800114}