1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021-2022, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include <linux/tty_driver.h>
7 
8 /**
9  * ice_gnss_do_write - Write data to internal GNSS
10  * @pf: board private structure
11  * @buf: command buffer
12  * @size: command buffer size
13  *
14  * Write UBX command data to the GNSS receiver
15  */
16 static unsigned int
17 ice_gnss_do_write(struct ice_pf *pf, unsigned char *buf, unsigned int size)
18 {
19 	struct ice_aqc_link_topo_addr link_topo;
20 	struct ice_hw *hw = &pf->hw;
21 	unsigned int offset = 0;
22 	int err = 0;
23 
24 	memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr));
25 	link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS;
26 	link_topo.topo_params.node_type_ctx |=
27 		FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
28 			   ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE);
29 
30 	/* It's not possible to write a single byte to u-blox.
31 	 * Write all bytes in a loop until there are 6 or less bytes left. If
32 	 * there are exactly 6 bytes left, the last write would be only a byte.
33 	 * In this case, do 4+2 bytes writes instead of 5+1. Otherwise, do the
34 	 * last 2 to 5 bytes write.
35 	 */
36 	while (size - offset > ICE_GNSS_UBX_WRITE_BYTES + 1) {
37 		err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
38 				       cpu_to_le16(buf[offset]),
39 				       ICE_MAX_I2C_WRITE_BYTES,
40 				       &buf[offset + 1], NULL);
41 		if (err)
42 			goto err_out;
43 
44 		offset += ICE_GNSS_UBX_WRITE_BYTES;
45 	}
46 
47 	/* Single byte would be written. Write 4 bytes instead of 5. */
48 	if (size - offset == ICE_GNSS_UBX_WRITE_BYTES + 1) {
49 		err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
50 				       cpu_to_le16(buf[offset]),
51 				       ICE_MAX_I2C_WRITE_BYTES - 1,
52 				       &buf[offset + 1], NULL);
53 		if (err)
54 			goto err_out;
55 
56 		offset += ICE_GNSS_UBX_WRITE_BYTES - 1;
57 	}
58 
59 	/* Do the last write, 2 to 5 bytes. */
60 	err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
61 			       cpu_to_le16(buf[offset]), size - offset - 1,
62 			       &buf[offset + 1], NULL);
63 	if (err)
64 		goto err_out;
65 
66 	return size;
67 
68 err_out:
69 	dev_err(ice_pf_to_dev(pf), "GNSS failed to write, offset=%u, size=%u, err=%d\n",
70 		offset, size, err);
71 
72 	return offset;
73 }
74 
75 /**
76  * ice_gnss_write_pending - Write all pending data to internal GNSS
77  * @work: GNSS write work structure
78  */
79 static void ice_gnss_write_pending(struct kthread_work *work)
80 {
81 	struct gnss_serial *gnss = container_of(work, struct gnss_serial,
82 						write_work);
83 	struct ice_pf *pf = gnss->back;
84 
85 	if (!list_empty(&gnss->queue)) {
86 		struct gnss_write_buf *write_buf = NULL;
87 		unsigned int bytes;
88 
89 		write_buf = list_first_entry(&gnss->queue,
90 					     struct gnss_write_buf, queue);
91 
92 		bytes = ice_gnss_do_write(pf, write_buf->buf, write_buf->size);
93 		dev_dbg(ice_pf_to_dev(pf), "%u bytes written to GNSS\n", bytes);
94 
95 		list_del(&write_buf->queue);
96 		kfree(write_buf->buf);
97 		kfree(write_buf);
98 	}
99 }
100 
101 /**
102  * ice_gnss_read - Read data from internal GNSS module
103  * @work: GNSS read work structure
104  *
105  * Read the data from internal GNSS receiver, number of bytes read will be
106  * returned in *read_data parameter.
107  */
108 static void ice_gnss_read(struct kthread_work *work)
109 {
110 	struct gnss_serial *gnss = container_of(work, struct gnss_serial,
111 						read_work.work);
112 	struct ice_aqc_link_topo_addr link_topo;
113 	unsigned int i, bytes_read, data_len;
114 	struct tty_port *port;
115 	struct ice_pf *pf;
116 	struct ice_hw *hw;
117 	__be16 data_len_b;
118 	char *buf = NULL;
119 	u8 i2c_params;
120 	int err = 0;
121 
122 	pf = gnss->back;
123 	if (!pf || !gnss->tty || !gnss->tty->port) {
124 		err = -EFAULT;
125 		goto exit;
126 	}
127 
128 	hw = &pf->hw;
129 	port = gnss->tty->port;
130 
131 	buf = (char *)get_zeroed_page(GFP_KERNEL);
132 	if (!buf) {
133 		err = -ENOMEM;
134 		goto exit;
135 	}
136 
137 	memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr));
138 	link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS;
139 	link_topo.topo_params.node_type_ctx |=
140 		FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
141 			   ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE);
142 
143 	i2c_params = ICE_GNSS_UBX_DATA_LEN_WIDTH |
144 		     ICE_AQC_I2C_USE_REPEATED_START;
145 
146 	/* Read data length in a loop, when it's not 0 the data is ready */
147 	for (i = 0; i < ICE_MAX_UBX_READ_TRIES; i++) {
148 		err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
149 				      cpu_to_le16(ICE_GNSS_UBX_DATA_LEN_H),
150 				      i2c_params, (u8 *)&data_len_b, NULL);
151 		if (err)
152 			goto exit_buf;
153 
154 		data_len = be16_to_cpu(data_len_b);
155 		if (data_len != 0 && data_len != U16_MAX)
156 			break;
157 
158 		mdelay(10);
159 	}
160 
161 	data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
162 	data_len = tty_buffer_request_room(port, data_len);
163 	if (!data_len) {
164 		err = -ENOMEM;
165 		goto exit_buf;
166 	}
167 
168 	/* Read received data */
169 	for (i = 0; i < data_len; i += bytes_read) {
170 		unsigned int bytes_left = data_len - i;
171 
172 		bytes_read = min_t(typeof(bytes_left), bytes_left,
173 				   ICE_MAX_I2C_DATA_SIZE);
174 
175 		err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
176 				      cpu_to_le16(ICE_GNSS_UBX_EMPTY_DATA),
177 				      bytes_read, &buf[i], NULL);
178 		if (err)
179 			goto exit_buf;
180 	}
181 
182 	/* Send the data to the tty layer for users to read. This doesn't
183 	 * actually push the data through unless tty->low_latency is set.
184 	 */
185 	tty_insert_flip_string(port, buf, i);
186 	tty_flip_buffer_push(port);
187 
188 exit_buf:
189 	free_page((unsigned long)buf);
190 	kthread_queue_delayed_work(gnss->kworker, &gnss->read_work,
191 				   ICE_GNSS_TIMER_DELAY_TIME);
192 exit:
193 	if (err)
194 		dev_dbg(ice_pf_to_dev(pf), "GNSS failed to read err=%d\n", err);
195 }
196 
197 /**
198  * ice_gnss_struct_init - Initialize GNSS structure for the TTY
199  * @pf: Board private structure
200  * @index: TTY device index
201  */
202 static struct gnss_serial *ice_gnss_struct_init(struct ice_pf *pf, int index)
203 {
204 	struct device *dev = ice_pf_to_dev(pf);
205 	struct kthread_worker *kworker;
206 	struct gnss_serial *gnss;
207 
208 	gnss = kzalloc(sizeof(*gnss), GFP_KERNEL);
209 	if (!gnss)
210 		return NULL;
211 
212 	mutex_init(&gnss->gnss_mutex);
213 	gnss->open_count = 0;
214 	gnss->back = pf;
215 	pf->gnss_serial[index] = gnss;
216 
217 	kthread_init_delayed_work(&gnss->read_work, ice_gnss_read);
218 	INIT_LIST_HEAD(&gnss->queue);
219 	kthread_init_work(&gnss->write_work, ice_gnss_write_pending);
220 	/* Allocate a kworker for handling work required for the GNSS TTY
221 	 * writes.
222 	 */
223 	kworker = kthread_create_worker(0, "ice-gnss-%s", dev_name(dev));
224 	if (IS_ERR(kworker)) {
225 		kfree(gnss);
226 		return NULL;
227 	}
228 
229 	gnss->kworker = kworker;
230 
231 	return gnss;
232 }
233 
234 /**
235  * ice_gnss_tty_open - Initialize GNSS structures on TTY device open
236  * @tty: pointer to the tty_struct
237  * @filp: pointer to the file
238  *
239  * This routine is mandatory. If this routine is not filled in, the attempted
240  * open will fail with ENODEV.
241  */
242 static int ice_gnss_tty_open(struct tty_struct *tty, struct file *filp)
243 {
244 	struct gnss_serial *gnss;
245 	struct ice_pf *pf;
246 
247 	pf = (struct ice_pf *)tty->driver->driver_state;
248 	if (!pf)
249 		return -EFAULT;
250 
251 	/* Clear the pointer in case something fails */
252 	tty->driver_data = NULL;
253 
254 	/* Get the serial object associated with this tty pointer */
255 	gnss = pf->gnss_serial[tty->index];
256 	if (!gnss) {
257 		/* Initialize GNSS struct on the first device open */
258 		gnss = ice_gnss_struct_init(pf, tty->index);
259 		if (!gnss)
260 			return -ENOMEM;
261 	}
262 
263 	mutex_lock(&gnss->gnss_mutex);
264 
265 	/* Save our structure within the tty structure */
266 	tty->driver_data = gnss;
267 	gnss->tty = tty;
268 	gnss->open_count++;
269 	kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, 0);
270 
271 	mutex_unlock(&gnss->gnss_mutex);
272 
273 	return 0;
274 }
275 
276 /**
277  * ice_gnss_tty_close - Cleanup GNSS structures on tty device close
278  * @tty: pointer to the tty_struct
279  * @filp: pointer to the file
280  */
281 static void ice_gnss_tty_close(struct tty_struct *tty, struct file *filp)
282 {
283 	struct gnss_serial *gnss = tty->driver_data;
284 	struct ice_pf *pf;
285 
286 	if (!gnss)
287 		return;
288 
289 	pf = (struct ice_pf *)tty->driver->driver_state;
290 	if (!pf)
291 		return;
292 
293 	mutex_lock(&gnss->gnss_mutex);
294 
295 	if (!gnss->open_count) {
296 		/* Port was never opened */
297 		dev_err(ice_pf_to_dev(pf), "GNSS port not opened\n");
298 		goto exit;
299 	}
300 
301 	gnss->open_count--;
302 	if (gnss->open_count <= 0) {
303 		/* Port is in shutdown state */
304 		kthread_cancel_delayed_work_sync(&gnss->read_work);
305 	}
306 exit:
307 	mutex_unlock(&gnss->gnss_mutex);
308 }
309 
310 /**
311  * ice_gnss_tty_write - Write GNSS data
312  * @tty: pointer to the tty_struct
313  * @buf: pointer to the user data
314  * @count: the number of characters queued to be sent to the HW
315  *
316  * The write function call is called by the user when there is data to be sent
317  * to the hardware. First the tty core receives the call, and then it passes the
318  * data on to the tty driver's write function. The tty core also tells the tty
319  * driver the size of the data being sent.
320  * If any errors happen during the write call, a negative error value should be
321  * returned instead of the number of characters queued to be written.
322  */
323 static int
324 ice_gnss_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
325 {
326 	struct gnss_write_buf *write_buf;
327 	struct gnss_serial *gnss;
328 	unsigned char *cmd_buf;
329 	struct ice_pf *pf;
330 	int err = count;
331 
332 	/* We cannot write a single byte using our I2C implementation. */
333 	if (count <= 1 || count > ICE_GNSS_TTY_WRITE_BUF)
334 		return -EINVAL;
335 
336 	gnss = tty->driver_data;
337 	if (!gnss)
338 		return -EFAULT;
339 
340 	pf = (struct ice_pf *)tty->driver->driver_state;
341 	if (!pf)
342 		return -EFAULT;
343 
344 	/* Only allow to write on TTY 0 */
345 	if (gnss != pf->gnss_serial[0])
346 		return -EIO;
347 
348 	mutex_lock(&gnss->gnss_mutex);
349 
350 	if (!gnss->open_count) {
351 		err = -EINVAL;
352 		goto exit;
353 	}
354 
355 	cmd_buf = kcalloc(count, sizeof(*buf), GFP_KERNEL);
356 	if (!cmd_buf) {
357 		err = -ENOMEM;
358 		goto exit;
359 	}
360 
361 	memcpy(cmd_buf, buf, count);
362 
363 	/* Send the data out to a hardware port */
364 	write_buf = kzalloc(sizeof(*write_buf), GFP_KERNEL);
365 	if (!write_buf) {
366 		err = -ENOMEM;
367 		goto exit;
368 	}
369 
370 	write_buf->buf = cmd_buf;
371 	write_buf->size = count;
372 	INIT_LIST_HEAD(&write_buf->queue);
373 	list_add_tail(&write_buf->queue, &gnss->queue);
374 	kthread_queue_work(gnss->kworker, &gnss->write_work);
375 exit:
376 	mutex_unlock(&gnss->gnss_mutex);
377 	return err;
378 }
379 
380 /**
381  * ice_gnss_tty_write_room - Returns the numbers of characters to be written.
382  * @tty: pointer to the tty_struct
383  *
384  * This routine returns the numbers of characters the tty driver will accept
385  * for queuing to be written or 0 if either the TTY is not open or user
386  * tries to write to the TTY other than the first.
387  */
388 static unsigned int ice_gnss_tty_write_room(struct tty_struct *tty)
389 {
390 	struct gnss_serial *gnss = tty->driver_data;
391 
392 	/* Only allow to write on TTY 0 */
393 	if (!gnss || gnss != gnss->back->gnss_serial[0])
394 		return 0;
395 
396 	mutex_lock(&gnss->gnss_mutex);
397 
398 	if (!gnss->open_count) {
399 		mutex_unlock(&gnss->gnss_mutex);
400 		return 0;
401 	}
402 
403 	mutex_unlock(&gnss->gnss_mutex);
404 	return ICE_GNSS_TTY_WRITE_BUF;
405 }
406 
407 static const struct tty_operations tty_gps_ops = {
408 	.open =		ice_gnss_tty_open,
409 	.close =	ice_gnss_tty_close,
410 	.write =	ice_gnss_tty_write,
411 	.write_room =	ice_gnss_tty_write_room,
412 };
413 
414 /**
415  * ice_gnss_create_tty_driver - Create a TTY driver for GNSS
416  * @pf: Board private structure
417  */
418 static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf)
419 {
420 	struct device *dev = ice_pf_to_dev(pf);
421 	const int ICE_TTYDRV_NAME_MAX = 14;
422 	struct tty_driver *tty_driver;
423 	char *ttydrv_name;
424 	unsigned int i;
425 	int err;
426 
427 	tty_driver = tty_alloc_driver(ICE_GNSS_TTY_MINOR_DEVICES,
428 				      TTY_DRIVER_REAL_RAW);
429 	if (IS_ERR(tty_driver)) {
430 		dev_err(dev, "Failed to allocate memory for GNSS TTY\n");
431 		return NULL;
432 	}
433 
434 	ttydrv_name = kzalloc(ICE_TTYDRV_NAME_MAX, GFP_KERNEL);
435 	if (!ttydrv_name) {
436 		tty_driver_kref_put(tty_driver);
437 		return NULL;
438 	}
439 
440 	snprintf(ttydrv_name, ICE_TTYDRV_NAME_MAX, "ttyGNSS_%02x%02x_",
441 		 (u8)pf->pdev->bus->number, (u8)PCI_SLOT(pf->pdev->devfn));
442 
443 	/* Initialize the tty driver*/
444 	tty_driver->owner = THIS_MODULE;
445 	tty_driver->driver_name = dev_driver_string(dev);
446 	tty_driver->name = (const char *)ttydrv_name;
447 	tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
448 	tty_driver->subtype = SERIAL_TYPE_NORMAL;
449 	tty_driver->init_termios = tty_std_termios;
450 	tty_driver->init_termios.c_iflag &= ~INLCR;
451 	tty_driver->init_termios.c_iflag |= IGNCR;
452 	tty_driver->init_termios.c_oflag &= ~OPOST;
453 	tty_driver->init_termios.c_lflag &= ~ICANON;
454 	tty_driver->init_termios.c_cflag &= ~(CSIZE | CBAUD | CBAUDEX);
455 	/* baud rate 9600 */
456 	tty_termios_encode_baud_rate(&tty_driver->init_termios, 9600, 9600);
457 	tty_driver->driver_state = pf;
458 	tty_set_operations(tty_driver, &tty_gps_ops);
459 
460 	for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
461 		pf->gnss_tty_port[i] = kzalloc(sizeof(*pf->gnss_tty_port[i]),
462 					       GFP_KERNEL);
463 		pf->gnss_serial[i] = NULL;
464 
465 		tty_port_init(pf->gnss_tty_port[i]);
466 		tty_port_link_device(pf->gnss_tty_port[i], tty_driver, i);
467 	}
468 
469 	err = tty_register_driver(tty_driver);
470 	if (err) {
471 		dev_err(dev, "Failed to register TTY driver err=%d\n", err);
472 
473 		for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
474 			tty_port_destroy(pf->gnss_tty_port[i]);
475 			kfree(pf->gnss_tty_port[i]);
476 		}
477 		kfree(ttydrv_name);
478 		tty_driver_kref_put(pf->ice_gnss_tty_driver);
479 
480 		return NULL;
481 	}
482 
483 	for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++)
484 		dev_info(dev, "%s%d registered\n", ttydrv_name, i);
485 
486 	return tty_driver;
487 }
488 
489 /**
490  * ice_gnss_init - Initialize GNSS TTY support
491  * @pf: Board private structure
492  */
493 void ice_gnss_init(struct ice_pf *pf)
494 {
495 	struct tty_driver *tty_driver;
496 
497 	tty_driver = ice_gnss_create_tty_driver(pf);
498 	if (!tty_driver)
499 		return;
500 
501 	pf->ice_gnss_tty_driver = tty_driver;
502 
503 	set_bit(ICE_FLAG_GNSS, pf->flags);
504 	dev_info(ice_pf_to_dev(pf), "GNSS TTY init successful\n");
505 }
506 
507 /**
508  * ice_gnss_exit - Disable GNSS TTY support
509  * @pf: Board private structure
510  */
511 void ice_gnss_exit(struct ice_pf *pf)
512 {
513 	unsigned int i;
514 
515 	if (!test_bit(ICE_FLAG_GNSS, pf->flags) || !pf->ice_gnss_tty_driver)
516 		return;
517 
518 	for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
519 		if (pf->gnss_tty_port[i]) {
520 			tty_port_destroy(pf->gnss_tty_port[i]);
521 			kfree(pf->gnss_tty_port[i]);
522 		}
523 
524 		if (pf->gnss_serial[i]) {
525 			struct gnss_serial *gnss = pf->gnss_serial[i];
526 
527 			kthread_cancel_work_sync(&gnss->write_work);
528 			kthread_cancel_delayed_work_sync(&gnss->read_work);
529 			kfree(gnss);
530 			pf->gnss_serial[i] = NULL;
531 		}
532 	}
533 
534 	tty_unregister_driver(pf->ice_gnss_tty_driver);
535 	kfree(pf->ice_gnss_tty_driver->name);
536 	tty_driver_kref_put(pf->ice_gnss_tty_driver);
537 	pf->ice_gnss_tty_driver = NULL;
538 }
539 
540 /**
541  * ice_gnss_is_gps_present - Check if GPS HW is present
542  * @hw: pointer to HW struct
543  */
544 bool ice_gnss_is_gps_present(struct ice_hw *hw)
545 {
546 	if (!hw->func_caps.ts_func_info.src_tmr_owned)
547 		return false;
548 
549 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
550 	if (ice_is_e810t(hw)) {
551 		int err;
552 		u8 data;
553 
554 		err = ice_read_pca9575_reg_e810t(hw, ICE_PCA9575_P0_IN, &data);
555 		if (err || !!(data & ICE_E810T_P0_GNSS_PRSNT_N))
556 			return false;
557 	} else {
558 		return false;
559 	}
560 #else
561 	if (!ice_is_e810t(hw))
562 		return false;
563 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
564 
565 	return true;
566 }
567