isp1760-if.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <linux/platform_device.h>
  13. #include "../core/hcd.h"
  14. #include "isp1760-hcd.h"
  15. #ifdef CONFIG_PPC_OF
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #endif
  19. #ifdef CONFIG_PCI
  20. #include <linux/pci.h>
  21. #endif
  22. #ifdef CONFIG_PPC_OF
  23. static int of_isp1760_probe(struct of_device *dev,
  24. const struct of_device_id *match)
  25. {
  26. struct usb_hcd *hcd;
  27. struct device_node *dp = dev->node;
  28. struct resource *res;
  29. struct resource memory;
  30. struct of_irq oirq;
  31. int virq;
  32. u64 res_len;
  33. int ret;
  34. const unsigned int *prop;
  35. unsigned int devflags = 0;
  36. ret = of_address_to_resource(dp, 0, &memory);
  37. if (ret)
  38. return -ENXIO;
  39. res = request_mem_region(memory.start, memory.end - memory.start + 1,
  40. dev_name(&dev->dev));
  41. if (!res)
  42. return -EBUSY;
  43. res_len = memory.end - memory.start + 1;
  44. if (of_irq_map_one(dp, 0, &oirq)) {
  45. ret = -ENODEV;
  46. goto release_reg;
  47. }
  48. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  49. oirq.size);
  50. if (of_device_is_compatible(dp, "nxp,usb-isp1761"))
  51. devflags |= ISP1760_FLAG_ISP1761;
  52. /* Some systems wire up only 16 of the 32 data lines */
  53. prop = of_get_property(dp, "bus-width", NULL);
  54. if (prop && *prop == 16)
  55. devflags |= ISP1760_FLAG_BUS_WIDTH_16;
  56. if (of_get_property(dp, "port1-otg", NULL) != NULL)
  57. devflags |= ISP1760_FLAG_OTG_EN;
  58. if (of_get_property(dp, "analog-oc", NULL) != NULL)
  59. devflags |= ISP1760_FLAG_ANALOG_OC;
  60. if (of_get_property(dp, "dack-polarity", NULL) != NULL)
  61. devflags |= ISP1760_FLAG_DACK_POL_HIGH;
  62. if (of_get_property(dp, "dreq-polarity", NULL) != NULL)
  63. devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
  64. hcd = isp1760_register(memory.start, res_len, virq,
  65. IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
  66. devflags);
  67. if (IS_ERR(hcd)) {
  68. ret = PTR_ERR(hcd);
  69. goto release_reg;
  70. }
  71. dev_set_drvdata(&dev->dev, hcd);
  72. return ret;
  73. release_reg:
  74. release_mem_region(memory.start, memory.end - memory.start + 1);
  75. return ret;
  76. }
  77. static int of_isp1760_remove(struct of_device *dev)
  78. {
  79. struct usb_hcd *hcd = dev_get_drvdata(&dev->dev);
  80. dev_set_drvdata(&dev->dev, NULL);
  81. usb_remove_hcd(hcd);
  82. iounmap(hcd->regs);
  83. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  84. usb_put_hcd(hcd);
  85. return 0;
  86. }
  87. static struct of_device_id of_isp1760_match[] = {
  88. {
  89. .compatible = "nxp,usb-isp1760",
  90. },
  91. {
  92. .compatible = "nxp,usb-isp1761",
  93. },
  94. { },
  95. };
  96. MODULE_DEVICE_TABLE(of, of_isp1760_match);
  97. static struct of_platform_driver isp1760_of_driver = {
  98. .name = "nxp-isp1760",
  99. .match_table = of_isp1760_match,
  100. .probe = of_isp1760_probe,
  101. .remove = of_isp1760_remove,
  102. };
  103. #endif
  104. #ifdef CONFIG_PCI
  105. static int __devinit isp1761_pci_probe(struct pci_dev *dev,
  106. const struct pci_device_id *id)
  107. {
  108. u8 latency, limit;
  109. __u32 reg_data;
  110. int retry_count;
  111. struct usb_hcd *hcd;
  112. unsigned int devflags = 0;
  113. int ret_status = 0;
  114. resource_size_t pci_mem_phy0;
  115. resource_size_t memlength;
  116. u8 __iomem *chip_addr;
  117. u8 __iomem *iobase;
  118. resource_size_t nxp_pci_io_base;
  119. resource_size_t iolength;
  120. if (usb_disabled())
  121. return -ENODEV;
  122. if (pci_enable_device(dev) < 0)
  123. return -ENODEV;
  124. if (!dev->irq)
  125. return -ENODEV;
  126. /* Grab the PLX PCI mem maped port start address we need */
  127. nxp_pci_io_base = pci_resource_start(dev, 0);
  128. iolength = pci_resource_len(dev, 0);
  129. if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
  130. printk(KERN_ERR "request region #1\n");
  131. return -EBUSY;
  132. }
  133. iobase = ioremap_nocache(nxp_pci_io_base, iolength);
  134. if (!iobase) {
  135. printk(KERN_ERR "ioremap #1\n");
  136. ret_status = -ENOMEM;
  137. goto cleanup1;
  138. }
  139. /* Grab the PLX PCI shared memory of the ISP 1761 we need */
  140. pci_mem_phy0 = pci_resource_start(dev, 3);
  141. memlength = pci_resource_len(dev, 3);
  142. if (memlength < 0xffff) {
  143. printk(KERN_ERR "memory length for this resource is wrong\n");
  144. ret_status = -ENOMEM;
  145. goto cleanup2;
  146. }
  147. if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
  148. printk(KERN_ERR "host controller already in use\n");
  149. ret_status = -EBUSY;
  150. goto cleanup2;
  151. }
  152. /* map available memory */
  153. chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
  154. if (!chip_addr) {
  155. printk(KERN_ERR "Error ioremap failed\n");
  156. ret_status = -ENOMEM;
  157. goto cleanup3;
  158. }
  159. /* bad pci latencies can contribute to overruns */
  160. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
  161. if (latency) {
  162. pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
  163. if (limit && limit < latency)
  164. pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
  165. }
  166. /* Try to check whether we can access Scratch Register of
  167. * Host Controller or not. The initial PCI access is retried until
  168. * local init for the PCI bridge is completed
  169. */
  170. retry_count = 20;
  171. reg_data = 0;
  172. while ((reg_data != 0xFACE) && retry_count) {
  173. /*by default host is in 16bit mode, so
  174. * io operations at this stage must be 16 bit
  175. * */
  176. writel(0xface, chip_addr + HC_SCRATCH_REG);
  177. udelay(100);
  178. reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
  179. retry_count--;
  180. }
  181. iounmap(chip_addr);
  182. /* Host Controller presence is detected by writing to scratch register
  183. * and reading back and checking the contents are same or not
  184. */
  185. if (reg_data != 0xFACE) {
  186. dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
  187. ret_status = -ENOMEM;
  188. goto cleanup3;
  189. }
  190. pci_set_master(dev);
  191. /* configure PLX PCI chip to pass interrupts */
  192. #define PLX_INT_CSR_REG 0x68
  193. reg_data = readl(iobase + PLX_INT_CSR_REG);
  194. reg_data |= 0x900;
  195. writel(reg_data, iobase + PLX_INT_CSR_REG);
  196. dev->dev.dma_mask = NULL;
  197. hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
  198. IRQF_SHARED | IRQF_DISABLED, &dev->dev, dev_name(&dev->dev),
  199. devflags);
  200. if (IS_ERR(hcd)) {
  201. ret_status = -ENODEV;
  202. goto cleanup3;
  203. }
  204. /* done with PLX IO access */
  205. iounmap(iobase);
  206. release_mem_region(nxp_pci_io_base, iolength);
  207. pci_set_drvdata(dev, hcd);
  208. return 0;
  209. cleanup3:
  210. release_mem_region(pci_mem_phy0, memlength);
  211. cleanup2:
  212. iounmap(iobase);
  213. cleanup1:
  214. release_mem_region(nxp_pci_io_base, iolength);
  215. return ret_status;
  216. }
  217. static void isp1761_pci_remove(struct pci_dev *dev)
  218. {
  219. struct usb_hcd *hcd;
  220. hcd = pci_get_drvdata(dev);
  221. usb_remove_hcd(hcd);
  222. iounmap(hcd->regs);
  223. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  224. usb_put_hcd(hcd);
  225. pci_disable_device(dev);
  226. }
  227. static void isp1761_pci_shutdown(struct pci_dev *dev)
  228. {
  229. printk(KERN_ERR "ips1761_pci_shutdown\n");
  230. }
  231. static const struct pci_device_id isp1760_plx [] = {
  232. {
  233. .class = PCI_CLASS_BRIDGE_OTHER << 8,
  234. .class_mask = ~0,
  235. .vendor = PCI_VENDOR_ID_PLX,
  236. .device = 0x5406,
  237. .subvendor = PCI_VENDOR_ID_PLX,
  238. .subdevice = 0x9054,
  239. },
  240. { }
  241. };
  242. MODULE_DEVICE_TABLE(pci, isp1760_plx);
  243. static struct pci_driver isp1761_pci_driver = {
  244. .name = "isp1760",
  245. .id_table = isp1760_plx,
  246. .probe = isp1761_pci_probe,
  247. .remove = isp1761_pci_remove,
  248. .shutdown = isp1761_pci_shutdown,
  249. };
  250. #endif
  251. static int __devinit isp1760_plat_probe(struct platform_device *pdev)
  252. {
  253. int ret = 0;
  254. struct usb_hcd *hcd;
  255. struct resource *mem_res;
  256. struct resource *irq_res;
  257. resource_size_t mem_size;
  258. unsigned long irqflags = IRQF_SHARED | IRQF_DISABLED;
  259. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  260. if (!mem_res) {
  261. pr_warning("isp1760: Memory resource not available\n");
  262. ret = -ENODEV;
  263. goto out;
  264. }
  265. mem_size = resource_size(mem_res);
  266. if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
  267. pr_warning("isp1760: Cannot reserve the memory resource\n");
  268. ret = -EBUSY;
  269. goto out;
  270. }
  271. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  272. if (!irq_res) {
  273. pr_warning("isp1760: IRQ resource not available\n");
  274. return -ENODEV;
  275. }
  276. irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
  277. hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
  278. irqflags, &pdev->dev, dev_name(&pdev->dev), 0);
  279. if (IS_ERR(hcd)) {
  280. pr_warning("isp1760: Failed to register the HCD device\n");
  281. ret = -ENODEV;
  282. goto cleanup;
  283. }
  284. pr_info("ISP1760 USB device initialised\n");
  285. return ret;
  286. cleanup:
  287. release_mem_region(mem_res->start, mem_size);
  288. out:
  289. return ret;
  290. }
  291. static int __devexit isp1760_plat_remove(struct platform_device *pdev)
  292. {
  293. struct resource *mem_res;
  294. resource_size_t mem_size;
  295. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  296. mem_size = resource_size(mem_res);
  297. release_mem_region(mem_res->start, mem_size);
  298. return 0;
  299. }
  300. static struct platform_driver isp1760_plat_driver = {
  301. .probe = isp1760_plat_probe,
  302. .remove = __devexit_p(isp1760_plat_remove),
  303. .driver = {
  304. .name = "isp1760",
  305. },
  306. };
  307. static int __init isp1760_init(void)
  308. {
  309. int ret, any_ret = -ENODEV;
  310. init_kmem_once();
  311. ret = platform_driver_register(&isp1760_plat_driver);
  312. if (!ret)
  313. any_ret = 0;
  314. #ifdef CONFIG_PPC_OF
  315. ret = of_register_platform_driver(&isp1760_of_driver);
  316. if (!ret)
  317. any_ret = 0;
  318. #endif
  319. #ifdef CONFIG_PCI
  320. ret = pci_register_driver(&isp1761_pci_driver);
  321. if (!ret)
  322. any_ret = 0;
  323. #endif
  324. if (any_ret)
  325. deinit_kmem_cache();
  326. return any_ret;
  327. }
  328. module_init(isp1760_init);
  329. static void __exit isp1760_exit(void)
  330. {
  331. platform_driver_unregister(&isp1760_plat_driver);
  332. #ifdef CONFIG_PPC_OF
  333. of_unregister_platform_driver(&isp1760_of_driver);
  334. #endif
  335. #ifdef CONFIG_PCI
  336. pci_unregister_driver(&isp1761_pci_driver);
  337. #endif
  338. deinit_kmem_cache();
  339. }
  340. module_exit(isp1760_exit);