1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Mediatek MT7621 PCI PHY Driver
4 * Author: Sergio Paracuellos <sergio.paracuellos@gmail.com>
5 */
6
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>
16 #include <mt7621.h>
17 #include <ralink_regs.h>
18
19 #define RG_PE1_PIPE_REG 0x02c
20 #define RG_PE1_PIPE_RST BIT(12)
21 #define RG_PE1_PIPE_CMD_FRC BIT(4)
22
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)
27
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)
32
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)
36
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)
50
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)
54
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)
58
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)
64
65 #define RG_PE1_LCDDS_CLK_PH_INV_REG 0x4a0
66 #define RG_PE1_LCDDS_CLK_PH_INV BIT(5)
67
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)
71
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)
75
76 #define RG_PE1_FRC_MSTCKDIV BIT(5)
77
78 #define XTAL_MODE_SEL_SHIFT 6
79 #define XTAL_MODE_SEL_MASK 0x7
80
81 #define MAX_PHYS 2
82
83 /**
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.
92 */
93 struct mt7621_pci_phy {
94 struct device *dev;
95 struct regmap *regmap;
96 struct phy *phy;
97 void __iomem *port_base;
98 bool has_dual_port;
99 bool bypass_pipe_rst;
100 };
101
phy_read(struct mt7621_pci_phy * phy,u32 reg)102 static inline u32 phy_read(struct mt7621_pci_phy *phy, u32 reg)
103 {
104 u32 val;
105
106 regmap_read(phy->regmap, reg, &val);
107
108 return val;
109 }
110
phy_write(struct mt7621_pci_phy * phy,u32 val,u32 reg)111 static inline void phy_write(struct mt7621_pci_phy *phy, u32 val, u32 reg)
112 {
113 regmap_write(phy->regmap, reg, val);
114 }
115
mt7621_phy_rmw(struct mt7621_pci_phy * phy,u32 reg,u32 clr,u32 set)116 static inline void mt7621_phy_rmw(struct mt7621_pci_phy *phy,
117 u32 reg, u32 clr, u32 set)
118 {
119 u32 val = phy_read(phy, reg);
120
121 val &= ~clr;
122 val |= set;
123 phy_write(phy, val, reg);
124 }
125
mt7621_bypass_pipe_rst(struct mt7621_pci_phy * phy)126 static void mt7621_bypass_pipe_rst(struct mt7621_pci_phy *phy)
127 {
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);
130
131 if (phy->has_dual_port) {
132 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG + RG_P0_TO_P1_WIDTH,
133 0, RG_PE1_PIPE_RST);
134 mt7621_phy_rmw(phy, RG_PE1_PIPE_REG + RG_P0_TO_P1_WIDTH,
135 0, RG_PE1_PIPE_CMD_FRC);
136 }
137 }
138
mt7621_set_phy_for_ssc(struct mt7621_pci_phy * phy)139 static void mt7621_set_phy_for_ssc(struct mt7621_pci_phy *phy)
140 {
141 struct device *dev = phy->dev;
142 u32 xtal_mode;
143
144 xtal_mode = (rt_sysc_r32(SYSC_REG_SYSTEM_CONFIG0)
145 >> XTAL_MODE_SEL_SHIFT) & XTAL_MODE_SEL_MASK;
146
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));
152
153 /* disable port */
154 mt7621_phy_rmw(phy, RG_PE1_FRC_PHY_REG,
155 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
156
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);
160 }
161
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,
165 RG_PE1_H_PLL_PREDIV,
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,
170 RG_PE1_H_PLL_PREDIV,
171 RG_PE1_H_PLL_PREDIV_VAL(0x00));
172 /* Select feedback clock */
173 mt7621_phy_rmw(phy, RG_PE1_H_PLL_FBKSEL_REG,
174 RG_PE1_H_PLL_FBKSEL,
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,
193 RG_PE1_H_PLL_PREDIV,
194 RG_PE1_H_PLL_PREDIV_VAL(0x00));
195
196 dev_info(dev, "Xtal is 20MHz\n");
197 }
198
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);
202
203 /* Set PLL bits */
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));
210
211 mt7621_phy_rmw(phy, RG_PE1_H_PLL_BR_REG,
212 RG_PE1_H_PLL_BR, RG_PE1_H_PLL_BR_VAL(0x00));
213
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);
219 }
220 }
221
mt7621_pci_phy_init(struct phy * phy)222 static int mt7621_pci_phy_init(struct phy *phy)
223 {
224 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
225
226 if (mphy->bypass_pipe_rst)
227 mt7621_bypass_pipe_rst(mphy);
228
229 mt7621_set_phy_for_ssc(mphy);
230
231 return 0;
232 }
233
mt7621_pci_phy_power_on(struct phy * phy)234 static int mt7621_pci_phy_power_on(struct phy *phy)
235 {
236 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
237
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);
241
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);
245 }
246
247 return 0;
248 }
249
mt7621_pci_phy_power_off(struct phy * phy)250 static int mt7621_pci_phy_power_off(struct phy *phy)
251 {
252 struct mt7621_pci_phy *mphy = phy_get_drvdata(phy);
253
254 /* Disable PHY */
255 mt7621_phy_rmw(mphy, RG_PE1_FRC_PHY_REG,
256 RG_PE1_PHY_EN, RG_PE1_FRC_PHY_EN);
257
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);
261 }
262
263 return 0;
264 }
265
mt7621_pci_phy_exit(struct phy * phy)266 static int mt7621_pci_phy_exit(struct phy *phy)
267 {
268 return 0;
269 }
270
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,
277 };
278
mt7621_pcie_phy_of_xlate(struct device * dev,struct of_phandle_args * args)279 static struct phy *mt7621_pcie_phy_of_xlate(struct device *dev,
280 struct of_phandle_args *args)
281 {
282 struct mt7621_pci_phy *mt7621_phy = dev_get_drvdata(dev);
283
284 if (WARN_ON(args->args[0] >= MAX_PHYS))
285 return ERR_PTR(-ENODEV);
286
287 mt7621_phy->has_dual_port = args->args[0];
288
289 dev_info(dev, "PHY for 0x%08x (dual port = %d)\n",
290 (unsigned int)mt7621_phy->port_base, mt7621_phy->has_dual_port);
291
292 return mt7621_phy->phy;
293 }
294
295 static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
296 { .soc_id = "mt7621", .revision = "E2" }
297 };
298
299 static const struct regmap_config mt7621_pci_phy_regmap_config = {
300 .reg_bits = 32,
301 .val_bits = 32,
302 .reg_stride = 4,
303 .max_register = 0x700,
304 };
305
mt7621_pci_phy_probe(struct platform_device * pdev)306 static int mt7621_pci_phy_probe(struct platform_device *pdev)
307 {
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;
313
314 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
315 if (!phy)
316 return -ENOMEM;
317
318 attr = soc_device_match(mt7621_pci_quirks_match);
319 if (attr)
320 phy->bypass_pipe_rst = true;
321
322 phy->dev = dev;
323 platform_set_drvdata(pdev, phy);
324
325 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
326 if (!res) {
327 dev_err(dev, "failed to get address resource\n");
328 return -ENXIO;
329 }
330
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);
335 }
336
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);
341
342 phy->phy = devm_phy_create(dev, dev->of_node, &mt7621_pci_phy_ops);
343 if (IS_ERR(phy)) {
344 dev_err(dev, "failed to create phy\n");
345 return PTR_ERR(phy);
346 }
347
348 phy_set_drvdata(phy->phy, phy);
349
350 provider = devm_of_phy_provider_register(dev, mt7621_pcie_phy_of_xlate);
351
352 return PTR_ERR_OR_ZERO(provider);
353 }
354
355 static const struct of_device_id mt7621_pci_phy_ids[] = {
356 { .compatible = "mediatek,mt7621-pci-phy" },
357 {},
358 };
359 MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
360
361 static struct platform_driver mt7621_pci_phy_driver = {
362 .probe = mt7621_pci_phy_probe,
363 .driver = {
364 .name = "mt7621-pci-phy",
365 .of_match_table = of_match_ptr(mt7621_pci_phy_ids),
366 },
367 };
368
369 builtin_platform_driver(mt7621_pci_phy_driver);
370
371 MODULE_AUTHOR("Sergio Paracuellos <sergio.paracuellos@gmail.com>");
372 MODULE_DESCRIPTION("MediaTek MT7621 PCIe PHY driver");
373 MODULE_LICENSE("GPL v2");
374