dwc3-meson-g12a.c 26 KB

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