blob: 99f6bcd5a933b2e78ba0ea6c1531c83a32b32209 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Scott Wood865b8ae2007-04-16 14:54:15 -05002/*
3 * Copyright (C) Freescale Semiconductor, Inc. 2006-2007
4 *
5 * Authors: Nick.Spence@freescale.com
6 * Wilson.Lo@freescale.com
7 * scottwood@freescale.com
Scott Wood865b8ae2007-04-16 14:54:15 -05008 */
9
10#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Scott Wood865b8ae2007-04-16 14:54:15 -050012#include <mpc83xx.h>
13#include <spd_sdram.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Scott Wood865b8ae2007-04-16 14:54:15 -050015
16#include <asm/bitops.h>
17#include <asm/io.h>
18
19#include <asm/processor.h>
20
Wolfgang Denkd112a2c2007-09-15 20:48:41 +020021DECLARE_GLOBAL_DATA_PTR;
22
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023#ifndef CONFIG_SYS_8313ERDB_BROKEN_PMC
Scott Wood865b8ae2007-04-16 14:54:15 -050024static void resume_from_sleep(void)
25{
Scott Wood865b8ae2007-04-16 14:54:15 -050026 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#endif
38
39/* Fixed sdram init -- doesn't use serial presence detect.
40 *
41 * This is useful for faster booting in configs where the RAM is unlikely
42 * to be changed, or for things like NAND booting where space is tight.
43 */
44static long fixed_sdram(void)
45{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046 u32 msize = CONFIG_SYS_DDR_SIZE * 1024 * 1024;
Scott Woodb71689b2008-06-30 14:13:28 -050047
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020048#ifndef CONFIG_SYS_RAMBOOT
49 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
Scott Wood865b8ae2007-04-16 14:54:15 -050050 u32 msize_log2 = __ilog2(msize);
51
Mario Six805cac12019-01-21 09:18:16 +010052 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE & 0xfffff000;
Scott Wood865b8ae2007-04-16 14:54:15 -050053 im->sysconf.ddrlaw[0].ar = LBLAWAR_EN | (msize_log2 - 1);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054 im->sysconf.ddrcdr = CONFIG_SYS_DDRCDR_VALUE;
Scott Wood865b8ae2007-04-16 14:54:15 -050055
56 /*
57 * Erratum DDR3 requires a 50ms delay after clearing DDRCDR[DDR_cfg],
58 * or the DDR2 controller may fail to initialize correctly.
59 */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010060 __udelay(50000);
Scott Wood865b8ae2007-04-16 14:54:15 -050061
Mario Six805cac12019-01-21 09:18:16 +010062#if ((CONFIG_SYS_SDRAM_BASE & 0x00FFFFFF) != 0)
Joe Hershberger5ade3902011-10-11 23:57:31 -050063#warning Chip select bounds is only configurable in 16MB increments
64#endif
65 im->ddr.csbnds[0].csbnds =
Mario Six805cac12019-01-21 09:18:16 +010066 ((CONFIG_SYS_SDRAM_BASE >> CSBNDS_SA_SHIFT) & CSBNDS_SA) |
67 (((CONFIG_SYS_SDRAM_BASE + msize - 1) >> CSBNDS_EA_SHIFT) &
Joe Hershberger5ade3902011-10-11 23:57:31 -050068 CSBNDS_EA);
69 im->ddr.cs_config[0] = CONFIG_SYS_DDR_CS0_CONFIG;
Scott Wood865b8ae2007-04-16 14:54:15 -050070
71 /* Currently we use only one CS, so disable the other bank. */
72 im->ddr.cs_config[1] = 0;
73
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020074 im->ddr.sdram_clk_cntl = CONFIG_SYS_DDR_CLK_CNTL;
75 im->ddr.timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3;
76 im->ddr.timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1;
77 im->ddr.timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2;
78 im->ddr.timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0;
Scott Wood865b8ae2007-04-16 14:54:15 -050079
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020080#ifndef CONFIG_SYS_8313ERDB_BROKEN_PMC
Scott Wood865b8ae2007-04-16 14:54:15 -050081 if (im->pmc.pmccr1 & PMCCR1_POWER_OFF)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020082 im->ddr.sdram_cfg = CONFIG_SYS_SDRAM_CFG | SDRAM_CFG_BI;
Scott Wood865b8ae2007-04-16 14:54:15 -050083 else
84#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020085 im->ddr.sdram_cfg = CONFIG_SYS_SDRAM_CFG;
Scott Wood865b8ae2007-04-16 14:54:15 -050086
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020087 im->ddr.sdram_cfg2 = CONFIG_SYS_SDRAM_CFG2;
88 im->ddr.sdram_mode = CONFIG_SYS_DDR_MODE;
89 im->ddr.sdram_mode2 = CONFIG_SYS_DDR_MODE_2;
Scott Wood865b8ae2007-04-16 14:54:15 -050090
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020091 im->ddr.sdram_interval = CONFIG_SYS_DDR_INTERVAL;
Scott Wood865b8ae2007-04-16 14:54:15 -050092 sync();
93
94 /* enable DDR controller */
95 im->ddr.sdram_cfg |= SDRAM_CFG_MEM_EN;
Scott Woodb71689b2008-06-30 14:13:28 -050096#endif
Scott Wood865b8ae2007-04-16 14:54:15 -050097
98 return msize;
99}
100
Simon Glassd35f3382017-04-06 12:47:05 -0600101int dram_init(void)
Scott Wood865b8ae2007-04-16 14:54:15 -0500102{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200103 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
Becky Bruce0d4cee12010-06-17 11:37:20 -0500104 volatile fsl_lbc_t *lbc = &im->im_lbc;
Scott Wood865b8ae2007-04-16 14:54:15 -0500105 u32 msize;
106
107 if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
Simon Glass39f90ba2017-03-31 08:40:25 -0600108 return -ENXIO;
Scott Wood865b8ae2007-04-16 14:54:15 -0500109
Scott Wood865b8ae2007-04-16 14:54:15 -0500110 /* DDR SDRAM - Main SODIMM */
111 msize = fixed_sdram();
112
113 /* Local Bus setup lbcr and mrtpr */
Mario Sixdc003002019-01-21 09:18:17 +0100114 lbc->lbcr = (0x00040000 | (0xFF << LBCR_BMT_SHIFT) | 0xF);
115 /* LB refresh timer prescal, 266MHz/32 */
116 lbc->mrtpr = 0x20000000;
Scott Wood865b8ae2007-04-16 14:54:15 -0500117 sync();
118
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200119#ifndef CONFIG_SYS_8313ERDB_BROKEN_PMC
Scott Wood865b8ae2007-04-16 14:54:15 -0500120 if (im->pmc.pmccr1 & PMCCR1_POWER_OFF)
121 resume_from_sleep();
122#endif
123
Scott Wood865b8ae2007-04-16 14:54:15 -0500124 /* return total bus SDRAM size(bytes) -- DDR */
Simon Glass39f90ba2017-03-31 08:40:25 -0600125 gd->ram_size = msize;
126
127 return 0;
Scott Wood865b8ae2007-04-16 14:54:15 -0500128}