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_id(ndev
, 0);
108 static inline int fdp_nci_get_versions(struct nci_dev
*ndev
)
110 return nci_core_cmd(ndev
, NCI_OP_CORE_GET_CONFIG_CMD
,
111 sizeof(nci_core_get_config_otp_ram_version
),
112 (__u8
*) &nci_core_get_config_otp_ram_version
);
115 static inline int fdp_nci_patch_cmd(struct nci_dev
*ndev
, u8 type
)
117 return nci_prop_cmd(ndev
, NCI_OP_PROP_PATCH_OID
, sizeof(type
), &type
);
120 static inline int fdp_nci_set_production_data(struct nci_dev
*ndev
, u8 len
,
123 return nci_prop_cmd(ndev
, NCI_OP_PROP_SET_PDATA_OID
, len
, data
);
126 static int fdp_nci_set_clock(struct nci_dev
*ndev
, u8 clock_type
,
133 nd
= (24 * fc
) / clock_freq
;
134 delta
= 24 * fc
- nd
* clock_freq
;
135 num
= (32768 * delta
) / clock_freq
;
143 data
[5] = num
& 0xFF;
144 data
[6] = (num
>> 8) & 0xff;
146 data
[8] = clock_type
;
148 return fdp_nci_set_production_data(ndev
, 9, data
);
151 static void fdp_nci_send_patch_cb(struct nci_dev
*ndev
)
153 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
155 info
->setup_patch_sent
= 1;
156 wake_up(&info
->setup_wq
);
160 * Register a packet sent counter and a callback
162 * We have no other way of knowing when all firmware packets were sent out
163 * on the i2c bus. We need to know that in order to close the connection and
164 * send the patch end message.
166 static void fdp_nci_set_data_pkt_counter(struct nci_dev
*ndev
,
167 void (*cb
)(struct nci_dev
*ndev
), int count
)
169 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
170 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
172 dev_dbg(dev
, "NCI data pkt counter %d\n", count
);
173 atomic_set(&info
->data_pkt_counter
, count
);
174 info
->data_pkt_counter_cb
= cb
;
178 * The device is expecting a stream of packets. All packets need to
179 * have the PBF flag set to 0x0 (last packet) even if the firmware
180 * file is segmented and there are multiple packets. If we give the
181 * whole firmware to nci_send_data it will segment it and it will set
182 * the PBF flag to 0x01 so we need to do the segmentation here.
184 * The firmware will be analyzed and applied when we send NCI_OP_PROP_PATCH_CMD
185 * command with NCI_PATCH_TYPE_EOT parameter. The device will send a
186 * NFCC_PATCH_NTF packaet and a NCI_OP_CORE_RESET_NTF packet.
188 static int fdp_nci_send_patch(struct nci_dev
*ndev
, u8 conn_id
, u8 type
)
190 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
191 const struct firmware
*fw
;
194 u8 max_size
, payload_size
;
197 if ((type
== NCI_PATCH_TYPE_OTP
&& !info
->otp_patch
) ||
198 (type
== NCI_PATCH_TYPE_RAM
&& !info
->ram_patch
))
201 if (type
== NCI_PATCH_TYPE_OTP
)
202 fw
= info
->otp_patch
;
204 fw
= info
->ram_patch
;
206 max_size
= nci_conn_max_data_pkt_payload_size(ndev
, conn_id
);
212 fdp_nci_set_data_pkt_counter(ndev
, fdp_nci_send_patch_cb
,
213 DIV_ROUND_UP(fw
->size
, max_size
));
217 payload_size
= min_t(unsigned long, (unsigned long) max_size
,
220 skb
= nci_skb_alloc(ndev
, (NCI_CTRL_HDR_SIZE
+ payload_size
),
223 fdp_nci_set_data_pkt_counter(ndev
, NULL
, 0);
228 skb_reserve(skb
, NCI_CTRL_HDR_SIZE
);
230 memcpy(skb_put(skb
, payload_size
), fw
->data
+ (fw
->size
- len
),
233 rc
= nci_send_data(ndev
, conn_id
, skb
);
236 fdp_nci_set_data_pkt_counter(ndev
, NULL
, 0);
246 static int fdp_nci_open(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__
);
254 r
= info
->phy_ops
->enable(info
->phy
);
259 static int fdp_nci_close(struct nci_dev
*ndev
)
261 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
262 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
264 dev_dbg(dev
, "%s\n", __func__
);
268 static int fdp_nci_send(struct nci_dev
*ndev
, struct sk_buff
*skb
)
270 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
271 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
273 dev_dbg(dev
, "%s\n", __func__
);
275 if (atomic_dec_and_test(&info
->data_pkt_counter
))
276 info
->data_pkt_counter_cb(ndev
);
278 return info
->phy_ops
->write(info
->phy
, skb
);
281 int fdp_nci_recv_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
283 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
284 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
286 dev_dbg(dev
, "%s\n", __func__
);
287 return nci_recv_frame(ndev
, skb
);
289 EXPORT_SYMBOL(fdp_nci_recv_frame
);
291 static int fdp_nci_request_firmware(struct nci_dev
*ndev
)
293 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
294 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
298 r
= request_firmware(&info
->ram_patch
, FDP_RAM_PATCH_NAME
, dev
);
300 nfc_err(dev
, "RAM patch request error\n");
304 data
= (u8
*) info
->ram_patch
->data
;
305 info
->ram_patch_version
=
306 data
[FDP_FW_HEADER_SIZE
] |
307 (data
[FDP_FW_HEADER_SIZE
+ 1] << 8) |
308 (data
[FDP_FW_HEADER_SIZE
+ 2] << 16) |
309 (data
[FDP_FW_HEADER_SIZE
+ 3] << 24);
311 dev_dbg(dev
, "RAM patch version: %d, size: %d\n",
312 info
->ram_patch_version
, (int) info
->ram_patch
->size
);
315 r
= request_firmware(&info
->otp_patch
, FDP_OTP_PATCH_NAME
, dev
);
317 nfc_err(dev
, "OTP patch request error\n");
321 data
= (u8
*) info
->otp_patch
->data
;
322 info
->otp_patch_version
=
323 data
[FDP_FW_HEADER_SIZE
] |
324 (data
[FDP_FW_HEADER_SIZE
+ 1] << 8) |
325 (data
[FDP_FW_HEADER_SIZE
+2] << 16) |
326 (data
[FDP_FW_HEADER_SIZE
+3] << 24);
328 dev_dbg(dev
, "OTP patch version: %d, size: %d\n",
329 info
->otp_patch_version
, (int) info
->otp_patch
->size
);
336 static void fdp_nci_release_firmware(struct nci_dev
*ndev
)
338 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
340 if (info
->otp_patch
) {
341 release_firmware(info
->otp_patch
);
342 info
->otp_patch
= NULL
;
345 if (info
->ram_patch
) {
346 release_firmware(info
->ram_patch
);
347 info
->otp_patch
= NULL
;
351 static int fdp_nci_patch_otp(struct nci_dev
*ndev
)
353 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
354 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
358 if (info
->otp_version
>= info
->otp_patch_version
)
361 info
->setup_patch_sent
= 0;
362 info
->setup_reset_ntf
= 0;
363 info
->setup_patch_ntf
= 0;
365 /* Patch init request */
366 r
= fdp_nci_patch_cmd(ndev
, NCI_PATCH_TYPE_OTP
);
370 /* Patch data connection creation */
371 conn_id
= fdp_nci_create_conn(ndev
);
377 /* Send the patch over the data connection */
378 r
= fdp_nci_send_patch(ndev
, conn_id
, NCI_PATCH_TYPE_OTP
);
382 /* Wait for all the packets to be send over i2c */
383 wait_event_interruptible(info
->setup_wq
,
384 info
->setup_patch_sent
== 1);
386 /* make sure that the NFCC processed the last data packet */
387 msleep(FDP_FW_UPDATE_SLEEP
);
389 /* Close the data connection */
390 r
= nci_core_conn_close(info
->ndev
, conn_id
);
394 /* Patch finish message */
395 if (fdp_nci_patch_cmd(ndev
, NCI_PATCH_TYPE_EOT
)) {
396 nfc_err(dev
, "OTP patch error 0x%x\n", r
);
401 /* If the patch notification didn't arrive yet, wait for it */
402 wait_event_interruptible(info
->setup_wq
, info
->setup_patch_ntf
);
404 /* Check if the patching was successful */
405 r
= info
->setup_patch_status
;
407 nfc_err(dev
, "OTP patch error 0x%x\n", r
);
413 * We need to wait for the reset notification before we
416 wait_event_interruptible(info
->setup_wq
, info
->setup_reset_ntf
);
422 static int fdp_nci_patch_ram(struct nci_dev
*ndev
)
424 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
425 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
429 if (info
->ram_version
>= info
->ram_patch_version
)
432 info
->setup_patch_sent
= 0;
433 info
->setup_reset_ntf
= 0;
434 info
->setup_patch_ntf
= 0;
436 /* Patch init request */
437 r
= fdp_nci_patch_cmd(ndev
, NCI_PATCH_TYPE_RAM
);
441 /* Patch data connection creation */
442 conn_id
= fdp_nci_create_conn(ndev
);
448 /* Send the patch over the data connection */
449 r
= fdp_nci_send_patch(ndev
, conn_id
, NCI_PATCH_TYPE_RAM
);
453 /* Wait for all the packets to be send over i2c */
454 wait_event_interruptible(info
->setup_wq
,
455 info
->setup_patch_sent
== 1);
457 /* make sure that the NFCC processed the last data packet */
458 msleep(FDP_FW_UPDATE_SLEEP
);
460 /* Close the data connection */
461 r
= nci_core_conn_close(info
->ndev
, conn_id
);
465 /* Patch finish message */
466 if (fdp_nci_patch_cmd(ndev
, NCI_PATCH_TYPE_EOT
)) {
467 nfc_err(dev
, "RAM patch error 0x%x\n", r
);
472 /* If the patch notification didn't arrive yet, wait for it */
473 wait_event_interruptible(info
->setup_wq
, info
->setup_patch_ntf
);
475 /* Check if the patching was successful */
476 r
= info
->setup_patch_status
;
478 nfc_err(dev
, "RAM patch error 0x%x\n", r
);
484 * We need to wait for the reset notification before we
487 wait_event_interruptible(info
->setup_wq
, info
->setup_reset_ntf
);
493 static int fdp_nci_setup(struct nci_dev
*ndev
)
495 /* Format: total length followed by an NCI packet */
496 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
497 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
501 dev_dbg(dev
, "%s\n", __func__
);
503 r
= nci_core_init(ndev
);
507 /* Get RAM and OTP version */
508 r
= fdp_nci_get_versions(ndev
);
512 /* Load firmware from disk */
513 r
= fdp_nci_request_firmware(ndev
);
518 if (info
->otp_version
< info
->otp_patch_version
) {
519 r
= fdp_nci_patch_otp(ndev
);
526 if (info
->ram_version
< info
->ram_patch_version
) {
527 r
= fdp_nci_patch_ram(ndev
);
533 /* Release the firmware buffers */
534 fdp_nci_release_firmware(ndev
);
536 /* If a patch was applied the new version is checked */
538 r
= nci_core_init(ndev
);
542 r
= fdp_nci_get_versions(ndev
);
546 if (info
->otp_version
!= info
->otp_patch_version
||
547 info
->ram_version
!= info
->ram_patch_version
) {
548 nfc_err(dev
, "Firmware update failed");
555 * We initialized the devices but the NFC subsystem expects
556 * it to not be initialized.
558 return nci_core_reset(ndev
);
561 fdp_nci_release_firmware(ndev
);
562 nfc_err(dev
, "Setup error %d\n", r
);
566 static int fdp_nci_post_setup(struct nci_dev
*ndev
)
568 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
569 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
572 /* Check if the device has VSC */
573 if (info
->fw_vsc_cfg
&& info
->fw_vsc_cfg
[0]) {
575 /* Set the vendor specific configuration */
576 r
= fdp_nci_set_production_data(ndev
, info
->fw_vsc_cfg
[3],
577 &info
->fw_vsc_cfg
[4]);
579 nfc_err(dev
, "Vendor specific config set error %d\n",
585 /* Set clock type and frequency */
586 r
= fdp_nci_set_clock(ndev
, info
->clock_type
, info
->clock_freq
);
588 nfc_err(dev
, "Clock set error %d\n", r
);
593 * In order to apply the VSC FDP needs a reset
595 r
= nci_core_reset(ndev
);
600 * The nci core was initialized when post setup was called
601 * so we leave it like that
603 return nci_core_init(ndev
);
606 static int fdp_nci_core_reset_ntf_packet(struct nci_dev
*ndev
,
609 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
610 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
612 dev_dbg(dev
, "%s\n", __func__
);
613 info
->setup_reset_ntf
= 1;
614 wake_up(&info
->setup_wq
);
619 static int fdp_nci_prop_patch_ntf_packet(struct nci_dev
*ndev
,
622 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
623 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
625 dev_dbg(dev
, "%s\n", __func__
);
626 info
->setup_patch_ntf
= 1;
627 info
->setup_patch_status
= skb
->data
[0];
628 wake_up(&info
->setup_wq
);
633 static int fdp_nci_prop_patch_rsp_packet(struct nci_dev
*ndev
,
636 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
637 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
638 u8 status
= skb
->data
[0];
640 dev_dbg(dev
, "%s: status 0x%x\n", __func__
, status
);
641 nci_req_complete(ndev
, status
);
646 static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev
*ndev
,
649 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
650 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
651 u8 status
= skb
->data
[0];
653 dev_dbg(dev
, "%s: status 0x%x\n", __func__
, status
);
654 nci_req_complete(ndev
, status
);
659 static int fdp_nci_core_get_config_rsp_packet(struct nci_dev
*ndev
,
662 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
663 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
664 struct nci_core_get_config_rsp
*rsp
= (void *) skb
->data
;
667 if (rsp
->status
== NCI_STATUS_OK
) {
670 for (i
= 0; i
< 4; i
++) {
673 case NCI_PARAM_ID_FW_RAM_VERSION
:
675 info
->ram_version
= le32_to_cpup((__le32
*) p
);
678 case NCI_PARAM_ID_FW_OTP_VERSION
:
680 info
->otp_version
= le32_to_cpup((__le32
*) p
);
683 case NCI_PARAM_ID_OTP_LIMITED_VERSION
:
685 info
->otp_version
= le32_to_cpup((__le32
*) p
);
688 case NCI_PARAM_ID_KEY_INDEX_ID
:
690 info
->key_index
= *p
++;
695 dev_dbg(dev
, "OTP version %d\n", info
->otp_version
);
696 dev_dbg(dev
, "RAM version %d\n", info
->ram_version
);
697 dev_dbg(dev
, "key index %d\n", info
->key_index
);
698 dev_dbg(dev
, "%s: status 0x%x\n", __func__
, rsp
->status
);
700 nci_req_complete(ndev
, rsp
->status
);
705 static struct nci_driver_ops fdp_core_ops
[] = {
707 .opcode
= NCI_OP_CORE_GET_CONFIG_RSP
,
708 .rsp
= fdp_nci_core_get_config_rsp_packet
,
711 .opcode
= NCI_OP_CORE_RESET_NTF
,
712 .ntf
= fdp_nci_core_reset_ntf_packet
,
716 static struct nci_driver_ops fdp_prop_ops
[] = {
718 .opcode
= nci_opcode_pack(NCI_GID_PROP
, NCI_OP_PROP_PATCH_OID
),
719 .rsp
= fdp_nci_prop_patch_rsp_packet
,
720 .ntf
= fdp_nci_prop_patch_ntf_packet
,
723 .opcode
= nci_opcode_pack(NCI_GID_PROP
,
724 NCI_OP_PROP_SET_PDATA_OID
),
725 .rsp
= fdp_nci_prop_set_production_data_rsp_packet
,
729 struct nci_ops nci_ops
= {
730 .open
= fdp_nci_open
,
731 .close
= fdp_nci_close
,
732 .send
= fdp_nci_send
,
733 .setup
= fdp_nci_setup
,
734 .post_setup
= fdp_nci_post_setup
,
735 .prop_ops
= fdp_prop_ops
,
736 .n_prop_ops
= ARRAY_SIZE(fdp_prop_ops
),
737 .core_ops
= fdp_core_ops
,
738 .n_core_ops
= ARRAY_SIZE(fdp_core_ops
),
741 int fdp_nci_probe(struct fdp_i2c_phy
*phy
, struct nfc_phy_ops
*phy_ops
,
742 struct nci_dev
**ndevp
, int tx_headroom
,
743 int tx_tailroom
, u8 clock_type
, u32 clock_freq
,
746 struct device
*dev
= &phy
->i2c_dev
->dev
;
747 struct fdp_nci_info
*info
;
748 struct nci_dev
*ndev
;
752 info
= kzalloc(sizeof(struct fdp_nci_info
), GFP_KERNEL
);
759 info
->phy_ops
= phy_ops
;
760 info
->clock_type
= clock_type
;
761 info
->clock_freq
= clock_freq
;
762 info
->fw_vsc_cfg
= fw_vsc_cfg
;
764 init_waitqueue_head(&info
->setup_wq
);
766 protocols
= NFC_PROTO_JEWEL_MASK
|
767 NFC_PROTO_MIFARE_MASK
|
768 NFC_PROTO_FELICA_MASK
|
769 NFC_PROTO_ISO14443_MASK
|
770 NFC_PROTO_ISO14443_B_MASK
|
771 NFC_PROTO_NFC_DEP_MASK
|
772 NFC_PROTO_ISO15693_MASK
;
774 ndev
= nci_allocate_device(&nci_ops
, protocols
, tx_headroom
,
777 nfc_err(dev
, "Cannot allocate nfc ndev\n");
782 r
= nci_register_device(ndev
);
789 nci_set_drvdata(ndev
, info
);
794 nci_free_device(ndev
);
800 EXPORT_SYMBOL(fdp_nci_probe
);
802 void fdp_nci_remove(struct nci_dev
*ndev
)
804 struct fdp_nci_info
*info
= nci_get_drvdata(ndev
);
805 struct device
*dev
= &info
->phy
->i2c_dev
->dev
;
807 dev_dbg(dev
, "%s\n", __func__
);
809 nci_unregister_device(ndev
);
810 nci_free_device(ndev
);
813 EXPORT_SYMBOL(fdp_nci_remove
);
815 MODULE_LICENSE("GPL");
816 MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
817 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");