xhci-mtk.c 19 KB

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