misc.c 7.0 KB

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