summaryrefslogtreecommitdiffstats
path: root/drivers/net/vsc9953.c
blob: 1dddb44c8bf256c7666fdafb545af6a3fb2cc804 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/*
 *  Copyright 2014 Freescale Semiconductor, Inc.
 *
 *  SPDX-License-Identifier:      GPL-2.0+
 *
 *  Driver for the Vitesse VSC9953 L2 Switch
 */

#include <asm/io.h>
#include <asm/fsl_serdes.h>
#include <fm_eth.h>
#include <fsl_memac.h>
#include <bitfield.h>
#include <errno.h>
#include <malloc.h>
#include <vsc9953.h>
#include <ethsw.h>

static struct vsc9953_info vsc9953_l2sw = {
		.port[0] = VSC9953_PORT_INFO_INITIALIZER(0),
		.port[1] = VSC9953_PORT_INFO_INITIALIZER(1),
		.port[2] = VSC9953_PORT_INFO_INITIALIZER(2),
		.port[3] = VSC9953_PORT_INFO_INITIALIZER(3),
		.port[4] = VSC9953_PORT_INFO_INITIALIZER(4),
		.port[5] = VSC9953_PORT_INFO_INITIALIZER(5),
		.port[6] = VSC9953_PORT_INFO_INITIALIZER(6),
		.port[7] = VSC9953_PORT_INFO_INITIALIZER(7),
		.port[8] = VSC9953_PORT_INFO_INITIALIZER(8),
		.port[9] = VSC9953_PORT_INFO_INITIALIZER(9),
};

void vsc9953_port_info_set_mdio(int port_no, struct mii_dev *bus)
{
	if (!VSC9953_PORT_CHECK(port_no))
		return;

	vsc9953_l2sw.port[port_no].bus = bus;
}

void vsc9953_port_info_set_phy_address(int port_no, int address)
{
	if (!VSC9953_PORT_CHECK(port_no))
		return;

	vsc9953_l2sw.port[port_no].phyaddr = address;
}

void vsc9953_port_info_set_phy_int(int port_no, phy_interface_t phy_int)
{
	if (!VSC9953_PORT_CHECK(port_no))
		return;

	vsc9953_l2sw.port[port_no].enet_if = phy_int;
}

void vsc9953_port_enable(int port_no)
{
	if (!VSC9953_PORT_CHECK(port_no))
		return;

	vsc9953_l2sw.port[port_no].enabled = 1;
}

void vsc9953_port_disable(int port_no)
{
	if (!VSC9953_PORT_CHECK(port_no))
		return;

	vsc9953_l2sw.port[port_no].enabled = 0;
}

static void vsc9953_mdio_write(struct vsc9953_mii_mng *phyregs, int port_addr,
		int regnum, int value)
{
	int timeout = 50000;

	out_le32(&phyregs->miimcmd, (0x1 << 31) | ((port_addr & 0x1f) << 25) |
			((regnum & 0x1f) << 20) | ((value & 0xffff) << 4) |
			(0x1 << 1));
	asm("sync");

	while ((in_le32(&phyregs->miimstatus) & 0x8) && --timeout)
		udelay(1);

	if (timeout == 0)
		debug("Timeout waiting for MDIO write\n");
}

static int vsc9953_mdio_read(struct vsc9953_mii_mng *phyregs, int port_addr,
		int regnum)
{
	int value = 0xFFFF;
	int timeout = 50000;

	while ((in_le32(&phyregs->miimstatus) & MIIMIND_OPR_PEND) && --timeout)
		udelay(1);
	if (timeout == 0) {
		debug("Timeout waiting for MDIO operation to finish\n");
		return value;
	}

	/* Put the address of the phy, and the register
	 * number into MIICMD
	 */
	out_le32(&phyregs->miimcmd, (0x1 << 31) | ((port_addr & 0x1f) << 25) |
			((regnum & 0x1f) << 20) | ((value & 0xffff) << 4) |
			(0x2 << 1));

	timeout = 50000;
	/* Wait for the the indication that the read is done */
	while ((in_le32(&phyregs->miimstatus) & 0x8) && --timeout)
		udelay(1);
	if (timeout == 0)
		debug("Timeout waiting for MDIO read\n");

	/* Grab the value read from the PHY */
	value = in_le32(&phyregs->miimdata);

	if ((value & 0x00030000) == 0)
		return value & 0x0000ffff;

	return value;
}

static int init_phy(struct eth_device *dev)
{
	struct vsc9953_port_info *l2sw_port = dev->priv;
	struct phy_device *phydev = NULL;

#ifdef CONFIG_PHYLIB
	if (!l2sw_port->bus)
		return 0;
	phydev = phy_connect(l2sw_port->bus, l2sw_port->phyaddr, dev,
			l2sw_port->enet_if);
	if (!phydev) {
		printf("Failed to connect\n");
		return -1;
	}

	phydev->supported &= SUPPORTED_10baseT_Half |
			SUPPORTED_10baseT_Full |
			SUPPORTED_100baseT_Half |
			SUPPORTED_100baseT_Full |
			SUPPORTED_1000baseT_Full;
	phydev->advertising = phydev->supported;

	l2sw_port->phydev = phydev;

	phy_config(phydev);
#endif

	return 0;
}

static int vsc9953_port_init(int port_no)
{
	struct eth_device *dev;

	/* Internal ports never have a PHY */
	if (VSC9953_INTERNAL_PORT_CHECK(port_no))
		return 0;

	/* alloc eth device */
	dev = (struct eth_device *)calloc(1, sizeof(struct eth_device));
	if (!dev)
		return -ENOMEM;

	sprintf(dev->name, "SW@PORT%d", port_no);
	dev->priv = &vsc9953_l2sw.port[port_no];
	dev->init = NULL;
	dev->halt = NULL;
	dev->send = NULL;
	dev->recv = NULL;

	if (init_phy(dev)) {
		free(dev);
		return -ENODEV;
	}

	return 0;
}

static int vsc9953_vlan_table_poll_idle(void)
{
	struct vsc9953_analyzer *l2ana_reg;
	int timeout;

	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
			VSC9953_ANA_OFFSET);

	timeout = 50000;
	while (((in_le32(&l2ana_reg->ana_tables.vlan_access) &
		 VSC9953_VLAN_CMD_MASK) != VSC9953_VLAN_CMD_IDLE) && --timeout)
		udelay(1);

	return timeout ? 0 : -EBUSY;
}

/* vlan table set/clear all membership of vid */
static void vsc9953_vlan_table_membership_all_set(int vid, int set_member)
{
	uint val;
	struct vsc9953_analyzer *l2ana_reg;

	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
			VSC9953_ANA_OFFSET);

	if (vsc9953_vlan_table_poll_idle() < 0) {
		debug("VLAN table timeout\n");
		return;
	}

	/* read current vlan configuration */
	val = in_le32(&l2ana_reg->ana_tables.vlan_tidx);
	out_le32(&l2ana_reg->ana_tables.vlan_tidx,
		 bitfield_replace_by_mask(val, VSC9953_ANA_TBL_VID_MASK, vid));

	clrsetbits_le32(&l2ana_reg->ana_tables.vlan_access,
			VSC9953_VLAN_CMD_MASK, VSC9953_VLAN_CMD_READ);

	if (vsc9953_vlan_table_poll_idle() < 0) {
		debug("VLAN table timeout\n");
		return;
	}

	val = in_le32(&l2ana_reg->ana_tables.vlan_tidx);
	out_le32(&l2ana_reg->ana_tables.vlan_tidx,
		 bitfield_replace_by_mask(val, VSC9953_ANA_TBL_VID_MASK, vid));

	clrsetbits_le32(&l2ana_reg->ana_tables.vlan_access,
			VSC9953_VLAN_PORT_MASK | VSC9953_VLAN_CMD_MASK,
			VSC9953_VLAN_CMD_WRITE |
			(set_member ? VSC9953_VLAN_PORT_MASK : 0));
}

/* Set PVID for a VSC9953 port */
static void vsc9953_port_vlan_pvid_set(int port_no, int pvid)
{
	uint val;
	struct vsc9953_analyzer *l2ana_reg;
	struct vsc9953_rew_reg *l2rew_reg;

	/* Administrative down */
	if (!vsc9953_l2sw.port[port_no].enabled) {
		printf("Port %d is administrative down\n", port_no);
		return;
	}

	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
			VSC9953_ANA_OFFSET);
	l2rew_reg = (struct vsc9953_rew_reg *)(VSC9953_OFFSET +
			VSC9953_REW_OFFSET);

	/* Set PVID on ingress */
	val = in_le32(&l2ana_reg->port[port_no].vlan_cfg);
	val = bitfield_replace_by_mask(val, VSC9953_VLAN_CFG_VID_MASK, pvid);
	out_le32(&l2ana_reg->port[port_no].vlan_cfg, val);

	/* Set PVID on egress */
	val = in_le32(&l2rew_reg->port[port_no].port_vlan_cfg);
	val = bitfield_replace_by_mask(val, VSC9953_PORT_VLAN_CFG_VID_MASK,
				       pvid);
	out_le32(&l2rew_reg->port[port_no].port_vlan_cfg, val);
}

static void vsc9953_port_all_vlan_pvid_set(int pvid)
{
	int i;

	for (i = 0; i < VSC9953_MAX_PORTS; i++)
		vsc9953_port_vlan_pvid_set(i, pvid);
}

/* Enable/disable vlan aware of a VSC9953 port */
static void vsc9953_port_vlan_aware_set(int port_no, int enabled)
{
	struct vsc9953_analyzer *l2ana_reg;

	/* Administrative down */
	if (!vsc9953_l2sw.port[port_no].enabled) {
		printf("Port %d is administrative down\n", port_no);
		return;
	}

	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
			VSC9953_ANA_OFFSET);

	if (enabled)
		setbits_le32(&l2ana_reg->port[port_no].vlan_cfg,
			     VSC9953_VLAN_CFG_AWARE_ENA);
	else
		clrbits_le32(&l2ana_reg->port[port_no].vlan_cfg,
			     VSC9953_VLAN_CFG_AWARE_ENA);
}

/* Set all VSC9953 ports' vlan aware  */
static void vsc9953_port_all_vlan_aware_set(int enabled)
{
	int i;

	for (i = 0; i < VSC9953_MAX_PORTS; i++)
		vsc9953_port_vlan_aware_set(i, enabled);
}

/* Enable/disable vlan pop count of a VSC9953 port */
static void vsc9953_port_vlan_popcnt_set(int port_no, int popcnt)
{
	uint val;
	struct vsc9953_analyzer *l2ana_reg;

	/* Administrative down */
	if (!vsc9953_l2sw.port[port_no].enabled) {
		printf("Port %d is administrative down\n", port_no);
		return;
	}

	if (popcnt > 3 || popcnt < 0) {
		printf("Invalid pop count value: %d\n", port_no);
		return;
	}

	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
			VSC9953_ANA_OFFSET);

	val = in_le32(&l2ana_reg->port[port_no].vlan_cfg);
	val = bitfield_replace_by_mask(val, VSC9953_VLAN_CFG_POP_CNT_MASK,
				       popcnt);
	out_le32(&l2ana_reg->port[port_no].vlan_cfg, val);
}

/* Set all VSC9953 ports' pop count  */
static void vsc9953_port_all_vlan_poncnt_set(int popcnt)
{
	int i;

	for (i = 0; i < VSC9953_MAX_PORTS; i++)
		vsc9953_port_vlan_popcnt_set(i, popcnt);
}

/* Enable/disable learning for frames dropped due to ingress filtering */
static void vsc9953_vlan_ingr_fltr_learn_drop(int enable)
{
	struct vsc9953_analyzer *l2ana_reg;

	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
			VSC9953_ANA_OFFSET);

	if (enable)
		setbits_le32(&l2ana_reg->ana.adv_learn, VSC9953_VLAN_CHK);
	else
		clrbits_le32(&l2ana_reg->ana.adv_learn, VSC9953_VLAN_CHK);
}

/* Egress untag modes of a VSC9953 port */
enum egress_untag_mode {
	EGRESS_UNTAG_ALL = 0,
	EGRESS_UNTAG_PVID_AND_ZERO,
	EGRESS_UNTAG_ZERO,
	EGRESS_UNTAG_NONE,
};

static void vsc9953_port_vlan_egr_untag_set(int port_no,
					    enum egress_untag_mode mode)
{
	struct vsc9953_rew_reg *l2rew_reg;

	/* Administrative down */
	if (!vsc9953_l2sw.port[port_no].enabled) {
		printf("Port %d is administrative down\n", port_no);
		return;
	}

	l2rew_reg = (struct vsc9953_rew_reg *)(VSC9953_OFFSET +
			VSC9953_REW_OFFSET);

	switch (mode) {
	case EGRESS_UNTAG_ALL:
		clrsetbits_le32(&l2rew_reg->port[port_no].port_tag_cfg,
				VSC9953_TAG_CFG_MASK, VSC9953_TAG_CFG_NONE);
		break;
	case EGRESS_UNTAG_PVID_AND_ZERO:
		clrsetbits_le32(&l2rew_reg->port[port_no].port_tag_cfg,
				VSC9953_TAG_CFG_MASK,
				VSC9953_TAG_CFG_ALL_BUT_PVID_ZERO);
		break;
	case EGRESS_UNTAG_ZERO:
		clrsetbits_le32(&l2rew_reg->port[port_no].port_tag_cfg,
				VSC9953_TAG_CFG_MASK,
				VSC9953_TAG_CFG_ALL_BUT_ZERO);
		break;
	case EGRESS_UNTAG_NONE:
		clrsetbits_le32(&l2rew_reg->port[port_no].port_tag_cfg,
				VSC9953_TAG_CFG_MASK, VSC9953_TAG_CFG_ALL);
		break;
	default:
		printf("Unknown untag mode for port %d\n", port_no);
	}
}

static void vsc9953_port_all_vlan_egress_untagged_set(
		enum egress_untag_mode mode)
{
	int i;

	for (i = 0; i < VSC9953_MAX_PORTS; i++)
		vsc9953_port_vlan_egr_untag_set(i, mode);
}

#ifdef CONFIG_CMD_ETHSW

/* Enable/disable status of a VSC9953 port */
static void vsc9953_port_status_set(int port_no, u8 enabled)
{
	struct vsc9953_qsys_reg *l2qsys_reg;

	/* Administrative down */
	if (!vsc9953_l2sw.port[port_no].enabled)
		return;

	l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
			VSC9953_QSYS_OFFSET);

	if (enabled)
		setbits_le32(&l2qsys_reg->sys.switch_port_mode[port_no],
			     VSC9953_PORT_ENA);
	else
		clrbits_le32(&l2qsys_reg->sys.switch_port_mode[port_no],
			     VSC9953_PORT_ENA);
}

/* Start autonegotiation for a VSC9953 PHY */
static void vsc9953_phy_autoneg(int port_no)
{
	if (!vsc9953_l2sw.port[port_no].phydev)
		return;

	if (vsc9953_l2sw.port[port_no].phydev->drv->startup(
			vsc9953_l2sw.port[port_no].phydev))
		printf("Failed to start PHY for port %d\n", port_no);
}

/* Print a VSC9953 port's configuration */
static void vsc9953_port_config_show(int port_no)
{
	int speed;
	int duplex;
	int link;
	u8 enabled;
	u32 val;
	struct vsc9953_qsys_reg *l2qsys_reg;

	l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
			VSC9953_QSYS_OFFSET);

	val = in_le32(&l2qsys_reg->sys.switch_port_mode[port_no]);
	enabled = vsc9953_l2sw.port[port_no].enabled &&
		  (val & VSC9953_PORT_ENA);

	/* internal ports (8 and 9) are fixed */
	if (VSC9953_INTERNAL_PORT_CHECK(port_no)) {
		link = 1;
		speed = SPEED_2500;
		duplex = DUPLEX_FULL;
	} else {
		if (vsc9953_l2sw.port[port_no].phydev) {
			link = vsc9953_l2sw.port[port_no].phydev->link;
			speed = vsc9953_l2sw.port[port_no].phydev->speed;
			duplex = vsc9953_l2sw.port[port_no].phydev->duplex;
		} else {
			link = -1;
			speed = -1;
			duplex = -1;
		}
	}

	printf("%8d ", port_no);
	printf("%8s ", enabled == 1 ? "enabled" : "disabled");
	printf("%8s ", link == 1 ? "up" : "down");

	switch (speed) {
	case SPEED_10:
		printf("%8d ", 10);
		break;
	case SPEED_100:
		printf("%8d ", 100);
		break;
	case SPEED_1000:
		printf("%8d ", 1000);
		break;
	case SPEED_2500:
		printf("%8d ", 2500);
		break;
	case SPEED_10000:
		printf("%8d ", 10000);
		break;
	default:
		printf("%8s ", "-");
	}

	printf("%8s\n", duplex == DUPLEX_FULL ? "full" : "half");
}

/* Show VSC9953 ports' statistics */
static void vsc9953_port_statistics_show(int port_no)
{
	u32 rx_val;
	u32 tx_val;
	struct vsc9953_system_reg *l2sys_reg;

	/* Administrative down */
	if (!vsc9953_l2sw.port[port_no].enabled) {
		printf("Port %d is administrative down\n", port_no);
		return;
	}

	l2sys_reg = (struct vsc9953_system_reg *)(VSC9953_OFFSET +
			VSC9953_SYS_OFFSET);

	printf("Statistics for L2 Switch port %d:\n", port_no);

	/* Set counter view for our port */
	out_le32(&l2sys_reg->sys.stat_cfg, port_no);

#define VSC9953_STATS_PRINTF "%-15s %10u"

	/* Get number of Rx and Tx frames */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_short) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_frag) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_jabber) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_long) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_64) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_65_127) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_128_255) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_256_511) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_512_1023) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_1024_1526) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_jumbo);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_64) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_65_127) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_128_255) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_256_511) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_512_1023) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_1024_1526) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_jumbo);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx frames:", rx_val, "Tx frames:", tx_val);

	/* Get number of Rx and Tx bytes */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_oct);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_oct);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx bytes:", rx_val, "Tx bytes:", tx_val);

	/* Get number of Rx frames received ok and Tx frames sent ok */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_yellow_prio_0) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_yellow_prio_1) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_yellow_prio_2) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_yellow_prio_3) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_yellow_prio_4) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_yellow_prio_5) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_yellow_prio_6) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_yellow_prio_7) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_green_prio_0) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_green_prio_1) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_green_prio_2) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_green_prio_3) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_green_prio_4) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_green_prio_5) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_green_prio_6) +
		 in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_green_prio_7);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_64) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_65_127) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_128_255) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_256_511) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_512_1023) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_1024_1526) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_jumbo);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx frames ok:", rx_val, "Tx frames ok:", tx_val);

	/* Get number of Rx and Tx unicast frames */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_uc);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_uc);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx unicast:", rx_val, "Tx unicast:", tx_val);

	/* Get number of Rx and Tx broadcast frames */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_bc);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_bc);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx broadcast:", rx_val, "Tx broadcast:", tx_val);

	/* Get number of Rx and Tx frames of 64B */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_64);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_64);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx 64B:", rx_val, "Tx 64B:", tx_val);

	/* Get number of Rx and Tx frames with sizes between 65B and 127B */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_65_127);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_65_127);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx 65B-127B:", rx_val, "Tx 65B-127B:", tx_val);

	/* Get number of Rx and Tx frames with sizes between 128B and 255B */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_128_255);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_128_255);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx 128B-255B:", rx_val, "Tx 128B-255B:", tx_val);

	/* Get number of Rx and Tx frames with sizes between 256B and 511B */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_256_511);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_256_511);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx 256B-511B:", rx_val, "Tx 256B-511B:", tx_val);

	/* Get number of Rx and Tx frames with sizes between 512B and 1023B */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_512_1023);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_512_1023);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx 512B-1023B:", rx_val, "Tx 512B-1023B:", tx_val);

	/* Get number of Rx and Tx frames with sizes between 1024B and 1526B */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_1024_1526);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_1024_1526);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx 1024B-1526B:", rx_val, "Tx 1024B-1526B:", tx_val);

	/* Get number of Rx and Tx jumbo frames */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_sz_jumbo);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_sz_jumbo);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx jumbo:", rx_val, "Tx jumbo:", tx_val);

	/* Get number of Rx and Tx dropped frames */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_cat_drop) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_tail) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_yellow_prio_0) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_yellow_prio_1) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_yellow_prio_2) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_yellow_prio_3) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_yellow_prio_4) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_yellow_prio_5) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_yellow_prio_6) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_yellow_prio_7) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_green_prio_0) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_green_prio_1) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_green_prio_2) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_green_prio_3) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_green_prio_4) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_green_prio_5) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_green_prio_6) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_green_prio_7);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_drop) +
		 in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_aged);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx drops:", rx_val, "Tx drops:", tx_val);

	/*
	 * Get number of Rx frames with CRC or alignment errors
	 * and number of detected Tx collisions
	 */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_crc);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_col);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx CRC&align:", rx_val, "Tx coll:", tx_val);

	/*
	 * Get number of Rx undersized frames and
	 * number of Tx aged frames
	 */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_short);
	tx_val = in_le32(&l2sys_reg->stat.tx_cntrs.c_tx_aged);
	printf(VSC9953_STATS_PRINTF"\t\t"VSC9953_STATS_PRINTF"\n",
	       "Rx undersize:", rx_val, "Tx aged:", tx_val);

	/* Get number of Rx oversized frames */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_long);
	printf(VSC9953_STATS_PRINTF"\n", "Rx oversized:", rx_val);

	/* Get number of Rx fragmented frames */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_frag);
	printf(VSC9953_STATS_PRINTF"\n", "Rx fragments:", rx_val);

	/* Get number of Rx jabber errors */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_jabber);
	printf(VSC9953_STATS_PRINTF"\n", "Rx jabbers:", rx_val);

	/*
	 * Get number of Rx frames filtered due to classification rules or
	 * no destination ports
	 */
	rx_val = in_le32(&l2sys_reg->stat.rx_cntrs.c_rx_cat_drop) +
		 in_le32(&l2sys_reg->stat.drop_cntrs.c_dr_local);
	printf(VSC9953_STATS_PRINTF"\n", "Rx filtered:", rx_val);

	printf("\n");
}

/* Clear statistics for a VSC9953 port */
static void vsc9953_port_statistics_clear(int port_no)
{
	struct vsc9953_system_reg *l2sys_reg;

	/* Administrative down */
	if (!vsc9953_l2sw.port[port_no].enabled) {
		printf("Port %d is administrative down\n", port_no);
		return;
	}

	l2sys_reg = (struct vsc9953_system_reg *)(VSC9953_OFFSET +
			VSC9953_SYS_OFFSET);

	/* Clear all counter groups for our ports */
	out_le32(&l2sys_reg->sys.stat_cfg, port_no |
		 VSC9953_STAT_CLEAR_RX | VSC9953_STAT_CLEAR_TX |
		 VSC9953_STAT_CLEAR_DR);
}

static int vsc9953_port_status_key_func(struct ethsw_command_def *parsed_cmd)
{
	int i;
	u8 enabled;

	/* Last keyword should tell us if we should enable/disable the port */
	if (parsed_cmd->cmd_to_keywords[parsed_cmd->cmd_keywords_nr - 1] ==
	    ethsw_id_enable)
		enabled = 1;
	else if (parsed_cmd->cmd_to_keywords[parsed_cmd->cmd_keywords_nr - 1] ==
		 ethsw_id_disable)
		enabled = 0;
	else
		return CMD_RET_USAGE;

	if (parsed_cmd->port != ETHSW_CMD_PORT_ALL) {
		if (!VSC9953_PORT_CHECK(parsed_cmd->port)) {
			printf("Invalid port number: %d\n", parsed_cmd->port);
			return CMD_RET_FAILURE;
		}
		vsc9953_port_status_set(parsed_cmd->port, enabled);
	} else {
		for (i = 0; i < VSC9953_MAX_PORTS; i++)
			vsc9953_port_status_set(i, enabled);
	}

	return CMD_RET_SUCCESS;
}

static int vsc9953_port_config_key_func(struct ethsw_command_def *parsed_cmd)
{
	int i;

	if (parsed_cmd->port != ETHSW_CMD_PORT_ALL) {
		if (!VSC9953_PORT_CHECK(parsed_cmd->port)) {
			printf("Invalid port number: %d\n", parsed_cmd->port);
			return CMD_RET_FAILURE;
		}
		vsc9953_phy_autoneg(parsed_cmd->port);
		printf("%8s %8s %8s %8s %8s\n",
		       "Port", "Status", "Link", "Speed",
		       "Duplex");
		vsc9953_port_config_show(parsed_cmd->port);

	} else {
		for (i = 0; i < VSC9953_MAX_PORTS; i++)
			vsc9953_phy_autoneg(i);
		printf("%8s %8s %8s %8s %8s\n",
		       "Port", "Status", "Link", "Speed", "Duplex");
		for (i = 0; i < VSC9953_MAX_PORTS; i++)
			vsc9953_port_config_show(i);
	}

	return CMD_RET_SUCCESS;
}

static int vsc9953_port_stats_key_func(struct ethsw_command_def *parsed_cmd)
{
	int i;

	if (parsed_cmd->port != ETHSW_CMD_PORT_ALL) {
		if (!VSC9953_PORT_CHECK(parsed_cmd->port)) {
			printf("Invalid port number: %d\n", parsed_cmd->port);
			return CMD_RET_FAILURE;
		}
		vsc9953_port_statistics_show(parsed_cmd->port);
	} else {
		for (i = 0; i < VSC9953_MAX_PORTS; i++)
			vsc9953_port_statistics_show(i);
	}

	return CMD_RET_SUCCESS;
}

static int vsc9953_port_stats_clear_key_func(struct ethsw_command_def
					     *parsed_cmd)
{
	int i;

	if (parsed_cmd->port != ETHSW_CMD_PORT_ALL) {
		if (!VSC9953_PORT_CHECK(parsed_cmd->port)) {
			printf("Invalid port number: %d\n", parsed_cmd->port);
			return CMD_RET_FAILURE;
		}
		vsc9953_port_statistics_clear(parsed_cmd->port);
	} else {
		for (i = 0; i < VSC9953_MAX_PORTS; i++)
			vsc9953_port_statistics_clear(i);
	}

	return CMD_RET_SUCCESS;
}

static struct ethsw_command_func vsc9953_cmd_func = {
		.ethsw_name = "L2 Switch VSC9953",
		.port_enable = &vsc9953_port_status_key_func,
		.port_disable = &vsc9953_port_status_key_func,
		.port_show = &vsc9953_port_config_key_func,
		.port_stats = &vsc9953_port_stats_key_func,
		.port_stats_clear = &vsc9953_port_stats_clear_key_func,
};

#endif /* CONFIG_CMD_ETHSW */

/*****************************************************************************
At startup, the default configuration would be:
	- HW learning enabled on all ports; (HW default)
	- All ports are in VLAN 1;
	- All ports are VLAN aware;
	- All ports have POP_COUNT 1;
	- All ports have PVID 1;
	- All ports have TPID 0x8100; (HW default)
	- All ports tag frames classified to all VLANs that are not PVID;
*****************************************************************************/
void vsc9953_default_configuration(void)
{
	int i;

	for (i = 0; i < VSC9953_MAX_VLAN; i++)
		vsc9953_vlan_table_membership_all_set(i, 0);
	vsc9953_port_all_vlan_aware_set(1);
	vsc9953_port_all_vlan_pvid_set(1);
	vsc9953_port_all_vlan_poncnt_set(1);
	vsc9953_vlan_table_membership_all_set(1, 1);
	vsc9953_vlan_ingr_fltr_learn_drop(1);
	vsc9953_port_all_vlan_egress_untagged_set(EGRESS_UNTAG_PVID_AND_ZERO);
}

void vsc9953_init(bd_t *bis)
{
	u32 i;
	u32 hdx_cfg = 0;
	u32 phy_addr = 0;
	int timeout;
	struct vsc9953_system_reg *l2sys_reg;
	struct vsc9953_qsys_reg *l2qsys_reg;
	struct vsc9953_dev_gmii *l2dev_gmii_reg;
	struct vsc9953_analyzer *l2ana_reg;
	struct vsc9953_devcpu_gcb *l2dev_gcb;

	l2dev_gmii_reg = (struct vsc9953_dev_gmii *)(VSC9953_OFFSET +
			VSC9953_DEV_GMII_OFFSET);

	l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
			VSC9953_ANA_OFFSET);

	l2sys_reg = (struct vsc9953_system_reg *)(VSC9953_OFFSET +
			VSC9953_SYS_OFFSET);

	l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
			VSC9953_QSYS_OFFSET);

	l2dev_gcb = (struct vsc9953_devcpu_gcb *)(VSC9953_OFFSET +
			VSC9953_DEVCPU_GCB);

	out_le32(&l2dev_gcb->chip_regs.soft_rst,
		 VSC9953_SOFT_SWC_RST_ENA);
	timeout = 50000;
	while ((in_le32(&l2dev_gcb->chip_regs.soft_rst) &
			VSC9953_SOFT_SWC_RST_ENA) && --timeout)
		udelay(1); /* busy wait for vsc9953 soft reset */
	if (timeout == 0)
		debug("Timeout waiting for VSC9953 to reset\n");

	out_le32(&l2sys_reg->sys.reset_cfg, VSC9953_MEM_ENABLE |
		 VSC9953_MEM_INIT);

	timeout = 50000;
	while ((in_le32(&l2sys_reg->sys.reset_cfg) &
		VSC9953_MEM_INIT) && --timeout)
		udelay(1); /* busy wait for vsc9953 memory init */
	if (timeout == 0)
		debug("Timeout waiting for VSC9953 memory to initialize\n");

	out_le32(&l2sys_reg->sys.reset_cfg, (in_le32(&l2sys_reg->sys.reset_cfg)
			| VSC9953_CORE_ENABLE));

	/* VSC9953 Setting to be done once only */
	out_le32(&l2qsys_reg->sys.ext_cpu_cfg, 0x00000b00);

	for (i = 0; i < VSC9953_MAX_PORTS; i++) {
		if (vsc9953_port_init(i))
			printf("Failed to initialize l2switch port %d\n", i);

		/* Enable VSC9953 GMII Ports Port ID 0 - 7 */
		if (VSC9953_INTERNAL_PORT_CHECK(i)) {
			out_le32(&l2ana_reg->pfc[i].pfc_cfg,
				 VSC9953_PFC_FC_QSGMII);
			out_le32(&l2sys_reg->pause_cfg.mac_fc_cfg[i],
				 VSC9953_MAC_FC_CFG_QSGMII);
		} else {
			out_le32(&l2ana_reg->pfc[i].pfc_cfg,
				 VSC9953_PFC_FC);
			out_le32(&l2sys_reg->pause_cfg.mac_fc_cfg[i],
				 VSC9953_MAC_FC_CFG);
		}
		out_le32(&l2dev_gmii_reg->port_mode.clock_cfg,
			 VSC9953_CLOCK_CFG);
		out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_ena_cfg,
			 VSC9953_MAC_ENA_CFG);
		out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_mode_cfg,
			 VSC9953_MAC_MODE_CFG);
		out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_ifg_cfg,
			 VSC9953_MAC_IFG_CFG);
		/* mac_hdx_cfg varies with port id*/
		hdx_cfg = VSC9953_MAC_HDX_CFG | (i << 16);
		out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_hdx_cfg, hdx_cfg);
		out_le32(&l2sys_reg->sys.front_port_mode[i],
			 VSC9953_FRONT_PORT_MODE);
		setbits_le32(&l2qsys_reg->sys.switch_port_mode[i],
			     VSC9953_PORT_ENA);
		out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_maxlen_cfg,
			 VSC9953_MAC_MAX_LEN);
		out_le32(&l2sys_reg->pause_cfg.pause_cfg[i],
			 VSC9953_PAUSE_CFG);
		/* WAIT FOR 2 us*/
		udelay(2);

		l2dev_gmii_reg = (struct vsc9953_dev_gmii *)(
				(char *)l2dev_gmii_reg
				+ T1040_SWITCH_GMII_DEV_OFFSET);

		/* Initialize Lynx PHY Wrappers */
		phy_addr = 0;
		if (vsc9953_l2sw.port[i].enet_if ==
				PHY_INTERFACE_MODE_QSGMII)
			phy_addr = (i + 0x4) & 0x1F;
		else if (vsc9953_l2sw.port[i].enet_if ==
				PHY_INTERFACE_MODE_SGMII)
			phy_addr = (i + 1) & 0x1F;

		if (phy_addr) {
			/* SGMII IF mode + AN enable */
			vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
					   0x14, PHY_SGMII_IF_MODE_AN |
					   PHY_SGMII_IF_MODE_SGMII);
			/* Dev ability according to SGMII specification */
			vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
					   0x4, PHY_SGMII_DEV_ABILITY_SGMII);
			/* Adjust link timer for SGMII
			 * 1.6 ms in units of 8 ns = 2 * 10^5 = 0x30d40
			 */
			vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
					   0x13, 0x0003);
			vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
					   0x12, 0x0d40);
			/* Restart AN */
			vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
					   0x0, PHY_SGMII_CR_DEF_VAL |
					   PHY_SGMII_CR_RESET_AN);

			timeout = 50000;
			while ((vsc9953_mdio_read(&l2dev_gcb->mii_mng[0],
					phy_addr, 0x01) & 0x0020) && --timeout)
				udelay(1); /* wait for AN to complete */
			if (timeout == 0)
				debug("Timeout waiting for AN to complete\n");
		}
	}

	vsc9953_default_configuration();

#ifdef CONFIG_CMD_ETHSW
	if (ethsw_define_functions(&vsc9953_cmd_func) < 0)
		debug("Unable to use \"ethsw\" commands\n");
#endif

	printf("VSC9953 L2 switch initialized\n");
	return;
}
OpenPOWER on IntegriCloud