Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Glue code for the ISP1760 driver and bus
0004  * Currently there is support for
0005  * - OpenFirmware
0006  * - PCI
0007  * - PDEV (generic platform device centralized driver model)
0008  *
0009  * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
0010  * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org>
0011  *
0012  */
0013 
0014 #include <linux/usb.h>
0015 #include <linux/io.h>
0016 #include <linux/irq.h>
0017 #include <linux/module.h>
0018 #include <linux/of.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/slab.h>
0021 #include <linux/usb/hcd.h>
0022 #include <linux/usb/otg.h>
0023 
0024 #include "isp1760-core.h"
0025 #include "isp1760-regs.h"
0026 
0027 #ifdef CONFIG_USB_PCI
0028 #include <linux/pci.h>
0029 #endif
0030 
0031 #ifdef CONFIG_USB_PCI
0032 static int isp1761_pci_init(struct pci_dev *dev)
0033 {
0034     resource_size_t mem_start;
0035     resource_size_t mem_length;
0036     u8 __iomem *iobase;
0037     u8 latency, limit;
0038     int retry_count;
0039     u32 reg_data;
0040 
0041     /* Grab the PLX PCI shared memory of the ISP 1761 we need  */
0042     mem_start = pci_resource_start(dev, 3);
0043     mem_length = pci_resource_len(dev, 3);
0044     if (mem_length < 0xffff) {
0045         printk(KERN_ERR "memory length for this resource is wrong\n");
0046         return -ENOMEM;
0047     }
0048 
0049     if (!request_mem_region(mem_start, mem_length, "ISP-PCI")) {
0050         printk(KERN_ERR "host controller already in use\n");
0051         return -EBUSY;
0052     }
0053 
0054     /* map available memory */
0055     iobase = ioremap(mem_start, mem_length);
0056     if (!iobase) {
0057         printk(KERN_ERR "Error ioremap failed\n");
0058         release_mem_region(mem_start, mem_length);
0059         return -ENOMEM;
0060     }
0061 
0062     /* bad pci latencies can contribute to overruns */
0063     pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
0064     if (latency) {
0065         pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
0066         if (limit && limit < latency)
0067             pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
0068     }
0069 
0070     /* Try to check whether we can access Scratch Register of
0071      * Host Controller or not. The initial PCI access is retried until
0072      * local init for the PCI bridge is completed
0073      */
0074     retry_count = 20;
0075     reg_data = 0;
0076     while ((reg_data != 0xFACE) && retry_count) {
0077         /*by default host is in 16bit mode, so
0078          * io operations at this stage must be 16 bit
0079          * */
0080         writel(0xface, iobase + ISP176x_HC_SCRATCH);
0081         udelay(100);
0082         reg_data = readl(iobase + ISP176x_HC_SCRATCH) & 0x0000ffff;
0083         retry_count--;
0084     }
0085 
0086     iounmap(iobase);
0087     release_mem_region(mem_start, mem_length);
0088 
0089     /* Host Controller presence is detected by writing to scratch register
0090      * and reading back and checking the contents are same or not
0091      */
0092     if (reg_data != 0xFACE) {
0093         dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
0094         return -ENOMEM;
0095     }
0096 
0097     /* Grab the PLX PCI mem maped port start address we need  */
0098     mem_start = pci_resource_start(dev, 0);
0099     mem_length = pci_resource_len(dev, 0);
0100 
0101     if (!request_mem_region(mem_start, mem_length, "ISP1761 IO MEM")) {
0102         printk(KERN_ERR "request region #1\n");
0103         return -EBUSY;
0104     }
0105 
0106     iobase = ioremap(mem_start, mem_length);
0107     if (!iobase) {
0108         printk(KERN_ERR "ioremap #1\n");
0109         release_mem_region(mem_start, mem_length);
0110         return -ENOMEM;
0111     }
0112 
0113     /* configure PLX PCI chip to pass interrupts */
0114 #define PLX_INT_CSR_REG 0x68
0115     reg_data = readl(iobase + PLX_INT_CSR_REG);
0116     reg_data |= 0x900;
0117     writel(reg_data, iobase + PLX_INT_CSR_REG);
0118 
0119     /* done with PLX IO access */
0120     iounmap(iobase);
0121     release_mem_region(mem_start, mem_length);
0122 
0123     return 0;
0124 }
0125 
0126 static int isp1761_pci_probe(struct pci_dev *dev,
0127         const struct pci_device_id *id)
0128 {
0129     unsigned int devflags = 0;
0130     int ret;
0131 
0132     if (!dev->irq)
0133         return -ENODEV;
0134 
0135     if (pci_enable_device(dev) < 0)
0136         return -ENODEV;
0137 
0138     ret = isp1761_pci_init(dev);
0139     if (ret < 0)
0140         goto error;
0141 
0142     pci_set_master(dev);
0143 
0144     ret = isp1760_register(&dev->resource[3], dev->irq, 0, &dev->dev,
0145                    devflags);
0146     if (ret < 0)
0147         goto error;
0148 
0149     return 0;
0150 
0151 error:
0152     pci_disable_device(dev);
0153     return ret;
0154 }
0155 
0156 static void isp1761_pci_remove(struct pci_dev *dev)
0157 {
0158     isp1760_unregister(&dev->dev);
0159 
0160     pci_disable_device(dev);
0161 }
0162 
0163 static void isp1761_pci_shutdown(struct pci_dev *dev)
0164 {
0165     printk(KERN_ERR "ips1761_pci_shutdown\n");
0166 }
0167 
0168 static const struct pci_device_id isp1760_plx[] = {
0169     {
0170         .class          = PCI_CLASS_BRIDGE_OTHER << 8,
0171         .class_mask     = ~0,
0172         .vendor     = PCI_VENDOR_ID_PLX,
0173         .device     = 0x5406,
0174         .subvendor  = PCI_VENDOR_ID_PLX,
0175         .subdevice  = 0x9054,
0176     },
0177     { }
0178 };
0179 MODULE_DEVICE_TABLE(pci, isp1760_plx);
0180 
0181 static struct pci_driver isp1761_pci_driver = {
0182     .name =         "isp1760",
0183     .id_table =     isp1760_plx,
0184     .probe =        isp1761_pci_probe,
0185     .remove =       isp1761_pci_remove,
0186     .shutdown =     isp1761_pci_shutdown,
0187 };
0188 #endif
0189 
0190 static int isp1760_plat_probe(struct platform_device *pdev)
0191 {
0192     unsigned long irqflags;
0193     unsigned int devflags = 0;
0194     struct resource *mem_res;
0195     int irq;
0196     int ret;
0197 
0198     mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0199 
0200     irq = platform_get_irq(pdev, 0);
0201     if (irq < 0)
0202         return irq;
0203     irqflags = irq_get_trigger_type(irq);
0204 
0205     if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
0206         struct device_node *dp = pdev->dev.of_node;
0207         u32 bus_width = 0;
0208 
0209         if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
0210             devflags |= ISP1760_FLAG_ISP1761;
0211 
0212         if (of_device_is_compatible(dp, "nxp,usb-isp1763"))
0213             devflags |= ISP1760_FLAG_ISP1763;
0214 
0215         /*
0216          * Some systems wire up only 8 of 16 data lines or
0217          * 16 of the 32 data lines
0218          */
0219         of_property_read_u32(dp, "bus-width", &bus_width);
0220         if (bus_width == 16)
0221             devflags |= ISP1760_FLAG_BUS_WIDTH_16;
0222         else if (bus_width == 8)
0223             devflags |= ISP1760_FLAG_BUS_WIDTH_8;
0224 
0225         if (usb_get_dr_mode(&pdev->dev) == USB_DR_MODE_PERIPHERAL)
0226             devflags |= ISP1760_FLAG_PERIPHERAL_EN;
0227 
0228         if (of_property_read_bool(dp, "analog-oc"))
0229             devflags |= ISP1760_FLAG_ANALOG_OC;
0230 
0231         if (of_property_read_bool(dp, "dack-polarity"))
0232             devflags |= ISP1760_FLAG_DACK_POL_HIGH;
0233 
0234         if (of_property_read_bool(dp, "dreq-polarity"))
0235             devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
0236     } else {
0237         pr_err("isp1760: no platform data\n");
0238         return -ENXIO;
0239     }
0240 
0241     ret = isp1760_register(mem_res, irq, irqflags, &pdev->dev, devflags);
0242     if (ret < 0)
0243         return ret;
0244 
0245     pr_info("ISP1760 USB device initialised\n");
0246     return 0;
0247 }
0248 
0249 static int isp1760_plat_remove(struct platform_device *pdev)
0250 {
0251     isp1760_unregister(&pdev->dev);
0252 
0253     return 0;
0254 }
0255 
0256 #ifdef CONFIG_OF
0257 static const struct of_device_id isp1760_of_match[] = {
0258     { .compatible = "nxp,usb-isp1760", },
0259     { .compatible = "nxp,usb-isp1761", },
0260     { .compatible = "nxp,usb-isp1763", },
0261     { },
0262 };
0263 MODULE_DEVICE_TABLE(of, isp1760_of_match);
0264 #endif
0265 
0266 static struct platform_driver isp1760_plat_driver = {
0267     .probe  = isp1760_plat_probe,
0268     .remove = isp1760_plat_remove,
0269     .driver = {
0270         .name   = "isp1760",
0271         .of_match_table = of_match_ptr(isp1760_of_match),
0272     },
0273 };
0274 
0275 static int __init isp1760_init(void)
0276 {
0277     int ret, any_ret = -ENODEV;
0278 
0279     isp1760_init_kmem_once();
0280 
0281     ret = platform_driver_register(&isp1760_plat_driver);
0282     if (!ret)
0283         any_ret = 0;
0284 #ifdef CONFIG_USB_PCI
0285     ret = pci_register_driver(&isp1761_pci_driver);
0286     if (!ret)
0287         any_ret = 0;
0288 #endif
0289 
0290     if (any_ret)
0291         isp1760_deinit_kmem_cache();
0292     return any_ret;
0293 }
0294 module_init(isp1760_init);
0295 
0296 static void __exit isp1760_exit(void)
0297 {
0298     platform_driver_unregister(&isp1760_plat_driver);
0299 #ifdef CONFIG_USB_PCI
0300     pci_unregister_driver(&isp1761_pci_driver);
0301 #endif
0302     isp1760_deinit_kmem_cache();
0303 }
0304 module_exit(isp1760_exit);