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