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