blob: edbaf009a749ba89fbb551ba50796943cd85d713 [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
Nico Huber6b4678d2019-09-22 21:31:52 +02002-- Copyright (C) 2014-2016, 2019 secunet Security Networks AG
Nico Huber83693c82016-10-08 22:17:55 +02003--
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 GNAT.Source_Info;
16
17with HW.Time;
18with HW.Debug;
19with HW.GFX.GMA.Config;
20with HW.GFX.GMA.Registers;
Nico Huber312433c2019-09-28 03:15:48 +020021with HW.GFX.GMA.PCode;
Angel Pons3f86b0b2020-07-18 00:22:32 +020022with HW.GFX.GMA.Transcoder;
Nico Huber83693c82016-10-08 22:17:55 +020023
24use type HW.Word64;
25
Angel Pons3318bf22020-07-19 18:38:32 +020026package body HW.GFX.GMA.Power_And_Clocks is
Nico Huber83693c82016-10-08 22:17:55 +020027
28 type Power_Domain is (MISC_IO, PW1, PW2, DDI_AE, DDI_B, DDI_C, DDI_D);
29 subtype Power_Well is Power_Domain range PW1 .. PW2;
30 subtype Dynamic_Domain is Power_Domain range PW2 .. DDI_D;
31
32 NDE_RSTWRN_OPT_RST_PCH_Handshake_En : constant := 1 * 2 ** 4;
33
34 FUSE_STATUS_DOWNLOAD_STATUS : constant := 1 * 2 ** 31;
35 FUSE_STATUS_PG0_DIST_STATUS : constant := 1 * 2 ** 27;
36
Nico Huber6b4678d2019-09-22 21:31:52 +020037 DFSM_DISPLAY_CDCLK_LIMIT_675MHZ : constant := 0 * 2 ** 23;
38 DFSM_DISPLAY_CDCLK_LIMIT_540MHZ : constant := 1 * 2 ** 23;
39 DFSM_DISPLAY_CDCLK_LIMIT_450MHZ : constant := 2 * 2 ** 23;
40 DFSM_DISPLAY_CDCLK_LIMIT_337_5MHZ : constant := 3 * 2 ** 23;
41 DFSM_DISPLAY_CDCLK_LIMIT_MASK : constant := 3 * 2 ** 23;
42
Nico Huberdde06302020-12-20 02:18:30 +010043 SFUSE_STRAP_RAW_FREQUENCY : constant := 1 * 2 ** 8;
44
Nico Huber83693c82016-10-08 22:17:55 +020045 type Power_Domain_Values is array (Power_Domain) of Word32;
46 PWR_WELL_CTL_POWER_REQUEST : constant Power_Domain_Values :=
47 (MISC_IO => 1 * 2 ** 1,
48 DDI_AE => 1 * 2 ** 3,
49 DDI_B => 1 * 2 ** 5,
50 DDI_C => 1 * 2 ** 7,
51 DDI_D => 1 * 2 ** 9,
52 PW1 => 1 * 2 ** 29,
53 PW2 => 1 * 2 ** 31);
54 PWR_WELL_CTL_POWER_STATE : constant Power_Domain_Values :=
55 (MISC_IO => 1 * 2 ** 0,
56 DDI_AE => 1 * 2 ** 2,
57 DDI_B => 1 * 2 ** 4,
58 DDI_C => 1 * 2 ** 6,
59 DDI_D => 1 * 2 ** 8,
60 PW1 => 1 * 2 ** 28,
61 PW2 => 1 * 2 ** 30);
62
63 type Power_Well_Values is array (Power_Well) of Word32;
64 FUSE_STATUS_PGx_DIST_STATUS : constant Power_Well_Values :=
65 (PW1 => 1 * 2 ** 26,
66 PW2 => 1 * 2 ** 25);
67
68 DBUF_CTL_DBUF_POWER_REQUEST : constant := 1 * 2 ** 31;
69 DBUF_CTL_DBUF_POWER_STATE : constant := 1 * 2 ** 30;
70
71 ----------------------------------------------------------------------------
72
73 DPLL_CTRL1_DPLL0_LINK_RATE_MASK : constant := 7 * 2 ** 1;
74 DPLL_CTRL1_DPLL0_LINK_RATE_2700MHZ : constant := 0 * 2 ** 1;
75 DPLL_CTRL1_DPLL0_LINK_RATE_1350MHZ : constant := 1 * 2 ** 1;
76 DPLL_CTRL1_DPLL0_LINK_RATE_810MHZ : constant := 2 * 2 ** 1;
77 DPLL_CTRL1_DPLL0_LINK_RATE_1620MHZ : constant := 3 * 2 ** 1;
78 DPLL_CTRL1_DPLL0_LINK_RATE_1080MHZ : constant := 4 * 2 ** 1;
79 DPLL_CTRL1_DPLL0_LINK_RATE_2160MHZ : constant := 5 * 2 ** 1;
80 DPLL_CTRL1_DPLL0_OVERRIDE : constant := 1 * 2 ** 0;
81
82 LCPLL1_CTL_PLL_ENABLE : constant := 1 * 2 ** 31;
83 LCPLL1_CTL_PLL_LOCK : constant := 1 * 2 ** 30;
84
85 ----------------------------------------------------------------------------
86
87 CDCLK_CTL_CD_FREQ_SELECT_MASK : constant := 3 * 2 ** 26;
88 CDCLK_CTL_CD_FREQ_SELECT_450MHZ : constant := 0 * 2 ** 26;
89 CDCLK_CTL_CD_FREQ_SELECT_540MHZ : constant := 1 * 2 ** 26;
90 CDCLK_CTL_CD_FREQ_SELECT_337_5MHZ : constant := 2 * 2 ** 26;
91 CDCLK_CTL_CD_FREQ_SELECT_675MHZ : constant := 3 * 2 ** 26;
92 CDCLK_CTL_CD_FREQ_DECIMAL_MASK : constant := 16#7ff#;
93
94 SKL_PCODE_CDCLK_CONTROL : constant := 7;
95 SKL_CDCLK_PREPARE_FOR_CHANGE : constant := 3;
96 SKL_CDCLK_READY_FOR_CHANGE : constant := 1;
97
Nico Huber6b4678d2019-09-22 21:31:52 +020098 function CDCLK_CTL_CD_FREQ_DECIMAL (CDClk : Frequency_Type) return Word32 is
Nico Huber83693c82016-10-08 22:17:55 +020099 begin
Nico Huber6b4678d2019-09-22 21:31:52 +0200100 -- Weirdest representation: CDClk - 1MHz in 10.1 (10 + 1 fractional bit)
101 return Word32 ((CDClk - 1_000_000) / 500_000);
Nico Huber83693c82016-10-08 22:17:55 +0200102 end CDCLK_CTL_CD_FREQ_DECIMAL;
103
104 ----------------------------------------------------------------------------
105
Nico Huber83693c82016-10-08 22:17:55 +0200106 procedure PD_Off (PD : Power_Domain)
107 is
108 Ctl1, Ctl2, Ctl3, Ctl4 : Word32;
109 begin
110 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
111
112 Registers.Read (Registers.PWR_WELL_CTL_BIOS, Ctl1);
113 Registers.Read (Registers.PWR_WELL_CTL_DRIVER, Ctl2);
114 Registers.Read (Registers.PWR_WELL_CTL_KVMR, Ctl3);
115 Registers.Read (Registers.PWR_WELL_CTL_DEBUG, Ctl4);
116 pragma Debug (Registers.Posting_Read (Registers.PWR_WELL_CTL5)); -- Result for debugging only
117 pragma Debug (Registers.Posting_Read (Registers.PWR_WELL_CTL6)); -- Result for debugging only
118
119 if ((Ctl1 or Ctl2 or Ctl3 or Ctl4) and
120 PWR_WELL_CTL_POWER_REQUEST (PD)) /= 0
121 then
122 Registers.Wait_Set_Mask
123 (Register => Registers.PWR_WELL_CTL_DRIVER,
124 Mask => PWR_WELL_CTL_POWER_STATE (PD));
125 end if;
126
127 if (Ctl1 and PWR_WELL_CTL_POWER_REQUEST (PD)) /= 0 then
128 Registers.Unset_Mask
129 (Register => Registers.PWR_WELL_CTL_BIOS,
130 Mask => PWR_WELL_CTL_POWER_REQUEST (PD));
131 end if;
132
133 if (Ctl2 and PWR_WELL_CTL_POWER_REQUEST (PD)) /= 0 then
134 Registers.Unset_Mask
135 (Register => Registers.PWR_WELL_CTL_DRIVER,
136 Mask => PWR_WELL_CTL_POWER_REQUEST (PD));
137 end if;
138 end PD_Off;
139
140 procedure PD_On (PD : Power_Domain)
141 with
142 Pre => True
143 is
144 Ctl1, Ctl2, Ctl3, Ctl4 : Word32;
145 begin
146 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
147
148 Registers.Read (Registers.PWR_WELL_CTL_BIOS, Ctl1);
149 Registers.Read (Registers.PWR_WELL_CTL_DRIVER, Ctl2);
150 Registers.Read (Registers.PWR_WELL_CTL_KVMR, Ctl3);
151 Registers.Read (Registers.PWR_WELL_CTL_DEBUG, Ctl4);
152 pragma Debug (Registers.Posting_Read (Registers.PWR_WELL_CTL5)); -- Result for debugging only
153 pragma Debug (Registers.Posting_Read (Registers.PWR_WELL_CTL6)); -- Result for debugging only
154
155 if ((Ctl1 or Ctl2 or Ctl3 or Ctl4) and
156 PWR_WELL_CTL_POWER_REQUEST (PD)) = 0
157 then
158 Registers.Wait_Unset_Mask
159 (Register => Registers.PWR_WELL_CTL_DRIVER,
160 Mask => PWR_WELL_CTL_POWER_STATE (PD));
161 end if;
162
163 if (Ctl2 and PWR_WELL_CTL_POWER_REQUEST (PD)) = 0 then
164 Registers.Set_Mask
165 (Register => Registers.PWR_WELL_CTL_DRIVER,
166 Mask => PWR_WELL_CTL_POWER_REQUEST (PD));
167 Registers.Wait_Set_Mask
168 (Register => Registers.PWR_WELL_CTL_DRIVER,
169 Mask => PWR_WELL_CTL_POWER_STATE (PD));
170
171 if PD in Power_Well then
172 Registers.Wait_Set_Mask
173 (Register => Registers.FUSE_STATUS,
174 Mask => FUSE_STATUS_PGx_DIST_STATUS (PD));
175 end if;
176 end if;
177 end PD_On;
178
Nico Huber99f10f32016-11-20 00:34:05 +0100179 function Need_PD (PD : Dynamic_Domain; Configs : Pipe_Configs) return Boolean
Nico Huber83693c82016-10-08 22:17:55 +0200180 is
181 begin
182 return (case PD is
Nico Huber8beafd72020-01-07 14:59:44 +0100183 when DDI_AE => Configs (Primary).Port = eDP or
184 Configs (Secondary).Port = eDP or
185 Configs (Tertiary).Port = eDP,
Nico Huber0d454cd2016-11-21 13:33:43 +0100186 when DDI_B => Configs (Primary).Port = HDMI1 or
Nico Huber83693c82016-10-08 22:17:55 +0200187 Configs (Primary).Port = DP1 or
Nico Huber0d454cd2016-11-21 13:33:43 +0100188 Configs (Secondary).Port = HDMI1 or
Nico Huber83693c82016-10-08 22:17:55 +0200189 Configs (Secondary).Port = DP1 or
Nico Huber0d454cd2016-11-21 13:33:43 +0100190 Configs (Tertiary).Port = HDMI1 or
Nico Huber83693c82016-10-08 22:17:55 +0200191 Configs (Tertiary).Port = DP1,
Nico Huber0d454cd2016-11-21 13:33:43 +0100192 when DDI_C => Configs (Primary).Port = HDMI2 or
Nico Huber83693c82016-10-08 22:17:55 +0200193 Configs (Primary).Port = DP2 or
Nico Huber0d454cd2016-11-21 13:33:43 +0100194 Configs (Secondary).Port = HDMI2 or
Nico Huber83693c82016-10-08 22:17:55 +0200195 Configs (Secondary).Port = DP2 or
Nico Huber0d454cd2016-11-21 13:33:43 +0100196 Configs (Tertiary).Port = HDMI2 or
Nico Huber83693c82016-10-08 22:17:55 +0200197 Configs (Tertiary).Port = DP2,
Nico Huber0d454cd2016-11-21 13:33:43 +0100198 when DDI_D => Configs (Primary).Port = HDMI3 or
Nico Huber83693c82016-10-08 22:17:55 +0200199 Configs (Primary).Port = DP3 or
Nico Huber0d454cd2016-11-21 13:33:43 +0100200 Configs (Secondary).Port = HDMI3 or
Nico Huber83693c82016-10-08 22:17:55 +0200201 Configs (Secondary).Port = DP3 or
Nico Huber0d454cd2016-11-21 13:33:43 +0100202 Configs (Tertiary).Port = HDMI3 or
Nico Huber83693c82016-10-08 22:17:55 +0200203 Configs (Tertiary).Port = DP3,
204 when PW2 => (Configs (Primary).Port /= Disabled and
Nico Huber8beafd72020-01-07 14:59:44 +0100205 Configs (Primary).Port /= eDP) or
Nico Huber83693c82016-10-08 22:17:55 +0200206 Configs (Secondary).Port /= Disabled or
207 Configs (Tertiary).Port /= Disabled);
208 end Need_PD;
209
210 ----------------------------------------------------------------------------
211
212 procedure Pre_All_Off is
213 begin
Angel Pons3f86b0b2020-07-18 00:22:32 +0200214 Transcoder.PSR_Off;
Nico Huber83693c82016-10-08 22:17:55 +0200215 end Pre_All_Off;
216
217 procedure Post_All_Off is
218 begin
219 for PD in reverse Dynamic_Domain loop
220 PD_Off (PD);
221 end loop;
222
223 Registers.Unset_Mask
224 (Register => Registers.DBUF_CTL,
225 Mask => DBUF_CTL_DBUF_POWER_REQUEST);
226 Registers.Wait_Unset_Mask
227 (Register => Registers.DBUF_CTL,
228 Mask => DBUF_CTL_DBUF_POWER_STATE);
229
230 Registers.Unset_Mask
231 (Register => Registers.LCPLL1_CTL,
232 Mask => LCPLL1_CTL_PLL_ENABLE);
233 Registers.Wait_Unset_Mask
234 (Register => Registers.LCPLL1_CTL,
235 Mask => LCPLL1_CTL_PLL_LOCK);
236
237 PD_Off (MISC_IO);
238 PD_Off (PW1);
239 end Post_All_Off;
240
Nico Huber6b4678d2019-09-22 21:31:52 +0200241 function Normalize_CDClk (CDClk : in Int64) return Config.CDClk_Range is
242 ( if CDClk <= 337_500_000 then 337_500_000
243 elsif CDClk <= 450_000_000 then 450_000_000
244 elsif CDClk <= 540_000_000 then 540_000_000
245 else 675_000_000);
246
247 procedure Get_Cur_CDClk (CDClk : out Config.CDClk_Range)
Nico Huber83693c82016-10-08 22:17:55 +0200248 is
Nico Huber6b4678d2019-09-22 21:31:52 +0200249 CDCLK_CTL : Word32;
250 begin
251 Registers.Read (Registers.CDCLK_CTL, CDCLK_CTL);
252 CDCLK_CTL := CDCLK_CTL and CDCLK_CTL_CD_FREQ_DECIMAL_MASK;
253 CDClk := Normalize_CDClk (Int64 (CDCLK_CTL) * 500_000 + 1_000_000);
254 end Get_Cur_CDClk;
255
256 procedure Get_Max_CDClk (CDClk : out Config.CDClk_Range)
257 is
258 DFSM : Word32;
259 begin
260 Registers.Read (Registers.DFSM, DFSM);
261 CDClk :=
262 (case DFSM and DFSM_DISPLAY_CDCLK_LIMIT_MASK is
263 when DFSM_DISPLAY_CDCLK_LIMIT_675MHZ => 675_000_000,
264 when DFSM_DISPLAY_CDCLK_LIMIT_540MHZ => 540_000_000,
265 when DFSM_DISPLAY_CDCLK_LIMIT_450MHZ => 450_000_000,
266 when others => 337_500_000);
267 end Get_Max_CDClk;
268
269 procedure Set_CDClk (CDClk_In : Frequency_Type)
270 is
271 CDClk : constant Config.CDClk_Range :=
272 Normalize_CDClk (Frequency_Type'Min (CDClk_In, Config.Max_CDClk));
Nico Huber312433c2019-09-28 03:15:48 +0200273 Success : Boolean;
Nico Huber83693c82016-10-08 22:17:55 +0200274 begin
Nico Huber6b4678d2019-09-22 21:31:52 +0200275 PCode.Mailbox_Request
276 (MBox => SKL_PCODE_CDCLK_CONTROL,
277 Command => SKL_CDCLK_PREPARE_FOR_CHANGE,
278 Reply_Mask => SKL_CDCLK_READY_FOR_CHANGE,
279 Wait_Ready => True,
280 Success => Success);
281
282 if not Success then
283 pragma Debug (Debug.Put_Line
284 ("ERROR: PCODE not ready for frequency change."));
285 return;
286 end if;
287
288 Registers.Write
289 (Register => Registers.CDCLK_CTL,
290 Value => (case CDClk is
291 when 675_000_000 => CDCLK_CTL_CD_FREQ_SELECT_675MHZ,
292 when 540_000_000 => CDCLK_CTL_CD_FREQ_SELECT_540MHZ,
293 when 450_000_000 => CDCLK_CTL_CD_FREQ_SELECT_450MHZ,
294 when others => CDCLK_CTL_CD_FREQ_SELECT_337_5MHZ)
295 or CDCLK_CTL_CD_FREQ_DECIMAL (CDClk));
296
297 PCode.Mailbox_Write
298 (MBox => SKL_PCODE_CDCLK_CONTROL,
299 Command => (case CDClk is
300 when 675_000_000 => 3,
301 when 540_000_000 => 2,
302 when 450_000_000 => 1,
303 when others => 0));
304 Registers.Set_Mask
305 (Register => Registers.DBUF_CTL,
306 Mask => DBUF_CTL_DBUF_POWER_REQUEST);
307 Registers.Wait_Set_Mask
308 (Register => Registers.DBUF_CTL,
309 Mask => DBUF_CTL_DBUF_POWER_STATE);
310
311 Config.CDClk := CDClk;
312 end Set_CDClk;
313
Nico Huberdde06302020-12-20 02:18:30 +0100314 procedure Get_Raw_Clock (Raw_Clock : out Frequency_Type)
315 is
316 Freq_24MHz : Boolean;
317 begin
318 Raw_Clock := Config.Default_RawClk_Freq;
319 if Config.Has_Fractional_RawClk then
320 Registers.Is_Set_Mask
321 (Register => Registers.SFUSE_STRAP,
322 Mask => SFUSE_STRAP_RAW_FREQUENCY,
323 Result => Freq_24MHz);
324 if not Freq_24MHz then
325 Raw_Clock := 19_200_000;
326 end if;
327 end if;
328 end Get_Raw_Clock;
329
Nico Huber6b4678d2019-09-22 21:31:52 +0200330 procedure Initialize is
331 begin
Nico Huber83693c82016-10-08 22:17:55 +0200332 Registers.Set_Mask
333 (Register => Registers.NDE_RSTWRN_OPT,
334 Mask => NDE_RSTWRN_OPT_RST_PCH_Handshake_En);
335
336 Registers.Wait_Set_Mask
337 (Register => Registers.FUSE_STATUS,
338 Mask => FUSE_STATUS_PG0_DIST_STATUS);
339 PD_On (PW1);
340 PD_On (MISC_IO);
341
Nico Huber83693c82016-10-08 22:17:55 +0200342 -- TODO: Set to preferred eDP rate:
343 -- Registers.Unset_And_Set_Mask
344 -- (Register => Registers.DPLL_CTRL1,
345 -- Unset_Mask => DPLL_CTRL1_DPLL0_LINK_RATE_MASK,
346 -- Set_Mask => DPLL_CTRL1_DPLL0_LINK_RATE_...);
347 Registers.Set_Mask
348 (Register => Registers.LCPLL1_CTL,
349 Mask => LCPLL1_CTL_PLL_ENABLE);
350 Registers.Wait_Set_Mask
351 (Register => Registers.LCPLL1_CTL,
352 Mask => LCPLL1_CTL_PLL_LOCK);
353
Nico Huber6b4678d2019-09-22 21:31:52 +0200354 Get_Cur_CDClk (Config.CDClk);
355 Get_Max_CDClk (Config.Max_CDClk);
356 Set_CDClk (Config.Default_CDClk_Freq);
Arthur Heymansd1988d12018-03-28 16:27:57 +0200357
Nico Huberdde06302020-12-20 02:18:30 +0100358 Get_Raw_Clock (Config.Raw_Clock);
Nico Huber83693c82016-10-08 22:17:55 +0200359 end Initialize;
360
Nico Huber6b4678d2019-09-22 21:31:52 +0200361 procedure Limit_Dotclocks
362 (Configs : in out Pipe_Configs;
363 CDClk_Switch : out Boolean)
364 is
365 begin
366 Config_Helpers.Limit_Dotclocks (Configs, Config.Max_CDClk);
367 CDClk_Switch :=
368 Config.CDClk /= Normalize_CDClk
369 (Config_Helpers.Highest_Dotclock (Configs));
370 end Limit_Dotclocks;
371
372 procedure Update_CDClk (Configs : in out Pipe_Configs)
373 is
374 New_CDClk : constant Frequency_Type :=
375 Config_Helpers.Highest_Dotclock (Configs);
376 begin
377 Set_CDClk (New_CDClk);
378 Config_Helpers.Limit_Dotclocks (Configs, Config.CDClk);
379 end Update_CDClk;
380
Nico Huber99f10f32016-11-20 00:34:05 +0100381 procedure Power_Set_To (Configs : Pipe_Configs) is
Nico Huber83693c82016-10-08 22:17:55 +0200382 begin
383 for PD in reverse Dynamic_Domain loop
384 if not Need_PD (PD, Configs) then
385 PD_Off (PD);
386 end if;
387 end loop;
388 for PD in Dynamic_Domain loop
389 if Need_PD (PD, Configs) then
390 PD_On (PD);
391 end if;
392 end loop;
393 end Power_Set_To;
394
Nico Huber99f10f32016-11-20 00:34:05 +0100395 procedure Power_Up (Old_Configs, New_Configs : Pipe_Configs) is
Nico Huber83693c82016-10-08 22:17:55 +0200396 begin
397 for PD in Dynamic_Domain loop
398 if not Need_PD (PD, Old_Configs) and Need_PD (PD, New_Configs) then
399 PD_On (PD);
400 end if;
401 end loop;
402 end Power_Up;
403
Nico Huber99f10f32016-11-20 00:34:05 +0100404 procedure Power_Down (Old_Configs, Tmp_Configs, New_Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200405 is
406 begin
407 for PD in reverse Dynamic_Domain loop
408 if (Need_PD (PD, Old_Configs) or Need_PD (PD, Tmp_Configs)) and
409 not Need_PD (PD, New_Configs)
410 then
411 PD_Off (PD);
412 end if;
413 end loop;
414 end Power_Down;
415
Angel Pons3318bf22020-07-19 18:38:32 +0200416end HW.GFX.GMA.Power_And_Clocks;