blob: d28a1398c09168783dc2f604c195a2903e01a549 [file] [log] [blame]
Sjur Braendeland529d6da2010-06-29 00:08:21 -07001/*
2 * Copyright (C) ST-Ericsson AB 2010
sjur.brandeland@stericsson.comc2cd0a52013-04-22 23:57:02 +00003 * Author: Daniel Martensson
Sjur Braendeland529d6da2010-06-29 00:08:21 -07004 * License terms: GNU General Public License (GPL) version 2.
5 */
6
Sjur Braendeland529d6da2010-06-29 00:08:21 -07007#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/device.h>
10#include <linux/platform_device.h>
11#include <linux/string.h>
12#include <linux/workqueue.h>
13#include <linux/completion.h>
14#include <linux/list.h>
15#include <linux/interrupt.h>
16#include <linux/dma-mapping.h>
17#include <linux/delay.h>
18#include <linux/sched.h>
19#include <linux/debugfs.h>
20#include <linux/if_arp.h>
21#include <net/caif/caif_layer.h>
22#include <net/caif/caif_spi.h>
23
24#ifndef CONFIG_CAIF_SPI_SYNC
25#define FLAVOR "Flavour: Vanilla.\n"
26#else
27#define FLAVOR "Flavour: Master CMD&LEN at start.\n"
28#endif /* CONFIG_CAIF_SPI_SYNC */
29
30MODULE_LICENSE("GPL");
sjur.brandeland@stericsson.comc2cd0a52013-04-22 23:57:02 +000031MODULE_AUTHOR("Daniel Martensson");
Sjur Braendeland529d6da2010-06-29 00:08:21 -070032MODULE_DESCRIPTION("CAIF SPI driver");
33
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +000034/* Returns the number of padding bytes for alignment. */
35#define PAD_POW2(x, pow) ((((x)&((pow)-1))==0) ? 0 : (((pow)-((x)&((pow)-1)))))
36
Rusty Russelleb939922011-12-19 14:08:01 +000037static bool spi_loop;
Joe Perchesd61e4032018-03-23 15:54:39 -070038module_param(spi_loop, bool, 0444);
Sjur Braendeland529d6da2010-06-29 00:08:21 -070039MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode.");
40
41/* SPI frame alignment. */
Joe Perchesd61e4032018-03-23 15:54:39 -070042module_param(spi_frm_align, int, 0444);
Sjur Braendeland529d6da2010-06-29 00:08:21 -070043MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment.");
44
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +000045/*
46 * SPI padding options.
47 * Warning: must be a base of 2 (& operation used) and can not be zero !
48 */
Joe Perchesd61e4032018-03-23 15:54:39 -070049module_param(spi_up_head_align, int, 0444);
Sjur Braendeland529d6da2010-06-29 00:08:21 -070050MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment.");
51
Joe Perchesd61e4032018-03-23 15:54:39 -070052module_param(spi_up_tail_align, int, 0444);
Sjur Braendeland529d6da2010-06-29 00:08:21 -070053MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment.");
54
Joe Perchesd61e4032018-03-23 15:54:39 -070055module_param(spi_down_head_align, int, 0444);
Sjur Braendeland529d6da2010-06-29 00:08:21 -070056MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment.");
57
Joe Perchesd61e4032018-03-23 15:54:39 -070058module_param(spi_down_tail_align, int, 0444);
Sjur Braendeland529d6da2010-06-29 00:08:21 -070059MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment.");
60
61#ifdef CONFIG_ARM
62#define BYTE_HEX_FMT "%02X"
63#else
64#define BYTE_HEX_FMT "%02hhX"
65#endif
66
67#define SPI_MAX_PAYLOAD_SIZE 4096
68/*
69 * Threshold values for the SPI packet queue. Flowcontrol will be asserted
70 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
71 * deasserted before the number of packets drops below LOW_WATER_MARK.
72 */
73#define LOW_WATER_MARK 100
74#define HIGH_WATER_MARK (LOW_WATER_MARK*5)
75
76#ifdef CONFIG_UML
77
78/*
79 * We sometimes use UML for debugging, but it cannot handle
80 * dma_alloc_coherent so we have to wrap it.
81 */
82static inline void *dma_alloc(dma_addr_t *daddr)
83{
84 return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL);
85}
86
87static inline void dma_free(void *cpu_addr, dma_addr_t handle)
88{
89 kfree(cpu_addr);
90}
91
92#else
93
94static inline void *dma_alloc(dma_addr_t *daddr)
95{
96 return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr,
97 GFP_KERNEL);
98}
99
100static inline void dma_free(void *cpu_addr, dma_addr_t handle)
101{
102 dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle);
103}
104#endif /* CONFIG_UML */
105
106#ifdef CONFIG_DEBUG_FS
107
108#define DEBUGFS_BUF_SIZE 4096
109
110static struct dentry *dbgfs_root;
111
112static inline void driver_debugfs_create(void)
113{
114 dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL);
115}
116
117static inline void driver_debugfs_remove(void)
118{
119 debugfs_remove(dbgfs_root);
120}
121
122static inline void dev_debugfs_rem(struct cfspi *cfspi)
123{
124 debugfs_remove(cfspi->dbgfs_frame);
125 debugfs_remove(cfspi->dbgfs_state);
126 debugfs_remove(cfspi->dbgfs_dir);
127}
128
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700129static ssize_t dbgfs_state(struct file *file, char __user *user_buf,
130 size_t count, loff_t *ppos)
131{
132 char *buf;
133 int len = 0;
134 ssize_t size;
Joe Perchescb8b6a92010-07-12 10:50:05 +0000135 struct cfspi *cfspi = file->private_data;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700136
137 buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
138 if (!buf)
139 return 0;
140
141 /* Print out debug information. */
142 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
143 "CAIF SPI debug information:\n");
144
145 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR);
146
147 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
148 "STATE: %d\n", cfspi->dbg_state);
149 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
150 "Previous CMD: 0x%x\n", cfspi->pcmd);
151 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
152 "Current CMD: 0x%x\n", cfspi->cmd);
153 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
154 "Previous TX len: %d\n", cfspi->tx_ppck_len);
155 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
156 "Previous RX len: %d\n", cfspi->rx_ppck_len);
157 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
158 "Current TX len: %d\n", cfspi->tx_cpck_len);
159 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
160 "Current RX len: %d\n", cfspi->rx_cpck_len);
161 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
162 "Next TX len: %d\n", cfspi->tx_npck_len);
163 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
164 "Next RX len: %d\n", cfspi->rx_npck_len);
165
Dan Carpenter7b7b0b92010-07-25 21:23:59 +0000166 if (len > DEBUGFS_BUF_SIZE)
167 len = DEBUGFS_BUF_SIZE;
168
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700169 size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
170 kfree(buf);
171
172 return size;
173}
174
175static ssize_t print_frame(char *buf, size_t size, char *frm,
176 size_t count, size_t cut)
177{
178 int len = 0;
179 int i;
180 for (i = 0; i < count; i++) {
181 len += snprintf((buf + len), (size - len),
182 "[0x" BYTE_HEX_FMT "]",
183 frm[i]);
184 if ((i == cut) && (count > (cut * 2))) {
185 /* Fast forward. */
186 i = count - cut;
187 len += snprintf((buf + len), (size - len),
xypron.glpk@gmx.de59d53bc2016-07-31 09:19:50 +0200188 "--- %zu bytes skipped ---\n",
189 count - (cut * 2));
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700190 }
191
192 if ((!(i % 10)) && i) {
193 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
194 "\n");
195 }
196 }
197 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n");
198 return len;
199}
200
201static ssize_t dbgfs_frame(struct file *file, char __user *user_buf,
202 size_t count, loff_t *ppos)
203{
204 char *buf;
205 int len = 0;
206 ssize_t size;
207 struct cfspi *cfspi;
208
Joe Perchescb8b6a92010-07-12 10:50:05 +0000209 cfspi = file->private_data;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700210 buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
211 if (!buf)
212 return 0;
213
214 /* Print out debug information. */
215 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
216 "Current frame:\n");
217
218 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
219 "Tx data (Len: %d):\n", cfspi->tx_cpck_len);
220
221 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000222 cfspi->xfer.va_tx[0],
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700223 (cfspi->tx_cpck_len + SPI_CMD_SZ), 100);
224
225 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
226 "Rx data (Len: %d):\n", cfspi->rx_cpck_len);
227
228 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
229 cfspi->xfer.va_rx,
230 (cfspi->rx_cpck_len + SPI_CMD_SZ), 100);
231
232 size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
233 kfree(buf);
234
235 return size;
236}
237
238static const struct file_operations dbgfs_state_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700239 .open = simple_open,
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700240 .read = dbgfs_state,
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000241 .owner = THIS_MODULE
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700242};
243
244static const struct file_operations dbgfs_frame_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700245 .open = simple_open,
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700246 .read = dbgfs_frame,
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000247 .owner = THIS_MODULE
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700248};
249
250static inline void dev_debugfs_add(struct cfspi *cfspi)
251{
252 cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root);
Joe Perchesd61e4032018-03-23 15:54:39 -0700253 cfspi->dbgfs_state = debugfs_create_file("state", 0444,
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700254 cfspi->dbgfs_dir, cfspi,
255 &dbgfs_state_fops);
Joe Perchesd61e4032018-03-23 15:54:39 -0700256 cfspi->dbgfs_frame = debugfs_create_file("frame", 0444,
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700257 cfspi->dbgfs_dir, cfspi,
258 &dbgfs_frame_fops);
259}
260
261inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
262{
263 cfspi->dbg_state = state;
264};
265#else
266
267static inline void driver_debugfs_create(void)
268{
269}
270
271static inline void driver_debugfs_remove(void)
272{
273}
274
275static inline void dev_debugfs_add(struct cfspi *cfspi)
276{
277}
278
279static inline void dev_debugfs_rem(struct cfspi *cfspi)
280{
281}
282
283inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
284{
285}
286#endif /* CONFIG_DEBUG_FS */
287
288static LIST_HEAD(cfspi_list);
289static spinlock_t cfspi_list_lock;
290
291/* SPI uplink head alignment. */
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200292static ssize_t up_head_align_show(struct device_driver *driver, char *buf)
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700293{
294 return sprintf(buf, "%d\n", spi_up_head_align);
295}
296
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200297static DRIVER_ATTR_RO(up_head_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700298
299/* SPI uplink tail alignment. */
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200300static ssize_t up_tail_align_show(struct device_driver *driver, char *buf)
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700301{
302 return sprintf(buf, "%d\n", spi_up_tail_align);
303}
304
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200305static DRIVER_ATTR_RO(up_tail_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700306
307/* SPI downlink head alignment. */
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200308static ssize_t down_head_align_show(struct device_driver *driver, char *buf)
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700309{
310 return sprintf(buf, "%d\n", spi_down_head_align);
311}
312
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200313static DRIVER_ATTR_RO(down_head_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700314
315/* SPI downlink tail alignment. */
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200316static ssize_t down_tail_align_show(struct device_driver *driver, char *buf)
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700317{
318 return sprintf(buf, "%d\n", spi_down_tail_align);
319}
320
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200321static DRIVER_ATTR_RO(down_tail_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700322
323/* SPI frame alignment. */
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200324static ssize_t frame_align_show(struct device_driver *driver, char *buf)
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700325{
326 return sprintf(buf, "%d\n", spi_frm_align);
327}
328
Greg Kroah-Hartmanf85205c2017-06-09 11:03:08 +0200329static DRIVER_ATTR_RO(frame_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700330
331int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len)
332{
333 u8 *dst = buf;
334 caif_assert(buf);
335
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000336 if (cfspi->slave && !cfspi->slave_talked)
337 cfspi->slave_talked = true;
338
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700339 do {
340 struct sk_buff *skb;
341 struct caif_payload_info *info;
342 int spad = 0;
343 int epad;
344
345 skb = skb_dequeue(&cfspi->chead);
346 if (!skb)
347 break;
348
349 /*
350 * Calculate length of frame including SPI padding.
351 * The payload position is found in the control buffer.
352 */
353 info = (struct caif_payload_info *)&skb->cb;
354
355 /*
356 * Compute head offset i.e. number of bytes to add to
357 * get the start of the payload aligned.
358 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000359 if (spi_up_head_align > 1) {
360 spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700361 *dst = (u8)(spad - 1);
362 dst += spad;
363 }
364
365 /* Copy in CAIF frame. */
366 skb_copy_bits(skb, 0, dst, skb->len);
367 dst += skb->len;
368 cfspi->ndev->stats.tx_packets++;
369 cfspi->ndev->stats.tx_bytes += skb->len;
370
371 /*
372 * Compute tail offset i.e. number of bytes to add to
373 * get the complete CAIF frame aligned.
374 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000375 epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700376 dst += epad;
377
378 dev_kfree_skb(skb);
379
380 } while ((dst - buf) < len);
381
382 return dst - buf;
383}
384
385int cfspi_xmitlen(struct cfspi *cfspi)
386{
387 struct sk_buff *skb = NULL;
388 int frm_len = 0;
389 int pkts = 0;
390
391 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300392 * Decommit previously committed frames.
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700393 * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead)
394 */
395 while (skb_peek(&cfspi->chead)) {
396 skb = skb_dequeue_tail(&cfspi->chead);
397 skb_queue_head(&cfspi->qhead, skb);
398 }
399
400 do {
401 struct caif_payload_info *info = NULL;
402 int spad = 0;
403 int epad = 0;
404
405 skb = skb_dequeue(&cfspi->qhead);
406 if (!skb)
407 break;
408
409 /*
410 * Calculate length of frame including SPI padding.
411 * The payload position is found in the control buffer.
412 */
413 info = (struct caif_payload_info *)&skb->cb;
414
415 /*
416 * Compute head offset i.e. number of bytes to add to
417 * get the start of the payload aligned.
418 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000419 if (spi_up_head_align > 1)
420 spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700421
422 /*
423 * Compute tail offset i.e. number of bytes to add to
424 * get the complete CAIF frame aligned.
425 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000426 epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700427
428 if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) {
429 skb_queue_tail(&cfspi->chead, skb);
430 pkts++;
431 frm_len += skb->len + spad + epad;
432 } else {
433 /* Put back packet. */
434 skb_queue_head(&cfspi->qhead, skb);
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000435 break;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700436 }
437 } while (pkts <= CAIF_MAX_SPI_PKTS);
438
439 /*
440 * Send flow on if previously sent flow off
441 * and now go below the low water mark
442 */
443 if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark &&
444 cfspi->cfdev.flowctrl) {
445 cfspi->flow_off_sent = 0;
446 cfspi->cfdev.flowctrl(cfspi->ndev, 1);
447 }
448
449 return frm_len;
450}
451
452static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc)
453{
454 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
455
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000456 /*
457 * The slave device is the master on the link. Interrupts before the
458 * slave has transmitted are considered spurious.
459 */
460 if (cfspi->slave && !cfspi->slave_talked) {
461 printk(KERN_WARNING "CFSPI: Spurious SS interrupt.\n");
462 return;
463 }
464
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700465 if (!in_interrupt())
466 spin_lock(&cfspi->lock);
467 if (assert) {
468 set_bit(SPI_SS_ON, &cfspi->state);
469 set_bit(SPI_XFER, &cfspi->state);
470 } else {
471 set_bit(SPI_SS_OFF, &cfspi->state);
472 }
473 if (!in_interrupt())
474 spin_unlock(&cfspi->lock);
475
476 /* Wake up the xfer thread. */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000477 if (assert)
478 wake_up_interruptible(&cfspi->wait);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700479}
480
481static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc)
482{
483 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
484
485 /* Transfer done, complete work queue */
486 complete(&cfspi->comp);
487}
488
489static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev)
490{
491 struct cfspi *cfspi = NULL;
492 unsigned long flags;
493 if (!dev)
494 return -EINVAL;
495
496 cfspi = netdev_priv(dev);
497
498 skb_queue_tail(&cfspi->qhead, skb);
499
500 spin_lock_irqsave(&cfspi->lock, flags);
501 if (!test_and_set_bit(SPI_XFER, &cfspi->state)) {
502 /* Wake up xfer thread. */
503 wake_up_interruptible(&cfspi->wait);
504 }
505 spin_unlock_irqrestore(&cfspi->lock, flags);
506
507 /* Send flow off if number of bytes is above high water mark */
508 if (!cfspi->flow_off_sent &&
509 cfspi->qhead.qlen > cfspi->qd_high_mark &&
510 cfspi->cfdev.flowctrl) {
511 cfspi->flow_off_sent = 1;
512 cfspi->cfdev.flowctrl(cfspi->ndev, 0);
513 }
514
515 return 0;
516}
517
518int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len)
519{
520 u8 *src = buf;
521
522 caif_assert(buf != NULL);
523
524 do {
525 int res;
526 struct sk_buff *skb = NULL;
527 int spad = 0;
528 int epad = 0;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700529 int pkt_len = 0;
530
531 /*
532 * Compute head offset i.e. number of bytes added to
533 * get the start of the payload aligned.
534 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000535 if (spi_down_head_align > 1) {
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700536 spad = 1 + *src;
537 src += spad;
538 }
539
540 /* Read length of CAIF frame (little endian). */
541 pkt_len = *src;
542 pkt_len |= ((*(src+1)) << 8) & 0xFF00;
543 pkt_len += 2; /* Add FCS fields. */
544
545 /* Get a suitable caif packet and copy in data. */
546
547 skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1);
548 caif_assert(skb != NULL);
549
yuan linyub952f4d2017-06-18 22:52:04 +0800550 skb_put_data(skb, src, pkt_len);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700551 src += pkt_len;
552
553 skb->protocol = htons(ETH_P_CAIF);
554 skb_reset_mac_header(skb);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700555
556 /*
557 * Push received packet up the stack.
558 */
559 if (!spi_loop)
560 res = netif_rx_ni(skb);
561 else
562 res = cfspi_xmit(skb, cfspi->ndev);
563
564 if (!res) {
565 cfspi->ndev->stats.rx_packets++;
566 cfspi->ndev->stats.rx_bytes += pkt_len;
567 } else
568 cfspi->ndev->stats.rx_dropped++;
569
570 /*
571 * Compute tail offset i.e. number of bytes added to
572 * get the complete CAIF frame aligned.
573 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000574 epad = PAD_POW2((pkt_len + spad), spi_down_tail_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700575 src += epad;
576 } while ((src - buf) < len);
577
578 return src - buf;
579}
580
581static int cfspi_open(struct net_device *dev)
582{
583 netif_wake_queue(dev);
584 return 0;
585}
586
587static int cfspi_close(struct net_device *dev)
588{
589 netif_stop_queue(dev);
590 return 0;
591}
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700592
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000593static int cfspi_init(struct net_device *dev)
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700594{
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000595 int res = 0;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700596 struct cfspi *cfspi = netdev_priv(dev);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700597
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000598 /* Set flow info. */
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700599 cfspi->flow_off_sent = 0;
600 cfspi->qd_low_mark = LOW_WATER_MARK;
601 cfspi->qd_high_mark = HIGH_WATER_MARK;
602
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000603 /* Set slave info. */
604 if (!strncmp(cfspi_spi_driver.driver.name, "cfspi_sspi", 10)) {
605 cfspi->slave = true;
606 cfspi->slave_talked = false;
607 } else {
608 cfspi->slave = false;
609 cfspi->slave_talked = false;
610 }
611
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700612 /* Allocate DMA buffers. */
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000613 cfspi->xfer.va_tx[0] = dma_alloc(&cfspi->xfer.pa_tx[0]);
614 if (!cfspi->xfer.va_tx[0]) {
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700615 res = -ENODEV;
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000616 goto err_dma_alloc_tx_0;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700617 }
618
619 cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx);
620
621 if (!cfspi->xfer.va_rx) {
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700622 res = -ENODEV;
623 goto err_dma_alloc_rx;
624 }
625
626 /* Initialize the work queue. */
627 INIT_WORK(&cfspi->work, cfspi_xfer);
628
629 /* Initialize spin locks. */
630 spin_lock_init(&cfspi->lock);
631
632 /* Initialize flow control state. */
633 cfspi->flow_stop = false;
634
635 /* Initialize wait queue. */
636 init_waitqueue_head(&cfspi->wait);
637
638 /* Create work thread. */
639 cfspi->wq = create_singlethread_workqueue(dev->name);
640 if (!cfspi->wq) {
641 printk(KERN_WARNING "CFSPI: failed to create work queue.\n");
642 res = -ENODEV;
643 goto err_create_wq;
644 }
645
646 /* Initialize work queue. */
647 init_completion(&cfspi->comp);
648
649 /* Create debugfs entries. */
650 dev_debugfs_add(cfspi);
651
652 /* Set up the ifc. */
653 cfspi->ifc.ss_cb = cfspi_ss_cb;
654 cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb;
655 cfspi->ifc.priv = cfspi;
656
657 /* Add CAIF SPI device to list. */
658 spin_lock(&cfspi_list_lock);
659 list_add_tail(&cfspi->list, &cfspi_list);
660 spin_unlock(&cfspi_list_lock);
661
662 /* Schedule the work queue. */
663 queue_work(cfspi->wq, &cfspi->work);
664
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000665 return 0;
666
667 err_create_wq:
668 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
669 err_dma_alloc_rx:
670 dma_free(cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]);
671 err_dma_alloc_tx_0:
672 return res;
673}
674
675static void cfspi_uninit(struct net_device *dev)
676{
677 struct cfspi *cfspi = netdev_priv(dev);
678
679 /* Remove from list. */
680 spin_lock(&cfspi_list_lock);
681 list_del(&cfspi->list);
682 spin_unlock(&cfspi_list_lock);
683
684 cfspi->ndev = NULL;
685 /* Free DMA buffers. */
686 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
687 dma_free(cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]);
688 set_bit(SPI_TERMINATE, &cfspi->state);
689 wake_up_interruptible(&cfspi->wait);
690 destroy_workqueue(cfspi->wq);
691 /* Destroy debugfs directory and files. */
692 dev_debugfs_rem(cfspi);
693 return;
694}
695
696static const struct net_device_ops cfspi_ops = {
697 .ndo_open = cfspi_open,
698 .ndo_stop = cfspi_close,
699 .ndo_init = cfspi_init,
700 .ndo_uninit = cfspi_uninit,
701 .ndo_start_xmit = cfspi_xmit
702};
703
704static void cfspi_setup(struct net_device *dev)
705{
706 struct cfspi *cfspi = netdev_priv(dev);
707 dev->features = 0;
708 dev->netdev_ops = &cfspi_ops;
709 dev->type = ARPHRD_CAIF;
710 dev->flags = IFF_NOARP | IFF_POINTOPOINT;
Phil Sutter4676a152015-08-18 10:30:46 +0200711 dev->priv_flags |= IFF_NO_QUEUE;
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000712 dev->mtu = SPI_MAX_PAYLOAD_SIZE;
David S. Millercf124db2017-05-08 12:52:56 -0400713 dev->needs_free_netdev = true;
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000714 skb_queue_head_init(&cfspi->qhead);
715 skb_queue_head_init(&cfspi->chead);
716 cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
717 cfspi->cfdev.use_frag = false;
718 cfspi->cfdev.use_stx = false;
719 cfspi->cfdev.use_fcs = false;
720 cfspi->ndev = dev;
721}
722
723int cfspi_spi_probe(struct platform_device *pdev)
724{
725 struct cfspi *cfspi = NULL;
726 struct net_device *ndev;
727 struct cfspi_dev *dev;
728 int res;
729 dev = (struct cfspi_dev *)pdev->dev.platform_data;
730
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000731 if (!dev)
732 return -ENODEV;
733
Rasmus Villemoescfb76d72015-11-09 13:19:10 +0100734 ndev = alloc_netdev(sizeof(struct cfspi), "cfspi%d",
735 NET_NAME_UNKNOWN, cfspi_setup);
736 if (!ndev)
737 return -ENOMEM;
738
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000739 cfspi = netdev_priv(ndev);
740 netif_stop_queue(ndev);
741 cfspi->ndev = ndev;
742 cfspi->pdev = pdev;
743
744 /* Assign the SPI device. */
745 cfspi->dev = dev;
746 /* Assign the device ifc to this SPI interface. */
747 dev->ifc = &cfspi->ifc;
748
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700749 /* Register network device. */
750 res = register_netdev(ndev);
751 if (res) {
752 printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res);
753 goto err_net_reg;
754 }
755 return res;
756
757 err_net_reg:
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700758 free_netdev(ndev);
759
760 return res;
761}
762
763int cfspi_spi_remove(struct platform_device *pdev)
764{
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000765 /* Everything is done in cfspi_uninit(). */
766 return 0;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700767}
768
769static void __exit cfspi_exit_module(void)
770{
771 struct list_head *list_node;
772 struct list_head *n;
773 struct cfspi *cfspi = NULL;
774
775 list_for_each_safe(list_node, n, &cfspi_list) {
776 cfspi = list_entry(list_node, struct cfspi, list);
Erwan Bracqd5f43c12011-12-06 07:25:05 +0000777 unregister_netdev(cfspi->ndev);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700778 }
779
780 /* Destroy sysfs files. */
781 driver_remove_file(&cfspi_spi_driver.driver,
782 &driver_attr_up_head_align);
783 driver_remove_file(&cfspi_spi_driver.driver,
784 &driver_attr_up_tail_align);
785 driver_remove_file(&cfspi_spi_driver.driver,
786 &driver_attr_down_head_align);
787 driver_remove_file(&cfspi_spi_driver.driver,
788 &driver_attr_down_tail_align);
789 driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align);
790 /* Unregister platform driver. */
791 platform_driver_unregister(&cfspi_spi_driver);
792 /* Destroy debugfs root directory. */
793 driver_debugfs_remove();
794}
795
796static int __init cfspi_init_module(void)
797{
798 int result;
799
800 /* Initialize spin lock. */
801 spin_lock_init(&cfspi_list_lock);
802
803 /* Register platform driver. */
804 result = platform_driver_register(&cfspi_spi_driver);
805 if (result) {
806 printk(KERN_ERR "Could not register platform SPI driver.\n");
807 goto err_dev_register;
808 }
809
810 /* Create sysfs files. */
811 result =
812 driver_create_file(&cfspi_spi_driver.driver,
813 &driver_attr_up_head_align);
814 if (result) {
815 printk(KERN_ERR "Sysfs creation failed 1.\n");
816 goto err_create_up_head_align;
817 }
818
819 result =
820 driver_create_file(&cfspi_spi_driver.driver,
821 &driver_attr_up_tail_align);
822 if (result) {
823 printk(KERN_ERR "Sysfs creation failed 2.\n");
824 goto err_create_up_tail_align;
825 }
826
827 result =
828 driver_create_file(&cfspi_spi_driver.driver,
829 &driver_attr_down_head_align);
830 if (result) {
831 printk(KERN_ERR "Sysfs creation failed 3.\n");
832 goto err_create_down_head_align;
833 }
834
835 result =
836 driver_create_file(&cfspi_spi_driver.driver,
837 &driver_attr_down_tail_align);
838 if (result) {
839 printk(KERN_ERR "Sysfs creation failed 4.\n");
840 goto err_create_down_tail_align;
841 }
842
843 result =
844 driver_create_file(&cfspi_spi_driver.driver,
845 &driver_attr_frame_align);
846 if (result) {
847 printk(KERN_ERR "Sysfs creation failed 5.\n");
848 goto err_create_frame_align;
849 }
850 driver_debugfs_create();
851 return result;
852
853 err_create_frame_align:
854 driver_remove_file(&cfspi_spi_driver.driver,
855 &driver_attr_down_tail_align);
856 err_create_down_tail_align:
857 driver_remove_file(&cfspi_spi_driver.driver,
858 &driver_attr_down_head_align);
859 err_create_down_head_align:
860 driver_remove_file(&cfspi_spi_driver.driver,
861 &driver_attr_up_tail_align);
862 err_create_up_tail_align:
863 driver_remove_file(&cfspi_spi_driver.driver,
864 &driver_attr_up_head_align);
865 err_create_up_head_align:
Wei Yongjune56db272013-04-25 15:15:23 +0800866 platform_driver_unregister(&cfspi_spi_driver);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700867 err_dev_register:
868 return result;
869}
870
871module_init(cfspi_init_module);
872module_exit(cfspi_exit_module);