blob: 20da1996d354d563d8a2616276de477041b2368d [file] [log] [blame]
Sjur Braendeland529d6da2010-06-29 00:08:21 -07001/*
2 * Copyright (C) ST-Ericsson AB 2010
3 * Contact: Sjur Brendeland / sjur.brandeland@stericsson.com
4 * Author: Daniel Martensson / Daniel.Martensson@stericsson.com
5 * License terms: GNU General Public License (GPL) version 2.
6 */
7
8#include <linux/version.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/platform_device.h>
13#include <linux/string.h>
14#include <linux/workqueue.h>
15#include <linux/completion.h>
16#include <linux/list.h>
17#include <linux/interrupt.h>
18#include <linux/dma-mapping.h>
19#include <linux/delay.h>
20#include <linux/sched.h>
21#include <linux/debugfs.h>
22#include <linux/if_arp.h>
23#include <net/caif/caif_layer.h>
24#include <net/caif/caif_spi.h>
25
26#ifndef CONFIG_CAIF_SPI_SYNC
27#define FLAVOR "Flavour: Vanilla.\n"
28#else
29#define FLAVOR "Flavour: Master CMD&LEN at start.\n"
30#endif /* CONFIG_CAIF_SPI_SYNC */
31
32MODULE_LICENSE("GPL");
33MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
34MODULE_DESCRIPTION("CAIF SPI driver");
35
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +000036/* Returns the number of padding bytes for alignment. */
37#define PAD_POW2(x, pow) ((((x)&((pow)-1))==0) ? 0 : (((pow)-((x)&((pow)-1)))))
38
Sjur Braendeland529d6da2010-06-29 00:08:21 -070039static int spi_loop;
40module_param(spi_loop, bool, S_IRUGO);
41MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode.");
42
43/* SPI frame alignment. */
44module_param(spi_frm_align, int, S_IRUGO);
45MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment.");
46
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +000047/*
48 * SPI padding options.
49 * Warning: must be a base of 2 (& operation used) and can not be zero !
50 */
Sjur Braendeland529d6da2010-06-29 00:08:21 -070051module_param(spi_up_head_align, int, S_IRUGO);
52MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment.");
53
54module_param(spi_up_tail_align, int, S_IRUGO);
55MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment.");
56
57module_param(spi_down_head_align, int, S_IRUGO);
58MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment.");
59
60module_param(spi_down_tail_align, int, S_IRUGO);
61MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment.");
62
63#ifdef CONFIG_ARM
64#define BYTE_HEX_FMT "%02X"
65#else
66#define BYTE_HEX_FMT "%02hhX"
67#endif
68
69#define SPI_MAX_PAYLOAD_SIZE 4096
70/*
71 * Threshold values for the SPI packet queue. Flowcontrol will be asserted
72 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
73 * deasserted before the number of packets drops below LOW_WATER_MARK.
74 */
75#define LOW_WATER_MARK 100
76#define HIGH_WATER_MARK (LOW_WATER_MARK*5)
77
78#ifdef CONFIG_UML
79
80/*
81 * We sometimes use UML for debugging, but it cannot handle
82 * dma_alloc_coherent so we have to wrap it.
83 */
84static inline void *dma_alloc(dma_addr_t *daddr)
85{
86 return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL);
87}
88
89static inline void dma_free(void *cpu_addr, dma_addr_t handle)
90{
91 kfree(cpu_addr);
92}
93
94#else
95
96static inline void *dma_alloc(dma_addr_t *daddr)
97{
98 return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr,
99 GFP_KERNEL);
100}
101
102static inline void dma_free(void *cpu_addr, dma_addr_t handle)
103{
104 dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle);
105}
106#endif /* CONFIG_UML */
107
108#ifdef CONFIG_DEBUG_FS
109
110#define DEBUGFS_BUF_SIZE 4096
111
112static struct dentry *dbgfs_root;
113
114static inline void driver_debugfs_create(void)
115{
116 dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL);
117}
118
119static inline void driver_debugfs_remove(void)
120{
121 debugfs_remove(dbgfs_root);
122}
123
124static inline void dev_debugfs_rem(struct cfspi *cfspi)
125{
126 debugfs_remove(cfspi->dbgfs_frame);
127 debugfs_remove(cfspi->dbgfs_state);
128 debugfs_remove(cfspi->dbgfs_dir);
129}
130
131static int dbgfs_open(struct inode *inode, struct file *file)
132{
133 file->private_data = inode->i_private;
134 return 0;
135}
136
137static ssize_t dbgfs_state(struct file *file, char __user *user_buf,
138 size_t count, loff_t *ppos)
139{
140 char *buf;
141 int len = 0;
142 ssize_t size;
Joe Perchescb8b6a92010-07-12 10:50:05 +0000143 struct cfspi *cfspi = file->private_data;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700144
145 buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
146 if (!buf)
147 return 0;
148
149 /* Print out debug information. */
150 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
151 "CAIF SPI debug information:\n");
152
153 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR);
154
155 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
156 "STATE: %d\n", cfspi->dbg_state);
157 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
158 "Previous CMD: 0x%x\n", cfspi->pcmd);
159 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
160 "Current CMD: 0x%x\n", cfspi->cmd);
161 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
162 "Previous TX len: %d\n", cfspi->tx_ppck_len);
163 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
164 "Previous RX len: %d\n", cfspi->rx_ppck_len);
165 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
166 "Current TX len: %d\n", cfspi->tx_cpck_len);
167 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
168 "Current RX len: %d\n", cfspi->rx_cpck_len);
169 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
170 "Next TX len: %d\n", cfspi->tx_npck_len);
171 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
172 "Next RX len: %d\n", cfspi->rx_npck_len);
173
Dan Carpenter7b7b0b92010-07-25 21:23:59 +0000174 if (len > DEBUGFS_BUF_SIZE)
175 len = DEBUGFS_BUF_SIZE;
176
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700177 size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
178 kfree(buf);
179
180 return size;
181}
182
183static ssize_t print_frame(char *buf, size_t size, char *frm,
184 size_t count, size_t cut)
185{
186 int len = 0;
187 int i;
188 for (i = 0; i < count; i++) {
189 len += snprintf((buf + len), (size - len),
190 "[0x" BYTE_HEX_FMT "]",
191 frm[i]);
192 if ((i == cut) && (count > (cut * 2))) {
193 /* Fast forward. */
194 i = count - cut;
195 len += snprintf((buf + len), (size - len),
196 "--- %u bytes skipped ---\n",
197 (int)(count - (cut * 2)));
198 }
199
200 if ((!(i % 10)) && i) {
201 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
202 "\n");
203 }
204 }
205 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n");
206 return len;
207}
208
209static ssize_t dbgfs_frame(struct file *file, char __user *user_buf,
210 size_t count, loff_t *ppos)
211{
212 char *buf;
213 int len = 0;
214 ssize_t size;
215 struct cfspi *cfspi;
216
Joe Perchescb8b6a92010-07-12 10:50:05 +0000217 cfspi = file->private_data;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700218 buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
219 if (!buf)
220 return 0;
221
222 /* Print out debug information. */
223 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
224 "Current frame:\n");
225
226 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
227 "Tx data (Len: %d):\n", cfspi->tx_cpck_len);
228
229 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
230 cfspi->xfer.va_tx,
231 (cfspi->tx_cpck_len + SPI_CMD_SZ), 100);
232
233 len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
234 "Rx data (Len: %d):\n", cfspi->rx_cpck_len);
235
236 len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
237 cfspi->xfer.va_rx,
238 (cfspi->rx_cpck_len + SPI_CMD_SZ), 100);
239
240 size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
241 kfree(buf);
242
243 return size;
244}
245
246static const struct file_operations dbgfs_state_fops = {
247 .open = dbgfs_open,
248 .read = dbgfs_state,
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000249 .owner = THIS_MODULE
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700250};
251
252static const struct file_operations dbgfs_frame_fops = {
253 .open = dbgfs_open,
254 .read = dbgfs_frame,
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000255 .owner = THIS_MODULE
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700256};
257
258static inline void dev_debugfs_add(struct cfspi *cfspi)
259{
260 cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root);
261 cfspi->dbgfs_state = debugfs_create_file("state", S_IRUGO,
262 cfspi->dbgfs_dir, cfspi,
263 &dbgfs_state_fops);
264 cfspi->dbgfs_frame = debugfs_create_file("frame", S_IRUGO,
265 cfspi->dbgfs_dir, cfspi,
266 &dbgfs_frame_fops);
267}
268
269inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
270{
271 cfspi->dbg_state = state;
272};
273#else
274
275static inline void driver_debugfs_create(void)
276{
277}
278
279static inline void driver_debugfs_remove(void)
280{
281}
282
283static inline void dev_debugfs_add(struct cfspi *cfspi)
284{
285}
286
287static inline void dev_debugfs_rem(struct cfspi *cfspi)
288{
289}
290
291inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
292{
293}
294#endif /* CONFIG_DEBUG_FS */
295
296static LIST_HEAD(cfspi_list);
297static spinlock_t cfspi_list_lock;
298
299/* SPI uplink head alignment. */
300static ssize_t show_up_head_align(struct device_driver *driver, char *buf)
301{
302 return sprintf(buf, "%d\n", spi_up_head_align);
303}
304
305static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL);
306
307/* SPI uplink tail alignment. */
308static ssize_t show_up_tail_align(struct device_driver *driver, char *buf)
309{
310 return sprintf(buf, "%d\n", spi_up_tail_align);
311}
312
313static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL);
314
315/* SPI downlink head alignment. */
316static ssize_t show_down_head_align(struct device_driver *driver, char *buf)
317{
318 return sprintf(buf, "%d\n", spi_down_head_align);
319}
320
321static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL);
322
323/* SPI downlink tail alignment. */
324static ssize_t show_down_tail_align(struct device_driver *driver, char *buf)
325{
326 return sprintf(buf, "%d\n", spi_down_tail_align);
327}
328
329static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL);
330
331/* SPI frame alignment. */
332static ssize_t show_frame_align(struct device_driver *driver, char *buf)
333{
334 return sprintf(buf, "%d\n", spi_frm_align);
335}
336
337static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL);
338
339int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len)
340{
341 u8 *dst = buf;
342 caif_assert(buf);
343
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000344 if (cfspi->slave && !cfspi->slave_talked)
345 cfspi->slave_talked = true;
346
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700347 do {
348 struct sk_buff *skb;
349 struct caif_payload_info *info;
350 int spad = 0;
351 int epad;
352
353 skb = skb_dequeue(&cfspi->chead);
354 if (!skb)
355 break;
356
357 /*
358 * Calculate length of frame including SPI padding.
359 * The payload position is found in the control buffer.
360 */
361 info = (struct caif_payload_info *)&skb->cb;
362
363 /*
364 * Compute head offset i.e. number of bytes to add to
365 * get the start of the payload aligned.
366 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000367 if (spi_up_head_align > 1) {
368 spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700369 *dst = (u8)(spad - 1);
370 dst += spad;
371 }
372
373 /* Copy in CAIF frame. */
374 skb_copy_bits(skb, 0, dst, skb->len);
375 dst += skb->len;
376 cfspi->ndev->stats.tx_packets++;
377 cfspi->ndev->stats.tx_bytes += skb->len;
378
379 /*
380 * Compute tail offset i.e. number of bytes to add to
381 * get the complete CAIF frame aligned.
382 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000383 epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700384 dst += epad;
385
386 dev_kfree_skb(skb);
387
388 } while ((dst - buf) < len);
389
390 return dst - buf;
391}
392
393int cfspi_xmitlen(struct cfspi *cfspi)
394{
395 struct sk_buff *skb = NULL;
396 int frm_len = 0;
397 int pkts = 0;
398
399 /*
400 * Decommit previously commited frames.
401 * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead)
402 */
403 while (skb_peek(&cfspi->chead)) {
404 skb = skb_dequeue_tail(&cfspi->chead);
405 skb_queue_head(&cfspi->qhead, skb);
406 }
407
408 do {
409 struct caif_payload_info *info = NULL;
410 int spad = 0;
411 int epad = 0;
412
413 skb = skb_dequeue(&cfspi->qhead);
414 if (!skb)
415 break;
416
417 /*
418 * Calculate length of frame including SPI padding.
419 * The payload position is found in the control buffer.
420 */
421 info = (struct caif_payload_info *)&skb->cb;
422
423 /*
424 * Compute head offset i.e. number of bytes to add to
425 * get the start of the payload aligned.
426 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000427 if (spi_up_head_align > 1)
428 spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700429
430 /*
431 * Compute tail offset i.e. number of bytes to add to
432 * get the complete CAIF frame aligned.
433 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000434 epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700435
436 if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) {
437 skb_queue_tail(&cfspi->chead, skb);
438 pkts++;
439 frm_len += skb->len + spad + epad;
440 } else {
441 /* Put back packet. */
442 skb_queue_head(&cfspi->qhead, skb);
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000443 break;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700444 }
445 } while (pkts <= CAIF_MAX_SPI_PKTS);
446
447 /*
448 * Send flow on if previously sent flow off
449 * and now go below the low water mark
450 */
451 if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark &&
452 cfspi->cfdev.flowctrl) {
453 cfspi->flow_off_sent = 0;
454 cfspi->cfdev.flowctrl(cfspi->ndev, 1);
455 }
456
457 return frm_len;
458}
459
460static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc)
461{
462 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
463
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000464 /*
465 * The slave device is the master on the link. Interrupts before the
466 * slave has transmitted are considered spurious.
467 */
468 if (cfspi->slave && !cfspi->slave_talked) {
469 printk(KERN_WARNING "CFSPI: Spurious SS interrupt.\n");
470 return;
471 }
472
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700473 if (!in_interrupt())
474 spin_lock(&cfspi->lock);
475 if (assert) {
476 set_bit(SPI_SS_ON, &cfspi->state);
477 set_bit(SPI_XFER, &cfspi->state);
478 } else {
479 set_bit(SPI_SS_OFF, &cfspi->state);
480 }
481 if (!in_interrupt())
482 spin_unlock(&cfspi->lock);
483
484 /* Wake up the xfer thread. */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000485 if (assert)
486 wake_up_interruptible(&cfspi->wait);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700487}
488
489static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc)
490{
491 struct cfspi *cfspi = (struct cfspi *)ifc->priv;
492
493 /* Transfer done, complete work queue */
494 complete(&cfspi->comp);
495}
496
497static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev)
498{
499 struct cfspi *cfspi = NULL;
500 unsigned long flags;
501 if (!dev)
502 return -EINVAL;
503
504 cfspi = netdev_priv(dev);
505
506 skb_queue_tail(&cfspi->qhead, skb);
507
508 spin_lock_irqsave(&cfspi->lock, flags);
509 if (!test_and_set_bit(SPI_XFER, &cfspi->state)) {
510 /* Wake up xfer thread. */
511 wake_up_interruptible(&cfspi->wait);
512 }
513 spin_unlock_irqrestore(&cfspi->lock, flags);
514
515 /* Send flow off if number of bytes is above high water mark */
516 if (!cfspi->flow_off_sent &&
517 cfspi->qhead.qlen > cfspi->qd_high_mark &&
518 cfspi->cfdev.flowctrl) {
519 cfspi->flow_off_sent = 1;
520 cfspi->cfdev.flowctrl(cfspi->ndev, 0);
521 }
522
523 return 0;
524}
525
526int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len)
527{
528 u8 *src = buf;
529
530 caif_assert(buf != NULL);
531
532 do {
533 int res;
534 struct sk_buff *skb = NULL;
535 int spad = 0;
536 int epad = 0;
537 u8 *dst = NULL;
538 int pkt_len = 0;
539
540 /*
541 * Compute head offset i.e. number of bytes added to
542 * get the start of the payload aligned.
543 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000544 if (spi_down_head_align > 1) {
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700545 spad = 1 + *src;
546 src += spad;
547 }
548
549 /* Read length of CAIF frame (little endian). */
550 pkt_len = *src;
551 pkt_len |= ((*(src+1)) << 8) & 0xFF00;
552 pkt_len += 2; /* Add FCS fields. */
553
554 /* Get a suitable caif packet and copy in data. */
555
556 skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1);
557 caif_assert(skb != NULL);
558
559 dst = skb_put(skb, pkt_len);
560 memcpy(dst, src, pkt_len);
561 src += pkt_len;
562
563 skb->protocol = htons(ETH_P_CAIF);
564 skb_reset_mac_header(skb);
565 skb->dev = cfspi->ndev;
566
567 /*
568 * Push received packet up the stack.
569 */
570 if (!spi_loop)
571 res = netif_rx_ni(skb);
572 else
573 res = cfspi_xmit(skb, cfspi->ndev);
574
575 if (!res) {
576 cfspi->ndev->stats.rx_packets++;
577 cfspi->ndev->stats.rx_bytes += pkt_len;
578 } else
579 cfspi->ndev->stats.rx_dropped++;
580
581 /*
582 * Compute tail offset i.e. number of bytes added to
583 * get the complete CAIF frame aligned.
584 */
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000585 epad = PAD_POW2((pkt_len + spad), spi_down_tail_align);
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700586 src += epad;
587 } while ((src - buf) < len);
588
589 return src - buf;
590}
591
592static int cfspi_open(struct net_device *dev)
593{
594 netif_wake_queue(dev);
595 return 0;
596}
597
598static int cfspi_close(struct net_device *dev)
599{
600 netif_stop_queue(dev);
601 return 0;
602}
603static const struct net_device_ops cfspi_ops = {
604 .ndo_open = cfspi_open,
605 .ndo_stop = cfspi_close,
606 .ndo_start_xmit = cfspi_xmit
607};
608
609static void cfspi_setup(struct net_device *dev)
610{
611 struct cfspi *cfspi = netdev_priv(dev);
612 dev->features = 0;
613 dev->netdev_ops = &cfspi_ops;
614 dev->type = ARPHRD_CAIF;
615 dev->flags = IFF_NOARP | IFF_POINTOPOINT;
616 dev->tx_queue_len = 0;
617 dev->mtu = SPI_MAX_PAYLOAD_SIZE;
618 dev->destructor = free_netdev;
619 skb_queue_head_init(&cfspi->qhead);
620 skb_queue_head_init(&cfspi->chead);
621 cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
622 cfspi->cfdev.use_frag = false;
623 cfspi->cfdev.use_stx = false;
624 cfspi->cfdev.use_fcs = false;
625 cfspi->ndev = dev;
626}
627
628int cfspi_spi_probe(struct platform_device *pdev)
629{
630 struct cfspi *cfspi = NULL;
631 struct net_device *ndev;
632 struct cfspi_dev *dev;
633 int res;
634 dev = (struct cfspi_dev *)pdev->dev.platform_data;
635
636 ndev = alloc_netdev(sizeof(struct cfspi),
637 "cfspi%d", cfspi_setup);
Vasiliy Kulikov8ea91222010-11-18 10:35:58 -0800638 if (!ndev)
639 return -ENOMEM;
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700640
641 cfspi = netdev_priv(ndev);
642 netif_stop_queue(ndev);
643 cfspi->ndev = ndev;
644 cfspi->pdev = pdev;
645
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000646 /* Set flow info. */
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700647 cfspi->flow_off_sent = 0;
648 cfspi->qd_low_mark = LOW_WATER_MARK;
649 cfspi->qd_high_mark = HIGH_WATER_MARK;
650
Sjur Brændeland2c24a5d2010-11-01 11:52:48 +0000651 /* Set slave info. */
652 if (!strncmp(cfspi_spi_driver.driver.name, "cfspi_sspi", 10)) {
653 cfspi->slave = true;
654 cfspi->slave_talked = false;
655 } else {
656 cfspi->slave = false;
657 cfspi->slave_talked = false;
658 }
659
Sjur Braendeland529d6da2010-06-29 00:08:21 -0700660 /* Assign the SPI device. */
661 cfspi->dev = dev;
662 /* Assign the device ifc to this SPI interface. */
663 dev->ifc = &cfspi->ifc;
664
665 /* Allocate DMA buffers. */
666 cfspi->xfer.va_tx = dma_alloc(&cfspi->xfer.pa_tx);
667 if (!cfspi->xfer.va_tx) {
668 printk(KERN_WARNING
669 "CFSPI: failed to allocate dma TX buffer.\n");
670 res = -ENODEV;
671 goto err_dma_alloc_tx;
672 }
673
674 cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx);
675
676 if (!cfspi->xfer.va_rx) {
677 printk(KERN_WARNING
678 "CFSPI: failed to allocate dma TX buffer.\n");
679 res = -ENODEV;
680 goto err_dma_alloc_rx;
681 }
682
683 /* Initialize the work queue. */
684 INIT_WORK(&cfspi->work, cfspi_xfer);
685
686 /* Initialize spin locks. */
687 spin_lock_init(&cfspi->lock);
688
689 /* Initialize flow control state. */
690 cfspi->flow_stop = false;
691
692 /* Initialize wait queue. */
693 init_waitqueue_head(&cfspi->wait);
694
695 /* Create work thread. */
696 cfspi->wq = create_singlethread_workqueue(dev->name);
697 if (!cfspi->wq) {
698 printk(KERN_WARNING "CFSPI: failed to create work queue.\n");
699 res = -ENODEV;
700 goto err_create_wq;
701 }
702
703 /* Initialize work queue. */
704 init_completion(&cfspi->comp);
705
706 /* Create debugfs entries. */
707 dev_debugfs_add(cfspi);
708
709 /* Set up the ifc. */
710 cfspi->ifc.ss_cb = cfspi_ss_cb;
711 cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb;
712 cfspi->ifc.priv = cfspi;
713
714 /* Add CAIF SPI device to list. */
715 spin_lock(&cfspi_list_lock);
716 list_add_tail(&cfspi->list, &cfspi_list);
717 spin_unlock(&cfspi_list_lock);
718
719 /* Schedule the work queue. */
720 queue_work(cfspi->wq, &cfspi->work);
721
722 /* Register network device. */
723 res = register_netdev(ndev);
724 if (res) {
725 printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res);
726 goto err_net_reg;
727 }
728 return res;
729
730 err_net_reg:
731 dev_debugfs_rem(cfspi);
732 set_bit(SPI_TERMINATE, &cfspi->state);
733 wake_up_interruptible(&cfspi->wait);
734 destroy_workqueue(cfspi->wq);
735 err_create_wq:
736 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
737 err_dma_alloc_rx:
738 dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
739 err_dma_alloc_tx:
740 free_netdev(ndev);
741
742 return res;
743}
744
745int cfspi_spi_remove(struct platform_device *pdev)
746{
747 struct list_head *list_node;
748 struct list_head *n;
749 struct cfspi *cfspi = NULL;
750 struct cfspi_dev *dev;
751
752 dev = (struct cfspi_dev *)pdev->dev.platform_data;
753 spin_lock(&cfspi_list_lock);
754 list_for_each_safe(list_node, n, &cfspi_list) {
755 cfspi = list_entry(list_node, struct cfspi, list);
756 /* Find the corresponding device. */
757 if (cfspi->dev == dev) {
758 /* Remove from list. */
759 list_del(list_node);
760 /* Free DMA buffers. */
761 dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
762 dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx);
763 set_bit(SPI_TERMINATE, &cfspi->state);
764 wake_up_interruptible(&cfspi->wait);
765 destroy_workqueue(cfspi->wq);
766 /* Destroy debugfs directory and files. */
767 dev_debugfs_rem(cfspi);
768 unregister_netdev(cfspi->ndev);
769 spin_unlock(&cfspi_list_lock);
770 return 0;
771 }
772 }
773 spin_unlock(&cfspi_list_lock);
774 return -ENODEV;
775}
776
777static void __exit cfspi_exit_module(void)
778{
779 struct list_head *list_node;
780 struct list_head *n;
781 struct cfspi *cfspi = NULL;
782
783 list_for_each_safe(list_node, n, &cfspi_list) {
784 cfspi = list_entry(list_node, struct cfspi, list);
785 platform_device_unregister(cfspi->pdev);
786 }
787
788 /* Destroy sysfs files. */
789 driver_remove_file(&cfspi_spi_driver.driver,
790 &driver_attr_up_head_align);
791 driver_remove_file(&cfspi_spi_driver.driver,
792 &driver_attr_up_tail_align);
793 driver_remove_file(&cfspi_spi_driver.driver,
794 &driver_attr_down_head_align);
795 driver_remove_file(&cfspi_spi_driver.driver,
796 &driver_attr_down_tail_align);
797 driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align);
798 /* Unregister platform driver. */
799 platform_driver_unregister(&cfspi_spi_driver);
800 /* Destroy debugfs root directory. */
801 driver_debugfs_remove();
802}
803
804static int __init cfspi_init_module(void)
805{
806 int result;
807
808 /* Initialize spin lock. */
809 spin_lock_init(&cfspi_list_lock);
810
811 /* Register platform driver. */
812 result = platform_driver_register(&cfspi_spi_driver);
813 if (result) {
814 printk(KERN_ERR "Could not register platform SPI driver.\n");
815 goto err_dev_register;
816 }
817
818 /* Create sysfs files. */
819 result =
820 driver_create_file(&cfspi_spi_driver.driver,
821 &driver_attr_up_head_align);
822 if (result) {
823 printk(KERN_ERR "Sysfs creation failed 1.\n");
824 goto err_create_up_head_align;
825 }
826
827 result =
828 driver_create_file(&cfspi_spi_driver.driver,
829 &driver_attr_up_tail_align);
830 if (result) {
831 printk(KERN_ERR "Sysfs creation failed 2.\n");
832 goto err_create_up_tail_align;
833 }
834
835 result =
836 driver_create_file(&cfspi_spi_driver.driver,
837 &driver_attr_down_head_align);
838 if (result) {
839 printk(KERN_ERR "Sysfs creation failed 3.\n");
840 goto err_create_down_head_align;
841 }
842
843 result =
844 driver_create_file(&cfspi_spi_driver.driver,
845 &driver_attr_down_tail_align);
846 if (result) {
847 printk(KERN_ERR "Sysfs creation failed 4.\n");
848 goto err_create_down_tail_align;
849 }
850
851 result =
852 driver_create_file(&cfspi_spi_driver.driver,
853 &driver_attr_frame_align);
854 if (result) {
855 printk(KERN_ERR "Sysfs creation failed 5.\n");
856 goto err_create_frame_align;
857 }
858 driver_debugfs_create();
859 return result;
860
861 err_create_frame_align:
862 driver_remove_file(&cfspi_spi_driver.driver,
863 &driver_attr_down_tail_align);
864 err_create_down_tail_align:
865 driver_remove_file(&cfspi_spi_driver.driver,
866 &driver_attr_down_head_align);
867 err_create_down_head_align:
868 driver_remove_file(&cfspi_spi_driver.driver,
869 &driver_attr_up_tail_align);
870 err_create_up_tail_align:
871 driver_remove_file(&cfspi_spi_driver.driver,
872 &driver_attr_up_head_align);
873 err_create_up_head_align:
874 err_dev_register:
875 return result;
876}
877
878module_init(cfspi_init_module);
879module_exit(cfspi_exit_module);