
/*****************************************************************************
 * Copyright (c) 1988-96 Digi International Inc., All Rights Reserved
 * 
 * This  software  contains  proprietary  and  confidential   information   of
 * Digi International  Inc.   By  accepting  transfer  of this copy, Recipient
 * agrees to retain this software in  confidence,  to  prevent  disclosure  to
 * others,  and  to make no  use of this software other than that for which it
 * was delivered.  This is an unpublished copyright work  Digi   International
 * Inc.    Except   as   permitted   by  federal  law,  17 USC 117, copying is
 * strictly prohibited.
 * 
 * Digi  International  Inc.  CONFIDENTIAL  -   (Digi    International    Inc.
 * Confidential  when   combined   with   the   aggregated  modules  for  this
 * product) OBJECT CODE ONLY SOURCE MATERIALS
 * 
 * (C) COPYRIGHT Digi International Inc. 1995
 * 
 * All Rights Reserved
 * 
 * US   Government   Users   Restricted   Rights   -   Use,   duplication   or
 * disclosure  restricted by GSA ADP Schedule Contract with Digi International
 * Inc.
 * 
 *****************************************************************************/

/*
 * stac9706.h - header file for the STAC 9706 compression engine.
 *
 * @(#) $Id: stac9706.h,v 1.10 1997/02/19 22:50:07 gary Exp $
 */

typedef struct {
	struct {
		uint32	Config[2] ;
		uint32	_reserved_0 ;
		uint32	FifoAccess ;
		uint32	Command ;
		uint32	Status ;
		uint32	CounterLo ;
		uint32	CounterHi ;
	} Base ;
	union {
		struct {
			uint32	Config ;
			uint32	Status ;
			uint32	AddrLo ;
			uint32	AddrHi ;
			uint32	Data ;
		} MPU ;
		struct {
			uint32	Config ;
			uint32	Status ;
			uint32	Map ;
			uint32	Id ;
			uint32	ReadLo ;
			uint32	ReadHi ;
			uint32	WriteLo ;
			uint32	WriteHi ;
		} FIFO ;
		struct {
			uint32	HistoryLo ;
			uint32	HistoryHi ;
			uint32	Info[2] ;
		} Decompress ;
		struct {
			uint32	Info[5] ;
		} Compress ;
	} Upper ;
} STAC9706 ;

typedef struct StacJob_structure {
	struct StacJob_structure *NextJob ;
	uint32 Data ;
	void (*CallBack)() ;
	uint32 CallParam ;
	uint32 JobState ;
	struct StacContext_structure *ThisContext ;
	uint8  XORByte ;
} StacJob_t ;

typedef struct StacContext_structure {
	uint32 State ;
	uint32 CompInfo[5] ;
	uint32 DecompInfo[2] ;
	uint32 CompHistLo ;
	uint32 CompHistHi ;
	uint32 DecompHistLo ;
	uint32 DecompHistHi ;
	StacJob_t CompJob ;
	StacJob_t DecompJob ;
	StacJob_t ResetJob ;
} StacContext_t ;

/*
 * Values written to the Base Command register.
 */
#define	STOPAB		0x00
#define	STOPA		0x01
#define	STOPB		0x02
#define	STOP		0x03
#define	COMP		0x04
#define	FLUSH		0x05
#define	INIT		0x06
#define	DECOMP		0x08
#define	PASSAB		0x0A
#define	NOP		0x0C
#define	CLRA		0x0D
#define	DISABLERST	0x0E
#define	ENABLERST	0x0F
#define	PAUSE		0x40
#define	AUTOPAUSE	0x80

/*
 * Values written to the Base Control/Map register. Assumes no
 * CRAM parity.
 */
#define	MAP_MPU_PAGE		0x30
#define	MAP_FIFO_PAGE		0x31
#define	MAP_DECOMPRESS_PAGE	0x32
#define	MAP_COMPRESS_PAGE	0x33

#define	CTL_CNT_A_ZERO_CLR	0x80
#define	CTL_OP_DONE_CLR		0x40
#define	CTL_LCB_REG_SELECT	0x10
#define	CTL_ACCESS_A		0x00
#define	CTL_ACCESS_A_PRESET_B	0x01
#define	CTL_ACCESS_B_NO_SWAP	0x02
#define	CTL_ACCESS_B_SWAP	0x0A

/*
 * Values read from the Base Status register.
 */
#define	STATUS_A_ZERO		0x80
#define	STATUS_OP_DONE		0x40
#define	STATUS_FIFO_A_OUT	0x10
#define	STATUS_FIFO_A_IN	0x08
#define	STATUS_CNT_B_MASK	0x07

/*
 * Values written to the Base Config[0] register.
 */
#define	OP_DONE_INT_ENABLE	0x04
#define	CNT_A_0_INT_ENABLE	0x02

/*
 * Values written to the Base Config[1] register.
 */
#define	FIFO_PTR_ENABLE		0x08
#define	RAM_CYCLE_SELECT	0x02
#define	RAM_WIDTH_16BITS	0x01

/*
 * Values written to the MPU Config register.
 */
#define	FAST_DOWNLOAD		0x0800
#define	WAIT_MODE		0x0500	/* other values won't work on our HW */
#define	DOWNLOAD_CNT_A		0x0080
#define	MPU_DATA_ENABLE		0x0040
#define	MPU_DATA_READ		0x0020
#define	HALT_RAM		0x0008
#define	SPLIT_FIFO		0x0004
#define	FIFO_SIZE_16K		0x0000
#define	FIFO_SIZE_32K		0x0001
#define	FIFO_SIZE_48K		0x0002
#define	FIFO_SIZE_64K		0x0003

/*
 * Values read from the MPU Status register.
 */
#define	MPU_BUFFER_EMPTY	0x0040

/*
 * Values written to the FIFO Config register (note bits 7 & 3 always set..)
 */
#define	DOWNLOAD_SESSION	0xC8
#define	CRAM_PARITY_ENABLE	0xA8
#define	CRAM_PARITY_INT_ENABLE	0x98
#define	PSEUDO_FLUSH		0x8C
#define	DRAM_SLOW_REFRESH	0x8A
#define	DRAM_SELECT		0x89
#define FIFO_CONFIG_NOP		0x88

/*
 * Values read from the FIFO Status register.
 */
#define	CRAM_PARITY_ERROR	(CRAM_PARITY_ERROR_HI | CRAM_PARITY_ERROR_LO)
#define	CRAM_PARITY_ERROR_HI	0x04
#define	CRAM_PARITY_ERROR_LO	0x02
#define	RESET_DONE		0x01

/*
 * Values written to the Decompress History High register.
 */
#define	KEEP_HISTORY		0x80
#define	DECOMPRESS_HISTORY	0x40

/*
 * Values for State field in StacContext structure
 */
#define	STAC_CONTEXT_USED			0x01
#define	STAC_CONTEXT_COMPRESS_FINISHED		0x02
#define	STAC_CONTEXT_DECOMPRESS_FINISHED	0x04

/*
 * Values for JobState field in StacJob structure
 */
#define	STAC_JOB_COMPRESS	0x01
#define	STAC_JOB_DECOMPRESS	0x02
#define	STAC_JOB_RESET		0x04
#define	STAC_JOB_TYPE		0x07

/*
 * Values for StacFifoState variable
 */
#define	STAC_STATE_IDLE		0x00
#define	STAC_STATE_COMPRESSING	0x01
#define	STAC_STATE_BUSY		(STAC_STATE_COMPRESSING)

/*
 * Other defines.. base on 2MB of memory, 64K FIFO (split or not split).
 */
#define	CRAM_SIZE		0x200000
#define	FIFO_BASE		(CRAM_SIZE - 0x10000)
#define	FIFO_SIZE		(CRAM_SIZE - FIFO_BASE)
#define	COMP_HISTORY_SIZE	0x4000		/* 16K per compression history*/
#define	DECOMP_HISTORY_SIZE	0x1000		/* 4K per decompression --"-- */

/*
 * This is our best estimate so far
 */
#define	BREAKEVEN_POINT			100000	/* No backgrounding for now */
#define	STAC_PREPEND_MARGIN		31
#define	STAC_PERFORMANCE_PARAMETER	8

//#define	STAC_DECOMP_BUFFER_SIZE		1600
#define	STAC_DECOMP_BUFFER_SIZE		(2048 - STAC_PREPEND_MARGIN)

/*
 * Functional prototypes
 */
uint32 StacCompress() ;
uint32 StacDecompress() ;
uint32 StacResetHistory() ;
uint32 StacBeginCompress() ;
uint32 StacFinishCompress() ;
uint32 StacBeginDecompress() ;
uint32 StacBeginReset() ;
caddr_t StacGetNewContext() ;
void StacEmergencyCleanup() ;

/*
 * Macros
 */

/*
 * Macro which determines whether or not a STAC 9706 is present or not
 */
#if PRI
#define	STAC_9706_EXISTS	(!(GPP0 & GP_STAC_MISSING))
#else
#define	STAC_9706_EXISTS	0
#endif

/*
 * These three macros allow a user to quickly determine whether or not
 * a job has already been scheduled by the "staclib" library.
 *
 * Each macro requires a context handle as a parameter.
 */
#define	COMPRESS_JOB_PENDING(x) \
	((((StacContext_t *) (x))->CompJob.Data) || \
	 (((StacContext_t *) (x))->State & STAC_CONTEXT_COMPRESS_FINISHED))

#define	DECOMPRESS_JOB_PENDING(x) \
	((((StacContext_t *) (x))->DecompJob.Data) || \
	 (((StacContext_t *) (x))->State & STAC_CONTEXT_DECOMPRESS_FINISHED))

#define	RESET_JOB_PENDING(x) \
			(((StacContext_t *) (x))->ResetJob.Data)

/*
 * These macros allow the user to turn the KeepHistory option on or off
 */
#define	STAC_KEEP_HISTORY_OFF(x) \
		if (STAC_9706_EXISTS) \
		((StacContext_t *) (x))->CompHistHi &= ~(KEEP_HISTORY) ; \
		else \
		((StacContext_t *) (x))->CompHistHi &= ~(LZS_SAVE_HISTORY)

#define	STAC_KEEP_HISTORY_ON(x) \
		if (STAC_9706_EXISTS) \
		((StacContext_t *) (x))->CompHistHi |= KEEP_HISTORY ; \
		else \
		((StacContext_t *) (x))->CompHistHi |= LZS_SAVE_HISTORY

