Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / core / subdev / bios / init.c
1 #include <core/engine.h>
2 #include <core/device.h>
3
4 #include <subdev/bios.h>
5 #include <subdev/bios/bmp.h>
6 #include <subdev/bios/bit.h>
7 #include <subdev/bios/conn.h>
8 #include <subdev/bios/dcb.h>
9 #include <subdev/bios/dp.h>
10 #include <subdev/bios/gpio.h>
11 #include <subdev/bios/init.h>
12 #include <subdev/devinit.h>
13 #include <subdev/clock.h>
14 #include <subdev/i2c.h>
15 #include <subdev/vga.h>
16 #include <subdev/gpio.h>
17
18 #define bioslog(lvl, fmt, args...) do {                                        \
19         nv_printk(init->bios, lvl, "0x%04x[%c]: "fmt, init->offset,            \
20                   init_exec(init) ? '0' + (init->nested - 1) : ' ', ##args);   \
21 } while(0)
22 #define cont(fmt, args...) do {                                                \
23         if (nv_subdev(init->bios)->debug >= NV_DBG_TRACE)                      \
24                 printk(fmt, ##args);                                           \
25 } while(0)
26 #define trace(fmt, args...) bioslog(TRACE, fmt, ##args)
27 #define warn(fmt, args...) bioslog(WARN, fmt, ##args)
28 #define error(fmt, args...) bioslog(ERROR, fmt, ##args)
29
30 /******************************************************************************
31  * init parser control flow helpers
32  *****************************************************************************/
33
34 static inline bool
35 init_exec(struct nvbios_init *init)
36 {
37         return (init->execute == 1) || ((init->execute & 5) == 5);
38 }
39
40 static inline void
41 init_exec_set(struct nvbios_init *init, bool exec)
42 {
43         if (exec) init->execute &= 0xfd;
44         else      init->execute |= 0x02;
45 }
46
47 static inline void
48 init_exec_inv(struct nvbios_init *init)
49 {
50         init->execute ^= 0x02;
51 }
52
53 static inline void
54 init_exec_force(struct nvbios_init *init, bool exec)
55 {
56         if (exec) init->execute |= 0x04;
57         else      init->execute &= 0xfb;
58 }
59
60 /******************************************************************************
61  * init parser wrappers for normal register/i2c/whatever accessors
62  *****************************************************************************/
63
64 static inline int
65 init_or(struct nvbios_init *init)
66 {
67         if (init_exec(init)) {
68                 if (init->outp)
69                         return ffs(init->outp->or) - 1;
70                 error("script needs OR!!\n");
71         }
72         return 0;
73 }
74
75 static inline int
76 init_link(struct nvbios_init *init)
77 {
78         if (init_exec(init)) {
79                 if (init->outp)
80                         return !(init->outp->sorconf.link & 1);
81                 error("script needs OR link\n");
82         }
83         return 0;
84 }
85
86 static inline int
87 init_crtc(struct nvbios_init *init)
88 {
89         if (init_exec(init)) {
90                 if (init->crtc >= 0)
91                         return init->crtc;
92                 error("script needs crtc\n");
93         }
94         return 0;
95 }
96
97 static u8
98 init_conn(struct nvbios_init *init)
99 {
100         struct nouveau_bios *bios = init->bios;
101         u8  ver, len;
102         u16 conn;
103
104         if (init_exec(init)) {
105                 if (init->outp) {
106                         conn = init->outp->connector;
107                         conn = dcb_conn(bios, conn, &ver, &len);
108                         if (conn)
109                                 return nv_ro08(bios, conn);
110                 }
111
112                 error("script needs connector type\n");
113         }
114
115         return 0xff;
116 }
117
118 static inline u32
119 init_nvreg(struct nvbios_init *init, u32 reg)
120 {
121         /* C51 (at least) sometimes has the lower bits set which the VBIOS
122          * interprets to mean that access needs to go through certain IO
123          * ports instead.  The NVIDIA binary driver has been seen to access
124          * these through the NV register address, so lets assume we can
125          * do the same
126          */
127         reg &= ~0x00000003;
128
129         /* GF8+ display scripts need register addresses mangled a bit to
130          * select a specific CRTC/OR
131          */
132         if (nv_device(init->bios)->card_type >= NV_50) {
133                 if (reg & 0x80000000) {
134                         reg += init_crtc(init) * 0x800;
135                         reg &= ~0x80000000;
136                 }
137
138                 if (reg & 0x40000000) {
139                         reg += init_or(init) * 0x800;
140                         reg &= ~0x40000000;
141                         if (reg & 0x20000000) {
142                                 reg += init_link(init) * 0x80;
143                                 reg &= ~0x20000000;
144                         }
145                 }
146         }
147
148         if (reg & ~0x00fffffc)
149                 warn("unknown bits in register 0x%08x\n", reg);
150         return reg;
151 }
152
153 static u32
154 init_rd32(struct nvbios_init *init, u32 reg)
155 {
156         reg = init_nvreg(init, reg);
157         if (init_exec(init))
158                 return nv_rd32(init->subdev, reg);
159         return 0x00000000;
160 }
161
162 static void
163 init_wr32(struct nvbios_init *init, u32 reg, u32 val)
164 {
165         reg = init_nvreg(init, reg);
166         if (init_exec(init))
167                 nv_wr32(init->subdev, reg, val);
168 }
169
170 static u32
171 init_mask(struct nvbios_init *init, u32 reg, u32 mask, u32 val)
172 {
173         reg = init_nvreg(init, reg);
174         if (init_exec(init)) {
175                 u32 tmp = nv_rd32(init->subdev, reg);
176                 nv_wr32(init->subdev, reg, (tmp & ~mask) | val);
177                 return tmp;
178         }
179         return 0x00000000;
180 }
181
182 static u8
183 init_rdport(struct nvbios_init *init, u16 port)
184 {
185         if (init_exec(init))
186                 return nv_rdport(init->subdev, init->crtc, port);
187         return 0x00;
188 }
189
190 static void
191 init_wrport(struct nvbios_init *init, u16 port, u8 value)
192 {
193         if (init_exec(init))
194                 nv_wrport(init->subdev, init->crtc, port, value);
195 }
196
197 static u8
198 init_rdvgai(struct nvbios_init *init, u16 port, u8 index)
199 {
200         struct nouveau_subdev *subdev = init->subdev;
201         if (init_exec(init)) {
202                 int head = init->crtc < 0 ? 0 : init->crtc;
203                 return nv_rdvgai(subdev, head, port, index);
204         }
205         return 0x00;
206 }
207
208 static void
209 init_wrvgai(struct nvbios_init *init, u16 port, u8 index, u8 value)
210 {
211         /* force head 0 for updates to cr44, it only exists on first head */
212         if (nv_device(init->subdev)->card_type < NV_50) {
213                 if (port == 0x03d4 && index == 0x44)
214                         init->crtc = 0;
215         }
216
217         if (init_exec(init)) {
218                 int head = init->crtc < 0 ? 0 : init->crtc;
219                 nv_wrvgai(init->subdev, head, port, index, value);
220         }
221
222         /* select head 1 if cr44 write selected it */
223         if (nv_device(init->subdev)->card_type < NV_50) {
224                 if (port == 0x03d4 && index == 0x44 && value == 3)
225                         init->crtc = 1;
226         }
227 }
228
229 static struct nouveau_i2c_port *
230 init_i2c(struct nvbios_init *init, int index)
231 {
232         struct nouveau_i2c *i2c = nouveau_i2c(init->bios);
233
234         if (index == 0xff) {
235                 index = NV_I2C_DEFAULT(0);
236                 if (init->outp && init->outp->i2c_upper_default)
237                         index = NV_I2C_DEFAULT(1);
238         } else
239         if (index < 0) {
240                 if (!init->outp) {
241                         if (init_exec(init))
242                                 error("script needs output for i2c\n");
243                         return NULL;
244                 }
245
246                 if (index == -2 && init->outp->location) {
247                         index = NV_I2C_TYPE_EXTAUX(init->outp->extdev);
248                         return i2c->find_type(i2c, index);
249                 }
250
251                 index = init->outp->i2c_index;
252         }
253
254         return i2c->find(i2c, index);
255 }
256
257 static int
258 init_rdi2cr(struct nvbios_init *init, u8 index, u8 addr, u8 reg)
259 {
260         struct nouveau_i2c_port *port = init_i2c(init, index);
261         if (port && init_exec(init))
262                 return nv_rdi2cr(port, addr, reg);
263         return -ENODEV;
264 }
265
266 static int
267 init_wri2cr(struct nvbios_init *init, u8 index, u8 addr, u8 reg, u8 val)
268 {
269         struct nouveau_i2c_port *port = init_i2c(init, index);
270         if (port && init_exec(init))
271                 return nv_wri2cr(port, addr, reg, val);
272         return -ENODEV;
273 }
274
275 static int
276 init_rdauxr(struct nvbios_init *init, u32 addr)
277 {
278         struct nouveau_i2c_port *port = init_i2c(init, -2);
279         u8 data;
280
281         if (port && init_exec(init)) {
282                 int ret = nv_rdaux(port, addr, &data, 1);
283                 if (ret)
284                         return ret;
285                 return data;
286         }
287
288         return -ENODEV;
289 }
290
291 static int
292 init_wrauxr(struct nvbios_init *init, u32 addr, u8 data)
293 {
294         struct nouveau_i2c_port *port = init_i2c(init, -2);
295         if (port && init_exec(init))
296                 return nv_wraux(port, addr, &data, 1);
297         return -ENODEV;
298 }
299
300 static void
301 init_prog_pll(struct nvbios_init *init, u32 id, u32 freq)
302 {
303         struct nouveau_clock *clk = nouveau_clock(init->bios);
304         if (clk && clk->pll_set && init_exec(init)) {
305                 int ret = clk->pll_set(clk, id, freq);
306                 if (ret)
307                         warn("failed to prog pll 0x%08x to %dkHz\n", id, freq);
308         }
309 }
310
311 /******************************************************************************
312  * parsing of bios structures that are required to execute init tables
313  *****************************************************************************/
314
315 static u16
316 init_table(struct nouveau_bios *bios, u16 *len)
317 {
318         struct bit_entry bit_I;
319
320         if (!bit_entry(bios, 'I', &bit_I)) {
321                 *len = bit_I.length;
322                 return bit_I.offset;
323         }
324
325         if (bmp_version(bios) >= 0x0510) {
326                 *len = 14;
327                 return bios->bmp_offset + 75;
328         }
329
330         return 0x0000;
331 }
332
333 static u16
334 init_table_(struct nvbios_init *init, u16 offset, const char *name)
335 {
336         struct nouveau_bios *bios = init->bios;
337         u16 len, data = init_table(bios, &len);
338         if (data) {
339                 if (len >= offset + 2) {
340                         data = nv_ro16(bios, data + offset);
341                         if (data)
342                                 return data;
343
344                         warn("%s pointer invalid\n", name);
345                         return 0x0000;
346                 }
347
348                 warn("init data too short for %s pointer", name);
349                 return 0x0000;
350         }
351
352         warn("init data not found\n");
353         return 0x0000;
354 }
355
356 #define init_script_table(b) init_table_((b), 0x00, "script table")
357 #define init_macro_index_table(b) init_table_((b), 0x02, "macro index table")
358 #define init_macro_table(b) init_table_((b), 0x04, "macro table")
359 #define init_condition_table(b) init_table_((b), 0x06, "condition table")
360 #define init_io_condition_table(b) init_table_((b), 0x08, "io condition table")
361 #define init_io_flag_condition_table(b) init_table_((b), 0x0a, "io flag conditon table")
362 #define init_function_table(b) init_table_((b), 0x0c, "function table")
363 #define init_xlat_table(b) init_table_((b), 0x10, "xlat table");
364
365 static u16
366 init_script(struct nouveau_bios *bios, int index)
367 {
368         struct nvbios_init init = { .bios = bios };
369         u16 bmp_ver = bmp_version(bios), data;
370
371         if (bmp_ver && bmp_ver < 0x0510) {
372                 if (index > 1 || bmp_ver < 0x0100)
373                         return 0x0000;
374
375                 data = bios->bmp_offset + (bmp_ver < 0x0200 ? 14 : 18);
376                 return nv_ro16(bios, data + (index * 2));
377         }
378
379         data = init_script_table(&init);
380         if (data)
381                 return nv_ro16(bios, data + (index * 2));
382
383         return 0x0000;
384 }
385
386 static u16
387 init_unknown_script(struct nouveau_bios *bios)
388 {
389         u16 len, data = init_table(bios, &len);
390         if (data && len >= 16)
391                 return nv_ro16(bios, data + 14);
392         return 0x0000;
393 }
394
395 static u16
396 init_ram_restrict_table(struct nvbios_init *init)
397 {
398         struct nouveau_bios *bios = init->bios;
399         struct bit_entry bit_M;
400         u16 data = 0x0000;
401
402         if (!bit_entry(bios, 'M', &bit_M)) {
403                 if (bit_M.version == 1 && bit_M.length >= 5)
404                         data = nv_ro16(bios, bit_M.offset + 3);
405                 if (bit_M.version == 2 && bit_M.length >= 3)
406                         data = nv_ro16(bios, bit_M.offset + 1);
407         }
408
409         if (data == 0x0000)
410                 warn("ram restrict table not found\n");
411         return data;
412 }
413
414 static u8
415 init_ram_restrict_group_count(struct nvbios_init *init)
416 {
417         struct nouveau_bios *bios = init->bios;
418         struct bit_entry bit_M;
419
420         if (!bit_entry(bios, 'M', &bit_M)) {
421                 if (bit_M.version == 1 && bit_M.length >= 5)
422                         return nv_ro08(bios, bit_M.offset + 2);
423                 if (bit_M.version == 2 && bit_M.length >= 3)
424                         return nv_ro08(bios, bit_M.offset + 0);
425         }
426
427         return 0x00;
428 }
429
430 static u8
431 init_ram_restrict_strap(struct nvbios_init *init)
432 {
433         /* This appears to be the behaviour of the VBIOS parser, and *is*
434          * important to cache the NV_PEXTDEV_BOOT0 on later chipsets to
435          * avoid fucking up the memory controller (somehow) by reading it
436          * on every INIT_RAM_RESTRICT_ZM_GROUP opcode.
437          *
438          * Preserving the non-caching behaviour on earlier chipsets just
439          * in case *not* re-reading the strap causes similar breakage.
440          */
441         if (!init->ramcfg || init->bios->version.major < 0x70)
442                 init->ramcfg = init_rd32(init, 0x101000);
443         return (init->ramcfg & 0x00000003c) >> 2;
444 }
445
446 static u8
447 init_ram_restrict(struct nvbios_init *init)
448 {
449         u8  strap = init_ram_restrict_strap(init);
450         u16 table = init_ram_restrict_table(init);
451         if (table)
452                 return nv_ro08(init->bios, table + strap);
453         return 0x00;
454 }
455
456 static u8
457 init_xlat_(struct nvbios_init *init, u8 index, u8 offset)
458 {
459         struct nouveau_bios *bios = init->bios;
460         u16 table = init_xlat_table(init);
461         if (table) {
462                 u16 data = nv_ro16(bios, table + (index * 2));
463                 if (data)
464                         return nv_ro08(bios, data + offset);
465                 warn("xlat table pointer %d invalid\n", index);
466         }
467         return 0x00;
468 }
469
470 /******************************************************************************
471  * utility functions used by various init opcode handlers
472  *****************************************************************************/
473
474 static bool
475 init_condition_met(struct nvbios_init *init, u8 cond)
476 {
477         struct nouveau_bios *bios = init->bios;
478         u16 table = init_condition_table(init);
479         if (table) {
480                 u32 reg = nv_ro32(bios, table + (cond * 12) + 0);
481                 u32 msk = nv_ro32(bios, table + (cond * 12) + 4);
482                 u32 val = nv_ro32(bios, table + (cond * 12) + 8);
483                 trace("\t[0x%02x] (R[0x%06x] & 0x%08x) == 0x%08x\n",
484                       cond, reg, msk, val);
485                 return (init_rd32(init, reg) & msk) == val;
486         }
487         return false;
488 }
489
490 static bool
491 init_io_condition_met(struct nvbios_init *init, u8 cond)
492 {
493         struct nouveau_bios *bios = init->bios;
494         u16 table = init_io_condition_table(init);
495         if (table) {
496                 u16 port = nv_ro16(bios, table + (cond * 5) + 0);
497                 u8 index = nv_ro08(bios, table + (cond * 5) + 2);
498                 u8  mask = nv_ro08(bios, table + (cond * 5) + 3);
499                 u8 value = nv_ro08(bios, table + (cond * 5) + 4);
500                 trace("\t[0x%02x] (0x%04x[0x%02x] & 0x%02x) == 0x%02x\n",
501                       cond, port, index, mask, value);
502                 return (init_rdvgai(init, port, index) & mask) == value;
503         }
504         return false;
505 }
506
507 static bool
508 init_io_flag_condition_met(struct nvbios_init *init, u8 cond)
509 {
510         struct nouveau_bios *bios = init->bios;
511         u16 table = init_io_flag_condition_table(init);
512         if (table) {
513                 u16 port = nv_ro16(bios, table + (cond * 9) + 0);
514                 u8 index = nv_ro08(bios, table + (cond * 9) + 2);
515                 u8  mask = nv_ro08(bios, table + (cond * 9) + 3);
516                 u8 shift = nv_ro08(bios, table + (cond * 9) + 4);
517                 u16 data = nv_ro16(bios, table + (cond * 9) + 5);
518                 u8 dmask = nv_ro08(bios, table + (cond * 9) + 7);
519                 u8 value = nv_ro08(bios, table + (cond * 9) + 8);
520                 u8 ioval = (init_rdvgai(init, port, index) & mask) >> shift;
521                 return (nv_ro08(bios, data + ioval) & dmask) == value;
522         }
523         return false;
524 }
525
526 static inline u32
527 init_shift(u32 data, u8 shift)
528 {
529         if (shift < 0x80)
530                 return data >> shift;
531         return data << (0x100 - shift);
532 }
533
534 static u32
535 init_tmds_reg(struct nvbios_init *init, u8 tmds)
536 {
537         /* For mlv < 0x80, it is an index into a table of TMDS base addresses.
538          * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
539          * CR58 for CR57 = 0 to index a table of offsets to the basic
540          * 0x6808b0 address.
541          * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
542          * CR58 for CR57 = 0 to index a table of offsets to the basic
543          * 0x6808b0 address, and then flip the offset by 8.
544          */
545
546         const int pramdac_offset[13] = {
547                 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
548         const u32 pramdac_table[4] = {
549                 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
550
551         if (tmds >= 0x80) {
552                 if (init->outp) {
553                         u32 dacoffset = pramdac_offset[init->outp->or];
554                         if (tmds == 0x81)
555                                 dacoffset ^= 8;
556                         return 0x6808b0 + dacoffset;
557                 }
558
559                 if (init_exec(init))
560                         error("tmds opcodes need dcb\n");
561         } else {
562                 if (tmds < ARRAY_SIZE(pramdac_table))
563                         return pramdac_table[tmds];
564
565                 error("tmds selector 0x%02x unknown\n", tmds);
566         }
567
568         return 0;
569 }
570
571 /******************************************************************************
572  * init opcode handlers
573  *****************************************************************************/
574
575 /**
576  * init_reserved - stub for various unknown/unused single-byte opcodes
577  *
578  */
579 static void
580 init_reserved(struct nvbios_init *init)
581 {
582         u8 opcode = nv_ro08(init->bios, init->offset);
583         u8 length, i;
584
585         switch (opcode) {
586         case 0xaa:
587                 length = 4;
588                 break;
589         default:
590                 length = 1;
591                 break;
592         }
593
594         trace("RESERVED 0x%02x\t", opcode);
595         for (i = 1; i < length; i++)
596                 cont(" 0x%02x", nv_ro08(init->bios, init->offset + i));
597         cont("\n");
598         init->offset += length;
599 }
600
601 /**
602  * INIT_DONE - opcode 0x71
603  *
604  */
605 static void
606 init_done(struct nvbios_init *init)
607 {
608         trace("DONE\n");
609         init->offset = 0x0000;
610 }
611
612 /**
613  * INIT_IO_RESTRICT_PROG - opcode 0x32
614  *
615  */
616 static void
617 init_io_restrict_prog(struct nvbios_init *init)
618 {
619         struct nouveau_bios *bios = init->bios;
620         u16 port = nv_ro16(bios, init->offset + 1);
621         u8 index = nv_ro08(bios, init->offset + 3);
622         u8  mask = nv_ro08(bios, init->offset + 4);
623         u8 shift = nv_ro08(bios, init->offset + 5);
624         u8 count = nv_ro08(bios, init->offset + 6);
625         u32  reg = nv_ro32(bios, init->offset + 7);
626         u8 conf, i;
627
628         trace("IO_RESTRICT_PROG\tR[0x%06x] = "
629               "((0x%04x[0x%02x] & 0x%02x) >> %d) [{\n",
630               reg, port, index, mask, shift);
631         init->offset += 11;
632
633         conf = (init_rdvgai(init, port, index) & mask) >> shift;
634         for (i = 0; i < count; i++) {
635                 u32 data = nv_ro32(bios, init->offset);
636
637                 if (i == conf) {
638                         trace("\t0x%08x *\n", data);
639                         init_wr32(init, reg, data);
640                 } else {
641                         trace("\t0x%08x\n", data);
642                 }
643
644                 init->offset += 4;
645         }
646         trace("}]\n");
647 }
648
649 /**
650  * INIT_REPEAT - opcode 0x33
651  *
652  */
653 static void
654 init_repeat(struct nvbios_init *init)
655 {
656         struct nouveau_bios *bios = init->bios;
657         u8 count = nv_ro08(bios, init->offset + 1);
658         u16 repeat = init->repeat;
659
660         trace("REPEAT\t0x%02x\n", count);
661         init->offset += 2;
662
663         init->repeat = init->offset;
664         init->repend = init->offset;
665         while (count--) {
666                 init->offset = init->repeat;
667                 nvbios_exec(init);
668                 if (count)
669                         trace("REPEAT\t0x%02x\n", count);
670         }
671         init->offset = init->repend;
672         init->repeat = repeat;
673 }
674
675 /**
676  * INIT_IO_RESTRICT_PLL - opcode 0x34
677  *
678  */
679 static void
680 init_io_restrict_pll(struct nvbios_init *init)
681 {
682         struct nouveau_bios *bios = init->bios;
683         u16 port = nv_ro16(bios, init->offset + 1);
684         u8 index = nv_ro08(bios, init->offset + 3);
685         u8  mask = nv_ro08(bios, init->offset + 4);
686         u8 shift = nv_ro08(bios, init->offset + 5);
687         s8  iofc = nv_ro08(bios, init->offset + 6);
688         u8 count = nv_ro08(bios, init->offset + 7);
689         u32  reg = nv_ro32(bios, init->offset + 8);
690         u8 conf, i;
691
692         trace("IO_RESTRICT_PLL\tR[0x%06x] =PLL= "
693               "((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) IOFCOND 0x%02x [{\n",
694               reg, port, index, mask, shift, iofc);
695         init->offset += 12;
696
697         conf = (init_rdvgai(init, port, index) & mask) >> shift;
698         for (i = 0; i < count; i++) {
699                 u32 freq = nv_ro16(bios, init->offset) * 10;
700
701                 if (i == conf) {
702                         trace("\t%dkHz *\n", freq);
703                         if (iofc > 0 && init_io_flag_condition_met(init, iofc))
704                                 freq *= 2;
705                         init_prog_pll(init, reg, freq);
706                 } else {
707                         trace("\t%dkHz\n", freq);
708                 }
709
710                 init->offset += 2;
711         }
712         trace("}]\n");
713 }
714
715 /**
716  * INIT_END_REPEAT - opcode 0x36
717  *
718  */
719 static void
720 init_end_repeat(struct nvbios_init *init)
721 {
722         trace("END_REPEAT\n");
723         init->offset += 1;
724
725         if (init->repeat) {
726                 init->repend = init->offset;
727                 init->offset = 0;
728         }
729 }
730
731 /**
732  * INIT_COPY - opcode 0x37
733  *
734  */
735 static void
736 init_copy(struct nvbios_init *init)
737 {
738         struct nouveau_bios *bios = init->bios;
739         u32  reg = nv_ro32(bios, init->offset + 1);
740         u8 shift = nv_ro08(bios, init->offset + 5);
741         u8 smask = nv_ro08(bios, init->offset + 6);
742         u16 port = nv_ro16(bios, init->offset + 7);
743         u8 index = nv_ro08(bios, init->offset + 9);
744         u8  mask = nv_ro08(bios, init->offset + 10);
745         u8  data;
746
747         trace("COPY\t0x%04x[0x%02x] &= 0x%02x |= "
748               "((R[0x%06x] %s 0x%02x) & 0x%02x)\n",
749               port, index, mask, reg, (shift & 0x80) ? "<<" : ">>",
750               (shift & 0x80) ? (0x100 - shift) : shift, smask);
751         init->offset += 11;
752
753         data  = init_rdvgai(init, port, index) & mask;
754         data |= init_shift(init_rd32(init, reg), shift) & smask;
755         init_wrvgai(init, port, index, data);
756 }
757
758 /**
759  * INIT_NOT - opcode 0x38
760  *
761  */
762 static void
763 init_not(struct nvbios_init *init)
764 {
765         trace("NOT\n");
766         init->offset += 1;
767         init_exec_inv(init);
768 }
769
770 /**
771  * INIT_IO_FLAG_CONDITION - opcode 0x39
772  *
773  */
774 static void
775 init_io_flag_condition(struct nvbios_init *init)
776 {
777         struct nouveau_bios *bios = init->bios;
778         u8 cond = nv_ro08(bios, init->offset + 1);
779
780         trace("IO_FLAG_CONDITION\t0x%02x\n", cond);
781         init->offset += 2;
782
783         if (!init_io_flag_condition_met(init, cond))
784                 init_exec_set(init, false);
785 }
786
787 /**
788  * INIT_DP_CONDITION - opcode 0x3a
789  *
790  */
791 static void
792 init_dp_condition(struct nvbios_init *init)
793 {
794         struct nouveau_bios *bios = init->bios;
795         struct nvbios_dpout info;
796         u8  cond = nv_ro08(bios, init->offset + 1);
797         u8  unkn = nv_ro08(bios, init->offset + 2);
798         u8  ver, hdr, cnt, len;
799         u16 data;
800
801         trace("DP_CONDITION\t0x%02x 0x%02x\n", cond, unkn);
802         init->offset += 3;
803
804         switch (cond) {
805         case 0:
806                 if (init_conn(init) != DCB_CONNECTOR_eDP)
807                         init_exec_set(init, false);
808                 break;
809         case 1:
810         case 2:
811                 if ( init->outp &&
812                     (data = nvbios_dpout_match(bios, DCB_OUTPUT_DP,
813                                                (init->outp->or << 0) |
814                                                (init->outp->sorconf.link << 6),
815                                                &ver, &hdr, &cnt, &len, &info)))
816                 {
817                         if (!(info.flags & cond))
818                                 init_exec_set(init, false);
819                         break;
820                 }
821
822                 if (init_exec(init))
823                         warn("script needs dp output table data\n");
824                 break;
825         case 5:
826                 if (!(init_rdauxr(init, 0x0d) & 1))
827                         init_exec_set(init, false);
828                 break;
829         default:
830                 warn("unknown dp condition 0x%02x\n", cond);
831                 break;
832         }
833 }
834
835 /**
836  * INIT_IO_MASK_OR - opcode 0x3b
837  *
838  */
839 static void
840 init_io_mask_or(struct nvbios_init *init)
841 {
842         struct nouveau_bios *bios = init->bios;
843         u8 index = nv_ro08(bios, init->offset + 1);
844         u8    or = init_or(init);
845         u8  data;
846
847         trace("IO_MASK_OR\t0x03d4[0x%02x] &= ~(1 << 0x%02x)\n", index, or);
848         init->offset += 2;
849
850         data = init_rdvgai(init, 0x03d4, index);
851         init_wrvgai(init, 0x03d4, index, data &= ~(1 << or));
852 }
853
854 /**
855  * INIT_IO_OR - opcode 0x3c
856  *
857  */
858 static void
859 init_io_or(struct nvbios_init *init)
860 {
861         struct nouveau_bios *bios = init->bios;
862         u8 index = nv_ro08(bios, init->offset + 1);
863         u8    or = init_or(init);
864         u8  data;
865
866         trace("IO_OR\t0x03d4[0x%02x] |= (1 << 0x%02x)\n", index, or);
867         init->offset += 2;
868
869         data = init_rdvgai(init, 0x03d4, index);
870         init_wrvgai(init, 0x03d4, index, data | (1 << or));
871 }
872
873 /**
874  * INIT_INDEX_ADDRESS_LATCHED - opcode 0x49
875  *
876  */
877 static void
878 init_idx_addr_latched(struct nvbios_init *init)
879 {
880         struct nouveau_bios *bios = init->bios;
881         u32 creg = nv_ro32(bios, init->offset + 1);
882         u32 dreg = nv_ro32(bios, init->offset + 5);
883         u32 mask = nv_ro32(bios, init->offset + 9);
884         u32 data = nv_ro32(bios, init->offset + 13);
885         u8 count = nv_ro08(bios, init->offset + 17);
886
887         trace("INDEX_ADDRESS_LATCHED\t"
888               "R[0x%06x] : R[0x%06x]\n\tCTRL &= 0x%08x |= 0x%08x\n",
889               creg, dreg, mask, data);
890         init->offset += 18;
891
892         while (count--) {
893                 u8 iaddr = nv_ro08(bios, init->offset + 0);
894                 u8 idata = nv_ro08(bios, init->offset + 1);
895
896                 trace("\t[0x%02x] = 0x%02x\n", iaddr, idata);
897                 init->offset += 2;
898
899                 init_wr32(init, dreg, idata);
900                 init_mask(init, creg, ~mask, data | iaddr);
901         }
902 }
903
904 /**
905  * INIT_IO_RESTRICT_PLL2 - opcode 0x4a
906  *
907  */
908 static void
909 init_io_restrict_pll2(struct nvbios_init *init)
910 {
911         struct nouveau_bios *bios = init->bios;
912         u16 port = nv_ro16(bios, init->offset + 1);
913         u8 index = nv_ro08(bios, init->offset + 3);
914         u8  mask = nv_ro08(bios, init->offset + 4);
915         u8 shift = nv_ro08(bios, init->offset + 5);
916         u8 count = nv_ro08(bios, init->offset + 6);
917         u32  reg = nv_ro32(bios, init->offset + 7);
918         u8  conf, i;
919
920         trace("IO_RESTRICT_PLL2\t"
921               "R[0x%06x] =PLL= ((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) [{\n",
922               reg, port, index, mask, shift);
923         init->offset += 11;
924
925         conf = (init_rdvgai(init, port, index) & mask) >> shift;
926         for (i = 0; i < count; i++) {
927                 u32 freq = nv_ro32(bios, init->offset);
928                 if (i == conf) {
929                         trace("\t%dkHz *\n", freq);
930                         init_prog_pll(init, reg, freq);
931                 } else {
932                         trace("\t%dkHz\n", freq);
933                 }
934                 init->offset += 4;
935         }
936         trace("}]\n");
937 }
938
939 /**
940  * INIT_PLL2 - opcode 0x4b
941  *
942  */
943 static void
944 init_pll2(struct nvbios_init *init)
945 {
946         struct nouveau_bios *bios = init->bios;
947         u32  reg = nv_ro32(bios, init->offset + 1);
948         u32 freq = nv_ro32(bios, init->offset + 5);
949
950         trace("PLL2\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
951         init->offset += 9;
952
953         init_prog_pll(init, reg, freq);
954 }
955
956 /**
957  * INIT_I2C_BYTE - opcode 0x4c
958  *
959  */
960 static void
961 init_i2c_byte(struct nvbios_init *init)
962 {
963         struct nouveau_bios *bios = init->bios;
964         u8 index = nv_ro08(bios, init->offset + 1);
965         u8  addr = nv_ro08(bios, init->offset + 2) >> 1;
966         u8 count = nv_ro08(bios, init->offset + 3);
967
968         trace("I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
969         init->offset += 4;
970
971         while (count--) {
972                 u8  reg = nv_ro08(bios, init->offset + 0);
973                 u8 mask = nv_ro08(bios, init->offset + 1);
974                 u8 data = nv_ro08(bios, init->offset + 2);
975                 int val;
976
977                 trace("\t[0x%02x] &= 0x%02x |= 0x%02x\n", reg, mask, data);
978                 init->offset += 3;
979
980                 val = init_rdi2cr(init, index, addr, reg);
981                 if (val < 0)
982                         continue;
983                 init_wri2cr(init, index, addr, reg, (val & mask) | data);
984         }
985 }
986
987 /**
988  * INIT_ZM_I2C_BYTE - opcode 0x4d
989  *
990  */
991 static void
992 init_zm_i2c_byte(struct nvbios_init *init)
993 {
994         struct nouveau_bios *bios = init->bios;
995         u8 index = nv_ro08(bios, init->offset + 1);
996         u8  addr = nv_ro08(bios, init->offset + 2) >> 1;
997         u8 count = nv_ro08(bios, init->offset + 3);
998
999         trace("ZM_I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
1000         init->offset += 4;
1001
1002         while (count--) {
1003                 u8  reg = nv_ro08(bios, init->offset + 0);
1004                 u8 data = nv_ro08(bios, init->offset + 1);
1005
1006                 trace("\t[0x%02x] = 0x%02x\n", reg, data);
1007                 init->offset += 2;
1008
1009                 init_wri2cr(init, index, addr, reg, data);
1010         }
1011
1012 }
1013
1014 /**
1015  * INIT_ZM_I2C - opcode 0x4e
1016  *
1017  */
1018 static void
1019 init_zm_i2c(struct nvbios_init *init)
1020 {
1021         struct nouveau_bios *bios = init->bios;
1022         u8 index = nv_ro08(bios, init->offset + 1);
1023         u8  addr = nv_ro08(bios, init->offset + 2) >> 1;
1024         u8 count = nv_ro08(bios, init->offset + 3);
1025         u8 data[256], i;
1026
1027         trace("ZM_I2C\tI2C[0x%02x][0x%02x]\n", index, addr);
1028         init->offset += 4;
1029
1030         for (i = 0; i < count; i++) {
1031                 data[i] = nv_ro08(bios, init->offset);
1032                 trace("\t0x%02x\n", data[i]);
1033                 init->offset++;
1034         }
1035
1036         if (init_exec(init)) {
1037                 struct nouveau_i2c_port *port = init_i2c(init, index);
1038                 struct i2c_msg msg = {
1039                         .addr = addr, .flags = 0, .len = count, .buf = data,
1040                 };
1041                 int ret;
1042
1043                 if (port && (ret = i2c_transfer(&port->adapter, &msg, 1)) != 1)
1044                         warn("i2c wr failed, %d\n", ret);
1045         }
1046 }
1047
1048 /**
1049  * INIT_TMDS - opcode 0x4f
1050  *
1051  */
1052 static void
1053 init_tmds(struct nvbios_init *init)
1054 {
1055         struct nouveau_bios *bios = init->bios;
1056         u8 tmds = nv_ro08(bios, init->offset + 1);
1057         u8 addr = nv_ro08(bios, init->offset + 2);
1058         u8 mask = nv_ro08(bios, init->offset + 3);
1059         u8 data = nv_ro08(bios, init->offset + 4);
1060         u32 reg = init_tmds_reg(init, tmds);
1061
1062         trace("TMDS\tT[0x%02x][0x%02x] &= 0x%02x |= 0x%02x\n",
1063               tmds, addr, mask, data);
1064         init->offset += 5;
1065
1066         if (reg == 0)
1067                 return;
1068
1069         init_wr32(init, reg + 0, addr | 0x00010000);
1070         init_wr32(init, reg + 4, data | (init_rd32(init, reg + 4) & mask));
1071         init_wr32(init, reg + 0, addr);
1072 }
1073
1074 /**
1075  * INIT_ZM_TMDS_GROUP - opcode 0x50
1076  *
1077  */
1078 static void
1079 init_zm_tmds_group(struct nvbios_init *init)
1080 {
1081         struct nouveau_bios *bios = init->bios;
1082         u8  tmds = nv_ro08(bios, init->offset + 1);
1083         u8 count = nv_ro08(bios, init->offset + 2);
1084         u32  reg = init_tmds_reg(init, tmds);
1085
1086         trace("TMDS_ZM_GROUP\tT[0x%02x]\n", tmds);
1087         init->offset += 3;
1088
1089         while (count--) {
1090                 u8 addr = nv_ro08(bios, init->offset + 0);
1091                 u8 data = nv_ro08(bios, init->offset + 1);
1092
1093                 trace("\t[0x%02x] = 0x%02x\n", addr, data);
1094                 init->offset += 2;
1095
1096                 init_wr32(init, reg + 4, data);
1097                 init_wr32(init, reg + 0, addr);
1098         }
1099 }
1100
1101 /**
1102  * INIT_CR_INDEX_ADDRESS_LATCHED - opcode 0x51
1103  *
1104  */
1105 static void
1106 init_cr_idx_adr_latch(struct nvbios_init *init)
1107 {
1108         struct nouveau_bios *bios = init->bios;
1109         u8 addr0 = nv_ro08(bios, init->offset + 1);
1110         u8 addr1 = nv_ro08(bios, init->offset + 2);
1111         u8  base = nv_ro08(bios, init->offset + 3);
1112         u8 count = nv_ro08(bios, init->offset + 4);
1113         u8 save0;
1114
1115         trace("CR_INDEX_ADDR C[%02x] C[%02x]\n", addr0, addr1);
1116         init->offset += 5;
1117
1118         save0 = init_rdvgai(init, 0x03d4, addr0);
1119         while (count--) {
1120                 u8 data = nv_ro08(bios, init->offset);
1121
1122                 trace("\t\t[0x%02x] = 0x%02x\n", base, data);
1123                 init->offset += 1;
1124
1125                 init_wrvgai(init, 0x03d4, addr0, base++);
1126                 init_wrvgai(init, 0x03d4, addr1, data);
1127         }
1128         init_wrvgai(init, 0x03d4, addr0, save0);
1129 }
1130
1131 /**
1132  * INIT_CR - opcode 0x52
1133  *
1134  */
1135 static void
1136 init_cr(struct nvbios_init *init)
1137 {
1138         struct nouveau_bios *bios = init->bios;
1139         u8 addr = nv_ro08(bios, init->offset + 1);
1140         u8 mask = nv_ro08(bios, init->offset + 2);
1141         u8 data = nv_ro08(bios, init->offset + 3);
1142         u8 val;
1143
1144         trace("CR\t\tC[0x%02x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
1145         init->offset += 4;
1146
1147         val = init_rdvgai(init, 0x03d4, addr) & mask;
1148         init_wrvgai(init, 0x03d4, addr, val | data);
1149 }
1150
1151 /**
1152  * INIT_ZM_CR - opcode 0x53
1153  *
1154  */
1155 static void
1156 init_zm_cr(struct nvbios_init *init)
1157 {
1158         struct nouveau_bios *bios = init->bios;
1159         u8 addr = nv_ro08(bios, init->offset + 1);
1160         u8 data = nv_ro08(bios, init->offset + 2);
1161
1162         trace("ZM_CR\tC[0x%02x] = 0x%02x\n", addr,  data);
1163         init->offset += 3;
1164
1165         init_wrvgai(init, 0x03d4, addr, data);
1166 }
1167
1168 /**
1169  * INIT_ZM_CR_GROUP - opcode 0x54
1170  *
1171  */
1172 static void
1173 init_zm_cr_group(struct nvbios_init *init)
1174 {
1175         struct nouveau_bios *bios = init->bios;
1176         u8 count = nv_ro08(bios, init->offset + 1);
1177
1178         trace("ZM_CR_GROUP\n");
1179         init->offset += 2;
1180
1181         while (count--) {
1182                 u8 addr = nv_ro08(bios, init->offset + 0);
1183                 u8 data = nv_ro08(bios, init->offset + 1);
1184
1185                 trace("\t\tC[0x%02x] = 0x%02x\n", addr, data);
1186                 init->offset += 2;
1187
1188                 init_wrvgai(init, 0x03d4, addr, data);
1189         }
1190 }
1191
1192 /**
1193  * INIT_CONDITION_TIME - opcode 0x56
1194  *
1195  */
1196 static void
1197 init_condition_time(struct nvbios_init *init)
1198 {
1199         struct nouveau_bios *bios = init->bios;
1200         u8  cond = nv_ro08(bios, init->offset + 1);
1201         u8 retry = nv_ro08(bios, init->offset + 2);
1202         u8  wait = min((u16)retry * 50, 100);
1203
1204         trace("CONDITION_TIME\t0x%02x 0x%02x\n", cond, retry);
1205         init->offset += 3;
1206
1207         if (!init_exec(init))
1208                 return;
1209
1210         while (wait--) {
1211                 if (init_condition_met(init, cond))
1212                         return;
1213                 mdelay(20);
1214         }
1215
1216         init_exec_set(init, false);
1217 }
1218
1219 /**
1220  * INIT_LTIME - opcode 0x57
1221  *
1222  */
1223 static void
1224 init_ltime(struct nvbios_init *init)
1225 {
1226         struct nouveau_bios *bios = init->bios;
1227         u16 msec = nv_ro16(bios, init->offset + 1);
1228
1229         trace("LTIME\t0x%04x\n", msec);
1230         init->offset += 3;
1231
1232         if (init_exec(init))
1233                 mdelay(msec);
1234 }
1235
1236 /**
1237  * INIT_ZM_REG_SEQUENCE - opcode 0x58
1238  *
1239  */
1240 static void
1241 init_zm_reg_sequence(struct nvbios_init *init)
1242 {
1243         struct nouveau_bios *bios = init->bios;
1244         u32 base = nv_ro32(bios, init->offset + 1);
1245         u8 count = nv_ro08(bios, init->offset + 5);
1246
1247         trace("ZM_REG_SEQUENCE\t0x%02x\n", count);
1248         init->offset += 6;
1249
1250         while (count--) {
1251                 u32 data = nv_ro32(bios, init->offset);
1252
1253                 trace("\t\tR[0x%06x] = 0x%08x\n", base, data);
1254                 init->offset += 4;
1255
1256                 init_wr32(init, base, data);
1257                 base += 4;
1258         }
1259 }
1260
1261 /**
1262  * INIT_SUB_DIRECT - opcode 0x5b
1263  *
1264  */
1265 static void
1266 init_sub_direct(struct nvbios_init *init)
1267 {
1268         struct nouveau_bios *bios = init->bios;
1269         u16 addr = nv_ro16(bios, init->offset + 1);
1270         u16 save;
1271
1272         trace("SUB_DIRECT\t0x%04x\n", addr);
1273
1274         if (init_exec(init)) {
1275                 save = init->offset;
1276                 init->offset = addr;
1277                 if (nvbios_exec(init)) {
1278                         error("error parsing sub-table\n");
1279                         return;
1280                 }
1281                 init->offset = save;
1282         }
1283
1284         init->offset += 3;
1285 }
1286
1287 /**
1288  * INIT_JUMP - opcode 0x5c
1289  *
1290  */
1291 static void
1292 init_jump(struct nvbios_init *init)
1293 {
1294         struct nouveau_bios *bios = init->bios;
1295         u16 offset = nv_ro16(bios, init->offset + 1);
1296
1297         trace("JUMP\t0x%04x\n", offset);
1298
1299         if (init_exec(init))
1300                 init->offset = offset;
1301         else
1302                 init->offset += 3;
1303 }
1304
1305 /**
1306  * INIT_I2C_IF - opcode 0x5e
1307  *
1308  */
1309 static void
1310 init_i2c_if(struct nvbios_init *init)
1311 {
1312         struct nouveau_bios *bios = init->bios;
1313         u8 index = nv_ro08(bios, init->offset + 1);
1314         u8  addr = nv_ro08(bios, init->offset + 2);
1315         u8   reg = nv_ro08(bios, init->offset + 3);
1316         u8  mask = nv_ro08(bios, init->offset + 4);
1317         u8  data = nv_ro08(bios, init->offset + 5);
1318         u8 value;
1319
1320         trace("I2C_IF\tI2C[0x%02x][0x%02x][0x%02x] & 0x%02x == 0x%02x\n",
1321               index, addr, reg, mask, data);
1322         init->offset += 6;
1323         init_exec_force(init, true);
1324
1325         value = init_rdi2cr(init, index, addr, reg);
1326         if ((value & mask) != data)
1327                 init_exec_set(init, false);
1328
1329         init_exec_force(init, false);
1330 }
1331
1332 /**
1333  * INIT_COPY_NV_REG - opcode 0x5f
1334  *
1335  */
1336 static void
1337 init_copy_nv_reg(struct nvbios_init *init)
1338 {
1339         struct nouveau_bios *bios = init->bios;
1340         u32  sreg = nv_ro32(bios, init->offset + 1);
1341         u8  shift = nv_ro08(bios, init->offset + 5);
1342         u32 smask = nv_ro32(bios, init->offset + 6);
1343         u32  sxor = nv_ro32(bios, init->offset + 10);
1344         u32  dreg = nv_ro32(bios, init->offset + 14);
1345         u32 dmask = nv_ro32(bios, init->offset + 18);
1346         u32 data;
1347
1348         trace("COPY_NV_REG\tR[0x%06x] &= 0x%08x |= "
1349               "((R[0x%06x] %s 0x%02x) & 0x%08x ^ 0x%08x)\n",
1350               dreg, dmask, sreg, (shift & 0x80) ? "<<" : ">>",
1351               (shift & 0x80) ? (0x100 - shift) : shift, smask, sxor);
1352         init->offset += 22;
1353
1354         data = init_shift(init_rd32(init, sreg), shift);
1355         init_mask(init, dreg, ~dmask, (data & smask) ^ sxor);
1356 }
1357
1358 /**
1359  * INIT_ZM_INDEX_IO - opcode 0x62
1360  *
1361  */
1362 static void
1363 init_zm_index_io(struct nvbios_init *init)
1364 {
1365         struct nouveau_bios *bios = init->bios;
1366         u16 port = nv_ro16(bios, init->offset + 1);
1367         u8 index = nv_ro08(bios, init->offset + 3);
1368         u8  data = nv_ro08(bios, init->offset + 4);
1369
1370         trace("ZM_INDEX_IO\tI[0x%04x][0x%02x] = 0x%02x\n", port, index, data);
1371         init->offset += 5;
1372
1373         init_wrvgai(init, port, index, data);
1374 }
1375
1376 /**
1377  * INIT_COMPUTE_MEM - opcode 0x63
1378  *
1379  */
1380 static void
1381 init_compute_mem(struct nvbios_init *init)
1382 {
1383         struct nouveau_devinit *devinit = nouveau_devinit(init->bios);
1384
1385         trace("COMPUTE_MEM\n");
1386         init->offset += 1;
1387
1388         init_exec_force(init, true);
1389         if (init_exec(init) && devinit->meminit)
1390                 devinit->meminit(devinit);
1391         init_exec_force(init, false);
1392 }
1393
1394 /**
1395  * INIT_RESET - opcode 0x65
1396  *
1397  */
1398 static void
1399 init_reset(struct nvbios_init *init)
1400 {
1401         struct nouveau_bios *bios = init->bios;
1402         u32   reg = nv_ro32(bios, init->offset + 1);
1403         u32 data1 = nv_ro32(bios, init->offset + 5);
1404         u32 data2 = nv_ro32(bios, init->offset + 9);
1405         u32 savepci19;
1406
1407         trace("RESET\tR[0x%08x] = 0x%08x, 0x%08x", reg, data1, data2);
1408         init->offset += 13;
1409         init_exec_force(init, true);
1410
1411         savepci19 = init_mask(init, 0x00184c, 0x00000f00, 0x00000000);
1412         init_wr32(init, reg, data1);
1413         udelay(10);
1414         init_wr32(init, reg, data2);
1415         init_wr32(init, 0x00184c, savepci19);
1416         init_mask(init, 0x001850, 0x00000001, 0x00000000);
1417
1418         init_exec_force(init, false);
1419 }
1420
1421 /**
1422  * INIT_CONFIGURE_MEM - opcode 0x66
1423  *
1424  */
1425 static u16
1426 init_configure_mem_clk(struct nvbios_init *init)
1427 {
1428         u16 mdata = bmp_mem_init_table(init->bios);
1429         if (mdata)
1430                 mdata += (init_rdvgai(init, 0x03d4, 0x3c) >> 4) * 66;
1431         return mdata;
1432 }
1433
1434 static void
1435 init_configure_mem(struct nvbios_init *init)
1436 {
1437         struct nouveau_bios *bios = init->bios;
1438         u16 mdata, sdata;
1439         u32 addr, data;
1440
1441         trace("CONFIGURE_MEM\n");
1442         init->offset += 1;
1443
1444         if (bios->version.major > 2) {
1445                 init_done(init);
1446                 return;
1447         }
1448         init_exec_force(init, true);
1449
1450         mdata = init_configure_mem_clk(init);
1451         sdata = bmp_sdr_seq_table(bios);
1452         if (nv_ro08(bios, mdata) & 0x01)
1453                 sdata = bmp_ddr_seq_table(bios);
1454         mdata += 6; /* skip to data */
1455
1456         data = init_rdvgai(init, 0x03c4, 0x01);
1457         init_wrvgai(init, 0x03c4, 0x01, data | 0x20);
1458
1459         while ((addr = nv_ro32(bios, sdata)) != 0xffffffff) {
1460                 switch (addr) {
1461                 case 0x10021c: /* CKE_NORMAL */
1462                 case 0x1002d0: /* CMD_REFRESH */
1463                 case 0x1002d4: /* CMD_PRECHARGE */
1464                         data = 0x00000001;
1465                         break;
1466                 default:
1467                         data = nv_ro32(bios, mdata);
1468                         mdata += 4;
1469                         if (data == 0xffffffff)
1470                                 continue;
1471                         break;
1472                 }
1473
1474                 init_wr32(init, addr, data);
1475         }
1476
1477         init_exec_force(init, false);
1478 }
1479
1480 /**
1481  * INIT_CONFIGURE_CLK - opcode 0x67
1482  *
1483  */
1484 static void
1485 init_configure_clk(struct nvbios_init *init)
1486 {
1487         struct nouveau_bios *bios = init->bios;
1488         u16 mdata, clock;
1489
1490         trace("CONFIGURE_CLK\n");
1491         init->offset += 1;
1492
1493         if (bios->version.major > 2) {
1494                 init_done(init);
1495                 return;
1496         }
1497         init_exec_force(init, true);
1498
1499         mdata = init_configure_mem_clk(init);
1500
1501         /* NVPLL */
1502         clock = nv_ro16(bios, mdata + 4) * 10;
1503         init_prog_pll(init, 0x680500, clock);
1504
1505         /* MPLL */
1506         clock = nv_ro16(bios, mdata + 2) * 10;
1507         if (nv_ro08(bios, mdata) & 0x01)
1508                 clock *= 2;
1509         init_prog_pll(init, 0x680504, clock);
1510
1511         init_exec_force(init, false);
1512 }
1513
1514 /**
1515  * INIT_CONFIGURE_PREINIT - opcode 0x68
1516  *
1517  */
1518 static void
1519 init_configure_preinit(struct nvbios_init *init)
1520 {
1521         struct nouveau_bios *bios = init->bios;
1522         u32 strap;
1523
1524         trace("CONFIGURE_PREINIT\n");
1525         init->offset += 1;
1526
1527         if (bios->version.major > 2) {
1528                 init_done(init);
1529                 return;
1530         }
1531         init_exec_force(init, true);
1532
1533         strap = init_rd32(init, 0x101000);
1534         strap = ((strap << 2) & 0xf0) | ((strap & 0x40) >> 6);
1535         init_wrvgai(init, 0x03d4, 0x3c, strap);
1536
1537         init_exec_force(init, false);
1538 }
1539
1540 /**
1541  * INIT_IO - opcode 0x69
1542  *
1543  */
1544 static void
1545 init_io(struct nvbios_init *init)
1546 {
1547         struct nouveau_bios *bios = init->bios;
1548         u16 port = nv_ro16(bios, init->offset + 1);
1549         u8  mask = nv_ro16(bios, init->offset + 3);
1550         u8  data = nv_ro16(bios, init->offset + 4);
1551         u8 value;
1552
1553         trace("IO\t\tI[0x%04x] &= 0x%02x |= 0x%02x\n", port, mask, data);
1554         init->offset += 5;
1555
1556         /* ummm.. yes.. should really figure out wtf this is and why it's
1557          * needed some day..  it's almost certainly wrong, but, it also
1558          * somehow makes things work...
1559          */
1560         if (nv_device(init->bios)->card_type >= NV_50 &&
1561             port == 0x03c3 && data == 0x01) {
1562                 init_mask(init, 0x614100, 0xf0800000, 0x00800000);
1563                 init_mask(init, 0x00e18c, 0x00020000, 0x00020000);
1564                 init_mask(init, 0x614900, 0xf0800000, 0x00800000);
1565                 init_mask(init, 0x000200, 0x40000000, 0x00000000);
1566                 mdelay(10);
1567                 init_mask(init, 0x00e18c, 0x00020000, 0x00000000);
1568                 init_mask(init, 0x000200, 0x40000000, 0x40000000);
1569                 init_wr32(init, 0x614100, 0x00800018);
1570                 init_wr32(init, 0x614900, 0x00800018);
1571                 mdelay(10);
1572                 init_wr32(init, 0x614100, 0x10000018);
1573                 init_wr32(init, 0x614900, 0x10000018);
1574         }
1575
1576         value = init_rdport(init, port) & mask;
1577         init_wrport(init, port, data | value);
1578 }
1579
1580 /**
1581  * INIT_SUB - opcode 0x6b
1582  *
1583  */
1584 static void
1585 init_sub(struct nvbios_init *init)
1586 {
1587         struct nouveau_bios *bios = init->bios;
1588         u8 index = nv_ro08(bios, init->offset + 1);
1589         u16 addr, save;
1590
1591         trace("SUB\t0x%02x\n", index);
1592
1593         addr = init_script(bios, index);
1594         if (addr && init_exec(init)) {
1595                 save = init->offset;
1596                 init->offset = addr;
1597                 if (nvbios_exec(init)) {
1598                         error("error parsing sub-table\n");
1599                         return;
1600                 }
1601                 init->offset = save;
1602         }
1603
1604         init->offset += 2;
1605 }
1606
1607 /**
1608  * INIT_RAM_CONDITION - opcode 0x6d
1609  *
1610  */
1611 static void
1612 init_ram_condition(struct nvbios_init *init)
1613 {
1614         struct nouveau_bios *bios = init->bios;
1615         u8  mask = nv_ro08(bios, init->offset + 1);
1616         u8 value = nv_ro08(bios, init->offset + 2);
1617
1618         trace("RAM_CONDITION\t"
1619               "(R[0x100000] & 0x%02x) == 0x%02x\n", mask, value);
1620         init->offset += 3;
1621
1622         if ((init_rd32(init, 0x100000) & mask) != value)
1623                 init_exec_set(init, false);
1624 }
1625
1626 /**
1627  * INIT_NV_REG - opcode 0x6e
1628  *
1629  */
1630 static void
1631 init_nv_reg(struct nvbios_init *init)
1632 {
1633         struct nouveau_bios *bios = init->bios;
1634         u32  reg = nv_ro32(bios, init->offset + 1);
1635         u32 mask = nv_ro32(bios, init->offset + 5);
1636         u32 data = nv_ro32(bios, init->offset + 9);
1637
1638         trace("NV_REG\tR[0x%06x] &= 0x%08x |= 0x%08x\n", reg, mask, data);
1639         init->offset += 13;
1640
1641         init_mask(init, reg, ~mask, data);
1642 }
1643
1644 /**
1645  * INIT_MACRO - opcode 0x6f
1646  *
1647  */
1648 static void
1649 init_macro(struct nvbios_init *init)
1650 {
1651         struct nouveau_bios *bios = init->bios;
1652         u8  macro = nv_ro08(bios, init->offset + 1);
1653         u16 table;
1654
1655         trace("MACRO\t0x%02x\n", macro);
1656
1657         table = init_macro_table(init);
1658         if (table) {
1659                 u32 addr = nv_ro32(bios, table + (macro * 8) + 0);
1660                 u32 data = nv_ro32(bios, table + (macro * 8) + 4);
1661                 trace("\t\tR[0x%06x] = 0x%08x\n", addr, data);
1662                 init_wr32(init, addr, data);
1663         }
1664
1665         init->offset += 2;
1666 }
1667
1668 /**
1669  * INIT_RESUME - opcode 0x72
1670  *
1671  */
1672 static void
1673 init_resume(struct nvbios_init *init)
1674 {
1675         trace("RESUME\n");
1676         init->offset += 1;
1677         init_exec_set(init, true);
1678 }
1679
1680 /**
1681  * INIT_TIME - opcode 0x74
1682  *
1683  */
1684 static void
1685 init_time(struct nvbios_init *init)
1686 {
1687         struct nouveau_bios *bios = init->bios;
1688         u16 usec = nv_ro16(bios, init->offset + 1);
1689
1690         trace("TIME\t0x%04x\n", usec);
1691         init->offset += 3;
1692
1693         if (init_exec(init)) {
1694                 if (usec < 1000)
1695                         udelay(usec);
1696                 else
1697                         mdelay((usec + 900) / 1000);
1698         }
1699 }
1700
1701 /**
1702  * INIT_CONDITION - opcode 0x75
1703  *
1704  */
1705 static void
1706 init_condition(struct nvbios_init *init)
1707 {
1708         struct nouveau_bios *bios = init->bios;
1709         u8 cond = nv_ro08(bios, init->offset + 1);
1710
1711         trace("CONDITION\t0x%02x\n", cond);
1712         init->offset += 2;
1713
1714         if (!init_condition_met(init, cond))
1715                 init_exec_set(init, false);
1716 }
1717
1718 /**
1719  * INIT_IO_CONDITION - opcode 0x76
1720  *
1721  */
1722 static void
1723 init_io_condition(struct nvbios_init *init)
1724 {
1725         struct nouveau_bios *bios = init->bios;
1726         u8 cond = nv_ro08(bios, init->offset + 1);
1727
1728         trace("IO_CONDITION\t0x%02x\n", cond);
1729         init->offset += 2;
1730
1731         if (!init_io_condition_met(init, cond))
1732                 init_exec_set(init, false);
1733 }
1734
1735 /**
1736  * INIT_INDEX_IO - opcode 0x78
1737  *
1738  */
1739 static void
1740 init_index_io(struct nvbios_init *init)
1741 {
1742         struct nouveau_bios *bios = init->bios;
1743         u16 port = nv_ro16(bios, init->offset + 1);
1744         u8 index = nv_ro16(bios, init->offset + 3);
1745         u8  mask = nv_ro08(bios, init->offset + 4);
1746         u8  data = nv_ro08(bios, init->offset + 5);
1747         u8 value;
1748
1749         trace("INDEX_IO\tI[0x%04x][0x%02x] &= 0x%02x |= 0x%02x\n",
1750               port, index, mask, data);
1751         init->offset += 6;
1752
1753         value = init_rdvgai(init, port, index) & mask;
1754         init_wrvgai(init, port, index, data | value);
1755 }
1756
1757 /**
1758  * INIT_PLL - opcode 0x79
1759  *
1760  */
1761 static void
1762 init_pll(struct nvbios_init *init)
1763 {
1764         struct nouveau_bios *bios = init->bios;
1765         u32  reg = nv_ro32(bios, init->offset + 1);
1766         u32 freq = nv_ro16(bios, init->offset + 5) * 10;
1767
1768         trace("PLL\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
1769         init->offset += 7;
1770
1771         init_prog_pll(init, reg, freq);
1772 }
1773
1774 /**
1775  * INIT_ZM_REG - opcode 0x7a
1776  *
1777  */
1778 static void
1779 init_zm_reg(struct nvbios_init *init)
1780 {
1781         struct nouveau_bios *bios = init->bios;
1782         u32 addr = nv_ro32(bios, init->offset + 1);
1783         u32 data = nv_ro32(bios, init->offset + 5);
1784
1785         trace("ZM_REG\tR[0x%06x] = 0x%08x\n", addr, data);
1786         init->offset += 9;
1787
1788         if (addr == 0x000200)
1789                 data |= 0x00000001;
1790
1791         init_wr32(init, addr, data);
1792 }
1793
1794 /**
1795  * INIT_RAM_RESTRICT_PLL - opcde 0x87
1796  *
1797  */
1798 static void
1799 init_ram_restrict_pll(struct nvbios_init *init)
1800 {
1801         struct nouveau_bios *bios = init->bios;
1802         u8  type = nv_ro08(bios, init->offset + 1);
1803         u8 count = init_ram_restrict_group_count(init);
1804         u8 strap = init_ram_restrict(init);
1805         u8 cconf;
1806
1807         trace("RAM_RESTRICT_PLL\t0x%02x\n", type);
1808         init->offset += 2;
1809
1810         for (cconf = 0; cconf < count; cconf++) {
1811                 u32 freq = nv_ro32(bios, init->offset);
1812
1813                 if (cconf == strap) {
1814                         trace("%dkHz *\n", freq);
1815                         init_prog_pll(init, type, freq);
1816                 } else {
1817                         trace("%dkHz\n", freq);
1818                 }
1819
1820                 init->offset += 4;
1821         }
1822 }
1823
1824 /**
1825  * INIT_GPIO - opcode 0x8e
1826  *
1827  */
1828 static void
1829 init_gpio(struct nvbios_init *init)
1830 {
1831         struct nouveau_gpio *gpio = nouveau_gpio(init->bios);
1832
1833         trace("GPIO\n");
1834         init->offset += 1;
1835
1836         if (init_exec(init) && gpio && gpio->reset)
1837                 gpio->reset(gpio, DCB_GPIO_UNUSED);
1838 }
1839
1840 /**
1841  * INIT_RAM_RESTRICT_ZM_GROUP - opcode 0x8f
1842  *
1843  */
1844 static void
1845 init_ram_restrict_zm_reg_group(struct nvbios_init *init)
1846 {
1847         struct nouveau_bios *bios = init->bios;
1848         u32 addr = nv_ro32(bios, init->offset + 1);
1849         u8  incr = nv_ro08(bios, init->offset + 5);
1850         u8   num = nv_ro08(bios, init->offset + 6);
1851         u8 count = init_ram_restrict_group_count(init);
1852         u8 index = init_ram_restrict(init);
1853         u8 i, j;
1854
1855         trace("RAM_RESTRICT_ZM_REG_GROUP\t"
1856               "R[0x%08x] 0x%02x 0x%02x\n", addr, incr, num);
1857         init->offset += 7;
1858
1859         for (i = 0; i < num; i++) {
1860                 trace("\tR[0x%06x] = {\n", addr);
1861                 for (j = 0; j < count; j++) {
1862                         u32 data = nv_ro32(bios, init->offset);
1863
1864                         if (j == index) {
1865                                 trace("\t\t0x%08x *\n", data);
1866                                 init_wr32(init, addr, data);
1867                         } else {
1868                                 trace("\t\t0x%08x\n", data);
1869                         }
1870
1871                         init->offset += 4;
1872                 }
1873                 trace("\t}\n");
1874                 addr += incr;
1875         }
1876 }
1877
1878 /**
1879  * INIT_COPY_ZM_REG - opcode 0x90
1880  *
1881  */
1882 static void
1883 init_copy_zm_reg(struct nvbios_init *init)
1884 {
1885         struct nouveau_bios *bios = init->bios;
1886         u32 sreg = nv_ro32(bios, init->offset + 1);
1887         u32 dreg = nv_ro32(bios, init->offset + 5);
1888
1889         trace("COPY_ZM_REG\tR[0x%06x] = R[0x%06x]\n", dreg, sreg);
1890         init->offset += 9;
1891
1892         init_wr32(init, dreg, init_rd32(init, sreg));
1893 }
1894
1895 /**
1896  * INIT_ZM_REG_GROUP - opcode 0x91
1897  *
1898  */
1899 static void
1900 init_zm_reg_group(struct nvbios_init *init)
1901 {
1902         struct nouveau_bios *bios = init->bios;
1903         u32 addr = nv_ro32(bios, init->offset + 1);
1904         u8 count = nv_ro08(bios, init->offset + 5);
1905
1906         trace("ZM_REG_GROUP\tR[0x%06x] =\n", addr);
1907         init->offset += 6;
1908
1909         while (count--) {
1910                 u32 data = nv_ro32(bios, init->offset);
1911                 trace("\t0x%08x\n", data);
1912                 init_wr32(init, addr, data);
1913                 init->offset += 4;
1914         }
1915 }
1916
1917 /**
1918  * INIT_XLAT - opcode 0x96
1919  *
1920  */
1921 static void
1922 init_xlat(struct nvbios_init *init)
1923 {
1924         struct nouveau_bios *bios = init->bios;
1925         u32 saddr = nv_ro32(bios, init->offset + 1);
1926         u8 sshift = nv_ro08(bios, init->offset + 5);
1927         u8  smask = nv_ro08(bios, init->offset + 6);
1928         u8  index = nv_ro08(bios, init->offset + 7);
1929         u32 daddr = nv_ro32(bios, init->offset + 8);
1930         u32 dmask = nv_ro32(bios, init->offset + 12);
1931         u8  shift = nv_ro08(bios, init->offset + 16);
1932         u32 data;
1933
1934         trace("INIT_XLAT\tR[0x%06x] &= 0x%08x |= "
1935               "(X%02x((R[0x%06x] %s 0x%02x) & 0x%02x) << 0x%02x)\n",
1936               daddr, dmask, index, saddr, (sshift & 0x80) ? "<<" : ">>",
1937               (sshift & 0x80) ? (0x100 - sshift) : sshift, smask, shift);
1938         init->offset += 17;
1939
1940         data = init_shift(init_rd32(init, saddr), sshift) & smask;
1941         data = init_xlat_(init, index, data) << shift;
1942         init_mask(init, daddr, ~dmask, data);
1943 }
1944
1945 /**
1946  * INIT_ZM_MASK_ADD - opcode 0x97
1947  *
1948  */
1949 static void
1950 init_zm_mask_add(struct nvbios_init *init)
1951 {
1952         struct nouveau_bios *bios = init->bios;
1953         u32 addr = nv_ro32(bios, init->offset + 1);
1954         u32 mask = nv_ro32(bios, init->offset + 5);
1955         u32  add = nv_ro32(bios, init->offset + 9);
1956         u32 data;
1957
1958         trace("ZM_MASK_ADD\tR[0x%06x] &= 0x%08x += 0x%08x\n", addr, mask, add);
1959         init->offset += 13;
1960
1961         data =  init_rd32(init, addr);
1962         data = (data & mask) | ((data + add) & ~mask);
1963         init_wr32(init, addr, data);
1964 }
1965
1966 /**
1967  * INIT_AUXCH - opcode 0x98
1968  *
1969  */
1970 static void
1971 init_auxch(struct nvbios_init *init)
1972 {
1973         struct nouveau_bios *bios = init->bios;
1974         u32 addr = nv_ro32(bios, init->offset + 1);
1975         u8 count = nv_ro08(bios, init->offset + 5);
1976
1977         trace("AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
1978         init->offset += 6;
1979
1980         while (count--) {
1981                 u8 mask = nv_ro08(bios, init->offset + 0);
1982                 u8 data = nv_ro08(bios, init->offset + 1);
1983                 trace("\tAUX[0x%08x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
1984                 mask = init_rdauxr(init, addr) & mask;
1985                 init_wrauxr(init, addr, mask | data);
1986                 init->offset += 2;
1987         }
1988 }
1989
1990 /**
1991  * INIT_AUXCH - opcode 0x99
1992  *
1993  */
1994 static void
1995 init_zm_auxch(struct nvbios_init *init)
1996 {
1997         struct nouveau_bios *bios = init->bios;
1998         u32 addr = nv_ro32(bios, init->offset + 1);
1999         u8 count = nv_ro08(bios, init->offset + 5);
2000
2001         trace("ZM_AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
2002         init->offset += 6;
2003
2004         while (count--) {
2005                 u8 data = nv_ro08(bios, init->offset + 0);
2006                 trace("\tAUX[0x%08x] = 0x%02x\n", addr, data);
2007                 init_wrauxr(init, addr, data);
2008                 init->offset += 1;
2009         }
2010 }
2011
2012 /**
2013  * INIT_I2C_LONG_IF - opcode 0x9a
2014  *
2015  */
2016 static void
2017 init_i2c_long_if(struct nvbios_init *init)
2018 {
2019         struct nouveau_bios *bios = init->bios;
2020         u8 index = nv_ro08(bios, init->offset + 1);
2021         u8  addr = nv_ro08(bios, init->offset + 2) >> 1;
2022         u8 reglo = nv_ro08(bios, init->offset + 3);
2023         u8 reghi = nv_ro08(bios, init->offset + 4);
2024         u8  mask = nv_ro08(bios, init->offset + 5);
2025         u8  data = nv_ro08(bios, init->offset + 6);
2026         struct nouveau_i2c_port *port;
2027
2028         trace("I2C_LONG_IF\t"
2029               "I2C[0x%02x][0x%02x][0x%02x%02x] & 0x%02x == 0x%02x\n",
2030               index, addr, reglo, reghi, mask, data);
2031         init->offset += 7;
2032
2033         port = init_i2c(init, index);
2034         if (port) {
2035                 u8 i[2] = { reghi, reglo };
2036                 u8 o[1] = {};
2037                 struct i2c_msg msg[] = {
2038                         { .addr = addr, .flags = 0, .len = 2, .buf = i },
2039                         { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = o }
2040                 };
2041                 int ret;
2042
2043                 ret = i2c_transfer(&port->adapter, msg, 2);
2044                 if (ret == 2 && ((o[0] & mask) == data))
2045                         return;
2046         }
2047
2048         init_exec_set(init, false);
2049 }
2050
2051 /**
2052  * INIT_GPIO_NE - opcode 0xa9
2053  *
2054  */
2055 static void
2056 init_gpio_ne(struct nvbios_init *init)
2057 {
2058         struct nouveau_bios *bios = init->bios;
2059         struct nouveau_gpio *gpio = nouveau_gpio(bios);
2060         struct dcb_gpio_func func;
2061         u8 count = nv_ro08(bios, init->offset + 1);
2062         u8 idx = 0, ver, len;
2063         u16 data, i;
2064
2065         trace("GPIO_NE\t");
2066         init->offset += 2;
2067
2068         for (i = init->offset; i < init->offset + count; i++)
2069                 cont("0x%02x ", nv_ro08(bios, i));
2070         cont("\n");
2071
2072         while ((data = dcb_gpio_parse(bios, 0, idx++, &ver, &len, &func))) {
2073                 if (func.func != DCB_GPIO_UNUSED) {
2074                         for (i = init->offset; i < init->offset + count; i++) {
2075                                 if (func.func == nv_ro08(bios, i))
2076                                         break;
2077                         }
2078
2079                         trace("\tFUNC[0x%02x]", func.func);
2080                         if (i == (init->offset + count)) {
2081                                 cont(" *");
2082                                 if (init_exec(init) && gpio && gpio->reset)
2083                                         gpio->reset(gpio, func.func);
2084                         }
2085                         cont("\n");
2086                 }
2087         }
2088
2089         init->offset += count;
2090 }
2091
2092 static struct nvbios_init_opcode {
2093         void (*exec)(struct nvbios_init *);
2094 } init_opcode[] = {
2095         [0x32] = { init_io_restrict_prog },
2096         [0x33] = { init_repeat },
2097         [0x34] = { init_io_restrict_pll },
2098         [0x36] = { init_end_repeat },
2099         [0x37] = { init_copy },
2100         [0x38] = { init_not },
2101         [0x39] = { init_io_flag_condition },
2102         [0x3a] = { init_dp_condition },
2103         [0x3b] = { init_io_mask_or },
2104         [0x3c] = { init_io_or },
2105         [0x49] = { init_idx_addr_latched },
2106         [0x4a] = { init_io_restrict_pll2 },
2107         [0x4b] = { init_pll2 },
2108         [0x4c] = { init_i2c_byte },
2109         [0x4d] = { init_zm_i2c_byte },
2110         [0x4e] = { init_zm_i2c },
2111         [0x4f] = { init_tmds },
2112         [0x50] = { init_zm_tmds_group },
2113         [0x51] = { init_cr_idx_adr_latch },
2114         [0x52] = { init_cr },
2115         [0x53] = { init_zm_cr },
2116         [0x54] = { init_zm_cr_group },
2117         [0x56] = { init_condition_time },
2118         [0x57] = { init_ltime },
2119         [0x58] = { init_zm_reg_sequence },
2120         [0x5b] = { init_sub_direct },
2121         [0x5c] = { init_jump },
2122         [0x5e] = { init_i2c_if },
2123         [0x5f] = { init_copy_nv_reg },
2124         [0x62] = { init_zm_index_io },
2125         [0x63] = { init_compute_mem },
2126         [0x65] = { init_reset },
2127         [0x66] = { init_configure_mem },
2128         [0x67] = { init_configure_clk },
2129         [0x68] = { init_configure_preinit },
2130         [0x69] = { init_io },
2131         [0x6b] = { init_sub },
2132         [0x6d] = { init_ram_condition },
2133         [0x6e] = { init_nv_reg },
2134         [0x6f] = { init_macro },
2135         [0x71] = { init_done },
2136         [0x72] = { init_resume },
2137         [0x74] = { init_time },
2138         [0x75] = { init_condition },
2139         [0x76] = { init_io_condition },
2140         [0x78] = { init_index_io },
2141         [0x79] = { init_pll },
2142         [0x7a] = { init_zm_reg },
2143         [0x87] = { init_ram_restrict_pll },
2144         [0x8c] = { init_reserved },
2145         [0x8d] = { init_reserved },
2146         [0x8e] = { init_gpio },
2147         [0x8f] = { init_ram_restrict_zm_reg_group },
2148         [0x90] = { init_copy_zm_reg },
2149         [0x91] = { init_zm_reg_group },
2150         [0x92] = { init_reserved },
2151         [0x96] = { init_xlat },
2152         [0x97] = { init_zm_mask_add },
2153         [0x98] = { init_auxch },
2154         [0x99] = { init_zm_auxch },
2155         [0x9a] = { init_i2c_long_if },
2156         [0xa9] = { init_gpio_ne },
2157         [0xaa] = { init_reserved },
2158 };
2159
2160 #define init_opcode_nr (sizeof(init_opcode) / sizeof(init_opcode[0]))
2161
2162 int
2163 nvbios_exec(struct nvbios_init *init)
2164 {
2165         init->nested++;
2166         while (init->offset) {
2167                 u8 opcode = nv_ro08(init->bios, init->offset);
2168                 if (opcode >= init_opcode_nr || !init_opcode[opcode].exec) {
2169                         error("unknown opcode 0x%02x\n", opcode);
2170                         return -EINVAL;
2171                 }
2172
2173                 init_opcode[opcode].exec(init);
2174         }
2175         init->nested--;
2176         return 0;
2177 }
2178
2179 int
2180 nvbios_init(struct nouveau_subdev *subdev, bool execute)
2181 {
2182         struct nouveau_bios *bios = nouveau_bios(subdev);
2183         int ret = 0;
2184         int i = -1;
2185         u16 data;
2186
2187         if (execute)
2188                 nv_info(bios, "running init tables\n");
2189         while (!ret && (data = (init_script(bios, ++i)))) {
2190                 struct nvbios_init init = {
2191                         .subdev = subdev,
2192                         .bios = bios,
2193                         .offset = data,
2194                         .outp = NULL,
2195                         .crtc = -1,
2196                         .execute = execute ? 1 : 0,
2197                 };
2198
2199                 ret = nvbios_exec(&init);
2200         }
2201
2202         /* the vbios parser will run this right after the normal init
2203          * tables, whereas the binary driver appears to run it later.
2204          */
2205         if (!ret && (data = init_unknown_script(bios))) {
2206                 struct nvbios_init init = {
2207                         .subdev = subdev,
2208                         .bios = bios,
2209                         .offset = data,
2210                         .outp = NULL,
2211                         .crtc = -1,
2212                         .execute = execute ? 1 : 0,
2213                 };
2214
2215                 ret = nvbios_exec(&init);
2216         }
2217
2218         return 0;
2219 }