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