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> 143a082ec9SNeil Zhang #include <linux/platform_data/mv_usb.h> 150440fa3dSLubomir Rintel #include <linux/io.h> 160440fa3dSLubomir Rintel 170440fa3dSLubomir Rintel #include <linux/usb/hcd.h> 180440fa3dSLubomir Rintel 190440fa3dSLubomir Rintel #include "ehci.h" 203a082ec9SNeil Zhang 21a740f20dSLubomir Rintel /* registers */ 22a740f20dSLubomir Rintel #define U2x_CAPREGS_OFFSET 0x100 23a740f20dSLubomir Rintel 243a082ec9SNeil Zhang #define CAPLENGTH_MASK (0xff) 253a082ec9SNeil Zhang 260440fa3dSLubomir Rintel #define hcd_to_ehci_hcd_mv(h) ((struct ehci_hcd_mv *)hcd_to_ehci(h)->priv) 273a082ec9SNeil Zhang 280440fa3dSLubomir Rintel struct ehci_hcd_mv { 293a082ec9SNeil Zhang /* Which mode does this ehci running OTG/Host ? */ 303a082ec9SNeil Zhang int mode; 313a082ec9SNeil Zhang 32a740f20dSLubomir Rintel void __iomem *base; 333a082ec9SNeil Zhang void __iomem *cap_regs; 343a082ec9SNeil Zhang void __iomem *op_regs; 353a082ec9SNeil Zhang 3686753811SHeikki Krogerus struct usb_phy *otg; 37813e18b1SLubomir Rintel struct clk *clk; 383a082ec9SNeil Zhang 39a740f20dSLubomir Rintel struct phy *phy; 403a082ec9SNeil Zhang 41813e18b1SLubomir Rintel int (*set_vbus)(unsigned int vbus); 423a082ec9SNeil Zhang }; 433a082ec9SNeil Zhang 443a082ec9SNeil Zhang static void ehci_clock_enable(struct ehci_hcd_mv *ehci_mv) 453a082ec9SNeil Zhang { 46b7e159c2SChao Xie clk_prepare_enable(ehci_mv->clk); 473a082ec9SNeil Zhang } 483a082ec9SNeil Zhang 493a082ec9SNeil Zhang static void ehci_clock_disable(struct ehci_hcd_mv *ehci_mv) 503a082ec9SNeil Zhang { 51b7e159c2SChao Xie clk_disable_unprepare(ehci_mv->clk); 523a082ec9SNeil Zhang } 533a082ec9SNeil Zhang 543a082ec9SNeil Zhang static int mv_ehci_enable(struct ehci_hcd_mv *ehci_mv) 553a082ec9SNeil Zhang { 563a082ec9SNeil Zhang ehci_clock_enable(ehci_mv); 57a740f20dSLubomir Rintel return phy_init(ehci_mv->phy); 583a082ec9SNeil Zhang } 593a082ec9SNeil Zhang 603a082ec9SNeil Zhang static void mv_ehci_disable(struct ehci_hcd_mv *ehci_mv) 613a082ec9SNeil Zhang { 62a740f20dSLubomir Rintel phy_exit(ehci_mv->phy); 633a082ec9SNeil Zhang ehci_clock_disable(ehci_mv); 643a082ec9SNeil Zhang } 653a082ec9SNeil Zhang 663a082ec9SNeil Zhang static int mv_ehci_reset(struct usb_hcd *hcd) 673a082ec9SNeil Zhang { 683a082ec9SNeil Zhang struct device *dev = hcd->self.controller; 690440fa3dSLubomir Rintel struct ehci_hcd_mv *ehci_mv = hcd_to_ehci_hcd_mv(hcd); 703a082ec9SNeil Zhang int retval; 713a082ec9SNeil Zhang 723a082ec9SNeil Zhang if (ehci_mv == NULL) { 733a082ec9SNeil Zhang dev_err(dev, "Can not find private ehci data\n"); 743a082ec9SNeil Zhang return -ENODEV; 753a082ec9SNeil Zhang } 763a082ec9SNeil Zhang 773a082ec9SNeil Zhang hcd->has_tt = 1; 783a082ec9SNeil Zhang 791a49e2acSAlan Stern retval = ehci_setup(hcd); 801a49e2acSAlan Stern if (retval) 811a49e2acSAlan Stern dev_err(dev, "ehci_setup failed %d\n", retval); 821a49e2acSAlan Stern 833a082ec9SNeil Zhang return retval; 843a082ec9SNeil Zhang } 853a082ec9SNeil Zhang 860440fa3dSLubomir Rintel static struct hc_driver __read_mostly ehci_platform_hc_driver; 873a082ec9SNeil Zhang 880440fa3dSLubomir Rintel static const struct ehci_driver_overrides platform_overrides __initconst = { 893a082ec9SNeil Zhang .reset = mv_ehci_reset, 900440fa3dSLubomir Rintel .extra_priv_size = sizeof(struct ehci_hcd_mv), 913a082ec9SNeil Zhang }; 923a082ec9SNeil Zhang 933a082ec9SNeil Zhang static int mv_ehci_probe(struct platform_device *pdev) 943a082ec9SNeil Zhang { 95d4f09e28SJingoo Han struct mv_usb_platform_data *pdata = dev_get_platdata(&pdev->dev); 963a082ec9SNeil Zhang struct usb_hcd *hcd; 973a082ec9SNeil Zhang struct ehci_hcd *ehci; 983a082ec9SNeil Zhang struct ehci_hcd_mv *ehci_mv; 993a082ec9SNeil Zhang struct resource *r; 100b7e159c2SChao Xie int retval = -ENODEV; 1013a082ec9SNeil Zhang u32 offset; 1023a082ec9SNeil Zhang 1033a082ec9SNeil Zhang if (usb_disabled()) 1043a082ec9SNeil Zhang return -ENODEV; 1053a082ec9SNeil Zhang 1060440fa3dSLubomir Rintel hcd = usb_create_hcd(&ehci_platform_hc_driver, &pdev->dev, "mv ehci"); 1073a082ec9SNeil Zhang if (!hcd) 1083a082ec9SNeil Zhang return -ENOMEM; 1093a082ec9SNeil Zhang 1100440fa3dSLubomir Rintel platform_set_drvdata(pdev, hcd); 1110440fa3dSLubomir Rintel ehci_mv = hcd_to_ehci_hcd_mv(hcd); 112813e18b1SLubomir Rintel 113813e18b1SLubomir Rintel ehci_mv->mode = MV_USB_MODE_HOST; 114813e18b1SLubomir Rintel if (pdata) { 115813e18b1SLubomir Rintel ehci_mv->mode = pdata->mode; 116813e18b1SLubomir Rintel ehci_mv->set_vbus = pdata->set_vbus; 117813e18b1SLubomir Rintel } 1183a082ec9SNeil Zhang 119*cf94ca49SLubomir Rintel ehci_mv->phy = devm_phy_optional_get(&pdev->dev, "usb"); 120a740f20dSLubomir Rintel if (IS_ERR(ehci_mv->phy)) { 121a740f20dSLubomir Rintel retval = PTR_ERR(ehci_mv->phy); 122a740f20dSLubomir Rintel if (retval != -EPROBE_DEFER) 123a740f20dSLubomir Rintel dev_err(&pdev->dev, "Failed to get phy.\n"); 124a740f20dSLubomir Rintel goto err_put_hcd; 125a740f20dSLubomir Rintel } 126a740f20dSLubomir Rintel 127b7e159c2SChao Xie ehci_mv->clk = devm_clk_get(&pdev->dev, NULL); 128b7e159c2SChao Xie if (IS_ERR(ehci_mv->clk)) { 129b7e159c2SChao Xie dev_err(&pdev->dev, "error getting clock\n"); 130b7e159c2SChao Xie retval = PTR_ERR(ehci_mv->clk); 131970691ebSJingoo Han goto err_put_hcd; 1323a082ec9SNeil Zhang } 1333a082ec9SNeil Zhang 1343a082ec9SNeil Zhang 135a740f20dSLubomir Rintel 136a740f20dSLubomir Rintel r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 137a740f20dSLubomir Rintel ehci_mv->base = devm_ioremap_resource(&pdev->dev, r); 138a740f20dSLubomir Rintel if (IS_ERR(ehci_mv->base)) { 139a740f20dSLubomir Rintel retval = PTR_ERR(ehci_mv->base); 140970691ebSJingoo Han goto err_put_hcd; 1413a082ec9SNeil Zhang } 1423a082ec9SNeil Zhang 1433a082ec9SNeil Zhang retval = mv_ehci_enable(ehci_mv); 1443a082ec9SNeil Zhang if (retval) { 1453a082ec9SNeil Zhang dev_err(&pdev->dev, "init phy error %d\n", retval); 146970691ebSJingoo Han goto err_put_hcd; 1473a082ec9SNeil Zhang } 1483a082ec9SNeil Zhang 149a740f20dSLubomir Rintel ehci_mv->cap_regs = 150a740f20dSLubomir Rintel (void __iomem *) ((unsigned long) ehci_mv->base + U2x_CAPREGS_OFFSET); 1513a082ec9SNeil Zhang offset = readl(ehci_mv->cap_regs) & CAPLENGTH_MASK; 1523a082ec9SNeil Zhang ehci_mv->op_regs = 1533a082ec9SNeil Zhang (void __iomem *) ((unsigned long) ehci_mv->cap_regs + offset); 1543a082ec9SNeil Zhang 1553a082ec9SNeil Zhang hcd->rsrc_start = r->start; 15607cd29d7SPaul Vlase hcd->rsrc_len = resource_size(r); 1573a082ec9SNeil Zhang hcd->regs = ehci_mv->op_regs; 1583a082ec9SNeil Zhang 1593a082ec9SNeil Zhang hcd->irq = platform_get_irq(pdev, 0); 1603a082ec9SNeil Zhang if (!hcd->irq) { 1613a082ec9SNeil Zhang dev_err(&pdev->dev, "Cannot get irq."); 1623a082ec9SNeil Zhang retval = -ENODEV; 1633a082ec9SNeil Zhang goto err_disable_clk; 1643a082ec9SNeil Zhang } 1653a082ec9SNeil Zhang 1663a082ec9SNeil Zhang ehci = hcd_to_ehci(hcd); 1673a082ec9SNeil Zhang ehci->caps = (struct ehci_caps *) ehci_mv->cap_regs; 1683a082ec9SNeil Zhang 1693a082ec9SNeil Zhang if (ehci_mv->mode == MV_USB_MODE_OTG) { 17035b55563SJulia Lawall ehci_mv->otg = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2); 1716f3ed4ecSFelipe Balbi if (IS_ERR(ehci_mv->otg)) { 1726f3ed4ecSFelipe Balbi retval = PTR_ERR(ehci_mv->otg); 1736f3ed4ecSFelipe Balbi 1746f3ed4ecSFelipe Balbi if (retval == -ENXIO) 1756f3ed4ecSFelipe Balbi dev_info(&pdev->dev, "MV_USB_MODE_OTG " 1766f3ed4ecSFelipe Balbi "must have CONFIG_USB_PHY enabled\n"); 1776f3ed4ecSFelipe Balbi else 1783a082ec9SNeil Zhang dev_err(&pdev->dev, 1793a082ec9SNeil Zhang "unable to find transceiver\n"); 1803a082ec9SNeil Zhang goto err_disable_clk; 1813a082ec9SNeil Zhang } 1823a082ec9SNeil Zhang 1836e13c650SHeikki Krogerus retval = otg_set_host(ehci_mv->otg->otg, &hcd->self); 1843a082ec9SNeil Zhang if (retval < 0) { 1853a082ec9SNeil Zhang dev_err(&pdev->dev, 1863a082ec9SNeil Zhang "unable to register with transceiver\n"); 1873a082ec9SNeil Zhang retval = -ENODEV; 18835b55563SJulia Lawall goto err_disable_clk; 1893a082ec9SNeil Zhang } 1903a082ec9SNeil Zhang /* otg will enable clock before use as host */ 1913a082ec9SNeil Zhang mv_ehci_disable(ehci_mv); 1923a082ec9SNeil Zhang } else { 193813e18b1SLubomir Rintel if (ehci_mv->set_vbus) 194813e18b1SLubomir Rintel ehci_mv->set_vbus(1); 1953a082ec9SNeil Zhang 1963a082ec9SNeil Zhang retval = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED); 1973a082ec9SNeil Zhang if (retval) { 1983a082ec9SNeil Zhang dev_err(&pdev->dev, 1993a082ec9SNeil Zhang "failed to add hcd with err %d\n", retval); 2003a082ec9SNeil Zhang goto err_set_vbus; 2013a082ec9SNeil Zhang } 2023c9740a1SPeter Chen device_wakeup_enable(hcd->self.controller); 2033a082ec9SNeil Zhang } 2043a082ec9SNeil Zhang 2053a082ec9SNeil Zhang dev_info(&pdev->dev, 2063a082ec9SNeil Zhang "successful find EHCI device with regs 0x%p irq %d" 2073a082ec9SNeil Zhang " working in %s mode\n", hcd->regs, hcd->irq, 2083a082ec9SNeil Zhang ehci_mv->mode == MV_USB_MODE_OTG ? "OTG" : "Host"); 2093a082ec9SNeil Zhang 2103a082ec9SNeil Zhang return 0; 2113a082ec9SNeil Zhang 2123a082ec9SNeil Zhang err_set_vbus: 213813e18b1SLubomir Rintel if (ehci_mv->set_vbus) 214813e18b1SLubomir Rintel ehci_mv->set_vbus(0); 2153a082ec9SNeil Zhang err_disable_clk: 2163a082ec9SNeil Zhang mv_ehci_disable(ehci_mv); 2173a082ec9SNeil Zhang err_put_hcd: 2183a082ec9SNeil Zhang usb_put_hcd(hcd); 2193a082ec9SNeil Zhang 2203a082ec9SNeil Zhang return retval; 2213a082ec9SNeil Zhang } 2223a082ec9SNeil Zhang 2233a082ec9SNeil Zhang static int mv_ehci_remove(struct platform_device *pdev) 2243a082ec9SNeil Zhang { 2250440fa3dSLubomir Rintel struct usb_hcd *hcd = platform_get_drvdata(pdev); 2260440fa3dSLubomir Rintel struct ehci_hcd_mv *ehci_mv = hcd_to_ehci_hcd_mv(hcd); 2273a082ec9SNeil Zhang 2283a082ec9SNeil Zhang if (hcd->rh_registered) 2293a082ec9SNeil Zhang usb_remove_hcd(hcd); 2303a082ec9SNeil Zhang 23135b55563SJulia Lawall if (!IS_ERR_OR_NULL(ehci_mv->otg)) 2326e13c650SHeikki Krogerus otg_set_host(ehci_mv->otg->otg, NULL); 2333a082ec9SNeil Zhang 2343a082ec9SNeil Zhang if (ehci_mv->mode == MV_USB_MODE_HOST) { 235813e18b1SLubomir Rintel if (ehci_mv->set_vbus) 236813e18b1SLubomir Rintel ehci_mv->set_vbus(0); 2373a082ec9SNeil Zhang 2383a082ec9SNeil Zhang mv_ehci_disable(ehci_mv); 2393a082ec9SNeil Zhang } 2403a082ec9SNeil Zhang 2413a082ec9SNeil Zhang usb_put_hcd(hcd); 2423a082ec9SNeil Zhang 2433a082ec9SNeil Zhang return 0; 2443a082ec9SNeil Zhang } 2453a082ec9SNeil Zhang 2463a082ec9SNeil Zhang MODULE_ALIAS("mv-ehci"); 2473a082ec9SNeil Zhang 2483a082ec9SNeil Zhang static const struct platform_device_id ehci_id_table[] = { 2493a082ec9SNeil Zhang {"pxa-u2oehci", PXA_U2OEHCI}, 2503a082ec9SNeil Zhang {"pxa-sph", PXA_SPH}, 2513a082ec9SNeil Zhang {"mmp3-hsic", MMP3_HSIC}, 2523a082ec9SNeil Zhang {"mmp3-fsic", MMP3_FSIC}, 2533a082ec9SNeil Zhang {}, 2543a082ec9SNeil Zhang }; 2553a082ec9SNeil Zhang 2563a082ec9SNeil Zhang static void mv_ehci_shutdown(struct platform_device *pdev) 2573a082ec9SNeil Zhang { 2580440fa3dSLubomir Rintel struct usb_hcd *hcd = platform_get_drvdata(pdev); 2593a082ec9SNeil Zhang 2603a082ec9SNeil Zhang if (!hcd->rh_registered) 2613a082ec9SNeil Zhang return; 2623a082ec9SNeil Zhang 2633a082ec9SNeil Zhang if (hcd->driver->shutdown) 2643a082ec9SNeil Zhang hcd->driver->shutdown(hcd); 2653a082ec9SNeil Zhang } 2663a082ec9SNeil Zhang 267813e18b1SLubomir Rintel static const struct of_device_id ehci_mv_dt_ids[] = { 268813e18b1SLubomir Rintel { .compatible = "marvell,pxau2o-ehci", }, 269813e18b1SLubomir Rintel {}, 270813e18b1SLubomir Rintel }; 271813e18b1SLubomir Rintel 2723a082ec9SNeil Zhang static struct platform_driver ehci_mv_driver = { 2733a082ec9SNeil Zhang .probe = mv_ehci_probe, 2743a082ec9SNeil Zhang .remove = mv_ehci_remove, 2753a082ec9SNeil Zhang .shutdown = mv_ehci_shutdown, 2763a082ec9SNeil Zhang .driver = { 2773a082ec9SNeil Zhang .name = "mv-ehci", 2783a082ec9SNeil Zhang .bus = &platform_bus_type, 279813e18b1SLubomir Rintel .of_match_table = ehci_mv_dt_ids, 2803a082ec9SNeil Zhang }, 2813a082ec9SNeil Zhang .id_table = ehci_id_table, 2823a082ec9SNeil Zhang }; 2830440fa3dSLubomir Rintel 2840440fa3dSLubomir Rintel static int __init ehci_platform_init(void) 2850440fa3dSLubomir Rintel { 2860440fa3dSLubomir Rintel if (usb_disabled()) 2870440fa3dSLubomir Rintel return -ENODEV; 2880440fa3dSLubomir Rintel 2890440fa3dSLubomir Rintel ehci_init_driver(&ehci_platform_hc_driver, &platform_overrides); 2900440fa3dSLubomir Rintel return platform_driver_register(&ehci_mv_driver); 2910440fa3dSLubomir Rintel } 2920440fa3dSLubomir Rintel module_init(ehci_platform_init); 2930440fa3dSLubomir Rintel 2940440fa3dSLubomir Rintel static void __exit ehci_platform_cleanup(void) 2950440fa3dSLubomir Rintel { 2960440fa3dSLubomir Rintel platform_driver_unregister(&ehci_mv_driver); 2970440fa3dSLubomir Rintel } 2980440fa3dSLubomir Rintel module_exit(ehci_platform_cleanup); 2990440fa3dSLubomir Rintel 3000440fa3dSLubomir Rintel MODULE_DESCRIPTION("Marvell EHCI driver"); 3010440fa3dSLubomir Rintel MODULE_AUTHOR("Chao Xie <chao.xie@marvell.com>"); 3020440fa3dSLubomir Rintel MODULE_AUTHOR("Neil Zhang <zhangwm@marvell.com>"); 3030440fa3dSLubomir Rintel MODULE_ALIAS("mv-ehci"); 3040440fa3dSLubomir Rintel MODULE_LICENSE("GPL"); 30570d0ba4cSLubomir Rintel MODULE_DEVICE_TABLE(of, ehci_mv_dt_ids); 306