1 /* -------------------------------------------------------------------------
2 * Copyright (C) 2014-2016, Intel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * -------------------------------------------------------------------------
16 #include <linux/module.h>
17 #include <linux/nfc.h>
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <linux/firmware.h>
21 #include <net/nfc/nci_core.h>
25 #define FDP_OTP_PATCH_NAME "otp.bin"
26 #define FDP_RAM_PATCH_NAME "ram.bin"
27 #define FDP_FW_HEADER_SIZE 576
28 #define FDP_FW_UPDATE_SLEEP 1000
30 #define NCI_GET_VERSION_TIMEOUT 8000
31 #define NCI_PATCH_REQUEST_TIMEOUT 8000
32 #define FDP_PATCH_CONN_DEST 0xC2
33 #define FDP_PATCH_CONN_PARAM_TYPE 0xA0
35 #define NCI_PATCH_TYPE_RAM 0x00
36 #define NCI_PATCH_TYPE_OTP 0x01
37 #define NCI_PATCH_TYPE_EOT 0xFF
39 #define NCI_PARAM_ID_FW_RAM_VERSION 0xA0
40 #define NCI_PARAM_ID_FW_OTP_VERSION 0xA1
41 #define NCI_PARAM_ID_OTP_LIMITED_VERSION 0xC5
42 #define NCI_PARAM_ID_KEY_INDEX_ID 0xC6
44 #define NCI_GID_PROP 0x0F
45 #define NCI_OP_PROP_PATCH_OID 0x08
46 #define NCI_OP_PROP_SET_PDATA_OID 0x23
49 struct nfc_phy_ops
*phy_ops
;
50 struct fdp_i2c_phy
*phy
;
53 const struct firmware
*otp_patch
;
54 const struct firmware
*ram_patch
;
55 u32 otp_patch_version
;
56 u32 ram_patch_version
;
60 u32 limited_otp_version
;
67 atomic_t data_pkt_counter
;
68 void (*data_pkt_counter_cb
)(struct nci_dev
*ndev
);
71 u8 setup_patch_status
;
73 wait_queue_head_t setup_wq
;
76 static u8 nci_core_get_config_otp_ram_version
[5] = {
78 NCI_PARAM_ID_FW_RAM_VERSION
,
79 NCI_PARAM_ID_FW_OTP_VERSION
,
80 NCI_PARAM_ID_OTP_LIMITED_VERSION
,
81 NCI_PARAM_ID_KEY_INDEX_ID
84 struct nci_core_get_config_rsp
{
90 static int fdp_nci_create_conn(struct nci_dev
*ndev
)
92 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
93 struct core_conn_create_dest_spec_params param
;
96 /* proprietary destination specific paramerer without value */
97 param
.type
= FDP_PATCH_CONN_PARAM_TYPE
;
100 r
= nci_core_conn_create(info
->ndev
, FDP_PATCH_CONN_DEST
, 1,
101 sizeof(param
), ¶m
);
105 return nci_get_conn_info_by_dest_type_params(ndev
,
106 FDP_PATCH_CONN_DEST
, NULL
);
109 static inline int fdp_nci_get_versions(struct nci_dev
*ndev
)
111 return nci_core_cmd(ndev
, NCI_OP_CORE_GET_CONFIG_CMD
,
112 sizeof(nci_core_get_config_otp_ram_version
),
113 (__u8
*) &nci_core_get_config_otp_ram_version
);
116 static inline int fdp_nci_patch_cmd(struct nci_dev
*ndev
, u8 type
)
118 return nci_prop_cmd(ndev
, NCI_OP_PROP_PATCH_OID
, sizeof(type
), &type
);
121 static inline int fdp_nci_set_production_data(struct nci_dev
*ndev
, u8 len
,
124 return nci_prop_cmd(ndev
, NCI_OP_PROP_SET_PDATA_OID
, len
, data
);
127 static int fdp_nci_set_clock(struct nci_dev
*ndev
, u8 clock_type
,
134 nd
= (24 * fc
) / clock_freq
;
135 delta
= 24 * fc
- nd
* clock_freq
;
136 num
= (32768 * delta
) / clock_freq
;
144 data
[5] = num
& 0xFF;
145 data
[6] = (num
>> 8) & 0xff;
147 data
[8] = clock_type
;
149 return fdp_nci_set_production_data(ndev
, 9, data
);
152 static void fdp_nci_send_patch_cb(struct nci_dev
*ndev
)
154 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
156 info
->setup_patch_sent
= 1;
157 wake_up(&info
->setup_wq
);
161 * Register a packet sent counter and a callback
163 * We have no other way of knowing when all firmware packets were sent out
164 * on the i2c bus. We need to know that in order to close the connection and
165 * send the patch end message.
167 static void fdp_nci_set_data_pkt_counter(struct nci_dev
*ndev
,
168 void (*cb
)(struct nci_dev
*ndev
), int count
)
170 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
171 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
173 dev_dbg(dev
, "NCI data pkt counter %d\n", count
);
174 atomic_set(&info
->data_pkt_counter
, count
);
175 info
->data_pkt_counter_cb
= cb
;
179 * The device is expecting a stream of packets. All packets need to
180 * have the PBF flag set to 0x0 (last packet) even if the firmware
181 * file is segmented and there are multiple packets. If we give the
182 * whole firmware to nci_send_data it will segment it and it will set
183 * the PBF flag to 0x01 so we need to do the segmentation here.
185 * The firmware will be analyzed and applied when we send NCI_OP_PROP_PATCH_CMD
186 * command with NCI_PATCH_TYPE_EOT parameter. The device will send a
187 * NFCC_PATCH_NTF packaet and a NCI_OP_CORE_RESET_NTF packet.
189 static int fdp_nci_send_patch(struct nci_dev
*ndev
, u8 conn_id
, u8 type
)
191 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
192 const struct firmware
*fw
;
195 u8 max_size
, payload_size
;
198 if ((type
== NCI_PATCH_TYPE_OTP
&& !info
->otp_patch
) ||
199 (type
== NCI_PATCH_TYPE_RAM
&& !info
->ram_patch
))
202 if (type
== NCI_PATCH_TYPE_OTP
)
203 fw
= info
->otp_patch
;
205 fw
= info
->ram_patch
;
207 max_size
= nci_conn_max_data_pkt_payload_size(ndev
, conn_id
);
213 fdp_nci_set_data_pkt_counter(ndev
, fdp_nci_send_patch_cb
,
214 DIV_ROUND_UP(fw
->size
, max_size
));
218 payload_size
= min_t(unsigned long, (unsigned long) max_size
,
221 skb
= nci_skb_alloc(ndev
, (NCI_CTRL_HDR_SIZE
+ payload_size
),
224 fdp_nci_set_data_pkt_counter(ndev
, NULL
, 0);
229 skb_reserve(skb
, NCI_CTRL_HDR_SIZE
);
231 memcpy(skb_put(skb
, payload_size
), fw
->data
+ (fw
->size
- len
),
234 rc
= nci_send_data(ndev
, conn_id
, skb
);
237 fdp_nci_set_data_pkt_counter(ndev
, NULL
, 0);
247 static int fdp_nci_open(struct nci_dev
*ndev
)
250 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
251 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
253 dev_dbg(dev
, "%s\n", __func__
);
255 r
= info
->phy_ops
->enable(info
->phy
);
260 static int fdp_nci_close(struct nci_dev
*ndev
)
262 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
263 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
265 dev_dbg(dev
, "%s\n", __func__
);
269 static int fdp_nci_send(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__
);
276 if (atomic_dec_and_test(&info
->data_pkt_counter
))
277 info
->data_pkt_counter_cb(ndev
);
279 return info
->phy_ops
->write(info
->phy
, skb
);
282 int fdp_nci_recv_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
284 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
285 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
287 dev_dbg(dev
, "%s\n", __func__
);
288 return nci_recv_frame(ndev
, skb
);
290 EXPORT_SYMBOL(fdp_nci_recv_frame
);
292 static int fdp_nci_request_firmware(struct nci_dev
*ndev
)
294 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
295 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
299 r
= request_firmware(&info
->ram_patch
, FDP_RAM_PATCH_NAME
, dev
);
301 nfc_err(dev
, "RAM patch request error\n");
305 data
= (u8
*) info
->ram_patch
->data
;
306 info
->ram_patch_version
=
307 data
[FDP_FW_HEADER_SIZE
] |
308 (data
[FDP_FW_HEADER_SIZE
+ 1] << 8) |
309 (data
[FDP_FW_HEADER_SIZE
+ 2] << 16) |
310 (data
[FDP_FW_HEADER_SIZE
+ 3] << 24);
312 dev_dbg(dev
, "RAM patch version: %d, size: %d\n",
313 info
->ram_patch_version
, (int) info
->ram_patch
->size
);
316 r
= request_firmware(&info
->otp_patch
, FDP_OTP_PATCH_NAME
, dev
);
318 nfc_err(dev
, "OTP patch request error\n");
322 data
= (u8
*) info
->otp_patch
->data
;
323 info
->otp_patch_version
=
324 data
[FDP_FW_HEADER_SIZE
] |
325 (data
[FDP_FW_HEADER_SIZE
+ 1] << 8) |
326 (data
[FDP_FW_HEADER_SIZE
+2] << 16) |
327 (data
[FDP_FW_HEADER_SIZE
+3] << 24);
329 dev_dbg(dev
, "OTP patch version: %d, size: %d\n",
330 info
->otp_patch_version
, (int) info
->otp_patch
->size
);
337 static void fdp_nci_release_firmware(struct nci_dev
*ndev
)
339 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
341 if (info
->otp_patch
) {
342 release_firmware(info
->otp_patch
);
343 info
->otp_patch
= NULL
;
346 if (info
->ram_patch
) {
347 release_firmware(info
->ram_patch
);
348 info
->ram_patch
= NULL
;
352 static int fdp_nci_patch_otp(struct nci_dev
*ndev
)
354 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
355 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
359 if (info
->otp_version
>= info
->otp_patch_version
)
362 info
->setup_patch_sent
= 0;
363 info
->setup_reset_ntf
= 0;
364 info
->setup_patch_ntf
= 0;
366 /* Patch init request */
367 r
= fdp_nci_patch_cmd(ndev
, NCI_PATCH_TYPE_OTP
);
371 /* Patch data connection creation */
372 conn_id
= fdp_nci_create_conn(ndev
);
378 /* Send the patch over the data connection */
379 r
= fdp_nci_send_patch(ndev
, conn_id
, NCI_PATCH_TYPE_OTP
);
383 /* Wait for all the packets to be send over i2c */
384 wait_event_interruptible(info
->setup_wq
,
385 info
->setup_patch_sent
== 1);
387 /* make sure that the NFCC processed the last data packet */
388 msleep(FDP_FW_UPDATE_SLEEP
);
390 /* Close the data connection */
391 r
= nci_core_conn_close(info
->ndev
, conn_id
);
395 /* Patch finish message */
396 if (fdp_nci_patch_cmd(ndev
, NCI_PATCH_TYPE_EOT
)) {
397 nfc_err(dev
, "OTP patch error 0x%x\n", r
);
402 /* If the patch notification didn't arrive yet, wait for it */
403 wait_event_interruptible(info
->setup_wq
, info
->setup_patch_ntf
);
405 /* Check if the patching was successful */
406 r
= info
->setup_patch_status
;
408 nfc_err(dev
, "OTP patch error 0x%x\n", r
);
414 * We need to wait for the reset notification before we
417 wait_event_interruptible(info
->setup_wq
, info
->setup_reset_ntf
);
423 static int fdp_nci_patch_ram(struct nci_dev
*ndev
)
425 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
426 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
430 if (info
->ram_version
>= info
->ram_patch_version
)
433 info
->setup_patch_sent
= 0;
434 info
->setup_reset_ntf
= 0;
435 info
->setup_patch_ntf
= 0;
437 /* Patch init request */
438 r
= fdp_nci_patch_cmd(ndev
, NCI_PATCH_TYPE_RAM
);
442 /* Patch data connection creation */
443 conn_id
= fdp_nci_create_conn(ndev
);
449 /* Send the patch over the data connection */
450 r
= fdp_nci_send_patch(ndev
, conn_id
, NCI_PATCH_TYPE_RAM
);
454 /* Wait for all the packets to be send over i2c */
455 wait_event_interruptible(info
->setup_wq
,
456 info
->setup_patch_sent
== 1);
458 /* make sure that the NFCC processed the last data packet */
459 msleep(FDP_FW_UPDATE_SLEEP
);
461 /* Close the data connection */
462 r
= nci_core_conn_close(info
->ndev
, conn_id
);
466 /* Patch finish message */
467 if (fdp_nci_patch_cmd(ndev
, NCI_PATCH_TYPE_EOT
)) {
468 nfc_err(dev
, "RAM patch error 0x%x\n", r
);
473 /* If the patch notification didn't arrive yet, wait for it */
474 wait_event_interruptible(info
->setup_wq
, info
->setup_patch_ntf
);
476 /* Check if the patching was successful */
477 r
= info
->setup_patch_status
;
479 nfc_err(dev
, "RAM patch error 0x%x\n", r
);
485 * We need to wait for the reset notification before we
488 wait_event_interruptible(info
->setup_wq
, info
->setup_reset_ntf
);
494 static int fdp_nci_setup(struct nci_dev
*ndev
)
496 /* Format: total length followed by an NCI packet */
497 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
498 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
502 dev_dbg(dev
, "%s\n", __func__
);
504 r
= nci_core_init(ndev
);
508 /* Get RAM and OTP version */
509 r
= fdp_nci_get_versions(ndev
);
513 /* Load firmware from disk */
514 r
= fdp_nci_request_firmware(ndev
);
519 if (info
->otp_version
< info
->otp_patch_version
) {
520 r
= fdp_nci_patch_otp(ndev
);
527 if (info
->ram_version
< info
->ram_patch_version
) {
528 r
= fdp_nci_patch_ram(ndev
);
534 /* Release the firmware buffers */
535 fdp_nci_release_firmware(ndev
);
537 /* If a patch was applied the new version is checked */
539 r
= nci_core_init(ndev
);
543 r
= fdp_nci_get_versions(ndev
);
547 if (info
->otp_version
!= info
->otp_patch_version
||
548 info
->ram_version
!= info
->ram_patch_version
) {
549 nfc_err(dev
, "Firmware update failed");
556 * We initialized the devices but the NFC subsystem expects
557 * it to not be initialized.
559 return nci_core_reset(ndev
);
562 fdp_nci_release_firmware(ndev
);
563 nfc_err(dev
, "Setup error %d\n", r
);
567 static int fdp_nci_post_setup(struct nci_dev
*ndev
)
569 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
570 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
573 /* Check if the device has VSC */
574 if (info
->fw_vsc_cfg
&& info
->fw_vsc_cfg
[0]) {
576 /* Set the vendor specific configuration */
577 r
= fdp_nci_set_production_data(ndev
, info
->fw_vsc_cfg
[3],
578 &info
->fw_vsc_cfg
[4]);
580 nfc_err(dev
, "Vendor specific config set error %d\n",
586 /* Set clock type and frequency */
587 r
= fdp_nci_set_clock(ndev
, info
->clock_type
, info
->clock_freq
);
589 nfc_err(dev
, "Clock set error %d\n", r
);
594 * In order to apply the VSC FDP needs a reset
596 r
= nci_core_reset(ndev
);
601 * The nci core was initialized when post setup was called
602 * so we leave it like that
604 return nci_core_init(ndev
);
607 static int fdp_nci_core_reset_ntf_packet(struct nci_dev
*ndev
,
610 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
611 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
613 dev_dbg(dev
, "%s\n", __func__
);
614 info
->setup_reset_ntf
= 1;
615 wake_up(&info
->setup_wq
);
620 static int fdp_nci_prop_patch_ntf_packet(struct nci_dev
*ndev
,
623 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
624 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
626 dev_dbg(dev
, "%s\n", __func__
);
627 info
->setup_patch_ntf
= 1;
628 info
->setup_patch_status
= skb
->data
[0];
629 wake_up(&info
->setup_wq
);
634 static int fdp_nci_prop_patch_rsp_packet(struct nci_dev
*ndev
,
637 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
638 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
639 u8 status
= skb
->data
[0];
641 dev_dbg(dev
, "%s: status 0x%x\n", __func__
, status
);
642 nci_req_complete(ndev
, status
);
647 static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev
*ndev
,
650 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
651 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
652 u8 status
= skb
->data
[0];
654 dev_dbg(dev
, "%s: status 0x%x\n", __func__
, status
);
655 nci_req_complete(ndev
, status
);
660 static int fdp_nci_core_get_config_rsp_packet(struct nci_dev
*ndev
,
663 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
664 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
665 struct nci_core_get_config_rsp
*rsp
= (void *) skb
->data
;
668 if (rsp
->status
== NCI_STATUS_OK
) {
671 for (i
= 0; i
< 4; i
++) {
674 case NCI_PARAM_ID_FW_RAM_VERSION
:
676 info
->ram_version
= le32_to_cpup((__le32
*) p
);
679 case NCI_PARAM_ID_FW_OTP_VERSION
:
681 info
->otp_version
= le32_to_cpup((__le32
*) p
);
684 case NCI_PARAM_ID_OTP_LIMITED_VERSION
:
686 info
->otp_version
= le32_to_cpup((__le32
*) p
);
689 case NCI_PARAM_ID_KEY_INDEX_ID
:
691 info
->key_index
= *p
++;
696 dev_dbg(dev
, "OTP version %d\n", info
->otp_version
);
697 dev_dbg(dev
, "RAM version %d\n", info
->ram_version
);
698 dev_dbg(dev
, "key index %d\n", info
->key_index
);
699 dev_dbg(dev
, "%s: status 0x%x\n", __func__
, rsp
->status
);
701 nci_req_complete(ndev
, rsp
->status
);
706 static struct nci_driver_ops fdp_core_ops
[] = {
708 .opcode
= NCI_OP_CORE_GET_CONFIG_RSP
,
709 .rsp
= fdp_nci_core_get_config_rsp_packet
,
712 .opcode
= NCI_OP_CORE_RESET_NTF
,
713 .ntf
= fdp_nci_core_reset_ntf_packet
,
717 static struct nci_driver_ops fdp_prop_ops
[] = {
719 .opcode
= nci_opcode_pack(NCI_GID_PROP
, NCI_OP_PROP_PATCH_OID
),
720 .rsp
= fdp_nci_prop_patch_rsp_packet
,
721 .ntf
= fdp_nci_prop_patch_ntf_packet
,
724 .opcode
= nci_opcode_pack(NCI_GID_PROP
,
725 NCI_OP_PROP_SET_PDATA_OID
),
726 .rsp
= fdp_nci_prop_set_production_data_rsp_packet
,
730 struct nci_ops nci_ops
= {
731 .open
= fdp_nci_open
,
732 .close
= fdp_nci_close
,
733 .send
= fdp_nci_send
,
734 .setup
= fdp_nci_setup
,
735 .post_setup
= fdp_nci_post_setup
,
736 .prop_ops
= fdp_prop_ops
,
737 .n_prop_ops
= ARRAY_SIZE(fdp_prop_ops
),
738 .core_ops
= fdp_core_ops
,
739 .n_core_ops
= ARRAY_SIZE(fdp_core_ops
),
742 int fdp_nci_probe(struct fdp_i2c_phy
*phy
, struct nfc_phy_ops
*phy_ops
,
743 struct nci_dev
**ndevp
, int tx_headroom
,
744 int tx_tailroom
, u8 clock_type
, u32 clock_freq
,
747 struct device
*dev
= &phy
->i2c_dev
->dev
;
748 struct fdp_nci_info
*info
;
749 struct nci_dev
*ndev
;
753 info
= kzalloc(sizeof(struct fdp_nci_info
), GFP_KERNEL
);
760 info
->phy_ops
= phy_ops
;
761 info
->clock_type
= clock_type
;
762 info
->clock_freq
= clock_freq
;
763 info
->fw_vsc_cfg
= fw_vsc_cfg
;
765 init_waitqueue_head(&info
->setup_wq
);
767 protocols
= NFC_PROTO_JEWEL_MASK
|
768 NFC_PROTO_MIFARE_MASK
|
769 NFC_PROTO_FELICA_MASK
|
770 NFC_PROTO_ISO14443_MASK
|
771 NFC_PROTO_ISO14443_B_MASK
|
772 NFC_PROTO_NFC_DEP_MASK
|
773 NFC_PROTO_ISO15693_MASK
;
775 ndev
= nci_allocate_device(&nci_ops
, protocols
, tx_headroom
,
778 nfc_err(dev
, "Cannot allocate nfc ndev\n");
783 r
= nci_register_device(ndev
);
790 nci_set_drvdata(ndev
, info
);
795 nci_free_device(ndev
);
801 EXPORT_SYMBOL(fdp_nci_probe
);
803 void fdp_nci_remove(struct nci_dev
*ndev
)
805 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
806 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
808 dev_dbg(dev
, "%s\n", __func__
);
810 nci_unregister_device(ndev
);
811 nci_free_device(ndev
);
814 EXPORT_SYMBOL(fdp_nci_remove
);
816 MODULE_LICENSE("GPL");
817 MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
818 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");