isp1760-if.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Glue code for the ISP1760 driver and bus
  3. * Currently there is support for
  4. * - OpenFirmware
  5. * - PCI
  6. *
  7. * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
  8. *
  9. */
  10. #include <linux/usb.h>
  11. #include <linux/io.h>
  12. #include "../core/hcd.h"
  13. #include "isp1760-hcd.h"
  14. #ifdef CONFIG_USB_ISP1760_OF
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #endif
  18. #ifdef CONFIG_USB_ISP1760_PCI
  19. #include <linux/pci.h>
  20. #endif
  21. #ifdef CONFIG_USB_ISP1760_OF
  22. static int of_isp1760_probe(struct of_device *dev,
  23. const struct of_device_id *match)
  24. {
  25. struct usb_hcd *hcd;
  26. struct device_node *dp = dev->node;
  27. struct resource *res;
  28. struct resource memory;
  29. struct of_irq oirq;
  30. int virq;
  31. u64 res_len;
  32. int ret;
  33. ret = of_address_to_resource(dp, 0, &memory);
  34. if (ret)
  35. return -ENXIO;
  36. res = request_mem_region(memory.start, memory.end - memory.start + 1,
  37. dev->dev.bus_id);
  38. if (!res)
  39. return -EBUSY;
  40. res_len = memory.end - memory.start + 1;
  41. if (of_irq_map_one(dp, 0, &oirq)) {
  42. ret = -ENODEV;
  43. goto release_reg;
  44. }
  45. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  46. oirq.size);
  47. hcd = isp1760_register(memory.start, res_len, virq,
  48. IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev->dev.bus_id);
  49. if (IS_ERR(hcd)) {
  50. ret = PTR_ERR(hcd);
  51. goto release_reg;
  52. }
  53. dev_set_drvdata(&dev->dev, hcd);
  54. return ret;
  55. release_reg:
  56. release_mem_region(memory.start, memory.end - memory.start + 1);
  57. return ret;
  58. }
  59. static int of_isp1760_remove(struct of_device *dev)
  60. {
  61. struct usb_hcd *hcd = dev_get_drvdata(&dev->dev);
  62. dev_set_drvdata(&dev->dev, NULL);
  63. usb_remove_hcd(hcd);
  64. iounmap(hcd->regs);
  65. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  66. usb_put_hcd(hcd);
  67. return 0;
  68. }
  69. static struct of_device_id of_isp1760_match[] = {
  70. {
  71. .compatible = "nxp,usb-isp1760",
  72. },
  73. { },
  74. };
  75. MODULE_DEVICE_TABLE(of, of_isp1760_match);
  76. static struct of_platform_driver isp1760_of_driver = {
  77. .name = "nxp-isp1760",
  78. .match_table = of_isp1760_match,
  79. .probe = of_isp1760_probe,
  80. .remove = of_isp1760_remove,
  81. };
  82. #endif
  83. #ifdef CONFIG_USB_ISP1760_PCI
  84. static u32 nxp_pci_io_base;
  85. static u32 iolength;
  86. static u32 pci_mem_phy0;
  87. static u32 length;
  88. static u8 *chip_addr;
  89. static u8 *iobase;
  90. static int __devinit isp1761_pci_probe(struct pci_dev *dev,
  91. const struct pci_device_id *id)
  92. {
  93. u8 latency, limit;
  94. __u32 reg_data;
  95. int retry_count;
  96. int length;
  97. int status = 1;
  98. struct usb_hcd *hcd;
  99. if (usb_disabled())
  100. return -ENODEV;
  101. if (pci_enable_device(dev) < 0)
  102. return -ENODEV;
  103. if (!dev->irq)
  104. return -ENODEV;
  105. /* Grab the PLX PCI mem maped port start address we need */
  106. nxp_pci_io_base = pci_resource_start(dev, 0);
  107. iolength = pci_resource_len(dev, 0);
  108. if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
  109. printk(KERN_ERR "request region #1\n");
  110. return -EBUSY;
  111. }
  112. iobase = ioremap_nocache(nxp_pci_io_base, iolength);
  113. if (!iobase) {
  114. printk(KERN_ERR "ioremap #1\n");
  115. release_mem_region(nxp_pci_io_base, iolength);
  116. return -ENOMEM;
  117. }
  118. /* Grab the PLX PCI shared memory of the ISP 1761 we need */
  119. pci_mem_phy0 = pci_resource_start(dev, 3);
  120. length = pci_resource_len(dev, 3);
  121. if (length < 0xffff) {
  122. printk(KERN_ERR "memory length for this resource is less than "
  123. "required\n");
  124. release_mem_region(nxp_pci_io_base, iolength);
  125. iounmap(iobase);
  126. return -ENOMEM;
  127. }
  128. if (!request_mem_region(pci_mem_phy0, length, "ISP-PCI")) {
  129. printk(KERN_ERR "host controller already in use\n");
  130. release_mem_region(nxp_pci_io_base, iolength);
  131. iounmap(iobase);
  132. return -EBUSY;
  133. }
  134. /* bad pci latencies can contribute to overruns */
  135. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
  136. if (latency) {
  137. pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
  138. if (limit && limit < latency)
  139. pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
  140. }
  141. /* Try to check whether we can access Scratch Register of
  142. * Host Controller or not. The initial PCI access is retried until
  143. * local init for the PCI bridge is completed
  144. */
  145. retry_count = 20;
  146. reg_data = 0;
  147. while ((reg_data != 0xFACE) && retry_count) {
  148. /*by default host is in 16bit mode, so
  149. * io operations at this stage must be 16 bit
  150. * */
  151. writel(0xface, chip_addr + HC_SCRATCH_REG);
  152. udelay(100);
  153. reg_data = readl(chip_addr + HC_SCRATCH_REG);
  154. retry_count--;
  155. }
  156. /* Host Controller presence is detected by writing to scratch register
  157. * and reading back and checking the contents are same or not
  158. */
  159. if (reg_data != 0xFACE) {
  160. err("scratch register mismatch %x", reg_data);
  161. goto clean;
  162. }
  163. pci_set_master(dev);
  164. status = readl(iobase + 0x68);
  165. status |= 0x900;
  166. writel(status, iobase + 0x68);
  167. dev->dev.dma_mask = NULL;
  168. hcd = isp1760_register(pci_mem_phy0, length, dev->irq,
  169. IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev->dev.bus_id);
  170. pci_set_drvdata(dev, hcd);
  171. if (!hcd)
  172. return 0;
  173. clean:
  174. status = -ENODEV;
  175. iounmap(iobase);
  176. release_mem_region(pci_mem_phy0, length);
  177. release_mem_region(nxp_pci_io_base, iolength);
  178. return status;
  179. }
  180. static void isp1761_pci_remove(struct pci_dev *dev)
  181. {
  182. struct usb_hcd *hcd;
  183. hcd = pci_get_drvdata(dev);
  184. usb_remove_hcd(hcd);
  185. iounmap(hcd->regs);
  186. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  187. usb_put_hcd(hcd);
  188. pci_disable_device(dev);
  189. iounmap(iobase);
  190. iounmap(chip_addr);
  191. release_mem_region(nxp_pci_io_base, iolength);
  192. release_mem_region(pci_mem_phy0, length);
  193. }
  194. static void isp1761_pci_shutdown(struct pci_dev *dev)
  195. {
  196. printk(KERN_ERR "ips1761_pci_shutdown\n");
  197. }
  198. static const struct pci_device_id isp1760_plx [] = { {
  199. /* handle any USB 2.0 EHCI controller */
  200. PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_OTHER << 8) | (0x06 << 16)), ~0),
  201. .driver_data = 0,
  202. },
  203. { /* end: all zeroes */ }
  204. };
  205. MODULE_DEVICE_TABLE(pci, isp1760_plx);
  206. static struct pci_driver isp1761_pci_driver = {
  207. .name = "isp1760",
  208. .id_table = isp1760_plx,
  209. .probe = isp1761_pci_probe,
  210. .remove = isp1761_pci_remove,
  211. .shutdown = isp1761_pci_shutdown,
  212. };
  213. #endif
  214. static int __init isp1760_init(void)
  215. {
  216. int ret = -ENODEV;
  217. init_kmem_once();
  218. #ifdef CONFIG_USB_ISP1760_OF
  219. ret = of_register_platform_driver(&isp1760_of_driver);
  220. if (ret) {
  221. deinit_kmem_cache();
  222. return ret;
  223. }
  224. #endif
  225. #ifdef CONFIG_USB_ISP1760_PCI
  226. ret = pci_register_driver(&isp1761_pci_driver);
  227. if (ret)
  228. goto unreg_of;
  229. #endif
  230. return ret;
  231. #ifdef CONFIG_USB_ISP1760_PCI
  232. unreg_of:
  233. #endif
  234. #ifdef CONFIG_USB_ISP1760_OF
  235. of_unregister_platform_driver(&isp1760_of_driver);
  236. #endif
  237. deinit_kmem_cache();
  238. return ret;
  239. }
  240. module_init(isp1760_init);
  241. static void __exit isp1760_exit(void)
  242. {
  243. #ifdef CONFIG_USB_ISP1760_OF
  244. of_unregister_platform_driver(&isp1760_of_driver);
  245. #endif
  246. #ifdef CONFIG_USB_ISP1760_PCI
  247. pci_unregister_driver(&isp1761_pci_driver);
  248. #endif
  249. deinit_kmem_cache();
  250. }
  251. module_exit(isp1760_exit);