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
;
47 resource_size_t res_len
;
49 unsigned int devflags
= 0;
50 enum of_gpio_flags gpio_flags
;
53 drvdata
= kzalloc(sizeof(*drvdata
), GFP_KERNEL
);
57 ret
= of_address_to_resource(dp
, 0, &memory
);
63 res_len
= resource_size(&memory
);
65 res
= request_mem_region(memory
.start
, res_len
, dev_name(&dev
->dev
));
71 virq
= irq_of_parse_and_map(dp
, 0);
77 if (of_device_is_compatible(dp
, "nxp,usb-isp1761"))
78 devflags
|= ISP1760_FLAG_ISP1761
;
80 /* Some systems wire up only 16 of the 32 data lines */
81 of_property_read_u32(dp
, "bus-width", &bus_width
);
83 devflags
|= ISP1760_FLAG_BUS_WIDTH_16
;
85 if (of_get_property(dp
, "port1-otg", NULL
) != NULL
)
86 devflags
|= ISP1760_FLAG_OTG_EN
;
88 if (of_get_property(dp
, "analog-oc", NULL
) != NULL
)
89 devflags
|= ISP1760_FLAG_ANALOG_OC
;
91 if (of_get_property(dp
, "dack-polarity", NULL
) != NULL
)
92 devflags
|= ISP1760_FLAG_DACK_POL_HIGH
;
94 if (of_get_property(dp
, "dreq-polarity", NULL
) != NULL
)
95 devflags
|= ISP1760_FLAG_DREQ_POL_HIGH
;
97 drvdata
->rst_gpio
= of_get_gpio_flags(dp
, 0, &gpio_flags
);
98 if (gpio_is_valid(drvdata
->rst_gpio
)) {
99 ret
= gpio_request(drvdata
->rst_gpio
, dev_name(&dev
->dev
));
101 if (!(gpio_flags
& OF_GPIO_ACTIVE_LOW
)) {
102 devflags
|= ISP1760_FLAG_RESET_ACTIVE_HIGH
;
103 gpio_direction_output(drvdata
->rst_gpio
, 0);
105 gpio_direction_output(drvdata
->rst_gpio
, 1);
108 drvdata
->rst_gpio
= ret
;
112 drvdata
->hcd
= isp1760_register(memory
.start
, res_len
, virq
,
113 IRQF_SHARED
, drvdata
->rst_gpio
,
114 &dev
->dev
, dev_name(&dev
->dev
),
116 if (IS_ERR(drvdata
->hcd
)) {
117 ret
= PTR_ERR(drvdata
->hcd
);
121 platform_set_drvdata(dev
, drvdata
);
125 if (gpio_is_valid(drvdata
->rst_gpio
))
126 gpio_free(drvdata
->rst_gpio
);
128 release_mem_region(memory
.start
, res_len
);
134 static int of_isp1760_remove(struct platform_device
*dev
)
136 struct isp1760
*drvdata
= platform_get_drvdata(dev
);
138 usb_remove_hcd(drvdata
->hcd
);
139 iounmap(drvdata
->hcd
->regs
);
140 release_mem_region(drvdata
->hcd
->rsrc_start
, drvdata
->hcd
->rsrc_len
);
141 usb_put_hcd(drvdata
->hcd
);
143 if (gpio_is_valid(drvdata
->rst_gpio
))
144 gpio_free(drvdata
->rst_gpio
);
150 static const struct of_device_id of_isp1760_match
[] = {
152 .compatible
= "nxp,usb-isp1760",
155 .compatible
= "nxp,usb-isp1761",
159 MODULE_DEVICE_TABLE(of
, of_isp1760_match
);
161 static struct platform_driver isp1760_of_driver
= {
163 .name
= "nxp-isp1760",
164 .owner
= THIS_MODULE
,
165 .of_match_table
= of_isp1760_match
,
167 .probe
= of_isp1760_probe
,
168 .remove
= of_isp1760_remove
,
173 static int isp1761_pci_probe(struct pci_dev
*dev
,
174 const struct pci_device_id
*id
)
180 unsigned int devflags
= 0;
183 resource_size_t pci_mem_phy0
;
184 resource_size_t memlength
;
186 u8 __iomem
*chip_addr
;
188 resource_size_t nxp_pci_io_base
;
189 resource_size_t iolength
;
194 if (pci_enable_device(dev
) < 0)
200 /* Grab the PLX PCI mem maped port start address we need */
201 nxp_pci_io_base
= pci_resource_start(dev
, 0);
202 iolength
= pci_resource_len(dev
, 0);
204 if (!request_mem_region(nxp_pci_io_base
, iolength
, "ISP1761 IO MEM")) {
205 printk(KERN_ERR
"request region #1\n");
209 iobase
= ioremap_nocache(nxp_pci_io_base
, iolength
);
211 printk(KERN_ERR
"ioremap #1\n");
212 ret_status
= -ENOMEM
;
215 /* Grab the PLX PCI shared memory of the ISP 1761 we need */
216 pci_mem_phy0
= pci_resource_start(dev
, 3);
217 memlength
= pci_resource_len(dev
, 3);
218 if (memlength
< 0xffff) {
219 printk(KERN_ERR
"memory length for this resource is wrong\n");
220 ret_status
= -ENOMEM
;
224 if (!request_mem_region(pci_mem_phy0
, memlength
, "ISP-PCI")) {
225 printk(KERN_ERR
"host controller already in use\n");
230 /* map available memory */
231 chip_addr
= ioremap_nocache(pci_mem_phy0
,memlength
);
233 printk(KERN_ERR
"Error ioremap failed\n");
234 ret_status
= -ENOMEM
;
238 /* bad pci latencies can contribute to overruns */
239 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &latency
);
241 pci_read_config_byte(dev
, PCI_MAX_LAT
, &limit
);
242 if (limit
&& limit
< latency
)
243 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, limit
);
246 /* Try to check whether we can access Scratch Register of
247 * Host Controller or not. The initial PCI access is retried until
248 * local init for the PCI bridge is completed
252 while ((reg_data
!= 0xFACE) && retry_count
) {
253 /*by default host is in 16bit mode, so
254 * io operations at this stage must be 16 bit
256 writel(0xface, chip_addr
+ HC_SCRATCH_REG
);
258 reg_data
= readl(chip_addr
+ HC_SCRATCH_REG
) & 0x0000ffff;
264 /* Host Controller presence is detected by writing to scratch register
265 * and reading back and checking the contents are same or not
267 if (reg_data
!= 0xFACE) {
268 dev_err(&dev
->dev
, "scratch register mismatch %x\n", reg_data
);
269 ret_status
= -ENOMEM
;
275 /* configure PLX PCI chip to pass interrupts */
276 #define PLX_INT_CSR_REG 0x68
277 reg_data
= readl(iobase
+ PLX_INT_CSR_REG
);
279 writel(reg_data
, iobase
+ PLX_INT_CSR_REG
);
281 dev
->dev
.dma_mask
= NULL
;
282 hcd
= isp1760_register(pci_mem_phy0
, memlength
, dev
->irq
,
283 IRQF_SHARED
, -ENOENT
, &dev
->dev
, dev_name(&dev
->dev
),
286 ret_status
= -ENODEV
;
290 /* done with PLX IO access */
292 release_mem_region(nxp_pci_io_base
, iolength
);
294 pci_set_drvdata(dev
, hcd
);
298 release_mem_region(pci_mem_phy0
, memlength
);
302 release_mem_region(nxp_pci_io_base
, iolength
);
306 static void isp1761_pci_remove(struct pci_dev
*dev
)
310 hcd
= pci_get_drvdata(dev
);
314 release_mem_region(hcd
->rsrc_start
, hcd
->rsrc_len
);
317 pci_disable_device(dev
);
320 static void isp1761_pci_shutdown(struct pci_dev
*dev
)
322 printk(KERN_ERR
"ips1761_pci_shutdown\n");
325 static const struct pci_device_id isp1760_plx
[] = {
327 .class = PCI_CLASS_BRIDGE_OTHER
<< 8,
329 .vendor
= PCI_VENDOR_ID_PLX
,
331 .subvendor
= PCI_VENDOR_ID_PLX
,
336 MODULE_DEVICE_TABLE(pci
, isp1760_plx
);
338 static struct pci_driver isp1761_pci_driver
= {
340 .id_table
= isp1760_plx
,
341 .probe
= isp1761_pci_probe
,
342 .remove
= isp1761_pci_remove
,
343 .shutdown
= isp1761_pci_shutdown
,
347 static int isp1760_plat_probe(struct platform_device
*pdev
)
351 struct resource
*mem_res
;
352 struct resource
*irq_res
;
353 resource_size_t mem_size
;
354 struct isp1760_platform_data
*priv
= pdev
->dev
.platform_data
;
355 unsigned int devflags
= 0;
356 unsigned long irqflags
= IRQF_SHARED
;
358 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
360 pr_warning("isp1760: Memory resource not available\n");
364 mem_size
= resource_size(mem_res
);
365 if (!request_mem_region(mem_res
->start
, mem_size
, "isp1760")) {
366 pr_warning("isp1760: Cannot reserve the memory resource\n");
371 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
373 pr_warning("isp1760: IRQ resource not available\n");
378 irqflags
|= irq_res
->flags
& IRQF_TRIGGER_MASK
;
381 if (priv
->is_isp1761
)
382 devflags
|= ISP1760_FLAG_ISP1761
;
383 if (priv
->bus_width_16
)
384 devflags
|= ISP1760_FLAG_BUS_WIDTH_16
;
386 devflags
|= ISP1760_FLAG_OTG_EN
;
388 devflags
|= ISP1760_FLAG_ANALOG_OC
;
389 if (priv
->dack_polarity_high
)
390 devflags
|= ISP1760_FLAG_DACK_POL_HIGH
;
391 if (priv
->dreq_polarity_high
)
392 devflags
|= ISP1760_FLAG_DREQ_POL_HIGH
;
395 hcd
= isp1760_register(mem_res
->start
, mem_size
, irq_res
->start
,
397 &pdev
->dev
, dev_name(&pdev
->dev
), devflags
);
399 platform_set_drvdata(pdev
, hcd
);
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 isp1760_plat_remove(struct platform_device
*pdev
)
418 struct resource
*mem_res
;
419 resource_size_t mem_size
;
420 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
424 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
425 mem_size
= resource_size(mem_res
);
426 release_mem_region(mem_res
->start
, mem_size
);
433 static struct platform_driver isp1760_plat_driver
= {
434 .probe
= isp1760_plat_probe
,
435 .remove
= isp1760_plat_remove
,
441 static int __init
isp1760_init(void)
443 int ret
, any_ret
= -ENODEV
;
447 ret
= platform_driver_register(&isp1760_plat_driver
);
450 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
451 ret
= platform_driver_register(&isp1760_of_driver
);
456 ret
= pci_register_driver(&isp1761_pci_driver
);
465 module_init(isp1760_init
);
467 static void __exit
isp1760_exit(void)
469 platform_driver_unregister(&isp1760_plat_driver
);
470 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
471 platform_driver_unregister(&isp1760_of_driver
);
474 pci_unregister_driver(&isp1761_pci_driver
);
478 module_exit(isp1760_exit
);