blob: 2a9fabdf1b75683cf9f3db5eaab83dd531a3163c [file] [log] [blame]
Ben Skeggs02c30ca2010-09-16 16:17:35 +10001/*
2 * Copyright 2010 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Ben Skeggs
23 */
24
25#include "drmP.h"
26#include "nouveau_drv.h"
Ben Skeggsaee582d2010-09-27 10:13:23 +100027#include "nouveau_bios.h"
Ben Skeggs02c30ca2010-09-16 16:17:35 +100028#include "nouveau_pm.h"
29
30/*XXX: boards using limits 0x40 need fixing, the register layout
31 * is correct here, but, there's some other funny magic
32 * that modifies things, so it's not likely we'll set/read
33 * the correct timings yet.. working on it...
34 */
35
36struct nv50_pm_state {
Ben Skeggsaee582d2010-09-27 10:13:23 +100037 struct nouveau_pm_level *perflvl;
Ben Skeggs02c30ca2010-09-16 16:17:35 +100038 struct pll_lims pll;
39 enum pll_types type;
40 int N, M, P;
41};
42
43int
44nv50_pm_clock_get(struct drm_device *dev, u32 id)
45{
46 struct pll_lims pll;
47 int P, N, M, ret;
48 u32 reg0, reg1;
49
50 ret = get_pll_limits(dev, id, &pll);
51 if (ret)
52 return ret;
53
54 if (pll.vco2.maxfreq) {
55 reg0 = nv_rd32(dev, pll.reg + 0);
56 reg1 = nv_rd32(dev, pll.reg + 4);
57 P = (reg0 & 0x00070000) >> 16;
58 N = (reg1 & 0x0000ff00) >> 8;
59 M = (reg1 & 0x000000ff);
60
61 return ((pll.refclk * N / M) >> P);
62 }
63
64 reg0 = nv_rd32(dev, pll.reg + 4);
65 P = (reg0 & 0x003f0000) >> 16;
66 N = (reg0 & 0x0000ff00) >> 8;
67 M = (reg0 & 0x000000ff);
68 return pll.refclk * N / M / P;
69}
70
71void *
Ben Skeggs5c6dc652010-09-27 09:47:56 +100072nv50_pm_clock_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl,
73 u32 id, int khz)
Ben Skeggs02c30ca2010-09-16 16:17:35 +100074{
75 struct nv50_pm_state *state;
76 int dummy, ret;
77
78 state = kzalloc(sizeof(*state), GFP_KERNEL);
79 if (!state)
80 return ERR_PTR(-ENOMEM);
81 state->type = id;
Ben Skeggsaee582d2010-09-27 10:13:23 +100082 state->perflvl = perflvl;
Ben Skeggs02c30ca2010-09-16 16:17:35 +100083
84 ret = get_pll_limits(dev, id, &state->pll);
85 if (ret < 0) {
86 kfree(state);
Ben Skeggs6f876982010-09-16 16:47:14 +100087 return (ret == -ENOENT) ? NULL : ERR_PTR(ret);
Ben Skeggs02c30ca2010-09-16 16:17:35 +100088 }
89
90 ret = nv50_calc_pll(dev, &state->pll, khz, &state->N, &state->M,
91 &dummy, &dummy, &state->P);
92 if (ret < 0) {
93 kfree(state);
94 return ERR_PTR(ret);
95 }
96
97 return state;
98}
99
100void
101nv50_pm_clock_set(struct drm_device *dev, void *pre_state)
102{
103 struct nv50_pm_state *state = pre_state;
Ben Skeggsaee582d2010-09-27 10:13:23 +1000104 struct nouveau_pm_level *perflvl = state->perflvl;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000105 u32 reg = state->pll.reg, tmp;
Ben Skeggsaee582d2010-09-27 10:13:23 +1000106 struct bit_entry BIT_M;
107 u16 script;
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000108 int N = state->N;
109 int M = state->M;
110 int P = state->P;
111
Ben Skeggsaee582d2010-09-27 10:13:23 +1000112 if (state->type == PLL_MEMORY && perflvl->memscript &&
113 bit_table(dev, 'M', &BIT_M) == 0 &&
114 BIT_M.version == 1 && BIT_M.length >= 0x0b) {
115 script = ROM16(BIT_M.data[0x05]);
116 if (script)
117 nouveau_bios_run_init_table(dev, script, NULL);
118 script = ROM16(BIT_M.data[0x07]);
119 if (script)
120 nouveau_bios_run_init_table(dev, script, NULL);
121 script = ROM16(BIT_M.data[0x09]);
122 if (script)
123 nouveau_bios_run_init_table(dev, script, NULL);
124
125 nouveau_bios_run_init_table(dev, perflvl->memscript, NULL);
126 }
127
Ben Skeggs02c30ca2010-09-16 16:17:35 +1000128 if (state->pll.vco2.maxfreq) {
129 if (state->type == PLL_MEMORY) {
130 nv_wr32(dev, 0x100210, 0);
131 nv_wr32(dev, 0x1002dc, 1);
132 }
133
134 tmp = nv_rd32(dev, reg + 0) & 0xfff8ffff;
135 tmp |= 0x80000000 | (P << 16);
136 nv_wr32(dev, reg + 0, tmp);
137 nv_wr32(dev, reg + 4, (N << 8) | M);
138
139 if (state->type == PLL_MEMORY) {
140 nv_wr32(dev, 0x1002dc, 0);
141 nv_wr32(dev, 0x100210, 0x80000000);
142 }
143 } else {
144 nv_wr32(dev, reg + 4, (P << 16) | (N << 8) | M);
145 }
146
147 kfree(state);
148}
149