1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/bug.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/string.h>
30#include <linux/timer.h>
31#include <linux/errno.h>
32#include <linux/ioport.h>
33#include <linux/slab.h>
34#include <linux/interrupt.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/skbuff.h>
38#include <linux/bitops.h>
39#include <linux/err.h>
40#include <linux/ethtool.h>
41#include <linux/mii.h>
42#include <linux/phy.h>
43#include <linux/platform_device.h>
44#include <linux/prefetch.h>
45
46#include <asm/cache.h>
47#include <asm/io.h>
48#include <asm/processor.h>
49
50
51
52#define CONFIG_SBMAC_COALESCE
53
54
55#define TX_TIMEOUT (2*HZ)
56
57
58MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
59MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
60
61
62
63
64
65static int debug = 1;
66module_param(debug, int, S_IRUGO);
67MODULE_PARM_DESC(debug, "Debug messages");
68
69#ifdef CONFIG_SBMAC_COALESCE
70static int int_pktcnt_tx = 255;
71module_param(int_pktcnt_tx, int, S_IRUGO);
72MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
73
74static int int_timeout_tx = 255;
75module_param(int_timeout_tx, int, S_IRUGO);
76MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
77
78static int int_pktcnt_rx = 64;
79module_param(int_pktcnt_rx, int, S_IRUGO);
80MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
81
82static int int_timeout_rx = 64;
83module_param(int_timeout_rx, int, S_IRUGO);
84MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
85#endif
86
87#include <asm/sibyte/board.h>
88#include <asm/sibyte/sb1250.h>
89#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
90#include <asm/sibyte/bcm1480_regs.h>
91#include <asm/sibyte/bcm1480_int.h>
92#define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
93#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
94#include <asm/sibyte/sb1250_regs.h>
95#include <asm/sibyte/sb1250_int.h>
96#else
97#error invalid SiByte MAC configuration
98#endif
99#include <asm/sibyte/sb1250_scd.h>
100#include <asm/sibyte/sb1250_mac.h>
101#include <asm/sibyte/sb1250_dma.h>
102
103#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
104#define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
105#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
106#define UNIT_INT(n) (K_INT_MAC_0 + (n))
107#else
108#error invalid SiByte MAC configuration
109#endif
110
111#ifdef K_INT_PHY
112#define SBMAC_PHY_INT K_INT_PHY
113#else
114#define SBMAC_PHY_INT PHY_POLL
115#endif
116
117
118
119
120
121enum sbmac_speed {
122 sbmac_speed_none = 0,
123 sbmac_speed_10 = SPEED_10,
124 sbmac_speed_100 = SPEED_100,
125 sbmac_speed_1000 = SPEED_1000,
126};
127
128enum sbmac_duplex {
129 sbmac_duplex_none = -1,
130 sbmac_duplex_half = DUPLEX_HALF,
131 sbmac_duplex_full = DUPLEX_FULL,
132};
133
134enum sbmac_fc {
135 sbmac_fc_none,
136 sbmac_fc_disabled,
137 sbmac_fc_frame,
138 sbmac_fc_collision,
139 sbmac_fc_carrier,
140};
141
142enum sbmac_state {
143 sbmac_state_uninit,
144 sbmac_state_off,
145 sbmac_state_on,
146 sbmac_state_broken,
147};
148
149
150
151
152
153
154
155#define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
156 (d)->sbdma_dscrtable : (d)->f+1)
157
158
159#define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
160
161#define SBMAC_MAX_TXDESCR 256
162#define SBMAC_MAX_RXDESCR 256
163
164#define ENET_PACKET_SIZE 1518
165
166
167
168
169
170
171struct sbdmadscr {
172 uint64_t dscr_a;
173 uint64_t dscr_b;
174};
175
176
177
178
179
180struct sbmacdma {
181
182
183
184
185
186 struct sbmac_softc *sbdma_eth;
187
188 int sbdma_channel;
189 int sbdma_txdir;
190 int sbdma_maxdescr;
191
192#ifdef CONFIG_SBMAC_COALESCE
193 int sbdma_int_pktcnt;
194
195
196 int sbdma_int_timeout;
197
198#endif
199 void __iomem *sbdma_config0;
200 void __iomem *sbdma_config1;
201 void __iomem *sbdma_dscrbase;
202
203 void __iomem *sbdma_dscrcnt;
204 void __iomem *sbdma_curdscr;
205
206 void __iomem *sbdma_oodpktlost;
207
208
209
210
211
212 void *sbdma_dscrtable_unaligned;
213 struct sbdmadscr *sbdma_dscrtable;
214
215 struct sbdmadscr *sbdma_dscrtable_end;
216
217 struct sk_buff **sbdma_ctxtable;
218
219
220 dma_addr_t sbdma_dscrtable_phys;
221
222 struct sbdmadscr *sbdma_addptr;
223 struct sbdmadscr *sbdma_remptr;
224
225};
226
227
228
229
230
231
232struct sbmac_softc {
233
234
235
236
237 struct net_device *sbm_dev;
238 struct napi_struct napi;
239 struct phy_device *phy_dev;
240 struct mii_bus *mii_bus;
241 spinlock_t sbm_lock;
242 int sbm_devflags;
243
244
245
246
247 void __iomem *sbm_base;
248 enum sbmac_state sbm_state;
249
250 void __iomem *sbm_macenable;
251 void __iomem *sbm_maccfg;
252 void __iomem *sbm_fifocfg;
253 void __iomem *sbm_framecfg;
254 void __iomem *sbm_rxfilter;
255 void __iomem *sbm_isr;
256 void __iomem *sbm_imr;
257 void __iomem *sbm_mdio;
258
259 enum sbmac_speed sbm_speed;
260 enum sbmac_duplex sbm_duplex;
261 enum sbmac_fc sbm_fc;
262 int sbm_pause;
263 int sbm_link;
264
265 unsigned char sbm_hwaddr[ETH_ALEN];
266
267 struct sbmacdma sbm_txdma;
268 struct sbmacdma sbm_rxdma;
269 int rx_hw_checksum;
270 int sbe_idx;
271};
272
273
274
275
276
277
278
279
280
281
282static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
283 int txrx, int maxdescr);
284static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
285static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
286 struct sk_buff *m);
287static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
288static void sbdma_emptyring(struct sbmacdma *d);
289static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
290static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
291 int work_to_do, int poll);
292static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
293 int poll);
294static int sbmac_initctx(struct sbmac_softc *s);
295static void sbmac_channel_start(struct sbmac_softc *s);
296static void sbmac_channel_stop(struct sbmac_softc *s);
297static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
298 enum sbmac_state);
299static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
300static uint64_t sbmac_addr2reg(unsigned char *ptr);
301static irqreturn_t sbmac_intr(int irq, void *dev_instance);
302static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
303static void sbmac_setmulti(struct sbmac_softc *sc);
304static int sbmac_init(struct platform_device *pldev, long long base);
305static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
306static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
307 enum sbmac_fc fc);
308
309static int sbmac_open(struct net_device *dev);
310static void sbmac_tx_timeout (struct net_device *dev);
311static void sbmac_set_rx_mode(struct net_device *dev);
312static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
313static int sbmac_close(struct net_device *dev);
314static int sbmac_poll(struct napi_struct *napi, int budget);
315
316static void sbmac_mii_poll(struct net_device *dev);
317static int sbmac_mii_probe(struct net_device *dev);
318
319static void sbmac_mii_sync(void __iomem *sbm_mdio);
320static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
321 int bitcnt);
322static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
323static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
324 u16 val);
325
326
327
328
329
330
331static char sbmac_string[] = "sb1250-mac";
332
333static char sbmac_mdio_string[] = "sb1250-mac-mdio";
334
335
336
337
338
339
340#define MII_COMMAND_START 0x01
341#define MII_COMMAND_READ 0x02
342#define MII_COMMAND_WRITE 0x01
343#define MII_COMMAND_ACK 0x02
344
345#define M_MAC_MDIO_DIR_OUTPUT 0
346
347#define ENABLE 1
348#define DISABLE 0
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363static void sbmac_mii_sync(void __iomem *sbm_mdio)
364{
365 int cnt;
366 uint64_t bits;
367 int mac_mdio_genc;
368
369 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
370
371 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
372
373 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
374
375 for (cnt = 0; cnt < 32; cnt++) {
376 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
377 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
378 }
379}
380
381
382
383
384
385
386
387
388
389
390
391
392
393static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
394 int bitcnt)
395{
396 int i;
397 uint64_t bits;
398 unsigned int curmask;
399 int mac_mdio_genc;
400
401 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
402
403 bits = M_MAC_MDIO_DIR_OUTPUT;
404 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
405
406 curmask = 1 << (bitcnt - 1);
407
408 for (i = 0; i < bitcnt; i++) {
409 if (data & curmask)
410 bits |= M_MAC_MDIO_OUT;
411 else bits &= ~M_MAC_MDIO_OUT;
412 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
413 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
414 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
415 curmask >>= 1;
416 }
417}
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
435{
436 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
437 void __iomem *sbm_mdio = sc->sbm_mdio;
438 int idx;
439 int error;
440 int regval;
441 int mac_mdio_genc;
442
443
444
445
446
447 sbmac_mii_sync(sbm_mdio);
448
449
450
451
452
453
454
455
456 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
457 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
458 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
459 sbmac_mii_senddata(sbm_mdio, regidx, 5);
460
461 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
462
463
464
465
466 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
467
468
469
470
471 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
472 sbm_mdio);
473 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
474
475
476
477
478 error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
479
480
481
482
483
484 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
485 sbm_mdio);
486 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
487
488 regval = 0;
489
490 for (idx = 0; idx < 16; idx++) {
491 regval <<= 1;
492
493 if (error == 0) {
494 if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
495 regval |= 1;
496 }
497
498 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
499 sbm_mdio);
500 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
501 }
502
503
504 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
505
506 if (error == 0)
507 return regval;
508 return 0xffff;
509}
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
528 u16 regval)
529{
530 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
531 void __iomem *sbm_mdio = sc->sbm_mdio;
532 int mac_mdio_genc;
533
534 sbmac_mii_sync(sbm_mdio);
535
536 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
537 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
538 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
539 sbmac_mii_senddata(sbm_mdio, regidx, 5);
540 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
541 sbmac_mii_senddata(sbm_mdio, regval, 16);
542
543 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
544
545 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
546
547 return 0;
548}
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
571 int txrx, int maxdescr)
572{
573#ifdef CONFIG_SBMAC_COALESCE
574 int int_pktcnt, int_timeout;
575#endif
576
577
578
579
580
581 d->sbdma_eth = s;
582 d->sbdma_channel = chan;
583 d->sbdma_txdir = txrx;
584
585#if 0
586
587 s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
588#endif
589
590 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
591 __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
592 __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
593 __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
594 __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
595 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
596 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
597 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
598 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
599 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
600 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
601 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
602 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
603 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
604 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
605 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
606 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
607 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
608 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
609 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
610 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
611
612
613
614
615
616 d->sbdma_config0 =
617 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
618 d->sbdma_config1 =
619 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
620 d->sbdma_dscrbase =
621 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
622 d->sbdma_dscrcnt =
623 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
624 d->sbdma_curdscr =
625 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
626 if (d->sbdma_txdir)
627 d->sbdma_oodpktlost = NULL;
628 else
629 d->sbdma_oodpktlost =
630 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
631
632
633
634
635
636 d->sbdma_maxdescr = maxdescr;
637
638 d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
639 sizeof(*d->sbdma_dscrtable),
640 GFP_KERNEL);
641
642
643
644
645
646 d->sbdma_dscrtable = (struct sbdmadscr *)
647 ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
648 sizeof(*d->sbdma_dscrtable));
649
650 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
651
652 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
653
654
655
656
657
658 d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
659 sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
660
661#ifdef CONFIG_SBMAC_COALESCE
662
663
664
665
666 int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
667 if ( int_pktcnt ) {
668 d->sbdma_int_pktcnt = int_pktcnt;
669 } else {
670 d->sbdma_int_pktcnt = 1;
671 }
672
673 int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
674 if ( int_timeout ) {
675 d->sbdma_int_timeout = int_timeout;
676 } else {
677 d->sbdma_int_timeout = 0;
678 }
679#endif
680
681}
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
697{
698
699
700
701
702#ifdef CONFIG_SBMAC_COALESCE
703 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
704 0, d->sbdma_config1);
705 __raw_writeq(M_DMA_EOP_INT_EN |
706 V_DMA_RINGSZ(d->sbdma_maxdescr) |
707 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
708 0, d->sbdma_config0);
709#else
710 __raw_writeq(0, d->sbdma_config1);
711 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
712 0, d->sbdma_config0);
713#endif
714
715 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
716
717
718
719
720
721 d->sbdma_addptr = d->sbdma_dscrtable;
722 d->sbdma_remptr = d->sbdma_dscrtable;
723}
724
725
726
727
728
729
730
731
732
733
734
735
736
737static void sbdma_channel_stop(struct sbmacdma *d)
738{
739
740
741
742
743 __raw_writeq(0, d->sbdma_config1);
744
745 __raw_writeq(0, d->sbdma_dscrbase);
746
747 __raw_writeq(0, d->sbdma_config0);
748
749
750
751
752
753 d->sbdma_addptr = NULL;
754 d->sbdma_remptr = NULL;
755}
756
757static inline void sbdma_align_skb(struct sk_buff *skb,
758 unsigned int power2, unsigned int offset)
759{
760 unsigned char *addr = skb->data;
761 unsigned char *newaddr = PTR_ALIGN(addr, power2);
762
763 skb_reserve(skb, newaddr - addr + offset);
764}
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
785 struct sk_buff *sb)
786{
787 struct net_device *dev = sc->sbm_dev;
788 struct sbdmadscr *dsc;
789 struct sbdmadscr *nextdsc;
790 struct sk_buff *sb_new = NULL;
791 int pktsize = ENET_PACKET_SIZE;
792
793
794
795 dsc = d->sbdma_addptr;
796 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
797
798
799
800
801
802
803
804 if (nextdsc == d->sbdma_remptr) {
805 return -ENOSPC;
806 }
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827 if (sb == NULL) {
828 sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
829 SMP_CACHE_BYTES * 2 +
830 NET_IP_ALIGN);
831 if (sb_new == NULL)
832 return -ENOBUFS;
833
834 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
835 }
836 else {
837 sb_new = sb;
838
839
840
841
842 }
843
844
845
846
847
848#ifdef CONFIG_SBMAC_COALESCE
849
850
851
852 dsc->dscr_a = virt_to_phys(sb_new->data) |
853 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
854#else
855 dsc->dscr_a = virt_to_phys(sb_new->data) |
856 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
857 M_DMA_DSCRA_INTERRUPT;
858#endif
859
860
861 dsc->dscr_b = 0;
862
863
864
865
866
867 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
868
869
870
871
872
873 d->sbdma_addptr = nextdsc;
874
875
876
877
878
879 __raw_writeq(1, d->sbdma_dscrcnt);
880
881 return 0;
882}
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
901{
902 struct sbdmadscr *dsc;
903 struct sbdmadscr *nextdsc;
904 uint64_t phys;
905 uint64_t ncb;
906 int length;
907
908
909
910 dsc = d->sbdma_addptr;
911 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
912
913
914
915
916
917
918
919 if (nextdsc == d->sbdma_remptr) {
920 return -ENOSPC;
921 }
922
923
924
925
926
927
928
929 length = sb->len;
930
931
932
933
934
935
936
937
938 phys = virt_to_phys(sb->data);
939 ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
940
941 dsc->dscr_a = phys |
942 V_DMA_DSCRA_A_SIZE(ncb) |
943#ifndef CONFIG_SBMAC_COALESCE
944 M_DMA_DSCRA_INTERRUPT |
945#endif
946 M_DMA_ETHTX_SOP;
947
948
949
950 dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
951 V_DMA_DSCRB_PKT_SIZE(length);
952
953
954
955
956
957 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
958
959
960
961
962
963 d->sbdma_addptr = nextdsc;
964
965
966
967
968
969 __raw_writeq(1, d->sbdma_dscrcnt);
970
971 return 0;
972}
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989static void sbdma_emptyring(struct sbmacdma *d)
990{
991 int idx;
992 struct sk_buff *sb;
993
994 for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
995 sb = d->sbdma_ctxtable[idx];
996 if (sb) {
997 dev_kfree_skb(sb);
998 d->sbdma_ctxtable[idx] = NULL;
999 }
1000 }
1001}
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
1019{
1020 int idx;
1021
1022 for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
1023 if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
1024 break;
1025 }
1026}
1027
1028#ifdef CONFIG_NET_POLL_CONTROLLER
1029static void sbmac_netpoll(struct net_device *netdev)
1030{
1031 struct sbmac_softc *sc = netdev_priv(netdev);
1032 int irq = sc->sbm_dev->irq;
1033
1034 __raw_writeq(0, sc->sbm_imr);
1035
1036 sbmac_intr(irq, netdev);
1037
1038#ifdef CONFIG_SBMAC_COALESCE
1039 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1040 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1041 sc->sbm_imr);
1042#else
1043 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1044 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1045#endif
1046}
1047#endif
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1066 int work_to_do, int poll)
1067{
1068 struct net_device *dev = sc->sbm_dev;
1069 int curidx;
1070 int hwidx;
1071 struct sbdmadscr *dsc;
1072 struct sk_buff *sb;
1073 int len;
1074 int work_done = 0;
1075 int dropped = 0;
1076
1077 prefetch(d);
1078
1079again:
1080
1081 dev->stats.rx_fifo_errors
1082 += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1083 __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1084
1085 while (work_to_do-- > 0) {
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097 dsc = d->sbdma_remptr;
1098 curidx = dsc - d->sbdma_dscrtable;
1099
1100 prefetch(dsc);
1101 prefetch(&d->sbdma_ctxtable[curidx]);
1102
1103 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1104 d->sbdma_dscrtable_phys) /
1105 sizeof(*d->sbdma_dscrtable);
1106
1107
1108
1109
1110
1111
1112
1113 if (curidx == hwidx)
1114 goto done;
1115
1116
1117
1118
1119
1120 sb = d->sbdma_ctxtable[curidx];
1121 d->sbdma_ctxtable[curidx] = NULL;
1122
1123 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
1124
1125
1126
1127
1128
1129
1130
1131 if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
1132
1133
1134
1135
1136
1137
1138
1139 if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
1140 -ENOBUFS)) {
1141 dev->stats.rx_dropped++;
1142
1143 sbdma_add_rcvbuffer(sc, d, sb);
1144
1145 printk(KERN_ERR "dropped packet (1)\n");
1146 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1147 goto done;
1148 } else {
1149
1150
1151
1152 skb_put(sb,len);
1153
1154
1155
1156
1157
1158
1159 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1160
1161 if (sc->rx_hw_checksum == ENABLE) {
1162 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1163 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1164 sb->ip_summed = CHECKSUM_UNNECESSARY;
1165
1166 } else {
1167 skb_checksum_none_assert(sb);
1168 }
1169 }
1170 prefetch(sb->data);
1171 prefetch((const void *)(((char *)sb->data)+32));
1172 if (poll)
1173 dropped = netif_receive_skb(sb);
1174 else
1175 dropped = netif_rx(sb);
1176
1177 if (dropped == NET_RX_DROP) {
1178 dev->stats.rx_dropped++;
1179 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1180 goto done;
1181 }
1182 else {
1183 dev->stats.rx_bytes += len;
1184 dev->stats.rx_packets++;
1185 }
1186 }
1187 } else {
1188
1189
1190
1191
1192 dev->stats.rx_errors++;
1193 sbdma_add_rcvbuffer(sc, d, sb);
1194 }
1195
1196
1197
1198
1199
1200
1201 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1202 work_done++;
1203 }
1204 if (!poll) {
1205 work_to_do = 32;
1206 goto again;
1207 }
1208done:
1209 return work_done;
1210}
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1231 int poll)
1232{
1233 struct net_device *dev = sc->sbm_dev;
1234 int curidx;
1235 int hwidx;
1236 struct sbdmadscr *dsc;
1237 struct sk_buff *sb;
1238 unsigned long flags;
1239 int packets_handled = 0;
1240
1241 spin_lock_irqsave(&(sc->sbm_lock), flags);
1242
1243 if (d->sbdma_remptr == d->sbdma_addptr)
1244 goto end_unlock;
1245
1246 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1247 d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
1248
1249 for (;;) {
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
1262
1263
1264
1265
1266
1267
1268
1269 if (curidx == hwidx)
1270 break;
1271
1272
1273
1274
1275
1276 dsc = &(d->sbdma_dscrtable[curidx]);
1277 sb = d->sbdma_ctxtable[curidx];
1278 d->sbdma_ctxtable[curidx] = NULL;
1279
1280
1281
1282
1283
1284 dev->stats.tx_bytes += sb->len;
1285 dev->stats.tx_packets++;
1286
1287
1288
1289
1290
1291 dev_kfree_skb_irq(sb);
1292
1293
1294
1295
1296
1297 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1298
1299 packets_handled++;
1300
1301 }
1302
1303
1304
1305
1306
1307
1308
1309 if (packets_handled)
1310 netif_wake_queue(d->sbdma_eth->sbm_dev);
1311
1312end_unlock:
1313 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
1314
1315}
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334static int sbmac_initctx(struct sbmac_softc *s)
1335{
1336
1337
1338
1339
1340
1341 s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1342 s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1343 s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1344 s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1345 s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1346 s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1347 s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1348 s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1349
1350
1351
1352
1353
1354
1355 sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1356 sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
1357
1358
1359
1360
1361
1362 s->sbm_state = sbmac_state_off;
1363
1364 return 0;
1365}
1366
1367
1368static void sbdma_uninitctx(struct sbmacdma *d)
1369{
1370 if (d->sbdma_dscrtable_unaligned) {
1371 kfree(d->sbdma_dscrtable_unaligned);
1372 d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
1373 }
1374
1375 if (d->sbdma_ctxtable) {
1376 kfree(d->sbdma_ctxtable);
1377 d->sbdma_ctxtable = NULL;
1378 }
1379}
1380
1381
1382static void sbmac_uninitctx(struct sbmac_softc *sc)
1383{
1384 sbdma_uninitctx(&(sc->sbm_txdma));
1385 sbdma_uninitctx(&(sc->sbm_rxdma));
1386}
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401static void sbmac_channel_start(struct sbmac_softc *s)
1402{
1403 uint64_t reg;
1404 void __iomem *port;
1405 uint64_t cfg,fifo,framecfg;
1406 int idx, th_value;
1407
1408
1409
1410
1411
1412 if (s->sbm_state == sbmac_state_on)
1413 return;
1414
1415
1416
1417
1418
1419 __raw_writeq(0, s->sbm_macenable);
1420
1421
1422
1423
1424
1425 __raw_writeq(0, s->sbm_rxfilter);
1426
1427
1428
1429
1430
1431 cfg = M_MAC_RETRY_EN |
1432 M_MAC_TX_HOLD_SOP_EN |
1433 V_MAC_TX_PAUSE_CNT_16K |
1434 M_MAC_AP_STAT_EN |
1435 M_MAC_FAST_SYNC |
1436 M_MAC_SS_EN |
1437 0;
1438
1439
1440
1441
1442
1443
1444 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
1445 th_value = 28;
1446 else
1447 th_value = 64;
1448
1449 fifo = V_MAC_TX_WR_THRSH(4) |
1450 ((s->sbm_speed == sbmac_speed_1000)
1451 ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1452 V_MAC_TX_RL_THRSH(4) |
1453 V_MAC_RX_PL_THRSH(4) |
1454 V_MAC_RX_RD_THRSH(4) |
1455 V_MAC_RX_RL_THRSH(8) |
1456 0;
1457
1458 framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1459 V_MAC_MAX_FRAMESZ_DEFAULT |
1460 V_MAC_BACKOFF_SEL(1);
1461
1462
1463
1464
1465
1466 port = s->sbm_base + R_MAC_HASH_BASE;
1467 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
1468 __raw_writeq(0, port);
1469 port += sizeof(uint64_t);
1470 }
1471
1472
1473
1474
1475
1476 port = s->sbm_base + R_MAC_ADDR_BASE;
1477 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
1478 __raw_writeq(0, port);
1479 port += sizeof(uint64_t);
1480 }
1481
1482
1483
1484
1485
1486 port = s->sbm_base + R_MAC_CHUP0_BASE;
1487 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1488 __raw_writeq(0, port);
1489 port += sizeof(uint64_t);
1490 }
1491
1492
1493 port = s->sbm_base + R_MAC_CHLO0_BASE;
1494 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1495 __raw_writeq(0, port);
1496 port += sizeof(uint64_t);
1497 }
1498
1499
1500
1501
1502
1503
1504 reg = sbmac_addr2reg(s->sbm_hwaddr);
1505
1506 port = s->sbm_base + R_MAC_ADDR_BASE;
1507 __raw_writeq(reg, port);
1508 port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1509
1510 __raw_writeq(reg, port);
1511
1512
1513
1514
1515
1516
1517 __raw_writeq(0, s->sbm_rxfilter);
1518 __raw_writeq(0, s->sbm_imr);
1519 __raw_writeq(framecfg, s->sbm_framecfg);
1520 __raw_writeq(fifo, s->sbm_fifocfg);
1521 __raw_writeq(cfg, s->sbm_maccfg);
1522
1523
1524
1525
1526
1527 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1528 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
1529
1530
1531
1532
1533
1534 sbmac_set_speed(s,s->sbm_speed);
1535 sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
1536
1537
1538
1539
1540
1541 sbdma_fillring(s, &(s->sbm_rxdma));
1542
1543
1544
1545
1546
1547#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
1548 __raw_writeq(M_MAC_RXDMA_EN0 |
1549 M_MAC_TXDMA_EN0, s->sbm_macenable);
1550#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
1551 __raw_writeq(M_MAC_RXDMA_EN0 |
1552 M_MAC_TXDMA_EN0 |
1553 M_MAC_RX_ENABLE |
1554 M_MAC_TX_ENABLE, s->sbm_macenable);
1555#else
1556#error invalid SiByte MAC configuration
1557#endif
1558
1559#ifdef CONFIG_SBMAC_COALESCE
1560 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1561 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
1562#else
1563 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1564 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
1565#endif
1566
1567
1568
1569
1570
1571 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1572
1573
1574
1575
1576
1577 s->sbm_state = sbmac_state_on;
1578
1579
1580
1581
1582
1583 sbmac_setmulti(s);
1584
1585
1586
1587
1588
1589 if (s->sbm_devflags & IFF_PROMISC) {
1590 sbmac_promiscuous_mode(s,1);
1591 }
1592
1593}
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608static void sbmac_channel_stop(struct sbmac_softc *s)
1609{
1610
1611
1612 if (s->sbm_state == sbmac_state_off)
1613 return;
1614
1615
1616
1617 __raw_writeq(0, s->sbm_rxfilter);
1618 __raw_writeq(0, s->sbm_imr);
1619
1620
1621
1622
1623
1624
1625
1626 __raw_writeq(0, s->sbm_macenable);
1627
1628
1629
1630 s->sbm_state = sbmac_state_off;
1631
1632
1633
1634
1635
1636 sbdma_channel_stop(&(s->sbm_rxdma));
1637 sbdma_channel_stop(&(s->sbm_txdma));
1638
1639
1640
1641 sbdma_emptyring(&(s->sbm_rxdma));
1642 sbdma_emptyring(&(s->sbm_txdma));
1643
1644}
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
1658 enum sbmac_state state)
1659{
1660 enum sbmac_state oldstate = sc->sbm_state;
1661
1662
1663
1664
1665
1666 if (state == oldstate) {
1667 return oldstate;
1668 }
1669
1670
1671
1672
1673
1674 if (state == sbmac_state_on) {
1675 sbmac_channel_start(sc);
1676 }
1677 else {
1678 sbmac_channel_stop(sc);
1679 }
1680
1681
1682
1683
1684
1685 return oldstate;
1686}
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1703{
1704 uint64_t reg;
1705
1706 if (sc->sbm_state != sbmac_state_on)
1707 return;
1708
1709 if (onoff) {
1710 reg = __raw_readq(sc->sbm_rxfilter);
1711 reg |= M_MAC_ALLPKT_EN;
1712 __raw_writeq(reg, sc->sbm_rxfilter);
1713 }
1714 else {
1715 reg = __raw_readq(sc->sbm_rxfilter);
1716 reg &= ~M_MAC_ALLPKT_EN;
1717 __raw_writeq(reg, sc->sbm_rxfilter);
1718 }
1719}
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1734{
1735 uint64_t reg;
1736
1737
1738 reg = __raw_readq(sc->sbm_rxfilter);
1739 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
1740 __raw_writeq(reg, sc->sbm_rxfilter);
1741
1742
1743
1744 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
1745 sc->rx_hw_checksum = DISABLE;
1746 } else {
1747 sc->rx_hw_checksum = ENABLE;
1748 }
1749}
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765static uint64_t sbmac_addr2reg(unsigned char *ptr)
1766{
1767 uint64_t reg = 0;
1768
1769 ptr += 6;
1770
1771 reg |= (uint64_t) *(--ptr);
1772 reg <<= 8;
1773 reg |= (uint64_t) *(--ptr);
1774 reg <<= 8;
1775 reg |= (uint64_t) *(--ptr);
1776 reg <<= 8;
1777 reg |= (uint64_t) *(--ptr);
1778 reg <<= 8;
1779 reg |= (uint64_t) *(--ptr);
1780 reg <<= 8;
1781 reg |= (uint64_t) *(--ptr);
1782
1783 return reg;
1784}
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
1803{
1804 uint64_t cfg;
1805 uint64_t framecfg;
1806
1807
1808
1809
1810
1811 s->sbm_speed = speed;
1812
1813 if (s->sbm_state == sbmac_state_on)
1814 return 0;
1815
1816
1817
1818
1819
1820 cfg = __raw_readq(s->sbm_maccfg);
1821 framecfg = __raw_readq(s->sbm_framecfg);
1822
1823
1824
1825
1826
1827 cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1828 framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1829 M_MAC_SLOT_SIZE);
1830
1831
1832
1833
1834
1835 switch (speed) {
1836 case sbmac_speed_10:
1837 framecfg |= V_MAC_IFG_RX_10 |
1838 V_MAC_IFG_TX_10 |
1839 K_MAC_IFG_THRSH_10 |
1840 V_MAC_SLOT_SIZE_10;
1841 cfg |= V_MAC_SPEED_SEL_10MBPS;
1842 break;
1843
1844 case sbmac_speed_100:
1845 framecfg |= V_MAC_IFG_RX_100 |
1846 V_MAC_IFG_TX_100 |
1847 V_MAC_IFG_THRSH_100 |
1848 V_MAC_SLOT_SIZE_100;
1849 cfg |= V_MAC_SPEED_SEL_100MBPS ;
1850 break;
1851
1852 case sbmac_speed_1000:
1853 framecfg |= V_MAC_IFG_RX_1000 |
1854 V_MAC_IFG_TX_1000 |
1855 V_MAC_IFG_THRSH_1000 |
1856 V_MAC_SLOT_SIZE_1000;
1857 cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1858 break;
1859
1860 default:
1861 return 0;
1862 }
1863
1864
1865
1866
1867
1868 __raw_writeq(framecfg, s->sbm_framecfg);
1869 __raw_writeq(cfg, s->sbm_maccfg);
1870
1871 return 1;
1872}
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
1891 enum sbmac_fc fc)
1892{
1893 uint64_t cfg;
1894
1895
1896
1897
1898
1899 s->sbm_duplex = duplex;
1900 s->sbm_fc = fc;
1901
1902 if (s->sbm_state == sbmac_state_on)
1903 return 0;
1904
1905
1906
1907
1908
1909 cfg = __raw_readq(s->sbm_maccfg);
1910
1911
1912
1913
1914
1915 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
1916
1917
1918 switch (duplex) {
1919 case sbmac_duplex_half:
1920 switch (fc) {
1921 case sbmac_fc_disabled:
1922 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1923 break;
1924
1925 case sbmac_fc_collision:
1926 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1927 break;
1928
1929 case sbmac_fc_carrier:
1930 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1931 break;
1932
1933 case sbmac_fc_frame:
1934 default:
1935 return 0;
1936 }
1937 break;
1938
1939 case sbmac_duplex_full:
1940 switch (fc) {
1941 case sbmac_fc_disabled:
1942 cfg |= V_MAC_FC_CMD_DISABLED;
1943 break;
1944
1945 case sbmac_fc_frame:
1946 cfg |= V_MAC_FC_CMD_ENABLED;
1947 break;
1948
1949 case sbmac_fc_collision:
1950 case sbmac_fc_carrier:
1951 default:
1952 return 0;
1953 }
1954 break;
1955 default:
1956 return 0;
1957 }
1958
1959
1960
1961
1962
1963 __raw_writeq(cfg, s->sbm_maccfg);
1964
1965 return 1;
1966}
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982static irqreturn_t sbmac_intr(int irq,void *dev_instance)
1983{
1984 struct net_device *dev = (struct net_device *) dev_instance;
1985 struct sbmac_softc *sc = netdev_priv(dev);
1986 uint64_t isr;
1987 int handled = 0;
1988
1989
1990
1991
1992
1993
1994 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
1995
1996 if (isr == 0)
1997 return IRQ_RETVAL(0);
1998 handled = 1;
1999
2000
2001
2002
2003
2004 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
2005 sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
2006
2007 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
2008 if (napi_schedule_prep(&sc->napi)) {
2009 __raw_writeq(0, sc->sbm_imr);
2010 __napi_schedule(&sc->napi);
2011
2012 }
2013 else {
2014
2015 sbdma_rx_process(sc,&(sc->sbm_rxdma),
2016 SBMAC_MAX_RXDESCR * 2, 0);
2017 }
2018 }
2019 return IRQ_RETVAL(handled);
2020}
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2036{
2037 struct sbmac_softc *sc = netdev_priv(dev);
2038 unsigned long flags;
2039
2040
2041 spin_lock_irqsave(&sc->sbm_lock, flags);
2042
2043
2044
2045
2046
2047
2048 if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2049
2050 netif_stop_queue(dev);
2051 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2052
2053 return NETDEV_TX_BUSY;
2054 }
2055
2056 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2057
2058 return NETDEV_TX_OK;
2059}
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075static void sbmac_setmulti(struct sbmac_softc *sc)
2076{
2077 uint64_t reg;
2078 void __iomem *port;
2079 int idx;
2080 struct netdev_hw_addr *ha;
2081 struct net_device *dev = sc->sbm_dev;
2082
2083
2084
2085
2086
2087
2088
2089 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2090 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
2091 __raw_writeq(0, port);
2092 }
2093
2094 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2095 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
2096 __raw_writeq(0, port);
2097 }
2098
2099
2100
2101
2102
2103 reg = __raw_readq(sc->sbm_rxfilter);
2104 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2105 __raw_writeq(reg, sc->sbm_rxfilter);
2106
2107 if (dev->flags & IFF_ALLMULTI) {
2108
2109
2110
2111
2112 reg = __raw_readq(sc->sbm_rxfilter);
2113 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2114 __raw_writeq(reg, sc->sbm_rxfilter);
2115 return;
2116 }
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128 idx = 1;
2129 netdev_for_each_mc_addr(ha, dev) {
2130 if (idx == MAC_ADDR_COUNT)
2131 break;
2132 reg = sbmac_addr2reg(ha->addr);
2133 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
2134 __raw_writeq(reg, port);
2135 idx++;
2136 }
2137
2138
2139
2140
2141
2142
2143 if (idx > 1) {
2144 reg = __raw_readq(sc->sbm_rxfilter);
2145 reg |= M_MAC_MCAST_EN;
2146 __raw_writeq(reg, sc->sbm_rxfilter);
2147 }
2148}
2149
2150static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
2151{
2152 if (new_mtu > ENET_PACKET_SIZE)
2153 return -EINVAL;
2154 _dev->mtu = new_mtu;
2155 pr_info("changing the mtu to %d\n", new_mtu);
2156 return 0;
2157}
2158
2159static const struct net_device_ops sbmac_netdev_ops = {
2160 .ndo_open = sbmac_open,
2161 .ndo_stop = sbmac_close,
2162 .ndo_start_xmit = sbmac_start_tx,
2163 .ndo_set_rx_mode = sbmac_set_rx_mode,
2164 .ndo_tx_timeout = sbmac_tx_timeout,
2165 .ndo_do_ioctl = sbmac_mii_ioctl,
2166 .ndo_change_mtu = sb1250_change_mtu,
2167 .ndo_validate_addr = eth_validate_addr,
2168 .ndo_set_mac_address = eth_mac_addr,
2169#ifdef CONFIG_NET_POLL_CONTROLLER
2170 .ndo_poll_controller = sbmac_netpoll,
2171#endif
2172};
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186static int sbmac_init(struct platform_device *pldev, long long base)
2187{
2188 struct net_device *dev = platform_get_drvdata(pldev);
2189 int idx = pldev->id;
2190 struct sbmac_softc *sc = netdev_priv(dev);
2191 unsigned char *eaddr;
2192 uint64_t ea_reg;
2193 int i;
2194 int err;
2195
2196 sc->sbm_dev = dev;
2197 sc->sbe_idx = idx;
2198
2199 eaddr = sc->sbm_hwaddr;
2200
2201
2202
2203
2204
2205
2206 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2207 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
2208 for (i = 0; i < 6; i++) {
2209 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2210 ea_reg >>= 8;
2211 }
2212
2213 for (i = 0; i < 6; i++) {
2214 dev->dev_addr[i] = eaddr[i];
2215 }
2216
2217
2218
2219
2220
2221
2222 sbmac_initctx(sc);
2223
2224
2225
2226
2227
2228 spin_lock_init(&(sc->sbm_lock));
2229
2230 dev->netdev_ops = &sbmac_netdev_ops;
2231 dev->watchdog_timeo = TX_TIMEOUT;
2232
2233 netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
2234
2235 dev->irq = UNIT_INT(idx);
2236
2237
2238 sbmac_set_iphdr_offset(sc);
2239
2240 sc->mii_bus = mdiobus_alloc();
2241 if (sc->mii_bus == NULL) {
2242 err = -ENOMEM;
2243 goto uninit_ctx;
2244 }
2245
2246 sc->mii_bus->name = sbmac_mdio_string;
2247 snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2248 pldev->name, idx);
2249 sc->mii_bus->priv = sc;
2250 sc->mii_bus->read = sbmac_mii_read;
2251 sc->mii_bus->write = sbmac_mii_write;
2252
2253 sc->mii_bus->parent = &pldev->dev;
2254
2255
2256
2257 err = mdiobus_register(sc->mii_bus);
2258 if (err) {
2259 printk(KERN_ERR "%s: unable to register MDIO bus\n",
2260 dev->name);
2261 goto free_mdio;
2262 }
2263 platform_set_drvdata(pldev, sc->mii_bus);
2264
2265 err = register_netdev(dev);
2266 if (err) {
2267 printk(KERN_ERR "%s.%d: unable to register netdev\n",
2268 sbmac_string, idx);
2269 goto unreg_mdio;
2270 }
2271
2272 pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
2273
2274 if (sc->rx_hw_checksum == ENABLE)
2275 pr_info("%s: enabling TCP rcv checksum\n", dev->name);
2276
2277
2278
2279
2280
2281
2282 pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
2283 dev->name, base, eaddr);
2284
2285 return 0;
2286unreg_mdio:
2287 mdiobus_unregister(sc->mii_bus);
2288free_mdio:
2289 mdiobus_free(sc->mii_bus);
2290uninit_ctx:
2291 sbmac_uninitctx(sc);
2292 return err;
2293}
2294
2295
2296static int sbmac_open(struct net_device *dev)
2297{
2298 struct sbmac_softc *sc = netdev_priv(dev);
2299 int err;
2300
2301 if (debug > 1)
2302 pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
2303
2304
2305
2306
2307
2308
2309
2310 __raw_readq(sc->sbm_isr);
2311 err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
2312 if (err) {
2313 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
2314 dev->irq);
2315 goto out_err;
2316 }
2317
2318 sc->sbm_speed = sbmac_speed_none;
2319 sc->sbm_duplex = sbmac_duplex_none;
2320 sc->sbm_fc = sbmac_fc_none;
2321 sc->sbm_pause = -1;
2322 sc->sbm_link = 0;
2323
2324
2325
2326
2327 err = sbmac_mii_probe(dev);
2328 if (err)
2329 goto out_unregister;
2330
2331
2332
2333
2334
2335 sbmac_set_channel_state(sc,sbmac_state_on);
2336
2337 netif_start_queue(dev);
2338
2339 sbmac_set_rx_mode(dev);
2340
2341 phy_start(sc->phy_dev);
2342
2343 napi_enable(&sc->napi);
2344
2345 return 0;
2346
2347out_unregister:
2348 free_irq(dev->irq, dev);
2349out_err:
2350 return err;
2351}
2352
2353static int sbmac_mii_probe(struct net_device *dev)
2354{
2355 struct sbmac_softc *sc = netdev_priv(dev);
2356 struct phy_device *phy_dev;
2357
2358 phy_dev = phy_find_first(sc->mii_bus);
2359 if (!phy_dev) {
2360 printk(KERN_ERR "%s: no PHY found\n", dev->name);
2361 return -ENXIO;
2362 }
2363
2364 phy_dev = phy_connect(dev, dev_name(&phy_dev->mdio.dev),
2365 &sbmac_mii_poll, PHY_INTERFACE_MODE_GMII);
2366 if (IS_ERR(phy_dev)) {
2367 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
2368 return PTR_ERR(phy_dev);
2369 }
2370
2371
2372 phy_dev->supported &= SUPPORTED_10baseT_Half |
2373 SUPPORTED_10baseT_Full |
2374 SUPPORTED_100baseT_Half |
2375 SUPPORTED_100baseT_Full |
2376 SUPPORTED_1000baseT_Half |
2377 SUPPORTED_1000baseT_Full |
2378 SUPPORTED_Autoneg |
2379 SUPPORTED_MII |
2380 SUPPORTED_Pause |
2381 SUPPORTED_Asym_Pause;
2382
2383 phy_attached_info(phy_dev);
2384
2385 phy_dev->advertising = phy_dev->supported;
2386
2387 sc->phy_dev = phy_dev;
2388
2389 return 0;
2390}
2391
2392
2393static void sbmac_mii_poll(struct net_device *dev)
2394{
2395 struct sbmac_softc *sc = netdev_priv(dev);
2396 struct phy_device *phy_dev = sc->phy_dev;
2397 unsigned long flags;
2398 enum sbmac_fc fc;
2399 int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
2400
2401 link_chg = (sc->sbm_link != phy_dev->link);
2402 speed_chg = (sc->sbm_speed != phy_dev->speed);
2403 duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
2404 pause_chg = (sc->sbm_pause != phy_dev->pause);
2405
2406 if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
2407 return;
2408
2409 if (!phy_dev->link) {
2410 if (link_chg) {
2411 sc->sbm_link = phy_dev->link;
2412 sc->sbm_speed = sbmac_speed_none;
2413 sc->sbm_duplex = sbmac_duplex_none;
2414 sc->sbm_fc = sbmac_fc_disabled;
2415 sc->sbm_pause = -1;
2416 pr_info("%s: link unavailable\n", dev->name);
2417 }
2418 return;
2419 }
2420
2421 if (phy_dev->duplex == DUPLEX_FULL) {
2422 if (phy_dev->pause)
2423 fc = sbmac_fc_frame;
2424 else
2425 fc = sbmac_fc_disabled;
2426 } else
2427 fc = sbmac_fc_collision;
2428 fc_chg = (sc->sbm_fc != fc);
2429
2430 pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
2431 phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
2432
2433 spin_lock_irqsave(&sc->sbm_lock, flags);
2434
2435 sc->sbm_speed = phy_dev->speed;
2436 sc->sbm_duplex = phy_dev->duplex;
2437 sc->sbm_fc = fc;
2438 sc->sbm_pause = phy_dev->pause;
2439 sc->sbm_link = phy_dev->link;
2440
2441 if ((speed_chg || duplex_chg || fc_chg) &&
2442 sc->sbm_state != sbmac_state_off) {
2443
2444
2445
2446 if (debug > 1)
2447 pr_debug("%s: restarting channel "
2448 "because PHY state changed\n", dev->name);
2449 sbmac_channel_stop(sc);
2450 sbmac_channel_start(sc);
2451 }
2452
2453 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2454}
2455
2456
2457static void sbmac_tx_timeout (struct net_device *dev)
2458{
2459 struct sbmac_softc *sc = netdev_priv(dev);
2460 unsigned long flags;
2461
2462 spin_lock_irqsave(&sc->sbm_lock, flags);
2463
2464
2465 netif_trans_update(dev);
2466 dev->stats.tx_errors++;
2467
2468 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2469
2470 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2471}
2472
2473
2474
2475
2476static void sbmac_set_rx_mode(struct net_device *dev)
2477{
2478 unsigned long flags;
2479 struct sbmac_softc *sc = netdev_priv(dev);
2480
2481 spin_lock_irqsave(&sc->sbm_lock, flags);
2482 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2483
2484
2485
2486
2487 if (dev->flags & IFF_PROMISC) {
2488 sbmac_promiscuous_mode(sc,1);
2489 }
2490 else {
2491 sbmac_promiscuous_mode(sc,0);
2492 }
2493 }
2494 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2495
2496
2497
2498
2499
2500 sbmac_setmulti(sc);
2501
2502}
2503
2504static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2505{
2506 struct sbmac_softc *sc = netdev_priv(dev);
2507
2508 if (!netif_running(dev) || !sc->phy_dev)
2509 return -EINVAL;
2510
2511 return phy_mii_ioctl(sc->phy_dev, rq, cmd);
2512}
2513
2514static int sbmac_close(struct net_device *dev)
2515{
2516 struct sbmac_softc *sc = netdev_priv(dev);
2517
2518 napi_disable(&sc->napi);
2519
2520 phy_stop(sc->phy_dev);
2521
2522 sbmac_set_channel_state(sc, sbmac_state_off);
2523
2524 netif_stop_queue(dev);
2525
2526 if (debug > 1)
2527 pr_debug("%s: Shutting down ethercard\n", dev->name);
2528
2529 phy_disconnect(sc->phy_dev);
2530 sc->phy_dev = NULL;
2531 free_irq(dev->irq, dev);
2532
2533 sbdma_emptyring(&(sc->sbm_txdma));
2534 sbdma_emptyring(&(sc->sbm_rxdma));
2535
2536 return 0;
2537}
2538
2539static int sbmac_poll(struct napi_struct *napi, int budget)
2540{
2541 struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
2542 int work_done;
2543
2544 work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
2545 sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2546
2547 if (work_done < budget) {
2548 napi_complete(napi);
2549
2550#ifdef CONFIG_SBMAC_COALESCE
2551 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2552 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2553 sc->sbm_imr);
2554#else
2555 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2556 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2557#endif
2558 }
2559
2560 return work_done;
2561}
2562
2563
2564static int sbmac_probe(struct platform_device *pldev)
2565{
2566 struct net_device *dev;
2567 struct sbmac_softc *sc;
2568 void __iomem *sbm_base;
2569 struct resource *res;
2570 u64 sbmac_orig_hwaddr;
2571 int err;
2572
2573 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
2574 BUG_ON(!res);
2575 sbm_base = ioremap_nocache(res->start, resource_size(res));
2576 if (!sbm_base) {
2577 printk(KERN_ERR "%s: unable to map device registers\n",
2578 dev_name(&pldev->dev));
2579 err = -ENOMEM;
2580 goto out_out;
2581 }
2582
2583
2584
2585
2586
2587
2588 sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2589 pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
2590 sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
2591 if (sbmac_orig_hwaddr == 0) {
2592 err = 0;
2593 goto out_unmap;
2594 }
2595
2596
2597
2598
2599 dev = alloc_etherdev(sizeof(struct sbmac_softc));
2600 if (!dev) {
2601 err = -ENOMEM;
2602 goto out_unmap;
2603 }
2604
2605 platform_set_drvdata(pldev, dev);
2606 SET_NETDEV_DEV(dev, &pldev->dev);
2607
2608 sc = netdev_priv(dev);
2609 sc->sbm_base = sbm_base;
2610
2611 err = sbmac_init(pldev, res->start);
2612 if (err)
2613 goto out_kfree;
2614
2615 return 0;
2616
2617out_kfree:
2618 free_netdev(dev);
2619 __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
2620
2621out_unmap:
2622 iounmap(sbm_base);
2623
2624out_out:
2625 return err;
2626}
2627
2628static int __exit sbmac_remove(struct platform_device *pldev)
2629{
2630 struct net_device *dev = platform_get_drvdata(pldev);
2631 struct sbmac_softc *sc = netdev_priv(dev);
2632
2633 unregister_netdev(dev);
2634 sbmac_uninitctx(sc);
2635 mdiobus_unregister(sc->mii_bus);
2636 mdiobus_free(sc->mii_bus);
2637 iounmap(sc->sbm_base);
2638 free_netdev(dev);
2639
2640 return 0;
2641}
2642
2643static struct platform_driver sbmac_driver = {
2644 .probe = sbmac_probe,
2645 .remove = __exit_p(sbmac_remove),
2646 .driver = {
2647 .name = sbmac_string,
2648 },
2649};
2650
2651module_platform_driver(sbmac_driver);
2652