ehci-npcm7xx.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Nuvoton NPCM7xx driver for EHCI HCD
  4. *
  5. * Copyright (C) 2018 Nuvoton Technologies,
  6. * Avi Fishman <avi.fishman@nuvoton.com> <avifishman70@gmail.com>
  7. * Tomer Maimon <tomer.maimon@nuvoton.com> <tmaimon77@gmail.com>
  8. *
  9. * Based on various ehci-spear.c driver
  10. */
  11. #include <linux/dma-mapping.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm.h>
  17. #include <linux/usb.h>
  18. #include <linux/usb/hcd.h>
  19. #include "ehci.h"
  20. #include <linux/regmap.h>
  21. #include <linux/mfd/syscon.h>
  22. #define DRIVER_DESC "EHCI npcm7xx driver"
  23. static const char hcd_name[] = "npcm7xx-ehci";
  24. #define USB2PHYCTL_OFFSET 0x144
  25. #define IPSRST2_OFFSET 0x24
  26. #define IPSRST3_OFFSET 0x34
  27. static struct hc_driver __read_mostly ehci_npcm7xx_hc_driver;
  28. #ifdef CONFIG_PM_SLEEP
  29. static int ehci_npcm7xx_drv_suspend(struct device *dev)
  30. {
  31. struct usb_hcd *hcd = dev_get_drvdata(dev);
  32. bool do_wakeup = device_may_wakeup(dev);
  33. return ehci_suspend(hcd, do_wakeup);
  34. }
  35. static int ehci_npcm7xx_drv_resume(struct device *dev)
  36. {
  37. struct usb_hcd *hcd = dev_get_drvdata(dev);
  38. ehci_resume(hcd, false);
  39. return 0;
  40. }
  41. #endif /* CONFIG_PM_SLEEP */
  42. static SIMPLE_DEV_PM_OPS(ehci_npcm7xx_pm_ops, ehci_npcm7xx_drv_suspend,
  43. ehci_npcm7xx_drv_resume);
  44. static int npcm7xx_ehci_hcd_drv_probe(struct platform_device *pdev)
  45. {
  46. struct usb_hcd *hcd;
  47. struct resource *res;
  48. struct regmap *gcr_regmap;
  49. struct regmap *rst_regmap;
  50. const struct hc_driver *driver = &ehci_npcm7xx_hc_driver;
  51. int irq;
  52. int retval;
  53. dev_dbg(&pdev->dev, "initializing npcm7xx ehci USB Controller\n");
  54. gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr");
  55. if (IS_ERR(gcr_regmap)) {
  56. dev_err(&pdev->dev, "%s: failed to find nuvoton,npcm750-gcr\n",
  57. __func__);
  58. return PTR_ERR(gcr_regmap);
  59. }
  60. rst_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-rst");
  61. if (IS_ERR(rst_regmap)) {
  62. dev_err(&pdev->dev, "%s: failed to find nuvoton,npcm750-rst\n",
  63. __func__);
  64. return PTR_ERR(rst_regmap);
  65. }
  66. /********* phy init ******/
  67. // reset usb host
  68. regmap_update_bits(rst_regmap, IPSRST2_OFFSET,
  69. (0x1 << 26), (0x1 << 26));
  70. regmap_update_bits(rst_regmap, IPSRST3_OFFSET,
  71. (0x1 << 25), (0x1 << 25));
  72. regmap_update_bits(gcr_regmap, USB2PHYCTL_OFFSET,
  73. (0x1 << 28), 0);
  74. udelay(1);
  75. // enable phy
  76. regmap_update_bits(rst_regmap, IPSRST3_OFFSET,
  77. (0x1 << 25), 0);
  78. udelay(50); // enable phy
  79. regmap_update_bits(gcr_regmap, USB2PHYCTL_OFFSET,
  80. (0x1 << 28), (0x1 << 28));
  81. // enable host
  82. regmap_update_bits(rst_regmap, IPSRST2_OFFSET,
  83. (0x1 << 26), 0);
  84. if (usb_disabled())
  85. return -ENODEV;
  86. irq = platform_get_irq(pdev, 0);
  87. if (irq < 0) {
  88. retval = irq;
  89. goto fail;
  90. }
  91. /*
  92. * Right now device-tree probed devices don't get dma_mask set.
  93. * Since shared usb code relies on it, set it here for now.
  94. * Once we have dma capability bindings this can go away.
  95. */
  96. retval = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  97. if (retval)
  98. goto fail;
  99. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  100. if (!hcd) {
  101. retval = -ENOMEM;
  102. goto fail;
  103. }
  104. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  105. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  106. if (IS_ERR(hcd->regs)) {
  107. retval = PTR_ERR(hcd->regs);
  108. goto err_put_hcd;
  109. }
  110. hcd->rsrc_start = res->start;
  111. hcd->rsrc_len = resource_size(res);
  112. /* registers start at offset 0x0 */
  113. hcd_to_ehci(hcd)->caps = hcd->regs;
  114. retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
  115. if (retval)
  116. goto err_put_hcd;
  117. device_wakeup_enable(hcd->self.controller);
  118. return retval;
  119. err_put_hcd:
  120. usb_put_hcd(hcd);
  121. fail:
  122. dev_err(&pdev->dev, "init fail, %d\n", retval);
  123. return retval;
  124. }
  125. static int npcm7xx_ehci_hcd_drv_remove(struct platform_device *pdev)
  126. {
  127. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  128. usb_remove_hcd(hcd);
  129. usb_put_hcd(hcd);
  130. return 0;
  131. }
  132. static const struct of_device_id npcm7xx_ehci_id_table[] = {
  133. { .compatible = "nuvoton,npcm750-ehci" },
  134. { },
  135. };
  136. MODULE_DEVICE_TABLE(of, npcm7xx_ehci_id_table);
  137. static struct platform_driver npcm7xx_ehci_hcd_driver = {
  138. .probe = npcm7xx_ehci_hcd_drv_probe,
  139. .remove = npcm7xx_ehci_hcd_drv_remove,
  140. .shutdown = usb_hcd_platform_shutdown,
  141. .driver = {
  142. .name = "npcm7xx-ehci",
  143. .bus = &platform_bus_type,
  144. .pm = &ehci_npcm7xx_pm_ops,
  145. .of_match_table = npcm7xx_ehci_id_table,
  146. }
  147. };
  148. static int __init ehci_npcm7xx_init(void)
  149. {
  150. if (usb_disabled())
  151. return -ENODEV;
  152. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  153. ehci_init_driver(&ehci_npcm7xx_hc_driver, NULL);
  154. return platform_driver_register(&npcm7xx_ehci_hcd_driver);
  155. }
  156. module_init(ehci_npcm7xx_init);
  157. static void __exit ehci_npcm7xx_cleanup(void)
  158. {
  159. platform_driver_unregister(&npcm7xx_ehci_hcd_driver);
  160. }
  161. module_exit(ehci_npcm7xx_cleanup);
  162. MODULE_DESCRIPTION(DRIVER_DESC);
  163. MODULE_ALIAS("platform:npcm7xx-ehci");
  164. MODULE_AUTHOR("Avi Fishman");
  165. MODULE_LICENSE("GPL v2");