isp1760-if.c 11 KB

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