/****************************************************************************
*
* (C) 2000 by BECK IPC GmbH
*
*  BECK IPC GmbH
*  Garbenheimerstr. 38
*  D-35578 Wetzlar
*
*  Phone : (49)-6441-905-240
*  Fax   : (49)-6441-905-245
*

$Header: HWAPI.H, 8, 04.02.2002 16:44:22, Christoph Stoidner$

$Log: 
 8    IPC@CHIP  1.7         04.02.2002 16:44:22  Christoph Stoidner add DMA
      WordTransfer
 7    IPC@CHIP  1.6         16.01.2002 14:17:45  Christoph Stoidner add
      GetFrequency defines. restructured DmaInfo struct
 6    IPC@CHIP  1.5         16.01.2002 11:45:04  Christoph Stoidner add starteam
      directives
 5    IPC@CHIP  1.4         16.01.2002 11:04:29  Christoph Stoidner add new
      functions and some comments
 4    IPC@CHIP  1.3         31.05.2001 16:18:24  Christoph Stoidner 
 3    IPC@CHIP  1.2         23.02.2001 11:59:14  Ernest Schlösser Added a few
      comments
 2    IPC@CHIP  1.1         15.02.2001 15:16:52  Christoph Stoidner 
 1    IPC@CHIP  1.0         14.02.2001 16:09:19  Christoph Stoidner 
$
*****************************************************************************/


#ifndef HWAPI_H
#define HWAPI_H

/***************************************************************************/
// Typedefs
/***************************************************************************/
typedef void interrupt (*InterruptHandler)(void);

/***************************************************************************/
// Defines
/***************************************************************************/
#ifdef _MSC_VER /* If Microsoft C Compiler is used, define new FP_SEG and FP_OFF Macro */
	#ifdef FP_SEG
  		#undef FP_SEG
	#endif
	#define FP_SEG(ptr)    ((unsigned int)(((unsigned long)ptr)>>16))

	#ifdef FP_OFF
  		#undef FP_OFF
	#endif
	#define FP_OFF(ptr)    ((unsigned int)(((unsigned long)ptr)&0xFFFF))
#endif


/***************************************************************************/
// Hardware API Function Numbers
/***************************************************************************/
// PFE Services
#define PFE_ENABLE_DATA_BUS             0x80
#define PFE_ENABLE_NON_MULTI_ADDR_BUS   0x81
#define PFE_ENABLE_PIO                  0x82
#define PFE_ENABLE_PCS                  0x83
#define PFE_ENABLE_EXTERNAL_IRQ         0x84
#define PFE_ENABLE_EXTERNAL_TIMER       0x85
#define PFE_SET_EDGE_LEVEL_INTR_MODE    0x86
#define PFE_ENABLE_PWD                  0x87
#define PFE_ENABLE_EXTERNAL_DMA         0x88
#define PFE_ENABLE_INT_CASCADE_MODE     0x89
// HAL Services
#define HAL_SET_INT0_VECT               0x10
#define HAL_READ_DATA_BUS               0x80
#define HAL_WRITE_DATA_BUS              0x81
#define HAL_READ_PIO                    0x82
#define HAL_WRITE_PIO                   0x83
#define HAL_INSTALL_ISR                 0x84
#define HAL_INIT_TIMER                  0x85
#define HAL_START_TIMER                 0x86
#define HAL_STOP_TIMER                  0x87
#define HAL_READ_TIMER_COUNT            0x88
#define HAL_WRITE_TIMER_COUNT           0x89
#define HAL_GET_FREQUENCIES             0x8A
#define HAL_SET_DUTY_CYCLE_WAVEFORM     0x8B
#define HAL_READ_SPECIFIC_PIO           0x8C
#define HAL_WRITE_SPECIFIC_PIO          0x8D
#define HAL_GIVE_EOI                    0x8E
#define HAL_GET_SET_WATCHDOG_MODE       0x90
#define HAL_REFRESH_WATCHDOG            0x91
#define HAL_BLOCK_READ_DATA_BUS         0xA0
#define HAL_BLOCK_WRITE_DATA_BUS        0xA1
#define HAL_START_DMA                   0xB0
#define HAL_STOP_DMA                    0xB1
#define HAL_GET_DMA_INFO                0xB2
#define HAL_INIT_REST_NON_VOLATILE_DATA 0xC0
#define HAL_SAVE_NON_VOLATILE_DATA      0xC1
#define HAL_GET_REBOOT_REASON           0xC2


/***************************************************************************/
// Interrupt Vectors
/***************************************************************************/
#define PFE_INT  0xA2
#define HAL_INT  0xA1


/***************************************************************************/
// General definitions
/***************************************************************************/
#define DIRECT_IO


#define PIO_I      1
#define PIO_IPU    2
#define PIO_IPD    3
#define PIO_O1     4
#define PIO_O0     5

#define A0         0
#define A1         1
#define A2         2

#define GET_FRQ_PROCESSOR  0
#define GET_FRQ_TIMER      1
#define GET_FRQ_MAX_BAUD   2
#define GET_FRQ_PWD        3

#ifdef DIRECT_IO
 #define hal_inportb(x)      inportb(x)
 #define hal_outportb(x,y)   outportb(x,y)
#else
 #define hal_inportb(x)      hal_read_bus(x,0xFFFF,0x0000)
 #define hal_outportb(x,y)   hal_write_bus(x,y,0xFFFF,0x0000)
#endif

#define hal_inportb_block    hal_readblock_bus
#define hal_outportb_block   hal_writeblock_bus


// DMA Control registers Bits
#define DMA_PRIO      (1<<0)
#define DMA_SYNCSRC   (1<<1)
#define DMA_SYNCDEST  (1<<2)
#define DMA_DOINTR    (1<<3)
#define DMA_SRCINC    (1<<5)
#define DMA_SRCDEC    (1<<6)
#define DMA_SRCIO     (0)     /* Bit 7  equal to null */
#define DMA_SRCMEM    (1<<7)
#define DMA_DESTINC   (1<<8)
#define DMA_DESTDEC   (1<<9)
#define DMA_DESTIO    (0)     /* Bit 10 equal to null */
#define DMA_DESTMEM   (1<<10)
#define DMA_WORDTRANS (1<<11)

// Dma channels
#define DMA_CH0       0
#define DMA_CH1       1



/***************************************************************************/
// Datatypes
/***************************************************************************/

// DMA Info Struct
typedef struct
{
   unsigned count;
   union
   {
     void     *mem;
     unsigned  io;
   } src;

   union
   {
     void     *mem;
     unsigned  io;
   } dest;

   unsigned ctrl_reg;
} Dma_Info;

// Non Valotile Data
#ifndef MAX_RETENTIVE_AREAS
 /*!!!! Has to be passed for the user application !!!!*/
 #define MAX_RETENTIVE_AREAS 10
#endif
typedef struct _REMOP
{
    unsigned entries; // number of entries in struct REMOP_ENTRY x[]
    unsigned segment; // common segment address

    struct REMOP_ENTRY
    {
        unsigned offs;      // offset address
        unsigned size;      // number of bytes (must be <= maxsize)
        unsigned maxsize;   // maximal number of bytes
        unsigned elemsize;  // number of bytes per data element
        unsigned distance;  // distance to next data element (must be >= elemsize)
    }x[MAX_RETENTIVE_AREAS];
} Remop;



/***************************************************************************/
// Prototyps
/***************************************************************************/

// ********** PFE Functiuons **************************
void pfe_enable_bus (unsigned mask, unsigned char ale);
void pfe_enable_adr (unsigned char adr);
void pfe_enable_pcs (unsigned char pcs);
void pfe_enable_int (unsigned char irq);
void pfe_enable_timer (unsigned mode);
void pfe_enable_pio (unsigned pio, unsigned char mode);
void pfe_get_pio_state (unsigned *wPio, unsigned *wInp, unsigned *wOut);
void pfe_set_edge_level_intr_mode(unsigned char mode, unsigned mask);
void pfe_enable_pwd(void);
int pfe_enable_external_dma( unsigned char channel);
void pfe_enable_int_cascade_mode(void);


// ************ HAL functions ************************
unsigned char hal_read_bus (unsigned adr, unsigned wAND, unsigned wXOR);
void hal_write_bus (unsigned adr, unsigned char value, unsigned wAND, unsigned wXOR);
void hal_readblock_bus (unsigned adr0, unsigned adr1, unsigned char *buf, unsigned cnt);
void hal_writeblock_bus (unsigned adr0, unsigned adr1, unsigned char *buf, unsigned cnt);
//  PIO functions
void hal_get_pio_output_state (unsigned *pins, unsigned wOut);
// function 0x82: returns the value of PIO 0..13
unsigned hal_read_pios (unsigned wAND, unsigned wXOR);
void     hal_write_pios (unsigned value, unsigned wAND, unsigned wXOR);

// function 0x8C: read specifik pio pin 0..13, ret 0 if low
unsigned char hal_read_pio (unsigned char pio);
void          hal_write_pio (unsigned char pio, unsigned value);

InterruptHandler hal_install_isr (unsigned irq, unsigned count, InterruptHandler handler);
void hal_init_timer (unsigned char timer, unsigned mode, unsigned divider);
void hal_start_timer (unsigned char timer);
void hal_stop_timer (unsigned char timer);
unsigned hal_read_timer_count (unsigned char timer, unsigned far *maxcount);
void hal_write_timer_count (unsigned char timer, unsigned counter);
unsigned char hal_set_watchdog_mode (unsigned char mode);
void hal_refresh_watchdog (void);

unsigned long hal_get_frequencies( unsigned char frq );
void hal_set_duty_cycle_waveform( unsigned char timer, int mode,
                                  unsigned clock_divider );
void hal_give_eoi( unsigned char intr_no );

// DMA Functions
void hal_start_dma( unsigned char channel, Dma_Info *dmaInfo );
int hal_stop_dma( unsigned char channel );
int hal_get_dma_info( unsigned char channel, Dma_Info *dmaInfo );

// NMI
int hal_init_non_volatile_data( Remop *remop );
void hal_save_non_volatile_data(void);

int hal_get_reboot_reason(void);


#endif

