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