xref: /dpdk/drivers/common/cnxk/roc_nix_ptp.c (revision 44a9307c0908ae28989f5ab9b90ddcbc5484592f)
1c443e0d3SSunil Kumar Kori /* SPDX-License-Identifier: BSD-3-Clause
2c443e0d3SSunil Kumar Kori  * Copyright(C) 2021 Marvell.
3c443e0d3SSunil Kumar Kori  */
4c443e0d3SSunil Kumar Kori 
5c443e0d3SSunil Kumar Kori #include "roc_api.h"
6c443e0d3SSunil Kumar Kori #include "roc_priv.h"
7c443e0d3SSunil Kumar Kori 
8c443e0d3SSunil Kumar Kori #define PTP_FREQ_ADJUST (1 << 9)
9c443e0d3SSunil Kumar Kori 
10c443e0d3SSunil Kumar Kori int
roc_nix_ptp_rx_ena_dis(struct roc_nix * roc_nix,int enable)11c443e0d3SSunil Kumar Kori roc_nix_ptp_rx_ena_dis(struct roc_nix *roc_nix, int enable)
12c443e0d3SSunil Kumar Kori {
13*44a9307cSRakesh Kudurumalla 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
14*44a9307cSRakesh Kudurumalla 	struct dev *dev = &nix->dev;
15*44a9307cSRakesh Kudurumalla 	struct mbox *mbox = mbox_get(dev->mbox);
16*44a9307cSRakesh Kudurumalla 	int rc;
17c443e0d3SSunil Kumar Kori 
18*44a9307cSRakesh Kudurumalla 	if (roc_nix_is_vf_or_sdp(roc_nix) || roc_nix_is_lbk(roc_nix)) {
19*44a9307cSRakesh Kudurumalla 		rc = NIX_ERR_PARAM;
20*44a9307cSRakesh Kudurumalla 		goto exit;
21*44a9307cSRakesh Kudurumalla 	}
22c443e0d3SSunil Kumar Kori 
23c443e0d3SSunil Kumar Kori 	if (enable)
24c443e0d3SSunil Kumar Kori 		mbox_alloc_msg_cgx_ptp_rx_enable(mbox);
25c443e0d3SSunil Kumar Kori 	else
26c443e0d3SSunil Kumar Kori 		mbox_alloc_msg_cgx_ptp_rx_disable(mbox);
27c443e0d3SSunil Kumar Kori 
28*44a9307cSRakesh Kudurumalla 	rc = mbox_process(mbox);
29*44a9307cSRakesh Kudurumalla exit:
30*44a9307cSRakesh Kudurumalla 	mbox_put(mbox);
31*44a9307cSRakesh Kudurumalla 	return rc;
32c443e0d3SSunil Kumar Kori }
33c443e0d3SSunil Kumar Kori 
34c443e0d3SSunil Kumar Kori int
roc_nix_ptp_tx_ena_dis(struct roc_nix * roc_nix,int enable)35c443e0d3SSunil Kumar Kori roc_nix_ptp_tx_ena_dis(struct roc_nix *roc_nix, int enable)
36c443e0d3SSunil Kumar Kori {
37*44a9307cSRakesh Kudurumalla 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
38*44a9307cSRakesh Kudurumalla 	struct dev *dev = &nix->dev;
39*44a9307cSRakesh Kudurumalla 	struct mbox *mbox = mbox_get(dev->mbox);
40*44a9307cSRakesh Kudurumalla 	int rc;
41c443e0d3SSunil Kumar Kori 
42*44a9307cSRakesh Kudurumalla 	if (roc_nix_is_vf_or_sdp(roc_nix) || roc_nix_is_lbk(roc_nix)) {
43*44a9307cSRakesh Kudurumalla 		rc = NIX_ERR_PARAM;
44*44a9307cSRakesh Kudurumalla 		goto exit;
45*44a9307cSRakesh Kudurumalla 	}
46c443e0d3SSunil Kumar Kori 
47c443e0d3SSunil Kumar Kori 	if (enable)
48c443e0d3SSunil Kumar Kori 		mbox_alloc_msg_nix_lf_ptp_tx_enable(mbox);
49c443e0d3SSunil Kumar Kori 	else
50c443e0d3SSunil Kumar Kori 		mbox_alloc_msg_nix_lf_ptp_tx_disable(mbox);
51c443e0d3SSunil Kumar Kori 
52*44a9307cSRakesh Kudurumalla 	rc = mbox_process(mbox);
53*44a9307cSRakesh Kudurumalla exit:
54*44a9307cSRakesh Kudurumalla 	mbox_put(mbox);
55*44a9307cSRakesh Kudurumalla 	return rc;
56c443e0d3SSunil Kumar Kori }
57c443e0d3SSunil Kumar Kori 
58c443e0d3SSunil Kumar Kori int
roc_nix_ptp_clock_read(struct roc_nix * roc_nix,uint64_t * clock,uint64_t * tsc,uint8_t is_pmu)59c443e0d3SSunil Kumar Kori roc_nix_ptp_clock_read(struct roc_nix *roc_nix, uint64_t *clock, uint64_t *tsc,
60c443e0d3SSunil Kumar Kori 		       uint8_t is_pmu)
61c443e0d3SSunil Kumar Kori {
62*44a9307cSRakesh Kudurumalla 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
63*44a9307cSRakesh Kudurumalla 	struct dev *dev = &nix->dev;
64*44a9307cSRakesh Kudurumalla 	struct mbox *mbox = mbox_get(dev->mbox);
65c443e0d3SSunil Kumar Kori 	struct ptp_req *req;
66c443e0d3SSunil Kumar Kori 	struct ptp_rsp *rsp;
67c443e0d3SSunil Kumar Kori 	int rc = -ENOSPC;
68c443e0d3SSunil Kumar Kori 
69c443e0d3SSunil Kumar Kori 	req = mbox_alloc_msg_ptp_op(mbox);
70c443e0d3SSunil Kumar Kori 	if (req == NULL)
71*44a9307cSRakesh Kudurumalla 		goto exit;
72c443e0d3SSunil Kumar Kori 	req->op = PTP_OP_GET_CLOCK;
73c443e0d3SSunil Kumar Kori 	req->is_pmu = is_pmu;
74c443e0d3SSunil Kumar Kori 	rc = mbox_process_msg(mbox, (void *)&rsp);
75c443e0d3SSunil Kumar Kori 	if (rc)
76*44a9307cSRakesh Kudurumalla 		goto exit;
77c443e0d3SSunil Kumar Kori 
78c443e0d3SSunil Kumar Kori 	if (clock)
79c443e0d3SSunil Kumar Kori 		*clock = rsp->clk;
80c443e0d3SSunil Kumar Kori 
81c443e0d3SSunil Kumar Kori 	if (tsc)
82c443e0d3SSunil Kumar Kori 		*tsc = rsp->tsc;
83c443e0d3SSunil Kumar Kori 
84*44a9307cSRakesh Kudurumalla 	rc = 0;
85*44a9307cSRakesh Kudurumalla exit:
86*44a9307cSRakesh Kudurumalla 	mbox_put(mbox);
87*44a9307cSRakesh Kudurumalla 	return rc;
88c443e0d3SSunil Kumar Kori }
89c443e0d3SSunil Kumar Kori 
90c443e0d3SSunil Kumar Kori int
roc_nix_ptp_sync_time_adjust(struct roc_nix * roc_nix,int64_t delta)91c443e0d3SSunil Kumar Kori roc_nix_ptp_sync_time_adjust(struct roc_nix *roc_nix, int64_t delta)
92c443e0d3SSunil Kumar Kori {
93*44a9307cSRakesh Kudurumalla 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
94*44a9307cSRakesh Kudurumalla 	struct dev *dev = &nix->dev;
95*44a9307cSRakesh Kudurumalla 	struct mbox *mbox = mbox_get(dev->mbox);
96c443e0d3SSunil Kumar Kori 	struct ptp_req *req;
97c443e0d3SSunil Kumar Kori 	struct ptp_rsp *rsp;
98c443e0d3SSunil Kumar Kori 	int rc = -ENOSPC;
99c443e0d3SSunil Kumar Kori 
100*44a9307cSRakesh Kudurumalla 	if (roc_nix_is_vf_or_sdp(roc_nix) || roc_nix_is_lbk(roc_nix)) {
101*44a9307cSRakesh Kudurumalla 		rc = NIX_ERR_PARAM;
102*44a9307cSRakesh Kudurumalla 		goto exit;
103*44a9307cSRakesh Kudurumalla 	}
104c443e0d3SSunil Kumar Kori 
105*44a9307cSRakesh Kudurumalla 	if ((delta <= -PTP_FREQ_ADJUST) || (delta >= PTP_FREQ_ADJUST)) {
106*44a9307cSRakesh Kudurumalla 		rc = NIX_ERR_INVALID_RANGE;
107*44a9307cSRakesh Kudurumalla 		goto exit;
108*44a9307cSRakesh Kudurumalla 	}
109c443e0d3SSunil Kumar Kori 
110c443e0d3SSunil Kumar Kori 	req = mbox_alloc_msg_ptp_op(mbox);
111c443e0d3SSunil Kumar Kori 	if (req == NULL)
112*44a9307cSRakesh Kudurumalla 		goto exit;
113c443e0d3SSunil Kumar Kori 	req->op = PTP_OP_ADJFINE;
114c443e0d3SSunil Kumar Kori 	req->scaled_ppm = delta;
115c443e0d3SSunil Kumar Kori 
116*44a9307cSRakesh Kudurumalla 	rc = mbox_process_msg(mbox, (void *)&rsp);
117*44a9307cSRakesh Kudurumalla exit:
118*44a9307cSRakesh Kudurumalla 	mbox_put(mbox);
119*44a9307cSRakesh Kudurumalla 	return rc;
120c443e0d3SSunil Kumar Kori }
121c443e0d3SSunil Kumar Kori 
122c443e0d3SSunil Kumar Kori int
roc_nix_ptp_info_cb_register(struct roc_nix * roc_nix,ptp_info_update_t ptp_update)123c443e0d3SSunil Kumar Kori roc_nix_ptp_info_cb_register(struct roc_nix *roc_nix,
124c443e0d3SSunil Kumar Kori 			     ptp_info_update_t ptp_update)
125c443e0d3SSunil Kumar Kori {
126c443e0d3SSunil Kumar Kori 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
127c443e0d3SSunil Kumar Kori 	struct dev *dev = &nix->dev;
128c443e0d3SSunil Kumar Kori 
129c443e0d3SSunil Kumar Kori 	if (ptp_update == NULL)
130c443e0d3SSunil Kumar Kori 		return NIX_ERR_PARAM;
131c443e0d3SSunil Kumar Kori 
132c443e0d3SSunil Kumar Kori 	dev->ops->ptp_info_update = (ptp_info_t)ptp_update;
133c443e0d3SSunil Kumar Kori 	return 0;
134c443e0d3SSunil Kumar Kori }
135c443e0d3SSunil Kumar Kori 
136c443e0d3SSunil Kumar Kori void
roc_nix_ptp_info_cb_unregister(struct roc_nix * roc_nix)137c443e0d3SSunil Kumar Kori roc_nix_ptp_info_cb_unregister(struct roc_nix *roc_nix)
138c443e0d3SSunil Kumar Kori {
139c443e0d3SSunil Kumar Kori 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
140c443e0d3SSunil Kumar Kori 	struct dev *dev = &nix->dev;
141c443e0d3SSunil Kumar Kori 
142c443e0d3SSunil Kumar Kori 	dev->ops->ptp_info_update = NULL;
143c443e0d3SSunil Kumar Kori }
1448f98e3ecSHarman Kalra 
1458f98e3ecSHarman Kalra bool
roc_nix_ptp_is_enable(struct roc_nix * roc_nix)1468f98e3ecSHarman Kalra roc_nix_ptp_is_enable(struct roc_nix *roc_nix)
1478f98e3ecSHarman Kalra {
1488f98e3ecSHarman Kalra 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
1498f98e3ecSHarman Kalra 
1508f98e3ecSHarman Kalra 	return nix->ptp_en;
1518f98e3ecSHarman Kalra }
152