Merge branch 'linux-next' of git://git.infradead.org/ubi-2.6
[pandora-kernel.git] / drivers / usb / host / isp1760-if.c
1 /*
2  * Glue code for the ISP1760 driver and bus
3  * Currently there is support for
4  * - OpenFirmware
5  * - PCI
6  * - PDEV (generic platform device centralized driver model)
7  *
8  * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
9  *
10  */
11
12 #include <linux/usb.h>
13 #include <linux/io.h>
14 #include <linux/platform_device.h>
15 #include <linux/usb/isp1760.h>
16 #include <linux/usb/hcd.h>
17
18 #include "isp1760-hcd.h"
19
20 #ifdef CONFIG_PPC_OF
21 #include <linux/of.h>
22 #include <linux/of_platform.h>
23 #endif
24
25 #ifdef CONFIG_PCI
26 #include <linux/pci.h>
27 #endif
28
29 #ifdef CONFIG_PPC_OF
30 static int of_isp1760_probe(struct of_device *dev,
31                 const struct of_device_id *match)
32 {
33         struct usb_hcd *hcd;
34         struct device_node *dp = dev->node;
35         struct resource *res;
36         struct resource memory;
37         struct of_irq oirq;
38         int virq;
39         resource_size_t res_len;
40         int ret;
41         const unsigned int *prop;
42         unsigned int devflags = 0;
43
44         ret = of_address_to_resource(dp, 0, &memory);
45         if (ret)
46                 return -ENXIO;
47
48         res_len = resource_size(&memory);
49
50         res = request_mem_region(memory.start, res_len, dev_name(&dev->dev));
51         if (!res)
52                 return -EBUSY;
53
54         if (of_irq_map_one(dp, 0, &oirq)) {
55                 ret = -ENODEV;
56                 goto release_reg;
57         }
58
59         virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
60                         oirq.size);
61
62         if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
63                 devflags |= ISP1760_FLAG_ISP1761;
64
65         /* Some systems wire up only 16 of the 32 data lines */
66         prop = of_get_property(dp, "bus-width", NULL);
67         if (prop && *prop == 16)
68                 devflags |= ISP1760_FLAG_BUS_WIDTH_16;
69
70         if (of_get_property(dp, "port1-otg", NULL) != NULL)
71                 devflags |= ISP1760_FLAG_OTG_EN;
72
73         if (of_get_property(dp, "analog-oc", NULL) != NULL)
74                 devflags |= ISP1760_FLAG_ANALOG_OC;
75
76         if (of_get_property(dp, "dack-polarity", NULL) != NULL)
77                 devflags |= ISP1760_FLAG_DACK_POL_HIGH;
78
79         if (of_get_property(dp, "dreq-polarity", NULL) != NULL)
80                 devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
81
82         hcd = isp1760_register(memory.start, res_len, virq,
83                 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
84                 devflags);
85         if (IS_ERR(hcd)) {
86                 ret = PTR_ERR(hcd);
87                 goto release_reg;
88         }
89
90         dev_set_drvdata(&dev->dev, hcd);
91         return ret;
92
93 release_reg:
94         release_mem_region(memory.start, res_len);
95         return ret;
96 }
97
98 static int of_isp1760_remove(struct of_device *dev)
99 {
100         struct usb_hcd *hcd = dev_get_drvdata(&dev->dev);
101
102         dev_set_drvdata(&dev->dev, NULL);
103
104         usb_remove_hcd(hcd);
105         iounmap(hcd->regs);
106         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
107         usb_put_hcd(hcd);
108         return 0;
109 }
110
111 static const struct of_device_id of_isp1760_match[] = {
112         {
113                 .compatible = "nxp,usb-isp1760",
114         },
115         {
116                 .compatible = "nxp,usb-isp1761",
117         },
118         { },
119 };
120 MODULE_DEVICE_TABLE(of, of_isp1760_match);
121
122 static struct of_platform_driver isp1760_of_driver = {
123         .name           = "nxp-isp1760",
124         .match_table    = of_isp1760_match,
125         .probe          = of_isp1760_probe,
126         .remove         = of_isp1760_remove,
127 };
128 #endif
129
130 #ifdef CONFIG_PCI
131 static int __devinit isp1761_pci_probe(struct pci_dev *dev,
132                 const struct pci_device_id *id)
133 {
134         u8 latency, limit;
135         __u32 reg_data;
136         int retry_count;
137         struct usb_hcd *hcd;
138         unsigned int devflags = 0;
139         int ret_status = 0;
140
141         resource_size_t pci_mem_phy0;
142         resource_size_t memlength;
143
144         u8 __iomem *chip_addr;
145         u8 __iomem *iobase;
146         resource_size_t nxp_pci_io_base;
147         resource_size_t iolength;
148
149         if (usb_disabled())
150                 return -ENODEV;
151
152         if (pci_enable_device(dev) < 0)
153                 return -ENODEV;
154
155         if (!dev->irq)
156                 return -ENODEV;
157
158         /* Grab the PLX PCI mem maped port start address we need  */
159         nxp_pci_io_base = pci_resource_start(dev, 0);
160         iolength = pci_resource_len(dev, 0);
161
162         if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
163                 printk(KERN_ERR "request region #1\n");
164                 return -EBUSY;
165         }
166
167         iobase = ioremap_nocache(nxp_pci_io_base, iolength);
168         if (!iobase) {
169                 printk(KERN_ERR "ioremap #1\n");
170                 ret_status = -ENOMEM;
171                 goto cleanup1;
172         }
173         /* Grab the PLX PCI shared memory of the ISP 1761 we need  */
174         pci_mem_phy0 = pci_resource_start(dev, 3);
175         memlength = pci_resource_len(dev, 3);
176         if (memlength < 0xffff) {
177                 printk(KERN_ERR "memory length for this resource is wrong\n");
178                 ret_status = -ENOMEM;
179                 goto cleanup2;
180         }
181
182         if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
183                 printk(KERN_ERR "host controller already in use\n");
184                 ret_status = -EBUSY;
185                 goto cleanup2;
186         }
187
188         /* map available memory */
189         chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
190         if (!chip_addr) {
191                 printk(KERN_ERR "Error ioremap failed\n");
192                 ret_status = -ENOMEM;
193                 goto cleanup3;
194         }
195
196         /* bad pci latencies can contribute to overruns */
197         pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
198         if (latency) {
199                 pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
200                 if (limit && limit < latency)
201                         pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
202         }
203
204         /* Try to check whether we can access Scratch Register of
205          * Host Controller or not. The initial PCI access is retried until
206          * local init for the PCI bridge is completed
207          */
208         retry_count = 20;
209         reg_data = 0;
210         while ((reg_data != 0xFACE) && retry_count) {
211                 /*by default host is in 16bit mode, so
212                  * io operations at this stage must be 16 bit
213                  * */
214                 writel(0xface, chip_addr + HC_SCRATCH_REG);
215                 udelay(100);
216                 reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
217                 retry_count--;
218         }
219
220         iounmap(chip_addr);
221
222         /* Host Controller presence is detected by writing to scratch register
223          * and reading back and checking the contents are same or not
224          */
225         if (reg_data != 0xFACE) {
226                 dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
227                 ret_status = -ENOMEM;
228                 goto cleanup3;
229         }
230
231         pci_set_master(dev);
232
233         /* configure PLX PCI chip to pass interrupts */
234 #define PLX_INT_CSR_REG 0x68
235         reg_data = readl(iobase + PLX_INT_CSR_REG);
236         reg_data |= 0x900;
237         writel(reg_data, iobase + PLX_INT_CSR_REG);
238
239         dev->dev.dma_mask = NULL;
240         hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
241                 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
242                 devflags);
243         if (IS_ERR(hcd)) {
244                 ret_status = -ENODEV;
245                 goto cleanup3;
246         }
247
248         /* done with PLX IO access */
249         iounmap(iobase);
250         release_mem_region(nxp_pci_io_base, iolength);
251
252         pci_set_drvdata(dev, hcd);
253         return 0;
254
255 cleanup3:
256         release_mem_region(pci_mem_phy0, memlength);
257 cleanup2:
258         iounmap(iobase);
259 cleanup1:
260         release_mem_region(nxp_pci_io_base, iolength);
261         return ret_status;
262 }
263
264 static void isp1761_pci_remove(struct pci_dev *dev)
265 {
266         struct usb_hcd *hcd;
267
268         hcd = pci_get_drvdata(dev);
269
270         usb_remove_hcd(hcd);
271         iounmap(hcd->regs);
272         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
273         usb_put_hcd(hcd);
274
275         pci_disable_device(dev);
276 }
277
278 static void isp1761_pci_shutdown(struct pci_dev *dev)
279 {
280         printk(KERN_ERR "ips1761_pci_shutdown\n");
281 }
282
283 static const struct pci_device_id isp1760_plx [] = {
284         {
285                 .class          = PCI_CLASS_BRIDGE_OTHER << 8,
286                 .class_mask     = ~0,
287                 .vendor         = PCI_VENDOR_ID_PLX,
288                 .device         = 0x5406,
289                 .subvendor      = PCI_VENDOR_ID_PLX,
290                 .subdevice      = 0x9054,
291         },
292         { }
293 };
294 MODULE_DEVICE_TABLE(pci, isp1760_plx);
295
296 static struct pci_driver isp1761_pci_driver = {
297         .name =         "isp1760",
298         .id_table =     isp1760_plx,
299         .probe =        isp1761_pci_probe,
300         .remove =       isp1761_pci_remove,
301         .shutdown =     isp1761_pci_shutdown,
302 };
303 #endif
304
305 static int __devinit isp1760_plat_probe(struct platform_device *pdev)
306 {
307         int ret = 0;
308         struct usb_hcd *hcd;
309         struct resource *mem_res;
310         struct resource *irq_res;
311         resource_size_t mem_size;
312         struct isp1760_platform_data *priv = pdev->dev.platform_data;
313         unsigned int devflags = 0;
314         unsigned long irqflags = IRQF_SHARED | IRQF_DISABLED;
315
316         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
317         if (!mem_res) {
318                 pr_warning("isp1760: Memory resource not available\n");
319                 ret = -ENODEV;
320                 goto out;
321         }
322         mem_size = resource_size(mem_res);
323         if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
324                 pr_warning("isp1760: Cannot reserve the memory resource\n");
325                 ret = -EBUSY;
326                 goto out;
327         }
328
329         irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
330         if (!irq_res) {
331                 pr_warning("isp1760: IRQ resource not available\n");
332                 return -ENODEV;
333         }
334         irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
335
336         if (priv) {
337                 if (priv->is_isp1761)
338                         devflags |= ISP1760_FLAG_ISP1761;
339                 if (priv->bus_width_16)
340                         devflags |= ISP1760_FLAG_BUS_WIDTH_16;
341                 if (priv->port1_otg)
342                         devflags |= ISP1760_FLAG_OTG_EN;
343                 if (priv->analog_oc)
344                         devflags |= ISP1760_FLAG_ANALOG_OC;
345                 if (priv->dack_polarity_high)
346                         devflags |= ISP1760_FLAG_DACK_POL_HIGH;
347                 if (priv->dreq_polarity_high)
348                         devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
349         }
350
351         hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
352                                irqflags, &pdev->dev, dev_name(&pdev->dev), devflags);
353         if (IS_ERR(hcd)) {
354                 pr_warning("isp1760: Failed to register the HCD device\n");
355                 ret = -ENODEV;
356                 goto cleanup;
357         }
358
359         pr_info("ISP1760 USB device initialised\n");
360         return ret;
361
362 cleanup:
363         release_mem_region(mem_res->start, mem_size);
364 out:
365         return ret;
366 }
367
368 static int __devexit isp1760_plat_remove(struct platform_device *pdev)
369 {
370         struct resource *mem_res;
371         resource_size_t mem_size;
372
373         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
374         mem_size = resource_size(mem_res);
375         release_mem_region(mem_res->start, mem_size);
376
377         return 0;
378 }
379
380 static struct platform_driver isp1760_plat_driver = {
381         .probe  = isp1760_plat_probe,
382         .remove = __devexit_p(isp1760_plat_remove),
383         .driver = {
384                 .name   = "isp1760",
385         },
386 };
387
388 static int __init isp1760_init(void)
389 {
390         int ret, any_ret = -ENODEV;
391
392         init_kmem_once();
393
394         ret = platform_driver_register(&isp1760_plat_driver);
395         if (!ret)
396                 any_ret = 0;
397 #ifdef CONFIG_PPC_OF
398         ret = of_register_platform_driver(&isp1760_of_driver);
399         if (!ret)
400                 any_ret = 0;
401 #endif
402 #ifdef CONFIG_PCI
403         ret = pci_register_driver(&isp1761_pci_driver);
404         if (!ret)
405                 any_ret = 0;
406 #endif
407
408         if (any_ret)
409                 deinit_kmem_cache();
410         return any_ret;
411 }
412 module_init(isp1760_init);
413
414 static void __exit isp1760_exit(void)
415 {
416         platform_driver_unregister(&isp1760_plat_driver);
417 #ifdef CONFIG_PPC_OF
418         of_unregister_platform_driver(&isp1760_of_driver);
419 #endif
420 #ifdef CONFIG_PCI
421         pci_unregister_driver(&isp1761_pci_driver);
422 #endif
423         deinit_kmem_cache();
424 }
425 module_exit(isp1760_exit);