[modesetting-101] update mode count after fill_modes.
[platform/upstream/libdrm.git] / linux-core / nouveau_bios.c
1 /*
2  * Copyright (C) 2005-2006 Erik Waling
3  * Copyright (C) 2006 Stephane Marchesin
4  * Copyright (C) 2007-2008 Stuart Bennett
5  * Copyright (C) 2008 Maarten Maathuis.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial
18  * portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  */
29
30 #include <asm/byteorder.h>
31 #include "nouveau_bios.h"
32 #include "nouveau_drv.h"
33
34 /* returns true if it mismatches */
35 static bool nv_checksum(const uint8_t *data, unsigned int length)
36 {
37         /* there's a few checksums in the BIOS, so here's a generic checking function */
38         int i;
39         uint8_t sum = 0;
40
41         for (i = 0; i < length; i++)
42                 sum += data[i];
43
44         if (sum)
45                 return true;
46
47         return false;
48 }
49
50 static int nv_valid_bios(struct drm_device *dev, uint8_t *data)
51 {
52         /* check for BIOS signature */
53         if (!(data[0] == 0x55 && data[1] == 0xAA)) {
54                 DRM_ERROR("BIOS signature not found.\n");
55                 return 0;
56         }
57
58         if (nv_checksum(data, data[2] * 512)) {
59                 DRM_ERROR("BIOS checksum invalid.\n");
60                 return 1;
61         }
62
63         return 2;
64 }
65
66 static void nv_shadow_bios_rom(struct drm_device *dev, uint8_t *data)
67 {
68         struct drm_nouveau_private *dev_priv = dev->dev_private;
69         int i;
70
71         /* enable access to rom */
72         NV_WRITE(NV04_PBUS_PCI_NV_20, NV04_PBUS_PCI_NV_20_ROM_SHADOW_DISABLED);
73
74         /* This is also valid for pre-NV50, it just happened to be the only define already present. */
75         for (i=0; i < NV50_PROM__ESIZE; i++) {
76                 /* Appearantly needed for a 6600GT/6800LE bug. */
77                 data[i] = DRM_READ8(dev_priv->mmio, NV50_PROM + i);
78                 data[i] = DRM_READ8(dev_priv->mmio, NV50_PROM + i);
79                 data[i] = DRM_READ8(dev_priv->mmio, NV50_PROM + i);
80                 data[i] = DRM_READ8(dev_priv->mmio, NV50_PROM + i);
81                 data[i] = DRM_READ8(dev_priv->mmio, NV50_PROM + i);
82         }
83
84         /* disable access to rom */
85         NV_WRITE(NV04_PBUS_PCI_NV_20, NV04_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
86 }
87
88 static void nv_shadow_bios_ramin(struct drm_device *dev, uint8_t *data)
89 {
90         struct drm_nouveau_private *dev_priv = dev->dev_private;
91         uint32_t old_bar0_pramin = 0;
92         int i;
93
94         /* Move the bios copy to the start of ramin? */
95         if (dev_priv->card_type >= NV_50) {
96                 uint32_t vbios_vram = (NV_READ(0x619f04) & ~0xff) << 8;
97
98                 if (!vbios_vram)
99                         vbios_vram = (NV_READ(0x1700) << 16) + 0xf0000;
100
101                 old_bar0_pramin = NV_READ(0x1700);
102                 NV_WRITE(0x1700, vbios_vram >> 16);
103         }
104
105         for (i=0; i < NV50_PROM__ESIZE; i++)
106                 data[i] = DRM_READ8(dev_priv->mmio, NV04_PRAMIN + i);
107
108         if (dev_priv->card_type >= NV_50)
109                 NV_WRITE(0x1700, old_bar0_pramin);
110 }
111
112 static bool nv_shadow_bios(struct drm_device *dev, uint8_t *data)
113 {
114         nv_shadow_bios_rom(dev, data);
115         if (nv_valid_bios(dev, data) == 2)
116                 return true;
117
118         nv_shadow_bios_ramin(dev, data);
119         if (nv_valid_bios(dev, data))
120                 return true;
121
122         return false;
123 }
124
125 struct bit_entry {
126         uint8_t id[2];
127         uint16_t length;
128         uint16_t offset;
129 };
130
131 static int parse_bit_C_tbl_entry(struct drm_device *dev, struct bios *bios, struct bit_entry *bitentry)
132 {
133         /* offset + 8  (16 bits): PLL limits table pointer
134          *
135          * There's more in here, but that's unknown.
136          */
137
138         if (bitentry->length < 10) {
139                 DRM_ERROR( "Do not understand BIT C table\n");
140                 return 0;
141         }
142
143         bios->pll_limit_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 8])));
144
145         return 1;
146 }
147
148 static void parse_bit_structure(struct drm_device *dev, struct bios *bios, const uint16_t bitoffset)
149 {
150         int entries = bios->data[bitoffset + 4];
151         /* parse i first, I next (which needs C & M before it), and L before D */
152         char parseorder[] = "iCMILDT";
153         struct bit_entry bitentry;
154         int i, j, offset;
155
156         for (i = 0; i < sizeof(parseorder); i++) {
157                 for (j = 0, offset = bitoffset + 6; j < entries; j++, offset += 6) {
158                         bitentry.id[0] = bios->data[offset];
159                         bitentry.id[1] = bios->data[offset + 1];
160                         bitentry.length = le16_to_cpu(*((uint16_t *)&bios->data[offset + 2]));
161                         bitentry.offset = le16_to_cpu(*((uint16_t *)&bios->data[offset + 4]));
162
163                         if (bitentry.id[0] != parseorder[i])
164                                 continue;
165
166                         switch (bitentry.id[0]) {
167                         case 'C':
168                                 parse_bit_C_tbl_entry(dev, bios, &bitentry);
169                                 break;
170                         }
171                 }
172         }
173 }
174
175 static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
176 {
177         int i, j;
178
179         for (i = 0; i <= (n - len); i++) {
180                 for (j = 0; j < len; j++)
181                         if (data[i + j] != str[j])
182                                 break;
183                 if (j == len)
184                         return i;
185         }
186
187         return 0;
188 }
189
190 static void
191 read_dcb_i2c_entry(struct drm_device *dev, uint8_t dcb_version, uint16_t i2ctabptr, int index)
192 {
193         struct drm_nouveau_private *dev_priv = dev->dev_private;
194         struct bios *bios = &dev_priv->bios;
195         uint8_t *i2ctable = &bios->data[i2ctabptr];
196         uint8_t headerlen = 0;
197         int i2c_entries = MAX_NUM_DCB_ENTRIES;
198         int recordoffset = 0, rdofs = 1, wrofs = 0;
199
200         if (!i2ctabptr)
201                 return;
202
203         if (dcb_version >= 0x30) {
204                 if (i2ctable[0] != dcb_version) /* necessary? */
205                         DRM_ERROR(
206                                    "DCB I2C table version mismatch (%02X vs %02X)\n",
207                                    i2ctable[0], dcb_version);
208                 headerlen = i2ctable[1];
209                 i2c_entries = i2ctable[2];
210
211                 /* same address offset used for read and write for C51 and G80 */
212                 if (bios->chip_version == 0x51)
213                         rdofs = wrofs = 1;
214                 if (i2ctable[0] >= 0x40)
215                         rdofs = wrofs = 0;
216         }
217         /* it's your own fault if you call this function on a DCB 1.1 BIOS --
218          * the test below is for DCB 1.2
219          */
220         if (dcb_version < 0x14) {
221                 recordoffset = 2;
222                 rdofs = 0;
223                 wrofs = 1;
224         }
225
226         if (index == 0xf)
227                 return;
228         if (index > i2c_entries) {
229                 DRM_ERROR(
230                            "DCB I2C index too big (%d > %d)\n",
231                            index, i2ctable[2]);
232                 return;
233         }
234         if (i2ctable[headerlen + 4 * index + 3] == 0xff) {
235                 DRM_ERROR(
236                            "DCB I2C entry invalid\n");
237                 return;
238         }
239
240         if (bios->chip_version == 0x51) {
241                 int port_type = i2ctable[headerlen + 4 * index + 3];
242
243                 if (port_type != 4)
244                         DRM_ERROR(
245                                    "DCB I2C table has port type %d\n", port_type);
246         }
247         if (i2ctable[0] >= 0x40) {
248                 int port_type = i2ctable[headerlen + 4 * index + 3];
249
250                 if (port_type != 5)
251                         DRM_ERROR(
252                                    "DCB I2C table has port type %d\n", port_type);
253         }
254
255         dev_priv->dcb_table.i2c_read[index] = i2ctable[headerlen + recordoffset + rdofs + 4 * index];
256         dev_priv->dcb_table.i2c_write[index] = i2ctable[headerlen + recordoffset + wrofs + 4 * index];
257 }
258
259 static bool
260 parse_dcb_entry(struct drm_device *dev, int index, uint8_t dcb_version, uint16_t i2ctabptr, uint32_t conn, uint32_t conf)
261 {
262         struct drm_nouveau_private *dev_priv = dev->dev_private;
263         struct dcb_entry *entry = &dev_priv->dcb_table.entry[index];
264
265         memset(entry, 0, sizeof (struct dcb_entry));
266
267         entry->index = index;
268         /* safe defaults for a crt */
269         entry->type = 0;
270         entry->i2c_index = 0;
271         entry->heads = 1;
272         entry->bus = 0;
273         entry->location = LOC_ON_CHIP;
274         entry->or = 1;
275         entry->duallink_possible = false;
276
277         if (dcb_version >= 0x20) {
278                 entry->type = conn & 0xf;
279                 entry->i2c_index = (conn >> 4) & 0xf;
280                 entry->heads = (conn >> 8) & 0xf;
281                 entry->bus = (conn >> 16) & 0xf;
282                 entry->location = (conn >> 20) & 0xf;
283                 entry->or = (conn >> 24) & 0xf;
284                 /* Normal entries consist of a single bit, but dual link has the
285                  * adjacent more significant bit set too
286                  */
287                 if ((1 << (ffs(entry->or) - 1)) * 3 == entry->or)
288                         entry->duallink_possible = true;
289
290                 switch (entry->type) {
291                 case DCB_OUTPUT_LVDS:
292                         {
293                         uint32_t mask;
294                         if (conf & 0x1)
295                                 entry->lvdsconf.use_straps_for_mode = true;
296                         if (dcb_version < 0x22) {
297                                 mask = ~0xd;
298                                 /* both 0x4 and 0x8 show up in v2.0 tables; assume they mean
299                                  * the same thing, which is probably wrong, but might work */
300                                 if (conf & 0x4 || conf & 0x8)
301                                         entry->lvdsconf.use_power_scripts = true;
302                         } else {
303                                 mask = ~0x5;
304                                 if (conf & 0x4)
305                                         entry->lvdsconf.use_power_scripts = true;
306                         }
307                         if (conf & mask) {
308                                 DRM_ERROR(
309                                            "Unknown LVDS configuration bits, please report\n");
310                                 /* cause output setting to fail, so message is seen */
311                                 dev_priv->dcb_table.entries = 0;
312                                 return false;
313                         }
314                         break;
315                         }
316                 case 0xe:
317                         /* weird type that appears on g80 mobile bios; nv driver treats it as a terminator */
318                         return false;
319                 }
320                 read_dcb_i2c_entry(dev, dcb_version, i2ctabptr, entry->i2c_index);
321         } else if (dcb_version >= 0x14 ) {
322                 if (conn != 0xf0003f00 && conn != 0xf2247f10 && conn != 0xf2204001 && conn != 0xf2204301 && conn != 0xf2244311 && conn != 0xf2045f14 && conn != 0xf2205004 && conn != 0xf2208001 && conn != 0xf4204011 && conn != 0xf4208011 && conn != 0xf4248011) {
323                         DRM_ERROR(
324                                    "Unknown DCB 1.4 / 1.5 entry, please report\n");
325                         /* cause output setting to fail, so message is seen */
326                         dev_priv->dcb_table.entries = 0;
327                         return false;
328                 }
329                 /* most of the below is a "best guess" atm */
330                 entry->type = conn & 0xf;
331                 if (entry->type == 4) { /* digital */
332                         if (conn & 0x10)
333                                 entry->type = DCB_OUTPUT_LVDS;
334                         else
335                                 entry->type = DCB_OUTPUT_TMDS;
336                 }
337                 /* what's in bits 5-13? could be some brooktree/chrontel/philips thing, in tv case */
338                 entry->i2c_index = (conn >> 14) & 0xf;
339                 /* raw heads field is in range 0-1, so move to 1-2 */
340                 entry->heads = ((conn >> 18) & 0x7) + 1;
341                 entry->location = (conn >> 21) & 0xf;
342                 entry->bus = (conn >> 25) & 0x7;
343                 /* set or to be same as heads -- hopefully safe enough */
344                 entry->or = entry->heads;
345
346                 switch (entry->type) {
347                 case DCB_OUTPUT_LVDS:
348                         /* this is probably buried in conn's unknown bits */
349                         entry->lvdsconf.use_power_scripts = true;
350                         break;
351                 case DCB_OUTPUT_TMDS:
352                         /* invent a DVI-A output, by copying the fields of the DVI-D output
353                          * reported to work by math_b on an NV20(!) */
354                         memcpy(&entry[1], &entry[0], sizeof(struct dcb_entry));
355                         entry[1].type = DCB_OUTPUT_ANALOG;
356                         dev_priv->dcb_table.entries++;
357                 }
358                 read_dcb_i2c_entry(dev, dcb_version, i2ctabptr, entry->i2c_index);
359         } else if (dcb_version >= 0x12) {
360                 /* v1.2 tables normally have the same 5 entries, which are not
361                  * specific to the card, so use the defaults for a crt */
362                 /* DCB v1.2 does have an I2C table that read_dcb_i2c_table can handle, but cards
363                  * exist (seen on nv11) where the pointer to the table points to the wrong
364                  * place, so for now, we rely on the indices parsed in parse_bmp_structure
365                  */
366                 entry->i2c_index = dev_priv->bios.legacy.i2c_indices.crt;
367         } else { /* pre DCB / v1.1 - use the safe defaults for a crt */
368                 DRM_ERROR(
369                            "No information in BIOS output table; assuming a CRT output exists\n");
370                 entry->i2c_index = dev_priv->bios.legacy.i2c_indices.crt;
371         }
372
373         if (entry->type == DCB_OUTPUT_LVDS && dev_priv->bios.fp.strapping != 0xff)
374                 entry->lvdsconf.use_straps_for_mode = true;
375
376         dev_priv->dcb_table.entries++;
377
378         return true;
379 }
380
381 static void merge_like_dcb_entries(struct drm_device *dev)
382 {
383         struct drm_nouveau_private *dev_priv = dev->dev_private;
384
385         /* DCB v2.0 lists each output combination separately.
386          * Here we merge compatible entries to have fewer outputs, with more options
387          */
388         int i, newentries = 0;
389
390         for (i = 0; i < dev_priv->dcb_table.entries; i++) {
391                 struct dcb_entry *ient = &dev_priv->dcb_table.entry[i];
392                 int j;
393
394                 for (j = i + 1; j < dev_priv->dcb_table.entries; j++) {
395                         struct dcb_entry *jent = &dev_priv->dcb_table.entry[j];
396
397                         if (jent->type == 100) /* already merged entry */
398                                 continue;
399
400                         /* merge heads field when all other fields the same */
401                         if (jent->i2c_index == ient->i2c_index && jent->type == ient->type && jent->location == ient->location && jent->or == ient->or) {
402                                 DRM_INFO(
403                                            "Merging DCB entries %d and %d\n", i, j);
404                                 ient->heads |= jent->heads;
405                                 jent->type = 100; /* dummy value */
406                         }
407                 }
408         }
409
410         /* Compact entries merged into others out of dcb_table */
411         for (i = 0; i < dev_priv->dcb_table.entries; i++) {
412                 if ( dev_priv->dcb_table.entry[i].type == 100 )
413                         continue;
414
415                 if (newentries != i)
416                         memcpy(&dev_priv->dcb_table.entry[newentries], &dev_priv->dcb_table.entry[i], sizeof(struct dcb_entry));
417                 newentries++;
418         }
419
420         dev_priv->dcb_table.entries = newentries;
421 }
422
423 static unsigned int parse_dcb_table(struct drm_device *dev, struct bios *bios)
424 {
425         struct drm_nouveau_private *dev_priv = dev->dev_private;
426         uint16_t dcbptr, i2ctabptr = 0;
427         uint8_t *dcbtable;
428         uint8_t dcb_version, headerlen = 0x4, entries = MAX_NUM_DCB_ENTRIES;
429         bool configblock = true;
430         int recordlength = 8, confofs = 4;
431         int i;
432
433         dev_priv->dcb_table.entries = 0;
434
435         /* get the offset from 0x36 */
436         dcbptr = le16_to_cpu(*(uint16_t *)&bios->data[0x36]);
437
438         if (dcbptr == 0x0) {
439                 DRM_ERROR(
440                            "No Display Configuration Block pointer found\n");
441                 /* this situation likely means a really old card, pre DCB, so we'll add the safe CRT entry */
442                 parse_dcb_entry(dev, 0, 0, 0, 0, 0);
443                 return 1;
444         }
445
446         dcbtable = &bios->data[dcbptr];
447
448         /* get DCB version */
449         dcb_version = dcbtable[0];
450         DRM_INFO(
451                    "Display Configuration Block version %d.%d found\n",
452                    dcb_version >> 4, dcb_version & 0xf);
453
454         if (dcb_version >= 0x20) { /* NV17+ */
455                 uint32_t sig;
456
457                 if (dcb_version >= 0x30) { /* NV40+ */
458                         headerlen = dcbtable[1];
459                         entries = dcbtable[2];
460                         recordlength = dcbtable[3];
461                         i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[4]);
462                         sig = le32_to_cpu(*(uint32_t *)&dcbtable[6]);
463
464                         DRM_INFO(
465                                    "DCB header length %d, with %d possible entries\n",
466                                    headerlen, entries);
467                 } else {
468                         i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
469                         sig = le32_to_cpu(*(uint32_t *)&dcbtable[4]);
470                         headerlen = 8;
471                 }
472
473                 if (sig != 0x4edcbdcb) {
474                         DRM_ERROR(
475                                    "Bad Display Configuration Block signature (%08X)\n", sig);
476                         return 0;
477                 }
478         } else if (dcb_version >= 0x14) { /* some NV15/16, and NV11+ */
479                 char sig[8];
480
481                 memset(sig, 0, 8);
482                 strncpy(sig, (char *)&dcbtable[-7], 7);
483                 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
484                 recordlength = 10;
485                 confofs = 6;
486
487                 if (strcmp(sig, "DEV_REC")) {
488                         DRM_ERROR(
489                                    "Bad Display Configuration Block signature (%s)\n", sig);
490                         return 0;
491                 }
492         } else if (dcb_version >= 0x12) { /* some NV6/10, and NV15+ */
493                 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
494                 configblock = false;
495         } else {        /* NV5+, maybe NV4 */
496                 /* DCB 1.1 seems to be quite unhelpful - we'll just add the safe CRT entry */
497                 parse_dcb_entry(dev, 0, dcb_version, 0, 0, 0);
498                 return 1;
499         }
500
501         if (entries >= MAX_NUM_DCB_ENTRIES)
502                 entries = MAX_NUM_DCB_ENTRIES;
503
504         for (i = 0; i < entries; i++) {
505                 uint32_t connection, config = 0;
506
507                 connection = le32_to_cpu(*(uint32_t *)&dcbtable[headerlen + recordlength * i]);
508                 if (configblock)
509                         config = le32_to_cpu(*(uint32_t *)&dcbtable[headerlen + confofs + recordlength * i]);
510
511                 /* Should we allow discontinuous DCBs? Certainly DCB I2C tables can be discontinuous */
512                 if ((connection & 0x0000000f) == 0x0000000f) /* end of records */
513                         break;
514                 if (connection == 0x00000000) /* seen on an NV11 with DCB v1.5 */
515                         break;
516
517                 DRM_INFO("Raw DCB entry %d: %08x %08x\n", i, connection, config);
518                 if (!parse_dcb_entry(dev, dev_priv->dcb_table.entries, dcb_version, i2ctabptr, connection, config))
519                         break;
520         }
521
522         merge_like_dcb_entries(dev);
523
524         return dev_priv->dcb_table.entries;
525 }
526
527 int nouveau_parse_bios(struct drm_device *dev)
528 {
529         struct drm_nouveau_private *dev_priv = dev->dev_private;
530
531         const uint8_t bit_signature[] = { 'B', 'I', 'T' };
532         int offset;
533
534         dev_priv->bios.data = kzalloc(NV50_PROM__ESIZE, GFP_KERNEL);
535         if (!dev_priv->bios.data)
536                 return -ENOMEM;
537
538         if (!nv_shadow_bios(dev, dev_priv->bios.data))
539                 return -EINVAL;
540
541         dev_priv->bios.length = dev_priv->bios.data[2] * 512;
542         if (dev_priv->bios.length > NV50_PROM__ESIZE)
543                 dev_priv->bios.length = NV50_PROM__ESIZE;
544
545         if ((offset = findstr(dev_priv->bios.data, dev_priv->bios.length, bit_signature, sizeof(bit_signature)))) {
546                 DRM_INFO("BIT BIOS found\n");
547                 parse_bit_structure(dev, &dev_priv->bios, offset + 4);
548         } else {
549                 DRM_ERROR("BIT BIOS not found\n");
550                 return -EINVAL;
551         }
552
553         if (parse_dcb_table(dev, &dev_priv->bios))
554                 DRM_INFO("Found %d entries in DCB\n", dev_priv->dcb_table.entries);
555
556         return 0;
557 }
558
559 /* temporary */
560 #define NV_RAMDAC_NVPLL                 0x00680500
561 #define NV_RAMDAC_MPLL                  0x00680504
562 #define NV_RAMDAC_VPLL                  0x00680508
563 #       define NV_RAMDAC_PLL_COEFF_MDIV                 0x000000FF
564 #       define NV_RAMDAC_PLL_COEFF_NDIV                 0x0000FF00
565 #       define NV_RAMDAC_PLL_COEFF_PDIV                 0x00070000
566 #       define NV30_RAMDAC_ENABLE_VCO2                  (1 << 7)
567 #define NV_RAMDAC_VPLL2                 0x00680520
568
569 bool get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
570 {
571         /* PLL limits table
572          *
573          * Version 0x10: NV31
574          * One byte header (version), one record of 24 bytes
575          * Version 0x11: NV36 - Not implemented
576          * Seems to have same record style as 0x10, but 3 records rather than 1
577          * Version 0x20: Found on Geforce 6 cards
578          * Trivial 4 byte BIT header. 31 (0x1f) byte record length
579          * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
580          * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
581          * length in general, some (integrated) have an extra configuration byte
582          */
583
584         struct drm_nouveau_private *dev_priv = dev->dev_private;
585         struct bios *bios = &dev_priv->bios;
586         uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
587         int pllindex = 0;
588         uint32_t crystal_strap_mask, crystal_straps;
589
590         if (!bios->pll_limit_tbl_ptr) {
591                 if (bios->chip_version >= 0x40 || bios->chip_version == 0x31 || bios->chip_version == 0x36) {
592                         DRM_ERROR("Pointer to PLL limits table invalid\n");
593                         return false;
594                 }
595         } else {
596                 pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
597
598                 DRM_INFO("Found PLL limits table version 0x%X\n", pll_lim_ver);
599         }
600
601         crystal_strap_mask = 1 << 6;
602         /* open coded pNv->twoHeads test */
603         if (bios->chip_version > 0x10 && bios->chip_version != 0x15 &&
604                 bios->chip_version != 0x1a && bios->chip_version != 0x20)
605                 crystal_strap_mask |= 1 << 22;
606         crystal_straps = NV_READ(NV50_PEXTDEV + 0x0) & crystal_strap_mask;
607
608         switch (pll_lim_ver) {
609         /* we use version 0 to indicate a pre limit table bios (single stage pll)
610          * and load the hard coded limits instead */
611         case 0:
612                 break;
613         case 0x10:
614         case 0x11: /* strictly v0x11 has 3 entries, but the last two don't seem to get used */
615                 headerlen = 1;
616                 recordlen = 0x18;
617                 entries = 1;
618                 pllindex = 0;
619                 break;
620         case 0x20:
621         case 0x21:
622                 headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
623                 recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
624                 entries = bios->data[bios->pll_limit_tbl_ptr + 3];
625                 break;
626         default:
627                 DRM_ERROR("PLL limits table revision not currently supported\n");
628                 return false;
629         }
630
631         /* initialize all members to zero */
632         memset(pll_lim, 0, sizeof(struct pll_lims));
633
634         if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
635                 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex;
636
637                 pll_lim->vco1.minfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs])));
638                 pll_lim->vco1.maxfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 4])));
639                 pll_lim->vco2.minfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 8])));
640                 pll_lim->vco2.maxfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 12])));
641                 pll_lim->vco1.min_inputfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 16])));
642                 pll_lim->vco2.min_inputfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 20])));
643                 pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
644
645                 /* these values taken from nv30/31/36 */
646                 pll_lim->vco1.min_n = 0x1;
647                 if (bios->chip_version == 0x36)
648                         pll_lim->vco1.min_n = 0x5;
649                 pll_lim->vco1.max_n = 0xff;
650                 pll_lim->vco1.min_m = 0x1;
651                 pll_lim->vco1.max_m = 0xd;
652                 pll_lim->vco2.min_n = 0x4;
653                 /* on nv30, 31, 36 (i.e. all cards with two stage PLLs with this
654                  * table version (apart from nv35)), N2 is compared to
655                  * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
656                  * save a comparison
657                  */
658                 pll_lim->vco2.max_n = 0x28;
659                 if (bios->chip_version == 0x30 || bios->chip_version == 0x35)
660                        /* only 5 bits available for N2 on nv30/35 */
661                         pll_lim->vco2.max_n = 0x1f;
662                 pll_lim->vco2.min_m = 0x1;
663                 pll_lim->vco2.max_m = 0x4;
664         } else if (pll_lim_ver) {       /* ver 0x20, 0x21 */
665                 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
666                 uint32_t reg = 0; /* default match */
667                 int i;
668
669                 /* first entry is default match, if nothing better. warn if reg field nonzero */
670                 if (le32_to_cpu(*((uint32_t *)&bios->data[plloffs])))
671                         DRM_ERROR("Default PLL limit entry has non-zero register field\n");
672
673                 if (limit_match > MAX_PLL_TYPES)
674                         /* we've been passed a reg as the match */
675                         reg = limit_match;
676                 else /* limit match is a pll type */
677                         for (i = 1; i < entries && !reg; i++) {
678                                 uint32_t cmpreg = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + recordlen * i])));
679
680                                 if (limit_match == NVPLL && (cmpreg == NV_RAMDAC_NVPLL || cmpreg == 0x4000))
681                                         reg = cmpreg;
682                                 if (limit_match == MPLL && (cmpreg == NV_RAMDAC_MPLL || cmpreg == 0x4020))
683                                         reg = cmpreg;
684                                 if (limit_match == VPLL1 && (cmpreg == NV_RAMDAC_VPLL || cmpreg == 0x4010))
685                                         reg = cmpreg;
686                                 if (limit_match == VPLL2 && (cmpreg == NV_RAMDAC_VPLL2 || cmpreg == 0x4018))
687                                         reg = cmpreg;
688                         }
689
690                 for (i = 1; i < entries; i++)
691                         if (le32_to_cpu(*((uint32_t *)&bios->data[plloffs + recordlen * i])) == reg) {
692                                 pllindex = i;
693                                 break;
694                         }
695
696                 plloffs += recordlen * pllindex;
697
698                 DRM_INFO("Loading PLL limits for reg 0x%08x\n", pllindex ? reg : 0);
699
700                 /* frequencies are stored in tables in MHz, kHz are more useful, so we convert */
701
702                 /* What output frequencies can each VCO generate? */
703                 pll_lim->vco1.minfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 4]))) * 1000;
704                 pll_lim->vco1.maxfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 6]))) * 1000;
705                 pll_lim->vco2.minfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 8]))) * 1000;
706                 pll_lim->vco2.maxfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 10]))) * 1000;
707
708                 /* What input frequencies do they accept (past the m-divider)? */
709                 pll_lim->vco1.min_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 12]))) * 1000;
710                 pll_lim->vco2.min_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 14]))) * 1000;
711                 pll_lim->vco1.max_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 16]))) * 1000;
712                 pll_lim->vco2.max_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 18]))) * 1000;
713
714                 /* What values are accepted as multiplier and divider? */
715                 pll_lim->vco1.min_n = bios->data[plloffs + 20];
716                 pll_lim->vco1.max_n = bios->data[plloffs + 21];
717                 pll_lim->vco1.min_m = bios->data[plloffs + 22];
718                 pll_lim->vco1.max_m = bios->data[plloffs + 23];
719                 pll_lim->vco2.min_n = bios->data[plloffs + 24];
720                 pll_lim->vco2.max_n = bios->data[plloffs + 25];
721                 pll_lim->vco2.min_m = bios->data[plloffs + 26];
722                 pll_lim->vco2.max_m = bios->data[plloffs + 27];
723
724                 pll_lim->unk1c = bios->data[plloffs + 28];
725                 pll_lim->max_log2p_bias = bios->data[plloffs + 29];
726                 pll_lim->log2p_bias = bios->data[plloffs + 30];
727
728                 if (recordlen > 0x22)
729                         pll_lim->refclk = le32_to_cpu(*((uint32_t *)&bios->data[plloffs + 31]));
730
731                 if (recordlen > 0x23)
732                         if (bios->data[plloffs + 35])
733                                 DRM_ERROR("Bits set in PLL configuration byte (%x)\n", bios->data[plloffs + 35]);
734
735                 /* C51 special not seen elsewhere */
736                 /*if (bios->chip_version == 0x51 && !pll_lim->refclk) {
737                         uint32_t sel_clk = nv32_rd(pScrn, NV_RAMDAC_SEL_CLK);
738
739                         if (((limit_match == NV_RAMDAC_VPLL || limit_match == VPLL1) && sel_clk & 0x20) ||
740                             ((limit_match == NV_RAMDAC_VPLL2 || limit_match == VPLL2) && sel_clk & 0x80)) {
741                                 if (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_REVISION) < 0xa3)
742                                         pll_lim->refclk = 200000;
743                                 else
744                                         pll_lim->refclk = 25000;
745                         }
746                 }*/
747         }
748
749         /* By now any valid limit table ought to have set a max frequency for
750          * vco1, so if it's zero it's either a pre limit table bios, or one
751          * with an empty limit table (seen on nv18)
752          */
753         if (!pll_lim->vco1.maxfreq) {
754                 pll_lim->vco1.minfreq = bios->fminvco;
755                 pll_lim->vco1.maxfreq = bios->fmaxvco;
756                 pll_lim->vco1.min_inputfreq = 0;
757                 pll_lim->vco1.max_inputfreq = INT_MAX;
758                 pll_lim->vco1.min_n = 0x1;
759                 pll_lim->vco1.max_n = 0xff;
760                 pll_lim->vco1.min_m = 0x1;
761                 if (crystal_straps == 0) {
762                         /* nv05 does this, nv11 doesn't, nv10 unknown */
763                         if (bios->chip_version < 0x11)
764                                 pll_lim->vco1.min_m = 0x7;
765                         pll_lim->vco1.max_m = 0xd;
766                 } else {
767                         if (bios->chip_version < 0x11)
768                                 pll_lim->vco1.min_m = 0x8;
769                         pll_lim->vco1.max_m = 0xe;
770                 }
771         }
772
773         if (!pll_lim->refclk)
774                 switch (crystal_straps) {
775                 case 0:
776                         pll_lim->refclk = 13500;
777                         break;
778                 case (1 << 6):
779                         pll_lim->refclk = 14318;
780                         break;
781                 case (1 << 22):
782                         pll_lim->refclk = 27000;
783                         break;
784                 case (1 << 22 | 1 << 6):
785                         pll_lim->refclk = 25000;
786                         break;
787                 }
788
789 #if 1 /* for easy debugging */
790         DRM_INFO("pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
791         DRM_INFO("pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
792         DRM_INFO("pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
793         DRM_INFO("pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
794
795         DRM_INFO("pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
796         DRM_INFO("pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
797         DRM_INFO("pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
798         DRM_INFO("pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
799
800         DRM_INFO("pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
801         DRM_INFO("pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
802         DRM_INFO("pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
803         DRM_INFO("pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
804         DRM_INFO("pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
805         DRM_INFO("pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
806         DRM_INFO("pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
807         DRM_INFO("pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
808
809         DRM_INFO("pll.unk1c: %d\n", pll_lim->unk1c);
810         DRM_INFO("pll.max_log2p_bias: %d\n", pll_lim->max_log2p_bias);
811         DRM_INFO("pll.log2p_bias: %d\n", pll_lim->log2p_bias);
812
813         DRM_INFO("pll.refclk: %d\n", pll_lim->refclk);
814 #endif
815
816         return true;
817 }