1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021-2022, Intel Corporation. */
6 #include <linux/tty_driver.h>
9 * ice_gnss_do_write - Write data to internal GNSS
10 * @pf: board private structure
11 * @buf: command buffer
12 * @size: command buffer size
14 * Write UBX command data to the GNSS receiver
17 ice_gnss_do_write(struct ice_pf *pf, unsigned char *buf, unsigned int size)
19 struct ice_aqc_link_topo_addr link_topo;
20 struct ice_hw *hw = &pf->hw;
21 unsigned int offset = 0;
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);
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.
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);
44 offset += ICE_GNSS_UBX_WRITE_BYTES;
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);
56 offset += ICE_GNSS_UBX_WRITE_BYTES - 1;
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);
69 dev_err(ice_pf_to_dev(pf), "GNSS failed to write, offset=%u, size=%u, err=%d\n",
76 * ice_gnss_write_pending - Write all pending data to internal GNSS
77 * @work: GNSS write work structure
79 static void ice_gnss_write_pending(struct kthread_work *work)
81 struct gnss_serial *gnss = container_of(work, struct gnss_serial,
83 struct ice_pf *pf = gnss->back;
85 if (!list_empty(&gnss->queue)) {
86 struct gnss_write_buf *write_buf = NULL;
89 write_buf = list_first_entry(&gnss->queue,
90 struct gnss_write_buf, queue);
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);
95 list_del(&write_buf->queue);
96 kfree(write_buf->buf);
102 * ice_gnss_read - Read data from internal GNSS module
103 * @work: GNSS read work structure
105 * Read the data from internal GNSS receiver, number of bytes read will be
106 * returned in *read_data parameter.
108 static void ice_gnss_read(struct kthread_work *work)
110 struct gnss_serial *gnss = container_of(work, struct gnss_serial,
112 struct ice_aqc_link_topo_addr link_topo;
113 unsigned int i, bytes_read, data_len;
114 struct tty_port *port;
123 if (!pf || !gnss->tty || !gnss->tty->port) {
129 port = gnss->tty->port;
131 buf = (char *)get_zeroed_page(GFP_KERNEL);
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);
143 i2c_params = ICE_GNSS_UBX_DATA_LEN_WIDTH |
144 ICE_AQC_I2C_USE_REPEATED_START;
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);
154 data_len = be16_to_cpu(data_len_b);
155 if (data_len != 0 && data_len != U16_MAX)
161 data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
162 data_len = tty_buffer_request_room(port, data_len);
168 /* Read received data */
169 for (i = 0; i < data_len; i += bytes_read) {
170 unsigned int bytes_left = data_len - i;
172 bytes_read = min_t(typeof(bytes_left), bytes_left,
173 ICE_MAX_I2C_DATA_SIZE);
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);
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.
185 tty_insert_flip_string(port, buf, i);
186 tty_flip_buffer_push(port);
189 free_page((unsigned long)buf);
190 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work,
191 ICE_GNSS_TIMER_DELAY_TIME);
194 dev_dbg(ice_pf_to_dev(pf), "GNSS failed to read err=%d\n", err);
198 * ice_gnss_struct_init - Initialize GNSS structure for the TTY
199 * @pf: Board private structure
200 * @index: TTY device index
202 static struct gnss_serial *ice_gnss_struct_init(struct ice_pf *pf, int index)
204 struct device *dev = ice_pf_to_dev(pf);
205 struct kthread_worker *kworker;
206 struct gnss_serial *gnss;
208 gnss = kzalloc(sizeof(*gnss), GFP_KERNEL);
212 mutex_init(&gnss->gnss_mutex);
213 gnss->open_count = 0;
215 pf->gnss_serial[index] = gnss;
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
223 kworker = kthread_create_worker(0, "ice-gnss-%s", dev_name(dev));
224 if (IS_ERR(kworker)) {
229 gnss->kworker = kworker;
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
239 * This routine is mandatory. If this routine is not filled in, the attempted
240 * open will fail with ENODEV.
242 static int ice_gnss_tty_open(struct tty_struct *tty, struct file *filp)
244 struct gnss_serial *gnss;
247 pf = (struct ice_pf *)tty->driver->driver_state;
251 /* Clear the pointer in case something fails */
252 tty->driver_data = NULL;
254 /* Get the serial object associated with this tty pointer */
255 gnss = pf->gnss_serial[tty->index];
257 /* Initialize GNSS struct on the first device open */
258 gnss = ice_gnss_struct_init(pf, tty->index);
263 mutex_lock(&gnss->gnss_mutex);
265 /* Save our structure within the tty structure */
266 tty->driver_data = gnss;
269 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, 0);
271 mutex_unlock(&gnss->gnss_mutex);
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
281 static void ice_gnss_tty_close(struct tty_struct *tty, struct file *filp)
283 struct gnss_serial *gnss = tty->driver_data;
289 pf = (struct ice_pf *)tty->driver->driver_state;
293 mutex_lock(&gnss->gnss_mutex);
295 if (!gnss->open_count) {
296 /* Port was never opened */
297 dev_err(ice_pf_to_dev(pf), "GNSS port not opened\n");
302 if (gnss->open_count <= 0) {
303 /* Port is in shutdown state */
304 kthread_cancel_delayed_work_sync(&gnss->read_work);
307 mutex_unlock(&gnss->gnss_mutex);
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
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.
324 ice_gnss_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
326 struct gnss_write_buf *write_buf;
327 struct gnss_serial *gnss;
328 unsigned char *cmd_buf;
332 /* We cannot write a single byte using our I2C implementation. */
333 if (count <= 1 || count > ICE_GNSS_TTY_WRITE_BUF)
336 gnss = tty->driver_data;
340 pf = (struct ice_pf *)tty->driver->driver_state;
344 /* Only allow to write on TTY 0 */
345 if (gnss != pf->gnss_serial[0])
348 mutex_lock(&gnss->gnss_mutex);
350 if (!gnss->open_count) {
355 cmd_buf = kcalloc(count, sizeof(*buf), GFP_KERNEL);
361 memcpy(cmd_buf, buf, count);
363 /* Send the data out to a hardware port */
364 write_buf = kzalloc(sizeof(*write_buf), GFP_KERNEL);
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);
376 mutex_unlock(&gnss->gnss_mutex);
381 * ice_gnss_tty_write_room - Returns the numbers of characters to be written.
382 * @tty: pointer to the tty_struct
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.
388 static unsigned int ice_gnss_tty_write_room(struct tty_struct *tty)
390 struct gnss_serial *gnss = tty->driver_data;
392 /* Only allow to write on TTY 0 */
393 if (!gnss || gnss != gnss->back->gnss_serial[0])
396 mutex_lock(&gnss->gnss_mutex);
398 if (!gnss->open_count) {
399 mutex_unlock(&gnss->gnss_mutex);
403 mutex_unlock(&gnss->gnss_mutex);
404 return ICE_GNSS_TTY_WRITE_BUF;
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,
415 * ice_gnss_create_tty_driver - Create a TTY driver for GNSS
416 * @pf: Board private structure
418 static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf)
420 struct device *dev = ice_pf_to_dev(pf);
421 const int ICE_TTYDRV_NAME_MAX = 14;
422 struct tty_driver *tty_driver;
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");
434 ttydrv_name = kzalloc(ICE_TTYDRV_NAME_MAX, GFP_KERNEL);
436 tty_driver_kref_put(tty_driver);
440 snprintf(ttydrv_name, ICE_TTYDRV_NAME_MAX, "ttyGNSS_%02x%02x_",
441 (u8)pf->pdev->bus->number, (u8)PCI_SLOT(pf->pdev->devfn));
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);
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);
460 for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
461 pf->gnss_tty_port[i] = kzalloc(sizeof(*pf->gnss_tty_port[i]),
463 pf->gnss_serial[i] = NULL;
465 tty_port_init(pf->gnss_tty_port[i]);
466 tty_port_link_device(pf->gnss_tty_port[i], tty_driver, i);
469 err = tty_register_driver(tty_driver);
471 dev_err(dev, "Failed to register TTY driver err=%d\n", err);
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]);
478 tty_driver_kref_put(pf->ice_gnss_tty_driver);
483 for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++)
484 dev_info(dev, "%s%d registered\n", ttydrv_name, i);
490 * ice_gnss_init - Initialize GNSS TTY support
491 * @pf: Board private structure
493 void ice_gnss_init(struct ice_pf *pf)
495 struct tty_driver *tty_driver;
497 tty_driver = ice_gnss_create_tty_driver(pf);
501 pf->ice_gnss_tty_driver = tty_driver;
503 set_bit(ICE_FLAG_GNSS, pf->flags);
504 dev_info(ice_pf_to_dev(pf), "GNSS TTY init successful\n");
508 * ice_gnss_exit - Disable GNSS TTY support
509 * @pf: Board private structure
511 void ice_gnss_exit(struct ice_pf *pf)
515 if (!test_bit(ICE_FLAG_GNSS, pf->flags) || !pf->ice_gnss_tty_driver)
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]);
524 if (pf->gnss_serial[i]) {
525 struct gnss_serial *gnss = pf->gnss_serial[i];
527 kthread_cancel_work_sync(&gnss->write_work);
528 kthread_cancel_delayed_work_sync(&gnss->read_work);
530 pf->gnss_serial[i] = NULL;
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;
541 * ice_gnss_is_gps_present - Check if GPS HW is present
542 * @hw: pointer to HW struct
544 bool ice_gnss_is_gps_present(struct ice_hw *hw)
546 if (!hw->func_caps.ts_func_info.src_tmr_owned)
549 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
550 if (ice_is_e810t(hw)) {
554 err = ice_read_pca9575_reg_e810t(hw, ICE_PCA9575_P0_IN, &data);
555 if (err || !!(data & ICE_E810T_P0_GNSS_PRSNT_N))
561 if (!ice_is_e810t(hw))
563 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */