isp1760-if.c 7.0 KB

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