blob: 33df9ec917951d1e48871af8f3a1edb8f8ccf319 [file] [log] [blame]
Ondrej Zajiceka2684222007-02-12 00:54:49 -08001/*
2 * Common utility functions for VGA-based graphics cards.
3 *
4 * Copyright (c) 2006-2007 Ondrej Zajicek <santiago@crfreenet.org>
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive for
8 * more details.
9 *
10 * Some parts are based on David Boucher's viafb (http://davesdomain.org.uk/viafb/)
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <linux/fb.h>
17#include <linux/svga.h>
Ondrej Zajiceka2684222007-02-12 00:54:49 -080018#include <asm/types.h>
19#include <asm/io.h>
20
21
22/* Write a CRT register value spread across multiple registers */
David Miller21da3862011-01-11 23:49:34 +000023void svga_wcrt_multi(void __iomem *regbase, const struct vga_regset *regset, u32 value)
24{
Ondrej Zajiceka2684222007-02-12 00:54:49 -080025 u8 regval, bitval, bitnum;
26
27 while (regset->regnum != VGA_REGSET_END_VAL) {
David Miller21da3862011-01-11 23:49:34 +000028 regval = vga_rcrt(regbase, regset->regnum);
Ondrej Zajiceka2684222007-02-12 00:54:49 -080029 bitnum = regset->lowbit;
30 while (bitnum <= regset->highbit) {
31 bitval = 1 << bitnum;
32 regval = regval & ~bitval;
33 if (value & 1) regval = regval | bitval;
34 bitnum ++;
35 value = value >> 1;
36 }
David Miller21da3862011-01-11 23:49:34 +000037 vga_wcrt(regbase, regset->regnum, regval);
Ondrej Zajiceka2684222007-02-12 00:54:49 -080038 regset ++;
39 }
40}
41
42/* Write a sequencer register value spread across multiple registers */
David Millerdc6aff32011-01-11 23:49:49 +000043void svga_wseq_multi(void __iomem *regbase, const struct vga_regset *regset, u32 value)
44{
Ondrej Zajiceka2684222007-02-12 00:54:49 -080045 u8 regval, bitval, bitnum;
46
47 while (regset->regnum != VGA_REGSET_END_VAL) {
David Millerdc6aff32011-01-11 23:49:49 +000048 regval = vga_rseq(regbase, regset->regnum);
Ondrej Zajiceka2684222007-02-12 00:54:49 -080049 bitnum = regset->lowbit;
50 while (bitnum <= regset->highbit) {
51 bitval = 1 << bitnum;
52 regval = regval & ~bitval;
53 if (value & 1) regval = regval | bitval;
54 bitnum ++;
55 value = value >> 1;
56 }
David Millerdc6aff32011-01-11 23:49:49 +000057 vga_wseq(regbase, regset->regnum, regval);
Ondrej Zajiceka2684222007-02-12 00:54:49 -080058 regset ++;
59 }
60}
61
62static unsigned int svga_regset_size(const struct vga_regset *regset)
63{
64 u8 count = 0;
65
66 while (regset->regnum != VGA_REGSET_END_VAL) {
67 count += regset->highbit - regset->lowbit + 1;
68 regset ++;
69 }
70 return 1 << count;
71}
72
73
74/* ------------------------------------------------------------------------- */
75
76
77/* Set graphics controller registers to sane values */
David Millere2fade22011-01-11 23:50:04 +000078void svga_set_default_gfx_regs(void __iomem *regbase)
Ondrej Zajiceka2684222007-02-12 00:54:49 -080079{
80 /* All standard GFX registers (GR00 - GR08) */
David Millere2fade22011-01-11 23:50:04 +000081 vga_wgfx(regbase, VGA_GFX_SR_VALUE, 0x00);
82 vga_wgfx(regbase, VGA_GFX_SR_ENABLE, 0x00);
83 vga_wgfx(regbase, VGA_GFX_COMPARE_VALUE, 0x00);
84 vga_wgfx(regbase, VGA_GFX_DATA_ROTATE, 0x00);
85 vga_wgfx(regbase, VGA_GFX_PLANE_READ, 0x00);
86 vga_wgfx(regbase, VGA_GFX_MODE, 0x00);
87/* vga_wgfx(regbase, VGA_GFX_MODE, 0x20); */
88/* vga_wgfx(regbase, VGA_GFX_MODE, 0x40); */
89 vga_wgfx(regbase, VGA_GFX_MISC, 0x05);
90/* vga_wgfx(regbase, VGA_GFX_MISC, 0x01); */
91 vga_wgfx(regbase, VGA_GFX_COMPARE_MASK, 0x0F);
92 vga_wgfx(regbase, VGA_GFX_BIT_MASK, 0xFF);
Ondrej Zajiceka2684222007-02-12 00:54:49 -080093}
94
95/* Set attribute controller registers to sane values */
David Millerf51a14d2011-01-11 23:50:36 +000096void svga_set_default_atc_regs(void __iomem *regbase)
Ondrej Zajiceka2684222007-02-12 00:54:49 -080097{
98 u8 count;
99
David Millerf51a14d2011-01-11 23:50:36 +0000100 vga_r(regbase, 0x3DA);
101 vga_w(regbase, VGA_ATT_W, 0x00);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800102
103 /* All standard ATC registers (AR00 - AR14) */
104 for (count = 0; count <= 0xF; count ++)
David Millerf51a14d2011-01-11 23:50:36 +0000105 svga_wattr(regbase, count, count);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800106
David Millerf51a14d2011-01-11 23:50:36 +0000107 svga_wattr(regbase, VGA_ATC_MODE, 0x01);
108/* svga_wattr(regbase, VGA_ATC_MODE, 0x41); */
109 svga_wattr(regbase, VGA_ATC_OVERSCAN, 0x00);
110 svga_wattr(regbase, VGA_ATC_PLANE_ENABLE, 0x0F);
111 svga_wattr(regbase, VGA_ATC_PEL, 0x00);
112 svga_wattr(regbase, VGA_ATC_COLOR_PAGE, 0x00);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800113
David Millerf51a14d2011-01-11 23:50:36 +0000114 vga_r(regbase, 0x3DA);
115 vga_w(regbase, VGA_ATT_W, 0x20);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800116}
117
118/* Set sequencer registers to sane values */
David Millera4ade832011-01-11 23:50:54 +0000119void svga_set_default_seq_regs(void __iomem *regbase)
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800120{
121 /* Standard sequencer registers (SR01 - SR04), SR00 is not set */
David Millera4ade832011-01-11 23:50:54 +0000122 vga_wseq(regbase, VGA_SEQ_CLOCK_MODE, VGA_SR01_CHAR_CLK_8DOTS);
123 vga_wseq(regbase, VGA_SEQ_PLANE_WRITE, VGA_SR02_ALL_PLANES);
124 vga_wseq(regbase, VGA_SEQ_CHARACTER_MAP, 0x00);
125/* vga_wseq(regbase, VGA_SEQ_MEMORY_MODE, VGA_SR04_EXT_MEM | VGA_SR04_SEQ_MODE | VGA_SR04_CHN_4M); */
126 vga_wseq(regbase, VGA_SEQ_MEMORY_MODE, VGA_SR04_EXT_MEM | VGA_SR04_SEQ_MODE);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800127}
128
129/* Set CRTC registers to sane values */
David Miller1d28fca2011-01-11 23:51:41 +0000130void svga_set_default_crt_regs(void __iomem *regbase)
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800131{
132 /* Standard CRT registers CR03 CR08 CR09 CR14 CR17 */
David Miller1d28fca2011-01-11 23:51:41 +0000133 svga_wcrt_mask(regbase, 0x03, 0x80, 0x80); /* Enable vertical retrace EVRA */
134 vga_wcrt(regbase, VGA_CRTC_PRESET_ROW, 0);
135 svga_wcrt_mask(regbase, VGA_CRTC_MAX_SCAN, 0, 0x1F);
136 vga_wcrt(regbase, VGA_CRTC_UNDERLINE, 0);
137 vga_wcrt(regbase, VGA_CRTC_MODE, 0xE3);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800138}
139
David Miller9c963942011-01-11 23:51:56 +0000140void svga_set_textmode_vga_regs(void __iomem *regbase)
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800141{
David Miller9c963942011-01-11 23:51:56 +0000142 /* svga_wseq_mask(regbase, 0x1, 0x00, 0x01); */ /* Switch 8/9 pixel per char */
143 vga_wseq(regbase, VGA_SEQ_MEMORY_MODE, VGA_SR04_EXT_MEM);
144 vga_wseq(regbase, VGA_SEQ_PLANE_WRITE, 0x03);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800145
David Miller9c963942011-01-11 23:51:56 +0000146 vga_wcrt(regbase, VGA_CRTC_MAX_SCAN, 0x0f); /* 0x4f */
147 vga_wcrt(regbase, VGA_CRTC_UNDERLINE, 0x1f);
148 svga_wcrt_mask(regbase, VGA_CRTC_MODE, 0x23, 0x7f);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800149
David Miller9c963942011-01-11 23:51:56 +0000150 vga_wcrt(regbase, VGA_CRTC_CURSOR_START, 0x0d);
151 vga_wcrt(regbase, VGA_CRTC_CURSOR_END, 0x0e);
152 vga_wcrt(regbase, VGA_CRTC_CURSOR_HI, 0x00);
153 vga_wcrt(regbase, VGA_CRTC_CURSOR_LO, 0x00);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800154
David Miller9c963942011-01-11 23:51:56 +0000155 vga_wgfx(regbase, VGA_GFX_MODE, 0x10); /* Odd/even memory mode */
156 vga_wgfx(regbase, VGA_GFX_MISC, 0x0E); /* Misc graphics register - text mode enable */
157 vga_wgfx(regbase, VGA_GFX_COMPARE_MASK, 0x00);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800158
David Miller9c963942011-01-11 23:51:56 +0000159 vga_r(regbase, 0x3DA);
160 vga_w(regbase, VGA_ATT_W, 0x00);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800161
David Miller9c963942011-01-11 23:51:56 +0000162 svga_wattr(regbase, 0x10, 0x0C); /* Attribute Mode Control Register - text mode, blinking and line graphics */
163 svga_wattr(regbase, 0x13, 0x08); /* Horizontal Pixel Panning Register */
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800164
David Miller9c963942011-01-11 23:51:56 +0000165 vga_r(regbase, 0x3DA);
166 vga_w(regbase, VGA_ATT_W, 0x20);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800167}
168
169#if 0
170void svga_dump_var(struct fb_var_screeninfo *var, int node)
171{
172 pr_debug("fb%d: var.vmode : 0x%X\n", node, var->vmode);
173 pr_debug("fb%d: var.xres : %d\n", node, var->xres);
174 pr_debug("fb%d: var.yres : %d\n", node, var->yres);
175 pr_debug("fb%d: var.bits_per_pixel: %d\n", node, var->bits_per_pixel);
176 pr_debug("fb%d: var.xres_virtual : %d\n", node, var->xres_virtual);
177 pr_debug("fb%d: var.yres_virtual : %d\n", node, var->yres_virtual);
178 pr_debug("fb%d: var.left_margin : %d\n", node, var->left_margin);
179 pr_debug("fb%d: var.right_margin : %d\n", node, var->right_margin);
180 pr_debug("fb%d: var.upper_margin : %d\n", node, var->upper_margin);
181 pr_debug("fb%d: var.lower_margin : %d\n", node, var->lower_margin);
182 pr_debug("fb%d: var.hsync_len : %d\n", node, var->hsync_len);
183 pr_debug("fb%d: var.vsync_len : %d\n", node, var->vsync_len);
184 pr_debug("fb%d: var.sync : 0x%X\n", node, var->sync);
185 pr_debug("fb%d: var.pixclock : %d\n\n", node, var->pixclock);
186}
187#endif /* 0 */
188
189
190/* ------------------------------------------------------------------------- */
191
192
193void svga_settile(struct fb_info *info, struct fb_tilemap *map)
194{
195 const u8 *font = map->data;
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700196 u8 __iomem *fb = (u8 __iomem *)info->screen_base;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800197 int i, c;
198
199 if ((map->width != 8) || (map->height != 16) ||
200 (map->depth != 1) || (map->length != 256)) {
201 printk(KERN_ERR "fb%d: unsupported font parameters: width %d, height %d, depth %d, length %d\n",
202 info->node, map->width, map->height, map->depth, map->length);
203 return;
204 }
205
206 fb += 2;
207 for (c = 0; c < map->length; c++) {
208 for (i = 0; i < map->height; i++) {
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700209 fb_writeb(font[i], fb + i * 4);
210// fb[i * 4] = font[i];
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800211 }
212 fb += 128;
213 font += map->height;
214 }
215}
216
217/* Copy area in text (tileblit) mode */
218void svga_tilecopy(struct fb_info *info, struct fb_tilearea *area)
219{
220 int dx, dy;
221 /* colstride is halved in this function because u16 are used */
222 int colstride = 1 << (info->fix.type_aux & FB_AUX_TEXT_SVGA_MASK);
223 int rowstride = colstride * (info->var.xres_virtual / 8);
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700224 u16 __iomem *fb = (u16 __iomem *) info->screen_base;
225 u16 __iomem *src, *dst;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800226
227 if ((area->sy > area->dy) ||
228 ((area->sy == area->dy) && (area->sx > area->dx))) {
229 src = fb + area->sx * colstride + area->sy * rowstride;
230 dst = fb + area->dx * colstride + area->dy * rowstride;
231 } else {
232 src = fb + (area->sx + area->width - 1) * colstride
233 + (area->sy + area->height - 1) * rowstride;
234 dst = fb + (area->dx + area->width - 1) * colstride
235 + (area->dy + area->height - 1) * rowstride;
236
237 colstride = -colstride;
238 rowstride = -rowstride;
239 }
240
241 for (dy = 0; dy < area->height; dy++) {
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700242 u16 __iomem *src2 = src;
243 u16 __iomem *dst2 = dst;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800244 for (dx = 0; dx < area->width; dx++) {
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700245 fb_writew(fb_readw(src2), dst2);
246// *dst2 = *src2;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800247 src2 += colstride;
248 dst2 += colstride;
249 }
250 src += rowstride;
251 dst += rowstride;
252 }
253}
254
255/* Fill area in text (tileblit) mode */
256void svga_tilefill(struct fb_info *info, struct fb_tilerect *rect)
257{
258 int dx, dy;
259 int colstride = 2 << (info->fix.type_aux & FB_AUX_TEXT_SVGA_MASK);
260 int rowstride = colstride * (info->var.xres_virtual / 8);
261 int attr = (0x0F & rect->bg) << 4 | (0x0F & rect->fg);
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700262 u8 __iomem *fb = (u8 __iomem *)info->screen_base;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800263 fb += rect->sx * colstride + rect->sy * rowstride;
264
265 for (dy = 0; dy < rect->height; dy++) {
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700266 u8 __iomem *fb2 = fb;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800267 for (dx = 0; dx < rect->width; dx++) {
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700268 fb_writeb(rect->index, fb2);
269 fb_writeb(attr, fb2 + 1);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800270 fb2 += colstride;
271 }
272 fb += rowstride;
273 }
274}
275
276/* Write text in text (tileblit) mode */
277void svga_tileblit(struct fb_info *info, struct fb_tileblit *blit)
278{
279 int dx, dy, i;
280 int colstride = 2 << (info->fix.type_aux & FB_AUX_TEXT_SVGA_MASK);
281 int rowstride = colstride * (info->var.xres_virtual / 8);
282 int attr = (0x0F & blit->bg) << 4 | (0x0F & blit->fg);
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700283 u8 __iomem *fb = (u8 __iomem *)info->screen_base;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800284 fb += blit->sx * colstride + blit->sy * rowstride;
285
286 i=0;
287 for (dy=0; dy < blit->height; dy ++) {
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700288 u8 __iomem *fb2 = fb;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800289 for (dx = 0; dx < blit->width; dx ++) {
Antonino A. Daplas78494dd2007-05-08 00:38:46 -0700290 fb_writeb(blit->indices[i], fb2);
291 fb_writeb(attr, fb2 + 1);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800292 fb2 += colstride;
293 i ++;
294 if (i == blit->length) return;
295 }
296 fb += rowstride;
297 }
298
299}
300
301/* Set cursor in text (tileblit) mode */
David Miller55db0922011-01-11 23:52:11 +0000302void svga_tilecursor(void __iomem *regbase, struct fb_info *info, struct fb_tilecursor *cursor)
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800303{
304 u8 cs = 0x0d;
305 u8 ce = 0x0e;
306 u16 pos = cursor->sx + (info->var.xoffset / 8)
307 + (cursor->sy + (info->var.yoffset / 16))
308 * (info->var.xres_virtual / 8);
309
310 if (! cursor -> mode)
311 return;
312
David Miller55db0922011-01-11 23:52:11 +0000313 svga_wcrt_mask(regbase, 0x0A, 0x20, 0x20); /* disable cursor */
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800314
315 if (cursor -> shape == FB_TILE_CURSOR_NONE)
316 return;
317
318 switch (cursor -> shape) {
319 case FB_TILE_CURSOR_UNDERLINE:
320 cs = 0x0d;
321 break;
322 case FB_TILE_CURSOR_LOWER_THIRD:
323 cs = 0x09;
324 break;
325 case FB_TILE_CURSOR_LOWER_HALF:
326 cs = 0x07;
327 break;
328 case FB_TILE_CURSOR_TWO_THIRDS:
329 cs = 0x05;
330 break;
331 case FB_TILE_CURSOR_BLOCK:
332 cs = 0x01;
333 break;
334 }
335
336 /* set cursor position */
David Miller55db0922011-01-11 23:52:11 +0000337 vga_wcrt(regbase, 0x0E, pos >> 8);
338 vga_wcrt(regbase, 0x0F, pos & 0xFF);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800339
David Miller55db0922011-01-11 23:52:11 +0000340 vga_wcrt(regbase, 0x0B, ce); /* set cursor end */
341 vga_wcrt(regbase, 0x0A, cs); /* set cursor start and enable it */
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800342}
343
Ondrej Zajicek34ed25f2007-05-08 00:40:00 -0700344int svga_get_tilemax(struct fb_info *info)
345{
346 return 256;
347}
348
Antonino A. Daplas5a87ede2007-05-09 02:35:32 -0700349/* Get capabilities of accelerator based on the mode */
350
351void svga_get_caps(struct fb_info *info, struct fb_blit_caps *caps,
352 struct fb_var_screeninfo *var)
353{
354 if (var->bits_per_pixel == 0) {
355 /* can only support 256 8x16 bitmap */
356 caps->x = 1 << (8 - 1);
357 caps->y = 1 << (16 - 1);
358 caps->len = 256;
359 } else {
360 caps->x = (var->bits_per_pixel == 4) ? 1 << (8 - 1) : ~(u32)0;
361 caps->y = ~(u32)0;
362 caps->len = ~(u32)0;
363 }
364}
365EXPORT_SYMBOL(svga_get_caps);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800366
367/* ------------------------------------------------------------------------- */
368
369
370/*
371 * Compute PLL settings (M, N, R)
372 * F_VCO = (F_BASE * M) / N
373 * F_OUT = F_VCO / (2^R)
374 */
375
376static inline u32 abs_diff(u32 a, u32 b)
377{
378 return (a > b) ? (a - b) : (b - a);
379}
380
381int svga_compute_pll(const struct svga_pll *pll, u32 f_wanted, u16 *m, u16 *n, u16 *r, int node)
382{
383 u16 am, an, ar;
384 u32 f_vco, f_current, delta_current, delta_best;
385
386 pr_debug("fb%d: ideal frequency: %d kHz\n", node, (unsigned int) f_wanted);
387
388 ar = pll->r_max;
389 f_vco = f_wanted << ar;
390
391 /* overflow check */
392 if ((f_vco >> ar) != f_wanted)
393 return -EINVAL;
394
395 /* It is usually better to have greater VCO clock
396 because of better frequency stability.
397 So first try r_max, then r smaller. */
398 while ((ar > pll->r_min) && (f_vco > pll->f_vco_max)) {
399 ar--;
400 f_vco = f_vco >> 1;
401 }
402
403 /* VCO bounds check */
404 if ((f_vco < pll->f_vco_min) || (f_vco > pll->f_vco_max))
405 return -EINVAL;
406
407 delta_best = 0xFFFFFFFF;
408 *m = 0;
409 *n = 0;
410 *r = ar;
411
412 am = pll->m_min;
413 an = pll->n_min;
414
415 while ((am <= pll->m_max) && (an <= pll->n_max)) {
416 f_current = (pll->f_base * am) / an;
417 delta_current = abs_diff (f_current, f_vco);
418
419 if (delta_current < delta_best) {
420 delta_best = delta_current;
421 *m = am;
422 *n = an;
423 }
424
425 if (f_current <= f_vco) {
426 am ++;
427 } else {
428 an ++;
429 }
430 }
431
432 f_current = (pll->f_base * *m) / *n;
433 pr_debug("fb%d: found frequency: %d kHz (VCO %d kHz)\n", node, (int) (f_current >> ar), (int) f_current);
434 pr_debug("fb%d: m = %d n = %d r = %d\n", node, (unsigned int) *m, (unsigned int) *n, (unsigned int) *r);
435 return 0;
436}
437
438
439/* ------------------------------------------------------------------------- */
440
441
442/* Check CRT timing values */
443int svga_check_timings(const struct svga_timing_regs *tm, struct fb_var_screeninfo *var, int node)
444{
445 u32 value;
446
447 var->xres = (var->xres+7)&~7;
448 var->left_margin = (var->left_margin+7)&~7;
449 var->right_margin = (var->right_margin+7)&~7;
450 var->hsync_len = (var->hsync_len+7)&~7;
451
452 /* Check horizontal total */
453 value = var->xres + var->left_margin + var->right_margin + var->hsync_len;
454 if (((value / 8) - 5) >= svga_regset_size (tm->h_total_regs))
455 return -EINVAL;
456
457 /* Check horizontal display and blank start */
458 value = var->xres;
459 if (((value / 8) - 1) >= svga_regset_size (tm->h_display_regs))
460 return -EINVAL;
461 if (((value / 8) - 1) >= svga_regset_size (tm->h_blank_start_regs))
462 return -EINVAL;
463
464 /* Check horizontal sync start */
465 value = var->xres + var->right_margin;
466 if (((value / 8) - 1) >= svga_regset_size (tm->h_sync_start_regs))
467 return -EINVAL;
468
469 /* Check horizontal blank end (or length) */
470 value = var->left_margin + var->right_margin + var->hsync_len;
471 if ((value == 0) || ((value / 8) >= svga_regset_size (tm->h_blank_end_regs)))
472 return -EINVAL;
473
474 /* Check horizontal sync end (or length) */
475 value = var->hsync_len;
476 if ((value == 0) || ((value / 8) >= svga_regset_size (tm->h_sync_end_regs)))
477 return -EINVAL;
478
479 /* Check vertical total */
480 value = var->yres + var->upper_margin + var->lower_margin + var->vsync_len;
481 if ((value - 1) >= svga_regset_size(tm->v_total_regs))
482 return -EINVAL;
483
484 /* Check vertical display and blank start */
485 value = var->yres;
486 if ((value - 1) >= svga_regset_size(tm->v_display_regs))
487 return -EINVAL;
488 if ((value - 1) >= svga_regset_size(tm->v_blank_start_regs))
489 return -EINVAL;
490
491 /* Check vertical sync start */
492 value = var->yres + var->lower_margin;
493 if ((value - 1) >= svga_regset_size(tm->v_sync_start_regs))
494 return -EINVAL;
495
496 /* Check vertical blank end (or length) */
497 value = var->upper_margin + var->lower_margin + var->vsync_len;
498 if ((value == 0) || (value >= svga_regset_size (tm->v_blank_end_regs)))
499 return -EINVAL;
500
501 /* Check vertical sync end (or length) */
502 value = var->vsync_len;
503 if ((value == 0) || (value >= svga_regset_size (tm->v_sync_end_regs)))
504 return -EINVAL;
505
506 return 0;
507}
508
509/* Set CRT timing registers */
David Miller38d26202011-01-11 23:52:25 +0000510void svga_set_timings(void __iomem *regbase, const struct svga_timing_regs *tm,
511 struct fb_var_screeninfo *var,
512 u32 hmul, u32 hdiv, u32 vmul, u32 vdiv, u32 hborder, int node)
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800513{
514 u8 regval;
515 u32 value;
516
517 value = var->xres + var->left_margin + var->right_margin + var->hsync_len;
518 value = (value * hmul) / hdiv;
519 pr_debug("fb%d: horizontal total : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000520 svga_wcrt_multi(regbase, tm->h_total_regs, (value / 8) - 5);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800521
522 value = var->xres;
523 value = (value * hmul) / hdiv;
524 pr_debug("fb%d: horizontal display : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000525 svga_wcrt_multi(regbase, tm->h_display_regs, (value / 8) - 1);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800526
527 value = var->xres;
528 value = (value * hmul) / hdiv;
529 pr_debug("fb%d: horizontal blank start: %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000530 svga_wcrt_multi(regbase, tm->h_blank_start_regs, (value / 8) - 1 + hborder);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800531
532 value = var->xres + var->left_margin + var->right_margin + var->hsync_len;
533 value = (value * hmul) / hdiv;
534 pr_debug("fb%d: horizontal blank end : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000535 svga_wcrt_multi(regbase, tm->h_blank_end_regs, (value / 8) - 1 - hborder);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800536
537 value = var->xres + var->right_margin;
538 value = (value * hmul) / hdiv;
539 pr_debug("fb%d: horizontal sync start : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000540 svga_wcrt_multi(regbase, tm->h_sync_start_regs, (value / 8));
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800541
542 value = var->xres + var->right_margin + var->hsync_len;
543 value = (value * hmul) / hdiv;
544 pr_debug("fb%d: horizontal sync end : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000545 svga_wcrt_multi(regbase, tm->h_sync_end_regs, (value / 8));
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800546
547 value = var->yres + var->upper_margin + var->lower_margin + var->vsync_len;
548 value = (value * vmul) / vdiv;
549 pr_debug("fb%d: vertical total : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000550 svga_wcrt_multi(regbase, tm->v_total_regs, value - 2);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800551
552 value = var->yres;
553 value = (value * vmul) / vdiv;
554 pr_debug("fb%d: vertical display : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000555 svga_wcrt_multi(regbase, tm->v_display_regs, value - 1);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800556
557 value = var->yres;
558 value = (value * vmul) / vdiv;
559 pr_debug("fb%d: vertical blank start : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000560 svga_wcrt_multi(regbase, tm->v_blank_start_regs, value);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800561
562 value = var->yres + var->upper_margin + var->lower_margin + var->vsync_len;
563 value = (value * vmul) / vdiv;
564 pr_debug("fb%d: vertical blank end : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000565 svga_wcrt_multi(regbase, tm->v_blank_end_regs, value - 2);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800566
567 value = var->yres + var->lower_margin;
568 value = (value * vmul) / vdiv;
569 pr_debug("fb%d: vertical sync start : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000570 svga_wcrt_multi(regbase, tm->v_sync_start_regs, value);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800571
572 value = var->yres + var->lower_margin + var->vsync_len;
573 value = (value * vmul) / vdiv;
574 pr_debug("fb%d: vertical sync end : %d\n", node, value);
David Miller38d26202011-01-11 23:52:25 +0000575 svga_wcrt_multi(regbase, tm->v_sync_end_regs, value);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800576
577 /* Set horizontal and vertical sync pulse polarity in misc register */
578
David Miller38d26202011-01-11 23:52:25 +0000579 regval = vga_r(regbase, VGA_MIS_R);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800580 if (var->sync & FB_SYNC_HOR_HIGH_ACT) {
581 pr_debug("fb%d: positive horizontal sync\n", node);
582 regval = regval & ~0x80;
583 } else {
584 pr_debug("fb%d: negative horizontal sync\n", node);
585 regval = regval | 0x80;
586 }
587 if (var->sync & FB_SYNC_VERT_HIGH_ACT) {
588 pr_debug("fb%d: positive vertical sync\n", node);
589 regval = regval & ~0x40;
590 } else {
591 pr_debug("fb%d: negative vertical sync\n\n", node);
592 regval = regval | 0x40;
593 }
David Miller38d26202011-01-11 23:52:25 +0000594 vga_w(regbase, VGA_MIS_W, regval);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800595}
596
597
598/* ------------------------------------------------------------------------- */
599
600
Ondrej Zajicekd4b766a2007-10-16 01:29:52 -0700601static inline int match_format(const struct svga_fb_format *frm,
602 struct fb_var_screeninfo *var)
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800603{
604 int i = 0;
Ondrej Zajicekd4b766a2007-10-16 01:29:52 -0700605 int stored = -EINVAL;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800606
607 while (frm->bits_per_pixel != SVGA_FORMAT_END_VAL)
608 {
609 if ((var->bits_per_pixel == frm->bits_per_pixel) &&
610 (var->red.length <= frm->red.length) &&
611 (var->green.length <= frm->green.length) &&
612 (var->blue.length <= frm->blue.length) &&
613 (var->transp.length <= frm->transp.length) &&
Ondrej Zajicekd4b766a2007-10-16 01:29:52 -0700614 (var->nonstd == frm->nonstd))
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800615 return i;
Ondrej Zajicekd4b766a2007-10-16 01:29:52 -0700616 if (var->bits_per_pixel == frm->bits_per_pixel)
617 stored = i;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800618 i++;
619 frm++;
620 }
Ondrej Zajicekd4b766a2007-10-16 01:29:52 -0700621 return stored;
622}
623
624int svga_match_format(const struct svga_fb_format *frm,
625 struct fb_var_screeninfo *var,
626 struct fb_fix_screeninfo *fix)
627{
628 int i = match_format(frm, var);
629
630 if (i >= 0) {
631 var->bits_per_pixel = frm[i].bits_per_pixel;
632 var->red = frm[i].red;
633 var->green = frm[i].green;
634 var->blue = frm[i].blue;
635 var->transp = frm[i].transp;
636 var->nonstd = frm[i].nonstd;
637 if (fix != NULL) {
638 fix->type = frm[i].type;
639 fix->type_aux = frm[i].type_aux;
640 fix->visual = frm[i].visual;
641 fix->xpanstep = frm[i].xpanstep;
642 }
643 }
644
645 return i;
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800646}
647
648
649EXPORT_SYMBOL(svga_wcrt_multi);
650EXPORT_SYMBOL(svga_wseq_multi);
651
652EXPORT_SYMBOL(svga_set_default_gfx_regs);
653EXPORT_SYMBOL(svga_set_default_atc_regs);
654EXPORT_SYMBOL(svga_set_default_seq_regs);
655EXPORT_SYMBOL(svga_set_default_crt_regs);
656EXPORT_SYMBOL(svga_set_textmode_vga_regs);
657
658EXPORT_SYMBOL(svga_settile);
659EXPORT_SYMBOL(svga_tilecopy);
660EXPORT_SYMBOL(svga_tilefill);
661EXPORT_SYMBOL(svga_tileblit);
662EXPORT_SYMBOL(svga_tilecursor);
Ondrej Zajicek34ed25f2007-05-08 00:40:00 -0700663EXPORT_SYMBOL(svga_get_tilemax);
Ondrej Zajiceka2684222007-02-12 00:54:49 -0800664
665EXPORT_SYMBOL(svga_compute_pll);
666EXPORT_SYMBOL(svga_check_timings);
667EXPORT_SYMBOL(svga_set_timings);
668EXPORT_SYMBOL(svga_match_format);
669
670MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>");
671MODULE_DESCRIPTION("Common utility functions for VGA-based graphics cards");
672MODULE_LICENSE("GPL");