Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Nuvoton NPCM7xx driver for EHCI HCD
0004  *
0005  * Copyright (C) 2018 Nuvoton Technologies,
0006  * Avi Fishman <avi.fishman@nuvoton.com> <avifishman70@gmail.com>
0007  * Tomer Maimon <tomer.maimon@nuvoton.com> <tmaimon77@gmail.com>
0008  *
0009  * Based on various ehci-spear.c driver
0010  */
0011 
0012 
0013 #include <linux/dma-mapping.h>
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/of.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/pm.h>
0020 #include <linux/usb.h>
0021 #include <linux/usb/hcd.h>
0022 
0023 #include "ehci.h"
0024 
0025 #include <linux/regmap.h>
0026 #include <linux/mfd/syscon.h>
0027 
0028 #define DRIVER_DESC "EHCI npcm7xx driver"
0029 
0030 static const char hcd_name[] = "npcm7xx-ehci";
0031 
0032 #define  USB2PHYCTL_OFFSET 0x144
0033 
0034 #define  IPSRST2_OFFSET 0x24
0035 #define  IPSRST3_OFFSET 0x34
0036 
0037 
0038 static struct hc_driver __read_mostly ehci_npcm7xx_hc_driver;
0039 
0040 static int __maybe_unused ehci_npcm7xx_drv_suspend(struct device *dev)
0041 {
0042     struct usb_hcd *hcd = dev_get_drvdata(dev);
0043     bool do_wakeup = device_may_wakeup(dev);
0044 
0045     return ehci_suspend(hcd, do_wakeup);
0046 }
0047 
0048 static int __maybe_unused ehci_npcm7xx_drv_resume(struct device *dev)
0049 {
0050     struct usb_hcd *hcd = dev_get_drvdata(dev);
0051 
0052     ehci_resume(hcd, false);
0053     return 0;
0054 }
0055 
0056 static SIMPLE_DEV_PM_OPS(ehci_npcm7xx_pm_ops, ehci_npcm7xx_drv_suspend,
0057         ehci_npcm7xx_drv_resume);
0058 
0059 static int npcm7xx_ehci_hcd_drv_probe(struct platform_device *pdev)
0060 {
0061     struct usb_hcd *hcd;
0062     struct resource *res;
0063     struct regmap *gcr_regmap;
0064     struct regmap *rst_regmap;
0065     const struct hc_driver *driver = &ehci_npcm7xx_hc_driver;
0066     int irq;
0067     int retval;
0068 
0069     dev_dbg(&pdev->dev, "initializing npcm7xx ehci USB Controller\n");
0070 
0071     gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
0072     if (IS_ERR(gcr_regmap)) {
0073         dev_err(&pdev->dev, "%s: failed to find nuvoton,npcm750-gcr\n",
0074             __func__);
0075         return PTR_ERR(gcr_regmap);
0076     }
0077 
0078     rst_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-rst");
0079     if (IS_ERR(rst_regmap)) {
0080         dev_err(&pdev->dev, "%s: failed to find nuvoton,npcm750-rst\n",
0081             __func__);
0082         return PTR_ERR(rst_regmap);
0083     }
0084 
0085     /********* phy init  ******/
0086     // reset usb host
0087     regmap_update_bits(rst_regmap, IPSRST2_OFFSET,
0088             (0x1 << 26), (0x1 << 26));
0089     regmap_update_bits(rst_regmap, IPSRST3_OFFSET,
0090             (0x1 << 25), (0x1 << 25));
0091     regmap_update_bits(gcr_regmap, USB2PHYCTL_OFFSET,
0092             (0x1 << 28), 0);
0093 
0094     udelay(1);
0095 
0096     // enable phy
0097     regmap_update_bits(rst_regmap, IPSRST3_OFFSET,
0098             (0x1 << 25), 0);
0099 
0100     udelay(50); // enable phy
0101 
0102     regmap_update_bits(gcr_regmap, USB2PHYCTL_OFFSET,
0103             (0x1 << 28), (0x1 << 28));
0104 
0105     // enable host
0106     regmap_update_bits(rst_regmap, IPSRST2_OFFSET,
0107             (0x1 << 26), 0);
0108 
0109     if (usb_disabled())
0110         return -ENODEV;
0111 
0112     irq = platform_get_irq(pdev, 0);
0113     if (irq < 0) {
0114         retval = irq;
0115         goto fail;
0116     }
0117 
0118     /*
0119      * Right now device-tree probed devices don't get dma_mask set.
0120      * Since shared usb code relies on it, set it here for now.
0121      * Once we have dma capability bindings this can go away.
0122      */
0123     retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
0124     if (retval)
0125         goto fail;
0126 
0127     hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
0128     if (!hcd) {
0129         retval = -ENOMEM;
0130         goto fail;
0131     }
0132 
0133     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0134     hcd->regs = devm_ioremap_resource(&pdev->dev, res);
0135     if (IS_ERR(hcd->regs)) {
0136         retval = PTR_ERR(hcd->regs);
0137         goto err_put_hcd;
0138     }
0139     hcd->rsrc_start = res->start;
0140     hcd->rsrc_len = resource_size(res);
0141 
0142     /* registers start at offset 0x0 */
0143     hcd_to_ehci(hcd)->caps = hcd->regs;
0144 
0145     retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
0146     if (retval)
0147         goto err_put_hcd;
0148 
0149     device_wakeup_enable(hcd->self.controller);
0150     return retval;
0151 
0152 err_put_hcd:
0153     usb_put_hcd(hcd);
0154 fail:
0155     dev_err(&pdev->dev, "init fail, %d\n", retval);
0156 
0157     return retval;
0158 }
0159 
0160 static int npcm7xx_ehci_hcd_drv_remove(struct platform_device *pdev)
0161 {
0162     struct usb_hcd *hcd = platform_get_drvdata(pdev);
0163 
0164     usb_remove_hcd(hcd);
0165 
0166     usb_put_hcd(hcd);
0167 
0168     return 0;
0169 }
0170 
0171 static const struct of_device_id npcm7xx_ehci_id_table[] = {
0172     { .compatible = "nuvoton,npcm750-ehci" },
0173     { },
0174 };
0175 MODULE_DEVICE_TABLE(of, npcm7xx_ehci_id_table);
0176 
0177 static struct platform_driver npcm7xx_ehci_hcd_driver = {
0178     .probe      = npcm7xx_ehci_hcd_drv_probe,
0179     .remove     = npcm7xx_ehci_hcd_drv_remove,
0180     .shutdown   = usb_hcd_platform_shutdown,
0181     .driver     = {
0182         .name = "npcm7xx-ehci",
0183         .bus = &platform_bus_type,
0184         .pm = pm_ptr(&ehci_npcm7xx_pm_ops),
0185         .of_match_table = npcm7xx_ehci_id_table,
0186     }
0187 };
0188 
0189 static int __init ehci_npcm7xx_init(void)
0190 {
0191     if (usb_disabled())
0192         return -ENODEV;
0193 
0194     pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0195 
0196     ehci_init_driver(&ehci_npcm7xx_hc_driver, NULL);
0197     return platform_driver_register(&npcm7xx_ehci_hcd_driver);
0198 }
0199 module_init(ehci_npcm7xx_init);
0200 
0201 static void __exit ehci_npcm7xx_cleanup(void)
0202 {
0203     platform_driver_unregister(&npcm7xx_ehci_hcd_driver);
0204 }
0205 module_exit(ehci_npcm7xx_cleanup);
0206 
0207 MODULE_DESCRIPTION(DRIVER_DESC);
0208 MODULE_ALIAS("platform:npcm7xx-ehci");
0209 MODULE_AUTHOR("Avi Fishman");
0210 MODULE_LICENSE("GPL v2");