/*
* @file hv_drv_UsbMusbIO.h
* @brief Header of MUSB driver register I/O.
*
* @author HiView SoC Software Team
* @version 1.0.0
* @date 2022-06-15
*/


#ifndef __HV_DRV_USB_MUSB_IO_H_
#define __HV_DRV_USB_MUSB_IO_H_

#include "hv_vos_Comm.h"
#include "hv_drv_UsbMusbRegs.h"

static inline u16 musb_readw(void *mbase, u16 offset) {
    return HAL_R16((u32)mbase + offset);
}

static inline void musb_writew(void *mbase, u16 offset, u16 data) {
    HAL_W16((u32)mbase + offset, data);
}

static inline void musb_writew_lo(void *mbase, u16 offset, u16 data) {
}

static inline void musb_writew_hi(void *mbase, u16 offset, u16 data) {
}

static inline u32 musb_readl(void *mbase, u16 offset) {
    return 0;
}

static inline void musb_writel(void *mbase, u16 offset, u32 data) {
}

static inline u8 musb_readb(void *mbase, u16 offset) {
    return HAL_R8((u32)mbase + offset);
}

static inline void musb_writeb(void *mbase, u16 offset, u8 data) {
    HAL_W8((u32)mbase + offset, data);
}


static inline void readsb(unsigned int *addr, void *data, int bytelen)
{
    unsigned char *ptr;
    unsigned char *ptr2;

    ptr = (unsigned char *)addr;
    ptr2 = (unsigned char *)data;

    while (bytelen) {
        *ptr2 = *ptr;
        ptr2++;
        bytelen--;
    }
}

static inline void readsw(unsigned int *addr, void *data, int wordlen)
{
    unsigned short *ptr;
    unsigned short *ptr2;

    ptr = (unsigned short *)addr;
    ptr2 = (unsigned short *)data;

    while (wordlen) {
        *ptr2 = *ptr;
        ptr2++;
        wordlen--;
    }
}

static inline void readsl(unsigned int *addr, void *data, int longlen)
{
    unsigned int *ptr;
    unsigned int *ptr2;

    ptr = (unsigned int *)addr;
    ptr2 = (unsigned int *)data;

    while (longlen) {
        *ptr2 = *ptr;
        ptr2++;
        longlen--;
    }
}


static inline void writesb(unsigned int *addr, const void *data, int bytelen)
{
    unsigned char *ptr;
    unsigned char *ptr2;

    ptr = (unsigned char *)addr;
    ptr2 = (unsigned char *)data;

    while (bytelen) {
        *ptr = *ptr2;
        ptr2++;
        bytelen--;
    }
}

static inline void writesw(unsigned int *addr, const void *data, int wordlen)
{
    unsigned short *ptr;
    unsigned short *ptr2;

    ptr = (unsigned short *)addr;
    ptr2 = (unsigned short *)data;

    while (wordlen) {
        *ptr = *ptr2;
        ptr2++;
        wordlen--;
    }
}

static inline void writesl(unsigned int *addr, const void *data, int longlen)
{
    unsigned int *ptr;
    unsigned int *ptr2;

    ptr = (unsigned int *)addr;
    ptr2 = (unsigned int *)data;

    while (longlen) {
        *ptr = *ptr2;
        ptr2++;
        longlen--;
    }
}


static inline void musb_write_txfifosz(void  *mbase, u8 c_size)
{
    musb_writeb(mbase, MUSB_TXFIFOSZ, c_size);
}

static inline void musb_write_txfifoadd(void  *mbase, u16 c_off)
{
    musb_writew(mbase, MUSB_TXFIFOADD, c_off);
}

static inline void musb_write_rxfifosz(void  *mbase, u8 c_size)
{
    musb_writeb(mbase, MUSB_RXFIFOSZ, c_size);
}

static inline void  musb_write_rxfifoadd(void  *mbase, u16 c_off)
{
    musb_writew(mbase, MUSB_RXFIFOADD, c_off);
}

static inline void musb_write_ulpi_buscontrol(void  *mbase, u8 val)
{
    musb_writeb(mbase, MUSB_ULPI_BUSCONTROL, val);
}

static inline u8 musb_read_txfifosz(void  *mbase)
{
    return musb_readb(mbase, MUSB_TXFIFOSZ);
}

static inline u16 musb_read_txfifoadd(void  *mbase)
{
    return musb_readw(mbase, MUSB_TXFIFOADD);
}

static inline u8 musb_read_rxfifosz(void  *mbase)
{
    return musb_readb(mbase, MUSB_RXFIFOSZ);
}

static inline u16  musb_read_rxfifoadd(void  *mbase)
{
    return musb_readw(mbase, MUSB_RXFIFOADD);
}

static inline u8 musb_read_ulpi_buscontrol(void  *mbase)
{
    return musb_readb(mbase, MUSB_ULPI_BUSCONTROL);
}

static inline u8 musb_read_configdata(void  *mbase)
{
    musb_writeb(mbase, MUSB_INDEX, 0);
    return musb_readb(mbase, 0x10 + MUSB_CONFIGDATA);
}

static inline u16 musb_read_hwvers(void  *mbase)
{
    return musb_readw(mbase, MUSB_HWVERS);
}

static inline void  *musb_read_target_reg_base(u8 i, void  *mbase)
{
    return (MUSB_BUSCTL_OFFSET(i, 0) + mbase);
}

static inline void musb_write_rxfunaddr(void  *ep_target_regs,
        u8 qh_addr_reg)
{
    musb_writeb(ep_target_regs, MUSB_RXFUNCADDR, qh_addr_reg);
}

static inline void musb_write_rxhubaddr(void  *ep_target_regs,
        u8 qh_h_addr_reg)
{
    musb_writeb(ep_target_regs, MUSB_RXHUBADDR, qh_h_addr_reg);
}

static inline void musb_write_rxhubport(void  *ep_target_regs,
        u8 qh_h_port_reg)
{
    musb_writeb(ep_target_regs, MUSB_RXHUBPORT, qh_h_port_reg);
}

static inline void  musb_write_txfunaddr(void  *mbase, u8 epnum,
        u8 qh_addr_reg)
{
    musb_writeb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_TXFUNCADDR),
            qh_addr_reg);
}

static inline void  musb_write_txhubaddr(void  *mbase, u8 epnum,
        u8 qh_addr_reg)
{
    musb_writeb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBADDR),
            qh_addr_reg);
}

static inline void  musb_write_txhubport(void  *mbase, u8 epnum,
        u8 qh_h_port_reg)
{
    musb_writeb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBPORT),
            qh_h_port_reg);
}

static inline u8 musb_read_rxfunaddr(void  *mbase, u8 epnum)
{
    return musb_readb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_RXFUNCADDR));
}

static inline u8 musb_read_rxhubaddr(void  *mbase, u8 epnum)
{
    return musb_readb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_RXHUBADDR));
}

static inline u8 musb_read_rxhubport(void  *mbase, u8 epnum)
{
    return musb_readb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_RXHUBPORT));
}

static inline u8  musb_read_txfunaddr(void  *mbase, u8 epnum)
{
    return musb_readb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_TXFUNCADDR));
}

static inline u8  musb_read_txhubaddr(void  *mbase, u8 epnum)
{
    return musb_readb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBADDR));
}

static inline u8  musb_read_txhubport(void  *mbase, u8 epnum)
{
    return musb_readb(mbase, MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBPORT));
}



#endif
