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/platform_device.h>
15 #include <linux/usb/isp1760.h>
16 #include <linux/usb/hcd.h>
18 #include "isp1760-hcd.h"
21 #include <linux/slab.h>
23 #include <linux/of_platform.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_gpio.h>
30 #include <linux/pci.h>
39 static int of_isp1760_probe(struct platform_device
*dev
)
41 struct isp1760
*drvdata
;
42 struct device_node
*dp
= dev
->dev
.of_node
;
44 struct resource memory
;
47 resource_size_t res_len
;
49 const unsigned int *prop
;
50 unsigned int devflags
= 0;
51 enum of_gpio_flags gpio_flags
;
53 drvdata
= kzalloc(sizeof(*drvdata
), GFP_KERNEL
);
57 ret
= of_address_to_resource(dp
, 0, &memory
);
61 res_len
= resource_size(&memory
);
63 res
= request_mem_region(memory
.start
, res_len
, dev_name(&dev
->dev
));
67 if (of_irq_map_one(dp
, 0, &oirq
)) {
72 virq
= irq_create_of_mapping(oirq
.controller
, oirq
.specifier
,
75 if (of_device_is_compatible(dp
, "nxp,usb-isp1761"))
76 devflags
|= ISP1760_FLAG_ISP1761
;
78 /* Some systems wire up only 16 of the 32 data lines */
79 prop
= of_get_property(dp
, "bus-width", NULL
);
80 if (prop
&& *prop
== 16)
81 devflags
|= ISP1760_FLAG_BUS_WIDTH_16
;
83 if (of_get_property(dp
, "port1-otg", NULL
) != NULL
)
84 devflags
|= ISP1760_FLAG_OTG_EN
;
86 if (of_get_property(dp
, "analog-oc", NULL
) != NULL
)
87 devflags
|= ISP1760_FLAG_ANALOG_OC
;
89 if (of_get_property(dp
, "dack-polarity", NULL
) != NULL
)
90 devflags
|= ISP1760_FLAG_DACK_POL_HIGH
;
92 if (of_get_property(dp
, "dreq-polarity", NULL
) != NULL
)
93 devflags
|= ISP1760_FLAG_DREQ_POL_HIGH
;
95 drvdata
->rst_gpio
= of_get_gpio_flags(dp
, 0, &gpio_flags
);
96 if (gpio_is_valid(drvdata
->rst_gpio
)) {
97 ret
= gpio_request(drvdata
->rst_gpio
, dev_name(&dev
->dev
));
99 if (!(gpio_flags
& OF_GPIO_ACTIVE_LOW
)) {
100 devflags
|= ISP1760_FLAG_RESET_ACTIVE_HIGH
;
101 gpio_direction_output(drvdata
->rst_gpio
, 0);
103 gpio_direction_output(drvdata
->rst_gpio
, 1);
106 drvdata
->rst_gpio
= ret
;
110 drvdata
->hcd
= isp1760_register(memory
.start
, res_len
, virq
,
111 IRQF_SHARED
, drvdata
->rst_gpio
,
112 &dev
->dev
, dev_name(&dev
->dev
),
114 if (IS_ERR(drvdata
->hcd
)) {
115 ret
= PTR_ERR(drvdata
->hcd
);
119 dev_set_drvdata(&dev
->dev
, drvdata
);
123 if (gpio_is_valid(drvdata
->rst_gpio
))
124 gpio_free(drvdata
->rst_gpio
);
126 release_mem_region(memory
.start
, res_len
);
131 static int of_isp1760_remove(struct platform_device
*dev
)
133 struct isp1760
*drvdata
= dev_get_drvdata(&dev
->dev
);
135 dev_set_drvdata(&dev
->dev
, NULL
);
137 usb_remove_hcd(drvdata
->hcd
);
138 iounmap(drvdata
->hcd
->regs
);
139 release_mem_region(drvdata
->hcd
->rsrc_start
, drvdata
->hcd
->rsrc_len
);
140 usb_put_hcd(drvdata
->hcd
);
142 if (gpio_is_valid(drvdata
->rst_gpio
))
143 gpio_free(drvdata
->rst_gpio
);
149 static const struct of_device_id of_isp1760_match
[] = {
151 .compatible
= "nxp,usb-isp1760",
154 .compatible
= "nxp,usb-isp1761",
158 MODULE_DEVICE_TABLE(of
, of_isp1760_match
);
160 static struct platform_driver isp1760_of_driver
= {
162 .name
= "nxp-isp1760",
163 .owner
= THIS_MODULE
,
164 .of_match_table
= of_isp1760_match
,
166 .probe
= of_isp1760_probe
,
167 .remove
= of_isp1760_remove
,
172 static int __devinit
isp1761_pci_probe(struct pci_dev
*dev
,
173 const struct pci_device_id
*id
)
179 unsigned int devflags
= 0;
182 resource_size_t pci_mem_phy0
;
183 resource_size_t memlength
;
185 u8 __iomem
*chip_addr
;
187 resource_size_t nxp_pci_io_base
;
188 resource_size_t iolength
;
193 if (pci_enable_device(dev
) < 0)
199 /* Grab the PLX PCI mem maped port start address we need */
200 nxp_pci_io_base
= pci_resource_start(dev
, 0);
201 iolength
= pci_resource_len(dev
, 0);
203 if (!request_mem_region(nxp_pci_io_base
, iolength
, "ISP1761 IO MEM")) {
204 printk(KERN_ERR
"request region #1\n");
208 iobase
= ioremap_nocache(nxp_pci_io_base
, iolength
);
210 printk(KERN_ERR
"ioremap #1\n");
211 ret_status
= -ENOMEM
;
214 /* Grab the PLX PCI shared memory of the ISP 1761 we need */
215 pci_mem_phy0
= pci_resource_start(dev
, 3);
216 memlength
= pci_resource_len(dev
, 3);
217 if (memlength
< 0xffff) {
218 printk(KERN_ERR
"memory length for this resource is wrong\n");
219 ret_status
= -ENOMEM
;
223 if (!request_mem_region(pci_mem_phy0
, memlength
, "ISP-PCI")) {
224 printk(KERN_ERR
"host controller already in use\n");
229 /* map available memory */
230 chip_addr
= ioremap_nocache(pci_mem_phy0
,memlength
);
232 printk(KERN_ERR
"Error ioremap failed\n");
233 ret_status
= -ENOMEM
;
237 /* bad pci latencies can contribute to overruns */
238 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &latency
);
240 pci_read_config_byte(dev
, PCI_MAX_LAT
, &limit
);
241 if (limit
&& limit
< latency
)
242 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, limit
);
245 /* Try to check whether we can access Scratch Register of
246 * Host Controller or not. The initial PCI access is retried until
247 * local init for the PCI bridge is completed
251 while ((reg_data
!= 0xFACE) && retry_count
) {
252 /*by default host is in 16bit mode, so
253 * io operations at this stage must be 16 bit
255 writel(0xface, chip_addr
+ HC_SCRATCH_REG
);
257 reg_data
= readl(chip_addr
+ HC_SCRATCH_REG
) & 0x0000ffff;
263 /* Host Controller presence is detected by writing to scratch register
264 * and reading back and checking the contents are same or not
266 if (reg_data
!= 0xFACE) {
267 dev_err(&dev
->dev
, "scratch register mismatch %x\n", reg_data
);
268 ret_status
= -ENOMEM
;
274 /* configure PLX PCI chip to pass interrupts */
275 #define PLX_INT_CSR_REG 0x68
276 reg_data
= readl(iobase
+ PLX_INT_CSR_REG
);
278 writel(reg_data
, iobase
+ PLX_INT_CSR_REG
);
280 dev
->dev
.dma_mask
= NULL
;
281 hcd
= isp1760_register(pci_mem_phy0
, memlength
, dev
->irq
,
282 IRQF_SHARED
, -ENOENT
, &dev
->dev
, dev_name(&dev
->dev
),
285 ret_status
= -ENODEV
;
289 /* done with PLX IO access */
291 release_mem_region(nxp_pci_io_base
, iolength
);
293 pci_set_drvdata(dev
, hcd
);
297 release_mem_region(pci_mem_phy0
, memlength
);
301 release_mem_region(nxp_pci_io_base
, iolength
);
305 static void isp1761_pci_remove(struct pci_dev
*dev
)
309 hcd
= pci_get_drvdata(dev
);
313 release_mem_region(hcd
->rsrc_start
, hcd
->rsrc_len
);
316 pci_disable_device(dev
);
319 static void isp1761_pci_shutdown(struct pci_dev
*dev
)
321 printk(KERN_ERR
"ips1761_pci_shutdown\n");
324 static const struct pci_device_id isp1760_plx
[] = {
326 .class = PCI_CLASS_BRIDGE_OTHER
<< 8,
328 .vendor
= PCI_VENDOR_ID_PLX
,
330 .subvendor
= PCI_VENDOR_ID_PLX
,
335 MODULE_DEVICE_TABLE(pci
, isp1760_plx
);
337 static struct pci_driver isp1761_pci_driver
= {
339 .id_table
= isp1760_plx
,
340 .probe
= isp1761_pci_probe
,
341 .remove
= isp1761_pci_remove
,
342 .shutdown
= isp1761_pci_shutdown
,
346 static int __devinit
isp1760_plat_probe(struct platform_device
*pdev
)
350 struct resource
*mem_res
;
351 struct resource
*irq_res
;
352 resource_size_t mem_size
;
353 struct isp1760_platform_data
*priv
= pdev
->dev
.platform_data
;
354 unsigned int devflags
= 0;
355 unsigned long irqflags
= IRQF_SHARED
;
357 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
359 pr_warning("isp1760: Memory resource not available\n");
363 mem_size
= resource_size(mem_res
);
364 if (!request_mem_region(mem_res
->start
, mem_size
, "isp1760")) {
365 pr_warning("isp1760: Cannot reserve the memory resource\n");
370 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
372 pr_warning("isp1760: IRQ resource not available\n");
375 irqflags
|= irq_res
->flags
& IRQF_TRIGGER_MASK
;
378 if (priv
->is_isp1761
)
379 devflags
|= ISP1760_FLAG_ISP1761
;
380 if (priv
->bus_width_16
)
381 devflags
|= ISP1760_FLAG_BUS_WIDTH_16
;
383 devflags
|= ISP1760_FLAG_OTG_EN
;
385 devflags
|= ISP1760_FLAG_ANALOG_OC
;
386 if (priv
->dack_polarity_high
)
387 devflags
|= ISP1760_FLAG_DACK_POL_HIGH
;
388 if (priv
->dreq_polarity_high
)
389 devflags
|= ISP1760_FLAG_DREQ_POL_HIGH
;
392 hcd
= isp1760_register(mem_res
->start
, mem_size
, irq_res
->start
,
394 &pdev
->dev
, dev_name(&pdev
->dev
), devflags
);
396 pr_warning("isp1760: Failed to register the HCD device\n");
401 pr_info("ISP1760 USB device initialised\n");
405 release_mem_region(mem_res
->start
, mem_size
);
410 static int __devexit
isp1760_plat_remove(struct platform_device
*pdev
)
412 struct resource
*mem_res
;
413 resource_size_t mem_size
;
415 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
416 mem_size
= resource_size(mem_res
);
417 release_mem_region(mem_res
->start
, mem_size
);
422 static struct platform_driver isp1760_plat_driver
= {
423 .probe
= isp1760_plat_probe
,
424 .remove
= __devexit_p(isp1760_plat_remove
),
430 static int __init
isp1760_init(void)
432 int ret
, any_ret
= -ENODEV
;
436 ret
= platform_driver_register(&isp1760_plat_driver
);
440 ret
= platform_driver_register(&isp1760_of_driver
);
445 ret
= pci_register_driver(&isp1761_pci_driver
);
454 module_init(isp1760_init
);
456 static void __exit
isp1760_exit(void)
458 platform_driver_unregister(&isp1760_plat_driver
);
460 platform_driver_unregister(&isp1760_of_driver
);
463 pci_unregister_driver(&isp1761_pci_driver
);
467 module_exit(isp1760_exit
);