s3fb.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * linux/drivers/video/s3fb.c -- Frame buffer device driver for S3 Trio/Virge
  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. * Code is based on David Boucher's viafb (http://davesdomain.org.uk/viafb/)
  11. * which is based on the code of neofb.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/tty.h>
  19. #include <linux/delay.h>
  20. #include <linux/fb.h>
  21. #include <linux/svga.h>
  22. #include <linux/init.h>
  23. #include <linux/pci.h>
  24. #include <linux/console.h> /* Why should fb driver call console functions? because console_lock() */
  25. #include <video/vga.h>
  26. #ifdef CONFIG_MTRR
  27. #include <asm/mtrr.h>
  28. #endif
  29. struct s3fb_info {
  30. int chip, rev, mclk_freq;
  31. int mtrr_reg;
  32. struct vgastate state;
  33. struct mutex open_lock;
  34. unsigned int ref_count;
  35. u32 pseudo_palette[16];
  36. };
  37. /* ------------------------------------------------------------------------- */
  38. static const struct svga_fb_format s3fb_formats[] = {
  39. { 0, {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0}, 0,
  40. FB_TYPE_TEXT, FB_AUX_TEXT_SVGA_STEP4, FB_VISUAL_PSEUDOCOLOR, 8, 16},
  41. { 4, {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0}, 0,
  42. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_PSEUDOCOLOR, 8, 16},
  43. { 4, {0, 4, 0}, {0, 4, 0}, {0, 4, 0}, {0, 0, 0}, 1,
  44. FB_TYPE_INTERLEAVED_PLANES, 1, FB_VISUAL_PSEUDOCOLOR, 8, 16},
  45. { 8, {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0}, 0,
  46. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_PSEUDOCOLOR, 4, 8},
  47. {16, {10, 5, 0}, {5, 5, 0}, {0, 5, 0}, {0, 0, 0}, 0,
  48. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_TRUECOLOR, 2, 4},
  49. {16, {11, 5, 0}, {5, 6, 0}, {0, 5, 0}, {0, 0, 0}, 0,
  50. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_TRUECOLOR, 2, 4},
  51. {24, {16, 8, 0}, {8, 8, 0}, {0, 8, 0}, {0, 0, 0}, 0,
  52. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_TRUECOLOR, 1, 2},
  53. {32, {16, 8, 0}, {8, 8, 0}, {0, 8, 0}, {0, 0, 0}, 0,
  54. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_TRUECOLOR, 1, 2},
  55. SVGA_FORMAT_END
  56. };
  57. static const struct svga_pll s3_pll = {3, 129, 3, 33, 0, 3,
  58. 35000, 240000, 14318};
  59. static const int s3_memsizes[] = {4096, 0, 3072, 8192, 2048, 6144, 1024, 512};
  60. static const char * const s3_names[] = {"S3 Unknown", "S3 Trio32", "S3 Trio64", "S3 Trio64V+",
  61. "S3 Trio64UV+", "S3 Trio64V2/DX", "S3 Trio64V2/GX",
  62. "S3 Plato/PX", "S3 Aurora64VP", "S3 Virge",
  63. "S3 Virge/VX", "S3 Virge/DX", "S3 Virge/GX",
  64. "S3 Virge/GX2", "S3 Virge/GX2P", "S3 Virge/GX2P",
  65. "S3 Trio3D/1X", "S3 Trio3D/2X", "S3 Trio3D/2X"};
  66. #define CHIP_UNKNOWN 0x00
  67. #define CHIP_732_TRIO32 0x01
  68. #define CHIP_764_TRIO64 0x02
  69. #define CHIP_765_TRIO64VP 0x03
  70. #define CHIP_767_TRIO64UVP 0x04
  71. #define CHIP_775_TRIO64V2_DX 0x05
  72. #define CHIP_785_TRIO64V2_GX 0x06
  73. #define CHIP_551_PLATO_PX 0x07
  74. #define CHIP_M65_AURORA64VP 0x08
  75. #define CHIP_325_VIRGE 0x09
  76. #define CHIP_988_VIRGE_VX 0x0A
  77. #define CHIP_375_VIRGE_DX 0x0B
  78. #define CHIP_385_VIRGE_GX 0x0C
  79. #define CHIP_356_VIRGE_GX2 0x0D
  80. #define CHIP_357_VIRGE_GX2P 0x0E
  81. #define CHIP_359_VIRGE_GX2P 0x0F
  82. #define CHIP_360_TRIO3D_1X 0x10
  83. #define CHIP_362_TRIO3D_2X 0x11
  84. #define CHIP_368_TRIO3D_2X 0x12
  85. #define CHIP_XXX_TRIO 0x80
  86. #define CHIP_XXX_TRIO64V2_DXGX 0x81
  87. #define CHIP_XXX_VIRGE_DXGX 0x82
  88. #define CHIP_36X_TRIO3D_1X_2X 0x83
  89. #define CHIP_UNDECIDED_FLAG 0x80
  90. #define CHIP_MASK 0xFF
  91. /* CRT timing register sets */
  92. static const struct vga_regset s3_h_total_regs[] = {{0x00, 0, 7}, {0x5D, 0, 0}, VGA_REGSET_END};
  93. static const struct vga_regset s3_h_display_regs[] = {{0x01, 0, 7}, {0x5D, 1, 1}, VGA_REGSET_END};
  94. static const struct vga_regset s3_h_blank_start_regs[] = {{0x02, 0, 7}, {0x5D, 2, 2}, VGA_REGSET_END};
  95. static const struct vga_regset s3_h_blank_end_regs[] = {{0x03, 0, 4}, {0x05, 7, 7}, VGA_REGSET_END};
  96. static const struct vga_regset s3_h_sync_start_regs[] = {{0x04, 0, 7}, {0x5D, 4, 4}, VGA_REGSET_END};
  97. static const struct vga_regset s3_h_sync_end_regs[] = {{0x05, 0, 4}, VGA_REGSET_END};
  98. static const struct vga_regset s3_v_total_regs[] = {{0x06, 0, 7}, {0x07, 0, 0}, {0x07, 5, 5}, {0x5E, 0, 0}, VGA_REGSET_END};
  99. static const struct vga_regset s3_v_display_regs[] = {{0x12, 0, 7}, {0x07, 1, 1}, {0x07, 6, 6}, {0x5E, 1, 1}, VGA_REGSET_END};
  100. static const struct vga_regset s3_v_blank_start_regs[] = {{0x15, 0, 7}, {0x07, 3, 3}, {0x09, 5, 5}, {0x5E, 2, 2}, VGA_REGSET_END};
  101. static const struct vga_regset s3_v_blank_end_regs[] = {{0x16, 0, 7}, VGA_REGSET_END};
  102. static const struct vga_regset s3_v_sync_start_regs[] = {{0x10, 0, 7}, {0x07, 2, 2}, {0x07, 7, 7}, {0x5E, 4, 4}, VGA_REGSET_END};
  103. static const struct vga_regset s3_v_sync_end_regs[] = {{0x11, 0, 3}, VGA_REGSET_END};
  104. static const struct vga_regset s3_line_compare_regs[] = {{0x18, 0, 7}, {0x07, 4, 4}, {0x09, 6, 6}, {0x5E, 6, 6}, VGA_REGSET_END};
  105. static const struct vga_regset s3_start_address_regs[] = {{0x0d, 0, 7}, {0x0c, 0, 7}, {0x31, 4, 5}, {0x51, 0, 1}, VGA_REGSET_END};
  106. static const struct vga_regset s3_offset_regs[] = {{0x13, 0, 7}, {0x51, 4, 5}, VGA_REGSET_END}; /* set 0x43 bit 2 to 0 */
  107. static const struct svga_timing_regs s3_timing_regs = {
  108. s3_h_total_regs, s3_h_display_regs, s3_h_blank_start_regs,
  109. s3_h_blank_end_regs, s3_h_sync_start_regs, s3_h_sync_end_regs,
  110. s3_v_total_regs, s3_v_display_regs, s3_v_blank_start_regs,
  111. s3_v_blank_end_regs, s3_v_sync_start_regs, s3_v_sync_end_regs,
  112. };
  113. /* ------------------------------------------------------------------------- */
  114. /* Module parameters */
  115. static char *mode_option __devinitdata = "640x480-8@60";
  116. #ifdef CONFIG_MTRR
  117. static int mtrr __devinitdata = 1;
  118. #endif
  119. static int fasttext = 1;
  120. MODULE_AUTHOR("(c) 2006-2007 Ondrej Zajicek <santiago@crfreenet.org>");
  121. MODULE_LICENSE("GPL");
  122. MODULE_DESCRIPTION("fbdev driver for S3 Trio/Virge");
  123. module_param(mode_option, charp, 0444);
  124. MODULE_PARM_DESC(mode_option, "Default video mode ('640x480-8@60', etc)");
  125. module_param_named(mode, mode_option, charp, 0444);
  126. MODULE_PARM_DESC(mode, "Default video mode ('640x480-8@60', etc) (deprecated)");
  127. #ifdef CONFIG_MTRR
  128. module_param(mtrr, int, 0444);
  129. MODULE_PARM_DESC(mtrr, "Enable write-combining with MTRR (1=enable, 0=disable, default=1)");
  130. #endif
  131. module_param(fasttext, int, 0644);
  132. MODULE_PARM_DESC(fasttext, "Enable S3 fast text mode (1=enable, 0=disable, default=1)");
  133. /* ------------------------------------------------------------------------- */
  134. /* Set font in S3 fast text mode */
  135. static void s3fb_settile_fast(struct fb_info *info, struct fb_tilemap *map)
  136. {
  137. const u8 *font = map->data;
  138. u8 __iomem *fb = (u8 __iomem *) info->screen_base;
  139. int i, c;
  140. if ((map->width != 8) || (map->height != 16) ||
  141. (map->depth != 1) || (map->length != 256)) {
  142. printk(KERN_ERR "fb%d: unsupported font parameters: width %d, height %d, depth %d, length %d\n",
  143. info->node, map->width, map->height, map->depth, map->length);
  144. return;
  145. }
  146. fb += 2;
  147. for (i = 0; i < map->height; i++) {
  148. for (c = 0; c < map->length; c++) {
  149. fb_writeb(font[c * map->height + i], fb + c * 4);
  150. }
  151. fb += 1024;
  152. }
  153. }
  154. static void s3fb_tilecursor(struct fb_info *info, struct fb_tilecursor *cursor)
  155. {
  156. struct s3fb_info *par = info->par;
  157. svga_tilecursor(par->state.vgabase, info, cursor);
  158. }
  159. static struct fb_tile_ops s3fb_tile_ops = {
  160. .fb_settile = svga_settile,
  161. .fb_tilecopy = svga_tilecopy,
  162. .fb_tilefill = svga_tilefill,
  163. .fb_tileblit = svga_tileblit,
  164. .fb_tilecursor = s3fb_tilecursor,
  165. .fb_get_tilemax = svga_get_tilemax,
  166. };
  167. static struct fb_tile_ops s3fb_fast_tile_ops = {
  168. .fb_settile = s3fb_settile_fast,
  169. .fb_tilecopy = svga_tilecopy,
  170. .fb_tilefill = svga_tilefill,
  171. .fb_tileblit = svga_tileblit,
  172. .fb_tilecursor = s3fb_tilecursor,
  173. .fb_get_tilemax = svga_get_tilemax,
  174. };
  175. /* ------------------------------------------------------------------------- */
  176. /* image data is MSB-first, fb structure is MSB-first too */
  177. static inline u32 expand_color(u32 c)
  178. {
  179. return ((c & 1) | ((c & 2) << 7) | ((c & 4) << 14) | ((c & 8) << 21)) * 0xFF;
  180. }
  181. /* s3fb_iplan_imageblit silently assumes that almost everything is 8-pixel aligned */
  182. static void s3fb_iplan_imageblit(struct fb_info *info, const struct fb_image *image)
  183. {
  184. u32 fg = expand_color(image->fg_color);
  185. u32 bg = expand_color(image->bg_color);
  186. const u8 *src1, *src;
  187. u8 __iomem *dst1;
  188. u32 __iomem *dst;
  189. u32 val;
  190. int x, y;
  191. src1 = image->data;
  192. dst1 = info->screen_base + (image->dy * info->fix.line_length)
  193. + ((image->dx / 8) * 4);
  194. for (y = 0; y < image->height; y++) {
  195. src = src1;
  196. dst = (u32 __iomem *) dst1;
  197. for (x = 0; x < image->width; x += 8) {
  198. val = *(src++) * 0x01010101;
  199. val = (val & fg) | (~val & bg);
  200. fb_writel(val, dst++);
  201. }
  202. src1 += image->width / 8;
  203. dst1 += info->fix.line_length;
  204. }
  205. }
  206. /* s3fb_iplan_fillrect silently assumes that almost everything is 8-pixel aligned */
  207. static void s3fb_iplan_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  208. {
  209. u32 fg = expand_color(rect->color);
  210. u8 __iomem *dst1;
  211. u32 __iomem *dst;
  212. int x, y;
  213. dst1 = info->screen_base + (rect->dy * info->fix.line_length)
  214. + ((rect->dx / 8) * 4);
  215. for (y = 0; y < rect->height; y++) {
  216. dst = (u32 __iomem *) dst1;
  217. for (x = 0; x < rect->width; x += 8) {
  218. fb_writel(fg, dst++);
  219. }
  220. dst1 += info->fix.line_length;
  221. }
  222. }
  223. /* image data is MSB-first, fb structure is high-nibble-in-low-byte-first */
  224. static inline u32 expand_pixel(u32 c)
  225. {
  226. return (((c & 1) << 24) | ((c & 2) << 27) | ((c & 4) << 14) | ((c & 8) << 17) |
  227. ((c & 16) << 4) | ((c & 32) << 7) | ((c & 64) >> 6) | ((c & 128) >> 3)) * 0xF;
  228. }
  229. /* s3fb_cfb4_imageblit silently assumes that almost everything is 8-pixel aligned */
  230. static void s3fb_cfb4_imageblit(struct fb_info *info, const struct fb_image *image)
  231. {
  232. u32 fg = image->fg_color * 0x11111111;
  233. u32 bg = image->bg_color * 0x11111111;
  234. const u8 *src1, *src;
  235. u8 __iomem *dst1;
  236. u32 __iomem *dst;
  237. u32 val;
  238. int x, y;
  239. src1 = image->data;
  240. dst1 = info->screen_base + (image->dy * info->fix.line_length)
  241. + ((image->dx / 8) * 4);
  242. for (y = 0; y < image->height; y++) {
  243. src = src1;
  244. dst = (u32 __iomem *) dst1;
  245. for (x = 0; x < image->width; x += 8) {
  246. val = expand_pixel(*(src++));
  247. val = (val & fg) | (~val & bg);
  248. fb_writel(val, dst++);
  249. }
  250. src1 += image->width / 8;
  251. dst1 += info->fix.line_length;
  252. }
  253. }
  254. static void s3fb_imageblit(struct fb_info *info, const struct fb_image *image)
  255. {
  256. if ((info->var.bits_per_pixel == 4) && (image->depth == 1)
  257. && ((image->width % 8) == 0) && ((image->dx % 8) == 0)) {
  258. if (info->fix.type == FB_TYPE_INTERLEAVED_PLANES)
  259. s3fb_iplan_imageblit(info, image);
  260. else
  261. s3fb_cfb4_imageblit(info, image);
  262. } else
  263. cfb_imageblit(info, image);
  264. }
  265. static void s3fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  266. {
  267. if ((info->var.bits_per_pixel == 4)
  268. && ((rect->width % 8) == 0) && ((rect->dx % 8) == 0)
  269. && (info->fix.type == FB_TYPE_INTERLEAVED_PLANES))
  270. s3fb_iplan_fillrect(info, rect);
  271. else
  272. cfb_fillrect(info, rect);
  273. }
  274. /* ------------------------------------------------------------------------- */
  275. static void s3_set_pixclock(struct fb_info *info, u32 pixclock)
  276. {
  277. struct s3fb_info *par = info->par;
  278. u16 m, n, r;
  279. u8 regval;
  280. int rv;
  281. rv = svga_compute_pll(&s3_pll, 1000000000 / pixclock, &m, &n, &r, info->node);
  282. if (rv < 0) {
  283. printk(KERN_ERR "fb%d: cannot set requested pixclock, keeping old value\n", info->node);
  284. return;
  285. }
  286. /* Set VGA misc register */
  287. regval = vga_r(NULL, VGA_MIS_R);
  288. vga_w(NULL, VGA_MIS_W, regval | VGA_MIS_ENB_PLL_LOAD);
  289. /* Set S3 clock registers */
  290. if (par->chip == CHIP_360_TRIO3D_1X ||
  291. par->chip == CHIP_362_TRIO3D_2X ||
  292. par->chip == CHIP_368_TRIO3D_2X) {
  293. vga_wseq(NULL, 0x12, (n - 2) | ((r & 3) << 6)); /* n and two bits of r */
  294. vga_wseq(NULL, 0x29, r >> 2); /* remaining highest bit of r */
  295. } else
  296. vga_wseq(NULL, 0x12, (n - 2) | (r << 5));
  297. vga_wseq(NULL, 0x13, m - 2);
  298. udelay(1000);
  299. /* Activate clock - write 0, 1, 0 to seq/15 bit 5 */
  300. regval = vga_rseq (NULL, 0x15); /* | 0x80; */
  301. vga_wseq(NULL, 0x15, regval & ~(1<<5));
  302. vga_wseq(NULL, 0x15, regval | (1<<5));
  303. vga_wseq(NULL, 0x15, regval & ~(1<<5));
  304. }
  305. /* Open framebuffer */
  306. static int s3fb_open(struct fb_info *info, int user)
  307. {
  308. struct s3fb_info *par = info->par;
  309. mutex_lock(&(par->open_lock));
  310. if (par->ref_count == 0) {
  311. memset(&(par->state), 0, sizeof(struct vgastate));
  312. par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS | VGA_SAVE_CMAP;
  313. par->state.num_crtc = 0x70;
  314. par->state.num_seq = 0x20;
  315. save_vga(&(par->state));
  316. }
  317. par->ref_count++;
  318. mutex_unlock(&(par->open_lock));
  319. return 0;
  320. }
  321. /* Close framebuffer */
  322. static int s3fb_release(struct fb_info *info, int user)
  323. {
  324. struct s3fb_info *par = info->par;
  325. mutex_lock(&(par->open_lock));
  326. if (par->ref_count == 0) {
  327. mutex_unlock(&(par->open_lock));
  328. return -EINVAL;
  329. }
  330. if (par->ref_count == 1)
  331. restore_vga(&(par->state));
  332. par->ref_count--;
  333. mutex_unlock(&(par->open_lock));
  334. return 0;
  335. }
  336. /* Validate passed in var */
  337. static int s3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  338. {
  339. struct s3fb_info *par = info->par;
  340. int rv, mem, step;
  341. u16 m, n, r;
  342. /* Find appropriate format */
  343. rv = svga_match_format (s3fb_formats, var, NULL);
  344. /* 32bpp mode is not supported on VIRGE VX,
  345. 24bpp is not supported on others */
  346. if ((par->chip == CHIP_988_VIRGE_VX) ? (rv == 7) : (rv == 6))
  347. rv = -EINVAL;
  348. if (rv < 0) {
  349. printk(KERN_ERR "fb%d: unsupported mode requested\n", info->node);
  350. return rv;
  351. }
  352. /* Do not allow to have real resoulution larger than virtual */
  353. if (var->xres > var->xres_virtual)
  354. var->xres_virtual = var->xres;
  355. if (var->yres > var->yres_virtual)
  356. var->yres_virtual = var->yres;
  357. /* Round up xres_virtual to have proper alignment of lines */
  358. step = s3fb_formats[rv].xresstep - 1;
  359. var->xres_virtual = (var->xres_virtual+step) & ~step;
  360. /* Check whether have enough memory */
  361. mem = ((var->bits_per_pixel * var->xres_virtual) >> 3) * var->yres_virtual;
  362. if (mem > info->screen_size) {
  363. printk(KERN_ERR "fb%d: not enough framebuffer memory (%d kB requested , %d kB available)\n",
  364. info->node, mem >> 10, (unsigned int) (info->screen_size >> 10));
  365. return -EINVAL;
  366. }
  367. rv = svga_check_timings (&s3_timing_regs, var, info->node);
  368. if (rv < 0) {
  369. printk(KERN_ERR "fb%d: invalid timings requested\n", info->node);
  370. return rv;
  371. }
  372. rv = svga_compute_pll(&s3_pll, PICOS2KHZ(var->pixclock), &m, &n, &r,
  373. info->node);
  374. if (rv < 0) {
  375. printk(KERN_ERR "fb%d: invalid pixclock value requested\n",
  376. info->node);
  377. return rv;
  378. }
  379. return 0;
  380. }
  381. /* Set video mode from par */
  382. static int s3fb_set_par(struct fb_info *info)
  383. {
  384. struct s3fb_info *par = info->par;
  385. u32 value, mode, hmul, offset_value, screen_size, multiplex, dbytes;
  386. u32 bpp = info->var.bits_per_pixel;
  387. if (bpp != 0) {
  388. info->fix.ypanstep = 1;
  389. info->fix.line_length = (info->var.xres_virtual * bpp) / 8;
  390. info->flags &= ~FBINFO_MISC_TILEBLITTING;
  391. info->tileops = NULL;
  392. /* in 4bpp supports 8p wide tiles only, any tiles otherwise */
  393. info->pixmap.blit_x = (bpp == 4) ? (1 << (8 - 1)) : (~(u32)0);
  394. info->pixmap.blit_y = ~(u32)0;
  395. offset_value = (info->var.xres_virtual * bpp) / 64;
  396. screen_size = info->var.yres_virtual * info->fix.line_length;
  397. } else {
  398. info->fix.ypanstep = 16;
  399. info->fix.line_length = 0;
  400. info->flags |= FBINFO_MISC_TILEBLITTING;
  401. info->tileops = fasttext ? &s3fb_fast_tile_ops : &s3fb_tile_ops;
  402. /* supports 8x16 tiles only */
  403. info->pixmap.blit_x = 1 << (8 - 1);
  404. info->pixmap.blit_y = 1 << (16 - 1);
  405. offset_value = info->var.xres_virtual / 16;
  406. screen_size = (info->var.xres_virtual * info->var.yres_virtual) / 64;
  407. }
  408. info->var.xoffset = 0;
  409. info->var.yoffset = 0;
  410. info->var.activate = FB_ACTIVATE_NOW;
  411. /* Unlock registers */
  412. vga_wcrt(NULL, 0x38, 0x48);
  413. vga_wcrt(NULL, 0x39, 0xA5);
  414. vga_wseq(NULL, 0x08, 0x06);
  415. svga_wcrt_mask(par->state.vgabase, 0x11, 0x00, 0x80);
  416. /* Blank screen and turn off sync */
  417. svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
  418. svga_wcrt_mask(par->state.vgabase, 0x17, 0x00, 0x80);
  419. /* Set default values */
  420. svga_set_default_gfx_regs(par->state.vgabase);
  421. svga_set_default_atc_regs(par->state.vgabase);
  422. svga_set_default_seq_regs(par->state.vgabase);
  423. svga_set_default_crt_regs(par->state.vgabase);
  424. svga_wcrt_multi(par->state.vgabase, s3_line_compare_regs, 0xFFFFFFFF);
  425. svga_wcrt_multi(par->state.vgabase, s3_start_address_regs, 0);
  426. /* S3 specific initialization */
  427. svga_wcrt_mask(par->state.vgabase, 0x58, 0x10, 0x10); /* enable linear framebuffer */
  428. svga_wcrt_mask(par->state.vgabase, 0x31, 0x08, 0x08); /* enable sequencer access to framebuffer above 256 kB */
  429. /* svga_wcrt_mask(par->state.vgabase, 0x33, 0x08, 0x08); */ /* DDR ? */
  430. /* svga_wcrt_mask(par->state.vgabase, 0x43, 0x01, 0x01); */ /* DDR ? */
  431. svga_wcrt_mask(par->state.vgabase, 0x33, 0x00, 0x08); /* no DDR ? */
  432. svga_wcrt_mask(par->state.vgabase, 0x43, 0x00, 0x01); /* no DDR ? */
  433. svga_wcrt_mask(par->state.vgabase, 0x5D, 0x00, 0x28); /* Clear strange HSlen bits */
  434. /* svga_wcrt_mask(par->state.vgabase, 0x58, 0x03, 0x03); */
  435. /* svga_wcrt_mask(par->state.vgabase, 0x53, 0x12, 0x13); */ /* enable MMIO */
  436. /* svga_wcrt_mask(par->state.vgabase, 0x40, 0x08, 0x08); */ /* enable write buffer */
  437. /* Set the offset register */
  438. pr_debug("fb%d: offset register : %d\n", info->node, offset_value);
  439. svga_wcrt_multi(par->state.vgabase, s3_offset_regs, offset_value);
  440. if (par->chip != CHIP_360_TRIO3D_1X &&
  441. par->chip != CHIP_362_TRIO3D_2X &&
  442. par->chip != CHIP_368_TRIO3D_2X) {
  443. vga_wcrt(NULL, 0x54, 0x18); /* M parameter */
  444. vga_wcrt(NULL, 0x60, 0xff); /* N parameter */
  445. vga_wcrt(NULL, 0x61, 0xff); /* L parameter */
  446. vga_wcrt(NULL, 0x62, 0xff); /* L parameter */
  447. }
  448. vga_wcrt(NULL, 0x3A, 0x35);
  449. svga_wattr(par->state.vgabase, 0x33, 0x00);
  450. if (info->var.vmode & FB_VMODE_DOUBLE)
  451. svga_wcrt_mask(par->state.vgabase, 0x09, 0x80, 0x80);
  452. else
  453. svga_wcrt_mask(par->state.vgabase, 0x09, 0x00, 0x80);
  454. if (info->var.vmode & FB_VMODE_INTERLACED)
  455. svga_wcrt_mask(par->state.vgabase, 0x42, 0x20, 0x20);
  456. else
  457. svga_wcrt_mask(par->state.vgabase, 0x42, 0x00, 0x20);
  458. /* Disable hardware graphics cursor */
  459. svga_wcrt_mask(par->state.vgabase, 0x45, 0x00, 0x01);
  460. /* Disable Streams engine */
  461. svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0x0C);
  462. mode = svga_match_format(s3fb_formats, &(info->var), &(info->fix));
  463. /* S3 virge DX hack */
  464. if (par->chip == CHIP_375_VIRGE_DX) {
  465. vga_wcrt(NULL, 0x86, 0x80);
  466. vga_wcrt(NULL, 0x90, 0x00);
  467. }
  468. /* S3 virge VX hack */
  469. if (par->chip == CHIP_988_VIRGE_VX) {
  470. vga_wcrt(NULL, 0x50, 0x00);
  471. vga_wcrt(NULL, 0x67, 0x50);
  472. vga_wcrt(NULL, 0x63, (mode <= 2) ? 0x90 : 0x09);
  473. vga_wcrt(NULL, 0x66, 0x90);
  474. }
  475. if (par->chip == CHIP_360_TRIO3D_1X ||
  476. par->chip == CHIP_362_TRIO3D_2X ||
  477. par->chip == CHIP_368_TRIO3D_2X) {
  478. dbytes = info->var.xres * ((bpp+7)/8);
  479. vga_wcrt(NULL, 0x91, (dbytes + 7) / 8);
  480. vga_wcrt(NULL, 0x90, (((dbytes + 7) / 8) >> 8) | 0x80);
  481. vga_wcrt(NULL, 0x66, 0x81);
  482. }
  483. svga_wcrt_mask(par->state.vgabase, 0x31, 0x00, 0x40);
  484. multiplex = 0;
  485. hmul = 1;
  486. /* Set mode-specific register values */
  487. switch (mode) {
  488. case 0:
  489. pr_debug("fb%d: text mode\n", info->node);
  490. svga_set_textmode_vga_regs(par->state.vgabase);
  491. /* Set additional registers like in 8-bit mode */
  492. svga_wcrt_mask(par->state.vgabase, 0x50, 0x00, 0x30);
  493. svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0xF0);
  494. /* Disable enhanced mode */
  495. svga_wcrt_mask(par->state.vgabase, 0x3A, 0x00, 0x30);
  496. if (fasttext) {
  497. pr_debug("fb%d: high speed text mode set\n", info->node);
  498. svga_wcrt_mask(par->state.vgabase, 0x31, 0x40, 0x40);
  499. }
  500. break;
  501. case 1:
  502. pr_debug("fb%d: 4 bit pseudocolor\n", info->node);
  503. vga_wgfx(NULL, VGA_GFX_MODE, 0x40);
  504. /* Set additional registers like in 8-bit mode */
  505. svga_wcrt_mask(par->state.vgabase, 0x50, 0x00, 0x30);
  506. svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0xF0);
  507. /* disable enhanced mode */
  508. svga_wcrt_mask(par->state.vgabase, 0x3A, 0x00, 0x30);
  509. break;
  510. case 2:
  511. pr_debug("fb%d: 4 bit pseudocolor, planar\n", info->node);
  512. /* Set additional registers like in 8-bit mode */
  513. svga_wcrt_mask(par->state.vgabase, 0x50, 0x00, 0x30);
  514. svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0xF0);
  515. /* disable enhanced mode */
  516. svga_wcrt_mask(par->state.vgabase, 0x3A, 0x00, 0x30);
  517. break;
  518. case 3:
  519. pr_debug("fb%d: 8 bit pseudocolor\n", info->node);
  520. svga_wcrt_mask(par->state.vgabase, 0x50, 0x00, 0x30);
  521. if (info->var.pixclock > 20000 ||
  522. par->chip == CHIP_360_TRIO3D_1X ||
  523. par->chip == CHIP_362_TRIO3D_2X ||
  524. par->chip == CHIP_368_TRIO3D_2X)
  525. svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0xF0);
  526. else {
  527. svga_wcrt_mask(par->state.vgabase, 0x67, 0x10, 0xF0);
  528. multiplex = 1;
  529. }
  530. break;
  531. case 4:
  532. pr_debug("fb%d: 5/5/5 truecolor\n", info->node);
  533. if (par->chip == CHIP_988_VIRGE_VX) {
  534. if (info->var.pixclock > 20000)
  535. svga_wcrt_mask(par->state.vgabase, 0x67, 0x20, 0xF0);
  536. else
  537. svga_wcrt_mask(par->state.vgabase, 0x67, 0x30, 0xF0);
  538. } else {
  539. svga_wcrt_mask(par->state.vgabase, 0x50, 0x10, 0x30);
  540. svga_wcrt_mask(par->state.vgabase, 0x67, 0x30, 0xF0);
  541. if (par->chip != CHIP_360_TRIO3D_1X &&
  542. par->chip != CHIP_362_TRIO3D_2X &&
  543. par->chip != CHIP_368_TRIO3D_2X)
  544. hmul = 2;
  545. }
  546. break;
  547. case 5:
  548. pr_debug("fb%d: 5/6/5 truecolor\n", info->node);
  549. if (par->chip == CHIP_988_VIRGE_VX) {
  550. if (info->var.pixclock > 20000)
  551. svga_wcrt_mask(par->state.vgabase, 0x67, 0x40, 0xF0);
  552. else
  553. svga_wcrt_mask(par->state.vgabase, 0x67, 0x50, 0xF0);
  554. } else {
  555. svga_wcrt_mask(par->state.vgabase, 0x50, 0x10, 0x30);
  556. svga_wcrt_mask(par->state.vgabase, 0x67, 0x50, 0xF0);
  557. if (par->chip != CHIP_360_TRIO3D_1X &&
  558. par->chip != CHIP_362_TRIO3D_2X &&
  559. par->chip != CHIP_368_TRIO3D_2X)
  560. hmul = 2;
  561. }
  562. break;
  563. case 6:
  564. /* VIRGE VX case */
  565. pr_debug("fb%d: 8/8/8 truecolor\n", info->node);
  566. svga_wcrt_mask(par->state.vgabase, 0x67, 0xD0, 0xF0);
  567. break;
  568. case 7:
  569. pr_debug("fb%d: 8/8/8/8 truecolor\n", info->node);
  570. svga_wcrt_mask(par->state.vgabase, 0x50, 0x30, 0x30);
  571. svga_wcrt_mask(par->state.vgabase, 0x67, 0xD0, 0xF0);
  572. break;
  573. default:
  574. printk(KERN_ERR "fb%d: unsupported mode - bug\n", info->node);
  575. return -EINVAL;
  576. }
  577. if (par->chip != CHIP_988_VIRGE_VX) {
  578. svga_wseq_mask(par->state.vgabase, 0x15, multiplex ? 0x10 : 0x00, 0x10);
  579. svga_wseq_mask(par->state.vgabase, 0x18, multiplex ? 0x80 : 0x00, 0x80);
  580. }
  581. s3_set_pixclock(info, info->var.pixclock);
  582. svga_set_timings(par->state.vgabase, &s3_timing_regs, &(info->var), hmul, 1,
  583. (info->var.vmode & FB_VMODE_DOUBLE) ? 2 : 1,
  584. (info->var.vmode & FB_VMODE_INTERLACED) ? 2 : 1,
  585. hmul, info->node);
  586. /* Set interlaced mode start/end register */
  587. value = info->var.xres + info->var.left_margin + info->var.right_margin + info->var.hsync_len;
  588. value = ((value * hmul) / 8) - 5;
  589. vga_wcrt(NULL, 0x3C, (value + 1) / 2);
  590. memset_io(info->screen_base, 0x00, screen_size);
  591. /* Device and screen back on */
  592. svga_wcrt_mask(par->state.vgabase, 0x17, 0x80, 0x80);
  593. svga_wseq_mask(par->state.vgabase, 0x01, 0x00, 0x20);
  594. return 0;
  595. }
  596. /* Set a colour register */
  597. static int s3fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  598. u_int transp, struct fb_info *fb)
  599. {
  600. switch (fb->var.bits_per_pixel) {
  601. case 0:
  602. case 4:
  603. if (regno >= 16)
  604. return -EINVAL;
  605. if ((fb->var.bits_per_pixel == 4) &&
  606. (fb->var.nonstd == 0)) {
  607. outb(0xF0, VGA_PEL_MSK);
  608. outb(regno*16, VGA_PEL_IW);
  609. } else {
  610. outb(0x0F, VGA_PEL_MSK);
  611. outb(regno, VGA_PEL_IW);
  612. }
  613. outb(red >> 10, VGA_PEL_D);
  614. outb(green >> 10, VGA_PEL_D);
  615. outb(blue >> 10, VGA_PEL_D);
  616. break;
  617. case 8:
  618. if (regno >= 256)
  619. return -EINVAL;
  620. outb(0xFF, VGA_PEL_MSK);
  621. outb(regno, VGA_PEL_IW);
  622. outb(red >> 10, VGA_PEL_D);
  623. outb(green >> 10, VGA_PEL_D);
  624. outb(blue >> 10, VGA_PEL_D);
  625. break;
  626. case 16:
  627. if (regno >= 16)
  628. return 0;
  629. if (fb->var.green.length == 5)
  630. ((u32*)fb->pseudo_palette)[regno] = ((red & 0xF800) >> 1) |
  631. ((green & 0xF800) >> 6) | ((blue & 0xF800) >> 11);
  632. else if (fb->var.green.length == 6)
  633. ((u32*)fb->pseudo_palette)[regno] = (red & 0xF800) |
  634. ((green & 0xFC00) >> 5) | ((blue & 0xF800) >> 11);
  635. else return -EINVAL;
  636. break;
  637. case 24:
  638. case 32:
  639. if (regno >= 16)
  640. return 0;
  641. ((u32*)fb->pseudo_palette)[regno] = ((red & 0xFF00) << 8) |
  642. (green & 0xFF00) | ((blue & 0xFF00) >> 8);
  643. break;
  644. default:
  645. return -EINVAL;
  646. }
  647. return 0;
  648. }
  649. /* Set the display blanking state */
  650. static int s3fb_blank(int blank_mode, struct fb_info *info)
  651. {
  652. struct s3fb_info *par = info->par;
  653. switch (blank_mode) {
  654. case FB_BLANK_UNBLANK:
  655. pr_debug("fb%d: unblank\n", info->node);
  656. svga_wcrt_mask(par->state.vgabase, 0x56, 0x00, 0x06);
  657. svga_wseq_mask(par->state.vgabase, 0x01, 0x00, 0x20);
  658. break;
  659. case FB_BLANK_NORMAL:
  660. pr_debug("fb%d: blank\n", info->node);
  661. svga_wcrt_mask(par->state.vgabase, 0x56, 0x00, 0x06);
  662. svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
  663. break;
  664. case FB_BLANK_HSYNC_SUSPEND:
  665. pr_debug("fb%d: hsync\n", info->node);
  666. svga_wcrt_mask(par->state.vgabase, 0x56, 0x02, 0x06);
  667. svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
  668. break;
  669. case FB_BLANK_VSYNC_SUSPEND:
  670. pr_debug("fb%d: vsync\n", info->node);
  671. svga_wcrt_mask(par->state.vgabase, 0x56, 0x04, 0x06);
  672. svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
  673. break;
  674. case FB_BLANK_POWERDOWN:
  675. pr_debug("fb%d: sync down\n", info->node);
  676. svga_wcrt_mask(par->state.vgabase, 0x56, 0x06, 0x06);
  677. svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
  678. break;
  679. }
  680. return 0;
  681. }
  682. /* Pan the display */
  683. static int s3fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  684. {
  685. struct s3fb_info *par = info->par;
  686. unsigned int offset;
  687. /* Calculate the offset */
  688. if (var->bits_per_pixel == 0) {
  689. offset = (var->yoffset / 16) * (var->xres_virtual / 2) + (var->xoffset / 2);
  690. offset = offset >> 2;
  691. } else {
  692. offset = (var->yoffset * info->fix.line_length) +
  693. (var->xoffset * var->bits_per_pixel / 8);
  694. offset = offset >> 2;
  695. }
  696. /* Set the offset */
  697. svga_wcrt_multi(par->state.vgabase, s3_start_address_regs, offset);
  698. return 0;
  699. }
  700. /* ------------------------------------------------------------------------- */
  701. /* Frame buffer operations */
  702. static struct fb_ops s3fb_ops = {
  703. .owner = THIS_MODULE,
  704. .fb_open = s3fb_open,
  705. .fb_release = s3fb_release,
  706. .fb_check_var = s3fb_check_var,
  707. .fb_set_par = s3fb_set_par,
  708. .fb_setcolreg = s3fb_setcolreg,
  709. .fb_blank = s3fb_blank,
  710. .fb_pan_display = s3fb_pan_display,
  711. .fb_fillrect = s3fb_fillrect,
  712. .fb_copyarea = cfb_copyarea,
  713. .fb_imageblit = s3fb_imageblit,
  714. .fb_get_caps = svga_get_caps,
  715. };
  716. /* ------------------------------------------------------------------------- */
  717. static int __devinit s3_identification(int chip)
  718. {
  719. if (chip == CHIP_XXX_TRIO) {
  720. u8 cr30 = vga_rcrt(NULL, 0x30);
  721. u8 cr2e = vga_rcrt(NULL, 0x2e);
  722. u8 cr2f = vga_rcrt(NULL, 0x2f);
  723. if ((cr30 == 0xE0) || (cr30 == 0xE1)) {
  724. if (cr2e == 0x10)
  725. return CHIP_732_TRIO32;
  726. if (cr2e == 0x11) {
  727. if (! (cr2f & 0x40))
  728. return CHIP_764_TRIO64;
  729. else
  730. return CHIP_765_TRIO64VP;
  731. }
  732. }
  733. }
  734. if (chip == CHIP_XXX_TRIO64V2_DXGX) {
  735. u8 cr6f = vga_rcrt(NULL, 0x6f);
  736. if (! (cr6f & 0x01))
  737. return CHIP_775_TRIO64V2_DX;
  738. else
  739. return CHIP_785_TRIO64V2_GX;
  740. }
  741. if (chip == CHIP_XXX_VIRGE_DXGX) {
  742. u8 cr6f = vga_rcrt(NULL, 0x6f);
  743. if (! (cr6f & 0x01))
  744. return CHIP_375_VIRGE_DX;
  745. else
  746. return CHIP_385_VIRGE_GX;
  747. }
  748. if (chip == CHIP_36X_TRIO3D_1X_2X) {
  749. switch (vga_rcrt(NULL, 0x2f)) {
  750. case 0x00:
  751. return CHIP_360_TRIO3D_1X;
  752. case 0x01:
  753. return CHIP_362_TRIO3D_2X;
  754. case 0x02:
  755. return CHIP_368_TRIO3D_2X;
  756. }
  757. }
  758. return CHIP_UNKNOWN;
  759. }
  760. /* PCI probe */
  761. static int __devinit s3_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  762. {
  763. struct fb_info *info;
  764. struct s3fb_info *par;
  765. int rc;
  766. u8 regval, cr38, cr39;
  767. /* Ignore secondary VGA device because there is no VGA arbitration */
  768. if (! svga_primary_device(dev)) {
  769. dev_info(&(dev->dev), "ignoring secondary device\n");
  770. return -ENODEV;
  771. }
  772. /* Allocate and fill driver data structure */
  773. info = framebuffer_alloc(sizeof(struct s3fb_info), &(dev->dev));
  774. if (!info) {
  775. dev_err(&(dev->dev), "cannot allocate memory\n");
  776. return -ENOMEM;
  777. }
  778. par = info->par;
  779. mutex_init(&par->open_lock);
  780. info->flags = FBINFO_PARTIAL_PAN_OK | FBINFO_HWACCEL_YPAN;
  781. info->fbops = &s3fb_ops;
  782. /* Prepare PCI device */
  783. rc = pci_enable_device(dev);
  784. if (rc < 0) {
  785. dev_err(info->device, "cannot enable PCI device\n");
  786. goto err_enable_device;
  787. }
  788. rc = pci_request_regions(dev, "s3fb");
  789. if (rc < 0) {
  790. dev_err(info->device, "cannot reserve framebuffer region\n");
  791. goto err_request_regions;
  792. }
  793. info->fix.smem_start = pci_resource_start(dev, 0);
  794. info->fix.smem_len = pci_resource_len(dev, 0);
  795. /* Map physical IO memory address into kernel space */
  796. info->screen_base = pci_iomap(dev, 0, 0);
  797. if (! info->screen_base) {
  798. rc = -ENOMEM;
  799. dev_err(info->device, "iomap for framebuffer failed\n");
  800. goto err_iomap;
  801. }
  802. /* Unlock regs */
  803. cr38 = vga_rcrt(NULL, 0x38);
  804. cr39 = vga_rcrt(NULL, 0x39);
  805. vga_wseq(NULL, 0x08, 0x06);
  806. vga_wcrt(NULL, 0x38, 0x48);
  807. vga_wcrt(NULL, 0x39, 0xA5);
  808. /* Identify chip type */
  809. par->chip = id->driver_data & CHIP_MASK;
  810. par->rev = vga_rcrt(NULL, 0x2f);
  811. if (par->chip & CHIP_UNDECIDED_FLAG)
  812. par->chip = s3_identification(par->chip);
  813. /* Find how many physical memory there is on card */
  814. /* 0x36 register is accessible even if other registers are locked */
  815. regval = vga_rcrt(NULL, 0x36);
  816. if (par->chip == CHIP_360_TRIO3D_1X ||
  817. par->chip == CHIP_362_TRIO3D_2X ||
  818. par->chip == CHIP_368_TRIO3D_2X) {
  819. switch ((regval & 0xE0) >> 5) {
  820. case 0: /* 8MB -- only 4MB usable for display */
  821. case 1: /* 4MB with 32-bit bus */
  822. case 2: /* 4MB */
  823. info->screen_size = 4 << 20;
  824. break;
  825. case 6: /* 2MB */
  826. info->screen_size = 2 << 20;
  827. break;
  828. }
  829. } else
  830. info->screen_size = s3_memsizes[regval >> 5] << 10;
  831. info->fix.smem_len = info->screen_size;
  832. /* Find MCLK frequency */
  833. regval = vga_rseq(NULL, 0x10);
  834. par->mclk_freq = ((vga_rseq(NULL, 0x11) + 2) * 14318) / ((regval & 0x1F) + 2);
  835. par->mclk_freq = par->mclk_freq >> (regval >> 5);
  836. /* Restore locks */
  837. vga_wcrt(NULL, 0x38, cr38);
  838. vga_wcrt(NULL, 0x39, cr39);
  839. strcpy(info->fix.id, s3_names [par->chip]);
  840. info->fix.mmio_start = 0;
  841. info->fix.mmio_len = 0;
  842. info->fix.type = FB_TYPE_PACKED_PIXELS;
  843. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  844. info->fix.ypanstep = 0;
  845. info->fix.accel = FB_ACCEL_NONE;
  846. info->pseudo_palette = (void*) (par->pseudo_palette);
  847. /* Prepare startup mode */
  848. rc = fb_find_mode(&(info->var), info, mode_option, NULL, 0, NULL, 8);
  849. if (! ((rc == 1) || (rc == 2))) {
  850. rc = -EINVAL;
  851. dev_err(info->device, "mode %s not found\n", mode_option);
  852. goto err_find_mode;
  853. }
  854. rc = fb_alloc_cmap(&info->cmap, 256, 0);
  855. if (rc < 0) {
  856. dev_err(info->device, "cannot allocate colormap\n");
  857. goto err_alloc_cmap;
  858. }
  859. rc = register_framebuffer(info);
  860. if (rc < 0) {
  861. dev_err(info->device, "cannot register framebuffer\n");
  862. goto err_reg_fb;
  863. }
  864. printk(KERN_INFO "fb%d: %s on %s, %d MB RAM, %d MHz MCLK\n", info->node, info->fix.id,
  865. pci_name(dev), info->fix.smem_len >> 20, (par->mclk_freq + 500) / 1000);
  866. if (par->chip == CHIP_UNKNOWN)
  867. printk(KERN_INFO "fb%d: unknown chip, CR2D=%x, CR2E=%x, CRT2F=%x, CRT30=%x\n",
  868. info->node, vga_rcrt(NULL, 0x2d), vga_rcrt(NULL, 0x2e),
  869. vga_rcrt(NULL, 0x2f), vga_rcrt(NULL, 0x30));
  870. /* Record a reference to the driver data */
  871. pci_set_drvdata(dev, info);
  872. #ifdef CONFIG_MTRR
  873. if (mtrr) {
  874. par->mtrr_reg = -1;
  875. par->mtrr_reg = mtrr_add(info->fix.smem_start, info->fix.smem_len, MTRR_TYPE_WRCOMB, 1);
  876. }
  877. #endif
  878. return 0;
  879. /* Error handling */
  880. err_reg_fb:
  881. fb_dealloc_cmap(&info->cmap);
  882. err_alloc_cmap:
  883. err_find_mode:
  884. pci_iounmap(dev, info->screen_base);
  885. err_iomap:
  886. pci_release_regions(dev);
  887. err_request_regions:
  888. /* pci_disable_device(dev); */
  889. err_enable_device:
  890. framebuffer_release(info);
  891. return rc;
  892. }
  893. /* PCI remove */
  894. static void __devexit s3_pci_remove(struct pci_dev *dev)
  895. {
  896. struct fb_info *info = pci_get_drvdata(dev);
  897. if (info) {
  898. #ifdef CONFIG_MTRR
  899. struct s3fb_info *par = info->par;
  900. if (par->mtrr_reg >= 0) {
  901. mtrr_del(par->mtrr_reg, 0, 0);
  902. par->mtrr_reg = -1;
  903. }
  904. #endif
  905. unregister_framebuffer(info);
  906. fb_dealloc_cmap(&info->cmap);
  907. pci_iounmap(dev, info->screen_base);
  908. pci_release_regions(dev);
  909. /* pci_disable_device(dev); */
  910. pci_set_drvdata(dev, NULL);
  911. framebuffer_release(info);
  912. }
  913. }
  914. /* PCI suspend */
  915. static int s3_pci_suspend(struct pci_dev* dev, pm_message_t state)
  916. {
  917. struct fb_info *info = pci_get_drvdata(dev);
  918. struct s3fb_info *par = info->par;
  919. dev_info(info->device, "suspend\n");
  920. console_lock();
  921. mutex_lock(&(par->open_lock));
  922. if ((state.event == PM_EVENT_FREEZE) || (par->ref_count == 0)) {
  923. mutex_unlock(&(par->open_lock));
  924. console_unlock();
  925. return 0;
  926. }
  927. fb_set_suspend(info, 1);
  928. pci_save_state(dev);
  929. pci_disable_device(dev);
  930. pci_set_power_state(dev, pci_choose_state(dev, state));
  931. mutex_unlock(&(par->open_lock));
  932. console_unlock();
  933. return 0;
  934. }
  935. /* PCI resume */
  936. static int s3_pci_resume(struct pci_dev* dev)
  937. {
  938. struct fb_info *info = pci_get_drvdata(dev);
  939. struct s3fb_info *par = info->par;
  940. int err;
  941. dev_info(info->device, "resume\n");
  942. console_lock();
  943. mutex_lock(&(par->open_lock));
  944. if (par->ref_count == 0) {
  945. mutex_unlock(&(par->open_lock));
  946. console_unlock();
  947. return 0;
  948. }
  949. pci_set_power_state(dev, PCI_D0);
  950. pci_restore_state(dev);
  951. err = pci_enable_device(dev);
  952. if (err) {
  953. mutex_unlock(&(par->open_lock));
  954. console_unlock();
  955. dev_err(info->device, "error %d enabling device for resume\n", err);
  956. return err;
  957. }
  958. pci_set_master(dev);
  959. s3fb_set_par(info);
  960. fb_set_suspend(info, 0);
  961. mutex_unlock(&(par->open_lock));
  962. console_unlock();
  963. return 0;
  964. }
  965. /* List of boards that we are trying to support */
  966. static struct pci_device_id s3_devices[] __devinitdata = {
  967. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8810), .driver_data = CHIP_XXX_TRIO},
  968. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8811), .driver_data = CHIP_XXX_TRIO},
  969. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8812), .driver_data = CHIP_M65_AURORA64VP},
  970. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8814), .driver_data = CHIP_767_TRIO64UVP},
  971. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8901), .driver_data = CHIP_XXX_TRIO64V2_DXGX},
  972. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8902), .driver_data = CHIP_551_PLATO_PX},
  973. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x5631), .driver_data = CHIP_325_VIRGE},
  974. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x883D), .driver_data = CHIP_988_VIRGE_VX},
  975. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A01), .driver_data = CHIP_XXX_VIRGE_DXGX},
  976. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A10), .driver_data = CHIP_356_VIRGE_GX2},
  977. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A11), .driver_data = CHIP_357_VIRGE_GX2P},
  978. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A12), .driver_data = CHIP_359_VIRGE_GX2P},
  979. {PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A13), .driver_data = CHIP_36X_TRIO3D_1X_2X},
  980. {0, 0, 0, 0, 0, 0, 0}
  981. };
  982. MODULE_DEVICE_TABLE(pci, s3_devices);
  983. static struct pci_driver s3fb_pci_driver = {
  984. .name = "s3fb",
  985. .id_table = s3_devices,
  986. .probe = s3_pci_probe,
  987. .remove = __devexit_p(s3_pci_remove),
  988. .suspend = s3_pci_suspend,
  989. .resume = s3_pci_resume,
  990. };
  991. /* Parse user speficied options */
  992. #ifndef MODULE
  993. static int __init s3fb_setup(char *options)
  994. {
  995. char *opt;
  996. if (!options || !*options)
  997. return 0;
  998. while ((opt = strsep(&options, ",")) != NULL) {
  999. if (!*opt)
  1000. continue;
  1001. #ifdef CONFIG_MTRR
  1002. else if (!strncmp(opt, "mtrr:", 5))
  1003. mtrr = simple_strtoul(opt + 5, NULL, 0);
  1004. #endif
  1005. else if (!strncmp(opt, "fasttext:", 9))
  1006. fasttext = simple_strtoul(opt + 9, NULL, 0);
  1007. else
  1008. mode_option = opt;
  1009. }
  1010. return 0;
  1011. }
  1012. #endif
  1013. /* Cleanup */
  1014. static void __exit s3fb_cleanup(void)
  1015. {
  1016. pr_debug("s3fb: cleaning up\n");
  1017. pci_unregister_driver(&s3fb_pci_driver);
  1018. }
  1019. /* Driver Initialisation */
  1020. static int __init s3fb_init(void)
  1021. {
  1022. #ifndef MODULE
  1023. char *option = NULL;
  1024. if (fb_get_options("s3fb", &option))
  1025. return -ENODEV;
  1026. s3fb_setup(option);
  1027. #endif
  1028. pr_debug("s3fb: initializing\n");
  1029. return pci_register_driver(&s3fb_pci_driver);
  1030. }
  1031. /* ------------------------------------------------------------------------- */
  1032. /* Modularization */
  1033. module_init(s3fb_init);
  1034. module_exit(s3fb_cleanup);