blob: 7e0ff10a2759cc94d1c6bd91030f2c3451061896 [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
Ben Skeggs77145f12012-07-31 16:16:21 +100033#include "nouveau_drm.h"
Francisco Jerezcbab95db2010-10-11 03:43:58 +020034#include "nouveau_pm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100035
Ben Skeggs77145f12012-07-31 16:16:21 +100036#include <subdev/fb.h>
37
Ben Skeggsfd99fd62012-01-17 21:10:58 +100038static int
39nv40_mem_timing_calc(struct drm_device *dev, u32 freq,
40 struct nouveau_pm_tbl_entry *e, u8 len,
41 struct nouveau_pm_memtiming *boot,
42 struct nouveau_pm_memtiming *t)
Martin Peresddb20052011-12-17 12:24:59 +010043{
Ben Skeggs77145f12012-07-31 16:16:21 +100044 struct nouveau_drm *drm = nouveau_drm(dev);
45
Roy Splietc7c039f2012-01-09 15:23:07 +100046 t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
Roy Spliet9a782482011-07-09 21:18:11 +020047
48 /* XXX: I don't trust the -1's and +1's... they must come
49 * from somewhere! */
Roy Splietc7c039f2012-01-09 15:23:07 +100050 t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
51 1 << 16 |
52 (e->tWTR + 2 + (t->tCWL - 1)) << 8 |
53 (e->tCL + 2 - (t->tCWL - 1));
Roy Splietbfb31462011-11-25 15:52:22 +010054
Roy Splietc7c039f2012-01-09 15:23:07 +100055 t->reg[2] = 0x20200000 |
56 ((t->tCWL - 1) << 24 |
57 e->tRRD << 16 |
58 e->tRCDWR << 8 |
59 e->tRCDRD);
Roy Spliet9a782482011-07-09 21:18:11 +020060
Ben Skeggs77145f12012-07-31 16:16:21 +100061 NV_DEBUG(drm, "Entry %d: 220: %08x %08x %08x\n", t->id,
Roy Splietc7c039f2012-01-09 15:23:07 +100062 t->reg[0], t->reg[1], t->reg[2]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +100063 return 0;
Roy Spliet9a782482011-07-09 21:18:11 +020064}
65
Ben Skeggsfd99fd62012-01-17 21:10:58 +100066static int
67nv50_mem_timing_calc(struct drm_device *dev, u32 freq,
68 struct nouveau_pm_tbl_entry *e, u8 len,
69 struct nouveau_pm_memtiming *boot,
70 struct nouveau_pm_memtiming *t)
Martin Peresddb20052011-12-17 12:24:59 +010071{
Ben Skeggs77145f12012-07-31 16:16:21 +100072 struct nouveau_device *device = nouveau_dev(dev);
73 struct nouveau_fb *pfb = nouveau_fb(device);
74 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsfd99fd62012-01-17 21:10:58 +100075 struct bit_entry P;
Roy Splietc7c039f2012-01-09 15:23:07 +100076 uint8_t unk18 = 1, unk20 = 0, unk21 = 0, tmp7_3;
Roy Spliet9a782482011-07-09 21:18:11 +020077
Ben Skeggsfd99fd62012-01-17 21:10:58 +100078 if (bit_table(dev, 'P', &P))
79 return -EINVAL;
80
81 switch (min(len, (u8) 22)) {
Roy Spliet9a782482011-07-09 21:18:11 +020082 case 22:
83 unk21 = e->tUNK_21;
84 case 21:
85 unk20 = e->tUNK_20;
86 case 20:
Roy Splietbfb31462011-11-25 15:52:22 +010087 if (e->tCWL > 0)
Roy Splietc7c039f2012-01-09 15:23:07 +100088 t->tCWL = e->tCWL;
Roy Spliet9a782482011-07-09 21:18:11 +020089 case 19:
90 unk18 = e->tUNK_18;
91 break;
92 }
93
Roy Splietc7c039f2012-01-09 15:23:07 +100094 t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
Roy Spliet9a782482011-07-09 21:18:11 +020095
Roy Splietc7c039f2012-01-09 15:23:07 +100096 t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
Roy Splietbfb31462011-11-25 15:52:22 +010097 max(unk18, (u8) 1) << 16 |
Roy Splietc7c039f2012-01-09 15:23:07 +100098 (e->tWTR + 2 + (t->tCWL - 1)) << 8;
Martin Peresddb20052011-12-17 12:24:59 +010099
Roy Splietc7c039f2012-01-09 15:23:07 +1000100 t->reg[2] = ((t->tCWL - 1) << 24 |
101 e->tRRD << 16 |
102 e->tRCDWR << 8 |
103 e->tRCDRD);
Roy Spliet9a782482011-07-09 21:18:11 +0200104
Roy Splietc7c039f2012-01-09 15:23:07 +1000105 t->reg[4] = e->tUNK_13 << 8 | e->tUNK_13;
Roy Splietbfb31462011-11-25 15:52:22 +0100106
Roy Splietc7c039f2012-01-09 15:23:07 +1000107 t->reg[5] = (e->tRFC << 24 | max(e->tRCDRD, e->tRCDWR) << 16 | e->tRP);
Roy Splietbfb31462011-11-25 15:52:22 +0100108
Roy Splietc7c039f2012-01-09 15:23:07 +1000109 t->reg[8] = boot->reg[8] & 0xffffff00;
Roy Spliet9a782482011-07-09 21:18:11 +0200110
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000111 if (P.version == 1) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000112 t->reg[1] |= (e->tCL + 2 - (t->tCWL - 1));
Martin Peresddb20052011-12-17 12:24:59 +0100113
Roy Splietc7c039f2012-01-09 15:23:07 +1000114 t->reg[3] = (0x14 + e->tCL) << 24 |
115 0x16 << 16 |
116 (e->tCL - 1) << 8 |
117 (e->tCL - 1);
Martin Peresddb20052011-12-17 12:24:59 +0100118
Roy Splietc7c039f2012-01-09 15:23:07 +1000119 t->reg[4] |= boot->reg[4] & 0xffff0000;
Martin Peresddb20052011-12-17 12:24:59 +0100120
Roy Splietc7c039f2012-01-09 15:23:07 +1000121 t->reg[6] = (0x33 - t->tCWL) << 16 |
122 t->tCWL << 8 |
123 (0x2e + e->tCL - t->tCWL);
Martin Peresddb20052011-12-17 12:24:59 +0100124
Roy Splietc7c039f2012-01-09 15:23:07 +1000125 t->reg[7] = 0x4000202 | (e->tCL - 1) << 16;
Roy Spliet9a782482011-07-09 21:18:11 +0200126
Roy Splietbfb31462011-11-25 15:52:22 +0100127 /* XXX: P.version == 1 only has DDR2 and GDDR3? */
Ben Skeggs77145f12012-07-31 16:16:21 +1000128 if (pfb->ram.type == NV_MEM_TYPE_DDR2) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000129 t->reg[5] |= (e->tCL + 3) << 8;
130 t->reg[6] |= (t->tCWL - 2) << 8;
131 t->reg[8] |= (e->tCL - 4);
Roy Splietbfb31462011-11-25 15:52:22 +0100132 } else {
Roy Splietc7c039f2012-01-09 15:23:07 +1000133 t->reg[5] |= (e->tCL + 2) << 8;
134 t->reg[6] |= t->tCWL << 8;
135 t->reg[8] |= (e->tCL - 2);
Roy Splietbfb31462011-11-25 15:52:22 +0100136 }
137 } else {
Roy Splietc7c039f2012-01-09 15:23:07 +1000138 t->reg[1] |= (5 + e->tCL - (t->tCWL));
Roy Splietbfb31462011-11-25 15:52:22 +0100139
140 /* XXX: 0xb? 0x30? */
Roy Splietc7c039f2012-01-09 15:23:07 +1000141 t->reg[3] = (0x30 + e->tCL) << 24 |
142 (boot->reg[3] & 0x00ff0000)|
143 (0xb + e->tCL) << 8 |
144 (e->tCL - 1);
Roy Splietbfb31462011-11-25 15:52:22 +0100145
Roy Splietc7c039f2012-01-09 15:23:07 +1000146 t->reg[4] |= (unk20 << 24 | unk21 << 16);
Roy Splietbfb31462011-11-25 15:52:22 +0100147
148 /* XXX: +6? */
Roy Splietc7c039f2012-01-09 15:23:07 +1000149 t->reg[5] |= (t->tCWL + 6) << 8;
Roy Splietbfb31462011-11-25 15:52:22 +0100150
Roy Splietc7c039f2012-01-09 15:23:07 +1000151 t->reg[6] = (0x5a + e->tCL) << 16 |
152 (6 - e->tCL + t->tCWL) << 8 |
153 (0x50 + e->tCL - t->tCWL);
Roy Splietbfb31462011-11-25 15:52:22 +0100154
Roy Splietc7c039f2012-01-09 15:23:07 +1000155 tmp7_3 = (boot->reg[7] & 0xff000000) >> 24;
156 t->reg[7] = (tmp7_3 << 24) |
157 ((tmp7_3 - 6 + e->tCL) << 16) |
158 0x202;
Roy Spliet9a782482011-07-09 21:18:11 +0200159 }
160
Ben Skeggs77145f12012-07-31 16:16:21 +1000161 NV_DEBUG(drm, "Entry %d: 220: %08x %08x %08x %08x\n", t->id,
Roy Splietc7c039f2012-01-09 15:23:07 +1000162 t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
Ben Skeggs77145f12012-07-31 16:16:21 +1000163 NV_DEBUG(drm, " 230: %08x %08x %08x %08x\n",
Roy Splietc7c039f2012-01-09 15:23:07 +1000164 t->reg[4], t->reg[5], t->reg[6], t->reg[7]);
Ben Skeggs77145f12012-07-31 16:16:21 +1000165 NV_DEBUG(drm, " 240: %08x\n", t->reg[8]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000166 return 0;
Roy Spliet9a782482011-07-09 21:18:11 +0200167}
168
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000169static int
170nvc0_mem_timing_calc(struct drm_device *dev, u32 freq,
171 struct nouveau_pm_tbl_entry *e, u8 len,
172 struct nouveau_pm_memtiming *boot,
173 struct nouveau_pm_memtiming *t)
Martin Peresddb20052011-12-17 12:24:59 +0100174{
Ben Skeggs77145f12012-07-31 16:16:21 +1000175 struct nouveau_drm *drm = nouveau_drm(dev);
176
Roy Splietc7c039f2012-01-09 15:23:07 +1000177 if (e->tCWL > 0)
178 t->tCWL = e->tCWL;
Roy Splietbfb31462011-11-25 15:52:22 +0100179
Roy Splietc7c039f2012-01-09 15:23:07 +1000180 t->reg[0] = (e->tRP << 24 | (e->tRAS & 0x7f) << 17 |
181 e->tRFC << 8 | e->tRC);
Martin Peresddb20052011-12-17 12:24:59 +0100182
Roy Splietc7c039f2012-01-09 15:23:07 +1000183 t->reg[1] = (boot->reg[1] & 0xff000000) |
184 (e->tRCDWR & 0x0f) << 20 |
185 (e->tRCDRD & 0x0f) << 14 |
Roy Spliete6084252012-02-07 00:29:06 +0100186 (t->tCWL << 7) |
Roy Splietc7c039f2012-01-09 15:23:07 +1000187 (e->tCL & 0x0f);
Martin Peresddb20052011-12-17 12:24:59 +0100188
Roy Splietc7c039f2012-01-09 15:23:07 +1000189 t->reg[2] = (boot->reg[2] & 0xff0000ff) |
190 e->tWR << 16 | e->tWTR << 8;
Martin Peresddb20052011-12-17 12:24:59 +0100191
Roy Spliete6084252012-02-07 00:29:06 +0100192 t->reg[3] = (e->tUNK_20 & 0x1f) << 9 |
Roy Splietc7c039f2012-01-09 15:23:07 +1000193 (e->tUNK_21 & 0xf) << 5 |
194 (e->tUNK_13 & 0x1f);
Martin Peresddb20052011-12-17 12:24:59 +0100195
Roy Splietc7c039f2012-01-09 15:23:07 +1000196 t->reg[4] = (boot->reg[4] & 0xfff00fff) |
197 (e->tRRD&0x1f) << 15;
Martin Peresddb20052011-12-17 12:24:59 +0100198
Ben Skeggs77145f12012-07-31 16:16:21 +1000199 NV_DEBUG(drm, "Entry %d: 290: %08x %08x %08x %08x\n", t->id,
Roy Splietc7c039f2012-01-09 15:23:07 +1000200 t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
Ben Skeggs77145f12012-07-31 16:16:21 +1000201 NV_DEBUG(drm, " 2a0: %08x\n", t->reg[4]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000202 return 0;
Roy Splietbfb31462011-11-25 15:52:22 +0100203}
204
Roy Splietc7c039f2012-01-09 15:23:07 +1000205/**
206 * MR generation methods
207 */
208
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000209static int
210nouveau_mem_ddr2_mr(struct drm_device *dev, u32 freq,
211 struct nouveau_pm_tbl_entry *e, u8 len,
212 struct nouveau_pm_memtiming *boot,
213 struct nouveau_pm_memtiming *t)
Roy Splietbfb31462011-11-25 15:52:22 +0100214{
Ben Skeggs77145f12012-07-31 16:16:21 +1000215 struct nouveau_drm *drm = nouveau_drm(dev);
216
Roy Splietc7c039f2012-01-09 15:23:07 +1000217 t->drive_strength = 0;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000218 if (len < 15) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000219 t->odt = boot->odt;
220 } else {
221 t->odt = e->RAM_FT1 & 0x07;
Roy Splietbfb31462011-11-25 15:52:22 +0100222 }
Roy Splietc7c039f2012-01-09 15:23:07 +1000223
224 if (e->tCL >= NV_MEM_CL_DDR2_MAX) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000225 NV_WARN(drm, "(%u) Invalid tCL: %u", t->id, e->tCL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000226 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000227 }
228
229 if (e->tWR >= NV_MEM_WR_DDR2_MAX) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000230 NV_WARN(drm, "(%u) Invalid tWR: %u", t->id, e->tWR);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000231 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000232 }
233
234 if (t->odt > 3) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000235 NV_WARN(drm, "(%u) Invalid odt value, assuming disabled: %x",
Roy Splietc7c039f2012-01-09 15:23:07 +1000236 t->id, t->odt);
237 t->odt = 0;
238 }
239
240 t->mr[0] = (boot->mr[0] & 0x100f) |
241 (e->tCL) << 4 |
242 (e->tWR - 1) << 9;
243 t->mr[1] = (boot->mr[1] & 0x101fbb) |
244 (t->odt & 0x1) << 2 |
245 (t->odt & 0x2) << 5;
246
Ben Skeggs77145f12012-07-31 16:16:21 +1000247 NV_DEBUG(drm, "(%u) MR: %08x", t->id, t->mr[0]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000248 return 0;
Roy Splietc7c039f2012-01-09 15:23:07 +1000249}
250
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200251static const uint8_t nv_mem_wr_lut_ddr3[NV_MEM_WR_DDR3_MAX] = {
Roy Splietc7c039f2012-01-09 15:23:07 +1000252 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
253
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000254static int
255nouveau_mem_ddr3_mr(struct drm_device *dev, u32 freq,
256 struct nouveau_pm_tbl_entry *e, u8 len,
257 struct nouveau_pm_memtiming *boot,
258 struct nouveau_pm_memtiming *t)
Roy Splietc7c039f2012-01-09 15:23:07 +1000259{
Ben Skeggs77145f12012-07-31 16:16:21 +1000260 struct nouveau_drm *drm = nouveau_drm(dev);
Roy Splietc7c039f2012-01-09 15:23:07 +1000261 u8 cl = e->tCL - 4;
262
263 t->drive_strength = 0;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000264 if (len < 15) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000265 t->odt = boot->odt;
266 } else {
267 t->odt = e->RAM_FT1 & 0x07;
268 }
269
270 if (e->tCL >= NV_MEM_CL_DDR3_MAX || e->tCL < 4) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000271 NV_WARN(drm, "(%u) Invalid tCL: %u", t->id, e->tCL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000272 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000273 }
274
275 if (e->tWR >= NV_MEM_WR_DDR3_MAX || e->tWR < 4) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000276 NV_WARN(drm, "(%u) Invalid tWR: %u", t->id, e->tWR);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000277 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000278 }
279
280 if (e->tCWL < 5) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000281 NV_WARN(drm, "(%u) Invalid tCWL: %u", t->id, e->tCWL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000282 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000283 }
284
285 t->mr[0] = (boot->mr[0] & 0x180b) |
286 /* CAS */
287 (cl & 0x7) << 4 |
288 (cl & 0x8) >> 1 |
289 (nv_mem_wr_lut_ddr3[e->tWR]) << 9;
290 t->mr[1] = (boot->mr[1] & 0x101dbb) |
291 (t->odt & 0x1) << 2 |
292 (t->odt & 0x2) << 5 |
293 (t->odt & 0x4) << 7;
294 t->mr[2] = (boot->mr[2] & 0x20ffb7) | (e->tCWL - 5) << 3;
295
Ben Skeggs77145f12012-07-31 16:16:21 +1000296 NV_DEBUG(drm, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[2]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000297 return 0;
Roy Splietc7c039f2012-01-09 15:23:07 +1000298}
299
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200300static const uint8_t nv_mem_cl_lut_gddr3[NV_MEM_CL_GDDR3_MAX] = {
Roy Splietc7c039f2012-01-09 15:23:07 +1000301 0, 0, 0, 0, 4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11};
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200302static const uint8_t nv_mem_wr_lut_gddr3[NV_MEM_WR_GDDR3_MAX] = {
Roy Splietc7c039f2012-01-09 15:23:07 +1000303 0, 0, 0, 0, 0, 2, 3, 8, 9, 10, 11, 0, 0, 1, 1, 0, 3};
304
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000305static int
306nouveau_mem_gddr3_mr(struct drm_device *dev, u32 freq,
307 struct nouveau_pm_tbl_entry *e, u8 len,
308 struct nouveau_pm_memtiming *boot,
309 struct nouveau_pm_memtiming *t)
Roy Splietc7c039f2012-01-09 15:23:07 +1000310{
Ben Skeggs77145f12012-07-31 16:16:21 +1000311 struct nouveau_drm *drm = nouveau_drm(dev);
312
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000313 if (len < 15) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000314 t->drive_strength = boot->drive_strength;
315 t->odt = boot->odt;
316 } else {
317 t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
318 t->odt = e->RAM_FT1 & 0x07;
319 }
320
321 if (e->tCL >= NV_MEM_CL_GDDR3_MAX) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000322 NV_WARN(drm, "(%u) Invalid tCL: %u", t->id, e->tCL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000323 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000324 }
325
326 if (e->tWR >= NV_MEM_WR_GDDR3_MAX) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000327 NV_WARN(drm, "(%u) Invalid tWR: %u", t->id, e->tWR);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000328 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000329 }
330
331 if (t->odt > 3) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000332 NV_WARN(drm, "(%u) Invalid odt value, assuming autocal: %x",
Roy Splietc7c039f2012-01-09 15:23:07 +1000333 t->id, t->odt);
334 t->odt = 0;
335 }
336
337 t->mr[0] = (boot->mr[0] & 0xe0b) |
338 /* CAS */
339 ((nv_mem_cl_lut_gddr3[e->tCL] & 0x7) << 4) |
340 ((nv_mem_cl_lut_gddr3[e->tCL] & 0x8) >> 2);
341 t->mr[1] = (boot->mr[1] & 0x100f40) | t->drive_strength |
342 (t->odt << 2) |
343 (nv_mem_wr_lut_gddr3[e->tWR] & 0xf) << 4;
Ben Skeggs1a7287e2012-01-24 10:24:05 +1000344 t->mr[2] = boot->mr[2];
Roy Splietc7c039f2012-01-09 15:23:07 +1000345
Ben Skeggs77145f12012-07-31 16:16:21 +1000346 NV_DEBUG(drm, "(%u) MR: %08x %08x %08x", t->id,
Ben Skeggs1a7287e2012-01-24 10:24:05 +1000347 t->mr[0], t->mr[1], t->mr[2]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000348 return 0;
Roy Splietc7c039f2012-01-09 15:23:07 +1000349}
350
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000351static int
352nouveau_mem_gddr5_mr(struct drm_device *dev, u32 freq,
353 struct nouveau_pm_tbl_entry *e, u8 len,
354 struct nouveau_pm_memtiming *boot,
355 struct nouveau_pm_memtiming *t)
Roy Splietc7c039f2012-01-09 15:23:07 +1000356{
Ben Skeggs77145f12012-07-31 16:16:21 +1000357 struct nouveau_drm *drm = nouveau_drm(dev);
358
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000359 if (len < 15) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000360 t->drive_strength = boot->drive_strength;
361 t->odt = boot->odt;
362 } else {
363 t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
364 t->odt = e->RAM_FT1 & 0x03;
365 }
366
367 if (e->tCL >= NV_MEM_CL_GDDR5_MAX) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000368 NV_WARN(drm, "(%u) Invalid tCL: %u", t->id, e->tCL);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000369 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000370 }
371
372 if (e->tWR >= NV_MEM_WR_GDDR5_MAX) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000373 NV_WARN(drm, "(%u) Invalid tWR: %u", t->id, e->tWR);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000374 return -ERANGE;
Roy Splietc7c039f2012-01-09 15:23:07 +1000375 }
376
377 if (t->odt > 3) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000378 NV_WARN(drm, "(%u) Invalid odt value, assuming autocal: %x",
Roy Splietc7c039f2012-01-09 15:23:07 +1000379 t->id, t->odt);
380 t->odt = 0;
381 }
382
383 t->mr[0] = (boot->mr[0] & 0x007) |
384 ((e->tCL - 5) << 3) |
385 ((e->tWR - 4) << 8);
386 t->mr[1] = (boot->mr[1] & 0x1007f0) |
387 t->drive_strength |
388 (t->odt << 2);
389
Ben Skeggs77145f12012-07-31 16:16:21 +1000390 NV_DEBUG(drm, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[1]);
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000391 return 0;
Roy Splietc7c039f2012-01-09 15:23:07 +1000392}
393
Ben Skeggs085028c2012-01-18 09:02:28 +1000394int
395nouveau_mem_timing_calc(struct drm_device *dev, u32 freq,
396 struct nouveau_pm_memtiming *t)
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000397{
Ben Skeggs77145f12012-07-31 16:16:21 +1000398 struct nouveau_device *device = nouveau_dev(dev);
399 struct nouveau_fb *pfb = nouveau_fb(device);
400 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs085028c2012-01-18 09:02:28 +1000401 struct nouveau_pm_memtiming *boot = &pm->boot.timing;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000402 struct nouveau_pm_tbl_entry *e;
Ben Skeggs070be292012-01-24 18:30:10 +1000403 u8 ver, len, *ptr, *ramcfg;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000404 int ret;
405
406 ptr = nouveau_perf_timing(dev, freq, &ver, &len);
Ben Skeggs085028c2012-01-18 09:02:28 +1000407 if (!ptr || ptr[0] == 0x00) {
408 *t = *boot;
409 return 0;
410 }
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000411 e = (struct nouveau_pm_tbl_entry *)ptr;
412
Ben Skeggs085028c2012-01-18 09:02:28 +1000413 t->tCWL = boot->tCWL;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000414
Ben Skeggs77145f12012-07-31 16:16:21 +1000415 switch (device->card_type) {
Ben Skeggs085028c2012-01-18 09:02:28 +1000416 case NV_40:
417 ret = nv40_mem_timing_calc(dev, freq, e, len, boot, t);
418 break;
419 case NV_50:
420 ret = nv50_mem_timing_calc(dev, freq, e, len, boot, t);
421 break;
422 case NV_C0:
Ben Skeggsa94ba1f2012-02-06 11:42:29 +1000423 case NV_D0:
Ben Skeggs085028c2012-01-18 09:02:28 +1000424 ret = nvc0_mem_timing_calc(dev, freq, e, len, boot, t);
425 break;
426 default:
427 ret = -ENODEV;
428 break;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000429 }
430
Ben Skeggs77145f12012-07-31 16:16:21 +1000431 switch (pfb->ram.type * !ret) {
Ben Skeggs085028c2012-01-18 09:02:28 +1000432 case NV_MEM_TYPE_GDDR3:
433 ret = nouveau_mem_gddr3_mr(dev, freq, e, len, boot, t);
434 break;
435 case NV_MEM_TYPE_GDDR5:
436 ret = nouveau_mem_gddr5_mr(dev, freq, e, len, boot, t);
437 break;
438 case NV_MEM_TYPE_DDR2:
439 ret = nouveau_mem_ddr2_mr(dev, freq, e, len, boot, t);
440 break;
441 case NV_MEM_TYPE_DDR3:
442 ret = nouveau_mem_ddr3_mr(dev, freq, e, len, boot, t);
443 break;
444 default:
445 ret = -EINVAL;
Ben Skeggs070be292012-01-24 18:30:10 +1000446 break;
447 }
448
449 ramcfg = nouveau_perf_ramcfg(dev, freq, &ver, &len);
450 if (ramcfg) {
451 int dll_off;
452
453 if (ver == 0x00)
454 dll_off = !!(ramcfg[3] & 0x04);
455 else
456 dll_off = !!(ramcfg[2] & 0x40);
457
Ben Skeggs77145f12012-07-31 16:16:21 +1000458 switch (pfb->ram.type) {
Ben Skeggs070be292012-01-24 18:30:10 +1000459 case NV_MEM_TYPE_GDDR3:
460 t->mr[1] &= ~0x00000040;
461 t->mr[1] |= 0x00000040 * dll_off;
462 break;
463 default:
464 t->mr[1] &= ~0x00000001;
465 t->mr[1] |= 0x00000001 * dll_off;
466 break;
467 }
Ben Skeggs085028c2012-01-18 09:02:28 +1000468 }
469
470 return ret;
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000471}
472
473void
474nouveau_mem_timing_read(struct drm_device *dev, struct nouveau_pm_memtiming *t)
Roy Splietc7c039f2012-01-09 15:23:07 +1000475{
Ben Skeggs77145f12012-07-31 16:16:21 +1000476 struct nouveau_device *device = nouveau_dev(dev);
477 struct nouveau_fb *pfb = nouveau_fb(device);
Roy Splietc7c039f2012-01-09 15:23:07 +1000478 u32 timing_base, timing_regs, mr_base;
479 int i;
480
Ben Skeggs77145f12012-07-31 16:16:21 +1000481 if (device->card_type >= 0xC0) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000482 timing_base = 0x10f290;
483 mr_base = 0x10f300;
484 } else {
485 timing_base = 0x100220;
486 mr_base = 0x1002c0;
487 }
488
489 t->id = -1;
490
Ben Skeggs77145f12012-07-31 16:16:21 +1000491 switch (device->card_type) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000492 case NV_50:
493 timing_regs = 9;
494 break;
495 case NV_C0:
496 case NV_D0:
497 timing_regs = 5;
498 break;
499 case NV_30:
500 case NV_40:
501 timing_regs = 3;
502 break;
503 default:
504 timing_regs = 0;
505 return;
506 }
507 for(i = 0; i < timing_regs; i++)
Ben Skeggs77145f12012-07-31 16:16:21 +1000508 t->reg[i] = nv_rd32(device, timing_base + (0x04 * i));
Roy Splietc7c039f2012-01-09 15:23:07 +1000509
510 t->tCWL = 0;
Ben Skeggs77145f12012-07-31 16:16:21 +1000511 if (device->card_type < NV_C0) {
512 t->tCWL = ((nv_rd32(device, 0x100228) & 0x0f000000) >> 24) + 1;
513 } else if (device->card_type <= NV_D0) {
514 t->tCWL = ((nv_rd32(device, 0x10f294) & 0x00000f80) >> 7);
Roy Splietc7c039f2012-01-09 15:23:07 +1000515 }
516
Ben Skeggs77145f12012-07-31 16:16:21 +1000517 t->mr[0] = nv_rd32(device, mr_base);
518 t->mr[1] = nv_rd32(device, mr_base + 0x04);
519 t->mr[2] = nv_rd32(device, mr_base + 0x20);
520 t->mr[3] = nv_rd32(device, mr_base + 0x24);
Roy Splietc7c039f2012-01-09 15:23:07 +1000521
522 t->odt = 0;
523 t->drive_strength = 0;
524
Ben Skeggs77145f12012-07-31 16:16:21 +1000525 switch (pfb->ram.type) {
Roy Splietc7c039f2012-01-09 15:23:07 +1000526 case NV_MEM_TYPE_DDR3:
527 t->odt |= (t->mr[1] & 0x200) >> 7;
528 case NV_MEM_TYPE_DDR2:
529 t->odt |= (t->mr[1] & 0x04) >> 2 |
530 (t->mr[1] & 0x40) >> 5;
531 break;
532 case NV_MEM_TYPE_GDDR3:
533 case NV_MEM_TYPE_GDDR5:
534 t->drive_strength = t->mr[1] & 0x03;
535 t->odt = (t->mr[1] & 0x0c) >> 2;
536 break;
537 default:
538 break;
539 }
540}
541
Ben Skeggsc70c41e2011-12-13 11:57:55 +1000542int
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000543nouveau_mem_exec(struct nouveau_mem_exec_func *exec,
544 struct nouveau_pm_level *perflvl)
545{
Ben Skeggs77145f12012-07-31 16:16:21 +1000546 struct nouveau_drm *drm = nouveau_drm(exec->dev);
547 struct nouveau_device *device = nouveau_dev(exec->dev);
548 struct nouveau_fb *pfb = nouveau_fb(device);
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000549 struct nouveau_pm_memtiming *info = &perflvl->timing;
550 u32 tMRD = 1000, tCKSRE = 0, tCKSRX = 0, tXS = 0, tDLLK = 0;
551 u32 mr[3] = { info->mr[0], info->mr[1], info->mr[2] };
552 u32 mr1_dlloff;
553
Ben Skeggs77145f12012-07-31 16:16:21 +1000554 switch (pfb->ram.type) {
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000555 case NV_MEM_TYPE_DDR2:
556 tDLLK = 2000;
557 mr1_dlloff = 0x00000001;
558 break;
559 case NV_MEM_TYPE_DDR3:
560 tDLLK = 12000;
Ben Skeggs78c20182012-02-06 16:20:30 +1000561 tCKSRE = 2000;
562 tXS = 1000;
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000563 mr1_dlloff = 0x00000001;
564 break;
565 case NV_MEM_TYPE_GDDR3:
566 tDLLK = 40000;
567 mr1_dlloff = 0x00000040;
568 break;
569 default:
Ben Skeggs77145f12012-07-31 16:16:21 +1000570 NV_ERROR(drm, "cannot reclock unsupported memtype\n");
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000571 return -ENODEV;
572 }
573
574 /* fetch current MRs */
Ben Skeggs77145f12012-07-31 16:16:21 +1000575 switch (pfb->ram.type) {
Ben Skeggs1a7287e2012-01-24 10:24:05 +1000576 case NV_MEM_TYPE_GDDR3:
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000577 case NV_MEM_TYPE_DDR3:
578 mr[2] = exec->mrg(exec, 2);
579 default:
580 mr[1] = exec->mrg(exec, 1);
581 mr[0] = exec->mrg(exec, 0);
582 break;
583 }
584
585 /* DLL 'on' -> DLL 'off' mode, disable before entering self-refresh */
586 if (!(mr[1] & mr1_dlloff) && (info->mr[1] & mr1_dlloff)) {
587 exec->precharge(exec);
588 exec->mrs (exec, 1, mr[1] | mr1_dlloff);
589 exec->wait(exec, tMRD);
590 }
591
592 /* enter self-refresh mode */
593 exec->precharge(exec);
594 exec->refresh(exec);
595 exec->refresh(exec);
596 exec->refresh_auto(exec, false);
597 exec->refresh_self(exec, true);
598 exec->wait(exec, tCKSRE);
599
600 /* modify input clock frequency */
601 exec->clock_set(exec);
602
603 /* exit self-refresh mode */
604 exec->wait(exec, tCKSRX);
605 exec->precharge(exec);
606 exec->refresh_self(exec, false);
607 exec->refresh_auto(exec, true);
608 exec->wait(exec, tXS);
Ben Skeggs78c20182012-02-06 16:20:30 +1000609 exec->wait(exec, tXS);
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000610
611 /* update MRs */
612 if (mr[2] != info->mr[2]) {
613 exec->mrs (exec, 2, info->mr[2]);
614 exec->wait(exec, tMRD);
615 }
616
617 if (mr[1] != info->mr[1]) {
Ben Skeggsb8309732012-01-24 13:39:56 +1000618 /* need to keep DLL off until later, at least on GDDR3 */
619 exec->mrs (exec, 1, info->mr[1] | (mr[1] & mr1_dlloff));
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000620 exec->wait(exec, tMRD);
621 }
622
623 if (mr[0] != info->mr[0]) {
624 exec->mrs (exec, 0, info->mr[0]);
625 exec->wait(exec, tMRD);
626 }
627
628 /* update PFB timing registers */
629 exec->timing_set(exec);
630
Ben Skeggsb8309732012-01-24 13:39:56 +1000631 /* DLL (enable + ) reset */
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000632 if (!(info->mr[1] & mr1_dlloff)) {
Ben Skeggsb8309732012-01-24 13:39:56 +1000633 if (mr[1] & mr1_dlloff) {
634 exec->mrs (exec, 1, info->mr[1]);
635 exec->wait(exec, tMRD);
636 }
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000637 exec->mrs (exec, 0, info->mr[0] | 0x00000100);
638 exec->wait(exec, tMRD);
639 exec->mrs (exec, 0, info->mr[0] | 0x00000000);
640 exec->wait(exec, tMRD);
641 exec->wait(exec, tDLLK);
Ben Skeggs77145f12012-07-31 16:16:21 +1000642 if (pfb->ram.type == NV_MEM_TYPE_GDDR3)
Ben Skeggs2d85bc82012-01-23 13:12:09 +1000643 exec->precharge(exec);
644 }
645
646 return 0;
647}