avr32_linux.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. DECLARE_GLOBAL_DATA_PTR;
  32. extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  33. /* CPU-specific hook to allow flushing of caches, etc. */
  34. extern void prepare_to_boot(void);
  35. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  36. # include <status_led.h>
  37. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  38. #else
  39. # define SHOW_BOOT_PROGRESS(arg)
  40. #endif
  41. extern image_header_t header; /* from cmd_bootm.c */
  42. static struct tag *setup_start_tag(struct tag *params)
  43. {
  44. params->hdr.tag = ATAG_CORE;
  45. params->hdr.size = tag_size(tag_core);
  46. params->u.core.flags = 0;
  47. params->u.core.pagesize = 4096;
  48. params->u.core.rootdev = 0;
  49. return tag_next(params);
  50. }
  51. static struct tag *setup_memory_tags(struct tag *params)
  52. {
  53. bd_t *bd = gd->bd;
  54. int i;
  55. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  56. params->hdr.tag = ATAG_MEM;
  57. params->hdr.size = tag_size(tag_mem_range);
  58. params->u.mem_range.addr = bd->bi_dram[i].start;
  59. params->u.mem_range.size = bd->bi_dram[i].size;
  60. params = tag_next(params);
  61. }
  62. return params;
  63. }
  64. static struct tag *setup_commandline_tag(struct tag *params, char *cmdline)
  65. {
  66. if (!cmdline)
  67. return params;
  68. /* eat leading white space */
  69. while (*cmdline == ' ') cmdline++;
  70. /*
  71. * Don't include tags for empty command lines; let the kernel
  72. * use its default command line.
  73. */
  74. if (*cmdline == '\0')
  75. return params;
  76. params->hdr.tag = ATAG_CMDLINE;
  77. params->hdr.size =
  78. (sizeof (struct tag_header) + strlen(cmdline) + 1 + 3) >> 2;
  79. strcpy(params->u.cmdline.cmdline, cmdline);
  80. return tag_next(params);
  81. }
  82. static struct tag *setup_ramdisk_tag(struct tag *params,
  83. unsigned long rd_start,
  84. unsigned long rd_end)
  85. {
  86. if (rd_start == rd_end)
  87. return params;
  88. params->hdr.tag = ATAG_RDIMG;
  89. params->hdr.size = tag_size(tag_mem_range);
  90. params->u.mem_range.addr = rd_start;
  91. params->u.mem_range.size = rd_end - rd_start;
  92. return tag_next(params);
  93. }
  94. static struct tag *setup_clock_tags(struct tag *params)
  95. {
  96. params->hdr.tag = ATAG_CLOCK;
  97. params->hdr.size = tag_size(tag_clock);
  98. params->u.clock.clock_id = ACLOCK_BOOTCPU;
  99. params->u.clock.clock_flags = 0;
  100. params->u.clock.clock_hz = gd->cpu_hz;
  101. #ifdef CONFIG_AT32AP7000
  102. /*
  103. * New kernels don't need this, but we should be backwards
  104. * compatible for a while...
  105. */
  106. params = tag_next(params);
  107. params->hdr.tag = ATAG_CLOCK;
  108. params->hdr.size = tag_size(tag_clock);
  109. params->u.clock.clock_id = ACLOCK_HSB;
  110. params->u.clock.clock_flags = 0;
  111. params->u.clock.clock_hz = pm_get_clock_freq(CLOCK_HSB);
  112. #endif
  113. return tag_next(params);
  114. }
  115. static struct tag *setup_ethernet_tag(struct tag *params,
  116. char *addr, int index)
  117. {
  118. char *s, *e;
  119. int i;
  120. params->hdr.tag = ATAG_ETHERNET;
  121. params->hdr.size = tag_size(tag_ethernet);
  122. params->u.ethernet.mac_index = index;
  123. params->u.ethernet.mii_phy_addr = gd->bd->bi_phy_id[index];
  124. s = addr;
  125. for (i = 0; i < 6; i++) {
  126. params->u.ethernet.hw_address[i] = simple_strtoul(s, &e, 16);
  127. s = e + 1;
  128. }
  129. return tag_next(params);
  130. }
  131. static struct tag *setup_ethernet_tags(struct tag *params)
  132. {
  133. char name[16] = "ethaddr";
  134. char *addr;
  135. int i = 0;
  136. do {
  137. addr = getenv(name);
  138. if (addr)
  139. params = setup_ethernet_tag(params, addr, i);
  140. sprintf(name, "eth%daddr", ++i);
  141. } while (i < 4);
  142. return params;
  143. }
  144. static void setup_end_tag(struct tag *params)
  145. {
  146. params->hdr.tag = ATAG_NONE;
  147. params->hdr.size = 0;
  148. }
  149. void do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  150. unsigned long addr, unsigned long *len_ptr, int verify)
  151. {
  152. unsigned long data, len = 0;
  153. unsigned long initrd_start, initrd_end;
  154. unsigned long image_start, image_end;
  155. unsigned long checksum;
  156. void (*theKernel)(int magic, void *tagtable);
  157. image_header_t *hdr;
  158. struct tag *params, *params_start;
  159. char *commandline = getenv("bootargs");
  160. hdr = (image_header_t *)addr;
  161. image_start = addr;
  162. image_end = addr + hdr->ih_size;
  163. theKernel = (void *)ntohl(hdr->ih_ep);
  164. /*
  165. * Check if there is an initrd image
  166. */
  167. if (argc >= 3) {
  168. SHOW_BOOT_PROGRESS(9);
  169. addr = simple_strtoul(argv[2], NULL, 16);
  170. printf("## Loading RAMDISK image at %08lx ...\n", addr);
  171. memcpy(&header, (char *)addr, sizeof(header));
  172. hdr = &header;
  173. if (ntohl(hdr->ih_magic) != IH_MAGIC) {
  174. puts("Bad Magic Number\n");
  175. SHOW_BOOT_PROGRESS(-10);
  176. do_reset(cmdtp, flag, argc, argv);
  177. }
  178. data = (unsigned long)hdr;
  179. len = sizeof(*hdr);
  180. checksum = ntohl(hdr->ih_hcrc);
  181. hdr->ih_hcrc = 0;
  182. if (crc32(0, (unsigned char *)data, len) != checksum) {
  183. puts("Bad Header Checksum\n");
  184. SHOW_BOOT_PROGRESS(-11);
  185. do_reset(cmdtp, flag, argc, argv);
  186. }
  187. SHOW_BOOT_PROGRESS(10);
  188. print_image_hdr(hdr);
  189. data = addr + sizeof(header);
  190. len = ntohl(hdr->ih_size);
  191. if (verify) {
  192. unsigned long csum = 0;
  193. puts(" Verifying Checksum ... ");
  194. csum = crc32(0, (unsigned char *)data, len);
  195. if (csum != ntohl(hdr->ih_dcrc)) {
  196. puts("Bad Data CRC\n");
  197. SHOW_BOOT_PROGRESS(-12);
  198. do_reset(cmdtp, flag, argc, argv);
  199. }
  200. puts("OK\n");
  201. }
  202. SHOW_BOOT_PROGRESS(11);
  203. if ((hdr->ih_os != IH_OS_LINUX) ||
  204. (hdr->ih_arch != IH_CPU_AVR32) ||
  205. (hdr->ih_type != IH_TYPE_RAMDISK)) {
  206. puts("Not a Linux/AVR32 RAMDISK image\n");
  207. SHOW_BOOT_PROGRESS(-13);
  208. do_reset(cmdtp, flag, argc, argv);
  209. }
  210. } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
  211. ulong tail = ntohl (len_ptr[0]) % 4;
  212. int i;
  213. SHOW_BOOT_PROGRESS (13);
  214. /* skip kernel length and terminator */
  215. data = (ulong) (&len_ptr[2]);
  216. /* skip any additional image length fields */
  217. for (i = 1; len_ptr[i]; ++i)
  218. data += 4;
  219. /* add kernel length, and align */
  220. data += ntohl (len_ptr[0]);
  221. if (tail) {
  222. data += 4 - tail;
  223. }
  224. len = ntohl (len_ptr[1]);
  225. } else {
  226. /* no initrd image */
  227. SHOW_BOOT_PROGRESS(14);
  228. len = data = 0;
  229. }
  230. if (data) {
  231. initrd_start = data;
  232. initrd_end = initrd_start + len;
  233. } else {
  234. initrd_start = 0;
  235. initrd_end = 0;
  236. }
  237. SHOW_BOOT_PROGRESS(15);
  238. params = params_start = (struct tag *)gd->bd->bi_boot_params;
  239. params = setup_start_tag(params);
  240. params = setup_memory_tags(params);
  241. if (initrd_start) {
  242. params = setup_ramdisk_tag(params,
  243. PHYSADDR(initrd_start),
  244. PHYSADDR(initrd_end));
  245. }
  246. params = setup_commandline_tag(params, commandline);
  247. params = setup_clock_tags(params);
  248. params = setup_ethernet_tags(params);
  249. setup_end_tag(params);
  250. printf("\nStarting kernel at %p (params at %p)...\n\n",
  251. theKernel, params_start);
  252. prepare_to_boot();
  253. theKernel(ATAG_MAGIC, params_start);
  254. }