s3fb.c 39 KB

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