c1b6e24d4e8279b4d9e251fed1855337e87520ec
[profile/mobile/platform/kernel/u-boot-tm1.git] / arch / arm / lib / bootm.c
1 /*
2  * (C) Copyright 2002
3  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4  * Marius Groeger <mgroeger@sysgo.de>
5  *
6  * Copyright (C) 2001  Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <common.h>
25 #include <command.h>
26 #include <image.h>
27 #include <u-boot/zlib.h>
28 #include <asm/byteorder.h>
29 #include <fdt.h>
30 #include <libfdt.h>
31 #include <fdt_support.h>
32
33 #ifdef CMDLINE_NEED_CONV
34 #include <asm/arch/endian_conv.h>
35 #endif
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
40     defined (CONFIG_CMDLINE_TAG) || \
41     defined (CONFIG_INITRD_TAG) || \
42     defined (CONFIG_SERIAL_TAG) || \
43     defined (CONFIG_REVISION_TAG)
44 static void setup_start_tag (bd_t *bd);
45
46 # ifdef CONFIG_SETUP_MEMORY_TAGS
47 static void setup_memory_tags (bd_t *bd);
48 # endif
49 static void setup_commandline_tag (bd_t *bd, char *commandline);
50
51 # ifdef CONFIG_INITRD_TAG
52 static void setup_initrd_tag (bd_t *bd, ulong initrd_start,
53                               ulong initrd_end);
54 # endif
55 static void setup_end_tag (bd_t *bd);
56
57 static struct tag *params;
58 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
59
60 static ulong get_sp(void);
61 #if defined(CONFIG_OF_LIBFDT)
62 static int bootm_linux_fdt(int machid, bootm_headers_t *images);
63 #endif
64
65 void arch_lmb_reserve(struct lmb *lmb)
66 {
67         ulong sp;
68
69         /*
70          * Booting a (Linux) kernel image
71          *
72          * Allocate space for command line and board info - the
73          * address should be as high as possible within the reach of
74          * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
75          * memory, which means far enough below the current stack
76          * pointer.
77          */
78         sp = get_sp();
79         debug("## Current stack ends at 0x%08lx ", sp);
80
81         /* adjust sp by 1K to be safe */
82         sp -= 1024;
83         lmb_reserve(lmb, sp,
84                     gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size - sp);
85 }
86
87 static void announce_and_cleanup(void)
88 {
89         printf("\nStarting kernel ...\n\n");
90
91 #ifdef CONFIG_USB_DEVICE
92         {
93                 extern void udc_disconnect(void);
94                 udc_disconnect();
95         }
96 #endif
97         cleanup_before_linux();
98 }
99
100 int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
101 {
102         bd_t    *bd = gd->bd;
103         char    *s;
104         int     machid = bd->bi_arch_number;
105         void    (*kernel_entry)(int zero, int arch, uint params);
106
107 #ifdef CONFIG_CMDLINE_TAG
108         char *commandline = getenv ("bootargs");
109 #endif
110
111         if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
112                 return 1;
113
114         s = getenv ("machid");
115         if (s) {
116                 machid = simple_strtoul (s, NULL, 16);
117                 printf ("Using machid 0x%x from environment\n", machid);
118         }
119
120         show_boot_progress (15);
121
122 #ifdef CONFIG_OF_LIBFDT
123         if (images->ft_len)
124                 return bootm_linux_fdt(machid, images);
125 #endif
126
127         kernel_entry = (void (*)(int, int, uint))images->ep;
128
129         debug ("## Transferring control to Linux (at address %08lx) ...\n",
130                (ulong) kernel_entry);
131
132 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
133     defined (CONFIG_CMDLINE_TAG) || \
134     defined (CONFIG_INITRD_TAG) || \
135     defined (CONFIG_SERIAL_TAG) || \
136     defined (CONFIG_REVISION_TAG)
137         setup_start_tag (bd);
138 #ifdef CONFIG_SERIAL_TAG
139         setup_serial_tag (&params);
140 #endif
141 #ifdef CONFIG_REVISION_TAG
142         setup_revision_tag (&params);
143 #endif
144 #ifdef CONFIG_SETUP_MEMORY_TAGS
145         setup_memory_tags (bd);
146 #endif
147 #ifdef CONFIG_CMDLINE_TAG
148         setup_commandline_tag (bd, commandline);
149 #endif
150 #ifdef CONFIG_INITRD_TAG
151         if (images->rd_start && images->rd_end)
152                 setup_initrd_tag (bd, images->rd_start, images->rd_end);
153 #endif
154         setup_end_tag(bd);
155 #endif
156
157         announce_and_cleanup();
158
159         kernel_entry(0, machid, bd->bi_boot_params);
160         /* does not return */
161
162         return 1;
163 }
164
165 #if defined(CONFIG_OF_LIBFDT)
166 static int fixup_memory_node(void *blob)
167 {
168         bd_t    *bd = gd->bd;
169         int bank;
170         u64 start[CONFIG_NR_DRAM_BANKS];
171         u64 size[CONFIG_NR_DRAM_BANKS];
172
173         for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
174                 start[bank] = bd->bi_dram[bank].start;
175                 size[bank] = bd->bi_dram[bank].size;
176         }
177
178         return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
179 }
180
181 static int bootm_linux_fdt(int machid, bootm_headers_t *images)
182 {
183         ulong rd_len;
184         void (*kernel_entry)(int zero, int dt_machid, void *dtblob);
185         ulong bootmap_base = getenv_bootm_low();
186         ulong of_size = images->ft_len;
187         char **of_flat_tree = &images->ft_addr;
188         ulong *initrd_start = &images->initrd_start;
189         ulong *initrd_end = &images->initrd_end;
190         struct lmb *lmb = &images->lmb;
191         int ret;
192
193         kernel_entry = (void (*)(int, int, void *))images->ep;
194
195         rd_len = images->rd_end - images->rd_start;
196         ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
197                                 initrd_start, initrd_end);
198         if (ret)
199                 return ret;
200
201         //ret = boot_relocate_fdt(lmb, bootmap_base, of_flat_tree, &of_size);
202         //if (ret)
203         //      return ret;
204
205         debug("## Transferring control to Linux (at address %08lx) ...\n",
206                (ulong) kernel_entry);
207
208         fdt_chosen(*of_flat_tree, 1);
209
210         fixup_memory_node(*of_flat_tree);
211
212         fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
213
214         announce_and_cleanup();
215
216 #ifdef SPRD_EVM_TAG_ON
217         SPRD_EVM_TAG(17);
218         int ijk = 0;
219         int last = 0, now;
220         printf("boot steps evalution:\n");
221         for(ijk=0;ijk<18;ijk++){
222                 now = *((unsigned long *)SPRD_EVM_ADDR_START+ ijk);
223                 printf("%03d\taddr %x\t%lu\t%u\n", ijk,(unsigned long *)SPRD_EVM_ADDR_START+ ijk,now ,now-last);
224                 last = now;
225         }
226 #if 0
227         unsigned int temp_cnt;
228         for(ijk=0;ijk<0xffffff;ijk++){
229                 temp_cnt = *(volatile unsigned long *)0x87003004;
230                 udelay(1000);
231 (*(((unsigned long *)0x2000000)+ijk) = *(volatile unsigned long *)0x87003004);
232                 printf(" the cnt is %x\n", temp_cnt);
233                 printf(" [0x%08x] 0x%08x\n", ((unsigned int *)0x2000000)+ijk,*(((unsigned int *)0x2000000)+ijk));
234         }
235 #endif
236         while(1);
237 #endif
238         kernel_entry(0, machid, *of_flat_tree);
239         /* does not return */
240
241         return 1;
242 }
243 #endif
244
245 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
246     defined (CONFIG_CMDLINE_TAG) || \
247     defined (CONFIG_INITRD_TAG) || \
248     defined (CONFIG_SERIAL_TAG) || \
249     defined (CONFIG_REVISION_TAG)
250 static void setup_start_tag (bd_t *bd)
251 {
252         params = (struct tag *) bd->bi_boot_params;
253
254         params->hdr.tag = ATAG_CORE;
255         params->hdr.size = tag_size (tag_core);
256
257         params->u.core.flags = 0;
258         params->u.core.pagesize = 0;
259         params->u.core.rootdev = 0;
260
261         params = tag_next (params);
262 }
263
264
265 #ifdef CONFIG_SETUP_MEMORY_TAGS
266 static void setup_memory_tags (bd_t *bd)
267 {
268         int i;
269
270         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
271                 params->hdr.tag = ATAG_MEM;
272                 params->hdr.size = tag_size (tag_mem32);
273
274                 params->u.mem.start = bd->bi_dram[i].start;
275                 params->u.mem.size = bd->bi_dram[i].size;
276
277                 params = tag_next (params);
278         }
279 }
280 #endif /* CONFIG_SETUP_MEMORY_TAGS */
281
282
283 static void setup_commandline_tag (bd_t *bd, char *commandline)
284 {
285         char *p;
286
287         if (!commandline)
288                 return;
289
290         /* eat leading white space */
291         for (p = commandline; *p == ' '; p++);
292
293         /* skip non-existent command lines so the kernel will still
294          * use its default command line.
295          */
296         if (*p == '\0')
297                 return;
298
299         params->hdr.tag = ATAG_CMDLINE;
300         params->hdr.size =
301                 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
302
303         strcpy (params->u.cmdline.cmdline, p);
304 #ifdef CMDLINE_NEED_CONV
305         int i = 0, len_temp = 0;
306         len_temp = strlen(p);
307         len_temp = (len_temp +3) >> 2;
308
309         for(i=0;i<len_temp;i++){
310                 INT_SWAP((*((uint32_t *)params->u.cmdline.cmdline +i)));
311         }
312 #endif
313
314         params = tag_next (params);
315 }
316
317
318 #ifdef CONFIG_INITRD_TAG
319 static void setup_initrd_tag (bd_t *bd, ulong initrd_start, ulong initrd_end)
320 {
321         /* an ATAG_INITRD node tells the kernel where the compressed
322          * ramdisk can be found. ATAG_RDIMG is a better name, actually.
323          */
324         params->hdr.tag = ATAG_INITRD2;
325         params->hdr.size = tag_size (tag_initrd);
326
327         params->u.initrd.start = initrd_start;
328         params->u.initrd.size = initrd_end - initrd_start;
329
330         params = tag_next (params);
331 }
332 #endif /* CONFIG_INITRD_TAG */
333
334 #ifdef CONFIG_SERIAL_TAG
335 void setup_serial_tag (struct tag **tmp)
336 {
337         struct tag *params = *tmp;
338         struct tag_serialnr serialnr;
339         void get_board_serial(struct tag_serialnr *serialnr);
340
341         get_board_serial(&serialnr);
342         params->hdr.tag = ATAG_SERIAL;
343         params->hdr.size = tag_size (tag_serialnr);
344         params->u.serialnr.low = serialnr.low;
345         params->u.serialnr.high= serialnr.high;
346         params = tag_next (params);
347         *tmp = params;
348 }
349 #endif
350
351 #ifdef CONFIG_REVISION_TAG
352 void setup_revision_tag(struct tag **in_params)
353 {
354         u32 rev = 0;
355         u32 get_board_rev(void);
356
357         rev = get_board_rev();
358         params->hdr.tag = ATAG_REVISION;
359         params->hdr.size = tag_size (tag_revision);
360         params->u.revision.rev = rev;
361         params = tag_next (params);
362 }
363 #endif  /* CONFIG_REVISION_TAG */
364
365
366 static void setup_end_tag (bd_t *bd)
367 {
368         params->hdr.tag = ATAG_NONE;
369         params->hdr.size = 0;
370 }
371
372 static ulong get_sp(void)
373 {
374         ulong ret;
375
376         asm("mov %0, sp" : "=r"(ret) : );
377         return ret;
378 }
379
380 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */