2 * linux/drivers/video/s3fb.c -- Frame buffer device driver for S3 Trio/Virge
4 * Copyright (c) 2006-2007 Ondrej Zajicek <santiago@crfreenet.org>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive for
10 * Code is based on David Boucher's viafb (http://davesdomain.org.uk/viafb/)
11 * which is based on the code of neofb.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
19 #include <linux/tty.h>
20 #include <linux/delay.h>
22 #include <linux/svga.h>
23 #include <linux/init.h>
24 #include <linux/pci.h>
25 #include <linux/console.h> /* Why should fb driver call console functions? because console_lock() */
26 #include <video/vga.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-algo-bit.h>
36 int chip
, rev
, mclk_freq
;
38 struct vgastate state
;
39 struct mutex open_lock
;
40 unsigned int ref_count
;
41 u32 pseudo_palette
[16];
42 #ifdef CONFIG_FB_S3_DDC
45 struct i2c_adapter ddc_adapter
;
46 struct i2c_algo_bit_data ddc_algo
;
51 /* ------------------------------------------------------------------------- */
53 static const struct svga_fb_format s3fb_formats
[] = {
54 { 0, {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0}, 0,
55 FB_TYPE_TEXT
, FB_AUX_TEXT_SVGA_STEP4
, FB_VISUAL_PSEUDOCOLOR
, 8, 16},
56 { 4, {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0}, 0,
57 FB_TYPE_PACKED_PIXELS
, 0, FB_VISUAL_PSEUDOCOLOR
, 8, 16},
58 { 4, {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0}, 1,
59 FB_TYPE_INTERLEAVED_PLANES
, 1, FB_VISUAL_PSEUDOCOLOR
, 8, 16},
60 { 8, {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0}, 0,
61 FB_TYPE_PACKED_PIXELS
, 0, FB_VISUAL_PSEUDOCOLOR
, 4, 8},
62 {16, {10, 5, 0}, {5, 5, 0}, {0, 5, 0}, {0, 0, 0}, 0,
63 FB_TYPE_PACKED_PIXELS
, 0, FB_VISUAL_TRUECOLOR
, 2, 4},
64 {16, {11, 5, 0}, {5, 6, 0}, {0, 5, 0}, {0, 0, 0}, 0,
65 FB_TYPE_PACKED_PIXELS
, 0, FB_VISUAL_TRUECOLOR
, 2, 4},
66 {24, {16, 8, 0}, {8, 8, 0}, {0, 8, 0}, {0, 0, 0}, 0,
67 FB_TYPE_PACKED_PIXELS
, 0, FB_VISUAL_TRUECOLOR
, 1, 2},
68 {32, {16, 8, 0}, {8, 8, 0}, {0, 8, 0}, {0, 0, 0}, 0,
69 FB_TYPE_PACKED_PIXELS
, 0, FB_VISUAL_TRUECOLOR
, 1, 2},
74 static const struct svga_pll s3_pll
= {3, 129, 3, 33, 0, 3,
75 35000, 240000, 14318};
76 static const struct svga_pll s3_trio3d_pll
= {3, 129, 3, 31, 0, 4,
77 230000, 460000, 14318};
79 static const int s3_memsizes
[] = {4096, 0, 3072, 8192, 2048, 6144, 1024, 512};
81 static const char * const s3_names
[] = {"S3 Unknown", "S3 Trio32", "S3 Trio64", "S3 Trio64V+",
82 "S3 Trio64UV+", "S3 Trio64V2/DX", "S3 Trio64V2/GX",
83 "S3 Plato/PX", "S3 Aurora64V+", "S3 Virge",
84 "S3 Virge/VX", "S3 Virge/DX", "S3 Virge/GX",
85 "S3 Virge/GX2", "S3 Virge/GX2+", "",
86 "S3 Trio3D/1X", "S3 Trio3D/2X", "S3 Trio3D/2X",
89 #define CHIP_UNKNOWN 0x00
90 #define CHIP_732_TRIO32 0x01
91 #define CHIP_764_TRIO64 0x02
92 #define CHIP_765_TRIO64VP 0x03
93 #define CHIP_767_TRIO64UVP 0x04
94 #define CHIP_775_TRIO64V2_DX 0x05
95 #define CHIP_785_TRIO64V2_GX 0x06
96 #define CHIP_551_PLATO_PX 0x07
97 #define CHIP_M65_AURORA64VP 0x08
98 #define CHIP_325_VIRGE 0x09
99 #define CHIP_988_VIRGE_VX 0x0A
100 #define CHIP_375_VIRGE_DX 0x0B
101 #define CHIP_385_VIRGE_GX 0x0C
102 #define CHIP_357_VIRGE_GX2 0x0D
103 #define CHIP_359_VIRGE_GX2P 0x0E
104 #define CHIP_360_TRIO3D_1X 0x10
105 #define CHIP_362_TRIO3D_2X 0x11
106 #define CHIP_368_TRIO3D_2X 0x12
107 #define CHIP_365_TRIO3D 0x13
109 #define CHIP_XXX_TRIO 0x80
110 #define CHIP_XXX_TRIO64V2_DXGX 0x81
111 #define CHIP_XXX_VIRGE_DXGX 0x82
112 #define CHIP_36X_TRIO3D_1X_2X 0x83
114 #define CHIP_UNDECIDED_FLAG 0x80
115 #define CHIP_MASK 0xFF
117 #define MMIO_OFFSET 0x1000000
118 #define MMIO_SIZE 0x10000
120 /* CRT timing register sets */
122 static const struct vga_regset s3_h_total_regs
[] = {{0x00, 0, 7}, {0x5D, 0, 0}, VGA_REGSET_END
};
123 static const struct vga_regset s3_h_display_regs
[] = {{0x01, 0, 7}, {0x5D, 1, 1}, VGA_REGSET_END
};
124 static const struct vga_regset s3_h_blank_start_regs
[] = {{0x02, 0, 7}, {0x5D, 2, 2}, VGA_REGSET_END
};
125 static const struct vga_regset s3_h_blank_end_regs
[] = {{0x03, 0, 4}, {0x05, 7, 7}, VGA_REGSET_END
};
126 static const struct vga_regset s3_h_sync_start_regs
[] = {{0x04, 0, 7}, {0x5D, 4, 4}, VGA_REGSET_END
};
127 static const struct vga_regset s3_h_sync_end_regs
[] = {{0x05, 0, 4}, VGA_REGSET_END
};
129 static const struct vga_regset s3_v_total_regs
[] = {{0x06, 0, 7}, {0x07, 0, 0}, {0x07, 5, 5}, {0x5E, 0, 0}, VGA_REGSET_END
};
130 static const struct vga_regset s3_v_display_regs
[] = {{0x12, 0, 7}, {0x07, 1, 1}, {0x07, 6, 6}, {0x5E, 1, 1}, VGA_REGSET_END
};
131 static const struct vga_regset s3_v_blank_start_regs
[] = {{0x15, 0, 7}, {0x07, 3, 3}, {0x09, 5, 5}, {0x5E, 2, 2}, VGA_REGSET_END
};
132 static const struct vga_regset s3_v_blank_end_regs
[] = {{0x16, 0, 7}, VGA_REGSET_END
};
133 static const struct vga_regset s3_v_sync_start_regs
[] = {{0x10, 0, 7}, {0x07, 2, 2}, {0x07, 7, 7}, {0x5E, 4, 4}, VGA_REGSET_END
};
134 static const struct vga_regset s3_v_sync_end_regs
[] = {{0x11, 0, 3}, VGA_REGSET_END
};
136 static const struct vga_regset s3_line_compare_regs
[] = {{0x18, 0, 7}, {0x07, 4, 4}, {0x09, 6, 6}, {0x5E, 6, 6}, VGA_REGSET_END
};
137 static const struct vga_regset s3_start_address_regs
[] = {{0x0d, 0, 7}, {0x0c, 0, 7}, {0x69, 0, 4}, VGA_REGSET_END
};
138 static const struct vga_regset s3_offset_regs
[] = {{0x13, 0, 7}, {0x51, 4, 5}, VGA_REGSET_END
}; /* set 0x43 bit 2 to 0 */
140 static const struct vga_regset s3_dtpc_regs
[] = {{0x3B, 0, 7}, {0x5D, 6, 6}, VGA_REGSET_END
};
142 static const struct svga_timing_regs s3_timing_regs
= {
143 s3_h_total_regs
, s3_h_display_regs
, s3_h_blank_start_regs
,
144 s3_h_blank_end_regs
, s3_h_sync_start_regs
, s3_h_sync_end_regs
,
145 s3_v_total_regs
, s3_v_display_regs
, s3_v_blank_start_regs
,
146 s3_v_blank_end_regs
, s3_v_sync_start_regs
, s3_v_sync_end_regs
,
150 /* ------------------------------------------------------------------------- */
152 /* Module parameters */
155 static char *mode_option __devinitdata
;
158 static int mtrr __devinitdata
= 1;
161 static int fasttext
= 1;
164 MODULE_AUTHOR("(c) 2006-2007 Ondrej Zajicek <santiago@crfreenet.org>");
165 MODULE_LICENSE("GPL");
166 MODULE_DESCRIPTION("fbdev driver for S3 Trio/Virge");
168 module_param(mode_option
, charp
, 0444);
169 MODULE_PARM_DESC(mode_option
, "Default video mode ('640x480-8@60', etc)");
170 module_param_named(mode
, mode_option
, charp
, 0444);
171 MODULE_PARM_DESC(mode
, "Default video mode ('640x480-8@60', etc) (deprecated)");
174 module_param(mtrr
, int, 0444);
175 MODULE_PARM_DESC(mtrr
, "Enable write-combining with MTRR (1=enable, 0=disable, default=1)");
178 module_param(fasttext
, int, 0644);
179 MODULE_PARM_DESC(fasttext
, "Enable S3 fast text mode (1=enable, 0=disable, default=1)");
182 /* ------------------------------------------------------------------------- */
184 #ifdef CONFIG_FB_S3_DDC
186 #define DDC_REG 0xaa /* Trio 3D/1X/2X */
187 #define DDC_MMIO_REG 0xff20 /* all other chips */
188 #define DDC_SCL_OUT (1 << 0)
189 #define DDC_SDA_OUT (1 << 1)
190 #define DDC_SCL_IN (1 << 2)
191 #define DDC_SDA_IN (1 << 3)
192 #define DDC_DRIVE_EN (1 << 4)
194 static bool s3fb_ddc_needs_mmio(int chip
)
196 return !(chip
== CHIP_360_TRIO3D_1X
||
197 chip
== CHIP_362_TRIO3D_2X
||
198 chip
== CHIP_368_TRIO3D_2X
);
201 static u8
s3fb_ddc_read(struct s3fb_info
*par
)
203 if (s3fb_ddc_needs_mmio(par
->chip
))
204 return readb(par
->mmio
+ DDC_MMIO_REG
);
206 return vga_rcrt(par
->state
.vgabase
, DDC_REG
);
209 static void s3fb_ddc_write(struct s3fb_info
*par
, u8 val
)
211 if (s3fb_ddc_needs_mmio(par
->chip
))
212 writeb(val
, par
->mmio
+ DDC_MMIO_REG
);
214 vga_wcrt(par
->state
.vgabase
, DDC_REG
, val
);
217 static void s3fb_ddc_setscl(void *data
, int val
)
219 struct s3fb_info
*par
= data
;
222 reg
= s3fb_ddc_read(par
) | DDC_DRIVE_EN
;
227 s3fb_ddc_write(par
, reg
);
230 static void s3fb_ddc_setsda(void *data
, int val
)
232 struct s3fb_info
*par
= data
;
235 reg
= s3fb_ddc_read(par
) | DDC_DRIVE_EN
;
240 s3fb_ddc_write(par
, reg
);
243 static int s3fb_ddc_getscl(void *data
)
245 struct s3fb_info
*par
= data
;
247 return !!(s3fb_ddc_read(par
) & DDC_SCL_IN
);
250 static int s3fb_ddc_getsda(void *data
)
252 struct s3fb_info
*par
= data
;
254 return !!(s3fb_ddc_read(par
) & DDC_SDA_IN
);
257 static int __devinit
s3fb_setup_ddc_bus(struct fb_info
*info
)
259 struct s3fb_info
*par
= info
->par
;
261 strlcpy(par
->ddc_adapter
.name
, info
->fix
.id
,
262 sizeof(par
->ddc_adapter
.name
));
263 par
->ddc_adapter
.owner
= THIS_MODULE
;
264 par
->ddc_adapter
.class = I2C_CLASS_DDC
;
265 par
->ddc_adapter
.algo_data
= &par
->ddc_algo
;
266 par
->ddc_adapter
.dev
.parent
= info
->device
;
267 par
->ddc_algo
.setsda
= s3fb_ddc_setsda
;
268 par
->ddc_algo
.setscl
= s3fb_ddc_setscl
;
269 par
->ddc_algo
.getsda
= s3fb_ddc_getsda
;
270 par
->ddc_algo
.getscl
= s3fb_ddc_getscl
;
271 par
->ddc_algo
.udelay
= 10;
272 par
->ddc_algo
.timeout
= 20;
273 par
->ddc_algo
.data
= par
;
275 i2c_set_adapdata(&par
->ddc_adapter
, par
);
278 * some Virge cards have external MUX to switch chip I2C bus between
279 * DDC and extension pins - switch it do DDC
281 /* vga_wseq(par->state.vgabase, 0x08, 0x06); - not needed, already unlocked */
282 if (par
->chip
== CHIP_357_VIRGE_GX2
||
283 par
->chip
== CHIP_359_VIRGE_GX2P
)
284 svga_wseq_mask(par
->state
.vgabase
, 0x0d, 0x01, 0x03);
286 svga_wseq_mask(par
->state
.vgabase
, 0x0d, 0x00, 0x03);
287 /* some Virge need this or the DDC is ignored */
288 svga_wcrt_mask(par
->state
.vgabase
, 0x5c, 0x03, 0x03);
290 return i2c_bit_add_bus(&par
->ddc_adapter
);
292 #endif /* CONFIG_FB_S3_DDC */
295 /* ------------------------------------------------------------------------- */
297 /* Set font in S3 fast text mode */
299 static void s3fb_settile_fast(struct fb_info
*info
, struct fb_tilemap
*map
)
301 const u8
*font
= map
->data
;
302 u8 __iomem
*fb
= (u8 __iomem
*) info
->screen_base
;
305 if ((map
->width
!= 8) || (map
->height
!= 16) ||
306 (map
->depth
!= 1) || (map
->length
!= 256)) {
307 printk(KERN_ERR
"fb%d: unsupported font parameters: width %d, height %d, depth %d, length %d\n",
308 info
->node
, map
->width
, map
->height
, map
->depth
, map
->length
);
313 for (i
= 0; i
< map
->height
; i
++) {
314 for (c
= 0; c
< map
->length
; c
++) {
315 fb_writeb(font
[c
* map
->height
+ i
], fb
+ c
* 4);
321 static void s3fb_tilecursor(struct fb_info
*info
, struct fb_tilecursor
*cursor
)
323 struct s3fb_info
*par
= info
->par
;
325 svga_tilecursor(par
->state
.vgabase
, info
, cursor
);
328 static struct fb_tile_ops s3fb_tile_ops
= {
329 .fb_settile
= svga_settile
,
330 .fb_tilecopy
= svga_tilecopy
,
331 .fb_tilefill
= svga_tilefill
,
332 .fb_tileblit
= svga_tileblit
,
333 .fb_tilecursor
= s3fb_tilecursor
,
334 .fb_get_tilemax
= svga_get_tilemax
,
337 static struct fb_tile_ops s3fb_fast_tile_ops
= {
338 .fb_settile
= s3fb_settile_fast
,
339 .fb_tilecopy
= svga_tilecopy
,
340 .fb_tilefill
= svga_tilefill
,
341 .fb_tileblit
= svga_tileblit
,
342 .fb_tilecursor
= s3fb_tilecursor
,
343 .fb_get_tilemax
= svga_get_tilemax
,
347 /* ------------------------------------------------------------------------- */
349 /* image data is MSB-first, fb structure is MSB-first too */
350 static inline u32
expand_color(u32 c
)
352 return ((c
& 1) | ((c
& 2) << 7) | ((c
& 4) << 14) | ((c
& 8) << 21)) * 0xFF;
355 /* s3fb_iplan_imageblit silently assumes that almost everything is 8-pixel aligned */
356 static void s3fb_iplan_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
358 u32 fg
= expand_color(image
->fg_color
);
359 u32 bg
= expand_color(image
->bg_color
);
360 const u8
*src1
, *src
;
367 dst1
= info
->screen_base
+ (image
->dy
* info
->fix
.line_length
)
368 + ((image
->dx
/ 8) * 4);
370 for (y
= 0; y
< image
->height
; y
++) {
372 dst
= (u32 __iomem
*) dst1
;
373 for (x
= 0; x
< image
->width
; x
+= 8) {
374 val
= *(src
++) * 0x01010101;
375 val
= (val
& fg
) | (~val
& bg
);
376 fb_writel(val
, dst
++);
378 src1
+= image
->width
/ 8;
379 dst1
+= info
->fix
.line_length
;
384 /* s3fb_iplan_fillrect silently assumes that almost everything is 8-pixel aligned */
385 static void s3fb_iplan_fillrect(struct fb_info
*info
, const struct fb_fillrect
*rect
)
387 u32 fg
= expand_color(rect
->color
);
392 dst1
= info
->screen_base
+ (rect
->dy
* info
->fix
.line_length
)
393 + ((rect
->dx
/ 8) * 4);
395 for (y
= 0; y
< rect
->height
; y
++) {
396 dst
= (u32 __iomem
*) dst1
;
397 for (x
= 0; x
< rect
->width
; x
+= 8) {
398 fb_writel(fg
, dst
++);
400 dst1
+= info
->fix
.line_length
;
405 /* image data is MSB-first, fb structure is high-nibble-in-low-byte-first */
406 static inline u32
expand_pixel(u32 c
)
408 return (((c
& 1) << 24) | ((c
& 2) << 27) | ((c
& 4) << 14) | ((c
& 8) << 17) |
409 ((c
& 16) << 4) | ((c
& 32) << 7) | ((c
& 64) >> 6) | ((c
& 128) >> 3)) * 0xF;
412 /* s3fb_cfb4_imageblit silently assumes that almost everything is 8-pixel aligned */
413 static void s3fb_cfb4_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
415 u32 fg
= image
->fg_color
* 0x11111111;
416 u32 bg
= image
->bg_color
* 0x11111111;
417 const u8
*src1
, *src
;
424 dst1
= info
->screen_base
+ (image
->dy
* info
->fix
.line_length
)
425 + ((image
->dx
/ 8) * 4);
427 for (y
= 0; y
< image
->height
; y
++) {
429 dst
= (u32 __iomem
*) dst1
;
430 for (x
= 0; x
< image
->width
; x
+= 8) {
431 val
= expand_pixel(*(src
++));
432 val
= (val
& fg
) | (~val
& bg
);
433 fb_writel(val
, dst
++);
435 src1
+= image
->width
/ 8;
436 dst1
+= info
->fix
.line_length
;
440 static void s3fb_imageblit(struct fb_info
*info
, const struct fb_image
*image
)
442 if ((info
->var
.bits_per_pixel
== 4) && (image
->depth
== 1)
443 && ((image
->width
% 8) == 0) && ((image
->dx
% 8) == 0)) {
444 if (info
->fix
.type
== FB_TYPE_INTERLEAVED_PLANES
)
445 s3fb_iplan_imageblit(info
, image
);
447 s3fb_cfb4_imageblit(info
, image
);
449 cfb_imageblit(info
, image
);
452 static void s3fb_fillrect(struct fb_info
*info
, const struct fb_fillrect
*rect
)
454 if ((info
->var
.bits_per_pixel
== 4)
455 && ((rect
->width
% 8) == 0) && ((rect
->dx
% 8) == 0)
456 && (info
->fix
.type
== FB_TYPE_INTERLEAVED_PLANES
))
457 s3fb_iplan_fillrect(info
, rect
);
459 cfb_fillrect(info
, rect
);
464 /* ------------------------------------------------------------------------- */
467 static void s3_set_pixclock(struct fb_info
*info
, u32 pixclock
)
469 struct s3fb_info
*par
= info
->par
;
474 rv
= svga_compute_pll((par
->chip
== CHIP_365_TRIO3D
) ? &s3_trio3d_pll
: &s3_pll
,
475 1000000000 / pixclock
, &m
, &n
, &r
, info
->node
);
477 printk(KERN_ERR
"fb%d: cannot set requested pixclock, keeping old value\n", info
->node
);
481 /* Set VGA misc register */
482 regval
= vga_r(par
->state
.vgabase
, VGA_MIS_R
);
483 vga_w(par
->state
.vgabase
, VGA_MIS_W
, regval
| VGA_MIS_ENB_PLL_LOAD
);
485 /* Set S3 clock registers */
486 if (par
->chip
== CHIP_357_VIRGE_GX2
||
487 par
->chip
== CHIP_359_VIRGE_GX2P
||
488 par
->chip
== CHIP_360_TRIO3D_1X
||
489 par
->chip
== CHIP_362_TRIO3D_2X
||
490 par
->chip
== CHIP_368_TRIO3D_2X
) {
491 vga_wseq(par
->state
.vgabase
, 0x12, (n
- 2) | ((r
& 3) << 6)); /* n and two bits of r */
492 vga_wseq(par
->state
.vgabase
, 0x29, r
>> 2); /* remaining highest bit of r */
494 vga_wseq(par
->state
.vgabase
, 0x12, (n
- 2) | (r
<< 5));
495 vga_wseq(par
->state
.vgabase
, 0x13, m
- 2);
499 /* Activate clock - write 0, 1, 0 to seq/15 bit 5 */
500 regval
= vga_rseq (par
->state
.vgabase
, 0x15); /* | 0x80; */
501 vga_wseq(par
->state
.vgabase
, 0x15, regval
& ~(1<<5));
502 vga_wseq(par
->state
.vgabase
, 0x15, regval
| (1<<5));
503 vga_wseq(par
->state
.vgabase
, 0x15, regval
& ~(1<<5));
507 /* Open framebuffer */
509 static int s3fb_open(struct fb_info
*info
, int user
)
511 struct s3fb_info
*par
= info
->par
;
513 mutex_lock(&(par
->open_lock
));
514 if (par
->ref_count
== 0) {
515 void __iomem
*vgabase
= par
->state
.vgabase
;
517 memset(&(par
->state
), 0, sizeof(struct vgastate
));
518 par
->state
.vgabase
= vgabase
;
519 par
->state
.flags
= VGA_SAVE_MODE
| VGA_SAVE_FONTS
| VGA_SAVE_CMAP
;
520 par
->state
.num_crtc
= 0x70;
521 par
->state
.num_seq
= 0x20;
522 save_vga(&(par
->state
));
526 mutex_unlock(&(par
->open_lock
));
531 /* Close framebuffer */
533 static int s3fb_release(struct fb_info
*info
, int user
)
535 struct s3fb_info
*par
= info
->par
;
537 mutex_lock(&(par
->open_lock
));
538 if (par
->ref_count
== 0) {
539 mutex_unlock(&(par
->open_lock
));
543 if (par
->ref_count
== 1)
544 restore_vga(&(par
->state
));
547 mutex_unlock(&(par
->open_lock
));
552 /* Validate passed in var */
554 static int s3fb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
556 struct s3fb_info
*par
= info
->par
;
560 /* Find appropriate format */
561 rv
= svga_match_format (s3fb_formats
, var
, NULL
);
563 /* 32bpp mode is not supported on VIRGE VX,
564 24bpp is not supported on others */
565 if ((par
->chip
== CHIP_988_VIRGE_VX
) ? (rv
== 7) : (rv
== 6))
569 printk(KERN_ERR
"fb%d: unsupported mode requested\n", info
->node
);
573 /* Do not allow to have real resoulution larger than virtual */
574 if (var
->xres
> var
->xres_virtual
)
575 var
->xres_virtual
= var
->xres
;
577 if (var
->yres
> var
->yres_virtual
)
578 var
->yres_virtual
= var
->yres
;
580 /* Round up xres_virtual to have proper alignment of lines */
581 step
= s3fb_formats
[rv
].xresstep
- 1;
582 var
->xres_virtual
= (var
->xres_virtual
+step
) & ~step
;
584 /* Check whether have enough memory */
585 mem
= ((var
->bits_per_pixel
* var
->xres_virtual
) >> 3) * var
->yres_virtual
;
586 if (mem
> info
->screen_size
) {
587 printk(KERN_ERR
"fb%d: not enough framebuffer memory (%d kB requested , %d kB available)\n",
588 info
->node
, mem
>> 10, (unsigned int) (info
->screen_size
>> 10));
592 rv
= svga_check_timings (&s3_timing_regs
, var
, info
->node
);
594 printk(KERN_ERR
"fb%d: invalid timings requested\n", info
->node
);
598 rv
= svga_compute_pll(&s3_pll
, PICOS2KHZ(var
->pixclock
), &m
, &n
, &r
,
601 printk(KERN_ERR
"fb%d: invalid pixclock value requested\n",
609 /* Set video mode from par */
611 static int s3fb_set_par(struct fb_info
*info
)
613 struct s3fb_info
*par
= info
->par
;
614 u32 value
, mode
, hmul
, offset_value
, screen_size
, multiplex
, dbytes
;
615 u32 bpp
= info
->var
.bits_per_pixel
;
619 info
->fix
.ypanstep
= 1;
620 info
->fix
.line_length
= (info
->var
.xres_virtual
* bpp
) / 8;
622 info
->flags
&= ~FBINFO_MISC_TILEBLITTING
;
623 info
->tileops
= NULL
;
625 /* in 4bpp supports 8p wide tiles only, any tiles otherwise */
626 info
->pixmap
.blit_x
= (bpp
== 4) ? (1 << (8 - 1)) : (~(u32
)0);
627 info
->pixmap
.blit_y
= ~(u32
)0;
629 offset_value
= (info
->var
.xres_virtual
* bpp
) / 64;
630 screen_size
= info
->var
.yres_virtual
* info
->fix
.line_length
;
632 info
->fix
.ypanstep
= 16;
633 info
->fix
.line_length
= 0;
635 info
->flags
|= FBINFO_MISC_TILEBLITTING
;
636 info
->tileops
= fasttext
? &s3fb_fast_tile_ops
: &s3fb_tile_ops
;
638 /* supports 8x16 tiles only */
639 info
->pixmap
.blit_x
= 1 << (8 - 1);
640 info
->pixmap
.blit_y
= 1 << (16 - 1);
642 offset_value
= info
->var
.xres_virtual
/ 16;
643 screen_size
= (info
->var
.xres_virtual
* info
->var
.yres_virtual
) / 64;
646 info
->var
.xoffset
= 0;
647 info
->var
.yoffset
= 0;
648 info
->var
.activate
= FB_ACTIVATE_NOW
;
650 /* Unlock registers */
651 vga_wcrt(par
->state
.vgabase
, 0x38, 0x48);
652 vga_wcrt(par
->state
.vgabase
, 0x39, 0xA5);
653 vga_wseq(par
->state
.vgabase
, 0x08, 0x06);
654 svga_wcrt_mask(par
->state
.vgabase
, 0x11, 0x00, 0x80);
656 /* Blank screen and turn off sync */
657 svga_wseq_mask(par
->state
.vgabase
, 0x01, 0x20, 0x20);
658 svga_wcrt_mask(par
->state
.vgabase
, 0x17, 0x00, 0x80);
660 /* Set default values */
661 svga_set_default_gfx_regs(par
->state
.vgabase
);
662 svga_set_default_atc_regs(par
->state
.vgabase
);
663 svga_set_default_seq_regs(par
->state
.vgabase
);
664 svga_set_default_crt_regs(par
->state
.vgabase
);
665 svga_wcrt_multi(par
->state
.vgabase
, s3_line_compare_regs
, 0xFFFFFFFF);
666 svga_wcrt_multi(par
->state
.vgabase
, s3_start_address_regs
, 0);
668 /* S3 specific initialization */
669 svga_wcrt_mask(par
->state
.vgabase
, 0x58, 0x10, 0x10); /* enable linear framebuffer */
670 svga_wcrt_mask(par
->state
.vgabase
, 0x31, 0x08, 0x08); /* enable sequencer access to framebuffer above 256 kB */
672 /* svga_wcrt_mask(par->state.vgabase, 0x33, 0x08, 0x08); */ /* DDR ? */
673 /* svga_wcrt_mask(par->state.vgabase, 0x43, 0x01, 0x01); */ /* DDR ? */
674 svga_wcrt_mask(par
->state
.vgabase
, 0x33, 0x00, 0x08); /* no DDR ? */
675 svga_wcrt_mask(par
->state
.vgabase
, 0x43, 0x00, 0x01); /* no DDR ? */
677 svga_wcrt_mask(par
->state
.vgabase
, 0x5D, 0x00, 0x28); /* Clear strange HSlen bits */
679 /* svga_wcrt_mask(par->state.vgabase, 0x58, 0x03, 0x03); */
681 /* svga_wcrt_mask(par->state.vgabase, 0x53, 0x12, 0x13); */ /* enable MMIO */
682 /* svga_wcrt_mask(par->state.vgabase, 0x40, 0x08, 0x08); */ /* enable write buffer */
685 /* Set the offset register */
686 pr_debug("fb%d: offset register : %d\n", info
->node
, offset_value
);
687 svga_wcrt_multi(par
->state
.vgabase
, s3_offset_regs
, offset_value
);
689 if (par
->chip
!= CHIP_357_VIRGE_GX2
&&
690 par
->chip
!= CHIP_359_VIRGE_GX2P
&&
691 par
->chip
!= CHIP_360_TRIO3D_1X
&&
692 par
->chip
!= CHIP_362_TRIO3D_2X
&&
693 par
->chip
!= CHIP_368_TRIO3D_2X
) {
694 vga_wcrt(par
->state
.vgabase
, 0x54, 0x18); /* M parameter */
695 vga_wcrt(par
->state
.vgabase
, 0x60, 0xff); /* N parameter */
696 vga_wcrt(par
->state
.vgabase
, 0x61, 0xff); /* L parameter */
697 vga_wcrt(par
->state
.vgabase
, 0x62, 0xff); /* L parameter */
700 vga_wcrt(par
->state
.vgabase
, 0x3A, 0x35);
701 svga_wattr(par
->state
.vgabase
, 0x33, 0x00);
703 if (info
->var
.vmode
& FB_VMODE_DOUBLE
)
704 svga_wcrt_mask(par
->state
.vgabase
, 0x09, 0x80, 0x80);
706 svga_wcrt_mask(par
->state
.vgabase
, 0x09, 0x00, 0x80);
708 if (info
->var
.vmode
& FB_VMODE_INTERLACED
)
709 svga_wcrt_mask(par
->state
.vgabase
, 0x42, 0x20, 0x20);
711 svga_wcrt_mask(par
->state
.vgabase
, 0x42, 0x00, 0x20);
713 /* Disable hardware graphics cursor */
714 svga_wcrt_mask(par
->state
.vgabase
, 0x45, 0x00, 0x01);
715 /* Disable Streams engine */
716 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x00, 0x0C);
718 mode
= svga_match_format(s3fb_formats
, &(info
->var
), &(info
->fix
));
720 /* S3 virge DX hack */
721 if (par
->chip
== CHIP_375_VIRGE_DX
) {
722 vga_wcrt(par
->state
.vgabase
, 0x86, 0x80);
723 vga_wcrt(par
->state
.vgabase
, 0x90, 0x00);
726 /* S3 virge VX hack */
727 if (par
->chip
== CHIP_988_VIRGE_VX
) {
728 vga_wcrt(par
->state
.vgabase
, 0x50, 0x00);
729 vga_wcrt(par
->state
.vgabase
, 0x67, 0x50);
730 msleep(10); /* screen remains blank sometimes without this */
731 vga_wcrt(par
->state
.vgabase
, 0x63, (mode
<= 2) ? 0x90 : 0x09);
732 vga_wcrt(par
->state
.vgabase
, 0x66, 0x90);
735 if (par
->chip
== CHIP_357_VIRGE_GX2
||
736 par
->chip
== CHIP_359_VIRGE_GX2P
||
737 par
->chip
== CHIP_360_TRIO3D_1X
||
738 par
->chip
== CHIP_362_TRIO3D_2X
||
739 par
->chip
== CHIP_368_TRIO3D_2X
||
740 par
->chip
== CHIP_365_TRIO3D
||
741 par
->chip
== CHIP_375_VIRGE_DX
||
742 par
->chip
== CHIP_385_VIRGE_GX
) {
743 dbytes
= info
->var
.xres
* ((bpp
+7)/8);
744 vga_wcrt(par
->state
.vgabase
, 0x91, (dbytes
+ 7) / 8);
745 vga_wcrt(par
->state
.vgabase
, 0x90, (((dbytes
+ 7) / 8) >> 8) | 0x80);
747 vga_wcrt(par
->state
.vgabase
, 0x66, 0x81);
750 if (par
->chip
== CHIP_357_VIRGE_GX2
||
751 par
->chip
== CHIP_359_VIRGE_GX2P
||
752 par
->chip
== CHIP_360_TRIO3D_1X
||
753 par
->chip
== CHIP_362_TRIO3D_2X
||
754 par
->chip
== CHIP_368_TRIO3D_2X
)
755 vga_wcrt(par
->state
.vgabase
, 0x34, 0x00);
756 else /* enable Data Transfer Position Control (DTPC) */
757 vga_wcrt(par
->state
.vgabase
, 0x34, 0x10);
759 svga_wcrt_mask(par
->state
.vgabase
, 0x31, 0x00, 0x40);
763 /* Set mode-specific register values */
766 pr_debug("fb%d: text mode\n", info
->node
);
767 svga_set_textmode_vga_regs(par
->state
.vgabase
);
769 /* Set additional registers like in 8-bit mode */
770 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x00, 0x30);
771 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x00, 0xF0);
773 /* Disable enhanced mode */
774 svga_wcrt_mask(par
->state
.vgabase
, 0x3A, 0x00, 0x30);
777 pr_debug("fb%d: high speed text mode set\n", info
->node
);
778 svga_wcrt_mask(par
->state
.vgabase
, 0x31, 0x40, 0x40);
782 pr_debug("fb%d: 4 bit pseudocolor\n", info
->node
);
783 vga_wgfx(par
->state
.vgabase
, VGA_GFX_MODE
, 0x40);
785 /* Set additional registers like in 8-bit mode */
786 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x00, 0x30);
787 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x00, 0xF0);
789 /* disable enhanced mode */
790 svga_wcrt_mask(par
->state
.vgabase
, 0x3A, 0x00, 0x30);
793 pr_debug("fb%d: 4 bit pseudocolor, planar\n", info
->node
);
795 /* Set additional registers like in 8-bit mode */
796 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x00, 0x30);
797 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x00, 0xF0);
799 /* disable enhanced mode */
800 svga_wcrt_mask(par
->state
.vgabase
, 0x3A, 0x00, 0x30);
803 pr_debug("fb%d: 8 bit pseudocolor\n", info
->node
);
804 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x00, 0x30);
805 if (info
->var
.pixclock
> 20000 ||
806 par
->chip
== CHIP_357_VIRGE_GX2
||
807 par
->chip
== CHIP_359_VIRGE_GX2P
||
808 par
->chip
== CHIP_360_TRIO3D_1X
||
809 par
->chip
== CHIP_362_TRIO3D_2X
||
810 par
->chip
== CHIP_368_TRIO3D_2X
)
811 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x00, 0xF0);
813 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x10, 0xF0);
818 pr_debug("fb%d: 5/5/5 truecolor\n", info
->node
);
819 if (par
->chip
== CHIP_988_VIRGE_VX
) {
820 if (info
->var
.pixclock
> 20000)
821 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x20, 0xF0);
823 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x30, 0xF0);
824 } else if (par
->chip
== CHIP_365_TRIO3D
) {
825 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x10, 0x30);
826 if (info
->var
.pixclock
> 8695) {
827 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x30, 0xF0);
830 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x20, 0xF0);
834 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x10, 0x30);
835 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x30, 0xF0);
836 if (par
->chip
!= CHIP_357_VIRGE_GX2
&&
837 par
->chip
!= CHIP_359_VIRGE_GX2P
&&
838 par
->chip
!= CHIP_360_TRIO3D_1X
&&
839 par
->chip
!= CHIP_362_TRIO3D_2X
&&
840 par
->chip
!= CHIP_368_TRIO3D_2X
)
845 pr_debug("fb%d: 5/6/5 truecolor\n", info
->node
);
846 if (par
->chip
== CHIP_988_VIRGE_VX
) {
847 if (info
->var
.pixclock
> 20000)
848 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x40, 0xF0);
850 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x50, 0xF0);
851 } else if (par
->chip
== CHIP_365_TRIO3D
) {
852 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x10, 0x30);
853 if (info
->var
.pixclock
> 8695) {
854 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x50, 0xF0);
857 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x40, 0xF0);
861 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x10, 0x30);
862 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0x50, 0xF0);
863 if (par
->chip
!= CHIP_357_VIRGE_GX2
&&
864 par
->chip
!= CHIP_359_VIRGE_GX2P
&&
865 par
->chip
!= CHIP_360_TRIO3D_1X
&&
866 par
->chip
!= CHIP_362_TRIO3D_2X
&&
867 par
->chip
!= CHIP_368_TRIO3D_2X
)
873 pr_debug("fb%d: 8/8/8 truecolor\n", info
->node
);
874 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0xD0, 0xF0);
877 pr_debug("fb%d: 8/8/8/8 truecolor\n", info
->node
);
878 svga_wcrt_mask(par
->state
.vgabase
, 0x50, 0x30, 0x30);
879 svga_wcrt_mask(par
->state
.vgabase
, 0x67, 0xD0, 0xF0);
882 printk(KERN_ERR
"fb%d: unsupported mode - bug\n", info
->node
);
886 if (par
->chip
!= CHIP_988_VIRGE_VX
) {
887 svga_wseq_mask(par
->state
.vgabase
, 0x15, multiplex
? 0x10 : 0x00, 0x10);
888 svga_wseq_mask(par
->state
.vgabase
, 0x18, multiplex
? 0x80 : 0x00, 0x80);
891 s3_set_pixclock(info
, info
->var
.pixclock
);
892 svga_set_timings(par
->state
.vgabase
, &s3_timing_regs
, &(info
->var
), hmul
, 1,
893 (info
->var
.vmode
& FB_VMODE_DOUBLE
) ? 2 : 1,
894 (info
->var
.vmode
& FB_VMODE_INTERLACED
) ? 2 : 1,
897 /* Set interlaced mode start/end register */
898 htotal
= info
->var
.xres
+ info
->var
.left_margin
+ info
->var
.right_margin
+ info
->var
.hsync_len
;
899 htotal
= ((htotal
* hmul
) / 8) - 5;
900 vga_wcrt(par
->state
.vgabase
, 0x3C, (htotal
+ 1) / 2);
902 /* Set Data Transfer Position */
903 hsstart
= ((info
->var
.xres
+ info
->var
.right_margin
) * hmul
) / 8;
904 /* + 2 is needed for Virge/VX, does no harm on other cards */
905 value
= clamp((htotal
+ hsstart
+ 1) / 2 + 2, hsstart
+ 4, htotal
+ 1);
906 svga_wcrt_multi(par
->state
.vgabase
, s3_dtpc_regs
, value
);
908 memset_io(info
->screen_base
, 0x00, screen_size
);
909 /* Device and screen back on */
910 svga_wcrt_mask(par
->state
.vgabase
, 0x17, 0x80, 0x80);
911 svga_wseq_mask(par
->state
.vgabase
, 0x01, 0x00, 0x20);
916 /* Set a colour register */
918 static int s3fb_setcolreg(u_int regno
, u_int red
, u_int green
, u_int blue
,
919 u_int transp
, struct fb_info
*fb
)
921 switch (fb
->var
.bits_per_pixel
) {
927 if ((fb
->var
.bits_per_pixel
== 4) &&
928 (fb
->var
.nonstd
== 0)) {
929 outb(0xF0, VGA_PEL_MSK
);
930 outb(regno
*16, VGA_PEL_IW
);
932 outb(0x0F, VGA_PEL_MSK
);
933 outb(regno
, VGA_PEL_IW
);
935 outb(red
>> 10, VGA_PEL_D
);
936 outb(green
>> 10, VGA_PEL_D
);
937 outb(blue
>> 10, VGA_PEL_D
);
943 outb(0xFF, VGA_PEL_MSK
);
944 outb(regno
, VGA_PEL_IW
);
945 outb(red
>> 10, VGA_PEL_D
);
946 outb(green
>> 10, VGA_PEL_D
);
947 outb(blue
>> 10, VGA_PEL_D
);
953 if (fb
->var
.green
.length
== 5)
954 ((u32
*)fb
->pseudo_palette
)[regno
] = ((red
& 0xF800) >> 1) |
955 ((green
& 0xF800) >> 6) | ((blue
& 0xF800) >> 11);
956 else if (fb
->var
.green
.length
== 6)
957 ((u32
*)fb
->pseudo_palette
)[regno
] = (red
& 0xF800) |
958 ((green
& 0xFC00) >> 5) | ((blue
& 0xF800) >> 11);
966 ((u32
*)fb
->pseudo_palette
)[regno
] = ((red
& 0xFF00) << 8) |
967 (green
& 0xFF00) | ((blue
& 0xFF00) >> 8);
977 /* Set the display blanking state */
979 static int s3fb_blank(int blank_mode
, struct fb_info
*info
)
981 struct s3fb_info
*par
= info
->par
;
983 switch (blank_mode
) {
984 case FB_BLANK_UNBLANK
:
985 pr_debug("fb%d: unblank\n", info
->node
);
986 svga_wcrt_mask(par
->state
.vgabase
, 0x56, 0x00, 0x06);
987 svga_wseq_mask(par
->state
.vgabase
, 0x01, 0x00, 0x20);
989 case FB_BLANK_NORMAL
:
990 pr_debug("fb%d: blank\n", info
->node
);
991 svga_wcrt_mask(par
->state
.vgabase
, 0x56, 0x00, 0x06);
992 svga_wseq_mask(par
->state
.vgabase
, 0x01, 0x20, 0x20);
994 case FB_BLANK_HSYNC_SUSPEND
:
995 pr_debug("fb%d: hsync\n", info
->node
);
996 svga_wcrt_mask(par
->state
.vgabase
, 0x56, 0x02, 0x06);
997 svga_wseq_mask(par
->state
.vgabase
, 0x01, 0x20, 0x20);
999 case FB_BLANK_VSYNC_SUSPEND
:
1000 pr_debug("fb%d: vsync\n", info
->node
);
1001 svga_wcrt_mask(par
->state
.vgabase
, 0x56, 0x04, 0x06);
1002 svga_wseq_mask(par
->state
.vgabase
, 0x01, 0x20, 0x20);
1004 case FB_BLANK_POWERDOWN
:
1005 pr_debug("fb%d: sync down\n", info
->node
);
1006 svga_wcrt_mask(par
->state
.vgabase
, 0x56, 0x06, 0x06);
1007 svga_wseq_mask(par
->state
.vgabase
, 0x01, 0x20, 0x20);
1015 /* Pan the display */
1017 static int s3fb_pan_display(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1019 struct s3fb_info
*par
= info
->par
;
1020 unsigned int offset
;
1022 /* Calculate the offset */
1023 if (info
->var
.bits_per_pixel
== 0) {
1024 offset
= (var
->yoffset
/ 16) * (info
->var
.xres_virtual
/ 2)
1025 + (var
->xoffset
/ 2);
1026 offset
= offset
>> 2;
1028 offset
= (var
->yoffset
* info
->fix
.line_length
) +
1029 (var
->xoffset
* info
->var
.bits_per_pixel
/ 8);
1030 offset
= offset
>> 2;
1033 /* Set the offset */
1034 svga_wcrt_multi(par
->state
.vgabase
, s3_start_address_regs
, offset
);
1039 /* ------------------------------------------------------------------------- */
1041 /* Frame buffer operations */
1043 static struct fb_ops s3fb_ops
= {
1044 .owner
= THIS_MODULE
,
1045 .fb_open
= s3fb_open
,
1046 .fb_release
= s3fb_release
,
1047 .fb_check_var
= s3fb_check_var
,
1048 .fb_set_par
= s3fb_set_par
,
1049 .fb_setcolreg
= s3fb_setcolreg
,
1050 .fb_blank
= s3fb_blank
,
1051 .fb_pan_display
= s3fb_pan_display
,
1052 .fb_fillrect
= s3fb_fillrect
,
1053 .fb_copyarea
= cfb_copyarea
,
1054 .fb_imageblit
= s3fb_imageblit
,
1055 .fb_get_caps
= svga_get_caps
,
1058 /* ------------------------------------------------------------------------- */
1060 static int __devinit
s3_identification(struct s3fb_info
*par
)
1062 int chip
= par
->chip
;
1064 if (chip
== CHIP_XXX_TRIO
) {
1065 u8 cr30
= vga_rcrt(par
->state
.vgabase
, 0x30);
1066 u8 cr2e
= vga_rcrt(par
->state
.vgabase
, 0x2e);
1067 u8 cr2f
= vga_rcrt(par
->state
.vgabase
, 0x2f);
1069 if ((cr30
== 0xE0) || (cr30
== 0xE1)) {
1071 return CHIP_732_TRIO32
;
1073 if (! (cr2f
& 0x40))
1074 return CHIP_764_TRIO64
;
1076 return CHIP_765_TRIO64VP
;
1081 if (chip
== CHIP_XXX_TRIO64V2_DXGX
) {
1082 u8 cr6f
= vga_rcrt(par
->state
.vgabase
, 0x6f);
1084 if (! (cr6f
& 0x01))
1085 return CHIP_775_TRIO64V2_DX
;
1087 return CHIP_785_TRIO64V2_GX
;
1090 if (chip
== CHIP_XXX_VIRGE_DXGX
) {
1091 u8 cr6f
= vga_rcrt(par
->state
.vgabase
, 0x6f);
1093 if (! (cr6f
& 0x01))
1094 return CHIP_375_VIRGE_DX
;
1096 return CHIP_385_VIRGE_GX
;
1099 if (chip
== CHIP_36X_TRIO3D_1X_2X
) {
1100 switch (vga_rcrt(par
->state
.vgabase
, 0x2f)) {
1102 return CHIP_360_TRIO3D_1X
;
1104 return CHIP_362_TRIO3D_2X
;
1106 return CHIP_368_TRIO3D_2X
;
1110 return CHIP_UNKNOWN
;
1116 static int __devinit
s3_pci_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
1118 struct pci_bus_region bus_reg
;
1119 struct resource vga_res
;
1120 struct fb_info
*info
;
1121 struct s3fb_info
*par
;
1123 u8 regval
, cr38
, cr39
;
1126 /* Ignore secondary VGA device because there is no VGA arbitration */
1127 if (! svga_primary_device(dev
)) {
1128 dev_info(&(dev
->dev
), "ignoring secondary device\n");
1132 /* Allocate and fill driver data structure */
1133 info
= framebuffer_alloc(sizeof(struct s3fb_info
), &(dev
->dev
));
1135 dev_err(&(dev
->dev
), "cannot allocate memory\n");
1140 mutex_init(&par
->open_lock
);
1142 info
->flags
= FBINFO_PARTIAL_PAN_OK
| FBINFO_HWACCEL_YPAN
;
1143 info
->fbops
= &s3fb_ops
;
1145 /* Prepare PCI device */
1146 rc
= pci_enable_device(dev
);
1148 dev_err(info
->device
, "cannot enable PCI device\n");
1149 goto err_enable_device
;
1152 rc
= pci_request_regions(dev
, "s3fb");
1154 dev_err(info
->device
, "cannot reserve framebuffer region\n");
1155 goto err_request_regions
;
1159 info
->fix
.smem_start
= pci_resource_start(dev
, 0);
1160 info
->fix
.smem_len
= pci_resource_len(dev
, 0);
1162 /* Map physical IO memory address into kernel space */
1163 info
->screen_base
= pci_iomap(dev
, 0, 0);
1164 if (! info
->screen_base
) {
1166 dev_err(info
->device
, "iomap for framebuffer failed\n");
1171 bus_reg
.end
= 64 * 1024;
1173 vga_res
.flags
= IORESOURCE_IO
;
1175 pcibios_bus_to_resource(dev
, &vga_res
, &bus_reg
);
1177 par
->state
.vgabase
= (void __iomem
*) vga_res
.start
;
1180 cr38
= vga_rcrt(par
->state
.vgabase
, 0x38);
1181 cr39
= vga_rcrt(par
->state
.vgabase
, 0x39);
1182 vga_wseq(par
->state
.vgabase
, 0x08, 0x06);
1183 vga_wcrt(par
->state
.vgabase
, 0x38, 0x48);
1184 vga_wcrt(par
->state
.vgabase
, 0x39, 0xA5);
1186 /* Identify chip type */
1187 par
->chip
= id
->driver_data
& CHIP_MASK
;
1188 par
->rev
= vga_rcrt(par
->state
.vgabase
, 0x2f);
1189 if (par
->chip
& CHIP_UNDECIDED_FLAG
)
1190 par
->chip
= s3_identification(par
);
1192 /* Find how many physical memory there is on card */
1193 /* 0x36 register is accessible even if other registers are locked */
1194 regval
= vga_rcrt(par
->state
.vgabase
, 0x36);
1195 if (par
->chip
== CHIP_360_TRIO3D_1X
||
1196 par
->chip
== CHIP_362_TRIO3D_2X
||
1197 par
->chip
== CHIP_368_TRIO3D_2X
||
1198 par
->chip
== CHIP_365_TRIO3D
) {
1199 switch ((regval
& 0xE0) >> 5) {
1200 case 0: /* 8MB -- only 4MB usable for display */
1201 case 1: /* 4MB with 32-bit bus */
1203 info
->screen_size
= 4 << 20;
1205 case 4: /* 2MB on 365 Trio3D */
1207 info
->screen_size
= 2 << 20;
1210 } else if (par
->chip
== CHIP_357_VIRGE_GX2
||
1211 par
->chip
== CHIP_359_VIRGE_GX2P
) {
1212 switch ((regval
& 0xC0) >> 6) {
1214 info
->screen_size
= 4 << 20;
1217 info
->screen_size
= 2 << 20;
1220 } else if (par
->chip
== CHIP_988_VIRGE_VX
) {
1221 switch ((regval
& 0x60) >> 5) {
1223 info
->screen_size
= 2 << 20;
1226 info
->screen_size
= 4 << 20;
1229 info
->screen_size
= 6 << 20;
1232 info
->screen_size
= 8 << 20;
1235 /* off-screen memory */
1236 regval
= vga_rcrt(par
->state
.vgabase
, 0x37);
1237 switch ((regval
& 0x60) >> 5) {
1239 info
->screen_size
-= 4 << 20;
1242 info
->screen_size
-= 2 << 20;
1246 info
->screen_size
= s3_memsizes
[regval
>> 5] << 10;
1247 info
->fix
.smem_len
= info
->screen_size
;
1249 /* Find MCLK frequency */
1250 regval
= vga_rseq(par
->state
.vgabase
, 0x10);
1251 par
->mclk_freq
= ((vga_rseq(par
->state
.vgabase
, 0x11) + 2) * 14318) / ((regval
& 0x1F) + 2);
1252 par
->mclk_freq
= par
->mclk_freq
>> (regval
>> 5);
1255 vga_wcrt(par
->state
.vgabase
, 0x38, cr38
);
1256 vga_wcrt(par
->state
.vgabase
, 0x39, cr39
);
1258 strcpy(info
->fix
.id
, s3_names
[par
->chip
]);
1259 info
->fix
.mmio_start
= 0;
1260 info
->fix
.mmio_len
= 0;
1261 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1262 info
->fix
.visual
= FB_VISUAL_PSEUDOCOLOR
;
1263 info
->fix
.ypanstep
= 0;
1264 info
->fix
.accel
= FB_ACCEL_NONE
;
1265 info
->pseudo_palette
= (void*) (par
->pseudo_palette
);
1266 info
->var
.bits_per_pixel
= 8;
1268 #ifdef CONFIG_FB_S3_DDC
1269 /* Enable MMIO if needed */
1270 if (s3fb_ddc_needs_mmio(par
->chip
)) {
1271 par
->mmio
= ioremap(info
->fix
.smem_start
+ MMIO_OFFSET
, MMIO_SIZE
);
1273 svga_wcrt_mask(par
->state
.vgabase
, 0x53, 0x08, 0x08); /* enable MMIO */
1275 dev_err(info
->device
, "unable to map MMIO at 0x%lx, disabling DDC",
1276 info
->fix
.smem_start
+ MMIO_OFFSET
);
1278 if (!s3fb_ddc_needs_mmio(par
->chip
) || par
->mmio
)
1279 if (s3fb_setup_ddc_bus(info
) == 0) {
1280 u8
*edid
= fb_ddc_read(&par
->ddc_adapter
);
1281 par
->ddc_registered
= true;
1283 fb_edid_to_monspecs(edid
, &info
->monspecs
);
1285 if (!info
->monspecs
.modedb
)
1286 dev_err(info
->device
, "error getting mode database\n");
1288 const struct fb_videomode
*m
;
1290 fb_videomode_to_modelist(info
->monspecs
.modedb
,
1291 info
->monspecs
.modedb_len
,
1293 m
= fb_find_best_display(&info
->monspecs
, &info
->modelist
);
1295 fb_videomode_to_var(&info
->var
, m
);
1296 /* fill all other info->var's fields */
1297 if (s3fb_check_var(&info
->var
, info
) == 0)
1304 if (!mode_option
&& !found
)
1305 mode_option
= "640x480-8@60";
1307 /* Prepare startup mode */
1309 rc
= fb_find_mode(&info
->var
, info
, mode_option
,
1310 info
->monspecs
.modedb
, info
->monspecs
.modedb_len
,
1311 NULL
, info
->var
.bits_per_pixel
);
1312 if (!rc
|| rc
== 4) {
1314 dev_err(info
->device
, "mode %s not found\n", mode_option
);
1315 fb_destroy_modedb(info
->monspecs
.modedb
);
1316 info
->monspecs
.modedb
= NULL
;
1321 fb_destroy_modedb(info
->monspecs
.modedb
);
1322 info
->monspecs
.modedb
= NULL
;
1324 /* maximize virtual vertical size for fast scrolling */
1325 info
->var
.yres_virtual
= info
->fix
.smem_len
* 8 /
1326 (info
->var
.bits_per_pixel
* info
->var
.xres_virtual
);
1327 if (info
->var
.yres_virtual
< info
->var
.yres
) {
1328 dev_err(info
->device
, "virtual vertical size smaller than real\n");
1332 /* maximize virtual vertical size for fast scrolling */
1333 info
->var
.yres_virtual
= info
->fix
.smem_len
* 8 /
1334 (info
->var
.bits_per_pixel
* info
->var
.xres_virtual
);
1335 if (info
->var
.yres_virtual
< info
->var
.yres
) {
1336 dev_err(info
->device
, "virtual vertical size smaller than real\n");
1340 rc
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1342 dev_err(info
->device
, "cannot allocate colormap\n");
1343 goto err_alloc_cmap
;
1346 rc
= register_framebuffer(info
);
1348 dev_err(info
->device
, "cannot register framebuffer\n");
1352 printk(KERN_INFO
"fb%d: %s on %s, %d MB RAM, %d MHz MCLK\n", info
->node
, info
->fix
.id
,
1353 pci_name(dev
), info
->fix
.smem_len
>> 20, (par
->mclk_freq
+ 500) / 1000);
1355 if (par
->chip
== CHIP_UNKNOWN
)
1356 printk(KERN_INFO
"fb%d: unknown chip, CR2D=%x, CR2E=%x, CRT2F=%x, CRT30=%x\n",
1357 info
->node
, vga_rcrt(par
->state
.vgabase
, 0x2d), vga_rcrt(par
->state
.vgabase
, 0x2e),
1358 vga_rcrt(par
->state
.vgabase
, 0x2f), vga_rcrt(par
->state
.vgabase
, 0x30));
1360 /* Record a reference to the driver data */
1361 pci_set_drvdata(dev
, info
);
1366 par
->mtrr_reg
= mtrr_add(info
->fix
.smem_start
, info
->fix
.smem_len
, MTRR_TYPE_WRCOMB
, 1);
1372 /* Error handling */
1374 fb_dealloc_cmap(&info
->cmap
);
1377 #ifdef CONFIG_FB_S3_DDC
1378 if (par
->ddc_registered
)
1379 i2c_del_adapter(&par
->ddc_adapter
);
1383 pci_iounmap(dev
, info
->screen_base
);
1385 pci_release_regions(dev
);
1386 err_request_regions
:
1387 /* pci_disable_device(dev); */
1389 framebuffer_release(info
);
1396 static void __devexit
s3_pci_remove(struct pci_dev
*dev
)
1398 struct fb_info
*info
= pci_get_drvdata(dev
);
1399 struct s3fb_info __maybe_unused
*par
= info
->par
;
1404 if (par
->mtrr_reg
>= 0) {
1405 mtrr_del(par
->mtrr_reg
, 0, 0);
1410 unregister_framebuffer(info
);
1411 fb_dealloc_cmap(&info
->cmap
);
1413 #ifdef CONFIG_FB_S3_DDC
1414 if (par
->ddc_registered
)
1415 i2c_del_adapter(&par
->ddc_adapter
);
1420 pci_iounmap(dev
, info
->screen_base
);
1421 pci_release_regions(dev
);
1422 /* pci_disable_device(dev); */
1424 pci_set_drvdata(dev
, NULL
);
1425 framebuffer_release(info
);
1431 static int s3_pci_suspend(struct pci_dev
* dev
, pm_message_t state
)
1433 struct fb_info
*info
= pci_get_drvdata(dev
);
1434 struct s3fb_info
*par
= info
->par
;
1436 dev_info(info
->device
, "suspend\n");
1439 mutex_lock(&(par
->open_lock
));
1441 if ((state
.event
== PM_EVENT_FREEZE
) || (par
->ref_count
== 0)) {
1442 mutex_unlock(&(par
->open_lock
));
1447 fb_set_suspend(info
, 1);
1449 pci_save_state(dev
);
1450 pci_disable_device(dev
);
1451 pci_set_power_state(dev
, pci_choose_state(dev
, state
));
1453 mutex_unlock(&(par
->open_lock
));
1462 static int s3_pci_resume(struct pci_dev
* dev
)
1464 struct fb_info
*info
= pci_get_drvdata(dev
);
1465 struct s3fb_info
*par
= info
->par
;
1468 dev_info(info
->device
, "resume\n");
1471 mutex_lock(&(par
->open_lock
));
1473 if (par
->ref_count
== 0) {
1474 mutex_unlock(&(par
->open_lock
));
1479 pci_set_power_state(dev
, PCI_D0
);
1480 pci_restore_state(dev
);
1481 err
= pci_enable_device(dev
);
1483 mutex_unlock(&(par
->open_lock
));
1485 dev_err(info
->device
, "error %d enabling device for resume\n", err
);
1488 pci_set_master(dev
);
1491 fb_set_suspend(info
, 0);
1493 mutex_unlock(&(par
->open_lock
));
1500 /* List of boards that we are trying to support */
1502 static struct pci_device_id s3_devices
[] __devinitdata
= {
1503 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8810), .driver_data
= CHIP_XXX_TRIO
},
1504 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8811), .driver_data
= CHIP_XXX_TRIO
},
1505 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8812), .driver_data
= CHIP_M65_AURORA64VP
},
1506 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8814), .driver_data
= CHIP_767_TRIO64UVP
},
1507 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8901), .driver_data
= CHIP_XXX_TRIO64V2_DXGX
},
1508 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8902), .driver_data
= CHIP_551_PLATO_PX
},
1510 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x5631), .driver_data
= CHIP_325_VIRGE
},
1511 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x883D), .driver_data
= CHIP_988_VIRGE_VX
},
1512 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8A01), .driver_data
= CHIP_XXX_VIRGE_DXGX
},
1513 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8A10), .driver_data
= CHIP_357_VIRGE_GX2
},
1514 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8A11), .driver_data
= CHIP_359_VIRGE_GX2P
},
1515 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8A12), .driver_data
= CHIP_359_VIRGE_GX2P
},
1516 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8A13), .driver_data
= CHIP_36X_TRIO3D_1X_2X
},
1517 {PCI_DEVICE(PCI_VENDOR_ID_S3
, 0x8904), .driver_data
= CHIP_365_TRIO3D
},
1519 {0, 0, 0, 0, 0, 0, 0}
1523 MODULE_DEVICE_TABLE(pci
, s3_devices
);
1525 static struct pci_driver s3fb_pci_driver
= {
1527 .id_table
= s3_devices
,
1528 .probe
= s3_pci_probe
,
1529 .remove
= __devexit_p(s3_pci_remove
),
1530 .suspend
= s3_pci_suspend
,
1531 .resume
= s3_pci_resume
,
1534 /* Parse user specified options */
1537 static int __init
s3fb_setup(char *options
)
1541 if (!options
|| !*options
)
1544 while ((opt
= strsep(&options
, ",")) != NULL
) {
1549 else if (!strncmp(opt
, "mtrr:", 5))
1550 mtrr
= simple_strtoul(opt
+ 5, NULL
, 0);
1552 else if (!strncmp(opt
, "fasttext:", 9))
1553 fasttext
= simple_strtoul(opt
+ 9, NULL
, 0);
1564 static void __exit
s3fb_cleanup(void)
1566 pr_debug("s3fb: cleaning up\n");
1567 pci_unregister_driver(&s3fb_pci_driver
);
1570 /* Driver Initialisation */
1572 static int __init
s3fb_init(void)
1576 char *option
= NULL
;
1578 if (fb_get_options("s3fb", &option
))
1583 pr_debug("s3fb: initializing\n");
1584 return pci_register_driver(&s3fb_pci_driver
);
1587 /* ------------------------------------------------------------------------- */
1589 /* Modularization */
1591 module_init(s3fb_init
);
1592 module_exit(s3fb_cleanup
);