drm/nva3: split pm backend out from nv50
[linux-2.6.git] / drivers / gpu / drm / nouveau / nv50_pm.c
blob7dbb305d7e63598056678c8e3e20b594c8c25a18
1 /*
2 * Copyright 2010 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.
22 * Authors: Ben Skeggs
25 #include "drmP.h"
26 #include "nouveau_drv.h"
27 #include "nouveau_bios.h"
28 #include "nouveau_pm.h"
30 struct nv50_pm_state {
31 struct nouveau_pm_level *perflvl;
32 struct pll_lims pll;
33 enum pll_types type;
34 int N, M, P;
37 int
38 nv50_pm_clock_get(struct drm_device *dev, u32 id)
40 struct pll_lims pll;
41 int P, N, M, ret;
42 u32 reg0, reg1;
44 ret = get_pll_limits(dev, id, &pll);
45 if (ret)
46 return ret;
48 reg0 = nv_rd32(dev, pll.reg + 0);
49 reg1 = nv_rd32(dev, pll.reg + 4);
50 P = (reg0 & 0x00070000) >> 16;
51 N = (reg1 & 0x0000ff00) >> 8;
52 M = (reg1 & 0x000000ff);
54 return ((pll.refclk * N / M) >> P);
57 void *
58 nv50_pm_clock_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl,
59 u32 id, int khz)
61 struct nv50_pm_state *state;
62 int dummy, ret;
64 state = kzalloc(sizeof(*state), GFP_KERNEL);
65 if (!state)
66 return ERR_PTR(-ENOMEM);
67 state->type = id;
68 state->perflvl = perflvl;
70 ret = get_pll_limits(dev, id, &state->pll);
71 if (ret < 0) {
72 kfree(state);
73 return (ret == -ENOENT) ? NULL : ERR_PTR(ret);
76 ret = nv50_calc_pll(dev, &state->pll, khz, &state->N, &state->M,
77 &dummy, &dummy, &state->P);
78 if (ret < 0) {
79 kfree(state);
80 return ERR_PTR(ret);
83 return state;
86 void
87 nv50_pm_clock_set(struct drm_device *dev, void *pre_state)
89 struct nv50_pm_state *state = pre_state;
90 struct nouveau_pm_level *perflvl = state->perflvl;
91 u32 reg = state->pll.reg, tmp;
92 struct bit_entry BIT_M;
93 u16 script;
94 int N = state->N;
95 int M = state->M;
96 int P = state->P;
98 if (state->type == PLL_MEMORY && perflvl->memscript &&
99 bit_table(dev, 'M', &BIT_M) == 0 &&
100 BIT_M.version == 1 && BIT_M.length >= 0x0b) {
101 script = ROM16(BIT_M.data[0x05]);
102 if (script)
103 nouveau_bios_run_init_table(dev, script, NULL);
104 script = ROM16(BIT_M.data[0x07]);
105 if (script)
106 nouveau_bios_run_init_table(dev, script, NULL);
107 script = ROM16(BIT_M.data[0x09]);
108 if (script)
109 nouveau_bios_run_init_table(dev, script, NULL);
111 nouveau_bios_run_init_table(dev, perflvl->memscript, NULL);
114 if (state->type == PLL_MEMORY) {
115 nv_wr32(dev, 0x100210, 0);
116 nv_wr32(dev, 0x1002dc, 1);
119 tmp = nv_rd32(dev, reg + 0) & 0xfff8ffff;
120 tmp |= 0x80000000 | (P << 16);
121 nv_wr32(dev, reg + 0, tmp);
122 nv_wr32(dev, reg + 4, (N << 8) | M);
124 if (state->type == PLL_MEMORY) {
125 nv_wr32(dev, 0x1002dc, 0);
126 nv_wr32(dev, 0x100210, 0x80000000);
129 kfree(state);