blob: 47bc190ba6d4ba6d4bae253b696a838b3431bb90 [file] [log] [blame]
Mikael Starvik51533b62005-07-27 11:44:44 -07001/*
2 * misc.c
3 *
Mikael Starvik51533b62005-07-27 11:44:44 -07004 * 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
Simon Arlott49b4ff32007-10-20 01:08:50 +02009 * adaptation for Linux/CRIS Axis Communications AB, 1999
Mikael Starvik51533b62005-07-27 11:44:44 -070010 *
11 */
12
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
16 * at by vmlinux.lds.S
17 */
18
19#define KERNEL_LOAD_ADR 0x40004000
20
Mikael Starvik51533b62005-07-27 11:44:44 -070021#include <linux/types.h>
Jesper Nilsson66ab3a72009-04-21 11:44:57 +020022
23#ifdef CONFIG_ETRAX_ARCH_V32
Jesper Nilssona5d204b2007-11-30 17:16:09 +010024#include <hwregs/reg_rdwr.h>
25#include <hwregs/reg_map.h>
26#include <hwregs/ser_defs.h>
27#include <hwregs/pinmux_defs.h>
28#ifdef CONFIG_CRIS_MACH_ARTPEC3
29#include <hwregs/clkgen_defs.h>
30#endif
Jesper Nilsson66ab3a72009-04-21 11:44:57 +020031#else
32#include <arch/svinto.h>
33#endif
Mikael Starvik51533b62005-07-27 11:44:44 -070034
35/*
36 * gzip declarations
37 */
38
39#define OF(args) args
40#define STATIC static
41
Jesper Nilsson66ab3a72009-04-21 11:44:57 +020042void *memset(void *s, int c, size_t n);
43void *memcpy(void *__dest, __const void *__src, size_t __n);
Mikael Starvik51533b62005-07-27 11:44:44 -070044
Jesper Nilsson66ab3a72009-04-21 11:44:57 +020045#define memzero(s, n) memset((s), 0, (n))
Mikael Starvik51533b62005-07-27 11:44:44 -070046
47typedef unsigned char uch;
48typedef unsigned short ush;
49typedef unsigned long ulg;
50
51#define WSIZE 0x8000 /* Window size must be at least 32k, */
52 /* and a power of two */
53
54static uch *inbuf; /* input buffer */
55static uch window[WSIZE]; /* Sliding window buffer */
56
57unsigned inptr = 0; /* index of next byte to be processed in inbuf
58 * After decompression it will contain the
59 * compressed size, and head.S will read it.
60 */
61
62static unsigned outcnt = 0; /* bytes in output buffer */
63
64/* gzip flag byte */
65#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
66#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
67#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
68#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
69#define COMMENT 0x10 /* bit 4 set: file comment present */
70#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
71#define RESERVED 0xC0 /* bit 6,7: reserved */
72
Jesper Nilsson66ab3a72009-04-21 11:44:57 +020073#define get_byte() (inbuf[inptr++])
Mikael Starvik51533b62005-07-27 11:44:44 -070074
75/* Diagnostic functions */
76#ifdef DEBUG
Jesper Nilsson66ab3a72009-04-21 11:44:57 +020077# define Assert(cond, msg) do { \
78 if (!(cond)) \
79 error(msg); \
80 } while (0)
Mikael Starvik51533b62005-07-27 11:44:44 -070081# define Trace(x) fprintf x
Jesper Nilsson66ab3a72009-04-21 11:44:57 +020082# define Tracev(x) do { \
83 if (verbose) \
84 fprintf x; \
85 } while (0)
86# define Tracevv(x) do { \
87 if (verbose > 1) \
88 fprintf x; \
89 } while (0)
90# define Tracec(c, x) do { \
91 if (verbose && (c)) \
92 fprintf x; \
93 } while (0)
94# define Tracecv(c, x) do { \
95 if (verbose > 1 && (c)) \
96 fprintf x; \
97 } while (0)
Mikael Starvik51533b62005-07-27 11:44:44 -070098#else
Jesper Nilsson66ab3a72009-04-21 11:44:57 +020099# define Assert(cond, msg)
Mikael Starvik51533b62005-07-27 11:44:44 -0700100# define Trace(x)
101# define Tracev(x)
102# define Tracevv(x)
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200103# define Tracec(c, x)
104# define Tracecv(c, x)
Mikael Starvik51533b62005-07-27 11:44:44 -0700105#endif
106
Mikael Starvik51533b62005-07-27 11:44:44 -0700107static void flush_window(void);
108static void error(char *m);
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200109static void puts(const char *);
Mikael Starvik51533b62005-07-27 11:44:44 -0700110
111extern char *input_data; /* lives in head.S */
112
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700113static long bytes_out;
Mikael Starvik51533b62005-07-27 11:44:44 -0700114static uch *output_data;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700115static unsigned long output_ptr;
Mikael Starvik51533b62005-07-27 11:44:44 -0700116
Mikael Starvik51533b62005-07-27 11:44:44 -0700117/* the "heap" is put directly after the BSS ends, at end */
118
119extern int _end;
120static long free_mem_ptr = (long)&_end;
Thomas Petazzoni2d6ffcc2008-07-25 01:45:44 -0700121static long free_mem_end_ptr;
Mikael Starvik51533b62005-07-27 11:44:44 -0700122
123#include "../../../../../lib/inflate.c"
124
Mikael Starvik51533b62005-07-27 11:44:44 -0700125/* decompressor info and error messages to serial console */
126
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200127#ifdef CONFIG_ETRAX_ARCH_V32
128static inline void serout(const char *s, reg_scope_instances regi_ser)
Mikael Starvik51533b62005-07-27 11:44:44 -0700129{
130 reg_ser_rs_stat_din rs;
131 reg_ser_rw_dout dout = {.data = *s};
132
133 do {
134 rs = REG_RD(ser, regi_ser, rs_stat_din);
135 }
Simon Arlott49b4ff32007-10-20 01:08:50 +0200136 while (!rs.tr_rdy);/* Wait for transceiver. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700137
138 REG_WR(ser, regi_ser, rw_dout, dout);
139}
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200140#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700141
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200142static void puts(const char *s)
Mikael Starvik51533b62005-07-27 11:44:44 -0700143{
144#ifndef CONFIG_ETRAX_DEBUG_PORT_NULL
145 while (*s) {
146#ifdef CONFIG_ETRAX_DEBUG_PORT0
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200147#ifdef CONFIG_ETRAX_ARCH_V32
Mikael Starvik51533b62005-07-27 11:44:44 -0700148 serout(s, regi_ser0);
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200149#else
150 while (!(*R_SERIAL0_STATUS & (1 << 5)))
151 ;
152 *R_SERIAL0_TR_DATA = *s++;
153#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700154#endif
155#ifdef CONFIG_ETRAX_DEBUG_PORT1
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200156#ifdef CONFIG_ETRAX_ARCH_V32
Mikael Starvik51533b62005-07-27 11:44:44 -0700157 serout(s, regi_ser1);
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200158#else
159 while (!(*R_SERIAL1_STATUS & (1 << 5)))
160 ;
161 *R_SERIAL1_TR_DATA = *s++;
162#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700163#endif
164#ifdef CONFIG_ETRAX_DEBUG_PORT2
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200165#ifdef CONFIG_ETRAX_ARCH_V32
Mikael Starvik51533b62005-07-27 11:44:44 -0700166 serout(s, regi_ser2);
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200167#else
168 while (!(*R_SERIAL2_STATUS & (1 << 5)))
169 ;
170 *R_SERIAL2_TR_DATA = *s++;
171#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700172#endif
173#ifdef CONFIG_ETRAX_DEBUG_PORT3
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200174#ifdef CONFIG_ETRAX_ARCH_V32
Mikael Starvik51533b62005-07-27 11:44:44 -0700175 serout(s, regi_ser3);
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200176#else
177 while (!(*R_SERIAL3_STATUS & (1 << 5)))
178 ;
179 *R_SERIAL3_TR_DATA = *s++;
180#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700181#endif
182 *s++;
183 }
184/* CONFIG_ETRAX_DEBUG_PORT_NULL */
185#endif
186}
187
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200188void *memset(void *s, int c, size_t n)
Mikael Starvik51533b62005-07-27 11:44:44 -0700189{
190 int i;
191 char *ss = (char*)s;
192
193 for (i=0;i<n;i++) ss[i] = c;
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100194
195 return s;
Mikael Starvik51533b62005-07-27 11:44:44 -0700196}
197
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200198void *memcpy(void *__dest, __const void *__src, size_t __n)
Mikael Starvik51533b62005-07-27 11:44:44 -0700199{
200 int i;
201 char *d = (char *)__dest, *s = (char *)__src;
202
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200203 for (i = 0; i < __n; i++)
204 d[i] = s[i];
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100205
206 return __dest;
Mikael Starvik51533b62005-07-27 11:44:44 -0700207}
208
209/* ===========================================================================
210 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
211 * (Used for the decompressed data only.)
212 */
213
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200214static void flush_window(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700215{
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200216 ulg c = crc; /* temporary variable */
217 unsigned n;
218 uch *in, *out, ch;
Mikael Starvik51533b62005-07-27 11:44:44 -0700219
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200220 in = window;
221 out = &output_data[output_ptr];
222 for (n = 0; n < outcnt; n++) {
223 ch = *out = *in;
224 out++;
225 in++;
226 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
227 }
228 crc = c;
229 bytes_out += (ulg)outcnt;
230 output_ptr += (ulg)outcnt;
231 outcnt = 0;
Mikael Starvik51533b62005-07-27 11:44:44 -0700232}
233
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200234static void error(char *x)
Mikael Starvik51533b62005-07-27 11:44:44 -0700235{
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200236 puts("\n\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700237 puts(x);
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200238 puts("\n\n -- System halted\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700239
240 while(1); /* Halt */
241}
242
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200243void setup_normal_output_buffer(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700244{
245 output_data = (char *)KERNEL_LOAD_ADR;
246}
247
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200248#ifdef CONFIG_ETRAX_ARCH_V32
249static inline void serial_setup(reg_scope_instances regi_ser)
Mikael Starvik51533b62005-07-27 11:44:44 -0700250{
251 reg_ser_rw_xoff xoff;
252 reg_ser_rw_tr_ctrl tr_ctrl;
253 reg_ser_rw_rec_ctrl rec_ctrl;
254 reg_ser_rw_tr_baud_div tr_baud;
255 reg_ser_rw_rec_baud_div rec_baud;
256
257 /* Turn off XOFF. */
258 xoff = REG_RD(ser, regi_ser, rw_xoff);
259
260 xoff.chr = 0;
261 xoff.automatic = regk_ser_no;
262
263 REG_WR(ser, regi_ser, rw_xoff, xoff);
264
265 /* Set baudrate and stopbits. */
266 tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
267 rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
268 tr_baud = REG_RD(ser, regi_ser, rw_tr_baud_div);
269 rec_baud = REG_RD(ser, regi_ser, rw_rec_baud_div);
270
271 tr_ctrl.stop_bits = 1; /* 2 stop bits. */
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100272 tr_ctrl.en = 1; /* enable transmitter */
273 rec_ctrl.en = 1; /* enabler receiver */
Mikael Starvik51533b62005-07-27 11:44:44 -0700274
275 /*
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100276 * The baudrate setup used to be a bit fishy, but now transmitter and
277 * receiver are both set to the intended baud rate, 115200.
278 * The magic value is 29.493 MHz.
Mikael Starvik51533b62005-07-27 11:44:44 -0700279 */
280 tr_ctrl.base_freq = regk_ser_f29_493;
281 rec_ctrl.base_freq = regk_ser_f29_493;
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100282 tr_baud.div = (29493000 / 8) / 115200;
Mikael Starvik51533b62005-07-27 11:44:44 -0700283 rec_baud.div = (29493000 / 8) / 115200;
284
285 REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
286 REG_WR(ser, regi_ser, rw_tr_baud_div, tr_baud);
287 REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
288 REG_WR(ser, regi_ser, rw_rec_baud_div, rec_baud);
289}
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200290#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700291
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200292void decompress_kernel(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700293{
294 char revision;
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200295 char compile_rev;
Mikael Starvik51533b62005-07-27 11:44:44 -0700296
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200297#ifdef CONFIG_ETRAX_ARCH_V32
298 /* Need at least a CRISv32 to run. */
299 compile_rev = 32;
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100300#if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
301 defined(CONFIG_ETRAX_DEBUG_PORT2) || \
302 defined(CONFIG_ETRAX_DEBUG_PORT3)
303 reg_pinmux_rw_hwprot hwprot;
304
305#ifdef CONFIG_CRIS_MACH_ARTPEC3
306 reg_clkgen_rw_clk_ctrl clk_ctrl;
307
308 /* Enable corresponding clock region when serial 1..3 selected */
309
310 clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);
311 clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;
312 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);
313#endif
314
315 /* pinmux setup for ports 1..3 */
316 hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
317#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700318
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200319
Mikael Starvik51533b62005-07-27 11:44:44 -0700320#ifdef CONFIG_ETRAX_DEBUG_PORT0
321 serial_setup(regi_ser0);
322#endif
323#ifdef CONFIG_ETRAX_DEBUG_PORT1
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100324 hwprot.ser1 = regk_pinmux_yes;
Mikael Starvik51533b62005-07-27 11:44:44 -0700325 serial_setup(regi_ser1);
326#endif
327#ifdef CONFIG_ETRAX_DEBUG_PORT2
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100328 hwprot.ser2 = regk_pinmux_yes;
Mikael Starvik51533b62005-07-27 11:44:44 -0700329 serial_setup(regi_ser2);
330#endif
331#ifdef CONFIG_ETRAX_DEBUG_PORT3
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100332 hwprot.ser3 = regk_pinmux_yes;
Mikael Starvik51533b62005-07-27 11:44:44 -0700333 serial_setup(regi_ser3);
334#endif
Jesper Nilssona5d204b2007-11-30 17:16:09 +0100335#if defined(CONFIG_ETRAX_DEBUG_PORT1) || \
336 defined(CONFIG_ETRAX_DEBUG_PORT2) || \
337 defined(CONFIG_ETRAX_DEBUG_PORT3)
338 REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
339#endif
340
341 /* input_data is set in head.S */
342 inbuf = input_data;
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200343#else /* CRISv10 */
344 /* Need at least a crisv10 to run. */
345 compile_rev = 10;
346
347 /* input_data is set in head.S */
348 inbuf = input_data;
349
350#ifdef CONFIG_ETRAX_DEBUG_PORT0
351 *R_SERIAL0_XOFF = 0;
352 *R_SERIAL0_BAUD = 0x99;
353 *R_SERIAL0_TR_CTRL = 0x40;
354#endif
355#ifdef CONFIG_ETRAX_DEBUG_PORT1
356 *R_SERIAL1_XOFF = 0;
357 *R_SERIAL1_BAUD = 0x99;
358 *R_SERIAL1_TR_CTRL = 0x40;
359#endif
360#ifdef CONFIG_ETRAX_DEBUG_PORT2
361 *R_GEN_CONFIG = 0x08;
362 *R_SERIAL2_XOFF = 0;
363 *R_SERIAL2_BAUD = 0x99;
364 *R_SERIAL2_TR_CTRL = 0x40;
365#endif
366#ifdef CONFIG_ETRAX_DEBUG_PORT3
367 *R_GEN_CONFIG = 0x100;
368 *R_SERIAL3_XOFF = 0;
369 *R_SERIAL3_BAUD = 0x99;
370 *R_SERIAL3_TR_CTRL = 0x40;
371#endif
372#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700373
374 setup_normal_output_buffer();
375
376 makecrc();
377
378 __asm__ volatile ("move $vr,%0" : "=rm" (revision));
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200379 if (revision < compile_rev) {
380#ifdef CONFIG_ETRAX_ARCH_V32
381 puts("You need an ETRAX FS to run Linux 2.6/crisv32\n");
382#else
383 puts("You need an ETRAX 100LX to run linux 2.6\n");
384#endif
Mikael Starvik51533b62005-07-27 11:44:44 -0700385 while(1);
386 }
387
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200388 puts("Uncompressing Linux...\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700389 gunzip();
Jesper Nilsson66ab3a72009-04-21 11:44:57 +0200390 puts("Done. Now booting the kernel\n");
Mikael Starvik51533b62005-07-27 11:44:44 -0700391}