183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 25a822118SMateusz Kulikowski /* 35a822118SMateusz Kulikowski * Qualcomm EHCI driver 45a822118SMateusz Kulikowski * 55a822118SMateusz Kulikowski * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> 65a822118SMateusz Kulikowski * 75a822118SMateusz Kulikowski * Based on Linux driver 85a822118SMateusz Kulikowski */ 95a822118SMateusz Kulikowski 105a822118SMateusz Kulikowski #include <common.h> 115a822118SMateusz Kulikowski #include <dm.h> 125a822118SMateusz Kulikowski #include <errno.h> 135a822118SMateusz Kulikowski #include <fdtdec.h> 14b08c8c48SMasahiro Yamada #include <linux/libfdt.h> 155a822118SMateusz Kulikowski #include <usb.h> 165a822118SMateusz Kulikowski #include <usb/ehci-ci.h> 175a822118SMateusz Kulikowski #include <usb/ulpi.h> 185a822118SMateusz Kulikowski #include <wait_bit.h> 195a822118SMateusz Kulikowski #include <asm/gpio.h> 205a822118SMateusz Kulikowski #include <asm/io.h> 215a822118SMateusz Kulikowski #include <linux/compat.h> 225a822118SMateusz Kulikowski #include "ehci.h" 235a822118SMateusz Kulikowski 245a822118SMateusz Kulikowski struct msm_ehci_priv { 255a822118SMateusz Kulikowski struct ehci_ctrl ctrl; /* Needed by EHCI */ 265a822118SMateusz Kulikowski struct usb_ehci *ehci; /* Start of IP core*/ 275a822118SMateusz Kulikowski struct ulpi_viewport ulpi_vp; /* ULPI Viewport */ 280ac0b6ebSRamon Fried struct phy phy; 295a822118SMateusz Kulikowski }; 305a822118SMateusz Kulikowski 315a822118SMateusz Kulikowski static int msm_init_after_reset(struct ehci_ctrl *dev) 325a822118SMateusz Kulikowski { 335a822118SMateusz Kulikowski struct msm_ehci_priv *p = container_of(dev, struct msm_ehci_priv, ctrl); 345a822118SMateusz Kulikowski struct usb_ehci *ehci = p->ehci; 355a822118SMateusz Kulikowski 360ac0b6ebSRamon Fried generic_phy_reset(&p->phy); 375a822118SMateusz Kulikowski 385a822118SMateusz Kulikowski /* set mode to host controller */ 395a822118SMateusz Kulikowski writel(CM_HOST, &ehci->usbmode); 405a822118SMateusz Kulikowski 415a822118SMateusz Kulikowski return 0; 425a822118SMateusz Kulikowski } 435a822118SMateusz Kulikowski 445a822118SMateusz Kulikowski static const struct ehci_ops msm_ehci_ops = { 455a822118SMateusz Kulikowski .init_after_reset = msm_init_after_reset 465a822118SMateusz Kulikowski }; 475a822118SMateusz Kulikowski 485a822118SMateusz Kulikowski static int ehci_usb_probe(struct udevice *dev) 495a822118SMateusz Kulikowski { 505a822118SMateusz Kulikowski struct msm_ehci_priv *p = dev_get_priv(dev); 515a822118SMateusz Kulikowski struct usb_ehci *ehci = p->ehci; 520683b27aSRamon Fried struct usb_platdata *plat = dev_get_platdata(dev); 535a822118SMateusz Kulikowski struct ehci_hccr *hccr; 545a822118SMateusz Kulikowski struct ehci_hcor *hcor; 555a822118SMateusz Kulikowski int ret; 565a822118SMateusz Kulikowski 575a822118SMateusz Kulikowski hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength); 585a822118SMateusz Kulikowski hcor = (struct ehci_hcor *)((phys_addr_t)hccr + 595a822118SMateusz Kulikowski HC_LENGTH(ehci_readl(&(hccr)->cr_capbase))); 605a822118SMateusz Kulikowski 610ac0b6ebSRamon Fried ret = ehci_setup_phy(dev, &p->phy, 0); 620ac0b6ebSRamon Fried if (ret) 630ac0b6ebSRamon Fried return ret; 640ac0b6ebSRamon Fried 650683b27aSRamon Fried ret = board_usb_init(0, plat->init_type); 665a822118SMateusz Kulikowski if (ret < 0) 675a822118SMateusz Kulikowski return ret; 685a822118SMateusz Kulikowski 690683b27aSRamon Fried return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0, 700683b27aSRamon Fried plat->init_type); 715a822118SMateusz Kulikowski } 725a822118SMateusz Kulikowski 735a822118SMateusz Kulikowski static int ehci_usb_remove(struct udevice *dev) 745a822118SMateusz Kulikowski { 755a822118SMateusz Kulikowski struct msm_ehci_priv *p = dev_get_priv(dev); 765a822118SMateusz Kulikowski struct usb_ehci *ehci = p->ehci; 775a822118SMateusz Kulikowski int ret; 785a822118SMateusz Kulikowski 795a822118SMateusz Kulikowski ret = ehci_deregister(dev); 805a822118SMateusz Kulikowski if (ret) 815a822118SMateusz Kulikowski return ret; 825a822118SMateusz Kulikowski 835a822118SMateusz Kulikowski /* Stop controller. */ 845a822118SMateusz Kulikowski clrbits_le32(&ehci->usbcmd, CMD_RUN); 855a822118SMateusz Kulikowski 860ac0b6ebSRamon Fried ret = ehci_shutdown_phy(dev, &p->phy); 870ac0b6ebSRamon Fried if (ret) 880ac0b6ebSRamon Fried return ret; 895a822118SMateusz Kulikowski 90cd8c3aecSRamon Fried ret = board_usb_init(0, USB_INIT_DEVICE); /* Board specific hook */ 915a822118SMateusz Kulikowski if (ret < 0) 925a822118SMateusz Kulikowski return ret; 935a822118SMateusz Kulikowski 945a822118SMateusz Kulikowski /* Reset controller */ 955a822118SMateusz Kulikowski setbits_le32(&ehci->usbcmd, CMD_RESET); 965a822118SMateusz Kulikowski 975a822118SMateusz Kulikowski /* Wait for reset */ 9848263504SÁlvaro Fernández Rojas if (wait_for_bit_le32(&ehci->usbcmd, CMD_RESET, false, 30, false)) { 995a822118SMateusz Kulikowski printf("Stuck on USB reset.\n"); 1005a822118SMateusz Kulikowski return -ETIMEDOUT; 1015a822118SMateusz Kulikowski } 1025a822118SMateusz Kulikowski 1035a822118SMateusz Kulikowski return 0; 1045a822118SMateusz Kulikowski } 1055a822118SMateusz Kulikowski 1065a822118SMateusz Kulikowski static int ehci_usb_ofdata_to_platdata(struct udevice *dev) 1075a822118SMateusz Kulikowski { 1085a822118SMateusz Kulikowski struct msm_ehci_priv *priv = dev_get_priv(dev); 1095a822118SMateusz Kulikowski 1105a822118SMateusz Kulikowski priv->ulpi_vp.port_num = 0; 111a821c4afSSimon Glass priv->ehci = (void *)devfdt_get_addr(dev); 1125a822118SMateusz Kulikowski 1135a822118SMateusz Kulikowski if (priv->ehci == (void *)FDT_ADDR_T_NONE) 1145a822118SMateusz Kulikowski return -EINVAL; 1155a822118SMateusz Kulikowski 1165a822118SMateusz Kulikowski /* Warning: this will not work if viewport address is > 64 bit due to 1175a822118SMateusz Kulikowski * ULPI design. 1185a822118SMateusz Kulikowski */ 1195a822118SMateusz Kulikowski priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint; 1205a822118SMateusz Kulikowski 1215a822118SMateusz Kulikowski return 0; 1225a822118SMateusz Kulikowski } 1235a822118SMateusz Kulikowski 124*24667041SRamon Fried #if defined(CONFIG_CI_UDC) 125*24667041SRamon Fried /* Little quirk that MSM needs with Chipidea controller 126*24667041SRamon Fried * Must reinit phy after reset 127*24667041SRamon Fried */ 128*24667041SRamon Fried void ci_init_after_reset(struct ehci_ctrl *ctrl) 129*24667041SRamon Fried { 130*24667041SRamon Fried struct msm_ehci_priv *p = ctrl->priv; 131*24667041SRamon Fried 132*24667041SRamon Fried generic_phy_reset(&p->phy); 133*24667041SRamon Fried } 134*24667041SRamon Fried #endif 135*24667041SRamon Fried 1365a822118SMateusz Kulikowski static const struct udevice_id ehci_usb_ids[] = { 1375a822118SMateusz Kulikowski { .compatible = "qcom,ehci-host", }, 1385a822118SMateusz Kulikowski { } 1395a822118SMateusz Kulikowski }; 1405a822118SMateusz Kulikowski 1415a822118SMateusz Kulikowski U_BOOT_DRIVER(usb_ehci) = { 1425a822118SMateusz Kulikowski .name = "ehci_msm", 1435a822118SMateusz Kulikowski .id = UCLASS_USB, 1445a822118SMateusz Kulikowski .of_match = ehci_usb_ids, 1455a822118SMateusz Kulikowski .ofdata_to_platdata = ehci_usb_ofdata_to_platdata, 1465a822118SMateusz Kulikowski .probe = ehci_usb_probe, 1475a822118SMateusz Kulikowski .remove = ehci_usb_remove, 1485a822118SMateusz Kulikowski .ops = &ehci_usb_ops, 1495a822118SMateusz Kulikowski .priv_auto_alloc_size = sizeof(struct msm_ehci_priv), 1502df49234SRamon Fried .platdata_auto_alloc_size = sizeof(struct usb_platdata), 1515a822118SMateusz Kulikowski .flags = DM_FLAG_ALLOC_PRIV_DMA, 1525a822118SMateusz Kulikowski }; 153