/*
 *  Copyright (c) 1996-2007 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 copyrighted work of Digi International
 *  Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 *  prohibited.
 *
 *  Restricted Rights Legend
 *
 *  Use, duplication, or disclosure by the Government is subject to
 *  restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 *  Technical Data and Computer Software clause at DFARS 252.227-7031 or
 *  subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 *  Restricted Rights at 48 CFR 52.227-19, as applicable.
 *
 *  Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 */

#ifndef _IOP_SERIAL_H
#define _IOP_SERIAL_H

#include <tx_api.h>
#include <iop_api.h>
#include <iohub_reg.h>
#include <termios.h>

#ifdef __cplusplus
extern "C"
{
#endif

/* IOP control registers */
#define NA_IOHUB_CONTROL_BASE               (0x90001010)
#define NA_IOHUB_CONTROL_getAddress(pic, index)  ((unsigned long volatile *)NA_IOHUB_CONTROL_BASE + (pic * NA_IOHUB_ADDRESS_INCREMENT) + index)

/* IOP port expansion registers */
#define NA_IOHUB_EXP_BASE                   (0x90001050)
#define NA_IOHUB_EXP_getAddress(pic, index)  ((unsigned long volatile *)NA_IOHUB_EXP_BASE + (pic * NA_IOHUB_ADDRESS_INCREMENT) + index)

#define IOP_MAX_UART_PORTS      NA_IOP_MAX_PICS

/* default settings */
#define IOP_DEFAULT_BAUDRATE    9600

#define	O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
#define O_NOFLUSH	0x10000000

#define IOP_ONE_STOP_BIT        0x1
#define IOP_TWO_STOP_BITS       0x11

#define IOP_RX_BUF_SZ           0x4000      /* 16k for now */
#define IOP_RX_DESCR_MASK       (IOP_MAX_DMA_DESCRIPTORS - 1)
#define IOP_TX_BUF_SZ           0x4000      /* 16k for now */
#define IOP_TX_BUF_MASK         (IOP_TX_BUF_SZ - 1)

typedef struct iop_serial_info_t
{
    unsigned tx_in;
    unsigned tx_out;
    unsigned rx_in_idx;
    unsigned rx_out_idx;
    unsigned tx_dma_error;
    unsigned rx_dma_error;
    unsigned rx_len[IOP_MAX_DMA_DESCRIPTORS];
    unsigned rx_offset[IOP_MAX_DMA_DESCRIPTORS];
}iop_serial_info_t;


typedef struct iop_serl_port_t 
{
    NaIopProcessor_t processor;
    int opened;
    unsigned mode;
    unsigned char *rxBuf;
    unsigned char *txBuf;
    TX_MUTEX rxMutex;
    TX_MUTEX txMutex;
    int baudrate;
    int numBits;
    int totalBits;
    struct termios tios;
    struct serial_cntr_t counters;
    struct serial_status_t status;
    iop_serial_info_t  info;
    unsigned short mask;  // bit mask
}iop_serl_port_t;

/* interrupts code from driver to FIM */
#define IOP_INSERT_CHAR         0x01
#define IOP_SETUP_MATCH_CHAR    0x02
#define IOP_SET_BITS_IN_CHAR    0x04
#define IOP_SET_CHAR_GAP        0x08
#define IOP_SET_BIT_POSITION    0x10
#define IOP_SET_TIMER_PRESCALE  0x20
#define IOP_SET_BIT_TIME        0x40

/* interrupt code from FIM to driver */
#define IOP_MATCH_CHAR1     0x01
#define IOP_MATCH_CHAR2     0x02
#define IOP_RX_OVERFLOW     0x04

/*FIM status */
#define IOP_INIT_COMPLETE       0x01
#define IOP_TX_ENABLE           0x02
#define IOP_EN_HW_FLOW_CNTRL    0x04
#define IOP_EN_MATCH_CHAR1      0x08
#define IOP_EN_MATCH_CHAR2      0x10

/* DMA status bit values */
#define IOP_DMA_NORMAL_CLOSE    0x01
#define IOP_DMA_MATCH_CLOSE     0x02

#ifdef __cplusplus
}
#endif

#endif /* _IOP_SERIAL_H */
