1
2
3
4
5
6
7
8
9
10
11
12#define DEBUG
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/ethtool.h>
19#include <linux/cache.h>
20#include <linux/crc32.h>
21#include <linux/mii.h>
22
23#include <linux/spi/spi.h>
24
25#include "ks8851.h"
26
27
28
29
30
31
32
33
34
35
36
37
38
39struct ks8851_rxctrl {
40 u16 mchash[4];
41 u16 rxcr1;
42 u16 rxcr2;
43};
44
45
46
47
48
49
50
51
52
53
54union ks8851_tx_hdr {
55 u8 txb[6];
56 __le16 txw[3];
57};
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94struct ks8851_net {
95 struct net_device *netdev;
96 struct spi_device *spidev;
97 struct mutex lock;
98 spinlock_t statelock;
99
100 union ks8851_tx_hdr txh ____cacheline_aligned;
101 u8 rxd[8];
102 u8 txd[8];
103
104 u32 msg_enable ____cacheline_aligned;
105 u16 tx_space;
106 u8 fid;
107
108 u16 rc_ier;
109 u16 rc_rxqcr;
110
111 struct mii_if_info mii;
112 struct ks8851_rxctrl rxctrl;
113
114 struct work_struct tx_work;
115 struct work_struct irq_work;
116 struct work_struct rxctrl_work;
117
118 struct sk_buff_head txq;
119
120 struct spi_message spi_msg1;
121 struct spi_message spi_msg2;
122 struct spi_transfer spi_xfer1;
123 struct spi_transfer spi_xfer2[2];
124};
125
126static int msg_enable;
127
128#define ks_info(_ks, _msg...) dev_info(&(_ks)->spidev->dev, _msg)
129#define ks_warn(_ks, _msg...) dev_warn(&(_ks)->spidev->dev, _msg)
130#define ks_dbg(_ks, _msg...) dev_dbg(&(_ks)->spidev->dev, _msg)
131#define ks_err(_ks, _msg...) dev_err(&(_ks)->spidev->dev, _msg)
132
133
134#define BYTE_EN(_x) ((_x) << 2)
135
136
137#define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val)
155{
156 struct spi_transfer *xfer = &ks->spi_xfer1;
157 struct spi_message *msg = &ks->spi_msg1;
158 __le16 txb[2];
159 int ret;
160
161 txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR);
162 txb[1] = cpu_to_le16(val);
163
164 xfer->tx_buf = txb;
165 xfer->rx_buf = NULL;
166 xfer->len = 4;
167
168 ret = spi_sync(ks->spidev, msg);
169 if (ret < 0)
170 ks_err(ks, "spi_sync() failed\n");
171}
172
173
174
175
176
177
178
179
180
181static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val)
182{
183 struct spi_transfer *xfer = &ks->spi_xfer1;
184 struct spi_message *msg = &ks->spi_msg1;
185 __le16 txb[2];
186 int ret;
187 int bit;
188
189 bit = 1 << (reg & 3);
190
191 txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR);
192 txb[1] = val;
193
194 xfer->tx_buf = txb;
195 xfer->rx_buf = NULL;
196 xfer->len = 3;
197
198 ret = spi_sync(ks->spidev, msg);
199 if (ret < 0)
200 ks_err(ks, "spi_sync() failed\n");
201}
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217static inline bool ks8851_rx_1msg(struct ks8851_net *ks)
218{
219 return true;
220}
221
222
223
224
225
226
227
228
229
230
231
232static void ks8851_rdreg(struct ks8851_net *ks, unsigned op,
233 u8 *rxb, unsigned rxl)
234{
235 struct spi_transfer *xfer;
236 struct spi_message *msg;
237 __le16 *txb = (__le16 *)ks->txd;
238 u8 *trx = ks->rxd;
239 int ret;
240
241 txb[0] = cpu_to_le16(op | KS_SPIOP_RD);
242
243 if (ks8851_rx_1msg(ks)) {
244 msg = &ks->spi_msg1;
245 xfer = &ks->spi_xfer1;
246
247 xfer->tx_buf = txb;
248 xfer->rx_buf = trx;
249 xfer->len = rxl + 2;
250 } else {
251 msg = &ks->spi_msg2;
252 xfer = ks->spi_xfer2;
253
254 xfer->tx_buf = txb;
255 xfer->rx_buf = NULL;
256 xfer->len = 2;
257
258 xfer++;
259 xfer->tx_buf = NULL;
260 xfer->rx_buf = trx;
261 xfer->len = rxl;
262 }
263
264 ret = spi_sync(ks->spidev, msg);
265 if (ret < 0)
266 ks_err(ks, "read: spi_sync() failed\n");
267 else if (ks8851_rx_1msg(ks))
268 memcpy(rxb, trx + 2, rxl);
269 else
270 memcpy(rxb, trx, rxl);
271}
272
273
274
275
276
277
278
279
280static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg)
281{
282 u8 rxb[1];
283
284 ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1);
285 return rxb[0];
286}
287
288
289
290
291
292
293
294
295static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg)
296{
297 __le16 rx = 0;
298
299 ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2);
300 return le16_to_cpu(rx);
301}
302
303
304
305
306
307
308
309
310
311
312static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg)
313{
314 __le32 rx = 0;
315
316 WARN_ON(reg & 3);
317
318 ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4);
319 return le32_to_cpu(rx);
320}
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op)
336{
337 ks8851_wrreg16(ks, KS_GRR, op);
338 mdelay(1);
339 ks8851_wrreg16(ks, KS_GRR, 0);
340 mdelay(1);
341}
342
343
344
345
346
347
348
349
350
351
352static int ks8851_write_mac_addr(struct net_device *dev)
353{
354 struct ks8851_net *ks = netdev_priv(dev);
355 int i;
356
357 mutex_lock(&ks->lock);
358
359 for (i = 0; i < ETH_ALEN; i++)
360 ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]);
361
362 mutex_unlock(&ks->lock);
363
364 return 0;
365}
366
367
368
369
370
371
372
373
374
375
376
377
378
379static void ks8851_init_mac(struct ks8851_net *ks)
380{
381 struct net_device *dev = ks->netdev;
382
383 random_ether_addr(dev->dev_addr);
384 ks8851_write_mac_addr(dev);
385}
386
387
388
389
390
391
392
393
394
395static irqreturn_t ks8851_irq(int irq, void *pw)
396{
397 struct ks8851_net *ks = pw;
398
399 disable_irq_nosync(irq);
400 schedule_work(&ks->irq_work);
401 return IRQ_HANDLED;
402}
403
404
405
406
407
408
409
410
411
412
413static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len)
414{
415 struct spi_transfer *xfer = ks->spi_xfer2;
416 struct spi_message *msg = &ks->spi_msg2;
417 u8 txb[1];
418 int ret;
419
420 if (netif_msg_rx_status(ks))
421 ks_dbg(ks, "%s: %d@%p\n", __func__, len, buff);
422
423
424 txb[0] = KS_SPIOP_RXFIFO;
425
426 xfer->tx_buf = txb;
427 xfer->rx_buf = NULL;
428 xfer->len = 1;
429
430 xfer++;
431 xfer->rx_buf = buff;
432 xfer->tx_buf = NULL;
433 xfer->len = len;
434
435 ret = spi_sync(ks->spidev, msg);
436 if (ret < 0)
437 ks_err(ks, "%s: spi_sync() failed\n", __func__);
438}
439
440
441
442
443
444
445
446
447static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt)
448{
449 ks_dbg(ks, "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
450 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
451 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
452 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
453}
454
455
456
457
458
459
460
461
462
463static void ks8851_rx_pkts(struct ks8851_net *ks)
464{
465 struct sk_buff *skb;
466 unsigned rxfc;
467 unsigned rxlen;
468 unsigned rxstat;
469 u32 rxh;
470 u8 *rxpkt;
471
472 rxfc = ks8851_rdreg8(ks, KS_RXFC);
473
474 if (netif_msg_rx_status(ks))
475 ks_dbg(ks, "%s: %d packets\n", __func__, rxfc);
476
477
478
479
480
481
482
483
484
485
486
487 for (; rxfc != 0; rxfc--) {
488 rxh = ks8851_rdreg32(ks, KS_RXFHSR);
489 rxstat = rxh & 0xffff;
490 rxlen = rxh >> 16;
491
492 if (netif_msg_rx_status(ks))
493 ks_dbg(ks, "rx: stat 0x%04x, len 0x%04x\n",
494 rxstat, rxlen);
495
496
497
498
499 ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00);
500
501
502 ks8851_wrreg16(ks, KS_RXQCR,
503 ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE);
504
505 if (rxlen > 0) {
506 skb = netdev_alloc_skb(ks->netdev, rxlen + 2 + 8);
507 if (!skb) {
508
509 }
510
511
512
513 skb_reserve(skb, 2 + 4 + 4);
514
515 rxpkt = skb_put(skb, rxlen - 4) - 8;
516
517
518
519 ks8851_rdfifo(ks, rxpkt, ALIGN(rxlen, 4) + 8);
520
521 if (netif_msg_pktdata(ks))
522 ks8851_dbg_dumpkkt(ks, rxpkt);
523
524 skb->protocol = eth_type_trans(skb, ks->netdev);
525 netif_rx(skb);
526
527 ks->netdev->stats.rx_packets++;
528 ks->netdev->stats.rx_bytes += rxlen - 4;
529 }
530
531 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
532 }
533}
534
535
536
537
538
539
540
541
542
543
544
545
546static void ks8851_irq_work(struct work_struct *work)
547{
548 struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work);
549 unsigned status;
550 unsigned handled = 0;
551
552 mutex_lock(&ks->lock);
553
554 status = ks8851_rdreg16(ks, KS_ISR);
555
556 if (netif_msg_intr(ks))
557 dev_dbg(&ks->spidev->dev, "%s: status 0x%04x\n",
558 __func__, status);
559
560 if (status & IRQ_LCI) {
561
562 handled |= IRQ_LCI;
563 }
564
565 if (status & IRQ_LDI) {
566 u16 pmecr = ks8851_rdreg16(ks, KS_PMECR);
567 pmecr &= ~PMECR_WKEVT_MASK;
568 ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
569
570 handled |= IRQ_LDI;
571 }
572
573 if (status & IRQ_RXPSI)
574 handled |= IRQ_RXPSI;
575
576 if (status & IRQ_TXI) {
577 handled |= IRQ_TXI;
578
579
580
581
582
583 ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR);
584
585 if (netif_msg_intr(ks))
586 ks_dbg(ks, "%s: txspace %d\n", __func__, ks->tx_space);
587 }
588
589 if (status & IRQ_RXI)
590 handled |= IRQ_RXI;
591
592 if (status & IRQ_SPIBEI) {
593 dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__);
594 handled |= IRQ_SPIBEI;
595 }
596
597 ks8851_wrreg16(ks, KS_ISR, handled);
598
599 if (status & IRQ_RXI) {
600
601
602
603
604
605 ks8851_rx_pkts(ks);
606 }
607
608
609
610
611 if (status & IRQ_RXPSI) {
612 struct ks8851_rxctrl *rxc = &ks->rxctrl;
613
614
615 ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]);
616 ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]);
617 ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]);
618 ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]);
619
620 ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2);
621 ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
622 }
623
624 mutex_unlock(&ks->lock);
625
626 if (status & IRQ_TXI)
627 netif_wake_queue(ks->netdev);
628
629 enable_irq(ks->netdev->irq);
630}
631
632
633
634
635
636
637
638
639static inline unsigned calc_txlen(unsigned len)
640{
641 return ALIGN(len + 4, 4);
642}
643
644
645
646
647
648
649
650
651
652
653
654
655static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq)
656{
657 struct spi_transfer *xfer = ks->spi_xfer2;
658 struct spi_message *msg = &ks->spi_msg2;
659 unsigned fid = 0;
660 int ret;
661
662 if (netif_msg_tx_queued(ks))
663 dev_dbg(&ks->spidev->dev, "%s: skb %p, %d@%p, irq %d\n",
664 __func__, txp, txp->len, txp->data, irq);
665
666 fid = ks->fid++;
667 fid &= TXFR_TXFID_MASK;
668
669 if (irq)
670 fid |= TXFR_TXIC;
671
672
673 ks->txh.txb[1] = KS_SPIOP_TXFIFO;
674 ks->txh.txw[1] = cpu_to_le16(fid);
675 ks->txh.txw[2] = cpu_to_le16(txp->len);
676
677 xfer->tx_buf = &ks->txh.txb[1];
678 xfer->rx_buf = NULL;
679 xfer->len = 5;
680
681 xfer++;
682 xfer->tx_buf = txp->data;
683 xfer->rx_buf = NULL;
684 xfer->len = ALIGN(txp->len, 4);
685
686 ret = spi_sync(ks->spidev, msg);
687 if (ret < 0)
688 ks_err(ks, "%s: spi_sync() failed\n", __func__);
689}
690
691
692
693
694
695
696static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
697{
698 struct net_device *dev = ks->netdev;
699
700 dev->stats.tx_bytes += txb->len;
701 dev->stats.tx_packets++;
702
703 dev_kfree_skb(txb);
704}
705
706
707
708
709
710
711
712
713static void ks8851_tx_work(struct work_struct *work)
714{
715 struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
716 struct sk_buff *txb;
717 bool last = false;
718
719 mutex_lock(&ks->lock);
720
721 while (!last) {
722 txb = skb_dequeue(&ks->txq);
723 last = skb_queue_empty(&ks->txq);
724
725 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
726 ks8851_wrpkt(ks, txb, last);
727 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
728 ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
729
730 ks8851_done_tx(ks, txb);
731 }
732
733 mutex_unlock(&ks->lock);
734}
735
736
737
738
739
740
741
742
743static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
744{
745 unsigned pmecr;
746
747 if (netif_msg_hw(ks))
748 ks_dbg(ks, "setting power mode %d\n", pwrmode);
749
750 pmecr = ks8851_rdreg16(ks, KS_PMECR);
751 pmecr &= ~PMECR_PM_MASK;
752 pmecr |= pwrmode;
753
754 ks8851_wrreg16(ks, KS_PMECR, pmecr);
755}
756
757
758
759
760
761
762
763
764static int ks8851_net_open(struct net_device *dev)
765{
766 struct ks8851_net *ks = netdev_priv(dev);
767
768
769
770 mutex_lock(&ks->lock);
771
772 if (netif_msg_ifup(ks))
773 ks_dbg(ks, "opening %s\n", dev->name);
774
775
776 ks8851_set_powermode(ks, PMECR_PM_NORMAL);
777
778
779
780 ks8851_soft_reset(ks, GRR_QMU);
781
782
783
784 ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE |
785 TXCR_TXPE |
786 TXCR_TXCRC |
787 TXCR_TXFCE));
788
789
790 ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
791
792
793
794 ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA |
795 RXCR1_RXFCE |
796 RXCR1_RXBE |
797 RXCR1_RXUE |
798 RXCR1_RXE));
799
800
801 ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME);
802
803
804 ks8851_wrreg16(ks, KS_RXDTTR, 1000);
805 ks8851_wrreg16(ks, KS_RXDBCTR, 4096);
806 ks8851_wrreg16(ks, KS_RXFCTR, 10);
807
808 ks->rc_rxqcr = (RXQCR_RXFCTE |
809 RXQCR_RXDBCTE |
810 RXQCR_RXDTTE);
811
812 ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
813
814
815
816#define STD_IRQ (IRQ_LCI | \
817 IRQ_TXI | \
818 IRQ_RXI | \
819 IRQ_SPIBEI | \
820 IRQ_TXPSI | \
821 IRQ_RXPSI)
822
823 ks->rc_ier = STD_IRQ;
824 ks8851_wrreg16(ks, KS_ISR, STD_IRQ);
825 ks8851_wrreg16(ks, KS_IER, STD_IRQ);
826
827 netif_start_queue(ks->netdev);
828
829 if (netif_msg_ifup(ks))
830 ks_dbg(ks, "network device %s up\n", dev->name);
831
832 mutex_unlock(&ks->lock);
833 return 0;
834}
835
836
837
838
839
840
841
842
843
844static int ks8851_net_stop(struct net_device *dev)
845{
846 struct ks8851_net *ks = netdev_priv(dev);
847
848 if (netif_msg_ifdown(ks))
849 ks_info(ks, "%s: shutting down\n", dev->name);
850
851 netif_stop_queue(dev);
852
853 mutex_lock(&ks->lock);
854
855
856 flush_work(&ks->irq_work);
857 flush_work(&ks->tx_work);
858 flush_work(&ks->rxctrl_work);
859
860
861 ks8851_wrreg16(ks, KS_IER, 0x0000);
862 ks8851_wrreg16(ks, KS_ISR, 0xffff);
863
864
865 ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
866
867
868 ks8851_wrreg16(ks, KS_TXCR, 0x0000);
869
870
871 ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
872
873
874 while (!skb_queue_empty(&ks->txq)) {
875 struct sk_buff *txb = skb_dequeue(&ks->txq);
876
877 if (netif_msg_ifdown(ks))
878 ks_dbg(ks, "%s: freeing txb %p\n", __func__, txb);
879
880 dev_kfree_skb(txb);
881 }
882
883 mutex_unlock(&ks->lock);
884 return 0;
885}
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
901 struct net_device *dev)
902{
903 struct ks8851_net *ks = netdev_priv(dev);
904 unsigned needed = calc_txlen(skb->len);
905 netdev_tx_t ret = NETDEV_TX_OK;
906
907 if (netif_msg_tx_queued(ks))
908 ks_dbg(ks, "%s: skb %p, %d@%p\n", __func__,
909 skb, skb->len, skb->data);
910
911 spin_lock(&ks->statelock);
912
913 if (needed > ks->tx_space) {
914 netif_stop_queue(dev);
915 ret = NETDEV_TX_BUSY;
916 } else {
917 ks->tx_space -= needed;
918 skb_queue_tail(&ks->txq, skb);
919 }
920
921 spin_unlock(&ks->statelock);
922 schedule_work(&ks->tx_work);
923
924 return ret;
925}
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940static void ks8851_rxctrl_work(struct work_struct *work)
941{
942 struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
943
944 mutex_lock(&ks->lock);
945
946
947 ks8851_wrreg16(ks, KS_RXCR1, 0x00);
948
949 mutex_unlock(&ks->lock);
950}
951
952static void ks8851_set_rx_mode(struct net_device *dev)
953{
954 struct ks8851_net *ks = netdev_priv(dev);
955 struct ks8851_rxctrl rxctrl;
956
957 memset(&rxctrl, 0, sizeof(rxctrl));
958
959 if (dev->flags & IFF_PROMISC) {
960
961
962 rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF;
963 } else if (dev->flags & IFF_ALLMULTI) {
964
965
966 rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE |
967 RXCR1_RXPAFMA | RXCR1_RXMAFMA);
968 } else if (dev->flags & IFF_MULTICAST && dev->mc_count > 0) {
969 struct dev_mc_list *mcptr = dev->mc_list;
970 u32 crc;
971 int i;
972
973
974
975 for (i = dev->mc_count; i > 0; i--) {
976 crc = ether_crc(ETH_ALEN, mcptr->dmi_addr);
977 crc >>= (32 - 6);
978
979 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
980 mcptr = mcptr->next;
981 }
982
983 rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA;
984 } else {
985
986 rxctrl.rxcr1 = RXCR1_RXPAFMA;
987 }
988
989 rxctrl.rxcr1 |= (RXCR1_RXUE |
990 RXCR1_RXBE |
991 RXCR1_RXE |
992 RXCR1_RXFCE);
993
994 rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME;
995
996
997
998 spin_lock(&ks->statelock);
999
1000 if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) {
1001 memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl));
1002 schedule_work(&ks->rxctrl_work);
1003 }
1004
1005 spin_unlock(&ks->statelock);
1006}
1007
1008static int ks8851_set_mac_address(struct net_device *dev, void *addr)
1009{
1010 struct sockaddr *sa = addr;
1011
1012 if (netif_running(dev))
1013 return -EBUSY;
1014
1015 if (!is_valid_ether_addr(sa->sa_data))
1016 return -EADDRNOTAVAIL;
1017
1018 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1019 return ks8851_write_mac_addr(dev);
1020}
1021
1022static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1023{
1024 struct ks8851_net *ks = netdev_priv(dev);
1025
1026 if (!netif_running(dev))
1027 return -EINVAL;
1028
1029 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1030}
1031
1032static const struct net_device_ops ks8851_netdev_ops = {
1033 .ndo_open = ks8851_net_open,
1034 .ndo_stop = ks8851_net_stop,
1035 .ndo_do_ioctl = ks8851_net_ioctl,
1036 .ndo_start_xmit = ks8851_start_xmit,
1037 .ndo_set_mac_address = ks8851_set_mac_address,
1038 .ndo_set_rx_mode = ks8851_set_rx_mode,
1039 .ndo_change_mtu = eth_change_mtu,
1040 .ndo_validate_addr = eth_validate_addr,
1041};
1042
1043
1044
1045static void ks8851_get_drvinfo(struct net_device *dev,
1046 struct ethtool_drvinfo *di)
1047{
1048 strlcpy(di->driver, "KS8851", sizeof(di->driver));
1049 strlcpy(di->version, "1.00", sizeof(di->version));
1050 strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info));
1051}
1052
1053static u32 ks8851_get_msglevel(struct net_device *dev)
1054{
1055 struct ks8851_net *ks = netdev_priv(dev);
1056 return ks->msg_enable;
1057}
1058
1059static void ks8851_set_msglevel(struct net_device *dev, u32 to)
1060{
1061 struct ks8851_net *ks = netdev_priv(dev);
1062 ks->msg_enable = to;
1063}
1064
1065static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1066{
1067 struct ks8851_net *ks = netdev_priv(dev);
1068 return mii_ethtool_gset(&ks->mii, cmd);
1069}
1070
1071static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1072{
1073 struct ks8851_net *ks = netdev_priv(dev);
1074 return mii_ethtool_sset(&ks->mii, cmd);
1075}
1076
1077static u32 ks8851_get_link(struct net_device *dev)
1078{
1079 struct ks8851_net *ks = netdev_priv(dev);
1080 return mii_link_ok(&ks->mii);
1081}
1082
1083static int ks8851_nway_reset(struct net_device *dev)
1084{
1085 struct ks8851_net *ks = netdev_priv(dev);
1086 return mii_nway_restart(&ks->mii);
1087}
1088
1089static const struct ethtool_ops ks8851_ethtool_ops = {
1090 .get_drvinfo = ks8851_get_drvinfo,
1091 .get_msglevel = ks8851_get_msglevel,
1092 .set_msglevel = ks8851_set_msglevel,
1093 .get_settings = ks8851_get_settings,
1094 .set_settings = ks8851_set_settings,
1095 .get_link = ks8851_get_link,
1096 .nway_reset = ks8851_nway_reset,
1097};
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109static int ks8851_phy_reg(int reg)
1110{
1111 switch (reg) {
1112 case MII_BMCR:
1113 return KS_P1MBCR;
1114 case MII_BMSR:
1115 return KS_P1MBSR;
1116 case MII_PHYSID1:
1117 return KS_PHY1ILR;
1118 case MII_PHYSID2:
1119 return KS_PHY1IHR;
1120 case MII_ADVERTISE:
1121 return KS_P1ANAR;
1122 case MII_LPA:
1123 return KS_P1ANLPR;
1124 }
1125
1126 return 0x0;
1127}
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
1145{
1146 struct ks8851_net *ks = netdev_priv(dev);
1147 int ksreg;
1148 int result;
1149
1150 ksreg = ks8851_phy_reg(reg);
1151 if (!ksreg)
1152 return 0x0;
1153
1154 mutex_lock(&ks->lock);
1155 result = ks8851_rdreg16(ks, ksreg);
1156 mutex_unlock(&ks->lock);
1157
1158 return result;
1159}
1160
1161static void ks8851_phy_write(struct net_device *dev,
1162 int phy, int reg, int value)
1163{
1164 struct ks8851_net *ks = netdev_priv(dev);
1165 int ksreg;
1166
1167 ksreg = ks8851_phy_reg(reg);
1168 if (ksreg) {
1169 mutex_lock(&ks->lock);
1170 ks8851_wrreg16(ks, ksreg, value);
1171 mutex_unlock(&ks->lock);
1172 }
1173}
1174
1175
1176
1177
1178
1179
1180
1181static int ks8851_read_selftest(struct ks8851_net *ks)
1182{
1183 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1184 int ret = 0;
1185 unsigned rd;
1186
1187 rd = ks8851_rdreg16(ks, KS_MBIR);
1188
1189 if ((rd & both_done) != both_done) {
1190 ks_warn(ks, "Memory selftest not finished\n");
1191 return 0;
1192 }
1193
1194 if (rd & MBIR_TXMBFA) {
1195 ks_err(ks, "TX memory selftest fail\n");
1196 ret |= 1;
1197 }
1198
1199 if (rd & MBIR_RXMBFA) {
1200 ks_err(ks, "RX memory selftest fail\n");
1201 ret |= 2;
1202 }
1203
1204 return 0;
1205}
1206
1207
1208
1209static int __devinit ks8851_probe(struct spi_device *spi)
1210{
1211 struct net_device *ndev;
1212 struct ks8851_net *ks;
1213 int ret;
1214
1215 ndev = alloc_etherdev(sizeof(struct ks8851_net));
1216 if (!ndev) {
1217 dev_err(&spi->dev, "failed to alloc ethernet device\n");
1218 return -ENOMEM;
1219 }
1220
1221 spi->bits_per_word = 8;
1222
1223 ks = netdev_priv(ndev);
1224
1225 ks->netdev = ndev;
1226 ks->spidev = spi;
1227 ks->tx_space = 6144;
1228
1229 mutex_init(&ks->lock);
1230 spin_lock_init(&ks->statelock);
1231
1232 INIT_WORK(&ks->tx_work, ks8851_tx_work);
1233 INIT_WORK(&ks->irq_work, ks8851_irq_work);
1234 INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work);
1235
1236
1237
1238 spi_message_init(&ks->spi_msg1);
1239 spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1);
1240
1241 spi_message_init(&ks->spi_msg2);
1242 spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2);
1243 spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2);
1244
1245
1246 ks->mii.dev = ndev;
1247 ks->mii.phy_id = 1,
1248 ks->mii.phy_id_mask = 1;
1249 ks->mii.reg_num_mask = 0xf;
1250 ks->mii.mdio_read = ks8851_phy_read;
1251 ks->mii.mdio_write = ks8851_phy_write;
1252
1253 dev_info(&spi->dev, "message enable is %d\n", msg_enable);
1254
1255
1256 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1257 NETIF_MSG_PROBE |
1258 NETIF_MSG_LINK));
1259
1260 skb_queue_head_init(&ks->txq);
1261
1262 SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops);
1263 SET_NETDEV_DEV(ndev, &spi->dev);
1264
1265 dev_set_drvdata(&spi->dev, ks);
1266
1267 ndev->if_port = IF_PORT_100BASET;
1268 ndev->netdev_ops = &ks8851_netdev_ops;
1269 ndev->irq = spi->irq;
1270
1271
1272 ks8851_soft_reset(ks, GRR_GSR);
1273
1274
1275
1276 if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1277 dev_err(&spi->dev, "failed to read device ID\n");
1278 ret = -ENODEV;
1279 goto err_id;
1280 }
1281
1282 ks8851_read_selftest(ks);
1283 ks8851_init_mac(ks);
1284
1285 ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW,
1286 ndev->name, ks);
1287 if (ret < 0) {
1288 dev_err(&spi->dev, "failed to get irq\n");
1289 goto err_irq;
1290 }
1291
1292 ret = register_netdev(ndev);
1293 if (ret) {
1294 dev_err(&spi->dev, "failed to register network device\n");
1295 goto err_netdev;
1296 }
1297
1298 dev_info(&spi->dev, "revision %d, MAC %pM, IRQ %d\n",
1299 CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)),
1300 ndev->dev_addr, ndev->irq);
1301
1302 return 0;
1303
1304
1305err_netdev:
1306 free_irq(ndev->irq, ndev);
1307
1308err_id:
1309err_irq:
1310 free_netdev(ndev);
1311 return ret;
1312}
1313
1314static int __devexit ks8851_remove(struct spi_device *spi)
1315{
1316 struct ks8851_net *priv = dev_get_drvdata(&spi->dev);
1317
1318 if (netif_msg_drv(priv))
1319 dev_info(&spi->dev, "remove");
1320
1321 unregister_netdev(priv->netdev);
1322 free_irq(spi->irq, priv);
1323 free_netdev(priv->netdev);
1324
1325 return 0;
1326}
1327
1328static struct spi_driver ks8851_driver = {
1329 .driver = {
1330 .name = "ks8851",
1331 .owner = THIS_MODULE,
1332 },
1333 .probe = ks8851_probe,
1334 .remove = __devexit_p(ks8851_remove),
1335};
1336
1337static int __init ks8851_init(void)
1338{
1339 return spi_register_driver(&ks8851_driver);
1340}
1341
1342static void __exit ks8851_exit(void)
1343{
1344 spi_unregister_driver(&ks8851_driver);
1345}
1346
1347module_init(ks8851_init);
1348module_exit(ks8851_exit);
1349
1350MODULE_DESCRIPTION("KS8851 Network driver");
1351MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1352MODULE_LICENSE("GPL");
1353
1354module_param_named(message, msg_enable, int, 0);
1355MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1356MODULE_ALIAS("spi:ks8851");
1357