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