2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
25 #include <subdev/bios.h>
26 #include <subdev/bios/pll.h>
27 #include <subdev/timer.h>
33 struct nva3_clock_priv {
34 struct nouveau_clock base;
35 struct nva3_clock_info eng[nv_clk_src_max];
38 static u32 read_clk(struct nva3_clock_priv *, int, bool);
39 static u32 read_pll(struct nva3_clock_priv *, int, u32);
42 read_vco(struct nva3_clock_priv *priv, int clk)
44 u32 sctl = nv_rd32(priv, 0x4120 + (clk * 4));
45 if ((sctl & 0x00000030) != 0x00000030)
46 return read_pll(priv, 0x41, 0x00e820);
47 return read_pll(priv, 0x42, 0x00e8a0);
51 read_clk(struct nva3_clock_priv *priv, int clk, bool ignore_en)
55 /* refclk for the 0xe8xx plls is a fixed frequency */
57 if (nv_device(priv)->chipset == 0xaf) {
58 /* no joke.. seriously.. sigh.. */
59 return nv_rd32(priv, 0x00471c) * 1000;
62 return nv_device(priv)->crystal;
65 sctl = nv_rd32(priv, 0x4120 + (clk * 4));
66 if (!ignore_en && !(sctl & 0x00000100))
69 switch (sctl & 0x00003000) {
71 return nv_device(priv)->crystal;
73 if (sctl & 0x00000040)
77 sclk = read_vco(priv, clk);
78 sdiv = ((sctl & 0x003f0000) >> 16) + 2;
79 return (sclk * 2) / sdiv;
86 read_pll(struct nva3_clock_priv *priv, int clk, u32 pll)
88 u32 ctrl = nv_rd32(priv, pll + 0);
89 u32 sclk = 0, P = 1, N = 1, M = 1;
91 if (!(ctrl & 0x00000008)) {
92 if (ctrl & 0x00000001) {
93 u32 coef = nv_rd32(priv, pll + 4);
94 M = (coef & 0x000000ff) >> 0;
95 N = (coef & 0x0000ff00) >> 8;
96 P = (coef & 0x003f0000) >> 16;
98 /* no post-divider on these.. */
99 if ((pll & 0x00ff00) == 0x00e800)
102 sclk = read_clk(priv, 0x00 + clk, false);
105 sclk = read_clk(priv, 0x10 + clk, false);
109 return sclk * N / (M * P);
114 nva3_clock_read(struct nouveau_clock *clk, enum nv_clk_src src)
116 struct nva3_clock_priv *priv = (void *)clk;
119 case nv_clk_src_crystal:
120 return nv_device(priv)->crystal;
121 case nv_clk_src_href:
123 case nv_clk_src_core:
124 return read_pll(priv, 0x00, 0x4200);
125 case nv_clk_src_shader:
126 return read_pll(priv, 0x01, 0x4220);
128 return read_pll(priv, 0x02, 0x4000);
129 case nv_clk_src_disp:
130 return read_clk(priv, 0x20, false);
131 case nv_clk_src_vdec:
132 return read_clk(priv, 0x21, false);
133 case nv_clk_src_daemon:
134 return read_clk(priv, 0x25, false);
136 nv_error(clk, "invalid clock source %d\n", src);
142 nva3_clock_info(struct nouveau_clock *clock, int clk, u32 pll, u32 khz,
143 struct nva3_clock_info *info)
145 struct nouveau_bios *bios = nouveau_bios(clock);
146 struct nva3_clock_priv *priv = (void *)clock;
147 struct nvbios_pll limits;
148 u32 oclk, sclk, sdiv;
157 info->clk = 0x00000100;
160 info->clk = 0x00002100;
163 info->clk = 0x00002140;
166 sclk = read_vco(priv, clk);
167 sdiv = min((sclk * 2) / (khz - 2999), (u32)65);
168 /* if the clock has a PLL attached, and we can get a within
169 * [-2, 3) MHz of a divider, we'll disable the PLL and use
170 * the divider instead.
172 * divider can go as low as 2, limited here because NVIDIA
173 * and the VBIOS on my NVA8 seem to prefer using the PLL
174 * for 810MHz - is there a good reason?
177 oclk = (sclk * 2) / sdiv;
179 if (!pll || (diff >= -2000 && diff < 3000)) {
180 info->clk = (((sdiv - 2) << 16) | 0x00003100);
190 ret = nvbios_pll_parse(bios, pll, &limits);
194 limits.refclk = read_clk(priv, clk - 0x10, true);
198 ret = nva3_pll_calc(nv_subdev(priv), &limits, khz, &N, NULL, &M, &P);
200 info->clk = nv_rd32(priv, 0x4120 + (clk * 4));
201 info->pll = (P << 16) | (N << 8) | M;
204 return ret ? ret : -ERANGE;
208 calc_clk(struct nva3_clock_priv *priv, struct nouveau_cstate *cstate,
209 int clk, u32 pll, int idx)
211 int ret = nva3_clock_info(&priv->base, clk, pll, cstate->domain[idx],
219 prog_pll(struct nva3_clock_priv *priv, int clk, u32 pll, int idx)
221 struct nva3_clock_info *info = &priv->eng[idx];
222 const u32 src0 = 0x004120 + (clk * 4);
223 const u32 src1 = 0x004160 + (clk * 4);
224 const u32 ctrl = pll + 0;
225 const u32 coef = pll + 4;
228 nv_mask(priv, src0, 0x00000101, 0x00000101);
229 nv_wr32(priv, coef, info->pll);
230 nv_mask(priv, ctrl, 0x00000015, 0x00000015);
231 nv_mask(priv, ctrl, 0x00000010, 0x00000000);
232 nv_wait(priv, ctrl, 0x00020000, 0x00020000);
233 nv_mask(priv, ctrl, 0x00000010, 0x00000010);
234 nv_mask(priv, ctrl, 0x00000008, 0x00000000);
235 nv_mask(priv, src1, 0x00000100, 0x00000000);
236 nv_mask(priv, src1, 0x00000001, 0x00000000);
238 nv_mask(priv, src1, 0x003f3141, 0x00000101 | info->clk);
239 nv_mask(priv, ctrl, 0x00000018, 0x00000018);
241 nv_mask(priv, ctrl, 0x00000001, 0x00000000);
242 nv_mask(priv, src0, 0x00000100, 0x00000000);
243 nv_mask(priv, src0, 0x00000001, 0x00000000);
248 prog_clk(struct nva3_clock_priv *priv, int clk, int idx)
250 struct nva3_clock_info *info = &priv->eng[idx];
251 nv_mask(priv, 0x004120 + (clk * 4), 0x003f3141, 0x00000101 | info->clk);
255 nva3_clock_calc(struct nouveau_clock *clk, struct nouveau_cstate *cstate)
257 struct nva3_clock_priv *priv = (void *)clk;
260 if ((ret = calc_clk(priv, cstate, 0x10, 0x4200, nv_clk_src_core)) ||
261 (ret = calc_clk(priv, cstate, 0x11, 0x4220, nv_clk_src_shader)) ||
262 (ret = calc_clk(priv, cstate, 0x20, 0x0000, nv_clk_src_disp)) ||
263 (ret = calc_clk(priv, cstate, 0x21, 0x0000, nv_clk_src_vdec)))
270 nva3_clock_prog(struct nouveau_clock *clk)
272 struct nva3_clock_priv *priv = (void *)clk;
273 prog_pll(priv, 0x00, 0x004200, nv_clk_src_core);
274 prog_pll(priv, 0x01, 0x004220, nv_clk_src_shader);
275 prog_clk(priv, 0x20, nv_clk_src_disp);
276 prog_clk(priv, 0x21, nv_clk_src_vdec);
281 nva3_clock_tidy(struct nouveau_clock *clk)
285 static struct nouveau_clocks
287 { nv_clk_src_crystal, 0xff },
288 { nv_clk_src_href , 0xff },
289 { nv_clk_src_core , 0x00, 0, "core", 1000 },
290 { nv_clk_src_shader , 0x01, 0, "shader", 1000 },
291 { nv_clk_src_mem , 0x02, 0, "memory", 1000 },
292 { nv_clk_src_vdec , 0x03 },
293 { nv_clk_src_disp , 0x04 },
298 nva3_clock_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
299 struct nouveau_oclass *oclass, void *data, u32 size,
300 struct nouveau_object **pobject)
302 struct nva3_clock_priv *priv;
305 ret = nouveau_clock_create(parent, engine, oclass, nva3_domain, false,
307 *pobject = nv_object(priv);
311 priv->base.read = nva3_clock_read;
312 priv->base.calc = nva3_clock_calc;
313 priv->base.prog = nva3_clock_prog;
314 priv->base.tidy = nva3_clock_tidy;
318 struct nouveau_oclass
319 nva3_clock_oclass = {
320 .handle = NV_SUBDEV(CLOCK, 0xa3),
321 .ofuncs = &(struct nouveau_ofuncs) {
322 .ctor = nva3_clock_ctor,
323 .dtor = _nouveau_clock_dtor,
324 .init = _nouveau_clock_init,
325 .fini = _nouveau_clock_fini,