Merge remote-tracking branch 'lsk/v3.10/topic/configs' 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 data;
370
371         if (bmp_version(bios) && bmp_version(bios) < 0x0510) {
372                 if (index > 1)
373                         return 0x0000;
374
375                 data = bios->bmp_offset + (bios->version.major < 2 ? 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         init->offset = offset;
1299 }
1300
1301 /**
1302  * INIT_I2C_IF - opcode 0x5e
1303  *
1304  */
1305 static void
1306 init_i2c_if(struct nvbios_init *init)
1307 {
1308         struct nouveau_bios *bios = init->bios;
1309         u8 index = nv_ro08(bios, init->offset + 1);
1310         u8  addr = nv_ro08(bios, init->offset + 2);
1311         u8   reg = nv_ro08(bios, init->offset + 3);
1312         u8  mask = nv_ro08(bios, init->offset + 4);
1313         u8  data = nv_ro08(bios, init->offset + 5);
1314         u8 value;
1315
1316         trace("I2C_IF\tI2C[0x%02x][0x%02x][0x%02x] & 0x%02x == 0x%02x\n",
1317               index, addr, reg, mask, data);
1318         init->offset += 6;
1319         init_exec_force(init, true);
1320
1321         value = init_rdi2cr(init, index, addr, reg);
1322         if ((value & mask) != data)
1323                 init_exec_set(init, false);
1324
1325         init_exec_force(init, false);
1326 }
1327
1328 /**
1329  * INIT_COPY_NV_REG - opcode 0x5f
1330  *
1331  */
1332 static void
1333 init_copy_nv_reg(struct nvbios_init *init)
1334 {
1335         struct nouveau_bios *bios = init->bios;
1336         u32  sreg = nv_ro32(bios, init->offset + 1);
1337         u8  shift = nv_ro08(bios, init->offset + 5);
1338         u32 smask = nv_ro32(bios, init->offset + 6);
1339         u32  sxor = nv_ro32(bios, init->offset + 10);
1340         u32  dreg = nv_ro32(bios, init->offset + 14);
1341         u32 dmask = nv_ro32(bios, init->offset + 18);
1342         u32 data;
1343
1344         trace("COPY_NV_REG\tR[0x%06x] &= 0x%08x |= "
1345               "((R[0x%06x] %s 0x%02x) & 0x%08x ^ 0x%08x)\n",
1346               dreg, dmask, sreg, (shift & 0x80) ? "<<" : ">>",
1347               (shift & 0x80) ? (0x100 - shift) : shift, smask, sxor);
1348         init->offset += 22;
1349
1350         data = init_shift(init_rd32(init, sreg), shift);
1351         init_mask(init, dreg, ~dmask, (data & smask) ^ sxor);
1352 }
1353
1354 /**
1355  * INIT_ZM_INDEX_IO - opcode 0x62
1356  *
1357  */
1358 static void
1359 init_zm_index_io(struct nvbios_init *init)
1360 {
1361         struct nouveau_bios *bios = init->bios;
1362         u16 port = nv_ro16(bios, init->offset + 1);
1363         u8 index = nv_ro08(bios, init->offset + 3);
1364         u8  data = nv_ro08(bios, init->offset + 4);
1365
1366         trace("ZM_INDEX_IO\tI[0x%04x][0x%02x] = 0x%02x\n", port, index, data);
1367         init->offset += 5;
1368
1369         init_wrvgai(init, port, index, data);
1370 }
1371
1372 /**
1373  * INIT_COMPUTE_MEM - opcode 0x63
1374  *
1375  */
1376 static void
1377 init_compute_mem(struct nvbios_init *init)
1378 {
1379         struct nouveau_devinit *devinit = nouveau_devinit(init->bios);
1380
1381         trace("COMPUTE_MEM\n");
1382         init->offset += 1;
1383
1384         init_exec_force(init, true);
1385         if (init_exec(init) && devinit->meminit)
1386                 devinit->meminit(devinit);
1387         init_exec_force(init, false);
1388 }
1389
1390 /**
1391  * INIT_RESET - opcode 0x65
1392  *
1393  */
1394 static void
1395 init_reset(struct nvbios_init *init)
1396 {
1397         struct nouveau_bios *bios = init->bios;
1398         u32   reg = nv_ro32(bios, init->offset + 1);
1399         u32 data1 = nv_ro32(bios, init->offset + 5);
1400         u32 data2 = nv_ro32(bios, init->offset + 9);
1401         u32 savepci19;
1402
1403         trace("RESET\tR[0x%08x] = 0x%08x, 0x%08x", reg, data1, data2);
1404         init->offset += 13;
1405         init_exec_force(init, true);
1406
1407         savepci19 = init_mask(init, 0x00184c, 0x00000f00, 0x00000000);
1408         init_wr32(init, reg, data1);
1409         udelay(10);
1410         init_wr32(init, reg, data2);
1411         init_wr32(init, 0x00184c, savepci19);
1412         init_mask(init, 0x001850, 0x00000001, 0x00000000);
1413
1414         init_exec_force(init, false);
1415 }
1416
1417 /**
1418  * INIT_CONFIGURE_MEM - opcode 0x66
1419  *
1420  */
1421 static u16
1422 init_configure_mem_clk(struct nvbios_init *init)
1423 {
1424         u16 mdata = bmp_mem_init_table(init->bios);
1425         if (mdata)
1426                 mdata += (init_rdvgai(init, 0x03d4, 0x3c) >> 4) * 66;
1427         return mdata;
1428 }
1429
1430 static void
1431 init_configure_mem(struct nvbios_init *init)
1432 {
1433         struct nouveau_bios *bios = init->bios;
1434         u16 mdata, sdata;
1435         u32 addr, data;
1436
1437         trace("CONFIGURE_MEM\n");
1438         init->offset += 1;
1439
1440         if (bios->version.major > 2) {
1441                 init_done(init);
1442                 return;
1443         }
1444         init_exec_force(init, true);
1445
1446         mdata = init_configure_mem_clk(init);
1447         sdata = bmp_sdr_seq_table(bios);
1448         if (nv_ro08(bios, mdata) & 0x01)
1449                 sdata = bmp_ddr_seq_table(bios);
1450         mdata += 6; /* skip to data */
1451
1452         data = init_rdvgai(init, 0x03c4, 0x01);
1453         init_wrvgai(init, 0x03c4, 0x01, data | 0x20);
1454
1455         while ((addr = nv_ro32(bios, sdata)) != 0xffffffff) {
1456                 switch (addr) {
1457                 case 0x10021c: /* CKE_NORMAL */
1458                 case 0x1002d0: /* CMD_REFRESH */
1459                 case 0x1002d4: /* CMD_PRECHARGE */
1460                         data = 0x00000001;
1461                         break;
1462                 default:
1463                         data = nv_ro32(bios, mdata);
1464                         mdata += 4;
1465                         if (data == 0xffffffff)
1466                                 continue;
1467                         break;
1468                 }
1469
1470                 init_wr32(init, addr, data);
1471         }
1472
1473         init_exec_force(init, false);
1474 }
1475
1476 /**
1477  * INIT_CONFIGURE_CLK - opcode 0x67
1478  *
1479  */
1480 static void
1481 init_configure_clk(struct nvbios_init *init)
1482 {
1483         struct nouveau_bios *bios = init->bios;
1484         u16 mdata, clock;
1485
1486         trace("CONFIGURE_CLK\n");
1487         init->offset += 1;
1488
1489         if (bios->version.major > 2) {
1490                 init_done(init);
1491                 return;
1492         }
1493         init_exec_force(init, true);
1494
1495         mdata = init_configure_mem_clk(init);
1496
1497         /* NVPLL */
1498         clock = nv_ro16(bios, mdata + 4) * 10;
1499         init_prog_pll(init, 0x680500, clock);
1500
1501         /* MPLL */
1502         clock = nv_ro16(bios, mdata + 2) * 10;
1503         if (nv_ro08(bios, mdata) & 0x01)
1504                 clock *= 2;
1505         init_prog_pll(init, 0x680504, clock);
1506
1507         init_exec_force(init, false);
1508 }
1509
1510 /**
1511  * INIT_CONFIGURE_PREINIT - opcode 0x68
1512  *
1513  */
1514 static void
1515 init_configure_preinit(struct nvbios_init *init)
1516 {
1517         struct nouveau_bios *bios = init->bios;
1518         u32 strap;
1519
1520         trace("CONFIGURE_PREINIT\n");
1521         init->offset += 1;
1522
1523         if (bios->version.major > 2) {
1524                 init_done(init);
1525                 return;
1526         }
1527         init_exec_force(init, true);
1528
1529         strap = init_rd32(init, 0x101000);
1530         strap = ((strap << 2) & 0xf0) | ((strap & 0x40) >> 6);
1531         init_wrvgai(init, 0x03d4, 0x3c, strap);
1532
1533         init_exec_force(init, false);
1534 }
1535
1536 /**
1537  * INIT_IO - opcode 0x69
1538  *
1539  */
1540 static void
1541 init_io(struct nvbios_init *init)
1542 {
1543         struct nouveau_bios *bios = init->bios;
1544         u16 port = nv_ro16(bios, init->offset + 1);
1545         u8  mask = nv_ro16(bios, init->offset + 3);
1546         u8  data = nv_ro16(bios, init->offset + 4);
1547         u8 value;
1548
1549         trace("IO\t\tI[0x%04x] &= 0x%02x |= 0x%02x\n", port, mask, data);
1550         init->offset += 5;
1551
1552         /* ummm.. yes.. should really figure out wtf this is and why it's
1553          * needed some day..  it's almost certainly wrong, but, it also
1554          * somehow makes things work...
1555          */
1556         if (nv_device(init->bios)->card_type >= NV_50 &&
1557             port == 0x03c3 && data == 0x01) {
1558                 init_mask(init, 0x614100, 0xf0800000, 0x00800000);
1559                 init_mask(init, 0x00e18c, 0x00020000, 0x00020000);
1560                 init_mask(init, 0x614900, 0xf0800000, 0x00800000);
1561                 init_mask(init, 0x000200, 0x40000000, 0x00000000);
1562                 mdelay(10);
1563                 init_mask(init, 0x00e18c, 0x00020000, 0x00000000);
1564                 init_mask(init, 0x000200, 0x40000000, 0x40000000);
1565                 init_wr32(init, 0x614100, 0x00800018);
1566                 init_wr32(init, 0x614900, 0x00800018);
1567                 mdelay(10);
1568                 init_wr32(init, 0x614100, 0x10000018);
1569                 init_wr32(init, 0x614900, 0x10000018);
1570         }
1571
1572         value = init_rdport(init, port) & mask;
1573         init_wrport(init, port, data | value);
1574 }
1575
1576 /**
1577  * INIT_SUB - opcode 0x6b
1578  *
1579  */
1580 static void
1581 init_sub(struct nvbios_init *init)
1582 {
1583         struct nouveau_bios *bios = init->bios;
1584         u8 index = nv_ro08(bios, init->offset + 1);
1585         u16 addr, save;
1586
1587         trace("SUB\t0x%02x\n", index);
1588
1589         addr = init_script(bios, index);
1590         if (addr && init_exec(init)) {
1591                 save = init->offset;
1592                 init->offset = addr;
1593                 if (nvbios_exec(init)) {
1594                         error("error parsing sub-table\n");
1595                         return;
1596                 }
1597                 init->offset = save;
1598         }
1599
1600         init->offset += 2;
1601 }
1602
1603 /**
1604  * INIT_RAM_CONDITION - opcode 0x6d
1605  *
1606  */
1607 static void
1608 init_ram_condition(struct nvbios_init *init)
1609 {
1610         struct nouveau_bios *bios = init->bios;
1611         u8  mask = nv_ro08(bios, init->offset + 1);
1612         u8 value = nv_ro08(bios, init->offset + 2);
1613
1614         trace("RAM_CONDITION\t"
1615               "(R[0x100000] & 0x%02x) == 0x%02x\n", mask, value);
1616         init->offset += 3;
1617
1618         if ((init_rd32(init, 0x100000) & mask) != value)
1619                 init_exec_set(init, false);
1620 }
1621
1622 /**
1623  * INIT_NV_REG - opcode 0x6e
1624  *
1625  */
1626 static void
1627 init_nv_reg(struct nvbios_init *init)
1628 {
1629         struct nouveau_bios *bios = init->bios;
1630         u32  reg = nv_ro32(bios, init->offset + 1);
1631         u32 mask = nv_ro32(bios, init->offset + 5);
1632         u32 data = nv_ro32(bios, init->offset + 9);
1633
1634         trace("NV_REG\tR[0x%06x] &= 0x%08x |= 0x%08x\n", reg, mask, data);
1635         init->offset += 13;
1636
1637         init_mask(init, reg, ~mask, data);
1638 }
1639
1640 /**
1641  * INIT_MACRO - opcode 0x6f
1642  *
1643  */
1644 static void
1645 init_macro(struct nvbios_init *init)
1646 {
1647         struct nouveau_bios *bios = init->bios;
1648         u8  macro = nv_ro08(bios, init->offset + 1);
1649         u16 table;
1650
1651         trace("MACRO\t0x%02x\n", macro);
1652
1653         table = init_macro_table(init);
1654         if (table) {
1655                 u32 addr = nv_ro32(bios, table + (macro * 8) + 0);
1656                 u32 data = nv_ro32(bios, table + (macro * 8) + 4);
1657                 trace("\t\tR[0x%06x] = 0x%08x\n", addr, data);
1658                 init_wr32(init, addr, data);
1659         }
1660
1661         init->offset += 2;
1662 }
1663
1664 /**
1665  * INIT_RESUME - opcode 0x72
1666  *
1667  */
1668 static void
1669 init_resume(struct nvbios_init *init)
1670 {
1671         trace("RESUME\n");
1672         init->offset += 1;
1673         init_exec_set(init, true);
1674 }
1675
1676 /**
1677  * INIT_TIME - opcode 0x74
1678  *
1679  */
1680 static void
1681 init_time(struct nvbios_init *init)
1682 {
1683         struct nouveau_bios *bios = init->bios;
1684         u16 usec = nv_ro16(bios, init->offset + 1);
1685
1686         trace("TIME\t0x%04x\n", usec);
1687         init->offset += 3;
1688
1689         if (init_exec(init)) {
1690                 if (usec < 1000)
1691                         udelay(usec);
1692                 else
1693                         mdelay((usec + 900) / 1000);
1694         }
1695 }
1696
1697 /**
1698  * INIT_CONDITION - opcode 0x75
1699  *
1700  */
1701 static void
1702 init_condition(struct nvbios_init *init)
1703 {
1704         struct nouveau_bios *bios = init->bios;
1705         u8 cond = nv_ro08(bios, init->offset + 1);
1706
1707         trace("CONDITION\t0x%02x\n", cond);
1708         init->offset += 2;
1709
1710         if (!init_condition_met(init, cond))
1711                 init_exec_set(init, false);
1712 }
1713
1714 /**
1715  * INIT_IO_CONDITION - opcode 0x76
1716  *
1717  */
1718 static void
1719 init_io_condition(struct nvbios_init *init)
1720 {
1721         struct nouveau_bios *bios = init->bios;
1722         u8 cond = nv_ro08(bios, init->offset + 1);
1723
1724         trace("IO_CONDITION\t0x%02x\n", cond);
1725         init->offset += 2;
1726
1727         if (!init_io_condition_met(init, cond))
1728                 init_exec_set(init, false);
1729 }
1730
1731 /**
1732  * INIT_INDEX_IO - opcode 0x78
1733  *
1734  */
1735 static void
1736 init_index_io(struct nvbios_init *init)
1737 {
1738         struct nouveau_bios *bios = init->bios;
1739         u16 port = nv_ro16(bios, init->offset + 1);
1740         u8 index = nv_ro16(bios, init->offset + 3);
1741         u8  mask = nv_ro08(bios, init->offset + 4);
1742         u8  data = nv_ro08(bios, init->offset + 5);
1743         u8 value;
1744
1745         trace("INDEX_IO\tI[0x%04x][0x%02x] &= 0x%02x |= 0x%02x\n",
1746               port, index, mask, data);
1747         init->offset += 6;
1748
1749         value = init_rdvgai(init, port, index) & mask;
1750         init_wrvgai(init, port, index, data | value);
1751 }
1752
1753 /**
1754  * INIT_PLL - opcode 0x79
1755  *
1756  */
1757 static void
1758 init_pll(struct nvbios_init *init)
1759 {
1760         struct nouveau_bios *bios = init->bios;
1761         u32  reg = nv_ro32(bios, init->offset + 1);
1762         u32 freq = nv_ro16(bios, init->offset + 5) * 10;
1763
1764         trace("PLL\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
1765         init->offset += 7;
1766
1767         init_prog_pll(init, reg, freq);
1768 }
1769
1770 /**
1771  * INIT_ZM_REG - opcode 0x7a
1772  *
1773  */
1774 static void
1775 init_zm_reg(struct nvbios_init *init)
1776 {
1777         struct nouveau_bios *bios = init->bios;
1778         u32 addr = nv_ro32(bios, init->offset + 1);
1779         u32 data = nv_ro32(bios, init->offset + 5);
1780
1781         trace("ZM_REG\tR[0x%06x] = 0x%08x\n", addr, data);
1782         init->offset += 9;
1783
1784         if (addr == 0x000200)
1785                 data |= 0x00000001;
1786
1787         init_wr32(init, addr, data);
1788 }
1789
1790 /**
1791  * INIT_RAM_RESTRICT_PLL - opcde 0x87
1792  *
1793  */
1794 static void
1795 init_ram_restrict_pll(struct nvbios_init *init)
1796 {
1797         struct nouveau_bios *bios = init->bios;
1798         u8  type = nv_ro08(bios, init->offset + 1);
1799         u8 count = init_ram_restrict_group_count(init);
1800         u8 strap = init_ram_restrict(init);
1801         u8 cconf;
1802
1803         trace("RAM_RESTRICT_PLL\t0x%02x\n", type);
1804         init->offset += 2;
1805
1806         for (cconf = 0; cconf < count; cconf++) {
1807                 u32 freq = nv_ro32(bios, init->offset);
1808
1809                 if (cconf == strap) {
1810                         trace("%dkHz *\n", freq);
1811                         init_prog_pll(init, type, freq);
1812                 } else {
1813                         trace("%dkHz\n", freq);
1814                 }
1815
1816                 init->offset += 4;
1817         }
1818 }
1819
1820 /**
1821  * INIT_GPIO - opcode 0x8e
1822  *
1823  */
1824 static void
1825 init_gpio(struct nvbios_init *init)
1826 {
1827         struct nouveau_gpio *gpio = nouveau_gpio(init->bios);
1828
1829         trace("GPIO\n");
1830         init->offset += 1;
1831
1832         if (init_exec(init) && gpio && gpio->reset)
1833                 gpio->reset(gpio, DCB_GPIO_UNUSED);
1834 }
1835
1836 /**
1837  * INIT_RAM_RESTRICT_ZM_GROUP - opcode 0x8f
1838  *
1839  */
1840 static void
1841 init_ram_restrict_zm_reg_group(struct nvbios_init *init)
1842 {
1843         struct nouveau_bios *bios = init->bios;
1844         u32 addr = nv_ro32(bios, init->offset + 1);
1845         u8  incr = nv_ro08(bios, init->offset + 5);
1846         u8   num = nv_ro08(bios, init->offset + 6);
1847         u8 count = init_ram_restrict_group_count(init);
1848         u8 index = init_ram_restrict(init);
1849         u8 i, j;
1850
1851         trace("RAM_RESTRICT_ZM_REG_GROUP\t"
1852               "R[0x%08x] 0x%02x 0x%02x\n", addr, incr, num);
1853         init->offset += 7;
1854
1855         for (i = 0; i < num; i++) {
1856                 trace("\tR[0x%06x] = {\n", addr);
1857                 for (j = 0; j < count; j++) {
1858                         u32 data = nv_ro32(bios, init->offset);
1859
1860                         if (j == index) {
1861                                 trace("\t\t0x%08x *\n", data);
1862                                 init_wr32(init, addr, data);
1863                         } else {
1864                                 trace("\t\t0x%08x\n", data);
1865                         }
1866
1867                         init->offset += 4;
1868                 }
1869                 trace("\t}\n");
1870                 addr += incr;
1871         }
1872 }
1873
1874 /**
1875  * INIT_COPY_ZM_REG - opcode 0x90
1876  *
1877  */
1878 static void
1879 init_copy_zm_reg(struct nvbios_init *init)
1880 {
1881         struct nouveau_bios *bios = init->bios;
1882         u32 sreg = nv_ro32(bios, init->offset + 1);
1883         u32 dreg = nv_ro32(bios, init->offset + 5);
1884
1885         trace("COPY_ZM_REG\tR[0x%06x] = R[0x%06x]\n", dreg, sreg);
1886         init->offset += 9;
1887
1888         init_wr32(init, dreg, init_rd32(init, sreg));
1889 }
1890
1891 /**
1892  * INIT_ZM_REG_GROUP - opcode 0x91
1893  *
1894  */
1895 static void
1896 init_zm_reg_group(struct nvbios_init *init)
1897 {
1898         struct nouveau_bios *bios = init->bios;
1899         u32 addr = nv_ro32(bios, init->offset + 1);
1900         u8 count = nv_ro08(bios, init->offset + 5);
1901
1902         trace("ZM_REG_GROUP\tR[0x%06x] =\n", addr);
1903         init->offset += 6;
1904
1905         while (count--) {
1906                 u32 data = nv_ro32(bios, init->offset);
1907                 trace("\t0x%08x\n", data);
1908                 init_wr32(init, addr, data);
1909                 init->offset += 4;
1910         }
1911 }
1912
1913 /**
1914  * INIT_XLAT - opcode 0x96
1915  *
1916  */
1917 static void
1918 init_xlat(struct nvbios_init *init)
1919 {
1920         struct nouveau_bios *bios = init->bios;
1921         u32 saddr = nv_ro32(bios, init->offset + 1);
1922         u8 sshift = nv_ro08(bios, init->offset + 5);
1923         u8  smask = nv_ro08(bios, init->offset + 6);
1924         u8  index = nv_ro08(bios, init->offset + 7);
1925         u32 daddr = nv_ro32(bios, init->offset + 8);
1926         u32 dmask = nv_ro32(bios, init->offset + 12);
1927         u8  shift = nv_ro08(bios, init->offset + 16);
1928         u32 data;
1929
1930         trace("INIT_XLAT\tR[0x%06x] &= 0x%08x |= "
1931               "(X%02x((R[0x%06x] %s 0x%02x) & 0x%02x) << 0x%02x)\n",
1932               daddr, dmask, index, saddr, (sshift & 0x80) ? "<<" : ">>",
1933               (sshift & 0x80) ? (0x100 - sshift) : sshift, smask, shift);
1934         init->offset += 17;
1935
1936         data = init_shift(init_rd32(init, saddr), sshift) & smask;
1937         data = init_xlat_(init, index, data) << shift;
1938         init_mask(init, daddr, ~dmask, data);
1939 }
1940
1941 /**
1942  * INIT_ZM_MASK_ADD - opcode 0x97
1943  *
1944  */
1945 static void
1946 init_zm_mask_add(struct nvbios_init *init)
1947 {
1948         struct nouveau_bios *bios = init->bios;
1949         u32 addr = nv_ro32(bios, init->offset + 1);
1950         u32 mask = nv_ro32(bios, init->offset + 5);
1951         u32  add = nv_ro32(bios, init->offset + 9);
1952         u32 data;
1953
1954         trace("ZM_MASK_ADD\tR[0x%06x] &= 0x%08x += 0x%08x\n", addr, mask, add);
1955         init->offset += 13;
1956
1957         data =  init_rd32(init, addr);
1958         data = (data & mask) | ((data + add) & ~mask);
1959         init_wr32(init, addr, data);
1960 }
1961
1962 /**
1963  * INIT_AUXCH - opcode 0x98
1964  *
1965  */
1966 static void
1967 init_auxch(struct nvbios_init *init)
1968 {
1969         struct nouveau_bios *bios = init->bios;
1970         u32 addr = nv_ro32(bios, init->offset + 1);
1971         u8 count = nv_ro08(bios, init->offset + 5);
1972
1973         trace("AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
1974         init->offset += 6;
1975
1976         while (count--) {
1977                 u8 mask = nv_ro08(bios, init->offset + 0);
1978                 u8 data = nv_ro08(bios, init->offset + 1);
1979                 trace("\tAUX[0x%08x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
1980                 mask = init_rdauxr(init, addr) & mask;
1981                 init_wrauxr(init, addr, mask | data);
1982                 init->offset += 2;
1983         }
1984 }
1985
1986 /**
1987  * INIT_AUXCH - opcode 0x99
1988  *
1989  */
1990 static void
1991 init_zm_auxch(struct nvbios_init *init)
1992 {
1993         struct nouveau_bios *bios = init->bios;
1994         u32 addr = nv_ro32(bios, init->offset + 1);
1995         u8 count = nv_ro08(bios, init->offset + 5);
1996
1997         trace("ZM_AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
1998         init->offset += 6;
1999
2000         while (count--) {
2001                 u8 data = nv_ro08(bios, init->offset + 0);
2002                 trace("\tAUX[0x%08x] = 0x%02x\n", addr, data);
2003                 init_wrauxr(init, addr, data);
2004                 init->offset += 1;
2005         }
2006 }
2007
2008 /**
2009  * INIT_I2C_LONG_IF - opcode 0x9a
2010  *
2011  */
2012 static void
2013 init_i2c_long_if(struct nvbios_init *init)
2014 {
2015         struct nouveau_bios *bios = init->bios;
2016         u8 index = nv_ro08(bios, init->offset + 1);
2017         u8  addr = nv_ro08(bios, init->offset + 2) >> 1;
2018         u8 reglo = nv_ro08(bios, init->offset + 3);
2019         u8 reghi = nv_ro08(bios, init->offset + 4);
2020         u8  mask = nv_ro08(bios, init->offset + 5);
2021         u8  data = nv_ro08(bios, init->offset + 6);
2022         struct nouveau_i2c_port *port;
2023
2024         trace("I2C_LONG_IF\t"
2025               "I2C[0x%02x][0x%02x][0x%02x%02x] & 0x%02x == 0x%02x\n",
2026               index, addr, reglo, reghi, mask, data);
2027         init->offset += 7;
2028
2029         port = init_i2c(init, index);
2030         if (port) {
2031                 u8 i[2] = { reghi, reglo };
2032                 u8 o[1] = {};
2033                 struct i2c_msg msg[] = {
2034                         { .addr = addr, .flags = 0, .len = 2, .buf = i },
2035                         { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = o }
2036                 };
2037                 int ret;
2038
2039                 ret = i2c_transfer(&port->adapter, msg, 2);
2040                 if (ret == 2 && ((o[0] & mask) == data))
2041                         return;
2042         }
2043
2044         init_exec_set(init, false);
2045 }
2046
2047 /**
2048  * INIT_GPIO_NE - opcode 0xa9
2049  *
2050  */
2051 static void
2052 init_gpio_ne(struct nvbios_init *init)
2053 {
2054         struct nouveau_bios *bios = init->bios;
2055         struct nouveau_gpio *gpio = nouveau_gpio(bios);
2056         struct dcb_gpio_func func;
2057         u8 count = nv_ro08(bios, init->offset + 1);
2058         u8 idx = 0, ver, len;
2059         u16 data, i;
2060
2061         trace("GPIO_NE\t");
2062         init->offset += 2;
2063
2064         for (i = init->offset; i < init->offset + count; i++)
2065                 cont("0x%02x ", nv_ro08(bios, i));
2066         cont("\n");
2067
2068         while ((data = dcb_gpio_parse(bios, 0, idx++, &ver, &len, &func))) {
2069                 if (func.func != DCB_GPIO_UNUSED) {
2070                         for (i = init->offset; i < init->offset + count; i++) {
2071                                 if (func.func == nv_ro08(bios, i))
2072                                         break;
2073                         }
2074
2075                         trace("\tFUNC[0x%02x]", func.func);
2076                         if (i == (init->offset + count)) {
2077                                 cont(" *");
2078                                 if (init_exec(init) && gpio && gpio->reset)
2079                                         gpio->reset(gpio, func.func);
2080                         }
2081                         cont("\n");
2082                 }
2083         }
2084
2085         init->offset += count;
2086 }
2087
2088 static struct nvbios_init_opcode {
2089         void (*exec)(struct nvbios_init *);
2090 } init_opcode[] = {
2091         [0x32] = { init_io_restrict_prog },
2092         [0x33] = { init_repeat },
2093         [0x34] = { init_io_restrict_pll },
2094         [0x36] = { init_end_repeat },
2095         [0x37] = { init_copy },
2096         [0x38] = { init_not },
2097         [0x39] = { init_io_flag_condition },
2098         [0x3a] = { init_dp_condition },
2099         [0x3b] = { init_io_mask_or },
2100         [0x3c] = { init_io_or },
2101         [0x49] = { init_idx_addr_latched },
2102         [0x4a] = { init_io_restrict_pll2 },
2103         [0x4b] = { init_pll2 },
2104         [0x4c] = { init_i2c_byte },
2105         [0x4d] = { init_zm_i2c_byte },
2106         [0x4e] = { init_zm_i2c },
2107         [0x4f] = { init_tmds },
2108         [0x50] = { init_zm_tmds_group },
2109         [0x51] = { init_cr_idx_adr_latch },
2110         [0x52] = { init_cr },
2111         [0x53] = { init_zm_cr },
2112         [0x54] = { init_zm_cr_group },
2113         [0x56] = { init_condition_time },
2114         [0x57] = { init_ltime },
2115         [0x58] = { init_zm_reg_sequence },
2116         [0x5b] = { init_sub_direct },
2117         [0x5c] = { init_jump },
2118         [0x5e] = { init_i2c_if },
2119         [0x5f] = { init_copy_nv_reg },
2120         [0x62] = { init_zm_index_io },
2121         [0x63] = { init_compute_mem },
2122         [0x65] = { init_reset },
2123         [0x66] = { init_configure_mem },
2124         [0x67] = { init_configure_clk },
2125         [0x68] = { init_configure_preinit },
2126         [0x69] = { init_io },
2127         [0x6b] = { init_sub },
2128         [0x6d] = { init_ram_condition },
2129         [0x6e] = { init_nv_reg },
2130         [0x6f] = { init_macro },
2131         [0x71] = { init_done },
2132         [0x72] = { init_resume },
2133         [0x74] = { init_time },
2134         [0x75] = { init_condition },
2135         [0x76] = { init_io_condition },
2136         [0x78] = { init_index_io },
2137         [0x79] = { init_pll },
2138         [0x7a] = { init_zm_reg },
2139         [0x87] = { init_ram_restrict_pll },
2140         [0x8c] = { init_reserved },
2141         [0x8d] = { init_reserved },
2142         [0x8e] = { init_gpio },
2143         [0x8f] = { init_ram_restrict_zm_reg_group },
2144         [0x90] = { init_copy_zm_reg },
2145         [0x91] = { init_zm_reg_group },
2146         [0x92] = { init_reserved },
2147         [0x96] = { init_xlat },
2148         [0x97] = { init_zm_mask_add },
2149         [0x98] = { init_auxch },
2150         [0x99] = { init_zm_auxch },
2151         [0x9a] = { init_i2c_long_if },
2152         [0xa9] = { init_gpio_ne },
2153         [0xaa] = { init_reserved },
2154 };
2155
2156 #define init_opcode_nr (sizeof(init_opcode) / sizeof(init_opcode[0]))
2157
2158 int
2159 nvbios_exec(struct nvbios_init *init)
2160 {
2161         init->nested++;
2162         while (init->offset) {
2163                 u8 opcode = nv_ro08(init->bios, init->offset);
2164                 if (opcode >= init_opcode_nr || !init_opcode[opcode].exec) {
2165                         error("unknown opcode 0x%02x\n", opcode);
2166                         return -EINVAL;
2167                 }
2168
2169                 init_opcode[opcode].exec(init);
2170         }
2171         init->nested--;
2172         return 0;
2173 }
2174
2175 int
2176 nvbios_init(struct nouveau_subdev *subdev, bool execute)
2177 {
2178         struct nouveau_bios *bios = nouveau_bios(subdev);
2179         int ret = 0;
2180         int i = -1;
2181         u16 data;
2182
2183         if (execute)
2184                 nv_info(bios, "running init tables\n");
2185         while (!ret && (data = (init_script(bios, ++i)))) {
2186                 struct nvbios_init init = {
2187                         .subdev = subdev,
2188                         .bios = bios,
2189                         .offset = data,
2190                         .outp = NULL,
2191                         .crtc = -1,
2192                         .execute = execute ? 1 : 0,
2193                 };
2194
2195                 ret = nvbios_exec(&init);
2196         }
2197
2198         /* the vbios parser will run this right after the normal init
2199          * tables, whereas the binary driver appears to run it later.
2200          */
2201         if (!ret && (data = init_unknown_script(bios))) {
2202                 struct nvbios_init init = {
2203                         .subdev = subdev,
2204                         .bios = bios,
2205                         .offset = data,
2206                         .outp = NULL,
2207                         .crtc = -1,
2208                         .execute = execute ? 1 : 0,
2209                 };
2210
2211                 ret = nvbios_exec(&init);
2212         }
2213
2214         return 0;
2215 }