4 * This is a collection of several routines from gzip-1.0.3
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
13 /* where the piggybacked kernel image expects itself to live.
14 * it is the same address we use when we network load an uncompressed
15 * image into DRAM, and it is the address the kernel is linked to live
19 #define KERNEL_LOAD_ADR 0x40004000
22 #include <linux/types.h>
23 #include <hwregs/reg_rdwr.h>
24 #include <hwregs/reg_map.h>
25 #include <hwregs/ser_defs.h>
26 #include <hwregs/pinmux_defs.h>
27 #ifdef CONFIG_CRIS_MACH_ARTPEC3
28 #include <hwregs/clkgen_defs.h>
38 void* memset(void* s, int c, size_t n);
39 void* memcpy(void* __dest, __const void* __src,
42 #define memzero(s, n) memset ((s), 0, (n))
45 typedef unsigned char uch;
46 typedef unsigned short ush;
47 typedef unsigned long ulg;
49 #define WSIZE 0x8000 /* Window size must be at least 32k, */
50 /* and a power of two */
52 static uch *inbuf; /* input buffer */
53 static uch window[WSIZE]; /* Sliding window buffer */
55 unsigned inptr = 0; /* index of next byte to be processed in inbuf
56 * After decompression it will contain the
57 * compressed size, and head.S will read it.
60 static unsigned outcnt = 0; /* bytes in output buffer */
63 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
64 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
65 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
66 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
67 #define COMMENT 0x10 /* bit 4 set: file comment present */
68 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
69 #define RESERVED 0xC0 /* bit 6,7: reserved */
71 #define get_byte() inbuf[inptr++]
73 /* Diagnostic functions */
75 # define Assert(cond,msg) {if(!(cond)) error(msg);}
76 # define Trace(x) fprintf x
77 # define Tracev(x) {if (verbose) fprintf x ;}
78 # define Tracevv(x) {if (verbose>1) fprintf x ;}
79 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
80 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
82 # define Assert(cond,msg)
90 static void flush_window(void);
91 static void error(char *m);
92 static void gzip_mark(void **);
93 static void gzip_release(void **);
95 extern char *input_data; /* lives in head.S */
97 static long bytes_out = 0;
98 static uch *output_data;
99 static unsigned long output_ptr = 0;
101 static void *malloc(int size);
102 static void free(void *where);
103 static void error(char *m);
104 static void gzip_mark(void **);
105 static void gzip_release(void **);
107 static void puts(const char *);
109 /* the "heap" is put directly after the BSS ends, at end */
112 static long free_mem_ptr = (long)&_end;
114 #include "../../../../../lib/inflate.c"
116 static void *malloc(int size)
120 if (size <0) error("Malloc error");
122 free_mem_ptr = (free_mem_ptr + 3) & ~3; /* Align */
124 p = (void *)free_mem_ptr;
125 free_mem_ptr += size;
130 static void free(void *where)
134 static void gzip_mark(void **ptr)
136 *ptr = (void *) free_mem_ptr;
139 static void gzip_release(void **ptr)
141 free_mem_ptr = (long) *ptr;
144 /* decompressor info and error messages to serial console */
147 serout(const char *s, reg_scope_instances regi_ser)
149 reg_ser_rs_stat_din rs;
150 reg_ser_rw_dout dout = {.data = *s};
153 rs = REG_RD(ser, regi_ser, rs_stat_din);
155 while (!rs.tr_rdy);/* Wait for transceiver. */
157 REG_WR(ser, regi_ser, rw_dout, dout);
163 #ifndef CONFIG_ETRAX_DEBUG_PORT_NULL
165 #ifdef CONFIG_ETRAX_DEBUG_PORT0
166 serout(s, regi_ser0);
168 #ifdef CONFIG_ETRAX_DEBUG_PORT1
169 serout(s, regi_ser1);
171 #ifdef CONFIG_ETRAX_DEBUG_PORT2
172 serout(s, regi_ser2);
174 #ifdef CONFIG_ETRAX_DEBUG_PORT3
175 serout(s, regi_ser3);
179 /* CONFIG_ETRAX_DEBUG_PORT_NULL */
184 memset(void* s, int c, size_t n)
189 for (i=0;i<n;i++) ss[i] = c;
195 memcpy(void* __dest, __const void* __src,
199 char *d = (char *)__dest, *s = (char *)__src;
201 for (i=0;i<__n;i++) d[i] = s[i];
206 /* ===========================================================================
207 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
208 * (Used for the decompressed data only.)
214 ulg c = crc; /* temporary variable */
219 out = &output_data[output_ptr];
220 for (n = 0; n < outcnt; n++) {
222 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
225 bytes_out += (ulg)outcnt;
226 output_ptr += (ulg)outcnt;
235 puts("\r\n\n -- System halted\n");
241 setup_normal_output_buffer(void)
243 output_data = (char *)KERNEL_LOAD_ADR;
247 serial_setup(reg_scope_instances regi_ser)
249 reg_ser_rw_xoff xoff;
250 reg_ser_rw_tr_ctrl tr_ctrl;
251 reg_ser_rw_rec_ctrl rec_ctrl;
252 reg_ser_rw_tr_baud_div tr_baud;
253 reg_ser_rw_rec_baud_div rec_baud;
256 xoff = REG_RD(ser, regi_ser, rw_xoff);
259 xoff.automatic = regk_ser_no;
261 REG_WR(ser, regi_ser, rw_xoff, xoff);
263 /* Set baudrate and stopbits. */
264 tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
265 rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
266 tr_baud = REG_RD(ser, regi_ser, rw_tr_baud_div);
267 rec_baud = REG_RD(ser, regi_ser, rw_rec_baud_div);
269 tr_ctrl.stop_bits = 1; /* 2 stop bits. */
270 tr_ctrl.en = 1; /* enable transmitter */
271 rec_ctrl.en = 1; /* enabler receiver */
274 * The baudrate setup used to be a bit fishy, but now transmitter and
275 * receiver are both set to the intended baud rate, 115200.
276 * The magic value is 29.493 MHz.
278 tr_ctrl.base_freq = regk_ser_f29_493;
279 rec_ctrl.base_freq = regk_ser_f29_493;
280 tr_baud.div = (29493000 / 8) / 115200;
281 rec_baud.div = (29493000 / 8) / 115200;
283 REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
284 REG_WR(ser, regi_ser, rw_tr_baud_div, tr_baud);
285 REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
286 REG_WR(ser, regi_ser, rw_rec_baud_div, rec_baud);
290 decompress_kernel(void)
294 #if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
295 defined(CONFIG_ETRAX_DEBUG_PORT2) || \
296 defined(CONFIG_ETRAX_DEBUG_PORT3)
297 reg_pinmux_rw_hwprot hwprot;
299 #ifdef CONFIG_CRIS_MACH_ARTPEC3
300 reg_clkgen_rw_clk_ctrl clk_ctrl;
302 /* Enable corresponding clock region when serial 1..3 selected */
304 clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
305 clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
306 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);
309 /* pinmux setup for ports 1..3 */
310 hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
313 #ifdef CONFIG_ETRAX_DEBUG_PORT0
314 serial_setup(regi_ser0);
316 #ifdef CONFIG_ETRAX_DEBUG_PORT1
317 hwprot.ser1 = regk_pinmux_yes;
318 serial_setup(regi_ser1);
320 #ifdef CONFIG_ETRAX_DEBUG_PORT2
321 hwprot.ser2 = regk_pinmux_yes;
322 serial_setup(regi_ser2);
324 #ifdef CONFIG_ETRAX_DEBUG_PORT3
325 hwprot.ser3 = regk_pinmux_yes;
326 serial_setup(regi_ser3);
328 #if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
329 defined(CONFIG_ETRAX_DEBUG_PORT2) || \
330 defined(CONFIG_ETRAX_DEBUG_PORT3)
331 REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
334 /* input_data is set in head.S */
337 setup_normal_output_buffer();
341 __asm__ volatile ("move $vr,%0" : "=rm" (revision));
344 puts("You need an ETRAX FS to run Linux 2.6/crisv32.\r\n");
348 puts("Uncompressing Linux...\r\n");
350 puts("Done. Now booting the kernel.\r\n");