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