/**********************************************************************************************************************
*  Copyright (C), 2022-2026, SMARTLOGIC TECHNOLOGY LTD.
*  File Name: ecpri_comm.h
*  Create Date: 23/12/11
*  Description: eCPRI Common Module Header File
*  Change History:
*      <author>      <time>      <version>   <desc>
*    1. ShangH      23/12/11        1.0       Build this module
**********************************************************************************************************************/

#ifndef _ECPRI_COMM_H_
#define _ECPRI_COMM_H_


/* -------------------------------------------------- <INC FILE> --------------------------------------------------- */
#include "typedef.h"
#include "apcs.h"


/* -------------------------------------------------- <MACRO DEF> -------------------------------------------------- */
#ifndef NULL
#define NULL                                    ((void *)0)
#endif

/* operation result */
#ifndef SUCCESS
#define SUCCESS                                 0
#endif
#ifndef ERROR
#define ERROR                                   (-1)
#endif

/* byte order convert */
#ifndef HTON_S
#define HTON_S(X)                               X = (((X & 0x00FF) << 8) | ((X & 0xFF00) >> 8))
#endif
#ifndef HTON_L
#define HTON_L(X)                               X = (((X & 0x000000FF) << 24) | \
                                                     ((X & 0x0000FF00) << 8) | \
                                                     ((X & 0x00FF0000) >> 8) | \
                                                     ((X & 0xFF000000) >> 24))
#endif

/* MAC address length */
#ifndef MAC_ADDR_LEN
#define MAC_ADDR_LEN                            6
#endif

/* eCPRI buffer address & size(64MB) */
#define ECPRI_BUF_ADDR                          0xAEE00000ul
#define ECPRI_BUF_SIZE                          0x04000000ul

/* eCPRI RX buffer address & size(63MB) */
#define ECPRI_RX_BUF_ADDR                       ECPRI_BUF_ADDR
#define ECPRI_RX_BUF_SIZE                       0x03F00000ul

/* eCPRI TX buffer address & size(512KB) */
#define ECPRI_TX_BUF_ADDR                       (ECPRI_RX_BUF_ADDR + ECPRI_RX_BUF_SIZE)
#define ECPRI_TX_BUF_SIZE                       0x00080000ul

/* eCPRI antenna config data address & size(512KB) */
#define ECPRI_ANT_BUF_ADDR                      (ECPRI_TX_BUF_ADDR + ECPRI_TX_BUF_SIZE)
#define ECPRI_ANT_BUF_SIZE                      0x00080000ul

/* eCPRI packet size(16KB) */
#define ECPRI_PKT_SIZE                          0x00004000ul

/* eCPRI packet max number */
#define ECPRI_PKT_MAX                           (ECPRI_RX_BUF_SIZE / ECPRI_PKT_SIZE)

/* eCPRI descriptor address unit（1:32B） */
#define ECPRI_DESC_ADDR_UNIT                    32

/* eCPRI 10G & 25G antenna number */
#define ECPRI_10G_ANT_NUM                       4
#define ECPRI_25G_ANT_NUM                       8
#define ECPRI_ANT_NUM_MAX                       ECPRI_25G_ANT_NUM

/* eCPRI slot number */
#define ECPRI_SLOT_NUM                          20 // LTE 10

/* eCPRI slot type - odd/even */
#define ECPRI_SLOT_TYPE                         2
#define ECPRI_SLOT_ODD                          0
#define ECPRI_SLOT_EVEN                         1

/* eCPRI symbol number */
#define ECPRI_SYM_NUM                           14

/* eCPRI symbol info length(packet address + packet length) */
#define ECPRI_SYM_INFO_LEN                      8

/* eCPRI debug ECS_RFM0 index & address - 0xB7E30000 ~ 0xB7E30FFC(X = 0 ~ 0x3FF) */
#define ECPRI_DBG_IDX0(X)                       DBG_DDR_ECPRI_IDX(0, X)
#define ECPRI_DBG_ADDR0(X)                      (DBG_DDR_ADDR_BASE + ECPRI_DBG_IDX0(X) * 4)

/* eCPRI debug ECS_RFM1 index & address - 0xB7E31000 ~ 0xB7E31FFC(X = 0 ~ 0x3FF) */
#define ECPRI_DBG_IDX1(X)                       DBG_DDR_ECPRI_IDX(1, X)
#define ECPRI_DBG_ADDR1(X)                      (DBG_DDR_ADDR_BASE + ECPRI_DBG_IDX1(X) * 4)

/* eCPRI test index & address - 0xB7E32000(X = 0 ~ 0x7FFF) */
#define ECPRI_TEST_IDX(X)                       DBG_DDR_ECPRI_IDX(2, X)
#define ECPRI_TEST_ADDR(X)                      (DBG_DDR_ADDR_BASE + ECPRI_TEST_IDX(X) * 4)

/* eCPRI test ECS_RFM0 index & address - 0xB7E32000 ~ 0xB7E41FFC(X = 0 ~ 0x3FFF) */
#define ECPRI_TEST_IDX0(X)                      ECPRI_TEST_IDX(X)
#define ECPRI_TEST_ADDR0(X)                     ECPRI_TEST_ADDR(X)

/* eCPRI test ECS_RFM1 index & address - 0xB7E42000 ~ 0xB7E51FFC(X = 0 ~ 0x3FFF) */
#define ECPRI_TEST_IDX1(X)                      ECPRI_TEST_IDX(X + 0x4000)
#define ECPRI_TEST_ADDR1(X)                     ECPRI_TEST_ADDR(X + 0x4000)

/* APE cycle(1.4GHz) */
#define apecycle(x)                             (*(uint32_t *)(x) = do_read_volatile(&APC0_CNT0_TSG_CTRL_CNTCVL))
#define APE_CYCLE_MAX                           0x100000000ull

/* eCPRI CSU mode */
#define ECPRI_CSU_REG                           0
#define ECPRI_CSU_LINK                          1
#define ECPRI_CSU_MODE                          ECPRI_CSU_LINK


/* -------------------------------------------------- <TYPE DEF> --------------------------------------------------- */
/* eCPRI time statistic structure */
typedef struct EcpriTimeStats
{
    uint32_t time_cur;                              /* current time comsumption */
    uint32_t time_max;                              /* maximum time comsumption */
    uint32_t time_min;                              /* minimum time comsumption */
} EcpriTimeStats_t;


/* --------------------------------------------------- <DECLARE> --------------------------------------------------- */


#endif /* _ECPRI_COMM_H_ */

