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