File: /usr/src/linux/arch/ppc/boot/tree/main.c

1     /*
2      * BK Id: SCCS/s.main.c 1.9 06/15/01 13:16:10 paulus
3      */
4     /*
5      *    Copyright (c) 1997 Paul Mackerras <paulus@cs.anu.edu.au>
6      *      Initial Power Macintosh COFF version.
7      *    Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
8      *      Modifications for an ELF-based IBM evaluation board version.
9      *    Copyright 2000-2001 MontaVista Software Inc.
10      *	PPC405GP modifications
11      * 	Author: MontaVista Software, Inc.
12      *         	frank_rowand@mvista.com or source@mvista.com
13      * 	   	debbie_chu@mvista.com
14      *
15      *    Module name: main.c
16      *
17      *    Description:
18      *      This module does most of the real work for the boot loader. It
19      *      checks the variables holding the absolute start address and size
20      *      of the Linux kernel "image" and initial RAM disk "initrd" sections
21      *      and if they are present, moves them to their "proper" locations.
22      *
23      *      For the Linux kernel, "proper" is physical address 0x00000000.
24      *      For the RAM disk, "proper" is the image's size below the top
25      *      of physical memory. The Linux kernel may be in either raw
26      *      binary form or compressed with GNU zip (aka gzip).
27      *
28      *    This program is free software; you can redistribute it and/or
29      *    modify it under the terms of the GNU General Public License 
30      *    as published by the Free Software Foundation; either version
31      *    2 of the License, or (at your option) any later version.
32      *
33      */
34     
35     #include <linux/config.h>
36     #include <asm/ppc4xx.h>
37     
38     #include "nonstdio.h"
39     #include "irSect.h"
40     #if defined(CONFIG_SERIAL_CONSOLE)
41     #include "ns16550.h"
42     #endif /* CONFIG_SERIAL_CONSOLE */
43     
44     
45     /* Preprocessor Defines */
46     
47     /*
48      * Location of the IBM boot ROM function pointer address for retrieving
49      * the board information structure.
50      */
51     
52     #define	BOARD_INFO_VECTOR	0xFFFE0B50
53     
54     /* 
55      * Warning: the board_info doesn't contain valid data until get_board_info() 
56      * 	    gets called in start().
57      */
58     #define RAM_SIZE        board_info.bi_memsize 
59     
60     #define	RAM_PBASE	0x00000000
61     #define	RAM_PEND	(RAM_PBASE + RAM_SIZE)
62     
63     #define	RAM_VBASE	0xC0000000
64     #define	RAM_VEND	(RAM_VBASE + RAM_SIZE)
65     
66     #define	RAM_START	RAM_PBASE
67     #define	RAM_END		RAM_PEND
68     #define	RAM_FREE	(imageSect_start + imageSect_size + initrdSect_size)
69     
70     #define	PROG_START	RAM_START
71     
72     
73     /* Function Macros */
74     
75     #define	ALIGN_UP(x, align)	(((x) + ((align) - 1)) & ~((align) - 1))
76     
77     /* Global Variables */
78     
79     /* Needed by zalloc and zfree for allocating memory */
80     
81     char *avail_ram;	/* Indicates start of RAM available for heap */
82     char *end_avail;	/* Indicates end of RAM available for heap */
83     
84     /* Needed for serial I/O.
85     */
86     extern unsigned long *com_port;
87     
88     bd_t	board_info;
89     
90     /*
91     ** The bootrom may change bootrom_cmdline to point to a buffer in the
92     ** bootrom.
93     */
94     char *bootrom_cmdline = "";
95     char treeboot_bootrom_cmdline[512];
96     
97     #ifdef CONFIG_CMDLINE
98     char *cmdline = CONFIG_CMDLINE;
99     #else
100     char *cmdline = "";
101     #endif
102     
103     /* Function Prototypes */
104     
105     extern void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp);
106     
107     void
108     kick_watchdog(void)
109     {
110     #ifdef CONFIG_405GP
111         mtspr(SPRN_TSR, (TSR_ENW | TSR_WIS));
112     #endif
113     }
114     
115     void start(void)
116     {
117         void *options;
118         int ns, oh, i;
119         unsigned long sa, len;
120         void *dst;
121         unsigned char *im;
122         unsigned long initrd_start, initrd_size;
123         bd_t *(*get_board_info)(void) = 
124     	    (bd_t *(*)(void))(*(unsigned long *)BOARD_INFO_VECTOR);
125         bd_t *bip = NULL;
126     
127     
128         com_port = (struct NS16550 *)serial_init(0);
129     
130     #ifdef CONFIG_405GP
131         /* turn off on-chip ethernet */
132         /* This is to fix a problem with early walnut bootrom. */
133      
134         {
135     	/* Physical mapping of ethernet register space. */
136     	static struct   ppc405_enet_regs *ppc405_enet_regp =
137     	(struct ppc405_enet_regs *)PPC405_EM0_REG_ADDR;
138     
139     	mtdcr(DCRN_MALCR, MALCR_MMSR);                /* 1st reset MAL */
140     
141     	while (mfdcr(DCRN_MALCR) & MALCR_MMSR) {};    /* wait for the reset */
142         
143     	ppc405_enet_regp->em0mr0 = 0x20000000;        /* then reset EMAC */
144         }
145     #endif
146     
147         if ((bip = get_board_info()) != NULL)
148     	    memcpy(&board_info, bip, sizeof(bd_t));
149     
150         /* Init RAM disk (initrd) section */
151     
152         kick_watchdog();
153     
154         if (initrdSect_start != 0 && (initrd_size = initrdSect_size) != 0) {
155             initrd_start = (RAM_END - initrd_size) & ~0xFFF;
156     
157     	_printk("Initial RAM disk at 0x%08x (%u bytes)\n",
158     	       initrd_start, initrd_size);
159     
160     	memcpy((char *)initrd_start,
161     	       (char *)(initrdSect_start),
162     	       initrdSect_size);
163     
164     	end_avail = (char *)initrd_start;
165         } else {
166     	initrd_start = initrd_size = 0;
167     	end_avail = (char *)RAM_END;
168         }
169     
170         /* Linux kernel image section */
171     	
172         kick_watchdog();
173     
174         im = (unsigned char *)(imageSect_start);
175         len = imageSect_size;
176         dst = (void *)PROG_START;
177     
178         /* Check for the gzip archive magic numbers */
179     
180         if (im[0] == 0x1f && im[1] == 0x8b) {
181     
182             /* The gunzip routine needs everything nice and aligned */
183     
184     	void *cp = (void *)ALIGN_UP(RAM_FREE, 8);
185     	avail_ram = (void *)(cp + ALIGN_UP(len, 8));	/* used by zalloc() */
186     	memcpy(cp, im, len);
187     
188     	/* I'm not sure what the 0x200000 parameter is for, but it works. */
189     	/* It tells gzip the end of the area you wish to reserve, and it
190     	 * can use data past that point....unfortunately, this value
191     	 * isn't big enough (luck ran out).  -- Dan
192     	 */
193     
194     	gunzip(dst, 0x400000, cp, (int *)&len);
195         } else {
196     	memmove(dst, im, len);
197         }
198     
199         kick_watchdog();
200     
201         flush_cache(dst, len);
202     
203         sa = (unsigned long)dst;
204     
205         (*(void (*)())sa)(&board_info,
206     		      initrd_start,
207     		      initrd_start + initrd_size,
208     		      cmdline,
209     		      cmdline + strlen(cmdline));
210     
211         pause();
212     }
213