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