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 _idtcm_set_scsr_read_trig(struct idtcm_channel *channel,
243 enum scsr_read_trig_sel trig, u8 ref)
245 struct idtcm *idtcm = channel->idtcm;
246 u16 tod_read_cmd = IDTCM_FW_REG(idtcm->fw_ver, V520, TOD_READ_PRIMARY_CMD);
250 if (trig == SCSR_TOD_READ_TRIG_SEL_REFCLK) {
251 err = idtcm_read(idtcm, channel->tod_read_primary,
252 TOD_READ_PRIMARY_SEL_CFG_0, &val, sizeof(val));
256 val &= ~(WR_REF_INDEX_MASK << WR_REF_INDEX_SHIFT);
257 val |= (ref << WR_REF_INDEX_SHIFT);
259 err = idtcm_write(idtcm, channel->tod_read_primary,
260 TOD_READ_PRIMARY_SEL_CFG_0, &val, sizeof(val));
265 err = idtcm_read(idtcm, channel->tod_read_primary,
266 tod_read_cmd, &val, sizeof(val));
270 val &= ~(TOD_READ_TRIGGER_MASK << TOD_READ_TRIGGER_SHIFT);
271 val |= (trig << TOD_READ_TRIGGER_SHIFT);
272 val &= ~TOD_READ_TRIGGER_MODE; /* single shot */
274 err = idtcm_write(idtcm, channel->tod_read_primary,
275 tod_read_cmd, &val, sizeof(val));
279 static int idtcm_enable_extts(struct idtcm_channel *channel, u8 todn, u8 ref,
282 struct idtcm *idtcm = channel->idtcm;
283 u8 old_mask = idtcm->extts_mask;
291 if (ref > 0xF) /* E_REF_CLK15 */
293 if (idtcm->extts_mask & mask)
295 err = _idtcm_set_scsr_read_trig(&idtcm->channel[todn],
296 SCSR_TOD_READ_TRIG_SEL_REFCLK,
299 idtcm->extts_mask |= mask;
300 idtcm->event_channel[todn] = channel;
301 idtcm->channel[todn].refn = ref;
304 idtcm->extts_mask &= ~mask;
306 if (old_mask == 0 && idtcm->extts_mask)
307 schedule_delayed_work(&idtcm->extts_work,
308 msecs_to_jiffies(EXTTS_PERIOD_MS));
313 static int read_sys_apll_status(struct idtcm *idtcm, u8 *status)
315 return idtcm_read(idtcm, STATUS, DPLL_SYS_APLL_STATUS, status,
319 static int read_sys_dpll_status(struct idtcm *idtcm, u8 *status)
321 return idtcm_read(idtcm, STATUS, DPLL_SYS_STATUS, status, sizeof(u8));
324 static int wait_for_sys_apll_dpll_lock(struct idtcm *idtcm)
326 unsigned long timeout = jiffies + msecs_to_jiffies(LOCK_TIMEOUT_MS);
332 err = read_sys_apll_status(idtcm, &apll);
336 err = read_sys_dpll_status(idtcm, &dpll);
340 apll &= SYS_APLL_LOSS_LOCK_LIVE_MASK;
341 dpll &= DPLL_SYS_STATE_MASK;
343 if (apll == SYS_APLL_LOSS_LOCK_LIVE_LOCKED &&
344 dpll == DPLL_STATE_LOCKED) {
346 } else if (dpll == DPLL_STATE_FREERUN ||
347 dpll == DPLL_STATE_HOLDOVER ||
348 dpll == DPLL_STATE_OPEN_LOOP) {
350 "No wait state: DPLL_SYS_STATE %d", dpll);
354 msleep(LOCK_POLL_INTERVAL_MS);
355 } while (time_is_after_jiffies(timeout));
358 "%d ms lock timeout: SYS APLL Loss Lock %d SYS DPLL state %d",
359 LOCK_TIMEOUT_MS, apll, dpll);
364 static void wait_for_chip_ready(struct idtcm *idtcm)
366 if (wait_for_boot_status_ready(idtcm))
367 dev_warn(idtcm->dev, "BOOT_STATUS != 0xA0");
369 if (wait_for_sys_apll_dpll_lock(idtcm))
371 "Continuing while SYS APLL/DPLL is not locked");
374 static int _idtcm_gettime(struct idtcm_channel *channel,
375 struct timespec64 *ts, u8 timeout)
377 struct idtcm *idtcm = channel->idtcm;
378 u16 tod_read_cmd = IDTCM_FW_REG(idtcm->fw_ver, V520, TOD_READ_PRIMARY_CMD);
379 u8 buf[TOD_BYTE_COUNT];
383 /* wait trigger to be 0 */
388 if (idtcm->calculate_overhead_flag)
389 idtcm->start_time = ktime_get_raw();
391 err = idtcm_read(idtcm, channel->tod_read_primary,
392 tod_read_cmd, &trigger,
396 } while (trigger & TOD_READ_TRIGGER_MASK);
398 err = idtcm_read(idtcm, channel->tod_read_primary,
399 TOD_READ_PRIMARY, buf, sizeof(buf));
403 err = char_array_to_timespec(buf, sizeof(buf), ts);
408 static int idtcm_extts_check_channel(struct idtcm *idtcm, u8 todn)
410 struct idtcm_channel *ptp_channel, *extts_channel;
411 struct ptp_clock_event event;
412 struct timespec64 ts;
416 extts_channel = &idtcm->channel[todn];
417 ptp_channel = idtcm->event_channel[todn];
418 if (extts_channel == ptp_channel)
419 dco_delay = ptp_channel->dco_delay;
421 err = _idtcm_gettime(extts_channel, &ts, 1);
423 event.type = PTP_CLOCK_EXTTS;
425 event.timestamp = timespec64_to_ns(&ts) - dco_delay;
426 ptp_clock_event(ptp_channel->ptp_clock, &event);
431 static u8 idtcm_enable_extts_mask(struct idtcm_channel *channel,
432 u8 extts_mask, bool enable)
434 struct idtcm *idtcm = channel->idtcm;
437 for (i = 0; i < MAX_TOD; i++) {
439 u8 refn = idtcm->channel[i].refn;
441 if (extts_mask & mask) {
442 /* check extts before disabling it */
443 if (enable == false) {
444 err = idtcm_extts_check_channel(idtcm, i);
445 /* trigger happened so we won't re-enable it */
449 (void)idtcm_enable_extts(channel, i, refn, enable);
456 static int _idtcm_gettime_immediate(struct idtcm_channel *channel,
457 struct timespec64 *ts)
459 struct idtcm *idtcm = channel->idtcm;
464 if (idtcm->extts_mask) {
465 extts_mask = idtcm_enable_extts_mask(channel, idtcm->extts_mask,
469 err = _idtcm_set_scsr_read_trig(channel,
470 SCSR_TOD_READ_TRIG_SEL_IMMEDIATE, 0);
472 err = _idtcm_gettime(channel, ts, 10);
474 /* Re-enable extts */
476 idtcm_enable_extts_mask(channel, extts_mask, true);
481 static int _sync_pll_output(struct idtcm *idtcm,
493 if (qn == 0 && qn_plus_1 == 0)
498 sync_ctrl0 = HW_Q0_Q1_CH_SYNC_CTRL_0;
499 sync_ctrl1 = HW_Q0_Q1_CH_SYNC_CTRL_1;
502 sync_ctrl0 = HW_Q2_Q3_CH_SYNC_CTRL_0;
503 sync_ctrl1 = HW_Q2_Q3_CH_SYNC_CTRL_1;
506 sync_ctrl0 = HW_Q4_Q5_CH_SYNC_CTRL_0;
507 sync_ctrl1 = HW_Q4_Q5_CH_SYNC_CTRL_1;
510 sync_ctrl0 = HW_Q6_Q7_CH_SYNC_CTRL_0;
511 sync_ctrl1 = HW_Q6_Q7_CH_SYNC_CTRL_1;
514 sync_ctrl0 = HW_Q8_CH_SYNC_CTRL_0;
515 sync_ctrl1 = HW_Q8_CH_SYNC_CTRL_1;
518 sync_ctrl0 = HW_Q9_CH_SYNC_CTRL_0;
519 sync_ctrl1 = HW_Q9_CH_SYNC_CTRL_1;
522 sync_ctrl0 = HW_Q10_CH_SYNC_CTRL_0;
523 sync_ctrl1 = HW_Q10_CH_SYNC_CTRL_1;
526 sync_ctrl0 = HW_Q11_CH_SYNC_CTRL_0;
527 sync_ctrl1 = HW_Q11_CH_SYNC_CTRL_1;
533 val = SYNCTRL1_MASTER_SYNC_RST;
535 /* Place master sync in reset */
536 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
540 err = idtcm_write(idtcm, 0, sync_ctrl0, &sync_src, sizeof(sync_src));
544 /* Set sync trigger mask */
545 val |= SYNCTRL1_FBDIV_FRAME_SYNC_TRIG | SYNCTRL1_FBDIV_SYNC_TRIG;
548 val |= SYNCTRL1_Q0_DIV_SYNC_TRIG;
551 val |= SYNCTRL1_Q1_DIV_SYNC_TRIG;
553 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
557 /* PLL5 can have OUT8 as second additional output. */
558 if (pll == 5 && qn_plus_1 != 0) {
559 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
560 &temp, sizeof(temp));
564 temp &= ~(Q9_TO_Q8_SYNC_TRIG);
566 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
567 &temp, sizeof(temp));
571 temp |= Q9_TO_Q8_SYNC_TRIG;
573 err = idtcm_write(idtcm, 0, HW_Q8_CTRL_SPARE,
574 &temp, sizeof(temp));
579 /* PLL6 can have OUT11 as second additional output. */
580 if (pll == 6 && qn_plus_1 != 0) {
581 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
582 &temp, sizeof(temp));
586 temp &= ~(Q10_TO_Q11_SYNC_TRIG);
588 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
589 &temp, sizeof(temp));
593 temp |= Q10_TO_Q11_SYNC_TRIG;
595 err = idtcm_write(idtcm, 0, HW_Q11_CTRL_SPARE,
596 &temp, sizeof(temp));
601 /* Place master sync out of reset */
602 val &= ~(SYNCTRL1_MASTER_SYNC_RST);
603 err = idtcm_write(idtcm, 0, sync_ctrl1, &val, sizeof(val));
608 static int idtcm_sync_pps_output(struct idtcm_channel *channel)
610 struct idtcm *idtcm = channel->idtcm;
618 u16 output_mask = channel->output_mask;
620 err = idtcm_read(idtcm, 0, HW_Q8_CTRL_SPARE,
621 &temp, sizeof(temp));
625 if ((temp & Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
626 Q9_TO_Q8_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
629 err = idtcm_read(idtcm, 0, HW_Q11_CTRL_SPARE,
630 &temp, sizeof(temp));
634 if ((temp & Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK) ==
635 Q10_TO_Q11_FANOUT_AND_CLOCK_SYNC_ENABLE_MASK)
638 for (pll = 0; pll < 8; pll++) {
643 /* First 4 pll has 2 outputs */
644 qn = output_mask & 0x1;
645 output_mask = output_mask >> 1;
646 qn_plus_1 = output_mask & 0x1;
647 output_mask = output_mask >> 1;
648 } else if (pll == 4) {
650 qn = output_mask & 0x1;
651 output_mask = output_mask >> 1;
653 } else if (pll == 5) {
655 qn_plus_1 = output_mask & 0x1;
656 output_mask = output_mask >> 1;
658 qn = output_mask & 0x1;
659 output_mask = output_mask >> 1;
660 } else if (pll == 6) {
661 qn = output_mask & 0x1;
662 output_mask = output_mask >> 1;
664 qn_plus_1 = output_mask & 0x1;
665 output_mask = output_mask >> 1;
667 } else if (pll == 7) {
668 if (out11_mux == 0) {
669 qn = output_mask & 0x1;
670 output_mask = output_mask >> 1;
674 if (qn != 0 || qn_plus_1 != 0)
675 err = _sync_pll_output(idtcm, pll, channel->sync_src,
685 static int _idtcm_set_dpll_hw_tod(struct idtcm_channel *channel,
686 struct timespec64 const *ts,
687 enum hw_tod_write_trig_sel wr_trig)
689 struct idtcm *idtcm = channel->idtcm;
690 u8 buf[TOD_BYTE_COUNT];
693 struct timespec64 local_ts = *ts;
694 s64 total_overhead_ns;
696 /* Configure HW TOD write trigger. */
697 err = idtcm_read(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
703 cmd |= wr_trig | 0x08;
705 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
710 if (wr_trig != HW_TOD_WR_TRIG_SEL_MSB) {
711 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
715 err = idtcm_write(idtcm, channel->hw_dpll_n,
716 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
721 /* ARM HW TOD write trigger. */
724 err = idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_CTRL_1,
727 if (wr_trig == HW_TOD_WR_TRIG_SEL_MSB) {
728 if (idtcm->calculate_overhead_flag) {
729 /* Assumption: I2C @ 400KHz */
730 ktime_t diff = ktime_sub(ktime_get_raw(),
732 total_overhead_ns = ktime_to_ns(diff)
733 + idtcm->tod_write_overhead_ns
734 + SETTIME_CORRECTION;
736 timespec64_add_ns(&local_ts, total_overhead_ns);
738 idtcm->calculate_overhead_flag = 0;
741 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
745 err = idtcm_write(idtcm, channel->hw_dpll_n,
746 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
752 static int _idtcm_set_dpll_scsr_tod(struct idtcm_channel *channel,
753 struct timespec64 const *ts,
754 enum scsr_tod_write_trig_sel wr_trig,
755 enum scsr_tod_write_type_sel wr_type)
757 struct idtcm *idtcm = channel->idtcm;
758 unsigned char buf[TOD_BYTE_COUNT], cmd;
759 struct timespec64 local_ts = *ts;
762 timespec64_add_ns(&local_ts, SETTIME_CORRECTION);
764 err = timespec_to_char_array(&local_ts, buf, sizeof(buf));
768 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE,
773 /* Trigger the write operation. */
774 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
779 cmd &= ~(TOD_WRITE_SELECTION_MASK << TOD_WRITE_SELECTION_SHIFT);
780 cmd &= ~(TOD_WRITE_TYPE_MASK << TOD_WRITE_TYPE_SHIFT);
781 cmd |= (wr_trig << TOD_WRITE_SELECTION_SHIFT);
782 cmd |= (wr_type << TOD_WRITE_TYPE_SHIFT);
784 err = idtcm_write(idtcm, channel->tod_write, TOD_WRITE_CMD,
789 /* Wait for the operation to complete. */
791 /* pps trigger takes up to 1 sec to complete */
792 if (wr_trig == SCSR_TOD_WR_TRIG_SEL_TODPPS)
795 err = idtcm_read(idtcm, channel->tod_write, TOD_WRITE_CMD,
800 if ((cmd & TOD_WRITE_SELECTION_MASK) == 0)
805 "Timed out waiting for the write counter");
813 static int get_output_base_addr(enum fw_version ver, u8 outn)
819 base = IDTCM_FW_REG(ver, V520, OUTPUT_0);
822 base = IDTCM_FW_REG(ver, V520, OUTPUT_1);
825 base = IDTCM_FW_REG(ver, V520, OUTPUT_2);
828 base = IDTCM_FW_REG(ver, V520, OUTPUT_3);
831 base = IDTCM_FW_REG(ver, V520, OUTPUT_4);
834 base = IDTCM_FW_REG(ver, V520, OUTPUT_5);
837 base = IDTCM_FW_REG(ver, V520, OUTPUT_6);
840 base = IDTCM_FW_REG(ver, V520, OUTPUT_7);
843 base = IDTCM_FW_REG(ver, V520, OUTPUT_8);
846 base = IDTCM_FW_REG(ver, V520, OUTPUT_9);
849 base = IDTCM_FW_REG(ver, V520, OUTPUT_10);
852 base = IDTCM_FW_REG(ver, V520, OUTPUT_11);
861 static int _idtcm_settime_deprecated(struct idtcm_channel *channel,
862 struct timespec64 const *ts)
864 struct idtcm *idtcm = channel->idtcm;
867 err = _idtcm_set_dpll_hw_tod(channel, ts, HW_TOD_WR_TRIG_SEL_MSB);
870 "%s: Set HW ToD failed", __func__);
874 return idtcm_sync_pps_output(channel);
877 static int _idtcm_settime(struct idtcm_channel *channel,
878 struct timespec64 const *ts,
879 enum scsr_tod_write_type_sel wr_type)
881 return _idtcm_set_dpll_scsr_tod(channel, ts,
882 SCSR_TOD_WR_TRIG_SEL_IMMEDIATE,
886 static int idtcm_set_phase_pull_in_offset(struct idtcm_channel *channel,
891 struct idtcm *idtcm = channel->idtcm;
894 for (i = 0; i < 4; i++) {
895 buf[i] = 0xff & (offset_ns);
899 err = idtcm_write(idtcm, channel->dpll_phase_pull_in, PULL_IN_OFFSET,
905 static int idtcm_set_phase_pull_in_slope_limit(struct idtcm_channel *channel,
910 struct idtcm *idtcm = channel->idtcm;
913 if (max_ffo_ppb & 0xff000000)
916 for (i = 0; i < 3; i++) {
917 buf[i] = 0xff & (max_ffo_ppb);
921 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
922 PULL_IN_SLOPE_LIMIT, buf, sizeof(buf));
927 static int idtcm_start_phase_pull_in(struct idtcm_channel *channel)
930 struct idtcm *idtcm = channel->idtcm;
933 err = idtcm_read(idtcm, channel->dpll_phase_pull_in, PULL_IN_CTRL,
940 err = idtcm_write(idtcm, channel->dpll_phase_pull_in,
941 PULL_IN_CTRL, &buf, sizeof(buf));
949 static int do_phase_pull_in_fw(struct idtcm_channel *channel,
955 err = idtcm_set_phase_pull_in_offset(channel, -offset_ns);
959 err = idtcm_set_phase_pull_in_slope_limit(channel, max_ffo_ppb);
963 err = idtcm_start_phase_pull_in(channel);
968 static int set_tod_write_overhead(struct idtcm_channel *channel)
970 struct idtcm *idtcm = channel->idtcm;
979 char buf[TOD_BYTE_COUNT] = {0};
981 /* Set page offset */
982 idtcm_write(idtcm, channel->hw_dpll_n, HW_DPLL_TOD_OVR__0,
985 for (i = 0; i < TOD_WRITE_OVERHEAD_COUNT_MAX; i++) {
986 start = ktime_get_raw();
988 err = idtcm_write(idtcm, channel->hw_dpll_n,
989 HW_DPLL_TOD_OVR__0, buf, sizeof(buf));
993 stop = ktime_get_raw();
995 diff = ktime_sub(stop, start);
997 current_ns = ktime_to_ns(diff);
1000 lowest_ns = current_ns;
1002 if (current_ns < lowest_ns)
1003 lowest_ns = current_ns;
1007 idtcm->tod_write_overhead_ns = lowest_ns;
1012 static int _idtcm_adjtime_deprecated(struct idtcm_channel *channel, s64 delta)
1015 struct idtcm *idtcm = channel->idtcm;
1016 struct timespec64 ts;
1019 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS_DEPRECATED) {
1020 err = channel->do_phase_pull_in(channel, delta, 0);
1022 idtcm->calculate_overhead_flag = 1;
1024 err = set_tod_write_overhead(channel);
1028 err = _idtcm_gettime_immediate(channel, &ts);
1032 now = timespec64_to_ns(&ts);
1035 ts = ns_to_timespec64(now);
1037 err = _idtcm_settime_deprecated(channel, &ts);
1043 static int idtcm_state_machine_reset(struct idtcm *idtcm)
1045 u8 byte = SM_RESET_CMD;
1050 clear_boot_status(idtcm);
1052 err = idtcm_write(idtcm, RESET_CTRL,
1053 IDTCM_FW_REG(idtcm->fw_ver, V520, SM_RESET),
1054 &byte, sizeof(byte));
1057 for (i = 0; i < 30; i++) {
1058 msleep_interruptible(100);
1059 read_boot_status(idtcm, &status);
1061 if (status == 0xA0) {
1063 "SM_RESET completed in %d ms", i * 100);
1070 "Timed out waiting for CM_RESET to complete");
1076 static int idtcm_read_hw_rev_id(struct idtcm *idtcm, u8 *hw_rev_id)
1078 return idtcm_read(idtcm, HW_REVISION, REV_ID, hw_rev_id, sizeof(u8));
1081 static int idtcm_read_product_id(struct idtcm *idtcm, u16 *product_id)
1086 err = idtcm_read(idtcm, GENERAL_STATUS, PRODUCT_ID, buf, sizeof(buf));
1088 *product_id = (buf[1] << 8) | buf[0];
1093 static int idtcm_read_major_release(struct idtcm *idtcm, u8 *major)
1098 err = idtcm_read(idtcm, GENERAL_STATUS, MAJ_REL, &buf, sizeof(buf));
1105 static int idtcm_read_minor_release(struct idtcm *idtcm, u8 *minor)
1107 return idtcm_read(idtcm, GENERAL_STATUS, MIN_REL, minor, sizeof(u8));
1110 static int idtcm_read_hotfix_release(struct idtcm *idtcm, u8 *hotfix)
1112 return idtcm_read(idtcm,
1119 static int idtcm_read_otp_scsr_config_select(struct idtcm *idtcm,
1122 return idtcm_read(idtcm, GENERAL_STATUS, OTP_SCSR_CONFIG_SELECT,
1123 config_select, sizeof(u8));
1126 static int set_pll_output_mask(struct idtcm *idtcm, u16 addr, u8 val)
1131 case TOD0_OUT_ALIGN_MASK_ADDR:
1132 SET_U16_LSB(idtcm->channel[0].output_mask, val);
1134 case TOD0_OUT_ALIGN_MASK_ADDR + 1:
1135 SET_U16_MSB(idtcm->channel[0].output_mask, val);
1137 case TOD1_OUT_ALIGN_MASK_ADDR:
1138 SET_U16_LSB(idtcm->channel[1].output_mask, val);
1140 case TOD1_OUT_ALIGN_MASK_ADDR + 1:
1141 SET_U16_MSB(idtcm->channel[1].output_mask, val);
1143 case TOD2_OUT_ALIGN_MASK_ADDR:
1144 SET_U16_LSB(idtcm->channel[2].output_mask, val);
1146 case TOD2_OUT_ALIGN_MASK_ADDR + 1:
1147 SET_U16_MSB(idtcm->channel[2].output_mask, val);
1149 case TOD3_OUT_ALIGN_MASK_ADDR:
1150 SET_U16_LSB(idtcm->channel[3].output_mask, val);
1152 case TOD3_OUT_ALIGN_MASK_ADDR + 1:
1153 SET_U16_MSB(idtcm->channel[3].output_mask, val);
1156 err = -EFAULT; /* Bad address */;
1163 static int set_tod_ptp_pll(struct idtcm *idtcm, u8 index, u8 pll)
1165 if (index >= MAX_TOD) {
1166 dev_err(idtcm->dev, "ToD%d not supported", index);
1170 if (pll >= MAX_PLL) {
1171 dev_err(idtcm->dev, "Pll%d not supported", pll);
1175 idtcm->channel[index].pll = pll;
1180 static int check_and_set_masks(struct idtcm *idtcm,
1188 if ((val & 0xf0) || !(val & 0x0f)) {
1189 dev_err(idtcm->dev, "Invalid TOD mask 0x%02x", val);
1192 idtcm->tod_mask = val;
1195 case TOD0_PTP_PLL_ADDR:
1196 err = set_tod_ptp_pll(idtcm, 0, val);
1198 case TOD1_PTP_PLL_ADDR:
1199 err = set_tod_ptp_pll(idtcm, 1, val);
1201 case TOD2_PTP_PLL_ADDR:
1202 err = set_tod_ptp_pll(idtcm, 2, val);
1204 case TOD3_PTP_PLL_ADDR:
1205 err = set_tod_ptp_pll(idtcm, 3, val);
1208 err = set_pll_output_mask(idtcm, regaddr, val);
1215 static void display_pll_and_masks(struct idtcm *idtcm)
1220 dev_dbg(idtcm->dev, "tod_mask = 0x%02x", idtcm->tod_mask);
1222 for (i = 0; i < MAX_TOD; i++) {
1225 if (mask & idtcm->tod_mask)
1227 "TOD%d pll = %d output_mask = 0x%04x",
1228 i, idtcm->channel[i].pll,
1229 idtcm->channel[i].output_mask);
1233 static int idtcm_load_firmware(struct idtcm *idtcm,
1236 u16 scratch = IDTCM_FW_REG(idtcm->fw_ver, V520, SCRATCH);
1237 char fname[128] = FW_FILENAME;
1238 const struct firmware *fw;
1239 struct idtcm_fwrc *rec;
1246 if (firmware) /* module parameter */
1247 snprintf(fname, sizeof(fname), "%s", firmware);
1249 dev_info(idtcm->dev, "requesting firmware '%s'", fname);
1251 err = request_firmware(&fw, fname, dev);
1254 "Failed at line %d in %s!", __LINE__, __func__);
1258 dev_dbg(idtcm->dev, "firmware size %zu bytes", fw->size);
1260 rec = (struct idtcm_fwrc *) fw->data;
1262 if (contains_full_configuration(idtcm, fw))
1263 idtcm_state_machine_reset(idtcm);
1265 for (len = fw->size; len > 0; len -= sizeof(*rec)) {
1266 if (rec->reserved) {
1268 "bad firmware, reserved field non-zero");
1271 regaddr = rec->hiaddr << 8;
1272 regaddr |= rec->loaddr;
1275 loaddr = rec->loaddr;
1279 err = check_and_set_masks(idtcm, regaddr, val);
1282 if (err != -EINVAL) {
1285 /* Top (status registers) and bottom are read-only */
1286 if (regaddr < GPIO_USER_CONTROL || regaddr >= scratch)
1289 /* Page size 128, last 4 bytes of page skipped */
1290 if ((loaddr > 0x7b && loaddr <= 0x7f) || loaddr > 0xfb)
1293 err = idtcm_write(idtcm, regaddr, 0, &val, sizeof(val));
1300 display_pll_and_masks(idtcm);
1303 release_firmware(fw);
1307 static int idtcm_output_enable(struct idtcm_channel *channel,
1308 bool enable, unsigned int outn)
1310 struct idtcm *idtcm = channel->idtcm;
1315 base = get_output_base_addr(idtcm->fw_ver, outn);
1319 "%s - Unsupported out%d", __func__, outn);
1323 err = idtcm_read(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1328 val |= SQUELCH_DISABLE;
1330 val &= ~SQUELCH_DISABLE;
1332 return idtcm_write(idtcm, (u16)base, OUT_CTRL_1, &val, sizeof(val));
1335 static int idtcm_output_mask_enable(struct idtcm_channel *channel,
1342 mask = channel->output_mask;
1347 err = idtcm_output_enable(channel, enable, outn);
1359 static int idtcm_perout_enable(struct idtcm_channel *channel,
1360 struct ptp_perout_request *perout,
1363 struct idtcm *idtcm = channel->idtcm;
1364 unsigned int flags = perout->flags;
1365 struct timespec64 ts = {0, 0};
1368 if (flags == PEROUT_ENABLE_OUTPUT_MASK)
1369 err = idtcm_output_mask_enable(channel, enable);
1371 err = idtcm_output_enable(channel, enable, perout->index);
1374 dev_err(idtcm->dev, "Unable to set output enable");
1378 /* Align output to internal 1 PPS */
1379 return _idtcm_settime(channel, &ts, SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS);
1382 static int idtcm_get_pll_mode(struct idtcm_channel *channel,
1383 enum pll_mode *mode)
1385 struct idtcm *idtcm = channel->idtcm;
1389 err = idtcm_read(idtcm, channel->dpll_n,
1390 IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_MODE),
1391 &dpll_mode, sizeof(dpll_mode));
1395 *mode = (dpll_mode >> PLL_MODE_SHIFT) & PLL_MODE_MASK;
1400 static int idtcm_set_pll_mode(struct idtcm_channel *channel,
1403 struct idtcm *idtcm = channel->idtcm;
1407 err = idtcm_read(idtcm, channel->dpll_n,
1408 IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_MODE),
1409 &dpll_mode, sizeof(dpll_mode));
1413 dpll_mode &= ~(PLL_MODE_MASK << PLL_MODE_SHIFT);
1415 dpll_mode |= (mode << PLL_MODE_SHIFT);
1417 err = idtcm_write(idtcm, channel->dpll_n,
1418 IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_MODE),
1419 &dpll_mode, sizeof(dpll_mode));
1423 static int idtcm_get_manual_reference(struct idtcm_channel *channel,
1424 enum manual_reference *ref)
1426 struct idtcm *idtcm = channel->idtcm;
1427 u8 dpll_manu_ref_cfg;
1430 err = idtcm_read(idtcm, channel->dpll_ctrl_n,
1431 DPLL_CTRL_DPLL_MANU_REF_CFG,
1432 &dpll_manu_ref_cfg, sizeof(dpll_manu_ref_cfg));
1436 dpll_manu_ref_cfg &= (MANUAL_REFERENCE_MASK << MANUAL_REFERENCE_SHIFT);
1438 *ref = dpll_manu_ref_cfg >> MANUAL_REFERENCE_SHIFT;
1443 static int idtcm_set_manual_reference(struct idtcm_channel *channel,
1444 enum manual_reference ref)
1446 struct idtcm *idtcm = channel->idtcm;
1447 u8 dpll_manu_ref_cfg;
1450 err = idtcm_read(idtcm, channel->dpll_ctrl_n,
1451 DPLL_CTRL_DPLL_MANU_REF_CFG,
1452 &dpll_manu_ref_cfg, sizeof(dpll_manu_ref_cfg));
1456 dpll_manu_ref_cfg &= ~(MANUAL_REFERENCE_MASK << MANUAL_REFERENCE_SHIFT);
1458 dpll_manu_ref_cfg |= (ref << MANUAL_REFERENCE_SHIFT);
1460 err = idtcm_write(idtcm, channel->dpll_ctrl_n,
1461 DPLL_CTRL_DPLL_MANU_REF_CFG,
1462 &dpll_manu_ref_cfg, sizeof(dpll_manu_ref_cfg));
1467 static int configure_dpll_mode_write_frequency(struct idtcm_channel *channel)
1469 struct idtcm *idtcm = channel->idtcm;
1472 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_FREQUENCY);
1475 dev_err(idtcm->dev, "Failed to set pll mode to write frequency");
1477 channel->mode = PTP_PLL_MODE_WRITE_FREQUENCY;
1482 static int configure_dpll_mode_write_phase(struct idtcm_channel *channel)
1484 struct idtcm *idtcm = channel->idtcm;
1487 err = idtcm_set_pll_mode(channel, PLL_MODE_WRITE_PHASE);
1490 dev_err(idtcm->dev, "Failed to set pll mode to write phase");
1492 channel->mode = PTP_PLL_MODE_WRITE_PHASE;
1497 static int configure_manual_reference_write_frequency(struct idtcm_channel *channel)
1499 struct idtcm *idtcm = channel->idtcm;
1502 err = idtcm_set_manual_reference(channel, MANU_REF_WRITE_FREQUENCY);
1505 dev_err(idtcm->dev, "Failed to set manual reference to write frequency");
1507 channel->mode = PTP_PLL_MODE_WRITE_FREQUENCY;
1512 static int configure_manual_reference_write_phase(struct idtcm_channel *channel)
1514 struct idtcm *idtcm = channel->idtcm;
1517 err = idtcm_set_manual_reference(channel, MANU_REF_WRITE_PHASE);
1520 dev_err(idtcm->dev, "Failed to set manual reference to write phase");
1522 channel->mode = PTP_PLL_MODE_WRITE_PHASE;
1527 static int idtcm_stop_phase_pull_in(struct idtcm_channel *channel)
1531 err = _idtcm_adjfine(channel, channel->current_freq_scaled_ppm);
1535 channel->phase_pull_in = false;
1540 static long idtcm_work_handler(struct ptp_clock_info *ptp)
1542 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1543 struct idtcm *idtcm = channel->idtcm;
1545 mutex_lock(idtcm->lock);
1547 (void)idtcm_stop_phase_pull_in(channel);
1549 mutex_unlock(idtcm->lock);
1551 /* Return a negative value here to not reschedule */
1555 static s32 phase_pull_in_scaled_ppm(s32 current_ppm, s32 phase_pull_in_ppb)
1557 /* ppb = scaled_ppm * 125 / 2^13 */
1558 /* scaled_ppm = ppb * 2^13 / 125 */
1560 s64 max_scaled_ppm = div_s64((s64)PHASE_PULL_IN_MAX_PPB << 13, 125);
1561 s64 scaled_ppm = div_s64((s64)phase_pull_in_ppb << 13, 125);
1563 current_ppm += scaled_ppm;
1565 if (current_ppm > max_scaled_ppm)
1566 current_ppm = max_scaled_ppm;
1567 else if (current_ppm < -max_scaled_ppm)
1568 current_ppm = -max_scaled_ppm;
1573 static int do_phase_pull_in_sw(struct idtcm_channel *channel,
1577 s32 current_ppm = channel->current_freq_scaled_ppm;
1578 u32 duration_ms = MSEC_PER_SEC;
1583 /* If the ToD correction is less than PHASE_PULL_IN_MIN_THRESHOLD_NS,
1584 * skip. The error introduced by the ToD adjustment procedure would
1585 * be bigger than the required ToD correction
1587 if (abs(delta_ns) < PHASE_PULL_IN_MIN_THRESHOLD_NS)
1590 if (max_ffo_ppb == 0)
1591 max_ffo_ppb = PHASE_PULL_IN_MAX_PPB;
1593 /* For most cases, keep phase pull-in duration 1 second */
1595 while (abs(ppb) > max_ffo_ppb) {
1600 delta_ppm = phase_pull_in_scaled_ppm(current_ppm, ppb);
1602 err = _idtcm_adjfine(channel, delta_ppm);
1607 /* schedule the worker to cancel phase pull-in */
1608 ptp_schedule_worker(channel->ptp_clock,
1609 msecs_to_jiffies(duration_ms) - 1);
1611 channel->phase_pull_in = true;
1616 static int initialize_operating_mode_with_manual_reference(struct idtcm_channel *channel,
1617 enum manual_reference ref)
1619 struct idtcm *idtcm = channel->idtcm;
1621 channel->mode = PTP_PLL_MODE_UNSUPPORTED;
1622 channel->configure_write_frequency = configure_manual_reference_write_frequency;
1623 channel->configure_write_phase = configure_manual_reference_write_phase;
1624 channel->do_phase_pull_in = do_phase_pull_in_sw;
1627 case MANU_REF_WRITE_PHASE:
1628 channel->mode = PTP_PLL_MODE_WRITE_PHASE;
1630 case MANU_REF_WRITE_FREQUENCY:
1631 channel->mode = PTP_PLL_MODE_WRITE_FREQUENCY;
1634 dev_warn(idtcm->dev,
1635 "Unsupported MANUAL_REFERENCE: 0x%02x", ref);
1641 static int initialize_operating_mode_with_pll_mode(struct idtcm_channel *channel,
1644 struct idtcm *idtcm = channel->idtcm;
1647 channel->mode = PTP_PLL_MODE_UNSUPPORTED;
1648 channel->configure_write_frequency = configure_dpll_mode_write_frequency;
1649 channel->configure_write_phase = configure_dpll_mode_write_phase;
1650 channel->do_phase_pull_in = do_phase_pull_in_fw;
1653 case PLL_MODE_WRITE_PHASE:
1654 channel->mode = PTP_PLL_MODE_WRITE_PHASE;
1656 case PLL_MODE_WRITE_FREQUENCY:
1657 channel->mode = PTP_PLL_MODE_WRITE_FREQUENCY;
1661 "Unsupported PLL_MODE: 0x%02x", mode);
1668 static int initialize_dco_operating_mode(struct idtcm_channel *channel)
1670 enum manual_reference ref = MANU_REF_XO_DPLL;
1671 enum pll_mode mode = PLL_MODE_DISABLED;
1672 struct idtcm *idtcm = channel->idtcm;
1675 channel->mode = PTP_PLL_MODE_UNSUPPORTED;
1677 err = idtcm_get_pll_mode(channel, &mode);
1679 dev_err(idtcm->dev, "Unable to read pll mode!");
1683 if (mode == PLL_MODE_PLL) {
1684 err = idtcm_get_manual_reference(channel, &ref);
1686 dev_err(idtcm->dev, "Unable to read manual reference!");
1689 err = initialize_operating_mode_with_manual_reference(channel, ref);
1691 err = initialize_operating_mode_with_pll_mode(channel, mode);
1694 if (channel->mode == PTP_PLL_MODE_WRITE_PHASE)
1695 channel->configure_write_frequency(channel);
1700 /* PTP Hardware Clock interface */
1703 * Maximum absolute value for write phase offset in picoseconds
1705 * Destination signed register is 32-bit register in resolution of 50ps
1707 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350
1709 static int _idtcm_adjphase(struct idtcm_channel *channel, s32 delta_ns)
1711 struct idtcm *idtcm = channel->idtcm;
1718 if (channel->mode != PTP_PLL_MODE_WRITE_PHASE) {
1719 err = channel->configure_write_phase(channel);
1724 offset_ps = (s64)delta_ns * 1000;
1727 * Check for 32-bit signed max * 50:
1729 * 0x7fffffff * 50 = 2147483647 * 50 = 107374182350
1731 if (offset_ps > MAX_ABS_WRITE_PHASE_PICOSECONDS)
1732 offset_ps = MAX_ABS_WRITE_PHASE_PICOSECONDS;
1733 else if (offset_ps < -MAX_ABS_WRITE_PHASE_PICOSECONDS)
1734 offset_ps = -MAX_ABS_WRITE_PHASE_PICOSECONDS;
1736 phase_50ps = div_s64(offset_ps, 50);
1738 for (i = 0; i < 4; i++) {
1739 buf[i] = phase_50ps & 0xff;
1743 err = idtcm_write(idtcm, channel->dpll_phase, DPLL_WR_PHASE,
1749 static int _idtcm_adjfine(struct idtcm_channel *channel, long scaled_ppm)
1751 struct idtcm *idtcm = channel->idtcm;
1757 if (channel->mode != PTP_PLL_MODE_WRITE_FREQUENCY) {
1758 err = channel->configure_write_frequency(channel);
1764 * Frequency Control Word unit is: 1.11 * 10^-10 ppm
1773 * FCW = -------------
1777 /* 2 ^ -53 = 1.1102230246251565404236316680908e-16 */
1778 fcw = scaled_ppm * 244140625ULL;
1780 fcw = div_s64(fcw, 1776);
1782 for (i = 0; i < 6; i++) {
1783 buf[i] = fcw & 0xff;
1787 err = idtcm_write(idtcm, channel->dpll_freq, DPLL_WR_FREQ,
1793 static int idtcm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
1795 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1796 struct idtcm *idtcm = channel->idtcm;
1799 mutex_lock(idtcm->lock);
1800 err = _idtcm_gettime_immediate(channel, ts);
1801 mutex_unlock(idtcm->lock);
1804 dev_err(idtcm->dev, "Failed at line %d in %s!",
1805 __LINE__, __func__);
1810 static int idtcm_settime_deprecated(struct ptp_clock_info *ptp,
1811 const struct timespec64 *ts)
1813 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1814 struct idtcm *idtcm = channel->idtcm;
1817 mutex_lock(idtcm->lock);
1818 err = _idtcm_settime_deprecated(channel, ts);
1819 mutex_unlock(idtcm->lock);
1823 "Failed at line %d in %s!", __LINE__, __func__);
1828 static int idtcm_settime(struct ptp_clock_info *ptp,
1829 const struct timespec64 *ts)
1831 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1832 struct idtcm *idtcm = channel->idtcm;
1835 mutex_lock(idtcm->lock);
1836 err = _idtcm_settime(channel, ts, SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
1837 mutex_unlock(idtcm->lock);
1841 "Failed at line %d in %s!", __LINE__, __func__);
1846 static int idtcm_adjtime_deprecated(struct ptp_clock_info *ptp, s64 delta)
1848 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1849 struct idtcm *idtcm = channel->idtcm;
1852 mutex_lock(idtcm->lock);
1853 err = _idtcm_adjtime_deprecated(channel, delta);
1854 mutex_unlock(idtcm->lock);
1858 "Failed at line %d in %s!", __LINE__, __func__);
1863 static int idtcm_adjtime(struct ptp_clock_info *ptp, s64 delta)
1865 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1866 struct idtcm *idtcm = channel->idtcm;
1867 struct timespec64 ts;
1868 enum scsr_tod_write_type_sel type;
1871 if (channel->phase_pull_in == true)
1874 mutex_lock(idtcm->lock);
1876 if (abs(delta) < PHASE_PULL_IN_THRESHOLD_NS) {
1877 err = channel->do_phase_pull_in(channel, delta, 0);
1880 ts = ns_to_timespec64(delta);
1881 type = SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS;
1883 ts = ns_to_timespec64(-delta);
1884 type = SCSR_TOD_WR_TYPE_SEL_DELTA_MINUS;
1886 err = _idtcm_settime(channel, &ts, type);
1889 mutex_unlock(idtcm->lock);
1893 "Failed at line %d in %s!", __LINE__, __func__);
1898 static int idtcm_adjphase(struct ptp_clock_info *ptp, s32 delta)
1900 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1901 struct idtcm *idtcm = channel->idtcm;
1904 mutex_lock(idtcm->lock);
1905 err = _idtcm_adjphase(channel, delta);
1906 mutex_unlock(idtcm->lock);
1910 "Failed at line %d in %s!", __LINE__, __func__);
1915 static int idtcm_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
1917 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1918 struct idtcm *idtcm = channel->idtcm;
1921 if (channel->phase_pull_in == true)
1924 if (scaled_ppm == channel->current_freq_scaled_ppm)
1927 mutex_lock(idtcm->lock);
1928 err = _idtcm_adjfine(channel, scaled_ppm);
1929 mutex_unlock(idtcm->lock);
1933 "Failed at line %d in %s!", __LINE__, __func__);
1935 channel->current_freq_scaled_ppm = scaled_ppm;
1940 static int idtcm_enable(struct ptp_clock_info *ptp,
1941 struct ptp_clock_request *rq, int on)
1943 struct idtcm_channel *channel = container_of(ptp, struct idtcm_channel, caps);
1944 struct idtcm *idtcm = channel->idtcm;
1945 int err = -EOPNOTSUPP;
1947 mutex_lock(idtcm->lock);
1950 case PTP_CLK_REQ_PEROUT:
1952 err = idtcm_perout_enable(channel, &rq->perout, false);
1953 /* Only accept a 1-PPS aligned to the second. */
1954 else if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
1955 rq->perout.period.nsec)
1958 err = idtcm_perout_enable(channel, &rq->perout, true);
1960 case PTP_CLK_REQ_EXTTS:
1961 err = idtcm_enable_extts(channel, rq->extts.index,
1962 rq->extts.rsv[0], on);
1968 mutex_unlock(idtcm->lock);
1971 dev_err(channel->idtcm->dev,
1972 "Failed in %s with err %d!", __func__, err);
1977 static int idtcm_enable_tod(struct idtcm_channel *channel)
1979 struct idtcm *idtcm = channel->idtcm;
1980 struct timespec64 ts = {0, 0};
1981 u16 tod_cfg = IDTCM_FW_REG(idtcm->fw_ver, V520, TOD_CFG);
1985 /* STEELAI-366 - Temporary workaround for ts2phc compatibility */
1987 err = idtcm_output_mask_enable(channel, false);
1993 * Start the TOD clock ticking.
1995 err = idtcm_read(idtcm, channel->tod_n, tod_cfg, &cfg, sizeof(cfg));
2001 err = idtcm_write(idtcm, channel->tod_n, tod_cfg, &cfg, sizeof(cfg));
2005 if (idtcm->fw_ver < V487)
2006 return _idtcm_settime_deprecated(channel, &ts);
2008 return _idtcm_settime(channel, &ts,
2009 SCSR_TOD_WR_TYPE_SEL_ABSOLUTE);
2012 static void idtcm_set_version_info(struct idtcm *idtcm)
2021 idtcm_read_major_release(idtcm, &major);
2022 idtcm_read_minor_release(idtcm, &minor);
2023 idtcm_read_hotfix_release(idtcm, &hotfix);
2025 idtcm_read_product_id(idtcm, &product_id);
2026 idtcm_read_hw_rev_id(idtcm, &hw_rev_id);
2028 idtcm_read_otp_scsr_config_select(idtcm, &config_select);
2030 snprintf(idtcm->version, sizeof(idtcm->version), "%u.%u.%u",
2031 major, minor, hotfix);
2033 idtcm->fw_ver = idtcm_fw_version(idtcm->version);
2035 dev_info(idtcm->dev,
2036 "%d.%d.%d, Id: 0x%04x HW Rev: %d OTP Config Select: %d",
2037 major, minor, hotfix,
2038 product_id, hw_rev_id, config_select);
2041 static const struct ptp_clock_info idtcm_caps = {
2042 .owner = THIS_MODULE,
2045 .n_ext_ts = MAX_TOD,
2046 .adjphase = &idtcm_adjphase,
2047 .adjfine = &idtcm_adjfine,
2048 .adjtime = &idtcm_adjtime,
2049 .gettime64 = &idtcm_gettime,
2050 .settime64 = &idtcm_settime,
2051 .enable = &idtcm_enable,
2052 .do_aux_work = &idtcm_work_handler,
2055 static const struct ptp_clock_info idtcm_caps_deprecated = {
2056 .owner = THIS_MODULE,
2059 .n_ext_ts = MAX_TOD,
2060 .adjphase = &idtcm_adjphase,
2061 .adjfine = &idtcm_adjfine,
2062 .adjtime = &idtcm_adjtime_deprecated,
2063 .gettime64 = &idtcm_gettime,
2064 .settime64 = &idtcm_settime_deprecated,
2065 .enable = &idtcm_enable,
2066 .do_aux_work = &idtcm_work_handler,
2069 static int configure_channel_pll(struct idtcm_channel *channel)
2071 struct idtcm *idtcm = channel->idtcm;
2074 switch (channel->pll) {
2076 channel->dpll_freq = DPLL_FREQ_0;
2077 channel->dpll_n = DPLL_0;
2078 channel->hw_dpll_n = HW_DPLL_0;
2079 channel->dpll_phase = DPLL_PHASE_0;
2080 channel->dpll_ctrl_n = DPLL_CTRL_0;
2081 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_0;
2084 channel->dpll_freq = DPLL_FREQ_1;
2085 channel->dpll_n = DPLL_1;
2086 channel->hw_dpll_n = HW_DPLL_1;
2087 channel->dpll_phase = DPLL_PHASE_1;
2088 channel->dpll_ctrl_n = DPLL_CTRL_1;
2089 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_1;
2092 channel->dpll_freq = DPLL_FREQ_2;
2093 channel->dpll_n = IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_2);
2094 channel->hw_dpll_n = HW_DPLL_2;
2095 channel->dpll_phase = DPLL_PHASE_2;
2096 channel->dpll_ctrl_n = DPLL_CTRL_2;
2097 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_2;
2100 channel->dpll_freq = DPLL_FREQ_3;
2101 channel->dpll_n = DPLL_3;
2102 channel->hw_dpll_n = HW_DPLL_3;
2103 channel->dpll_phase = DPLL_PHASE_3;
2104 channel->dpll_ctrl_n = DPLL_CTRL_3;
2105 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_3;
2108 channel->dpll_freq = DPLL_FREQ_4;
2109 channel->dpll_n = IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_4);
2110 channel->hw_dpll_n = HW_DPLL_4;
2111 channel->dpll_phase = DPLL_PHASE_4;
2112 channel->dpll_ctrl_n = DPLL_CTRL_4;
2113 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_4;
2116 channel->dpll_freq = DPLL_FREQ_5;
2117 channel->dpll_n = DPLL_5;
2118 channel->hw_dpll_n = HW_DPLL_5;
2119 channel->dpll_phase = DPLL_PHASE_5;
2120 channel->dpll_ctrl_n = DPLL_CTRL_5;
2121 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_5;
2124 channel->dpll_freq = DPLL_FREQ_6;
2125 channel->dpll_n = IDTCM_FW_REG(idtcm->fw_ver, V520, DPLL_6);
2126 channel->hw_dpll_n = HW_DPLL_6;
2127 channel->dpll_phase = DPLL_PHASE_6;
2128 channel->dpll_ctrl_n = DPLL_CTRL_6;
2129 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_6;
2132 channel->dpll_freq = DPLL_FREQ_7;
2133 channel->dpll_n = DPLL_7;
2134 channel->hw_dpll_n = HW_DPLL_7;
2135 channel->dpll_phase = DPLL_PHASE_7;
2136 channel->dpll_ctrl_n = DPLL_CTRL_7;
2137 channel->dpll_phase_pull_in = DPLL_PHASE_PULL_IN_7;
2147 * Compensate for the PTP DCO input-to-output delay.
2148 * This delay is 18 FOD cycles.
2150 static u32 idtcm_get_dco_delay(struct idtcm_channel *channel)
2152 struct idtcm *idtcm = channel->idtcm;
2160 err = idtcm_read(idtcm, channel->dpll_ctrl_n,
2161 DPLL_CTRL_DPLL_FOD_FREQ, mbuf, 6);
2165 err = idtcm_read(idtcm, channel->dpll_ctrl_n,
2166 DPLL_CTRL_DPLL_FOD_FREQ + 6, nbuf, 2);
2170 m = get_unaligned_le64(mbuf);
2171 n = get_unaligned_le16(nbuf);
2176 fodFreq = (u32)div_u64(m, n);
2177 if (fodFreq >= 500000000)
2178 return 18 * (u32)div_u64(NSEC_PER_SEC, fodFreq);
2183 static int configure_channel_tod(struct idtcm_channel *channel, u32 index)
2185 enum fw_version fw_ver = channel->idtcm->fw_ver;
2187 /* Set tod addresses */
2190 channel->tod_read_primary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_PRIMARY_0);
2191 channel->tod_write = IDTCM_FW_REG(fw_ver, V520, TOD_WRITE_0);
2192 channel->tod_n = IDTCM_FW_REG(fw_ver, V520, TOD_0);
2193 channel->sync_src = SYNC_SOURCE_DPLL0_TOD_PPS;
2196 channel->tod_read_primary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_PRIMARY_1);
2197 channel->tod_write = IDTCM_FW_REG(fw_ver, V520, TOD_WRITE_1);
2198 channel->tod_n = IDTCM_FW_REG(fw_ver, V520, TOD_1);
2199 channel->sync_src = SYNC_SOURCE_DPLL1_TOD_PPS;
2202 channel->tod_read_primary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_PRIMARY_2);
2203 channel->tod_write = IDTCM_FW_REG(fw_ver, V520, TOD_WRITE_2);
2204 channel->tod_n = IDTCM_FW_REG(fw_ver, V520, TOD_2);
2205 channel->sync_src = SYNC_SOURCE_DPLL2_TOD_PPS;
2208 channel->tod_read_primary = IDTCM_FW_REG(fw_ver, V520, TOD_READ_PRIMARY_3);
2209 channel->tod_write = IDTCM_FW_REG(fw_ver, V520, TOD_WRITE_3);
2210 channel->tod_n = IDTCM_FW_REG(fw_ver, V520, TOD_3);
2211 channel->sync_src = SYNC_SOURCE_DPLL3_TOD_PPS;
2220 static int idtcm_enable_channel(struct idtcm *idtcm, u32 index)
2222 struct idtcm_channel *channel;
2225 if (!(index < MAX_TOD))
2228 channel = &idtcm->channel[index];
2230 channel->idtcm = idtcm;
2231 channel->current_freq_scaled_ppm = 0;
2233 /* Set pll addresses */
2234 err = configure_channel_pll(channel);
2238 /* Set tod addresses */
2239 err = configure_channel_tod(channel, index);
2243 if (idtcm->fw_ver < V487)
2244 channel->caps = idtcm_caps_deprecated;
2246 channel->caps = idtcm_caps;
2248 snprintf(channel->caps.name, sizeof(channel->caps.name),
2249 "IDT CM TOD%u", index);
2251 err = initialize_dco_operating_mode(channel);
2255 err = idtcm_enable_tod(channel);
2258 "Failed at line %d in %s!", __LINE__, __func__);
2262 channel->dco_delay = idtcm_get_dco_delay(channel);
2264 channel->ptp_clock = ptp_clock_register(&channel->caps, NULL);
2266 if (IS_ERR(channel->ptp_clock)) {
2267 err = PTR_ERR(channel->ptp_clock);
2268 channel->ptp_clock = NULL;
2272 if (!channel->ptp_clock)
2275 dev_info(idtcm->dev, "PLL%d registered as ptp%d",
2276 index, channel->ptp_clock->index);
2281 static int idtcm_enable_extts_channel(struct idtcm *idtcm, u32 index)
2283 struct idtcm_channel *channel;
2286 if (!(index < MAX_TOD))
2289 channel = &idtcm->channel[index];
2290 channel->idtcm = idtcm;
2292 /* Set tod addresses */
2293 err = configure_channel_tod(channel, index);
2297 channel->idtcm = idtcm;
2302 static void idtcm_extts_check(struct work_struct *work)
2304 struct idtcm *idtcm = container_of(work, struct idtcm, extts_work.work);
2307 if (idtcm->extts_mask == 0)
2310 mutex_lock(idtcm->lock);
2311 for (i = 0; i < MAX_TOD; i++) {
2314 if (idtcm->extts_mask & mask) {
2315 err = idtcm_extts_check_channel(idtcm, i);
2316 /* trigger clears itself, so clear the mask */
2318 idtcm->extts_mask &= ~mask;
2322 if (idtcm->extts_mask)
2323 schedule_delayed_work(&idtcm->extts_work,
2324 msecs_to_jiffies(EXTTS_PERIOD_MS));
2325 mutex_unlock(idtcm->lock);
2328 static void ptp_clock_unregister_all(struct idtcm *idtcm)
2331 struct idtcm_channel *channel;
2333 for (i = 0; i < MAX_TOD; i++) {
2334 channel = &idtcm->channel[i];
2335 if (channel->ptp_clock)
2336 ptp_clock_unregister(channel->ptp_clock);
2340 static void set_default_masks(struct idtcm *idtcm)
2342 idtcm->tod_mask = DEFAULT_TOD_MASK;
2343 idtcm->extts_mask = 0;
2345 idtcm->channel[0].pll = DEFAULT_TOD0_PTP_PLL;
2346 idtcm->channel[1].pll = DEFAULT_TOD1_PTP_PLL;
2347 idtcm->channel[2].pll = DEFAULT_TOD2_PTP_PLL;
2348 idtcm->channel[3].pll = DEFAULT_TOD3_PTP_PLL;
2350 idtcm->channel[0].output_mask = DEFAULT_OUTPUT_MASK_PLL0;
2351 idtcm->channel[1].output_mask = DEFAULT_OUTPUT_MASK_PLL1;
2352 idtcm->channel[2].output_mask = DEFAULT_OUTPUT_MASK_PLL2;
2353 idtcm->channel[3].output_mask = DEFAULT_OUTPUT_MASK_PLL3;
2356 static int idtcm_probe(struct platform_device *pdev)
2358 struct rsmu_ddata *ddata = dev_get_drvdata(pdev->dev.parent);
2359 struct idtcm *idtcm;
2363 idtcm = devm_kzalloc(&pdev->dev, sizeof(struct idtcm), GFP_KERNEL);
2368 idtcm->dev = &pdev->dev;
2369 idtcm->mfd = pdev->dev.parent;
2370 idtcm->lock = &ddata->lock;
2371 idtcm->regmap = ddata->regmap;
2372 idtcm->calculate_overhead_flag = 0;
2374 INIT_DELAYED_WORK(&idtcm->extts_work, idtcm_extts_check);
2376 set_default_masks(idtcm);
2378 mutex_lock(idtcm->lock);
2380 idtcm_set_version_info(idtcm);
2382 err = idtcm_load_firmware(idtcm, &pdev->dev);
2385 dev_warn(idtcm->dev, "loading firmware failed with %d", err);
2387 wait_for_chip_ready(idtcm);
2389 if (idtcm->tod_mask) {
2390 for (i = 0; i < MAX_TOD; i++) {
2391 if (idtcm->tod_mask & (1 << i))
2392 err = idtcm_enable_channel(idtcm, i);
2394 err = idtcm_enable_extts_channel(idtcm, i);
2397 "idtcm_enable_channel %d failed!", i);
2403 "no PLLs flagged as PHCs, nothing to do");
2407 mutex_unlock(idtcm->lock);
2410 ptp_clock_unregister_all(idtcm);
2414 platform_set_drvdata(pdev, idtcm);
2419 static int idtcm_remove(struct platform_device *pdev)
2421 struct idtcm *idtcm = platform_get_drvdata(pdev);
2423 ptp_clock_unregister_all(idtcm);
2425 cancel_delayed_work_sync(&idtcm->extts_work);
2430 static struct platform_driver idtcm_driver = {
2432 .name = "8a3400x-phc",
2434 .probe = idtcm_probe,
2435 .remove = idtcm_remove,
2438 module_platform_driver(idtcm_driver);