blob: e7411d0055d37bae11e8f682f8b4a33609efcf46 [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
2-- Copyright (C) 2015-2016 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?
260 -- Linux always accepts new values from the sink, we don't, for now.
261 if Success and then (CR_Was_Done and not EQ_Done) then
262 Train_Set.Pre_Emph :=
263 DP_Info.Max_Requested_Emph (Status, Link);
264 if Train_Set.Pre_Emph > Max_Pre_Emph (Port, Train_Set)
265 then
266 Train_Set.Pre_Emph := Max_Pre_Emph (Port, Train_Set);
267 end if;
268 end if;
269 end Sink_Adjust_Training;
270 pragma Warnings (GNATprove, On, "unused initial value of ""Port""*");
271
272 ----------------------------------------------------------------------------
273
274 procedure Train_DP
275 (Port : in T;
276 Link : in DP_Link;
277 Success : out Boolean)
278 is
279 use type DP_Info.DP_Voltage_Swing;
280 use type DP_Info.DP_Pre_Emph;
281 use type Word8;
282
283 DP : constant Aux_T := To_Aux (Port);
284
285 Retries : Natural;
286 Max_Retry : constant := 4;
287 CR_Done, EQ_Done : Boolean := False;
288
289 EQ_Pattern : constant DP_Info.Training_Pattern :=
290 (if TPS3_Supported and Link.Receiver_Caps.TPS3_Supported then
291 DP_Info.TP_3
292 else
293 DP_Info.TP_2);
294
295 Train_Set, Last_Train_Set : DP_Info.Train_Set;
296
297 function CR_Delay return Natural is
298 Result : Natural := 100; -- DP spec: 100us
299 begin
300 if Link.Bandwidth = DP_Bandwidth_5_4 and
301 Link.Receiver_Caps.Aux_RD_Interval /= 0
302 then
303 Result := Natural (Link.Receiver_Caps.Aux_RD_Interval) * 4_000;
304 end if;
305 return Result;
306 end CR_Delay;
307
308 function EQ_Delay return Natural is
309 Result : Natural := 400; -- DP spec: 400us
310 begin
311 if Link.Bandwidth = DP_Bandwidth_5_4 and
312 Link.Receiver_Caps.Aux_RD_Interval /= 0
313 then
314 Result := Natural (Link.Receiver_Caps.Aux_RD_Interval) * 4_000;
315 end if;
316 return Result;
317 end EQ_Delay;
318 begin
319 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
320
321 Train_Set.Voltage_Swing := DP_Info.DP_Voltage_Swing'First;
322 Train_Set.Pre_Emph := DP_Info.DP_Pre_Emph'First;
323
324 Set_Pattern (Port, Link, DP_Info.TP_1);
325 Set_Signal_Levels (Port, Link, Train_Set);
326
327 pragma Warnings
328 (GNATprove, Off, """Success"" modified by call, but value overwritten*",
329 Reason => "Read first, then overwritten, looks like a false positive");
330 Sink_Init (DP, Link, Success);
331 pragma Warnings
332 (GNATprove, On, """Success"" modified by call, but value overwritten*");
333 if Success then
334 Sink_Set_Training_Pattern (DP, Link, DP_Info.TP_1, Success);
335 end if;
336
337 if Success then
338 Retries := 0;
339 for Tries in 1 .. 32 loop
340 pragma Loop_Invariant (Retries <= Max_Retry);
341
342 Time.U_Delay (CR_Delay);
343
344 Last_Train_Set := Train_Set;
345 Sink_Adjust_Training
346 (Port, DP, Link, Train_Set, CR_Done, EQ_Done, Success);
347 exit when CR_Done or not Success;
348
349 if Train_Set.Voltage_Swing = Last_Train_Set.Voltage_Swing then
350 exit when Retries = Max_Retry;
351 Retries := Retries + 1;
352 else
353 exit when Last_Train_Set.Voltage_Swing = Max_V_Swing (Port);
354 Retries := 0;
355 end if;
356
357 Set_Signal_Levels (Port, Link, Train_Set);
358 Sink_Set_Signal_Levels (Port, DP, Link, Train_Set, Success);
359 exit when not Success;
360 end loop;
361 end if;
362
363 Success := Success and CR_Done;
364
365 if Success then
366 Set_Pattern (Port, Link, EQ_Pattern);
367 Sink_Set_Training_Pattern (DP, Link, EQ_Pattern, Success);
368 end if;
369
370 if Success then
Nico Huber41b18ca2017-08-27 02:03:04 +0200371 for Tries in 1 .. 6 loop
Nico Huber83693c82016-10-08 22:17:55 +0200372 Time.U_Delay (EQ_Delay);
373
Nico Huber83693c82016-10-08 22:17:55 +0200374 Sink_Adjust_Training
375 (Port, DP, Link, Train_Set, CR_Done, EQ_Done, Success);
376 exit when EQ_Done or not Success;
377
Nico Huber83693c82016-10-08 22:17:55 +0200378 Set_Signal_Levels (Port, Link, Train_Set);
379 Sink_Set_Signal_Levels (Port, DP, Link, Train_Set, Success);
380 exit when not Success;
381 end loop;
382 end if;
383
384 if Success then
385 if EQ_Done then
386 -- Set_Pattern (TP_None) includes sending the Idle Pattern,
387 -- so tell sink first.
388 Sink_Set_Training_Pattern
389 (DP, Link, DP_Info.TP_None, Success);
390 else
391 Success := False;
392 end if;
393 end if;
394
395 if Success then
396 Set_Pattern (Port, Link, DP_Info.TP_None);
397 else
398 Off (Port);
399 end if;
400 end Train_DP;
401
402end HW.GFX.DP_Training;