phy-intel-lgm-combo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel Combo-PHY driver
  4. *
  5. * Copyright (C) 2019-2020 Intel Corporation.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/of.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/reset.h>
  18. #include <dt-bindings/phy/phy.h>
  19. #define PCIE_PHY_GEN_CTRL 0x00
  20. #define PCIE_PHY_CLK_PAD BIT(17)
  21. #define PAD_DIS_CFG 0x174
  22. #define PCS_XF_ATE_OVRD_IN_2 0x3008
  23. #define ADAPT_REQ_MSK GENMASK(5, 4)
  24. #define PCS_XF_RX_ADAPT_ACK 0x3010
  25. #define RX_ADAPT_ACK_BIT BIT(0)
  26. #define CR_ADDR(addr, lane) (((addr) + (lane) * 0x100) << 2)
  27. #define REG_COMBO_MODE(x) ((x) * 0x200)
  28. #define REG_CLK_DISABLE(x) ((x) * 0x200 + 0x124)
  29. #define COMBO_PHY_ID(x) ((x)->parent->id)
  30. #define PHY_ID(x) ((x)->id)
  31. #define CLK_100MHZ 100000000
  32. #define CLK_156_25MHZ 156250000
  33. static const unsigned long intel_iphy_clk_rates[] = {
  34. CLK_100MHZ, CLK_156_25MHZ, CLK_100MHZ,
  35. };
  36. enum {
  37. PHY_0,
  38. PHY_1,
  39. PHY_MAX_NUM
  40. };
  41. /*
  42. * Clock Register bit fields to enable clocks
  43. * for ComboPhy according to the mode.
  44. */
  45. enum intel_phy_mode {
  46. PHY_PCIE_MODE = 0,
  47. PHY_XPCS_MODE,
  48. PHY_SATA_MODE,
  49. };
  50. /* ComboPhy mode Register values */
  51. enum intel_combo_mode {
  52. PCIE0_PCIE1_MODE = 0,
  53. PCIE_DL_MODE,
  54. RXAUI_MODE,
  55. XPCS0_XPCS1_MODE,
  56. SATA0_SATA1_MODE,
  57. };
  58. enum aggregated_mode {
  59. PHY_SL_MODE,
  60. PHY_DL_MODE,
  61. };
  62. struct intel_combo_phy;
  63. struct intel_cbphy_iphy {
  64. struct phy *phy;
  65. struct intel_combo_phy *parent;
  66. struct reset_control *app_rst;
  67. u32 id;
  68. };
  69. struct intel_combo_phy {
  70. struct device *dev;
  71. struct clk *core_clk;
  72. unsigned long clk_rate;
  73. void __iomem *app_base;
  74. void __iomem *cr_base;
  75. struct regmap *syscfg;
  76. struct regmap *hsiocfg;
  77. u32 id;
  78. u32 bid;
  79. struct reset_control *phy_rst;
  80. struct reset_control *core_rst;
  81. struct intel_cbphy_iphy iphy[PHY_MAX_NUM];
  82. enum intel_phy_mode phy_mode;
  83. enum aggregated_mode aggr_mode;
  84. u32 init_cnt;
  85. struct mutex lock;
  86. };
  87. static int intel_cbphy_iphy_enable(struct intel_cbphy_iphy *iphy, bool set)
  88. {
  89. struct intel_combo_phy *cbphy = iphy->parent;
  90. u32 mask = BIT(cbphy->phy_mode * 2 + iphy->id);
  91. u32 val;
  92. /* Register: 0 is enable, 1 is disable */
  93. val = set ? 0 : mask;
  94. return regmap_update_bits(cbphy->hsiocfg, REG_CLK_DISABLE(cbphy->bid),
  95. mask, val);
  96. }
  97. static int intel_cbphy_pcie_refclk_cfg(struct intel_cbphy_iphy *iphy, bool set)
  98. {
  99. struct intel_combo_phy *cbphy = iphy->parent;
  100. u32 mask = BIT(cbphy->id * 2 + iphy->id);
  101. u32 val;
  102. /* Register: 0 is enable, 1 is disable */
  103. val = set ? 0 : mask;
  104. return regmap_update_bits(cbphy->syscfg, PAD_DIS_CFG, mask, val);
  105. }
  106. static inline void combo_phy_w32_off_mask(void __iomem *base, unsigned int reg,
  107. u32 mask, u32 val)
  108. {
  109. u32 reg_val;
  110. reg_val = readl(base + reg);
  111. reg_val &= ~mask;
  112. reg_val |= val;
  113. writel(reg_val, base + reg);
  114. }
  115. static int intel_cbphy_iphy_cfg(struct intel_cbphy_iphy *iphy,
  116. int (*phy_cfg)(struct intel_cbphy_iphy *))
  117. {
  118. struct intel_combo_phy *cbphy = iphy->parent;
  119. int ret;
  120. ret = phy_cfg(iphy);
  121. if (ret)
  122. return ret;
  123. if (cbphy->aggr_mode != PHY_DL_MODE)
  124. return 0;
  125. return phy_cfg(&cbphy->iphy[PHY_1]);
  126. }
  127. static int intel_cbphy_pcie_en_pad_refclk(struct intel_cbphy_iphy *iphy)
  128. {
  129. struct intel_combo_phy *cbphy = iphy->parent;
  130. int ret;
  131. ret = intel_cbphy_pcie_refclk_cfg(iphy, true);
  132. if (ret) {
  133. dev_err(cbphy->dev, "Failed to enable PCIe pad refclk\n");
  134. return ret;
  135. }
  136. if (cbphy->init_cnt)
  137. return 0;
  138. combo_phy_w32_off_mask(cbphy->app_base, PCIE_PHY_GEN_CTRL,
  139. PCIE_PHY_CLK_PAD, FIELD_PREP(PCIE_PHY_CLK_PAD, 0));
  140. /* Delay for stable clock PLL */
  141. usleep_range(50, 100);
  142. return 0;
  143. }
  144. static int intel_cbphy_pcie_dis_pad_refclk(struct intel_cbphy_iphy *iphy)
  145. {
  146. struct intel_combo_phy *cbphy = iphy->parent;
  147. int ret;
  148. ret = intel_cbphy_pcie_refclk_cfg(iphy, false);
  149. if (ret) {
  150. dev_err(cbphy->dev, "Failed to disable PCIe pad refclk\n");
  151. return ret;
  152. }
  153. if (cbphy->init_cnt)
  154. return 0;
  155. combo_phy_w32_off_mask(cbphy->app_base, PCIE_PHY_GEN_CTRL,
  156. PCIE_PHY_CLK_PAD, FIELD_PREP(PCIE_PHY_CLK_PAD, 1));
  157. return 0;
  158. }
  159. static int intel_cbphy_set_mode(struct intel_combo_phy *cbphy)
  160. {
  161. enum intel_combo_mode cb_mode;
  162. enum aggregated_mode aggr = cbphy->aggr_mode;
  163. struct device *dev = cbphy->dev;
  164. enum intel_phy_mode mode;
  165. int ret;
  166. mode = cbphy->phy_mode;
  167. switch (mode) {
  168. case PHY_PCIE_MODE:
  169. cb_mode = (aggr == PHY_DL_MODE) ? PCIE_DL_MODE : PCIE0_PCIE1_MODE;
  170. break;
  171. case PHY_XPCS_MODE:
  172. cb_mode = (aggr == PHY_DL_MODE) ? RXAUI_MODE : XPCS0_XPCS1_MODE;
  173. break;
  174. case PHY_SATA_MODE:
  175. if (aggr == PHY_DL_MODE) {
  176. dev_err(dev, "Mode:%u not support dual lane!\n", mode);
  177. return -EINVAL;
  178. }
  179. cb_mode = SATA0_SATA1_MODE;
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. ret = regmap_write(cbphy->hsiocfg, REG_COMBO_MODE(cbphy->bid), cb_mode);
  185. if (ret)
  186. dev_err(dev, "Failed to set ComboPhy mode: %d\n", ret);
  187. return ret;
  188. }
  189. static void intel_cbphy_rst_assert(struct intel_combo_phy *cbphy)
  190. {
  191. reset_control_assert(cbphy->core_rst);
  192. reset_control_assert(cbphy->phy_rst);
  193. }
  194. static void intel_cbphy_rst_deassert(struct intel_combo_phy *cbphy)
  195. {
  196. reset_control_deassert(cbphy->core_rst);
  197. reset_control_deassert(cbphy->phy_rst);
  198. /* Delay to ensure reset process is done */
  199. usleep_range(10, 20);
  200. }
  201. static int intel_cbphy_iphy_power_on(struct intel_cbphy_iphy *iphy)
  202. {
  203. struct intel_combo_phy *cbphy = iphy->parent;
  204. int ret;
  205. if (!cbphy->init_cnt) {
  206. ret = clk_prepare_enable(cbphy->core_clk);
  207. if (ret) {
  208. dev_err(cbphy->dev, "Clock enable failed!\n");
  209. return ret;
  210. }
  211. ret = clk_set_rate(cbphy->core_clk, cbphy->clk_rate);
  212. if (ret) {
  213. dev_err(cbphy->dev, "Clock freq set to %lu failed!\n",
  214. cbphy->clk_rate);
  215. goto clk_err;
  216. }
  217. intel_cbphy_rst_assert(cbphy);
  218. intel_cbphy_rst_deassert(cbphy);
  219. ret = intel_cbphy_set_mode(cbphy);
  220. if (ret)
  221. goto clk_err;
  222. }
  223. ret = intel_cbphy_iphy_enable(iphy, true);
  224. if (ret) {
  225. dev_err(cbphy->dev, "Failed enabling PHY core\n");
  226. goto clk_err;
  227. }
  228. ret = reset_control_deassert(iphy->app_rst);
  229. if (ret) {
  230. dev_err(cbphy->dev, "PHY(%u:%u) reset deassert failed!\n",
  231. COMBO_PHY_ID(iphy), PHY_ID(iphy));
  232. goto clk_err;
  233. }
  234. /* Delay to ensure reset process is done */
  235. udelay(1);
  236. return 0;
  237. clk_err:
  238. clk_disable_unprepare(cbphy->core_clk);
  239. return ret;
  240. }
  241. static int intel_cbphy_iphy_power_off(struct intel_cbphy_iphy *iphy)
  242. {
  243. struct intel_combo_phy *cbphy = iphy->parent;
  244. int ret;
  245. ret = reset_control_assert(iphy->app_rst);
  246. if (ret) {
  247. dev_err(cbphy->dev, "PHY(%u:%u) reset assert failed!\n",
  248. COMBO_PHY_ID(iphy), PHY_ID(iphy));
  249. return ret;
  250. }
  251. ret = intel_cbphy_iphy_enable(iphy, false);
  252. if (ret) {
  253. dev_err(cbphy->dev, "Failed disabling PHY core\n");
  254. return ret;
  255. }
  256. if (cbphy->init_cnt)
  257. return 0;
  258. clk_disable_unprepare(cbphy->core_clk);
  259. intel_cbphy_rst_assert(cbphy);
  260. return 0;
  261. }
  262. static int intel_cbphy_init(struct phy *phy)
  263. {
  264. struct intel_cbphy_iphy *iphy = phy_get_drvdata(phy);
  265. struct intel_combo_phy *cbphy = iphy->parent;
  266. int ret;
  267. mutex_lock(&cbphy->lock);
  268. ret = intel_cbphy_iphy_cfg(iphy, intel_cbphy_iphy_power_on);
  269. if (ret)
  270. goto err;
  271. if (cbphy->phy_mode == PHY_PCIE_MODE) {
  272. ret = intel_cbphy_iphy_cfg(iphy, intel_cbphy_pcie_en_pad_refclk);
  273. if (ret)
  274. goto err;
  275. }
  276. cbphy->init_cnt++;
  277. err:
  278. mutex_unlock(&cbphy->lock);
  279. return ret;
  280. }
  281. static int intel_cbphy_exit(struct phy *phy)
  282. {
  283. struct intel_cbphy_iphy *iphy = phy_get_drvdata(phy);
  284. struct intel_combo_phy *cbphy = iphy->parent;
  285. int ret;
  286. mutex_lock(&cbphy->lock);
  287. cbphy->init_cnt--;
  288. if (cbphy->phy_mode == PHY_PCIE_MODE) {
  289. ret = intel_cbphy_iphy_cfg(iphy, intel_cbphy_pcie_dis_pad_refclk);
  290. if (ret)
  291. goto err;
  292. }
  293. ret = intel_cbphy_iphy_cfg(iphy, intel_cbphy_iphy_power_off);
  294. err:
  295. mutex_unlock(&cbphy->lock);
  296. return ret;
  297. }
  298. static int intel_cbphy_calibrate(struct phy *phy)
  299. {
  300. struct intel_cbphy_iphy *iphy = phy_get_drvdata(phy);
  301. struct intel_combo_phy *cbphy = iphy->parent;
  302. void __iomem *cr_base = cbphy->cr_base;
  303. int val, ret, id;
  304. if (cbphy->phy_mode != PHY_XPCS_MODE)
  305. return 0;
  306. id = PHY_ID(iphy);
  307. /* trigger auto RX adaptation */
  308. combo_phy_w32_off_mask(cr_base, CR_ADDR(PCS_XF_ATE_OVRD_IN_2, id),
  309. ADAPT_REQ_MSK, FIELD_PREP(ADAPT_REQ_MSK, 3));
  310. /* Wait RX adaptation to finish */
  311. ret = readl_poll_timeout(cr_base + CR_ADDR(PCS_XF_RX_ADAPT_ACK, id),
  312. val, val & RX_ADAPT_ACK_BIT, 10, 5000);
  313. if (ret)
  314. dev_err(cbphy->dev, "RX Adaptation failed!\n");
  315. else
  316. dev_dbg(cbphy->dev, "RX Adaptation success!\n");
  317. /* Stop RX adaptation */
  318. combo_phy_w32_off_mask(cr_base, CR_ADDR(PCS_XF_ATE_OVRD_IN_2, id),
  319. ADAPT_REQ_MSK, FIELD_PREP(ADAPT_REQ_MSK, 0));
  320. return ret;
  321. }
  322. static int intel_cbphy_fwnode_parse(struct intel_combo_phy *cbphy)
  323. {
  324. struct device *dev = cbphy->dev;
  325. struct platform_device *pdev = to_platform_device(dev);
  326. struct fwnode_handle *fwnode = dev_fwnode(dev);
  327. struct fwnode_reference_args ref;
  328. int ret;
  329. u32 val;
  330. cbphy->core_clk = devm_clk_get(dev, NULL);
  331. if (IS_ERR(cbphy->core_clk))
  332. return dev_err_probe(dev, PTR_ERR(cbphy->core_clk),
  333. "Get clk failed!\n");
  334. cbphy->core_rst = devm_reset_control_get_optional(dev, "core");
  335. if (IS_ERR(cbphy->core_rst))
  336. return dev_err_probe(dev, PTR_ERR(cbphy->core_rst),
  337. "Get core reset control err!\n");
  338. cbphy->phy_rst = devm_reset_control_get_optional(dev, "phy");
  339. if (IS_ERR(cbphy->phy_rst))
  340. return dev_err_probe(dev, PTR_ERR(cbphy->phy_rst),
  341. "Get PHY reset control err!\n");
  342. cbphy->iphy[0].app_rst = devm_reset_control_get_optional(dev, "iphy0");
  343. if (IS_ERR(cbphy->iphy[0].app_rst))
  344. return dev_err_probe(dev, PTR_ERR(cbphy->iphy[0].app_rst),
  345. "Get phy0 reset control err!\n");
  346. cbphy->iphy[1].app_rst = devm_reset_control_get_optional(dev, "iphy1");
  347. if (IS_ERR(cbphy->iphy[1].app_rst))
  348. return dev_err_probe(dev, PTR_ERR(cbphy->iphy[1].app_rst),
  349. "Get phy1 reset control err!\n");
  350. cbphy->app_base = devm_platform_ioremap_resource_byname(pdev, "app");
  351. if (IS_ERR(cbphy->app_base))
  352. return PTR_ERR(cbphy->app_base);
  353. cbphy->cr_base = devm_platform_ioremap_resource_byname(pdev, "core");
  354. if (IS_ERR(cbphy->cr_base))
  355. return PTR_ERR(cbphy->cr_base);
  356. /*
  357. * syscfg and hsiocfg variables stores the handle of the registers set
  358. * in which ComboPhy subsystem specific registers are subset. Using
  359. * Register map framework to access the registers set.
  360. */
  361. ret = fwnode_property_get_reference_args(fwnode, "intel,syscfg", NULL,
  362. 1, 0, &ref);
  363. if (ret < 0)
  364. return ret;
  365. cbphy->id = ref.args[0];
  366. cbphy->syscfg = device_node_to_regmap(to_of_node(ref.fwnode));
  367. fwnode_handle_put(ref.fwnode);
  368. ret = fwnode_property_get_reference_args(fwnode, "intel,hsio", NULL, 1,
  369. 0, &ref);
  370. if (ret < 0)
  371. return ret;
  372. cbphy->bid = ref.args[0];
  373. cbphy->hsiocfg = device_node_to_regmap(to_of_node(ref.fwnode));
  374. fwnode_handle_put(ref.fwnode);
  375. ret = fwnode_property_read_u32_array(fwnode, "intel,phy-mode", &val, 1);
  376. if (ret)
  377. return ret;
  378. switch (val) {
  379. case PHY_TYPE_PCIE:
  380. cbphy->phy_mode = PHY_PCIE_MODE;
  381. break;
  382. case PHY_TYPE_SATA:
  383. cbphy->phy_mode = PHY_SATA_MODE;
  384. break;
  385. case PHY_TYPE_XPCS:
  386. cbphy->phy_mode = PHY_XPCS_MODE;
  387. break;
  388. default:
  389. dev_err(dev, "Invalid PHY mode: %u\n", val);
  390. return -EINVAL;
  391. }
  392. cbphy->clk_rate = intel_iphy_clk_rates[cbphy->phy_mode];
  393. if (fwnode_property_present(fwnode, "intel,aggregation"))
  394. cbphy->aggr_mode = PHY_DL_MODE;
  395. else
  396. cbphy->aggr_mode = PHY_SL_MODE;
  397. return 0;
  398. }
  399. static const struct phy_ops intel_cbphy_ops = {
  400. .init = intel_cbphy_init,
  401. .exit = intel_cbphy_exit,
  402. .calibrate = intel_cbphy_calibrate,
  403. .owner = THIS_MODULE,
  404. };
  405. static struct phy *intel_cbphy_xlate(struct device *dev,
  406. struct of_phandle_args *args)
  407. {
  408. struct intel_combo_phy *cbphy = dev_get_drvdata(dev);
  409. u32 iphy_id;
  410. if (args->args_count < 1) {
  411. dev_err(dev, "Invalid number of arguments\n");
  412. return ERR_PTR(-EINVAL);
  413. }
  414. iphy_id = args->args[0];
  415. if (iphy_id >= PHY_MAX_NUM) {
  416. dev_err(dev, "Invalid phy instance %d\n", iphy_id);
  417. return ERR_PTR(-EINVAL);
  418. }
  419. if (cbphy->aggr_mode == PHY_DL_MODE && iphy_id == PHY_1) {
  420. dev_err(dev, "Invalid. ComboPhy is in Dual lane mode %d\n", iphy_id);
  421. return ERR_PTR(-EINVAL);
  422. }
  423. return cbphy->iphy[iphy_id].phy;
  424. }
  425. static int intel_cbphy_create(struct intel_combo_phy *cbphy)
  426. {
  427. struct phy_provider *phy_provider;
  428. struct device *dev = cbphy->dev;
  429. struct intel_cbphy_iphy *iphy;
  430. int i;
  431. for (i = 0; i < PHY_MAX_NUM; i++) {
  432. iphy = &cbphy->iphy[i];
  433. iphy->parent = cbphy;
  434. iphy->id = i;
  435. /* In dual lane mode skip phy creation for the second phy */
  436. if (cbphy->aggr_mode == PHY_DL_MODE && iphy->id == PHY_1)
  437. continue;
  438. iphy->phy = devm_phy_create(dev, NULL, &intel_cbphy_ops);
  439. if (IS_ERR(iphy->phy)) {
  440. dev_err(dev, "PHY[%u:%u]: create PHY instance failed!\n",
  441. COMBO_PHY_ID(iphy), PHY_ID(iphy));
  442. return PTR_ERR(iphy->phy);
  443. }
  444. phy_set_drvdata(iphy->phy, iphy);
  445. }
  446. dev_set_drvdata(dev, cbphy);
  447. phy_provider = devm_of_phy_provider_register(dev, intel_cbphy_xlate);
  448. if (IS_ERR(phy_provider))
  449. dev_err(dev, "Register PHY provider failed!\n");
  450. return PTR_ERR_OR_ZERO(phy_provider);
  451. }
  452. static int intel_cbphy_probe(struct platform_device *pdev)
  453. {
  454. struct device *dev = &pdev->dev;
  455. struct intel_combo_phy *cbphy;
  456. int ret;
  457. cbphy = devm_kzalloc(dev, sizeof(*cbphy), GFP_KERNEL);
  458. if (!cbphy)
  459. return -ENOMEM;
  460. cbphy->dev = dev;
  461. cbphy->init_cnt = 0;
  462. mutex_init(&cbphy->lock);
  463. ret = intel_cbphy_fwnode_parse(cbphy);
  464. if (ret)
  465. return ret;
  466. platform_set_drvdata(pdev, cbphy);
  467. return intel_cbphy_create(cbphy);
  468. }
  469. static int intel_cbphy_remove(struct platform_device *pdev)
  470. {
  471. struct intel_combo_phy *cbphy = platform_get_drvdata(pdev);
  472. intel_cbphy_rst_assert(cbphy);
  473. clk_disable_unprepare(cbphy->core_clk);
  474. return 0;
  475. }
  476. static const struct of_device_id of_intel_cbphy_match[] = {
  477. { .compatible = "intel,combo-phy" },
  478. { .compatible = "intel,combophy-lgm" },
  479. {}
  480. };
  481. static struct platform_driver intel_cbphy_driver = {
  482. .probe = intel_cbphy_probe,
  483. .remove = intel_cbphy_remove,
  484. .driver = {
  485. .name = "intel-combo-phy",
  486. .of_match_table = of_intel_cbphy_match,
  487. }
  488. };
  489. module_platform_driver(intel_cbphy_driver);
  490. MODULE_DESCRIPTION("Intel Combo-phy driver");
  491. MODULE_LICENSE("GPL v2");