xref: /openbmc/linux/drivers/nfc/nfcmrvl/fw_dnld.c (revision aa0dc6a7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Marvell NFC driver: Firmware downloader
4  *
5  * Copyright (C) 2015, Marvell International Ltd.
6  */
7 
8 #include <linux/module.h>
9 #include <asm/unaligned.h>
10 #include <linux/firmware.h>
11 #include <linux/nfc.h>
12 #include <net/nfc/nci.h>
13 #include <net/nfc/nci_core.h>
14 #include "nfcmrvl.h"
15 
16 #define FW_DNLD_TIMEOUT			15000
17 
18 #define NCI_OP_PROPRIETARY_BOOT_CMD	nci_opcode_pack(NCI_GID_PROPRIETARY, \
19 							NCI_OP_PROP_BOOT_CMD)
20 
21 /* FW download states */
22 
23 enum {
24 	STATE_RESET = 0,
25 	STATE_INIT,
26 	STATE_SET_REF_CLOCK,
27 	STATE_SET_HI_CONFIG,
28 	STATE_OPEN_LC,
29 	STATE_FW_DNLD,
30 	STATE_CLOSE_LC,
31 	STATE_BOOT
32 };
33 
34 enum {
35 	SUBSTATE_WAIT_COMMAND = 0,
36 	SUBSTATE_WAIT_ACK_CREDIT,
37 	SUBSTATE_WAIT_NACK_CREDIT,
38 	SUBSTATE_WAIT_DATA_CREDIT,
39 };
40 
41 /*
42  * Patterns for responses
43  */
44 
45 static const uint8_t nci_pattern_core_reset_ntf[] = {
46 	0x60, 0x00, 0x02, 0xA0, 0x01
47 };
48 
49 static const uint8_t nci_pattern_core_init_rsp[] = {
50 	0x40, 0x01, 0x11
51 };
52 
53 static const uint8_t nci_pattern_core_set_config_rsp[] = {
54 	0x40, 0x02, 0x02, 0x00, 0x00
55 };
56 
57 static const uint8_t nci_pattern_core_conn_create_rsp[] = {
58 	0x40, 0x04, 0x04, 0x00
59 };
60 
61 static const uint8_t nci_pattern_core_conn_close_rsp[] = {
62 	0x40, 0x05, 0x01, 0x00
63 };
64 
65 static const uint8_t nci_pattern_core_conn_credits_ntf[] = {
66 	0x60, 0x06, 0x03, 0x01, NCI_CORE_LC_CONNID_PROP_FW_DL, 0x01
67 };
68 
69 static const uint8_t nci_pattern_proprietary_boot_rsp[] = {
70 	0x4F, 0x3A, 0x01, 0x00
71 };
72 
73 static struct sk_buff *alloc_lc_skb(struct nfcmrvl_private *priv, uint8_t plen)
74 {
75 	struct sk_buff *skb;
76 	struct nci_data_hdr *hdr;
77 
78 	skb = nci_skb_alloc(priv->ndev, (NCI_DATA_HDR_SIZE + plen), GFP_KERNEL);
79 	if (!skb) {
80 		pr_err("no memory for data\n");
81 		return NULL;
82 	}
83 
84 	hdr = skb_put(skb, NCI_DATA_HDR_SIZE);
85 	hdr->conn_id = NCI_CORE_LC_CONNID_PROP_FW_DL;
86 	hdr->rfu = 0;
87 	hdr->plen = plen;
88 
89 	nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
90 	nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
91 
92 	return skb;
93 }
94 
95 static void fw_dnld_over(struct nfcmrvl_private *priv, u32 error)
96 {
97 	if (priv->fw_dnld.fw) {
98 		release_firmware(priv->fw_dnld.fw);
99 		priv->fw_dnld.fw = NULL;
100 		priv->fw_dnld.header = NULL;
101 		priv->fw_dnld.binary_config = NULL;
102 	}
103 
104 	atomic_set(&priv->ndev->cmd_cnt, 0);
105 
106 	if (timer_pending(&priv->ndev->cmd_timer))
107 		del_timer_sync(&priv->ndev->cmd_timer);
108 
109 	if (timer_pending(&priv->fw_dnld.timer))
110 		del_timer_sync(&priv->fw_dnld.timer);
111 
112 	nfc_info(priv->dev, "FW loading over (%d)]\n", error);
113 
114 	if (error != 0) {
115 		/* failed, halt the chip to avoid power consumption */
116 		nfcmrvl_chip_halt(priv);
117 	}
118 
119 	nfc_fw_download_done(priv->ndev->nfc_dev, priv->fw_dnld.name, error);
120 }
121 
122 static void fw_dnld_timeout(struct timer_list *t)
123 {
124 	struct nfcmrvl_private *priv = from_timer(priv, t, fw_dnld.timer);
125 
126 	nfc_err(priv->dev, "FW loading timeout");
127 	priv->fw_dnld.state = STATE_RESET;
128 	fw_dnld_over(priv, -ETIMEDOUT);
129 }
130 
131 static int process_state_reset(struct nfcmrvl_private *priv,
132 			       struct sk_buff *skb)
133 {
134 	if (sizeof(nci_pattern_core_reset_ntf) != skb->len ||
135 	    memcmp(skb->data, nci_pattern_core_reset_ntf,
136 		   sizeof(nci_pattern_core_reset_ntf)))
137 		return -EINVAL;
138 
139 	nfc_info(priv->dev, "BootROM reset, start fw download\n");
140 
141 	/* Start FW download state machine */
142 	priv->fw_dnld.state = STATE_INIT;
143 	nci_send_cmd(priv->ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
144 
145 	return 0;
146 }
147 
148 static int process_state_init(struct nfcmrvl_private *priv, struct sk_buff *skb)
149 {
150 	struct nci_core_set_config_cmd cmd;
151 
152 	if (sizeof(nci_pattern_core_init_rsp) >= skb->len ||
153 	    memcmp(skb->data, nci_pattern_core_init_rsp,
154 		   sizeof(nci_pattern_core_init_rsp)))
155 		return -EINVAL;
156 
157 	cmd.num_params = 1;
158 	cmd.param.id = NFCMRVL_PROP_REF_CLOCK;
159 	cmd.param.len = 4;
160 	memcpy(cmd.param.val, &priv->fw_dnld.header->ref_clock, 4);
161 
162 	nci_send_cmd(priv->ndev, NCI_OP_CORE_SET_CONFIG_CMD, 3 + cmd.param.len,
163 		     &cmd);
164 
165 	priv->fw_dnld.state = STATE_SET_REF_CLOCK;
166 	return 0;
167 }
168 
169 static void create_lc(struct nfcmrvl_private *priv)
170 {
171 	uint8_t param[2] = { NCI_CORE_LC_PROP_FW_DL, 0x0 };
172 
173 	priv->fw_dnld.state = STATE_OPEN_LC;
174 	nci_send_cmd(priv->ndev, NCI_OP_CORE_CONN_CREATE_CMD, 2, param);
175 }
176 
177 static int process_state_set_ref_clock(struct nfcmrvl_private *priv,
178 				       struct sk_buff *skb)
179 {
180 	struct nci_core_set_config_cmd cmd;
181 
182 	if (sizeof(nci_pattern_core_set_config_rsp) != skb->len ||
183 	    memcmp(skb->data, nci_pattern_core_set_config_rsp, skb->len))
184 		return -EINVAL;
185 
186 	cmd.num_params = 1;
187 	cmd.param.id = NFCMRVL_PROP_SET_HI_CONFIG;
188 
189 	switch (priv->phy) {
190 	case NFCMRVL_PHY_UART:
191 		cmd.param.len = 5;
192 		memcpy(cmd.param.val,
193 		       &priv->fw_dnld.binary_config->uart.baudrate,
194 		       4);
195 		cmd.param.val[4] =
196 			priv->fw_dnld.binary_config->uart.flow_control;
197 		break;
198 	case NFCMRVL_PHY_I2C:
199 		cmd.param.len = 5;
200 		memcpy(cmd.param.val,
201 		       &priv->fw_dnld.binary_config->i2c.clk,
202 		       4);
203 		cmd.param.val[4] = 0;
204 		break;
205 	case NFCMRVL_PHY_SPI:
206 		cmd.param.len = 5;
207 		memcpy(cmd.param.val,
208 		       &priv->fw_dnld.binary_config->spi.clk,
209 		       4);
210 		cmd.param.val[4] = 0;
211 		break;
212 	default:
213 		create_lc(priv);
214 		return 0;
215 	}
216 
217 	priv->fw_dnld.state = STATE_SET_HI_CONFIG;
218 	nci_send_cmd(priv->ndev, NCI_OP_CORE_SET_CONFIG_CMD, 3 + cmd.param.len,
219 		     &cmd);
220 	return 0;
221 }
222 
223 static int process_state_set_hi_config(struct nfcmrvl_private *priv,
224 				       struct sk_buff *skb)
225 {
226 	if (sizeof(nci_pattern_core_set_config_rsp) != skb->len ||
227 	    memcmp(skb->data, nci_pattern_core_set_config_rsp, skb->len))
228 		return -EINVAL;
229 
230 	create_lc(priv);
231 	return 0;
232 }
233 
234 static int process_state_open_lc(struct nfcmrvl_private *priv,
235 				 struct sk_buff *skb)
236 {
237 	if (sizeof(nci_pattern_core_conn_create_rsp) >= skb->len ||
238 	    memcmp(skb->data, nci_pattern_core_conn_create_rsp,
239 		   sizeof(nci_pattern_core_conn_create_rsp)))
240 		return -EINVAL;
241 
242 	priv->fw_dnld.state = STATE_FW_DNLD;
243 	priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND;
244 	priv->fw_dnld.offset = priv->fw_dnld.binary_config->offset;
245 	return 0;
246 }
247 
248 static int process_state_fw_dnld(struct nfcmrvl_private *priv,
249 				 struct sk_buff *skb)
250 {
251 	uint16_t len;
252 	uint16_t comp_len;
253 	struct sk_buff *out_skb;
254 
255 	switch (priv->fw_dnld.substate) {
256 	case SUBSTATE_WAIT_COMMAND:
257 		/*
258 		 * Command format:
259 		 * B0..2: NCI header
260 		 * B3   : Helper command (0xA5)
261 		 * B4..5: le16 data size
262 		 * B6..7: le16 data size complement (~)
263 		 * B8..N: payload
264 		 */
265 
266 		/* Remove NCI HDR */
267 		skb_pull(skb, 3);
268 		if (skb->data[0] != HELPER_CMD_PACKET_FORMAT || skb->len != 5) {
269 			nfc_err(priv->dev, "bad command");
270 			return -EINVAL;
271 		}
272 		skb_pull(skb, 1);
273 		len = get_unaligned_le16(skb->data);
274 		skb_pull(skb, 2);
275 		comp_len = get_unaligned_le16(skb->data);
276 		memcpy(&comp_len, skb->data, 2);
277 		skb_pull(skb, 2);
278 		if (((~len) & 0xFFFF) != comp_len) {
279 			nfc_err(priv->dev, "bad len complement: %x %x %x",
280 				len, comp_len, (~len & 0xFFFF));
281 			out_skb = alloc_lc_skb(priv, 1);
282 			if (!out_skb)
283 				return -ENOMEM;
284 			skb_put_u8(out_skb, 0xBF);
285 			nci_send_frame(priv->ndev, out_skb);
286 			priv->fw_dnld.substate = SUBSTATE_WAIT_NACK_CREDIT;
287 			return 0;
288 		}
289 		priv->fw_dnld.chunk_len = len;
290 		out_skb = alloc_lc_skb(priv, 1);
291 		if (!out_skb)
292 			return -ENOMEM;
293 		skb_put_u8(out_skb, HELPER_ACK_PACKET_FORMAT);
294 		nci_send_frame(priv->ndev, out_skb);
295 		priv->fw_dnld.substate = SUBSTATE_WAIT_ACK_CREDIT;
296 		break;
297 
298 	case SUBSTATE_WAIT_ACK_CREDIT:
299 		if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len ||
300 		    memcmp(nci_pattern_core_conn_credits_ntf, skb->data,
301 			   skb->len)) {
302 			nfc_err(priv->dev, "bad packet: waiting for credit");
303 			return -EINVAL;
304 		}
305 		if (priv->fw_dnld.chunk_len == 0) {
306 			/* FW Loading is done */
307 			uint8_t conn_id = NCI_CORE_LC_CONNID_PROP_FW_DL;
308 
309 			priv->fw_dnld.state = STATE_CLOSE_LC;
310 			nci_send_cmd(priv->ndev, NCI_OP_CORE_CONN_CLOSE_CMD,
311 				     1, &conn_id);
312 		} else {
313 			out_skb = alloc_lc_skb(priv, priv->fw_dnld.chunk_len);
314 			if (!out_skb)
315 				return -ENOMEM;
316 			skb_put_data(out_skb,
317 				     ((uint8_t *)priv->fw_dnld.fw->data) + priv->fw_dnld.offset,
318 				     priv->fw_dnld.chunk_len);
319 			nci_send_frame(priv->ndev, out_skb);
320 			priv->fw_dnld.substate = SUBSTATE_WAIT_DATA_CREDIT;
321 		}
322 		break;
323 
324 	case SUBSTATE_WAIT_DATA_CREDIT:
325 		if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len ||
326 		    memcmp(nci_pattern_core_conn_credits_ntf, skb->data,
327 			    skb->len)) {
328 			nfc_err(priv->dev, "bad packet: waiting for credit");
329 			return -EINVAL;
330 		}
331 		priv->fw_dnld.offset += priv->fw_dnld.chunk_len;
332 		priv->fw_dnld.chunk_len = 0;
333 		priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND;
334 		break;
335 
336 	case SUBSTATE_WAIT_NACK_CREDIT:
337 		if (sizeof(nci_pattern_core_conn_credits_ntf) != skb->len ||
338 		    memcmp(nci_pattern_core_conn_credits_ntf, skb->data,
339 			    skb->len)) {
340 			nfc_err(priv->dev, "bad packet: waiting for credit");
341 			return -EINVAL;
342 		}
343 		priv->fw_dnld.substate = SUBSTATE_WAIT_COMMAND;
344 		break;
345 	}
346 	return 0;
347 }
348 
349 static int process_state_close_lc(struct nfcmrvl_private *priv,
350 				  struct sk_buff *skb)
351 {
352 	if (sizeof(nci_pattern_core_conn_close_rsp) != skb->len ||
353 	    memcmp(skb->data, nci_pattern_core_conn_close_rsp, skb->len))
354 		return -EINVAL;
355 
356 	priv->fw_dnld.state = STATE_BOOT;
357 	nci_send_cmd(priv->ndev, NCI_OP_PROPRIETARY_BOOT_CMD, 0, NULL);
358 	return 0;
359 }
360 
361 static int process_state_boot(struct nfcmrvl_private *priv, struct sk_buff *skb)
362 {
363 	if (sizeof(nci_pattern_proprietary_boot_rsp) != skb->len ||
364 	    memcmp(skb->data, nci_pattern_proprietary_boot_rsp, skb->len))
365 		return -EINVAL;
366 
367 	/*
368 	 * Update HI config to use the right configuration for the next
369 	 * data exchanges.
370 	 */
371 	priv->if_ops->nci_update_config(priv,
372 					&priv->fw_dnld.binary_config->config);
373 
374 	if (priv->fw_dnld.binary_config == &priv->fw_dnld.header->helper) {
375 		/*
376 		 * This is the case where an helper was needed and we have
377 		 * uploaded it. Now we have to wait the next RESET NTF to start
378 		 * FW download.
379 		 */
380 		priv->fw_dnld.state = STATE_RESET;
381 		priv->fw_dnld.binary_config = &priv->fw_dnld.header->firmware;
382 		nfc_info(priv->dev, "FW loading: helper loaded");
383 	} else {
384 		nfc_info(priv->dev, "FW loading: firmware loaded");
385 		fw_dnld_over(priv, 0);
386 	}
387 	return 0;
388 }
389 
390 static void fw_dnld_rx_work(struct work_struct *work)
391 {
392 	int ret;
393 	struct sk_buff *skb;
394 	struct nfcmrvl_fw_dnld *fw_dnld = container_of(work,
395 						       struct nfcmrvl_fw_dnld,
396 						       rx_work);
397 	struct nfcmrvl_private *priv = container_of(fw_dnld,
398 						    struct nfcmrvl_private,
399 						    fw_dnld);
400 
401 	while ((skb = skb_dequeue(&fw_dnld->rx_q))) {
402 		nfc_send_to_raw_sock(priv->ndev->nfc_dev, skb,
403 				     RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
404 		switch (fw_dnld->state) {
405 		case STATE_RESET:
406 			ret = process_state_reset(priv, skb);
407 			break;
408 		case STATE_INIT:
409 			ret = process_state_init(priv, skb);
410 			break;
411 		case STATE_SET_REF_CLOCK:
412 			ret = process_state_set_ref_clock(priv, skb);
413 			break;
414 		case STATE_SET_HI_CONFIG:
415 			ret = process_state_set_hi_config(priv, skb);
416 			break;
417 		case STATE_OPEN_LC:
418 			ret = process_state_open_lc(priv, skb);
419 			break;
420 		case STATE_FW_DNLD:
421 			ret = process_state_fw_dnld(priv, skb);
422 			break;
423 		case STATE_CLOSE_LC:
424 			ret = process_state_close_lc(priv, skb);
425 			break;
426 		case STATE_BOOT:
427 			ret = process_state_boot(priv, skb);
428 			break;
429 		default:
430 			ret = -EFAULT;
431 		}
432 
433 		kfree_skb(skb);
434 
435 		if (ret != 0) {
436 			nfc_err(priv->dev, "FW loading error");
437 			fw_dnld_over(priv, ret);
438 			break;
439 		}
440 	}
441 }
442 
443 int nfcmrvl_fw_dnld_init(struct nfcmrvl_private *priv)
444 {
445 	char name[32];
446 
447 	INIT_WORK(&priv->fw_dnld.rx_work, fw_dnld_rx_work);
448 	snprintf(name, sizeof(name), "%s_nfcmrvl_fw_dnld_rx_wq",
449 		 dev_name(&priv->ndev->nfc_dev->dev));
450 	priv->fw_dnld.rx_wq = create_singlethread_workqueue(name);
451 	if (!priv->fw_dnld.rx_wq)
452 		return -ENOMEM;
453 	skb_queue_head_init(&priv->fw_dnld.rx_q);
454 	return 0;
455 }
456 
457 void nfcmrvl_fw_dnld_deinit(struct nfcmrvl_private *priv)
458 {
459 	destroy_workqueue(priv->fw_dnld.rx_wq);
460 }
461 
462 void nfcmrvl_fw_dnld_recv_frame(struct nfcmrvl_private *priv,
463 				struct sk_buff *skb)
464 {
465 	/* Discard command timer */
466 	if (timer_pending(&priv->ndev->cmd_timer))
467 		del_timer_sync(&priv->ndev->cmd_timer);
468 
469 	/* Allow next command */
470 	atomic_set(&priv->ndev->cmd_cnt, 1);
471 
472 	/* Queue and trigger rx work */
473 	skb_queue_tail(&priv->fw_dnld.rx_q, skb);
474 	queue_work(priv->fw_dnld.rx_wq, &priv->fw_dnld.rx_work);
475 }
476 
477 void nfcmrvl_fw_dnld_abort(struct nfcmrvl_private *priv)
478 {
479 	fw_dnld_over(priv, -EHOSTDOWN);
480 }
481 
482 int nfcmrvl_fw_dnld_start(struct nci_dev *ndev, const char *firmware_name)
483 {
484 	struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
485 	struct nfcmrvl_fw_dnld *fw_dnld = &priv->fw_dnld;
486 	int res;
487 
488 	if (!priv->support_fw_dnld)
489 		return -ENOTSUPP;
490 
491 	if (!firmware_name || !firmware_name[0])
492 		return -EINVAL;
493 
494 	strcpy(fw_dnld->name, firmware_name);
495 
496 	/*
497 	 * Retrieve FW binary file and parse it to initialize FW download
498 	 * state machine.
499 	 */
500 
501 	/* Retrieve FW binary */
502 	res = request_firmware(&fw_dnld->fw, firmware_name,
503 			       &ndev->nfc_dev->dev);
504 	if (res < 0) {
505 		nfc_err(priv->dev, "failed to retrieve FW %s", firmware_name);
506 		return -ENOENT;
507 	}
508 
509 	fw_dnld->header = (const struct nfcmrvl_fw *) priv->fw_dnld.fw->data;
510 
511 	if (fw_dnld->header->magic != NFCMRVL_FW_MAGIC ||
512 	    fw_dnld->header->phy != priv->phy) {
513 		nfc_err(priv->dev, "bad firmware binary %s magic=0x%x phy=%d",
514 			firmware_name, fw_dnld->header->magic,
515 			fw_dnld->header->phy);
516 		release_firmware(fw_dnld->fw);
517 		fw_dnld->header = NULL;
518 		return -EINVAL;
519 	}
520 
521 	if (fw_dnld->header->helper.offset != 0) {
522 		nfc_info(priv->dev, "loading helper");
523 		fw_dnld->binary_config = &fw_dnld->header->helper;
524 	} else {
525 		nfc_info(priv->dev, "loading firmware");
526 		fw_dnld->binary_config = &fw_dnld->header->firmware;
527 	}
528 
529 	/* Configure a timer for timeout */
530 	timer_setup(&priv->fw_dnld.timer, fw_dnld_timeout, 0);
531 	mod_timer(&priv->fw_dnld.timer,
532 		  jiffies + msecs_to_jiffies(FW_DNLD_TIMEOUT));
533 
534 	/* Ronfigure HI to be sure that it is the bootrom values */
535 	priv->if_ops->nci_update_config(priv,
536 					&fw_dnld->header->bootrom.config);
537 
538 	/* Allow first command */
539 	atomic_set(&priv->ndev->cmd_cnt, 1);
540 
541 	/* First, reset the chip */
542 	priv->fw_dnld.state = STATE_RESET;
543 	nfcmrvl_chip_reset(priv);
544 
545 	/* Now wait for CORE_RESET_NTF or timeout */
546 
547 	return 0;
548 }
549