x86: Create weak init_cache() and default enable_caches() functions
[oweals/u-boot.git] / arch / x86 / lib / board.c
1 /*
2  * (C) Copyright 2008-2011
3  * Graeme Russ, <graeme.russ@gmail.com>
4  *
5  * (C) Copyright 2002
6  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
7  *
8  * (C) Copyright 2002
9  * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
10  *
11  * (C) Copyright 2002
12  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13  * Marius Groeger <mgroeger@sysgo.de>
14  *
15  * See file CREDITS for list of people who contributed to this
16  * project.
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License as
20  * published by the Free Software Foundation; either version 2 of
21  * the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31  * MA 02111-1307 USA
32  */
33
34 #include <common.h>
35 #include <watchdog.h>
36 #include <command.h>
37 #include <stdio_dev.h>
38 #include <version.h>
39 #include <malloc.h>
40 #include <net.h>
41 #include <ide.h>
42 #include <serial.h>
43 #include <asm/u-boot-x86.h>
44 #include <elf.h>
45 #include <asm/processor.h>
46
47 #ifdef CONFIG_BITBANGMII
48 #include <miiphy.h>
49 #endif
50
51 /************************************************************************
52  * Init Utilities                                                       *
53  ************************************************************************
54  * Some of this code should be moved into the core functions,
55  * or dropped completely,
56  * but let's get it working (again) first...
57  */
58 static int init_baudrate(void)
59 {
60         gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
61         return 0;
62 }
63
64 static int display_banner(void)
65 {
66
67         printf("\n\n%s\n\n", version_string);
68
69         return 0;
70 }
71
72 static int display_dram_config(void)
73 {
74         int i;
75
76         puts("DRAM Configuration:\n");
77
78         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
79                 printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
80                 print_size(gd->bd->bi_dram[i].size, "\n");
81         }
82
83         return 0;
84 }
85
86 #ifndef CONFIG_SYS_NO_FLASH
87 static void display_flash_config(ulong size)
88 {
89         puts("Flash: ");
90         print_size(size, "\n");
91 }
92 #endif
93
94 /*
95  * Breath some life into the board...
96  *
97  * Initialize an SMC for serial comms, and carry out some hardware
98  * tests.
99  *
100  * The first part of initialization is running from Flash memory;
101  * its main purpose is to initialize the RAM so that we
102  * can relocate the monitor code to RAM.
103  */
104
105 /*
106  * All attempts to come up with a "common" initialization sequence
107  * that works for all boards and architectures failed: some of the
108  * requirements are just _too_ different. To get rid of the resulting
109  * mess of board dependend #ifdef'ed code we now make the whole
110  * initialization sequence configurable to the user.
111  *
112  * The requirements for any new initalization function is simple: it
113  * receives a pointer to the "global data" structure as it's only
114  * argument, and returns an integer return code, where 0 means
115  * "continue" and != 0 means "fatal error, hang the system".
116  */
117 typedef int (init_fnc_t) (void);
118
119 static int calculate_relocation_address(void);
120 static int copy_uboot_to_ram(void);
121 static int clear_bss(void);
122 static int do_elf_reloc_fixups(void);
123 static int copy_gd_to_ram(void);
124
125 init_fnc_t *init_sequence_f[] = {
126         cpu_init_f,
127         board_early_init_f,
128         env_init,
129         init_baudrate,
130         serial_init,
131         console_init_f,
132         dram_init_f,
133         calculate_relocation_address,
134         copy_uboot_to_ram,
135         clear_bss,
136         do_elf_reloc_fixups,
137
138         NULL,
139 };
140
141 init_fnc_t *init_sequence_r[] = {
142         copy_gd_to_ram,
143         init_cache,
144         cpu_init_r,             /* basic cpu dependent setup */
145         board_early_init_r,     /* basic board dependent setup */
146         dram_init,              /* configure available RAM banks */
147         interrupt_init,         /* set up exceptions */
148         timer_init,
149         display_banner,
150         display_dram_config,
151
152         NULL,
153 };
154
155 static int calculate_relocation_address(void)
156 {
157         ulong text_start = (ulong)&__text_start;
158         ulong bss_end = (ulong)&__bss_end;
159         ulong dest_addr;
160
161         /*
162          * NOTE: All destination address are rounded down to 16-byte
163          *       boundary to satisfy various worst-case alignment
164          *       requirements
165          */
166
167         /* Global Data is at top of available memory */
168         dest_addr = gd->ram_size;
169         dest_addr -= GENERATED_GBL_DATA_SIZE;
170         dest_addr &= ~15;
171         gd->new_gd_addr = dest_addr;
172
173         /* GDT is below Global Data */
174         dest_addr -= X86_GDT_SIZE;
175         dest_addr &= ~15;
176         gd->gdt_addr = dest_addr;
177
178         /* Stack is below GDT */
179         gd->start_addr_sp = dest_addr;
180
181         /* U-Boot is below the stack */
182         dest_addr -= CONFIG_SYS_STACK_SIZE;
183         dest_addr -= (bss_end - text_start);
184         dest_addr &= ~15;
185         gd->relocaddr = dest_addr;
186         gd->reloc_off = (dest_addr - text_start);
187
188         return 0;
189 }
190
191 static int copy_uboot_to_ram(void)
192 {
193         size_t len = (size_t)&__data_end - (size_t)&__text_start;
194
195         memcpy((void *)gd->relocaddr, (void *)&__text_start, len);
196
197         return 0;
198 }
199
200 static int clear_bss(void)
201 {
202         ulong dst_addr = (ulong)&__bss_start + gd->reloc_off;
203         size_t len = (size_t)&__bss_end - (size_t)&__bss_start;
204
205         memset((void *)dst_addr, 0x00, len);
206
207         return 0;
208 }
209
210 static int do_elf_reloc_fixups(void)
211 {
212         Elf32_Rel *re_src = (Elf32_Rel *)(&__rel_dyn_start);
213         Elf32_Rel *re_end = (Elf32_Rel *)(&__rel_dyn_end);
214
215         Elf32_Addr *offset_ptr_rom;
216         Elf32_Addr *offset_ptr_ram;
217
218         /* The size of the region of u-boot that runs out of RAM. */
219         uintptr_t size = (uintptr_t)&__bss_end - (uintptr_t)&__text_start;
220
221         do {
222                 /* Get the location from the relocation entry */
223                 offset_ptr_rom = (Elf32_Addr *)re_src->r_offset;
224
225                 /* Check that the location of the relocation is in .text */
226                 if (offset_ptr_rom >= (Elf32_Addr *)CONFIG_SYS_TEXT_BASE) {
227
228                         /* Switch to the in-RAM version */
229                         offset_ptr_ram = (Elf32_Addr *)((ulong)offset_ptr_rom +
230                                                         gd->reloc_off);
231
232                         /* Check that the target points into .text */
233                         if (*offset_ptr_ram >= CONFIG_SYS_TEXT_BASE &&
234                                         *offset_ptr_ram <
235                                         (CONFIG_SYS_TEXT_BASE + size)) {
236                                 *offset_ptr_ram += gd->reloc_off;
237                         }
238                 }
239         } while (re_src++ < re_end);
240
241         return 0;
242 }
243
244 /* Load U-Boot into RAM, initialize BSS, perform relocation adjustments */
245 void board_init_f(ulong boot_flags)
246 {
247         init_fnc_t **init_fnc_ptr;
248
249         gd->flags = boot_flags;
250
251         for (init_fnc_ptr = init_sequence_f; *init_fnc_ptr; ++init_fnc_ptr) {
252                 if ((*init_fnc_ptr)() != 0)
253                         hang();
254         }
255
256         /*
257          * SDRAM is now initialised, U-Boot has been copied into SDRAM,
258          * the BSS has been cleared etc. The final stack can now be setup
259          * in SDRAM. Code execution will continue (momentarily) in Flash,
260          * but with the stack in SDRAM and Global Data in temporary memory
261          * (CPU cache)
262          */
263         board_init_f_r_trampoline(gd->start_addr_sp);
264
265         /* NOTREACHED - board_init_f_r_trampoline() does not return */
266         while (1)
267                 ;
268 }
269
270 void board_init_f_r(void)
271 {
272         /*
273          * Transfer execution from Flash to RAM by calculating the address
274          * of the in-RAM copy of board_init_r() and calling it
275          */
276         (board_init_r + gd->reloc_off)(gd, gd->relocaddr);
277
278         /* NOTREACHED - board_init_r() does not return */
279         while (1)
280                 ;
281 }
282
283 static int copy_gd_to_ram(void)
284 {
285         gd_t *ram_gd;
286
287         /*
288          * Global data is still in temporary memory (the CPU cache).
289          * calculate_relocation_address() has set gd->new_gd_addr to
290          * where the global data lives in RAM but getting it there
291          * safely is a bit tricky due to the 'F-Segment Hack' that
292          * we need to use for x86
293          */
294         ram_gd = (gd_t *)gd->new_gd_addr;
295         memcpy((void *)ram_gd, gd, sizeof(gd_t));
296
297         /*
298          * Reload the Global Descriptor Table so FS points to the
299          * in-RAM copy of Global Data (calculate_relocation_address()
300          * has already calculated the in-RAM location of the GDT)
301          */
302         ram_gd->gd_addr = (ulong)ram_gd;
303         init_gd(ram_gd, (u64 *)gd->gdt_addr);
304
305         return 0;
306 }
307
308 void board_init_r(gd_t *id, ulong dest_addr)
309 {
310 #if defined(CONFIG_CMD_NET)
311         char *s;
312 #endif
313 #ifndef CONFIG_SYS_NO_FLASH
314         ulong size;
315 #endif
316         static bd_t bd_data;
317         init_fnc_t **init_fnc_ptr;
318
319         show_boot_progress(0x21);
320
321         /* compiler optimization barrier needed for GCC >= 3.4 */
322         __asm__ __volatile__("" : : : "memory");
323
324         gd->flags |= GD_FLG_RELOC;
325
326         gd->bd = &bd_data;
327         memset(gd->bd, 0, sizeof(bd_t));
328         show_boot_progress(0x22);
329
330         gd->baudrate =  CONFIG_BAUDRATE;
331
332         mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
333                         CONFIG_SYS_MALLOC_LEN);
334
335         for (init_fnc_ptr = init_sequence_r; *init_fnc_ptr; ++init_fnc_ptr) {
336                 if ((*init_fnc_ptr)() != 0)
337                         hang();
338         }
339         show_boot_progress(0x23);
340
341 #ifdef CONFIG_SERIAL_MULTI
342         serial_initialize();
343 #endif
344
345 #ifndef CONFIG_SYS_NO_FLASH
346         /* configure available FLASH banks */
347         size = flash_init();
348         display_flash_config(size);
349         show_boot_progress(0x24);
350 #endif
351
352         show_boot_progress(0x25);
353
354         /* initialize environment */
355         env_relocate();
356         show_boot_progress(0x26);
357
358
359 #ifdef CONFIG_CMD_NET
360         /* IP Address */
361         bd_data.bi_ip_addr = getenv_IPaddr("ipaddr");
362 #endif
363
364 #if defined(CONFIG_PCI)
365         /*
366          * Do pci configuration
367          */
368         pci_init();
369 #endif
370
371         show_boot_progress(0x27);
372
373
374         stdio_init();
375
376         jumptable_init();
377
378         /* Initialize the console (after the relocation and devices init) */
379         console_init_r();
380
381 #ifdef CONFIG_MISC_INIT_R
382         /* miscellaneous platform dependent initialisations */
383         misc_init_r();
384 #endif
385
386 #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
387         WATCHDOG_RESET();
388         puts("PCMCIA:");
389         pcmcia_init();
390 #endif
391
392 #if defined(CONFIG_CMD_KGDB)
393         WATCHDOG_RESET();
394         puts("KGDB:  ");
395         kgdb_init();
396 #endif
397
398         /* enable exceptions */
399         enable_interrupts();
400         show_boot_progress(0x28);
401
402 #ifdef CONFIG_STATUS_LED
403         status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
404 #endif
405
406         udelay(20);
407
408         /* Initialize from environment */
409         load_addr = getenv_ulong("loadaddr", 16, load_addr);
410 #if defined(CONFIG_CMD_NET)
411         s = getenv("bootfile");
412
413         if (s != NULL)
414                 copy_filename(BootFile, s, sizeof(BootFile));
415 #endif
416
417         WATCHDOG_RESET();
418
419 #if defined(CONFIG_CMD_IDE)
420         WATCHDOG_RESET();
421         puts("IDE:   ");
422         ide_init();
423 #endif
424
425 #if defined(CONFIG_CMD_SCSI)
426         WATCHDOG_RESET();
427         puts("SCSI:  ");
428         scsi_init();
429 #endif
430
431 #if defined(CONFIG_CMD_DOC)
432         WATCHDOG_RESET();
433         puts("DOC:   ");
434         doc_init();
435 #endif
436
437 #ifdef CONFIG_BITBANGMII
438         bb_miiphy_init();
439 #endif
440 #if defined(CONFIG_CMD_NET)
441         WATCHDOG_RESET();
442         puts("Net:   ");
443         eth_initialize(gd->bd);
444 #endif
445
446 #if (defined(CONFIG_CMD_NET)) && (0)
447         WATCHDOG_RESET();
448 # ifdef DEBUG
449         puts("Reset Ethernet PHY\n");
450 # endif
451         reset_phy();
452 #endif
453
454 #ifdef CONFIG_LAST_STAGE_INIT
455         WATCHDOG_RESET();
456         /*
457          * Some parts can be only initialized if all others (like
458          * Interrupts) are up and running (i.e. the PC-style ISA
459          * keyboard).
460          */
461         last_stage_init();
462 #endif
463
464
465 #ifdef CONFIG_POST
466         post_run(NULL, POST_RAM | post_bootmode_get(0));
467 #endif
468
469         show_boot_progress(0x29);
470
471         /* main_loop() can return to retry autoboot, if so just run it again. */
472         for (;;)
473                 main_loop();
474
475         /* NOTREACHED - no way out of command loop except booting */
476 }
477
478 void hang(void)
479 {
480         puts("### ERROR ### Please RESET the board ###\n");
481         for (;;)
482                 ;
483 }
484
485 unsigned long do_go_exec(ulong (*entry)(int, char * const []),
486                          int argc, char * const argv[])
487 {
488         unsigned long ret = 0;
489         char **argv_tmp;
490
491         /*
492          * x86 does not use a dedicated register to pass the pointer to
493          * the global_data, so it is instead passed as argv[-1]. By using
494          * argv[-1], the called 'Application' can use the contents of
495          * argv natively. However, to safely use argv[-1] a new copy of
496          * argv is needed with the extra element
497          */
498         argv_tmp = malloc(sizeof(char *) * (argc + 1));
499
500         if (argv_tmp) {
501                 argv_tmp[0] = (char *)gd;
502
503                 memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
504
505                 ret = (entry) (argc, &argv_tmp[1]);
506                 free(argv_tmp);
507         }
508
509         return ret;
510 }
511
512 void setup_pcat_compatibility(void)
513         __attribute__((weak, alias("__setup_pcat_compatibility")));
514
515 void __setup_pcat_compatibility(void)
516 {
517 }