blob: c6b3334f24c9ea3ba53a65274859ce7becabc095 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Robert Dolcaa06347c2015-10-22 12:11:42 +03002/* -------------------------------------------------------------------------
3 * Copyright (C) 2014-2016, Intel Corporation
4 *
Robert Dolcaa06347c2015-10-22 12:11:42 +03005 * -------------------------------------------------------------------------
6 */
7
8#include <linux/module.h>
9#include <linux/nfc.h>
10#include <linux/i2c.h>
11#include <linux/delay.h>
12#include <linux/firmware.h>
13#include <net/nfc/nci_core.h>
14
15#include "fdp.h"
16
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
21
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
26
27#define NCI_PATCH_TYPE_RAM 0x00
28#define NCI_PATCH_TYPE_OTP 0x01
29#define NCI_PATCH_TYPE_EOT 0xFF
30
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
35
36#define NCI_GID_PROP 0x0F
37#define NCI_OP_PROP_PATCH_OID 0x08
38#define NCI_OP_PROP_SET_PDATA_OID 0x23
39
40struct fdp_nci_info {
Krzysztof Kozlowski7a5e98d2021-07-24 23:47:36 +020041 const struct nfc_phy_ops *phy_ops;
Robert Dolcaa06347c2015-10-22 12:11:42 +030042 struct fdp_i2c_phy *phy;
43 struct nci_dev *ndev;
44
45 const struct firmware *otp_patch;
46 const struct firmware *ram_patch;
47 u32 otp_patch_version;
48 u32 ram_patch_version;
49
50 u32 otp_version;
51 u32 ram_version;
52 u32 limited_otp_version;
53 u8 key_index;
54
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +020055 const u8 *fw_vsc_cfg;
Robert Dolcaa06347c2015-10-22 12:11:42 +030056 u8 clock_type;
57 u32 clock_freq;
58
59 atomic_t data_pkt_counter;
60 void (*data_pkt_counter_cb)(struct nci_dev *ndev);
61 u8 setup_patch_sent;
62 u8 setup_patch_ntf;
63 u8 setup_patch_status;
64 u8 setup_reset_ntf;
65 wait_queue_head_t setup_wq;
66};
67
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +020068static const u8 nci_core_get_config_otp_ram_version[5] = {
Robert Dolcaa06347c2015-10-22 12:11:42 +030069 0x04,
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
74};
75
76struct nci_core_get_config_rsp {
77 u8 status;
78 u8 count;
Gustavo A. R. Silvada60fbe2020-02-26 15:20:00 -060079 u8 data[];
Robert Dolcaa06347c2015-10-22 12:11:42 +030080};
81
82static int fdp_nci_create_conn(struct nci_dev *ndev)
83{
84 struct fdp_nci_info *info = nci_get_drvdata(ndev);
85 struct core_conn_create_dest_spec_params param;
86 int r;
87
88 /* proprietary destination specific paramerer without value */
89 param.type = FDP_PATCH_CONN_PARAM_TYPE;
90 param.length = 0x00;
91
92 r = nci_core_conn_create(info->ndev, FDP_PATCH_CONN_DEST, 1,
93 sizeof(param), &param);
94 if (r)
95 return r;
96
Christophe Ricard9b8d1a42016-04-30 09:12:51 +020097 return nci_get_conn_info_by_dest_type_params(ndev,
98 FDP_PATCH_CONN_DEST, NULL);
Robert Dolcaa06347c2015-10-22 12:11:42 +030099}
100
101static inline int fdp_nci_get_versions(struct nci_dev *ndev)
102{
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);
106}
107
108static inline int fdp_nci_patch_cmd(struct nci_dev *ndev, u8 type)
109{
110 return nci_prop_cmd(ndev, NCI_OP_PROP_PATCH_OID, sizeof(type), &type);
111}
112
113static inline int fdp_nci_set_production_data(struct nci_dev *ndev, u8 len,
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +0200114 const char *data)
Robert Dolcaa06347c2015-10-22 12:11:42 +0300115{
116 return nci_prop_cmd(ndev, NCI_OP_PROP_SET_PDATA_OID, len, data);
117}
118
119static int fdp_nci_set_clock(struct nci_dev *ndev, u8 clock_type,
120 u32 clock_freq)
121{
122 u32 fc = 13560;
123 u32 nd, num, delta;
124 char data[9];
125
126 nd = (24 * fc) / clock_freq;
127 delta = 24 * fc - nd * clock_freq;
128 num = (32768 * delta) / clock_freq;
129
130 data[0] = 0x00;
131 data[1] = 0x00;
132 data[2] = 0x00;
133
134 data[3] = 0x10;
135 data[4] = 0x04;
136 data[5] = num & 0xFF;
137 data[6] = (num >> 8) & 0xff;
138 data[7] = nd;
139 data[8] = clock_type;
140
141 return fdp_nci_set_production_data(ndev, 9, data);
142}
143
144static void fdp_nci_send_patch_cb(struct nci_dev *ndev)
145{
146 struct fdp_nci_info *info = nci_get_drvdata(ndev);
147
148 info->setup_patch_sent = 1;
149 wake_up(&info->setup_wq);
150}
151
Krzysztof Kozlowskicd4375d2021-05-28 08:41:49 -0400152/*
Robert Dolcaa06347c2015-10-22 12:11:42 +0300153 * Register a packet sent counter and a callback
154 *
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.
158 */
159static void fdp_nci_set_data_pkt_counter(struct nci_dev *ndev,
160 void (*cb)(struct nci_dev *ndev), int count)
161{
162 struct fdp_nci_info *info = nci_get_drvdata(ndev);
163 struct device *dev = &info->phy->i2c_dev->dev;
164
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;
168}
169
Krzysztof Kozlowskicd4375d2021-05-28 08:41:49 -0400170/*
Robert Dolcaa06347c2015-10-22 12:11:42 +0300171 * 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.
176 *
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
Bhaskar Chowdhury0853f5a2021-03-22 06:24:30 +0530179 * NFCC_PATCH_NTF packet and a NCI_OP_CORE_RESET_NTF packet.
Robert Dolcaa06347c2015-10-22 12:11:42 +0300180 */
181static int fdp_nci_send_patch(struct nci_dev *ndev, u8 conn_id, u8 type)
182{
183 struct fdp_nci_info *info = nci_get_drvdata(ndev);
184 const struct firmware *fw;
185 struct sk_buff *skb;
186 unsigned long len;
Dan Carpenter0dcdf9f2020-03-20 16:21:17 +0300187 int max_size, payload_size;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300188 int rc = 0;
189
190 if ((type == NCI_PATCH_TYPE_OTP && !info->otp_patch) ||
191 (type == NCI_PATCH_TYPE_RAM && !info->ram_patch))
192 return -EINVAL;
193
194 if (type == NCI_PATCH_TYPE_OTP)
195 fw = info->otp_patch;
196 else
197 fw = info->ram_patch;
198
199 max_size = nci_conn_max_data_pkt_payload_size(ndev, conn_id);
200 if (max_size <= 0)
201 return -EINVAL;
202
203 len = fw->size;
204
205 fdp_nci_set_data_pkt_counter(ndev, fdp_nci_send_patch_cb,
206 DIV_ROUND_UP(fw->size, max_size));
207
208 while (len) {
209
Dan Carpenter0dcdf9f2020-03-20 16:21:17 +0300210 payload_size = min_t(unsigned long, max_size, len);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300211
212 skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + payload_size),
213 GFP_KERNEL);
214 if (!skb) {
215 fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
216 return -ENOMEM;
217 }
218
219
220 skb_reserve(skb, NCI_CTRL_HDR_SIZE);
221
Johannes Berg59ae1d12017-06-16 14:29:20 +0200222 skb_put_data(skb, fw->data + (fw->size - len), payload_size);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300223
224 rc = nci_send_data(ndev, conn_id, skb);
225
226 if (rc) {
227 fdp_nci_set_data_pkt_counter(ndev, NULL, 0);
228 return rc;
229 }
230
231 len -= payload_size;
232 }
233
234 return rc;
235}
236
237static int fdp_nci_open(struct nci_dev *ndev)
238{
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +0200239 const struct fdp_nci_info *info = nci_get_drvdata(ndev);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300240
zuoqilin92a310c2021-03-18 21:36:40 +0800241 return info->phy_ops->enable(info->phy);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300242}
243
244static int fdp_nci_close(struct nci_dev *ndev)
245{
Robert Dolcaa06347c2015-10-22 12:11:42 +0300246 return 0;
247}
248
249static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
250{
251 struct fdp_nci_info *info = nci_get_drvdata(ndev);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300252
253 if (atomic_dec_and_test(&info->data_pkt_counter))
254 info->data_pkt_counter_cb(ndev);
255
256 return info->phy_ops->write(info->phy, skb);
257}
258
Robert Dolcaa06347c2015-10-22 12:11:42 +0300259static int fdp_nci_request_firmware(struct nci_dev *ndev)
260{
261 struct fdp_nci_info *info = nci_get_drvdata(ndev);
262 struct device *dev = &info->phy->i2c_dev->dev;
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +0200263 const u8 *data;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300264 int r;
265
266 r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev);
267 if (r < 0) {
268 nfc_err(dev, "RAM patch request error\n");
wengjianfeng43fa32d2021-06-10 10:46:16 +0800269 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300270 }
271
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +0200272 data = info->ram_patch->data;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300273 info->ram_patch_version =
274 data[FDP_FW_HEADER_SIZE] |
275 (data[FDP_FW_HEADER_SIZE + 1] << 8) |
276 (data[FDP_FW_HEADER_SIZE + 2] << 16) |
277 (data[FDP_FW_HEADER_SIZE + 3] << 24);
278
Krzysztof Kozlowski6c755b12021-07-29 12:40:17 +0200279 dev_dbg(dev, "RAM patch version: %d, size: %zu\n",
280 info->ram_patch_version, info->ram_patch->size);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300281
282
283 r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev);
284 if (r < 0) {
285 nfc_err(dev, "OTP patch request error\n");
wengjianfeng43fa32d2021-06-10 10:46:16 +0800286 return 0;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300287 }
288
289 data = (u8 *) info->otp_patch->data;
290 info->otp_patch_version =
291 data[FDP_FW_HEADER_SIZE] |
292 (data[FDP_FW_HEADER_SIZE + 1] << 8) |
293 (data[FDP_FW_HEADER_SIZE+2] << 16) |
294 (data[FDP_FW_HEADER_SIZE+3] << 24);
295
Krzysztof Kozlowski6c755b12021-07-29 12:40:17 +0200296 dev_dbg(dev, "OTP patch version: %d, size: %zu\n",
297 info->otp_patch_version, info->otp_patch->size);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300298 return 0;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300299}
300
301static void fdp_nci_release_firmware(struct nci_dev *ndev)
302{
303 struct fdp_nci_info *info = nci_get_drvdata(ndev);
304
305 if (info->otp_patch) {
306 release_firmware(info->otp_patch);
307 info->otp_patch = NULL;
308 }
309
310 if (info->ram_patch) {
311 release_firmware(info->ram_patch);
Colin Ian Kingf36acc32016-03-22 23:17:00 +0000312 info->ram_patch = NULL;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300313 }
314}
315
316static int fdp_nci_patch_otp(struct nci_dev *ndev)
317{
318 struct fdp_nci_info *info = nci_get_drvdata(ndev);
319 struct device *dev = &info->phy->i2c_dev->dev;
Geert Uytterhoevenfa1ce542016-06-05 11:17:10 +0200320 int conn_id;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300321 int r = 0;
322
323 if (info->otp_version >= info->otp_patch_version)
wengjianfeng872fff32021-04-07 11:16:38 +0800324 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300325
326 info->setup_patch_sent = 0;
327 info->setup_reset_ntf = 0;
328 info->setup_patch_ntf = 0;
329
330 /* Patch init request */
331 r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP);
332 if (r)
wengjianfeng872fff32021-04-07 11:16:38 +0800333 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300334
335 /* Patch data connection creation */
336 conn_id = fdp_nci_create_conn(ndev);
wengjianfeng872fff32021-04-07 11:16:38 +0800337 if (conn_id < 0)
338 return conn_id;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300339
340 /* Send the patch over the data connection */
341 r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP);
342 if (r)
wengjianfeng872fff32021-04-07 11:16:38 +0800343 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300344
345 /* Wait for all the packets to be send over i2c */
346 wait_event_interruptible(info->setup_wq,
347 info->setup_patch_sent == 1);
348
349 /* make sure that the NFCC processed the last data packet */
350 msleep(FDP_FW_UPDATE_SLEEP);
351
352 /* Close the data connection */
353 r = nci_core_conn_close(info->ndev, conn_id);
354 if (r)
wengjianfeng872fff32021-04-07 11:16:38 +0800355 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300356
357 /* Patch finish message */
358 if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
359 nfc_err(dev, "OTP patch error 0x%x\n", r);
wengjianfeng872fff32021-04-07 11:16:38 +0800360 return -EINVAL;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300361 }
362
363 /* If the patch notification didn't arrive yet, wait for it */
364 wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
365
366 /* Check if the patching was successful */
367 r = info->setup_patch_status;
368 if (r) {
369 nfc_err(dev, "OTP patch error 0x%x\n", r);
wengjianfeng872fff32021-04-07 11:16:38 +0800370 return -EINVAL;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300371 }
372
373 /*
374 * We need to wait for the reset notification before we
375 * can continue
376 */
377 wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
378
Robert Dolcaa06347c2015-10-22 12:11:42 +0300379 return r;
380}
381
382static int fdp_nci_patch_ram(struct nci_dev *ndev)
383{
384 struct fdp_nci_info *info = nci_get_drvdata(ndev);
385 struct device *dev = &info->phy->i2c_dev->dev;
Geert Uytterhoevenfa1ce542016-06-05 11:17:10 +0200386 int conn_id;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300387 int r = 0;
388
389 if (info->ram_version >= info->ram_patch_version)
wengjianfeng872fff32021-04-07 11:16:38 +0800390 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300391
392 info->setup_patch_sent = 0;
393 info->setup_reset_ntf = 0;
394 info->setup_patch_ntf = 0;
395
396 /* Patch init request */
397 r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM);
398 if (r)
wengjianfeng872fff32021-04-07 11:16:38 +0800399 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300400
401 /* Patch data connection creation */
402 conn_id = fdp_nci_create_conn(ndev);
wengjianfeng872fff32021-04-07 11:16:38 +0800403 if (conn_id < 0)
404 return conn_id;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300405
406 /* Send the patch over the data connection */
407 r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM);
408 if (r)
wengjianfeng872fff32021-04-07 11:16:38 +0800409 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300410
411 /* Wait for all the packets to be send over i2c */
412 wait_event_interruptible(info->setup_wq,
413 info->setup_patch_sent == 1);
414
415 /* make sure that the NFCC processed the last data packet */
416 msleep(FDP_FW_UPDATE_SLEEP);
417
418 /* Close the data connection */
419 r = nci_core_conn_close(info->ndev, conn_id);
420 if (r)
wengjianfeng872fff32021-04-07 11:16:38 +0800421 return r;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300422
423 /* Patch finish message */
424 if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
425 nfc_err(dev, "RAM patch error 0x%x\n", r);
wengjianfeng872fff32021-04-07 11:16:38 +0800426 return -EINVAL;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300427 }
428
429 /* If the patch notification didn't arrive yet, wait for it */
430 wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
431
432 /* Check if the patching was successful */
433 r = info->setup_patch_status;
434 if (r) {
435 nfc_err(dev, "RAM patch error 0x%x\n", r);
wengjianfeng872fff32021-04-07 11:16:38 +0800436 return -EINVAL;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300437 }
438
439 /*
440 * We need to wait for the reset notification before we
441 * can continue
442 */
443 wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
444
Robert Dolcaa06347c2015-10-22 12:11:42 +0300445 return r;
446}
447
448static int fdp_nci_setup(struct nci_dev *ndev)
449{
450 /* Format: total length followed by an NCI packet */
451 struct fdp_nci_info *info = nci_get_drvdata(ndev);
452 struct device *dev = &info->phy->i2c_dev->dev;
453 int r;
454 u8 patched = 0;
455
Robert Dolcaa06347c2015-10-22 12:11:42 +0300456 r = nci_core_init(ndev);
457 if (r)
458 goto error;
459
460 /* Get RAM and OTP version */
461 r = fdp_nci_get_versions(ndev);
462 if (r)
463 goto error;
464
465 /* Load firmware from disk */
466 r = fdp_nci_request_firmware(ndev);
467 if (r)
468 goto error;
469
470 /* Update OTP */
471 if (info->otp_version < info->otp_patch_version) {
472 r = fdp_nci_patch_otp(ndev);
473 if (r)
474 goto error;
475 patched = 1;
476 }
477
478 /* Update RAM */
479 if (info->ram_version < info->ram_patch_version) {
480 r = fdp_nci_patch_ram(ndev);
481 if (r)
482 goto error;
483 patched = 1;
484 }
485
486 /* Release the firmware buffers */
487 fdp_nci_release_firmware(ndev);
488
489 /* If a patch was applied the new version is checked */
490 if (patched) {
491 r = nci_core_init(ndev);
492 if (r)
493 goto error;
494
495 r = fdp_nci_get_versions(ndev);
496 if (r)
497 goto error;
498
499 if (info->otp_version != info->otp_patch_version ||
500 info->ram_version != info->ram_patch_version) {
501 nfc_err(dev, "Firmware update failed");
502 r = -EINVAL;
503 goto error;
504 }
505 }
506
507 /*
508 * We initialized the devices but the NFC subsystem expects
509 * it to not be initialized.
510 */
511 return nci_core_reset(ndev);
512
513error:
514 fdp_nci_release_firmware(ndev);
515 nfc_err(dev, "Setup error %d\n", r);
516 return r;
517}
518
519static int fdp_nci_post_setup(struct nci_dev *ndev)
520{
521 struct fdp_nci_info *info = nci_get_drvdata(ndev);
522 struct device *dev = &info->phy->i2c_dev->dev;
523 int r;
524
525 /* Check if the device has VSC */
526 if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) {
527
528 /* Set the vendor specific configuration */
529 r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3],
530 &info->fw_vsc_cfg[4]);
531 if (r) {
532 nfc_err(dev, "Vendor specific config set error %d\n",
533 r);
534 return r;
535 }
536 }
537
538 /* Set clock type and frequency */
539 r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq);
540 if (r) {
541 nfc_err(dev, "Clock set error %d\n", r);
542 return r;
543 }
544
545 /*
546 * In order to apply the VSC FDP needs a reset
547 */
548 r = nci_core_reset(ndev);
549 if (r)
550 return r;
551
552 /**
553 * The nci core was initialized when post setup was called
554 * so we leave it like that
555 */
556 return nci_core_init(ndev);
557}
558
559static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev,
560 struct sk_buff *skb)
561{
562 struct fdp_nci_info *info = nci_get_drvdata(ndev);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300563
Robert Dolcaa06347c2015-10-22 12:11:42 +0300564 info->setup_reset_ntf = 1;
565 wake_up(&info->setup_wq);
566
567 return 0;
568}
569
570static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev,
571 struct sk_buff *skb)
572{
573 struct fdp_nci_info *info = nci_get_drvdata(ndev);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300574
Robert Dolcaa06347c2015-10-22 12:11:42 +0300575 info->setup_patch_ntf = 1;
576 info->setup_patch_status = skb->data[0];
577 wake_up(&info->setup_wq);
578
579 return 0;
580}
581
582static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev,
583 struct sk_buff *skb)
584{
585 struct fdp_nci_info *info = nci_get_drvdata(ndev);
586 struct device *dev = &info->phy->i2c_dev->dev;
587 u8 status = skb->data[0];
588
589 dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
590 nci_req_complete(ndev, status);
591
592 return 0;
593}
594
595static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev,
596 struct sk_buff *skb)
597{
598 struct fdp_nci_info *info = nci_get_drvdata(ndev);
599 struct device *dev = &info->phy->i2c_dev->dev;
600 u8 status = skb->data[0];
601
602 dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
603 nci_req_complete(ndev, status);
604
605 return 0;
606}
607
608static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev,
609 struct sk_buff *skb)
610{
611 struct fdp_nci_info *info = nci_get_drvdata(ndev);
612 struct device *dev = &info->phy->i2c_dev->dev;
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +0200613 const struct nci_core_get_config_rsp *rsp = (void *) skb->data;
Krzysztof Kozlowskic3e26b62021-07-29 12:40:18 +0200614 unsigned int i;
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +0200615 const u8 *p;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300616
617 if (rsp->status == NCI_STATUS_OK) {
618
619 p = rsp->data;
620 for (i = 0; i < 4; i++) {
621
622 switch (*p++) {
623 case NCI_PARAM_ID_FW_RAM_VERSION:
624 p++;
625 info->ram_version = le32_to_cpup((__le32 *) p);
626 p += 4;
627 break;
628 case NCI_PARAM_ID_FW_OTP_VERSION:
629 p++;
630 info->otp_version = le32_to_cpup((__le32 *) p);
631 p += 4;
632 break;
633 case NCI_PARAM_ID_OTP_LIMITED_VERSION:
634 p++;
635 info->otp_version = le32_to_cpup((__le32 *) p);
636 p += 4;
637 break;
638 case NCI_PARAM_ID_KEY_INDEX_ID:
639 p++;
640 info->key_index = *p++;
641 }
642 }
643 }
644
645 dev_dbg(dev, "OTP version %d\n", info->otp_version);
646 dev_dbg(dev, "RAM version %d\n", info->ram_version);
647 dev_dbg(dev, "key index %d\n", info->key_index);
648 dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status);
649
650 nci_req_complete(ndev, rsp->status);
651
652 return 0;
653}
654
Krzysztof Kozlowskicb8caa32021-07-24 23:47:35 +0200655static const struct nci_driver_ops fdp_core_ops[] = {
Robert Dolcaa06347c2015-10-22 12:11:42 +0300656 {
657 .opcode = NCI_OP_CORE_GET_CONFIG_RSP,
658 .rsp = fdp_nci_core_get_config_rsp_packet,
659 },
660 {
661 .opcode = NCI_OP_CORE_RESET_NTF,
662 .ntf = fdp_nci_core_reset_ntf_packet,
663 },
664};
665
Krzysztof Kozlowskicb8caa32021-07-24 23:47:35 +0200666static const struct nci_driver_ops fdp_prop_ops[] = {
Robert Dolcaa06347c2015-10-22 12:11:42 +0300667 {
668 .opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID),
669 .rsp = fdp_nci_prop_patch_rsp_packet,
670 .ntf = fdp_nci_prop_patch_ntf_packet,
671 },
672 {
673 .opcode = nci_opcode_pack(NCI_GID_PROP,
674 NCI_OP_PROP_SET_PDATA_OID),
675 .rsp = fdp_nci_prop_set_production_data_rsp_packet,
676 },
677};
678
Krzysztof Kozlowskib9c28282021-07-24 23:47:33 +0200679static const struct nci_ops nci_ops = {
Robert Dolcaa06347c2015-10-22 12:11:42 +0300680 .open = fdp_nci_open,
681 .close = fdp_nci_close,
682 .send = fdp_nci_send,
683 .setup = fdp_nci_setup,
684 .post_setup = fdp_nci_post_setup,
685 .prop_ops = fdp_prop_ops,
686 .n_prop_ops = ARRAY_SIZE(fdp_prop_ops),
687 .core_ops = fdp_core_ops,
688 .n_core_ops = ARRAY_SIZE(fdp_core_ops),
689};
690
Krzysztof Kozlowski7a5e98d2021-07-24 23:47:36 +0200691int fdp_nci_probe(struct fdp_i2c_phy *phy, const struct nfc_phy_ops *phy_ops,
Robert Dolcaa06347c2015-10-22 12:11:42 +0300692 struct nci_dev **ndevp, int tx_headroom,
693 int tx_tailroom, u8 clock_type, u32 clock_freq,
Krzysztof Kozlowski3d463dd2021-07-29 12:40:19 +0200694 const u8 *fw_vsc_cfg)
Robert Dolcaa06347c2015-10-22 12:11:42 +0300695{
696 struct device *dev = &phy->i2c_dev->dev;
697 struct fdp_nci_info *info;
698 struct nci_dev *ndev;
699 u32 protocols;
700 int r;
701
Andy Shevchenko7b9fcda2017-06-19 13:08:50 +0300702 info = devm_kzalloc(dev, sizeof(struct fdp_nci_info), GFP_KERNEL);
703 if (!info)
704 return -ENOMEM;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300705
706 info->phy = phy;
707 info->phy_ops = phy_ops;
708 info->clock_type = clock_type;
709 info->clock_freq = clock_freq;
710 info->fw_vsc_cfg = fw_vsc_cfg;
711
712 init_waitqueue_head(&info->setup_wq);
713
714 protocols = NFC_PROTO_JEWEL_MASK |
715 NFC_PROTO_MIFARE_MASK |
716 NFC_PROTO_FELICA_MASK |
717 NFC_PROTO_ISO14443_MASK |
718 NFC_PROTO_ISO14443_B_MASK |
719 NFC_PROTO_NFC_DEP_MASK |
720 NFC_PROTO_ISO15693_MASK;
721
Krzysztof Kozlowskib9c28282021-07-24 23:47:33 +0200722 BUILD_BUG_ON(ARRAY_SIZE(fdp_prop_ops) > NCI_MAX_PROPRIETARY_CMD);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300723 ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom,
724 tx_tailroom);
725 if (!ndev) {
726 nfc_err(dev, "Cannot allocate nfc ndev\n");
Andy Shevchenko7b9fcda2017-06-19 13:08:50 +0300727 return -ENOMEM;
Robert Dolcaa06347c2015-10-22 12:11:42 +0300728 }
729
730 r = nci_register_device(ndev);
731 if (r)
732 goto err_regdev;
733
734 *ndevp = ndev;
735 info->ndev = ndev;
736
737 nci_set_drvdata(ndev, info);
738
739 return 0;
740
741err_regdev:
742 nci_free_device(ndev);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300743 return r;
744}
745EXPORT_SYMBOL(fdp_nci_probe);
746
747void fdp_nci_remove(struct nci_dev *ndev)
748{
Robert Dolcaa06347c2015-10-22 12:11:42 +0300749 nci_unregister_device(ndev);
750 nci_free_device(ndev);
Robert Dolcaa06347c2015-10-22 12:11:42 +0300751}
752EXPORT_SYMBOL(fdp_nci_remove);
753
754MODULE_LICENSE("GPL");
755MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
756MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");