isp1760-if.c 11 KB

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