1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic driver for NXP NCI NFC chips
5 * Copyright (C) 2014 NXP Semiconductors All rights reserved.
7 * Author: Clément Perrochaud <clement.perrochaud@nxp.com>
9 * Derived from PN544 device driver:
10 * Copyright (C) 2012 Intel Corporation. All rights reserved.
13 #include <linux/completion.h>
14 #include <linux/firmware.h>
15 #include <linux/nfc.h>
16 #include <linux/unaligned.h>
20 /* Crypto operations can take up to 30 seconds */
21 #define NXP_NCI_FW_ANSWER_TIMEOUT msecs_to_jiffies(30000)
23 #define NXP_NCI_FW_CMD_RESET 0xF0
24 #define NXP_NCI_FW_CMD_GETVERSION 0xF1
25 #define NXP_NCI_FW_CMD_CHECKINTEGRITY 0xE0
26 #define NXP_NCI_FW_CMD_WRITE 0xC0
27 #define NXP_NCI_FW_CMD_READ 0xA2
28 #define NXP_NCI_FW_CMD_GETSESSIONSTATE 0xF2
29 #define NXP_NCI_FW_CMD_LOG 0xA7
30 #define NXP_NCI_FW_CMD_FORCE 0xD0
31 #define NXP_NCI_FW_CMD_GET_DIE_ID 0xF4
33 #define NXP_NCI_FW_CHUNK_FLAG 0x0400
35 #define NXP_NCI_FW_RESULT_OK 0x00
36 #define NXP_NCI_FW_RESULT_INVALID_ADDR 0x01
37 #define NXP_NCI_FW_RESULT_GENERIC_ERROR 0x02
38 #define NXP_NCI_FW_RESULT_UNKNOWN_CMD 0x0B
39 #define NXP_NCI_FW_RESULT_ABORTED_CMD 0x0C
40 #define NXP_NCI_FW_RESULT_PLL_ERROR 0x0D
41 #define NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR 0x1E
42 #define NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR 0x1F
43 #define NXP_NCI_FW_RESULT_MEM_BSY 0x20
44 #define NXP_NCI_FW_RESULT_SIGNATURE_ERROR 0x21
45 #define NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR 0x24
46 #define NXP_NCI_FW_RESULT_PROTOCOL_ERROR 0x28
47 #define NXP_NCI_FW_RESULT_SFWU_DEGRADED 0x2A
48 #define NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK 0x2D
49 #define NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK 0x2E
50 #define NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5 0xC5
52 void nxp_nci_fw_work_complete(struct nxp_nci_info
*info
, int result
)
54 struct nxp_nci_fw_info
*fw_info
= &info
->fw_info
;
57 if (info
->phy_ops
->set_mode
) {
58 r
= info
->phy_ops
->set_mode(info
->phy_id
, NXP_NCI_MODE_COLD
);
59 if (r
< 0 && result
== 0)
63 info
->mode
= NXP_NCI_MODE_COLD
;
66 release_firmware(fw_info
->fw
);
70 nfc_fw_download_done(info
->ndev
->nfc_dev
, fw_info
->name
, (u32
) -result
);
73 /* crc_ccitt cannot be used since it is computed MSB first and not LSB first */
74 static u16
nxp_nci_fw_crc(u8
const *buffer
, size_t len
)
79 crc
= ((crc
>> 8) | (crc
<< 8)) ^ *buffer
++;
80 crc
^= (crc
& 0xff) >> 4;
81 crc
^= (crc
& 0xff) << 12;
82 crc
^= (crc
& 0xff) << 5;
88 static int nxp_nci_fw_send_chunk(struct nxp_nci_info
*info
)
90 struct nxp_nci_fw_info
*fw_info
= &info
->fw_info
;
97 skb
= nci_skb_alloc(info
->ndev
, info
->max_payload
, GFP_KERNEL
);
101 chunk_len
= info
->max_payload
- NXP_NCI_FW_HDR_LEN
- NXP_NCI_FW_CRC_LEN
;
102 remaining_len
= fw_info
->frame_size
- fw_info
->written
;
104 if (remaining_len
> chunk_len
) {
105 header
= NXP_NCI_FW_CHUNK_FLAG
;
107 chunk_len
= remaining_len
;
111 header
|= chunk_len
& NXP_NCI_FW_FRAME_LEN_MASK
;
112 put_unaligned_be16(header
, skb_put(skb
, NXP_NCI_FW_HDR_LEN
));
114 skb_put_data(skb
, fw_info
->data
+ fw_info
->written
, chunk_len
);
116 crc
= nxp_nci_fw_crc(skb
->data
, chunk_len
+ NXP_NCI_FW_HDR_LEN
);
117 put_unaligned_be16(crc
, skb_put(skb
, NXP_NCI_FW_CRC_LEN
));
119 r
= info
->phy_ops
->write(info
->phy_id
, skb
);
128 static int nxp_nci_fw_send(struct nxp_nci_info
*info
)
130 struct nxp_nci_fw_info
*fw_info
= &info
->fw_info
;
134 reinit_completion(&fw_info
->cmd_completion
);
136 if (fw_info
->written
== 0) {
137 fw_info
->frame_size
= get_unaligned_be16(fw_info
->data
) &
138 NXP_NCI_FW_FRAME_LEN_MASK
;
139 fw_info
->data
+= NXP_NCI_FW_HDR_LEN
;
140 fw_info
->size
-= NXP_NCI_FW_HDR_LEN
;
143 if (fw_info
->frame_size
> fw_info
->size
)
146 r
= nxp_nci_fw_send_chunk(info
);
150 fw_info
->written
+= r
;
152 if (*fw_info
->data
== NXP_NCI_FW_CMD_RESET
) {
153 fw_info
->cmd_result
= 0;
155 schedule_work(&fw_info
->work
);
157 completion_rc
= wait_for_completion_interruptible_timeout(
158 &fw_info
->cmd_completion
, NXP_NCI_FW_ANSWER_TIMEOUT
);
159 if (completion_rc
== 0)
166 void nxp_nci_fw_work(struct work_struct
*work
)
168 struct nxp_nci_info
*info
;
169 struct nxp_nci_fw_info
*fw_info
;
172 fw_info
= container_of(work
, struct nxp_nci_fw_info
, work
);
173 info
= container_of(fw_info
, struct nxp_nci_info
, fw_info
);
175 mutex_lock(&info
->info_lock
);
177 r
= fw_info
->cmd_result
;
181 if (fw_info
->written
== fw_info
->frame_size
) {
182 fw_info
->data
+= fw_info
->frame_size
;
183 fw_info
->size
-= fw_info
->frame_size
;
184 fw_info
->written
= 0;
187 if (fw_info
->size
> 0)
188 r
= nxp_nci_fw_send(info
);
191 if (r
< 0 || fw_info
->size
== 0)
192 nxp_nci_fw_work_complete(info
, r
);
193 mutex_unlock(&info
->info_lock
);
196 int nxp_nci_fw_download(struct nci_dev
*ndev
, const char *firmware_name
)
198 struct nxp_nci_info
*info
= nci_get_drvdata(ndev
);
199 struct nxp_nci_fw_info
*fw_info
= &info
->fw_info
;
202 mutex_lock(&info
->info_lock
);
204 if (!info
->phy_ops
->set_mode
|| !info
->phy_ops
->write
) {
206 goto fw_download_exit
;
209 if (!firmware_name
|| firmware_name
[0] == '\0') {
211 goto fw_download_exit
;
214 strcpy(fw_info
->name
, firmware_name
);
216 r
= request_firmware(&fw_info
->fw
, firmware_name
,
217 ndev
->nfc_dev
->dev
.parent
);
219 goto fw_download_exit
;
221 r
= info
->phy_ops
->set_mode(info
->phy_id
, NXP_NCI_MODE_FW
);
223 release_firmware(fw_info
->fw
);
224 goto fw_download_exit
;
227 info
->mode
= NXP_NCI_MODE_FW
;
229 fw_info
->data
= fw_info
->fw
->data
;
230 fw_info
->size
= fw_info
->fw
->size
;
231 fw_info
->written
= 0;
232 fw_info
->frame_size
= 0;
233 fw_info
->cmd_result
= 0;
235 schedule_work(&fw_info
->work
);
238 mutex_unlock(&info
->info_lock
);
242 static int nxp_nci_fw_read_status(u8 stat
)
245 case NXP_NCI_FW_RESULT_OK
:
247 case NXP_NCI_FW_RESULT_INVALID_ADDR
:
249 case NXP_NCI_FW_RESULT_UNKNOWN_CMD
:
251 case NXP_NCI_FW_RESULT_ABORTED_CMD
:
253 case NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR
:
254 return -EADDRNOTAVAIL
;
255 case NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR
:
257 case NXP_NCI_FW_RESULT_MEM_BSY
:
259 case NXP_NCI_FW_RESULT_SIGNATURE_ERROR
:
260 return -EKEYREJECTED
;
261 case NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR
:
263 case NXP_NCI_FW_RESULT_PROTOCOL_ERROR
:
265 case NXP_NCI_FW_RESULT_SFWU_DEGRADED
:
267 case NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK
:
269 case NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK
:
271 case NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5
:
278 static u16
nxp_nci_fw_check_crc(struct sk_buff
*skb
)
281 size_t len
= skb
->len
- NXP_NCI_FW_CRC_LEN
;
283 crc
= nxp_nci_fw_crc(skb
->data
, len
);
284 frame_crc
= get_unaligned_be16(skb
->data
+ len
);
286 return (crc
^ frame_crc
);
289 void nxp_nci_fw_recv_frame(struct nci_dev
*ndev
, struct sk_buff
*skb
)
291 struct nxp_nci_info
*info
= nci_get_drvdata(ndev
);
292 struct nxp_nci_fw_info
*fw_info
= &info
->fw_info
;
294 complete(&fw_info
->cmd_completion
);
297 if (nxp_nci_fw_check_crc(skb
) != 0x00)
298 fw_info
->cmd_result
= -EBADMSG
;
300 fw_info
->cmd_result
= nxp_nci_fw_read_status(*(u8
*)skb_pull(skb
, NXP_NCI_FW_HDR_LEN
));
303 fw_info
->cmd_result
= -EIO
;
307 schedule_work(&fw_info
->work
);
309 EXPORT_SYMBOL(nxp_nci_fw_recv_frame
);