ramgk104.c 47.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2013 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */
24 25
#define gk104_ram(p) container_of((p), struct gk104_ram, base)
#include "ram.h"
26
#include "ramfuc.h"
27

28
#include <core/option.h>
29 30
#include <subdev/bios.h>
#include <subdev/bios/init.h>
31 32
#include <subdev/bios/M0205.h>
#include <subdev/bios/M0209.h>
33 34 35
#include <subdev/bios/pll.h>
#include <subdev/bios/rammap.h>
#include <subdev/bios/timing.h>
36 37
#include <subdev/clk.h>
#include <subdev/clk/pll.h>
38
#include <subdev/gpio.h>
39

40
struct gk104_ramfuc {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	struct ramfuc base;

	struct nvbios_pll refpll;
	struct nvbios_pll mempll;

	struct ramfuc_reg r_gpioMV;
	u32 r_funcMV[2];
	struct ramfuc_reg r_gpio2E;
	u32 r_func2E[2];
	struct ramfuc_reg r_gpiotrig;

	struct ramfuc_reg r_0x132020;
	struct ramfuc_reg r_0x132028;
	struct ramfuc_reg r_0x132024;
	struct ramfuc_reg r_0x132030;
	struct ramfuc_reg r_0x132034;
	struct ramfuc_reg r_0x132000;
	struct ramfuc_reg r_0x132004;
	struct ramfuc_reg r_0x132040;

	struct ramfuc_reg r_0x10f248;
	struct ramfuc_reg r_0x10f290;
	struct ramfuc_reg r_0x10f294;
	struct ramfuc_reg r_0x10f298;
	struct ramfuc_reg r_0x10f29c;
	struct ramfuc_reg r_0x10f2a0;
	struct ramfuc_reg r_0x10f2a4;
	struct ramfuc_reg r_0x10f2a8;
	struct ramfuc_reg r_0x10f2ac;
	struct ramfuc_reg r_0x10f2cc;
	struct ramfuc_reg r_0x10f2e8;
	struct ramfuc_reg r_0x10f250;
	struct ramfuc_reg r_0x10f24c;
	struct ramfuc_reg r_0x10fec4;
	struct ramfuc_reg r_0x10fec8;
	struct ramfuc_reg r_0x10f604;
	struct ramfuc_reg r_0x10f614;
	struct ramfuc_reg r_0x10f610;
	struct ramfuc_reg r_0x100770;
	struct ramfuc_reg r_0x100778;
	struct ramfuc_reg r_0x10f224;

	struct ramfuc_reg r_0x10f870;
	struct ramfuc_reg r_0x10f698;
	struct ramfuc_reg r_0x10f694;
	struct ramfuc_reg r_0x10f6b8;
	struct ramfuc_reg r_0x10f808;
	struct ramfuc_reg r_0x10f670;
	struct ramfuc_reg r_0x10f60c;
	struct ramfuc_reg r_0x10f830;
	struct ramfuc_reg r_0x1373ec;
	struct ramfuc_reg r_0x10f800;
	struct ramfuc_reg r_0x10f82c;

	struct ramfuc_reg r_0x10f978;
	struct ramfuc_reg r_0x10f910;
	struct ramfuc_reg r_0x10f914;

	struct ramfuc_reg r_mr[16]; /* MR0 - MR8, MR15 */

	struct ramfuc_reg r_0x62c000;
102

103
	struct ramfuc_reg r_0x10f200;
104

105 106 107 108 109 110 111 112 113 114 115 116 117
	struct ramfuc_reg r_0x10f210;
	struct ramfuc_reg r_0x10f310;
	struct ramfuc_reg r_0x10f314;
	struct ramfuc_reg r_0x10f318;
	struct ramfuc_reg r_0x10f090;
	struct ramfuc_reg r_0x10f69c;
	struct ramfuc_reg r_0x10f824;
	struct ramfuc_reg r_0x1373f0;
	struct ramfuc_reg r_0x1373f4;
	struct ramfuc_reg r_0x137320;
	struct ramfuc_reg r_0x10f65c;
	struct ramfuc_reg r_0x10f6bc;
	struct ramfuc_reg r_0x100710;
B
Ben Skeggs 已提交
118
	struct ramfuc_reg r_0x100750;
119 120
};

121 122 123
struct gk104_ram {
	struct nvkm_ram base;
	struct gk104_ramfuc fuc;
124

125
	struct list_head cfg;
126 127 128 129
	u32 parts;
	u32 pmask;
	u32 pnuts;

130
	struct nvbios_ramcfg diff;
131 132 133 134 135 136 137 138 139 140
	int from;
	int mode;
	int N1, fN1, M1, P1;
	int N2, M2, P2;
};

/*******************************************************************************
 * GDDR5
 ******************************************************************************/
static void
141
gk104_ram_train(struct gk104_ramfuc *fuc, u32 mask, u32 data)
142
{
143
	struct gk104_ram *ram = container_of(fuc, typeof(*ram), fuc);
144
	u32 addr = 0x110974, i;
145

146 147
	ram_mask(fuc, 0x10f910, mask, data);
	ram_mask(fuc, 0x10f914, mask, data);
148

149
	for (i = 0; (data & 0x80000000) && i < ram->parts; addr += 0x1000, i++) {
150
		if (ram->pmask & (1 << i))
151
			continue;
152 153 154 155 156
		ram_wait(fuc, addr, 0x0000000f, 0x00000000, 500000);
	}
}

static void
157
r1373f4_init(struct gk104_ramfuc *fuc)
158
{
159
	struct gk104_ram *ram = container_of(fuc, typeof(*ram), fuc);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	const u32 mcoef = ((--ram->P2 << 28) | (ram->N2 << 8) | ram->M2);
	const u32 rcoef = ((  ram->P1 << 16) | (ram->N1 << 8) | ram->M1);
	const u32 runk0 = ram->fN1 << 16;
	const u32 runk1 = ram->fN1;

	if (ram->from == 2) {
		ram_mask(fuc, 0x1373f4, 0x00000000, 0x00001100);
		ram_mask(fuc, 0x1373f4, 0x00000000, 0x00000010);
	} else {
		ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010010);
	}

	ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000000);
	ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000000);

	/* (re)program refpll, if required */
	if ((ram_rd32(fuc, 0x132024) & 0xffffffff) != rcoef ||
	    (ram_rd32(fuc, 0x132034) & 0x0000ffff) != runk1) {
		ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
		ram_mask(fuc, 0x132020, 0x00000001, 0x00000000);
		ram_wr32(fuc, 0x137320, 0x00000000);
		ram_mask(fuc, 0x132030, 0xffff0000, runk0);
		ram_mask(fuc, 0x132034, 0x0000ffff, runk1);
		ram_wr32(fuc, 0x132024, rcoef);
		ram_mask(fuc, 0x132028, 0x00080000, 0x00080000);
		ram_mask(fuc, 0x132020, 0x00000001, 0x00000001);
		ram_wait(fuc, 0x137390, 0x00020000, 0x00020000, 64000);
		ram_mask(fuc, 0x132028, 0x00080000, 0x00000000);
	}

	/* (re)program mempll, if required */
	if (ram->mode == 2) {
		ram_mask(fuc, 0x1373f4, 0x00010000, 0x00000000);
B
Ben Skeggs 已提交
193
		ram_mask(fuc, 0x132000, 0x80000000, 0x80000000);
194 195 196 197 198 199 200 201 202 203 204 205 206
		ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
		ram_mask(fuc, 0x132004, 0x103fffff, mcoef);
		ram_mask(fuc, 0x132000, 0x00000001, 0x00000001);
		ram_wait(fuc, 0x137390, 0x00000002, 0x00000002, 64000);
		ram_mask(fuc, 0x1373f4, 0x00000000, 0x00001100);
	} else {
		ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010100);
	}

	ram_mask(fuc, 0x1373f4, 0x00000000, 0x00000010);
}

static void
207
r1373f4_fini(struct gk104_ramfuc *fuc)
208
{
209 210
	struct gk104_ram *ram = container_of(fuc, typeof(*ram), fuc);
	struct nvkm_ram_data *next = ram->base.next;
211 212
	u8 v0 = next->bios.ramcfg_11_03_c0;
	u8 v1 = next->bios.ramcfg_11_03_30;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	u32 tmp;

	tmp = ram_rd32(fuc, 0x1373ec) & ~0x00030000;
	ram_wr32(fuc, 0x1373ec, tmp | (v1 << 16));
	ram_mask(fuc, 0x1373f0, (~ram->mode & 3), 0x00000000);
	if (ram->mode == 2) {
		ram_mask(fuc, 0x1373f4, 0x00000003, 0x000000002);
		ram_mask(fuc, 0x1373f4, 0x00001100, 0x000000000);
	} else {
		ram_mask(fuc, 0x1373f4, 0x00000003, 0x000000001);
		ram_mask(fuc, 0x1373f4, 0x00010000, 0x000000000);
	}
	ram_mask(fuc, 0x10f800, 0x00000030, (v0 ^ v1) << 4);
}

228
static void
229 230
gk104_ram_nuts(struct gk104_ram *ram, struct ramfuc_reg *reg,
	       u32 _mask, u32 _data, u32 _copy)
231
{
232
	struct nvkm_fb *fb = ram->base.fb;
233
	struct ramfuc *fuc = &ram->fuc.base;
234
	struct nvkm_device *device = fb->subdev.device;
235
	u32 addr = 0x110000 + (reg->addr & 0xfff);
236 237 238 239 240 241
	u32 mask = _mask | _copy;
	u32 data = (_data & _mask) | (reg->data & _copy);
	u32 i;

	for (i = 0; i < 16; i++, addr += 0x1000) {
		if (ram->pnuts & (1 << i)) {
242
			u32 prev = nvkm_rd32(device, addr);
243
			u32 next = (prev & ~mask) | data;
244
			nvkm_memx_wr32(fuc->memx, addr, next);
245 246 247 248
		}
	}
}
#define ram_nuts(s,r,m,d,c)                                                    \
249
	gk104_ram_nuts((s), &(s)->fuc.r_##r, (m), (d), (c))
250

251
static int
252
gk104_ram_calc_gddr5(struct gk104_ram *ram, u32 freq)
253
{
254 255
	struct gk104_ramfuc *fuc = &ram->fuc;
	struct nvkm_ram_data *next = ram->base.next;
256 257
	int vc = !next->bios.ramcfg_11_02_08;
	int mv = !next->bios.ramcfg_11_02_04;
258
	u32 mask, data;
259 260

	ram_mask(fuc, 0x10f808, 0x40000000, 0x40000000);
261
	ram_block(fuc);
262 263 264
	ram_wr32(fuc, 0x62c000, 0x0f0f0000);

	/* MR1: turn termination on early, for some reason.. */
265
	if ((ram->base.mr[1] & 0x03c) != 0x030) {
266
		ram_mask(fuc, mr[1], 0x03c, ram->base.mr[1] & 0x03c);
267 268
		ram_nuts(ram, mr[1], 0x03c, ram->base.mr1_nuts & 0x03c, 0x000);
	}
269 270 271 272 273 274 275 276 277 278 279

	if (vc == 1 && ram_have(fuc, gpio2E)) {
		u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[1]);
		if (temp != ram_rd32(fuc, gpio2E)) {
			ram_wr32(fuc, gpiotrig, 1);
			ram_nsec(fuc, 20000);
		}
	}

	ram_mask(fuc, 0x10f200, 0x00000800, 0x00000000);

280
	gk104_ram_train(fuc, 0x01020000, 0x000c0000);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

	ram_wr32(fuc, 0x10f210, 0x00000000); /* REFRESH_AUTO = 0 */
	ram_nsec(fuc, 1000);
	ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
	ram_nsec(fuc, 1000);

	ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
	ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
	ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
	ram_wr32(fuc, 0x10f090, 0x00000061);
	ram_wr32(fuc, 0x10f090, 0xc000007f);
	ram_nsec(fuc, 1000);

	ram_wr32(fuc, 0x10f698, 0x00000000);
	ram_wr32(fuc, 0x10f69c, 0x00000000);

	/*XXX: there does appear to be some kind of condition here, simply
	 *     modifying these bits in the vbios from the default pl0
	 *     entries shows no change.  however, the data does appear to
	 *     be correct and may be required for the transition back
	 */
	mask = 0x800f07e0;
	data = 0x00030000;
	if (ram_rd32(fuc, 0x10f978) & 0x00800000)
		data |= 0x00040000;

	if (1) {
		data |= 0x800807e0;
309 310 311 312 313
		switch (next->bios.ramcfg_11_03_c0) {
		case 3: data &= ~0x00000040; break;
		case 2: data &= ~0x00000100; break;
		case 1: data &= ~0x80000000; break;
		case 0: data &= ~0x00000400; break;
314 315
		}

316 317 318 319 320
		switch (next->bios.ramcfg_11_03_30) {
		case 3: data &= ~0x00000020; break;
		case 2: data &= ~0x00000080; break;
		case 1: data &= ~0x00080000; break;
		case 0: data &= ~0x00000200; break;
321 322 323
		}
	}

324
	if (next->bios.ramcfg_11_02_80)
325
		mask |= 0x03000000;
326
	if (next->bios.ramcfg_11_02_40)
327
		mask |= 0x00002000;
328
	if (next->bios.ramcfg_11_07_10)
329
		mask |= 0x00004000;
330
	if (next->bios.ramcfg_11_07_08)
331 332 333 334 335 336 337 338 339 340 341 342
		mask |= 0x00000003;
	else {
		mask |= 0x34000000;
		if (ram_rd32(fuc, 0x10f978) & 0x00800000)
			mask |= 0x40000000;
	}
	ram_mask(fuc, 0x10f824, mask, data);

	ram_mask(fuc, 0x132040, 0x00010000, 0x00000000);

	if (ram->from == 2 && ram->mode != 2) {
		ram_mask(fuc, 0x10f808, 0x00080000, 0x00000000);
343
		ram_mask(fuc, 0x10f200, 0x18008000, 0x00008000);
344 345 346 347 348
		ram_mask(fuc, 0x10f800, 0x00000000, 0x00000004);
		ram_mask(fuc, 0x10f830, 0x00008000, 0x01040010);
		ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
		r1373f4_init(fuc);
		ram_mask(fuc, 0x1373f0, 0x00000002, 0x00000001);
349
		r1373f4_fini(fuc);
350 351 352 353
		ram_mask(fuc, 0x10f830, 0x00c00000, 0x00240001);
	} else
	if (ram->from != 2 && ram->mode != 2) {
		r1373f4_init(fuc);
354
		r1373f4_fini(fuc);
355 356 357 358 359 360 361 362 363 364
	}

	if (ram_have(fuc, gpioMV)) {
		u32 temp  = ram_mask(fuc, gpioMV, 0x3000, fuc->r_funcMV[mv]);
		if (temp != ram_rd32(fuc, gpioMV)) {
			ram_wr32(fuc, gpiotrig, 1);
			ram_nsec(fuc, 64000);
		}
	}

365 366
	if (next->bios.ramcfg_11_02_40 ||
	    next->bios.ramcfg_11_07_10) {
367 368 369 370 371
		ram_mask(fuc, 0x132040, 0x00010000, 0x00010000);
		ram_nsec(fuc, 20000);
	}

	if (ram->from != 2 && ram->mode == 2) {
372 373
		if (0 /*XXX: Titan */)
			ram_mask(fuc, 0x10f200, 0x18000000, 0x18000000);
374 375 376 377
		ram_mask(fuc, 0x10f800, 0x00000004, 0x00000000);
		ram_mask(fuc, 0x1373f0, 0x00000000, 0x00000002);
		ram_mask(fuc, 0x10f830, 0x00800001, 0x00408010);
		r1373f4_init(fuc);
378
		r1373f4_fini(fuc);
379 380 381 382 383 384
		ram_mask(fuc, 0x10f808, 0x00000000, 0x00080000);
		ram_mask(fuc, 0x10f200, 0x00808000, 0x00800000);
	} else
	if (ram->from == 2 && ram->mode == 2) {
		ram_mask(fuc, 0x10f800, 0x00000004, 0x00000000);
		r1373f4_init(fuc);
385
		r1373f4_fini(fuc);
386 387 388
	}

	if (ram->mode != 2) /*XXX*/ {
389
		if (next->bios.ramcfg_11_07_40)
390 391 392
			ram_mask(fuc, 0x10f670, 0x80000000, 0x80000000);
	}

393 394 395
	ram_wr32(fuc, 0x10f65c, 0x00000011 * next->bios.rammap_11_11_0c);
	ram_wr32(fuc, 0x10f6b8, 0x01010101 * next->bios.ramcfg_11_09);
	ram_wr32(fuc, 0x10f6bc, 0x01010101 * next->bios.ramcfg_11_09);
396

B
Ben Skeggs 已提交
397
	if (!next->bios.ramcfg_11_07_08 && !next->bios.ramcfg_11_07_04) {
398 399
		ram_wr32(fuc, 0x10f698, 0x01010101 * next->bios.ramcfg_11_04);
		ram_wr32(fuc, 0x10f69c, 0x01010101 * next->bios.ramcfg_11_04);
B
Ben Skeggs 已提交
400 401 402 403
	} else
	if (!next->bios.ramcfg_11_07_08) {
		ram_wr32(fuc, 0x10f698, 0x00000000);
		ram_wr32(fuc, 0x10f69c, 0x00000000);
404 405 406
	}

	if (ram->mode != 2) {
407 408 409
		u32 data = 0x01000100 * next->bios.ramcfg_11_04;
		ram_nuke(fuc, 0x10f694);
		ram_mask(fuc, 0x10f694, 0xff00ff00, data);
410 411
	}

412
	if (ram->mode == 2 && next->bios.ramcfg_11_08_10)
413 414 415 416 417 418 419
		data = 0x00000080;
	else
		data = 0x00000000;
	ram_mask(fuc, 0x10f60c, 0x00000080, data);

	mask = 0x00070000;
	data = 0x00000000;
420
	if (!next->bios.ramcfg_11_02_80)
421
		data |= 0x03000000;
422
	if (!next->bios.ramcfg_11_02_40)
423
		data |= 0x00002000;
424
	if (!next->bios.ramcfg_11_07_10)
425
		data |= 0x00004000;
426
	if (!next->bios.ramcfg_11_07_08)
427 428 429 430 431
		data |= 0x00000003;
	else
		data |= 0x74000000;
	ram_mask(fuc, 0x10f824, mask, data);

432
	if (next->bios.ramcfg_11_01_08)
433 434 435 436 437 438 439 440 441 442
		data = 0x00000000;
	else
		data = 0x00001000;
	ram_mask(fuc, 0x10f200, 0x00001000, data);

	if (ram_rd32(fuc, 0x10f670) & 0x80000000) {
		ram_nsec(fuc, 10000);
		ram_mask(fuc, 0x10f670, 0x80000000, 0x00000000);
	}

443
	if (next->bios.ramcfg_11_08_01)
444 445 446 447 448 449
		data = 0x00100000;
	else
		data = 0x00000000;
	ram_mask(fuc, 0x10f82c, 0x00100000, data);

	data = 0x00000000;
450
	if (next->bios.ramcfg_11_08_08)
451
		data |= 0x00002000;
452
	if (next->bios.ramcfg_11_08_04)
453
		data |= 0x00001000;
454
	if (next->bios.ramcfg_11_08_02)
455 456 457 458
		data |= 0x00004000;
	ram_mask(fuc, 0x10f830, 0x00007000, data);

	/* PFB timing */
459 460 461 462 463 464 465 466 467 468 469
	ram_mask(fuc, 0x10f248, 0xffffffff, next->bios.timing[10]);
	ram_mask(fuc, 0x10f290, 0xffffffff, next->bios.timing[0]);
	ram_mask(fuc, 0x10f294, 0xffffffff, next->bios.timing[1]);
	ram_mask(fuc, 0x10f298, 0xffffffff, next->bios.timing[2]);
	ram_mask(fuc, 0x10f29c, 0xffffffff, next->bios.timing[3]);
	ram_mask(fuc, 0x10f2a0, 0xffffffff, next->bios.timing[4]);
	ram_mask(fuc, 0x10f2a4, 0xffffffff, next->bios.timing[5]);
	ram_mask(fuc, 0x10f2a8, 0xffffffff, next->bios.timing[6]);
	ram_mask(fuc, 0x10f2ac, 0xffffffff, next->bios.timing[7]);
	ram_mask(fuc, 0x10f2cc, 0xffffffff, next->bios.timing[8]);
	ram_mask(fuc, 0x10f2e8, 0xffffffff, next->bios.timing[9]);
470

471
	data = mask = 0x00000000;
472
	if (ram->diff.ramcfg_11_08_20) {
473 474 475 476 477 478
		if (next->bios.ramcfg_11_08_20)
			data |= 0x01000000;
		mask |= 0x01000000;
	}
	ram_mask(fuc, 0x10f200, mask, data);

479
	data = mask = 0x00000000;
480
	if (ram->diff.ramcfg_11_02_03) {
481
		data |= next->bios.ramcfg_11_02_03 << 8;
482 483
		mask |= 0x00000300;
	}
484
	if (ram->diff.ramcfg_11_01_10) {
485
		if (next->bios.ramcfg_11_01_10)
486
			data |= 0x70000000;
487 488 489
		mask |= 0x70000000;
	}
	ram_mask(fuc, 0x10f604, mask, data);
490

491
	data = mask = 0x00000000;
492
	if (ram->diff.timing_20_30_07) {
493
		data |= next->bios.timing_20_30_07 << 28;
494 495
		mask |= 0x70000000;
	}
496
	if (ram->diff.ramcfg_11_01_01) {
497
		if (next->bios.ramcfg_11_01_01)
498
			data |= 0x00000100;
499 500 501
		mask |= 0x00000100;
	}
	ram_mask(fuc, 0x10f614, mask, data);
502

503
	data = mask = 0x00000000;
504
	if (ram->diff.timing_20_30_07) {
505
		data |= next->bios.timing_20_30_07 << 28;
506 507
		mask |= 0x70000000;
	}
508
	if (ram->diff.ramcfg_11_01_02) {
509
		if (next->bios.ramcfg_11_01_02)
510
			data |= 0x00000100;
511
		mask |= 0x00000100;
512
	}
513
	ram_mask(fuc, 0x10f610, mask, data);
514 515 516

	mask = 0x33f00000;
	data = 0x00000000;
517
	if (!next->bios.ramcfg_11_01_04)
518
		data |= 0x20200000;
519
	if (!next->bios.ramcfg_11_07_80)
520 521 522 523
		data |= 0x12800000;
	/*XXX: see note above about there probably being some condition
	 *     for the 10f824 stuff that uses ramcfg 3...
	 */
524
	if (next->bios.ramcfg_11_03_f0) {
525
		if (next->bios.rammap_11_08_0c) {
526
			if (!next->bios.ramcfg_11_07_80)
527 528 529 530 531 532 533 534 535 536 537 538
				mask |= 0x00000020;
			else
				data |= 0x00000020;
			mask |= 0x00000004;
		}
	} else {
		mask |= 0x40000020;
		data |= 0x00000004;
	}

	ram_mask(fuc, 0x10f808, mask, data);

539
	ram_wr32(fuc, 0x10f870, 0x11111111 * next->bios.ramcfg_11_03_0f);
540

541
	data = mask = 0x00000000;
542
	if (ram->diff.ramcfg_11_02_03) {
543
		data |= next->bios.ramcfg_11_02_03;
544 545
		mask |= 0x00000003;
	}
546
	if (ram->diff.ramcfg_11_01_10) {
547
		if (next->bios.ramcfg_11_01_10)
548 549 550 551 552 553
			data |= 0x00000004;
		mask |= 0x00000004;
	}

	if ((ram_mask(fuc, 0x100770, mask, data) & mask & 4) != (data & 4)) {
		ram_mask(fuc, 0x100750, 0x00000008, 0x00000008);
554 555 556 557
		ram_wr32(fuc, 0x100710, 0x00000000);
		ram_wait(fuc, 0x100710, 0x80000000, 0x80000000, 200000);
	}

558
	data = next->bios.timing_20_30_07 << 8;
559
	if (next->bios.ramcfg_11_01_01)
560 561 562
		data |= 0x80000000;
	ram_mask(fuc, 0x100778, 0x00000700, data);

563
	ram_mask(fuc, 0x10f250, 0x000003f0, next->bios.timing_20_2c_003f << 4);
564 565 566 567
	data = (next->bios.timing[10] & 0x7f000000) >> 24;
	if (data < next->bios.timing_20_2c_1fc0)
		data = next->bios.timing_20_2c_1fc0;
	ram_mask(fuc, 0x10f24c, 0x7f000000, data << 24);
568
	ram_mask(fuc, 0x10f224, 0x001f0000, next->bios.timing_20_30_f8 << 16);
569

570 571 572 573 574 575
	ram_mask(fuc, 0x10fec4, 0x041e0f07, next->bios.timing_20_31_0800 << 26 |
					    next->bios.timing_20_31_0780 << 17 |
					    next->bios.timing_20_31_0078 << 8 |
					    next->bios.timing_20_31_0007);
	ram_mask(fuc, 0x10fec8, 0x00000027, next->bios.timing_20_31_8000 << 5 |
					    next->bios.timing_20_31_7000);
576 577

	ram_wr32(fuc, 0x10f090, 0x4000007e);
578
	ram_nsec(fuc, 2000);
579 580 581 582
	ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
	ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
	ram_wr32(fuc, 0x10f210, 0x80000000); /* REFRESH_AUTO = 1 */

583
	if (next->bios.ramcfg_11_08_10 && (ram->mode == 2) /*XXX*/) {
584
		u32 temp = ram_mask(fuc, 0x10f294, 0xff000000, 0x24000000);
585
		gk104_ram_train(fuc, 0xbc0e0000, 0xa4010000); /*XXX*/
586 587 588 589 590 591 592 593 594
		ram_nsec(fuc, 1000);
		ram_wr32(fuc, 0x10f294, temp);
	}

	ram_mask(fuc, mr[3], 0xfff, ram->base.mr[3]);
	ram_wr32(fuc, mr[0], ram->base.mr[0]);
	ram_mask(fuc, mr[8], 0xfff, ram->base.mr[8]);
	ram_nsec(fuc, 1000);
	ram_mask(fuc, mr[1], 0xfff, ram->base.mr[1]);
595
	ram_mask(fuc, mr[5], 0xfff, ram->base.mr[5] & ~0x004); /* LP3 later */
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
	ram_mask(fuc, mr[6], 0xfff, ram->base.mr[6]);
	ram_mask(fuc, mr[7], 0xfff, ram->base.mr[7]);

	if (vc == 0 && ram_have(fuc, gpio2E)) {
		u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[0]);
		if (temp != ram_rd32(fuc, gpio2E)) {
			ram_wr32(fuc, gpiotrig, 1);
			ram_nsec(fuc, 20000);
		}
	}

	ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
	ram_wr32(fuc, 0x10f318, 0x00000001); /* NOP? */
	ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
	ram_nsec(fuc, 1000);
611
	ram_nuts(ram, 0x10f200, 0x18808800, 0x00000000, 0x18808800);
612 613 614 615

	data  = ram_rd32(fuc, 0x10f978);
	data &= ~0x00046144;
	data |=  0x0000000b;
616 617
	if (!next->bios.ramcfg_11_07_08) {
		if (!next->bios.ramcfg_11_07_04)
618 619 620 621 622 623 624 625 626 627 628 629 630
			data |= 0x0000200c;
		else
			data |= 0x00000000;
	} else {
		data |= 0x00040044;
	}
	ram_wr32(fuc, 0x10f978, data);

	if (ram->mode == 1) {
		data = ram_rd32(fuc, 0x10f830) | 0x00000001;
		ram_wr32(fuc, 0x10f830, data);
	}

631
	if (!next->bios.ramcfg_11_07_08) {
632
		data = 0x88020000;
633
		if ( next->bios.ramcfg_11_07_04)
634
			data |= 0x10000000;
635
		if (!next->bios.rammap_11_08_10)
636 637 638 639
			data |= 0x00080000;
	} else {
		data = 0xa40e0000;
	}
640
	gk104_ram_train(fuc, 0xbc0f0000, data);
641 642
	if (1) /* XXX: not always? */
		ram_nsec(fuc, 1000);
643 644 645 646 647

	if (ram->mode == 2) { /*XXX*/
		ram_mask(fuc, 0x10f800, 0x00000004, 0x00000004);
	}

648 649 650
	/* LP3 */
	if (ram_mask(fuc, mr[5], 0x004, ram->base.mr[5]) != ram->base.mr[5])
		ram_nsec(fuc, 1000);
651 652 653 654 655 656

	if (ram->mode != 2) {
		ram_mask(fuc, 0x10f830, 0x01000000, 0x01000000);
		ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
	}

657
	if (next->bios.ramcfg_11_07_02)
658
		gk104_ram_train(fuc, 0x80020000, 0x01000000);
659

660
	ram_unblock(fuc);
661 662
	ram_wr32(fuc, 0x62c000, 0x0f0f0f00);

663
	if (next->bios.rammap_11_08_01)
664 665 666 667
		data = 0x00000800;
	else
		data = 0x00000000;
	ram_mask(fuc, 0x10f200, 0x00000800, data);
668
	ram_nuts(ram, 0x10f200, 0x18808800, data, 0x18808800);
669 670 671 672 673 674 675
	return 0;
}

/*******************************************************************************
 * DDR3
 ******************************************************************************/

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
static void
nvkm_sddr3_dll_reset(struct gk104_ramfuc *fuc)
{
	ram_nuke(fuc, mr[0]);
	ram_mask(fuc, mr[0], 0x100, 0x100);
	ram_mask(fuc, mr[0], 0x100, 0x000);
}

static void
nvkm_sddr3_dll_disable(struct gk104_ramfuc *fuc)
{
	u32 mr1_old = ram_rd32(fuc, mr[1]);

	if (!(mr1_old & 0x1)) {
		ram_mask(fuc, mr[1], 0x1, 0x1);
		ram_nsec(fuc, 1000);
	}
}

695
static int
696
gk104_ram_calc_sddr3(struct gk104_ram *ram, u32 freq)
697
{
698
	struct gk104_ramfuc *fuc = &ram->fuc;
699 700 701
	const u32 rcoef = ((  ram->P1 << 16) | (ram->N1 << 8) | ram->M1);
	const u32 runk0 = ram->fN1 << 16;
	const u32 runk1 = ram->fN1;
702
	struct nvkm_ram_data *next = ram->base.next;
703 704
	int vc = !next->bios.ramcfg_11_02_08;
	int mv = !next->bios.ramcfg_11_02_04;
705 706 707
	u32 mask, data;

	ram_mask(fuc, 0x10f808, 0x40000000, 0x40000000);
708
	ram_block(fuc);
709 710 711 712 713 714 715 716 717 718 719
	ram_wr32(fuc, 0x62c000, 0x0f0f0000);

	if (vc == 1 && ram_have(fuc, gpio2E)) {
		u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[1]);
		if (temp != ram_rd32(fuc, gpio2E)) {
			ram_wr32(fuc, gpiotrig, 1);
			ram_nsec(fuc, 20000);
		}
	}

	ram_mask(fuc, 0x10f200, 0x00000800, 0x00000000);
720
	if (next->bios.ramcfg_11_03_f0)
721 722 723
		ram_mask(fuc, 0x10f808, 0x04000000, 0x04000000);

	ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
724 725 726 727

	if (next->bios.ramcfg_DLLoff)
		nvkm_sddr3_dll_disable(fuc);

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
	ram_wr32(fuc, 0x10f210, 0x00000000); /* REFRESH_AUTO = 0 */
	ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
	ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
	ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
	ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
	ram_nsec(fuc, 1000);

	ram_wr32(fuc, 0x10f090, 0x00000060);
	ram_wr32(fuc, 0x10f090, 0xc000007e);

	/*XXX: there does appear to be some kind of condition here, simply
	 *     modifying these bits in the vbios from the default pl0
	 *     entries shows no change.  however, the data does appear to
	 *     be correct and may be required for the transition back
	 */
	mask = 0x00010000;
	data = 0x00010000;

	if (1) {
		mask |= 0x800807e0;
		data |= 0x800807e0;
749 750 751 752 753
		switch (next->bios.ramcfg_11_03_c0) {
		case 3: data &= ~0x00000040; break;
		case 2: data &= ~0x00000100; break;
		case 1: data &= ~0x80000000; break;
		case 0: data &= ~0x00000400; break;
754 755
		}

756 757 758 759 760
		switch (next->bios.ramcfg_11_03_30) {
		case 3: data &= ~0x00000020; break;
		case 2: data &= ~0x00000080; break;
		case 1: data &= ~0x00080000; break;
		case 0: data &= ~0x00000200; break;
761 762 763
		}
	}

764
	if (next->bios.ramcfg_11_02_80)
765
		mask |= 0x03000000;
766
	if (next->bios.ramcfg_11_02_40)
767
		mask |= 0x00002000;
768
	if (next->bios.ramcfg_11_07_10)
769
		mask |= 0x00004000;
770
	if (next->bios.ramcfg_11_07_08)
771 772 773 774 775 776 777 778 779
		mask |= 0x00000003;
	else
		mask |= 0x14000000;
	ram_mask(fuc, 0x10f824, mask, data);

	ram_mask(fuc, 0x132040, 0x00010000, 0x00000000);

	ram_mask(fuc, 0x1373f4, 0x00000000, 0x00010010);
	data  = ram_rd32(fuc, 0x1373ec) & ~0x00030000;
780
	data |= next->bios.ramcfg_11_03_30 << 16;
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
	ram_wr32(fuc, 0x1373ec, data);
	ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000000);
	ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000000);

	/* (re)program refpll, if required */
	if ((ram_rd32(fuc, 0x132024) & 0xffffffff) != rcoef ||
	    (ram_rd32(fuc, 0x132034) & 0x0000ffff) != runk1) {
		ram_mask(fuc, 0x132000, 0x00000001, 0x00000000);
		ram_mask(fuc, 0x132020, 0x00000001, 0x00000000);
		ram_wr32(fuc, 0x137320, 0x00000000);
		ram_mask(fuc, 0x132030, 0xffff0000, runk0);
		ram_mask(fuc, 0x132034, 0x0000ffff, runk1);
		ram_wr32(fuc, 0x132024, rcoef);
		ram_mask(fuc, 0x132028, 0x00080000, 0x00080000);
		ram_mask(fuc, 0x132020, 0x00000001, 0x00000001);
		ram_wait(fuc, 0x137390, 0x00020000, 0x00020000, 64000);
		ram_mask(fuc, 0x132028, 0x00080000, 0x00000000);
	}

	ram_mask(fuc, 0x1373f4, 0x00000010, 0x00000010);
	ram_mask(fuc, 0x1373f4, 0x00000003, 0x00000001);
	ram_mask(fuc, 0x1373f4, 0x00010000, 0x00000000);

	if (ram_have(fuc, gpioMV)) {
		u32 temp  = ram_mask(fuc, gpioMV, 0x3000, fuc->r_funcMV[mv]);
		if (temp != ram_rd32(fuc, gpioMV)) {
			ram_wr32(fuc, gpiotrig, 1);
			ram_nsec(fuc, 64000);
		}
	}

812 813
	if (next->bios.ramcfg_11_02_40 ||
	    next->bios.ramcfg_11_07_10) {
814 815 816 817 818
		ram_mask(fuc, 0x132040, 0x00010000, 0x00010000);
		ram_nsec(fuc, 20000);
	}

	if (ram->mode != 2) /*XXX*/ {
819
		if (next->bios.ramcfg_11_07_40)
820 821 822
			ram_mask(fuc, 0x10f670, 0x80000000, 0x80000000);
	}

823 824 825
	ram_wr32(fuc, 0x10f65c, 0x00000011 * next->bios.rammap_11_11_0c);
	ram_wr32(fuc, 0x10f6b8, 0x01010101 * next->bios.ramcfg_11_09);
	ram_wr32(fuc, 0x10f6bc, 0x01010101 * next->bios.ramcfg_11_09);
826 827 828

	mask = 0x00010000;
	data = 0x00000000;
829
	if (!next->bios.ramcfg_11_02_80)
830
		data |= 0x03000000;
831
	if (!next->bios.ramcfg_11_02_40)
832
		data |= 0x00002000;
833
	if (!next->bios.ramcfg_11_07_10)
834
		data |= 0x00004000;
835
	if (!next->bios.ramcfg_11_07_08)
836 837 838 839 840 841
		data |= 0x00000003;
	else
		data |= 0x14000000;
	ram_mask(fuc, 0x10f824, mask, data);
	ram_nsec(fuc, 1000);

842
	if (next->bios.ramcfg_11_08_01)
843 844 845 846 847 848
		data = 0x00100000;
	else
		data = 0x00000000;
	ram_mask(fuc, 0x10f82c, 0x00100000, data);

	/* PFB timing */
849 850 851 852 853 854 855 856 857 858 859
	ram_mask(fuc, 0x10f248, 0xffffffff, next->bios.timing[10]);
	ram_mask(fuc, 0x10f290, 0xffffffff, next->bios.timing[0]);
	ram_mask(fuc, 0x10f294, 0xffffffff, next->bios.timing[1]);
	ram_mask(fuc, 0x10f298, 0xffffffff, next->bios.timing[2]);
	ram_mask(fuc, 0x10f29c, 0xffffffff, next->bios.timing[3]);
	ram_mask(fuc, 0x10f2a0, 0xffffffff, next->bios.timing[4]);
	ram_mask(fuc, 0x10f2a4, 0xffffffff, next->bios.timing[5]);
	ram_mask(fuc, 0x10f2a8, 0xffffffff, next->bios.timing[6]);
	ram_mask(fuc, 0x10f2ac, 0xffffffff, next->bios.timing[7]);
	ram_mask(fuc, 0x10f2cc, 0xffffffff, next->bios.timing[8]);
	ram_mask(fuc, 0x10f2e8, 0xffffffff, next->bios.timing[9]);
860 861 862

	mask = 0x33f00000;
	data = 0x00000000;
863
	if (!next->bios.ramcfg_11_01_04)
864
		data |= 0x20200000;
865
	if (!next->bios.ramcfg_11_07_80)
866 867 868 869
		data |= 0x12800000;
	/*XXX: see note above about there probably being some condition
	 *     for the 10f824 stuff that uses ramcfg 3...
	 */
870
	if (next->bios.ramcfg_11_03_f0) {
871
		if (next->bios.rammap_11_08_0c) {
872
			if (!next->bios.ramcfg_11_07_80)
873 874 875 876 877 878 879 880 881 882 883 884 885
				mask |= 0x00000020;
			else
				data |= 0x00000020;
			mask |= 0x08000004;
		}
		data |= 0x04000000;
	} else {
		mask |= 0x44000020;
		data |= 0x08000004;
	}

	ram_mask(fuc, 0x10f808, mask, data);

886
	ram_wr32(fuc, 0x10f870, 0x11111111 * next->bios.ramcfg_11_03_0f);
887

888
	ram_mask(fuc, 0x10f250, 0x000003f0, next->bios.timing_20_2c_003f << 4);
889

890
	data = (next->bios.timing[10] & 0x7f000000) >> 24;
891
	if (data < next->bios.timing_20_2c_1fc0)
892
		data = next->bios.timing_20_2c_1fc0;
893 894
	ram_mask(fuc, 0x10f24c, 0x7f000000, data << 24);

895
	ram_mask(fuc, 0x10f224, 0x001f0000, next->bios.timing_20_30_f8 << 16);
896 897 898 899 900 901 902 903 904

	ram_wr32(fuc, 0x10f090, 0x4000007f);
	ram_nsec(fuc, 1000);

	ram_wr32(fuc, 0x10f314, 0x00000001); /* PRECHARGE */
	ram_wr32(fuc, 0x10f310, 0x00000001); /* REFRESH */
	ram_wr32(fuc, 0x10f210, 0x80000000); /* REFRESH_AUTO = 1 */
	ram_nsec(fuc, 1000);

905 906 907 908
	if (!next->bios.ramcfg_DLLoff) {
		ram_mask(fuc, mr[1], 0x1, 0x0);
		nvkm_sddr3_dll_reset(fuc);
	}
909

910 911
	ram_mask(fuc, mr[2], 0x00000fff, ram->base.mr[2]);
	ram_mask(fuc, mr[1], 0xffffffff, ram->base.mr[1]);
912 913 914
	ram_wr32(fuc, mr[0], ram->base.mr[0]);
	ram_nsec(fuc, 1000);

915 916 917 918
	if (!next->bios.ramcfg_DLLoff) {
		nvkm_sddr3_dll_reset(fuc);
		ram_nsec(fuc, 1000);
	}
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937

	if (vc == 0 && ram_have(fuc, gpio2E)) {
		u32 temp  = ram_mask(fuc, gpio2E, 0x3000, fuc->r_func2E[0]);
		if (temp != ram_rd32(fuc, gpio2E)) {
			ram_wr32(fuc, gpiotrig, 1);
			ram_nsec(fuc, 20000);
		}
	}

	if (ram->mode != 2) {
		ram_mask(fuc, 0x10f830, 0x01000000, 0x01000000);
		ram_mask(fuc, 0x10f830, 0x01000000, 0x00000000);
	}

	ram_mask(fuc, 0x10f200, 0x80000000, 0x80000000);
	ram_wr32(fuc, 0x10f318, 0x00000001); /* NOP? */
	ram_mask(fuc, 0x10f200, 0x80000000, 0x00000000);
	ram_nsec(fuc, 1000);

938
	ram_unblock(fuc);
939 940
	ram_wr32(fuc, 0x62c000, 0x0f0f0f00);

941
	if (next->bios.rammap_11_08_01)
942 943 944 945 946 947 948 949 950 951 952 953
		data = 0x00000800;
	else
		data = 0x00000000;
	ram_mask(fuc, 0x10f200, 0x00000800, data);
	return 0;
}

/*******************************************************************************
 * main hooks
 ******************************************************************************/

static int
954
gk104_ram_calc_data(struct gk104_ram *ram, u32 khz, struct nvkm_ram_data *data)
955
{
956
	struct nvkm_subdev *subdev = &ram->base.fb->subdev;
957
	struct nvkm_ram_data *cfg;
958 959 960 961 962 963 964 965
	u32 mhz = khz / 1000;

	list_for_each_entry(cfg, &ram->cfg, head) {
		if (mhz >= cfg->bios.rammap_min &&
		    mhz <= cfg->bios.rammap_max) {
			*data = *cfg;
			data->freq = khz;
			return 0;
966 967 968
		}
	}

969
	nvkm_error(subdev, "ramcfg data for %dMHz not found\n", mhz);
970
	return -EINVAL;
971 972 973
}

static int
974
gk104_ram_calc_xits(struct gk104_ram *ram, struct nvkm_ram_data *next)
975
{
976
	struct gk104_ramfuc *fuc = &ram->fuc;
977
	struct nvkm_subdev *subdev = &ram->base.fb->subdev;
978 979
	int refclk, i;
	int ret;
980

981
	ret = ram_init(fuc, ram->base.fb);
982 983 984
	if (ret)
		return ret;

985
	ram->mode = (next->freq > fuc->refpll.vco1.max_freq) ? 2 : 1;
986 987 988 989 990 991 992 993 994 995
	ram->from = ram_rd32(fuc, 0x1373f4) & 0x0000000f;

	/* XXX: this is *not* what nvidia do.  on fermi nvidia generally
	 * select, based on some unknown condition, one of the two possible
	 * reference frequencies listed in the vbios table for mempll and
	 * program refpll to that frequency.
	 *
	 * so far, i've seen very weird values being chosen by nvidia on
	 * kepler boards, no idea how/why they're chosen.
	 */
996
	refclk = next->freq;
997 998 999 1000
	if (ram->mode == 2)
		refclk = fuc->mempll.refclk;

	/* calculate refpll coefficients */
1001
	ret = gt215_pll_calc(subdev, &fuc->refpll, refclk, &ram->N1,
1002
			     &ram->fN1, &ram->M1, &ram->P1);
1003 1004
	fuc->mempll.refclk = ret;
	if (ret <= 0) {
1005
		nvkm_error(subdev, "unable to calc refpll\n");
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
		return -EINVAL;
	}

	/* calculate mempll coefficients, if we're using it */
	if (ram->mode == 2) {
		/* post-divider doesn't work... the reg takes the values but
		 * appears to completely ignore it.  there *is* a bit at
		 * bit 28 that appears to divide the clock by 2 if set.
		 */
		fuc->mempll.min_p = 1;
		fuc->mempll.max_p = 2;

1018
		ret = gt215_pll_calc(subdev, &fuc->mempll, next->freq,
1019
				     &ram->N2, NULL, &ram->M2, &ram->P2);
1020
		if (ret <= 0) {
1021
			nvkm_error(subdev, "unable to calc mempll\n");
1022 1023 1024 1025 1026 1027 1028 1029
			return -EINVAL;
		}
	}

	for (i = 0; i < ARRAY_SIZE(fuc->r_mr); i++) {
		if (ram_have(fuc, mr[i]))
			ram->base.mr[i] = ram_rd32(fuc, mr[i]);
	}
1030
	ram->base.freq = next->freq;
1031 1032

	switch (ram->base.type) {
1033
	case NVKM_RAM_TYPE_DDR3:
1034
		ret = nvkm_sddr3_calc(&ram->base);
1035
		if (ret == 0)
1036
			ret = gk104_ram_calc_sddr3(ram, next->freq);
1037
		break;
1038
	case NVKM_RAM_TYPE_GDDR5:
1039
		ret = nvkm_gddr5_calc(&ram->base, ram->pnuts != 0);
1040
		if (ret == 0)
1041
			ret = gk104_ram_calc_gddr5(ram, next->freq);
1042 1043 1044 1045 1046 1047 1048 1049 1050
		break;
	default:
		ret = -ENOSYS;
		break;
	}

	return ret;
}

1051
static int
1052
gk104_ram_calc(struct nvkm_ram *base, u32 freq)
1053
{
1054 1055
	struct gk104_ram *ram = gk104_ram(base);
	struct nvkm_clk *clk = ram->base.fb->subdev.device->clk;
1056 1057
	struct nvkm_ram_data *xits = &ram->base.xition;
	struct nvkm_ram_data *copy;
1058 1059 1060
	int ret;

	if (ram->base.next == NULL) {
1061 1062
		ret = gk104_ram_calc_data(ram,
					  nvkm_clk_read(clk, nv_clk_src_mem),
1063
					  &ram->base.former);
1064 1065 1066
		if (ret)
			return ret;

1067
		ret = gk104_ram_calc_data(ram, freq, &ram->base.target);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
		if (ret)
			return ret;

		if (ram->base.target.freq < ram->base.former.freq) {
			*xits = ram->base.target;
			copy = &ram->base.former;
		} else {
			*xits = ram->base.former;
			copy = &ram->base.target;
		}

		xits->bios.ramcfg_11_02_04 = copy->bios.ramcfg_11_02_04;
		xits->bios.ramcfg_11_02_03 = copy->bios.ramcfg_11_02_03;
		xits->bios.timing_20_30_07 = copy->bios.timing_20_30_07;

		ram->base.next = &ram->base.target;
		if (memcmp(xits, &ram->base.former, sizeof(xits->bios)))
			ram->base.next = &ram->base.xition;
	} else {
		BUG_ON(ram->base.next != &ram->base.xition);
		ram->base.next = &ram->base.target;
	}

1091
	return gk104_ram_calc_xits(ram, ram->base.next);
1092 1093
}

1094
static void
1095
gk104_ram_prog_0(struct gk104_ram *ram, u32 freq)
1096
{
1097
	struct nvkm_device *device = ram->base.fb->subdev.device;
1098
	struct nvkm_ram_data *cfg;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	u32 mhz = freq / 1000;
	u32 mask, data;

	list_for_each_entry(cfg, &ram->cfg, head) {
		if (mhz >= cfg->bios.rammap_min &&
		    mhz <= cfg->bios.rammap_max)
			break;
	}

	if (&cfg->head == &ram->cfg)
		return;

	if (mask = 0, data = 0, ram->diff.rammap_11_0a_03fe) {
		data |= cfg->bios.rammap_11_0a_03fe << 12;
		mask |= 0x001ff000;
	}
	if (ram->diff.rammap_11_09_01ff) {
		data |= cfg->bios.rammap_11_09_01ff;
		mask |= 0x000001ff;
	}
1119
	nvkm_mask(device, 0x10f468, mask, data);
1120 1121 1122 1123 1124

	if (mask = 0, data = 0, ram->diff.rammap_11_0a_0400) {
		data |= cfg->bios.rammap_11_0a_0400;
		mask |= 0x00000001;
	}
1125
	nvkm_mask(device, 0x10f420, mask, data);
1126 1127 1128 1129 1130

	if (mask = 0, data = 0, ram->diff.rammap_11_0a_0800) {
		data |= cfg->bios.rammap_11_0a_0800;
		mask |= 0x00000001;
	}
1131
	nvkm_mask(device, 0x10f430, mask, data);
1132 1133 1134 1135 1136

	if (mask = 0, data = 0, ram->diff.rammap_11_0b_01f0) {
		data |= cfg->bios.rammap_11_0b_01f0;
		mask |= 0x0000001f;
	}
1137
	nvkm_mask(device, 0x10f400, mask, data);
1138 1139 1140 1141 1142

	if (mask = 0, data = 0, ram->diff.rammap_11_0b_0200) {
		data |= cfg->bios.rammap_11_0b_0200 << 9;
		mask |= 0x00000200;
	}
1143
	nvkm_mask(device, 0x10f410, mask, data);
1144 1145 1146 1147 1148 1149 1150 1151 1152

	if (mask = 0, data = 0, ram->diff.rammap_11_0d) {
		data |= cfg->bios.rammap_11_0d << 16;
		mask |= 0x00ff0000;
	}
	if (ram->diff.rammap_11_0f) {
		data |= cfg->bios.rammap_11_0f << 8;
		mask |= 0x0000ff00;
	}
1153
	nvkm_mask(device, 0x10f440, mask, data);
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

	if (mask = 0, data = 0, ram->diff.rammap_11_0e) {
		data |= cfg->bios.rammap_11_0e << 8;
		mask |= 0x0000ff00;
	}
	if (ram->diff.rammap_11_0b_0800) {
		data |= cfg->bios.rammap_11_0b_0800 << 7;
		mask |= 0x00000080;
	}
	if (ram->diff.rammap_11_0b_0400) {
		data |= cfg->bios.rammap_11_0b_0400 << 5;
		mask |= 0x00000020;
	}
1167
	nvkm_mask(device, 0x10f444, mask, data);
1168 1169
}

1170
static int
1171
gk104_ram_prog(struct nvkm_ram *base)
1172
{
1173
	struct gk104_ram *ram = gk104_ram(base);
1174
	struct gk104_ramfuc *fuc = &ram->fuc;
1175
	struct nvkm_device *device = ram->base.fb->subdev.device;
1176
	struct nvkm_ram_data *next = ram->base.next;
1177

1178
	if (!nvkm_boolopt(device->cfgopt, "NvMemExec", true)) {
1179 1180 1181 1182
		ram_exec(fuc, false);
		return (ram->base.next == &ram->base.xition);
	}

1183
	gk104_ram_prog_0(ram, 1000);
1184
	ram_exec(fuc, true);
1185
	gk104_ram_prog_0(ram, next->freq);
1186

1187
	return (ram->base.next == &ram->base.xition);
1188 1189 1190
}

static void
1191
gk104_ram_tidy(struct nvkm_ram *base)
1192
{
1193
	struct gk104_ram *ram = gk104_ram(base);
1194
	ram->base.next = NULL;
1195
	ram_exec(&ram->fuc, false);
1196 1197
}

1198
struct gk104_ram_train {
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	u16 mask;
	struct nvbios_M0209S remap;
	struct nvbios_M0209S type00;
	struct nvbios_M0209S type01;
	struct nvbios_M0209S type04;
	struct nvbios_M0209S type06;
	struct nvbios_M0209S type07;
	struct nvbios_M0209S type08;
	struct nvbios_M0209S type09;
};

static int
1211
gk104_ram_train_type(struct nvkm_ram *ram, int i, u8 ramcfg,
1212
		     struct gk104_ram_train *train)
1213
{
1214
	struct nvkm_bios *bios = ram->fb->subdev.device->bios;
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
	struct nvbios_M0205E M0205E;
	struct nvbios_M0205S M0205S;
	struct nvbios_M0209E M0209E;
	struct nvbios_M0209S *remap = &train->remap;
	struct nvbios_M0209S *value;
	u8  ver, hdr, cnt, len;
	u32 data;

	/* determine type of data for this index */
	if (!(data = nvbios_M0205Ep(bios, i, &ver, &hdr, &cnt, &len, &M0205E)))
		return -ENOENT;

	switch (M0205E.type) {
	case 0x00: value = &train->type00; break;
	case 0x01: value = &train->type01; break;
	case 0x04: value = &train->type04; break;
	case 0x06: value = &train->type06; break;
	case 0x07: value = &train->type07; break;
	case 0x08: value = &train->type08; break;
	case 0x09: value = &train->type09; break;
	default:
		return 0;
	}

	/* training data index determined by ramcfg strap */
	if (!(data = nvbios_M0205Sp(bios, i, ramcfg, &ver, &hdr, &M0205S)))
		return -EINVAL;
	i = M0205S.data;

	/* training data format information */
	if (!(data = nvbios_M0209Ep(bios, i, &ver, &hdr, &cnt, &len, &M0209E)))
		return -EINVAL;

	/* ... and the raw data */
	if (!(data = nvbios_M0209Sp(bios, i, 0, &ver, &hdr, value)))
		return -EINVAL;

	if (M0209E.v02_07 == 2) {
		/* of course! why wouldn't we have a pointer to another entry
		 * in the same table, and use the first one as an array of
		 * remap indices...
		 */
		if (!(data = nvbios_M0209Sp(bios, M0209E.v03, 0, &ver, &hdr,
					    remap)))
			return -EINVAL;

		for (i = 0; i < ARRAY_SIZE(value->data); i++)
			value->data[i] = remap->data[value->data[i]];
	} else
	if (M0209E.v02_07 != 1)
		return -EINVAL;

	train->mask |= 1 << M0205E.type;
	return 0;
}

static int
1272
gk104_ram_train_init_0(struct nvkm_ram *ram, struct gk104_ram_train *train)
1273
{
1274
	struct nvkm_subdev *subdev = &ram->fb->subdev;
1275
	struct nvkm_device *device = subdev->device;
1276 1277 1278
	int i, j;

	if ((train->mask & 0x03d3) != 0x03d3) {
1279
		nvkm_warn(subdev, "missing link training data\n");
1280 1281 1282 1283 1284
		return -EINVAL;
	}

	for (i = 0; i < 0x30; i++) {
		for (j = 0; j < 8; j += 4) {
1285 1286
			nvkm_wr32(device, 0x10f968 + j, 0x00000000 | (i << 8));
			nvkm_wr32(device, 0x10f920 + j, 0x00000000 |
1287 1288
						   train->type08.data[i] << 4 |
						   train->type06.data[i]);
1289 1290
			nvkm_wr32(device, 0x10f918 + j, train->type00.data[i]);
			nvkm_wr32(device, 0x10f920 + j, 0x00000100 |
1291 1292
						   train->type09.data[i] << 4 |
						   train->type07.data[i]);
1293
			nvkm_wr32(device, 0x10f918 + j, train->type01.data[i]);
1294 1295 1296 1297 1298
		}
	}

	for (j = 0; j < 8; j += 4) {
		for (i = 0; i < 0x100; i++) {
1299 1300
			nvkm_wr32(device, 0x10f968 + j, i);
			nvkm_wr32(device, 0x10f900 + j, train->type04.data[i]);
1301 1302 1303 1304 1305 1306 1307
		}
	}

	return 0;
}

static int
1308
gk104_ram_train_init(struct nvkm_ram *ram)
1309
{
1310
	u8 ramcfg = nvbios_ramcfg_index(&ram->fb->subdev);
1311
	struct gk104_ram_train *train;
B
Ben Skeggs 已提交
1312
	int ret, i;
1313

B
Ben Skeggs 已提交
1314 1315 1316 1317
	if (!(train = kzalloc(sizeof(*train), GFP_KERNEL)))
		return -ENOMEM;

	for (i = 0; i < 0x100; i++) {
1318
		ret = gk104_ram_train_type(ram, i, ramcfg, train);
B
Ben Skeggs 已提交
1319 1320
		if (ret && ret != -ENOENT)
			break;
1321 1322
	}

1323 1324 1325
	switch (ram->type) {
	case NVKM_RAM_TYPE_GDDR5:
		ret = gk104_ram_train_init_0(ram, train);
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
		break;
	default:
		ret = 0;
		break;
	}

	kfree(train);
	return ret;
}

1336
int
1337
gk104_ram_init(struct nvkm_ram *ram)
1338
{
1339 1340
	struct nvkm_subdev *subdev = &ram->fb->subdev;
	struct nvkm_device *device = subdev->device;
1341
	struct nvkm_bios *bios = device->bios;
1342 1343
	u8  ver, hdr, cnt, len, snr, ssz;
	u32 data, save;
1344
	int i;
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

	/* run a bunch of tables from rammap table.  there's actually
	 * individual pointers for each rammap entry too, but, nvidia
	 * seem to just run the last two entries' scripts early on in
	 * their init, and never again.. we'll just run 'em all once
	 * for now.
	 *
	 * i strongly suspect that each script is for a separate mode
	 * (likely selected by 0x10f65c's lower bits?), and the
	 * binary driver skips the one that's already been setup by
	 * the init tables.
	 */
1357
	data = nvbios_rammapTe(bios, &ver, &hdr, &cnt, &len, &snr, &ssz);
1358 1359 1360
	if (!data || hdr < 0x15)
		return -EINVAL;

1361 1362
	cnt  = nvbios_rd08(bios, data + 0x14); /* guess at count */
	data = nvbios_rd32(bios, data + 0x10); /* guess u32... */
1363
	save = nvkm_rd32(device, 0x10f65c) & 0x000000f0;
1364 1365
	for (i = 0; i < cnt; i++, data += 4) {
		if (i != save >> 4) {
1366
			nvkm_mask(device, 0x10f65c, 0x000000f0, i << 4);
1367
			nvbios_exec(&(struct nvbios_init) {
1368
					.subdev = subdev,
1369
					.bios = bios,
1370
					.offset = nvbios_rd32(bios, data),
1371 1372 1373
					.execute = 1,
				    });
		}
1374
	}
1375 1376 1377 1378
	nvkm_mask(device, 0x10f65c, 0x000000f0, save);
	nvkm_mask(device, 0x10f584, 0x11000000, 0x00000000);
	nvkm_wr32(device, 0x10ecc0, 0xffffffff);
	nvkm_mask(device, 0x10f160, 0x00000010, 0x00000010);
1379

1380
	return gk104_ram_train_init(ram);
1381 1382
}

1383
static int
1384
gk104_ram_ctor_data(struct gk104_ram *ram, u8 ramcfg, int i)
1385
{
1386
	struct nvkm_bios *bios = ram->base.fb->subdev.device->bios;
1387
	struct nvkm_ram_data *cfg;
1388 1389
	struct nvbios_ramcfg *d = &ram->diff;
	struct nvbios_ramcfg *p, *n;
1390 1391 1392 1393 1394 1395
	u8  ver, hdr, cnt, len;
	u32 data;
	int ret;

	if (!(cfg = kmalloc(sizeof(*cfg), GFP_KERNEL)))
		return -ENOMEM;
1396 1397
	p = &list_last_entry(&ram->cfg, typeof(*cfg), head)->bios;
	n = &cfg->bios;
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

	/* memory config data for a range of target frequencies */
	data = nvbios_rammapEp(bios, i, &ver, &hdr, &cnt, &len, &cfg->bios);
	if (ret = -ENOENT, !data)
		goto done;
	if (ret = -ENOSYS, ver != 0x11 || hdr < 0x12)
		goto done;

	/* ... and a portion specific to the attached memory */
	data = nvbios_rammapSp(bios, data, ver, hdr, cnt, len, ramcfg,
			       &ver, &hdr, &cfg->bios);
	if (ret = -EINVAL, !data)
		goto done;
	if (ret = -ENOSYS, ver != 0x11 || hdr < 0x0a)
		goto done;

	/* lookup memory timings, if bios says they're present */
	if (cfg->bios.ramcfg_timing != 0xff) {
		data = nvbios_timingEp(bios, cfg->bios.ramcfg_timing,
				       &ver, &hdr, &cnt, &len,
				       &cfg->bios);
		if (ret = -EINVAL, !data)
			goto done;
		if (ret = -ENOSYS, ver != 0x20 || hdr < 0x33)
			goto done;
	}

	list_add_tail(&cfg->head, &ram->cfg);
1426 1427 1428
	if (ret = 0, i == 0)
		goto done;

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
	d->rammap_11_0a_03fe |= p->rammap_11_0a_03fe != n->rammap_11_0a_03fe;
	d->rammap_11_09_01ff |= p->rammap_11_09_01ff != n->rammap_11_09_01ff;
	d->rammap_11_0a_0400 |= p->rammap_11_0a_0400 != n->rammap_11_0a_0400;
	d->rammap_11_0a_0800 |= p->rammap_11_0a_0800 != n->rammap_11_0a_0800;
	d->rammap_11_0b_01f0 |= p->rammap_11_0b_01f0 != n->rammap_11_0b_01f0;
	d->rammap_11_0b_0200 |= p->rammap_11_0b_0200 != n->rammap_11_0b_0200;
	d->rammap_11_0d |= p->rammap_11_0d != n->rammap_11_0d;
	d->rammap_11_0f |= p->rammap_11_0f != n->rammap_11_0f;
	d->rammap_11_0e |= p->rammap_11_0e != n->rammap_11_0e;
	d->rammap_11_0b_0800 |= p->rammap_11_0b_0800 != n->rammap_11_0b_0800;
	d->rammap_11_0b_0400 |= p->rammap_11_0b_0400 != n->rammap_11_0b_0400;
1440 1441 1442 1443 1444 1445
	d->ramcfg_11_01_01 |= p->ramcfg_11_01_01 != n->ramcfg_11_01_01;
	d->ramcfg_11_01_02 |= p->ramcfg_11_01_02 != n->ramcfg_11_01_02;
	d->ramcfg_11_01_10 |= p->ramcfg_11_01_10 != n->ramcfg_11_01_10;
	d->ramcfg_11_02_03 |= p->ramcfg_11_02_03 != n->ramcfg_11_02_03;
	d->ramcfg_11_08_20 |= p->ramcfg_11_08_20 != n->ramcfg_11_08_20;
	d->timing_20_30_07 |= p->timing_20_30_07 != n->timing_20_30_07;
1446 1447 1448 1449 1450 1451
done:
	if (ret)
		kfree(cfg);
	return ret;
}

1452 1453
static void *
gk104_ram_dtor(struct nvkm_ram *base)
1454
{
1455
	struct gk104_ram *ram = gk104_ram(base);
1456
	struct nvkm_ram_data *cfg, *tmp;
1457 1458 1459 1460 1461

	list_for_each_entry_safe(cfg, tmp, &ram->cfg, head) {
		kfree(cfg);
	}

1462
	return ram;
1463 1464
}

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
static const struct nvkm_ram_func
gk104_ram_func = {
	.dtor = gk104_ram_dtor,
	.init = gk104_ram_init,
	.get = gf100_ram_get,
	.put = gf100_ram_put,
	.calc = gk104_ram_calc,
	.prog = gk104_ram_prog,
	.tidy = gk104_ram_tidy,
};

int
gk104_ram_new(struct nvkm_fb *fb, struct nvkm_ram **pram)
1478
{
1479 1480
	struct nvkm_subdev *subdev = &fb->subdev;
	struct nvkm_device *device = subdev->device;
1481 1482
	struct nvkm_bios *bios = device->bios;
	struct nvkm_gpio *gpio = device->gpio;
1483
	struct dcb_gpio_func func;
1484
	struct gk104_ram *ram;
1485
	int ret, i;
1486
	u8  ramcfg = nvbios_ramcfg_index(subdev);
1487
	u32 tmp;
1488

1489 1490 1491 1492 1493
	if (!(ram = kzalloc(sizeof(*ram), GFP_KERNEL)))
		return -ENOMEM;
	*pram = &ram->base;

	ret = gf100_ram_ctor(&gk104_ram_func, fb, 0x022554, &ram->base);
1494 1495 1496
	if (ret)
		return ret;

1497 1498
	INIT_LIST_HEAD(&ram->cfg);

1499 1500 1501 1502 1503
	/* calculate a mask of differently configured memory partitions,
	 * because, of course reclocking wasn't complicated enough
	 * already without having to treat some of them differently to
	 * the others....
	 */
1504 1505
	ram->parts = nvkm_rd32(device, 0x022438);
	ram->pmask = nvkm_rd32(device, 0x022554);
1506 1507 1508
	ram->pnuts = 0;
	for (i = 0, tmp = 0; i < ram->parts; i++) {
		if (!(ram->pmask & (1 << i))) {
1509
			u32 cfg1 = nvkm_rd32(device, 0x110204 + (i * 0x1000));
1510 1511 1512 1513 1514 1515 1516 1517
			if (tmp && tmp != cfg1) {
				ram->pnuts |= (1 << i);
				continue;
			}
			tmp = cfg1;
		}
	}

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
	/* parse bios data for all rammap table entries up-front, and
	 * build information on whether certain fields differ between
	 * any of the entries.
	 *
	 * the binary driver appears to completely ignore some fields
	 * when all entries contain the same value.  at first, it was
	 * hoped that these were mere optimisations and the bios init
	 * tables had configured as per the values here, but there is
	 * evidence now to suggest that this isn't the case and we do
	 * need to treat this condition as a "don't touch" indicator.
	 */
1529
	for (i = 0; !ret; i++) {
1530
		ret = gk104_ram_ctor_data(ram, ramcfg, i);
1531
		if (ret && ret != -ENOENT) {
1532
			nvkm_error(subdev, "failed to parse ramcfg data\n");
1533 1534 1535 1536 1537
			return ret;
		}
	}

	/* parse bios data for both pll's */
1538 1539
	ret = nvbios_pll_parse(bios, 0x0c, &ram->fuc.refpll);
	if (ret) {
1540
		nvkm_error(subdev, "mclk refpll data not found\n");
1541 1542 1543 1544 1545
		return ret;
	}

	ret = nvbios_pll_parse(bios, 0x04, &ram->fuc.mempll);
	if (ret) {
1546
		nvkm_error(subdev, "mclk pll data not found\n");
1547 1548 1549
		return ret;
	}

1550
	/* lookup memory voltage gpios */
1551
	ret = nvkm_gpio_find(gpio, 0, 0x18, DCB_GPIO_UNUSED, &func);
1552 1553 1554 1555 1556 1557
	if (ret == 0) {
		ram->fuc.r_gpioMV = ramfuc_reg(0x00d610 + (func.line * 0x04));
		ram->fuc.r_funcMV[0] = (func.log[0] ^ 2) << 12;
		ram->fuc.r_funcMV[1] = (func.log[1] ^ 2) << 12;
	}

1558
	ret = nvkm_gpio_find(gpio, 0, 0x2e, DCB_GPIO_UNUSED, &func);
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
	if (ret == 0) {
		ram->fuc.r_gpio2E = ramfuc_reg(0x00d610 + (func.line * 0x04));
		ram->fuc.r_func2E[0] = (func.log[0] ^ 2) << 12;
		ram->fuc.r_func2E[1] = (func.log[1] ^ 2) << 12;
	}

	ram->fuc.r_gpiotrig = ramfuc_reg(0x00d604);

	ram->fuc.r_0x132020 = ramfuc_reg(0x132020);
	ram->fuc.r_0x132028 = ramfuc_reg(0x132028);
	ram->fuc.r_0x132024 = ramfuc_reg(0x132024);
	ram->fuc.r_0x132030 = ramfuc_reg(0x132030);
	ram->fuc.r_0x132034 = ramfuc_reg(0x132034);
	ram->fuc.r_0x132000 = ramfuc_reg(0x132000);
	ram->fuc.r_0x132004 = ramfuc_reg(0x132004);
	ram->fuc.r_0x132040 = ramfuc_reg(0x132040);

	ram->fuc.r_0x10f248 = ramfuc_reg(0x10f248);
	ram->fuc.r_0x10f290 = ramfuc_reg(0x10f290);
	ram->fuc.r_0x10f294 = ramfuc_reg(0x10f294);
	ram->fuc.r_0x10f298 = ramfuc_reg(0x10f298);
	ram->fuc.r_0x10f29c = ramfuc_reg(0x10f29c);
	ram->fuc.r_0x10f2a0 = ramfuc_reg(0x10f2a0);
	ram->fuc.r_0x10f2a4 = ramfuc_reg(0x10f2a4);
	ram->fuc.r_0x10f2a8 = ramfuc_reg(0x10f2a8);
	ram->fuc.r_0x10f2ac = ramfuc_reg(0x10f2ac);
	ram->fuc.r_0x10f2cc = ramfuc_reg(0x10f2cc);
	ram->fuc.r_0x10f2e8 = ramfuc_reg(0x10f2e8);
	ram->fuc.r_0x10f250 = ramfuc_reg(0x10f250);
	ram->fuc.r_0x10f24c = ramfuc_reg(0x10f24c);
	ram->fuc.r_0x10fec4 = ramfuc_reg(0x10fec4);
	ram->fuc.r_0x10fec8 = ramfuc_reg(0x10fec8);
	ram->fuc.r_0x10f604 = ramfuc_reg(0x10f604);
	ram->fuc.r_0x10f614 = ramfuc_reg(0x10f614);
	ram->fuc.r_0x10f610 = ramfuc_reg(0x10f610);
	ram->fuc.r_0x100770 = ramfuc_reg(0x100770);
	ram->fuc.r_0x100778 = ramfuc_reg(0x100778);
	ram->fuc.r_0x10f224 = ramfuc_reg(0x10f224);

	ram->fuc.r_0x10f870 = ramfuc_reg(0x10f870);
	ram->fuc.r_0x10f698 = ramfuc_reg(0x10f698);
	ram->fuc.r_0x10f694 = ramfuc_reg(0x10f694);
	ram->fuc.r_0x10f6b8 = ramfuc_reg(0x10f6b8);
	ram->fuc.r_0x10f808 = ramfuc_reg(0x10f808);
	ram->fuc.r_0x10f670 = ramfuc_reg(0x10f670);
	ram->fuc.r_0x10f60c = ramfuc_reg(0x10f60c);
	ram->fuc.r_0x10f830 = ramfuc_reg(0x10f830);
	ram->fuc.r_0x1373ec = ramfuc_reg(0x1373ec);
	ram->fuc.r_0x10f800 = ramfuc_reg(0x10f800);
	ram->fuc.r_0x10f82c = ramfuc_reg(0x10f82c);

	ram->fuc.r_0x10f978 = ramfuc_reg(0x10f978);
	ram->fuc.r_0x10f910 = ramfuc_reg(0x10f910);
	ram->fuc.r_0x10f914 = ramfuc_reg(0x10f914);

	switch (ram->base.type) {
1615
	case NVKM_RAM_TYPE_GDDR5:
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
		ram->fuc.r_mr[0] = ramfuc_reg(0x10f300);
		ram->fuc.r_mr[1] = ramfuc_reg(0x10f330);
		ram->fuc.r_mr[2] = ramfuc_reg(0x10f334);
		ram->fuc.r_mr[3] = ramfuc_reg(0x10f338);
		ram->fuc.r_mr[4] = ramfuc_reg(0x10f33c);
		ram->fuc.r_mr[5] = ramfuc_reg(0x10f340);
		ram->fuc.r_mr[6] = ramfuc_reg(0x10f344);
		ram->fuc.r_mr[7] = ramfuc_reg(0x10f348);
		ram->fuc.r_mr[8] = ramfuc_reg(0x10f354);
		ram->fuc.r_mr[15] = ramfuc_reg(0x10f34c);
		break;
1627
	case NVKM_RAM_TYPE_DDR3:
1628
		ram->fuc.r_mr[0] = ramfuc_reg(0x10f300);
1629
		ram->fuc.r_mr[1] = ramfuc_reg(0x10f304);
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
		ram->fuc.r_mr[2] = ramfuc_reg(0x10f320);
		break;
	default:
		break;
	}

	ram->fuc.r_0x62c000 = ramfuc_reg(0x62c000);
	ram->fuc.r_0x10f200 = ramfuc_reg(0x10f200);
	ram->fuc.r_0x10f210 = ramfuc_reg(0x10f210);
	ram->fuc.r_0x10f310 = ramfuc_reg(0x10f310);
	ram->fuc.r_0x10f314 = ramfuc_reg(0x10f314);
	ram->fuc.r_0x10f318 = ramfuc_reg(0x10f318);
	ram->fuc.r_0x10f090 = ramfuc_reg(0x10f090);
	ram->fuc.r_0x10f69c = ramfuc_reg(0x10f69c);
	ram->fuc.r_0x10f824 = ramfuc_reg(0x10f824);
	ram->fuc.r_0x1373f0 = ramfuc_reg(0x1373f0);
	ram->fuc.r_0x1373f4 = ramfuc_reg(0x1373f4);
	ram->fuc.r_0x137320 = ramfuc_reg(0x137320);
	ram->fuc.r_0x10f65c = ramfuc_reg(0x10f65c);
	ram->fuc.r_0x10f6bc = ramfuc_reg(0x10f6bc);
	ram->fuc.r_0x100710 = ramfuc_reg(0x100710);
B
Ben Skeggs 已提交
1651
	ram->fuc.r_0x100750 = ramfuc_reg(0x100750);
1652 1653
	return 0;
}