Commit | Line | Data |
---|---|---|
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> | |
f7e7aa58 | 14 | #include <linux/platform_device.h> |
9da69c60 | 15 | #include <linux/usb/isp1760.h> |
27729aad | 16 | #include <linux/usb/hcd.h> |
db11e47d | 17 | |
db11e47d SS |
18 | #include "isp1760-hcd.h" |
19 | ||
ff30bf1c | 20 | #ifdef CONFIG_PPC_OF |
db11e47d SS |
21 | #include <linux/of.h> |
22 | #include <linux/of_platform.h> | |
23 | #endif | |
24 | ||
ff30bf1c | 25 | #ifdef CONFIG_PCI |
db11e47d SS |
26 | #include <linux/pci.h> |
27 | #endif | |
28 | ||
ff30bf1c | 29 | #ifdef CONFIG_PPC_OF |
db11e47d SS |
30 | static int of_isp1760_probe(struct of_device *dev, |
31 | const struct of_device_id *match) | |
32 | { | |
33 | struct usb_hcd *hcd; | |
34 | struct device_node *dp = dev->node; | |
35 | struct resource *res; | |
36 | struct resource memory; | |
37 | struct of_irq oirq; | |
38 | int virq; | |
e07afd3f | 39 | resource_size_t res_len; |
db11e47d | 40 | int ret; |
3faefc88 NC |
41 | const unsigned int *prop; |
42 | unsigned int devflags = 0; | |
db11e47d SS |
43 | |
44 | ret = of_address_to_resource(dp, 0, &memory); | |
45 | if (ret) | |
46 | return -ENXIO; | |
47 | ||
e07afd3f TK |
48 | res_len = resource_size(&memory); |
49 | ||
50 | res = request_mem_region(memory.start, res_len, dev_name(&dev->dev)); | |
db11e47d SS |
51 | if (!res) |
52 | return -EBUSY; | |
53 | ||
db11e47d SS |
54 | if (of_irq_map_one(dp, 0, &oirq)) { |
55 | ret = -ENODEV; | |
56 | goto release_reg; | |
57 | } | |
58 | ||
59 | virq = irq_create_of_mapping(oirq.controller, oirq.specifier, | |
60 | oirq.size); | |
61 | ||
3faefc88 NC |
62 | if (of_device_is_compatible(dp, "nxp,usb-isp1761")) |
63 | devflags |= ISP1760_FLAG_ISP1761; | |
64 | ||
3faefc88 NC |
65 | /* Some systems wire up only 16 of the 32 data lines */ |
66 | prop = of_get_property(dp, "bus-width", NULL); | |
67 | if (prop && *prop == 16) | |
68 | devflags |= ISP1760_FLAG_BUS_WIDTH_16; | |
69 | ||
70 | if (of_get_property(dp, "port1-otg", NULL) != NULL) | |
71 | devflags |= ISP1760_FLAG_OTG_EN; | |
72 | ||
73 | if (of_get_property(dp, "analog-oc", NULL) != NULL) | |
74 | devflags |= ISP1760_FLAG_ANALOG_OC; | |
75 | ||
76 | if (of_get_property(dp, "dack-polarity", NULL) != NULL) | |
77 | devflags |= ISP1760_FLAG_DACK_POL_HIGH; | |
78 | ||
79 | if (of_get_property(dp, "dreq-polarity", NULL) != NULL) | |
80 | devflags |= ISP1760_FLAG_DREQ_POL_HIGH; | |
81 | ||
db11e47d | 82 | hcd = isp1760_register(memory.start, res_len, virq, |
3faefc88 NC |
83 | IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev), |
84 | devflags); | |
db11e47d SS |
85 | if (IS_ERR(hcd)) { |
86 | ret = PTR_ERR(hcd); | |
87 | goto release_reg; | |
88 | } | |
89 | ||
90 | dev_set_drvdata(&dev->dev, hcd); | |
91 | return ret; | |
92 | ||
93 | release_reg: | |
e07afd3f | 94 | release_mem_region(memory.start, res_len); |
db11e47d SS |
95 | return ret; |
96 | } | |
97 | ||
98 | static int of_isp1760_remove(struct of_device *dev) | |
99 | { | |
100 | struct usb_hcd *hcd = dev_get_drvdata(&dev->dev); | |
101 | ||
102 | dev_set_drvdata(&dev->dev, NULL); | |
103 | ||
104 | usb_remove_hcd(hcd); | |
105 | iounmap(hcd->regs); | |
106 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); | |
107 | usb_put_hcd(hcd); | |
108 | return 0; | |
109 | } | |
110 | ||
c4386ad0 | 111 | static const struct of_device_id of_isp1760_match[] = { |
db11e47d SS |
112 | { |
113 | .compatible = "nxp,usb-isp1760", | |
114 | }, | |
3faefc88 NC |
115 | { |
116 | .compatible = "nxp,usb-isp1761", | |
117 | }, | |
db11e47d SS |
118 | { }, |
119 | }; | |
120 | MODULE_DEVICE_TABLE(of, of_isp1760_match); | |
121 | ||
122 | static struct of_platform_driver isp1760_of_driver = { | |
123 | .name = "nxp-isp1760", | |
124 | .match_table = of_isp1760_match, | |
125 | .probe = of_isp1760_probe, | |
126 | .remove = of_isp1760_remove, | |
127 | }; | |
128 | #endif | |
129 | ||
ff30bf1c | 130 | #ifdef CONFIG_PCI |
db11e47d SS |
131 | static int __devinit isp1761_pci_probe(struct pci_dev *dev, |
132 | const struct pci_device_id *id) | |
133 | { | |
134 | u8 latency, limit; | |
135 | __u32 reg_data; | |
136 | int retry_count; | |
db11e47d | 137 | struct usb_hcd *hcd; |
3faefc88 | 138 | unsigned int devflags = 0; |
6013bbba KB |
139 | int ret_status = 0; |
140 | ||
141 | resource_size_t pci_mem_phy0; | |
142 | resource_size_t memlength; | |
143 | ||
144 | u8 __iomem *chip_addr; | |
145 | u8 __iomem *iobase; | |
146 | resource_size_t nxp_pci_io_base; | |
147 | resource_size_t iolength; | |
db11e47d SS |
148 | |
149 | if (usb_disabled()) | |
150 | return -ENODEV; | |
151 | ||
152 | if (pci_enable_device(dev) < 0) | |
153 | return -ENODEV; | |
154 | ||
155 | if (!dev->irq) | |
156 | return -ENODEV; | |
157 | ||
158 | /* Grab the PLX PCI mem maped port start address we need */ | |
159 | nxp_pci_io_base = pci_resource_start(dev, 0); | |
160 | iolength = pci_resource_len(dev, 0); | |
161 | ||
162 | if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) { | |
163 | printk(KERN_ERR "request region #1\n"); | |
164 | return -EBUSY; | |
165 | } | |
166 | ||
167 | iobase = ioremap_nocache(nxp_pci_io_base, iolength); | |
168 | if (!iobase) { | |
169 | printk(KERN_ERR "ioremap #1\n"); | |
6013bbba KB |
170 | ret_status = -ENOMEM; |
171 | goto cleanup1; | |
db11e47d SS |
172 | } |
173 | /* Grab the PLX PCI shared memory of the ISP 1761 we need */ | |
174 | pci_mem_phy0 = pci_resource_start(dev, 3); | |
6013bbba KB |
175 | memlength = pci_resource_len(dev, 3); |
176 | if (memlength < 0xffff) { | |
177 | printk(KERN_ERR "memory length for this resource is wrong\n"); | |
178 | ret_status = -ENOMEM; | |
179 | goto cleanup2; | |
db11e47d SS |
180 | } |
181 | ||
6013bbba | 182 | if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) { |
db11e47d | 183 | printk(KERN_ERR "host controller already in use\n"); |
6013bbba KB |
184 | ret_status = -EBUSY; |
185 | goto cleanup2; | |
186 | } | |
187 | ||
188 | /* map available memory */ | |
189 | chip_addr = ioremap_nocache(pci_mem_phy0,memlength); | |
190 | if (!chip_addr) { | |
191 | printk(KERN_ERR "Error ioremap failed\n"); | |
192 | ret_status = -ENOMEM; | |
193 | goto cleanup3; | |
db11e47d SS |
194 | } |
195 | ||
196 | /* bad pci latencies can contribute to overruns */ | |
197 | pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency); | |
198 | if (latency) { | |
199 | pci_read_config_byte(dev, PCI_MAX_LAT, &limit); | |
200 | if (limit && limit < latency) | |
201 | pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit); | |
202 | } | |
203 | ||
204 | /* Try to check whether we can access Scratch Register of | |
205 | * Host Controller or not. The initial PCI access is retried until | |
206 | * local init for the PCI bridge is completed | |
207 | */ | |
208 | retry_count = 20; | |
209 | reg_data = 0; | |
210 | while ((reg_data != 0xFACE) && retry_count) { | |
211 | /*by default host is in 16bit mode, so | |
212 | * io operations at this stage must be 16 bit | |
213 | * */ | |
214 | writel(0xface, chip_addr + HC_SCRATCH_REG); | |
215 | udelay(100); | |
6013bbba | 216 | reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff; |
db11e47d SS |
217 | retry_count--; |
218 | } | |
219 | ||
6013bbba KB |
220 | iounmap(chip_addr); |
221 | ||
db11e47d SS |
222 | /* Host Controller presence is detected by writing to scratch register |
223 | * and reading back and checking the contents are same or not | |
224 | */ | |
225 | if (reg_data != 0xFACE) { | |
802f389a | 226 | dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data); |
6013bbba KB |
227 | ret_status = -ENOMEM; |
228 | goto cleanup3; | |
db11e47d SS |
229 | } |
230 | ||
231 | pci_set_master(dev); | |
232 | ||
6013bbba KB |
233 | /* configure PLX PCI chip to pass interrupts */ |
234 | #define PLX_INT_CSR_REG 0x68 | |
235 | reg_data = readl(iobase + PLX_INT_CSR_REG); | |
236 | reg_data |= 0x900; | |
237 | writel(reg_data, iobase + PLX_INT_CSR_REG); | |
db11e47d SS |
238 | |
239 | dev->dev.dma_mask = NULL; | |
6013bbba | 240 | hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq, |
3faefc88 NC |
241 | IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev), |
242 | devflags); | |
6013bbba KB |
243 | if (IS_ERR(hcd)) { |
244 | ret_status = -ENODEV; | |
245 | goto cleanup3; | |
ce5dee50 | 246 | } |
6013bbba KB |
247 | |
248 | /* done with PLX IO access */ | |
db11e47d | 249 | iounmap(iobase); |
db11e47d | 250 | release_mem_region(nxp_pci_io_base, iolength); |
6013bbba KB |
251 | |
252 | pci_set_drvdata(dev, hcd); | |
253 | return 0; | |
254 | ||
255 | cleanup3: | |
256 | release_mem_region(pci_mem_phy0, memlength); | |
257 | cleanup2: | |
258 | iounmap(iobase); | |
259 | cleanup1: | |
260 | release_mem_region(nxp_pci_io_base, iolength); | |
261 | return ret_status; | |
db11e47d | 262 | } |
6013bbba | 263 | |
db11e47d SS |
264 | static void isp1761_pci_remove(struct pci_dev *dev) |
265 | { | |
266 | struct usb_hcd *hcd; | |
267 | ||
268 | hcd = pci_get_drvdata(dev); | |
269 | ||
270 | usb_remove_hcd(hcd); | |
271 | iounmap(hcd->regs); | |
272 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); | |
273 | usb_put_hcd(hcd); | |
274 | ||
275 | pci_disable_device(dev); | |
db11e47d SS |
276 | } |
277 | ||
278 | static void isp1761_pci_shutdown(struct pci_dev *dev) | |
279 | { | |
280 | printk(KERN_ERR "ips1761_pci_shutdown\n"); | |
281 | } | |
282 | ||
6c073568 SAS |
283 | static const struct pci_device_id isp1760_plx [] = { |
284 | { | |
285 | .class = PCI_CLASS_BRIDGE_OTHER << 8, | |
286 | .class_mask = ~0, | |
287 | .vendor = PCI_VENDOR_ID_PLX, | |
288 | .device = 0x5406, | |
289 | .subvendor = PCI_VENDOR_ID_PLX, | |
290 | .subdevice = 0x9054, | |
291 | }, | |
292 | { } | |
db11e47d SS |
293 | }; |
294 | MODULE_DEVICE_TABLE(pci, isp1760_plx); | |
295 | ||
296 | static struct pci_driver isp1761_pci_driver = { | |
297 | .name = "isp1760", | |
298 | .id_table = isp1760_plx, | |
299 | .probe = isp1761_pci_probe, | |
300 | .remove = isp1761_pci_remove, | |
301 | .shutdown = isp1761_pci_shutdown, | |
302 | }; | |
303 | #endif | |
304 | ||
f7e7aa58 CM |
305 | static int __devinit isp1760_plat_probe(struct platform_device *pdev) |
306 | { | |
307 | int ret = 0; | |
308 | struct usb_hcd *hcd; | |
309 | struct resource *mem_res; | |
310 | struct resource *irq_res; | |
311 | resource_size_t mem_size; | |
9da69c60 MH |
312 | struct isp1760_platform_data *priv = pdev->dev.platform_data; |
313 | unsigned int devflags = 0; | |
f7e7aa58 CM |
314 | unsigned long irqflags = IRQF_SHARED | IRQF_DISABLED; |
315 | ||
316 | mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
317 | if (!mem_res) { | |
318 | pr_warning("isp1760: Memory resource not available\n"); | |
319 | ret = -ENODEV; | |
320 | goto out; | |
321 | } | |
322 | mem_size = resource_size(mem_res); | |
323 | if (!request_mem_region(mem_res->start, mem_size, "isp1760")) { | |
324 | pr_warning("isp1760: Cannot reserve the memory resource\n"); | |
325 | ret = -EBUSY; | |
326 | goto out; | |
327 | } | |
328 | ||
329 | irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
330 | if (!irq_res) { | |
331 | pr_warning("isp1760: IRQ resource not available\n"); | |
332 | return -ENODEV; | |
333 | } | |
334 | irqflags |= irq_res->flags & IRQF_TRIGGER_MASK; | |
335 | ||
9da69c60 MH |
336 | if (priv) { |
337 | if (priv->is_isp1761) | |
338 | devflags |= ISP1760_FLAG_ISP1761; | |
339 | if (priv->bus_width_16) | |
340 | devflags |= ISP1760_FLAG_BUS_WIDTH_16; | |
341 | if (priv->port1_otg) | |
342 | devflags |= ISP1760_FLAG_OTG_EN; | |
343 | if (priv->analog_oc) | |
344 | devflags |= ISP1760_FLAG_ANALOG_OC; | |
345 | if (priv->dack_polarity_high) | |
346 | devflags |= ISP1760_FLAG_DACK_POL_HIGH; | |
347 | if (priv->dreq_polarity_high) | |
348 | devflags |= ISP1760_FLAG_DREQ_POL_HIGH; | |
349 | } | |
350 | ||
f7e7aa58 | 351 | hcd = isp1760_register(mem_res->start, mem_size, irq_res->start, |
9da69c60 | 352 | irqflags, &pdev->dev, dev_name(&pdev->dev), devflags); |
f7e7aa58 CM |
353 | if (IS_ERR(hcd)) { |
354 | pr_warning("isp1760: Failed to register the HCD device\n"); | |
355 | ret = -ENODEV; | |
356 | goto cleanup; | |
357 | } | |
358 | ||
359 | pr_info("ISP1760 USB device initialised\n"); | |
360 | return ret; | |
361 | ||
362 | cleanup: | |
363 | release_mem_region(mem_res->start, mem_size); | |
364 | out: | |
365 | return ret; | |
366 | } | |
367 | ||
368 | static int __devexit isp1760_plat_remove(struct platform_device *pdev) | |
369 | { | |
370 | struct resource *mem_res; | |
371 | resource_size_t mem_size; | |
372 | ||
373 | mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
374 | mem_size = resource_size(mem_res); | |
375 | release_mem_region(mem_res->start, mem_size); | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
380 | static struct platform_driver isp1760_plat_driver = { | |
381 | .probe = isp1760_plat_probe, | |
4198e4f7 | 382 | .remove = __devexit_p(isp1760_plat_remove), |
f7e7aa58 CM |
383 | .driver = { |
384 | .name = "isp1760", | |
385 | }, | |
386 | }; | |
387 | ||
db11e47d SS |
388 | static int __init isp1760_init(void) |
389 | { | |
f7e7aa58 | 390 | int ret, any_ret = -ENODEV; |
db11e47d SS |
391 | |
392 | init_kmem_once(); | |
393 | ||
f7e7aa58 CM |
394 | ret = platform_driver_register(&isp1760_plat_driver); |
395 | if (!ret) | |
396 | any_ret = 0; | |
ff30bf1c | 397 | #ifdef CONFIG_PPC_OF |
db11e47d | 398 | ret = of_register_platform_driver(&isp1760_of_driver); |
f7e7aa58 CM |
399 | if (!ret) |
400 | any_ret = 0; | |
db11e47d | 401 | #endif |
ff30bf1c | 402 | #ifdef CONFIG_PCI |
db11e47d | 403 | ret = pci_register_driver(&isp1761_pci_driver); |
f7e7aa58 CM |
404 | if (!ret) |
405 | any_ret = 0; | |
db11e47d | 406 | #endif |
db11e47d | 407 | |
f7e7aa58 CM |
408 | if (any_ret) |
409 | deinit_kmem_cache(); | |
410 | return any_ret; | |
db11e47d SS |
411 | } |
412 | module_init(isp1760_init); | |
413 | ||
414 | static void __exit isp1760_exit(void) | |
415 | { | |
f7e7aa58 | 416 | platform_driver_unregister(&isp1760_plat_driver); |
ff30bf1c | 417 | #ifdef CONFIG_PPC_OF |
db11e47d SS |
418 | of_unregister_platform_driver(&isp1760_of_driver); |
419 | #endif | |
ff30bf1c | 420 | #ifdef CONFIG_PCI |
db11e47d SS |
421 | pci_unregister_driver(&isp1761_pci_driver); |
422 | #endif | |
423 | deinit_kmem_cache(); | |
424 | } | |
425 | module_exit(isp1760_exit); |