blob: 10e9ff2bfb849914bf0cc5471a7b6d04a091cfb1 [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
2-- Copyright (C) 2015-2016 secunet Security Networks AG
3--
4-- This program is free software; you can redistribute it and/or modify
5-- it under the terms of the GNU General Public License as published by
Nico Huber125a29e2016-10-18 00:23:54 +02006-- the Free Software Foundation; either version 2 of the License, or
7-- (at your option) any later version.
Nico Huber83693c82016-10-08 22:17:55 +02008--
9-- This program is distributed in the hope that it will be useful,
10-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-- GNU General Public License for more details.
13--
14
15with HW.Time;
16with HW.GFX.GMA.Config;
17with HW.GFX.GMA.Registers;
18
19with HW.Debug;
20with GNAT.Source_Info;
21
22package body HW.GFX.GMA.PLLs
23with
24 Refined_State => (State => PLLs)
25is
26
27 Debug_Clocks : constant Boolean := False;
28
29 type Count_Range is new Natural range 0 .. 2;
30
31 type PLL_State is record
32 Use_Count : Count_Range;
33 Used_For_DP : Boolean;
34 Link_Rate : DP_Bandwidth;
35 Mode : Mode_Type;
36 end record;
37
38 type PLL_State_Array is array (DPLLs) of PLL_State;
39
40 PLLs : PLL_State_Array;
41
42 ----------------------------------------------------------------------------
43
44 subtype N_Range is Int64 range 3 .. 8;
45 subtype M_Range is Int64 range 79 .. 128;
46 subtype M1_Range is Int64 range 14 .. 25;
47 subtype M2_Range is Int64 range 7 .. 12;
48 subtype P_Range is Int64 range 5 .. 112;
49 subtype P1_Range is Int64 range 1 .. 8;
50 subtype P2_Range is Int64 range 5 .. 14;
51 subtype VCO_Range is Int64 range 1760000000 .. 3510000000;
52 subtype Clock_Range is HW.GFX.Frequency_Type;
53
54 type Clock_Type is
55 record
56 N : N_Range;
57 M1 : M1_Range;
58 M2 : M2_Range;
59 P1 : P1_Range;
60 P2 : P2_Range;
61 M : M_Range;
62 P : P_Range;
63 VCO : VCO_Range;
64 Reference_Clock : Clock_Range;
65 Dotclock : Clock_Range;
66 end record;
67
68 Invalid_Clock : constant Clock_Type := Clock_Type'
69 (N => N_Range'Last,
70 M1 => M1_Range'Last,
71 M2 => M2_Range'Last,
72 P1 => P1_Range'Last,
73 P2 => P2_Range'Last,
74 Reference_Clock => Clock_Range'Last,
75 M => M_Range'Last,
76 P => P_Range'Last,
77 VCO => VCO_Range'Last,
78 Dotclock => Clock_Range'Last);
79
80 type Limits_Type is
81 record
82 N_Lower : N_Range;
83 N_Upper : N_Range;
84 M_Lower : M_Range;
85 M_Upper : M_Range;
86 M1_Lower : M1_Range;
87 M1_Upper : M1_Range;
88 M2_Lower : M2_Range;
89 M2_Upper : M2_Range;
90 P_Lower : P_Range;
91 P_Upper : P_Range;
92 P1_Lower : P1_Range;
93 P1_Upper : P1_Range;
94 P2_Fast : P2_Range;
95 P2_Slow : P2_Range;
96 P2_Threshold : Clock_Range;
97 VCO_Lower : VCO_Range;
98 VCO_Upper : VCO_Range;
99 end record;
100
101 LVDS_Single_Limits : constant Limits_Type := Limits_Type'
102 (N_Lower => 3, N_Upper => 5,
103 M_Lower => 79, M_Upper => 118,
104 M1_Lower => 14, M1_Upper => 22, -- this is capped by M_Upper >= 5 * M1 + M2_Lower
105 M2_Lower => 7, M2_Upper => 11,
106 P_Lower => 28, P_Upper => 112,
107 P1_Lower => 2, P1_Upper => 8,
108 P2_Fast => 14, P2_Slow => 14,
109 P2_Threshold => Clock_Range'First,
110 VCO_Lower => 1_760_000_000, VCO_Upper => 3_510_000_000);
111 LVDS_Dual_Limits : constant Limits_Type := Limits_Type'
112 (N_Lower => 3, N_Upper => 5,
113 M_Lower => 79, M_Upper => 127,
114 M1_Lower => 14, M1_Upper => 24,
115 M2_Lower => 7, M2_Upper => 11,
116 P_Lower => 14, P_Upper => 56,
117 P1_Lower => 2, P1_Upper => 8,
118 P2_Fast => 7, P2_Slow => 7,
119 P2_Threshold => Clock_Range'First,
120 VCO_Lower => 1_760_000_000, VCO_Upper => 3_510_000_000);
121 All_Other_Limits : constant Limits_Type := Limits_Type'
122 (N_Lower => 3, N_Upper => 7,
123 M_Lower => 79, M_Upper => 127,
124 M1_Lower => 14, M1_Upper => 24,
125 M2_Lower => 7, M2_Upper => 11,
126 P_Lower => 5, P_Upper => 80,
127 P1_Lower => 1, P1_Upper => 8,
128 -- use P2_Slow if Dotclock <= P2_Threshold, P2_Fast otherwise
129 P2_Fast => 5, P2_Slow => 10,
130 P2_Threshold => 225_000_000,
131 VCO_Lower => 1_760_000_000, VCO_Upper => 3_510_000_000);
132
133 ----------------------------------------------------------------------------
134
135 type Regs is array (DPLLs) of Registers.Registers_Index;
136
137 DPLL : constant Regs := Regs'(Registers.PCH_DPLL_A, Registers.PCH_DPLL_B);
138 DPLL_VCO_ENABLE : constant := 1 * 2 ** 31;
139 DPLL_P2_10_OR_14 : constant := 0 * 2 ** 24;
140 DPLL_P2_5_OR_7 : constant := 1 * 2 ** 24;
141 DPLL_P1_DIVIDER_SHIFT : constant := 16;
142 DPLL_SDVOCLK : constant := 2 * 2 ** 13;
143
144 DPLL_HIGH_SPEED : constant := 1 * 2 ** 30;
145 DPLL_MODE_LVDS : constant := 2 * 2 ** 26;
146 DPLL_MODE_DAC : constant := 1 * 2 ** 26;
147 DPLL_DREFCLK : constant := 0 * 2 ** 13;
148 DPLL_SSC : constant := 3 * 2 ** 13;
149
150 MODE_DPLL_DAC_HDMI : constant Word32 := Word32'
151 (DPLL_MODE_DAC or DPLL_DREFCLK or DPLL_HIGH_SPEED);
152
153 MODE_DPLL_LVDS : constant Word32 := Word32'
154 (DPLL_MODE_LVDS or DPLL_SSC);
155
156 MODE_DPLL_DP : constant Word32 := Word32'
157 (DPLL_MODE_DAC or DPLL_SSC or DPLL_HIGH_SPEED);
158
159 type DPLL_Mode_Array is array (Display_Type) of Word32;
160
161 DPLL_Mode : constant DPLL_Mode_Array := DPLL_Mode_Array'
162 (LVDS => MODE_DPLL_LVDS,
163 DP => MODE_DPLL_DP,
164 others => MODE_DPLL_DAC_HDMI);
165
166 FP0 : constant Regs := Regs'(Registers.PCH_FPA0, Registers.PCH_FPB0);
167 FP1 : constant Regs := Regs'(Registers.PCH_FPA1, Registers.PCH_FPB1);
168 FP_DOUBLE_CLOCK : constant := 1 * 2 ** 27;
169 FP_N_SHIFT : constant := 16;
170 FP_M1_SHIFT : constant := 8;
171 FP_M2_SHIFT : constant := 0;
172
173 ----------------------------------------------------------------------------
174
175 procedure Verify_Parameters
176 (N : in N_Range;
177 M1 : in M1_Range;
178 M2 : in M2_Range;
179 P1 : in P1_Range;
180 P2 : in P2_Range;
181 Reference_Clock : in Clock_Range;
182 Current_Limits : in Limits_Type;
183 Result : out Clock_Type;
184 Valid : out Boolean)
185 with
186 Global => null,
187 Pre => True,
188 Post => True
189 is
190 M : Int64;
191 P : Int64;
192 VCO : Int64;
193 Dotclock : Int64;
194 begin
195 pragma Debug (Debug_Clocks, Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
196
197 M := 5 * M1 + M2;
198 P := P1 * P2;
199 VCO := (Int64 (Reference_Clock) * M) / N;
200 Dotclock := VCO / P;
201
202 pragma Debug (Debug_Clocks and not (Current_Limits.P1_Lower <= P1 and P1 <= Current_Limits.P1_Upper ), Debug.Put_Line ("P1 out of range."));
203 pragma Debug (Debug_Clocks and (Current_Limits.P2_Fast /= P2 and P2 /= Current_Limits.P2_Slow ), Debug.Put_Line ("P2 out of range."));
204 pragma Debug (Debug_Clocks and not (Current_Limits.P_Lower <= P and P <= Current_Limits.P_Upper ), Debug.Put_Line ("P out of range."));
205 pragma Debug (Debug_Clocks and not (Current_Limits.M1_Lower <= M1 and M1 <= Current_Limits.M1_Upper ), Debug.Put_Line ("M1 out of range."));
206 pragma Debug (Debug_Clocks and not (Current_Limits.M2_Lower <= M2 and M2 <= Current_Limits.M2_Upper ), Debug.Put_Line ("M2 out of range."));
207 -- pragma Debug (Debug_Clocks and not (M2 <= M1 ), Debug.Put_Line ("M1 greater thant M2."));
208 pragma Debug (Debug_Clocks and not (Current_Limits.N_Lower <= N and N <= Current_Limits.N_Upper ), Debug.Put_Line ("N out of range."));
209 pragma Debug (Debug_Clocks and not (Current_Limits.M_Lower <= M and M <= Current_Limits.M_Upper ), Debug.Put_Line ("M out of range."));
210 pragma Debug (Debug_Clocks and not (Current_Limits.VCO_Lower <= VCO and VCO <= Current_Limits.VCO_Upper), Debug.Put_Line ("VCO out of range."));
211
212 pragma Debug (Debug_Clocks and not (Int64 (Clock_Range'First) <= Dotclock), Debug.Put_Line ("Dotclock too low."));
213 pragma Debug (Debug_Clocks and not (Int64 (Clock_Range'First) <= Dotclock), Debug.Put_Int64 (Dotclock));
214 pragma Debug (Debug_Clocks and not (Int64 (Clock_Range'First) <= Dotclock), Debug.New_Line);
215
216 pragma Debug (Debug_Clocks and not (Dotclock <= Int64 (Clock_Range'Last)), Debug.Put_Line ("Dotclock too high."));
217 pragma Debug (Debug_Clocks and not (Dotclock <= Int64 (Clock_Range'Last)), Debug.Put_Int64 (Dotclock));
218 pragma Debug (Debug_Clocks and not (Dotclock <= Int64 (Clock_Range'Last)), Debug.New_Line);
219
220 Valid :=
221 Current_Limits.P1_Lower <= P1 and P1 <= Current_Limits.P1_Upper and
222 (Current_Limits.P2_Fast = P2 or P2 = Current_Limits.P2_Slow) and
223 Current_Limits.P_Lower <= P and P <= Current_Limits.P_Upper and
224 Current_Limits.M1_Lower <= M1 and M1 <= Current_Limits.M1_Upper and
225 Current_Limits.M2_Lower <= M2 and M2 <= Current_Limits.M2_Upper and
226 -- M2 <= M1 and
227 Current_Limits.N_Lower <= N and N <= Current_Limits.N_Upper and
228 Current_Limits.M_Lower <= M and M <= Current_Limits.M_Upper and
229 Current_Limits.VCO_Lower <= VCO and VCO <= Current_Limits.VCO_Upper and
230 Int64 (Clock_Range'First) <= Dotclock and
231 Dotclock <= Int64 (Clock_Range'Last);
232
233 if Valid
234 then
235 Result := Clock_Type'
236 (N => N,
237 M1 => M1,
238 M2 => M2,
239 P1 => P1,
240 P2 => P2,
241 Reference_Clock => Reference_Clock,
242 M => M,
243 P => P,
244 VCO => VCO,
245 Dotclock => Clock_Range (Dotclock));
246 else
247 Result := Invalid_Clock;
248 end if;
249
250 end Verify_Parameters;
251
252 procedure Calculate_Clock_Parameters
253 (Display : in Display_Type;
254 Target_Dotclock : in Clock_Range;
255 Reference_Clock : in Clock_Range;
256 Best_Clock : out Clock_Type;
257 Valid : out Boolean)
258 with
259 Global => null,
260 Pre => True,
261 Post => True
262 is
263 Limits : constant Limits_Type :=
264 (if Display = LVDS then
265 (if Target_Dotclock >= Config.LVDS_Dual_Threshold then
266 LVDS_Dual_Limits
267 else
268 LVDS_Single_Limits)
269 else
270 All_Other_Limits);
271
272 P2 : P2_Range;
273 Best_Delta : Int64 := Int64'Last;
274 Current_Delta : Int64;
275 Current_Clock : Clock_Type;
276 Registers_Valid : Boolean;
277 begin
278 pragma Debug (Debug_Clocks, Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
279
280 Valid := False;
281 Best_Clock := Invalid_Clock;
282
283 if Target_Dotclock <= Limits.P2_Threshold then
284 P2 := Limits.P2_Slow;
285 else
286 P2 := Limits.P2_Fast;
287 end if;
288
289 for N in N_Range range Limits.N_Lower .. Limits.N_Upper
290 loop
291 -- reverse loops as hardware prefers higher values
292 for M1 in reverse M1_Range range Limits.M1_Lower .. Limits.M1_Upper
293 loop
294 for M2 in reverse M2_Range range Limits.M2_Lower .. Limits.M2_Upper
295 loop
296 for P1 in reverse P1_Range range Limits.P1_Lower .. Limits.P1_Upper
297 loop
298 Verify_Parameters
299 (N => N,
300 M1 => M1,
301 M2 => M2,
302 P1 => P1,
303 P2 => P2,
304 Reference_Clock => Reference_Clock,
305 Current_Limits => Limits,
306 Result => Current_Clock,
307 Valid => Registers_Valid);
308
309 if Registers_Valid
310 then
311 if Current_Clock.Dotclock > Target_Dotclock
312 then
313 Current_Delta := Current_Clock.Dotclock - Target_Dotclock;
314 else
315 Current_Delta := Target_Dotclock - Current_Clock.Dotclock;
316 end if;
317
318 if Current_Delta < Best_Delta
319 then
320 Best_Delta := Current_Delta;
321 Best_Clock := Current_Clock;
322 Valid := True;
323 end if;
324
325 pragma Debug (Debug_Clocks, Debug.Put ("Current/Target/Best_Delta: "));
326 pragma Debug (Debug_Clocks, Debug.Put_Int64 (Current_Clock.Dotclock));
327 pragma Debug (Debug_Clocks, Debug.Put ("/"));
328 pragma Debug (Debug_Clocks, Debug.Put_Int64 (Target_Dotclock));
329 pragma Debug (Debug_Clocks, Debug.Put ("/"));
330 pragma Debug (Debug_Clocks, Debug.Put_Int64 (Best_Delta));
331 pragma Debug (Debug_Clocks, Debug.Put_Line ("."));
332
333 end if;
334 end loop;
335 end loop;
336 end loop;
337 end loop;
338
339 pragma Debug (Valid, Debug.Put_Line ("Valid clock found."));
340 pragma Debug (Valid, Debug.Put ("Best/Target/Delta: "));
341 pragma Debug (Valid, Debug.Put_Int64 (Best_Clock.Dotclock));
342 pragma Debug (Valid, Debug.Put ("/"));
343 pragma Debug (Valid, Debug.Put_Int64 (Target_Dotclock));
344 pragma Debug (Valid, Debug.Put ("/"));
345 pragma Debug (Valid, Debug.Put_Int64 (Best_Delta));
346 pragma Debug (Valid, Debug.Put_Line ("."));
347 pragma Debug (not Valid, Debug.Put_Line ("No valid clock found."));
348
349 end Calculate_Clock_Parameters;
350
351 procedure Program_DPLL
352 (PLL : DPLLs;
353 Display : Display_Type;
354 Clk : Clock_Type)
355 with
356 Global => (In_Out => Registers.Register_State),
357 Pre => True,
358 Post => True
359 is
360 FP, Encoded_P1, Encoded_P2 : Word32;
361 begin
362 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
363
364 FP :=
365 Shift_Left (Word32 (Clk.N - 2), FP_N_SHIFT) or
366 Shift_Left (Word32 (Clk.M1 - 2), FP_M1_SHIFT) or
367 Shift_Left (Word32 (Clk.M2 - 2), FP_M2_SHIFT);
368
369 Registers.Write (FP0 (PLL), FP);
370 Registers.Write (FP1 (PLL), FP);
371
372 Encoded_P1 := Shift_Left (1, Natural (Clk.P1) - 1);
373
374 if Clk.P2 = 5 or Clk.P2 = 7
375 then
376 Encoded_P2 := DPLL_P2_5_OR_7;
377 else
378 Encoded_P2 := DPLL_P2_10_OR_14;
379 end if;
380
381 Registers.Write
382 (Register => DPLL (PLL),
383 Value => DPLL_Mode (Display) or
384 Encoded_P2 or
385 Shift_Left (Encoded_P1, DPLL_P1_DIVIDER_SHIFT) or
386 Encoded_P1);
387 end Program_DPLL;
388
389 procedure On
390 (PLL : in T;
391 Port_Cfg : in Port_Config;
392 Success : out Boolean)
393 is
394 Target_Clock : constant Frequency_Type :=
395 (if Port_Cfg.Display = DP then
396 DP_Symbol_Rate (Port_Cfg.DP.Bandwidth)
397 else
398 Port_Cfg.Mode.Dotclock);
399 Clk : Clock_Type;
400 begin
401 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
402
403 Success := PLL in DPLLs;
404 Clk := Invalid_Clock;
405
406 if Success then
407 if Port_Cfg.Display = DP then
408 Success := True;
409 -- we use static values for DP
410 case Port_Cfg.DP.Bandwidth is
411 when DP_Bandwidth_1_62 =>
412 Clk.N := 3;
413 Clk.M1 := 14;
414 Clk.M2 := 11;
415 Clk.P1 := 2;
416 Clk.P2 := 10;
417 when DP_Bandwidth_2_7 =>
418 Clk.N := 4;
419 Clk.M1 := 16;
420 Clk.M2 := 10;
421 Clk.P1 := 1;
422 Clk.P2 := 10;
423 when others =>
424 Success := False;
425 end case;
426 elsif Target_Clock <= 340_000_000 then
427 Calculate_Clock_Parameters
428 (Display => Port_Cfg.Display,
429 Target_Dotclock => Target_Clock,
430 -- should be, but doesn't has to be always the same:
431 Reference_Clock => 120_000_000,
432 Best_Clock => Clk,
433 Valid => Success);
434 else
435 Success := False;
436 pragma Debug (Debug.Put ("WARNING: Targeted clock too high: "));
437 pragma Debug (Debug.Put_Int64 (Target_Clock));
438 pragma Debug (Debug.Put (" > "));
439 pragma Debug (Debug.Put_Int32 (340_000_000));
440 pragma Debug (Debug.New_Line);
441 pragma Debug (Debug.New_Line);
442 end if;
443 end if;
444
445 if Success then
446 Program_DPLL (PLL, Port_Cfg.Display, Clk);
447
448 Registers.Set_Mask (DPLL (PLL), DPLL_VCO_ENABLE);
449 Registers.Posting_Read (DPLL (PLL));
450 Time.U_Delay (150);
451 end if;
452 end On;
453
454 procedure Off (PLL : T)
455 is
456 begin
457 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
458
459 if PLL in DPLLs then
460 Registers.Unset_Mask (DPLL (PLL), DPLL_VCO_ENABLE);
461 end if;
462 end Off;
463
464 ----------------------------------------------------------------------------
465
466 procedure Initialize
467 is
468 begin
469 PLLs :=
470 (DPLLs =>
471 (Use_Count => 0,
472 Used_For_DP => False,
473 Link_Rate => DP_Bandwidth'First,
474 Mode => Invalid_Mode));
475 end Initialize;
476
477 procedure Alloc_Configurable
478 (Port_Cfg : in Port_Config;
479 PLL : out T;
480 Success : out Boolean)
481 with
482 Pre => True
483 is
484 function Config_Matches (PE : PLL_State) return Boolean
485 is
486 begin
487 return
488 PE.Used_For_DP = (Port_Cfg.Display = DP) and
489 ((PE.Used_For_DP and PE.Link_Rate = Port_Cfg.DP.Bandwidth) or
490 (not PE.Used_For_DP and PE.Mode = Port_Cfg.Mode));
491 end Config_Matches;
492 begin
493 -- try to find shareable PLL
494 for P in DPLLs loop
495 Success := PLLs (P).Use_Count /= 0 and
496 PLLs (P).Use_Count /= Count_Range'Last and
497 Config_Matches (PLLs (P));
498 if Success then
499 PLL := P;
500 PLLs (PLL).Use_Count := PLLs (PLL).Use_Count + 1;
501 return;
502 end if;
503 end loop;
504
505 -- try to find free PLL
506 for P in DPLLs loop
507 if PLLs (P).Use_Count = 0 then
508 PLL := P;
509 On (PLL, Port_Cfg, Success);
510 if Success then
511 PLLs (PLL) :=
512 (Use_Count => 1,
513 Used_For_DP => Port_Cfg.Display = DP,
514 Link_Rate => Port_Cfg.DP.Bandwidth,
515 Mode => Port_Cfg.Mode);
516 end if;
517 return;
518 end if;
519 end loop;
520
521 PLL := Invalid;
522 end Alloc_Configurable;
523
524 procedure Alloc
525 (Port_Cfg : in Port_Config;
526 PLL : out T;
527 Success : out Boolean)
528 is
529 begin
530 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
531
532 if Port_Cfg.Port = DIGI_A then
533 PLL := Invalid;
534 Success := True;
535 else
536 Alloc_Configurable (Port_Cfg, PLL, Success);
537 end if;
538 end Alloc;
539
540 procedure Free (PLL : T)
541 is
542 begin
543 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
544
545 if PLL in DPLLs then
546 if PLLs (PLL).Use_Count /= 0 then
547 PLLs (PLL).Use_Count := PLLs (PLL).Use_Count - 1;
548 if PLLs (PLL).Use_Count = 0 then
549 Off (PLL);
550 end if;
551 end if;
552 end if;
553 end Free;
554
555 procedure All_Off
556 is
557 begin
558 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
559
560 for PLL in DPLLs loop
561 Off (PLL);
562 end loop;
563 end All_Off;
564
565 function Register_Value (PLL : T) return Word32
566 is
567 begin
568 return (if PLL = DPLL_B then 1 else 0);
569 end Register_Value;
570
571end HW.GFX.GMA.PLLs;