xref: /openbmc/u-boot/drivers/usb/host/ehci-msm.c (revision 0ac0b6eb)
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 */
28*0ac0b6ebSRamon 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 
36*0ac0b6ebSRamon 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;
525a822118SMateusz Kulikowski 	struct ehci_hccr *hccr;
535a822118SMateusz Kulikowski 	struct ehci_hcor *hcor;
545a822118SMateusz Kulikowski 	int ret;
555a822118SMateusz Kulikowski 
565a822118SMateusz Kulikowski 	hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength);
575a822118SMateusz Kulikowski 	hcor = (struct ehci_hcor *)((phys_addr_t)hccr +
585a822118SMateusz Kulikowski 			HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
595a822118SMateusz Kulikowski 
60*0ac0b6ebSRamon Fried 	ret = ehci_setup_phy(dev, &p->phy, 0);
61*0ac0b6ebSRamon Fried 	if (ret)
62*0ac0b6ebSRamon Fried 		return ret;
63*0ac0b6ebSRamon Fried 
64cd8c3aecSRamon Fried 	ret = board_usb_init(0, USB_INIT_HOST);
655a822118SMateusz Kulikowski 	if (ret < 0)
665a822118SMateusz Kulikowski 		return ret;
675a822118SMateusz Kulikowski 
685a822118SMateusz Kulikowski 	return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0, USB_INIT_HOST);
695a822118SMateusz Kulikowski }
705a822118SMateusz Kulikowski 
715a822118SMateusz Kulikowski static int ehci_usb_remove(struct udevice *dev)
725a822118SMateusz Kulikowski {
735a822118SMateusz Kulikowski 	struct msm_ehci_priv *p = dev_get_priv(dev);
745a822118SMateusz Kulikowski 	struct usb_ehci *ehci = p->ehci;
755a822118SMateusz Kulikowski 	int ret;
765a822118SMateusz Kulikowski 
775a822118SMateusz Kulikowski 	ret = ehci_deregister(dev);
785a822118SMateusz Kulikowski 	if (ret)
795a822118SMateusz Kulikowski 		return ret;
805a822118SMateusz Kulikowski 
815a822118SMateusz Kulikowski 	/* Stop controller. */
825a822118SMateusz Kulikowski 	clrbits_le32(&ehci->usbcmd, CMD_RUN);
835a822118SMateusz Kulikowski 
84*0ac0b6ebSRamon Fried 	ret = ehci_shutdown_phy(dev, &p->phy);
85*0ac0b6ebSRamon Fried 	if (ret)
86*0ac0b6ebSRamon Fried 		return ret;
875a822118SMateusz Kulikowski 
88cd8c3aecSRamon Fried 	ret = board_usb_init(0, USB_INIT_DEVICE); /* Board specific hook */
895a822118SMateusz Kulikowski 	if (ret < 0)
905a822118SMateusz Kulikowski 		return ret;
915a822118SMateusz Kulikowski 
925a822118SMateusz Kulikowski 	/* Reset controller */
935a822118SMateusz Kulikowski 	setbits_le32(&ehci->usbcmd, CMD_RESET);
945a822118SMateusz Kulikowski 
955a822118SMateusz Kulikowski 	/* Wait for reset */
9648263504SÁlvaro Fernández Rojas 	if (wait_for_bit_le32(&ehci->usbcmd, CMD_RESET, false, 30, false)) {
975a822118SMateusz Kulikowski 		printf("Stuck on USB reset.\n");
985a822118SMateusz Kulikowski 		return -ETIMEDOUT;
995a822118SMateusz Kulikowski 	}
1005a822118SMateusz Kulikowski 
1015a822118SMateusz Kulikowski 	return 0;
1025a822118SMateusz Kulikowski }
1035a822118SMateusz Kulikowski 
1045a822118SMateusz Kulikowski static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
1055a822118SMateusz Kulikowski {
1065a822118SMateusz Kulikowski 	struct msm_ehci_priv *priv = dev_get_priv(dev);
1075a822118SMateusz Kulikowski 
1085a822118SMateusz Kulikowski 	priv->ulpi_vp.port_num = 0;
109a821c4afSSimon Glass 	priv->ehci = (void *)devfdt_get_addr(dev);
1105a822118SMateusz Kulikowski 
1115a822118SMateusz Kulikowski 	if (priv->ehci == (void *)FDT_ADDR_T_NONE)
1125a822118SMateusz Kulikowski 		return -EINVAL;
1135a822118SMateusz Kulikowski 
1145a822118SMateusz Kulikowski 	/* Warning: this will not work if viewport address is > 64 bit due to
1155a822118SMateusz Kulikowski 	 * ULPI design.
1165a822118SMateusz Kulikowski 	 */
1175a822118SMateusz Kulikowski 	priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint;
1185a822118SMateusz Kulikowski 
1195a822118SMateusz Kulikowski 	return 0;
1205a822118SMateusz Kulikowski }
1215a822118SMateusz Kulikowski 
1225a822118SMateusz Kulikowski static const struct udevice_id ehci_usb_ids[] = {
1235a822118SMateusz Kulikowski 	{ .compatible = "qcom,ehci-host", },
1245a822118SMateusz Kulikowski 	{ }
1255a822118SMateusz Kulikowski };
1265a822118SMateusz Kulikowski 
1275a822118SMateusz Kulikowski U_BOOT_DRIVER(usb_ehci) = {
1285a822118SMateusz Kulikowski 	.name	= "ehci_msm",
1295a822118SMateusz Kulikowski 	.id	= UCLASS_USB,
1305a822118SMateusz Kulikowski 	.of_match = ehci_usb_ids,
1315a822118SMateusz Kulikowski 	.ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
1325a822118SMateusz Kulikowski 	.probe = ehci_usb_probe,
1335a822118SMateusz Kulikowski 	.remove = ehci_usb_remove,
1345a822118SMateusz Kulikowski 	.ops	= &ehci_usb_ops,
1355a822118SMateusz Kulikowski 	.priv_auto_alloc_size = sizeof(struct msm_ehci_priv),
1362df49234SRamon Fried 	.platdata_auto_alloc_size = sizeof(struct usb_platdata),
1375a822118SMateusz Kulikowski 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
1385a822118SMateusz Kulikowski };
139