usb: isp1760: Manage device driver data in common code
[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/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/usb/isp1760.h>
19 #include <linux/usb/hcd.h>
20
21 #include "isp1760-hcd.h"
22
23 #ifdef CONFIG_PCI
24 #include <linux/pci.h>
25 #endif
26
27 #ifdef CONFIG_PCI
28 static int isp1761_pci_probe(struct pci_dev *dev,
29                 const struct pci_device_id *id)
30 {
31         u8 latency, limit;
32         __u32 reg_data;
33         int retry_count;
34         struct usb_hcd *hcd;
35         unsigned int devflags = 0;
36         int ret_status = 0;
37
38         resource_size_t pci_mem_phy0;
39         resource_size_t memlength;
40
41         u8 __iomem *chip_addr;
42         u8 __iomem *iobase;
43         resource_size_t nxp_pci_io_base;
44         resource_size_t iolength;
45
46         if (usb_disabled())
47                 return -ENODEV;
48
49         if (pci_enable_device(dev) < 0)
50                 return -ENODEV;
51
52         if (!dev->irq)
53                 return -ENODEV;
54
55         /* Grab the PLX PCI mem maped port start address we need  */
56         nxp_pci_io_base = pci_resource_start(dev, 0);
57         iolength = pci_resource_len(dev, 0);
58
59         if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
60                 printk(KERN_ERR "request region #1\n");
61                 return -EBUSY;
62         }
63
64         iobase = ioremap_nocache(nxp_pci_io_base, iolength);
65         if (!iobase) {
66                 printk(KERN_ERR "ioremap #1\n");
67                 ret_status = -ENOMEM;
68                 goto cleanup1;
69         }
70         /* Grab the PLX PCI shared memory of the ISP 1761 we need  */
71         pci_mem_phy0 = pci_resource_start(dev, 3);
72         memlength = pci_resource_len(dev, 3);
73         if (memlength < 0xffff) {
74                 printk(KERN_ERR "memory length for this resource is wrong\n");
75                 ret_status = -ENOMEM;
76                 goto cleanup2;
77         }
78
79         if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
80                 printk(KERN_ERR "host controller already in use\n");
81                 ret_status = -EBUSY;
82                 goto cleanup2;
83         }
84
85         /* map available memory */
86         chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
87         if (!chip_addr) {
88                 printk(KERN_ERR "Error ioremap failed\n");
89                 ret_status = -ENOMEM;
90                 goto cleanup3;
91         }
92
93         /* bad pci latencies can contribute to overruns */
94         pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
95         if (latency) {
96                 pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
97                 if (limit && limit < latency)
98                         pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
99         }
100
101         /* Try to check whether we can access Scratch Register of
102          * Host Controller or not. The initial PCI access is retried until
103          * local init for the PCI bridge is completed
104          */
105         retry_count = 20;
106         reg_data = 0;
107         while ((reg_data != 0xFACE) && retry_count) {
108                 /*by default host is in 16bit mode, so
109                  * io operations at this stage must be 16 bit
110                  * */
111                 writel(0xface, chip_addr + HC_SCRATCH_REG);
112                 udelay(100);
113                 reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
114                 retry_count--;
115         }
116
117         iounmap(chip_addr);
118
119         /* Host Controller presence is detected by writing to scratch register
120          * and reading back and checking the contents are same or not
121          */
122         if (reg_data != 0xFACE) {
123                 dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
124                 ret_status = -ENOMEM;
125                 goto cleanup3;
126         }
127
128         pci_set_master(dev);
129
130         /* configure PLX PCI chip to pass interrupts */
131 #define PLX_INT_CSR_REG 0x68
132         reg_data = readl(iobase + PLX_INT_CSR_REG);
133         reg_data |= 0x900;
134         writel(reg_data, iobase + PLX_INT_CSR_REG);
135
136         dev->dev.dma_mask = NULL;
137         hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
138                                IRQF_SHARED, &dev->dev, dev_name(&dev->dev),
139                                devflags);
140         if (IS_ERR(hcd)) {
141                 ret_status = -ENODEV;
142                 goto cleanup3;
143         }
144
145         /* done with PLX IO access */
146         iounmap(iobase);
147         release_mem_region(nxp_pci_io_base, iolength);
148
149         return 0;
150
151 cleanup3:
152         release_mem_region(pci_mem_phy0, memlength);
153 cleanup2:
154         iounmap(iobase);
155 cleanup1:
156         release_mem_region(nxp_pci_io_base, iolength);
157         return ret_status;
158 }
159
160 static void isp1761_pci_remove(struct pci_dev *dev)
161 {
162         isp1760_unregister(&dev->dev);
163
164         pci_disable_device(dev);
165 }
166
167 static void isp1761_pci_shutdown(struct pci_dev *dev)
168 {
169         printk(KERN_ERR "ips1761_pci_shutdown\n");
170 }
171
172 static const struct pci_device_id isp1760_plx [] = {
173         {
174                 .class          = PCI_CLASS_BRIDGE_OTHER << 8,
175                 .class_mask     = ~0,
176                 .vendor         = PCI_VENDOR_ID_PLX,
177                 .device         = 0x5406,
178                 .subvendor      = PCI_VENDOR_ID_PLX,
179                 .subdevice      = 0x9054,
180         },
181         { }
182 };
183 MODULE_DEVICE_TABLE(pci, isp1760_plx);
184
185 static struct pci_driver isp1761_pci_driver = {
186         .name =         "isp1760",
187         .id_table =     isp1760_plx,
188         .probe =        isp1761_pci_probe,
189         .remove =       isp1761_pci_remove,
190         .shutdown =     isp1761_pci_shutdown,
191 };
192 #endif
193
194 static int isp1760_plat_probe(struct platform_device *pdev)
195 {
196         unsigned long irqflags = IRQF_SHARED;
197         unsigned int devflags = 0;
198         struct resource *mem_res;
199         struct resource *irq_res;
200         resource_size_t mem_size;
201         struct usb_hcd *hcd;
202         int ret;
203
204         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
205         if (!mem_res) {
206                 pr_warning("isp1760: Memory resource not available\n");
207                 return -ENODEV;
208         }
209         mem_size = resource_size(mem_res);
210         if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
211                 pr_warning("isp1760: Cannot reserve the memory resource\n");
212                 return -EBUSY;
213         }
214
215         irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
216         if (!irq_res) {
217                 pr_warning("isp1760: IRQ resource not available\n");
218                 ret = -ENODEV;
219                 goto cleanup;
220         }
221
222         irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
223
224         if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
225                 struct device_node *dp = pdev->dev.of_node;
226                 u32 bus_width = 0;
227
228                 if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
229                         devflags |= ISP1760_FLAG_ISP1761;
230
231                 /* Some systems wire up only 16 of the 32 data lines */
232                 of_property_read_u32(dp, "bus-width", &bus_width);
233                 if (bus_width == 16)
234                         devflags |= ISP1760_FLAG_BUS_WIDTH_16;
235
236                 if (of_property_read_bool(dp, "port1-otg"))
237                         devflags |= ISP1760_FLAG_OTG_EN;
238
239                 if (of_property_read_bool(dp, "analog-oc"))
240                         devflags |= ISP1760_FLAG_ANALOG_OC;
241
242                 if (of_property_read_bool(dp, "dack-polarity"))
243                         devflags |= ISP1760_FLAG_DACK_POL_HIGH;
244
245                 if (of_property_read_bool(dp, "dreq-polarity"))
246                         devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
247         } else if (dev_get_platdata(&pdev->dev)) {
248                 struct isp1760_platform_data *pdata =
249                         dev_get_platdata(&pdev->dev);
250
251                 if (pdata->is_isp1761)
252                         devflags |= ISP1760_FLAG_ISP1761;
253                 if (pdata->bus_width_16)
254                         devflags |= ISP1760_FLAG_BUS_WIDTH_16;
255                 if (pdata->port1_otg)
256                         devflags |= ISP1760_FLAG_OTG_EN;
257                 if (pdata->analog_oc)
258                         devflags |= ISP1760_FLAG_ANALOG_OC;
259                 if (pdata->dack_polarity_high)
260                         devflags |= ISP1760_FLAG_DACK_POL_HIGH;
261                 if (pdata->dreq_polarity_high)
262                         devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
263         }
264
265         hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
266                                irqflags, &pdev->dev, dev_name(&pdev->dev),
267                                devflags);
268         if (IS_ERR(hcd)) {
269                 pr_warning("isp1760: Failed to register the HCD device\n");
270                 ret = PTR_ERR(hcd);
271                 goto cleanup;
272         }
273
274         pr_info("ISP1760 USB device initialised\n");
275         return 0;
276
277 cleanup:
278         release_mem_region(mem_res->start, mem_size);
279         return ret;
280 }
281
282 static int isp1760_plat_remove(struct platform_device *pdev)
283 {
284         isp1760_unregister(&pdev->dev);
285
286         return 0;
287 }
288
289 #ifdef CONFIG_OF
290 static const struct of_device_id isp1760_of_match[] = {
291         { .compatible = "nxp,usb-isp1760", },
292         { .compatible = "nxp,usb-isp1761", },
293         { },
294 };
295 MODULE_DEVICE_TABLE(of, isp1760_of_match);
296 #endif
297
298 static struct platform_driver isp1760_plat_driver = {
299         .probe  = isp1760_plat_probe,
300         .remove = isp1760_plat_remove,
301         .driver = {
302                 .name   = "isp1760",
303                 .of_match_table = of_match_ptr(isp1760_of_match),
304         },
305 };
306
307 static int __init isp1760_init(void)
308 {
309         int ret, any_ret = -ENODEV;
310
311         init_kmem_once();
312
313         ret = platform_driver_register(&isp1760_plat_driver);
314         if (!ret)
315                 any_ret = 0;
316 #ifdef CONFIG_PCI
317         ret = pci_register_driver(&isp1761_pci_driver);
318         if (!ret)
319                 any_ret = 0;
320 #endif
321
322         if (any_ret)
323                 deinit_kmem_cache();
324         return any_ret;
325 }
326 module_init(isp1760_init);
327
328 static void __exit isp1760_exit(void)
329 {
330         platform_driver_unregister(&isp1760_plat_driver);
331 #ifdef CONFIG_PCI
332         pci_unregister_driver(&isp1761_pci_driver);
333 #endif
334         deinit_kmem_cache();
335 }
336 module_exit(isp1760_exit);