phy-rockchip-pcie.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Rockchip PCIe PHY driver
  3. *
  4. * Copyright (C) 2016 Shawn Lin <shawn.lin@rock-chips.com>
  5. * Copyright (C) 2016 ROCKCHIP, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/phy/phy.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regmap.h>
  27. #include <linux/reset.h>
  28. /*
  29. * The higher 16-bit of this register is used for write protection
  30. * only if BIT(x + 16) set to 1 the BIT(x) can be written.
  31. */
  32. #define HIWORD_UPDATE(val, mask, shift) \
  33. ((val) << (shift) | (mask) << ((shift) + 16))
  34. #define PHY_MAX_LANE_NUM 4
  35. #define PHY_CFG_DATA_SHIFT 7
  36. #define PHY_CFG_ADDR_SHIFT 1
  37. #define PHY_CFG_DATA_MASK 0xf
  38. #define PHY_CFG_ADDR_MASK 0x3f
  39. #define PHY_CFG_RD_MASK 0x3ff
  40. #define PHY_CFG_WR_ENABLE 1
  41. #define PHY_CFG_WR_DISABLE 1
  42. #define PHY_CFG_WR_SHIFT 0
  43. #define PHY_CFG_WR_MASK 1
  44. #define PHY_CFG_PLL_LOCK 0x10
  45. #define PHY_CFG_CLK_TEST 0x10
  46. #define PHY_CFG_CLK_SCC 0x12
  47. #define PHY_CFG_SEPE_RATE BIT(3)
  48. #define PHY_CFG_PLL_100M BIT(3)
  49. #define PHY_PLL_LOCKED BIT(9)
  50. #define PHY_PLL_OUTPUT BIT(10)
  51. #define PHY_LANE_A_STATUS 0x30
  52. #define PHY_LANE_B_STATUS 0x31
  53. #define PHY_LANE_C_STATUS 0x32
  54. #define PHY_LANE_D_STATUS 0x33
  55. #define PHY_LANE_RX_DET_SHIFT 11
  56. #define PHY_LANE_RX_DET_TH 0x1
  57. #define PHY_LANE_IDLE_OFF 0x1
  58. #define PHY_LANE_IDLE_MASK 0x1
  59. #define PHY_LANE_IDLE_A_SHIFT 3
  60. #define PHY_LANE_IDLE_B_SHIFT 4
  61. #define PHY_LANE_IDLE_C_SHIFT 5
  62. #define PHY_LANE_IDLE_D_SHIFT 6
  63. struct rockchip_pcie_data {
  64. unsigned int pcie_conf;
  65. unsigned int pcie_status;
  66. unsigned int pcie_laneoff;
  67. };
  68. struct rockchip_pcie_phy {
  69. struct rockchip_pcie_data *phy_data;
  70. struct regmap *reg_base;
  71. struct reset_control *phy_rst;
  72. struct clk *clk_pciephy_ref;
  73. };
  74. static inline void phy_wr_cfg(struct rockchip_pcie_phy *rk_phy,
  75. u32 addr, u32 data)
  76. {
  77. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  78. HIWORD_UPDATE(data,
  79. PHY_CFG_DATA_MASK,
  80. PHY_CFG_DATA_SHIFT) |
  81. HIWORD_UPDATE(addr,
  82. PHY_CFG_ADDR_MASK,
  83. PHY_CFG_ADDR_SHIFT));
  84. udelay(1);
  85. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  86. HIWORD_UPDATE(PHY_CFG_WR_ENABLE,
  87. PHY_CFG_WR_MASK,
  88. PHY_CFG_WR_SHIFT));
  89. udelay(1);
  90. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  91. HIWORD_UPDATE(PHY_CFG_WR_DISABLE,
  92. PHY_CFG_WR_MASK,
  93. PHY_CFG_WR_SHIFT));
  94. }
  95. static inline u32 phy_rd_cfg(struct rockchip_pcie_phy *rk_phy,
  96. u32 addr)
  97. {
  98. u32 val;
  99. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  100. HIWORD_UPDATE(addr,
  101. PHY_CFG_RD_MASK,
  102. PHY_CFG_ADDR_SHIFT));
  103. regmap_read(rk_phy->reg_base,
  104. rk_phy->phy_data->pcie_status,
  105. &val);
  106. return val;
  107. }
  108. static int rockchip_pcie_phy_power_off(struct phy *phy)
  109. {
  110. struct rockchip_pcie_phy *rk_phy = phy_get_drvdata(phy);
  111. int err = 0;
  112. err = reset_control_assert(rk_phy->phy_rst);
  113. if (err) {
  114. dev_err(&phy->dev, "assert phy_rst err %d\n", err);
  115. return err;
  116. }
  117. return 0;
  118. }
  119. static int rockchip_pcie_phy_power_on(struct phy *phy)
  120. {
  121. struct rockchip_pcie_phy *rk_phy = phy_get_drvdata(phy);
  122. int err = 0;
  123. u32 status;
  124. unsigned long timeout;
  125. err = reset_control_deassert(rk_phy->phy_rst);
  126. if (err) {
  127. dev_err(&phy->dev, "deassert phy_rst err %d\n", err);
  128. return err;
  129. }
  130. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  131. HIWORD_UPDATE(PHY_CFG_PLL_LOCK,
  132. PHY_CFG_ADDR_MASK,
  133. PHY_CFG_ADDR_SHIFT));
  134. /*
  135. * No documented timeout value for phy operation below,
  136. * so we make it large enough here. And we use loop-break
  137. * method which should not be harmful.
  138. */
  139. timeout = jiffies + msecs_to_jiffies(1000);
  140. err = -EINVAL;
  141. while (time_before(jiffies, timeout)) {
  142. regmap_read(rk_phy->reg_base,
  143. rk_phy->phy_data->pcie_status,
  144. &status);
  145. if (status & PHY_PLL_LOCKED) {
  146. dev_dbg(&phy->dev, "pll locked!\n");
  147. err = 0;
  148. break;
  149. }
  150. msleep(20);
  151. }
  152. if (err) {
  153. dev_err(&phy->dev, "pll lock timeout!\n");
  154. goto err_pll_lock;
  155. }
  156. phy_wr_cfg(rk_phy, PHY_CFG_CLK_TEST, PHY_CFG_SEPE_RATE);
  157. phy_wr_cfg(rk_phy, PHY_CFG_CLK_SCC, PHY_CFG_PLL_100M);
  158. err = -ETIMEDOUT;
  159. while (time_before(jiffies, timeout)) {
  160. regmap_read(rk_phy->reg_base,
  161. rk_phy->phy_data->pcie_status,
  162. &status);
  163. if (!(status & PHY_PLL_OUTPUT)) {
  164. dev_dbg(&phy->dev, "pll output enable done!\n");
  165. err = 0;
  166. break;
  167. }
  168. msleep(20);
  169. }
  170. if (err) {
  171. dev_err(&phy->dev, "pll output enable timeout!\n");
  172. goto err_pll_lock;
  173. }
  174. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  175. HIWORD_UPDATE(PHY_CFG_PLL_LOCK,
  176. PHY_CFG_ADDR_MASK,
  177. PHY_CFG_ADDR_SHIFT));
  178. err = -EINVAL;
  179. while (time_before(jiffies, timeout)) {
  180. regmap_read(rk_phy->reg_base,
  181. rk_phy->phy_data->pcie_status,
  182. &status);
  183. if (status & PHY_PLL_LOCKED) {
  184. dev_dbg(&phy->dev, "pll relocked!\n");
  185. err = 0;
  186. break;
  187. }
  188. msleep(20);
  189. }
  190. if (err) {
  191. dev_err(&phy->dev, "pll relock timeout!\n");
  192. goto err_pll_lock;
  193. }
  194. return 0;
  195. err_pll_lock:
  196. reset_control_assert(rk_phy->phy_rst);
  197. return err;
  198. }
  199. static int rockchip_pcie_phy_init(struct phy *phy)
  200. {
  201. struct rockchip_pcie_phy *rk_phy = phy_get_drvdata(phy);
  202. int err = 0;
  203. err = clk_prepare_enable(rk_phy->clk_pciephy_ref);
  204. if (err) {
  205. dev_err(&phy->dev, "Fail to enable pcie ref clock.\n");
  206. goto err_refclk;
  207. }
  208. err = reset_control_assert(rk_phy->phy_rst);
  209. if (err) {
  210. dev_err(&phy->dev, "assert phy_rst err %d\n", err);
  211. goto err_reset;
  212. }
  213. return err;
  214. err_reset:
  215. clk_disable_unprepare(rk_phy->clk_pciephy_ref);
  216. err_refclk:
  217. return err;
  218. }
  219. static int rockchip_pcie_phy_exit(struct phy *phy)
  220. {
  221. struct rockchip_pcie_phy *rk_phy = phy_get_drvdata(phy);
  222. clk_disable_unprepare(rk_phy->clk_pciephy_ref);
  223. return 0;
  224. }
  225. static const struct phy_ops ops = {
  226. .init = rockchip_pcie_phy_init,
  227. .exit = rockchip_pcie_phy_exit,
  228. .power_on = rockchip_pcie_phy_power_on,
  229. .power_off = rockchip_pcie_phy_power_off,
  230. .owner = THIS_MODULE,
  231. };
  232. static const struct rockchip_pcie_data rk3399_pcie_data = {
  233. .pcie_conf = 0xe220,
  234. .pcie_status = 0xe2a4,
  235. .pcie_laneoff = 0xe214,
  236. };
  237. static const struct of_device_id rockchip_pcie_phy_dt_ids[] = {
  238. {
  239. .compatible = "rockchip,rk3399-pcie-phy",
  240. .data = &rk3399_pcie_data,
  241. },
  242. {}
  243. };
  244. MODULE_DEVICE_TABLE(of, rockchip_pcie_phy_dt_ids);
  245. static int rockchip_pcie_phy_probe(struct platform_device *pdev)
  246. {
  247. struct device *dev = &pdev->dev;
  248. struct rockchip_pcie_phy *rk_phy;
  249. struct phy *generic_phy;
  250. struct phy_provider *phy_provider;
  251. struct regmap *grf;
  252. const struct of_device_id *of_id;
  253. grf = syscon_node_to_regmap(dev->parent->of_node);
  254. if (IS_ERR(grf)) {
  255. dev_err(dev, "Cannot find GRF syscon\n");
  256. return PTR_ERR(grf);
  257. }
  258. rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
  259. if (!rk_phy)
  260. return -ENOMEM;
  261. of_id = of_match_device(rockchip_pcie_phy_dt_ids, &pdev->dev);
  262. if (!of_id)
  263. return -EINVAL;
  264. rk_phy->phy_data = (struct rockchip_pcie_data *)of_id->data;
  265. rk_phy->reg_base = grf;
  266. rk_phy->phy_rst = devm_reset_control_get(dev, "phy");
  267. if (IS_ERR(rk_phy->phy_rst)) {
  268. if (PTR_ERR(rk_phy->phy_rst) != -EPROBE_DEFER)
  269. dev_err(dev,
  270. "missing phy property for reset controller\n");
  271. return PTR_ERR(rk_phy->phy_rst);
  272. }
  273. rk_phy->clk_pciephy_ref = devm_clk_get(dev, "refclk");
  274. if (IS_ERR(rk_phy->clk_pciephy_ref)) {
  275. dev_err(dev, "refclk not found.\n");
  276. return PTR_ERR(rk_phy->clk_pciephy_ref);
  277. }
  278. generic_phy = devm_phy_create(dev, dev->of_node, &ops);
  279. if (IS_ERR(generic_phy)) {
  280. dev_err(dev, "failed to create PHY\n");
  281. return PTR_ERR(generic_phy);
  282. }
  283. phy_set_drvdata(generic_phy, rk_phy);
  284. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  285. return PTR_ERR_OR_ZERO(phy_provider);
  286. }
  287. static struct platform_driver rockchip_pcie_driver = {
  288. .probe = rockchip_pcie_phy_probe,
  289. .driver = {
  290. .name = "rockchip-pcie-phy",
  291. .of_match_table = rockchip_pcie_phy_dt_ids,
  292. },
  293. };
  294. module_platform_driver(rockchip_pcie_driver);
  295. MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
  296. MODULE_DESCRIPTION("Rockchip PCIe PHY driver");
  297. MODULE_LICENSE("GPL v2");