s3fb.c 39 KB

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