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>
8 #include <linux/bitops.h>
9 #include <linux/module.h>
10 #include <linux/of_address.h>
11 #include <linux/of_device.h>
12 #include <linux/phy/phy.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/sys_soc.h>
17 #include <ralink_regs.h>
19 #define RG_PE1_PIPE_REG 0x02c
20 #define RG_PE1_PIPE_RST BIT(12)
21 #define RG_PE1_PIPE_CMD_FRC BIT(4)
23 #define RG_P0_TO_P1_WIDTH 0x100
24 #define RG_PE1_H_LCDDS_REG 0x49c
25 #define RG_PE1_H_LCDDS_PCW GENMASK(30, 0)
26 #define RG_PE1_H_LCDDS_PCW_VAL(x) ((0x7fffffff & (x)) << 0)
28 #define RG_PE1_FRC_H_XTAL_REG 0x400
29 #define RG_PE1_FRC_H_XTAL_TYPE BIT(8)
30 #define RG_PE1_H_XTAL_TYPE GENMASK(10, 9)
31 #define RG_PE1_H_XTAL_TYPE_VAL(x) ((0x3 & (x)) << 9)
33 #define RG_PE1_FRC_PHY_REG 0x000
34 #define RG_PE1_FRC_PHY_EN BIT(4)
35 #define RG_PE1_PHY_EN BIT(5)
37 #define RG_PE1_H_PLL_REG 0x490
38 #define RG_PE1_H_PLL_BC GENMASK(23, 22)
39 #define RG_PE1_H_PLL_BC_VAL(x) ((0x3 & (x)) << 22)
40 #define RG_PE1_H_PLL_BP GENMASK(21, 18)
41 #define RG_PE1_H_PLL_BP_VAL(x) ((0xf & (x)) << 18)
42 #define RG_PE1_H_PLL_IR GENMASK(15, 12)
43 #define RG_PE1_H_PLL_IR_VAL(x) ((0xf & (x)) << 12)
44 #define RG_PE1_H_PLL_IC GENMASK(11, 8)
45 #define RG_PE1_H_PLL_IC_VAL(x) ((0xf & (x)) << 8)
46 #define RG_PE1_H_PLL_PREDIV GENMASK(7, 6)
47 #define RG_PE1_H_PLL_PREDIV_VAL(x) ((0x3 & (x)) << 6)
48 #define RG_PE1_PLL_DIVEN GENMASK(3, 1)
49 #define RG_PE1_PLL_DIVEN_VAL(x) ((0x7 & (x)) << 1)
51 #define RG_PE1_H_PLL_FBKSEL_REG 0x4bc
52 #define RG_PE1_H_PLL_FBKSEL GENMASK(5, 4)
53 #define RG_PE1_H_PLL_FBKSEL_VAL(x) ((0x3 & (x)) << 4)
55 #define RG_PE1_H_LCDDS_SSC_PRD_REG 0x4a4
56 #define RG_PE1_H_LCDDS_SSC_PRD GENMASK(15, 0)
57 #define RG_PE1_H_LCDDS_SSC_PRD_VAL(x) ((0xffff & (x)) << 0)
59 #define RG_PE1_H_LCDDS_SSC_DELTA_REG 0x4a8
60 #define RG_PE1_H_LCDDS_SSC_DELTA GENMASK(11, 0)
61 #define RG_PE1_H_LCDDS_SSC_DELTA_VAL(x) ((0xfff & (x)) << 0)
62 #define RG_PE1_H_LCDDS_SSC_DELTA1 GENMASK(27, 16)
63 #define RG_PE1_H_LCDDS_SSC_DELTA1_VAL(x) ((0xff & (x)) << 16)
65 #define RG_PE1_LCDDS_CLK_PH_INV_REG 0x4a0
66 #define RG_PE1_LCDDS_CLK_PH_INV BIT(5)
68 #define RG_PE1_H_PLL_BR_REG 0x4ac
69 #define RG_PE1_H_PLL_BR GENMASK(18, 16)
70 #define RG_PE1_H_PLL_BR_VAL(x) ((0x7 & (x)) << 16)
72 #define RG_PE1_MSTCKDIV_REG 0x414
73 #define RG_PE1_MSTCKDIV GENMASK(7, 6)
74 #define RG_PE1_MSTCKDIV_VAL(x) ((0x3 & (x)) << 6)
76 #define RG_PE1_FRC_MSTCKDIV BIT(5)
78 #define XTAL_MODE_SEL_SHIFT 6
79 #define XTAL_MODE_SEL_MASK 0x7
84 * struct mt7621_pci_phy - Mt7621 Pcie PHY core
85 * @dev: pointer to device
86 * @regmap: kernel regmap pointer
87 * @phy: pointer to the kernel PHY device
88 * @port_base: base register
89 * @has_dual_port: if the phy has dual ports.
90 * @bypass_pipe_rst: mark if 'mt7621_bypass_pipe_rst'
91 * needs to be executed. Depends on chip revision.
93 struct mt7621_pci_phy {
95 struct regmap *regmap;
97 void __iomem *port_base;
102 static inline u32 phy_read(struct mt7621_pci_phy *phy, u32 reg)
106 regmap_read(phy->regmap, reg, &val);
111 static inline void phy_write(struct mt7621_pci_phy *phy, u32 val, u32 reg)
113 regmap_write(phy->regmap, reg, val);
116 static inline void mt7621_phy_rmw(struct mt7621_pci_phy *phy,
117 u32 reg, u32 clr, u32 set)
119 u32 val = phy_read(phy, reg);
123 phy_write(phy, val, reg);
126 static void mt7621_bypass_pipe_rst(struct mt7621_pci_phy *phy)
128 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG, 0, RG_PE1_PIPE_RST);
129 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG, 0, RG_PE1_PIPE_CMD_FRC);
131 if (phy->has_dual_port) {
132 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG + RG_P0_TO_P1_WIDTH,
134 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG + RG_P0_TO_P1_WIDTH,
135 0, RG_PE1_PIPE_CMD_FRC);
139 static void mt7621_set_phy_for_ssc(struct mt7621_pci_phy *phy)
141 struct device *dev = phy->dev;
144 xtal_mode = (rt_sysc_r32(SYSC_REG_SYSTEM_CONFIG0)
145 >> XTAL_MODE_SEL_SHIFT) & XTAL_MODE_SEL_MASK;
147 /* Set PCIe Port PHY to disable SSC */
148 /* Debug Xtal Type */
149 mt7621_phy_rmw(phy, RG_PE1_FRC_H_XTAL_REG,
150 RG_PE1_FRC_H_XTAL_TYPE | RG_PE1_H_XTAL_TYPE,
151 RG_PE1_FRC_H_XTAL_TYPE | RG_PE1_H_XTAL_TYPE_VAL(0x00));
154 mt7621_phy_rmw(phy, RG_PE1_FRC_PHY_REG,
155 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
157 if (phy->has_dual_port) {
158 mt7621_phy_rmw(phy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
159 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
162 if (xtal_mode <= 5 && xtal_mode >= 3) { /* 40MHz Xtal */
163 /* Set Pre-divider ratio (for host mode) */
164 mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG,
166 RG_PE1_H_PLL_PREDIV_VAL(0x01));
167 dev_info(dev, "Xtal is 40MHz\n");
168 } else if (xtal_mode >= 6) { /* 25MHz Xal */
169 mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG,
171 RG_PE1_H_PLL_PREDIV_VAL(0x00));
172 /* Select feedback clock */
173 mt7621_phy_rmw(phy, RG_PE1_H_PLL_FBKSEL_REG,
175 RG_PE1_H_PLL_FBKSEL_VAL(0x01));
176 /* DDS NCPO PCW (for host mode) */
177 mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_PRD_REG,
178 RG_PE1_H_LCDDS_SSC_PRD,
179 RG_PE1_H_LCDDS_SSC_PRD_VAL(0x18000000));
180 /* DDS SSC dither period control */
181 mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_PRD_REG,
182 RG_PE1_H_LCDDS_SSC_PRD,
183 RG_PE1_H_LCDDS_SSC_PRD_VAL(0x18d));
184 /* DDS SSC dither amplitude control */
185 mt7621_phy_rmw(phy, RG_PE1_H_LCDDS_SSC_DELTA_REG,
186 RG_PE1_H_LCDDS_SSC_DELTA |
187 RG_PE1_H_LCDDS_SSC_DELTA1,
188 RG_PE1_H_LCDDS_SSC_DELTA_VAL(0x4a) |
189 RG_PE1_H_LCDDS_SSC_DELTA1_VAL(0x4a));
190 dev_info(dev, "Xtal is 25MHz\n");
191 } else { /* 20MHz Xtal */
192 mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG,
194 RG_PE1_H_PLL_PREDIV_VAL(0x00));
196 dev_info(dev, "Xtal is 20MHz\n");
199 /* DDS clock inversion */
200 mt7621_phy_rmw(phy, RG_PE1_LCDDS_CLK_PH_INV_REG,
201 RG_PE1_LCDDS_CLK_PH_INV, RG_PE1_LCDDS_CLK_PH_INV);
204 mt7621_phy_rmw(phy, RG_PE1_H_PLL_REG,
205 RG_PE1_H_PLL_BC | RG_PE1_H_PLL_BP | RG_PE1_H_PLL_IR |
206 RG_PE1_H_PLL_IC | RG_PE1_PLL_DIVEN,
207 RG_PE1_H_PLL_BC_VAL(0x02) | RG_PE1_H_PLL_BP_VAL(0x06) |
208 RG_PE1_H_PLL_IR_VAL(0x02) | RG_PE1_H_PLL_IC_VAL(0x01) |
209 RG_PE1_PLL_DIVEN_VAL(0x02));
211 mt7621_phy_rmw(phy, RG_PE1_H_PLL_BR_REG,
212 RG_PE1_H_PLL_BR, RG_PE1_H_PLL_BR_VAL(0x00));
214 if (xtal_mode <= 5 && xtal_mode >= 3) { /* 40MHz Xtal */
215 /* set force mode enable of da_pe1_mstckdiv */
216 mt7621_phy_rmw(phy, RG_PE1_MSTCKDIV_REG,
217 RG_PE1_MSTCKDIV | RG_PE1_FRC_MSTCKDIV,
218 RG_PE1_MSTCKDIV_VAL(0x01) | RG_PE1_FRC_MSTCKDIV);
222 static int mt7621_pci_phy_init(struct phy *phy)
224 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
226 if (mphy->bypass_pipe_rst)
227 mt7621_bypass_pipe_rst(mphy);
229 mt7621_set_phy_for_ssc(mphy);
234 static int mt7621_pci_phy_power_on(struct phy *phy)
236 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
238 /* Enable PHY and disable force mode */
239 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG,
240 RG_PE1_FRC_PHY_EN, RG_PE1_PHY_EN);
242 if (mphy->has_dual_port) {
243 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
244 RG_PE1_FRC_PHY_EN, RG_PE1_PHY_EN);
250 static int mt7621_pci_phy_power_off(struct phy *phy)
252 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
255 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG,
256 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
258 if (mphy->has_dual_port) {
259 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG + RG_P0_TO_P1_WIDTH,
260 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
266 static int mt7621_pci_phy_exit(struct phy *phy)
271 static const struct phy_ops mt7621_pci_phy_ops = {
272 .init = mt7621_pci_phy_init,
273 .exit = mt7621_pci_phy_exit,
274 .power_on = mt7621_pci_phy_power_on,
275 .power_off = mt7621_pci_phy_power_off,
276 .owner = THIS_MODULE,
279 static struct phy *mt7621_pcie_phy_of_xlate(struct device *dev,
280 struct of_phandle_args *args)
282 struct mt7621_pci_phy *mt7621_phy = dev_get_drvdata(dev);
284 if (WARN_ON(args->args[0] >= MAX_PHYS))
285 return ERR_PTR(-ENODEV);
287 mt7621_phy->has_dual_port = args->args[0];
289 dev_info(dev, "PHY for 0x%08x (dual port = %d)\n",
290 (unsigned int)mt7621_phy->port_base, mt7621_phy->has_dual_port);
292 return mt7621_phy->phy;
295 static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
296 { .soc_id = "mt7621", .revision = "E2" }
299 static const struct regmap_config mt7621_pci_phy_regmap_config = {
303 .max_register = 0x700,
306 static int mt7621_pci_phy_probe(struct platform_device *pdev)
308 struct device *dev = &pdev->dev;
309 const struct soc_device_attribute *attr;
310 struct phy_provider *provider;
311 struct mt7621_pci_phy *phy;
312 struct resource *res;
314 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
318 attr = soc_device_match(mt7621_pci_quirks_match);
320 phy->bypass_pipe_rst = true;
323 platform_set_drvdata(pdev, phy);
325 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
327 dev_err(dev, "failed to get address resource\n");
331 phy->port_base = devm_ioremap_resource(dev, res);
332 if (IS_ERR(phy->port_base)) {
333 dev_err(dev, "failed to remap phy regs\n");
334 return PTR_ERR(phy->port_base);
337 phy->regmap = devm_regmap_init_mmio(phy->dev, phy->port_base,
338 &mt7621_pci_phy_regmap_config);
339 if (IS_ERR(phy->regmap))
340 return PTR_ERR(phy->regmap);
342 phy->phy = devm_phy_create(dev, dev->of_node, &mt7621_pci_phy_ops);
344 dev_err(dev, "failed to create phy\n");
348 phy_set_drvdata(phy->phy, phy);
350 provider = devm_of_phy_provider_register(dev, mt7621_pcie_phy_of_xlate);
352 return PTR_ERR_OR_ZERO(provider);
355 static const struct of_device_id mt7621_pci_phy_ids[] = {
356 { .compatible = "mediatek,mt7621-pci-phy" },
359 MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
361 static struct platform_driver mt7621_pci_phy_driver = {
362 .probe = mt7621_pci_phy_probe,
364 .name = "mt7621-pci-phy",
365 .of_match_table = of_match_ptr(mt7621_pci_phy_ids),
369 builtin_platform_driver(mt7621_pci_phy_driver);
371 MODULE_AUTHOR("Sergio Paracuellos <sergio.paracuellos@gmail.com>");
372 MODULE_DESCRIPTION("MediaTek MT7621 PCIe PHY driver");
373 MODULE_LICENSE("GPL v2");