phy-mt7621-pci.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Mediatek MT7621 PCI PHY Driver
  4. * Author: Sergio Paracuellos <sergio.paracuellos@gmail.com>
  5. */
  6. #include <dt-bindings/phy/phy.h>
  7. #include <linux/clk.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/bitops.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/phy/phy.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/sys_soc.h>
  16. #define RG_PE1_PIPE_REG 0x02c
  17. #define RG_PE1_PIPE_RST BIT(12)
  18. #define RG_PE1_PIPE_CMD_FRC BIT(4)
  19. #define RG_P0_TO_P1_WIDTH 0x100
  20. #define RG_PE1_H_LCDDS_REG 0x49c
  21. #define RG_PE1_H_LCDDS_PCW GENMASK(30, 0)
  22. #define RG_PE1_FRC_H_XTAL_REG 0x400
  23. #define RG_PE1_FRC_H_XTAL_TYPE BIT(8)
  24. #define RG_PE1_H_XTAL_TYPE GENMASK(10, 9)
  25. #define RG_PE1_FRC_PHY_REG 0x000
  26. #define RG_PE1_FRC_PHY_EN BIT(4)
  27. #define RG_PE1_PHY_EN BIT(5)
  28. #define RG_PE1_H_PLL_REG 0x490
  29. #define RG_PE1_H_PLL_BC GENMASK(23, 22)
  30. #define RG_PE1_H_PLL_BP GENMASK(21, 18)
  31. #define RG_PE1_H_PLL_IR GENMASK(15, 12)
  32. #define RG_PE1_H_PLL_IC GENMASK(11, 8)
  33. #define RG_PE1_H_PLL_PREDIV GENMASK(7, 6)
  34. #define RG_PE1_PLL_DIVEN GENMASK(3, 1)
  35. #define RG_PE1_H_PLL_FBKSEL_REG 0x4bc
  36. #define RG_PE1_H_PLL_FBKSEL GENMASK(5, 4)
  37. #define RG_PE1_H_LCDDS_SSC_PRD_REG 0x4a4
  38. #define RG_PE1_H_LCDDS_SSC_PRD GENMASK(15, 0)
  39. #define RG_PE1_H_LCDDS_SSC_DELTA_REG 0x4a8
  40. #define RG_PE1_H_LCDDS_SSC_DELTA GENMASK(11, 0)
  41. #define RG_PE1_H_LCDDS_SSC_DELTA1 GENMASK(27, 16)
  42. #define RG_PE1_LCDDS_CLK_PH_INV_REG 0x4a0
  43. #define RG_PE1_LCDDS_CLK_PH_INV BIT(5)
  44. #define RG_PE1_H_PLL_BR_REG 0x4ac
  45. #define RG_PE1_H_PLL_BR GENMASK(18, 16)
  46. #define RG_PE1_MSTCKDIV_REG 0x414
  47. #define RG_PE1_MSTCKDIV GENMASK(7, 6)
  48. #define RG_PE1_FRC_MSTCKDIV BIT(5)
  49. #define MAX_PHYS 2
  50. /**
  51. * struct mt7621_pci_phy - Mt7621 Pcie PHY core
  52. * @dev: pointer to device
  53. * @regmap: kernel regmap pointer
  54. * @phy: pointer to the kernel PHY device
  55. * @sys_clk: pointer to the system XTAL clock
  56. * @port_base: base register
  57. * @has_dual_port: if the phy has dual ports.
  58. * @bypass_pipe_rst: mark if 'mt7621_bypass_pipe_rst'
  59. * needs to be executed. Depends on chip revision.
  60. */
  61. struct mt7621_pci_phy {
  62. struct device *dev;
  63. struct regmap *regmap;
  64. struct phy *phy;
  65. struct clk *sys_clk;
  66. void __iomem *port_base;
  67. bool has_dual_port;
  68. bool bypass_pipe_rst;
  69. };
  70. static inline void mt7621_phy_rmw(struct mt7621_pci_phy *phy,
  71. u32 reg, u32 clr, u32 set)
  72. {
  73. u32 val;
  74. /*
  75. * We cannot use 'regmap_write_bits' here because internally
  76. * 'set' is masked before is set to the value that will be
  77. * written to the register. That way results in no reliable
  78. * pci setup. Avoid to mask 'set' before set value to 'val'
  79. * completely avoid the problem.
  80. */
  81. regmap_read(phy->regmap, reg, &val);
  82. val &= ~clr;
  83. val |= set;
  84. regmap_write(phy->regmap, reg, val);
  85. }
  86. static void mt7621_bypass_pipe_rst(struct mt7621_pci_phy *phy)
  87. {
  88. mt7621_phy_rmw(phy, RG_PE1_PIPE_REG, 0, RG_PE1_PIPE_RST);
  89. mt7621_phy_rmw(phy, RG_PE1_PIPE_REG, 0, RG_PE1_PIPE_CMD_FRC);
  90. if (phy->has_dual_port) {
  91. mt7621_phy_rmw(phy, RG_PE1_PIPE_REG + RG_P0_TO_P1_WIDTH,
  92. 0, RG_PE1_PIPE_RST);
  93. mt7621_phy_rmw(phy, RG_PE1_PIPE_REG + RG_P0_TO_P1_WIDTH,
  94. 0, RG_PE1_PIPE_CMD_FRC);
  95. }
  96. }
  97. static int mt7621_set_phy_for_ssc(struct mt7621_pci_phy *phy)
  98. {
  99. struct device *dev = phy->dev;
  100. unsigned long clk_rate;
  101. clk_rate = clk_get_rate(phy->sys_clk);
  102. if (!clk_rate)
  103. return -EINVAL;
  104. /* Set PCIe Port PHY to disable SSC */
  105. /* Debug Xtal Type */
  106. mt7621_phy_rmw(phy, RG_PE1_FRC_H_XTAL_REG,
  107. RG_PE1_FRC_H_XTAL_TYPE | RG_PE1_H_XTAL_TYPE,
  108. RG_PE1_FRC_H_XTAL_TYPE |
  109. FIELD_PREP(RG_PE1_H_XTAL_TYPE, 0x00));
  110. /* disable port */
  111. mt7621_phy_rmw(phy, RG_PE1_FRC_PHY_REG, RG_PE1_PHY_EN,
  112. RG_PE1_FRC_PHY_EN);
  113. if (phy->has_dual_port) {
  114. mt7621_phy_rmw(phy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
  115. RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
  116. }
  117. if (clk_rate == 40000000) { /* 40MHz Xtal */
  118. /* Set Pre-divider ratio (for host mode) */
  119. mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG, RG_PE1_H_PLL_PREDIV,
  120. FIELD_PREP(RG_PE1_H_PLL_PREDIV, 0x01));
  121. dev_dbg(dev, "Xtal is 40MHz\n");
  122. } else if (clk_rate == 25000000) { /* 25MHz Xal */
  123. mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG, RG_PE1_H_PLL_PREDIV,
  124. FIELD_PREP(RG_PE1_H_PLL_PREDIV, 0x00));
  125. /* Select feedback clock */
  126. mt7621_phy_rmw(phy, RG_PE1_H_PLL_FBKSEL_REG,
  127. RG_PE1_H_PLL_FBKSEL,
  128. FIELD_PREP(RG_PE1_H_PLL_FBKSEL, 0x01));
  129. /* DDS NCPO PCW (for host mode) */
  130. mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_PRD_REG,
  131. RG_PE1_H_LCDDS_SSC_PRD,
  132. FIELD_PREP(RG_PE1_H_LCDDS_SSC_PRD, 0x00));
  133. /* DDS SSC dither period control */
  134. mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_PRD_REG,
  135. RG_PE1_H_LCDDS_SSC_PRD,
  136. FIELD_PREP(RG_PE1_H_LCDDS_SSC_PRD, 0x18d));
  137. /* DDS SSC dither amplitude control */
  138. mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_DELTA_REG,
  139. RG_PE1_H_LCDDS_SSC_DELTA |
  140. RG_PE1_H_LCDDS_SSC_DELTA1,
  141. FIELD_PREP(RG_PE1_H_LCDDS_SSC_DELTA, 0x4a) |
  142. FIELD_PREP(RG_PE1_H_LCDDS_SSC_DELTA1, 0x4a));
  143. dev_dbg(dev, "Xtal is 25MHz\n");
  144. } else { /* 20MHz Xtal */
  145. mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG, RG_PE1_H_PLL_PREDIV,
  146. FIELD_PREP(RG_PE1_H_PLL_PREDIV, 0x00));
  147. dev_dbg(dev, "Xtal is 20MHz\n");
  148. }
  149. /* DDS clock inversion */
  150. mt7621_phy_rmw(phy, RG_PE1_LCDDS_CLK_PH_INV_REG,
  151. RG_PE1_LCDDS_CLK_PH_INV, RG_PE1_LCDDS_CLK_PH_INV);
  152. /* Set PLL bits */
  153. mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG,
  154. RG_PE1_H_PLL_BC | RG_PE1_H_PLL_BP | RG_PE1_H_PLL_IR |
  155. RG_PE1_H_PLL_IC | RG_PE1_PLL_DIVEN,
  156. FIELD_PREP(RG_PE1_H_PLL_BC, 0x02) |
  157. FIELD_PREP(RG_PE1_H_PLL_BP, 0x06) |
  158. FIELD_PREP(RG_PE1_H_PLL_IR, 0x02) |
  159. FIELD_PREP(RG_PE1_H_PLL_IC, 0x01) |
  160. FIELD_PREP(RG_PE1_PLL_DIVEN, 0x02));
  161. mt7621_phy_rmw(phy, RG_PE1_H_PLL_BR_REG, RG_PE1_H_PLL_BR,
  162. FIELD_PREP(RG_PE1_H_PLL_BR, 0x00));
  163. if (clk_rate == 40000000) { /* 40MHz Xtal */
  164. /* set force mode enable of da_pe1_mstckdiv */
  165. mt7621_phy_rmw(phy, RG_PE1_MSTCKDIV_REG,
  166. RG_PE1_MSTCKDIV | RG_PE1_FRC_MSTCKDIV,
  167. FIELD_PREP(RG_PE1_MSTCKDIV, 0x01) |
  168. RG_PE1_FRC_MSTCKDIV);
  169. }
  170. return 0;
  171. }
  172. static int mt7621_pci_phy_init(struct phy *phy)
  173. {
  174. struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
  175. if (mphy->bypass_pipe_rst)
  176. mt7621_bypass_pipe_rst(mphy);
  177. return mt7621_set_phy_for_ssc(mphy);
  178. }
  179. static int mt7621_pci_phy_power_on(struct phy *phy)
  180. {
  181. struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
  182. /* Enable PHY and disable force mode */
  183. mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG,
  184. RG_PE1_FRC_PHY_EN, RG_PE1_PHY_EN);
  185. if (mphy->has_dual_port) {
  186. mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
  187. RG_PE1_FRC_PHY_EN, RG_PE1_PHY_EN);
  188. }
  189. return 0;
  190. }
  191. static int mt7621_pci_phy_power_off(struct phy *phy)
  192. {
  193. struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
  194. /* Disable PHY */
  195. mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG,
  196. RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
  197. if (mphy->has_dual_port) {
  198. mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
  199. RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
  200. }
  201. return 0;
  202. }
  203. static int mt7621_pci_phy_exit(struct phy *phy)
  204. {
  205. return 0;
  206. }
  207. static const struct phy_ops mt7621_pci_phy_ops = {
  208. .init = mt7621_pci_phy_init,
  209. .exit = mt7621_pci_phy_exit,
  210. .power_on = mt7621_pci_phy_power_on,
  211. .power_off = mt7621_pci_phy_power_off,
  212. .owner = THIS_MODULE,
  213. };
  214. static struct phy *mt7621_pcie_phy_of_xlate(struct device *dev,
  215. const struct of_phandle_args *args)
  216. {
  217. struct mt7621_pci_phy *mt7621_phy = dev_get_drvdata(dev);
  218. if (WARN_ON(args->args[0] >= MAX_PHYS))
  219. return ERR_PTR(-ENODEV);
  220. mt7621_phy->has_dual_port = args->args[0];
  221. dev_dbg(dev, "PHY for 0x%px (dual port = %d)\n",
  222. mt7621_phy->port_base, mt7621_phy->has_dual_port);
  223. return mt7621_phy->phy;
  224. }
  225. static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
  226. { .soc_id = "mt7621", .revision = "E2" },
  227. { /* sentinel */ }
  228. };
  229. static const struct regmap_config mt7621_pci_phy_regmap_config = {
  230. .reg_bits = 32,
  231. .val_bits = 32,
  232. .reg_stride = 4,
  233. .max_register = 0x700,
  234. };
  235. static int mt7621_pci_phy_probe(struct platform_device *pdev)
  236. {
  237. struct device *dev = &pdev->dev;
  238. const struct soc_device_attribute *attr;
  239. struct phy_provider *provider;
  240. struct mt7621_pci_phy *phy;
  241. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  242. if (!phy)
  243. return -ENOMEM;
  244. attr = soc_device_match(mt7621_pci_quirks_match);
  245. if (attr)
  246. phy->bypass_pipe_rst = true;
  247. phy->dev = dev;
  248. platform_set_drvdata(pdev, phy);
  249. phy->port_base = devm_platform_ioremap_resource(pdev, 0);
  250. if (IS_ERR(phy->port_base)) {
  251. dev_err(dev, "failed to remap phy regs\n");
  252. return PTR_ERR(phy->port_base);
  253. }
  254. phy->regmap = devm_regmap_init_mmio(phy->dev, phy->port_base,
  255. &mt7621_pci_phy_regmap_config);
  256. if (IS_ERR(phy->regmap))
  257. return PTR_ERR(phy->regmap);
  258. phy->phy = devm_phy_create(dev, dev->of_node, &mt7621_pci_phy_ops);
  259. if (IS_ERR(phy->phy)) {
  260. dev_err(dev, "failed to create phy\n");
  261. return PTR_ERR(phy->phy);
  262. }
  263. phy->sys_clk = devm_clk_get(dev, NULL);
  264. if (IS_ERR(phy->sys_clk)) {
  265. dev_err(dev, "failed to get phy clock\n");
  266. return PTR_ERR(phy->sys_clk);
  267. }
  268. phy_set_drvdata(phy->phy, phy);
  269. provider = devm_of_phy_provider_register(dev, mt7621_pcie_phy_of_xlate);
  270. return PTR_ERR_OR_ZERO(provider);
  271. }
  272. static const struct of_device_id mt7621_pci_phy_ids[] = {
  273. { .compatible = "mediatek,mt7621-pci-phy" },
  274. {},
  275. };
  276. MODULE_DEVICE_TABLE(of, mt7621_pci_phy_ids);
  277. static struct platform_driver mt7621_pci_phy_driver = {
  278. .probe = mt7621_pci_phy_probe,
  279. .driver = {
  280. .name = "mt7621-pci-phy",
  281. .of_match_table = mt7621_pci_phy_ids,
  282. },
  283. };
  284. builtin_platform_driver(mt7621_pci_phy_driver);
  285. MODULE_AUTHOR("Sergio Paracuellos <sergio.paracuellos@gmail.com>");
  286. MODULE_DESCRIPTION("MediaTek MT7621 PCIe PHY driver");
  287. MODULE_LICENSE("GPL v2");