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 kfree(cmd_buf); 367 err = -ENOMEM; 368 goto exit; 369 } 370 371 write_buf->buf = cmd_buf; 372 write_buf->size = count; 373 INIT_LIST_HEAD(&write_buf->queue); 374 list_add_tail(&write_buf->queue, &gnss->queue); 375 kthread_queue_work(gnss->kworker, &gnss->write_work); 376 exit: 377 mutex_unlock(&gnss->gnss_mutex); 378 return err; 379 } 380 381 /** 382 * ice_gnss_tty_write_room - Returns the numbers of characters to be written. 383 * @tty: pointer to the tty_struct 384 * 385 * This routine returns the numbers of characters the tty driver will accept 386 * for queuing to be written or 0 if either the TTY is not open or user 387 * tries to write to the TTY other than the first. 388 */ 389 static unsigned int ice_gnss_tty_write_room(struct tty_struct *tty) 390 { 391 struct gnss_serial *gnss = tty->driver_data; 392 393 /* Only allow to write on TTY 0 */ 394 if (!gnss || gnss != gnss->back->gnss_serial[0]) 395 return 0; 396 397 mutex_lock(&gnss->gnss_mutex); 398 399 if (!gnss->open_count) { 400 mutex_unlock(&gnss->gnss_mutex); 401 return 0; 402 } 403 404 mutex_unlock(&gnss->gnss_mutex); 405 return ICE_GNSS_TTY_WRITE_BUF; 406 } 407 408 static const struct tty_operations tty_gps_ops = { 409 .open = ice_gnss_tty_open, 410 .close = ice_gnss_tty_close, 411 .write = ice_gnss_tty_write, 412 .write_room = ice_gnss_tty_write_room, 413 }; 414 415 /** 416 * ice_gnss_create_tty_driver - Create a TTY driver for GNSS 417 * @pf: Board private structure 418 */ 419 static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf) 420 { 421 struct device *dev = ice_pf_to_dev(pf); 422 const int ICE_TTYDRV_NAME_MAX = 14; 423 struct tty_driver *tty_driver; 424 char *ttydrv_name; 425 unsigned int i; 426 int err; 427 428 tty_driver = tty_alloc_driver(ICE_GNSS_TTY_MINOR_DEVICES, 429 TTY_DRIVER_REAL_RAW); 430 if (IS_ERR(tty_driver)) { 431 dev_err(dev, "Failed to allocate memory for GNSS TTY\n"); 432 return NULL; 433 } 434 435 ttydrv_name = kzalloc(ICE_TTYDRV_NAME_MAX, GFP_KERNEL); 436 if (!ttydrv_name) { 437 tty_driver_kref_put(tty_driver); 438 return NULL; 439 } 440 441 snprintf(ttydrv_name, ICE_TTYDRV_NAME_MAX, "ttyGNSS_%02x%02x_", 442 (u8)pf->pdev->bus->number, (u8)PCI_SLOT(pf->pdev->devfn)); 443 444 /* Initialize the tty driver*/ 445 tty_driver->owner = THIS_MODULE; 446 tty_driver->driver_name = dev_driver_string(dev); 447 tty_driver->name = (const char *)ttydrv_name; 448 tty_driver->type = TTY_DRIVER_TYPE_SERIAL; 449 tty_driver->subtype = SERIAL_TYPE_NORMAL; 450 tty_driver->init_termios = tty_std_termios; 451 tty_driver->init_termios.c_iflag &= ~INLCR; 452 tty_driver->init_termios.c_iflag |= IGNCR; 453 tty_driver->init_termios.c_oflag &= ~OPOST; 454 tty_driver->init_termios.c_lflag &= ~ICANON; 455 tty_driver->init_termios.c_cflag &= ~(CSIZE | CBAUD | CBAUDEX); 456 /* baud rate 9600 */ 457 tty_termios_encode_baud_rate(&tty_driver->init_termios, 9600, 9600); 458 tty_driver->driver_state = pf; 459 tty_set_operations(tty_driver, &tty_gps_ops); 460 461 for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) { 462 pf->gnss_tty_port[i] = kzalloc(sizeof(*pf->gnss_tty_port[i]), 463 GFP_KERNEL); 464 if (!pf->gnss_tty_port[i]) 465 goto err_out; 466 467 pf->gnss_serial[i] = NULL; 468 469 tty_port_init(pf->gnss_tty_port[i]); 470 tty_port_link_device(pf->gnss_tty_port[i], tty_driver, i); 471 } 472 473 err = tty_register_driver(tty_driver); 474 if (err) { 475 dev_err(dev, "Failed to register TTY driver err=%d\n", err); 476 goto err_out; 477 } 478 479 for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) 480 dev_info(dev, "%s%d registered\n", ttydrv_name, i); 481 482 return tty_driver; 483 484 err_out: 485 while (i--) { 486 tty_port_destroy(pf->gnss_tty_port[i]); 487 kfree(pf->gnss_tty_port[i]); 488 } 489 kfree(ttydrv_name); 490 tty_driver_kref_put(pf->ice_gnss_tty_driver); 491 492 return NULL; 493 } 494 495 /** 496 * ice_gnss_init - Initialize GNSS TTY support 497 * @pf: Board private structure 498 */ 499 void ice_gnss_init(struct ice_pf *pf) 500 { 501 struct tty_driver *tty_driver; 502 503 tty_driver = ice_gnss_create_tty_driver(pf); 504 if (!tty_driver) 505 return; 506 507 pf->ice_gnss_tty_driver = tty_driver; 508 509 set_bit(ICE_FLAG_GNSS, pf->flags); 510 dev_info(ice_pf_to_dev(pf), "GNSS TTY init successful\n"); 511 } 512 513 /** 514 * ice_gnss_exit - Disable GNSS TTY support 515 * @pf: Board private structure 516 */ 517 void ice_gnss_exit(struct ice_pf *pf) 518 { 519 unsigned int i; 520 521 if (!test_bit(ICE_FLAG_GNSS, pf->flags) || !pf->ice_gnss_tty_driver) 522 return; 523 524 for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) { 525 if (pf->gnss_tty_port[i]) { 526 tty_port_destroy(pf->gnss_tty_port[i]); 527 kfree(pf->gnss_tty_port[i]); 528 } 529 530 if (pf->gnss_serial[i]) { 531 struct gnss_serial *gnss = pf->gnss_serial[i]; 532 533 kthread_cancel_work_sync(&gnss->write_work); 534 kthread_cancel_delayed_work_sync(&gnss->read_work); 535 kfree(gnss); 536 pf->gnss_serial[i] = NULL; 537 } 538 } 539 540 tty_unregister_driver(pf->ice_gnss_tty_driver); 541 kfree(pf->ice_gnss_tty_driver->name); 542 tty_driver_kref_put(pf->ice_gnss_tty_driver); 543 pf->ice_gnss_tty_driver = NULL; 544 } 545 546 /** 547 * ice_gnss_is_gps_present - Check if GPS HW is present 548 * @hw: pointer to HW struct 549 */ 550 bool ice_gnss_is_gps_present(struct ice_hw *hw) 551 { 552 if (!hw->func_caps.ts_func_info.src_tmr_owned) 553 return false; 554 555 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK) 556 if (ice_is_e810t(hw)) { 557 int err; 558 u8 data; 559 560 err = ice_read_pca9575_reg_e810t(hw, ICE_PCA9575_P0_IN, &data); 561 if (err || !!(data & ICE_E810T_P0_GNSS_PRSNT_N)) 562 return false; 563 } else { 564 return false; 565 } 566 #else 567 if (!ice_is_e810t(hw)) 568 return false; 569 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */ 570 571 return true; 572 } 573