upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / video / aty / radeon_base.c
1 /*
2  *      drivers/video/radeonfb.c
3  *      framebuffer driver for ATI Radeon chipset video boards
4  *
5  *      Copyright 2003  Ben. Herrenschmidt <benh@kernel.crashing.org>
6  *      Copyright 2000  Ani Joshi <ajoshi@kernel.crashing.org>
7  *
8  *      i2c bits from Luca Tettamanti <kronos@kronoz.cjb.net>
9  *      
10  *      Special thanks to ATI DevRel team for their hardware donations.
11  *
12  *      ...Insert GPL boilerplate here...
13  *
14  *      Significant portions of this driver apdated from XFree86 Radeon
15  *      driver which has the following copyright notice:
16  *
17  *      Copyright 2000 ATI Technologies Inc., Markham, Ontario, and
18  *                     VA Linux Systems Inc., Fremont, California.
19  *
20  *      All Rights Reserved.
21  *
22  *      Permission is hereby granted, free of charge, to any person obtaining
23  *      a copy of this software and associated documentation files (the
24  *      "Software"), to deal in the Software without restriction, including
25  *      without limitation on the rights to use, copy, modify, merge,
26  *      publish, distribute, sublicense, and/or sell copies of the Software,
27  *      and to permit persons to whom the Software is furnished to do so,
28  *      subject to the following conditions:
29  *
30  *      The above copyright notice and this permission notice (including the
31  *      next paragraph) shall be included in all copies or substantial
32  *      portions of the Software.
33  *
34  *      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35  *      EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36  *      MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37  *      NON-INFRINGEMENT.  IN NO EVENT SHALL ATI, VA LINUX SYSTEMS AND/OR
38  *      THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
39  *      WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40  *      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
41  *      DEALINGS IN THE SOFTWARE.
42  *
43  *      XFree86 driver authors:
44  *
45  *         Kevin E. Martin <martin@xfree86.org>
46  *         Rickard E. Faith <faith@valinux.com>
47  *         Alan Hourihane <alanh@fairlite.demon.co.uk>
48  *
49  */
50
51
52 #define RADEON_VERSION  "0.2.0"
53
54 #include <linux/config.h>
55 #include <linux/module.h>
56 #include <linux/moduleparam.h>
57 #include <linux/kernel.h>
58 #include <linux/errno.h>
59 #include <linux/string.h>
60 #include <linux/mm.h>
61 #include <linux/tty.h>
62 #include <linux/slab.h>
63 #include <linux/delay.h>
64 #include <linux/time.h>
65 #include <linux/fb.h>
66 #include <linux/ioport.h>
67 #include <linux/init.h>
68 #include <linux/pci.h>
69 #include <linux/vmalloc.h>
70 #include <linux/device.h>
71 #include <linux/i2c.h>
72
73 #include <asm/io.h>
74 #include <asm/uaccess.h>
75
76 #ifdef CONFIG_PPC_OF
77
78 #include <asm/prom.h>
79 #include <asm/pci-bridge.h>
80 #include "../macmodes.h"
81
82 #ifdef CONFIG_PMAC_BACKLIGHT
83 #include <asm/backlight.h>
84 #endif
85
86 #ifdef CONFIG_BOOTX_TEXT
87 #include <asm/btext.h>
88 #endif
89
90 #endif /* CONFIG_PPC_OF */
91
92 #ifdef CONFIG_MTRR
93 #include <asm/mtrr.h>
94 #endif
95
96 #include <video/radeon.h>
97 #include <linux/radeonfb.h>
98
99 #include "../edid.h" // MOVE THAT TO include/video
100 #include "ati_ids.h"
101 #include "radeonfb.h"               
102
103 #define MAX_MAPPED_VRAM (2048*2048*4)
104 #define MIN_MAPPED_VRAM (1024*768*1)
105
106 #define CHIP_DEF(id, family, flags)                                     \
107         { PCI_VENDOR_ID_ATI, id, PCI_ANY_ID, PCI_ANY_ID, 0, 0, (flags) | (CHIP_FAMILY_##family) }
108
109 static struct pci_device_id radeonfb_pci_table[] = {
110         /* Mobility M6 */
111         CHIP_DEF(PCI_CHIP_RADEON_LY,    RV100,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
112         CHIP_DEF(PCI_CHIP_RADEON_LZ,    RV100,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
113         /* Radeon VE/7000 */
114         CHIP_DEF(PCI_CHIP_RV100_QY,     RV100,  CHIP_HAS_CRTC2),
115         CHIP_DEF(PCI_CHIP_RV100_QZ,     RV100,  CHIP_HAS_CRTC2),
116         /* Radeon IGP320M (U1) */
117         CHIP_DEF(PCI_CHIP_RS100_4336,   RS100,  CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
118         /* Radeon IGP320 (A3) */
119         CHIP_DEF(PCI_CHIP_RS100_4136,   RS100,  CHIP_HAS_CRTC2 | CHIP_IS_IGP), 
120         /* IGP330M/340M/350M (U2) */
121         CHIP_DEF(PCI_CHIP_RS200_4337,   RS200,  CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
122         /* IGP330/340/350 (A4) */
123         CHIP_DEF(PCI_CHIP_RS200_4137,   RS200,  CHIP_HAS_CRTC2 | CHIP_IS_IGP),
124         /* Mobility 7000 IGP */
125         CHIP_DEF(PCI_CHIP_RS250_4437,   RS200,  CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
126         /* 7000 IGP (A4+) */
127         CHIP_DEF(PCI_CHIP_RS250_4237,   RS200,  CHIP_HAS_CRTC2 | CHIP_IS_IGP),
128         /* 8500 AIW */
129         CHIP_DEF(PCI_CHIP_R200_BB,      R200,   CHIP_HAS_CRTC2),
130         CHIP_DEF(PCI_CHIP_R200_BC,      R200,   CHIP_HAS_CRTC2),
131         /* 8700/8800 */
132         CHIP_DEF(PCI_CHIP_R200_QH,      R200,   CHIP_HAS_CRTC2),
133         /* 8500 */
134         CHIP_DEF(PCI_CHIP_R200_QL,      R200,   CHIP_HAS_CRTC2),
135         /* 9100 */
136         CHIP_DEF(PCI_CHIP_R200_QM,      R200,   CHIP_HAS_CRTC2),
137         /* Mobility M7 */
138         CHIP_DEF(PCI_CHIP_RADEON_LW,    RV200,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
139         CHIP_DEF(PCI_CHIP_RADEON_LX,    RV200,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
140         /* 7500 */
141         CHIP_DEF(PCI_CHIP_RV200_QW,     RV200,  CHIP_HAS_CRTC2),
142         CHIP_DEF(PCI_CHIP_RV200_QX,     RV200,  CHIP_HAS_CRTC2),
143         /* Mobility M9 */
144         CHIP_DEF(PCI_CHIP_RV250_Ld,     RV250,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
145         CHIP_DEF(PCI_CHIP_RV250_Le,     RV250,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
146         CHIP_DEF(PCI_CHIP_RV250_Lf,     RV250,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
147         CHIP_DEF(PCI_CHIP_RV250_Lg,     RV250,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
148         /* 9000/Pro */
149         CHIP_DEF(PCI_CHIP_RV250_If,     RV250,  CHIP_HAS_CRTC2),
150         CHIP_DEF(PCI_CHIP_RV250_Ig,     RV250,  CHIP_HAS_CRTC2),
151         /* Mobility 9100 IGP (U3) */
152         CHIP_DEF(PCI_CHIP_RS300_5835,   RS300,  CHIP_HAS_CRTC2 | CHIP_IS_IGP | CHIP_IS_MOBILITY),
153         /* 9100 IGP (A5) */
154         CHIP_DEF(PCI_CHIP_RS300_5834,   RS300,  CHIP_HAS_CRTC2 | CHIP_IS_IGP),
155         /* Mobility 9200 (M9+) */
156         CHIP_DEF(PCI_CHIP_RV280_5C61,   RV280,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
157         CHIP_DEF(PCI_CHIP_RV280_5C63,   RV280,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
158         /* 9200 */
159         CHIP_DEF(PCI_CHIP_RV280_5960,   RV280,  CHIP_HAS_CRTC2),
160         CHIP_DEF(PCI_CHIP_RV280_5961,   RV280,  CHIP_HAS_CRTC2),
161         CHIP_DEF(PCI_CHIP_RV280_5962,   RV280,  CHIP_HAS_CRTC2),
162         CHIP_DEF(PCI_CHIP_RV280_5964,   RV280,  CHIP_HAS_CRTC2),
163         /* 9500 */
164         CHIP_DEF(PCI_CHIP_R300_AD,      R300,   CHIP_HAS_CRTC2),
165         CHIP_DEF(PCI_CHIP_R300_AE,      R300,   CHIP_HAS_CRTC2),
166         /* 9600TX / FireGL Z1 */
167         CHIP_DEF(PCI_CHIP_R300_AF,      R300,   CHIP_HAS_CRTC2),
168         CHIP_DEF(PCI_CHIP_R300_AG,      R300,   CHIP_HAS_CRTC2),
169         /* 9700/9500/Pro/FireGL X1 */
170         CHIP_DEF(PCI_CHIP_R300_ND,      R300,   CHIP_HAS_CRTC2),
171         CHIP_DEF(PCI_CHIP_R300_NE,      R300,   CHIP_HAS_CRTC2),
172         CHIP_DEF(PCI_CHIP_R300_NF,      R300,   CHIP_HAS_CRTC2),
173         CHIP_DEF(PCI_CHIP_R300_NG,      R300,   CHIP_HAS_CRTC2),
174         /* Mobility M10/M11 */
175         CHIP_DEF(PCI_CHIP_RV350_NP,     RV350,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
176         CHIP_DEF(PCI_CHIP_RV350_NQ,     RV350,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
177         CHIP_DEF(PCI_CHIP_RV350_NR,     RV350,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
178         CHIP_DEF(PCI_CHIP_RV350_NS,     RV350,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
179         CHIP_DEF(PCI_CHIP_RV350_NT,     RV350,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
180         CHIP_DEF(PCI_CHIP_RV350_NV,     RV350,  CHIP_HAS_CRTC2 | CHIP_IS_MOBILITY),
181         /* 9600/FireGL T2 */
182         CHIP_DEF(PCI_CHIP_RV350_AP,     RV350,  CHIP_HAS_CRTC2),
183         CHIP_DEF(PCI_CHIP_RV350_AQ,     RV350,  CHIP_HAS_CRTC2),
184         CHIP_DEF(PCI_CHIP_RV360_AR,     RV350,  CHIP_HAS_CRTC2),
185         CHIP_DEF(PCI_CHIP_RV350_AS,     RV350,  CHIP_HAS_CRTC2),
186         CHIP_DEF(PCI_CHIP_RV350_AT,     RV350,  CHIP_HAS_CRTC2),
187         CHIP_DEF(PCI_CHIP_RV350_AV,     RV350,  CHIP_HAS_CRTC2),
188         /* 9800/Pro/FileGL X2 */
189         CHIP_DEF(PCI_CHIP_R350_AH,      R350,   CHIP_HAS_CRTC2),
190         CHIP_DEF(PCI_CHIP_R350_AI,      R350,   CHIP_HAS_CRTC2),
191         CHIP_DEF(PCI_CHIP_R350_AJ,      R350,   CHIP_HAS_CRTC2),
192         CHIP_DEF(PCI_CHIP_R350_AK,      R350,   CHIP_HAS_CRTC2),
193         CHIP_DEF(PCI_CHIP_R350_NH,      R350,   CHIP_HAS_CRTC2),
194         CHIP_DEF(PCI_CHIP_R350_NI,      R350,   CHIP_HAS_CRTC2),
195         CHIP_DEF(PCI_CHIP_R360_NJ,      R350,   CHIP_HAS_CRTC2),
196         CHIP_DEF(PCI_CHIP_R350_NK,      R350,   CHIP_HAS_CRTC2),
197         /* Original Radeon/7200 */
198         CHIP_DEF(PCI_CHIP_RADEON_QD,    RADEON, 0),
199         CHIP_DEF(PCI_CHIP_RADEON_QE,    RADEON, 0),
200         CHIP_DEF(PCI_CHIP_RADEON_QF,    RADEON, 0),
201         CHIP_DEF(PCI_CHIP_RADEON_QG,    RADEON, 0),
202         { 0, }
203 };
204 MODULE_DEVICE_TABLE(pci, radeonfb_pci_table);
205
206
207 typedef struct {
208         u16 reg;
209         u32 val;
210 } reg_val;
211
212
213 /* these common regs are cleared before mode setting so they do not
214  * interfere with anything
215  */
216 static reg_val common_regs[] = {
217         { OVR_CLR, 0 }, 
218         { OVR_WID_LEFT_RIGHT, 0 },
219         { OVR_WID_TOP_BOTTOM, 0 },
220         { OV0_SCALE_CNTL, 0 },
221         { SUBPIC_CNTL, 0 },
222         { VIPH_CONTROL, 0 },
223         { I2C_CNTL_1, 0 },
224         { GEN_INT_CNTL, 0 },
225         { CAP0_TRIG_CNTL, 0 },
226         { CAP1_TRIG_CNTL, 0 },
227 };
228
229 /*
230  * globals
231  */
232         
233 static char *mode_option;
234 static char *monitor_layout;
235 static int noaccel = 0;
236 static int nomodeset = 0;
237 static int ignore_edid = 0;
238 static int mirror = 0;
239 static int panel_yres = 0;
240 static int force_dfp = 0;
241 static int force_measure_pll = 0;
242 #ifdef CONFIG_MTRR
243 static int nomtrr = 0;
244 #endif
245
246 /*
247  * prototypes
248  */
249
250
251 #ifdef CONFIG_PPC_OF
252
253 #ifdef CONFIG_PMAC_BACKLIGHT
254 static int radeon_set_backlight_enable(int on, int level, void *data);
255 static int radeon_set_backlight_level(int level, void *data);
256 static struct backlight_controller radeon_backlight_controller = {
257         radeon_set_backlight_enable,
258         radeon_set_backlight_level
259 };
260 #endif /* CONFIG_PMAC_BACKLIGHT */
261
262 #endif /* CONFIG_PPC_OF */
263
264 static void __devexit radeon_unmap_ROM(struct radeonfb_info *rinfo, struct pci_dev *dev)
265 {
266         // leave it disabled and unassigned
267         struct resource *r = &dev->resource[PCI_ROM_RESOURCE];
268         
269         if (!rinfo->bios_seg)
270                 return;
271         iounmap(rinfo->bios_seg);
272         
273         /* Release the ROM resource if we used it in the first place */
274         if (r->parent && r->flags & PCI_ROM_ADDRESS_ENABLE) {
275                 release_resource(r);
276                 r->flags &= ~PCI_ROM_ADDRESS_ENABLE;
277                 r->end -= r->start;
278                 r->start = 0;
279         }
280         /* This will disable and set address to unassigned */
281         pci_write_config_dword(dev, dev->rom_base_reg, 0);
282 }
283
284 static int __devinit radeon_map_ROM(struct radeonfb_info *rinfo, struct pci_dev *dev)
285 {
286         void __iomem *rom;
287         struct resource *r;
288         u16 dptr;
289         u8 rom_type;
290
291         /* If this is a primary card, there is a shadow copy of the
292          * ROM somewhere in the first meg. We will just ignore the copy
293          * and use the ROM directly.
294          */
295     
296         /* Fix from ATI for problem with Radeon hardware not leaving ROM enabled */
297         unsigned int temp;
298         temp = INREG(MPP_TB_CONFIG);
299         temp &= 0x00ffffffu;
300         temp |= 0x04 << 24;
301         OUTREG(MPP_TB_CONFIG, temp);
302         temp = INREG(MPP_TB_CONFIG);
303                                                                                                           
304         /* no need to search for the ROM, just ask the card where it is. */
305         r = &dev->resource[PCI_ROM_RESOURCE];
306         
307         /* assign the ROM an address if it doesn't have one */
308         if (r->parent == NULL)
309                 pci_assign_resource(dev, PCI_ROM_RESOURCE);
310         
311         /* enable if needed */
312         if (!(r->flags & PCI_ROM_ADDRESS_ENABLE)) {
313                 pci_write_config_dword(dev, dev->rom_base_reg,
314                                        r->start | PCI_ROM_ADDRESS_ENABLE);
315                 r->flags |= PCI_ROM_ADDRESS_ENABLE;
316         }
317         
318         rom = ioremap(r->start, r->end - r->start + 1);
319         if (!rom) {
320                 printk(KERN_ERR "radeonfb: ROM failed to map\n");
321                 return -ENOMEM;
322         }
323         
324         rinfo->bios_seg = rom;
325
326         /* Very simple test to make sure it appeared */
327         if (BIOS_IN16(0) != 0xaa55) {
328                 printk(KERN_ERR "radeonfb: Invalid ROM signature %x should be 0xaa55\n",
329                        BIOS_IN16(0));
330                 goto failed;
331         }
332         /* Look for the PCI data to check the ROM type */
333         dptr = BIOS_IN16(0x18);
334
335         /* Check the PCI data signature. If it's wrong, we still assume a normal x86 ROM
336          * for now, until I've verified this works everywhere. The goal here is more
337          * to phase out Open Firmware images.
338          *
339          * Currently, we only look at the first PCI data, we could iteratre and deal with
340          * them all, and we should use fb_bios_start relative to start of image and not
341          * relative start of ROM, but so far, I never found a dual-image ATI card
342          *
343          * typedef struct {
344          *      u32     signature;      + 0x00
345          *      u16     vendor;         + 0x04
346          *      u16     device;         + 0x06
347          *      u16     reserved_1;     + 0x08
348          *      u16     dlen;           + 0x0a
349          *      u8      drevision;      + 0x0c
350          *      u8      class_hi;       + 0x0d
351          *      u16     class_lo;       + 0x0e
352          *      u16     ilen;           + 0x10
353          *      u16     irevision;      + 0x12
354          *      u8      type;           + 0x14
355          *      u8      indicator;      + 0x15
356          *      u16     reserved_2;     + 0x16
357          * } pci_data_t;
358          */
359         if (BIOS_IN32(dptr) !=  (('R' << 24) | ('I' << 16) | ('C' << 8) | 'P')) {
360                 printk(KERN_WARNING "radeonfb: PCI DATA signature in ROM incorrect: %08x\n",
361                        BIOS_IN32(dptr));
362                 goto anyway;
363         }
364         rom_type = BIOS_IN8(dptr + 0x14);
365         switch(rom_type) {
366         case 0:
367                 printk(KERN_INFO "radeonfb: Found Intel x86 BIOS ROM Image\n");
368                 break;
369         case 1:
370                 printk(KERN_INFO "radeonfb: Found Open Firmware ROM Image\n");
371                 goto failed;
372         case 2:
373                 printk(KERN_INFO "radeonfb: Found HP PA-RISC ROM Image\n");
374                 goto failed;
375         default:
376                 printk(KERN_INFO "radeonfb: Found unknown type %d ROM Image\n", rom_type);
377                 goto failed;
378         }
379  anyway:
380         /* Locate the flat panel infos, do some sanity checking !!! */
381         rinfo->fp_bios_start = BIOS_IN16(0x48);
382         return 0;
383
384  failed:
385         rinfo->bios_seg = NULL;
386         radeon_unmap_ROM(rinfo, dev);
387         return -ENXIO;
388 }
389
390 #ifdef CONFIG_X86
391 static int  __devinit radeon_find_mem_vbios(struct radeonfb_info *rinfo)
392 {
393         /* I simplified this code as we used to miss the signatures in
394          * a lot of case. It's now closer to XFree, we just don't check
395          * for signatures at all... Something better will have to be done
396          * if we end up having conflicts
397          */
398         u32  segstart;
399         void __iomem *rom_base = NULL;
400                                                 
401         for(segstart=0x000c0000; segstart<0x000f0000; segstart+=0x00001000) {
402                 rom_base = ioremap(segstart, 0x10000);
403                 if (rom_base == NULL)
404                         return -ENOMEM;
405                 if (readb(rom_base) == 0x55 && readb(rom_base + 1) == 0xaa)
406                         break;
407                 iounmap(rom_base);
408                 rom_base = NULL;
409         }
410         if (rom_base == NULL)
411                 return -ENXIO;
412
413         /* Locate the flat panel infos, do some sanity checking !!! */
414         rinfo->bios_seg = rom_base;
415         rinfo->fp_bios_start = BIOS_IN16(0x48);
416
417         return 0;
418 }
419 #endif
420
421 #ifdef CONFIG_PPC_OF
422 /*
423  * Read XTAL (ref clock), SCLK and MCLK from Open Firmware device
424  * tree. Hopefully, ATI OF driver is kind enough to fill these
425  */
426 static int __devinit radeon_read_xtal_OF (struct radeonfb_info *rinfo)
427 {
428         struct device_node *dp;
429         u32 *val;
430
431         dp = pci_device_to_OF_node(rinfo->pdev);
432         if (dp == NULL) {
433                 printk(KERN_WARNING "radeonfb: Cannot match card to OF node !\n");
434                 return -ENODEV;
435         }
436         val = (u32 *) get_property(dp, "ATY,RefCLK", NULL);
437         if (!val || !*val) {
438                 printk(KERN_WARNING "radeonfb: No ATY,RefCLK property !\n");
439                 return -EINVAL;
440         }
441
442         rinfo->pll.ref_clk = (*val) / 10;
443
444         val = (u32 *) get_property(dp, "ATY,SCLK", NULL);
445         if (val && *val)
446                 rinfo->pll.sclk = (*val) / 10;
447
448         val = (u32 *) get_property(dp, "ATY,MCLK", NULL);
449         if (val && *val)
450                 rinfo->pll.mclk = (*val) / 10;
451
452         return 0;
453 }
454 #endif /* CONFIG_PPC_OF */
455
456 /*
457  * Read PLL infos from chip registers
458  */
459 static int __devinit radeon_probe_pll_params(struct radeonfb_info *rinfo)
460 {
461         unsigned char ppll_div_sel;
462         unsigned Ns, Nm, M;
463         unsigned sclk, mclk, tmp, ref_div;
464         int hTotal, vTotal, num, denom, m, n;
465         unsigned long long hz, vclk;
466         long xtal;
467         struct timeval start_tv, stop_tv;
468         long total_secs, total_usecs;
469         int i;
470
471         /* Ugh, we cut interrupts, bad bad bad, but we want some precision
472          * here, so... --BenH
473          */
474
475         /* Flush PCI buffers ? */
476         tmp = INREG(DEVICE_ID);
477
478         local_irq_disable();
479
480         for(i=0; i<1000000; i++)
481                 if (((INREG(CRTC_VLINE_CRNT_VLINE) >> 16) & 0x3ff) == 0)
482                         break;
483
484         do_gettimeofday(&start_tv);
485
486         for(i=0; i<1000000; i++)
487                 if (((INREG(CRTC_VLINE_CRNT_VLINE) >> 16) & 0x3ff) != 0)
488                         break;
489
490         for(i=0; i<1000000; i++)
491                 if (((INREG(CRTC_VLINE_CRNT_VLINE) >> 16) & 0x3ff) == 0)
492                         break;
493         
494         do_gettimeofday(&stop_tv);
495         
496         local_irq_enable();
497
498         total_secs = stop_tv.tv_sec - start_tv.tv_sec;
499         if (total_secs > 10)
500                 return -1;
501         total_usecs = stop_tv.tv_usec - start_tv.tv_usec;
502         total_usecs += total_secs * 1000000;
503         if (total_usecs < 0)
504                 total_usecs = -total_usecs;
505         hz = 1000000/total_usecs;
506  
507         hTotal = ((INREG(CRTC_H_TOTAL_DISP) & 0x1ff) + 1) * 8;
508         vTotal = ((INREG(CRTC_V_TOTAL_DISP) & 0x3ff) + 1);
509         vclk = (long long)hTotal * (long long)vTotal * hz;
510
511         switch((INPLL(PPLL_REF_DIV) & 0x30000) >> 16) {
512         case 0:
513         default:
514                 num = 1;
515                 denom = 1;
516                 break;
517         case 1:
518                 n = ((INPLL(X_MPLL_REF_FB_DIV) >> 16) & 0xff);
519                 m = (INPLL(X_MPLL_REF_FB_DIV) & 0xff);
520                 num = 2*n;
521                 denom = 2*m;
522                 break;
523         case 2:
524                 n = ((INPLL(X_MPLL_REF_FB_DIV) >> 8) & 0xff);
525                 m = (INPLL(X_MPLL_REF_FB_DIV) & 0xff);
526                 num = 2*n;
527                 denom = 2*m;
528         break;
529         }
530
531         ppll_div_sel = INREG(CLOCK_CNTL_INDEX + 1) & 0x3;
532
533         n = (INPLL(PPLL_DIV_0 + ppll_div_sel) & 0x7ff);
534         m = (INPLL(PPLL_REF_DIV) & 0x3ff);
535
536         num *= n;
537         denom *= m;
538
539         switch ((INPLL(PPLL_DIV_0 + ppll_div_sel) >> 16) & 0x7) {
540         case 1:
541                 denom *= 2;
542                 break;
543         case 2:
544                 denom *= 4;
545                 break;
546         case 3:
547                 denom *= 8;
548                 break;
549         case 4:
550                 denom *= 3;
551                 break;
552         case 6:
553                 denom *= 6;   
554                 break;
555         case 7:
556                 denom *= 12;
557                 break;
558         }
559
560         vclk *= denom;
561         do_div(vclk, 1000 * num);
562         xtal = vclk;
563
564         if ((xtal > 26900) && (xtal < 27100))
565                 xtal = 2700;
566         else if ((xtal > 14200) && (xtal < 14400))
567                 xtal = 1432;
568         else if ((xtal > 29400) && (xtal < 29600))
569                 xtal = 2950;
570         else {
571                 printk(KERN_WARNING "xtal calculation failed: %ld\n", xtal);
572                 return -1;
573         }
574
575         tmp = INPLL(X_MPLL_REF_FB_DIV);
576         ref_div = INPLL(PPLL_REF_DIV) & 0x3ff;
577
578         Ns = (tmp & 0xff0000) >> 16;
579         Nm = (tmp & 0xff00) >> 8;
580         M = (tmp & 0xff);
581         sclk = round_div((2 * Ns * xtal), (2 * M));
582         mclk = round_div((2 * Nm * xtal), (2 * M));
583
584         /* we're done, hopefully these are sane values */
585         rinfo->pll.ref_clk = xtal;
586         rinfo->pll.ref_div = ref_div;
587         rinfo->pll.sclk = sclk;
588         rinfo->pll.mclk = mclk;
589
590         return 0;
591 }
592
593 /*
594  * Retreive PLL infos by different means (BIOS, Open Firmware, register probing...)
595  */
596 static void __devinit radeon_get_pllinfo(struct radeonfb_info *rinfo)
597 {
598         /*
599          * In the case nothing works, these are defaults; they are mostly
600          * incomplete, however.  It does provide ppll_max and _min values
601          * even for most other methods, however.
602          */
603         switch (rinfo->chipset) {
604         case PCI_DEVICE_ID_ATI_RADEON_QW:
605         case PCI_DEVICE_ID_ATI_RADEON_QX:
606                 rinfo->pll.ppll_max = 35000;
607                 rinfo->pll.ppll_min = 12000;
608                 rinfo->pll.mclk = 23000;
609                 rinfo->pll.sclk = 23000;
610                 rinfo->pll.ref_clk = 2700;
611                 break;
612         case PCI_DEVICE_ID_ATI_RADEON_QL:
613         case PCI_DEVICE_ID_ATI_RADEON_QN:
614         case PCI_DEVICE_ID_ATI_RADEON_QO:
615         case PCI_DEVICE_ID_ATI_RADEON_Ql:
616         case PCI_DEVICE_ID_ATI_RADEON_BB:
617                 rinfo->pll.ppll_max = 35000;
618                 rinfo->pll.ppll_min = 12000;
619                 rinfo->pll.mclk = 27500;
620                 rinfo->pll.sclk = 27500;
621                 rinfo->pll.ref_clk = 2700;
622                 break;
623         case PCI_DEVICE_ID_ATI_RADEON_Id:
624         case PCI_DEVICE_ID_ATI_RADEON_Ie:
625         case PCI_DEVICE_ID_ATI_RADEON_If:
626         case PCI_DEVICE_ID_ATI_RADEON_Ig:
627                 rinfo->pll.ppll_max = 35000;
628                 rinfo->pll.ppll_min = 12000;
629                 rinfo->pll.mclk = 25000;
630                 rinfo->pll.sclk = 25000;
631                 rinfo->pll.ref_clk = 2700;
632                 break;
633         case PCI_DEVICE_ID_ATI_RADEON_ND:
634         case PCI_DEVICE_ID_ATI_RADEON_NE:
635         case PCI_DEVICE_ID_ATI_RADEON_NF:
636         case PCI_DEVICE_ID_ATI_RADEON_NG:
637                 rinfo->pll.ppll_max = 40000;
638                 rinfo->pll.ppll_min = 20000;
639                 rinfo->pll.mclk = 27000;
640                 rinfo->pll.sclk = 27000;
641                 rinfo->pll.ref_clk = 2700;
642                 break;
643         case PCI_DEVICE_ID_ATI_RADEON_QD:
644         case PCI_DEVICE_ID_ATI_RADEON_QE:
645         case PCI_DEVICE_ID_ATI_RADEON_QF:
646         case PCI_DEVICE_ID_ATI_RADEON_QG:
647         default:
648                 rinfo->pll.ppll_max = 35000;
649                 rinfo->pll.ppll_min = 12000;
650                 rinfo->pll.mclk = 16600;
651                 rinfo->pll.sclk = 16600;
652                 rinfo->pll.ref_clk = 2700;
653                 break;
654         }
655         rinfo->pll.ref_div = INPLL(PPLL_REF_DIV) & 0x3ff;
656
657
658 #ifdef CONFIG_PPC_OF
659         /*
660          * Retreive PLL infos from Open Firmware first
661          */
662         if (!force_measure_pll && radeon_read_xtal_OF(rinfo) == 0) {
663                 printk(KERN_INFO "radeonfb: Retreived PLL infos from Open Firmware\n");
664                 goto found;
665         }
666 #endif /* CONFIG_PPC_OF */
667
668         /*
669          * Check out if we have an X86 which gave us some PLL informations
670          * and if yes, retreive them
671          */
672         if (!force_measure_pll && rinfo->bios_seg) {
673                 u16 pll_info_block = BIOS_IN16(rinfo->fp_bios_start + 0x30);
674
675                 rinfo->pll.sclk         = BIOS_IN16(pll_info_block + 0x08);
676                 rinfo->pll.mclk         = BIOS_IN16(pll_info_block + 0x0a);
677                 rinfo->pll.ref_clk      = BIOS_IN16(pll_info_block + 0x0e);
678                 rinfo->pll.ref_div      = BIOS_IN16(pll_info_block + 0x10);
679                 rinfo->pll.ppll_min     = BIOS_IN32(pll_info_block + 0x12);
680                 rinfo->pll.ppll_max     = BIOS_IN32(pll_info_block + 0x16);
681
682                 printk(KERN_INFO "radeonfb: Retreived PLL infos from BIOS\n");
683                 goto found;
684         }
685
686         /*
687          * We didn't get PLL parameters from either OF or BIOS, we try to
688          * probe them
689          */
690         if (radeon_probe_pll_params(rinfo) == 0) {
691                 printk(KERN_INFO "radeonfb: Retreived PLL infos from registers\n");
692                 goto found;
693         }
694
695         /*
696          * Fall back to already-set defaults...
697          */
698         printk(KERN_INFO "radeonfb: Used default PLL infos\n");
699
700 found:
701         /*
702          * Some methods fail to retreive SCLK and MCLK values, we apply default
703          * settings in this case (200Mhz). If that really happne often, we could
704          * fetch from registers instead...
705          */
706         if (rinfo->pll.mclk == 0)
707                 rinfo->pll.mclk = 20000;
708         if (rinfo->pll.sclk == 0)
709                 rinfo->pll.sclk = 20000;
710
711         printk("radeonfb: Reference=%d.%02d MHz (RefDiv=%d) Memory=%d.%02d Mhz, System=%d.%02d MHz\n",
712                rinfo->pll.ref_clk / 100, rinfo->pll.ref_clk % 100,
713                rinfo->pll.ref_div,
714                rinfo->pll.mclk / 100, rinfo->pll.mclk % 100,
715                rinfo->pll.sclk / 100, rinfo->pll.sclk % 100);
716         printk("radeonfb: PLL min %d max %d\n", rinfo->pll.ppll_min, rinfo->pll.ppll_max);
717 }
718
719 static int radeonfb_check_var (struct fb_var_screeninfo *var, struct fb_info *info)
720 {
721         struct radeonfb_info *rinfo = info->par;
722         struct fb_var_screeninfo v;
723         int nom, den;
724         unsigned int pitch;
725
726         if (radeon_match_mode(rinfo, &v, var))
727                 return -EINVAL;
728
729         switch (v.bits_per_pixel) {
730                 case 0 ... 8:
731                         v.bits_per_pixel = 8;
732                         break;
733                 case 9 ... 16:
734                         v.bits_per_pixel = 16;
735                         break;
736                 case 17 ... 24:
737 #if 0 /* Doesn't seem to work */
738                         v.bits_per_pixel = 24;
739                         break;
740 #endif                  
741                         return -EINVAL;
742                 case 25 ... 32:
743                         v.bits_per_pixel = 32;
744                         break;
745                 default:
746                         return -EINVAL;
747         }
748
749         switch (var_to_depth(&v)) {
750                 case 8:
751                         nom = den = 1;
752                         v.red.offset = v.green.offset = v.blue.offset = 0;
753                         v.red.length = v.green.length = v.blue.length = 8;
754                         v.transp.offset = v.transp.length = 0;
755                         break;
756                 case 15:
757                         nom = 2;
758                         den = 1;
759                         v.red.offset = 10;
760                         v.green.offset = 5;
761                         v.blue.offset = 0;
762                         v.red.length = v.green.length = v.blue.length = 5;
763                         v.transp.offset = v.transp.length = 0;
764                         break;
765                 case 16:
766                         nom = 2;
767                         den = 1;
768                         v.red.offset = 11;
769                         v.green.offset = 5;
770                         v.blue.offset = 0;
771                         v.red.length = 5;
772                         v.green.length = 6;
773                         v.blue.length = 5;
774                         v.transp.offset = v.transp.length = 0;
775                         break;                          
776                 case 24:
777                         nom = 4;
778                         den = 1;
779                         v.red.offset = 16;
780                         v.green.offset = 8;
781                         v.blue.offset = 0;
782                         v.red.length = v.blue.length = v.green.length = 8;
783                         v.transp.offset = v.transp.length = 0;
784                         break;
785                 case 32:
786                         nom = 4;
787                         den = 1;
788                         v.red.offset = 16;
789                         v.green.offset = 8;
790                         v.blue.offset = 0;
791                         v.red.length = v.blue.length = v.green.length = 8;
792                         v.transp.offset = 24;
793                         v.transp.length = 8;
794                         break;
795                 default:
796                         printk ("radeonfb: mode %dx%dx%d rejected, color depth invalid\n",
797                                 var->xres, var->yres, var->bits_per_pixel);
798                         return -EINVAL;
799         }
800
801         if (v.yres_virtual < v.yres)
802                 v.yres_virtual = v.yres;
803         if (v.xres_virtual < v.xres)
804                 v.xres_virtual = v.xres;
805                 
806
807         /* XXX I'm adjusting xres_virtual to the pitch, that may help XFree
808          * with some panels, though I don't quite like this solution
809          */
810         if (rinfo->info->flags & FBINFO_HWACCEL_DISABLED) {
811                 v.xres_virtual = v.xres_virtual & ~7ul;
812         } else {
813                 pitch = ((v.xres_virtual * ((v.bits_per_pixel + 1) / 8) + 0x3f)
814                                 & ~(0x3f)) >> 6;
815                 v.xres_virtual = (pitch << 6) / ((v.bits_per_pixel + 1) / 8);
816         }
817
818         if (((v.xres_virtual * v.yres_virtual * nom) / den) > rinfo->mapped_vram)
819                 return -EINVAL;
820
821         if (v.xres_virtual < v.xres)
822                 v.xres = v.xres_virtual;
823
824         if (v.xoffset < 0)
825                 v.xoffset = 0;
826         if (v.yoffset < 0)
827                 v.yoffset = 0;
828          
829         if (v.xoffset > v.xres_virtual - v.xres)
830                 v.xoffset = v.xres_virtual - v.xres - 1;
831                         
832         if (v.yoffset > v.yres_virtual - v.yres)
833                 v.yoffset = v.yres_virtual - v.yres - 1;
834          
835         v.red.msb_right = v.green.msb_right = v.blue.msb_right =
836                           v.transp.offset = v.transp.length =
837                           v.transp.msb_right = 0;
838         
839         memcpy(var, &v, sizeof(v));
840
841         return 0;
842 }
843
844
845 static int radeonfb_pan_display (struct fb_var_screeninfo *var,
846                                  struct fb_info *info)
847 {
848         struct radeonfb_info *rinfo = info->par;
849
850         if ((var->xoffset + var->xres > var->xres_virtual)
851             || (var->yoffset + var->yres > var->yres_virtual))
852                return -EINVAL;
853                 
854         if (rinfo->asleep)
855                 return 0;
856
857         radeon_fifo_wait(2);
858         OUTREG(CRTC_OFFSET, ((var->yoffset * var->xres_virtual + var->xoffset)
859                              * var->bits_per_pixel / 8) & ~7);
860         return 0;
861 }
862
863
864 static int radeonfb_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
865                            unsigned long arg, struct fb_info *info)
866 {
867         struct radeonfb_info *rinfo = info->par;
868         unsigned int tmp;
869         u32 value = 0;
870         int rc;
871
872         switch (cmd) {
873                 /*
874                  * TODO:  set mirror accordingly for non-Mobility chipsets with 2 CRTC's
875                  *        and do something better using 2nd CRTC instead of just hackish
876                  *        routing to second output
877                  */
878                 case FBIO_RADEON_SET_MIRROR:
879                         if (!rinfo->is_mobility)
880                                 return -EINVAL;
881
882                         rc = get_user(value, (__u32 __user *)arg);
883
884                         if (rc)
885                                 return rc;
886
887                         radeon_fifo_wait(2);
888                         if (value & 0x01) {
889                                 tmp = INREG(LVDS_GEN_CNTL);
890
891                                 tmp |= (LVDS_ON | LVDS_BLON);
892                         } else {
893                                 tmp = INREG(LVDS_GEN_CNTL);
894
895                                 tmp &= ~(LVDS_ON | LVDS_BLON);
896                         }
897
898                         OUTREG(LVDS_GEN_CNTL, tmp);
899
900                         if (value & 0x02) {
901                                 tmp = INREG(CRTC_EXT_CNTL);
902                                 tmp |= CRTC_CRT_ON;
903
904                                 mirror = 1;
905                         } else {
906                                 tmp = INREG(CRTC_EXT_CNTL);
907                                 tmp &= ~CRTC_CRT_ON;
908
909                                 mirror = 0;
910                         }
911
912                         OUTREG(CRTC_EXT_CNTL, tmp);
913
914                         break;
915                 case FBIO_RADEON_GET_MIRROR:
916                         if (!rinfo->is_mobility)
917                                 return -EINVAL;
918
919                         tmp = INREG(LVDS_GEN_CNTL);
920                         if ((LVDS_ON | LVDS_BLON) & tmp)
921                                 value |= 0x01;
922
923                         tmp = INREG(CRTC_EXT_CNTL);
924                         if (CRTC_CRT_ON & tmp)
925                                 value |= 0x02;
926
927                         return put_user(value, (__u32 __user *)arg);
928                 default:
929                         return -EINVAL;
930         }
931
932         return -EINVAL;
933 }
934
935
936 static int radeon_screen_blank (struct radeonfb_info *rinfo, int blank, int mode_switch)
937 {
938         u32 val;
939         u32 tmp_pix_clks;
940
941         if (rinfo->lock_blank)
942                 return 0;
943
944         radeon_engine_idle();
945
946         val = INREG(CRTC_EXT_CNTL);
947         val &= ~(CRTC_DISPLAY_DIS | CRTC_HSYNC_DIS |
948                  CRTC_VSYNC_DIS);
949         switch (blank) {
950         case FB_BLANK_UNBLANK:
951         case FB_BLANK_NORMAL:
952                 break;
953         case FB_BLANK_VSYNC_SUSPEND:
954                 val |= (CRTC_DISPLAY_DIS | CRTC_VSYNC_DIS);
955                 break;
956         case FB_BLANK_HSYNC_SUSPEND:
957                 val |= (CRTC_DISPLAY_DIS | CRTC_HSYNC_DIS);
958                 break;
959         case FB_BLANK_POWERDOWN:
960                 val |= (CRTC_DISPLAY_DIS | CRTC_VSYNC_DIS |
961                         CRTC_HSYNC_DIS);
962                 break;
963         }
964         OUTREG(CRTC_EXT_CNTL, val);
965
966
967         switch (rinfo->mon1_type) {
968         case MT_DFP:
969                 if (mode_switch)
970                         break;
971                 if (blank == FB_BLANK_UNBLANK ||
972                     blank == FB_BLANK_NORMAL)
973                         OUTREGP(FP_GEN_CNTL, (FP_FPON | FP_TMDS_EN),
974                                 ~(FP_FPON | FP_TMDS_EN));
975                 else
976                         OUTREGP(FP_GEN_CNTL, 0, ~(FP_FPON | FP_TMDS_EN));
977                 break;
978         case MT_LCD:
979                 val = INREG(LVDS_GEN_CNTL);
980                 if (blank == FB_BLANK_UNBLANK ||
981                     blank == FB_BLANK_NORMAL) {
982                         u32 target_val = (val & ~LVDS_DISPLAY_DIS) | LVDS_BLON | LVDS_ON
983                                 | LVDS_ON | (rinfo->init_state.lvds_gen_cntl & LVDS_DIGON);
984                         if ((val ^ target_val) == LVDS_DISPLAY_DIS)
985                                 OUTREG(LVDS_GEN_CNTL, target_val);
986                         else if ((val ^ target_val) != 0) {
987                                 del_timer_sync(&rinfo->lvds_timer);
988                                 OUTREG(LVDS_GEN_CNTL, target_val & ~LVDS_ON);
989                                 rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
990                                 rinfo->init_state.lvds_gen_cntl |= target_val & LVDS_STATE_MASK;
991                                 if (mode_switch) {
992                                         msleep(rinfo->panel_info.pwr_delay);
993                                         OUTREG(LVDS_GEN_CNTL, target_val);
994                                 }
995                                 else {
996                                         rinfo->pending_lvds_gen_cntl = target_val;
997                                         mod_timer(&rinfo->lvds_timer,
998                                                 jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay));
999                                 }
1000                         }
1001                 } else {
1002                         val |= LVDS_DISPLAY_DIS;
1003                         OUTREG(LVDS_GEN_CNTL, val);
1004
1005                         /* We don't do a full switch-off on a simple mode switch */
1006                         if (mode_switch)
1007                                 break;
1008
1009                         /* Asic bug, when turning off LVDS_ON, we have to make sure
1010                          * RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
1011                          */
1012                         tmp_pix_clks = INPLL(PIXCLKS_CNTL);
1013                         if (rinfo->is_mobility || rinfo->is_IGP)
1014                                 OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb);
1015                         val &= ~(LVDS_BLON | LVDS_ON);
1016                         OUTREG(LVDS_GEN_CNTL, val);
1017                         rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
1018                         rinfo->init_state.lvds_gen_cntl |= val & LVDS_STATE_MASK;
1019                         if (rinfo->is_mobility || rinfo->is_IGP)
1020                                 OUTPLL(PIXCLKS_CNTL, tmp_pix_clks);
1021                 }
1022                 break;
1023         case MT_CRT:
1024                 // todo: powerdown DAC
1025         default:
1026                 break;
1027         }
1028
1029         /* let fbcon do a soft blank for us */
1030         return (blank == FB_BLANK_NORMAL) ? -EINVAL : 0;
1031 }
1032
1033 int radeonfb_blank (int blank, struct fb_info *info)
1034 {
1035         struct radeonfb_info *rinfo = info->par;
1036
1037         if (rinfo->asleep)
1038                 return 0;
1039                 
1040         radeon_screen_blank(rinfo, blank, 0);
1041
1042         return 0;
1043 }
1044
1045 static int radeonfb_setcolreg (unsigned regno, unsigned red, unsigned green,
1046                              unsigned blue, unsigned transp, struct fb_info *info)
1047 {
1048         struct radeonfb_info *rinfo = info->par;
1049         u32 pindex;
1050         unsigned int i;
1051         
1052         if (regno > 255)
1053                 return 1;
1054
1055         red >>= 8;
1056         green >>= 8;
1057         blue >>= 8;
1058         rinfo->palette[regno].red = red;
1059         rinfo->palette[regno].green = green;
1060         rinfo->palette[regno].blue = blue;
1061
1062         /* default */
1063         pindex = regno;
1064
1065         if (!rinfo->asleep) {
1066                 u32 dac_cntl2, vclk_cntl = 0;
1067                 
1068                 radeon_fifo_wait(9);
1069                 if (rinfo->is_mobility) {
1070                         vclk_cntl = INPLL(VCLK_ECP_CNTL);
1071                         OUTPLL(VCLK_ECP_CNTL, vclk_cntl & ~PIXCLK_DAC_ALWAYS_ONb);
1072                 }
1073
1074                 /* Make sure we are on first palette */
1075                 if (rinfo->has_CRTC2) {
1076                         dac_cntl2 = INREG(DAC_CNTL2);
1077                         dac_cntl2 &= ~DAC2_PALETTE_ACCESS_CNTL;
1078                         OUTREG(DAC_CNTL2, dac_cntl2);
1079                 }
1080
1081                 if (rinfo->bpp == 16) {
1082                         pindex = regno * 8;
1083
1084                         if (rinfo->depth == 16 && regno > 63)
1085                                 return 1;
1086                         if (rinfo->depth == 15 && regno > 31)
1087                                 return 1;
1088
1089                         /* For 565, the green component is mixed one order below */
1090                         if (rinfo->depth == 16) {
1091                                 OUTREG(PALETTE_INDEX, pindex>>1);
1092                                 OUTREG(PALETTE_DATA, (rinfo->palette[regno>>1].red << 16) |
1093                                         (green << 8) | (rinfo->palette[regno>>1].blue));
1094                                 green = rinfo->palette[regno<<1].green;
1095                         }
1096                 }
1097
1098                 if (rinfo->depth != 16 || regno < 32) {
1099                         OUTREG(PALETTE_INDEX, pindex);
1100                         OUTREG(PALETTE_DATA, (red << 16) | (green << 8) | blue);
1101                 }
1102                 if (rinfo->is_mobility)
1103                         OUTPLL(VCLK_ECP_CNTL, vclk_cntl);
1104         }
1105         if (regno < 16) {
1106                 u32 *pal = info->pseudo_palette;
1107                 switch (rinfo->depth) {
1108                 case 15:
1109                         pal[regno] = (regno << 10) | (regno << 5) | regno;
1110                         break;
1111                 case 16:
1112                         pal[regno] = (regno << 11) | (regno << 5) | regno;
1113                         break;
1114                 case 24:
1115                         pal[regno] = (regno << 16) | (regno << 8) | regno;
1116                         break;
1117                 case 32:
1118                         i = (regno << 8) | regno;
1119                         pal[regno] = (i << 16) | i;
1120                         break;
1121                 }
1122         }
1123         return 0;
1124 }
1125
1126
1127 static void radeon_save_state (struct radeonfb_info *rinfo, struct radeon_regs *save)
1128 {
1129         /* CRTC regs */
1130         save->crtc_gen_cntl = INREG(CRTC_GEN_CNTL);
1131         save->crtc_ext_cntl = INREG(CRTC_EXT_CNTL);
1132         save->crtc_more_cntl = INREG(CRTC_MORE_CNTL);
1133         save->dac_cntl = INREG(DAC_CNTL);
1134         save->crtc_h_total_disp = INREG(CRTC_H_TOTAL_DISP);
1135         save->crtc_h_sync_strt_wid = INREG(CRTC_H_SYNC_STRT_WID);
1136         save->crtc_v_total_disp = INREG(CRTC_V_TOTAL_DISP);
1137         save->crtc_v_sync_strt_wid = INREG(CRTC_V_SYNC_STRT_WID);
1138         save->crtc_pitch = INREG(CRTC_PITCH);
1139         save->surface_cntl = INREG(SURFACE_CNTL);
1140
1141         /* FP regs */
1142         save->fp_crtc_h_total_disp = INREG(FP_CRTC_H_TOTAL_DISP);
1143         save->fp_crtc_v_total_disp = INREG(FP_CRTC_V_TOTAL_DISP);
1144         save->fp_gen_cntl = INREG(FP_GEN_CNTL);
1145         save->fp_h_sync_strt_wid = INREG(FP_H_SYNC_STRT_WID);
1146         save->fp_horz_stretch = INREG(FP_HORZ_STRETCH);
1147         save->fp_v_sync_strt_wid = INREG(FP_V_SYNC_STRT_WID);
1148         save->fp_vert_stretch = INREG(FP_VERT_STRETCH);
1149         save->lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
1150         save->lvds_pll_cntl = INREG(LVDS_PLL_CNTL);
1151         save->tmds_crc = INREG(TMDS_CRC);       save->tmds_transmitter_cntl = INREG(TMDS_TRANSMITTER_CNTL);
1152         save->vclk_ecp_cntl = INPLL(VCLK_ECP_CNTL);
1153 }
1154
1155
1156 static void radeon_write_pll_regs(struct radeonfb_info *rinfo, struct radeon_regs *mode)
1157 {
1158         int i;
1159
1160         radeon_fifo_wait(20);
1161
1162         /* Workaround from XFree */
1163         if (rinfo->is_mobility) {
1164                 /* A temporal workaround for the occational blanking on certain laptop panels. 
1165                    This appears to related to the PLL divider registers (fail to lock?).  
1166                    It occurs even when all dividers are the same with their old settings.  
1167                    In this case we really don't need to fiddle with PLL registers. 
1168                    By doing this we can avoid the blanking problem with some panels.
1169                 */
1170                 if ((mode->ppll_ref_div == (INPLL(PPLL_REF_DIV) & PPLL_REF_DIV_MASK)) &&
1171                     (mode->ppll_div_3 == (INPLL(PPLL_DIV_3) &
1172                                           (PPLL_POST3_DIV_MASK | PPLL_FB3_DIV_MASK)))) {
1173                         /* We still have to force a switch to PPLL div 3 thanks to
1174                          * an XFree86 driver bug which will switch it away in some cases
1175                          * even when using UseFDev */
1176                         OUTREGP(CLOCK_CNTL_INDEX, PPLL_DIV_SEL_MASK, ~PPLL_DIV_SEL_MASK);
1177                         return;
1178                 }
1179         }
1180
1181         /* Swich VCKL clock input to CPUCLK so it stays fed while PPLL updates*/
1182         OUTPLLP(VCLK_ECP_CNTL, VCLK_SRC_SEL_CPUCLK, ~VCLK_SRC_SEL_MASK);
1183
1184         /* Reset PPLL & enable atomic update */
1185         OUTPLLP(PPLL_CNTL,
1186                 PPLL_RESET | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN,
1187                 ~(PPLL_RESET | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN));
1188
1189         /* Switch to PPLL div 3 */
1190         OUTREGP(CLOCK_CNTL_INDEX, PPLL_DIV_SEL_MASK, ~PPLL_DIV_SEL_MASK);
1191
1192         /* Set PPLL ref. div */
1193         if (rinfo->family == CHIP_FAMILY_R300 ||
1194             rinfo->family == CHIP_FAMILY_RS300 ||
1195             rinfo->family == CHIP_FAMILY_R350 ||
1196             rinfo->family == CHIP_FAMILY_RV350) {
1197                 if (mode->ppll_ref_div & R300_PPLL_REF_DIV_ACC_MASK) {
1198                         /* When restoring console mode, use saved PPLL_REF_DIV
1199                          * setting.
1200                          */
1201                         OUTPLLP(PPLL_REF_DIV, mode->ppll_ref_div, 0);
1202                 } else {
1203                         /* R300 uses ref_div_acc field as real ref divider */
1204                         OUTPLLP(PPLL_REF_DIV,
1205                                 (mode->ppll_ref_div << R300_PPLL_REF_DIV_ACC_SHIFT), 
1206                                 ~R300_PPLL_REF_DIV_ACC_MASK);
1207                 }
1208         } else
1209                 OUTPLLP(PPLL_REF_DIV, mode->ppll_ref_div, ~PPLL_REF_DIV_MASK);
1210
1211         /* Set PPLL divider 3 & post divider*/
1212         OUTPLLP(PPLL_DIV_3, mode->ppll_div_3, ~PPLL_FB3_DIV_MASK);
1213         OUTPLLP(PPLL_DIV_3, mode->ppll_div_3, ~PPLL_POST3_DIV_MASK);
1214
1215         /* Write update */
1216         while (INPLL(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R)
1217                 ;
1218         OUTPLLP(PPLL_REF_DIV, PPLL_ATOMIC_UPDATE_W, ~PPLL_ATOMIC_UPDATE_W);
1219
1220         /* Wait read update complete */
1221         /* FIXME: Certain revisions of R300 can't recover here.  Not sure of
1222            the cause yet, but this workaround will mask the problem for now.
1223            Other chips usually will pass at the very first test, so the
1224            workaround shouldn't have any effect on them. */
1225         for (i = 0; (i < 10000 && INPLL(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R); i++)
1226                 ;
1227         
1228         OUTPLL(HTOTAL_CNTL, 0);
1229
1230         /* Clear reset & atomic update */
1231         OUTPLLP(PPLL_CNTL, 0,
1232                 ~(PPLL_RESET | PPLL_SLEEP | PPLL_ATOMIC_UPDATE_EN | PPLL_VGA_ATOMIC_UPDATE_EN));
1233
1234         /* We may want some locking ... oh well */
1235         msleep(5);
1236
1237         /* Switch back VCLK source to PPLL */
1238         OUTPLLP(VCLK_ECP_CNTL, VCLK_SRC_SEL_PPLLCLK, ~VCLK_SRC_SEL_MASK);
1239 }
1240
1241 /*
1242  * Timer function for delayed LVDS panel power up/down
1243  */
1244 static void radeon_lvds_timer_func(unsigned long data)
1245 {
1246         struct radeonfb_info *rinfo = (struct radeonfb_info *)data;
1247
1248         radeon_fifo_wait(3);
1249
1250         OUTREG(LVDS_GEN_CNTL, rinfo->pending_lvds_gen_cntl);
1251         if (rinfo->pending_pixclks_cntl) {
1252                 OUTPLL(PIXCLKS_CNTL, rinfo->pending_pixclks_cntl);
1253                 rinfo->pending_pixclks_cntl = 0;
1254         }
1255 }
1256
1257 /*
1258  * Apply a video mode. This will apply the whole register set, including
1259  * the PLL registers, to the card
1260  */
1261 static void radeon_write_mode (struct radeonfb_info *rinfo,
1262                                struct radeon_regs *mode)
1263 {
1264         int i;
1265         int primary_mon = PRIMARY_MONITOR(rinfo);
1266
1267         if (nomodeset)
1268                 return;
1269
1270         del_timer_sync(&rinfo->lvds_timer);
1271
1272         radeon_screen_blank(rinfo, FB_BLANK_POWERDOWN, 1);
1273         msleep(100);
1274
1275         radeon_fifo_wait(31);
1276         for (i=0; i<10; i++)
1277                 OUTREG(common_regs[i].reg, common_regs[i].val);
1278
1279         /* Apply surface registers */
1280         for (i=0; i<8; i++) {
1281                 OUTREG(SURFACE0_LOWER_BOUND + 0x10*i, mode->surf_lower_bound[i]);
1282                 OUTREG(SURFACE0_UPPER_BOUND + 0x10*i, mode->surf_upper_bound[i]);
1283                 OUTREG(SURFACE0_INFO + 0x10*i, mode->surf_info[i]);
1284         }
1285
1286         OUTREG(CRTC_GEN_CNTL, mode->crtc_gen_cntl);
1287         OUTREGP(CRTC_EXT_CNTL, mode->crtc_ext_cntl,
1288                 ~(CRTC_HSYNC_DIS | CRTC_VSYNC_DIS | CRTC_DISPLAY_DIS));
1289         OUTREG(CRTC_MORE_CNTL, mode->crtc_more_cntl);
1290         OUTREGP(DAC_CNTL, mode->dac_cntl, DAC_RANGE_CNTL | DAC_BLANKING);
1291         OUTREG(CRTC_H_TOTAL_DISP, mode->crtc_h_total_disp);
1292         OUTREG(CRTC_H_SYNC_STRT_WID, mode->crtc_h_sync_strt_wid);
1293         OUTREG(CRTC_V_TOTAL_DISP, mode->crtc_v_total_disp);
1294         OUTREG(CRTC_V_SYNC_STRT_WID, mode->crtc_v_sync_strt_wid);
1295         OUTREG(CRTC_OFFSET, 0);
1296         OUTREG(CRTC_OFFSET_CNTL, 0);
1297         OUTREG(CRTC_PITCH, mode->crtc_pitch);
1298         OUTREG(SURFACE_CNTL, mode->surface_cntl);
1299
1300         radeon_write_pll_regs(rinfo, mode);
1301
1302         if ((primary_mon == MT_DFP) || (primary_mon == MT_LCD)) {
1303                 radeon_fifo_wait(10);
1304                 OUTREG(FP_CRTC_H_TOTAL_DISP, mode->fp_crtc_h_total_disp);
1305                 OUTREG(FP_CRTC_V_TOTAL_DISP, mode->fp_crtc_v_total_disp);
1306                 OUTREG(FP_H_SYNC_STRT_WID, mode->fp_h_sync_strt_wid);
1307                 OUTREG(FP_V_SYNC_STRT_WID, mode->fp_v_sync_strt_wid);
1308                 OUTREG(FP_HORZ_STRETCH, mode->fp_horz_stretch);
1309                 OUTREG(FP_VERT_STRETCH, mode->fp_vert_stretch);
1310                 OUTREG(FP_GEN_CNTL, mode->fp_gen_cntl);
1311                 OUTREG(TMDS_CRC, mode->tmds_crc);
1312                 OUTREG(TMDS_TRANSMITTER_CNTL, mode->tmds_transmitter_cntl);
1313         }
1314
1315         radeon_screen_blank(rinfo, FB_BLANK_UNBLANK, 1);
1316
1317         radeon_fifo_wait(2);
1318         OUTPLL(VCLK_ECP_CNTL, mode->vclk_ecp_cntl);
1319         
1320         return;
1321 }
1322
1323 /*
1324  * Calculate the PLL values for a given mode
1325  */
1326 static void radeon_calc_pll_regs(struct radeonfb_info *rinfo, struct radeon_regs *regs,
1327                                  unsigned long freq)
1328 {
1329         const struct {
1330                 int divider;
1331                 int bitvalue;
1332         } *post_div,
1333           post_divs[] = {
1334                 { 1,  0 },
1335                 { 2,  1 },
1336                 { 4,  2 },
1337                 { 8,  3 },
1338                 { 3,  4 },
1339                 { 16, 5 },
1340                 { 6,  6 },
1341                 { 12, 7 },
1342                 { 0,  0 },
1343         };
1344         int fb_div, pll_output_freq = 0;
1345         int uses_dvo = 0;
1346
1347         /* Check if the DVO port is enabled and sourced from the primary CRTC. I'm
1348          * not sure which model starts having FP2_GEN_CNTL, I assume anything more
1349          * recent than an r(v)100...
1350          */
1351 #if 1
1352         /* XXX I had reports of flicker happening with the cinema display
1353          * on TMDS1 that seem to be fixed if I also forbit odd dividers in
1354          * this case. This could just be a bandwidth calculation issue, I
1355          * haven't implemented the bandwidth code yet, but in the meantime,
1356          * forcing uses_dvo to 1 fixes it and shouln't have bad side effects,
1357          * I haven't seen a case were were absolutely needed an odd PLL
1358          * divider. I'll find a better fix once I have more infos on the
1359          * real cause of the problem.
1360          */
1361         while (rinfo->has_CRTC2) {
1362                 u32 fp2_gen_cntl = INREG(FP2_GEN_CNTL);
1363                 u32 disp_output_cntl;
1364                 int source;
1365
1366                 /* FP2 path not enabled */
1367                 if ((fp2_gen_cntl & FP2_ON) == 0)
1368                         break;
1369                 /* Not all chip revs have the same format for this register,
1370                  * extract the source selection
1371                  */
1372                 if (rinfo->family == CHIP_FAMILY_R200 ||
1373                     rinfo->family == CHIP_FAMILY_R300 ||
1374                     rinfo->family == CHIP_FAMILY_R350 ||
1375                     rinfo->family == CHIP_FAMILY_RV350) {
1376                         source = (fp2_gen_cntl >> 10) & 0x3;
1377                         /* sourced from transform unit, check for transform unit
1378                          * own source
1379                          */
1380                         if (source == 3) {
1381                                 disp_output_cntl = INREG(DISP_OUTPUT_CNTL);
1382                                 source = (disp_output_cntl >> 12) & 0x3;
1383                         }
1384                 } else
1385                         source = (fp2_gen_cntl >> 13) & 0x1;
1386                 /* sourced from CRTC2 -> exit */
1387                 if (source == 1)
1388                         break;
1389
1390                 /* so we end up on CRTC1, let's set uses_dvo to 1 now */
1391                 uses_dvo = 1;
1392                 break;
1393         }
1394 #else
1395         uses_dvo = 1;
1396 #endif
1397         if (freq > rinfo->pll.ppll_max)
1398                 freq = rinfo->pll.ppll_max;
1399         if (freq*12 < rinfo->pll.ppll_min)
1400                 freq = rinfo->pll.ppll_min / 12;
1401         RTRACE("freq = %lu, PLL min = %u, PLL max = %u\n",
1402                freq, rinfo->pll.ppll_min, rinfo->pll.ppll_max);
1403
1404         for (post_div = &post_divs[0]; post_div->divider; ++post_div) {
1405                 pll_output_freq = post_div->divider * freq;
1406                 /* If we output to the DVO port (external TMDS), we don't allow an
1407                  * odd PLL divider as those aren't supported on this path
1408                  */
1409                 if (uses_dvo && (post_div->divider & 1))
1410                         continue;
1411                 if (pll_output_freq >= rinfo->pll.ppll_min  &&
1412                     pll_output_freq <= rinfo->pll.ppll_max)
1413                         break;
1414         }
1415
1416         /* If we fall through the bottom, try the "default value"
1417            given by the terminal post_div->bitvalue */
1418         if ( !post_div->divider ) {
1419                 post_div = &post_divs[post_div->bitvalue];
1420                 pll_output_freq = post_div->divider * freq;
1421         }
1422         RTRACE("ref_div = %d, ref_clk = %d, output_freq = %d\n",
1423                rinfo->pll.ref_div, rinfo->pll.ref_clk,
1424                pll_output_freq);
1425
1426         fb_div = round_div(rinfo->pll.ref_div*pll_output_freq,
1427                                   rinfo->pll.ref_clk);
1428         regs->ppll_ref_div = rinfo->pll.ref_div;
1429         regs->ppll_div_3 = fb_div | (post_div->bitvalue << 16);
1430
1431         RTRACE("post div = 0x%x\n", post_div->bitvalue);
1432         RTRACE("fb_div = 0x%x\n", fb_div);
1433         RTRACE("ppll_div_3 = 0x%x\n", regs->ppll_div_3);
1434 }
1435
1436 int radeonfb_set_par(struct fb_info *info)
1437 {
1438         struct radeonfb_info *rinfo = info->par;
1439         struct fb_var_screeninfo *mode = &info->var;
1440         struct radeon_regs *newmode;
1441         int hTotal, vTotal, hSyncStart, hSyncEnd,
1442             hSyncPol, vSyncStart, vSyncEnd, vSyncPol, cSync;
1443         u8 hsync_adj_tab[] = {0, 0x12, 9, 9, 6, 5};
1444         u8 hsync_fudge_fp[] = {2, 2, 0, 0, 5, 5};
1445         u32 sync, h_sync_pol, v_sync_pol, dotClock, pixClock;
1446         int i, freq;
1447         int format = 0;
1448         int nopllcalc = 0;
1449         int hsync_start, hsync_fudge, bytpp, hsync_wid, vsync_wid;
1450         int primary_mon = PRIMARY_MONITOR(rinfo);
1451         int depth = var_to_depth(mode);
1452
1453         newmode = kmalloc(sizeof(struct radeon_regs), GFP_KERNEL);
1454         if (!newmode)
1455                 return -ENOMEM;
1456
1457         /* We always want engine to be idle on a mode switch, even
1458          * if we won't actually change the mode
1459          */
1460         radeon_engine_idle();
1461
1462         hSyncStart = mode->xres + mode->right_margin;
1463         hSyncEnd = hSyncStart + mode->hsync_len;
1464         hTotal = hSyncEnd + mode->left_margin;
1465
1466         vSyncStart = mode->yres + mode->lower_margin;
1467         vSyncEnd = vSyncStart + mode->vsync_len;
1468         vTotal = vSyncEnd + mode->upper_margin;
1469         pixClock = mode->pixclock;
1470
1471         sync = mode->sync;
1472         h_sync_pol = sync & FB_SYNC_HOR_HIGH_ACT ? 0 : 1;
1473         v_sync_pol = sync & FB_SYNC_VERT_HIGH_ACT ? 0 : 1;
1474
1475         if (primary_mon == MT_DFP || primary_mon == MT_LCD) {
1476                 if (rinfo->panel_info.xres < mode->xres)
1477                         mode->xres = rinfo->panel_info.xres;
1478                 if (rinfo->panel_info.yres < mode->yres)
1479                         mode->yres = rinfo->panel_info.yres;
1480
1481                 hTotal = mode->xres + rinfo->panel_info.hblank;
1482                 hSyncStart = mode->xres + rinfo->panel_info.hOver_plus;
1483                 hSyncEnd = hSyncStart + rinfo->panel_info.hSync_width;
1484
1485                 vTotal = mode->yres + rinfo->panel_info.vblank;
1486                 vSyncStart = mode->yres + rinfo->panel_info.vOver_plus;
1487                 vSyncEnd = vSyncStart + rinfo->panel_info.vSync_width;
1488
1489                 h_sync_pol = !rinfo->panel_info.hAct_high;
1490                 v_sync_pol = !rinfo->panel_info.vAct_high;
1491
1492                 pixClock = 100000000 / rinfo->panel_info.clock;
1493
1494                 if (rinfo->panel_info.use_bios_dividers) {
1495                         nopllcalc = 1;
1496                         newmode->ppll_div_3 = rinfo->panel_info.fbk_divider |
1497                                 (rinfo->panel_info.post_divider << 16);
1498                         newmode->ppll_ref_div = rinfo->panel_info.ref_divider;
1499                 }
1500         }
1501         dotClock = 1000000000 / pixClock;
1502         freq = dotClock / 10; /* x100 */
1503
1504         RTRACE("hStart = %d, hEnd = %d, hTotal = %d\n",
1505                 hSyncStart, hSyncEnd, hTotal);
1506         RTRACE("vStart = %d, vEnd = %d, vTotal = %d\n",
1507                 vSyncStart, vSyncEnd, vTotal);
1508
1509         hsync_wid = (hSyncEnd - hSyncStart) / 8;
1510         vsync_wid = vSyncEnd - vSyncStart;
1511         if (hsync_wid == 0)
1512                 hsync_wid = 1;
1513         else if (hsync_wid > 0x3f)      /* max */
1514                 hsync_wid = 0x3f;
1515
1516         if (vsync_wid == 0)
1517                 vsync_wid = 1;
1518         else if (vsync_wid > 0x1f)      /* max */
1519                 vsync_wid = 0x1f;
1520
1521         hSyncPol = mode->sync & FB_SYNC_HOR_HIGH_ACT ? 0 : 1;
1522         vSyncPol = mode->sync & FB_SYNC_VERT_HIGH_ACT ? 0 : 1;
1523
1524         cSync = mode->sync & FB_SYNC_COMP_HIGH_ACT ? (1 << 4) : 0;
1525
1526         format = radeon_get_dstbpp(depth);
1527         bytpp = mode->bits_per_pixel >> 3;
1528
1529         if ((primary_mon == MT_DFP) || (primary_mon == MT_LCD))
1530                 hsync_fudge = hsync_fudge_fp[format-1];
1531         else
1532                 hsync_fudge = hsync_adj_tab[format-1];
1533
1534         hsync_start = hSyncStart - 8 + hsync_fudge;
1535
1536         newmode->crtc_gen_cntl = CRTC_EXT_DISP_EN | CRTC_EN |
1537                                 (format << 8);
1538
1539         /* Clear auto-center etc... */
1540         newmode->crtc_more_cntl = rinfo->init_state.crtc_more_cntl;
1541         newmode->crtc_more_cntl &= 0xfffffff0;
1542         
1543         if ((primary_mon == MT_DFP) || (primary_mon == MT_LCD)) {
1544                 newmode->crtc_ext_cntl = VGA_ATI_LINEAR | XCRT_CNT_EN;
1545                 if (mirror)
1546                         newmode->crtc_ext_cntl |= CRTC_CRT_ON;
1547
1548                 newmode->crtc_gen_cntl &= ~(CRTC_DBL_SCAN_EN |
1549                                            CRTC_INTERLACE_EN);
1550         } else {
1551                 newmode->crtc_ext_cntl = VGA_ATI_LINEAR | XCRT_CNT_EN |
1552                                         CRTC_CRT_ON;
1553         }
1554
1555         newmode->dac_cntl = /* INREG(DAC_CNTL) | */ DAC_MASK_ALL | DAC_VGA_ADR_EN |
1556                            DAC_8BIT_EN;
1557
1558         newmode->crtc_h_total_disp = ((((hTotal / 8) - 1) & 0x3ff) |
1559                                      (((mode->xres / 8) - 1) << 16));
1560
1561         newmode->crtc_h_sync_strt_wid = ((hsync_start & 0x1fff) |
1562                                         (hsync_wid << 16) | (h_sync_pol << 23));
1563
1564         newmode->crtc_v_total_disp = ((vTotal - 1) & 0xffff) |
1565                                     ((mode->yres - 1) << 16);
1566
1567         newmode->crtc_v_sync_strt_wid = (((vSyncStart - 1) & 0xfff) |
1568                                          (vsync_wid << 16) | (v_sync_pol  << 23));
1569
1570         if (!(info->flags & FBINFO_HWACCEL_DISABLED)) {
1571                 /* We first calculate the engine pitch */
1572                 rinfo->pitch = ((mode->xres_virtual * ((mode->bits_per_pixel + 1) / 8) + 0x3f)
1573                                 & ~(0x3f)) >> 6;
1574
1575                 /* Then, re-multiply it to get the CRTC pitch */
1576                 newmode->crtc_pitch = (rinfo->pitch << 3) / ((mode->bits_per_pixel + 1) / 8);
1577         } else
1578                 newmode->crtc_pitch = (mode->xres_virtual >> 3);
1579
1580         newmode->crtc_pitch |= (newmode->crtc_pitch << 16);
1581
1582         /*
1583          * It looks like recent chips have a problem with SURFACE_CNTL,
1584          * setting SURF_TRANSLATION_DIS completely disables the
1585          * swapper as well, so we leave it unset now.
1586          */
1587         newmode->surface_cntl = 0;
1588
1589 #if defined(__BIG_ENDIAN)
1590
1591         /* Setup swapping on both apertures, though we currently
1592          * only use aperture 0, enabling swapper on aperture 1
1593          * won't harm
1594          */
1595         switch (mode->bits_per_pixel) {
1596                 case 16:
1597                         newmode->surface_cntl |= NONSURF_AP0_SWP_16BPP;
1598                         newmode->surface_cntl |= NONSURF_AP1_SWP_16BPP;
1599                         break;
1600                 case 24:        
1601                 case 32:
1602                         newmode->surface_cntl |= NONSURF_AP0_SWP_32BPP;
1603                         newmode->surface_cntl |= NONSURF_AP1_SWP_32BPP;
1604                         break;
1605         }
1606 #endif
1607
1608         /* Clear surface registers */
1609         for (i=0; i<8; i++) {
1610                 newmode->surf_lower_bound[i] = 0;
1611                 newmode->surf_upper_bound[i] = 0x1f;
1612                 newmode->surf_info[i] = 0;
1613         }
1614
1615         RTRACE("h_total_disp = 0x%x\t   hsync_strt_wid = 0x%x\n",
1616                 newmode->crtc_h_total_disp, newmode->crtc_h_sync_strt_wid);
1617         RTRACE("v_total_disp = 0x%x\t   vsync_strt_wid = 0x%x\n",
1618                 newmode->crtc_v_total_disp, newmode->crtc_v_sync_strt_wid);
1619
1620         rinfo->bpp = mode->bits_per_pixel;
1621         rinfo->depth = depth;
1622
1623         RTRACE("pixclock = %lu\n", (unsigned long)pixClock);
1624         RTRACE("freq = %lu\n", (unsigned long)freq);
1625
1626         if (!nopllcalc)
1627                 radeon_calc_pll_regs(rinfo, newmode, freq);
1628
1629         newmode->vclk_ecp_cntl = rinfo->init_state.vclk_ecp_cntl;
1630
1631         if ((primary_mon == MT_DFP) || (primary_mon == MT_LCD)) {
1632                 unsigned int hRatio, vRatio;
1633
1634                 if (mode->xres > rinfo->panel_info.xres)
1635                         mode->xres = rinfo->panel_info.xres;
1636                 if (mode->yres > rinfo->panel_info.yres)
1637                         mode->yres = rinfo->panel_info.yres;
1638
1639                 newmode->fp_horz_stretch = (((rinfo->panel_info.xres / 8) - 1)
1640                                            << HORZ_PANEL_SHIFT);
1641                 newmode->fp_vert_stretch = ((rinfo->panel_info.yres - 1)
1642                                            << VERT_PANEL_SHIFT);
1643
1644                 if (mode->xres != rinfo->panel_info.xres) {
1645                         hRatio = round_div(mode->xres * HORZ_STRETCH_RATIO_MAX,
1646                                            rinfo->panel_info.xres);
1647                         newmode->fp_horz_stretch = (((((unsigned long)hRatio) & HORZ_STRETCH_RATIO_MASK)) |
1648                                                    (newmode->fp_horz_stretch &
1649                                                     (HORZ_PANEL_SIZE | HORZ_FP_LOOP_STRETCH |
1650                                                      HORZ_AUTO_RATIO_INC)));
1651                         newmode->fp_horz_stretch |= (HORZ_STRETCH_BLEND |
1652                                                     HORZ_STRETCH_ENABLE);
1653                 }
1654                 newmode->fp_horz_stretch &= ~HORZ_AUTO_RATIO;
1655
1656                 if (mode->yres != rinfo->panel_info.yres) {
1657                         vRatio = round_div(mode->yres * VERT_STRETCH_RATIO_MAX,
1658                                            rinfo->panel_info.yres);
1659                         newmode->fp_vert_stretch = (((((unsigned long)vRatio) & VERT_STRETCH_RATIO_MASK)) |
1660                                                    (newmode->fp_vert_stretch &
1661                                                    (VERT_PANEL_SIZE | VERT_STRETCH_RESERVED)));
1662                         newmode->fp_vert_stretch |= (VERT_STRETCH_BLEND |
1663                                                     VERT_STRETCH_ENABLE);
1664                 }
1665                 newmode->fp_vert_stretch &= ~VERT_AUTO_RATIO_EN;
1666
1667                 newmode->fp_gen_cntl = (rinfo->init_state.fp_gen_cntl & (u32)
1668                                        ~(FP_SEL_CRTC2 |
1669                                          FP_RMX_HVSYNC_CONTROL_EN |
1670                                          FP_DFP_SYNC_SEL |
1671                                          FP_CRT_SYNC_SEL |
1672                                          FP_CRTC_LOCK_8DOT |
1673                                          FP_USE_SHADOW_EN |
1674                                          FP_CRTC_USE_SHADOW_VEND |
1675                                          FP_CRT_SYNC_ALT));
1676
1677                 newmode->fp_gen_cntl |= (FP_CRTC_DONT_SHADOW_VPAR |
1678                                         FP_CRTC_DONT_SHADOW_HEND);
1679
1680                 newmode->lvds_gen_cntl = rinfo->init_state.lvds_gen_cntl;
1681                 newmode->lvds_pll_cntl = rinfo->init_state.lvds_pll_cntl;
1682                 newmode->tmds_crc = rinfo->init_state.tmds_crc;
1683                 newmode->tmds_transmitter_cntl = rinfo->init_state.tmds_transmitter_cntl;
1684
1685                 if (primary_mon == MT_LCD) {
1686                         newmode->lvds_gen_cntl |= (LVDS_ON | LVDS_BLON);
1687                         newmode->fp_gen_cntl &= ~(FP_FPON | FP_TMDS_EN);
1688                 } else {
1689                         /* DFP */
1690                         newmode->fp_gen_cntl |= (FP_FPON | FP_TMDS_EN);
1691                         newmode->tmds_transmitter_cntl = (TMDS_RAN_PAT_RST | TMDS_ICHCSEL) &
1692                                                          ~(TMDS_PLLRST);
1693                         /* TMDS_PLL_EN bit is reversed on RV (and mobility) chips */
1694                         if ((rinfo->family == CHIP_FAMILY_R300) ||
1695                             (rinfo->family == CHIP_FAMILY_R350) ||
1696                             (rinfo->family == CHIP_FAMILY_RV350) ||
1697                             (rinfo->family == CHIP_FAMILY_R200) || !rinfo->has_CRTC2)
1698                                 newmode->tmds_transmitter_cntl &= ~TMDS_PLL_EN;
1699                         else
1700                                 newmode->tmds_transmitter_cntl |= TMDS_PLL_EN;
1701                         newmode->crtc_ext_cntl &= ~CRTC_CRT_ON;
1702                 }
1703
1704                 newmode->fp_crtc_h_total_disp = (((rinfo->panel_info.hblank / 8) & 0x3ff) |
1705                                 (((mode->xres / 8) - 1) << 16));
1706                 newmode->fp_crtc_v_total_disp = (rinfo->panel_info.vblank & 0xffff) |
1707                                 ((mode->yres - 1) << 16);
1708                 newmode->fp_h_sync_strt_wid = ((rinfo->panel_info.hOver_plus & 0x1fff) |
1709                                 (hsync_wid << 16) | (h_sync_pol << 23));
1710                 newmode->fp_v_sync_strt_wid = ((rinfo->panel_info.vOver_plus & 0xfff) |
1711                                 (vsync_wid << 16) | (v_sync_pol  << 23));
1712         }
1713
1714         /* do it! */
1715         if (!rinfo->asleep) {
1716                 radeon_write_mode (rinfo, newmode);
1717                 /* (re)initialize the engine */
1718                 if (!(info->flags & FBINFO_HWACCEL_DISABLED))
1719                         radeonfb_engine_init (rinfo);
1720         }
1721         /* Update fix */
1722         if (!(info->flags & FBINFO_HWACCEL_DISABLED))
1723                 info->fix.line_length = rinfo->pitch*64;
1724         else
1725                 info->fix.line_length = mode->xres_virtual
1726                         * ((mode->bits_per_pixel + 1) / 8);
1727         info->fix.visual = rinfo->depth == 8 ? FB_VISUAL_PSEUDOCOLOR
1728                 : FB_VISUAL_DIRECTCOLOR;
1729
1730 #ifdef CONFIG_BOOTX_TEXT
1731         /* Update debug text engine */
1732         btext_update_display(rinfo->fb_base_phys, mode->xres, mode->yres,
1733                              rinfo->depth, info->fix.line_length);
1734 #endif
1735
1736         kfree(newmode);
1737         return 0;
1738 }
1739
1740
1741 static struct fb_ops radeonfb_ops = {
1742         .owner                  = THIS_MODULE,
1743         .fb_check_var           = radeonfb_check_var,
1744         .fb_set_par             = radeonfb_set_par,
1745         .fb_setcolreg           = radeonfb_setcolreg,
1746         .fb_pan_display         = radeonfb_pan_display,
1747         .fb_blank               = radeonfb_blank,
1748         .fb_ioctl               = radeonfb_ioctl,
1749         .fb_sync                = radeonfb_sync,
1750         .fb_fillrect            = radeonfb_fillrect,
1751         .fb_copyarea            = radeonfb_copyarea,
1752         .fb_imageblit           = radeonfb_imageblit,
1753         .fb_cursor              = soft_cursor,
1754 };
1755
1756
1757 static int __devinit radeon_set_fbinfo (struct radeonfb_info *rinfo)
1758 {
1759         struct fb_info *info = rinfo->info;
1760
1761         info->par = rinfo;
1762         info->pseudo_palette = rinfo->pseudo_palette;
1763         info->flags = FBINFO_DEFAULT
1764                     | FBINFO_HWACCEL_COPYAREA
1765                     | FBINFO_HWACCEL_FILLRECT
1766                     | FBINFO_HWACCEL_XPAN
1767                     | FBINFO_HWACCEL_YPAN;
1768         info->fbops = &radeonfb_ops;
1769         info->screen_base = rinfo->fb_base;
1770         info->screen_size = rinfo->mapped_vram;
1771         /* Fill fix common fields */
1772         strlcpy(info->fix.id, rinfo->name, sizeof(info->fix.id));
1773         info->fix.smem_start = rinfo->fb_base_phys;
1774         info->fix.smem_len = rinfo->video_ram;
1775         info->fix.type = FB_TYPE_PACKED_PIXELS;
1776         info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
1777         info->fix.xpanstep = 8;
1778         info->fix.ypanstep = 1;
1779         info->fix.ywrapstep = 0;
1780         info->fix.type_aux = 0;
1781         info->fix.mmio_start = rinfo->mmio_base_phys;
1782         info->fix.mmio_len = RADEON_REGSIZE;
1783
1784         fb_alloc_cmap(&info->cmap, 256, 0);
1785
1786         if (noaccel)
1787                 info->flags |= FBINFO_HWACCEL_DISABLED;
1788
1789         return 0;
1790 }
1791
1792
1793 #ifdef CONFIG_PMAC_BACKLIGHT
1794
1795 /* TODO: Dbl check these tables, we don't go up to full ON backlight
1796  * in these, possibly because we noticed MacOS doesn't, but I'd prefer
1797  * having some more official numbers from ATI
1798  */
1799 static int backlight_conv_m6[] = {
1800         0xff, 0xc0, 0xb5, 0xaa, 0x9f, 0x94, 0x89, 0x7e,
1801         0x73, 0x68, 0x5d, 0x52, 0x47, 0x3c, 0x31, 0x24
1802 };
1803 static int backlight_conv_m7[] = {
1804         0x00, 0x3f, 0x4a, 0x55, 0x60, 0x6b, 0x76, 0x81,
1805         0x8c, 0x97, 0xa2, 0xad, 0xb8, 0xc3, 0xce, 0xd9
1806 };
1807
1808 #define BACKLIGHT_LVDS_OFF
1809 #undef BACKLIGHT_DAC_OFF
1810
1811 /* We turn off the LCD completely instead of just dimming the backlight.
1812  * This provides some greater power saving and the display is useless
1813  * without backlight anyway.
1814  */
1815 static int radeon_set_backlight_enable(int on, int level, void *data)
1816 {
1817         struct radeonfb_info *rinfo = (struct radeonfb_info *)data;
1818         u32 lvds_gen_cntl, tmpPixclksCntl;
1819         int* conv_table;
1820
1821         if (rinfo->mon1_type != MT_LCD)
1822                 return 0;
1823
1824         /* Pardon me for that hack... maybe some day we can figure
1825          * out in what direction backlight should work on a given
1826          * panel ?
1827          */
1828         if ((rinfo->family == CHIP_FAMILY_RV200 ||
1829              rinfo->family == CHIP_FAMILY_RV250 ||
1830              rinfo->family == CHIP_FAMILY_RV280 ||
1831              rinfo->family == CHIP_FAMILY_RV350) &&
1832             !machine_is_compatible("PowerBook4,3") &&
1833             !machine_is_compatible("PowerBook6,3") &&
1834             !machine_is_compatible("PowerBook6,5"))
1835                 conv_table = backlight_conv_m7;
1836         else
1837                 conv_table = backlight_conv_m6;
1838
1839         del_timer_sync(&rinfo->lvds_timer);
1840         radeon_engine_idle();
1841
1842         lvds_gen_cntl = INREG(LVDS_GEN_CNTL);
1843         if (on && (level > BACKLIGHT_OFF)) {
1844                 lvds_gen_cntl &= ~LVDS_DISPLAY_DIS;
1845                 if (!(lvds_gen_cntl & LVDS_BLON) || !(lvds_gen_cntl & LVDS_ON)) {
1846                         lvds_gen_cntl |= LVDS_BLON /* | LVDS_EN | LVDS_DIGON */;
1847                         OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
1848                         lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
1849                         lvds_gen_cntl |= (conv_table[level] <<
1850                                           LVDS_BL_MOD_LEVEL_SHIFT);
1851                         lvds_gen_cntl |= LVDS_ON;
1852                         rinfo->pending_lvds_gen_cntl = lvds_gen_cntl;
1853                         mod_timer(&rinfo->lvds_timer,
1854                                   jiffies + msecs_to_jiffies(rinfo->panel_info.pwr_delay));
1855                 } else {
1856                         lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
1857                         lvds_gen_cntl |= (conv_table[level] <<
1858                                           LVDS_BL_MOD_LEVEL_SHIFT);
1859                         OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
1860                 }
1861                 rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
1862                 rinfo->init_state.lvds_gen_cntl |= rinfo->pending_lvds_gen_cntl
1863                         & LVDS_STATE_MASK;
1864         } else {
1865                 /* Asic bug, when turning off LVDS_ON, we have to make sure
1866                    RADEON_PIXCLK_LVDS_ALWAYS_ON bit is off
1867                 */
1868                 tmpPixclksCntl = INPLL(PIXCLKS_CNTL);
1869                 if (rinfo->is_mobility || rinfo->is_IGP)
1870                         OUTPLLP(PIXCLKS_CNTL, 0, ~PIXCLK_LVDS_ALWAYS_ONb);
1871                 lvds_gen_cntl &= ~LVDS_BL_MOD_LEVEL_MASK;
1872                 lvds_gen_cntl |= (conv_table[0] <<
1873                                   LVDS_BL_MOD_LEVEL_SHIFT);
1874                 lvds_gen_cntl |= LVDS_DISPLAY_DIS;
1875                 OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
1876                 lvds_gen_cntl &= ~(LVDS_ON | LVDS_BLON /* | LVDS_EN | LVDS_DIGON */);
1877                 OUTREG(LVDS_GEN_CNTL, lvds_gen_cntl);
1878                 if (rinfo->is_mobility || rinfo->is_IGP)
1879                         OUTPLL(PIXCLKS_CNTL, tmpPixclksCntl);
1880         }
1881         rinfo->init_state.lvds_gen_cntl &= ~LVDS_STATE_MASK;
1882         rinfo->init_state.lvds_gen_cntl |= (lvds_gen_cntl & LVDS_STATE_MASK);
1883
1884         return 0;
1885 }
1886
1887
1888 static int radeon_set_backlight_level(int level, void *data)
1889 {
1890         return radeon_set_backlight_enable(1, level, data);
1891 }
1892 #endif /* CONFIG_PMAC_BACKLIGHT */
1893
1894
1895 /*
1896  * This reconfigure the card's internal memory map. In theory, we'd like
1897  * to setup the card's memory at the same address as it's PCI bus address,
1898  * and the AGP aperture right after that so that system RAM on 32 bits
1899  * machines at least, is directly accessible. However, doing so would
1900  * conflict with the current XFree drivers...
1901  * Ultimately, I hope XFree, GATOS and ATI binary drivers will all agree
1902  * on the proper way to set this up and duplicate this here. In the meantime,
1903  * I put the card's memory at 0 in card space and AGP at some random high
1904  * local (0xe0000000 for now) that will be changed by XFree/DRI anyway
1905  */
1906 #ifdef CONFIG_PPC_OF
1907 #undef SET_MC_FB_FROM_APERTURE
1908 static void fixup_memory_mappings(struct radeonfb_info *rinfo)
1909 {
1910         u32 save_crtc_gen_cntl, save_crtc2_gen_cntl = 0;
1911         u32 save_crtc_ext_cntl;
1912         u32 aper_base, aper_size;
1913         u32 agp_base;
1914
1915         /* First, we disable display to avoid interfering */
1916         if (rinfo->has_CRTC2) {
1917                 save_crtc2_gen_cntl = INREG(CRTC2_GEN_CNTL);
1918                 OUTREG(CRTC2_GEN_CNTL, save_crtc2_gen_cntl | CRTC2_DISP_REQ_EN_B);
1919         }
1920         save_crtc_gen_cntl = INREG(CRTC_GEN_CNTL);
1921         save_crtc_ext_cntl = INREG(CRTC_EXT_CNTL);
1922         
1923         OUTREG(CRTC_EXT_CNTL, save_crtc_ext_cntl | CRTC_DISPLAY_DIS);
1924         OUTREG(CRTC_GEN_CNTL, save_crtc_gen_cntl | CRTC_DISP_REQ_EN_B);
1925         mdelay(100);
1926
1927         aper_base = INREG(CONFIG_APER_0_BASE);
1928         aper_size = INREG(CONFIG_APER_SIZE);
1929
1930 #ifdef SET_MC_FB_FROM_APERTURE
1931         /* Set framebuffer to be at the same address as set in PCI BAR */
1932         OUTREG(MC_FB_LOCATION, 
1933                 ((aper_base + aper_size - 1) & 0xffff0000) | (aper_base >> 16));
1934         rinfo->fb_local_base = aper_base;
1935 #else
1936         OUTREG(MC_FB_LOCATION, 0x7fff0000);
1937         rinfo->fb_local_base = 0;
1938 #endif
1939         agp_base = aper_base + aper_size;
1940         if (agp_base & 0xf0000000)
1941                 agp_base = (aper_base | 0x0fffffff) + 1;
1942
1943         /* Set AGP to be just after the framebuffer on a 256Mb boundary. This
1944          * assumes the FB isn't mapped to 0xf0000000 or above, but this is
1945          * always the case on PPCs afaik.
1946          */
1947 #ifdef SET_MC_FB_FROM_APERTURE
1948         OUTREG(MC_AGP_LOCATION, 0xffff0000 | (agp_base >> 16));
1949 #else
1950         OUTREG(MC_AGP_LOCATION, 0xffffe000);
1951 #endif
1952
1953         /* Fixup the display base addresses & engine offsets while we
1954          * are at it as well
1955          */
1956 #ifdef SET_MC_FB_FROM_APERTURE
1957         OUTREG(DISPLAY_BASE_ADDR, aper_base);
1958         if (rinfo->has_CRTC2)
1959                 OUTREG(CRTC2_DISPLAY_BASE_ADDR, aper_base);
1960 #else
1961         OUTREG(DISPLAY_BASE_ADDR, 0);
1962         if (rinfo->has_CRTC2)
1963                 OUTREG(CRTC2_DISPLAY_BASE_ADDR, 0);
1964 #endif
1965         mdelay(100);
1966
1967         /* Restore display settings */
1968         OUTREG(CRTC_GEN_CNTL, save_crtc_gen_cntl);
1969         OUTREG(CRTC_EXT_CNTL, save_crtc_ext_cntl);
1970         if (rinfo->has_CRTC2)
1971                 OUTREG(CRTC2_GEN_CNTL, save_crtc2_gen_cntl);    
1972
1973         RTRACE("aper_base: %08x MC_FB_LOC to: %08x, MC_AGP_LOC to: %08x\n",
1974                 aper_base,
1975                 ((aper_base + aper_size - 1) & 0xffff0000) | (aper_base >> 16),
1976                 0xffff0000 | (agp_base >> 16));
1977 }
1978 #endif /* CONFIG_PPC_OF */
1979
1980
1981 /*
1982  * Sysfs
1983  */
1984
1985 static ssize_t radeon_show_one_edid(char *buf, loff_t off, size_t count, const u8 *edid)
1986 {
1987         if (off > EDID_LENGTH)
1988                 return 0;
1989
1990         if (off + count > EDID_LENGTH)
1991                 count = EDID_LENGTH - off;
1992
1993         memcpy(buf, edid + off, count);
1994
1995         return count;
1996 }
1997
1998
1999 static ssize_t radeon_show_edid1(struct kobject *kobj, char *buf, loff_t off, size_t count)
2000 {
2001         struct device *dev = container_of(kobj, struct device, kobj);
2002         struct pci_dev *pdev = to_pci_dev(dev);
2003         struct fb_info *info = pci_get_drvdata(pdev);
2004         struct radeonfb_info *rinfo = info->par;
2005
2006         return radeon_show_one_edid(buf, off, count, rinfo->mon1_EDID);
2007 }
2008
2009
2010 static ssize_t radeon_show_edid2(struct kobject *kobj, char *buf, loff_t off, size_t count)
2011 {
2012         struct device *dev = container_of(kobj, struct device, kobj);
2013         struct pci_dev *pdev = to_pci_dev(dev);
2014         struct fb_info *info = pci_get_drvdata(pdev);
2015         struct radeonfb_info *rinfo = info->par;
2016
2017         return radeon_show_one_edid(buf, off, count, rinfo->mon2_EDID);
2018 }
2019
2020 static struct bin_attribute edid1_attr = {
2021         .attr   = {
2022                 .name   = "edid1",
2023                 .owner  = THIS_MODULE,
2024                 .mode   = 0444,
2025         },
2026         .size   = EDID_LENGTH,
2027         .read   = radeon_show_edid1,
2028 };
2029
2030 static struct bin_attribute edid2_attr = {
2031         .attr   = {
2032                 .name   = "edid2",
2033                 .owner  = THIS_MODULE,
2034                 .mode   = 0444,
2035         },
2036         .size   = EDID_LENGTH,
2037         .read   = radeon_show_edid2,
2038 };
2039
2040
2041 static int radeonfb_pci_register (struct pci_dev *pdev,
2042                                   const struct pci_device_id *ent)
2043 {
2044         struct fb_info *info;
2045         struct radeonfb_info *rinfo;
2046         u32 tmp;
2047         int ret;
2048
2049         RTRACE("radeonfb_pci_register BEGIN\n");
2050         
2051         /* Enable device in PCI config */
2052         ret = pci_enable_device(pdev);
2053         if (ret < 0) {
2054                 printk(KERN_ERR "radeonfb: Cannot enable PCI device\n");
2055                 goto err_out;
2056         }
2057
2058         info = framebuffer_alloc(sizeof(struct radeonfb_info), &pdev->dev);
2059         if (!info) {
2060                 printk (KERN_ERR "radeonfb: could not allocate memory\n");
2061                 ret = -ENOMEM;
2062                 goto err_disable;
2063         }
2064         rinfo = info->par;
2065         rinfo->info = info;     
2066         rinfo->pdev = pdev;
2067         
2068         spin_lock_init(&rinfo->reg_lock);
2069         init_timer(&rinfo->lvds_timer);
2070         rinfo->lvds_timer.function = radeon_lvds_timer_func;
2071         rinfo->lvds_timer.data = (unsigned long)rinfo;
2072
2073         strcpy(rinfo->name, "ATI Radeon XX ");
2074         rinfo->name[11] = ent->device >> 8;
2075         rinfo->name[12] = ent->device & 0xFF;
2076         rinfo->family = ent->driver_data & CHIP_FAMILY_MASK;
2077         rinfo->chipset = pdev->device;
2078         rinfo->has_CRTC2 = (ent->driver_data & CHIP_HAS_CRTC2) != 0;
2079         rinfo->is_mobility = (ent->driver_data & CHIP_IS_MOBILITY) != 0;
2080         rinfo->is_IGP = (ent->driver_data & CHIP_IS_IGP) != 0;
2081                 
2082         /* Set base addrs */
2083         rinfo->fb_base_phys = pci_resource_start (pdev, 0);
2084         rinfo->mmio_base_phys = pci_resource_start (pdev, 2);
2085
2086         /* request the mem regions */
2087         ret = pci_request_regions(pdev, "radeonfb");
2088         if (ret < 0) {
2089                 printk( KERN_ERR "radeonfb: cannot reserve PCI regions."
2090                         "  Someone already got them?\n");
2091                 goto err_release_fb;
2092         }
2093
2094         /* map the regions */
2095         rinfo->mmio_base = ioremap(rinfo->mmio_base_phys, RADEON_REGSIZE);
2096         if (!rinfo->mmio_base) {
2097                 printk(KERN_ERR "radeonfb: cannot map MMIO\n");
2098                 ret = -EIO;
2099                 goto err_release_pci;
2100         }
2101
2102         /* On PPC, the firmware sets up a memory mapping that tends
2103          * to cause lockups when enabling the engine. We reconfigure
2104          * the card internal memory mappings properly
2105          */
2106 #ifdef CONFIG_PPC_OF
2107         fixup_memory_mappings(rinfo);
2108 #else   
2109         rinfo->fb_local_base = INREG(MC_FB_LOCATION) << 16;
2110 #endif /* CONFIG_PPC_OF */
2111
2112         /* framebuffer size */
2113         if ((rinfo->family == CHIP_FAMILY_RS100) ||
2114             (rinfo->family == CHIP_FAMILY_RS200) ||
2115             (rinfo->family == CHIP_FAMILY_RS300)) {
2116           u32 tom = INREG(NB_TOM);
2117           tmp = ((((tom >> 16) - (tom & 0xffff) + 1) << 6) * 1024);
2118  
2119                 radeon_fifo_wait(6);
2120           OUTREG(MC_FB_LOCATION, tom);
2121           OUTREG(DISPLAY_BASE_ADDR, (tom & 0xffff) << 16);
2122           OUTREG(CRTC2_DISPLAY_BASE_ADDR, (tom & 0xffff) << 16);
2123           OUTREG(OV0_BASE_ADDR, (tom & 0xffff) << 16);
2124  
2125           /* This is supposed to fix the crtc2 noise problem. */
2126           OUTREG(GRPH2_BUFFER_CNTL, INREG(GRPH2_BUFFER_CNTL) & ~0x7f0000);
2127  
2128           if ((rinfo->family == CHIP_FAMILY_RS100) ||
2129               (rinfo->family == CHIP_FAMILY_RS200)) {
2130              /* This is to workaround the asic bug for RMX, some versions
2131                 of BIOS dosen't have this register initialized correctly.
2132              */
2133              OUTREGP(CRTC_MORE_CNTL, CRTC_H_CUTOFF_ACTIVE_EN,
2134                      ~CRTC_H_CUTOFF_ACTIVE_EN);
2135           }
2136         } else {
2137           tmp = INREG(CONFIG_MEMSIZE);
2138         }
2139
2140         /* mem size is bits [28:0], mask off the rest */
2141         rinfo->video_ram = tmp & CONFIG_MEMSIZE_MASK;
2142
2143         /* ram type */
2144         tmp = INREG(MEM_SDRAM_MODE_REG);
2145         switch ((MEM_CFG_TYPE & tmp) >> 30) {
2146         case 0:
2147                 /* SDR SGRAM (2:1) */
2148                 strcpy(rinfo->ram_type, "SDR SGRAM");
2149                 rinfo->ram.ml = 4;
2150                 rinfo->ram.mb = 4;
2151                 rinfo->ram.trcd = 1;
2152                 rinfo->ram.trp = 2;
2153                 rinfo->ram.twr = 1;
2154                 rinfo->ram.cl = 2;
2155                 rinfo->ram.loop_latency = 16;
2156                 rinfo->ram.rloop = 16;
2157                 break;
2158         case 1:
2159                 /* DDR SGRAM */
2160                 strcpy(rinfo->ram_type, "DDR SGRAM");
2161                 rinfo->ram.ml = 4;
2162                 rinfo->ram.mb = 4;
2163                 rinfo->ram.trcd = 3;
2164                 rinfo->ram.trp = 3;
2165                 rinfo->ram.twr = 2;
2166                 rinfo->ram.cl = 3;
2167                 rinfo->ram.tr2w = 1;
2168                 rinfo->ram.loop_latency = 16;
2169                 rinfo->ram.rloop = 16;
2170                 break;
2171         default:
2172                 /* 64-bit SDR SGRAM */
2173                 strcpy(rinfo->ram_type, "SDR SGRAM 64");
2174                 rinfo->ram.ml = 4;
2175                 rinfo->ram.mb = 8;
2176                 rinfo->ram.trcd = 3;
2177                 rinfo->ram.trp = 3;
2178                 rinfo->ram.twr = 1;
2179                 rinfo->ram.cl = 3;
2180                 rinfo->ram.tr2w = 1;
2181                 rinfo->ram.loop_latency = 17;
2182                 rinfo->ram.rloop = 17;
2183                 break;
2184         }
2185
2186         /*
2187          * Hack to get around some busted production M6's
2188          * reporting no ram
2189          */
2190         if (rinfo->video_ram == 0) {
2191                 switch (pdev->device) {
2192                 case PCI_CHIP_RADEON_LY:
2193                 case PCI_CHIP_RADEON_LZ:
2194                         rinfo->video_ram = 8192 * 1024;
2195                         break;
2196                 default:
2197                         printk (KERN_ERR "radeonfb: no video RAM reported\n");
2198                         ret = -ENXIO;
2199                         goto err_unmap_rom;
2200                 }
2201         }
2202
2203         RTRACE("radeonfb: probed %s %ldk videoram\n", (rinfo->ram_type), (rinfo->video_ram/1024));
2204
2205         rinfo->mapped_vram = min_t(unsigned long, MAX_MAPPED_VRAM, rinfo->video_ram);
2206
2207         do {
2208                 rinfo->fb_base = ioremap (rinfo->fb_base_phys,
2209                                           rinfo->mapped_vram);
2210         } while (   rinfo->fb_base == 0 &&
2211                   ((rinfo->mapped_vram /=2) >= MIN_MAPPED_VRAM) );
2212
2213         if (rinfo->fb_base)
2214                 memset_io(rinfo->fb_base, 0, rinfo->mapped_vram);
2215         else {
2216                 printk (KERN_ERR "radeonfb: cannot map FB\n");
2217                 ret = -EIO;
2218                 goto err_unmap_rom;
2219         }
2220
2221         RTRACE("radeonfb: mapped %ldk videoram\n", rinfo->mapped_vram/1024);
2222
2223         /*
2224          * Check for required workaround for PLL accesses
2225          */
2226         rinfo->R300_cg_workaround = (rinfo->family == CHIP_FAMILY_R300 &&
2227                                      (INREG(CONFIG_CNTL) & CFG_ATI_REV_ID_MASK)
2228                                      == CFG_ATI_REV_A11);
2229
2230         /*
2231          * Map the BIOS ROM if any and retreive PLL parameters from
2232          * the BIOS. We skip that on mobility chips as the real panel
2233          * values we need aren't in the ROM but in the BIOS image in
2234          * memory. This is definitely not the best meacnism though,
2235          * we really need the arch code to tell us which is the "primary"
2236          * video adapter to use the memory image (or better, the arch
2237          * should provide us a copy of the BIOS image to shield us from
2238          * archs who would store that elsewhere and/or could initialize
2239          * more than one adapter during boot).
2240          */
2241         if (!rinfo->is_mobility)
2242                 radeon_map_ROM(rinfo, pdev);
2243
2244         /*
2245          * On x86, the primary display on laptop may have it's BIOS
2246          * ROM elsewhere, try to locate it at the legacy memory hole.
2247          * We probably need to make sure this is the primary display,
2248          * but that is difficult without some arch support.
2249          */
2250 #ifdef CONFIG_X86
2251         if (rinfo->bios_seg == NULL)
2252                 radeon_find_mem_vbios(rinfo);
2253 #endif
2254
2255         /* If both above failed, try the BIOS ROM again for mobility
2256          * chips
2257          */
2258         if (rinfo->bios_seg == NULL && rinfo->is_mobility)
2259                 radeon_map_ROM(rinfo, pdev);
2260
2261         /* Get informations about the board's PLL */
2262         radeon_get_pllinfo(rinfo);
2263
2264 #ifdef CONFIG_FB_RADEON_I2C
2265         /* Register I2C bus */
2266         radeon_create_i2c_busses(rinfo);
2267 #endif
2268
2269         /* set all the vital stuff */
2270         radeon_set_fbinfo (rinfo);
2271
2272         /* Probe screen types */
2273         radeon_probe_screens(rinfo, monitor_layout, ignore_edid);
2274
2275         /* Build mode list, check out panel native model */
2276         radeon_check_modes(rinfo, mode_option);
2277
2278         /* Register some sysfs stuff (should be done better) */
2279         if (rinfo->mon1_EDID)
2280                 sysfs_create_bin_file(&rinfo->pdev->dev.kobj, &edid1_attr);
2281         if (rinfo->mon2_EDID)
2282                 sysfs_create_bin_file(&rinfo->pdev->dev.kobj, &edid2_attr);
2283
2284         /* save current mode regs before we switch into the new one
2285          * so we can restore this upon __exit
2286          */
2287         radeon_save_state (rinfo, &rinfo->init_state);
2288
2289         pci_set_drvdata(pdev, info);
2290
2291         /* Enable PM on mobility chips */
2292         if (rinfo->is_mobility) {
2293                 /* Find PM registers in config space */
2294                 rinfo->pm_reg = pci_find_capability(pdev, PCI_CAP_ID_PM);
2295                 /* Enable dynamic PM of chip clocks */
2296                 radeon_pm_enable_dynamic_mode(rinfo);
2297                 printk("radeonfb: Power Management enabled for Mobility chipsets\n");
2298         }
2299
2300         ret = register_framebuffer(info);
2301         if (ret < 0) {
2302                 printk (KERN_ERR "radeonfb: could not register framebuffer\n");
2303                 goto err_unmap_fb;
2304         }
2305
2306 #ifdef CONFIG_MTRR
2307         rinfo->mtrr_hdl = nomtrr ? -1 : mtrr_add(rinfo->fb_base_phys,
2308                                                  rinfo->video_ram,
2309                                                  MTRR_TYPE_WRCOMB, 1);
2310 #endif
2311
2312 #ifdef CONFIG_PMAC_BACKLIGHT
2313         if (rinfo->mon1_type == MT_LCD) {
2314                 register_backlight_controller(&radeon_backlight_controller,
2315                                               rinfo, "ati");
2316                 register_backlight_controller(&radeon_backlight_controller,
2317                                               rinfo, "mnca");
2318         }
2319 #endif
2320
2321         printk ("radeonfb: %s %s %ld MB\n", rinfo->name, rinfo->ram_type,
2322                 (rinfo->video_ram/(1024*1024)));
2323
2324         if (rinfo->bios_seg)
2325                 radeon_unmap_ROM(rinfo, pdev);
2326         RTRACE("radeonfb_pci_register END\n");
2327
2328         return 0;
2329 err_unmap_fb:
2330         iounmap(rinfo->fb_base);
2331 err_unmap_rom:
2332         if (rinfo->mon1_EDID)
2333             kfree(rinfo->mon1_EDID);
2334         if (rinfo->mon2_EDID)
2335             kfree(rinfo->mon2_EDID);
2336         if (rinfo->mon1_modedb)
2337                 fb_destroy_modedb(rinfo->mon1_modedb);
2338         fb_dealloc_cmap(&info->cmap);
2339 #ifdef CONFIG_FB_RADEON_I2C
2340         radeon_delete_i2c_busses(rinfo);
2341 #endif
2342         if (rinfo->bios_seg)
2343                 radeon_unmap_ROM(rinfo, pdev);
2344         iounmap(rinfo->mmio_base);
2345 err_release_pci:
2346         pci_release_regions(pdev);
2347 err_release_fb:
2348         framebuffer_release(info);
2349 err_disable:
2350         pci_disable_device(pdev);
2351 err_out:
2352         return ret;
2353 }
2354
2355
2356
2357 static void __devexit radeonfb_pci_unregister (struct pci_dev *pdev)
2358 {
2359         struct fb_info *info = pci_get_drvdata(pdev);
2360         struct radeonfb_info *rinfo = info->par;
2361  
2362         if (!rinfo)
2363                 return;
2364  
2365         /* restore original state
2366          * 
2367          * Doesn't quite work yet, possibly because of the PPC hacking
2368          * I do on startup, disable for now. --BenH
2369          */
2370         radeon_write_mode (rinfo, &rinfo->init_state);
2371  
2372         del_timer_sync(&rinfo->lvds_timer);
2373
2374 #ifdef CONFIG_MTRR
2375         if (rinfo->mtrr_hdl >= 0)
2376                 mtrr_del(rinfo->mtrr_hdl, 0, 0);
2377 #endif
2378
2379         unregister_framebuffer(info);
2380
2381         iounmap(rinfo->mmio_base);
2382         iounmap(rinfo->fb_base);
2383  
2384         pci_release_regions(pdev);
2385
2386         if (rinfo->mon1_EDID)
2387                 kfree(rinfo->mon1_EDID);
2388         if (rinfo->mon2_EDID)
2389                 kfree(rinfo->mon2_EDID);
2390         if (rinfo->mon1_modedb)
2391                 fb_destroy_modedb(rinfo->mon1_modedb);
2392 #ifdef CONFIG_FB_RADEON_I2C
2393         radeon_delete_i2c_busses(rinfo);
2394 #endif        
2395         fb_dealloc_cmap(&info->cmap);
2396         framebuffer_release(info);
2397         pci_disable_device(pdev);
2398 }
2399
2400
2401 static struct pci_driver radeonfb_driver = {
2402         .name           = "radeonfb",
2403         .id_table       = radeonfb_pci_table,
2404         .probe          = radeonfb_pci_register,
2405         .remove         = __devexit_p(radeonfb_pci_unregister),
2406 #ifdef CONFIG_PM
2407         .suspend        = radeonfb_pci_suspend,
2408         .resume         = radeonfb_pci_resume,
2409 #endif /* CONFIG_PM */
2410 };
2411
2412 int __init radeonfb_setup (char *options);
2413
2414 int __init radeonfb_init (void)
2415 {
2416 #ifndef MODULE
2417         char *option = NULL;
2418
2419         if (fb_get_options("radeonfb", &option))
2420                 return -ENODEV;
2421         radeonfb_setup(option);
2422 #endif
2423         return pci_module_init (&radeonfb_driver);
2424 }
2425
2426
2427 void __exit radeonfb_exit (void)
2428 {
2429         pci_unregister_driver (&radeonfb_driver);
2430 }
2431
2432 int __init radeonfb_setup (char *options)
2433 {
2434         char *this_opt;
2435
2436         if (!options || !*options)
2437                 return 0;
2438
2439         while ((this_opt = strsep (&options, ",")) != NULL) {
2440                 if (!*this_opt)
2441                         continue;
2442
2443                 if (!strncmp(this_opt, "noaccel", 7)) {
2444                         noaccel = 1;
2445                 } else if (!strncmp(this_opt, "mirror", 6)) {
2446                         mirror = 1;
2447                 } else if (!strncmp(this_opt, "force_dfp", 9)) {
2448                         force_dfp = 1;
2449                 } else if (!strncmp(this_opt, "panel_yres:", 11)) {
2450                         panel_yres = simple_strtoul((this_opt+11), NULL, 0);
2451 #ifdef CONFIG_MTRR
2452                 } else if (!strncmp(this_opt, "nomtrr", 6)) {
2453                         nomtrr = 1;
2454 #endif
2455                 } else if (!strncmp(this_opt, "nomodeset", 9)) {
2456                         nomodeset = 1;
2457                 } else if (!strncmp(this_opt, "force_measure_pll", 17)) {
2458                         force_measure_pll = 1;
2459                 } else if (!strncmp(this_opt, "ignore_edid", 11)) {
2460                         ignore_edid = 1;
2461                 } else
2462                         mode_option = this_opt;
2463         }
2464         return 0;
2465 }
2466
2467 module_init(radeonfb_init);
2468
2469 #ifdef MODULE
2470 module_exit(radeonfb_exit);
2471 #endif
2472
2473 MODULE_AUTHOR("Ani Joshi");
2474 MODULE_DESCRIPTION("framebuffer driver for ATI Radeon chipset");
2475 MODULE_LICENSE("GPL");
2476 module_param(noaccel, bool, 0);
2477 MODULE_PARM_DESC(noaccel, "bool: disable acceleration");
2478 module_param(nomodeset, bool, 0);
2479 MODULE_PARM_DESC(nomodeset, "bool: disable actual setting of video mode");
2480 module_param(mirror, bool, 0);
2481 MODULE_PARM_DESC(mirror, "bool: mirror the display to both monitors");
2482 module_param(force_dfp, bool, 0);
2483 MODULE_PARM_DESC(force_dfp, "bool: force display to dfp");
2484 module_param(ignore_edid, bool, 0);
2485 MODULE_PARM_DESC(ignore_edid, "bool: Ignore EDID data when doing DDC probe");
2486 module_param(monitor_layout, charp, 0);
2487 MODULE_PARM_DESC(monitor_layout, "Specify monitor mapping (like XFree86)");
2488 module_param(force_measure_pll, bool, 0);
2489 MODULE_PARM_DESC(force_measure_pll, "Force measurement of PLL (debug)");
2490 #ifdef CONFIG_MTRR
2491 module_param(nomtrr, bool, 0);
2492 MODULE_PARM_DESC(nomtrr, "bool: disable use of MTRR registers");
2493 #endif
2494 module_param(panel_yres, int, 0);
2495 MODULE_PARM_DESC(panel_yres, "int: set panel yres");
2496 module_param(mode_option, charp, 0);
2497 MODULE_PARM_DESC(mode_option, "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");