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.
24 #define gf100_clk(p) container_of((p), struct gf100_clk, base)
28 #include <subdev/bios.h>
29 #include <subdev/bios/pll.h>
30 #include <subdev/timer.h>
32 struct gf100_clk_info
{
43 struct gf100_clk_info eng
[16];
46 static u32
read_div(struct gf100_clk
*, int, u32
, u32
);
49 read_vco(struct gf100_clk
*clk
, u32 dsrc
)
51 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
52 u32 ssrc
= nvkm_rd32(device
, dsrc
);
53 if (!(ssrc
& 0x00000100))
54 return nvkm_clk_read(&clk
->base
, nv_clk_src_sppll0
);
55 return nvkm_clk_read(&clk
->base
, nv_clk_src_sppll1
);
59 read_pll(struct gf100_clk
*clk
, u32 pll
)
61 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
62 u32 ctrl
= nvkm_rd32(device
, pll
+ 0x00);
63 u32 coef
= nvkm_rd32(device
, pll
+ 0x04);
64 u32 P
= (coef
& 0x003f0000) >> 16;
65 u32 N
= (coef
& 0x0000ff00) >> 8;
66 u32 M
= (coef
& 0x000000ff) >> 0;
69 if (!(ctrl
& 0x00000001))
75 sclk
= device
->crystal
;
79 sclk
= nvkm_clk_read(&clk
->base
, nv_clk_src_mpllsrc
);
82 sclk
= nvkm_clk_read(&clk
->base
, nv_clk_src_mpllsrcref
);
88 sclk
= read_div(clk
, (pll
& 0xff) / 0x20, 0x137120, 0x137140);
94 return sclk
* N
/ M
/ P
;
98 read_div(struct gf100_clk
*clk
, int doff
, u32 dsrc
, u32 dctl
)
100 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
101 u32 ssrc
= nvkm_rd32(device
, dsrc
+ (doff
* 4));
102 u32 sclk
, sctl
, sdiv
= 2;
104 switch (ssrc
& 0x00000003) {
106 if ((ssrc
& 0x00030000) != 0x00030000)
107 return device
->crystal
;
112 sclk
= read_vco(clk
, dsrc
+ (doff
* 4));
114 /* Memclk has doff of 0 despite its alt. location */
116 sctl
= nvkm_rd32(device
, dctl
+ (doff
* 4));
118 if (sctl
& 0x80000000) {
122 sdiv
= (sctl
& 0x3f) + 2;
126 return (sclk
* 2) / sdiv
;
133 read_clk(struct gf100_clk
*clk
, int idx
)
135 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
136 u32 sctl
= nvkm_rd32(device
, 0x137250 + (idx
* 4));
137 u32 ssel
= nvkm_rd32(device
, 0x137100);
140 if (ssel
& (1 << idx
)) {
142 sclk
= read_pll(clk
, 0x137000 + (idx
* 0x20));
144 sclk
= read_pll(clk
, 0x1370e0);
145 sdiv
= ((sctl
& 0x00003f00) >> 8) + 2;
147 sclk
= read_div(clk
, idx
, 0x137160, 0x1371d0);
148 sdiv
= ((sctl
& 0x0000003f) >> 0) + 2;
151 if (sctl
& 0x80000000)
152 return (sclk
* 2) / sdiv
;
158 gf100_clk_read(struct nvkm_clk
*base
, enum nv_clk_src src
)
160 struct gf100_clk
*clk
= gf100_clk(base
);
161 struct nvkm_subdev
*subdev
= &clk
->base
.subdev
;
162 struct nvkm_device
*device
= subdev
->device
;
165 case nv_clk_src_crystal
:
166 return device
->crystal
;
167 case nv_clk_src_href
:
169 case nv_clk_src_sppll0
:
170 return read_pll(clk
, 0x00e800);
171 case nv_clk_src_sppll1
:
172 return read_pll(clk
, 0x00e820);
174 case nv_clk_src_mpllsrcref
:
175 return read_div(clk
, 0, 0x137320, 0x137330);
176 case nv_clk_src_mpllsrc
:
177 return read_pll(clk
, 0x132020);
178 case nv_clk_src_mpll
:
179 return read_pll(clk
, 0x132000);
180 case nv_clk_src_mdiv
:
181 return read_div(clk
, 0, 0x137300, 0x137310);
183 if (nvkm_rd32(device
, 0x1373f0) & 0x00000002)
184 return nvkm_clk_read(&clk
->base
, nv_clk_src_mpll
);
185 return nvkm_clk_read(&clk
->base
, nv_clk_src_mdiv
);
188 return read_clk(clk
, 0x00);
190 return read_clk(clk
, 0x01);
191 case nv_clk_src_hubk07
:
192 return read_clk(clk
, 0x02);
193 case nv_clk_src_hubk06
:
194 return read_clk(clk
, 0x07);
195 case nv_clk_src_hubk01
:
196 return read_clk(clk
, 0x08);
197 case nv_clk_src_copy
:
198 return read_clk(clk
, 0x09);
200 return read_clk(clk
, 0x0c);
201 case nv_clk_src_vdec
:
202 return read_clk(clk
, 0x0e);
204 nvkm_error(subdev
, "invalid clock source %d\n", src
);
210 calc_div(struct gf100_clk
*clk
, int idx
, u32 ref
, u32 freq
, u32
*ddiv
)
212 u32 div
= min((ref
* 2) / freq
, (u32
)65);
217 return (ref
* 2) / div
;
221 calc_src(struct gf100_clk
*clk
, int idx
, u32 freq
, u32
*dsrc
, u32
*ddiv
)
225 /* use one of the fixed frequencies if possible */
242 /* otherwise, calculate the closest divider */
243 sclk
= read_vco(clk
, 0x137160 + (idx
* 4));
245 sclk
= calc_div(clk
, idx
, sclk
, freq
, ddiv
);
250 calc_pll(struct gf100_clk
*clk
, int idx
, u32 freq
, u32
*coef
)
252 struct nvkm_subdev
*subdev
= &clk
->base
.subdev
;
253 struct nvkm_bios
*bios
= subdev
->device
->bios
;
254 struct nvbios_pll limits
;
257 ret
= nvbios_pll_parse(bios
, 0x137000 + (idx
* 0x20), &limits
);
261 limits
.refclk
= read_div(clk
, idx
, 0x137120, 0x137140);
265 ret
= gt215_pll_calc(subdev
, &limits
, freq
, &N
, NULL
, &M
, &P
);
269 *coef
= (P
<< 16) | (N
<< 8) | M
;
274 calc_clk(struct gf100_clk
*clk
, struct nvkm_cstate
*cstate
, int idx
, int dom
)
276 struct gf100_clk_info
*info
= &clk
->eng
[idx
];
277 u32 freq
= cstate
->domain
[dom
];
278 u32 src0
, div0
, div1D
, div1P
= 0;
281 /* invalid clock domain */
285 /* first possible path, using only dividers */
286 clk0
= calc_src(clk
, idx
, freq
, &src0
, &div0
);
287 clk0
= calc_div(clk
, idx
, clk0
, freq
, &div1D
);
289 /* see if we can get any closer using PLLs */
290 if (clk0
!= freq
&& (0x00004387 & (1 << idx
))) {
292 clk1
= calc_pll(clk
, idx
, freq
, &info
->coef
);
294 clk1
= cstate
->domain
[nv_clk_src_hubk06
];
295 clk1
= calc_div(clk
, idx
, clk1
, freq
, &div1P
);
298 /* select the method which gets closest to target freq */
299 if (abs((int)freq
- clk0
) <= abs((int)freq
- clk1
)) {
302 info
->ddiv
|= 0x80000000;
303 info
->ddiv
|= div0
<< 8;
307 info
->mdiv
|= 0x80000000;
310 info
->ssel
= info
->coef
= 0;
314 info
->mdiv
|= 0x80000000;
315 info
->mdiv
|= div1P
<< 8;
317 info
->ssel
= (1 << idx
);
325 gf100_clk_calc(struct nvkm_clk
*base
, struct nvkm_cstate
*cstate
)
327 struct gf100_clk
*clk
= gf100_clk(base
);
330 if ((ret
= calc_clk(clk
, cstate
, 0x00, nv_clk_src_gpc
)) ||
331 (ret
= calc_clk(clk
, cstate
, 0x01, nv_clk_src_rop
)) ||
332 (ret
= calc_clk(clk
, cstate
, 0x02, nv_clk_src_hubk07
)) ||
333 (ret
= calc_clk(clk
, cstate
, 0x07, nv_clk_src_hubk06
)) ||
334 (ret
= calc_clk(clk
, cstate
, 0x08, nv_clk_src_hubk01
)) ||
335 (ret
= calc_clk(clk
, cstate
, 0x09, nv_clk_src_copy
)) ||
336 (ret
= calc_clk(clk
, cstate
, 0x0c, nv_clk_src_pmu
)) ||
337 (ret
= calc_clk(clk
, cstate
, 0x0e, nv_clk_src_vdec
)))
344 gf100_clk_prog_0(struct gf100_clk
*clk
, int idx
)
346 struct gf100_clk_info
*info
= &clk
->eng
[idx
];
347 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
348 if (idx
< 7 && !info
->ssel
) {
349 nvkm_mask(device
, 0x1371d0 + (idx
* 0x04), 0x80003f3f, info
->ddiv
);
350 nvkm_wr32(device
, 0x137160 + (idx
* 0x04), info
->dsrc
);
355 gf100_clk_prog_1(struct gf100_clk
*clk
, int idx
)
357 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
358 nvkm_mask(device
, 0x137100, (1 << idx
), 0x00000000);
359 nvkm_msec(device
, 2000,
360 if (!(nvkm_rd32(device
, 0x137100) & (1 << idx
)))
366 gf100_clk_prog_2(struct gf100_clk
*clk
, int idx
)
368 struct gf100_clk_info
*info
= &clk
->eng
[idx
];
369 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
370 const u32 addr
= 0x137000 + (idx
* 0x20);
372 nvkm_mask(device
, addr
+ 0x00, 0x00000004, 0x00000000);
373 nvkm_mask(device
, addr
+ 0x00, 0x00000001, 0x00000000);
375 nvkm_wr32(device
, addr
+ 0x04, info
->coef
);
376 nvkm_mask(device
, addr
+ 0x00, 0x00000001, 0x00000001);
379 nvkm_mask(device
, addr
+ 0x00, 0x00000010, 0x00000000);
380 nvkm_msec(device
, 2000,
381 if (nvkm_rd32(device
, addr
+ 0x00) & 0x00020000)
384 nvkm_mask(device
, addr
+ 0x00, 0x00000010, 0x00000010);
386 /* Enable sync mode */
387 nvkm_mask(device
, addr
+ 0x00, 0x00000004, 0x00000004);
393 gf100_clk_prog_3(struct gf100_clk
*clk
, int idx
)
395 struct gf100_clk_info
*info
= &clk
->eng
[idx
];
396 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
398 nvkm_mask(device
, 0x137100, (1 << idx
), info
->ssel
);
399 nvkm_msec(device
, 2000,
400 u32 tmp
= nvkm_rd32(device
, 0x137100) & (1 << idx
);
401 if (tmp
== info
->ssel
)
408 gf100_clk_prog_4(struct gf100_clk
*clk
, int idx
)
410 struct gf100_clk_info
*info
= &clk
->eng
[idx
];
411 struct nvkm_device
*device
= clk
->base
.subdev
.device
;
412 nvkm_mask(device
, 0x137250 + (idx
* 0x04), 0x00003f3f, info
->mdiv
);
416 gf100_clk_prog(struct nvkm_clk
*base
)
418 struct gf100_clk
*clk
= gf100_clk(base
);
420 void (*exec
)(struct gf100_clk
*, int);
422 { gf100_clk_prog_0
}, /* div programming */
423 { gf100_clk_prog_1
}, /* select div mode */
424 { gf100_clk_prog_2
}, /* (maybe) program pll */
425 { gf100_clk_prog_3
}, /* (maybe) select pll mode */
426 { gf100_clk_prog_4
}, /* final divider */
430 for (i
= 0; i
< ARRAY_SIZE(stage
); i
++) {
431 for (j
= 0; j
< ARRAY_SIZE(clk
->eng
); j
++) {
432 if (!clk
->eng
[j
].freq
)
434 stage
[i
].exec(clk
, j
);
442 gf100_clk_tidy(struct nvkm_clk
*base
)
444 struct gf100_clk
*clk
= gf100_clk(base
);
445 memset(clk
->eng
, 0x00, sizeof(clk
->eng
));
448 static const struct nvkm_clk_func
450 .read
= gf100_clk_read
,
451 .calc
= gf100_clk_calc
,
452 .prog
= gf100_clk_prog
,
453 .tidy
= gf100_clk_tidy
,
455 { nv_clk_src_crystal
, 0xff },
456 { nv_clk_src_href
, 0xff },
457 { nv_clk_src_hubk06
, 0x00 },
458 { nv_clk_src_hubk01
, 0x01 },
459 { nv_clk_src_copy
, 0x02 },
460 { nv_clk_src_gpc
, 0x03, NVKM_CLK_DOM_FLAG_VPSTATE
, "core", 2000 },
461 { nv_clk_src_rop
, 0x04 },
462 { nv_clk_src_mem
, 0x05, 0, "memory", 1000 },
463 { nv_clk_src_vdec
, 0x06 },
464 { nv_clk_src_pmu
, 0x0a },
465 { nv_clk_src_hubk07
, 0x0b },
471 gf100_clk_new(struct nvkm_device
*device
, int index
, struct nvkm_clk
**pclk
)
473 struct gf100_clk
*clk
;
475 if (!(clk
= kzalloc(sizeof(*clk
), GFP_KERNEL
)))
479 return nvkm_clk_ctor(&gf100_clk
, device
, index
, false, &clk
->base
);