2 * Glue code for the ISP1760 driver and bus
3 * Currently there is support for
6 * - PDEV (generic platform device centralized driver model)
8 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
12 #include <linux/usb.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/usb/isp1760.h>
17 #include <linux/usb/hcd.h>
19 #include "isp1760-hcd.h"
21 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
22 #include <linux/slab.h>
24 #include <linux/of_platform.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_gpio.h>
31 #include <linux/pci.h>
34 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
40 static int of_isp1760_probe(struct platform_device *dev)
42 struct isp1760 *drvdata;
43 struct device_node *dp = dev->dev.of_node;
45 struct resource memory;
48 resource_size_t res_len;
50 unsigned int devflags = 0;
51 enum of_gpio_flags gpio_flags;
54 drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
58 ret = of_address_to_resource(dp, 0, &memory);
64 res_len = resource_size(&memory);
66 res = request_mem_region(memory.start, res_len, dev_name(&dev->dev));
72 if (of_irq_map_one(dp, 0, &oirq)) {
77 virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
80 if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
81 devflags |= ISP1760_FLAG_ISP1761;
83 /* Some systems wire up only 16 of the 32 data lines */
84 of_property_read_u32(dp, "bus-width", &bus_width);
86 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
88 if (of_get_property(dp, "port1-otg", NULL) != NULL)
89 devflags |= ISP1760_FLAG_OTG_EN;
91 if (of_get_property(dp, "analog-oc", NULL) != NULL)
92 devflags |= ISP1760_FLAG_ANALOG_OC;
94 if (of_get_property(dp, "dack-polarity", NULL) != NULL)
95 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
97 if (of_get_property(dp, "dreq-polarity", NULL) != NULL)
98 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
100 drvdata->rst_gpio = of_get_gpio_flags(dp, 0, &gpio_flags);
101 if (gpio_is_valid(drvdata->rst_gpio)) {
102 ret = gpio_request(drvdata->rst_gpio, dev_name(&dev->dev));
104 if (!(gpio_flags & OF_GPIO_ACTIVE_LOW)) {
105 devflags |= ISP1760_FLAG_RESET_ACTIVE_HIGH;
106 gpio_direction_output(drvdata->rst_gpio, 0);
108 gpio_direction_output(drvdata->rst_gpio, 1);
111 drvdata->rst_gpio = ret;
115 drvdata->hcd = isp1760_register(memory.start, res_len, virq,
116 IRQF_SHARED, drvdata->rst_gpio,
117 &dev->dev, dev_name(&dev->dev),
119 if (IS_ERR(drvdata->hcd)) {
120 ret = PTR_ERR(drvdata->hcd);
124 dev_set_drvdata(&dev->dev, drvdata);
128 if (gpio_is_valid(drvdata->rst_gpio))
129 gpio_free(drvdata->rst_gpio);
131 release_mem_region(memory.start, res_len);
137 static int of_isp1760_remove(struct platform_device *dev)
139 struct isp1760 *drvdata = dev_get_drvdata(&dev->dev);
141 dev_set_drvdata(&dev->dev, NULL);
143 usb_remove_hcd(drvdata->hcd);
144 iounmap(drvdata->hcd->regs);
145 release_mem_region(drvdata->hcd->rsrc_start, drvdata->hcd->rsrc_len);
146 usb_put_hcd(drvdata->hcd);
148 if (gpio_is_valid(drvdata->rst_gpio))
149 gpio_free(drvdata->rst_gpio);
155 static const struct of_device_id of_isp1760_match[] = {
157 .compatible = "nxp,usb-isp1760",
160 .compatible = "nxp,usb-isp1761",
164 MODULE_DEVICE_TABLE(of, of_isp1760_match);
166 static struct platform_driver isp1760_of_driver = {
168 .name = "nxp-isp1760",
169 .owner = THIS_MODULE,
170 .of_match_table = of_isp1760_match,
172 .probe = of_isp1760_probe,
173 .remove = of_isp1760_remove,
178 static int __devinit isp1761_pci_probe(struct pci_dev *dev,
179 const struct pci_device_id *id)
185 unsigned int devflags = 0;
188 resource_size_t pci_mem_phy0;
189 resource_size_t memlength;
191 u8 __iomem *chip_addr;
193 resource_size_t nxp_pci_io_base;
194 resource_size_t iolength;
199 if (pci_enable_device(dev) < 0)
205 /* Grab the PLX PCI mem maped port start address we need */
206 nxp_pci_io_base = pci_resource_start(dev, 0);
207 iolength = pci_resource_len(dev, 0);
209 if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
210 printk(KERN_ERR "request region #1\n");
214 iobase = ioremap_nocache(nxp_pci_io_base, iolength);
216 printk(KERN_ERR "ioremap #1\n");
217 ret_status = -ENOMEM;
220 /* Grab the PLX PCI shared memory of the ISP 1761 we need */
221 pci_mem_phy0 = pci_resource_start(dev, 3);
222 memlength = pci_resource_len(dev, 3);
223 if (memlength < 0xffff) {
224 printk(KERN_ERR "memory length for this resource is wrong\n");
225 ret_status = -ENOMEM;
229 if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
230 printk(KERN_ERR "host controller already in use\n");
235 /* map available memory */
236 chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
238 printk(KERN_ERR "Error ioremap failed\n");
239 ret_status = -ENOMEM;
243 /* bad pci latencies can contribute to overruns */
244 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
246 pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
247 if (limit && limit < latency)
248 pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
251 /* Try to check whether we can access Scratch Register of
252 * Host Controller or not. The initial PCI access is retried until
253 * local init for the PCI bridge is completed
257 while ((reg_data != 0xFACE) && retry_count) {
258 /*by default host is in 16bit mode, so
259 * io operations at this stage must be 16 bit
261 writel(0xface, chip_addr + HC_SCRATCH_REG);
263 reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
269 /* Host Controller presence is detected by writing to scratch register
270 * and reading back and checking the contents are same or not
272 if (reg_data != 0xFACE) {
273 dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
274 ret_status = -ENOMEM;
280 /* configure PLX PCI chip to pass interrupts */
281 #define PLX_INT_CSR_REG 0x68
282 reg_data = readl(iobase + PLX_INT_CSR_REG);
284 writel(reg_data, iobase + PLX_INT_CSR_REG);
286 dev->dev.dma_mask = NULL;
287 hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
288 IRQF_SHARED, -ENOENT, &dev->dev, dev_name(&dev->dev),
291 ret_status = -ENODEV;
295 /* done with PLX IO access */
297 release_mem_region(nxp_pci_io_base, iolength);
299 pci_set_drvdata(dev, hcd);
303 release_mem_region(pci_mem_phy0, memlength);
307 release_mem_region(nxp_pci_io_base, iolength);
311 static void isp1761_pci_remove(struct pci_dev *dev)
315 hcd = pci_get_drvdata(dev);
319 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
322 pci_disable_device(dev);
325 static void isp1761_pci_shutdown(struct pci_dev *dev)
327 printk(KERN_ERR "ips1761_pci_shutdown\n");
330 static const struct pci_device_id isp1760_plx [] = {
332 .class = PCI_CLASS_BRIDGE_OTHER << 8,
334 .vendor = PCI_VENDOR_ID_PLX,
336 .subvendor = PCI_VENDOR_ID_PLX,
341 MODULE_DEVICE_TABLE(pci, isp1760_plx);
343 static struct pci_driver isp1761_pci_driver = {
345 .id_table = isp1760_plx,
346 .probe = isp1761_pci_probe,
347 .remove = isp1761_pci_remove,
348 .shutdown = isp1761_pci_shutdown,
352 static int __devinit isp1760_plat_probe(struct platform_device *pdev)
356 struct resource *mem_res;
357 struct resource *irq_res;
358 resource_size_t mem_size;
359 struct isp1760_platform_data *priv = pdev->dev.platform_data;
360 unsigned int devflags = 0;
361 unsigned long irqflags = IRQF_SHARED;
363 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
365 pr_warning("isp1760: Memory resource not available\n");
369 mem_size = resource_size(mem_res);
370 if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
371 pr_warning("isp1760: Cannot reserve the memory resource\n");
376 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
378 pr_warning("isp1760: IRQ resource not available\n");
381 irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
384 if (priv->is_isp1761)
385 devflags |= ISP1760_FLAG_ISP1761;
386 if (priv->bus_width_16)
387 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
389 devflags |= ISP1760_FLAG_OTG_EN;
391 devflags |= ISP1760_FLAG_ANALOG_OC;
392 if (priv->dack_polarity_high)
393 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
394 if (priv->dreq_polarity_high)
395 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
398 hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
400 &pdev->dev, dev_name(&pdev->dev), devflags);
402 pr_warning("isp1760: Failed to register the HCD device\n");
407 pr_info("ISP1760 USB device initialised\n");
411 release_mem_region(mem_res->start, mem_size);
416 static int __devexit isp1760_plat_remove(struct platform_device *pdev)
418 struct resource *mem_res;
419 resource_size_t mem_size;
421 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
422 mem_size = resource_size(mem_res);
423 release_mem_region(mem_res->start, mem_size);
428 static struct platform_driver isp1760_plat_driver = {
429 .probe = isp1760_plat_probe,
430 .remove = __devexit_p(isp1760_plat_remove),
436 static int __init isp1760_init(void)
438 int ret, any_ret = -ENODEV;
442 ret = platform_driver_register(&isp1760_plat_driver);
445 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
446 ret = platform_driver_register(&isp1760_of_driver);
451 ret = pci_register_driver(&isp1761_pci_driver);
460 module_init(isp1760_init);
462 static void __exit isp1760_exit(void)
464 platform_driver_unregister(&isp1760_plat_driver);
465 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
466 platform_driver_unregister(&isp1760_of_driver);
469 pci_unregister_driver(&isp1761_pci_driver);
473 module_exit(isp1760_exit);