isp1760-if.c 9.8 KB

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