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