xref: /openbmc/u-boot/drivers/usb/host/ehci-exynos.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
27590d3ceSRajeshwari Shinde /*
37590d3ceSRajeshwari Shinde  * SAMSUNG EXYNOS USB HOST EHCI Controller
47590d3ceSRajeshwari Shinde  *
57590d3ceSRajeshwari Shinde  * Copyright (C) 2012 Samsung Electronics Co.Ltd
67590d3ceSRajeshwari Shinde  *	Vivek Gautam <gautam.vivek@samsung.com>
77590d3ceSRajeshwari Shinde  */
87590d3ceSRajeshwari Shinde 
97590d3ceSRajeshwari Shinde #include <common.h>
10aae04d07SSimon Glass #include <dm.h>
11e18bf1f9SRajeshwari Shinde #include <fdtdec.h>
12b08c8c48SMasahiro Yamada #include <linux/libfdt.h>
13e18bf1f9SRajeshwari Shinde #include <malloc.h>
147590d3ceSRajeshwari Shinde #include <usb.h>
157590d3ceSRajeshwari Shinde #include <asm/arch/cpu.h>
167590d3ceSRajeshwari Shinde #include <asm/arch/ehci.h>
1771045da8SRajeshwari Shinde #include <asm/arch/system.h>
18c48ac113SRajeshwari Shinde #include <asm/arch/power.h>
194a271cb1SJulius Werner #include <asm/gpio.h>
205d97dff0SMasahiro Yamada #include <linux/errno.h>
21e18bf1f9SRajeshwari Shinde #include <linux/compat.h>
227590d3ceSRajeshwari Shinde #include "ehci.h"
237590d3ceSRajeshwari Shinde 
24e18bf1f9SRajeshwari Shinde /* Declare global data pointer */
25e18bf1f9SRajeshwari Shinde DECLARE_GLOBAL_DATA_PTR;
26e18bf1f9SRajeshwari Shinde 
27aae04d07SSimon Glass struct exynos_ehci_platdata {
28aae04d07SSimon Glass 	struct usb_platdata usb_plat;
29aae04d07SSimon Glass 	fdt_addr_t hcd_base;
30aae04d07SSimon Glass 	fdt_addr_t phy_base;
31aae04d07SSimon Glass 	struct gpio_desc vbus_gpio;
32aae04d07SSimon Glass };
33aae04d07SSimon Glass 
34e18bf1f9SRajeshwari Shinde /**
35e18bf1f9SRajeshwari Shinde  * Contains pointers to register base addresses
36e18bf1f9SRajeshwari Shinde  * for the usb controller.
37e18bf1f9SRajeshwari Shinde  */
38e18bf1f9SRajeshwari Shinde struct exynos_ehci {
39aae04d07SSimon Glass 	struct ehci_ctrl ctrl;
40e18bf1f9SRajeshwari Shinde 	struct exynos_usb_phy *usb;
4124a4775fSVivek Gautam 	struct ehci_hccr *hcd;
42e18bf1f9SRajeshwari Shinde };
43e18bf1f9SRajeshwari Shinde 
ehci_usb_ofdata_to_platdata(struct udevice * dev)44aae04d07SSimon Glass static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
45aae04d07SSimon Glass {
46aae04d07SSimon Glass 	struct exynos_ehci_platdata *plat = dev_get_platdata(dev);
47aae04d07SSimon Glass 	const void *blob = gd->fdt_blob;
48aae04d07SSimon Glass 	unsigned int node;
49aae04d07SSimon Glass 	int depth;
50aae04d07SSimon Glass 
51aae04d07SSimon Glass 	/*
52aae04d07SSimon Glass 	 * Get the base address for XHCI controller from the device node
53aae04d07SSimon Glass 	 */
54a821c4afSSimon Glass 	plat->hcd_base = devfdt_get_addr(dev);
55aae04d07SSimon Glass 	if (plat->hcd_base == FDT_ADDR_T_NONE) {
56aae04d07SSimon Glass 		debug("Can't get the XHCI register base address\n");
57aae04d07SSimon Glass 		return -ENXIO;
58aae04d07SSimon Glass 	}
59aae04d07SSimon Glass 
60aae04d07SSimon Glass 	depth = 0;
61e160f7d4SSimon Glass 	node = fdtdec_next_compatible_subnode(blob, dev_of_offset(dev),
62aae04d07SSimon Glass 				COMPAT_SAMSUNG_EXYNOS_USB_PHY, &depth);
63aae04d07SSimon Glass 	if (node <= 0) {
64aae04d07SSimon Glass 		debug("XHCI: Can't get device node for usb3-phy controller\n");
65aae04d07SSimon Glass 		return -ENODEV;
66aae04d07SSimon Glass 	}
67aae04d07SSimon Glass 
68aae04d07SSimon Glass 	/*
69aae04d07SSimon Glass 	 * Get the base address for usbphy from the device node
70aae04d07SSimon Glass 	 */
71aae04d07SSimon Glass 	plat->phy_base = fdtdec_get_addr(blob, node, "reg");
72aae04d07SSimon Glass 	if (plat->phy_base == FDT_ADDR_T_NONE) {
73aae04d07SSimon Glass 		debug("Can't get the usbphy register address\n");
74aae04d07SSimon Glass 		return -ENXIO;
75aae04d07SSimon Glass 	}
76aae04d07SSimon Glass 
77aae04d07SSimon Glass 	/* Vbus gpio */
78aae04d07SSimon Glass 	gpio_request_by_name(dev, "samsung,vbus-gpio", 0,
79aae04d07SSimon Glass 			     &plat->vbus_gpio, GPIOD_IS_OUT);
80aae04d07SSimon Glass 
81aae04d07SSimon Glass 	return 0;
82aae04d07SSimon Glass }
83e18bf1f9SRajeshwari Shinde 
exynos5_setup_usb_phy(struct exynos_usb_phy * usb)846a23c653SSuriyan Ramasami static void exynos5_setup_usb_phy(struct exynos_usb_phy *usb)
857590d3ceSRajeshwari Shinde {
8616f9480dSInderpal Singh 	u32 hsic_ctrl;
8716f9480dSInderpal Singh 
887590d3ceSRajeshwari Shinde 	clrbits_le32(&usb->usbphyctrl0,
897590d3ceSRajeshwari Shinde 			HOST_CTRL0_FSEL_MASK |
907590d3ceSRajeshwari Shinde 			HOST_CTRL0_COMMONON_N |
917590d3ceSRajeshwari Shinde 			/* HOST Phy setting */
927590d3ceSRajeshwari Shinde 			HOST_CTRL0_PHYSWRST |
937590d3ceSRajeshwari Shinde 			HOST_CTRL0_PHYSWRSTALL |
947590d3ceSRajeshwari Shinde 			HOST_CTRL0_SIDDQ |
957590d3ceSRajeshwari Shinde 			HOST_CTRL0_FORCESUSPEND |
967590d3ceSRajeshwari Shinde 			HOST_CTRL0_FORCESLEEP);
977590d3ceSRajeshwari Shinde 
987590d3ceSRajeshwari Shinde 	setbits_le32(&usb->usbphyctrl0,
997590d3ceSRajeshwari Shinde 			/* Setting up the ref freq */
1007590d3ceSRajeshwari Shinde 			(CLK_24MHZ << 16) |
1017590d3ceSRajeshwari Shinde 			/* HOST Phy setting */
1027590d3ceSRajeshwari Shinde 			HOST_CTRL0_LINKSWRST |
1037590d3ceSRajeshwari Shinde 			HOST_CTRL0_UTMISWRST);
1047590d3ceSRajeshwari Shinde 	udelay(10);
1057590d3ceSRajeshwari Shinde 	clrbits_le32(&usb->usbphyctrl0,
1067590d3ceSRajeshwari Shinde 			HOST_CTRL0_LINKSWRST |
1077590d3ceSRajeshwari Shinde 			HOST_CTRL0_UTMISWRST);
10816f9480dSInderpal Singh 
10916f9480dSInderpal Singh 	/* HSIC Phy Setting */
11016f9480dSInderpal Singh 	hsic_ctrl = (HSIC_CTRL_FORCESUSPEND |
11116f9480dSInderpal Singh 			HSIC_CTRL_FORCESLEEP |
11216f9480dSInderpal Singh 			HSIC_CTRL_SIDDQ);
11316f9480dSInderpal Singh 
11416f9480dSInderpal Singh 	clrbits_le32(&usb->hsicphyctrl1, hsic_ctrl);
11516f9480dSInderpal Singh 	clrbits_le32(&usb->hsicphyctrl2, hsic_ctrl);
11616f9480dSInderpal Singh 
11716f9480dSInderpal Singh 	hsic_ctrl = (((HSIC_CTRL_REFCLKDIV_12 & HSIC_CTRL_REFCLKDIV_MASK)
11816f9480dSInderpal Singh 				<< HSIC_CTRL_REFCLKDIV_SHIFT)
11916f9480dSInderpal Singh 			| ((HSIC_CTRL_REFCLKSEL & HSIC_CTRL_REFCLKSEL_MASK)
12016f9480dSInderpal Singh 				<< HSIC_CTRL_REFCLKSEL_SHIFT)
12116f9480dSInderpal Singh 			| HSIC_CTRL_UTMISWRST);
12216f9480dSInderpal Singh 
12316f9480dSInderpal Singh 	setbits_le32(&usb->hsicphyctrl1, hsic_ctrl);
12416f9480dSInderpal Singh 	setbits_le32(&usb->hsicphyctrl2, hsic_ctrl);
12516f9480dSInderpal Singh 
12616f9480dSInderpal Singh 	udelay(10);
12716f9480dSInderpal Singh 
12816f9480dSInderpal Singh 	clrbits_le32(&usb->hsicphyctrl1, HSIC_CTRL_PHYSWRST |
12916f9480dSInderpal Singh 					HSIC_CTRL_UTMISWRST);
13016f9480dSInderpal Singh 
13116f9480dSInderpal Singh 	clrbits_le32(&usb->hsicphyctrl2, HSIC_CTRL_PHYSWRST |
13216f9480dSInderpal Singh 					HSIC_CTRL_UTMISWRST);
13316f9480dSInderpal Singh 
1347590d3ceSRajeshwari Shinde 	udelay(20);
1357590d3ceSRajeshwari Shinde 
1367590d3ceSRajeshwari Shinde 	/* EHCI Ctrl setting */
1377590d3ceSRajeshwari Shinde 	setbits_le32(&usb->ehcictrl,
1387590d3ceSRajeshwari Shinde 			EHCICTRL_ENAINCRXALIGN |
1397590d3ceSRajeshwari Shinde 			EHCICTRL_ENAINCR4 |
1407590d3ceSRajeshwari Shinde 			EHCICTRL_ENAINCR8 |
1417590d3ceSRajeshwari Shinde 			EHCICTRL_ENAINCR16);
1427590d3ceSRajeshwari Shinde }
1437590d3ceSRajeshwari Shinde 
exynos4412_setup_usb_phy(struct exynos4412_usb_phy * usb)1446a23c653SSuriyan Ramasami static void exynos4412_setup_usb_phy(struct exynos4412_usb_phy *usb)
1456a23c653SSuriyan Ramasami {
1466a23c653SSuriyan Ramasami 	writel(CLK_24MHZ, &usb->usbphyclk);
1476a23c653SSuriyan Ramasami 
1486a23c653SSuriyan Ramasami 	clrbits_le32(&usb->usbphyctrl, (PHYPWR_NORMAL_MASK_HSIC0 |
1496a23c653SSuriyan Ramasami 		PHYPWR_NORMAL_MASK_HSIC1 | PHYPWR_NORMAL_MASK_PHY1 |
1506a23c653SSuriyan Ramasami 		PHYPWR_NORMAL_MASK_PHY0));
1516a23c653SSuriyan Ramasami 
1526a23c653SSuriyan Ramasami 	setbits_le32(&usb->usbphyrstcon, (RSTCON_HOSTPHY_SWRST | RSTCON_SWRST));
1536a23c653SSuriyan Ramasami 	udelay(10);
1546a23c653SSuriyan Ramasami 	clrbits_le32(&usb->usbphyrstcon, (RSTCON_HOSTPHY_SWRST | RSTCON_SWRST));
1556a23c653SSuriyan Ramasami }
1566a23c653SSuriyan Ramasami 
setup_usb_phy(struct exynos_usb_phy * usb)1576a23c653SSuriyan Ramasami static void setup_usb_phy(struct exynos_usb_phy *usb)
1586a23c653SSuriyan Ramasami {
1596a23c653SSuriyan Ramasami 	set_usbhost_mode(USB20_PHY_CFG_HOST_LINK_EN);
1606a23c653SSuriyan Ramasami 
1616a23c653SSuriyan Ramasami 	set_usbhost_phy_ctrl(POWER_USB_HOST_PHY_CTRL_EN);
1626a23c653SSuriyan Ramasami 
1636a23c653SSuriyan Ramasami 	if (cpu_is_exynos5())
1646a23c653SSuriyan Ramasami 		exynos5_setup_usb_phy(usb);
1656a23c653SSuriyan Ramasami 	else if (cpu_is_exynos4())
1666a23c653SSuriyan Ramasami 		if (proid_is_exynos4412())
1676a23c653SSuriyan Ramasami 			exynos4412_setup_usb_phy((struct exynos4412_usb_phy *)
1686a23c653SSuriyan Ramasami 						 usb);
1696a23c653SSuriyan Ramasami }
1706a23c653SSuriyan Ramasami 
exynos5_reset_usb_phy(struct exynos_usb_phy * usb)1716a23c653SSuriyan Ramasami static void exynos5_reset_usb_phy(struct exynos_usb_phy *usb)
1727590d3ceSRajeshwari Shinde {
17316f9480dSInderpal Singh 	u32 hsic_ctrl;
17416f9480dSInderpal Singh 
1757590d3ceSRajeshwari Shinde 	/* HOST_PHY reset */
1767590d3ceSRajeshwari Shinde 	setbits_le32(&usb->usbphyctrl0,
1777590d3ceSRajeshwari Shinde 			HOST_CTRL0_PHYSWRST |
1787590d3ceSRajeshwari Shinde 			HOST_CTRL0_PHYSWRSTALL |
1797590d3ceSRajeshwari Shinde 			HOST_CTRL0_SIDDQ |
1807590d3ceSRajeshwari Shinde 			HOST_CTRL0_FORCESUSPEND |
1817590d3ceSRajeshwari Shinde 			HOST_CTRL0_FORCESLEEP);
182c48ac113SRajeshwari Shinde 
18316f9480dSInderpal Singh 	/* HSIC Phy reset */
18416f9480dSInderpal Singh 	hsic_ctrl = (HSIC_CTRL_FORCESUSPEND |
18516f9480dSInderpal Singh 			HSIC_CTRL_FORCESLEEP |
18616f9480dSInderpal Singh 			HSIC_CTRL_SIDDQ |
18716f9480dSInderpal Singh 			HSIC_CTRL_PHYSWRST);
18816f9480dSInderpal Singh 
18916f9480dSInderpal Singh 	setbits_le32(&usb->hsicphyctrl1, hsic_ctrl);
19016f9480dSInderpal Singh 	setbits_le32(&usb->hsicphyctrl2, hsic_ctrl);
1916a23c653SSuriyan Ramasami }
1926a23c653SSuriyan Ramasami 
exynos4412_reset_usb_phy(struct exynos4412_usb_phy * usb)1936a23c653SSuriyan Ramasami static void exynos4412_reset_usb_phy(struct exynos4412_usb_phy *usb)
1946a23c653SSuriyan Ramasami {
1956a23c653SSuriyan Ramasami 	setbits_le32(&usb->usbphyctrl, (PHYPWR_NORMAL_MASK_HSIC0 |
1966a23c653SSuriyan Ramasami 		PHYPWR_NORMAL_MASK_HSIC1 | PHYPWR_NORMAL_MASK_PHY1 |
1976a23c653SSuriyan Ramasami 		PHYPWR_NORMAL_MASK_PHY0));
1986a23c653SSuriyan Ramasami }
1996a23c653SSuriyan Ramasami 
2006a23c653SSuriyan Ramasami /* Reset the EHCI host controller. */
reset_usb_phy(struct exynos_usb_phy * usb)2016a23c653SSuriyan Ramasami static void reset_usb_phy(struct exynos_usb_phy *usb)
2026a23c653SSuriyan Ramasami {
2036a23c653SSuriyan Ramasami 	if (cpu_is_exynos5())
2046a23c653SSuriyan Ramasami 		exynos5_reset_usb_phy(usb);
2056a23c653SSuriyan Ramasami 	else if (cpu_is_exynos4())
2066a23c653SSuriyan Ramasami 		if (proid_is_exynos4412())
2076a23c653SSuriyan Ramasami 			exynos4412_reset_usb_phy((struct exynos4412_usb_phy *)
2086a23c653SSuriyan Ramasami 						 usb);
20916f9480dSInderpal Singh 
210c48ac113SRajeshwari Shinde 	set_usbhost_phy_ctrl(POWER_USB_HOST_PHY_CTRL_DISABLE);
2117590d3ceSRajeshwari Shinde }
2127590d3ceSRajeshwari Shinde 
ehci_usb_probe(struct udevice * dev)213aae04d07SSimon Glass static int ehci_usb_probe(struct udevice *dev)
214aae04d07SSimon Glass {
215aae04d07SSimon Glass 	struct exynos_ehci_platdata *plat = dev_get_platdata(dev);
216aae04d07SSimon Glass 	struct exynos_ehci *ctx = dev_get_priv(dev);
217aae04d07SSimon Glass 	struct ehci_hcor *hcor;
218aae04d07SSimon Glass 
219aae04d07SSimon Glass 	ctx->hcd = (struct ehci_hccr *)plat->hcd_base;
220aae04d07SSimon Glass 	ctx->usb = (struct exynos_usb_phy *)plat->phy_base;
221aae04d07SSimon Glass 
222aae04d07SSimon Glass 	/* setup the Vbus gpio here */
223aae04d07SSimon Glass 	if (dm_gpio_is_valid(&plat->vbus_gpio))
224aae04d07SSimon Glass 		dm_gpio_set_value(&plat->vbus_gpio, 1);
225aae04d07SSimon Glass 
226aae04d07SSimon Glass 	setup_usb_phy(ctx->usb);
22770cc443dSLukasz Majewski 	hcor = (struct ehci_hcor *)((uint32_t)ctx->hcd +
22870cc443dSLukasz Majewski 			HC_LENGTH(ehci_readl(&ctx->hcd->cr_capbase)));
229aae04d07SSimon Glass 
230aae04d07SSimon Glass 	return ehci_register(dev, ctx->hcd, hcor, NULL, 0, USB_INIT_HOST);
231aae04d07SSimon Glass }
232aae04d07SSimon Glass 
ehci_usb_remove(struct udevice * dev)233aae04d07SSimon Glass static int ehci_usb_remove(struct udevice *dev)
234aae04d07SSimon Glass {
235aae04d07SSimon Glass 	struct exynos_ehci *ctx = dev_get_priv(dev);
236aae04d07SSimon Glass 	int ret;
237aae04d07SSimon Glass 
238aae04d07SSimon Glass 	ret = ehci_deregister(dev);
239aae04d07SSimon Glass 	if (ret)
240aae04d07SSimon Glass 		return ret;
241aae04d07SSimon Glass 	reset_usb_phy(ctx->usb);
242aae04d07SSimon Glass 
243aae04d07SSimon Glass 	return 0;
244aae04d07SSimon Glass }
245aae04d07SSimon Glass 
246aae04d07SSimon Glass static const struct udevice_id ehci_usb_ids[] = {
247aae04d07SSimon Glass 	{ .compatible = "samsung,exynos-ehci" },
248aae04d07SSimon Glass 	{ }
249aae04d07SSimon Glass };
250aae04d07SSimon Glass 
251aae04d07SSimon Glass U_BOOT_DRIVER(usb_ehci) = {
252aae04d07SSimon Glass 	.name	= "ehci_exynos",
253aae04d07SSimon Glass 	.id	= UCLASS_USB,
254aae04d07SSimon Glass 	.of_match = ehci_usb_ids,
255aae04d07SSimon Glass 	.ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
256aae04d07SSimon Glass 	.probe = ehci_usb_probe,
257aae04d07SSimon Glass 	.remove = ehci_usb_remove,
258aae04d07SSimon Glass 	.ops	= &ehci_usb_ops,
259aae04d07SSimon Glass 	.priv_auto_alloc_size = sizeof(struct exynos_ehci),
260aae04d07SSimon Glass 	.platdata_auto_alloc_size = sizeof(struct exynos_ehci_platdata),
261aae04d07SSimon Glass 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
262aae04d07SSimon Glass };
263