1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Intel Corporation. All rights reserved.
6 #define pr_fmt(fmt) "nci_spi: %s: " fmt, __func__
8 #include <linux/module.h>
10 #include <linux/export.h>
11 #include <linux/spi/spi.h>
12 #include <linux/crc-ccitt.h>
13 #include <net/nfc/nci_core.h>
15 #define NCI_SPI_ACK_SHIFT 6
16 #define NCI_SPI_MSB_PAYLOAD_MASK 0x3F
18 #define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
19 NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
21 #define NCI_SPI_DIRECT_WRITE 0x01
22 #define NCI_SPI_DIRECT_READ 0x02
24 #define ACKNOWLEDGE_NONE 0
25 #define ACKNOWLEDGE_ACK 1
26 #define ACKNOWLEDGE_NACK 2
28 #define CRC_INIT 0xFFFF
30 static int __nci_spi_send(struct nci_spi *nspi, struct sk_buff *skb,
34 struct spi_transfer t;
36 memset(&t, 0, sizeof(struct spi_transfer));
37 /* a NULL skb means we just want the SPI chip select line to raise */
42 /* still set tx_buf non NULL to make the driver happy */
46 t.cs_change = cs_change;
47 t.delay_usecs = nspi->xfer_udelay;
48 t.speed_hz = nspi->xfer_speed_hz;
51 spi_message_add_tail(&t, &m);
53 return spi_sync(nspi->spi, &m);
56 int nci_spi_send(struct nci_spi *nspi,
57 struct completion *write_handshake_completion,
60 unsigned int payload_len = skb->len;
65 /* add the NCI SPI header to the start of the buffer */
66 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
67 hdr[0] = NCI_SPI_DIRECT_WRITE;
68 hdr[1] = nspi->acknowledge_mode;
69 hdr[2] = payload_len >> 8;
70 hdr[3] = payload_len & 0xFF;
72 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
75 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
76 skb_put_u8(skb, crc >> 8);
77 skb_put_u8(skb, crc & 0xFF);
80 if (write_handshake_completion) {
81 /* Trick SPI driver to raise chip select */
82 ret = __nci_spi_send(nspi, NULL, 1);
86 /* wait for NFC chip hardware handshake to complete */
87 if (wait_for_completion_timeout(write_handshake_completion,
88 msecs_to_jiffies(1000)) == 0) {
94 ret = __nci_spi_send(nspi, skb, 0);
95 if (ret != 0 || nspi->acknowledge_mode == NCI_SPI_CRC_DISABLED)
98 reinit_completion(&nspi->req_completion);
99 completion_rc = wait_for_completion_interruptible_timeout(
100 &nspi->req_completion,
101 NCI_SPI_SEND_TIMEOUT);
103 if (completion_rc <= 0 || nspi->req_result == ACKNOWLEDGE_NACK)
111 EXPORT_SYMBOL_GPL(nci_spi_send);
113 /* ---- Interface to NCI SPI drivers ---- */
116 * nci_spi_allocate_spi - allocate a new nci spi
119 * @acknowledge_mode: Acknowledge mode used by the NFC device
120 * @delay: delay between transactions in us
121 * @ndev: nci dev to send incoming nci frames to
123 struct nci_spi *nci_spi_allocate_spi(struct spi_device *spi,
124 u8 acknowledge_mode, unsigned int delay,
125 struct nci_dev *ndev)
127 struct nci_spi *nspi;
129 nspi = devm_kzalloc(&spi->dev, sizeof(struct nci_spi), GFP_KERNEL);
133 nspi->acknowledge_mode = acknowledge_mode;
134 nspi->xfer_udelay = delay;
135 /* Use controller max SPI speed by default */
136 nspi->xfer_speed_hz = 0;
139 init_completion(&nspi->req_completion);
143 EXPORT_SYMBOL_GPL(nci_spi_allocate_spi);
145 static int send_acknowledge(struct nci_spi *nspi, u8 acknowledge)
152 skb = nci_skb_alloc(nspi->ndev, 0, GFP_KERNEL);
154 /* add the NCI SPI header to the start of the buffer */
155 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
156 hdr[0] = NCI_SPI_DIRECT_WRITE;
157 hdr[1] = NCI_SPI_CRC_ENABLED;
158 hdr[2] = acknowledge << NCI_SPI_ACK_SHIFT;
161 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
162 skb_put_u8(skb, crc >> 8);
163 skb_put_u8(skb, crc & 0xFF);
165 ret = __nci_spi_send(nspi, skb, 0);
172 static struct sk_buff *__nci_spi_read(struct nci_spi *nspi)
175 struct spi_message m;
176 unsigned char req[2], resp_hdr[2];
177 struct spi_transfer tx, rx;
178 unsigned short rx_len = 0;
181 spi_message_init(&m);
183 memset(&tx, 0, sizeof(struct spi_transfer));
184 req[0] = NCI_SPI_DIRECT_READ;
185 req[1] = nspi->acknowledge_mode;
189 tx.speed_hz = nspi->xfer_speed_hz;
190 spi_message_add_tail(&tx, &m);
192 memset(&rx, 0, sizeof(struct spi_transfer));
193 rx.rx_buf = resp_hdr;
196 rx.speed_hz = nspi->xfer_speed_hz;
197 spi_message_add_tail(&rx, &m);
199 ret = spi_sync(nspi->spi, &m);
203 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
204 rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) +
205 resp_hdr[1] + NCI_SPI_CRC_LEN;
207 rx_len = (resp_hdr[0] << 8) | resp_hdr[1];
209 skb = nci_skb_alloc(nspi->ndev, rx_len, GFP_KERNEL);
213 spi_message_init(&m);
215 memset(&rx, 0, sizeof(struct spi_transfer));
216 rx.rx_buf = skb_put(skb, rx_len);
219 rx.delay_usecs = nspi->xfer_udelay;
220 rx.speed_hz = nspi->xfer_speed_hz;
221 spi_message_add_tail(&rx, &m);
223 ret = spi_sync(nspi->spi, &m);
227 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
228 *(u8 *)skb_push(skb, 1) = resp_hdr[1];
229 *(u8 *)skb_push(skb, 1) = resp_hdr[0];
240 static int nci_spi_check_crc(struct sk_buff *skb)
242 u16 crc_data = (skb->data[skb->len - 2] << 8) |
243 skb->data[skb->len - 1];
246 ret = (crc_ccitt(CRC_INIT, skb->data, skb->len - NCI_SPI_CRC_LEN)
249 skb_trim(skb, skb->len - NCI_SPI_CRC_LEN);
254 static u8 nci_spi_get_ack(struct sk_buff *skb)
258 ret = skb->data[0] >> NCI_SPI_ACK_SHIFT;
260 /* Remove NFCC part of the header: ACK, NACK and MSB payload len */
267 * nci_spi_read - read frame from NCI SPI drivers
272 * This call may only be used from a context that may sleep. The sleep
273 * is non-interruptible, and has no timeout.
275 * It returns an allocated skb containing the frame on success, or NULL.
277 struct sk_buff *nci_spi_read(struct nci_spi *nspi)
281 /* Retrieve frame from SPI */
282 skb = __nci_spi_read(nspi);
286 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
287 if (!nci_spi_check_crc(skb)) {
288 send_acknowledge(nspi, ACKNOWLEDGE_NACK);
292 /* In case of acknowledged mode: if ACK or NACK received,
293 * unblock completion of latest frame sent.
295 nspi->req_result = nci_spi_get_ack(skb);
296 if (nspi->req_result)
297 complete(&nspi->req_completion);
300 /* If there is no payload (ACK/NACK only frame),
301 * free the socket buffer
309 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
310 send_acknowledge(nspi, ACKNOWLEDGE_ACK);
316 EXPORT_SYMBOL_GPL(nci_spi_read);
318 MODULE_LICENSE("GPL");