usb: isp1760: Reorganize header files
[firefly-linux-kernel-4.4.55.git] / drivers / usb / host / isp1760-if.c
index 09254a43bc0191e4aff1633a050bd6bd0d533578..b1591c6cf54a52a4dae32102a4fb8ea44e73db33 100644 (file)
 #include <linux/usb.h>
 #include <linux/io.h>
 #include <linux/module.h>
+#include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/slab.h>
 #include <linux/usb/isp1760.h>
 #include <linux/usb/hcd.h>
 
 #include "isp1760-hcd.h"
-
-#if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
-#include <linux/slab.h>
-#include <linux/of.h>
-#include <linux/of_platform.h>
-#include <linux/of_address.h>
-#include <linux/of_irq.h>
-#include <linux/of_gpio.h>
-#endif
+#include "isp1760-regs.h"
 
 #ifdef CONFIG_PCI
 #include <linux/pci.h>
 #endif
 
-#if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
-struct isp1760 {
-       struct usb_hcd *hcd;
-       int rst_gpio;
-};
-
-static int of_isp1760_probe(struct platform_device *dev)
-{
-       struct isp1760 *drvdata;
-       struct device_node *dp = dev->dev.of_node;
-       struct resource *res;
-       struct resource memory;
-       int virq;
-       resource_size_t res_len;
-       int ret;
-       unsigned int devflags = 0;
-       enum of_gpio_flags gpio_flags;
-       u32 bus_width = 0;
-
-       drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
-       if (!drvdata)
-               return -ENOMEM;
-
-       ret = of_address_to_resource(dp, 0, &memory);
-       if (ret) {
-               ret = -ENXIO;
-               goto free_data;
-       }
-
-       res_len = resource_size(&memory);
-
-       res = request_mem_region(memory.start, res_len, dev_name(&dev->dev));
-       if (!res) {
-               ret = -EBUSY;
-               goto free_data;
-       }
-
-       virq = irq_of_parse_and_map(dp, 0);
-       if (!virq) {
-               ret = -ENODEV;
-               goto release_reg;
-       }
-
-       if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
-               devflags |= ISP1760_FLAG_ISP1761;
-
-       /* Some systems wire up only 16 of the 32 data lines */
-       of_property_read_u32(dp, "bus-width", &bus_width);
-       if (bus_width == 16)
-               devflags |= ISP1760_FLAG_BUS_WIDTH_16;
-
-       if (of_get_property(dp, "port1-otg", NULL) != NULL)
-               devflags |= ISP1760_FLAG_OTG_EN;
-
-       if (of_get_property(dp, "analog-oc", NULL) != NULL)
-               devflags |= ISP1760_FLAG_ANALOG_OC;
-
-       if (of_get_property(dp, "dack-polarity", NULL) != NULL)
-               devflags |= ISP1760_FLAG_DACK_POL_HIGH;
-
-       if (of_get_property(dp, "dreq-polarity", NULL) != NULL)
-               devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
-
-       drvdata->rst_gpio = of_get_gpio_flags(dp, 0, &gpio_flags);
-       if (gpio_is_valid(drvdata->rst_gpio)) {
-               ret = gpio_request(drvdata->rst_gpio, dev_name(&dev->dev));
-               if (!ret) {
-                       if (!(gpio_flags & OF_GPIO_ACTIVE_LOW)) {
-                               devflags |= ISP1760_FLAG_RESET_ACTIVE_HIGH;
-                               gpio_direction_output(drvdata->rst_gpio, 0);
-                       } else {
-                               gpio_direction_output(drvdata->rst_gpio, 1);
-                       }
-               } else {
-                       drvdata->rst_gpio = ret;
-               }
-       }
-
-       drvdata->hcd = isp1760_register(memory.start, res_len, virq,
-                                       IRQF_SHARED, drvdata->rst_gpio,
-                                       &dev->dev, dev_name(&dev->dev),
-                                       devflags);
-       if (IS_ERR(drvdata->hcd)) {
-               ret = PTR_ERR(drvdata->hcd);
-               goto free_gpio;
-       }
-
-       platform_set_drvdata(dev, drvdata);
-       return ret;
-
-free_gpio:
-       if (gpio_is_valid(drvdata->rst_gpio))
-               gpio_free(drvdata->rst_gpio);
-release_reg:
-       release_mem_region(memory.start, res_len);
-free_data:
-       kfree(drvdata);
-       return ret;
-}
-
-static int of_isp1760_remove(struct platform_device *dev)
-{
-       struct isp1760 *drvdata = platform_get_drvdata(dev);
-
-       usb_remove_hcd(drvdata->hcd);
-       iounmap(drvdata->hcd->regs);
-       release_mem_region(drvdata->hcd->rsrc_start, drvdata->hcd->rsrc_len);
-       usb_put_hcd(drvdata->hcd);
-
-       if (gpio_is_valid(drvdata->rst_gpio))
-               gpio_free(drvdata->rst_gpio);
-
-       kfree(drvdata);
-       return 0;
-}
-
-static const struct of_device_id of_isp1760_match[] = {
-       {
-               .compatible = "nxp,usb-isp1760",
-       },
-       {
-               .compatible = "nxp,usb-isp1761",
-       },
-       { },
-};
-MODULE_DEVICE_TABLE(of, of_isp1760_match);
-
-static struct platform_driver isp1760_of_driver = {
-       .driver = {
-               .name = "nxp-isp1760",
-               .of_match_table = of_isp1760_match,
-       },
-       .probe          = of_isp1760_probe,
-       .remove         = of_isp1760_remove,
-};
-#endif
-
 #ifdef CONFIG_PCI
-static int isp1761_pci_probe(struct pci_dev *dev,
-               const struct pci_device_id *id)
+static int isp1761_pci_init(struct pci_dev *dev)
 {
+       resource_size_t mem_start;
+       resource_size_t mem_length;
+       u8 __iomem *iobase;
        u8 latency, limit;
-       __u32 reg_data;
        int retry_count;
-       struct usb_hcd *hcd;
-       unsigned int devflags = 0;
-       int ret_status = 0;
-
-       resource_size_t pci_mem_phy0;
-       resource_size_t memlength;
-
-       u8 __iomem *chip_addr;
-       u8 __iomem *iobase;
-       resource_size_t nxp_pci_io_base;
-       resource_size_t iolength;
-
-       if (usb_disabled())
-               return -ENODEV;
-
-       if (pci_enable_device(dev) < 0)
-               return -ENODEV;
+       u32 reg_data;
 
-       if (!dev->irq)
-               return -ENODEV;
-
-       /* Grab the PLX PCI mem maped port start address we need  */
-       nxp_pci_io_base = pci_resource_start(dev, 0);
-       iolength = pci_resource_len(dev, 0);
-
-       if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
-               printk(KERN_ERR "request region #1\n");
-               return -EBUSY;
-       }
-
-       iobase = ioremap_nocache(nxp_pci_io_base, iolength);
-       if (!iobase) {
-               printk(KERN_ERR "ioremap #1\n");
-               ret_status = -ENOMEM;
-               goto cleanup1;
-       }
        /* Grab the PLX PCI shared memory of the ISP 1761 we need  */
-       pci_mem_phy0 = pci_resource_start(dev, 3);
-       memlength = pci_resource_len(dev, 3);
-       if (memlength < 0xffff) {
+       mem_start = pci_resource_start(dev, 3);
+       mem_length = pci_resource_len(dev, 3);
+       if (mem_length < 0xffff) {
                printk(KERN_ERR "memory length for this resource is wrong\n");
-               ret_status = -ENOMEM;
-               goto cleanup2;
+               return -ENOMEM;
        }
 
-       if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
+       if (!request_mem_region(mem_start, mem_length, "ISP-PCI")) {
                printk(KERN_ERR "host controller already in use\n");
-               ret_status = -EBUSY;
-               goto cleanup2;
+               return -EBUSY;
        }
 
        /* map available memory */
-       chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
-       if (!chip_addr) {
+       iobase = ioremap_nocache(mem_start, mem_length);
+       if (!iobase) {
                printk(KERN_ERR "Error ioremap failed\n");
-               ret_status = -ENOMEM;
-               goto cleanup3;
+               release_mem_region(mem_start, mem_length);
+               return -ENOMEM;
        }
 
        /* bad pci latencies can contribute to overruns */
@@ -252,24 +74,38 @@ static int isp1761_pci_probe(struct pci_dev *dev,
                /*by default host is in 16bit mode, so
                 * io operations at this stage must be 16 bit
                 * */
-               writel(0xface, chip_addr + HC_SCRATCH_REG);
+               writel(0xface, iobase + HC_SCRATCH_REG);
                udelay(100);
-               reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
+               reg_data = readl(iobase + HC_SCRATCH_REG) & 0x0000ffff;
                retry_count--;
        }
 
-       iounmap(chip_addr);
+       iounmap(iobase);
+       release_mem_region(mem_start, mem_length);
 
        /* Host Controller presence is detected by writing to scratch register
         * and reading back and checking the contents are same or not
         */
        if (reg_data != 0xFACE) {
                dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
-               ret_status = -ENOMEM;
-               goto cleanup3;
+               return -ENOMEM;
        }
 
-       pci_set_master(dev);
+       /* Grab the PLX PCI mem maped port start address we need  */
+       mem_start = pci_resource_start(dev, 0);
+       mem_length = pci_resource_len(dev, 0);
+
+       if (!request_mem_region(mem_start, mem_length, "ISP1761 IO MEM")) {
+               printk(KERN_ERR "request region #1\n");
+               return -EBUSY;
+       }
+
+       iobase = ioremap_nocache(mem_start, mem_length);
+       if (!iobase) {
+               printk(KERN_ERR "ioremap #1\n");
+               release_mem_region(mem_start, mem_length);
+               return -ENOMEM;
+       }
 
        /* configure PLX PCI chip to pass interrupts */
 #define PLX_INT_CSR_REG 0x68
@@ -277,41 +113,50 @@ static int isp1761_pci_probe(struct pci_dev *dev,
        reg_data |= 0x900;
        writel(reg_data, iobase + PLX_INT_CSR_REG);
 
-       dev->dev.dma_mask = NULL;
-       hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
-               IRQF_SHARED, -ENOENT, &dev->dev, dev_name(&dev->dev),
-               devflags);
-       if (IS_ERR(hcd)) {
-               ret_status = -ENODEV;
-               goto cleanup3;
-       }
-
        /* done with PLX IO access */
        iounmap(iobase);
-       release_mem_region(nxp_pci_io_base, iolength);
+       release_mem_region(mem_start, mem_length);
 
-       pci_set_drvdata(dev, hcd);
        return 0;
-
-cleanup3:
-       release_mem_region(pci_mem_phy0, memlength);
-cleanup2:
-       iounmap(iobase);
-cleanup1:
-       release_mem_region(nxp_pci_io_base, iolength);
-       return ret_status;
 }
 
-static void isp1761_pci_remove(struct pci_dev *dev)
+static int isp1761_pci_probe(struct pci_dev *dev,
+               const struct pci_device_id *id)
 {
-       struct usb_hcd *hcd;
+       unsigned int devflags = 0;
+       int ret;
 
-       hcd = pci_get_drvdata(dev);
+       if (usb_disabled())
+               return -ENODEV;
 
-       usb_remove_hcd(hcd);
-       iounmap(hcd->regs);
-       release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
-       usb_put_hcd(hcd);
+       if (!dev->irq)
+               return -ENODEV;
+
+       if (pci_enable_device(dev) < 0)
+               return -ENODEV;
+
+       ret = isp1761_pci_init(dev);
+       if (ret < 0)
+               goto error;
+
+       pci_set_master(dev);
+
+       dev->dev.dma_mask = NULL;
+       ret = isp1760_register(&dev->resource[3], dev->irq, IRQF_SHARED,
+                              &dev->dev, devflags);
+       if (ret < 0)
+               goto error;
+
+       return 0;
+
+error:
+       pci_disable_device(dev);
+       return ret;
+}
+
+static void isp1761_pci_remove(struct pci_dev *dev)
+{
+       isp1760_unregister(&dev->dev);
 
        pci_disable_device(dev);
 }
@@ -345,95 +190,94 @@ static struct pci_driver isp1761_pci_driver = {
 
 static int isp1760_plat_probe(struct platform_device *pdev)
 {
-       int ret = 0;
-       struct usb_hcd *hcd;
+       unsigned long irqflags = IRQF_SHARED;
+       unsigned int devflags = 0;
        struct resource *mem_res;
        struct resource *irq_res;
-       resource_size_t mem_size;
-       struct isp1760_platform_data *priv = dev_get_platdata(&pdev->dev);
-       unsigned int devflags = 0;
-       unsigned long irqflags = IRQF_SHARED;
+       int ret;
 
        mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-       if (!mem_res) {
-               pr_warning("isp1760: Memory resource not available\n");
-               ret = -ENODEV;
-               goto out;
-       }
-       mem_size = resource_size(mem_res);
-       if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
-               pr_warning("isp1760: Cannot reserve the memory resource\n");
-               ret = -EBUSY;
-               goto out;
-       }
 
        irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
        if (!irq_res) {
                pr_warning("isp1760: IRQ resource not available\n");
-               ret = -ENODEV;
-               goto cleanup;
+               return -ENODEV;
        }
 
        irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
 
-       if (priv) {
-               if (priv->is_isp1761)
+       if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
+               struct device_node *dp = pdev->dev.of_node;
+               u32 bus_width = 0;
+
+               if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
                        devflags |= ISP1760_FLAG_ISP1761;
-               if (priv->bus_width_16)
+
+               /* Some systems wire up only 16 of the 32 data lines */
+               of_property_read_u32(dp, "bus-width", &bus_width);
+               if (bus_width == 16)
                        devflags |= ISP1760_FLAG_BUS_WIDTH_16;
-               if (priv->port1_otg)
+
+               if (of_property_read_bool(dp, "port1-otg"))
                        devflags |= ISP1760_FLAG_OTG_EN;
-               if (priv->analog_oc)
+
+               if (of_property_read_bool(dp, "analog-oc"))
                        devflags |= ISP1760_FLAG_ANALOG_OC;
-               if (priv->dack_polarity_high)
-                       devflags |= ISP1760_FLAG_DACK_POL_HIGH;
-               if (priv->dreq_polarity_high)
-                       devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
-       }
 
-       hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
-                              irqflags, -ENOENT,
-                              &pdev->dev, dev_name(&pdev->dev), devflags);
+               if (of_property_read_bool(dp, "dack-polarity"))
+                       devflags |= ISP1760_FLAG_DACK_POL_HIGH;
 
-       platform_set_drvdata(pdev, hcd);
+               if (of_property_read_bool(dp, "dreq-polarity"))
+                       devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
+       } else if (dev_get_platdata(&pdev->dev)) {
+               struct isp1760_platform_data *pdata =
+                       dev_get_platdata(&pdev->dev);
 
-       if (IS_ERR(hcd)) {
-               pr_warning("isp1760: Failed to register the HCD device\n");
-               ret = -ENODEV;
-               goto cleanup;
+               if (pdata->is_isp1761)
+                       devflags |= ISP1760_FLAG_ISP1761;
+               if (pdata->bus_width_16)
+                       devflags |= ISP1760_FLAG_BUS_WIDTH_16;
+               if (pdata->port1_otg)
+                       devflags |= ISP1760_FLAG_OTG_EN;
+               if (pdata->analog_oc)
+                       devflags |= ISP1760_FLAG_ANALOG_OC;
+               if (pdata->dack_polarity_high)
+                       devflags |= ISP1760_FLAG_DACK_POL_HIGH;
+               if (pdata->dreq_polarity_high)
+                       devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
        }
 
-       pr_info("ISP1760 USB device initialised\n");
-       return ret;
+       ret = isp1760_register(mem_res, irq_res->start, irqflags, &pdev->dev,
+                              devflags);
+       if (ret < 0)
+               return ret;
 
-cleanup:
-       release_mem_region(mem_res->start, mem_size);
-out:
-       return ret;
+       pr_info("ISP1760 USB device initialised\n");
+       return 0;
 }
 
 static int isp1760_plat_remove(struct platform_device *pdev)
 {
-       struct resource *mem_res;
-       resource_size_t mem_size;
-       struct usb_hcd *hcd = platform_get_drvdata(pdev);
-
-       usb_remove_hcd(hcd);
-
-       mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-       mem_size = resource_size(mem_res);
-       release_mem_region(mem_res->start, mem_size);
-
-       usb_put_hcd(hcd);
+       isp1760_unregister(&pdev->dev);
 
        return 0;
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id isp1760_of_match[] = {
+       { .compatible = "nxp,usb-isp1760", },
+       { .compatible = "nxp,usb-isp1761", },
+       { },
+};
+MODULE_DEVICE_TABLE(of, isp1760_of_match);
+#endif
+
 static struct platform_driver isp1760_plat_driver = {
        .probe  = isp1760_plat_probe,
        .remove = isp1760_plat_remove,
        .driver = {
                .name   = "isp1760",
+               .of_match_table = of_match_ptr(isp1760_of_match),
        },
 };
 
@@ -441,16 +285,11 @@ static int __init isp1760_init(void)
 {
        int ret, any_ret = -ENODEV;
 
-       init_kmem_once();
+       isp1760_init_kmem_once();
 
        ret = platform_driver_register(&isp1760_plat_driver);
        if (!ret)
                any_ret = 0;
-#if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
-       ret = platform_driver_register(&isp1760_of_driver);
-       if (!ret)
-               any_ret = 0;
-#endif
 #ifdef CONFIG_PCI
        ret = pci_register_driver(&isp1761_pci_driver);
        if (!ret)
@@ -458,7 +297,7 @@ static int __init isp1760_init(void)
 #endif
 
        if (any_ret)
-               deinit_kmem_cache();
+               isp1760_deinit_kmem_cache();
        return any_ret;
 }
 module_init(isp1760_init);
@@ -466,12 +305,9 @@ module_init(isp1760_init);
 static void __exit isp1760_exit(void)
 {
        platform_driver_unregister(&isp1760_plat_driver);
-#if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
-       platform_driver_unregister(&isp1760_of_driver);
-#endif
 #ifdef CONFIG_PCI
        pci_unregister_driver(&isp1761_pci_driver);
 #endif
-       deinit_kmem_cache();
+       isp1760_deinit_kmem_cache();
 }
 module_exit(isp1760_exit);