Pull platform-drivers into test branch
[pandora-kernel.git] / drivers / video / aty / aty128fb.c
1 /* $Id: aty128fb.c,v 1.1.1.1.36.1 1999/12/11 09:03:05 Exp $
2  *  linux/drivers/video/aty128fb.c -- Frame buffer device for ATI Rage128
3  *
4  *  Copyright (C) 1999-2003, Brad Douglas <brad@neruo.com>
5  *  Copyright (C) 1999, Anthony Tong <atong@uiuc.edu>
6  *
7  *                Ani Joshi / Jeff Garzik
8  *                      - Code cleanup
9  *
10  *                Michel Danzer <michdaen@iiic.ethz.ch>
11  *                      - 15/16 bit cleanup
12  *                      - fix panning
13  *
14  *                Benjamin Herrenschmidt
15  *                      - pmac-specific PM stuff
16  *                      - various fixes & cleanups
17  *
18  *                Andreas Hundt <andi@convergence.de>
19  *                      - FB_ACTIVATE fixes
20  *
21  *                Paul Mackerras <paulus@samba.org>
22  *                      - Convert to new framebuffer API,
23  *                        fix colormap setting at 16 bits/pixel (565)
24  *
25  *                Paul Mundt 
26  *                      - PCI hotplug
27  *
28  *                Jon Smirl <jonsmirl@yahoo.com>
29  *                      - PCI ID update
30  *                      - replace ROM BIOS search
31  *
32  *  Based off of Geert's atyfb.c and vfb.c.
33  *
34  *  TODO:
35  *              - monitor sensing (DDC)
36  *              - virtual display
37  *              - other platform support (only ppc/x86 supported)
38  *              - hardware cursor support
39  *
40  *    Please cc: your patches to brad@neruo.com.
41  */
42
43 /*
44  * A special note of gratitude to ATI's devrel for providing documentation,
45  * example code and hardware. Thanks Nitya.     -atong and brad
46  */
47
48
49 #include <linux/module.h>
50 #include <linux/moduleparam.h>
51 #include <linux/kernel.h>
52 #include <linux/errno.h>
53 #include <linux/string.h>
54 #include <linux/mm.h>
55 #include <linux/slab.h>
56 #include <linux/vmalloc.h>
57 #include <linux/delay.h>
58 #include <linux/interrupt.h>
59 #include <asm/uaccess.h>
60 #include <linux/fb.h>
61 #include <linux/init.h>
62 #include <linux/pci.h>
63 #include <linux/ioport.h>
64 #include <linux/console.h>
65 #include <linux/backlight.h>
66 #include <asm/io.h>
67
68 #ifdef CONFIG_PPC_PMAC
69 #include <asm/machdep.h>
70 #include <asm/pmac_feature.h>
71 #include <asm/prom.h>
72 #include <asm/pci-bridge.h>
73 #include "../macmodes.h"
74 #endif
75
76 #ifdef CONFIG_PMAC_BACKLIGHT
77 #include <asm/backlight.h>
78 #endif
79
80 #ifdef CONFIG_BOOTX_TEXT
81 #include <asm/btext.h>
82 #endif /* CONFIG_BOOTX_TEXT */
83
84 #ifdef CONFIG_MTRR
85 #include <asm/mtrr.h>
86 #endif
87
88 #include <video/aty128.h>
89
90 /* Debug flag */
91 #undef DEBUG
92
93 #ifdef DEBUG
94 #define DBG(fmt, args...)               printk(KERN_DEBUG "aty128fb: %s " fmt, __FUNCTION__, ##args);
95 #else
96 #define DBG(fmt, args...)
97 #endif
98
99 #ifndef CONFIG_PPC_PMAC
100 /* default mode */
101 static struct fb_var_screeninfo default_var __devinitdata = {
102         /* 640x480, 60 Hz, Non-Interlaced (25.175 MHz dotclock) */
103         640, 480, 640, 480, 0, 0, 8, 0,
104         {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
105         0, 0, -1, -1, 0, 39722, 48, 16, 33, 10, 96, 2,
106         0, FB_VMODE_NONINTERLACED
107 };
108
109 #else /* CONFIG_PPC_PMAC */
110 /* default to 1024x768 at 75Hz on PPC - this will work
111  * on the iMac, the usual 640x480 @ 60Hz doesn't. */
112 static struct fb_var_screeninfo default_var = {
113         /* 1024x768, 75 Hz, Non-Interlaced (78.75 MHz dotclock) */
114         1024, 768, 1024, 768, 0, 0, 8, 0,
115         {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
116         0, 0, -1, -1, 0, 12699, 160, 32, 28, 1, 96, 3,
117         FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
118         FB_VMODE_NONINTERLACED
119 };
120 #endif /* CONFIG_PPC_PMAC */
121
122 /* default modedb mode */
123 /* 640x480, 60 Hz, Non-Interlaced (25.172 MHz dotclock) */
124 static struct fb_videomode defaultmode __devinitdata = {
125         .refresh =      60,
126         .xres =         640,
127         .yres =         480,
128         .pixclock =     39722,
129         .left_margin =  48,
130         .right_margin = 16,
131         .upper_margin = 33,
132         .lower_margin = 10,
133         .hsync_len =    96,
134         .vsync_len =    2,
135         .sync =         0,
136         .vmode =        FB_VMODE_NONINTERLACED
137 };
138
139 /* Chip generations */
140 enum {
141         rage_128,
142         rage_128_pci,
143         rage_128_pro,
144         rage_128_pro_pci,
145         rage_M3,
146         rage_M3_pci,
147         rage_M4,
148         rage_128_ultra,
149 };
150
151 /* Must match above enum */
152 static const char *r128_family[] __devinitdata = {
153         "AGP",
154         "PCI",
155         "PRO AGP",
156         "PRO PCI",
157         "M3 AGP",
158         "M3 PCI",
159         "M4 AGP",
160         "Ultra AGP",
161 };
162
163 /*
164  * PCI driver prototypes
165  */
166 static int aty128_probe(struct pci_dev *pdev,
167                                const struct pci_device_id *ent);
168 static void aty128_remove(struct pci_dev *pdev);
169 static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state);
170 static int aty128_pci_resume(struct pci_dev *pdev);
171 static int aty128_do_resume(struct pci_dev *pdev);
172
173 /* supported Rage128 chipsets */
174 static struct pci_device_id aty128_pci_tbl[] = {
175         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_LE,
176           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M3_pci },
177         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_LF,
178           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M3 },
179         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_MF,
180           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M4 },
181         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_ML,
182           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M4 },
183         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PA,
184           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
185         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PB,
186           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
187         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PC,
188           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
189         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PD,
190           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
191         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PE,
192           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
193         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PF,
194           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
195         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PG,
196           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
197         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PH,
198           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
199         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PI,
200           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
201         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PJ,
202           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
203         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PK,
204           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
205         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PL,
206           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
207         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PM,
208           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
209         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PN,
210           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
211         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PO,
212           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
213         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PP,
214           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
215         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PQ,
216           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
217         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PR,
218           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
219         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PS,
220           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
221         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PT,
222           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
223         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PU,
224           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
225         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PV,
226           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
227         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PW,
228           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
229         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PX,
230           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
231         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RE,
232           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
233         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RF,
234           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
235         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RG,
236           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
237         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RK,
238           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
239         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RL,
240           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
241         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SE,
242           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
243         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SF,
244           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
245         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SG,
246           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
247         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SH,
248           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
249         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SK,
250           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
251         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SL,
252           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
253         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SM,
254           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
255         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SN,
256           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
257         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TF,
258           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
259         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TL,
260           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
261         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TR,
262           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
263         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TS,
264           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
265         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TT,
266           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
267         { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TU,
268           PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
269         { 0, }
270 };
271
272 MODULE_DEVICE_TABLE(pci, aty128_pci_tbl);
273
274 static struct pci_driver aty128fb_driver = {
275         .name           = "aty128fb",
276         .id_table       = aty128_pci_tbl,
277         .probe          = aty128_probe,
278         .remove         = __devexit_p(aty128_remove),
279         .suspend        = aty128_pci_suspend,
280         .resume         = aty128_pci_resume,
281 };
282
283 /* packed BIOS settings */
284 #ifndef CONFIG_PPC
285 typedef struct {
286         u8 clock_chip_type;
287         u8 struct_size;
288         u8 accelerator_entry;
289         u8 VGA_entry;
290         u16 VGA_table_offset;
291         u16 POST_table_offset;
292         u16 XCLK;
293         u16 MCLK;
294         u8 num_PLL_blocks;
295         u8 size_PLL_blocks;
296         u16 PCLK_ref_freq;
297         u16 PCLK_ref_divider;
298         u32 PCLK_min_freq;
299         u32 PCLK_max_freq;
300         u16 MCLK_ref_freq;
301         u16 MCLK_ref_divider;
302         u32 MCLK_min_freq;
303         u32 MCLK_max_freq;
304         u16 XCLK_ref_freq;
305         u16 XCLK_ref_divider;
306         u32 XCLK_min_freq;
307         u32 XCLK_max_freq;
308 } __attribute__ ((packed)) PLL_BLOCK;
309 #endif /* !CONFIG_PPC */
310
311 /* onboard memory information */
312 struct aty128_meminfo {
313         u8 ML;
314         u8 MB;
315         u8 Trcd;
316         u8 Trp;
317         u8 Twr;
318         u8 CL;
319         u8 Tr2w;
320         u8 LoopLatency;
321         u8 DspOn;
322         u8 Rloop;
323         const char *name;
324 };
325
326 /* various memory configurations */
327 static const struct aty128_meminfo sdr_128   =
328         { 4, 4, 3, 3, 1, 3, 1, 16, 30, 16, "128-bit SDR SGRAM (1:1)" };
329 static const struct aty128_meminfo sdr_64    =
330         { 4, 8, 3, 3, 1, 3, 1, 17, 46, 17, "64-bit SDR SGRAM (1:1)" };
331 static const struct aty128_meminfo sdr_sgram =
332         { 4, 4, 1, 2, 1, 2, 1, 16, 24, 16, "64-bit SDR SGRAM (2:1)" };
333 static const struct aty128_meminfo ddr_sgram =
334         { 4, 4, 3, 3, 2, 3, 1, 16, 31, 16, "64-bit DDR SGRAM" };
335
336 static struct fb_fix_screeninfo aty128fb_fix __devinitdata = {
337         .id             = "ATY Rage128",
338         .type           = FB_TYPE_PACKED_PIXELS,
339         .visual         = FB_VISUAL_PSEUDOCOLOR,
340         .xpanstep       = 8,
341         .ypanstep       = 1,
342         .mmio_len       = 0x2000,
343         .accel          = FB_ACCEL_ATI_RAGE128,
344 };
345
346 static char *mode_option __devinitdata = NULL;
347
348 #ifdef CONFIG_PPC_PMAC
349 static int default_vmode __devinitdata = VMODE_1024_768_60;
350 static int default_cmode __devinitdata = CMODE_8;
351 #endif
352
353 static int default_crt_on __devinitdata = 0;
354 static int default_lcd_on __devinitdata = 1;
355
356 #ifdef CONFIG_MTRR
357 static int mtrr = 1;
358 #endif
359
360 /* PLL constants */
361 struct aty128_constants {
362         u32 ref_clk;
363         u32 ppll_min;
364         u32 ppll_max;
365         u32 ref_divider;
366         u32 xclk;
367         u32 fifo_width;
368         u32 fifo_depth;
369 };
370
371 struct aty128_crtc {
372         u32 gen_cntl;
373         u32 h_total, h_sync_strt_wid;
374         u32 v_total, v_sync_strt_wid;
375         u32 pitch;
376         u32 offset, offset_cntl;
377         u32 xoffset, yoffset;
378         u32 vxres, vyres;
379         u32 depth, bpp;
380 };
381
382 struct aty128_pll {
383         u32 post_divider;
384         u32 feedback_divider;
385         u32 vclk;
386 };
387
388 struct aty128_ddafifo {
389         u32 dda_config;
390         u32 dda_on_off;
391 };
392
393 /* register values for a specific mode */
394 struct aty128fb_par {
395         struct aty128_crtc crtc;
396         struct aty128_pll pll;
397         struct aty128_ddafifo fifo_reg;
398         u32 accel_flags;
399         struct aty128_constants constants;  /* PLL and others      */
400         void __iomem *regbase;              /* remapped mmio       */
401         u32 vram_size;                      /* onboard video ram   */
402         int chip_gen;
403         const struct aty128_meminfo *mem;   /* onboard mem info    */
404 #ifdef CONFIG_MTRR
405         struct { int vram; int vram_valid; } mtrr;
406 #endif
407         int blitter_may_be_busy;
408         int fifo_slots;                 /* free slots in FIFO (64 max) */
409
410         int     pm_reg;
411         int crt_on, lcd_on;
412         struct pci_dev *pdev;
413         struct fb_info *next;
414         int     asleep;
415         int     lock_blank;
416
417         u8      red[32];                /* see aty128fb_setcolreg */
418         u8      green[64];
419         u8      blue[32];
420         u32     pseudo_palette[16];     /* used for TRUECOLOR */
421 };
422
423
424 #define round_div(n, d) ((n+(d/2))/d)
425
426 static int aty128fb_check_var(struct fb_var_screeninfo *var,
427                               struct fb_info *info);
428 static int aty128fb_set_par(struct fb_info *info);
429 static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
430                               u_int transp, struct fb_info *info);
431 static int aty128fb_pan_display(struct fb_var_screeninfo *var,
432                            struct fb_info *fb);
433 static int aty128fb_blank(int blank, struct fb_info *fb);
434 static int aty128fb_ioctl(struct fb_info *info, u_int cmd, unsigned long arg);
435 static int aty128fb_sync(struct fb_info *info);
436
437     /*
438      *  Internal routines
439      */
440
441 static int aty128_encode_var(struct fb_var_screeninfo *var,
442                              const struct aty128fb_par *par);
443 static int aty128_decode_var(struct fb_var_screeninfo *var,
444                              struct aty128fb_par *par);
445 #if 0
446 static void __devinit aty128_get_pllinfo(struct aty128fb_par *par,
447                                       void __iomem *bios);
448 static void __devinit __iomem *aty128_map_ROM(struct pci_dev *pdev, const struct aty128fb_par *par);
449 #endif
450 static void aty128_timings(struct aty128fb_par *par);
451 static void aty128_init_engine(struct aty128fb_par *par);
452 static void aty128_reset_engine(const struct aty128fb_par *par);
453 static void aty128_flush_pixel_cache(const struct aty128fb_par *par);
454 static void do_wait_for_fifo(u16 entries, struct aty128fb_par *par);
455 static void wait_for_fifo(u16 entries, struct aty128fb_par *par);
456 static void wait_for_idle(struct aty128fb_par *par);
457 static u32 depth_to_dst(u32 depth);
458
459 #ifdef CONFIG_FB_ATY128_BACKLIGHT
460 static void aty128_bl_set_power(struct fb_info *info, int power);
461 #endif
462
463 #define BIOS_IN8(v)     (readb(bios + (v)))
464 #define BIOS_IN16(v)    (readb(bios + (v)) | \
465                           (readb(bios + (v) + 1) << 8))
466 #define BIOS_IN32(v)    (readb(bios + (v)) | \
467                           (readb(bios + (v) + 1) << 8) | \
468                           (readb(bios + (v) + 2) << 16) | \
469                           (readb(bios + (v) + 3) << 24))
470
471
472 static struct fb_ops aty128fb_ops = {
473         .owner          = THIS_MODULE,
474         .fb_check_var   = aty128fb_check_var,
475         .fb_set_par     = aty128fb_set_par,
476         .fb_setcolreg   = aty128fb_setcolreg,
477         .fb_pan_display = aty128fb_pan_display,
478         .fb_blank       = aty128fb_blank,
479         .fb_ioctl       = aty128fb_ioctl,
480         .fb_sync        = aty128fb_sync,
481         .fb_fillrect    = cfb_fillrect,
482         .fb_copyarea    = cfb_copyarea,
483         .fb_imageblit   = cfb_imageblit,
484 };
485
486     /*
487      * Functions to read from/write to the mmio registers
488      *  - endian conversions may possibly be avoided by
489      *    using the other register aperture. TODO.
490      */
491 static inline u32 _aty_ld_le32(volatile unsigned int regindex, 
492                                const struct aty128fb_par *par)
493 {
494         return readl (par->regbase + regindex);
495 }
496
497 static inline void _aty_st_le32(volatile unsigned int regindex, u32 val, 
498                                 const struct aty128fb_par *par)
499 {
500         writel (val, par->regbase + regindex);
501 }
502
503 static inline u8 _aty_ld_8(unsigned int regindex,
504                            const struct aty128fb_par *par)
505 {
506         return readb (par->regbase + regindex);
507 }
508
509 static inline void _aty_st_8(unsigned int regindex, u8 val,
510                              const struct aty128fb_par *par)
511 {
512         writeb (val, par->regbase + regindex);
513 }
514
515 #define aty_ld_le32(regindex)           _aty_ld_le32(regindex, par)
516 #define aty_st_le32(regindex, val)      _aty_st_le32(regindex, val, par)
517 #define aty_ld_8(regindex)              _aty_ld_8(regindex, par)
518 #define aty_st_8(regindex, val)         _aty_st_8(regindex, val, par)
519
520     /*
521      * Functions to read from/write to the pll registers
522      */
523
524 #define aty_ld_pll(pll_index)           _aty_ld_pll(pll_index, par)
525 #define aty_st_pll(pll_index, val)      _aty_st_pll(pll_index, val, par)
526
527
528 static u32 _aty_ld_pll(unsigned int pll_index,
529                        const struct aty128fb_par *par)
530 {       
531         aty_st_8(CLOCK_CNTL_INDEX, pll_index & 0x3F);
532         return aty_ld_le32(CLOCK_CNTL_DATA);
533 }
534
535     
536 static void _aty_st_pll(unsigned int pll_index, u32 val,
537                         const struct aty128fb_par *par)
538 {
539         aty_st_8(CLOCK_CNTL_INDEX, (pll_index & 0x3F) | PLL_WR_EN);
540         aty_st_le32(CLOCK_CNTL_DATA, val);
541 }
542
543
544 /* return true when the PLL has completed an atomic update */
545 static int aty_pll_readupdate(const struct aty128fb_par *par)
546 {
547         return !(aty_ld_pll(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R);
548 }
549
550
551 static void aty_pll_wait_readupdate(const struct aty128fb_par *par)
552 {
553         unsigned long timeout = jiffies + HZ/100; // should be more than enough
554         int reset = 1;
555
556         while (time_before(jiffies, timeout))
557                 if (aty_pll_readupdate(par)) {
558                         reset = 0;
559                         break;
560                 }
561
562         if (reset)      /* reset engine?? */
563                 printk(KERN_DEBUG "aty128fb: PLL write timeout!\n");
564 }
565
566
567 /* tell PLL to update */
568 static void aty_pll_writeupdate(const struct aty128fb_par *par)
569 {
570         aty_pll_wait_readupdate(par);
571
572         aty_st_pll(PPLL_REF_DIV,
573                    aty_ld_pll(PPLL_REF_DIV) | PPLL_ATOMIC_UPDATE_W);
574 }
575
576
577 /* write to the scratch register to test r/w functionality */
578 static int __devinit register_test(const struct aty128fb_par *par)
579 {
580         u32 val;
581         int flag = 0;
582
583         val = aty_ld_le32(BIOS_0_SCRATCH);
584
585         aty_st_le32(BIOS_0_SCRATCH, 0x55555555);
586         if (aty_ld_le32(BIOS_0_SCRATCH) == 0x55555555) {
587                 aty_st_le32(BIOS_0_SCRATCH, 0xAAAAAAAA);
588
589                 if (aty_ld_le32(BIOS_0_SCRATCH) == 0xAAAAAAAA)
590                         flag = 1; 
591         }
592
593         aty_st_le32(BIOS_0_SCRATCH, val);       // restore value
594         return flag;
595 }
596
597
598 /*
599  * Accelerator engine functions
600  */
601 static void do_wait_for_fifo(u16 entries, struct aty128fb_par *par)
602 {
603         int i;
604
605         for (;;) {
606                 for (i = 0; i < 2000000; i++) {
607                         par->fifo_slots = aty_ld_le32(GUI_STAT) & 0x0fff;
608                         if (par->fifo_slots >= entries)
609                                 return;
610                 }
611                 aty128_reset_engine(par);
612         }
613 }
614
615
616 static void wait_for_idle(struct aty128fb_par *par)
617 {
618         int i;
619
620         do_wait_for_fifo(64, par);
621
622         for (;;) {
623                 for (i = 0; i < 2000000; i++) {
624                         if (!(aty_ld_le32(GUI_STAT) & (1 << 31))) {
625                                 aty128_flush_pixel_cache(par);
626                                 par->blitter_may_be_busy = 0;
627                                 return;
628                         }
629                 }
630                 aty128_reset_engine(par);
631         }
632 }
633
634
635 static void wait_for_fifo(u16 entries, struct aty128fb_par *par)
636 {
637         if (par->fifo_slots < entries)
638                 do_wait_for_fifo(64, par);
639         par->fifo_slots -= entries;
640 }
641
642
643 static void aty128_flush_pixel_cache(const struct aty128fb_par *par)
644 {
645         int i;
646         u32 tmp;
647
648         tmp = aty_ld_le32(PC_NGUI_CTLSTAT);
649         tmp &= ~(0x00ff);
650         tmp |= 0x00ff;
651         aty_st_le32(PC_NGUI_CTLSTAT, tmp);
652
653         for (i = 0; i < 2000000; i++)
654                 if (!(aty_ld_le32(PC_NGUI_CTLSTAT) & PC_BUSY))
655                         break;
656 }
657
658
659 static void aty128_reset_engine(const struct aty128fb_par *par)
660 {
661         u32 gen_reset_cntl, clock_cntl_index, mclk_cntl;
662
663         aty128_flush_pixel_cache(par);
664
665         clock_cntl_index = aty_ld_le32(CLOCK_CNTL_INDEX);
666         mclk_cntl = aty_ld_pll(MCLK_CNTL);
667
668         aty_st_pll(MCLK_CNTL, mclk_cntl | 0x00030000);
669
670         gen_reset_cntl = aty_ld_le32(GEN_RESET_CNTL);
671         aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl | SOFT_RESET_GUI);
672         aty_ld_le32(GEN_RESET_CNTL);
673         aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl & ~(SOFT_RESET_GUI));
674         aty_ld_le32(GEN_RESET_CNTL);
675
676         aty_st_pll(MCLK_CNTL, mclk_cntl);
677         aty_st_le32(CLOCK_CNTL_INDEX, clock_cntl_index);
678         aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl);
679
680         /* use old pio mode */
681         aty_st_le32(PM4_BUFFER_CNTL, PM4_BUFFER_CNTL_NONPM4);
682
683         DBG("engine reset");
684 }
685
686
687 static void aty128_init_engine(struct aty128fb_par *par)
688 {
689         u32 pitch_value;
690
691         wait_for_idle(par);
692
693         /* 3D scaler not spoken here */
694         wait_for_fifo(1, par);
695         aty_st_le32(SCALE_3D_CNTL, 0x00000000);
696
697         aty128_reset_engine(par);
698
699         pitch_value = par->crtc.pitch;
700         if (par->crtc.bpp == 24) {
701                 pitch_value = pitch_value * 3;
702         }
703
704         wait_for_fifo(4, par);
705         /* setup engine offset registers */
706         aty_st_le32(DEFAULT_OFFSET, 0x00000000);
707
708         /* setup engine pitch registers */
709         aty_st_le32(DEFAULT_PITCH, pitch_value);
710
711         /* set the default scissor register to max dimensions */
712         aty_st_le32(DEFAULT_SC_BOTTOM_RIGHT, (0x1FFF << 16) | 0x1FFF);
713
714         /* set the drawing controls registers */
715         aty_st_le32(DP_GUI_MASTER_CNTL,
716                     GMC_SRC_PITCH_OFFSET_DEFAULT                |
717                     GMC_DST_PITCH_OFFSET_DEFAULT                |
718                     GMC_SRC_CLIP_DEFAULT                        |
719                     GMC_DST_CLIP_DEFAULT                        |
720                     GMC_BRUSH_SOLIDCOLOR                        |
721                     (depth_to_dst(par->crtc.depth) << 8)        |
722                     GMC_SRC_DSTCOLOR                    |
723                     GMC_BYTE_ORDER_MSB_TO_LSB           |
724                     GMC_DP_CONVERSION_TEMP_6500         |
725                     ROP3_PATCOPY                                |
726                     GMC_DP_SRC_RECT                             |
727                     GMC_3D_FCN_EN_CLR                   |
728                     GMC_DST_CLR_CMP_FCN_CLEAR           |
729                     GMC_AUX_CLIP_CLEAR                  |
730                     GMC_WRITE_MASK_SET);
731
732         wait_for_fifo(8, par);
733         /* clear the line drawing registers */
734         aty_st_le32(DST_BRES_ERR, 0);
735         aty_st_le32(DST_BRES_INC, 0);
736         aty_st_le32(DST_BRES_DEC, 0);
737
738         /* set brush color registers */
739         aty_st_le32(DP_BRUSH_FRGD_CLR, 0xFFFFFFFF); /* white */
740         aty_st_le32(DP_BRUSH_BKGD_CLR, 0x00000000); /* black */
741
742         /* set source color registers */
743         aty_st_le32(DP_SRC_FRGD_CLR, 0xFFFFFFFF);   /* white */
744         aty_st_le32(DP_SRC_BKGD_CLR, 0x00000000);   /* black */
745
746         /* default write mask */
747         aty_st_le32(DP_WRITE_MASK, 0xFFFFFFFF);
748
749         /* Wait for all the writes to be completed before returning */
750         wait_for_idle(par);
751 }
752
753
754 /* convert depth values to their register representation */
755 static u32 depth_to_dst(u32 depth)
756 {
757         if (depth <= 8)
758                 return DST_8BPP;
759         else if (depth <= 15)
760                 return DST_15BPP;
761         else if (depth == 16)
762                 return DST_16BPP;
763         else if (depth <= 24)
764                 return DST_24BPP;
765         else if (depth <= 32)
766                 return DST_32BPP;
767
768         return -EINVAL;
769 }
770
771 /*
772  * PLL informations retreival
773  */
774
775
776 #ifndef __sparc__
777 static void __iomem * __devinit aty128_map_ROM(const struct aty128fb_par *par, struct pci_dev *dev)
778 {
779         u16 dptr;
780         u8 rom_type;
781         void __iomem *bios;
782         size_t rom_size;
783
784         /* Fix from ATI for problem with Rage128 hardware not leaving ROM enabled */
785         unsigned int temp;
786         temp = aty_ld_le32(RAGE128_MPP_TB_CONFIG);
787         temp &= 0x00ffffffu;
788         temp |= 0x04 << 24;
789         aty_st_le32(RAGE128_MPP_TB_CONFIG, temp);
790         temp = aty_ld_le32(RAGE128_MPP_TB_CONFIG);
791
792         bios = pci_map_rom(dev, &rom_size);
793
794         if (!bios) {
795                 printk(KERN_ERR "aty128fb: ROM failed to map\n");
796                 return NULL;
797         }
798
799         /* Very simple test to make sure it appeared */
800         if (BIOS_IN16(0) != 0xaa55) {
801                 printk(KERN_DEBUG "aty128fb: Invalid ROM signature %x should "
802                         " be 0xaa55\n", BIOS_IN16(0));
803                 goto failed;
804         }
805
806         /* Look for the PCI data to check the ROM type */
807         dptr = BIOS_IN16(0x18);
808
809         /* Check the PCI data signature. If it's wrong, we still assume a normal x86 ROM
810          * for now, until I've verified this works everywhere. The goal here is more
811          * to phase out Open Firmware images.
812          *
813          * Currently, we only look at the first PCI data, we could iteratre and deal with
814          * them all, and we should use fb_bios_start relative to start of image and not
815          * relative start of ROM, but so far, I never found a dual-image ATI card
816          *
817          * typedef struct {
818          *      u32     signature;      + 0x00
819          *      u16     vendor;         + 0x04
820          *      u16     device;         + 0x06
821          *      u16     reserved_1;     + 0x08
822          *      u16     dlen;           + 0x0a
823          *      u8      drevision;      + 0x0c
824          *      u8      class_hi;       + 0x0d
825          *      u16     class_lo;       + 0x0e
826          *      u16     ilen;           + 0x10
827          *      u16     irevision;      + 0x12
828          *      u8      type;           + 0x14
829          *      u8      indicator;      + 0x15
830          *      u16     reserved_2;     + 0x16
831          * } pci_data_t;
832          */
833         if (BIOS_IN32(dptr) !=  (('R' << 24) | ('I' << 16) | ('C' << 8) | 'P')) {
834                 printk(KERN_WARNING "aty128fb: PCI DATA signature in ROM incorrect: %08x\n",
835                        BIOS_IN32(dptr));
836                 goto anyway;
837         }
838         rom_type = BIOS_IN8(dptr + 0x14);
839         switch(rom_type) {
840         case 0:
841                 printk(KERN_INFO "aty128fb: Found Intel x86 BIOS ROM Image\n");
842                 break;
843         case 1:
844                 printk(KERN_INFO "aty128fb: Found Open Firmware ROM Image\n");
845                 goto failed;
846         case 2:
847                 printk(KERN_INFO "aty128fb: Found HP PA-RISC ROM Image\n");
848                 goto failed;
849         default:
850                 printk(KERN_INFO "aty128fb: Found unknown type %d ROM Image\n", rom_type);
851                 goto failed;
852         }
853  anyway:
854         return bios;
855
856  failed:
857         pci_unmap_rom(dev, bios);
858         return NULL;
859 }
860
861 static void __devinit aty128_get_pllinfo(struct aty128fb_par *par, unsigned char __iomem *bios)
862 {
863         unsigned int bios_hdr;
864         unsigned int bios_pll;
865
866         bios_hdr = BIOS_IN16(0x48);
867         bios_pll = BIOS_IN16(bios_hdr + 0x30);
868         
869         par->constants.ppll_max = BIOS_IN32(bios_pll + 0x16);
870         par->constants.ppll_min = BIOS_IN32(bios_pll + 0x12);
871         par->constants.xclk = BIOS_IN16(bios_pll + 0x08);
872         par->constants.ref_divider = BIOS_IN16(bios_pll + 0x10);
873         par->constants.ref_clk = BIOS_IN16(bios_pll + 0x0e);
874
875         DBG("ppll_max %d ppll_min %d xclk %d ref_divider %d ref clock %d\n",
876                         par->constants.ppll_max, par->constants.ppll_min,
877                         par->constants.xclk, par->constants.ref_divider,
878                         par->constants.ref_clk);
879
880 }           
881
882 #ifdef CONFIG_X86
883 static void __iomem *  __devinit aty128_find_mem_vbios(struct aty128fb_par *par)
884 {
885         /* I simplified this code as we used to miss the signatures in
886          * a lot of case. It's now closer to XFree, we just don't check
887          * for signatures at all... Something better will have to be done
888          * if we end up having conflicts
889          */
890         u32  segstart;
891         unsigned char __iomem *rom_base = NULL;
892                                                 
893         for (segstart=0x000c0000; segstart<0x000f0000; segstart+=0x00001000) {
894                 rom_base = ioremap(segstart, 0x10000);
895                 if (rom_base == NULL)
896                         return NULL;
897                 if (readb(rom_base) == 0x55 && readb(rom_base + 1) == 0xaa)
898                         break;
899                 iounmap(rom_base);
900                 rom_base = NULL;
901         }
902         return rom_base;
903 }
904 #endif
905 #endif /* ndef(__sparc__) */
906
907 /* fill in known card constants if pll_block is not available */
908 static void __devinit aty128_timings(struct aty128fb_par *par)
909 {
910 #ifdef CONFIG_PPC_OF
911         /* instead of a table lookup, assume OF has properly
912          * setup the PLL registers and use their values
913          * to set the XCLK values and reference divider values */
914
915         u32 x_mpll_ref_fb_div;
916         u32 xclk_cntl;
917         u32 Nx, M;
918         unsigned PostDivSet[] = { 0, 1, 2, 4, 8, 3, 6, 12 };
919 #endif
920
921         if (!par->constants.ref_clk)
922                 par->constants.ref_clk = 2950;
923
924 #ifdef CONFIG_PPC_OF
925         x_mpll_ref_fb_div = aty_ld_pll(X_MPLL_REF_FB_DIV);
926         xclk_cntl = aty_ld_pll(XCLK_CNTL) & 0x7;
927         Nx = (x_mpll_ref_fb_div & 0x00ff00) >> 8;
928         M  = x_mpll_ref_fb_div & 0x0000ff;
929
930         par->constants.xclk = round_div((2 * Nx * par->constants.ref_clk),
931                                         (M * PostDivSet[xclk_cntl]));
932
933         par->constants.ref_divider =
934                 aty_ld_pll(PPLL_REF_DIV) & PPLL_REF_DIV_MASK;
935 #endif
936
937         if (!par->constants.ref_divider) {
938                 par->constants.ref_divider = 0x3b;
939
940                 aty_st_pll(X_MPLL_REF_FB_DIV, 0x004c4c1e);
941                 aty_pll_writeupdate(par);
942         }
943         aty_st_pll(PPLL_REF_DIV, par->constants.ref_divider);
944         aty_pll_writeupdate(par);
945
946         /* from documentation */
947         if (!par->constants.ppll_min)
948                 par->constants.ppll_min = 12500;
949         if (!par->constants.ppll_max)
950                 par->constants.ppll_max = 25000;    /* 23000 on some cards? */
951         if (!par->constants.xclk)
952                 par->constants.xclk = 0x1d4d;        /* same as mclk */
953
954         par->constants.fifo_width = 128;
955         par->constants.fifo_depth = 32;
956
957         switch (aty_ld_le32(MEM_CNTL) & 0x3) {
958         case 0:
959                 par->mem = &sdr_128;
960                 break;
961         case 1:
962                 par->mem = &sdr_sgram;
963                 break;
964         case 2:
965                 par->mem = &ddr_sgram;
966                 break;
967         default:
968                 par->mem = &sdr_sgram;
969         }
970 }
971
972
973
974 /*
975  * CRTC programming
976  */
977
978 /* Program the CRTC registers */
979 static void aty128_set_crtc(const struct aty128_crtc *crtc,
980                             const struct aty128fb_par *par)
981 {
982         aty_st_le32(CRTC_GEN_CNTL, crtc->gen_cntl);
983         aty_st_le32(CRTC_H_TOTAL_DISP, crtc->h_total);
984         aty_st_le32(CRTC_H_SYNC_STRT_WID, crtc->h_sync_strt_wid);
985         aty_st_le32(CRTC_V_TOTAL_DISP, crtc->v_total);
986         aty_st_le32(CRTC_V_SYNC_STRT_WID, crtc->v_sync_strt_wid);
987         aty_st_le32(CRTC_PITCH, crtc->pitch);
988         aty_st_le32(CRTC_OFFSET, crtc->offset);
989         aty_st_le32(CRTC_OFFSET_CNTL, crtc->offset_cntl);
990         /* Disable ATOMIC updating.  Is this the right place? */
991         aty_st_pll(PPLL_CNTL, aty_ld_pll(PPLL_CNTL) & ~(0x00030000));
992 }
993
994
995 static int aty128_var_to_crtc(const struct fb_var_screeninfo *var,
996                               struct aty128_crtc *crtc,
997                               const struct aty128fb_par *par)
998 {
999         u32 xres, yres, vxres, vyres, xoffset, yoffset, bpp, dst;
1000         u32 left, right, upper, lower, hslen, vslen, sync, vmode;
1001         u32 h_total, h_disp, h_sync_strt, h_sync_wid, h_sync_pol;
1002         u32 v_total, v_disp, v_sync_strt, v_sync_wid, v_sync_pol, c_sync;
1003         u32 depth, bytpp;
1004         u8 mode_bytpp[7] = { 0, 0, 1, 2, 2, 3, 4 };
1005
1006         /* input */
1007         xres = var->xres;
1008         yres = var->yres;
1009         vxres   = var->xres_virtual;
1010         vyres   = var->yres_virtual;
1011         xoffset = var->xoffset;
1012         yoffset = var->yoffset;
1013         bpp   = var->bits_per_pixel;
1014         left  = var->left_margin;
1015         right = var->right_margin;
1016         upper = var->upper_margin;
1017         lower = var->lower_margin;
1018         hslen = var->hsync_len;
1019         vslen = var->vsync_len;
1020         sync  = var->sync;
1021         vmode = var->vmode;
1022
1023         if (bpp != 16)
1024                 depth = bpp;
1025         else
1026                 depth = (var->green.length == 6) ? 16 : 15;
1027
1028         /* check for mode eligibility
1029          * accept only non interlaced modes */
1030         if ((vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
1031                 return -EINVAL;
1032
1033         /* convert (and round up) and validate */
1034         xres = (xres + 7) & ~7;
1035         xoffset = (xoffset + 7) & ~7;
1036
1037         if (vxres < xres + xoffset)
1038                 vxres = xres + xoffset;
1039
1040         if (vyres < yres + yoffset)
1041                 vyres = yres + yoffset;
1042
1043         /* convert depth into ATI register depth */
1044         dst = depth_to_dst(depth);
1045
1046         if (dst == -EINVAL) {
1047                 printk(KERN_ERR "aty128fb: Invalid depth or RGBA\n");
1048                 return -EINVAL;
1049         }
1050
1051         /* convert register depth to bytes per pixel */
1052         bytpp = mode_bytpp[dst];
1053
1054         /* make sure there is enough video ram for the mode */
1055         if ((u32)(vxres * vyres * bytpp) > par->vram_size) {
1056                 printk(KERN_ERR "aty128fb: Not enough memory for mode\n");
1057                 return -EINVAL;
1058         }
1059
1060         h_disp = (xres >> 3) - 1;
1061         h_total = (((xres + right + hslen + left) >> 3) - 1) & 0xFFFFL;
1062
1063         v_disp = yres - 1;
1064         v_total = (yres + upper + vslen + lower - 1) & 0xFFFFL;
1065
1066         /* check to make sure h_total and v_total are in range */
1067         if (((h_total >> 3) - 1) > 0x1ff || (v_total - 1) > 0x7FF) {
1068                 printk(KERN_ERR "aty128fb: invalid width ranges\n");
1069                 return -EINVAL;
1070         }
1071
1072         h_sync_wid = (hslen + 7) >> 3;
1073         if (h_sync_wid == 0)
1074                 h_sync_wid = 1;
1075         else if (h_sync_wid > 0x3f)        /* 0x3f = max hwidth */
1076                 h_sync_wid = 0x3f;
1077
1078         h_sync_strt = (h_disp << 3) + right;
1079
1080         v_sync_wid = vslen;
1081         if (v_sync_wid == 0)
1082                 v_sync_wid = 1;
1083         else if (v_sync_wid > 0x1f)        /* 0x1f = max vwidth */
1084                 v_sync_wid = 0x1f;
1085     
1086         v_sync_strt = v_disp + lower;
1087
1088         h_sync_pol = sync & FB_SYNC_HOR_HIGH_ACT ? 0 : 1;
1089         v_sync_pol = sync & FB_SYNC_VERT_HIGH_ACT ? 0 : 1;
1090     
1091         c_sync = sync & FB_SYNC_COMP_HIGH_ACT ? (1 << 4) : 0;
1092
1093         crtc->gen_cntl = 0x3000000L | c_sync | (dst << 8);
1094
1095         crtc->h_total = h_total | (h_disp << 16);
1096         crtc->v_total = v_total | (v_disp << 16);
1097
1098         crtc->h_sync_strt_wid = h_sync_strt | (h_sync_wid << 16) |
1099                 (h_sync_pol << 23);
1100         crtc->v_sync_strt_wid = v_sync_strt | (v_sync_wid << 16) |
1101                 (v_sync_pol << 23);
1102
1103         crtc->pitch = vxres >> 3;
1104
1105         crtc->offset = 0;
1106
1107         if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW)
1108                 crtc->offset_cntl = 0x00010000;
1109         else
1110                 crtc->offset_cntl = 0;
1111
1112         crtc->vxres = vxres;
1113         crtc->vyres = vyres;
1114         crtc->xoffset = xoffset;
1115         crtc->yoffset = yoffset;
1116         crtc->depth = depth;
1117         crtc->bpp = bpp;
1118
1119         return 0;
1120 }
1121
1122
1123 static int aty128_pix_width_to_var(int pix_width, struct fb_var_screeninfo *var)
1124 {
1125
1126         /* fill in pixel info */
1127         var->red.msb_right = 0;
1128         var->green.msb_right = 0;
1129         var->blue.offset = 0;
1130         var->blue.msb_right = 0;
1131         var->transp.offset = 0;
1132         var->transp.length = 0;
1133         var->transp.msb_right = 0;
1134         switch (pix_width) {
1135         case CRTC_PIX_WIDTH_8BPP:
1136                 var->bits_per_pixel = 8;
1137                 var->red.offset = 0;
1138                 var->red.length = 8;
1139                 var->green.offset = 0;
1140                 var->green.length = 8;
1141                 var->blue.length = 8;
1142                 break;
1143         case CRTC_PIX_WIDTH_15BPP:
1144                 var->bits_per_pixel = 16;
1145                 var->red.offset = 10;
1146                 var->red.length = 5;
1147                 var->green.offset = 5;
1148                 var->green.length = 5;
1149                 var->blue.length = 5;
1150                 break;
1151         case CRTC_PIX_WIDTH_16BPP:
1152                 var->bits_per_pixel = 16;
1153                 var->red.offset = 11;
1154                 var->red.length = 5;
1155                 var->green.offset = 5;
1156                 var->green.length = 6;
1157                 var->blue.length = 5;
1158                 break;
1159         case CRTC_PIX_WIDTH_24BPP:
1160                 var->bits_per_pixel = 24;
1161                 var->red.offset = 16;
1162                 var->red.length = 8;
1163                 var->green.offset = 8;
1164                 var->green.length = 8;
1165                 var->blue.length = 8;
1166                 break;
1167         case CRTC_PIX_WIDTH_32BPP:
1168                 var->bits_per_pixel = 32;
1169                 var->red.offset = 16;
1170                 var->red.length = 8;
1171                 var->green.offset = 8;
1172                 var->green.length = 8;
1173                 var->blue.length = 8;
1174                 var->transp.offset = 24;
1175                 var->transp.length = 8;
1176                 break;
1177         default:
1178                 printk(KERN_ERR "aty128fb: Invalid pixel width\n");
1179                 return -EINVAL;
1180         }
1181
1182         return 0;
1183 }
1184
1185
1186 static int aty128_crtc_to_var(const struct aty128_crtc *crtc,
1187                               struct fb_var_screeninfo *var)
1188 {
1189         u32 xres, yres, left, right, upper, lower, hslen, vslen, sync;
1190         u32 h_total, h_disp, h_sync_strt, h_sync_dly, h_sync_wid, h_sync_pol;
1191         u32 v_total, v_disp, v_sync_strt, v_sync_wid, v_sync_pol, c_sync;
1192         u32 pix_width;
1193
1194         /* fun with masking */
1195         h_total     = crtc->h_total & 0x1ff;
1196         h_disp      = (crtc->h_total >> 16) & 0xff;
1197         h_sync_strt = (crtc->h_sync_strt_wid >> 3) & 0x1ff;
1198         h_sync_dly  = crtc->h_sync_strt_wid & 0x7;
1199         h_sync_wid  = (crtc->h_sync_strt_wid >> 16) & 0x3f;
1200         h_sync_pol  = (crtc->h_sync_strt_wid >> 23) & 0x1;
1201         v_total     = crtc->v_total & 0x7ff;
1202         v_disp      = (crtc->v_total >> 16) & 0x7ff;
1203         v_sync_strt = crtc->v_sync_strt_wid & 0x7ff;
1204         v_sync_wid  = (crtc->v_sync_strt_wid >> 16) & 0x1f;
1205         v_sync_pol  = (crtc->v_sync_strt_wid >> 23) & 0x1;
1206         c_sync      = crtc->gen_cntl & CRTC_CSYNC_EN ? 1 : 0;
1207         pix_width   = crtc->gen_cntl & CRTC_PIX_WIDTH_MASK;
1208
1209         /* do conversions */
1210         xres  = (h_disp + 1) << 3;
1211         yres  = v_disp + 1;
1212         left  = ((h_total - h_sync_strt - h_sync_wid) << 3) - h_sync_dly;
1213         right = ((h_sync_strt - h_disp) << 3) + h_sync_dly;
1214         hslen = h_sync_wid << 3;
1215         upper = v_total - v_sync_strt - v_sync_wid;
1216         lower = v_sync_strt - v_disp;
1217         vslen = v_sync_wid;
1218         sync  = (h_sync_pol ? 0 : FB_SYNC_HOR_HIGH_ACT) |
1219                 (v_sync_pol ? 0 : FB_SYNC_VERT_HIGH_ACT) |
1220                 (c_sync ? FB_SYNC_COMP_HIGH_ACT : 0);
1221
1222         aty128_pix_width_to_var(pix_width, var);
1223
1224         var->xres = xres;
1225         var->yres = yres;
1226         var->xres_virtual = crtc->vxres;
1227         var->yres_virtual = crtc->vyres;
1228         var->xoffset = crtc->xoffset;
1229         var->yoffset = crtc->yoffset;
1230         var->left_margin  = left;
1231         var->right_margin = right;
1232         var->upper_margin = upper;
1233         var->lower_margin = lower;
1234         var->hsync_len = hslen;
1235         var->vsync_len = vslen;
1236         var->sync  = sync;
1237         var->vmode = FB_VMODE_NONINTERLACED;
1238
1239         return 0;
1240 }
1241
1242 static void aty128_set_crt_enable(struct aty128fb_par *par, int on)
1243 {
1244         if (on) {
1245                 aty_st_le32(CRTC_EXT_CNTL, aty_ld_le32(CRTC_EXT_CNTL) | CRT_CRTC_ON);
1246                 aty_st_le32(DAC_CNTL, (aty_ld_le32(DAC_CNTL) | DAC_PALETTE2_SNOOP_EN));
1247         } else
1248                 aty_st_le32(CRTC_EXT_CNTL, aty_ld_le32(CRTC_EXT_CNTL) & ~CRT_CRTC_ON);
1249 }
1250
1251 static void aty128_set_lcd_enable(struct aty128fb_par *par, int on)
1252 {
1253         u32 reg;
1254 #ifdef CONFIG_FB_ATY128_BACKLIGHT
1255         struct fb_info *info = pci_get_drvdata(par->pdev);
1256 #endif
1257
1258         if (on) {
1259                 reg = aty_ld_le32(LVDS_GEN_CNTL);
1260                 reg |= LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION;
1261                 reg &= ~LVDS_DISPLAY_DIS;
1262                 aty_st_le32(LVDS_GEN_CNTL, reg);
1263 #ifdef CONFIG_FB_ATY128_BACKLIGHT
1264                 aty128_bl_set_power(info, FB_BLANK_UNBLANK);
1265 #endif  
1266         } else {
1267 #ifdef CONFIG_FB_ATY128_BACKLIGHT
1268                 aty128_bl_set_power(info, FB_BLANK_POWERDOWN);
1269 #endif  
1270                 reg = aty_ld_le32(LVDS_GEN_CNTL);
1271                 reg |= LVDS_DISPLAY_DIS;
1272                 aty_st_le32(LVDS_GEN_CNTL, reg);
1273                 mdelay(100);
1274                 reg &= ~(LVDS_ON /*| LVDS_EN*/);
1275                 aty_st_le32(LVDS_GEN_CNTL, reg);
1276         }
1277 }
1278
1279 static void aty128_set_pll(struct aty128_pll *pll, const struct aty128fb_par *par)
1280 {
1281         u32 div3;
1282
1283         unsigned char post_conv[] =     /* register values for post dividers */
1284         { 2, 0, 1, 4, 2, 2, 6, 2, 3, 2, 2, 2, 7 };
1285
1286         /* select PPLL_DIV_3 */
1287         aty_st_le32(CLOCK_CNTL_INDEX, aty_ld_le32(CLOCK_CNTL_INDEX) | (3 << 8));
1288
1289         /* reset PLL */
1290         aty_st_pll(PPLL_CNTL,
1291                    aty_ld_pll(PPLL_CNTL) | PPLL_RESET | PPLL_ATOMIC_UPDATE_EN);
1292
1293         /* write the reference divider */
1294         aty_pll_wait_readupdate(par);
1295         aty_st_pll(PPLL_REF_DIV, par->constants.ref_divider & 0x3ff);
1296         aty_pll_writeupdate(par);
1297
1298         div3 = aty_ld_pll(PPLL_DIV_3);
1299         div3 &= ~PPLL_FB3_DIV_MASK;
1300         div3 |= pll->feedback_divider;
1301         div3 &= ~PPLL_POST3_DIV_MASK;
1302         div3 |= post_conv[pll->post_divider] << 16;
1303
1304         /* write feedback and post dividers */
1305         aty_pll_wait_readupdate(par);
1306         aty_st_pll(PPLL_DIV_3, div3);
1307         aty_pll_writeupdate(par);
1308
1309         aty_pll_wait_readupdate(par);
1310         aty_st_pll(HTOTAL_CNTL, 0);     /* no horiz crtc adjustment */
1311         aty_pll_writeupdate(par);
1312
1313         /* clear the reset, just in case */
1314         aty_st_pll(PPLL_CNTL, aty_ld_pll(PPLL_CNTL) & ~PPLL_RESET);
1315 }
1316
1317
1318 static int aty128_var_to_pll(u32 period_in_ps, struct aty128_pll *pll,
1319                              const struct aty128fb_par *par)
1320 {
1321         const struct aty128_constants c = par->constants;
1322         unsigned char post_dividers[] = {1,2,4,8,3,6,12};
1323         u32 output_freq;
1324         u32 vclk;        /* in .01 MHz */
1325         int i = 0;
1326         u32 n, d;
1327
1328         vclk = 100000000 / period_in_ps;        /* convert units to 10 kHz */
1329
1330         /* adjust pixel clock if necessary */
1331         if (vclk > c.ppll_max)
1332                 vclk = c.ppll_max;
1333         if (vclk * 12 < c.ppll_min)
1334                 vclk = c.ppll_min/12;
1335
1336         pll->post_divider = -1;
1337
1338         /* now, find an acceptable divider */
1339         for (i = 0; i < sizeof(post_dividers); i++) {
1340                 output_freq = post_dividers[i] * vclk;
1341                 if (output_freq >= c.ppll_min && output_freq <= c.ppll_max) {
1342                         pll->post_divider = post_dividers[i];
1343                         break;
1344                 }
1345         }
1346
1347         if (pll->post_divider < 0)
1348                 return -EINVAL;
1349
1350         /* calculate feedback divider */
1351         n = c.ref_divider * output_freq;
1352         d = c.ref_clk;
1353
1354         pll->feedback_divider = round_div(n, d);
1355         pll->vclk = vclk;
1356
1357         DBG("post %d feedback %d vlck %d output %d ref_divider %d "
1358             "vclk_per: %d\n", pll->post_divider,
1359             pll->feedback_divider, vclk, output_freq,
1360             c.ref_divider, period_in_ps);
1361
1362         return 0;
1363 }
1364
1365
1366 static int aty128_pll_to_var(const struct aty128_pll *pll, struct fb_var_screeninfo *var)
1367 {
1368         var->pixclock = 100000000 / pll->vclk;
1369
1370         return 0;
1371 }
1372
1373
1374 static void aty128_set_fifo(const struct aty128_ddafifo *dsp,
1375                             const struct aty128fb_par *par)
1376 {
1377         aty_st_le32(DDA_CONFIG, dsp->dda_config);
1378         aty_st_le32(DDA_ON_OFF, dsp->dda_on_off);
1379 }
1380
1381
1382 static int aty128_ddafifo(struct aty128_ddafifo *dsp,
1383                           const struct aty128_pll *pll,
1384                           u32 depth,
1385                           const struct aty128fb_par *par)
1386 {
1387         const struct aty128_meminfo *m = par->mem;
1388         u32 xclk = par->constants.xclk;
1389         u32 fifo_width = par->constants.fifo_width;
1390         u32 fifo_depth = par->constants.fifo_depth;
1391         s32 x, b, p, ron, roff;
1392         u32 n, d, bpp;
1393
1394         /* round up to multiple of 8 */
1395         bpp = (depth+7) & ~7;
1396
1397         n = xclk * fifo_width;
1398         d = pll->vclk * bpp;
1399         x = round_div(n, d);
1400
1401         ron = 4 * m->MB +
1402                 3 * ((m->Trcd - 2 > 0) ? m->Trcd - 2 : 0) +
1403                 2 * m->Trp +
1404                 m->Twr +
1405                 m->CL +
1406                 m->Tr2w +
1407                 x;
1408
1409         DBG("x %x\n", x);
1410
1411         b = 0;
1412         while (x) {
1413                 x >>= 1;
1414                 b++;
1415         }
1416         p = b + 1;
1417
1418         ron <<= (11 - p);
1419
1420         n <<= (11 - p);
1421         x = round_div(n, d);
1422         roff = x * (fifo_depth - 4);
1423
1424         if ((ron + m->Rloop) >= roff) {
1425                 printk(KERN_ERR "aty128fb: Mode out of range!\n");
1426                 return -EINVAL;
1427         }
1428
1429         DBG("p: %x rloop: %x x: %x ron: %x roff: %x\n",
1430             p, m->Rloop, x, ron, roff);
1431
1432         dsp->dda_config = p << 16 | m->Rloop << 20 | x;
1433         dsp->dda_on_off = ron << 16 | roff;
1434
1435         return 0;
1436 }
1437
1438
1439 /*
1440  * This actually sets the video mode.
1441  */
1442 static int aty128fb_set_par(struct fb_info *info)
1443
1444         struct aty128fb_par *par = info->par;
1445         u32 config;
1446         int err;
1447
1448         if ((err = aty128_decode_var(&info->var, par)) != 0)
1449                 return err;
1450
1451         if (par->blitter_may_be_busy)
1452                 wait_for_idle(par);
1453
1454         /* clear all registers that may interfere with mode setting */
1455         aty_st_le32(OVR_CLR, 0);
1456         aty_st_le32(OVR_WID_LEFT_RIGHT, 0);
1457         aty_st_le32(OVR_WID_TOP_BOTTOM, 0);
1458         aty_st_le32(OV0_SCALE_CNTL, 0);
1459         aty_st_le32(MPP_TB_CONFIG, 0);
1460         aty_st_le32(MPP_GP_CONFIG, 0);
1461         aty_st_le32(SUBPIC_CNTL, 0);
1462         aty_st_le32(VIPH_CONTROL, 0);
1463         aty_st_le32(I2C_CNTL_1, 0);         /* turn off i2c */
1464         aty_st_le32(GEN_INT_CNTL, 0);   /* turn off interrupts */
1465         aty_st_le32(CAP0_TRIG_CNTL, 0);
1466         aty_st_le32(CAP1_TRIG_CNTL, 0);
1467
1468         aty_st_8(CRTC_EXT_CNTL + 1, 4); /* turn video off */
1469
1470         aty128_set_crtc(&par->crtc, par);
1471         aty128_set_pll(&par->pll, par);
1472         aty128_set_fifo(&par->fifo_reg, par);
1473
1474         config = aty_ld_le32(CONFIG_CNTL) & ~3;
1475
1476 #if defined(__BIG_ENDIAN)
1477         if (par->crtc.bpp == 32)
1478                 config |= 2;    /* make aperture do 32 bit swapping */
1479         else if (par->crtc.bpp == 16)
1480                 config |= 1;    /* make aperture do 16 bit swapping */
1481 #endif
1482
1483         aty_st_le32(CONFIG_CNTL, config);
1484         aty_st_8(CRTC_EXT_CNTL + 1, 0); /* turn the video back on */
1485
1486         info->fix.line_length = (par->crtc.vxres * par->crtc.bpp) >> 3;
1487         info->fix.visual = par->crtc.bpp == 8 ? FB_VISUAL_PSEUDOCOLOR
1488                 : FB_VISUAL_DIRECTCOLOR;
1489
1490         if (par->chip_gen == rage_M3) {
1491                 aty128_set_crt_enable(par, par->crt_on);
1492                 aty128_set_lcd_enable(par, par->lcd_on);
1493         }
1494         if (par->accel_flags & FB_ACCELF_TEXT)
1495                 aty128_init_engine(par);
1496
1497 #ifdef CONFIG_BOOTX_TEXT
1498         btext_update_display(info->fix.smem_start,
1499                              (((par->crtc.h_total>>16) & 0xff)+1)*8,
1500                              ((par->crtc.v_total>>16) & 0x7ff)+1,
1501                              par->crtc.bpp,
1502                              par->crtc.vxres*par->crtc.bpp/8);
1503 #endif /* CONFIG_BOOTX_TEXT */
1504
1505         return 0;
1506 }
1507
1508 /*
1509  *  encode/decode the User Defined Part of the Display
1510  */
1511
1512 static int aty128_decode_var(struct fb_var_screeninfo *var, struct aty128fb_par *par)
1513 {
1514         int err;
1515         struct aty128_crtc crtc;
1516         struct aty128_pll pll;
1517         struct aty128_ddafifo fifo_reg;
1518
1519         if ((err = aty128_var_to_crtc(var, &crtc, par)))
1520                 return err;
1521
1522         if ((err = aty128_var_to_pll(var->pixclock, &pll, par)))
1523                 return err;
1524
1525         if ((err = aty128_ddafifo(&fifo_reg, &pll, crtc.depth, par)))
1526                 return err;
1527
1528         par->crtc = crtc;
1529         par->pll = pll;
1530         par->fifo_reg = fifo_reg;
1531         par->accel_flags = var->accel_flags;
1532
1533         return 0;
1534 }
1535
1536
1537 static int aty128_encode_var(struct fb_var_screeninfo *var,
1538                              const struct aty128fb_par *par)
1539 {
1540         int err;
1541
1542         if ((err = aty128_crtc_to_var(&par->crtc, var)))
1543                 return err;
1544
1545         if ((err = aty128_pll_to_var(&par->pll, var)))
1546                 return err;
1547
1548         var->nonstd = 0;
1549         var->activate = 0;
1550
1551         var->height = -1;
1552         var->width = -1;
1553         var->accel_flags = par->accel_flags;
1554
1555         return 0;
1556 }           
1557
1558
1559 static int aty128fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1560 {
1561         struct aty128fb_par par;
1562         int err;
1563
1564         par = *(struct aty128fb_par *)info->par;
1565         if ((err = aty128_decode_var(var, &par)) != 0)
1566                 return err;
1567         aty128_encode_var(var, &par);
1568         return 0;
1569 }
1570
1571
1572 /*
1573  *  Pan or Wrap the Display
1574  */
1575 static int aty128fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fb) 
1576 {
1577         struct aty128fb_par *par = fb->par;
1578         u32 xoffset, yoffset;
1579         u32 offset;
1580         u32 xres, yres;
1581
1582         xres = (((par->crtc.h_total >> 16) & 0xff) + 1) << 3;
1583         yres = ((par->crtc.v_total >> 16) & 0x7ff) + 1;
1584
1585         xoffset = (var->xoffset +7) & ~7;
1586         yoffset = var->yoffset;
1587
1588         if (xoffset+xres > par->crtc.vxres || yoffset+yres > par->crtc.vyres)
1589                 return -EINVAL;
1590
1591         par->crtc.xoffset = xoffset;
1592         par->crtc.yoffset = yoffset;
1593
1594         offset = ((yoffset * par->crtc.vxres + xoffset)*(par->crtc.bpp >> 3)) & ~7;
1595
1596         if (par->crtc.bpp == 24)
1597                 offset += 8 * (offset % 3); /* Must be multiple of 8 and 3 */
1598
1599         aty_st_le32(CRTC_OFFSET, offset);
1600
1601         return 0;
1602 }
1603
1604
1605 /*
1606  *  Helper function to store a single palette register
1607  */
1608 static void aty128_st_pal(u_int regno, u_int red, u_int green, u_int blue,
1609                           struct aty128fb_par *par)
1610 {
1611         if (par->chip_gen == rage_M3) {
1612 #if 0
1613                 /* Note: For now, on M3, we set palette on both heads, which may
1614                  * be useless. Can someone with a M3 check this ?
1615                  * 
1616                  * This code would still be useful if using the second CRTC to 
1617                  * do mirroring
1618                  */
1619
1620                 aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PALETTE_ACCESS_CNTL);
1621                 aty_st_8(PALETTE_INDEX, regno);
1622                 aty_st_le32(PALETTE_DATA, (red<<16)|(green<<8)|blue);
1623 #endif
1624                 aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & ~DAC_PALETTE_ACCESS_CNTL);
1625         }
1626
1627         aty_st_8(PALETTE_INDEX, regno);
1628         aty_st_le32(PALETTE_DATA, (red<<16)|(green<<8)|blue);
1629 }
1630
1631 static int aty128fb_sync(struct fb_info *info)
1632 {
1633         struct aty128fb_par *par = info->par;
1634
1635         if (par->blitter_may_be_busy)
1636                 wait_for_idle(par);
1637         return 0;
1638 }
1639
1640 #ifndef MODULE
1641 static int __devinit aty128fb_setup(char *options)
1642 {
1643         char *this_opt;
1644
1645         if (!options || !*options)
1646                 return 0;
1647
1648         while ((this_opt = strsep(&options, ",")) != NULL) {
1649                 if (!strncmp(this_opt, "lcd:", 4)) {
1650                         default_lcd_on = simple_strtoul(this_opt+4, NULL, 0);
1651                         continue;
1652                 } else if (!strncmp(this_opt, "crt:", 4)) {
1653                         default_crt_on = simple_strtoul(this_opt+4, NULL, 0);
1654                         continue;
1655                 }
1656 #ifdef CONFIG_MTRR
1657                 if(!strncmp(this_opt, "nomtrr", 6)) {
1658                         mtrr = 0;
1659                         continue;
1660                 }
1661 #endif
1662 #ifdef CONFIG_PPC_PMAC
1663                 /* vmode and cmode deprecated */
1664                 if (!strncmp(this_opt, "vmode:", 6)) {
1665                         unsigned int vmode = simple_strtoul(this_opt+6, NULL, 0);
1666                         if (vmode > 0 && vmode <= VMODE_MAX)
1667                                 default_vmode = vmode;
1668                         continue;
1669                 } else if (!strncmp(this_opt, "cmode:", 6)) {
1670                         unsigned int cmode = simple_strtoul(this_opt+6, NULL, 0);
1671                         switch (cmode) {
1672                         case 0:
1673                         case 8:
1674                                 default_cmode = CMODE_8;
1675                                 break;
1676                         case 15:
1677                         case 16:
1678                                 default_cmode = CMODE_16;
1679                                 break;
1680                         case 24:
1681                         case 32:
1682                                 default_cmode = CMODE_32;
1683                                 break;
1684                         }
1685                         continue;
1686                 }
1687 #endif /* CONFIG_PPC_PMAC */
1688                 mode_option = this_opt;
1689         }
1690         return 0;
1691 }
1692 #endif  /*  MODULE  */
1693
1694 /* Backlight */
1695 #ifdef CONFIG_FB_ATY128_BACKLIGHT
1696 #define MAX_LEVEL 0xFF
1697
1698 static struct backlight_properties aty128_bl_data;
1699
1700 /* Call with fb_info->bl_mutex held */
1701 static int aty128_bl_get_level_brightness(struct aty128fb_par *par,
1702                 int level)
1703 {
1704         struct fb_info *info = pci_get_drvdata(par->pdev);
1705         int atylevel;
1706
1707         /* Get and convert the value */
1708         atylevel = MAX_LEVEL -
1709                 (info->bl_curve[level] * FB_BACKLIGHT_MAX / MAX_LEVEL);
1710
1711         if (atylevel < 0)
1712                 atylevel = 0;
1713         else if (atylevel > MAX_LEVEL)
1714                 atylevel = MAX_LEVEL;
1715
1716         return atylevel;
1717 }
1718
1719 /* We turn off the LCD completely instead of just dimming the backlight.
1720  * This provides greater power saving and the display is useless without
1721  * backlight anyway
1722  */
1723 #define BACKLIGHT_LVDS_OFF
1724 /* That one prevents proper CRT output with LCD off */
1725 #undef BACKLIGHT_DAC_OFF
1726
1727 /* Call with fb_info->bl_mutex held */
1728 static int __aty128_bl_update_status(struct backlight_device *bd)
1729 {
1730         struct aty128fb_par *par = class_get_devdata(&bd->class_dev);
1731         unsigned int reg = aty_ld_le32(LVDS_GEN_CNTL);
1732         int level;
1733
1734         if (bd->props->power != FB_BLANK_UNBLANK ||
1735             bd->props->fb_blank != FB_BLANK_UNBLANK ||
1736             !par->lcd_on)
1737                 level = 0;
1738         else
1739                 level = bd->props->brightness;
1740
1741         reg |= LVDS_BL_MOD_EN | LVDS_BLON;
1742         if (level > 0) {
1743                 reg |= LVDS_DIGION;
1744                 if (!(reg & LVDS_ON)) {
1745                         reg &= ~LVDS_BLON;
1746                         aty_st_le32(LVDS_GEN_CNTL, reg);
1747                         aty_ld_le32(LVDS_GEN_CNTL);
1748                         mdelay(10);
1749                         reg |= LVDS_BLON;
1750                         aty_st_le32(LVDS_GEN_CNTL, reg);
1751                 }
1752                 reg &= ~LVDS_BL_MOD_LEVEL_MASK;
1753                 reg |= (aty128_bl_get_level_brightness(par, level) << LVDS_BL_MOD_LEVEL_SHIFT);
1754 #ifdef BACKLIGHT_LVDS_OFF
1755                 reg |= LVDS_ON | LVDS_EN;
1756                 reg &= ~LVDS_DISPLAY_DIS;
1757 #endif
1758                 aty_st_le32(LVDS_GEN_CNTL, reg);
1759 #ifdef BACKLIGHT_DAC_OFF
1760                 aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & (~DAC_PDWN));
1761 #endif
1762         } else {
1763                 reg &= ~LVDS_BL_MOD_LEVEL_MASK;
1764                 reg |= (aty128_bl_get_level_brightness(par, 0) << LVDS_BL_MOD_LEVEL_SHIFT);
1765 #ifdef BACKLIGHT_LVDS_OFF
1766                 reg |= LVDS_DISPLAY_DIS;
1767                 aty_st_le32(LVDS_GEN_CNTL, reg);
1768                 aty_ld_le32(LVDS_GEN_CNTL);
1769                 udelay(10);
1770                 reg &= ~(LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION);
1771 #endif
1772                 aty_st_le32(LVDS_GEN_CNTL, reg);
1773 #ifdef BACKLIGHT_DAC_OFF
1774                 aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PDWN);
1775 #endif
1776         }
1777
1778         return 0;
1779 }
1780
1781 static int aty128_bl_update_status(struct backlight_device *bd)
1782 {
1783         struct aty128fb_par *par = class_get_devdata(&bd->class_dev);
1784         struct fb_info *info = pci_get_drvdata(par->pdev);
1785         int ret;
1786
1787         mutex_lock(&info->bl_mutex);
1788         ret = __aty128_bl_update_status(bd);
1789         mutex_unlock(&info->bl_mutex);
1790
1791         return ret;
1792 }
1793
1794 static int aty128_bl_get_brightness(struct backlight_device *bd)
1795 {
1796         return bd->props->brightness;
1797 }
1798
1799 static struct backlight_properties aty128_bl_data = {
1800         .owner          = THIS_MODULE,
1801         .get_brightness = aty128_bl_get_brightness,
1802         .update_status  = aty128_bl_update_status,
1803         .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
1804 };
1805
1806 static void aty128_bl_set_power(struct fb_info *info, int power)
1807 {
1808         mutex_lock(&info->bl_mutex);
1809
1810         if (info->bl_dev) {
1811                 down(&info->bl_dev->sem);
1812                 info->bl_dev->props->power = power;
1813                 __aty128_bl_update_status(info->bl_dev);
1814                 up(&info->bl_dev->sem);
1815         }
1816
1817         mutex_unlock(&info->bl_mutex);
1818 }
1819
1820 static void aty128_bl_init(struct aty128fb_par *par)
1821 {
1822         struct fb_info *info = pci_get_drvdata(par->pdev);
1823         struct backlight_device *bd;
1824         char name[12];
1825
1826         /* Could be extended to Rage128Pro LVDS output too */
1827         if (par->chip_gen != rage_M3)
1828                 return;
1829
1830 #ifdef CONFIG_PMAC_BACKLIGHT
1831         if (!pmac_has_backlight_type("ati"))
1832                 return;
1833 #endif
1834
1835         snprintf(name, sizeof(name), "aty128bl%d", info->node);
1836
1837         bd = backlight_device_register(name, info->dev, par, &aty128_bl_data);
1838         if (IS_ERR(bd)) {
1839                 info->bl_dev = NULL;
1840                 printk(KERN_WARNING "aty128: Backlight registration failed\n");
1841                 goto error;
1842         }
1843
1844         mutex_lock(&info->bl_mutex);
1845         info->bl_dev = bd;
1846         fb_bl_default_curve(info, 0,
1847                  63 * FB_BACKLIGHT_MAX / MAX_LEVEL,
1848                 219 * FB_BACKLIGHT_MAX / MAX_LEVEL);
1849         mutex_unlock(&info->bl_mutex);
1850
1851         down(&bd->sem);
1852         bd->props->brightness = aty128_bl_data.max_brightness;
1853         bd->props->power = FB_BLANK_UNBLANK;
1854         bd->props->update_status(bd);
1855         up(&bd->sem);
1856
1857 #ifdef CONFIG_PMAC_BACKLIGHT
1858         mutex_lock(&pmac_backlight_mutex);
1859         if (!pmac_backlight)
1860                 pmac_backlight = bd;
1861         mutex_unlock(&pmac_backlight_mutex);
1862 #endif
1863
1864         printk("aty128: Backlight initialized (%s)\n", name);
1865
1866         return;
1867
1868 error:
1869         return;
1870 }
1871
1872 static void aty128_bl_exit(struct aty128fb_par *par)
1873 {
1874         struct fb_info *info = pci_get_drvdata(par->pdev);
1875
1876 #ifdef CONFIG_PMAC_BACKLIGHT
1877         mutex_lock(&pmac_backlight_mutex);
1878 #endif
1879
1880         mutex_lock(&info->bl_mutex);
1881         if (info->bl_dev) {
1882 #ifdef CONFIG_PMAC_BACKLIGHT
1883                 if (pmac_backlight == info->bl_dev)
1884                         pmac_backlight = NULL;
1885 #endif
1886
1887                 backlight_device_unregister(info->bl_dev);
1888                 info->bl_dev = NULL;
1889
1890                 printk("aty128: Backlight unloaded\n");
1891         }
1892         mutex_unlock(&info->bl_mutex);
1893
1894 #ifdef CONFIG_PMAC_BACKLIGHT
1895         mutex_unlock(&pmac_backlight_mutex);
1896 #endif
1897 }
1898 #endif /* CONFIG_FB_ATY128_BACKLIGHT */
1899
1900 /*
1901  *  Initialisation
1902  */
1903
1904 #ifdef CONFIG_PPC_PMAC
1905 static void aty128_early_resume(void *data)
1906 {
1907         struct aty128fb_par *par = data;
1908
1909         if (try_acquire_console_sem())
1910                 return;
1911         aty128_do_resume(par->pdev);
1912         release_console_sem();
1913 }
1914 #endif /* CONFIG_PPC_PMAC */
1915
1916 static int __devinit aty128_init(struct pci_dev *pdev, const struct pci_device_id *ent)
1917 {
1918         struct fb_info *info = pci_get_drvdata(pdev);
1919         struct aty128fb_par *par = info->par;
1920         struct fb_var_screeninfo var;
1921         char video_card[DEVICE_NAME_SIZE];
1922         u8 chip_rev;
1923         u32 dac;
1924
1925         /* Get the chip revision */
1926         chip_rev = (aty_ld_le32(CONFIG_CNTL) >> 16) & 0x1F;
1927
1928         strcpy(video_card, "Rage128 XX ");
1929         video_card[8] = ent->device >> 8;
1930         video_card[9] = ent->device & 0xFF;
1931
1932         /* range check to make sure */
1933         if (ent->driver_data < ARRAY_SIZE(r128_family))
1934             strncat(video_card, r128_family[ent->driver_data], sizeof(video_card));
1935
1936         printk(KERN_INFO "aty128fb: %s [chip rev 0x%x] ", video_card, chip_rev);
1937
1938         if (par->vram_size % (1024 * 1024) == 0)
1939                 printk("%dM %s\n", par->vram_size / (1024*1024), par->mem->name);
1940         else
1941                 printk("%dk %s\n", par->vram_size / 1024, par->mem->name);
1942
1943         par->chip_gen = ent->driver_data;
1944
1945         /* fill in info */
1946         info->fbops = &aty128fb_ops;
1947         info->flags = FBINFO_FLAG_DEFAULT;
1948
1949         par->lcd_on = default_lcd_on;
1950         par->crt_on = default_crt_on;
1951
1952         var = default_var;
1953 #ifdef CONFIG_PPC_PMAC
1954         if (machine_is(powermac)) {
1955                 /* Indicate sleep capability */
1956                 if (par->chip_gen == rage_M3) {
1957                         pmac_call_feature(PMAC_FTR_DEVICE_CAN_WAKE, NULL, 0, 1);
1958                         pmac_set_early_video_resume(aty128_early_resume, par);
1959                 }
1960
1961                 /* Find default mode */
1962                 if (mode_option) {
1963                         if (!mac_find_mode(&var, info, mode_option, 8))
1964                                 var = default_var;
1965                 } else {
1966                         if (default_vmode <= 0 || default_vmode > VMODE_MAX)
1967                                 default_vmode = VMODE_1024_768_60;
1968
1969                         /* iMacs need that resolution
1970                          * PowerMac2,1 first r128 iMacs
1971                          * PowerMac2,2 summer 2000 iMacs
1972                          * PowerMac4,1 january 2001 iMacs "flower power"
1973                          */
1974                         if (machine_is_compatible("PowerMac2,1") ||
1975                             machine_is_compatible("PowerMac2,2") ||
1976                             machine_is_compatible("PowerMac4,1"))
1977                                 default_vmode = VMODE_1024_768_75;
1978
1979                         /* iBook SE */
1980                         if (machine_is_compatible("PowerBook2,2"))
1981                                 default_vmode = VMODE_800_600_60;
1982
1983                         /* PowerBook Firewire (Pismo), iBook Dual USB */
1984                         if (machine_is_compatible("PowerBook3,1") ||
1985                             machine_is_compatible("PowerBook4,1"))
1986                                 default_vmode = VMODE_1024_768_60;
1987
1988                         /* PowerBook Titanium */
1989                         if (machine_is_compatible("PowerBook3,2"))
1990                                 default_vmode = VMODE_1152_768_60;
1991         
1992                         if (default_cmode > 16) 
1993                             default_cmode = CMODE_32;
1994                         else if (default_cmode > 8) 
1995                             default_cmode = CMODE_16;
1996                         else 
1997                             default_cmode = CMODE_8;
1998
1999                         if (mac_vmode_to_var(default_vmode, default_cmode, &var))
2000                                 var = default_var;
2001                 }
2002         } else
2003 #endif /* CONFIG_PPC_PMAC */
2004         {
2005                 if (mode_option)
2006                         if (fb_find_mode(&var, info, mode_option, NULL, 
2007                                          0, &defaultmode, 8) == 0)
2008                                 var = default_var;
2009         }
2010
2011         var.accel_flags &= ~FB_ACCELF_TEXT;
2012 //      var.accel_flags |= FB_ACCELF_TEXT;/* FIXME Will add accel later */
2013
2014         if (aty128fb_check_var(&var, info)) {
2015                 printk(KERN_ERR "aty128fb: Cannot set default mode.\n");
2016                 return 0;
2017         }
2018
2019         /* setup the DAC the way we like it */
2020         dac = aty_ld_le32(DAC_CNTL);
2021         dac |= (DAC_8BIT_EN | DAC_RANGE_CNTL);
2022         dac |= DAC_MASK;
2023         if (par->chip_gen == rage_M3)
2024                 dac |= DAC_PALETTE2_SNOOP_EN;
2025         aty_st_le32(DAC_CNTL, dac);
2026
2027         /* turn off bus mastering, just in case */
2028         aty_st_le32(BUS_CNTL, aty_ld_le32(BUS_CNTL) | BUS_MASTER_DIS);
2029
2030         info->var = var;
2031         fb_alloc_cmap(&info->cmap, 256, 0);
2032
2033         var.activate = FB_ACTIVATE_NOW;
2034
2035         aty128_init_engine(par);
2036
2037         par->pm_reg = pci_find_capability(pdev, PCI_CAP_ID_PM);
2038         par->pdev = pdev;
2039         par->asleep = 0;
2040         par->lock_blank = 0;
2041
2042 #ifdef CONFIG_FB_ATY128_BACKLIGHT
2043         aty128_bl_init(par);
2044 #endif
2045
2046         if (register_framebuffer(info) < 0)
2047                 return 0;
2048
2049         printk(KERN_INFO "fb%d: %s frame buffer device on %s\n",
2050                info->node, info->fix.id, video_card);
2051
2052         return 1;       /* success! */
2053 }
2054
2055 #ifdef CONFIG_PCI
2056 /* register a card    ++ajoshi */
2057 static int __devinit aty128_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2058 {
2059         unsigned long fb_addr, reg_addr;
2060         struct aty128fb_par *par;
2061         struct fb_info *info;
2062         int err;
2063 #ifndef __sparc__
2064         void __iomem *bios = NULL;
2065 #endif
2066
2067         /* Enable device in PCI config */
2068         if ((err = pci_enable_device(pdev))) {
2069                 printk(KERN_ERR "aty128fb: Cannot enable PCI device: %d\n",
2070                                 err);
2071                 return -ENODEV;
2072         }
2073
2074         fb_addr = pci_resource_start(pdev, 0);
2075         if (!request_mem_region(fb_addr, pci_resource_len(pdev, 0),
2076                                 "aty128fb FB")) {
2077                 printk(KERN_ERR "aty128fb: cannot reserve frame "
2078                                 "buffer memory\n");
2079                 return -ENODEV;
2080         }
2081
2082         reg_addr = pci_resource_start(pdev, 2);
2083         if (!request_mem_region(reg_addr, pci_resource_len(pdev, 2),
2084                                 "aty128fb MMIO")) {
2085                 printk(KERN_ERR "aty128fb: cannot reserve MMIO region\n");
2086                 goto err_free_fb;
2087         }
2088
2089         /* We have the resources. Now virtualize them */
2090         info = framebuffer_alloc(sizeof(struct aty128fb_par), &pdev->dev);
2091         if (info == NULL) {
2092                 printk(KERN_ERR "aty128fb: can't alloc fb_info_aty128\n");
2093                 goto err_free_mmio;
2094         }
2095         par = info->par;
2096
2097         info->pseudo_palette = par->pseudo_palette;
2098
2099         /* Virtualize mmio region */
2100         info->fix.mmio_start = reg_addr;
2101         par->regbase = ioremap(reg_addr, pci_resource_len(pdev, 2));
2102         if (!par->regbase)
2103                 goto err_free_info;
2104
2105         /* Grab memory size from the card */
2106         // How does this relate to the resource length from the PCI hardware?
2107         par->vram_size = aty_ld_le32(CONFIG_MEMSIZE) & 0x03FFFFFF;
2108
2109         /* Virtualize the framebuffer */
2110         info->screen_base = ioremap(fb_addr, par->vram_size);
2111         if (!info->screen_base)
2112                 goto err_unmap_out;
2113
2114         /* Set up info->fix */
2115         info->fix = aty128fb_fix;
2116         info->fix.smem_start = fb_addr;
2117         info->fix.smem_len = par->vram_size;
2118         info->fix.mmio_start = reg_addr;
2119
2120         /* If we can't test scratch registers, something is seriously wrong */
2121         if (!register_test(par)) {
2122                 printk(KERN_ERR "aty128fb: Can't write to video register!\n");
2123                 goto err_out;
2124         }
2125
2126 #ifndef __sparc__
2127         bios = aty128_map_ROM(par, pdev);
2128 #ifdef CONFIG_X86
2129         if (bios == NULL)
2130                 bios = aty128_find_mem_vbios(par);
2131 #endif
2132         if (bios == NULL)
2133                 printk(KERN_INFO "aty128fb: BIOS not located, guessing timings.\n");
2134         else {
2135                 printk(KERN_INFO "aty128fb: Rage128 BIOS located\n");
2136                 aty128_get_pllinfo(par, bios);
2137                 pci_unmap_rom(pdev, bios);
2138         }
2139 #endif /* __sparc__ */
2140
2141         aty128_timings(par);
2142         pci_set_drvdata(pdev, info);
2143
2144         if (!aty128_init(pdev, ent))
2145                 goto err_out;
2146
2147 #ifdef CONFIG_MTRR
2148         if (mtrr) {
2149                 par->mtrr.vram = mtrr_add(info->fix.smem_start,
2150                                 par->vram_size, MTRR_TYPE_WRCOMB, 1);
2151                 par->mtrr.vram_valid = 1;
2152                 /* let there be speed */
2153                 printk(KERN_INFO "aty128fb: Rage128 MTRR set to ON\n");
2154         }
2155 #endif /* CONFIG_MTRR */
2156         return 0;
2157
2158 err_out:
2159         iounmap(info->screen_base);
2160 err_unmap_out:
2161         iounmap(par->regbase);
2162 err_free_info:
2163         framebuffer_release(info);
2164 err_free_mmio:
2165         release_mem_region(pci_resource_start(pdev, 2),
2166                         pci_resource_len(pdev, 2));
2167 err_free_fb:
2168         release_mem_region(pci_resource_start(pdev, 0),
2169                         pci_resource_len(pdev, 0));
2170         return -ENODEV;
2171 }
2172
2173 static void __devexit aty128_remove(struct pci_dev *pdev)
2174 {
2175         struct fb_info *info = pci_get_drvdata(pdev);
2176         struct aty128fb_par *par;
2177
2178         if (!info)
2179                 return;
2180
2181         par = info->par;
2182
2183 #ifdef CONFIG_FB_ATY128_BACKLIGHT
2184         aty128_bl_exit(par);
2185 #endif
2186
2187         unregister_framebuffer(info);
2188 #ifdef CONFIG_MTRR
2189         if (par->mtrr.vram_valid)
2190                 mtrr_del(par->mtrr.vram, info->fix.smem_start,
2191                          par->vram_size);
2192 #endif /* CONFIG_MTRR */
2193         iounmap(par->regbase);
2194         iounmap(info->screen_base);
2195
2196         release_mem_region(pci_resource_start(pdev, 0),
2197                            pci_resource_len(pdev, 0));
2198         release_mem_region(pci_resource_start(pdev, 2),
2199                            pci_resource_len(pdev, 2));
2200         framebuffer_release(info);
2201 }
2202 #endif /* CONFIG_PCI */
2203
2204
2205
2206     /*
2207      *  Blank the display.
2208      */
2209 static int aty128fb_blank(int blank, struct fb_info *fb)
2210 {
2211         struct aty128fb_par *par = fb->par;
2212         u8 state = 0;
2213
2214         if (par->lock_blank || par->asleep)
2215                 return 0;
2216
2217 #ifdef CONFIG_FB_ATY128_BACKLIGHT
2218         if (machine_is(powermac) && blank)
2219                 aty128_bl_set_power(fb, FB_BLANK_POWERDOWN);
2220 #endif
2221
2222         if (blank & FB_BLANK_VSYNC_SUSPEND)
2223                 state |= 2;
2224         if (blank & FB_BLANK_HSYNC_SUSPEND)
2225                 state |= 1;
2226         if (blank & FB_BLANK_POWERDOWN)
2227                 state |= 4;
2228
2229         aty_st_8(CRTC_EXT_CNTL+1, state);
2230
2231         if (par->chip_gen == rage_M3) {
2232                 aty128_set_crt_enable(par, par->crt_on && !blank);
2233                 aty128_set_lcd_enable(par, par->lcd_on && !blank);
2234         }
2235
2236 #ifdef CONFIG_FB_ATY128_BACKLIGHT
2237         if (machine_is(powermac) && !blank)
2238                 aty128_bl_set_power(fb, FB_BLANK_UNBLANK);
2239 #endif
2240
2241         return 0;
2242 }
2243
2244 /*
2245  *  Set a single color register. The values supplied are already
2246  *  rounded down to the hardware's capabilities (according to the
2247  *  entries in the var structure). Return != 0 for invalid regno.
2248  */
2249 static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
2250                               u_int transp, struct fb_info *info)
2251 {
2252         struct aty128fb_par *par = info->par;
2253
2254         if (regno > 255
2255             || (par->crtc.depth == 16 && regno > 63)
2256             || (par->crtc.depth == 15 && regno > 31))
2257                 return 1;
2258
2259         red >>= 8;
2260         green >>= 8;
2261         blue >>= 8;
2262
2263         if (regno < 16) {
2264                 int i;
2265                 u32 *pal = info->pseudo_palette;
2266
2267                 switch (par->crtc.depth) {
2268                 case 15:
2269                         pal[regno] = (regno << 10) | (regno << 5) | regno;
2270                         break;
2271                 case 16:
2272                         pal[regno] = (regno << 11) | (regno << 6) | regno;
2273                         break;
2274                 case 24:
2275                         pal[regno] = (regno << 16) | (regno << 8) | regno;
2276                         break;
2277                 case 32:
2278                         i = (regno << 8) | regno;
2279                         pal[regno] = (i << 16) | i;
2280                         break;
2281                 }
2282         }
2283
2284         if (par->crtc.depth == 16 && regno > 0) {
2285                 /*
2286                  * With the 5-6-5 split of bits for RGB at 16 bits/pixel, we
2287                  * have 32 slots for R and B values but 64 slots for G values.
2288                  * Thus the R and B values go in one slot but the G value
2289                  * goes in a different slot, and we have to avoid disturbing
2290                  * the other fields in the slots we touch.
2291                  */
2292                 par->green[regno] = green;
2293                 if (regno < 32) {
2294                         par->red[regno] = red;
2295                         par->blue[regno] = blue;
2296                         aty128_st_pal(regno * 8, red, par->green[regno*2],
2297                                       blue, par);
2298                 }
2299                 red = par->red[regno/2];
2300                 blue = par->blue[regno/2];
2301                 regno <<= 2;
2302         } else if (par->crtc.bpp == 16)
2303                 regno <<= 3;
2304         aty128_st_pal(regno, red, green, blue, par);
2305
2306         return 0;
2307 }
2308
2309 #define ATY_MIRROR_LCD_ON       0x00000001
2310 #define ATY_MIRROR_CRT_ON       0x00000002
2311
2312 /* out param: u32*      backlight value: 0 to 15 */
2313 #define FBIO_ATY128_GET_MIRROR  _IOR('@', 1, __u32)
2314 /* in param: u32*       backlight value: 0 to 15 */
2315 #define FBIO_ATY128_SET_MIRROR  _IOW('@', 2, __u32)
2316
2317 static int aty128fb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
2318 {
2319         struct aty128fb_par *par = info->par;
2320         u32 value;
2321         int rc;
2322     
2323         switch (cmd) {
2324         case FBIO_ATY128_SET_MIRROR:
2325                 if (par->chip_gen != rage_M3)
2326                         return -EINVAL;
2327                 rc = get_user(value, (__u32 __user *)arg);
2328                 if (rc)
2329                         return rc;
2330                 par->lcd_on = (value & 0x01) != 0;
2331                 par->crt_on = (value & 0x02) != 0;
2332                 if (!par->crt_on && !par->lcd_on)
2333                         par->lcd_on = 1;
2334                 aty128_set_crt_enable(par, par->crt_on);        
2335                 aty128_set_lcd_enable(par, par->lcd_on);        
2336                 return 0;
2337         case FBIO_ATY128_GET_MIRROR:
2338                 if (par->chip_gen != rage_M3)
2339                         return -EINVAL;
2340                 value = (par->crt_on << 1) | par->lcd_on;
2341                 return put_user(value, (__u32 __user *)arg);
2342         }
2343         return -EINVAL;
2344 }
2345
2346 #if 0
2347     /*
2348      *  Accelerated functions
2349      */
2350
2351 static inline void aty128_rectcopy(int srcx, int srcy, int dstx, int dsty,
2352                                    u_int width, u_int height,
2353                                    struct fb_info_aty128 *par)
2354 {
2355     u32 save_dp_datatype, save_dp_cntl, dstval;
2356
2357     if (!width || !height)
2358         return;
2359
2360     dstval = depth_to_dst(par->current_par.crtc.depth);
2361     if (dstval == DST_24BPP) {
2362         srcx *= 3;
2363         dstx *= 3;
2364         width *= 3;
2365     } else if (dstval == -EINVAL) {
2366         printk("aty128fb: invalid depth or RGBA\n");
2367         return;
2368     }
2369
2370     wait_for_fifo(2, par);
2371     save_dp_datatype = aty_ld_le32(DP_DATATYPE);
2372     save_dp_cntl     = aty_ld_le32(DP_CNTL);
2373
2374     wait_for_fifo(6, par);
2375     aty_st_le32(SRC_Y_X, (srcy << 16) | srcx);
2376     aty_st_le32(DP_MIX, ROP3_SRCCOPY | DP_SRC_RECT);
2377     aty_st_le32(DP_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
2378     aty_st_le32(DP_DATATYPE, save_dp_datatype | dstval | SRC_DSTCOLOR);
2379
2380     aty_st_le32(DST_Y_X, (dsty << 16) | dstx);
2381     aty_st_le32(DST_HEIGHT_WIDTH, (height << 16) | width);
2382
2383     par->blitter_may_be_busy = 1;
2384
2385     wait_for_fifo(2, par);
2386     aty_st_le32(DP_DATATYPE, save_dp_datatype);
2387     aty_st_le32(DP_CNTL, save_dp_cntl); 
2388 }
2389
2390
2391     /*
2392      * Text mode accelerated functions
2393      */
2394
2395 static void fbcon_aty128_bmove(struct display *p, int sy, int sx, int dy, int dx,
2396                         int height, int width)
2397 {
2398     sx     *= fontwidth(p);
2399     sy     *= fontheight(p);
2400     dx     *= fontwidth(p);
2401     dy     *= fontheight(p);
2402     width  *= fontwidth(p);
2403     height *= fontheight(p);
2404
2405     aty128_rectcopy(sx, sy, dx, dy, width, height,
2406                         (struct fb_info_aty128 *)p->fb_info);
2407 }
2408 #endif /* 0 */
2409
2410 static void aty128_set_suspend(struct aty128fb_par *par, int suspend)
2411 {
2412         u32     pmgt;
2413         u16     pwr_command;
2414         struct pci_dev *pdev = par->pdev;
2415
2416         if (!par->pm_reg)
2417                 return;
2418                 
2419         /* Set the chip into the appropriate suspend mode (we use D2,
2420          * D3 would require a complete re-initialisation of the chip,
2421          * including PCI config registers, clocks, AGP configuration, ...)
2422          */
2423         if (suspend) {
2424                 /* Make sure CRTC2 is reset. Remove that the day we decide to
2425                  * actually use CRTC2 and replace it with real code for disabling
2426                  * the CRTC2 output during sleep
2427                  */
2428                 aty_st_le32(CRTC2_GEN_CNTL, aty_ld_le32(CRTC2_GEN_CNTL) &
2429                         ~(CRTC2_EN));
2430
2431                 /* Set the power management mode to be PCI based */
2432                 /* Use this magic value for now */
2433                 pmgt = 0x0c005407;
2434                 aty_st_pll(POWER_MANAGEMENT, pmgt);
2435                 (void)aty_ld_pll(POWER_MANAGEMENT);
2436                 aty_st_le32(BUS_CNTL1, 0x00000010);
2437                 aty_st_le32(MEM_POWER_MISC, 0x0c830000);
2438                 mdelay(100);
2439                 pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
2440                 /* Switch PCI power management to D2 */
2441                 pci_write_config_word(pdev, par->pm_reg+PCI_PM_CTRL,
2442                         (pwr_command & ~PCI_PM_CTRL_STATE_MASK) | 2);
2443                 pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
2444         } else {
2445                 /* Switch back PCI power management to D0 */
2446                 mdelay(100);
2447                 pci_write_config_word(pdev, par->pm_reg+PCI_PM_CTRL, 0);
2448                 pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
2449                 mdelay(100);
2450         }
2451 }
2452
2453 static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2454 {
2455         struct fb_info *info = pci_get_drvdata(pdev);
2456         struct aty128fb_par *par = info->par;
2457
2458         /* We don't do anything but D2, for now we return 0, but
2459          * we may want to change that. How do we know if the BIOS
2460          * can properly take care of D3 ? Also, with swsusp, we
2461          * know we'll be rebooted, ...
2462          */
2463 #ifndef CONFIG_PPC_PMAC
2464         /* HACK ALERT ! Once I find a proper way to say to each driver
2465          * individually what will happen with it's PCI slot, I'll change
2466          * that. On laptops, the AGP slot is just unclocked, so D2 is
2467          * expected, while on desktops, the card is powered off
2468          */
2469         return 0;
2470 #endif /* CONFIG_PPC_PMAC */
2471          
2472         if (state.event == pdev->dev.power.power_state.event)
2473                 return 0;
2474
2475         printk(KERN_DEBUG "aty128fb: suspending...\n");
2476         
2477         acquire_console_sem();
2478
2479         fb_set_suspend(info, 1);
2480
2481         /* Make sure engine is reset */
2482         wait_for_idle(par);
2483         aty128_reset_engine(par);
2484         wait_for_idle(par);
2485
2486         /* Blank display and LCD */
2487         aty128fb_blank(VESA_POWERDOWN, info);
2488
2489         /* Sleep */
2490         par->asleep = 1;
2491         par->lock_blank = 1;
2492
2493 #ifdef CONFIG_PPC_PMAC
2494         /* On powermac, we have hooks to properly suspend/resume AGP now,
2495          * use them here. We'll ultimately need some generic support here,
2496          * but the generic code isn't quite ready for that yet
2497          */
2498         pmac_suspend_agp_for_card(pdev);
2499 #endif /* CONFIG_PPC_PMAC */
2500
2501         /* We need a way to make sure the fbdev layer will _not_ touch the
2502          * framebuffer before we put the chip to suspend state. On 2.4, I
2503          * used dummy fb ops, 2.5 need proper support for this at the
2504          * fbdev level
2505          */
2506         if (state.event != PM_EVENT_ON)
2507                 aty128_set_suspend(par, 1);
2508
2509         release_console_sem();
2510
2511         pdev->dev.power.power_state = state;
2512
2513         return 0;
2514 }
2515
2516 static int aty128_do_resume(struct pci_dev *pdev)
2517 {
2518         struct fb_info *info = pci_get_drvdata(pdev);
2519         struct aty128fb_par *par = info->par;
2520
2521         if (pdev->dev.power.power_state.event == PM_EVENT_ON)
2522                 return 0;
2523
2524         /* Wakeup chip */
2525         aty128_set_suspend(par, 0);
2526         par->asleep = 0;
2527
2528         /* Restore display & engine */
2529         aty128_reset_engine(par);
2530         wait_for_idle(par);
2531         aty128fb_set_par(info);
2532         fb_pan_display(info, &info->var);
2533         fb_set_cmap(&info->cmap, info);
2534
2535         /* Refresh */
2536         fb_set_suspend(info, 0);
2537
2538         /* Unblank */
2539         par->lock_blank = 0;
2540         aty128fb_blank(0, info);
2541
2542 #ifdef CONFIG_PPC_PMAC
2543         /* On powermac, we have hooks to properly suspend/resume AGP now,
2544          * use them here. We'll ultimately need some generic support here,
2545          * but the generic code isn't quite ready for that yet
2546          */
2547         pmac_resume_agp_for_card(pdev);
2548 #endif /* CONFIG_PPC_PMAC */
2549
2550         pdev->dev.power.power_state = PMSG_ON;
2551
2552         printk(KERN_DEBUG "aty128fb: resumed !\n");
2553
2554         return 0;
2555 }
2556
2557 static int aty128_pci_resume(struct pci_dev *pdev)
2558 {
2559         int rc;
2560
2561         acquire_console_sem();
2562         rc = aty128_do_resume(pdev);
2563         release_console_sem();
2564
2565         return rc;
2566 }
2567
2568
2569 static int __devinit aty128fb_init(void)
2570 {
2571 #ifndef MODULE
2572         char *option = NULL;
2573
2574         if (fb_get_options("aty128fb", &option))
2575                 return -ENODEV;
2576         aty128fb_setup(option);
2577 #endif
2578
2579         return pci_register_driver(&aty128fb_driver);
2580 }
2581
2582 static void __exit aty128fb_exit(void)
2583 {
2584         pci_unregister_driver(&aty128fb_driver);
2585 }
2586
2587 module_init(aty128fb_init);
2588
2589 module_exit(aty128fb_exit);
2590
2591 MODULE_AUTHOR("(c)1999-2003 Brad Douglas <brad@neruo.com>");
2592 MODULE_DESCRIPTION("FBDev driver for ATI Rage128 / Pro cards");
2593 MODULE_LICENSE("GPL");
2594 module_param(mode_option, charp, 0);
2595 MODULE_PARM_DESC(mode_option, "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
2596 #ifdef CONFIG_MTRR
2597 module_param_named(nomtrr, mtrr, invbool, 0);
2598 MODULE_PARM_DESC(nomtrr, "bool: Disable MTRR support (0 or 1=disabled) (default=0)");
2599 #endif
2600