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