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