blob: 17b4662c167f3f7e89ee39bb2982d874ef461847 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020014#include <asm/mpc8349_pci.h>
15#include <i2c.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020016#include <miiphy.h>
Peter Tyser133c0fe2010-04-12 22:28:07 -050017#include <asm/mmu.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020018#include <pci.h>
Stefan Roesefb9a7302010-08-31 10:00:10 +020019#include <flash.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Stefan Roesefb9a7302010-08-31 10:00:10 +020021#include <mtd/cfi_flash.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020022
Wolfgang Denk6405a152006-03-31 18:32:53 +020023DECLARE_GLOBAL_DATA_PTR;
24
Marian Balakowicz513b4a12005-10-11 19:09:42 +020025#define IOSYNC asm("eieio")
26#define ISYNC asm("isync")
27#define SYNC asm("sync")
28#define FPW FLASH_PORT_WIDTH
29#define FPWV FLASH_PORT_WIDTHV
30
31#define DDR_MAX_SIZE_PER_CS 0x20000000
32
33#if defined(DDR_CASLAT_20)
34#define TIMING_CASLAT TIMING_CFG1_CASLAT_20
35#define MODE_CASLAT DDR_MODE_CASLAT_20
36#else
37#define TIMING_CASLAT TIMING_CFG1_CASLAT_25
38#define MODE_CASLAT DDR_MODE_CASLAT_25
39#endif
40
41#define INITIAL_CS_CONFIG (CSCONFIG_EN | CSCONFIG_ROW_BIT_12 | \
42 CSCONFIG_COL_BIT_9)
43
Marian Balakowicz513b4a12005-10-11 19:09:42 +020044/* External definitions */
45ulong flash_get_size (ulong base, int banknum);
Marian Balakowicz513b4a12005-10-11 19:09:42 +020046
47/* Local functions */
48static int detect_num_flash_banks(void);
Wolfgang Denk3da2e9f2011-07-30 23:50:50 +020049static long int get_ddr_bank_size(short cs, long *base);
Bin Mengb5973242016-01-25 00:29:55 -080050static void set_cs_bounds(short cs, ulong base, ulong size);
Marian Balakowicz513b4a12005-10-11 19:09:42 +020051static void set_cs_config(short cs, long config);
52static void set_ddr_config(void);
53
54/* Local variable */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020055static volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
Marian Balakowicz513b4a12005-10-11 19:09:42 +020056
57/**************************************************************************
58 * Board initialzation after relocation to RAM. Used to detect the number
59 * of Flash banks on TQM834x.
60 */
61int board_early_init_r (void) {
62 /* sanity check, IMMARBAR should be mirrored at offset zero of IMMR */
63 if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
64 return 0;
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010065
Marian Balakowicz513b4a12005-10-11 19:09:42 +020066 /* detect the number of Flash banks */
67 return detect_num_flash_banks();
68}
69
70/**************************************************************************
71 * DRAM initalization and size detection
72 */
Simon Glassd35f3382017-04-06 12:47:05 -060073int dram_init(void)
Marian Balakowicz513b4a12005-10-11 19:09:42 +020074{
75 long bank_size;
76 long size;
77 int cs;
78
79 /* during size detection, set up the max DDRLAW size */
Mario Sixc9f92772019-01-21 09:18:15 +010080 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz513b4a12005-10-11 19:09:42 +020081 im->sysconf.ddrlaw[0].ar = (LAWAR_EN | LAWAR_SIZE_2G);
82
83 /* set CS bounds to maximum size */
84 for(cs = 0; cs < 4; ++cs) {
85 set_cs_bounds(cs,
Mario Sixc9f92772019-01-21 09:18:15 +010086 CONFIG_SYS_SDRAM_BASE + (cs * DDR_MAX_SIZE_PER_CS),
Marian Balakowicz513b4a12005-10-11 19:09:42 +020087 DDR_MAX_SIZE_PER_CS);
88
89 set_cs_config(cs, INITIAL_CS_CONFIG);
90 }
91
92 /* configure ddr controller */
93 set_ddr_config();
94
95 udelay(200);
Wolfgang Denkf6a692b2005-12-04 00:40:34 +010096
Marian Balakowicz513b4a12005-10-11 19:09:42 +020097 /* enable DDR controller */
98 im->ddr.sdram_cfg = (SDRAM_CFG_MEM_EN |
99 SDRAM_CFG_SREN |
Kim Phillips3b9c20f2007-08-16 22:52:48 -0500100 SDRAM_CFG_SDRAM_TYPE_DDR1);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200101 SYNC;
102
103 /* size detection */
104 debug("\n");
105 size = 0;
106 for(cs = 0; cs < 4; ++cs) {
107 debug("\nDetecting Bank%d\n", cs);
108
109 bank_size = get_ddr_bank_size(cs,
Mario Sixc9f92772019-01-21 09:18:15 +0100110 (long *)(CONFIG_SYS_SDRAM_BASE + size));
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200111 size += bank_size;
112
Marek Vasut71a14a62011-10-21 14:17:10 +0000113 debug("DDR Bank%d size: %ld MiB\n\n", cs, bank_size >> 20);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200114
115 /* exit if less than one bank */
116 if(size < DDR_MAX_SIZE_PER_CS) break;
117 }
118
Simon Glass39f90ba2017-03-31 08:40:25 -0600119 gd->ram_size = size;
120
121 return 0;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200122}
123
124/**************************************************************************
125 * checkboard()
126 */
127int checkboard (void)
128{
129 puts("Board: TQM834x\n");
130
131#ifdef CONFIG_PCI
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200132 volatile immap_t * immr;
133 u32 w, f;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200134
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200135 immr = (immap_t *)CONFIG_SYS_IMMR;
Dave Liu0b6bc772006-12-07 21:11:58 +0800136 if (!(immr->reset.rcwh & HRCWH_PCI_HOST)) {
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200137 printf("PCI: NOT in host mode..?!\n");
138 return 0;
139 }
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200140
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200141 /* get bus width */
142 w = 32;
Dave Liu0b6bc772006-12-07 21:11:58 +0800143 if (immr->reset.rcwh & HRCWH_64_BIT_PCI)
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200144 w = 64;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200145
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200146 /* get clock */
147 f = gd->pci_clk;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200148
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200149 printf("PCI1: %d bit, %d MHz\n", w, f / 1000000);
150#else
151 printf("PCI: disabled\n");
152#endif
153 return 0;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200154}
155
Rafal Jaworowski384da5e2005-10-17 02:39:53 +0200156
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200157/**************************************************************************
158 *
159 * Local functions
160 *
161 *************************************************************************/
162
163/**************************************************************************
164 * Detect the number of flash banks (1 or 2). Store it in
165 * a global variable tqm834x_num_flash_banks.
166 * Bank detection code based on the Monitor code.
167 */
168static int detect_num_flash_banks(void)
169{
170 typedef unsigned long FLASH_PORT_WIDTH;
171 typedef volatile unsigned long FLASH_PORT_WIDTHV;
172 FPWV *bank1_base;
173 FPWV *bank2_base;
174 FPW bank1_read;
175 FPW bank2_read;
176 ulong bank1_size;
177 ulong bank2_size;
178 ulong total_size;
179
Stefan Roesefb9a7302010-08-31 10:00:10 +0200180 cfi_flash_num_flash_banks = 2; /* assume two banks */
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100181
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200182 /* Get bank 1 and 2 information */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200183 bank1_size = flash_get_size(CONFIG_SYS_FLASH_BASE, 0);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200184 debug("Bank1 size: %lu\n", bank1_size);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200185 bank2_size = flash_get_size(CONFIG_SYS_FLASH_BASE + bank1_size, 1);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200186 debug("Bank2 size: %lu\n", bank2_size);
187 total_size = bank1_size + bank2_size;
188
189 if (bank2_size > 0) {
190 /* Seems like we've got bank 2, but maybe it's mirrored 1 */
191
192 /* Set the base addresses */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200193 bank1_base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
194 bank2_base = (FPWV *) (CONFIG_SYS_FLASH_BASE + bank1_size);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200195
196 /* Put bank 2 into CFI command mode and read */
197 bank2_base[0x55] = 0x00980098;
198 IOSYNC;
199 ISYNC;
200 bank2_read = bank2_base[0x10];
201
202 /* Read from bank 1 (it's in read mode) */
203 bank1_read = bank1_base[0x10];
204
205 /* Reset Flash */
206 bank1_base[0] = 0x00F000F0;
207 bank2_base[0] = 0x00F000F0;
208
209 if (bank2_read == bank1_read) {
210 /*
211 * Looks like just one bank, but not sure yet. Let's
212 * read from bank 2 in autosoelect mode.
213 */
214 bank2_base[0x0555] = 0x00AA00AA;
215 bank2_base[0x02AA] = 0x00550055;
216 bank2_base[0x0555] = 0x00900090;
217 IOSYNC;
218 ISYNC;
219 bank2_read = bank2_base[0x10];
220
221 /* Read from bank 1 (it's in read mode) */
222 bank1_read = bank1_base[0x10];
223
224 /* Reset Flash */
225 bank1_base[0] = 0x00F000F0;
226 bank2_base[0] = 0x00F000F0;
227
228 if (bank2_read == bank1_read) {
229 /*
230 * In both CFI command and autoselect modes,
231 * we got the some data reading from Flash.
232 * There is only one mirrored bank.
233 */
Stefan Roesefb9a7302010-08-31 10:00:10 +0200234 cfi_flash_num_flash_banks = 1;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200235 total_size = bank1_size;
236 }
237 }
238 }
239
Stefan Roesefb9a7302010-08-31 10:00:10 +0200240 debug("Number of flash banks detected: %d\n", cfi_flash_num_flash_banks);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200241
242 /* set OR0 and BR0 */
Mario Six560f2e92019-01-21 09:18:00 +0100243 set_lbc_or(0, OR_GPCM_CSNT | OR_GPCM_ACS_DIV4 | OR_GPCM_SCY_5 |
244 OR_GPCM_TRLX | (-(total_size) & OR_GPCM_AM));
Becky Bruce0d4cee12010-06-17 11:37:20 -0500245 set_lbc_br(0, (CONFIG_SYS_FLASH_BASE & BR_BA) |
246 (BR_MS_GPCM | BR_PS_32 | BR_V));
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200247
248 return (0);
249}
250
251/*************************************************************************
252 * Detect the size of a ddr bank. Sets CS bounds and CS config accordingly.
253 */
Wolfgang Denk3da2e9f2011-07-30 23:50:50 +0200254static long int get_ddr_bank_size(short cs, long *base)
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200255{
256 /* This array lists all valid DDR SDRAM configurations, with
257 * Bank sizes in bytes. (Refer to Table 9-27 in the MPC8349E RM).
258 * The last entry has to to have size equal 0 and is igonred during
259 * autodection. Bank sizes must be in increasing order of size
260 */
261 struct {
262 long row;
263 long col;
264 long size;
265 } conf[] = {
266 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_8, 32 << 20},
267 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_9, 64 << 20},
268 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_10, 128 << 20},
269 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_9, 128 << 20},
270 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_10, 256 << 20},
271 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_11, 512 << 20},
272 {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_10, 512 << 20},
273 {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_11, 1024 << 20},
274 {0, 0, 0}
275 };
276
277 int i;
278 int detected;
279 long size;
280
281 detected = -1;
282 for(i = 0; conf[i].size != 0; ++i) {
283
284 /* set sdram bank configuration */
285 set_cs_config(cs, CSCONFIG_EN | conf[i].col | conf[i].row);
286
287 debug("Getting RAM size...\n");
288 size = get_ram_size(base, DDR_MAX_SIZE_PER_CS);
289
290 if((size == conf[i].size) && (i == detected + 1))
291 detected = i;
292
293 debug("Trying %ld x %ld (%ld MiB) at addr %p, detected: %ld MiB\n",
294 conf[i].row,
295 conf[i].col,
296 conf[i].size >> 20,
297 base,
298 size >> 20);
299 }
300
301 if(detected == -1){
302 /* disable empty cs */
303 debug("\nNo valid configurations for CS%d, disabling...\n", cs);
304 set_cs_config(cs, 0);
305 return 0;
306 }
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100307
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200308 debug("\nDetected configuration %ld x %ld (%ld MiB) at addr %p\n",
309 conf[detected].row, conf[detected].col, conf[detected].size >> 20, base);
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100310
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200311 /* configure cs ro detected params */
312 set_cs_config(cs, CSCONFIG_EN | conf[detected].row |
313 conf[detected].col);
314
315 set_cs_bounds(cs, (long)base, conf[detected].size);
316
317 return(conf[detected].size);
318}
319
320/**************************************************************************
321 * Sets DDR bank CS bounds.
322 */
Bin Mengb5973242016-01-25 00:29:55 -0800323static void set_cs_bounds(short cs, ulong base, ulong size)
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200324{
Marek Vasut71a14a62011-10-21 14:17:10 +0000325 debug("Setting bounds %08lx, %08lx for cs %d\n", base, size, cs);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200326 if(size == 0){
327 im->ddr.csbnds[cs].csbnds = 0x00000000;
328 } else {
329 im->ddr.csbnds[cs].csbnds =
330 ((base >> CSBNDS_SA_SHIFT) & CSBNDS_SA) |
331 (((base + size - 1) >> CSBNDS_EA_SHIFT) &
332 CSBNDS_EA);
333 }
334 SYNC;
335}
336
337/**************************************************************************
338 * Sets DDR banks CS configuration.
339 * config == 0x00000000 disables the CS.
340 */
341static void set_cs_config(short cs, long config)
342{
Marek Vasut71a14a62011-10-21 14:17:10 +0000343 debug("Setting config %08lx for cs %d\n", config, cs);
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200344 im->ddr.cs_config[cs] = config;
345 SYNC;
346}
347
348/**************************************************************************
349 * Sets DDR clocks, timings and configuration.
350 */
351static void set_ddr_config(void) {
352 /* clock control */
353 im->ddr.sdram_clk_cntl = DDR_SDRAM_CLK_CNTL_SS_EN |
354 DDR_SDRAM_CLK_CNTL_CLK_ADJUST_05;
355 SYNC;
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100356
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200357 /* timing configuration */
358 im->ddr.timing_cfg_1 =
359 (4 << TIMING_CFG1_PRETOACT_SHIFT) |
360 (7 << TIMING_CFG1_ACTTOPRE_SHIFT) |
361 (4 << TIMING_CFG1_ACTTORW_SHIFT) |
362 (5 << TIMING_CFG1_REFREC_SHIFT) |
363 (3 << TIMING_CFG1_WRREC_SHIFT) |
364 (3 << TIMING_CFG1_ACTTOACT_SHIFT) |
365 (1 << TIMING_CFG1_WRTORD_SHIFT) |
366 (TIMING_CFG1_CASLAT & TIMING_CASLAT);
367
368 im->ddr.timing_cfg_2 =
369 TIMING_CFG2_CPO_DEF |
370 (2 << TIMING_CFG2_WR_DATA_DELAY_SHIFT);
371 SYNC;
372
373 /* don't enable DDR controller yet */
374 im->ddr.sdram_cfg =
375 SDRAM_CFG_SREN |
Kim Phillips3b9c20f2007-08-16 22:52:48 -0500376 SDRAM_CFG_SDRAM_TYPE_DDR1;
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200377 SYNC;
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100378
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200379 /* Set SDRAM mode */
380 im->ddr.sdram_mode =
381 ((DDR_MODE_EXT_MODEREG | DDR_MODE_WEAK) <<
382 SDRAM_MODE_ESD_SHIFT) |
383 ((DDR_MODE_MODEREG | DDR_MODE_BLEN_4) <<
384 SDRAM_MODE_SD_SHIFT) |
385 ((DDR_MODE_CASLAT << SDRAM_MODE_SD_SHIFT) &
386 MODE_CASLAT);
387 SYNC;
388
389 /* Set fast SDRAM refresh rate */
390 im->ddr.sdram_interval =
391 (DDR_REFINT_166MHZ_7US << SDRAM_INTERVAL_REFINT_SHIFT) |
392 (DDR_BSTOPRE << SDRAM_INTERVAL_BSTOPRE_SHIFT);
393 SYNC;
Wolfgang Denk1305bd42006-06-16 16:53:06 +0200394
395 /* Workaround for DDR6 Erratum
396 * see MPC8349E Device Errata Rev.8, 2/2006
397 * This workaround influences the MPC internal "input enables"
398 * dependent on CAS latency and MPC revision. According to errata
399 * sheet the internal reserved registers for this workaround are
400 * not available from revision 2.0 and up.
401 */
402
403 /* Get REVID from register SPRIDR. Skip workaround if rev >= 2.0
404 * (0x200)
405 */
406 if ((im->sysconf.spridr & SPRIDR_REVID) < 0x200) {
407
408 /* There is a internal reserved register at IMMRBAR+0x2F00
409 * which has to be written with a certain value defined by
410 * errata sheet.
411 */
Wolfgang Denk31560d12006-07-21 15:24:56 +0200412 u32 *reserved_p = (u32 *)((u8 *)im + 0x2f00);
413
Wolfgang Denk1305bd42006-06-16 16:53:06 +0200414#if defined(DDR_CASLAT_20)
Wolfgang Denk31560d12006-07-21 15:24:56 +0200415 *reserved_p = 0x201c0000;
Wolfgang Denk1305bd42006-06-16 16:53:06 +0200416#else
Wolfgang Denk31560d12006-07-21 15:24:56 +0200417 *reserved_p = 0x202c0000;
Wolfgang Denk1305bd42006-06-16 16:53:06 +0200418#endif
419 }
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200420}
Wolfgang Denk95593572009-05-14 23:18:34 +0200421
422#ifdef CONFIG_OF_BOARD_SETUP
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900423int ft_board_setup(void *blob, struct bd_info *bd)
Wolfgang Denk95593572009-05-14 23:18:34 +0200424{
425 ft_cpu_setup(blob, bd);
426
427#ifdef CONFIG_PCI
428 ft_pci_setup(blob, bd);
429#endif /* CONFIG_PCI */
Simon Glass2aec3cc2014-10-23 18:58:47 -0600430
431 return 0;
Wolfgang Denk95593572009-05-14 23:18:34 +0200432}
433#endif /* CONFIG_OF_BOARD_SETUP */