drm/radeon: insist on 32-bit DMA for Cedar on PPC64/PPC64LE
[pandora-kernel.git] / drivers / video / tgafb.c
1 /*
2  *  linux/drivers/video/tgafb.c -- DEC 21030 TGA frame buffer device
3  *
4  *      Copyright (C) 1995 Jay Estabrook
5  *      Copyright (C) 1997 Geert Uytterhoeven
6  *      Copyright (C) 1999,2000 Martin Lucina, Tom Zerucha
7  *      Copyright (C) 2002 Richard Henderson
8  *      Copyright (C) 2006, 2007  Maciej W. Rozycki
9  *
10  *  This file is subject to the terms and conditions of the GNU General Public
11  *  License. See the file COPYING in the main directory of this archive for
12  *  more details.
13  */
14
15 #include <linux/bitrev.h>
16 #include <linux/compiler.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/errno.h>
20 #include <linux/fb.h>
21 #include <linux/init.h>
22 #include <linux/ioport.h>
23 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/selection.h>
28 #include <linux/string.h>
29 #include <linux/tc.h>
30
31 #include <asm/io.h>
32
33 #include <video/tgafb.h>
34
35 #ifdef CONFIG_PCI
36 #define TGA_BUS_PCI(dev) (dev->bus == &pci_bus_type)
37 #else
38 #define TGA_BUS_PCI(dev) 0
39 #endif
40
41 #ifdef CONFIG_TC
42 #define TGA_BUS_TC(dev) (dev->bus == &tc_bus_type)
43 #else
44 #define TGA_BUS_TC(dev) 0
45 #endif
46
47 /*
48  * Local functions.
49  */
50
51 static int tgafb_check_var(struct fb_var_screeninfo *, struct fb_info *);
52 static int tgafb_set_par(struct fb_info *);
53 static void tgafb_set_pll(struct tga_par *, int);
54 static int tgafb_setcolreg(unsigned, unsigned, unsigned, unsigned,
55                            unsigned, struct fb_info *);
56 static int tgafb_blank(int, struct fb_info *);
57 static void tgafb_init_fix(struct fb_info *);
58
59 static void tgafb_imageblit(struct fb_info *, const struct fb_image *);
60 static void tgafb_fillrect(struct fb_info *, const struct fb_fillrect *);
61 static void tgafb_copyarea(struct fb_info *, const struct fb_copyarea *);
62 static int tgafb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info);
63
64 static int __devinit tgafb_register(struct device *dev);
65 static void __devexit tgafb_unregister(struct device *dev);
66
67 static const char *mode_option;
68 static const char *mode_option_pci = "640x480@60";
69 static const char *mode_option_tc = "1280x1024@72";
70
71
72 static struct pci_driver tgafb_pci_driver;
73 static struct tc_driver tgafb_tc_driver;
74
75 /*
76  *  Frame buffer operations
77  */
78
79 static struct fb_ops tgafb_ops = {
80         .owner                  = THIS_MODULE,
81         .fb_check_var           = tgafb_check_var,
82         .fb_set_par             = tgafb_set_par,
83         .fb_setcolreg           = tgafb_setcolreg,
84         .fb_blank               = tgafb_blank,
85         .fb_pan_display         = tgafb_pan_display,
86         .fb_fillrect            = tgafb_fillrect,
87         .fb_copyarea            = tgafb_copyarea,
88         .fb_imageblit           = tgafb_imageblit,
89 };
90
91
92 #ifdef CONFIG_PCI
93 /*
94  *  PCI registration operations
95  */
96 static int __devinit tgafb_pci_register(struct pci_dev *,
97                                         const struct pci_device_id *);
98 static void __devexit tgafb_pci_unregister(struct pci_dev *);
99
100 static struct pci_device_id const tgafb_pci_table[] = {
101         { PCI_DEVICE(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TGA) },
102         { }
103 };
104 MODULE_DEVICE_TABLE(pci, tgafb_pci_table);
105
106 static struct pci_driver tgafb_pci_driver = {
107         .name                   = "tgafb",
108         .id_table               = tgafb_pci_table,
109         .probe                  = tgafb_pci_register,
110         .remove                 = __devexit_p(tgafb_pci_unregister),
111 };
112
113 static int __devinit
114 tgafb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
115 {
116         return tgafb_register(&pdev->dev);
117 }
118
119 static void __devexit
120 tgafb_pci_unregister(struct pci_dev *pdev)
121 {
122         tgafb_unregister(&pdev->dev);
123 }
124 #endif /* CONFIG_PCI */
125
126 #ifdef CONFIG_TC
127 /*
128  *  TC registration operations
129  */
130 static int __devinit tgafb_tc_register(struct device *);
131 static int __devexit tgafb_tc_unregister(struct device *);
132
133 static struct tc_device_id const tgafb_tc_table[] = {
134         { "DEC     ", "PMAGD-AA" },
135         { "DEC     ", "PMAGD   " },
136         { }
137 };
138 MODULE_DEVICE_TABLE(tc, tgafb_tc_table);
139
140 static struct tc_driver tgafb_tc_driver = {
141         .id_table               = tgafb_tc_table,
142         .driver                 = {
143                 .name           = "tgafb",
144                 .bus            = &tc_bus_type,
145                 .probe          = tgafb_tc_register,
146                 .remove         = __devexit_p(tgafb_tc_unregister),
147         },
148 };
149
150 static int __devinit
151 tgafb_tc_register(struct device *dev)
152 {
153         int status = tgafb_register(dev);
154         if (!status)
155                 get_device(dev);
156         return status;
157 }
158
159 static int __devexit
160 tgafb_tc_unregister(struct device *dev)
161 {
162         put_device(dev);
163         tgafb_unregister(dev);
164         return 0;
165 }
166 #endif /* CONFIG_TC */
167
168
169 /**
170  *      tgafb_check_var - Optional function.  Validates a var passed in.
171  *      @var: frame buffer variable screen structure
172  *      @info: frame buffer structure that represents a single frame buffer
173  */
174 static int
175 tgafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
176 {
177         struct tga_par *par = (struct tga_par *)info->par;
178
179         if (par->tga_type == TGA_TYPE_8PLANE) {
180                 if (var->bits_per_pixel != 8)
181                         return -EINVAL;
182         } else {
183                 if (var->bits_per_pixel != 32)
184                         return -EINVAL;
185         }
186         var->red.length = var->green.length = var->blue.length = 8;
187         if (var->bits_per_pixel == 32) {
188                 var->red.offset = 16;
189                 var->green.offset = 8;
190                 var->blue.offset = 0;
191         }
192
193         if (var->xres_virtual != var->xres || var->yres_virtual != var->yres)
194                 return -EINVAL;
195         if (var->xres * var->yres * (var->bits_per_pixel >> 3) > info->fix.smem_len)
196                 return -EINVAL;
197         if (var->nonstd)
198                 return -EINVAL;
199         if (1000000000 / var->pixclock > TGA_PLL_MAX_FREQ)
200                 return -EINVAL;
201         if ((var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
202                 return -EINVAL;
203
204         /* Some of the acceleration routines assume the line width is
205            a multiple of 64 bytes.  */
206         if (var->xres * (par->tga_type == TGA_TYPE_8PLANE ? 1 : 4) % 64)
207                 return -EINVAL;
208
209         return 0;
210 }
211
212 /**
213  *      tgafb_set_par - Optional function.  Alters the hardware state.
214  *      @info: frame buffer structure that represents a single frame buffer
215  */
216 static int
217 tgafb_set_par(struct fb_info *info)
218 {
219         static unsigned int const deep_presets[4] = {
220                 0x00004000,
221                 0x0000440d,
222                 0xffffffff,
223                 0x0000441d
224         };
225         static unsigned int const rasterop_presets[4] = {
226                 0x00000003,
227                 0x00000303,
228                 0xffffffff,
229                 0x00000303
230         };
231         static unsigned int const mode_presets[4] = {
232                 0x00000000,
233                 0x00000300,
234                 0xffffffff,
235                 0x00000300
236         };
237         static unsigned int const base_addr_presets[4] = {
238                 0x00000000,
239                 0x00000001,
240                 0xffffffff,
241                 0x00000001
242         };
243
244         struct tga_par *par = (struct tga_par *) info->par;
245         int tga_bus_pci = TGA_BUS_PCI(par->dev);
246         int tga_bus_tc = TGA_BUS_TC(par->dev);
247         u32 htimings, vtimings, pll_freq;
248         u8 tga_type;
249         int i;
250
251         /* Encode video timings.  */
252         htimings = (((info->var.xres/4) & TGA_HORIZ_ACT_LSB)
253                     | (((info->var.xres/4) & 0x600 << 19) & TGA_HORIZ_ACT_MSB));
254         vtimings = (info->var.yres & TGA_VERT_ACTIVE);
255         htimings |= ((info->var.right_margin/4) << 9) & TGA_HORIZ_FP;
256         vtimings |= (info->var.lower_margin << 11) & TGA_VERT_FP;
257         htimings |= ((info->var.hsync_len/4) << 14) & TGA_HORIZ_SYNC;
258         vtimings |= (info->var.vsync_len << 16) & TGA_VERT_SYNC;
259         htimings |= ((info->var.left_margin/4) << 21) & TGA_HORIZ_BP;
260         vtimings |= (info->var.upper_margin << 22) & TGA_VERT_BP;
261
262         if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
263                 htimings |= TGA_HORIZ_POLARITY;
264         if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
265                 vtimings |= TGA_VERT_POLARITY;
266
267         par->htimings = htimings;
268         par->vtimings = vtimings;
269
270         par->sync_on_green = !!(info->var.sync & FB_SYNC_ON_GREEN);
271
272         /* Store other useful values in par.  */
273         par->xres = info->var.xres;
274         par->yres = info->var.yres;
275         par->pll_freq = pll_freq = 1000000000 / info->var.pixclock;
276         par->bits_per_pixel = info->var.bits_per_pixel;
277         info->fix.line_length = par->xres * (par->bits_per_pixel >> 3);
278
279         tga_type = par->tga_type;
280
281         /* First, disable video.  */
282         TGA_WRITE_REG(par, TGA_VALID_VIDEO | TGA_VALID_BLANK, TGA_VALID_REG);
283
284         /* Write the DEEP register.  */
285         while (TGA_READ_REG(par, TGA_CMD_STAT_REG) & 1) /* wait for not busy */
286                 continue;
287         mb();
288         TGA_WRITE_REG(par, deep_presets[tga_type] |
289                            (par->sync_on_green ? 0x0 : 0x00010000),
290                       TGA_DEEP_REG);
291         while (TGA_READ_REG(par, TGA_CMD_STAT_REG) & 1) /* wait for not busy */
292                 continue;
293         mb();
294
295         /* Write some more registers.  */
296         TGA_WRITE_REG(par, rasterop_presets[tga_type], TGA_RASTEROP_REG);
297         TGA_WRITE_REG(par, mode_presets[tga_type], TGA_MODE_REG);
298         TGA_WRITE_REG(par, base_addr_presets[tga_type], TGA_BASE_ADDR_REG);
299
300         /* Calculate & write the PLL.  */
301         tgafb_set_pll(par, pll_freq);
302
303         /* Write some more registers.  */
304         TGA_WRITE_REG(par, 0xffffffff, TGA_PLANEMASK_REG);
305         TGA_WRITE_REG(par, 0xffffffff, TGA_PIXELMASK_REG);
306
307         /* Init video timing regs.  */
308         TGA_WRITE_REG(par, htimings, TGA_HORIZ_REG);
309         TGA_WRITE_REG(par, vtimings, TGA_VERT_REG);
310
311         /* Initialise RAMDAC. */
312         if (tga_type == TGA_TYPE_8PLANE && tga_bus_pci) {
313
314                 /* Init BT485 RAMDAC registers.  */
315                 BT485_WRITE(par, 0xa2 | (par->sync_on_green ? 0x8 : 0x0),
316                             BT485_CMD_0);
317                 BT485_WRITE(par, 0x01, BT485_ADDR_PAL_WRITE);
318                 BT485_WRITE(par, 0x14, BT485_CMD_3); /* cursor 64x64 */
319                 BT485_WRITE(par, 0x40, BT485_CMD_1);
320                 BT485_WRITE(par, 0x20, BT485_CMD_2); /* cursor off, for now */
321                 BT485_WRITE(par, 0xff, BT485_PIXEL_MASK);
322
323                 /* Fill palette registers.  */
324                 BT485_WRITE(par, 0x00, BT485_ADDR_PAL_WRITE);
325                 TGA_WRITE_REG(par, BT485_DATA_PAL, TGA_RAMDAC_SETUP_REG);
326
327                 for (i = 0; i < 256 * 3; i += 4) {
328                         TGA_WRITE_REG(par, 0x55 | (BT485_DATA_PAL << 8),
329                                       TGA_RAMDAC_REG);
330                         TGA_WRITE_REG(par, 0x00 | (BT485_DATA_PAL << 8),
331                                       TGA_RAMDAC_REG);
332                         TGA_WRITE_REG(par, 0x00 | (BT485_DATA_PAL << 8),
333                                       TGA_RAMDAC_REG);
334                         TGA_WRITE_REG(par, 0x00 | (BT485_DATA_PAL << 8),
335                                       TGA_RAMDAC_REG);
336                 }
337
338         } else if (tga_type == TGA_TYPE_8PLANE && tga_bus_tc) {
339
340                 /* Init BT459 RAMDAC registers.  */
341                 BT459_WRITE(par, BT459_REG_ACC, BT459_CMD_REG_0, 0x40);
342                 BT459_WRITE(par, BT459_REG_ACC, BT459_CMD_REG_1, 0x00);
343                 BT459_WRITE(par, BT459_REG_ACC, BT459_CMD_REG_2,
344                             (par->sync_on_green ? 0xc0 : 0x40));
345
346                 BT459_WRITE(par, BT459_REG_ACC, BT459_CUR_CMD_REG, 0x00);
347
348                 /* Fill the palette.  */
349                 BT459_LOAD_ADDR(par, 0x0000);
350                 TGA_WRITE_REG(par, BT459_PALETTE << 2, TGA_RAMDAC_SETUP_REG);
351
352                 for (i = 0; i < 256 * 3; i += 4) {
353                         TGA_WRITE_REG(par, 0x55, TGA_RAMDAC_REG);
354                         TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
355                         TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
356                         TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
357                 }
358
359         } else { /* 24-plane or 24plusZ */
360
361                 /* Init BT463 RAMDAC registers.  */
362                 BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_0, 0x40);
363                 BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_1, 0x08);
364                 BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_2,
365                             (par->sync_on_green ? 0xc0 : 0x40));
366
367                 BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_0, 0xff);
368                 BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_1, 0xff);
369                 BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_2, 0xff);
370                 BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_3, 0x0f);
371
372                 BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_0, 0x00);
373                 BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_1, 0x00);
374                 BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_2, 0x00);
375                 BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_3, 0x00);
376
377                 /* Fill the palette.  */
378                 BT463_LOAD_ADDR(par, 0x0000);
379                 TGA_WRITE_REG(par, BT463_PALETTE << 2, TGA_RAMDAC_SETUP_REG);
380
381 #ifdef CONFIG_HW_CONSOLE
382                 for (i = 0; i < 16; i++) {
383                         int j = color_table[i];
384
385                         TGA_WRITE_REG(par, default_red[j], TGA_RAMDAC_REG);
386                         TGA_WRITE_REG(par, default_grn[j], TGA_RAMDAC_REG);
387                         TGA_WRITE_REG(par, default_blu[j], TGA_RAMDAC_REG);
388                 }
389                 for (i = 0; i < 512 * 3; i += 4) {
390 #else
391                 for (i = 0; i < 528 * 3; i += 4) {
392 #endif
393                         TGA_WRITE_REG(par, 0x55, TGA_RAMDAC_REG);
394                         TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
395                         TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
396                         TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
397                 }
398
399                 /* Fill window type table after start of vertical retrace.  */
400                 while (!(TGA_READ_REG(par, TGA_INTR_STAT_REG) & 0x01))
401                         continue;
402                 TGA_WRITE_REG(par, 0x01, TGA_INTR_STAT_REG);
403                 mb();
404                 while (!(TGA_READ_REG(par, TGA_INTR_STAT_REG) & 0x01))
405                         continue;
406                 TGA_WRITE_REG(par, 0x01, TGA_INTR_STAT_REG);
407
408                 BT463_LOAD_ADDR(par, BT463_WINDOW_TYPE_BASE);
409                 TGA_WRITE_REG(par, BT463_REG_ACC << 2, TGA_RAMDAC_SETUP_REG);
410
411                 for (i = 0; i < 16; i++) {
412                         TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
413                         TGA_WRITE_REG(par, 0x01, TGA_RAMDAC_REG);
414                         TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
415                 }
416
417         }
418
419         /* Finally, enable video scan (and pray for the monitor... :-) */
420         TGA_WRITE_REG(par, TGA_VALID_VIDEO, TGA_VALID_REG);
421
422         return 0;
423 }
424
425 #define DIFFCHECK(X)                                                      \
426 do {                                                                      \
427         if (m <= 0x3f) {                                                  \
428                 int delta = f - (TGA_PLL_BASE_FREQ * (X)) / (r << shift); \
429                 if (delta < 0)                                            \
430                         delta = -delta;                                   \
431                 if (delta < min_diff)                                     \
432                         min_diff = delta, vm = m, va = a, vr = r;         \
433         }                                                                 \
434 } while (0)
435
436 static void
437 tgafb_set_pll(struct tga_par *par, int f)
438 {
439         int n, shift, base, min_diff, target;
440         int r,a,m,vm = 34, va = 1, vr = 30;
441
442         for (r = 0 ; r < 12 ; r++)
443                 TGA_WRITE_REG(par, !r, TGA_CLOCK_REG);
444
445         if (f > TGA_PLL_MAX_FREQ)
446                 f = TGA_PLL_MAX_FREQ;
447
448         if (f >= TGA_PLL_MAX_FREQ / 2)
449                 shift = 0;
450         else if (f >= TGA_PLL_MAX_FREQ / 4)
451                 shift = 1;
452         else
453                 shift = 2;
454
455         TGA_WRITE_REG(par, shift & 1, TGA_CLOCK_REG);
456         TGA_WRITE_REG(par, shift >> 1, TGA_CLOCK_REG);
457
458         for (r = 0 ; r < 10 ; r++)
459                 TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
460
461         if (f <= 120000) {
462                 TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
463                 TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
464         }
465         else if (f <= 200000) {
466                 TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
467                 TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
468         }
469         else {
470                 TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
471                 TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
472         }
473
474         TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
475         TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
476         TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
477         TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
478         TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
479         TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
480
481         target = (f << shift) / TGA_PLL_BASE_FREQ;
482         min_diff = TGA_PLL_MAX_FREQ;
483
484         r = 7 / target;
485         if (!r) r = 1;
486
487         base = target * r;
488         while (base < 449) {
489                 for (n = base < 7 ? 7 : base; n < base + target && n < 449; n++) {
490                         m = ((n + 3) / 7) - 1;
491                         a = 0;
492                         DIFFCHECK((m + 1) * 7);
493                         m++;
494                         DIFFCHECK((m + 1) * 7);
495                         m = (n / 6) - 1;
496                         if ((a = n % 6))
497                                 DIFFCHECK(n);
498                 }
499                 r++;
500                 base += target;
501         }
502
503         vr--;
504
505         for (r = 0; r < 8; r++)
506                 TGA_WRITE_REG(par, (vm >> r) & 1, TGA_CLOCK_REG);
507         for (r = 0; r < 8 ; r++)
508                 TGA_WRITE_REG(par, (va >> r) & 1, TGA_CLOCK_REG);
509         for (r = 0; r < 7 ; r++)
510                 TGA_WRITE_REG(par, (vr >> r) & 1, TGA_CLOCK_REG);
511         TGA_WRITE_REG(par, ((vr >> 7) & 1)|2, TGA_CLOCK_REG);
512 }
513
514
515 /**
516  *      tgafb_setcolreg - Optional function. Sets a color register.
517  *      @regno: boolean, 0 copy local, 1 get_user() function
518  *      @red: frame buffer colormap structure
519  *      @green: The green value which can be up to 16 bits wide
520  *      @blue:  The blue value which can be up to 16 bits wide.
521  *      @transp: If supported the alpha value which can be up to 16 bits wide.
522  *      @info: frame buffer info structure
523  */
524 static int
525 tgafb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
526                 unsigned transp, struct fb_info *info)
527 {
528         struct tga_par *par = (struct tga_par *) info->par;
529         int tga_bus_pci = TGA_BUS_PCI(par->dev);
530         int tga_bus_tc = TGA_BUS_TC(par->dev);
531
532         if (regno > 255)
533                 return 1;
534         red >>= 8;
535         green >>= 8;
536         blue >>= 8;
537
538         if (par->tga_type == TGA_TYPE_8PLANE && tga_bus_pci) {
539                 BT485_WRITE(par, regno, BT485_ADDR_PAL_WRITE);
540                 TGA_WRITE_REG(par, BT485_DATA_PAL, TGA_RAMDAC_SETUP_REG);
541                 TGA_WRITE_REG(par, red|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
542                 TGA_WRITE_REG(par, green|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
543                 TGA_WRITE_REG(par, blue|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
544         } else if (par->tga_type == TGA_TYPE_8PLANE && tga_bus_tc) {
545                 BT459_LOAD_ADDR(par, regno);
546                 TGA_WRITE_REG(par, BT459_PALETTE << 2, TGA_RAMDAC_SETUP_REG);
547                 TGA_WRITE_REG(par, red, TGA_RAMDAC_REG);
548                 TGA_WRITE_REG(par, green, TGA_RAMDAC_REG);
549                 TGA_WRITE_REG(par, blue, TGA_RAMDAC_REG);
550         } else {
551                 if (regno < 16) {
552                         u32 value = (regno << 16) | (regno << 8) | regno;
553                         ((u32 *)info->pseudo_palette)[regno] = value;
554                 }
555                 BT463_LOAD_ADDR(par, regno);
556                 TGA_WRITE_REG(par, BT463_PALETTE << 2, TGA_RAMDAC_SETUP_REG);
557                 TGA_WRITE_REG(par, red, TGA_RAMDAC_REG);
558                 TGA_WRITE_REG(par, green, TGA_RAMDAC_REG);
559                 TGA_WRITE_REG(par, blue, TGA_RAMDAC_REG);
560         }
561
562         return 0;
563 }
564
565
566 /**
567  *      tgafb_blank - Optional function.  Blanks the display.
568  *      @blank_mode: the blank mode we want.
569  *      @info: frame buffer structure that represents a single frame buffer
570  */
571 static int
572 tgafb_blank(int blank, struct fb_info *info)
573 {
574         struct tga_par *par = (struct tga_par *) info->par;
575         u32 vhcr, vvcr, vvvr;
576         unsigned long flags;
577
578         local_irq_save(flags);
579
580         vhcr = TGA_READ_REG(par, TGA_HORIZ_REG);
581         vvcr = TGA_READ_REG(par, TGA_VERT_REG);
582         vvvr = TGA_READ_REG(par, TGA_VALID_REG);
583         vvvr &= ~(TGA_VALID_VIDEO | TGA_VALID_BLANK);
584
585         switch (blank) {
586         case FB_BLANK_UNBLANK: /* Unblanking */
587                 if (par->vesa_blanked) {
588                         TGA_WRITE_REG(par, vhcr & 0xbfffffff, TGA_HORIZ_REG);
589                         TGA_WRITE_REG(par, vvcr & 0xbfffffff, TGA_VERT_REG);
590                         par->vesa_blanked = 0;
591                 }
592                 TGA_WRITE_REG(par, vvvr | TGA_VALID_VIDEO, TGA_VALID_REG);
593                 break;
594
595         case FB_BLANK_NORMAL: /* Normal blanking */
596                 TGA_WRITE_REG(par, vvvr | TGA_VALID_VIDEO | TGA_VALID_BLANK,
597                               TGA_VALID_REG);
598                 break;
599
600         case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
601                 TGA_WRITE_REG(par, vvcr | 0x40000000, TGA_VERT_REG);
602                 TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
603                 par->vesa_blanked = 1;
604                 break;
605
606         case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
607                 TGA_WRITE_REG(par, vhcr | 0x40000000, TGA_HORIZ_REG);
608                 TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
609                 par->vesa_blanked = 1;
610                 break;
611
612         case FB_BLANK_POWERDOWN: /* Poweroff */
613                 TGA_WRITE_REG(par, vhcr | 0x40000000, TGA_HORIZ_REG);
614                 TGA_WRITE_REG(par, vvcr | 0x40000000, TGA_VERT_REG);
615                 TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
616                 par->vesa_blanked = 1;
617                 break;
618         }
619
620         local_irq_restore(flags);
621         return 0;
622 }
623
624
625 /*
626  *  Acceleration.
627  */
628
629 static void
630 tgafb_mono_imageblit(struct fb_info *info, const struct fb_image *image)
631 {
632         struct tga_par *par = (struct tga_par *) info->par;
633         u32 fgcolor, bgcolor, dx, dy, width, height, vxres, vyres, pixelmask;
634         unsigned long rincr, line_length, shift, pos, is8bpp;
635         unsigned long i, j;
636         const unsigned char *data;
637         void __iomem *regs_base;
638         void __iomem *fb_base;
639
640         is8bpp = info->var.bits_per_pixel == 8;
641
642         dx = image->dx;
643         dy = image->dy;
644         width = image->width;
645         height = image->height;
646         vxres = info->var.xres_virtual;
647         vyres = info->var.yres_virtual;
648         line_length = info->fix.line_length;
649         rincr = (width + 7) / 8;
650
651         /* A shift below cannot cope with.  */
652         if (unlikely(width == 0))
653                 return;
654         /* Crop the image to the screen.  */
655         if (dx > vxres || dy > vyres)
656                 return;
657         if (dx + width > vxres)
658                 width = vxres - dx;
659         if (dy + height > vyres)
660                 height = vyres - dy;
661
662         regs_base = par->tga_regs_base;
663         fb_base = par->tga_fb_base;
664
665         /* Expand the color values to fill 32-bits.  */
666         /* ??? Would be nice to notice colour changes elsewhere, so
667            that we can do this only when necessary.  */
668         fgcolor = image->fg_color;
669         bgcolor = image->bg_color;
670         if (is8bpp) {
671                 fgcolor |= fgcolor << 8;
672                 fgcolor |= fgcolor << 16;
673                 bgcolor |= bgcolor << 8;
674                 bgcolor |= bgcolor << 16;
675         } else {
676                 if (fgcolor < 16)
677                         fgcolor = ((u32 *)info->pseudo_palette)[fgcolor];
678                 if (bgcolor < 16)
679                         bgcolor = ((u32 *)info->pseudo_palette)[bgcolor];
680         }
681         __raw_writel(fgcolor, regs_base + TGA_FOREGROUND_REG);
682         __raw_writel(bgcolor, regs_base + TGA_BACKGROUND_REG);
683
684         /* Acquire proper alignment; set up the PIXELMASK register
685            so that we only write the proper character cell.  */
686         pos = dy * line_length;
687         if (is8bpp) {
688                 pos += dx;
689                 shift = pos & 3;
690                 pos &= -4;
691         } else {
692                 pos += dx * 4;
693                 shift = (pos & 7) >> 2;
694                 pos &= -8;
695         }
696
697         data = (const unsigned char *) image->data;
698
699         /* Enable opaque stipple mode.  */
700         __raw_writel((is8bpp
701                       ? TGA_MODE_SBM_8BPP | TGA_MODE_OPAQUE_STIPPLE
702                       : TGA_MODE_SBM_24BPP | TGA_MODE_OPAQUE_STIPPLE),
703                      regs_base + TGA_MODE_REG);
704
705         if (width + shift <= 32) {
706                 unsigned long bwidth;
707
708                 /* Handle common case of imaging a single character, in
709                    a font less than or 32 pixels wide.  */
710
711                 /* Avoid a shift by 32; width > 0 implied.  */
712                 pixelmask = (2ul << (width - 1)) - 1;
713                 pixelmask <<= shift;
714                 __raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
715                 wmb();
716
717                 bwidth = (width + 7) / 8;
718
719                 for (i = 0; i < height; ++i) {
720                         u32 mask = 0;
721
722                         /* The image data is bit big endian; we need
723                            little endian.  */
724                         for (j = 0; j < bwidth; ++j)
725                                 mask |= bitrev8(data[j]) << (j * 8);
726
727                         __raw_writel(mask << shift, fb_base + pos);
728
729                         pos += line_length;
730                         data += rincr;
731                 }
732                 wmb();
733                 __raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
734         } else if (shift == 0) {
735                 unsigned long pos0 = pos;
736                 const unsigned char *data0 = data;
737                 unsigned long bincr = (is8bpp ? 8 : 8*4);
738                 unsigned long bwidth;
739
740                 /* Handle another common case in which accel_putcs
741                    generates a large bitmap, which happens to be aligned.
742                    Allow the tail to be misaligned.  This case is 
743                    interesting because we've not got to hold partial
744                    bytes across the words being written.  */
745
746                 wmb();
747
748                 bwidth = (width / 8) & -4;
749                 for (i = 0; i < height; ++i) {
750                         for (j = 0; j < bwidth; j += 4) {
751                                 u32 mask = 0;
752                                 mask |= bitrev8(data[j+0]) << (0 * 8);
753                                 mask |= bitrev8(data[j+1]) << (1 * 8);
754                                 mask |= bitrev8(data[j+2]) << (2 * 8);
755                                 mask |= bitrev8(data[j+3]) << (3 * 8);
756                                 __raw_writel(mask, fb_base + pos + j*bincr);
757                         }
758                         pos += line_length;
759                         data += rincr;
760                 }
761                 wmb();
762
763                 pixelmask = (1ul << (width & 31)) - 1;
764                 if (pixelmask) {
765                         __raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
766                         wmb();
767
768                         pos = pos0 + bwidth*bincr;
769                         data = data0 + bwidth;
770                         bwidth = ((width & 31) + 7) / 8;
771
772                         for (i = 0; i < height; ++i) {
773                                 u32 mask = 0;
774                                 for (j = 0; j < bwidth; ++j)
775                                         mask |= bitrev8(data[j]) << (j * 8);
776                                 __raw_writel(mask, fb_base + pos);
777                                 pos += line_length;
778                                 data += rincr;
779                         }
780                         wmb();
781                         __raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
782                 }
783         } else {
784                 unsigned long pos0 = pos;
785                 const unsigned char *data0 = data;
786                 unsigned long bincr = (is8bpp ? 8 : 8*4);
787                 unsigned long bwidth;
788
789                 /* Finally, handle the generic case of misaligned start.
790                    Here we split the write into 16-bit spans.  This allows
791                    us to use only one pixel mask, instead of four as would
792                    be required by writing 24-bit spans.  */
793
794                 pixelmask = 0xffff << shift;
795                 __raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
796                 wmb();
797
798                 bwidth = (width / 8) & -2;
799                 for (i = 0; i < height; ++i) {
800                         for (j = 0; j < bwidth; j += 2) {
801                                 u32 mask = 0;
802                                 mask |= bitrev8(data[j+0]) << (0 * 8);
803                                 mask |= bitrev8(data[j+1]) << (1 * 8);
804                                 mask <<= shift;
805                                 __raw_writel(mask, fb_base + pos + j*bincr);
806                         }
807                         pos += line_length;
808                         data += rincr;
809                 }
810                 wmb();
811
812                 pixelmask = ((1ul << (width & 15)) - 1) << shift;
813                 if (pixelmask) {
814                         __raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
815                         wmb();
816
817                         pos = pos0 + bwidth*bincr;
818                         data = data0 + bwidth;
819                         bwidth = (width & 15) > 8;
820
821                         for (i = 0; i < height; ++i) {
822                                 u32 mask = bitrev8(data[0]);
823                                 if (bwidth)
824                                         mask |= bitrev8(data[1]) << 8;
825                                 mask <<= shift;
826                                 __raw_writel(mask, fb_base + pos);
827                                 pos += line_length;
828                                 data += rincr;
829                         }
830                         wmb();
831                 }
832                 __raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
833         }
834
835         /* Disable opaque stipple mode.  */
836         __raw_writel((is8bpp
837                       ? TGA_MODE_SBM_8BPP | TGA_MODE_SIMPLE
838                       : TGA_MODE_SBM_24BPP | TGA_MODE_SIMPLE),
839                      regs_base + TGA_MODE_REG);
840 }
841
842 static void
843 tgafb_clut_imageblit(struct fb_info *info, const struct fb_image *image)
844 {
845         struct tga_par *par = (struct tga_par *) info->par;
846         u32 color, dx, dy, width, height, vxres, vyres;
847         u32 *palette = ((u32 *)info->pseudo_palette);
848         unsigned long pos, line_length, i, j;
849         const unsigned char *data;
850         void __iomem *regs_base, *fb_base;
851
852         dx = image->dx;
853         dy = image->dy;
854         width = image->width;
855         height = image->height;
856         vxres = info->var.xres_virtual;
857         vyres = info->var.yres_virtual;
858         line_length = info->fix.line_length;
859
860         /* Crop the image to the screen.  */
861         if (dx > vxres || dy > vyres)
862                 return;
863         if (dx + width > vxres)
864                 width = vxres - dx;
865         if (dy + height > vyres)
866                 height = vyres - dy;
867
868         regs_base = par->tga_regs_base;
869         fb_base = par->tga_fb_base;
870
871         pos = dy * line_length + (dx * 4);
872         data = image->data;
873
874         /* Now copy the image, color_expanding via the palette. */
875         for (i = 0; i < height; i++) {
876                 for (j = 0; j < width; j++) {
877                         color = palette[*data++];
878                         __raw_writel(color, fb_base + pos + j*4);
879                 }
880                 pos += line_length;
881         }
882 }
883
884 /**
885  *      tgafb_imageblit - REQUIRED function. Can use generic routines if
886  *                        non acclerated hardware and packed pixel based.
887  *                        Copies a image from system memory to the screen.
888  *
889  *      @info: frame buffer structure that represents a single frame buffer
890  *      @image: structure defining the image.
891  */
892 static void
893 tgafb_imageblit(struct fb_info *info, const struct fb_image *image)
894 {
895         unsigned int is8bpp = info->var.bits_per_pixel == 8;
896
897         /* If a mono image, regardless of FB depth, go do it. */
898         if (image->depth == 1) {
899                 tgafb_mono_imageblit(info, image);
900                 return;
901         }
902
903         /* For copies that aren't pixel expansion, there's little we
904            can do better than the generic code.  */
905         /* ??? There is a DMA write mode; I wonder if that could be
906            made to pull the data from the image buffer...  */
907         if (image->depth == info->var.bits_per_pixel) {
908                 cfb_imageblit(info, image);
909                 return;
910         }
911
912         /* If 24-plane FB and the image is 8-plane with CLUT, we can do it. */
913         if (!is8bpp && image->depth == 8) {
914                 tgafb_clut_imageblit(info, image);
915                 return;
916         }
917
918         /* Silently return... */
919 }
920
921 /**
922  *      tgafb_fillrect - REQUIRED function. Can use generic routines if 
923  *                       non acclerated hardware and packed pixel based.
924  *                       Draws a rectangle on the screen.               
925  *
926  *      @info: frame buffer structure that represents a single frame buffer
927  *      @rect: structure defining the rectagle and operation.
928  */
929 static void
930 tgafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
931 {
932         struct tga_par *par = (struct tga_par *) info->par;
933         int is8bpp = info->var.bits_per_pixel == 8;
934         u32 dx, dy, width, height, vxres, vyres, color;
935         unsigned long pos, align, line_length, i, j;
936         void __iomem *regs_base;
937         void __iomem *fb_base;
938
939         dx = rect->dx;
940         dy = rect->dy;
941         width = rect->width;
942         height = rect->height;
943         vxres = info->var.xres_virtual;
944         vyres = info->var.yres_virtual;
945         line_length = info->fix.line_length;
946         regs_base = par->tga_regs_base;
947         fb_base = par->tga_fb_base;
948
949         /* Crop the rectangle to the screen.  */
950         if (dx > vxres || dy > vyres || !width || !height)
951                 return;
952         if (dx + width > vxres)
953                 width = vxres - dx;
954         if (dy + height > vyres)
955                 height = vyres - dy;
956
957         pos = dy * line_length + dx * (is8bpp ? 1 : 4);
958
959         /* ??? We could implement ROP_XOR with opaque fill mode
960            and a RasterOp setting of GXxor, but as far as I can
961            tell, this mode is not actually used in the kernel.
962            Thus I am ignoring it for now.  */
963         if (rect->rop != ROP_COPY) {
964                 cfb_fillrect(info, rect);
965                 return;
966         }
967
968         /* Expand the color value to fill 8 pixels.  */
969         color = rect->color;
970         if (is8bpp) {
971                 color |= color << 8;
972                 color |= color << 16;
973                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR0_REG);
974                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR1_REG);
975         } else {
976                 if (color < 16)
977                         color = ((u32 *)info->pseudo_palette)[color];
978                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR0_REG);
979                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR1_REG);
980                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR2_REG);
981                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR3_REG);
982                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR4_REG);
983                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR5_REG);
984                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR6_REG);
985                 __raw_writel(color, regs_base + TGA_BLOCK_COLOR7_REG);
986         }
987
988         /* The DATA register holds the fill mask for block fill mode.
989            Since we're not stippling, this is all ones.  */
990         __raw_writel(0xffffffff, regs_base + TGA_DATA_REG);
991
992         /* Enable block fill mode.  */
993         __raw_writel((is8bpp
994                       ? TGA_MODE_SBM_8BPP | TGA_MODE_BLOCK_FILL
995                       : TGA_MODE_SBM_24BPP | TGA_MODE_BLOCK_FILL),
996                      regs_base + TGA_MODE_REG);
997         wmb();
998
999         /* We can fill 2k pixels per operation.  Notice blocks that fit
1000            the width of the screen so that we can take advantage of this
1001            and fill more than one line per write.  */
1002         if (width == line_length)
1003                 width *= height, height = 1;
1004
1005         /* The write into the frame buffer must be aligned to 4 bytes,
1006            but we are allowed to encode the offset within the word in
1007            the data word written.  */
1008         align = (pos & 3) << 16;
1009         pos &= -4;
1010
1011         if (width <= 2048) {
1012                 u32 data;
1013
1014                 data = (width - 1) | align;
1015
1016                 for (i = 0; i < height; ++i) {
1017                         __raw_writel(data, fb_base + pos);
1018                         pos += line_length;
1019                 }
1020         } else {
1021                 unsigned long Bpp = (is8bpp ? 1 : 4);
1022                 unsigned long nwidth = width & -2048;
1023                 u32 fdata, ldata;
1024
1025                 fdata = (2048 - 1) | align;
1026                 ldata = ((width & 2047) - 1) | align;
1027
1028                 for (i = 0; i < height; ++i) {
1029                         for (j = 0; j < nwidth; j += 2048)
1030                                 __raw_writel(fdata, fb_base + pos + j*Bpp);
1031                         if (j < width)
1032                                 __raw_writel(ldata, fb_base + pos + j*Bpp);
1033                         pos += line_length;
1034                 }
1035         }
1036         wmb();
1037
1038         /* Disable block fill mode.  */
1039         __raw_writel((is8bpp
1040                       ? TGA_MODE_SBM_8BPP | TGA_MODE_SIMPLE
1041                       : TGA_MODE_SBM_24BPP | TGA_MODE_SIMPLE),
1042                      regs_base + TGA_MODE_REG);
1043 }
1044
1045 /**
1046  *      tgafb_copyarea - REQUIRED function. Can use generic routines if
1047  *                       non acclerated hardware and packed pixel based.
1048  *                       Copies on area of the screen to another area.
1049  *
1050  *      @info: frame buffer structure that represents a single frame buffer
1051  *      @area: structure defining the source and destination.
1052  */
1053
1054 /* Handle the special case of copying entire lines, e.g. during scrolling.
1055    We can avoid a lot of needless computation in this case.  In the 8bpp
1056    case we need to use the COPY64 registers instead of mask writes into 
1057    the frame buffer to achieve maximum performance.  */
1058
1059 static inline void
1060 copyarea_line_8bpp(struct fb_info *info, u32 dy, u32 sy,
1061                    u32 height, u32 width)
1062 {
1063         struct tga_par *par = (struct tga_par *) info->par;
1064         void __iomem *tga_regs = par->tga_regs_base;
1065         unsigned long dpos, spos, i, n64;
1066
1067         /* Set up the MODE and PIXELSHIFT registers.  */
1068         __raw_writel(TGA_MODE_SBM_8BPP | TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
1069         __raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
1070         wmb();
1071
1072         n64 = (height * width) / 64;
1073
1074         if (sy < dy) {
1075                 spos = (sy + height) * width;
1076                 dpos = (dy + height) * width;
1077
1078                 for (i = 0; i < n64; ++i) {
1079                         spos -= 64;
1080                         dpos -= 64;
1081                         __raw_writel(spos, tga_regs+TGA_COPY64_SRC);
1082                         wmb();
1083                         __raw_writel(dpos, tga_regs+TGA_COPY64_DST);
1084                         wmb();
1085                 }
1086         } else {
1087                 spos = sy * width;
1088                 dpos = dy * width;
1089
1090                 for (i = 0; i < n64; ++i) {
1091                         __raw_writel(spos, tga_regs+TGA_COPY64_SRC);
1092                         wmb();
1093                         __raw_writel(dpos, tga_regs+TGA_COPY64_DST);
1094                         wmb();
1095                         spos += 64;
1096                         dpos += 64;
1097                 }
1098         }
1099
1100         /* Reset the MODE register to normal.  */
1101         __raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
1102 }
1103
1104 static inline void
1105 copyarea_line_32bpp(struct fb_info *info, u32 dy, u32 sy,
1106                     u32 height, u32 width)
1107 {
1108         struct tga_par *par = (struct tga_par *) info->par;
1109         void __iomem *tga_regs = par->tga_regs_base;
1110         void __iomem *tga_fb = par->tga_fb_base;
1111         void __iomem *src;
1112         void __iomem *dst;
1113         unsigned long i, n16;
1114
1115         /* Set up the MODE and PIXELSHIFT registers.  */
1116         __raw_writel(TGA_MODE_SBM_24BPP | TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
1117         __raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
1118         wmb();
1119
1120         n16 = (height * width) / 16;
1121
1122         if (sy < dy) {
1123                 src = tga_fb + (sy + height) * width * 4;
1124                 dst = tga_fb + (dy + height) * width * 4;
1125
1126                 for (i = 0; i < n16; ++i) {
1127                         src -= 64;
1128                         dst -= 64;
1129                         __raw_writel(0xffff, src);
1130                         wmb();
1131                         __raw_writel(0xffff, dst);
1132                         wmb();
1133                 }
1134         } else {
1135                 src = tga_fb + sy * width * 4;
1136                 dst = tga_fb + dy * width * 4;
1137
1138                 for (i = 0; i < n16; ++i) {
1139                         __raw_writel(0xffff, src);
1140                         wmb();
1141                         __raw_writel(0xffff, dst);
1142                         wmb();
1143                         src += 64;
1144                         dst += 64;
1145                 }
1146         }
1147
1148         /* Reset the MODE register to normal.  */
1149         __raw_writel(TGA_MODE_SBM_24BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
1150 }
1151
1152 /* The (almost) general case of backward copy in 8bpp mode.  */
1153 static inline void
1154 copyarea_8bpp(struct fb_info *info, u32 dx, u32 dy, u32 sx, u32 sy,
1155               u32 height, u32 width, u32 line_length,
1156               const struct fb_copyarea *area)
1157 {
1158         struct tga_par *par = (struct tga_par *) info->par;
1159         unsigned i, yincr;
1160         int depos, sepos, backward, last_step, step;
1161         u32 mask_last;
1162         unsigned n32;
1163         void __iomem *tga_regs;
1164         void __iomem *tga_fb;
1165
1166         /* Do acceleration only if we are aligned on 8 pixels */
1167         if ((dx | sx | width) & 7) {
1168                 cfb_copyarea(info, area);
1169                 return;
1170         }
1171
1172         yincr = line_length;
1173         if (dy > sy) {
1174                 dy += height - 1;
1175                 sy += height - 1;
1176                 yincr = -yincr;
1177         }
1178         backward = dy == sy && dx > sx && dx < sx + width;
1179
1180         /* Compute the offsets and alignments in the frame buffer.
1181            More than anything else, these control how we do copies.  */
1182         depos = dy * line_length + dx;
1183         sepos = sy * line_length + sx;
1184         if (backward)
1185                 depos += width, sepos += width;
1186
1187         /* Next copy full words at a time.  */
1188         n32 = width / 32;
1189         last_step = width % 32;
1190
1191         /* Finally copy the unaligned head of the span.  */
1192         mask_last = (1ul << last_step) - 1;
1193
1194         if (!backward) {
1195                 step = 32;
1196                 last_step = 32;
1197         } else {
1198                 step = -32;
1199                 last_step = -last_step;
1200                 sepos -= 32;
1201                 depos -= 32;
1202         }
1203
1204         tga_regs = par->tga_regs_base;
1205         tga_fb = par->tga_fb_base;
1206
1207         /* Set up the MODE and PIXELSHIFT registers.  */
1208         __raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
1209         __raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
1210         wmb();
1211
1212         for (i = 0; i < height; ++i) {
1213                 unsigned long j;
1214                 void __iomem *sfb;
1215                 void __iomem *dfb;
1216
1217                 sfb = tga_fb + sepos;
1218                 dfb = tga_fb + depos;
1219
1220                 for (j = 0; j < n32; j++) {
1221                         if (j < 2 && j + 1 < n32 && !backward &&
1222                             !(((unsigned long)sfb | (unsigned long)dfb) & 63)) {
1223                                 do {
1224                                         __raw_writel(sfb - tga_fb, tga_regs+TGA_COPY64_SRC);
1225                                         wmb();
1226                                         __raw_writel(dfb - tga_fb, tga_regs+TGA_COPY64_DST);
1227                                         wmb();
1228                                         sfb += 64;
1229                                         dfb += 64;
1230                                         j += 2;
1231                                 } while (j + 1 < n32);
1232                                 j--;
1233                                 continue;
1234                         }
1235                         __raw_writel(0xffffffff, sfb);
1236                         wmb();
1237                         __raw_writel(0xffffffff, dfb);
1238                         wmb();
1239                         sfb += step;
1240                         dfb += step;
1241                 }
1242
1243                 if (mask_last) {
1244                         sfb += last_step - step;
1245                         dfb += last_step - step;
1246                         __raw_writel(mask_last, sfb);
1247                         wmb();
1248                         __raw_writel(mask_last, dfb);
1249                         wmb();
1250                 }
1251
1252                 sepos += yincr;
1253                 depos += yincr;
1254         }
1255
1256         /* Reset the MODE register to normal.  */
1257         __raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
1258 }
1259
1260 static void
1261 tgafb_copyarea(struct fb_info *info, const struct fb_copyarea *area) 
1262 {
1263         unsigned long dx, dy, width, height, sx, sy, vxres, vyres;
1264         unsigned long line_length, bpp;
1265
1266         dx = area->dx;
1267         dy = area->dy;
1268         width = area->width;
1269         height = area->height;
1270         sx = area->sx;
1271         sy = area->sy;
1272         vxres = info->var.xres_virtual;
1273         vyres = info->var.yres_virtual;
1274         line_length = info->fix.line_length;
1275
1276         /* The top left corners must be in the virtual screen.  */
1277         if (dx > vxres || sx > vxres || dy > vyres || sy > vyres)
1278                 return;
1279
1280         /* Clip the destination.  */
1281         if (dx + width > vxres)
1282                 width = vxres - dx;
1283         if (dy + height > vyres)
1284                 height = vyres - dy;
1285
1286         /* The source must be completely inside the virtual screen.  */
1287         if (sx + width > vxres || sy + height > vyres)
1288                 return;
1289
1290         bpp = info->var.bits_per_pixel;
1291
1292         /* Detect copies of the entire line.  */
1293         if (width * (bpp >> 3) == line_length) {
1294                 if (bpp == 8)
1295                         copyarea_line_8bpp(info, dy, sy, height, width);
1296                 else
1297                         copyarea_line_32bpp(info, dy, sy, height, width);
1298         }
1299
1300         /* ??? The documentation is unclear to me exactly how the pixelshift
1301            register works in 32bpp mode.  Since I don't have hardware to test,
1302            give up for now and fall back on the generic routines.  */
1303         else if (bpp == 32)
1304                 cfb_copyarea(info, area);
1305
1306         else
1307                 copyarea_8bpp(info, dx, dy, sx, sy, height,
1308                               width, line_length, area);
1309 }
1310
1311
1312 /*
1313  *  Initialisation
1314  */
1315
1316 static void
1317 tgafb_init_fix(struct fb_info *info)
1318 {
1319         struct tga_par *par = (struct tga_par *)info->par;
1320         int tga_bus_pci = TGA_BUS_PCI(par->dev);
1321         int tga_bus_tc = TGA_BUS_TC(par->dev);
1322         u8 tga_type = par->tga_type;
1323         const char *tga_type_name = NULL;
1324         unsigned memory_size;
1325
1326         switch (tga_type) {
1327         case TGA_TYPE_8PLANE:
1328                 if (tga_bus_pci)
1329                         tga_type_name = "Digital ZLXp-E1";
1330                 if (tga_bus_tc)
1331                         tga_type_name = "Digital ZLX-E1";
1332                 memory_size = 2097152;
1333                 break;
1334         case TGA_TYPE_24PLANE:
1335                 if (tga_bus_pci)
1336                         tga_type_name = "Digital ZLXp-E2";
1337                 if (tga_bus_tc)
1338                         tga_type_name = "Digital ZLX-E2";
1339                 memory_size = 8388608;
1340                 break;
1341         case TGA_TYPE_24PLUSZ:
1342                 if (tga_bus_pci)
1343                         tga_type_name = "Digital ZLXp-E3";
1344                 if (tga_bus_tc)
1345                         tga_type_name = "Digital ZLX-E3";
1346                 memory_size = 16777216;
1347                 break;
1348         default:
1349                 tga_type_name = "Unknown";
1350                 memory_size = 16777216;
1351                 break;
1352         }
1353
1354         strlcpy(info->fix.id, tga_type_name, sizeof(info->fix.id));
1355
1356         info->fix.type = FB_TYPE_PACKED_PIXELS;
1357         info->fix.type_aux = 0;
1358         info->fix.visual = (tga_type == TGA_TYPE_8PLANE
1359                             ? FB_VISUAL_PSEUDOCOLOR
1360                             : FB_VISUAL_DIRECTCOLOR);
1361
1362         info->fix.smem_start = (size_t) par->tga_fb_base;
1363         info->fix.smem_len = memory_size;
1364         info->fix.mmio_start = (size_t) par->tga_regs_base;
1365         info->fix.mmio_len = 512;
1366
1367         info->fix.xpanstep = 0;
1368         info->fix.ypanstep = 0;
1369         info->fix.ywrapstep = 0;
1370
1371         info->fix.accel = FB_ACCEL_DEC_TGA;
1372
1373         /*
1374          * These are needed by fb_set_logo_truepalette(), so we
1375          * set them here for 24-plane cards.
1376          */
1377         if (tga_type != TGA_TYPE_8PLANE) {
1378                 info->var.red.length = 8;
1379                 info->var.green.length = 8;
1380                 info->var.blue.length = 8;
1381                 info->var.red.offset = 16;
1382                 info->var.green.offset = 8;
1383                 info->var.blue.offset = 0;
1384         }
1385 }
1386
1387 static int tgafb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1388 {
1389         /* We just use this to catch switches out of graphics mode. */
1390         tgafb_set_par(info); /* A bit of overkill for BASE_ADDR reset. */
1391         return 0;
1392 }
1393
1394 static int __devinit
1395 tgafb_register(struct device *dev)
1396 {
1397         static const struct fb_videomode modedb_tc = {
1398                 /* 1280x1024 @ 72 Hz, 76.8 kHz hsync */
1399                 "1280x1024@72", 0, 1280, 1024, 7645, 224, 28, 33, 3, 160, 3,
1400                 FB_SYNC_ON_GREEN, FB_VMODE_NONINTERLACED
1401         };
1402
1403         static unsigned int const fb_offset_presets[4] = {
1404                 TGA_8PLANE_FB_OFFSET,
1405                 TGA_24PLANE_FB_OFFSET,
1406                 0xffffffff,
1407                 TGA_24PLUSZ_FB_OFFSET
1408         };
1409
1410         const struct fb_videomode *modedb_tga = NULL;
1411         resource_size_t bar0_start = 0, bar0_len = 0;
1412         const char *mode_option_tga = NULL;
1413         int tga_bus_pci = TGA_BUS_PCI(dev);
1414         int tga_bus_tc = TGA_BUS_TC(dev);
1415         unsigned int modedbsize_tga = 0;
1416         void __iomem *mem_base;
1417         struct fb_info *info;
1418         struct tga_par *par;
1419         u8 tga_type;
1420         int ret = 0;
1421
1422         /* Enable device in PCI config.  */
1423         if (tga_bus_pci && pci_enable_device(to_pci_dev(dev))) {
1424                 printk(KERN_ERR "tgafb: Cannot enable PCI device\n");
1425                 return -ENODEV;
1426         }
1427
1428         /* Allocate the fb and par structures.  */
1429         info = framebuffer_alloc(sizeof(struct tga_par), dev);
1430         if (!info) {
1431                 printk(KERN_ERR "tgafb: Cannot allocate memory\n");
1432                 return -ENOMEM;
1433         }
1434
1435         par = info->par;
1436         dev_set_drvdata(dev, info);
1437
1438         /* Request the mem regions.  */
1439         ret = -ENODEV;
1440         if (tga_bus_pci) {
1441                 bar0_start = pci_resource_start(to_pci_dev(dev), 0);
1442                 bar0_len = pci_resource_len(to_pci_dev(dev), 0);
1443         }
1444         if (tga_bus_tc) {
1445                 bar0_start = to_tc_dev(dev)->resource.start;
1446                 bar0_len = to_tc_dev(dev)->resource.end - bar0_start + 1;
1447         }
1448         if (!request_mem_region (bar0_start, bar0_len, "tgafb")) {
1449                 printk(KERN_ERR "tgafb: cannot reserve FB region\n");
1450                 goto err0;
1451         }
1452
1453         /* Map the framebuffer.  */
1454         mem_base = ioremap_nocache(bar0_start, bar0_len);
1455         if (!mem_base) {
1456                 printk(KERN_ERR "tgafb: Cannot map MMIO\n");
1457                 goto err1;
1458         }
1459
1460         /* Grab info about the card.  */
1461         tga_type = (readl(mem_base) >> 12) & 0x0f;
1462         par->dev = dev;
1463         par->tga_mem_base = mem_base;
1464         par->tga_fb_base = mem_base + fb_offset_presets[tga_type];
1465         par->tga_regs_base = mem_base + TGA_REGS_OFFSET;
1466         par->tga_type = tga_type;
1467         if (tga_bus_pci)
1468                 par->tga_chip_rev = (to_pci_dev(dev))->revision;
1469         if (tga_bus_tc)
1470                 par->tga_chip_rev = TGA_READ_REG(par, TGA_START_REG) & 0xff;
1471
1472         /* Setup framebuffer.  */
1473         info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
1474                       FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT;
1475         info->fbops = &tgafb_ops;
1476         info->screen_base = par->tga_fb_base;
1477         info->pseudo_palette = par->palette;
1478
1479         /* This should give a reasonable default video mode.  */
1480         if (tga_bus_pci) {
1481                 mode_option_tga = mode_option_pci;
1482         }
1483         if (tga_bus_tc) {
1484                 mode_option_tga = mode_option_tc;
1485                 modedb_tga = &modedb_tc;
1486                 modedbsize_tga = 1;
1487         }
1488
1489         tgafb_init_fix(info);
1490
1491         ret = fb_find_mode(&info->var, info,
1492                            mode_option ? mode_option : mode_option_tga,
1493                            modedb_tga, modedbsize_tga, NULL,
1494                            tga_type == TGA_TYPE_8PLANE ? 8 : 32);
1495         if (ret == 0 || ret == 4) {
1496                 printk(KERN_ERR "tgafb: Could not find valid video mode\n");
1497                 ret = -EINVAL;
1498                 goto err1;
1499         }
1500
1501         if (fb_alloc_cmap(&info->cmap, 256, 0)) {
1502                 printk(KERN_ERR "tgafb: Could not allocate color map\n");
1503                 ret = -ENOMEM;
1504                 goto err1;
1505         }
1506
1507         tgafb_set_par(info);
1508
1509         if (register_framebuffer(info) < 0) {
1510                 printk(KERN_ERR "tgafb: Could not register framebuffer\n");
1511                 ret = -EINVAL;
1512                 goto err2;
1513         }
1514
1515         if (tga_bus_pci) {
1516                 pr_info("tgafb: DC21030 [TGA] detected, rev=0x%02x\n",
1517                         par->tga_chip_rev);
1518                 pr_info("tgafb: at PCI bus %d, device %d, function %d\n",
1519                         to_pci_dev(dev)->bus->number,
1520                         PCI_SLOT(to_pci_dev(dev)->devfn),
1521                         PCI_FUNC(to_pci_dev(dev)->devfn));
1522         }
1523         if (tga_bus_tc)
1524                 pr_info("tgafb: SFB+ detected, rev=0x%02x\n",
1525                         par->tga_chip_rev);
1526         pr_info("fb%d: %s frame buffer device at 0x%lx\n",
1527                 info->node, info->fix.id, (long)bar0_start);
1528
1529         return 0;
1530
1531  err2:
1532         fb_dealloc_cmap(&info->cmap);
1533  err1:
1534         if (mem_base)
1535                 iounmap(mem_base);
1536         release_mem_region(bar0_start, bar0_len);
1537  err0:
1538         framebuffer_release(info);
1539         return ret;
1540 }
1541
1542 static void __devexit
1543 tgafb_unregister(struct device *dev)
1544 {
1545         resource_size_t bar0_start = 0, bar0_len = 0;
1546         int tga_bus_pci = TGA_BUS_PCI(dev);
1547         int tga_bus_tc = TGA_BUS_TC(dev);
1548         struct fb_info *info = NULL;
1549         struct tga_par *par;
1550
1551         info = dev_get_drvdata(dev);
1552         if (!info)
1553                 return;
1554
1555         par = info->par;
1556         unregister_framebuffer(info);
1557         fb_dealloc_cmap(&info->cmap);
1558         iounmap(par->tga_mem_base);
1559         if (tga_bus_pci) {
1560                 bar0_start = pci_resource_start(to_pci_dev(dev), 0);
1561                 bar0_len = pci_resource_len(to_pci_dev(dev), 0);
1562         }
1563         if (tga_bus_tc) {
1564                 bar0_start = to_tc_dev(dev)->resource.start;
1565                 bar0_len = to_tc_dev(dev)->resource.end - bar0_start + 1;
1566         }
1567         release_mem_region(bar0_start, bar0_len);
1568         framebuffer_release(info);
1569 }
1570
1571 static void __devexit
1572 tgafb_exit(void)
1573 {
1574         tc_unregister_driver(&tgafb_tc_driver);
1575         pci_unregister_driver(&tgafb_pci_driver);
1576 }
1577
1578 #ifndef MODULE
1579 static int __devinit
1580 tgafb_setup(char *arg)
1581 {
1582         char *this_opt;
1583
1584         if (arg && *arg) {
1585                 while ((this_opt = strsep(&arg, ","))) {
1586                         if (!*this_opt)
1587                                 continue;
1588                         if (!strncmp(this_opt, "mode:", 5))
1589                                 mode_option = this_opt+5;
1590                         else
1591                                 printk(KERN_ERR
1592                                        "tgafb: unknown parameter %s\n",
1593                                        this_opt);
1594                 }
1595         }
1596
1597         return 0;
1598 }
1599 #endif /* !MODULE */
1600
1601 static int __devinit
1602 tgafb_init(void)
1603 {
1604         int status;
1605 #ifndef MODULE
1606         char *option = NULL;
1607
1608         if (fb_get_options("tgafb", &option))
1609                 return -ENODEV;
1610         tgafb_setup(option);
1611 #endif
1612         status = pci_register_driver(&tgafb_pci_driver);
1613         if (!status)
1614                 status = tc_register_driver(&tgafb_tc_driver);
1615         return status;
1616 }
1617
1618 /*
1619  *  Modularisation
1620  */
1621
1622 module_init(tgafb_init);
1623 module_exit(tgafb_exit);
1624
1625 MODULE_DESCRIPTION("Framebuffer driver for TGA/SFB+ chipset");
1626 MODULE_LICENSE("GPL");