TQM834x: clean up configuration
[platform/kernel/u-boot.git] / lib_arm / armlinux.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 <zlib.h>
28 #include <asm/byteorder.h>
29 #ifdef CONFIG_HAS_DATAFLASH
30 #include <dataflash.h>
31 #endif
32
33 DECLARE_GLOBAL_DATA_PTR;
34
35 /*cmd_boot.c*/
36 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
37
38 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
39     defined (CONFIG_CMDLINE_TAG) || \
40     defined (CONFIG_INITRD_TAG) || \
41     defined (CONFIG_SERIAL_TAG) || \
42     defined (CONFIG_REVISION_TAG) || \
43     defined (CONFIG_VFD) || \
44     defined (CONFIG_LCD)
45 static void setup_start_tag (bd_t *bd);
46
47 # ifdef CONFIG_SETUP_MEMORY_TAGS
48 static void setup_memory_tags (bd_t *bd);
49 # endif
50 static void setup_commandline_tag (bd_t *bd, char *commandline);
51
52 #if 0
53 static void setup_ramdisk_tag (bd_t *bd);
54 #endif
55 # ifdef CONFIG_INITRD_TAG
56 static void setup_initrd_tag (bd_t *bd, ulong initrd_start,
57                               ulong initrd_end);
58 # endif
59 static void setup_end_tag (bd_t *bd);
60
61 # if defined (CONFIG_VFD) || defined (CONFIG_LCD)
62 static void setup_videolfb_tag (gd_t *gd);
63 # endif
64
65
66 static struct tag *params;
67 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */
68
69 extern image_header_t header;   /* from cmd_bootm.c */
70
71
72 void do_bootm_linux (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
73                      ulong addr, ulong *len_ptr, int verify)
74 {
75         ulong len = 0, checksum;
76         ulong initrd_start, initrd_end;
77         ulong data;
78         void (*theKernel)(int zero, int arch, uint params);
79         image_header_t *hdr = &header;
80         bd_t *bd = gd->bd;
81         int machid = bd->bi_arch_number;
82         char *s;
83
84 #ifdef CONFIG_CMDLINE_TAG
85         char *commandline = getenv ("bootargs");
86 #endif
87
88         theKernel = (void (*)(int, int, uint))ntohl(hdr->ih_ep);
89
90         s = getenv ("machid");
91         if (s) {
92                 machid = simple_strtoul (s, NULL, 16);
93                 printf ("Using machid 0x%x from environment\n", machid);
94         }
95
96         /*
97          * Check if there is an initrd image
98          */
99         if (argc >= 3) {
100                 show_boot_progress (9);
101
102                 addr = simple_strtoul (argv[2], NULL, 16);
103
104                 printf ("## Loading Ramdisk Image at %08lx ...\n", addr);
105
106                 /* Copy header so we can blank CRC field for re-calculation */
107 #ifdef CONFIG_HAS_DATAFLASH
108                 if (addr_dataflash (addr)) {
109                         read_dataflash (addr, sizeof (image_header_t),
110                                         (char *) &header);
111                 } else
112 #endif
113                         memcpy (&header, (char *) addr,
114                                 sizeof (image_header_t));
115
116                 if (ntohl (hdr->ih_magic) != IH_MAGIC) {
117                         printf ("Bad Magic Number\n");
118                         show_boot_progress (-10);
119                         do_reset (cmdtp, flag, argc, argv);
120                 }
121
122                 data = (ulong) & header;
123                 len = sizeof (image_header_t);
124
125                 checksum = ntohl (hdr->ih_hcrc);
126                 hdr->ih_hcrc = 0;
127
128                 if (crc32 (0, (unsigned char *) data, len) != checksum) {
129                         printf ("Bad Header Checksum\n");
130                         show_boot_progress (-11);
131                         do_reset (cmdtp, flag, argc, argv);
132                 }
133
134                 show_boot_progress (10);
135
136                 print_image_hdr (hdr);
137
138                 data = addr + sizeof (image_header_t);
139                 len = ntohl (hdr->ih_size);
140
141 #ifdef CONFIG_HAS_DATAFLASH
142                 if (addr_dataflash (addr)) {
143                         read_dataflash (data, len, (char *) CFG_LOAD_ADDR);
144                         data = CFG_LOAD_ADDR;
145                 }
146 #endif
147
148                 if (verify) {
149                         ulong csum = 0;
150
151                         printf ("   Verifying Checksum ... ");
152                         csum = crc32 (0, (unsigned char *) data, len);
153                         if (csum != ntohl (hdr->ih_dcrc)) {
154                                 printf ("Bad Data CRC\n");
155                                 show_boot_progress (-12);
156                                 do_reset (cmdtp, flag, argc, argv);
157                         }
158                         printf ("OK\n");
159                 }
160
161                 show_boot_progress (11);
162
163                 if ((hdr->ih_os != IH_OS_LINUX) ||
164                     (hdr->ih_arch != IH_CPU_ARM) ||
165                     (hdr->ih_type != IH_TYPE_RAMDISK)) {
166                         printf ("No Linux ARM Ramdisk Image\n");
167                         show_boot_progress (-13);
168                         do_reset (cmdtp, flag, argc, argv);
169                 }
170
171 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
172                 /*
173                  *we need to copy the ramdisk to SRAM to let Linux boot
174                  */
175                 memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
176                 data = ntohl(hdr->ih_load);
177 #endif /* CONFIG_B2 || CONFIG_EVB4510 */
178
179                 /*
180                  * Now check if we have a multifile image
181                  */
182         } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
183                 ulong tail = ntohl (len_ptr[0]) % 4;
184                 int i;
185
186                 show_boot_progress (13);
187
188                 /* skip kernel length and terminator */
189                 data = (ulong) (&len_ptr[2]);
190                 /* skip any additional image length fields */
191                 for (i = 1; len_ptr[i]; ++i)
192                         data += 4;
193                 /* add kernel length, and align */
194                 data += ntohl (len_ptr[0]);
195                 if (tail) {
196                         data += 4 - tail;
197                 }
198
199                 len = ntohl (len_ptr[1]);
200
201         } else {
202                 /*
203                  * no initrd image
204                  */
205                 show_boot_progress (14);
206
207                 len = data = 0;
208         }
209
210 #ifdef  DEBUG
211         if (!data) {
212                 printf ("No initrd\n");
213         }
214 #endif
215
216         if (data) {
217                 initrd_start = data;
218                 initrd_end = initrd_start + len;
219         } else {
220                 initrd_start = 0;
221                 initrd_end = 0;
222         }
223
224         show_boot_progress (15);
225
226         debug ("## Transferring control to Linux (at address %08lx) ...\n",
227                (ulong) theKernel);
228
229 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
230     defined (CONFIG_CMDLINE_TAG) || \
231     defined (CONFIG_INITRD_TAG) || \
232     defined (CONFIG_SERIAL_TAG) || \
233     defined (CONFIG_REVISION_TAG) || \
234     defined (CONFIG_LCD) || \
235     defined (CONFIG_VFD)
236         setup_start_tag (bd);
237 #ifdef CONFIG_SERIAL_TAG
238         setup_serial_tag (&params);
239 #endif
240 #ifdef CONFIG_REVISION_TAG
241         setup_revision_tag (&params);
242 #endif
243 #ifdef CONFIG_SETUP_MEMORY_TAGS
244         setup_memory_tags (bd);
245 #endif
246 #ifdef CONFIG_CMDLINE_TAG
247         setup_commandline_tag (bd, commandline);
248 #endif
249 #ifdef CONFIG_INITRD_TAG
250         if (initrd_start && initrd_end)
251                 setup_initrd_tag (bd, initrd_start, initrd_end);
252 #endif
253 #if defined (CONFIG_VFD) || defined (CONFIG_LCD)
254         setup_videolfb_tag ((gd_t *) gd);
255 #endif
256         setup_end_tag (bd);
257 #endif
258
259         /* we assume that the kernel is in place */
260         printf ("\nStarting kernel ...\n\n");
261
262 #ifdef CONFIG_USB_DEVICE
263         {
264                 extern void udc_disconnect (void);
265                 udc_disconnect ();
266         }
267 #endif
268
269         cleanup_before_linux ();
270
271         theKernel (0, machid, bd->bi_boot_params);
272 }
273
274
275 #if defined (CONFIG_SETUP_MEMORY_TAGS) || \
276     defined (CONFIG_CMDLINE_TAG) || \
277     defined (CONFIG_INITRD_TAG) || \
278     defined (CONFIG_SERIAL_TAG) || \
279     defined (CONFIG_REVISION_TAG) || \
280     defined (CONFIG_LCD) || \
281     defined (CONFIG_VFD)
282 static void setup_start_tag (bd_t *bd)
283 {
284         params = (struct tag *) bd->bi_boot_params;
285
286         params->hdr.tag = ATAG_CORE;
287         params->hdr.size = tag_size (tag_core);
288
289         params->u.core.flags = 0;
290         params->u.core.pagesize = 0;
291         params->u.core.rootdev = 0;
292
293         params = tag_next (params);
294 }
295
296
297 #ifdef CONFIG_SETUP_MEMORY_TAGS
298 static void setup_memory_tags (bd_t *bd)
299 {
300         int i;
301
302         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
303                 params->hdr.tag = ATAG_MEM;
304                 params->hdr.size = tag_size (tag_mem32);
305
306                 params->u.mem.start = bd->bi_dram[i].start;
307                 params->u.mem.size = bd->bi_dram[i].size;
308
309                 params = tag_next (params);
310         }
311 }
312 #endif /* CONFIG_SETUP_MEMORY_TAGS */
313
314
315 static void setup_commandline_tag (bd_t *bd, char *commandline)
316 {
317         char *p;
318
319         if (!commandline)
320                 return;
321
322         /* eat leading white space */
323         for (p = commandline; *p == ' '; p++);
324
325         /* skip non-existent command lines so the kernel will still
326          * use its default command line.
327          */
328         if (*p == '\0')
329                 return;
330
331         params->hdr.tag = ATAG_CMDLINE;
332         params->hdr.size =
333                 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
334
335         strcpy (params->u.cmdline.cmdline, p);
336
337         params = tag_next (params);
338 }
339
340
341 #ifdef CONFIG_INITRD_TAG
342 static void setup_initrd_tag (bd_t *bd, ulong initrd_start, ulong initrd_end)
343 {
344         /* an ATAG_INITRD node tells the kernel where the compressed
345          * ramdisk can be found. ATAG_RDIMG is a better name, actually.
346          */
347         params->hdr.tag = ATAG_INITRD2;
348         params->hdr.size = tag_size (tag_initrd);
349
350         params->u.initrd.start = initrd_start;
351         params->u.initrd.size = initrd_end - initrd_start;
352
353         params = tag_next (params);
354 }
355 #endif /* CONFIG_INITRD_TAG */
356
357
358 #if defined (CONFIG_VFD) || defined (CONFIG_LCD)
359 extern ulong calc_fbsize (void);
360 static void setup_videolfb_tag (gd_t *gd)
361 {
362         /* An ATAG_VIDEOLFB node tells the kernel where and how large
363          * the framebuffer for video was allocated (among other things).
364          * Note that a _physical_ address is passed !
365          *
366          * We only use it to pass the address and size, the other entries
367          * in the tag_videolfb are not of interest.
368          */
369         params->hdr.tag = ATAG_VIDEOLFB;
370         params->hdr.size = tag_size (tag_videolfb);
371
372         params->u.videolfb.lfb_base = (u32) gd->fb_base;
373         /* Fb size is calculated according to parameters for our panel
374          */
375         params->u.videolfb.lfb_size = calc_fbsize();
376
377         params = tag_next (params);
378 }
379 #endif /* CONFIG_VFD || CONFIG_LCD */
380
381 #ifdef CONFIG_SERIAL_TAG
382 void setup_serial_tag (struct tag **tmp)
383 {
384         struct tag *params = *tmp;
385         struct tag_serialnr serialnr;
386         void get_board_serial(struct tag_serialnr *serialnr);
387
388         get_board_serial(&serialnr);
389         params->hdr.tag = ATAG_SERIAL;
390         params->hdr.size = tag_size (tag_serialnr);
391         params->u.serialnr.low = serialnr.low;
392         params->u.serialnr.high= serialnr.high;
393         params = tag_next (params);
394         *tmp = params;
395 }
396 #endif
397
398 #ifdef CONFIG_REVISION_TAG
399 void setup_revision_tag(struct tag **in_params)
400 {
401         u32 rev = 0;
402         u32 get_board_rev(void);
403
404         rev = get_board_rev();
405         params->hdr.tag = ATAG_REVISION;
406         params->hdr.size = tag_size (tag_revision);
407         params->u.revision.rev = rev;
408         params = tag_next (params);
409 }
410 #endif  /* CONFIG_REVISION_TAG */
411
412
413 static void setup_end_tag (bd_t *bd)
414 {
415         params->hdr.tag = ATAG_NONE;
416         params->hdr.size = 0;
417 }
418
419 #endif /* CONFIG_SETUP_MEMORY_TAGS || CONFIG_CMDLINE_TAG || CONFIG_INITRD_TAG */