blob: 77fe7e243f9eed57a71d0f59c5d820bef4b81ff2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marian Balakowicz513b4a12005-10-11 19:09:42 +02002/*
3 * (C) Copyright 2005
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Marian Balakowicz513b4a12005-10-11 19:09:42 +02005 */
6
7#include <common.h>
Simon Glass3bbe70c2019-12-28 10:44:54 -07008#include <fdt_support.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -07009#include <init.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020010#include <ioports.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020012#include <mpc83xx.h>
13#include <asm/mpc8349_pci.h>
14#include <i2c.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020015#include <miiphy.h>
Peter Tyser133c0fe2010-04-12 22:28:07 -050016#include <asm/mmu.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020017#include <pci.h>
Stefan Roesefb9a7302010-08-31 10:00:10 +020018#include <flash.h>
19#include <mtd/cfi_flash.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020020
Wolfgang Denk6405a152006-03-31 18:32:53 +020021DECLARE_GLOBAL_DATA_PTR;
22
Marian Balakowicz513b4a12005-10-11 19:09:42 +020023#define IOSYNC asm("eieio")
24#define ISYNC asm("isync")
25#define SYNC asm("sync")
26#define FPW FLASH_PORT_WIDTH
27#define FPWV FLASH_PORT_WIDTHV
28
29#define DDR_MAX_SIZE_PER_CS 0x20000000
30
31#if defined(DDR_CASLAT_20)
32#define TIMING_CASLAT TIMING_CFG1_CASLAT_20
33#define MODE_CASLAT DDR_MODE_CASLAT_20
34#else
35#define TIMING_CASLAT TIMING_CFG1_CASLAT_25
36#define MODE_CASLAT DDR_MODE_CASLAT_25
37#endif
38
39#define INITIAL_CS_CONFIG (CSCONFIG_EN | CSCONFIG_ROW_BIT_12 | \
40 CSCONFIG_COL_BIT_9)
41
Marian Balakowicz513b4a12005-10-11 19:09:42 +020042/* External definitions */
43ulong flash_get_size (ulong base, int banknum);
Marian Balakowicz513b4a12005-10-11 19:09:42 +020044
45/* Local functions */
46static int detect_num_flash_banks(void);
Wolfgang Denk3da2e9f2011-07-30 23:50:50 +020047static long int get_ddr_bank_size(short cs, long *base);
Bin Mengb5973242016-01-25 00:29:55 -080048static void set_cs_bounds(short cs, ulong base, ulong size);
Marian Balakowicz513b4a12005-10-11 19:09:42 +020049static void set_cs_config(short cs, long config);
50static void set_ddr_config(void);
51
52/* Local variable */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020053static volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
Marian Balakowicz513b4a12005-10-11 19:09:42 +020054
55/**************************************************************************
56 * Board initialzation after relocation to RAM. Used to detect the number
57 * of Flash banks on TQM834x.
58 */
59int board_early_init_r (void) {
60 /* sanity check, IMMARBAR should be mirrored at offset zero of IMMR */
61 if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
62 return 0;
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010063
Marian Balakowicz513b4a12005-10-11 19:09:42 +020064 /* detect the number of Flash banks */
65 return detect_num_flash_banks();
66}
67
68/**************************************************************************
69 * DRAM initalization and size detection
70 */
Simon Glassd35f3382017-04-06 12:47:05 -060071int dram_init(void)
Marian Balakowicz513b4a12005-10-11 19:09:42 +020072{
73 long bank_size;
74 long size;
75 int cs;
76
77 /* during size detection, set up the max DDRLAW size */
Mario Sixc9f92772019-01-21 09:18:15 +010078 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz513b4a12005-10-11 19:09:42 +020079 im->sysconf.ddrlaw[0].ar = (LAWAR_EN | LAWAR_SIZE_2G);
80
81 /* set CS bounds to maximum size */
82 for(cs = 0; cs < 4; ++cs) {
83 set_cs_bounds(cs,
Mario Sixc9f92772019-01-21 09:18:15 +010084 CONFIG_SYS_SDRAM_BASE + (cs * DDR_MAX_SIZE_PER_CS),
Marian Balakowicz513b4a12005-10-11 19:09:42 +020085 DDR_MAX_SIZE_PER_CS);
86
87 set_cs_config(cs, INITIAL_CS_CONFIG);
88 }
89
90 /* configure ddr controller */
91 set_ddr_config();
92
93 udelay(200);
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010094
Marian Balakowicz513b4a12005-10-11 19:09:42 +020095 /* enable DDR controller */
96 im->ddr.sdram_cfg = (SDRAM_CFG_MEM_EN |
97 SDRAM_CFG_SREN |
Kim Phillips3b9c20f2007-08-16 22:52:48 -050098 SDRAM_CFG_SDRAM_TYPE_DDR1);
Marian Balakowicz513b4a12005-10-11 19:09:42 +020099 SYNC;
100
101 /* size detection */
102 debug("\n");
103 size = 0;
104 for(cs = 0; cs < 4; ++cs) {
105 debug("\nDetecting Bank%d\n", cs);
106
107 bank_size = get_ddr_bank_size(cs,
Mario Sixc9f92772019-01-21 09:18:15 +0100108 (long *)(CONFIG_SYS_SDRAM_BASE + size));
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200109 size += bank_size;
110
Marek Vasut71a14a62011-10-21 14:17:10 +0000111 debug("DDR Bank%d size: %ld MiB\n\n", cs, bank_size >> 20);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200112
113 /* exit if less than one bank */
114 if(size < DDR_MAX_SIZE_PER_CS) break;
115 }
116
Simon Glass39f90ba2017-03-31 08:40:25 -0600117 gd->ram_size = size;
118
119 return 0;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200120}
121
122/**************************************************************************
123 * checkboard()
124 */
125int checkboard (void)
126{
127 puts("Board: TQM834x\n");
128
129#ifdef CONFIG_PCI
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200130 volatile immap_t * immr;
131 u32 w, f;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200132
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200133 immr = (immap_t *)CONFIG_SYS_IMMR;
Dave Liu0b6bc772006-12-07 21:11:58 +0800134 if (!(immr->reset.rcwh & HRCWH_PCI_HOST)) {
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200135 printf("PCI: NOT in host mode..?!\n");
136 return 0;
137 }
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200138
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200139 /* get bus width */
140 w = 32;
Dave Liu0b6bc772006-12-07 21:11:58 +0800141 if (immr->reset.rcwh & HRCWH_64_BIT_PCI)
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200142 w = 64;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200143
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200144 /* get clock */
145 f = gd->pci_clk;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200146
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200147 printf("PCI1: %d bit, %d MHz\n", w, f / 1000000);
148#else
149 printf("PCI: disabled\n");
150#endif
151 return 0;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200152}
153
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200154
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200155/**************************************************************************
156 *
157 * Local functions
158 *
159 *************************************************************************/
160
161/**************************************************************************
162 * Detect the number of flash banks (1 or 2). Store it in
163 * a global variable tqm834x_num_flash_banks.
164 * Bank detection code based on the Monitor code.
165 */
166static int detect_num_flash_banks(void)
167{
168 typedef unsigned long FLASH_PORT_WIDTH;
169 typedef volatile unsigned long FLASH_PORT_WIDTHV;
170 FPWV *bank1_base;
171 FPWV *bank2_base;
172 FPW bank1_read;
173 FPW bank2_read;
174 ulong bank1_size;
175 ulong bank2_size;
176 ulong total_size;
177
Stefan Roesefb9a7302010-08-31 10:00:10 +0200178 cfi_flash_num_flash_banks = 2; /* assume two banks */
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100179
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200180 /* Get bank 1 and 2 information */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200181 bank1_size = flash_get_size(CONFIG_SYS_FLASH_BASE, 0);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200182 debug("Bank1 size: %lu\n", bank1_size);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200183 bank2_size = flash_get_size(CONFIG_SYS_FLASH_BASE + bank1_size, 1);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200184 debug("Bank2 size: %lu\n", bank2_size);
185 total_size = bank1_size + bank2_size;
186
187 if (bank2_size > 0) {
188 /* Seems like we've got bank 2, but maybe it's mirrored 1 */
189
190 /* Set the base addresses */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200191 bank1_base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
192 bank2_base = (FPWV *) (CONFIG_SYS_FLASH_BASE + bank1_size);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200193
194 /* Put bank 2 into CFI command mode and read */
195 bank2_base[0x55] = 0x00980098;
196 IOSYNC;
197 ISYNC;
198 bank2_read = bank2_base[0x10];
199
200 /* Read from bank 1 (it's in read mode) */
201 bank1_read = bank1_base[0x10];
202
203 /* Reset Flash */
204 bank1_base[0] = 0x00F000F0;
205 bank2_base[0] = 0x00F000F0;
206
207 if (bank2_read == bank1_read) {
208 /*
209 * Looks like just one bank, but not sure yet. Let's
210 * read from bank 2 in autosoelect mode.
211 */
212 bank2_base[0x0555] = 0x00AA00AA;
213 bank2_base[0x02AA] = 0x00550055;
214 bank2_base[0x0555] = 0x00900090;
215 IOSYNC;
216 ISYNC;
217 bank2_read = bank2_base[0x10];
218
219 /* Read from bank 1 (it's in read mode) */
220 bank1_read = bank1_base[0x10];
221
222 /* Reset Flash */
223 bank1_base[0] = 0x00F000F0;
224 bank2_base[0] = 0x00F000F0;
225
226 if (bank2_read == bank1_read) {
227 /*
228 * In both CFI command and autoselect modes,
229 * we got the some data reading from Flash.
230 * There is only one mirrored bank.
231 */
Stefan Roesefb9a7302010-08-31 10:00:10 +0200232 cfi_flash_num_flash_banks = 1;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200233 total_size = bank1_size;
234 }
235 }
236 }
237
Stefan Roesefb9a7302010-08-31 10:00:10 +0200238 debug("Number of flash banks detected: %d\n", cfi_flash_num_flash_banks);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200239
240 /* set OR0 and BR0 */
Mario Six560f2e92019-01-21 09:18:00 +0100241 set_lbc_or(0, OR_GPCM_CSNT | OR_GPCM_ACS_DIV4 | OR_GPCM_SCY_5 |
242 OR_GPCM_TRLX | (-(total_size) & OR_GPCM_AM));
Becky Bruce0d4cee12010-06-17 11:37:20 -0500243 set_lbc_br(0, (CONFIG_SYS_FLASH_BASE & BR_BA) |
244 (BR_MS_GPCM | BR_PS_32 | BR_V));
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200245
246 return (0);
247}
248
249/*************************************************************************
250 * Detect the size of a ddr bank. Sets CS bounds and CS config accordingly.
251 */
Wolfgang Denk3da2e9f2011-07-30 23:50:50 +0200252static long int get_ddr_bank_size(short cs, long *base)
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200253{
254 /* This array lists all valid DDR SDRAM configurations, with
255 * Bank sizes in bytes. (Refer to Table 9-27 in the MPC8349E RM).
256 * The last entry has to to have size equal 0 and is igonred during
257 * autodection. Bank sizes must be in increasing order of size
258 */
259 struct {
260 long row;
261 long col;
262 long size;
263 } conf[] = {
264 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_8, 32 << 20},
265 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_9, 64 << 20},
266 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_10, 128 << 20},
267 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_9, 128 << 20},
268 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_10, 256 << 20},
269 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_11, 512 << 20},
270 {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_10, 512 << 20},
271 {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_11, 1024 << 20},
272 {0, 0, 0}
273 };
274
275 int i;
276 int detected;
277 long size;
278
279 detected = -1;
280 for(i = 0; conf[i].size != 0; ++i) {
281
282 /* set sdram bank configuration */
283 set_cs_config(cs, CSCONFIG_EN | conf[i].col | conf[i].row);
284
285 debug("Getting RAM size...\n");
286 size = get_ram_size(base, DDR_MAX_SIZE_PER_CS);
287
288 if((size == conf[i].size) && (i == detected + 1))
289 detected = i;
290
291 debug("Trying %ld x %ld (%ld MiB) at addr %p, detected: %ld MiB\n",
292 conf[i].row,
293 conf[i].col,
294 conf[i].size >> 20,
295 base,
296 size >> 20);
297 }
298
299 if(detected == -1){
300 /* disable empty cs */
301 debug("\nNo valid configurations for CS%d, disabling...\n", cs);
302 set_cs_config(cs, 0);
303 return 0;
304 }
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100305
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200306 debug("\nDetected configuration %ld x %ld (%ld MiB) at addr %p\n",
307 conf[detected].row, conf[detected].col, conf[detected].size >> 20, base);
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100308
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200309 /* configure cs ro detected params */
310 set_cs_config(cs, CSCONFIG_EN | conf[detected].row |
311 conf[detected].col);
312
313 set_cs_bounds(cs, (long)base, conf[detected].size);
314
315 return(conf[detected].size);
316}
317
318/**************************************************************************
319 * Sets DDR bank CS bounds.
320 */
Bin Mengb5973242016-01-25 00:29:55 -0800321static void set_cs_bounds(short cs, ulong base, ulong size)
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200322{
Marek Vasut71a14a62011-10-21 14:17:10 +0000323 debug("Setting bounds %08lx, %08lx for cs %d\n", base, size, cs);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200324 if(size == 0){
325 im->ddr.csbnds[cs].csbnds = 0x00000000;
326 } else {
327 im->ddr.csbnds[cs].csbnds =
328 ((base >> CSBNDS_SA_SHIFT) & CSBNDS_SA) |
329 (((base + size - 1) >> CSBNDS_EA_SHIFT) &
330 CSBNDS_EA);
331 }
332 SYNC;
333}
334
335/**************************************************************************
336 * Sets DDR banks CS configuration.
337 * config == 0x00000000 disables the CS.
338 */
339static void set_cs_config(short cs, long config)
340{
Marek Vasut71a14a62011-10-21 14:17:10 +0000341 debug("Setting config %08lx for cs %d\n", config, cs);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200342 im->ddr.cs_config[cs] = config;
343 SYNC;
344}
345
346/**************************************************************************
347 * Sets DDR clocks, timings and configuration.
348 */
349static void set_ddr_config(void) {
350 /* clock control */
351 im->ddr.sdram_clk_cntl = DDR_SDRAM_CLK_CNTL_SS_EN |
352 DDR_SDRAM_CLK_CNTL_CLK_ADJUST_05;
353 SYNC;
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100354
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200355 /* timing configuration */
356 im->ddr.timing_cfg_1 =
357 (4 << TIMING_CFG1_PRETOACT_SHIFT) |
358 (7 << TIMING_CFG1_ACTTOPRE_SHIFT) |
359 (4 << TIMING_CFG1_ACTTORW_SHIFT) |
360 (5 << TIMING_CFG1_REFREC_SHIFT) |
361 (3 << TIMING_CFG1_WRREC_SHIFT) |
362 (3 << TIMING_CFG1_ACTTOACT_SHIFT) |
363 (1 << TIMING_CFG1_WRTORD_SHIFT) |
364 (TIMING_CFG1_CASLAT & TIMING_CASLAT);
365
366 im->ddr.timing_cfg_2 =
367 TIMING_CFG2_CPO_DEF |
368 (2 << TIMING_CFG2_WR_DATA_DELAY_SHIFT);
369 SYNC;
370
371 /* don't enable DDR controller yet */
372 im->ddr.sdram_cfg =
373 SDRAM_CFG_SREN |
Kim Phillips3b9c20f2007-08-16 22:52:48 -0500374 SDRAM_CFG_SDRAM_TYPE_DDR1;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200375 SYNC;
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100376
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200377 /* Set SDRAM mode */
378 im->ddr.sdram_mode =
379 ((DDR_MODE_EXT_MODEREG | DDR_MODE_WEAK) <<
380 SDRAM_MODE_ESD_SHIFT) |
381 ((DDR_MODE_MODEREG | DDR_MODE_BLEN_4) <<
382 SDRAM_MODE_SD_SHIFT) |
383 ((DDR_MODE_CASLAT << SDRAM_MODE_SD_SHIFT) &
384 MODE_CASLAT);
385 SYNC;
386
387 /* Set fast SDRAM refresh rate */
388 im->ddr.sdram_interval =
389 (DDR_REFINT_166MHZ_7US << SDRAM_INTERVAL_REFINT_SHIFT) |
390 (DDR_BSTOPRE << SDRAM_INTERVAL_BSTOPRE_SHIFT);
391 SYNC;
Wolfgang Denk1305bd42006-06-16 16:53:06 +0200392
393 /* Workaround for DDR6 Erratum
394 * see MPC8349E Device Errata Rev.8, 2/2006
395 * This workaround influences the MPC internal "input enables"
396 * dependent on CAS latency and MPC revision. According to errata
397 * sheet the internal reserved registers for this workaround are
398 * not available from revision 2.0 and up.
399 */
400
401 /* Get REVID from register SPRIDR. Skip workaround if rev >= 2.0
402 * (0x200)
403 */
404 if ((im->sysconf.spridr & SPRIDR_REVID) < 0x200) {
405
406 /* There is a internal reserved register at IMMRBAR+0x2F00
407 * which has to be written with a certain value defined by
408 * errata sheet.
409 */
Wolfgang Denk31560d12006-07-21 15:24:56 +0200410 u32 *reserved_p = (u32 *)((u8 *)im + 0x2f00);
411
Wolfgang Denk1305bd42006-06-16 16:53:06 +0200412#if defined(DDR_CASLAT_20)
Wolfgang Denk31560d12006-07-21 15:24:56 +0200413 *reserved_p = 0x201c0000;
Wolfgang Denk1305bd42006-06-16 16:53:06 +0200414#else
Wolfgang Denk31560d12006-07-21 15:24:56 +0200415 *reserved_p = 0x202c0000;
Wolfgang Denk1305bd42006-06-16 16:53:06 +0200416#endif
417 }
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200418}
Wolfgang Denk95593572009-05-14 23:18:34 +0200419
420#ifdef CONFIG_OF_BOARD_SETUP
Simon Glass2aec3cc2014-10-23 18:58:47 -0600421int ft_board_setup(void *blob, bd_t *bd)
Wolfgang Denk95593572009-05-14 23:18:34 +0200422{
423 ft_cpu_setup(blob, bd);
424
425#ifdef CONFIG_PCI
426 ft_pci_setup(blob, bd);
427#endif /* CONFIG_PCI */
Simon Glass2aec3cc2014-10-23 18:58:47 -0600428
429 return 0;
Wolfgang Denk95593572009-05-14 23:18:34 +0200430}
431#endif /* CONFIG_OF_BOARD_SETUP */