aboutsummaryrefslogtreecommitdiff
path: root/sys/contrib/octeon-sdk/cvmx-helper-board.c
blob: 2582dae3f3bba20e17876bc545d1e0312581de73 (plain) (blame)
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
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
/***********************license start***************
 * Copyright (c) 2003-2011  Cavium Inc. (support@cavium.com). All rights
 * reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.

 *   * Neither the name of Cavium Inc. nor the names of
 *     its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written
 *     permission.

 * This Software, including technical data, may be subject to U.S. export  control
 * laws, including the U.S. Export Administration Act and its  associated
 * regulations, and may be subject to export or import  regulations in other
 * countries.

 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
 ***********************license end**************************************/







/**
 * @file
 *
 * Helper functions to abstract board specific data about
 * network ports from the rest of the cvmx-helper files.
 *
 * <hr>$Revision: 70030 $<hr>
 */
#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
#include <linux/module.h>
#include <asm/octeon/cvmx.h>
#include <asm/octeon/cvmx-bootinfo.h>
#include <asm/octeon/cvmx-smix-defs.h>
#include <asm/octeon/cvmx-gmxx-defs.h>
#include <asm/octeon/cvmx-asxx-defs.h>
#include <asm/octeon/cvmx-mdio.h>
#include <asm/octeon/cvmx-helper.h>
#include <asm/octeon/cvmx-helper-util.h>
#include <asm/octeon/cvmx-helper-board.h>
#include <asm/octeon/cvmx-twsi.h>
#else
#include "cvmx.h"
#include "cvmx-app-init.h"
#include "cvmx-sysinfo.h"
#include "cvmx-twsi.h"
#include "cvmx-mdio.h"
#include "cvmx-helper.h"
#include "cvmx-helper-util.h"
#include "cvmx-helper-board.h"
#include "cvmx-gpio.h"
#if !defined(__FreeBSD__) || !defined(_KERNEL)
#ifdef __U_BOOT__
# include <libfdt.h>
#else
# include "libfdt/libfdt.h"
#endif
#endif
#include "cvmx-swap.h"
#endif

/**
 * cvmx_override_board_link_get(int ipd_port) is a function
 * pointer. It is meant to allow customization of the process of
 * talking to a PHY to determine link speed. It is called every
 * time a PHY must be polled for link status. Users should set
 * this pointer to a function before calling any cvmx-helper
 * operations.
 */
CVMX_SHARED cvmx_helper_link_info_t (*cvmx_override_board_link_get)(int ipd_port) = NULL;

#if !defined(CVMX_BUILD_FOR_LINUX_KERNEL) && (!defined(__FreeBSD__) || !defined(_KERNEL))

static void cvmx_retry_i2c_write(int twsi_id, uint8_t dev_addr, uint16_t internal_addr, int num_bytes, int ia_width_bytes, uint64_t data)
{
    int tries = 3;
    int r;
    do {
        r = cvmx_twsix_write_ia(twsi_id, dev_addr, internal_addr, num_bytes, ia_width_bytes, data);
    } while (tries-- > 0 && r < 0);
}

static int __pip_eth_node(const void *fdt_addr, int aliases, int ipd_port)
{
    char name_buffer[20];
    const char*pip_path;
    int pip, iface, eth;
    int interface_num    = cvmx_helper_get_interface_num(ipd_port);
    int interface_index  = cvmx_helper_get_interface_index_num(ipd_port);

    pip_path = fdt_getprop(fdt_addr, aliases, "pip", NULL);
    if (!pip_path)
    {
        cvmx_dprintf("ERROR: pip path not found in device tree\n");
        return -1;
    }
    pip = fdt_path_offset(fdt_addr, pip_path);
    if (pip < 0)
    {
        cvmx_dprintf("ERROR: pip not found in device tree\n");
        return -1;
    }
#ifdef __U_BOOT__
    sprintf(name_buffer, "interface@%d", interface_num);
#else
    snprintf(name_buffer, sizeof(name_buffer), "interface@%d", interface_num);
#endif
    iface =  fdt_subnode_offset(fdt_addr, pip, name_buffer);
    if (iface < 0)
    {
        cvmx_dprintf("ERROR : pip intf %d not found in device tree \n",
                     interface_num);
        return -1;
    }
#ifdef __U_BOOT__
    sprintf(name_buffer, "ethernet@%x", interface_index);
#else
    snprintf(name_buffer, sizeof(name_buffer), "ethernet@%x", interface_index);
#endif
    eth = fdt_subnode_offset(fdt_addr, iface, name_buffer);
    if (eth < 0)
    {
        cvmx_dprintf("ERROR : pip interface@%d ethernet@%d not found in device "
                     "tree\n", interface_num, interface_index);
        return -1;
    }
    return eth;
}

static int __mix_eth_node(const void *fdt_addr, int aliases, int interface_index)
{
    char name_buffer[20];
    const char*mix_path;
    int mix;

#ifdef __U_BOOT__
    sprintf(name_buffer, "mix%d", interface_index);
#else
    snprintf(name_buffer, sizeof(name_buffer), "mix%d", interface_index);
#endif
    mix_path = fdt_getprop(fdt_addr, aliases, name_buffer, NULL);
    if (!mix_path)
    {
        cvmx_dprintf("ERROR: mix%d path not found in device tree\n",interface_index);
    }
    mix = fdt_path_offset(fdt_addr, mix_path);
    if (mix < 0)
    {
        cvmx_dprintf("ERROR: %s not found in device tree\n", mix_path);
        return -1;
    }
    return mix;
}

typedef struct cvmx_phy_info
{
    int phy_addr;
    int direct_connect;
    cvmx_phy_type_t phy_type;
}cvmx_phy_info_t;


static int __mdiobus_addr_to_unit(uint32_t addr)
{
    int unit = (addr >> 7) & 3;
    if (!OCTEON_IS_MODEL(OCTEON_CN68XX))
        unit >>= 1;
    return unit;
}
/**
 * Return the MII PHY address associated with the given IPD
 * port. The phy address is obtained from the device tree.
 *
 * @param ipd_port Octeon IPD port to get the MII address for.
 *
 * @return MII PHY address and bus number or -1.
 */

static cvmx_phy_info_t __get_phy_info_from_dt(int ipd_port)
{
    const void *fdt_addr = CASTPTR(const void *, cvmx_sysinfo_get()->fdt_addr);
    uint32_t *phy_handle;
    int aliases, eth, phy, phy_parent, phandle, ret;
    cvmx_phy_info_t phy_info;
    int mdio_unit=-1;
    const char *phy_comaptible_str;
    uint32_t *phy_addr_ptr;

    phy_info.phy_addr = -1;
    phy_info.direct_connect = -1;
    phy_info.phy_type = (cvmx_phy_type_t) -1;

    if (!fdt_addr)
    {
        cvmx_dprintf("No device tree found.\n");
        return phy_info;
    }
    aliases = fdt_path_offset(fdt_addr, "/aliases");
    if (aliases < 0) {
        cvmx_dprintf("Error: No /aliases node in device tree.\n");
        return phy_info;
    }
    if (ipd_port < 0)
    {
        int interface_index = ipd_port - CVMX_HELPER_BOARD_MGMT_IPD_PORT;
        eth = __mix_eth_node(fdt_addr, aliases, interface_index) ;
    }
    else
    {
        eth = __pip_eth_node(fdt_addr, aliases, ipd_port);
    }
    if (eth < 0 )
    {
        cvmx_dprintf("ERROR : cannot find interface for ipd_port=%d\n", ipd_port);
        return phy_info;
    }
    /* Get handle to phy */
    phy_handle = (uint32_t *) fdt_getprop(fdt_addr, eth, "phy-handle", NULL);
    if (!phy_handle)
    {
        cvmx_dprintf("ERROR : phy handle not found in device tree ipd_port=%d"
                     "\n", ipd_port);
        return phy_info;
    }
    phandle = cvmx_be32_to_cpu(*phy_handle);
    phy = fdt_node_offset_by_phandle(fdt_addr, phandle);
    if (phy < 0)
    {
        cvmx_dprintf("ERROR : cannot find phy for ipd_port=%d ret=%d\n",
                     ipd_port, phy);
        return phy_info;
    }
    phy_comaptible_str = (const char *) fdt_getprop(fdt_addr, phy,
                                                    "compatible", NULL);
    if (!phy_comaptible_str)
    {
        cvmx_dprintf("ERROR : no compatible prop in phy\n");
        return phy_info;
    }
    if (memcmp("marvell", phy_comaptible_str, strlen("marvell")) == 0)
    {
        phy_info.phy_type = MARVELL_GENERIC_PHY;
    }
    else if (memcmp("broadcom", phy_comaptible_str, strlen("broadcom")) == 0)
    {
        phy_info.phy_type = BROADCOM_GENERIC_PHY;
    }
    else
    {
        phy_info.phy_type = -1;
    }

    /* Check if PHY parent is the octeon MDIO bus. Some boards are connected
       though a MUX and for them direct_connect_to_phy will be 0 */
    phy_parent = fdt_parent_offset(fdt_addr, phy);
    if (phy_parent < 0)
    {
        cvmx_dprintf("ERROR : cannot find phy parent for ipd_port=%d ret=%d\n",
                     ipd_port, phy_parent);
        return phy_info;
    }
    ret = fdt_node_check_compatible(fdt_addr, phy_parent,
                                    "cavium,octeon-3860-mdio");
    if (ret == 0)
    {
        phy_info.direct_connect = 1 ;
        uint32_t *mdio_reg_base = (uint32_t *) fdt_getprop(fdt_addr, phy_parent,"reg",0);
        if (mdio_reg_base == 0)
        {
            cvmx_dprintf("ERROR : unable to get reg property in phy mdio\n");
            return phy_info;
        }
        mdio_unit = __mdiobus_addr_to_unit(mdio_reg_base[1]);
        //cvmx_dprintf("phy parent=%s reg_base=%08x unit=%d \n",
        //             fdt_get_name(fdt_addr,phy_parent, NULL), mdio_reg_base[1], mdio_unit);
    }
    else
    {
        phy_info.direct_connect = 0;
        /* The PHY is not directly connected to the Octeon MDIO bus.
           SE doesn't  have abstractions for MDIO MUX or MDIO MUX drivers and
           hence for the non direct cases code will be needed which is
           board specific.
           For now the the MDIO Unit is defaulted to 1.
        */
        mdio_unit = 1;
    }

    phy_addr_ptr = (uint32_t *) fdt_getprop(fdt_addr, phy, "reg", NULL);
    phy_info.phy_addr = cvmx_be32_to_cpu(*phy_addr_ptr) | mdio_unit << 8;
    return phy_info;

}

/**
 * Return the MII PHY address associated with the given IPD
 * port. The phy address is obtained from the device tree.
 *
 * @param ipd_port Octeon IPD port to get the MII address for.
 *
 * @return MII PHY address and bus number or -1.
 */

int cvmx_helper_board_get_mii_address_from_dt(int ipd_port)
{
        cvmx_phy_info_t phy_info = __get_phy_info_from_dt(ipd_port);
        return phy_info.phy_addr;
}
#endif

/**
 * Return the MII PHY address associated with the given IPD
 * port. A result of -1 means there isn't a MII capable PHY
 * connected to this port. On chips supporting multiple MII
 * busses the bus number is encoded in bits <15:8>.
 *
 * This function must be modified for every new Octeon board.
 * Internally it uses switch statements based on the cvmx_sysinfo
 * data to determine board types and revisions. It replies on the
 * fact that every Octeon board receives a unique board type
 * enumeration from the bootloader.
 *
 * @param ipd_port Octeon IPD port to get the MII address for.
 *
 * @return MII PHY address and bus number or -1.
 */
int cvmx_helper_board_get_mii_address(int ipd_port)
{
    /*
     * Board types we have to know at compile-time.
     */
#ifdef OCTEON_BOARD_CAPK_0100ND
    switch (ipd_port) {
    case 0:
	return 2;
    case 1:
	return 3;
    case 2:
	/* XXX Switch PHY?  */
	return -1;
    default:
	return -1;
    }
#endif

    /*
     * For board types we can determine at runtime.
     */
    if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
        return -1;
#if !defined(CVMX_BUILD_FOR_LINUX_KERNEL) && (!defined(__FreeBSD__) || !defined(_KERNEL))
    if (cvmx_sysinfo_get()->fdt_addr)
    {
        cvmx_phy_info_t phy_info = __get_phy_info_from_dt(ipd_port);
        //cvmx_dprintf("ipd_port=%d phy_addr=%d\n", ipd_port, phy_info.phy_addr);
        if (phy_info.phy_addr >= 0) return phy_info.phy_addr;
    }
#endif
    switch (cvmx_sysinfo_get()->board_type)
    {
        case CVMX_BOARD_TYPE_SIM:
            /* Simulator doesn't have MII */
            return -1;
        case CVMX_BOARD_TYPE_EBT3000:
        case CVMX_BOARD_TYPE_EBT5800:
        case CVMX_BOARD_TYPE_THUNDER:
        case CVMX_BOARD_TYPE_NICPRO2:
            /* Interface 0 is SPI4, interface 1 is RGMII */
            if ((ipd_port >= 16) && (ipd_port < 20))
                return ipd_port - 16;
            else
                return -1;
        case CVMX_BOARD_TYPE_LANAI2_A:
            if (ipd_port == 0)
                return 0;
            else
                return -1;
        case CVMX_BOARD_TYPE_LANAI2_U:
        case CVMX_BOARD_TYPE_LANAI2_G:
            if (ipd_port == 0)
                return 0x1c;
            else
                return -1;
        case CVMX_BOARD_TYPE_KODAMA:
        case CVMX_BOARD_TYPE_EBH3100:
        case CVMX_BOARD_TYPE_HIKARI:
        case CVMX_BOARD_TYPE_CN3010_EVB_HS5:
        case CVMX_BOARD_TYPE_CN3005_EVB_HS5:
        case CVMX_BOARD_TYPE_CN3020_EVB_HS5:
            /* Port 0 is WAN connected to a PHY, Port 1 is GMII connected to a
                switch */
            if (ipd_port == 0)
                return 4;
            else if (ipd_port == 1)
                return 9;
            else
                return -1;
        case CVMX_BOARD_TYPE_EBH3000:
            /* Board has dual SPI4 and no PHYs */
            return -1;
        case CVMX_BOARD_TYPE_EBT5810:
            /* Board has 10g PHYs hooked up to the MII controller on the
            ** IXF18201 MAC.  The 10G PHYS use clause 45 MDIO which the CN58XX
            ** does not support. All MII accesses go through the IXF part. */
            return -1;
        case CVMX_BOARD_TYPE_EBH5200:
        case CVMX_BOARD_TYPE_EBH5201:
        case CVMX_BOARD_TYPE_EBT5200:
            /* Board has 2 management ports */
            if ((ipd_port >= CVMX_HELPER_BOARD_MGMT_IPD_PORT) && (ipd_port < (CVMX_HELPER_BOARD_MGMT_IPD_PORT + 2)))
                return ipd_port - CVMX_HELPER_BOARD_MGMT_IPD_PORT;
            /* Board has 4 SGMII ports. The PHYs start right after the MII
                ports MII0 = 0, MII1 = 1, SGMII = 2-5 */
            if ((ipd_port >= 0) && (ipd_port < 4))
                return ipd_port+2;
            else
                return -1;
        case CVMX_BOARD_TYPE_EBH5600:
        case CVMX_BOARD_TYPE_EBH5601:
        case CVMX_BOARD_TYPE_EBH5610:
            /* Board has 1 management port */
            if (ipd_port == CVMX_HELPER_BOARD_MGMT_IPD_PORT)
                return 0;
            /* Board has 8 SGMII ports. 4 connect out, two connect to a switch,
                and 2 loop to each other */
            if ((ipd_port >= 0) && (ipd_port < 4))
                return ipd_port+1;
            else
                return -1;
        case CVMX_BOARD_TYPE_EBT5600:
	    /* Board has 1 management port */
            if (ipd_port == CVMX_HELPER_BOARD_MGMT_IPD_PORT)
                return 0;
	    /* Board has 1 XAUI port connected to a switch.  */
	    return -1;
        case CVMX_BOARD_TYPE_EBB5600:
            {
                static unsigned char qlm_switch_addr = 0;

                /* Board has 1 management port */
                if (ipd_port == CVMX_HELPER_BOARD_MGMT_IPD_PORT)
                    return 0;

                /* Board has 8 SGMII ports. 4 connected QLM1, 4 connected QLM3 */
                if ((ipd_port >= 0) && (ipd_port < 4))
                {
                    if (qlm_switch_addr != 0x3)
                    {
                        qlm_switch_addr = 0x3;  /* QLM1 */
                        cvmx_twsix_write_ia(0, 0x71, 0, 1, 1, qlm_switch_addr);
                        cvmx_wait_usec(11000); /* Let the write complete */
                    }
                    return ipd_port+1 + (1<<8);
                }
                else if ((ipd_port >= 16) && (ipd_port < 20))
                {
                    if (qlm_switch_addr != 0xC)
                    {
                        qlm_switch_addr = 0xC;  /* QLM3 */
                        cvmx_twsix_write_ia(0, 0x71, 0, 1, 1, qlm_switch_addr);
                        cvmx_wait_usec(11000); /* Let the write complete */
                    }
                    return ipd_port-16+1 + (1<<8);
                }
                else
                    return -1;
            }
        case CVMX_BOARD_TYPE_EBB6300:
            /* Board has 2 management ports */
            if ((ipd_port >= CVMX_HELPER_BOARD_MGMT_IPD_PORT) && (ipd_port < (CVMX_HELPER_BOARD_MGMT_IPD_PORT + 2)))
                return ipd_port - CVMX_HELPER_BOARD_MGMT_IPD_PORT + 4;
            if ((ipd_port >= 0) && (ipd_port < 4))
                return ipd_port + 1 + (1<<8);
            else
                return -1;
        case CVMX_BOARD_TYPE_EBB6800:
            /* Board has 1 management ports */
            if (ipd_port == CVMX_HELPER_BOARD_MGMT_IPD_PORT)
                return 6;
            if (ipd_port >= 0x800 && ipd_port < 0x900) /* QLM 0*/
                return 0x101 + ((ipd_port >> 4) & 3); /* SMI 1*/
            if (ipd_port >= 0xa00 && ipd_port < 0xb00) /* QLM 2*/
                return 0x201 + ((ipd_port >> 4) & 3); /* SMI 2*/
            if (ipd_port >= 0xb00 && ipd_port < 0xc00) /* QLM 3*/
                return 0x301 + ((ipd_port >> 4) & 3); /* SMI 3*/
            if (ipd_port >= 0xc00 && ipd_port < 0xd00) /* QLM 4*/
                return 0x001 + ((ipd_port >> 4) & 3); /* SMI 0*/
            return -1;
        case CVMX_BOARD_TYPE_EP6300C:
            if (ipd_port == CVMX_HELPER_BOARD_MGMT_IPD_PORT)
                return 0x01;
            if (ipd_port == CVMX_HELPER_BOARD_MGMT_IPD_PORT+1)
                return 0x02;
#ifdef CVMX_ENABLE_PKO_FUNCTIONS
            {
                int interface = cvmx_helper_get_interface_num(ipd_port);
                int mode = cvmx_helper_interface_get_mode(interface);
                if (mode == CVMX_HELPER_INTERFACE_MODE_XAUI)
                    return ipd_port;
                else if ((ipd_port >= 0) && (ipd_port < 4))
                    return ipd_port + 3;
                else
                    return -1;
            }
#endif
            break;
        case CVMX_BOARD_TYPE_CUST_NB5:
            if (ipd_port == 2)
                return 4;
            else
                return -1;
        case CVMX_BOARD_TYPE_NIC_XLE_4G:
            /* Board has 4 SGMII ports. connected QLM3(interface 1) */
            if ((ipd_port >= 16) && (ipd_port < 20))
                return ipd_port - 16 + 1;
            else
                return -1;
        case CVMX_BOARD_TYPE_NIC_XLE_10G:
        case CVMX_BOARD_TYPE_NIC10E:
            return -1;  /* We don't use clause 45 MDIO for anything */
        case CVMX_BOARD_TYPE_NIC4E:
            if (ipd_port >= 0 && ipd_port <= 3)
                return (ipd_port + 0x1f) & 0x1f;
            else
                return -1;
        case CVMX_BOARD_TYPE_NIC2E:
            if (ipd_port >= 0 && ipd_port <= 1)
                return (ipd_port + 1);
            else
                return -1;
        case CVMX_BOARD_TYPE_REDWING:
	    return -1;  /* No PHYs connected to Octeon */
        case CVMX_BOARD_TYPE_BBGW_REF:
            return -1;  /* No PHYs are connected to Octeon, everything is through switch */
	case CVMX_BOARD_TYPE_CUST_WSX16:
		if (ipd_port >= 0 && ipd_port <= 3)
			return ipd_port;
		else if (ipd_port >= 16 && ipd_port <= 19)
			return ipd_port - 16 + 4;
		else
			return -1;

	/* Private vendor-defined boards.  */
#if defined(OCTEON_VENDOR_LANNER)
	case CVMX_BOARD_TYPE_CUST_LANNER_MR955:
	    /* Interface 1 is 12 BCM5482S PHYs.  */
            if ((ipd_port >= 16) && (ipd_port < 28))
                return ipd_port - 16;
	    return -1;
	case CVMX_BOARD_TYPE_CUST_LANNER_MR730:
            if ((ipd_port >= CVMX_HELPER_BOARD_MGMT_IPD_PORT) && (ipd_port < (CVMX_HELPER_BOARD_MGMT_IPD_PORT + 2)))
		return (ipd_port - CVMX_HELPER_BOARD_MGMT_IPD_PORT) + 0x81;
            if ((ipd_port >= 0) && (ipd_port < 4))
                return ipd_port;
	    return -1;
	case CVMX_BOARD_TYPE_CUST_LANNER_MR320:
	case CVMX_BOARD_TYPE_CUST_LANNER_MR321X:
	    /* Port 0 is a Marvell 88E6161 switch, ports 1 and 2 are Marvell
	       88E1111 interfaces.  */
	    switch (ipd_port) {
	    case 0:
		return 16;
	    case 1:
		return 1;
	    case 2:
		return 2;
	    default:
		return -1;
	    }
#endif
#if defined(OCTEON_VENDOR_RADISYS)
	case CVMX_BOARD_TYPE_CUST_RADISYS_RSYS4GBE:
	    /* No MII.  */
	    return -1;
#endif
    }

    /* Some unknown board. Somebody forgot to update this function... */
    cvmx_dprintf("%s: Unknown board type %d\n",
                 __FUNCTION__, cvmx_sysinfo_get()->board_type);
    return -1;
}
#ifdef CVMX_BUILD_FOR_LINUX_KERNEL
EXPORT_SYMBOL(cvmx_helper_board_get_mii_address);
#endif

/**
 * @INTERNAL
 * Get link state of marvell PHY
 */
static cvmx_helper_link_info_t __get_marvell_phy_link_state(int phy_addr)
{
    cvmx_helper_link_info_t  result;
    int phy_status;

    result.u64 = 0;
    /*All the speed information can be read from register 17 in one go.*/
    phy_status = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, 17);

    /* If the resolve bit 11 isn't set, see if autoneg is turned off
       (bit 12, reg 0). The resolve bit doesn't get set properly when
       autoneg is off, so force it */
    if ((phy_status & (1<<11)) == 0)
    {
        int auto_status = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, 0);
        if ((auto_status & (1<<12)) == 0)
            phy_status |= 1<<11;
    }

    /* Only return a link if the PHY has finished auto negotiation
       and set the resolved bit (bit 11) */
    if (phy_status & (1<<11))
    {
        result.s.link_up = 1;
        result.s.full_duplex = ((phy_status>>13)&1);
        switch ((phy_status>>14)&3)
        {
            case 0: /* 10 Mbps */
                result.s.speed = 10;
                break;
            case 1: /* 100 Mbps */
                result.s.speed = 100;
                break;
            case 2: /* 1 Gbps */
                result.s.speed = 1000;
                break;
            case 3: /* Illegal */
                result.u64 = 0;
                break;
        }
    }
    return result;
}

/**
 * @INTERNAL
 * Get link state of broadcom PHY
 */
static cvmx_helper_link_info_t __get_broadcom_phy_link_state(int phy_addr)
{
    cvmx_helper_link_info_t  result;
    int phy_status;

    result.u64 = 0;
    /* Below we are going to read SMI/MDIO register 0x19 which works
       on Broadcom parts */
    phy_status = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, 0x19);
    switch ((phy_status>>8) & 0x7)
    {
        case 0:
            result.u64 = 0;
            break;
        case 1:
            result.s.link_up = 1;
            result.s.full_duplex = 0;
            result.s.speed = 10;
            break;
        case 2:
            result.s.link_up = 1;
            result.s.full_duplex = 1;
            result.s.speed = 10;
            break;
        case 3:
            result.s.link_up = 1;
            result.s.full_duplex = 0;
            result.s.speed = 100;
            break;
        case 4:
            result.s.link_up = 1;
            result.s.full_duplex = 1;
            result.s.speed = 100;
            break;
        case 5:
            result.s.link_up = 1;
            result.s.full_duplex = 1;
            result.s.speed = 100;
            break;
        case 6:
            result.s.link_up = 1;
            result.s.full_duplex = 0;
            result.s.speed = 1000;
            break;
        case 7:
            result.s.link_up = 1;
            result.s.full_duplex = 1;
            result.s.speed = 1000;
            break;
    }
    return result;
}


/**
 * @INTERNAL
 * Get link state using inband status
 */
static cvmx_helper_link_info_t __get_inband_link_state(int ipd_port)
{
    cvmx_helper_link_info_t  result;
    cvmx_gmxx_rxx_rx_inbnd_t inband_status;
    int interface = cvmx_helper_get_interface_num(ipd_port);
    int index = cvmx_helper_get_interface_index_num(ipd_port);

    result.u64 = 0;
    inband_status.u64 = cvmx_read_csr(CVMX_GMXX_RXX_RX_INBND(index, interface));
    result.s.link_up = inband_status.s.status;
    result.s.full_duplex = inband_status.s.duplex;
    switch (inband_status.s.speed)
    {
        case 0: /* 10 Mbps */
            result.s.speed = 10;
            break;
        case 1: /* 100 Mbps */
            result.s.speed = 100;
            break;
        case 2: /* 1 Gbps */
            result.s.speed = 1000;
            break;
        case 3: /* Illegal */
            result.u64 = 0;
            break;
    }
    return result;
}

#if !defined(CVMX_BUILD_FOR_LINUX_KERNEL) && (!defined(__FreeBSD__) || !defined(_KERNEL))
/**
 * @INTERNAL
 * Switch MDIO mux to the specified port.
 */
static int __switch_mdio_mux(int ipd_port)
{
    /* This method is board specific and doesn't use the device tree
       information as SE doesn't implement MDIO MUX abstration */
    switch (cvmx_sysinfo_get()->board_type)
    {
        case CVMX_BOARD_TYPE_EBB5600:
        {
            static unsigned char qlm_switch_addr = 0;
            /* Board has 1 management port */
            if (ipd_port == CVMX_HELPER_BOARD_MGMT_IPD_PORT)
                return 0;
            /* Board has 8 SGMII ports. 4 connected QLM1, 4 connected QLM3 */
            if ((ipd_port >= 0) && (ipd_port < 4))
            {
                if (qlm_switch_addr != 0x3)
                {
                    qlm_switch_addr = 0x3;  /* QLM1 */
                    cvmx_twsix_write_ia(0, 0x71, 0, 1, 1, qlm_switch_addr);
                    cvmx_wait_usec(11000); /* Let the write complete */
                }
                return ipd_port+1 + (1<<8);
            }
            else if ((ipd_port >= 16) && (ipd_port < 20))
            {
                if (qlm_switch_addr != 0xC)
                {
                    qlm_switch_addr = 0xC;  /* QLM3 */
                    cvmx_twsix_write_ia(0, 0x71, 0, 1, 1, qlm_switch_addr);
                    cvmx_wait_usec(11000); /* Let the write complete */
                }
                return ipd_port-16+1 + (1<<8);
            }
            else
                return -1;
        }
        case CVMX_BOARD_TYPE_EBB6600:
        {
            static unsigned char qlm_switch_addr = 0;
            int old_twsi_switch_reg;
            /* Board has 2 management ports */
            if ((ipd_port >= CVMX_HELPER_BOARD_MGMT_IPD_PORT) &&
                (ipd_port < (CVMX_HELPER_BOARD_MGMT_IPD_PORT + 2)))
                return ipd_port - CVMX_HELPER_BOARD_MGMT_IPD_PORT + 4;
            if ((ipd_port >= 0) && (ipd_port < 4)) /* QLM 2 */
            {
                if (qlm_switch_addr != 2)
                {
                    int tries;
                    qlm_switch_addr = 2;
                    tries = 3;
                    do {
                        old_twsi_switch_reg = cvmx_twsix_read8(0, 0x70, 0);
                    } while (tries-- > 0 && old_twsi_switch_reg < 0);
                    /* Set I2C MUX to enable port expander */
                    cvmx_retry_i2c_write(0, 0x70, 0, 1, 0, 8);
                    /* Set selecter to QLM 1 */
                    cvmx_retry_i2c_write(0, 0x38, 0, 1, 0, 0xff);
                    /* disable port expander */
                    cvmx_retry_i2c_write(0, 0x70, 0, 1, 0, old_twsi_switch_reg);
                }
                return 0x101 + ipd_port;
            }
            else if ((ipd_port >= 16) && (ipd_port < 20)) /* QLM 1 */
            {
                if (qlm_switch_addr != 1)
                {
                    int tries;
                    qlm_switch_addr = 1;
                    tries = 3;
                    do {
                            old_twsi_switch_reg = cvmx_twsix_read8(0, 0x70, 0);
                    } while (tries-- > 0 && old_twsi_switch_reg < 0);
                    /* Set I2C MUX to enable port expander */
                    cvmx_retry_i2c_write(0, 0x70, 0, 1, 0, 8);
                    /* Set selecter to QLM 2 */
                    cvmx_retry_i2c_write(0, 0x38, 0, 1, 0, 0xf7);
                    /* disable port expander */
                    cvmx_retry_i2c_write(0, 0x70, 0, 1, 0, old_twsi_switch_reg);
                }
                return 0x101 + (ipd_port - 16);
            } else
                return -1;
        }
        case CVMX_BOARD_TYPE_EBB6100:
        {
            static char gpio_configured = 0;

            if (!gpio_configured)
            {
                cvmx_gpio_cfg(3, 1);
                gpio_configured = 1;
            }
            /* Board has 2 management ports */
            if ((ipd_port >= CVMX_HELPER_BOARD_MGMT_IPD_PORT) &&
                (ipd_port < (CVMX_HELPER_BOARD_MGMT_IPD_PORT + 2)))
                return ipd_port - CVMX_HELPER_BOARD_MGMT_IPD_PORT + 4;
            if ((ipd_port >= 0) && (ipd_port < 4)) /* QLM 2 */
            {
                cvmx_gpio_set(1ull << 3);
                return 0x101 + ipd_port;
            }
            else if ((ipd_port >= 16) && (ipd_port < 20)) /* QLM 0 */
            {
                cvmx_gpio_clear(1ull << 3);
                return 0x101 + (ipd_port - 16);
            }
            else
            {
                printf("%s: Unknown ipd port 0x%x\n", __func__, ipd_port);
                return -1;
            }
        }
        default:
        {
            cvmx_dprintf("ERROR : unexpected mdio switch for board=%08x\n",
                         cvmx_sysinfo_get()->board_type);
            return -1;
        }
    }
    /* should never get here */
    return -1;
}

/**
 * @INTERNAL
 * This function is used ethernet ports link speed. This functions uses the
 * device tree information to determine the phy address and type of PHY.
 * The only supproted PHYs are Marvell and Broadcom.
 *
 * @param ipd_port IPD input port associated with the port we want to get link
 *                 status for.
 *
 * @return The ports link status. If the link isn't fully resolved, this must
 *         return zero.
 */

cvmx_helper_link_info_t __cvmx_helper_board_link_get_from_dt(int ipd_port)
{
    cvmx_helper_link_info_t  result;
    cvmx_phy_info_t phy_info;

    result.u64 = 0;
    if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
    {
        /* The simulator gives you a simulated 1Gbps full duplex link */
        result.s.link_up = 1;
        result.s.full_duplex = 1;
        result.s.speed = 1000;
        return result;
    }
    phy_info = __get_phy_info_from_dt(ipd_port);
    //cvmx_dprintf("ipd_port=%d phy_addr=%d dc=%d type=%d \n", ipd_port,
    //             phy_info.phy_addr, phy_info.direct_connect, phy_info.phy_type);
    if (phy_info.phy_addr < 0) return result;

    if (phy_info.direct_connect == 0)
        __switch_mdio_mux(ipd_port);
    switch(phy_info.phy_type)
    {
        case BROADCOM_GENERIC_PHY:
            result = __get_broadcom_phy_link_state(phy_info.phy_addr);
            break;
        case MARVELL_GENERIC_PHY:
            result = __get_marvell_phy_link_state(phy_info.phy_addr);
            break;
        default:
            result = __get_inband_link_state(ipd_port);
    }
    return result;

}
#endif

/**
 * @INTERNAL
 * This function invokes  __cvmx_helper_board_link_get_from_dt when device tree
 * info is available. When the device tree information is not available then
 * this function is the board specific method of determining an
 * ethernet ports link speed. Most Octeon boards have Marvell PHYs
 * and are handled by the fall through case. This function must be
 * updated for boards that don't have the normal Marvell PHYs.
 *
 * This function must be modified for every new Octeon board.
 * Internally it uses switch statements based on the cvmx_sysinfo
 * data to determine board types and revisions. It relies on the
 * fact that every Octeon board receives a unique board type
 * enumeration from the bootloader.
 *
 * @param ipd_port IPD input port associated with the port we want to get link
 *                 status for.
 *
 * @return The ports link status. If the link isn't fully resolved, this must
 *         return zero.
 */
cvmx_helper_link_info_t __cvmx_helper_board_link_get(int ipd_port)
{
    cvmx_helper_link_info_t result;
    int phy_addr;
    int is_broadcom_phy = 0;

#if !defined(CVMX_BUILD_FOR_LINUX_KERNEL) && (!defined(__FreeBSD__) || !defined(_KERNEL))
    if (cvmx_sysinfo_get()->fdt_addr)
    {
        return __cvmx_helper_board_link_get_from_dt(ipd_port);
    }
#endif

    /* Give the user a chance to override the processing of this function */
    if (cvmx_override_board_link_get)
        return cvmx_override_board_link_get(ipd_port);

    /* Unless we fix it later, all links are defaulted to down */
    result.u64 = 0;

#if !defined(OCTEON_BOARD_CAPK_0100ND)
    /* This switch statement should handle all ports that either don't use
        Marvell PHYS, or don't support in-band status */
    switch (cvmx_sysinfo_get()->board_type)
    {
        case CVMX_BOARD_TYPE_SIM:
            /* The simulator gives you a simulated 1Gbps full duplex link */
            result.s.link_up = 1;
            result.s.full_duplex = 1;
            result.s.speed = 1000;
            return result;
        case CVMX_BOARD_TYPE_LANAI2_A:
        case CVMX_BOARD_TYPE_LANAI2_U:
        case CVMX_BOARD_TYPE_LANAI2_G:
            break;
        case CVMX_BOARD_TYPE_EBH3100:
        case CVMX_BOARD_TYPE_CN3010_EVB_HS5:
        case CVMX_BOARD_TYPE_CN3005_EVB_HS5:
        case CVMX_BOARD_TYPE_CN3020_EVB_HS5:
            /* Port 1 on these boards is always Gigabit */
            if (ipd_port == 1)
            {
                result.s.link_up = 1;
                result.s.full_duplex = 1;
                result.s.speed = 1000;
                return result;
            }
            /* Fall through to the generic code below */
            break;
        case CVMX_BOARD_TYPE_EBT5600:
        case CVMX_BOARD_TYPE_EBH5600:
        case CVMX_BOARD_TYPE_EBH5601:
        case CVMX_BOARD_TYPE_EBH5610:
            /* Board has 1 management ports */
            if (ipd_port == CVMX_HELPER_BOARD_MGMT_IPD_PORT)
                is_broadcom_phy = 1;
            break;
        case CVMX_BOARD_TYPE_EBH5200:
        case CVMX_BOARD_TYPE_EBH5201:
        case CVMX_BOARD_TYPE_EBT5200:
            /* Board has 2 management ports */
            if ((ipd_port >= CVMX_HELPER_BOARD_MGMT_IPD_PORT) && (ipd_port < (CVMX_HELPER_BOARD_MGMT_IPD_PORT + 2)))
                is_broadcom_phy = 1;
            break;
        case CVMX_BOARD_TYPE_EBB6100:
        case CVMX_BOARD_TYPE_EBB6300:   /* Only for MII mode, with PHY addresses 0/1. Default is RGMII*/
        case CVMX_BOARD_TYPE_EBB6600:   /* Only for MII mode, with PHY addresses 0/1. Default is RGMII*/
            if ((ipd_port >= CVMX_HELPER_BOARD_MGMT_IPD_PORT) && (ipd_port < (CVMX_HELPER_BOARD_MGMT_IPD_PORT + 2))
                && cvmx_helper_board_get_mii_address(ipd_port) >= 0 && cvmx_helper_board_get_mii_address(ipd_port) <= 1)
                is_broadcom_phy = 1;
            break;
        case CVMX_BOARD_TYPE_EP6300C:
            is_broadcom_phy = 1;
            break;
        case CVMX_BOARD_TYPE_CUST_NB5:
            /* Port 1 on these boards is always Gigabit */
            if (ipd_port == 1)
            {
                result.s.link_up = 1;
                result.s.full_duplex = 1;
                result.s.speed = 1000;
                return result;
            }
            else /* The other port uses a broadcom PHY */
                is_broadcom_phy = 1;
            break;
        case CVMX_BOARD_TYPE_BBGW_REF:
            /* Port 1 on these boards is always Gigabit */
            if (ipd_port == 2)
            {
                /* Port 2 is not hooked up */
                result.u64 = 0;
                return result;
            }
            else
            {
                /* Ports 0 and 1 connect to the switch */
                result.s.link_up = 1;
                result.s.full_duplex = 1;
                result.s.speed = 1000;
                return result;
            }
        case CVMX_BOARD_TYPE_NIC4E:
        case CVMX_BOARD_TYPE_NIC2E:
            is_broadcom_phy = 1;
            break;
	/* Private vendor-defined boards.  */
#if defined(OCTEON_VENDOR_LANNER)
	case CVMX_BOARD_TYPE_CUST_LANNER_MR730:
	    /* Ports are BCM5482S */
	    is_broadcom_phy = 1;
	    break;
	case CVMX_BOARD_TYPE_CUST_LANNER_MR320:
	case CVMX_BOARD_TYPE_CUST_LANNER_MR321X:
	    /* Port 0 connects to the switch */
	    if (ipd_port == 0)
	    {
                result.s.link_up = 1;
                result.s.full_duplex = 1;
                result.s.speed = 1000;
		return result;
	    }
	    break;
#endif
    }
#endif

    phy_addr = cvmx_helper_board_get_mii_address(ipd_port);
    //cvmx_dprintf("ipd_port=%d phy_addr=%d broadcom=%d\n",
    //             ipd_port, phy_addr, is_broadcom_phy);
    if (phy_addr != -1)
    {
        if (is_broadcom_phy)
        {
            result =  __get_broadcom_phy_link_state(phy_addr);
        }
        else
        {
            /* This code assumes we are using a Marvell Gigabit PHY. */
            result = __get_marvell_phy_link_state(phy_addr);
        }
    }
    else if (OCTEON_IS_MODEL(OCTEON_CN3XXX) || OCTEON_IS_MODEL(OCTEON_CN58XX)
             || OCTEON_IS_MODEL(OCTEON_CN50XX))
    {
        /* We don't have a PHY address, so attempt to use in-band status. It is
            really important that boards not supporting in-band status never get
            here. Reading broken in-band status tends to do bad things */
        result = __get_inband_link_state(ipd_port);
    }
    else
    {
        /* We don't have a PHY address and we don't have in-band status. There
            is no way to determine the link speed. Return down assuming this
            port isn't wired */
        result.u64 = 0;
    }

    /* If link is down, return all fields as zero. */
    if (!result.s.link_up)
        result.u64 = 0;

    return result;
}


/**
 * This function as a board specific method of changing the PHY
 * speed, duplex, and autonegotiation. This programs the PHY and
 * not Octeon. This can be used to force Octeon's links to
 * specific settings.
 *
 * @param phy_addr  The address of the PHY to program
 * @param link_flags
 *                  Flags to control autonegotiation.  Bit 0 is autonegotiation
 *                  enable/disable to maintain backward compatibility.
 * @param link_info Link speed to program. If the speed is zero and autonegotiation
 *                  is enabled, all possible negotiation speeds are advertised.
 *
 * @return Zero on success, negative on failure
 */
int cvmx_helper_board_link_set_phy(int phy_addr, cvmx_helper_board_set_phy_link_flags_types_t link_flags,
                                   cvmx_helper_link_info_t link_info)
{

    /* Set the flow control settings based on link_flags */
    if ((link_flags & set_phy_link_flags_flow_control_mask) != set_phy_link_flags_flow_control_dont_touch)
    {
        cvmx_mdio_phy_reg_autoneg_adver_t reg_autoneg_adver;
        reg_autoneg_adver.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_AUTONEG_ADVER);
        reg_autoneg_adver.s.asymmetric_pause = (link_flags & set_phy_link_flags_flow_control_mask) == set_phy_link_flags_flow_control_enable;
        reg_autoneg_adver.s.pause = (link_flags & set_phy_link_flags_flow_control_mask) == set_phy_link_flags_flow_control_enable;
        cvmx_mdio_write(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_AUTONEG_ADVER, reg_autoneg_adver.u16);
    }

    /* If speed isn't set and autoneg is on advertise all supported modes */
    if ((link_flags & set_phy_link_flags_autoneg) && (link_info.s.speed == 0))
    {
        cvmx_mdio_phy_reg_control_t reg_control;
        cvmx_mdio_phy_reg_status_t reg_status;
        cvmx_mdio_phy_reg_autoneg_adver_t reg_autoneg_adver;
        cvmx_mdio_phy_reg_extended_status_t reg_extended_status;
        cvmx_mdio_phy_reg_control_1000_t reg_control_1000;

        reg_status.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_STATUS);
        reg_autoneg_adver.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_AUTONEG_ADVER);
        reg_autoneg_adver.s.advert_100base_t4 = reg_status.s.capable_100base_t4;
        reg_autoneg_adver.s.advert_10base_tx_full = reg_status.s.capable_10_full;
        reg_autoneg_adver.s.advert_10base_tx_half = reg_status.s.capable_10_half;
        reg_autoneg_adver.s.advert_100base_tx_full = reg_status.s.capable_100base_x_full;
        reg_autoneg_adver.s.advert_100base_tx_half = reg_status.s.capable_100base_x_half;
        cvmx_mdio_write(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_AUTONEG_ADVER, reg_autoneg_adver.u16);
        if (reg_status.s.capable_extended_status)
        {
            reg_extended_status.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_EXTENDED_STATUS);
            reg_control_1000.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL_1000);
            reg_control_1000.s.advert_1000base_t_full = reg_extended_status.s.capable_1000base_t_full;
            reg_control_1000.s.advert_1000base_t_half = reg_extended_status.s.capable_1000base_t_half;
            cvmx_mdio_write(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL_1000, reg_control_1000.u16);
        }
        reg_control.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL);
        reg_control.s.autoneg_enable = 1;
        reg_control.s.restart_autoneg = 1;
        cvmx_mdio_write(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL, reg_control.u16);
    }
    else if ((link_flags & set_phy_link_flags_autoneg))
    {
        cvmx_mdio_phy_reg_control_t reg_control;
        cvmx_mdio_phy_reg_status_t reg_status;
        cvmx_mdio_phy_reg_autoneg_adver_t reg_autoneg_adver;
        cvmx_mdio_phy_reg_control_1000_t reg_control_1000;

        reg_status.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_STATUS);
        reg_autoneg_adver.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_AUTONEG_ADVER);
        reg_autoneg_adver.s.advert_100base_t4 = 0;
        reg_autoneg_adver.s.advert_10base_tx_full = 0;
        reg_autoneg_adver.s.advert_10base_tx_half = 0;
        reg_autoneg_adver.s.advert_100base_tx_full = 0;
        reg_autoneg_adver.s.advert_100base_tx_half = 0;
        if (reg_status.s.capable_extended_status)
        {
            reg_control_1000.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL_1000);
            reg_control_1000.s.advert_1000base_t_full = 0;
            reg_control_1000.s.advert_1000base_t_half = 0;
        }
        switch (link_info.s.speed)
        {
            case 10:
                reg_autoneg_adver.s.advert_10base_tx_full = link_info.s.full_duplex;
                reg_autoneg_adver.s.advert_10base_tx_half = !link_info.s.full_duplex;
                break;
            case 100:
                reg_autoneg_adver.s.advert_100base_tx_full = link_info.s.full_duplex;
                reg_autoneg_adver.s.advert_100base_tx_half = !link_info.s.full_duplex;
                break;
            case 1000:
                reg_control_1000.s.advert_1000base_t_full = link_info.s.full_duplex;
                reg_control_1000.s.advert_1000base_t_half = !link_info.s.full_duplex;
                break;
        }
        cvmx_mdio_write(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_AUTONEG_ADVER, reg_autoneg_adver.u16);
        if (reg_status.s.capable_extended_status)
            cvmx_mdio_write(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL_1000, reg_control_1000.u16);
        reg_control.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL);
        reg_control.s.autoneg_enable = 1;
        reg_control.s.restart_autoneg = 1;
        cvmx_mdio_write(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL, reg_control.u16);
    }
    else
    {
        cvmx_mdio_phy_reg_control_t reg_control;
        reg_control.u16 = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL);
        reg_control.s.autoneg_enable = 0;
        reg_control.s.restart_autoneg = 1;
        reg_control.s.duplex = link_info.s.full_duplex;
        if (link_info.s.speed == 1000)
        {
            reg_control.s.speed_msb = 1;
            reg_control.s.speed_lsb = 0;
        }
        else if (link_info.s.speed == 100)
        {
            reg_control.s.speed_msb = 0;
            reg_control.s.speed_lsb = 1;
        }
        else if (link_info.s.speed == 10)
        {
            reg_control.s.speed_msb = 0;
            reg_control.s.speed_lsb = 0;
        }
        cvmx_mdio_write(phy_addr >> 8, phy_addr & 0xff, CVMX_MDIO_PHY_REG_CONTROL, reg_control.u16);
    }
    return 0;
}


/**
 * @INTERNAL
 * This function is called by cvmx_helper_interface_probe() after it
 * determines the number of ports Octeon can support on a specific
 * interface. This function is the per board location to override
 * this value. It is called with the number of ports Octeon might
 * support and should return the number of actual ports on the
 * board.
 *
 * This function must be modified for every new Octeon board.
 * Internally it uses switch statements based on the cvmx_sysinfo
 * data to determine board types and revisions. It relies on the
 * fact that every Octeon board receives a unique board type
 * enumeration from the bootloader.
 *
 * @param interface Interface to probe
 * @param supported_ports
 *                  Number of ports Octeon supports.
 *
 * @return Number of ports the actual board supports. Many times this will
 *         simple be "support_ports".
 */
int __cvmx_helper_board_interface_probe(int interface, int supported_ports)
{
    switch (cvmx_sysinfo_get()->board_type)
    {
        case CVMX_BOARD_TYPE_CN3005_EVB_HS5:
        case CVMX_BOARD_TYPE_LANAI2_A:
        case CVMX_BOARD_TYPE_LANAI2_U:
        case CVMX_BOARD_TYPE_LANAI2_G:
            if (interface == 0)
                return 2;
	    break;
        case CVMX_BOARD_TYPE_BBGW_REF:
            if (interface == 0)
                return 2;
	    break;
        case CVMX_BOARD_TYPE_NIC_XLE_4G:
            if (interface == 0)
                return 0;
	    break;
        /* The 2nd interface on the EBH5600 is connected to the Marvel switch,
            which we don't support. Disable ports connected to it */
        case CVMX_BOARD_TYPE_EBH5600:
            if (interface == 1)
                return 0;
	    break;
        case CVMX_BOARD_TYPE_EBB5600:
#ifdef CVMX_ENABLE_PKO_FUNCTIONS
            if (cvmx_helper_interface_get_mode(interface) == CVMX_HELPER_INTERFACE_MODE_PICMG)
                return 0;
#endif
	    break;
        case CVMX_BOARD_TYPE_EBT5600:
	    /* Disable loopback.  */
	    if (interface == 3)
		return 0;
	    break;
        case CVMX_BOARD_TYPE_EBT5810:
            return 1;  /* Two ports on each SPI: 1 hooked to MAC, 1 loopback
                       ** Loopback disabled by default. */
        case CVMX_BOARD_TYPE_NIC2E:
            if (interface == 0)
                return 2;
#if defined(OCTEON_VENDOR_LANNER)
	case CVMX_BOARD_TYPE_CUST_LANNER_MR955:
	    if (interface == 1)
	        return 12;
	    break;
#endif
#if defined(OCTEON_VENDOR_RADISYS)
	case CVMX_BOARD_TYPE_CUST_RADISYS_RSYS4GBE:
	    if (interface == 0)
		    return 13;
	    if (interface == 1)
		    return 8;
	    return 0;
#endif
    }
#ifdef CVMX_BUILD_FOR_UBOOT
    if (CVMX_HELPER_INTERFACE_MODE_SPI == cvmx_helper_interface_get_mode(interface) && getenv("disable_spi"))
        return 0;
#endif
    return supported_ports;
}


/**
 * @INTERNAL
 * Enable packet input/output from the hardware. This function is
 * called after by cvmx_helper_packet_hardware_enable() to
 * perform board specific initialization. For most boards
 * nothing is needed.
 *
 * @param interface Interface to enable
 *
 * @return Zero on success, negative on failure
 */
int __cvmx_helper_board_hardware_enable(int interface)
{
    if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_CN3005_EVB_HS5)
    {
        if (interface == 0)
        {
            /* Different config for switch port */
            cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(1, interface), 0);
            cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(1, interface), 0);
            /* Boards with gigabit WAN ports need a different setting that is
                compatible with 100 Mbit settings */
            cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(0, interface), 0xc);
            cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(0, interface), 0xc);
        }
    }
    else if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_LANAI2_U)
    {
        if (interface == 0)
        {
            cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(0, interface), 16);
            cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(0, interface), 16);
        }
    }
    else if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_CN3010_EVB_HS5)
    {
        /* Broadcom PHYs require different ASX clocks. Unfortunately
            many customer don't define a new board Id and simply
            mangle the CN3010_EVB_HS5 */
        if (interface == 0)
        {
            /* Some customers boards use a hacked up bootloader that identifies them as
            ** CN3010_EVB_HS5 evaluation boards.  This leads to all kinds of configuration
            ** problems.  Detect one case, and print warning, while trying to do the right thing.
            */
            int phy_addr = cvmx_helper_board_get_mii_address(0);
            if (phy_addr != -1)
            {
                int phy_identifier = cvmx_mdio_read(phy_addr >> 8, phy_addr & 0xff, 0x2);
                /* Is it a Broadcom PHY? */
                if (phy_identifier == 0x0143)
                {
                    cvmx_dprintf("\n");
                    cvmx_dprintf("ERROR:\n");
                    cvmx_dprintf("ERROR: Board type is CVMX_BOARD_TYPE_CN3010_EVB_HS5, but Broadcom PHY found.\n");
                    cvmx_dprintf("ERROR: The board type is mis-configured, and software malfunctions are likely.\n");
                    cvmx_dprintf("ERROR: All boards require a unique board type to identify them.\n");
                    cvmx_dprintf("ERROR:\n");
                    cvmx_dprintf("\n");
                    cvmx_wait(1000000000);
                    cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(0, interface), 5);
                    cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(0, interface), 5);
                }
            }
        }
    }
    return 0;
}


/**
 * @INTERNAL
 * Gets the clock type used for the USB block based on board type.
 * Used by the USB code for auto configuration of clock type.
 *
 * @return USB clock type enumeration
 */
cvmx_helper_board_usb_clock_types_t __cvmx_helper_board_usb_get_clock_type(void)
{
#if !defined(CVMX_BUILD_FOR_LINUX_KERNEL) && (!defined(__FreeBSD__) || !defined(_KERNEL))
    const void *fdt_addr = CASTPTR(const void *, cvmx_sysinfo_get()->fdt_addr);
    int nodeoffset;
    const void *nodep;
    int len;
    uint32_t speed = 0;
    const char *type = NULL;

    if (fdt_addr)
    {
        nodeoffset = fdt_path_offset(fdt_addr, "/soc/uctl");
        if (nodeoffset < 0)
            nodeoffset = fdt_path_offset(fdt_addr, "/soc/usbn");

        if (nodeoffset >= 0)
        {
            nodep = fdt_getprop(fdt_addr, nodeoffset, "refclk-type", &len);
            if (nodep != NULL && len > 0)
                type = (const char *)nodep;
            else
                type = "unknown";
            nodep = fdt_getprop(fdt_addr, nodeoffset, "refclk-frequency", &len);
            if (nodep != NULL && len == sizeof(uint32_t))
                speed = fdt32_to_cpu(*(int *)nodep);
            else
                speed = 0;
            if (!strcmp(type, "crystal"))
            {
                if (speed == 0 || speed == 12000000)
                    return USB_CLOCK_TYPE_CRYSTAL_12;
                else
                    printf("Warning: invalid crystal speed for USB clock type in FDT\n");
            }
            else if (!strcmp(type, "external"))
            {
                switch (speed) {
                case 12000000:
                    return USB_CLOCK_TYPE_REF_12;
                case 24000000:
                    return USB_CLOCK_TYPE_REF_24;
                case 0:
                case 48000000:
                    return USB_CLOCK_TYPE_REF_48;
                default:
                    printf("Warning: invalid USB clock speed of %u hz in FDT\n", speed);
                }
            }
            else
                printf("Warning: invalid USB reference clock type \"%s\" in FDT\n", type ? type : "NULL");
        }
    }
#endif
    switch (cvmx_sysinfo_get()->board_type)
    {
        case CVMX_BOARD_TYPE_BBGW_REF:
        case CVMX_BOARD_TYPE_LANAI2_A:
        case CVMX_BOARD_TYPE_LANAI2_U:
        case CVMX_BOARD_TYPE_LANAI2_G:
#if defined(OCTEON_VENDOR_LANNER)
    case CVMX_BOARD_TYPE_CUST_LANNER_MR320:
    case CVMX_BOARD_TYPE_CUST_LANNER_MR321X:
#endif
#if defined(OCTEON_BOARD_CAPK_0100ND)
	case CVMX_BOARD_TYPE_CN3010_EVB_HS5:
#endif
        case CVMX_BOARD_TYPE_NIC10E_66:
            return USB_CLOCK_TYPE_CRYSTAL_12;
        case CVMX_BOARD_TYPE_NIC10E:
            return USB_CLOCK_TYPE_REF_12;
        default:
            break;
    }
    if (OCTEON_IS_MODEL(OCTEON_CN6XXX)	/* Most boards except NIC10e use a 12MHz crystal */
        || OCTEON_IS_MODEL(OCTEON_CNF7XXX))
        return USB_CLOCK_TYPE_CRYSTAL_12;
    return USB_CLOCK_TYPE_REF_48;
}


/**
 * @INTERNAL
 * Adjusts the number of available USB ports on Octeon based on board
 * specifics.
 *
 * @param supported_ports expected number of ports based on chip type;
 *
 *
 * @return number of available usb ports, based on board specifics.
 *         Return value is supported_ports if function does not
 *         override.
 */
int __cvmx_helper_board_usb_get_num_ports(int supported_ports)
{
    switch (cvmx_sysinfo_get()->board_type)
    {
        case CVMX_BOARD_TYPE_NIC_XLE_4G:
        case CVMX_BOARD_TYPE_NIC2E:
            return 0;
    }

    return supported_ports;
}