bcp_si.c 4.85 KB


/*************************************************************************
*
*  File: bcp_si.c
*
*  Whatever platform it is:
*      MSB --- bit 31
*      LSB --- bit 0
*
*/

#include <stdlib.h>
#include <stdio.h>
#include "simipc.h"
#include "iotest.h"
#include "trace.h"
#include  <errno.h>
#include  <memory.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <fcntl.h>
#include "iomap.h"
#include "bcp_util.h"

int linux_IO_Request(IpcPkt *req, IpcPkt *rsp);

#define BDSIDBG  1

int bd_si_ctrl_setup(int ctrl, int func, int data, int extra)
{
    IpcPkt          req, rsp;

    //assert((ctrl >= LOCAL_CTRL) && (ctrl <= JCTRL_3));
    //assert((func  >= CRTL_ENALE) && (ctrl <= CTRL_FRAMEERR));

    memset(&req, 0, sizeof(IpcPkt));
    memset(&rsp, 0, sizeof(IpcPkt));

    req.length = htonl(sizeof(IpcPkt));
    req.code   = htonl(BD_REQ_SI_CTRL);
    req.size   = htonl(func);
    req.address = htonl(ctrl);
    req.data[0] = htonl(data);

    if (linux_IO_Request(&req, &rsp) < 0) {
        fprintf(LogFp, "Fatal error: Cannot Write to BCP SI Backdoor!\n");
        return -1;
    }

    if ((ntohl(rsp.code) & 0x1ff) != RSP_OK) {
        fprintf(LogFp, "bd_si_ctrl_setup Fatal error: rsp.code %d\n", ntohl(rsp.code));
        return -1;
    }

    return 0;
}


int bd_si_ctrl_rd(unsigned int *result, int bytes)
{
    /* this routine only works for MJCTRL response data reads
     * use bd_si_ctrl_rd_pkd for general si bd reads
     *
     * returns 1st transmited byte in most significant byte of result
     */
    IpcPkt  req, rsp;
    int     i;
    u8      data[8];

    memset(&req, 0, sizeof(IpcPkt));
    memset(&rsp, 0, sizeof(IpcPkt));

    req.length = htonl(sizeof(IpcPkt));
    req.code = htonl(BD_RD_SI_CTRL);
    req.size = htonl(bytes);

    if (linux_IO_Request(&req, &rsp) < 0) {
        fprintf(LogFp, "bd_si_ctrl_rd Fatal error: Cannot get data!\n");
        return -1;
    }

    if ((ntohl(rsp.code) & 0x1ff) != RSP_DATA) {
        fprintf(LogFp, "bd_si_ctrl_rd Fatal error: rsp.code %d\n", ntohl(rsp.code));
        return -1;
    }

    for(i=0; i<4; i++)
        *((u32*)&data[i]) = ntohl(rsp.data[i]);
        
    *result = htonl(*((u32*)&data[0]));

    #if BDSIDBG
    fprintf(LogFp,"\nbd_si_ctrl_rd:  rsp.data[0]=%08x  ntohl=%08x",rsp.data[0], ntohl(rsp.data[0]));
    fprintf(LogFp,"\nbd_si_ctrl_rd:  rsp.data[1]=%08x  ntohl=%08x",rsp.data[1], ntohl(rsp.data[1]));
    fprintf(LogFp,"\nbd_si_ctrl_rd:  rsp.data[2]=%08x  ntohl=%08x",rsp.data[2], ntohl(rsp.data[2]));
    fprintf(LogFp,"\nbd_si_ctrl_rd:  rsp.data[3]=%08x  ntohl=%08x",rsp.data[3], ntohl(rsp.data[3]));
    fprintf(LogFp,"\nbd_si_ctrl_rd:  result=%08x",*result);
    #endif

    return 0;
}


int bd_si_ctrl_wr_pkd(int ctrl, int func, unsigned char *data, int bytes)
{
    IpcPkt  req, rsp;
    int     i;

    memset(&req, 0, sizeof(IpcPkt));
    memset(&rsp, 0, sizeof(IpcPkt));

    req.length = htonl(sizeof(IpcPkt));
    req.code   = htonl(BD_REQ_SI_CTRL);
    req.size   = htonl(func);
    req.address = htonl(ctrl);

    for(i=0; i<bytes && i/4<8; i+=4) {
        req.data[i/4] = htonl(*((u32*)(&data[i])));
        #if BDSIDBG
        fprintf(LogFp,"\nbd_si_ctrl_wr_pkd:  req.data[%d]=%08x  *((u32*)(&data[%d]))=%08lx",i/4,req.data[i/4],i,*((u32*)(&data[i])));
        #endif
    }

    if (linux_IO_Request(&req, &rsp) < 0) {
        fprintf(LogFp, "bd_si_ctrl_bsetup Fatal error: Cannot Write to BCP SI Backdoor!\n");
        return -1;
    }

    if ((ntohl(rsp.code) & 0x1ff) != RSP_OK) {
        fprintf(LogFp, "bd_si_ctrl_bsetup Fatal error: rsp.code %d\n", ntohl(rsp.code));
        return -1;
    }

    return 0;
}


int bd_si_ctrl_set_mm_tx_data(unsigned char mm_tx_data[SI_MM_TX_DATA_SIZE])
{
    int     ret;

    ret =        bd_si_ctrl_wr_pkd(JCTRL_1, CTRL_TX_DATA_0_7,  &mm_tx_data[0], 8);
    if(!ret) ret=bd_si_ctrl_wr_pkd(JCTRL_1, CTRL_TX_DATA_8_35, &mm_tx_data[8], 28);
    return ret;
}


int bd_si_ctrl_rd_pkd(int ctrl, int func, unsigned char *result, int bytes)
{
    IpcPkt  req, rsp;
    int     i, j;

    memset(&req, 0, sizeof(IpcPkt));
    memset(&rsp, 0, sizeof(IpcPkt));

    req.length = htonl(sizeof(IpcPkt));
    req.code = htonl(BD_RD_SI_CTRL_PKD);
    req.size   = htonl(func);
    req.address = htonl(ctrl);

    if (linux_IO_Request(&req, &rsp) < 0) {
        fprintf(LogFp, "bd_si_ctrl_rd Fatal error: Cannot get data!\n");
        return -1;
    }

    if ((ntohl(rsp.code) & 0x1ff) != RSP_DATA) {
        fprintf(LogFp, "bd_si_ctrl_rd Fatal error: rsp.code %d\n", ntohl(rsp.code));
        return -1;
    }

    for(i=0; i*4<bytes && i<8; i++) {
        u8  data[4];
        #if BDSIDBG
        fprintf(LogFp,"\nbd_si_ctrl_rd_pkd:  rsp.data[%d]=%08x  ntohl=%08x",i,rsp.data[i],ntohl(rsp.data[i]));
        #endif
        *((u32*)&data[0]) = ntohl(rsp.data[i]);
        for(j=0; j<4 && i*4+j<bytes; j++) {
            result[i*4+j] = data[j];
        }
    }

    return 0;
}