phy-rockchip-pcie.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 phy_pcie_instance {
  72. struct phy *phy;
  73. u32 index;
  74. } phys[PHY_MAX_LANE_NUM];
  75. struct mutex pcie_mutex;
  76. struct reset_control *phy_rst;
  77. struct clk *clk_pciephy_ref;
  78. int pwr_cnt;
  79. int init_cnt;
  80. };
  81. static struct rockchip_pcie_phy *to_pcie_phy(struct phy_pcie_instance *inst)
  82. {
  83. return container_of(inst, struct rockchip_pcie_phy,
  84. phys[inst->index]);
  85. }
  86. static struct phy *rockchip_pcie_phy_of_xlate(struct device *dev,
  87. struct of_phandle_args *args)
  88. {
  89. struct rockchip_pcie_phy *rk_phy = dev_get_drvdata(dev);
  90. if (args->args_count == 0)
  91. return rk_phy->phys[0].phy;
  92. if (WARN_ON(args->args[0] >= PHY_MAX_LANE_NUM))
  93. return ERR_PTR(-ENODEV);
  94. return rk_phy->phys[args->args[0]].phy;
  95. }
  96. static inline void phy_wr_cfg(struct rockchip_pcie_phy *rk_phy,
  97. u32 addr, u32 data)
  98. {
  99. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  100. HIWORD_UPDATE(data,
  101. PHY_CFG_DATA_MASK,
  102. PHY_CFG_DATA_SHIFT) |
  103. HIWORD_UPDATE(addr,
  104. PHY_CFG_ADDR_MASK,
  105. PHY_CFG_ADDR_SHIFT));
  106. udelay(1);
  107. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  108. HIWORD_UPDATE(PHY_CFG_WR_ENABLE,
  109. PHY_CFG_WR_MASK,
  110. PHY_CFG_WR_SHIFT));
  111. udelay(1);
  112. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  113. HIWORD_UPDATE(PHY_CFG_WR_DISABLE,
  114. PHY_CFG_WR_MASK,
  115. PHY_CFG_WR_SHIFT));
  116. }
  117. static inline u32 phy_rd_cfg(struct rockchip_pcie_phy *rk_phy,
  118. u32 addr)
  119. {
  120. u32 val;
  121. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  122. HIWORD_UPDATE(addr,
  123. PHY_CFG_RD_MASK,
  124. PHY_CFG_ADDR_SHIFT));
  125. regmap_read(rk_phy->reg_base,
  126. rk_phy->phy_data->pcie_status,
  127. &val);
  128. return val;
  129. }
  130. static int rockchip_pcie_phy_power_off(struct phy *phy)
  131. {
  132. struct phy_pcie_instance *inst = phy_get_drvdata(phy);
  133. struct rockchip_pcie_phy *rk_phy = to_pcie_phy(inst);
  134. int err = 0;
  135. mutex_lock(&rk_phy->pcie_mutex);
  136. regmap_write(rk_phy->reg_base,
  137. rk_phy->phy_data->pcie_laneoff,
  138. HIWORD_UPDATE(PHY_LANE_IDLE_OFF,
  139. PHY_LANE_IDLE_MASK,
  140. PHY_LANE_IDLE_A_SHIFT + inst->index));
  141. if (--rk_phy->pwr_cnt)
  142. goto err_out;
  143. err = reset_control_assert(rk_phy->phy_rst);
  144. if (err) {
  145. dev_err(&phy->dev, "assert phy_rst err %d\n", err);
  146. goto err_restore;
  147. }
  148. err_out:
  149. mutex_unlock(&rk_phy->pcie_mutex);
  150. return 0;
  151. err_restore:
  152. rk_phy->pwr_cnt++;
  153. regmap_write(rk_phy->reg_base,
  154. rk_phy->phy_data->pcie_laneoff,
  155. HIWORD_UPDATE(!PHY_LANE_IDLE_OFF,
  156. PHY_LANE_IDLE_MASK,
  157. PHY_LANE_IDLE_A_SHIFT + inst->index));
  158. mutex_unlock(&rk_phy->pcie_mutex);
  159. return err;
  160. }
  161. static int rockchip_pcie_phy_power_on(struct phy *phy)
  162. {
  163. struct phy_pcie_instance *inst = phy_get_drvdata(phy);
  164. struct rockchip_pcie_phy *rk_phy = to_pcie_phy(inst);
  165. int err = 0;
  166. u32 status;
  167. unsigned long timeout;
  168. mutex_lock(&rk_phy->pcie_mutex);
  169. if (rk_phy->pwr_cnt++)
  170. goto err_out;
  171. err = reset_control_deassert(rk_phy->phy_rst);
  172. if (err) {
  173. dev_err(&phy->dev, "deassert phy_rst err %d\n", err);
  174. goto err_pwr_cnt;
  175. }
  176. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  177. HIWORD_UPDATE(PHY_CFG_PLL_LOCK,
  178. PHY_CFG_ADDR_MASK,
  179. PHY_CFG_ADDR_SHIFT));
  180. regmap_write(rk_phy->reg_base,
  181. rk_phy->phy_data->pcie_laneoff,
  182. HIWORD_UPDATE(!PHY_LANE_IDLE_OFF,
  183. PHY_LANE_IDLE_MASK,
  184. PHY_LANE_IDLE_A_SHIFT + inst->index));
  185. /*
  186. * No documented timeout value for phy operation below,
  187. * so we make it large enough here. And we use loop-break
  188. * method which should not be harmful.
  189. */
  190. timeout = jiffies + msecs_to_jiffies(1000);
  191. err = -EINVAL;
  192. while (time_before(jiffies, timeout)) {
  193. regmap_read(rk_phy->reg_base,
  194. rk_phy->phy_data->pcie_status,
  195. &status);
  196. if (status & PHY_PLL_LOCKED) {
  197. dev_dbg(&phy->dev, "pll locked!\n");
  198. err = 0;
  199. break;
  200. }
  201. msleep(20);
  202. }
  203. if (err) {
  204. dev_err(&phy->dev, "pll lock timeout!\n");
  205. goto err_pll_lock;
  206. }
  207. phy_wr_cfg(rk_phy, PHY_CFG_CLK_TEST, PHY_CFG_SEPE_RATE);
  208. phy_wr_cfg(rk_phy, PHY_CFG_CLK_SCC, PHY_CFG_PLL_100M);
  209. err = -ETIMEDOUT;
  210. while (time_before(jiffies, timeout)) {
  211. regmap_read(rk_phy->reg_base,
  212. rk_phy->phy_data->pcie_status,
  213. &status);
  214. if (!(status & PHY_PLL_OUTPUT)) {
  215. dev_dbg(&phy->dev, "pll output enable done!\n");
  216. err = 0;
  217. break;
  218. }
  219. msleep(20);
  220. }
  221. if (err) {
  222. dev_err(&phy->dev, "pll output enable timeout!\n");
  223. goto err_pll_lock;
  224. }
  225. regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
  226. HIWORD_UPDATE(PHY_CFG_PLL_LOCK,
  227. PHY_CFG_ADDR_MASK,
  228. PHY_CFG_ADDR_SHIFT));
  229. err = -EINVAL;
  230. while (time_before(jiffies, timeout)) {
  231. regmap_read(rk_phy->reg_base,
  232. rk_phy->phy_data->pcie_status,
  233. &status);
  234. if (status & PHY_PLL_LOCKED) {
  235. dev_dbg(&phy->dev, "pll relocked!\n");
  236. err = 0;
  237. break;
  238. }
  239. msleep(20);
  240. }
  241. if (err) {
  242. dev_err(&phy->dev, "pll relock timeout!\n");
  243. goto err_pll_lock;
  244. }
  245. err_out:
  246. mutex_unlock(&rk_phy->pcie_mutex);
  247. return 0;
  248. err_pll_lock:
  249. reset_control_assert(rk_phy->phy_rst);
  250. err_pwr_cnt:
  251. rk_phy->pwr_cnt--;
  252. mutex_unlock(&rk_phy->pcie_mutex);
  253. return err;
  254. }
  255. static int rockchip_pcie_phy_init(struct phy *phy)
  256. {
  257. struct phy_pcie_instance *inst = phy_get_drvdata(phy);
  258. struct rockchip_pcie_phy *rk_phy = to_pcie_phy(inst);
  259. int err = 0;
  260. mutex_lock(&rk_phy->pcie_mutex);
  261. if (rk_phy->init_cnt++)
  262. goto err_out;
  263. err = clk_prepare_enable(rk_phy->clk_pciephy_ref);
  264. if (err) {
  265. dev_err(&phy->dev, "Fail to enable pcie ref clock.\n");
  266. goto err_refclk;
  267. }
  268. err = reset_control_assert(rk_phy->phy_rst);
  269. if (err) {
  270. dev_err(&phy->dev, "assert phy_rst err %d\n", err);
  271. goto err_reset;
  272. }
  273. err_out:
  274. mutex_unlock(&rk_phy->pcie_mutex);
  275. return 0;
  276. err_reset:
  277. clk_disable_unprepare(rk_phy->clk_pciephy_ref);
  278. err_refclk:
  279. rk_phy->init_cnt--;
  280. mutex_unlock(&rk_phy->pcie_mutex);
  281. return err;
  282. }
  283. static int rockchip_pcie_phy_exit(struct phy *phy)
  284. {
  285. struct phy_pcie_instance *inst = phy_get_drvdata(phy);
  286. struct rockchip_pcie_phy *rk_phy = to_pcie_phy(inst);
  287. mutex_lock(&rk_phy->pcie_mutex);
  288. if (--rk_phy->init_cnt)
  289. goto err_init_cnt;
  290. clk_disable_unprepare(rk_phy->clk_pciephy_ref);
  291. err_init_cnt:
  292. mutex_unlock(&rk_phy->pcie_mutex);
  293. return 0;
  294. }
  295. static const struct phy_ops ops = {
  296. .init = rockchip_pcie_phy_init,
  297. .exit = rockchip_pcie_phy_exit,
  298. .power_on = rockchip_pcie_phy_power_on,
  299. .power_off = rockchip_pcie_phy_power_off,
  300. .owner = THIS_MODULE,
  301. };
  302. static const struct rockchip_pcie_data rk3399_pcie_data = {
  303. .pcie_conf = 0xe220,
  304. .pcie_status = 0xe2a4,
  305. .pcie_laneoff = 0xe214,
  306. };
  307. static const struct of_device_id rockchip_pcie_phy_dt_ids[] = {
  308. {
  309. .compatible = "rockchip,rk3399-pcie-phy",
  310. .data = &rk3399_pcie_data,
  311. },
  312. {}
  313. };
  314. MODULE_DEVICE_TABLE(of, rockchip_pcie_phy_dt_ids);
  315. static int rockchip_pcie_phy_probe(struct platform_device *pdev)
  316. {
  317. struct device *dev = &pdev->dev;
  318. struct rockchip_pcie_phy *rk_phy;
  319. struct phy_provider *phy_provider;
  320. struct regmap *grf;
  321. const struct of_device_id *of_id;
  322. int i;
  323. u32 phy_num;
  324. grf = syscon_node_to_regmap(dev->parent->of_node);
  325. if (IS_ERR(grf)) {
  326. dev_err(dev, "Cannot find GRF syscon\n");
  327. return PTR_ERR(grf);
  328. }
  329. rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
  330. if (!rk_phy)
  331. return -ENOMEM;
  332. of_id = of_match_device(rockchip_pcie_phy_dt_ids, &pdev->dev);
  333. if (!of_id)
  334. return -EINVAL;
  335. rk_phy->phy_data = (struct rockchip_pcie_data *)of_id->data;
  336. rk_phy->reg_base = grf;
  337. mutex_init(&rk_phy->pcie_mutex);
  338. rk_phy->phy_rst = devm_reset_control_get(dev, "phy");
  339. if (IS_ERR(rk_phy->phy_rst)) {
  340. if (PTR_ERR(rk_phy->phy_rst) != -EPROBE_DEFER)
  341. dev_err(dev,
  342. "missing phy property for reset controller\n");
  343. return PTR_ERR(rk_phy->phy_rst);
  344. }
  345. rk_phy->clk_pciephy_ref = devm_clk_get(dev, "refclk");
  346. if (IS_ERR(rk_phy->clk_pciephy_ref)) {
  347. dev_err(dev, "refclk not found.\n");
  348. return PTR_ERR(rk_phy->clk_pciephy_ref);
  349. }
  350. /* parse #phy-cells to see if it's legacy PHY model */
  351. if (of_property_read_u32(dev->of_node, "#phy-cells", &phy_num))
  352. return -ENOENT;
  353. phy_num = (phy_num == 0) ? 1 : PHY_MAX_LANE_NUM;
  354. dev_dbg(dev, "phy number is %d\n", phy_num);
  355. for (i = 0; i < phy_num; i++) {
  356. rk_phy->phys[i].phy = devm_phy_create(dev, dev->of_node, &ops);
  357. if (IS_ERR(rk_phy->phys[i].phy)) {
  358. dev_err(dev, "failed to create PHY%d\n", i);
  359. return PTR_ERR(rk_phy->phys[i].phy);
  360. }
  361. rk_phy->phys[i].index = i;
  362. phy_set_drvdata(rk_phy->phys[i].phy, &rk_phy->phys[i]);
  363. }
  364. platform_set_drvdata(pdev, rk_phy);
  365. phy_provider = devm_of_phy_provider_register(dev,
  366. rockchip_pcie_phy_of_xlate);
  367. return PTR_ERR_OR_ZERO(phy_provider);
  368. }
  369. static struct platform_driver rockchip_pcie_driver = {
  370. .probe = rockchip_pcie_phy_probe,
  371. .driver = {
  372. .name = "rockchip-pcie-phy",
  373. .of_match_table = rockchip_pcie_phy_dt_ids,
  374. },
  375. };
  376. module_platform_driver(rockchip_pcie_driver);
  377. MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
  378. MODULE_DESCRIPTION("Rockchip PCIe PHY driver");
  379. MODULE_LICENSE("GPL v2");