1 // SPDX-License-Identifier: GPL-2.0+
3 * Mediatek MT7621 PCI PHY Driver
4 * Author: Sergio Paracuellos <sergio.paracuellos@gmail.com>
7 #include <dt-bindings/phy/phy.h>
9 #include <linux/bitfield.h>
10 #include <linux/bitops.h>
11 #include <linux/module.h>
13 #include <linux/phy/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/sys_soc.h>
18 #define RG_PE1_PIPE_REG 0x02c
19 #define RG_PE1_PIPE_RST BIT(12)
20 #define RG_PE1_PIPE_CMD_FRC BIT(4)
22 #define RG_P0_TO_P1_WIDTH 0x100
23 #define RG_PE1_H_LCDDS_REG 0x49c
24 #define RG_PE1_H_LCDDS_PCW GENMASK(30, 0)
26 #define RG_PE1_FRC_H_XTAL_REG 0x400
27 #define RG_PE1_FRC_H_XTAL_TYPE BIT(8)
28 #define RG_PE1_H_XTAL_TYPE GENMASK(10, 9)
30 #define RG_PE1_FRC_PHY_REG 0x000
31 #define RG_PE1_FRC_PHY_EN BIT(4)
32 #define RG_PE1_PHY_EN BIT(5)
34 #define RG_PE1_H_PLL_REG 0x490
35 #define RG_PE1_H_PLL_BC GENMASK(23, 22)
36 #define RG_PE1_H_PLL_BP GENMASK(21, 18)
37 #define RG_PE1_H_PLL_IR GENMASK(15, 12)
38 #define RG_PE1_H_PLL_IC GENMASK(11, 8)
39 #define RG_PE1_H_PLL_PREDIV GENMASK(7, 6)
40 #define RG_PE1_PLL_DIVEN GENMASK(3, 1)
42 #define RG_PE1_H_PLL_FBKSEL_REG 0x4bc
43 #define RG_PE1_H_PLL_FBKSEL GENMASK(5, 4)
45 #define RG_PE1_H_LCDDS_SSC_PRD_REG 0x4a4
46 #define RG_PE1_H_LCDDS_SSC_PRD GENMASK(15, 0)
48 #define RG_PE1_H_LCDDS_SSC_DELTA_REG 0x4a8
49 #define RG_PE1_H_LCDDS_SSC_DELTA GENMASK(11, 0)
50 #define RG_PE1_H_LCDDS_SSC_DELTA1 GENMASK(27, 16)
52 #define RG_PE1_LCDDS_CLK_PH_INV_REG 0x4a0
53 #define RG_PE1_LCDDS_CLK_PH_INV BIT(5)
55 #define RG_PE1_H_PLL_BR_REG 0x4ac
56 #define RG_PE1_H_PLL_BR GENMASK(18, 16)
58 #define RG_PE1_MSTCKDIV_REG 0x414
59 #define RG_PE1_MSTCKDIV GENMASK(7, 6)
61 #define RG_PE1_FRC_MSTCKDIV BIT(5)
66 * struct mt7621_pci_phy - Mt7621 Pcie PHY core
67 * @dev: pointer to device
68 * @regmap: kernel regmap pointer
69 * @phy: pointer to the kernel PHY device
70 * @sys_clk: pointer to the system XTAL clock
71 * @port_base: base register
72 * @has_dual_port: if the phy has dual ports.
73 * @bypass_pipe_rst: mark if 'mt7621_bypass_pipe_rst'
74 * needs to be executed. Depends on chip revision.
76 struct mt7621_pci_phy {
78 struct regmap *regmap;
81 void __iomem *port_base;
86 static inline void mt7621_phy_rmw(struct mt7621_pci_phy *phy,
87 u32 reg, u32 clr, u32 set)
92 * We cannot use 'regmap_write_bits' here because internally
93 * 'set' is masked before is set to the value that will be
94 * written to the register. That way results in no reliable
95 * pci setup. Avoid to mask 'set' before set value to 'val'
96 * completely avoid the problem.
98 regmap_read(phy->regmap, reg, &val);
101 regmap_write(phy->regmap, reg, val);
104 static void mt7621_bypass_pipe_rst(struct mt7621_pci_phy *phy)
106 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG, 0, RG_PE1_PIPE_RST);
107 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG, 0, RG_PE1_PIPE_CMD_FRC);
109 if (phy->has_dual_port) {
110 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG + RG_P0_TO_P1_WIDTH,
112 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG + RG_P0_TO_P1_WIDTH,
113 0, RG_PE1_PIPE_CMD_FRC);
117 static int mt7621_set_phy_for_ssc(struct mt7621_pci_phy *phy)
119 struct device *dev = phy->dev;
120 unsigned long clk_rate;
122 clk_rate = clk_get_rate(phy->sys_clk);
126 /* Set PCIe Port PHY to disable SSC */
127 /* Debug Xtal Type */
128 mt7621_phy_rmw(phy, RG_PE1_FRC_H_XTAL_REG,
129 RG_PE1_FRC_H_XTAL_TYPE | RG_PE1_H_XTAL_TYPE,
130 RG_PE1_FRC_H_XTAL_TYPE |
131 FIELD_PREP(RG_PE1_H_XTAL_TYPE, 0x00));
134 mt7621_phy_rmw(phy, RG_PE1_FRC_PHY_REG, RG_PE1_PHY_EN,
137 if (phy->has_dual_port) {
138 mt7621_phy_rmw(phy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
139 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
142 if (clk_rate == 40000000) { /* 40MHz Xtal */
143 /* Set Pre-divider ratio (for host mode) */
144 mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG, RG_PE1_H_PLL_PREDIV,
145 FIELD_PREP(RG_PE1_H_PLL_PREDIV, 0x01));
147 dev_dbg(dev, "Xtal is 40MHz\n");
148 } else if (clk_rate == 25000000) { /* 25MHz Xal */
149 mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG, RG_PE1_H_PLL_PREDIV,
150 FIELD_PREP(RG_PE1_H_PLL_PREDIV, 0x00));
152 /* Select feedback clock */
153 mt7621_phy_rmw(phy, RG_PE1_H_PLL_FBKSEL_REG,
155 FIELD_PREP(RG_PE1_H_PLL_FBKSEL, 0x01));
157 /* DDS NCPO PCW (for host mode) */
158 mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_PRD_REG,
159 RG_PE1_H_LCDDS_SSC_PRD,
160 FIELD_PREP(RG_PE1_H_LCDDS_SSC_PRD, 0x00));
162 /* DDS SSC dither period control */
163 mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_PRD_REG,
164 RG_PE1_H_LCDDS_SSC_PRD,
165 FIELD_PREP(RG_PE1_H_LCDDS_SSC_PRD, 0x18d));
167 /* DDS SSC dither amplitude control */
168 mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_DELTA_REG,
169 RG_PE1_H_LCDDS_SSC_DELTA |
170 RG_PE1_H_LCDDS_SSC_DELTA1,
171 FIELD_PREP(RG_PE1_H_LCDDS_SSC_DELTA, 0x4a) |
172 FIELD_PREP(RG_PE1_H_LCDDS_SSC_DELTA1, 0x4a));
174 dev_dbg(dev, "Xtal is 25MHz\n");
175 } else { /* 20MHz Xtal */
176 mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG, RG_PE1_H_PLL_PREDIV,
177 FIELD_PREP(RG_PE1_H_PLL_PREDIV, 0x00));
179 dev_dbg(dev, "Xtal is 20MHz\n");
182 /* DDS clock inversion */
183 mt7621_phy_rmw(phy, RG_PE1_LCDDS_CLK_PH_INV_REG,
184 RG_PE1_LCDDS_CLK_PH_INV, RG_PE1_LCDDS_CLK_PH_INV);
187 mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG,
188 RG_PE1_H_PLL_BC | RG_PE1_H_PLL_BP | RG_PE1_H_PLL_IR |
189 RG_PE1_H_PLL_IC | RG_PE1_PLL_DIVEN,
190 FIELD_PREP(RG_PE1_H_PLL_BC, 0x02) |
191 FIELD_PREP(RG_PE1_H_PLL_BP, 0x06) |
192 FIELD_PREP(RG_PE1_H_PLL_IR, 0x02) |
193 FIELD_PREP(RG_PE1_H_PLL_IC, 0x01) |
194 FIELD_PREP(RG_PE1_PLL_DIVEN, 0x02));
196 mt7621_phy_rmw(phy, RG_PE1_H_PLL_BR_REG, RG_PE1_H_PLL_BR,
197 FIELD_PREP(RG_PE1_H_PLL_BR, 0x00));
199 if (clk_rate == 40000000) { /* 40MHz Xtal */
200 /* set force mode enable of da_pe1_mstckdiv */
201 mt7621_phy_rmw(phy, RG_PE1_MSTCKDIV_REG,
202 RG_PE1_MSTCKDIV | RG_PE1_FRC_MSTCKDIV,
203 FIELD_PREP(RG_PE1_MSTCKDIV, 0x01) |
204 RG_PE1_FRC_MSTCKDIV);
210 static int mt7621_pci_phy_init(struct phy *phy)
212 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
214 if (mphy->bypass_pipe_rst)
215 mt7621_bypass_pipe_rst(mphy);
217 return mt7621_set_phy_for_ssc(mphy);
220 static int mt7621_pci_phy_power_on(struct phy *phy)
222 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
224 /* Enable PHY and disable force mode */
225 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG,
226 RG_PE1_FRC_PHY_EN, RG_PE1_PHY_EN);
228 if (mphy->has_dual_port) {
229 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
230 RG_PE1_FRC_PHY_EN, RG_PE1_PHY_EN);
236 static int mt7621_pci_phy_power_off(struct phy *phy)
238 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
241 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG,
242 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
244 if (mphy->has_dual_port) {
245 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
246 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
252 static int mt7621_pci_phy_exit(struct phy *phy)
257 static const struct phy_ops mt7621_pci_phy_ops = {
258 .init = mt7621_pci_phy_init,
259 .exit = mt7621_pci_phy_exit,
260 .power_on = mt7621_pci_phy_power_on,
261 .power_off = mt7621_pci_phy_power_off,
262 .owner = THIS_MODULE,
265 static struct phy *mt7621_pcie_phy_of_xlate(struct device *dev,
266 struct of_phandle_args *args)
268 struct mt7621_pci_phy *mt7621_phy = dev_get_drvdata(dev);
270 if (WARN_ON(args->args[0] >= MAX_PHYS))
271 return ERR_PTR(-ENODEV);
273 mt7621_phy->has_dual_port = args->args[0];
275 dev_dbg(dev, "PHY for 0x%px (dual port = %d)\n",
276 mt7621_phy->port_base, mt7621_phy->has_dual_port);
278 return mt7621_phy->phy;
281 static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
282 { .soc_id = "mt7621", .revision = "E2" },
286 static const struct regmap_config mt7621_pci_phy_regmap_config = {
290 .max_register = 0x700,
293 static int mt7621_pci_phy_probe(struct platform_device *pdev)
295 struct device *dev = &pdev->dev;
296 const struct soc_device_attribute *attr;
297 struct phy_provider *provider;
298 struct mt7621_pci_phy *phy;
300 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
304 attr = soc_device_match(mt7621_pci_quirks_match);
306 phy->bypass_pipe_rst = true;
309 platform_set_drvdata(pdev, phy);
311 phy->port_base = devm_platform_ioremap_resource(pdev, 0);
312 if (IS_ERR(phy->port_base)) {
313 dev_err(dev, "failed to remap phy regs\n");
314 return PTR_ERR(phy->port_base);
317 phy->regmap = devm_regmap_init_mmio(phy->dev, phy->port_base,
318 &mt7621_pci_phy_regmap_config);
319 if (IS_ERR(phy->regmap))
320 return PTR_ERR(phy->regmap);
322 phy->phy = devm_phy_create(dev, dev->of_node, &mt7621_pci_phy_ops);
323 if (IS_ERR(phy->phy)) {
324 dev_err(dev, "failed to create phy\n");
325 return PTR_ERR(phy->phy);
328 phy->sys_clk = devm_clk_get(dev, NULL);
329 if (IS_ERR(phy->sys_clk)) {
330 dev_err(dev, "failed to get phy clock\n");
331 return PTR_ERR(phy->sys_clk);
334 phy_set_drvdata(phy->phy, phy);
336 provider = devm_of_phy_provider_register(dev, mt7621_pcie_phy_of_xlate);
338 return PTR_ERR_OR_ZERO(provider);
341 static const struct of_device_id mt7621_pci_phy_ids[] = {
342 { .compatible = "mediatek,mt7621-pci-phy" },
345 MODULE_DEVICE_TABLE(of, mt7621_pci_phy_ids);
347 static struct platform_driver mt7621_pci_phy_driver = {
348 .probe = mt7621_pci_phy_probe,
350 .name = "mt7621-pci-phy",
351 .of_match_table = mt7621_pci_phy_ids,
355 builtin_platform_driver(mt7621_pci_phy_driver);
357 MODULE_AUTHOR("Sergio Paracuellos <sergio.paracuellos@gmail.com>");
358 MODULE_DESCRIPTION("MediaTek MT7621 PCIe PHY driver");
359 MODULE_LICENSE("GPL v2");