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