xref: /openbmc/linux/drivers/usb/host/ehci-mv.c (revision aed67922031377b83a5dc3afa1e207152069100e)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
23a082ec9SNeil Zhang /*
33a082ec9SNeil Zhang  * Copyright (C) 2011 Marvell International Ltd. All rights reserved.
43a082ec9SNeil Zhang  * Author: Chao Xie <chao.xie@marvell.com>
53a082ec9SNeil Zhang  *        Neil Zhang <zhangwm@marvell.com>
63a082ec9SNeil Zhang  */
73a082ec9SNeil Zhang 
83a082ec9SNeil Zhang #include <linux/kernel.h>
93a082ec9SNeil Zhang #include <linux/module.h>
103a082ec9SNeil Zhang #include <linux/platform_device.h>
113a082ec9SNeil Zhang #include <linux/clk.h>
12ded017eeSKishon Vijay Abraham I #include <linux/err.h>
133a082ec9SNeil Zhang #include <linux/usb/otg.h>
147b104f89SLubomir Rintel #include <linux/usb/of.h>
153a082ec9SNeil Zhang #include <linux/platform_data/mv_usb.h>
160440fa3dSLubomir Rintel #include <linux/io.h>
170440fa3dSLubomir Rintel 
180440fa3dSLubomir Rintel #include <linux/usb/hcd.h>
190440fa3dSLubomir Rintel 
200440fa3dSLubomir Rintel #include "ehci.h"
213a082ec9SNeil Zhang 
22a740f20dSLubomir Rintel /* registers */
23a740f20dSLubomir Rintel #define U2x_CAPREGS_OFFSET       0x100
24a740f20dSLubomir Rintel 
253a082ec9SNeil Zhang #define CAPLENGTH_MASK         (0xff)
263a082ec9SNeil Zhang 
270440fa3dSLubomir Rintel #define hcd_to_ehci_hcd_mv(h) ((struct ehci_hcd_mv *)hcd_to_ehci(h)->priv)
283a082ec9SNeil Zhang 
290440fa3dSLubomir Rintel struct ehci_hcd_mv {
303a082ec9SNeil Zhang 	/* Which mode does this ehci running OTG/Host ? */
313a082ec9SNeil Zhang 	int mode;
323a082ec9SNeil Zhang 
33a740f20dSLubomir Rintel 	void __iomem *base;
343a082ec9SNeil Zhang 	void __iomem *cap_regs;
353a082ec9SNeil Zhang 	void __iomem *op_regs;
363a082ec9SNeil Zhang 
3786753811SHeikki Krogerus 	struct usb_phy *otg;
38813e18b1SLubomir Rintel 	struct clk *clk;
393a082ec9SNeil Zhang 
40a740f20dSLubomir Rintel 	struct phy *phy;
413a082ec9SNeil Zhang 
42813e18b1SLubomir Rintel 	int (*set_vbus)(unsigned int vbus);
433a082ec9SNeil Zhang };
443a082ec9SNeil Zhang 
453a082ec9SNeil Zhang static void ehci_clock_enable(struct ehci_hcd_mv *ehci_mv)
463a082ec9SNeil Zhang {
47b7e159c2SChao Xie 	clk_prepare_enable(ehci_mv->clk);
483a082ec9SNeil Zhang }
493a082ec9SNeil Zhang 
503a082ec9SNeil Zhang static void ehci_clock_disable(struct ehci_hcd_mv *ehci_mv)
513a082ec9SNeil Zhang {
52b7e159c2SChao Xie 	clk_disable_unprepare(ehci_mv->clk);
533a082ec9SNeil Zhang }
543a082ec9SNeil Zhang 
553a082ec9SNeil Zhang static int mv_ehci_enable(struct ehci_hcd_mv *ehci_mv)
563a082ec9SNeil Zhang {
573a082ec9SNeil Zhang 	ehci_clock_enable(ehci_mv);
58a740f20dSLubomir Rintel 	return phy_init(ehci_mv->phy);
593a082ec9SNeil Zhang }
603a082ec9SNeil Zhang 
613a082ec9SNeil Zhang static void mv_ehci_disable(struct ehci_hcd_mv *ehci_mv)
623a082ec9SNeil Zhang {
63a740f20dSLubomir Rintel 	phy_exit(ehci_mv->phy);
643a082ec9SNeil Zhang 	ehci_clock_disable(ehci_mv);
653a082ec9SNeil Zhang }
663a082ec9SNeil Zhang 
673a082ec9SNeil Zhang static int mv_ehci_reset(struct usb_hcd *hcd)
683a082ec9SNeil Zhang {
693a082ec9SNeil Zhang 	struct device *dev = hcd->self.controller;
700440fa3dSLubomir Rintel 	struct ehci_hcd_mv *ehci_mv = hcd_to_ehci_hcd_mv(hcd);
717b104f89SLubomir Rintel 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
727b104f89SLubomir Rintel 	u32 status;
733a082ec9SNeil Zhang 	int retval;
743a082ec9SNeil Zhang 
753a082ec9SNeil Zhang 	if (ehci_mv == NULL) {
763a082ec9SNeil Zhang 		dev_err(dev, "Can not find private ehci data\n");
773a082ec9SNeil Zhang 		return -ENODEV;
783a082ec9SNeil Zhang 	}
793a082ec9SNeil Zhang 
803a082ec9SNeil Zhang 	hcd->has_tt = 1;
813a082ec9SNeil Zhang 
821a49e2acSAlan Stern 	retval = ehci_setup(hcd);
831a49e2acSAlan Stern 	if (retval)
841a49e2acSAlan Stern 		dev_err(dev, "ehci_setup failed %d\n", retval);
851a49e2acSAlan Stern 
867b104f89SLubomir Rintel 	if (of_usb_get_phy_mode(dev->of_node) == USBPHY_INTERFACE_MODE_HSIC) {
877b104f89SLubomir Rintel 		status = ehci_readl(ehci, &ehci->regs->port_status[0]);
887b104f89SLubomir Rintel 		status |= PORT_TEST_FORCE;
897b104f89SLubomir Rintel 		ehci_writel(ehci, status, &ehci->regs->port_status[0]);
907b104f89SLubomir Rintel 		status &= ~PORT_TEST_FORCE;
917b104f89SLubomir Rintel 		ehci_writel(ehci, status, &ehci->regs->port_status[0]);
927b104f89SLubomir Rintel 	}
937b104f89SLubomir Rintel 
943a082ec9SNeil Zhang 	return retval;
953a082ec9SNeil Zhang }
963a082ec9SNeil Zhang 
970440fa3dSLubomir Rintel static struct hc_driver __read_mostly ehci_platform_hc_driver;
983a082ec9SNeil Zhang 
990440fa3dSLubomir Rintel static const struct ehci_driver_overrides platform_overrides __initconst = {
1003a082ec9SNeil Zhang 	.reset =		mv_ehci_reset,
1010440fa3dSLubomir Rintel 	.extra_priv_size =	sizeof(struct ehci_hcd_mv),
1023a082ec9SNeil Zhang };
1033a082ec9SNeil Zhang 
1043a082ec9SNeil Zhang static int mv_ehci_probe(struct platform_device *pdev)
1053a082ec9SNeil Zhang {
106d4f09e28SJingoo Han 	struct mv_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
1073a082ec9SNeil Zhang 	struct usb_hcd *hcd;
1083a082ec9SNeil Zhang 	struct ehci_hcd *ehci;
1093a082ec9SNeil Zhang 	struct ehci_hcd_mv *ehci_mv;
1103a082ec9SNeil Zhang 	struct resource *r;
111b7e159c2SChao Xie 	int retval = -ENODEV;
1123a082ec9SNeil Zhang 	u32 offset;
1136351f170SLubomir Rintel 	u32 status;
1143a082ec9SNeil Zhang 
1153a082ec9SNeil Zhang 	if (usb_disabled())
1163a082ec9SNeil Zhang 		return -ENODEV;
1173a082ec9SNeil Zhang 
118*aed67922SLubomir Rintel 	hcd = usb_create_hcd(&ehci_platform_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1193a082ec9SNeil Zhang 	if (!hcd)
1203a082ec9SNeil Zhang 		return -ENOMEM;
1213a082ec9SNeil Zhang 
1220440fa3dSLubomir Rintel 	platform_set_drvdata(pdev, hcd);
1230440fa3dSLubomir Rintel 	ehci_mv = hcd_to_ehci_hcd_mv(hcd);
124813e18b1SLubomir Rintel 
125813e18b1SLubomir Rintel 	ehci_mv->mode = MV_USB_MODE_HOST;
126813e18b1SLubomir Rintel 	if (pdata) {
127813e18b1SLubomir Rintel 		ehci_mv->mode = pdata->mode;
128813e18b1SLubomir Rintel 		ehci_mv->set_vbus = pdata->set_vbus;
129813e18b1SLubomir Rintel 	}
1303a082ec9SNeil Zhang 
131cf94ca49SLubomir Rintel 	ehci_mv->phy = devm_phy_optional_get(&pdev->dev, "usb");
132a740f20dSLubomir Rintel 	if (IS_ERR(ehci_mv->phy)) {
133a740f20dSLubomir Rintel 		retval = PTR_ERR(ehci_mv->phy);
134a740f20dSLubomir Rintel 		if (retval != -EPROBE_DEFER)
135a740f20dSLubomir Rintel 			dev_err(&pdev->dev, "Failed to get phy.\n");
136a740f20dSLubomir Rintel 		goto err_put_hcd;
137a740f20dSLubomir Rintel 	}
138a740f20dSLubomir Rintel 
139b7e159c2SChao Xie 	ehci_mv->clk = devm_clk_get(&pdev->dev, NULL);
140b7e159c2SChao Xie 	if (IS_ERR(ehci_mv->clk)) {
141b7e159c2SChao Xie 		dev_err(&pdev->dev, "error getting clock\n");
142b7e159c2SChao Xie 		retval = PTR_ERR(ehci_mv->clk);
143970691ebSJingoo Han 		goto err_put_hcd;
1443a082ec9SNeil Zhang 	}
1453a082ec9SNeil Zhang 
1463a082ec9SNeil Zhang 
147a740f20dSLubomir Rintel 
148a740f20dSLubomir Rintel 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149a740f20dSLubomir Rintel 	ehci_mv->base = devm_ioremap_resource(&pdev->dev, r);
150a740f20dSLubomir Rintel 	if (IS_ERR(ehci_mv->base)) {
151a740f20dSLubomir Rintel 		retval = PTR_ERR(ehci_mv->base);
152970691ebSJingoo Han 		goto err_put_hcd;
1533a082ec9SNeil Zhang 	}
1543a082ec9SNeil Zhang 
1553a082ec9SNeil Zhang 	retval = mv_ehci_enable(ehci_mv);
1563a082ec9SNeil Zhang 	if (retval) {
1573a082ec9SNeil Zhang 		dev_err(&pdev->dev, "init phy error %d\n", retval);
158970691ebSJingoo Han 		goto err_put_hcd;
1593a082ec9SNeil Zhang 	}
1603a082ec9SNeil Zhang 
161a740f20dSLubomir Rintel 	ehci_mv->cap_regs =
162a740f20dSLubomir Rintel 		(void __iomem *) ((unsigned long) ehci_mv->base + U2x_CAPREGS_OFFSET);
1633a082ec9SNeil Zhang 	offset = readl(ehci_mv->cap_regs) & CAPLENGTH_MASK;
1643a082ec9SNeil Zhang 	ehci_mv->op_regs =
1653a082ec9SNeil Zhang 		(void __iomem *) ((unsigned long) ehci_mv->cap_regs + offset);
1663a082ec9SNeil Zhang 
1673a082ec9SNeil Zhang 	hcd->rsrc_start = r->start;
16807cd29d7SPaul Vlase 	hcd->rsrc_len = resource_size(r);
1693a082ec9SNeil Zhang 	hcd->regs = ehci_mv->op_regs;
1703a082ec9SNeil Zhang 
1713a082ec9SNeil Zhang 	hcd->irq = platform_get_irq(pdev, 0);
1723a082ec9SNeil Zhang 	if (!hcd->irq) {
1733a082ec9SNeil Zhang 		dev_err(&pdev->dev, "Cannot get irq.");
1743a082ec9SNeil Zhang 		retval = -ENODEV;
1753a082ec9SNeil Zhang 		goto err_disable_clk;
1763a082ec9SNeil Zhang 	}
1773a082ec9SNeil Zhang 
1783a082ec9SNeil Zhang 	ehci = hcd_to_ehci(hcd);
17960826786SKrzysztof Kozlowski 	ehci->caps = (struct ehci_caps __iomem *) ehci_mv->cap_regs;
1803a082ec9SNeil Zhang 
1813a082ec9SNeil Zhang 	if (ehci_mv->mode == MV_USB_MODE_OTG) {
18235b55563SJulia Lawall 		ehci_mv->otg = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
1836f3ed4ecSFelipe Balbi 		if (IS_ERR(ehci_mv->otg)) {
1846f3ed4ecSFelipe Balbi 			retval = PTR_ERR(ehci_mv->otg);
1856f3ed4ecSFelipe Balbi 
1866f3ed4ecSFelipe Balbi 			if (retval == -ENXIO)
1876f3ed4ecSFelipe Balbi 				dev_info(&pdev->dev, "MV_USB_MODE_OTG "
1886f3ed4ecSFelipe Balbi 						"must have CONFIG_USB_PHY enabled\n");
1896f3ed4ecSFelipe Balbi 			else
1903a082ec9SNeil Zhang 				dev_err(&pdev->dev,
1913a082ec9SNeil Zhang 						"unable to find transceiver\n");
1923a082ec9SNeil Zhang 			goto err_disable_clk;
1933a082ec9SNeil Zhang 		}
1943a082ec9SNeil Zhang 
1956e13c650SHeikki Krogerus 		retval = otg_set_host(ehci_mv->otg->otg, &hcd->self);
1963a082ec9SNeil Zhang 		if (retval < 0) {
1973a082ec9SNeil Zhang 			dev_err(&pdev->dev,
1983a082ec9SNeil Zhang 				"unable to register with transceiver\n");
1993a082ec9SNeil Zhang 			retval = -ENODEV;
20035b55563SJulia Lawall 			goto err_disable_clk;
2013a082ec9SNeil Zhang 		}
2023a082ec9SNeil Zhang 		/* otg will enable clock before use as host */
2033a082ec9SNeil Zhang 		mv_ehci_disable(ehci_mv);
2043a082ec9SNeil Zhang 	} else {
205813e18b1SLubomir Rintel 		if (ehci_mv->set_vbus)
206813e18b1SLubomir Rintel 			ehci_mv->set_vbus(1);
2073a082ec9SNeil Zhang 
2083a082ec9SNeil Zhang 		retval = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
2093a082ec9SNeil Zhang 		if (retval) {
2103a082ec9SNeil Zhang 			dev_err(&pdev->dev,
2113a082ec9SNeil Zhang 				"failed to add hcd with err %d\n", retval);
2123a082ec9SNeil Zhang 			goto err_set_vbus;
2133a082ec9SNeil Zhang 		}
2143c9740a1SPeter Chen 		device_wakeup_enable(hcd->self.controller);
2153a082ec9SNeil Zhang 	}
2163a082ec9SNeil Zhang 
2176351f170SLubomir Rintel 	if (of_usb_get_phy_mode(pdev->dev.of_node) == USBPHY_INTERFACE_MODE_HSIC) {
2186351f170SLubomir Rintel 		status = ehci_readl(ehci, &ehci->regs->port_status[0]);
2196351f170SLubomir Rintel 		/* These "reserved" bits actually enable HSIC mode. */
2206351f170SLubomir Rintel 		status |= BIT(25);
2216351f170SLubomir Rintel 		status &= ~GENMASK(31, 30);
2226351f170SLubomir Rintel 		ehci_writel(ehci, status, &ehci->regs->port_status[0]);
2236351f170SLubomir Rintel 	}
2246351f170SLubomir Rintel 
2253a082ec9SNeil Zhang 	dev_info(&pdev->dev,
2263a082ec9SNeil Zhang 		 "successful find EHCI device with regs 0x%p irq %d"
2273a082ec9SNeil Zhang 		 " working in %s mode\n", hcd->regs, hcd->irq,
2283a082ec9SNeil Zhang 		 ehci_mv->mode == MV_USB_MODE_OTG ? "OTG" : "Host");
2293a082ec9SNeil Zhang 
2303a082ec9SNeil Zhang 	return 0;
2313a082ec9SNeil Zhang 
2323a082ec9SNeil Zhang err_set_vbus:
233813e18b1SLubomir Rintel 	if (ehci_mv->set_vbus)
234813e18b1SLubomir Rintel 		ehci_mv->set_vbus(0);
2353a082ec9SNeil Zhang err_disable_clk:
2363a082ec9SNeil Zhang 	mv_ehci_disable(ehci_mv);
2373a082ec9SNeil Zhang err_put_hcd:
2383a082ec9SNeil Zhang 	usb_put_hcd(hcd);
2393a082ec9SNeil Zhang 
2403a082ec9SNeil Zhang 	return retval;
2413a082ec9SNeil Zhang }
2423a082ec9SNeil Zhang 
2433a082ec9SNeil Zhang static int mv_ehci_remove(struct platform_device *pdev)
2443a082ec9SNeil Zhang {
2450440fa3dSLubomir Rintel 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
2460440fa3dSLubomir Rintel 	struct ehci_hcd_mv *ehci_mv = hcd_to_ehci_hcd_mv(hcd);
2473a082ec9SNeil Zhang 
2483a082ec9SNeil Zhang 	if (hcd->rh_registered)
2493a082ec9SNeil Zhang 		usb_remove_hcd(hcd);
2503a082ec9SNeil Zhang 
25135b55563SJulia Lawall 	if (!IS_ERR_OR_NULL(ehci_mv->otg))
2526e13c650SHeikki Krogerus 		otg_set_host(ehci_mv->otg->otg, NULL);
2533a082ec9SNeil Zhang 
2543a082ec9SNeil Zhang 	if (ehci_mv->mode == MV_USB_MODE_HOST) {
255813e18b1SLubomir Rintel 		if (ehci_mv->set_vbus)
256813e18b1SLubomir Rintel 			ehci_mv->set_vbus(0);
2573a082ec9SNeil Zhang 
2583a082ec9SNeil Zhang 		mv_ehci_disable(ehci_mv);
2593a082ec9SNeil Zhang 	}
2603a082ec9SNeil Zhang 
2613a082ec9SNeil Zhang 	usb_put_hcd(hcd);
2623a082ec9SNeil Zhang 
2633a082ec9SNeil Zhang 	return 0;
2643a082ec9SNeil Zhang }
2653a082ec9SNeil Zhang 
2663a082ec9SNeil Zhang MODULE_ALIAS("mv-ehci");
2673a082ec9SNeil Zhang 
2683a082ec9SNeil Zhang static const struct platform_device_id ehci_id_table[] = {
26992f98352SLubomir Rintel 	{"pxa-u2oehci", 0},
27092f98352SLubomir Rintel 	{"pxa-sph", 0},
2713a082ec9SNeil Zhang 	{},
2723a082ec9SNeil Zhang };
2733a082ec9SNeil Zhang 
2743a082ec9SNeil Zhang static void mv_ehci_shutdown(struct platform_device *pdev)
2753a082ec9SNeil Zhang {
2760440fa3dSLubomir Rintel 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
2773a082ec9SNeil Zhang 
2783a082ec9SNeil Zhang 	if (!hcd->rh_registered)
2793a082ec9SNeil Zhang 		return;
2803a082ec9SNeil Zhang 
2813a082ec9SNeil Zhang 	if (hcd->driver->shutdown)
2823a082ec9SNeil Zhang 		hcd->driver->shutdown(hcd);
2833a082ec9SNeil Zhang }
2843a082ec9SNeil Zhang 
285813e18b1SLubomir Rintel static const struct of_device_id ehci_mv_dt_ids[] = {
286813e18b1SLubomir Rintel 	{ .compatible = "marvell,pxau2o-ehci", },
287813e18b1SLubomir Rintel 	{},
288813e18b1SLubomir Rintel };
289813e18b1SLubomir Rintel 
2903a082ec9SNeil Zhang static struct platform_driver ehci_mv_driver = {
2913a082ec9SNeil Zhang 	.probe = mv_ehci_probe,
2923a082ec9SNeil Zhang 	.remove = mv_ehci_remove,
2933a082ec9SNeil Zhang 	.shutdown = mv_ehci_shutdown,
2943a082ec9SNeil Zhang 	.driver = {
2953a082ec9SNeil Zhang 		.name = "mv-ehci",
2963a082ec9SNeil Zhang 		.bus = &platform_bus_type,
297813e18b1SLubomir Rintel 		.of_match_table = ehci_mv_dt_ids,
2983a082ec9SNeil Zhang 	},
2993a082ec9SNeil Zhang 	.id_table = ehci_id_table,
3003a082ec9SNeil Zhang };
3010440fa3dSLubomir Rintel 
3020440fa3dSLubomir Rintel static int __init ehci_platform_init(void)
3030440fa3dSLubomir Rintel {
3040440fa3dSLubomir Rintel 	if (usb_disabled())
3050440fa3dSLubomir Rintel 		return -ENODEV;
3060440fa3dSLubomir Rintel 
3070440fa3dSLubomir Rintel 	ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides);
3080440fa3dSLubomir Rintel 	return platform_driver_register(&ehci_mv_driver);
3090440fa3dSLubomir Rintel }
3100440fa3dSLubomir Rintel module_init(ehci_platform_init);
3110440fa3dSLubomir Rintel 
3120440fa3dSLubomir Rintel static void __exit ehci_platform_cleanup(void)
3130440fa3dSLubomir Rintel {
3140440fa3dSLubomir Rintel 	platform_driver_unregister(&ehci_mv_driver);
3150440fa3dSLubomir Rintel }
3160440fa3dSLubomir Rintel module_exit(ehci_platform_cleanup);
3170440fa3dSLubomir Rintel 
3180440fa3dSLubomir Rintel MODULE_DESCRIPTION("Marvell EHCI driver");
3190440fa3dSLubomir Rintel MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>");
3200440fa3dSLubomir Rintel MODULE_AUTHOR("Neil Zhang <zhangwm@marvell.com>");
3210440fa3dSLubomir Rintel MODULE_ALIAS("mv-ehci");
3220440fa3dSLubomir Rintel MODULE_LICENSE("GPL");
32370d0ba4cSLubomir Rintel MODULE_DEVICE_TABLE(of, ehci_mv_dt_ids);
324