1*83d290c5STom 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 /* PHY viewport regs */ 255a822118SMateusz Kulikowski #define ULPI_MISC_A_READ 0x96 265a822118SMateusz Kulikowski #define ULPI_MISC_A_SET 0x97 275a822118SMateusz Kulikowski #define ULPI_MISC_A_CLEAR 0x98 285a822118SMateusz Kulikowski #define ULPI_MISC_A_VBUSVLDEXTSEL (1 << 1) 295a822118SMateusz Kulikowski #define ULPI_MISC_A_VBUSVLDEXT (1 << 0) 305a822118SMateusz Kulikowski 315a822118SMateusz Kulikowski #define GEN2_SESS_VLD_CTRL_EN (1 << 7) 325a822118SMateusz Kulikowski 335a822118SMateusz Kulikowski #define SESS_VLD_CTRL (1 << 25) 345a822118SMateusz Kulikowski 355a822118SMateusz Kulikowski struct msm_ehci_priv { 365a822118SMateusz Kulikowski struct ehci_ctrl ctrl; /* Needed by EHCI */ 375a822118SMateusz Kulikowski struct usb_ehci *ehci; /* Start of IP core*/ 385a822118SMateusz Kulikowski struct ulpi_viewport ulpi_vp; /* ULPI Viewport */ 395a822118SMateusz Kulikowski }; 405a822118SMateusz Kulikowski 415a822118SMateusz Kulikowski int __weak board_prepare_usb(enum usb_init_type type) 425a822118SMateusz Kulikowski { 435a822118SMateusz Kulikowski return 0; 445a822118SMateusz Kulikowski } 455a822118SMateusz Kulikowski 465a822118SMateusz Kulikowski static void setup_usb_phy(struct msm_ehci_priv *priv) 475a822118SMateusz Kulikowski { 485a822118SMateusz Kulikowski /* Select and enable external configuration with USB PHY */ 495a822118SMateusz Kulikowski ulpi_write(&priv->ulpi_vp, (u8 *)ULPI_MISC_A_SET, 505a822118SMateusz Kulikowski ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT); 515a822118SMateusz Kulikowski } 525a822118SMateusz Kulikowski 535a822118SMateusz Kulikowski static void reset_usb_phy(struct msm_ehci_priv *priv) 545a822118SMateusz Kulikowski { 555a822118SMateusz Kulikowski /* Disable VBUS mimicing in the controller. */ 565a822118SMateusz Kulikowski ulpi_write(&priv->ulpi_vp, (u8 *)ULPI_MISC_A_CLEAR, 575a822118SMateusz Kulikowski ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT); 585a822118SMateusz Kulikowski } 595a822118SMateusz Kulikowski 605a822118SMateusz Kulikowski 615a822118SMateusz Kulikowski static int msm_init_after_reset(struct ehci_ctrl *dev) 625a822118SMateusz Kulikowski { 635a822118SMateusz Kulikowski struct msm_ehci_priv *p = container_of(dev, struct msm_ehci_priv, ctrl); 645a822118SMateusz Kulikowski struct usb_ehci *ehci = p->ehci; 655a822118SMateusz Kulikowski 665a822118SMateusz Kulikowski /* select ULPI phy */ 675a822118SMateusz Kulikowski writel(PORT_PTS_ULPI, &ehci->portsc); 685a822118SMateusz Kulikowski setup_usb_phy(p); 695a822118SMateusz Kulikowski 705a822118SMateusz Kulikowski /* Enable sess_vld */ 715a822118SMateusz Kulikowski setbits_le32(&ehci->genconfig2, GEN2_SESS_VLD_CTRL_EN); 725a822118SMateusz Kulikowski 735a822118SMateusz Kulikowski /* Enable external vbus configuration in the LINK */ 745a822118SMateusz Kulikowski setbits_le32(&ehci->usbcmd, SESS_VLD_CTRL); 755a822118SMateusz Kulikowski 765a822118SMateusz Kulikowski /* USB_OTG_HS_AHB_BURST */ 775a822118SMateusz Kulikowski writel(0x0, &ehci->sbuscfg); 785a822118SMateusz Kulikowski 795a822118SMateusz Kulikowski /* USB_OTG_HS_AHB_MODE: HPROT_MODE */ 805a822118SMateusz Kulikowski /* Bus access related config. */ 815a822118SMateusz Kulikowski writel(0x08, &ehci->sbusmode); 825a822118SMateusz Kulikowski 835a822118SMateusz Kulikowski /* set mode to host controller */ 845a822118SMateusz Kulikowski writel(CM_HOST, &ehci->usbmode); 855a822118SMateusz Kulikowski 865a822118SMateusz Kulikowski return 0; 875a822118SMateusz Kulikowski } 885a822118SMateusz Kulikowski 895a822118SMateusz Kulikowski static const struct ehci_ops msm_ehci_ops = { 905a822118SMateusz Kulikowski .init_after_reset = msm_init_after_reset 915a822118SMateusz Kulikowski }; 925a822118SMateusz Kulikowski 935a822118SMateusz Kulikowski static int ehci_usb_probe(struct udevice *dev) 945a822118SMateusz Kulikowski { 955a822118SMateusz Kulikowski struct msm_ehci_priv *p = dev_get_priv(dev); 965a822118SMateusz Kulikowski struct usb_ehci *ehci = p->ehci; 975a822118SMateusz Kulikowski struct ehci_hccr *hccr; 985a822118SMateusz Kulikowski struct ehci_hcor *hcor; 995a822118SMateusz Kulikowski int ret; 1005a822118SMateusz Kulikowski 1015a822118SMateusz Kulikowski hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength); 1025a822118SMateusz Kulikowski hcor = (struct ehci_hcor *)((phys_addr_t)hccr + 1035a822118SMateusz Kulikowski HC_LENGTH(ehci_readl(&(hccr)->cr_capbase))); 1045a822118SMateusz Kulikowski 1055a822118SMateusz Kulikowski ret = board_prepare_usb(USB_INIT_HOST); 1065a822118SMateusz Kulikowski if (ret < 0) 1075a822118SMateusz Kulikowski return ret; 1085a822118SMateusz Kulikowski 1095a822118SMateusz Kulikowski return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0, USB_INIT_HOST); 1105a822118SMateusz Kulikowski } 1115a822118SMateusz Kulikowski 1125a822118SMateusz Kulikowski static int ehci_usb_remove(struct udevice *dev) 1135a822118SMateusz Kulikowski { 1145a822118SMateusz Kulikowski struct msm_ehci_priv *p = dev_get_priv(dev); 1155a822118SMateusz Kulikowski struct usb_ehci *ehci = p->ehci; 1165a822118SMateusz Kulikowski int ret; 1175a822118SMateusz Kulikowski 1185a822118SMateusz Kulikowski ret = ehci_deregister(dev); 1195a822118SMateusz Kulikowski if (ret) 1205a822118SMateusz Kulikowski return ret; 1215a822118SMateusz Kulikowski 1225a822118SMateusz Kulikowski /* Stop controller. */ 1235a822118SMateusz Kulikowski clrbits_le32(&ehci->usbcmd, CMD_RUN); 1245a822118SMateusz Kulikowski 1255a822118SMateusz Kulikowski reset_usb_phy(p); 1265a822118SMateusz Kulikowski 1275a822118SMateusz Kulikowski ret = board_prepare_usb(USB_INIT_DEVICE); /* Board specific hook */ 1285a822118SMateusz Kulikowski if (ret < 0) 1295a822118SMateusz Kulikowski return ret; 1305a822118SMateusz Kulikowski 1315a822118SMateusz Kulikowski /* Reset controller */ 1325a822118SMateusz Kulikowski setbits_le32(&ehci->usbcmd, CMD_RESET); 1335a822118SMateusz Kulikowski 1345a822118SMateusz Kulikowski /* Wait for reset */ 13548263504SÁlvaro Fernández Rojas if (wait_for_bit_le32(&ehci->usbcmd, CMD_RESET, false, 30, false)) { 1365a822118SMateusz Kulikowski printf("Stuck on USB reset.\n"); 1375a822118SMateusz Kulikowski return -ETIMEDOUT; 1385a822118SMateusz Kulikowski } 1395a822118SMateusz Kulikowski 1405a822118SMateusz Kulikowski return 0; 1415a822118SMateusz Kulikowski } 1425a822118SMateusz Kulikowski 1435a822118SMateusz Kulikowski static int ehci_usb_ofdata_to_platdata(struct udevice *dev) 1445a822118SMateusz Kulikowski { 1455a822118SMateusz Kulikowski struct msm_ehci_priv *priv = dev_get_priv(dev); 1465a822118SMateusz Kulikowski 1475a822118SMateusz Kulikowski priv->ulpi_vp.port_num = 0; 148a821c4afSSimon Glass priv->ehci = (void *)devfdt_get_addr(dev); 1495a822118SMateusz Kulikowski 1505a822118SMateusz Kulikowski if (priv->ehci == (void *)FDT_ADDR_T_NONE) 1515a822118SMateusz Kulikowski return -EINVAL; 1525a822118SMateusz Kulikowski 1535a822118SMateusz Kulikowski /* Warning: this will not work if viewport address is > 64 bit due to 1545a822118SMateusz Kulikowski * ULPI design. 1555a822118SMateusz Kulikowski */ 1565a822118SMateusz Kulikowski priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint; 1575a822118SMateusz Kulikowski 1585a822118SMateusz Kulikowski return 0; 1595a822118SMateusz Kulikowski } 1605a822118SMateusz Kulikowski 1615a822118SMateusz Kulikowski static const struct udevice_id ehci_usb_ids[] = { 1625a822118SMateusz Kulikowski { .compatible = "qcom,ehci-host", }, 1635a822118SMateusz Kulikowski { } 1645a822118SMateusz Kulikowski }; 1655a822118SMateusz Kulikowski 1665a822118SMateusz Kulikowski U_BOOT_DRIVER(usb_ehci) = { 1675a822118SMateusz Kulikowski .name = "ehci_msm", 1685a822118SMateusz Kulikowski .id = UCLASS_USB, 1695a822118SMateusz Kulikowski .of_match = ehci_usb_ids, 1705a822118SMateusz Kulikowski .ofdata_to_platdata = ehci_usb_ofdata_to_platdata, 1715a822118SMateusz Kulikowski .probe = ehci_usb_probe, 1725a822118SMateusz Kulikowski .remove = ehci_usb_remove, 1735a822118SMateusz Kulikowski .ops = &ehci_usb_ops, 1745a822118SMateusz Kulikowski .priv_auto_alloc_size = sizeof(struct msm_ehci_priv), 1755a822118SMateusz Kulikowski .flags = DM_FLAG_ALLOC_PRIV_DMA, 1765a822118SMateusz Kulikowski }; 177