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