Merge with /home/wd/git/u-boot/mailing-list/Haavard_Skinnemoen
[platform/kernel/u-boot.git] / lib_avr32 / avr32_linux.c
1 /*
2  * Copyright (C) 2004-2006 Atmel Corporation
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
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.
11  *
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.
16  *
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
21  */
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>
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
35
36 /* CPU-specific hook to allow flushing of caches, etc. */
37 extern void prepare_to_boot(void);
38
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
45
46 extern image_header_t header;           /* from cmd_bootm.c */
47
48 static struct tag *setup_start_tag(struct tag *params)
49 {
50         params->hdr.tag = ATAG_CORE;
51         params->hdr.size = tag_size(tag_core);
52
53         params->u.core.flags = 0;
54         params->u.core.pagesize = 4096;
55         params->u.core.rootdev = 0;
56
57         return tag_next(params);
58 }
59
60 static struct tag *setup_memory_tags(struct tag *params)
61 {
62         bd_t *bd = gd->bd;
63         int i;
64
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);
68
69                 params->u.mem_range.addr = bd->bi_dram[i].start;
70                 params->u.mem_range.size = bd->bi_dram[i].size;
71
72                 params = tag_next(params);
73         }
74
75         return params;
76 }
77
78 static struct tag *setup_commandline_tag(struct tag *params, char *cmdline)
79 {
80         if (!cmdline)
81                 return params;
82
83         /* eat leading white space */
84         while (*cmdline == ' ') cmdline++;
85
86         /*
87          * Don't include tags for empty command lines; let the kernel
88          * use its default command line.
89          */
90         if (*cmdline == '\0')
91                 return params;
92
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);
97
98         return tag_next(params);
99 }
100
101 static struct tag *setup_ramdisk_tag(struct tag *params,
102                                      unsigned long rd_start,
103                                      unsigned long rd_end)
104 {
105         if (rd_start == rd_end)
106                 return params;
107
108         params->hdr.tag = ATAG_RDIMG;
109         params->hdr.size = tag_size(tag_mem_range);
110
111         params->u.mem_range.addr = rd_start;
112         params->u.mem_range.size = rd_end - rd_start;
113
114         return tag_next(params);
115 }
116
117 static struct tag *setup_clock_tags(struct tag *params)
118 {
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;
124
125 #ifdef CONFIG_AT32AP7000
126         /*
127          * New kernels don't need this, but we should be backwards
128          * compatible for a while...
129          */
130         params = tag_next(params);
131
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
138
139         return tag_next(params);
140 }
141
142 static struct tag *setup_ethernet_tag(struct tag *params,
143                                       char *addr, int index)
144 {
145         char *s, *e;
146         int i;
147
148         params->hdr.tag = ATAG_ETHERNET;
149         params->hdr.size = tag_size(tag_ethernet);
150
151         params->u.ethernet.mac_index = index;
152         params->u.ethernet.mii_phy_addr = gd->bd->bi_phy_id[index];
153
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;
158         }
159
160         return tag_next(params);
161 }
162
163 static struct tag *setup_ethernet_tags(struct tag *params)
164 {
165         char name[16] = "ethaddr";
166         char *addr;
167         int i = 0;
168
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);
175
176         return params;
177 }
178
179 static void setup_end_tag(struct tag *params)
180 {
181         params->hdr.tag = ATAG_NONE;
182         params->hdr.size = 0;
183 }
184
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)
187 {
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");
196
197         hdr = (image_header_t *)addr;
198         image_start = addr;
199         image_end = addr + hdr->ih_size;
200
201         theKernel = (void *)ntohl(hdr->ih_ep);
202
203         /*
204          * Check if there is an initrd image
205          */
206         if (argc >= 3) {
207                 SHOW_BOOT_PROGRESS(9);
208
209                 addr = simple_strtoul(argv[2], NULL, 16);
210
211                 printf("## Loading RAMDISK image at %08lx ...\n", addr);
212
213                 memcpy(&header, (char *)addr, sizeof(header));
214                 hdr = &header;
215
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);
220                 }
221
222                 data = (unsigned long)hdr;
223                 len = sizeof(*hdr);
224                 checksum = ntohl(hdr->ih_hcrc);
225                 hdr->ih_hcrc = 0;
226
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);
231                 }
232
233                 SHOW_BOOT_PROGRESS(10);
234
235                 print_image_hdr(hdr);
236
237                 data = addr + sizeof(header);
238                 len = ntohl(hdr->ih_size);
239
240                 if (verify) {
241                         unsigned long csum = 0;
242
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);
249                         }
250                         puts("OK\n");
251                 }
252
253                 SHOW_BOOT_PROGRESS(11);
254
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);
261                 }
262         } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
263                 ulong tail = ntohl (len_ptr[0]) % 4;
264                 int i;
265
266                 SHOW_BOOT_PROGRESS (13);
267
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;
277                 }
278
279                 len = ntohl (len_ptr[1]);
280         } else {
281                 /* no initrd image */
282                 SHOW_BOOT_PROGRESS(14);
283                 len = data = 0;
284         }
285
286         if (data) {
287                 initrd_start = data;
288                 initrd_end = initrd_start + len;
289         } else {
290                 initrd_start = 0;
291                 initrd_end = 0;
292         }
293
294         SHOW_BOOT_PROGRESS(15);
295
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));
303         }
304         params = setup_commandline_tag(params, commandline);
305         params = setup_clock_tags(params);
306         params = setup_ethernet_tags(params);
307         setup_end_tag(params);
308
309         printf("\nStarting kernel at %p (params at %p)...\n\n",
310                theKernel, params_start);
311
312         prepare_to_boot();
313
314         theKernel(ATAG_MAGIC, params_start);
315 }