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. dev_set_drvdata(&dev->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 = dev_get_drvdata(&dev->dev);
  115. dev_set_drvdata(&dev->dev, NULL);
  116. usb_remove_hcd(drvdata->hcd);
  117. iounmap(drvdata->hcd->regs);
  118. release_mem_region(drvdata->hcd->rsrc_start, drvdata->hcd->rsrc_len);
  119. usb_put_hcd(drvdata->hcd);
  120. if (gpio_is_valid(drvdata->rst_gpio))
  121. gpio_free(drvdata->rst_gpio);
  122. kfree(drvdata);
  123. return 0;
  124. }
  125. static const struct of_device_id of_isp1760_match[] = {
  126. {
  127. .compatible = "nxp,usb-isp1760",
  128. },
  129. {
  130. .compatible = "nxp,usb-isp1761",
  131. },
  132. { },
  133. };
  134. MODULE_DEVICE_TABLE(of, of_isp1760_match);
  135. static struct platform_driver isp1760_of_driver = {
  136. .driver = {
  137. .name = "nxp-isp1760",
  138. .owner = THIS_MODULE,
  139. .of_match_table = of_isp1760_match,
  140. },
  141. .probe = of_isp1760_probe,
  142. .remove = of_isp1760_remove,
  143. };
  144. #endif
  145. #ifdef CONFIG_PCI
  146. static int isp1761_pci_probe(struct pci_dev *dev,
  147. const struct pci_device_id *id)
  148. {
  149. u8 latency, limit;
  150. __u32 reg_data;
  151. int retry_count;
  152. struct usb_hcd *hcd;
  153. unsigned int devflags = 0;
  154. int ret_status = 0;
  155. resource_size_t pci_mem_phy0;
  156. resource_size_t memlength;
  157. u8 __iomem *chip_addr;
  158. u8 __iomem *iobase;
  159. resource_size_t nxp_pci_io_base;
  160. resource_size_t iolength;
  161. if (usb_disabled())
  162. return -ENODEV;
  163. if (pci_enable_device(dev) < 0)
  164. return -ENODEV;
  165. if (!dev->irq)
  166. return -ENODEV;
  167. /* Grab the PLX PCI mem maped port start address we need */
  168. nxp_pci_io_base = pci_resource_start(dev, 0);
  169. iolength = pci_resource_len(dev, 0);
  170. if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
  171. printk(KERN_ERR "request region #1\n");
  172. return -EBUSY;
  173. }
  174. iobase = ioremap_nocache(nxp_pci_io_base, iolength);
  175. if (!iobase) {
  176. printk(KERN_ERR "ioremap #1\n");
  177. ret_status = -ENOMEM;
  178. goto cleanup1;
  179. }
  180. /* Grab the PLX PCI shared memory of the ISP 1761 we need */
  181. pci_mem_phy0 = pci_resource_start(dev, 3);
  182. memlength = pci_resource_len(dev, 3);
  183. if (memlength < 0xffff) {
  184. printk(KERN_ERR "memory length for this resource is wrong\n");
  185. ret_status = -ENOMEM;
  186. goto cleanup2;
  187. }
  188. if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
  189. printk(KERN_ERR "host controller already in use\n");
  190. ret_status = -EBUSY;
  191. goto cleanup2;
  192. }
  193. /* map available memory */
  194. chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
  195. if (!chip_addr) {
  196. printk(KERN_ERR "Error ioremap failed\n");
  197. ret_status = -ENOMEM;
  198. goto cleanup3;
  199. }
  200. /* bad pci latencies can contribute to overruns */
  201. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
  202. if (latency) {
  203. pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
  204. if (limit && limit < latency)
  205. pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
  206. }
  207. /* Try to check whether we can access Scratch Register of
  208. * Host Controller or not. The initial PCI access is retried until
  209. * local init for the PCI bridge is completed
  210. */
  211. retry_count = 20;
  212. reg_data = 0;
  213. while ((reg_data != 0xFACE) && retry_count) {
  214. /*by default host is in 16bit mode, so
  215. * io operations at this stage must be 16 bit
  216. * */
  217. writel(0xface, chip_addr + HC_SCRATCH_REG);
  218. udelay(100);
  219. reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
  220. retry_count--;
  221. }
  222. iounmap(chip_addr);
  223. /* Host Controller presence is detected by writing to scratch register
  224. * and reading back and checking the contents are same or not
  225. */
  226. if (reg_data != 0xFACE) {
  227. dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
  228. ret_status = -ENOMEM;
  229. goto cleanup3;
  230. }
  231. pci_set_master(dev);
  232. /* configure PLX PCI chip to pass interrupts */
  233. #define PLX_INT_CSR_REG 0x68
  234. reg_data = readl(iobase + PLX_INT_CSR_REG);
  235. reg_data |= 0x900;
  236. writel(reg_data, iobase + PLX_INT_CSR_REG);
  237. dev->dev.dma_mask = NULL;
  238. hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
  239. IRQF_SHARED, -ENOENT, &dev->dev, dev_name(&dev->dev),
  240. devflags);
  241. if (IS_ERR(hcd)) {
  242. ret_status = -ENODEV;
  243. goto cleanup3;
  244. }
  245. /* done with PLX IO access */
  246. iounmap(iobase);
  247. release_mem_region(nxp_pci_io_base, iolength);
  248. pci_set_drvdata(dev, hcd);
  249. return 0;
  250. cleanup3:
  251. release_mem_region(pci_mem_phy0, memlength);
  252. cleanup2:
  253. iounmap(iobase);
  254. cleanup1:
  255. release_mem_region(nxp_pci_io_base, iolength);
  256. return ret_status;
  257. }
  258. static void isp1761_pci_remove(struct pci_dev *dev)
  259. {
  260. struct usb_hcd *hcd;
  261. hcd = pci_get_drvdata(dev);
  262. usb_remove_hcd(hcd);
  263. iounmap(hcd->regs);
  264. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  265. usb_put_hcd(hcd);
  266. pci_disable_device(dev);
  267. }
  268. static void isp1761_pci_shutdown(struct pci_dev *dev)
  269. {
  270. printk(KERN_ERR "ips1761_pci_shutdown\n");
  271. }
  272. static const struct pci_device_id isp1760_plx [] = {
  273. {
  274. .class = PCI_CLASS_BRIDGE_OTHER << 8,
  275. .class_mask = ~0,
  276. .vendor = PCI_VENDOR_ID_PLX,
  277. .device = 0x5406,
  278. .subvendor = PCI_VENDOR_ID_PLX,
  279. .subdevice = 0x9054,
  280. },
  281. { }
  282. };
  283. MODULE_DEVICE_TABLE(pci, isp1760_plx);
  284. static struct pci_driver isp1761_pci_driver = {
  285. .name = "isp1760",
  286. .id_table = isp1760_plx,
  287. .probe = isp1761_pci_probe,
  288. .remove = isp1761_pci_remove,
  289. .shutdown = isp1761_pci_shutdown,
  290. };
  291. #endif
  292. static int isp1760_plat_probe(struct platform_device *pdev)
  293. {
  294. int ret = 0;
  295. struct usb_hcd *hcd;
  296. struct resource *mem_res;
  297. struct resource *irq_res;
  298. resource_size_t mem_size;
  299. struct isp1760_platform_data *priv = pdev->dev.platform_data;
  300. unsigned int devflags = 0;
  301. unsigned long irqflags = IRQF_SHARED;
  302. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  303. if (!mem_res) {
  304. pr_warning("isp1760: Memory resource not available\n");
  305. ret = -ENODEV;
  306. goto out;
  307. }
  308. mem_size = resource_size(mem_res);
  309. if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
  310. pr_warning("isp1760: Cannot reserve the memory resource\n");
  311. ret = -EBUSY;
  312. goto out;
  313. }
  314. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  315. if (!irq_res) {
  316. pr_warning("isp1760: IRQ resource not available\n");
  317. return -ENODEV;
  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. dev_set_drvdata(&pdev->dev, 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 = dev_get_drvdata(&pdev->dev);
  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);