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