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