isp1760-if.c 11 KB

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