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 long delay = ICE_GNSS_POLL_DATA_DELAY_TIME; 121 unsigned int i, bytes_read, data_len, count; 122 struct ice_aqc_link_topo_addr link_topo; 123 struct ice_pf *pf; 124 struct ice_hw *hw; 125 __be16 data_len_b; 126 char *buf = NULL; 127 u8 i2c_params; 128 int err = 0; 129 130 pf = gnss->back; 131 if (!pf) { 132 err = -EFAULT; 133 goto exit; 134 } 135 136 if (!test_bit(ICE_FLAG_GNSS, pf->flags)) 137 return; 138 139 hw = &pf->hw; 140 141 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr)); 142 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS; 143 link_topo.topo_params.node_type_ctx |= 144 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M, 145 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE); 146 147 i2c_params = ICE_GNSS_UBX_DATA_LEN_WIDTH | 148 ICE_AQC_I2C_USE_REPEATED_START; 149 150 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR, 151 cpu_to_le16(ICE_GNSS_UBX_DATA_LEN_H), 152 i2c_params, (u8 *)&data_len_b, NULL); 153 if (err) 154 goto requeue; 155 156 data_len = be16_to_cpu(data_len_b); 157 if (data_len == 0 || data_len == U16_MAX) 158 goto requeue; 159 160 /* The u-blox has data_len bytes for us to read */ 161 162 data_len = min_t(typeof(data_len), data_len, PAGE_SIZE); 163 164 buf = (char *)get_zeroed_page(GFP_KERNEL); 165 if (!buf) { 166 err = -ENOMEM; 167 goto requeue; 168 } 169 170 /* Read received data */ 171 for (i = 0; i < data_len; i += bytes_read) { 172 unsigned int bytes_left = data_len - i; 173 174 bytes_read = min_t(typeof(bytes_left), bytes_left, 175 ICE_MAX_I2C_DATA_SIZE); 176 177 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR, 178 cpu_to_le16(ICE_GNSS_UBX_EMPTY_DATA), 179 bytes_read, &buf[i], NULL); 180 if (err) 181 goto free_buf; 182 } 183 184 count = gnss_insert_raw(pf->gnss_dev, buf, i); 185 if (count != i) 186 dev_warn(ice_pf_to_dev(pf), 187 "gnss_insert_raw ret=%d size=%d\n", 188 count, i); 189 delay = ICE_GNSS_TIMER_DELAY_TIME; 190 free_buf: 191 free_page((unsigned long)buf); 192 requeue: 193 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, delay); 194 exit: 195 if (err) 196 dev_dbg(ice_pf_to_dev(pf), "GNSS failed to read err=%d\n", err); 197 } 198 199 /** 200 * ice_gnss_struct_init - Initialize GNSS receiver 201 * @pf: Board private structure 202 * 203 * Initialize GNSS structures and workers. 204 * 205 * Return: 206 * * pointer to initialized gnss_serial struct - success 207 * * NULL - error 208 */ 209 static struct gnss_serial *ice_gnss_struct_init(struct ice_pf *pf) 210 { 211 struct device *dev = ice_pf_to_dev(pf); 212 struct kthread_worker *kworker; 213 struct gnss_serial *gnss; 214 215 gnss = kzalloc(sizeof(*gnss), GFP_KERNEL); 216 if (!gnss) 217 return NULL; 218 219 gnss->back = pf; 220 pf->gnss_serial = gnss; 221 222 kthread_init_delayed_work(&gnss->read_work, ice_gnss_read); 223 INIT_LIST_HEAD(&gnss->queue); 224 kthread_init_work(&gnss->write_work, ice_gnss_write_pending); 225 kworker = kthread_create_worker(0, "ice-gnss-%s", dev_name(dev)); 226 if (IS_ERR(kworker)) { 227 kfree(gnss); 228 return NULL; 229 } 230 231 gnss->kworker = kworker; 232 233 return gnss; 234 } 235 236 /** 237 * ice_gnss_open - Open GNSS device 238 * @gdev: pointer to the gnss device struct 239 * 240 * Open GNSS device and start filling the read buffer for consumer. 241 * 242 * Return: 243 * * 0 - success 244 * * negative - error code 245 */ 246 static int ice_gnss_open(struct gnss_device *gdev) 247 { 248 struct ice_pf *pf = gnss_get_drvdata(gdev); 249 struct gnss_serial *gnss; 250 251 if (!pf) 252 return -EFAULT; 253 254 if (!test_bit(ICE_FLAG_GNSS, pf->flags)) 255 return -EFAULT; 256 257 gnss = pf->gnss_serial; 258 if (!gnss) 259 return -ENODEV; 260 261 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, 0); 262 263 return 0; 264 } 265 266 /** 267 * ice_gnss_close - Close GNSS device 268 * @gdev: pointer to the gnss device struct 269 * 270 * Close GNSS device, cancel worker, stop filling the read buffer. 271 */ 272 static void ice_gnss_close(struct gnss_device *gdev) 273 { 274 struct ice_pf *pf = gnss_get_drvdata(gdev); 275 struct gnss_serial *gnss; 276 277 if (!pf) 278 return; 279 280 gnss = pf->gnss_serial; 281 if (!gnss) 282 return; 283 284 kthread_cancel_work_sync(&gnss->write_work); 285 kthread_cancel_delayed_work_sync(&gnss->read_work); 286 } 287 288 /** 289 * ice_gnss_write - Write to GNSS device 290 * @gdev: pointer to the gnss device struct 291 * @buf: pointer to the user data 292 * @count: size of the buffer to be sent to the GNSS device 293 * 294 * Return: 295 * * number of written bytes - success 296 * * negative - error code 297 */ 298 static int 299 ice_gnss_write(struct gnss_device *gdev, const unsigned char *buf, 300 size_t count) 301 { 302 struct ice_pf *pf = gnss_get_drvdata(gdev); 303 struct gnss_write_buf *write_buf; 304 struct gnss_serial *gnss; 305 unsigned char *cmd_buf; 306 int err = count; 307 308 /* We cannot write a single byte using our I2C implementation. */ 309 if (count <= 1 || count > ICE_GNSS_TTY_WRITE_BUF) 310 return -EINVAL; 311 312 if (!pf) 313 return -EFAULT; 314 315 if (!test_bit(ICE_FLAG_GNSS, pf->flags)) 316 return -EFAULT; 317 318 gnss = pf->gnss_serial; 319 if (!gnss) 320 return -ENODEV; 321 322 cmd_buf = kcalloc(count, sizeof(*buf), GFP_KERNEL); 323 if (!cmd_buf) 324 return -ENOMEM; 325 326 memcpy(cmd_buf, buf, count); 327 write_buf = kzalloc(sizeof(*write_buf), GFP_KERNEL); 328 if (!write_buf) { 329 kfree(cmd_buf); 330 return -ENOMEM; 331 } 332 333 write_buf->buf = cmd_buf; 334 write_buf->size = count; 335 INIT_LIST_HEAD(&write_buf->queue); 336 list_add_tail(&write_buf->queue, &gnss->queue); 337 kthread_queue_work(gnss->kworker, &gnss->write_work); 338 339 return err; 340 } 341 342 static const struct gnss_operations ice_gnss_ops = { 343 .open = ice_gnss_open, 344 .close = ice_gnss_close, 345 .write_raw = ice_gnss_write, 346 }; 347 348 /** 349 * ice_gnss_register - Register GNSS receiver 350 * @pf: Board private structure 351 * 352 * Allocate and register GNSS receiver in the Linux GNSS subsystem. 353 * 354 * Return: 355 * * 0 - success 356 * * negative - error code 357 */ 358 static int ice_gnss_register(struct ice_pf *pf) 359 { 360 struct gnss_device *gdev; 361 int ret; 362 363 gdev = gnss_allocate_device(ice_pf_to_dev(pf)); 364 if (!gdev) { 365 dev_err(ice_pf_to_dev(pf), 366 "gnss_allocate_device returns NULL\n"); 367 return -ENOMEM; 368 } 369 370 gdev->ops = &ice_gnss_ops; 371 gdev->type = GNSS_TYPE_UBX; 372 gnss_set_drvdata(gdev, pf); 373 ret = gnss_register_device(gdev); 374 if (ret) { 375 dev_err(ice_pf_to_dev(pf), "gnss_register_device err=%d\n", 376 ret); 377 gnss_put_device(gdev); 378 } else { 379 pf->gnss_dev = gdev; 380 } 381 382 return ret; 383 } 384 385 /** 386 * ice_gnss_deregister - Deregister GNSS receiver 387 * @pf: Board private structure 388 * 389 * Deregister GNSS receiver from the Linux GNSS subsystem, 390 * release its resources. 391 */ 392 static void ice_gnss_deregister(struct ice_pf *pf) 393 { 394 if (pf->gnss_dev) { 395 gnss_deregister_device(pf->gnss_dev); 396 gnss_put_device(pf->gnss_dev); 397 pf->gnss_dev = NULL; 398 } 399 } 400 401 /** 402 * ice_gnss_init - Initialize GNSS support 403 * @pf: Board private structure 404 */ 405 void ice_gnss_init(struct ice_pf *pf) 406 { 407 int ret; 408 409 pf->gnss_serial = ice_gnss_struct_init(pf); 410 if (!pf->gnss_serial) 411 return; 412 413 ret = ice_gnss_register(pf); 414 if (!ret) { 415 set_bit(ICE_FLAG_GNSS, pf->flags); 416 dev_info(ice_pf_to_dev(pf), "GNSS init successful\n"); 417 } else { 418 ice_gnss_exit(pf); 419 dev_err(ice_pf_to_dev(pf), "GNSS init failure\n"); 420 } 421 } 422 423 /** 424 * ice_gnss_exit - Disable GNSS TTY support 425 * @pf: Board private structure 426 */ 427 void ice_gnss_exit(struct ice_pf *pf) 428 { 429 ice_gnss_deregister(pf); 430 clear_bit(ICE_FLAG_GNSS, pf->flags); 431 432 if (pf->gnss_serial) { 433 struct gnss_serial *gnss = pf->gnss_serial; 434 435 kthread_cancel_work_sync(&gnss->write_work); 436 kthread_cancel_delayed_work_sync(&gnss->read_work); 437 kthread_destroy_worker(gnss->kworker); 438 gnss->kworker = NULL; 439 440 kfree(gnss); 441 pf->gnss_serial = NULL; 442 } 443 } 444 445 /** 446 * ice_gnss_is_gps_present - Check if GPS HW is present 447 * @hw: pointer to HW struct 448 */ 449 bool ice_gnss_is_gps_present(struct ice_hw *hw) 450 { 451 if (!hw->func_caps.ts_func_info.src_tmr_owned) 452 return false; 453 454 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK) 455 if (ice_is_e810t(hw)) { 456 int err; 457 u8 data; 458 459 err = ice_read_pca9575_reg_e810t(hw, ICE_PCA9575_P0_IN, &data); 460 if (err || !!(data & ICE_E810T_P0_GNSS_PRSNT_N)) 461 return false; 462 } else { 463 return false; 464 } 465 #else 466 if (!ice_is_e810t(hw)) 467 return false; 468 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */ 469 470 return true; 471 } 472