dwc3-meson-g12a.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB Glue for Amlogic G12A SoCs
  4. *
  5. * Copyright (c) 2019 BayLibre, SAS
  6. * Author: Neil Armstrong <[email protected]>
  7. */
  8. /*
  9. * The USB is organized with a glue around the DWC3 Controller IP as :
  10. * - Control registers for each USB2 Ports
  11. * - Control registers for the USB PHY layer
  12. * - SuperSpeed PHY can be enabled only if port is used
  13. * - Dynamic OTG switching with ID change interrupt
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/clk.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/regmap.h>
  23. #include <linux/bitfield.h>
  24. #include <linux/bitops.h>
  25. #include <linux/reset.h>
  26. #include <linux/phy/phy.h>
  27. #include <linux/usb/otg.h>
  28. #include <linux/usb/role.h>
  29. #include <linux/regulator/consumer.h>
  30. /* USB2 Ports Control Registers, offsets are per-port */
  31. #define U2P_REG_SIZE 0x20
  32. #define U2P_R0 0x0
  33. #define U2P_R0_HOST_DEVICE BIT(0)
  34. #define U2P_R0_POWER_OK BIT(1)
  35. #define U2P_R0_HAST_MODE BIT(2)
  36. #define U2P_R0_POWER_ON_RESET BIT(3)
  37. #define U2P_R0_ID_PULLUP BIT(4)
  38. #define U2P_R0_DRV_VBUS BIT(5)
  39. #define U2P_R1 0x4
  40. #define U2P_R1_PHY_READY BIT(0)
  41. #define U2P_R1_ID_DIG BIT(1)
  42. #define U2P_R1_OTG_SESSION_VALID BIT(2)
  43. #define U2P_R1_VBUS_VALID BIT(3)
  44. /* USB Glue Control Registers */
  45. #define G12A_GLUE_OFFSET 0x80
  46. #define USB_R0 0x00
  47. #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17)
  48. #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18)
  49. #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19)
  50. #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29)
  51. #define USB_R0_U2D_ACT BIT(31)
  52. #define USB_R1 0x04
  53. #define USB_R1_U3H_BIGENDIAN_GS BIT(0)
  54. #define USB_R1_U3H_PME_ENABLE BIT(1)
  55. #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(4, 2)
  56. #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(9, 7)
  57. #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(13, 12)
  58. #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16)
  59. #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17)
  60. #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18)
  61. #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19)
  62. #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25)
  63. #define USB_R2 0x08
  64. #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20)
  65. #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26)
  66. #define USB_R3 0x0c
  67. #define USB_R3_P30_SSC_ENABLE BIT(0)
  68. #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1)
  69. #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4)
  70. #define USB_R3_P30_REF_SSP_EN BIT(13)
  71. #define USB_R4 0x10
  72. #define USB_R4_P21_PORT_RESET_0 BIT(0)
  73. #define USB_R4_P21_SLEEP_M0 BIT(1)
  74. #define USB_R4_MEM_PD_MASK GENMASK(3, 2)
  75. #define USB_R4_P21_ONLY BIT(4)
  76. #define USB_R5 0x14
  77. #define USB_R5_ID_DIG_SYNC BIT(0)
  78. #define USB_R5_ID_DIG_REG BIT(1)
  79. #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2)
  80. #define USB_R5_ID_DIG_EN_0 BIT(4)
  81. #define USB_R5_ID_DIG_EN_1 BIT(5)
  82. #define USB_R5_ID_DIG_CURR BIT(6)
  83. #define USB_R5_ID_DIG_IRQ BIT(7)
  84. #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8)
  85. #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16)
  86. #define PHY_COUNT 3
  87. #define USB2_OTG_PHY 1
  88. static struct clk_bulk_data meson_gxl_clocks[] = {
  89. { .id = "usb_ctrl" },
  90. { .id = "ddr" },
  91. };
  92. static struct clk_bulk_data meson_g12a_clocks[] = {
  93. { .id = NULL },
  94. };
  95. static struct clk_bulk_data meson_a1_clocks[] = {
  96. { .id = "usb_ctrl" },
  97. { .id = "usb_bus" },
  98. { .id = "xtal_usb_ctrl" },
  99. };
  100. static const char * const meson_gxm_phy_names[] = {
  101. "usb2-phy0", "usb2-phy1", "usb2-phy2",
  102. };
  103. static const char * const meson_g12a_phy_names[] = {
  104. "usb2-phy0", "usb2-phy1", "usb3-phy0",
  105. };
  106. /*
  107. * Amlogic A1 has a single physical PHY, in slot 1, but still has the
  108. * two U2 PHY controls register blocks like G12A.
  109. * AXG has the similar scheme, thus needs the same tweak.
  110. * Handling the first PHY on slot 1 would need a large amount of code
  111. * changes, and the current management is generic enough to handle it
  112. * correctly when only the "usb2-phy1" phy is specified on-par with the
  113. * DT bindings.
  114. */
  115. static const char * const meson_a1_phy_names[] = {
  116. "usb2-phy0", "usb2-phy1"
  117. };
  118. struct dwc3_meson_g12a;
  119. struct dwc3_meson_g12a_drvdata {
  120. bool otg_switch_supported;
  121. bool otg_phy_host_port_disable;
  122. struct clk_bulk_data *clks;
  123. int num_clks;
  124. const char * const *phy_names;
  125. int num_phys;
  126. int (*setup_regmaps)(struct dwc3_meson_g12a *priv, void __iomem *base);
  127. int (*usb2_init_phy)(struct dwc3_meson_g12a *priv, int i,
  128. enum phy_mode mode);
  129. int (*set_phy_mode)(struct dwc3_meson_g12a *priv, int i,
  130. enum phy_mode mode);
  131. int (*usb_init)(struct dwc3_meson_g12a *priv);
  132. int (*usb_post_init)(struct dwc3_meson_g12a *priv);
  133. };
  134. static int dwc3_meson_gxl_setup_regmaps(struct dwc3_meson_g12a *priv,
  135. void __iomem *base);
  136. static int dwc3_meson_g12a_setup_regmaps(struct dwc3_meson_g12a *priv,
  137. void __iomem *base);
  138. static int dwc3_meson_g12a_usb2_init_phy(struct dwc3_meson_g12a *priv, int i,
  139. enum phy_mode mode);
  140. static int dwc3_meson_gxl_usb2_init_phy(struct dwc3_meson_g12a *priv, int i,
  141. enum phy_mode mode);
  142. static int dwc3_meson_g12a_set_phy_mode(struct dwc3_meson_g12a *priv,
  143. int i, enum phy_mode mode);
  144. static int dwc3_meson_gxl_set_phy_mode(struct dwc3_meson_g12a *priv,
  145. int i, enum phy_mode mode);
  146. static int dwc3_meson_g12a_usb_init(struct dwc3_meson_g12a *priv);
  147. static int dwc3_meson_gxl_usb_init(struct dwc3_meson_g12a *priv);
  148. static int dwc3_meson_gxl_usb_post_init(struct dwc3_meson_g12a *priv);
  149. /*
  150. * For GXL and GXM SoCs:
  151. * USB Phy muxing between the DWC2 Device controller and the DWC3 Host
  152. * controller is buggy when switching from Device to Host when USB port
  153. * is unpopulated, it causes the DWC3 to hard crash.
  154. * When populated (including OTG switching with ID pin), the switch works
  155. * like a charm like on the G12A platforms.
  156. * In order to still switch from Host to Device on an USB Type-A port,
  157. * an U2_PORT_DISABLE bit has been added to disconnect the DWC3 Host
  158. * controller from the port, but when used the DWC3 controller must be
  159. * reset to recover usage of the port.
  160. */
  161. static const struct dwc3_meson_g12a_drvdata gxl_drvdata = {
  162. .otg_switch_supported = true,
  163. .otg_phy_host_port_disable = true,
  164. .clks = meson_gxl_clocks,
  165. .num_clks = ARRAY_SIZE(meson_g12a_clocks),
  166. .phy_names = meson_a1_phy_names,
  167. .num_phys = ARRAY_SIZE(meson_a1_phy_names),
  168. .setup_regmaps = dwc3_meson_gxl_setup_regmaps,
  169. .usb2_init_phy = dwc3_meson_gxl_usb2_init_phy,
  170. .set_phy_mode = dwc3_meson_gxl_set_phy_mode,
  171. .usb_init = dwc3_meson_gxl_usb_init,
  172. .usb_post_init = dwc3_meson_gxl_usb_post_init,
  173. };
  174. static const struct dwc3_meson_g12a_drvdata gxm_drvdata = {
  175. .otg_switch_supported = true,
  176. .otg_phy_host_port_disable = true,
  177. .clks = meson_gxl_clocks,
  178. .num_clks = ARRAY_SIZE(meson_g12a_clocks),
  179. .phy_names = meson_gxm_phy_names,
  180. .num_phys = ARRAY_SIZE(meson_gxm_phy_names),
  181. .setup_regmaps = dwc3_meson_gxl_setup_regmaps,
  182. .usb2_init_phy = dwc3_meson_gxl_usb2_init_phy,
  183. .set_phy_mode = dwc3_meson_gxl_set_phy_mode,
  184. .usb_init = dwc3_meson_gxl_usb_init,
  185. .usb_post_init = dwc3_meson_gxl_usb_post_init,
  186. };
  187. static const struct dwc3_meson_g12a_drvdata axg_drvdata = {
  188. .otg_switch_supported = true,
  189. .clks = meson_gxl_clocks,
  190. .num_clks = ARRAY_SIZE(meson_gxl_clocks),
  191. .phy_names = meson_a1_phy_names,
  192. .num_phys = ARRAY_SIZE(meson_a1_phy_names),
  193. .setup_regmaps = dwc3_meson_gxl_setup_regmaps,
  194. .usb2_init_phy = dwc3_meson_gxl_usb2_init_phy,
  195. .set_phy_mode = dwc3_meson_gxl_set_phy_mode,
  196. .usb_init = dwc3_meson_g12a_usb_init,
  197. .usb_post_init = dwc3_meson_gxl_usb_post_init,
  198. };
  199. static const struct dwc3_meson_g12a_drvdata g12a_drvdata = {
  200. .otg_switch_supported = true,
  201. .clks = meson_g12a_clocks,
  202. .num_clks = ARRAY_SIZE(meson_g12a_clocks),
  203. .phy_names = meson_g12a_phy_names,
  204. .num_phys = ARRAY_SIZE(meson_g12a_phy_names),
  205. .setup_regmaps = dwc3_meson_g12a_setup_regmaps,
  206. .usb2_init_phy = dwc3_meson_g12a_usb2_init_phy,
  207. .set_phy_mode = dwc3_meson_g12a_set_phy_mode,
  208. .usb_init = dwc3_meson_g12a_usb_init,
  209. };
  210. static const struct dwc3_meson_g12a_drvdata a1_drvdata = {
  211. .otg_switch_supported = false,
  212. .clks = meson_a1_clocks,
  213. .num_clks = ARRAY_SIZE(meson_a1_clocks),
  214. .phy_names = meson_a1_phy_names,
  215. .num_phys = ARRAY_SIZE(meson_a1_phy_names),
  216. .setup_regmaps = dwc3_meson_g12a_setup_regmaps,
  217. .usb2_init_phy = dwc3_meson_g12a_usb2_init_phy,
  218. .set_phy_mode = dwc3_meson_g12a_set_phy_mode,
  219. .usb_init = dwc3_meson_g12a_usb_init,
  220. };
  221. struct dwc3_meson_g12a {
  222. struct device *dev;
  223. struct regmap *u2p_regmap[PHY_COUNT];
  224. struct regmap *usb_glue_regmap;
  225. struct reset_control *reset;
  226. struct phy *phys[PHY_COUNT];
  227. enum usb_dr_mode otg_mode;
  228. enum phy_mode otg_phy_mode;
  229. unsigned int usb2_ports;
  230. unsigned int usb3_ports;
  231. struct regulator *vbus;
  232. struct usb_role_switch_desc switch_desc;
  233. struct usb_role_switch *role_switch;
  234. const struct dwc3_meson_g12a_drvdata *drvdata;
  235. };
  236. static int dwc3_meson_gxl_set_phy_mode(struct dwc3_meson_g12a *priv,
  237. int i, enum phy_mode mode)
  238. {
  239. return phy_set_mode(priv->phys[i], mode);
  240. }
  241. static int dwc3_meson_gxl_usb2_init_phy(struct dwc3_meson_g12a *priv, int i,
  242. enum phy_mode mode)
  243. {
  244. /* On GXL PHY must be started in device mode for DWC2 init */
  245. return priv->drvdata->set_phy_mode(priv, i,
  246. (i == USB2_OTG_PHY) ? PHY_MODE_USB_DEVICE
  247. : PHY_MODE_USB_HOST);
  248. }
  249. static int dwc3_meson_g12a_set_phy_mode(struct dwc3_meson_g12a *priv,
  250. int i, enum phy_mode mode)
  251. {
  252. if (mode == PHY_MODE_USB_HOST)
  253. regmap_update_bits(priv->u2p_regmap[i], U2P_R0,
  254. U2P_R0_HOST_DEVICE,
  255. U2P_R0_HOST_DEVICE);
  256. else
  257. regmap_update_bits(priv->u2p_regmap[i], U2P_R0,
  258. U2P_R0_HOST_DEVICE, 0);
  259. return 0;
  260. }
  261. static int dwc3_meson_g12a_usb2_init_phy(struct dwc3_meson_g12a *priv, int i,
  262. enum phy_mode mode)
  263. {
  264. int ret;
  265. regmap_update_bits(priv->u2p_regmap[i], U2P_R0,
  266. U2P_R0_POWER_ON_RESET,
  267. U2P_R0_POWER_ON_RESET);
  268. if (priv->drvdata->otg_switch_supported && i == USB2_OTG_PHY) {
  269. regmap_update_bits(priv->u2p_regmap[i], U2P_R0,
  270. U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS,
  271. U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS);
  272. ret = priv->drvdata->set_phy_mode(priv, i, mode);
  273. } else
  274. ret = priv->drvdata->set_phy_mode(priv, i,
  275. PHY_MODE_USB_HOST);
  276. if (ret)
  277. return ret;
  278. regmap_update_bits(priv->u2p_regmap[i], U2P_R0,
  279. U2P_R0_POWER_ON_RESET, 0);
  280. return 0;
  281. }
  282. static int dwc3_meson_g12a_usb2_init(struct dwc3_meson_g12a *priv,
  283. enum phy_mode mode)
  284. {
  285. int i, ret;
  286. for (i = 0; i < priv->drvdata->num_phys; ++i) {
  287. if (!priv->phys[i])
  288. continue;
  289. if (!strstr(priv->drvdata->phy_names[i], "usb2"))
  290. continue;
  291. ret = priv->drvdata->usb2_init_phy(priv, i, mode);
  292. if (ret)
  293. return ret;
  294. }
  295. return 0;
  296. }
  297. static void dwc3_meson_g12a_usb3_init(struct dwc3_meson_g12a *priv)
  298. {
  299. regmap_update_bits(priv->usb_glue_regmap, USB_R3,
  300. USB_R3_P30_SSC_RANGE_MASK |
  301. USB_R3_P30_REF_SSP_EN,
  302. USB_R3_P30_SSC_ENABLE |
  303. FIELD_PREP(USB_R3_P30_SSC_RANGE_MASK, 2) |
  304. USB_R3_P30_REF_SSP_EN);
  305. udelay(2);
  306. regmap_update_bits(priv->usb_glue_regmap, USB_R2,
  307. USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK,
  308. FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK, 0x15));
  309. regmap_update_bits(priv->usb_glue_regmap, USB_R2,
  310. USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK,
  311. FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK, 0x20));
  312. udelay(2);
  313. regmap_update_bits(priv->usb_glue_regmap, USB_R1,
  314. USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT,
  315. USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT);
  316. regmap_update_bits(priv->usb_glue_regmap, USB_R1,
  317. USB_R1_P30_PCS_TX_SWING_FULL_MASK,
  318. FIELD_PREP(USB_R1_P30_PCS_TX_SWING_FULL_MASK, 127));
  319. }
  320. static void dwc3_meson_g12a_usb_otg_apply_mode(struct dwc3_meson_g12a *priv,
  321. enum phy_mode mode)
  322. {
  323. if (mode == PHY_MODE_USB_DEVICE) {
  324. if (priv->otg_mode != USB_DR_MODE_OTG &&
  325. priv->drvdata->otg_phy_host_port_disable)
  326. /* Isolate the OTG PHY port from the Host Controller */
  327. regmap_update_bits(priv->usb_glue_regmap, USB_R1,
  328. USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK,
  329. FIELD_PREP(USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK,
  330. BIT(USB2_OTG_PHY)));
  331. regmap_update_bits(priv->usb_glue_regmap, USB_R0,
  332. USB_R0_U2D_ACT, USB_R0_U2D_ACT);
  333. regmap_update_bits(priv->usb_glue_regmap, USB_R0,
  334. USB_R0_U2D_SS_SCALEDOWN_MODE_MASK, 0);
  335. regmap_update_bits(priv->usb_glue_regmap, USB_R4,
  336. USB_R4_P21_SLEEP_M0, USB_R4_P21_SLEEP_M0);
  337. } else {
  338. if (priv->otg_mode != USB_DR_MODE_OTG &&
  339. priv->drvdata->otg_phy_host_port_disable) {
  340. regmap_update_bits(priv->usb_glue_regmap, USB_R1,
  341. USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK, 0);
  342. msleep(500);
  343. }
  344. regmap_update_bits(priv->usb_glue_regmap, USB_R0,
  345. USB_R0_U2D_ACT, 0);
  346. regmap_update_bits(priv->usb_glue_regmap, USB_R4,
  347. USB_R4_P21_SLEEP_M0, 0);
  348. }
  349. }
  350. static int dwc3_meson_g12a_usb_init_glue(struct dwc3_meson_g12a *priv,
  351. enum phy_mode mode)
  352. {
  353. int ret;
  354. ret = dwc3_meson_g12a_usb2_init(priv, mode);
  355. if (ret)
  356. return ret;
  357. regmap_update_bits(priv->usb_glue_regmap, USB_R1,
  358. USB_R1_U3H_FLADJ_30MHZ_REG_MASK,
  359. FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 0x20));
  360. regmap_update_bits(priv->usb_glue_regmap, USB_R5,
  361. USB_R5_ID_DIG_EN_0,
  362. USB_R5_ID_DIG_EN_0);
  363. regmap_update_bits(priv->usb_glue_regmap, USB_R5,
  364. USB_R5_ID_DIG_EN_1,
  365. USB_R5_ID_DIG_EN_1);
  366. regmap_update_bits(priv->usb_glue_regmap, USB_R5,
  367. USB_R5_ID_DIG_TH_MASK,
  368. FIELD_PREP(USB_R5_ID_DIG_TH_MASK, 0xff));
  369. /* If we have an actual SuperSpeed port, initialize it */
  370. if (priv->usb3_ports)
  371. dwc3_meson_g12a_usb3_init(priv);
  372. dwc3_meson_g12a_usb_otg_apply_mode(priv, mode);
  373. return 0;
  374. }
  375. static const struct regmap_config phy_meson_g12a_usb_glue_regmap_conf = {
  376. .name = "usb-glue",
  377. .reg_bits = 8,
  378. .val_bits = 32,
  379. .reg_stride = 4,
  380. .max_register = USB_R5,
  381. };
  382. static int dwc3_meson_g12a_get_phys(struct dwc3_meson_g12a *priv)
  383. {
  384. const char *phy_name;
  385. int i;
  386. for (i = 0 ; i < priv->drvdata->num_phys ; ++i) {
  387. phy_name = priv->drvdata->phy_names[i];
  388. priv->phys[i] = devm_phy_optional_get(priv->dev, phy_name);
  389. if (!priv->phys[i])
  390. continue;
  391. if (IS_ERR(priv->phys[i]))
  392. return PTR_ERR(priv->phys[i]);
  393. if (strstr(phy_name, "usb3"))
  394. priv->usb3_ports++;
  395. else
  396. priv->usb2_ports++;
  397. }
  398. dev_info(priv->dev, "USB2 ports: %d\n", priv->usb2_ports);
  399. dev_info(priv->dev, "USB3 ports: %d\n", priv->usb3_ports);
  400. return 0;
  401. }
  402. static enum phy_mode dwc3_meson_g12a_get_id(struct dwc3_meson_g12a *priv)
  403. {
  404. u32 reg;
  405. regmap_read(priv->usb_glue_regmap, USB_R5, &reg);
  406. if (reg & (USB_R5_ID_DIG_SYNC | USB_R5_ID_DIG_REG))
  407. return PHY_MODE_USB_DEVICE;
  408. return PHY_MODE_USB_HOST;
  409. }
  410. static int dwc3_meson_g12a_otg_mode_set(struct dwc3_meson_g12a *priv,
  411. enum phy_mode mode)
  412. {
  413. int ret;
  414. if (!priv->drvdata->otg_switch_supported || !priv->phys[USB2_OTG_PHY])
  415. return -EINVAL;
  416. if (mode == PHY_MODE_USB_HOST)
  417. dev_info(priv->dev, "switching to Host Mode\n");
  418. else
  419. dev_info(priv->dev, "switching to Device Mode\n");
  420. if (priv->vbus) {
  421. if (mode == PHY_MODE_USB_DEVICE)
  422. ret = regulator_disable(priv->vbus);
  423. else
  424. ret = regulator_enable(priv->vbus);
  425. if (ret)
  426. return ret;
  427. }
  428. priv->otg_phy_mode = mode;
  429. ret = priv->drvdata->set_phy_mode(priv, USB2_OTG_PHY, mode);
  430. if (ret)
  431. return ret;
  432. dwc3_meson_g12a_usb_otg_apply_mode(priv, mode);
  433. return 0;
  434. }
  435. static int dwc3_meson_g12a_role_set(struct usb_role_switch *sw,
  436. enum usb_role role)
  437. {
  438. struct dwc3_meson_g12a *priv = usb_role_switch_get_drvdata(sw);
  439. enum phy_mode mode;
  440. if (role == USB_ROLE_NONE)
  441. return 0;
  442. mode = (role == USB_ROLE_HOST) ? PHY_MODE_USB_HOST
  443. : PHY_MODE_USB_DEVICE;
  444. if (mode == priv->otg_phy_mode)
  445. return 0;
  446. if (priv->drvdata->otg_phy_host_port_disable)
  447. dev_warn_once(priv->dev, "Broken manual OTG switch\n");
  448. return dwc3_meson_g12a_otg_mode_set(priv, mode);
  449. }
  450. static enum usb_role dwc3_meson_g12a_role_get(struct usb_role_switch *sw)
  451. {
  452. struct dwc3_meson_g12a *priv = usb_role_switch_get_drvdata(sw);
  453. return priv->otg_phy_mode == PHY_MODE_USB_HOST ?
  454. USB_ROLE_HOST : USB_ROLE_DEVICE;
  455. }
  456. static irqreturn_t dwc3_meson_g12a_irq_thread(int irq, void *data)
  457. {
  458. struct dwc3_meson_g12a *priv = data;
  459. enum phy_mode otg_id;
  460. otg_id = dwc3_meson_g12a_get_id(priv);
  461. if (otg_id != priv->otg_phy_mode) {
  462. if (dwc3_meson_g12a_otg_mode_set(priv, otg_id))
  463. dev_warn(priv->dev, "Failed to switch OTG mode\n");
  464. }
  465. regmap_update_bits(priv->usb_glue_regmap, USB_R5,
  466. USB_R5_ID_DIG_IRQ, 0);
  467. return IRQ_HANDLED;
  468. }
  469. static struct device *dwc3_meson_g12_find_child(struct device *dev,
  470. const char *compatible)
  471. {
  472. struct platform_device *pdev;
  473. struct device_node *np;
  474. np = of_get_compatible_child(dev->of_node, compatible);
  475. if (!np)
  476. return NULL;
  477. pdev = of_find_device_by_node(np);
  478. of_node_put(np);
  479. if (!pdev)
  480. return NULL;
  481. return &pdev->dev;
  482. }
  483. static int dwc3_meson_g12a_otg_init(struct platform_device *pdev,
  484. struct dwc3_meson_g12a *priv)
  485. {
  486. enum phy_mode otg_id;
  487. int ret, irq;
  488. struct device *dev = &pdev->dev;
  489. if (!priv->drvdata->otg_switch_supported)
  490. return 0;
  491. if (priv->otg_mode == USB_DR_MODE_OTG) {
  492. /* Ack irq before registering */
  493. regmap_update_bits(priv->usb_glue_regmap, USB_R5,
  494. USB_R5_ID_DIG_IRQ, 0);
  495. irq = platform_get_irq(pdev, 0);
  496. if (irq < 0)
  497. return irq;
  498. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  499. dwc3_meson_g12a_irq_thread,
  500. IRQF_ONESHOT, pdev->name, priv);
  501. if (ret)
  502. return ret;
  503. }
  504. /* Setup OTG mode corresponding to the ID pin */
  505. if (priv->otg_mode == USB_DR_MODE_OTG) {
  506. otg_id = dwc3_meson_g12a_get_id(priv);
  507. if (otg_id != priv->otg_phy_mode) {
  508. if (dwc3_meson_g12a_otg_mode_set(priv, otg_id))
  509. dev_warn(dev, "Failed to switch OTG mode\n");
  510. }
  511. }
  512. /* Setup role switcher */
  513. priv->switch_desc.usb2_port = dwc3_meson_g12_find_child(dev,
  514. "snps,dwc3");
  515. priv->switch_desc.udc = dwc3_meson_g12_find_child(dev, "snps,dwc2");
  516. priv->switch_desc.allow_userspace_control = true;
  517. priv->switch_desc.set = dwc3_meson_g12a_role_set;
  518. priv->switch_desc.get = dwc3_meson_g12a_role_get;
  519. priv->switch_desc.driver_data = priv;
  520. priv->role_switch = usb_role_switch_register(dev, &priv->switch_desc);
  521. if (IS_ERR(priv->role_switch))
  522. dev_warn(dev, "Unable to register Role Switch\n");
  523. return 0;
  524. }
  525. static int dwc3_meson_gxl_setup_regmaps(struct dwc3_meson_g12a *priv,
  526. void __iomem *base)
  527. {
  528. /* GXL controls the PHY mode in the PHY registers unlike G12A */
  529. priv->usb_glue_regmap = devm_regmap_init_mmio(priv->dev, base,
  530. &phy_meson_g12a_usb_glue_regmap_conf);
  531. return PTR_ERR_OR_ZERO(priv->usb_glue_regmap);
  532. }
  533. static int dwc3_meson_g12a_setup_regmaps(struct dwc3_meson_g12a *priv,
  534. void __iomem *base)
  535. {
  536. int i;
  537. priv->usb_glue_regmap = devm_regmap_init_mmio(priv->dev,
  538. base + G12A_GLUE_OFFSET,
  539. &phy_meson_g12a_usb_glue_regmap_conf);
  540. if (IS_ERR(priv->usb_glue_regmap))
  541. return PTR_ERR(priv->usb_glue_regmap);
  542. /* Create a regmap for each USB2 PHY control register set */
  543. for (i = 0; i < priv->drvdata->num_phys; i++) {
  544. struct regmap_config u2p_regmap_config = {
  545. .reg_bits = 8,
  546. .val_bits = 32,
  547. .reg_stride = 4,
  548. .max_register = U2P_R1,
  549. };
  550. if (!strstr(priv->drvdata->phy_names[i], "usb2"))
  551. continue;
  552. u2p_regmap_config.name = devm_kasprintf(priv->dev, GFP_KERNEL,
  553. "u2p-%d", i);
  554. if (!u2p_regmap_config.name)
  555. return -ENOMEM;
  556. priv->u2p_regmap[i] = devm_regmap_init_mmio(priv->dev,
  557. base + (i * U2P_REG_SIZE),
  558. &u2p_regmap_config);
  559. if (IS_ERR(priv->u2p_regmap[i]))
  560. return PTR_ERR(priv->u2p_regmap[i]);
  561. }
  562. return 0;
  563. }
  564. static int dwc3_meson_g12a_usb_init(struct dwc3_meson_g12a *priv)
  565. {
  566. return dwc3_meson_g12a_usb_init_glue(priv, priv->otg_phy_mode);
  567. }
  568. static int dwc3_meson_gxl_usb_init(struct dwc3_meson_g12a *priv)
  569. {
  570. return dwc3_meson_g12a_usb_init_glue(priv, PHY_MODE_USB_DEVICE);
  571. }
  572. static int dwc3_meson_gxl_usb_post_init(struct dwc3_meson_g12a *priv)
  573. {
  574. int ret;
  575. ret = priv->drvdata->set_phy_mode(priv, USB2_OTG_PHY,
  576. priv->otg_phy_mode);
  577. if (ret)
  578. return ret;
  579. dwc3_meson_g12a_usb_otg_apply_mode(priv, priv->otg_phy_mode);
  580. return 0;
  581. }
  582. static int dwc3_meson_g12a_probe(struct platform_device *pdev)
  583. {
  584. struct dwc3_meson_g12a *priv;
  585. struct device *dev = &pdev->dev;
  586. struct device_node *np = dev->of_node;
  587. void __iomem *base;
  588. int ret, i;
  589. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  590. if (!priv)
  591. return -ENOMEM;
  592. base = devm_platform_ioremap_resource(pdev, 0);
  593. if (IS_ERR(base))
  594. return PTR_ERR(base);
  595. priv->drvdata = of_device_get_match_data(&pdev->dev);
  596. priv->dev = dev;
  597. priv->vbus = devm_regulator_get_optional(dev, "vbus");
  598. if (IS_ERR(priv->vbus)) {
  599. if (PTR_ERR(priv->vbus) == -EPROBE_DEFER)
  600. return PTR_ERR(priv->vbus);
  601. priv->vbus = NULL;
  602. }
  603. ret = devm_clk_bulk_get(dev,
  604. priv->drvdata->num_clks,
  605. priv->drvdata->clks);
  606. if (ret)
  607. return ret;
  608. ret = clk_bulk_prepare_enable(priv->drvdata->num_clks,
  609. priv->drvdata->clks);
  610. if (ret)
  611. return ret;
  612. platform_set_drvdata(pdev, priv);
  613. priv->reset = devm_reset_control_get_shared(dev, NULL);
  614. if (IS_ERR(priv->reset)) {
  615. ret = PTR_ERR(priv->reset);
  616. dev_err(dev, "failed to get device reset, err=%d\n", ret);
  617. goto err_disable_clks;
  618. }
  619. ret = reset_control_reset(priv->reset);
  620. if (ret)
  621. goto err_disable_clks;
  622. ret = dwc3_meson_g12a_get_phys(priv);
  623. if (ret)
  624. goto err_rearm;
  625. ret = priv->drvdata->setup_regmaps(priv, base);
  626. if (ret)
  627. goto err_rearm;
  628. if (priv->vbus) {
  629. ret = regulator_enable(priv->vbus);
  630. if (ret)
  631. goto err_rearm;
  632. }
  633. /* Get dr_mode */
  634. priv->otg_mode = usb_get_dr_mode(dev);
  635. if (priv->otg_mode == USB_DR_MODE_PERIPHERAL)
  636. priv->otg_phy_mode = PHY_MODE_USB_DEVICE;
  637. else
  638. priv->otg_phy_mode = PHY_MODE_USB_HOST;
  639. ret = priv->drvdata->usb_init(priv);
  640. if (ret)
  641. goto err_disable_regulator;
  642. /* Init PHYs */
  643. for (i = 0 ; i < PHY_COUNT ; ++i) {
  644. ret = phy_init(priv->phys[i]);
  645. if (ret)
  646. goto err_disable_regulator;
  647. }
  648. /* Set PHY Power */
  649. for (i = 0 ; i < PHY_COUNT ; ++i) {
  650. ret = phy_power_on(priv->phys[i]);
  651. if (ret)
  652. goto err_phys_exit;
  653. }
  654. if (priv->drvdata->usb_post_init) {
  655. ret = priv->drvdata->usb_post_init(priv);
  656. if (ret)
  657. goto err_phys_power;
  658. }
  659. ret = of_platform_populate(np, NULL, NULL, dev);
  660. if (ret)
  661. goto err_phys_power;
  662. ret = dwc3_meson_g12a_otg_init(pdev, priv);
  663. if (ret)
  664. goto err_plat_depopulate;
  665. pm_runtime_set_active(dev);
  666. pm_runtime_enable(dev);
  667. pm_runtime_get_sync(dev);
  668. return 0;
  669. err_plat_depopulate:
  670. of_platform_depopulate(dev);
  671. err_phys_power:
  672. for (i = 0 ; i < PHY_COUNT ; ++i)
  673. phy_power_off(priv->phys[i]);
  674. err_phys_exit:
  675. for (i = 0 ; i < PHY_COUNT ; ++i)
  676. phy_exit(priv->phys[i]);
  677. err_disable_regulator:
  678. if (priv->vbus)
  679. regulator_disable(priv->vbus);
  680. err_rearm:
  681. reset_control_rearm(priv->reset);
  682. err_disable_clks:
  683. clk_bulk_disable_unprepare(priv->drvdata->num_clks,
  684. priv->drvdata->clks);
  685. return ret;
  686. }
  687. static int dwc3_meson_g12a_remove(struct platform_device *pdev)
  688. {
  689. struct dwc3_meson_g12a *priv = platform_get_drvdata(pdev);
  690. struct device *dev = &pdev->dev;
  691. int i;
  692. if (priv->drvdata->otg_switch_supported)
  693. usb_role_switch_unregister(priv->role_switch);
  694. of_platform_depopulate(dev);
  695. for (i = 0 ; i < PHY_COUNT ; ++i) {
  696. phy_power_off(priv->phys[i]);
  697. phy_exit(priv->phys[i]);
  698. }
  699. pm_runtime_disable(dev);
  700. pm_runtime_put_noidle(dev);
  701. pm_runtime_set_suspended(dev);
  702. reset_control_rearm(priv->reset);
  703. clk_bulk_disable_unprepare(priv->drvdata->num_clks,
  704. priv->drvdata->clks);
  705. return 0;
  706. }
  707. static int __maybe_unused dwc3_meson_g12a_runtime_suspend(struct device *dev)
  708. {
  709. struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
  710. clk_bulk_disable_unprepare(priv->drvdata->num_clks,
  711. priv->drvdata->clks);
  712. return 0;
  713. }
  714. static int __maybe_unused dwc3_meson_g12a_runtime_resume(struct device *dev)
  715. {
  716. struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
  717. return clk_bulk_prepare_enable(priv->drvdata->num_clks,
  718. priv->drvdata->clks);
  719. }
  720. static int __maybe_unused dwc3_meson_g12a_suspend(struct device *dev)
  721. {
  722. struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
  723. int i, ret;
  724. if (priv->vbus && priv->otg_phy_mode == PHY_MODE_USB_HOST) {
  725. ret = regulator_disable(priv->vbus);
  726. if (ret)
  727. return ret;
  728. }
  729. for (i = 0 ; i < PHY_COUNT ; ++i) {
  730. phy_power_off(priv->phys[i]);
  731. phy_exit(priv->phys[i]);
  732. }
  733. reset_control_rearm(priv->reset);
  734. return 0;
  735. }
  736. static int __maybe_unused dwc3_meson_g12a_resume(struct device *dev)
  737. {
  738. struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
  739. int i, ret;
  740. ret = reset_control_reset(priv->reset);
  741. if (ret)
  742. return ret;
  743. ret = priv->drvdata->usb_init(priv);
  744. if (ret)
  745. return ret;
  746. /* Init PHYs */
  747. for (i = 0 ; i < PHY_COUNT ; ++i) {
  748. ret = phy_init(priv->phys[i]);
  749. if (ret)
  750. return ret;
  751. }
  752. /* Set PHY Power */
  753. for (i = 0 ; i < PHY_COUNT ; ++i) {
  754. ret = phy_power_on(priv->phys[i]);
  755. if (ret)
  756. return ret;
  757. }
  758. if (priv->vbus && priv->otg_phy_mode == PHY_MODE_USB_HOST) {
  759. ret = regulator_enable(priv->vbus);
  760. if (ret)
  761. return ret;
  762. }
  763. if (priv->drvdata->usb_post_init) {
  764. ret = priv->drvdata->usb_post_init(priv);
  765. if (ret)
  766. return ret;
  767. }
  768. return 0;
  769. }
  770. static const struct dev_pm_ops dwc3_meson_g12a_dev_pm_ops = {
  771. SET_SYSTEM_SLEEP_PM_OPS(dwc3_meson_g12a_suspend, dwc3_meson_g12a_resume)
  772. SET_RUNTIME_PM_OPS(dwc3_meson_g12a_runtime_suspend,
  773. dwc3_meson_g12a_runtime_resume, NULL)
  774. };
  775. static const struct of_device_id dwc3_meson_g12a_match[] = {
  776. {
  777. .compatible = "amlogic,meson-gxl-usb-ctrl",
  778. .data = &gxl_drvdata,
  779. },
  780. {
  781. .compatible = "amlogic,meson-gxm-usb-ctrl",
  782. .data = &gxm_drvdata,
  783. },
  784. {
  785. .compatible = "amlogic,meson-axg-usb-ctrl",
  786. .data = &axg_drvdata,
  787. },
  788. {
  789. .compatible = "amlogic,meson-g12a-usb-ctrl",
  790. .data = &g12a_drvdata,
  791. },
  792. {
  793. .compatible = "amlogic,meson-a1-usb-ctrl",
  794. .data = &a1_drvdata,
  795. },
  796. { /* Sentinel */ }
  797. };
  798. MODULE_DEVICE_TABLE(of, dwc3_meson_g12a_match);
  799. static struct platform_driver dwc3_meson_g12a_driver = {
  800. .probe = dwc3_meson_g12a_probe,
  801. .remove = dwc3_meson_g12a_remove,
  802. .driver = {
  803. .name = "dwc3-meson-g12a",
  804. .of_match_table = dwc3_meson_g12a_match,
  805. .pm = &dwc3_meson_g12a_dev_pm_ops,
  806. },
  807. };
  808. module_platform_driver(dwc3_meson_g12a_driver);
  809. MODULE_LICENSE("GPL v2");
  810. MODULE_DESCRIPTION("Amlogic Meson G12A USB Glue Layer");
  811. MODULE_AUTHOR("Neil Armstrong <[email protected]>");