isp1760-if.c 11 KB

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