isp1760-if.c 7.5 KB

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