static bool musb_is_host(void)
{
	u8 devctl = 0;
	int iddig_state = 1;
	bool usb_is_host = 0;

	DBG(0, "will mask PMIC charger detection\n");
#ifndef FPGA_PLATFORM
	pmic_chrdet_int_en(0);
#endif

	musb_platform_enable(mtk_musb);

#ifdef ID_PIN_USE_EX_EINT
#ifndef CONFIG_MTK_FPGA
	#ifdef CONFIG_OF
	#if defined(CONFIG_MTK_LEGACY)
	iddig_state = mt_get_gpio_in(iddig_pin);
	#else
	iddig_state = __gpio_get_value(iddig_pin);
	#endif
	#else
	iddig_state = mt_get_gpio_in(GPIO_OTG_IDDIG_EINT_PIN);
	#endif
	DBG(0, "iddig_state = %d\n", iddig_state);
#endif
#else
	iddig_state = 0;
	devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
	DBG(0, "devctl = %x before end session\n", devctl);
	devctl &= ~MUSB_DEVCTL_SESSION;	/* this will cause A-device change back to B-device after A-cable plug out */
	musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, devctl);
	msleep(delay_time);

	devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
	DBG(0, "devctl = %x before set session\n", devctl);

	devctl |= MUSB_DEVCTL_SESSION;
	musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, devctl);
	msleep(delay_time1);
	devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
	DBG(0, "devclt = %x\n", devctl);
#endif

	if (devctl & MUSB_DEVCTL_BDEVICE || iddig_state) {
		DBG(0, "will unmask PMIC charger detection\n");
#ifndef FPGA_PLATFORM
		pmic_chrdet_int_en(1);
#endif
		usb_is_host = false;
	} else {
		usb_is_host = true;
	}

	DBG(0, "usb_is_host = %d\n", usb_is_host);
	return usb_is_host;
}
void usb_phy_switch_to_uart(void)
{
	if (usb_phy_check_in_uart_mode())
		return;
	DBG(0, "Mask PMIC charger detection in UART mode.\n");
	pmic_chrdet_int_en(0);

	usb_enable_clock(true);
	udelay(50);

	/* RG_USB20_BC11_SW_EN = 1'b0 */
	USBPHY_CLR8(0x1a, 0x80);

	/* Set RG_SUSPENDM to 1 */
	USBPHY_SET8(0x68, 0x08);

	/* force suspendm = 1 */
	USBPHY_SET8(0x6a, 0x04);

	/* Set ru_uart_mode to 2'b01 */
	USBPHY_SET8(0x6B, 0x5C);

	/* Set RG_UART_EN to 1 */
	USBPHY_SET8(0x6E, 0x07);

	/* Set RG_USB20_DM_100K_EN to 1 */
	USBPHY_SET8(0x22, 0x02);
	usb_enable_clock(false);

	/* GPIO Selection */
	DRV_WriteReg32(ap_uart0_base + 0xB0, 0x1);
}
int musb_otg_env_init(void){
    u8 power;
    //u8 intrusb;
    //step1: mask the PMU/PMIC EINT
    mtk_musb->usb_if = true;
    mtk_musb->is_host = true;//workaround for PMIC charger detection
    //mt65xx_eint_mask(EINT_CHR_DET_NUM);

    pmic_chrdet_int_en(0);

    mt_usb_init_drvvbus();

    //step5: make sure to power on the USB module
    if(mtk_musb->power)
        mtk_musb->power = FALSE;

    musb_platform_enable(mtk_musb);
    //step6: clear session bit
    musb_writeb(mtk_musb->mregs,MUSB_DEVCTL,0);
    //step7: disable and enable usb interrupt
    usb_l1intm_store = musb_readl(mtk_musb->mregs,USB_L1INTM);
    usb_intrrxe_store = musb_readw(mtk_musb->mregs,MUSB_INTRRXE);
    usb_intrtxe_store = musb_readw(mtk_musb->mregs,MUSB_INTRTXE);
    usb_intrusbe_store = musb_readb(mtk_musb->mregs,MUSB_INTRUSBE);

    musb_writel(mtk_musb->mregs,USB_L1INTM,0);
    musb_writew(mtk_musb->mregs,MUSB_INTRRXE,0);
    musb_writew(mtk_musb->mregs,MUSB_INTRTXE,0);
    musb_writeb(mtk_musb->mregs,MUSB_INTRUSBE,0);
    musb_writew(mtk_musb->mregs,MUSB_INTRRX,0xffff);
    musb_writew(mtk_musb->mregs,MUSB_INTRTX,0xffff);
    musb_writeb(mtk_musb->mregs,MUSB_INTRUSB,0xff);
    free_irq (mtk_musb->nIrq, mtk_musb);
    musb_writel(mtk_musb->mregs,USB_L1INTM,0x105);
    musb_writew(mtk_musb->mregs,MUSB_INTRTXE,1);
    musb_writeb(mtk_musb->mregs,MUSB_INTRUSBE,0xf7);
    //setp8: set the index to 0 for ep0, maybe no need. Designers said it is better not to use the index register.
    musb_writeb(mtk_musb->mregs, MUSB_INDEX, 0);
    //setp9: init message
    g_otg_message.msg = 0;
    spin_lock_init(&g_otg_message.lock);

    init_completion(&stop_event);
    #ifdef DX_DBG
    power = musb_readb(mtk_musb->mregs,MUSB_POWER);
    DBG(0,"start the USB-IF test in EM,power=0x%x!\n",power);
    #endif

    return 0;
}
void usb_phy_switch_to_usb(void)
{
	/* GPIO Selection */
	DRV_WriteReg32(ap_uart0_base + 0xB0, 0x0);

	usb_enable_clock(true);
	udelay(50);
	/* clear force_uart_en */
	USBPHY_WRITE8(0x6B, 0x00);
	usb_enable_clock(false);
	usb_phy_poweron();
	/* disable the USB clock turned on in usb_phy_poweron() */
	usb_enable_clock(false);

	DBG(0, "Unmask PMIC charger detection in USB mode.\n");
	pmic_chrdet_int_en(1);
}
int musb_otg_env_exit(void){
    DBG(0,"stop the USB-IF test in EM!\n");
    musb_writel(mtk_musb->mregs,USB_L1INTM,0);
    musb_writew(mtk_musb->mregs,MUSB_INTRRXE,0);
    musb_writew(mtk_musb->mregs,MUSB_INTRTXE,0);
    musb_writeb(mtk_musb->mregs,MUSB_INTRUSBE,0);
    musb_writew(mtk_musb->mregs,MUSB_INTRRX,0xffff);
    musb_writew(mtk_musb->mregs,MUSB_INTRTX,0xffff);
    musb_writeb(mtk_musb->mregs,MUSB_INTRUSB,0xff);
    musb_writel(mtk_musb->mregs,USB_L1INTM,usb_l1intm_store);
    musb_writew(mtk_musb->mregs,MUSB_INTRRXE,usb_intrrxe_store);
    musb_writew(mtk_musb->mregs,MUSB_INTRTXE,usb_intrtxe_store);
    musb_writeb(mtk_musb->mregs,MUSB_INTRUSBE,usb_intrusbe_store);
    mtk_musb->usb_if = false;
    mtk_musb->is_host = false;
    pmic_chrdet_int_en(1);
    return 0;
}
bool usb_phy_check_in_uart_mode(void)
{
	UINT8 usb_port_mode;

	usb_enable_clock(true);
	udelay(50);
	usb_port_mode = USBPHY_READ8(0x6B);
	usb_enable_clock(false);

	if ((usb_port_mode == 0x5C) || (usb_port_mode == 0x5E)) {
		DBG(0, "%s:%d - IN UART MODE : 0x%x\n", __func__, __LINE__, usb_port_mode);
		DBG(0, "Mask PMIC charger detection in UART mode.\n");
		pmic_chrdet_int_en(0);
		in_uart_mode = true;
	} else {
		DBG(0, "%s:%d - NOT IN UART MODE : 0x%x\n", __func__, __LINE__, usb_port_mode);
		in_uart_mode = false;
	}
	return in_uart_mode;
}