misc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 puts(const char *);
  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. #endif
  115. static void puts(const char *s)
  116. {
  117. #ifndef CONFIG_ETRAX_DEBUG_PORT_NULL
  118. while (*s) {
  119. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  120. #ifdef CONFIG_ETRAX_ARCH_V32
  121. serout(s, regi_ser0);
  122. #else
  123. while (!(*R_SERIAL0_STATUS & (1 << 5)))
  124. ;
  125. *R_SERIAL0_TR_DATA = *s++;
  126. #endif
  127. #endif
  128. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  129. #ifdef CONFIG_ETRAX_ARCH_V32
  130. serout(s, regi_ser1);
  131. #else
  132. while (!(*R_SERIAL1_STATUS & (1 << 5)))
  133. ;
  134. *R_SERIAL1_TR_DATA = *s++;
  135. #endif
  136. #endif
  137. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  138. #ifdef CONFIG_ETRAX_ARCH_V32
  139. serout(s, regi_ser2);
  140. #else
  141. while (!(*R_SERIAL2_STATUS & (1 << 5)))
  142. ;
  143. *R_SERIAL2_TR_DATA = *s++;
  144. #endif
  145. #endif
  146. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  147. #ifdef CONFIG_ETRAX_ARCH_V32
  148. serout(s, regi_ser3);
  149. #else
  150. while (!(*R_SERIAL3_STATUS & (1 << 5)))
  151. ;
  152. *R_SERIAL3_TR_DATA = *s++;
  153. #endif
  154. #endif
  155. *s++;
  156. }
  157. /* CONFIG_ETRAX_DEBUG_PORT_NULL */
  158. #endif
  159. }
  160. void *memset(void *s, int c, size_t n)
  161. {
  162. int i;
  163. char *ss = (char*)s;
  164. for (i=0;i<n;i++) ss[i] = c;
  165. return s;
  166. }
  167. void *memcpy(void *__dest, __const void *__src, size_t __n)
  168. {
  169. int i;
  170. char *d = (char *)__dest, *s = (char *)__src;
  171. for (i = 0; i < __n; i++)
  172. d[i] = s[i];
  173. return __dest;
  174. }
  175. /* ===========================================================================
  176. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  177. * (Used for the decompressed data only.)
  178. */
  179. static void flush_window(void)
  180. {
  181. ulg c = crc; /* temporary variable */
  182. unsigned n;
  183. uch *in, *out, ch;
  184. in = window;
  185. out = &output_data[output_ptr];
  186. for (n = 0; n < outcnt; n++) {
  187. ch = *out = *in;
  188. out++;
  189. in++;
  190. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  191. }
  192. crc = c;
  193. bytes_out += (ulg)outcnt;
  194. output_ptr += (ulg)outcnt;
  195. outcnt = 0;
  196. }
  197. static void error(char *x)
  198. {
  199. puts("\n\n");
  200. puts(x);
  201. puts("\n\n -- System halted\n");
  202. while(1); /* Halt */
  203. }
  204. void setup_normal_output_buffer(void)
  205. {
  206. output_data = (char *)KERNEL_LOAD_ADR;
  207. }
  208. #ifdef CONFIG_ETRAX_ARCH_V32
  209. static inline void serial_setup(reg_scope_instances regi_ser)
  210. {
  211. reg_ser_rw_xoff xoff;
  212. reg_ser_rw_tr_ctrl tr_ctrl;
  213. reg_ser_rw_rec_ctrl rec_ctrl;
  214. reg_ser_rw_tr_baud_div tr_baud;
  215. reg_ser_rw_rec_baud_div rec_baud;
  216. /* Turn off XOFF. */
  217. xoff = REG_RD(ser, regi_ser, rw_xoff);
  218. xoff.chr = 0;
  219. xoff.automatic = regk_ser_no;
  220. REG_WR(ser, regi_ser, rw_xoff, xoff);
  221. /* Set baudrate and stopbits. */
  222. tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  223. rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
  224. tr_baud = REG_RD(ser, regi_ser, rw_tr_baud_div);
  225. rec_baud = REG_RD(ser, regi_ser, rw_rec_baud_div);
  226. tr_ctrl.stop_bits = 1; /* 2 stop bits. */
  227. tr_ctrl.en = 1; /* enable transmitter */
  228. rec_ctrl.en = 1; /* enabler receiver */
  229. /*
  230. * The baudrate setup used to be a bit fishy, but now transmitter and
  231. * receiver are both set to the intended baud rate, 115200.
  232. * The magic value is 29.493 MHz.
  233. */
  234. tr_ctrl.base_freq = regk_ser_f29_493;
  235. rec_ctrl.base_freq = regk_ser_f29_493;
  236. tr_baud.div = (29493000 / 8) / 115200;
  237. rec_baud.div = (29493000 / 8) / 115200;
  238. REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  239. REG_WR(ser, regi_ser, rw_tr_baud_div, tr_baud);
  240. REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  241. REG_WR(ser, regi_ser, rw_rec_baud_div, rec_baud);
  242. }
  243. #endif
  244. void decompress_kernel(void)
  245. {
  246. char revision;
  247. char compile_rev;
  248. #ifdef CONFIG_ETRAX_ARCH_V32
  249. /* Need at least a CRISv32 to run. */
  250. compile_rev = 32;
  251. #if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
  252. defined(CONFIG_ETRAX_DEBUG_PORT2) || \
  253. defined(CONFIG_ETRAX_DEBUG_PORT3)
  254. reg_pinmux_rw_hwprot hwprot;
  255. #ifdef CONFIG_CRIS_MACH_ARTPEC3
  256. reg_clkgen_rw_clk_ctrl clk_ctrl;
  257. /* Enable corresponding clock region when serial 1..3 selected */
  258. clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
  259. clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
  260. REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);
  261. #endif
  262. /* pinmux setup for ports 1..3 */
  263. hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
  264. #endif
  265. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  266. serial_setup(regi_ser0);
  267. #endif
  268. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  269. hwprot.ser1 = regk_pinmux_yes;
  270. serial_setup(regi_ser1);
  271. #endif
  272. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  273. hwprot.ser2 = regk_pinmux_yes;
  274. serial_setup(regi_ser2);
  275. #endif
  276. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  277. hwprot.ser3 = regk_pinmux_yes;
  278. serial_setup(regi_ser3);
  279. #endif
  280. #if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
  281. defined(CONFIG_ETRAX_DEBUG_PORT2) || \
  282. defined(CONFIG_ETRAX_DEBUG_PORT3)
  283. REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
  284. #endif
  285. /* input_data is set in head.S */
  286. inbuf = input_data;
  287. #else /* CRISv10 */
  288. /* Need at least a crisv10 to run. */
  289. compile_rev = 10;
  290. /* input_data is set in head.S */
  291. inbuf = input_data;
  292. #ifdef CONFIG_ETRAX_DEBUG_PORT0
  293. *R_SERIAL0_XOFF = 0;
  294. *R_SERIAL0_BAUD = 0x99;
  295. *R_SERIAL0_TR_CTRL = 0x40;
  296. #endif
  297. #ifdef CONFIG_ETRAX_DEBUG_PORT1
  298. *R_SERIAL1_XOFF = 0;
  299. *R_SERIAL1_BAUD = 0x99;
  300. *R_SERIAL1_TR_CTRL = 0x40;
  301. #endif
  302. #ifdef CONFIG_ETRAX_DEBUG_PORT2
  303. *R_GEN_CONFIG = 0x08;
  304. *R_SERIAL2_XOFF = 0;
  305. *R_SERIAL2_BAUD = 0x99;
  306. *R_SERIAL2_TR_CTRL = 0x40;
  307. #endif
  308. #ifdef CONFIG_ETRAX_DEBUG_PORT3
  309. *R_GEN_CONFIG = 0x100;
  310. *R_SERIAL3_XOFF = 0;
  311. *R_SERIAL3_BAUD = 0x99;
  312. *R_SERIAL3_TR_CTRL = 0x40;
  313. #endif
  314. #endif
  315. setup_normal_output_buffer();
  316. makecrc();
  317. __asm__ volatile ("move $vr,%0" : "=rm" (revision));
  318. if (revision < compile_rev) {
  319. #ifdef CONFIG_ETRAX_ARCH_V32
  320. puts("You need an ETRAX FS to run Linux 2.6/crisv32\n");
  321. #else
  322. puts("You need an ETRAX 100LX to run linux 2.6\n");
  323. #endif
  324. while(1);
  325. }
  326. puts("Uncompressing Linux...\n");
  327. gunzip();
  328. puts("Done. Now booting the kernel\n");
  329. }