1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot PTP clock driver
4 * Copyright (c) 2017 Microsemi Corporation
7 #include <linux/time64.h>
9 #include <soc/mscc/ocelot_ptp.h>
10 #include <soc/mscc/ocelot_sys.h>
11 #include <soc/mscc/ocelot.h>
13 int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
15 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
21 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
23 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
24 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
25 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
26 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
28 s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
30 s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
31 ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
33 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
35 /* Deal with negative values */
36 if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
42 set_normalized_timespec64(ts, s, ns);
45 EXPORT_SYMBOL(ocelot_ptp_gettime64);
47 int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
48 const struct timespec64 *ts)
50 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
54 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
56 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
57 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
58 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
60 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
62 ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
64 ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
66 ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
68 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
69 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
70 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
72 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
74 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
77 EXPORT_SYMBOL(ocelot_ptp_settime64);
79 int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
81 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
82 struct ocelot *ocelot = container_of(ptp, struct ocelot,
87 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
89 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
90 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
92 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
94 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
96 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
97 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
98 ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
100 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
101 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
103 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
105 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
107 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
109 /* Fall back using ocelot_ptp_settime64 which is not exact. */
110 struct timespec64 ts;
113 ocelot_ptp_gettime64(ptp, &ts);
115 now = ktime_to_ns(timespec64_to_ktime(ts));
116 ts = ns_to_timespec64(now + delta);
118 ocelot_ptp_settime64(ptp, &ts);
122 EXPORT_SYMBOL(ocelot_ptp_adjtime);
124 int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
126 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
127 u32 unit = 0, direction = 0;
131 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
136 if (scaled_ppm < 0) {
137 direction = PTP_CFG_CLK_ADJ_CFG_DIR;
138 scaled_ppm = -scaled_ppm;
141 adj = PSEC_PER_SEC << 16;
142 do_div(adj, scaled_ppm);
145 /* If the adjustment value is too large, use ns instead */
146 if (adj >= (1L << 30)) {
147 unit = PTP_CFG_CLK_ADJ_FREQ_NS;
152 if (adj >= (1L << 30))
155 ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
156 ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
157 PTP_CLK_CFG_ADJ_CFG);
159 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
163 ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
165 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
168 EXPORT_SYMBOL(ocelot_ptp_adjfine);
170 int ocelot_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
171 enum ptp_pin_function func, unsigned int chan)
183 EXPORT_SYMBOL(ocelot_ptp_verify);
185 int ocelot_ptp_enable(struct ptp_clock_info *ptp,
186 struct ptp_clock_request *rq, int on)
188 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
189 struct timespec64 ts_phase, ts_period;
190 enum ocelot_ptp_pins ptp_pin;
199 case PTP_CLK_REQ_PEROUT:
200 /* Reject requests with unsupported flags */
201 if (rq->perout.flags & ~(PTP_PEROUT_DUTY_CYCLE |
205 pin = ptp_find_pin(ocelot->ptp_clock, PTP_PF_PEROUT,
218 ts_period.tv_sec = rq->perout.period.sec;
219 ts_period.tv_nsec = rq->perout.period.nsec;
221 if (ts_period.tv_sec == 1 && ts_period.tv_nsec == 0)
224 /* Handle turning off */
226 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
227 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
228 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
229 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
233 if (rq->perout.flags & PTP_PEROUT_PHASE) {
234 ts_phase.tv_sec = rq->perout.phase.sec;
235 ts_phase.tv_nsec = rq->perout.phase.nsec;
238 ts_phase.tv_sec = rq->perout.start.sec;
239 ts_phase.tv_nsec = rq->perout.start.nsec;
241 if (ts_phase.tv_sec || (ts_phase.tv_nsec && !pps)) {
242 dev_warn(ocelot->dev,
243 "Absolute start time not supported!\n");
244 dev_warn(ocelot->dev,
245 "Accept nsec for PPS phase adjustment, otherwise start time should be 0 0.\n");
249 /* Calculate waveform high and low times */
250 if (rq->perout.flags & PTP_PEROUT_DUTY_CYCLE) {
251 struct timespec64 ts_on;
253 ts_on.tv_sec = rq->perout.on.sec;
254 ts_on.tv_nsec = rq->perout.on.nsec;
256 wf_high = timespec64_to_ns(&ts_on);
261 wf_high = timespec64_to_ns(&ts_period);
262 wf_high = div_s64(wf_high, 2);
266 wf_low = timespec64_to_ns(&ts_period);
269 /* Handle PPS request */
271 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
272 ocelot_write_rix(ocelot, ts_phase.tv_nsec,
273 PTP_PIN_WF_LOW_PERIOD, ptp_pin);
274 ocelot_write_rix(ocelot, wf_high,
275 PTP_PIN_WF_HIGH_PERIOD, ptp_pin);
276 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_CLOCK);
277 val |= PTP_PIN_CFG_SYNC;
278 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
279 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
283 /* Handle periodic clock */
284 if (wf_high > 0x3fffffff || wf_high <= 0x6)
286 if (wf_low > 0x3fffffff || wf_low <= 0x6)
289 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
290 ocelot_write_rix(ocelot, wf_low, PTP_PIN_WF_LOW_PERIOD,
292 ocelot_write_rix(ocelot, wf_high, PTP_PIN_WF_HIGH_PERIOD,
294 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_CLOCK);
295 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
296 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
303 EXPORT_SYMBOL(ocelot_ptp_enable);
305 int ocelot_init_timestamp(struct ocelot *ocelot,
306 const struct ptp_clock_info *info)
308 struct ptp_clock *ptp_clock;
311 ocelot->ptp_info = *info;
313 for (i = 0; i < OCELOT_PTP_PINS_NUM; i++) {
314 struct ptp_pin_desc *p = &ocelot->ptp_pins[i];
316 snprintf(p->name, sizeof(p->name), "switch_1588_dat%d", i);
318 p->func = PTP_PF_NONE;
321 ocelot->ptp_info.pin_config = &ocelot->ptp_pins[0];
323 ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
324 if (IS_ERR(ptp_clock))
325 return PTR_ERR(ptp_clock);
326 /* Check if PHC support is missing at the configuration level */
330 ocelot->ptp_clock = ptp_clock;
332 ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
333 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
334 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
336 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
338 /* There is no device reconfiguration, PTP Rx stamping is always
341 ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
345 EXPORT_SYMBOL(ocelot_init_timestamp);
347 int ocelot_deinit_timestamp(struct ocelot *ocelot)
349 if (ocelot->ptp_clock)
350 ptp_clock_unregister(ocelot->ptp_clock);
353 EXPORT_SYMBOL(ocelot_deinit_timestamp);