misc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Misc. bootloader code for many machines. This assumes you have are using
  3. * a 6xx/7xx/74xx CPU in your machine. This assumes the chunk of memory
  4. * below 8MB is free. Finally, it assumes you have a NS16550-style uart for
  5. * your serial console. If a machine meets these requirements, it can quite
  6. * likely use this code during boot.
  7. *
  8. * Author: Matt Porter <mporter@mvista.com>
  9. * Derived from arch/ppc/boot/prep/misc.c
  10. *
  11. * 2001 (c) MontaVista, Software, Inc. This file is licensed under
  12. * the terms of the GNU General Public License version 2. This program
  13. * is licensed "as is" without any warranty of any kind, whether express
  14. * or implied.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/string.h>
  18. #include <asm/page.h>
  19. #include <asm/mmu.h>
  20. #include <asm/bootinfo.h>
  21. #ifdef CONFIG_4xx
  22. #include <asm/ibm4xx.h>
  23. #endif
  24. #include <asm/reg.h>
  25. #include "nonstdio.h"
  26. /* Default cmdline */
  27. #ifdef CONFIG_CMDLINE
  28. #define CMDLINE CONFIG_CMDLINE
  29. #else
  30. #define CMDLINE ""
  31. #endif
  32. /* Keyboard (and VGA console)? */
  33. #ifdef CONFIG_VGA_CONSOLE
  34. #define HAS_KEYB 1
  35. #else
  36. #define HAS_KEYB 0
  37. #endif
  38. /* Will / Can the user give input?
  39. * Val Henson has requested that Gemini doesn't wait for the
  40. * user to edit the cmdline or not.
  41. */
  42. #if (defined(CONFIG_SERIAL_8250_CONSOLE) \
  43. || defined(CONFIG_VGA_CONSOLE) \
  44. || defined(CONFIG_SERIAL_MPC52xx_CONSOLE) \
  45. || defined(CONFIG_SERIAL_MPSC_CONSOLE)) \
  46. && !defined(CONFIG_GEMINI)
  47. #define INTERACTIVE_CONSOLE 1
  48. #endif
  49. char *avail_ram;
  50. char *end_avail;
  51. char *zimage_start;
  52. char cmd_preset[] = CMDLINE;
  53. char cmd_buf[256];
  54. char *cmd_line = cmd_buf;
  55. int keyb_present = HAS_KEYB;
  56. int zimage_size;
  57. unsigned long com_port;
  58. unsigned long initrd_size = 0;
  59. /* The linker tells us various locations in the image */
  60. extern char __image_begin, __image_end;
  61. extern char __ramdisk_begin, __ramdisk_end;
  62. extern char _end[];
  63. /* Original location */
  64. extern unsigned long start;
  65. extern int CRT_tstc(void);
  66. extern unsigned long serial_init(int chan, void *ignored);
  67. extern void serial_close(unsigned long com_port);
  68. extern void gunzip(void *, int, unsigned char *, int *);
  69. extern void serial_fixups(void);
  70. /* Allow get_mem_size to be hooked into. This is the default. */
  71. unsigned long __attribute__ ((weak))
  72. get_mem_size(void)
  73. {
  74. return 0;
  75. }
  76. #if defined(CONFIG_40x)
  77. #define PPC4xx_EMAC0_MR0 EMAC0_BASE
  78. #endif
  79. #if defined(CONFIG_44x) && defined(PPC44x_EMAC0_MR0)
  80. #define PPC4xx_EMAC0_MR0 PPC44x_EMAC0_MR0
  81. #endif
  82. struct bi_record *
  83. decompress_kernel(unsigned long load_addr, int num_words, unsigned long cksum)
  84. {
  85. #ifdef INTERACTIVE_CONSOLE
  86. int timer = 0;
  87. char ch;
  88. #endif
  89. char *cp;
  90. struct bi_record *rec;
  91. unsigned long initrd_loc = 0, TotalMemory = 0;
  92. #if defined(CONFIG_SERIAL_8250_CONSOLE) || defined(CONFIG_SERIAL_MPSC_CONSOLE)
  93. com_port = serial_init(0, NULL);
  94. #endif
  95. #if defined(PPC4xx_EMAC0_MR0)
  96. /* Reset MAL */
  97. mtdcr(DCRN_MALCR(DCRN_MAL_BASE), MALCR_MMSR);
  98. /* Wait for reset */
  99. while (mfdcr(DCRN_MALCR(DCRN_MAL_BASE)) & MALCR_MMSR) {};
  100. /* Reset EMAC */
  101. *(volatile unsigned long *)PPC4xx_EMAC0_MR0 = 0x20000000;
  102. __asm__ __volatile__("eieio");
  103. #endif
  104. /*
  105. * Call get_mem_size(), which is memory controller dependent,
  106. * and we must have the correct file linked in here.
  107. */
  108. TotalMemory = get_mem_size();
  109. /* assume the chunk below 8M is free */
  110. end_avail = (char *)0x00800000;
  111. /*
  112. * Reveal where we were loaded at and where we
  113. * were relocated to.
  114. */
  115. puts("loaded at: "); puthex(load_addr);
  116. puts(" "); puthex((unsigned long)(load_addr + (4*num_words)));
  117. puts("\n");
  118. if ( (unsigned long)load_addr != (unsigned long)&start )
  119. {
  120. puts("relocated to: "); puthex((unsigned long)&start);
  121. puts(" ");
  122. puthex((unsigned long)((unsigned long)&start + (4*num_words)));
  123. puts("\n");
  124. }
  125. /*
  126. * We link ourself to 0x00800000. When we run, we relocate
  127. * ourselves there. So we just need __image_begin for the
  128. * start. -- Tom
  129. */
  130. zimage_start = (char *)(unsigned long)(&__image_begin);
  131. zimage_size = (unsigned long)(&__image_end) -
  132. (unsigned long)(&__image_begin);
  133. initrd_size = (unsigned long)(&__ramdisk_end) -
  134. (unsigned long)(&__ramdisk_begin);
  135. /*
  136. * The zImage and initrd will be between start and _end, so they've
  137. * already been moved once. We're good to go now. -- Tom
  138. */
  139. avail_ram = (char *)PAGE_ALIGN((unsigned long)_end);
  140. puts("zimage at: "); puthex((unsigned long)zimage_start);
  141. puts(" "); puthex((unsigned long)(zimage_size+zimage_start));
  142. puts("\n");
  143. if ( initrd_size ) {
  144. puts("initrd at: ");
  145. puthex((unsigned long)(&__ramdisk_begin));
  146. puts(" "); puthex((unsigned long)(&__ramdisk_end));puts("\n");
  147. }
  148. #ifndef CONFIG_40x /* don't overwrite the 40x image located at 0x00400000! */
  149. avail_ram = (char *)0x00400000;
  150. #endif
  151. end_avail = (char *)0x00800000;
  152. puts("avail ram: "); puthex((unsigned long)avail_ram); puts(" ");
  153. puthex((unsigned long)end_avail); puts("\n");
  154. if (keyb_present)
  155. CRT_tstc(); /* Forces keyboard to be initialized */
  156. #ifdef CONFIG_GEMINI
  157. /*
  158. * If cmd_line is empty and cmd_preset is not, copy cmd_preset
  159. * to cmd_line. This way we can override cmd_preset with the
  160. * command line from Smon.
  161. */
  162. if ( (cmd_line[0] == '\0') && (cmd_preset[0] != '\0'))
  163. memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
  164. #endif
  165. /* Display standard Linux/PPC boot prompt for kernel args */
  166. puts("\nLinux/PPC load: ");
  167. cp = cmd_line;
  168. memcpy (cmd_line, cmd_preset, sizeof(cmd_preset));
  169. while ( *cp ) putc(*cp++);
  170. #ifdef INTERACTIVE_CONSOLE
  171. /*
  172. * If they have a console, allow them to edit the command line.
  173. * Otherwise, don't bother wasting the five seconds.
  174. */
  175. while (timer++ < 5*1000) {
  176. if (tstc()) {
  177. while ((ch = getc()) != '\n' && ch != '\r') {
  178. /* Test for backspace/delete */
  179. if (ch == '\b' || ch == '\177') {
  180. if (cp != cmd_line) {
  181. cp--;
  182. puts("\b \b");
  183. }
  184. /* Test for ^x/^u (and wipe the line) */
  185. } else if (ch == '\030' || ch == '\025') {
  186. while (cp != cmd_line) {
  187. cp--;
  188. puts("\b \b");
  189. }
  190. } else {
  191. *cp++ = ch;
  192. putc(ch);
  193. }
  194. }
  195. break; /* Exit 'timer' loop */
  196. }
  197. udelay(1000); /* 1 msec */
  198. }
  199. *cp = 0;
  200. #endif
  201. puts("\n");
  202. puts("Uncompressing Linux...");
  203. gunzip(NULL, 0x400000, zimage_start, &zimage_size);
  204. puts("done.\n");
  205. /* get the bi_rec address */
  206. rec = bootinfo_addr(zimage_size);
  207. /* We need to make sure that the initrd and bi_recs do not
  208. * overlap. */
  209. if ( initrd_size ) {
  210. unsigned long rec_loc = (unsigned long) rec;
  211. initrd_loc = (unsigned long)(&__ramdisk_begin);
  212. /* If the bi_recs are in the middle of the current
  213. * initrd, move the initrd to the next MB
  214. * boundary. */
  215. if ((rec_loc > initrd_loc) &&
  216. ((initrd_loc + initrd_size) > rec_loc)) {
  217. initrd_loc = _ALIGN((unsigned long)(zimage_size)
  218. + (2 << 20) - 1, (2 << 20));
  219. memmove((void *)initrd_loc, &__ramdisk_begin,
  220. initrd_size);
  221. puts("initrd moved: "); puthex(initrd_loc);
  222. puts(" "); puthex(initrd_loc + initrd_size);
  223. puts("\n");
  224. }
  225. }
  226. bootinfo_init(rec);
  227. if ( TotalMemory )
  228. bootinfo_append(BI_MEMSIZE, sizeof(int), (void*)&TotalMemory);
  229. bootinfo_append(BI_CMD_LINE, strlen(cmd_line)+1, (void*)cmd_line);
  230. /* add a bi_rec for the initrd if it exists */
  231. if (initrd_size) {
  232. unsigned long initrd[2];
  233. initrd[0] = initrd_loc;
  234. initrd[1] = initrd_size;
  235. bootinfo_append(BI_INITRD, sizeof(initrd), &initrd);
  236. }
  237. puts("Now booting the kernel\n");
  238. serial_close(com_port);
  239. return rec;
  240. }
  241. void __attribute__ ((weak))
  242. board_isa_init(void)
  243. {
  244. }
  245. /* Allow decompress_kernel to be hooked into. This is the default. */
  246. void * __attribute__ ((weak))
  247. load_kernel(unsigned long load_addr, int num_words, unsigned long cksum,
  248. void *ign1, void *ign2)
  249. {
  250. board_isa_init();
  251. return decompress_kernel(load_addr, num_words, cksum);
  252. }