blob: 079d49c242fbc5dddb17d3b7c6f1c6d9009aa4ec [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
Nico Huberf80c3e42019-05-10 13:44:02 +02002-- Copyright (C) 2015-2016, 2019 secunet Security Networks AG
Nico Huber41b18ca2017-08-27 02:03:04 +02003-- Copyright (C) 2017 Nico Huber <nico.h@gmx.de>
Nico Huber83693c82016-10-08 22:17:55 +02004--
5-- This program is free software; you can redistribute it and/or modify
6-- it under the terms of the GNU General Public License as published by
Nico Huber125a29e2016-10-18 00:23:54 +02007-- the Free Software Foundation; either version 2 of the License, or
8-- (at your option) any later version.
Nico Huber83693c82016-10-08 22:17:55 +02009--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-- GNU General Public License for more details.
14--
15
16with Ada.Unchecked_Conversion;
17
18with HW.Debug;
19with GNAT.Source_Info;
20
21with HW.Time;
22with HW.GFX.DP_Defs;
23
24package body HW.GFX.DP_Training is
25
26 pragma Warnings (GNATprove, Off, "unused initial value of ""Port""*",
27 Reason => "Needed for a common interface");
28 function Training_Set
29 (Port : T;
30 Train_Set : DP_Info.Train_Set)
31 return Word8
32 is
33 use type DP_Info.DP_Voltage_Swing;
34 use type DP_Info.DP_Pre_Emph;
35 use type Word8;
36 Value : Word8;
37 begin
38 case Train_Set.Voltage_Swing is
39 when DP_Info.VS_Level_0 => Value := 16#00#;
40 when DP_Info.VS_Level_1 => Value := 16#01#;
41 when DP_Info.VS_Level_2 => Value := 16#02#;
42 when DP_Info.VS_Level_3 => Value := 16#03#;
43 end case;
44 if Train_Set.Voltage_Swing = Max_V_Swing (Port) then
45 Value := Value or 16#04#;
46 end if;
47
48 case Train_Set.Pre_Emph is
49 when DP_Info.Emph_Level_0 => Value := Value or 16#00#;
50 when DP_Info.Emph_Level_1 => Value := Value or 16#08#;
51 when DP_Info.Emph_Level_2 => Value := Value or 16#10#;
52 when DP_Info.Emph_Level_3 => Value := Value or 16#18#;
53 end case;
54 if Train_Set.Pre_Emph = Max_Pre_Emph (Port, Train_Set) then
55 Value := Value or 16#20#;
56 end if;
57
58 return Value;
59 end Training_Set;
60 pragma Warnings (GNATprove, On, "unused initial value of ""Port""*");
61
62 ----------------------------------------------------------------------------
63
64 function Lane_Count (Link : DP_Link) return Positive
65 with
66 Post => Lane_Count'Result <= 4
67 is
68 begin
69 return Positive (Lane_Count_As_Integer (Link.Lane_Count));
70 end Lane_Count;
71
72 procedure Sink_Init
73 (Port : in Aux_T;
74 Link : in DP_Link;
75 Success : out Boolean)
76 is
77 use type Word8;
78 function Link_Rate_As_Word8 is new Ada.Unchecked_Conversion
79 (Source => DP_Bandwidth, Target => Word8);
80 Data : DP_Defs.Aux_Payload;
81 begin
82 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
83
84 Data :=
85 (0 => Link_Rate_As_Word8 (Link.Bandwidth),
86 1 => Word8 (Lane_Count (Link)),
87 others => 0); -- Don't care
88
89 if Link.Enhanced_Framing then
90 Data (1) := Data (1) or 16#80#;
91 end if;
92
93 Aux_Ch.Aux_Write
94 (Port => Port,
95 Address => 16#00100#, -- LINK_BW_SET, LANE_COUNT_SET
96 Length => 2,
97 Data => Data,
98 Success => Success);
99 Success := Success or Link.Opportunistic_Training;
100
101 if Success then
102 Data (0) := 16#00#; -- no downspread
103 Data (1) := 16#01#; -- ANSI8B10B coding
104
105 Aux_Ch.Aux_Write
106 (Port => Port,
107 Address => 16#00107#, -- DOWNSPREAD_CTRL,
108 Length => 2, -- MAIN_LINK_CHANNEL_CODING_SET
109 Data => Data,
110 Success => Success);
111 Success := Success or Link.Opportunistic_Training;
112 end if;
113 end Sink_Init;
114
115 procedure Sink_Set_Training_Pattern
116 (DP : in Aux_T;
117 Link : in DP_Link;
118 Pattern : in DP_Info.Training_Pattern;
119 Success : out Boolean)
120 is
121 use type DP_Info.Training_Pattern;
122
123 type TP_Array is array (DP_Info.Training_Pattern) of Word8;
124 TP : constant TP_Array := TP_Array'
125 (DP_Info.TP_1 => 16#21#, DP_Info.TP_2 => 16#22#, DP_Info.TP_3 => 16#23#,
126 DP_Info.TP_Idle => 16#00#, DP_Info.TP_None => 16#00#);
127
128 Data : DP_Defs.Aux_Payload;
129 Length : Positive := 1;
130 begin
131 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
132
133 Data :=
134 (0 => TP (Pattern),
135 others => 0); -- Don't care
136
137 if Pattern < DP_Info.TP_Idle then
138 Length := Length + Lane_Count (Link);
139 end if;
140 Aux_Ch.Aux_Write
141 (Port => DP,
142 Address => 16#00102#, -- TRAINING_PATTERN_SET
143 Length => Length,
144 Data => Data,
145 Success => Success);
146 end Sink_Set_Training_Pattern;
147
148 procedure Sink_Set_Signal_Levels
149 (Port : in T;
150 DP : in Aux_T;
151 Link : in DP_Link;
152 Train_Set : in DP_Info.Train_Set;
153 Success : out Boolean)
154 is
155 Data : DP_Defs.Aux_Payload;
156 T_Set : constant Word8 := Training_Set (Port, Train_Set);
157 begin
158 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
159
160 Data := (others => 0); -- Initialize
161 Data (0 .. Lane_Count (Link) - 1) := (others => T_Set);
162
163 Aux_Ch.Aux_Write
164 (Port => DP,
165 Address => 16#00103#, -- TRAINING_LANEx_SET
166 Length => Lane_Count (Link),
167 Data => Data,
168 Success => Success);
169 end Sink_Set_Signal_Levels;
170
171 pragma Warnings (GNATprove, Off, "unused initial value of ""Port""*",
172 Reason => "Needed for a common interface");
173 procedure Sink_Adjust_Training
174 (Port : in T;
175 DP : in Aux_T;
176 Link : in DP_Link;
177 Train_Set : in out DP_Info.Train_Set;
178 CR_Done : in out Boolean;
179 EQ_Done : out Boolean;
180 Success : out Boolean)
181 is
182 use type DP_Info.DP_Voltage_Swing;
183 use type DP_Info.DP_Pre_Emph;
184
185 Status : DP_Info.Link_Status;
186 CR_Was_Done : constant Boolean := CR_Done;
187
188 pragma Warnings
189 (GNATprove, Off, "subprogram ""Dump_Link_Status"" has no effect*",
190 Reason => "It's only used for debugging");
191 procedure Dump_Link_Status
192 is
193 begin
194 Debug.New_Line;
195 Debug.Put_Line ("Link Status:");
196
197 for Lane in DP_Info.Lane_Index range 0
198 .. DP_Info.Lane_Index (Lane_Count_As_Integer (Link.Lane_Count) - 1)
199 loop
200 Debug.Put (" Lane");
201 Debug.Put_Int8 (Int8 (Lane));
202 Debug.Put_Line (":");
203
204 Debug.Put_Line (" CR_Done : " &
205 (if Status.Lanes (Lane).CR_Done then "1" else "0"));
206 Debug.Put_Line (" Channel_EQ_Done: " &
207 (if Status.Lanes (Lane).Channel_EQ_Done then "1" else "0"));
208 Debug.Put_Line (" Symbol_Locked : " &
209 (if Status.Lanes (Lane).Symbol_Locked then "1" else "0"));
210 end loop;
211
212 Debug.Put_Line (" Interlane_Align_Done: " &
213 (if Status.Interlane_Align_Done then "1" else "0"));
214
215 for Lane in DP_Info.Lane_Index range 0
216 .. DP_Info.Lane_Index (Lane_Count_As_Integer (Link.Lane_Count) - 1)
217 loop
218 Debug.Put (" Adjust");
219 Debug.Put_Int8 (Int8 (Lane));
220 Debug.Put_Line (":");
221
222 Debug.Put (" Voltage_Swing: ");
223 Debug.Put_Int8 (Int8 (DP_Info.DP_Voltage_Swing'Pos
224 (Status.Adjust_Requests (Lane).Voltage_Swing)));
225 Debug.New_Line;
226 Debug.Put (" Pre_Emph : ");
227 Debug.Put_Int8 (Int8 (DP_Info.DP_Pre_Emph'Pos
228 (Status.Adjust_Requests (Lane).Pre_Emph)));
229 Debug.New_Line;
230 end loop;
231
232 Debug.New_Line;
233 end Dump_Link_Status;
234 begin
235 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
236
237 DP_Info.Read_Link_Status
238 (Port => DP,
239 Status => Status,
240 Success => Success);
241
242 pragma Debug (Success, Dump_Link_Status);
243
244 CR_Done := Success and then DP_Info.All_CR_Done (Status, Link);
245 EQ_Done := Success and then DP_Info.All_EQ_Done (Status, Link);
246 Success := Success and then (CR_Done or not CR_Was_Done);
247
Nico Huber41b18ca2017-08-27 02:03:04 +0200248 -- Voltage swing may be updated during channel equalization too.
249 if Success and not EQ_Done then
Nico Huber83693c82016-10-08 22:17:55 +0200250 Train_Set.Voltage_Swing :=
251 DP_Info.Max_Requested_VS (Status, Link);
252 if Train_Set.Voltage_Swing > Max_V_Swing (Port)
253 then
254 Train_Set.Voltage_Swing := Max_V_Swing (Port);
255 end if;
256 end if;
257
258 -- According to DP spec, only change preemphasis during channel
259 -- equalization. What to do if sink requests it during clock recovery?
Nico Huber234e7722017-08-27 02:07:31 +0200260 -- Linux always accepts new values from the sink, we too, now: There
261 -- are sinks in the wild that need this.
262 if Success and not EQ_Done then
Nico Huber83693c82016-10-08 22:17:55 +0200263 Train_Set.Pre_Emph :=
264 DP_Info.Max_Requested_Emph (Status, Link);
265 if Train_Set.Pre_Emph > Max_Pre_Emph (Port, Train_Set)
266 then
267 Train_Set.Pre_Emph := Max_Pre_Emph (Port, Train_Set);
268 end if;
269 end if;
270 end Sink_Adjust_Training;
271 pragma Warnings (GNATprove, On, "unused initial value of ""Port""*");
272
273 ----------------------------------------------------------------------------
274
275 procedure Train_DP
276 (Port : in T;
277 Link : in DP_Link;
278 Success : out Boolean)
279 is
280 use type DP_Info.DP_Voltage_Swing;
281 use type DP_Info.DP_Pre_Emph;
282 use type Word8;
283
284 DP : constant Aux_T := To_Aux (Port);
285
286 Retries : Natural;
287 Max_Retry : constant := 4;
288 CR_Done, EQ_Done : Boolean := False;
289
290 EQ_Pattern : constant DP_Info.Training_Pattern :=
291 (if TPS3_Supported and Link.Receiver_Caps.TPS3_Supported then
292 DP_Info.TP_3
293 else
294 DP_Info.TP_2);
295
296 Train_Set, Last_Train_Set : DP_Info.Train_Set;
297
298 function CR_Delay return Natural is
299 Result : Natural := 100; -- DP spec: 100us
300 begin
301 if Link.Bandwidth = DP_Bandwidth_5_4 and
302 Link.Receiver_Caps.Aux_RD_Interval /= 0
303 then
304 Result := Natural (Link.Receiver_Caps.Aux_RD_Interval) * 4_000;
305 end if;
306 return Result;
307 end CR_Delay;
308
309 function EQ_Delay return Natural is
310 Result : Natural := 400; -- DP spec: 400us
311 begin
312 if Link.Bandwidth = DP_Bandwidth_5_4 and
313 Link.Receiver_Caps.Aux_RD_Interval /= 0
314 then
315 Result := Natural (Link.Receiver_Caps.Aux_RD_Interval) * 4_000;
316 end if;
317 return Result;
318 end EQ_Delay;
319 begin
320 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
321
322 Train_Set.Voltage_Swing := DP_Info.DP_Voltage_Swing'First;
323 Train_Set.Pre_Emph := DP_Info.DP_Pre_Emph'First;
324
325 Set_Pattern (Port, Link, DP_Info.TP_1);
326 Set_Signal_Levels (Port, Link, Train_Set);
327
328 pragma Warnings
329 (GNATprove, Off, """Success"" modified by call, but value overwritten*",
330 Reason => "Read first, then overwritten, looks like a false positive");
331 Sink_Init (DP, Link, Success);
332 pragma Warnings
333 (GNATprove, On, """Success"" modified by call, but value overwritten*");
334 if Success then
335 Sink_Set_Training_Pattern (DP, Link, DP_Info.TP_1, Success);
336 end if;
337
338 if Success then
339 Retries := 0;
340 for Tries in 1 .. 32 loop
341 pragma Loop_Invariant (Retries <= Max_Retry);
342
343 Time.U_Delay (CR_Delay);
344
345 Last_Train_Set := Train_Set;
346 Sink_Adjust_Training
347 (Port, DP, Link, Train_Set, CR_Done, EQ_Done, Success);
348 exit when CR_Done or not Success;
349
350 if Train_Set.Voltage_Swing = Last_Train_Set.Voltage_Swing then
351 exit when Retries = Max_Retry;
352 Retries := Retries + 1;
353 else
354 exit when Last_Train_Set.Voltage_Swing = Max_V_Swing (Port);
355 Retries := 0;
356 end if;
357
358 Set_Signal_Levels (Port, Link, Train_Set);
359 Sink_Set_Signal_Levels (Port, DP, Link, Train_Set, Success);
360 exit when not Success;
361 end loop;
362 end if;
363
364 Success := Success and CR_Done;
365
366 if Success then
367 Set_Pattern (Port, Link, EQ_Pattern);
368 Sink_Set_Training_Pattern (DP, Link, EQ_Pattern, Success);
369 end if;
370
371 if Success then
Nico Huber41b18ca2017-08-27 02:03:04 +0200372 for Tries in 1 .. 6 loop
Nico Huber83693c82016-10-08 22:17:55 +0200373 Time.U_Delay (EQ_Delay);
374
Nico Huber83693c82016-10-08 22:17:55 +0200375 Sink_Adjust_Training
376 (Port, DP, Link, Train_Set, CR_Done, EQ_Done, Success);
377 exit when EQ_Done or not Success;
378
Nico Huber83693c82016-10-08 22:17:55 +0200379 Set_Signal_Levels (Port, Link, Train_Set);
380 Sink_Set_Signal_Levels (Port, DP, Link, Train_Set, Success);
381 exit when not Success;
382 end loop;
383 end if;
384
Nico Huberf80c3e42019-05-10 13:44:02 +0200385 -- Set_Pattern (TP_None) includes sending the Idle Pattern,
386 -- so tell sink first.
387 Sink_Set_Training_Pattern
388 (DP, Link, DP_Info.TP_None, Success);
389 Set_Pattern (Port, Link, DP_Info.TP_None);
Nico Huber83693c82016-10-08 22:17:55 +0200390
Nico Huberf80c3e42019-05-10 13:44:02 +0200391 Success := Success and then EQ_Done;
392 if not Success then
Nico Huber83693c82016-10-08 22:17:55 +0200393 Off (Port);
394 end if;
395 end Train_DP;
396
397end HW.GFX.DP_Training;