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 7 /** 8 * ice_gnss_do_write - Write data to internal GNSS receiver 9 * @pf: board private structure 10 * @buf: command buffer 11 * @size: command buffer size 12 * 13 * Write UBX command data to the GNSS receiver 14 * 15 * Return: 16 * * number of bytes written - success 17 * * negative - error code 18 */ 19 static unsigned int 20 ice_gnss_do_write(struct ice_pf *pf, unsigned char *buf, unsigned int size) 21 { 22 struct ice_aqc_link_topo_addr link_topo; 23 struct ice_hw *hw = &pf->hw; 24 unsigned int offset = 0; 25 int err = 0; 26 27 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr)); 28 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS; 29 link_topo.topo_params.node_type_ctx |= 30 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M, 31 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE); 32 33 /* It's not possible to write a single byte to u-blox. 34 * Write all bytes in a loop until there are 6 or less bytes left. If 35 * there are exactly 6 bytes left, the last write would be only a byte. 36 * In this case, do 4+2 bytes writes instead of 5+1. Otherwise, do the 37 * last 2 to 5 bytes write. 38 */ 39 while (size - offset > ICE_GNSS_UBX_WRITE_BYTES + 1) { 40 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR, 41 cpu_to_le16(buf[offset]), 42 ICE_MAX_I2C_WRITE_BYTES, 43 &buf[offset + 1], NULL); 44 if (err) 45 goto err_out; 46 47 offset += ICE_GNSS_UBX_WRITE_BYTES; 48 } 49 50 /* Single byte would be written. Write 4 bytes instead of 5. */ 51 if (size - offset == ICE_GNSS_UBX_WRITE_BYTES + 1) { 52 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR, 53 cpu_to_le16(buf[offset]), 54 ICE_MAX_I2C_WRITE_BYTES - 1, 55 &buf[offset + 1], NULL); 56 if (err) 57 goto err_out; 58 59 offset += ICE_GNSS_UBX_WRITE_BYTES - 1; 60 } 61 62 /* Do the last write, 2 to 5 bytes. */ 63 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR, 64 cpu_to_le16(buf[offset]), size - offset - 1, 65 &buf[offset + 1], NULL); 66 if (err) 67 goto err_out; 68 69 return size; 70 71 err_out: 72 dev_err(ice_pf_to_dev(pf), "GNSS failed to write, offset=%u, size=%u, err=%d\n", 73 offset, size, err); 74 75 return offset; 76 } 77 78 /** 79 * ice_gnss_write_pending - Write all pending data to internal GNSS 80 * @work: GNSS write work structure 81 */ 82 static void ice_gnss_write_pending(struct kthread_work *work) 83 { 84 struct gnss_serial *gnss = container_of(work, struct gnss_serial, 85 write_work); 86 struct ice_pf *pf = gnss->back; 87 88 if (!pf) 89 return; 90 91 if (!test_bit(ICE_FLAG_GNSS, pf->flags)) 92 return; 93 94 if (!list_empty(&gnss->queue)) { 95 struct gnss_write_buf *write_buf = NULL; 96 unsigned int bytes; 97 98 write_buf = list_first_entry(&gnss->queue, 99 struct gnss_write_buf, queue); 100 101 bytes = ice_gnss_do_write(pf, write_buf->buf, write_buf->size); 102 dev_dbg(ice_pf_to_dev(pf), "%u bytes written to GNSS\n", bytes); 103 104 list_del(&write_buf->queue); 105 kfree(write_buf->buf); 106 kfree(write_buf); 107 } 108 } 109 110 /** 111 * ice_gnss_read - Read data from internal GNSS module 112 * @work: GNSS read work structure 113 * 114 * Read the data from internal GNSS receiver, write it to gnss_dev. 115 */ 116 static void ice_gnss_read(struct kthread_work *work) 117 { 118 struct gnss_serial *gnss = container_of(work, struct gnss_serial, 119 read_work.work); 120 unsigned int i, bytes_read, data_len, count; 121 struct ice_aqc_link_topo_addr link_topo; 122 struct ice_pf *pf; 123 struct ice_hw *hw; 124 __be16 data_len_b; 125 char *buf = NULL; 126 u8 i2c_params; 127 int err = 0; 128 129 pf = gnss->back; 130 if (!pf) { 131 err = -EFAULT; 132 goto exit; 133 } 134 135 if (!test_bit(ICE_FLAG_GNSS, pf->flags)) 136 return; 137 138 hw = &pf->hw; 139 buf = (char *)get_zeroed_page(GFP_KERNEL); 140 if (!buf) { 141 err = -ENOMEM; 142 goto exit; 143 } 144 145 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr)); 146 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS; 147 link_topo.topo_params.node_type_ctx |= 148 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M, 149 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE); 150 151 i2c_params = ICE_GNSS_UBX_DATA_LEN_WIDTH | 152 ICE_AQC_I2C_USE_REPEATED_START; 153 154 /* Read data length in a loop, when it's not 0 the data is ready */ 155 for (i = 0; i < ICE_MAX_UBX_READ_TRIES; i++) { 156 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR, 157 cpu_to_le16(ICE_GNSS_UBX_DATA_LEN_H), 158 i2c_params, (u8 *)&data_len_b, NULL); 159 if (err) 160 goto exit_buf; 161 162 data_len = be16_to_cpu(data_len_b); 163 if (data_len != 0 && data_len != U16_MAX) 164 break; 165 166 mdelay(10); 167 } 168 169 data_len = min_t(typeof(data_len), data_len, PAGE_SIZE); 170 if (!data_len) { 171 err = -ENOMEM; 172 goto exit_buf; 173 } 174 175 /* Read received data */ 176 for (i = 0; i < data_len; i += bytes_read) { 177 unsigned int bytes_left = data_len - i; 178 179 bytes_read = min_t(typeof(bytes_left), bytes_left, 180 ICE_MAX_I2C_DATA_SIZE); 181 182 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR, 183 cpu_to_le16(ICE_GNSS_UBX_EMPTY_DATA), 184 bytes_read, &buf[i], NULL); 185 if (err) 186 goto exit_buf; 187 } 188 189 count = gnss_insert_raw(pf->gnss_dev, buf, i); 190 if (count != i) 191 dev_warn(ice_pf_to_dev(pf), 192 "gnss_insert_raw ret=%d size=%d\n", 193 count, i); 194 exit_buf: 195 free_page((unsigned long)buf); 196 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, 197 ICE_GNSS_TIMER_DELAY_TIME); 198 exit: 199 if (err) 200 dev_dbg(ice_pf_to_dev(pf), "GNSS failed to read err=%d\n", err); 201 } 202 203 /** 204 * ice_gnss_struct_init - Initialize GNSS receiver 205 * @pf: Board private structure 206 * 207 * Initialize GNSS structures and workers. 208 * 209 * Return: 210 * * pointer to initialized gnss_serial struct - success 211 * * NULL - error 212 */ 213 static struct gnss_serial *ice_gnss_struct_init(struct ice_pf *pf) 214 { 215 struct device *dev = ice_pf_to_dev(pf); 216 struct kthread_worker *kworker; 217 struct gnss_serial *gnss; 218 219 gnss = kzalloc(sizeof(*gnss), GFP_KERNEL); 220 if (!gnss) 221 return NULL; 222 223 gnss->back = pf; 224 pf->gnss_serial = gnss; 225 226 kthread_init_delayed_work(&gnss->read_work, ice_gnss_read); 227 INIT_LIST_HEAD(&gnss->queue); 228 kthread_init_work(&gnss->write_work, ice_gnss_write_pending); 229 kworker = kthread_create_worker(0, "ice-gnss-%s", dev_name(dev)); 230 if (IS_ERR(kworker)) { 231 kfree(gnss); 232 return NULL; 233 } 234 235 gnss->kworker = kworker; 236 237 return gnss; 238 } 239 240 /** 241 * ice_gnss_open - Open GNSS device 242 * @gdev: pointer to the gnss device struct 243 * 244 * Open GNSS device and start filling the read buffer for consumer. 245 * 246 * Return: 247 * * 0 - success 248 * * negative - error code 249 */ 250 static int ice_gnss_open(struct gnss_device *gdev) 251 { 252 struct ice_pf *pf = gnss_get_drvdata(gdev); 253 struct gnss_serial *gnss; 254 255 if (!pf) 256 return -EFAULT; 257 258 if (!test_bit(ICE_FLAG_GNSS, pf->flags)) 259 return -EFAULT; 260 261 gnss = pf->gnss_serial; 262 if (!gnss) 263 return -ENODEV; 264 265 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, 0); 266 267 return 0; 268 } 269 270 /** 271 * ice_gnss_close - Close GNSS device 272 * @gdev: pointer to the gnss device struct 273 * 274 * Close GNSS device, cancel worker, stop filling the read buffer. 275 */ 276 static void ice_gnss_close(struct gnss_device *gdev) 277 { 278 struct ice_pf *pf = gnss_get_drvdata(gdev); 279 struct gnss_serial *gnss; 280 281 if (!pf) 282 return; 283 284 gnss = pf->gnss_serial; 285 if (!gnss) 286 return; 287 288 kthread_cancel_work_sync(&gnss->write_work); 289 kthread_cancel_delayed_work_sync(&gnss->read_work); 290 } 291 292 /** 293 * ice_gnss_write - Write to GNSS device 294 * @gdev: pointer to the gnss device struct 295 * @buf: pointer to the user data 296 * @count: size of the buffer to be sent to the GNSS device 297 * 298 * Return: 299 * * number of written bytes - success 300 * * negative - error code 301 */ 302 static int 303 ice_gnss_write(struct gnss_device *gdev, const unsigned char *buf, 304 size_t count) 305 { 306 struct ice_pf *pf = gnss_get_drvdata(gdev); 307 struct gnss_write_buf *write_buf; 308 struct gnss_serial *gnss; 309 unsigned char *cmd_buf; 310 int err = count; 311 312 /* We cannot write a single byte using our I2C implementation. */ 313 if (count <= 1 || count > ICE_GNSS_TTY_WRITE_BUF) 314 return -EINVAL; 315 316 if (!pf) 317 return -EFAULT; 318 319 if (!test_bit(ICE_FLAG_GNSS, pf->flags)) 320 return -EFAULT; 321 322 gnss = pf->gnss_serial; 323 if (!gnss) 324 return -ENODEV; 325 326 cmd_buf = kcalloc(count, sizeof(*buf), GFP_KERNEL); 327 if (!cmd_buf) 328 return -ENOMEM; 329 330 memcpy(cmd_buf, buf, count); 331 write_buf = kzalloc(sizeof(*write_buf), GFP_KERNEL); 332 if (!write_buf) { 333 kfree(cmd_buf); 334 return -ENOMEM; 335 } 336 337 write_buf->buf = cmd_buf; 338 write_buf->size = count; 339 INIT_LIST_HEAD(&write_buf->queue); 340 list_add_tail(&write_buf->queue, &gnss->queue); 341 kthread_queue_work(gnss->kworker, &gnss->write_work); 342 343 return err; 344 } 345 346 static const struct gnss_operations ice_gnss_ops = { 347 .open = ice_gnss_open, 348 .close = ice_gnss_close, 349 .write_raw = ice_gnss_write, 350 }; 351 352 /** 353 * ice_gnss_register - Register GNSS receiver 354 * @pf: Board private structure 355 * 356 * Allocate and register GNSS receiver in the Linux GNSS subsystem. 357 * 358 * Return: 359 * * 0 - success 360 * * negative - error code 361 */ 362 static int ice_gnss_register(struct ice_pf *pf) 363 { 364 struct gnss_device *gdev; 365 int ret; 366 367 gdev = gnss_allocate_device(ice_pf_to_dev(pf)); 368 if (!gdev) { 369 dev_err(ice_pf_to_dev(pf), 370 "gnss_allocate_device returns NULL\n"); 371 return -ENOMEM; 372 } 373 374 gdev->ops = &ice_gnss_ops; 375 gdev->type = GNSS_TYPE_UBX; 376 gnss_set_drvdata(gdev, pf); 377 ret = gnss_register_device(gdev); 378 if (ret) { 379 dev_err(ice_pf_to_dev(pf), "gnss_register_device err=%d\n", 380 ret); 381 gnss_put_device(gdev); 382 } else { 383 pf->gnss_dev = gdev; 384 } 385 386 return ret; 387 } 388 389 /** 390 * ice_gnss_deregister - Deregister GNSS receiver 391 * @pf: Board private structure 392 * 393 * Deregister GNSS receiver from the Linux GNSS subsystem, 394 * release its resources. 395 */ 396 static void ice_gnss_deregister(struct ice_pf *pf) 397 { 398 if (pf->gnss_dev) { 399 gnss_deregister_device(pf->gnss_dev); 400 gnss_put_device(pf->gnss_dev); 401 pf->gnss_dev = NULL; 402 } 403 } 404 405 /** 406 * ice_gnss_init - Initialize GNSS support 407 * @pf: Board private structure 408 */ 409 void ice_gnss_init(struct ice_pf *pf) 410 { 411 int ret; 412 413 pf->gnss_serial = ice_gnss_struct_init(pf); 414 if (!pf->gnss_serial) 415 return; 416 417 ret = ice_gnss_register(pf); 418 if (!ret) { 419 set_bit(ICE_FLAG_GNSS, pf->flags); 420 dev_info(ice_pf_to_dev(pf), "GNSS init successful\n"); 421 } else { 422 ice_gnss_exit(pf); 423 dev_err(ice_pf_to_dev(pf), "GNSS init failure\n"); 424 } 425 } 426 427 /** 428 * ice_gnss_exit - Disable GNSS TTY support 429 * @pf: Board private structure 430 */ 431 void ice_gnss_exit(struct ice_pf *pf) 432 { 433 ice_gnss_deregister(pf); 434 clear_bit(ICE_FLAG_GNSS, pf->flags); 435 436 if (pf->gnss_serial) { 437 struct gnss_serial *gnss = pf->gnss_serial; 438 439 kthread_cancel_work_sync(&gnss->write_work); 440 kthread_cancel_delayed_work_sync(&gnss->read_work); 441 kthread_destroy_worker(gnss->kworker); 442 gnss->kworker = NULL; 443 444 kfree(gnss); 445 pf->gnss_serial = NULL; 446 } 447 } 448 449 /** 450 * ice_gnss_is_gps_present - Check if GPS HW is present 451 * @hw: pointer to HW struct 452 */ 453 bool ice_gnss_is_gps_present(struct ice_hw *hw) 454 { 455 if (!hw->func_caps.ts_func_info.src_tmr_owned) 456 return false; 457 458 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK) 459 if (ice_is_e810t(hw)) { 460 int err; 461 u8 data; 462 463 err = ice_read_pca9575_reg_e810t(hw, ICE_PCA9575_P0_IN, &data); 464 if (err || !!(data & ICE_E810T_P0_GNSS_PRSNT_N)) 465 return false; 466 } else { 467 return false; 468 } 469 #else 470 if (!ice_is_e810t(hw)) 471 return false; 472 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */ 473 474 return true; 475 } 476