32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/errno.h>
35 #include <linux/string.h>
37 #include <linux/slab.h>
41 #include <linux/pci.h>
49 #include <asm/pci-bridge.h>
51 #ifdef CONFIG_PMAC_BACKLIGHT
52 #include <asm/machdep.h>
60 #define RIVAFB_VERSION "0.9.5b"
67 #ifdef CONFIG_FB_RIVA_DEBUG
68 #define NVTRACE printk
70 #define NVTRACE if(0) printk
73 #define NVTRACE_ENTER(...) NVTRACE("%s START\n", __func__)
74 #define NVTRACE_LEAVE(...) NVTRACE("%s END\n", __func__)
76 #ifdef CONFIG_FB_RIVA_DEBUG
77 #define assert(expr) \
79 printk( "Assertion failed! %s,%s,%s,line=%d\n",\
80 #expr,__FILE__,__func__,__LINE__); \
87 #define PFX "rivafb: "
90 #define SetBitField(value,from,to) SetBF(to,GetBF(value,from))
91 #define SetBit(n) (1<<(n))
92 #define Set8Bits(value) ((value)&0xff)
103 static int rivafb_blank(
int blank,
struct fb_info *
info);
214 #ifdef CONFIG_PMAC_BACKLIGHT
221 static bool strictmode = 0;
253 static const struct riva_regs reg_template = {
254 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
255 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
256 0x41, 0x01, 0x0F, 0x00, 0x00},
257 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
259 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE3,
260 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
261 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
262 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
263 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
264 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267 {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F,
269 {0x03, 0x01, 0x0F, 0x00, 0x0E},
276 #ifdef CONFIG_FB_RIVA_BACKLIGHT
280 #define MIN_LEVEL 0x158
281 #define MAX_LEVEL 0x534
282 #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
284 static int riva_bl_get_level_brightness(
struct riva_par *par,
306 struct riva_par *par = bl_get_data(bd);
307 U032 tmp_pcrt, tmp_pmc;
314 level = bd->
props.brightness;
320 tmp_pmc |= (1 << 31);
321 tmp_pmc |= riva_bl_get_level_brightness(par, level) << 16;
331 return bd->
props.brightness;
336 .update_status = riva_bl_update_status,
339 static void riva_bl_init(
struct riva_par *par)
349 #ifdef CONFIG_PMAC_BACKLIGHT
350 if (!machine_is(powermac) ||
359 props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
373 bd->
props.brightness = bd->
props.max_brightness;
375 backlight_update_status(bd);
377 printk(
"riva: Backlight initialized (%s)\n", name);
385 static void riva_bl_exit(
struct fb_info *info)
390 printk(
"riva: Backlight unloaded\n");
393 static inline void riva_bl_init(
struct riva_par *par) {}
394 static inline void riva_bl_exit(
struct fb_info *info) {}
403 static inline void CRTCout(
struct riva_par *par,
unsigned char index,
410 static inline unsigned char CRTCin(
struct riva_par *par,
417 static inline void GRAout(
struct riva_par *par,
unsigned char index,
424 static inline unsigned char GRAin(
struct riva_par *par,
431 static inline void SEQout(
struct riva_par *par,
unsigned char index,
438 static inline unsigned char SEQin(
struct riva_par *par,
445 static inline void ATTRout(
struct riva_par *par,
unsigned char index,
452 static inline unsigned char ATTRin(
struct riva_par *par,
459 static inline void MISCout(
struct riva_par *par,
unsigned char val)
464 static inline unsigned char MISCin(
struct riva_par *par)
472 a[0] = bitrev8(a[0]);
473 a[1] = bitrev8(a[1]);
474 a[2] = bitrev8(a[2]);
475 a[3] = bitrev8(a[3]);
502 static void rivafb_load_cursor_image(
struct riva_par *par,
u8 *data8,
513 for (i = 0; i <
h; i++) {
517 for (j = 0; j < w/2; j++) {
519 #if defined (__BIG_ENDIAN)
520 tmp = (b & (1 << 31)) ? fg << 16 : bg << 16;
522 tmp |= (b & (1 << 31)) ? fg :
bg;
525 tmp = (b & 1) ? fg : bg;
527 tmp |= (b & 1) ? fg << 16 : bg << 16;
557 unsigned char regnum,
unsigned char red,
581 unsigned char regnum,
unsigned char *
red,
582 unsigned char *
green,
unsigned char *
blue)
615 regs->
crtc[i] = CRTCin(par, i);
618 regs->
attr[i] = ATTRin(par, i);
621 regs->
gra[i] = GRAin(par, i);
624 regs->
seq[i] = SEQin(par, i);
648 CRTCout(par, 0x11, 0x00);
662 CRTCout(par, i, regs->
crtc[i]);
667 ATTRout(par, i, regs->
attr[i]);
670 GRAout(par, i, regs->
gra[i]);
673 SEQout(par, i, regs->
seq[i]);
687 static int riva_load_video_mode(
struct fb_info *info)
689 int bpp,
width, hDisplaySize, hDisplay, hStart,
690 hEnd, hTotal,
height, vDisplay, vStart, vEnd, vTotal, dotClock;
691 int hBlankStart, hBlankEnd, vBlankStart, vBlankEnd;
700 bpp = info->
var.bits_per_pixel;
701 if (bpp == 16 && info->
var.green.length == 5)
703 width = info->
var.xres_virtual;
704 hDisplaySize = info->
var.xres;
705 hDisplay = (hDisplaySize / 8) - 1;
706 hStart = (hDisplaySize + info->
var.right_margin) / 8 - 1;
707 hEnd = (hDisplaySize + info->
var.right_margin +
708 info->
var.hsync_len) / 8 - 1;
709 hTotal = (hDisplaySize + info->
var.right_margin +
710 info->
var.hsync_len + info->
var.left_margin) / 8 - 5;
711 hBlankStart = hDisplay;
712 hBlankEnd = hTotal + 4;
714 height = info->
var.yres_virtual;
715 vDisplay = info->
var.yres - 1;
716 vStart = info->
var.yres + info->
var.lower_margin - 1;
717 vEnd = info->
var.yres + info->
var.lower_margin +
718 info->
var.vsync_len - 1;
719 vTotal = info->
var.yres + info->
var.lower_margin +
720 info->
var.vsync_len + info->
var.upper_margin + 2;
721 vBlankStart = vDisplay;
722 vBlankEnd = vTotal + 1;
723 dotClock = 1000000000 / info->
var.pixclock;
733 vBlankStart = vStart;
736 hBlankEnd = hTotal + 4;
739 newmode.crtc[0x0] =
Set8Bits (hTotal);
740 newmode.crtc[0x1] =
Set8Bits (hDisplay);
741 newmode.crtc[0x2] =
Set8Bits (hBlankStart);
743 newmode.crtc[0x4] =
Set8Bits (hStart);
744 newmode.crtc[0x5] =
SetBitField (hBlankEnd, 5: 5, 7:7)
746 newmode.crtc[0x6] =
SetBitField (vTotal, 7: 0, 7:0);
747 newmode.crtc[0x7] =
SetBitField (vTotal, 8: 8, 0:0)
755 newmode.crtc[0x9] =
SetBitField (vBlankStart, 9: 9, 5:5)
757 newmode.crtc[0x10] =
Set8Bits (vStart);
760 newmode.crtc[0x12] =
Set8Bits (vDisplay);
761 newmode.crtc[0x13] = (width / 8) * ((bpp + 1) / 8);
762 newmode.crtc[0x15] =
Set8Bits (vBlankStart);
763 newmode.crtc[0x16] =
Set8Bits (vBlankEnd);
765 newmode.ext.screen =
SetBitField(hBlankEnd,6:6,4:4)
780 int tmp = (hTotal >> 1) & ~1;
781 newmode.ext.interlace =
Set8Bits(tmp);
784 newmode.ext.interlace = 0xff;
790 newmode.misc_output &= ~0x40;
792 newmode.misc_output |= 0x40;
794 newmode.misc_output &= ~0x80;
796 newmode.misc_output |= 0x80;
799 hDisplaySize, height, dotClock);
806 newmode.ext.pixel |= (1 << 7);
807 newmode.ext.scale |= (1 << 8);
814 newmode.ext.crtcOwner = 3;
815 newmode.ext.pllsel |= 0x20000800;
816 newmode.ext.vpll2 = newmode.ext.vpll;
822 newmode.ext.crtcOwner = 0;
826 newmode.ext.pixel |= (1 << 7);
827 newmode.ext.scale |= (1 << 8);
829 newmode.ext.cursorConfig = 0x02000100;
879 static int rivafb_do_maximize(
struct fb_info *info,
899 "using maximum available virtual resolution\n");
900 for (i = 0; modes[
i].xres != -1; i++) {
901 if (modes[i].xres * nom / den * modes[i].yres <
905 if (modes[i].xres == -1) {
907 "could not find a virtual resolution that fits into video memory!!\n");
908 NVTRACE(
"EXIT - EINVAL error\n");
915 "virtual resolution set to maximum of %dx%d\n",
921 "setting virtual X resolution to %d\n", var->
xres_virtual);
927 "setting virtual Y resolution to %d\n", var->
yres_virtual);
932 "mode %dx%dx%d rejected...resolution too high to fit into video memory!\n",
934 NVTRACE(
"EXIT - EINVAL error\n");
941 "virtual X resolution (%d) is too high, lowering to %d\n",
948 "virtual X resolution (%d) is smaller than real\n", var->
xres_virtual);
954 "virtual Y resolution (%d) is smaller than real\n", var->
yres_virtual);
966 riva_set_pattern(
struct riva_par *par,
int clr0,
int clr1,
int pat0,
int pat1)
976 static inline void wait_for_idle(
struct riva_par *par)
985 riva_set_rop_solid(
struct riva_par *par,
int rop)
987 riva_set_pattern(par, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
993 static void riva_setup_accel(
struct fb_info *info)
1000 (info->
var.xres_virtual & 0xffff) |
1001 (info->
var.yres_virtual << 16));
1002 riva_set_rop_solid(par, 0xcc);
1023 switch (var->
green.length) {
1046 static int rivafb_open(
struct fb_info *info,
int user)
1062 CRTCout(par, 0x11, 0xFF);
1073 static int rivafb_release(
struct fb_info *info,
int user)
1108 var->
red.offset = var->
green.offset = var->
blue.offset = 0;
1109 var->
red.length = var->
green.length = var->
blue.length = 8;
1114 var->
green.length = 5;
1120 var->
green.length = 5;
1121 if (var->
green.length == 5) {
1123 var->
red.offset = 10;
1124 var->
green.offset = 5;
1125 var->
blue.offset = 0;
1126 var->
red.length = 5;
1127 var->
green.length = 5;
1128 var->
blue.length = 5;
1131 var->
red.offset = 11;
1132 var->
green.offset = 5;
1133 var->
blue.offset = 0;
1134 var->
red.length = 5;
1135 var->
green.length = 6;
1136 var->
blue.length = 5;
1142 var->
red.length = var->
green.length = var->
blue.length = 8;
1144 var->
red.offset = 16;
1145 var->
green.offset = 8;
1146 var->
blue.offset = 0;
1152 "mode %dx%dx%d rejected...color depth not supported.\n",
1154 NVTRACE(
"EXIT, returning -EINVAL\n");
1165 if (!mode_valid && info->
monspecs.gtf) {
1173 riva_update_var(var, mode);
1178 if (!mode_valid && info->
monspecs.modedb_len)
1185 if (rivafb_do_maximize(info, var, nom, den) < 0)
1200 var->
red.msb_right =
1201 var->
green.msb_right =
1202 var->
blue.msb_right =
1208 static int rivafb_set_par(
struct fb_info *info)
1215 CRTCout(par, 0x11, 0xFF);
1217 rc = riva_load_video_mode(info);
1221 riva_setup_accel(info);
1224 info->
fix.line_length = (info->
var.xres_virtual * (info->
var.bits_per_pixel >> 3));
1225 info->
fix.visual = (info->
var.bits_per_pixel == 8) ?
1229 info->
pixmap.scan_align = 1;
1231 info->
pixmap.scan_align = 4;
1264 static int rivafb_blank(
int blank,
struct fb_info *info)
1267 unsigned char tmp, vesa;
1269 tmp = SEQin(par, 0x01) & ~0x20;
1270 vesa = CRTCin(par, 0x1a) & ~0xc0;
1292 SEQout(par, 0x01, tmp);
1293 CRTCout(par, 0x1a, vesa);
1319 static int rivafb_setcolreg(
unsigned regno,
unsigned red,
unsigned green,
1327 if (regno >= riva_get_cmap_len(&info->
var))
1330 if (info->
var.grayscale) {
1332 red = green = blue =
1333 (red * 77 + green * 151 + blue * 28) >> 8;
1338 (regno << info->
var.red.offset) |
1339 (regno << info->
var.green.offset) |
1340 (regno << info->
var.blue.offset);
1346 switch (info->
var.bits_per_pixel) {
1348 par->
palette[regno] = ((red & 0xf800) >> 1) |
1349 ((green & 0xf800) >> 6) |
1350 ((blue & 0xf800) >> 11);
1353 par->
palette[regno] = ((red & 0xff00) << 8) |
1354 ((green & 0xff00)) |
1355 ((blue & 0xff00) >> 8);
1361 switch (info->
var.bits_per_pixel) {
1364 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1367 if (info->
var.green.length == 5) {
1368 for (i = 0; i < 8; i++) {
1369 riva_wclut(chip, regno*8+i, red >> 8,
1370 green >> 8, blue >> 8);
1376 for (i = 0; i < 8; i++) {
1377 riva_wclut(chip, regno*8+i,
1378 red >> 8, green >> 8,
1382 riva_rclut(chip, regno*4, &r, &g, &b);
1383 for (i = 0; i < 4; i++)
1384 riva_wclut(chip, regno*4+i, r,
1389 riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
1420 if (info->
var.bits_per_pixel == 8)
1421 color = rect->
color;
1429 switch (rect->
rop) {
1439 riva_set_rop_solid(par, rop);
1446 (rect->
dx << 16) | rect->
dy);
1451 riva_set_rop_solid(par, 0xcc);
1477 (region->
sy << 16) | region->
sx);
1479 (region->
dy << 16) | region->
dx);
1486 static inline void convert_bgcolor_16(
u32 *col)
1488 *col = ((*col & 0x0000F800) << 8)
1489 | ((*col & 0x00007E0) << 5)
1490 | ((*col & 0x0000001F) << 3)
1511 static void rivafb_imageblit(
struct fb_info *info,
1525 switch (info->
var.bits_per_pixel) {
1539 if (info->
var.green.length == 6)
1540 convert_bgcolor_16(&bgx);
1546 (image->
dy << 16) | (image->
dx & 0xFFFF));
1548 (((image->
dy + image->
height) << 16) |
1549 ((image->
dx + image->
width) & 0xffff)));
1553 (image->
height << 16) | ((image->
width + 31) & ~31));
1555 (image->
height << 16) | ((image->
width + 31) & ~31));
1557 (image->
dy << 16) | (image->
dx & 0xFFFF));
1561 width = (image->
width + 31)/32;
1563 while (
size >= 16) {
1565 for (i = 0; i < 16; i++) {
1566 tmp = *((
u32 *)cdat);
1567 cdat = (
u8 *)((
u32 *)cdat + 1);
1575 for (i = 0; i <
size; i++) {
1576 tmp = *((
u32 *) cdat);
1577 cdat = (
u8 *)((
u32 *)cdat + 1);
1601 int i,
set = cursor->
set;
1620 yy = cursor->
image.dy - info->
var.yoffset;
1621 xx = cursor->
image.dx - info->
var.xoffset;
1630 u32 bg_idx = cursor->
image.bg_color;
1631 u32 fg_idx = cursor->
image.fg_color;
1632 u32 s_pitch = (cursor->
image.width+7) >> 3;
1641 switch (cursor->
rop) {
1643 for (i = 0; i < s_pitch * cursor->
image.height; i++)
1644 src[i] = dat[i] ^
msk[i];
1648 for (i = 0; i < s_pitch * cursor->
image.height; i++)
1649 src[i] = dat[i] &
msk[i];
1654 cursor->
image.height);
1656 bg = ((info->
cmap.red[bg_idx] & 0xf8) << 7) |
1657 ((info->
cmap.green[bg_idx] & 0xf8) << 2) |
1658 ((info->
cmap.blue[bg_idx] & 0xf8) >> 3) |
1661 fg = ((info->
cmap.red[fg_idx] & 0xf8) << 7) |
1662 ((info->
cmap.green[fg_idx] & 0xf8) << 2) |
1663 ((info->
cmap.blue[fg_idx] & 0xf8) >> 3) |
1668 rivafb_load_cursor_image(par, data, bg, fg,
1669 cursor->
image.width,
1670 cursor->
image.height);
1681 static int rivafb_sync(
struct fb_info *info)
1696 static struct fb_ops riva_fb_ops = {
1698 .fb_open = rivafb_open,
1699 .fb_release = rivafb_release,
1700 .fb_check_var = rivafb_check_var,
1701 .fb_set_par = rivafb_set_par,
1702 .fb_setcolreg = rivafb_setcolreg,
1703 .fb_pan_display = rivafb_pan_display,
1704 .fb_blank = rivafb_blank,
1705 .fb_fillrect = rivafb_fillrect,
1706 .fb_copyarea = rivafb_copyarea,
1707 .fb_imageblit = rivafb_imageblit,
1708 .fb_cursor = rivafb_cursor,
1709 .fb_sync = rivafb_sync,
1714 unsigned int cmap_len;
1731 info->
var = rivafb_default_var;
1732 info->
fix.visual = (info->
var.bits_per_pixel == 8) ?
1737 cmap_len = riva_get_cmap_len(&info->
var);
1740 info->
pixmap.size = 8 * 1024;
1741 info->
pixmap.buf_align = 4;
1742 info->
pixmap.access_align = 32;
1744 info->
var.yres_virtual = -1;
1746 return (rivafb_check_var(&info->
var, info));
1749 #ifdef CONFIG_PPC_OF
1754 const unsigned char *pedid =
NULL;
1755 const unsigned char *disptype =
NULL;
1756 static char *propnames[] = {
1757 "DFP,EDID",
"LCD,EDID",
"EDID",
"EDID1",
"EDID,B",
"EDID,A",
NULL };
1761 dp = pci_device_to_OF_node(pd);
1764 if (disptype ==
NULL)
1766 if (
strncmp(disptype,
"LCD", 3) != 0)
1768 for (i = 0; propnames[
i] !=
NULL; ++
i) {
1770 if (pedid !=
NULL) {
1771 par->
EDID = (
unsigned char *)pedid;
1782 #if defined(CONFIG_FB_RIVA_I2C) && !defined(CONFIG_PPC_OF)
1791 for (i = 0; i < 3; i++) {
1792 if (!par->
chan[i].par)
1796 printk(
PFX "Found EDID Block from BUS %i\n", i);
1802 return (par->
EDID) ? 1 : 0;
1820 modedb = specs->
modedb[0];
1833 riva_update_var(var, &modedb);
1842 #ifdef CONFIG_PPC_OF
1843 if (!riva_get_EDID_OF(info, pdev))
1844 printk(
PFX "could not retrieve EDID from OF\n");
1845 #elif defined(CONFIG_FB_RIVA_I2C)
1846 if (!riva_get_EDID_i2c(info))
1847 printk(
PFX "could not retrieve EDID from DDC/I2C\n");
1861 riva_update_default_var(var, info);
1878 switch (pd->
device & 0x0ff0) {
1928 default_par = info->
par;
1929 default_par->
pdev = pd;
1934 goto err_framebuffer_release;
1940 goto err_free_pixmap;
1946 goto err_disable_device;
1958 goto err_release_region;
1993 goto err_release_region;
2006 goto err_iounmap_ctrl_base;
2033 goto err_iounmap_pramin;
2041 if (default_par->mtrr.vram < 0) {
2044 default_par->mtrr.vram_valid = 1;
2051 info->
fbops = &riva_fb_ops;
2052 info->
fix = rivafb_fix;
2053 riva_get_EDID(info, pd);
2054 riva_get_edidinfo(info);
2056 ret=riva_set_fbinfo(info);
2059 goto err_iounmap_screen_base;
2065 pci_set_drvdata(pd, info);
2068 riva_bl_init(info->
par);
2073 "error registering riva framebuffer\n");
2074 goto err_iounmap_screen_base;
2078 "PCI nVidia %s framebuffer ver %s (%dMB @ 0x%lX)\n",
2081 info->
fix.smem_len / (1024 * 1024),
2082 info->
fix.smem_start);
2087 err_iounmap_screen_base:
2088 #ifdef CONFIG_FB_RIVA_I2C
2095 err_iounmap_ctrl_base:
2102 err_framebuffer_release:
2110 struct fb_info *info = pci_get_drvdata(pd);
2115 #ifdef CONFIG_FB_RIVA_I2C
2125 if (par->mtrr.vram_valid)
2127 info->
fix.smem_len);
2137 pci_set_drvdata(pd,
NULL);
2153 if (!options || !*options)
2156 while ((this_opt =
strsep(&options,
",")) !=
NULL) {
2157 if (!
strncmp(this_opt,
"forceCRTC", 9)) {
2161 if (!*p || !*(++p))
continue;
2163 if (forceCRTC < 0 || forceCRTC > 1)
2165 }
else if (!
strncmp(this_opt,
"flatpanel", 9)) {
2167 }
else if (!
strncmp(this_opt,
"backlight:", 10)) {
2170 }
else if (!
strncmp(this_opt,
"nomtrr", 6)) {
2173 }
else if (!
strncmp(this_opt,
"strictmode", 10)) {
2175 }
else if (!
strncmp(this_opt,
"noaccel", 7)) {
2178 mode_option = this_opt;
2187 .id_table = rivafb_pci_tbl,
2188 .probe = rivafb_probe,
2207 rivafb_setup(option);
2209 return pci_register_driver(&rivafb_driver);
2215 static void __exit rivafb_exit(
void)
2225 MODULE_PARM_DESC(flatpanel,
"Enables experimental flat panel support for some chipsets. (0 or 1=enabled) (default=0)");
2227 MODULE_PARM_DESC(forceCRTC,
"Forces usage of a particular CRTC in case autodetection fails. (0 or 1) (default=autodetect)");
2230 MODULE_PARM_DESC(nomtrr,
"Disables MTRR support (0 or 1=disabled) (default=0)");
2236 MODULE_DESCRIPTION(
"Framebuffer driver for nVidia Riva 128, TNT, TNT2, and the GeForce series");