|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
| 2 | + |
| 3 | +#include <linux/ptp_classify.h> |
| 4 | + |
| 5 | +#include "lan966x_main.h" |
| 6 | + |
| 7 | +#define LAN966X_MAX_PTP_ID 512 |
| 8 | + |
| 9 | +/* Represents 1ppm adjustment in 2^59 format with 6.037735849ns as reference |
| 10 | + * The value is calculated as following: (1/1000000)/((2^-59)/6.037735849) |
| 11 | + */ |
| 12 | +#define LAN966X_1PPM_FORMAT 3480517749723LL |
| 13 | + |
| 14 | +/* Represents 1ppb adjustment in 2^29 format with 6.037735849ns as reference |
| 15 | + * The value is calculated as following: (1/1000000000)/((2^59)/6.037735849) |
| 16 | + */ |
| 17 | +#define LAN966X_1PPB_FORMAT 3480517749LL |
| 18 | + |
| 19 | +#define TOD_ACC_PIN 0x5 |
| 20 | + |
| 21 | +enum { |
| 22 | + PTP_PIN_ACTION_IDLE = 0, |
| 23 | + PTP_PIN_ACTION_LOAD, |
| 24 | + PTP_PIN_ACTION_SAVE, |
| 25 | + PTP_PIN_ACTION_CLOCK, |
| 26 | + PTP_PIN_ACTION_DELTA, |
| 27 | + PTP_PIN_ACTION_TOD |
| 28 | +}; |
| 29 | + |
| 30 | +static u64 lan966x_ptp_get_nominal_value(void) |
| 31 | +{ |
| 32 | + u64 res = 0x304d2df1; |
| 33 | + |
| 34 | + res <<= 32; |
| 35 | + return res; |
| 36 | +} |
| 37 | + |
| 38 | +static int lan966x_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) |
| 39 | +{ |
| 40 | + struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info); |
| 41 | + struct lan966x *lan966x = phc->lan966x; |
| 42 | + unsigned long flags; |
| 43 | + bool neg_adj = 0; |
| 44 | + u64 tod_inc; |
| 45 | + u64 ref; |
| 46 | + |
| 47 | + if (!scaled_ppm) |
| 48 | + return 0; |
| 49 | + |
| 50 | + if (scaled_ppm < 0) { |
| 51 | + neg_adj = 1; |
| 52 | + scaled_ppm = -scaled_ppm; |
| 53 | + } |
| 54 | + |
| 55 | + tod_inc = lan966x_ptp_get_nominal_value(); |
| 56 | + |
| 57 | + /* The multiplication is split in 2 separate additions because of |
| 58 | + * overflow issues. If scaled_ppm with 16bit fractional part was bigger |
| 59 | + * than 20ppm then we got overflow. |
| 60 | + */ |
| 61 | + ref = LAN966X_1PPM_FORMAT * (scaled_ppm >> 16); |
| 62 | + ref += (LAN966X_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16; |
| 63 | + tod_inc = neg_adj ? tod_inc - ref : tod_inc + ref; |
| 64 | + |
| 65 | + spin_lock_irqsave(&lan966x->ptp_clock_lock, flags); |
| 66 | + |
| 67 | + lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(1 << BIT(phc->index)), |
| 68 | + PTP_DOM_CFG_CLKCFG_DIS, |
| 69 | + lan966x, PTP_DOM_CFG); |
| 70 | + |
| 71 | + lan_wr((u32)tod_inc & 0xFFFFFFFF, lan966x, |
| 72 | + PTP_CLK_PER_CFG(phc->index, 0)); |
| 73 | + lan_wr((u32)(tod_inc >> 32), lan966x, |
| 74 | + PTP_CLK_PER_CFG(phc->index, 1)); |
| 75 | + |
| 76 | + lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0), |
| 77 | + PTP_DOM_CFG_CLKCFG_DIS, |
| 78 | + lan966x, PTP_DOM_CFG); |
| 79 | + |
| 80 | + spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static int lan966x_ptp_settime64(struct ptp_clock_info *ptp, |
| 86 | + const struct timespec64 *ts) |
| 87 | +{ |
| 88 | + struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info); |
| 89 | + struct lan966x *lan966x = phc->lan966x; |
| 90 | + unsigned long flags; |
| 91 | + |
| 92 | + spin_lock_irqsave(&lan966x->ptp_clock_lock, flags); |
| 93 | + |
| 94 | + /* Must be in IDLE mode before the time can be loaded */ |
| 95 | + lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) | |
| 96 | + PTP_PIN_CFG_PIN_DOM_SET(phc->index) | |
| 97 | + PTP_PIN_CFG_PIN_SYNC_SET(0), |
| 98 | + PTP_PIN_CFG_PIN_ACTION | |
| 99 | + PTP_PIN_CFG_PIN_DOM | |
| 100 | + PTP_PIN_CFG_PIN_SYNC, |
| 101 | + lan966x, PTP_PIN_CFG(TOD_ACC_PIN)); |
| 102 | + |
| 103 | + /* Set new value */ |
| 104 | + lan_wr(PTP_TOD_SEC_MSB_TOD_SEC_MSB_SET(upper_32_bits(ts->tv_sec)), |
| 105 | + lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN)); |
| 106 | + lan_wr(lower_32_bits(ts->tv_sec), |
| 107 | + lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN)); |
| 108 | + lan_wr(ts->tv_nsec, lan966x, PTP_TOD_NSEC(TOD_ACC_PIN)); |
| 109 | + |
| 110 | + /* Apply new values */ |
| 111 | + lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_LOAD) | |
| 112 | + PTP_PIN_CFG_PIN_DOM_SET(phc->index) | |
| 113 | + PTP_PIN_CFG_PIN_SYNC_SET(0), |
| 114 | + PTP_PIN_CFG_PIN_ACTION | |
| 115 | + PTP_PIN_CFG_PIN_DOM | |
| 116 | + PTP_PIN_CFG_PIN_SYNC, |
| 117 | + lan966x, PTP_PIN_CFG(TOD_ACC_PIN)); |
| 118 | + |
| 119 | + spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags); |
| 120 | + |
| 121 | + return 0; |
| 122 | +} |
| 123 | + |
| 124 | +static int lan966x_ptp_gettime64(struct ptp_clock_info *ptp, |
| 125 | + struct timespec64 *ts) |
| 126 | +{ |
| 127 | + struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info); |
| 128 | + struct lan966x *lan966x = phc->lan966x; |
| 129 | + unsigned long flags; |
| 130 | + time64_t s; |
| 131 | + s64 ns; |
| 132 | + |
| 133 | + spin_lock_irqsave(&lan966x->ptp_clock_lock, flags); |
| 134 | + |
| 135 | + lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) | |
| 136 | + PTP_PIN_CFG_PIN_DOM_SET(phc->index) | |
| 137 | + PTP_PIN_CFG_PIN_SYNC_SET(0), |
| 138 | + PTP_PIN_CFG_PIN_ACTION | |
| 139 | + PTP_PIN_CFG_PIN_DOM | |
| 140 | + PTP_PIN_CFG_PIN_SYNC, |
| 141 | + lan966x, PTP_PIN_CFG(TOD_ACC_PIN)); |
| 142 | + |
| 143 | + s = lan_rd(lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN)); |
| 144 | + s <<= 32; |
| 145 | + s |= lan_rd(lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN)); |
| 146 | + ns = lan_rd(lan966x, PTP_TOD_NSEC(TOD_ACC_PIN)); |
| 147 | + ns &= PTP_TOD_NSEC_TOD_NSEC; |
| 148 | + |
| 149 | + spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags); |
| 150 | + |
| 151 | + /* Deal with negative values */ |
| 152 | + if ((ns & 0xFFFFFFF0) == 0x3FFFFFF0) { |
| 153 | + s--; |
| 154 | + ns &= 0xf; |
| 155 | + ns += 999999984; |
| 156 | + } |
| 157 | + |
| 158 | + set_normalized_timespec64(ts, s, ns); |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
| 162 | +static int lan966x_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) |
| 163 | +{ |
| 164 | + struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info); |
| 165 | + struct lan966x *lan966x = phc->lan966x; |
| 166 | + |
| 167 | + if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) { |
| 168 | + unsigned long flags; |
| 169 | + |
| 170 | + spin_lock_irqsave(&lan966x->ptp_clock_lock, flags); |
| 171 | + |
| 172 | + /* Must be in IDLE mode before the time can be loaded */ |
| 173 | + lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) | |
| 174 | + PTP_PIN_CFG_PIN_DOM_SET(phc->index) | |
| 175 | + PTP_PIN_CFG_PIN_SYNC_SET(0), |
| 176 | + PTP_PIN_CFG_PIN_ACTION | |
| 177 | + PTP_PIN_CFG_PIN_DOM | |
| 178 | + PTP_PIN_CFG_PIN_SYNC, |
| 179 | + lan966x, PTP_PIN_CFG(TOD_ACC_PIN)); |
| 180 | + |
| 181 | + lan_wr(PTP_TOD_NSEC_TOD_NSEC_SET(delta), |
| 182 | + lan966x, PTP_TOD_NSEC(TOD_ACC_PIN)); |
| 183 | + |
| 184 | + /* Adjust time with the value of PTP_TOD_NSEC */ |
| 185 | + lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_DELTA) | |
| 186 | + PTP_PIN_CFG_PIN_DOM_SET(phc->index) | |
| 187 | + PTP_PIN_CFG_PIN_SYNC_SET(0), |
| 188 | + PTP_PIN_CFG_PIN_ACTION | |
| 189 | + PTP_PIN_CFG_PIN_DOM | |
| 190 | + PTP_PIN_CFG_PIN_SYNC, |
| 191 | + lan966x, PTP_PIN_CFG(TOD_ACC_PIN)); |
| 192 | + |
| 193 | + spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags); |
| 194 | + } else { |
| 195 | + /* Fall back using lan966x_ptp_settime64 which is not exact */ |
| 196 | + struct timespec64 ts; |
| 197 | + u64 now; |
| 198 | + |
| 199 | + lan966x_ptp_gettime64(ptp, &ts); |
| 200 | + |
| 201 | + now = ktime_to_ns(timespec64_to_ktime(ts)); |
| 202 | + ts = ns_to_timespec64(now + delta); |
| 203 | + |
| 204 | + lan966x_ptp_settime64(ptp, &ts); |
| 205 | + } |
| 206 | + |
| 207 | + return 0; |
| 208 | +} |
| 209 | + |
| 210 | +static struct ptp_clock_info lan966x_ptp_clock_info = { |
| 211 | + .owner = THIS_MODULE, |
| 212 | + .name = "lan966x ptp", |
| 213 | + .max_adj = 200000, |
| 214 | + .gettime64 = lan966x_ptp_gettime64, |
| 215 | + .settime64 = lan966x_ptp_settime64, |
| 216 | + .adjtime = lan966x_ptp_adjtime, |
| 217 | + .adjfine = lan966x_ptp_adjfine, |
| 218 | +}; |
| 219 | + |
| 220 | +static int lan966x_ptp_phc_init(struct lan966x *lan966x, |
| 221 | + int index, |
| 222 | + struct ptp_clock_info *clock_info) |
| 223 | +{ |
| 224 | + struct lan966x_phc *phc = &lan966x->phc[index]; |
| 225 | + |
| 226 | + phc->info = *clock_info; |
| 227 | + phc->clock = ptp_clock_register(&phc->info, lan966x->dev); |
| 228 | + if (IS_ERR(phc->clock)) |
| 229 | + return PTR_ERR(phc->clock); |
| 230 | + |
| 231 | + phc->index = index; |
| 232 | + phc->lan966x = lan966x; |
| 233 | + |
| 234 | + /* PTP Rx stamping is always enabled. */ |
| 235 | + phc->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; |
| 236 | + |
| 237 | + return 0; |
| 238 | +} |
| 239 | + |
| 240 | +int lan966x_ptp_init(struct lan966x *lan966x) |
| 241 | +{ |
| 242 | + u64 tod_adj = lan966x_ptp_get_nominal_value(); |
| 243 | + int err, i; |
| 244 | + |
| 245 | + if (!lan966x->ptp) |
| 246 | + return 0; |
| 247 | + |
| 248 | + for (i = 0; i < LAN966X_PHC_COUNT; ++i) { |
| 249 | + err = lan966x_ptp_phc_init(lan966x, i, &lan966x_ptp_clock_info); |
| 250 | + if (err) |
| 251 | + return err; |
| 252 | + } |
| 253 | + |
| 254 | + spin_lock_init(&lan966x->ptp_clock_lock); |
| 255 | + |
| 256 | + /* Disable master counters */ |
| 257 | + lan_wr(PTP_DOM_CFG_ENA_SET(0), lan966x, PTP_DOM_CFG); |
| 258 | + |
| 259 | + /* Configure the nominal TOD increment per clock cycle */ |
| 260 | + lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0x7), |
| 261 | + PTP_DOM_CFG_CLKCFG_DIS, |
| 262 | + lan966x, PTP_DOM_CFG); |
| 263 | + |
| 264 | + for (i = 0; i < LAN966X_PHC_COUNT; ++i) { |
| 265 | + lan_wr((u32)tod_adj & 0xFFFFFFFF, lan966x, |
| 266 | + PTP_CLK_PER_CFG(i, 0)); |
| 267 | + lan_wr((u32)(tod_adj >> 32), lan966x, |
| 268 | + PTP_CLK_PER_CFG(i, 1)); |
| 269 | + } |
| 270 | + |
| 271 | + lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0), |
| 272 | + PTP_DOM_CFG_CLKCFG_DIS, |
| 273 | + lan966x, PTP_DOM_CFG); |
| 274 | + |
| 275 | + /* Enable master counters */ |
| 276 | + lan_wr(PTP_DOM_CFG_ENA_SET(0x7), lan966x, PTP_DOM_CFG); |
| 277 | + |
| 278 | + return 0; |
| 279 | +} |
| 280 | + |
| 281 | +void lan966x_ptp_deinit(struct lan966x *lan966x) |
| 282 | +{ |
| 283 | + int i; |
| 284 | + |
| 285 | + for (i = 0; i < LAN966X_PHC_COUNT; ++i) |
| 286 | + ptp_clock_unregister(lan966x->phc[i].clock); |
| 287 | +} |
0 commit comments