blob: 5451437f37f68eea79a62c4bd8177d220e0b6b80 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marian Balakowiczd7a3f722006-03-14 16:24:38 +01002/*
3 * (C) Copyright 2006
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Marian Balakowiczd7a3f722006-03-14 16:24:38 +01005 */
6
7#include <common.h>
Simon Glass3bbe70c2019-12-28 10:44:54 -07008#include <fdt_support.h>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010010#include <ioports.h>
11#include <mpc83xx.h>
12#include <asm/mpc8349_pci.h>
13#include <i2c.h>
Ben Warren81362c12008-01-16 22:37:42 -050014#include <spi.h>
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010015#include <miiphy.h>
York Sunf0626592013-09-30 09:22:09 -070016#ifdef CONFIG_SYS_FSL_DDR2
17#include <fsl_ddr_sdram.h>
York Sunc3c301e2011-08-26 11:32:45 -070018#else
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010019#include <spd_sdram.h>
York Sunc3c301e2011-08-26 11:32:45 -070020#endif
Jon Loeligerde9737d2008-03-04 10:03:03 -060021
Kim Phillips3204c7c2007-12-20 15:57:28 -060022#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamada75f82d02018-03-05 01:20:11 +090023#include <linux/libfdt.h>
Kim Phillips774e1b52006-11-01 00:10:40 -060024#endif
25
Simon Glass39f90ba2017-03-31 08:40:25 -060026DECLARE_GLOBAL_DATA_PTR;
27
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010028int fixed_sdram(void);
29void sdram_init(void);
30
Peter Tyser62e73982009-05-22 17:23:24 -050031#if defined(CONFIG_DDR_ECC) && defined(CONFIG_MPC83xx)
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010032void ddr_enable_ecc(unsigned int dram_size);
33#endif
34
35int board_early_init_f (void)
36{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020037 volatile u8* bcsr = (volatile u8*)CONFIG_SYS_BCSR;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010038
39 /* Enable flash write */
40 bcsr[1] &= ~0x01;
41
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020042#ifdef CONFIG_SYS_USE_MPC834XSYS_USB_PHY
Kumar Gala4c7efd82006-04-20 13:45:32 -050043 /* Use USB PHY on SYS board */
44 bcsr[5] |= 0x02;
45#endif
46
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010047 return 0;
48}
49
50#define ns2clk(ns) (ns / (1000000000 / CONFIG_8349_CLKIN) + 1)
51
Simon Glassd35f3382017-04-06 12:47:05 -060052int dram_init(void)
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010053{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
York Sunc3c301e2011-08-26 11:32:45 -070055 phys_size_t msize = 0;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010056
57 if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
Simon Glass39f90ba2017-03-31 08:40:25 -060058 return -ENXIO;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010059
60 /* DDR SDRAM - Main SODIMM */
Mario Sixc9f92772019-01-21 09:18:15 +010061 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE & LAWBAR_BAR;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010062#if defined(CONFIG_SPD_EEPROM)
York Sunf0626592013-09-30 09:22:09 -070063#ifndef CONFIG_SYS_FSL_DDR2
York Sunc3c301e2011-08-26 11:32:45 -070064 msize = spd_sdram() * 1024 * 1024;
65#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
66 ddr_enable_ecc(msize);
67#endif
68#else
69 msize = fsl_ddr_sdram();
70#endif
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010071#else
York Sunc3c301e2011-08-26 11:32:45 -070072 msize = fixed_sdram() * 1024 * 1024;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010073#endif
74 /*
75 * Initialize SDRAM if it is on local bus.
76 */
77 sdram_init();
78
Simon Glass39f90ba2017-03-31 08:40:25 -060079 /* set total bus SDRAM size(bytes) -- DDR */
80 gd->ram_size = msize;
81
82 return 0;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010083}
84
85#if !defined(CONFIG_SPD_EEPROM)
86/*************************************************************************
87 * fixed sdram init -- doesn't use serial presence detect.
88 ************************************************************************/
89int fixed_sdram(void)
90{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020091 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
Joe Hershberger5ade3902011-10-11 23:57:31 -050092 u32 msize = CONFIG_SYS_DDR_SIZE;
93 u32 ddr_size = msize << 20; /* DDR size in bytes */
94 u32 ddr_size_log2 = __ilog2(ddr_size);
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010095
Mario Six805cac12019-01-21 09:18:16 +010096 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE & 0xfffff000;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +010097 im->sysconf.ddrlaw[0].ar = LAWAR_EN | ((ddr_size_log2 - 1) & LAWAR_SIZE);
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +010098
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020099#if (CONFIG_SYS_DDR_SIZE != 256)
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100100#warning Currenly any ddr size other than 256 is not supported
101#endif
Xie Xiaobo6149a5a2007-02-14 18:27:17 +0800102#ifdef CONFIG_DDR_II
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200103 im->ddr.csbnds[2].csbnds = CONFIG_SYS_DDR_CS2_BNDS;
104 im->ddr.cs_config[2] = CONFIG_SYS_DDR_CS2_CONFIG;
105 im->ddr.timing_cfg_0 = CONFIG_SYS_DDR_TIMING_0;
106 im->ddr.timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1;
107 im->ddr.timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2;
108 im->ddr.timing_cfg_3 = CONFIG_SYS_DDR_TIMING_3;
109 im->ddr.sdram_cfg = CONFIG_SYS_DDR_SDRAM_CFG;
110 im->ddr.sdram_cfg2 = CONFIG_SYS_DDR_SDRAM_CFG2;
111 im->ddr.sdram_mode = CONFIG_SYS_DDR_MODE;
112 im->ddr.sdram_mode2 = CONFIG_SYS_DDR_MODE2;
113 im->ddr.sdram_interval = CONFIG_SYS_DDR_INTERVAL;
114 im->ddr.sdram_clk_cntl = CONFIG_SYS_DDR_CLK_CNTL;
Xie Xiaobo6149a5a2007-02-14 18:27:17 +0800115#else
Joe Hershberger5ade3902011-10-11 23:57:31 -0500116
Mario Six805cac12019-01-21 09:18:16 +0100117#if ((CONFIG_SYS_SDRAM_BASE & 0x00FFFFFF) != 0)
Joe Hershberger5ade3902011-10-11 23:57:31 -0500118#warning Chip select bounds is only configurable in 16MB increments
119#endif
120 im->ddr.csbnds[2].csbnds =
Mario Six805cac12019-01-21 09:18:16 +0100121 ((CONFIG_SYS_SDRAM_BASE >> CSBNDS_SA_SHIFT) & CSBNDS_SA) |
122 (((CONFIG_SYS_SDRAM_BASE + ddr_size - 1) >>
Joe Hershberger5ade3902011-10-11 23:57:31 -0500123 CSBNDS_EA_SHIFT) & CSBNDS_EA);
124 im->ddr.cs_config[2] = CONFIG_SYS_DDR_CS2_CONFIG;
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +0100125
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200126 /* currently we use only one CS, so disable the other banks */
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +0100127 im->ddr.cs_config[0] = 0;
128 im->ddr.cs_config[1] = 0;
129 im->ddr.cs_config[3] = 0;
130
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200131 im->ddr.timing_cfg_1 = CONFIG_SYS_DDR_TIMING_1;
132 im->ddr.timing_cfg_2 = CONFIG_SYS_DDR_TIMING_2;
Wolfgang Denkebd3deb2006-04-16 10:51:58 +0200133
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100134 im->ddr.sdram_cfg =
135 SDRAM_CFG_SREN
136#if defined(CONFIG_DDR_2T_TIMING)
137 | SDRAM_CFG_2T_EN
138#endif
139 | 2 << SDRAM_CFG_SDRAM_TYPE_SHIFT;
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +0100140#if defined (CONFIG_DDR_32BIT)
141 /* for 32-bit mode burst length is 8 */
142 im->ddr.sdram_cfg |= (SDRAM_CFG_32_BE | SDRAM_CFG_8_BE);
143#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200144 im->ddr.sdram_mode = CONFIG_SYS_DDR_MODE;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100145
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200146 im->ddr.sdram_interval = CONFIG_SYS_DDR_INTERVAL;
Xie Xiaobo6149a5a2007-02-14 18:27:17 +0800147#endif
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100148 udelay(200);
149
Rafal Jaworowski4a9b6aa2006-03-16 17:46:46 +0100150 /* enable DDR controller */
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100151 im->ddr.sdram_cfg |= SDRAM_CFG_MEM_EN;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100152 return msize;
153}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200154#endif/*!CONFIG_SYS_SPD_EEPROM*/
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100155
156
157int checkboard (void)
158{
Ira W. Snyder4adfd022008-08-22 11:00:15 -0700159 /*
160 * Warning: do not read the BCSR registers here
161 *
162 * There is a timing bug in the 8349E and 8349EA BCSR code
163 * version 1.2 (read from BCSR 11) that will cause the CFI
164 * flash initialization code to overwrite BCSR 0, disabling
165 * the serial ports and gigabit ethernet
166 */
167
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100168 puts("Board: Freescale MPC8349EMDS\n");
169 return 0;
170}
171
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100172/*
173 * if MPC8349EMDS is soldered with SDRAM
174 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200175#if defined(CONFIG_SYS_BR2_PRELIM) \
176 && defined(CONFIG_SYS_OR2_PRELIM) \
177 && defined(CONFIG_SYS_LBLAWBAR2_PRELIM) \
178 && defined(CONFIG_SYS_LBLAWAR2_PRELIM)
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100179/*
180 * Initialize SDRAM memory on the Local Bus.
181 */
182
183void sdram_init(void)
184{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200185 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
Becky Bruce0d4cee12010-06-17 11:37:20 -0500186 volatile fsl_lbc_t *lbc = &immap->im_lbc;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200187 uint *sdram_addr = (uint *)CONFIG_SYS_LBC_SDRAM_BASE;
Mario Sixdc003002019-01-21 09:18:17 +0100188 const u32 lsdmr_common = LSDMR_RFEN | LSDMR_BSMA1516 | LSDMR_RFCR8 |
189 LSDMR_PRETOACT6 | LSDMR_ACTTORW3 | LSDMR_BL8 |
190 LSDMR_WRC3 | LSDMR_CL3;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100191 /*
192 * Setup SDRAM Base and Option Registers, already done in cpu_init.c
193 */
194
195 /* setup mtrpt, lsrt and lbcr for LB bus */
Mario Sixdc003002019-01-21 09:18:17 +0100196 lbc->lbcr = 0x00000000;
197 /* LB refresh timer prescal, 266MHz/32 */
198 lbc->mrtpr = 0x20000000;
199 /* LB sdram refresh timer, about 6us */
200 lbc->lsrt = 0x32000000;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100201 asm("sync");
202
203 /*
204 * Configure the SDRAM controller Machine Mode Register.
205 */
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100206
Mario Sixdc003002019-01-21 09:18:17 +0100207 /* 0x40636733; normal operation */
208 lbc->lsdmr = lsdmr_common | LSDMR_OP_NORMAL;
209
210 /* 0x68636733; precharge all the banks */
211 lbc->lsdmr = lsdmr_common | LSDMR_OP_PCHALL;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100212 asm("sync");
213 *sdram_addr = 0xff;
214 udelay(100);
215
Mario Sixdc003002019-01-21 09:18:17 +0100216 /* 0x48636733; auto refresh */
217 lbc->lsdmr = lsdmr_common | LSDMR_OP_ARFRSH;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100218 asm("sync");
219 /*1 times*/
220 *sdram_addr = 0xff;
221 udelay(100);
222 /*2 times*/
223 *sdram_addr = 0xff;
224 udelay(100);
225 /*3 times*/
226 *sdram_addr = 0xff;
227 udelay(100);
228 /*4 times*/
229 *sdram_addr = 0xff;
230 udelay(100);
231 /*5 times*/
232 *sdram_addr = 0xff;
233 udelay(100);
234 /*6 times*/
235 *sdram_addr = 0xff;
236 udelay(100);
237 /*7 times*/
238 *sdram_addr = 0xff;
239 udelay(100);
240 /*8 times*/
241 *sdram_addr = 0xff;
242 udelay(100);
243
244 /* 0x58636733; mode register write operation */
Mario Sixdc003002019-01-21 09:18:17 +0100245 lbc->lsdmr = lsdmr_common | LSDMR_OP_MRW;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100246 asm("sync");
247 *sdram_addr = 0xff;
248 udelay(100);
249
Mario Sixdc003002019-01-21 09:18:17 +0100250 /* 0x40636733; normal operation */
251 lbc->lsdmr = lsdmr_common | LSDMR_OP_NORMAL;
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100252 asm("sync");
253 *sdram_addr = 0xff;
254 udelay(100);
255}
256#else
257void sdram_init(void)
258{
Marian Balakowiczd7a3f722006-03-14 16:24:38 +0100259}
260#endif
Marian Balakowicz52ee4bd2006-03-16 15:19:35 +0100261
Ben Warren81362c12008-01-16 22:37:42 -0500262/*
263 * The following are used to control the SPI chip selects for the SPI command.
264 */
Ben Warren20582da2008-06-08 23:28:33 -0700265#ifdef CONFIG_MPC8XXX_SPI
Ben Warren81362c12008-01-16 22:37:42 -0500266
267#define SPI_CS_MASK 0x80000000
268
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200269int spi_cs_is_valid(unsigned int bus, unsigned int cs)
270{
271 return bus == 0 && cs == 0;
272}
273
274void spi_cs_activate(struct spi_slave *slave)
Ben Warren81362c12008-01-16 22:37:42 -0500275{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200276 volatile gpio83xx_t *iopd = &((immap_t *)CONFIG_SYS_IMMR)->gpio[0];
Ben Warren81362c12008-01-16 22:37:42 -0500277
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200278 iopd->dat &= ~SPI_CS_MASK;
Ben Warren81362c12008-01-16 22:37:42 -0500279}
280
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200281void spi_cs_deactivate(struct spi_slave *slave)
282{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200283 volatile gpio83xx_t *iopd = &((immap_t *)CONFIG_SYS_IMMR)->gpio[0];
Ben Warren81362c12008-01-16 22:37:42 -0500284
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200285 iopd->dat |= SPI_CS_MASK;
286}
Jagan Teki5931fbb2018-11-24 14:31:12 +0530287#endif
Ben Warren81362c12008-01-16 22:37:42 -0500288
Kim Phillips21416812007-08-15 22:30:33 -0500289#if defined(CONFIG_OF_BOARD_SETUP)
Simon Glass2aec3cc2014-10-23 18:58:47 -0600290int ft_board_setup(void *blob, bd_t *bd)
Kim Phillips774e1b52006-11-01 00:10:40 -0600291{
Kim Phillips21416812007-08-15 22:30:33 -0500292 ft_cpu_setup(blob, bd);
293#ifdef CONFIG_PCI
294 ft_pci_setup(blob, bd);
295#endif
Simon Glass2aec3cc2014-10-23 18:58:47 -0600296
297 return 0;
Kim Phillips774e1b52006-11-01 00:10:40 -0600298}
299#endif