xref: /openbmc/linux/drivers/nfc/fdp/fdp.c (revision eb09fc2d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -------------------------------------------------------------------------
3  * Copyright (C) 2014-2016, Intel Corporation
4  *
5  * -------------------------------------------------------------------------
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 
40 struct fdp_nci_info {
41 	const struct nfc_phy_ops *phy_ops;
42 	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 
55 	const u8 *fw_vsc_cfg;
56 	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 
68 static const u8 nci_core_get_config_otp_ram_version[5] = {
69 	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 
76 struct nci_core_get_config_rsp {
77 	u8 status;
78 	u8 count;
79 	u8 data[];
80 };
81 
fdp_nci_create_conn(struct nci_dev * ndev)82 static 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 
97 	return nci_get_conn_info_by_dest_type_params(ndev,
98 						     FDP_PATCH_CONN_DEST, NULL);
99 }
100 
fdp_nci_get_versions(struct nci_dev * ndev)101 static 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 
fdp_nci_patch_cmd(struct nci_dev * ndev,u8 type)108 static 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 
fdp_nci_set_production_data(struct nci_dev * ndev,u8 len,const char * data)113 static inline int fdp_nci_set_production_data(struct nci_dev *ndev, u8 len,
114 					      const char *data)
115 {
116 	return nci_prop_cmd(ndev, NCI_OP_PROP_SET_PDATA_OID, len, data);
117 }
118 
fdp_nci_set_clock(struct nci_dev * ndev,u8 clock_type,u32 clock_freq)119 static 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 
fdp_nci_send_patch_cb(struct nci_dev * ndev)144 static 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 
152 /*
153  * 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  */
fdp_nci_set_data_pkt_counter(struct nci_dev * ndev,void (* cb)(struct nci_dev * ndev),int count)159 static 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 
170 /*
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.
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
179  * NFCC_PATCH_NTF packet and a NCI_OP_CORE_RESET_NTF packet.
180  */
fdp_nci_send_patch(struct nci_dev * ndev,u8 conn_id,u8 type)181 static 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;
187 	int max_size, payload_size;
188 	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 
210 		payload_size = min_t(unsigned long, max_size, len);
211 
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 
222 		skb_put_data(skb, fw->data + (fw->size - len), payload_size);
223 
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 
fdp_nci_open(struct nci_dev * ndev)237 static int fdp_nci_open(struct nci_dev *ndev)
238 {
239 	const struct fdp_nci_info *info = nci_get_drvdata(ndev);
240 
241 	return info->phy_ops->enable(info->phy);
242 }
243 
fdp_nci_close(struct nci_dev * ndev)244 static int fdp_nci_close(struct nci_dev *ndev)
245 {
246 	return 0;
247 }
248 
fdp_nci_send(struct nci_dev * ndev,struct sk_buff * skb)249 static int fdp_nci_send(struct nci_dev *ndev, struct sk_buff *skb)
250 {
251 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
252 	int ret;
253 
254 	if (atomic_dec_and_test(&info->data_pkt_counter))
255 		info->data_pkt_counter_cb(ndev);
256 
257 	ret = info->phy_ops->write(info->phy, skb);
258 	if (ret < 0) {
259 		kfree_skb(skb);
260 		return ret;
261 	}
262 
263 	consume_skb(skb);
264 	return 0;
265 }
266 
fdp_nci_request_firmware(struct nci_dev * ndev)267 static int fdp_nci_request_firmware(struct nci_dev *ndev)
268 {
269 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
270 	struct device *dev = &info->phy->i2c_dev->dev;
271 	const u8 *data;
272 	int r;
273 
274 	r = request_firmware(&info->ram_patch, FDP_RAM_PATCH_NAME, dev);
275 	if (r < 0) {
276 		nfc_err(dev, "RAM patch request error\n");
277 		return r;
278 	}
279 
280 	data = info->ram_patch->data;
281 	info->ram_patch_version =
282 		data[FDP_FW_HEADER_SIZE] |
283 		(data[FDP_FW_HEADER_SIZE + 1] << 8) |
284 		(data[FDP_FW_HEADER_SIZE + 2] << 16) |
285 		(data[FDP_FW_HEADER_SIZE + 3] << 24);
286 
287 	dev_dbg(dev, "RAM patch version: %d, size: %zu\n",
288 		  info->ram_patch_version, info->ram_patch->size);
289 
290 
291 	r = request_firmware(&info->otp_patch, FDP_OTP_PATCH_NAME, dev);
292 	if (r < 0) {
293 		nfc_err(dev, "OTP patch request error\n");
294 		return 0;
295 	}
296 
297 	data = (u8 *) info->otp_patch->data;
298 	info->otp_patch_version =
299 		data[FDP_FW_HEADER_SIZE] |
300 		(data[FDP_FW_HEADER_SIZE + 1] << 8) |
301 		(data[FDP_FW_HEADER_SIZE+2] << 16) |
302 		(data[FDP_FW_HEADER_SIZE+3] << 24);
303 
304 	dev_dbg(dev, "OTP patch version: %d, size: %zu\n",
305 		 info->otp_patch_version, info->otp_patch->size);
306 	return 0;
307 }
308 
fdp_nci_release_firmware(struct nci_dev * ndev)309 static void fdp_nci_release_firmware(struct nci_dev *ndev)
310 {
311 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
312 
313 	if (info->otp_patch) {
314 		release_firmware(info->otp_patch);
315 		info->otp_patch = NULL;
316 	}
317 
318 	if (info->ram_patch) {
319 		release_firmware(info->ram_patch);
320 		info->ram_patch = NULL;
321 	}
322 }
323 
fdp_nci_patch_otp(struct nci_dev * ndev)324 static int fdp_nci_patch_otp(struct nci_dev *ndev)
325 {
326 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
327 	struct device *dev = &info->phy->i2c_dev->dev;
328 	int conn_id;
329 	int r = 0;
330 
331 	if (info->otp_version >= info->otp_patch_version)
332 		return r;
333 
334 	info->setup_patch_sent = 0;
335 	info->setup_reset_ntf = 0;
336 	info->setup_patch_ntf = 0;
337 
338 	/* Patch init request */
339 	r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_OTP);
340 	if (r)
341 		return r;
342 
343 	/* Patch data connection creation */
344 	conn_id = fdp_nci_create_conn(ndev);
345 	if (conn_id < 0)
346 		return conn_id;
347 
348 	/* Send the patch over the data connection */
349 	r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_OTP);
350 	if (r)
351 		return r;
352 
353 	/* Wait for all the packets to be send over i2c */
354 	wait_event_interruptible(info->setup_wq,
355 				 info->setup_patch_sent == 1);
356 
357 	/* make sure that the NFCC processed the last data packet */
358 	msleep(FDP_FW_UPDATE_SLEEP);
359 
360 	/* Close the data connection */
361 	r = nci_core_conn_close(info->ndev, conn_id);
362 	if (r)
363 		return r;
364 
365 	/* Patch finish message */
366 	if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
367 		nfc_err(dev, "OTP patch error 0x%x\n", r);
368 		return -EINVAL;
369 	}
370 
371 	/* If the patch notification didn't arrive yet, wait for it */
372 	wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
373 
374 	/* Check if the patching was successful */
375 	r = info->setup_patch_status;
376 	if (r) {
377 		nfc_err(dev, "OTP patch error 0x%x\n", r);
378 		return -EINVAL;
379 	}
380 
381 	/*
382 	 * We need to wait for the reset notification before we
383 	 * can continue
384 	 */
385 	wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
386 
387 	return r;
388 }
389 
fdp_nci_patch_ram(struct nci_dev * ndev)390 static int fdp_nci_patch_ram(struct nci_dev *ndev)
391 {
392 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
393 	struct device *dev = &info->phy->i2c_dev->dev;
394 	int conn_id;
395 	int r = 0;
396 
397 	if (info->ram_version >= info->ram_patch_version)
398 		return r;
399 
400 	info->setup_patch_sent = 0;
401 	info->setup_reset_ntf = 0;
402 	info->setup_patch_ntf = 0;
403 
404 	/* Patch init request */
405 	r = fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_RAM);
406 	if (r)
407 		return r;
408 
409 	/* Patch data connection creation */
410 	conn_id = fdp_nci_create_conn(ndev);
411 	if (conn_id < 0)
412 		return conn_id;
413 
414 	/* Send the patch over the data connection */
415 	r = fdp_nci_send_patch(ndev, conn_id, NCI_PATCH_TYPE_RAM);
416 	if (r)
417 		return r;
418 
419 	/* Wait for all the packets to be send over i2c */
420 	wait_event_interruptible(info->setup_wq,
421 				 info->setup_patch_sent == 1);
422 
423 	/* make sure that the NFCC processed the last data packet */
424 	msleep(FDP_FW_UPDATE_SLEEP);
425 
426 	/* Close the data connection */
427 	r = nci_core_conn_close(info->ndev, conn_id);
428 	if (r)
429 		return r;
430 
431 	/* Patch finish message */
432 	if (fdp_nci_patch_cmd(ndev, NCI_PATCH_TYPE_EOT)) {
433 		nfc_err(dev, "RAM patch error 0x%x\n", r);
434 		return -EINVAL;
435 	}
436 
437 	/* If the patch notification didn't arrive yet, wait for it */
438 	wait_event_interruptible(info->setup_wq, info->setup_patch_ntf);
439 
440 	/* Check if the patching was successful */
441 	r = info->setup_patch_status;
442 	if (r) {
443 		nfc_err(dev, "RAM patch error 0x%x\n", r);
444 		return -EINVAL;
445 	}
446 
447 	/*
448 	 * We need to wait for the reset notification before we
449 	 * can continue
450 	 */
451 	wait_event_interruptible(info->setup_wq, info->setup_reset_ntf);
452 
453 	return r;
454 }
455 
fdp_nci_setup(struct nci_dev * ndev)456 static int fdp_nci_setup(struct nci_dev *ndev)
457 {
458 	/* Format: total length followed by an NCI packet */
459 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
460 	struct device *dev = &info->phy->i2c_dev->dev;
461 	int r;
462 	u8 patched = 0;
463 
464 	r = nci_core_init(ndev);
465 	if (r)
466 		goto error;
467 
468 	/* Get RAM and OTP version */
469 	r = fdp_nci_get_versions(ndev);
470 	if (r)
471 		goto error;
472 
473 	/* Load firmware from disk */
474 	r = fdp_nci_request_firmware(ndev);
475 	if (r)
476 		goto error;
477 
478 	/* Update OTP */
479 	if (info->otp_version < info->otp_patch_version) {
480 		r = fdp_nci_patch_otp(ndev);
481 		if (r)
482 			goto error;
483 		patched = 1;
484 	}
485 
486 	/* Update RAM */
487 	if (info->ram_version < info->ram_patch_version) {
488 		r = fdp_nci_patch_ram(ndev);
489 		if (r)
490 			goto error;
491 		patched = 1;
492 	}
493 
494 	/* Release the firmware buffers */
495 	fdp_nci_release_firmware(ndev);
496 
497 	/* If a patch was applied the new version is checked */
498 	if (patched) {
499 		r = nci_core_init(ndev);
500 		if (r)
501 			goto error;
502 
503 		r = fdp_nci_get_versions(ndev);
504 		if (r)
505 			goto error;
506 
507 		if (info->otp_version != info->otp_patch_version ||
508 		    info->ram_version != info->ram_patch_version) {
509 			nfc_err(dev, "Firmware update failed");
510 			r = -EINVAL;
511 			goto error;
512 		}
513 	}
514 
515 	/*
516 	 * We initialized the devices but the NFC subsystem expects
517 	 * it to not be initialized.
518 	 */
519 	return nci_core_reset(ndev);
520 
521 error:
522 	fdp_nci_release_firmware(ndev);
523 	nfc_err(dev, "Setup error %d\n", r);
524 	return r;
525 }
526 
fdp_nci_post_setup(struct nci_dev * ndev)527 static int fdp_nci_post_setup(struct nci_dev *ndev)
528 {
529 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
530 	struct device *dev = &info->phy->i2c_dev->dev;
531 	int r;
532 
533 	/* Check if the device has VSC */
534 	if (info->fw_vsc_cfg && info->fw_vsc_cfg[0]) {
535 
536 		/* Set the vendor specific configuration */
537 		r = fdp_nci_set_production_data(ndev, info->fw_vsc_cfg[3],
538 						&info->fw_vsc_cfg[4]);
539 		if (r) {
540 			nfc_err(dev, "Vendor specific config set error %d\n",
541 				r);
542 			return r;
543 		}
544 	}
545 
546 	/* Set clock type and frequency */
547 	r = fdp_nci_set_clock(ndev, info->clock_type, info->clock_freq);
548 	if (r) {
549 		nfc_err(dev, "Clock set error %d\n", r);
550 		return r;
551 	}
552 
553 	/*
554 	 * In order to apply the VSC FDP needs a reset
555 	 */
556 	r = nci_core_reset(ndev);
557 	if (r)
558 		return r;
559 
560 	/**
561 	 * The nci core was initialized when post setup was called
562 	 * so we leave it like that
563 	 */
564 	return nci_core_init(ndev);
565 }
566 
fdp_nci_core_reset_ntf_packet(struct nci_dev * ndev,struct sk_buff * skb)567 static int fdp_nci_core_reset_ntf_packet(struct nci_dev *ndev,
568 					  struct sk_buff *skb)
569 {
570 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
571 
572 	info->setup_reset_ntf = 1;
573 	wake_up(&info->setup_wq);
574 
575 	return 0;
576 }
577 
fdp_nci_prop_patch_ntf_packet(struct nci_dev * ndev,struct sk_buff * skb)578 static int fdp_nci_prop_patch_ntf_packet(struct nci_dev *ndev,
579 					  struct sk_buff *skb)
580 {
581 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
582 
583 	info->setup_patch_ntf = 1;
584 	info->setup_patch_status = skb->data[0];
585 	wake_up(&info->setup_wq);
586 
587 	return 0;
588 }
589 
fdp_nci_prop_patch_rsp_packet(struct nci_dev * ndev,struct sk_buff * skb)590 static int fdp_nci_prop_patch_rsp_packet(struct nci_dev *ndev,
591 					  struct sk_buff *skb)
592 {
593 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
594 	struct device *dev = &info->phy->i2c_dev->dev;
595 	u8 status = skb->data[0];
596 
597 	dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
598 	nci_req_complete(ndev, status);
599 
600 	return 0;
601 }
602 
fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev * ndev,struct sk_buff * skb)603 static int fdp_nci_prop_set_production_data_rsp_packet(struct nci_dev *ndev,
604 							struct sk_buff *skb)
605 {
606 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
607 	struct device *dev = &info->phy->i2c_dev->dev;
608 	u8 status = skb->data[0];
609 
610 	dev_dbg(dev, "%s: status 0x%x\n", __func__, status);
611 	nci_req_complete(ndev, status);
612 
613 	return 0;
614 }
615 
fdp_nci_core_get_config_rsp_packet(struct nci_dev * ndev,struct sk_buff * skb)616 static int fdp_nci_core_get_config_rsp_packet(struct nci_dev *ndev,
617 						struct sk_buff *skb)
618 {
619 	struct fdp_nci_info *info = nci_get_drvdata(ndev);
620 	struct device *dev = &info->phy->i2c_dev->dev;
621 	const struct nci_core_get_config_rsp *rsp = (void *) skb->data;
622 	unsigned int i;
623 	const u8 *p;
624 
625 	if (rsp->status == NCI_STATUS_OK) {
626 
627 		p = rsp->data;
628 		for (i = 0; i < 4; i++) {
629 
630 			switch (*p++) {
631 			case NCI_PARAM_ID_FW_RAM_VERSION:
632 				p++;
633 				info->ram_version = le32_to_cpup((__le32 *) p);
634 				p += 4;
635 				break;
636 			case NCI_PARAM_ID_FW_OTP_VERSION:
637 				p++;
638 				info->otp_version = le32_to_cpup((__le32 *) p);
639 				p += 4;
640 				break;
641 			case NCI_PARAM_ID_OTP_LIMITED_VERSION:
642 				p++;
643 				info->otp_version = le32_to_cpup((__le32 *) p);
644 				p += 4;
645 				break;
646 			case NCI_PARAM_ID_KEY_INDEX_ID:
647 				p++;
648 				info->key_index = *p++;
649 			}
650 		}
651 	}
652 
653 	dev_dbg(dev, "OTP version %d\n", info->otp_version);
654 	dev_dbg(dev, "RAM version %d\n", info->ram_version);
655 	dev_dbg(dev, "key index %d\n", info->key_index);
656 	dev_dbg(dev, "%s: status 0x%x\n", __func__, rsp->status);
657 
658 	nci_req_complete(ndev, rsp->status);
659 
660 	return 0;
661 }
662 
663 static const struct nci_driver_ops fdp_core_ops[] = {
664 	{
665 		.opcode = NCI_OP_CORE_GET_CONFIG_RSP,
666 		.rsp = fdp_nci_core_get_config_rsp_packet,
667 	},
668 	{
669 		.opcode = NCI_OP_CORE_RESET_NTF,
670 		.ntf = fdp_nci_core_reset_ntf_packet,
671 	},
672 };
673 
674 static const struct nci_driver_ops fdp_prop_ops[] = {
675 	{
676 		.opcode = nci_opcode_pack(NCI_GID_PROP, NCI_OP_PROP_PATCH_OID),
677 		.rsp = fdp_nci_prop_patch_rsp_packet,
678 		.ntf = fdp_nci_prop_patch_ntf_packet,
679 	},
680 	{
681 		.opcode = nci_opcode_pack(NCI_GID_PROP,
682 					  NCI_OP_PROP_SET_PDATA_OID),
683 		.rsp = fdp_nci_prop_set_production_data_rsp_packet,
684 	},
685 };
686 
687 static const struct nci_ops nci_ops = {
688 	.open = fdp_nci_open,
689 	.close = fdp_nci_close,
690 	.send = fdp_nci_send,
691 	.setup = fdp_nci_setup,
692 	.post_setup = fdp_nci_post_setup,
693 	.prop_ops = fdp_prop_ops,
694 	.n_prop_ops = ARRAY_SIZE(fdp_prop_ops),
695 	.core_ops = fdp_core_ops,
696 	.n_core_ops = ARRAY_SIZE(fdp_core_ops),
697 };
698 
fdp_nci_probe(struct fdp_i2c_phy * phy,const struct nfc_phy_ops * phy_ops,struct nci_dev ** ndevp,int tx_headroom,int tx_tailroom,u8 clock_type,u32 clock_freq,const u8 * fw_vsc_cfg)699 int fdp_nci_probe(struct fdp_i2c_phy *phy, const struct nfc_phy_ops *phy_ops,
700 			struct nci_dev **ndevp, int tx_headroom,
701 			int tx_tailroom, u8 clock_type, u32 clock_freq,
702 			const u8 *fw_vsc_cfg)
703 {
704 	struct device *dev = &phy->i2c_dev->dev;
705 	struct fdp_nci_info *info;
706 	struct nci_dev *ndev;
707 	u32 protocols;
708 	int r;
709 
710 	info = devm_kzalloc(dev, sizeof(struct fdp_nci_info), GFP_KERNEL);
711 	if (!info)
712 		return -ENOMEM;
713 
714 	info->phy = phy;
715 	info->phy_ops = phy_ops;
716 	info->clock_type = clock_type;
717 	info->clock_freq = clock_freq;
718 	info->fw_vsc_cfg = fw_vsc_cfg;
719 
720 	init_waitqueue_head(&info->setup_wq);
721 
722 	protocols = NFC_PROTO_JEWEL_MASK |
723 		    NFC_PROTO_MIFARE_MASK |
724 		    NFC_PROTO_FELICA_MASK |
725 		    NFC_PROTO_ISO14443_MASK |
726 		    NFC_PROTO_ISO14443_B_MASK |
727 		    NFC_PROTO_NFC_DEP_MASK |
728 		    NFC_PROTO_ISO15693_MASK;
729 
730 	BUILD_BUG_ON(ARRAY_SIZE(fdp_prop_ops) > NCI_MAX_PROPRIETARY_CMD);
731 	ndev = nci_allocate_device(&nci_ops, protocols, tx_headroom,
732 				   tx_tailroom);
733 	if (!ndev) {
734 		nfc_err(dev, "Cannot allocate nfc ndev\n");
735 		return -ENOMEM;
736 	}
737 
738 	r = nci_register_device(ndev);
739 	if (r)
740 		goto err_regdev;
741 
742 	*ndevp = ndev;
743 	info->ndev = ndev;
744 
745 	nci_set_drvdata(ndev, info);
746 
747 	return 0;
748 
749 err_regdev:
750 	nci_free_device(ndev);
751 	return r;
752 }
753 EXPORT_SYMBOL(fdp_nci_probe);
754 
fdp_nci_remove(struct nci_dev * ndev)755 void fdp_nci_remove(struct nci_dev *ndev)
756 {
757 	nci_unregister_device(ndev);
758 	nci_free_device(ndev);
759 }
760 EXPORT_SYMBOL(fdp_nci_remove);
761 
762 MODULE_LICENSE("GPL");
763 MODULE_DESCRIPTION("NFC NCI driver for Intel Fields Peak NFC controller");
764 MODULE_AUTHOR("Robert Dolca <robert.dolca@intel.com>");
765 
766 MODULE_FIRMWARE(FDP_OTP_PATCH_NAME);
767 MODULE_FIRMWARE(FDP_RAM_PATCH_NAME);
768