isp1760-if.c 9.8 KB

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