blob: 8f0259eaa2c73094bbfcfbcbd6905882d40bd0fa [file] [log] [blame]
Sebastian Siewiordb11e472008-04-24 00:37:04 +02001/*
2 * Glue code for the ISP1760 driver and bus
3 * Currently there is support for
4 * - OpenFirmware
5 * - PCI
Michael Hennerich9da69c62009-07-15 23:22:54 -04006 * - PDEV (generic platform device centralized driver model)
Sebastian Siewiordb11e472008-04-24 00:37:04 +02007 *
8 * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
9 *
10 */
11
12#include <linux/usb.h>
13#include <linux/io.h>
Catalin Marinasf7e7aa52009-02-10 16:55:51 +000014#include <linux/platform_device.h>
Michael Hennerich9da69c62009-07-15 23:22:54 -040015#include <linux/usb/isp1760.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020016#include <linux/usb/hcd.h>
Sebastian Siewiordb11e472008-04-24 00:37:04 +020017
Sebastian Siewiordb11e472008-04-24 00:37:04 +020018#include "isp1760-hcd.h"
19
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010020#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +020021#include <linux/of.h>
22#include <linux/of_platform.h>
23#endif
24
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010025#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +020026#include <linux/pci.h>
27#endif
28
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +010029#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +020030static 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;
Tobias Klausere07afd32010-05-05 11:18:41 +020039 resource_size_t res_len;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020040 int ret;
Nate Case3faefc82008-06-17 11:11:38 -050041 const unsigned int *prop;
42 unsigned int devflags = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +020043
44 ret = of_address_to_resource(dp, 0, &memory);
45 if (ret)
46 return -ENXIO;
47
Tobias Klausere07afd32010-05-05 11:18:41 +020048 res_len = resource_size(&memory);
49
50 res = request_mem_region(memory.start, res_len, dev_name(&dev->dev));
Sebastian Siewiordb11e472008-04-24 00:37:04 +020051 if (!res)
52 return -EBUSY;
53
Sebastian Siewiordb11e472008-04-24 00:37:04 +020054 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
Nate Case3faefc82008-06-17 11:11:38 -050062 if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
63 devflags |= ISP1760_FLAG_ISP1761;
64
Nate Case3faefc82008-06-17 11:11:38 -050065 /* 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
Sebastian Siewiordb11e472008-04-24 00:37:04 +020082 hcd = isp1760_register(memory.start, res_len, virq,
Nate Case3faefc82008-06-17 11:11:38 -050083 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
84 devflags);
Sebastian Siewiordb11e472008-04-24 00:37:04 +020085 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
93release_reg:
Tobias Klausere07afd32010-05-05 11:18:41 +020094 release_mem_region(memory.start, res_len);
Sebastian Siewiordb11e472008-04-24 00:37:04 +020095 return ret;
96}
97
98static 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
Németh Mártonc4386ad2010-01-10 15:35:03 +0100111static const struct of_device_id of_isp1760_match[] = {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200112 {
113 .compatible = "nxp,usb-isp1760",
114 },
Nate Case3faefc82008-06-17 11:11:38 -0500115 {
116 .compatible = "nxp,usb-isp1761",
117 },
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200118 { },
119};
120MODULE_DEVICE_TABLE(of, of_isp1760_match);
121
122static 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
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100130#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200131static 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;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200137 struct usb_hcd *hcd;
Nate Case3faefc82008-06-17 11:11:38 -0500138 unsigned int devflags = 0;
Karl Bongers6013bbb2008-12-01 11:47:40 +0100139 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;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200148
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");
Karl Bongers6013bbb2008-12-01 11:47:40 +0100170 ret_status = -ENOMEM;
171 goto cleanup1;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200172 }
173 /* Grab the PLX PCI shared memory of the ISP 1761 we need */
174 pci_mem_phy0 = pci_resource_start(dev, 3);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100175 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;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200180 }
181
Karl Bongers6013bbb2008-12-01 11:47:40 +0100182 if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200183 printk(KERN_ERR "host controller already in use\n");
Karl Bongers6013bbb2008-12-01 11:47:40 +0100184 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;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200194 }
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);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100216 reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200217 retry_count--;
218 }
219
Karl Bongers6013bbb2008-12-01 11:47:40 +0100220 iounmap(chip_addr);
221
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200222 /* 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) {
Greg Kroah-Hartman802f3892008-08-14 09:37:34 -0700226 dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100227 ret_status = -ENOMEM;
228 goto cleanup3;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200229 }
230
231 pci_set_master(dev);
232
Karl Bongers6013bbb2008-12-01 11:47:40 +0100233 /* 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);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200238
239 dev->dev.dma_mask = NULL;
Karl Bongers6013bbb2008-12-01 11:47:40 +0100240 hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
Nate Case3faefc82008-06-17 11:11:38 -0500241 IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
242 devflags);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100243 if (IS_ERR(hcd)) {
244 ret_status = -ENODEV;
245 goto cleanup3;
Julien Brunelce5dee52008-09-24 18:00:36 +0200246 }
Karl Bongers6013bbb2008-12-01 11:47:40 +0100247
248 /* done with PLX IO access */
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200249 iounmap(iobase);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200250 release_mem_region(nxp_pci_io_base, iolength);
Karl Bongers6013bbb2008-12-01 11:47:40 +0100251
252 pci_set_drvdata(dev, hcd);
253 return 0;
254
255cleanup3:
256 release_mem_region(pci_mem_phy0, memlength);
257cleanup2:
258 iounmap(iobase);
259cleanup1:
260 release_mem_region(nxp_pci_io_base, iolength);
261 return ret_status;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200262}
Karl Bongers6013bbb2008-12-01 11:47:40 +0100263
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200264static 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);
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200276}
277
278static void isp1761_pci_shutdown(struct pci_dev *dev)
279{
280 printk(KERN_ERR "ips1761_pci_shutdown\n");
281}
282
Sebastian Andrzej Siewior6c073562008-11-30 16:50:04 +0100283static 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 { }
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200293};
294MODULE_DEVICE_TABLE(pci, isp1760_plx);
295
296static 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
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000305static 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;
Michael Hennerich9da69c62009-07-15 23:22:54 -0400312 struct isp1760_platform_data *priv = pdev->dev.platform_data;
313 unsigned int devflags = 0;
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000314 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
Michael Hennerich9da69c62009-07-15 23:22:54 -0400336 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
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000351 hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
Michael Hennerich9da69c62009-07-15 23:22:54 -0400352 irqflags, &pdev->dev, dev_name(&pdev->dev), devflags);
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000353 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
362cleanup:
363 release_mem_region(mem_res->start, mem_size);
364out:
365 return ret;
366}
367
368static 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
380static struct platform_driver isp1760_plat_driver = {
381 .probe = isp1760_plat_probe,
Mike Frysinger4198e4f2009-06-11 21:59:00 -0400382 .remove = __devexit_p(isp1760_plat_remove),
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000383 .driver = {
384 .name = "isp1760",
385 },
386};
387
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200388static int __init isp1760_init(void)
389{
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000390 int ret, any_ret = -ENODEV;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200391
392 init_kmem_once();
393
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000394 ret = platform_driver_register(&isp1760_plat_driver);
395 if (!ret)
396 any_ret = 0;
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100397#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200398 ret = of_register_platform_driver(&isp1760_of_driver);
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000399 if (!ret)
400 any_ret = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200401#endif
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100402#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200403 ret = pci_register_driver(&isp1761_pci_driver);
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000404 if (!ret)
405 any_ret = 0;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200406#endif
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200407
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000408 if (any_ret)
409 deinit_kmem_cache();
410 return any_ret;
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200411}
412module_init(isp1760_init);
413
414static void __exit isp1760_exit(void)
415{
Catalin Marinasf7e7aa52009-02-10 16:55:51 +0000416 platform_driver_unregister(&isp1760_plat_driver);
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100417#ifdef CONFIG_PPC_OF
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200418 of_unregister_platform_driver(&isp1760_of_driver);
419#endif
Sebastian Andrzej Siewiorff30bf12008-11-02 15:25:42 +0100420#ifdef CONFIG_PCI
Sebastian Siewiordb11e472008-04-24 00:37:04 +0200421 pci_unregister_driver(&isp1761_pci_driver);
422#endif
423 deinit_kmem_cache();
424}
425module_exit(isp1760_exit);