1 // SPDX-License-Identifier: GPL-2.0+
3 * PTP hardware clock driver for the IDT ClockMatrix(TM) family of timing and
4 * synchronization devices.
6 * Copyright (C) 2019 Integrated Device Technology, Inc., a Renesas Company.
8 #include <linux/firmware.h>
9 #include <linux/platform_device.h>
10 #include <linux/module.h>
11 #include <linux/ptp_clock_kernel.h>
12 #include <linux/delay.h>
13 #include <linux/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/timekeeping.h>
16 #include <linux/string.h>
18 #include <linux/mfd/rsmu.h>
19 #include <linux/mfd/idt8a340_reg.h>
20 #include <asm/unaligned.h>
22 #include "ptp_private.h"
23 #include "ptp_clockmatrix.h"
25 MODULE_DESCRIPTION("Driver for IDT ClockMatrix(TM) family");
26 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
27 MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
28 MODULE_VERSION("1.0");
29 MODULE_LICENSE("GPL");
32 * The name of the firmware file to be loaded
33 * over-rides any automatic selection
35 static char *firmware;
36 module_param(firmware, charp, 0);
38 #define SETTIME_CORRECTION (0)
39 #define EXTTS_PERIOD_MS (95)
41 static int _idtcm_adjfine(struct idtcm_channel *channel, long scaled_ppm);
43 static inline int idtcm_read(struct idtcm *idtcm,
49 return regmap_bulk_read(idtcm->regmap, module + regaddr, buf, count);
52 static inline int idtcm_write(struct idtcm *idtcm,
58 return regmap_bulk_write(idtcm->regmap, module + regaddr, buf, count);
61 static int contains_full_configuration(struct idtcm *idtcm,
62 const struct firmware *fw)
64 struct idtcm_fwrc *rec = (struct idtcm_fwrc *)fw->data;
65 u16 scratch = IDTCM_FW_REG(idtcm->fw_ver, V520, SCRATCH);
72 /* 4 bytes skipped every 0x80 */
73 full_count = (scratch - GPIO_USER_CONTROL) -
74 ((scratch >> 7) - (GPIO_USER_CONTROL >> 7)) * 4;
76 /* If the firmware contains 'full configuration' SM_RESET can be used
77 * to ensure proper configuration.
79 * Full configuration is defined as the number of programmable
80 * bytes within the configuration range minus page offset addr range.
82 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
83 regaddr = rec->hiaddr << 8;
84 regaddr |= rec->loaddr;
90 /* Top (status registers) and bottom are read-only */
91 if (regaddr < GPIO_USER_CONTROL || regaddr >= scratch)
94 /* Page size 128, last 4 bytes of page skipped */
95 if ((loaddr > 0x7b && loaddr <= 0x7f) || loaddr > 0xfb)
101 return (count >= full_count);
104 static int char_array_to_timespec(u8 *buf,
106 struct timespec64 *ts)
112 if (count < TOD_BYTE_COUNT)
115 /* Sub-nanoseconds are in buf[0]. */
117 for (i = 0; i < 3; i++) {
123 for (i = 0; i < 5; i++) {
134 static int timespec_to_char_array(struct timespec64 const *ts,
142 if (count < TOD_BYTE_COUNT)
148 /* Sub-nanoseconds are in buf[0]. */
150 for (i = 1; i < 5; i++) {
151 buf[i] = nsec & 0xff;
155 for (i = 5; i < TOD_BYTE_COUNT; i++) {
164 static int idtcm_strverscmp(const char *version1, const char *version2)
169 if (sscanf(version1, "%hhu.%hhu.%hhu",
170 &ver1[0], &ver1[1], &ver1[2]) != 3)
172 if (sscanf(version2, "%hhu.%hhu.%hhu",
173 &ver2[0], &ver2[1], &ver2[2]) != 3)
176 for (i = 0; i < 3; i++) {
177 if (ver1[i] > ver2[i])
179 if (ver1[i] < ver2[i])
186 static enum fw_version idtcm_fw_version(const char *version)
188 enum fw_version ver = V_DEFAULT;
190 if (idtcm_strverscmp(version, "4.8.7") >= 0)
193 if (idtcm_strverscmp(version, "5.2.0") >= 0)
199 static int clear_boot_status(struct idtcm *idtcm)
203 return idtcm_write(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));
206 static int read_boot_status(struct idtcm *idtcm, u32 *status)
211 err = idtcm_read(idtcm, GENERAL_STATUS, BOOT_STATUS, buf, sizeof(buf));
213 *status = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
218 static int wait_for_boot_status_ready(struct idtcm *idtcm)
221 u8 i = 30; /* 30 * 100ms = 3s */
225 err = read_boot_status(idtcm, &status);
237 dev_warn(idtcm->dev, "%s timed out", __func__);
242 static int arm_tod_read_trig_sel_refclk(struct idtcm_channel *channel, u8 ref)
244 struct idtcm *idtcm = channel->idtcm;
245 u16 tod_read_cmd = IDTCM_FW_REG(idtcm->fw_ver, V520, TOD_READ_SECONDARY_CMD);
249 val &= ~(WR_REF_INDEX_MASK << WR_REF_INDEX_SHIFT);
250 val |= (ref << WR_REF_INDEX_SHIFT);
252 err = idtcm_write(idtcm, channel->tod_read_secondary,
253 TOD_READ_SECONDARY_SEL_CFG_0, &val, sizeof(val));
257 val = 0 | (SCSR_TOD_READ_TRIG_SEL_REFCLK << TOD_READ_TRIGGER_SHIFT);
259 err = idtcm_write(idtcm, channel->tod_read_secondary, tod_read_cmd,
262 dev_err(idtcm->dev, "%s: err = %d", __func__, err);
267 static bool is_single_shot(u8 mask)
269 /* Treat single bit ToD masks as continuous trigger */
270 return !(mask <= 8 && is_power_of_2(mask));
273 static int idtcm_extts_enable(struct idtcm_channel *channel,
274 struct ptp_clock_request *rq, int on)
276 u8 index = rq->extts.index;
278 u8 mask = 1 << index;
283 idtcm = channel->idtcm;
284 old_mask = idtcm->extts_mask;
286 /* Reject requests with unsupported flags */
287 if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
293 /* Reject requests to enable time stamping on falling edge */
294 if ((rq->extts.flags & PTP_ENABLE_FEATURE) &&
295 (rq->extts.flags & PTP_FALLING_EDGE))
298 if (index >= MAX_TOD)
302 /* Support triggering more than one TOD_0/1/2/3 by same pin */
303 /* Use the pin configured for the channel */
304 ref = ptp_find_pin(channel->ptp_clock, PTP_PF_EXTTS, channel->tod);
307 dev_err(idtcm->dev, "%s: No valid pin found for TOD%d!\n",
308 __func__, channel->tod);
312 err = arm_tod_read_trig_sel_refclk(&idtcm->channel[index], ref);
315 idtcm->extts_mask |= mask;
316 idtcm->event_channel[index] = channel;
317 idtcm->channel[index].refn = ref;
318 idtcm->extts_single_shot = is_single_shot(idtcm->extts_mask);
323 schedule_delayed_work(&idtcm->extts_work,
324 msecs_to_jiffies(EXTTS_PERIOD_MS));
327 idtcm->extts_mask &= ~mask;
328 idtcm->extts_single_shot = is_single_shot(idtcm->extts_mask);
330 if (idtcm->extts_mask == 0)
331 cancel_delayed_work(&idtcm->extts_work);
337 static int read_sys_apll_status(struct idtcm *idtcm, u8 *status)
339 return idtcm_read(idtcm, STATUS, DPLL_SYS_APLL_STATUS, status,
343 static int read_sys_dpll_status(struct idtcm *idtcm, u8 *status)
345 return idtcm_read(idtcm, STATUS, DPLL_SYS_STATUS, status, sizeof(u8));
348 static int wait_for_sys_apll_dpll_lock(struct idtcm *idtcm)
350 unsigned long timeout = jiffies + msecs_to_jiffies(LOCK_TIMEOUT_MS);
356 err = read_sys_apll_status(idtcm, &apll);
360 err = read_sys_dpll_status(idtcm, &dpll);
364 apll &= SYS_APLL_LOSS_LOCK_LIVE_MASK;
365 dpll &= DPLL_SYS_STATE_MASK;
367 if (apll == SYS_APLL_LOSS_LOCK_LIVE_LOCKED &&
368 dpll == DPLL_STATE_LOCKED) {
370 } else if (dpll == DPLL_STATE_FREERUN ||
371 dpll == DPLL_STATE_HOLDOVER ||
372 dpll == DPLL_STATE_OPEN_LOOP) {
374 "No wait state: DPLL_SYS_STATE %d", dpll);
378 msleep(LOCK_POLL_INTERVAL_MS);
379 } while (time_is_after_jiffies(timeout));
382 "%d ms lock timeout: SYS APLL Loss Lock %d SYS DPLL state %d",
383 LOCK_TIMEOUT_MS, apll, dpll);
388 static void wait_for_chip_ready(struct idtcm *idtcm)
390 if (wait_for_boot_status_ready(idtcm))
391 dev_warn(idtcm->dev, "BOOT_STATUS != 0xA0");
393 if (wait_for_sys_apll_dpll_lock(idtcm))
395 "Continuing while SYS APLL/DPLL is not locked");
398 static int _idtcm_gettime_triggered(struct idtcm_channel *channel,
399 struct timespec64 *ts)
401 struct idtcm *idtcm = channel->idtcm;
402 u16 tod_read_cmd = IDTCM_FW_REG(idtcm->fw_ver, V520, TOD_READ_SECONDARY_CMD);
403 u8 buf[TOD_BYTE_COUNT];
407 err = idtcm_read(idtcm, channel->tod_read_secondary,
408 tod_read_cmd, &trigger, sizeof(trigger));
412 if (trigger & TOD_READ_TRIGGER_MASK)
415 err = idtcm_read(idtcm, channel->tod_read_secondary,
416 TOD_READ_SECONDARY_BASE, buf, sizeof(buf));
420 return char_array_to_timespec(buf, sizeof(buf), ts);
423 static int _idtcm_gettime(struct idtcm_channel *channel,
424 struct timespec64 *ts, u8 timeout)
426 struct idtcm *idtcm = channel->idtcm;
427 u16 tod_read_cmd = IDTCM_FW_REG(idtcm->fw_ver, V520, TOD_READ_PRIMARY_CMD);
428 u8 buf[TOD_BYTE_COUNT];
432 /* wait trigger to be 0 */
437 if (idtcm->calculate_overhead_flag)
438 idtcm->start_time = ktime_get_raw();
440 err = idtcm_read(idtcm, channel->tod_read_primary,
441 tod_read_cmd, &trigger,
445 } while (trigger & TOD_READ_TRIGGER_MASK);
447 err = idtcm_read(idtcm, channel->tod_read_primary,
448 TOD_READ_PRIMARY_BASE, buf, sizeof(buf));
452 err = char_array_to_timespec(buf, sizeof(buf), ts);
457 static int idtcm_extts_check_channel(struct idtcm *idtcm, u8 todn)
459 struct idtcm_channel *ptp_channel, *extts_channel;
460 struct ptp_clock_event event;
461 struct timespec64 ts;
465 extts_channel = &idtcm->channel[todn];
466 ptp_channel = idtcm->event_channel[todn];
468 if (extts_channel == ptp_channel)
469 dco_delay = ptp_channel->dco_delay;
471 err = _idtcm_gettime_triggered(extts_channel, &ts);
475 /* Triggered - save timestamp */
476 event.type = PTP_CLOCK_EXTTS;
478 event.timestamp = timespec64_to_ns(&ts) - dco_delay;
479 ptp_clock_event(ptp_channel->ptp_clock, &event);
484 static int _idtcm_gettime_immediate(struct idtcm_channel *channel,
485 struct timespec64 *ts)
487 struct idtcm *idtcm = channel->idtcm;
489 u16 tod_read_cmd = IDTCM_FW_REG(idtcm->fw_ver, V520, TOD_READ_PRIMARY_CMD);
490 u8 val = (SCSR_TOD_READ_TRIG_SEL_IMMEDIATE << TOD_READ_TRIGGER_SHIFT);
493 err = idtcm_write(idtcm, channel->tod_read_primary,
494 tod_read_cmd, &val, sizeof(val));
498 return _idtcm_gettime(channel, ts, 10);
501 static int _sync_pll_output(struct idtcm *idtcm,
513 if (qn == 0 && qn_plus_1 == 0)
518 sync_ctrl0 = HW_Q0_Q1_CH_SYNC_CTRL_0;
519 sync_ctrl1 = HW_Q0_Q1_CH_SYNC_CTRL_1;
522 sync_ctrl0 = HW_Q2_Q3_CH_SYNC_CTRL_0;
523 sync_ctrl1 = HW_Q2_Q3_CH_SYNC_CTRL_1;
526 sync_ctrl0 = HW_Q4_Q5_CH_SYNC_CTRL_0;
527 sync_ctrl1 = HW_Q4_Q5_CH_SYNC_CTRL_1;
530 sync_ctrl0 = HW_Q6_Q7_CH_SYNC_CTRL_0;
531 sync_ctrl1 = HW_Q6_Q7_CH_SYNC_CTRL_1;
534 sync_ctrl0 = HW_Q8_CH_SYNC_CTRL_0;
535 sync_ctrl1 = HW_Q8_CH_SYNC_CTRL_1;
538 sync_ctrl0 = HW_Q9_CH_SYNC_CTRL_0;
539 sync_ctrl1 = HW_Q9_CH_SYNC_CTRL_1;
542 sync_ctrl0 = HW_Q10_CH_SYNC_CTRL_0;
543 sync_ctrl1 = HW_Q10_CH_SYNC_CTRL_1;
546 sync_ctrl0 = HW_Q11_CH_SYNC_CTRL_0;
547 sync_ctrl1 = HW_Q11_CH_SYNC_CTRL_1;
553 val = SYNCTRL1_MASTER_SYNC_RST;
555 /* Place master sync in reset */
556 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
560 err = idtcm_write(idtcm, 0, sync_ctrl0, &sync_src, sizeof(sync_src));
564 /* Set sync trigger mask */
565 val |= SYNCTRL1_FBDIV_FRAME_SYNC_TRIG | SYNCTRL1_FBDIV_SYNC_TRIG;
568 val |= SYNCTRL1_Q0_DIV_SYNC_TRIG;
571 val |= SYNCTRL1_Q1_DIV_SYNC_TRIG;
573 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
577 /* PLL5 can have OUT8 as second additional output. */
578 if (pll == 5 && qn_plus_1 != 0) {
579 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
580 &temp, sizeof(temp));
584 temp &= ~(Q9_TO_Q8_SYNC_TRIG);
586 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
587 &temp, sizeof(temp));
591 temp |= Q9_TO_Q8_SYNC_TRIG;
593 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
594 &temp, sizeof(temp));
599 /* PLL6 can have OUT11 as second additional output. */
600 if (pll == 6 && qn_plus_1 != 0) {
601 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
602 &temp, sizeof(temp));
606 temp &= ~(Q10_TO_Q11_SYNC_TRIG);
608 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
609 &temp, sizeof(temp));
613 temp |= Q10_TO_Q11_SYNC_TRIG;
615 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
616 &temp, sizeof(temp));
621 /* Place master sync out of reset */
622 val &= ~(SYNCTRL1_MASTER_SYNC_RST);
623 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
628 static int idtcm_sync_pps_output(struct idtcm_channel *channel)
630 struct idtcm *idtcm = channel->idtcm;
638 u16 output_mask = channel->output_mask;
640 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
641 &temp, sizeof(temp));
645 if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
646 Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
649 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
650 &temp, sizeof(temp));
654 if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
655 Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
658 for (pll = 0; pll < 8; pll++) {
663 /* First 4 pll has 2 outputs */
664 qn = output_mask & 0x1;
665 output_mask = output_mask >> 1;
666 qn_plus_1 = output_mask & 0x1;
667 output_mask = output_mask >> 1;
668 } else if (pll == 4) {
670 qn = output_mask & 0x1;
671 output_mask = output_mask >> 1;
673 } else if (pll == 5) {
675 qn_plus_1 = output_mask & 0x1;
676 output_mask = output_mask >> 1;
678 qn = output_mask & 0x1;
679 output_mask = output_mask >> 1;
680 } else if (pll == 6) {
681 qn = output_mask & 0x1;
682 output_mask = output_mask >> 1;
684 qn_plus_1 = output_mask & 0x1;
685 output_mask = output_mask >> 1;
687 } else if (pll == 7) {
688 if (out11_mux == 0) {
689 qn = output_mask & 0x1;
690 output_mask = output_mask >> 1;
694 if (qn != 0 || qn_plus_1 != 0)
695 err = _sync_pll_output(idtcm, pll, channel->sync_src,
705 static int _idtcm_set_dpll_hw_tod(struct idtcm_channel *channel,
706 struct timespec64 const *ts,
707 enum hw_tod_write_trig_sel wr_trig)
709 struct idtcm *idtcm = channel->idtcm;
710 u8 buf[TOD_BYTE_COUNT];
713 struct timespec64 local_ts = *ts;
714 s64 total_overhead_ns;
716 /* Configure HW TOD write trigger. */
717 err = idtcm_read(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
723 cmd |= wr_trig | 0x08;
725 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
730 if (wr_trig != HW_TOD_WR_TRIG_SEL_MSB) {
731 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
735 err = idtcm_write(idtcm, channel->hw_dpll_n,
736 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
741 /* ARM HW TOD write trigger. */
744 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
747 if (wr_trig == HW_TOD_WR_TRIG_SEL_MSB) {
748 if (idtcm->calculate_overhead_flag) {
749 /* Assumption: I2C @ 400KHz */
750 ktime_t diff = ktime_sub(ktime_get_raw(),
752 total_overhead_ns = ktime_to_ns(diff)
753 + idtcm->tod_write_overhead_ns
754 + SETTIME_CORRECTION;
756 timespec64_add_ns(&local_ts, total_overhead_ns);
758 idtcm->calculate_overhead_flag = 0;
761 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
765 err = idtcm_write(idtcm, channel->hw_dpll_n,
766 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
772 static int _idtcm_set_dpll_scsr_tod(struct idtcm_channel *channel,
773 struct timespec64 const *ts,
774 enum scsr_tod_write_trig_sel wr_trig,
775 enum scsr_tod_write_type_sel wr_type)
777 struct idtcm *idtcm = channel->idtcm;
778 unsigned char buf[TOD_BYTE_COUNT], cmd;
779 struct timespec64 local_ts = *ts;
782 timespec64_add_ns(&local_ts, SETTIME_CORRECTION);
784 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
788 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE,
793 /* Trigger the write operation. */
794 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
799 cmd &= ~(TOD_WRITE_SELECTION_MASK << TOD_WRITE_SELECTION_SHIFT);
800 cmd &= ~(TOD_WRITE_TYPE_MASK << TOD_WRITE_TYPE_SHIFT);
801 cmd |= (wr_trig << TOD_WRITE_SELECTION_SHIFT);
802 cmd |= (wr_type << TOD_WRITE_TYPE_SHIFT);
804 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE_CMD,
809 /* Wait for the operation to complete. */
811 /* pps trigger takes up to 1 sec to complete */
812 if (wr_trig == SCSR_TOD_WR_TRIG_SEL_TODPPS)
815 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
820 if ((cmd & TOD_WRITE_SELECTION_MASK) == 0)
825 "Timed out waiting for the write counter");
833 static int get_output_base_addr(enum fw_version ver, u8 outn)
839 base = IDTCM_FW_REG(ver, V520, OUTPUT_0);
842 base = IDTCM_FW_REG(ver, V520, OUTPUT_1);
845 base = IDTCM_FW_REG(ver, V520, OUTPUT_2);
848 base = IDTCM_FW_REG(ver, V520, OUTPUT_3);
851 base = IDTCM_FW_REG(ver, V520, OUTPUT_4);
854 base = IDTCM_FW_REG(ver, V520, OUTPUT_5);
857 base = IDTCM_FW_REG(ver, V520, OUTPUT_6);
860 base = IDTCM_FW_REG(ver, V520, OUTPUT_7);
863 base = IDTCM_FW_REG(ver, V520, OUTPUT_8);
866 base = IDTCM_FW_REG(ver, V520, OUTPUT_9);
869 base = IDTCM_FW_REG(ver, V520, OUTPUT_10);
872 base = IDTCM_FW_REG(ver, V520, OUTPUT_11);
881 static int _idtcm_settime_deprecated(struct idtcm_channel *channel,
882 struct timespec64 const *ts)
884 struct idtcm *idtcm = channel->idtcm;
887 err = _idtcm_set_dpll_hw_tod(channel, ts, HW_TOD_WR_TRIG_SEL_MSB);
890 "%s: Set HW ToD failed", __func__);
894 return idtcm_sync_pps_output(channel);
897 static int _idtcm_settime(struct idtcm_channel *channel,
898 struct timespec64 const *ts,
899 enum scsr_tod_write_type_sel wr_type)
901 return _idtcm_set_dpll_scsr_tod(channel, ts,
902 SCSR_TOD_WR_TRIG_SEL_IMMEDIATE,
906 static int idtcm_set_phase_pull_in_offset(struct idtcm_channel *channel,
911 struct idtcm *idtcm = channel->idtcm;
914 for (i = 0; i < 4; i++) {
915 buf[i] = 0xff & (offset_ns);
919 err = idtcm_write(idtcm, channel->dpll_phase_pull_in, PULL_IN_OFFSET,
925 static int idtcm_set_phase_pull_in_slope_limit(struct idtcm_channel *channel,
930 struct idtcm *idtcm = channel->idtcm;
933 if (max_ffo_ppb & 0xff000000)
936 for (i = 0; i < 3; i++) {
937 buf[i] = 0xff & (max_ffo_ppb);
941 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
942 PULL_IN_SLOPE_LIMIT, buf, sizeof(buf));
947 static int idtcm_start_phase_pull_in(struct idtcm_channel *channel)
950 struct idtcm *idtcm = channel->idtcm;
953 err = idtcm_read(idtcm, channel->dpll_phase_pull_in, PULL_IN_CTRL,
960 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
961 PULL_IN_CTRL, &buf, sizeof(buf));
969 static int do_phase_pull_in_fw(struct idtcm_channel *channel,
975 err = idtcm_set_phase_pull_in_offset(channel, -offset_ns);
979 err = idtcm_set_phase_pull_in_slope_limit(channel, max_ffo_ppb);
983 err = idtcm_start_phase_pull_in(channel);
988 static int set_tod_write_overhead(struct idtcm_channel *channel)
990 struct idtcm *idtcm = channel->idtcm;
999 char buf[TOD_BYTE_COUNT] = {0};
1001 /* Set page offset */
1002 idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_OVR__0,
1005 for (i = 0; i < TOD_WRITE_OVERHEAD_COUNT_MAX; i++) {
1006 start = ktime_get_raw();
1008 err = idtcm_write(idtcm, channel->hw_dpll_n,
1009 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
1013 stop = ktime_get_raw();
1015 diff = ktime_sub(stop, start);
1017 current_ns = ktime_to_ns(diff);
1020 lowest_ns = current_ns;
1022 if (current_ns < lowest_ns)
1023 lowest_ns = current_ns;
1027 idtcm->tod_write_overhead_ns = lowest_ns;
1032 static int _idtcm_adjtime_deprecated(struct idtcm_channel *channel, s64 delta)
1035 struct idtcm *idtcm = channel->idtcm;
1036 struct timespec64 ts;
1039 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS_DEPRECATED) {
1040 err = channel->do_phase_pull_in(channel, delta, 0);
1042 idtcm->calculate_overhead_flag = 1;
1044 err = set_tod_write_overhead(channel);
1048 err = _idtcm_gettime_immediate(channel, &ts);
1052 now = timespec64_to_ns(&ts);
1055 ts = ns_to_timespec64(now);
1057 err = _idtcm_settime_deprecated(channel, &ts);
1063 static int idtcm_state_machine_reset(struct idtcm *idtcm)
1065 u8 byte = SM_RESET_CMD;
1070 clear_boot_status(idtcm);
1072 err = idtcm_write(idtcm, RESET_CTRL,
1073 IDTCM_FW_REG(idtcm->fw_ver, V520, SM_RESET),
1074 &byte, sizeof(byte));
1077 for (i = 0; i < 30; i++) {
1078 msleep_interruptible(100);
1079 read_boot_status(idtcm, &status);
1081 if (status == 0xA0) {
1083 "SM_RESET completed in %d ms", i * 100);
1090 "Timed out waiting for CM_RESET to complete");
1096 static int idtcm_read_hw_rev_id(struct idtcm *idtcm, u8 *hw_rev_id)
1098 return idtcm_read(idtcm, HW_REVISION, REV_ID, hw_rev_id, sizeof(u8));
1101 static int idtcm_read_product_id(struct idtcm *idtcm, u16 *product_id)
1106 err = idtcm_read(idtcm, GENERAL_STATUS, PRODUCT_ID, buf, sizeof(buf));
1108 *product_id = (buf[1] << 8) | buf[0];
1113 static int idtcm_read_major_release(struct idtcm *idtcm, u8 *major)
1118 err = idtcm_read(idtcm, GENERAL_STATUS, MAJ_REL, &buf, sizeof(buf));
1125 static int idtcm_read_minor_release(struct idtcm *idtcm, u8 *minor)
1127 return idtcm_read(idtcm, GENERAL_STATUS, MIN_REL, minor, sizeof(u8));
1130 static int idtcm_read_hotfix_release(struct idtcm *idtcm, u8 *hotfix)
1132 return idtcm_read(idtcm,
1139 static int idtcm_read_otp_scsr_config_select(struct idtcm *idtcm,
1142 return idtcm_read(idtcm, GENERAL_STATUS, OTP_SCSR_CONFIG_SELECT,
1143 config_select, sizeof(u8));
1146 static int set_pll_output_mask(struct idtcm *idtcm, u16 addr, u8 val)
1151 case TOD0_OUT_ALIGN_MASK_ADDR:
1152 SET_U16_LSB(idtcm->channel[0].output_mask, val);
1154 case TOD0_OUT_ALIGN_MASK_ADDR + 1:
1155 SET_U16_MSB(idtcm->channel[0].output_mask, val);
1157 case TOD1_OUT_ALIGN_MASK_ADDR:
1158 SET_U16_LSB(idtcm->channel[1].output_mask, val);
1160 case TOD1_OUT_ALIGN_MASK_ADDR + 1:
1161 SET_U16_MSB(idtcm->channel[1].output_mask, val);
1163 case TOD2_OUT_ALIGN_MASK_ADDR:
1164 SET_U16_LSB(idtcm->channel[2].output_mask, val);
1166 case TOD2_OUT_ALIGN_MASK_ADDR + 1:
1167 SET_U16_MSB(idtcm->channel[2].output_mask, val);
1169 case TOD3_OUT_ALIGN_MASK_ADDR:
1170 SET_U16_LSB(idtcm->channel[3].output_mask, val);
1172 case TOD3_OUT_ALIGN_MASK_ADDR + 1:
1173 SET_U16_MSB(idtcm->channel[3].output_mask, val);
1176 err = -EFAULT; /* Bad address */;
1183 static int set_tod_ptp_pll(struct idtcm *idtcm, u8 index, u8 pll)
1185 if (index >= MAX_TOD) {
1186 dev_err(idtcm->dev, "ToD%d not supported", index);
1190 if (pll >= MAX_PLL) {
1191 dev_err(idtcm->dev, "Pll%d not supported", pll);
1195 idtcm->channel[index].pll = pll;
1200 static int check_and_set_masks(struct idtcm *idtcm,
1208 if ((val & 0xf0) || !(val & 0x0f)) {
1209 dev_err(idtcm->dev, "Invalid TOD mask 0x%02x", val);
1212 idtcm->tod_mask = val;
1215 case TOD0_PTP_PLL_ADDR:
1216 err = set_tod_ptp_pll(idtcm, 0, val);
1218 case TOD1_PTP_PLL_ADDR:
1219 err = set_tod_ptp_pll(idtcm, 1, val);
1221 case TOD2_PTP_PLL_ADDR:
1222 err = set_tod_ptp_pll(idtcm, 2, val);
1224 case TOD3_PTP_PLL_ADDR:
1225 err = set_tod_ptp_pll(idtcm, 3, val);
1228 err = set_pll_output_mask(idtcm, regaddr, val);
1235 static void display_pll_and_masks(struct idtcm *idtcm)
1240 dev_dbg(idtcm->dev, "tod_mask = 0x%02x", idtcm->tod_mask);
1242 for (i = 0; i < MAX_TOD; i++) {
1245 if (mask & idtcm->tod_mask)
1247 "TOD%d pll = %d output_mask = 0x%04x",
1248 i, idtcm->channel[i].pll,
1249 idtcm->channel[i].output_mask);
1253 static int idtcm_load_firmware(struct idtcm *idtcm,
1256 u16 scratch = IDTCM_FW_REG(idtcm->fw_ver, V520, SCRATCH);
1257 char fname[128] = FW_FILENAME;
1258 const struct firmware *fw;
1259 struct idtcm_fwrc *rec;
1266 if (firmware) /* module parameter */
1267 snprintf(fname, sizeof(fname), "%s", firmware);
1269 dev_info(idtcm->dev, "requesting firmware '%s'", fname);
1271 err = request_firmware(&fw, fname, dev);
1274 "Failed at line %d in %s!", __LINE__, __func__);
1278 dev_dbg(idtcm->dev, "firmware size %zu bytes", fw->size);
1280 rec = (struct idtcm_fwrc *) fw->data;
1282 if (contains_full_configuration(idtcm, fw))
1283 idtcm_state_machine_reset(idtcm);
1285 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
1286 if (rec->reserved) {
1288 "bad firmware, reserved field non-zero");
1291 regaddr = rec->hiaddr << 8;
1292 regaddr |= rec->loaddr;
1295 loaddr = rec->loaddr;
1299 err = check_and_set_masks(idtcm, regaddr, val);
1302 if (err != -EINVAL) {
1305 /* Top (status registers) and bottom are read-only */
1306 if (regaddr < GPIO_USER_CONTROL || regaddr >= scratch)
1309 /* Page size 128, last 4 bytes of page skipped */
1310 if ((loaddr > 0x7b && loaddr <= 0x7f) || loaddr > 0xfb)
1313 err = idtcm_write(idtcm, regaddr, 0, &val, sizeof(val));
1320 display_pll_and_masks(idtcm);
1323 release_firmware(fw);
1327 static int idtcm_output_enable(struct idtcm_channel *channel,
1328 bool enable, unsigned int outn)
1330 struct idtcm *idtcm = channel->idtcm;
1335 base = get_output_base_addr(idtcm->fw_ver, outn);
1339 "%s - Unsupported out%d", __func__, outn);
1343 err = idtcm_read(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1348 val |= SQUELCH_DISABLE;
1350 val &= ~SQUELCH_DISABLE;
1352 return idtcm_write(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1355 static int idtcm_perout_enable(struct idtcm_channel *channel,
1356 struct ptp_perout_request *perout,
1359 struct idtcm *idtcm = channel->idtcm;
1360 struct timespec64 ts = {0, 0};
1363 err = idtcm_output_enable(channel, enable, perout->index);
1366 dev_err(idtcm->dev, "Unable to set output enable");
1370 /* Align output to internal 1 PPS */
1371 return _idtcm_settime(channel, &ts, SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS);
1374 static int idtcm_get_pll_mode(struct idtcm_channel *channel,
1375 enum pll_mode *mode)
1377 struct idtcm *idtcm = channel->idtcm;
1381 err = idtcm_read(idtcm, channel->dpll_n,
1382 IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_MODE),
1383 &dpll_mode, sizeof(dpll_mode));
1387 *mode = (dpll_mode >> PLL_MODE_SHIFT) & PLL_MODE_MASK;
1392 static int idtcm_set_pll_mode(struct idtcm_channel *channel,
1395 struct idtcm *idtcm = channel->idtcm;
1399 err = idtcm_read(idtcm, channel->dpll_n,
1400 IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_MODE),
1401 &dpll_mode, sizeof(dpll_mode));
1405 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
1407 dpll_mode |= (mode << PLL_MODE_SHIFT);
1409 err = idtcm_write(idtcm, channel->dpll_n,
1410 IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_MODE),
1411 &dpll_mode, sizeof(dpll_mode));
1415 static int idtcm_get_manual_reference(struct idtcm_channel *channel,
1416 enum manual_reference *ref)
1418 struct idtcm *idtcm = channel->idtcm;
1419 u8 dpll_manu_ref_cfg;
1422 err = idtcm_read(idtcm, channel->dpll_ctrl_n,
1423 DPLL_CTRL_DPLL_MANU_REF_CFG,
1424 &dpll_manu_ref_cfg, sizeof(dpll_manu_ref_cfg));
1428 dpll_manu_ref_cfg &= (MANUAL_REFERENCE_MASK << MANUAL_REFERENCE_SHIFT);
1430 *ref = dpll_manu_ref_cfg >> MANUAL_REFERENCE_SHIFT;
1435 static int idtcm_set_manual_reference(struct idtcm_channel *channel,
1436 enum manual_reference ref)
1438 struct idtcm *idtcm = channel->idtcm;
1439 u8 dpll_manu_ref_cfg;
1442 err = idtcm_read(idtcm, channel->dpll_ctrl_n,
1443 DPLL_CTRL_DPLL_MANU_REF_CFG,
1444 &dpll_manu_ref_cfg, sizeof(dpll_manu_ref_cfg));
1448 dpll_manu_ref_cfg &= ~(MANUAL_REFERENCE_MASK << MANUAL_REFERENCE_SHIFT);
1450 dpll_manu_ref_cfg |= (ref << MANUAL_REFERENCE_SHIFT);
1452 err = idtcm_write(idtcm, channel->dpll_ctrl_n,
1453 DPLL_CTRL_DPLL_MANU_REF_CFG,
1454 &dpll_manu_ref_cfg, sizeof(dpll_manu_ref_cfg));
1459 static int configure_dpll_mode_write_frequency(struct idtcm_channel *channel)
1461 struct idtcm *idtcm = channel->idtcm;
1464 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_FREQUENCY);
1467 dev_err(idtcm->dev, "Failed to set pll mode to write frequency");
1469 channel->mode = PTP_PLL_MODE_WRITE_FREQUENCY;
1474 static int configure_dpll_mode_write_phase(struct idtcm_channel *channel)
1476 struct idtcm *idtcm = channel->idtcm;
1479 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_PHASE);
1482 dev_err(idtcm->dev, "Failed to set pll mode to write phase");
1484 channel->mode = PTP_PLL_MODE_WRITE_PHASE;
1489 static int configure_manual_reference_write_frequency(struct idtcm_channel *channel)
1491 struct idtcm *idtcm = channel->idtcm;
1494 err = idtcm_set_manual_reference(channel, MANU_REF_WRITE_FREQUENCY);
1497 dev_err(idtcm->dev, "Failed to set manual reference to write frequency");
1499 channel->mode = PTP_PLL_MODE_WRITE_FREQUENCY;
1504 static int configure_manual_reference_write_phase(struct idtcm_channel *channel)
1506 struct idtcm *idtcm = channel->idtcm;
1509 err = idtcm_set_manual_reference(channel, MANU_REF_WRITE_PHASE);
1512 dev_err(idtcm->dev, "Failed to set manual reference to write phase");
1514 channel->mode = PTP_PLL_MODE_WRITE_PHASE;
1519 static int idtcm_stop_phase_pull_in(struct idtcm_channel *channel)
1523 err = _idtcm_adjfine(channel, channel->current_freq_scaled_ppm);
1527 channel->phase_pull_in = false;
1532 static long idtcm_work_handler(struct ptp_clock_info *ptp)
1534 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1535 struct idtcm *idtcm = channel->idtcm;
1537 mutex_lock(idtcm->lock);
1539 (void)idtcm_stop_phase_pull_in(channel);
1541 mutex_unlock(idtcm->lock);
1543 /* Return a negative value here to not reschedule */
1547 static s32 phase_pull_in_scaled_ppm(s32 current_ppm, s32 phase_pull_in_ppb)
1549 /* ppb = scaled_ppm * 125 / 2^13 */
1550 /* scaled_ppm = ppb * 2^13 / 125 */
1552 s64 max_scaled_ppm = div_s64((s64)PHASE_PULL_IN_MAX_PPB << 13, 125);
1553 s64 scaled_ppm = div_s64((s64)phase_pull_in_ppb << 13, 125);
1555 current_ppm += scaled_ppm;
1557 if (current_ppm > max_scaled_ppm)
1558 current_ppm = max_scaled_ppm;
1559 else if (current_ppm < -max_scaled_ppm)
1560 current_ppm = -max_scaled_ppm;
1565 static int do_phase_pull_in_sw(struct idtcm_channel *channel,
1569 s32 current_ppm = channel->current_freq_scaled_ppm;
1570 u32 duration_ms = MSEC_PER_SEC;
1575 /* If the ToD correction is less than PHASE_PULL_IN_MIN_THRESHOLD_NS,
1576 * skip. The error introduced by the ToD adjustment procedure would
1577 * be bigger than the required ToD correction
1579 if (abs(delta_ns) < PHASE_PULL_IN_MIN_THRESHOLD_NS)
1582 if (max_ffo_ppb == 0)
1583 max_ffo_ppb = PHASE_PULL_IN_MAX_PPB;
1585 /* For most cases, keep phase pull-in duration 1 second */
1587 while (abs(ppb) > max_ffo_ppb) {
1592 delta_ppm = phase_pull_in_scaled_ppm(current_ppm, ppb);
1594 err = _idtcm_adjfine(channel, delta_ppm);
1599 /* schedule the worker to cancel phase pull-in */
1600 ptp_schedule_worker(channel->ptp_clock,
1601 msecs_to_jiffies(duration_ms) - 1);
1603 channel->phase_pull_in = true;
1608 static int initialize_operating_mode_with_manual_reference(struct idtcm_channel *channel,
1609 enum manual_reference ref)
1611 struct idtcm *idtcm = channel->idtcm;
1613 channel->mode = PTP_PLL_MODE_UNSUPPORTED;
1614 channel->configure_write_frequency = configure_manual_reference_write_frequency;
1615 channel->configure_write_phase = configure_manual_reference_write_phase;
1616 channel->do_phase_pull_in = do_phase_pull_in_sw;
1619 case MANU_REF_WRITE_PHASE:
1620 channel->mode = PTP_PLL_MODE_WRITE_PHASE;
1622 case MANU_REF_WRITE_FREQUENCY:
1623 channel->mode = PTP_PLL_MODE_WRITE_FREQUENCY;
1626 dev_warn(idtcm->dev,
1627 "Unsupported MANUAL_REFERENCE: 0x%02x", ref);
1633 static int initialize_operating_mode_with_pll_mode(struct idtcm_channel *channel,
1636 struct idtcm *idtcm = channel->idtcm;
1639 channel->mode = PTP_PLL_MODE_UNSUPPORTED;
1640 channel->configure_write_frequency = configure_dpll_mode_write_frequency;
1641 channel->configure_write_phase = configure_dpll_mode_write_phase;
1642 channel->do_phase_pull_in = do_phase_pull_in_fw;
1645 case PLL_MODE_WRITE_PHASE:
1646 channel->mode = PTP_PLL_MODE_WRITE_PHASE;
1648 case PLL_MODE_WRITE_FREQUENCY:
1649 channel->mode = PTP_PLL_MODE_WRITE_FREQUENCY;
1653 "Unsupported PLL_MODE: 0x%02x", mode);
1660 static int initialize_dco_operating_mode(struct idtcm_channel *channel)
1662 enum manual_reference ref = MANU_REF_XO_DPLL;
1663 enum pll_mode mode = PLL_MODE_DISABLED;
1664 struct idtcm *idtcm = channel->idtcm;
1667 channel->mode = PTP_PLL_MODE_UNSUPPORTED;
1669 err = idtcm_get_pll_mode(channel, &mode);
1671 dev_err(idtcm->dev, "Unable to read pll mode!");
1675 if (mode == PLL_MODE_PLL) {
1676 err = idtcm_get_manual_reference(channel, &ref);
1678 dev_err(idtcm->dev, "Unable to read manual reference!");
1681 err = initialize_operating_mode_with_manual_reference(channel, ref);
1683 err = initialize_operating_mode_with_pll_mode(channel, mode);
1686 if (channel->mode == PTP_PLL_MODE_WRITE_PHASE)
1687 channel->configure_write_frequency(channel);
1692 /* PTP Hardware Clock interface */
1695 * Maximum absolute value for write phase offset in nanoseconds
1697 * Destination signed register is 32-bit register in resolution of 50ps
1699 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350 ps
1700 * Represent 107374182350 ps as 107374182 ns
1702 static s32 idtcm_getmaxphase(struct ptp_clock_info *ptp __always_unused)
1704 return MAX_ABS_WRITE_PHASE_NANOSECONDS;
1708 * Internal function for implementing support for write phase offset
1711 * @delta_ns: delta in nanoseconds
1713 static int _idtcm_adjphase(struct idtcm_channel *channel, s32 delta_ns)
1715 struct idtcm *idtcm = channel->idtcm;
1721 if (channel->mode != PTP_PLL_MODE_WRITE_PHASE) {
1722 err = channel->configure_write_phase(channel);
1727 phase_50ps = div_s64((s64)delta_ns * 1000, 50);
1729 for (i = 0; i < 4; i++) {
1730 buf[i] = phase_50ps & 0xff;
1734 err = idtcm_write(idtcm, channel->dpll_phase, DPLL_WR_PHASE,
1740 static int _idtcm_adjfine(struct idtcm_channel *channel, long scaled_ppm)
1742 struct idtcm *idtcm = channel->idtcm;
1748 if (channel->mode != PTP_PLL_MODE_WRITE_FREQUENCY) {
1749 err = channel->configure_write_frequency(channel);
1755 * Frequency Control Word unit is: 1.11 * 10^-10 ppm
1764 * FCW = -------------
1768 /* 2 ^ -53 = 1.1102230246251565404236316680908e-16 */
1769 fcw = scaled_ppm * 244140625ULL;
1771 fcw = div_s64(fcw, 1776);
1773 for (i = 0; i < 6; i++) {
1774 buf[i] = fcw & 0xff;
1778 err = idtcm_write(idtcm, channel->dpll_freq, DPLL_WR_FREQ,
1784 static int idtcm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
1786 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1787 struct idtcm *idtcm = channel->idtcm;
1790 mutex_lock(idtcm->lock);
1791 err = _idtcm_gettime_immediate(channel, ts);
1792 mutex_unlock(idtcm->lock);
1795 dev_err(idtcm->dev, "Failed at line %d in %s!",
1796 __LINE__, __func__);
1801 static int idtcm_settime_deprecated(struct ptp_clock_info *ptp,
1802 const struct timespec64 *ts)
1804 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1805 struct idtcm *idtcm = channel->idtcm;
1808 mutex_lock(idtcm->lock);
1809 err = _idtcm_settime_deprecated(channel, ts);
1810 mutex_unlock(idtcm->lock);
1814 "Failed at line %d in %s!", __LINE__, __func__);
1819 static int idtcm_settime(struct ptp_clock_info *ptp,
1820 const struct timespec64 *ts)
1822 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1823 struct idtcm *idtcm = channel->idtcm;
1826 mutex_lock(idtcm->lock);
1827 err = _idtcm_settime(channel, ts, SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1828 mutex_unlock(idtcm->lock);
1832 "Failed at line %d in %s!", __LINE__, __func__);
1837 static int idtcm_adjtime_deprecated(struct ptp_clock_info *ptp, s64 delta)
1839 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1840 struct idtcm *idtcm = channel->idtcm;
1843 mutex_lock(idtcm->lock);
1844 err = _idtcm_adjtime_deprecated(channel, delta);
1845 mutex_unlock(idtcm->lock);
1849 "Failed at line %d in %s!", __LINE__, __func__);
1854 static int idtcm_adjtime(struct ptp_clock_info *ptp, s64 delta)
1856 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1857 struct idtcm *idtcm = channel->idtcm;
1858 struct timespec64 ts;
1859 enum scsr_tod_write_type_sel type;
1862 if (channel->phase_pull_in == true)
1865 mutex_lock(idtcm->lock);
1867 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS) {
1868 err = channel->do_phase_pull_in(channel, delta, 0);
1871 ts = ns_to_timespec64(delta);
1872 type = SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS;
1874 ts = ns_to_timespec64(-delta);
1875 type = SCSR_TOD_WR_TYPE_SEL_DELTA_MINUS;
1877 err = _idtcm_settime(channel, &ts, type);
1880 mutex_unlock(idtcm->lock);
1884 "Failed at line %d in %s!", __LINE__, __func__);
1889 static int idtcm_adjphase(struct ptp_clock_info *ptp, s32 delta)
1891 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1892 struct idtcm *idtcm = channel->idtcm;
1895 mutex_lock(idtcm->lock);
1896 err = _idtcm_adjphase(channel, delta);
1897 mutex_unlock(idtcm->lock);
1901 "Failed at line %d in %s!", __LINE__, __func__);
1906 static int idtcm_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
1908 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1909 struct idtcm *idtcm = channel->idtcm;
1912 if (channel->phase_pull_in == true)
1915 if (scaled_ppm == channel->current_freq_scaled_ppm)
1918 mutex_lock(idtcm->lock);
1919 err = _idtcm_adjfine(channel, scaled_ppm);
1920 mutex_unlock(idtcm->lock);
1924 "Failed at line %d in %s!", __LINE__, __func__);
1926 channel->current_freq_scaled_ppm = scaled_ppm;
1931 static int idtcm_enable(struct ptp_clock_info *ptp,
1932 struct ptp_clock_request *rq, int on)
1934 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1935 struct idtcm *idtcm = channel->idtcm;
1936 int err = -EOPNOTSUPP;
1938 mutex_lock(idtcm->lock);
1941 case PTP_CLK_REQ_PEROUT:
1943 err = idtcm_perout_enable(channel, &rq->perout, false);
1944 /* Only accept a 1-PPS aligned to the second. */
1945 else if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
1946 rq->perout.period.nsec)
1949 err = idtcm_perout_enable(channel, &rq->perout, true);
1951 case PTP_CLK_REQ_EXTTS:
1952 err = idtcm_extts_enable(channel, rq, on);
1958 mutex_unlock(idtcm->lock);
1961 dev_err(channel->idtcm->dev,
1962 "Failed in %s with err %d!", __func__, err);
1967 static int idtcm_enable_tod(struct idtcm_channel *channel)
1969 struct idtcm *idtcm = channel->idtcm;
1970 struct timespec64 ts = {0, 0};
1971 u16 tod_cfg = IDTCM_FW_REG(idtcm->fw_ver, V520, TOD_CFG);
1976 * Start the TOD clock ticking.
1978 err = idtcm_read(idtcm, channel->tod_n, tod_cfg, &cfg, sizeof(cfg));
1984 err = idtcm_write(idtcm, channel->tod_n, tod_cfg, &cfg, sizeof(cfg));
1988 if (idtcm->fw_ver < V487)
1989 return _idtcm_settime_deprecated(channel, &ts);
1991 return _idtcm_settime(channel, &ts,
1992 SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1995 static void idtcm_set_version_info(struct idtcm *idtcm)
2004 idtcm_read_major_release(idtcm, &major);
2005 idtcm_read_minor_release(idtcm, &minor);
2006 idtcm_read_hotfix_release(idtcm, &hotfix);
2008 idtcm_read_product_id(idtcm, &product_id);
2009 idtcm_read_hw_rev_id(idtcm, &hw_rev_id);
2011 idtcm_read_otp_scsr_config_select(idtcm, &config_select);
2013 snprintf(idtcm->version, sizeof(idtcm->version), "%u.%u.%u",
2014 major, minor, hotfix);
2016 idtcm->fw_ver = idtcm_fw_version(idtcm->version);
2018 dev_info(idtcm->dev,
2019 "%d.%d.%d, Id: 0x%04x HW Rev: %d OTP Config Select: %d",
2020 major, minor, hotfix,
2021 product_id, hw_rev_id, config_select);
2024 static int idtcm_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
2025 enum ptp_pin_function func, unsigned int chan)
2032 case PTP_PF_PHYSYNC:
2038 static struct ptp_pin_desc pin_config[MAX_TOD][MAX_REF_CLK];
2040 static const struct ptp_clock_info idtcm_caps = {
2041 .owner = THIS_MODULE,
2044 .n_ext_ts = MAX_TOD,
2045 .n_pins = MAX_REF_CLK,
2046 .adjphase = &idtcm_adjphase,
2047 .getmaxphase = &idtcm_getmaxphase,
2048 .adjfine = &idtcm_adjfine,
2049 .adjtime = &idtcm_adjtime,
2050 .gettime64 = &idtcm_gettime,
2051 .settime64 = &idtcm_settime,
2052 .enable = &idtcm_enable,
2053 .verify = &idtcm_verify_pin,
2054 .do_aux_work = &idtcm_work_handler,
2057 static const struct ptp_clock_info idtcm_caps_deprecated = {
2058 .owner = THIS_MODULE,
2061 .n_ext_ts = MAX_TOD,
2062 .n_pins = MAX_REF_CLK,
2063 .adjphase = &idtcm_adjphase,
2064 .getmaxphase = &idtcm_getmaxphase,
2065 .adjfine = &idtcm_adjfine,
2066 .adjtime = &idtcm_adjtime_deprecated,
2067 .gettime64 = &idtcm_gettime,
2068 .settime64 = &idtcm_settime_deprecated,
2069 .enable = &idtcm_enable,
2070 .verify = &idtcm_verify_pin,
2071 .do_aux_work = &idtcm_work_handler,
2074 static int configure_channel_pll(struct idtcm_channel *channel)
2076 struct idtcm *idtcm = channel->idtcm;
2079 switch (channel->pll) {
2081 channel->dpll_freq = DPLL_FREQ_0;
2082 channel->dpll_n = DPLL_0;
2083 channel->hw_dpll_n = HW_DPLL_0;
2084 channel->dpll_phase = DPLL_PHASE_0;
2085 channel->dpll_ctrl_n = DPLL_CTRL_0;
2086 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_0;
2089 channel->dpll_freq = DPLL_FREQ_1;
2090 channel->dpll_n = DPLL_1;
2091 channel->hw_dpll_n = HW_DPLL_1;
2092 channel->dpll_phase = DPLL_PHASE_1;
2093 channel->dpll_ctrl_n = DPLL_CTRL_1;
2094 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_1;
2097 channel->dpll_freq = DPLL_FREQ_2;
2098 channel->dpll_n = IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_2);
2099 channel->hw_dpll_n = HW_DPLL_2;
2100 channel->dpll_phase = DPLL_PHASE_2;
2101 channel->dpll_ctrl_n = DPLL_CTRL_2;
2102 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_2;
2105 channel->dpll_freq = DPLL_FREQ_3;
2106 channel->dpll_n = DPLL_3;
2107 channel->hw_dpll_n = HW_DPLL_3;
2108 channel->dpll_phase = DPLL_PHASE_3;
2109 channel->dpll_ctrl_n = DPLL_CTRL_3;
2110 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_3;
2113 channel->dpll_freq = DPLL_FREQ_4;
2114 channel->dpll_n = IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_4);
2115 channel->hw_dpll_n = HW_DPLL_4;
2116 channel->dpll_phase = DPLL_PHASE_4;
2117 channel->dpll_ctrl_n = DPLL_CTRL_4;
2118 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_4;
2121 channel->dpll_freq = DPLL_FREQ_5;
2122 channel->dpll_n = DPLL_5;
2123 channel->hw_dpll_n = HW_DPLL_5;
2124 channel->dpll_phase = DPLL_PHASE_5;
2125 channel->dpll_ctrl_n = DPLL_CTRL_5;
2126 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_5;
2129 channel->dpll_freq = DPLL_FREQ_6;
2130 channel->dpll_n = IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_6);
2131 channel->hw_dpll_n = HW_DPLL_6;
2132 channel->dpll_phase = DPLL_PHASE_6;
2133 channel->dpll_ctrl_n = DPLL_CTRL_6;
2134 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_6;
2137 channel->dpll_freq = DPLL_FREQ_7;
2138 channel->dpll_n = DPLL_7;
2139 channel->hw_dpll_n = HW_DPLL_7;
2140 channel->dpll_phase = DPLL_PHASE_7;
2141 channel->dpll_ctrl_n = DPLL_CTRL_7;
2142 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_7;
2152 * Compensate for the PTP DCO input-to-output delay.
2153 * This delay is 18 FOD cycles.
2155 static u32 idtcm_get_dco_delay(struct idtcm_channel *channel)
2157 struct idtcm *idtcm = channel->idtcm;
2165 err = idtcm_read(idtcm, channel->dpll_ctrl_n,
2166 DPLL_CTRL_DPLL_FOD_FREQ, mbuf, 6);
2170 err = idtcm_read(idtcm, channel->dpll_ctrl_n,
2171 DPLL_CTRL_DPLL_FOD_FREQ + 6, nbuf, 2);
2175 m = get_unaligned_le64(mbuf);
2176 n = get_unaligned_le16(nbuf);
2181 fodFreq = (u32)div_u64(m, n);
2183 if (fodFreq >= 500000000)
2184 return (u32)div_u64(18 * (u64)NSEC_PER_SEC, fodFreq);
2189 static int configure_channel_tod(struct idtcm_channel *channel, u32 index)
2191 enum fw_version fw_ver = channel->idtcm->fw_ver;
2193 /* Set tod addresses */
2196 channel->tod_read_primary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_PRIMARY_0);
2197 channel->tod_read_secondary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_SECONDARY_0);
2198 channel->tod_write = IDTCM_FW_REG(fw_ver, V520, TOD_WRITE_0);
2199 channel->tod_n = IDTCM_FW_REG(fw_ver, V520, TOD_0);
2200 channel->sync_src = SYNC_SOURCE_DPLL0_TOD_PPS;
2203 channel->tod_read_primary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_PRIMARY_1);
2204 channel->tod_read_secondary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_SECONDARY_1);
2205 channel->tod_write = IDTCM_FW_REG(fw_ver, V520, TOD_WRITE_1);
2206 channel->tod_n = IDTCM_FW_REG(fw_ver, V520, TOD_1);
2207 channel->sync_src = SYNC_SOURCE_DPLL1_TOD_PPS;
2210 channel->tod_read_primary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_PRIMARY_2);
2211 channel->tod_read_secondary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_SECONDARY_2);
2212 channel->tod_write = IDTCM_FW_REG(fw_ver, V520, TOD_WRITE_2);
2213 channel->tod_n = IDTCM_FW_REG(fw_ver, V520, TOD_2);
2214 channel->sync_src = SYNC_SOURCE_DPLL2_TOD_PPS;
2217 channel->tod_read_primary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_PRIMARY_3);
2218 channel->tod_read_secondary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_SECONDARY_3);
2219 channel->tod_write = IDTCM_FW_REG(fw_ver, V520, TOD_WRITE_3);
2220 channel->tod_n = IDTCM_FW_REG(fw_ver, V520, TOD_3);
2221 channel->sync_src = SYNC_SOURCE_DPLL3_TOD_PPS;
2230 static int idtcm_enable_channel(struct idtcm *idtcm, u32 index)
2232 struct idtcm_channel *channel;
2236 if (!(index < MAX_TOD))
2239 channel = &idtcm->channel[index];
2241 channel->idtcm = idtcm;
2242 channel->current_freq_scaled_ppm = 0;
2244 /* Set pll addresses */
2245 err = configure_channel_pll(channel);
2249 /* Set tod addresses */
2250 err = configure_channel_tod(channel, index);
2254 if (idtcm->fw_ver < V487)
2255 channel->caps = idtcm_caps_deprecated;
2257 channel->caps = idtcm_caps;
2259 snprintf(channel->caps.name, sizeof(channel->caps.name),
2260 "IDT CM TOD%u", index);
2262 channel->caps.pin_config = pin_config[index];
2264 for (i = 0; i < channel->caps.n_pins; ++i) {
2265 struct ptp_pin_desc *ppd = &channel->caps.pin_config[i];
2267 snprintf(ppd->name, sizeof(ppd->name), "input_ref%d", i);
2269 ppd->func = PTP_PF_NONE;
2273 err = initialize_dco_operating_mode(channel);
2277 err = idtcm_enable_tod(channel);
2280 "Failed at line %d in %s!", __LINE__, __func__);
2284 channel->dco_delay = idtcm_get_dco_delay(channel);
2286 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
2288 if (IS_ERR(channel->ptp_clock)) {
2289 err = PTR_ERR(channel->ptp_clock);
2290 channel->ptp_clock = NULL;
2294 if (!channel->ptp_clock)
2297 dev_info(idtcm->dev, "PLL%d registered as ptp%d",
2298 index, channel->ptp_clock->index);
2303 static int idtcm_enable_extts_channel(struct idtcm *idtcm, u32 index)
2305 struct idtcm_channel *channel;
2308 if (!(index < MAX_TOD))
2311 channel = &idtcm->channel[index];
2312 channel->idtcm = idtcm;
2314 /* Set tod addresses */
2315 err = configure_channel_tod(channel, index);
2319 channel->idtcm = idtcm;
2324 static void idtcm_extts_check(struct work_struct *work)
2326 struct idtcm *idtcm = container_of(work, struct idtcm, extts_work.work);
2327 struct idtcm_channel *channel;
2332 if (idtcm->extts_mask == 0)
2335 mutex_lock(idtcm->lock);
2337 for (i = 0; i < MAX_TOD; i++) {
2340 if ((idtcm->extts_mask & mask) == 0)
2343 err = idtcm_extts_check_channel(idtcm, i);
2346 /* trigger clears itself, so clear the mask */
2347 if (idtcm->extts_single_shot) {
2348 idtcm->extts_mask &= ~mask;
2351 channel = &idtcm->channel[i];
2352 arm_tod_read_trig_sel_refclk(channel, channel->refn);
2357 if (idtcm->extts_mask)
2358 schedule_delayed_work(&idtcm->extts_work,
2359 msecs_to_jiffies(EXTTS_PERIOD_MS));
2361 mutex_unlock(idtcm->lock);
2364 static void ptp_clock_unregister_all(struct idtcm *idtcm)
2367 struct idtcm_channel *channel;
2369 for (i = 0; i < MAX_TOD; i++) {
2370 channel = &idtcm->channel[i];
2371 if (channel->ptp_clock)
2372 ptp_clock_unregister(channel->ptp_clock);
2376 static void set_default_masks(struct idtcm *idtcm)
2378 idtcm->tod_mask = DEFAULT_TOD_MASK;
2379 idtcm->extts_mask = 0;
2381 idtcm->channel[0].tod = 0;
2382 idtcm->channel[1].tod = 1;
2383 idtcm->channel[2].tod = 2;
2384 idtcm->channel[3].tod = 3;
2386 idtcm->channel[0].pll = DEFAULT_TOD0_PTP_PLL;
2387 idtcm->channel[1].pll = DEFAULT_TOD1_PTP_PLL;
2388 idtcm->channel[2].pll = DEFAULT_TOD2_PTP_PLL;
2389 idtcm->channel[3].pll = DEFAULT_TOD3_PTP_PLL;
2391 idtcm->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
2392 idtcm->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
2393 idtcm->channel[2].output_mask = DEFAULT_OUTPUT_MASK_PLL2;
2394 idtcm->channel[3].output_mask = DEFAULT_OUTPUT_MASK_PLL3;
2397 static int idtcm_probe(struct platform_device *pdev)
2399 struct rsmu_ddata *ddata = dev_get_drvdata(pdev->dev.parent);
2400 struct idtcm *idtcm;
2404 idtcm = devm_kzalloc(&pdev->dev, sizeof(struct idtcm), GFP_KERNEL);
2409 idtcm->dev = &pdev->dev;
2410 idtcm->mfd = pdev->dev.parent;
2411 idtcm->lock = &ddata->lock;
2412 idtcm->regmap = ddata->regmap;
2413 idtcm->calculate_overhead_flag = 0;
2415 INIT_DELAYED_WORK(&idtcm->extts_work, idtcm_extts_check);
2417 set_default_masks(idtcm);
2419 mutex_lock(idtcm->lock);
2421 idtcm_set_version_info(idtcm);
2423 err = idtcm_load_firmware(idtcm, &pdev->dev);
2426 dev_warn(idtcm->dev, "loading firmware failed with %d", err);
2428 wait_for_chip_ready(idtcm);
2430 if (idtcm->tod_mask) {
2431 for (i = 0; i < MAX_TOD; i++) {
2432 if (idtcm->tod_mask & (1 << i))
2433 err = idtcm_enable_channel(idtcm, i);
2435 err = idtcm_enable_extts_channel(idtcm, i);
2438 "idtcm_enable_channel %d failed!", i);
2444 "no PLLs flagged as PHCs, nothing to do");
2448 mutex_unlock(idtcm->lock);
2451 ptp_clock_unregister_all(idtcm);
2455 platform_set_drvdata(pdev, idtcm);
2460 static int idtcm_remove(struct platform_device *pdev)
2462 struct idtcm *idtcm = platform_get_drvdata(pdev);
2464 idtcm->extts_mask = 0;
2465 ptp_clock_unregister_all(idtcm);
2466 cancel_delayed_work_sync(&idtcm->extts_work);
2471 static struct platform_driver idtcm_driver = {
2473 .name = "8a3400x-phc",
2475 .probe = idtcm_probe,
2476 .remove = idtcm_remove,
2479 module_platform_driver(idtcm_driver);