2 * i740fb - framebuffer driver for Intel740
3 * Copyright (c) 2011 Ondrej Zary
5 * Based on old i740fb driver (c) 2001-2002 Andrey Ulanov <drey@rt.mipt.ru>
6 * which was partially based on:
7 * VGA 16-color framebuffer driver (c) 1999 Ben Pfaff <pfaffben@debian.org>
8 * and Petr Vandrovec <VANDROVE@vc.cvut.cz>
9 * i740 driver from XFree86 (c) 1998-1999 Precision Insight, Inc., Cedar Park,
11 * i740fb by Patrick LERDA, v0.9
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <linux/pci_ids.h>
25 #include <linux/i2c.h>
26 #include <linux/i2c-algo-bit.h>
27 #include <linux/console.h>
28 #include <video/vga.h>
36 static char *mode_option
;
43 unsigned char __iomem
*regs
;
49 struct i2c_adapter ddc_adapter
;
50 struct i2c_algo_bit_data ddc_algo
;
51 u32 pseudo_palette
[16];
52 struct mutex open_lock
;
53 unsigned int ref_count
;
62 /* i740 specific registers */
69 u8 video_clk2_mn_msbs
;
70 u8 video_clk2_div_sel
;
77 u8 ext_vert_sync_start
;
78 u8 ext_vert_blank_start
;
83 u32 lmi_fifo_watermark
;
89 #define DACSPEED16 163
90 #define DACSPEED24_SG 136
91 #define DACSPEED24_SD 128
94 static struct fb_fix_screeninfo i740fb_fix
= {
96 .type
= FB_TYPE_PACKED_PIXELS
,
97 .visual
= FB_VISUAL_TRUECOLOR
,
100 .accel
= FB_ACCEL_NONE
,
103 static inline void i740outb(struct i740fb_par
*par
, u16 port
, u8 val
)
105 vga_mm_w(par
->regs
, port
, val
);
107 static inline u8
i740inb(struct i740fb_par
*par
, u16 port
)
109 return vga_mm_r(par
->regs
, port
);
111 static inline void i740outreg(struct i740fb_par
*par
, u16 port
, u8 reg
, u8 val
)
113 vga_mm_w_fast(par
->regs
, port
, reg
, val
);
115 static inline u8
i740inreg(struct i740fb_par
*par
, u16 port
, u8 reg
)
117 vga_mm_w(par
->regs
, port
, reg
);
118 return vga_mm_r(par
->regs
, port
+1);
120 static inline void i740outreg_mask(struct i740fb_par
*par
, u16 port
, u8 reg
,
123 vga_mm_w_fast(par
->regs
, port
, reg
, (val
& mask
)
124 | (i740inreg(par
, port
, reg
) & ~mask
));
127 #define REG_DDC_DRIVE 0x62
128 #define REG_DDC_STATE 0x63
129 #define DDC_SCL (1 << 3)
130 #define DDC_SDA (1 << 2)
132 static void i740fb_ddc_setscl(void *data
, int val
)
134 struct i740fb_par
*par
= data
;
136 i740outreg_mask(par
, XRX
, REG_DDC_DRIVE
, DDC_SCL
, DDC_SCL
);
137 i740outreg_mask(par
, XRX
, REG_DDC_STATE
, val
? DDC_SCL
: 0, DDC_SCL
);
140 static void i740fb_ddc_setsda(void *data
, int val
)
142 struct i740fb_par
*par
= data
;
144 i740outreg_mask(par
, XRX
, REG_DDC_DRIVE
, DDC_SDA
, DDC_SDA
);
145 i740outreg_mask(par
, XRX
, REG_DDC_STATE
, val
? DDC_SDA
: 0, DDC_SDA
);
148 static int i740fb_ddc_getscl(void *data
)
150 struct i740fb_par
*par
= data
;
152 i740outreg_mask(par
, XRX
, REG_DDC_DRIVE
, 0, DDC_SCL
);
154 return !!(i740inreg(par
, XRX
, REG_DDC_STATE
) & DDC_SCL
);
157 static int i740fb_ddc_getsda(void *data
)
159 struct i740fb_par
*par
= data
;
161 i740outreg_mask(par
, XRX
, REG_DDC_DRIVE
, 0, DDC_SDA
);
163 return !!(i740inreg(par
, XRX
, REG_DDC_STATE
) & DDC_SDA
);
166 static int i740fb_setup_ddc_bus(struct fb_info
*info
)
168 struct i740fb_par
*par
= info
->par
;
170 strlcpy(par
->ddc_adapter
.name
, info
->fix
.id
,
171 sizeof(par
->ddc_adapter
.name
));
172 par
->ddc_adapter
.owner
= THIS_MODULE
;
173 par
->ddc_adapter
.class = I2C_CLASS_DDC
;
174 par
->ddc_adapter
.algo_data
= &par
->ddc_algo
;
175 par
->ddc_adapter
.dev
.parent
= info
->device
;
176 par
->ddc_algo
.setsda
= i740fb_ddc_setsda
;
177 par
->ddc_algo
.setscl
= i740fb_ddc_setscl
;
178 par
->ddc_algo
.getsda
= i740fb_ddc_getsda
;
179 par
->ddc_algo
.getscl
= i740fb_ddc_getscl
;
180 par
->ddc_algo
.udelay
= 10;
181 par
->ddc_algo
.timeout
= 20;
182 par
->ddc_algo
.data
= par
;
184 i2c_set_adapdata(&par
->ddc_adapter
, par
);
186 return i2c_bit_add_bus(&par
->ddc_adapter
);
189 static int i740fb_open(struct fb_info
*info
, int user
)
191 struct i740fb_par
*par
= info
->par
;
193 mutex_lock(&(par
->open_lock
));
195 mutex_unlock(&(par
->open_lock
));
200 static int i740fb_release(struct fb_info
*info
, int user
)
202 struct i740fb_par
*par
= info
->par
;
204 mutex_lock(&(par
->open_lock
));
205 if (par
->ref_count
== 0) {
206 fb_err(info
, "release called with zero refcount\n");
207 mutex_unlock(&(par
->open_lock
));
212 mutex_unlock(&(par
->open_lock
));
217 static u32
i740_calc_fifo(struct i740fb_par
*par
, u32 freq
, int bpp
)
220 * Would like to calculate these values automatically, but a generic
221 * algorithm does not seem possible. Note: These FIFO water mark
222 * values were tested on several cards and seem to eliminate the
223 * all of the snow and vertical banding, but fine adjustments will
224 * probably be required for other cards.
242 if (par
->has_sgram
) {
279 if (par
->has_sgram
) {
314 if (par
->has_sgram
) {
343 /* clock calculation from i740fb by Patrick LERDA */
345 #define I740_RFREQ 1000000
346 #define TARGET_MAX_N 30
347 #define I740_FFIX (1 << 8)
348 #define I740_RFREQ_FIX (I740_RFREQ / I740_FFIX)
349 #define I740_REF_FREQ (6667 * I740_FFIX / 100) /* 66.67 MHz */
350 #define I740_MAX_VCO_FREQ (450 * I740_FFIX) /* 450 MHz */
352 static void i740_calc_vclk(u32 freq
, struct i740fb_par
*par
)
354 const u32 err_max
= freq
/ (200 * I740_RFREQ
/ I740_FFIX
);
355 const u32 err_target
= freq
/ (1000 * I740_RFREQ
/ I740_FFIX
);
356 u32 err_best
= 512 * I740_FFIX
;
358 int m_best
= 0, n_best
= 0, p_best
= 0, d_best
= 0;
361 p_best
= min(15, ilog2(I740_MAX_VCO_FREQ
/ (freq
/ I740_RFREQ_FIX
)));
363 f_vco
= (freq
* (1 << p_best
)) / I740_RFREQ_FIX
;
364 freq
= freq
/ I740_RFREQ_FIX
;
369 m
= ((f_vco
* n
) / I740_REF_FREQ
+ 2) / 4;
375 u32 f_out
= (((m
* I740_REF_FREQ
* (4 << 2 * d_best
))
376 / n
) + ((1 << p_best
) / 2)) / (1 << p_best
);
378 f_err
= (freq
- f_out
);
380 if (abs(f_err
) < err_max
) {
386 } while ((abs(f_err
) >= err_target
) &&
387 ((n
<= TARGET_MAX_N
) || (abs(err_best
) > err_max
)));
389 if (abs(f_err
) < err_target
) {
394 par
->video_clk2_m
= (m_best
- 2) & 0xFF;
395 par
->video_clk2_n
= (n_best
- 2) & 0xFF;
396 par
->video_clk2_mn_msbs
= ((((n_best
- 2) >> 4) & VCO_N_MSBS
)
397 | (((m_best
- 2) >> 8) & VCO_M_MSBS
));
398 par
->video_clk2_div_sel
=
399 ((p_best
<< 4) | (d_best
? 4 : 0) | REF_DIV_1
);
402 static int i740fb_decode_var(const struct fb_var_screeninfo
*var
,
403 struct i740fb_par
*par
, struct fb_info
*info
)
406 * Get the video params out of 'var'.
407 * If a value doesn't fit, round it up, if it's too big, return -EINVAL.
410 u32 xres
, right
, hslen
, left
, xtotal
;
411 u32 yres
, lower
, vslen
, upper
, ytotal
;
412 u32 vxres
, xoffset
, vyres
, yoffset
;
413 u32 bpp
, base
, dacspeed24
, mem
;
417 dev_dbg(info
->device
, "decode_var: xres: %i, yres: %i, xres_v: %i, xres_v: %i\n",
418 var
->xres
, var
->yres
, var
->xres_virtual
, var
->xres_virtual
);
419 dev_dbg(info
->device
, " xoff: %i, yoff: %i, bpp: %i, graysc: %i\n",
420 var
->xoffset
, var
->yoffset
, var
->bits_per_pixel
,
422 dev_dbg(info
->device
, " activate: %i, nonstd: %i, vmode: %i\n",
423 var
->activate
, var
->nonstd
, var
->vmode
);
424 dev_dbg(info
->device
, " pixclock: %i, hsynclen:%i, vsynclen:%i\n",
425 var
->pixclock
, var
->hsync_len
, var
->vsync_len
);
426 dev_dbg(info
->device
, " left: %i, right: %i, up:%i, lower:%i\n",
427 var
->left_margin
, var
->right_margin
, var
->upper_margin
,
431 bpp
= var
->bits_per_pixel
;
435 if ((1000000 / var
->pixclock
) > DACSPEED8
) {
436 dev_err(info
->device
, "requested pixclock %i MHz out of range (max. %i MHz at 8bpp)\n",
437 1000000 / var
->pixclock
, DACSPEED8
);
444 if ((1000000 / var
->pixclock
) > DACSPEED16
) {
445 dev_err(info
->device
, "requested pixclock %i MHz out of range (max. %i MHz at 15/16bpp)\n",
446 1000000 / var
->pixclock
, DACSPEED16
);
452 dacspeed24
= par
->has_sgram
? DACSPEED24_SG
: DACSPEED24_SD
;
453 if ((1000000 / var
->pixclock
) > dacspeed24
) {
454 dev_err(info
->device
, "requested pixclock %i MHz out of range (max. %i MHz at 24bpp)\n",
455 1000000 / var
->pixclock
, dacspeed24
);
461 if ((1000000 / var
->pixclock
) > DACSPEED32
) {
462 dev_err(info
->device
, "requested pixclock %i MHz out of range (max. %i MHz at 32bpp)\n",
463 1000000 / var
->pixclock
, DACSPEED32
);
471 xres
= ALIGN(var
->xres
, 8);
472 vxres
= ALIGN(var
->xres_virtual
, 16);
476 xoffset
= ALIGN(var
->xoffset
, 8);
477 if (xres
+ xoffset
> vxres
)
478 xoffset
= vxres
- xres
;
480 left
= ALIGN(var
->left_margin
, 8);
481 right
= ALIGN(var
->right_margin
, 8);
482 hslen
= ALIGN(var
->hsync_len
, 8);
485 vyres
= var
->yres_virtual
;
489 yoffset
= var
->yoffset
;
490 if (yres
+ yoffset
> vyres
)
491 yoffset
= vyres
- yres
;
493 lower
= var
->lower_margin
;
494 vslen
= var
->vsync_len
;
495 upper
= var
->upper_margin
;
497 mem
= vxres
* vyres
* ((bpp
+ 1) / 8);
498 if (mem
> info
->screen_size
) {
499 dev_err(info
->device
, "not enough video memory (%d KB requested, %ld KB available)\n",
500 mem
>> 10, info
->screen_size
>> 10);
504 if (yoffset
+ yres
> vyres
)
505 yoffset
= vyres
- yres
;
507 xtotal
= xres
+ right
+ hslen
+ left
;
508 ytotal
= yres
+ lower
+ vslen
+ upper
;
510 par
->crtc
[VGA_CRTC_H_TOTAL
] = (xtotal
>> 3) - 5;
511 par
->crtc
[VGA_CRTC_H_DISP
] = (xres
>> 3) - 1;
512 par
->crtc
[VGA_CRTC_H_BLANK_START
] = ((xres
+ right
) >> 3) - 1;
513 par
->crtc
[VGA_CRTC_H_SYNC_START
] = (xres
+ right
) >> 3;
514 par
->crtc
[VGA_CRTC_H_SYNC_END
] = (((xres
+ right
+ hslen
) >> 3) & 0x1F)
515 | ((((xres
+ right
+ hslen
) >> 3) & 0x20) << 2);
516 par
->crtc
[VGA_CRTC_H_BLANK_END
] = ((xres
+ right
+ hslen
) >> 3 & 0x1F)
519 par
->crtc
[VGA_CRTC_V_TOTAL
] = ytotal
- 2;
521 r7
= 0x10; /* disable linecompare */
527 par
->crtc
[VGA_CRTC_PRESET_ROW
] = 0;
528 par
->crtc
[VGA_CRTC_MAX_SCAN
] = 0x40; /* 1 scanline, no linecmp */
529 if (var
->vmode
& FB_VMODE_DOUBLE
)
530 par
->crtc
[VGA_CRTC_MAX_SCAN
] |= 0x80;
531 par
->crtc
[VGA_CRTC_CURSOR_START
] = 0x00;
532 par
->crtc
[VGA_CRTC_CURSOR_END
] = 0x00;
533 par
->crtc
[VGA_CRTC_CURSOR_HI
] = 0x00;
534 par
->crtc
[VGA_CRTC_CURSOR_LO
] = 0x00;
535 par
->crtc
[VGA_CRTC_V_DISP_END
] = yres
-1;
536 if ((yres
-1) & 0x100)
538 if ((yres
-1) & 0x200)
541 par
->crtc
[VGA_CRTC_V_BLANK_START
] = yres
+ lower
- 1;
542 par
->crtc
[VGA_CRTC_V_SYNC_START
] = yres
+ lower
- 1;
543 if ((yres
+ lower
- 1) & 0x100)
545 if ((yres
+ lower
- 1) & 0x200) {
546 par
->crtc
[VGA_CRTC_MAX_SCAN
] |= 0x20;
551 par
->crtc
[VGA_CRTC_V_SYNC_END
] =
552 ((yres
+ lower
- 1 + vslen
) & 0x0F) & ~0x10;
553 /* 0x7F for VGA, but some SVGA chips require all 8 bits to be set */
554 par
->crtc
[VGA_CRTC_V_BLANK_END
] = (yres
+ lower
- 1 + vslen
) & 0xFF;
556 par
->crtc
[VGA_CRTC_UNDERLINE
] = 0x00;
557 par
->crtc
[VGA_CRTC_MODE
] = 0xC3 ;
558 par
->crtc
[VGA_CRTC_LINE_COMPARE
] = 0xFF;
559 par
->crtc
[VGA_CRTC_OVERFLOW
] = r7
;
561 par
->vss
= 0x00; /* 3DA */
563 for (i
= 0x00; i
< 0x10; i
++)
565 par
->atc
[VGA_ATC_MODE
] = 0x81;
566 par
->atc
[VGA_ATC_OVERSCAN
] = 0x00; /* 0 for EGA, 0xFF for VGA */
567 par
->atc
[VGA_ATC_PLANE_ENABLE
] = 0x0F;
568 par
->atc
[VGA_ATC_COLOR_PAGE
] = 0x00;
571 if (var
->sync
& FB_SYNC_HOR_HIGH_ACT
)
573 if (var
->sync
& FB_SYNC_VERT_HIGH_ACT
)
576 par
->seq
[VGA_SEQ_CLOCK_MODE
] = 0x01;
577 par
->seq
[VGA_SEQ_PLANE_WRITE
] = 0x0F;
578 par
->seq
[VGA_SEQ_CHARACTER_MAP
] = 0x00;
579 par
->seq
[VGA_SEQ_MEMORY_MODE
] = 0x06;
581 par
->gdc
[VGA_GFX_SR_VALUE
] = 0x00;
582 par
->gdc
[VGA_GFX_SR_ENABLE
] = 0x00;
583 par
->gdc
[VGA_GFX_COMPARE_VALUE
] = 0x00;
584 par
->gdc
[VGA_GFX_DATA_ROTATE
] = 0x00;
585 par
->gdc
[VGA_GFX_PLANE_READ
] = 0;
586 par
->gdc
[VGA_GFX_MODE
] = 0x02;
587 par
->gdc
[VGA_GFX_MISC
] = 0x05;
588 par
->gdc
[VGA_GFX_COMPARE_MASK
] = 0x0F;
589 par
->gdc
[VGA_GFX_BIT_MASK
] = 0xFF;
591 base
= (yoffset
* vxres
+ (xoffset
& ~7)) >> 2;
594 par
->crtc
[VGA_CRTC_OFFSET
] = vxres
>> 3;
595 par
->ext_offset
= vxres
>> 11;
596 par
->pixelpipe_cfg1
= DISPLAY_8BPP_MODE
;
597 par
->bitblt_cntl
= COLEXP_8BPP
;
599 case 15: /* 0rrrrrgg gggbbbbb */
600 case 16: /* rrrrrggg gggbbbbb */
601 par
->pixelpipe_cfg1
= (var
->green
.length
== 6) ?
602 DISPLAY_16BPP_MODE
: DISPLAY_15BPP_MODE
;
603 par
->crtc
[VGA_CRTC_OFFSET
] = vxres
>> 2;
604 par
->ext_offset
= vxres
>> 10;
605 par
->bitblt_cntl
= COLEXP_16BPP
;
609 par
->crtc
[VGA_CRTC_OFFSET
] = (vxres
* 3) >> 3;
610 par
->ext_offset
= (vxres
* 3) >> 11;
611 par
->pixelpipe_cfg1
= DISPLAY_24BPP_MODE
;
612 par
->bitblt_cntl
= COLEXP_24BPP
;
613 base
&= 0xFFFFFFFE; /* ...ignore the last bit. */
617 par
->crtc
[VGA_CRTC_OFFSET
] = vxres
>> 1;
618 par
->ext_offset
= vxres
>> 9;
619 par
->pixelpipe_cfg1
= DISPLAY_32BPP_MODE
;
620 par
->bitblt_cntl
= COLEXP_RESERVED
; /* Unimplemented on i740 */
625 par
->crtc
[VGA_CRTC_START_LO
] = base
& 0x000000FF;
626 par
->crtc
[VGA_CRTC_START_HI
] = (base
& 0x0000FF00) >> 8;
627 par
->ext_start_addr
=
628 ((base
& 0x003F0000) >> 16) | EXT_START_ADDR_ENABLE
;
629 par
->ext_start_addr_hi
= (base
& 0x3FC00000) >> 22;
631 par
->pixelpipe_cfg0
= DAC_8_BIT
;
633 par
->pixelpipe_cfg2
= DISPLAY_GAMMA_ENABLE
| OVERLAY_GAMMA_ENABLE
;
634 par
->io_cntl
= EXTENDED_CRTC_CNTL
;
635 par
->address_mapping
= LINEAR_MODE_ENABLE
| PAGE_MAPPING_ENABLE
;
636 par
->display_cntl
= HIRES_MODE
;
638 /* Set the MCLK freq */
639 par
->pll_cntl
= PLL_MEMCLK_100000KHZ
; /* 100 MHz -- use as default */
641 /* Calculate the extended CRTC regs */
642 par
->ext_vert_total
= (ytotal
- 2) >> 8;
643 par
->ext_vert_disp_end
= (yres
- 1) >> 8;
644 par
->ext_vert_sync_start
= (yres
+ lower
) >> 8;
645 par
->ext_vert_blank_start
= (yres
+ lower
) >> 8;
646 par
->ext_horiz_total
= ((xtotal
>> 3) - 5) >> 8;
647 par
->ext_horiz_blank
= (((xres
+ right
) >> 3) & 0x40) >> 6;
649 par
->interlace_cntl
= INTERLACE_DISABLE
;
651 /* Set the overscan color to 0. (NOTE: This only affects >8bpp mode) */
652 par
->atc
[VGA_ATC_OVERSCAN
] = 0;
654 /* Calculate VCLK that most closely matches the requested dot clock */
655 i740_calc_vclk((((u32
)1e9
) / var
->pixclock
) * (u32
)(1e3
), par
);
657 /* Since we program the clocks ourselves, always use VCLK2. */
660 /* Calculate the FIFO Watermark and Burst Length. */
661 par
->lmi_fifo_watermark
=
662 i740_calc_fifo(par
, 1000000 / var
->pixclock
, bpp
);
667 static int i740fb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
669 switch (var
->bits_per_pixel
) {
671 var
->red
.offset
= var
->green
.offset
= var
->blue
.offset
= 0;
672 var
->red
.length
= var
->green
.length
= var
->blue
.length
= 8;
675 switch (var
->green
.length
) {
678 var
->red
.offset
= 10;
679 var
->green
.offset
= 5;
680 var
->blue
.offset
= 0;
682 var
->green
.length
= 5;
683 var
->blue
.length
= 5;
686 var
->red
.offset
= 11;
687 var
->green
.offset
= 5;
688 var
->blue
.offset
= 0;
689 var
->red
.length
= var
->blue
.length
= 5;
694 var
->red
.offset
= 16;
695 var
->green
.offset
= 8;
696 var
->blue
.offset
= 0;
697 var
->red
.length
= var
->green
.length
= var
->blue
.length
= 8;
700 var
->transp
.offset
= 24;
701 var
->red
.offset
= 16;
702 var
->green
.offset
= 8;
703 var
->blue
.offset
= 0;
704 var
->transp
.length
= 8;
705 var
->red
.length
= var
->green
.length
= var
->blue
.length
= 8;
711 if (var
->xres
> var
->xres_virtual
)
712 var
->xres_virtual
= var
->xres
;
714 if (var
->yres
> var
->yres_virtual
)
715 var
->yres_virtual
= var
->yres
;
717 if (info
->monspecs
.hfmax
&& info
->monspecs
.vfmax
&&
718 info
->monspecs
.dclkmax
&& fb_validate_mode(var
, info
) < 0)
724 static void vga_protect(struct i740fb_par
*par
)
726 /* disable the display */
727 i740outreg_mask(par
, VGA_SEQ_I
, VGA_SEQ_CLOCK_MODE
, 0x20, 0x20);
730 i740outb(par
, VGA_ATT_W
, 0x00); /* enable palette access */
733 static void vga_unprotect(struct i740fb_par
*par
)
735 /* reenable display */
736 i740outreg_mask(par
, VGA_SEQ_I
, VGA_SEQ_CLOCK_MODE
, 0, 0x20);
739 i740outb(par
, VGA_ATT_W
, 0x20); /* disable palette access */
742 static int i740fb_set_par(struct fb_info
*info
)
744 struct i740fb_par
*par
= info
->par
;
748 i
= i740fb_decode_var(&info
->var
, par
, info
);
752 memset(info
->screen_base
, 0, info
->screen_size
);
756 i740outreg(par
, XRX
, DRAM_EXT_CNTL
, DRAM_REFRESH_DISABLE
);
760 i740outreg(par
, XRX
, VCLK2_VCO_M
, par
->video_clk2_m
);
761 i740outreg(par
, XRX
, VCLK2_VCO_N
, par
->video_clk2_n
);
762 i740outreg(par
, XRX
, VCLK2_VCO_MN_MSBS
, par
->video_clk2_mn_msbs
);
763 i740outreg(par
, XRX
, VCLK2_VCO_DIV_SEL
, par
->video_clk2_div_sel
);
765 i740outreg_mask(par
, XRX
, PIXPIPE_CONFIG_0
,
766 par
->pixelpipe_cfg0
& DAC_8_BIT
, 0x80);
769 i740outb(par
, 0x3C0, 0x00);
771 /* update misc output register */
772 i740outb(par
, VGA_MIS_W
, par
->misc
| 0x01);
774 /* synchronous reset on */
775 i740outreg(par
, VGA_SEQ_I
, VGA_SEQ_RESET
, 0x01);
776 /* write sequencer registers */
777 i740outreg(par
, VGA_SEQ_I
, VGA_SEQ_CLOCK_MODE
,
778 par
->seq
[VGA_SEQ_CLOCK_MODE
] | 0x20);
779 for (i
= 2; i
< VGA_SEQ_C
; i
++)
780 i740outreg(par
, VGA_SEQ_I
, i
, par
->seq
[i
]);
782 /* synchronous reset off */
783 i740outreg(par
, VGA_SEQ_I
, VGA_SEQ_RESET
, 0x03);
785 /* deprotect CRT registers 0-7 */
786 i740outreg(par
, VGA_CRT_IC
, VGA_CRTC_V_SYNC_END
,
787 par
->crtc
[VGA_CRTC_V_SYNC_END
]);
789 /* write CRT registers */
790 for (i
= 0; i
< VGA_CRT_C
; i
++)
791 i740outreg(par
, VGA_CRT_IC
, i
, par
->crtc
[i
]);
793 /* write graphics controller registers */
794 for (i
= 0; i
< VGA_GFX_C
; i
++)
795 i740outreg(par
, VGA_GFX_I
, i
, par
->gdc
[i
]);
797 /* write attribute controller registers */
798 for (i
= 0; i
< VGA_ATT_C
; i
++) {
799 i740inb(par
, VGA_IS1_RC
); /* reset flip-flop */
800 i740outb(par
, VGA_ATT_IW
, i
);
801 i740outb(par
, VGA_ATT_IW
, par
->atc
[i
]);
804 i740inb(par
, VGA_IS1_RC
);
805 i740outb(par
, VGA_ATT_IW
, 0x20);
807 i740outreg(par
, VGA_CRT_IC
, EXT_VERT_TOTAL
, par
->ext_vert_total
);
808 i740outreg(par
, VGA_CRT_IC
, EXT_VERT_DISPLAY
, par
->ext_vert_disp_end
);
809 i740outreg(par
, VGA_CRT_IC
, EXT_VERT_SYNC_START
,
810 par
->ext_vert_sync_start
);
811 i740outreg(par
, VGA_CRT_IC
, EXT_VERT_BLANK_START
,
812 par
->ext_vert_blank_start
);
813 i740outreg(par
, VGA_CRT_IC
, EXT_HORIZ_TOTAL
, par
->ext_horiz_total
);
814 i740outreg(par
, VGA_CRT_IC
, EXT_HORIZ_BLANK
, par
->ext_horiz_blank
);
815 i740outreg(par
, VGA_CRT_IC
, EXT_OFFSET
, par
->ext_offset
);
816 i740outreg(par
, VGA_CRT_IC
, EXT_START_ADDR_HI
, par
->ext_start_addr_hi
);
817 i740outreg(par
, VGA_CRT_IC
, EXT_START_ADDR
, par
->ext_start_addr
);
819 i740outreg_mask(par
, VGA_CRT_IC
, INTERLACE_CNTL
,
820 par
->interlace_cntl
, INTERLACE_ENABLE
);
821 i740outreg_mask(par
, XRX
, ADDRESS_MAPPING
, par
->address_mapping
, 0x1F);
822 i740outreg_mask(par
, XRX
, BITBLT_CNTL
, par
->bitblt_cntl
, COLEXP_MODE
);
823 i740outreg_mask(par
, XRX
, DISPLAY_CNTL
,
824 par
->display_cntl
, VGA_WRAP_MODE
| GUI_MODE
);
825 i740outreg_mask(par
, XRX
, PIXPIPE_CONFIG_0
, par
->pixelpipe_cfg0
, 0x9B);
826 i740outreg_mask(par
, XRX
, PIXPIPE_CONFIG_2
, par
->pixelpipe_cfg2
, 0x0C);
828 i740outreg(par
, XRX
, PLL_CNTL
, par
->pll_cntl
);
830 i740outreg_mask(par
, XRX
, PIXPIPE_CONFIG_1
,
831 par
->pixelpipe_cfg1
, DISPLAY_COLOR_MODE
);
833 itemp
= readl(par
->regs
+ FWATER_BLC
);
834 itemp
&= ~(LMI_BURST_LENGTH
| LMI_FIFO_WATERMARK
);
835 itemp
|= par
->lmi_fifo_watermark
;
836 writel(itemp
, par
->regs
+ FWATER_BLC
);
838 i740outreg(par
, XRX
, DRAM_EXT_CNTL
, DRAM_REFRESH_60HZ
);
840 i740outreg_mask(par
, MRX
, COL_KEY_CNTL_1
, 0, BLANK_DISP_OVERLAY
);
841 i740outreg_mask(par
, XRX
, IO_CTNL
,
842 par
->io_cntl
, EXTENDED_ATTR_CNTL
| EXTENDED_CRTC_CNTL
);
844 if (par
->pixelpipe_cfg1
!= DISPLAY_8BPP_MODE
) {
845 i740outb(par
, VGA_PEL_MSK
, 0xFF);
846 i740outb(par
, VGA_PEL_IW
, 0x00);
847 for (i
= 0; i
< 256; i
++) {
848 itemp
= (par
->pixelpipe_cfg0
& DAC_8_BIT
) ? i
: i
>> 2;
849 i740outb(par
, VGA_PEL_D
, itemp
);
850 i740outb(par
, VGA_PEL_D
, itemp
);
851 i740outb(par
, VGA_PEL_D
, itemp
);
855 /* Wait for screen to stabilize. */
859 info
->fix
.line_length
=
860 info
->var
.xres_virtual
* info
->var
.bits_per_pixel
/ 8;
861 if (info
->var
.bits_per_pixel
== 8)
862 info
->fix
.visual
= FB_VISUAL_PSEUDOCOLOR
;
864 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
869 static int i740fb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
870 unsigned blue
, unsigned transp
,
871 struct fb_info
*info
)
875 dev_dbg(info
->device
, "setcolreg: regno: %i, red=%d, green=%d, blue=%d, transp=%d, bpp=%d\n",
876 regno
, red
, green
, blue
, transp
, info
->var
.bits_per_pixel
);
878 switch (info
->fix
.visual
) {
879 case FB_VISUAL_PSEUDOCOLOR
:
882 i740outb(info
->par
, VGA_PEL_IW
, regno
);
883 i740outb(info
->par
, VGA_PEL_D
, red
>> 8);
884 i740outb(info
->par
, VGA_PEL_D
, green
>> 8);
885 i740outb(info
->par
, VGA_PEL_D
, blue
>> 8);
887 case FB_VISUAL_TRUECOLOR
:
890 r
= (red
>> (16 - info
->var
.red
.length
))
891 << info
->var
.red
.offset
;
892 b
= (blue
>> (16 - info
->var
.blue
.length
))
893 << info
->var
.blue
.offset
;
894 g
= (green
>> (16 - info
->var
.green
.length
))
895 << info
->var
.green
.offset
;
896 ((u32
*) info
->pseudo_palette
)[regno
] = r
| g
| b
;
905 static int i740fb_pan_display(struct fb_var_screeninfo
*var
,
906 struct fb_info
*info
)
908 struct i740fb_par
*par
= info
->par
;
909 u32 base
= (var
->yoffset
* info
->var
.xres_virtual
910 + (var
->xoffset
& ~7)) >> 2;
912 dev_dbg(info
->device
, "pan_display: xoffset: %i yoffset: %i base: %i\n",
913 var
->xoffset
, var
->yoffset
, base
);
915 switch (info
->var
.bits_per_pixel
) {
924 * The last bit does not seem to have any effect on the start
925 * address register in 24bpp mode, so...
927 base
&= 0xFFFFFFFE; /* ...ignore the last bit. */
935 par
->crtc
[VGA_CRTC_START_LO
] = base
& 0x000000FF;
936 par
->crtc
[VGA_CRTC_START_HI
] = (base
& 0x0000FF00) >> 8;
937 par
->ext_start_addr_hi
= (base
& 0x3FC00000) >> 22;
938 par
->ext_start_addr
=
939 ((base
& 0x003F0000) >> 16) | EXT_START_ADDR_ENABLE
;
941 i740outreg(par
, VGA_CRT_IC
, VGA_CRTC_START_LO
, base
& 0x000000FF);
942 i740outreg(par
, VGA_CRT_IC
, VGA_CRTC_START_HI
,
943 (base
& 0x0000FF00) >> 8);
944 i740outreg(par
, VGA_CRT_IC
, EXT_START_ADDR_HI
,
945 (base
& 0x3FC00000) >> 22);
946 i740outreg(par
, VGA_CRT_IC
, EXT_START_ADDR
,
947 ((base
& 0x003F0000) >> 16) | EXT_START_ADDR_ENABLE
);
952 static int i740fb_blank(int blank_mode
, struct fb_info
*info
)
954 struct i740fb_par
*par
= info
->par
;
959 switch (blank_mode
) {
960 case FB_BLANK_UNBLANK
:
961 case FB_BLANK_NORMAL
:
963 DPMSSyncSelect
= HSYNC_ON
| VSYNC_ON
;
965 case FB_BLANK_VSYNC_SUSPEND
:
967 DPMSSyncSelect
= HSYNC_ON
| VSYNC_OFF
;
969 case FB_BLANK_HSYNC_SUSPEND
:
971 DPMSSyncSelect
= HSYNC_OFF
| VSYNC_ON
;
973 case FB_BLANK_POWERDOWN
:
975 DPMSSyncSelect
= HSYNC_OFF
| VSYNC_OFF
;
980 /* Turn the screen on/off */
981 i740outb(par
, SRX
, 0x01);
982 SEQ01
|= i740inb(par
, SRX
+ 1) & ~0x20;
983 i740outb(par
, SRX
, 0x01);
984 i740outb(par
, SRX
+ 1, SEQ01
);
986 /* Set the DPMS mode */
987 i740outreg(par
, XRX
, DPMS_SYNC_SELECT
, DPMSSyncSelect
);
989 /* Let fbcon do a soft blank for us */
990 return (blank_mode
== FB_BLANK_NORMAL
) ? 1 : 0;
993 static struct fb_ops i740fb_ops
= {
994 .owner
= THIS_MODULE
,
995 .fb_open
= i740fb_open
,
996 .fb_release
= i740fb_release
,
997 .fb_check_var
= i740fb_check_var
,
998 .fb_set_par
= i740fb_set_par
,
999 .fb_setcolreg
= i740fb_setcolreg
,
1000 .fb_blank
= i740fb_blank
,
1001 .fb_pan_display
= i740fb_pan_display
,
1002 .fb_fillrect
= cfb_fillrect
,
1003 .fb_copyarea
= cfb_copyarea
,
1004 .fb_imageblit
= cfb_imageblit
,
1007 /* ------------------------------------------------------------------------- */
1009 static int i740fb_probe(struct pci_dev
*dev
, const struct pci_device_id
*ent
)
1011 struct fb_info
*info
;
1012 struct i740fb_par
*par
;
1017 info
= framebuffer_alloc(sizeof(struct i740fb_par
), &(dev
->dev
));
1019 dev_err(&(dev
->dev
), "cannot allocate framebuffer\n");
1024 mutex_init(&par
->open_lock
);
1026 info
->var
.activate
= FB_ACTIVATE_NOW
;
1027 info
->var
.bits_per_pixel
= 8;
1028 info
->fbops
= &i740fb_ops
;
1029 info
->pseudo_palette
= par
->pseudo_palette
;
1031 ret
= pci_enable_device(dev
);
1033 dev_err(info
->device
, "cannot enable PCI device\n");
1034 goto err_enable_device
;
1037 ret
= pci_request_regions(dev
, info
->fix
.id
);
1039 dev_err(info
->device
, "error requesting regions\n");
1040 goto err_request_regions
;
1043 info
->screen_base
= pci_ioremap_bar(dev
, 0);
1044 if (!info
->screen_base
) {
1045 dev_err(info
->device
, "error remapping base\n");
1050 par
->regs
= pci_ioremap_bar(dev
, 1);
1052 dev_err(info
->device
, "error remapping MMIO\n");
1057 /* detect memory size */
1058 if ((i740inreg(par
, XRX
, DRAM_ROW_TYPE
) & DRAM_ROW_1
)
1059 == DRAM_ROW_1_SDRAM
)
1060 i740outb(par
, XRX
, DRAM_ROW_BNDRY_1
);
1062 i740outb(par
, XRX
, DRAM_ROW_BNDRY_0
);
1063 info
->screen_size
= i740inb(par
, XRX
+ 1) * 1024 * 1024;
1064 /* detect memory type */
1065 tmp
= i740inreg(par
, XRX
, DRAM_ROW_CNTL_LO
);
1066 par
->has_sgram
= !((tmp
& DRAM_RAS_TIMING
) ||
1067 (tmp
& DRAM_RAS_PRECHARGE
));
1069 fb_info(info
, "Intel740 on %s, %ld KB %s\n",
1070 pci_name(dev
), info
->screen_size
>> 10,
1071 par
->has_sgram
? "SGRAM" : "SDRAM");
1073 info
->fix
= i740fb_fix
;
1074 info
->fix
.mmio_start
= pci_resource_start(dev
, 1);
1075 info
->fix
.mmio_len
= pci_resource_len(dev
, 1);
1076 info
->fix
.smem_start
= pci_resource_start(dev
, 0);
1077 info
->fix
.smem_len
= info
->screen_size
;
1078 info
->flags
= FBINFO_DEFAULT
| FBINFO_HWACCEL_YPAN
;
1080 if (i740fb_setup_ddc_bus(info
) == 0) {
1081 par
->ddc_registered
= true;
1082 edid
= fb_ddc_read(&par
->ddc_adapter
);
1084 fb_edid_to_monspecs(edid
, &info
->monspecs
);
1086 if (!info
->monspecs
.modedb
)
1087 dev_err(info
->device
,
1088 "error getting mode database\n");
1090 const struct fb_videomode
*m
;
1092 fb_videomode_to_modelist(
1093 info
->monspecs
.modedb
,
1094 info
->monspecs
.modedb_len
,
1096 m
= fb_find_best_display(&info
->monspecs
,
1099 fb_videomode_to_var(&info
->var
, m
);
1100 /* fill all other info->var's fields */
1101 if (!i740fb_check_var(&info
->var
, info
))
1108 if (!mode_option
&& !found
)
1109 mode_option
= "640x480-8@60";
1112 ret
= fb_find_mode(&info
->var
, info
, mode_option
,
1113 info
->monspecs
.modedb
,
1114 info
->monspecs
.modedb_len
,
1115 NULL
, info
->var
.bits_per_pixel
);
1116 if (!ret
|| ret
== 4) {
1117 dev_err(info
->device
, "mode %s not found\n",
1123 fb_destroy_modedb(info
->monspecs
.modedb
);
1124 info
->monspecs
.modedb
= NULL
;
1126 /* maximize virtual vertical size for fast scrolling */
1127 info
->var
.yres_virtual
= info
->fix
.smem_len
* 8 /
1128 (info
->var
.bits_per_pixel
* info
->var
.xres_virtual
);
1133 ret
= fb_alloc_cmap(&info
->cmap
, 256, 0);
1135 dev_err(info
->device
, "cannot allocate colormap\n");
1136 goto err_alloc_cmap
;
1139 ret
= register_framebuffer(info
);
1141 dev_err(info
->device
, "error registering framebuffer\n");
1142 goto err_reg_framebuffer
;
1145 fb_info(info
, "%s frame buffer device\n", info
->fix
.id
);
1146 pci_set_drvdata(dev
, info
);
1150 par
->mtrr_reg
= mtrr_add(info
->fix
.smem_start
,
1151 info
->fix
.smem_len
, MTRR_TYPE_WRCOMB
, 1);
1156 err_reg_framebuffer
:
1157 fb_dealloc_cmap(&info
->cmap
);
1160 if (par
->ddc_registered
)
1161 i2c_del_adapter(&par
->ddc_adapter
);
1162 pci_iounmap(dev
, par
->regs
);
1164 pci_iounmap(dev
, info
->screen_base
);
1166 pci_release_regions(dev
);
1167 err_request_regions
:
1168 /* pci_disable_device(dev); */
1170 framebuffer_release(info
);
1174 static void i740fb_remove(struct pci_dev
*dev
)
1176 struct fb_info
*info
= pci_get_drvdata(dev
);
1179 struct i740fb_par
*par
= info
->par
;
1182 if (par
->mtrr_reg
>= 0) {
1183 mtrr_del(par
->mtrr_reg
, 0, 0);
1187 unregister_framebuffer(info
);
1188 fb_dealloc_cmap(&info
->cmap
);
1189 if (par
->ddc_registered
)
1190 i2c_del_adapter(&par
->ddc_adapter
);
1191 pci_iounmap(dev
, par
->regs
);
1192 pci_iounmap(dev
, info
->screen_base
);
1193 pci_release_regions(dev
);
1194 /* pci_disable_device(dev); */
1195 framebuffer_release(info
);
1200 static int i740fb_suspend(struct pci_dev
*dev
, pm_message_t state
)
1202 struct fb_info
*info
= pci_get_drvdata(dev
);
1203 struct i740fb_par
*par
= info
->par
;
1205 /* don't disable console during hibernation and wakeup from it */
1206 if (state
.event
== PM_EVENT_FREEZE
|| state
.event
== PM_EVENT_PRETHAW
)
1210 mutex_lock(&(par
->open_lock
));
1212 /* do nothing if framebuffer is not active */
1213 if (par
->ref_count
== 0) {
1214 mutex_unlock(&(par
->open_lock
));
1219 fb_set_suspend(info
, 1);
1221 pci_save_state(dev
);
1222 pci_disable_device(dev
);
1223 pci_set_power_state(dev
, pci_choose_state(dev
, state
));
1225 mutex_unlock(&(par
->open_lock
));
1231 static int i740fb_resume(struct pci_dev
*dev
)
1233 struct fb_info
*info
= pci_get_drvdata(dev
);
1234 struct i740fb_par
*par
= info
->par
;
1237 mutex_lock(&(par
->open_lock
));
1239 if (par
->ref_count
== 0)
1242 pci_set_power_state(dev
, PCI_D0
);
1243 pci_restore_state(dev
);
1244 if (pci_enable_device(dev
))
1247 i740fb_set_par(info
);
1248 fb_set_suspend(info
, 0);
1251 mutex_unlock(&(par
->open_lock
));
1256 #define i740fb_suspend NULL
1257 #define i740fb_resume NULL
1258 #endif /* CONFIG_PM */
1260 #define I740_ID_PCI 0x00d1
1261 #define I740_ID_AGP 0x7800
1263 static const struct pci_device_id i740fb_id_table
[] = {
1264 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, I740_ID_PCI
) },
1265 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, I740_ID_AGP
) },
1268 MODULE_DEVICE_TABLE(pci
, i740fb_id_table
);
1270 static struct pci_driver i740fb_driver
= {
1272 .id_table
= i740fb_id_table
,
1273 .probe
= i740fb_probe
,
1274 .remove
= i740fb_remove
,
1275 .suspend
= i740fb_suspend
,
1276 .resume
= i740fb_resume
,
1280 static int __init
i740fb_setup(char *options
)
1284 if (!options
|| !*options
)
1287 while ((opt
= strsep(&options
, ",")) != NULL
) {
1291 else if (!strncmp(opt
, "mtrr:", 5))
1292 mtrr
= simple_strtoul(opt
+ 5, NULL
, 0);
1302 static int __init
i740fb_init(void)
1305 char *option
= NULL
;
1307 if (fb_get_options("i740fb", &option
))
1309 i740fb_setup(option
);
1312 return pci_register_driver(&i740fb_driver
);
1315 static void __exit
i740fb_exit(void)
1317 pci_unregister_driver(&i740fb_driver
);
1320 module_init(i740fb_init
);
1321 module_exit(i740fb_exit
);
1323 MODULE_AUTHOR("(c) 2011 Ondrej Zary <linux@rainbow-software.org>");
1324 MODULE_LICENSE("GPL");
1325 MODULE_DESCRIPTION("fbdev driver for Intel740");
1327 module_param(mode_option
, charp
, 0444);
1328 MODULE_PARM_DESC(mode_option
, "Default video mode ('640x480-8@60', etc)");
1331 module_param(mtrr
, int, 0444);
1332 MODULE_PARM_DESC(mtrr
, "Enable write-combining with MTRR (1=enable, 0=disable, default=1)");