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