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