phy-mtk-xsphy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MediaTek USB3.1 gen2 xsphy Driver
  4. *
  5. * Copyright (c) 2018 MediaTek Inc.
  6. * Author: Chunfeng Yun <[email protected]>
  7. *
  8. */
  9. #include <dt-bindings/phy/phy.h>
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/module.h>
  14. #include <linux/of_address.h>
  15. #include <linux/phy/phy.h>
  16. #include <linux/platform_device.h>
  17. #include "phy-mtk-io.h"
  18. /* u2 phy banks */
  19. #define SSUSB_SIFSLV_MISC 0x000
  20. #define SSUSB_SIFSLV_U2FREQ 0x100
  21. #define SSUSB_SIFSLV_U2PHY_COM 0x300
  22. /* u3 phy shared banks */
  23. #define SSPXTP_SIFSLV_DIG_GLB 0x000
  24. #define SSPXTP_SIFSLV_PHYA_GLB 0x100
  25. /* u3 phy banks */
  26. #define SSPXTP_SIFSLV_DIG_LN_TOP 0x000
  27. #define SSPXTP_SIFSLV_DIG_LN_TX0 0x100
  28. #define SSPXTP_SIFSLV_DIG_LN_RX0 0x200
  29. #define SSPXTP_SIFSLV_DIG_LN_DAIF 0x300
  30. #define SSPXTP_SIFSLV_PHYA_LN 0x400
  31. #define XSP_U2FREQ_FMCR0 ((SSUSB_SIFSLV_U2FREQ) + 0x00)
  32. #define P2F_RG_FREQDET_EN BIT(24)
  33. #define P2F_RG_CYCLECNT GENMASK(23, 0)
  34. #define XSP_U2FREQ_MMONR0 ((SSUSB_SIFSLV_U2FREQ) + 0x0c)
  35. #define XSP_U2FREQ_FMMONR1 ((SSUSB_SIFSLV_U2FREQ) + 0x10)
  36. #define P2F_RG_FRCK_EN BIT(8)
  37. #define P2F_USB_FM_VALID BIT(0)
  38. #define XSP_USBPHYACR0 ((SSUSB_SIFSLV_U2PHY_COM) + 0x00)
  39. #define P2A0_RG_INTR_EN BIT(5)
  40. #define XSP_USBPHYACR1 ((SSUSB_SIFSLV_U2PHY_COM) + 0x04)
  41. #define P2A1_RG_INTR_CAL GENMASK(23, 19)
  42. #define P2A1_RG_VRT_SEL GENMASK(14, 12)
  43. #define P2A1_RG_TERM_SEL GENMASK(10, 8)
  44. #define XSP_USBPHYACR5 ((SSUSB_SIFSLV_U2PHY_COM) + 0x014)
  45. #define P2A5_RG_HSTX_SRCAL_EN BIT(15)
  46. #define P2A5_RG_HSTX_SRCTRL GENMASK(14, 12)
  47. #define XSP_USBPHYACR6 ((SSUSB_SIFSLV_U2PHY_COM) + 0x018)
  48. #define P2A6_RG_BC11_SW_EN BIT(23)
  49. #define P2A6_RG_OTG_VBUSCMP_EN BIT(20)
  50. #define XSP_U2PHYDTM1 ((SSUSB_SIFSLV_U2PHY_COM) + 0x06C)
  51. #define P2D_FORCE_IDDIG BIT(9)
  52. #define P2D_RG_VBUSVALID BIT(5)
  53. #define P2D_RG_SESSEND BIT(4)
  54. #define P2D_RG_AVALID BIT(2)
  55. #define P2D_RG_IDDIG BIT(1)
  56. #define SSPXTP_PHYA_GLB_00 ((SSPXTP_SIFSLV_PHYA_GLB) + 0x00)
  57. #define RG_XTP_GLB_BIAS_INTR_CTRL GENMASK(21, 16)
  58. #define SSPXTP_PHYA_LN_04 ((SSPXTP_SIFSLV_PHYA_LN) + 0x04)
  59. #define RG_XTP_LN0_TX_IMPSEL GENMASK(4, 0)
  60. #define SSPXTP_PHYA_LN_14 ((SSPXTP_SIFSLV_PHYA_LN) + 0x014)
  61. #define RG_XTP_LN0_RX_IMPSEL GENMASK(4, 0)
  62. #define XSP_REF_CLK 26 /* MHZ */
  63. #define XSP_SLEW_RATE_COEF 17
  64. #define XSP_SR_COEF_DIVISOR 1000
  65. #define XSP_FM_DET_CYCLE_CNT 1024
  66. struct xsphy_instance {
  67. struct phy *phy;
  68. void __iomem *port_base;
  69. struct clk *ref_clk; /* reference clock of anolog phy */
  70. u32 index;
  71. u32 type;
  72. /* only for HQA test */
  73. int efuse_intr;
  74. int efuse_tx_imp;
  75. int efuse_rx_imp;
  76. /* u2 eye diagram */
  77. int eye_src;
  78. int eye_vrt;
  79. int eye_term;
  80. };
  81. struct mtk_xsphy {
  82. struct device *dev;
  83. void __iomem *glb_base; /* only shared u3 sif */
  84. struct xsphy_instance **phys;
  85. int nphys;
  86. int src_ref_clk; /* MHZ, reference clock for slew rate calibrate */
  87. int src_coef; /* coefficient for slew rate calibrate */
  88. };
  89. static void u2_phy_slew_rate_calibrate(struct mtk_xsphy *xsphy,
  90. struct xsphy_instance *inst)
  91. {
  92. void __iomem *pbase = inst->port_base;
  93. int calib_val;
  94. int fm_out;
  95. u32 tmp;
  96. /* use force value */
  97. if (inst->eye_src)
  98. return;
  99. /* enable USB ring oscillator */
  100. mtk_phy_set_bits(pbase + XSP_USBPHYACR5, P2A5_RG_HSTX_SRCAL_EN);
  101. udelay(1); /* wait clock stable */
  102. /* enable free run clock */
  103. mtk_phy_set_bits(pbase + XSP_U2FREQ_FMMONR1, P2F_RG_FRCK_EN);
  104. /* set cycle count as 1024 */
  105. mtk_phy_update_field(pbase + XSP_U2FREQ_FMCR0, P2F_RG_CYCLECNT,
  106. XSP_FM_DET_CYCLE_CNT);
  107. /* enable frequency meter */
  108. mtk_phy_set_bits(pbase + XSP_U2FREQ_FMCR0, P2F_RG_FREQDET_EN);
  109. /* ignore return value */
  110. readl_poll_timeout(pbase + XSP_U2FREQ_FMMONR1, tmp,
  111. (tmp & P2F_USB_FM_VALID), 10, 200);
  112. fm_out = readl(pbase + XSP_U2FREQ_MMONR0);
  113. /* disable frequency meter */
  114. mtk_phy_clear_bits(pbase + XSP_U2FREQ_FMCR0, P2F_RG_FREQDET_EN);
  115. /* disable free run clock */
  116. mtk_phy_clear_bits(pbase + XSP_U2FREQ_FMMONR1, P2F_RG_FRCK_EN);
  117. if (fm_out) {
  118. /* (1024 / FM_OUT) x reference clock frequency x coefficient */
  119. tmp = xsphy->src_ref_clk * xsphy->src_coef;
  120. tmp = (tmp * XSP_FM_DET_CYCLE_CNT) / fm_out;
  121. calib_val = DIV_ROUND_CLOSEST(tmp, XSP_SR_COEF_DIVISOR);
  122. } else {
  123. /* if FM detection fail, set default value */
  124. calib_val = 3;
  125. }
  126. dev_dbg(xsphy->dev, "phy.%d, fm_out:%d, calib:%d (clk:%d, coef:%d)\n",
  127. inst->index, fm_out, calib_val,
  128. xsphy->src_ref_clk, xsphy->src_coef);
  129. /* set HS slew rate */
  130. mtk_phy_update_field(pbase + XSP_USBPHYACR5, P2A5_RG_HSTX_SRCTRL, calib_val);
  131. /* disable USB ring oscillator */
  132. mtk_phy_clear_bits(pbase + XSP_USBPHYACR5, P2A5_RG_HSTX_SRCAL_EN);
  133. }
  134. static void u2_phy_instance_init(struct mtk_xsphy *xsphy,
  135. struct xsphy_instance *inst)
  136. {
  137. void __iomem *pbase = inst->port_base;
  138. /* DP/DM BC1.1 path Disable */
  139. mtk_phy_clear_bits(pbase + XSP_USBPHYACR6, P2A6_RG_BC11_SW_EN);
  140. mtk_phy_set_bits(pbase + XSP_USBPHYACR0, P2A0_RG_INTR_EN);
  141. }
  142. static void u2_phy_instance_power_on(struct mtk_xsphy *xsphy,
  143. struct xsphy_instance *inst)
  144. {
  145. void __iomem *pbase = inst->port_base;
  146. u32 index = inst->index;
  147. mtk_phy_set_bits(pbase + XSP_USBPHYACR6, P2A6_RG_OTG_VBUSCMP_EN);
  148. mtk_phy_update_bits(pbase + XSP_U2PHYDTM1,
  149. P2D_RG_VBUSVALID | P2D_RG_AVALID | P2D_RG_SESSEND,
  150. P2D_RG_VBUSVALID | P2D_RG_AVALID);
  151. dev_dbg(xsphy->dev, "%s(%d)\n", __func__, index);
  152. }
  153. static void u2_phy_instance_power_off(struct mtk_xsphy *xsphy,
  154. struct xsphy_instance *inst)
  155. {
  156. void __iomem *pbase = inst->port_base;
  157. u32 index = inst->index;
  158. mtk_phy_clear_bits(pbase + XSP_USBPHYACR6, P2A6_RG_OTG_VBUSCMP_EN);
  159. mtk_phy_update_bits(pbase + XSP_U2PHYDTM1,
  160. P2D_RG_VBUSVALID | P2D_RG_AVALID | P2D_RG_SESSEND,
  161. P2D_RG_SESSEND);
  162. dev_dbg(xsphy->dev, "%s(%d)\n", __func__, index);
  163. }
  164. static void u2_phy_instance_set_mode(struct mtk_xsphy *xsphy,
  165. struct xsphy_instance *inst,
  166. enum phy_mode mode)
  167. {
  168. u32 tmp;
  169. tmp = readl(inst->port_base + XSP_U2PHYDTM1);
  170. switch (mode) {
  171. case PHY_MODE_USB_DEVICE:
  172. tmp |= P2D_FORCE_IDDIG | P2D_RG_IDDIG;
  173. break;
  174. case PHY_MODE_USB_HOST:
  175. tmp |= P2D_FORCE_IDDIG;
  176. tmp &= ~P2D_RG_IDDIG;
  177. break;
  178. case PHY_MODE_USB_OTG:
  179. tmp &= ~(P2D_FORCE_IDDIG | P2D_RG_IDDIG);
  180. break;
  181. default:
  182. return;
  183. }
  184. writel(tmp, inst->port_base + XSP_U2PHYDTM1);
  185. }
  186. static void phy_parse_property(struct mtk_xsphy *xsphy,
  187. struct xsphy_instance *inst)
  188. {
  189. struct device *dev = &inst->phy->dev;
  190. switch (inst->type) {
  191. case PHY_TYPE_USB2:
  192. device_property_read_u32(dev, "mediatek,efuse-intr",
  193. &inst->efuse_intr);
  194. device_property_read_u32(dev, "mediatek,eye-src",
  195. &inst->eye_src);
  196. device_property_read_u32(dev, "mediatek,eye-vrt",
  197. &inst->eye_vrt);
  198. device_property_read_u32(dev, "mediatek,eye-term",
  199. &inst->eye_term);
  200. dev_dbg(dev, "intr:%d, src:%d, vrt:%d, term:%d\n",
  201. inst->efuse_intr, inst->eye_src,
  202. inst->eye_vrt, inst->eye_term);
  203. break;
  204. case PHY_TYPE_USB3:
  205. device_property_read_u32(dev, "mediatek,efuse-intr",
  206. &inst->efuse_intr);
  207. device_property_read_u32(dev, "mediatek,efuse-tx-imp",
  208. &inst->efuse_tx_imp);
  209. device_property_read_u32(dev, "mediatek,efuse-rx-imp",
  210. &inst->efuse_rx_imp);
  211. dev_dbg(dev, "intr:%d, tx-imp:%d, rx-imp:%d\n",
  212. inst->efuse_intr, inst->efuse_tx_imp,
  213. inst->efuse_rx_imp);
  214. break;
  215. default:
  216. dev_err(xsphy->dev, "incompatible phy type\n");
  217. return;
  218. }
  219. }
  220. static void u2_phy_props_set(struct mtk_xsphy *xsphy,
  221. struct xsphy_instance *inst)
  222. {
  223. void __iomem *pbase = inst->port_base;
  224. if (inst->efuse_intr)
  225. mtk_phy_update_field(pbase + XSP_USBPHYACR1, P2A1_RG_INTR_CAL,
  226. inst->efuse_intr);
  227. if (inst->eye_src)
  228. mtk_phy_update_field(pbase + XSP_USBPHYACR5, P2A5_RG_HSTX_SRCTRL,
  229. inst->eye_src);
  230. if (inst->eye_vrt)
  231. mtk_phy_update_field(pbase + XSP_USBPHYACR1, P2A1_RG_VRT_SEL,
  232. inst->eye_vrt);
  233. if (inst->eye_term)
  234. mtk_phy_update_field(pbase + XSP_USBPHYACR1, P2A1_RG_TERM_SEL,
  235. inst->eye_term);
  236. }
  237. static void u3_phy_props_set(struct mtk_xsphy *xsphy,
  238. struct xsphy_instance *inst)
  239. {
  240. void __iomem *pbase = inst->port_base;
  241. if (inst->efuse_intr)
  242. mtk_phy_update_field(xsphy->glb_base + SSPXTP_PHYA_GLB_00,
  243. RG_XTP_GLB_BIAS_INTR_CTRL, inst->efuse_intr);
  244. if (inst->efuse_tx_imp)
  245. mtk_phy_update_field(pbase + SSPXTP_PHYA_LN_04,
  246. RG_XTP_LN0_TX_IMPSEL, inst->efuse_tx_imp);
  247. if (inst->efuse_rx_imp)
  248. mtk_phy_update_field(pbase + SSPXTP_PHYA_LN_14,
  249. RG_XTP_LN0_RX_IMPSEL, inst->efuse_rx_imp);
  250. }
  251. static int mtk_phy_init(struct phy *phy)
  252. {
  253. struct xsphy_instance *inst = phy_get_drvdata(phy);
  254. struct mtk_xsphy *xsphy = dev_get_drvdata(phy->dev.parent);
  255. int ret;
  256. ret = clk_prepare_enable(inst->ref_clk);
  257. if (ret) {
  258. dev_err(xsphy->dev, "failed to enable ref_clk\n");
  259. return ret;
  260. }
  261. switch (inst->type) {
  262. case PHY_TYPE_USB2:
  263. u2_phy_instance_init(xsphy, inst);
  264. u2_phy_props_set(xsphy, inst);
  265. break;
  266. case PHY_TYPE_USB3:
  267. u3_phy_props_set(xsphy, inst);
  268. break;
  269. default:
  270. dev_err(xsphy->dev, "incompatible phy type\n");
  271. clk_disable_unprepare(inst->ref_clk);
  272. return -EINVAL;
  273. }
  274. return 0;
  275. }
  276. static int mtk_phy_power_on(struct phy *phy)
  277. {
  278. struct xsphy_instance *inst = phy_get_drvdata(phy);
  279. struct mtk_xsphy *xsphy = dev_get_drvdata(phy->dev.parent);
  280. if (inst->type == PHY_TYPE_USB2) {
  281. u2_phy_instance_power_on(xsphy, inst);
  282. u2_phy_slew_rate_calibrate(xsphy, inst);
  283. }
  284. return 0;
  285. }
  286. static int mtk_phy_power_off(struct phy *phy)
  287. {
  288. struct xsphy_instance *inst = phy_get_drvdata(phy);
  289. struct mtk_xsphy *xsphy = dev_get_drvdata(phy->dev.parent);
  290. if (inst->type == PHY_TYPE_USB2)
  291. u2_phy_instance_power_off(xsphy, inst);
  292. return 0;
  293. }
  294. static int mtk_phy_exit(struct phy *phy)
  295. {
  296. struct xsphy_instance *inst = phy_get_drvdata(phy);
  297. clk_disable_unprepare(inst->ref_clk);
  298. return 0;
  299. }
  300. static int mtk_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
  301. {
  302. struct xsphy_instance *inst = phy_get_drvdata(phy);
  303. struct mtk_xsphy *xsphy = dev_get_drvdata(phy->dev.parent);
  304. if (inst->type == PHY_TYPE_USB2)
  305. u2_phy_instance_set_mode(xsphy, inst, mode);
  306. return 0;
  307. }
  308. static struct phy *mtk_phy_xlate(struct device *dev,
  309. struct of_phandle_args *args)
  310. {
  311. struct mtk_xsphy *xsphy = dev_get_drvdata(dev);
  312. struct xsphy_instance *inst = NULL;
  313. struct device_node *phy_np = args->np;
  314. int index;
  315. if (args->args_count != 1) {
  316. dev_err(dev, "invalid number of cells in 'phy' property\n");
  317. return ERR_PTR(-EINVAL);
  318. }
  319. for (index = 0; index < xsphy->nphys; index++)
  320. if (phy_np == xsphy->phys[index]->phy->dev.of_node) {
  321. inst = xsphy->phys[index];
  322. break;
  323. }
  324. if (!inst) {
  325. dev_err(dev, "failed to find appropriate phy\n");
  326. return ERR_PTR(-EINVAL);
  327. }
  328. inst->type = args->args[0];
  329. if (!(inst->type == PHY_TYPE_USB2 ||
  330. inst->type == PHY_TYPE_USB3)) {
  331. dev_err(dev, "unsupported phy type: %d\n", inst->type);
  332. return ERR_PTR(-EINVAL);
  333. }
  334. phy_parse_property(xsphy, inst);
  335. return inst->phy;
  336. }
  337. static const struct phy_ops mtk_xsphy_ops = {
  338. .init = mtk_phy_init,
  339. .exit = mtk_phy_exit,
  340. .power_on = mtk_phy_power_on,
  341. .power_off = mtk_phy_power_off,
  342. .set_mode = mtk_phy_set_mode,
  343. .owner = THIS_MODULE,
  344. };
  345. static const struct of_device_id mtk_xsphy_id_table[] = {
  346. { .compatible = "mediatek,xsphy", },
  347. { },
  348. };
  349. MODULE_DEVICE_TABLE(of, mtk_xsphy_id_table);
  350. static int mtk_xsphy_probe(struct platform_device *pdev)
  351. {
  352. struct device *dev = &pdev->dev;
  353. struct device_node *np = dev->of_node;
  354. struct device_node *child_np;
  355. struct phy_provider *provider;
  356. struct resource *glb_res;
  357. struct mtk_xsphy *xsphy;
  358. struct resource res;
  359. int port, retval;
  360. xsphy = devm_kzalloc(dev, sizeof(*xsphy), GFP_KERNEL);
  361. if (!xsphy)
  362. return -ENOMEM;
  363. xsphy->nphys = of_get_child_count(np);
  364. xsphy->phys = devm_kcalloc(dev, xsphy->nphys,
  365. sizeof(*xsphy->phys), GFP_KERNEL);
  366. if (!xsphy->phys)
  367. return -ENOMEM;
  368. xsphy->dev = dev;
  369. platform_set_drvdata(pdev, xsphy);
  370. glb_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  371. /* optional, may not exist if no u3 phys */
  372. if (glb_res) {
  373. /* get banks shared by multiple u3 phys */
  374. xsphy->glb_base = devm_ioremap_resource(dev, glb_res);
  375. if (IS_ERR(xsphy->glb_base)) {
  376. dev_err(dev, "failed to remap glb regs\n");
  377. return PTR_ERR(xsphy->glb_base);
  378. }
  379. }
  380. xsphy->src_ref_clk = XSP_REF_CLK;
  381. xsphy->src_coef = XSP_SLEW_RATE_COEF;
  382. /* update parameters of slew rate calibrate if exist */
  383. device_property_read_u32(dev, "mediatek,src-ref-clk-mhz",
  384. &xsphy->src_ref_clk);
  385. device_property_read_u32(dev, "mediatek,src-coef", &xsphy->src_coef);
  386. port = 0;
  387. for_each_child_of_node(np, child_np) {
  388. struct xsphy_instance *inst;
  389. struct phy *phy;
  390. inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
  391. if (!inst) {
  392. retval = -ENOMEM;
  393. goto put_child;
  394. }
  395. xsphy->phys[port] = inst;
  396. phy = devm_phy_create(dev, child_np, &mtk_xsphy_ops);
  397. if (IS_ERR(phy)) {
  398. dev_err(dev, "failed to create phy\n");
  399. retval = PTR_ERR(phy);
  400. goto put_child;
  401. }
  402. retval = of_address_to_resource(child_np, 0, &res);
  403. if (retval) {
  404. dev_err(dev, "failed to get address resource(id-%d)\n",
  405. port);
  406. goto put_child;
  407. }
  408. inst->port_base = devm_ioremap_resource(&phy->dev, &res);
  409. if (IS_ERR(inst->port_base)) {
  410. dev_err(dev, "failed to remap phy regs\n");
  411. retval = PTR_ERR(inst->port_base);
  412. goto put_child;
  413. }
  414. inst->phy = phy;
  415. inst->index = port;
  416. phy_set_drvdata(phy, inst);
  417. port++;
  418. inst->ref_clk = devm_clk_get(&phy->dev, "ref");
  419. if (IS_ERR(inst->ref_clk)) {
  420. dev_err(dev, "failed to get ref_clk(id-%d)\n", port);
  421. retval = PTR_ERR(inst->ref_clk);
  422. goto put_child;
  423. }
  424. }
  425. provider = devm_of_phy_provider_register(dev, mtk_phy_xlate);
  426. return PTR_ERR_OR_ZERO(provider);
  427. put_child:
  428. of_node_put(child_np);
  429. return retval;
  430. }
  431. static struct platform_driver mtk_xsphy_driver = {
  432. .probe = mtk_xsphy_probe,
  433. .driver = {
  434. .name = "mtk-xsphy",
  435. .of_match_table = mtk_xsphy_id_table,
  436. },
  437. };
  438. module_platform_driver(mtk_xsphy_driver);
  439. MODULE_AUTHOR("Chunfeng Yun <[email protected]>");
  440. MODULE_DESCRIPTION("MediaTek USB XS-PHY driver");
  441. MODULE_LICENSE("GPL v2");