USB: isp1760: Fix probe in PCI glue code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / host / isp1760-if.c
blob9c4a76a62bf11fa6f1ce26d65610bec2ce3d3098
1 /*
2 * Glue code for the ISP1760 driver and bus
3 * Currently there is support for
4 * - OpenFirmware
5 * - PCI
7 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
9 */
11 #include <linux/usb.h>
12 #include <linux/io.h>
14 #include "../core/hcd.h"
15 #include "isp1760-hcd.h"
17 #ifdef CONFIG_PPC_OF
18 #include <linux/of.h>
19 #include <linux/of_platform.h>
20 #endif
22 #ifdef CONFIG_PCI
23 #include <linux/pci.h>
24 #endif
26 #ifdef CONFIG_PPC_OF
27 static int of_isp1760_probe(struct of_device *dev,
28 const struct of_device_id *match)
30 struct usb_hcd *hcd;
31 struct device_node *dp = dev->node;
32 struct resource *res;
33 struct resource memory;
34 struct of_irq oirq;
35 int virq;
36 u64 res_len;
37 int ret;
38 const unsigned int *prop;
39 unsigned int devflags = 0;
41 ret = of_address_to_resource(dp, 0, &memory);
42 if (ret)
43 return -ENXIO;
45 res = request_mem_region(memory.start, memory.end - memory.start + 1,
46 dev_name(&dev->dev));
47 if (!res)
48 return -EBUSY;
50 res_len = memory.end - memory.start + 1;
52 if (of_irq_map_one(dp, 0, &oirq)) {
53 ret = -ENODEV;
54 goto release_reg;
57 virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
58 oirq.size);
60 if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
61 devflags |= ISP1760_FLAG_ISP1761;
63 if (of_get_property(dp, "port1-disable", NULL) != NULL)
64 devflags |= ISP1760_FLAG_PORT1_DIS;
66 /* Some systems wire up only 16 of the 32 data lines */
67 prop = of_get_property(dp, "bus-width", NULL);
68 if (prop && *prop == 16)
69 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
71 if (of_get_property(dp, "port1-otg", NULL) != NULL)
72 devflags |= ISP1760_FLAG_OTG_EN;
74 if (of_get_property(dp, "analog-oc", NULL) != NULL)
75 devflags |= ISP1760_FLAG_ANALOG_OC;
77 if (of_get_property(dp, "dack-polarity", NULL) != NULL)
78 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
80 if (of_get_property(dp, "dreq-polarity", NULL) != NULL)
81 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
83 hcd = isp1760_register(memory.start, res_len, virq,
84 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
85 devflags);
86 if (IS_ERR(hcd)) {
87 ret = PTR_ERR(hcd);
88 goto release_reg;
91 dev_set_drvdata(&dev->dev, hcd);
92 return ret;
94 release_reg:
95 release_mem_region(memory.start, memory.end - memory.start + 1);
96 return ret;
99 static int of_isp1760_remove(struct of_device *dev)
101 struct usb_hcd *hcd = dev_get_drvdata(&dev->dev);
103 dev_set_drvdata(&dev->dev, NULL);
105 usb_remove_hcd(hcd);
106 iounmap(hcd->regs);
107 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
108 usb_put_hcd(hcd);
109 return 0;
112 static struct of_device_id of_isp1760_match[] = {
114 .compatible = "nxp,usb-isp1760",
117 .compatible = "nxp,usb-isp1761",
119 { },
121 MODULE_DEVICE_TABLE(of, of_isp1760_match);
123 static struct of_platform_driver isp1760_of_driver = {
124 .name = "nxp-isp1760",
125 .match_table = of_isp1760_match,
126 .probe = of_isp1760_probe,
127 .remove = of_isp1760_remove,
129 #endif
131 #ifdef CONFIG_PCI
132 static int __devinit isp1761_pci_probe(struct pci_dev *dev,
133 const struct pci_device_id *id)
135 u8 latency, limit;
136 __u32 reg_data;
137 int retry_count;
138 struct usb_hcd *hcd;
139 unsigned int devflags = 0;
140 int ret_status = 0;
142 resource_size_t pci_mem_phy0;
143 resource_size_t memlength;
145 u8 __iomem *chip_addr;
146 u8 __iomem *iobase;
147 resource_size_t nxp_pci_io_base;
148 resource_size_t iolength;
150 if (usb_disabled())
151 return -ENODEV;
153 if (pci_enable_device(dev) < 0)
154 return -ENODEV;
156 if (!dev->irq)
157 return -ENODEV;
159 /* Grab the PLX PCI mem maped port start address we need */
160 nxp_pci_io_base = pci_resource_start(dev, 0);
161 iolength = pci_resource_len(dev, 0);
163 if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
164 printk(KERN_ERR "request region #1\n");
165 return -EBUSY;
168 iobase = ioremap_nocache(nxp_pci_io_base, iolength);
169 if (!iobase) {
170 printk(KERN_ERR "ioremap #1\n");
171 ret_status = -ENOMEM;
172 goto cleanup1;
174 /* Grab the PLX PCI shared memory of the ISP 1761 we need */
175 pci_mem_phy0 = pci_resource_start(dev, 3);
176 memlength = pci_resource_len(dev, 3);
177 if (memlength < 0xffff) {
178 printk(KERN_ERR "memory length for this resource is wrong\n");
179 ret_status = -ENOMEM;
180 goto cleanup2;
183 if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
184 printk(KERN_ERR "host controller already in use\n");
185 ret_status = -EBUSY;
186 goto cleanup2;
189 /* map available memory */
190 chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
191 if (!chip_addr) {
192 printk(KERN_ERR "Error ioremap failed\n");
193 ret_status = -ENOMEM;
194 goto cleanup3;
197 /* bad pci latencies can contribute to overruns */
198 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
199 if (latency) {
200 pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
201 if (limit && limit < latency)
202 pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
205 /* Try to check whether we can access Scratch Register of
206 * Host Controller or not. The initial PCI access is retried until
207 * local init for the PCI bridge is completed
209 retry_count = 20;
210 reg_data = 0;
211 while ((reg_data != 0xFACE) && retry_count) {
212 /*by default host is in 16bit mode, so
213 * io operations at this stage must be 16 bit
214 * */
215 writel(0xface, chip_addr + HC_SCRATCH_REG);
216 udelay(100);
217 reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
218 retry_count--;
221 iounmap(chip_addr);
223 /* Host Controller presence is detected by writing to scratch register
224 * and reading back and checking the contents are same or not
226 if (reg_data != 0xFACE) {
227 err("scratch register mismatch %x", reg_data);
228 ret_status = -ENOMEM;
229 goto cleanup3;
232 pci_set_master(dev);
234 /* configure PLX PCI chip to pass interrupts */
235 #define PLX_INT_CSR_REG 0x68
236 reg_data = readl(iobase + PLX_INT_CSR_REG);
237 reg_data |= 0x900;
238 writel(reg_data, iobase + PLX_INT_CSR_REG);
240 dev->dev.dma_mask = NULL;
241 hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
242 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
243 devflags);
244 if (IS_ERR(hcd)) {
245 ret_status = -ENODEV;
246 goto cleanup3;
249 /* done with PLX IO access */
250 iounmap(iobase);
251 release_mem_region(nxp_pci_io_base, iolength);
253 pci_set_drvdata(dev, hcd);
254 return 0;
256 cleanup3:
257 release_mem_region(pci_mem_phy0, memlength);
258 cleanup2:
259 iounmap(iobase);
260 cleanup1:
261 release_mem_region(nxp_pci_io_base, iolength);
262 return ret_status;
265 static void isp1761_pci_remove(struct pci_dev *dev)
267 struct usb_hcd *hcd;
269 hcd = pci_get_drvdata(dev);
271 usb_remove_hcd(hcd);
272 iounmap(hcd->regs);
273 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
274 usb_put_hcd(hcd);
276 pci_disable_device(dev);
279 static void isp1761_pci_shutdown(struct pci_dev *dev)
281 printk(KERN_ERR "ips1761_pci_shutdown\n");
284 static const struct pci_device_id isp1760_plx [] = {
286 .class = PCI_CLASS_BRIDGE_OTHER << 8,
287 .class_mask = ~0,
288 .vendor = PCI_VENDOR_ID_PLX,
289 .device = 0x5406,
290 .subvendor = PCI_VENDOR_ID_PLX,
291 .subdevice = 0x9054,
295 MODULE_DEVICE_TABLE(pci, isp1760_plx);
297 static struct pci_driver isp1761_pci_driver = {
298 .name = "isp1760",
299 .id_table = isp1760_plx,
300 .probe = isp1761_pci_probe,
301 .remove = isp1761_pci_remove,
302 .shutdown = isp1761_pci_shutdown,
304 #endif
306 static int __init isp1760_init(void)
308 int ret;
310 init_kmem_once();
312 #ifdef CONFIG_PPC_OF
313 ret = of_register_platform_driver(&isp1760_of_driver);
314 if (ret) {
315 deinit_kmem_cache();
316 return ret;
318 #endif
319 #ifdef CONFIG_PCI
320 ret = pci_register_driver(&isp1761_pci_driver);
321 if (ret)
322 goto unreg_of;
323 #endif
324 return ret;
326 #ifdef CONFIG_PCI
327 unreg_of:
328 #endif
329 #ifdef CONFIG_PPC_OF
330 of_unregister_platform_driver(&isp1760_of_driver);
331 #endif
332 deinit_kmem_cache();
333 return ret;
335 module_init(isp1760_init);
337 static void __exit isp1760_exit(void)
339 #ifdef CONFIG_PPC_OF
340 of_unregister_platform_driver(&isp1760_of_driver);
341 #endif
342 #ifdef CONFIG_PCI
343 pci_unregister_driver(&isp1761_pci_driver);
344 #endif
345 deinit_kmem_cache();
347 module_exit(isp1760_exit);