isp1760-if.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Glue code for the ISP1760 driver and bus
  3. * Currently there is support for
  4. * - OpenFirmware
  5. * - PCI
  6. * - PDEV (generic platform device centralized driver model)
  7. *
  8. * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
  9. *
  10. */
  11. #include <linux/usb.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/usb/isp1760.h>
  18. #include <linux/usb/hcd.h>
  19. #include "isp1760-hcd.h"
  20. #ifdef CONFIG_PCI
  21. #include <linux/pci.h>
  22. #endif
  23. #ifdef CONFIG_PCI
  24. static int isp1761_pci_probe(struct pci_dev *dev,
  25. const struct pci_device_id *id)
  26. {
  27. u8 latency, limit;
  28. __u32 reg_data;
  29. int retry_count;
  30. struct usb_hcd *hcd;
  31. unsigned int devflags = 0;
  32. int ret_status = 0;
  33. resource_size_t pci_mem_phy0;
  34. resource_size_t memlength;
  35. u8 __iomem *chip_addr;
  36. u8 __iomem *iobase;
  37. resource_size_t nxp_pci_io_base;
  38. resource_size_t iolength;
  39. if (usb_disabled())
  40. return -ENODEV;
  41. if (pci_enable_device(dev) < 0)
  42. return -ENODEV;
  43. if (!dev->irq)
  44. return -ENODEV;
  45. /* Grab the PLX PCI mem maped port start address we need */
  46. nxp_pci_io_base = pci_resource_start(dev, 0);
  47. iolength = pci_resource_len(dev, 0);
  48. if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
  49. printk(KERN_ERR "request region #1\n");
  50. return -EBUSY;
  51. }
  52. iobase = ioremap_nocache(nxp_pci_io_base, iolength);
  53. if (!iobase) {
  54. printk(KERN_ERR "ioremap #1\n");
  55. ret_status = -ENOMEM;
  56. goto cleanup1;
  57. }
  58. /* Grab the PLX PCI shared memory of the ISP 1761 we need */
  59. pci_mem_phy0 = pci_resource_start(dev, 3);
  60. memlength = pci_resource_len(dev, 3);
  61. if (memlength < 0xffff) {
  62. printk(KERN_ERR "memory length for this resource is wrong\n");
  63. ret_status = -ENOMEM;
  64. goto cleanup2;
  65. }
  66. if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
  67. printk(KERN_ERR "host controller already in use\n");
  68. ret_status = -EBUSY;
  69. goto cleanup2;
  70. }
  71. /* map available memory */
  72. chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
  73. if (!chip_addr) {
  74. printk(KERN_ERR "Error ioremap failed\n");
  75. ret_status = -ENOMEM;
  76. goto cleanup3;
  77. }
  78. /* bad pci latencies can contribute to overruns */
  79. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
  80. if (latency) {
  81. pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
  82. if (limit && limit < latency)
  83. pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
  84. }
  85. /* Try to check whether we can access Scratch Register of
  86. * Host Controller or not. The initial PCI access is retried until
  87. * local init for the PCI bridge is completed
  88. */
  89. retry_count = 20;
  90. reg_data = 0;
  91. while ((reg_data != 0xFACE) && retry_count) {
  92. /*by default host is in 16bit mode, so
  93. * io operations at this stage must be 16 bit
  94. * */
  95. writel(0xface, chip_addr + HC_SCRATCH_REG);
  96. udelay(100);
  97. reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
  98. retry_count--;
  99. }
  100. iounmap(chip_addr);
  101. /* Host Controller presence is detected by writing to scratch register
  102. * and reading back and checking the contents are same or not
  103. */
  104. if (reg_data != 0xFACE) {
  105. dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
  106. ret_status = -ENOMEM;
  107. goto cleanup3;
  108. }
  109. pci_set_master(dev);
  110. /* configure PLX PCI chip to pass interrupts */
  111. #define PLX_INT_CSR_REG 0x68
  112. reg_data = readl(iobase + PLX_INT_CSR_REG);
  113. reg_data |= 0x900;
  114. writel(reg_data, iobase + PLX_INT_CSR_REG);
  115. dev->dev.dma_mask = NULL;
  116. hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
  117. IRQF_SHARED, &dev->dev, dev_name(&dev->dev),
  118. devflags);
  119. if (IS_ERR(hcd)) {
  120. ret_status = -ENODEV;
  121. goto cleanup3;
  122. }
  123. /* done with PLX IO access */
  124. iounmap(iobase);
  125. release_mem_region(nxp_pci_io_base, iolength);
  126. pci_set_drvdata(dev, hcd);
  127. return 0;
  128. cleanup3:
  129. release_mem_region(pci_mem_phy0, memlength);
  130. cleanup2:
  131. iounmap(iobase);
  132. cleanup1:
  133. release_mem_region(nxp_pci_io_base, iolength);
  134. return ret_status;
  135. }
  136. static void isp1761_pci_remove(struct pci_dev *dev)
  137. {
  138. isp1760_unregister(&dev->dev);
  139. pci_disable_device(dev);
  140. }
  141. static void isp1761_pci_shutdown(struct pci_dev *dev)
  142. {
  143. printk(KERN_ERR "ips1761_pci_shutdown\n");
  144. }
  145. static const struct pci_device_id isp1760_plx [] = {
  146. {
  147. .class = PCI_CLASS_BRIDGE_OTHER << 8,
  148. .class_mask = ~0,
  149. .vendor = PCI_VENDOR_ID_PLX,
  150. .device = 0x5406,
  151. .subvendor = PCI_VENDOR_ID_PLX,
  152. .subdevice = 0x9054,
  153. },
  154. { }
  155. };
  156. MODULE_DEVICE_TABLE(pci, isp1760_plx);
  157. static struct pci_driver isp1761_pci_driver = {
  158. .name = "isp1760",
  159. .id_table = isp1760_plx,
  160. .probe = isp1761_pci_probe,
  161. .remove = isp1761_pci_remove,
  162. .shutdown = isp1761_pci_shutdown,
  163. };
  164. #endif
  165. static int isp1760_plat_probe(struct platform_device *pdev)
  166. {
  167. unsigned long irqflags = IRQF_SHARED;
  168. unsigned int devflags = 0;
  169. struct resource *mem_res;
  170. struct resource *irq_res;
  171. resource_size_t mem_size;
  172. struct usb_hcd *hcd;
  173. int ret;
  174. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  175. if (!mem_res) {
  176. pr_warning("isp1760: Memory resource not available\n");
  177. return -ENODEV;
  178. }
  179. mem_size = resource_size(mem_res);
  180. if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
  181. pr_warning("isp1760: Cannot reserve the memory resource\n");
  182. return -EBUSY;
  183. }
  184. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  185. if (!irq_res) {
  186. pr_warning("isp1760: IRQ resource not available\n");
  187. ret = -ENODEV;
  188. goto cleanup;
  189. }
  190. irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
  191. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  192. struct device_node *dp = pdev->dev.of_node;
  193. u32 bus_width = 0;
  194. if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
  195. devflags |= ISP1760_FLAG_ISP1761;
  196. /* Some systems wire up only 16 of the 32 data lines */
  197. of_property_read_u32(dp, "bus-width", &bus_width);
  198. if (bus_width == 16)
  199. devflags |= ISP1760_FLAG_BUS_WIDTH_16;
  200. if (of_property_read_bool(dp, "port1-otg"))
  201. devflags |= ISP1760_FLAG_OTG_EN;
  202. if (of_property_read_bool(dp, "analog-oc"))
  203. devflags |= ISP1760_FLAG_ANALOG_OC;
  204. if (of_property_read_bool(dp, "dack-polarity"))
  205. devflags |= ISP1760_FLAG_DACK_POL_HIGH;
  206. if (of_property_read_bool(dp, "dreq-polarity"))
  207. devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
  208. } else if (dev_get_platdata(&pdev->dev)) {
  209. struct isp1760_platform_data *pdata =
  210. dev_get_platdata(&pdev->dev);
  211. if (pdata->is_isp1761)
  212. devflags |= ISP1760_FLAG_ISP1761;
  213. if (pdata->bus_width_16)
  214. devflags |= ISP1760_FLAG_BUS_WIDTH_16;
  215. if (pdata->port1_otg)
  216. devflags |= ISP1760_FLAG_OTG_EN;
  217. if (pdata->analog_oc)
  218. devflags |= ISP1760_FLAG_ANALOG_OC;
  219. if (pdata->dack_polarity_high)
  220. devflags |= ISP1760_FLAG_DACK_POL_HIGH;
  221. if (pdata->dreq_polarity_high)
  222. devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
  223. }
  224. hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
  225. irqflags, &pdev->dev, dev_name(&pdev->dev),
  226. devflags);
  227. if (IS_ERR(hcd)) {
  228. pr_warning("isp1760: Failed to register the HCD device\n");
  229. ret = PTR_ERR(hcd);
  230. goto cleanup;
  231. }
  232. platform_set_drvdata(pdev, hcd);
  233. pr_info("ISP1760 USB device initialised\n");
  234. return 0;
  235. cleanup:
  236. release_mem_region(mem_res->start, mem_size);
  237. return ret;
  238. }
  239. static int isp1760_plat_remove(struct platform_device *pdev)
  240. {
  241. isp1760_unregister(&pdev->dev);
  242. return 0;
  243. }
  244. #ifdef CONFIG_OF
  245. static const struct of_device_id isp1760_of_match[] = {
  246. { .compatible = "nxp,usb-isp1760", },
  247. { .compatible = "nxp,usb-isp1761", },
  248. { },
  249. };
  250. MODULE_DEVICE_TABLE(of, isp1760_of_match);
  251. #endif
  252. static struct platform_driver isp1760_plat_driver = {
  253. .probe = isp1760_plat_probe,
  254. .remove = isp1760_plat_remove,
  255. .driver = {
  256. .name = "isp1760",
  257. .of_match_table = of_match_ptr(isp1760_of_match),
  258. },
  259. };
  260. static int __init isp1760_init(void)
  261. {
  262. int ret, any_ret = -ENODEV;
  263. init_kmem_once();
  264. ret = platform_driver_register(&isp1760_plat_driver);
  265. if (!ret)
  266. any_ret = 0;
  267. #ifdef CONFIG_PCI
  268. ret = pci_register_driver(&isp1761_pci_driver);
  269. if (!ret)
  270. any_ret = 0;
  271. #endif
  272. if (any_ret)
  273. deinit_kmem_cache();
  274. return any_ret;
  275. }
  276. module_init(isp1760_init);
  277. static void __exit isp1760_exit(void)
  278. {
  279. platform_driver_unregister(&isp1760_plat_driver);
  280. #ifdef CONFIG_PCI
  281. pci_unregister_driver(&isp1761_pci_driver);
  282. #endif
  283. deinit_kmem_cache();
  284. }
  285. module_exit(isp1760_exit);