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