Skip to content

Commit 7e3c180

Browse files
shenghaoyangPaolo Abeni
authored andcommitted
net: dsa: mv88e6xxx: read cycle counter period from hardware
Instead of relying on a fixed mapping of hardware family to cycle counter frequency, pull this information from the MV88E6XXX_TAI_CLOCK_PERIOD register. This lets us support switches whose cycle counter frequencies depend on board design. Fixes: de776d0 ("net: dsa: mv88e6xxx: add support for mv88e6393x family") Suggested-by: Andrew Lunn <[email protected]> Signed-off-by: Shenghao Yang <[email protected]> Reviewed-by: Andrew Lunn <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 67af86a commit 7e3c180

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

drivers/net/dsa/mv88e6xxx/chip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ struct mv88e6xxx_chip {
409409
struct cyclecounter tstamp_cc;
410410
struct timecounter tstamp_tc;
411411
struct delayed_work overflow_work;
412+
const struct mv88e6xxx_cc_coeffs *cc_coeffs;
412413

413414
struct ptp_clock *ptp_clock;
414415
struct ptp_clock_info ptp_clock_info;
@@ -732,7 +733,6 @@ struct mv88e6xxx_ptp_ops {
732733
int arr1_sts_reg;
733734
int dep_sts_reg;
734735
u32 rx_filters;
735-
const struct mv88e6xxx_cc_coeffs *cc_coeffs;
736736
};
737737

738738
struct mv88e6xxx_pcs_ops {

drivers/net/dsa/mv88e6xxx/ptp.c

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ struct mv88e6xxx_cc_coeffs {
3232
* simplifies to
3333
* clkadj = scaled_ppm * 2^7 / 5^5
3434
*/
35-
#define MV88E6250_CC_SHIFT 28
36-
static const struct mv88e6xxx_cc_coeffs mv88e6250_cc_coeffs = {
37-
.cc_shift = MV88E6250_CC_SHIFT,
38-
.cc_mult = 10 << MV88E6250_CC_SHIFT,
35+
#define MV88E6XXX_CC_10NS_SHIFT 28
36+
static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_10ns_coeffs = {
37+
.cc_shift = MV88E6XXX_CC_10NS_SHIFT,
38+
.cc_mult = 10 << MV88E6XXX_CC_10NS_SHIFT,
3939
.cc_mult_num = 1 << 7,
4040
.cc_mult_dem = 3125ULL,
4141
};
@@ -47,10 +47,10 @@ static const struct mv88e6xxx_cc_coeffs mv88e6250_cc_coeffs = {
4747
* simplifies to
4848
* clkadj = scaled_ppm * 2^9 / 5^6
4949
*/
50-
#define MV88E6XXX_CC_SHIFT 28
51-
static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_coeffs = {
52-
.cc_shift = MV88E6XXX_CC_SHIFT,
53-
.cc_mult = 8 << MV88E6XXX_CC_SHIFT,
50+
#define MV88E6XXX_CC_8NS_SHIFT 28
51+
static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_8ns_coeffs = {
52+
.cc_shift = MV88E6XXX_CC_8NS_SHIFT,
53+
.cc_mult = 8 << MV88E6XXX_CC_8NS_SHIFT,
5454
.cc_mult_num = 1 << 9,
5555
.cc_mult_dem = 15625ULL
5656
};
@@ -96,6 +96,31 @@ static int mv88e6352_set_gpio_func(struct mv88e6xxx_chip *chip, int pin,
9696
return chip->info->ops->gpio_ops->set_pctl(chip, pin, func);
9797
}
9898

99+
static const struct mv88e6xxx_cc_coeffs *
100+
mv88e6xxx_cc_coeff_get(struct mv88e6xxx_chip *chip)
101+
{
102+
u16 period_ps;
103+
int err;
104+
105+
err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_CLOCK_PERIOD, &period_ps, 1);
106+
if (err) {
107+
dev_err(chip->dev, "failed to read cycle counter period: %d\n",
108+
err);
109+
return ERR_PTR(err);
110+
}
111+
112+
switch (period_ps) {
113+
case 8000:
114+
return &mv88e6xxx_cc_8ns_coeffs;
115+
case 10000:
116+
return &mv88e6xxx_cc_10ns_coeffs;
117+
default:
118+
dev_err(chip->dev, "unexpected cycle counter period of %u ps\n",
119+
period_ps);
120+
return ERR_PTR(-ENODEV);
121+
}
122+
}
123+
99124
static u64 mv88e6352_ptp_clock_read(const struct cyclecounter *cc)
100125
{
101126
struct mv88e6xxx_chip *chip = cc_to_chip(cc);
@@ -217,7 +242,6 @@ static void mv88e6352_tai_event_work(struct work_struct *ugly)
217242
static int mv88e6xxx_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
218243
{
219244
struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
220-
const struct mv88e6xxx_ptp_ops *ptp_ops = chip->info->ops->ptp_ops;
221245
int neg_adj = 0;
222246
u32 diff, mult;
223247
u64 adj;
@@ -227,10 +251,10 @@ static int mv88e6xxx_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
227251
scaled_ppm = -scaled_ppm;
228252
}
229253

230-
mult = ptp_ops->cc_coeffs->cc_mult;
231-
adj = ptp_ops->cc_coeffs->cc_mult_num;
254+
mult = chip->cc_coeffs->cc_mult;
255+
adj = chip->cc_coeffs->cc_mult_num;
232256
adj *= scaled_ppm;
233-
diff = div_u64(adj, ptp_ops->cc_coeffs->cc_mult_dem);
257+
diff = div_u64(adj, chip->cc_coeffs->cc_mult_dem);
234258

235259
mv88e6xxx_reg_lock(chip);
236260

@@ -377,7 +401,6 @@ const struct mv88e6xxx_ptp_ops mv88e6165_ptp_ops = {
377401
(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
378402
(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
379403
(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
380-
.cc_coeffs = &mv88e6xxx_cc_coeffs
381404
};
382405

383406
const struct mv88e6xxx_ptp_ops mv88e6250_ptp_ops = {
@@ -401,7 +424,6 @@ const struct mv88e6xxx_ptp_ops mv88e6250_ptp_ops = {
401424
(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
402425
(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
403426
(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
404-
.cc_coeffs = &mv88e6250_cc_coeffs,
405427
};
406428

407429
const struct mv88e6xxx_ptp_ops mv88e6352_ptp_ops = {
@@ -425,7 +447,6 @@ const struct mv88e6xxx_ptp_ops mv88e6352_ptp_ops = {
425447
(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
426448
(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
427449
(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
428-
.cc_coeffs = &mv88e6xxx_cc_coeffs,
429450
};
430451

431452
const struct mv88e6xxx_ptp_ops mv88e6390_ptp_ops = {
@@ -450,7 +471,6 @@ const struct mv88e6xxx_ptp_ops mv88e6390_ptp_ops = {
450471
(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
451472
(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
452473
(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
453-
.cc_coeffs = &mv88e6xxx_cc_coeffs,
454474
};
455475

456476
static u64 mv88e6xxx_ptp_clock_read(const struct cyclecounter *cc)
@@ -485,11 +505,15 @@ int mv88e6xxx_ptp_setup(struct mv88e6xxx_chip *chip)
485505
int i;
486506

487507
/* Set up the cycle counter */
508+
chip->cc_coeffs = mv88e6xxx_cc_coeff_get(chip);
509+
if (IS_ERR(chip->cc_coeffs))
510+
return PTR_ERR(chip->cc_coeffs);
511+
488512
memset(&chip->tstamp_cc, 0, sizeof(chip->tstamp_cc));
489513
chip->tstamp_cc.read = mv88e6xxx_ptp_clock_read;
490514
chip->tstamp_cc.mask = CYCLECOUNTER_MASK(32);
491-
chip->tstamp_cc.mult = ptp_ops->cc_coeffs->cc_mult;
492-
chip->tstamp_cc.shift = ptp_ops->cc_coeffs->cc_shift;
515+
chip->tstamp_cc.mult = chip->cc_coeffs->cc_mult;
516+
chip->tstamp_cc.shift = chip->cc_coeffs->cc_shift;
493517

494518
timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc,
495519
ktime_to_ns(ktime_get_real()));

0 commit comments

Comments
 (0)