2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <subdev/bios.h>
25 #include <subdev/bios/bit.h>
26 #include <subdev/bios/bmp.h>
27 #include <subdev/bios/conn.h>
28 #include <subdev/bios/dcb.h>
29 #include <subdev/bios/dp.h>
30 #include <subdev/bios/gpio.h>
31 #include <subdev/bios/init.h>
32 #include <subdev/bios/ramcfg.h>
34 #include <subdev/devinit.h>
35 #include <subdev/gpio.h>
36 #include <subdev/i2c.h>
37 #include <subdev/vga.h>
39 #define bioslog(lvl, fmt, args...) do { \
40 nvkm_printk(init->subdev, lvl, info, "0x%08x[%c]: "fmt, \
41 init->offset, init_exec(init) ? \
42 '0' + (init->nested - 1) : ' ', ##args); \
44 #define cont(fmt, args...) do { \
45 if (init->subdev->debug >= NV_DBG_TRACE) \
46 printk(fmt, ##args); \
48 #define trace(fmt, args...) bioslog(TRACE, fmt, ##args)
49 #define warn(fmt, args...) bioslog(WARN, fmt, ##args)
50 #define error(fmt, args...) bioslog(ERROR, fmt, ##args)
52 /******************************************************************************
53 * init parser control flow helpers
54 *****************************************************************************/
57 init_exec(struct nvbios_init *init)
59 return (init->execute == 1) || ((init->execute & 5) == 5);
63 init_exec_set(struct nvbios_init *init, bool exec)
65 if (exec) init->execute &= 0xfd;
66 else init->execute |= 0x02;
70 init_exec_inv(struct nvbios_init *init)
72 init->execute ^= 0x02;
76 init_exec_force(struct nvbios_init *init, bool exec)
78 if (exec) init->execute |= 0x04;
79 else init->execute &= 0xfb;
82 /******************************************************************************
83 * init parser wrappers for normal register/i2c/whatever accessors
84 *****************************************************************************/
87 init_or(struct nvbios_init *init)
89 if (init_exec(init)) {
92 error("script needs OR!!\n");
98 init_link(struct nvbios_init *init)
100 if (init_exec(init)) {
102 return init->link == 2;
103 error("script needs OR link\n");
109 init_head(struct nvbios_init *init)
111 if (init_exec(init)) {
114 error("script needs head\n");
120 init_conn(struct nvbios_init *init)
122 struct nvkm_bios *bios = init->subdev->device->bios;
123 struct nvbios_connE connE;
127 if (init_exec(init)) {
129 conn = init->outp->connector;
130 conn = nvbios_connEp(bios, conn, &ver, &hdr, &connE);
135 error("script needs connector type\n");
142 init_nvreg(struct nvbios_init *init, u32 reg)
144 struct nvkm_devinit *devinit = init->subdev->device->devinit;
146 /* C51 (at least) sometimes has the lower bits set which the VBIOS
147 * interprets to mean that access needs to go through certain IO
148 * ports instead. The NVIDIA binary driver has been seen to access
149 * these through the NV register address, so lets assume we can
154 /* GF8+ display scripts need register addresses mangled a bit to
155 * select a specific CRTC/OR
157 if (init->subdev->device->card_type >= NV_50) {
158 if (reg & 0x80000000) {
159 reg += init_head(init) * 0x800;
163 if (reg & 0x40000000) {
164 reg += init_or(init) * 0x800;
166 if (reg & 0x20000000) {
167 reg += init_link(init) * 0x80;
173 if (reg & ~0x00fffffc)
174 warn("unknown bits in register 0x%08x\n", reg);
176 return nvkm_devinit_mmio(devinit, reg);
180 init_rd32(struct nvbios_init *init, u32 reg)
182 struct nvkm_device *device = init->subdev->device;
183 reg = init_nvreg(init, reg);
184 if (reg != ~0 && init_exec(init))
185 return nvkm_rd32(device, reg);
190 init_wr32(struct nvbios_init *init, u32 reg, u32 val)
192 struct nvkm_device *device = init->subdev->device;
193 reg = init_nvreg(init, reg);
194 if (reg != ~0 && init_exec(init))
195 nvkm_wr32(device, reg, val);
199 init_mask(struct nvbios_init *init, u32 reg, u32 mask, u32 val)
201 struct nvkm_device *device = init->subdev->device;
202 reg = init_nvreg(init, reg);
203 if (reg != ~0 && init_exec(init)) {
204 u32 tmp = nvkm_rd32(device, reg);
205 nvkm_wr32(device, reg, (tmp & ~mask) | val);
212 init_rdport(struct nvbios_init *init, u16 port)
215 return nvkm_rdport(init->subdev->device, init->head, port);
220 init_wrport(struct nvbios_init *init, u16 port, u8 value)
223 nvkm_wrport(init->subdev->device, init->head, port, value);
227 init_rdvgai(struct nvbios_init *init, u16 port, u8 index)
229 struct nvkm_subdev *subdev = init->subdev;
230 if (init_exec(init)) {
231 int head = init->head < 0 ? 0 : init->head;
232 return nvkm_rdvgai(subdev->device, head, port, index);
238 init_wrvgai(struct nvbios_init *init, u16 port, u8 index, u8 value)
240 struct nvkm_device *device = init->subdev->device;
242 /* force head 0 for updates to cr44, it only exists on first head */
243 if (device->card_type < NV_50) {
244 if (port == 0x03d4 && index == 0x44)
248 if (init_exec(init)) {
249 int head = init->head < 0 ? 0 : init->head;
250 nvkm_wrvgai(device, head, port, index, value);
253 /* select head 1 if cr44 write selected it */
254 if (device->card_type < NV_50) {
255 if (port == 0x03d4 && index == 0x44 && value == 3)
260 static struct i2c_adapter *
261 init_i2c(struct nvbios_init *init, int index)
263 struct nvkm_i2c *i2c = init->subdev->device->i2c;
264 struct nvkm_i2c_bus *bus;
267 index = NVKM_I2C_BUS_PRI;
268 if (init->outp && init->outp->i2c_upper_default)
269 index = NVKM_I2C_BUS_SEC;
272 index = NVKM_I2C_BUS_PRI;
275 index = NVKM_I2C_BUS_SEC;
278 bus = nvkm_i2c_bus_find(i2c, index);
279 return bus ? &bus->i2c : NULL;
283 init_rdi2cr(struct nvbios_init *init, u8 index, u8 addr, u8 reg)
285 struct i2c_adapter *adap = init_i2c(init, index);
286 if (adap && init_exec(init))
287 return nvkm_rdi2cr(adap, addr, reg);
292 init_wri2cr(struct nvbios_init *init, u8 index, u8 addr, u8 reg, u8 val)
294 struct i2c_adapter *adap = init_i2c(init, index);
295 if (adap && init_exec(init))
296 return nvkm_wri2cr(adap, addr, reg, val);
300 static struct nvkm_i2c_aux *
301 init_aux(struct nvbios_init *init)
303 struct nvkm_i2c *i2c = init->subdev->device->i2c;
306 error("script needs output for aux\n");
309 return nvkm_i2c_aux_find(i2c, init->outp->i2c_index);
313 init_rdauxr(struct nvbios_init *init, u32 addr)
315 struct nvkm_i2c_aux *aux = init_aux(init);
318 if (aux && init_exec(init)) {
319 int ret = nvkm_rdaux(aux, addr, &data, 1);
322 trace("auxch read failed with %d\n", ret);
329 init_wrauxr(struct nvbios_init *init, u32 addr, u8 data)
331 struct nvkm_i2c_aux *aux = init_aux(init);
332 if (aux && init_exec(init)) {
333 int ret = nvkm_wraux(aux, addr, &data, 1);
335 trace("auxch write failed with %d\n", ret);
342 init_prog_pll(struct nvbios_init *init, u32 id, u32 freq)
344 struct nvkm_devinit *devinit = init->subdev->device->devinit;
345 if (init_exec(init)) {
346 int ret = nvkm_devinit_pll_set(devinit, id, freq);
348 warn("failed to prog pll 0x%08x to %dkHz\n", id, freq);
352 /******************************************************************************
353 * parsing of bios structures that are required to execute init tables
354 *****************************************************************************/
357 init_table(struct nvkm_bios *bios, u16 *len)
359 struct bit_entry bit_I;
361 if (!bit_entry(bios, 'I', &bit_I)) {
366 if (bmp_version(bios) >= 0x0510) {
368 return bios->bmp_offset + 75;
375 init_table_(struct nvbios_init *init, u16 offset, const char *name)
377 struct nvkm_bios *bios = init->subdev->device->bios;
378 u16 len, data = init_table(bios, &len);
380 if (len >= offset + 2) {
381 data = nvbios_rd16(bios, data + offset);
385 warn("%s pointer invalid\n", name);
389 warn("init data too short for %s pointer", name);
393 warn("init data not found\n");
397 #define init_script_table(b) init_table_((b), 0x00, "script table")
398 #define init_macro_index_table(b) init_table_((b), 0x02, "macro index table")
399 #define init_macro_table(b) init_table_((b), 0x04, "macro table")
400 #define init_condition_table(b) init_table_((b), 0x06, "condition table")
401 #define init_io_condition_table(b) init_table_((b), 0x08, "io condition table")
402 #define init_io_flag_condition_table(b) init_table_((b), 0x0a, "io flag conditon table")
403 #define init_function_table(b) init_table_((b), 0x0c, "function table")
404 #define init_xlat_table(b) init_table_((b), 0x10, "xlat table");
407 init_script(struct nvkm_bios *bios, int index)
409 struct nvbios_init init = { .subdev = &bios->subdev };
410 u16 bmp_ver = bmp_version(bios), data;
412 if (bmp_ver && bmp_ver < 0x0510) {
413 if (index > 1 || bmp_ver < 0x0100)
416 data = bios->bmp_offset + (bmp_ver < 0x0200 ? 14 : 18);
417 return nvbios_rd16(bios, data + (index * 2));
420 data = init_script_table(&init);
422 return nvbios_rd16(bios, data + (index * 2));
428 init_unknown_script(struct nvkm_bios *bios)
430 u16 len, data = init_table(bios, &len);
431 if (data && len >= 16)
432 return nvbios_rd16(bios, data + 14);
437 init_ram_restrict_group_count(struct nvbios_init *init)
439 return nvbios_ramcfg_count(init->subdev->device->bios);
443 init_ram_restrict(struct nvbios_init *init)
445 /* This appears to be the behaviour of the VBIOS parser, and *is*
446 * important to cache the NV_PEXTDEV_BOOT0 on later chipsets to
447 * avoid fucking up the memory controller (somehow) by reading it
448 * on every INIT_RAM_RESTRICT_ZM_GROUP opcode.
450 * Preserving the non-caching behaviour on earlier chipsets just
451 * in case *not* re-reading the strap causes similar breakage.
453 if (!init->ramcfg || init->subdev->device->bios->version.major < 0x70)
454 init->ramcfg = 0x80000000 | nvbios_ramcfg_index(init->subdev);
455 return (init->ramcfg & 0x7fffffff);
459 init_xlat_(struct nvbios_init *init, u8 index, u8 offset)
461 struct nvkm_bios *bios = init->subdev->device->bios;
462 u16 table = init_xlat_table(init);
464 u16 data = nvbios_rd16(bios, table + (index * 2));
466 return nvbios_rd08(bios, data + offset);
467 warn("xlat table pointer %d invalid\n", index);
472 /******************************************************************************
473 * utility functions used by various init opcode handlers
474 *****************************************************************************/
477 init_condition_met(struct nvbios_init *init, u8 cond)
479 struct nvkm_bios *bios = init->subdev->device->bios;
480 u16 table = init_condition_table(init);
482 u32 reg = nvbios_rd32(bios, table + (cond * 12) + 0);
483 u32 msk = nvbios_rd32(bios, table + (cond * 12) + 4);
484 u32 val = nvbios_rd32(bios, table + (cond * 12) + 8);
485 trace("\t[0x%02x] (R[0x%06x] & 0x%08x) == 0x%08x\n",
486 cond, reg, msk, val);
487 return (init_rd32(init, reg) & msk) == val;
493 init_io_condition_met(struct nvbios_init *init, u8 cond)
495 struct nvkm_bios *bios = init->subdev->device->bios;
496 u16 table = init_io_condition_table(init);
498 u16 port = nvbios_rd16(bios, table + (cond * 5) + 0);
499 u8 index = nvbios_rd08(bios, table + (cond * 5) + 2);
500 u8 mask = nvbios_rd08(bios, table + (cond * 5) + 3);
501 u8 value = nvbios_rd08(bios, table + (cond * 5) + 4);
502 trace("\t[0x%02x] (0x%04x[0x%02x] & 0x%02x) == 0x%02x\n",
503 cond, port, index, mask, value);
504 return (init_rdvgai(init, port, index) & mask) == value;
510 init_io_flag_condition_met(struct nvbios_init *init, u8 cond)
512 struct nvkm_bios *bios = init->subdev->device->bios;
513 u16 table = init_io_flag_condition_table(init);
515 u16 port = nvbios_rd16(bios, table + (cond * 9) + 0);
516 u8 index = nvbios_rd08(bios, table + (cond * 9) + 2);
517 u8 mask = nvbios_rd08(bios, table + (cond * 9) + 3);
518 u8 shift = nvbios_rd08(bios, table + (cond * 9) + 4);
519 u16 data = nvbios_rd16(bios, table + (cond * 9) + 5);
520 u8 dmask = nvbios_rd08(bios, table + (cond * 9) + 7);
521 u8 value = nvbios_rd08(bios, table + (cond * 9) + 8);
522 u8 ioval = (init_rdvgai(init, port, index) & mask) >> shift;
523 return (nvbios_rd08(bios, data + ioval) & dmask) == value;
529 init_shift(u32 data, u8 shift)
532 return data >> shift;
533 return data << (0x100 - shift);
537 init_tmds_reg(struct nvbios_init *init, u8 tmds)
539 /* For mlv < 0x80, it is an index into a table of TMDS base addresses.
540 * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
541 * CR58 for CR57 = 0 to index a table of offsets to the basic
543 * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
544 * CR58 for CR57 = 0 to index a table of offsets to the basic
545 * 0x6808b0 address, and then flip the offset by 8.
547 const int pramdac_offset[13] = {
548 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
549 const u32 pramdac_table[4] = {
550 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
554 u32 dacoffset = pramdac_offset[init->outp->or];
557 return 0x6808b0 + dacoffset;
561 error("tmds opcodes need dcb\n");
563 if (tmds < ARRAY_SIZE(pramdac_table))
564 return pramdac_table[tmds];
566 error("tmds selector 0x%02x unknown\n", tmds);
572 /******************************************************************************
573 * init opcode handlers
574 *****************************************************************************/
577 * init_reserved - stub for various unknown/unused single-byte opcodes
581 init_reserved(struct nvbios_init *init)
583 struct nvkm_bios *bios = init->subdev->device->bios;
584 u8 opcode = nvbios_rd08(bios, init->offset);
596 trace("RESERVED 0x%02x\t", opcode);
597 for (i = 1; i < length; i++)
598 cont(" 0x%02x", nvbios_rd08(bios, init->offset + i));
600 init->offset += length;
604 * INIT_DONE - opcode 0x71
608 init_done(struct nvbios_init *init)
611 init->offset = 0x0000;
615 * INIT_IO_RESTRICT_PROG - opcode 0x32
619 init_io_restrict_prog(struct nvbios_init *init)
621 struct nvkm_bios *bios = init->subdev->device->bios;
622 u16 port = nvbios_rd16(bios, init->offset + 1);
623 u8 index = nvbios_rd08(bios, init->offset + 3);
624 u8 mask = nvbios_rd08(bios, init->offset + 4);
625 u8 shift = nvbios_rd08(bios, init->offset + 5);
626 u8 count = nvbios_rd08(bios, init->offset + 6);
627 u32 reg = nvbios_rd32(bios, init->offset + 7);
630 trace("IO_RESTRICT_PROG\tR[0x%06x] = "
631 "((0x%04x[0x%02x] & 0x%02x) >> %d) [{\n",
632 reg, port, index, mask, shift);
635 conf = (init_rdvgai(init, port, index) & mask) >> shift;
636 for (i = 0; i < count; i++) {
637 u32 data = nvbios_rd32(bios, init->offset);
640 trace("\t0x%08x *\n", data);
641 init_wr32(init, reg, data);
643 trace("\t0x%08x\n", data);
652 * INIT_REPEAT - opcode 0x33
656 init_repeat(struct nvbios_init *init)
658 struct nvkm_bios *bios = init->subdev->device->bios;
659 u8 count = nvbios_rd08(bios, init->offset + 1);
660 u16 repeat = init->repeat;
662 trace("REPEAT\t0x%02x\n", count);
665 init->repeat = init->offset;
666 init->repend = init->offset;
668 init->offset = init->repeat;
671 trace("REPEAT\t0x%02x\n", count);
673 init->offset = init->repend;
674 init->repeat = repeat;
678 * INIT_IO_RESTRICT_PLL - opcode 0x34
682 init_io_restrict_pll(struct nvbios_init *init)
684 struct nvkm_bios *bios = init->subdev->device->bios;
685 u16 port = nvbios_rd16(bios, init->offset + 1);
686 u8 index = nvbios_rd08(bios, init->offset + 3);
687 u8 mask = nvbios_rd08(bios, init->offset + 4);
688 u8 shift = nvbios_rd08(bios, init->offset + 5);
689 s8 iofc = nvbios_rd08(bios, init->offset + 6);
690 u8 count = nvbios_rd08(bios, init->offset + 7);
691 u32 reg = nvbios_rd32(bios, init->offset + 8);
694 trace("IO_RESTRICT_PLL\tR[0x%06x] =PLL= "
695 "((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) IOFCOND 0x%02x [{\n",
696 reg, port, index, mask, shift, iofc);
699 conf = (init_rdvgai(init, port, index) & mask) >> shift;
700 for (i = 0; i < count; i++) {
701 u32 freq = nvbios_rd16(bios, init->offset) * 10;
704 trace("\t%dkHz *\n", freq);
705 if (iofc > 0 && init_io_flag_condition_met(init, iofc))
707 init_prog_pll(init, reg, freq);
709 trace("\t%dkHz\n", freq);
718 * INIT_END_REPEAT - opcode 0x36
722 init_end_repeat(struct nvbios_init *init)
724 trace("END_REPEAT\n");
728 init->repend = init->offset;
734 * INIT_COPY - opcode 0x37
738 init_copy(struct nvbios_init *init)
740 struct nvkm_bios *bios = init->subdev->device->bios;
741 u32 reg = nvbios_rd32(bios, init->offset + 1);
742 u8 shift = nvbios_rd08(bios, init->offset + 5);
743 u8 smask = nvbios_rd08(bios, init->offset + 6);
744 u16 port = nvbios_rd16(bios, init->offset + 7);
745 u8 index = nvbios_rd08(bios, init->offset + 9);
746 u8 mask = nvbios_rd08(bios, init->offset + 10);
749 trace("COPY\t0x%04x[0x%02x] &= 0x%02x |= "
750 "((R[0x%06x] %s 0x%02x) & 0x%02x)\n",
751 port, index, mask, reg, (shift & 0x80) ? "<<" : ">>",
752 (shift & 0x80) ? (0x100 - shift) : shift, smask);
755 data = init_rdvgai(init, port, index) & mask;
756 data |= init_shift(init_rd32(init, reg), shift) & smask;
757 init_wrvgai(init, port, index, data);
761 * INIT_NOT - opcode 0x38
765 init_not(struct nvbios_init *init)
773 * INIT_IO_FLAG_CONDITION - opcode 0x39
777 init_io_flag_condition(struct nvbios_init *init)
779 struct nvkm_bios *bios = init->subdev->device->bios;
780 u8 cond = nvbios_rd08(bios, init->offset + 1);
782 trace("IO_FLAG_CONDITION\t0x%02x\n", cond);
785 if (!init_io_flag_condition_met(init, cond))
786 init_exec_set(init, false);
790 * INIT_GENERIC_CONDITION - opcode 0x3a
794 init_generic_condition(struct nvbios_init *init)
796 struct nvkm_bios *bios = init->subdev->device->bios;
797 struct nvbios_dpout info;
798 u8 cond = nvbios_rd08(bios, init->offset + 1);
799 u8 size = nvbios_rd08(bios, init->offset + 2);
800 u8 ver, hdr, cnt, len;
803 trace("GENERIC_CONDITION\t0x%02x 0x%02x\n", cond, size);
808 if (init_conn(init) != DCB_CONNECTOR_eDP)
809 init_exec_set(init, false);
814 (data = nvbios_dpout_match(bios, DCB_OUTPUT_DP,
815 (init->outp->or << 0) |
816 (init->outp->sorconf.link << 6),
817 &ver, &hdr, &cnt, &len, &info)))
819 if (!(info.flags & cond))
820 init_exec_set(init, false);
825 warn("script needs dp output table data\n");
828 if (!(init_rdauxr(init, 0x0d) & 1))
829 init_exec_set(init, false);
832 warn("INIT_GENERIC_CONDITON: unknown 0x%02x\n", cond);
833 init->offset += size;
839 * INIT_IO_MASK_OR - opcode 0x3b
843 init_io_mask_or(struct nvbios_init *init)
845 struct nvkm_bios *bios = init->subdev->device->bios;
846 u8 index = nvbios_rd08(bios, init->offset + 1);
847 u8 or = init_or(init);
850 trace("IO_MASK_OR\t0x03d4[0x%02x] &= ~(1 << 0x%02x)\n", index, or);
853 data = init_rdvgai(init, 0x03d4, index);
854 init_wrvgai(init, 0x03d4, index, data &= ~(1 << or));
858 * INIT_IO_OR - opcode 0x3c
862 init_io_or(struct nvbios_init *init)
864 struct nvkm_bios *bios = init->subdev->device->bios;
865 u8 index = nvbios_rd08(bios, init->offset + 1);
866 u8 or = init_or(init);
869 trace("IO_OR\t0x03d4[0x%02x] |= (1 << 0x%02x)\n", index, or);
872 data = init_rdvgai(init, 0x03d4, index);
873 init_wrvgai(init, 0x03d4, index, data | (1 << or));
877 * INIT_ANDN_REG - opcode 0x47
881 init_andn_reg(struct nvbios_init *init)
883 struct nvkm_bios *bios = init->subdev->device->bios;
884 u32 reg = nvbios_rd32(bios, init->offset + 1);
885 u32 mask = nvbios_rd32(bios, init->offset + 5);
887 trace("ANDN_REG\tR[0x%06x] &= ~0x%08x\n", reg, mask);
890 init_mask(init, reg, mask, 0);
894 * INIT_OR_REG - opcode 0x48
898 init_or_reg(struct nvbios_init *init)
900 struct nvkm_bios *bios = init->subdev->device->bios;
901 u32 reg = nvbios_rd32(bios, init->offset + 1);
902 u32 mask = nvbios_rd32(bios, init->offset + 5);
904 trace("OR_REG\tR[0x%06x] |= 0x%08x\n", reg, mask);
907 init_mask(init, reg, 0, mask);
911 * INIT_INDEX_ADDRESS_LATCHED - opcode 0x49
915 init_idx_addr_latched(struct nvbios_init *init)
917 struct nvkm_bios *bios = init->subdev->device->bios;
918 u32 creg = nvbios_rd32(bios, init->offset + 1);
919 u32 dreg = nvbios_rd32(bios, init->offset + 5);
920 u32 mask = nvbios_rd32(bios, init->offset + 9);
921 u32 data = nvbios_rd32(bios, init->offset + 13);
922 u8 count = nvbios_rd08(bios, init->offset + 17);
924 trace("INDEX_ADDRESS_LATCHED\tR[0x%06x] : R[0x%06x]\n", creg, dreg);
925 trace("\tCTRL &= 0x%08x |= 0x%08x\n", mask, data);
929 u8 iaddr = nvbios_rd08(bios, init->offset + 0);
930 u8 idata = nvbios_rd08(bios, init->offset + 1);
932 trace("\t[0x%02x] = 0x%02x\n", iaddr, idata);
935 init_wr32(init, dreg, idata);
936 init_mask(init, creg, ~mask, data | iaddr);
941 * INIT_IO_RESTRICT_PLL2 - opcode 0x4a
945 init_io_restrict_pll2(struct nvbios_init *init)
947 struct nvkm_bios *bios = init->subdev->device->bios;
948 u16 port = nvbios_rd16(bios, init->offset + 1);
949 u8 index = nvbios_rd08(bios, init->offset + 3);
950 u8 mask = nvbios_rd08(bios, init->offset + 4);
951 u8 shift = nvbios_rd08(bios, init->offset + 5);
952 u8 count = nvbios_rd08(bios, init->offset + 6);
953 u32 reg = nvbios_rd32(bios, init->offset + 7);
956 trace("IO_RESTRICT_PLL2\t"
957 "R[0x%06x] =PLL= ((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) [{\n",
958 reg, port, index, mask, shift);
961 conf = (init_rdvgai(init, port, index) & mask) >> shift;
962 for (i = 0; i < count; i++) {
963 u32 freq = nvbios_rd32(bios, init->offset);
965 trace("\t%dkHz *\n", freq);
966 init_prog_pll(init, reg, freq);
968 trace("\t%dkHz\n", freq);
976 * INIT_PLL2 - opcode 0x4b
980 init_pll2(struct nvbios_init *init)
982 struct nvkm_bios *bios = init->subdev->device->bios;
983 u32 reg = nvbios_rd32(bios, init->offset + 1);
984 u32 freq = nvbios_rd32(bios, init->offset + 5);
986 trace("PLL2\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
989 init_prog_pll(init, reg, freq);
993 * INIT_I2C_BYTE - opcode 0x4c
997 init_i2c_byte(struct nvbios_init *init)
999 struct nvkm_bios *bios = init->subdev->device->bios;
1000 u8 index = nvbios_rd08(bios, init->offset + 1);
1001 u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
1002 u8 count = nvbios_rd08(bios, init->offset + 3);
1004 trace("I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
1008 u8 reg = nvbios_rd08(bios, init->offset + 0);
1009 u8 mask = nvbios_rd08(bios, init->offset + 1);
1010 u8 data = nvbios_rd08(bios, init->offset + 2);
1013 trace("\t[0x%02x] &= 0x%02x |= 0x%02x\n", reg, mask, data);
1016 val = init_rdi2cr(init, index, addr, reg);
1019 init_wri2cr(init, index, addr, reg, (val & mask) | data);
1024 * INIT_ZM_I2C_BYTE - opcode 0x4d
1028 init_zm_i2c_byte(struct nvbios_init *init)
1030 struct nvkm_bios *bios = init->subdev->device->bios;
1031 u8 index = nvbios_rd08(bios, init->offset + 1);
1032 u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
1033 u8 count = nvbios_rd08(bios, init->offset + 3);
1035 trace("ZM_I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
1039 u8 reg = nvbios_rd08(bios, init->offset + 0);
1040 u8 data = nvbios_rd08(bios, init->offset + 1);
1042 trace("\t[0x%02x] = 0x%02x\n", reg, data);
1045 init_wri2cr(init, index, addr, reg, data);
1050 * INIT_ZM_I2C - opcode 0x4e
1054 init_zm_i2c(struct nvbios_init *init)
1056 struct nvkm_bios *bios = init->subdev->device->bios;
1057 u8 index = nvbios_rd08(bios, init->offset + 1);
1058 u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
1059 u8 count = nvbios_rd08(bios, init->offset + 3);
1062 trace("ZM_I2C\tI2C[0x%02x][0x%02x]\n", index, addr);
1065 for (i = 0; i < count; i++) {
1066 data[i] = nvbios_rd08(bios, init->offset);
1067 trace("\t0x%02x\n", data[i]);
1071 if (init_exec(init)) {
1072 struct i2c_adapter *adap = init_i2c(init, index);
1073 struct i2c_msg msg = {
1074 .addr = addr, .flags = 0, .len = count, .buf = data,
1078 if (adap && (ret = i2c_transfer(adap, &msg, 1)) != 1)
1079 warn("i2c wr failed, %d\n", ret);
1084 * INIT_TMDS - opcode 0x4f
1088 init_tmds(struct nvbios_init *init)
1090 struct nvkm_bios *bios = init->subdev->device->bios;
1091 u8 tmds = nvbios_rd08(bios, init->offset + 1);
1092 u8 addr = nvbios_rd08(bios, init->offset + 2);
1093 u8 mask = nvbios_rd08(bios, init->offset + 3);
1094 u8 data = nvbios_rd08(bios, init->offset + 4);
1095 u32 reg = init_tmds_reg(init, tmds);
1097 trace("TMDS\tT[0x%02x][0x%02x] &= 0x%02x |= 0x%02x\n",
1098 tmds, addr, mask, data);
1104 init_wr32(init, reg + 0, addr | 0x00010000);
1105 init_wr32(init, reg + 4, data | (init_rd32(init, reg + 4) & mask));
1106 init_wr32(init, reg + 0, addr);
1110 * INIT_ZM_TMDS_GROUP - opcode 0x50
1114 init_zm_tmds_group(struct nvbios_init *init)
1116 struct nvkm_bios *bios = init->subdev->device->bios;
1117 u8 tmds = nvbios_rd08(bios, init->offset + 1);
1118 u8 count = nvbios_rd08(bios, init->offset + 2);
1119 u32 reg = init_tmds_reg(init, tmds);
1121 trace("TMDS_ZM_GROUP\tT[0x%02x]\n", tmds);
1125 u8 addr = nvbios_rd08(bios, init->offset + 0);
1126 u8 data = nvbios_rd08(bios, init->offset + 1);
1128 trace("\t[0x%02x] = 0x%02x\n", addr, data);
1131 init_wr32(init, reg + 4, data);
1132 init_wr32(init, reg + 0, addr);
1137 * INIT_CR_INDEX_ADDRESS_LATCHED - opcode 0x51
1141 init_cr_idx_adr_latch(struct nvbios_init *init)
1143 struct nvkm_bios *bios = init->subdev->device->bios;
1144 u8 addr0 = nvbios_rd08(bios, init->offset + 1);
1145 u8 addr1 = nvbios_rd08(bios, init->offset + 2);
1146 u8 base = nvbios_rd08(bios, init->offset + 3);
1147 u8 count = nvbios_rd08(bios, init->offset + 4);
1150 trace("CR_INDEX_ADDR C[%02x] C[%02x]\n", addr0, addr1);
1153 save0 = init_rdvgai(init, 0x03d4, addr0);
1155 u8 data = nvbios_rd08(bios, init->offset);
1157 trace("\t\t[0x%02x] = 0x%02x\n", base, data);
1160 init_wrvgai(init, 0x03d4, addr0, base++);
1161 init_wrvgai(init, 0x03d4, addr1, data);
1163 init_wrvgai(init, 0x03d4, addr0, save0);
1167 * INIT_CR - opcode 0x52
1171 init_cr(struct nvbios_init *init)
1173 struct nvkm_bios *bios = init->subdev->device->bios;
1174 u8 addr = nvbios_rd08(bios, init->offset + 1);
1175 u8 mask = nvbios_rd08(bios, init->offset + 2);
1176 u8 data = nvbios_rd08(bios, init->offset + 3);
1179 trace("CR\t\tC[0x%02x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
1182 val = init_rdvgai(init, 0x03d4, addr) & mask;
1183 init_wrvgai(init, 0x03d4, addr, val | data);
1187 * INIT_ZM_CR - opcode 0x53
1191 init_zm_cr(struct nvbios_init *init)
1193 struct nvkm_bios *bios = init->subdev->device->bios;
1194 u8 addr = nvbios_rd08(bios, init->offset + 1);
1195 u8 data = nvbios_rd08(bios, init->offset + 2);
1197 trace("ZM_CR\tC[0x%02x] = 0x%02x\n", addr, data);
1200 init_wrvgai(init, 0x03d4, addr, data);
1204 * INIT_ZM_CR_GROUP - opcode 0x54
1208 init_zm_cr_group(struct nvbios_init *init)
1210 struct nvkm_bios *bios = init->subdev->device->bios;
1211 u8 count = nvbios_rd08(bios, init->offset + 1);
1213 trace("ZM_CR_GROUP\n");
1217 u8 addr = nvbios_rd08(bios, init->offset + 0);
1218 u8 data = nvbios_rd08(bios, init->offset + 1);
1220 trace("\t\tC[0x%02x] = 0x%02x\n", addr, data);
1223 init_wrvgai(init, 0x03d4, addr, data);
1228 * INIT_CONDITION_TIME - opcode 0x56
1232 init_condition_time(struct nvbios_init *init)
1234 struct nvkm_bios *bios = init->subdev->device->bios;
1235 u8 cond = nvbios_rd08(bios, init->offset + 1);
1236 u8 retry = nvbios_rd08(bios, init->offset + 2);
1237 u8 wait = min((u16)retry * 50, 100);
1239 trace("CONDITION_TIME\t0x%02x 0x%02x\n", cond, retry);
1242 if (!init_exec(init))
1246 if (init_condition_met(init, cond))
1251 init_exec_set(init, false);
1255 * INIT_LTIME - opcode 0x57
1259 init_ltime(struct nvbios_init *init)
1261 struct nvkm_bios *bios = init->subdev->device->bios;
1262 u16 msec = nvbios_rd16(bios, init->offset + 1);
1264 trace("LTIME\t0x%04x\n", msec);
1267 if (init_exec(init))
1272 * INIT_ZM_REG_SEQUENCE - opcode 0x58
1276 init_zm_reg_sequence(struct nvbios_init *init)
1278 struct nvkm_bios *bios = init->subdev->device->bios;
1279 u32 base = nvbios_rd32(bios, init->offset + 1);
1280 u8 count = nvbios_rd08(bios, init->offset + 5);
1282 trace("ZM_REG_SEQUENCE\t0x%02x\n", count);
1286 u32 data = nvbios_rd32(bios, init->offset);
1288 trace("\t\tR[0x%06x] = 0x%08x\n", base, data);
1291 init_wr32(init, base, data);
1297 * INIT_PLL_INDIRECT - opcode 0x59
1301 init_pll_indirect(struct nvbios_init *init)
1303 struct nvkm_bios *bios = init->subdev->device->bios;
1304 u32 reg = nvbios_rd32(bios, init->offset + 1);
1305 u16 addr = nvbios_rd16(bios, init->offset + 5);
1306 u32 freq = (u32)nvbios_rd16(bios, addr) * 1000;
1308 trace("PLL_INDIRECT\tR[0x%06x] =PLL= VBIOS[%04x] = %dkHz\n",
1312 init_prog_pll(init, reg, freq);
1316 * INIT_ZM_REG_INDIRECT - opcode 0x5a
1320 init_zm_reg_indirect(struct nvbios_init *init)
1322 struct nvkm_bios *bios = init->subdev->device->bios;
1323 u32 reg = nvbios_rd32(bios, init->offset + 1);
1324 u16 addr = nvbios_rd16(bios, init->offset + 5);
1325 u32 data = nvbios_rd32(bios, addr);
1327 trace("ZM_REG_INDIRECT\tR[0x%06x] = VBIOS[0x%04x] = 0x%08x\n",
1331 init_wr32(init, addr, data);
1335 * INIT_SUB_DIRECT - opcode 0x5b
1339 init_sub_direct(struct nvbios_init *init)
1341 struct nvkm_bios *bios = init->subdev->device->bios;
1342 u16 addr = nvbios_rd16(bios, init->offset + 1);
1345 trace("SUB_DIRECT\t0x%04x\n", addr);
1347 if (init_exec(init)) {
1348 save = init->offset;
1349 init->offset = addr;
1350 if (nvbios_exec(init)) {
1351 error("error parsing sub-table\n");
1354 init->offset = save;
1361 * INIT_JUMP - opcode 0x5c
1365 init_jump(struct nvbios_init *init)
1367 struct nvkm_bios *bios = init->subdev->device->bios;
1368 u16 offset = nvbios_rd16(bios, init->offset + 1);
1370 trace("JUMP\t0x%04x\n", offset);
1372 if (init_exec(init))
1373 init->offset = offset;
1379 * INIT_I2C_IF - opcode 0x5e
1383 init_i2c_if(struct nvbios_init *init)
1385 struct nvkm_bios *bios = init->subdev->device->bios;
1386 u8 index = nvbios_rd08(bios, init->offset + 1);
1387 u8 addr = nvbios_rd08(bios, init->offset + 2);
1388 u8 reg = nvbios_rd08(bios, init->offset + 3);
1389 u8 mask = nvbios_rd08(bios, init->offset + 4);
1390 u8 data = nvbios_rd08(bios, init->offset + 5);
1393 trace("I2C_IF\tI2C[0x%02x][0x%02x][0x%02x] & 0x%02x == 0x%02x\n",
1394 index, addr, reg, mask, data);
1396 init_exec_force(init, true);
1398 value = init_rdi2cr(init, index, addr, reg);
1399 if ((value & mask) != data)
1400 init_exec_set(init, false);
1402 init_exec_force(init, false);
1406 * INIT_COPY_NV_REG - opcode 0x5f
1410 init_copy_nv_reg(struct nvbios_init *init)
1412 struct nvkm_bios *bios = init->subdev->device->bios;
1413 u32 sreg = nvbios_rd32(bios, init->offset + 1);
1414 u8 shift = nvbios_rd08(bios, init->offset + 5);
1415 u32 smask = nvbios_rd32(bios, init->offset + 6);
1416 u32 sxor = nvbios_rd32(bios, init->offset + 10);
1417 u32 dreg = nvbios_rd32(bios, init->offset + 14);
1418 u32 dmask = nvbios_rd32(bios, init->offset + 18);
1421 trace("COPY_NV_REG\tR[0x%06x] &= 0x%08x |= "
1422 "((R[0x%06x] %s 0x%02x) & 0x%08x ^ 0x%08x)\n",
1423 dreg, dmask, sreg, (shift & 0x80) ? "<<" : ">>",
1424 (shift & 0x80) ? (0x100 - shift) : shift, smask, sxor);
1427 data = init_shift(init_rd32(init, sreg), shift);
1428 init_mask(init, dreg, ~dmask, (data & smask) ^ sxor);
1432 * INIT_ZM_INDEX_IO - opcode 0x62
1436 init_zm_index_io(struct nvbios_init *init)
1438 struct nvkm_bios *bios = init->subdev->device->bios;
1439 u16 port = nvbios_rd16(bios, init->offset + 1);
1440 u8 index = nvbios_rd08(bios, init->offset + 3);
1441 u8 data = nvbios_rd08(bios, init->offset + 4);
1443 trace("ZM_INDEX_IO\tI[0x%04x][0x%02x] = 0x%02x\n", port, index, data);
1446 init_wrvgai(init, port, index, data);
1450 * INIT_COMPUTE_MEM - opcode 0x63
1454 init_compute_mem(struct nvbios_init *init)
1456 struct nvkm_devinit *devinit = init->subdev->device->devinit;
1458 trace("COMPUTE_MEM\n");
1461 init_exec_force(init, true);
1462 if (init_exec(init))
1463 nvkm_devinit_meminit(devinit);
1464 init_exec_force(init, false);
1468 * INIT_RESET - opcode 0x65
1472 init_reset(struct nvbios_init *init)
1474 struct nvkm_bios *bios = init->subdev->device->bios;
1475 u32 reg = nvbios_rd32(bios, init->offset + 1);
1476 u32 data1 = nvbios_rd32(bios, init->offset + 5);
1477 u32 data2 = nvbios_rd32(bios, init->offset + 9);
1480 trace("RESET\tR[0x%08x] = 0x%08x, 0x%08x", reg, data1, data2);
1482 init_exec_force(init, true);
1484 savepci19 = init_mask(init, 0x00184c, 0x00000f00, 0x00000000);
1485 init_wr32(init, reg, data1);
1487 init_wr32(init, reg, data2);
1488 init_wr32(init, 0x00184c, savepci19);
1489 init_mask(init, 0x001850, 0x00000001, 0x00000000);
1491 init_exec_force(init, false);
1495 * INIT_CONFIGURE_MEM - opcode 0x66
1499 init_configure_mem_clk(struct nvbios_init *init)
1501 u16 mdata = bmp_mem_init_table(init->subdev->device->bios);
1503 mdata += (init_rdvgai(init, 0x03d4, 0x3c) >> 4) * 66;
1508 init_configure_mem(struct nvbios_init *init)
1510 struct nvkm_bios *bios = init->subdev->device->bios;
1514 trace("CONFIGURE_MEM\n");
1517 if (bios->version.major > 2) {
1521 init_exec_force(init, true);
1523 mdata = init_configure_mem_clk(init);
1524 sdata = bmp_sdr_seq_table(bios);
1525 if (nvbios_rd08(bios, mdata) & 0x01)
1526 sdata = bmp_ddr_seq_table(bios);
1527 mdata += 6; /* skip to data */
1529 data = init_rdvgai(init, 0x03c4, 0x01);
1530 init_wrvgai(init, 0x03c4, 0x01, data | 0x20);
1532 for (; (addr = nvbios_rd32(bios, sdata)) != 0xffffffff; sdata += 4) {
1534 case 0x10021c: /* CKE_NORMAL */
1535 case 0x1002d0: /* CMD_REFRESH */
1536 case 0x1002d4: /* CMD_PRECHARGE */
1540 data = nvbios_rd32(bios, mdata);
1542 if (data == 0xffffffff)
1547 init_wr32(init, addr, data);
1550 init_exec_force(init, false);
1554 * INIT_CONFIGURE_CLK - opcode 0x67
1558 init_configure_clk(struct nvbios_init *init)
1560 struct nvkm_bios *bios = init->subdev->device->bios;
1563 trace("CONFIGURE_CLK\n");
1566 if (bios->version.major > 2) {
1570 init_exec_force(init, true);
1572 mdata = init_configure_mem_clk(init);
1575 clock = nvbios_rd16(bios, mdata + 4) * 10;
1576 init_prog_pll(init, 0x680500, clock);
1579 clock = nvbios_rd16(bios, mdata + 2) * 10;
1580 if (nvbios_rd08(bios, mdata) & 0x01)
1582 init_prog_pll(init, 0x680504, clock);
1584 init_exec_force(init, false);
1588 * INIT_CONFIGURE_PREINIT - opcode 0x68
1592 init_configure_preinit(struct nvbios_init *init)
1594 struct nvkm_bios *bios = init->subdev->device->bios;
1597 trace("CONFIGURE_PREINIT\n");
1600 if (bios->version.major > 2) {
1604 init_exec_force(init, true);
1606 strap = init_rd32(init, 0x101000);
1607 strap = ((strap << 2) & 0xf0) | ((strap & 0x40) >> 6);
1608 init_wrvgai(init, 0x03d4, 0x3c, strap);
1610 init_exec_force(init, false);
1614 * INIT_IO - opcode 0x69
1618 init_io(struct nvbios_init *init)
1620 struct nvkm_bios *bios = init->subdev->device->bios;
1621 u16 port = nvbios_rd16(bios, init->offset + 1);
1622 u8 mask = nvbios_rd16(bios, init->offset + 3);
1623 u8 data = nvbios_rd16(bios, init->offset + 4);
1626 trace("IO\t\tI[0x%04x] &= 0x%02x |= 0x%02x\n", port, mask, data);
1629 /* ummm.. yes.. should really figure out wtf this is and why it's
1630 * needed some day.. it's almost certainly wrong, but, it also
1631 * somehow makes things work...
1633 if (bios->subdev.device->card_type >= NV_50 &&
1634 port == 0x03c3 && data == 0x01) {
1635 init_mask(init, 0x614100, 0xf0800000, 0x00800000);
1636 init_mask(init, 0x00e18c, 0x00020000, 0x00020000);
1637 init_mask(init, 0x614900, 0xf0800000, 0x00800000);
1638 init_mask(init, 0x000200, 0x40000000, 0x00000000);
1640 init_mask(init, 0x00e18c, 0x00020000, 0x00000000);
1641 init_mask(init, 0x000200, 0x40000000, 0x40000000);
1642 init_wr32(init, 0x614100, 0x00800018);
1643 init_wr32(init, 0x614900, 0x00800018);
1645 init_wr32(init, 0x614100, 0x10000018);
1646 init_wr32(init, 0x614900, 0x10000018);
1649 value = init_rdport(init, port) & mask;
1650 init_wrport(init, port, data | value);
1654 * INIT_SUB - opcode 0x6b
1658 init_sub(struct nvbios_init *init)
1660 struct nvkm_bios *bios = init->subdev->device->bios;
1661 u8 index = nvbios_rd08(bios, init->offset + 1);
1664 trace("SUB\t0x%02x\n", index);
1666 addr = init_script(bios, index);
1667 if (addr && init_exec(init)) {
1668 save = init->offset;
1669 init->offset = addr;
1670 if (nvbios_exec(init)) {
1671 error("error parsing sub-table\n");
1674 init->offset = save;
1681 * INIT_RAM_CONDITION - opcode 0x6d
1685 init_ram_condition(struct nvbios_init *init)
1687 struct nvkm_bios *bios = init->subdev->device->bios;
1688 u8 mask = nvbios_rd08(bios, init->offset + 1);
1689 u8 value = nvbios_rd08(bios, init->offset + 2);
1691 trace("RAM_CONDITION\t"
1692 "(R[0x100000] & 0x%02x) == 0x%02x\n", mask, value);
1695 if ((init_rd32(init, 0x100000) & mask) != value)
1696 init_exec_set(init, false);
1700 * INIT_NV_REG - opcode 0x6e
1704 init_nv_reg(struct nvbios_init *init)
1706 struct nvkm_bios *bios = init->subdev->device->bios;
1707 u32 reg = nvbios_rd32(bios, init->offset + 1);
1708 u32 mask = nvbios_rd32(bios, init->offset + 5);
1709 u32 data = nvbios_rd32(bios, init->offset + 9);
1711 trace("NV_REG\tR[0x%06x] &= 0x%08x |= 0x%08x\n", reg, mask, data);
1714 init_mask(init, reg, ~mask, data);
1718 * INIT_MACRO - opcode 0x6f
1722 init_macro(struct nvbios_init *init)
1724 struct nvkm_bios *bios = init->subdev->device->bios;
1725 u8 macro = nvbios_rd08(bios, init->offset + 1);
1728 trace("MACRO\t0x%02x\n", macro);
1730 table = init_macro_table(init);
1732 u32 addr = nvbios_rd32(bios, table + (macro * 8) + 0);
1733 u32 data = nvbios_rd32(bios, table + (macro * 8) + 4);
1734 trace("\t\tR[0x%06x] = 0x%08x\n", addr, data);
1735 init_wr32(init, addr, data);
1742 * INIT_RESUME - opcode 0x72
1746 init_resume(struct nvbios_init *init)
1750 init_exec_set(init, true);
1754 * INIT_STRAP_CONDITION - opcode 0x73
1758 init_strap_condition(struct nvbios_init *init)
1760 struct nvkm_bios *bios = init->subdev->device->bios;
1761 u32 mask = nvbios_rd32(bios, init->offset + 1);
1762 u32 value = nvbios_rd32(bios, init->offset + 5);
1764 trace("STRAP_CONDITION\t(R[0x101000] & 0x%08x) == 0x%08x\n", mask, value);
1767 if ((init_rd32(init, 0x101000) & mask) != value)
1768 init_exec_set(init, false);
1772 * INIT_TIME - opcode 0x74
1776 init_time(struct nvbios_init *init)
1778 struct nvkm_bios *bios = init->subdev->device->bios;
1779 u16 usec = nvbios_rd16(bios, init->offset + 1);
1781 trace("TIME\t0x%04x\n", usec);
1784 if (init_exec(init)) {
1788 mdelay((usec + 900) / 1000);
1793 * INIT_CONDITION - opcode 0x75
1797 init_condition(struct nvbios_init *init)
1799 struct nvkm_bios *bios = init->subdev->device->bios;
1800 u8 cond = nvbios_rd08(bios, init->offset + 1);
1802 trace("CONDITION\t0x%02x\n", cond);
1805 if (!init_condition_met(init, cond))
1806 init_exec_set(init, false);
1810 * INIT_IO_CONDITION - opcode 0x76
1814 init_io_condition(struct nvbios_init *init)
1816 struct nvkm_bios *bios = init->subdev->device->bios;
1817 u8 cond = nvbios_rd08(bios, init->offset + 1);
1819 trace("IO_CONDITION\t0x%02x\n", cond);
1822 if (!init_io_condition_met(init, cond))
1823 init_exec_set(init, false);
1827 * INIT_ZM_REG16 - opcode 0x77
1831 init_zm_reg16(struct nvbios_init *init)
1833 struct nvkm_bios *bios = init->subdev->device->bios;
1834 u32 addr = nvbios_rd32(bios, init->offset + 1);
1835 u16 data = nvbios_rd16(bios, init->offset + 5);
1837 trace("ZM_REG\tR[0x%06x] = 0x%04x\n", addr, data);
1840 init_wr32(init, addr, data);
1844 * INIT_INDEX_IO - opcode 0x78
1848 init_index_io(struct nvbios_init *init)
1850 struct nvkm_bios *bios = init->subdev->device->bios;
1851 u16 port = nvbios_rd16(bios, init->offset + 1);
1852 u8 index = nvbios_rd16(bios, init->offset + 3);
1853 u8 mask = nvbios_rd08(bios, init->offset + 4);
1854 u8 data = nvbios_rd08(bios, init->offset + 5);
1857 trace("INDEX_IO\tI[0x%04x][0x%02x] &= 0x%02x |= 0x%02x\n",
1858 port, index, mask, data);
1861 value = init_rdvgai(init, port, index) & mask;
1862 init_wrvgai(init, port, index, data | value);
1866 * INIT_PLL - opcode 0x79
1870 init_pll(struct nvbios_init *init)
1872 struct nvkm_bios *bios = init->subdev->device->bios;
1873 u32 reg = nvbios_rd32(bios, init->offset + 1);
1874 u32 freq = nvbios_rd16(bios, init->offset + 5) * 10;
1876 trace("PLL\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
1879 init_prog_pll(init, reg, freq);
1883 * INIT_ZM_REG - opcode 0x7a
1887 init_zm_reg(struct nvbios_init *init)
1889 struct nvkm_bios *bios = init->subdev->device->bios;
1890 u32 addr = nvbios_rd32(bios, init->offset + 1);
1891 u32 data = nvbios_rd32(bios, init->offset + 5);
1893 trace("ZM_REG\tR[0x%06x] = 0x%08x\n", addr, data);
1896 if (addr == 0x000200)
1899 init_wr32(init, addr, data);
1903 * INIT_RAM_RESTRICT_PLL - opcde 0x87
1907 init_ram_restrict_pll(struct nvbios_init *init)
1909 struct nvkm_bios *bios = init->subdev->device->bios;
1910 u8 type = nvbios_rd08(bios, init->offset + 1);
1911 u8 count = init_ram_restrict_group_count(init);
1912 u8 strap = init_ram_restrict(init);
1915 trace("RAM_RESTRICT_PLL\t0x%02x\n", type);
1918 for (cconf = 0; cconf < count; cconf++) {
1919 u32 freq = nvbios_rd32(bios, init->offset);
1921 if (cconf == strap) {
1922 trace("%dkHz *\n", freq);
1923 init_prog_pll(init, type, freq);
1925 trace("%dkHz\n", freq);
1933 * INIT_GPIO - opcode 0x8e
1937 init_gpio(struct nvbios_init *init)
1939 struct nvkm_gpio *gpio = init->subdev->device->gpio;
1944 if (init_exec(init))
1945 nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED);
1949 * INIT_RAM_RESTRICT_ZM_GROUP - opcode 0x8f
1953 init_ram_restrict_zm_reg_group(struct nvbios_init *init)
1955 struct nvkm_bios *bios = init->subdev->device->bios;
1956 u32 addr = nvbios_rd32(bios, init->offset + 1);
1957 u8 incr = nvbios_rd08(bios, init->offset + 5);
1958 u8 num = nvbios_rd08(bios, init->offset + 6);
1959 u8 count = init_ram_restrict_group_count(init);
1960 u8 index = init_ram_restrict(init);
1963 trace("RAM_RESTRICT_ZM_REG_GROUP\t"
1964 "R[0x%08x] 0x%02x 0x%02x\n", addr, incr, num);
1967 for (i = 0; i < num; i++) {
1968 trace("\tR[0x%06x] = {\n", addr);
1969 for (j = 0; j < count; j++) {
1970 u32 data = nvbios_rd32(bios, init->offset);
1973 trace("\t\t0x%08x *\n", data);
1974 init_wr32(init, addr, data);
1976 trace("\t\t0x%08x\n", data);
1987 * INIT_COPY_ZM_REG - opcode 0x90
1991 init_copy_zm_reg(struct nvbios_init *init)
1993 struct nvkm_bios *bios = init->subdev->device->bios;
1994 u32 sreg = nvbios_rd32(bios, init->offset + 1);
1995 u32 dreg = nvbios_rd32(bios, init->offset + 5);
1997 trace("COPY_ZM_REG\tR[0x%06x] = R[0x%06x]\n", dreg, sreg);
2000 init_wr32(init, dreg, init_rd32(init, sreg));
2004 * INIT_ZM_REG_GROUP - opcode 0x91
2008 init_zm_reg_group(struct nvbios_init *init)
2010 struct nvkm_bios *bios = init->subdev->device->bios;
2011 u32 addr = nvbios_rd32(bios, init->offset + 1);
2012 u8 count = nvbios_rd08(bios, init->offset + 5);
2014 trace("ZM_REG_GROUP\tR[0x%06x] =\n", addr);
2018 u32 data = nvbios_rd32(bios, init->offset);
2019 trace("\t0x%08x\n", data);
2020 init_wr32(init, addr, data);
2026 * INIT_XLAT - opcode 0x96
2030 init_xlat(struct nvbios_init *init)
2032 struct nvkm_bios *bios = init->subdev->device->bios;
2033 u32 saddr = nvbios_rd32(bios, init->offset + 1);
2034 u8 sshift = nvbios_rd08(bios, init->offset + 5);
2035 u8 smask = nvbios_rd08(bios, init->offset + 6);
2036 u8 index = nvbios_rd08(bios, init->offset + 7);
2037 u32 daddr = nvbios_rd32(bios, init->offset + 8);
2038 u32 dmask = nvbios_rd32(bios, init->offset + 12);
2039 u8 shift = nvbios_rd08(bios, init->offset + 16);
2042 trace("INIT_XLAT\tR[0x%06x] &= 0x%08x |= "
2043 "(X%02x((R[0x%06x] %s 0x%02x) & 0x%02x) << 0x%02x)\n",
2044 daddr, dmask, index, saddr, (sshift & 0x80) ? "<<" : ">>",
2045 (sshift & 0x80) ? (0x100 - sshift) : sshift, smask, shift);
2048 data = init_shift(init_rd32(init, saddr), sshift) & smask;
2049 data = init_xlat_(init, index, data) << shift;
2050 init_mask(init, daddr, ~dmask, data);
2054 * INIT_ZM_MASK_ADD - opcode 0x97
2058 init_zm_mask_add(struct nvbios_init *init)
2060 struct nvkm_bios *bios = init->subdev->device->bios;
2061 u32 addr = nvbios_rd32(bios, init->offset + 1);
2062 u32 mask = nvbios_rd32(bios, init->offset + 5);
2063 u32 add = nvbios_rd32(bios, init->offset + 9);
2066 trace("ZM_MASK_ADD\tR[0x%06x] &= 0x%08x += 0x%08x\n", addr, mask, add);
2069 data = init_rd32(init, addr);
2070 data = (data & mask) | ((data + add) & ~mask);
2071 init_wr32(init, addr, data);
2075 * INIT_AUXCH - opcode 0x98
2079 init_auxch(struct nvbios_init *init)
2081 struct nvkm_bios *bios = init->subdev->device->bios;
2082 u32 addr = nvbios_rd32(bios, init->offset + 1);
2083 u8 count = nvbios_rd08(bios, init->offset + 5);
2085 trace("AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
2089 u8 mask = nvbios_rd08(bios, init->offset + 0);
2090 u8 data = nvbios_rd08(bios, init->offset + 1);
2091 trace("\tAUX[0x%08x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
2092 mask = init_rdauxr(init, addr) & mask;
2093 init_wrauxr(init, addr, mask | data);
2099 * INIT_AUXCH - opcode 0x99
2103 init_zm_auxch(struct nvbios_init *init)
2105 struct nvkm_bios *bios = init->subdev->device->bios;
2106 u32 addr = nvbios_rd32(bios, init->offset + 1);
2107 u8 count = nvbios_rd08(bios, init->offset + 5);
2109 trace("ZM_AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
2113 u8 data = nvbios_rd08(bios, init->offset + 0);
2114 trace("\tAUX[0x%08x] = 0x%02x\n", addr, data);
2115 init_wrauxr(init, addr, data);
2121 * INIT_I2C_LONG_IF - opcode 0x9a
2125 init_i2c_long_if(struct nvbios_init *init)
2127 struct nvkm_bios *bios = init->subdev->device->bios;
2128 u8 index = nvbios_rd08(bios, init->offset + 1);
2129 u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
2130 u8 reglo = nvbios_rd08(bios, init->offset + 3);
2131 u8 reghi = nvbios_rd08(bios, init->offset + 4);
2132 u8 mask = nvbios_rd08(bios, init->offset + 5);
2133 u8 data = nvbios_rd08(bios, init->offset + 6);
2134 struct i2c_adapter *adap;
2136 trace("I2C_LONG_IF\t"
2137 "I2C[0x%02x][0x%02x][0x%02x%02x] & 0x%02x == 0x%02x\n",
2138 index, addr, reglo, reghi, mask, data);
2141 adap = init_i2c(init, index);
2143 u8 i[2] = { reghi, reglo };
2145 struct i2c_msg msg[] = {
2146 { .addr = addr, .flags = 0, .len = 2, .buf = i },
2147 { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = o }
2151 ret = i2c_transfer(adap, msg, 2);
2152 if (ret == 2 && ((o[0] & mask) == data))
2156 init_exec_set(init, false);
2160 * INIT_GPIO_NE - opcode 0xa9
2164 init_gpio_ne(struct nvbios_init *init)
2166 struct nvkm_bios *bios = init->subdev->device->bios;
2167 struct nvkm_gpio *gpio = bios->subdev.device->gpio;
2168 struct dcb_gpio_func func;
2169 u8 count = nvbios_rd08(bios, init->offset + 1);
2170 u8 idx = 0, ver, len;
2176 for (i = init->offset; i < init->offset + count; i++)
2177 cont("0x%02x ", nvbios_rd08(bios, i));
2180 while ((data = dcb_gpio_parse(bios, 0, idx++, &ver, &len, &func))) {
2181 if (func.func != DCB_GPIO_UNUSED) {
2182 for (i = init->offset; i < init->offset + count; i++) {
2183 if (func.func == nvbios_rd08(bios, i))
2187 trace("\tFUNC[0x%02x]", func.func);
2188 if (i == (init->offset + count)) {
2190 if (init_exec(init))
2191 nvkm_gpio_reset(gpio, func.func);
2197 init->offset += count;
2200 static struct nvbios_init_opcode {
2201 void (*exec)(struct nvbios_init *);
2203 [0x32] = { init_io_restrict_prog },
2204 [0x33] = { init_repeat },
2205 [0x34] = { init_io_restrict_pll },
2206 [0x36] = { init_end_repeat },
2207 [0x37] = { init_copy },
2208 [0x38] = { init_not },
2209 [0x39] = { init_io_flag_condition },
2210 [0x3a] = { init_generic_condition },
2211 [0x3b] = { init_io_mask_or },
2212 [0x3c] = { init_io_or },
2213 [0x47] = { init_andn_reg },
2214 [0x48] = { init_or_reg },
2215 [0x49] = { init_idx_addr_latched },
2216 [0x4a] = { init_io_restrict_pll2 },
2217 [0x4b] = { init_pll2 },
2218 [0x4c] = { init_i2c_byte },
2219 [0x4d] = { init_zm_i2c_byte },
2220 [0x4e] = { init_zm_i2c },
2221 [0x4f] = { init_tmds },
2222 [0x50] = { init_zm_tmds_group },
2223 [0x51] = { init_cr_idx_adr_latch },
2224 [0x52] = { init_cr },
2225 [0x53] = { init_zm_cr },
2226 [0x54] = { init_zm_cr_group },
2227 [0x56] = { init_condition_time },
2228 [0x57] = { init_ltime },
2229 [0x58] = { init_zm_reg_sequence },
2230 [0x59] = { init_pll_indirect },
2231 [0x5a] = { init_zm_reg_indirect },
2232 [0x5b] = { init_sub_direct },
2233 [0x5c] = { init_jump },
2234 [0x5e] = { init_i2c_if },
2235 [0x5f] = { init_copy_nv_reg },
2236 [0x62] = { init_zm_index_io },
2237 [0x63] = { init_compute_mem },
2238 [0x65] = { init_reset },
2239 [0x66] = { init_configure_mem },
2240 [0x67] = { init_configure_clk },
2241 [0x68] = { init_configure_preinit },
2242 [0x69] = { init_io },
2243 [0x6b] = { init_sub },
2244 [0x6d] = { init_ram_condition },
2245 [0x6e] = { init_nv_reg },
2246 [0x6f] = { init_macro },
2247 [0x71] = { init_done },
2248 [0x72] = { init_resume },
2249 [0x73] = { init_strap_condition },
2250 [0x74] = { init_time },
2251 [0x75] = { init_condition },
2252 [0x76] = { init_io_condition },
2253 [0x77] = { init_zm_reg16 },
2254 [0x78] = { init_index_io },
2255 [0x79] = { init_pll },
2256 [0x7a] = { init_zm_reg },
2257 [0x87] = { init_ram_restrict_pll },
2258 [0x8c] = { init_reserved },
2259 [0x8d] = { init_reserved },
2260 [0x8e] = { init_gpio },
2261 [0x8f] = { init_ram_restrict_zm_reg_group },
2262 [0x90] = { init_copy_zm_reg },
2263 [0x91] = { init_zm_reg_group },
2264 [0x92] = { init_reserved },
2265 [0x96] = { init_xlat },
2266 [0x97] = { init_zm_mask_add },
2267 [0x98] = { init_auxch },
2268 [0x99] = { init_zm_auxch },
2269 [0x9a] = { init_i2c_long_if },
2270 [0xa9] = { init_gpio_ne },
2271 [0xaa] = { init_reserved },
2274 #define init_opcode_nr (sizeof(init_opcode) / sizeof(init_opcode[0]))
2277 nvbios_exec(struct nvbios_init *init)
2279 struct nvkm_bios *bios = init->subdev->device->bios;
2282 while (init->offset) {
2283 u8 opcode = nvbios_rd08(bios, init->offset);
2284 if (opcode >= init_opcode_nr || !init_opcode[opcode].exec) {
2285 error("unknown opcode 0x%02x\n", opcode);
2289 init_opcode[opcode].exec(init);
2296 nvbios_post(struct nvkm_subdev *subdev, bool execute)
2298 struct nvkm_bios *bios = subdev->device->bios;
2304 nvkm_debug(subdev, "running init tables\n");
2305 while (!ret && (data = (init_script(bios, ++i)))) {
2306 ret = nvbios_init(subdev, data,
2307 init.execute = execute ? 1 : 0;
2311 /* the vbios parser will run this right after the normal init
2312 * tables, whereas the binary driver appears to run it later.
2314 if (!ret && (data = init_unknown_script(bios))) {
2315 ret = nvbios_init(subdev, data,
2316 init.execute = execute ? 1 : 0;