fotg210-core.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Central probing code for the FOTG210 dual role driver
  4. * We register one driver for the hardware and then we decide
  5. * whether to proceed with probing the host or the peripheral
  6. * driver.
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/device.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. #include <linux/usb.h>
  17. #include <linux/usb/otg.h>
  18. #include "fotg210.h"
  19. /* Role Register 0x80 */
  20. #define FOTG210_RR 0x80
  21. #define FOTG210_RR_ID BIT(21) /* 1 = B-device, 0 = A-device */
  22. #define FOTG210_RR_CROLE BIT(20) /* 1 = device, 0 = host */
  23. /*
  24. * Gemini-specific initialization function, only executed on the
  25. * Gemini SoC using the global misc control register.
  26. *
  27. * The gemini USB blocks are connected to either Mini-A (host mode) or
  28. * Mini-B (peripheral mode) plugs. There is no role switch support on the
  29. * Gemini SoC, just either-or.
  30. */
  31. #define GEMINI_GLOBAL_MISC_CTRL 0x30
  32. #define GEMINI_MISC_USB0_WAKEUP BIT(14)
  33. #define GEMINI_MISC_USB1_WAKEUP BIT(15)
  34. #define GEMINI_MISC_USB0_VBUS_ON BIT(22)
  35. #define GEMINI_MISC_USB1_VBUS_ON BIT(23)
  36. #define GEMINI_MISC_USB0_MINI_B BIT(29)
  37. #define GEMINI_MISC_USB1_MINI_B BIT(30)
  38. static int fotg210_gemini_init(struct fotg210 *fotg, struct resource *res,
  39. enum usb_dr_mode mode)
  40. {
  41. struct device *dev = fotg->dev;
  42. struct device_node *np = dev->of_node;
  43. struct regmap *map;
  44. bool wakeup;
  45. u32 mask, val;
  46. int ret;
  47. map = syscon_regmap_lookup_by_phandle(np, "syscon");
  48. if (IS_ERR(map))
  49. return dev_err_probe(dev, PTR_ERR(map), "no syscon\n");
  50. fotg->map = map;
  51. wakeup = of_property_read_bool(np, "wakeup-source");
  52. /*
  53. * Figure out if this is USB0 or USB1 by simply checking the
  54. * physical base address.
  55. */
  56. mask = 0;
  57. if (res->start == 0x69000000) {
  58. fotg->port = GEMINI_PORT_1;
  59. mask = GEMINI_MISC_USB1_VBUS_ON | GEMINI_MISC_USB1_MINI_B |
  60. GEMINI_MISC_USB1_WAKEUP;
  61. if (mode == USB_DR_MODE_HOST)
  62. val = GEMINI_MISC_USB1_VBUS_ON;
  63. else
  64. val = GEMINI_MISC_USB1_MINI_B;
  65. if (wakeup)
  66. val |= GEMINI_MISC_USB1_WAKEUP;
  67. } else {
  68. fotg->port = GEMINI_PORT_0;
  69. mask = GEMINI_MISC_USB0_VBUS_ON | GEMINI_MISC_USB0_MINI_B |
  70. GEMINI_MISC_USB0_WAKEUP;
  71. if (mode == USB_DR_MODE_HOST)
  72. val = GEMINI_MISC_USB0_VBUS_ON;
  73. else
  74. val = GEMINI_MISC_USB0_MINI_B;
  75. if (wakeup)
  76. val |= GEMINI_MISC_USB0_WAKEUP;
  77. }
  78. ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, mask, val);
  79. if (ret) {
  80. dev_err(dev, "failed to initialize Gemini PHY\n");
  81. return ret;
  82. }
  83. dev_info(dev, "initialized Gemini PHY in %s mode\n",
  84. (mode == USB_DR_MODE_HOST) ? "host" : "gadget");
  85. return 0;
  86. }
  87. /**
  88. * fotg210_vbus() - Called by gadget driver to enable/disable VBUS
  89. * @fotg: pointer to a private fotg210 object
  90. * @enable: true to enable VBUS, false to disable VBUS
  91. */
  92. void fotg210_vbus(struct fotg210 *fotg, bool enable)
  93. {
  94. u32 mask;
  95. u32 val;
  96. int ret;
  97. switch (fotg->port) {
  98. case GEMINI_PORT_0:
  99. mask = GEMINI_MISC_USB0_VBUS_ON;
  100. val = enable ? GEMINI_MISC_USB0_VBUS_ON : 0;
  101. break;
  102. case GEMINI_PORT_1:
  103. mask = GEMINI_MISC_USB1_VBUS_ON;
  104. val = enable ? GEMINI_MISC_USB1_VBUS_ON : 0;
  105. break;
  106. default:
  107. return;
  108. }
  109. ret = regmap_update_bits(fotg->map, GEMINI_GLOBAL_MISC_CTRL, mask, val);
  110. if (ret)
  111. dev_err(fotg->dev, "failed to %s VBUS\n",
  112. enable ? "enable" : "disable");
  113. dev_info(fotg->dev, "%s: %s VBUS\n", __func__, enable ? "enable" : "disable");
  114. }
  115. static int fotg210_probe(struct platform_device *pdev)
  116. {
  117. struct device *dev = &pdev->dev;
  118. enum usb_dr_mode mode;
  119. struct fotg210 *fotg;
  120. u32 val;
  121. int ret;
  122. fotg = devm_kzalloc(dev, sizeof(*fotg), GFP_KERNEL);
  123. if (!fotg)
  124. return -ENOMEM;
  125. fotg->dev = dev;
  126. fotg->base = devm_platform_get_and_ioremap_resource(pdev, 0, &fotg->res);
  127. if (IS_ERR(fotg->base))
  128. return PTR_ERR(fotg->base);
  129. fotg->pclk = devm_clk_get_optional_enabled(dev, "PCLK");
  130. if (IS_ERR(fotg->pclk))
  131. return PTR_ERR(fotg->pclk);
  132. mode = usb_get_dr_mode(dev);
  133. if (of_device_is_compatible(dev->of_node, "cortina,gemini-usb")) {
  134. ret = fotg210_gemini_init(fotg, fotg->res, mode);
  135. if (ret)
  136. return ret;
  137. }
  138. val = readl(fotg->base + FOTG210_RR);
  139. if (mode == USB_DR_MODE_PERIPHERAL) {
  140. if (!(val & FOTG210_RR_CROLE))
  141. dev_err(dev, "block not in device role\n");
  142. ret = fotg210_udc_probe(pdev, fotg);
  143. } else {
  144. if (val & FOTG210_RR_CROLE)
  145. dev_err(dev, "block not in host role\n");
  146. ret = fotg210_hcd_probe(pdev, fotg);
  147. }
  148. return ret;
  149. }
  150. static void fotg210_remove(struct platform_device *pdev)
  151. {
  152. struct device *dev = &pdev->dev;
  153. enum usb_dr_mode mode;
  154. mode = usb_get_dr_mode(dev);
  155. if (mode == USB_DR_MODE_PERIPHERAL)
  156. fotg210_udc_remove(pdev);
  157. else
  158. fotg210_hcd_remove(pdev);
  159. }
  160. #ifdef CONFIG_OF
  161. static const struct of_device_id fotg210_of_match[] = {
  162. { .compatible = "faraday,fotg200" },
  163. { .compatible = "faraday,fotg210" },
  164. /* TODO: can we also handle FUSB220? */
  165. {},
  166. };
  167. MODULE_DEVICE_TABLE(of, fotg210_of_match);
  168. #endif
  169. static struct platform_driver fotg210_driver = {
  170. .driver = {
  171. .name = "fotg210",
  172. .of_match_table = of_match_ptr(fotg210_of_match),
  173. },
  174. .probe = fotg210_probe,
  175. .remove_new = fotg210_remove,
  176. };
  177. static int __init fotg210_init(void)
  178. {
  179. if (IS_ENABLED(CONFIG_USB_FOTG210_HCD) && !usb_disabled())
  180. fotg210_hcd_init();
  181. return platform_driver_register(&fotg210_driver);
  182. }
  183. module_init(fotg210_init);
  184. static void __exit fotg210_cleanup(void)
  185. {
  186. platform_driver_unregister(&fotg210_driver);
  187. if (IS_ENABLED(CONFIG_USB_FOTG210_HCD))
  188. fotg210_hcd_cleanup();
  189. }
  190. module_exit(fotg210_cleanup);
  191. MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang");
  192. MODULE_LICENSE("GPL");
  193. MODULE_DESCRIPTION("FOTG210 Dual Role Controller Driver");