isp1760-if.c 7.3 KB

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