blob: c62ba62172c093f99f134d42c3581d3fd671f764 [file] [log] [blame]
Stefan Roesee1b8d0b2012-08-14 15:04:19 +02001/*
2 * (C) Copyright 2003-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2004
6 * Mark Jonas, Freescale Semiconductor, mark.jonas@motorola.com.
7 *
8 * (C) Copyright 2006
9 * MicroSys GmbH
10 *
Stefan Roese9071a442013-04-25 23:20:23 +000011 * Copyright 2012-2013 Stefan Roese <sr@denx.de>
Stefan Roesee1b8d0b2012-08-14 15:04:19 +020012 *
13 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 */
21
22#include <common.h>
23#include <command.h>
24#include <mpc5xxx.h>
25#include <pci.h>
26#include <miiphy.h>
Stefan Roese512da3b2013-02-07 02:10:11 +000027#include <linux/compiler.h>
Stefan Roesee1b8d0b2012-08-14 15:04:19 +020028#include <asm/processor.h>
29#include <asm/io.h>
30
Stefan Roese512da3b2013-02-07 02:10:11 +000031#ifdef CONFIG_A4M2K
32#include "is46r16320d.h"
33#else
Stefan Roesee1b8d0b2012-08-14 15:04:19 +020034#include "mt46v16m16-75.h"
Stefan Roese512da3b2013-02-07 02:10:11 +000035#endif
Stefan Roesee1b8d0b2012-08-14 15:04:19 +020036
37DECLARE_GLOBAL_DATA_PTR;
38
39#if !defined(CONFIG_SYS_RAMBOOT) && \
40 (defined(CONFIG_SPL) && defined(CONFIG_SPL_BUILD))
41static void sdram_start(int hi_addr)
42{
43 long hi_addr_bit = hi_addr ? 0x01000000 : 0;
44 long control = SDRAM_CONTROL | hi_addr_bit;
45
46 /* unlock mode register */
47 out_be32((void *)MPC5XXX_SDRAM_CTRL, control | 0x80000000);
48
49 /* precharge all banks */
50 out_be32((void *)MPC5XXX_SDRAM_CTRL, control | 0x80000002);
51
52#ifdef SDRAM_DDR
53 /* set mode register: extended mode */
54 out_be32((void *)MPC5XXX_SDRAM_MODE, SDRAM_EMODE);
55
56 /* set mode register: reset DLL */
57 out_be32((void *)MPC5XXX_SDRAM_MODE, SDRAM_MODE | 0x04000000);
58#endif
59
60 /* precharge all banks */
61 out_be32((void *)MPC5XXX_SDRAM_CTRL, control | 0x80000002);
62
63 /* auto refresh */
64 out_be32((void *)MPC5XXX_SDRAM_CTRL, control | 0x80000004);
65
66 /* set mode register */
67 out_be32((void *)MPC5XXX_SDRAM_MODE, SDRAM_MODE);
68
69 /* normal operation */
70 out_be32((void *)MPC5XXX_SDRAM_CTRL, control);
Stefan Roese512da3b2013-02-07 02:10:11 +000071
72 /*
73 * Wait a short while for the DLL to lock before accessing
74 * the SDRAM
75 */
76 udelay(100);
Stefan Roesee1b8d0b2012-08-14 15:04:19 +020077}
78#endif
79
80/*
81 * ATTENTION: Although partially referenced initdram does NOT make real use
82 * use of CONFIG_SYS_SDRAM_BASE. The code does not work if
83 * CONFIG_SYS_SDRAM_BASE is something else than 0x00000000.
84 */
85phys_size_t initdram(int board_type)
86{
87 ulong dramsize = 0;
88 ulong dramsize2 = 0;
89 uint svr, pvr;
90#if !defined(CONFIG_SYS_RAMBOOT) && \
91 (defined(CONFIG_SPL) && defined(CONFIG_SPL_BUILD))
92 ulong test1, test2;
93
94 /* setup SDRAM chip selects */
95 out_be32((void *)MPC5XXX_SDRAM_CS0CFG, 0x0000001e); /* 2GB at 0x0 */
96 out_be32((void *)MPC5XXX_SDRAM_CS1CFG, 0x80000000); /* disabled */
97
98 /* setup config registers */
99 out_be32((void *)MPC5XXX_SDRAM_CONFIG1, SDRAM_CONFIG1);
100 out_be32((void *)MPC5XXX_SDRAM_CONFIG2, SDRAM_CONFIG2);
101
102#ifdef SDRAM_DDR
103 /* set tap delay */
104 out_be32((void *)MPC5XXX_CDM_PORCFG, SDRAM_TAPDELAY);
105#endif
106
107 /* find RAM size using SDRAM CS0 only */
108 sdram_start(0);
109 test1 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
110 sdram_start(1);
111 test2 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
112 if (test1 > test2) {
113 sdram_start(0);
114 dramsize = test1;
115 } else {
116 dramsize = test2;
117 }
118
119 /* memory smaller than 1MB is impossible */
120 if (dramsize < (1 << 20))
121 dramsize = 0;
122
123 /* set SDRAM CS0 size according to the amount of RAM found */
124 if (dramsize > 0) {
125 out_be32((void *)MPC5XXX_SDRAM_CS0CFG,
126 0x13 + __builtin_ffs(dramsize >> 20) - 1);
127 } else {
128 out_be32((void *)MPC5XXX_SDRAM_CS0CFG, 0); /* disabled */
129 }
130#else /* CONFIG_SYS_RAMBOOT */
131
132 /* retrieve size of memory connected to SDRAM CS0 */
133 dramsize = in_be32((void *)MPC5XXX_SDRAM_CS0CFG) & 0xFF;
134 if (dramsize >= 0x13)
135 dramsize = (1 << (dramsize - 0x13)) << 20;
136 else
137 dramsize = 0;
138
139 /* retrieve size of memory connected to SDRAM CS1 */
140 dramsize2 = in_be32((void *)MPC5XXX_SDRAM_CS1CFG) & 0xFF;
141 if (dramsize2 >= 0x13)
142 dramsize2 = (1 << (dramsize2 - 0x13)) << 20;
143 else
144 dramsize2 = 0;
145
146#endif /* CONFIG_SYS_RAMBOOT */
147
148 /*
149 * On MPC5200B we need to set the special configuration delay in the
150 * DDR controller. Please refer to Freescale's AN3221 "MPC5200B SDRAM
151 * Initialization and Configuration", 3.3.1 SDelay--MBAR + 0x0190:
152 *
153 * "The SDelay should be written to a value of 0x00000004. It is
154 * required to account for changes caused by normal wafer processing
155 * parameters."
156 */
157 svr = get_svr();
158 pvr = get_pvr();
159 if ((SVR_MJREV(svr) >= 2) && (PVR_MAJ(pvr) == 1) && (PVR_MIN(pvr) == 4))
160 out_be32((void *)MPC5XXX_SDRAM_SDELAY, 0x04);
161
162 return dramsize + dramsize2;
163}
164
165static void get_revisions(int *failsavelevel, int *digiboardversion,
166 int *fpgaversion)
167{
168 struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;
169 u8 val;
170
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200171 /* read digitalboard-version from TMR[2..4] */
172 val = 0;
173 val |= (gpt->gpt2.sr & (1 << (31 - 23))) ? (1) : 0;
174 val |= (gpt->gpt3.sr & (1 << (31 - 23))) ? (1 << 1) : 0;
175 val |= (gpt->gpt4.sr & (1 << (31 - 23))) ? (1 << 2) : 0;
176 *digiboardversion = val;
177
Stefan Roese512da3b2013-02-07 02:10:11 +0000178 /*
179 * A4M2K only supports digiboardversion. No failsavelevel and
180 * fpgaversion here.
181 */
182#if !defined(CONFIG_A4M2K)
183 /*
184 * Figure out failsavelevel
185 * see ticket dsvk#59
186 */
187 *failsavelevel = 0; /* 0=failsave, 1=board ok, 2=fpga ok */
188
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200189 if (*digiboardversion == 0) {
190 *failsavelevel = 1; /* digiboard-version ok */
191
192 /* read fpga-version from TMR[5..7] */
193 val = 0;
194 val |= (gpt->gpt5.sr & (1 << (31 - 23))) ? (1) : 0;
195 val |= (gpt->gpt6.sr & (1 << (31 - 23))) ? (1 << 1) : 0;
196 val |= (gpt->gpt7.sr & (1 << (31 - 23))) ? (1 << 2) : 0;
197 *fpgaversion = val;
198
199 if (*fpgaversion == 1)
200 *failsavelevel = 2; /* fpga-version ok */
201 }
Stefan Roese512da3b2013-02-07 02:10:11 +0000202#endif
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200203}
204
205/*
206 * This function is called from the SPL U-Boot version for
207 * early init stuff, that needs to be done for OS (e.g. Linux)
208 * booting. Doing it later in the real U-Boot would not work
209 * in case that the SPL U-Boot boots Linux directly.
210 */
211void spl_board_init(void)
212{
213 struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
214 struct mpc5xxx_mmap_ctl *mm =
215 (struct mpc5xxx_mmap_ctl *)CONFIG_SYS_MBAR;
Stefan Roese512da3b2013-02-07 02:10:11 +0000216
217#if defined(CONFIG_A4M2K)
218 /* enable CS3 and CS5 (FPGA) */
219 setbits_be32(&mm->ipbi_ws_ctrl, (1 << 19) | (1 << 21));
220#else
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200221 int digiboardversion;
222 int failsavelevel;
223 int fpgaversion;
224 u32 val;
225
226 get_revisions(&failsavelevel, &digiboardversion, &fpgaversion);
227
228 val = in_be32(&mm->ipbi_ws_ctrl);
229
230 /* first clear bits 19..21 (CS3...5) */
231 val &= ~((1 << 19) | (1 << 20) | (1 << 21));
232 if (failsavelevel == 2) {
233 /* FPGA ok */
234 val |= (1 << 19) | (1 << 21);
235 }
236
237 if (failsavelevel >= 1) {
238 /* at least digiboard-version ok */
239 val |= (1 << 20);
240 }
241
242 /* And write new value back to register */
243 out_be32(&mm->ipbi_ws_ctrl, val);
244
Stefan Roese9071a442013-04-25 23:20:23 +0000245
246 /* Setup pin multiplexing */
247 if (failsavelevel == 2) {
248 /* fpga-version ok */
249#if defined(CONFIG_SYS_GPS_PORT_CONFIG_2)
250 out_be32(&gpio->port_config, CONFIG_SYS_GPS_PORT_CONFIG_2);
251#endif
252 } else if (failsavelevel == 1) {
253 /* digiboard-version ok - fpga not */
254#if defined(CONFIG_SYS_GPS_PORT_CONFIG_1)
255 out_be32(&gpio->port_config, CONFIG_SYS_GPS_PORT_CONFIG_1);
256#endif
257 } else {
258 /* full failsave-mode */
259#if defined(CONFIG_SYS_GPS_PORT_CONFIG)
260 out_be32(&gpio->port_config, CONFIG_SYS_GPS_PORT_CONFIG);
261#endif
262 }
263#endif
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200264
265 /*
266 * Setup gpio_wkup_7 as watchdog AS INPUT to disable it - see
267 * ticket #60
268 *
269 * MPC5XXX_WU_GPIO_DIR direction is already 0 (INPUT)
270 * set bit 0(msb) to 1
271 */
Stefan Roese223008d2013-02-07 02:10:28 +0000272 setbits_be32((void *)MPC5XXX_WU_GPIO_ENABLE, CONFIG_WDOG_GPIO_PIN);
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200273
Stefan Roese512da3b2013-02-07 02:10:11 +0000274#if defined(CONFIG_A4M2K)
275 /* Setup USB[x] as MPCDiag[0..3] GPIO outputs */
276
277 /* set USB0,6,7,8 (MPCDiag[0..3]) direction to output */
278 gpio->simple_ddr |= 1 << (31 - 15);
279 gpio->simple_ddr |= 1 << (31 - 14);
280 gpio->simple_ddr |= 1 << (31 - 13);
281 gpio->simple_ddr |= 1 << (31 - 12);
282
283 /* enable USB0,6,7,8 (MPCDiag[0..3]) as GPIO */
284 gpio->simple_gpioe |= 1 << (31 - 15);
285 gpio->simple_gpioe |= 1 << (31 - 14);
286 gpio->simple_gpioe |= 1 << (31 - 13);
287 gpio->simple_gpioe |= 1 << (31 - 12);
288
289 /* Setup PSC2[0..2] as STSLED[0..2] GPIO outputs */
290
291 /* set PSC2[0..2] (STSLED[0..2]) direction to output */
292 gpio->simple_ddr |= 1 << (31 - 27);
293 gpio->simple_ddr |= 1 << (31 - 26);
294 gpio->simple_ddr |= 1 << (31 - 25);
295
296 /* enable PSC2[0..2] (STSLED[0..2]) as GPIO */
297 gpio->simple_gpioe |= 1 << (31 - 27);
298 gpio->simple_gpioe |= 1 << (31 - 26);
299 gpio->simple_gpioe |= 1 << (31 - 25);
300
301 /* Setup PSC6[2] as MRST2 self reset GPIO output */
302
303 /* set PSC6[2]/IRDA_TX (MRST2) direction to output */
304 gpio->simple_ddr |= 1 << (31 - 3);
305
306 /* set PSC6[2]/IRDA_TX (MRST2) output as open drain */
307 gpio->simple_ode |= 1 << (31 - 3);
308
309 /* set PSC6[2]/IRDA_TX (MRST2) output as default high */
310 gpio->simple_dvo |= 1 << (31 - 3);
311
312 /* enable PSC6[2]/IRDA_TX (MRST2) as GPIO */
313 gpio->simple_gpioe |= 1 << (31 - 3);
314
315 /* Setup PSC6[3] as HARNSSCD harness code GPIO input */
316
317 /* set PSC6[3]/IR_USB_CLK (HARNSSCD) direction to input */
318 gpio->simple_ddr |= 0 << (31 - 2);
319
320 /* enable PSC6[3]/IR_USB_CLK (HARNSSCD) as GPIO */
321 gpio->simple_gpioe |= 1 << (31 - 2);
322#else
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200323 /* setup GPIOs for status-leds if needed - see ticket #57 */
324 if (failsavelevel > 0) {
325 /* digiboard-version is OK */
326 /* LED is LOW ACTIVE - so deactivate by set output to 1 */
327 gpio->simple_dvo |= 1 << (31 - 12);
328 gpio->simple_dvo |= 1 << (31 - 13);
329 /* set GPIO direction to output */
330 gpio->simple_ddr |= 1 << (31 - 12);
331 gpio->simple_ddr |= 1 << (31 - 13);
332 /* open drain config is set to "normal output" at reset */
333 /* gpio->simple_ode &=~ ( 1 << (31-12) ); */
334 /* gpio->simple_ode &=~ ( 1 << (31-13) ); */
335 /* enable as GPIO */
336 gpio->simple_gpioe |= 1 << (31 - 12);
337 gpio->simple_gpioe |= 1 << (31 - 13);
338 }
339
340 /* setup fpga irq - see ticket #65 */
341 if (failsavelevel > 1) {
342 /*
343 * The main irq initialisation is done in interrupts.c
344 * mpc5xxx_init_irq
345 */
346 struct mpc5xxx_intr *intr =
347 (struct mpc5xxx_intr *)(MPC5XXX_ICTL);
348
349 setbits_be32(&intr->ctrl, 0x08C01801);
350
351 /*
352 * The MBAR+0x0524 Bit 21:23 CSe are ignored here due to the
353 * already cleared (intr_ctrl) MBAR+0x0510 ECLR[0] bit above
354 */
355 }
Stefan Roese512da3b2013-02-07 02:10:11 +0000356#endif
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200357}
358
359int checkboard(void)
360{
361 int digiboardversion;
362 int failsavelevel;
363 int fpgaversion;
364
365 get_revisions(&failsavelevel, &digiboardversion, &fpgaversion);
366
Stefan Roese512da3b2013-02-07 02:10:11 +0000367#ifdef CONFIG_A4M2K
368 puts("Board: A4M2K\n");
369 printf(" digiboard IO version %u\n", digiboardversion);
370#else
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200371 puts("Board: A3M071\n");
372 printf("Rev: failsave level %u\n", failsavelevel);
373 printf(" digiboard IO version %u\n", digiboardversion);
374 if (failsavelevel > 0) /* only if fpga-version red */
375 printf(" fpga IO version %u\n", fpgaversion);
Stefan Roese512da3b2013-02-07 02:10:11 +0000376#endif
Stefan Roesee1b8d0b2012-08-14 15:04:19 +0200377
378 return 0;
379}
380
381/* miscellaneous platform dependent initialisations */
382int misc_init_r(void)
383{
384 /* adjust flash start and offset to detected values */
385 gd->bd->bi_flashstart = flash_info[0].start[0];
386 gd->bd->bi_flashoffset = 0;
387
388 /* adjust mapping */
389 out_be32((void *)MPC5XXX_BOOTCS_START,
390 START_REG(gd->bd->bi_flashstart));
391 out_be32((void *)MPC5XXX_CS0_START, START_REG(gd->bd->bi_flashstart));
392 out_be32((void *)MPC5XXX_BOOTCS_STOP,
393 STOP_REG(gd->bd->bi_flashstart, gd->bd->bi_flashsize));
394 out_be32((void *)MPC5XXX_CS0_STOP,
395 STOP_REG(gd->bd->bi_flashstart, gd->bd->bi_flashsize));
396
397 return 0;
398}
399
400#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
401void ft_board_setup(void *blob, bd_t * bd)
402{
403 ft_cpu_setup(blob, bd);
404}
405#endif /* defined(CONFIG_OF_FLAT_TREE) && defined(CONFIG_OF_BOARD_SETUP) */
406
407#ifdef CONFIG_SPL_OS_BOOT
408/*
409 * A3M071 specific implementation of spl_start_uboot()
410 *
411 * RETURN
412 * 0 if booting into OS is selected (default)
413 * 1 if booting into U-Boot is selected
414 */
415int spl_start_uboot(void)
416{
417 char s[8];
418
419 env_init();
420 getenv_f("boot_os", s, sizeof(s));
421 if ((s != NULL) && (strcmp(s, "yes") == 0))
422 return 0;
423
424 return 1;
425}
426#endif
Stefan Roese223008d2013-02-07 02:10:28 +0000427
428#if defined(CONFIG_HW_WATCHDOG)
429static int watchdog_toggle;
430
431void hw_watchdog_reset(void)
432{
433 int val;
434
435 /*
436 * Check if watchdog is enabled via user command
437 */
438 if ((gd->flags & GD_FLG_RELOC) && watchdog_toggle) {
439 /* Set direction to output */
440 setbits_be32((void *)MPC5XXX_WU_GPIO_DIR, CONFIG_WDOG_GPIO_PIN);
441
442 /*
443 * Toggle watchdog output
444 */
445 val = (in_be32((void *)MPC5XXX_WU_GPIO_DATA_O) &
446 CONFIG_WDOG_GPIO_PIN);
447 if (val) {
448 clrbits_be32((void *)MPC5XXX_WU_GPIO_DATA_O,
449 CONFIG_WDOG_GPIO_PIN);
450 } else {
451 setbits_be32((void *)MPC5XXX_WU_GPIO_DATA_O,
452 CONFIG_WDOG_GPIO_PIN);
453 }
454 }
455}
456
457int do_wdog_toggle(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
458{
459 if (argc != 2)
460 goto usage;
461
462 if (strncmp(argv[1], "on", 2) == 0)
463 watchdog_toggle = 1;
464 else if (strncmp(argv[1], "off", 3) == 0)
465 watchdog_toggle = 0;
466 else
467 goto usage;
468
469 return 0;
470usage:
471 printf("Usage: wdogtoggle %s\n", cmdtp->usage);
472 return 1;
473}
474
475U_BOOT_CMD(
476 wdogtoggle, CONFIG_SYS_MAXARGS, 2, do_wdog_toggle,
477 "toggle GPIO pin to service watchdog",
478 "[on/off] - Switch watchdog toggling via GPIO pin on/off"
479);
480#endif