1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// NVIDIA Tegra264 MPHY driver.
#include <dt-bindings/phy/nvidia,tegra264-mphy.h>
#include <linux/clk.h>
#include <linux/iopoll.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/phy/phy.h>
#include <linux/platform_device.h>
#include <linux/reset.h>
#define MPHY_GO_BIT BIT(0)
#define MPHY_RX_APB_CAPABILITY_88_8B 0x88
#define RX_HS_G1_SYNC_LENGTH_CAPABILITY(x) (((x) & 0x3f) << 24)
#define MPHY_RX_APB_CAPABILITY_94_97 0x94
#define RX_HS_G3_SYNC_LENGTH_CAPABILITY(x) (((x) & 0x3f) << 8)
#define RX_HS_G2_SYNC_LENGTH_CAPABILITY(x) (((x) & 0x3f) << 0)
#define MPHY_TX_APB_VENDOR0 0x100
#define MPHY_TX_APB_VENDOR2 0x108
#define TX_CAL_DONE BIT(19)
#define TX_CAL_EN BIT(15)
#define MPHY_RX_APB_VENDOR2 0x184
#define RX_CAL_DONE BIT(19)
#define RX_CAL_EN BIT(15)
#define MPHY_RX_APB_VENDOR3 0x188
#define RX_MPHY2UPHY_IF_OVR_CTRL BIT(26)
#define MPHY_RX_APB_VENDOR3B 0x220
#define MPHY_RX_APB_VENDOR49 0x254
#define MPHY_EQ_TIMEOUT 0xffffffff
#define MPHY_PWR_CHANGE_CLK_BOOST 0x0017
#define MPHY_TX_OFFSET 0x1000
#define MPHY_RX_OFFSET 0x2000
struct tegra_mphy_lane {
void __iomem *regs;
void __iomem *tx_regs;
void __iomem *rx_regs;
struct reset_control *rst_rx;
struct reset_control *rst_tx;
struct phy *tx;
struct phy *rx;
};
struct tegra_mphy {
struct device *dev;
struct tegra_mphy_lane l0;
struct tegra_mphy_lane l1;
unsigned int num_clks;
struct clk_bulk_data *clks;
struct reset_control *rst_clk_ctl;
unsigned int power_count;
};
static int tegra_mphy_get_clocks(struct tegra_mphy *mphy)
{
struct device *dev = mphy->dev;
int num_clks;
num_clks = devm_clk_bulk_get_all(dev, &mphy->clks);
if (num_clks < 0)
return dev_err_probe(dev, num_clks, "failed to get clocks\n");
mphy->num_clks = num_clks;
return 0;
}
static int tegra_mphy_get_resets(struct tegra_mphy *mphy)
{
struct device *dev = mphy->dev;
mphy->rst_clk_ctl = devm_reset_control_get_exclusive(dev, "clk-ctl");
if (IS_ERR(mphy->rst_clk_ctl))
return dev_err_probe(dev, PTR_ERR(mphy->rst_clk_ctl),
"failed to get clk-ctl reset\n");
mphy->l0.rst_rx = devm_reset_control_get_exclusive(dev, "l0-rx");
if (IS_ERR(mphy->l0.rst_rx))
return dev_err_probe(dev, PTR_ERR(mphy->l0.rst_rx), "failed to get l0-rx reset\n");
mphy->l0.rst_tx = devm_reset_control_get_exclusive(dev, "l0-tx");
if (IS_ERR(mphy->l0.rst_tx))
return dev_err_probe(dev, PTR_ERR(mphy->l0.rst_tx), "failed to get l0-tx reset\n");
mphy->l1.rst_rx = devm_reset_control_get_exclusive(dev, "l1-rx");
if (IS_ERR(mphy->l1.rst_rx))
return dev_err_probe(dev, PTR_ERR(mphy->l1.rst_rx), "failed to get l1-rx reset\n");
mphy->l1.rst_tx = devm_reset_control_get_exclusive(dev, "l1-tx");
if (IS_ERR(mphy->l1.rst_tx))
return dev_err_probe(dev, PTR_ERR(mphy->l1.rst_tx), "failed to get l1-tx reset\n");
return 0;
}
static int tegra_mphy_rx_write_kick_go(struct tegra_mphy_lane *lane, u32 offset, u32 value)
{
u32 v;
writel(value, lane->rx_regs + offset);
v = readl(lane->rx_regs + MPHY_RX_APB_VENDOR2);
v |= MPHY_GO_BIT;
writel(v, lane->rx_regs + MPHY_RX_APB_VENDOR2);
return readl_poll_timeout(lane->rx_regs + MPHY_RX_APB_VENDOR2, v,
(v & MPHY_GO_BIT) == 0, 25, 5000);
}
static int tegra_mphy_rx_power_on(struct phy *phy)
{
struct tegra_mphy *mphy = dev_get_drvdata(phy->dev.parent);
struct tegra_mphy_lane *lane = phy_get_drvdata(phy);
u32 value;
int err;
if (mphy->power_count++ == 0) {
err = clk_bulk_prepare_enable(mphy->num_clks, mphy->clks);
if (err) {
mphy->power_count--;
dev_err(&phy->dev, "failed to enable clocks: %d\n", err);
return err;
}
reset_control_deassert(mphy->rst_clk_ctl);
}
reset_control_deassert(lane->rst_rx);
reset_control_deassert(lane->rst_tx);
err = tegra_mphy_rx_write_kick_go(lane, MPHY_RX_APB_VENDOR3B, MPHY_EQ_TIMEOUT);
if (err) {
dev_err(&phy->dev, "eq_timeout programming failed: %d\n", err);
goto err_reset;
}
err = tegra_mphy_rx_write_kick_go(lane, MPHY_RX_APB_VENDOR49, MPHY_PWR_CHANGE_CLK_BOOST);
if (err) {
dev_err(&phy->dev, "pwr_change_clk_boost programming failed: %d\n", err);
goto err_reset;
}
value = readl(lane->regs + MPHY_RX_APB_CAPABILITY_88_8B);
value &= ~RX_HS_G1_SYNC_LENGTH_CAPABILITY(~0);
value |= RX_HS_G1_SYNC_LENGTH_CAPABILITY(0xf);
writel(value, lane->regs + MPHY_RX_APB_CAPABILITY_88_8B);
value = readl(lane->regs + MPHY_RX_APB_CAPABILITY_94_97);
value &= ~RX_HS_G3_SYNC_LENGTH_CAPABILITY(~0);
value |= RX_HS_G3_SYNC_LENGTH_CAPABILITY(0xf);
value &= ~RX_HS_G2_SYNC_LENGTH_CAPABILITY(~0);
value |= RX_HS_G2_SYNC_LENGTH_CAPABILITY(0xf);
writel(value, lane->regs + MPHY_RX_APB_CAPABILITY_94_97);
value = readl(lane->rx_regs + MPHY_RX_APB_VENDOR3);
value |= RX_MPHY2UPHY_IF_OVR_CTRL;
writel(value, lane->rx_regs + MPHY_RX_APB_VENDOR3);
value = readl(lane->rx_regs + MPHY_RX_APB_VENDOR2);
value |= MPHY_GO_BIT;
writel(value, lane->rx_regs + MPHY_RX_APB_VENDOR2);
err = readl_poll_timeout(lane->rx_regs + MPHY_RX_APB_VENDOR2, value,
(value & MPHY_GO_BIT) == 0, 25, 5000);
if (err) {
dev_err(&phy->dev, "RX cap update failed: %d\n", err);
goto err_reset;
}
return 0;
err_reset:
reset_control_assert(lane->rst_rx);
reset_control_assert(lane->rst_tx);
if (--mphy->power_count == 0) {
reset_control_assert(mphy->rst_clk_ctl);
clk_bulk_disable_unprepare(mphy->num_clks, mphy->clks);
}
return err;
}
static int tegra_mphy_rx_power_off(struct phy *phy)
{
struct tegra_mphy *mphy = dev_get_drvdata(phy->dev.parent);
struct tegra_mphy_lane *lane = phy_get_drvdata(phy);
if (WARN_ON(mphy->power_count == 0))
return -EINVAL;
reset_control_assert(lane->rst_rx);
reset_control_assert(lane->rst_tx);
if (--mphy->power_count == 0)
clk_bulk_disable_unprepare(mphy->num_clks, mphy->clks);
return 0;
}
static int tegra_mphy_rx_configure(struct phy *phy, union phy_configure_opts *opts)
{
struct tegra_mphy_lane *lane = phy_get_drvdata(phy);
u32 value;
int err;
value = readl(lane->rx_regs + MPHY_RX_APB_VENDOR2);
value |= RX_CAL_EN;
writel(value, lane->rx_regs + MPHY_RX_APB_VENDOR2);
value = readl(lane->rx_regs + MPHY_RX_APB_VENDOR2);
value |= MPHY_GO_BIT;
writel(value, lane->rx_regs + MPHY_RX_APB_VENDOR2);
err = readl_poll_timeout(lane->rx_regs + MPHY_RX_APB_VENDOR2, value,
(value & MPHY_GO_BIT) == 0, 25, 5000);
if (err)
dev_err(&phy->dev, "failed to arm RX calibration: %d\n", err);
return err;
}
static int tegra_mphy_rx_calibrate(struct phy *phy)
{
struct tegra_mphy_lane *lane = phy_get_drvdata(phy);
u32 value;
int err;
err = readl_poll_timeout(lane->rx_regs + MPHY_RX_APB_VENDOR2, value,
(value & RX_CAL_DONE) != 0, 25, 100000);
if (err) {
dev_err(&phy->dev, "RX calibration timed out: %d\n", err);
return err;
}
value = readl(lane->rx_regs + MPHY_RX_APB_VENDOR2);
value &= ~RX_CAL_EN;
writel(value, lane->rx_regs + MPHY_RX_APB_VENDOR2);
value = readl(lane->rx_regs + MPHY_RX_APB_VENDOR2);
value |= MPHY_GO_BIT;
writel(value, lane->rx_regs + MPHY_RX_APB_VENDOR2);
err = readl_poll_timeout(lane->rx_regs + MPHY_RX_APB_VENDOR2, value,
(value & MPHY_GO_BIT) == 0, 25, 5000);
if (err) {
dev_err(&phy->dev, "failed to clear RX calibration: %d\n", err);
return err;
}
err = readl_poll_timeout(lane->rx_regs + MPHY_RX_APB_VENDOR2, value,
(value & RX_CAL_DONE) == 0, 25, 100000);
if (err)
dev_err(&phy->dev, "RX calibration failed to clear: %d\n", err);
return err;
}
static const struct phy_ops tegra_mphy_rx_ops = {
.power_on = tegra_mphy_rx_power_on,
.power_off = tegra_mphy_rx_power_off,
.configure = tegra_mphy_rx_configure,
.calibrate = tegra_mphy_rx_calibrate,
};
static int tegra_mphy_tx_configure(struct phy *phy, union phy_configure_opts *opts)
{
struct tegra_mphy_lane *lane = phy_get_drvdata(phy);
u32 value;
int err;
value = readl(lane->tx_regs + MPHY_TX_APB_VENDOR2);
value |= TX_CAL_EN;
writel(value, lane->tx_regs + MPHY_TX_APB_VENDOR2);
value = readl(lane->tx_regs + MPHY_TX_APB_VENDOR0);
value |= MPHY_GO_BIT;
writel(value, lane->tx_regs + MPHY_TX_APB_VENDOR0);
err = readl_poll_timeout(lane->tx_regs + MPHY_TX_APB_VENDOR0, value,
(value & MPHY_GO_BIT) == 0, 25, 5000);
if (err)
dev_err(&phy->dev, "failed to arm TX calibration: %d\n", err);
return err;
}
static int tegra_mphy_tx_calibrate(struct phy *phy)
{
struct tegra_mphy_lane *lane = phy_get_drvdata(phy);
u32 value;
int err;
err = readl_poll_timeout(lane->tx_regs + MPHY_TX_APB_VENDOR2, value,
(value & TX_CAL_DONE) != 0, 25, 50000);
if (err) {
dev_err(&phy->dev, "TX calibration timed out: %d\n", err);
return err;
}
value = readl(lane->tx_regs + MPHY_TX_APB_VENDOR2);
value &= ~TX_CAL_EN;
writel(value, lane->tx_regs + MPHY_TX_APB_VENDOR2);
value = readl(lane->tx_regs + MPHY_TX_APB_VENDOR0);
value |= MPHY_GO_BIT;
writel(value, lane->tx_regs + MPHY_TX_APB_VENDOR0);
err = readl_poll_timeout(lane->tx_regs + MPHY_TX_APB_VENDOR0, value,
(value & MPHY_GO_BIT) == 0, 25, 5000);
if (err) {
dev_err(&phy->dev, "GO bit failed to clear: %d\n", err);
return err;
}
err = readl_poll_timeout(lane->tx_regs + MPHY_TX_APB_VENDOR2, value,
(value & TX_CAL_DONE) == 0, 25, 50000);
if (err)
dev_err(&phy->dev, "TX calibration failed: %d\n", err);
return err;
}
static const struct phy_ops tegra_mphy_tx_ops = {
.configure = tegra_mphy_tx_configure,
.calibrate = tegra_mphy_tx_calibrate,
};
static struct phy *tegra_mphy_xlate(struct device *dev, const struct of_phandle_args *args)
{
struct tegra_mphy *mphy = dev_get_drvdata(dev);
if (args->args_count != 1)
return ERR_PTR(-EINVAL);
switch (args->args[0]) {
case TEGRA_MPHY_L0_TX:
return mphy->l0.tx;
case TEGRA_MPHY_L0_RX:
return mphy->l0.rx;
case TEGRA_MPHY_L1_TX:
return mphy->l1.tx;
case TEGRA_MPHY_L1_RX:
return mphy->l1.rx;
default:
return ERR_PTR(-EINVAL);
}
}
static int tegra_mphy_create_phys(struct tegra_mphy *mphy)
{
struct device *dev = mphy->dev;
mphy->l0.tx = devm_phy_create(dev, dev->of_node, &tegra_mphy_tx_ops);
if (IS_ERR(mphy->l0.tx))
return dev_err_probe(dev, PTR_ERR(mphy->l0.tx), "failed to create l0-tx PHY\n");
phy_set_drvdata(mphy->l0.tx, &mphy->l0);
mphy->l0.rx = devm_phy_create(dev, dev->of_node, &tegra_mphy_rx_ops);
if (IS_ERR(mphy->l0.rx))
return dev_err_probe(dev, PTR_ERR(mphy->l0.rx), "failed to create l0-rx PHY\n");
phy_set_drvdata(mphy->l0.rx, &mphy->l0);
mphy->l1.tx = devm_phy_create(dev, dev->of_node, &tegra_mphy_tx_ops);
if (IS_ERR(mphy->l1.tx))
return dev_err_probe(dev, PTR_ERR(mphy->l1.tx), "failed to create l1-tx PHY\n");
phy_set_drvdata(mphy->l1.tx, &mphy->l1);
mphy->l1.rx = devm_phy_create(dev, dev->of_node, &tegra_mphy_rx_ops);
if (IS_ERR(mphy->l1.rx))
return dev_err_probe(dev, PTR_ERR(mphy->l1.rx), "failed to create l1-rx PHY\n");
phy_set_drvdata(mphy->l1.rx, &mphy->l1);
return 0;
}
static int tegra_mphy_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct phy_provider *provider;
struct tegra_mphy *mphy;
int err;
mphy = devm_kzalloc(dev, sizeof(*mphy), GFP_KERNEL);
if (!mphy)
return -ENOMEM;
mphy->dev = dev;
mphy->l0.regs = devm_platform_ioremap_resource_byname(pdev, "l0");
if (IS_ERR(mphy->l0.regs))
return PTR_ERR(mphy->l0.regs);
mphy->l1.regs = devm_platform_ioremap_resource_byname(pdev, "l1");
if (IS_ERR(mphy->l1.regs))
return PTR_ERR(mphy->l1.regs);
mphy->l0.tx_regs = mphy->l0.regs + MPHY_TX_OFFSET;
mphy->l0.rx_regs = mphy->l0.regs + MPHY_RX_OFFSET;
mphy->l1.tx_regs = mphy->l1.regs + MPHY_TX_OFFSET;
mphy->l1.rx_regs = mphy->l1.regs + MPHY_RX_OFFSET;
err = tegra_mphy_get_clocks(mphy);
if (err)
return err;
err = tegra_mphy_get_resets(mphy);
if (err)
return err;
err = tegra_mphy_create_phys(mphy);
if (err)
return err;
platform_set_drvdata(pdev, mphy);
provider = devm_of_phy_provider_register(dev, tegra_mphy_xlate);
if (IS_ERR(provider))
return dev_err_probe(dev, PTR_ERR(provider),
"failed to register PHY\n");
return 0;
}
static const struct of_device_id tegra_mphy_of_table[] = {
{ .compatible = "nvidia,tegra264-mphy" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, tegra_mphy_of_table);
static struct platform_driver tegra_mphy_driver = {
.driver = {
.name = "tegra-mphy",
.of_match_table = tegra_mphy_of_table,
},
.probe = tegra_mphy_probe,
};
module_platform_driver(tegra_mphy_driver);
MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
MODULE_AUTHOR("Kartik Rajput <kkartik@nvidia.com>");
MODULE_DESCRIPTION("NVIDIA Tegra MPHY driver");
MODULE_LICENSE("GPL");
|