1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 
8 #include "otx2_common.h"
9 #include "otx2_ptp.h"
10 
11 static int otx2_ptp_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
12 {
13 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
14 					    ptp_info);
15 	struct ptp_req *req;
16 
17 	if (!ptp->nic)
18 		return -ENODEV;
19 
20 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
21 	if (!req)
22 		return -ENOMEM;
23 
24 	req->op = PTP_OP_ADJFINE;
25 	req->scaled_ppm = scaled_ppm;
26 
27 	return otx2_sync_mbox_msg(&ptp->nic->mbox);
28 }
29 
30 static int ptp_set_thresh(struct otx2_ptp *ptp, u64 thresh)
31 {
32 	struct ptp_req *req;
33 
34 	if (!ptp->nic)
35 		return -ENODEV;
36 
37 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
38 	if (!req)
39 		return -ENOMEM;
40 
41 	req->op = PTP_OP_SET_THRESH;
42 	req->thresh = thresh;
43 
44 	return otx2_sync_mbox_msg(&ptp->nic->mbox);
45 }
46 
47 static u64 ptp_cc_read(const struct cyclecounter *cc)
48 {
49 	struct otx2_ptp *ptp = container_of(cc, struct otx2_ptp, cycle_counter);
50 	struct ptp_req *req;
51 	struct ptp_rsp *rsp;
52 	int err;
53 
54 	if (!ptp->nic)
55 		return 0;
56 
57 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
58 	if (!req)
59 		return 0;
60 
61 	req->op = PTP_OP_GET_CLOCK;
62 
63 	err = otx2_sync_mbox_msg(&ptp->nic->mbox);
64 	if (err)
65 		return 0;
66 
67 	rsp = (struct ptp_rsp *)otx2_mbox_get_rsp(&ptp->nic->mbox.mbox, 0,
68 						  &req->hdr);
69 	if (IS_ERR(rsp))
70 		return 0;
71 
72 	return rsp->clk;
73 }
74 
75 static u64 ptp_tstmp_read(struct otx2_ptp *ptp)
76 {
77 	struct ptp_req *req;
78 	struct ptp_rsp *rsp;
79 	int err;
80 
81 	if (!ptp->nic)
82 		return 0;
83 
84 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
85 	if (!req)
86 		return 0;
87 
88 	req->op = PTP_OP_GET_TSTMP;
89 
90 	err = otx2_sync_mbox_msg(&ptp->nic->mbox);
91 	if (err)
92 		return 0;
93 
94 	rsp = (struct ptp_rsp *)otx2_mbox_get_rsp(&ptp->nic->mbox.mbox, 0,
95 						  &req->hdr);
96 	if (IS_ERR(rsp))
97 		return 0;
98 
99 	return rsp->clk;
100 }
101 
102 static int otx2_ptp_adjtime(struct ptp_clock_info *ptp_info, s64 delta)
103 {
104 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
105 					    ptp_info);
106 	struct otx2_nic *pfvf = ptp->nic;
107 
108 	mutex_lock(&pfvf->mbox.lock);
109 	timecounter_adjtime(&ptp->time_counter, delta);
110 	mutex_unlock(&pfvf->mbox.lock);
111 
112 	return 0;
113 }
114 
115 static int otx2_ptp_gettime(struct ptp_clock_info *ptp_info,
116 			    struct timespec64 *ts)
117 {
118 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
119 					    ptp_info);
120 	struct otx2_nic *pfvf = ptp->nic;
121 	u64 nsec;
122 
123 	mutex_lock(&pfvf->mbox.lock);
124 	nsec = timecounter_read(&ptp->time_counter);
125 	mutex_unlock(&pfvf->mbox.lock);
126 
127 	*ts = ns_to_timespec64(nsec);
128 
129 	return 0;
130 }
131 
132 static int otx2_ptp_settime(struct ptp_clock_info *ptp_info,
133 			    const struct timespec64 *ts)
134 {
135 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
136 					    ptp_info);
137 	struct otx2_nic *pfvf = ptp->nic;
138 	u64 nsec;
139 
140 	nsec = timespec64_to_ns(ts);
141 
142 	mutex_lock(&pfvf->mbox.lock);
143 	timecounter_init(&ptp->time_counter, &ptp->cycle_counter, nsec);
144 	mutex_unlock(&pfvf->mbox.lock);
145 
146 	return 0;
147 }
148 
149 static int otx2_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
150 			       enum ptp_pin_function func, unsigned int chan)
151 {
152 	switch (func) {
153 	case PTP_PF_NONE:
154 	case PTP_PF_EXTTS:
155 		break;
156 	case PTP_PF_PEROUT:
157 	case PTP_PF_PHYSYNC:
158 		return -1;
159 	}
160 	return 0;
161 }
162 
163 static void otx2_ptp_extts_check(struct work_struct *work)
164 {
165 	struct otx2_ptp *ptp = container_of(work, struct otx2_ptp,
166 					    extts_work.work);
167 	struct ptp_clock_event event;
168 	u64 tstmp, new_thresh;
169 
170 	mutex_lock(&ptp->nic->mbox.lock);
171 	tstmp = ptp_tstmp_read(ptp);
172 	mutex_unlock(&ptp->nic->mbox.lock);
173 
174 	if (tstmp != ptp->last_extts) {
175 		event.type = PTP_CLOCK_EXTTS;
176 		event.index = 0;
177 		event.timestamp = timecounter_cyc2time(&ptp->time_counter, tstmp);
178 		ptp_clock_event(ptp->ptp_clock, &event);
179 		ptp->last_extts = tstmp;
180 
181 		new_thresh = tstmp % 500000000;
182 		if (ptp->thresh != new_thresh) {
183 			mutex_lock(&ptp->nic->mbox.lock);
184 			ptp_set_thresh(ptp, new_thresh);
185 			mutex_unlock(&ptp->nic->mbox.lock);
186 			ptp->thresh = new_thresh;
187 		}
188 	}
189 	schedule_delayed_work(&ptp->extts_work, msecs_to_jiffies(200));
190 }
191 
192 static int otx2_ptp_enable(struct ptp_clock_info *ptp_info,
193 			   struct ptp_clock_request *rq, int on)
194 {
195 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
196 					    ptp_info);
197 	int pin = -1;
198 
199 	if (!ptp->nic)
200 		return -ENODEV;
201 
202 	switch (rq->type) {
203 	case PTP_CLK_REQ_EXTTS:
204 		pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS,
205 				   rq->extts.index);
206 		if (pin < 0)
207 			return -EBUSY;
208 		if (on)
209 			schedule_delayed_work(&ptp->extts_work, msecs_to_jiffies(200));
210 		else
211 			cancel_delayed_work_sync(&ptp->extts_work);
212 		return 0;
213 	default:
214 		break;
215 	}
216 	return -EOPNOTSUPP;
217 }
218 
219 int otx2_ptp_init(struct otx2_nic *pfvf)
220 {
221 	struct otx2_ptp *ptp_ptr;
222 	struct cyclecounter *cc;
223 	struct ptp_req *req;
224 	int err;
225 
226 	mutex_lock(&pfvf->mbox.lock);
227 	/* check if PTP block is available */
228 	req = otx2_mbox_alloc_msg_ptp_op(&pfvf->mbox);
229 	if (!req) {
230 		mutex_unlock(&pfvf->mbox.lock);
231 		return -ENOMEM;
232 	}
233 
234 	req->op = PTP_OP_GET_CLOCK;
235 
236 	err = otx2_sync_mbox_msg(&pfvf->mbox);
237 	if (err) {
238 		mutex_unlock(&pfvf->mbox.lock);
239 		return err;
240 	}
241 	mutex_unlock(&pfvf->mbox.lock);
242 
243 	ptp_ptr = kzalloc(sizeof(*ptp_ptr), GFP_KERNEL);
244 	if (!ptp_ptr) {
245 		err = -ENOMEM;
246 		goto error;
247 	}
248 
249 	ptp_ptr->nic = pfvf;
250 
251 	cc = &ptp_ptr->cycle_counter;
252 	cc->read = ptp_cc_read;
253 	cc->mask = CYCLECOUNTER_MASK(64);
254 	cc->mult = 1;
255 	cc->shift = 0;
256 
257 	timecounter_init(&ptp_ptr->time_counter, &ptp_ptr->cycle_counter,
258 			 ktime_to_ns(ktime_get_real()));
259 
260 	snprintf(ptp_ptr->extts_config.name, sizeof(ptp_ptr->extts_config.name), "TSTAMP");
261 	ptp_ptr->extts_config.index = 0;
262 	ptp_ptr->extts_config.func = PTP_PF_NONE;
263 
264 	ptp_ptr->ptp_info = (struct ptp_clock_info) {
265 		.owner          = THIS_MODULE,
266 		.name           = "OcteonTX2 PTP",
267 		.max_adj        = 1000000000ull,
268 		.n_ext_ts       = 1,
269 		.n_pins         = 1,
270 		.pps            = 0,
271 		.pin_config     = &ptp_ptr->extts_config,
272 		.adjfine        = otx2_ptp_adjfine,
273 		.adjtime        = otx2_ptp_adjtime,
274 		.gettime64      = otx2_ptp_gettime,
275 		.settime64      = otx2_ptp_settime,
276 		.enable         = otx2_ptp_enable,
277 		.verify         = otx2_ptp_verify_pin,
278 	};
279 
280 	INIT_DELAYED_WORK(&ptp_ptr->extts_work, otx2_ptp_extts_check);
281 
282 	ptp_ptr->ptp_clock = ptp_clock_register(&ptp_ptr->ptp_info, pfvf->dev);
283 	if (IS_ERR_OR_NULL(ptp_ptr->ptp_clock)) {
284 		err = ptp_ptr->ptp_clock ?
285 		      PTR_ERR(ptp_ptr->ptp_clock) : -ENODEV;
286 		kfree(ptp_ptr);
287 		goto error;
288 	}
289 
290 	pfvf->ptp = ptp_ptr;
291 
292 error:
293 	return err;
294 }
295 
296 void otx2_ptp_destroy(struct otx2_nic *pfvf)
297 {
298 	struct otx2_ptp *ptp = pfvf->ptp;
299 
300 	if (!ptp)
301 		return;
302 
303 	ptp_clock_unregister(ptp->ptp_clock);
304 	kfree(ptp);
305 	pfvf->ptp = NULL;
306 }
307 
308 int otx2_ptp_clock_index(struct otx2_nic *pfvf)
309 {
310 	if (!pfvf->ptp)
311 		return -ENODEV;
312 
313 	return ptp_clock_index(pfvf->ptp->ptp_clock);
314 }
315 
316 int otx2_ptp_tstamp2time(struct otx2_nic *pfvf, u64 tstamp, u64 *tsns)
317 {
318 	if (!pfvf->ptp)
319 		return -ENODEV;
320 
321 	*tsns = timecounter_cyc2time(&pfvf->ptp->time_counter, tstamp);
322 
323 	return 0;
324 }
325