• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * XG20, XG21, XG40, XG42 frame buffer device
3  * for Linux kernels  2.5.x, 2.6.x
4  * Base on TW's sis fbdev code.
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/sizes.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 
13 #include "XGI_main.h"
14 #include "vb_init.h"
15 #include "vb_util.h"
16 #include "vb_setmode.h"
17 
18 #define Index_CR_GPIO_Reg1 0x48
19 #define Index_CR_GPIO_Reg3 0x4a
20 
21 #define GPIOG_EN    BIT(6)
22 #define GPIOG_READ  BIT(1)
23 
24 static char *forcecrt2type;
25 static char *mode;
26 static int vesa = -1;
27 static unsigned int refresh_rate;
28 
29 /* -------------------- Macro definitions ---------------------------- */
30 
31 #ifdef DEBUG
dumpVGAReg(struct xgifb_video_info * xgifb_info)32 static void dumpVGAReg(struct xgifb_video_info *xgifb_info)
33 {
34 	u8 i, reg;
35 
36 	xgifb_reg_set(XGISR, 0x05, 0x86);
37 
38 	for (i = 0; i < 0x4f; i++) {
39 		reg = xgifb_reg_get(XGISR, i);
40 		pr_debug("o 3c4 %x\n", i);
41 		pr_debug("i 3c5 => %x\n", reg);
42 	}
43 
44 	for (i = 0; i < 0xF0; i++) {
45 		reg = xgifb_reg_get(XGICR, i);
46 		pr_debug("o 3d4 %x\n", i);
47 		pr_debug("i 3d5 => %x\n", reg);
48 	}
49 }
50 #else
dumpVGAReg(struct xgifb_video_info * xgifb_info)51 static inline void dumpVGAReg(struct xgifb_video_info *xgifb_info)
52 {
53 }
54 #endif
55 
56 /* --------------- Hardware Access Routines -------------------------- */
57 
XGIfb_mode_rate_to_dclock(struct vb_device_info * XGI_Pr,struct xgi_hw_device_info * HwDeviceExtension,unsigned char modeno)58 static int XGIfb_mode_rate_to_dclock(struct vb_device_info *XGI_Pr,
59 				     struct xgi_hw_device_info *HwDeviceExtension,
60 				     unsigned char modeno)
61 {
62 	unsigned short ModeNo = modeno;
63 	unsigned short ModeIdIndex = 0, ClockIndex = 0;
64 	unsigned short RefreshRateTableIndex = 0;
65 
66 	InitTo330Pointer(HwDeviceExtension->jChipType, XGI_Pr);
67 
68 	XGI_SearchModeID(ModeNo, &ModeIdIndex);
69 
70 	RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
71 						   ModeIdIndex, XGI_Pr);
72 
73 	ClockIndex = XGI330_RefIndex[RefreshRateTableIndex].Ext_CRTVCLK;
74 
75 	return XGI_VCLKData[ClockIndex].CLOCK * 1000;
76 }
77 
XGIfb_mode_rate_to_ddata(struct vb_device_info * XGI_Pr,struct xgi_hw_device_info * HwDeviceExtension,unsigned char modeno,u32 * left_margin,u32 * right_margin,u32 * upper_margin,u32 * lower_margin,u32 * hsync_len,u32 * vsync_len,u32 * sync,u32 * vmode)78 static int XGIfb_mode_rate_to_ddata(struct vb_device_info *XGI_Pr,
79 				    struct xgi_hw_device_info *HwDeviceExtension,
80 				    unsigned char modeno, u32 *left_margin,
81 				    u32 *right_margin, u32 *upper_margin,
82 				    u32 *lower_margin, u32 *hsync_len,
83 				    u32 *vsync_len, u32 *sync, u32 *vmode)
84 {
85 	unsigned short ModeNo = modeno;
86 	unsigned short ModeIdIndex, index = 0;
87 	unsigned short RefreshRateTableIndex = 0;
88 
89 	unsigned short VRE, VBE, VRS, VDE;
90 	unsigned short HRE, HBE, HRS, HDE;
91 	unsigned char sr_data, cr_data, cr_data2;
92 	int B, C, D, F, temp, j;
93 
94 	InitTo330Pointer(HwDeviceExtension->jChipType, XGI_Pr);
95 	if (!XGI_SearchModeID(ModeNo, &ModeIdIndex))
96 		return 0;
97 	RefreshRateTableIndex = XGI_GetRatePtrCRT2(HwDeviceExtension, ModeNo,
98 						   ModeIdIndex, XGI_Pr);
99 	index = XGI330_RefIndex[RefreshRateTableIndex].Ext_CRT1CRTC;
100 
101 	sr_data = XGI_CRT1Table[index].CR[5];
102 
103 	HDE = XGI330_RefIndex[RefreshRateTableIndex].XRes >> 3;
104 
105 	cr_data = XGI_CRT1Table[index].CR[3];
106 
107 	/* Horizontal retrace (=sync) start */
108 	HRS = (cr_data & 0xff) | ((unsigned short)(sr_data & 0xC0) << 2);
109 	F = HRS - HDE - 3;
110 
111 	sr_data = XGI_CRT1Table[index].CR[6];
112 
113 	cr_data = XGI_CRT1Table[index].CR[2];
114 
115 	cr_data2 = XGI_CRT1Table[index].CR[4];
116 
117 	/* Horizontal blank end */
118 	HBE = (cr_data & 0x1f) | ((unsigned short)(cr_data2 & 0x80) >> 2)
119 			| ((unsigned short)(sr_data & 0x03) << 6);
120 
121 	/* Horizontal retrace (=sync) end */
122 	HRE = (cr_data2 & 0x1f) | ((sr_data & 0x04) << 3);
123 
124 	temp = HBE - ((HDE - 1) & 255);
125 	B = (temp > 0) ? temp : (temp + 256);
126 
127 	temp = HRE - ((HDE + F + 3) & 63);
128 	C = (temp > 0) ? temp : (temp + 64);
129 
130 	D = B - F - C;
131 
132 	*left_margin = D * 8;
133 	*right_margin = F * 8;
134 	*hsync_len = C * 8;
135 
136 	sr_data = XGI_CRT1Table[index].CR[14];
137 
138 	cr_data2 = XGI_CRT1Table[index].CR[9];
139 
140 	VDE = XGI330_RefIndex[RefreshRateTableIndex].YRes;
141 
142 	cr_data = XGI_CRT1Table[index].CR[10];
143 
144 	/* Vertical retrace (=sync) start */
145 	VRS = (cr_data & 0xff) | ((unsigned short)(cr_data2 & 0x04) << 6)
146 			| ((unsigned short)(cr_data2 & 0x80) << 2)
147 			| ((unsigned short)(sr_data & 0x08) << 7);
148 	F = VRS + 1 - VDE;
149 
150 	cr_data = XGI_CRT1Table[index].CR[13];
151 
152 	/* Vertical blank end */
153 	VBE = (cr_data & 0xff) | ((unsigned short)(sr_data & 0x10) << 4);
154 	temp = VBE - ((VDE - 1) & 511);
155 	B = (temp > 0) ? temp : (temp + 512);
156 
157 	cr_data = XGI_CRT1Table[index].CR[11];
158 
159 	/* Vertical retrace (=sync) end */
160 	VRE = (cr_data & 0x0f) | ((sr_data & 0x20) >> 1);
161 	temp = VRE - ((VDE + F - 1) & 31);
162 	C = (temp > 0) ? temp : (temp + 32);
163 
164 	D = B - F - C;
165 
166 	*upper_margin = D;
167 	*lower_margin = F;
168 	*vsync_len = C;
169 
170 	if (XGI330_RefIndex[RefreshRateTableIndex].Ext_InfoFlag & 0x8000)
171 		*sync &= ~FB_SYNC_VERT_HIGH_ACT;
172 	else
173 		*sync |= FB_SYNC_VERT_HIGH_ACT;
174 
175 	if (XGI330_RefIndex[RefreshRateTableIndex].Ext_InfoFlag & 0x4000)
176 		*sync &= ~FB_SYNC_HOR_HIGH_ACT;
177 	else
178 		*sync |= FB_SYNC_HOR_HIGH_ACT;
179 
180 	*vmode = FB_VMODE_NONINTERLACED;
181 	if (XGI330_RefIndex[RefreshRateTableIndex].Ext_InfoFlag & 0x0080) {
182 		*vmode = FB_VMODE_INTERLACED;
183 	} else {
184 		j = 0;
185 		while (XGI330_EModeIDTable[j].Ext_ModeID != 0xff) {
186 			if (XGI330_EModeIDTable[j].Ext_ModeID ==
187 			    XGI330_RefIndex[RefreshRateTableIndex].ModeID) {
188 				if (XGI330_EModeIDTable[j].Ext_ModeFlag &
189 				    DoubleScanMode) {
190 					*vmode = FB_VMODE_DOUBLE;
191 				}
192 				break;
193 			}
194 			j++;
195 		}
196 	}
197 
198 	return 1;
199 }
200 
XGIRegInit(struct vb_device_info * XGI_Pr,unsigned long BaseAddr)201 void XGIRegInit(struct vb_device_info *XGI_Pr, unsigned long BaseAddr)
202 {
203 	XGI_Pr->P3c4 = BaseAddr + 0x14;
204 	XGI_Pr->P3d4 = BaseAddr + 0x24;
205 	XGI_Pr->P3c0 = BaseAddr + 0x10;
206 	XGI_Pr->P3ce = BaseAddr + 0x1e;
207 	XGI_Pr->P3c2 = BaseAddr + 0x12;
208 	XGI_Pr->P3cc = BaseAddr + 0x1c;
209 	XGI_Pr->P3ca = BaseAddr + 0x1a;
210 	XGI_Pr->P3c6 = BaseAddr + 0x16;
211 	XGI_Pr->P3c7 = BaseAddr + 0x17;
212 	XGI_Pr->P3c8 = BaseAddr + 0x18;
213 	XGI_Pr->P3c9 = BaseAddr + 0x19;
214 	XGI_Pr->P3da = BaseAddr + 0x2A;
215 	XGI_Pr->Part0Port = BaseAddr + XGI_CRT2_PORT_00;
216 	/* Digital video interface registers (LCD) */
217 	XGI_Pr->Part1Port = BaseAddr + SIS_CRT2_PORT_04;
218 	/* 301 TV Encoder registers */
219 	XGI_Pr->Part2Port = BaseAddr + SIS_CRT2_PORT_10;
220 	/* 301 Macrovision registers */
221 	XGI_Pr->Part3Port = BaseAddr + SIS_CRT2_PORT_12;
222 	/* 301 VGA2 (and LCD) registers */
223 	XGI_Pr->Part4Port = BaseAddr + SIS_CRT2_PORT_14;
224 	/* 301 palette address port registers */
225 	XGI_Pr->Part5Port = BaseAddr + SIS_CRT2_PORT_14 + 2;
226 }
227 
228 /* ------------------ Internal helper routines ----------------- */
229 
XGIfb_GetXG21DefaultLVDSModeIdx(struct xgifb_video_info * xgifb_info)230 static int XGIfb_GetXG21DefaultLVDSModeIdx(struct xgifb_video_info *xgifb_info)
231 {
232 	int i = 0;
233 
234 	while ((XGIbios_mode[i].mode_no != 0) &&
235 	       (XGIbios_mode[i].xres <= xgifb_info->lvds_data.LVDSHDE)) {
236 		if ((XGIbios_mode[i].xres == xgifb_info->lvds_data.LVDSHDE) &&
237 		    (XGIbios_mode[i].yres == xgifb_info->lvds_data.LVDSVDE) &&
238 		    (XGIbios_mode[i].bpp == 8)) {
239 			return i;
240 		}
241 		i++;
242 	}
243 
244 	return -1;
245 }
246 
XGIfb_search_mode(struct xgifb_video_info * xgifb_info,const char * name)247 static void XGIfb_search_mode(struct xgifb_video_info *xgifb_info,
248 			      const char *name)
249 {
250 	unsigned int xres;
251 	unsigned int yres;
252 	unsigned int bpp;
253 	int i;
254 
255 	if (sscanf(name, "%ux%ux%u", &xres, &yres, &bpp) != 3)
256 		goto invalid_mode;
257 
258 	if (bpp == 24)
259 		bpp = 32; /* That's for people who mix up color and fb depth. */
260 
261 	for (i = 0; XGIbios_mode[i].mode_no != 0; i++)
262 		if (XGIbios_mode[i].xres == xres &&
263 		    XGIbios_mode[i].yres == yres &&
264 		    XGIbios_mode[i].bpp == bpp) {
265 			xgifb_info->mode_idx = i;
266 			return;
267 		}
268 invalid_mode:
269 	pr_info("Invalid mode '%s'\n", name);
270 }
271 
XGIfb_search_vesamode(struct xgifb_video_info * xgifb_info,unsigned int vesamode)272 static void XGIfb_search_vesamode(struct xgifb_video_info *xgifb_info,
273 				  unsigned int vesamode)
274 {
275 	int i = 0;
276 
277 	if (vesamode == 0)
278 		goto invalid;
279 
280 	vesamode &= 0x1dff; /* Clean VESA mode number from other flags */
281 
282 	while (XGIbios_mode[i].mode_no != 0) {
283 		if ((XGIbios_mode[i].vesa_mode_no_1 == vesamode) ||
284 		    (XGIbios_mode[i].vesa_mode_no_2 == vesamode)) {
285 			xgifb_info->mode_idx = i;
286 			return;
287 		}
288 		i++;
289 	}
290 
291 invalid:
292 	pr_info("Invalid VESA mode 0x%x'\n", vesamode);
293 }
294 
XGIfb_validate_mode(struct xgifb_video_info * xgifb_info,int myindex)295 static int XGIfb_validate_mode(struct xgifb_video_info *xgifb_info, int myindex)
296 {
297 	u16 xres, yres;
298 	struct xgi_hw_device_info *hw_info = &xgifb_info->hw_info;
299 	unsigned long required_mem;
300 
301 	if (xgifb_info->chip == XG21) {
302 		if (xgifb_info->display2 == XGIFB_DISP_LCD) {
303 			xres = xgifb_info->lvds_data.LVDSHDE;
304 			yres = xgifb_info->lvds_data.LVDSVDE;
305 			if (XGIbios_mode[myindex].xres > xres)
306 				return -1;
307 			if (XGIbios_mode[myindex].yres > yres)
308 				return -1;
309 			if ((XGIbios_mode[myindex].xres < xres) &&
310 			    (XGIbios_mode[myindex].yres < yres)) {
311 				if (XGIbios_mode[myindex].bpp > 8)
312 					return -1;
313 			}
314 		}
315 		goto check_memory;
316 	}
317 
318 	/* FIXME: for now, all is valid on XG27 */
319 	if (xgifb_info->chip == XG27)
320 		goto check_memory;
321 
322 	if (!(XGIbios_mode[myindex].chipset & MD_XGI315))
323 		return -1;
324 
325 	switch (xgifb_info->display2) {
326 	case XGIFB_DISP_LCD:
327 		switch (hw_info->ulCRT2LCDType) {
328 		case LCD_640x480:
329 			xres = 640;
330 			yres = 480;
331 			break;
332 		case LCD_800x600:
333 			xres = 800;
334 			yres = 600;
335 			break;
336 		case LCD_1024x600:
337 			xres = 1024;
338 			yres = 600;
339 			break;
340 		case LCD_1024x768:
341 			xres = 1024;
342 			yres = 768;
343 			break;
344 		case LCD_1152x768:
345 			xres = 1152;
346 			yres = 768;
347 			break;
348 		case LCD_1280x960:
349 			xres = 1280;
350 			yres = 960;
351 			break;
352 		case LCD_1280x768:
353 			xres = 1280;
354 			yres = 768;
355 			break;
356 		case LCD_1280x1024:
357 			xres = 1280;
358 			yres = 1024;
359 			break;
360 		case LCD_1400x1050:
361 			xres = 1400;
362 			yres = 1050;
363 			break;
364 		case LCD_1600x1200:
365 			xres = 1600;
366 			yres = 1200;
367 			break;
368 		default:
369 			xres = 0;
370 			yres = 0;
371 			break;
372 		}
373 		if (XGIbios_mode[myindex].xres > xres)
374 			return -1;
375 		if (XGIbios_mode[myindex].yres > yres)
376 			return -1;
377 		if ((hw_info->ulExternalChip == 0x01) || /* LVDS */
378 		    (hw_info->ulExternalChip == 0x05)) { /* LVDS+Chrontel */
379 			switch (XGIbios_mode[myindex].xres) {
380 			case 512:
381 				if (XGIbios_mode[myindex].yres != 512)
382 					return -1;
383 				if (hw_info->ulCRT2LCDType == LCD_1024x600)
384 					return -1;
385 				break;
386 			case 640:
387 				if ((XGIbios_mode[myindex].yres != 400) &&
388 				    (XGIbios_mode[myindex].yres	!= 480))
389 					return -1;
390 				break;
391 			case 800:
392 				if (XGIbios_mode[myindex].yres != 600)
393 					return -1;
394 				break;
395 			case 1024:
396 				if ((XGIbios_mode[myindex].yres != 600) &&
397 				    (XGIbios_mode[myindex].yres != 768))
398 					return -1;
399 				if ((XGIbios_mode[myindex].yres == 600) &&
400 				    (hw_info->ulCRT2LCDType != LCD_1024x600))
401 					return -1;
402 				break;
403 			case 1152:
404 				if ((XGIbios_mode[myindex].yres) != 768)
405 					return -1;
406 				if (hw_info->ulCRT2LCDType != LCD_1152x768)
407 					return -1;
408 				break;
409 			case 1280:
410 				if ((XGIbios_mode[myindex].yres != 768) &&
411 				    (XGIbios_mode[myindex].yres != 1024))
412 					return -1;
413 				if ((XGIbios_mode[myindex].yres == 768) &&
414 				    (hw_info->ulCRT2LCDType != LCD_1280x768))
415 					return -1;
416 				break;
417 			case 1400:
418 				if (XGIbios_mode[myindex].yres != 1050)
419 					return -1;
420 				break;
421 			case 1600:
422 				if (XGIbios_mode[myindex].yres != 1200)
423 					return -1;
424 				break;
425 			default:
426 				return -1;
427 			}
428 		} else {
429 			switch (XGIbios_mode[myindex].xres) {
430 			case 512:
431 				if (XGIbios_mode[myindex].yres != 512)
432 					return -1;
433 				break;
434 			case 640:
435 				if ((XGIbios_mode[myindex].yres != 400) &&
436 				    (XGIbios_mode[myindex].yres != 480))
437 					return -1;
438 				break;
439 			case 800:
440 				if (XGIbios_mode[myindex].yres != 600)
441 					return -1;
442 				break;
443 			case 1024:
444 				if (XGIbios_mode[myindex].yres != 768)
445 					return -1;
446 				break;
447 			case 1280:
448 				if ((XGIbios_mode[myindex].yres != 960) &&
449 				    (XGIbios_mode[myindex].yres != 1024))
450 					return -1;
451 				if (XGIbios_mode[myindex].yres == 960) {
452 					if (hw_info->ulCRT2LCDType ==
453 					    LCD_1400x1050)
454 						return -1;
455 				}
456 				break;
457 			case 1400:
458 				if (XGIbios_mode[myindex].yres != 1050)
459 					return -1;
460 				break;
461 			case 1600:
462 				if (XGIbios_mode[myindex].yres != 1200)
463 					return -1;
464 				break;
465 			default:
466 				return -1;
467 			}
468 		}
469 		break;
470 	case XGIFB_DISP_TV:
471 		switch (XGIbios_mode[myindex].xres) {
472 		case 512:
473 		case 640:
474 		case 800:
475 			break;
476 		case 720:
477 			if (xgifb_info->TV_type == TVMODE_NTSC) {
478 				if (XGIbios_mode[myindex].yres != 480)
479 					return -1;
480 			} else if (xgifb_info->TV_type == TVMODE_PAL) {
481 				if (XGIbios_mode[myindex].yres != 576)
482 					return -1;
483 			}
484 			/* LVDS/CHRONTEL does not support 720 */
485 			if (xgifb_info->hasVB == HASVB_LVDS_CHRONTEL ||
486 			    xgifb_info->hasVB == HASVB_CHRONTEL) {
487 				return -1;
488 			}
489 			break;
490 		case 1024:
491 			if (xgifb_info->TV_type == TVMODE_NTSC) {
492 				if (XGIbios_mode[myindex].bpp == 32)
493 					return -1;
494 			}
495 			break;
496 		default:
497 			return -1;
498 		}
499 		break;
500 	case XGIFB_DISP_CRT:
501 		if (XGIbios_mode[myindex].xres > 1280)
502 			return -1;
503 		break;
504 	case XGIFB_DISP_NONE:
505 		break;
506 	}
507 
508 check_memory:
509 	required_mem = XGIbios_mode[myindex].xres * XGIbios_mode[myindex].yres *
510 		       XGIbios_mode[myindex].bpp / 8;
511 	if (required_mem > xgifb_info->video_size)
512 		return -1;
513 	return myindex;
514 }
515 
XGIfb_search_crt2type(const char * name)516 static void XGIfb_search_crt2type(const char *name)
517 {
518 	int i = 0;
519 
520 	if (!name)
521 		return;
522 
523 	while (XGI_crt2type[i].type_no != -1) {
524 		if (!strcmp(name, XGI_crt2type[i].name)) {
525 			XGIfb_crt2type = XGI_crt2type[i].type_no;
526 			XGIfb_tvplug = XGI_crt2type[i].tvplug_no;
527 			break;
528 		}
529 		i++;
530 	}
531 	if (XGIfb_crt2type < 0)
532 		pr_info("Invalid CRT2 type: %s\n", name);
533 }
534 
XGIfb_search_refresh_rate(struct xgifb_video_info * xgifb_info,unsigned int rate)535 static u8 XGIfb_search_refresh_rate(struct xgifb_video_info *xgifb_info,
536 				    unsigned int rate)
537 {
538 	u16 xres, yres;
539 	int i = 0;
540 
541 	xres = XGIbios_mode[xgifb_info->mode_idx].xres;
542 	yres = XGIbios_mode[xgifb_info->mode_idx].yres;
543 
544 	xgifb_info->rate_idx = 0;
545 	while ((XGIfb_vrate[i].idx != 0) && (XGIfb_vrate[i].xres <= xres)) {
546 		if ((XGIfb_vrate[i].xres == xres) &&
547 		    (XGIfb_vrate[i].yres == yres)) {
548 			if (XGIfb_vrate[i].refresh == rate) {
549 				xgifb_info->rate_idx = XGIfb_vrate[i].idx;
550 				break;
551 			} else if (XGIfb_vrate[i].refresh > rate) {
552 				if ((XGIfb_vrate[i].refresh - rate) <= 3) {
553 					pr_debug("Adjusting rate from %d up to %d\n",
554 						 rate, XGIfb_vrate[i].refresh);
555 					xgifb_info->rate_idx =
556 						XGIfb_vrate[i].idx;
557 					xgifb_info->refresh_rate =
558 						XGIfb_vrate[i].refresh;
559 				} else if (((rate - XGIfb_vrate[i - 1].refresh)
560 						<= 2) && (XGIfb_vrate[i].idx
561 						!= 1)) {
562 					pr_debug("Adjusting rate from %d down to %d\n",
563 						 rate,
564 						 XGIfb_vrate[i - 1].refresh);
565 					xgifb_info->rate_idx =
566 						XGIfb_vrate[i - 1].idx;
567 					xgifb_info->refresh_rate =
568 						XGIfb_vrate[i - 1].refresh;
569 				}
570 				break;
571 			} else if ((rate - XGIfb_vrate[i].refresh) <= 2) {
572 				pr_debug("Adjusting rate from %d down to %d\n",
573 					 rate, XGIfb_vrate[i].refresh);
574 				xgifb_info->rate_idx = XGIfb_vrate[i].idx;
575 				break;
576 			}
577 		}
578 		i++;
579 	}
580 	if (xgifb_info->rate_idx > 0)
581 		return xgifb_info->rate_idx;
582 	pr_info("Unsupported rate %d for %dx%d\n",
583 		rate, xres, yres);
584 	return 0;
585 }
586 
XGIfb_search_tvstd(const char * name)587 static void XGIfb_search_tvstd(const char *name)
588 {
589 	int i = 0;
590 
591 	if (!name)
592 		return;
593 
594 	while (XGI_tvtype[i].type_no != -1) {
595 		if (!strcmp(name, XGI_tvtype[i].name)) {
596 			XGIfb_tvmode = XGI_tvtype[i].type_no;
597 			break;
598 		}
599 		i++;
600 	}
601 }
602 
603 /* ----------- FBDev related routines for all series ----------- */
604 
XGIfb_bpp_to_var(struct xgifb_video_info * xgifb_info,struct fb_var_screeninfo * var)605 static void XGIfb_bpp_to_var(struct xgifb_video_info *xgifb_info,
606 			     struct fb_var_screeninfo *var)
607 {
608 	switch (var->bits_per_pixel) {
609 	case 8:
610 		var->red.offset = 0;
611 		var->green.offset = 0;
612 		var->blue.offset = 0;
613 		var->red.length = 6;
614 		var->green.length = 6;
615 		var->blue.length = 6;
616 		xgifb_info->video_cmap_len = 256;
617 		break;
618 	case 16:
619 		var->red.offset = 11;
620 		var->red.length = 5;
621 		var->green.offset = 5;
622 		var->green.length = 6;
623 		var->blue.offset = 0;
624 		var->blue.length = 5;
625 		var->transp.offset = 0;
626 		var->transp.length = 0;
627 		xgifb_info->video_cmap_len = 16;
628 		break;
629 	case 32:
630 		var->red.offset = 16;
631 		var->red.length = 8;
632 		var->green.offset = 8;
633 		var->green.length = 8;
634 		var->blue.offset = 0;
635 		var->blue.length = 8;
636 		var->transp.offset = 24;
637 		var->transp.length = 8;
638 		xgifb_info->video_cmap_len = 16;
639 		break;
640 	}
641 }
642 
643 /* --------------------- SetMode routines ------------------------- */
644 
XGIfb_pre_setmode(struct xgifb_video_info * xgifb_info)645 static void XGIfb_pre_setmode(struct xgifb_video_info *xgifb_info)
646 {
647 	u8 cr30 = 0, cr31 = 0;
648 
649 	cr31 = xgifb_reg_get(XGICR, 0x31);
650 	cr31 &= ~0x60;
651 
652 	switch (xgifb_info->display2) {
653 	case XGIFB_DISP_CRT:
654 		cr30 = SIS_VB_OUTPUT_CRT2 | SIS_SIMULTANEOUS_VIEW_ENABLE;
655 		cr31 |= SIS_DRIVER_MODE;
656 		break;
657 	case XGIFB_DISP_LCD:
658 		cr30 = SIS_VB_OUTPUT_LCD | SIS_SIMULTANEOUS_VIEW_ENABLE;
659 		cr31 |= SIS_DRIVER_MODE;
660 		break;
661 	case XGIFB_DISP_TV:
662 		if (xgifb_info->TV_type == TVMODE_HIVISION)
663 			cr30 = SIS_VB_OUTPUT_HIVISION
664 					| SIS_SIMULTANEOUS_VIEW_ENABLE;
665 		else if (xgifb_info->TV_plug == TVPLUG_SVIDEO)
666 			cr30 = SIS_VB_OUTPUT_SVIDEO
667 					| SIS_SIMULTANEOUS_VIEW_ENABLE;
668 		else if (xgifb_info->TV_plug == TVPLUG_COMPOSITE)
669 			cr30 = SIS_VB_OUTPUT_COMPOSITE
670 					| SIS_SIMULTANEOUS_VIEW_ENABLE;
671 		else if (xgifb_info->TV_plug == TVPLUG_SCART)
672 			cr30 = SIS_VB_OUTPUT_SCART
673 					| SIS_SIMULTANEOUS_VIEW_ENABLE;
674 		cr31 |= SIS_DRIVER_MODE;
675 
676 		if (XGIfb_tvmode == 1 || xgifb_info->TV_type == TVMODE_PAL)
677 			cr31 |= 0x01;
678 		else
679 			cr31 &= ~0x01;
680 		break;
681 	default: /* disable CRT2 */
682 		cr30 = 0x00;
683 		cr31 |= (SIS_DRIVER_MODE | SIS_VB_OUTPUT_DISABLE);
684 	}
685 
686 	xgifb_reg_set(XGICR, IND_XGI_SCRATCH_REG_CR30, cr30);
687 	xgifb_reg_set(XGICR, IND_XGI_SCRATCH_REG_CR31, cr31);
688 	xgifb_reg_set(XGICR, IND_XGI_SCRATCH_REG_CR33,
689 		      (xgifb_info->rate_idx & 0x0F));
690 }
691 
XGIfb_post_setmode(struct xgifb_video_info * xgifb_info)692 static void XGIfb_post_setmode(struct xgifb_video_info *xgifb_info)
693 {
694 	u8 reg;
695 	unsigned char doit = 1;
696 
697 	if (xgifb_info->video_bpp == 8) {
698 		/*
699 		 * We can't switch off CRT1 on LVDS/Chrontel
700 		 * in 8bpp Modes
701 		 */
702 		if ((xgifb_info->hasVB == HASVB_LVDS) ||
703 		    (xgifb_info->hasVB == HASVB_LVDS_CHRONTEL)) {
704 			doit = 0;
705 		}
706 		/*
707 		 * We can't switch off CRT1 on 301B-DH
708 		 * in 8bpp Modes if using LCD
709 		 */
710 		if (xgifb_info->display2 == XGIFB_DISP_LCD)
711 			doit = 0;
712 	}
713 
714 	/* We can't switch off CRT1 if bridge is in slave mode */
715 	if (xgifb_info->hasVB != HASVB_NONE) {
716 		reg = xgifb_reg_get(XGIPART1, 0x00);
717 
718 		if ((reg & 0x50) == 0x10)
719 			doit = 0;
720 
721 	} else {
722 		XGIfb_crt1off = 0;
723 	}
724 
725 	reg = xgifb_reg_get(XGICR, 0x17);
726 	if ((XGIfb_crt1off) && (doit))
727 		reg &= ~0x80;
728 	else
729 		reg |= 0x80;
730 	xgifb_reg_set(XGICR, 0x17, reg);
731 
732 	xgifb_reg_and(XGISR, IND_SIS_RAMDAC_CONTROL, ~0x04);
733 
734 	if (xgifb_info->display2 == XGIFB_DISP_TV &&
735 	    xgifb_info->hasVB == HASVB_301) {
736 		reg = xgifb_reg_get(XGIPART4, 0x01);
737 
738 		if (reg < 0xB0) { /* Set filter for XGI301 */
739 			int filter_tb;
740 
741 			switch (xgifb_info->video_width) {
742 			case 320:
743 				filter_tb = (xgifb_info->TV_type ==
744 					     TVMODE_NTSC) ? 4 : 12;
745 				break;
746 			case 640:
747 				filter_tb = (xgifb_info->TV_type ==
748 					     TVMODE_NTSC) ? 5 : 13;
749 				break;
750 			case 720:
751 				filter_tb = (xgifb_info->TV_type ==
752 					     TVMODE_NTSC) ? 6 : 14;
753 				break;
754 			case 800:
755 				filter_tb = (xgifb_info->TV_type ==
756 					     TVMODE_NTSC) ? 7 : 15;
757 				break;
758 			default:
759 				filter_tb = 0;
760 				filter = -1;
761 				break;
762 			}
763 			xgifb_reg_or(XGIPART1,
764 				     SIS_CRT2_WENABLE_315,
765 				     0x01);
766 
767 			if (xgifb_info->TV_type == TVMODE_NTSC) {
768 				xgifb_reg_and(XGIPART2, 0x3a, 0x1f);
769 
770 				if (xgifb_info->TV_plug == TVPLUG_SVIDEO) {
771 					xgifb_reg_and(XGIPART2, 0x30, 0xdf);
772 
773 				} else if (xgifb_info->TV_plug
774 						== TVPLUG_COMPOSITE) {
775 					xgifb_reg_or(XGIPART2, 0x30, 0x20);
776 
777 					switch (xgifb_info->video_width) {
778 					case 640:
779 						xgifb_reg_set(XGIPART2,
780 							      0x35,
781 							      0xEB);
782 						xgifb_reg_set(XGIPART2,
783 							      0x36,
784 							      0x04);
785 						xgifb_reg_set(XGIPART2,
786 							      0x37,
787 							      0x25);
788 						xgifb_reg_set(XGIPART2,
789 							      0x38,
790 							      0x18);
791 						break;
792 					case 720:
793 						xgifb_reg_set(XGIPART2,
794 							      0x35,
795 							      0xEE);
796 						xgifb_reg_set(XGIPART2,
797 							      0x36,
798 							      0x0C);
799 						xgifb_reg_set(XGIPART2,
800 							      0x37,
801 							      0x22);
802 						xgifb_reg_set(XGIPART2,
803 							      0x38,
804 							      0x08);
805 						break;
806 					case 800:
807 						xgifb_reg_set(XGIPART2,
808 							      0x35,
809 							      0xEB);
810 						xgifb_reg_set(XGIPART2,
811 							      0x36,
812 							      0x15);
813 						xgifb_reg_set(XGIPART2,
814 							      0x37,
815 							      0x25);
816 						xgifb_reg_set(XGIPART2,
817 							      0x38,
818 							      0xF6);
819 						break;
820 					}
821 				}
822 
823 			} else if (xgifb_info->TV_type == TVMODE_PAL) {
824 				xgifb_reg_and(XGIPART2, 0x3A, 0x1F);
825 
826 				if (xgifb_info->TV_plug == TVPLUG_SVIDEO) {
827 					xgifb_reg_and(XGIPART2, 0x30, 0xDF);
828 
829 				} else if (xgifb_info->TV_plug
830 						== TVPLUG_COMPOSITE) {
831 					xgifb_reg_or(XGIPART2, 0x30, 0x20);
832 
833 					switch (xgifb_info->video_width) {
834 					case 640:
835 						xgifb_reg_set(XGIPART2,
836 							      0x35,
837 							      0xF1);
838 						xgifb_reg_set(XGIPART2,
839 							      0x36,
840 							      0xF7);
841 						xgifb_reg_set(XGIPART2,
842 							      0x37,
843 							      0x1F);
844 						xgifb_reg_set(XGIPART2,
845 							      0x38,
846 							      0x32);
847 						break;
848 					case 720:
849 						xgifb_reg_set(XGIPART2,
850 							      0x35,
851 							      0xF3);
852 						xgifb_reg_set(XGIPART2,
853 							      0x36,
854 							      0x00);
855 						xgifb_reg_set(XGIPART2,
856 							      0x37,
857 							      0x1D);
858 						xgifb_reg_set(XGIPART2,
859 							      0x38,
860 							      0x20);
861 						break;
862 					case 800:
863 						xgifb_reg_set(XGIPART2,
864 							      0x35,
865 							      0xFC);
866 						xgifb_reg_set(XGIPART2,
867 							      0x36,
868 							      0xFB);
869 						xgifb_reg_set(XGIPART2,
870 							      0x37,
871 							      0x14);
872 						xgifb_reg_set(XGIPART2,
873 							      0x38,
874 							      0x2A);
875 						break;
876 					}
877 				}
878 			}
879 
880 			if ((filter >= 0) && (filter <= 7)) {
881 				const u8 *f = XGI_TV_filter[filter_tb].filter[filter];
882 
883 				pr_debug("FilterTable[%d]-%d: %*ph\n",
884 					 filter_tb, filter, 4, f);
885 				xgifb_reg_set(XGIPART2, 0x35, f[0]);
886 				xgifb_reg_set(XGIPART2, 0x36, f[1]);
887 				xgifb_reg_set(XGIPART2, 0x37, f[2]);
888 				xgifb_reg_set(XGIPART2, 0x38, f[3]);
889 			}
890 		}
891 	}
892 }
893 
XGIfb_do_set_var(struct fb_var_screeninfo * var,int isactive,struct fb_info * info)894 static int XGIfb_do_set_var(struct fb_var_screeninfo *var, int isactive,
895 			    struct fb_info *info)
896 {
897 	struct xgifb_video_info *xgifb_info = info->par;
898 	struct xgi_hw_device_info *hw_info = &xgifb_info->hw_info;
899 	unsigned int htotal = var->left_margin + var->xres + var->right_margin
900 			+ var->hsync_len;
901 	unsigned int vtotal = var->upper_margin + var->yres + var->lower_margin
902 			+ var->vsync_len;
903 #if defined(__BIG_ENDIAN)
904 	u8 cr_data;
905 #endif
906 	unsigned int drate = 0, hrate = 0;
907 	int found_mode = 0;
908 	int old_mode;
909 
910 	info->var.xres_virtual = var->xres_virtual;
911 	info->var.yres_virtual = var->yres_virtual;
912 	info->var.bits_per_pixel = var->bits_per_pixel;
913 
914 	if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED)
915 		vtotal <<= 1;
916 	else if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE)
917 		vtotal <<= 2;
918 
919 	if (!htotal || !vtotal) {
920 		pr_debug("Invalid 'var' information\n");
921 		return -EINVAL;
922 	} pr_debug("var->pixclock=%d, htotal=%d, vtotal=%d\n",
923 			var->pixclock, htotal, vtotal);
924 
925 	if (var->pixclock) {
926 		drate = 1000000000 / var->pixclock;
927 		hrate = (drate * 1000) / htotal;
928 		xgifb_info->refresh_rate = (unsigned int)(hrate * 2
929 				/ vtotal);
930 	} else {
931 		xgifb_info->refresh_rate = 60;
932 	}
933 
934 	pr_debug("Change mode to %dx%dx%d-%dHz\n",
935 		 var->xres, var->yres, var->bits_per_pixel,
936 		 xgifb_info->refresh_rate);
937 
938 	old_mode = xgifb_info->mode_idx;
939 	xgifb_info->mode_idx = 0;
940 
941 	while ((XGIbios_mode[xgifb_info->mode_idx].mode_no != 0) &&
942 	       (XGIbios_mode[xgifb_info->mode_idx].xres <= var->xres)) {
943 		if ((XGIbios_mode[xgifb_info->mode_idx].xres == var->xres) &&
944 		    (XGIbios_mode[xgifb_info->mode_idx].yres == var->yres) &&
945 		    (XGIbios_mode[xgifb_info->mode_idx].bpp
946 						== var->bits_per_pixel)) {
947 			found_mode = 1;
948 			break;
949 		}
950 		xgifb_info->mode_idx++;
951 	}
952 
953 	if (found_mode)
954 		xgifb_info->mode_idx = XGIfb_validate_mode(xgifb_info,
955 							xgifb_info->mode_idx);
956 	else
957 		xgifb_info->mode_idx = -1;
958 
959 	if (xgifb_info->mode_idx < 0) {
960 		pr_err("Mode %dx%dx%d not supported\n",
961 		       var->xres, var->yres, var->bits_per_pixel);
962 		xgifb_info->mode_idx = old_mode;
963 		return -EINVAL;
964 	}
965 
966 	if (XGIfb_search_refresh_rate(xgifb_info,
967 				      xgifb_info->refresh_rate) == 0) {
968 		xgifb_info->rate_idx = 1;
969 		xgifb_info->refresh_rate = 60;
970 	}
971 
972 	if (isactive) {
973 		XGIfb_pre_setmode(xgifb_info);
974 		if (XGISetModeNew(xgifb_info, hw_info,
975 				  XGIbios_mode[xgifb_info->mode_idx].mode_no)
976 					== 0) {
977 			pr_err("Setting mode[0x%x] failed\n",
978 			       XGIbios_mode[xgifb_info->mode_idx].mode_no);
979 			return -EINVAL;
980 		}
981 		info->fix.line_length = (info->var.xres_virtual
982 				* info->var.bits_per_pixel) >> 6;
983 
984 		xgifb_reg_set(XGISR, IND_SIS_PASSWORD, SIS_PASSWORD);
985 
986 		xgifb_reg_set(XGICR, 0x13, (info->fix.line_length & 0x00ff));
987 		xgifb_reg_set(XGISR,
988 			      0x0E,
989 			      (info->fix.line_length & 0xff00) >> 8);
990 
991 		XGIfb_post_setmode(xgifb_info);
992 
993 		pr_debug("Set new mode: %dx%dx%d-%d\n",
994 			 XGIbios_mode[xgifb_info->mode_idx].xres,
995 			 XGIbios_mode[xgifb_info->mode_idx].yres,
996 			 XGIbios_mode[xgifb_info->mode_idx].bpp,
997 			 xgifb_info->refresh_rate);
998 
999 		xgifb_info->video_bpp = XGIbios_mode[xgifb_info->mode_idx].bpp;
1000 		xgifb_info->video_vwidth = info->var.xres_virtual;
1001 		xgifb_info->video_width =
1002 			XGIbios_mode[xgifb_info->mode_idx].xres;
1003 		xgifb_info->video_vheight = info->var.yres_virtual;
1004 		xgifb_info->video_height =
1005 			XGIbios_mode[xgifb_info->mode_idx].yres;
1006 		xgifb_info->org_x = 0;
1007 		xgifb_info->org_y = 0;
1008 		xgifb_info->video_linelength = info->var.xres_virtual
1009 				* (xgifb_info->video_bpp >> 3);
1010 		switch (xgifb_info->video_bpp) {
1011 		case 8:
1012 			xgifb_info->DstColor = 0x0000;
1013 			xgifb_info->XGI310_AccelDepth = 0x00000000;
1014 			xgifb_info->video_cmap_len = 256;
1015 #if defined(__BIG_ENDIAN)
1016 			cr_data = xgifb_reg_get(XGICR, 0x4D);
1017 			xgifb_reg_set(XGICR, 0x4D, (cr_data & 0xE0));
1018 #endif
1019 			break;
1020 		case 16:
1021 			xgifb_info->DstColor = 0x8000;
1022 			xgifb_info->XGI310_AccelDepth = 0x00010000;
1023 #if defined(__BIG_ENDIAN)
1024 			cr_data = xgifb_reg_get(XGICR, 0x4D);
1025 			xgifb_reg_set(XGICR, 0x4D, ((cr_data & 0xE0) | 0x0B));
1026 #endif
1027 			xgifb_info->video_cmap_len = 16;
1028 			break;
1029 		case 32:
1030 			xgifb_info->DstColor = 0xC000;
1031 			xgifb_info->XGI310_AccelDepth = 0x00020000;
1032 			xgifb_info->video_cmap_len = 16;
1033 #if defined(__BIG_ENDIAN)
1034 			cr_data = xgifb_reg_get(XGICR, 0x4D);
1035 			xgifb_reg_set(XGICR, 0x4D, ((cr_data & 0xE0) | 0x15));
1036 #endif
1037 			break;
1038 		default:
1039 			xgifb_info->video_cmap_len = 16;
1040 			pr_err("Unsupported depth %d\n",
1041 			       xgifb_info->video_bpp);
1042 			break;
1043 		}
1044 	}
1045 	XGIfb_bpp_to_var(xgifb_info, var); /* update ARGB info */
1046 
1047 	dumpVGAReg(xgifb_info);
1048 	return 0;
1049 }
1050 
XGIfb_pan_var(struct fb_var_screeninfo * var,struct fb_info * info)1051 static int XGIfb_pan_var(struct fb_var_screeninfo *var, struct fb_info *info)
1052 {
1053 	struct xgifb_video_info *xgifb_info = info->par;
1054 	unsigned int base;
1055 
1056 	base = var->yoffset * info->var.xres_virtual + var->xoffset;
1057 
1058 	/* calculate base bpp dep. */
1059 	switch (info->var.bits_per_pixel) {
1060 	case 16:
1061 		base >>= 1;
1062 		break;
1063 	case 32:
1064 		break;
1065 	case 8:
1066 	default:
1067 		base >>= 2;
1068 		break;
1069 	}
1070 
1071 	xgifb_reg_set(XGISR, IND_SIS_PASSWORD, SIS_PASSWORD);
1072 
1073 	xgifb_reg_set(XGICR, 0x0D, base & 0xFF);
1074 	xgifb_reg_set(XGICR, 0x0C, (base >> 8) & 0xFF);
1075 	xgifb_reg_set(XGISR, 0x0D, (base >> 16) & 0xFF);
1076 	xgifb_reg_set(XGISR, 0x37, (base >> 24) & 0x03);
1077 	xgifb_reg_and_or(XGISR, 0x37, 0xDF, (base >> 21) & 0x04);
1078 
1079 	if (xgifb_info->display2 != XGIFB_DISP_NONE) {
1080 		xgifb_reg_or(XGIPART1, SIS_CRT2_WENABLE_315, 0x01);
1081 		xgifb_reg_set(XGIPART1, 0x06, (base & 0xFF));
1082 		xgifb_reg_set(XGIPART1, 0x05, ((base >> 8) & 0xFF));
1083 		xgifb_reg_set(XGIPART1, 0x04, ((base >> 16) & 0xFF));
1084 		xgifb_reg_and_or(XGIPART1,
1085 				 0x02,
1086 				 0x7F,
1087 				 ((base >> 24) & 0x01) << 7);
1088 	}
1089 	return 0;
1090 }
1091 
XGIfb_open(struct fb_info * info,int user)1092 static int XGIfb_open(struct fb_info *info, int user)
1093 {
1094 	return 0;
1095 }
1096 
XGIfb_release(struct fb_info * info,int user)1097 static int XGIfb_release(struct fb_info *info, int user)
1098 {
1099 	return 0;
1100 }
1101 
1102 /* similar to sisfb_get_cmap_len */
XGIfb_get_cmap_len(const struct fb_var_screeninfo * var)1103 static int XGIfb_get_cmap_len(const struct fb_var_screeninfo *var)
1104 {
1105 	return (var->bits_per_pixel == 8) ? 256 : 16;
1106 }
1107 
XGIfb_setcolreg(unsigned int regno,unsigned int red,unsigned int green,unsigned int blue,unsigned int transp,struct fb_info * info)1108 static int XGIfb_setcolreg(unsigned int regno, unsigned int red,
1109 			   unsigned int green, unsigned int blue,
1110 			   unsigned int transp, struct fb_info *info)
1111 {
1112 	struct xgifb_video_info *xgifb_info = info->par;
1113 
1114 	if (regno >= XGIfb_get_cmap_len(&info->var))
1115 		return 1;
1116 
1117 	switch (info->var.bits_per_pixel) {
1118 	case 8:
1119 		outb(regno, XGIDACA);
1120 		outb((red >> 10), XGIDACD);
1121 		outb((green >> 10), XGIDACD);
1122 		outb((blue >> 10), XGIDACD);
1123 		if (xgifb_info->display2 != XGIFB_DISP_NONE) {
1124 			outb(regno, XGIDAC2A);
1125 			outb((red >> 8), XGIDAC2D);
1126 			outb((green >> 8), XGIDAC2D);
1127 			outb((blue >> 8), XGIDAC2D);
1128 		}
1129 		break;
1130 	case 16:
1131 		((u32 *)(info->pseudo_palette))[regno] = ((red & 0xf800))
1132 				| ((green & 0xfc00) >> 5) | ((blue & 0xf800)
1133 				>> 11);
1134 		break;
1135 	case 32:
1136 		red >>= 8;
1137 		green >>= 8;
1138 		blue >>= 8;
1139 		((u32 *)(info->pseudo_palette))[regno] = (red << 16) | (green
1140 				<< 8) | (blue);
1141 		break;
1142 	}
1143 	return 0;
1144 }
1145 
1146 /* ----------- FBDev related routines for all series ---------- */
1147 
XGIfb_get_fix(struct fb_fix_screeninfo * fix,int con,struct fb_info * info)1148 static int XGIfb_get_fix(struct fb_fix_screeninfo *fix, int con,
1149 			 struct fb_info *info)
1150 {
1151 	struct xgifb_video_info *xgifb_info = info->par;
1152 
1153 	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
1154 
1155 	strncpy(fix->id, "XGI", sizeof(fix->id) - 1);
1156 
1157 	/* if register_framebuffer has been called, we must lock */
1158 	if (atomic_read(&info->count))
1159 		mutex_lock(&info->mm_lock);
1160 
1161 	fix->smem_start = xgifb_info->video_base;
1162 	fix->smem_len = xgifb_info->video_size;
1163 
1164 	/* if register_framebuffer has been called, we can unlock */
1165 	if (atomic_read(&info->count))
1166 		mutex_unlock(&info->mm_lock);
1167 
1168 	fix->type = FB_TYPE_PACKED_PIXELS;
1169 	fix->type_aux = 0;
1170 	if (xgifb_info->video_bpp == 8)
1171 		fix->visual = FB_VISUAL_PSEUDOCOLOR;
1172 	else
1173 		fix->visual = FB_VISUAL_DIRECTCOLOR;
1174 	fix->xpanstep = 0;
1175 	if (XGIfb_ypan)
1176 		fix->ypanstep = 1;
1177 	fix->ywrapstep = 0;
1178 	fix->line_length = xgifb_info->video_linelength;
1179 	fix->mmio_start = xgifb_info->mmio_base;
1180 	fix->mmio_len = xgifb_info->mmio_size;
1181 	fix->accel = FB_ACCEL_SIS_XABRE;
1182 
1183 	return 0;
1184 }
1185 
XGIfb_set_par(struct fb_info * info)1186 static int XGIfb_set_par(struct fb_info *info)
1187 {
1188 	int err;
1189 
1190 	err = XGIfb_do_set_var(&info->var, 1, info);
1191 	if (err)
1192 		return err;
1193 	XGIfb_get_fix(&info->fix, -1, info);
1194 	return 0;
1195 }
1196 
XGIfb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)1197 static int XGIfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1198 {
1199 	struct xgifb_video_info *xgifb_info = info->par;
1200 	unsigned int htotal = var->left_margin + var->xres + var->right_margin
1201 			+ var->hsync_len;
1202 	unsigned int vtotal = 0;
1203 	unsigned int drate = 0, hrate = 0;
1204 	int found_mode = 0;
1205 	int search_idx;
1206 
1207 	if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED) {
1208 		vtotal = var->upper_margin + var->yres + var->lower_margin
1209 				+ var->vsync_len;
1210 		vtotal <<= 1;
1211 	} else if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
1212 		vtotal = var->upper_margin + var->yres + var->lower_margin
1213 				+ var->vsync_len;
1214 		vtotal <<= 2;
1215 	} else if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
1216 		vtotal = var->upper_margin + (var->yres / 2)
1217 				+ var->lower_margin + var->vsync_len;
1218 	} else
1219 		vtotal = var->upper_margin + var->yres + var->lower_margin
1220 				+ var->vsync_len;
1221 
1222 	if (!(htotal) || !(vtotal)) {
1223 		pr_debug("No valid timing data\n");
1224 		return -EINVAL;
1225 	}
1226 
1227 	if (var->pixclock && htotal && vtotal) {
1228 		drate = 1000000000 / var->pixclock;
1229 		hrate = (drate * 1000) / htotal;
1230 		xgifb_info->refresh_rate =
1231 			(unsigned int)(hrate * 2 / vtotal);
1232 		pr_debug(
1233 			"%s: pixclock = %d ,htotal=%d, vtotal=%d\n"
1234 			"%s: drate=%d, hrate=%d, refresh_rate=%d\n",
1235 			__func__, var->pixclock, htotal, vtotal,
1236 			__func__, drate, hrate, xgifb_info->refresh_rate);
1237 	} else {
1238 		xgifb_info->refresh_rate = 60;
1239 	}
1240 
1241 	search_idx = 0;
1242 	while ((XGIbios_mode[search_idx].mode_no != 0) &&
1243 	       (XGIbios_mode[search_idx].xres <= var->xres)) {
1244 		if ((XGIbios_mode[search_idx].xres == var->xres) &&
1245 		    (XGIbios_mode[search_idx].yres == var->yres) &&
1246 		    (XGIbios_mode[search_idx].bpp == var->bits_per_pixel)) {
1247 			if (XGIfb_validate_mode(xgifb_info, search_idx) > 0) {
1248 				found_mode = 1;
1249 				break;
1250 			}
1251 		}
1252 		search_idx++;
1253 	}
1254 
1255 	if (!found_mode) {
1256 		pr_err("%dx%dx%d is no valid mode\n",
1257 		       var->xres, var->yres, var->bits_per_pixel);
1258 		search_idx = 0;
1259 		while (XGIbios_mode[search_idx].mode_no != 0) {
1260 			if ((var->xres <= XGIbios_mode[search_idx].xres) &&
1261 			    (var->yres <= XGIbios_mode[search_idx].yres) &&
1262 			    (var->bits_per_pixel ==
1263 			     XGIbios_mode[search_idx].bpp)) {
1264 				if (XGIfb_validate_mode(xgifb_info,
1265 							search_idx) > 0) {
1266 					found_mode = 1;
1267 					break;
1268 				}
1269 			}
1270 			search_idx++;
1271 		}
1272 		if (found_mode) {
1273 			var->xres = XGIbios_mode[search_idx].xres;
1274 			var->yres = XGIbios_mode[search_idx].yres;
1275 			pr_debug("Adapted to mode %dx%dx%d\n",
1276 				 var->xres, var->yres, var->bits_per_pixel);
1277 
1278 		} else {
1279 			pr_err("Failed to find similar mode to %dx%dx%d\n",
1280 			       var->xres, var->yres, var->bits_per_pixel);
1281 			return -EINVAL;
1282 		}
1283 	}
1284 
1285 	/* Adapt RGB settings */
1286 	XGIfb_bpp_to_var(xgifb_info, var);
1287 
1288 	if (!XGIfb_ypan) {
1289 		if (var->xres != var->xres_virtual)
1290 			var->xres_virtual = var->xres;
1291 		if (var->yres != var->yres_virtual)
1292 			var->yres_virtual = var->yres;
1293 	}
1294 
1295 	/* Truncate offsets to maximum if too high */
1296 	if (var->xoffset > var->xres_virtual - var->xres)
1297 		var->xoffset = var->xres_virtual - var->xres - 1;
1298 
1299 	if (var->yoffset > var->yres_virtual - var->yres)
1300 		var->yoffset = var->yres_virtual - var->yres - 1;
1301 
1302 	/* Set everything else to 0 */
1303 	var->red.msb_right = 0;
1304 	var->green.msb_right = 0;
1305 	var->blue.msb_right = 0;
1306 	var->transp.offset = 0;
1307 	var->transp.length = 0;
1308 	var->transp.msb_right = 0;
1309 
1310 	return 0;
1311 }
1312 
XGIfb_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)1313 static int XGIfb_pan_display(struct fb_var_screeninfo *var,
1314 			     struct fb_info *info)
1315 {
1316 	int err;
1317 
1318 	if (var->xoffset > (info->var.xres_virtual - info->var.xres))
1319 		return -EINVAL;
1320 	if (var->yoffset > (info->var.yres_virtual - info->var.yres))
1321 		return -EINVAL;
1322 
1323 	if (var->vmode & FB_VMODE_YWRAP) {
1324 		if (var->yoffset >= info->var.yres_virtual || var->xoffset)
1325 			return -EINVAL;
1326 	} else if (var->xoffset + info->var.xres > info->var.xres_virtual ||
1327 		   var->yoffset + info->var.yres > info->var.yres_virtual) {
1328 		return -EINVAL;
1329 	}
1330 	err = XGIfb_pan_var(var, info);
1331 	if (err < 0)
1332 		return err;
1333 
1334 	info->var.xoffset = var->xoffset;
1335 	info->var.yoffset = var->yoffset;
1336 	if (var->vmode & FB_VMODE_YWRAP)
1337 		info->var.vmode |= FB_VMODE_YWRAP;
1338 	else
1339 		info->var.vmode &= ~FB_VMODE_YWRAP;
1340 
1341 	return 0;
1342 }
1343 
XGIfb_blank(int blank,struct fb_info * info)1344 static int XGIfb_blank(int blank, struct fb_info *info)
1345 {
1346 	struct xgifb_video_info *xgifb_info = info->par;
1347 	u8 reg;
1348 
1349 	reg = xgifb_reg_get(XGICR, 0x17);
1350 
1351 	if (blank > 0)
1352 		reg &= 0x7f;
1353 	else
1354 		reg |= 0x80;
1355 
1356 	xgifb_reg_set(XGICR, 0x17, reg);
1357 	xgifb_reg_set(XGISR, 0x00, 0x01); /* Synchronous Reset */
1358 	xgifb_reg_set(XGISR, 0x00, 0x03); /* End Reset */
1359 	return 0;
1360 }
1361 
1362 static struct fb_ops XGIfb_ops = {
1363 	.owner = THIS_MODULE,
1364 	.fb_open = XGIfb_open,
1365 	.fb_release = XGIfb_release,
1366 	.fb_check_var = XGIfb_check_var,
1367 	.fb_set_par = XGIfb_set_par,
1368 	.fb_setcolreg = XGIfb_setcolreg,
1369 	.fb_pan_display = XGIfb_pan_display,
1370 	.fb_blank = XGIfb_blank,
1371 	.fb_fillrect = cfb_fillrect,
1372 	.fb_copyarea = cfb_copyarea,
1373 	.fb_imageblit = cfb_imageblit,
1374 };
1375 
1376 /* ---------------- Chip generation dependent routines ---------------- */
1377 
1378 /* for XGI 315/550/650/740/330 */
1379 
XGIfb_get_dram_size(struct xgifb_video_info * xgifb_info)1380 static int XGIfb_get_dram_size(struct xgifb_video_info *xgifb_info)
1381 {
1382 	u8 ChannelNum, tmp;
1383 	u8 reg = 0;
1384 
1385 	/* xorg driver sets 32MB * 1 channel */
1386 	if (xgifb_info->chip == XG27)
1387 		xgifb_reg_set(XGISR, IND_SIS_DRAM_SIZE, 0x51);
1388 
1389 	reg = xgifb_reg_get(XGISR, IND_SIS_DRAM_SIZE);
1390 	if (!reg)
1391 		return -1;
1392 
1393 	switch ((reg & XGI_DRAM_SIZE_MASK) >> 4) {
1394 	case XGI_DRAM_SIZE_1MB:
1395 		xgifb_info->video_size = 0x100000;
1396 		break;
1397 	case XGI_DRAM_SIZE_2MB:
1398 		xgifb_info->video_size = 0x200000;
1399 		break;
1400 	case XGI_DRAM_SIZE_4MB:
1401 		xgifb_info->video_size = 0x400000;
1402 		break;
1403 	case XGI_DRAM_SIZE_8MB:
1404 		xgifb_info->video_size = 0x800000;
1405 		break;
1406 	case XGI_DRAM_SIZE_16MB:
1407 		xgifb_info->video_size = 0x1000000;
1408 		break;
1409 	case XGI_DRAM_SIZE_32MB:
1410 		xgifb_info->video_size = 0x2000000;
1411 		break;
1412 	case XGI_DRAM_SIZE_64MB:
1413 		xgifb_info->video_size = 0x4000000;
1414 		break;
1415 	case XGI_DRAM_SIZE_128MB:
1416 		xgifb_info->video_size = 0x8000000;
1417 		break;
1418 	case XGI_DRAM_SIZE_256MB:
1419 		xgifb_info->video_size = 0x10000000;
1420 		break;
1421 	default:
1422 		return -1;
1423 	}
1424 
1425 	tmp = (reg & 0x0c) >> 2;
1426 	switch (xgifb_info->chip) {
1427 	case XG20:
1428 	case XG21:
1429 	case XG27:
1430 		ChannelNum = 1;
1431 		break;
1432 
1433 	case XG42:
1434 		if (reg & 0x04)
1435 			ChannelNum = 2;
1436 		else
1437 			ChannelNum = 1;
1438 		break;
1439 
1440 	case XG40:
1441 	default:
1442 		if (tmp == 2)
1443 			ChannelNum = 2;
1444 		else if (tmp == 3)
1445 			ChannelNum = 3;
1446 		else
1447 			ChannelNum = 1;
1448 		break;
1449 	}
1450 
1451 	xgifb_info->video_size = xgifb_info->video_size * ChannelNum;
1452 
1453 	pr_info("SR14=%x DramSzie %x ChannelNum %x\n",
1454 		reg, xgifb_info->video_size, ChannelNum);
1455 	return 0;
1456 }
1457 
XGIfb_detect_VB(struct xgifb_video_info * xgifb_info)1458 static void XGIfb_detect_VB(struct xgifb_video_info *xgifb_info)
1459 {
1460 	u8 cr32, temp = 0;
1461 
1462 	xgifb_info->TV_plug = 0;
1463 	xgifb_info->TV_type = 0;
1464 
1465 	cr32 = xgifb_reg_get(XGICR, IND_XGI_SCRATCH_REG_CR32);
1466 
1467 	if ((cr32 & SIS_CRT1) && !XGIfb_crt1off) {
1468 		XGIfb_crt1off = 0;
1469 	} else {
1470 		if (cr32 & 0x5F)
1471 			XGIfb_crt1off = 1;
1472 		else
1473 			XGIfb_crt1off = 0;
1474 	}
1475 
1476 	if (!xgifb_info->display2_force) {
1477 		if (cr32 & SIS_VB_TV)
1478 			xgifb_info->display2 = XGIFB_DISP_TV;
1479 		else if (cr32 & SIS_VB_LCD)
1480 			xgifb_info->display2 = XGIFB_DISP_LCD;
1481 		else if (cr32 & SIS_VB_CRT2)
1482 			xgifb_info->display2 = XGIFB_DISP_CRT;
1483 		else
1484 			xgifb_info->display2 = XGIFB_DISP_NONE;
1485 	}
1486 
1487 	if (XGIfb_tvplug != -1) {
1488 		/* Override with option */
1489 		xgifb_info->TV_plug = XGIfb_tvplug;
1490 	} else if (cr32 & SIS_VB_HIVISION) {
1491 		xgifb_info->TV_type = TVMODE_HIVISION;
1492 		xgifb_info->TV_plug = TVPLUG_SVIDEO;
1493 	} else if (cr32 & SIS_VB_SVIDEO) {
1494 		xgifb_info->TV_plug = TVPLUG_SVIDEO;
1495 	} else if (cr32 & SIS_VB_COMPOSITE) {
1496 		xgifb_info->TV_plug = TVPLUG_COMPOSITE;
1497 	} else if (cr32 & SIS_VB_SCART) {
1498 		xgifb_info->TV_plug = TVPLUG_SCART;
1499 	}
1500 
1501 	if (xgifb_info->TV_type == 0) {
1502 		temp = xgifb_reg_get(XGICR, 0x38);
1503 		if (temp & 0x10)
1504 			xgifb_info->TV_type = TVMODE_PAL;
1505 		else
1506 			xgifb_info->TV_type = TVMODE_NTSC;
1507 	}
1508 
1509 	/* Copy forceCRT1 option to CRT1off if option is given */
1510 	if (XGIfb_forcecrt1 != -1) {
1511 		if (XGIfb_forcecrt1)
1512 			XGIfb_crt1off = 0;
1513 		else
1514 			XGIfb_crt1off = 1;
1515 	}
1516 }
1517 
XGIfb_has_VB(struct xgifb_video_info * xgifb_info)1518 static bool XGIfb_has_VB(struct xgifb_video_info *xgifb_info)
1519 {
1520 	u8 vb_chipid;
1521 
1522 	vb_chipid = xgifb_reg_get(XGIPART4, 0x00);
1523 	switch (vb_chipid) {
1524 	case 0x01:
1525 		xgifb_info->hasVB = HASVB_301;
1526 		break;
1527 	case 0x02:
1528 		xgifb_info->hasVB = HASVB_302;
1529 		break;
1530 	default:
1531 		xgifb_info->hasVB = HASVB_NONE;
1532 		return false;
1533 	}
1534 	return true;
1535 }
1536 
XGIfb_get_VB_type(struct xgifb_video_info * xgifb_info)1537 static void XGIfb_get_VB_type(struct xgifb_video_info *xgifb_info)
1538 {
1539 	u8 reg;
1540 
1541 	if (!XGIfb_has_VB(xgifb_info)) {
1542 		reg = xgifb_reg_get(XGICR, IND_XGI_SCRATCH_REG_CR37);
1543 		switch ((reg & SIS_EXTERNAL_CHIP_MASK) >> 1) {
1544 		case SIS_EXTERNAL_CHIP_LVDS:
1545 			xgifb_info->hasVB = HASVB_LVDS;
1546 			break;
1547 		case SIS_EXTERNAL_CHIP_LVDS_CHRONTEL:
1548 			xgifb_info->hasVB = HASVB_LVDS_CHRONTEL;
1549 			break;
1550 		default:
1551 			break;
1552 		}
1553 	}
1554 }
1555 
xgifb_optval(char * fullopt,int validx)1556 static int __init xgifb_optval(char *fullopt, int validx)
1557 {
1558 	unsigned long lres;
1559 
1560 	if (kstrtoul(fullopt + validx, 0, &lres) < 0 || lres > INT_MAX) {
1561 		pr_err("Invalid value for option: %s\n", fullopt);
1562 		return 0;
1563 	}
1564 	return lres;
1565 }
1566 
XGIfb_setup(char * options)1567 static int __init XGIfb_setup(char *options)
1568 {
1569 	char *this_opt;
1570 
1571 	if (!options || !*options)
1572 		return 0;
1573 
1574 	pr_info("Options: %s\n", options);
1575 
1576 	while ((this_opt = strsep(&options, ",")) != NULL) {
1577 		if (!*this_opt)
1578 			continue;
1579 
1580 		if (!strncmp(this_opt, "mode:", 5)) {
1581 			mode = this_opt + 5;
1582 		} else if (!strncmp(this_opt, "vesa:", 5)) {
1583 			vesa = xgifb_optval(this_opt, 5);
1584 		} else if (!strncmp(this_opt, "vrate:", 6)) {
1585 			refresh_rate = xgifb_optval(this_opt, 6);
1586 		} else if (!strncmp(this_opt, "rate:", 5)) {
1587 			refresh_rate = xgifb_optval(this_opt, 5);
1588 		} else if (!strncmp(this_opt, "crt1off", 7)) {
1589 			XGIfb_crt1off = 1;
1590 		} else if (!strncmp(this_opt, "filter:", 7)) {
1591 			filter = xgifb_optval(this_opt, 7);
1592 		} else if (!strncmp(this_opt, "forcecrt2type:", 14)) {
1593 			XGIfb_search_crt2type(this_opt + 14);
1594 		} else if (!strncmp(this_opt, "forcecrt1:", 10)) {
1595 			XGIfb_forcecrt1 = xgifb_optval(this_opt, 10);
1596 		} else if (!strncmp(this_opt, "tvmode:", 7)) {
1597 			XGIfb_search_tvstd(this_opt + 7);
1598 		} else if (!strncmp(this_opt, "tvstandard:", 11)) {
1599 			XGIfb_search_tvstd(this_opt + 7);
1600 		} else if (!strncmp(this_opt, "dstn", 4)) {
1601 			enable_dstn = 1;
1602 			/* DSTN overrules forcecrt2type */
1603 			XGIfb_crt2type = XGIFB_DISP_LCD;
1604 		} else if (!strncmp(this_opt, "noypan", 6)) {
1605 			XGIfb_ypan = 0;
1606 		} else {
1607 			mode = this_opt;
1608 		}
1609 	}
1610 	return 0;
1611 }
1612 
xgifb_probe(struct pci_dev * pdev,const struct pci_device_id * ent)1613 static int xgifb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1614 {
1615 	u8 reg, reg1;
1616 	u8 CR48, CR38;
1617 	int ret;
1618 	struct fb_info *fb_info;
1619 	struct xgifb_video_info *xgifb_info;
1620 	struct xgi_hw_device_info *hw_info;
1621 	unsigned long video_size_max;
1622 
1623 	fb_info = framebuffer_alloc(sizeof(*xgifb_info), &pdev->dev);
1624 	if (!fb_info)
1625 		return -ENOMEM;
1626 
1627 	xgifb_info = fb_info->par;
1628 	hw_info = &xgifb_info->hw_info;
1629 	xgifb_info->fb_info = fb_info;
1630 	xgifb_info->chip_id = pdev->device;
1631 	pci_read_config_byte(pdev,
1632 			     PCI_REVISION_ID,
1633 			     &xgifb_info->revision_id);
1634 	hw_info->jChipRevision = xgifb_info->revision_id;
1635 
1636 	xgifb_info->pcibus = pdev->bus->number;
1637 	xgifb_info->pcislot = PCI_SLOT(pdev->devfn);
1638 	xgifb_info->pcifunc = PCI_FUNC(pdev->devfn);
1639 	xgifb_info->subsysvendor = pdev->subsystem_vendor;
1640 	xgifb_info->subsysdevice = pdev->subsystem_device;
1641 
1642 	video_size_max = pci_resource_len(pdev, 0);
1643 	xgifb_info->video_base = pci_resource_start(pdev, 0);
1644 	xgifb_info->mmio_base = pci_resource_start(pdev, 1);
1645 	xgifb_info->mmio_size = pci_resource_len(pdev, 1);
1646 	xgifb_info->vga_base = pci_resource_start(pdev, 2) + 0x30;
1647 	dev_info(&pdev->dev, "Relocate IO address: %llx [%08lx]\n",
1648 		 (u64)pci_resource_start(pdev, 2),
1649 		 xgifb_info->vga_base);
1650 
1651 	if (pci_enable_device(pdev)) {
1652 		ret = -EIO;
1653 		goto error;
1654 	}
1655 
1656 	if (XGIfb_crt2type != -1) {
1657 		xgifb_info->display2 = XGIfb_crt2type;
1658 		xgifb_info->display2_force = true;
1659 	}
1660 
1661 	XGIRegInit(&xgifb_info->dev_info, xgifb_info->vga_base);
1662 
1663 	xgifb_reg_set(XGISR, IND_SIS_PASSWORD, SIS_PASSWORD);
1664 	reg1 = xgifb_reg_get(XGISR, IND_SIS_PASSWORD);
1665 
1666 	if (reg1 != 0xa1) { /* I/O error */
1667 		dev_err(&pdev->dev, "I/O error\n");
1668 		ret = -EIO;
1669 		goto error_disable;
1670 	}
1671 
1672 	switch (xgifb_info->chip_id) {
1673 	case PCI_DEVICE_ID_XGI_20:
1674 		xgifb_reg_or(XGICR, Index_CR_GPIO_Reg3, GPIOG_EN);
1675 		CR48 = xgifb_reg_get(XGICR, Index_CR_GPIO_Reg1);
1676 		if (CR48 & GPIOG_READ)
1677 			xgifb_info->chip = XG21;
1678 		else
1679 			xgifb_info->chip = XG20;
1680 		break;
1681 	case PCI_DEVICE_ID_XGI_40:
1682 		xgifb_info->chip = XG40;
1683 		break;
1684 	case PCI_DEVICE_ID_XGI_42:
1685 		xgifb_info->chip = XG42;
1686 		break;
1687 	case PCI_DEVICE_ID_XGI_27:
1688 		xgifb_info->chip = XG27;
1689 		break;
1690 	default:
1691 		ret = -ENODEV;
1692 		goto error_disable;
1693 	}
1694 
1695 	dev_info(&pdev->dev, "chipid = %x\n", xgifb_info->chip);
1696 	hw_info->jChipType = xgifb_info->chip;
1697 
1698 	if (XGIfb_get_dram_size(xgifb_info)) {
1699 		xgifb_info->video_size = min_t(unsigned long, video_size_max,
1700 						SZ_16M);
1701 	} else if (xgifb_info->video_size > video_size_max) {
1702 		xgifb_info->video_size = video_size_max;
1703 	}
1704 
1705 	/* Enable PCI_LINEAR_ADDRESSING and MMIO_ENABLE */
1706 	xgifb_reg_or(XGISR,
1707 		     IND_SIS_PCI_ADDRESS_SET,
1708 		     (SIS_PCI_ADDR_ENABLE | SIS_MEM_MAP_IO_ENABLE));
1709 	/* Enable 2D accelerator engine */
1710 	xgifb_reg_or(XGISR, IND_SIS_MODULE_ENABLE, SIS_ENABLE_2D);
1711 
1712 	hw_info->ulVideoMemorySize = xgifb_info->video_size;
1713 
1714 	if (!request_mem_region(xgifb_info->video_base,
1715 				xgifb_info->video_size,
1716 				"XGIfb FB")) {
1717 		dev_err(&pdev->dev, "Unable request memory size %x\n",
1718 			xgifb_info->video_size);
1719 		dev_err(&pdev->dev,
1720 			"Fatal error: Unable to reserve frame buffer memory. Is there another framebuffer driver active?\n");
1721 		ret = -ENODEV;
1722 		goto error_disable;
1723 	}
1724 
1725 	if (!request_mem_region(xgifb_info->mmio_base,
1726 				xgifb_info->mmio_size,
1727 				"XGIfb MMIO")) {
1728 		dev_err(&pdev->dev,
1729 			"Fatal error: Unable to reserve MMIO region\n");
1730 		ret = -ENODEV;
1731 		goto error_0;
1732 	}
1733 
1734 	xgifb_info->video_vbase =
1735 		ioremap_wc(xgifb_info->video_base, xgifb_info->video_size);
1736 	hw_info->pjVideoMemoryAddress =
1737 		ioremap_wc(xgifb_info->video_base, xgifb_info->video_size);
1738 	xgifb_info->mmio_vbase = ioremap(xgifb_info->mmio_base,
1739 					    xgifb_info->mmio_size);
1740 
1741 	dev_info(&pdev->dev,
1742 		 "Framebuffer at 0x%llx, mapped to 0x%p, size %dk\n",
1743 		 (u64)xgifb_info->video_base,
1744 		 xgifb_info->video_vbase,
1745 		 xgifb_info->video_size / 1024);
1746 
1747 	dev_info(&pdev->dev,
1748 		 "MMIO at 0x%llx, mapped to 0x%p, size %ldk\n",
1749 		 (u64)xgifb_info->mmio_base, xgifb_info->mmio_vbase,
1750 		 xgifb_info->mmio_size / 1024);
1751 
1752 	pci_set_drvdata(pdev, xgifb_info);
1753 	if (!XGIInitNew(pdev))
1754 		dev_err(&pdev->dev, "XGIInitNew() failed!\n");
1755 
1756 	xgifb_info->mtrr = -1;
1757 
1758 	xgifb_info->hasVB = HASVB_NONE;
1759 	if ((xgifb_info->chip == XG20) ||
1760 	    (xgifb_info->chip == XG27)) {
1761 		xgifb_info->hasVB = HASVB_NONE;
1762 	} else if (xgifb_info->chip == XG21) {
1763 		CR38 = xgifb_reg_get(XGICR, 0x38);
1764 		if ((CR38 & 0xE0) == 0xC0)
1765 			xgifb_info->display2 = XGIFB_DISP_LCD;
1766 		else if ((CR38 & 0xE0) == 0x60)
1767 			xgifb_info->hasVB = HASVB_CHRONTEL;
1768 		else
1769 			xgifb_info->hasVB = HASVB_NONE;
1770 	} else {
1771 		XGIfb_get_VB_type(xgifb_info);
1772 	}
1773 
1774 	hw_info->ujVBChipID = VB_CHIP_UNKNOWN;
1775 
1776 	hw_info->ulExternalChip = 0;
1777 
1778 	switch (xgifb_info->hasVB) {
1779 	case HASVB_301:
1780 		reg = xgifb_reg_get(XGIPART4, 0x01);
1781 		if (reg >= 0xE0) {
1782 			hw_info->ujVBChipID = VB_CHIP_302LV;
1783 			dev_info(&pdev->dev,
1784 				 "XGI302LV bridge detected (revision 0x%02x)\n",
1785 				 reg);
1786 		} else if (reg >= 0xD0) {
1787 			hw_info->ujVBChipID = VB_CHIP_301LV;
1788 			dev_info(&pdev->dev,
1789 				 "XGI301LV bridge detected (revision 0x%02x)\n",
1790 				 reg);
1791 		} else {
1792 			hw_info->ujVBChipID = VB_CHIP_301;
1793 			dev_info(&pdev->dev, "XGI301 bridge detected\n");
1794 		}
1795 		break;
1796 	case HASVB_302:
1797 		reg = xgifb_reg_get(XGIPART4, 0x01);
1798 		if (reg >= 0xE0) {
1799 			hw_info->ujVBChipID = VB_CHIP_302LV;
1800 			dev_info(&pdev->dev,
1801 				 "XGI302LV bridge detected (revision 0x%02x)\n",
1802 				 reg);
1803 		} else if (reg >= 0xD0) {
1804 			hw_info->ujVBChipID = VB_CHIP_301LV;
1805 			dev_info(&pdev->dev,
1806 				 "XGI302LV bridge detected (revision 0x%02x)\n",
1807 				 reg);
1808 		} else if (reg >= 0xB0) {
1809 			reg1 = xgifb_reg_get(XGIPART4, 0x23);
1810 
1811 			hw_info->ujVBChipID = VB_CHIP_302B;
1812 
1813 		} else {
1814 			hw_info->ujVBChipID = VB_CHIP_302;
1815 			dev_info(&pdev->dev, "XGI302 bridge detected\n");
1816 		}
1817 		break;
1818 	case HASVB_LVDS:
1819 		hw_info->ulExternalChip = 0x1;
1820 		dev_info(&pdev->dev, "LVDS transmitter detected\n");
1821 		break;
1822 	case HASVB_TRUMPION:
1823 		hw_info->ulExternalChip = 0x2;
1824 		dev_info(&pdev->dev, "Trumpion Zurac LVDS scaler detected\n");
1825 		break;
1826 	case HASVB_CHRONTEL:
1827 		hw_info->ulExternalChip = 0x4;
1828 		dev_info(&pdev->dev, "Chrontel TV encoder detected\n");
1829 		break;
1830 	case HASVB_LVDS_CHRONTEL:
1831 		hw_info->ulExternalChip = 0x5;
1832 		dev_info(&pdev->dev,
1833 			 "LVDS transmitter and Chrontel TV encoder detected\n");
1834 		break;
1835 	default:
1836 		dev_info(&pdev->dev, "No or unknown bridge type detected\n");
1837 		break;
1838 	}
1839 
1840 	if (xgifb_info->hasVB != HASVB_NONE)
1841 		XGIfb_detect_VB(xgifb_info);
1842 	else if (xgifb_info->chip != XG21)
1843 		xgifb_info->display2 = XGIFB_DISP_NONE;
1844 
1845 	if (xgifb_info->display2 == XGIFB_DISP_LCD) {
1846 		if (!enable_dstn) {
1847 			reg = xgifb_reg_get(XGICR, IND_XGI_LCD_PANEL);
1848 			reg &= 0x0f;
1849 			hw_info->ulCRT2LCDType = XGI310paneltype[reg];
1850 		}
1851 	}
1852 
1853 	xgifb_info->mode_idx = -1;
1854 
1855 	if (mode)
1856 		XGIfb_search_mode(xgifb_info, mode);
1857 	else if (vesa != -1)
1858 		XGIfb_search_vesamode(xgifb_info, vesa);
1859 
1860 	if (xgifb_info->mode_idx >= 0)
1861 		xgifb_info->mode_idx =
1862 			XGIfb_validate_mode(xgifb_info, xgifb_info->mode_idx);
1863 
1864 	if (xgifb_info->mode_idx < 0) {
1865 		if (xgifb_info->display2 == XGIFB_DISP_LCD &&
1866 		    xgifb_info->chip == XG21)
1867 			xgifb_info->mode_idx =
1868 				XGIfb_GetXG21DefaultLVDSModeIdx(xgifb_info);
1869 		else
1870 			xgifb_info->mode_idx = DEFAULT_MODE;
1871 	}
1872 
1873 	if (xgifb_info->mode_idx < 0) {
1874 		dev_err(&pdev->dev, "No supported video mode found\n");
1875 		ret = -EINVAL;
1876 		goto error_1;
1877 	}
1878 
1879 	/* set default refresh rate */
1880 	xgifb_info->refresh_rate = refresh_rate;
1881 	if (xgifb_info->refresh_rate == 0)
1882 		xgifb_info->refresh_rate = 60;
1883 	if (XGIfb_search_refresh_rate(xgifb_info, xgifb_info->refresh_rate) == 0) {
1884 		xgifb_info->rate_idx = 1;
1885 		xgifb_info->refresh_rate = 60;
1886 	}
1887 
1888 	xgifb_info->video_bpp = XGIbios_mode[xgifb_info->mode_idx].bpp;
1889 	xgifb_info->video_vwidth =
1890 		xgifb_info->video_width =
1891 			XGIbios_mode[xgifb_info->mode_idx].xres;
1892 	xgifb_info->video_vheight =
1893 		xgifb_info->video_height =
1894 			XGIbios_mode[xgifb_info->mode_idx].yres;
1895 	xgifb_info->org_x = 0;
1896 	xgifb_info->org_y = 0;
1897 	xgifb_info->video_linelength =
1898 		xgifb_info->video_width *
1899 		(xgifb_info->video_bpp >> 3);
1900 	switch (xgifb_info->video_bpp) {
1901 	case 8:
1902 		xgifb_info->DstColor = 0x0000;
1903 		xgifb_info->XGI310_AccelDepth = 0x00000000;
1904 		xgifb_info->video_cmap_len = 256;
1905 		break;
1906 	case 16:
1907 		xgifb_info->DstColor = 0x8000;
1908 		xgifb_info->XGI310_AccelDepth = 0x00010000;
1909 		xgifb_info->video_cmap_len = 16;
1910 		break;
1911 	case 32:
1912 		xgifb_info->DstColor = 0xC000;
1913 		xgifb_info->XGI310_AccelDepth = 0x00020000;
1914 		xgifb_info->video_cmap_len = 16;
1915 		break;
1916 	default:
1917 		xgifb_info->video_cmap_len = 16;
1918 		pr_info("Unsupported depth %d\n",
1919 			xgifb_info->video_bpp);
1920 		break;
1921 	}
1922 
1923 	pr_info("Default mode is %dx%dx%d (%dHz)\n",
1924 		xgifb_info->video_width, xgifb_info->video_height,
1925 		xgifb_info->video_bpp, xgifb_info->refresh_rate);
1926 
1927 	fb_info->var.red.length		= 8;
1928 	fb_info->var.green.length	= 8;
1929 	fb_info->var.blue.length	= 8;
1930 	fb_info->var.activate		= FB_ACTIVATE_NOW;
1931 	fb_info->var.height		= -1;
1932 	fb_info->var.width		= -1;
1933 	fb_info->var.vmode		= FB_VMODE_NONINTERLACED;
1934 	fb_info->var.xres		= xgifb_info->video_width;
1935 	fb_info->var.xres_virtual	= xgifb_info->video_width;
1936 	fb_info->var.yres		= xgifb_info->video_height;
1937 	fb_info->var.yres_virtual	= xgifb_info->video_height;
1938 	fb_info->var.bits_per_pixel	= xgifb_info->video_bpp;
1939 
1940 	XGIfb_bpp_to_var(xgifb_info, &fb_info->var);
1941 
1942 	fb_info->var.pixclock = (u32)(1000000000 / XGIfb_mode_rate_to_dclock
1943 				      (&xgifb_info->dev_info, hw_info,
1944 				       XGIbios_mode[xgifb_info->mode_idx].mode_no));
1945 
1946 	if (XGIfb_mode_rate_to_ddata(&xgifb_info->dev_info,
1947 				     hw_info, XGIbios_mode[xgifb_info->mode_idx].mode_no,
1948 				     &fb_info->var.left_margin,
1949 				     &fb_info->var.right_margin,
1950 				     &fb_info->var.upper_margin,
1951 				     &fb_info->var.lower_margin,
1952 				     &fb_info->var.hsync_len,
1953 				     &fb_info->var.vsync_len,
1954 				     &fb_info->var.sync,
1955 				     &fb_info->var.vmode)) {
1956 		if ((fb_info->var.vmode & FB_VMODE_MASK) ==
1957 		    FB_VMODE_INTERLACED) {
1958 			fb_info->var.yres <<= 1;
1959 			fb_info->var.yres_virtual <<= 1;
1960 		} else if ((fb_info->var.vmode & FB_VMODE_MASK) ==
1961 			   FB_VMODE_DOUBLE) {
1962 			fb_info->var.pixclock >>= 1;
1963 			fb_info->var.yres >>= 1;
1964 			fb_info->var.yres_virtual >>= 1;
1965 		}
1966 	}
1967 
1968 	fb_info->flags = FBINFO_FLAG_DEFAULT;
1969 	fb_info->screen_base = xgifb_info->video_vbase;
1970 	fb_info->fbops = &XGIfb_ops;
1971 	XGIfb_get_fix(&fb_info->fix, -1, fb_info);
1972 	fb_info->pseudo_palette = xgifb_info->pseudo_palette;
1973 
1974 	fb_alloc_cmap(&fb_info->cmap, 256, 0);
1975 
1976 	xgifb_info->mtrr = arch_phys_wc_add(xgifb_info->video_base,
1977 					    xgifb_info->video_size);
1978 
1979 	if (register_framebuffer(fb_info) < 0) {
1980 		ret = -EINVAL;
1981 		goto error_mtrr;
1982 	}
1983 
1984 	dumpVGAReg(xgifb_info);
1985 
1986 	return 0;
1987 
1988 error_mtrr:
1989 	arch_phys_wc_del(xgifb_info->mtrr);
1990 error_1:
1991 	iounmap(xgifb_info->mmio_vbase);
1992 	iounmap(xgifb_info->video_vbase);
1993 	release_mem_region(xgifb_info->mmio_base, xgifb_info->mmio_size);
1994 error_0:
1995 	release_mem_region(xgifb_info->video_base, xgifb_info->video_size);
1996 error_disable:
1997 	pci_disable_device(pdev);
1998 error:
1999 	framebuffer_release(fb_info);
2000 	return ret;
2001 }
2002 
2003 /* -------------------- PCI DEVICE HANDLING -------------------- */
2004 
xgifb_remove(struct pci_dev * pdev)2005 static void xgifb_remove(struct pci_dev *pdev)
2006 {
2007 	struct xgifb_video_info *xgifb_info = pci_get_drvdata(pdev);
2008 	struct fb_info *fb_info = xgifb_info->fb_info;
2009 
2010 	unregister_framebuffer(fb_info);
2011 	arch_phys_wc_del(xgifb_info->mtrr);
2012 	iounmap(xgifb_info->mmio_vbase);
2013 	iounmap(xgifb_info->video_vbase);
2014 	release_mem_region(xgifb_info->mmio_base, xgifb_info->mmio_size);
2015 	release_mem_region(xgifb_info->video_base, xgifb_info->video_size);
2016 	pci_disable_device(pdev);
2017 	framebuffer_release(fb_info);
2018 }
2019 
2020 static struct pci_driver xgifb_driver = {
2021 	.name = "xgifb",
2022 	.id_table = xgifb_pci_table,
2023 	.probe = xgifb_probe,
2024 	.remove = xgifb_remove
2025 };
2026 
2027 /* -------------------- MODULE -------------------- */
2028 
2029 module_param(mode, charp, 0000);
2030 MODULE_PARM_DESC(mode,
2031 		 "Selects the desired default display mode in the format XxYxDepth (eg. 1024x768x16).");
2032 
2033 module_param(forcecrt2type, charp, 0000);
2034 MODULE_PARM_DESC(forcecrt2type,
2035 		 "Force the second display output type. Possible values are NONE, LCD, TV, VGA, SVIDEO or COMPOSITE.");
2036 
2037 module_param(vesa, int, 0000);
2038 MODULE_PARM_DESC(vesa,
2039 		 "Selects the desired default display mode by VESA mode number (eg. 0x117).");
2040 
2041 module_param(filter, int, 0000);
2042 MODULE_PARM_DESC(filter,
2043 		 "Selects TV flicker filter type (only for systems with a SiS301 video bridge). Possible values 0-7. Default: [no filter]).");
2044 
xgifb_init(void)2045 static int __init xgifb_init(void)
2046 {
2047 	char *option = NULL;
2048 
2049 	if (forcecrt2type)
2050 		XGIfb_search_crt2type(forcecrt2type);
2051 	if (fb_get_options("xgifb", &option))
2052 		return -ENODEV;
2053 	XGIfb_setup(option);
2054 
2055 	return pci_register_driver(&xgifb_driver);
2056 }
2057 
xgifb_remove_module(void)2058 static void __exit xgifb_remove_module(void)
2059 {
2060 	pci_unregister_driver(&xgifb_driver);
2061 	pr_debug("Module unloaded\n");
2062 }
2063 
2064 MODULE_DESCRIPTION("Z7 Z9 Z9S Z11 framebuffer device driver");
2065 MODULE_LICENSE("GPL");
2066 MODULE_AUTHOR("XGITECH , Others");
2067 module_init(xgifb_init);
2068 module_exit(xgifb_remove_module);
2069