phy-rockchip-naneng-combphy.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Rockchip PIPE USB3.0 PCIE SATA Combo Phy driver
  4. *
  5. * Copyright (C) 2021 Rockchip Electronics Co., Ltd.
  6. */
  7. #include <dt-bindings/phy/phy.h>
  8. #include <linux/clk.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/of.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/reset.h>
  15. #include <linux/units.h>
  16. #define BIT_WRITEABLE_SHIFT 16
  17. #define REF_CLOCK_24MHz (24 * HZ_PER_MHZ)
  18. #define REF_CLOCK_25MHz (25 * HZ_PER_MHZ)
  19. #define REF_CLOCK_100MHz (100 * HZ_PER_MHZ)
  20. /* COMBO PHY REG */
  21. #define PHYREG6 0x14
  22. #define PHYREG6_PLL_DIV_MASK GENMASK(7, 6)
  23. #define PHYREG6_PLL_DIV_SHIFT 6
  24. #define PHYREG6_PLL_DIV_2 1
  25. #define PHYREG7 0x18
  26. #define PHYREG7_TX_RTERM_MASK GENMASK(7, 4)
  27. #define PHYREG7_TX_RTERM_SHIFT 4
  28. #define PHYREG7_TX_RTERM_50OHM 8
  29. #define PHYREG7_RX_RTERM_MASK GENMASK(3, 0)
  30. #define PHYREG7_RX_RTERM_SHIFT 0
  31. #define PHYREG7_RX_RTERM_44OHM 15
  32. #define PHYREG8 0x1C
  33. #define PHYREG8_SSC_EN BIT(4)
  34. #define PHYREG11 0x28
  35. #define PHYREG11_SU_TRIM_0_7 0xF0
  36. #define PHYREG12 0x2C
  37. #define PHYREG12_PLL_LPF_ADJ_VALUE 4
  38. #define PHYREG13 0x30
  39. #define PHYREG13_RESISTER_MASK GENMASK(5, 4)
  40. #define PHYREG13_RESISTER_SHIFT 0x4
  41. #define PHYREG13_RESISTER_HIGH_Z 3
  42. #define PHYREG13_CKRCV_AMP0 BIT(7)
  43. #define PHYREG14 0x34
  44. #define PHYREG14_CKRCV_AMP1 BIT(0)
  45. #define PHYREG15 0x38
  46. #define PHYREG15_CTLE_EN BIT(0)
  47. #define PHYREG15_SSC_CNT_MASK GENMASK(7, 6)
  48. #define PHYREG15_SSC_CNT_SHIFT 6
  49. #define PHYREG15_SSC_CNT_VALUE 1
  50. #define PHYREG16 0x3C
  51. #define PHYREG16_SSC_CNT_VALUE 0x5f
  52. #define PHYREG18 0x44
  53. #define PHYREG18_PLL_LOOP 0x32
  54. #define PHYREG27 0x6C
  55. #define PHYREG27_RX_TRIM_RK3588 0x4C
  56. #define PHYREG32 0x7C
  57. #define PHYREG32_SSC_MASK GENMASK(7, 4)
  58. #define PHYREG32_SSC_DIR_SHIFT 4
  59. #define PHYREG32_SSC_UPWARD 0
  60. #define PHYREG32_SSC_DOWNWARD 1
  61. #define PHYREG32_SSC_OFFSET_SHIFT 6
  62. #define PHYREG32_SSC_OFFSET_500PPM 1
  63. #define PHYREG33 0x80
  64. #define PHYREG33_PLL_KVCO_MASK GENMASK(4, 2)
  65. #define PHYREG33_PLL_KVCO_SHIFT 2
  66. #define PHYREG33_PLL_KVCO_VALUE 2
  67. struct rockchip_combphy_priv;
  68. struct combphy_reg {
  69. u16 offset;
  70. u16 bitend;
  71. u16 bitstart;
  72. u16 disable;
  73. u16 enable;
  74. };
  75. struct rockchip_combphy_grfcfg {
  76. struct combphy_reg pcie_mode_set;
  77. struct combphy_reg usb_mode_set;
  78. struct combphy_reg sgmii_mode_set;
  79. struct combphy_reg qsgmii_mode_set;
  80. struct combphy_reg pipe_rxterm_set;
  81. struct combphy_reg pipe_txelec_set;
  82. struct combphy_reg pipe_txcomp_set;
  83. struct combphy_reg pipe_clk_25m;
  84. struct combphy_reg pipe_clk_100m;
  85. struct combphy_reg pipe_phymode_sel;
  86. struct combphy_reg pipe_rate_sel;
  87. struct combphy_reg pipe_rxterm_sel;
  88. struct combphy_reg pipe_txelec_sel;
  89. struct combphy_reg pipe_txcomp_sel;
  90. struct combphy_reg pipe_clk_ext;
  91. struct combphy_reg pipe_sel_usb;
  92. struct combphy_reg pipe_sel_qsgmii;
  93. struct combphy_reg pipe_phy_status;
  94. struct combphy_reg con0_for_pcie;
  95. struct combphy_reg con1_for_pcie;
  96. struct combphy_reg con2_for_pcie;
  97. struct combphy_reg con3_for_pcie;
  98. struct combphy_reg con0_for_sata;
  99. struct combphy_reg con1_for_sata;
  100. struct combphy_reg con2_for_sata;
  101. struct combphy_reg con3_for_sata;
  102. struct combphy_reg pipe_con0_for_sata;
  103. struct combphy_reg pipe_con1_for_sata;
  104. struct combphy_reg pipe_xpcs_phy_ready;
  105. struct combphy_reg pipe_pcie1l0_sel;
  106. struct combphy_reg pipe_pcie1l1_sel;
  107. struct combphy_reg u3otg0_port_en;
  108. struct combphy_reg u3otg1_port_en;
  109. };
  110. struct rockchip_combphy_cfg {
  111. unsigned int num_phys;
  112. unsigned int phy_ids[3];
  113. const struct rockchip_combphy_grfcfg *grfcfg;
  114. int (*combphy_cfg)(struct rockchip_combphy_priv *priv);
  115. };
  116. struct rockchip_combphy_priv {
  117. u8 type;
  118. int id;
  119. void __iomem *mmio;
  120. int num_clks;
  121. struct clk_bulk_data *clks;
  122. struct device *dev;
  123. struct regmap *pipe_grf;
  124. struct regmap *phy_grf;
  125. struct phy *phy;
  126. struct reset_control *phy_rst;
  127. const struct rockchip_combphy_cfg *cfg;
  128. bool enable_ssc;
  129. bool ext_refclk;
  130. struct clk *refclk;
  131. };
  132. static void rockchip_combphy_updatel(struct rockchip_combphy_priv *priv,
  133. int mask, int val, int reg)
  134. {
  135. unsigned int temp;
  136. temp = readl(priv->mmio + reg);
  137. temp = (temp & ~(mask)) | val;
  138. writel(temp, priv->mmio + reg);
  139. }
  140. static int rockchip_combphy_param_write(struct regmap *base,
  141. const struct combphy_reg *reg, bool en)
  142. {
  143. u32 val, mask, tmp;
  144. tmp = en ? reg->enable : reg->disable;
  145. mask = GENMASK(reg->bitend, reg->bitstart);
  146. val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
  147. return regmap_write(base, reg->offset, val);
  148. }
  149. static u32 rockchip_combphy_is_ready(struct rockchip_combphy_priv *priv)
  150. {
  151. const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
  152. u32 mask, val;
  153. mask = GENMASK(cfg->pipe_phy_status.bitend,
  154. cfg->pipe_phy_status.bitstart);
  155. regmap_read(priv->phy_grf, cfg->pipe_phy_status.offset, &val);
  156. val = (val & mask) >> cfg->pipe_phy_status.bitstart;
  157. return val;
  158. }
  159. static int rockchip_combphy_init(struct phy *phy)
  160. {
  161. struct rockchip_combphy_priv *priv = phy_get_drvdata(phy);
  162. const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
  163. u32 val;
  164. int ret;
  165. ret = clk_bulk_prepare_enable(priv->num_clks, priv->clks);
  166. if (ret) {
  167. dev_err(priv->dev, "failed to enable clks\n");
  168. return ret;
  169. }
  170. switch (priv->type) {
  171. case PHY_TYPE_PCIE:
  172. case PHY_TYPE_USB3:
  173. case PHY_TYPE_SATA:
  174. case PHY_TYPE_SGMII:
  175. case PHY_TYPE_QSGMII:
  176. if (priv->cfg->combphy_cfg)
  177. ret = priv->cfg->combphy_cfg(priv);
  178. break;
  179. default:
  180. dev_err(priv->dev, "incompatible PHY type\n");
  181. ret = -EINVAL;
  182. break;
  183. }
  184. if (ret) {
  185. dev_err(priv->dev, "failed to init phy for phy type %x\n", priv->type);
  186. goto err_clk;
  187. }
  188. ret = reset_control_deassert(priv->phy_rst);
  189. if (ret)
  190. goto err_clk;
  191. if (priv->type == PHY_TYPE_USB3) {
  192. ret = readx_poll_timeout_atomic(rockchip_combphy_is_ready,
  193. priv, val,
  194. val == cfg->pipe_phy_status.enable,
  195. 10, 1000);
  196. if (ret)
  197. dev_warn(priv->dev, "wait phy status ready timeout\n");
  198. }
  199. return 0;
  200. err_clk:
  201. clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
  202. return ret;
  203. }
  204. static int rockchip_combphy_exit(struct phy *phy)
  205. {
  206. struct rockchip_combphy_priv *priv = phy_get_drvdata(phy);
  207. clk_bulk_disable_unprepare(priv->num_clks, priv->clks);
  208. reset_control_assert(priv->phy_rst);
  209. return 0;
  210. }
  211. static const struct phy_ops rockchip_combphy_ops = {
  212. .init = rockchip_combphy_init,
  213. .exit = rockchip_combphy_exit,
  214. .owner = THIS_MODULE,
  215. };
  216. static struct phy *rockchip_combphy_xlate(struct device *dev, const struct of_phandle_args *args)
  217. {
  218. struct rockchip_combphy_priv *priv = dev_get_drvdata(dev);
  219. if (args->args_count != 1) {
  220. dev_err(dev, "invalid number of arguments\n");
  221. return ERR_PTR(-EINVAL);
  222. }
  223. if (priv->type != PHY_NONE && priv->type != args->args[0])
  224. dev_warn(dev, "phy type select %d overwriting type %d\n",
  225. args->args[0], priv->type);
  226. priv->type = args->args[0];
  227. return priv->phy;
  228. }
  229. static int rockchip_combphy_parse_dt(struct device *dev, struct rockchip_combphy_priv *priv)
  230. {
  231. int i;
  232. priv->num_clks = devm_clk_bulk_get_all(dev, &priv->clks);
  233. if (priv->num_clks < 1)
  234. return -EINVAL;
  235. priv->refclk = NULL;
  236. for (i = 0; i < priv->num_clks; i++) {
  237. if (!strncmp(priv->clks[i].id, "ref", 3)) {
  238. priv->refclk = priv->clks[i].clk;
  239. break;
  240. }
  241. }
  242. if (!priv->refclk) {
  243. dev_err(dev, "no refclk found\n");
  244. return -EINVAL;
  245. }
  246. priv->pipe_grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,pipe-grf");
  247. if (IS_ERR(priv->pipe_grf)) {
  248. dev_err(dev, "failed to find peri_ctrl pipe-grf regmap\n");
  249. return PTR_ERR(priv->pipe_grf);
  250. }
  251. priv->phy_grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,pipe-phy-grf");
  252. if (IS_ERR(priv->phy_grf)) {
  253. dev_err(dev, "failed to find peri_ctrl pipe-phy-grf regmap\n");
  254. return PTR_ERR(priv->phy_grf);
  255. }
  256. priv->enable_ssc = device_property_present(dev, "rockchip,enable-ssc");
  257. priv->ext_refclk = device_property_present(dev, "rockchip,ext-refclk");
  258. priv->phy_rst = devm_reset_control_get_exclusive(dev, "phy");
  259. /* fallback to old behaviour */
  260. if (PTR_ERR(priv->phy_rst) == -ENOENT)
  261. priv->phy_rst = devm_reset_control_array_get_exclusive(dev);
  262. if (IS_ERR(priv->phy_rst))
  263. return dev_err_probe(dev, PTR_ERR(priv->phy_rst), "failed to get phy reset\n");
  264. return 0;
  265. }
  266. static int rockchip_combphy_probe(struct platform_device *pdev)
  267. {
  268. struct phy_provider *phy_provider;
  269. struct device *dev = &pdev->dev;
  270. struct rockchip_combphy_priv *priv;
  271. const struct rockchip_combphy_cfg *phy_cfg;
  272. struct resource *res;
  273. int ret, id;
  274. phy_cfg = of_device_get_match_data(dev);
  275. if (!phy_cfg) {
  276. dev_err(dev, "no OF match data provided\n");
  277. return -EINVAL;
  278. }
  279. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  280. if (!priv)
  281. return -ENOMEM;
  282. priv->mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  283. if (IS_ERR(priv->mmio)) {
  284. ret = PTR_ERR(priv->mmio);
  285. return ret;
  286. }
  287. /* find the phy-id from the io address */
  288. priv->id = -ENODEV;
  289. for (id = 0; id < phy_cfg->num_phys; id++) {
  290. if (res->start == phy_cfg->phy_ids[id]) {
  291. priv->id = id;
  292. break;
  293. }
  294. }
  295. priv->dev = dev;
  296. priv->type = PHY_NONE;
  297. priv->cfg = phy_cfg;
  298. ret = rockchip_combphy_parse_dt(dev, priv);
  299. if (ret)
  300. return ret;
  301. ret = reset_control_assert(priv->phy_rst);
  302. if (ret) {
  303. dev_err(dev, "failed to reset phy\n");
  304. return ret;
  305. }
  306. priv->phy = devm_phy_create(dev, NULL, &rockchip_combphy_ops);
  307. if (IS_ERR(priv->phy)) {
  308. dev_err(dev, "failed to create combphy\n");
  309. return PTR_ERR(priv->phy);
  310. }
  311. dev_set_drvdata(dev, priv);
  312. phy_set_drvdata(priv->phy, priv);
  313. phy_provider = devm_of_phy_provider_register(dev, rockchip_combphy_xlate);
  314. return PTR_ERR_OR_ZERO(phy_provider);
  315. }
  316. static int rk3568_combphy_cfg(struct rockchip_combphy_priv *priv)
  317. {
  318. const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
  319. unsigned long rate;
  320. u32 val;
  321. switch (priv->type) {
  322. case PHY_TYPE_PCIE:
  323. /* Set SSC downward spread spectrum. */
  324. rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK,
  325. PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT,
  326. PHYREG32);
  327. rockchip_combphy_param_write(priv->phy_grf, &cfg->con0_for_pcie, true);
  328. rockchip_combphy_param_write(priv->phy_grf, &cfg->con1_for_pcie, true);
  329. rockchip_combphy_param_write(priv->phy_grf, &cfg->con2_for_pcie, true);
  330. rockchip_combphy_param_write(priv->phy_grf, &cfg->con3_for_pcie, true);
  331. break;
  332. case PHY_TYPE_USB3:
  333. /* Set SSC downward spread spectrum. */
  334. rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK,
  335. PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT,
  336. PHYREG32);
  337. /* Enable adaptive CTLE for USB3.0 Rx. */
  338. val = readl(priv->mmio + PHYREG15);
  339. val |= PHYREG15_CTLE_EN;
  340. writel(val, priv->mmio + PHYREG15);
  341. /* Set PLL KVCO fine tuning signals. */
  342. rockchip_combphy_updatel(priv, PHYREG33_PLL_KVCO_MASK,
  343. PHYREG33_PLL_KVCO_VALUE << PHYREG33_PLL_KVCO_SHIFT,
  344. PHYREG33);
  345. /* Enable controlling random jitter. */
  346. writel(PHYREG12_PLL_LPF_ADJ_VALUE, priv->mmio + PHYREG12);
  347. /* Set PLL input clock divider 1/2. */
  348. rockchip_combphy_updatel(priv, PHYREG6_PLL_DIV_MASK,
  349. PHYREG6_PLL_DIV_2 << PHYREG6_PLL_DIV_SHIFT,
  350. PHYREG6);
  351. writel(PHYREG18_PLL_LOOP, priv->mmio + PHYREG18);
  352. writel(PHYREG11_SU_TRIM_0_7, priv->mmio + PHYREG11);
  353. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_sel_usb, true);
  354. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_txcomp_sel, false);
  355. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_txelec_sel, false);
  356. rockchip_combphy_param_write(priv->phy_grf, &cfg->usb_mode_set, true);
  357. switch (priv->id) {
  358. case 0:
  359. rockchip_combphy_param_write(priv->pipe_grf, &cfg->u3otg0_port_en, true);
  360. break;
  361. case 1:
  362. rockchip_combphy_param_write(priv->pipe_grf, &cfg->u3otg1_port_en, true);
  363. break;
  364. }
  365. break;
  366. case PHY_TYPE_SATA:
  367. /* Enable adaptive CTLE for SATA Rx. */
  368. val = readl(priv->mmio + PHYREG15);
  369. val |= PHYREG15_CTLE_EN;
  370. writel(val, priv->mmio + PHYREG15);
  371. /*
  372. * Set tx_rterm=50ohm and rx_rterm=44ohm for SATA.
  373. * 0: 60ohm, 8: 50ohm 15: 44ohm (by step abort 1ohm)
  374. */
  375. val = PHYREG7_TX_RTERM_50OHM << PHYREG7_TX_RTERM_SHIFT;
  376. val |= PHYREG7_RX_RTERM_44OHM << PHYREG7_RX_RTERM_SHIFT;
  377. writel(val, priv->mmio + PHYREG7);
  378. rockchip_combphy_param_write(priv->phy_grf, &cfg->con0_for_sata, true);
  379. rockchip_combphy_param_write(priv->phy_grf, &cfg->con1_for_sata, true);
  380. rockchip_combphy_param_write(priv->phy_grf, &cfg->con2_for_sata, true);
  381. rockchip_combphy_param_write(priv->phy_grf, &cfg->con3_for_sata, true);
  382. rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_con0_for_sata, true);
  383. break;
  384. case PHY_TYPE_SGMII:
  385. rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_xpcs_phy_ready, true);
  386. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_phymode_sel, true);
  387. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_sel_qsgmii, true);
  388. rockchip_combphy_param_write(priv->phy_grf, &cfg->sgmii_mode_set, true);
  389. break;
  390. case PHY_TYPE_QSGMII:
  391. rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_xpcs_phy_ready, true);
  392. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_phymode_sel, true);
  393. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_rate_sel, true);
  394. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_sel_qsgmii, true);
  395. rockchip_combphy_param_write(priv->phy_grf, &cfg->qsgmii_mode_set, true);
  396. break;
  397. default:
  398. dev_err(priv->dev, "incompatible PHY type\n");
  399. return -EINVAL;
  400. }
  401. rate = clk_get_rate(priv->refclk);
  402. switch (rate) {
  403. case REF_CLOCK_24MHz:
  404. if (priv->type == PHY_TYPE_USB3 || priv->type == PHY_TYPE_SATA) {
  405. /* Set ssc_cnt[9:0]=0101111101 & 31.5KHz. */
  406. val = PHYREG15_SSC_CNT_VALUE << PHYREG15_SSC_CNT_SHIFT;
  407. rockchip_combphy_updatel(priv, PHYREG15_SSC_CNT_MASK,
  408. val, PHYREG15);
  409. writel(PHYREG16_SSC_CNT_VALUE, priv->mmio + PHYREG16);
  410. }
  411. break;
  412. case REF_CLOCK_25MHz:
  413. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_25m, true);
  414. break;
  415. case REF_CLOCK_100MHz:
  416. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_100m, true);
  417. if (priv->type == PHY_TYPE_PCIE) {
  418. /* PLL KVCO fine tuning. */
  419. val = PHYREG33_PLL_KVCO_VALUE << PHYREG33_PLL_KVCO_SHIFT;
  420. rockchip_combphy_updatel(priv, PHYREG33_PLL_KVCO_MASK,
  421. val, PHYREG33);
  422. /* Enable controlling random jitter. */
  423. writel(PHYREG12_PLL_LPF_ADJ_VALUE, priv->mmio + PHYREG12);
  424. val = PHYREG6_PLL_DIV_2 << PHYREG6_PLL_DIV_SHIFT;
  425. rockchip_combphy_updatel(priv, PHYREG6_PLL_DIV_MASK,
  426. val, PHYREG6);
  427. writel(PHYREG18_PLL_LOOP, priv->mmio + PHYREG18);
  428. writel(PHYREG11_SU_TRIM_0_7, priv->mmio + PHYREG11);
  429. } else if (priv->type == PHY_TYPE_SATA) {
  430. /* downward spread spectrum +500ppm */
  431. val = PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT;
  432. val |= PHYREG32_SSC_OFFSET_500PPM << PHYREG32_SSC_OFFSET_SHIFT;
  433. rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK, val, PHYREG32);
  434. }
  435. break;
  436. default:
  437. dev_err(priv->dev, "unsupported rate: %lu\n", rate);
  438. return -EINVAL;
  439. }
  440. if (priv->ext_refclk) {
  441. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_ext, true);
  442. if (priv->type == PHY_TYPE_PCIE && rate == REF_CLOCK_100MHz) {
  443. val = PHYREG13_RESISTER_HIGH_Z << PHYREG13_RESISTER_SHIFT;
  444. val |= PHYREG13_CKRCV_AMP0;
  445. rockchip_combphy_updatel(priv, PHYREG13_RESISTER_MASK, val, PHYREG13);
  446. val = readl(priv->mmio + PHYREG14);
  447. val |= PHYREG14_CKRCV_AMP1;
  448. writel(val, priv->mmio + PHYREG14);
  449. }
  450. }
  451. if (priv->enable_ssc) {
  452. val = readl(priv->mmio + PHYREG8);
  453. val |= PHYREG8_SSC_EN;
  454. writel(val, priv->mmio + PHYREG8);
  455. }
  456. return 0;
  457. }
  458. static const struct rockchip_combphy_grfcfg rk3568_combphy_grfcfgs = {
  459. /* pipe-phy-grf */
  460. .pcie_mode_set = { 0x0000, 5, 0, 0x00, 0x11 },
  461. .usb_mode_set = { 0x0000, 5, 0, 0x00, 0x04 },
  462. .sgmii_mode_set = { 0x0000, 5, 0, 0x00, 0x01 },
  463. .qsgmii_mode_set = { 0x0000, 5, 0, 0x00, 0x21 },
  464. .pipe_rxterm_set = { 0x0000, 12, 12, 0x00, 0x01 },
  465. .pipe_txelec_set = { 0x0004, 1, 1, 0x00, 0x01 },
  466. .pipe_txcomp_set = { 0x0004, 4, 4, 0x00, 0x01 },
  467. .pipe_clk_25m = { 0x0004, 14, 13, 0x00, 0x01 },
  468. .pipe_clk_100m = { 0x0004, 14, 13, 0x00, 0x02 },
  469. .pipe_phymode_sel = { 0x0008, 1, 1, 0x00, 0x01 },
  470. .pipe_rate_sel = { 0x0008, 2, 2, 0x00, 0x01 },
  471. .pipe_rxterm_sel = { 0x0008, 8, 8, 0x00, 0x01 },
  472. .pipe_txelec_sel = { 0x0008, 12, 12, 0x00, 0x01 },
  473. .pipe_txcomp_sel = { 0x0008, 15, 15, 0x00, 0x01 },
  474. .pipe_clk_ext = { 0x000c, 9, 8, 0x02, 0x01 },
  475. .pipe_sel_usb = { 0x000c, 14, 13, 0x00, 0x01 },
  476. .pipe_sel_qsgmii = { 0x000c, 15, 13, 0x00, 0x07 },
  477. .pipe_phy_status = { 0x0034, 6, 6, 0x01, 0x00 },
  478. .con0_for_pcie = { 0x0000, 15, 0, 0x00, 0x1000 },
  479. .con1_for_pcie = { 0x0004, 15, 0, 0x00, 0x0000 },
  480. .con2_for_pcie = { 0x0008, 15, 0, 0x00, 0x0101 },
  481. .con3_for_pcie = { 0x000c, 15, 0, 0x00, 0x0200 },
  482. .con0_for_sata = { 0x0000, 15, 0, 0x00, 0x0119 },
  483. .con1_for_sata = { 0x0004, 15, 0, 0x00, 0x0040 },
  484. .con2_for_sata = { 0x0008, 15, 0, 0x00, 0x80c3 },
  485. .con3_for_sata = { 0x000c, 15, 0, 0x00, 0x4407 },
  486. /* pipe-grf */
  487. .pipe_con0_for_sata = { 0x0000, 15, 0, 0x00, 0x2220 },
  488. .pipe_xpcs_phy_ready = { 0x0040, 2, 2, 0x00, 0x01 },
  489. .u3otg0_port_en = { 0x0104, 15, 0, 0x0181, 0x1100 },
  490. .u3otg1_port_en = { 0x0144, 15, 0, 0x0181, 0x1100 },
  491. };
  492. static const struct rockchip_combphy_cfg rk3568_combphy_cfgs = {
  493. .num_phys = 3,
  494. .phy_ids = {
  495. 0xfe820000,
  496. 0xfe830000,
  497. 0xfe840000,
  498. },
  499. .grfcfg = &rk3568_combphy_grfcfgs,
  500. .combphy_cfg = rk3568_combphy_cfg,
  501. };
  502. static int rk3588_combphy_cfg(struct rockchip_combphy_priv *priv)
  503. {
  504. const struct rockchip_combphy_grfcfg *cfg = priv->cfg->grfcfg;
  505. unsigned long rate;
  506. u32 val;
  507. switch (priv->type) {
  508. case PHY_TYPE_PCIE:
  509. rockchip_combphy_param_write(priv->phy_grf, &cfg->con0_for_pcie, true);
  510. rockchip_combphy_param_write(priv->phy_grf, &cfg->con1_for_pcie, true);
  511. rockchip_combphy_param_write(priv->phy_grf, &cfg->con2_for_pcie, true);
  512. rockchip_combphy_param_write(priv->phy_grf, &cfg->con3_for_pcie, true);
  513. switch (priv->id) {
  514. case 1:
  515. rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_pcie1l0_sel, true);
  516. break;
  517. case 2:
  518. rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_pcie1l1_sel, true);
  519. break;
  520. }
  521. break;
  522. case PHY_TYPE_USB3:
  523. /* Set SSC downward spread spectrum */
  524. rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK,
  525. PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT,
  526. PHYREG32);
  527. /* Enable adaptive CTLE for USB3.0 Rx. */
  528. val = readl(priv->mmio + PHYREG15);
  529. val |= PHYREG15_CTLE_EN;
  530. writel(val, priv->mmio + PHYREG15);
  531. /* Set PLL KVCO fine tuning signals. */
  532. rockchip_combphy_updatel(priv, PHYREG33_PLL_KVCO_MASK,
  533. PHYREG33_PLL_KVCO_VALUE << PHYREG33_PLL_KVCO_SHIFT,
  534. PHYREG33);
  535. /* Enable controlling random jitter. */
  536. writel(PHYREG12_PLL_LPF_ADJ_VALUE, priv->mmio + PHYREG12);
  537. /* Set PLL input clock divider 1/2. */
  538. rockchip_combphy_updatel(priv, PHYREG6_PLL_DIV_MASK,
  539. PHYREG6_PLL_DIV_2 << PHYREG6_PLL_DIV_SHIFT,
  540. PHYREG6);
  541. writel(PHYREG18_PLL_LOOP, priv->mmio + PHYREG18);
  542. writel(PHYREG11_SU_TRIM_0_7, priv->mmio + PHYREG11);
  543. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_txcomp_sel, false);
  544. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_txelec_sel, false);
  545. rockchip_combphy_param_write(priv->phy_grf, &cfg->usb_mode_set, true);
  546. break;
  547. case PHY_TYPE_SATA:
  548. /* Enable adaptive CTLE for SATA Rx. */
  549. val = readl(priv->mmio + PHYREG15);
  550. val |= PHYREG15_CTLE_EN;
  551. writel(val, priv->mmio + PHYREG15);
  552. /*
  553. * Set tx_rterm=50ohm and rx_rterm=44ohm for SATA.
  554. * 0: 60ohm, 8: 50ohm 15: 44ohm (by step abort 1ohm)
  555. */
  556. val = PHYREG7_TX_RTERM_50OHM << PHYREG7_TX_RTERM_SHIFT;
  557. val |= PHYREG7_RX_RTERM_44OHM << PHYREG7_RX_RTERM_SHIFT;
  558. writel(val, priv->mmio + PHYREG7);
  559. rockchip_combphy_param_write(priv->phy_grf, &cfg->con0_for_sata, true);
  560. rockchip_combphy_param_write(priv->phy_grf, &cfg->con1_for_sata, true);
  561. rockchip_combphy_param_write(priv->phy_grf, &cfg->con2_for_sata, true);
  562. rockchip_combphy_param_write(priv->phy_grf, &cfg->con3_for_sata, true);
  563. rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_con0_for_sata, true);
  564. rockchip_combphy_param_write(priv->pipe_grf, &cfg->pipe_con1_for_sata, true);
  565. break;
  566. case PHY_TYPE_SGMII:
  567. case PHY_TYPE_QSGMII:
  568. default:
  569. dev_err(priv->dev, "incompatible PHY type\n");
  570. return -EINVAL;
  571. }
  572. rate = clk_get_rate(priv->refclk);
  573. switch (rate) {
  574. case REF_CLOCK_24MHz:
  575. if (priv->type == PHY_TYPE_USB3 || priv->type == PHY_TYPE_SATA) {
  576. /* Set ssc_cnt[9:0]=0101111101 & 31.5KHz. */
  577. val = PHYREG15_SSC_CNT_VALUE << PHYREG15_SSC_CNT_SHIFT;
  578. rockchip_combphy_updatel(priv, PHYREG15_SSC_CNT_MASK,
  579. val, PHYREG15);
  580. writel(PHYREG16_SSC_CNT_VALUE, priv->mmio + PHYREG16);
  581. }
  582. break;
  583. case REF_CLOCK_25MHz:
  584. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_25m, true);
  585. break;
  586. case REF_CLOCK_100MHz:
  587. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_100m, true);
  588. if (priv->type == PHY_TYPE_PCIE) {
  589. /* PLL KVCO fine tuning. */
  590. val = 4 << PHYREG33_PLL_KVCO_SHIFT;
  591. rockchip_combphy_updatel(priv, PHYREG33_PLL_KVCO_MASK,
  592. val, PHYREG33);
  593. /* Enable controlling random jitter. */
  594. writel(PHYREG12_PLL_LPF_ADJ_VALUE, priv->mmio + PHYREG12);
  595. /* Set up rx_trim: PLL LPF C1 85pf R1 1.25kohm */
  596. writel(PHYREG27_RX_TRIM_RK3588, priv->mmio + PHYREG27);
  597. /* Set up su_trim: */
  598. writel(PHYREG11_SU_TRIM_0_7, priv->mmio + PHYREG11);
  599. } else if (priv->type == PHY_TYPE_SATA) {
  600. /* downward spread spectrum +500ppm */
  601. val = PHYREG32_SSC_DOWNWARD << PHYREG32_SSC_DIR_SHIFT;
  602. val |= PHYREG32_SSC_OFFSET_500PPM << PHYREG32_SSC_OFFSET_SHIFT;
  603. rockchip_combphy_updatel(priv, PHYREG32_SSC_MASK, val, PHYREG32);
  604. }
  605. break;
  606. default:
  607. dev_err(priv->dev, "Unsupported rate: %lu\n", rate);
  608. return -EINVAL;
  609. }
  610. if (priv->ext_refclk) {
  611. rockchip_combphy_param_write(priv->phy_grf, &cfg->pipe_clk_ext, true);
  612. if (priv->type == PHY_TYPE_PCIE && rate == REF_CLOCK_100MHz) {
  613. val = PHYREG13_RESISTER_HIGH_Z << PHYREG13_RESISTER_SHIFT;
  614. val |= PHYREG13_CKRCV_AMP0;
  615. rockchip_combphy_updatel(priv, PHYREG13_RESISTER_MASK, val, PHYREG13);
  616. val = readl(priv->mmio + PHYREG14);
  617. val |= PHYREG14_CKRCV_AMP1;
  618. writel(val, priv->mmio + PHYREG14);
  619. }
  620. }
  621. if (priv->enable_ssc) {
  622. val = readl(priv->mmio + PHYREG8);
  623. val |= PHYREG8_SSC_EN;
  624. writel(val, priv->mmio + PHYREG8);
  625. }
  626. return 0;
  627. }
  628. static const struct rockchip_combphy_grfcfg rk3588_combphy_grfcfgs = {
  629. /* pipe-phy-grf */
  630. .pcie_mode_set = { 0x0000, 5, 0, 0x00, 0x11 },
  631. .usb_mode_set = { 0x0000, 5, 0, 0x00, 0x04 },
  632. .pipe_rxterm_set = { 0x0000, 12, 12, 0x00, 0x01 },
  633. .pipe_txelec_set = { 0x0004, 1, 1, 0x00, 0x01 },
  634. .pipe_txcomp_set = { 0x0004, 4, 4, 0x00, 0x01 },
  635. .pipe_clk_25m = { 0x0004, 14, 13, 0x00, 0x01 },
  636. .pipe_clk_100m = { 0x0004, 14, 13, 0x00, 0x02 },
  637. .pipe_rxterm_sel = { 0x0008, 8, 8, 0x00, 0x01 },
  638. .pipe_txelec_sel = { 0x0008, 12, 12, 0x00, 0x01 },
  639. .pipe_txcomp_sel = { 0x0008, 15, 15, 0x00, 0x01 },
  640. .pipe_clk_ext = { 0x000c, 9, 8, 0x02, 0x01 },
  641. .pipe_phy_status = { 0x0034, 6, 6, 0x01, 0x00 },
  642. .con0_for_pcie = { 0x0000, 15, 0, 0x00, 0x1000 },
  643. .con1_for_pcie = { 0x0004, 15, 0, 0x00, 0x0000 },
  644. .con2_for_pcie = { 0x0008, 15, 0, 0x00, 0x0101 },
  645. .con3_for_pcie = { 0x000c, 15, 0, 0x00, 0x0200 },
  646. .con0_for_sata = { 0x0000, 15, 0, 0x00, 0x0129 },
  647. .con1_for_sata = { 0x0004, 15, 0, 0x00, 0x0000 },
  648. .con2_for_sata = { 0x0008, 15, 0, 0x00, 0x80c1 },
  649. .con3_for_sata = { 0x000c, 15, 0, 0x00, 0x0407 },
  650. /* pipe-grf */
  651. .pipe_con0_for_sata = { 0x0000, 11, 5, 0x00, 0x22 },
  652. .pipe_con1_for_sata = { 0x0000, 2, 0, 0x00, 0x2 },
  653. .pipe_pcie1l0_sel = { 0x0100, 0, 0, 0x01, 0x0 },
  654. .pipe_pcie1l1_sel = { 0x0100, 1, 1, 0x01, 0x0 },
  655. };
  656. static const struct rockchip_combphy_cfg rk3588_combphy_cfgs = {
  657. .num_phys = 3,
  658. .phy_ids = {
  659. 0xfee00000,
  660. 0xfee10000,
  661. 0xfee20000,
  662. },
  663. .grfcfg = &rk3588_combphy_grfcfgs,
  664. .combphy_cfg = rk3588_combphy_cfg,
  665. };
  666. static const struct of_device_id rockchip_combphy_of_match[] = {
  667. {
  668. .compatible = "rockchip,rk3568-naneng-combphy",
  669. .data = &rk3568_combphy_cfgs,
  670. },
  671. {
  672. .compatible = "rockchip,rk3588-naneng-combphy",
  673. .data = &rk3588_combphy_cfgs,
  674. },
  675. { },
  676. };
  677. MODULE_DEVICE_TABLE(of, rockchip_combphy_of_match);
  678. static struct platform_driver rockchip_combphy_driver = {
  679. .probe = rockchip_combphy_probe,
  680. .driver = {
  681. .name = "rockchip-naneng-combphy",
  682. .of_match_table = rockchip_combphy_of_match,
  683. },
  684. };
  685. module_platform_driver(rockchip_combphy_driver);
  686. MODULE_DESCRIPTION("Rockchip NANENG COMBPHY driver");
  687. MODULE_LICENSE("GPL v2");