2 * I2C Link Layer for ST21NFCA HCI based Driver
3 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/crc-ccitt.h>
21 #include <linux/module.h>
22 #include <linux/i2c.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_gpio.h>
26 #include <linux/acpi.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/nfc.h>
30 #include <linux/firmware.h>
32 #include <asm/unaligned.h>
34 #include <net/nfc/hci.h>
35 #include <net/nfc/llc.h>
36 #include <net/nfc/nfc.h>
41 * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
42 * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
43 * called byte stuffing has been introduced.
45 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
46 * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
47 * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
49 #define ST21NFCA_SOF_EOF 0x7e
50 #define ST21NFCA_BYTE_STUFFING_MASK 0x20
51 #define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
54 #define ST21NFCA_FRAME_HEADROOM 2
56 /* 2 bytes crc + EOF */
57 #define ST21NFCA_FRAME_TAILROOM 3
58 #define IS_START_OF_FRAME(buf) (buf[0] == ST21NFCA_SOF_EOF && \
61 #define ST21NFCA_HCI_DRIVER_NAME "st21nfca_hci"
62 #define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
64 #define ST21NFCA_GPIO_NAME_EN "enable"
66 struct st21nfca_i2c_phy {
67 struct i2c_client *i2c_dev;
68 struct nfc_hci_dev *hdev;
70 struct gpio_desc *gpiod_ena;
71 struct st21nfca_se_status se_status;
73 struct sk_buff *pending_skb;
76 * crc might have fail because i2c macro
77 * is disable due to other interface activity
85 * < 0 if hardware error occured (e.g. i2c err)
86 * and prevents normal operation.
89 struct mutex phy_lock;
92 static u8 len_seq[] = { 16, 24, 12, 29 };
93 static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
95 #define I2C_DUMP_SKB(info, skb) \
97 pr_debug("%s:\n", info); \
98 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
99 16, 1, (skb)->data, (skb)->len, 0); \
103 * In order to get the CLF in a known state we generate an internal reboot
104 * using a proprietary command.
105 * Once the reboot is completed, we expect to receive a ST21NFCA_SOF_EOF
108 static int st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
110 u16 wait_reboot[] = { 50, 300, 1000 };
111 char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
112 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
115 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
116 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
119 msleep(wait_reboot[i]);
124 /* CLF is spending about 20ms to do an internal reboot */
127 for (i = 0; i < ARRAY_SIZE(wait_reboot) && r < 0; i++) {
128 r = i2c_master_recv(phy->i2c_dev, tmp,
129 ST21NFCA_HCI_LLC_MAX_SIZE);
131 msleep(wait_reboot[i]);
136 for (i = 0; i < ST21NFCA_HCI_LLC_MAX_SIZE &&
137 tmp[i] == ST21NFCA_SOF_EOF; i++)
140 if (r != ST21NFCA_HCI_LLC_MAX_SIZE)
143 usleep_range(1000, 1500);
147 static int st21nfca_hci_i2c_enable(void *phy_id)
149 struct st21nfca_i2c_phy *phy = phy_id;
151 gpiod_set_value(phy->gpiod_ena, 1);
153 phy->run_mode = ST21NFCA_HCI_MODE;
155 usleep_range(10000, 15000);
160 static void st21nfca_hci_i2c_disable(void *phy_id)
162 struct st21nfca_i2c_phy *phy = phy_id;
164 gpiod_set_value(phy->gpiod_ena, 0);
169 static void st21nfca_hci_add_len_crc(struct sk_buff *skb)
174 *skb_push(skb, 1) = 0;
176 crc = crc_ccitt(0xffff, skb->data, skb->len);
180 *skb_put(skb, 1) = tmp;
182 tmp = (crc >> 8) & 0x00ff;
183 *skb_put(skb, 1) = tmp;
186 static void st21nfca_hci_remove_len_crc(struct sk_buff *skb)
188 skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
189 skb_trim(skb, skb->len - ST21NFCA_FRAME_TAILROOM);
193 * Writing a frame must not return the number of written bytes.
194 * It must return either zero for success, or <0 for error.
195 * In addition, it must not alter the skb
197 static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
200 struct st21nfca_i2c_phy *phy = phy_id;
201 struct i2c_client *client = phy->i2c_dev;
202 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
204 I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
206 if (phy->hard_fault != 0)
207 return phy->hard_fault;
210 * Compute CRC before byte stuffing computation on frame
211 * Note st21nfca_hci_add_len_crc is doing a byte stuffing
214 st21nfca_hci_add_len_crc(skb);
216 /* add ST21NFCA_SOF_EOF on tail */
217 *skb_put(skb, 1) = ST21NFCA_SOF_EOF;
218 /* add ST21NFCA_SOF_EOF on head */
219 *skb_push(skb, 1) = ST21NFCA_SOF_EOF;
222 * Compute byte stuffing
223 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
224 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
225 * xor byte with ST21NFCA_BYTE_STUFFING_MASK
227 tmp[0] = skb->data[0];
228 for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
229 if (skb->data[i] == ST21NFCA_SOF_EOF
230 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
231 tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
233 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
235 tmp[j] = skb->data[i];
238 tmp[j] = skb->data[i];
243 * Try 3 times to send data with delay between each
245 mutex_lock(&phy->phy_lock);
246 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
247 r = i2c_master_send(client, tmp, j);
251 mutex_unlock(&phy->phy_lock);
260 st21nfca_hci_remove_len_crc(skb);
265 static int get_frame_size(u8 *buf, int buflen)
269 if (buf[len + 1] == ST21NFCA_SOF_EOF)
272 for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
278 static int check_crc(u8 *buf, int buflen)
282 crc = crc_ccitt(0xffff, buf, buflen - 2);
285 if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
286 pr_err(ST21NFCA_HCI_DRIVER_NAME
287 ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
290 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
291 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
292 16, 2, buf, buflen, false);
299 * Prepare received data for upper layer.
300 * Received data include byte stuffing, crc and sof/eof
301 * which is not usable by hci part.
303 * frame size without sof/eof, header and byte stuffing
304 * -EBADMSG : frame was incorrect and discarded
306 static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
310 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
313 size = get_frame_size(skb->data, skb->len);
316 /* remove ST21NFCA byte stuffing for upper layer */
317 for (i = 1, j = 0; i < skb->len; i++) {
318 if (skb->data[i + j] ==
319 (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
320 skb->data[i] = skb->data[i + j + 1]
321 | ST21NFCA_BYTE_STUFFING_MASK;
325 skb->data[i] = skb->data[i + j];
327 /* remove byte stuffing useless byte */
328 skb_trim(skb, i - j);
329 /* remove ST21NFCA_SOF_EOF from head */
332 r = check_crc(skb->data, skb->len);
338 /* remove headbyte */
340 /* remove crc. Byte Stuffing is already removed here */
341 skb_trim(skb, skb->len - 2);
348 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
349 * that i2c bus will be flushed and that next read will start on a new frame.
350 * returned skb contains only LLC header and payload.
352 * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
354 * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
356 * -EREMOTEIO : i2c read error (fatal)
357 * -EBADMSG : frame was incorrect and discarded
358 * (value returned from st21nfca_hci_i2c_repack)
359 * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
360 * the read length end sequence
362 static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
367 u8 buf[ST21NFCA_HCI_LLC_MAX_PAYLOAD];
368 struct i2c_client *client = phy->i2c_dev;
370 if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
371 len = len_seq[phy->current_read_len];
375 * Operation on I2C interface may fail in case of operation on
376 * RF or SWP interface
379 mutex_lock(&phy->phy_lock);
380 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
381 r = i2c_master_recv(client, buf, len);
385 mutex_unlock(&phy->phy_lock);
388 phy->current_read_len = 0;
393 * The first read sequence does not start with SOF.
394 * Data is corrupeted so we drop it.
396 if (!phy->current_read_len && !IS_START_OF_FRAME(buf)) {
398 phy->current_read_len = 0;
400 } else if (phy->current_read_len && IS_START_OF_FRAME(buf)) {
402 * Previous frame transmission was interrupted and
403 * the frame got repeated.
404 * Received frame start with ST21NFCA_SOF_EOF + 00.
407 phy->current_read_len = 0;
410 memcpy(skb_put(skb, len), buf, len);
412 if (skb->data[skb->len - 1] == ST21NFCA_SOF_EOF) {
413 phy->current_read_len = 0;
414 return st21nfca_hci_i2c_repack(skb);
416 phy->current_read_len++;
423 * Reads an shdlc frame from the chip. This is not as straightforward as it
424 * seems. The frame format is data-crc, and corruption can occur anywhere
425 * while transiting on i2c bus, such that we could read an invalid data.
426 * The tricky case is when we read a corrupted data or crc. We must detect
427 * this here in order to determine that data can be transmitted to the hci
428 * core. This is the reason why we check the crc here.
429 * The CLF will repeat a frame until we send a RR on that frame.
431 * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
432 * available in the incoming data, other IRQ might come. Every IRQ will trigger
433 * a read sequence with different length and will fill the current frame.
434 * The reception is complete once we reach a ST21NFCA_SOF_EOF.
436 static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
438 struct st21nfca_i2c_phy *phy = phy_id;
439 struct i2c_client *client;
443 if (!phy || irq != phy->i2c_dev->irq) {
448 client = phy->i2c_dev;
449 dev_dbg(&client->dev, "IRQ\n");
451 if (phy->hard_fault != 0)
454 r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
455 if (r == -EREMOTEIO) {
458 nfc_hci_recv_frame(phy->hdev, NULL);
461 } else if (r == -EAGAIN || r == -EIO) {
463 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
465 * With ST21NFCA, only one interface (I2C, RF or SWP)
466 * may be active at a time.
467 * Having incorrect crc is usually due to i2c macrocell
468 * deactivation in the middle of a transmission.
469 * It may generate corrupted data on i2c.
470 * We give sometime to get i2c back.
471 * The complete frame will be repeated.
473 msleep(wait_tab[phy->crc_trials]);
475 phy->current_read_len = 0;
476 kfree_skb(phy->pending_skb);
479 * We succeeded to read data from the CLF and
483 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
486 kfree_skb(phy->pending_skb);
489 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
490 if (phy->pending_skb == NULL) {
491 phy->hard_fault = -ENOMEM;
492 nfc_hci_recv_frame(phy->hdev, NULL);
498 static struct nfc_phy_ops i2c_phy_ops = {
499 .write = st21nfca_hci_i2c_write,
500 .enable = st21nfca_hci_i2c_enable,
501 .disable = st21nfca_hci_i2c_disable,
504 static int st21nfca_hci_i2c_acpi_request_resources(struct i2c_client *client)
506 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
507 struct device *dev = &client->dev;
509 /* Get EN GPIO from ACPI */
510 phy->gpiod_ena = devm_gpiod_get_index(dev, ST21NFCA_GPIO_NAME_EN, 1,
512 if (IS_ERR(phy->gpiod_ena)) {
513 nfc_err(dev, "Unable to get ENABLE GPIO\n");
514 return PTR_ERR(phy->gpiod_ena);
520 static int st21nfca_hci_i2c_of_request_resources(struct i2c_client *client)
522 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
523 struct device *dev = &client->dev;
525 /* Get GPIO from device tree */
526 phy->gpiod_ena = devm_gpiod_get_index(dev, ST21NFCA_GPIO_NAME_EN, 0,
528 if (IS_ERR(phy->gpiod_ena)) {
529 nfc_err(dev, "Failed to request enable pin\n");
530 return PTR_ERR(phy->gpiod_ena);
536 static int st21nfca_hci_i2c_probe(struct i2c_client *client,
537 const struct i2c_device_id *id)
539 struct st21nfca_i2c_phy *phy;
542 dev_dbg(&client->dev, "%s\n", __func__);
543 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
545 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
546 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
550 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
555 phy->i2c_dev = client;
556 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
557 if (phy->pending_skb == NULL)
560 phy->current_read_len = 0;
562 mutex_init(&phy->phy_lock);
563 i2c_set_clientdata(client, phy);
565 if (client->dev.of_node) {
566 r = st21nfca_hci_i2c_of_request_resources(client);
568 nfc_err(&client->dev, "No platform data\n");
571 } else if (ACPI_HANDLE(&client->dev)) {
572 r = st21nfca_hci_i2c_acpi_request_resources(client);
574 nfc_err(&client->dev, "Cannot get ACPI data\n");
578 nfc_err(&client->dev, "st21nfca platform resources not available\n");
582 phy->se_status.is_ese_present =
583 device_property_read_bool(&client->dev, "ese-present");
584 phy->se_status.is_uicc_present =
585 device_property_read_bool(&client->dev, "uicc-present");
587 r = st21nfca_hci_platform_init(phy);
589 nfc_err(&client->dev, "Unable to reboot st21nfca\n");
593 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
594 st21nfca_hci_irq_thread_fn,
596 ST21NFCA_HCI_DRIVER_NAME, phy);
598 nfc_err(&client->dev, "Unable to register IRQ handler\n");
602 return st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
603 ST21NFCA_FRAME_HEADROOM,
604 ST21NFCA_FRAME_TAILROOM,
605 ST21NFCA_HCI_LLC_MAX_PAYLOAD,
610 static int st21nfca_hci_i2c_remove(struct i2c_client *client)
612 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
614 dev_dbg(&client->dev, "%s\n", __func__);
616 st21nfca_hci_remove(phy->hdev);
619 st21nfca_hci_i2c_disable(phy);
624 static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
625 {ST21NFCA_HCI_DRIVER_NAME, 0},
628 MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
630 static const struct acpi_device_id st21nfca_hci_i2c_acpi_match[] = {
634 MODULE_DEVICE_TABLE(acpi, st21nfca_hci_i2c_acpi_match);
636 static const struct of_device_id of_st21nfca_i2c_match[] = {
637 { .compatible = "st,st21nfca-i2c", },
638 { .compatible = "st,st21nfca_i2c", },
641 MODULE_DEVICE_TABLE(of, of_st21nfca_i2c_match);
643 static struct i2c_driver st21nfca_hci_i2c_driver = {
645 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
646 .of_match_table = of_match_ptr(of_st21nfca_i2c_match),
647 .acpi_match_table = ACPI_PTR(st21nfca_hci_i2c_acpi_match),
649 .probe = st21nfca_hci_i2c_probe,
650 .id_table = st21nfca_hci_i2c_id_table,
651 .remove = st21nfca_hci_i2c_remove,
653 module_i2c_driver(st21nfca_hci_i2c_driver);
655 MODULE_LICENSE("GPL");
656 MODULE_DESCRIPTION(DRIVER_DESC);