misc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * misc.c
  3. *
  4. * This is a collection of several routines from gzip-1.0.3
  5. * adapted for Linux.
  6. *
  7. * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
  8. * puts by Nick Holloway 1993, better puts by Martin Mares 1995
  9. * adaptation for Linux/CRIS Axis Communications AB, 1999
  10. *
  11. */
  12. /* where the piggybacked kernel image expects itself to live.
  13. * it is the same address we use when we network load an uncompressed
  14. * image into DRAM, and it is the address the kernel is linked to live
  15. * at by vmlinux.lds.S
  16. */
  17. #define KERNEL_LOAD_ADR 0x40004000
  18. #include <linux/types.h>
  19. #include <hwregs/reg_rdwr.h>
  20. #include <hwregs/reg_map.h>
  21. #include <hwregs/ser_defs.h>
  22. #include <hwregs/pinmux_defs.h>
  23. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  24. #include <hwregs/clkgen_defs.h>
  25. #endif
  26. /*
  27. * gzip declarations
  28. */
  29. #define OF(args) args
  30. #define STATIC static
  31. void* memset(void* s, int c, size_t n);
  32. void* memcpy(void* __dest, __const void* __src,
  33. size_t __n);
  34. #define memzero(s, n) memset ((s), 0, (n))
  35. typedef unsigned char uch;
  36. typedef unsigned short ush;
  37. typedef unsigned long ulg;
  38. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  39. /* and a power of two */
  40. static uch *inbuf; /* input buffer */
  41. static uch window[WSIZE]; /* Sliding window buffer */
  42. unsigned inptr = 0; /* index of next byte to be processed in inbuf
  43. * After decompression it will contain the
  44. * compressed size, and head.S will read it.
  45. */
  46. static unsigned outcnt = 0; /* bytes in output buffer */
  47. /* gzip flag byte */
  48. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  49. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  50. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  51. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  52. #define COMMENT 0x10 /* bit 4 set: file comment present */
  53. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  54. #define RESERVED 0xC0 /* bit 6,7: reserved */
  55. #define get_byte() inbuf[inptr++]
  56. /* Diagnostic functions */
  57. #ifdef DEBUG
  58. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  59. # define Trace(x) fprintf x
  60. # define Tracev(x) {if (verbose) fprintf x ;}
  61. # define Tracevv(x) {if (verbose>1) fprintf x ;}
  62. # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
  63. # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
  64. #else
  65. # define Assert(cond,msg)
  66. # define Trace(x)
  67. # define Tracev(x)
  68. # define Tracevv(x)
  69. # define Tracec(c,x)
  70. # define Tracecv(c,x)
  71. #endif
  72. static void flush_window(void);
  73. static void error(char *m);
  74. extern char *input_data; /* lives in head.S */
  75. static long bytes_out;
  76. static uch *output_data;
  77. static unsigned long output_ptr;
  78. static void error(char *m);
  79. static void puts(const char *);
  80. /* the "heap" is put directly after the BSS ends, at end */
  81. extern int _end;
  82. static long free_mem_ptr = (long)&_end;
  83. static long free_mem_end_ptr;
  84. #include "../../../../../lib/inflate.c"
  85. /* decompressor info and error messages to serial console */
  86. static inline void
  87. serout(const char *s, reg_scope_instances regi_ser)
  88. {
  89. reg_ser_rs_stat_din rs;
  90. reg_ser_rw_dout dout = {.data = *s};
  91. do {
  92. rs = REG_RD(ser, regi_ser, rs_stat_din);
  93. }
  94. while (!rs.tr_rdy);/* Wait for transceiver. */
  95. REG_WR(ser, regi_ser, rw_dout, dout);
  96. }
  97. static void
  98. puts(const char *s)
  99. {
  100. #ifndef CONFIG_ETRAX_DEBUG_PORT_NULL
  101. while (*s) {
  102. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  103. serout(s, regi_ser0);
  104. #endif
  105. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  106. serout(s, regi_ser1);
  107. #endif
  108. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  109. serout(s, regi_ser2);
  110. #endif
  111. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  112. serout(s, regi_ser3);
  113. #endif
  114. *s++;
  115. }
  116. /* CONFIG_ETRAX_DEBUG_PORT_NULL */
  117. #endif
  118. }
  119. void*
  120. memset(void* s, int c, size_t n)
  121. {
  122. int i;
  123. char *ss = (char*)s;
  124. for (i=0;i<n;i++) ss[i] = c;
  125. return s;
  126. }
  127. void*
  128. memcpy(void* __dest, __const void* __src,
  129. size_t __n)
  130. {
  131. int i;
  132. char *d = (char *)__dest, *s = (char *)__src;
  133. for (i=0;i<__n;i++) d[i] = s[i];
  134. return __dest;
  135. }
  136. /* ===========================================================================
  137. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  138. * (Used for the decompressed data only.)
  139. */
  140. static void
  141. flush_window()
  142. {
  143. ulg c = crc; /* temporary variable */
  144. unsigned n;
  145. uch *in, *out, ch;
  146. in = window;
  147. out = &output_data[output_ptr];
  148. for (n = 0; n < outcnt; n++) {
  149. ch = *out++ = *in++;
  150. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  151. }
  152. crc = c;
  153. bytes_out += (ulg)outcnt;
  154. output_ptr += (ulg)outcnt;
  155. outcnt = 0;
  156. }
  157. static void
  158. error(char *x)
  159. {
  160. puts("\r\n\n");
  161. puts(x);
  162. puts("\r\n\n -- System halted\n");
  163. while(1); /* Halt */
  164. }
  165. void
  166. setup_normal_output_buffer(void)
  167. {
  168. output_data = (char *)KERNEL_LOAD_ADR;
  169. }
  170. static inline void
  171. serial_setup(reg_scope_instances regi_ser)
  172. {
  173. reg_ser_rw_xoff xoff;
  174. reg_ser_rw_tr_ctrl tr_ctrl;
  175. reg_ser_rw_rec_ctrl rec_ctrl;
  176. reg_ser_rw_tr_baud_div tr_baud;
  177. reg_ser_rw_rec_baud_div rec_baud;
  178. /* Turn off XOFF. */
  179. xoff = REG_RD(ser, regi_ser, rw_xoff);
  180. xoff.chr = 0;
  181. xoff.automatic = regk_ser_no;
  182. REG_WR(ser, regi_ser, rw_xoff, xoff);
  183. /* Set baudrate and stopbits. */
  184. tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  185. rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
  186. tr_baud = REG_RD(ser, regi_ser, rw_tr_baud_div);
  187. rec_baud = REG_RD(ser, regi_ser, rw_rec_baud_div);
  188. tr_ctrl.stop_bits = 1; /* 2 stop bits. */
  189. tr_ctrl.en = 1; /* enable transmitter */
  190. rec_ctrl.en = 1; /* enabler receiver */
  191. /*
  192. * The baudrate setup used to be a bit fishy, but now transmitter and
  193. * receiver are both set to the intended baud rate, 115200.
  194. * The magic value is 29.493 MHz.
  195. */
  196. tr_ctrl.base_freq = regk_ser_f29_493;
  197. rec_ctrl.base_freq = regk_ser_f29_493;
  198. tr_baud.div = (29493000 / 8) / 115200;
  199. rec_baud.div = (29493000 / 8) / 115200;
  200. REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  201. REG_WR(ser, regi_ser, rw_tr_baud_div, tr_baud);
  202. REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  203. REG_WR(ser, regi_ser, rw_rec_baud_div, rec_baud);
  204. }
  205. void
  206. decompress_kernel(void)
  207. {
  208. char revision;
  209. #if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
  210. defined(CONFIG_ETRAX_DEBUG_PORT2) || \
  211. defined(CONFIG_ETRAX_DEBUG_PORT3)
  212. reg_pinmux_rw_hwprot hwprot;
  213. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  214. reg_clkgen_rw_clk_ctrl clk_ctrl;
  215. /* Enable corresponding clock region when serial 1..3 selected */
  216. clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
  217. clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
  218. REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);
  219. #endif
  220. /* pinmux setup for ports 1..3 */
  221. hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
  222. #endif
  223. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  224. serial_setup(regi_ser0);
  225. #endif
  226. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  227. hwprot.ser1 = regk_pinmux_yes;
  228. serial_setup(regi_ser1);
  229. #endif
  230. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  231. hwprot.ser2 = regk_pinmux_yes;
  232. serial_setup(regi_ser2);
  233. #endif
  234. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  235. hwprot.ser3 = regk_pinmux_yes;
  236. serial_setup(regi_ser3);
  237. #endif
  238. #if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
  239. defined(CONFIG_ETRAX_DEBUG_PORT2) || \
  240. defined(CONFIG_ETRAX_DEBUG_PORT3)
  241. REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
  242. #endif
  243. /* input_data is set in head.S */
  244. inbuf = input_data;
  245. setup_normal_output_buffer();
  246. makecrc();
  247. __asm__ volatile ("move $vr,%0" : "=rm" (revision));
  248. if (revision < 32)
  249. {
  250. puts("You need an ETRAX FS to run Linux 2.6/crisv32.\r\n");
  251. while(1);
  252. }
  253. puts("Uncompressing Linux...\r\n");
  254. gunzip();
  255. puts("Done. Now booting the kernel.\r\n");
  256. }