1 /*
2  *  linux/drivers/video/sgivwfb.c -- SGI DBE frame buffer device
3  *
4  *	Copyright (C) 1999 Silicon Graphics, Inc.
5  *      Jeffrey Newquist, newquist@engr.sgi.som
6  *
7  *  This file is subject to the terms and conditions of the GNU General Public
8  *  License. See the file COPYING in the main directory of this archive for
9  *  more details.
10  */
11 
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/mm.h>
18 #include <linux/tty.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <asm/uaccess.h>
24 #include <linux/fb.h>
25 #include <linux/init.h>
26 #include <asm/io.h>
27 #include <asm/mtrr.h>
28 
29 #include <video/fbcon.h>
30 #include <video/fbcon-cfb8.h>
31 #include <video/fbcon-cfb16.h>
32 #include <video/fbcon-cfb32.h>
33 
34 #define INCLUDE_TIMING_TABLE_DATA
35 #define DBE_REG_BASE regs
36 #include "sgivwfb.h"
37 
38 struct sgivwfb_par {
39   struct fb_var_screeninfo var;
40   u_long timing_num;
41   int valid;
42 };
43 
44 /*
45  *  RAM we reserve for the frame buffer. This defines the maximum screen
46  *  size
47  *
48  *  The default can be overridden if the driver is compiled as a module
49  */
50 
51 /* set by arch/i386/kernel/setup.c */
52 u_long                sgivwfb_mem_phys;
53 u_long                sgivwfb_mem_size;
54 
55 static volatile char  *fbmem;
56 static asregs         *regs;
57 static struct fb_info fb_info;
58 static struct { u_char red, green, blue, pad; } palette[256];
59 static char           sgivwfb_name[16] = "SGI Vis WS FB";
60 static u32            cmap_fifo;
61 static int            ypan       = 0;
62 static int            ywrap      = 0;
63 
64 /* console related variables */
65 static int currcon = 0;
66 static struct display disp;
67 
68 static union {
69 #ifdef FBCON_HAS_CFB16
70   u16 cfb16[16];
71 #endif
72 #ifdef FBCON_HAS_CFB32
73   u32 cfb32[16];
74 #endif
75 } fbcon_cmap;
76 
77 static struct sgivwfb_par par_current = {
78   {                             /* var (screeninfo) */
79     /* 640x480, 8 bpp */
80     640, 480, 640, 480, 0, 0, 8, 0,
81     {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
82     0, 0, -1, -1, 0, 20000, 64, 64, 32, 32, 64, 2,
83     0, FB_VMODE_NONINTERLACED
84   },
85   0,                            /* timing_num */
86   0				/* par not activated */
87 };
88 
89 /*
90  *  Interface used by the world
91  */
92 int sgivwfb_setup(char*);
93 
94 static int sgivwfb_get_fix(struct fb_fix_screeninfo *fix, int con,
95 			   struct fb_info *info);
96 static int sgivwfb_get_var(struct fb_var_screeninfo *var, int con,
97 			   struct fb_info *info);
98 static int sgivwfb_set_var(struct fb_var_screeninfo *var, int con,
99 			   struct fb_info *info);
100 static int sgivwfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
101 			    struct fb_info *info);
102 static int sgivwfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
103 			    struct fb_info *info);
104 static int sgivwfb_mmap(struct fb_info *info, struct file *file,
105                         struct vm_area_struct *vma);
106 
107 static struct fb_ops sgivwfb_ops = {
108 	owner:		THIS_MODULE,
109 	fb_get_fix:	sgivwfb_get_fix,
110 	fb_get_var:	sgivwfb_get_var,
111 	fb_set_var:	sgivwfb_set_var,
112 	fb_get_cmap:	sgivwfb_get_cmap,
113 	fb_set_cmap:	sgivwfb_set_cmap,
114 	fb_mmap:	sgivwfb_mmap,
115 };
116 
117 /*
118  *  Interface to the low level console driver
119  */
120 int sgivwfb_init(void);
121 static int sgivwfbcon_switch(int con, struct fb_info *info);
122 static int sgivwfbcon_updatevar(int con, struct fb_info *info);
123 static void sgivwfbcon_blank(int blank, struct fb_info *info);
124 
125 /*
126  *  Internal routines
127  */
128 static u_long get_line_length(int xres_virtual, int bpp);
129 static unsigned long bytes_per_pixel(int bpp);
130 static void activate_par(struct sgivwfb_par *par);
131 static void sgivwfb_encode_fix(struct fb_fix_screeninfo *fix,
132 			       struct fb_var_screeninfo *var);
133 static int sgivwfb_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
134 			     u_int *transp, struct fb_info *info);
135 static int sgivwfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
136 			     u_int transp, struct fb_info *info);
137 static void do_install_cmap(int con, struct fb_info *info);
138 
get_line_length(int xres_virtual,int bpp)139 static unsigned long get_line_length(int xres_virtual, int bpp)
140 {
141   return(xres_virtual * bytes_per_pixel(bpp));
142 }
143 
bytes_per_pixel(int bpp)144 static unsigned long bytes_per_pixel(int bpp)
145 {
146   unsigned long length;
147 
148   switch (bpp) {
149   case 8:
150     length = 1;
151     break;
152   case 16:
153     length = 2;
154     break;
155   case 32:
156     length = 4;
157     break;
158   default:
159     printk(KERN_INFO "sgivwfb: unsupported bpp=%d\n", bpp);
160     length = 0;
161     break;
162   }
163   return(length);
164 }
165 
166 /*
167  * Function:	dbe_TurnOffDma
168  * Parameters:	(None)
169  * Description:	This should turn off the monitor and dbe.  This is used
170  *              when switching between the serial console and the graphics
171  *              console.
172  */
173 
dbe_TurnOffDma(void)174 static void dbe_TurnOffDma(void)
175 {
176   int i;
177   unsigned int readVal;
178 
179   // Check to see if things are already turned off:
180   // 1) Check to see if dbe is not using the internal dotclock.
181   // 2) Check to see if the xy counter in dbe is already off.
182 
183   DBE_GETREG(ctrlstat, readVal);
184   if (GET_DBE_FIELD(CTRLSTAT, PCLKSEL, readVal) < 2)
185     return;
186 
187   DBE_GETREG(vt_xy, readVal);
188   if (GET_DBE_FIELD(VT_XY, VT_FREEZE, readVal) == 1)
189     return;
190 
191   // Otherwise, turn off dbe
192 
193   DBE_GETREG(ovr_control, readVal);
194   SET_DBE_FIELD(OVR_CONTROL, OVR_DMA_ENABLE, readVal, 0);
195   DBE_SETREG(ovr_control, readVal);
196   udelay(1000);
197   DBE_GETREG(frm_control, readVal);
198   SET_DBE_FIELD(FRM_CONTROL, FRM_DMA_ENABLE, readVal, 0);
199   DBE_SETREG(frm_control, readVal);
200   udelay(1000);
201   DBE_GETREG(did_control, readVal);
202   SET_DBE_FIELD(DID_CONTROL, DID_DMA_ENABLE, readVal, 0);
203   DBE_SETREG(did_control, readVal);
204   udelay(1000);
205 
206   // XXX HACK:
207   //
208   //    This was necessary for GBE--we had to wait through two
209   //    vertical retrace periods before the pixel DMA was
210   //    turned off for sure.  I've left this in for now, in
211   //    case dbe needs it.
212 
213   for (i = 0; i < 10000; i++)
214     {
215       DBE_GETREG(frm_inhwctrl, readVal);
216       if (GET_DBE_FIELD(FRM_INHWCTRL, FRM_DMA_ENABLE, readVal) == 0)
217         udelay(10);
218       else
219         {
220           DBE_GETREG(ovr_inhwctrl, readVal);
221           if (GET_DBE_FIELD(OVR_INHWCTRL, OVR_DMA_ENABLE, readVal) == 0)
222             udelay(10);
223           else
224             {
225               DBE_GETREG(did_inhwctrl, readVal);
226               if (GET_DBE_FIELD(DID_INHWCTRL, DID_DMA_ENABLE, readVal) == 0)
227                 udelay(10);
228               else
229                 break;
230             }
231         }
232     }
233 }
234 
235 /*
236  *  Set the hardware according to 'par'.
237  */
activate_par(struct sgivwfb_par * par)238 static void activate_par(struct sgivwfb_par *par)
239 {
240   int i,j, htmp, temp;
241   u32 readVal, outputVal;
242   int wholeTilesX, maxPixelsPerTileX;
243   int frmWrite1, frmWrite2, frmWrite3b;
244   dbe_timing_info_t *currentTiming;    /* Current Video Timing */
245   int xpmax, ypmax;                       // Monitor resolution
246   int bytesPerPixel;                      // Bytes per pixel
247 
248   currentTiming = &dbeVTimings[par->timing_num];
249   bytesPerPixel = bytes_per_pixel(par->var.bits_per_pixel);
250   xpmax = currentTiming->width;
251   ypmax = currentTiming->height;
252 
253   /* dbe_InitGraphicsBase(); */
254   /* Turn on dotclock PLL */
255   DBE_SETREG(ctrlstat, 0x20000000);
256 
257   dbe_TurnOffDma();
258 
259   /* dbe_CalculateScreenParams(); */
260   maxPixelsPerTileX = 512/bytesPerPixel;
261   wholeTilesX = xpmax/maxPixelsPerTileX;
262   if (wholeTilesX*maxPixelsPerTileX < xpmax)
263     wholeTilesX++;
264 
265   printk(KERN_DEBUG "sgivwfb: pixPerTile=%d wholeTilesX=%d\n",
266 	 maxPixelsPerTileX, wholeTilesX);
267 
268   /* dbe_InitGammaMap(); */
269   udelay(10);
270 
271   for (i = 0; i < 256; i++)
272     {
273       DBE_ISETREG(gmap, i, (i << 24) | (i << 16) | (i << 8));
274     }
275 
276   /* dbe_TurnOn(); */
277   DBE_GETREG(vt_xy, readVal);
278   if (GET_DBE_FIELD(VT_XY, VT_FREEZE, readVal) == 1)
279     {
280       DBE_SETREG(vt_xy, 0x00000000);
281       udelay(1);
282     }
283   else
284     dbe_TurnOffDma();
285 
286   /* dbe_Initdbe(); */
287   for (i = 0; i < 256; i++)
288     {
289       for (j = 0; j < 100; j++)
290         {
291           DBE_GETREG(cm_fifo, readVal);
292           if (readVal != 0x00000000)
293             break;
294           else
295             udelay(10);
296         }
297 
298       // DBE_ISETREG(cmap, i, 0x00000000);
299       DBE_ISETREG(cmap, i, (i<<8)|(i<<16)|(i<<24));
300     }
301 
302   /* dbe_InitFramebuffer(); */
303   frmWrite1 = 0;
304   SET_DBE_FIELD(FRM_SIZE_TILE, FRM_WIDTH_TILE, frmWrite1, wholeTilesX);
305   SET_DBE_FIELD(FRM_SIZE_TILE, FRM_RHS, frmWrite1, 0);
306 
307   switch(bytesPerPixel)
308     {
309       case 1:
310         SET_DBE_FIELD(FRM_SIZE_TILE, FRM_DEPTH, frmWrite1, DBE_FRM_DEPTH_8);
311         break;
312       case 2:
313         SET_DBE_FIELD(FRM_SIZE_TILE, FRM_DEPTH, frmWrite1, DBE_FRM_DEPTH_16);
314         break;
315       case 4:
316         SET_DBE_FIELD(FRM_SIZE_TILE, FRM_DEPTH, frmWrite1, DBE_FRM_DEPTH_32);
317         break;
318     }
319 
320   frmWrite2 = 0;
321   SET_DBE_FIELD(FRM_SIZE_PIXEL, FB_HEIGHT_PIX, frmWrite2, ypmax);
322 
323   // Tell dbe about the framebuffer location and type
324   // XXX What format is the FRM_TILE_PTR??  64K aligned address?
325   frmWrite3b = 0;
326   SET_DBE_FIELD(FRM_CONTROL, FRM_TILE_PTR, frmWrite3b, sgivwfb_mem_phys>>9);
327   SET_DBE_FIELD(FRM_CONTROL, FRM_DMA_ENABLE, frmWrite3b, 1);
328   SET_DBE_FIELD(FRM_CONTROL, FRM_LINEAR, frmWrite3b, 1);
329 
330   /* Initialize DIDs */
331 
332   outputVal = 0;
333   switch(bytesPerPixel)
334     {
335       case 1:
336         SET_DBE_FIELD(WID, TYP, outputVal, DBE_CMODE_I8);
337         break;
338       case 2:
339         SET_DBE_FIELD(WID, TYP, outputVal, DBE_CMODE_RGBA5);
340         break;
341       case 4:
342         SET_DBE_FIELD(WID, TYP, outputVal, DBE_CMODE_RGB8);
343         break;
344     }
345   SET_DBE_FIELD(WID, BUF, outputVal, DBE_BMODE_BOTH);
346 
347   for (i = 0; i < 32; i++)
348     {
349       DBE_ISETREG(mode_regs, i, outputVal);
350     }
351 
352   /* dbe_InitTiming(); */
353   DBE_SETREG(vt_intr01, 0xffffffff);
354   DBE_SETREG(vt_intr23, 0xffffffff);
355 
356   DBE_GETREG(dotclock, readVal);
357   DBE_SETREG(dotclock, readVal & 0xffff);
358 
359   DBE_SETREG(vt_xymax, 0x00000000);
360   outputVal = 0;
361   SET_DBE_FIELD(VT_VSYNC, VT_VSYNC_ON, outputVal, currentTiming->vsync_start);
362   SET_DBE_FIELD(VT_VSYNC, VT_VSYNC_OFF, outputVal, currentTiming->vsync_end);
363   DBE_SETREG(vt_vsync, outputVal);
364   outputVal = 0;
365   SET_DBE_FIELD(VT_HSYNC, VT_HSYNC_ON, outputVal, currentTiming->hsync_start);
366   SET_DBE_FIELD(VT_HSYNC, VT_HSYNC_OFF, outputVal, currentTiming->hsync_end);
367   DBE_SETREG(vt_hsync, outputVal);
368   outputVal = 0;
369   SET_DBE_FIELD(VT_VBLANK, VT_VBLANK_ON, outputVal, currentTiming->vblank_start);
370   SET_DBE_FIELD(VT_VBLANK, VT_VBLANK_OFF, outputVal, currentTiming->vblank_end);
371   DBE_SETREG(vt_vblank, outputVal);
372   outputVal = 0;
373   SET_DBE_FIELD(VT_HBLANK, VT_HBLANK_ON, outputVal, currentTiming->hblank_start);
374   SET_DBE_FIELD(VT_HBLANK, VT_HBLANK_OFF, outputVal, currentTiming->hblank_end-3);
375   DBE_SETREG(vt_hblank, outputVal);
376   outputVal = 0;
377   SET_DBE_FIELD(VT_VCMAP, VT_VCMAP_ON, outputVal, currentTiming->vblank_start);
378   SET_DBE_FIELD(VT_VCMAP, VT_VCMAP_OFF, outputVal, currentTiming->vblank_end);
379   DBE_SETREG(vt_vcmap, outputVal);
380   outputVal = 0;
381   SET_DBE_FIELD(VT_HCMAP, VT_HCMAP_ON, outputVal, currentTiming->hblank_start);
382   SET_DBE_FIELD(VT_HCMAP, VT_HCMAP_OFF, outputVal, currentTiming->hblank_end-3);
383   DBE_SETREG(vt_hcmap, outputVal);
384 
385   outputVal = 0;
386   temp = currentTiming->vblank_start - currentTiming->vblank_end - 1;
387   if (temp > 0)
388     temp = -temp;
389 
390   SET_DBE_FIELD(DID_START_XY, DID_STARTY, outputVal, (u32)temp);
391   if (currentTiming->hblank_end >= 20)
392     SET_DBE_FIELD(DID_START_XY, DID_STARTX, outputVal,
393                       currentTiming->hblank_end - 20);
394   else
395     SET_DBE_FIELD(DID_START_XY, DID_STARTX, outputVal,
396                       currentTiming->htotal - (20 - currentTiming->hblank_end));
397   DBE_SETREG(did_start_xy, outputVal);
398 
399   outputVal = 0;
400   SET_DBE_FIELD(CRS_START_XY, CRS_STARTY, outputVal, (u32)(temp+1));
401   if (currentTiming->hblank_end >= DBE_CRS_MAGIC)
402     SET_DBE_FIELD(CRS_START_XY, CRS_STARTX, outputVal,
403                       currentTiming->hblank_end - DBE_CRS_MAGIC);
404   else
405     SET_DBE_FIELD(CRS_START_XY, CRS_STARTX, outputVal,
406                       currentTiming->htotal - (DBE_CRS_MAGIC - currentTiming->hblank_end));
407   DBE_SETREG(crs_start_xy, outputVal);
408 
409   outputVal = 0;
410   SET_DBE_FIELD(VC_START_XY, VC_STARTY, outputVal, (u32)temp);
411   SET_DBE_FIELD(VC_START_XY, VC_STARTX, outputVal,
412                     currentTiming->hblank_end - 4);
413   DBE_SETREG(vc_start_xy, outputVal);
414 
415   DBE_SETREG(frm_size_tile, frmWrite1);
416   DBE_SETREG(frm_size_pixel, frmWrite2);
417 
418   outputVal = 0;
419   SET_DBE_FIELD(DOTCLK, M, outputVal, currentTiming->pll_m-1);
420   SET_DBE_FIELD(DOTCLK, N, outputVal, currentTiming->pll_n-1);
421   SET_DBE_FIELD(DOTCLK, P, outputVal, currentTiming->pll_p);
422   SET_DBE_FIELD(DOTCLK, RUN, outputVal, 1);
423   DBE_SETREG(dotclock, outputVal);
424 
425   udelay(11*1000);
426 
427   DBE_SETREG(vt_vpixen, 0xffffff);
428   DBE_SETREG(vt_hpixen, 0xffffff);
429 
430   outputVal = 0;
431   SET_DBE_FIELD(VT_XYMAX, VT_MAXX, outputVal, currentTiming->htotal);
432   SET_DBE_FIELD(VT_XYMAX, VT_MAXY, outputVal, currentTiming->vtotal);
433   DBE_SETREG(vt_xymax, outputVal);
434 
435   outputVal = frmWrite1;
436   SET_DBE_FIELD(FRM_SIZE_TILE, FRM_FIFO_RESET, outputVal, 1);
437   DBE_SETREG(frm_size_tile, outputVal);
438   DBE_SETREG(frm_size_tile, frmWrite1);
439 
440   outputVal = 0;
441   SET_DBE_FIELD(OVR_WIDTH_TILE, OVR_FIFO_RESET, outputVal, 1);
442   DBE_SETREG(ovr_width_tile, outputVal);
443   DBE_SETREG(ovr_width_tile, 0);
444 
445   DBE_SETREG(frm_control, frmWrite3b);
446   DBE_SETREG(did_control, 0);
447 
448   // Wait for dbe to take frame settings
449   for (i=0; i<100000; i++)
450     {
451       DBE_GETREG(frm_inhwctrl, readVal);
452       if (GET_DBE_FIELD(FRM_INHWCTRL, FRM_DMA_ENABLE, readVal) != 0)
453         break;
454       else
455         udelay(1);
456     }
457 
458   if (i==100000)
459     printk(KERN_INFO "sgivwfb: timeout waiting for frame DMA enable.\n");
460 
461   outputVal = 0;
462   htmp = currentTiming->hblank_end - 19;
463   if (htmp < 0)
464     htmp += currentTiming->htotal;    /* allow blank to wrap around */
465   SET_DBE_FIELD(VT_HPIXEN, VT_HPIXEN_ON, outputVal, htmp);
466   SET_DBE_FIELD(VT_HPIXEN, VT_HPIXEN_OFF, outputVal,
467                     ((htmp + currentTiming->width - 2) % currentTiming->htotal));
468   DBE_SETREG(vt_hpixen, outputVal);
469 
470   outputVal = 0;
471   SET_DBE_FIELD(VT_VPIXEN, VT_VPIXEN_OFF, outputVal,
472                     currentTiming->vblank_start);
473   SET_DBE_FIELD(VT_VPIXEN, VT_VPIXEN_ON, outputVal,
474                     currentTiming->vblank_end);
475   DBE_SETREG(vt_vpixen, outputVal);
476 
477   // Turn off mouse cursor
478   regs->crs_ctl = 0;
479 
480   // XXX What's this section for??
481   DBE_GETREG(ctrlstat, readVal);
482   readVal &= 0x02000000;
483 
484   if (readVal != 0)
485     {
486       DBE_SETREG(ctrlstat, 0x30000000);
487     }
488 }
489 
sgivwfb_encode_fix(struct fb_fix_screeninfo * fix,struct fb_var_screeninfo * var)490 static void sgivwfb_encode_fix(struct fb_fix_screeninfo *fix,
491 			       struct fb_var_screeninfo *var)
492 {
493   memset(fix, 0, sizeof(struct fb_fix_screeninfo));
494   strcpy(fix->id, sgivwfb_name);
495   fix->smem_start = sgivwfb_mem_phys;
496   fix->smem_len = sgivwfb_mem_size;
497   fix->type = FB_TYPE_PACKED_PIXELS;
498   fix->type_aux = 0;
499   switch (var->bits_per_pixel) {
500   case 8:
501     fix->visual = FB_VISUAL_PSEUDOCOLOR;
502     break;
503   default:
504     fix->visual = FB_VISUAL_TRUECOLOR;
505     break;
506   }
507   fix->ywrapstep = ywrap;
508   fix->xpanstep = 0;
509   fix->ypanstep = ypan;
510   fix->line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
511   fix->mmio_start = DBE_REG_PHYS;
512   fix->mmio_len = DBE_REG_SIZE;
513 }
514 
515 /*
516  *  Read a single color register and split it into
517  *  colors/transparent. Return != 0 for invalid regno.
518  */
sgivwfb_getcolreg(u_int regno,u_int * red,u_int * green,u_int * blue,u_int * transp,struct fb_info * info)519 static int sgivwfb_getcolreg(u_int regno, u_int *red, u_int *green, u_int *blue,
520                              u_int *transp, struct fb_info *info)
521 {
522   if (regno > 255)
523     return 1;
524 
525   *red =   palette[regno].red << 8;
526   *green = palette[regno].green << 8;
527   *blue =  palette[regno].blue << 8;
528   *transp = 0;
529   return 0;
530 }
531 
532 /*
533  *  Set a single color register. The values supplied are already
534  *  rounded down to the hardware's capabilities (according to the
535  *  entries in the var structure). Return != 0 for invalid regno.
536  */
537 
sgivwfb_setcolreg(u_int regno,u_int red,u_int green,u_int blue,u_int transp,struct fb_info * info)538 static int sgivwfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
539                              u_int transp, struct fb_info *info)
540 {
541   if (regno > 255)
542     return 1;
543   red >>= 8;
544   green >>= 8;
545   blue >>= 8;
546   palette[regno].red = red;
547   palette[regno].green = green;
548   palette[regno].blue = blue;
549 
550   /* wait for the color map FIFO to have a free entry */
551   while (cmap_fifo == 0)
552     cmap_fifo = regs->cm_fifo;
553 
554   regs->cmap[regno] = (red << 24) | (green << 16) | (blue << 8);
555   cmap_fifo--;			/* assume FIFO is filling up */
556   return 0;
557 }
558 
do_install_cmap(int con,struct fb_info * info)559 static void do_install_cmap(int con, struct fb_info *info)
560 {
561     if (con != currcon)
562 	return;
563     if (fb_display[con].cmap.len)
564 	fb_set_cmap(&fb_display[con].cmap, 1, sgivwfb_setcolreg, info);
565     else
566 	fb_set_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel), 1,
567 		    sgivwfb_setcolreg, info);
568 }
569 
570 /* ---------------------------------------------------- */
571 
572 /*
573  *  Get the Fixed Part of the Display
574  */
sgivwfb_get_fix(struct fb_fix_screeninfo * fix,int con,struct fb_info * info)575 static int sgivwfb_get_fix(struct fb_fix_screeninfo *fix, int con,
576 			   struct fb_info *info)
577 {
578   struct fb_var_screeninfo *var;
579 
580   if (con == -1)
581     var = &par_current.var;
582   else
583     var = &fb_display[con].var;
584   sgivwfb_encode_fix(fix, var);
585   return 0;
586 }
587 
588 /*
589  *  Get the User Defined Part of the Display. If a real par get it form there
590  */
sgivwfb_get_var(struct fb_var_screeninfo * var,int con,struct fb_info * info)591 static int sgivwfb_get_var(struct fb_var_screeninfo *var, int con,
592 			   struct fb_info *info)
593 {
594   if (con == -1)
595     *var = par_current.var;
596   else
597     *var = fb_display[con].var;
598   return 0;
599 }
600 
601 /*
602  *  Set the User Defined Part of the Display. Again if par use it to get
603  *  real video mode.
604  */
sgivwfb_set_var(struct fb_var_screeninfo * var,int con,struct fb_info * info)605 static int sgivwfb_set_var(struct fb_var_screeninfo *var, int con,
606 			   struct fb_info *info)
607 {
608   int err, activate = var->activate;
609   int oldxres, oldyres, oldvxres, oldvyres, oldbpp;
610   u_long line_length;
611   u_long min_mode;
612   int req_dot;
613   int test_mode;
614 
615   struct dbe_timing_info *timing;
616 
617   struct display *display;
618 
619   if (con >= 0)
620     display = &fb_display[con];
621   else
622     display = &disp;	/* used during initialization */
623 
624   /*
625    *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
626    *  as FB_VMODE_SMOOTH_XPAN is only used internally
627    */
628 
629   if (var->vmode & FB_VMODE_CONUPDATE) {
630     var->vmode |= FB_VMODE_YWRAP;
631     var->xoffset = display->var.xoffset;
632     var->yoffset = display->var.yoffset;
633   }
634 
635   /* XXX FIXME - forcing var's */
636   var->xoffset = 0;
637   var->yoffset = 0;
638 
639   /* Limit bpp to 8, 16, and 32 */
640   if (var->bits_per_pixel <= 8)
641     var->bits_per_pixel = 8;
642   else if (var->bits_per_pixel <= 16)
643     var->bits_per_pixel = 16;
644   else if (var->bits_per_pixel <= 32)
645     var->bits_per_pixel = 32;
646   else
647     return -EINVAL;
648 
649   var->grayscale = 0;           /* No grayscale for now */
650 
651   /* determine valid resolution and timing */
652   for (min_mode=0; min_mode<DBE_VT_SIZE; min_mode++) {
653     if (dbeVTimings[min_mode].width >= var->xres &&
654         dbeVTimings[min_mode].height >= var->yres)
655       break;
656   }
657 
658   if (min_mode == DBE_VT_SIZE)
659     return -EINVAL;             /* Resolution to high */
660 
661   /* XXX FIXME - should try to pick best refresh rate */
662   /* for now, pick closest dot-clock within 3MHz*/
663 #error "Floating point not allowed in kernel"
664   req_dot = (int)((1.0e3/1.0e6) / (1.0e-12 * (float)var->pixclock));
665   printk(KERN_INFO "sgivwfb: requested pixclock=%d ps (%d KHz)\n", var->pixclock,
666 	 req_dot);
667   test_mode=min_mode;
668   while (dbeVTimings[min_mode].width == dbeVTimings[test_mode].width) {
669     if (dbeVTimings[test_mode].cfreq+3000 > req_dot)
670       break;
671     test_mode++;
672   }
673   if (dbeVTimings[min_mode].width != dbeVTimings[test_mode].width)
674     test_mode--;
675   min_mode = test_mode;
676   timing = &dbeVTimings[min_mode];
677   printk(KERN_INFO "sgivwfb: granted dot-clock=%d KHz\n", timing->cfreq);
678 
679   /* Adjust virtual resolution, if necessary */
680   if (var->xres > var->xres_virtual || (!ywrap && !ypan))
681     var->xres_virtual = var->xres;
682   if (var->yres > var->yres_virtual || (!ywrap && !ypan))
683     var->yres_virtual = var->yres;
684 
685   /*
686    *  Memory limit
687    */
688   line_length = get_line_length(var->xres_virtual, var->bits_per_pixel);
689   if (line_length*var->yres_virtual > sgivwfb_mem_size)
690     return -ENOMEM;             /* Virtual resolution to high */
691 
692   switch (var->bits_per_pixel)
693     {
694     case 8:
695       var->red.offset = 0;
696       var->red.length = 8;
697       var->green.offset = 0;
698       var->green.length = 8;
699       var->blue.offset = 0;
700       var->blue.length = 8;
701       var->transp.offset = 0;
702       var->transp.length = 0;
703       break;
704     case 16:	/* RGBA 5551 */
705       var->red.offset = 11;
706       var->red.length = 5;
707       var->green.offset = 6;
708       var->green.length = 5;
709       var->blue.offset = 1;
710       var->blue.length = 5;
711       var->transp.offset = 0;
712       var->transp.length = 0;
713       break;
714     case 32:	/* RGB 8888 */
715       var->red.offset = 0;
716       var->red.length = 8;
717       var->green.offset = 8;
718       var->green.length = 8;
719       var->blue.offset = 16;
720       var->blue.length = 8;
721       var->transp.offset = 24;
722       var->transp.length = 8;
723       break;
724     }
725   var->red.msb_right = 0;
726   var->green.msb_right = 0;
727   var->blue.msb_right = 0;
728   var->transp.msb_right = 0;
729 
730   /* set video timing information */
731   var->pixclock = (__u32)(1.0e+9/(float)timing->cfreq);
732   var->left_margin = timing->htotal - timing->hsync_end;
733   var->right_margin = timing->hsync_start - timing->width;
734   var->upper_margin = timing->vtotal - timing->vsync_end;
735   var->lower_margin = timing->vsync_start - timing->height;
736   var->hsync_len = timing->hsync_end - timing->hsync_start;
737   var->vsync_len = timing->vsync_end -  timing->vsync_start;
738 
739   if ((activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
740     oldxres = display->var.xres;
741     oldyres = display->var.yres;
742     oldvxres = display->var.xres_virtual;
743     oldvyres = display->var.yres_virtual;
744     oldbpp = display->var.bits_per_pixel;
745     display->var = *var;
746     par_current.var = *var;
747     par_current.timing_num = min_mode;
748     if (oldxres != var->xres || oldyres != var->yres ||
749 	oldvxres != var->xres_virtual || oldvyres != var->yres_virtual ||
750 	oldbpp != var->bits_per_pixel || !par_current.valid) {
751       struct fb_fix_screeninfo fix;
752       printk(KERN_INFO "sgivwfb: new video mode xres=%d yres=%d bpp=%d\n",
753 	     var->xres, var->yres, var->bits_per_pixel);
754       printk(KERN_INFO "         vxres=%d vyres=%d\n",
755 	     var->xres_virtual, var->yres_virtual);
756       activate_par(&par_current);
757       sgivwfb_encode_fix(&fix, var);
758       display->screen_base = (char *)fbmem;
759       display->visual = fix.visual;
760       display->type = fix.type;
761       display->type_aux = fix.type_aux;
762       display->ypanstep = fix.ypanstep;
763       display->ywrapstep = fix.ywrapstep;
764       display->line_length = fix.line_length;
765       display->can_soft_blank = 1;
766       display->inverse = 0;
767       if (oldbpp != var->bits_per_pixel || !par_current.valid) {
768 	if ((err = fb_alloc_cmap(&display->cmap, 0, 0)))
769 	  return err;
770 	do_install_cmap(con, info);
771       }
772       switch (var->bits_per_pixel) {
773 #ifdef FBCON_HAS_CFB8
774       case 8:
775 	display->dispsw = &fbcon_cfb8;
776 	break;
777 #endif
778 #ifdef FBCON_HAS_CFB16
779       case 16:
780 	display->dispsw = &fbcon_cfb16;
781 	display->dispsw_data = fbcon_cmap.cfb16;
782 	break;
783 #endif
784 #ifdef FBCON_HAS_CFB32
785       case 32:
786 	display->dispsw = &fbcon_cfb32;
787 	display->dispsw_data = fbcon_cmap.cfb32;
788 	break;
789 #endif
790       default:
791 	display->dispsw = &fbcon_dummy;
792 	break;
793       }
794       par_current.valid = 1;
795       if (fb_info.changevar)
796 	(*fb_info.changevar)(con);
797     }
798   }
799   return 0;
800 }
801 
802 /*
803  *  Get the Colormap
804  */
sgivwfb_get_cmap(struct fb_cmap * cmap,int kspc,int con,struct fb_info * info)805 static int sgivwfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
806 			    struct fb_info *info)
807 {
808   if (con == currcon) /* current console? */
809     return fb_get_cmap(cmap, kspc, sgivwfb_getcolreg, info);
810   else if (fb_display[con].cmap.len) /* non default colormap? */
811     fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0 : 2);
812   else
813     fb_copy_cmap(fb_default_cmap(1<<fb_display[con].var.bits_per_pixel),
814 		 cmap, kspc ? 0 : 2);
815   return 0;
816 }
817 
818 /*
819  *  Set the Colormap
820  */
sgivwfb_set_cmap(struct fb_cmap * cmap,int kspc,int con,struct fb_info * info)821 static int sgivwfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
822 			    struct fb_info *info)
823 {
824   int err;
825 
826   if (!fb_display[con].cmap.len) {	/* no colormap allocated? */
827     int size = fb_display[con].var.bits_per_pixel == 16 ? 32 : 256;
828     if ((err = fb_alloc_cmap(&fb_display[con].cmap, size, 0)))
829       return err;
830   }
831   if (con == currcon)			/* current console? */
832     return fb_set_cmap(cmap, kspc, sgivwfb_setcolreg, info);
833   else
834     fb_copy_cmap(cmap, &fb_display[con].cmap, kspc ? 0 : 1);
835   return 0;
836 }
837 
sgivwfb_mmap(struct fb_info * info,struct file * file,struct vm_area_struct * vma)838 static int sgivwfb_mmap(struct fb_info *info, struct file *file,
839                         struct vm_area_struct *vma)
840 {
841   unsigned long size = vma->vm_end - vma->vm_start;
842   unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
843   if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
844 	return -EINVAL;
845   if (offset+size > sgivwfb_mem_size)
846     return -EINVAL;
847   offset += sgivwfb_mem_phys;
848   pgprot_val(vma->vm_page_prot) = pgprot_val(vma->vm_page_prot) | _PAGE_PCD;
849   if (remap_page_range(vma->vm_start, offset, size, vma->vm_page_prot))
850     return -EAGAIN;
851   vma->vm_file = file;
852   printk(KERN_DEBUG "sgivwfb: mmap framebuffer P(%lx)->V(%lx)\n", offset, vma->vm_start);
853   return 0;
854 }
855 
sgivwfb_setup(char * options)856 int __init sgivwfb_setup(char *options)
857 {
858   char *this_opt;
859 
860   fb_info.fontname[0] = '\0';
861 
862   if (!options || !*options)
863     return 0;
864 
865   while ((this_opt = strsep(&options, ",")) != NULL) {
866     if (!strncmp(this_opt, "font:", 5))
867       strcpy(fb_info.fontname, this_opt+5);
868   }
869   return 0;
870 }
871 
872 /*
873  *  Initialisation
874  */
sgivwfb_init(void)875 int __init sgivwfb_init(void)
876 {
877   printk(KERN_INFO "sgivwfb: framebuffer at 0x%lx, size %ldk\n",
878 	 sgivwfb_mem_phys, sgivwfb_mem_size/1024);
879 
880   regs = (asregs*)ioremap_nocache(DBE_REG_PHYS, DBE_REG_SIZE);
881   if (!regs) {
882     printk(KERN_ERR "sgivwfb: couldn't ioremap registers\n");
883     goto fail_ioremap_regs;
884   }
885 
886 #ifdef CONFIG_MTRR
887   mtrr_add((unsigned long)sgivwfb_mem_phys, sgivwfb_mem_size, MTRR_TYPE_WRCOMB, 1);
888 #endif
889 
890   strcpy(fb_info.modename, sgivwfb_name);
891   fb_info.changevar = NULL;
892   fb_info.node = -1;
893   fb_info.fbops = &sgivwfb_ops;
894   fb_info.disp = &disp;
895   fb_info.switch_con = &sgivwfbcon_switch;
896   fb_info.updatevar = &sgivwfbcon_updatevar;
897   fb_info.blank = &sgivwfbcon_blank;
898   fb_info.flags = FBINFO_FLAG_DEFAULT;
899 
900   fbmem = ioremap_nocache((unsigned long)sgivwfb_mem_phys, sgivwfb_mem_size);
901   if (!fbmem) {
902     printk(KERN_ERR "sgivwfb: couldn't ioremap fbmem\n");
903     goto fail_ioremap_fbmem;
904   }
905 
906   /* turn on default video mode */
907   sgivwfb_set_var(&par_current.var, -1, &fb_info);
908 
909   if (register_framebuffer(&fb_info) < 0) {
910     printk(KERN_ERR "sgivwfb: couldn't register framebuffer\n");
911     goto fail_register_framebuffer;
912   }
913 
914   printk(KERN_INFO "fb%d: Virtual frame buffer device, using %ldK of video memory\n",
915 	 GET_FB_IDX(fb_info.node), sgivwfb_mem_size>>10);
916 
917   return 0;
918 
919  fail_register_framebuffer:
920   iounmap((char*)fbmem);
921  fail_ioremap_fbmem:
922   iounmap(regs);
923  fail_ioremap_regs:
924   return -ENXIO;
925 }
926 
sgivwfbcon_switch(int con,struct fb_info * info)927 static int sgivwfbcon_switch(int con, struct fb_info *info)
928 {
929   /* Do we have to save the colormap? */
930   if (fb_display[currcon].cmap.len)
931     fb_get_cmap(&fb_display[currcon].cmap, 1, sgivwfb_getcolreg, info);
932 
933   currcon = con;
934   /* Install new colormap */
935   do_install_cmap(con, info);
936   return 0;
937 }
938 
939 /*
940  *  Update the `var' structure (called by fbcon.c)
941  */
sgivwfbcon_updatevar(int con,struct fb_info * info)942 static int sgivwfbcon_updatevar(int con, struct fb_info *info)
943 {
944     /* Nothing */
945     return 0;
946 }
947 
948 /*
949  *  Blank the display.
950  */
sgivwfbcon_blank(int blank,struct fb_info * info)951 static void sgivwfbcon_blank(int blank, struct fb_info *info)
952 {
953     /* Nothing */
954 }
955 
956 #ifdef MODULE
957 MODULE_LICENSE("GPL");
958 
init_module(void)959 int init_module(void)
960 {
961   return sgivwfb_init();
962 }
963 
cleanup_module(void)964 void cleanup_module(void)
965 {
966   unregister_framebuffer(&fb_info);
967   dbe_TurnOffDma();
968   iounmap(regs);
969   iounmap(fbmem);
970 }
971 
972 #endif /* MODULE */
973