2 * linux/drivers/video/nvidia/nvidia.c - nVidia fb driver
4 * Copyright 2004 Antonino Daplas <adaplas@pol.net>
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
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
20 #include <linux/init.h>
21 #include <linux/pci.h>
22 #include <linux/console.h>
23 #include <linux/backlight.h>
29 #include <asm/pci-bridge.h>
31 #ifdef CONFIG_BOOTX_TEXT
32 #include <asm/btext.h>
40 #ifdef CONFIG_FB_NVIDIA_DEBUG
41 #define NVTRACE printk
43 #define NVTRACE if (0) printk
46 #define NVTRACE_ENTER(...) NVTRACE("%s START\n", __FUNCTION__)
47 #define NVTRACE_LEAVE(...) NVTRACE("%s END\n", __FUNCTION__)
49 #ifdef CONFIG_FB_NVIDIA_DEBUG
50 #define assert(expr) \
52 printk( "Assertion failed! %s,%s,%s,line=%d\n",\
53 #expr,__FILE__,__FUNCTION__,__LINE__); \
60 #define PFX "nvidiafb: "
62 /* HW cursor parameters */
65 static struct pci_device_id nvidiafb_pci_tbl[] = {
66 {PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
67 PCI_BASE_CLASS_DISPLAY << 16, 0xff0000, 0},
70 MODULE_DEVICE_TABLE(pci, nvidiafb_pci_tbl);
72 /* command line data, set in nvidiafb_setup() */
73 static int flatpanel __devinitdata = -1; /* Autodetect later */
74 static int fpdither __devinitdata = -1;
75 static int forceCRTC __devinitdata = -1;
76 static int hwcur __devinitdata = 0;
77 static int noaccel __devinitdata = 0;
78 static int noscale __devinitdata = 0;
79 static int paneltweak __devinitdata = 0;
80 static int vram __devinitdata = 0;
81 static int bpp __devinitdata = 8;
83 static int nomtrr __devinitdata = 0;
85 #ifdef CONFIG_PMAC_BACKLIGHT
86 static int backlight __devinitdata = 1;
88 static int backlight __devinitdata = 0;
91 static char *mode_option __devinitdata = NULL;
93 static struct fb_fix_screeninfo __devinitdata nvidiafb_fix = {
94 .type = FB_TYPE_PACKED_PIXELS,
99 static struct fb_var_screeninfo __devinitdata nvidiafb_default_var = {
109 .activate = FB_ACTIVATE_NOW,
119 .vmode = FB_VMODE_NONINTERLACED
122 static void nvidiafb_load_cursor_image(struct nvidia_par *par, u8 * data8,
123 u16 bg, u16 fg, u32 w, u32 h)
125 u32 *data = (u32 *) data8;
131 for (i = 0; i < h; i++) {
135 for (j = 0; j < w / 2; j++) {
137 #if defined (__BIG_ENDIAN)
138 tmp = (b & (1 << 31)) ? fg << 16 : bg << 16;
140 tmp |= (b & (1 << 31)) ? fg : bg;
143 tmp = (b & 1) ? fg : bg;
145 tmp |= (b & 1) ? fg << 16 : bg << 16;
148 NV_WR32(&par->CURSOR[k++], 0, tmp);
150 k += (MAX_CURS - w) / 2;
154 static void nvidia_write_clut(struct nvidia_par *par,
155 u8 regnum, u8 red, u8 green, u8 blue)
157 NVWriteDacMask(par, 0xff);
158 NVWriteDacWriteAddr(par, regnum);
159 NVWriteDacData(par, red);
160 NVWriteDacData(par, green);
161 NVWriteDacData(par, blue);
164 static void nvidia_read_clut(struct nvidia_par *par,
165 u8 regnum, u8 * red, u8 * green, u8 * blue)
167 NVWriteDacMask(par, 0xff);
168 NVWriteDacReadAddr(par, regnum);
169 *red = NVReadDacData(par);
170 *green = NVReadDacData(par);
171 *blue = NVReadDacData(par);
174 static int nvidia_panel_tweak(struct nvidia_par *par,
175 struct _riva_hw_state *state)
179 if (par->paneltweak) {
180 tweak = par->paneltweak;
182 /* begin flat panel hacks */
183 /* This is unfortunate, but some chips need this register
184 tweaked or else you get artifacts where adjacent pixels are
185 swapped. There are no hard rules for what to set here so all
186 we can do is experiment and apply hacks. */
188 if(((par->Chipset & 0xffff) == 0x0328) && (state->bpp == 32)) {
189 /* At least one NV34 laptop needs this workaround. */
193 if((par->Chipset & 0xfff0) == 0x0310) {
196 /* end flat panel hacks */
202 static void nvidia_screen_off(struct nvidia_par *par, int on)
208 * Turn off screen and disable sequencer.
210 tmp = NVReadSeq(par, 0x01);
212 NVWriteSeq(par, 0x00, 0x01); /* Synchronous Reset */
213 NVWriteSeq(par, 0x01, tmp | 0x20); /* disable the display */
216 * Reenable sequencer, then turn on screen.
219 tmp = NVReadSeq(par, 0x01);
221 NVWriteSeq(par, 0x01, tmp & ~0x20); /* reenable display */
222 NVWriteSeq(par, 0x00, 0x03); /* End Reset */
226 static void nvidia_save_vga(struct nvidia_par *par,
227 struct _riva_hw_state *state)
232 NVLockUnlock(par, 0);
234 NVUnloadStateExt(par, state);
236 state->misc_output = NVReadMiscOut(par);
238 for (i = 0; i < NUM_CRT_REGS; i++)
239 state->crtc[i] = NVReadCrtc(par, i);
241 for (i = 0; i < NUM_ATC_REGS; i++)
242 state->attr[i] = NVReadAttr(par, i);
244 for (i = 0; i < NUM_GRC_REGS; i++)
245 state->gra[i] = NVReadGr(par, i);
247 for (i = 0; i < NUM_SEQ_REGS; i++)
248 state->seq[i] = NVReadSeq(par, i);
254 static void nvidia_write_regs(struct nvidia_par *par,
255 struct _riva_hw_state *state)
261 NVLoadStateExt(par, state);
263 NVWriteMiscOut(par, state->misc_output);
265 for (i = 1; i < NUM_SEQ_REGS; i++) {
267 printk(" SEQ[%02x] = %08x\n", i, state->seq[i]);
269 NVWriteSeq(par, i, state->seq[i]);
272 /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17] */
273 NVWriteCrtc(par, 0x11, state->crtc[0x11] & ~0x80);
275 for (i = 0; i < NUM_CRT_REGS; i++) {
282 printk("CRTC[%02x] = %08x\n", i, state->crtc[i]);
284 NVWriteCrtc(par, i, state->crtc[i]);
288 for (i = 0; i < NUM_GRC_REGS; i++) {
290 printk(" GRA[%02x] = %08x\n", i, state->gra[i]);
292 NVWriteGr(par, i, state->gra[i]);
295 for (i = 0; i < NUM_ATC_REGS; i++) {
297 printk("ATTR[%02x] = %08x\n", i, state->attr[i]);
299 NVWriteAttr(par, i, state->attr[i]);
305 static int nvidia_calc_regs(struct fb_info *info)
307 struct nvidia_par *par = info->par;
308 struct _riva_hw_state *state = &par->ModeReg;
309 int i, depth = fb_get_color_depth(&info->var, &info->fix);
310 int h_display = info->var.xres / 8 - 1;
311 int h_start = (info->var.xres + info->var.right_margin) / 8 - 1;
312 int h_end = (info->var.xres + info->var.right_margin +
313 info->var.hsync_len) / 8 - 1;
314 int h_total = (info->var.xres + info->var.right_margin +
315 info->var.hsync_len + info->var.left_margin) / 8 - 5;
316 int h_blank_s = h_display;
317 int h_blank_e = h_total + 4;
318 int v_display = info->var.yres - 1;
319 int v_start = info->var.yres + info->var.lower_margin - 1;
320 int v_end = (info->var.yres + info->var.lower_margin +
321 info->var.vsync_len) - 1;
322 int v_total = (info->var.yres + info->var.lower_margin +
323 info->var.vsync_len + info->var.upper_margin) - 2;
324 int v_blank_s = v_display;
325 int v_blank_e = v_total + 1;
328 * Set all CRTC values.
331 if (info->var.vmode & FB_VMODE_INTERLACED)
334 if (par->FlatPanel == 1) {
335 v_start = v_total - 3;
338 h_start = h_total - 5;
340 h_blank_e = h_total + 4;
343 state->crtc[0x0] = Set8Bits(h_total);
344 state->crtc[0x1] = Set8Bits(h_display);
345 state->crtc[0x2] = Set8Bits(h_blank_s);
346 state->crtc[0x3] = SetBitField(h_blank_e, 4: 0, 4:0)
348 state->crtc[0x4] = Set8Bits(h_start);
349 state->crtc[0x5] = SetBitField(h_blank_e, 5: 5, 7:7)
350 | SetBitField(h_end, 4: 0, 4:0);
351 state->crtc[0x6] = SetBitField(v_total, 7: 0, 7:0);
352 state->crtc[0x7] = SetBitField(v_total, 8: 8, 0:0)
353 | SetBitField(v_display, 8: 8, 1:1)
354 | SetBitField(v_start, 8: 8, 2:2)
355 | SetBitField(v_blank_s, 8: 8, 3:3)
357 | SetBitField(v_total, 9: 9, 5:5)
358 | SetBitField(v_display, 9: 9, 6:6)
359 | SetBitField(v_start, 9: 9, 7:7);
360 state->crtc[0x9] = SetBitField(v_blank_s, 9: 9, 5:5)
362 | ((info->var.vmode & FB_VMODE_DOUBLE) ? 0x80 : 0x00);
363 state->crtc[0x10] = Set8Bits(v_start);
364 state->crtc[0x11] = SetBitField(v_end, 3: 0, 3:0) | SetBit(5);
365 state->crtc[0x12] = Set8Bits(v_display);
366 state->crtc[0x13] = ((info->var.xres_virtual / 8) *
367 (info->var.bits_per_pixel / 8));
368 state->crtc[0x15] = Set8Bits(v_blank_s);
369 state->crtc[0x16] = Set8Bits(v_blank_e);
371 state->attr[0x10] = 0x01;
374 state->attr[0x11] = 0x00;
376 state->screen = SetBitField(h_blank_e, 6: 6, 4:4)
377 | SetBitField(v_blank_s, 10: 10, 3:3)
378 | SetBitField(v_start, 10: 10, 2:2)
379 | SetBitField(v_display, 10: 10, 1:1)
380 | SetBitField(v_total, 10: 10, 0:0);
382 state->horiz = SetBitField(h_total, 8: 8, 0:0)
383 | SetBitField(h_display, 8: 8, 1:1)
384 | SetBitField(h_blank_s, 8: 8, 2:2)
385 | SetBitField(h_start, 8: 8, 3:3);
387 state->extra = SetBitField(v_total, 11: 11, 0:0)
388 | SetBitField(v_display, 11: 11, 2:2)
389 | SetBitField(v_start, 11: 11, 4:4)
390 | SetBitField(v_blank_s, 11: 11, 6:6);
392 if (info->var.vmode & FB_VMODE_INTERLACED) {
393 h_total = (h_total >> 1) & ~1;
394 state->interlace = Set8Bits(h_total);
395 state->horiz |= SetBitField(h_total, 8: 8, 4:4);
397 state->interlace = 0xff; /* interlace off */
401 * Calculate the extended registers.
409 if (par->Architecture >= NV_ARCH_10)
410 par->CURSOR = (volatile u32 __iomem *)(info->screen_base +
413 if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
414 state->misc_output &= ~0x40;
416 state->misc_output |= 0x40;
417 if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
418 state->misc_output &= ~0x80;
420 state->misc_output |= 0x80;
422 NVCalcStateExt(par, state, i, info->var.xres_virtual,
423 info->var.xres, info->var.yres_virtual,
424 1000000000 / info->var.pixclock, info->var.vmode);
426 state->scale = NV_RD32(par->PRAMDAC, 0x00000848) & 0xfff000ff;
427 if (par->FlatPanel == 1) {
428 state->pixel |= (1 << 7);
430 if (!par->fpScaler || (par->fpWidth <= info->var.xres)
431 || (par->fpHeight <= info->var.yres)) {
432 state->scale |= (1 << 8);
435 if (!par->crtcSync_read) {
436 state->crtcSync = NV_RD32(par->PRAMDAC, 0x0828);
437 par->crtcSync_read = 1;
440 par->PanelTweak = nvidia_panel_tweak(par, state);
443 state->vpll = state->pll;
444 state->vpll2 = state->pll;
445 state->vpllB = state->pllB;
446 state->vpll2B = state->pllB;
448 VGA_WR08(par->PCIO, 0x03D4, 0x1C);
449 state->fifo = VGA_RD08(par->PCIO, 0x03D5) & ~(1<<5);
451 if (par->CRTCnumber) {
452 state->head = NV_RD32(par->PCRTC0, 0x00000860) & ~0x00001000;
453 state->head2 = NV_RD32(par->PCRTC0, 0x00002860) | 0x00001000;
454 state->crtcOwner = 3;
455 state->pllsel |= 0x20000800;
456 state->vpll = NV_RD32(par->PRAMDAC0, 0x00000508);
457 if (par->twoStagePLL)
458 state->vpllB = NV_RD32(par->PRAMDAC0, 0x00000578);
459 } else if (par->twoHeads) {
460 state->head = NV_RD32(par->PCRTC0, 0x00000860) | 0x00001000;
461 state->head2 = NV_RD32(par->PCRTC0, 0x00002860) & ~0x00001000;
462 state->crtcOwner = 0;
463 state->vpll2 = NV_RD32(par->PRAMDAC0, 0x0520);
464 if (par->twoStagePLL)
465 state->vpll2B = NV_RD32(par->PRAMDAC0, 0x057C);
468 state->cursorConfig = 0x00000100;
470 if (info->var.vmode & FB_VMODE_DOUBLE)
471 state->cursorConfig |= (1 << 4);
473 if (par->alphaCursor) {
474 if ((par->Chipset & 0x0ff0) != 0x0110)
475 state->cursorConfig |= 0x04011000;
477 state->cursorConfig |= 0x14011000;
478 state->general |= (1 << 29);
480 state->cursorConfig |= 0x02000000;
483 if ((par->Chipset & 0x0ff0) == 0x0110) {
484 state->dither = NV_RD32(par->PRAMDAC, 0x0528) &
487 state->dither |= 0x00010000;
489 state->dither = NV_RD32(par->PRAMDAC, 0x083C) & ~1;
497 state->displayV = info->var.xres;
502 static void nvidia_init_vga(struct fb_info *info)
504 struct nvidia_par *par = info->par;
505 struct _riva_hw_state *state = &par->ModeReg;
508 for (i = 0; i < 0x10; i++)
510 state->attr[0x10] = 0x41;
511 state->attr[0x11] = 0xff;
512 state->attr[0x12] = 0x0f;
513 state->attr[0x13] = 0x00;
514 state->attr[0x14] = 0x00;
516 memset(state->crtc, 0x00, NUM_CRT_REGS);
517 state->crtc[0x0a] = 0x20;
518 state->crtc[0x17] = 0xe3;
519 state->crtc[0x18] = 0xff;
520 state->crtc[0x28] = 0x40;
522 memset(state->gra, 0x00, NUM_GRC_REGS);
523 state->gra[0x05] = 0x40;
524 state->gra[0x06] = 0x05;
525 state->gra[0x07] = 0x0f;
526 state->gra[0x08] = 0xff;
528 state->seq[0x00] = 0x03;
529 state->seq[0x01] = 0x01;
530 state->seq[0x02] = 0x0f;
531 state->seq[0x03] = 0x00;
532 state->seq[0x04] = 0x0e;
534 state->misc_output = 0xeb;
537 static int nvidiafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
539 struct nvidia_par *par = info->par;
540 u8 data[MAX_CURS * MAX_CURS / 8];
541 int i, set = cursor->set;
544 if (cursor->image.width > MAX_CURS || cursor->image.height > MAX_CURS)
547 NVShowHideCursor(par, 0);
549 if (par->cursor_reset) {
551 par->cursor_reset = 0;
554 if (set & FB_CUR_SETSIZE)
555 memset_io(par->CURSOR, 0, MAX_CURS * MAX_CURS * 2);
557 if (set & FB_CUR_SETPOS) {
560 yy = cursor->image.dy - info->var.yoffset;
561 xx = cursor->image.dx - info->var.xoffset;
565 NV_WR32(par->PRAMDAC, 0x0000300, temp);
568 if (set & (FB_CUR_SETSHAPE | FB_CUR_SETCMAP | FB_CUR_SETIMAGE)) {
569 u32 bg_idx = cursor->image.bg_color;
570 u32 fg_idx = cursor->image.fg_color;
571 u32 s_pitch = (cursor->image.width + 7) >> 3;
572 u32 d_pitch = MAX_CURS / 8;
573 u8 *dat = (u8 *) cursor->image.data;
574 u8 *msk = (u8 *) cursor->mask;
577 src = kmalloc(s_pitch * cursor->image.height, GFP_ATOMIC);
580 switch (cursor->rop) {
582 for (i = 0; i < s_pitch * cursor->image.height; i++)
583 src[i] = dat[i] ^ msk[i];
587 for (i = 0; i < s_pitch * cursor->image.height; i++)
588 src[i] = dat[i] & msk[i];
592 fb_pad_aligned_buffer(data, d_pitch, src, s_pitch,
593 cursor->image.height);
595 bg = ((info->cmap.red[bg_idx] & 0xf8) << 7) |
596 ((info->cmap.green[bg_idx] & 0xf8) << 2) |
597 ((info->cmap.blue[bg_idx] & 0xf8) >> 3) | 1 << 15;
599 fg = ((info->cmap.red[fg_idx] & 0xf8) << 7) |
600 ((info->cmap.green[fg_idx] & 0xf8) << 2) |
601 ((info->cmap.blue[fg_idx] & 0xf8) >> 3) | 1 << 15;
603 NVLockUnlock(par, 0);
605 nvidiafb_load_cursor_image(par, data, bg, fg,
607 cursor->image.height);
613 NVShowHideCursor(par, 1);
618 static int nvidiafb_set_par(struct fb_info *info)
620 struct nvidia_par *par = info->par;
624 NVLockUnlock(par, 1);
625 if (!par->FlatPanel || !par->twoHeads)
628 if (par->FPDither < 0) {
629 if ((par->Chipset & 0x0ff0) == 0x0110)
630 par->FPDither = !!(NV_RD32(par->PRAMDAC, 0x0528)
633 par->FPDither = !!(NV_RD32(par->PRAMDAC, 0x083C) & 1);
634 printk(KERN_INFO PFX "Flat panel dithering %s\n",
635 par->FPDither ? "enabled" : "disabled");
638 info->fix.visual = (info->var.bits_per_pixel == 8) ?
639 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
641 nvidia_init_vga(info);
642 nvidia_calc_regs(info);
644 NVLockUnlock(par, 0);
646 VGA_WR08(par->PCIO, 0x03D4, 0x44);
647 VGA_WR08(par->PCIO, 0x03D5, par->ModeReg.crtcOwner);
648 NVLockUnlock(par, 0);
651 nvidia_screen_off(par, 1);
653 nvidia_write_regs(par, &par->ModeReg);
654 NVSetStartAddress(par, 0);
656 #if defined (__BIG_ENDIAN)
657 /* turn on LFB swapping */
661 VGA_WR08(par->PCIO, 0x3d4, 0x46);
662 tmp = VGA_RD08(par->PCIO, 0x3d5);
664 VGA_WR08(par->PCIO, 0x3d5, tmp);
668 info->fix.line_length = (info->var.xres_virtual *
669 info->var.bits_per_pixel) >> 3;
670 if (info->var.accel_flags) {
671 info->fbops->fb_imageblit = nvidiafb_imageblit;
672 info->fbops->fb_fillrect = nvidiafb_fillrect;
673 info->fbops->fb_copyarea = nvidiafb_copyarea;
674 info->fbops->fb_sync = nvidiafb_sync;
675 info->pixmap.scan_align = 4;
676 info->flags &= ~FBINFO_HWACCEL_DISABLED;
677 NVResetGraphics(info);
679 info->fbops->fb_imageblit = cfb_imageblit;
680 info->fbops->fb_fillrect = cfb_fillrect;
681 info->fbops->fb_copyarea = cfb_copyarea;
682 info->fbops->fb_sync = NULL;
683 info->pixmap.scan_align = 1;
684 info->flags |= FBINFO_HWACCEL_DISABLED;
687 par->cursor_reset = 1;
689 nvidia_screen_off(par, 0);
691 #ifdef CONFIG_BOOTX_TEXT
692 /* Update debug text engine */
693 btext_update_display(info->fix.smem_start,
694 info->var.xres, info->var.yres,
695 info->var.bits_per_pixel, info->fix.line_length);
698 NVLockUnlock(par, 0);
703 static int nvidiafb_setcolreg(unsigned regno, unsigned red, unsigned green,
704 unsigned blue, unsigned transp,
705 struct fb_info *info)
707 struct nvidia_par *par = info->par;
711 if (regno >= (1 << info->var.green.length))
714 if (info->var.grayscale) {
715 /* gray = 0.30*R + 0.59*G + 0.11*B */
716 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
719 if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
720 ((u32 *) info->pseudo_palette)[regno] =
721 (regno << info->var.red.offset) |
722 (regno << info->var.green.offset) |
723 (regno << info->var.blue.offset);
726 switch (info->var.bits_per_pixel) {
728 /* "transparent" stuff is completely ignored. */
729 nvidia_write_clut(par, regno, red >> 8, green >> 8, blue >> 8);
732 if (info->var.green.length == 5) {
733 for (i = 0; i < 8; i++) {
734 nvidia_write_clut(par, regno * 8 + i, red >> 8,
735 green >> 8, blue >> 8);
741 for (i = 0; i < 8; i++) {
742 nvidia_write_clut(par, regno * 8 + i,
743 red >> 8, green >> 8,
748 nvidia_read_clut(par, regno * 4, &r, &g, &b);
750 for (i = 0; i < 4; i++)
751 nvidia_write_clut(par, regno * 4 + i, r,
756 nvidia_write_clut(par, regno, red >> 8, green >> 8, blue >> 8);
767 static int nvidiafb_check_var(struct fb_var_screeninfo *var,
768 struct fb_info *info)
770 struct nvidia_par *par = info->par;
771 int memlen, vramlen, mode_valid = 0;
776 var->transp.offset = 0;
777 var->transp.length = 0;
781 if (var->bits_per_pixel <= 8)
782 var->bits_per_pixel = 8;
783 else if (var->bits_per_pixel <= 16)
784 var->bits_per_pixel = 16;
786 var->bits_per_pixel = 32;
788 switch (var->bits_per_pixel) {
792 var->green.offset = 0;
793 var->green.length = 8;
794 var->blue.offset = 0;
795 var->blue.length = 8;
796 var->transp.offset = 0;
797 var->transp.length = 0;
800 var->green.length = (var->green.length < 6) ? 5 : 6;
802 var->blue.length = 5;
803 var->transp.length = 6 - var->green.length;
804 var->blue.offset = 0;
805 var->green.offset = 5;
806 var->red.offset = 5 + var->green.length;
807 var->transp.offset = (5 + var->red.offset) & 15;
809 case 32: /* RGBA 8888 */
810 var->red.offset = 16;
812 var->green.offset = 8;
813 var->green.length = 8;
814 var->blue.offset = 0;
815 var->blue.length = 8;
816 var->transp.length = 8;
817 var->transp.offset = 24;
821 var->red.msb_right = 0;
822 var->green.msb_right = 0;
823 var->blue.msb_right = 0;
824 var->transp.msb_right = 0;
826 if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
827 !info->monspecs.dclkmax || !fb_validate_mode(var, info))
830 /* calculate modeline if supported by monitor */
831 if (!mode_valid && info->monspecs.gtf) {
832 if (!fb_get_mode(FB_MAXTIMINGS, 0, var, info))
837 const struct fb_videomode *mode;
839 mode = fb_find_best_mode(var, &info->modelist);
841 fb_videomode_to_var(var, mode);
846 if (!mode_valid && info->monspecs.modedb_len)
849 if (par->fpWidth && par->fpHeight && (par->fpWidth < var->xres ||
850 par->fpHeight < var->yres))
853 if (var->yres_virtual < var->yres)
854 var->yres_virtual = var->yres;
856 if (var->xres_virtual < var->xres)
857 var->xres_virtual = var->xres;
859 var->xres_virtual = (var->xres_virtual + 63) & ~63;
861 vramlen = info->screen_size;
862 pitch = ((var->xres_virtual * var->bits_per_pixel) + 7) / 8;
863 memlen = pitch * var->yres_virtual;
865 if (memlen > vramlen) {
866 var->yres_virtual = vramlen / pitch;
868 if (var->yres_virtual < var->yres) {
869 var->yres_virtual = var->yres;
870 var->xres_virtual = vramlen / var->yres_virtual;
871 var->xres_virtual /= var->bits_per_pixel / 8;
872 var->xres_virtual &= ~63;
873 pitch = (var->xres_virtual *
874 var->bits_per_pixel + 7) / 8;
875 memlen = pitch * var->yres;
877 if (var->xres_virtual < var->xres) {
878 printk("nvidiafb: required video memory, "
879 "%d bytes, for %dx%d-%d (virtual) "
881 memlen, var->xres_virtual,
882 var->yres_virtual, var->bits_per_pixel);
888 if (var->accel_flags) {
889 if (var->yres_virtual > 0x7fff)
890 var->yres_virtual = 0x7fff;
891 if (var->xres_virtual > 0x7fff)
892 var->xres_virtual = 0x7fff;
895 var->xres_virtual &= ~63;
902 static int nvidiafb_pan_display(struct fb_var_screeninfo *var,
903 struct fb_info *info)
905 struct nvidia_par *par = info->par;
908 total = var->yoffset * info->fix.line_length + var->xoffset;
910 NVSetStartAddress(par, total);
915 static int nvidiafb_blank(int blank, struct fb_info *info)
917 struct nvidia_par *par = info->par;
918 unsigned char tmp, vesa;
920 tmp = NVReadSeq(par, 0x01) & ~0x20; /* screen on/off */
921 vesa = NVReadCrtc(par, 0x1a) & ~0xc0; /* sync on/off */
929 case FB_BLANK_UNBLANK:
930 case FB_BLANK_NORMAL:
932 case FB_BLANK_VSYNC_SUSPEND:
935 case FB_BLANK_HSYNC_SUSPEND:
938 case FB_BLANK_POWERDOWN:
943 NVWriteSeq(par, 0x01, tmp);
944 NVWriteCrtc(par, 0x1a, vesa);
952 * Because the VGA registers are not mapped linearly in its MMIO space,
953 * restrict VGA register saving and restore to x86 only, where legacy VGA IO
954 * access is legal. Consequently, we must also check if the device is the
958 static void save_vga_x86(struct nvidia_par *par)
960 struct resource *res= &par->pci_dev->resource[PCI_ROM_RESOURCE];
962 if (res && res->flags & IORESOURCE_ROM_SHADOW) {
963 memset(&par->vgastate, 0, sizeof(par->vgastate));
964 par->vgastate.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS |
966 save_vga(&par->vgastate);
970 static void restore_vga_x86(struct nvidia_par *par)
972 struct resource *res= &par->pci_dev->resource[PCI_ROM_RESOURCE];
974 if (res && res->flags & IORESOURCE_ROM_SHADOW)
975 restore_vga(&par->vgastate);
978 #define save_vga_x86(x) do {} while (0)
979 #define restore_vga_x86(x) do {} while (0)
982 static int nvidiafb_open(struct fb_info *info, int user)
984 struct nvidia_par *par = info->par;
986 mutex_lock(&par->open_lock);
988 if (!par->open_count) {
990 nvidia_save_vga(par, &par->initial_state);
994 mutex_unlock(&par->open_lock);
998 static int nvidiafb_release(struct fb_info *info, int user)
1000 struct nvidia_par *par = info->par;
1003 mutex_lock(&par->open_lock);
1005 if (!par->open_count) {
1010 if (par->open_count == 1) {
1011 nvidia_write_regs(par, &par->initial_state);
1012 restore_vga_x86(par);
1017 mutex_unlock(&par->open_lock);
1021 static struct fb_ops nvidia_fb_ops = {
1022 .owner = THIS_MODULE,
1023 .fb_open = nvidiafb_open,
1024 .fb_release = nvidiafb_release,
1025 .fb_check_var = nvidiafb_check_var,
1026 .fb_set_par = nvidiafb_set_par,
1027 .fb_setcolreg = nvidiafb_setcolreg,
1028 .fb_pan_display = nvidiafb_pan_display,
1029 .fb_blank = nvidiafb_blank,
1030 .fb_fillrect = nvidiafb_fillrect,
1031 .fb_copyarea = nvidiafb_copyarea,
1032 .fb_imageblit = nvidiafb_imageblit,
1033 .fb_cursor = nvidiafb_cursor,
1034 .fb_sync = nvidiafb_sync,
1038 static int nvidiafb_suspend(struct pci_dev *dev, pm_message_t mesg)
1040 struct fb_info *info = pci_get_drvdata(dev);
1041 struct nvidia_par *par = info->par;
1043 if (mesg.event == PM_EVENT_PRETHAW)
1044 mesg.event = PM_EVENT_FREEZE;
1045 acquire_console_sem();
1046 par->pm_state = mesg.event;
1048 if (mesg.event == PM_EVENT_SUSPEND) {
1049 fb_set_suspend(info, 1);
1050 nvidiafb_blank(FB_BLANK_POWERDOWN, info);
1051 nvidia_write_regs(par, &par->SavedReg);
1052 pci_save_state(dev);
1053 pci_disable_device(dev);
1054 pci_set_power_state(dev, pci_choose_state(dev, mesg));
1056 dev->dev.power.power_state = mesg;
1058 release_console_sem();
1062 static int nvidiafb_resume(struct pci_dev *dev)
1064 struct fb_info *info = pci_get_drvdata(dev);
1065 struct nvidia_par *par = info->par;
1067 acquire_console_sem();
1068 pci_set_power_state(dev, PCI_D0);
1070 if (par->pm_state != PM_EVENT_FREEZE) {
1071 pci_restore_state(dev);
1073 if (pci_enable_device(dev))
1076 pci_set_master(dev);
1079 par->pm_state = PM_EVENT_ON;
1080 nvidiafb_set_par(info);
1081 fb_set_suspend (info, 0);
1082 nvidiafb_blank(FB_BLANK_UNBLANK, info);
1085 release_console_sem();
1089 #define nvidiafb_suspend NULL
1090 #define nvidiafb_resume NULL
1093 static int __devinit nvidia_set_fbinfo(struct fb_info *info)
1095 struct fb_monspecs *specs = &info->monspecs;
1096 struct fb_videomode modedb;
1097 struct nvidia_par *par = info->par;
1101 info->flags = FBINFO_DEFAULT
1102 | FBINFO_HWACCEL_IMAGEBLIT
1103 | FBINFO_HWACCEL_FILLRECT
1104 | FBINFO_HWACCEL_COPYAREA
1105 | FBINFO_HWACCEL_YPAN;
1107 fb_videomode_to_modelist(info->monspecs.modedb,
1108 info->monspecs.modedb_len, &info->modelist);
1109 fb_var_to_videomode(&modedb, &nvidiafb_default_var);
1123 if (specs->modedb != NULL) {
1124 const struct fb_videomode *mode;
1126 mode = fb_find_best_display(specs, &info->modelist);
1127 fb_videomode_to_var(&nvidiafb_default_var, mode);
1128 nvidiafb_default_var.bits_per_pixel = bpp;
1129 } else if (par->fpWidth && par->fpHeight) {
1133 snprintf(buf, 15, "%dx%dMR", par->fpWidth, par->fpHeight);
1134 fb_find_mode(&nvidiafb_default_var, info, buf, specs->modedb,
1135 specs->modedb_len, &modedb, bpp);
1139 fb_find_mode(&nvidiafb_default_var, info, mode_option,
1140 specs->modedb, specs->modedb_len, &modedb, bpp);
1142 info->var = nvidiafb_default_var;
1143 info->fix.visual = (info->var.bits_per_pixel == 8) ?
1144 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
1145 info->pseudo_palette = par->pseudo_palette;
1146 fb_alloc_cmap(&info->cmap, 256, 0);
1147 fb_destroy_modedb(info->monspecs.modedb);
1148 info->monspecs.modedb = NULL;
1150 /* maximize virtual vertical length */
1151 lpitch = info->var.xres_virtual *
1152 ((info->var.bits_per_pixel + 7) >> 3);
1153 info->var.yres_virtual = info->screen_size / lpitch;
1155 info->pixmap.scan_align = 4;
1156 info->pixmap.buf_align = 4;
1157 info->pixmap.access_align = 32;
1158 info->pixmap.size = 8 * 1024;
1159 info->pixmap.flags = FB_PIXMAP_SYSTEM;
1162 info->fbops->fb_cursor = NULL;
1164 info->var.accel_flags = (!noaccel);
1166 switch (par->Architecture) {
1168 info->fix.accel = FB_ACCEL_NV4;
1171 info->fix.accel = FB_ACCEL_NV_10;
1174 info->fix.accel = FB_ACCEL_NV_20;
1177 info->fix.accel = FB_ACCEL_NV_30;
1180 info->fix.accel = FB_ACCEL_NV_40;
1186 return nvidiafb_check_var(&info->var, info);
1189 static u32 __devinit nvidia_get_chipset(struct fb_info *info)
1191 struct nvidia_par *par = info->par;
1192 u32 id = (par->pci_dev->vendor << 16) | par->pci_dev->device;
1194 printk(KERN_INFO PFX "Device ID: %x \n", id);
1196 if ((id & 0xfff0) == 0x00f0) {
1198 id = NV_RD32(par->REGS, 0x1800);
1200 if ((id & 0x0000ffff) == 0x000010DE)
1201 id = 0x10DE0000 | (id >> 16);
1202 else if ((id & 0xffff0000) == 0xDE100000) /* wrong endian */
1203 id = 0x10DE0000 | ((id << 8) & 0x0000ff00) |
1204 ((id >> 8) & 0x000000ff);
1205 printk(KERN_INFO PFX "Subsystem ID: %x \n", id);
1211 static u32 __devinit nvidia_get_arch(struct fb_info *info)
1213 struct nvidia_par *par = info->par;
1216 switch (par->Chipset & 0x0ff0) {
1217 case 0x0100: /* GeForce 256 */
1218 case 0x0110: /* GeForce2 MX */
1219 case 0x0150: /* GeForce2 */
1220 case 0x0170: /* GeForce4 MX */
1221 case 0x0180: /* GeForce4 MX (8x AGP) */
1222 case 0x01A0: /* nForce */
1223 case 0x01F0: /* nForce2 */
1226 case 0x0200: /* GeForce3 */
1227 case 0x0250: /* GeForce4 Ti */
1228 case 0x0280: /* GeForce4 Ti (8x AGP) */
1231 case 0x0300: /* GeForceFX 5800 */
1232 case 0x0310: /* GeForceFX 5600 */
1233 case 0x0320: /* GeForceFX 5200 */
1234 case 0x0330: /* GeForceFX 5900 */
1235 case 0x0340: /* GeForceFX 5700 */
1238 case 0x0040: /* GeForce 6800 */
1239 case 0x00C0: /* GeForce 6800 */
1240 case 0x0120: /* GeForce 6800 */
1242 case 0x0140: /* GeForce 6600 */
1243 case 0x0160: /* GeForce 6200 */
1244 case 0x01D0: /* GeForce 7200, 7300, 7400 */
1245 case 0x02E0: /* GeForce 7300 GT */
1246 case 0x0090: /* GeForce 7800 */
1247 case 0x0210: /* GeForce 6800 */
1248 case 0x0220: /* GeForce 6200 */
1250 case 0x0240: /* GeForce 6100 */
1251 case 0x0290: /* GeForce 7900 */
1252 case 0x0390: /* GeForce 7600 */
1255 case 0x0020: /* TNT, TNT2 */
1258 default: /* unknown architecture */
1265 static int __devinit nvidiafb_probe(struct pci_dev *pd,
1266 const struct pci_device_id *ent)
1268 struct nvidia_par *par;
1269 struct fb_info *info;
1276 info = framebuffer_alloc(sizeof(struct nvidia_par), &pd->dev);
1283 mutex_init(&par->open_lock);
1284 info->pixmap.addr = kzalloc(8 * 1024, GFP_KERNEL);
1286 if (info->pixmap.addr == NULL)
1289 if (pci_enable_device(pd)) {
1290 printk(KERN_ERR PFX "cannot enable PCI device\n");
1291 goto err_out_enable;
1294 if (pci_request_regions(pd, "nvidiafb")) {
1295 printk(KERN_ERR PFX "cannot request PCI regions\n");
1296 goto err_out_enable;
1299 par->FlatPanel = flatpanel;
1301 printk(KERN_INFO PFX "flatpanel support enabled\n");
1302 par->FPDither = fpdither;
1304 par->CRTCnumber = forceCRTC;
1305 par->FpScale = (!noscale);
1306 par->paneltweak = paneltweak;
1308 /* enable IO and mem if not already done */
1309 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1310 cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1311 pci_write_config_word(pd, PCI_COMMAND, cmd);
1313 nvidiafb_fix.mmio_start = pci_resource_start(pd, 0);
1314 nvidiafb_fix.smem_start = pci_resource_start(pd, 1);
1315 nvidiafb_fix.mmio_len = pci_resource_len(pd, 0);
1317 par->REGS = ioremap(nvidiafb_fix.mmio_start, nvidiafb_fix.mmio_len);
1320 printk(KERN_ERR PFX "cannot ioremap MMIO base\n");
1321 goto err_out_free_base0;
1324 par->Chipset = nvidia_get_chipset(info);
1325 par->Architecture = nvidia_get_arch(info);
1327 if (par->Architecture == 0) {
1328 printk(KERN_ERR PFX "unknown NV_ARCH\n");
1332 sprintf(nvidiafb_fix.id, "NV%x", (pd->device & 0x0ff0) >> 4);
1334 if (NVCommonSetup(info))
1337 par->FbAddress = nvidiafb_fix.smem_start;
1338 par->FbMapSize = par->RamAmountKBytes * 1024;
1339 if (vram && vram * 1024 * 1024 < par->FbMapSize)
1340 par->FbMapSize = vram * 1024 * 1024;
1342 /* Limit amount of vram to 64 MB */
1343 if (par->FbMapSize > 64 * 1024 * 1024)
1344 par->FbMapSize = 64 * 1024 * 1024;
1346 if(par->Architecture >= NV_ARCH_40)
1347 par->FbUsableSize = par->FbMapSize - (560 * 1024);
1349 par->FbUsableSize = par->FbMapSize - (128 * 1024);
1350 par->ScratchBufferSize = (par->Architecture < NV_ARCH_10) ? 8 * 1024 :
1352 par->ScratchBufferStart = par->FbUsableSize - par->ScratchBufferSize;
1353 par->CursorStart = par->FbUsableSize + (32 * 1024);
1355 info->screen_base = ioremap(nvidiafb_fix.smem_start, par->FbMapSize);
1356 info->screen_size = par->FbUsableSize;
1357 nvidiafb_fix.smem_len = par->RamAmountKBytes * 1024;
1359 if (!info->screen_base) {
1360 printk(KERN_ERR PFX "cannot ioremap FB base\n");
1361 goto err_out_free_base1;
1364 par->FbStart = info->screen_base;
1368 par->mtrr.vram = mtrr_add(nvidiafb_fix.smem_start,
1369 par->RamAmountKBytes * 1024,
1370 MTRR_TYPE_WRCOMB, 1);
1371 if (par->mtrr.vram < 0) {
1372 printk(KERN_ERR PFX "unable to setup MTRR\n");
1374 par->mtrr.vram_valid = 1;
1375 /* let there be speed */
1376 printk(KERN_INFO PFX "MTRR set to ON\n");
1379 #endif /* CONFIG_MTRR */
1381 info->fbops = &nvidia_fb_ops;
1382 info->fix = nvidiafb_fix;
1384 if (nvidia_set_fbinfo(info) < 0) {
1385 printk(KERN_ERR PFX "error setting initial video mode\n");
1386 goto err_out_iounmap_fb;
1389 nvidia_save_vga(par, &par->SavedReg);
1391 pci_set_drvdata(pd, info);
1394 nvidia_bl_init(par);
1396 if (register_framebuffer(info) < 0) {
1397 printk(KERN_ERR PFX "error registering nVidia framebuffer\n");
1398 goto err_out_iounmap_fb;
1402 printk(KERN_INFO PFX
1403 "PCI nVidia %s framebuffer (%dMB @ 0x%lX)\n",
1405 par->FbMapSize / (1024 * 1024), info->fix.smem_start);
1411 iounmap(info->screen_base);
1413 fb_destroy_modedb(info->monspecs.modedb);
1414 nvidia_delete_i2c_busses(par);
1418 pci_release_regions(pd);
1420 kfree(info->pixmap.addr);
1422 framebuffer_release(info);
1427 static void __devexit nvidiafb_remove(struct pci_dev *pd)
1429 struct fb_info *info = pci_get_drvdata(pd);
1430 struct nvidia_par *par = info->par;
1434 unregister_framebuffer(info);
1436 nvidia_bl_exit(par);
1439 if (par->mtrr.vram_valid)
1440 mtrr_del(par->mtrr.vram, info->fix.smem_start,
1441 info->fix.smem_len);
1442 #endif /* CONFIG_MTRR */
1444 iounmap(info->screen_base);
1445 fb_destroy_modedb(info->monspecs.modedb);
1446 nvidia_delete_i2c_busses(par);
1448 pci_release_regions(pd);
1449 kfree(info->pixmap.addr);
1450 framebuffer_release(info);
1451 pci_set_drvdata(pd, NULL);
1455 /* ------------------------------------------------------------------------- *
1459 * ------------------------------------------------------------------------- */
1462 static int __devinit nvidiafb_setup(char *options)
1467 if (!options || !*options)
1470 while ((this_opt = strsep(&options, ",")) != NULL) {
1471 if (!strncmp(this_opt, "forceCRTC", 9)) {
1477 forceCRTC = *p - '0';
1478 if (forceCRTC < 0 || forceCRTC > 1)
1480 } else if (!strncmp(this_opt, "flatpanel", 9)) {
1482 } else if (!strncmp(this_opt, "hwcur", 5)) {
1484 } else if (!strncmp(this_opt, "noaccel", 6)) {
1486 } else if (!strncmp(this_opt, "noscale", 7)) {
1488 } else if (!strncmp(this_opt, "paneltweak:", 11)) {
1489 paneltweak = simple_strtoul(this_opt+11, NULL, 0);
1490 } else if (!strncmp(this_opt, "vram:", 5)) {
1491 vram = simple_strtoul(this_opt+5, NULL, 0);
1492 } else if (!strncmp(this_opt, "backlight:", 10)) {
1493 backlight = simple_strtoul(this_opt+10, NULL, 0);
1495 } else if (!strncmp(this_opt, "nomtrr", 6)) {
1498 } else if (!strncmp(this_opt, "fpdither:", 9)) {
1499 fpdither = simple_strtol(this_opt+9, NULL, 0);
1500 } else if (!strncmp(this_opt, "bpp:", 4)) {
1501 bpp = simple_strtoul(this_opt+4, NULL, 0);
1503 mode_option = this_opt;
1508 #endif /* !MODULE */
1510 static struct pci_driver nvidiafb_driver = {
1512 .id_table = nvidiafb_pci_tbl,
1513 .probe = nvidiafb_probe,
1514 .suspend = nvidiafb_suspend,
1515 .resume = nvidiafb_resume,
1516 .remove = __devexit_p(nvidiafb_remove),
1519 /* ------------------------------------------------------------------------- *
1523 * ------------------------------------------------------------------------- */
1525 static int __devinit nvidiafb_init(void)
1528 char *option = NULL;
1530 if (fb_get_options("nvidiafb", &option))
1532 nvidiafb_setup(option);
1534 return pci_register_driver(&nvidiafb_driver);
1537 module_init(nvidiafb_init);
1540 static void __exit nvidiafb_exit(void)
1542 pci_unregister_driver(&nvidiafb_driver);
1545 module_exit(nvidiafb_exit);
1547 module_param(flatpanel, int, 0);
1548 MODULE_PARM_DESC(flatpanel,
1549 "Enables experimental flat panel support for some chipsets. "
1550 "(0=disabled, 1=enabled, -1=autodetect) (default=-1)");
1551 module_param(fpdither, int, 0);
1552 MODULE_PARM_DESC(fpdither,
1553 "Enables dithering of flat panel for 6 bits panels. "
1554 "(0=disabled, 1=enabled, -1=autodetect) (default=-1)");
1555 module_param(hwcur, int, 0);
1556 MODULE_PARM_DESC(hwcur,
1557 "Enables hardware cursor implementation. (0 or 1=enabled) "
1559 module_param(noaccel, int, 0);
1560 MODULE_PARM_DESC(noaccel,
1561 "Disables hardware acceleration. (0 or 1=disable) "
1563 module_param(noscale, int, 0);
1564 MODULE_PARM_DESC(noscale,
1565 "Disables screen scaleing. (0 or 1=disable) "
1566 "(default=0, do scaling)");
1567 module_param(paneltweak, int, 0);
1568 MODULE_PARM_DESC(paneltweak,
1569 "Tweak display settings for flatpanels. "
1570 "(default=0, no tweaks)");
1571 module_param(forceCRTC, int, 0);
1572 MODULE_PARM_DESC(forceCRTC,
1573 "Forces usage of a particular CRTC in case autodetection "
1574 "fails. (0 or 1) (default=autodetect)");
1575 module_param(vram, int, 0);
1576 MODULE_PARM_DESC(vram,
1577 "amount of framebuffer memory to remap in MiB"
1578 "(default=0 - remap entire memory)");
1579 module_param(mode_option, charp, 0);
1580 MODULE_PARM_DESC(mode_option, "Specify initial video mode");
1581 module_param(bpp, int, 0);
1582 MODULE_PARM_DESC(bpp, "pixel width in bits"
1585 module_param(nomtrr, bool, 0);
1586 MODULE_PARM_DESC(nomtrr, "Disables MTRR support (0 or 1=disabled) "
1590 MODULE_AUTHOR("Antonino Daplas");
1591 MODULE_DESCRIPTION("Framebuffer driver for nVidia graphics chipset");
1592 MODULE_LICENSE("GPL");