1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -------------------------------------------------------------------------
3 * Copyright (C) 2014-2016, Intel Corporation
5 * -------------------------------------------------------------------------
8 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/delay.h>
12 #include <linux/firmware.h>
13 #include <net/nfc/nci_core.h>
17 #define FDP_OTP_PATCH_NAME "otp.bin"
18 #define FDP_RAM_PATCH_NAME "ram.bin"
19 #define FDP_FW_HEADER_SIZE 576
20 #define FDP_FW_UPDATE_SLEEP 1000
22 #define NCI_GET_VERSION_TIMEOUT 8000
23 #define NCI_PATCH_REQUEST_TIMEOUT 8000
24 #define FDP_PATCH_CONN_DEST 0xC2
25 #define FDP_PATCH_CONN_PARAM_TYPE 0xA0
27 #define NCI_PATCH_TYPE_RAM 0x00
28 #define NCI_PATCH_TYPE_OTP 0x01
29 #define NCI_PATCH_TYPE_EOT 0xFF
31 #define NCI_PARAM_ID_FW_RAM_VERSION 0xA0
32 #define NCI_PARAM_ID_FW_OTP_VERSION 0xA1
33 #define NCI_PARAM_ID_OTP_LIMITED_VERSION 0xC5
34 #define NCI_PARAM_ID_KEY_INDEX_ID 0xC6
36 #define NCI_GID_PROP 0x0F
37 #define NCI_OP_PROP_PATCH_OID 0x08
38 #define NCI_OP_PROP_SET_PDATA_OID 0x23
41 struct nfc_phy_ops *phy_ops;
42 struct fdp_i2c_phy *phy;
45 const struct firmware *otp_patch;
46 const struct firmware *ram_patch;
47 u32 otp_patch_version;
48 u32 ram_patch_version;
52 u32 limited_otp_version;
59 atomic_t data_pkt_counter;
60 void (*data_pkt_counter_cb)(struct nci_dev *ndev);
63 u8 setup_patch_status;
65 wait_queue_head_t setup_wq;
68 static u8 nci_core_get_config_otp_ram_version[5] = {
70 NCI_PARAM_ID_FW_RAM_VERSION,
71 NCI_PARAM_ID_FW_OTP_VERSION,
72 NCI_PARAM_ID_OTP_LIMITED_VERSION,
73 NCI_PARAM_ID_KEY_INDEX_ID
76 struct nci_core_get_config_rsp {
82 static int fdp_nci_create_conn(struct nci_dev *ndev)
84 struct fdp_nci_info *info = nci_get_drvdata(ndev);
85 struct core_conn_create_dest_spec_params param;
88 /* proprietary destination specific paramerer without value */
89 param.type = FDP_PATCH_CONN_PARAM_TYPE;
92 r = nci_core_conn_create(info->ndev, FDP_PATCH_CONN_DEST, 1,
93 sizeof(param), ¶m);
97 return nci_get_conn_info_by_dest_type_params(ndev,
98 FDP_PATCH_CONN_DEST, NULL);
101 static inline int fdp_nci_get_versions(struct nci_dev *ndev)
103 return nci_core_cmd(ndev, NCI_OP_CORE_GET_CONFIG_CMD,
104 sizeof(nci_core_get_config_otp_ram_version),
105 (__u8 *) &nci_core_get_config_otp_ram_version);
108 static inline int fdp_nci_patch_cmd(struct nci_dev *ndev, u8 type)
110 return nci_prop_cmd(ndev, NCI_OP_PROP_PATCH_OID, sizeof(type), &type);
113 static inline int fdp_nci_set_production_data(struct nci_dev *ndev, u8 len,
116 return nci_prop_cmd(ndev, NCI_OP_PROP_SET_PDATA_OID, len, data);
119 static int fdp_nci_set_clock(struct nci_dev *ndev, u8 clock_type,
126 nd = (24 * fc) / clock_freq;
127 delta = 24 * fc - nd * clock_freq;
128 num = (32768 * delta) / clock_freq;
136 data[5] = num & 0xFF;
137 data[6] = (num >> 8) & 0xff;
139 data[8] = clock_type;
141 return fdp_nci_set_production_data(ndev, 9, data);
144 static void fdp_nci_send_patch_cb(struct nci_dev *ndev)
146 struct fdp_nci_info *info = nci_get_drvdata(ndev);
148 info->setup_patch_sent = 1;
149 wake_up(&info->setup_wq);
153 * Register a packet sent counter and a callback
155 * We have no other way of knowing when all firmware packets were sent out
156 * on the i2c bus. We need to know that in order to close the connection and
157 * send the patch end message.
159 static void fdp_nci_set_data_pkt_counter(struct nci_dev *ndev,
160 void (*cb)(struct nci_dev *ndev), int count)
162 struct fdp_nci_info *info = nci_get_drvdata(ndev);
163 struct device *dev = &info->phy->i2c_dev->dev;
165 dev_dbg(dev, "NCI data pkt counter %d\n", count);
166 atomic_set(&info->data_pkt_counter, count);
167 info->data_pkt_counter_cb = cb;
171 * The device is expecting a stream of packets. All packets need to
172 * have the PBF flag set to 0x0 (last packet) even if the firmware
173 * file is segmented and there are multiple packets. If we give the
174 * whole firmware to nci_send_data it will segment it and it will set
175 * the PBF flag to 0x01 so we need to do the segmentation here.
177 * The firmware will be analyzed and applied when we send NCI_OP_PROP_PATCH_CMD
178 * command with NCI_PATCH_TYPE_EOT parameter. The device will send a
179 * NFCC_PATCH_NTF packet and a NCI_OP_CORE_RESET_NTF packet.
181 static int fdp_nci_send_patch(struct nci_dev *ndev, u8 conn_id, u8 type)
183 struct fdp_nci_info *info = nci_get_drvdata(ndev);
184 const struct firmware *fw;
187 int max_size, payload_size;
190 if ((type == NCI_PATCH_TYPE_OTP && !info->otp_patch) ||
191 (type == NCI_PATCH_TYPE_RAM && !info->ram_patch))
194 if (type == NCI_PATCH_TYPE_OTP)
195 fw = info->otp_patch;
197 fw = info->ram_patch;
199 max_size = nci_conn_max_data_pkt_payload_size(ndev, conn_id);
205 fdp_nci_set_data_pkt_counter(ndev, fdp_nci_send_patch_cb,
206 DIV_ROUND_UP(fw->size, max_size));
210 payload_size = min_t(unsigned long, max_size, len);
212 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + payload_size),
215 fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
220 skb_reserve(skb, NCI_CTRL_HDR_SIZE);
222 skb_put_data(skb, fw->data + (fw->size - len), payload_size);
224 rc = nci_send_data(ndev, conn_id, skb);
227 fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
237 static int fdp_nci_open(struct nci_dev *ndev)
239 struct fdp_nci_info *info = nci_get_drvdata(ndev);
240 struct device *dev = &info->phy->i2c_dev->dev;
242 dev_dbg(dev, "%s\n", __func__);
244 return info->phy_ops->enable(info->phy);
247 static int fdp_nci_close(struct nci_dev *ndev)
249 struct fdp_nci_info *info = nci_get_drvdata(ndev);
250 struct device *dev = &info->phy->i2c_dev->dev;
252 dev_dbg(dev, "%s\n", __func__);
256 static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
258 struct fdp_nci_info *info = nci_get_drvdata(ndev);
259 struct device *dev = &info->phy->i2c_dev->dev;
261 dev_dbg(dev, "%s\n", __func__);
263 if (atomic_dec_and_test(&info->data_pkt_counter))
264 info->data_pkt_counter_cb(ndev);
266 return info->phy_ops->write(info->phy, skb);
269 int fdp_nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
271 struct fdp_nci_info *info = nci_get_drvdata(ndev);
272 struct device *dev = &info->phy->i2c_dev->dev;
274 dev_dbg(dev, "%s\n", __func__);
275 return nci_recv_frame(ndev, skb);
277 EXPORT_SYMBOL(fdp_nci_recv_frame);
279 static int fdp_nci_request_firmware(struct nci_dev *ndev)
281 struct fdp_nci_info *info = nci_get_drvdata(ndev);
282 struct device *dev = &info->phy->i2c_dev->dev;
286 r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev);
288 nfc_err(dev, "RAM patch request error\n");
292 data = (u8 *) info->ram_patch->data;
293 info->ram_patch_version =
294 data[FDP_FW_HEADER_SIZE] |
295 (data[FDP_FW_HEADER_SIZE + 1] << 8) |
296 (data[FDP_FW_HEADER_SIZE + 2] << 16) |
297 (data[FDP_FW_HEADER_SIZE + 3] << 24);
299 dev_dbg(dev, "RAM patch version: %d, size: %d\n",
300 info->ram_patch_version, (int) info->ram_patch->size);
303 r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev);
305 nfc_err(dev, "OTP patch request error\n");
309 data = (u8 *) info->otp_patch->data;
310 info->otp_patch_version =
311 data[FDP_FW_HEADER_SIZE] |
312 (data[FDP_FW_HEADER_SIZE + 1] << 8) |
313 (data[FDP_FW_HEADER_SIZE+2] << 16) |
314 (data[FDP_FW_HEADER_SIZE+3] << 24);
316 dev_dbg(dev, "OTP patch version: %d, size: %d\n",
317 info->otp_patch_version, (int) info->otp_patch->size);
324 static void fdp_nci_release_firmware(struct nci_dev *ndev)
326 struct fdp_nci_info *info = nci_get_drvdata(ndev);
328 if (info->otp_patch) {
329 release_firmware(info->otp_patch);
330 info->otp_patch = NULL;
333 if (info->ram_patch) {
334 release_firmware(info->ram_patch);
335 info->ram_patch = NULL;
339 static int fdp_nci_patch_otp(struct nci_dev *ndev)
341 struct fdp_nci_info *info = nci_get_drvdata(ndev);
342 struct device *dev = &info->phy->i2c_dev->dev;
346 if (info->otp_version >= info->otp_patch_version)
349 info->setup_patch_sent = 0;
350 info->setup_reset_ntf = 0;
351 info->setup_patch_ntf = 0;
353 /* Patch init request */
354 r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP);
358 /* Patch data connection creation */
359 conn_id = fdp_nci_create_conn(ndev);
363 /* Send the patch over the data connection */
364 r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP);
368 /* Wait for all the packets to be send over i2c */
369 wait_event_interruptible(info->setup_wq,
370 info->setup_patch_sent == 1);
372 /* make sure that the NFCC processed the last data packet */
373 msleep(FDP_FW_UPDATE_SLEEP);
375 /* Close the data connection */
376 r = nci_core_conn_close(info->ndev, conn_id);
380 /* Patch finish message */
381 if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
382 nfc_err(dev, "OTP patch error 0x%x\n", r);
386 /* If the patch notification didn't arrive yet, wait for it */
387 wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
389 /* Check if the patching was successful */
390 r = info->setup_patch_status;
392 nfc_err(dev, "OTP patch error 0x%x\n", r);
397 * We need to wait for the reset notification before we
400 wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
405 static int fdp_nci_patch_ram(struct nci_dev *ndev)
407 struct fdp_nci_info *info = nci_get_drvdata(ndev);
408 struct device *dev = &info->phy->i2c_dev->dev;
412 if (info->ram_version >= info->ram_patch_version)
415 info->setup_patch_sent = 0;
416 info->setup_reset_ntf = 0;
417 info->setup_patch_ntf = 0;
419 /* Patch init request */
420 r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM);
424 /* Patch data connection creation */
425 conn_id = fdp_nci_create_conn(ndev);
429 /* Send the patch over the data connection */
430 r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM);
434 /* Wait for all the packets to be send over i2c */
435 wait_event_interruptible(info->setup_wq,
436 info->setup_patch_sent == 1);
438 /* make sure that the NFCC processed the last data packet */
439 msleep(FDP_FW_UPDATE_SLEEP);
441 /* Close the data connection */
442 r = nci_core_conn_close(info->ndev, conn_id);
446 /* Patch finish message */
447 if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
448 nfc_err(dev, "RAM patch error 0x%x\n", r);
452 /* If the patch notification didn't arrive yet, wait for it */
453 wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
455 /* Check if the patching was successful */
456 r = info->setup_patch_status;
458 nfc_err(dev, "RAM patch error 0x%x\n", r);
463 * We need to wait for the reset notification before we
466 wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
471 static int fdp_nci_setup(struct nci_dev *ndev)
473 /* Format: total length followed by an NCI packet */
474 struct fdp_nci_info *info = nci_get_drvdata(ndev);
475 struct device *dev = &info->phy->i2c_dev->dev;
479 dev_dbg(dev, "%s\n", __func__);
481 r = nci_core_init(ndev);
485 /* Get RAM and OTP version */
486 r = fdp_nci_get_versions(ndev);
490 /* Load firmware from disk */
491 r = fdp_nci_request_firmware(ndev);
496 if (info->otp_version < info->otp_patch_version) {
497 r = fdp_nci_patch_otp(ndev);
504 if (info->ram_version < info->ram_patch_version) {
505 r = fdp_nci_patch_ram(ndev);
511 /* Release the firmware buffers */
512 fdp_nci_release_firmware(ndev);
514 /* If a patch was applied the new version is checked */
516 r = nci_core_init(ndev);
520 r = fdp_nci_get_versions(ndev);
524 if (info->otp_version != info->otp_patch_version ||
525 info->ram_version != info->ram_patch_version) {
526 nfc_err(dev, "Firmware update failed");
533 * We initialized the devices but the NFC subsystem expects
534 * it to not be initialized.
536 return nci_core_reset(ndev);
539 fdp_nci_release_firmware(ndev);
540 nfc_err(dev, "Setup error %d\n", r);
544 static int fdp_nci_post_setup(struct nci_dev *ndev)
546 struct fdp_nci_info *info = nci_get_drvdata(ndev);
547 struct device *dev = &info->phy->i2c_dev->dev;
550 /* Check if the device has VSC */
551 if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) {
553 /* Set the vendor specific configuration */
554 r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3],
555 &info->fw_vsc_cfg[4]);
557 nfc_err(dev, "Vendor specific config set error %d\n",
563 /* Set clock type and frequency */
564 r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq);
566 nfc_err(dev, "Clock set error %d\n", r);
571 * In order to apply the VSC FDP needs a reset
573 r = nci_core_reset(ndev);
578 * The nci core was initialized when post setup was called
579 * so we leave it like that
581 return nci_core_init(ndev);
584 static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev,
587 struct fdp_nci_info *info = nci_get_drvdata(ndev);
588 struct device *dev = &info->phy->i2c_dev->dev;
590 dev_dbg(dev, "%s\n", __func__);
591 info->setup_reset_ntf = 1;
592 wake_up(&info->setup_wq);
597 static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev,
600 struct fdp_nci_info *info = nci_get_drvdata(ndev);
601 struct device *dev = &info->phy->i2c_dev->dev;
603 dev_dbg(dev, "%s\n", __func__);
604 info->setup_patch_ntf = 1;
605 info->setup_patch_status = skb->data[0];
606 wake_up(&info->setup_wq);
611 static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev,
614 struct fdp_nci_info *info = nci_get_drvdata(ndev);
615 struct device *dev = &info->phy->i2c_dev->dev;
616 u8 status = skb->data[0];
618 dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
619 nci_req_complete(ndev, status);
624 static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev,
627 struct fdp_nci_info *info = nci_get_drvdata(ndev);
628 struct device *dev = &info->phy->i2c_dev->dev;
629 u8 status = skb->data[0];
631 dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
632 nci_req_complete(ndev, status);
637 static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev,
640 struct fdp_nci_info *info = nci_get_drvdata(ndev);
641 struct device *dev = &info->phy->i2c_dev->dev;
642 struct nci_core_get_config_rsp *rsp = (void *) skb->data;
645 if (rsp->status == NCI_STATUS_OK) {
648 for (i = 0; i < 4; i++) {
651 case NCI_PARAM_ID_FW_RAM_VERSION:
653 info->ram_version = le32_to_cpup((__le32 *) p);
656 case NCI_PARAM_ID_FW_OTP_VERSION:
658 info->otp_version = le32_to_cpup((__le32 *) p);
661 case NCI_PARAM_ID_OTP_LIMITED_VERSION:
663 info->otp_version = le32_to_cpup((__le32 *) p);
666 case NCI_PARAM_ID_KEY_INDEX_ID:
668 info->key_index = *p++;
673 dev_dbg(dev, "OTP version %d\n", info->otp_version);
674 dev_dbg(dev, "RAM version %d\n", info->ram_version);
675 dev_dbg(dev, "key index %d\n", info->key_index);
676 dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status);
678 nci_req_complete(ndev, rsp->status);
683 static struct nci_driver_ops fdp_core_ops[] = {
685 .opcode = NCI_OP_CORE_GET_CONFIG_RSP,
686 .rsp = fdp_nci_core_get_config_rsp_packet,
689 .opcode = NCI_OP_CORE_RESET_NTF,
690 .ntf = fdp_nci_core_reset_ntf_packet,
694 static struct nci_driver_ops fdp_prop_ops[] = {
696 .opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID),
697 .rsp = fdp_nci_prop_patch_rsp_packet,
698 .ntf = fdp_nci_prop_patch_ntf_packet,
701 .opcode = nci_opcode_pack(NCI_GID_PROP,
702 NCI_OP_PROP_SET_PDATA_OID),
703 .rsp = fdp_nci_prop_set_production_data_rsp_packet,
707 static struct nci_ops nci_ops = {
708 .open = fdp_nci_open,
709 .close = fdp_nci_close,
710 .send = fdp_nci_send,
711 .setup = fdp_nci_setup,
712 .post_setup = fdp_nci_post_setup,
713 .prop_ops = fdp_prop_ops,
714 .n_prop_ops = ARRAY_SIZE(fdp_prop_ops),
715 .core_ops = fdp_core_ops,
716 .n_core_ops = ARRAY_SIZE(fdp_core_ops),
719 int fdp_nci_probe(struct fdp_i2c_phy *phy, struct nfc_phy_ops *phy_ops,
720 struct nci_dev **ndevp, int tx_headroom,
721 int tx_tailroom, u8 clock_type, u32 clock_freq,
724 struct device *dev = &phy->i2c_dev->dev;
725 struct fdp_nci_info *info;
726 struct nci_dev *ndev;
730 info = devm_kzalloc(dev, sizeof(struct fdp_nci_info), GFP_KERNEL);
735 info->phy_ops = phy_ops;
736 info->clock_type = clock_type;
737 info->clock_freq = clock_freq;
738 info->fw_vsc_cfg = fw_vsc_cfg;
740 init_waitqueue_head(&info->setup_wq);
742 protocols = NFC_PROTO_JEWEL_MASK |
743 NFC_PROTO_MIFARE_MASK |
744 NFC_PROTO_FELICA_MASK |
745 NFC_PROTO_ISO14443_MASK |
746 NFC_PROTO_ISO14443_B_MASK |
747 NFC_PROTO_NFC_DEP_MASK |
748 NFC_PROTO_ISO15693_MASK;
750 ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom,
753 nfc_err(dev, "Cannot allocate nfc ndev\n");
757 r = nci_register_device(ndev);
764 nci_set_drvdata(ndev, info);
769 nci_free_device(ndev);
772 EXPORT_SYMBOL(fdp_nci_probe);
774 void fdp_nci_remove(struct nci_dev *ndev)
776 struct fdp_nci_info *info = nci_get_drvdata(ndev);
777 struct device *dev = &info->phy->i2c_dev->dev;
779 dev_dbg(dev, "%s\n", __func__);
781 nci_unregister_device(ndev);
782 nci_free_device(ndev);
784 EXPORT_SYMBOL(fdp_nci_remove);
786 MODULE_LICENSE("GPL");
787 MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
788 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");