misc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #ifdef CONFIG_ETRAX_ARCH_V32
  20. #include <hwregs/reg_rdwr.h>
  21. #include <hwregs/reg_map.h>
  22. #include <hwregs/ser_defs.h>
  23. #include <hwregs/pinmux_defs.h>
  24. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  25. #include <hwregs/clkgen_defs.h>
  26. #endif
  27. #else
  28. #include <arch/svinto.h>
  29. #endif
  30. /*
  31. * gzip declarations
  32. */
  33. #define OF(args) args
  34. #define STATIC static
  35. void *memset(void *s, int c, size_t n);
  36. void *memcpy(void *__dest, __const void *__src, size_t __n);
  37. #define memzero(s, n) memset((s), 0, (n))
  38. typedef unsigned char uch;
  39. typedef unsigned short ush;
  40. typedef unsigned long ulg;
  41. #define WSIZE 0x8000 /* Window size must be at least 32k, */
  42. /* and a power of two */
  43. static uch *inbuf; /* input buffer */
  44. static uch window[WSIZE]; /* Sliding window buffer */
  45. unsigned inptr = 0; /* index of next byte to be processed in inbuf
  46. * After decompression it will contain the
  47. * compressed size, and head.S will read it.
  48. */
  49. static unsigned outcnt = 0; /* bytes in output buffer */
  50. /* gzip flag byte */
  51. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  52. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  53. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  54. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  55. #define COMMENT 0x10 /* bit 4 set: file comment present */
  56. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  57. #define RESERVED 0xC0 /* bit 6,7: reserved */
  58. #define get_byte() (inbuf[inptr++])
  59. /* Diagnostic functions */
  60. #ifdef DEBUG
  61. # define Assert(cond, msg) do { \
  62. if (!(cond)) \
  63. error(msg); \
  64. } while (0)
  65. # define Trace(x) fprintf x
  66. # define Tracev(x) do { \
  67. if (verbose) \
  68. fprintf x; \
  69. } while (0)
  70. # define Tracevv(x) do { \
  71. if (verbose > 1) \
  72. fprintf x; \
  73. } while (0)
  74. # define Tracec(c, x) do { \
  75. if (verbose && (c)) \
  76. fprintf x; \
  77. } while (0)
  78. # define Tracecv(c, x) do { \
  79. if (verbose > 1 && (c)) \
  80. fprintf x; \
  81. } while (0)
  82. #else
  83. # define Assert(cond, msg)
  84. # define Trace(x)
  85. # define Tracev(x)
  86. # define Tracevv(x)
  87. # define Tracec(c, x)
  88. # define Tracecv(c, x)
  89. #endif
  90. static void flush_window(void);
  91. static void error(char *m);
  92. static void aputs(const char *s);
  93. extern char *input_data; /* lives in head.S */
  94. static long bytes_out;
  95. static uch *output_data;
  96. static unsigned long output_ptr;
  97. /* the "heap" is put directly after the BSS ends, at end */
  98. extern int _end;
  99. static long free_mem_ptr = (long)&_end;
  100. static long free_mem_end_ptr;
  101. #include "../../../../../lib/inflate.c"
  102. /* decompressor info and error messages to serial console */
  103. #ifdef CONFIG_ETRAX_ARCH_V32
  104. static inline void serout(const char *s, reg_scope_instances regi_ser)
  105. {
  106. reg_ser_rs_stat_din rs;
  107. reg_ser_rw_dout dout = {.data = *s};
  108. do {
  109. rs = REG_RD(ser, regi_ser, rs_stat_din);
  110. }
  111. while (!rs.tr_rdy);/* Wait for transceiver. */
  112. REG_WR(ser, regi_ser, rw_dout, dout);
  113. }
  114. #define SEROUT(S, N) \
  115. do { \
  116. serout(S, regi_ser ## N); \
  117. s++; \
  118. } while (0)
  119. #else
  120. #define SEROUT(S, N) do { \
  121. while (!(*R_SERIAL ## N ## _STATUS & (1 << 5))) \
  122. ; \
  123. *R_SERIAL ## N ## _TR_DATA = *s++; \
  124. } while (0)
  125. #endif
  126. static void aputs(const char *s)
  127. {
  128. #ifndef CONFIG_ETRAX_DEBUG_PORT_NULL
  129. while (*s) {
  130. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  131. SEROUT(s, 0);
  132. #endif
  133. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  134. SEROUT(s, 1);
  135. #endif
  136. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  137. SEROUT(s, 2);
  138. #endif
  139. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  140. SEROUT(s, 3);
  141. #endif
  142. }
  143. #endif /* CONFIG_ETRAX_DEBUG_PORT_NULL */
  144. }
  145. void *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. return s;
  151. }
  152. void *memcpy(void *__dest, __const void *__src, size_t __n)
  153. {
  154. int i;
  155. char *d = (char *)__dest, *s = (char *)__src;
  156. for (i = 0; i < __n; i++)
  157. d[i] = s[i];
  158. return __dest;
  159. }
  160. /* ===========================================================================
  161. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  162. * (Used for the decompressed data only.)
  163. */
  164. static void flush_window(void)
  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. out++;
  174. in++;
  175. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  176. }
  177. crc = c;
  178. bytes_out += (ulg)outcnt;
  179. output_ptr += (ulg)outcnt;
  180. outcnt = 0;
  181. }
  182. static void error(char *x)
  183. {
  184. aputs("\n\n");
  185. aputs(x);
  186. aputs("\n\n -- System halted\n");
  187. while(1); /* Halt */
  188. }
  189. void setup_normal_output_buffer(void)
  190. {
  191. output_data = (char *)KERNEL_LOAD_ADR;
  192. }
  193. #ifdef CONFIG_ETRAX_ARCH_V32
  194. static inline void 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. tr_ctrl.en = 1; /* enable transmitter */
  213. rec_ctrl.en = 1; /* enabler receiver */
  214. /*
  215. * The baudrate setup used to be a bit fishy, but now transmitter and
  216. * receiver are both set to the intended baud rate, 115200.
  217. * The magic value is 29.493 MHz.
  218. */
  219. tr_ctrl.base_freq = regk_ser_f29_493;
  220. rec_ctrl.base_freq = regk_ser_f29_493;
  221. tr_baud.div = (29493000 / 8) / 115200;
  222. rec_baud.div = (29493000 / 8) / 115200;
  223. REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  224. REG_WR(ser, regi_ser, rw_tr_baud_div, tr_baud);
  225. REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  226. REG_WR(ser, regi_ser, rw_rec_baud_div, rec_baud);
  227. }
  228. #endif
  229. void decompress_kernel(void)
  230. {
  231. char revision;
  232. char compile_rev;
  233. #ifdef CONFIG_ETRAX_ARCH_V32
  234. /* Need at least a CRISv32 to run. */
  235. compile_rev = 32;
  236. #if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
  237. defined(CONFIG_ETRAX_DEBUG_PORT2) || \
  238. defined(CONFIG_ETRAX_DEBUG_PORT3)
  239. reg_pinmux_rw_hwprot hwprot;
  240. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  241. reg_clkgen_rw_clk_ctrl clk_ctrl;
  242. /* Enable corresponding clock region when serial 1..3 selected */
  243. clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
  244. clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
  245. REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);
  246. #endif
  247. /* pinmux setup for ports 1..3 */
  248. hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
  249. #endif
  250. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  251. serial_setup(regi_ser0);
  252. #endif
  253. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  254. hwprot.ser1 = regk_pinmux_yes;
  255. serial_setup(regi_ser1);
  256. #endif
  257. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  258. hwprot.ser2 = regk_pinmux_yes;
  259. serial_setup(regi_ser2);
  260. #endif
  261. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  262. hwprot.ser3 = regk_pinmux_yes;
  263. serial_setup(regi_ser3);
  264. #endif
  265. #if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
  266. defined(CONFIG_ETRAX_DEBUG_PORT2) || \
  267. defined(CONFIG_ETRAX_DEBUG_PORT3)
  268. REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
  269. #endif
  270. /* input_data is set in head.S */
  271. inbuf = input_data;
  272. #else /* CRISv10 */
  273. /* Need at least a crisv10 to run. */
  274. compile_rev = 10;
  275. /* input_data is set in head.S */
  276. inbuf = input_data;
  277. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  278. *R_SERIAL0_XOFF = 0;
  279. *R_SERIAL0_BAUD = 0x99;
  280. *R_SERIAL0_TR_CTRL = 0x40;
  281. #endif
  282. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  283. *R_SERIAL1_XOFF = 0;
  284. *R_SERIAL1_BAUD = 0x99;
  285. *R_SERIAL1_TR_CTRL = 0x40;
  286. #endif
  287. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  288. *R_GEN_CONFIG = 0x08;
  289. *R_SERIAL2_XOFF = 0;
  290. *R_SERIAL2_BAUD = 0x99;
  291. *R_SERIAL2_TR_CTRL = 0x40;
  292. #endif
  293. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  294. *R_GEN_CONFIG = 0x100;
  295. *R_SERIAL3_XOFF = 0;
  296. *R_SERIAL3_BAUD = 0x99;
  297. *R_SERIAL3_TR_CTRL = 0x40;
  298. #endif
  299. #endif
  300. setup_normal_output_buffer();
  301. makecrc();
  302. __asm__ volatile ("move $vr,%0" : "=rm" (revision));
  303. if (revision < compile_rev) {
  304. #ifdef CONFIG_ETRAX_ARCH_V32
  305. aputs("You need at least ETRAX FS to run Linux 2.6/crisv32\n");
  306. #else
  307. aputs("You need an ETRAX 100LX to run linux 2.6/crisv10\n");
  308. #endif
  309. while(1);
  310. }
  311. aputs("Uncompressing Linux...\n");
  312. gunzip();
  313. aputs("Done. Now booting the kernel\n");
  314. }