xhci-mtk.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * MediaTek xHCI Host Controller Driver
  3. *
  4. * Copyright (c) 2015 MediaTek Inc.
  5. * Author:
  6. * Chunfeng Yun <chunfeng.yun@mediatek.com>
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/iopoll.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/phy/phy.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/regmap.h>
  29. #include <linux/regulator/consumer.h>
  30. #include "xhci.h"
  31. #include "xhci-mtk.h"
  32. /* ip_pw_ctrl0 register */
  33. #define CTRL0_IP_SW_RST BIT(0)
  34. /* ip_pw_ctrl1 register */
  35. #define CTRL1_IP_HOST_PDN BIT(0)
  36. /* ip_pw_ctrl2 register */
  37. #define CTRL2_IP_DEV_PDN BIT(0)
  38. /* ip_pw_sts1 register */
  39. #define STS1_IP_SLEEP_STS BIT(30)
  40. #define STS1_XHCI_RST BIT(11)
  41. #define STS1_SYS125_RST BIT(10)
  42. #define STS1_REF_RST BIT(8)
  43. #define STS1_SYSPLL_STABLE BIT(0)
  44. /* ip_xhci_cap register */
  45. #define CAP_U3_PORT_NUM(p) ((p) & 0xff)
  46. #define CAP_U2_PORT_NUM(p) (((p) >> 8) & 0xff)
  47. /* u3_ctrl_p register */
  48. #define CTRL_U3_PORT_HOST_SEL BIT(2)
  49. #define CTRL_U3_PORT_PDN BIT(1)
  50. #define CTRL_U3_PORT_DIS BIT(0)
  51. /* u2_ctrl_p register */
  52. #define CTRL_U2_PORT_HOST_SEL BIT(2)
  53. #define CTRL_U2_PORT_PDN BIT(1)
  54. #define CTRL_U2_PORT_DIS BIT(0)
  55. /* u2_phy_pll register */
  56. #define CTRL_U2_FORCE_PLL_STB BIT(28)
  57. #define PERI_WK_CTRL0 0x400
  58. #define UWK_CTR0_0P_LS_PE BIT(8) /* posedge */
  59. #define UWK_CTR0_0P_LS_NE BIT(7) /* negedge for 0p linestate*/
  60. #define UWK_CTL1_1P_LS_C(x) (((x) & 0xf) << 1)
  61. #define UWK_CTL1_1P_LS_E BIT(0)
  62. #define PERI_WK_CTRL1 0x404
  63. #define UWK_CTL1_IS_C(x) (((x) & 0xf) << 26)
  64. #define UWK_CTL1_IS_E BIT(25)
  65. #define UWK_CTL1_0P_LS_C(x) (((x) & 0xf) << 21)
  66. #define UWK_CTL1_0P_LS_E BIT(20)
  67. #define UWK_CTL1_IDDIG_C(x) (((x) & 0xf) << 11) /* cycle debounce */
  68. #define UWK_CTL1_IDDIG_E BIT(10) /* enable debounce */
  69. #define UWK_CTL1_IDDIG_P BIT(9) /* polarity */
  70. #define UWK_CTL1_0P_LS_P BIT(7)
  71. #define UWK_CTL1_IS_P BIT(6) /* polarity for ip sleep */
  72. enum ssusb_wakeup_src {
  73. SSUSB_WK_IP_SLEEP = 1,
  74. SSUSB_WK_LINE_STATE = 2,
  75. };
  76. static int xhci_mtk_host_enable(struct xhci_hcd_mtk *mtk)
  77. {
  78. struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
  79. u32 value, check_val;
  80. int ret;
  81. int i;
  82. if (!mtk->has_ippc)
  83. return 0;
  84. /* power on host ip */
  85. value = readl(&ippc->ip_pw_ctr1);
  86. value &= ~CTRL1_IP_HOST_PDN;
  87. writel(value, &ippc->ip_pw_ctr1);
  88. /* power on and enable all u3 ports */
  89. for (i = 0; i < mtk->num_u3_ports; i++) {
  90. value = readl(&ippc->u3_ctrl_p[i]);
  91. value &= ~(CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS);
  92. value |= CTRL_U3_PORT_HOST_SEL;
  93. writel(value, &ippc->u3_ctrl_p[i]);
  94. }
  95. /* power on and enable all u2 ports */
  96. for (i = 0; i < mtk->num_u2_ports; i++) {
  97. value = readl(&ippc->u2_ctrl_p[i]);
  98. value &= ~(CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS);
  99. value |= CTRL_U2_PORT_HOST_SEL;
  100. writel(value, &ippc->u2_ctrl_p[i]);
  101. }
  102. /*
  103. * wait for clocks to be stable, and clock domains reset to
  104. * be inactive after power on and enable ports
  105. */
  106. check_val = STS1_SYSPLL_STABLE | STS1_REF_RST |
  107. STS1_SYS125_RST | STS1_XHCI_RST;
  108. ret = readl_poll_timeout(&ippc->ip_pw_sts1, value,
  109. (check_val == (value & check_val)), 100, 20000);
  110. if (ret) {
  111. dev_err(mtk->dev, "clocks are not stable (0x%x)\n", value);
  112. return ret;
  113. }
  114. return 0;
  115. }
  116. static int xhci_mtk_host_disable(struct xhci_hcd_mtk *mtk)
  117. {
  118. struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
  119. u32 value;
  120. int ret;
  121. int i;
  122. if (!mtk->has_ippc)
  123. return 0;
  124. /* power down all u3 ports */
  125. for (i = 0; i < mtk->num_u3_ports; i++) {
  126. value = readl(&ippc->u3_ctrl_p[i]);
  127. value |= CTRL_U3_PORT_PDN;
  128. writel(value, &ippc->u3_ctrl_p[i]);
  129. }
  130. /* power down all u2 ports */
  131. for (i = 0; i < mtk->num_u2_ports; i++) {
  132. value = readl(&ippc->u2_ctrl_p[i]);
  133. value |= CTRL_U2_PORT_PDN;
  134. writel(value, &ippc->u2_ctrl_p[i]);
  135. }
  136. /* power down host ip */
  137. value = readl(&ippc->ip_pw_ctr1);
  138. value |= CTRL1_IP_HOST_PDN;
  139. writel(value, &ippc->ip_pw_ctr1);
  140. /* wait for host ip to sleep */
  141. ret = readl_poll_timeout(&ippc->ip_pw_sts1, value,
  142. (value & STS1_IP_SLEEP_STS), 100, 100000);
  143. if (ret) {
  144. dev_err(mtk->dev, "ip sleep failed!!!\n");
  145. return ret;
  146. }
  147. return 0;
  148. }
  149. static int xhci_mtk_ssusb_config(struct xhci_hcd_mtk *mtk)
  150. {
  151. struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
  152. u32 value;
  153. if (!mtk->has_ippc)
  154. return 0;
  155. /* reset whole ip */
  156. value = readl(&ippc->ip_pw_ctr0);
  157. value |= CTRL0_IP_SW_RST;
  158. writel(value, &ippc->ip_pw_ctr0);
  159. udelay(1);
  160. value = readl(&ippc->ip_pw_ctr0);
  161. value &= ~CTRL0_IP_SW_RST;
  162. writel(value, &ippc->ip_pw_ctr0);
  163. /*
  164. * device ip is default power-on in fact
  165. * power down device ip, otherwise ip-sleep will fail
  166. */
  167. value = readl(&ippc->ip_pw_ctr2);
  168. value |= CTRL2_IP_DEV_PDN;
  169. writel(value, &ippc->ip_pw_ctr2);
  170. value = readl(&ippc->ip_xhci_cap);
  171. mtk->num_u3_ports = CAP_U3_PORT_NUM(value);
  172. mtk->num_u2_ports = CAP_U2_PORT_NUM(value);
  173. dev_dbg(mtk->dev, "%s u2p:%d, u3p:%d\n", __func__,
  174. mtk->num_u2_ports, mtk->num_u3_ports);
  175. return xhci_mtk_host_enable(mtk);
  176. }
  177. static int xhci_mtk_clks_enable(struct xhci_hcd_mtk *mtk)
  178. {
  179. int ret;
  180. ret = clk_prepare_enable(mtk->ref_clk);
  181. if (ret) {
  182. dev_err(mtk->dev, "failed to enable ref_clk\n");
  183. goto ref_clk_err;
  184. }
  185. ret = clk_prepare_enable(mtk->sys_clk);
  186. if (ret) {
  187. dev_err(mtk->dev, "failed to enable sys_clk\n");
  188. goto sys_clk_err;
  189. }
  190. if (mtk->wakeup_src) {
  191. ret = clk_prepare_enable(mtk->wk_deb_p0);
  192. if (ret) {
  193. dev_err(mtk->dev, "failed to enable wk_deb_p0\n");
  194. goto usb_p0_err;
  195. }
  196. ret = clk_prepare_enable(mtk->wk_deb_p1);
  197. if (ret) {
  198. dev_err(mtk->dev, "failed to enable wk_deb_p1\n");
  199. goto usb_p1_err;
  200. }
  201. }
  202. return 0;
  203. usb_p1_err:
  204. clk_disable_unprepare(mtk->wk_deb_p0);
  205. usb_p0_err:
  206. clk_disable_unprepare(mtk->sys_clk);
  207. sys_clk_err:
  208. clk_disable_unprepare(mtk->ref_clk);
  209. ref_clk_err:
  210. return -EINVAL;
  211. }
  212. static void xhci_mtk_clks_disable(struct xhci_hcd_mtk *mtk)
  213. {
  214. if (mtk->wakeup_src) {
  215. clk_disable_unprepare(mtk->wk_deb_p1);
  216. clk_disable_unprepare(mtk->wk_deb_p0);
  217. }
  218. clk_disable_unprepare(mtk->sys_clk);
  219. clk_disable_unprepare(mtk->ref_clk);
  220. }
  221. /* only clocks can be turn off for ip-sleep wakeup mode */
  222. static void usb_wakeup_ip_sleep_en(struct xhci_hcd_mtk *mtk)
  223. {
  224. u32 tmp;
  225. struct regmap *pericfg = mtk->pericfg;
  226. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  227. tmp &= ~UWK_CTL1_IS_P;
  228. tmp &= ~(UWK_CTL1_IS_C(0xf));
  229. tmp |= UWK_CTL1_IS_C(0x8);
  230. regmap_write(pericfg, PERI_WK_CTRL1, tmp);
  231. regmap_write(pericfg, PERI_WK_CTRL1, tmp | UWK_CTL1_IS_E);
  232. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  233. dev_dbg(mtk->dev, "%s(): WK_CTRL1[P6,E25,C26:29]=%#x\n",
  234. __func__, tmp);
  235. }
  236. static void usb_wakeup_ip_sleep_dis(struct xhci_hcd_mtk *mtk)
  237. {
  238. u32 tmp;
  239. regmap_read(mtk->pericfg, PERI_WK_CTRL1, &tmp);
  240. tmp &= ~UWK_CTL1_IS_E;
  241. regmap_write(mtk->pericfg, PERI_WK_CTRL1, tmp);
  242. }
  243. /*
  244. * for line-state wakeup mode, phy's power should not power-down
  245. * and only support cable plug in/out
  246. */
  247. static void usb_wakeup_line_state_en(struct xhci_hcd_mtk *mtk)
  248. {
  249. u32 tmp;
  250. struct regmap *pericfg = mtk->pericfg;
  251. /* line-state of u2-port0 */
  252. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  253. tmp &= ~UWK_CTL1_0P_LS_P;
  254. tmp &= ~(UWK_CTL1_0P_LS_C(0xf));
  255. tmp |= UWK_CTL1_0P_LS_C(0x8);
  256. regmap_write(pericfg, PERI_WK_CTRL1, tmp);
  257. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  258. regmap_write(pericfg, PERI_WK_CTRL1, tmp | UWK_CTL1_0P_LS_E);
  259. /* line-state of u2-port1 */
  260. regmap_read(pericfg, PERI_WK_CTRL0, &tmp);
  261. tmp &= ~(UWK_CTL1_1P_LS_C(0xf));
  262. tmp |= UWK_CTL1_1P_LS_C(0x8);
  263. regmap_write(pericfg, PERI_WK_CTRL0, tmp);
  264. regmap_write(pericfg, PERI_WK_CTRL0, tmp | UWK_CTL1_1P_LS_E);
  265. }
  266. static void usb_wakeup_line_state_dis(struct xhci_hcd_mtk *mtk)
  267. {
  268. u32 tmp;
  269. struct regmap *pericfg = mtk->pericfg;
  270. /* line-state of u2-port0 */
  271. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  272. tmp &= ~UWK_CTL1_0P_LS_E;
  273. regmap_write(pericfg, PERI_WK_CTRL1, tmp);
  274. /* line-state of u2-port1 */
  275. regmap_read(pericfg, PERI_WK_CTRL0, &tmp);
  276. tmp &= ~UWK_CTL1_1P_LS_E;
  277. regmap_write(pericfg, PERI_WK_CTRL0, tmp);
  278. }
  279. static void usb_wakeup_enable(struct xhci_hcd_mtk *mtk)
  280. {
  281. if (mtk->wakeup_src == SSUSB_WK_IP_SLEEP)
  282. usb_wakeup_ip_sleep_en(mtk);
  283. else if (mtk->wakeup_src == SSUSB_WK_LINE_STATE)
  284. usb_wakeup_line_state_en(mtk);
  285. }
  286. static void usb_wakeup_disable(struct xhci_hcd_mtk *mtk)
  287. {
  288. if (mtk->wakeup_src == SSUSB_WK_IP_SLEEP)
  289. usb_wakeup_ip_sleep_dis(mtk);
  290. else if (mtk->wakeup_src == SSUSB_WK_LINE_STATE)
  291. usb_wakeup_line_state_dis(mtk);
  292. }
  293. static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk,
  294. struct device_node *dn)
  295. {
  296. struct device *dev = mtk->dev;
  297. /*
  298. * wakeup function is optional, so it is not an error if this property
  299. * does not exist, and in such case, no need to get relative
  300. * properties anymore.
  301. */
  302. of_property_read_u32(dn, "mediatek,wakeup-src", &mtk->wakeup_src);
  303. if (!mtk->wakeup_src)
  304. return 0;
  305. mtk->wk_deb_p0 = devm_clk_get(dev, "wakeup_deb_p0");
  306. if (IS_ERR(mtk->wk_deb_p0)) {
  307. dev_err(dev, "fail to get wakeup_deb_p0\n");
  308. return PTR_ERR(mtk->wk_deb_p0);
  309. }
  310. mtk->wk_deb_p1 = devm_clk_get(dev, "wakeup_deb_p1");
  311. if (IS_ERR(mtk->wk_deb_p1)) {
  312. dev_err(dev, "fail to get wakeup_deb_p1\n");
  313. return PTR_ERR(mtk->wk_deb_p1);
  314. }
  315. mtk->pericfg = syscon_regmap_lookup_by_phandle(dn,
  316. "mediatek,syscon-wakeup");
  317. if (IS_ERR(mtk->pericfg)) {
  318. dev_err(dev, "fail to get pericfg regs\n");
  319. return PTR_ERR(mtk->pericfg);
  320. }
  321. return 0;
  322. }
  323. static int xhci_mtk_setup(struct usb_hcd *hcd);
  324. static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
  325. .reset = xhci_mtk_setup,
  326. };
  327. static struct hc_driver __read_mostly xhci_mtk_hc_driver;
  328. static int xhci_mtk_phy_init(struct xhci_hcd_mtk *mtk)
  329. {
  330. int i;
  331. int ret;
  332. for (i = 0; i < mtk->num_phys; i++) {
  333. ret = phy_init(mtk->phys[i]);
  334. if (ret)
  335. goto exit_phy;
  336. }
  337. return 0;
  338. exit_phy:
  339. for (; i > 0; i--)
  340. phy_exit(mtk->phys[i - 1]);
  341. return ret;
  342. }
  343. static int xhci_mtk_phy_exit(struct xhci_hcd_mtk *mtk)
  344. {
  345. int i;
  346. for (i = 0; i < mtk->num_phys; i++)
  347. phy_exit(mtk->phys[i]);
  348. return 0;
  349. }
  350. static int xhci_mtk_phy_power_on(struct xhci_hcd_mtk *mtk)
  351. {
  352. int i;
  353. int ret;
  354. for (i = 0; i < mtk->num_phys; i++) {
  355. ret = phy_power_on(mtk->phys[i]);
  356. if (ret)
  357. goto power_off_phy;
  358. }
  359. return 0;
  360. power_off_phy:
  361. for (; i > 0; i--)
  362. phy_power_off(mtk->phys[i - 1]);
  363. return ret;
  364. }
  365. static void xhci_mtk_phy_power_off(struct xhci_hcd_mtk *mtk)
  366. {
  367. unsigned int i;
  368. for (i = 0; i < mtk->num_phys; i++)
  369. phy_power_off(mtk->phys[i]);
  370. }
  371. static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
  372. {
  373. int ret;
  374. ret = regulator_enable(mtk->vbus);
  375. if (ret) {
  376. dev_err(mtk->dev, "failed to enable vbus\n");
  377. return ret;
  378. }
  379. ret = regulator_enable(mtk->vusb33);
  380. if (ret) {
  381. dev_err(mtk->dev, "failed to enable vusb33\n");
  382. regulator_disable(mtk->vbus);
  383. return ret;
  384. }
  385. return 0;
  386. }
  387. static void xhci_mtk_ldos_disable(struct xhci_hcd_mtk *mtk)
  388. {
  389. regulator_disable(mtk->vbus);
  390. regulator_disable(mtk->vusb33);
  391. }
  392. static void xhci_mtk_quirks(struct device *dev, struct xhci_hcd *xhci)
  393. {
  394. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  395. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  396. /*
  397. * As of now platform drivers don't provide MSI support so we ensure
  398. * here that the generic code does not try to make a pci_dev from our
  399. * dev struct in order to setup MSI
  400. */
  401. xhci->quirks |= XHCI_PLAT;
  402. xhci->quirks |= XHCI_MTK_HOST;
  403. /*
  404. * MTK host controller gives a spurious successful event after a
  405. * short transfer. Ignore it.
  406. */
  407. xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
  408. if (mtk->lpm_support)
  409. xhci->quirks |= XHCI_LPM_SUPPORT;
  410. }
  411. /* called during probe() after chip reset completes */
  412. static int xhci_mtk_setup(struct usb_hcd *hcd)
  413. {
  414. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  415. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  416. int ret;
  417. if (usb_hcd_is_primary_hcd(hcd)) {
  418. ret = xhci_mtk_ssusb_config(mtk);
  419. if (ret)
  420. return ret;
  421. }
  422. ret = xhci_gen_setup(hcd, xhci_mtk_quirks);
  423. if (ret)
  424. return ret;
  425. if (usb_hcd_is_primary_hcd(hcd)) {
  426. mtk->num_u3_ports = xhci->num_usb3_ports;
  427. mtk->num_u2_ports = xhci->num_usb2_ports;
  428. ret = xhci_mtk_sch_init(mtk);
  429. if (ret)
  430. return ret;
  431. }
  432. return ret;
  433. }
  434. static int xhci_mtk_probe(struct platform_device *pdev)
  435. {
  436. struct device *dev = &pdev->dev;
  437. struct device_node *node = dev->of_node;
  438. struct xhci_hcd_mtk *mtk;
  439. const struct hc_driver *driver;
  440. struct xhci_hcd *xhci;
  441. struct resource *res;
  442. struct usb_hcd *hcd;
  443. struct phy *phy;
  444. int phy_num;
  445. int ret = -ENODEV;
  446. int irq;
  447. if (usb_disabled())
  448. return -ENODEV;
  449. driver = &xhci_mtk_hc_driver;
  450. mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL);
  451. if (!mtk)
  452. return -ENOMEM;
  453. mtk->dev = dev;
  454. mtk->vbus = devm_regulator_get(dev, "vbus");
  455. if (IS_ERR(mtk->vbus)) {
  456. dev_err(dev, "fail to get vbus\n");
  457. return PTR_ERR(mtk->vbus);
  458. }
  459. mtk->vusb33 = devm_regulator_get(dev, "vusb33");
  460. if (IS_ERR(mtk->vusb33)) {
  461. dev_err(dev, "fail to get vusb33\n");
  462. return PTR_ERR(mtk->vusb33);
  463. }
  464. mtk->sys_clk = devm_clk_get(dev, "sys_ck");
  465. if (IS_ERR(mtk->sys_clk)) {
  466. dev_err(dev, "fail to get sys_ck\n");
  467. return PTR_ERR(mtk->sys_clk);
  468. }
  469. /*
  470. * reference clock is usually a "fixed-clock", make it optional
  471. * for backward compatibility and ignore the error if it does
  472. * not exist.
  473. */
  474. mtk->ref_clk = devm_clk_get(dev, "ref_ck");
  475. if (IS_ERR(mtk->ref_clk)) {
  476. if (PTR_ERR(mtk->ref_clk) == -EPROBE_DEFER)
  477. return -EPROBE_DEFER;
  478. mtk->ref_clk = NULL;
  479. }
  480. mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable");
  481. ret = usb_wakeup_of_property_parse(mtk, node);
  482. if (ret)
  483. return ret;
  484. mtk->num_phys = of_count_phandle_with_args(node,
  485. "phys", "#phy-cells");
  486. if (mtk->num_phys > 0) {
  487. mtk->phys = devm_kcalloc(dev, mtk->num_phys,
  488. sizeof(*mtk->phys), GFP_KERNEL);
  489. if (!mtk->phys)
  490. return -ENOMEM;
  491. } else {
  492. mtk->num_phys = 0;
  493. }
  494. pm_runtime_enable(dev);
  495. pm_runtime_get_sync(dev);
  496. device_enable_async_suspend(dev);
  497. ret = xhci_mtk_ldos_enable(mtk);
  498. if (ret)
  499. goto disable_pm;
  500. ret = xhci_mtk_clks_enable(mtk);
  501. if (ret)
  502. goto disable_ldos;
  503. irq = platform_get_irq(pdev, 0);
  504. if (irq < 0) {
  505. ret = irq;
  506. goto disable_clk;
  507. }
  508. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  509. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  510. if (ret)
  511. goto disable_clk;
  512. if (!dev->dma_mask)
  513. dev->dma_mask = &dev->coherent_dma_mask;
  514. else
  515. dma_set_mask(dev, DMA_BIT_MASK(32));
  516. hcd = usb_create_hcd(driver, dev, dev_name(dev));
  517. if (!hcd) {
  518. ret = -ENOMEM;
  519. goto disable_clk;
  520. }
  521. /*
  522. * USB 2.0 roothub is stored in the platform_device.
  523. * Swap it with mtk HCD.
  524. */
  525. mtk->hcd = platform_get_drvdata(pdev);
  526. platform_set_drvdata(pdev, mtk);
  527. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac");
  528. hcd->regs = devm_ioremap_resource(dev, res);
  529. if (IS_ERR(hcd->regs)) {
  530. ret = PTR_ERR(hcd->regs);
  531. goto put_usb2_hcd;
  532. }
  533. hcd->rsrc_start = res->start;
  534. hcd->rsrc_len = resource_size(res);
  535. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
  536. if (res) { /* ippc register is optional */
  537. mtk->ippc_regs = devm_ioremap_resource(dev, res);
  538. if (IS_ERR(mtk->ippc_regs)) {
  539. ret = PTR_ERR(mtk->ippc_regs);
  540. goto put_usb2_hcd;
  541. }
  542. mtk->has_ippc = true;
  543. } else {
  544. mtk->has_ippc = false;
  545. }
  546. for (phy_num = 0; phy_num < mtk->num_phys; phy_num++) {
  547. phy = devm_of_phy_get_by_index(dev, node, phy_num);
  548. if (IS_ERR(phy)) {
  549. ret = PTR_ERR(phy);
  550. goto put_usb2_hcd;
  551. }
  552. mtk->phys[phy_num] = phy;
  553. }
  554. ret = xhci_mtk_phy_init(mtk);
  555. if (ret)
  556. goto put_usb2_hcd;
  557. ret = xhci_mtk_phy_power_on(mtk);
  558. if (ret)
  559. goto exit_phys;
  560. device_init_wakeup(dev, true);
  561. xhci = hcd_to_xhci(hcd);
  562. xhci->main_hcd = hcd;
  563. xhci->shared_hcd = usb_create_shared_hcd(driver, dev,
  564. dev_name(dev), hcd);
  565. if (!xhci->shared_hcd) {
  566. ret = -ENOMEM;
  567. goto power_off_phys;
  568. }
  569. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  570. if (ret)
  571. goto put_usb3_hcd;
  572. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  573. xhci->shared_hcd->can_do_streams = 1;
  574. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  575. if (ret)
  576. goto dealloc_usb2_hcd;
  577. return 0;
  578. dealloc_usb2_hcd:
  579. usb_remove_hcd(hcd);
  580. put_usb3_hcd:
  581. xhci_mtk_sch_exit(mtk);
  582. usb_put_hcd(xhci->shared_hcd);
  583. power_off_phys:
  584. xhci_mtk_phy_power_off(mtk);
  585. device_init_wakeup(dev, false);
  586. exit_phys:
  587. xhci_mtk_phy_exit(mtk);
  588. put_usb2_hcd:
  589. usb_put_hcd(hcd);
  590. disable_clk:
  591. xhci_mtk_clks_disable(mtk);
  592. disable_ldos:
  593. xhci_mtk_ldos_disable(mtk);
  594. disable_pm:
  595. pm_runtime_put_sync(dev);
  596. pm_runtime_disable(dev);
  597. return ret;
  598. }
  599. static int xhci_mtk_remove(struct platform_device *dev)
  600. {
  601. struct xhci_hcd_mtk *mtk = platform_get_drvdata(dev);
  602. struct usb_hcd *hcd = mtk->hcd;
  603. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  604. usb_remove_hcd(xhci->shared_hcd);
  605. xhci_mtk_phy_power_off(mtk);
  606. xhci_mtk_phy_exit(mtk);
  607. device_init_wakeup(&dev->dev, false);
  608. usb_remove_hcd(hcd);
  609. usb_put_hcd(xhci->shared_hcd);
  610. usb_put_hcd(hcd);
  611. xhci_mtk_sch_exit(mtk);
  612. xhci_mtk_clks_disable(mtk);
  613. xhci_mtk_ldos_disable(mtk);
  614. pm_runtime_put_sync(&dev->dev);
  615. pm_runtime_disable(&dev->dev);
  616. return 0;
  617. }
  618. /*
  619. * if ip sleep fails, and all clocks are disabled, access register will hang
  620. * AHB bus, so stop polling roothubs to avoid regs access on bus suspend.
  621. * and no need to check whether ip sleep failed or not; this will cause SPM
  622. * to wake up system immediately after system suspend complete if ip sleep
  623. * fails, it is what we wanted.
  624. */
  625. static int __maybe_unused xhci_mtk_suspend(struct device *dev)
  626. {
  627. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  628. struct usb_hcd *hcd = mtk->hcd;
  629. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  630. xhci_dbg(xhci, "%s: stop port polling\n", __func__);
  631. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  632. del_timer_sync(&hcd->rh_timer);
  633. clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  634. del_timer_sync(&xhci->shared_hcd->rh_timer);
  635. xhci_mtk_host_disable(mtk);
  636. xhci_mtk_phy_power_off(mtk);
  637. xhci_mtk_clks_disable(mtk);
  638. usb_wakeup_enable(mtk);
  639. return 0;
  640. }
  641. static int __maybe_unused xhci_mtk_resume(struct device *dev)
  642. {
  643. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  644. struct usb_hcd *hcd = mtk->hcd;
  645. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  646. usb_wakeup_disable(mtk);
  647. xhci_mtk_clks_enable(mtk);
  648. xhci_mtk_phy_power_on(mtk);
  649. xhci_mtk_host_enable(mtk);
  650. xhci_dbg(xhci, "%s: restart port polling\n", __func__);
  651. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  652. usb_hcd_poll_rh_status(hcd);
  653. set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  654. usb_hcd_poll_rh_status(xhci->shared_hcd);
  655. return 0;
  656. }
  657. static const struct dev_pm_ops xhci_mtk_pm_ops = {
  658. SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume)
  659. };
  660. #define DEV_PM_OPS IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL
  661. #ifdef CONFIG_OF
  662. static const struct of_device_id mtk_xhci_of_match[] = {
  663. { .compatible = "mediatek,mt8173-xhci"},
  664. { .compatible = "mediatek,mtk-xhci"},
  665. { },
  666. };
  667. MODULE_DEVICE_TABLE(of, mtk_xhci_of_match);
  668. #endif
  669. static struct platform_driver mtk_xhci_driver = {
  670. .probe = xhci_mtk_probe,
  671. .remove = xhci_mtk_remove,
  672. .driver = {
  673. .name = "xhci-mtk",
  674. .pm = DEV_PM_OPS,
  675. .of_match_table = of_match_ptr(mtk_xhci_of_match),
  676. },
  677. };
  678. MODULE_ALIAS("platform:xhci-mtk");
  679. static int __init xhci_mtk_init(void)
  680. {
  681. xhci_init_driver(&xhci_mtk_hc_driver, &xhci_mtk_overrides);
  682. return platform_driver_register(&mtk_xhci_driver);
  683. }
  684. module_init(xhci_mtk_init);
  685. static void __exit xhci_mtk_exit(void)
  686. {
  687. platform_driver_unregister(&mtk_xhci_driver);
  688. }
  689. module_exit(xhci_mtk_exit);
  690. MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
  691. MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");
  692. MODULE_LICENSE("GPL v2");