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
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
// SPDX-License-Identifier: Intel
/*
 * Copyright (C) 2013, Intel Corporation
 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
 *
 * Ported from Intel released Quark UEFI BIOS
 * QuarkSocPkg/QuarkNorthCluster/MemoryInit/Pei
 */

#include <hang.h>
#include <asm/arch/device.h>
#include <asm/arch/mrc.h>
#include <asm/arch/msg_port.h>
#include <asm/arch/quark.h>
#include <asm/u-boot-x86.h>
#include "mrc_util.h"
#include "hte.h"
#include "smc.h"

static const uint8_t vref_codes[64] = {
	/* lowest to highest */
	0x3f, 0x3e, 0x3d, 0x3c, 0x3b, 0x3a, 0x39, 0x38,
	0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30,
	0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28,
	0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20,
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};

void mrc_write_mask(u32 unit, u32 addr, u32 data, u32 mask)
{
	msg_port_write(unit, addr,
		       (msg_port_read(unit, addr) & ~(mask)) |
		       ((data) & (mask)));
}

void mrc_alt_write_mask(u32 unit, u32 addr, u32 data, u32 mask)
{
	msg_port_alt_write(unit, addr,
			   (msg_port_alt_read(unit, addr) & ~(mask)) |
			   ((data) & (mask)));
}

void mrc_post_code(uint8_t major, uint8_t minor)
{
	/* send message to UART */
	DPF(D_INFO, "POST: 0x%01x%02x\n", major, minor);

	/* error check */
	if (major == 0xee)
		hang();
}

/* Delay number of nanoseconds */
void delay_n(uint32_t ns)
{
	/* 1000 MHz clock has 1ns period --> no conversion required */
	uint64_t final_tsc = rdtsc();

	final_tsc += ((get_tbclk_mhz() * ns) / 1000);

	while (rdtsc() < final_tsc)
		;
}

/* Delay number of microseconds */
void delay_u(uint32_t ms)
{
	/* 64-bit math is not an option, just use loops */
	while (ms--)
		delay_n(1000);
}

/* Select Memory Manager as the source for PRI interface */
void select_mem_mgr(void)
{
	u32 dco;

	ENTERFN();

	dco = msg_port_read(MEM_CTLR, DCO);
	dco &= ~DCO_PMICTL;
	msg_port_write(MEM_CTLR, DCO, dco);

	LEAVEFN();
}

/* Select HTE as the source for PRI interface */
void select_hte(void)
{
	u32 dco;

	ENTERFN();

	dco = msg_port_read(MEM_CTLR, DCO);
	dco |= DCO_PMICTL;
	msg_port_write(MEM_CTLR, DCO, dco);

	LEAVEFN();
}

/*
 * Send DRAM command
 * data should be formated using DCMD_Xxxx macro or emrsXCommand structure
 */
void dram_init_command(uint32_t data)
{
	qrk_pci_write_config_dword(QUARK_HOST_BRIDGE, MSG_DATA_REG, data);
	qrk_pci_write_config_dword(QUARK_HOST_BRIDGE, MSG_CTRL_EXT_REG, 0);
	msg_port_setup(MSG_OP_DRAM_INIT, MEM_CTLR, 0);

	DPF(D_REGWR, "WR32 %03X %08X %08X\n", MEM_CTLR, 0, data);
}

/* Send DRAM wake command using special MCU side-band WAKE opcode */
void dram_wake_command(void)
{
	ENTERFN();

	msg_port_setup(MSG_OP_DRAM_WAKE, MEM_CTLR, 0);

	LEAVEFN();
}

void training_message(uint8_t channel, uint8_t rank, uint8_t byte_lane)
{
	/* send message to UART */
	DPF(D_INFO, "CH%01X RK%01X BL%01X\n", channel, rank, byte_lane);
}

/*
 * This function will program the RCVEN delays
 *
 * (currently doesn't comprehend rank)
 */
void set_rcvn(uint8_t channel, uint8_t rank,
	      uint8_t byte_lane, uint32_t pi_count)
{
	uint32_t reg;
	uint32_t msk;
	uint32_t temp;

	ENTERFN();

	DPF(D_TRN, "Rcvn ch%d rnk%d ln%d : pi=%03X\n",
	    channel, rank, byte_lane, pi_count);

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * BL0 -> B01PTRCTL0[11:08] (0x0-0xF)
	 * BL1 -> B01PTRCTL0[23:20] (0x0-0xF)
	 */
	reg = B01PTRCTL0 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	msk = (byte_lane & 1) ? 0xf00000 : 0xf00;
	temp = (byte_lane & 1) ? (pi_count / HALF_CLK) << 20 :
		(pi_count / HALF_CLK) << 8;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* Adjust PI_COUNT */
	pi_count -= ((pi_count / HALF_CLK) & 0xf) * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * BL0 -> B0DLLPICODER0[29:24] (0x00-0x3F)
	 * BL1 -> B1DLLPICODER0[29:24] (0x00-0x3F)
	 */
	reg = (byte_lane & 1) ? B1DLLPICODER0 : B0DLLPICODER0;
	reg += ((byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET);
	msk = 0x3f000000;
	temp = pi_count << 24;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/*
	 * DEADBAND
	 * BL0/1 -> B01DBCTL1[08/11] (+1 select)
	 * BL0/1 -> B01DBCTL1[02/05] (enable)
	 */
	reg = B01DBCTL1 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	msk = 0x00;
	temp = 0x00;

	/* enable */
	msk |= (byte_lane & 1) ? (1 << 5) : (1 << 2);
	if ((pi_count < EARLY_DB) || (pi_count > LATE_DB))
		temp |= msk;

	/* select */
	msk |= (byte_lane & 1) ? (1 << 11) : (1 << 8);
	if (pi_count < EARLY_DB)
		temp |= msk;

	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* error check */
	if (pi_count > 0x3f) {
		training_message(channel, rank, byte_lane);
		mrc_post_code(0xee, 0xe0);
	}

	LEAVEFN();
}

/*
 * This function will return the current RCVEN delay on the given
 * channel, rank, byte_lane as an absolute PI count.
 *
 * (currently doesn't comprehend rank)
 */
uint32_t get_rcvn(uint8_t channel, uint8_t rank, uint8_t byte_lane)
{
	uint32_t reg;
	uint32_t temp;
	uint32_t pi_count;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * BL0 -> B01PTRCTL0[11:08] (0x0-0xF)
	 * BL1 -> B01PTRCTL0[23:20] (0x0-0xF)
	 */
	reg = B01PTRCTL0 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= (byte_lane & 1) ? 20 : 8;
	temp &= 0xf;

	/* Adjust PI_COUNT */
	pi_count = temp * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * BL0 -> B0DLLPICODER0[29:24] (0x00-0x3F)
	 * BL1 -> B1DLLPICODER0[29:24] (0x00-0x3F)
	 */
	reg = (byte_lane & 1) ? B1DLLPICODER0 : B0DLLPICODER0;
	reg += ((byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET);
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= 24;
	temp &= 0x3f;

	/* Adjust PI_COUNT */
	pi_count += temp;

	LEAVEFN();

	return pi_count;
}

/*
 * This function will program the RDQS delays based on an absolute
 * amount of PIs.
 *
 * (currently doesn't comprehend rank)
 */
void set_rdqs(uint8_t channel, uint8_t rank,
	      uint8_t byte_lane, uint32_t pi_count)
{
	uint32_t reg;
	uint32_t msk;
	uint32_t temp;

	ENTERFN();
	DPF(D_TRN, "Rdqs ch%d rnk%d ln%d : pi=%03X\n",
	    channel, rank, byte_lane, pi_count);

	/*
	 * PI (1/128 MCLK)
	 * BL0 -> B0RXDQSPICODE[06:00] (0x00-0x47)
	 * BL1 -> B1RXDQSPICODE[06:00] (0x00-0x47)
	 */
	reg = (byte_lane & 1) ? B1RXDQSPICODE : B0RXDQSPICODE;
	reg += ((byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET);
	msk = 0x7f;
	temp = pi_count << 0;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* error check (shouldn't go above 0x3F) */
	if (pi_count > 0x47) {
		training_message(channel, rank, byte_lane);
		mrc_post_code(0xee, 0xe1);
	}

	LEAVEFN();
}

/*
 * This function will return the current RDQS delay on the given
 * channel, rank, byte_lane as an absolute PI count.
 *
 * (currently doesn't comprehend rank)
 */
uint32_t get_rdqs(uint8_t channel, uint8_t rank, uint8_t byte_lane)
{
	uint32_t reg;
	uint32_t temp;
	uint32_t pi_count;

	ENTERFN();

	/*
	 * PI (1/128 MCLK)
	 * BL0 -> B0RXDQSPICODE[06:00] (0x00-0x47)
	 * BL1 -> B1RXDQSPICODE[06:00] (0x00-0x47)
	 */
	reg = (byte_lane & 1) ? B1RXDQSPICODE : B0RXDQSPICODE;
	reg += ((byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET);
	temp = msg_port_alt_read(DDRPHY, reg);

	/* Adjust PI_COUNT */
	pi_count = temp & 0x7f;

	LEAVEFN();

	return pi_count;
}

/*
 * This function will program the WDQS delays based on an absolute
 * amount of PIs.
 *
 * (currently doesn't comprehend rank)
 */
void set_wdqs(uint8_t channel, uint8_t rank,
	      uint8_t byte_lane, uint32_t pi_count)
{
	uint32_t reg;
	uint32_t msk;
	uint32_t temp;

	ENTERFN();

	DPF(D_TRN, "Wdqs ch%d rnk%d ln%d : pi=%03X\n",
	    channel, rank, byte_lane, pi_count);

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * BL0 -> B01PTRCTL0[07:04] (0x0-0xF)
	 * BL1 -> B01PTRCTL0[19:16] (0x0-0xF)
	 */
	reg = B01PTRCTL0 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	msk = (byte_lane & 1) ? 0xf0000 : 0xf0;
	temp = pi_count / HALF_CLK;
	temp <<= (byte_lane & 1) ? 16 : 4;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* Adjust PI_COUNT */
	pi_count -= ((pi_count / HALF_CLK) & 0xf) * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * BL0 -> B0DLLPICODER0[21:16] (0x00-0x3F)
	 * BL1 -> B1DLLPICODER0[21:16] (0x00-0x3F)
	 */
	reg = (byte_lane & 1) ? B1DLLPICODER0 : B0DLLPICODER0;
	reg += ((byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET);
	msk = 0x3f0000;
	temp = pi_count << 16;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/*
	 * DEADBAND
	 * BL0/1 -> B01DBCTL1[07/10] (+1 select)
	 * BL0/1 -> B01DBCTL1[01/04] (enable)
	 */
	reg = B01DBCTL1 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	msk = 0x00;
	temp = 0x00;

	/* enable */
	msk |= (byte_lane & 1) ? (1 << 4) : (1 << 1);
	if ((pi_count < EARLY_DB) || (pi_count > LATE_DB))
		temp |= msk;

	/* select */
	msk |= (byte_lane & 1) ? (1 << 10) : (1 << 7);
	if (pi_count < EARLY_DB)
		temp |= msk;

	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* error check */
	if (pi_count > 0x3f) {
		training_message(channel, rank, byte_lane);
		mrc_post_code(0xee, 0xe2);
	}

	LEAVEFN();
}

/*
 * This function will return the amount of WDQS delay on the given
 * channel, rank, byte_lane as an absolute PI count.
 *
 * (currently doesn't comprehend rank)
 */
uint32_t get_wdqs(uint8_t channel, uint8_t rank, uint8_t byte_lane)
{
	uint32_t reg;
	uint32_t temp;
	uint32_t pi_count;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * BL0 -> B01PTRCTL0[07:04] (0x0-0xF)
	 * BL1 -> B01PTRCTL0[19:16] (0x0-0xF)
	 */
	reg = B01PTRCTL0 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= (byte_lane & 1) ? 16 : 4;
	temp &= 0xf;

	/* Adjust PI_COUNT */
	pi_count = (temp * HALF_CLK);

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * BL0 -> B0DLLPICODER0[21:16] (0x00-0x3F)
	 * BL1 -> B1DLLPICODER0[21:16] (0x00-0x3F)
	 */
	reg = (byte_lane & 1) ? B1DLLPICODER0 : B0DLLPICODER0;
	reg += ((byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET);
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= 16;
	temp &= 0x3f;

	/* Adjust PI_COUNT */
	pi_count += temp;

	LEAVEFN();

	return pi_count;
}

/*
 * This function will program the WDQ delays based on an absolute
 * number of PIs.
 *
 * (currently doesn't comprehend rank)
 */
void set_wdq(uint8_t channel, uint8_t rank,
	     uint8_t byte_lane, uint32_t pi_count)
{
	uint32_t reg;
	uint32_t msk;
	uint32_t temp;

	ENTERFN();

	DPF(D_TRN, "Wdq ch%d rnk%d ln%d : pi=%03X\n",
	    channel, rank, byte_lane, pi_count);

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * BL0 -> B01PTRCTL0[03:00] (0x0-0xF)
	 * BL1 -> B01PTRCTL0[15:12] (0x0-0xF)
	 */
	reg = B01PTRCTL0 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	msk = (byte_lane & 1) ? 0xf000 : 0xf;
	temp = pi_count / HALF_CLK;
	temp <<= (byte_lane & 1) ? 12 : 0;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* Adjust PI_COUNT */
	pi_count -= ((pi_count / HALF_CLK) & 0xf) * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * BL0 -> B0DLLPICODER0[13:08] (0x00-0x3F)
	 * BL1 -> B1DLLPICODER0[13:08] (0x00-0x3F)
	 */
	reg = (byte_lane & 1) ? B1DLLPICODER0 : B0DLLPICODER0;
	reg += ((byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET);
	msk = 0x3f00;
	temp = pi_count << 8;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/*
	 * DEADBAND
	 * BL0/1 -> B01DBCTL1[06/09] (+1 select)
	 * BL0/1 -> B01DBCTL1[00/03] (enable)
	 */
	reg = B01DBCTL1 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	msk = 0x00;
	temp = 0x00;

	/* enable */
	msk |= (byte_lane & 1) ? (1 << 3) : (1 << 0);
	if ((pi_count < EARLY_DB) || (pi_count > LATE_DB))
		temp |= msk;

	/* select */
	msk |= (byte_lane & 1) ? (1 << 9) : (1 << 6);
	if (pi_count < EARLY_DB)
		temp |= msk;

	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* error check */
	if (pi_count > 0x3f) {
		training_message(channel, rank, byte_lane);
		mrc_post_code(0xee, 0xe3);
	}

	LEAVEFN();
}

/*
 * This function will return the amount of WDQ delay on the given
 * channel, rank, byte_lane as an absolute PI count.
 *
 * (currently doesn't comprehend rank)
 */
uint32_t get_wdq(uint8_t channel, uint8_t rank, uint8_t byte_lane)
{
	uint32_t reg;
	uint32_t temp;
	uint32_t pi_count;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * BL0 -> B01PTRCTL0[03:00] (0x0-0xF)
	 * BL1 -> B01PTRCTL0[15:12] (0x0-0xF)
	 */
	reg = B01PTRCTL0 + (byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET;
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= (byte_lane & 1) ? 12 : 0;
	temp &= 0xf;

	/* Adjust PI_COUNT */
	pi_count = temp * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * BL0 -> B0DLLPICODER0[13:08] (0x00-0x3F)
	 * BL1 -> B1DLLPICODER0[13:08] (0x00-0x3F)
	 */
	reg = (byte_lane & 1) ? B1DLLPICODER0 : B0DLLPICODER0;
	reg += ((byte_lane >> 1) * DDRIODQ_BL_OFFSET +
		channel * DDRIODQ_CH_OFFSET);
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= 8;
	temp &= 0x3f;

	/* Adjust PI_COUNT */
	pi_count += temp;

	LEAVEFN();

	return pi_count;
}

/*
 * This function will program the WCMD delays based on an absolute
 * number of PIs.
 */
void set_wcmd(uint8_t channel, uint32_t pi_count)
{
	uint32_t reg;
	uint32_t msk;
	uint32_t temp;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * CMDPTRREG[11:08] (0x0-0xF)
	 */
	reg = CMDPTRREG + channel * DDRIOCCC_CH_OFFSET;
	msk = 0xf00;
	temp = pi_count / HALF_CLK;
	temp <<= 8;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* Adjust PI_COUNT */
	pi_count -= ((pi_count / HALF_CLK) & 0xf) * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * CMDDLLPICODER0[29:24] -> CMDSLICE R3 (unused)
	 * CMDDLLPICODER0[21:16] -> CMDSLICE L3 (unused)
	 * CMDDLLPICODER0[13:08] -> CMDSLICE R2 (unused)
	 * CMDDLLPICODER0[05:00] -> CMDSLICE L2 (unused)
	 * CMDDLLPICODER1[29:24] -> CMDSLICE R1 (unused)
	 * CMDDLLPICODER1[21:16] -> CMDSLICE L1 (0x00-0x3F)
	 * CMDDLLPICODER1[13:08] -> CMDSLICE R0 (unused)
	 * CMDDLLPICODER1[05:00] -> CMDSLICE L0 (unused)
	 */
	reg = CMDDLLPICODER1 + channel * DDRIOCCC_CH_OFFSET;
	msk = 0x3f3f3f3f;
	temp = (pi_count << 24) | (pi_count << 16) |
		(pi_count << 8) | (pi_count << 0);

	mrc_alt_write_mask(DDRPHY, reg, temp, msk);
	reg = CMDDLLPICODER0 + channel * DDRIOCCC_CH_OFFSET;	/* PO */
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/*
	 * DEADBAND
	 * CMDCFGREG0[17] (+1 select)
	 * CMDCFGREG0[16] (enable)
	 */
	reg = CMDCFGREG0 + channel * DDRIOCCC_CH_OFFSET;
	msk = 0x00;
	temp = 0x00;

	/* enable */
	msk |= (1 << 16);
	if ((pi_count < EARLY_DB) || (pi_count > LATE_DB))
		temp |= msk;

	/* select */
	msk |= (1 << 17);
	if (pi_count < EARLY_DB)
		temp |= msk;

	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* error check */
	if (pi_count > 0x3f)
		mrc_post_code(0xee, 0xe4);

	LEAVEFN();
}

/*
 * This function will return the amount of WCMD delay on the given
 * channel as an absolute PI count.
 */
uint32_t get_wcmd(uint8_t channel)
{
	uint32_t reg;
	uint32_t temp;
	uint32_t pi_count;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * CMDPTRREG[11:08] (0x0-0xF)
	 */
	reg = CMDPTRREG + channel * DDRIOCCC_CH_OFFSET;
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= 8;
	temp &= 0xf;

	/* Adjust PI_COUNT */
	pi_count = temp * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * CMDDLLPICODER0[29:24] -> CMDSLICE R3 (unused)
	 * CMDDLLPICODER0[21:16] -> CMDSLICE L3 (unused)
	 * CMDDLLPICODER0[13:08] -> CMDSLICE R2 (unused)
	 * CMDDLLPICODER0[05:00] -> CMDSLICE L2 (unused)
	 * CMDDLLPICODER1[29:24] -> CMDSLICE R1 (unused)
	 * CMDDLLPICODER1[21:16] -> CMDSLICE L1 (0x00-0x3F)
	 * CMDDLLPICODER1[13:08] -> CMDSLICE R0 (unused)
	 * CMDDLLPICODER1[05:00] -> CMDSLICE L0 (unused)
	 */
	reg = CMDDLLPICODER1 + channel * DDRIOCCC_CH_OFFSET;
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= 16;
	temp &= 0x3f;

	/* Adjust PI_COUNT */
	pi_count += temp;

	LEAVEFN();

	return pi_count;
}

/*
 * This function will program the WCLK delays based on an absolute
 * number of PIs.
 */
void set_wclk(uint8_t channel, uint8_t rank, uint32_t pi_count)
{
	uint32_t reg;
	uint32_t msk;
	uint32_t temp;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * CCPTRREG[15:12] -> CLK1 (0x0-0xF)
	 * CCPTRREG[11:08] -> CLK0 (0x0-0xF)
	 */
	reg = CCPTRREG + channel * DDRIOCCC_CH_OFFSET;
	msk = 0xff00;
	temp = ((pi_count / HALF_CLK) << 12) | ((pi_count / HALF_CLK) << 8);
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* Adjust PI_COUNT */
	pi_count -= ((pi_count / HALF_CLK) & 0xf) * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * ECCB1DLLPICODER0[13:08] -> CLK0 (0x00-0x3F)
	 * ECCB1DLLPICODER0[21:16] -> CLK1 (0x00-0x3F)
	 */
	reg = rank ? ECCB1DLLPICODER0 : ECCB1DLLPICODER0;
	reg += (channel * DDRIOCCC_CH_OFFSET);
	msk = 0x3f3f00;
	temp = (pi_count << 16) | (pi_count << 8);
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	reg = rank ? ECCB1DLLPICODER1 : ECCB1DLLPICODER1;
	reg += (channel * DDRIOCCC_CH_OFFSET);
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	reg = rank ? ECCB1DLLPICODER2 : ECCB1DLLPICODER2;
	reg += (channel * DDRIOCCC_CH_OFFSET);
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	reg = rank ? ECCB1DLLPICODER3 : ECCB1DLLPICODER3;
	reg += (channel * DDRIOCCC_CH_OFFSET);
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/*
	 * DEADBAND
	 * CCCFGREG1[11:08] (+1 select)
	 * CCCFGREG1[03:00] (enable)
	 */
	reg = CCCFGREG1 + channel * DDRIOCCC_CH_OFFSET;
	msk = 0x00;
	temp = 0x00;

	/* enable */
	msk |= 0xf;
	if ((pi_count < EARLY_DB) || (pi_count > LATE_DB))
		temp |= msk;

	/* select */
	msk |= 0xf00;
	if (pi_count < EARLY_DB)
		temp |= msk;

	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* error check */
	if (pi_count > 0x3f)
		mrc_post_code(0xee, 0xe5);

	LEAVEFN();
}

/*
 * This function will return the amout of WCLK delay on the given
 * channel, rank as an absolute PI count.
 */
uint32_t get_wclk(uint8_t channel, uint8_t rank)
{
	uint32_t reg;
	uint32_t temp;
	uint32_t pi_count;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * CCPTRREG[15:12] -> CLK1 (0x0-0xF)
	 * CCPTRREG[11:08] -> CLK0 (0x0-0xF)
	 */
	reg = CCPTRREG + channel * DDRIOCCC_CH_OFFSET;
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= rank ? 12 : 8;
	temp &= 0xf;

	/* Adjust PI_COUNT */
	pi_count = temp * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * ECCB1DLLPICODER0[13:08] -> CLK0 (0x00-0x3F)
	 * ECCB1DLLPICODER0[21:16] -> CLK1 (0x00-0x3F)
	 */
	reg = rank ? ECCB1DLLPICODER0 : ECCB1DLLPICODER0;
	reg += (channel * DDRIOCCC_CH_OFFSET);
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= rank ? 16 : 8;
	temp &= 0x3f;

	pi_count += temp;

	LEAVEFN();

	return pi_count;
}

/*
 * This function will program the WCTL delays based on an absolute
 * number of PIs.
 *
 * (currently doesn't comprehend rank)
 */
void set_wctl(uint8_t channel, uint8_t rank, uint32_t pi_count)
{
	uint32_t reg;
	uint32_t msk;
	uint32_t temp;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * CCPTRREG[31:28] (0x0-0xF)
	 * CCPTRREG[27:24] (0x0-0xF)
	 */
	reg = CCPTRREG + channel * DDRIOCCC_CH_OFFSET;
	msk = 0xff000000;
	temp = ((pi_count / HALF_CLK) << 28) | ((pi_count / HALF_CLK) << 24);
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* Adjust PI_COUNT */
	pi_count -= ((pi_count / HALF_CLK) & 0xf) * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * ECCB1DLLPICODER?[29:24] (0x00-0x3F)
	 * ECCB1DLLPICODER?[29:24] (0x00-0x3F)
	 */
	reg = ECCB1DLLPICODER0 + channel * DDRIOCCC_CH_OFFSET;
	msk = 0x3f000000;
	temp = (pi_count << 24);
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	reg = ECCB1DLLPICODER1 + channel * DDRIOCCC_CH_OFFSET;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	reg = ECCB1DLLPICODER2 + channel * DDRIOCCC_CH_OFFSET;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	reg = ECCB1DLLPICODER3 + channel * DDRIOCCC_CH_OFFSET;
	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/*
	 * DEADBAND
	 * CCCFGREG1[13:12] (+1 select)
	 * CCCFGREG1[05:04] (enable)
	 */
	reg = CCCFGREG1 + channel * DDRIOCCC_CH_OFFSET;
	msk = 0x00;
	temp = 0x00;

	/* enable */
	msk |= 0x30;
	if ((pi_count < EARLY_DB) || (pi_count > LATE_DB))
		temp |= msk;

	/* select */
	msk |= 0x3000;
	if (pi_count < EARLY_DB)
		temp |= msk;

	mrc_alt_write_mask(DDRPHY, reg, temp, msk);

	/* error check */
	if (pi_count > 0x3f)
		mrc_post_code(0xee, 0xe6);

	LEAVEFN();
}

/*
 * This function will return the amount of WCTL delay on the given
 * channel, rank as an absolute PI count.
 *
 * (currently doesn't comprehend rank)
 */
uint32_t get_wctl(uint8_t channel, uint8_t rank)
{
	uint32_t reg;
	uint32_t temp;
	uint32_t pi_count;

	ENTERFN();

	/*
	 * RDPTR (1/2 MCLK, 64 PIs)
	 * CCPTRREG[31:28] (0x0-0xF)
	 * CCPTRREG[27:24] (0x0-0xF)
	 */
	reg = CCPTRREG + channel * DDRIOCCC_CH_OFFSET;
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= 24;
	temp &= 0xf;

	/* Adjust PI_COUNT */
	pi_count = temp * HALF_CLK;

	/*
	 * PI (1/64 MCLK, 1 PIs)
	 * ECCB1DLLPICODER?[29:24] (0x00-0x3F)
	 * ECCB1DLLPICODER?[29:24] (0x00-0x3F)
	 */
	reg = ECCB1DLLPICODER0 + channel * DDRIOCCC_CH_OFFSET;
	temp = msg_port_alt_read(DDRPHY, reg);
	temp >>= 24;
	temp &= 0x3f;

	/* Adjust PI_COUNT */
	pi_count += temp;

	LEAVEFN();

	return pi_count;
}

/*
 * This function will program the internal Vref setting in a given
 * byte lane in a given channel.
 */
void set_vref(uint8_t channel, uint8_t byte_lane, uint32_t setting)
{
	uint32_t reg = (byte_lane & 0x1) ? B1VREFCTL : B0VREFCTL;

	ENTERFN();

	DPF(D_TRN, "Vref ch%d ln%d : val=%03X\n",
	    channel, byte_lane, setting);

	mrc_alt_write_mask(DDRPHY, reg + channel * DDRIODQ_CH_OFFSET +
		(byte_lane >> 1) * DDRIODQ_BL_OFFSET,
		vref_codes[setting] << 2, 0xfc);

	/*
	 * need to wait ~300ns for Vref to settle
	 * (check that this is necessary)
	 */
	delay_n(300);

	/* ??? may need to clear pointers ??? */

	LEAVEFN();
}

/*
 * This function will return the internal Vref setting for the given
 * channel, byte_lane.
 */
uint32_t get_vref(uint8_t channel, uint8_t byte_lane)
{
	uint8_t j;
	uint32_t ret_val = sizeof(vref_codes) / 2;
	uint32_t reg = (byte_lane & 0x1) ? B1VREFCTL : B0VREFCTL;
	uint32_t temp;

	ENTERFN();

	temp = msg_port_alt_read(DDRPHY, reg + channel * DDRIODQ_CH_OFFSET +
		(byte_lane >> 1) * DDRIODQ_BL_OFFSET);
	temp >>= 2;
	temp &= 0x3f;

	for (j = 0; j < sizeof(vref_codes); j++) {
		if (vref_codes[j] == temp) {
			ret_val = j;
			break;
		}
	}

	LEAVEFN();

	return ret_val;
}

/*
 * This function will return a 32-bit address in the desired
 * channel and rank.
 */
uint32_t get_addr(uint8_t channel, uint8_t rank)
{
	uint32_t offset = 32 * 1024 * 1024;	/* 32MB */

	/* Begin product specific code */
	if (channel > 0) {
		DPF(D_ERROR, "ILLEGAL CHANNEL\n");
		DEAD_LOOP();
	}

	if (rank > 1) {
		DPF(D_ERROR, "ILLEGAL RANK\n");
		DEAD_LOOP();
	}

	/* use 256MB lowest density as per DRP == 0x0003 */
	offset += rank * (256 * 1024 * 1024);

	return offset;
}

/*
 * This function will sample the DQTRAINSTS registers in the given
 * channel/rank SAMPLE_SIZE times looking for a valid '0' or '1'.
 *
 * It will return an encoded 32-bit date in which each bit corresponds to
 * the sampled value on the byte lane.
 */
uint32_t sample_dqs(struct mrc_params *mrc_params, uint8_t channel,
		    uint8_t rank, bool rcvn)
{
	uint8_t j;	/* just a counter */
	uint8_t bl;	/* which BL in the module (always 2 per module) */
	uint8_t bl_grp;	/* which BL module */
	/* byte lane divisor */
	uint8_t bl_divisor = (mrc_params->channel_width == X16) ? 2 : 1;
	uint32_t msk[2];	/* BLx in module */
	/* DQTRAINSTS register contents for each sample */
	uint32_t sampled_val[SAMPLE_SIZE];
	uint32_t num_0s;	/* tracks the number of '0' samples */
	uint32_t num_1s;	/* tracks the number of '1' samples */
	uint32_t ret_val = 0x00;	/* assume all '0' samples */
	uint32_t address = get_addr(channel, rank);

	/* initialise msk[] */
	msk[0] = rcvn ? (1 << 1) : (1 << 9);	/* BL0 */
	msk[1] = rcvn ? (1 << 0) : (1 << 8);	/* BL1 */

	/* cycle through each byte lane group */
	for (bl_grp = 0; bl_grp < (NUM_BYTE_LANES / bl_divisor) / 2; bl_grp++) {
		/* take SAMPLE_SIZE samples */
		for (j = 0; j < SAMPLE_SIZE; j++) {
			hte_mem_op(address, mrc_params->first_run,
				   rcvn ? 0 : 1);
			mrc_params->first_run = 0;

			/*
			 * record the contents of the proper
			 * DQTRAINSTS register
			 */
			sampled_val[j] = msg_port_alt_read(DDRPHY,
				DQTRAINSTS +
				bl_grp * DDRIODQ_BL_OFFSET +
				channel * DDRIODQ_CH_OFFSET);
		}

		/*
		 * look for a majority value (SAMPLE_SIZE / 2) + 1
		 * on the byte lane and set that value in the corresponding
		 * ret_val bit
		 */
		for (bl = 0; bl < 2; bl++) {
			num_0s = 0x00;	/* reset '0' tracker for byte lane */
			num_1s = 0x00;	/* reset '1' tracker for byte lane */
			for (j = 0; j < SAMPLE_SIZE; j++) {
				if (sampled_val[j] & msk[bl])
					num_1s++;
				else
					num_0s++;
			}
		if (num_1s > num_0s)
			ret_val |= (1 << (bl + bl_grp * 2));
		}
	}

	/*
	 * "ret_val.0" contains the status of BL0
	 * "ret_val.1" contains the status of BL1
	 * "ret_val.2" contains the status of BL2
	 * etc.
	 */
	return ret_val;
}

/* This function will find the rising edge transition on RCVN or WDQS */
void find_rising_edge(struct mrc_params *mrc_params, uint32_t delay[],
		      uint8_t channel, uint8_t rank, bool rcvn)
{
	bool all_edges_found;	/* determines stop condition */
	bool direction[NUM_BYTE_LANES];	/* direction indicator */
	uint8_t sample;	/* sample counter */
	uint8_t bl;	/* byte lane counter */
	/* byte lane divisor */
	uint8_t bl_divisor = (mrc_params->channel_width == X16) ? 2 : 1;
	uint32_t sample_result[SAMPLE_CNT];	/* results of sample_dqs() */
	uint32_t temp;
	uint32_t transition_pattern;

	ENTERFN();

	/* select hte and request initial configuration */
	select_hte();
	mrc_params->first_run = 1;

	/* Take 3 sample points (T1,T2,T3) to obtain a transition pattern */
	for (sample = 0; sample < SAMPLE_CNT; sample++) {
		/* program the desired delays for sample */
		for (bl = 0; bl < (NUM_BYTE_LANES / bl_divisor); bl++) {
			/* increase sample delay by 26 PI (0.2 CLK) */
			if (rcvn) {
				set_rcvn(channel, rank, bl,
					 delay[bl] + sample * SAMPLE_DLY);
			} else {
				set_wdqs(channel, rank, bl,
					 delay[bl] + sample * SAMPLE_DLY);
			}
		}

		/* take samples (Tsample_i) */
		sample_result[sample] = sample_dqs(mrc_params,
			channel, rank, rcvn);

		DPF(D_TRN,
		    "Find rising edge %s ch%d rnk%d: #%d dly=%d dqs=%02X\n",
		    rcvn ? "RCVN" : "WDQS", channel, rank, sample,
		    sample * SAMPLE_DLY, sample_result[sample]);
	}

	/*
	 * This pattern will help determine where we landed and ultimately
	 * how to place RCVEN/WDQS.
	 */
	for (bl = 0; bl < NUM_BYTE_LANES / bl_divisor; bl++) {
		/* build transition_pattern (MSB is 1st sample) */
		transition_pattern = 0;
		for (sample = 0; sample < SAMPLE_CNT; sample++) {
			transition_pattern |=
				((sample_result[sample] & (1 << bl)) >> bl) <<
				(SAMPLE_CNT - 1 - sample);
		}

		DPF(D_TRN, "=== transition pattern %d\n", transition_pattern);

		/*
		 * set up to look for rising edge based on
		 * transition_pattern
		 */
		switch (transition_pattern) {
		case 0:	/* sampled 0->0->0 */
			/* move forward from T3 looking for 0->1 */
			delay[bl] += 2 * SAMPLE_DLY;
			direction[bl] = FORWARD;
			break;
		case 1:	/* sampled 0->0->1 */
		case 5:	/* sampled 1->0->1 (bad duty cycle) *HSD#237503* */
			/* move forward from T2 looking for 0->1 */
			delay[bl] += 1 * SAMPLE_DLY;
			direction[bl] = FORWARD;
			break;
		case 2:	/* sampled 0->1->0 (bad duty cycle) *HSD#237503* */
		case 3:	/* sampled 0->1->1 */
			/* move forward from T1 looking for 0->1 */
			delay[bl] += 0 * SAMPLE_DLY;
			direction[bl] = FORWARD;
			break;
		case 4:	/* sampled 1->0->0 (assumes BL8, HSD#234975) */
			/* move forward from T3 looking for 0->1 */
			delay[bl] += 2 * SAMPLE_DLY;
			direction[bl] = FORWARD;
			break;
		case 6:	/* sampled 1->1->0 */
		case 7:	/* sampled 1->1->1 */
			/* move backward from T1 looking for 1->0 */
			delay[bl] += 0 * SAMPLE_DLY;
			direction[bl] = BACKWARD;
			break;
		default:
			mrc_post_code(0xee, 0xee);
			break;
		}

		/* program delays */
		if (rcvn)
			set_rcvn(channel, rank, bl, delay[bl]);
		else
			set_wdqs(channel, rank, bl, delay[bl]);
	}

	/*
	 * Based on the observed transition pattern on the byte lane,
	 * begin looking for a rising edge with single PI granularity.
	 */
	do {
		all_edges_found = true;	/* assume all byte lanes passed */
		/* take a sample */
		temp = sample_dqs(mrc_params, channel, rank, rcvn);
		/* check all each byte lane for proper edge */
		for (bl = 0; bl < NUM_BYTE_LANES / bl_divisor; bl++) {
			if (temp & (1 << bl)) {
				/* sampled "1" */
				if (direction[bl] == BACKWARD) {
					/*
					 * keep looking for edge
					 * on this byte lane
					 */
					all_edges_found = false;
					delay[bl] -= 1;
					if (rcvn) {
						set_rcvn(channel, rank,
							 bl, delay[bl]);
					} else {
						set_wdqs(channel, rank,
							 bl, delay[bl]);
					}
				}
			} else {
				/* sampled "0" */
				if (direction[bl] == FORWARD) {
					/*
					 * keep looking for edge
					 * on this byte lane
					 */
					all_edges_found = false;
					delay[bl] += 1;
					if (rcvn) {
						set_rcvn(channel, rank,
							 bl, delay[bl]);
					} else {
						set_wdqs(channel, rank,
							 bl, delay[bl]);
					}
				}
			}
		}
	} while (!all_edges_found);

	/* restore DDR idle state */
	dram_init_command(DCMD_PREA(rank));

	DPF(D_TRN, "Delay %03X %03X %03X %03X\n",
	    delay[0], delay[1], delay[2], delay[3]);

	LEAVEFN();
}

/*
 * This function will return a 32 bit mask that will be used to
 * check for byte lane failures.
 */
uint32_t byte_lane_mask(struct mrc_params *mrc_params)
{
	uint32_t j;
	uint32_t ret_val = 0x00;

	/*
	 * set ret_val based on NUM_BYTE_LANES such that you will check
	 * only BL0 in result
	 *
	 * (each bit in result represents a byte lane)
	 */
	for (j = 0; j < MAX_BYTE_LANES; j += NUM_BYTE_LANES)
		ret_val |= (1 << ((j / NUM_BYTE_LANES) * NUM_BYTE_LANES));

	/*
	 * HSD#235037
	 * need to adjust the mask for 16-bit mode
	 */
	if (mrc_params->channel_width == X16)
		ret_val |= (ret_val << 2);

	return ret_val;
}

/*
 * Check memory executing simple write/read/verify at the specified address.
 *
 * Bits in the result indicate failure on specific byte lane.
 */
uint32_t check_rw_coarse(struct mrc_params *mrc_params, uint32_t address)
{
	uint32_t result = 0;
	uint8_t first_run = 0;

	if (mrc_params->hte_setup) {
		mrc_params->hte_setup = 0;
		first_run = 1;
		select_hte();
	}

	result = hte_basic_write_read(mrc_params, address, first_run,
				      WRITE_TRAIN);

	DPF(D_TRN, "check_rw_coarse result is %x\n", result);

	return result;
}

/*
 * Check memory executing write/read/verify of many data patterns
 * at the specified address. Bits in the result indicate failure
 * on specific byte lane.
 */
uint32_t check_bls_ex(struct mrc_params *mrc_params, uint32_t address)
{
	uint32_t result;
	uint8_t first_run = 0;

	if (mrc_params->hte_setup) {
		mrc_params->hte_setup = 0;
		first_run = 1;
		select_hte();
	}

	result = hte_write_stress_bit_lanes(mrc_params, address, first_run);

	DPF(D_TRN, "check_bls_ex result is %x\n", result);

	return result;
}

/*
 * 32-bit LFSR with characteristic polynomial: X^32 + X^22 +X^2 + X^1
 *
 * The function takes pointer to previous 32 bit value and
 * modifies it to next value.
 */
void lfsr32(uint32_t *lfsr_ptr)
{
	uint32_t bit;
	uint32_t lfsr;
	int i;

	lfsr = *lfsr_ptr;

	for (i = 0; i < 32; i++) {
		bit = 1 ^ (lfsr & 1);
		bit = bit ^ ((lfsr & 2) >> 1);
		bit = bit ^ ((lfsr & 4) >> 2);
		bit = bit ^ ((lfsr & 0x400000) >> 22);

		lfsr = ((lfsr >> 1) | (bit << 31));
	}

	*lfsr_ptr = lfsr;
}

/* Clear the pointers in a given byte lane in a given channel */
void clear_pointers(void)
{
	uint8_t channel;
	uint8_t bl;

	ENTERFN();

	for (channel = 0; channel < NUM_CHANNELS; channel++) {
		for (bl = 0; bl < NUM_BYTE_LANES; bl++) {
			mrc_alt_write_mask(DDRPHY,
					   B01PTRCTL1 +
					   channel * DDRIODQ_CH_OFFSET +
					   (bl >> 1) * DDRIODQ_BL_OFFSET,
					   ~(1 << 8), (1 << 8));

			mrc_alt_write_mask(DDRPHY,
					   B01PTRCTL1 +
					   channel * DDRIODQ_CH_OFFSET +
					   (bl >> 1) * DDRIODQ_BL_OFFSET,
					   (1 << 8), (1 << 8));
		}
	}

	LEAVEFN();
}

static void print_timings_internal(uint8_t algo, uint8_t channel, uint8_t rank,
				   uint8_t bl_divisor)
{
	uint8_t bl;

	switch (algo) {
	case RCVN:
		DPF(D_INFO, "\nRCVN[%02d:%02d]", channel, rank);
		break;
	case WDQS:
		DPF(D_INFO, "\nWDQS[%02d:%02d]", channel, rank);
		break;
	case WDQX:
		DPF(D_INFO, "\nWDQx[%02d:%02d]", channel, rank);
		break;
	case RDQS:
		DPF(D_INFO, "\nRDQS[%02d:%02d]", channel, rank);
		break;
	case VREF:
		DPF(D_INFO, "\nVREF[%02d:%02d]", channel, rank);
		break;
	case WCMD:
		DPF(D_INFO, "\nWCMD[%02d:%02d]", channel, rank);
		break;
	case WCTL:
		DPF(D_INFO, "\nWCTL[%02d:%02d]", channel, rank);
		break;
	case WCLK:
		DPF(D_INFO, "\nWCLK[%02d:%02d]", channel, rank);
		break;
	default:
		break;
	}

	for (bl = 0; bl < NUM_BYTE_LANES / bl_divisor; bl++) {
		switch (algo) {
		case RCVN:
			DPF(D_INFO, " %03d", get_rcvn(channel, rank, bl));
			break;
		case WDQS:
			DPF(D_INFO, " %03d", get_wdqs(channel, rank, bl));
			break;
		case WDQX:
			DPF(D_INFO, " %03d", get_wdq(channel, rank, bl));
			break;
		case RDQS:
			DPF(D_INFO, " %03d", get_rdqs(channel, rank, bl));
			break;
		case VREF:
			DPF(D_INFO, " %03d", get_vref(channel, bl));
			break;
		case WCMD:
			DPF(D_INFO, " %03d", get_wcmd(channel));
			break;
		case WCTL:
			DPF(D_INFO, " %03d", get_wctl(channel, rank));
			break;
		case WCLK:
			DPF(D_INFO, " %03d", get_wclk(channel, rank));
			break;
		default:
			break;
		}
	}
}

void print_timings(struct mrc_params *mrc_params)
{
	uint8_t algo;
	uint8_t channel;
	uint8_t rank;
	uint8_t bl_divisor = (mrc_params->channel_width == X16) ? 2 : 1;

	DPF(D_INFO, "\n---------------------------");
	DPF(D_INFO, "\nALGO[CH:RK] BL0 BL1 BL2 BL3");
	DPF(D_INFO, "\n===========================");

	for (algo = 0; algo < MAX_ALGOS; algo++) {
		for (channel = 0; channel < NUM_CHANNELS; channel++) {
			if (mrc_params->channel_enables & (1 << channel)) {
				for (rank = 0; rank < NUM_RANKS; rank++) {
					if (mrc_params->rank_enables &
						(1 << rank)) {
						print_timings_internal(algo,
							channel, rank,
							bl_divisor);
					}
				}
			}
		}
	}

	DPF(D_INFO, "\n---------------------------");
	DPF(D_INFO, "\n");
}