avr32_linux.c 7.2 KB

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