avr32_linux.c 7.0 KB

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