1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 
8 #include <linux/module.h>
9 
10 #include "otx2_common.h"
11 #include "otx2_ptp.h"
12 
13 static bool is_tstmp_atomic_update_supported(struct otx2_ptp *ptp)
14 {
15 	struct ptp_get_cap_rsp *rsp;
16 	struct msg_req *req;
17 	int err;
18 
19 	if (!ptp->nic)
20 		return false;
21 
22 	mutex_lock(&ptp->nic->mbox.lock);
23 	req = otx2_mbox_alloc_msg_ptp_get_cap(&ptp->nic->mbox);
24 	if (!req) {
25 		mutex_unlock(&ptp->nic->mbox.lock);
26 		return false;
27 	}
28 
29 	err = otx2_sync_mbox_msg(&ptp->nic->mbox);
30 	if (err) {
31 		mutex_unlock(&ptp->nic->mbox.lock);
32 		return false;
33 	}
34 	rsp = (struct ptp_get_cap_rsp *)otx2_mbox_get_rsp(&ptp->nic->mbox.mbox, 0,
35 							  &req->hdr);
36 	mutex_unlock(&ptp->nic->mbox.lock);
37 
38 	if (IS_ERR(rsp))
39 		return false;
40 
41 	if (rsp->cap & PTP_CAP_HW_ATOMIC_UPDATE)
42 		return true;
43 
44 	return false;
45 }
46 
47 static int otx2_ptp_hw_adjtime(struct ptp_clock_info *ptp_info, s64 delta)
48 {
49 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
50 					    ptp_info);
51 	struct otx2_nic *pfvf = ptp->nic;
52 	struct ptp_req *req;
53 	int rc;
54 
55 	if (!ptp->nic)
56 		return -ENODEV;
57 
58 	mutex_lock(&pfvf->mbox.lock);
59 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
60 	if (!req) {
61 		mutex_unlock(&pfvf->mbox.lock);
62 		return -ENOMEM;
63 	}
64 	req->op = PTP_OP_ADJTIME;
65 	req->delta = delta;
66 	rc = otx2_sync_mbox_msg(&ptp->nic->mbox);
67 	mutex_unlock(&pfvf->mbox.lock);
68 
69 	return rc;
70 }
71 
72 static u64 otx2_ptp_get_clock(struct otx2_ptp *ptp)
73 {
74 	struct ptp_req *req;
75 	struct ptp_rsp *rsp;
76 	int err;
77 
78 	if (!ptp->nic)
79 		return 0;
80 
81 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
82 	if (!req)
83 		return 0;
84 
85 	req->op = PTP_OP_GET_CLOCK;
86 
87 	err = otx2_sync_mbox_msg(&ptp->nic->mbox);
88 	if (err)
89 		return 0;
90 
91 	rsp = (struct ptp_rsp *)otx2_mbox_get_rsp(&ptp->nic->mbox.mbox, 0,
92 						  &req->hdr);
93 	if (IS_ERR(rsp))
94 		return 0;
95 
96 	return rsp->clk;
97 }
98 
99 static int otx2_ptp_hw_gettime(struct ptp_clock_info *ptp_info,
100 			       struct timespec64 *ts)
101 {
102 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
103 					    ptp_info);
104 	u64 tstamp;
105 
106 	tstamp = otx2_ptp_get_clock(ptp);
107 
108 	*ts = ns_to_timespec64(tstamp);
109 	return 0;
110 }
111 
112 static int otx2_ptp_hw_settime(struct ptp_clock_info *ptp_info,
113 			       const struct timespec64 *ts)
114 {
115 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
116 					    ptp_info);
117 	struct otx2_nic *pfvf = ptp->nic;
118 	struct ptp_req *req;
119 	u64 nsec;
120 	int rc;
121 
122 	if (!ptp->nic)
123 		return -ENODEV;
124 
125 	nsec = timespec64_to_ns(ts);
126 
127 	mutex_lock(&pfvf->mbox.lock);
128 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
129 	if (!req) {
130 		mutex_unlock(&pfvf->mbox.lock);
131 		return -ENOMEM;
132 	}
133 
134 	req->op = PTP_OP_SET_CLOCK;
135 	req->clk = nsec;
136 	rc = otx2_sync_mbox_msg(&ptp->nic->mbox);
137 	mutex_unlock(&pfvf->mbox.lock);
138 
139 	return rc;
140 }
141 
142 static int otx2_ptp_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
143 {
144 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
145 					    ptp_info);
146 	struct ptp_req *req;
147 
148 	if (!ptp->nic)
149 		return -ENODEV;
150 
151 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
152 	if (!req)
153 		return -ENOMEM;
154 
155 	req->op = PTP_OP_ADJFINE;
156 	req->scaled_ppm = scaled_ppm;
157 
158 	return otx2_sync_mbox_msg(&ptp->nic->mbox);
159 }
160 
161 static int ptp_set_thresh(struct otx2_ptp *ptp, u64 thresh)
162 {
163 	struct ptp_req *req;
164 
165 	if (!ptp->nic)
166 		return -ENODEV;
167 
168 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
169 	if (!req)
170 		return -ENOMEM;
171 
172 	req->op = PTP_OP_SET_THRESH;
173 	req->thresh = thresh;
174 
175 	return otx2_sync_mbox_msg(&ptp->nic->mbox);
176 }
177 
178 static int ptp_extts_on(struct otx2_ptp *ptp, int on)
179 {
180 	struct ptp_req *req;
181 
182 	if (!ptp->nic)
183 		return -ENODEV;
184 
185 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
186 	if (!req)
187 		return -ENOMEM;
188 
189 	req->op = PTP_OP_EXTTS_ON;
190 	req->extts_on = on;
191 
192 	return otx2_sync_mbox_msg(&ptp->nic->mbox);
193 }
194 
195 static u64 ptp_cc_read(const struct cyclecounter *cc)
196 {
197 	struct otx2_ptp *ptp = container_of(cc, struct otx2_ptp, cycle_counter);
198 
199 	return otx2_ptp_get_clock(ptp);
200 }
201 
202 static u64 ptp_tstmp_read(struct otx2_ptp *ptp)
203 {
204 	struct ptp_req *req;
205 	struct ptp_rsp *rsp;
206 	int err;
207 
208 	if (!ptp->nic)
209 		return 0;
210 
211 	req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
212 	if (!req)
213 		return 0;
214 
215 	req->op = PTP_OP_GET_TSTMP;
216 
217 	err = otx2_sync_mbox_msg(&ptp->nic->mbox);
218 	if (err)
219 		return 0;
220 
221 	rsp = (struct ptp_rsp *)otx2_mbox_get_rsp(&ptp->nic->mbox.mbox, 0,
222 						  &req->hdr);
223 	if (IS_ERR(rsp))
224 		return 0;
225 
226 	return rsp->clk;
227 }
228 
229 static int otx2_ptp_tc_adjtime(struct ptp_clock_info *ptp_info, s64 delta)
230 {
231 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
232 					    ptp_info);
233 	struct otx2_nic *pfvf = ptp->nic;
234 
235 	mutex_lock(&pfvf->mbox.lock);
236 	timecounter_adjtime(&ptp->time_counter, delta);
237 	mutex_unlock(&pfvf->mbox.lock);
238 
239 	return 0;
240 }
241 
242 static int otx2_ptp_tc_gettime(struct ptp_clock_info *ptp_info,
243 			       struct timespec64 *ts)
244 {
245 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
246 					    ptp_info);
247 	u64 tstamp;
248 
249 	mutex_lock(&ptp->nic->mbox.lock);
250 	tstamp = timecounter_read(&ptp->time_counter);
251 	mutex_unlock(&ptp->nic->mbox.lock);
252 	*ts = ns_to_timespec64(tstamp);
253 
254 	return 0;
255 }
256 
257 static int otx2_ptp_tc_settime(struct ptp_clock_info *ptp_info,
258 			       const struct timespec64 *ts)
259 {
260 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
261 					    ptp_info);
262 	u64 nsec;
263 
264 	nsec = timespec64_to_ns(ts);
265 
266 	mutex_lock(&ptp->nic->mbox.lock);
267 	timecounter_init(&ptp->time_counter, &ptp->cycle_counter, nsec);
268 	mutex_unlock(&ptp->nic->mbox.lock);
269 
270 	return 0;
271 }
272 
273 static int otx2_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
274 			       enum ptp_pin_function func, unsigned int chan)
275 {
276 	switch (func) {
277 	case PTP_PF_NONE:
278 	case PTP_PF_EXTTS:
279 		break;
280 	case PTP_PF_PEROUT:
281 	case PTP_PF_PHYSYNC:
282 		return -1;
283 	}
284 	return 0;
285 }
286 
287 static u64 otx2_ptp_hw_tstamp2time(const struct timecounter *time_counter, u64 tstamp)
288 {
289 	/* On HW which supports atomic updates, timecounter is not initialized */
290 	return tstamp;
291 }
292 
293 static void otx2_ptp_extts_check(struct work_struct *work)
294 {
295 	struct otx2_ptp *ptp = container_of(work, struct otx2_ptp,
296 					    extts_work.work);
297 	struct ptp_clock_event event;
298 	u64 tstmp, new_thresh;
299 
300 	mutex_lock(&ptp->nic->mbox.lock);
301 	tstmp = ptp_tstmp_read(ptp);
302 	mutex_unlock(&ptp->nic->mbox.lock);
303 
304 	if (tstmp != ptp->last_extts) {
305 		event.type = PTP_CLOCK_EXTTS;
306 		event.index = 0;
307 		event.timestamp = ptp->ptp_tstamp2nsec(&ptp->time_counter, tstmp);
308 		ptp_clock_event(ptp->ptp_clock, &event);
309 		new_thresh = tstmp % 500000000;
310 		if (ptp->thresh != new_thresh) {
311 			mutex_lock(&ptp->nic->mbox.lock);
312 			ptp_set_thresh(ptp, new_thresh);
313 			mutex_unlock(&ptp->nic->mbox.lock);
314 			ptp->thresh = new_thresh;
315 		}
316 		ptp->last_extts = tstmp;
317 	}
318 	schedule_delayed_work(&ptp->extts_work, msecs_to_jiffies(200));
319 }
320 
321 static void otx2_sync_tstamp(struct work_struct *work)
322 {
323 	struct otx2_ptp *ptp = container_of(work, struct otx2_ptp,
324 					    synctstamp_work.work);
325 	struct otx2_nic *pfvf = ptp->nic;
326 	u64 tstamp;
327 
328 	mutex_lock(&pfvf->mbox.lock);
329 	tstamp = otx2_ptp_get_clock(ptp);
330 	mutex_unlock(&pfvf->mbox.lock);
331 
332 	ptp->tstamp = ptp->ptp_tstamp2nsec(&ptp->time_counter, tstamp);
333 	ptp->base_ns = tstamp % NSEC_PER_SEC;
334 
335 	schedule_delayed_work(&ptp->synctstamp_work, msecs_to_jiffies(250));
336 }
337 
338 static int otx2_ptp_enable(struct ptp_clock_info *ptp_info,
339 			   struct ptp_clock_request *rq, int on)
340 {
341 	struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
342 					    ptp_info);
343 	int pin;
344 
345 	if (!ptp->nic)
346 		return -ENODEV;
347 
348 	switch (rq->type) {
349 	case PTP_CLK_REQ_EXTTS:
350 		pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS,
351 				   rq->extts.index);
352 		if (pin < 0)
353 			return -EBUSY;
354 		if (on) {
355 			ptp_extts_on(ptp, on);
356 			schedule_delayed_work(&ptp->extts_work, msecs_to_jiffies(200));
357 		} else {
358 			ptp_extts_on(ptp, on);
359 			cancel_delayed_work_sync(&ptp->extts_work);
360 		}
361 		return 0;
362 	default:
363 		break;
364 	}
365 	return -EOPNOTSUPP;
366 }
367 
368 int otx2_ptp_init(struct otx2_nic *pfvf)
369 {
370 	struct otx2_ptp *ptp_ptr;
371 	struct cyclecounter *cc;
372 	struct ptp_req *req;
373 	int err;
374 
375 	if (is_otx2_lbkvf(pfvf->pdev)) {
376 		pfvf->ptp = NULL;
377 		return 0;
378 	}
379 
380 	mutex_lock(&pfvf->mbox.lock);
381 	/* check if PTP block is available */
382 	req = otx2_mbox_alloc_msg_ptp_op(&pfvf->mbox);
383 	if (!req) {
384 		mutex_unlock(&pfvf->mbox.lock);
385 		return -ENOMEM;
386 	}
387 
388 	req->op = PTP_OP_GET_CLOCK;
389 
390 	err = otx2_sync_mbox_msg(&pfvf->mbox);
391 	if (err) {
392 		mutex_unlock(&pfvf->mbox.lock);
393 		return err;
394 	}
395 	mutex_unlock(&pfvf->mbox.lock);
396 
397 	ptp_ptr = kzalloc(sizeof(*ptp_ptr), GFP_KERNEL);
398 	if (!ptp_ptr) {
399 		err = -ENOMEM;
400 		goto error;
401 	}
402 
403 	ptp_ptr->nic = pfvf;
404 
405 	snprintf(ptp_ptr->extts_config.name, sizeof(ptp_ptr->extts_config.name), "TSTAMP");
406 	ptp_ptr->extts_config.index = 0;
407 	ptp_ptr->extts_config.func = PTP_PF_NONE;
408 
409 	ptp_ptr->ptp_info = (struct ptp_clock_info) {
410 		.owner          = THIS_MODULE,
411 		.name           = "OcteonTX2 PTP",
412 		.max_adj        = 1000000000ull,
413 		.n_ext_ts       = 1,
414 		.n_pins         = 1,
415 		.pps            = 0,
416 		.pin_config     = &ptp_ptr->extts_config,
417 		.adjfine        = otx2_ptp_adjfine,
418 		.enable         = otx2_ptp_enable,
419 		.verify         = otx2_ptp_verify_pin,
420 	};
421 
422 	/* Check whether hardware supports atomic updates to timestamp */
423 	if (is_tstmp_atomic_update_supported(ptp_ptr)) {
424 		ptp_ptr->ptp_info.adjtime = otx2_ptp_hw_adjtime;
425 		ptp_ptr->ptp_info.gettime64 = otx2_ptp_hw_gettime;
426 		ptp_ptr->ptp_info.settime64 = otx2_ptp_hw_settime;
427 
428 		ptp_ptr->ptp_tstamp2nsec = otx2_ptp_hw_tstamp2time;
429 	} else {
430 		ptp_ptr->ptp_info.adjtime = otx2_ptp_tc_adjtime;
431 		ptp_ptr->ptp_info.gettime64 = otx2_ptp_tc_gettime;
432 		ptp_ptr->ptp_info.settime64 = otx2_ptp_tc_settime;
433 
434 		cc = &ptp_ptr->cycle_counter;
435 		cc->read = ptp_cc_read;
436 		cc->mask = CYCLECOUNTER_MASK(64);
437 		cc->mult = 1;
438 		cc->shift = 0;
439 		ptp_ptr->ptp_tstamp2nsec = timecounter_cyc2time;
440 
441 		timecounter_init(&ptp_ptr->time_counter, &ptp_ptr->cycle_counter,
442 				 ktime_to_ns(ktime_get_real()));
443 	}
444 
445 	INIT_DELAYED_WORK(&ptp_ptr->extts_work, otx2_ptp_extts_check);
446 
447 	ptp_ptr->ptp_clock = ptp_clock_register(&ptp_ptr->ptp_info, pfvf->dev);
448 	if (IS_ERR_OR_NULL(ptp_ptr->ptp_clock)) {
449 		err = ptp_ptr->ptp_clock ?
450 		      PTR_ERR(ptp_ptr->ptp_clock) : -ENODEV;
451 		kfree(ptp_ptr);
452 		goto error;
453 	}
454 
455 	if (is_dev_otx2(pfvf->pdev)) {
456 		ptp_ptr->convert_rx_ptp_tstmp = &otx2_ptp_convert_rx_timestamp;
457 		ptp_ptr->convert_tx_ptp_tstmp = &otx2_ptp_convert_tx_timestamp;
458 	} else {
459 		ptp_ptr->convert_rx_ptp_tstmp = &cn10k_ptp_convert_timestamp;
460 		ptp_ptr->convert_tx_ptp_tstmp = &cn10k_ptp_convert_timestamp;
461 	}
462 
463 	INIT_DELAYED_WORK(&ptp_ptr->synctstamp_work, otx2_sync_tstamp);
464 
465 	pfvf->ptp = ptp_ptr;
466 
467 error:
468 	return err;
469 }
470 EXPORT_SYMBOL_GPL(otx2_ptp_init);
471 
472 void otx2_ptp_destroy(struct otx2_nic *pfvf)
473 {
474 	struct otx2_ptp *ptp = pfvf->ptp;
475 
476 	if (!ptp)
477 		return;
478 
479 	cancel_delayed_work(&pfvf->ptp->synctstamp_work);
480 
481 	ptp_clock_unregister(ptp->ptp_clock);
482 	kfree(ptp);
483 	pfvf->ptp = NULL;
484 }
485 EXPORT_SYMBOL_GPL(otx2_ptp_destroy);
486 
487 int otx2_ptp_clock_index(struct otx2_nic *pfvf)
488 {
489 	if (!pfvf->ptp)
490 		return -ENODEV;
491 
492 	return ptp_clock_index(pfvf->ptp->ptp_clock);
493 }
494 EXPORT_SYMBOL_GPL(otx2_ptp_clock_index);
495 
496 int otx2_ptp_tstamp2time(struct otx2_nic *pfvf, u64 tstamp, u64 *tsns)
497 {
498 	if (!pfvf->ptp)
499 		return -ENODEV;
500 
501 	*tsns = pfvf->ptp->ptp_tstamp2nsec(&pfvf->ptp->time_counter, tstamp);
502 
503 	return 0;
504 }
505 EXPORT_SYMBOL_GPL(otx2_ptp_tstamp2time);
506 
507 MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
508 MODULE_DESCRIPTION("Marvell RVU NIC PTP Driver");
509 MODULE_LICENSE("GPL v2");
510