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
|
/*
* dp_lt.c: DP Link Training functions.
*
* Copyright (c) 2015-2017, NVIDIA CORPORATION, All rights reserved.
* Author: Animesh Kishore <ankishore@nvidia.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/mutex.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/completion.h>
#include <linux/kernel.h>
#include "dc.h"
#include <linux/moduleparam.h>
#include "dp_lt.h"
#include "dpaux.h"
#include "dp.h"
#include "sor.h"
#include "sor_regs.h"
/*
* By default we do not trigger LT if link is already stable.
* However, analyzer tests in their latest firmware are explicitly
* checking for LT. Add param to suppress this optimization. It
* has no usecase other than analyzer specific requirement.
*/
static bool force_trigger_lt;
module_param(force_trigger_lt, bool, 0644);
MODULE_PARM_DESC(force_trigger_lt,
"Retrigger LT even if link is already stable");
static void set_lt_state(struct tegra_dp_lt_data *lt_data,
int target_state, int delay_ms);
static void set_lt_tpg(struct tegra_dp_lt_data *lt_data, u32 tp);
static struct {
unsigned int key; /* Index into the link speed table */
unsigned int num_lanes;
} const tegra_dp_link_config_priority[] = {/* CTS approved list. Do not alter */
{.key = TEGRA_DC_SOR_LINK_SPEED_G5_4, .num_lanes = 4}, /* 21.6Gbps */
{.key = TEGRA_DC_SOR_LINK_SPEED_G2_7, .num_lanes = 4}, /* 10.8Gbps */
{.key = TEGRA_DC_SOR_LINK_SPEED_G1_62, .num_lanes = 4}, /* 6.48Gbps */
{.key = TEGRA_DC_SOR_LINK_SPEED_G5_4, .num_lanes = 2}, /* 10.8Gbps */
{.key = TEGRA_DC_SOR_LINK_SPEED_G2_7, .num_lanes = 2}, /* 5.4Gbps */
{.key = TEGRA_DC_SOR_LINK_SPEED_G1_62, .num_lanes = 2}, /* 3.24Gbps */
{.key = TEGRA_DC_SOR_LINK_SPEED_G5_4, .num_lanes = 1}, /* 5.4Gbps */
{.key = TEGRA_DC_SOR_LINK_SPEED_G2_7, .num_lanes = 1}, /* 2.7Gbps */
{.key = TEGRA_DC_SOR_LINK_SPEED_G1_62, .num_lanes = 1}, /* 1.62Gbps */
};
/* Check if post-cursor2 programming is supported */
static inline bool is_pc2_supported(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_link_config *cfg = <_data->dp->link_cfg;
struct tegra_dp_out *dp_out = lt_data->dp->dc->out->dp_out;
return (!dp_out->pc2_disabled &&
cfg->tps == TEGRA_DC_DP_TRAINING_PATTERN_3);
}
/*
* Wait period before reading link status.
* If dpcd addr 0xe TRAINING_AUX_RD_INTERVAL absent or zero,
* wait for 100us for CR status and 400us for CE status.
* Otherwise, use values as specified.
*/
static inline u32 wait_aux_training(struct tegra_dp_lt_data *lt_data,
bool is_clk_recovery)
{
if (!lt_data->aux_rd_interval)
is_clk_recovery ? usleep_range(150, 200) :
usleep_range(450, 500);
else
msleep(lt_data->aux_rd_interval * 4);
return lt_data->aux_rd_interval;
}
static int get_next_lower_link_config(struct tegra_dc_dp_data *dp,
struct tegra_dc_dp_link_config *link_cfg)
{
u8 cur_lanes = link_cfg->lane_count;
u8 cur_link_bw = link_cfg->link_bw;
u32 idx;
u32 size = ARRAY_SIZE(tegra_dp_link_config_priority);
unsigned int key; /* Index into the link speed table */
u8 link_rate, num_lanes; /* Per loop variables */
for (idx = 0; idx < size; idx++) {
key = tegra_dp_link_config_priority[idx].key;
link_rate = dp->sor->link_speeds[key].link_rate;
num_lanes = tegra_dp_link_config_priority[idx].num_lanes;
if (link_rate == cur_link_bw && num_lanes == cur_lanes)
break;
}
/* already at lowest link config */
if (idx == size - 1)
return -ENOENT;
for (idx++; idx < size; idx++) {
key = tegra_dp_link_config_priority[idx].key;
link_rate = dp->sor->link_speeds[key].link_rate;
num_lanes = tegra_dp_link_config_priority[idx].num_lanes;
if (link_rate <= link_cfg->max_link_bw &&
num_lanes <= link_cfg->max_lane_count)
return idx;
}
/* we should never end up here */
return -ENOENT;
}
static bool get_clock_recovery_status(struct tegra_dp_lt_data *lt_data)
{
u32 cnt;
u32 n_lanes = lt_data->n_lanes;
u8 data_ptr = 0;
/* support for 1 lane */
u32 loopcnt = (n_lanes == 1) ? 1 : n_lanes >> 1;
for (cnt = 0; cnt < loopcnt; cnt++) {
tegra_dc_dp_dpcd_read(lt_data->dp,
(NV_DPCD_LANE0_1_STATUS + cnt), &data_ptr);
if (n_lanes == 1)
return (data_ptr & 0x1) ? true : false;
else if (!(data_ptr & 0x1) ||
!(data_ptr &
(0x1 << NV_DPCD_STATUS_LANEXPLUS1_CR_DONE_SHIFT)))
return false;
}
return true;
}
static bool get_channel_eq_status(struct tegra_dp_lt_data *lt_data)
{
u32 cnt;
u32 n_lanes = lt_data->n_lanes;
u8 data_ptr = 0;
bool ce_done = true;
/* support for 1 lane */
u32 loopcnt = (n_lanes == 1) ? 1 : n_lanes >> 1;
for (cnt = 0; cnt < loopcnt; cnt++) {
tegra_dc_dp_dpcd_read(lt_data->dp,
(NV_DPCD_LANE0_1_STATUS + cnt), &data_ptr);
if (n_lanes == 1) {
ce_done = (data_ptr &
(0x1 << NV_DPCD_STATUS_LANEX_CHN_EQ_DONE_SHIFT)) &&
(data_ptr &
(0x1 << NV_DPCD_STATUS_LANEX_SYMBOL_LOCKED_SHFIT));
break;
} else if (!(data_ptr &
(0x1 << NV_DPCD_STATUS_LANEX_CHN_EQ_DONE_SHIFT)) ||
!(data_ptr &
(0x1 << NV_DPCD_STATUS_LANEX_SYMBOL_LOCKED_SHFIT)) ||
!(data_ptr &
(0x1 << NV_DPCD_STATUS_LANEXPLUS1_CHN_EQ_DONE_SHIFT)) ||
!(data_ptr &
(0x1 << NV_DPCD_STATUS_LANEXPLUS1_SYMBOL_LOCKED_SHIFT))) {
ce_done = false;
break;
}
}
if (ce_done) {
tegra_dc_dp_dpcd_read(lt_data->dp,
NV_DPCD_LANE_ALIGN_STATUS_UPDATED, &data_ptr);
if (!(data_ptr &
NV_DPCD_LANE_ALIGN_STATUS_INTERLANE_ALIGN_DONE_YES))
ce_done = false;
}
return ce_done;
}
static bool get_lt_status(struct tegra_dp_lt_data *lt_data)
{
bool cr_done, ce_done;
cr_done = get_clock_recovery_status(lt_data);
if (!cr_done)
return false;
ce_done = get_channel_eq_status(lt_data);
return ce_done;
}
/*
* get updated voltage swing, pre-emphasis and
* post-cursor2 settings from panel
*/
static void get_lt_new_config(struct tegra_dp_lt_data *lt_data)
{
u32 cnt;
u8 data_ptr;
u32 n_lanes = lt_data->n_lanes;
u32 *vs = lt_data->drive_current;
u32 *pe = lt_data->pre_emphasis;
u32 *pc = lt_data->post_cursor2;
bool pc2_supported = is_pc2_supported(lt_data);
/* support for 1 lane */
u32 loopcnt = (n_lanes == 1) ? 1 : n_lanes >> 1;
for (cnt = 0; cnt < loopcnt; cnt++) {
tegra_dc_dp_dpcd_read(lt_data->dp,
(NV_DPCD_LANE0_1_ADJUST_REQ + cnt), &data_ptr);
pe[2 * cnt] = (data_ptr & NV_DPCD_ADJUST_REQ_LANEX_PE_MASK) >>
NV_DPCD_ADJUST_REQ_LANEX_PE_SHIFT;
vs[2 * cnt] = (data_ptr & NV_DPCD_ADJUST_REQ_LANEX_DC_MASK) >>
NV_DPCD_ADJUST_REQ_LANEX_DC_SHIFT;
pe[1 + 2 * cnt] =
(data_ptr & NV_DPCD_ADJUST_REQ_LANEXPLUS1_PE_MASK) >>
NV_DPCD_ADJUST_REQ_LANEXPLUS1_PE_SHIFT;
vs[1 + 2 * cnt] =
(data_ptr & NV_DPCD_ADJUST_REQ_LANEXPLUS1_DC_MASK) >>
NV_DPCD_ADJUST_REQ_LANEXPLUS1_DC_SHIFT;
}
if (pc2_supported) {
tegra_dc_dp_dpcd_read(lt_data->dp,
NV_DPCD_ADJUST_REQ_POST_CURSOR2, &data_ptr);
for (cnt = 0; cnt < n_lanes; cnt++) {
pc[cnt] = (data_ptr >>
NV_DPCD_ADJUST_REQ_POST_CURSOR2_LANE_SHIFT(cnt)) &
NV_DPCD_ADJUST_REQ_POST_CURSOR2_LANE_MASK;
}
}
for (cnt = 0; cnt < n_lanes; cnt++)
pr_info("dp lt: new config: lane %d: "
"vs level: %d, pe level: %d, pc2 level: %d\n",
cnt, vs[cnt], pe[cnt], pc2_supported ? pc[cnt] : 0);
}
static void set_tx_pu(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp = lt_data->dp;
struct tegra_dc_sor_data *sor = dp->sor;
u32 n_lanes = lt_data->n_lanes;
int cnt = 1;
u32 *vs = lt_data->drive_current;
u32 *pe = lt_data->pre_emphasis;
u32 *pc = lt_data->post_cursor2;
u32 max_tx_pu;
u32 nv_sor_dp_padctl_reg = nv_sor_dp_padctl(sor->portnum);
if (!dp->pdata || (dp->pdata && dp->pdata->tx_pu_disable)) {
tegra_sor_write_field(dp->sor,
nv_sor_dp_padctl_reg,
NV_SOR_DP_PADCTL_TX_PU_ENABLE,
NV_SOR_DP_PADCTL_TX_PU_DISABLE);
lt_data->tx_pu = 0;
return;
}
max_tx_pu = dp->pdata->lt_data[DP_TX_PU].data[pc[0]][vs[0]][pe[0]];
for (; cnt < n_lanes; cnt++) {
max_tx_pu = (max_tx_pu <
dp->pdata->lt_data[DP_TX_PU].data[pc[cnt]][vs[cnt]][pe[cnt]]) ?
dp->pdata->lt_data[DP_TX_PU].data[pc[cnt]][vs[cnt]][pe[cnt]] :
max_tx_pu;
}
lt_data->tx_pu = max_tx_pu;
tegra_sor_write_field(sor, nv_sor_dp_padctl_reg,
NV_SOR_DP_PADCTL_TX_PU_VALUE_DEFAULT_MASK,
(max_tx_pu <<
NV_SOR_DP_PADCTL_TX_PU_VALUE_SHIFT |
NV_SOR_DP_PADCTL_TX_PU_ENABLE));
pr_info("dp lt: tx_pu: 0x%x\n", max_tx_pu);
}
/*
* configure voltage swing, pre-emphasis,
* post-cursor2 and tx_pu on host and sink
*/
static void set_lt_config(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp = lt_data->dp;
struct tegra_dc_sor_data *sor = dp->sor;
u32 n_lanes = lt_data->n_lanes;
bool pc2_supported = is_pc2_supported(lt_data);
int i, cnt;
u32 val;
u32 *vs = lt_data->drive_current;
u32 *pe = lt_data->pre_emphasis;
u32 *pc = lt_data->post_cursor2;
u32 aux_stat = 0;
u8 training_lanex_set[4] = {0, 0, 0, 0};
u32 training_lanex_set_size = sizeof(training_lanex_set);
/* support for 1 lane */
u32 loopcnt = (n_lanes == 1) ? 1 : n_lanes >> 1;
/*
* apply voltage swing, preemphasis, postcursor2 and tx_pu
* prod settings to each lane based on levels
*/
for (i = 0; i < n_lanes; i++) {
u32 mask = 0;
u32 pe_reg, vs_reg, pc_reg;
u32 shift = 0;
cnt = sor->xbar_ctrl[i];
switch (cnt) {
case 0:
mask = NV_SOR_PR_LANE2_DP_LANE0_MASK;
shift = NV_SOR_PR_LANE2_DP_LANE0_SHIFT;
break;
case 1:
mask = NV_SOR_PR_LANE1_DP_LANE1_MASK;
shift = NV_SOR_PR_LANE1_DP_LANE1_SHIFT;
break;
case 2:
mask = NV_SOR_PR_LANE0_DP_LANE2_MASK;
shift = NV_SOR_PR_LANE0_DP_LANE2_SHIFT;
break;
case 3:
mask = NV_SOR_PR_LANE3_DP_LANE3_MASK;
shift = NV_SOR_PR_LANE3_DP_LANE3_SHIFT;
break;
default:
dev_err(&dp->dc->ndev->dev,
"dp: incorrect lane cnt\n");
}
pe_reg = dp->pdata->lt_data[DP_PE].data[pc[i]][vs[i]][pe[i]];
vs_reg = dp->pdata->lt_data[DP_VS].data[pc[i]][vs[i]][pe[i]];
pc_reg = dp->pdata->lt_data[DP_PC].data[pc[i]][vs[i]][pe[i]];
tegra_sor_write_field(sor, NV_SOR_PR(sor->portnum),
mask, (pe_reg << shift));
tegra_sor_write_field(sor, NV_SOR_DC(sor->portnum),
mask, (vs_reg << shift));
if (pc2_supported) {
tegra_sor_write_field(
sor, NV_SOR_POSTCURSOR(sor->portnum),
mask, (pc_reg << shift));
}
pr_info("dp lt: config: lane %d: "
"vs level: %d, pe level: %d, pc2 level: %d\n",
i, vs[i], pe[i], pc2_supported ? pc[i] : 0);
}
set_tx_pu(lt_data);
usleep_range(15, 20); /* HW stabilization delay */
/* apply voltage swing and preemphasis levels to panel for each lane */
for (cnt = n_lanes - 1; cnt >= 0; cnt--) {
u32 max_vs_flag = tegra_dp_is_max_vs(pe[cnt], vs[cnt]);
u32 max_pe_flag = tegra_dp_is_max_pe(pe[cnt], vs[cnt]);
val = (vs[cnt] << NV_DPCD_TRAINING_LANEX_SET_DC_SHIFT) |
(max_vs_flag ?
NV_DPCD_TRAINING_LANEX_SET_DC_MAX_REACHED_T :
NV_DPCD_TRAINING_LANEX_SET_DC_MAX_REACHED_F) |
(pe[cnt] << NV_DPCD_TRAINING_LANEX_SET_PE_SHIFT) |
(max_pe_flag ?
NV_DPCD_TRAINING_LANEX_SET_PE_MAX_REACHED_T :
NV_DPCD_TRAINING_LANEX_SET_PE_MAX_REACHED_F);
training_lanex_set[cnt] = val;
}
tegra_dc_dpaux_write(dp->dpaux, DPAUX_DP_AUXCTL_CMD_AUXWR,
NV_DPCD_TRAINING_LANE0_SET, training_lanex_set,
&training_lanex_set_size, &aux_stat);
/* apply postcursor2 levels to panel for each lane */
if (pc2_supported) {
for (cnt = 0; cnt < loopcnt; cnt++) {
u32 max_pc_flag0 = tegra_dp_is_max_pc(pc[cnt]);
u32 max_pc_flag1 = tegra_dp_is_max_pc(pc[cnt + 1]);
val = (pc[cnt] << NV_DPCD_LANEX_SET2_PC2_SHIFT) |
(max_pc_flag0 ?
NV_DPCD_LANEX_SET2_PC2_MAX_REACHED_T :
NV_DPCD_LANEX_SET2_PC2_MAX_REACHED_F) |
(pc[cnt + 1] <<
NV_DPCD_LANEXPLUS1_SET2_PC2_SHIFT) |
(max_pc_flag1 ?
NV_DPCD_LANEXPLUS1_SET2_PC2_MAX_REACHED_T :
NV_DPCD_LANEXPLUS1_SET2_PC2_MAX_REACHED_F);
tegra_dc_dp_dpcd_write(dp,
(NV_DPCD_TRAINING_LANE0_1_SET2 + cnt), val);
}
}
}
static int do_fast_lt_no_handshake(struct tegra_dp_lt_data *lt_data)
{
BUG_ON(!lt_data->lt_config_valid);
/* transmit link training pattern 1 for min of 500us */
set_lt_tpg(lt_data, TEGRA_DC_DP_TRAINING_PATTERN_1);
usleep_range(500, 600);
/* transmit channel equalization training pattern for min of 500us */
set_lt_tpg(lt_data, lt_data->dp->link_cfg.tps);
usleep_range(500, 600);
return 0;
}
static void lt_data_sw_reset(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp = lt_data->dp;
BUG_ON(!dp);
lt_data->lt_config_valid = false;
lt_data->cr_retry = 0;
lt_data->ce_retry = 0;
lt_data->tx_pu = 0;
lt_data->n_lanes = dp->link_cfg.lane_count;
lt_data->link_bw = dp->link_cfg.link_bw;
lt_data->no_aux_handshake = dp->link_cfg.support_fast_lt;
lt_data->aux_rd_interval = dp->link_cfg.aux_rd_interval;
memset(lt_data->pre_emphasis, PRE_EMPHASIS_L0,
sizeof(lt_data->pre_emphasis));
memset(lt_data->drive_current, DRIVE_CURRENT_L0,
sizeof(lt_data->drive_current));
memset(lt_data->post_cursor2, POST_CURSOR2_L0,
sizeof(lt_data->post_cursor2));
}
static void lt_data_reset(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp = lt_data->dp;
BUG_ON(!dp);
lt_data_sw_reset(lt_data);
/* reset LT data on controller and panel only if hpd is asserted */
if (tegra_dc_hpd(dp->dc)) {
/*
* Training pattern is disabled here. Do not HW reset
* lt config i.e. vs, pe, pc2. CTS mandates modifying these
* only when training pattern is enabled.
*/
tegra_dp_update_link_config(dp);
}
}
static void set_lt_tpg(struct tegra_dp_lt_data *lt_data, u32 tp)
{
bool cur_hpd = tegra_dc_hpd(lt_data->dp->dc);
struct tegra_dc_dp_data *dp = lt_data->dp;
if (lt_data->tps == tp)
return;
if (cur_hpd)
tegra_dp_tpg(dp, tp, lt_data->n_lanes);
else
/*
* hpd deasserted. Just set training
* sequence from host side and exit
*/
tegra_sor_tpg(dp->sor, tp, lt_data->n_lanes);
lt_data->tps = tp;
}
static void lt_failed(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp = lt_data->dp;
mutex_lock(<_data->lock);
tegra_dc_sor_detach(dp->sor);
set_lt_tpg(lt_data, TEGRA_DC_DP_TRAINING_PATTERN_DISABLE);
lt_data_reset(lt_data);
mutex_unlock(<_data->lock);
}
static void lt_passed(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp = lt_data->dp;
mutex_lock(<_data->lock);
lt_data->lt_config_valid = true;
set_lt_tpg(lt_data, TEGRA_DC_DP_TRAINING_PATTERN_DISABLE);
tegra_dc_sor_attach(dp->sor);
mutex_unlock(<_data->lock);
}
static void lt_reset_state(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp;
struct tegra_dc_sor_data *sor;
bool cur_hpd;
int tgt_state;
int timeout;
BUG_ON(!lt_data || !lt_data->dp || !lt_data->dp->sor);
dp = lt_data->dp;
sor = dp->sor;
cur_hpd = tegra_dc_hpd(dp->dc);
if (lt_data->force_disable) {
pr_info("dp lt: link training force disable\n");
lt_data->force_disable = false;
lt_data->force_trigger = false;
if (!lt_data->no_aux_handshake)
lt_data->lt_config_valid = false;
tgt_state = STATE_DONE_FAIL;
timeout = -1;
goto done;
} else if (!cur_hpd || !dp->link_cfg.is_valid) {
pr_info("dp lt: cur_hpd: %d, link cfg valid: %d\n",
!!cur_hpd, !!dp->link_cfg.is_valid);
lt_failed(lt_data);
lt_data->force_disable = false;
lt_data->force_trigger = false;
tgt_state = STATE_DONE_FAIL;
timeout = -1;
goto done;
}
if (!force_trigger_lt && !lt_data->force_trigger &&
lt_data->lt_config_valid &&
get_lt_status(lt_data)) {
pr_info("dp_lt: link stable, do nothing\n");
lt_passed(lt_data);
tgt_state = STATE_DONE_PASS;
timeout = -1;
goto done;
}
lt_data->force_trigger = false;
/*
* detach SOR early.
* DP lane count can be changed only
* when SOR is asleep.
* DP link bandwidth can be changed only
* when SOR is in safe mode.
*/
mutex_lock(<_data->lock);
tegra_dc_sor_detach(sor);
mutex_unlock(<_data->lock);
lt_data_reset(lt_data);
tgt_state = STATE_CLOCK_RECOVERY;
timeout = 0;
WARN_ON(lt_data->tps != TEGRA_DC_DP_TRAINING_PATTERN_DISABLE);
/*
* pre-charge main link for at
* least 10us before initiating
* link training
*/
mutex_lock(<_data->lock);
tegra_sor_precharge_lanes(sor);
mutex_unlock(<_data->lock);
done:
set_lt_state(lt_data, tgt_state, timeout);
}
static void fast_lt_state(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp;
struct tegra_dc_sor_data *sor;
int tgt_state;
int timeout;
bool cur_hpd;
bool lt_status;
BUG_ON(!lt_data || !lt_data->dp || !lt_data->dp->sor);
dp = lt_data->dp;
sor = dp->sor;
BUG_ON(!lt_data->no_aux_handshake);
cur_hpd = tegra_dc_hpd(dp->dc);
if (!cur_hpd) {
pr_info("lt: hpd deasserted, wait for sometime, then reset\n");
lt_failed(lt_data);
tgt_state = STATE_RESET;
timeout = HPD_DROP_TIMEOUT_MS;
goto done;
}
WARN_ON(lt_data->tps != TEGRA_DC_DP_TRAINING_PATTERN_DISABLE);
mutex_lock(<_data->lock);
tegra_dc_sor_detach(sor);
set_lt_config(lt_data);
tegra_sor_precharge_lanes(sor);
mutex_unlock(<_data->lock);
do_fast_lt_no_handshake(lt_data);
lt_status = get_lt_status(lt_data);
if (lt_status) {
lt_passed(lt_data);
tgt_state = STATE_DONE_PASS;
timeout = -1;
} else {
lt_failed(lt_data);
tgt_state = STATE_RESET;
timeout = 0;
}
pr_info("dp lt: fast link training %s\n",
tgt_state == STATE_DONE_PASS ? "pass" : "fail");
done:
set_lt_state(lt_data, tgt_state, timeout);
}
static void lt_reduce_bit_rate_state(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp = lt_data->dp;
struct tegra_dc_dp_link_config tmp_cfg;
int next_link_index;
bool cur_hpd;
unsigned int key; /* Index into the link speed table */
cur_hpd = tegra_dc_hpd(dp->dc);
if (!cur_hpd) {
pr_info("lt: hpd deasserted, wait for sometime, then reset\n");
lt_failed(lt_data);
set_lt_state(lt_data, STATE_RESET, HPD_DROP_TIMEOUT_MS);
return;
}
dp->link_cfg.is_valid = false;
tmp_cfg = dp->link_cfg;
next_link_index = get_next_lower_link_config(dp, &tmp_cfg);
if (next_link_index < 0)
goto fail;
key = tegra_dp_link_config_priority[next_link_index].key;
tmp_cfg.link_bw = dp->sor->link_speeds[key].link_rate;
tmp_cfg.lane_count =
tegra_dp_link_config_priority[next_link_index].num_lanes;
if (!tegra_dc_dp_calc_config(dp, dp->mode, &tmp_cfg))
goto fail;
tmp_cfg.is_valid = true;
dp->link_cfg = tmp_cfg;
tegra_dp_update_link_config(dp);
lt_data->n_lanes = tmp_cfg.lane_count;
lt_data->link_bw = tmp_cfg.link_bw;
pr_info("dp lt: retry CR, lanes: %d, link_bw: 0x%x\n",
lt_data->n_lanes, lt_data->link_bw);
set_lt_state(lt_data, STATE_CLOCK_RECOVERY, 0);
return;
fail:
pr_info("dp lt: bit rate already lowest\n");
lt_failed(lt_data);
set_lt_state(lt_data, STATE_DONE_FAIL, -1);
return;
}
static void lt_channel_equalization_state(struct tegra_dp_lt_data *lt_data)
{
int tgt_state;
int timeout;
bool cr_done = true;
bool ce_done = true;
bool cur_hpd;
cur_hpd = tegra_dc_hpd(lt_data->dp->dc);
if (!cur_hpd) {
pr_info("lt: hpd deasserted, wait for sometime, then reset\n");
lt_failed(lt_data);
tgt_state = STATE_RESET;
timeout = HPD_DROP_TIMEOUT_MS;
goto done;
}
set_lt_tpg(lt_data, lt_data->dp->link_cfg.tps);
wait_aux_training(lt_data, false);
cr_done = get_clock_recovery_status(lt_data);
if (!cr_done) {
/*
* No HW reset here. CTS waits on write to
* reduced(where applicable) link BW dpcd offset.
*/
lt_data_sw_reset(lt_data);
tgt_state = STATE_REDUCE_BIT_RATE;
timeout = 0;
pr_info("dp lt: CR lost\n");
goto done;
}
ce_done = get_channel_eq_status(lt_data);
if (ce_done) {
lt_passed(lt_data);
tgt_state = STATE_DONE_PASS;
timeout = -1;
pr_info("dp lt: CE done\n");
goto done;
}
pr_info("dp lt: CE not done\n");
if (++(lt_data->ce_retry) > (CE_RETRY_LIMIT + 1)) {
pr_info("dp lt: CE retry limit %d reached\n",
lt_data->ce_retry - 2);
/*
* Just do LT SW reset here. CTS mandates that
* LT config should be reduced only after
* training pattern 1 is set. Reduce bitrate
* state would update new lane count and
* bandwidth. Proceeding clock recovery/fail/reset
* state would reset voltage swing, pre-emphasis
* and post-cursor2 after setting tps 1/0.
*/
lt_data_sw_reset(lt_data);
tgt_state = STATE_REDUCE_BIT_RATE;
timeout = 0;
goto done;
}
get_lt_new_config(lt_data);
set_lt_config(lt_data);
tgt_state = STATE_CHANNEL_EQUALIZATION;
timeout = 0;
pr_info("dp lt: CE retry\n");
done:
set_lt_state(lt_data, tgt_state, timeout);
}
static inline bool is_vs_already_max(struct tegra_dp_lt_data *lt_data,
u32 old_vs[4], u32 new_vs[4])
{
u32 n_lanes = lt_data->n_lanes;
int cnt;
for (cnt = 0; cnt < n_lanes; cnt++) {
if (old_vs[cnt] == DRIVE_CURRENT_L3 &&
new_vs[cnt] == DRIVE_CURRENT_L3)
continue;
return false;
}
return true;
}
static void lt_clock_recovery_state(struct tegra_dp_lt_data *lt_data)
{
struct tegra_dc_dp_data *dp;
struct tegra_dc_sor_data *sor;
int tgt_state;
int timeout;
u32 *vs = lt_data->drive_current;
bool cr_done;
u32 vs_temp[4];
bool cur_hpd;
BUG_ON(!lt_data || !lt_data->dp || !lt_data->dp->sor);
dp = lt_data->dp;
sor = dp->sor;
cur_hpd = tegra_dc_hpd(dp->dc);
if (!cur_hpd) {
pr_info("lt: hpd deasserted, wait for sometime, then reset\n");
lt_failed(lt_data);
tgt_state = STATE_RESET;
timeout = HPD_DROP_TIMEOUT_MS;
goto done;
}
set_lt_tpg(lt_data, TEGRA_DC_DP_TRAINING_PATTERN_1);
set_lt_config(lt_data);
wait_aux_training(lt_data, true);
cr_done = get_clock_recovery_status(lt_data);
if (cr_done) {
lt_data->cr_retry = 0;
tgt_state = STATE_CHANNEL_EQUALIZATION;
timeout = 0;
pr_info("dp lt: CR done\n");
goto done;
}
pr_info("dp lt: CR not done\n");
memcpy(vs_temp, vs, sizeof(vs_temp));
get_lt_new_config(lt_data);
if (!memcmp(vs_temp, vs, sizeof(vs_temp))) {
/*
* Reduce bit rate if voltage swing already max or
* CR retry limit of 5 reached.
*/
if (is_vs_already_max(lt_data, vs_temp, vs) ||
(lt_data->cr_retry)++ >= (CR_RETRY_LIMIT - 1)) {
pr_info("dp lt: CR retry limit %d %s reached\n",
lt_data->cr_retry, is_vs_already_max(
lt_data, vs_temp, vs) ? "for max vs" : "");
/*
* Just do LT SW reset here. CTS mandates that
* LT config should be reduced only after
* bit rate has been reduced. Reduce bitrate
* state would update new lane count and
* bandwidth. Proceeding clock recovery state
* would reset voltage swing, pre-emphasis
* and post-cursor2.
*/
lt_data_sw_reset(lt_data);
tgt_state = STATE_REDUCE_BIT_RATE;
timeout = 0;
goto done;
}
} else {
lt_data->cr_retry = 1;
}
tgt_state = STATE_CLOCK_RECOVERY;
timeout = 0;
pr_info("dp lt: CR retry\n");
done:
set_lt_state(lt_data, tgt_state, timeout);
}
typedef void (*dispatch_func_t)(struct tegra_dp_lt_data *lt_data);
static const dispatch_func_t state_machine_dispatch[] = {
lt_reset_state, /* STATE_RESET */
fast_lt_state, /* STATE_FAST_LT */
lt_clock_recovery_state, /* STATE_CLOCK_RECOVERY */
lt_channel_equalization_state, /* STATE_CHANNEL_EQUALIZATION */
NULL, /* STATE_DONE_FAIL */
NULL, /* STATE_DONE_PASS */
lt_reduce_bit_rate_state, /* STATE_REDUCE_BIT_RATE */
};
static void handle_lt_hpd_evt(struct tegra_dp_lt_data *lt_data, int cur_hpd)
{
int tgt_state = STATE_RESET;
int timeout = 0;
/*
* hpd deasserted while we are still in middle
* of link training. Wait for HPD_DROP_TIMEOUT_MS
* for hpd to come up. Thereafter, reset link training
* state machine.
*/
if (!cur_hpd && !lt_data->force_disable)
timeout = HPD_DROP_TIMEOUT_MS;
if (lt_data->lt_config_valid &&
lt_data->no_aux_handshake &&
!lt_data->force_disable)
tgt_state = STATE_FAST_LT;
set_lt_state(lt_data, tgt_state, timeout);
}
static void lt_worker(struct work_struct *work)
{
int pending_lt_evt;
int cur_hpd;
struct tegra_dp_lt_data *lt_data = container_of(to_delayed_work(work),
struct tegra_dp_lt_data, dwork);
/*
* Observe and clear pending flag
* and latch the current HPD state.
*/
mutex_lock(<_data->lock);
pending_lt_evt = lt_data->pending_evt;
lt_data->pending_evt = 0;
mutex_unlock(<_data->lock);
cur_hpd = !!tegra_dc_hpd(lt_data->dp->dc);
pr_info("dp lt: state %d (%s), hpd %d, pending_lt_evt %d\n",
lt_data->state, tegra_dp_lt_state_names[lt_data->state],
cur_hpd, pending_lt_evt);
if (pending_lt_evt) {
handle_lt_hpd_evt(lt_data, cur_hpd);
} else if (lt_data->state < ARRAY_SIZE(state_machine_dispatch)) {
dispatch_func_t func = state_machine_dispatch[lt_data->state];
if (!func)
pr_warn("dp lt: NULL state handler in state %d\n",
lt_data->state);
else
func(lt_data);
} else {
pr_warn("dp lt: unexpected state scheduled %d",
lt_data->state);
}
}
static void sched_lt_work(struct tegra_dp_lt_data *lt_data, int delay_ms)
{
cancel_delayed_work(<_data->dwork);
if ((delay_ms >= 0) && !lt_data->shutdown)
schedule_delayed_work(<_data->dwork,
msecs_to_jiffies(delay_ms));
}
static void set_lt_state(struct tegra_dp_lt_data *lt_data,
int target_state, int delay_ms)
{
mutex_lock(<_data->lock);
pr_info("dp lt: switching from state %d (%s) to state %d (%s)\n",
lt_data->state, tegra_dp_lt_state_names[lt_data->state],
target_state, tegra_dp_lt_state_names[target_state]);
lt_data->state = target_state;
/* we have reached final state. notify others. */
if (target_state == STATE_DONE_PASS ||
target_state == STATE_DONE_FAIL)
complete_all(<_data->lt_complete);
/*
* If the pending_hpd_evt flag is already set, don't bother to
* reschedule the state machine worker. We should be able to assert
* that there is a worker callback already scheduled, and that it is
* scheduled to run immediately
*/
if (!lt_data->pending_evt)
sched_lt_work(lt_data, delay_ms);
mutex_unlock(<_data->lock);
}
int tegra_dp_get_lt_state(struct tegra_dp_lt_data *lt_data)
{
int ret;
mutex_lock(<_data->lock);
ret = lt_data->state;
mutex_unlock(<_data->lock);
return ret;
}
void tegra_dp_lt_set_pending_evt(struct tegra_dp_lt_data *lt_data)
{
mutex_lock(<_data->lock);
/* always schedule work any time there is a pending lt event */
lt_data->pending_evt = 1;
sched_lt_work(lt_data, 0);
mutex_unlock(<_data->lock);
}
/*
* Marks previous LT configuration data as invalid.
* Full LT is required to get new LT config data.
*/
void tegra_dp_lt_invalidate(struct tegra_dp_lt_data *lt_data)
{
mutex_lock(<_data->lock);
lt_data->lt_config_valid = false;
mutex_unlock(<_data->lock);
}
/* block till link training has reached final state */
long tegra_dp_lt_wait_for_completion(struct tegra_dp_lt_data *lt_data,
int target_state, unsigned long timeout_ms)
{
might_sleep();
if (target_state == tegra_dp_get_lt_state(lt_data))
return 1;
mutex_lock(<_data->lock);
reinit_completion(<_data->lt_complete);
mutex_unlock(<_data->lock);
return wait_for_completion_timeout(<_data->lt_complete,
msecs_to_jiffies(timeout_ms));
}
void tegra_dp_lt_force_disable(struct tegra_dp_lt_data *lt_data)
{
mutex_lock(<_data->lock);
lt_data->force_disable = true;
mutex_unlock(<_data->lock);
tegra_dp_lt_set_pending_evt(lt_data);
}
void tegra_dp_lt_init(struct tegra_dp_lt_data *lt_data,
struct tegra_dc_dp_data *dp)
{
BUG_ON(!dp || !lt_data || !dp->dc);
lt_data->dp = dp;
/* Change for seamless */
if (tegra_dc_hpd(dp->dc) &&
dp->dc->initialized) {
lt_data->state = STATE_DONE_PASS;
/* Set config valid to false */
/* So that fresh LT starts */
lt_data->lt_config_valid = false;
} else {
lt_data->state = STATE_RESET;
lt_data->pending_evt = 0;
lt_data->shutdown = 0;
lt_data_sw_reset(lt_data);
}
mutex_init(<_data->lock);
INIT_DELAYED_WORK(<_data->dwork, lt_worker);
init_completion(<_data->lt_complete);
}
|