to make u-boot work for fat32 filesystem
[jz_uboot.git] / lib_avr32 / avr32_linux.c
blobd128dfb53f075b01b8f73616b59a4af6f4553896
1 /*
2 * Copyright (C) 2004-2006 Atmel Corporation
4 * See file CREDITS for list of people who contributed to this
5 * project.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
22 #include <common.h>
23 #include <command.h>
24 #include <image.h>
25 #include <zlib.h>
26 #include <asm/byteorder.h>
27 #include <asm/addrspace.h>
28 #include <asm/io.h>
29 #include <asm/setup.h>
30 #include <asm/arch/platform.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
36 /* CPU-specific hook to allow flushing of caches, etc. */
37 extern void prepare_to_boot(void);
39 #ifdef CONFIG_SHOW_BOOT_PROGRESS
40 # include <status_led.h>
41 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
42 #else
43 # define SHOW_BOOT_PROGRESS(arg)
44 #endif
46 extern image_header_t header; /* from cmd_bootm.c */
48 static struct tag *setup_start_tag(struct tag *params)
50 params->hdr.tag = ATAG_CORE;
51 params->hdr.size = tag_size(tag_core);
53 params->u.core.flags = 0;
54 params->u.core.pagesize = 4096;
55 params->u.core.rootdev = 0;
57 return tag_next(params);
60 static struct tag *setup_memory_tags(struct tag *params)
62 bd_t *bd = gd->bd;
63 int i;
65 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
66 params->hdr.tag = ATAG_MEM;
67 params->hdr.size = tag_size(tag_mem_range);
69 params->u.mem_range.addr = bd->bi_dram[i].start;
70 params->u.mem_range.size = bd->bi_dram[i].size;
72 params = tag_next(params);
75 return params;
78 static struct tag *setup_commandline_tag(struct tag *params, char *cmdline)
80 if (!cmdline)
81 return params;
83 /* eat leading white space */
84 while (*cmdline == ' ') cmdline++;
87 * Don't include tags for empty command lines; let the kernel
88 * use its default command line.
90 if (*cmdline == '\0')
91 return params;
93 params->hdr.tag = ATAG_CMDLINE;
94 params->hdr.size =
95 (sizeof (struct tag_header) + strlen(cmdline) + 1 + 3) >> 2;
96 strcpy(params->u.cmdline.cmdline, cmdline);
98 return tag_next(params);
101 static struct tag *setup_ramdisk_tag(struct tag *params,
102 unsigned long rd_start,
103 unsigned long rd_end)
105 if (rd_start == rd_end)
106 return params;
108 params->hdr.tag = ATAG_RDIMG;
109 params->hdr.size = tag_size(tag_mem_range);
111 params->u.mem_range.addr = rd_start;
112 params->u.mem_range.size = rd_end - rd_start;
114 return tag_next(params);
117 static struct tag *setup_clock_tags(struct tag *params)
119 params->hdr.tag = ATAG_CLOCK;
120 params->hdr.size = tag_size(tag_clock);
121 params->u.clock.clock_id = ACLOCK_BOOTCPU;
122 params->u.clock.clock_flags = 0;
123 params->u.clock.clock_hz = gd->cpu_hz;
125 #ifdef CONFIG_AT32AP7000
127 * New kernels don't need this, but we should be backwards
128 * compatible for a while...
130 params = tag_next(params);
132 params->hdr.tag = ATAG_CLOCK;
133 params->hdr.size = tag_size(tag_clock);
134 params->u.clock.clock_id = ACLOCK_HSB;
135 params->u.clock.clock_flags = 0;
136 params->u.clock.clock_hz = pm_get_clock_freq(CLOCK_HSB);
137 #endif
139 return tag_next(params);
142 static struct tag *setup_ethernet_tag(struct tag *params,
143 char *addr, int index)
145 char *s, *e;
146 int i;
148 params->hdr.tag = ATAG_ETHERNET;
149 params->hdr.size = tag_size(tag_ethernet);
151 params->u.ethernet.mac_index = index;
152 params->u.ethernet.mii_phy_addr = gd->bd->bi_phy_id[index];
154 s = addr;
155 for (i = 0; i < 6; i++) {
156 params->u.ethernet.hw_address[i] = simple_strtoul(s, &e, 16);
157 s = e + 1;
160 return tag_next(params);
163 static struct tag *setup_ethernet_tags(struct tag *params)
165 char name[16] = "ethaddr";
166 char *addr;
167 int i = 0;
169 do {
170 addr = getenv(name);
171 if (addr)
172 params = setup_ethernet_tag(params, addr, i);
173 sprintf(name, "eth%daddr", ++i);
174 } while (i < 4);
176 return params;
179 static void setup_end_tag(struct tag *params)
181 params->hdr.tag = ATAG_NONE;
182 params->hdr.size = 0;
185 void do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
186 unsigned long addr, unsigned long *len_ptr, int verify)
188 unsigned long data, len = 0;
189 unsigned long initrd_start, initrd_end;
190 unsigned long image_start, image_end;
191 unsigned long checksum;
192 void (*theKernel)(int magic, void *tagtable);
193 image_header_t *hdr;
194 struct tag *params, *params_start;
195 char *commandline = getenv("bootargs");
197 hdr = (image_header_t *)addr;
198 image_start = addr;
199 image_end = addr + hdr->ih_size;
201 theKernel = (void *)ntohl(hdr->ih_ep);
204 * Check if there is an initrd image
206 if (argc >= 3) {
207 SHOW_BOOT_PROGRESS(9);
209 addr = simple_strtoul(argv[2], NULL, 16);
211 printf("## Loading RAMDISK image at %08lx ...\n", addr);
213 memcpy(&header, (char *)addr, sizeof(header));
214 hdr = &header;
216 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
217 puts("Bad Magic Number\n");
218 SHOW_BOOT_PROGRESS(-10);
219 do_reset(cmdtp, flag, argc, argv);
222 data = (unsigned long)hdr;
223 len = sizeof(*hdr);
224 checksum = ntohl(hdr->ih_hcrc);
225 hdr->ih_hcrc = 0;
227 if (crc32(0, (unsigned char *)data, len) != checksum) {
228 puts("Bad Header Checksum\n");
229 SHOW_BOOT_PROGRESS(-11);
230 do_reset(cmdtp, flag, argc, argv);
233 SHOW_BOOT_PROGRESS(10);
235 print_image_hdr(hdr);
237 data = addr + sizeof(header);
238 len = ntohl(hdr->ih_size);
240 if (verify) {
241 unsigned long csum = 0;
243 puts(" Verifying Checksum ... ");
244 csum = crc32(0, (unsigned char *)data, len);
245 if (csum != ntohl(hdr->ih_dcrc)) {
246 puts("Bad Data CRC\n");
247 SHOW_BOOT_PROGRESS(-12);
248 do_reset(cmdtp, flag, argc, argv);
250 puts("OK\n");
253 SHOW_BOOT_PROGRESS(11);
255 if ((hdr->ih_os != IH_OS_LINUX) ||
256 (hdr->ih_arch != IH_CPU_AVR32) ||
257 (hdr->ih_type != IH_TYPE_RAMDISK)) {
258 puts("Not a Linux/AVR32 RAMDISK image\n");
259 SHOW_BOOT_PROGRESS(-13);
260 do_reset(cmdtp, flag, argc, argv);
262 } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
263 ulong tail = ntohl (len_ptr[0]) % 4;
264 int i;
266 SHOW_BOOT_PROGRESS (13);
268 /* skip kernel length and terminator */
269 data = (ulong) (&len_ptr[2]);
270 /* skip any additional image length fields */
271 for (i = 1; len_ptr[i]; ++i)
272 data += 4;
273 /* add kernel length, and align */
274 data += ntohl (len_ptr[0]);
275 if (tail) {
276 data += 4 - tail;
279 len = ntohl (len_ptr[1]);
280 } else {
281 /* no initrd image */
282 SHOW_BOOT_PROGRESS(14);
283 len = data = 0;
286 if (data) {
287 initrd_start = data;
288 initrd_end = initrd_start + len;
289 } else {
290 initrd_start = 0;
291 initrd_end = 0;
294 SHOW_BOOT_PROGRESS(15);
296 params = params_start = (struct tag *)gd->bd->bi_boot_params;
297 params = setup_start_tag(params);
298 params = setup_memory_tags(params);
299 if (initrd_start) {
300 params = setup_ramdisk_tag(params,
301 PHYSADDR(initrd_start),
302 PHYSADDR(initrd_end));
304 params = setup_commandline_tag(params, commandline);
305 params = setup_clock_tags(params);
306 params = setup_ethernet_tags(params);
307 setup_end_tag(params);
309 printf("\nStarting kernel at %p (params at %p)...\n\n",
310 theKernel, params_start);
312 prepare_to_boot();
314 theKernel(ATAG_MAGIC, params_start);