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