blob: 02b3d2dea11e2a52679bf30791e86135ce0aa10d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marian Balakowicze6f2e902005-10-11 19:09:42 +02002/*
3 * (C) Copyright 2005
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Marian Balakowicze6f2e902005-10-11 19:09:42 +02005 */
6
7#include <common.h>
Simon Glass807765b2019-12-28 10:44:54 -07008#include <fdt_support.h>
Simon Glass9b4a2052019-12-28 10:45:05 -07009#include <init.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020010#include <ioports.h>
11#include <mpc83xx.h>
12#include <asm/mpc8349_pci.h>
13#include <i2c.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020014#include <miiphy.h>
Peter Tyser61f2b382010-04-12 22:28:07 -050015#include <asm/mmu.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020016#include <pci.h>
Stefan Roeseca5def32010-08-31 10:00:10 +020017#include <flash.h>
18#include <mtd/cfi_flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020019
Wolfgang Denkd87080b2006-03-31 18:32:53 +020020DECLARE_GLOBAL_DATA_PTR;
21
Marian Balakowicze6f2e902005-10-11 19:09:42 +020022#define IOSYNC asm("eieio")
23#define ISYNC asm("isync")
24#define SYNC asm("sync")
25#define FPW FLASH_PORT_WIDTH
26#define FPWV FLASH_PORT_WIDTHV
27
28#define DDR_MAX_SIZE_PER_CS 0x20000000
29
30#if defined(DDR_CASLAT_20)
31#define TIMING_CASLAT TIMING_CFG1_CASLAT_20
32#define MODE_CASLAT DDR_MODE_CASLAT_20
33#else
34#define TIMING_CASLAT TIMING_CFG1_CASLAT_25
35#define MODE_CASLAT DDR_MODE_CASLAT_25
36#endif
37
38#define INITIAL_CS_CONFIG (CSCONFIG_EN | CSCONFIG_ROW_BIT_12 | \
39 CSCONFIG_COL_BIT_9)
40
Marian Balakowicze6f2e902005-10-11 19:09:42 +020041/* External definitions */
42ulong flash_get_size (ulong base, int banknum);
Marian Balakowicze6f2e902005-10-11 19:09:42 +020043
44/* Local functions */
45static int detect_num_flash_banks(void);
Wolfgang Denk982db892011-07-30 23:50:50 +020046static long int get_ddr_bank_size(short cs, long *base);
Bin Meng4019e542016-01-25 00:29:55 -080047static void set_cs_bounds(short cs, ulong base, ulong size);
Marian Balakowicze6f2e902005-10-11 19:09:42 +020048static void set_cs_config(short cs, long config);
49static void set_ddr_config(void);
50
51/* Local variable */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020052static volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
Marian Balakowicze6f2e902005-10-11 19:09:42 +020053
54/**************************************************************************
55 * Board initialzation after relocation to RAM. Used to detect the number
56 * of Flash banks on TQM834x.
57 */
58int board_early_init_r (void) {
59 /* sanity check, IMMARBAR should be mirrored at offset zero of IMMR */
60 if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
61 return 0;
Wolfgang Denkf013dac2005-12-04 00:40:34 +010062
Marian Balakowicze6f2e902005-10-11 19:09:42 +020063 /* detect the number of Flash banks */
64 return detect_num_flash_banks();
65}
66
67/**************************************************************************
68 * DRAM initalization and size detection
69 */
Simon Glassf1683aa2017-04-06 12:47:05 -060070int dram_init(void)
Marian Balakowicze6f2e902005-10-11 19:09:42 +020071{
72 long bank_size;
73 long size;
74 int cs;
75
76 /* during size detection, set up the max DDRLAW size */
Mario Six8a81bfd2019-01-21 09:18:15 +010077 im->sysconf.ddrlaw[0].bar = CONFIG_SYS_SDRAM_BASE;
Marian Balakowicze6f2e902005-10-11 19:09:42 +020078 im->sysconf.ddrlaw[0].ar = (LAWAR_EN | LAWAR_SIZE_2G);
79
80 /* set CS bounds to maximum size */
81 for(cs = 0; cs < 4; ++cs) {
82 set_cs_bounds(cs,
Mario Six8a81bfd2019-01-21 09:18:15 +010083 CONFIG_SYS_SDRAM_BASE + (cs * DDR_MAX_SIZE_PER_CS),
Marian Balakowicze6f2e902005-10-11 19:09:42 +020084 DDR_MAX_SIZE_PER_CS);
85
86 set_cs_config(cs, INITIAL_CS_CONFIG);
87 }
88
89 /* configure ddr controller */
90 set_ddr_config();
91
92 udelay(200);
Wolfgang Denkf013dac2005-12-04 00:40:34 +010093
Marian Balakowicze6f2e902005-10-11 19:09:42 +020094 /* enable DDR controller */
95 im->ddr.sdram_cfg = (SDRAM_CFG_MEM_EN |
96 SDRAM_CFG_SREN |
Kim Phillipsbbea46f2007-08-16 22:52:48 -050097 SDRAM_CFG_SDRAM_TYPE_DDR1);
Marian Balakowicze6f2e902005-10-11 19:09:42 +020098 SYNC;
99
100 /* size detection */
101 debug("\n");
102 size = 0;
103 for(cs = 0; cs < 4; ++cs) {
104 debug("\nDetecting Bank%d\n", cs);
105
106 bank_size = get_ddr_bank_size(cs,
Mario Six8a81bfd2019-01-21 09:18:15 +0100107 (long *)(CONFIG_SYS_SDRAM_BASE + size));
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200108 size += bank_size;
109
Marek Vasut3d546392011-10-21 14:17:10 +0000110 debug("DDR Bank%d size: %ld MiB\n\n", cs, bank_size >> 20);
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200111
112 /* exit if less than one bank */
113 if(size < DDR_MAX_SIZE_PER_CS) break;
114 }
115
Simon Glass088454c2017-03-31 08:40:25 -0600116 gd->ram_size = size;
117
118 return 0;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200119}
120
121/**************************************************************************
122 * checkboard()
123 */
124int checkboard (void)
125{
126 puts("Board: TQM834x\n");
127
128#ifdef CONFIG_PCI
Rafal Jaworowski6902df52005-10-17 02:39:53 +0200129 volatile immap_t * immr;
130 u32 w, f;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200131
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200132 immr = (immap_t *)CONFIG_SYS_IMMR;
Dave Liue0803132006-12-07 21:11:58 +0800133 if (!(immr->reset.rcwh & HRCWH_PCI_HOST)) {
Rafal Jaworowski6902df52005-10-17 02:39:53 +0200134 printf("PCI: NOT in host mode..?!\n");
135 return 0;
136 }
137
138 /* get bus width */
139 w = 32;
Dave Liue0803132006-12-07 21:11:58 +0800140 if (immr->reset.rcwh & HRCWH_64_BIT_PCI)
Rafal Jaworowski6902df52005-10-17 02:39:53 +0200141 w = 64;
142
143 /* get clock */
144 f = gd->pci_clk;
145
146 printf("PCI1: %d bit, %d MHz\n", w, f / 1000000);
147#else
148 printf("PCI: disabled\n");
149#endif
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200150 return 0;
151}
152
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200153
154/**************************************************************************
155 *
156 * Local functions
157 *
158 *************************************************************************/
159
160/**************************************************************************
161 * Detect the number of flash banks (1 or 2). Store it in
162 * a global variable tqm834x_num_flash_banks.
163 * Bank detection code based on the Monitor code.
164 */
165static int detect_num_flash_banks(void)
166{
167 typedef unsigned long FLASH_PORT_WIDTH;
168 typedef volatile unsigned long FLASH_PORT_WIDTHV;
169 FPWV *bank1_base;
170 FPWV *bank2_base;
171 FPW bank1_read;
172 FPW bank2_read;
173 ulong bank1_size;
174 ulong bank2_size;
175 ulong total_size;
176
Stefan Roeseca5def32010-08-31 10:00:10 +0200177 cfi_flash_num_flash_banks = 2; /* assume two banks */
Wolfgang Denkf013dac2005-12-04 00:40:34 +0100178
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200179 /* Get bank 1 and 2 information */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200180 bank1_size = flash_get_size(CONFIG_SYS_FLASH_BASE, 0);
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200181 debug("Bank1 size: %lu\n", bank1_size);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200182 bank2_size = flash_get_size(CONFIG_SYS_FLASH_BASE + bank1_size, 1);
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200183 debug("Bank2 size: %lu\n", bank2_size);
184 total_size = bank1_size + bank2_size;
185
186 if (bank2_size > 0) {
187 /* Seems like we've got bank 2, but maybe it's mirrored 1 */
188
189 /* Set the base addresses */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200190 bank1_base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
191 bank2_base = (FPWV *) (CONFIG_SYS_FLASH_BASE + bank1_size);
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200192
193 /* Put bank 2 into CFI command mode and read */
194 bank2_base[0x55] = 0x00980098;
195 IOSYNC;
196 ISYNC;
197 bank2_read = bank2_base[0x10];
198
199 /* Read from bank 1 (it's in read mode) */
200 bank1_read = bank1_base[0x10];
201
202 /* Reset Flash */
203 bank1_base[0] = 0x00F000F0;
204 bank2_base[0] = 0x00F000F0;
205
206 if (bank2_read == bank1_read) {
207 /*
208 * Looks like just one bank, but not sure yet. Let's
209 * read from bank 2 in autosoelect mode.
210 */
211 bank2_base[0x0555] = 0x00AA00AA;
212 bank2_base[0x02AA] = 0x00550055;
213 bank2_base[0x0555] = 0x00900090;
214 IOSYNC;
215 ISYNC;
216 bank2_read = bank2_base[0x10];
217
218 /* Read from bank 1 (it's in read mode) */
219 bank1_read = bank1_base[0x10];
220
221 /* Reset Flash */
222 bank1_base[0] = 0x00F000F0;
223 bank2_base[0] = 0x00F000F0;
224
225 if (bank2_read == bank1_read) {
226 /*
227 * In both CFI command and autoselect modes,
228 * we got the some data reading from Flash.
229 * There is only one mirrored bank.
230 */
Stefan Roeseca5def32010-08-31 10:00:10 +0200231 cfi_flash_num_flash_banks = 1;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200232 total_size = bank1_size;
233 }
234 }
235 }
236
Stefan Roeseca5def32010-08-31 10:00:10 +0200237 debug("Number of flash banks detected: %d\n", cfi_flash_num_flash_banks);
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200238
239 /* set OR0 and BR0 */
Mario Six87ee5102019-01-21 09:18:00 +0100240 set_lbc_or(0, OR_GPCM_CSNT | OR_GPCM_ACS_DIV4 | OR_GPCM_SCY_5 |
241 OR_GPCM_TRLX | (-(total_size) & OR_GPCM_AM));
Becky Brucef51cdaf2010-06-17 11:37:20 -0500242 set_lbc_br(0, (CONFIG_SYS_FLASH_BASE & BR_BA) |
243 (BR_MS_GPCM | BR_PS_32 | BR_V));
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200244
245 return (0);
246}
247
248/*************************************************************************
249 * Detect the size of a ddr bank. Sets CS bounds and CS config accordingly.
250 */
Wolfgang Denk982db892011-07-30 23:50:50 +0200251static long int get_ddr_bank_size(short cs, long *base)
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200252{
253 /* This array lists all valid DDR SDRAM configurations, with
254 * Bank sizes in bytes. (Refer to Table 9-27 in the MPC8349E RM).
255 * The last entry has to to have size equal 0 and is igonred during
256 * autodection. Bank sizes must be in increasing order of size
257 */
258 struct {
259 long row;
260 long col;
261 long size;
262 } conf[] = {
263 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_8, 32 << 20},
264 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_9, 64 << 20},
265 {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_10, 128 << 20},
266 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_9, 128 << 20},
267 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_10, 256 << 20},
268 {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_11, 512 << 20},
269 {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_10, 512 << 20},
270 {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_11, 1024 << 20},
271 {0, 0, 0}
272 };
273
274 int i;
275 int detected;
276 long size;
277
278 detected = -1;
279 for(i = 0; conf[i].size != 0; ++i) {
280
281 /* set sdram bank configuration */
282 set_cs_config(cs, CSCONFIG_EN | conf[i].col | conf[i].row);
283
284 debug("Getting RAM size...\n");
285 size = get_ram_size(base, DDR_MAX_SIZE_PER_CS);
286
287 if((size == conf[i].size) && (i == detected + 1))
288 detected = i;
289
290 debug("Trying %ld x %ld (%ld MiB) at addr %p, detected: %ld MiB\n",
291 conf[i].row,
292 conf[i].col,
293 conf[i].size >> 20,
294 base,
295 size >> 20);
296 }
297
298 if(detected == -1){
299 /* disable empty cs */
300 debug("\nNo valid configurations for CS%d, disabling...\n", cs);
301 set_cs_config(cs, 0);
302 return 0;
303 }
Wolfgang Denkf013dac2005-12-04 00:40:34 +0100304
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200305 debug("\nDetected configuration %ld x %ld (%ld MiB) at addr %p\n",
306 conf[detected].row, conf[detected].col, conf[detected].size >> 20, base);
Wolfgang Denkf013dac2005-12-04 00:40:34 +0100307
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200308 /* configure cs ro detected params */
309 set_cs_config(cs, CSCONFIG_EN | conf[detected].row |
310 conf[detected].col);
311
312 set_cs_bounds(cs, (long)base, conf[detected].size);
313
314 return(conf[detected].size);
315}
316
317/**************************************************************************
318 * Sets DDR bank CS bounds.
319 */
Bin Meng4019e542016-01-25 00:29:55 -0800320static void set_cs_bounds(short cs, ulong base, ulong size)
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200321{
Marek Vasut3d546392011-10-21 14:17:10 +0000322 debug("Setting bounds %08lx, %08lx for cs %d\n", base, size, cs);
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200323 if(size == 0){
324 im->ddr.csbnds[cs].csbnds = 0x00000000;
325 } else {
326 im->ddr.csbnds[cs].csbnds =
327 ((base >> CSBNDS_SA_SHIFT) & CSBNDS_SA) |
328 (((base + size - 1) >> CSBNDS_EA_SHIFT) &
329 CSBNDS_EA);
330 }
331 SYNC;
332}
333
334/**************************************************************************
335 * Sets DDR banks CS configuration.
336 * config == 0x00000000 disables the CS.
337 */
338static void set_cs_config(short cs, long config)
339{
Marek Vasut3d546392011-10-21 14:17:10 +0000340 debug("Setting config %08lx for cs %d\n", config, cs);
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200341 im->ddr.cs_config[cs] = config;
342 SYNC;
343}
344
345/**************************************************************************
346 * Sets DDR clocks, timings and configuration.
347 */
348static void set_ddr_config(void) {
349 /* clock control */
350 im->ddr.sdram_clk_cntl = DDR_SDRAM_CLK_CNTL_SS_EN |
351 DDR_SDRAM_CLK_CNTL_CLK_ADJUST_05;
352 SYNC;
Wolfgang Denkf013dac2005-12-04 00:40:34 +0100353
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200354 /* timing configuration */
355 im->ddr.timing_cfg_1 =
356 (4 << TIMING_CFG1_PRETOACT_SHIFT) |
357 (7 << TIMING_CFG1_ACTTOPRE_SHIFT) |
358 (4 << TIMING_CFG1_ACTTORW_SHIFT) |
359 (5 << TIMING_CFG1_REFREC_SHIFT) |
360 (3 << TIMING_CFG1_WRREC_SHIFT) |
361 (3 << TIMING_CFG1_ACTTOACT_SHIFT) |
362 (1 << TIMING_CFG1_WRTORD_SHIFT) |
363 (TIMING_CFG1_CASLAT & TIMING_CASLAT);
364
365 im->ddr.timing_cfg_2 =
366 TIMING_CFG2_CPO_DEF |
367 (2 << TIMING_CFG2_WR_DATA_DELAY_SHIFT);
368 SYNC;
369
370 /* don't enable DDR controller yet */
371 im->ddr.sdram_cfg =
372 SDRAM_CFG_SREN |
Kim Phillipsbbea46f2007-08-16 22:52:48 -0500373 SDRAM_CFG_SDRAM_TYPE_DDR1;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200374 SYNC;
Wolfgang Denkf013dac2005-12-04 00:40:34 +0100375
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200376 /* Set SDRAM mode */
377 im->ddr.sdram_mode =
378 ((DDR_MODE_EXT_MODEREG | DDR_MODE_WEAK) <<
379 SDRAM_MODE_ESD_SHIFT) |
380 ((DDR_MODE_MODEREG | DDR_MODE_BLEN_4) <<
381 SDRAM_MODE_SD_SHIFT) |
382 ((DDR_MODE_CASLAT << SDRAM_MODE_SD_SHIFT) &
383 MODE_CASLAT);
384 SYNC;
385
386 /* Set fast SDRAM refresh rate */
387 im->ddr.sdram_interval =
388 (DDR_REFINT_166MHZ_7US << SDRAM_INTERVAL_REFINT_SHIFT) |
389 (DDR_BSTOPRE << SDRAM_INTERVAL_BSTOPRE_SHIFT);
390 SYNC;
Wolfgang Denk10af6d52006-06-16 16:53:06 +0200391
392 /* Workaround for DDR6 Erratum
393 * see MPC8349E Device Errata Rev.8, 2/2006
394 * This workaround influences the MPC internal "input enables"
395 * dependent on CAS latency and MPC revision. According to errata
396 * sheet the internal reserved registers for this workaround are
397 * not available from revision 2.0 and up.
398 */
399
400 /* Get REVID from register SPRIDR. Skip workaround if rev >= 2.0
401 * (0x200)
402 */
403 if ((im->sysconf.spridr & SPRIDR_REVID) < 0x200) {
404
405 /* There is a internal reserved register at IMMRBAR+0x2F00
406 * which has to be written with a certain value defined by
407 * errata sheet.
408 */
Wolfgang Denk966083e2006-07-21 15:24:56 +0200409 u32 *reserved_p = (u32 *)((u8 *)im + 0x2f00);
410
Wolfgang Denk10af6d52006-06-16 16:53:06 +0200411#if defined(DDR_CASLAT_20)
Wolfgang Denk966083e2006-07-21 15:24:56 +0200412 *reserved_p = 0x201c0000;
Wolfgang Denk10af6d52006-06-16 16:53:06 +0200413#else
Wolfgang Denk966083e2006-07-21 15:24:56 +0200414 *reserved_p = 0x202c0000;
Wolfgang Denk10af6d52006-06-16 16:53:06 +0200415#endif
416 }
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200417}
Wolfgang Denk4681e672009-05-14 23:18:34 +0200418
419#ifdef CONFIG_OF_BOARD_SETUP
Simon Glasse895a4b2014-10-23 18:58:47 -0600420int ft_board_setup(void *blob, bd_t *bd)
Wolfgang Denk4681e672009-05-14 23:18:34 +0200421{
422 ft_cpu_setup(blob, bd);
423
424#ifdef CONFIG_PCI
425 ft_pci_setup(blob, bd);
426#endif /* CONFIG_PCI */
Simon Glasse895a4b2014-10-23 18:58:47 -0600427
428 return 0;
Wolfgang Denk4681e672009-05-14 23:18:34 +0200429}
430#endif /* CONFIG_OF_BOARD_SETUP */