Loading...
   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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015-2016 Marvell International Ltd.
 */

#include <fdt_support.h>
#include <log.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <asm/arch/cpu.h>
#include <asm/arch/soc.h>
#include <linux/delay.h>
#include <phy.h>

#include "comphy_a3700.h"

DECLARE_GLOBAL_DATA_PTR;

struct comphy_mux_data a3700_comphy_mux_data[] = {
	/* Lane 0 */
	{
		4,
		{
			{ COMPHY_TYPE_UNCONNECTED,	0x0 },
			{ COMPHY_TYPE_SGMII1,	0x0 },
			{ COMPHY_TYPE_USB3_HOST0,	0x1 },
			{ COMPHY_TYPE_USB3_DEVICE,	0x1 }
		}
	},
	/* Lane 1 */
	{
		3,
		{
			{ COMPHY_TYPE_UNCONNECTED,	0x0},
			{ COMPHY_TYPE_SGMII0,	0x0},
			{ COMPHY_TYPE_PEX0,	0x1}
		}
	},
	/* Lane 2 */
	{
		4,
		{
			{ COMPHY_TYPE_UNCONNECTED,	0x0},
			{ COMPHY_TYPE_SATA0,	0x0},
			{ COMPHY_TYPE_USB3_HOST0,	0x1},
			{ COMPHY_TYPE_USB3_DEVICE,	0x1}
		}
	},
};

struct sgmii_phy_init_data_fix {
	u16 addr;
	u16 value;
};

/* Changes to 40M1G25 mode data required for running 40M3G125 init mode */
static struct sgmii_phy_init_data_fix sgmii_phy_init_fix[] = {
	{0x005, 0x07CC}, {0x015, 0x0000}, {0x01B, 0x0000}, {0x01D, 0x0000},
	{0x01E, 0x0000}, {0x01F, 0x0000}, {0x020, 0x0000}, {0x021, 0x0030},
	{0x026, 0x0888}, {0x04D, 0x0152}, {0x04F, 0xA020}, {0x050, 0x07CC},
	{0x053, 0xE9CA}, {0x055, 0xBD97}, {0x071, 0x3015}, {0x076, 0x03AA},
	{0x07C, 0x0FDF}, {0x0C2, 0x3030}, {0x0C3, 0x8000}, {0x0E2, 0x5550},
	{0x0E3, 0x12A4}, {0x0E4, 0x7D00}, {0x0E6, 0x0C83}, {0x101, 0xFCC0},
	{0x104, 0x0C10}
};

/* 40M1G25 mode init data */
static u16 sgmii_phy_init[512] = {
	/* 0       1       2       3       4       5       6       7 */
	/*-----------------------------------------------------------*/
	/* 8       9       A       B       C       D       E       F */
	0x3110, 0xFD83, 0x6430, 0x412F, 0x82C0, 0x06FA, 0x4500, 0x6D26,	/* 00 */
	0xAFC0, 0x8000, 0xC000, 0x0000, 0x2000, 0x49CC, 0x0BC9, 0x2A52,	/* 08 */
	0x0BD2, 0x0CDE, 0x13D2, 0x0CE8, 0x1149, 0x10E0, 0x0000, 0x0000,	/* 10 */
	0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x4134, 0x0D2D, 0xFFFF,	/* 18 */
	0xFFE0, 0x4030, 0x1016, 0x0030, 0x0000, 0x0800, 0x0866, 0x0000,	/* 20 */
	0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,	/* 28 */
	0xFFFF, 0xFFFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* 30 */
	0x0000, 0x0000, 0x000F, 0x6A62, 0x1988, 0x3100, 0x3100, 0x3100,	/* 38 */
	0x3100, 0xA708, 0x2430, 0x0830, 0x1030, 0x4610, 0xFF00, 0xFF00,	/* 40 */
	0x0060, 0x1000, 0x0400, 0x0040, 0x00F0, 0x0155, 0x1100, 0xA02A,	/* 48 */
	0x06FA, 0x0080, 0xB008, 0xE3ED, 0x5002, 0xB592, 0x7A80, 0x0001,	/* 50 */
	0x020A, 0x8820, 0x6014, 0x8054, 0xACAA, 0xFC88, 0x2A02, 0x45CF,	/* 58 */
	0x000F, 0x1817, 0x2860, 0x064F, 0x0000, 0x0204, 0x1800, 0x6000,	/* 60 */
	0x810F, 0x4F23, 0x4000, 0x4498, 0x0850, 0x0000, 0x000E, 0x1002,	/* 68 */
	0x9D3A, 0x3009, 0xD066, 0x0491, 0x0001, 0x6AB0, 0x0399, 0x3780,	/* 70 */
	0x0040, 0x5AC0, 0x4A80, 0x0000, 0x01DF, 0x0000, 0x0007, 0x0000,	/* 78 */
	0x2D54, 0x00A1, 0x4000, 0x0100, 0xA20A, 0x0000, 0x0000, 0x0000,	/* 80 */
	0x0000, 0x0000, 0x0000, 0x7400, 0x0E81, 0x1000, 0x1242, 0x0210,	/* 88 */
	0x80DF, 0x0F1F, 0x2F3F, 0x4F5F, 0x6F7F, 0x0F1F, 0x2F3F, 0x4F5F,	/* 90 */
	0x6F7F, 0x4BAD, 0x0000, 0x0000, 0x0800, 0x0000, 0x2400, 0xB651,	/* 98 */
	0xC9E0, 0x4247, 0x0A24, 0x0000, 0xAF19, 0x1004, 0x0000, 0x0000,	/* A0 */
	0x0000, 0x0013, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* A8 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/* B0 */
	0x0000, 0x0000, 0x0000, 0x0060, 0x0000, 0x0000, 0x0000, 0x0000,	/* B8 */
	0x0000, 0x0000, 0x3010, 0xFA00, 0x0000, 0x0000, 0x0000, 0x0003,	/* C0 */
	0x1618, 0x8200, 0x8000, 0x0400, 0x050F, 0x0000, 0x0000, 0x0000,	/* C8 */
	0x4C93, 0x0000, 0x1000, 0x1120, 0x0010, 0x1242, 0x1242, 0x1E00,	/* D0 */
	0x0000, 0x0000, 0x0000, 0x00F8, 0x0000, 0x0041, 0x0800, 0x0000,	/* D8 */
	0x82A0, 0x572E, 0x2490, 0x14A9, 0x4E00, 0x0000, 0x0803, 0x0541,	/* E0 */
	0x0C15, 0x0000, 0x0000, 0x0400, 0x2626, 0x0000, 0x0000, 0x4200,	/* E8 */
	0x0000, 0xAA55, 0x1020, 0x0000, 0x0000, 0x5010, 0x0000, 0x0000,	/* F0 */
	0x0000, 0x0000, 0x5000, 0x0000, 0x0000, 0x0000, 0x02F2, 0x0000,	/* F8 */
	0x101F, 0xFDC0, 0x4000, 0x8010, 0x0110, 0x0006, 0x0000, 0x0000,	/*100 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*108 */
	0x04CF, 0x0000, 0x04CF, 0x0000, 0x04CF, 0x0000, 0x04C6, 0x0000,	/*110 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*118 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*120 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*128 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*130 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*138 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*140 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*148 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*150 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*158 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*160 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*168 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*170 */
	0x0000, 0x0000, 0x0000, 0x00F0, 0x08A2, 0x3112, 0x0A14, 0x0000,	/*178 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*180 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*188 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*190 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*198 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1A0 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1A8 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1B0 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1B8 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1C0 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1C8 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1D0 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1D8 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1E0 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1E8 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,	/*1F0 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000	/*1F8 */
};

/*
 * comphy_poll_reg
 *
 * return: 1 on success, 0 on timeout
 */
static u32 comphy_poll_reg(void *addr, u32 val, u32 mask, u8 op_type)
{
	u32 rval = 0xDEAD, timeout;

	for (timeout = PLL_LOCK_TIMEOUT; timeout > 0; timeout--) {
		if (op_type == POLL_16B_REG)
			rval = readw(addr);	/* 16 bit */
		else
			rval = readl(addr) ;	/* 32 bit */

		if ((rval & mask) == val)
			return 1;

		udelay(10000);
	}

	debug("Time out waiting (%p = %#010x)\n", addr, rval);
	return 0;
}

/*
 * comphy_pcie_power_up
 *
 * return: 1 if PLL locked (OK), 0 otherwise (FAIL)
 */
static int comphy_pcie_power_up(u32 speed, u32 invert)
{
	int ret;

	debug_enter();

	/*
	 * 1. Enable max PLL.
	 */
	reg_set16(phy_addr(PCIE, LANE_CFG1), bf_use_max_pll_rate, 0);

	/*
	 * 2. Select 20 bit SERDES interface.
	 */
	reg_set16(phy_addr(PCIE, GLOB_CLK_SRC_LO), bf_cfg_sel_20b, 0);

	/*
	 * 3. Force to use reg setting for PCIe mode
	 */
	reg_set16(phy_addr(PCIE, MISC_REG1), bf_sel_bits_pcie_force, 0);

	/*
	 * 4. Change RX wait
	 */
	reg_set16(phy_addr(PCIE, PWR_MGM_TIM1), 0x10C, 0xFFFF);

	/*
	 * 5. Enable idle sync
	 */
	reg_set16(phy_addr(PCIE, UNIT_CTRL), 0x60 | rb_idle_sync_en, 0xFFFF);

	/*
	 * 6. Enable the output of 100M/125M/500M clock
	 */
	reg_set16(phy_addr(PCIE, MISC_REG0),
		  0xA00D | rb_clk500m_en | rb_txdclk_2x_sel | rb_clk100m_125m_en, 0xFFFF);

	/*
	 * 7. Enable TX
	 */
	reg_set(PCIE_REF_CLK_ADDR, 0x1342, 0xFFFFFFFF);

	/*
	 * 8. Check crystal jumper setting and program the Power and PLL
	 *    Control accordingly
	 */
	if (get_ref_clk() == 40) {
		/* 40 MHz */
		reg_set16(phy_addr(PCIE, PWR_PLL_CTRL), 0xFC63, 0xFFFF);
	} else {
		/* 25 MHz */
		reg_set16(phy_addr(PCIE, PWR_PLL_CTRL), 0xFC62, 0xFFFF);
	}

	/*
	 * 9. Override Speed_PLL value and use MAC PLL
	 */
	reg_set16(phy_addr(PCIE, KVCO_CAL_CTRL), 0x0040 | rb_use_max_pll_rate,
		  0xFFFF);

	/*
	 * 10. Check the Polarity invert bit
	 */
	if (invert & COMPHY_POLARITY_TXD_INVERT)
		reg_set16(phy_addr(PCIE, SYNC_PATTERN), phy_txd_inv, 0);
	else
		reg_set16(phy_addr(PCIE, SYNC_PATTERN), 0, phy_txd_inv);

	if (invert & COMPHY_POLARITY_RXD_INVERT)
		reg_set16(phy_addr(PCIE, SYNC_PATTERN), phy_rxd_inv, 0);
	else
		reg_set16(phy_addr(PCIE, SYNC_PATTERN), 0, phy_rxd_inv);

	/*
	 * 11. Release SW reset
	 */
	reg_set16(phy_addr(PCIE, GLOB_PHY_CTRL0),
		  rb_mode_core_clk_freq_sel | rb_mode_pipe_width_32,
		  bf_soft_rst | bf_mode_refdiv);

	/* Wait for > 55 us to allow PCLK be enabled */
	udelay(PLL_SET_DELAY_US);

	/* Assert PCLK enabled */
	ret = comphy_poll_reg(phy_addr(PCIE, LANE_STAT1),	/* address */
			      rb_txdclk_pclk_en,		/* value */
			      rb_txdclk_pclk_en,		/* mask */
			      POLL_16B_REG);			/* 16bit */
	if (!ret)
		printf("Failed to lock PCIe PLL\n");

	debug_exit();

	/* Return the status of the PLL */
	return ret;
}

/*
 * reg_set_indirect
 *
 * return: void
 */
static void reg_set_indirect(u32 reg, u16 data, u16 mask)
{
	reg_set(rh_vsreg_addr, reg, 0xFFFFFFFF);
	reg_set(rh_vsreg_data, data, mask);
}

/*
 * comphy_sata_power_up
 *
 * return: 1 if PLL locked (OK), 0 otherwise (FAIL)
 */
static int comphy_sata_power_up(u32 invert)
{
	int ret;
	u32 data = 0;

	debug_enter();

	/*
	 * 0. Check the Polarity invert bits
	 */
	if (invert & COMPHY_POLARITY_TXD_INVERT)
		data |= bs_txd_inv;

	if (invert & COMPHY_POLARITY_RXD_INVERT)
		data |= bs_rxd_inv;

	reg_set_indirect(vphy_sync_pattern_reg, data, bs_txd_inv | bs_rxd_inv);

	/*
	 * 1. Select 40-bit data width width
	 */
	reg_set_indirect(vphy_loopback_reg0, 0x800, bs_phyintf_40bit);

	/*
	 * 2. Select reference clock and PHY mode (SATA)
	 */
	if (get_ref_clk() == 40) {
		/* 40 MHz */
		reg_set_indirect(vphy_power_reg0, 0x3, 0x00FF);
	} else {
		/* 20 MHz */
		reg_set_indirect(vphy_power_reg0, 0x1, 0x00FF);
	}

	/*
	 * 3. Use maximum PLL rate (no power save)
	 */
	reg_set_indirect(vphy_calctl_reg, bs_max_pll_rate, bs_max_pll_rate);

	/*
	 * 4. Reset reserved bit (??)
	 */
	reg_set_indirect(vphy_reserve_reg, 0, bs_phyctrl_frm_pin);

	/*
	 * 5. Set vendor-specific configuration (??)
	 */
	reg_set(rh_vs0_a, vsata_ctrl_reg, 0xFFFFFFFF);
	reg_set(rh_vs0_d, bs_phy_pu_pll, bs_phy_pu_pll);

	/* Wait for > 55 us to allow PLL be enabled */
	udelay(PLL_SET_DELAY_US);

	/* Assert SATA PLL enabled */
	reg_set(rh_vsreg_addr, vphy_loopback_reg0, 0xFFFFFFFF);
	ret = comphy_poll_reg(rh_vsreg_data,	/* address */
			      bs_pll_ready_tx,	/* value */
			      bs_pll_ready_tx,	/* mask */
			      POLL_32B_REG);	/* 32bit */
	if (!ret)
		printf("Failed to lock SATA PLL\n");

	debug_exit();

	return ret;
}

/*
 * usb3_reg_set16
 *
 * return: void
 */
static void usb3_reg_set16(u32 reg, u16 data, u16 mask, u32 lane)
{
	/*
	 * When Lane 2 PHY is for USB3, access the PHY registers
	 * through indirect Address and Data registers INDIR_ACC_PHY_ADDR
	 * (RD00E0178h [31:0]) and INDIR_ACC_PHY_DATA (RD00E017Ch [31:0])
	 * within the SATA Host Controller registers, Lane 2 base register
	 * offset is 0x200
	 */

	if (lane == 2)
		reg_set_indirect(USB3PHY_LANE2_REG_BASE_OFFSET + reg, data,
				 mask);
	else
		reg_set16(phy_addr(USB3, reg), data, mask);
}

/*
 * comphy_usb3_power_up
 *
 * return: 1 if PLL locked (OK), 0 otherwise (FAIL)
 */
static int comphy_usb3_power_up(u32 lane, u32 type, u32 speed, u32 invert)
{
	int ret;

	debug_enter();

	/*
	 * 1. Power up OTG module
	 */
	reg_set(USB2_PHY_OTG_CTRL_ADDR, rb_pu_otg, 0);

	/*
	 * 2. Set counter for 100us pulse in USB3 Host and Device
	 * restore default burst size limit (Reference Clock 31:24)
	 */
	reg_set(USB3_CTRPUL_VAL_REG, 0x8 << 24, rb_usb3_ctr_100ns);

	/* 0xd005c300 = 0x1001 */
	/* set PRD_TXDEEMPH (3.5db de-emph) */
	usb3_reg_set16(LANE_CFG0, 0x1, 0xFF, lane);

	/*
	 * Set BIT0: enable transmitter in high impedance mode
	 * Set BIT[3:4]: delay 2 clock cycles for HiZ off latency
	 * Set BIT6: Tx detect Rx at HiZ mode
	 * Unset BIT15: set to 0 to set USB3 De-emphasize level to -3.5db
	 *              together with bit 0 of COMPHY_REG_LANE_CFG0_ADDR
	 *              register
	 */
	usb3_reg_set16(LANE_CFG1,
		       tx_det_rx_mode | gen2_tx_data_dly_deft
		       | tx_elec_idle_mode_en,
		       prd_txdeemph1_mask | tx_det_rx_mode
		       | gen2_tx_data_dly_mask | tx_elec_idle_mode_en, lane);

	/* 0xd005c310 = 0x93: set Spread Spectrum Clock Enabled */
	usb3_reg_set16(LANE_CFG4, bf_spread_spectrum_clock_en, 0x80, lane);

	/*
	 * set Override Margining Controls From the MAC: Use margining signals
	 * from lane configuration
	 */
	usb3_reg_set16(TEST_MODE_CTRL, rb_mode_margin_override, 0xFFFF, lane);

	/* set Lane-to-Lane Bundle Clock Sampling Period = per PCLK cycles */
	/* set Mode Clock Source = PCLK is generated from REFCLK */
	usb3_reg_set16(GLOB_CLK_SRC_LO, 0x0, 0xFF, lane);

	/* set G2 Spread Spectrum Clock Amplitude at 4K */
	usb3_reg_set16(GEN2_SETTINGS_2, g2_tx_ssc_amp, 0xF000, lane);

	/*
	 * unset G3 Spread Spectrum Clock Amplitude & set G3 TX and RX Register
	 * Master Current Select
	 */
	usb3_reg_set16(GEN2_SETTINGS_3, 0x0, 0xFFFF, lane);

	/*
	 * 3. Check crystal jumper setting and program the Power and PLL
	 * Control accordingly
	 * 4. Change RX wait
	 */
	if (get_ref_clk() == 40) {
		/* 40 MHz */
		usb3_reg_set16(PWR_PLL_CTRL, 0xFCA3, 0xFFFF, lane);
		usb3_reg_set16(PWR_MGM_TIM1, 0x10C, 0xFFFF, lane);
	} else {
		/* 25 MHz */
		usb3_reg_set16(PWR_PLL_CTRL, 0xFCA2, 0xFFFF, lane);
		usb3_reg_set16(PWR_MGM_TIM1, 0x107, 0xFFFF, lane);
	}

	/*
	 * 5. Enable idle sync
	 */
	usb3_reg_set16(UNIT_CTRL, 0x60 | rb_idle_sync_en, 0xFFFF, lane);

	/*
	 * 6. Enable the output of 500M clock
	 */
	usb3_reg_set16(MISC_REG0, 0xA00D | rb_clk500m_en, 0xFFFF, lane);

	/*
	 * 7. Set 20-bit data width
	 */
	usb3_reg_set16(DIG_LB_EN, 0x0400, 0xFFFF, lane);

	/*
	 * 8. Override Speed_PLL value and use MAC PLL
	 */
	usb3_reg_set16(KVCO_CAL_CTRL, 0x0040 | rb_use_max_pll_rate, 0xFFFF,
		       lane);

	/*
	 * 9. Check the Polarity invert bit
	 */
	if (invert & COMPHY_POLARITY_TXD_INVERT)
		usb3_reg_set16(SYNC_PATTERN, phy_txd_inv, 0, lane);
	else
		usb3_reg_set16(SYNC_PATTERN, 0, phy_txd_inv, lane);

	if (invert & COMPHY_POLARITY_RXD_INVERT)
		usb3_reg_set16(SYNC_PATTERN, phy_rxd_inv, 0, lane);
	else
		usb3_reg_set16(SYNC_PATTERN, 0, phy_rxd_inv, lane);

	/*
	 * 10. Set max speed generation to USB3.0 5Gbps
	 */
	usb3_reg_set16(SYNC_MASK_GEN, 0x0400, 0x0C00, lane);

	/*
	 * 11. Set capacitor value for FFE gain peaking to 0xF
	 */
	usb3_reg_set16(GEN3_SETTINGS_3, 0xF, 0xF, lane);

	/*
	 * 12. Release SW reset
	 */
	usb3_reg_set16(GLOB_PHY_CTRL0,
		       rb_mode_core_clk_freq_sel | rb_mode_pipe_width_32
		       | 0x20, 0xFFFF, lane);

	/* Wait for > 55 us to allow PCLK be enabled */
	udelay(PLL_SET_DELAY_US);

	/* Assert PCLK enabled */
	if (lane == 2) {
		reg_set(rh_vsreg_addr,
			LANE_STAT1 + USB3PHY_LANE2_REG_BASE_OFFSET,
			0xFFFFFFFF);
		ret = comphy_poll_reg(rh_vsreg_data,		/* address */
				      rb_txdclk_pclk_en,	/* value */
				      rb_txdclk_pclk_en,	/* mask */
				      POLL_32B_REG);		/* 32bit */
	} else {
		ret = comphy_poll_reg(phy_addr(USB3, LANE_STAT1), /* address */
				      rb_txdclk_pclk_en,	  /* value */
				      rb_txdclk_pclk_en,	  /* mask */
				      POLL_16B_REG);		  /* 16bit */
	}
	if (!ret)
		printf("Failed to lock USB3 PLL\n");

	/*
	 * Set Soft ID for Host mode (Device mode works with Hard ID
	 * detection)
	 */
	if (type == COMPHY_TYPE_USB3_HOST0) {
		/*
		 * set   BIT0: set ID_MODE of Host/Device = "Soft ID" (BIT1)
		 * clear BIT1: set SOFT_ID = Host
		 * set   BIT4: set INT_MODE = ID. Interrupt Mode: enable
		 *             interrupt by ID instead of using both interrupts
		 *             of HOST and Device ORed simultaneously
		 *             INT_MODE=ID in order to avoid unexpected
		 *             behaviour or both interrupts together
		 */
		reg_set(USB32_CTRL_BASE,
			usb32_ctrl_id_mode | usb32_ctrl_int_mode,
			usb32_ctrl_id_mode | usb32_ctrl_soft_id |
			usb32_ctrl_int_mode);
	}

	debug_exit();

	return ret;
}

/*
 * comphy_usb2_power_up
 *
 * return: 1 if PLL locked (OK), 0 otherwise (FAIL)
 */
static int comphy_usb2_power_up(u8 usb32)
{
	int ret;

	debug_enter();

	if (usb32 != 0 && usb32 != 1) {
		printf("invalid usb32 value: (%d), should be either 0 or 1\n",
		       usb32);
		debug_exit();
		return 0;
	}

	/*
	 * 0. Setup PLL. 40MHz clock uses defaults.
	 *    See "PLL Settings for Typical REFCLK" table
	 */
	if (get_ref_clk() == 25) {
		reg_set(USB2_PHY_BASE(usb32), 5 | (96 << 16),
			0x3F | (0xFF << 16) | (0x3 << 28));
	}

	/*
	 * 1. PHY pull up and disable USB2 suspend
	 */
	reg_set(USB2_PHY_CTRL_ADDR(usb32),
		RB_USB2PHY_SUSPM(usb32) | RB_USB2PHY_PU(usb32), 0);

	if (usb32 != 0) {
		/*
		 * 2. Power up OTG module
		 */
		reg_set(USB2_PHY_OTG_CTRL_ADDR, rb_pu_otg, 0);

		/*
		 * 3. Configure PHY charger detection
		 */
		reg_set(USB2_PHY_CHRGR_DET_ADDR, 0,
			rb_cdp_en | rb_dcp_en | rb_pd_en | rb_cdp_dm_auto |
			rb_enswitch_dp | rb_enswitch_dm | rb_pu_chrg_dtc);
	}

	/* Assert PLL calibration done */
	ret = comphy_poll_reg(USB2_PHY_CAL_CTRL_ADDR(usb32),
			      rb_usb2phy_pllcal_done,	/* value */
			      rb_usb2phy_pllcal_done,	/* mask */
			      POLL_32B_REG);		/* 32bit */
	if (!ret) {
		printf("Failed to end USB2 PLL calibration\n");
		goto out;
	}

	/* Assert impedance calibration done */
	ret = comphy_poll_reg(USB2_PHY_CAL_CTRL_ADDR(usb32),
			      rb_usb2phy_impcal_done,	/* value */
			      rb_usb2phy_impcal_done,	/* mask */
			      POLL_32B_REG);		/* 32bit */
	if (!ret) {
		printf("Failed to end USB2 impedance calibration\n");
		goto out;
	}

	/* Assert squetch calibration done */
	ret = comphy_poll_reg(USB2_PHY_RX_CHAN_CTRL1_ADDR(usb32),
			      rb_usb2phy_sqcal_done,	/* value */
			      rb_usb2phy_sqcal_done,	/* mask */
			      POLL_32B_REG);		/* 32bit */
	if (!ret) {
		printf("Failed to end USB2 unknown calibration\n");
		goto out;
	}

	/* Assert PLL is ready */
	ret = comphy_poll_reg(USB2_PHY_PLL_CTRL0_ADDR(usb32),
			      rb_usb2phy_pll_ready,		/* value */
			      rb_usb2phy_pll_ready,		/* mask */
			      POLL_32B_REG);		/* 32bit */

	if (!ret) {
		printf("Failed to lock USB2 PLL\n");
		goto out;
	}

out:
	debug_exit();

	return ret;
}

/*
 * comphy_emmc_power_up
 *
 * return: 1 if PLL locked (OK), 0 otherwise (FAIL)
 */
static int comphy_emmc_power_up(void)
{
	debug_enter();

	/*
	 * 1. Bus power ON, Bus voltage 1.8V
	 */
	reg_set(SDIO_HOST_CTRL1_ADDR, 0xB00, 0xF00);

	/*
	 * 2. Set FIFO parameters
	 */
	reg_set(SDIO_SDHC_FIFO_ADDR, 0x315, 0xFFFFFFFF);

	/*
	 * 3. Set Capabilities 1_2
	 */
	reg_set(SDIO_CAP_12_ADDR, 0x25FAC8B2, 0xFFFFFFFF);

	/*
	 * 4. Set Endian
	 */
	reg_set(SDIO_ENDIAN_ADDR, 0x00c00000, 0);

	/*
	 * 4. Init PHY
	 */
	reg_set(SDIO_PHY_TIMING_ADDR, 0x80000000, 0x80000000);
	reg_set(SDIO_PHY_PAD_CTRL0_ADDR, 0x50000000, 0xF0000000);

	/*
	 * 5. DLL reset
	 */
	reg_set(SDIO_DLL_RST_ADDR, 0xFFFEFFFF, 0);
	reg_set(SDIO_DLL_RST_ADDR, 0x00010000, 0);

	debug_exit();

	return 1;
}

/*
 * comphy_sgmii_power_up
 *
 * return:
 */
static void comphy_sgmii_phy_init(u32 lane, u32 speed)
{
	const int fix_arr_sz = ARRAY_SIZE(sgmii_phy_init_fix);
	int addr, fix_idx;
	u16 val;

	fix_idx = 0;
	for (addr = 0; addr < 512; addr++) {
		/*
		 * All PHY register values are defined in full for 3.125Gbps
		 * SERDES speed. The values required for 1.25 Gbps are almost
		 * the same and only few registers should be "fixed" in
		 * comparison to 3.125 Gbps values. These register values are
		 * stored in "sgmii_phy_init_fix" array.
		 */
		if (speed != COMPHY_SPEED_1_25G &&
		    sgmii_phy_init_fix[fix_idx].addr == addr) {
			/* Use new value */
			val = sgmii_phy_init_fix[fix_idx].value;
			if (fix_idx < fix_arr_sz)
				fix_idx++;
		} else {
			val = sgmii_phy_init[addr];
		}

		reg_set16(sgmiiphy_addr(lane, addr), val, 0xFFFF);
	}
}

/*
 * comphy_sgmii_power_up
 *
 * return: 1 if PLL locked (OK), 0 otherwise (FAIL)
 */
static int comphy_sgmii_power_up(u32 lane, u32 speed, u32 invert)
{
	int ret;
	u32 saved_selector;

	debug_enter();

	/*
	 * 1. Configure PHY to SATA/SAS mode by setting pin PIN_PIPE_SEL=0
	 */
	saved_selector = readl(COMPHY_SEL_ADDR);
	reg_set(COMPHY_SEL_ADDR, 0, 0xFFFFFFFF);

	/*
	 * 2. Reset PHY by setting PHY input port PIN_RESET=1.
	 * 3. Set PHY input port PIN_TX_IDLE=1, PIN_PU_IVREF=1 to keep
	 *    PHY TXP/TXN output to idle state during PHY initialization
	 * 4. Set PHY input port PIN_PU_PLL=0, PIN_PU_RX=0, PIN_PU_TX=0.
	 */
	reg_set(COMPHY_PHY_CFG1_ADDR(lane),
		rb_pin_reset_comphy | rb_pin_tx_idle | rb_pin_pu_iveref,
		rb_pin_reset_core | rb_pin_pu_pll |
		rb_pin_pu_rx | rb_pin_pu_tx);

	/*
	 * 5. Release reset to the PHY by setting PIN_RESET=0.
	 */
	reg_set(COMPHY_PHY_CFG1_ADDR(lane), 0, rb_pin_reset_comphy);

	/*
	 * 7. Set PIN_PHY_GEN_TX[3:0] and PIN_PHY_GEN_RX[3:0] to decide
	 *    COMPHY bit rate
	 */
	if (speed == COMPHY_SPEED_3_125G) { /* 3.125 GHz */
		reg_set(COMPHY_PHY_CFG1_ADDR(lane),
			(0x8 << rf_gen_rx_sel_shift) |
			(0x8 << rf_gen_tx_sel_shift),
			rf_gen_rx_select | rf_gen_tx_select);

	} else if (speed == COMPHY_SPEED_1_25G) { /* 1.25 GHz */
		reg_set(COMPHY_PHY_CFG1_ADDR(lane),
			(0x6 << rf_gen_rx_sel_shift) |
			(0x6 << rf_gen_tx_sel_shift),
			rf_gen_rx_select | rf_gen_tx_select);
	} else {
		printf("Unsupported COMPHY speed!\n");
		return 0;
	}

	/*
	 * 8. Wait 1mS for bandgap and reference clocks to stabilize;
	 *    then start SW programming.
	 */
	mdelay(10);

	/* 9. Program COMPHY register PHY_MODE */
	reg_set16(sgmiiphy_addr(lane, PWR_PLL_CTRL),
		  PHY_MODE_SGMII << rf_phy_mode_shift, rf_phy_mode_mask);

	/*
	 * 10. Set COMPHY register REFCLK_SEL to select the correct REFCLK
	 *     source
	 */
	reg_set16(sgmiiphy_addr(lane, MISC_REG0), 0, rb_ref_clk_sel);

	/*
	 * 11. Set correct reference clock frequency in COMPHY register
	 *     REF_FREF_SEL.
	 */
	if (get_ref_clk() == 40) {
		reg_set16(sgmiiphy_addr(lane, PWR_PLL_CTRL),
			  0x4 << rf_ref_freq_sel_shift, rf_ref_freq_sel_mask);
	} else {
		/* 25MHz */
		reg_set16(sgmiiphy_addr(lane, PWR_PLL_CTRL),
			  0x1 << rf_ref_freq_sel_shift, rf_ref_freq_sel_mask);
	}

	/* 12. Program COMPHY register PHY_GEN_MAX[1:0] */
	/*
	 * This step is mentioned in the flow received from verification team.
	 * However the PHY_GEN_MAX value is only meaningful for other
	 * interfaces (not SGMII). For instance, it selects SATA speed
	 * 1.5/3/6 Gbps or PCIe speed  2.5/5 Gbps
	 */

	/*
	 * 13. Program COMPHY register SEL_BITS to set correct parallel data
	 *     bus width
	 */
	/* 10bit */
	reg_set16(sgmiiphy_addr(lane, DIG_LB_EN), 0, rf_data_width_mask);

	/*
	 * 14. As long as DFE function needs to be enabled in any mode,
	 *     COMPHY register DFE_UPDATE_EN[5:0] shall be programmed to 0x3F
	 *     for real chip during COMPHY power on.
	 */
	/*
	 * The step 14 exists (and empty) in the original initialization flow
	 * obtained from the verification team. According to the functional
	 * specification DFE_UPDATE_EN already has the default value 0x3F
	 */

	/*
	 * 15. Program COMPHY GEN registers.
	 *     These registers should be programmed based on the lab testing
	 *     result to achieve optimal performance. Please contact the CEA
	 *     group to get the related GEN table during real chip bring-up.
	 *     We only requred to run though the entire registers programming
	 *     flow defined by "comphy_sgmii_phy_init" when the REF clock is
	 *     40 MHz. For REF clock 25 MHz the default values stored in PHY
	 *     registers are OK.
	 */
	debug("Running C-DPI phy init %s mode\n",
	      speed == COMPHY_SPEED_3_125G ? "2G5" : "1G");
	if (get_ref_clk() == 40)
		comphy_sgmii_phy_init(lane, speed);

	/*
	 * 16. [Simulation Only] should not be used for real chip.
	 *     By pass power up calibration by programming EXT_FORCE_CAL_DONE
	 *     (R02h[9]) to 1 to shorten COMPHY simulation time.
	 */
	/*
	 * 17. [Simulation Only: should not be used for real chip]
	 *     Program COMPHY register FAST_DFE_TIMER_EN=1 to shorten RX
	 *     training simulation time.
	 */

	/*
	 * 18. Check the PHY Polarity invert bit
	 */
	if (invert & COMPHY_POLARITY_TXD_INVERT)
		reg_set16(sgmiiphy_addr(lane, SYNC_PATTERN), phy_txd_inv, 0);
	else
		reg_set16(sgmiiphy_addr(lane, SYNC_PATTERN), 0, phy_txd_inv);

	if (invert & COMPHY_POLARITY_RXD_INVERT)
		reg_set16(sgmiiphy_addr(lane, SYNC_PATTERN), phy_rxd_inv, 0);
	else
		reg_set16(sgmiiphy_addr(lane, SYNC_PATTERN), 0, phy_rxd_inv);

	/*
	 * 19. Set PHY input ports PIN_PU_PLL, PIN_PU_TX and PIN_PU_RX to 1
	 *     to start PHY power up sequence. All the PHY register
	 *     programming should be done before PIN_PU_PLL=1. There should be
	 *     no register programming for normal PHY operation from this point.
	 */
	reg_set(COMPHY_PHY_CFG1_ADDR(lane),
		rb_pin_pu_pll | rb_pin_pu_rx | rb_pin_pu_tx,
		rb_pin_pu_pll | rb_pin_pu_rx | rb_pin_pu_tx);

	/*
	 * 20. Wait for PHY power up sequence to finish by checking output ports
	 *     PIN_PLL_READY_TX=1 and PIN_PLL_READY_RX=1.
	 */
	ret = comphy_poll_reg(COMPHY_PHY_STAT1_ADDR(lane),	/* address */
			      rb_pll_ready_tx | rb_pll_ready_rx, /* value */
			      rb_pll_ready_tx | rb_pll_ready_rx, /* mask */
			      POLL_32B_REG);			/* 32bit */
	if (!ret) {
		printf("Failed to lock PLL for SGMII PHY %d\n", lane);
		goto out;
	}

	/*
	 * 21. Set COMPHY input port PIN_TX_IDLE=0
	 */
	reg_set(COMPHY_PHY_CFG1_ADDR(lane), 0x0, rb_pin_tx_idle);

	/*
	 * 22. After valid data appear on PIN_RXDATA bus, set PIN_RX_INIT=1.
	 *     to start RX initialization. PIN_RX_INIT_DONE will be cleared to
	 *     0 by the PHY. After RX initialization is done, PIN_RX_INIT_DONE
	 *     will be set to 1 by COMPHY. Set PIN_RX_INIT=0 after
	 *     PIN_RX_INIT_DONE= 1.
	 *     Please refer to RX initialization part for details.
	 */
	reg_set(COMPHY_PHY_CFG1_ADDR(lane), rb_phy_rx_init, 0x0);

	ret = comphy_poll_reg(COMPHY_PHY_STAT1_ADDR(lane), /* address */
			      rb_rx_init_done,			/* value */
			      rb_rx_init_done,			/* mask */
			      POLL_32B_REG);			/* 32bit */
	if (!ret) {
		printf("Failed to init RX of SGMII PHY %d\n", lane);
		goto out;
	}

	/*
	 * Restore saved selector.
	 */
	reg_set(COMPHY_SEL_ADDR, saved_selector, 0xFFFFFFFF);

out:
	debug_exit();

	return ret;
}

void comphy_dedicated_phys_init(void)
{
	int node, usb32, ret = 1;
	const void *blob = gd->fdt_blob;

	debug_enter();

	for (usb32 = 0; usb32 <= 1; usb32++) {
		/*
		 * There are 2 UTMI PHYs in this SOC.
		 * One is independendent and one is paired with USB3 port (OTG)
		 */
		if (usb32 == 0) {
			node = fdt_node_offset_by_compatible(
				blob, -1, "marvell,armada-3700-ehci");
		} else {
			node = fdt_node_offset_by_compatible(
				blob, -1, "marvell,armada3700-xhci");
		}

		if (node > 0) {
			if (fdtdec_get_is_enabled(blob, node)) {
				ret = comphy_usb2_power_up(usb32);
				if (!ret)
					printf("Failed to initialize UTMI PHY\n");
				else
					debug("UTMI PHY init succeed\n");
			} else {
				debug("USB%d node is disabled\n",
				      usb32 == 0 ? 2 : 3);
			}
		} else {
			debug("No USB%d node in DT\n", usb32 == 0 ? 2 : 3);
		}
	}

	node = fdt_node_offset_by_compatible(blob, -1,
					     "marvell,armada-8k-sdhci");
	if (node <= 0) {
		node = fdt_node_offset_by_compatible(
			blob, -1, "marvell,armada-3700-sdhci");
	}

	if (node > 0) {
		if (fdtdec_get_is_enabled(blob, node)) {
			ret = comphy_emmc_power_up();
			if (!ret)
				printf("Failed to initialize SDIO/eMMC PHY\n");
			else
				debug("SDIO/eMMC PHY init succeed\n");
		} else {
			debug("SDIO/eMMC node is disabled\n");
		}
	}  else {
		debug("No SDIO/eMMC node in DT\n");
	}

	debug_exit();
}

static int find_available_node_by_compatible(int offset, const char *compatible)
{
	fdt_for_each_node_by_compatible(offset, gd->fdt_blob, offset,
					compatible)
		if (fdtdec_get_is_enabled(gd->fdt_blob, offset))
			return offset;

	return -1;
}

static bool comphy_a3700_find_lane(const int nodes[3], int node,
				   int port, int *lane, int *invert)
{
	int res, i, j;

	for (i = 0; ; i++) {
		struct fdtdec_phandle_args args;

		res = fdtdec_parse_phandle_with_args(gd->fdt_blob, node, "phys",
						     "#phy-cells", 0, i, &args);
		if (res)
			return false;

		for (j = 0; j < 3; j++) {
			if (nodes[j] >= 0 && args.node == nodes[j] &&
			    (args.args_count >= 1 ? args.args[0] : 0) == port) {
				*lane = j;
				*invert = args.args_count >= 2 ? args.args[1]
							       : 0;
				return true;
			}
		}
	}

	return false;
}

static void comphy_a3700_fill_cfg(struct chip_serdes_phy_config *cfg,
				  const int nodes[3], const char *compatible,
				  int type)
{
	int node, lane, port, speed, invert;

	port = (type == COMPHY_TYPE_SGMII1) ? 1 : 0;

	node = -1;
	while (1) {
		node = find_available_node_by_compatible(node, compatible);
		if (node < 0)
			return;

		if (comphy_a3700_find_lane(nodes, node, port, &lane, &invert))
			break;
	}

	if (cfg->comphy_map_data[lane].type != COMPHY_TYPE_UNCONNECTED) {
		printf("Error: More PHYs defined for lane %d, skipping\n",
		       lane);
		return;
	}

	if (type == COMPHY_TYPE_SGMII0 || type == COMPHY_TYPE_SGMII1) {
		const char *phy_mode;

		phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
		if (phy_mode &&
		    !strcmp(phy_mode,
			    phy_string_for_interface(PHY_INTERFACE_MODE_2500BASEX)))
			speed = COMPHY_SPEED_3_125G;
		else
			speed = COMPHY_SPEED_1_25G;
	} else if (type == COMPHY_TYPE_SATA0) {
		speed = COMPHY_SPEED_6G;
	} else {
		speed = COMPHY_SPEED_5G;
	}

	cfg->comphy_map_data[lane].type = type;
	cfg->comphy_map_data[lane].speed = speed;
	cfg->comphy_map_data[lane].invert = invert;
}

static const fdt32_t comphy_a3700_mux_lane_order[3] = {
	__constant_cpu_to_be32(1),
	__constant_cpu_to_be32(0),
	__constant_cpu_to_be32(2),
};

int comphy_a3700_init_serdes_map(int node, struct chip_serdes_phy_config *cfg)
{
	int comphy_nodes[3];
	int child, i;

	for (i = 0; i < ARRAY_SIZE(comphy_nodes); i++)
		comphy_nodes[i] = -FDT_ERR_NOTFOUND;

	fdt_for_each_subnode(child, gd->fdt_blob, node) {
		if (!fdtdec_get_is_enabled(gd->fdt_blob, child))
			continue;

		i = fdtdec_get_int(gd->fdt_blob, child, "reg", -1);
		if (i < 0 || i >= ARRAY_SIZE(comphy_nodes))
			continue;

		comphy_nodes[i] = child;
	}

	for (i = 0; i < ARRAY_SIZE(comphy_nodes); i++) {
		cfg->comphy_map_data[i].type = COMPHY_TYPE_UNCONNECTED;
		cfg->comphy_map_data[i].speed = COMPHY_SPEED_INVALID;
	}

	comphy_a3700_fill_cfg(cfg, comphy_nodes, "marvell,armada3700-u3d",
			      COMPHY_TYPE_USB3_DEVICE);
	comphy_a3700_fill_cfg(cfg, comphy_nodes, "marvell,armada3700-xhci",
			      COMPHY_TYPE_USB3_HOST0);
	comphy_a3700_fill_cfg(cfg, comphy_nodes, "marvell,armada-3700-pcie",
			      COMPHY_TYPE_PEX0);
	comphy_a3700_fill_cfg(cfg, comphy_nodes, "marvell,armada-3700-ahci",
			      COMPHY_TYPE_SATA0);
	comphy_a3700_fill_cfg(cfg, comphy_nodes, "marvell,armada-3700-neta",
			      COMPHY_TYPE_SGMII0);
	comphy_a3700_fill_cfg(cfg, comphy_nodes, "marvell,armada-3700-neta",
			      COMPHY_TYPE_SGMII1);

	cfg->comphy_lanes_count = 3;
	cfg->comphy_mux_bitcount = 4;
	cfg->comphy_mux_lane_order = comphy_a3700_mux_lane_order;

	return 0;
}

int comphy_a3700_init(struct chip_serdes_phy_config *chip_cfg,
		      struct comphy_map *serdes_map)
{
	struct comphy_map *comphy_map;
	u32 comphy_max_count = chip_cfg->comphy_lanes_count;
	u32 lane, ret = 0;

	debug_enter();

	/* Initialize PHY mux */
	chip_cfg->mux_data = a3700_comphy_mux_data;
	comphy_mux_init(chip_cfg, serdes_map, COMPHY_SEL_ADDR);

	for (lane = 0, comphy_map = serdes_map; lane < comphy_max_count;
	     lane++, comphy_map++) {
		debug("Initialize serdes number %d\n", lane);
		debug("Serdes type = 0x%x invert=%d\n",
		      comphy_map->type, comphy_map->invert);

		switch (comphy_map->type) {
		case COMPHY_TYPE_UNCONNECTED:
			continue;
			break;

		case COMPHY_TYPE_PEX0:
			ret = comphy_pcie_power_up(comphy_map->speed,
						   comphy_map->invert);
			break;

		case COMPHY_TYPE_USB3_HOST0:
		case COMPHY_TYPE_USB3_DEVICE:
			ret = comphy_usb3_power_up(lane,
						   comphy_map->type,
						   comphy_map->speed,
						   comphy_map->invert);
			break;

		case COMPHY_TYPE_SGMII0:
		case COMPHY_TYPE_SGMII1:
			ret = comphy_sgmii_power_up(lane, comphy_map->speed,
						    comphy_map->invert);
			break;

		case COMPHY_TYPE_SATA0:
			ret = comphy_sata_power_up(comphy_map->invert);
			break;

		default:
			debug("Unknown SerDes type, skip initialize SerDes %d\n",
			      lane);
			ret = 1;
			break;
		}
		if (!ret)
			printf("PLL is not locked - Failed to initialize lane %d\n",
			       lane);
	}

	debug_exit();
	return ret;
}