isp1760-if.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. .of_match_table = of_isp1760_match,
  138. },
  139. .probe = of_isp1760_probe,
  140. .remove = of_isp1760_remove,
  141. };
  142. #endif
  143. #ifdef CONFIG_PCI
  144. static int isp1761_pci_probe(struct pci_dev *dev,
  145. const struct pci_device_id *id)
  146. {
  147. u8 latency, limit;
  148. __u32 reg_data;
  149. int retry_count;
  150. struct usb_hcd *hcd;
  151. unsigned int devflags = 0;
  152. int ret_status = 0;
  153. resource_size_t pci_mem_phy0;
  154. resource_size_t memlength;
  155. u8 __iomem *chip_addr;
  156. u8 __iomem *iobase;
  157. resource_size_t nxp_pci_io_base;
  158. resource_size_t iolength;
  159. if (usb_disabled())
  160. return -ENODEV;
  161. if (pci_enable_device(dev) < 0)
  162. return -ENODEV;
  163. if (!dev->irq)
  164. return -ENODEV;
  165. /* Grab the PLX PCI mem maped port start address we need */
  166. nxp_pci_io_base = pci_resource_start(dev, 0);
  167. iolength = pci_resource_len(dev, 0);
  168. if (!request_mem_region(nxp_pci_io_base, iolength, "ISP1761 IO MEM")) {
  169. printk(KERN_ERR "request region #1\n");
  170. return -EBUSY;
  171. }
  172. iobase = ioremap_nocache(nxp_pci_io_base, iolength);
  173. if (!iobase) {
  174. printk(KERN_ERR "ioremap #1\n");
  175. ret_status = -ENOMEM;
  176. goto cleanup1;
  177. }
  178. /* Grab the PLX PCI shared memory of the ISP 1761 we need */
  179. pci_mem_phy0 = pci_resource_start(dev, 3);
  180. memlength = pci_resource_len(dev, 3);
  181. if (memlength < 0xffff) {
  182. printk(KERN_ERR "memory length for this resource is wrong\n");
  183. ret_status = -ENOMEM;
  184. goto cleanup2;
  185. }
  186. if (!request_mem_region(pci_mem_phy0, memlength, "ISP-PCI")) {
  187. printk(KERN_ERR "host controller already in use\n");
  188. ret_status = -EBUSY;
  189. goto cleanup2;
  190. }
  191. /* map available memory */
  192. chip_addr = ioremap_nocache(pci_mem_phy0,memlength);
  193. if (!chip_addr) {
  194. printk(KERN_ERR "Error ioremap failed\n");
  195. ret_status = -ENOMEM;
  196. goto cleanup3;
  197. }
  198. /* bad pci latencies can contribute to overruns */
  199. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &latency);
  200. if (latency) {
  201. pci_read_config_byte(dev, PCI_MAX_LAT, &limit);
  202. if (limit && limit < latency)
  203. pci_write_config_byte(dev, PCI_LATENCY_TIMER, limit);
  204. }
  205. /* Try to check whether we can access Scratch Register of
  206. * Host Controller or not. The initial PCI access is retried until
  207. * local init for the PCI bridge is completed
  208. */
  209. retry_count = 20;
  210. reg_data = 0;
  211. while ((reg_data != 0xFACE) && retry_count) {
  212. /*by default host is in 16bit mode, so
  213. * io operations at this stage must be 16 bit
  214. * */
  215. writel(0xface, chip_addr + HC_SCRATCH_REG);
  216. udelay(100);
  217. reg_data = readl(chip_addr + HC_SCRATCH_REG) & 0x0000ffff;
  218. retry_count--;
  219. }
  220. iounmap(chip_addr);
  221. /* Host Controller presence is detected by writing to scratch register
  222. * and reading back and checking the contents are same or not
  223. */
  224. if (reg_data != 0xFACE) {
  225. dev_err(&dev->dev, "scratch register mismatch %x\n", reg_data);
  226. ret_status = -ENOMEM;
  227. goto cleanup3;
  228. }
  229. pci_set_master(dev);
  230. /* configure PLX PCI chip to pass interrupts */
  231. #define PLX_INT_CSR_REG 0x68
  232. reg_data = readl(iobase + PLX_INT_CSR_REG);
  233. reg_data |= 0x900;
  234. writel(reg_data, iobase + PLX_INT_CSR_REG);
  235. dev->dev.dma_mask = NULL;
  236. hcd = isp1760_register(pci_mem_phy0, memlength, dev->irq,
  237. IRQF_SHARED, -ENOENT, &dev->dev, dev_name(&dev->dev),
  238. devflags);
  239. if (IS_ERR(hcd)) {
  240. ret_status = -ENODEV;
  241. goto cleanup3;
  242. }
  243. /* done with PLX IO access */
  244. iounmap(iobase);
  245. release_mem_region(nxp_pci_io_base, iolength);
  246. pci_set_drvdata(dev, hcd);
  247. return 0;
  248. cleanup3:
  249. release_mem_region(pci_mem_phy0, memlength);
  250. cleanup2:
  251. iounmap(iobase);
  252. cleanup1:
  253. release_mem_region(nxp_pci_io_base, iolength);
  254. return ret_status;
  255. }
  256. static void isp1761_pci_remove(struct pci_dev *dev)
  257. {
  258. struct usb_hcd *hcd;
  259. hcd = pci_get_drvdata(dev);
  260. usb_remove_hcd(hcd);
  261. iounmap(hcd->regs);
  262. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  263. usb_put_hcd(hcd);
  264. pci_disable_device(dev);
  265. }
  266. static void isp1761_pci_shutdown(struct pci_dev *dev)
  267. {
  268. printk(KERN_ERR "ips1761_pci_shutdown\n");
  269. }
  270. static const struct pci_device_id isp1760_plx [] = {
  271. {
  272. .class = PCI_CLASS_BRIDGE_OTHER << 8,
  273. .class_mask = ~0,
  274. .vendor = PCI_VENDOR_ID_PLX,
  275. .device = 0x5406,
  276. .subvendor = PCI_VENDOR_ID_PLX,
  277. .subdevice = 0x9054,
  278. },
  279. { }
  280. };
  281. MODULE_DEVICE_TABLE(pci, isp1760_plx);
  282. static struct pci_driver isp1761_pci_driver = {
  283. .name = "isp1760",
  284. .id_table = isp1760_plx,
  285. .probe = isp1761_pci_probe,
  286. .remove = isp1761_pci_remove,
  287. .shutdown = isp1761_pci_shutdown,
  288. };
  289. #endif
  290. static int isp1760_plat_probe(struct platform_device *pdev)
  291. {
  292. int ret = 0;
  293. struct usb_hcd *hcd;
  294. struct resource *mem_res;
  295. struct resource *irq_res;
  296. resource_size_t mem_size;
  297. struct isp1760_platform_data *priv = dev_get_platdata(&pdev->dev);
  298. unsigned int devflags = 0;
  299. unsigned long irqflags = IRQF_SHARED;
  300. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  301. if (!mem_res) {
  302. pr_warning("isp1760: Memory resource not available\n");
  303. ret = -ENODEV;
  304. goto out;
  305. }
  306. mem_size = resource_size(mem_res);
  307. if (!request_mem_region(mem_res->start, mem_size, "isp1760")) {
  308. pr_warning("isp1760: Cannot reserve the memory resource\n");
  309. ret = -EBUSY;
  310. goto out;
  311. }
  312. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  313. if (!irq_res) {
  314. pr_warning("isp1760: IRQ resource not available\n");
  315. ret = -ENODEV;
  316. goto cleanup;
  317. }
  318. irqflags |= irq_res->flags & IRQF_TRIGGER_MASK;
  319. if (priv) {
  320. if (priv->is_isp1761)
  321. devflags |= ISP1760_FLAG_ISP1761;
  322. if (priv->bus_width_16)
  323. devflags |= ISP1760_FLAG_BUS_WIDTH_16;
  324. if (priv->port1_otg)
  325. devflags |= ISP1760_FLAG_OTG_EN;
  326. if (priv->analog_oc)
  327. devflags |= ISP1760_FLAG_ANALOG_OC;
  328. if (priv->dack_polarity_high)
  329. devflags |= ISP1760_FLAG_DACK_POL_HIGH;
  330. if (priv->dreq_polarity_high)
  331. devflags |= ISP1760_FLAG_DREQ_POL_HIGH;
  332. }
  333. hcd = isp1760_register(mem_res->start, mem_size, irq_res->start,
  334. irqflags, -ENOENT,
  335. &pdev->dev, dev_name(&pdev->dev), devflags);
  336. platform_set_drvdata(pdev, hcd);
  337. if (IS_ERR(hcd)) {
  338. pr_warning("isp1760: Failed to register the HCD device\n");
  339. ret = -ENODEV;
  340. goto cleanup;
  341. }
  342. pr_info("ISP1760 USB device initialised\n");
  343. return ret;
  344. cleanup:
  345. release_mem_region(mem_res->start, mem_size);
  346. out:
  347. return ret;
  348. }
  349. static int isp1760_plat_remove(struct platform_device *pdev)
  350. {
  351. struct resource *mem_res;
  352. resource_size_t mem_size;
  353. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  354. usb_remove_hcd(hcd);
  355. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  356. mem_size = resource_size(mem_res);
  357. release_mem_region(mem_res->start, mem_size);
  358. usb_put_hcd(hcd);
  359. return 0;
  360. }
  361. static struct platform_driver isp1760_plat_driver = {
  362. .probe = isp1760_plat_probe,
  363. .remove = isp1760_plat_remove,
  364. .driver = {
  365. .name = "isp1760",
  366. },
  367. };
  368. static int __init isp1760_init(void)
  369. {
  370. int ret, any_ret = -ENODEV;
  371. init_kmem_once();
  372. ret = platform_driver_register(&isp1760_plat_driver);
  373. if (!ret)
  374. any_ret = 0;
  375. #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
  376. ret = platform_driver_register(&isp1760_of_driver);
  377. if (!ret)
  378. any_ret = 0;
  379. #endif
  380. #ifdef CONFIG_PCI
  381. ret = pci_register_driver(&isp1761_pci_driver);
  382. if (!ret)
  383. any_ret = 0;
  384. #endif
  385. if (any_ret)
  386. deinit_kmem_cache();
  387. return any_ret;
  388. }
  389. module_init(isp1760_init);
  390. static void __exit isp1760_exit(void)
  391. {
  392. platform_driver_unregister(&isp1760_plat_driver);
  393. #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
  394. platform_driver_unregister(&isp1760_of_driver);
  395. #endif
  396. #ifdef CONFIG_PCI
  397. pci_unregister_driver(&isp1761_pci_driver);
  398. #endif
  399. deinit_kmem_cache();
  400. }
  401. module_exit(isp1760_exit);