init.c 54.6 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 2012 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
#include <subdev/bios.h>
#include <subdev/bios/bit.h>
26
#include <subdev/bios/bmp.h>
27
#include <subdev/bios/conn.h>
28 29
#include <subdev/bios/dcb.h>
#include <subdev/bios/dp.h>
30
#include <subdev/bios/gpio.h>
31
#include <subdev/bios/init.h>
32
#include <subdev/bios/ramcfg.h>
33

34
#include <subdev/devinit.h>
35
#include <subdev/gpio.h>
36 37 38
#include <subdev/i2c.h>
#include <subdev/vga.h>

39 40
#include <linux/kernel.h>

41
#define bioslog(lvl, fmt, args...) do {                                        \
42
	nvkm_printk(init->subdev, lvl, info, "0x%08x[%c]: "fmt,                \
43 44
		    init->offset, init_exec(init) ?                            \
		    '0' + (init->nested - 1) : ' ', ##args);                   \
45 46
} while(0)
#define cont(fmt, args...) do {                                                \
47
	if (init->subdev->debug >= NV_DBG_TRACE)                               \
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
		printk(fmt, ##args);                                           \
} while(0)
#define trace(fmt, args...) bioslog(TRACE, fmt, ##args)
#define warn(fmt, args...) bioslog(WARN, fmt, ##args)
#define error(fmt, args...) bioslog(ERROR, fmt, ##args)

/******************************************************************************
 * init parser control flow helpers
 *****************************************************************************/

static inline bool
init_exec(struct nvbios_init *init)
{
	return (init->execute == 1) || ((init->execute & 5) == 5);
}

static inline void
init_exec_set(struct nvbios_init *init, bool exec)
{
	if (exec) init->execute &= 0xfd;
	else      init->execute |= 0x02;
}

static inline void
init_exec_inv(struct nvbios_init *init)
{
	init->execute ^= 0x02;
}

static inline void
init_exec_force(struct nvbios_init *init, bool exec)
{
	if (exec) init->execute |= 0x04;
	else      init->execute &= 0xfb;
}

/******************************************************************************
 * init parser wrappers for normal register/i2c/whatever accessors
 *****************************************************************************/

static inline int
init_or(struct nvbios_init *init)
{
91
	if (init_exec(init)) {
92 93
		if (init->or >= 0)
			return init->or;
94 95
		error("script needs OR!!\n");
	}
96 97 98 99 100 101
	return 0;
}

static inline int
init_link(struct nvbios_init *init)
{
102
	if (init_exec(init)) {
103 104
		if (init->link)
			return init->link == 2;
105 106
		error("script needs OR link\n");
	}
107 108 109 110
	return 0;
}

static inline int
111
init_head(struct nvbios_init *init)
112
{
113
	if (init_exec(init)) {
114 115 116
		if (init->head >= 0)
			return init->head;
		error("script needs head\n");
117
	}
118 119 120 121 122 123
	return 0;
}

static u8
init_conn(struct nvbios_init *init)
{
124
	struct nvkm_bios *bios = init->subdev->device->bios;
125 126 127
	struct nvbios_connE connE;
	u8  ver, hdr;
	u32 conn;
128

129 130 131
	if (init_exec(init)) {
		if (init->outp) {
			conn = init->outp->connector;
132
			conn = nvbios_connEp(bios, conn, &ver, &hdr, &connE);
133
			if (conn)
134
				return connE.type;
135 136 137
		}

		error("script needs connector type\n");
138 139
	}

140
	return 0xff;
141 142 143 144 145
}

static inline u32
init_nvreg(struct nvbios_init *init, u32 reg)
{
146
	struct nvkm_devinit *devinit = init->subdev->device->devinit;
147

148 149 150 151 152 153 154 155 156 157 158
	/* C51 (at least) sometimes has the lower bits set which the VBIOS
	 * interprets to mean that access needs to go through certain IO
	 * ports instead.  The NVIDIA binary driver has been seen to access
	 * these through the NV register address, so lets assume we can
	 * do the same
	 */
	reg &= ~0x00000003;

	/* GF8+ display scripts need register addresses mangled a bit to
	 * select a specific CRTC/OR
	 */
159
	if (init->subdev->device->card_type >= NV_50) {
160
		if (reg & 0x80000000) {
161
			reg += init_head(init) * 0x800;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
			reg &= ~0x80000000;
		}

		if (reg & 0x40000000) {
			reg += init_or(init) * 0x800;
			reg &= ~0x40000000;
			if (reg & 0x20000000) {
				reg += init_link(init) * 0x80;
				reg &= ~0x20000000;
			}
		}
	}

	if (reg & ~0x00fffffc)
		warn("unknown bits in register 0x%08x\n", reg);
177

178
	return nvkm_devinit_mmio(devinit, reg);
179 180 181 182 183
}

static u32
init_rd32(struct nvbios_init *init, u32 reg)
{
184
	struct nvkm_device *device = init->subdev->device;
185
	reg = init_nvreg(init, reg);
186
	if (reg != ~0 && init_exec(init))
187
		return nvkm_rd32(device, reg);
188 189 190 191 192 193
	return 0x00000000;
}

static void
init_wr32(struct nvbios_init *init, u32 reg, u32 val)
{
194
	struct nvkm_device *device = init->subdev->device;
195
	reg = init_nvreg(init, reg);
196
	if (reg != ~0 && init_exec(init))
197
		nvkm_wr32(device, reg, val);
198 199 200 201 202
}

static u32
init_mask(struct nvbios_init *init, u32 reg, u32 mask, u32 val)
{
203
	struct nvkm_device *device = init->subdev->device;
204
	reg = init_nvreg(init, reg);
205
	if (reg != ~0 && init_exec(init)) {
206 207
		u32 tmp = nvkm_rd32(device, reg);
		nvkm_wr32(device, reg, (tmp & ~mask) | val);
208 209 210 211 212 213 214 215 216
		return tmp;
	}
	return 0x00000000;
}

static u8
init_rdport(struct nvbios_init *init, u16 port)
{
	if (init_exec(init))
217
		return nvkm_rdport(init->subdev->device, init->head, port);
218 219 220 221 222 223 224
	return 0x00;
}

static void
init_wrport(struct nvbios_init *init, u16 port, u8 value)
{
	if (init_exec(init))
225
		nvkm_wrport(init->subdev->device, init->head, port, value);
226 227 228 229 230
}

static u8
init_rdvgai(struct nvbios_init *init, u16 port, u8 index)
{
231
	struct nvkm_subdev *subdev = init->subdev;
232
	if (init_exec(init)) {
233
		int head = init->head < 0 ? 0 : init->head;
234
		return nvkm_rdvgai(subdev->device, head, port, index);
235 236 237 238 239 240 241
	}
	return 0x00;
}

static void
init_wrvgai(struct nvbios_init *init, u16 port, u8 index, u8 value)
{
242 243
	struct nvkm_device *device = init->subdev->device;

244
	/* force head 0 for updates to cr44, it only exists on first head */
245
	if (device->card_type < NV_50) {
246
		if (port == 0x03d4 && index == 0x44)
247
			init->head = 0;
248 249 250
	}

	if (init_exec(init)) {
251
		int head = init->head < 0 ? 0 : init->head;
252
		nvkm_wrvgai(device, head, port, index, value);
253 254 255
	}

	/* select head 1 if cr44 write selected it */
256
	if (device->card_type < NV_50) {
257
		if (port == 0x03d4 && index == 0x44 && value == 3)
258
			init->head = 1;
259 260 261
	}
}

262
static struct i2c_adapter *
263 264
init_i2c(struct nvbios_init *init, int index)
{
265
	struct nvkm_i2c *i2c = init->subdev->device->i2c;
266
	struct nvkm_i2c_bus *bus;
267 268

	if (index == 0xff) {
269
		index = NVKM_I2C_BUS_PRI;
270
		if (init->outp && init->outp->i2c_upper_default)
271
			index = NVKM_I2C_BUS_SEC;
272 273 274 275 276 277
	} else
	if (index == 0x80) {
		index = NVKM_I2C_BUS_PRI;
	} else
	if (index == 0x81) {
		index = NVKM_I2C_BUS_SEC;
278 279
	}

280 281
	bus = nvkm_i2c_bus_find(i2c, index);
	return bus ? &bus->i2c : NULL;
282 283 284 285 286
}

static int
init_rdi2cr(struct nvbios_init *init, u8 index, u8 addr, u8 reg)
{
287 288 289
	struct i2c_adapter *adap = init_i2c(init, index);
	if (adap && init_exec(init))
		return nvkm_rdi2cr(adap, addr, reg);
290 291 292 293 294 295
	return -ENODEV;
}

static int
init_wri2cr(struct nvbios_init *init, u8 index, u8 addr, u8 reg, u8 val)
{
296 297 298
	struct i2c_adapter *adap = init_i2c(init, index);
	if (adap && init_exec(init))
		return nvkm_wri2cr(adap, addr, reg, val);
299 300 301
	return -ENODEV;
}

302 303 304
static struct nvkm_i2c_aux *
init_aux(struct nvbios_init *init)
{
305
	struct nvkm_i2c *i2c = init->subdev->device->i2c;
306 307 308 309 310 311 312 313
	if (!init->outp) {
		if (init_exec(init))
			error("script needs output for aux\n");
		return NULL;
	}
	return nvkm_i2c_aux_find(i2c, init->outp->i2c_index);
}

314
static u8
315 316
init_rdauxr(struct nvbios_init *init, u32 addr)
{
317
	struct nvkm_i2c_aux *aux = init_aux(init);
318 319
	u8 data;

320 321
	if (aux && init_exec(init)) {
		int ret = nvkm_rdaux(aux, addr, &data, 1);
322 323 324
		if (ret == 0)
			return data;
		trace("auxch read failed with %d\n", ret);
325 326
	}

327
	return 0x00;
328 329 330 331 332
}

static int
init_wrauxr(struct nvbios_init *init, u32 addr, u8 data)
{
333 334 335
	struct nvkm_i2c_aux *aux = init_aux(init);
	if (aux && init_exec(init)) {
		int ret = nvkm_wraux(aux, addr, &data, 1);
336 337 338 339
		if (ret)
			trace("auxch write failed with %d\n", ret);
		return ret;
	}
340 341 342 343 344 345
	return -ENODEV;
}

static void
init_prog_pll(struct nvbios_init *init, u32 id, u32 freq)
{
346
	struct nvkm_devinit *devinit = init->subdev->device->devinit;
347 348
	if (init_exec(init)) {
		int ret = nvkm_devinit_pll_set(devinit, id, freq);
349 350 351 352 353 354 355 356 357 358
		if (ret)
			warn("failed to prog pll 0x%08x to %dkHz\n", id, freq);
	}
}

/******************************************************************************
 * parsing of bios structures that are required to execute init tables
 *****************************************************************************/

static u16
359
init_table(struct nvkm_bios *bios, u16 *len)
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
{
	struct bit_entry bit_I;

	if (!bit_entry(bios, 'I', &bit_I)) {
		*len = bit_I.length;
		return bit_I.offset;
	}

	if (bmp_version(bios) >= 0x0510) {
		*len = 14;
		return bios->bmp_offset + 75;
	}

	return 0x0000;
}

static u16
init_table_(struct nvbios_init *init, u16 offset, const char *name)
{
379
	struct nvkm_bios *bios = init->subdev->device->bios;
380 381 382
	u16 len, data = init_table(bios, &len);
	if (data) {
		if (len >= offset + 2) {
383
			data = nvbios_rd16(bios, data + offset);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
			if (data)
				return data;

			warn("%s pointer invalid\n", name);
			return 0x0000;
		}

		warn("init data too short for %s pointer", name);
		return 0x0000;
	}

	warn("init data not found\n");
	return 0x0000;
}

#define init_script_table(b) init_table_((b), 0x00, "script table")
#define init_macro_index_table(b) init_table_((b), 0x02, "macro index table")
#define init_macro_table(b) init_table_((b), 0x04, "macro table")
#define init_condition_table(b) init_table_((b), 0x06, "condition table")
#define init_io_condition_table(b) init_table_((b), 0x08, "io condition table")
#define init_io_flag_condition_table(b) init_table_((b), 0x0a, "io flag conditon table")
#define init_function_table(b) init_table_((b), 0x0c, "function table")
#define init_xlat_table(b) init_table_((b), 0x10, "xlat table");

static u16
409
init_script(struct nvkm_bios *bios, int index)
410
{
411
	struct nvbios_init init = { .subdev = &bios->subdev };
412
	u16 bmp_ver = bmp_version(bios), data;
413

414 415
	if (bmp_ver && bmp_ver < 0x0510) {
		if (index > 1 || bmp_ver < 0x0100)
416 417
			return 0x0000;

418
		data = bios->bmp_offset + (bmp_ver < 0x0200 ? 14 : 18);
419
		return nvbios_rd16(bios, data + (index * 2));
420 421 422 423
	}

	data = init_script_table(&init);
	if (data)
424
		return nvbios_rd16(bios, data + (index * 2));
425 426 427 428 429

	return 0x0000;
}

static u16
430
init_unknown_script(struct nvkm_bios *bios)
431 432 433
{
	u16 len, data = init_table(bios, &len);
	if (data && len >= 16)
434
		return nvbios_rd16(bios, data + 14);
435 436 437 438 439 440
	return 0x0000;
}

static u8
init_ram_restrict_group_count(struct nvbios_init *init)
{
441
	return nvbios_ramcfg_count(init->subdev->device->bios);
442 443
}

444
static u8
445
init_ram_restrict(struct nvbios_init *init)
446 447 448 449 450 451 452 453 454
{
	/* This appears to be the behaviour of the VBIOS parser, and *is*
	 * important to cache the NV_PEXTDEV_BOOT0 on later chipsets to
	 * avoid fucking up the memory controller (somehow) by reading it
	 * on every INIT_RAM_RESTRICT_ZM_GROUP opcode.
	 *
	 * Preserving the non-caching behaviour on earlier chipsets just
	 * in case *not* re-reading the strap causes similar breakage.
	 */
455
	if (!init->ramcfg || init->subdev->device->bios->version.major < 0x70)
456
		init->ramcfg = 0x80000000 | nvbios_ramcfg_index(init->subdev);
457
	return (init->ramcfg & 0x7fffffff);
458 459 460 461 462
}

static u8
init_xlat_(struct nvbios_init *init, u8 index, u8 offset)
{
463
	struct nvkm_bios *bios = init->subdev->device->bios;
464 465
	u16 table = init_xlat_table(init);
	if (table) {
466
		u16 data = nvbios_rd16(bios, table + (index * 2));
467
		if (data)
468
			return nvbios_rd08(bios, data + offset);
469 470 471 472 473 474 475 476 477 478 479 480
		warn("xlat table pointer %d invalid\n", index);
	}
	return 0x00;
}

/******************************************************************************
 * utility functions used by various init opcode handlers
 *****************************************************************************/

static bool
init_condition_met(struct nvbios_init *init, u8 cond)
{
481
	struct nvkm_bios *bios = init->subdev->device->bios;
482 483
	u16 table = init_condition_table(init);
	if (table) {
484 485 486
		u32 reg = nvbios_rd32(bios, table + (cond * 12) + 0);
		u32 msk = nvbios_rd32(bios, table + (cond * 12) + 4);
		u32 val = nvbios_rd32(bios, table + (cond * 12) + 8);
487 488 489 490 491 492 493 494 495 496
		trace("\t[0x%02x] (R[0x%06x] & 0x%08x) == 0x%08x\n",
		      cond, reg, msk, val);
		return (init_rd32(init, reg) & msk) == val;
	}
	return false;
}

static bool
init_io_condition_met(struct nvbios_init *init, u8 cond)
{
497
	struct nvkm_bios *bios = init->subdev->device->bios;
498 499
	u16 table = init_io_condition_table(init);
	if (table) {
500 501 502 503
		u16 port = nvbios_rd16(bios, table + (cond * 5) + 0);
		u8 index = nvbios_rd08(bios, table + (cond * 5) + 2);
		u8  mask = nvbios_rd08(bios, table + (cond * 5) + 3);
		u8 value = nvbios_rd08(bios, table + (cond * 5) + 4);
504 505 506 507 508 509 510 511 512 513
		trace("\t[0x%02x] (0x%04x[0x%02x] & 0x%02x) == 0x%02x\n",
		      cond, port, index, mask, value);
		return (init_rdvgai(init, port, index) & mask) == value;
	}
	return false;
}

static bool
init_io_flag_condition_met(struct nvbios_init *init, u8 cond)
{
514
	struct nvkm_bios *bios = init->subdev->device->bios;
515 516
	u16 table = init_io_flag_condition_table(init);
	if (table) {
517 518 519 520 521 522 523
		u16 port = nvbios_rd16(bios, table + (cond * 9) + 0);
		u8 index = nvbios_rd08(bios, table + (cond * 9) + 2);
		u8  mask = nvbios_rd08(bios, table + (cond * 9) + 3);
		u8 shift = nvbios_rd08(bios, table + (cond * 9) + 4);
		u16 data = nvbios_rd16(bios, table + (cond * 9) + 5);
		u8 dmask = nvbios_rd08(bios, table + (cond * 9) + 7);
		u8 value = nvbios_rd08(bios, table + (cond * 9) + 8);
524
		u8 ioval = (init_rdvgai(init, port, index) & mask) >> shift;
525
		return (nvbios_rd08(bios, data + ioval) & dmask) == value;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	}
	return false;
}

static inline u32
init_shift(u32 data, u8 shift)
{
	if (shift < 0x80)
		return data >> shift;
	return data << (0x100 - shift);
}

static u32
init_tmds_reg(struct nvbios_init *init, u8 tmds)
{
	/* For mlv < 0x80, it is an index into a table of TMDS base addresses.
	 * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
	 * CR58 for CR57 = 0 to index a table of offsets to the basic
	 * 0x6808b0 address.
	 * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
	 * CR58 for CR57 = 0 to index a table of offsets to the basic
	 * 0x6808b0 address, and then flip the offset by 8.
	 */
	const int pramdac_offset[13] = {
		0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
	const u32 pramdac_table[4] = {
		0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };

	if (tmds >= 0x80) {
		if (init->outp) {
			u32 dacoffset = pramdac_offset[init->outp->or];
			if (tmds == 0x81)
				dacoffset ^= 8;
			return 0x6808b0 + dacoffset;
		}

562 563
		if (init_exec(init))
			error("tmds opcodes need dcb\n");
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
	} else {
		if (tmds < ARRAY_SIZE(pramdac_table))
			return pramdac_table[tmds];

		error("tmds selector 0x%02x unknown\n", tmds);
	}

	return 0;
}

/******************************************************************************
 * init opcode handlers
 *****************************************************************************/

/**
 * init_reserved - stub for various unknown/unused single-byte opcodes
 *
 */
static void
init_reserved(struct nvbios_init *init)
{
585 586
	struct nvkm_bios *bios = init->subdev->device->bios;
	u8 opcode = nvbios_rd08(bios, init->offset);
587 588 589 590 591 592 593 594 595 596 597 598 599
	u8 length, i;

	switch (opcode) {
	case 0xaa:
		length = 4;
		break;
	default:
		length = 1;
		break;
	}

	trace("RESERVED 0x%02x\t", opcode);
	for (i = 1; i < length; i++)
600
		cont(" 0x%02x", nvbios_rd08(bios, init->offset + i));
601 602
	cont("\n");
	init->offset += length;
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
}

/**
 * INIT_DONE - opcode 0x71
 *
 */
static void
init_done(struct nvbios_init *init)
{
	trace("DONE\n");
	init->offset = 0x0000;
}

/**
 * INIT_IO_RESTRICT_PROG - opcode 0x32
 *
 */
static void
init_io_restrict_prog(struct nvbios_init *init)
{
623
	struct nvkm_bios *bios = init->subdev->device->bios;
624 625 626 627 628 629
	u16 port = nvbios_rd16(bios, init->offset + 1);
	u8 index = nvbios_rd08(bios, init->offset + 3);
	u8  mask = nvbios_rd08(bios, init->offset + 4);
	u8 shift = nvbios_rd08(bios, init->offset + 5);
	u8 count = nvbios_rd08(bios, init->offset + 6);
	u32  reg = nvbios_rd32(bios, init->offset + 7);
630 631 632 633 634 635 636 637 638
	u8 conf, i;

	trace("IO_RESTRICT_PROG\tR[0x%06x] = "
	      "((0x%04x[0x%02x] & 0x%02x) >> %d) [{\n",
	      reg, port, index, mask, shift);
	init->offset += 11;

	conf = (init_rdvgai(init, port, index) & mask) >> shift;
	for (i = 0; i < count; i++) {
639
		u32 data = nvbios_rd32(bios, init->offset);
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

		if (i == conf) {
			trace("\t0x%08x *\n", data);
			init_wr32(init, reg, data);
		} else {
			trace("\t0x%08x\n", data);
		}

		init->offset += 4;
	}
	trace("}]\n");
}

/**
 * INIT_REPEAT - opcode 0x33
 *
 */
static void
init_repeat(struct nvbios_init *init)
{
660
	struct nvkm_bios *bios = init->subdev->device->bios;
661
	u8 count = nvbios_rd08(bios, init->offset + 1);
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	u16 repeat = init->repeat;

	trace("REPEAT\t0x%02x\n", count);
	init->offset += 2;

	init->repeat = init->offset;
	init->repend = init->offset;
	while (count--) {
		init->offset = init->repeat;
		nvbios_exec(init);
		if (count)
			trace("REPEAT\t0x%02x\n", count);
	}
	init->offset = init->repend;
	init->repeat = repeat;
}

/**
 * INIT_IO_RESTRICT_PLL - opcode 0x34
 *
 */
static void
init_io_restrict_pll(struct nvbios_init *init)
{
686
	struct nvkm_bios *bios = init->subdev->device->bios;
687 688 689 690 691 692 693
	u16 port = nvbios_rd16(bios, init->offset + 1);
	u8 index = nvbios_rd08(bios, init->offset + 3);
	u8  mask = nvbios_rd08(bios, init->offset + 4);
	u8 shift = nvbios_rd08(bios, init->offset + 5);
	s8  iofc = nvbios_rd08(bios, init->offset + 6);
	u8 count = nvbios_rd08(bios, init->offset + 7);
	u32  reg = nvbios_rd32(bios, init->offset + 8);
694 695 696 697 698 699 700 701 702
	u8 conf, i;

	trace("IO_RESTRICT_PLL\tR[0x%06x] =PLL= "
	      "((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) IOFCOND 0x%02x [{\n",
	      reg, port, index, mask, shift, iofc);
	init->offset += 12;

	conf = (init_rdvgai(init, port, index) & mask) >> shift;
	for (i = 0; i < count; i++) {
703
		u32 freq = nvbios_rd16(bios, init->offset) * 10;
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

		if (i == conf) {
			trace("\t%dkHz *\n", freq);
			if (iofc > 0 && init_io_flag_condition_met(init, iofc))
				freq *= 2;
			init_prog_pll(init, reg, freq);
		} else {
			trace("\t%dkHz\n", freq);
		}

		init->offset += 2;
	}
	trace("}]\n");
}

/**
 * INIT_END_REPEAT - opcode 0x36
 *
 */
static void
init_end_repeat(struct nvbios_init *init)
{
	trace("END_REPEAT\n");
	init->offset += 1;

	if (init->repeat) {
		init->repend = init->offset;
		init->offset = 0;
	}
}

/**
 * INIT_COPY - opcode 0x37
 *
 */
static void
init_copy(struct nvbios_init *init)
{
742
	struct nvkm_bios *bios = init->subdev->device->bios;
743 744 745 746 747 748
	u32  reg = nvbios_rd32(bios, init->offset + 1);
	u8 shift = nvbios_rd08(bios, init->offset + 5);
	u8 smask = nvbios_rd08(bios, init->offset + 6);
	u16 port = nvbios_rd16(bios, init->offset + 7);
	u8 index = nvbios_rd08(bios, init->offset + 9);
	u8  mask = nvbios_rd08(bios, init->offset + 10);
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
	u8  data;

	trace("COPY\t0x%04x[0x%02x] &= 0x%02x |= "
	      "((R[0x%06x] %s 0x%02x) & 0x%02x)\n",
	      port, index, mask, reg, (shift & 0x80) ? "<<" : ">>",
	      (shift & 0x80) ? (0x100 - shift) : shift, smask);
	init->offset += 11;

	data  = init_rdvgai(init, port, index) & mask;
	data |= init_shift(init_rd32(init, reg), shift) & smask;
	init_wrvgai(init, port, index, data);
}

/**
 * INIT_NOT - opcode 0x38
 *
 */
static void
init_not(struct nvbios_init *init)
{
	trace("NOT\n");
	init->offset += 1;
	init_exec_inv(init);
}

/**
 * INIT_IO_FLAG_CONDITION - opcode 0x39
 *
 */
static void
init_io_flag_condition(struct nvbios_init *init)
{
781
	struct nvkm_bios *bios = init->subdev->device->bios;
782
	u8 cond = nvbios_rd08(bios, init->offset + 1);
783 784 785 786 787 788 789 790 791

	trace("IO_FLAG_CONDITION\t0x%02x\n", cond);
	init->offset += 2;

	if (!init_io_flag_condition_met(init, cond))
		init_exec_set(init, false);
}

/**
792
 * INIT_GENERIC_CONDITION - opcode 0x3a
793 794 795
 *
 */
static void
796
init_generic_condition(struct nvbios_init *init)
797
{
798
	struct nvkm_bios *bios = init->subdev->device->bios;
799
	struct nvbios_dpout info;
800
	u8  cond = nvbios_rd08(bios, init->offset + 1);
801
	u8  size = nvbios_rd08(bios, init->offset + 2);
802
	u8  ver, hdr, cnt, len;
803 804
	u16 data;

805
	trace("GENERIC_CONDITION\t0x%02x 0x%02x\n", cond, size);
806 807 808 809 810 811 812 813 814 815
	init->offset += 3;

	switch (cond) {
	case 0:
		if (init_conn(init) != DCB_CONNECTOR_eDP)
			init_exec_set(init, false);
		break;
	case 1:
	case 2:
		if ( init->outp &&
816 817 818 819 820 821
		    (data = nvbios_dpout_match(bios, DCB_OUTPUT_DP,
					       (init->outp->or << 0) |
					       (init->outp->sorconf.link << 6),
					       &ver, &hdr, &cnt, &len, &info)))
		{
			if (!(info.flags & cond))
822 823 824 825
				init_exec_set(init, false);
			break;
		}

826 827
		if (init_exec(init))
			warn("script needs dp output table data\n");
828 829 830 831 832 833
		break;
	case 5:
		if (!(init_rdauxr(init, 0x0d) & 1))
			init_exec_set(init, false);
		break;
	default:
834
		warn("INIT_GENERIC_CONDITON: unknown 0x%02x\n", cond);
835
		init->offset += size;
836 837 838 839 840 841 842 843 844 845 846
		break;
	}
}

/**
 * INIT_IO_MASK_OR - opcode 0x3b
 *
 */
static void
init_io_mask_or(struct nvbios_init *init)
{
847
	struct nvkm_bios *bios = init->subdev->device->bios;
848
	u8 index = nvbios_rd08(bios, init->offset + 1);
849 850 851
	u8    or = init_or(init);
	u8  data;

852
	trace("IO_MASK_OR\t0x03d4[0x%02x] &= ~(1 << 0x%02x)\n", index, or);
853 854 855 856 857 858 859 860 861 862 863 864 865
	init->offset += 2;

	data = init_rdvgai(init, 0x03d4, index);
	init_wrvgai(init, 0x03d4, index, data &= ~(1 << or));
}

/**
 * INIT_IO_OR - opcode 0x3c
 *
 */
static void
init_io_or(struct nvbios_init *init)
{
866
	struct nvkm_bios *bios = init->subdev->device->bios;
867
	u8 index = nvbios_rd08(bios, init->offset + 1);
868 869 870
	u8    or = init_or(init);
	u8  data;

871
	trace("IO_OR\t0x03d4[0x%02x] |= (1 << 0x%02x)\n", index, or);
872 873 874 875 876 877
	init->offset += 2;

	data = init_rdvgai(init, 0x03d4, index);
	init_wrvgai(init, 0x03d4, index, data | (1 << or));
}

878 879 880 881 882 883 884
/**
 * INIT_ANDN_REG - opcode 0x47
 *
 */
static void
init_andn_reg(struct nvbios_init *init)
{
885
	struct nvkm_bios *bios = init->subdev->device->bios;
886 887
	u32  reg = nvbios_rd32(bios, init->offset + 1);
	u32 mask = nvbios_rd32(bios, init->offset + 5);
888 889 890 891 892 893 894 895 896 897 898 899 900 901

	trace("ANDN_REG\tR[0x%06x] &= ~0x%08x\n", reg, mask);
	init->offset += 9;

	init_mask(init, reg, mask, 0);
}

/**
 * INIT_OR_REG - opcode 0x48
 *
 */
static void
init_or_reg(struct nvbios_init *init)
{
902
	struct nvkm_bios *bios = init->subdev->device->bios;
903 904
	u32  reg = nvbios_rd32(bios, init->offset + 1);
	u32 mask = nvbios_rd32(bios, init->offset + 5);
905 906 907 908 909 910 911

	trace("OR_REG\tR[0x%06x] |= 0x%08x\n", reg, mask);
	init->offset += 9;

	init_mask(init, reg, 0, mask);
}

912 913 914 915 916 917 918
/**
 * INIT_INDEX_ADDRESS_LATCHED - opcode 0x49
 *
 */
static void
init_idx_addr_latched(struct nvbios_init *init)
{
919
	struct nvkm_bios *bios = init->subdev->device->bios;
920 921 922 923 924
	u32 creg = nvbios_rd32(bios, init->offset + 1);
	u32 dreg = nvbios_rd32(bios, init->offset + 5);
	u32 mask = nvbios_rd32(bios, init->offset + 9);
	u32 data = nvbios_rd32(bios, init->offset + 13);
	u8 count = nvbios_rd08(bios, init->offset + 17);
925

926 927
	trace("INDEX_ADDRESS_LATCHED\tR[0x%06x] : R[0x%06x]\n", creg, dreg);
	trace("\tCTRL &= 0x%08x |= 0x%08x\n", mask, data);
928 929 930
	init->offset += 18;

	while (count--) {
931 932
		u8 iaddr = nvbios_rd08(bios, init->offset + 0);
		u8 idata = nvbios_rd08(bios, init->offset + 1);
933 934 935 936 937

		trace("\t[0x%02x] = 0x%02x\n", iaddr, idata);
		init->offset += 2;

		init_wr32(init, dreg, idata);
938
		init_mask(init, creg, ~mask, data | iaddr);
939 940 941 942 943 944 945 946 947 948
	}
}

/**
 * INIT_IO_RESTRICT_PLL2 - opcode 0x4a
 *
 */
static void
init_io_restrict_pll2(struct nvbios_init *init)
{
949
	struct nvkm_bios *bios = init->subdev->device->bios;
950 951 952 953 954 955
	u16 port = nvbios_rd16(bios, init->offset + 1);
	u8 index = nvbios_rd08(bios, init->offset + 3);
	u8  mask = nvbios_rd08(bios, init->offset + 4);
	u8 shift = nvbios_rd08(bios, init->offset + 5);
	u8 count = nvbios_rd08(bios, init->offset + 6);
	u32  reg = nvbios_rd32(bios, init->offset + 7);
956 957 958 959 960 961 962 963 964
	u8  conf, i;

	trace("IO_RESTRICT_PLL2\t"
	      "R[0x%06x] =PLL= ((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) [{\n",
	      reg, port, index, mask, shift);
	init->offset += 11;

	conf = (init_rdvgai(init, port, index) & mask) >> shift;
	for (i = 0; i < count; i++) {
965
		u32 freq = nvbios_rd32(bios, init->offset);
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
		if (i == conf) {
			trace("\t%dkHz *\n", freq);
			init_prog_pll(init, reg, freq);
		} else {
			trace("\t%dkHz\n", freq);
		}
		init->offset += 4;
	}
	trace("}]\n");
}

/**
 * INIT_PLL2 - opcode 0x4b
 *
 */
static void
init_pll2(struct nvbios_init *init)
{
984
	struct nvkm_bios *bios = init->subdev->device->bios;
985 986
	u32  reg = nvbios_rd32(bios, init->offset + 1);
	u32 freq = nvbios_rd32(bios, init->offset + 5);
987 988 989 990 991 992 993 994 995 996 997 998 999 1000

	trace("PLL2\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
	init->offset += 9;

	init_prog_pll(init, reg, freq);
}

/**
 * INIT_I2C_BYTE - opcode 0x4c
 *
 */
static void
init_i2c_byte(struct nvbios_init *init)
{
1001
	struct nvkm_bios *bios = init->subdev->device->bios;
1002 1003 1004
	u8 index = nvbios_rd08(bios, init->offset + 1);
	u8  addr = nvbios_rd08(bios, init->offset + 2) >> 1;
	u8 count = nvbios_rd08(bios, init->offset + 3);
1005 1006 1007 1008 1009

	trace("I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
	init->offset += 4;

	while (count--) {
1010 1011 1012
		u8  reg = nvbios_rd08(bios, init->offset + 0);
		u8 mask = nvbios_rd08(bios, init->offset + 1);
		u8 data = nvbios_rd08(bios, init->offset + 2);
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
		int val;

		trace("\t[0x%02x] &= 0x%02x |= 0x%02x\n", reg, mask, data);
		init->offset += 3;

		val = init_rdi2cr(init, index, addr, reg);
		if (val < 0)
			continue;
		init_wri2cr(init, index, addr, reg, (val & mask) | data);
	}
}

/**
 * INIT_ZM_I2C_BYTE - opcode 0x4d
 *
 */
static void
init_zm_i2c_byte(struct nvbios_init *init)
{
1032
	struct nvkm_bios *bios = init->subdev->device->bios;
1033 1034 1035
	u8 index = nvbios_rd08(bios, init->offset + 1);
	u8  addr = nvbios_rd08(bios, init->offset + 2) >> 1;
	u8 count = nvbios_rd08(bios, init->offset + 3);
1036 1037 1038 1039 1040

	trace("ZM_I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
	init->offset += 4;

	while (count--) {
1041 1042
		u8  reg = nvbios_rd08(bios, init->offset + 0);
		u8 data = nvbios_rd08(bios, init->offset + 1);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

		trace("\t[0x%02x] = 0x%02x\n", reg, data);
		init->offset += 2;

		init_wri2cr(init, index, addr, reg, data);
	}
}

/**
 * INIT_ZM_I2C - opcode 0x4e
 *
 */
static void
init_zm_i2c(struct nvbios_init *init)
{
1058
	struct nvkm_bios *bios = init->subdev->device->bios;
1059 1060 1061
	u8 index = nvbios_rd08(bios, init->offset + 1);
	u8  addr = nvbios_rd08(bios, init->offset + 2) >> 1;
	u8 count = nvbios_rd08(bios, init->offset + 3);
1062 1063 1064 1065 1066 1067
	u8 data[256], i;

	trace("ZM_I2C\tI2C[0x%02x][0x%02x]\n", index, addr);
	init->offset += 4;

	for (i = 0; i < count; i++) {
1068
		data[i] = nvbios_rd08(bios, init->offset);
1069 1070 1071 1072 1073
		trace("\t0x%02x\n", data[i]);
		init->offset++;
	}

	if (init_exec(init)) {
1074
		struct i2c_adapter *adap = init_i2c(init, index);
1075 1076 1077 1078 1079
		struct i2c_msg msg = {
			.addr = addr, .flags = 0, .len = count, .buf = data,
		};
		int ret;

1080
		if (adap && (ret = i2c_transfer(adap, &msg, 1)) != 1)
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
			warn("i2c wr failed, %d\n", ret);
	}
}

/**
 * INIT_TMDS - opcode 0x4f
 *
 */
static void
init_tmds(struct nvbios_init *init)
{
1092
	struct nvkm_bios *bios = init->subdev->device->bios;
1093 1094 1095 1096
	u8 tmds = nvbios_rd08(bios, init->offset + 1);
	u8 addr = nvbios_rd08(bios, init->offset + 2);
	u8 mask = nvbios_rd08(bios, init->offset + 3);
	u8 data = nvbios_rd08(bios, init->offset + 4);
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	u32 reg = init_tmds_reg(init, tmds);

	trace("TMDS\tT[0x%02x][0x%02x] &= 0x%02x |= 0x%02x\n",
	      tmds, addr, mask, data);
	init->offset += 5;

	if (reg == 0)
		return;

	init_wr32(init, reg + 0, addr | 0x00010000);
	init_wr32(init, reg + 4, data | (init_rd32(init, reg + 4) & mask));
	init_wr32(init, reg + 0, addr);
}

/**
 * INIT_ZM_TMDS_GROUP - opcode 0x50
 *
 */
static void
init_zm_tmds_group(struct nvbios_init *init)
{
1118
	struct nvkm_bios *bios = init->subdev->device->bios;
1119 1120
	u8  tmds = nvbios_rd08(bios, init->offset + 1);
	u8 count = nvbios_rd08(bios, init->offset + 2);
1121 1122 1123 1124 1125 1126
	u32  reg = init_tmds_reg(init, tmds);

	trace("TMDS_ZM_GROUP\tT[0x%02x]\n", tmds);
	init->offset += 3;

	while (count--) {
1127 1128
		u8 addr = nvbios_rd08(bios, init->offset + 0);
		u8 data = nvbios_rd08(bios, init->offset + 1);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

		trace("\t[0x%02x] = 0x%02x\n", addr, data);
		init->offset += 2;

		init_wr32(init, reg + 4, data);
		init_wr32(init, reg + 0, addr);
	}
}

/**
 * INIT_CR_INDEX_ADDRESS_LATCHED - opcode 0x51
 *
 */
static void
init_cr_idx_adr_latch(struct nvbios_init *init)
{
1145
	struct nvkm_bios *bios = init->subdev->device->bios;
1146 1147 1148 1149
	u8 addr0 = nvbios_rd08(bios, init->offset + 1);
	u8 addr1 = nvbios_rd08(bios, init->offset + 2);
	u8  base = nvbios_rd08(bios, init->offset + 3);
	u8 count = nvbios_rd08(bios, init->offset + 4);
1150 1151 1152 1153 1154 1155 1156
	u8 save0;

	trace("CR_INDEX_ADDR C[%02x] C[%02x]\n", addr0, addr1);
	init->offset += 5;

	save0 = init_rdvgai(init, 0x03d4, addr0);
	while (count--) {
1157
		u8 data = nvbios_rd08(bios, init->offset);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

		trace("\t\t[0x%02x] = 0x%02x\n", base, data);
		init->offset += 1;

		init_wrvgai(init, 0x03d4, addr0, base++);
		init_wrvgai(init, 0x03d4, addr1, data);
	}
	init_wrvgai(init, 0x03d4, addr0, save0);
}

/**
 * INIT_CR - opcode 0x52
 *
 */
static void
init_cr(struct nvbios_init *init)
{
1175
	struct nvkm_bios *bios = init->subdev->device->bios;
1176 1177 1178
	u8 addr = nvbios_rd08(bios, init->offset + 1);
	u8 mask = nvbios_rd08(bios, init->offset + 2);
	u8 data = nvbios_rd08(bios, init->offset + 3);
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	u8 val;

	trace("CR\t\tC[0x%02x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
	init->offset += 4;

	val = init_rdvgai(init, 0x03d4, addr) & mask;
	init_wrvgai(init, 0x03d4, addr, val | data);
}

/**
 * INIT_ZM_CR - opcode 0x53
 *
 */
static void
init_zm_cr(struct nvbios_init *init)
{
1195
	struct nvkm_bios *bios = init->subdev->device->bios;
1196 1197
	u8 addr = nvbios_rd08(bios, init->offset + 1);
	u8 data = nvbios_rd08(bios, init->offset + 2);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

	trace("ZM_CR\tC[0x%02x] = 0x%02x\n", addr,  data);
	init->offset += 3;

	init_wrvgai(init, 0x03d4, addr, data);
}

/**
 * INIT_ZM_CR_GROUP - opcode 0x54
 *
 */
static void
init_zm_cr_group(struct nvbios_init *init)
{
1212
	struct nvkm_bios *bios = init->subdev->device->bios;
1213
	u8 count = nvbios_rd08(bios, init->offset + 1);
1214 1215 1216 1217 1218

	trace("ZM_CR_GROUP\n");
	init->offset += 2;

	while (count--) {
1219 1220
		u8 addr = nvbios_rd08(bios, init->offset + 0);
		u8 data = nvbios_rd08(bios, init->offset + 1);
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

		trace("\t\tC[0x%02x] = 0x%02x\n", addr, data);
		init->offset += 2;

		init_wrvgai(init, 0x03d4, addr, data);
	}
}

/**
 * INIT_CONDITION_TIME - opcode 0x56
 *
 */
static void
init_condition_time(struct nvbios_init *init)
{
1236
	struct nvkm_bios *bios = init->subdev->device->bios;
1237 1238
	u8  cond = nvbios_rd08(bios, init->offset + 1);
	u8 retry = nvbios_rd08(bios, init->offset + 2);
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	u8  wait = min((u16)retry * 50, 100);

	trace("CONDITION_TIME\t0x%02x 0x%02x\n", cond, retry);
	init->offset += 3;

	if (!init_exec(init))
		return;

	while (wait--) {
		if (init_condition_met(init, cond))
			return;
		mdelay(20);
	}

	init_exec_set(init, false);
}

/**
 * INIT_LTIME - opcode 0x57
 *
 */
static void
init_ltime(struct nvbios_init *init)
{
1263
	struct nvkm_bios *bios = init->subdev->device->bios;
1264
	u16 msec = nvbios_rd16(bios, init->offset + 1);
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

	trace("LTIME\t0x%04x\n", msec);
	init->offset += 3;

	if (init_exec(init))
		mdelay(msec);
}

/**
 * INIT_ZM_REG_SEQUENCE - opcode 0x58
 *
 */
static void
init_zm_reg_sequence(struct nvbios_init *init)
{
1280
	struct nvkm_bios *bios = init->subdev->device->bios;
1281 1282
	u32 base = nvbios_rd32(bios, init->offset + 1);
	u8 count = nvbios_rd08(bios, init->offset + 5);
1283 1284 1285 1286 1287

	trace("ZM_REG_SEQUENCE\t0x%02x\n", count);
	init->offset += 6;

	while (count--) {
1288
		u32 data = nvbios_rd32(bios, init->offset);
1289 1290 1291 1292 1293 1294 1295 1296 1297

		trace("\t\tR[0x%06x] = 0x%08x\n", base, data);
		init->offset += 4;

		init_wr32(init, base, data);
		base += 4;
	}
}

1298 1299 1300 1301 1302 1303 1304
/**
 * INIT_PLL_INDIRECT - opcode 0x59
 *
 */
static void
init_pll_indirect(struct nvbios_init *init)
{
1305
	struct nvkm_bios *bios = init->subdev->device->bios;
1306 1307 1308
	u32  reg = nvbios_rd32(bios, init->offset + 1);
	u16 addr = nvbios_rd16(bios, init->offset + 5);
	u32 freq = (u32)nvbios_rd16(bios, addr) * 1000;
1309 1310 1311 1312 1313 1314 1315 1316

	trace("PLL_INDIRECT\tR[0x%06x] =PLL= VBIOS[%04x] = %dkHz\n",
	      reg, addr, freq);
	init->offset += 7;

	init_prog_pll(init, reg, freq);
}

1317 1318 1319 1320 1321 1322 1323
/**
 * INIT_ZM_REG_INDIRECT - opcode 0x5a
 *
 */
static void
init_zm_reg_indirect(struct nvbios_init *init)
{
1324
	struct nvkm_bios *bios = init->subdev->device->bios;
1325 1326 1327
	u32  reg = nvbios_rd32(bios, init->offset + 1);
	u16 addr = nvbios_rd16(bios, init->offset + 5);
	u32 data = nvbios_rd32(bios, addr);
1328 1329 1330 1331 1332 1333 1334 1335

	trace("ZM_REG_INDIRECT\tR[0x%06x] = VBIOS[0x%04x] = 0x%08x\n",
	      reg, addr, data);
	init->offset += 7;

	init_wr32(init, addr, data);
}

1336 1337 1338 1339 1340 1341 1342
/**
 * INIT_SUB_DIRECT - opcode 0x5b
 *
 */
static void
init_sub_direct(struct nvbios_init *init)
{
1343
	struct nvkm_bios *bios = init->subdev->device->bios;
1344
	u16 addr = nvbios_rd16(bios, init->offset + 1);
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	u16 save;

	trace("SUB_DIRECT\t0x%04x\n", addr);

	if (init_exec(init)) {
		save = init->offset;
		init->offset = addr;
		if (nvbios_exec(init)) {
			error("error parsing sub-table\n");
			return;
		}
		init->offset = save;
	}

	init->offset += 3;
}

/**
 * INIT_JUMP - opcode 0x5c
 *
 */
static void
init_jump(struct nvbios_init *init)
{
1369
	struct nvkm_bios *bios = init->subdev->device->bios;
1370
	u16 offset = nvbios_rd16(bios, init->offset + 1);
1371 1372

	trace("JUMP\t0x%04x\n", offset);
1373 1374 1375 1376 1377

	if (init_exec(init))
		init->offset = offset;
	else
		init->offset += 3;
1378 1379 1380 1381 1382 1383 1384 1385 1386
}

/**
 * INIT_I2C_IF - opcode 0x5e
 *
 */
static void
init_i2c_if(struct nvbios_init *init)
{
1387
	struct nvkm_bios *bios = init->subdev->device->bios;
1388 1389 1390 1391 1392
	u8 index = nvbios_rd08(bios, init->offset + 1);
	u8  addr = nvbios_rd08(bios, init->offset + 2);
	u8   reg = nvbios_rd08(bios, init->offset + 3);
	u8  mask = nvbios_rd08(bios, init->offset + 4);
	u8  data = nvbios_rd08(bios, init->offset + 5);
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	u8 value;

	trace("I2C_IF\tI2C[0x%02x][0x%02x][0x%02x] & 0x%02x == 0x%02x\n",
	      index, addr, reg, mask, data);
	init->offset += 6;
	init_exec_force(init, true);

	value = init_rdi2cr(init, index, addr, reg);
	if ((value & mask) != data)
		init_exec_set(init, false);

	init_exec_force(init, false);
}

/**
 * INIT_COPY_NV_REG - opcode 0x5f
 *
 */
static void
init_copy_nv_reg(struct nvbios_init *init)
{
1414
	struct nvkm_bios *bios = init->subdev->device->bios;
1415 1416 1417 1418 1419 1420
	u32  sreg = nvbios_rd32(bios, init->offset + 1);
	u8  shift = nvbios_rd08(bios, init->offset + 5);
	u32 smask = nvbios_rd32(bios, init->offset + 6);
	u32  sxor = nvbios_rd32(bios, init->offset + 10);
	u32  dreg = nvbios_rd32(bios, init->offset + 14);
	u32 dmask = nvbios_rd32(bios, init->offset + 18);
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
	u32 data;

	trace("COPY_NV_REG\tR[0x%06x] &= 0x%08x |= "
	      "((R[0x%06x] %s 0x%02x) & 0x%08x ^ 0x%08x)\n",
	      dreg, dmask, sreg, (shift & 0x80) ? "<<" : ">>",
	      (shift & 0x80) ? (0x100 - shift) : shift, smask, sxor);
	init->offset += 22;

	data = init_shift(init_rd32(init, sreg), shift);
	init_mask(init, dreg, ~dmask, (data & smask) ^ sxor);
}

/**
 * INIT_ZM_INDEX_IO - opcode 0x62
 *
 */
static void
init_zm_index_io(struct nvbios_init *init)
{
1440
	struct nvkm_bios *bios = init->subdev->device->bios;
1441 1442 1443
	u16 port = nvbios_rd16(bios, init->offset + 1);
	u8 index = nvbios_rd08(bios, init->offset + 3);
	u8  data = nvbios_rd08(bios, init->offset + 4);
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457

	trace("ZM_INDEX_IO\tI[0x%04x][0x%02x] = 0x%02x\n", port, index, data);
	init->offset += 5;

	init_wrvgai(init, port, index, data);
}

/**
 * INIT_COMPUTE_MEM - opcode 0x63
 *
 */
static void
init_compute_mem(struct nvbios_init *init)
{
1458
	struct nvkm_devinit *devinit = init->subdev->device->devinit;
1459 1460 1461 1462 1463

	trace("COMPUTE_MEM\n");
	init->offset += 1;

	init_exec_force(init, true);
1464 1465
	if (init_exec(init))
		nvkm_devinit_meminit(devinit);
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
	init_exec_force(init, false);
}

/**
 * INIT_RESET - opcode 0x65
 *
 */
static void
init_reset(struct nvbios_init *init)
{
1476
	struct nvkm_bios *bios = init->subdev->device->bios;
1477 1478 1479
	u32   reg = nvbios_rd32(bios, init->offset + 1);
	u32 data1 = nvbios_rd32(bios, init->offset + 5);
	u32 data2 = nvbios_rd32(bios, init->offset + 9);
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	u32 savepci19;

	trace("RESET\tR[0x%08x] = 0x%08x, 0x%08x", reg, data1, data2);
	init->offset += 13;
	init_exec_force(init, true);

	savepci19 = init_mask(init, 0x00184c, 0x00000f00, 0x00000000);
	init_wr32(init, reg, data1);
	udelay(10);
	init_wr32(init, reg, data2);
	init_wr32(init, 0x00184c, savepci19);
	init_mask(init, 0x001850, 0x00000001, 0x00000000);

	init_exec_force(init, false);
}

/**
 * INIT_CONFIGURE_MEM - opcode 0x66
 *
 */
static u16
init_configure_mem_clk(struct nvbios_init *init)
{
1503
	u16 mdata = bmp_mem_init_table(init->subdev->device->bios);
1504 1505 1506 1507 1508 1509 1510 1511
	if (mdata)
		mdata += (init_rdvgai(init, 0x03d4, 0x3c) >> 4) * 66;
	return mdata;
}

static void
init_configure_mem(struct nvbios_init *init)
{
1512
	struct nvkm_bios *bios = init->subdev->device->bios;
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
	u16 mdata, sdata;
	u32 addr, data;

	trace("CONFIGURE_MEM\n");
	init->offset += 1;

	if (bios->version.major > 2) {
		init_done(init);
		return;
	}
	init_exec_force(init, true);

	mdata = init_configure_mem_clk(init);
	sdata = bmp_sdr_seq_table(bios);
1527
	if (nvbios_rd08(bios, mdata) & 0x01)
1528 1529 1530 1531 1532 1533
		sdata = bmp_ddr_seq_table(bios);
	mdata += 6; /* skip to data */

	data = init_rdvgai(init, 0x03c4, 0x01);
	init_wrvgai(init, 0x03c4, 0x01, data | 0x20);

1534
	for (; (addr = nvbios_rd32(bios, sdata)) != 0xffffffff; sdata += 4) {
1535 1536 1537 1538 1539 1540 1541
		switch (addr) {
		case 0x10021c: /* CKE_NORMAL */
		case 0x1002d0: /* CMD_REFRESH */
		case 0x1002d4: /* CMD_PRECHARGE */
			data = 0x00000001;
			break;
		default:
1542
			data = nvbios_rd32(bios, mdata);
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
			mdata += 4;
			if (data == 0xffffffff)
				continue;
			break;
		}

		init_wr32(init, addr, data);
	}

	init_exec_force(init, false);
}

/**
 * INIT_CONFIGURE_CLK - opcode 0x67
 *
 */
static void
init_configure_clk(struct nvbios_init *init)
{
1562
	struct nvkm_bios *bios = init->subdev->device->bios;
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
	u16 mdata, clock;

	trace("CONFIGURE_CLK\n");
	init->offset += 1;

	if (bios->version.major > 2) {
		init_done(init);
		return;
	}
	init_exec_force(init, true);

	mdata = init_configure_mem_clk(init);

	/* NVPLL */
1577
	clock = nvbios_rd16(bios, mdata + 4) * 10;
1578 1579 1580
	init_prog_pll(init, 0x680500, clock);

	/* MPLL */
1581 1582
	clock = nvbios_rd16(bios, mdata + 2) * 10;
	if (nvbios_rd08(bios, mdata) & 0x01)
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
		clock *= 2;
	init_prog_pll(init, 0x680504, clock);

	init_exec_force(init, false);
}

/**
 * INIT_CONFIGURE_PREINIT - opcode 0x68
 *
 */
static void
init_configure_preinit(struct nvbios_init *init)
{
1596
	struct nvkm_bios *bios = init->subdev->device->bios;
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	u32 strap;

	trace("CONFIGURE_PREINIT\n");
	init->offset += 1;

	if (bios->version.major > 2) {
		init_done(init);
		return;
	}
	init_exec_force(init, true);

	strap = init_rd32(init, 0x101000);
	strap = ((strap << 2) & 0xf0) | ((strap & 0x40) >> 6);
	init_wrvgai(init, 0x03d4, 0x3c, strap);

	init_exec_force(init, false);
}

/**
 * INIT_IO - opcode 0x69
 *
 */
static void
init_io(struct nvbios_init *init)
{
1622
	struct nvkm_bios *bios = init->subdev->device->bios;
1623 1624 1625
	u16 port = nvbios_rd16(bios, init->offset + 1);
	u8  mask = nvbios_rd16(bios, init->offset + 3);
	u8  data = nvbios_rd16(bios, init->offset + 4);
1626 1627 1628 1629 1630 1631 1632 1633 1634
	u8 value;

	trace("IO\t\tI[0x%04x] &= 0x%02x |= 0x%02x\n", port, mask, data);
	init->offset += 5;

	/* ummm.. yes.. should really figure out wtf this is and why it's
	 * needed some day..  it's almost certainly wrong, but, it also
	 * somehow makes things work...
	 */
1635
	if (bios->subdev.device->card_type >= NV_50 &&
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
	    port == 0x03c3 && data == 0x01) {
		init_mask(init, 0x614100, 0xf0800000, 0x00800000);
		init_mask(init, 0x00e18c, 0x00020000, 0x00020000);
		init_mask(init, 0x614900, 0xf0800000, 0x00800000);
		init_mask(init, 0x000200, 0x40000000, 0x00000000);
		mdelay(10);
		init_mask(init, 0x00e18c, 0x00020000, 0x00000000);
		init_mask(init, 0x000200, 0x40000000, 0x40000000);
		init_wr32(init, 0x614100, 0x00800018);
		init_wr32(init, 0x614900, 0x00800018);
		mdelay(10);
		init_wr32(init, 0x614100, 0x10000018);
		init_wr32(init, 0x614900, 0x10000018);
	}

	value = init_rdport(init, port) & mask;
	init_wrport(init, port, data | value);
}

/**
 * INIT_SUB - opcode 0x6b
 *
 */
static void
init_sub(struct nvbios_init *init)
{
1662
	struct nvkm_bios *bios = init->subdev->device->bios;
1663
	u8 index = nvbios_rd08(bios, init->offset + 1);
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
	u16 addr, save;

	trace("SUB\t0x%02x\n", index);

	addr = init_script(bios, index);
	if (addr && init_exec(init)) {
		save = init->offset;
		init->offset = addr;
		if (nvbios_exec(init)) {
			error("error parsing sub-table\n");
			return;
		}
		init->offset = save;
	}

	init->offset += 2;
}

/**
 * INIT_RAM_CONDITION - opcode 0x6d
 *
 */
static void
init_ram_condition(struct nvbios_init *init)
{
1689
	struct nvkm_bios *bios = init->subdev->device->bios;
1690 1691
	u8  mask = nvbios_rd08(bios, init->offset + 1);
	u8 value = nvbios_rd08(bios, init->offset + 2);
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707

	trace("RAM_CONDITION\t"
	      "(R[0x100000] & 0x%02x) == 0x%02x\n", mask, value);
	init->offset += 3;

	if ((init_rd32(init, 0x100000) & mask) != value)
		init_exec_set(init, false);
}

/**
 * INIT_NV_REG - opcode 0x6e
 *
 */
static void
init_nv_reg(struct nvbios_init *init)
{
1708
	struct nvkm_bios *bios = init->subdev->device->bios;
1709 1710 1711
	u32  reg = nvbios_rd32(bios, init->offset + 1);
	u32 mask = nvbios_rd32(bios, init->offset + 5);
	u32 data = nvbios_rd32(bios, init->offset + 9);
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725

	trace("NV_REG\tR[0x%06x] &= 0x%08x |= 0x%08x\n", reg, mask, data);
	init->offset += 13;

	init_mask(init, reg, ~mask, data);
}

/**
 * INIT_MACRO - opcode 0x6f
 *
 */
static void
init_macro(struct nvbios_init *init)
{
1726
	struct nvkm_bios *bios = init->subdev->device->bios;
1727
	u8  macro = nvbios_rd08(bios, init->offset + 1);
1728 1729 1730 1731 1732 1733
	u16 table;

	trace("MACRO\t0x%02x\n", macro);

	table = init_macro_table(init);
	if (table) {
1734 1735
		u32 addr = nvbios_rd32(bios, table + (macro * 8) + 0);
		u32 data = nvbios_rd32(bios, table + (macro * 8) + 4);
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
		trace("\t\tR[0x%06x] = 0x%08x\n", addr, data);
		init_wr32(init, addr, data);
	}

	init->offset += 2;
}

/**
 * INIT_RESUME - opcode 0x72
 *
 */
static void
init_resume(struct nvbios_init *init)
{
	trace("RESUME\n");
	init->offset += 1;
	init_exec_set(init, true);
}

1755 1756 1757 1758 1759 1760 1761
/**
 * INIT_STRAP_CONDITION - opcode 0x73
 *
 */
static void
init_strap_condition(struct nvbios_init *init)
{
1762
	struct nvkm_bios *bios = init->subdev->device->bios;
1763 1764
	u32 mask = nvbios_rd32(bios, init->offset + 1);
	u32 value = nvbios_rd32(bios, init->offset + 5);
1765 1766 1767 1768 1769 1770 1771 1772

	trace("STRAP_CONDITION\t(R[0x101000] & 0x%08x) == 0x%08x\n", mask, value);
	init->offset += 9;

	if ((init_rd32(init, 0x101000) & mask) != value)
		init_exec_set(init, false);
}

1773 1774 1775 1776 1777 1778 1779
/**
 * INIT_TIME - opcode 0x74
 *
 */
static void
init_time(struct nvbios_init *init)
{
1780
	struct nvkm_bios *bios = init->subdev->device->bios;
1781
	u16 usec = nvbios_rd16(bios, init->offset + 1);
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800

	trace("TIME\t0x%04x\n", usec);
	init->offset += 3;

	if (init_exec(init)) {
		if (usec < 1000)
			udelay(usec);
		else
			mdelay((usec + 900) / 1000);
	}
}

/**
 * INIT_CONDITION - opcode 0x75
 *
 */
static void
init_condition(struct nvbios_init *init)
{
1801
	struct nvkm_bios *bios = init->subdev->device->bios;
1802
	u8 cond = nvbios_rd08(bios, init->offset + 1);
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817

	trace("CONDITION\t0x%02x\n", cond);
	init->offset += 2;

	if (!init_condition_met(init, cond))
		init_exec_set(init, false);
}

/**
 * INIT_IO_CONDITION - opcode 0x76
 *
 */
static void
init_io_condition(struct nvbios_init *init)
{
1818
	struct nvkm_bios *bios = init->subdev->device->bios;
1819
	u8 cond = nvbios_rd08(bios, init->offset + 1);
1820 1821 1822 1823 1824 1825 1826 1827

	trace("IO_CONDITION\t0x%02x\n", cond);
	init->offset += 2;

	if (!init_io_condition_met(init, cond))
		init_exec_set(init, false);
}

1828 1829 1830 1831 1832 1833 1834
/**
 * INIT_ZM_REG16 - opcode 0x77
 *
 */
static void
init_zm_reg16(struct nvbios_init *init)
{
1835
	struct nvkm_bios *bios = init->subdev->device->bios;
1836 1837
	u32 addr = nvbios_rd32(bios, init->offset + 1);
	u16 data = nvbios_rd16(bios, init->offset + 5);
1838 1839 1840 1841 1842 1843 1844

	trace("ZM_REG\tR[0x%06x] = 0x%04x\n", addr, data);
	init->offset += 7;

	init_wr32(init, addr, data);
}

1845 1846 1847 1848 1849 1850 1851
/**
 * INIT_INDEX_IO - opcode 0x78
 *
 */
static void
init_index_io(struct nvbios_init *init)
{
1852
	struct nvkm_bios *bios = init->subdev->device->bios;
1853 1854 1855 1856
	u16 port = nvbios_rd16(bios, init->offset + 1);
	u8 index = nvbios_rd16(bios, init->offset + 3);
	u8  mask = nvbios_rd08(bios, init->offset + 4);
	u8  data = nvbios_rd08(bios, init->offset + 5);
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
	u8 value;

	trace("INDEX_IO\tI[0x%04x][0x%02x] &= 0x%02x |= 0x%02x\n",
	      port, index, mask, data);
	init->offset += 6;

	value = init_rdvgai(init, port, index) & mask;
	init_wrvgai(init, port, index, data | value);
}

/**
 * INIT_PLL - opcode 0x79
 *
 */
static void
init_pll(struct nvbios_init *init)
{
1874
	struct nvkm_bios *bios = init->subdev->device->bios;
1875 1876
	u32  reg = nvbios_rd32(bios, init->offset + 1);
	u32 freq = nvbios_rd16(bios, init->offset + 5) * 10;
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890

	trace("PLL\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
	init->offset += 7;

	init_prog_pll(init, reg, freq);
}

/**
 * INIT_ZM_REG - opcode 0x7a
 *
 */
static void
init_zm_reg(struct nvbios_init *init)
{
1891
	struct nvkm_bios *bios = init->subdev->device->bios;
1892 1893
	u32 addr = nvbios_rd32(bios, init->offset + 1);
	u32 data = nvbios_rd32(bios, init->offset + 5);
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910

	trace("ZM_REG\tR[0x%06x] = 0x%08x\n", addr, data);
	init->offset += 9;

	if (addr == 0x000200)
		data |= 0x00000001;

	init_wr32(init, addr, data);
}

/**
 * INIT_RAM_RESTRICT_PLL - opcde 0x87
 *
 */
static void
init_ram_restrict_pll(struct nvbios_init *init)
{
1911
	struct nvkm_bios *bios = init->subdev->device->bios;
1912
	u8  type = nvbios_rd08(bios, init->offset + 1);
1913 1914 1915 1916 1917 1918 1919 1920
	u8 count = init_ram_restrict_group_count(init);
	u8 strap = init_ram_restrict(init);
	u8 cconf;

	trace("RAM_RESTRICT_PLL\t0x%02x\n", type);
	init->offset += 2;

	for (cconf = 0; cconf < count; cconf++) {
1921
		u32 freq = nvbios_rd32(bios, init->offset);
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940

		if (cconf == strap) {
			trace("%dkHz *\n", freq);
			init_prog_pll(init, type, freq);
		} else {
			trace("%dkHz\n", freq);
		}

		init->offset += 4;
	}
}

/**
 * INIT_GPIO - opcode 0x8e
 *
 */
static void
init_gpio(struct nvbios_init *init)
{
1941
	struct nvkm_gpio *gpio = init->subdev->device->gpio;
1942 1943 1944 1945

	trace("GPIO\n");
	init->offset += 1;

1946 1947
	if (init_exec(init))
		nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED);
1948 1949 1950 1951 1952 1953 1954 1955 1956
}

/**
 * INIT_RAM_RESTRICT_ZM_GROUP - opcode 0x8f
 *
 */
static void
init_ram_restrict_zm_reg_group(struct nvbios_init *init)
{
1957
	struct nvkm_bios *bios = init->subdev->device->bios;
1958 1959 1960
	u32 addr = nvbios_rd32(bios, init->offset + 1);
	u8  incr = nvbios_rd08(bios, init->offset + 5);
	u8   num = nvbios_rd08(bios, init->offset + 6);
1961 1962 1963 1964 1965
	u8 count = init_ram_restrict_group_count(init);
	u8 index = init_ram_restrict(init);
	u8 i, j;

	trace("RAM_RESTRICT_ZM_REG_GROUP\t"
1966
	      "R[0x%08x] 0x%02x 0x%02x\n", addr, incr, num);
1967 1968 1969 1970 1971
	init->offset += 7;

	for (i = 0; i < num; i++) {
		trace("\tR[0x%06x] = {\n", addr);
		for (j = 0; j < count; j++) {
1972
			u32 data = nvbios_rd32(bios, init->offset);
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994

			if (j == index) {
				trace("\t\t0x%08x *\n", data);
				init_wr32(init, addr, data);
			} else {
				trace("\t\t0x%08x\n", data);
			}

			init->offset += 4;
		}
		trace("\t}\n");
		addr += incr;
	}
}

/**
 * INIT_COPY_ZM_REG - opcode 0x90
 *
 */
static void
init_copy_zm_reg(struct nvbios_init *init)
{
1995
	struct nvkm_bios *bios = init->subdev->device->bios;
1996 1997
	u32 sreg = nvbios_rd32(bios, init->offset + 1);
	u32 dreg = nvbios_rd32(bios, init->offset + 5);
1998

1999
	trace("COPY_ZM_REG\tR[0x%06x] = R[0x%06x]\n", dreg, sreg);
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
	init->offset += 9;

	init_wr32(init, dreg, init_rd32(init, sreg));
}

/**
 * INIT_ZM_REG_GROUP - opcode 0x91
 *
 */
static void
init_zm_reg_group(struct nvbios_init *init)
{
2012
	struct nvkm_bios *bios = init->subdev->device->bios;
2013 2014
	u32 addr = nvbios_rd32(bios, init->offset + 1);
	u8 count = nvbios_rd08(bios, init->offset + 5);
2015

2016
	trace("ZM_REG_GROUP\tR[0x%06x] =\n", addr);
2017 2018 2019
	init->offset += 6;

	while (count--) {
2020
		u32 data = nvbios_rd32(bios, init->offset);
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
		trace("\t0x%08x\n", data);
		init_wr32(init, addr, data);
		init->offset += 4;
	}
}

/**
 * INIT_XLAT - opcode 0x96
 *
 */
static void
init_xlat(struct nvbios_init *init)
{
2034
	struct nvkm_bios *bios = init->subdev->device->bios;
2035 2036 2037 2038 2039 2040 2041
	u32 saddr = nvbios_rd32(bios, init->offset + 1);
	u8 sshift = nvbios_rd08(bios, init->offset + 5);
	u8  smask = nvbios_rd08(bios, init->offset + 6);
	u8  index = nvbios_rd08(bios, init->offset + 7);
	u32 daddr = nvbios_rd32(bios, init->offset + 8);
	u32 dmask = nvbios_rd32(bios, init->offset + 12);
	u8  shift = nvbios_rd08(bios, init->offset + 16);
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
	u32 data;

	trace("INIT_XLAT\tR[0x%06x] &= 0x%08x |= "
	      "(X%02x((R[0x%06x] %s 0x%02x) & 0x%02x) << 0x%02x)\n",
	      daddr, dmask, index, saddr, (sshift & 0x80) ? "<<" : ">>",
	      (sshift & 0x80) ? (0x100 - sshift) : sshift, smask, shift);
	init->offset += 17;

	data = init_shift(init_rd32(init, saddr), sshift) & smask;
	data = init_xlat_(init, index, data) << shift;
	init_mask(init, daddr, ~dmask, data);
}

/**
 * INIT_ZM_MASK_ADD - opcode 0x97
 *
 */
static void
init_zm_mask_add(struct nvbios_init *init)
{
2062
	struct nvkm_bios *bios = init->subdev->device->bios;
2063 2064 2065
	u32 addr = nvbios_rd32(bios, init->offset + 1);
	u32 mask = nvbios_rd32(bios, init->offset + 5);
	u32  add = nvbios_rd32(bios, init->offset + 9);
2066 2067 2068 2069 2070
	u32 data;

	trace("ZM_MASK_ADD\tR[0x%06x] &= 0x%08x += 0x%08x\n", addr, mask, add);
	init->offset += 13;

2071 2072
	data =  init_rd32(init, addr);
	data = (data & mask) | ((data + add) & ~mask);
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
	init_wr32(init, addr, data);
}

/**
 * INIT_AUXCH - opcode 0x98
 *
 */
static void
init_auxch(struct nvbios_init *init)
{
2083
	struct nvkm_bios *bios = init->subdev->device->bios;
2084 2085
	u32 addr = nvbios_rd32(bios, init->offset + 1);
	u8 count = nvbios_rd08(bios, init->offset + 5);
2086 2087 2088 2089 2090

	trace("AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
	init->offset += 6;

	while (count--) {
2091 2092
		u8 mask = nvbios_rd08(bios, init->offset + 0);
		u8 data = nvbios_rd08(bios, init->offset + 1);
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
		trace("\tAUX[0x%08x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
		mask = init_rdauxr(init, addr) & mask;
		init_wrauxr(init, addr, mask | data);
		init->offset += 2;
	}
}

/**
 * INIT_AUXCH - opcode 0x99
 *
 */
static void
init_zm_auxch(struct nvbios_init *init)
{
2107
	struct nvkm_bios *bios = init->subdev->device->bios;
2108 2109
	u32 addr = nvbios_rd32(bios, init->offset + 1);
	u8 count = nvbios_rd08(bios, init->offset + 5);
2110 2111 2112 2113 2114

	trace("ZM_AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
	init->offset += 6;

	while (count--) {
2115
		u8 data = nvbios_rd08(bios, init->offset + 0);
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
		trace("\tAUX[0x%08x] = 0x%02x\n", addr, data);
		init_wrauxr(init, addr, data);
		init->offset += 1;
	}
}

/**
 * INIT_I2C_LONG_IF - opcode 0x9a
 *
 */
static void
init_i2c_long_if(struct nvbios_init *init)
{
2129
	struct nvkm_bios *bios = init->subdev->device->bios;
2130 2131 2132 2133 2134 2135
	u8 index = nvbios_rd08(bios, init->offset + 1);
	u8  addr = nvbios_rd08(bios, init->offset + 2) >> 1;
	u8 reglo = nvbios_rd08(bios, init->offset + 3);
	u8 reghi = nvbios_rd08(bios, init->offset + 4);
	u8  mask = nvbios_rd08(bios, init->offset + 5);
	u8  data = nvbios_rd08(bios, init->offset + 6);
2136
	struct i2c_adapter *adap;
2137 2138 2139 2140 2141 2142

	trace("I2C_LONG_IF\t"
	      "I2C[0x%02x][0x%02x][0x%02x%02x] & 0x%02x == 0x%02x\n",
	      index, addr, reglo, reghi, mask, data);
	init->offset += 7;

2143 2144
	adap = init_i2c(init, index);
	if (adap) {
2145 2146 2147 2148 2149 2150 2151 2152
		u8 i[2] = { reghi, reglo };
		u8 o[1] = {};
		struct i2c_msg msg[] = {
			{ .addr = addr, .flags = 0, .len = 2, .buf = i },
			{ .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = o }
		};
		int ret;

2153
		ret = i2c_transfer(adap, msg, 2);
2154 2155 2156 2157 2158 2159 2160
		if (ret == 2 && ((o[0] & mask) == data))
			return;
	}

	init_exec_set(init, false);
}

2161 2162 2163 2164 2165 2166 2167
/**
 * INIT_GPIO_NE - opcode 0xa9
 *
 */
static void
init_gpio_ne(struct nvbios_init *init)
{
2168
	struct nvkm_bios *bios = init->subdev->device->bios;
2169
	struct nvkm_gpio *gpio = bios->subdev.device->gpio;
2170
	struct dcb_gpio_func func;
2171
	u8 count = nvbios_rd08(bios, init->offset + 1);
2172 2173 2174 2175 2176 2177 2178
	u8 idx = 0, ver, len;
	u16 data, i;

	trace("GPIO_NE\t");
	init->offset += 2;

	for (i = init->offset; i < init->offset + count; i++)
2179
		cont("0x%02x ", nvbios_rd08(bios, i));
2180 2181 2182 2183 2184
	cont("\n");

	while ((data = dcb_gpio_parse(bios, 0, idx++, &ver, &len, &func))) {
		if (func.func != DCB_GPIO_UNUSED) {
			for (i = init->offset; i < init->offset + count; i++) {
2185
				if (func.func == nvbios_rd08(bios, i))
2186 2187 2188 2189 2190 2191
					break;
			}

			trace("\tFUNC[0x%02x]", func.func);
			if (i == (init->offset + count)) {
				cont(" *");
2192 2193
				if (init_exec(init))
					nvkm_gpio_reset(gpio, func.func);
2194 2195 2196 2197 2198 2199 2200 2201
			}
			cont("\n");
		}
	}

	init->offset += count;
}

2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
static struct nvbios_init_opcode {
	void (*exec)(struct nvbios_init *);
} init_opcode[] = {
	[0x32] = { init_io_restrict_prog },
	[0x33] = { init_repeat },
	[0x34] = { init_io_restrict_pll },
	[0x36] = { init_end_repeat },
	[0x37] = { init_copy },
	[0x38] = { init_not },
	[0x39] = { init_io_flag_condition },
2212
	[0x3a] = { init_generic_condition },
2213 2214
	[0x3b] = { init_io_mask_or },
	[0x3c] = { init_io_or },
2215 2216
	[0x47] = { init_andn_reg },
	[0x48] = { init_or_reg },
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
	[0x49] = { init_idx_addr_latched },
	[0x4a] = { init_io_restrict_pll2 },
	[0x4b] = { init_pll2 },
	[0x4c] = { init_i2c_byte },
	[0x4d] = { init_zm_i2c_byte },
	[0x4e] = { init_zm_i2c },
	[0x4f] = { init_tmds },
	[0x50] = { init_zm_tmds_group },
	[0x51] = { init_cr_idx_adr_latch },
	[0x52] = { init_cr },
	[0x53] = { init_zm_cr },
	[0x54] = { init_zm_cr_group },
	[0x56] = { init_condition_time },
	[0x57] = { init_ltime },
	[0x58] = { init_zm_reg_sequence },
2232
	[0x59] = { init_pll_indirect },
2233
	[0x5a] = { init_zm_reg_indirect },
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
	[0x5b] = { init_sub_direct },
	[0x5c] = { init_jump },
	[0x5e] = { init_i2c_if },
	[0x5f] = { init_copy_nv_reg },
	[0x62] = { init_zm_index_io },
	[0x63] = { init_compute_mem },
	[0x65] = { init_reset },
	[0x66] = { init_configure_mem },
	[0x67] = { init_configure_clk },
	[0x68] = { init_configure_preinit },
	[0x69] = { init_io },
	[0x6b] = { init_sub },
	[0x6d] = { init_ram_condition },
	[0x6e] = { init_nv_reg },
	[0x6f] = { init_macro },
	[0x71] = { init_done },
	[0x72] = { init_resume },
2251
	[0x73] = { init_strap_condition },
2252 2253 2254
	[0x74] = { init_time },
	[0x75] = { init_condition },
	[0x76] = { init_io_condition },
2255
	[0x77] = { init_zm_reg16 },
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
	[0x78] = { init_index_io },
	[0x79] = { init_pll },
	[0x7a] = { init_zm_reg },
	[0x87] = { init_ram_restrict_pll },
	[0x8c] = { init_reserved },
	[0x8d] = { init_reserved },
	[0x8e] = { init_gpio },
	[0x8f] = { init_ram_restrict_zm_reg_group },
	[0x90] = { init_copy_zm_reg },
	[0x91] = { init_zm_reg_group },
	[0x92] = { init_reserved },
	[0x96] = { init_xlat },
	[0x97] = { init_zm_mask_add },
	[0x98] = { init_auxch },
	[0x99] = { init_zm_auxch },
	[0x9a] = { init_i2c_long_if },
2272
	[0xa9] = { init_gpio_ne },
2273
	[0xaa] = { init_reserved },
2274 2275 2276 2277 2278
};

int
nvbios_exec(struct nvbios_init *init)
{
2279
	struct nvkm_bios *bios = init->subdev->device->bios;
2280

2281 2282
	init->nested++;
	while (init->offset) {
2283
		u8 opcode = nvbios_rd08(bios, init->offset);
2284 2285
		if (opcode >= ARRAY_SIZE(init_opcode) ||
		    !init_opcode[opcode].exec) {
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
			error("unknown opcode 0x%02x\n", opcode);
			return -EINVAL;
		}

		init_opcode[opcode].exec(init);
	}
	init->nested--;
	return 0;
}

int
2297
nvbios_post(struct nvkm_subdev *subdev, bool execute)
2298
{
2299
	struct nvkm_bios *bios = subdev->device->bios;
2300 2301 2302 2303 2304
	int ret = 0;
	int i = -1;
	u16 data;

	if (execute)
2305
		nvkm_debug(subdev, "running init tables\n");
2306
	while (!ret && (data = (init_script(bios, ++i)))) {
2307 2308 2309
		ret = nvbios_init(subdev, data,
			init.execute = execute ? 1 : 0;
		      );
2310 2311 2312 2313 2314 2315
	}

	/* the vbios parser will run this right after the normal init
	 * tables, whereas the binary driver appears to run it later.
	 */
	if (!ret && (data = init_unknown_script(bios))) {
2316 2317 2318
		ret = nvbios_init(subdev, data,
			init.execute = execute ? 1 : 0;
		      );
2319 2320
	}

2321
	return ret;
2322
}