phy-mt7621-pci.c 9.8 KB

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