phy-rockchip-pcie.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Rockchip PCIe PHY driver
  4. *
  5. * Copyright (C) 2016 Shawn Lin <[email protected]>
  6. * Copyright (C) 2016 ROCKCHIP, Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/io.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/reset.h>
  20. /*
  21. * The higher 16-bit of this register is used for write protection
  22. * only if BIT(x + 16) set to 1 the BIT(x) can be written.
  23. */
  24. #define HIWORD_UPDATE(val, mask, shift) \
  25. ((val) << (shift) | (mask) << ((shift) + 16))
  26. #define PHY_MAX_LANE_NUM 4
  27. #define PHY_CFG_DATA_SHIFT 7
  28. #define PHY_CFG_ADDR_SHIFT 1
  29. #define PHY_CFG_DATA_MASK 0xf
  30. #define PHY_CFG_ADDR_MASK 0x3f
  31. #define PHY_CFG_RD_MASK 0x3ff
  32. #define PHY_CFG_WR_ENABLE 1
  33. #define PHY_CFG_WR_DISABLE 1
  34. #define PHY_CFG_WR_SHIFT 0
  35. #define PHY_CFG_WR_MASK 1
  36. #define PHY_CFG_PLL_LOCK 0x10
  37. #define PHY_CFG_CLK_TEST 0x10
  38. #define PHY_CFG_CLK_SCC 0x12
  39. #define PHY_CFG_SEPE_RATE BIT(3)
  40. #define PHY_CFG_PLL_100M BIT(3)
  41. #define PHY_PLL_LOCKED BIT(9)
  42. #define PHY_PLL_OUTPUT BIT(10)
  43. #define PHY_LANE_A_STATUS 0x30
  44. #define PHY_LANE_B_STATUS 0x31
  45. #define PHY_LANE_C_STATUS 0x32
  46. #define PHY_LANE_D_STATUS 0x33
  47. #define PHY_LANE_RX_DET_SHIFT 11
  48. #define PHY_LANE_RX_DET_TH 0x1
  49. #define PHY_LANE_IDLE_OFF 0x1
  50. #define PHY_LANE_IDLE_MASK 0x1
  51. #define PHY_LANE_IDLE_A_SHIFT 3
  52. #define PHY_LANE_IDLE_B_SHIFT 4
  53. #define PHY_LANE_IDLE_C_SHIFT 5
  54. #define PHY_LANE_IDLE_D_SHIFT 6
  55. struct rockchip_pcie_data {
  56. unsigned int pcie_conf;
  57. unsigned int pcie_status;
  58. unsigned int pcie_laneoff;
  59. };
  60. struct rockchip_pcie_phy {
  61. struct rockchip_pcie_data *phy_data;
  62. struct regmap *reg_base;
  63. struct phy_pcie_instance {
  64. struct phy *phy;
  65. u32 index;
  66. } phys[PHY_MAX_LANE_NUM];
  67. struct mutex pcie_mutex;
  68. struct reset_control *phy_rst;
  69. struct clk *clk_pciephy_ref;
  70. int pwr_cnt;
  71. int init_cnt;
  72. };
  73. static struct rockchip_pcie_phy *to_pcie_phy(struct phy_pcie_instance *inst)
  74. {
  75. return container_of(inst, struct rockchip_pcie_phy,
  76. phys[inst->index]);
  77. }
  78. static struct phy *rockchip_pcie_phy_of_xlate(struct device *dev,
  79. struct of_phandle_args *args)
  80. {
  81. struct rockchip_pcie_phy *rk_phy = dev_get_drvdata(dev);
  82. if (args->args_count == 0)
  83. return rk_phy->phys[0].phy;
  84. if (WARN_ON(args->args[0] >= PHY_MAX_LANE_NUM))
  85. return ERR_PTR(-ENODEV);
  86. return rk_phy->phys[args->args[0]].phy;
  87. }
  88. static inline void phy_wr_cfg(struct rockchip_pcie_phy *rk_phy,
  89. u32 addr, u32 data)
  90. {
  91. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  92. HIWORD_UPDATE(data,
  93. PHY_CFG_DATA_MASK,
  94. PHY_CFG_DATA_SHIFT) |
  95. HIWORD_UPDATE(addr,
  96. PHY_CFG_ADDR_MASK,
  97. PHY_CFG_ADDR_SHIFT));
  98. udelay(1);
  99. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  100. HIWORD_UPDATE(PHY_CFG_WR_ENABLE,
  101. PHY_CFG_WR_MASK,
  102. PHY_CFG_WR_SHIFT));
  103. udelay(1);
  104. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  105. HIWORD_UPDATE(PHY_CFG_WR_DISABLE,
  106. PHY_CFG_WR_MASK,
  107. PHY_CFG_WR_SHIFT));
  108. }
  109. static inline u32 phy_rd_cfg(struct rockchip_pcie_phy *rk_phy,
  110. u32 addr)
  111. {
  112. u32 val;
  113. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  114. HIWORD_UPDATE(addr,
  115. PHY_CFG_RD_MASK,
  116. PHY_CFG_ADDR_SHIFT));
  117. regmap_read(rk_phy->reg_base,
  118. rk_phy->phy_data->pcie_status,
  119. &val);
  120. return val;
  121. }
  122. static int rockchip_pcie_phy_power_off(struct phy *phy)
  123. {
  124. struct phy_pcie_instance *inst = phy_get_drvdata(phy);
  125. struct rockchip_pcie_phy *rk_phy = to_pcie_phy(inst);
  126. int err = 0;
  127. mutex_lock(&rk_phy->pcie_mutex);
  128. regmap_write(rk_phy->reg_base,
  129. rk_phy->phy_data->pcie_laneoff,
  130. HIWORD_UPDATE(PHY_LANE_IDLE_OFF,
  131. PHY_LANE_IDLE_MASK,
  132. PHY_LANE_IDLE_A_SHIFT + inst->index));
  133. if (--rk_phy->pwr_cnt)
  134. goto err_out;
  135. err = reset_control_assert(rk_phy->phy_rst);
  136. if (err) {
  137. dev_err(&phy->dev, "assert phy_rst err %d\n", err);
  138. goto err_restore;
  139. }
  140. err_out:
  141. mutex_unlock(&rk_phy->pcie_mutex);
  142. return 0;
  143. err_restore:
  144. rk_phy->pwr_cnt++;
  145. regmap_write(rk_phy->reg_base,
  146. rk_phy->phy_data->pcie_laneoff,
  147. HIWORD_UPDATE(!PHY_LANE_IDLE_OFF,
  148. PHY_LANE_IDLE_MASK,
  149. PHY_LANE_IDLE_A_SHIFT + inst->index));
  150. mutex_unlock(&rk_phy->pcie_mutex);
  151. return err;
  152. }
  153. static int rockchip_pcie_phy_power_on(struct phy *phy)
  154. {
  155. struct phy_pcie_instance *inst = phy_get_drvdata(phy);
  156. struct rockchip_pcie_phy *rk_phy = to_pcie_phy(inst);
  157. int err = 0;
  158. u32 status;
  159. unsigned long timeout;
  160. mutex_lock(&rk_phy->pcie_mutex);
  161. if (rk_phy->pwr_cnt++)
  162. goto err_out;
  163. err = reset_control_deassert(rk_phy->phy_rst);
  164. if (err) {
  165. dev_err(&phy->dev, "deassert phy_rst err %d\n", err);
  166. goto err_pwr_cnt;
  167. }
  168. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  169. HIWORD_UPDATE(PHY_CFG_PLL_LOCK,
  170. PHY_CFG_ADDR_MASK,
  171. PHY_CFG_ADDR_SHIFT));
  172. regmap_write(rk_phy->reg_base,
  173. rk_phy->phy_data->pcie_laneoff,
  174. HIWORD_UPDATE(!PHY_LANE_IDLE_OFF,
  175. PHY_LANE_IDLE_MASK,
  176. PHY_LANE_IDLE_A_SHIFT + inst->index));
  177. /*
  178. * No documented timeout value for phy operation below,
  179. * so we make it large enough here. And we use loop-break
  180. * method which should not be harmful.
  181. */
  182. timeout = jiffies + msecs_to_jiffies(1000);
  183. err = -EINVAL;
  184. while (time_before(jiffies, timeout)) {
  185. regmap_read(rk_phy->reg_base,
  186. rk_phy->phy_data->pcie_status,
  187. &status);
  188. if (status & PHY_PLL_LOCKED) {
  189. dev_dbg(&phy->dev, "pll locked!\n");
  190. err = 0;
  191. break;
  192. }
  193. msleep(20);
  194. }
  195. if (err) {
  196. dev_err(&phy->dev, "pll lock timeout!\n");
  197. goto err_pll_lock;
  198. }
  199. phy_wr_cfg(rk_phy, PHY_CFG_CLK_TEST, PHY_CFG_SEPE_RATE);
  200. phy_wr_cfg(rk_phy, PHY_CFG_CLK_SCC, PHY_CFG_PLL_100M);
  201. err = -ETIMEDOUT;
  202. while (time_before(jiffies, timeout)) {
  203. regmap_read(rk_phy->reg_base,
  204. rk_phy->phy_data->pcie_status,
  205. &status);
  206. if (!(status & PHY_PLL_OUTPUT)) {
  207. dev_dbg(&phy->dev, "pll output enable done!\n");
  208. err = 0;
  209. break;
  210. }
  211. msleep(20);
  212. }
  213. if (err) {
  214. dev_err(&phy->dev, "pll output enable timeout!\n");
  215. goto err_pll_lock;
  216. }
  217. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  218. HIWORD_UPDATE(PHY_CFG_PLL_LOCK,
  219. PHY_CFG_ADDR_MASK,
  220. PHY_CFG_ADDR_SHIFT));
  221. err = -EINVAL;
  222. while (time_before(jiffies, timeout)) {
  223. regmap_read(rk_phy->reg_base,
  224. rk_phy->phy_data->pcie_status,
  225. &status);
  226. if (status & PHY_PLL_LOCKED) {
  227. dev_dbg(&phy->dev, "pll relocked!\n");
  228. err = 0;
  229. break;
  230. }
  231. msleep(20);
  232. }
  233. if (err) {
  234. dev_err(&phy->dev, "pll relock timeout!\n");
  235. goto err_pll_lock;
  236. }
  237. err_out:
  238. mutex_unlock(&rk_phy->pcie_mutex);
  239. return 0;
  240. err_pll_lock:
  241. reset_control_assert(rk_phy->phy_rst);
  242. err_pwr_cnt:
  243. rk_phy->pwr_cnt--;
  244. mutex_unlock(&rk_phy->pcie_mutex);
  245. return err;
  246. }
  247. static int rockchip_pcie_phy_init(struct phy *phy)
  248. {
  249. struct phy_pcie_instance *inst = phy_get_drvdata(phy);
  250. struct rockchip_pcie_phy *rk_phy = to_pcie_phy(inst);
  251. int err = 0;
  252. mutex_lock(&rk_phy->pcie_mutex);
  253. if (rk_phy->init_cnt++)
  254. goto err_out;
  255. err = clk_prepare_enable(rk_phy->clk_pciephy_ref);
  256. if (err) {
  257. dev_err(&phy->dev, "Fail to enable pcie ref clock.\n");
  258. goto err_refclk;
  259. }
  260. err = reset_control_assert(rk_phy->phy_rst);
  261. if (err) {
  262. dev_err(&phy->dev, "assert phy_rst err %d\n", err);
  263. goto err_reset;
  264. }
  265. err_out:
  266. mutex_unlock(&rk_phy->pcie_mutex);
  267. return 0;
  268. err_reset:
  269. clk_disable_unprepare(rk_phy->clk_pciephy_ref);
  270. err_refclk:
  271. rk_phy->init_cnt--;
  272. mutex_unlock(&rk_phy->pcie_mutex);
  273. return err;
  274. }
  275. static int rockchip_pcie_phy_exit(struct phy *phy)
  276. {
  277. struct phy_pcie_instance *inst = phy_get_drvdata(phy);
  278. struct rockchip_pcie_phy *rk_phy = to_pcie_phy(inst);
  279. mutex_lock(&rk_phy->pcie_mutex);
  280. if (--rk_phy->init_cnt)
  281. goto err_init_cnt;
  282. clk_disable_unprepare(rk_phy->clk_pciephy_ref);
  283. err_init_cnt:
  284. mutex_unlock(&rk_phy->pcie_mutex);
  285. return 0;
  286. }
  287. static const struct phy_ops ops = {
  288. .init = rockchip_pcie_phy_init,
  289. .exit = rockchip_pcie_phy_exit,
  290. .power_on = rockchip_pcie_phy_power_on,
  291. .power_off = rockchip_pcie_phy_power_off,
  292. .owner = THIS_MODULE,
  293. };
  294. static const struct rockchip_pcie_data rk3399_pcie_data = {
  295. .pcie_conf = 0xe220,
  296. .pcie_status = 0xe2a4,
  297. .pcie_laneoff = 0xe214,
  298. };
  299. static const struct of_device_id rockchip_pcie_phy_dt_ids[] = {
  300. {
  301. .compatible = "rockchip,rk3399-pcie-phy",
  302. .data = &rk3399_pcie_data,
  303. },
  304. {}
  305. };
  306. MODULE_DEVICE_TABLE(of, rockchip_pcie_phy_dt_ids);
  307. static int rockchip_pcie_phy_probe(struct platform_device *pdev)
  308. {
  309. struct device *dev = &pdev->dev;
  310. struct rockchip_pcie_phy *rk_phy;
  311. struct phy_provider *phy_provider;
  312. struct regmap *grf;
  313. const struct of_device_id *of_id;
  314. int i;
  315. u32 phy_num;
  316. grf = syscon_node_to_regmap(dev->parent->of_node);
  317. if (IS_ERR(grf)) {
  318. dev_err(dev, "Cannot find GRF syscon\n");
  319. return PTR_ERR(grf);
  320. }
  321. rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
  322. if (!rk_phy)
  323. return -ENOMEM;
  324. of_id = of_match_device(rockchip_pcie_phy_dt_ids, &pdev->dev);
  325. if (!of_id)
  326. return -EINVAL;
  327. rk_phy->phy_data = (struct rockchip_pcie_data *)of_id->data;
  328. rk_phy->reg_base = grf;
  329. mutex_init(&rk_phy->pcie_mutex);
  330. rk_phy->phy_rst = devm_reset_control_get(dev, "phy");
  331. if (IS_ERR(rk_phy->phy_rst)) {
  332. if (PTR_ERR(rk_phy->phy_rst) != -EPROBE_DEFER)
  333. dev_err(dev,
  334. "missing phy property for reset controller\n");
  335. return PTR_ERR(rk_phy->phy_rst);
  336. }
  337. rk_phy->clk_pciephy_ref = devm_clk_get(dev, "refclk");
  338. if (IS_ERR(rk_phy->clk_pciephy_ref)) {
  339. dev_err(dev, "refclk not found.\n");
  340. return PTR_ERR(rk_phy->clk_pciephy_ref);
  341. }
  342. /* parse #phy-cells to see if it's legacy PHY model */
  343. if (of_property_read_u32(dev->of_node, "#phy-cells", &phy_num))
  344. return -ENOENT;
  345. phy_num = (phy_num == 0) ? 1 : PHY_MAX_LANE_NUM;
  346. dev_dbg(dev, "phy number is %d\n", phy_num);
  347. for (i = 0; i < phy_num; i++) {
  348. rk_phy->phys[i].phy = devm_phy_create(dev, dev->of_node, &ops);
  349. if (IS_ERR(rk_phy->phys[i].phy)) {
  350. dev_err(dev, "failed to create PHY%d\n", i);
  351. return PTR_ERR(rk_phy->phys[i].phy);
  352. }
  353. rk_phy->phys[i].index = i;
  354. phy_set_drvdata(rk_phy->phys[i].phy, &rk_phy->phys[i]);
  355. }
  356. platform_set_drvdata(pdev, rk_phy);
  357. phy_provider = devm_of_phy_provider_register(dev,
  358. rockchip_pcie_phy_of_xlate);
  359. return PTR_ERR_OR_ZERO(phy_provider);
  360. }
  361. static struct platform_driver rockchip_pcie_driver = {
  362. .probe = rockchip_pcie_phy_probe,
  363. .driver = {
  364. .name = "rockchip-pcie-phy",
  365. .of_match_table = rockchip_pcie_phy_dt_ids,
  366. },
  367. };
  368. module_platform_driver(rockchip_pcie_driver);
  369. MODULE_AUTHOR("Shawn Lin <[email protected]>");
  370. MODULE_DESCRIPTION("Rockchip PCIe PHY driver");
  371. MODULE_LICENSE("GPL v2");