isp1760-if.c 7.3 KB

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