xhci-mtk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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->sys_clk);
  181. if (ret) {
  182. dev_err(mtk->dev, "failed to enable sys_clk\n");
  183. goto sys_clk_err;
  184. }
  185. if (mtk->wakeup_src) {
  186. ret = clk_prepare_enable(mtk->wk_deb_p0);
  187. if (ret) {
  188. dev_err(mtk->dev, "failed to enable wk_deb_p0\n");
  189. goto usb_p0_err;
  190. }
  191. ret = clk_prepare_enable(mtk->wk_deb_p1);
  192. if (ret) {
  193. dev_err(mtk->dev, "failed to enable wk_deb_p1\n");
  194. goto usb_p1_err;
  195. }
  196. }
  197. return 0;
  198. usb_p1_err:
  199. clk_disable_unprepare(mtk->wk_deb_p0);
  200. usb_p0_err:
  201. clk_disable_unprepare(mtk->sys_clk);
  202. sys_clk_err:
  203. return -EINVAL;
  204. }
  205. static void xhci_mtk_clks_disable(struct xhci_hcd_mtk *mtk)
  206. {
  207. if (mtk->wakeup_src) {
  208. clk_disable_unprepare(mtk->wk_deb_p1);
  209. clk_disable_unprepare(mtk->wk_deb_p0);
  210. }
  211. clk_disable_unprepare(mtk->sys_clk);
  212. }
  213. /* only clocks can be turn off for ip-sleep wakeup mode */
  214. static void usb_wakeup_ip_sleep_en(struct xhci_hcd_mtk *mtk)
  215. {
  216. u32 tmp;
  217. struct regmap *pericfg = mtk->pericfg;
  218. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  219. tmp &= ~UWK_CTL1_IS_P;
  220. tmp &= ~(UWK_CTL1_IS_C(0xf));
  221. tmp |= UWK_CTL1_IS_C(0x8);
  222. regmap_write(pericfg, PERI_WK_CTRL1, tmp);
  223. regmap_write(pericfg, PERI_WK_CTRL1, tmp | UWK_CTL1_IS_E);
  224. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  225. dev_dbg(mtk->dev, "%s(): WK_CTRL1[P6,E25,C26:29]=%#x\n",
  226. __func__, tmp);
  227. }
  228. static void usb_wakeup_ip_sleep_dis(struct xhci_hcd_mtk *mtk)
  229. {
  230. u32 tmp;
  231. regmap_read(mtk->pericfg, PERI_WK_CTRL1, &tmp);
  232. tmp &= ~UWK_CTL1_IS_E;
  233. regmap_write(mtk->pericfg, PERI_WK_CTRL1, tmp);
  234. }
  235. /*
  236. * for line-state wakeup mode, phy's power should not power-down
  237. * and only support cable plug in/out
  238. */
  239. static void usb_wakeup_line_state_en(struct xhci_hcd_mtk *mtk)
  240. {
  241. u32 tmp;
  242. struct regmap *pericfg = mtk->pericfg;
  243. /* line-state of u2-port0 */
  244. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  245. tmp &= ~UWK_CTL1_0P_LS_P;
  246. tmp &= ~(UWK_CTL1_0P_LS_C(0xf));
  247. tmp |= UWK_CTL1_0P_LS_C(0x8);
  248. regmap_write(pericfg, PERI_WK_CTRL1, tmp);
  249. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  250. regmap_write(pericfg, PERI_WK_CTRL1, tmp | UWK_CTL1_0P_LS_E);
  251. /* line-state of u2-port1 */
  252. regmap_read(pericfg, PERI_WK_CTRL0, &tmp);
  253. tmp &= ~(UWK_CTL1_1P_LS_C(0xf));
  254. tmp |= UWK_CTL1_1P_LS_C(0x8);
  255. regmap_write(pericfg, PERI_WK_CTRL0, tmp);
  256. regmap_write(pericfg, PERI_WK_CTRL0, tmp | UWK_CTL1_1P_LS_E);
  257. }
  258. static void usb_wakeup_line_state_dis(struct xhci_hcd_mtk *mtk)
  259. {
  260. u32 tmp;
  261. struct regmap *pericfg = mtk->pericfg;
  262. /* line-state of u2-port0 */
  263. regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
  264. tmp &= ~UWK_CTL1_0P_LS_E;
  265. regmap_write(pericfg, PERI_WK_CTRL1, tmp);
  266. /* line-state of u2-port1 */
  267. regmap_read(pericfg, PERI_WK_CTRL0, &tmp);
  268. tmp &= ~UWK_CTL1_1P_LS_E;
  269. regmap_write(pericfg, PERI_WK_CTRL0, tmp);
  270. }
  271. static void usb_wakeup_enable(struct xhci_hcd_mtk *mtk)
  272. {
  273. if (mtk->wakeup_src == SSUSB_WK_IP_SLEEP)
  274. usb_wakeup_ip_sleep_en(mtk);
  275. else if (mtk->wakeup_src == SSUSB_WK_LINE_STATE)
  276. usb_wakeup_line_state_en(mtk);
  277. }
  278. static void usb_wakeup_disable(struct xhci_hcd_mtk *mtk)
  279. {
  280. if (mtk->wakeup_src == SSUSB_WK_IP_SLEEP)
  281. usb_wakeup_ip_sleep_dis(mtk);
  282. else if (mtk->wakeup_src == SSUSB_WK_LINE_STATE)
  283. usb_wakeup_line_state_dis(mtk);
  284. }
  285. static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk,
  286. struct device_node *dn)
  287. {
  288. struct device *dev = mtk->dev;
  289. /*
  290. * wakeup function is optional, so it is not an error if this property
  291. * does not exist, and in such case, no need to get relative
  292. * properties anymore.
  293. */
  294. of_property_read_u32(dn, "mediatek,wakeup-src", &mtk->wakeup_src);
  295. if (!mtk->wakeup_src)
  296. return 0;
  297. mtk->wk_deb_p0 = devm_clk_get(dev, "wakeup_deb_p0");
  298. if (IS_ERR(mtk->wk_deb_p0)) {
  299. dev_err(dev, "fail to get wakeup_deb_p0\n");
  300. return PTR_ERR(mtk->wk_deb_p0);
  301. }
  302. mtk->wk_deb_p1 = devm_clk_get(dev, "wakeup_deb_p1");
  303. if (IS_ERR(mtk->wk_deb_p1)) {
  304. dev_err(dev, "fail to get wakeup_deb_p1\n");
  305. return PTR_ERR(mtk->wk_deb_p1);
  306. }
  307. mtk->pericfg = syscon_regmap_lookup_by_phandle(dn,
  308. "mediatek,syscon-wakeup");
  309. if (IS_ERR(mtk->pericfg)) {
  310. dev_err(dev, "fail to get pericfg regs\n");
  311. return PTR_ERR(mtk->pericfg);
  312. }
  313. return 0;
  314. }
  315. static int xhci_mtk_setup(struct usb_hcd *hcd);
  316. static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
  317. .extra_priv_size = sizeof(struct xhci_hcd),
  318. .reset = xhci_mtk_setup,
  319. };
  320. static struct hc_driver __read_mostly xhci_mtk_hc_driver;
  321. static int xhci_mtk_phy_init(struct xhci_hcd_mtk *mtk)
  322. {
  323. int i;
  324. int ret;
  325. for (i = 0; i < mtk->num_phys; i++) {
  326. ret = phy_init(mtk->phys[i]);
  327. if (ret)
  328. goto exit_phy;
  329. }
  330. return 0;
  331. exit_phy:
  332. for (; i > 0; i--)
  333. phy_exit(mtk->phys[i - 1]);
  334. return ret;
  335. }
  336. static int xhci_mtk_phy_exit(struct xhci_hcd_mtk *mtk)
  337. {
  338. int i;
  339. for (i = 0; i < mtk->num_phys; i++)
  340. phy_exit(mtk->phys[i]);
  341. return 0;
  342. }
  343. static int xhci_mtk_phy_power_on(struct xhci_hcd_mtk *mtk)
  344. {
  345. int i;
  346. int ret;
  347. for (i = 0; i < mtk->num_phys; i++) {
  348. ret = phy_power_on(mtk->phys[i]);
  349. if (ret)
  350. goto power_off_phy;
  351. }
  352. return 0;
  353. power_off_phy:
  354. for (; i > 0; i--)
  355. phy_power_off(mtk->phys[i - 1]);
  356. return ret;
  357. }
  358. static void xhci_mtk_phy_power_off(struct xhci_hcd_mtk *mtk)
  359. {
  360. unsigned int i;
  361. for (i = 0; i < mtk->num_phys; i++)
  362. phy_power_off(mtk->phys[i]);
  363. }
  364. static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
  365. {
  366. int ret;
  367. ret = regulator_enable(mtk->vbus);
  368. if (ret) {
  369. dev_err(mtk->dev, "failed to enable vbus\n");
  370. return ret;
  371. }
  372. ret = regulator_enable(mtk->vusb33);
  373. if (ret) {
  374. dev_err(mtk->dev, "failed to enable vusb33\n");
  375. regulator_disable(mtk->vbus);
  376. return ret;
  377. }
  378. return 0;
  379. }
  380. static void xhci_mtk_ldos_disable(struct xhci_hcd_mtk *mtk)
  381. {
  382. regulator_disable(mtk->vbus);
  383. regulator_disable(mtk->vusb33);
  384. }
  385. static void xhci_mtk_quirks(struct device *dev, struct xhci_hcd *xhci)
  386. {
  387. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  388. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  389. /*
  390. * As of now platform drivers don't provide MSI support so we ensure
  391. * here that the generic code does not try to make a pci_dev from our
  392. * dev struct in order to setup MSI
  393. */
  394. xhci->quirks |= XHCI_PLAT;
  395. xhci->quirks |= XHCI_MTK_HOST;
  396. /*
  397. * MTK host controller gives a spurious successful event after a
  398. * short transfer. Ignore it.
  399. */
  400. xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
  401. if (mtk->lpm_support)
  402. xhci->quirks |= XHCI_LPM_SUPPORT;
  403. }
  404. /* called during probe() after chip reset completes */
  405. static int xhci_mtk_setup(struct usb_hcd *hcd)
  406. {
  407. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  408. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  409. int ret;
  410. if (usb_hcd_is_primary_hcd(hcd)) {
  411. ret = xhci_mtk_ssusb_config(mtk);
  412. if (ret)
  413. return ret;
  414. }
  415. ret = xhci_gen_setup(hcd, xhci_mtk_quirks);
  416. if (ret)
  417. return ret;
  418. if (usb_hcd_is_primary_hcd(hcd)) {
  419. mtk->num_u3_ports = xhci->num_usb3_ports;
  420. mtk->num_u2_ports = xhci->num_usb2_ports;
  421. ret = xhci_mtk_sch_init(mtk);
  422. if (ret)
  423. return ret;
  424. }
  425. return ret;
  426. }
  427. static int xhci_mtk_probe(struct platform_device *pdev)
  428. {
  429. struct device *dev = &pdev->dev;
  430. struct device_node *node = dev->of_node;
  431. struct xhci_hcd_mtk *mtk;
  432. const struct hc_driver *driver;
  433. struct xhci_hcd *xhci;
  434. struct resource *res;
  435. struct usb_hcd *hcd;
  436. struct phy *phy;
  437. int phy_num;
  438. int ret = -ENODEV;
  439. int irq;
  440. if (usb_disabled())
  441. return -ENODEV;
  442. driver = &xhci_mtk_hc_driver;
  443. mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL);
  444. if (!mtk)
  445. return -ENOMEM;
  446. mtk->dev = dev;
  447. mtk->vbus = devm_regulator_get(dev, "vbus");
  448. if (IS_ERR(mtk->vbus)) {
  449. dev_err(dev, "fail to get vbus\n");
  450. return PTR_ERR(mtk->vbus);
  451. }
  452. mtk->vusb33 = devm_regulator_get(dev, "vusb33");
  453. if (IS_ERR(mtk->vusb33)) {
  454. dev_err(dev, "fail to get vusb33\n");
  455. return PTR_ERR(mtk->vusb33);
  456. }
  457. mtk->sys_clk = devm_clk_get(dev, "sys_ck");
  458. if (IS_ERR(mtk->sys_clk)) {
  459. dev_err(dev, "fail to get sys_ck\n");
  460. return PTR_ERR(mtk->sys_clk);
  461. }
  462. mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable");
  463. ret = usb_wakeup_of_property_parse(mtk, node);
  464. if (ret)
  465. return ret;
  466. mtk->num_phys = of_count_phandle_with_args(node,
  467. "phys", "#phy-cells");
  468. if (mtk->num_phys > 0) {
  469. mtk->phys = devm_kcalloc(dev, mtk->num_phys,
  470. sizeof(*mtk->phys), GFP_KERNEL);
  471. if (!mtk->phys)
  472. return -ENOMEM;
  473. } else {
  474. mtk->num_phys = 0;
  475. }
  476. pm_runtime_enable(dev);
  477. pm_runtime_get_sync(dev);
  478. device_enable_async_suspend(dev);
  479. ret = xhci_mtk_ldos_enable(mtk);
  480. if (ret)
  481. goto disable_pm;
  482. ret = xhci_mtk_clks_enable(mtk);
  483. if (ret)
  484. goto disable_ldos;
  485. irq = platform_get_irq(pdev, 0);
  486. if (irq < 0) {
  487. ret = irq;
  488. goto disable_clk;
  489. }
  490. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  491. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  492. if (ret)
  493. goto disable_clk;
  494. if (!dev->dma_mask)
  495. dev->dma_mask = &dev->coherent_dma_mask;
  496. else
  497. dma_set_mask(dev, DMA_BIT_MASK(32));
  498. hcd = usb_create_hcd(driver, dev, dev_name(dev));
  499. if (!hcd) {
  500. ret = -ENOMEM;
  501. goto disable_clk;
  502. }
  503. /*
  504. * USB 2.0 roothub is stored in the platform_device.
  505. * Swap it with mtk HCD.
  506. */
  507. mtk->hcd = platform_get_drvdata(pdev);
  508. platform_set_drvdata(pdev, mtk);
  509. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac");
  510. hcd->regs = devm_ioremap_resource(dev, res);
  511. if (IS_ERR(hcd->regs)) {
  512. ret = PTR_ERR(hcd->regs);
  513. goto put_usb2_hcd;
  514. }
  515. hcd->rsrc_start = res->start;
  516. hcd->rsrc_len = resource_size(res);
  517. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
  518. if (res) { /* ippc register is optional */
  519. mtk->ippc_regs = devm_ioremap_resource(dev, res);
  520. if (IS_ERR(mtk->ippc_regs)) {
  521. ret = PTR_ERR(mtk->ippc_regs);
  522. goto put_usb2_hcd;
  523. }
  524. mtk->has_ippc = true;
  525. } else {
  526. mtk->has_ippc = false;
  527. }
  528. for (phy_num = 0; phy_num < mtk->num_phys; phy_num++) {
  529. phy = devm_of_phy_get_by_index(dev, node, phy_num);
  530. if (IS_ERR(phy)) {
  531. ret = PTR_ERR(phy);
  532. goto put_usb2_hcd;
  533. }
  534. mtk->phys[phy_num] = phy;
  535. }
  536. ret = xhci_mtk_phy_init(mtk);
  537. if (ret)
  538. goto put_usb2_hcd;
  539. ret = xhci_mtk_phy_power_on(mtk);
  540. if (ret)
  541. goto exit_phys;
  542. device_init_wakeup(dev, true);
  543. xhci = hcd_to_xhci(hcd);
  544. xhci->main_hcd = hcd;
  545. xhci->shared_hcd = usb_create_shared_hcd(driver, dev,
  546. dev_name(dev), hcd);
  547. if (!xhci->shared_hcd) {
  548. ret = -ENOMEM;
  549. goto power_off_phys;
  550. }
  551. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  552. xhci->shared_hcd->can_do_streams = 1;
  553. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  554. if (ret)
  555. goto put_usb3_hcd;
  556. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  557. if (ret)
  558. goto dealloc_usb2_hcd;
  559. return 0;
  560. dealloc_usb2_hcd:
  561. usb_remove_hcd(hcd);
  562. put_usb3_hcd:
  563. xhci_mtk_sch_exit(mtk);
  564. usb_put_hcd(xhci->shared_hcd);
  565. power_off_phys:
  566. xhci_mtk_phy_power_off(mtk);
  567. device_init_wakeup(dev, false);
  568. exit_phys:
  569. xhci_mtk_phy_exit(mtk);
  570. put_usb2_hcd:
  571. usb_put_hcd(hcd);
  572. disable_clk:
  573. xhci_mtk_clks_disable(mtk);
  574. disable_ldos:
  575. xhci_mtk_ldos_disable(mtk);
  576. disable_pm:
  577. pm_runtime_put_sync(dev);
  578. pm_runtime_disable(dev);
  579. return ret;
  580. }
  581. static int xhci_mtk_remove(struct platform_device *dev)
  582. {
  583. struct xhci_hcd_mtk *mtk = platform_get_drvdata(dev);
  584. struct usb_hcd *hcd = mtk->hcd;
  585. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  586. usb_remove_hcd(xhci->shared_hcd);
  587. xhci_mtk_phy_power_off(mtk);
  588. xhci_mtk_phy_exit(mtk);
  589. device_init_wakeup(&dev->dev, false);
  590. usb_remove_hcd(hcd);
  591. usb_put_hcd(xhci->shared_hcd);
  592. usb_put_hcd(hcd);
  593. xhci_mtk_sch_exit(mtk);
  594. xhci_mtk_clks_disable(mtk);
  595. xhci_mtk_ldos_disable(mtk);
  596. pm_runtime_put_sync(&dev->dev);
  597. pm_runtime_disable(&dev->dev);
  598. return 0;
  599. }
  600. /*
  601. * if ip sleep fails, and all clocks are disabled, access register will hang
  602. * AHB bus, so stop polling roothubs to avoid regs access on bus suspend.
  603. * and no need to check whether ip sleep failed or not; this will cause SPM
  604. * to wake up system immediately after system suspend complete if ip sleep
  605. * fails, it is what we wanted.
  606. */
  607. static int __maybe_unused xhci_mtk_suspend(struct device *dev)
  608. {
  609. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  610. struct usb_hcd *hcd = mtk->hcd;
  611. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  612. xhci_dbg(xhci, "%s: stop port polling\n", __func__);
  613. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  614. del_timer_sync(&hcd->rh_timer);
  615. clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  616. del_timer_sync(&xhci->shared_hcd->rh_timer);
  617. xhci_mtk_host_disable(mtk);
  618. xhci_mtk_phy_power_off(mtk);
  619. xhci_mtk_clks_disable(mtk);
  620. usb_wakeup_enable(mtk);
  621. return 0;
  622. }
  623. static int __maybe_unused xhci_mtk_resume(struct device *dev)
  624. {
  625. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  626. struct usb_hcd *hcd = mtk->hcd;
  627. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  628. usb_wakeup_disable(mtk);
  629. xhci_mtk_clks_enable(mtk);
  630. xhci_mtk_phy_power_on(mtk);
  631. xhci_mtk_host_enable(mtk);
  632. xhci_dbg(xhci, "%s: restart port polling\n", __func__);
  633. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  634. usb_hcd_poll_rh_status(hcd);
  635. set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  636. usb_hcd_poll_rh_status(xhci->shared_hcd);
  637. return 0;
  638. }
  639. static const struct dev_pm_ops xhci_mtk_pm_ops = {
  640. SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume)
  641. };
  642. #define DEV_PM_OPS IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL
  643. #ifdef CONFIG_OF
  644. static const struct of_device_id mtk_xhci_of_match[] = {
  645. { .compatible = "mediatek,mt8173-xhci"},
  646. { },
  647. };
  648. MODULE_DEVICE_TABLE(of, mtk_xhci_of_match);
  649. #endif
  650. static struct platform_driver mtk_xhci_driver = {
  651. .probe = xhci_mtk_probe,
  652. .remove = xhci_mtk_remove,
  653. .driver = {
  654. .name = "xhci-mtk",
  655. .pm = DEV_PM_OPS,
  656. .of_match_table = of_match_ptr(mtk_xhci_of_match),
  657. },
  658. };
  659. MODULE_ALIAS("platform:xhci-mtk");
  660. static int __init xhci_mtk_init(void)
  661. {
  662. xhci_init_driver(&xhci_mtk_hc_driver, &xhci_mtk_overrides);
  663. return platform_driver_register(&mtk_xhci_driver);
  664. }
  665. module_init(xhci_mtk_init);
  666. static void __exit xhci_mtk_exit(void)
  667. {
  668. platform_driver_unregister(&mtk_xhci_driver);
  669. }
  670. module_exit(xhci_mtk_exit);
  671. MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
  672. MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");
  673. MODULE_LICENSE("GPL v2");