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