blob: 833e5907c351d1b6e5051e74556661e1d8436a41 [file] [log] [blame]
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -04001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * PTP hardware clock driver for the IDT ClockMatrix(TM) family of timing and
4 * synchronization devices.
5 *
6 * Copyright (C) 2019 Integrated Device Technology, Inc., a Renesas Company.
7 */
8#ifndef PTP_IDTCLOCKMATRIX_H
9#define PTP_IDTCLOCKMATRIX_H
10
11#include <linux/ktime.h>
12
13#include "idt8a340_reg.h"
14
15#define FW_FILENAME "idtcm.bin"
Min Li7ea5fda2020-07-28 16:00:30 -040016#define MAX_TOD (4)
17#define MAX_PLL (8)
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -040018
Vincent Cheng425d2b12020-05-01 23:35:38 -040019#define MAX_ABS_WRITE_PHASE_PICOSECONDS (107374182350LL)
20
Min Li7ea5fda2020-07-28 16:00:30 -040021#define TOD_MASK_ADDR (0xFFA5)
22#define DEFAULT_TOD_MASK (0x04)
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -040023
24#define SET_U16_LSB(orig, val8) (orig = (0xff00 & (orig)) | (val8))
25#define SET_U16_MSB(orig, val8) (orig = (0x00ff & (orig)) | (val8 << 8))
26
Min Li7ea5fda2020-07-28 16:00:30 -040027#define TOD0_PTP_PLL_ADDR (0xFFA8)
28#define TOD1_PTP_PLL_ADDR (0xFFA9)
29#define TOD2_PTP_PLL_ADDR (0xFFAA)
30#define TOD3_PTP_PLL_ADDR (0xFFAB)
31
32#define TOD0_OUT_ALIGN_MASK_ADDR (0xFFB0)
33#define TOD1_OUT_ALIGN_MASK_ADDR (0xFFB2)
34#define TOD2_OUT_ALIGN_MASK_ADDR (0xFFB4)
35#define TOD3_OUT_ALIGN_MASK_ADDR (0xFFB6)
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -040036
37#define DEFAULT_OUTPUT_MASK_PLL0 (0x003)
38#define DEFAULT_OUTPUT_MASK_PLL1 (0x00c)
39#define DEFAULT_OUTPUT_MASK_PLL2 (0x030)
40#define DEFAULT_OUTPUT_MASK_PLL3 (0x0c0)
41
Min Li7ea5fda2020-07-28 16:00:30 -040042#define DEFAULT_TOD0_PTP_PLL (0)
43#define DEFAULT_TOD1_PTP_PLL (1)
44#define DEFAULT_TOD2_PTP_PLL (2)
45#define DEFAULT_TOD3_PTP_PLL (3)
46
Min Lida948232020-12-08 10:41:57 -050047#define POST_SM_RESET_DELAY_MS (3000)
48#define PHASE_PULL_IN_THRESHOLD_NS_DEPRECATED (150000)
49#define PHASE_PULL_IN_THRESHOLD_NS (15000)
50#define TOD_WRITE_OVERHEAD_COUNT_MAX (2)
51#define TOD_BYTE_COUNT (11)
Min Li7ea5fda2020-07-28 16:00:30 -040052
Vincent Cheng797d3182021-02-17 00:42:12 -050053#define LOCK_TIMEOUT_MS (2000)
54#define LOCK_POLL_INTERVAL_MS (10)
55
Min Li251f4fe2020-12-08 10:41:54 -050056#define PEROUT_ENABLE_OUTPUT_MASK (0xdeadbeef)
Min Li7ea5fda2020-07-28 16:00:30 -040057
Min Li251f4fe2020-12-08 10:41:54 -050058#define IDTCM_MAX_WRITE_COUNT (512)
59
Min Lida9facf2021-09-13 16:12:34 -040060#define PHASE_PULL_IN_MAX_PPB (144000)
61#define PHASE_PULL_IN_MIN_THRESHOLD_NS (2)
62
Min Li794c3df2021-09-13 16:12:33 -040063/*
64 * Return register address based on passed in firmware version
65 */
66#define IDTCM_FW_REG(FW, VER, REG) (((FW) < (VER)) ? (REG) : (REG##_##VER))
Min Li957ff422020-08-18 10:41:22 -040067
Min Lida9facf2021-09-13 16:12:34 -040068/* PTP PLL Mode */
69enum ptp_pll_mode {
70 PTP_PLL_MODE_MIN = 0,
71 PTP_PLL_MODE_WRITE_FREQUENCY = PTP_PLL_MODE_MIN,
72 PTP_PLL_MODE_WRITE_PHASE,
73 PTP_PLL_MODE_UNSUPPORTED,
74 PTP_PLL_MODE_MAX = PTP_PLL_MODE_UNSUPPORTED,
75};
76
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -040077/* Values of DPLL_N.DPLL_MODE.PLL_MODE */
78enum pll_mode {
79 PLL_MODE_MIN = 0,
Min Lida9facf2021-09-13 16:12:34 -040080 PLL_MODE_PLL = PLL_MODE_MIN,
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -040081 PLL_MODE_WRITE_PHASE = 1,
82 PLL_MODE_WRITE_FREQUENCY = 2,
83 PLL_MODE_GPIO_INC_DEC = 3,
84 PLL_MODE_SYNTHESIS = 4,
85 PLL_MODE_PHASE_MEASUREMENT = 5,
Min Li7ea5fda2020-07-28 16:00:30 -040086 PLL_MODE_DISABLED = 6,
87 PLL_MODE_MAX = PLL_MODE_DISABLED,
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -040088};
89
Min Lida9facf2021-09-13 16:12:34 -040090/* Values of DPLL_CTRL_n.DPLL_MANU_REF_CFG.MANUAL_REFERENCE */
91enum manual_reference {
92 MANU_REF_MIN = 0,
93 MANU_REF_CLK0 = MANU_REF_MIN,
94 MANU_REF_CLK1,
95 MANU_REF_CLK2,
96 MANU_REF_CLK3,
97 MANU_REF_CLK4,
98 MANU_REF_CLK5,
99 MANU_REF_CLK6,
100 MANU_REF_CLK7,
101 MANU_REF_CLK8,
102 MANU_REF_CLK9,
103 MANU_REF_CLK10,
104 MANU_REF_CLK11,
105 MANU_REF_CLK12,
106 MANU_REF_CLK13,
107 MANU_REF_CLK14,
108 MANU_REF_CLK15,
109 MANU_REF_WRITE_PHASE,
110 MANU_REF_WRITE_FREQUENCY,
111 MANU_REF_XO_DPLL,
112 MANU_REF_MAX = MANU_REF_XO_DPLL,
113};
114
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -0400115enum hw_tod_write_trig_sel {
116 HW_TOD_WR_TRIG_SEL_MIN = 0,
117 HW_TOD_WR_TRIG_SEL_MSB = HW_TOD_WR_TRIG_SEL_MIN,
118 HW_TOD_WR_TRIG_SEL_RESERVED = 1,
119 HW_TOD_WR_TRIG_SEL_TOD_PPS = 2,
120 HW_TOD_WR_TRIG_SEL_IRIGB_PPS = 3,
121 HW_TOD_WR_TRIG_SEL_PWM_PPS = 4,
122 HW_TOD_WR_TRIG_SEL_GPIO = 5,
123 HW_TOD_WR_TRIG_SEL_FOD_SYNC = 6,
124 WR_TRIG_SEL_MAX = HW_TOD_WR_TRIG_SEL_FOD_SYNC,
125};
126
Min Li7ea5fda2020-07-28 16:00:30 -0400127/* 4.8.7 only */
128enum scsr_tod_write_trig_sel {
129 SCSR_TOD_WR_TRIG_SEL_DISABLE = 0,
130 SCSR_TOD_WR_TRIG_SEL_IMMEDIATE = 1,
131 SCSR_TOD_WR_TRIG_SEL_REFCLK = 2,
132 SCSR_TOD_WR_TRIG_SEL_PWMPPS = 3,
133 SCSR_TOD_WR_TRIG_SEL_TODPPS = 4,
134 SCSR_TOD_WR_TRIG_SEL_SYNCFOD = 5,
135 SCSR_TOD_WR_TRIG_SEL_GPIO = 6,
136 SCSR_TOD_WR_TRIG_SEL_MAX = SCSR_TOD_WR_TRIG_SEL_GPIO,
137};
138
139/* 4.8.7 only */
140enum scsr_tod_write_type_sel {
141 SCSR_TOD_WR_TYPE_SEL_ABSOLUTE = 0,
142 SCSR_TOD_WR_TYPE_SEL_DELTA_PLUS = 1,
143 SCSR_TOD_WR_TYPE_SEL_DELTA_MINUS = 2,
144 SCSR_TOD_WR_TYPE_SEL_MAX = SCSR_TOD_WR_TYPE_SEL_DELTA_MINUS,
145};
146
Vincent Cheng797d3182021-02-17 00:42:12 -0500147/* Values STATUS.DPLL_SYS_STATUS.DPLL_SYS_STATE */
148enum dpll_state {
149 DPLL_STATE_MIN = 0,
150 DPLL_STATE_FREERUN = DPLL_STATE_MIN,
151 DPLL_STATE_LOCKACQ = 1,
152 DPLL_STATE_LOCKREC = 2,
153 DPLL_STATE_LOCKED = 3,
154 DPLL_STATE_HOLDOVER = 4,
155 DPLL_STATE_OPEN_LOOP = 5,
156 DPLL_STATE_MAX = DPLL_STATE_OPEN_LOOP,
157};
158
Min Li794c3df2021-09-13 16:12:33 -0400159enum fw_version {
160 V_DEFAULT = 0,
161 V487 = 1,
162 V520 = 2,
163};
164
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -0400165struct idtcm;
166
167struct idtcm_channel {
168 struct ptp_clock_info caps;
169 struct ptp_clock *ptp_clock;
170 struct idtcm *idtcm;
171 u16 dpll_phase;
172 u16 dpll_freq;
173 u16 dpll_n;
174 u16 dpll_ctrl_n;
175 u16 dpll_phase_pull_in;
176 u16 tod_read_primary;
177 u16 tod_write;
178 u16 tod_n;
179 u16 hw_dpll_n;
Min Li794c3df2021-09-13 16:12:33 -0400180 u8 sync_src;
Min Lida9facf2021-09-13 16:12:34 -0400181 enum ptp_pll_mode mode;
182 int (*configure_write_frequency)(struct idtcm_channel *channel);
183 int (*configure_write_phase)(struct idtcm_channel *channel);
184 int (*do_phase_pull_in)(struct idtcm_channel *channel,
185 s32 offset_ns, u32 max_ffo_ppb);
186 s32 current_freq_scaled_ppm;
187 bool phase_pull_in;
Min Li7ea5fda2020-07-28 16:00:30 -0400188 u8 pll;
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -0400189 u16 output_mask;
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -0400190};
191
192struct idtcm {
Min Li7ea5fda2020-07-28 16:00:30 -0400193 struct idtcm_channel channel[MAX_TOD];
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -0400194 struct i2c_client *client;
195 u8 page_offset;
Min Li7ea5fda2020-07-28 16:00:30 -0400196 u8 tod_mask;
197 char version[16];
Min Li794c3df2021-09-13 16:12:33 -0400198 enum fw_version fw_ver;
Vincent Cheng3a6ba7d2019-10-31 23:20:07 -0400199
200 /* Overhead calculation for adjtime */
201 u8 calculate_overhead_flag;
202 s64 tod_write_overhead_ns;
203 ktime_t start_time;
204
205 /* Protects I2C read/modify/write registers from concurrent access */
206 struct mutex reg_lock;
207};
208
209struct idtcm_fwrc {
210 u8 hiaddr;
211 u8 loaddr;
212 u8 value;
213 u8 reserved;
214} __packed;
215
216#endif /* PTP_IDTCLOCKMATRIX_H */