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