isp1760-if.c 9.9 KB

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