usb: isp1760: Remove isp1760 glue structure
[firefly-linux-kernel-4.4.55.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/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/usb/isp1760.h>
17 #include <linux/usb/hcd.h>
18
19 #include "isp1760-hcd.h"
20
21 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
22 #include <linux/slab.h>
23 #include <linux/of.h>
24 #include <linux/of_platform.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #endif
28
29 #ifdef CONFIG_PCI
30 #include <linux/pci.h>
31 #endif
32
33 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
34 static int of_isp1760_probe(struct platform_device *dev)
35 {
36         struct usb_hcd *hcd;
37         struct device_node *dp = dev->dev.of_node;
38         struct resource *res;
39         struct resource memory;
40         int virq;
41         resource_size_t res_len;
42         int ret;
43         unsigned int devflags = 0;
44         u32 bus_width = 0;
45
46         ret = of_address_to_resource(dp, 0, &memory);
47         if (ret)
48                 return -ENXIO;
49
50         res_len = resource_size(&memory);
51
52         res = request_mem_region(memory.start, res_len, dev_name(&dev->dev));
53         if (!res)
54                 return -EBUSY;
55
56         virq = irq_of_parse_and_map(dp, 0);
57         if (!virq) {
58                 ret = -ENODEV;
59                 goto release_reg;
60         }
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         of_property_read_u32(dp, "bus-width", &bus_width);
67         if (bus_width == 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, IRQF_SHARED,
83                                &dev->dev, dev_name(&dev->dev), devflags);
84         if (IS_ERR(hcd)) {
85                 ret = PTR_ERR(hcd);
86                 goto release_reg;
87         }
88
89         platform_set_drvdata(dev, hcd);
90         return ret;
91
92 release_reg:
93         release_mem_region(memory.start, res_len);
94         return ret;
95 }
96
97 static int of_isp1760_remove(struct platform_device *dev)
98 {
99         struct usb_hcd *hcd = platform_get_drvdata(dev);
100
101         usb_remove_hcd(hcd);
102         iounmap(hcd->regs);
103         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
104         usb_put_hcd(hcd);
105
106         return 0;
107 }
108
109 static const struct of_device_id of_isp1760_match[] = {
110         {
111                 .compatible = "nxp,usb-isp1760",
112         },
113         {
114                 .compatible = "nxp,usb-isp1761",
115         },
116         { },
117 };
118 MODULE_DEVICE_TABLE(of, of_isp1760_match);
119
120 static struct platform_driver isp1760_of_driver = {
121         .driver = {
122                 .name = "nxp-isp1760",
123                 .of_match_table = of_isp1760_match,
124         },
125         .probe          = of_isp1760_probe,
126         .remove         = of_isp1760_remove,
127 };
128 #endif
129
130 #ifdef CONFIG_PCI
131 static int 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, &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 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 = dev_get_platdata(&pdev->dev);
313         unsigned int devflags = 0;
314         unsigned long irqflags = IRQF_SHARED;
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                 ret = -ENODEV;
333                 goto cleanup;
334         }
335
336         irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
337
338         if (priv) {
339                 if (priv->is_isp1761)
340                         devflags |= ISP1760_FLAG_ISP1761;
341                 if (priv->bus_width_16)
342                         devflags |= ISP1760_FLAG_BUS_WIDTH_16;
343                 if (priv->port1_otg)
344                         devflags |= ISP1760_FLAG_OTG_EN;
345                 if (priv->analog_oc)
346                         devflags |= ISP1760_FLAG_ANALOG_OC;
347                 if (priv->dack_polarity_high)
348                         devflags |= ISP1760_FLAG_DACK_POL_HIGH;
349                 if (priv->dreq_polarity_high)
350                         devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
351         }
352
353         hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
354                                irqflags, &pdev->dev, dev_name(&pdev->dev),
355                                devflags);
356
357         platform_set_drvdata(pdev, hcd);
358
359         if (IS_ERR(hcd)) {
360                 pr_warning("isp1760: Failed to register the HCD device\n");
361                 ret = -ENODEV;
362                 goto cleanup;
363         }
364
365         pr_info("ISP1760 USB device initialised\n");
366         return ret;
367
368 cleanup:
369         release_mem_region(mem_res->start, mem_size);
370 out:
371         return ret;
372 }
373
374 static int isp1760_plat_remove(struct platform_device *pdev)
375 {
376         struct resource *mem_res;
377         resource_size_t mem_size;
378         struct usb_hcd *hcd = platform_get_drvdata(pdev);
379
380         usb_remove_hcd(hcd);
381
382         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
383         mem_size = resource_size(mem_res);
384         release_mem_region(mem_res->start, mem_size);
385
386         usb_put_hcd(hcd);
387
388         return 0;
389 }
390
391 static struct platform_driver isp1760_plat_driver = {
392         .probe  = isp1760_plat_probe,
393         .remove = isp1760_plat_remove,
394         .driver = {
395                 .name   = "isp1760",
396         },
397 };
398
399 static int __init isp1760_init(void)
400 {
401         int ret, any_ret = -ENODEV;
402
403         init_kmem_once();
404
405         ret = platform_driver_register(&isp1760_plat_driver);
406         if (!ret)
407                 any_ret = 0;
408 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
409         ret = platform_driver_register(&isp1760_of_driver);
410         if (!ret)
411                 any_ret = 0;
412 #endif
413 #ifdef CONFIG_PCI
414         ret = pci_register_driver(&isp1761_pci_driver);
415         if (!ret)
416                 any_ret = 0;
417 #endif
418
419         if (any_ret)
420                 deinit_kmem_cache();
421         return any_ret;
422 }
423 module_init(isp1760_init);
424
425 static void __exit isp1760_exit(void)
426 {
427         platform_driver_unregister(&isp1760_plat_driver);
428 #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
429         platform_driver_unregister(&isp1760_of_driver);
430 #endif
431 #ifdef CONFIG_PCI
432         pci_unregister_driver(&isp1761_pci_driver);
433 #endif
434         deinit_kmem_cache();
435 }
436 module_exit(isp1760_exit);