1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Marvell NFC driver: Firmware downloader 4 * 5 * Copyright (C) 2015, Marvell International Ltd. 6 */ 7 8 #include <linux/module.h> 9 #include <asm/unaligned.h> 10 #include <linux/firmware.h> 11 #include <linux/nfc.h> 12 #include <net/nfc/nci.h> 13 #include <net/nfc/nci_core.h> 14 #include "nfcmrvl.h" 15 16 #define FW_DNLD_TIMEOUT 15000 17 18 #define NCI_OP_PROPRIETARY_BOOT_CMD nci_opcode_pack(NCI_GID_PROPRIETARY, \ 19 NCI_OP_PROP_BOOT_CMD) 20 21 /* FW download states */ 22 23 enum { 24 STATE_RESET = 0, 25 STATE_INIT, 26 STATE_SET_REF_CLOCK, 27 STATE_SET_HI_CONFIG, 28 STATE_OPEN_LC, 29 STATE_FW_DNLD, 30 STATE_CLOSE_LC, 31 STATE_BOOT 32 }; 33 34 enum { 35 SUBSTATE_WAIT_COMMAND = 0, 36 SUBSTATE_WAIT_ACK_CREDIT, 37 SUBSTATE_WAIT_NACK_CREDIT, 38 SUBSTATE_WAIT_DATA_CREDIT, 39 }; 40 41 /* 42 * Patterns for responses 43 */ 44 45 static const uint8_t nci_pattern_core_reset_ntf[] = { 46 0x60, 0x00, 0x02, 0xA0, 0x01 47 }; 48 49 static const uint8_t nci_pattern_core_init_rsp[] = { 50 0x40, 0x01, 0x11 51 }; 52 53 static const uint8_t nci_pattern_core_set_config_rsp[] = { 54 0x40, 0x02, 0x02, 0x00, 0x00 55 }; 56 57 static const uint8_t nci_pattern_core_conn_create_rsp[] = { 58 0x40, 0x04, 0x04, 0x00 59 }; 60 61 static const uint8_t nci_pattern_core_conn_close_rsp[] = { 62 0x40, 0x05, 0x01, 0x00 63 }; 64 65 static const uint8_t nci_pattern_core_conn_credits_ntf[] = { 66 0x60, 0x06, 0x03, 0x01, NCI_CORE_LC_CONNID_PROP_FW_DL, 0x01 67 }; 68 69 static const uint8_t nci_pattern_proprietary_boot_rsp[] = { 70 0x4F, 0x3A, 0x01, 0x00 71 }; 72 73 static struct sk_buff *alloc_lc_skb(struct nfcmrvl_private *priv, uint8_t plen) 74 { 75 struct sk_buff *skb; 76 struct nci_data_hdr *hdr; 77 78 skb = nci_skb_alloc(priv->ndev, (NCI_DATA_HDR_SIZE + plen), GFP_KERNEL); 79 if (!skb) { 80 pr_err("no memory for data\n"); 81 return NULL; 82 } 83 84 hdr = skb_put(skb, NCI_DATA_HDR_SIZE); 85 hdr->conn_id = NCI_CORE_LC_CONNID_PROP_FW_DL; 86 hdr->rfu = 0; 87 hdr->plen = plen; 88 89 nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT); 90 nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST); 91 92 return skb; 93 } 94 95 static void fw_dnld_over(struct nfcmrvl_private *priv, u32 error) 96 { 97 if (priv->fw_dnld.fw) { 98 release_firmware(priv->fw_dnld.fw); 99 priv->fw_dnld.fw = NULL; 100 priv->fw_dnld.header = NULL; 101 priv->fw_dnld.binary_config = NULL; 102 } 103 104 atomic_set(&priv->ndev->cmd_cnt, 0); 105 106 if (timer_pending(&priv->ndev->cmd_timer)) 107 del_timer_sync(&priv->ndev->cmd_timer); 108 109 if (timer_pending(&priv->fw_dnld.timer)) 110 del_timer_sync(&priv->fw_dnld.timer); 111 112 nfc_info(priv->dev, "FW loading over (%d)]\n", error); 113 114 if (error != 0) { 115 /* failed, halt the chip to avoid power consumption */ 116 nfcmrvl_chip_halt(priv); 117 } 118 119 nfc_fw_download_done(priv->ndev->nfc_dev, priv->fw_dnld.name, error); 120 } 121 122 static void fw_dnld_timeout(struct timer_list *t) 123 { 124 struct nfcmrvl_private *priv = from_timer(priv, t, fw_dnld.timer); 125 126 nfc_err(priv->dev, "FW loading timeout"); 127 priv->fw_dnld.state = STATE_RESET; 128 fw_dnld_over(priv, -ETIMEDOUT); 129 } 130 131 static int process_state_reset(struct nfcmrvl_private *priv, 132 const struct sk_buff *skb) 133 { 134 if (sizeof(nci_pattern_core_reset_ntf) != skb->len || 135 memcmp(skb->data, nci_pattern_core_reset_ntf, 136 sizeof(nci_pattern_core_reset_ntf))) 137 return -EINVAL; 138 139 nfc_info(priv->dev, "BootROM reset, start fw download\n"); 140 141 /* Start FW download state machine */ 142 priv->fw_dnld.state = STATE_INIT; 143 nci_send_cmd(priv->ndev, NCI_OP_CORE_INIT_CMD, 0, NULL); 144 145 return 0; 146 } 147 148 static int process_state_init(struct nfcmrvl_private *priv, 149 const struct sk_buff *skb) 150 { 151 struct nci_core_set_config_cmd cmd; 152 153 if (sizeof(nci_pattern_core_init_rsp) >= skb->len || 154 memcmp(skb->data, nci_pattern_core_init_rsp, 155 sizeof(nci_pattern_core_init_rsp))) 156 return -EINVAL; 157 158 cmd.num_params = 1; 159 cmd.param.id = NFCMRVL_PROP_REF_CLOCK; 160 cmd.param.len = 4; 161 memcpy(cmd.param.val, &priv->fw_dnld.header->ref_clock, 4); 162 163 nci_send_cmd(priv->ndev, NCI_OP_CORE_SET_CONFIG_CMD, 3 + cmd.param.len, 164 &cmd); 165 166 priv->fw_dnld.state = STATE_SET_REF_CLOCK; 167 return 0; 168 } 169 170 static void create_lc(struct nfcmrvl_private *priv) 171 { 172 uint8_t param[2] = { NCI_CORE_LC_PROP_FW_DL, 0x0 }; 173 174 priv->fw_dnld.state = STATE_OPEN_LC; 175 nci_send_cmd(priv->ndev, NCI_OP_CORE_CONN_CREATE_CMD, 2, param); 176 } 177 178 static int process_state_set_ref_clock(struct nfcmrvl_private *priv, 179 const struct sk_buff *skb) 180 { 181 struct nci_core_set_config_cmd cmd; 182 183 if (sizeof(nci_pattern_core_set_config_rsp) != skb->len || 184 memcmp(skb->data, nci_pattern_core_set_config_rsp, skb->len)) 185 return -EINVAL; 186 187 cmd.num_params = 1; 188 cmd.param.id = NFCMRVL_PROP_SET_HI_CONFIG; 189 190 switch (priv->phy) { 191 case NFCMRVL_PHY_UART: 192 cmd.param.len = 5; 193 memcpy(cmd.param.val, 194 &priv->fw_dnld.binary_config->uart.baudrate, 195 4); 196 cmd.param.val[4] = 197 priv->fw_dnld.binary_config->uart.flow_control; 198 break; 199 case NFCMRVL_PHY_I2C: 200 cmd.param.len = 5; 201 memcpy(cmd.param.val, 202 &priv->fw_dnld.binary_config->i2c.clk, 203 4); 204 cmd.param.val[4] = 0; 205 break; 206 case NFCMRVL_PHY_SPI: 207 cmd.param.len = 5; 208 memcpy(cmd.param.val, 209 &priv->fw_dnld.binary_config->spi.clk, 210 4); 211 cmd.param.val[4] = 0; 212 break; 213 default: 214 create_lc(priv); 215 return 0; 216 } 217 218 priv->fw_dnld.state = STATE_SET_HI_CONFIG; 219 nci_send_cmd(priv->ndev, NCI_OP_CORE_SET_CONFIG_CMD, 3 + cmd.param.len, 220 &cmd); 221 return 0; 222 } 223 224 static int process_state_set_hi_config(struct nfcmrvl_private *priv, 225 const struct sk_buff *skb) 226 { 227 if (sizeof(nci_pattern_core_set_config_rsp) != skb->len || 228 memcmp(skb->data, nci_pattern_core_set_config_rsp, skb->len)) 229 return -EINVAL; 230 231 create_lc(priv); 232 return 0; 233 } 234 235 static int process_state_open_lc(struct nfcmrvl_private *priv, 236 const struct sk_buff *skb) 237 { 238 if (sizeof(nci_pattern_core_conn_create_rsp) >= skb->len || 239 memcmp(skb->data, nci_pattern_core_conn_create_rsp, 240 sizeof(nci_pattern_core_conn_create_rsp))) 241 return -EINVAL; 242 243 priv->fw_dnld.state = STATE_FW_DNLD; 244 priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND; 245 priv->fw_dnld.offset = priv->fw_dnld.binary_config->offset; 246 return 0; 247 } 248 249 static int process_state_fw_dnld(struct nfcmrvl_private *priv, 250 struct sk_buff *skb) 251 { 252 uint16_t len; 253 uint16_t comp_len; 254 struct sk_buff *out_skb; 255 256 switch (priv->fw_dnld.substate) { 257 case SUBSTATE_WAIT_COMMAND: 258 /* 259 * Command format: 260 * B0..2: NCI header 261 * B3 : Helper command (0xA5) 262 * B4..5: le16 data size 263 * B6..7: le16 data size complement (~) 264 * B8..N: payload 265 */ 266 267 /* Remove NCI HDR */ 268 skb_pull(skb, 3); 269 if (skb->data[0] != HELPER_CMD_PACKET_FORMAT || skb->len != 5) { 270 nfc_err(priv->dev, "bad command"); 271 return -EINVAL; 272 } 273 skb_pull(skb, 1); 274 len = get_unaligned_le16(skb->data); 275 skb_pull(skb, 2); 276 comp_len = get_unaligned_le16(skb->data); 277 memcpy(&comp_len, skb->data, 2); 278 skb_pull(skb, 2); 279 if (((~len) & 0xFFFF) != comp_len) { 280 nfc_err(priv->dev, "bad len complement: %x %x %x", 281 len, comp_len, (~len & 0xFFFF)); 282 out_skb = alloc_lc_skb(priv, 1); 283 if (!out_skb) 284 return -ENOMEM; 285 skb_put_u8(out_skb, 0xBF); 286 nci_send_frame(priv->ndev, out_skb); 287 priv->fw_dnld.substate = SUBSTATE_WAIT_NACK_CREDIT; 288 return 0; 289 } 290 priv->fw_dnld.chunk_len = len; 291 out_skb = alloc_lc_skb(priv, 1); 292 if (!out_skb) 293 return -ENOMEM; 294 skb_put_u8(out_skb, HELPER_ACK_PACKET_FORMAT); 295 nci_send_frame(priv->ndev, out_skb); 296 priv->fw_dnld.substate = SUBSTATE_WAIT_ACK_CREDIT; 297 break; 298 299 case SUBSTATE_WAIT_ACK_CREDIT: 300 if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len || 301 memcmp(nci_pattern_core_conn_credits_ntf, skb->data, 302 skb->len)) { 303 nfc_err(priv->dev, "bad packet: waiting for credit"); 304 return -EINVAL; 305 } 306 if (priv->fw_dnld.chunk_len == 0) { 307 /* FW Loading is done */ 308 uint8_t conn_id = NCI_CORE_LC_CONNID_PROP_FW_DL; 309 310 priv->fw_dnld.state = STATE_CLOSE_LC; 311 nci_send_cmd(priv->ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 312 1, &conn_id); 313 } else { 314 out_skb = alloc_lc_skb(priv, priv->fw_dnld.chunk_len); 315 if (!out_skb) 316 return -ENOMEM; 317 skb_put_data(out_skb, 318 ((uint8_t *)priv->fw_dnld.fw->data) + priv->fw_dnld.offset, 319 priv->fw_dnld.chunk_len); 320 nci_send_frame(priv->ndev, out_skb); 321 priv->fw_dnld.substate = SUBSTATE_WAIT_DATA_CREDIT; 322 } 323 break; 324 325 case SUBSTATE_WAIT_DATA_CREDIT: 326 if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len || 327 memcmp(nci_pattern_core_conn_credits_ntf, skb->data, 328 skb->len)) { 329 nfc_err(priv->dev, "bad packet: waiting for credit"); 330 return -EINVAL; 331 } 332 priv->fw_dnld.offset += priv->fw_dnld.chunk_len; 333 priv->fw_dnld.chunk_len = 0; 334 priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND; 335 break; 336 337 case SUBSTATE_WAIT_NACK_CREDIT: 338 if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len || 339 memcmp(nci_pattern_core_conn_credits_ntf, skb->data, 340 skb->len)) { 341 nfc_err(priv->dev, "bad packet: waiting for credit"); 342 return -EINVAL; 343 } 344 priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND; 345 break; 346 } 347 return 0; 348 } 349 350 static int process_state_close_lc(struct nfcmrvl_private *priv, 351 const struct sk_buff *skb) 352 { 353 if (sizeof(nci_pattern_core_conn_close_rsp) != skb->len || 354 memcmp(skb->data, nci_pattern_core_conn_close_rsp, skb->len)) 355 return -EINVAL; 356 357 priv->fw_dnld.state = STATE_BOOT; 358 nci_send_cmd(priv->ndev, NCI_OP_PROPRIETARY_BOOT_CMD, 0, NULL); 359 return 0; 360 } 361 362 static int process_state_boot(struct nfcmrvl_private *priv, 363 const struct sk_buff *skb) 364 { 365 if (sizeof(nci_pattern_proprietary_boot_rsp) != skb->len || 366 memcmp(skb->data, nci_pattern_proprietary_boot_rsp, skb->len)) 367 return -EINVAL; 368 369 /* 370 * Update HI config to use the right configuration for the next 371 * data exchanges. 372 */ 373 priv->if_ops->nci_update_config(priv, 374 &priv->fw_dnld.binary_config->config); 375 376 if (priv->fw_dnld.binary_config == &priv->fw_dnld.header->helper) { 377 /* 378 * This is the case where an helper was needed and we have 379 * uploaded it. Now we have to wait the next RESET NTF to start 380 * FW download. 381 */ 382 priv->fw_dnld.state = STATE_RESET; 383 priv->fw_dnld.binary_config = &priv->fw_dnld.header->firmware; 384 nfc_info(priv->dev, "FW loading: helper loaded"); 385 } else { 386 nfc_info(priv->dev, "FW loading: firmware loaded"); 387 fw_dnld_over(priv, 0); 388 } 389 return 0; 390 } 391 392 static void fw_dnld_rx_work(struct work_struct *work) 393 { 394 int ret; 395 struct sk_buff *skb; 396 struct nfcmrvl_fw_dnld *fw_dnld = container_of(work, 397 struct nfcmrvl_fw_dnld, 398 rx_work); 399 struct nfcmrvl_private *priv = container_of(fw_dnld, 400 struct nfcmrvl_private, 401 fw_dnld); 402 403 while ((skb = skb_dequeue(&fw_dnld->rx_q))) { 404 nfc_send_to_raw_sock(priv->ndev->nfc_dev, skb, 405 RAW_PAYLOAD_NCI, NFC_DIRECTION_RX); 406 switch (fw_dnld->state) { 407 case STATE_RESET: 408 ret = process_state_reset(priv, skb); 409 break; 410 case STATE_INIT: 411 ret = process_state_init(priv, skb); 412 break; 413 case STATE_SET_REF_CLOCK: 414 ret = process_state_set_ref_clock(priv, skb); 415 break; 416 case STATE_SET_HI_CONFIG: 417 ret = process_state_set_hi_config(priv, skb); 418 break; 419 case STATE_OPEN_LC: 420 ret = process_state_open_lc(priv, skb); 421 break; 422 case STATE_FW_DNLD: 423 ret = process_state_fw_dnld(priv, skb); 424 break; 425 case STATE_CLOSE_LC: 426 ret = process_state_close_lc(priv, skb); 427 break; 428 case STATE_BOOT: 429 ret = process_state_boot(priv, skb); 430 break; 431 default: 432 ret = -EFAULT; 433 } 434 435 kfree_skb(skb); 436 437 if (ret != 0) { 438 nfc_err(priv->dev, "FW loading error"); 439 fw_dnld_over(priv, ret); 440 break; 441 } 442 } 443 } 444 445 int nfcmrvl_fw_dnld_init(struct nfcmrvl_private *priv) 446 { 447 char name[32]; 448 449 INIT_WORK(&priv->fw_dnld.rx_work, fw_dnld_rx_work); 450 snprintf(name, sizeof(name), "%s_nfcmrvl_fw_dnld_rx_wq", 451 dev_name(&priv->ndev->nfc_dev->dev)); 452 priv->fw_dnld.rx_wq = create_singlethread_workqueue(name); 453 if (!priv->fw_dnld.rx_wq) 454 return -ENOMEM; 455 skb_queue_head_init(&priv->fw_dnld.rx_q); 456 return 0; 457 } 458 459 void nfcmrvl_fw_dnld_deinit(struct nfcmrvl_private *priv) 460 { 461 destroy_workqueue(priv->fw_dnld.rx_wq); 462 } 463 464 void nfcmrvl_fw_dnld_recv_frame(struct nfcmrvl_private *priv, 465 struct sk_buff *skb) 466 { 467 /* Discard command timer */ 468 if (timer_pending(&priv->ndev->cmd_timer)) 469 del_timer_sync(&priv->ndev->cmd_timer); 470 471 /* Allow next command */ 472 atomic_set(&priv->ndev->cmd_cnt, 1); 473 474 /* Queue and trigger rx work */ 475 skb_queue_tail(&priv->fw_dnld.rx_q, skb); 476 queue_work(priv->fw_dnld.rx_wq, &priv->fw_dnld.rx_work); 477 } 478 479 void nfcmrvl_fw_dnld_abort(struct nfcmrvl_private *priv) 480 { 481 fw_dnld_over(priv, -EHOSTDOWN); 482 } 483 484 int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name) 485 { 486 struct nfcmrvl_private *priv = nci_get_drvdata(ndev); 487 struct nfcmrvl_fw_dnld *fw_dnld = &priv->fw_dnld; 488 int res; 489 490 if (!priv->support_fw_dnld) 491 return -ENOTSUPP; 492 493 if (!firmware_name || !firmware_name[0]) 494 return -EINVAL; 495 496 strcpy(fw_dnld->name, firmware_name); 497 498 /* 499 * Retrieve FW binary file and parse it to initialize FW download 500 * state machine. 501 */ 502 503 /* Retrieve FW binary */ 504 res = request_firmware(&fw_dnld->fw, firmware_name, 505 &ndev->nfc_dev->dev); 506 if (res < 0) { 507 nfc_err(priv->dev, "failed to retrieve FW %s", firmware_name); 508 return -ENOENT; 509 } 510 511 fw_dnld->header = (const struct nfcmrvl_fw *) priv->fw_dnld.fw->data; 512 513 if (fw_dnld->header->magic != NFCMRVL_FW_MAGIC || 514 fw_dnld->header->phy != priv->phy) { 515 nfc_err(priv->dev, "bad firmware binary %s magic=0x%x phy=%d", 516 firmware_name, fw_dnld->header->magic, 517 fw_dnld->header->phy); 518 release_firmware(fw_dnld->fw); 519 fw_dnld->header = NULL; 520 return -EINVAL; 521 } 522 523 if (fw_dnld->header->helper.offset != 0) { 524 nfc_info(priv->dev, "loading helper"); 525 fw_dnld->binary_config = &fw_dnld->header->helper; 526 } else { 527 nfc_info(priv->dev, "loading firmware"); 528 fw_dnld->binary_config = &fw_dnld->header->firmware; 529 } 530 531 /* Configure a timer for timeout */ 532 timer_setup(&priv->fw_dnld.timer, fw_dnld_timeout, 0); 533 mod_timer(&priv->fw_dnld.timer, 534 jiffies + msecs_to_jiffies(FW_DNLD_TIMEOUT)); 535 536 /* Ronfigure HI to be sure that it is the bootrom values */ 537 priv->if_ops->nci_update_config(priv, 538 &fw_dnld->header->bootrom.config); 539 540 /* Allow first command */ 541 atomic_set(&priv->ndev->cmd_cnt, 1); 542 543 /* First, reset the chip */ 544 priv->fw_dnld.state = STATE_RESET; 545 nfcmrvl_chip_reset(priv); 546 547 /* Now wait for CORE_RESET_NTF or timeout */ 548 549 return 0; 550 } 551