Path: blob/master/arch/cris/boot/compressed/misc.c
10818 views
/*1* misc.c2*3* This is a collection of several routines from gzip-1.0.34* adapted for Linux.5*6* malloc by Hannu Savolainen 1993 and Matthias Urlichs 19947* puts by Nick Holloway 1993, better puts by Martin Mares 19958* adaptation for Linux/CRIS Axis Communications AB, 19999*10*/1112/* where the piggybacked kernel image expects itself to live.13* it is the same address we use when we network load an uncompressed14* image into DRAM, and it is the address the kernel is linked to live15* at by vmlinux.lds.S16*/1718#define KERNEL_LOAD_ADR 0x400040001920#include <linux/types.h>2122#ifdef CONFIG_ETRAX_ARCH_V3223#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_ARTPEC328#include <hwregs/clkgen_defs.h>29#endif30#else31#include <arch/svinto.h>32#endif3334/*35* gzip declarations36*/3738#define OF(args) args39#define STATIC static4041void *memset(void *s, int c, size_t n);42void *memcpy(void *__dest, __const void *__src, size_t __n);4344#define memzero(s, n) memset((s), 0, (n))4546typedef unsigned char uch;47typedef unsigned short ush;48typedef unsigned long ulg;4950#define WSIZE 0x8000 /* Window size must be at least 32k, */51/* and a power of two */5253static uch *inbuf; /* input buffer */54static uch window[WSIZE]; /* Sliding window buffer */5556unsigned inptr = 0; /* index of next byte to be processed in inbuf57* After decompression it will contain the58* compressed size, and head.S will read it.59*/6061static unsigned outcnt = 0; /* bytes in output buffer */6263/* gzip flag byte */64#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */65#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */66#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */67#define ORIG_NAME 0x08 /* bit 3 set: original file name present */68#define COMMENT 0x10 /* bit 4 set: file comment present */69#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */70#define RESERVED 0xC0 /* bit 6,7: reserved */7172#define get_byte() (inbuf[inptr++])7374/* Diagnostic functions */75#ifdef DEBUG76# define Assert(cond, msg) do { \77if (!(cond)) \78error(msg); \79} while (0)80# define Trace(x) fprintf x81# define Tracev(x) do { \82if (verbose) \83fprintf x; \84} while (0)85# define Tracevv(x) do { \86if (verbose > 1) \87fprintf x; \88} while (0)89# define Tracec(c, x) do { \90if (verbose && (c)) \91fprintf x; \92} while (0)93# define Tracecv(c, x) do { \94if (verbose > 1 && (c)) \95fprintf x; \96} while (0)97#else98# define Assert(cond, msg)99# define Trace(x)100# define Tracev(x)101# define Tracevv(x)102# define Tracec(c, x)103# define Tracecv(c, x)104#endif105106static void flush_window(void);107static void error(char *m);108static void aputs(const char *s);109110extern char *input_data; /* lives in head.S */111112static long bytes_out;113static uch *output_data;114static unsigned long output_ptr;115116/* the "heap" is put directly after the BSS ends, at end */117118extern int _end;119static long free_mem_ptr = (long)&_end;120static long free_mem_end_ptr;121122#include "../../../../../lib/inflate.c"123124/* decompressor info and error messages to serial console */125126#ifdef CONFIG_ETRAX_ARCH_V32127static inline void serout(const char *s, reg_scope_instances regi_ser)128{129reg_ser_rs_stat_din rs;130reg_ser_rw_dout dout = {.data = *s};131132do {133rs = REG_RD(ser, regi_ser, rs_stat_din);134}135while (!rs.tr_rdy);/* Wait for transceiver. */136137REG_WR(ser, regi_ser, rw_dout, dout);138}139#define SEROUT(S, N) \140do { \141serout(S, regi_ser ## N); \142s++; \143} while (0)144#else145#define SEROUT(S, N) do { \146while (!(*R_SERIAL ## N ## _STATUS & (1 << 5))) \147; \148*R_SERIAL ## N ## _TR_DATA = *s++; \149} while (0)150#endif151152static void aputs(const char *s)153{154#ifndef CONFIG_ETRAX_DEBUG_PORT_NULL155while (*s) {156#ifdef CONFIG_ETRAX_DEBUG_PORT0157SEROUT(s, 0);158#endif159#ifdef CONFIG_ETRAX_DEBUG_PORT1160SEROUT(s, 1);161#endif162#ifdef CONFIG_ETRAX_DEBUG_PORT2163SEROUT(s, 2);164#endif165#ifdef CONFIG_ETRAX_DEBUG_PORT3166SEROUT(s, 3);167#endif168}169#endif /* CONFIG_ETRAX_DEBUG_PORT_NULL */170}171172void *memset(void *s, int c, size_t n)173{174int i;175char *ss = (char*)s;176177for (i=0;i<n;i++) ss[i] = c;178179return s;180}181182void *memcpy(void *__dest, __const void *__src, size_t __n)183{184int i;185char *d = (char *)__dest, *s = (char *)__src;186187for (i = 0; i < __n; i++)188d[i] = s[i];189190return __dest;191}192193/* ===========================================================================194* Write the output window window[0..outcnt-1] and update crc and bytes_out.195* (Used for the decompressed data only.)196*/197198static void flush_window(void)199{200ulg c = crc; /* temporary variable */201unsigned n;202uch *in, *out, ch;203204in = window;205out = &output_data[output_ptr];206for (n = 0; n < outcnt; n++) {207ch = *out = *in;208out++;209in++;210c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);211}212crc = c;213bytes_out += (ulg)outcnt;214output_ptr += (ulg)outcnt;215outcnt = 0;216}217218static void error(char *x)219{220aputs("\n\n");221aputs(x);222aputs("\n\n -- System halted\n");223224while(1); /* Halt */225}226227void setup_normal_output_buffer(void)228{229output_data = (char *)KERNEL_LOAD_ADR;230}231232#ifdef CONFIG_ETRAX_ARCH_V32233static inline void serial_setup(reg_scope_instances regi_ser)234{235reg_ser_rw_xoff xoff;236reg_ser_rw_tr_ctrl tr_ctrl;237reg_ser_rw_rec_ctrl rec_ctrl;238reg_ser_rw_tr_baud_div tr_baud;239reg_ser_rw_rec_baud_div rec_baud;240241/* Turn off XOFF. */242xoff = REG_RD(ser, regi_ser, rw_xoff);243244xoff.chr = 0;245xoff.automatic = regk_ser_no;246247REG_WR(ser, regi_ser, rw_xoff, xoff);248249/* Set baudrate and stopbits. */250tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);251rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);252tr_baud = REG_RD(ser, regi_ser, rw_tr_baud_div);253rec_baud = REG_RD(ser, regi_ser, rw_rec_baud_div);254255tr_ctrl.stop_bits = 1; /* 2 stop bits. */256tr_ctrl.en = 1; /* enable transmitter */257rec_ctrl.en = 1; /* enabler receiver */258259/*260* The baudrate setup used to be a bit fishy, but now transmitter and261* receiver are both set to the intended baud rate, 115200.262* The magic value is 29.493 MHz.263*/264tr_ctrl.base_freq = regk_ser_f29_493;265rec_ctrl.base_freq = regk_ser_f29_493;266tr_baud.div = (29493000 / 8) / 115200;267rec_baud.div = (29493000 / 8) / 115200;268269REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);270REG_WR(ser, regi_ser, rw_tr_baud_div, tr_baud);271REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);272REG_WR(ser, regi_ser, rw_rec_baud_div, rec_baud);273}274#endif275276void decompress_kernel(void)277{278char revision;279char compile_rev;280281#ifdef CONFIG_ETRAX_ARCH_V32282/* Need at least a CRISv32 to run. */283compile_rev = 32;284#if defined(CONFIG_ETRAX_DEBUG_PORT1) || \285defined(CONFIG_ETRAX_DEBUG_PORT2) || \286defined(CONFIG_ETRAX_DEBUG_PORT3)287reg_pinmux_rw_hwprot hwprot;288289#ifdef CONFIG_CRIS_MACH_ARTPEC3290reg_clkgen_rw_clk_ctrl clk_ctrl;291292/* Enable corresponding clock region when serial 1..3 selected */293294clk_ctrl = REG_RD(clkgen, regi_clkgen, rw_clk_ctrl);295clk_ctrl.sser_ser_dma6_7 = regk_clkgen_yes;296REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);297#endif298299/* pinmux setup for ports 1..3 */300hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);301#endif302303304#ifdef CONFIG_ETRAX_DEBUG_PORT0305serial_setup(regi_ser0);306#endif307#ifdef CONFIG_ETRAX_DEBUG_PORT1308hwprot.ser1 = regk_pinmux_yes;309serial_setup(regi_ser1);310#endif311#ifdef CONFIG_ETRAX_DEBUG_PORT2312hwprot.ser2 = regk_pinmux_yes;313serial_setup(regi_ser2);314#endif315#ifdef CONFIG_ETRAX_DEBUG_PORT3316hwprot.ser3 = regk_pinmux_yes;317serial_setup(regi_ser3);318#endif319#if defined(CONFIG_ETRAX_DEBUG_PORT1) || \320defined(CONFIG_ETRAX_DEBUG_PORT2) || \321defined(CONFIG_ETRAX_DEBUG_PORT3)322REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);323#endif324325/* input_data is set in head.S */326inbuf = input_data;327#else /* CRISv10 */328/* Need at least a crisv10 to run. */329compile_rev = 10;330331/* input_data is set in head.S */332inbuf = input_data;333334#ifdef CONFIG_ETRAX_DEBUG_PORT0335*R_SERIAL0_XOFF = 0;336*R_SERIAL0_BAUD = 0x99;337*R_SERIAL0_TR_CTRL = 0x40;338#endif339#ifdef CONFIG_ETRAX_DEBUG_PORT1340*R_SERIAL1_XOFF = 0;341*R_SERIAL1_BAUD = 0x99;342*R_SERIAL1_TR_CTRL = 0x40;343#endif344#ifdef CONFIG_ETRAX_DEBUG_PORT2345*R_GEN_CONFIG = 0x08;346*R_SERIAL2_XOFF = 0;347*R_SERIAL2_BAUD = 0x99;348*R_SERIAL2_TR_CTRL = 0x40;349#endif350#ifdef CONFIG_ETRAX_DEBUG_PORT3351*R_GEN_CONFIG = 0x100;352*R_SERIAL3_XOFF = 0;353*R_SERIAL3_BAUD = 0x99;354*R_SERIAL3_TR_CTRL = 0x40;355#endif356#endif357358setup_normal_output_buffer();359360makecrc();361362__asm__ volatile ("move $vr,%0" : "=rm" (revision));363if (revision < compile_rev) {364#ifdef CONFIG_ETRAX_ARCH_V32365aputs("You need at least ETRAX FS to run Linux 2.6/crisv32\n");366#else367aputs("You need an ETRAX 100LX to run linux 2.6/crisv10\n");368#endif369while(1);370}371372aputs("Uncompressing Linux...\n");373gunzip();374aputs("Done. Now booting the kernel\n");375}376377378