blob: 220c1a877ff551eef1b638932bdcdef09eff30ee [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3 * Copyright 2005 Stephane Marchesin
4 *
5 * The Weather Channel (TM) funded Tungsten Graphics to develop the
6 * initial release of the Radeon 8500 driver under the XFree86 license.
7 * This notice must be preserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
Roy Spliete6084252012-02-07 00:29:06 +010029 * Ben Skeggs <bskeggs@redhat.com>
30 * Roy Spliet <r.spliet@student.tudelft.nl>
Ben Skeggs6ee73862009-12-11 19:24:15 +100031 */
32
33
34#include "drmP.h"
35#include "drm.h"
36#include "drm_sarea.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100037
Francisco Jerezcbab95db2010-10-11 03:43:58 +020038#include "nouveau_drv.h"
39#include "nouveau_pm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100040
Ben Skeggsfd99fd62012-01-17 21:10:58 +100041static int
42nv40_mem_timing_calc(struct drm_device *dev, u32 freq,
43 struct nouveau_pm_tbl_entry *e, u8 len,
44 struct nouveau_pm_memtiming *boot,
45 struct nouveau_pm_memtiming *t)
Martin Peresddb20052011-12-17 12:24:59 +010046{
Roy Splietc7c039f2012-01-09 15:23:07 +100047 t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
Roy Spliet9a782482011-07-09 21:18:11 +020048
49 /* XXX: I don't trust the -1's and +1's... they must come
50 * from somewhere! */
Roy Splietc7c039f2012-01-09 15:23:07 +100051 t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
52 1 << 16 |
53 (e->tWTR + 2 + (t->tCWL - 1)) << 8 |
54 (e->tCL + 2 - (t->tCWL - 1));
Roy Splietbfb31462011-11-25 15:52:22 +010055
Roy Splietc7c039f2012-01-09 15:23:07 +100056 t->reg[2] = 0x20200000 |
57 ((t->tCWL - 1) << 24 |
58 e->tRRD << 16 |
59 e->tRCDWR << 8 |
60 e->tRCDRD);
Roy Spliet9a782482011-07-09 21:18:11 +020061
Roy Splietc7c039f2012-01-09 15:23:07 +100062 NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x\n", t->id,
63 t->reg[0], t->reg[1], t->reg[2]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +100064 return 0;
Roy Spliet9a782482011-07-09 21:18:11 +020065}
66
Ben Skeggsfd99fd62012-01-17 21:10:58 +100067static int
68nv50_mem_timing_calc(struct drm_device *dev, u32 freq,
69 struct nouveau_pm_tbl_entry *e, u8 len,
70 struct nouveau_pm_memtiming *boot,
71 struct nouveau_pm_memtiming *t)
Martin Peresddb20052011-12-17 12:24:59 +010072{
Ben Skeggsfd99fd62012-01-17 21:10:58 +100073 struct bit_entry P;
Roy Splietc7c039f2012-01-09 15:23:07 +100074 uint8_t unk18 = 1, unk20 = 0, unk21 = 0, tmp7_3;
Roy Spliet9a782482011-07-09 21:18:11 +020075
Ben Skeggsfd99fd62012-01-17 21:10:58 +100076 if (bit_table(dev, 'P', &P))
77 return -EINVAL;
78
79 switch (min(len, (u8) 22)) {
Roy Spliet9a782482011-07-09 21:18:11 +020080 case 22:
81 unk21 = e->tUNK_21;
82 case 21:
83 unk20 = e->tUNK_20;
84 case 20:
Roy Splietbfb31462011-11-25 15:52:22 +010085 if (e->tCWL > 0)
Roy Splietc7c039f2012-01-09 15:23:07 +100086 t->tCWL = e->tCWL;
Roy Spliet9a782482011-07-09 21:18:11 +020087 case 19:
88 unk18 = e->tUNK_18;
89 break;
90 }
91
Roy Splietc7c039f2012-01-09 15:23:07 +100092 t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
Roy Spliet9a782482011-07-09 21:18:11 +020093
Roy Splietc7c039f2012-01-09 15:23:07 +100094 t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
Roy Splietbfb31462011-11-25 15:52:22 +010095 max(unk18, (u8) 1) << 16 |
Roy Splietc7c039f2012-01-09 15:23:07 +100096 (e->tWTR + 2 + (t->tCWL - 1)) << 8;
Martin Peresddb20052011-12-17 12:24:59 +010097
Roy Splietc7c039f2012-01-09 15:23:07 +100098 t->reg[2] = ((t->tCWL - 1) << 24 |
99 e->tRRD << 16 |
100 e->tRCDWR << 8 |
101 e->tRCDRD);
Roy Spliet9a782482011-07-09 21:18:11 +0200102
Roy Splietc7c039f2012-01-09 15:23:07 +1000103 t->reg[4] = e->tUNK_13 << 8 | e->tUNK_13;
Roy Splietbfb31462011-11-25 15:52:22 +0100104
Roy Splietc7c039f2012-01-09 15:23:07 +1000105 t->reg[5] = (e->tRFC << 24 | max(e->tRCDRD, e->tRCDWR) << 16 | e->tRP);
Roy Splietbfb31462011-11-25 15:52:22 +0100106
Roy Splietc7c039f2012-01-09 15:23:07 +1000107 t->reg[8] = boot->reg[8] & 0xffffff00;
Roy Spliet9a782482011-07-09 21:18:11 +0200108
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000109 if (P.version == 1) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000110 t->reg[1] |= (e->tCL + 2 - (t->tCWL - 1));
Martin Peresddb20052011-12-17 12:24:59 +0100111
Roy Splietc7c039f2012-01-09 15:23:07 +1000112 t->reg[3] = (0x14 + e->tCL) << 24 |
113 0x16 << 16 |
114 (e->tCL - 1) << 8 |
115 (e->tCL - 1);
Martin Peresddb20052011-12-17 12:24:59 +0100116
Roy Splietc7c039f2012-01-09 15:23:07 +1000117 t->reg[4] |= boot->reg[4] & 0xffff0000;
Martin Peresddb20052011-12-17 12:24:59 +0100118
Roy Splietc7c039f2012-01-09 15:23:07 +1000119 t->reg[6] = (0x33 - t->tCWL) << 16 |
120 t->tCWL << 8 |
121 (0x2e + e->tCL - t->tCWL);
Martin Peresddb20052011-12-17 12:24:59 +0100122
Roy Splietc7c039f2012-01-09 15:23:07 +1000123 t->reg[7] = 0x4000202 | (e->tCL - 1) << 16;
Roy Spliet9a782482011-07-09 21:18:11 +0200124
Roy Splietbfb31462011-11-25 15:52:22 +0100125 /* XXX: P.version == 1 only has DDR2 and GDDR3? */
Ben Skeggs861d2102012-07-11 19:05:01 +1000126 if (nvfb_vram_type(dev) == NV_MEM_TYPE_DDR2) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000127 t->reg[5] |= (e->tCL + 3) << 8;
128 t->reg[6] |= (t->tCWL - 2) << 8;
129 t->reg[8] |= (e->tCL - 4);
Roy Splietbfb31462011-11-25 15:52:22 +0100130 } else {
Roy Splietc7c039f2012-01-09 15:23:07 +1000131 t->reg[5] |= (e->tCL + 2) << 8;
132 t->reg[6] |= t->tCWL << 8;
133 t->reg[8] |= (e->tCL - 2);
Roy Splietbfb31462011-11-25 15:52:22 +0100134 }
135 } else {
Roy Splietc7c039f2012-01-09 15:23:07 +1000136 t->reg[1] |= (5 + e->tCL - (t->tCWL));
Roy Splietbfb31462011-11-25 15:52:22 +0100137
138 /* XXX: 0xb? 0x30? */
Roy Splietc7c039f2012-01-09 15:23:07 +1000139 t->reg[3] = (0x30 + e->tCL) << 24 |
140 (boot->reg[3] & 0x00ff0000)|
141 (0xb + e->tCL) << 8 |
142 (e->tCL - 1);
Roy Splietbfb31462011-11-25 15:52:22 +0100143
Roy Splietc7c039f2012-01-09 15:23:07 +1000144 t->reg[4] |= (unk20 << 24 | unk21 << 16);
Roy Splietbfb31462011-11-25 15:52:22 +0100145
146 /* XXX: +6? */
Roy Splietc7c039f2012-01-09 15:23:07 +1000147 t->reg[5] |= (t->tCWL + 6) << 8;
Roy Splietbfb31462011-11-25 15:52:22 +0100148
Roy Splietc7c039f2012-01-09 15:23:07 +1000149 t->reg[6] = (0x5a + e->tCL) << 16 |
150 (6 - e->tCL + t->tCWL) << 8 |
151 (0x50 + e->tCL - t->tCWL);
Roy Splietbfb31462011-11-25 15:52:22 +0100152
Roy Splietc7c039f2012-01-09 15:23:07 +1000153 tmp7_3 = (boot->reg[7] & 0xff000000) >> 24;
154 t->reg[7] = (tmp7_3 << 24) |
155 ((tmp7_3 - 6 + e->tCL) << 16) |
156 0x202;
Roy Spliet9a782482011-07-09 21:18:11 +0200157 }
158
Roy Splietc7c039f2012-01-09 15:23:07 +1000159 NV_DEBUG(dev, "Entry %d: 220: %08x %08x %08x %08x\n", t->id,
160 t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
Roy Spliet9a782482011-07-09 21:18:11 +0200161 NV_DEBUG(dev, " 230: %08x %08x %08x %08x\n",
Roy Splietc7c039f2012-01-09 15:23:07 +1000162 t->reg[4], t->reg[5], t->reg[6], t->reg[7]);
163 NV_DEBUG(dev, " 240: %08x\n", t->reg[8]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000164 return 0;
Roy Spliet9a782482011-07-09 21:18:11 +0200165}
166
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000167static int
168nvc0_mem_timing_calc(struct drm_device *dev, u32 freq,
169 struct nouveau_pm_tbl_entry *e, u8 len,
170 struct nouveau_pm_memtiming *boot,
171 struct nouveau_pm_memtiming *t)
Martin Peresddb20052011-12-17 12:24:59 +0100172{
Roy Splietc7c039f2012-01-09 15:23:07 +1000173 if (e->tCWL > 0)
174 t->tCWL = e->tCWL;
Roy Splietbfb31462011-11-25 15:52:22 +0100175
Roy Splietc7c039f2012-01-09 15:23:07 +1000176 t->reg[0] = (e->tRP << 24 | (e->tRAS & 0x7f) << 17 |
177 e->tRFC << 8 | e->tRC);
Martin Peresddb20052011-12-17 12:24:59 +0100178
Roy Splietc7c039f2012-01-09 15:23:07 +1000179 t->reg[1] = (boot->reg[1] & 0xff000000) |
180 (e->tRCDWR & 0x0f) << 20 |
181 (e->tRCDRD & 0x0f) << 14 |
Roy Spliete6084252012-02-07 00:29:06 +0100182 (t->tCWL << 7) |
Roy Splietc7c039f2012-01-09 15:23:07 +1000183 (e->tCL & 0x0f);
Martin Peresddb20052011-12-17 12:24:59 +0100184
Roy Splietc7c039f2012-01-09 15:23:07 +1000185 t->reg[2] = (boot->reg[2] & 0xff0000ff) |
186 e->tWR << 16 | e->tWTR << 8;
Martin Peresddb20052011-12-17 12:24:59 +0100187
Roy Spliete6084252012-02-07 00:29:06 +0100188 t->reg[3] = (e->tUNK_20 & 0x1f) << 9 |
Roy Splietc7c039f2012-01-09 15:23:07 +1000189 (e->tUNK_21 & 0xf) << 5 |
190 (e->tUNK_13 & 0x1f);
Martin Peresddb20052011-12-17 12:24:59 +0100191
Roy Splietc7c039f2012-01-09 15:23:07 +1000192 t->reg[4] = (boot->reg[4] & 0xfff00fff) |
193 (e->tRRD&0x1f) << 15;
Martin Peresddb20052011-12-17 12:24:59 +0100194
Roy Splietc7c039f2012-01-09 15:23:07 +1000195 NV_DEBUG(dev, "Entry %d: 290: %08x %08x %08x %08x\n", t->id,
196 t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
197 NV_DEBUG(dev, " 2a0: %08x\n", t->reg[4]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000198 return 0;
Roy Splietbfb31462011-11-25 15:52:22 +0100199}
200
Roy Splietc7c039f2012-01-09 15:23:07 +1000201/**
202 * MR generation methods
203 */
204
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000205static int
206nouveau_mem_ddr2_mr(struct drm_device *dev, u32 freq,
207 struct nouveau_pm_tbl_entry *e, u8 len,
208 struct nouveau_pm_memtiming *boot,
209 struct nouveau_pm_memtiming *t)
Roy Splietbfb31462011-11-25 15:52:22 +0100210{
Roy Splietc7c039f2012-01-09 15:23:07 +1000211 t->drive_strength = 0;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000212 if (len < 15) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000213 t->odt = boot->odt;
214 } else {
215 t->odt = e->RAM_FT1 & 0x07;
Roy Splietbfb31462011-11-25 15:52:22 +0100216 }
Roy Splietc7c039f2012-01-09 15:23:07 +1000217
218 if (e->tCL >= NV_MEM_CL_DDR2_MAX) {
219 NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000220 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000221 }
222
223 if (e->tWR >= NV_MEM_WR_DDR2_MAX) {
224 NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000225 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000226 }
227
228 if (t->odt > 3) {
229 NV_WARN(dev, "(%u) Invalid odt value, assuming disabled: %x",
230 t->id, t->odt);
231 t->odt = 0;
232 }
233
234 t->mr[0] = (boot->mr[0] & 0x100f) |
235 (e->tCL) << 4 |
236 (e->tWR - 1) << 9;
237 t->mr[1] = (boot->mr[1] & 0x101fbb) |
238 (t->odt & 0x1) << 2 |
239 (t->odt & 0x2) << 5;
240
241 NV_DEBUG(dev, "(%u) MR: %08x", t->id, t->mr[0]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000242 return 0;
Roy Splietc7c039f2012-01-09 15:23:07 +1000243}
244
245uint8_t nv_mem_wr_lut_ddr3[NV_MEM_WR_DDR3_MAX] = {
246 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
247
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000248static int
249nouveau_mem_ddr3_mr(struct drm_device *dev, u32 freq,
250 struct nouveau_pm_tbl_entry *e, u8 len,
251 struct nouveau_pm_memtiming *boot,
252 struct nouveau_pm_memtiming *t)
Roy Splietc7c039f2012-01-09 15:23:07 +1000253{
254 u8 cl = e->tCL - 4;
255
256 t->drive_strength = 0;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000257 if (len < 15) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000258 t->odt = boot->odt;
259 } else {
260 t->odt = e->RAM_FT1 & 0x07;
261 }
262
263 if (e->tCL >= NV_MEM_CL_DDR3_MAX || e->tCL < 4) {
264 NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000265 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000266 }
267
268 if (e->tWR >= NV_MEM_WR_DDR3_MAX || e->tWR < 4) {
269 NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000270 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000271 }
272
273 if (e->tCWL < 5) {
274 NV_WARN(dev, "(%u) Invalid tCWL: %u", t->id, e->tCWL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000275 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000276 }
277
278 t->mr[0] = (boot->mr[0] & 0x180b) |
279 /* CAS */
280 (cl & 0x7) << 4 |
281 (cl & 0x8) >> 1 |
282 (nv_mem_wr_lut_ddr3[e->tWR]) << 9;
283 t->mr[1] = (boot->mr[1] & 0x101dbb) |
284 (t->odt & 0x1) << 2 |
285 (t->odt & 0x2) << 5 |
286 (t->odt & 0x4) << 7;
287 t->mr[2] = (boot->mr[2] & 0x20ffb7) | (e->tCWL - 5) << 3;
288
289 NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[2]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000290 return 0;
Roy Splietc7c039f2012-01-09 15:23:07 +1000291}
292
293uint8_t nv_mem_cl_lut_gddr3[NV_MEM_CL_GDDR3_MAX] = {
294 0, 0, 0, 0, 4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11};
295uint8_t nv_mem_wr_lut_gddr3[NV_MEM_WR_GDDR3_MAX] = {
296 0, 0, 0, 0, 0, 2, 3, 8, 9, 10, 11, 0, 0, 1, 1, 0, 3};
297
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000298static int
299nouveau_mem_gddr3_mr(struct drm_device *dev, u32 freq,
300 struct nouveau_pm_tbl_entry *e, u8 len,
301 struct nouveau_pm_memtiming *boot,
302 struct nouveau_pm_memtiming *t)
Roy Splietc7c039f2012-01-09 15:23:07 +1000303{
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000304 if (len < 15) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000305 t->drive_strength = boot->drive_strength;
306 t->odt = boot->odt;
307 } else {
308 t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
309 t->odt = e->RAM_FT1 & 0x07;
310 }
311
312 if (e->tCL >= NV_MEM_CL_GDDR3_MAX) {
313 NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000314 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000315 }
316
317 if (e->tWR >= NV_MEM_WR_GDDR3_MAX) {
318 NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000319 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000320 }
321
322 if (t->odt > 3) {
323 NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
324 t->id, t->odt);
325 t->odt = 0;
326 }
327
328 t->mr[0] = (boot->mr[0] & 0xe0b) |
329 /* CAS */
330 ((nv_mem_cl_lut_gddr3[e->tCL] & 0x7) << 4) |
331 ((nv_mem_cl_lut_gddr3[e->tCL] & 0x8) >> 2);
332 t->mr[1] = (boot->mr[1] & 0x100f40) | t->drive_strength |
333 (t->odt << 2) |
334 (nv_mem_wr_lut_gddr3[e->tWR] & 0xf) << 4;
Ben Skeggs1a7287e2012-01-24 10:24:05 +1000335 t->mr[2] = boot->mr[2];
Roy Splietc7c039f2012-01-09 15:23:07 +1000336
Ben Skeggs1a7287e2012-01-24 10:24:05 +1000337 NV_DEBUG(dev, "(%u) MR: %08x %08x %08x", t->id,
338 t->mr[0], t->mr[1], t->mr[2]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000339 return 0;
Roy Splietc7c039f2012-01-09 15:23:07 +1000340}
341
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000342static int
343nouveau_mem_gddr5_mr(struct drm_device *dev, u32 freq,
344 struct nouveau_pm_tbl_entry *e, u8 len,
345 struct nouveau_pm_memtiming *boot,
346 struct nouveau_pm_memtiming *t)
Roy Splietc7c039f2012-01-09 15:23:07 +1000347{
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000348 if (len < 15) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000349 t->drive_strength = boot->drive_strength;
350 t->odt = boot->odt;
351 } else {
352 t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
353 t->odt = e->RAM_FT1 & 0x03;
354 }
355
356 if (e->tCL >= NV_MEM_CL_GDDR5_MAX) {
357 NV_WARN(dev, "(%u) Invalid tCL: %u", t->id, e->tCL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000358 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000359 }
360
361 if (e->tWR >= NV_MEM_WR_GDDR5_MAX) {
362 NV_WARN(dev, "(%u) Invalid tWR: %u", t->id, e->tWR);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000363 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000364 }
365
366 if (t->odt > 3) {
367 NV_WARN(dev, "(%u) Invalid odt value, assuming autocal: %x",
368 t->id, t->odt);
369 t->odt = 0;
370 }
371
372 t->mr[0] = (boot->mr[0] & 0x007) |
373 ((e->tCL - 5) << 3) |
374 ((e->tWR - 4) << 8);
375 t->mr[1] = (boot->mr[1] & 0x1007f0) |
376 t->drive_strength |
377 (t->odt << 2);
378
379 NV_DEBUG(dev, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[1]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000380 return 0;
Roy Splietc7c039f2012-01-09 15:23:07 +1000381}
382
Ben Skeggs085028c2012-01-18 09:02:28 +1000383int
384nouveau_mem_timing_calc(struct drm_device *dev, u32 freq,
385 struct nouveau_pm_memtiming *t)
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000386{
387 struct drm_nouveau_private *dev_priv = dev->dev_private;
388 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Ben Skeggs085028c2012-01-18 09:02:28 +1000389 struct nouveau_pm_memtiming *boot = &pm->boot.timing;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000390 struct nouveau_pm_tbl_entry *e;
Ben Skeggs070be292012-01-24 18:30:10 +1000391 u8 ver, len, *ptr, *ramcfg;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000392 int ret;
393
394 ptr = nouveau_perf_timing(dev, freq, &ver, &len);
Ben Skeggs085028c2012-01-18 09:02:28 +1000395 if (!ptr || ptr[0] == 0x00) {
396 *t = *boot;
397 return 0;
398 }
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000399 e = (struct nouveau_pm_tbl_entry *)ptr;
400
Ben Skeggs085028c2012-01-18 09:02:28 +1000401 t->tCWL = boot->tCWL;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000402
Ben Skeggs085028c2012-01-18 09:02:28 +1000403 switch (dev_priv->card_type) {
404 case NV_40:
405 ret = nv40_mem_timing_calc(dev, freq, e, len, boot, t);
406 break;
407 case NV_50:
408 ret = nv50_mem_timing_calc(dev, freq, e, len, boot, t);
409 break;
410 case NV_C0:
Ben Skeggsa94ba1f2012-02-06 11:42:29 +1000411 case NV_D0:
Ben Skeggs085028c2012-01-18 09:02:28 +1000412 ret = nvc0_mem_timing_calc(dev, freq, e, len, boot, t);
413 break;
414 default:
415 ret = -ENODEV;
416 break;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000417 }
418
Ben Skeggs861d2102012-07-11 19:05:01 +1000419 switch (nvfb_vram_type(dev) * !ret) {
Ben Skeggs085028c2012-01-18 09:02:28 +1000420 case NV_MEM_TYPE_GDDR3:
421 ret = nouveau_mem_gddr3_mr(dev, freq, e, len, boot, t);
422 break;
423 case NV_MEM_TYPE_GDDR5:
424 ret = nouveau_mem_gddr5_mr(dev, freq, e, len, boot, t);
425 break;
426 case NV_MEM_TYPE_DDR2:
427 ret = nouveau_mem_ddr2_mr(dev, freq, e, len, boot, t);
428 break;
429 case NV_MEM_TYPE_DDR3:
430 ret = nouveau_mem_ddr3_mr(dev, freq, e, len, boot, t);
431 break;
432 default:
433 ret = -EINVAL;
Ben Skeggs070be292012-01-24 18:30:10 +1000434 break;
435 }
436
437 ramcfg = nouveau_perf_ramcfg(dev, freq, &ver, &len);
438 if (ramcfg) {
439 int dll_off;
440
441 if (ver == 0x00)
442 dll_off = !!(ramcfg[3] & 0x04);
443 else
444 dll_off = !!(ramcfg[2] & 0x40);
445
Ben Skeggs861d2102012-07-11 19:05:01 +1000446 switch (nvfb_vram_type(dev)) {
Ben Skeggs070be292012-01-24 18:30:10 +1000447 case NV_MEM_TYPE_GDDR3:
448 t->mr[1] &= ~0x00000040;
449 t->mr[1] |= 0x00000040 * dll_off;
450 break;
451 default:
452 t->mr[1] &= ~0x00000001;
453 t->mr[1] |= 0x00000001 * dll_off;
454 break;
455 }
Ben Skeggs085028c2012-01-18 09:02:28 +1000456 }
457
458 return ret;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000459}
460
461void
462nouveau_mem_timing_read(struct drm_device *dev, struct nouveau_pm_memtiming *t)
Roy Splietc7c039f2012-01-09 15:23:07 +1000463{
464 struct drm_nouveau_private *dev_priv = dev->dev_private;
465 u32 timing_base, timing_regs, mr_base;
466 int i;
467
468 if (dev_priv->card_type >= 0xC0) {
469 timing_base = 0x10f290;
470 mr_base = 0x10f300;
471 } else {
472 timing_base = 0x100220;
473 mr_base = 0x1002c0;
474 }
475
476 t->id = -1;
477
478 switch (dev_priv->card_type) {
479 case NV_50:
480 timing_regs = 9;
481 break;
482 case NV_C0:
483 case NV_D0:
484 timing_regs = 5;
485 break;
486 case NV_30:
487 case NV_40:
488 timing_regs = 3;
489 break;
490 default:
491 timing_regs = 0;
492 return;
493 }
494 for(i = 0; i < timing_regs; i++)
495 t->reg[i] = nv_rd32(dev, timing_base + (0x04 * i));
496
497 t->tCWL = 0;
498 if (dev_priv->card_type < NV_C0) {
499 t->tCWL = ((nv_rd32(dev, 0x100228) & 0x0f000000) >> 24) + 1;
Roy Spliete6084252012-02-07 00:29:06 +0100500 } else if (dev_priv->card_type <= NV_D0) {
501 t->tCWL = ((nv_rd32(dev, 0x10f294) & 0x00000f80) >> 7);
Roy Splietc7c039f2012-01-09 15:23:07 +1000502 }
503
504 t->mr[0] = nv_rd32(dev, mr_base);
505 t->mr[1] = nv_rd32(dev, mr_base + 0x04);
506 t->mr[2] = nv_rd32(dev, mr_base + 0x20);
507 t->mr[3] = nv_rd32(dev, mr_base + 0x24);
508
509 t->odt = 0;
510 t->drive_strength = 0;
511
Ben Skeggs861d2102012-07-11 19:05:01 +1000512 switch (nvfb_vram_type(dev)) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000513 case NV_MEM_TYPE_DDR3:
514 t->odt |= (t->mr[1] & 0x200) >> 7;
515 case NV_MEM_TYPE_DDR2:
516 t->odt |= (t->mr[1] & 0x04) >> 2 |
517 (t->mr[1] & 0x40) >> 5;
518 break;
519 case NV_MEM_TYPE_GDDR3:
520 case NV_MEM_TYPE_GDDR5:
521 t->drive_strength = t->mr[1] & 0x03;
522 t->odt = (t->mr[1] & 0x0c) >> 2;
523 break;
524 default:
525 break;
526 }
527}
528
Ben Skeggsc70c41e2011-12-13 11:57:55 +1000529int
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000530nouveau_mem_exec(struct nouveau_mem_exec_func *exec,
531 struct nouveau_pm_level *perflvl)
532{
533 struct drm_nouveau_private *dev_priv = exec->dev->dev_private;
534 struct nouveau_pm_memtiming *info = &perflvl->timing;
535 u32 tMRD = 1000, tCKSRE = 0, tCKSRX = 0, tXS = 0, tDLLK = 0;
536 u32 mr[3] = { info->mr[0], info->mr[1], info->mr[2] };
537 u32 mr1_dlloff;
538
Ben Skeggs861d2102012-07-11 19:05:01 +1000539 switch (nvfb_vram_type(dev_priv->dev)) {
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000540 case NV_MEM_TYPE_DDR2:
541 tDLLK = 2000;
542 mr1_dlloff = 0x00000001;
543 break;
544 case NV_MEM_TYPE_DDR3:
545 tDLLK = 12000;
Ben Skeggs78c20182012-02-06 16:20:30 +1000546 tCKSRE = 2000;
547 tXS = 1000;
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000548 mr1_dlloff = 0x00000001;
549 break;
550 case NV_MEM_TYPE_GDDR3:
551 tDLLK = 40000;
552 mr1_dlloff = 0x00000040;
553 break;
554 default:
555 NV_ERROR(exec->dev, "cannot reclock unsupported memtype\n");
556 return -ENODEV;
557 }
558
559 /* fetch current MRs */
Ben Skeggs861d2102012-07-11 19:05:01 +1000560 switch (nvfb_vram_type(dev_priv->dev)) {
Ben Skeggs1a7287e2012-01-24 10:24:05 +1000561 case NV_MEM_TYPE_GDDR3:
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000562 case NV_MEM_TYPE_DDR3:
563 mr[2] = exec->mrg(exec, 2);
564 default:
565 mr[1] = exec->mrg(exec, 1);
566 mr[0] = exec->mrg(exec, 0);
567 break;
568 }
569
570 /* DLL 'on' -> DLL 'off' mode, disable before entering self-refresh */
571 if (!(mr[1] & mr1_dlloff) && (info->mr[1] & mr1_dlloff)) {
572 exec->precharge(exec);
573 exec->mrs (exec, 1, mr[1] | mr1_dlloff);
574 exec->wait(exec, tMRD);
575 }
576
577 /* enter self-refresh mode */
578 exec->precharge(exec);
579 exec->refresh(exec);
580 exec->refresh(exec);
581 exec->refresh_auto(exec, false);
582 exec->refresh_self(exec, true);
583 exec->wait(exec, tCKSRE);
584
585 /* modify input clock frequency */
586 exec->clock_set(exec);
587
588 /* exit self-refresh mode */
589 exec->wait(exec, tCKSRX);
590 exec->precharge(exec);
591 exec->refresh_self(exec, false);
592 exec->refresh_auto(exec, true);
593 exec->wait(exec, tXS);
Ben Skeggs78c20182012-02-06 16:20:30 +1000594 exec->wait(exec, tXS);
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000595
596 /* update MRs */
597 if (mr[2] != info->mr[2]) {
598 exec->mrs (exec, 2, info->mr[2]);
599 exec->wait(exec, tMRD);
600 }
601
602 if (mr[1] != info->mr[1]) {
Ben Skeggsb8309732012-01-24 13:39:56 +1000603 /* need to keep DLL off until later, at least on GDDR3 */
604 exec->mrs (exec, 1, info->mr[1] | (mr[1] & mr1_dlloff));
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000605 exec->wait(exec, tMRD);
606 }
607
608 if (mr[0] != info->mr[0]) {
609 exec->mrs (exec, 0, info->mr[0]);
610 exec->wait(exec, tMRD);
611 }
612
613 /* update PFB timing registers */
614 exec->timing_set(exec);
615
Ben Skeggsb8309732012-01-24 13:39:56 +1000616 /* DLL (enable + ) reset */
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000617 if (!(info->mr[1] & mr1_dlloff)) {
Ben Skeggsb8309732012-01-24 13:39:56 +1000618 if (mr[1] & mr1_dlloff) {
619 exec->mrs (exec, 1, info->mr[1]);
620 exec->wait(exec, tMRD);
621 }
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000622 exec->mrs (exec, 0, info->mr[0] | 0x00000100);
623 exec->wait(exec, tMRD);
624 exec->mrs (exec, 0, info->mr[0] | 0x00000000);
625 exec->wait(exec, tMRD);
626 exec->wait(exec, tDLLK);
Ben Skeggs861d2102012-07-11 19:05:01 +1000627 if (nvfb_vram_type(dev_priv->dev) == NV_MEM_TYPE_GDDR3)
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000628 exec->precharge(exec);
629 }
630
631 return 0;
632}