blob: 8ee4261abf3b604d9f4e9ff982e3458b5ce850f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Frame buffer driver for Trident Blade and Image series
3 *
Krzysztof Helt245a2c22007-10-16 01:28:42 -07004 * Copyright 2001, 2002 - Jani Monoses <jani@iv.ro>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 *
7 * CREDITS:(in order of appearance)
Krzysztof Helt245a2c22007-10-16 01:28:42 -07008 * skeletonfb.c by Geert Uytterhoeven and other fb code in drivers/video
9 * Special thanks ;) to Mattia Crivellini <tia@mclink.it>
10 * much inspired by the XFree86 4.x Trident driver sources
11 * by Alan Hourihane the FreeVGA project
12 * Francesco Salvestrini <salvestrini@users.sf.net> XP support,
13 * code, suggestions
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * TODO:
Krzysztof Helt245a2c22007-10-16 01:28:42 -070015 * timing value tweaking so it looks good on every monitor in every mode
16 * TGUI acceleration
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/fb.h>
21#include <linux/init.h>
22#include <linux/pci.h>
23
24#include <linux/delay.h>
Krzysztof Helt10172ed2008-07-23 21:30:54 -070025#include <video/vga.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <video/trident.h>
27
Krzysztof Helt122e8ad2008-07-23 21:30:52 -070028#define VERSION "0.7.9-NEWAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30struct tridentfb_par {
Krzysztof Helt245a2c22007-10-16 01:28:42 -070031 void __iomem *io_virt; /* iospace virtual memory address */
Krzysztof Heltea8ee552008-07-23 21:30:51 -070032 u32 pseudo_pal[16];
Krzysztof Helt122e8ad2008-07-23 21:30:52 -070033 int chip_id;
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070034 int flatpanel;
Krzysztof Heltd9cad042008-07-23 21:30:54 -070035 void (*init_accel) (struct tridentfb_par *, int, int);
36 void (*wait_engine) (struct tridentfb_par *);
37 void (*fill_rect)
38 (struct tridentfb_par *par, u32, u32, u32, u32, u32, u32);
39 void (*copy_rect)
40 (struct tridentfb_par *par, u32, u32, u32, u32, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
Krzysztof Helt245a2c22007-10-16 01:28:42 -070043static unsigned char eng_oper; /* engine operation... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static struct fb_ops tridentfb_ops;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static struct fb_fix_screeninfo tridentfb_fix = {
Krzysztof Helt245a2c22007-10-16 01:28:42 -070047 .id = "Trident",
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 .type = FB_TYPE_PACKED_PIXELS,
49 .ypanstep = 1,
50 .visual = FB_VISUAL_PSEUDOCOLOR,
51 .accel = FB_ACCEL_NONE,
52};
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* defaults which are normally overriden by user values */
55
56/* video mode */
Krzysztof Helt07f41e42008-04-28 02:15:06 -070057static char *mode_option __devinitdata = "640x480";
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070058static int bpp __devinitdata = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070060static int noaccel __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static int center;
63static int stretch;
64
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070065static int fp __devinitdata;
66static int crt __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070068static int memsize __devinitdata;
69static int memdiff __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int nativex;
71
Krzysztof Helt07f41e42008-04-28 02:15:06 -070072module_param(mode_option, charp, 0);
73MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
Krzysztof Helt9e3f0ca2008-04-28 02:15:10 -070074module_param_named(mode, mode_option, charp, 0);
75MODULE_PARM_DESC(mode, "Initial video mode e.g. '648x480-8@60' (deprecated)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076module_param(bpp, int, 0);
77module_param(center, int, 0);
78module_param(stretch, int, 0);
79module_param(noaccel, int, 0);
80module_param(memsize, int, 0);
81module_param(memdiff, int, 0);
82module_param(nativex, int, 0);
83module_param(fp, int, 0);
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070084MODULE_PARM_DESC(fp, "Define if flatpanel is connected");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085module_param(crt, int, 0);
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070086MODULE_PARM_DESC(crt, "Define if CRT is connected");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Krzysztof Helt6bdf1032008-07-23 21:30:56 -070088static int is_oldclock(int id)
89{
90 return (id == TGUI9660);
91}
92
Krzysztof Helte0759a52008-07-23 21:30:53 -070093static int is_blade(int id)
94{
95 return (id == BLADE3D) ||
96 (id == CYBERBLADEE4) ||
97 (id == CYBERBLADEi7) ||
98 (id == CYBERBLADEi7D) ||
99 (id == CYBERBLADEi1) ||
100 (id == CYBERBLADEi1D) ||
101 (id == CYBERBLADEAi1) ||
102 (id == CYBERBLADEAi1D);
103}
104
105static int is_xp(int id)
106{
107 return (id == CYBERBLADEXPAi1) ||
108 (id == CYBERBLADEXPm8) ||
109 (id == CYBERBLADEXPm16);
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int is3Dchip(int id)
113{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700114 return ((id == BLADE3D) || (id == CYBERBLADEE4) ||
115 (id == CYBERBLADEi7) || (id == CYBERBLADEi7D) ||
116 (id == CYBER9397) || (id == CYBER9397DVD) ||
117 (id == CYBER9520) || (id == CYBER9525DVD) ||
118 (id == IMAGE975) || (id == IMAGE985) ||
119 (id == CYBERBLADEi1) || (id == CYBERBLADEi1D) ||
120 (id == CYBERBLADEAi1) || (id == CYBERBLADEAi1D) ||
121 (id == CYBERBLADEXPm8) || (id == CYBERBLADEXPm16) ||
122 (id == CYBERBLADEXPAi1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static int iscyber(int id)
126{
127 switch (id) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700128 case CYBER9388:
129 case CYBER9382:
130 case CYBER9385:
131 case CYBER9397:
132 case CYBER9397DVD:
133 case CYBER9520:
134 case CYBER9525DVD:
135 case CYBERBLADEE4:
136 case CYBERBLADEi7D:
137 case CYBERBLADEi1:
138 case CYBERBLADEi1D:
139 case CYBERBLADEAi1:
140 case CYBERBLADEAi1D:
141 case CYBERBLADEXPAi1:
142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700144 case CYBER9320:
145 case TGUI9660:
146 case IMAGE975:
147 case IMAGE985:
148 case BLADE3D:
149 case CYBERBLADEi7: /* VIA MPV4 integrated version */
150
151 default:
152 /* case CYBERBLDAEXPm8: Strange */
153 /* case CYBERBLDAEXPm16: Strange */
154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156}
157
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700158static inline void t_outb(struct tridentfb_par *p, u8 val, u16 reg)
159{
160 fb_writeb(val, p->io_virt + reg);
161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700163static inline u8 t_inb(struct tridentfb_par *p, u16 reg)
164{
165 return fb_readb(p->io_virt + reg);
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700168static inline void writemmr(struct tridentfb_par *par, u16 r, u32 v)
169{
170 fb_writel(v, par->io_virt + r);
171}
172
173static inline u32 readmmr(struct tridentfb_par *par, u16 r)
174{
175 return fb_readl(par->io_virt + r);
176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * Blade specific acceleration.
180 */
181
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700182#define point(x, y) ((y) << 16 | (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#define STA 0x2120
184#define CMD 0x2144
185#define ROP 0x2148
186#define CLR 0x2160
187#define SR1 0x2100
188#define SR2 0x2104
189#define DR1 0x2108
190#define DR2 0x210C
191
192#define ROP_S 0xCC
193
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700194static void blade_init_accel(struct tridentfb_par *par, int pitch, int bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700196 int v1 = (pitch >> 3) << 20;
197 int tmp = 0, v2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700199 case 8:
200 tmp = 0;
201 break;
202 case 15:
203 tmp = 5;
204 break;
205 case 16:
206 tmp = 1;
207 break;
208 case 24:
209 case 32:
210 tmp = 2;
211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700213 v2 = v1 | (tmp << 29);
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700214 writemmr(par, 0x21C0, v2);
215 writemmr(par, 0x21C4, v2);
216 writemmr(par, 0x21B8, v2);
217 writemmr(par, 0x21BC, v2);
218 writemmr(par, 0x21D0, v1);
219 writemmr(par, 0x21D4, v1);
220 writemmr(par, 0x21C8, v1);
221 writemmr(par, 0x21CC, v1);
222 writemmr(par, 0x216C, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700225static void blade_wait_engine(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700227 while (readmmr(par, STA) & 0xFA800000) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700230static void blade_fill_rect(struct tridentfb_par *par,
231 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700233 writemmr(par, CLR, c);
234 writemmr(par, ROP, rop ? 0x66 : ROP_S);
235 writemmr(par, CMD, 0x20000000 | 1 << 19 | 1 << 4 | 2 << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700237 writemmr(par, DR1, point(x, y));
238 writemmr(par, DR2, point(x + w - 1, y + h - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700241static void blade_copy_rect(struct tridentfb_par *par,
242 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700244 u32 s1, s2, d1, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int direction = 2;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700246 s1 = point(x1, y1);
247 s2 = point(x1 + w - 1, y1 + h - 1);
248 d1 = point(x2, y2);
249 d2 = point(x2 + w - 1, y2 + h - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700252 direction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700254 writemmr(par, ROP, ROP_S);
255 writemmr(par, CMD, 0xE0000000 | 1 << 19 | 1 << 4 | 1 << 2 | direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700257 writemmr(par, SR1, direction ? s2 : s1);
258 writemmr(par, SR2, direction ? s1 : s2);
259 writemmr(par, DR1, direction ? d2 : d1);
260 writemmr(par, DR2, direction ? d1 : d2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
264 * BladeXP specific acceleration functions
265 */
266
267#define ROP_P 0xF0
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700268#define masked_point(x, y) ((y & 0xffff)<<16|(x & 0xffff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700270static void xp_init_accel(struct tridentfb_par *par, int pitch, int bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700272 int tmp = 0, v1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 unsigned char x = 0;
274
275 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700276 case 8:
277 x = 0;
278 break;
279 case 16:
280 x = 1;
281 break;
282 case 24:
283 x = 3;
284 break;
285 case 32:
286 x = 2;
287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 switch (pitch << (bpp >> 3)) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700291 case 8192:
292 case 512:
293 x |= 0x00;
294 break;
295 case 1024:
296 x |= 0x04;
297 break;
298 case 2048:
299 x |= 0x08;
300 break;
301 case 4096:
302 x |= 0x0C;
303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700306 t_outb(par, x, 0x2125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 eng_oper = x | 0x40;
309
310 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700311 case 8:
312 tmp = 18;
313 break;
314 case 15:
315 case 16:
316 tmp = 19;
317 break;
318 case 24:
319 case 32:
320 tmp = 20;
321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
324 v1 = pitch << tmp;
325
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700326 writemmr(par, 0x2154, v1);
327 writemmr(par, 0x2150, v1);
328 t_outb(par, 3, 0x2126);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700331static void xp_wait_engine(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 int busy;
334 int count, timeout;
335
336 count = 0;
337 timeout = 0;
338 for (;;) {
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700339 busy = t_inb(par, STA) & 0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (busy != 0x80)
341 return;
342 count++;
343 if (count == 10000000) {
344 /* Timeout */
345 count = 9990000;
346 timeout++;
347 if (timeout == 8) {
348 /* Reset engine */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700349 t_outb(par, 0x00, 0x2120);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return;
351 }
352 }
353 }
354}
355
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700356static void xp_fill_rect(struct tridentfb_par *par,
357 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700359 writemmr(par, 0x2127, ROP_P);
360 writemmr(par, 0x2158, c);
361 writemmr(par, 0x2128, 0x4000);
362 writemmr(par, 0x2140, masked_point(h, w));
363 writemmr(par, 0x2138, masked_point(y, x));
364 t_outb(par, 0x01, 0x2124);
365 t_outb(par, eng_oper, 0x2125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700368static void xp_copy_rect(struct tridentfb_par *par,
369 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 int direction;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700372 u32 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 direction = 0x0004;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if ((x1 < x2) && (y1 == y2)) {
377 direction |= 0x0200;
378 x1_tmp = x1 + w - 1;
379 x2_tmp = x2 + w - 1;
380 } else {
381 x1_tmp = x1;
382 x2_tmp = x2;
383 }
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (y1 < y2) {
386 direction |= 0x0100;
387 y1_tmp = y1 + h - 1;
388 y2_tmp = y2 + h - 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700389 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 y1_tmp = y1;
391 y2_tmp = y2;
392 }
393
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700394 writemmr(par, 0x2128, direction);
395 t_outb(par, ROP_S, 0x2127);
396 writemmr(par, 0x213C, masked_point(y1_tmp, x1_tmp));
397 writemmr(par, 0x2138, masked_point(y2_tmp, x2_tmp));
398 writemmr(par, 0x2140, masked_point(h, w));
399 t_outb(par, 0x01, 0x2124);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/*
403 * Image specific acceleration functions
404 */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700405static void image_init_accel(struct tridentfb_par *par, int pitch, int bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 int tmp = 0;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700408 switch (bpp) {
409 case 8:
410 tmp = 0;
411 break;
412 case 15:
413 tmp = 5;
414 break;
415 case 16:
416 tmp = 1;
417 break;
418 case 24:
419 case 32:
420 tmp = 2;
421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700423 writemmr(par, 0x2120, 0xF0000000);
424 writemmr(par, 0x2120, 0x40000000 | tmp);
425 writemmr(par, 0x2120, 0x80000000);
426 writemmr(par, 0x2144, 0x00000000);
427 writemmr(par, 0x2148, 0x00000000);
428 writemmr(par, 0x2150, 0x00000000);
429 writemmr(par, 0x2154, 0x00000000);
430 writemmr(par, 0x2120, 0x60000000 | (pitch << 16) | pitch);
431 writemmr(par, 0x216C, 0x00000000);
432 writemmr(par, 0x2170, 0x00000000);
433 writemmr(par, 0x217C, 0x00000000);
434 writemmr(par, 0x2120, 0x10000000);
435 writemmr(par, 0x2130, (2047 << 16) | 2047);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700438static void image_wait_engine(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700440 while (readmmr(par, 0x2164) & 0xF0000000) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700443static void image_fill_rect(struct tridentfb_par *par,
444 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700446 writemmr(par, 0x2120, 0x80000000);
447 writemmr(par, 0x2120, 0x90000000 | ROP_S);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700449 writemmr(par, 0x2144, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700451 writemmr(par, DR1, point(x, y));
452 writemmr(par, DR2, point(x + w - 1, y + h - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700454 writemmr(par, 0x2124, 0x80000000 | 3 << 22 | 1 << 10 | 1 << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700457static void image_copy_rect(struct tridentfb_par *par,
458 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700460 u32 s1, s2, d1, d2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int direction = 2;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700462 s1 = point(x1, y1);
463 s2 = point(x1 + w - 1, y1 + h - 1);
464 d1 = point(x2, y2);
465 d2 = point(x2 + w - 1, y2 + h - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700467 if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
468 direction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700470 writemmr(par, 0x2120, 0x80000000);
471 writemmr(par, 0x2120, 0x90000000 | ROP_S);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700473 writemmr(par, SR1, direction ? s2 : s1);
474 writemmr(par, SR2, direction ? s1 : s2);
475 writemmr(par, DR1, direction ? d2 : d1);
476 writemmr(par, DR2, direction ? d1 : d2);
477 writemmr(par, 0x2124,
478 0x80000000 | 1 << 22 | 1 << 10 | 1 << 7 | direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
482 * Accel functions called by the upper layers
483 */
484#ifdef CONFIG_FB_TRIDENT_ACCEL
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700485static void tridentfb_fillrect(struct fb_info *info,
486 const struct fb_fillrect *fr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700488 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int bpp = info->var.bits_per_pixel;
Antonino A. Daplas8dad46c2005-08-01 23:46:44 +0800490 int col = 0;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700493 default:
494 case 8:
495 col |= fr->color;
496 col |= col << 8;
497 col |= col << 16;
498 break;
499 case 16:
500 col = ((u32 *)(info->pseudo_palette))[fr->color];
501 break;
502 case 32:
503 col = ((u32 *)(info->pseudo_palette))[fr->color];
504 break;
505 }
506
Krzysztof Heltd9cad042008-07-23 21:30:54 -0700507 par->fill_rect(par, fr->dx, fr->dy, fr->width,
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700508 fr->height, col, fr->rop);
Krzysztof Heltd9cad042008-07-23 21:30:54 -0700509 par->wait_engine(par);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700511static void tridentfb_copyarea(struct fb_info *info,
512 const struct fb_copyarea *ca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700514 struct tridentfb_par *par = info->par;
515
Krzysztof Heltd9cad042008-07-23 21:30:54 -0700516 par->copy_rect(par, ca->sx, ca->sy, ca->dx, ca->dy,
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700517 ca->width, ca->height);
Krzysztof Heltd9cad042008-07-23 21:30:54 -0700518 par->wait_engine(par);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520#else /* !CONFIG_FB_TRIDENT_ACCEL */
521#define tridentfb_fillrect cfb_fillrect
522#define tridentfb_copyarea cfb_copyarea
523#endif /* CONFIG_FB_TRIDENT_ACCEL */
524
525
526/*
527 * Hardware access functions
528 */
529
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700530static inline unsigned char read3X4(struct tridentfb_par *par, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700532 return vga_mm_rcrt(par->io_virt, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700535static inline void write3X4(struct tridentfb_par *par, int reg,
536 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700538 vga_mm_wcrt(par->io_virt, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700541static inline unsigned char read3CE(struct tridentfb_par *par,
542 unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700544 return vga_mm_rgfx(par->io_virt, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700547static inline void writeAttr(struct tridentfb_par *par, int reg,
548 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700550 fb_readb(par->io_virt + VGA_IS1_RC); /* flip-flop to index */
551 vga_mm_wattr(par->io_virt, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700554static inline void write3CE(struct tridentfb_par *par, int reg,
555 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700557 vga_mm_wgfx(par->io_virt, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800560static void enable_mmio(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 /* Goto New Mode */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700563 vga_io_rseq(0x0B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* Unprotect registers */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700566 vga_io_wseq(NewMode1, 0x80);
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Enable MMIO */
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700569 outb(PCIReg, 0x3D4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 outb(inb(0x3D5) | 0x01, 0x3D5);
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800571}
572
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700573static void disable_mmio(struct tridentfb_par *par)
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800574{
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800575 /* Goto New Mode */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700576 vga_mm_rseq(par->io_virt, 0x0B);
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800577
578 /* Unprotect registers */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700579 vga_mm_wseq(par->io_virt, NewMode1, 0x80);
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800580
581 /* Disable MMIO */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700582 t_outb(par, PCIReg, 0x3D4);
583 t_outb(par, t_inb(par, 0x3D5) & ~0x01, 0x3D5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700586static void crtc_unlock(struct tridentfb_par *par)
587{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700588 write3X4(par, VGA_CRTC_V_SYNC_END,
589 read3X4(par, VGA_CRTC_V_SYNC_END) & 0x7F);
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700590}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592/* Return flat panel's maximum x resolution */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700593static int __devinit get_nativex(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700595 int x, y, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (nativex)
598 return nativex;
599
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700600 tmp = (read3CE(par, VertStretch) >> 4) & 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 switch (tmp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700603 case 0:
604 x = 1280; y = 1024;
605 break;
606 case 2:
607 x = 1024; y = 768;
608 break;
609 case 3:
610 x = 800; y = 600;
611 break;
612 case 4:
613 x = 1400; y = 1050;
614 break;
615 case 1:
616 default:
617 x = 640; y = 480;
618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
621 output("%dx%d flat panel found\n", x, y);
622 return x;
623}
624
625/* Set pitch */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700626static void set_lwidth(struct tridentfb_par *par, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700628 write3X4(par, VGA_CRTC_OFFSET, width & 0xFF);
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700629 write3X4(par, AddColReg,
630 (read3X4(par, AddColReg) & 0xCF) | ((width & 0x300) >> 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/* For resolutions smaller than FP resolution stretch */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700634static void screen_stretch(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Krzysztof Helt122e8ad2008-07-23 21:30:52 -0700636 if (par->chip_id != CYBERBLADEXPAi1)
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700637 write3CE(par, BiosReg, 0);
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700638 else
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700639 write3CE(par, BiosReg, 8);
640 write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 1);
641 write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/* For resolutions smaller than FP resolution center */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700645static void screen_center(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700647 write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 0x80);
648 write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/* Address of first shown pixel in display memory */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700652static void set_screen_start(struct tridentfb_par *par, int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700654 u8 tmp;
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700655 write3X4(par, VGA_CRTC_START_LO, base & 0xFF);
656 write3X4(par, VGA_CRTC_START_HI, (base & 0xFF00) >> 8);
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700657 tmp = read3X4(par, CRTCModuleTest) & 0xDF;
658 write3X4(par, CRTCModuleTest, tmp | ((base & 0x10000) >> 11));
659 tmp = read3X4(par, CRTHiOrd) & 0xF8;
660 write3X4(par, CRTHiOrd, tmp | ((base & 0xE0000) >> 17));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/* Set dotclock frequency */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700664static void set_vclk(struct tridentfb_par *par, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700666 int m, n, k;
Krzysztof Helt6bdf1032008-07-23 21:30:56 -0700667 unsigned long fi, d, di;
668 unsigned char best_m = 0, best_n = 0, best_k = 0;
669 unsigned char hi, lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Krzysztof Helt3f275ea2008-05-12 14:02:11 -0700671 d = 20000;
Krzysztof Helt6bdf1032008-07-23 21:30:56 -0700672 for (k = 1; k >= 0; k--)
673 for (m = 0; m < 32; m++)
674 for (n = 0; n < 122; n++) {
Krzysztof Helt3f275ea2008-05-12 14:02:11 -0700675 fi = ((14318l * (n + 8)) / (m + 2)) >> k;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700676 if ((di = abs(fi - freq)) < d) {
677 d = di;
Krzysztof Helt6bdf1032008-07-23 21:30:56 -0700678 best_n = n;
679 best_m = m;
680 best_k = k;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700681 }
Krzysztof Helt3f275ea2008-05-12 14:02:11 -0700682 if (fi > freq)
683 break;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700684 }
Krzysztof Helt6bdf1032008-07-23 21:30:56 -0700685
686 if (is_oldclock(par->chip_id)) {
687 lo = best_n | (best_m << 7);
688 hi = (best_m >> 1) | (best_k << 4);
689 } else {
690 lo = best_n;
691 hi = best_m | (best_k << 6);
692 }
693
Krzysztof Helt122e8ad2008-07-23 21:30:52 -0700694 if (is3Dchip(par->chip_id)) {
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700695 vga_mm_wseq(par->io_virt, ClockHigh, hi);
696 vga_mm_wseq(par->io_virt, ClockLow, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 } else {
Krzysztof Heltc1724fe2008-07-23 21:30:56 -0700698 t_outb(par, lo, 0x43C8);
699 t_outb(par, hi, 0x43C9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700701 debug("VCLK = %X %X\n", hi, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704/* Set number of lines for flat panels*/
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700705static void set_number_of_lines(struct tridentfb_par *par, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700707 int tmp = read3CE(par, CyberEnhance) & 0x8F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (lines > 1024)
709 tmp |= 0x50;
710 else if (lines > 768)
711 tmp |= 0x30;
712 else if (lines > 600)
713 tmp |= 0x20;
714 else if (lines > 480)
715 tmp |= 0x10;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700716 write3CE(par, CyberEnhance, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719/*
720 * If we see that FP is active we assume we have one.
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700721 * Otherwise we have a CRT display. User can override.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700723static int __devinit is_flatpanel(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 if (fp)
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700726 return 1;
Krzysztof Helt122e8ad2008-07-23 21:30:52 -0700727 if (crt || !iscyber(par->chip_id))
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700728 return 0;
729 return (read3CE(par, FPConfig) & 0x10) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732/* Try detecting the video memory size */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700733static unsigned int __devinit get_memsize(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 unsigned char tmp, tmp2;
736 unsigned int k;
737
738 /* If memory size provided by user */
739 if (memsize)
740 k = memsize * Kb;
741 else
Krzysztof Helt122e8ad2008-07-23 21:30:52 -0700742 switch (par->chip_id) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700743 case CYBER9525DVD:
744 k = 2560 * Kb;
745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 default:
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700747 tmp = read3X4(par, SPR) & 0x0F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 switch (tmp) {
749
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700750 case 0x01:
Krzysztof Heltb614ce82008-03-10 11:43:37 -0700751 k = 512 * Kb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700753 case 0x02:
754 k = 6 * Mb; /* XP */
755 break;
756 case 0x03:
757 k = 1 * Mb;
758 break;
759 case 0x04:
760 k = 8 * Mb;
761 break;
762 case 0x06:
763 k = 10 * Mb; /* XP */
764 break;
765 case 0x07:
766 k = 2 * Mb;
767 break;
768 case 0x08:
769 k = 12 * Mb; /* XP */
770 break;
771 case 0x0A:
772 k = 14 * Mb; /* XP */
773 break;
774 case 0x0C:
775 k = 16 * Mb; /* XP */
776 break;
777 case 0x0E: /* XP */
778
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700779 tmp2 = vga_mm_rseq(par->io_virt, 0xC1);
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700780 switch (tmp2) {
781 case 0x00:
782 k = 20 * Mb;
783 break;
784 case 0x01:
785 k = 24 * Mb;
786 break;
787 case 0x10:
788 k = 28 * Mb;
789 break;
790 case 0x11:
791 k = 32 * Mb;
792 break;
793 default:
794 k = 1 * Mb;
795 break;
796 }
797 break;
798
799 case 0x0F:
800 k = 4 * Mb;
801 break;
802 default:
803 k = 1 * Mb;
804 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 k -= memdiff * Kb;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700809 output("framebuffer size = %d Kb\n", k / Kb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return k;
811}
812
813/* See if we can handle the video mode described in var */
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700814static int tridentfb_check_var(struct fb_var_screeninfo *var,
815 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700817 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int bpp = var->bits_per_pixel;
819 debug("enter\n");
820
821 /* check color depth */
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700822 if (bpp == 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 bpp = var->bits_per_pixel = 32;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700824 /* check whether resolution fits on panel and in memory */
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700825 if (par->flatpanel && nativex && var->xres > nativex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -EINVAL;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700827 if (var->xres * var->yres_virtual * bpp / 8 > info->fix.smem_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EINVAL;
829
830 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700831 case 8:
832 var->red.offset = 0;
833 var->green.offset = 0;
834 var->blue.offset = 0;
835 var->red.length = 6;
836 var->green.length = 6;
837 var->blue.length = 6;
838 break;
839 case 16:
840 var->red.offset = 11;
841 var->green.offset = 5;
842 var->blue.offset = 0;
843 var->red.length = 5;
844 var->green.length = 6;
845 var->blue.length = 5;
846 break;
847 case 32:
848 var->red.offset = 16;
849 var->green.offset = 8;
850 var->blue.offset = 0;
851 var->red.length = 8;
852 var->green.length = 8;
853 var->blue.length = 8;
854 break;
855 default:
856 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 debug("exit\n");
859
860 return 0;
861
862}
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/* Pan the display */
865static int tridentfb_pan_display(struct fb_var_screeninfo *var,
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700866 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700868 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 unsigned int offset;
870
871 debug("enter\n");
872 offset = (var->xoffset + (var->yoffset * var->xres))
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700873 * var->bits_per_pixel / 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 info->var.xoffset = var->xoffset;
875 info->var.yoffset = var->yoffset;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700876 set_screen_start(par, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 debug("exit\n");
878 return 0;
879}
880
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700881static void shadowmode_on(struct tridentfb_par *par)
882{
883 write3CE(par, CyberControl, read3CE(par, CyberControl) | 0x81);
884}
885
886static void shadowmode_off(struct tridentfb_par *par)
887{
888 write3CE(par, CyberControl, read3CE(par, CyberControl) & 0x7E);
889}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891/* Set the hardware to the requested video mode */
892static int tridentfb_set_par(struct fb_info *info)
893{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700894 struct tridentfb_par *par = (struct tridentfb_par *)(info->par);
895 u32 htotal, hdispend, hsyncstart, hsyncend, hblankstart, hblankend;
896 u32 vtotal, vdispend, vsyncstart, vsyncend, vblankstart, vblankend;
897 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int bpp = var->bits_per_pixel;
899 unsigned char tmp;
Krzysztof Helt3f275ea2008-05-12 14:02:11 -0700900 unsigned long vclk;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 debug("enter\n");
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700903 hdispend = var->xres / 8 - 1;
Krzysztof Helt7f762d22008-07-23 21:30:55 -0700904 hsyncstart = (var->xres + var->right_margin) / 8 - 1;
905 hsyncend = (var->xres + var->right_margin + var->hsync_len) / 8 - 1;
906 htotal = (var->xres + var->left_margin + var->right_margin +
907 var->hsync_len) / 8 - 5;
908 hblankstart = hdispend + 2;
909 hblankend = htotal + 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 vdispend = var->yres - 1;
912 vsyncstart = var->yres + var->lower_margin;
Krzysztof Helt7f762d22008-07-23 21:30:55 -0700913 vsyncend = vsyncstart + var->vsync_len;
914 vtotal = var->upper_margin + vsyncend - 2;
915 vblankstart = vdispend + 2;
916 vblankend = vtotal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700918 crtc_unlock(par);
919 write3CE(par, CyberControl, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700921 if (par->flatpanel && var->xres < nativex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /*
923 * on flat panels with native size larger
924 * than requested resolution decide whether
925 * we stretch or center
926 */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700927 t_outb(par, 0xEB, VGA_MIS_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700929 shadowmode_on(par);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700931 if (center)
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700932 screen_center(par);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 else if (stretch)
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700934 screen_stretch(par);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 } else {
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700937 t_outb(par, 0x2B, VGA_MIS_W);
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700938 write3CE(par, CyberControl, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
941 /* vertical timing values */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700942 write3X4(par, VGA_CRTC_V_TOTAL, vtotal & 0xFF);
943 write3X4(par, VGA_CRTC_V_DISP_END, vdispend & 0xFF);
944 write3X4(par, VGA_CRTC_V_SYNC_START, vsyncstart & 0xFF);
945 write3X4(par, VGA_CRTC_V_SYNC_END, (vsyncend & 0x0F));
946 write3X4(par, VGA_CRTC_V_BLANK_START, vblankstart & 0xFF);
Krzysztof Helt7f762d22008-07-23 21:30:55 -0700947 write3X4(par, VGA_CRTC_V_BLANK_END, vblankend & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* horizontal timing values */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700950 write3X4(par, VGA_CRTC_H_TOTAL, htotal & 0xFF);
951 write3X4(par, VGA_CRTC_H_DISP, hdispend & 0xFF);
952 write3X4(par, VGA_CRTC_H_SYNC_START, hsyncstart & 0xFF);
953 write3X4(par, VGA_CRTC_H_SYNC_END,
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700954 (hsyncend & 0x1F) | ((hblankend & 0x20) << 2));
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700955 write3X4(par, VGA_CRTC_H_BLANK_START, hblankstart & 0xFF);
Krzysztof Helt7f762d22008-07-23 21:30:55 -0700956 write3X4(par, VGA_CRTC_H_BLANK_END, hblankend & 0x1F);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* higher bits of vertical timing values */
959 tmp = 0x10;
960 if (vtotal & 0x100) tmp |= 0x01;
961 if (vdispend & 0x100) tmp |= 0x02;
962 if (vsyncstart & 0x100) tmp |= 0x04;
963 if (vblankstart & 0x100) tmp |= 0x08;
964
965 if (vtotal & 0x200) tmp |= 0x20;
966 if (vdispend & 0x200) tmp |= 0x40;
967 if (vsyncstart & 0x200) tmp |= 0x80;
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700968 write3X4(par, VGA_CRTC_OVERFLOW, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Krzysztof Helt7f762d22008-07-23 21:30:55 -0700970 tmp = read3X4(par, CRTHiOrd) & 0x07;
971 tmp |= 0x08; /* line compare bit 10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (vtotal & 0x400) tmp |= 0x80;
973 if (vblankstart & 0x400) tmp |= 0x40;
974 if (vsyncstart & 0x400) tmp |= 0x20;
975 if (vdispend & 0x400) tmp |= 0x10;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700976 write3X4(par, CRTHiOrd, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Krzysztof Helt7f762d22008-07-23 21:30:55 -0700978 tmp = (htotal >> 8) & 0x01;
979 tmp |= (hdispend >> 7) & 0x02;
980 tmp |= (hsyncstart >> 5) & 0x08;
981 tmp |= (hblankstart >> 4) & 0x10;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700982 write3X4(par, HorizOverflow, tmp);
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 tmp = 0x40;
985 if (vblankstart & 0x200) tmp |= 0x20;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700986//FIXME if (info->var.vmode & FB_VMODE_DOUBLE) tmp |= 0x80; /* double scan for 200 line modes */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700987 write3X4(par, VGA_CRTC_MAX_SCAN, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700989 write3X4(par, VGA_CRTC_LINE_COMPARE, 0xFF);
990 write3X4(par, VGA_CRTC_PRESET_ROW, 0);
991 write3X4(par, VGA_CRTC_MODE, 0xC3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700993 write3X4(par, LinearAddReg, 0x20); /* enable linear addressing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700995 tmp = (info->var.vmode & FB_VMODE_INTERLACED) ? 0x84 : 0x80;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700996 /* enable access extended memory */
997 write3X4(par, CRTCModuleTest, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Krzysztof Helt306fa6f2008-07-23 21:30:50 -0700999 /* enable GE for text acceleration */
1000 write3X4(par, GraphEngReg, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001002#ifdef CONFIG_FB_TRIDENT_ACCEL
Krzysztof Heltd9cad042008-07-23 21:30:54 -07001003 par->init_accel(par, info->var.xres, bpp);
Antonino A. Daplas8dad46c2005-08-01 23:46:44 +08001004#endif
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001007 case 8:
1008 tmp = 0x00;
1009 break;
1010 case 16:
1011 tmp = 0x05;
1012 break;
1013 case 24:
1014 tmp = 0x29;
1015 break;
1016 case 32:
1017 tmp = 0x09;
1018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001021 write3X4(par, PixelBusReg, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 tmp = 0x10;
Krzysztof Helt122e8ad2008-07-23 21:30:52 -07001024 if (iscyber(par->chip_id))
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001025 tmp |= 0x20;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001026 write3X4(par, DRAMControl, tmp); /* both IO, linear enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001028 write3X4(par, InterfaceSel, read3X4(par, InterfaceSel) | 0x40);
1029 write3X4(par, Performance, 0x92);
1030 /* MMIO & PCI read and write burst enable */
1031 write3X4(par, PCIReg, 0x07);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Krzysztof Helt3f275ea2008-05-12 14:02:11 -07001033 /* convert from picoseconds to kHz */
1034 vclk = PICOS2KHZ(info->var.pixclock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (bpp == 32)
Krzysztof Helt3f275ea2008-05-12 14:02:11 -07001036 vclk *= 2;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001037 set_vclk(par, vclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001039 vga_mm_wseq(par->io_virt, 0, 3);
1040 vga_mm_wseq(par->io_virt, 1, 1); /* set char clock 8 dots wide */
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001041 /* enable 4 maps because needed in chain4 mode */
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001042 vga_mm_wseq(par->io_virt, 2, 0x0F);
1043 vga_mm_wseq(par->io_virt, 3, 0);
1044 vga_mm_wseq(par->io_virt, 4, 0x0E); /* memory mode enable bitmaps ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001046 /* divide clock by 2 if 32bpp chain4 mode display and CPU path */
1047 write3CE(par, MiscExtFunc, (bpp == 32) ? 0x1A : 0x12);
1048 write3CE(par, 0x5, 0x40); /* no CGA compat, allow 256 col */
1049 write3CE(par, 0x6, 0x05); /* graphics mode */
1050 write3CE(par, 0x7, 0x0F); /* planes? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Krzysztof Helt122e8ad2008-07-23 21:30:52 -07001052 if (par->chip_id == CYBERBLADEXPAi1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* This fixes snow-effect in 32 bpp */
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001054 write3X4(par, VGA_CRTC_H_SYNC_START, 0x84);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001057 /* graphics mode and support 256 color modes */
1058 writeAttr(par, 0x10, 0x41);
1059 writeAttr(par, 0x12, 0x0F); /* planes */
1060 writeAttr(par, 0x13, 0); /* horizontal pel panning */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001062 /* colors */
1063 for (tmp = 0; tmp < 0x10; tmp++)
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001064 writeAttr(par, tmp, tmp);
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001065 fb_readb(par->io_virt + VGA_IS1_RC); /* flip-flop to index */
1066 t_outb(par, 0x20, VGA_ATT_W); /* enable attr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001069 case 8:
1070 tmp = 0;
1071 break;
1072 case 15:
1073 tmp = 0x10;
1074 break;
1075 case 16:
1076 tmp = 0x30;
1077 break;
1078 case 24:
1079 case 32:
1080 tmp = 0xD0;
1081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001084 t_inb(par, VGA_PEL_IW);
1085 t_inb(par, VGA_PEL_MSK);
1086 t_inb(par, VGA_PEL_MSK);
1087 t_inb(par, VGA_PEL_MSK);
1088 t_inb(par, VGA_PEL_MSK);
1089 t_outb(par, tmp, VGA_PEL_MSK);
1090 t_inb(par, VGA_PEL_IW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001092 if (par->flatpanel)
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001093 set_number_of_lines(par, info->var.yres);
1094 set_lwidth(par, info->var.xres * bpp / (4 * 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 info->fix.visual = (bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001096 info->fix.line_length = info->var.xres * (bpp >> 3);
1097 info->cmap.len = (bpp == 8) ? 256 : 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 debug("exit\n");
1099 return 0;
1100}
1101
1102/* Set one color register */
1103static int tridentfb_setcolreg(unsigned regno, unsigned red, unsigned green,
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001104 unsigned blue, unsigned transp,
1105 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 int bpp = info->var.bits_per_pixel;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001108 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (regno >= info->cmap.len)
1111 return 1;
1112
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001113 if (bpp == 8) {
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001114 t_outb(par, 0xFF, VGA_PEL_MSK);
1115 t_outb(par, regno, VGA_PEL_IW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001117 t_outb(par, red >> 10, VGA_PEL_D);
1118 t_outb(par, green >> 10, VGA_PEL_D);
1119 t_outb(par, blue >> 10, VGA_PEL_D);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001121 } else if (regno < 16) {
1122 if (bpp == 16) { /* RGB 565 */
1123 u32 col;
Antonino A. Daplas8dad46c2005-08-01 23:46:44 +08001124
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001125 col = (red & 0xF800) | ((green & 0xFC00) >> 5) |
1126 ((blue & 0xF800) >> 11);
1127 col |= col << 16;
1128 ((u32 *)(info->pseudo_palette))[regno] = col;
1129 } else if (bpp == 32) /* ARGB 8888 */
1130 ((u32*)info->pseudo_palette)[regno] =
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001131 ((transp & 0xFF00) << 16) |
1132 ((red & 0xFF00) << 8) |
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001133 ((green & 0xFF00)) |
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001134 ((blue & 0xFF00) >> 8);
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001137/* debug("exit\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return 0;
1139}
1140
1141/* Try blanking the screen.For flat panels it does nothing */
1142static int tridentfb_blank(int blank_mode, struct fb_info *info)
1143{
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001144 unsigned char PMCont, DPMSCont;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001145 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 debug("enter\n");
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001148 if (par->flatpanel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return 0;
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001150 t_outb(par, 0x04, 0x83C8); /* Read DPMS Control */
1151 PMCont = t_inb(par, 0x83C6) & 0xFC;
1152 DPMSCont = read3CE(par, PowerStatus) & 0xFC;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001153 switch (blank_mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 case FB_BLANK_UNBLANK:
1155 /* Screen: On, HSync: On, VSync: On */
1156 case FB_BLANK_NORMAL:
1157 /* Screen: Off, HSync: On, VSync: On */
1158 PMCont |= 0x03;
1159 DPMSCont |= 0x00;
1160 break;
1161 case FB_BLANK_HSYNC_SUSPEND:
1162 /* Screen: Off, HSync: Off, VSync: On */
1163 PMCont |= 0x02;
1164 DPMSCont |= 0x01;
1165 break;
1166 case FB_BLANK_VSYNC_SUSPEND:
1167 /* Screen: Off, HSync: On, VSync: Off */
1168 PMCont |= 0x02;
1169 DPMSCont |= 0x02;
1170 break;
1171 case FB_BLANK_POWERDOWN:
1172 /* Screen: Off, HSync: Off, VSync: Off */
1173 PMCont |= 0x00;
1174 DPMSCont |= 0x03;
1175 break;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Krzysztof Helt306fa6f2008-07-23 21:30:50 -07001178 write3CE(par, PowerStatus, DPMSCont);
1179 t_outb(par, 4, 0x83C8);
1180 t_outb(par, PMCont, 0x83C6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 debug("exit\n");
1183
1184 /* let fbcon do a softblank for us */
1185 return (blank_mode == FB_BLANK_NORMAL) ? 1 : 0;
1186}
1187
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001188static struct fb_ops tridentfb_ops = {
1189 .owner = THIS_MODULE,
1190 .fb_setcolreg = tridentfb_setcolreg,
1191 .fb_pan_display = tridentfb_pan_display,
1192 .fb_blank = tridentfb_blank,
1193 .fb_check_var = tridentfb_check_var,
1194 .fb_set_par = tridentfb_set_par,
1195 .fb_fillrect = tridentfb_fillrect,
1196 .fb_copyarea = tridentfb_copyarea,
1197 .fb_imageblit = cfb_imageblit,
1198};
1199
Krzysztof Helte09ed092008-07-23 21:30:51 -07001200static int __devinit trident_pci_probe(struct pci_dev *dev,
1201 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 int err;
1204 unsigned char revision;
Krzysztof Helte09ed092008-07-23 21:30:51 -07001205 struct fb_info *info;
1206 struct tridentfb_par *default_par;
Krzysztof Helt122e8ad2008-07-23 21:30:52 -07001207 int defaultaccel;
1208 int chip3D;
1209 int chip_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 err = pci_enable_device(dev);
1212 if (err)
1213 return err;
1214
Krzysztof Helte09ed092008-07-23 21:30:51 -07001215 info = framebuffer_alloc(sizeof(struct tridentfb_par), &dev->dev);
1216 if (!info)
1217 return -ENOMEM;
1218 default_par = info->par;
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 chip_id = id->device;
1221
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001222 if (chip_id == CYBERBLADEi1)
Knut Petersen9fa68ea2005-09-09 13:04:56 -07001223 output("*** Please do use cyblafb, Cyberblade/i1 support "
1224 "will soon be removed from tridentfb!\n");
1225
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* If PCI id is 0x9660 then further detect chip type */
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (chip_id == TGUI9660) {
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001230 revision = vga_io_rseq(RevisionID);
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 switch (revision) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001233 case 0x22:
1234 case 0x23:
1235 chip_id = CYBER9397;
1236 break;
1237 case 0x2A:
1238 chip_id = CYBER9397DVD;
1239 break;
1240 case 0x30:
1241 case 0x33:
1242 case 0x34:
1243 case 0x35:
1244 case 0x38:
1245 case 0x3A:
1246 case 0xB3:
1247 chip_id = CYBER9385;
1248 break;
1249 case 0x40 ... 0x43:
1250 chip_id = CYBER9382;
1251 break;
1252 case 0x4A:
1253 chip_id = CYBER9388;
1254 break;
1255 default:
1256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 }
1259
1260 chip3D = is3Dchip(chip_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (is_xp(chip_id)) {
Krzysztof Heltd9cad042008-07-23 21:30:54 -07001263 default_par->init_accel = xp_init_accel;
1264 default_par->wait_engine = xp_wait_engine;
1265 default_par->fill_rect = xp_fill_rect;
1266 default_par->copy_rect = xp_copy_rect;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001267 } else if (is_blade(chip_id)) {
Krzysztof Heltd9cad042008-07-23 21:30:54 -07001268 default_par->init_accel = blade_init_accel;
1269 default_par->wait_engine = blade_wait_engine;
1270 default_par->fill_rect = blade_fill_rect;
1271 default_par->copy_rect = blade_copy_rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 } else {
Krzysztof Heltd9cad042008-07-23 21:30:54 -07001273 default_par->init_accel = image_init_accel;
1274 default_par->wait_engine = image_wait_engine;
1275 default_par->fill_rect = image_fill_rect;
1276 default_par->copy_rect = image_copy_rect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Krzysztof Helt122e8ad2008-07-23 21:30:52 -07001279 default_par->chip_id = chip_id;
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /* acceleration is on by default for 3D chips */
1282 defaultaccel = chip3D && !noaccel;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /* setup MMIO region */
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001285 tridentfb_fix.mmio_start = pci_resource_start(dev, 1);
1286 tridentfb_fix.mmio_len = chip3D ? 0x20000 : 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (!request_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len, "tridentfb")) {
1289 debug("request_region failed!\n");
1290 return -1;
1291 }
1292
Krzysztof Helte09ed092008-07-23 21:30:51 -07001293 default_par->io_virt = ioremap_nocache(tridentfb_fix.mmio_start,
1294 tridentfb_fix.mmio_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Krzysztof Helte09ed092008-07-23 21:30:51 -07001296 if (!default_par->io_virt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 debug("ioremap failed\n");
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001298 err = -1;
1299 goto out_unmap1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
1302 enable_mmio();
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /* setup framebuffer memory */
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001305 tridentfb_fix.smem_start = pci_resource_start(dev, 0);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001306 tridentfb_fix.smem_len = get_memsize(default_par);
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (!request_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len, "tridentfb")) {
1309 debug("request_mem_region failed!\n");
Krzysztof Helte09ed092008-07-23 21:30:51 -07001310 disable_mmio(info->par);
Amol Lada02f6402006-12-08 02:40:03 -08001311 err = -1;
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001312 goto out_unmap1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
Krzysztof Helte09ed092008-07-23 21:30:51 -07001315 info->screen_base = ioremap_nocache(tridentfb_fix.smem_start,
1316 tridentfb_fix.smem_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Krzysztof Helte09ed092008-07-23 21:30:51 -07001318 if (!info->screen_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 debug("ioremap failed\n");
Amol Lada02f6402006-12-08 02:40:03 -08001320 err = -1;
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001321 goto out_unmap2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
1324 output("%s board found\n", pci_name(dev));
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001325 default_par->flatpanel = is_flatpanel(default_par);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001327 if (default_par->flatpanel)
Krzysztof Helte09ed092008-07-23 21:30:51 -07001328 nativex = get_nativex(default_par);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Krzysztof Helte09ed092008-07-23 21:30:51 -07001330 info->fix = tridentfb_fix;
1331 info->fbops = &tridentfb_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333
Krzysztof Helte09ed092008-07-23 21:30:51 -07001334 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335#ifdef CONFIG_FB_TRIDENT_ACCEL
Krzysztof Helte09ed092008-07-23 21:30:51 -07001336 info->flags |= FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337#endif
Krzysztof Heltea8ee552008-07-23 21:30:51 -07001338 if (!fb_find_mode(&info->var, info,
Krzysztof Helt07f41e42008-04-28 02:15:06 -07001339 mode_option, NULL, 0, NULL, bpp)) {
Amol Lada02f6402006-12-08 02:40:03 -08001340 err = -EINVAL;
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001341 goto out_unmap2;
Amol Lada02f6402006-12-08 02:40:03 -08001342 }
Krzysztof Helte09ed092008-07-23 21:30:51 -07001343 err = fb_alloc_cmap(&info->cmap, 256, 0);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001344 if (err < 0)
1345 goto out_unmap2;
1346
Krzysztof Heltd9cad042008-07-23 21:30:54 -07001347 if (defaultaccel && default_par->init_accel)
Krzysztof Heltea8ee552008-07-23 21:30:51 -07001348 info->var.accel_flags |= FB_ACCELF_TEXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 else
Krzysztof Heltea8ee552008-07-23 21:30:51 -07001350 info->var.accel_flags &= ~FB_ACCELF_TEXT;
1351 info->var.activate |= FB_ACTIVATE_NOW;
Krzysztof Helte09ed092008-07-23 21:30:51 -07001352 info->device = &dev->dev;
1353 if (register_framebuffer(info) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 printk(KERN_ERR "tridentfb: could not register Trident framebuffer\n");
Krzysztof Helte09ed092008-07-23 21:30:51 -07001355 fb_dealloc_cmap(&info->cmap);
Amol Lada02f6402006-12-08 02:40:03 -08001356 err = -EINVAL;
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001357 goto out_unmap2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 output("fb%d: %s frame buffer device %dx%d-%dbpp\n",
Krzysztof Heltea8ee552008-07-23 21:30:51 -07001360 info->node, info->fix.id, info->var.xres,
1361 info->var.yres, info->var.bits_per_pixel);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001362
1363 pci_set_drvdata(dev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return 0;
Amol Lada02f6402006-12-08 02:40:03 -08001365
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001366out_unmap2:
Krzysztof Helte09ed092008-07-23 21:30:51 -07001367 if (info->screen_base)
1368 iounmap(info->screen_base);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001369 release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001370 disable_mmio(info->par);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001371out_unmap1:
Krzysztof Helte09ed092008-07-23 21:30:51 -07001372 if (default_par->io_virt)
1373 iounmap(default_par->io_virt);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001374 release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001375 framebuffer_release(info);
Amol Lada02f6402006-12-08 02:40:03 -08001376 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001379static void __devexit trident_pci_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Krzysztof Helte09ed092008-07-23 21:30:51 -07001381 struct fb_info *info = pci_get_drvdata(dev);
1382 struct tridentfb_par *par = info->par;
1383
1384 unregister_framebuffer(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 iounmap(par->io_virt);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001386 iounmap(info->screen_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001388 release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001389 pci_set_drvdata(dev, NULL);
1390 framebuffer_release(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393/* List of boards that we are trying to support */
1394static struct pci_device_id trident_devices[] = {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001395 {PCI_VENDOR_ID_TRIDENT, BLADE3D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1396 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1397 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi7D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1398 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1399 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi1D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1400 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEAi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1401 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEAi1D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1402 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEE4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1403 {PCI_VENDOR_ID_TRIDENT, TGUI9660, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1404 {PCI_VENDOR_ID_TRIDENT, IMAGE975, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1405 {PCI_VENDOR_ID_TRIDENT, IMAGE985, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1406 {PCI_VENDOR_ID_TRIDENT, CYBER9320, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1407 {PCI_VENDOR_ID_TRIDENT, CYBER9388, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1408 {PCI_VENDOR_ID_TRIDENT, CYBER9520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1409 {PCI_VENDOR_ID_TRIDENT, CYBER9525DVD, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1410 {PCI_VENDOR_ID_TRIDENT, CYBER9397, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1411 {PCI_VENDOR_ID_TRIDENT, CYBER9397DVD, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1412 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPAi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1413 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPm8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1414 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPm16, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 {0,}
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001416};
1417
1418MODULE_DEVICE_TABLE(pci, trident_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420static struct pci_driver tridentfb_pci_driver = {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001421 .name = "tridentfb",
1422 .id_table = trident_devices,
1423 .probe = trident_pci_probe,
1424 .remove = __devexit_p(trident_pci_remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425};
1426
1427/*
1428 * Parse user specified options (`video=trident:')
1429 * example:
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001430 * video=trident:800x600,bpp=16,noaccel
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
1432#ifndef MODULE
Krzysztof Helt07f41e42008-04-28 02:15:06 -07001433static int __init tridentfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001435 char *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!options || !*options)
1437 return 0;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001438 while ((opt = strsep(&options, ",")) != NULL) {
1439 if (!*opt)
1440 continue;
1441 if (!strncmp(opt, "noaccel", 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 noaccel = 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001443 else if (!strncmp(opt, "fp", 2))
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001444 fp = 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001445 else if (!strncmp(opt, "crt", 3))
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001446 fp = 0;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001447 else if (!strncmp(opt, "bpp=", 4))
1448 bpp = simple_strtoul(opt + 4, NULL, 0);
1449 else if (!strncmp(opt, "center", 6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 center = 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001451 else if (!strncmp(opt, "stretch", 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 stretch = 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001453 else if (!strncmp(opt, "memsize=", 8))
1454 memsize = simple_strtoul(opt + 8, NULL, 0);
1455 else if (!strncmp(opt, "memdiff=", 8))
1456 memdiff = simple_strtoul(opt + 8, NULL, 0);
1457 else if (!strncmp(opt, "nativex=", 8))
1458 nativex = simple_strtoul(opt + 8, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 else
Krzysztof Helt07f41e42008-04-28 02:15:06 -07001460 mode_option = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462 return 0;
1463}
1464#endif
1465
1466static int __init tridentfb_init(void)
1467{
1468#ifndef MODULE
1469 char *option = NULL;
1470
1471 if (fb_get_options("tridentfb", &option))
1472 return -ENODEV;
1473 tridentfb_setup(option);
1474#endif
1475 output("Trident framebuffer %s initializing\n", VERSION);
1476 return pci_register_driver(&tridentfb_pci_driver);
1477}
1478
1479static void __exit tridentfb_exit(void)
1480{
1481 pci_unregister_driver(&tridentfb_pci_driver);
1482}
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484module_init(tridentfb_init);
1485module_exit(tridentfb_exit);
1486
1487MODULE_AUTHOR("Jani Monoses <jani@iv.ro>");
1488MODULE_DESCRIPTION("Framebuffer driver for Trident cards");
1489MODULE_LICENSE("GPL");
1490