misc.c 7.0 KB

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