1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2021 in-tech smart charging GmbH
3  *
4  * driver is based on micrel/ks8851_spi.c
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/cache.h>
16 #include <linux/debugfs.h>
17 #include <linux/seq_file.h>
18 
19 #include <linux/spi/spi.h>
20 #include <linux/of_net.h>
21 
22 #define MSG_DEFAULT	(NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
23 			 NETIF_MSG_TIMER)
24 
25 #define DRV_NAME	"mse102x"
26 
27 #define DET_CMD		0x0001
28 #define DET_SOF		0x0002
29 #define DET_DFT		0x55AA
30 
31 #define CMD_SHIFT	12
32 #define CMD_RTS		(0x1 << CMD_SHIFT)
33 #define CMD_CTR		(0x2 << CMD_SHIFT)
34 
35 #define CMD_MASK	GENMASK(15, CMD_SHIFT)
36 #define LEN_MASK	GENMASK(CMD_SHIFT - 1, 0)
37 
38 #define DET_CMD_LEN	4
39 #define DET_SOF_LEN	2
40 #define DET_DFT_LEN	2
41 
42 #define MIN_FREQ_HZ	6000000
43 #define MAX_FREQ_HZ	7142857
44 
45 struct mse102x_stats {
46 	u64 xfer_err;
47 	u64 invalid_cmd;
48 	u64 invalid_ctr;
49 	u64 invalid_dft;
50 	u64 invalid_len;
51 	u64 invalid_rts;
52 	u64 invalid_sof;
53 	u64 tx_timeout;
54 };
55 
56 static const char mse102x_gstrings_stats[][ETH_GSTRING_LEN] = {
57 	"SPI transfer errors",
58 	"Invalid command",
59 	"Invalid CTR",
60 	"Invalid DFT",
61 	"Invalid frame length",
62 	"Invalid RTS",
63 	"Invalid SOF",
64 	"TX timeout",
65 };
66 
67 struct mse102x_net {
68 	struct net_device	*ndev;
69 
70 	u8			rxd[8];
71 	u8			txd[8];
72 
73 	u32			msg_enable ____cacheline_aligned;
74 
75 	struct sk_buff_head	txq;
76 	struct mse102x_stats	stats;
77 };
78 
79 struct mse102x_net_spi {
80 	struct mse102x_net	mse102x;
81 	struct mutex		lock;		/* Protect SPI frame transfer */
82 	struct work_struct	tx_work;
83 	struct spi_device	*spidev;
84 	struct spi_message	spi_msg;
85 	struct spi_transfer	spi_xfer;
86 
87 #ifdef CONFIG_DEBUG_FS
88 	struct dentry		*device_root;
89 #endif
90 };
91 
92 #define to_mse102x_spi(mse) container_of((mse), struct mse102x_net_spi, mse102x)
93 
94 #ifdef CONFIG_DEBUG_FS
95 
96 static int mse102x_info_show(struct seq_file *s, void *what)
97 {
98 	struct mse102x_net_spi *mses = s->private;
99 
100 	seq_printf(s, "TX ring size        : %u\n",
101 		   skb_queue_len(&mses->mse102x.txq));
102 
103 	seq_printf(s, "IRQ                 : %d\n",
104 		   mses->spidev->irq);
105 
106 	seq_printf(s, "SPI effective speed : %lu\n",
107 		   (unsigned long)mses->spi_xfer.effective_speed_hz);
108 	seq_printf(s, "SPI mode            : %x\n",
109 		   mses->spidev->mode);
110 
111 	return 0;
112 }
113 DEFINE_SHOW_ATTRIBUTE(mse102x_info);
114 
115 static void mse102x_init_device_debugfs(struct mse102x_net_spi *mses)
116 {
117 	mses->device_root = debugfs_create_dir(dev_name(&mses->mse102x.ndev->dev),
118 					       NULL);
119 
120 	debugfs_create_file("info", S_IFREG | 0444, mses->device_root, mses,
121 			    &mse102x_info_fops);
122 }
123 
124 static void mse102x_remove_device_debugfs(struct mse102x_net_spi *mses)
125 {
126 	debugfs_remove_recursive(mses->device_root);
127 }
128 
129 #else /* CONFIG_DEBUG_FS */
130 
131 static void mse102x_init_device_debugfs(struct mse102x_net_spi *mses)
132 {
133 }
134 
135 static void mse102x_remove_device_debugfs(struct mse102x_net_spi *mses)
136 {
137 }
138 
139 #endif
140 
141 /* SPI register read/write calls.
142  *
143  * All these calls issue SPI transactions to access the chip's registers. They
144  * all require that the necessary lock is held to prevent accesses when the
145  * chip is busy transferring packet data.
146  */
147 
148 static void mse102x_tx_cmd_spi(struct mse102x_net *mse, u16 cmd)
149 {
150 	struct mse102x_net_spi *mses = to_mse102x_spi(mse);
151 	struct spi_transfer *xfer = &mses->spi_xfer;
152 	struct spi_message *msg = &mses->spi_msg;
153 	__be16 txb[2];
154 	int ret;
155 
156 	txb[0] = cpu_to_be16(DET_CMD);
157 	txb[1] = cpu_to_be16(cmd);
158 
159 	xfer->tx_buf = txb;
160 	xfer->rx_buf = NULL;
161 	xfer->len = DET_CMD_LEN;
162 
163 	ret = spi_sync(mses->spidev, msg);
164 	if (ret < 0) {
165 		netdev_err(mse->ndev, "%s: spi_sync() failed: %d\n",
166 			   __func__, ret);
167 		mse->stats.xfer_err++;
168 	}
169 }
170 
171 static int mse102x_rx_cmd_spi(struct mse102x_net *mse, u8 *rxb)
172 {
173 	struct mse102x_net_spi *mses = to_mse102x_spi(mse);
174 	struct spi_transfer *xfer = &mses->spi_xfer;
175 	struct spi_message *msg = &mses->spi_msg;
176 	__be16 *txb = (__be16 *)mse->txd;
177 	__be16 *cmd = (__be16 *)mse->rxd;
178 	u8 *trx = mse->rxd;
179 	int ret;
180 
181 	txb[0] = 0;
182 	txb[1] = 0;
183 
184 	xfer->tx_buf = txb;
185 	xfer->rx_buf = trx;
186 	xfer->len = DET_CMD_LEN;
187 
188 	ret = spi_sync(mses->spidev, msg);
189 	if (ret < 0) {
190 		netdev_err(mse->ndev, "%s: spi_sync() failed: %d\n",
191 			   __func__, ret);
192 		mse->stats.xfer_err++;
193 	} else if (*cmd != cpu_to_be16(DET_CMD)) {
194 		net_dbg_ratelimited("%s: Unexpected response (0x%04x)\n",
195 				    __func__, *cmd);
196 		mse->stats.invalid_cmd++;
197 		ret = -EIO;
198 	} else {
199 		memcpy(rxb, trx + 2, 2);
200 	}
201 
202 	return ret;
203 }
204 
205 static inline void mse102x_push_header(struct sk_buff *skb)
206 {
207 	__be16 *header = skb_push(skb, DET_SOF_LEN);
208 
209 	*header = cpu_to_be16(DET_SOF);
210 }
211 
212 static inline void mse102x_put_footer(struct sk_buff *skb)
213 {
214 	__be16 *footer = skb_put(skb, DET_DFT_LEN);
215 
216 	*footer = cpu_to_be16(DET_DFT);
217 }
218 
219 static int mse102x_tx_frame_spi(struct mse102x_net *mse, struct sk_buff *txp,
220 				unsigned int pad)
221 {
222 	struct mse102x_net_spi *mses = to_mse102x_spi(mse);
223 	struct spi_transfer *xfer = &mses->spi_xfer;
224 	struct spi_message *msg = &mses->spi_msg;
225 	struct sk_buff *tskb = NULL;
226 	int ret;
227 
228 	netif_dbg(mse, tx_queued, mse->ndev, "%s: skb %p, %d@%p\n",
229 		  __func__, txp, txp->len, txp->data);
230 
231 	if ((skb_headroom(txp) < DET_SOF_LEN) ||
232 	    (skb_tailroom(txp) < DET_DFT_LEN + pad)) {
233 		tskb = skb_copy_expand(txp, DET_SOF_LEN, DET_DFT_LEN + pad,
234 				       GFP_KERNEL);
235 		if (!tskb)
236 			return -ENOMEM;
237 
238 		txp = tskb;
239 	}
240 
241 	mse102x_push_header(txp);
242 
243 	if (pad)
244 		skb_put_zero(txp, pad);
245 
246 	mse102x_put_footer(txp);
247 
248 	xfer->tx_buf = txp->data;
249 	xfer->rx_buf = NULL;
250 	xfer->len = txp->len;
251 
252 	ret = spi_sync(mses->spidev, msg);
253 	if (ret < 0) {
254 		netdev_err(mse->ndev, "%s: spi_sync() failed: %d\n",
255 			   __func__, ret);
256 		mse->stats.xfer_err++;
257 	}
258 
259 	dev_kfree_skb(tskb);
260 
261 	return ret;
262 }
263 
264 static int mse102x_rx_frame_spi(struct mse102x_net *mse, u8 *buff,
265 				unsigned int frame_len)
266 {
267 	struct mse102x_net_spi *mses = to_mse102x_spi(mse);
268 	struct spi_transfer *xfer = &mses->spi_xfer;
269 	struct spi_message *msg = &mses->spi_msg;
270 	__be16 *sof = (__be16 *)buff;
271 	__be16 *dft = (__be16 *)(buff + DET_SOF_LEN + frame_len);
272 	int ret;
273 
274 	xfer->rx_buf = buff;
275 	xfer->tx_buf = NULL;
276 	xfer->len = DET_SOF_LEN + frame_len + DET_DFT_LEN;
277 
278 	ret = spi_sync(mses->spidev, msg);
279 	if (ret < 0) {
280 		netdev_err(mse->ndev, "%s: spi_sync() failed: %d\n",
281 			   __func__, ret);
282 		mse->stats.xfer_err++;
283 	} else if (*sof != cpu_to_be16(DET_SOF)) {
284 		netdev_dbg(mse->ndev, "%s: SPI start of frame is invalid (0x%04x)\n",
285 			   __func__, *sof);
286 		mse->stats.invalid_sof++;
287 		ret = -EIO;
288 	} else if (*dft != cpu_to_be16(DET_DFT)) {
289 		netdev_dbg(mse->ndev, "%s: SPI frame tail is invalid (0x%04x)\n",
290 			   __func__, *dft);
291 		mse->stats.invalid_dft++;
292 		ret = -EIO;
293 	}
294 
295 	return ret;
296 }
297 
298 static void mse102x_dump_packet(const char *msg, int len, const char *data)
299 {
300 	printk(KERN_DEBUG ": %s - packet len:%d\n", msg, len);
301 	print_hex_dump(KERN_DEBUG, "pk data: ", DUMP_PREFIX_OFFSET, 16, 1,
302 		       data, len, true);
303 }
304 
305 static void mse102x_rx_pkt_spi(struct mse102x_net *mse)
306 {
307 	struct sk_buff *skb;
308 	unsigned int rxalign;
309 	unsigned int rxlen;
310 	__be16 rx = 0;
311 	u16 cmd_resp;
312 	u8 *rxpkt;
313 	int ret;
314 
315 	mse102x_tx_cmd_spi(mse, CMD_CTR);
316 	ret = mse102x_rx_cmd_spi(mse, (u8 *)&rx);
317 	cmd_resp = be16_to_cpu(rx);
318 
319 	if (ret || ((cmd_resp & CMD_MASK) != CMD_RTS)) {
320 		usleep_range(50, 100);
321 
322 		mse102x_tx_cmd_spi(mse, CMD_CTR);
323 		ret = mse102x_rx_cmd_spi(mse, (u8 *)&rx);
324 		if (ret)
325 			return;
326 
327 		cmd_resp = be16_to_cpu(rx);
328 		if ((cmd_resp & CMD_MASK) != CMD_RTS) {
329 			net_dbg_ratelimited("%s: Unexpected response (0x%04x)\n",
330 					    __func__, cmd_resp);
331 			mse->stats.invalid_rts++;
332 			return;
333 		}
334 
335 		net_dbg_ratelimited("%s: Unexpected response to first CMD\n",
336 				    __func__);
337 	}
338 
339 	rxlen = cmd_resp & LEN_MASK;
340 	if (!rxlen) {
341 		net_dbg_ratelimited("%s: No frame length defined\n", __func__);
342 		mse->stats.invalid_len++;
343 		return;
344 	}
345 
346 	rxalign = ALIGN(rxlen + DET_SOF_LEN + DET_DFT_LEN, 4);
347 	skb = netdev_alloc_skb_ip_align(mse->ndev, rxalign);
348 	if (!skb)
349 		return;
350 
351 	/* 2 bytes Start of frame (before ethernet header)
352 	 * 2 bytes Data frame tail (after ethernet frame)
353 	 * They are copied, but ignored.
354 	 */
355 	rxpkt = skb_put(skb, rxlen) - DET_SOF_LEN;
356 	if (mse102x_rx_frame_spi(mse, rxpkt, rxlen)) {
357 		mse->ndev->stats.rx_errors++;
358 		dev_kfree_skb(skb);
359 		return;
360 	}
361 
362 	if (netif_msg_pktdata(mse))
363 		mse102x_dump_packet(__func__, skb->len, skb->data);
364 
365 	skb->protocol = eth_type_trans(skb, mse->ndev);
366 	netif_rx(skb);
367 
368 	mse->ndev->stats.rx_packets++;
369 	mse->ndev->stats.rx_bytes += rxlen;
370 }
371 
372 static int mse102x_tx_pkt_spi(struct mse102x_net *mse, struct sk_buff *txb,
373 			      unsigned long work_timeout)
374 {
375 	unsigned int pad = 0;
376 	__be16 rx = 0;
377 	u16 cmd_resp;
378 	int ret;
379 	bool first = true;
380 
381 	if (txb->len < 60)
382 		pad = 60 - txb->len;
383 
384 	while (1) {
385 		mse102x_tx_cmd_spi(mse, CMD_RTS | (txb->len + pad));
386 		ret = mse102x_rx_cmd_spi(mse, (u8 *)&rx);
387 		cmd_resp = be16_to_cpu(rx);
388 
389 		if (!ret) {
390 			/* ready to send frame ? */
391 			if (cmd_resp == CMD_CTR)
392 				break;
393 
394 			net_dbg_ratelimited("%s: Unexpected response (0x%04x)\n",
395 					    __func__, cmd_resp);
396 			mse->stats.invalid_ctr++;
397 		}
398 
399 		/* It's not predictable how long / many retries it takes to
400 		 * send at least one packet, so TX timeouts are possible.
401 		 * That's the reason why the netdev watchdog is not used here.
402 		 */
403 		if (time_after(jiffies, work_timeout))
404 			return -ETIMEDOUT;
405 
406 		if (first) {
407 			/* throttle at first issue */
408 			netif_stop_queue(mse->ndev);
409 			/* fast retry */
410 			usleep_range(50, 100);
411 			first = false;
412 		} else {
413 			msleep(20);
414 		}
415 	}
416 
417 	ret = mse102x_tx_frame_spi(mse, txb, pad);
418 	if (ret)
419 		net_dbg_ratelimited("%s: Failed to send (%d), drop frame\n",
420 				    __func__, ret);
421 
422 	return ret;
423 }
424 
425 #define TX_QUEUE_MAX 10
426 
427 static void mse102x_tx_work(struct work_struct *work)
428 {
429 	/* Make sure timeout is sufficient to transfer TX_QUEUE_MAX frames */
430 	unsigned long work_timeout = jiffies + msecs_to_jiffies(1000);
431 	struct mse102x_net_spi *mses;
432 	struct mse102x_net *mse;
433 	struct sk_buff *txb;
434 	int ret = 0;
435 
436 	mses = container_of(work, struct mse102x_net_spi, tx_work);
437 	mse = &mses->mse102x;
438 
439 	while ((txb = skb_dequeue(&mse->txq))) {
440 		mutex_lock(&mses->lock);
441 		ret = mse102x_tx_pkt_spi(mse, txb, work_timeout);
442 		mutex_unlock(&mses->lock);
443 		if (ret) {
444 			mse->ndev->stats.tx_dropped++;
445 		} else {
446 			mse->ndev->stats.tx_bytes += txb->len;
447 			mse->ndev->stats.tx_packets++;
448 		}
449 
450 		dev_kfree_skb(txb);
451 	}
452 
453 	if (ret == -ETIMEDOUT) {
454 		if (netif_msg_timer(mse))
455 			netdev_err(mse->ndev, "tx work timeout\n");
456 
457 		mse->stats.tx_timeout++;
458 	}
459 
460 	netif_wake_queue(mse->ndev);
461 }
462 
463 static netdev_tx_t mse102x_start_xmit_spi(struct sk_buff *skb,
464 					  struct net_device *ndev)
465 {
466 	struct mse102x_net *mse = netdev_priv(ndev);
467 	struct mse102x_net_spi *mses = to_mse102x_spi(mse);
468 
469 	netif_dbg(mse, tx_queued, ndev,
470 		  "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data);
471 
472 	skb_queue_tail(&mse->txq, skb);
473 
474 	if (skb_queue_len(&mse->txq) >= TX_QUEUE_MAX)
475 		netif_stop_queue(ndev);
476 
477 	schedule_work(&mses->tx_work);
478 
479 	return NETDEV_TX_OK;
480 }
481 
482 static void mse102x_init_mac(struct mse102x_net *mse, struct device_node *np)
483 {
484 	struct net_device *ndev = mse->ndev;
485 	int ret = of_get_ethdev_address(np, ndev);
486 
487 	if (ret) {
488 		eth_hw_addr_random(ndev);
489 		netdev_err(ndev, "Using random MAC address: %pM\n",
490 			   ndev->dev_addr);
491 	}
492 }
493 
494 /* Assumption: this is called for every incoming packet */
495 static irqreturn_t mse102x_irq(int irq, void *_mse)
496 {
497 	struct mse102x_net *mse = _mse;
498 	struct mse102x_net_spi *mses = to_mse102x_spi(mse);
499 
500 	mutex_lock(&mses->lock);
501 	mse102x_rx_pkt_spi(mse);
502 	mutex_unlock(&mses->lock);
503 
504 	return IRQ_HANDLED;
505 }
506 
507 static int mse102x_net_open(struct net_device *ndev)
508 {
509 	struct mse102x_net *mse = netdev_priv(ndev);
510 	int ret;
511 
512 	ret = request_threaded_irq(ndev->irq, NULL, mse102x_irq, IRQF_ONESHOT,
513 				   ndev->name, mse);
514 	if (ret < 0) {
515 		netdev_err(ndev, "Failed to get irq: %d\n", ret);
516 		return ret;
517 	}
518 
519 	netif_dbg(mse, ifup, ndev, "opening\n");
520 
521 	netif_start_queue(ndev);
522 
523 	netif_carrier_on(ndev);
524 
525 	netif_dbg(mse, ifup, ndev, "network device up\n");
526 
527 	return 0;
528 }
529 
530 static int mse102x_net_stop(struct net_device *ndev)
531 {
532 	struct mse102x_net *mse = netdev_priv(ndev);
533 	struct mse102x_net_spi *mses = to_mse102x_spi(mse);
534 
535 	netif_info(mse, ifdown, ndev, "shutting down\n");
536 
537 	netif_carrier_off(mse->ndev);
538 
539 	/* stop any outstanding work */
540 	flush_work(&mses->tx_work);
541 
542 	netif_stop_queue(ndev);
543 
544 	skb_queue_purge(&mse->txq);
545 
546 	free_irq(ndev->irq, mse);
547 
548 	return 0;
549 }
550 
551 static const struct net_device_ops mse102x_netdev_ops = {
552 	.ndo_open		= mse102x_net_open,
553 	.ndo_stop		= mse102x_net_stop,
554 	.ndo_start_xmit		= mse102x_start_xmit_spi,
555 	.ndo_set_mac_address	= eth_mac_addr,
556 	.ndo_validate_addr	= eth_validate_addr,
557 };
558 
559 /* ethtool support */
560 
561 static void mse102x_get_drvinfo(struct net_device *ndev,
562 				struct ethtool_drvinfo *di)
563 {
564 	strscpy(di->driver, DRV_NAME, sizeof(di->driver));
565 	strscpy(di->bus_info, dev_name(ndev->dev.parent), sizeof(di->bus_info));
566 }
567 
568 static u32 mse102x_get_msglevel(struct net_device *ndev)
569 {
570 	struct mse102x_net *mse = netdev_priv(ndev);
571 
572 	return mse->msg_enable;
573 }
574 
575 static void mse102x_set_msglevel(struct net_device *ndev, u32 to)
576 {
577 	struct mse102x_net *mse = netdev_priv(ndev);
578 
579 	mse->msg_enable = to;
580 }
581 
582 static void mse102x_get_ethtool_stats(struct net_device *ndev,
583 				      struct ethtool_stats *estats, u64 *data)
584 {
585 	struct mse102x_net *mse = netdev_priv(ndev);
586 	struct mse102x_stats *st = &mse->stats;
587 
588 	memcpy(data, st, ARRAY_SIZE(mse102x_gstrings_stats) * sizeof(u64));
589 }
590 
591 static void mse102x_get_strings(struct net_device *ndev, u32 stringset, u8 *buf)
592 {
593 	switch (stringset) {
594 	case ETH_SS_STATS:
595 		memcpy(buf, &mse102x_gstrings_stats,
596 		       sizeof(mse102x_gstrings_stats));
597 		break;
598 	default:
599 		WARN_ON(1);
600 		break;
601 	}
602 }
603 
604 static int mse102x_get_sset_count(struct net_device *ndev, int sset)
605 {
606 	switch (sset) {
607 	case ETH_SS_STATS:
608 		return ARRAY_SIZE(mse102x_gstrings_stats);
609 	default:
610 		return -EINVAL;
611 	}
612 }
613 
614 static const struct ethtool_ops mse102x_ethtool_ops = {
615 	.get_drvinfo		= mse102x_get_drvinfo,
616 	.get_link		= ethtool_op_get_link,
617 	.get_msglevel		= mse102x_get_msglevel,
618 	.set_msglevel		= mse102x_set_msglevel,
619 	.get_ethtool_stats	= mse102x_get_ethtool_stats,
620 	.get_strings		= mse102x_get_strings,
621 	.get_sset_count		= mse102x_get_sset_count,
622 };
623 
624 /* driver bus management functions */
625 
626 #ifdef CONFIG_PM_SLEEP
627 
628 static int mse102x_suspend(struct device *dev)
629 {
630 	struct mse102x_net *mse = dev_get_drvdata(dev);
631 	struct net_device *ndev = mse->ndev;
632 
633 	if (netif_running(ndev)) {
634 		netif_device_detach(ndev);
635 		mse102x_net_stop(ndev);
636 	}
637 
638 	return 0;
639 }
640 
641 static int mse102x_resume(struct device *dev)
642 {
643 	struct mse102x_net *mse = dev_get_drvdata(dev);
644 	struct net_device *ndev = mse->ndev;
645 
646 	if (netif_running(ndev)) {
647 		mse102x_net_open(ndev);
648 		netif_device_attach(ndev);
649 	}
650 
651 	return 0;
652 }
653 #endif
654 
655 static SIMPLE_DEV_PM_OPS(mse102x_pm_ops, mse102x_suspend, mse102x_resume);
656 
657 static int mse102x_probe_spi(struct spi_device *spi)
658 {
659 	struct device *dev = &spi->dev;
660 	struct mse102x_net_spi *mses;
661 	struct net_device *ndev;
662 	struct mse102x_net *mse;
663 	int ret;
664 
665 	spi->bits_per_word = 8;
666 	spi->mode |= SPI_MODE_3;
667 	/* enforce minimum speed to ensure device functionality */
668 	spi->master->min_speed_hz = MIN_FREQ_HZ;
669 
670 	if (!spi->max_speed_hz)
671 		spi->max_speed_hz = MAX_FREQ_HZ;
672 
673 	if (spi->max_speed_hz < MIN_FREQ_HZ ||
674 	    spi->max_speed_hz > MAX_FREQ_HZ) {
675 		dev_err(&spi->dev, "SPI max frequency out of range (min: %u, max: %u)\n",
676 			MIN_FREQ_HZ, MAX_FREQ_HZ);
677 		return -EINVAL;
678 	}
679 
680 	ret = spi_setup(spi);
681 	if (ret < 0) {
682 		dev_err(&spi->dev, "Unable to setup SPI device: %d\n", ret);
683 		return ret;
684 	}
685 
686 	ndev = devm_alloc_etherdev(dev, sizeof(struct mse102x_net_spi));
687 	if (!ndev)
688 		return -ENOMEM;
689 
690 	ndev->needed_tailroom += ALIGN(DET_DFT_LEN, 4);
691 	ndev->needed_headroom += ALIGN(DET_SOF_LEN, 4);
692 	ndev->priv_flags &= ~IFF_TX_SKB_SHARING;
693 	ndev->tx_queue_len = 100;
694 
695 	mse = netdev_priv(ndev);
696 	mses = to_mse102x_spi(mse);
697 
698 	mses->spidev = spi;
699 	mutex_init(&mses->lock);
700 	INIT_WORK(&mses->tx_work, mse102x_tx_work);
701 
702 	/* initialise pre-made spi transfer messages */
703 	spi_message_init(&mses->spi_msg);
704 	spi_message_add_tail(&mses->spi_xfer, &mses->spi_msg);
705 
706 	ndev->irq = spi->irq;
707 	mse->ndev = ndev;
708 
709 	/* set the default message enable */
710 	mse->msg_enable = netif_msg_init(-1, MSG_DEFAULT);
711 
712 	skb_queue_head_init(&mse->txq);
713 
714 	SET_NETDEV_DEV(ndev, dev);
715 
716 	dev_set_drvdata(dev, mse);
717 
718 	netif_carrier_off(mse->ndev);
719 	ndev->netdev_ops = &mse102x_netdev_ops;
720 	ndev->ethtool_ops = &mse102x_ethtool_ops;
721 
722 	mse102x_init_mac(mse, dev->of_node);
723 
724 	ret = register_netdev(ndev);
725 	if (ret) {
726 		dev_err(dev, "failed to register network device: %d\n", ret);
727 		return ret;
728 	}
729 
730 	mse102x_init_device_debugfs(mses);
731 
732 	return 0;
733 }
734 
735 static void mse102x_remove_spi(struct spi_device *spi)
736 {
737 	struct mse102x_net *mse = dev_get_drvdata(&spi->dev);
738 	struct mse102x_net_spi *mses = to_mse102x_spi(mse);
739 
740 	if (netif_msg_drv(mse))
741 		dev_info(&spi->dev, "remove\n");
742 
743 	mse102x_remove_device_debugfs(mses);
744 	unregister_netdev(mse->ndev);
745 }
746 
747 static const struct of_device_id mse102x_match_table[] = {
748 	{ .compatible = "vertexcom,mse1021" },
749 	{ .compatible = "vertexcom,mse1022" },
750 	{ }
751 };
752 MODULE_DEVICE_TABLE(of, mse102x_match_table);
753 
754 static const struct spi_device_id mse102x_ids[] = {
755 	{ "mse1021" },
756 	{ "mse1022" },
757 	{ }
758 };
759 MODULE_DEVICE_TABLE(spi, mse102x_ids);
760 
761 static struct spi_driver mse102x_driver = {
762 	.driver = {
763 		.name = DRV_NAME,
764 		.of_match_table = mse102x_match_table,
765 		.pm = &mse102x_pm_ops,
766 	},
767 	.probe = mse102x_probe_spi,
768 	.remove = mse102x_remove_spi,
769 	.id_table = mse102x_ids,
770 };
771 module_spi_driver(mse102x_driver);
772 
773 MODULE_DESCRIPTION("MSE102x Network driver");
774 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@chargebyte.com>");
775 MODULE_LICENSE("GPL");
776 MODULE_ALIAS("spi:" DRV_NAME);
777