xref: /openbmc/linux/drivers/nfc/st21nfca/i2c.c (revision 50dc9a85)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * I2C Link Layer for ST21NFCA HCI based Driver
4  * Copyright (C) 2014  STMicroelectronics SAS. All rights reserved.
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/crc-ccitt.h>
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_gpio.h>
15 #include <linux/acpi.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/nfc.h>
19 #include <linux/firmware.h>
20 
21 #include <net/nfc/hci.h>
22 #include <net/nfc/llc.h>
23 #include <net/nfc/nfc.h>
24 
25 #include "st21nfca.h"
26 
27 /*
28  * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
29  * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
30  * called byte stuffing has been introduced.
31  *
32  * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
33  * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
34  * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
35  */
36 #define ST21NFCA_SOF_EOF		0x7e
37 #define ST21NFCA_BYTE_STUFFING_MASK	0x20
38 #define ST21NFCA_ESCAPE_BYTE_STUFFING	0x7d
39 
40 /* SOF + 00 */
41 #define ST21NFCA_FRAME_HEADROOM			2
42 
43 /* 2 bytes crc + EOF */
44 #define ST21NFCA_FRAME_TAILROOM 3
45 #define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
46 				buf[1] == 0)
47 
48 #define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci"
49 #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
50 
51 struct st21nfca_i2c_phy {
52 	struct i2c_client *i2c_dev;
53 	struct nfc_hci_dev *hdev;
54 
55 	struct gpio_desc *gpiod_ena;
56 	struct st21nfca_se_status se_status;
57 
58 	struct sk_buff *pending_skb;
59 	int current_read_len;
60 	/*
61 	 * crc might have fail because i2c macro
62 	 * is disable due to other interface activity
63 	 */
64 	int crc_trials;
65 
66 	int powered;
67 	int run_mode;
68 
69 	/*
70 	 * < 0 if hardware error occured (e.g. i2c err)
71 	 * and prevents normal operation.
72 	 */
73 	int hard_fault;
74 	struct mutex phy_lock;
75 };
76 
77 static const u8 len_seq[] = { 16, 24, 12, 29 };
78 static const u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
79 
80 #define I2C_DUMP_SKB(info, skb)					\
81 do {								\
82 	pr_debug("%s:\n", info);				\
83 	print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET,	\
84 		       16, 1, (skb)->data, (skb)->len, 0);	\
85 } while (0)
86 
87 /*
88  * In order to get the CLF in a known state we generate an internal reboot
89  * using a proprietary command.
90  * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
91  * fill buffer.
92  */
93 static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
94 {
95 	u16 wait_reboot[] = { 50, 300, 1000 };
96 	char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
97 	u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
98 	int i, r = -1;
99 
100 	for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
101 		r = i2c_master_send(phy->i2c_dev, reboot_cmd,
102 				    sizeof(reboot_cmd));
103 		if (r < 0)
104 			msleep(wait_reboot[i]);
105 	}
106 	if (r < 0)
107 		return r;
108 
109 	/* CLF is spending about 20ms to do an internal reboot */
110 	msleep(20);
111 	r = -1;
112 	for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
113 		r = i2c_master_recv(phy->i2c_dev, tmp,
114 				    ST21NFCA_HCI_LLC_MAX_SIZE);
115 		if (r < 0)
116 			msleep(wait_reboot[i]);
117 	}
118 	if (r < 0)
119 		return r;
120 
121 	for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
122 		tmp[i] == ST21NFCA_SOF_EOF; i++)
123 		;
124 
125 	if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
126 		return -ENODEV;
127 
128 	usleep_range(1000, 1500);
129 	return 0;
130 }
131 
132 static int st21nfca_hci_i2c_enable(void *phy_id)
133 {
134 	struct st21nfca_i2c_phy *phy = phy_id;
135 
136 	gpiod_set_value(phy->gpiod_ena, 1);
137 	phy->powered = 1;
138 	phy->run_mode = ST21NFCA_HCI_MODE;
139 
140 	usleep_range(10000, 15000);
141 
142 	return 0;
143 }
144 
145 static void st21nfca_hci_i2c_disable(void *phy_id)
146 {
147 	struct st21nfca_i2c_phy *phy = phy_id;
148 
149 	gpiod_set_value(phy->gpiod_ena, 0);
150 
151 	phy->powered = 0;
152 }
153 
154 static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
155 {
156 	u16 crc;
157 	u8 tmp;
158 
159 	*(u8 *)skb_push(skb, 1) = 0;
160 
161 	crc = crc_ccitt(0xffff, skb->data, skb->len);
162 	crc = ~crc;
163 
164 	tmp = crc & 0x00ff;
165 	skb_put_u8(skb, tmp);
166 
167 	tmp = (crc >> 8) & 0x00ff;
168 	skb_put_u8(skb, tmp);
169 }
170 
171 static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
172 {
173 	skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
174 	skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
175 }
176 
177 /*
178  * Writing a frame must not return the number of written bytes.
179  * It must return either zero for success, or <0 for error.
180  * In addition, it must not alter the skb
181  */
182 static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
183 {
184 	int r = -1, i, j;
185 	struct st21nfca_i2c_phy *phy = phy_id;
186 	struct i2c_client *client = phy->i2c_dev;
187 	u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
188 
189 	I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
190 
191 	if (phy->hard_fault != 0)
192 		return phy->hard_fault;
193 
194 	/*
195 	 * Compute CRC before byte stuffing computation on frame
196 	 * Note st21nfca_hci_add_len_crc is doing a byte stuffing
197 	 * on its own value
198 	 */
199 	st21nfca_hci_add_len_crc(skb);
200 
201 	/* add ST21NFCA_SOF_EOF on tail */
202 	skb_put_u8(skb, ST21NFCA_SOF_EOF);
203 	/* add ST21NFCA_SOF_EOF on head */
204 	*(u8 *)skb_push(skb, 1) = ST21NFCA_SOF_EOF;
205 
206 	/*
207 	 * Compute byte stuffing
208 	 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
209 	 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
210 	 * xor byte with ST21NFCA_BYTE_STUFFING_MASK
211 	 */
212 	tmp[0] = skb->data[0];
213 	for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
214 		if (skb->data[i] == ST21NFCA_SOF_EOF
215 		    || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
216 			tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
217 			j++;
218 			tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
219 		} else {
220 			tmp[j] = skb->data[i];
221 		}
222 	}
223 	tmp[j] = skb->data[i];
224 	j++;
225 
226 	/*
227 	 * Manage sleep mode
228 	 * Try 3 times to send data with delay between each
229 	 */
230 	mutex_lock(&phy->phy_lock);
231 	for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
232 		r = i2c_master_send(client, tmp, j);
233 		if (r < 0)
234 			msleep(wait_tab[i]);
235 	}
236 	mutex_unlock(&phy->phy_lock);
237 
238 	if (r >= 0) {
239 		if (r != j)
240 			r = -EREMOTEIO;
241 		else
242 			r = 0;
243 	}
244 
245 	st21nfca_hci_remove_len_crc(skb);
246 
247 	return r;
248 }
249 
250 static int get_frame_size(u8 *buf, int buflen)
251 {
252 	int len = 0;
253 
254 	if (buf[len + 1] == ST21NFCA_SOF_EOF)
255 		return 0;
256 
257 	for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
258 		;
259 
260 	return len;
261 }
262 
263 static int check_crc(u8 *buf, int buflen)
264 {
265 	u16 crc;
266 
267 	crc = crc_ccitt(0xffff, buf, buflen - 2);
268 	crc = ~crc;
269 
270 	if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
271 		pr_err(ST21NFCA_HCI_DRIVER_NAME
272 		       ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
273 		       buf[buflen - 2]);
274 
275 		pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
276 		print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
277 			       16, 2, buf, buflen, false);
278 		return -EPERM;
279 	}
280 	return 0;
281 }
282 
283 /*
284  * Prepare received data for upper layer.
285  * Received data include byte stuffing, crc and sof/eof
286  * which is not usable by hci part.
287  * returns:
288  * frame size without sof/eof, header and byte stuffing
289  * -EBADMSG : frame was incorrect and discarded
290  */
291 static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
292 {
293 	int i, j, r, size;
294 
295 	if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
296 		return -EBADMSG;
297 
298 	size = get_frame_size(skb->data, skb->len);
299 	if (size > 0) {
300 		skb_trim(skb, size);
301 		/* remove ST21NFCA byte stuffing for upper layer */
302 		for (i = 1, j = 0; i < skb->len; i++) {
303 			if (skb->data[i + j] ==
304 					(u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
305 				skb->data[i] = skb->data[i + j + 1]
306 						| ST21NFCA_BYTE_STUFFING_MASK;
307 				i++;
308 				j++;
309 			}
310 			skb->data[i] = skb->data[i + j];
311 		}
312 		/* remove byte stuffing useless byte */
313 		skb_trim(skb, i - j);
314 		/* remove ST21NFCA_SOF_EOF from head */
315 		skb_pull(skb, 1);
316 
317 		r = check_crc(skb->data, skb->len);
318 		if (r != 0) {
319 			i = 0;
320 			return -EBADMSG;
321 		}
322 
323 		/* remove headbyte */
324 		skb_pull(skb, 1);
325 		/* remove crc. Byte Stuffing is already removed here */
326 		skb_trim(skb, skb->len - 2);
327 		return skb->len;
328 	}
329 	return 0;
330 }
331 
332 /*
333  * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
334  * that i2c bus will be flushed and that next read will start on a new frame.
335  * returned skb contains only LLC header and payload.
336  * returns:
337  * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
338  * end of read)
339  * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
340  * at end of read)
341  * -EREMOTEIO : i2c read error (fatal)
342  * -EBADMSG : frame was incorrect and discarded
343  * (value returned from st21nfca_hci_i2c_repack)
344  * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
345  * the read length end sequence
346  */
347 static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
348 				 struct sk_buff *skb)
349 {
350 	int r, i;
351 	u8 len;
352 	u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
353 	struct i2c_client *client = phy->i2c_dev;
354 
355 	if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
356 		len = len_seq[phy->current_read_len];
357 
358 		/*
359 		 * Add retry mecanism
360 		 * Operation on I2C interface may fail in case of operation on
361 		 * RF or SWP interface
362 		 */
363 		r = 0;
364 		mutex_lock(&phy->phy_lock);
365 		for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
366 			r = i2c_master_recv(client, buf, len);
367 			if (r < 0)
368 				msleep(wait_tab[i]);
369 		}
370 		mutex_unlock(&phy->phy_lock);
371 
372 		if (r != len) {
373 			phy->current_read_len = 0;
374 			return -EREMOTEIO;
375 		}
376 
377 		/*
378 		 * The first read sequence does not start with SOF.
379 		 * Data is corrupeted so we drop it.
380 		 */
381 		if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) {
382 			skb_trim(skb, 0);
383 			phy->current_read_len = 0;
384 			return -EIO;
385 		} else if (phy->current_read_len && IS_START_OF_FRAME(buf)) {
386 			/*
387 			 * Previous frame transmission was interrupted and
388 			 * the frame got repeated.
389 			 * Received frame start with ST21NFCA_SOF_EOF + 00.
390 			 */
391 			skb_trim(skb, 0);
392 			phy->current_read_len = 0;
393 		}
394 
395 		skb_put_data(skb, buf, len);
396 
397 		if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
398 			phy->current_read_len = 0;
399 			return st21nfca_hci_i2c_repack(skb);
400 		}
401 		phy->current_read_len++;
402 		return -EAGAIN;
403 	}
404 	return -EIO;
405 }
406 
407 /*
408  * Reads an shdlc frame from the chip. This is not as straightforward as it
409  * seems. The frame format is data-crc, and corruption can occur anywhere
410  * while transiting on i2c bus, such that we could read an invalid data.
411  * The tricky case is when we read a corrupted data or crc. We must detect
412  * this here in order to determine that data can be transmitted to the hci
413  * core. This is the reason why we check the crc here.
414  * The CLF will repeat a frame until we send a RR on that frame.
415  *
416  * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
417  * available in the incoming data, other IRQ might come. Every IRQ will trigger
418  * a read sequence with different length and will fill the current frame.
419  * The reception is complete once we reach a ST21NFCA_SOF_EOF.
420  */
421 static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
422 {
423 	struct st21nfca_i2c_phy *phy = phy_id;
424 
425 	int r;
426 
427 	if (!phy || irq != phy->i2c_dev->irq) {
428 		WARN_ON_ONCE(1);
429 		return IRQ_NONE;
430 	}
431 
432 	if (phy->hard_fault != 0)
433 		return IRQ_HANDLED;
434 
435 	r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
436 	if (r == -EREMOTEIO) {
437 		phy->hard_fault = r;
438 
439 		nfc_hci_recv_frame(phy->hdev, NULL);
440 
441 		return IRQ_HANDLED;
442 	} else if (r == -EAGAIN || r == -EIO) {
443 		return IRQ_HANDLED;
444 	} else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
445 		/*
446 		 * With ST21NFCA, only one interface (I2C, RF or SWP)
447 		 * may be active at a time.
448 		 * Having incorrect crc is usually due to i2c macrocell
449 		 * deactivation in the middle of a transmission.
450 		 * It may generate corrupted data on i2c.
451 		 * We give sometime to get i2c back.
452 		 * The complete frame will be repeated.
453 		 */
454 		msleep(wait_tab[phy->crc_trials]);
455 		phy->crc_trials++;
456 		phy->current_read_len = 0;
457 		kfree_skb(phy->pending_skb);
458 	} else if (r > 0) {
459 		/*
460 		 * We succeeded to read data from the CLF and
461 		 * data is valid.
462 		 * Reset counter.
463 		 */
464 		nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
465 		phy->crc_trials = 0;
466 	} else {
467 		kfree_skb(phy->pending_skb);
468 	}
469 
470 	phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
471 	if (phy->pending_skb == NULL) {
472 		phy->hard_fault = -ENOMEM;
473 		nfc_hci_recv_frame(phy->hdev, NULL);
474 	}
475 
476 	return IRQ_HANDLED;
477 }
478 
479 static const struct nfc_phy_ops i2c_phy_ops = {
480 	.write = st21nfca_hci_i2c_write,
481 	.enable = st21nfca_hci_i2c_enable,
482 	.disable = st21nfca_hci_i2c_disable,
483 };
484 
485 static const struct acpi_gpio_params enable_gpios = { 1, 0, false };
486 
487 static const struct acpi_gpio_mapping acpi_st21nfca_gpios[] = {
488 	{ "enable-gpios", &enable_gpios, 1 },
489 	{},
490 };
491 
492 static int st21nfca_hci_i2c_probe(struct i2c_client *client,
493 				  const struct i2c_device_id *id)
494 {
495 	struct device *dev = &client->dev;
496 	struct st21nfca_i2c_phy *phy;
497 	int r;
498 
499 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
500 		nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
501 		return -ENODEV;
502 	}
503 
504 	phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
505 			   GFP_KERNEL);
506 	if (!phy)
507 		return -ENOMEM;
508 
509 	phy->i2c_dev = client;
510 	phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
511 	if (phy->pending_skb == NULL)
512 		return -ENOMEM;
513 
514 	phy->current_read_len = 0;
515 	phy->crc_trials = 0;
516 	mutex_init(&phy->phy_lock);
517 	i2c_set_clientdata(client, phy);
518 
519 	r = devm_acpi_dev_add_driver_gpios(dev, acpi_st21nfca_gpios);
520 	if (r)
521 		dev_dbg(dev, "Unable to add GPIO mapping table\n");
522 
523 	/* Get EN GPIO from resource provider */
524 	phy->gpiod_ena = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
525 	if (IS_ERR(phy->gpiod_ena)) {
526 		nfc_err(dev, "Unable to get ENABLE GPIO\n");
527 		return PTR_ERR(phy->gpiod_ena);
528 	}
529 
530 	phy->se_status.is_ese_present =
531 			device_property_read_bool(&client->dev, "ese-present");
532 	phy->se_status.is_uicc_present =
533 			device_property_read_bool(&client->dev, "uicc-present");
534 
535 	r = st21nfca_hci_platform_init(phy);
536 	if (r < 0) {
537 		nfc_err(&client->dev, "Unable to reboot st21nfca\n");
538 		return r;
539 	}
540 
541 	r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
542 				st21nfca_hci_irq_thread_fn,
543 				IRQF_ONESHOT,
544 				ST21NFCA_HCI_DRIVER_NAME, phy);
545 	if (r < 0) {
546 		nfc_err(&client->dev, "Unable to register IRQ handler\n");
547 		return r;
548 	}
549 
550 	return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
551 					ST21NFCA_FRAME_HEADROOM,
552 					ST21NFCA_FRAME_TAILROOM,
553 					ST21NFCA_HCI_LLC_MAX_PAYLOAD,
554 					&phy->hdev,
555 					&phy->se_status);
556 }
557 
558 static int st21nfca_hci_i2c_remove(struct i2c_client *client)
559 {
560 	struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
561 
562 	st21nfca_hci_remove(phy->hdev);
563 
564 	if (phy->powered)
565 		st21nfca_hci_i2c_disable(phy);
566 
567 	return 0;
568 }
569 
570 static const struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
571 	{ST21NFCA_HCI_DRIVER_NAME, 0},
572 	{}
573 };
574 MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
575 
576 static const struct acpi_device_id st21nfca_hci_i2c_acpi_match[] __maybe_unused = {
577 	{"SMO2100", 0},
578 	{}
579 };
580 MODULE_DEVICE_TABLE(acpi, st21nfca_hci_i2c_acpi_match);
581 
582 static const struct of_device_id of_st21nfca_i2c_match[] __maybe_unused = {
583 	{ .compatible = "st,st21nfca-i2c", },
584 	{ .compatible = "st,st21nfca_i2c", },
585 	{}
586 };
587 MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match);
588 
589 static struct i2c_driver st21nfca_hci_i2c_driver = {
590 	.driver = {
591 		.name = ST21NFCA_HCI_I2C_DRIVER_NAME,
592 		.of_match_table = of_match_ptr(of_st21nfca_i2c_match),
593 		.acpi_match_table = ACPI_PTR(st21nfca_hci_i2c_acpi_match),
594 	},
595 	.probe = st21nfca_hci_i2c_probe,
596 	.id_table = st21nfca_hci_i2c_id_table,
597 	.remove = st21nfca_hci_i2c_remove,
598 };
599 module_i2c_driver(st21nfca_hci_i2c_driver);
600 
601 MODULE_LICENSE("GPL");
602 MODULE_DESCRIPTION(DRIVER_DESC);
603