18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * PTP hardware clock driver for the IDT 82P33XXX family of clocks.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2019 Integrated Device Technology, Inc., a Renesas Company.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#ifndef PTP_IDT82P33_H
88c2ecf20Sopenharmony_ci#define PTP_IDT82P33_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/ktime.h>
118c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/* Register Map - AN888_SMUforIEEE_SynchEther_82P33xxx_RevH.pdf */
158c2ecf20Sopenharmony_ci#define PAGE_NUM (8)
168c2ecf20Sopenharmony_ci#define _ADDR(page, offset) (((page) << 0x7) | ((offset) & 0x7f))
178c2ecf20Sopenharmony_ci#define _PAGE(addr) (((addr) >> 0x7) & 0x7)
188c2ecf20Sopenharmony_ci#define _OFFSET(addr)  ((addr) & 0x7f)
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define DPLL1_TOD_CNFG 0x134
218c2ecf20Sopenharmony_ci#define DPLL2_TOD_CNFG 0x1B4
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define DPLL1_TOD_STS 0x10B
248c2ecf20Sopenharmony_ci#define DPLL2_TOD_STS 0x18B
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define DPLL1_TOD_TRIGGER 0x115
278c2ecf20Sopenharmony_ci#define DPLL2_TOD_TRIGGER 0x195
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DPLL1_OPERATING_MODE_CNFG 0x120
308c2ecf20Sopenharmony_ci#define DPLL2_OPERATING_MODE_CNFG 0x1A0
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define DPLL1_HOLDOVER_FREQ_CNFG 0x12C
338c2ecf20Sopenharmony_ci#define DPLL2_HOLDOVER_FREQ_CNFG 0x1AC
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define DPLL1_PHASE_OFFSET_CNFG 0x143
368c2ecf20Sopenharmony_ci#define DPLL2_PHASE_OFFSET_CNFG 0x1C3
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define DPLL1_SYNC_EDGE_CNFG 0X140
398c2ecf20Sopenharmony_ci#define DPLL2_SYNC_EDGE_CNFG 0X1C0
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define DPLL1_INPUT_MODE_CNFG 0X116
428c2ecf20Sopenharmony_ci#define DPLL2_INPUT_MODE_CNFG 0X196
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define OUT_MUX_CNFG(outn) _ADDR(0x6, (0xC * (outn)))
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define PAGE_ADDR 0x7F
478c2ecf20Sopenharmony_ci/* Register Map end */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* Register definitions - AN888_SMUforIEEE_SynchEther_82P33xxx_RevH.pdf*/
508c2ecf20Sopenharmony_ci#define TOD_TRIGGER(wr_trig, rd_trig) ((wr_trig & 0xf) << 4 | (rd_trig & 0xf))
518c2ecf20Sopenharmony_ci#define SYNC_TOD BIT(1)
528c2ecf20Sopenharmony_ci#define PH_OFFSET_EN BIT(7)
538c2ecf20Sopenharmony_ci#define SQUELCH_ENABLE BIT(5)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* Bit definitions for the DPLL_MODE register */
568c2ecf20Sopenharmony_ci#define PLL_MODE_SHIFT                    (0)
578c2ecf20Sopenharmony_ci#define PLL_MODE_MASK                     (0x1F)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cienum pll_mode {
608c2ecf20Sopenharmony_ci	PLL_MODE_MIN = 0,
618c2ecf20Sopenharmony_ci	PLL_MODE_AUTOMATIC = PLL_MODE_MIN,
628c2ecf20Sopenharmony_ci	PLL_MODE_FORCE_FREERUN = 1,
638c2ecf20Sopenharmony_ci	PLL_MODE_FORCE_HOLDOVER = 2,
648c2ecf20Sopenharmony_ci	PLL_MODE_FORCE_LOCKED = 4,
658c2ecf20Sopenharmony_ci	PLL_MODE_FORCE_PRE_LOCKED2 = 5,
668c2ecf20Sopenharmony_ci	PLL_MODE_FORCE_PRE_LOCKED = 6,
678c2ecf20Sopenharmony_ci	PLL_MODE_FORCE_LOST_PHASE = 7,
688c2ecf20Sopenharmony_ci	PLL_MODE_DCO = 10,
698c2ecf20Sopenharmony_ci	PLL_MODE_WPH = 18,
708c2ecf20Sopenharmony_ci	PLL_MODE_MAX = PLL_MODE_WPH,
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cienum hw_tod_trig_sel {
748c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_MIN = 0,
758c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_NO_WRITE = HW_TOD_TRIG_SEL_MIN,
768c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_SYNC_SEL = 1,
778c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_IN12 = 2,
788c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_IN13 = 3,
798c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_IN14 = 4,
808c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_TOD_PPS = 5,
818c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_TIMER_INTERVAL = 6,
828c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_MSB_PHASE_OFFSET_CNFG = 7,
838c2ecf20Sopenharmony_ci	HW_TOD_TRIG_SEL_MSB_HOLDOVER_FREQ_CNFG = 8,
848c2ecf20Sopenharmony_ci	HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG = 9,
858c2ecf20Sopenharmony_ci	HW_TOD_RD_TRIG_SEL_LSB_TOD_STS = HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
868c2ecf20Sopenharmony_ci	WR_TRIG_SEL_MAX = HW_TOD_WR_TRIG_SEL_MSB_TOD_CNFG,
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* Register bit definitions end */
908c2ecf20Sopenharmony_ci#define FW_FILENAME	"idt82p33xxx.bin"
918c2ecf20Sopenharmony_ci#define MAX_PHC_PLL (2)
928c2ecf20Sopenharmony_ci#define TOD_BYTE_COUNT (10)
938c2ecf20Sopenharmony_ci#define MAX_MEASURMENT_COUNT (5)
948c2ecf20Sopenharmony_ci#define SNAP_THRESHOLD_NS (150000)
958c2ecf20Sopenharmony_ci#define SYNC_TOD_TIMEOUT_SEC (5)
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci#define PLLMASK_ADDR_HI	0xFF
988c2ecf20Sopenharmony_ci#define PLLMASK_ADDR_LO	0xA5
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define PLL0_OUTMASK_ADDR_HI	0xFF
1018c2ecf20Sopenharmony_ci#define PLL0_OUTMASK_ADDR_LO	0xB0
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define PLL1_OUTMASK_ADDR_HI	0xFF
1048c2ecf20Sopenharmony_ci#define PLL1_OUTMASK_ADDR_LO	0xB2
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define PLL2_OUTMASK_ADDR_HI	0xFF
1078c2ecf20Sopenharmony_ci#define PLL2_OUTMASK_ADDR_LO	0xB4
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define PLL3_OUTMASK_ADDR_HI	0xFF
1108c2ecf20Sopenharmony_ci#define PLL3_OUTMASK_ADDR_LO	0xB6
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define DEFAULT_PLL_MASK	(0x01)
1138c2ecf20Sopenharmony_ci#define DEFAULT_OUTPUT_MASK_PLL0	(0xc0)
1148c2ecf20Sopenharmony_ci#define DEFAULT_OUTPUT_MASK_PLL1	DEFAULT_OUTPUT_MASK_PLL0
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/* PTP Hardware Clock interface */
1178c2ecf20Sopenharmony_cistruct idt82p33_channel {
1188c2ecf20Sopenharmony_ci	struct ptp_clock_info	caps;
1198c2ecf20Sopenharmony_ci	struct ptp_clock	*ptp_clock;
1208c2ecf20Sopenharmony_ci	struct idt82p33	*idt82p33;
1218c2ecf20Sopenharmony_ci	enum pll_mode	pll_mode;
1228c2ecf20Sopenharmony_ci	/* task to turn off SYNC_TOD bit after pps sync */
1238c2ecf20Sopenharmony_ci	struct delayed_work	sync_tod_work;
1248c2ecf20Sopenharmony_ci	bool			sync_tod_on;
1258c2ecf20Sopenharmony_ci	s32			current_freq_ppb;
1268c2ecf20Sopenharmony_ci	u8			output_mask;
1278c2ecf20Sopenharmony_ci	u16			dpll_tod_cnfg;
1288c2ecf20Sopenharmony_ci	u16			dpll_tod_trigger;
1298c2ecf20Sopenharmony_ci	u16			dpll_tod_sts;
1308c2ecf20Sopenharmony_ci	u16			dpll_mode_cnfg;
1318c2ecf20Sopenharmony_ci	u16			dpll_freq_cnfg;
1328c2ecf20Sopenharmony_ci	u16			dpll_phase_cnfg;
1338c2ecf20Sopenharmony_ci	u16			dpll_sync_cnfg;
1348c2ecf20Sopenharmony_ci	u16			dpll_input_mode_cnfg;
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistruct idt82p33 {
1388c2ecf20Sopenharmony_ci	struct idt82p33_channel channel[MAX_PHC_PLL];
1398c2ecf20Sopenharmony_ci	struct i2c_client	*client;
1408c2ecf20Sopenharmony_ci	u8	page_offset;
1418c2ecf20Sopenharmony_ci	u8	pll_mask;
1428c2ecf20Sopenharmony_ci	ktime_t start_time;
1438c2ecf20Sopenharmony_ci	int calculate_overhead_flag;
1448c2ecf20Sopenharmony_ci	s64 tod_write_overhead_ns;
1458c2ecf20Sopenharmony_ci	/* Protects I2C read/modify/write registers from concurrent access */
1468c2ecf20Sopenharmony_ci	struct mutex	reg_lock;
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/* firmware interface */
1508c2ecf20Sopenharmony_cistruct idt82p33_fwrc {
1518c2ecf20Sopenharmony_ci	u8 hiaddr;
1528c2ecf20Sopenharmony_ci	u8 loaddr;
1538c2ecf20Sopenharmony_ci	u8 value;
1548c2ecf20Sopenharmony_ci	u8 reserved;
1558c2ecf20Sopenharmony_ci} __packed;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/**
1588c2ecf20Sopenharmony_ci * @brief Maximum absolute value for write phase offset in femtoseconds
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_ci#define WRITE_PHASE_OFFSET_LIMIT (20000052084ll)
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/** @brief Phase offset resolution
1638c2ecf20Sopenharmony_ci *
1648c2ecf20Sopenharmony_ci *  DPLL phase offset = 10^15 fs / ( System Clock  * 2^13)
1658c2ecf20Sopenharmony_ci *                    = 10^15 fs / ( 1638400000 * 2^23)
1668c2ecf20Sopenharmony_ci *                    = 74.5058059692382 fs
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_ci#define IDT_T0DPLL_PHASE_RESOL 74506
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci#endif /* PTP_IDT82P33_H */
172