isp1760-if.c 9.9 KB

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