vt8623fb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * linux/drivers/video/vt8623fb.c - fbdev driver for
  3. * integrated graphic core in VIA VT8623 [CLE266] chipset
  4. *
  5. * Copyright (c) 2006-2007 Ondrej Zajicek <santiago@crfreenet.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive for
  9. * more details.
  10. *
  11. * Code is based on s3fb, some parts are from David Boucher's viafb
  12. * (http://davesdomain.org.uk/viafb/)
  13. */
  14. #include <linux/version.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/tty.h>
  21. #include <linux/slab.h>
  22. #include <linux/delay.h>
  23. #include <linux/fb.h>
  24. #include <linux/svga.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/console.h> /* Why should fb driver call console functions? because acquire_console_sem() */
  28. #include <video/vga.h>
  29. #ifdef CONFIG_MTRR
  30. #include <asm/mtrr.h>
  31. #endif
  32. struct vt8623fb_info {
  33. char __iomem *mmio_base;
  34. int mtrr_reg;
  35. struct vgastate state;
  36. struct mutex open_lock;
  37. unsigned int ref_count;
  38. u32 pseudo_palette[16];
  39. };
  40. /* ------------------------------------------------------------------------- */
  41. static const struct svga_fb_format vt8623fb_formats[] = {
  42. { 0, {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0}, 0,
  43. FB_TYPE_TEXT, FB_AUX_TEXT_SVGA_STEP8, FB_VISUAL_PSEUDOCOLOR, 16, 16},
  44. { 4, {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0}, 0,
  45. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_PSEUDOCOLOR, 16, 16},
  46. { 4, {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0}, 1,
  47. FB_TYPE_INTERLEAVED_PLANES, 1, FB_VISUAL_PSEUDOCOLOR, 16, 16},
  48. { 8, {0, 6, 0}, {0, 6, 0}, {0, 6, 0}, {0, 0, 0}, 0,
  49. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_PSEUDOCOLOR, 8, 8},
  50. /* {16, {10, 5, 0}, {5, 5, 0}, {0, 5, 0}, {0, 0, 0}, 0,
  51. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_TRUECOLOR, 4, 4}, */
  52. {16, {11, 5, 0}, {5, 6, 0}, {0, 5, 0}, {0, 0, 0}, 0,
  53. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_TRUECOLOR, 4, 4},
  54. {32, {16, 8, 0}, {8, 8, 0}, {0, 8, 0}, {0, 0, 0}, 0,
  55. FB_TYPE_PACKED_PIXELS, 0, FB_VISUAL_TRUECOLOR, 2, 2},
  56. SVGA_FORMAT_END
  57. };
  58. static const struct svga_pll vt8623_pll = {2, 127, 2, 7, 0, 3,
  59. 60000, 300000, 14318};
  60. /* CRT timing register sets */
  61. static struct vga_regset vt8623_h_total_regs[] = {{0x00, 0, 7}, {0x36, 3, 3}, VGA_REGSET_END};
  62. static struct vga_regset vt8623_h_display_regs[] = {{0x01, 0, 7}, VGA_REGSET_END};
  63. static struct vga_regset vt8623_h_blank_start_regs[] = {{0x02, 0, 7}, VGA_REGSET_END};
  64. static struct vga_regset vt8623_h_blank_end_regs[] = {{0x03, 0, 4}, {0x05, 7, 7}, {0x33, 5, 5}, VGA_REGSET_END};
  65. static struct vga_regset vt8623_h_sync_start_regs[] = {{0x04, 0, 7}, {0x33, 4, 4}, VGA_REGSET_END};
  66. static struct vga_regset vt8623_h_sync_end_regs[] = {{0x05, 0, 4}, VGA_REGSET_END};
  67. static struct vga_regset vt8623_v_total_regs[] = {{0x06, 0, 7}, {0x07, 0, 0}, {0x07, 5, 5}, {0x35, 0, 0}, VGA_REGSET_END};
  68. static struct vga_regset vt8623_v_display_regs[] = {{0x12, 0, 7}, {0x07, 1, 1}, {0x07, 6, 6}, {0x35, 2, 2}, VGA_REGSET_END};
  69. static struct vga_regset vt8623_v_blank_start_regs[] = {{0x15, 0, 7}, {0x07, 3, 3}, {0x09, 5, 5}, {0x35, 3, 3}, VGA_REGSET_END};
  70. static struct vga_regset vt8623_v_blank_end_regs[] = {{0x16, 0, 7}, VGA_REGSET_END};
  71. static struct vga_regset vt8623_v_sync_start_regs[] = {{0x10, 0, 7}, {0x07, 2, 2}, {0x07, 7, 7}, {0x35, 1, 1}, VGA_REGSET_END};
  72. static struct vga_regset vt8623_v_sync_end_regs[] = {{0x11, 0, 3}, VGA_REGSET_END};
  73. static struct vga_regset vt8623_offset_regs[] = {{0x13, 0, 7}, {0x35, 5, 7}, VGA_REGSET_END};
  74. static struct vga_regset vt8623_line_compare_regs[] = {{0x18, 0, 7}, {0x07, 4, 4}, {0x09, 6, 6}, {0x33, 0, 2}, {0x35, 4, 4}, VGA_REGSET_END};
  75. static struct vga_regset vt8623_fetch_count_regs[] = {{0x1C, 0, 7}, {0x1D, 0, 1}, VGA_REGSET_END};
  76. static struct vga_regset vt8623_start_address_regs[] = {{0x0d, 0, 7}, {0x0c, 0, 7}, {0x34, 0, 7}, {0x48, 0, 1}, VGA_REGSET_END};
  77. static struct svga_timing_regs vt8623_timing_regs = {
  78. vt8623_h_total_regs, vt8623_h_display_regs, vt8623_h_blank_start_regs,
  79. vt8623_h_blank_end_regs, vt8623_h_sync_start_regs, vt8623_h_sync_end_regs,
  80. vt8623_v_total_regs, vt8623_v_display_regs, vt8623_v_blank_start_regs,
  81. vt8623_v_blank_end_regs, vt8623_v_sync_start_regs, vt8623_v_sync_end_regs,
  82. };
  83. /* ------------------------------------------------------------------------- */
  84. /* Module parameters */
  85. static char *mode_option = "640x480-8@60";
  86. #ifdef CONFIG_MTRR
  87. static int mtrr = 1;
  88. #endif
  89. MODULE_AUTHOR("(c) 2006 Ondrej Zajicek <santiago@crfreenet.org>");
  90. MODULE_LICENSE("GPL");
  91. MODULE_DESCRIPTION("fbdev driver for integrated graphics core in VIA VT8623 [CLE266]");
  92. module_param(mode_option, charp, 0644);
  93. MODULE_PARM_DESC(mode_option, "Default video mode ('640x480-8@60', etc)");
  94. module_param_named(mode, mode_option, charp, 0);
  95. MODULE_PARM_DESC(mode, "Default video mode e.g. '648x480-8@60' (deprecated)");
  96. #ifdef CONFIG_MTRR
  97. module_param(mtrr, int, 0444);
  98. MODULE_PARM_DESC(mtrr, "Enable write-combining with MTRR (1=enable, 0=disable, default=1)");
  99. #endif
  100. /* ------------------------------------------------------------------------- */
  101. static struct fb_tile_ops vt8623fb_tile_ops = {
  102. .fb_settile = svga_settile,
  103. .fb_tilecopy = svga_tilecopy,
  104. .fb_tilefill = svga_tilefill,
  105. .fb_tileblit = svga_tileblit,
  106. .fb_tilecursor = svga_tilecursor,
  107. .fb_get_tilemax = svga_get_tilemax,
  108. };
  109. /* ------------------------------------------------------------------------- */
  110. /* image data is MSB-first, fb structure is MSB-first too */
  111. static inline u32 expand_color(u32 c)
  112. {
  113. return ((c & 1) | ((c & 2) << 7) | ((c & 4) << 14) | ((c & 8) << 21)) * 0xFF;
  114. }
  115. /* vt8623fb_iplan_imageblit silently assumes that almost everything is 8-pixel aligned */
  116. static void vt8623fb_iplan_imageblit(struct fb_info *info, const struct fb_image *image)
  117. {
  118. u32 fg = expand_color(image->fg_color);
  119. u32 bg = expand_color(image->bg_color);
  120. const u8 *src1, *src;
  121. u8 __iomem *dst1;
  122. u32 __iomem *dst;
  123. u32 val;
  124. int x, y;
  125. src1 = image->data;
  126. dst1 = info->screen_base + (image->dy * info->fix.line_length)
  127. + ((image->dx / 8) * 4);
  128. for (y = 0; y < image->height; y++) {
  129. src = src1;
  130. dst = (u32 __iomem *) dst1;
  131. for (x = 0; x < image->width; x += 8) {
  132. val = *(src++) * 0x01010101;
  133. val = (val & fg) | (~val & bg);
  134. fb_writel(val, dst++);
  135. }
  136. src1 += image->width / 8;
  137. dst1 += info->fix.line_length;
  138. }
  139. }
  140. /* vt8623fb_iplan_fillrect silently assumes that almost everything is 8-pixel aligned */
  141. static void vt8623fb_iplan_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  142. {
  143. u32 fg = expand_color(rect->color);
  144. u8 __iomem *dst1;
  145. u32 __iomem *dst;
  146. int x, y;
  147. dst1 = info->screen_base + (rect->dy * info->fix.line_length)
  148. + ((rect->dx / 8) * 4);
  149. for (y = 0; y < rect->height; y++) {
  150. dst = (u32 __iomem *) dst1;
  151. for (x = 0; x < rect->width; x += 8) {
  152. fb_writel(fg, dst++);
  153. }
  154. dst1 += info->fix.line_length;
  155. }
  156. }
  157. /* image data is MSB-first, fb structure is high-nibble-in-low-byte-first */
  158. static inline u32 expand_pixel(u32 c)
  159. {
  160. return (((c & 1) << 24) | ((c & 2) << 27) | ((c & 4) << 14) | ((c & 8) << 17) |
  161. ((c & 16) << 4) | ((c & 32) << 7) | ((c & 64) >> 6) | ((c & 128) >> 3)) * 0xF;
  162. }
  163. /* vt8623fb_cfb4_imageblit silently assumes that almost everything is 8-pixel aligned */
  164. static void vt8623fb_cfb4_imageblit(struct fb_info *info, const struct fb_image *image)
  165. {
  166. u32 fg = image->fg_color * 0x11111111;
  167. u32 bg = image->bg_color * 0x11111111;
  168. const u8 *src1, *src;
  169. u8 __iomem *dst1;
  170. u32 __iomem *dst;
  171. u32 val;
  172. int x, y;
  173. src1 = image->data;
  174. dst1 = info->screen_base + (image->dy * info->fix.line_length)
  175. + ((image->dx / 8) * 4);
  176. for (y = 0; y < image->height; y++) {
  177. src = src1;
  178. dst = (u32 __iomem *) dst1;
  179. for (x = 0; x < image->width; x += 8) {
  180. val = expand_pixel(*(src++));
  181. val = (val & fg) | (~val & bg);
  182. fb_writel(val, dst++);
  183. }
  184. src1 += image->width / 8;
  185. dst1 += info->fix.line_length;
  186. }
  187. }
  188. static void vt8623fb_imageblit(struct fb_info *info, const struct fb_image *image)
  189. {
  190. if ((info->var.bits_per_pixel == 4) && (image->depth == 1)
  191. && ((image->width % 8) == 0) && ((image->dx % 8) == 0)) {
  192. if (info->fix.type == FB_TYPE_INTERLEAVED_PLANES)
  193. vt8623fb_iplan_imageblit(info, image);
  194. else
  195. vt8623fb_cfb4_imageblit(info, image);
  196. } else
  197. cfb_imageblit(info, image);
  198. }
  199. static void vt8623fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  200. {
  201. if ((info->var.bits_per_pixel == 4)
  202. && ((rect->width % 8) == 0) && ((rect->dx % 8) == 0)
  203. && (info->fix.type == FB_TYPE_INTERLEAVED_PLANES))
  204. vt8623fb_iplan_fillrect(info, rect);
  205. else
  206. cfb_fillrect(info, rect);
  207. }
  208. /* ------------------------------------------------------------------------- */
  209. static void vt8623_set_pixclock(struct fb_info *info, u32 pixclock)
  210. {
  211. u16 m, n, r;
  212. u8 regval;
  213. int rv;
  214. rv = svga_compute_pll(&vt8623_pll, 1000000000 / pixclock, &m, &n, &r, info->node);
  215. if (rv < 0) {
  216. printk(KERN_ERR "fb%d: cannot set requested pixclock, keeping old value\n", info->node);
  217. return;
  218. }
  219. /* Set VGA misc register */
  220. regval = vga_r(NULL, VGA_MIS_R);
  221. vga_w(NULL, VGA_MIS_W, regval | VGA_MIS_ENB_PLL_LOAD);
  222. /* Set clock registers */
  223. vga_wseq(NULL, 0x46, (n | (r << 6)));
  224. vga_wseq(NULL, 0x47, m);
  225. udelay(1000);
  226. /* PLL reset */
  227. svga_wseq_mask(0x40, 0x02, 0x02);
  228. svga_wseq_mask(0x40, 0x00, 0x02);
  229. }
  230. static int vt8623fb_open(struct fb_info *info, int user)
  231. {
  232. struct vt8623fb_info *par = info->par;
  233. mutex_lock(&(par->open_lock));
  234. if (par->ref_count == 0) {
  235. memset(&(par->state), 0, sizeof(struct vgastate));
  236. par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS | VGA_SAVE_CMAP;
  237. par->state.num_crtc = 0xA2;
  238. par->state.num_seq = 0x50;
  239. save_vga(&(par->state));
  240. }
  241. par->ref_count++;
  242. mutex_unlock(&(par->open_lock));
  243. return 0;
  244. }
  245. static int vt8623fb_release(struct fb_info *info, int user)
  246. {
  247. struct vt8623fb_info *par = info->par;
  248. mutex_lock(&(par->open_lock));
  249. if (par->ref_count == 0) {
  250. mutex_unlock(&(par->open_lock));
  251. return -EINVAL;
  252. }
  253. if (par->ref_count == 1)
  254. restore_vga(&(par->state));
  255. par->ref_count--;
  256. mutex_unlock(&(par->open_lock));
  257. return 0;
  258. }
  259. static int vt8623fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  260. {
  261. int rv, mem, step;
  262. /* Find appropriate format */
  263. rv = svga_match_format (vt8623fb_formats, var, NULL);
  264. if (rv < 0)
  265. {
  266. printk(KERN_ERR "fb%d: unsupported mode requested\n", info->node);
  267. return rv;
  268. }
  269. /* Do not allow to have real resoulution larger than virtual */
  270. if (var->xres > var->xres_virtual)
  271. var->xres_virtual = var->xres;
  272. if (var->yres > var->yres_virtual)
  273. var->yres_virtual = var->yres;
  274. /* Round up xres_virtual to have proper alignment of lines */
  275. step = vt8623fb_formats[rv].xresstep - 1;
  276. var->xres_virtual = (var->xres_virtual+step) & ~step;
  277. /* Check whether have enough memory */
  278. mem = ((var->bits_per_pixel * var->xres_virtual) >> 3) * var->yres_virtual;
  279. if (mem > info->screen_size)
  280. {
  281. printk(KERN_ERR "fb%d: not enough framebuffer memory (%d kB requested , %d kB available)\n", info->node, mem >> 10, (unsigned int) (info->screen_size >> 10));
  282. return -EINVAL;
  283. }
  284. /* Text mode is limited to 256 kB of memory */
  285. if ((var->bits_per_pixel == 0) && (mem > (256*1024)))
  286. {
  287. printk(KERN_ERR "fb%d: text framebuffer size too large (%d kB requested, 256 kB possible)\n", info->node, mem >> 10);
  288. return -EINVAL;
  289. }
  290. rv = svga_check_timings (&vt8623_timing_regs, var, info->node);
  291. if (rv < 0)
  292. {
  293. printk(KERN_ERR "fb%d: invalid timings requested\n", info->node);
  294. return rv;
  295. }
  296. /* Interlaced mode not supported */
  297. if (var->vmode & FB_VMODE_INTERLACED)
  298. return -EINVAL;
  299. return 0;
  300. }
  301. static int vt8623fb_set_par(struct fb_info *info)
  302. {
  303. u32 mode, offset_value, fetch_value, screen_size;
  304. u32 bpp = info->var.bits_per_pixel;
  305. if (bpp != 0) {
  306. info->fix.ypanstep = 1;
  307. info->fix.line_length = (info->var.xres_virtual * bpp) / 8;
  308. info->flags &= ~FBINFO_MISC_TILEBLITTING;
  309. info->tileops = NULL;
  310. /* in 4bpp supports 8p wide tiles only, any tiles otherwise */
  311. info->pixmap.blit_x = (bpp == 4) ? (1 << (8 - 1)) : (~(u32)0);
  312. info->pixmap.blit_y = ~(u32)0;
  313. offset_value = (info->var.xres_virtual * bpp) / 64;
  314. fetch_value = ((info->var.xres * bpp) / 128) + 4;
  315. if (bpp == 4)
  316. fetch_value = (info->var.xres / 8) + 8; /* + 0 is OK */
  317. screen_size = info->var.yres_virtual * info->fix.line_length;
  318. } else {
  319. info->fix.ypanstep = 16;
  320. info->fix.line_length = 0;
  321. info->flags |= FBINFO_MISC_TILEBLITTING;
  322. info->tileops = &vt8623fb_tile_ops;
  323. /* supports 8x16 tiles only */
  324. info->pixmap.blit_x = 1 << (8 - 1);
  325. info->pixmap.blit_y = 1 << (16 - 1);
  326. offset_value = info->var.xres_virtual / 16;
  327. fetch_value = (info->var.xres / 8) + 8;
  328. screen_size = (info->var.xres_virtual * info->var.yres_virtual) / 64;
  329. }
  330. info->var.xoffset = 0;
  331. info->var.yoffset = 0;
  332. info->var.activate = FB_ACTIVATE_NOW;
  333. /* Unlock registers */
  334. svga_wseq_mask(0x10, 0x01, 0x01);
  335. svga_wcrt_mask(0x11, 0x00, 0x80);
  336. svga_wcrt_mask(0x47, 0x00, 0x01);
  337. /* Device, screen and sync off */
  338. svga_wseq_mask(0x01, 0x20, 0x20);
  339. svga_wcrt_mask(0x36, 0x30, 0x30);
  340. svga_wcrt_mask(0x17, 0x00, 0x80);
  341. /* Set default values */
  342. svga_set_default_gfx_regs();
  343. svga_set_default_atc_regs();
  344. svga_set_default_seq_regs();
  345. svga_set_default_crt_regs();
  346. svga_wcrt_multi(vt8623_line_compare_regs, 0xFFFFFFFF);
  347. svga_wcrt_multi(vt8623_start_address_regs, 0);
  348. svga_wcrt_multi(vt8623_offset_regs, offset_value);
  349. svga_wseq_multi(vt8623_fetch_count_regs, fetch_value);
  350. /* Clear H/V Skew */
  351. svga_wcrt_mask(0x03, 0x00, 0x60);
  352. svga_wcrt_mask(0x05, 0x00, 0x60);
  353. if (info->var.vmode & FB_VMODE_DOUBLE)
  354. svga_wcrt_mask(0x09, 0x80, 0x80);
  355. else
  356. svga_wcrt_mask(0x09, 0x00, 0x80);
  357. svga_wseq_mask(0x1E, 0xF0, 0xF0); // DI/DVP bus
  358. svga_wseq_mask(0x2A, 0x0F, 0x0F); // DI/DVP bus
  359. svga_wseq_mask(0x16, 0x08, 0xBF); // FIFO read treshold
  360. vga_wseq(NULL, 0x17, 0x1F); // FIFO depth
  361. vga_wseq(NULL, 0x18, 0x4E);
  362. svga_wseq_mask(0x1A, 0x08, 0x08); // enable MMIO ?
  363. vga_wcrt(NULL, 0x32, 0x00);
  364. vga_wcrt(NULL, 0x34, 0x00);
  365. vga_wcrt(NULL, 0x6A, 0x80);
  366. vga_wcrt(NULL, 0x6A, 0xC0);
  367. vga_wgfx(NULL, 0x20, 0x00);
  368. vga_wgfx(NULL, 0x21, 0x00);
  369. vga_wgfx(NULL, 0x22, 0x00);
  370. /* Set SR15 according to number of bits per pixel */
  371. mode = svga_match_format(vt8623fb_formats, &(info->var), &(info->fix));
  372. switch (mode) {
  373. case 0:
  374. pr_debug("fb%d: text mode\n", info->node);
  375. svga_set_textmode_vga_regs();
  376. svga_wseq_mask(0x15, 0x00, 0xFE);
  377. svga_wcrt_mask(0x11, 0x60, 0x70);
  378. break;
  379. case 1:
  380. pr_debug("fb%d: 4 bit pseudocolor\n", info->node);
  381. vga_wgfx(NULL, VGA_GFX_MODE, 0x40);
  382. svga_wseq_mask(0x15, 0x20, 0xFE);
  383. svga_wcrt_mask(0x11, 0x00, 0x70);
  384. break;
  385. case 2:
  386. pr_debug("fb%d: 4 bit pseudocolor, planar\n", info->node);
  387. svga_wseq_mask(0x15, 0x00, 0xFE);
  388. svga_wcrt_mask(0x11, 0x00, 0x70);
  389. break;
  390. case 3:
  391. pr_debug("fb%d: 8 bit pseudocolor\n", info->node);
  392. svga_wseq_mask(0x15, 0x22, 0xFE);
  393. break;
  394. case 4:
  395. pr_debug("fb%d: 5/6/5 truecolor\n", info->node);
  396. svga_wseq_mask(0x15, 0xB6, 0xFE);
  397. break;
  398. case 5:
  399. pr_debug("fb%d: 8/8/8 truecolor\n", info->node);
  400. svga_wseq_mask(0x15, 0xAE, 0xFE);
  401. break;
  402. default:
  403. printk(KERN_ERR "vt8623fb: unsupported mode - bug\n");
  404. return (-EINVAL);
  405. }
  406. vt8623_set_pixclock(info, info->var.pixclock);
  407. svga_set_timings(&vt8623_timing_regs, &(info->var), 1, 1,
  408. (info->var.vmode & FB_VMODE_DOUBLE) ? 2 : 1, 1,
  409. 1, info->node);
  410. memset_io(info->screen_base, 0x00, screen_size);
  411. /* Device and screen back on */
  412. svga_wcrt_mask(0x17, 0x80, 0x80);
  413. svga_wcrt_mask(0x36, 0x00, 0x30);
  414. svga_wseq_mask(0x01, 0x00, 0x20);
  415. return 0;
  416. }
  417. static int vt8623fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  418. u_int transp, struct fb_info *fb)
  419. {
  420. switch (fb->var.bits_per_pixel) {
  421. case 0:
  422. case 4:
  423. if (regno >= 16)
  424. return -EINVAL;
  425. outb(0x0F, VGA_PEL_MSK);
  426. outb(regno, VGA_PEL_IW);
  427. outb(red >> 10, VGA_PEL_D);
  428. outb(green >> 10, VGA_PEL_D);
  429. outb(blue >> 10, VGA_PEL_D);
  430. break;
  431. case 8:
  432. if (regno >= 256)
  433. return -EINVAL;
  434. outb(0xFF, VGA_PEL_MSK);
  435. outb(regno, VGA_PEL_IW);
  436. outb(red >> 10, VGA_PEL_D);
  437. outb(green >> 10, VGA_PEL_D);
  438. outb(blue >> 10, VGA_PEL_D);
  439. break;
  440. case 16:
  441. if (regno >= 16)
  442. return 0;
  443. if (fb->var.green.length == 5)
  444. ((u32*)fb->pseudo_palette)[regno] = ((red & 0xF800) >> 1) |
  445. ((green & 0xF800) >> 6) | ((blue & 0xF800) >> 11);
  446. else if (fb->var.green.length == 6)
  447. ((u32*)fb->pseudo_palette)[regno] = (red & 0xF800) |
  448. ((green & 0xFC00) >> 5) | ((blue & 0xF800) >> 11);
  449. else
  450. return -EINVAL;
  451. break;
  452. case 24:
  453. case 32:
  454. if (regno >= 16)
  455. return 0;
  456. /* ((transp & 0xFF00) << 16) */
  457. ((u32*)fb->pseudo_palette)[regno] = ((red & 0xFF00) << 8) |
  458. (green & 0xFF00) | ((blue & 0xFF00) >> 8);
  459. break;
  460. default:
  461. return -EINVAL;
  462. }
  463. return 0;
  464. }
  465. static int vt8623fb_blank(int blank_mode, struct fb_info *info)
  466. {
  467. switch (blank_mode) {
  468. case FB_BLANK_UNBLANK:
  469. pr_debug("fb%d: unblank\n", info->node);
  470. svga_wcrt_mask(0x36, 0x00, 0x30);
  471. svga_wseq_mask(0x01, 0x00, 0x20);
  472. break;
  473. case FB_BLANK_NORMAL:
  474. pr_debug("fb%d: blank\n", info->node);
  475. svga_wcrt_mask(0x36, 0x00, 0x30);
  476. svga_wseq_mask(0x01, 0x20, 0x20);
  477. break;
  478. case FB_BLANK_HSYNC_SUSPEND:
  479. pr_debug("fb%d: DPMS standby (hsync off)\n", info->node);
  480. svga_wcrt_mask(0x36, 0x10, 0x30);
  481. svga_wseq_mask(0x01, 0x20, 0x20);
  482. break;
  483. case FB_BLANK_VSYNC_SUSPEND:
  484. pr_debug("fb%d: DPMS suspend (vsync off)\n", info->node);
  485. svga_wcrt_mask(0x36, 0x20, 0x30);
  486. svga_wseq_mask(0x01, 0x20, 0x20);
  487. break;
  488. case FB_BLANK_POWERDOWN:
  489. pr_debug("fb%d: DPMS off (no sync)\n", info->node);
  490. svga_wcrt_mask(0x36, 0x30, 0x30);
  491. svga_wseq_mask(0x01, 0x20, 0x20);
  492. break;
  493. }
  494. return 0;
  495. }
  496. static int vt8623fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  497. {
  498. unsigned int offset;
  499. /* Calculate the offset */
  500. if (var->bits_per_pixel == 0) {
  501. offset = (var->yoffset / 16) * var->xres_virtual + var->xoffset;
  502. offset = offset >> 3;
  503. } else {
  504. offset = (var->yoffset * info->fix.line_length) +
  505. (var->xoffset * var->bits_per_pixel / 8);
  506. offset = offset >> ((var->bits_per_pixel == 4) ? 2 : 1);
  507. }
  508. /* Set the offset */
  509. svga_wcrt_multi(vt8623_start_address_regs, offset);
  510. return 0;
  511. }
  512. /* ------------------------------------------------------------------------- */
  513. /* Frame buffer operations */
  514. static struct fb_ops vt8623fb_ops = {
  515. .owner = THIS_MODULE,
  516. .fb_open = vt8623fb_open,
  517. .fb_release = vt8623fb_release,
  518. .fb_check_var = vt8623fb_check_var,
  519. .fb_set_par = vt8623fb_set_par,
  520. .fb_setcolreg = vt8623fb_setcolreg,
  521. .fb_blank = vt8623fb_blank,
  522. .fb_pan_display = vt8623fb_pan_display,
  523. .fb_fillrect = vt8623fb_fillrect,
  524. .fb_copyarea = cfb_copyarea,
  525. .fb_imageblit = vt8623fb_imageblit,
  526. .fb_get_caps = svga_get_caps,
  527. };
  528. /* PCI probe */
  529. static int __devinit vt8623_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  530. {
  531. struct fb_info *info;
  532. struct vt8623fb_info *par;
  533. unsigned int memsize1, memsize2;
  534. int rc;
  535. /* Ignore secondary VGA device because there is no VGA arbitration */
  536. if (! svga_primary_device(dev)) {
  537. dev_info(&(dev->dev), "ignoring secondary device\n");
  538. return -ENODEV;
  539. }
  540. /* Allocate and fill driver data structure */
  541. info = framebuffer_alloc(sizeof(struct vt8623fb_info), &(dev->dev));
  542. if (! info) {
  543. dev_err(&(dev->dev), "cannot allocate memory\n");
  544. return -ENOMEM;
  545. }
  546. par = info->par;
  547. mutex_init(&par->open_lock);
  548. info->flags = FBINFO_PARTIAL_PAN_OK | FBINFO_HWACCEL_YPAN;
  549. info->fbops = &vt8623fb_ops;
  550. /* Prepare PCI device */
  551. rc = pci_enable_device(dev);
  552. if (rc < 0) {
  553. dev_err(info->dev, "cannot enable PCI device\n");
  554. goto err_enable_device;
  555. }
  556. rc = pci_request_regions(dev, "vt8623fb");
  557. if (rc < 0) {
  558. dev_err(info->dev, "cannot reserve framebuffer region\n");
  559. goto err_request_regions;
  560. }
  561. info->fix.smem_start = pci_resource_start(dev, 0);
  562. info->fix.smem_len = pci_resource_len(dev, 0);
  563. info->fix.mmio_start = pci_resource_start(dev, 1);
  564. info->fix.mmio_len = pci_resource_len(dev, 1);
  565. /* Map physical IO memory address into kernel space */
  566. info->screen_base = pci_iomap(dev, 0, 0);
  567. if (! info->screen_base) {
  568. rc = -ENOMEM;
  569. dev_err(info->dev, "iomap for framebuffer failed\n");
  570. goto err_iomap_1;
  571. }
  572. par->mmio_base = pci_iomap(dev, 1, 0);
  573. if (! par->mmio_base) {
  574. rc = -ENOMEM;
  575. dev_err(info->dev, "iomap for MMIO failed\n");
  576. goto err_iomap_2;
  577. }
  578. /* Find how many physical memory there is on card */
  579. memsize1 = (vga_rseq(NULL, 0x34) + 1) >> 1;
  580. memsize2 = vga_rseq(NULL, 0x39) << 2;
  581. if ((16 <= memsize1) && (memsize1 <= 64) && (memsize1 == memsize2))
  582. info->screen_size = memsize1 << 20;
  583. else {
  584. dev_err(info->dev, "memory size detection failed (%x %x), suppose 16 MB\n", memsize1, memsize2);
  585. info->screen_size = 16 << 20;
  586. }
  587. info->fix.smem_len = info->screen_size;
  588. strcpy(info->fix.id, "VIA VT8623");
  589. info->fix.type = FB_TYPE_PACKED_PIXELS;
  590. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  591. info->fix.ypanstep = 0;
  592. info->fix.accel = FB_ACCEL_NONE;
  593. info->pseudo_palette = (void*)par->pseudo_palette;
  594. /* Prepare startup mode */
  595. rc = fb_find_mode(&(info->var), info, mode_option, NULL, 0, NULL, 8);
  596. if (! ((rc == 1) || (rc == 2))) {
  597. rc = -EINVAL;
  598. dev_err(info->dev, "mode %s not found\n", mode_option);
  599. goto err_find_mode;
  600. }
  601. rc = fb_alloc_cmap(&info->cmap, 256, 0);
  602. if (rc < 0) {
  603. dev_err(info->dev, "cannot allocate colormap\n");
  604. goto err_alloc_cmap;
  605. }
  606. rc = register_framebuffer(info);
  607. if (rc < 0) {
  608. dev_err(info->dev, "cannot register framebugger\n");
  609. goto err_reg_fb;
  610. }
  611. printk(KERN_INFO "fb%d: %s on %s, %d MB RAM\n", info->node, info->fix.id,
  612. pci_name(dev), info->fix.smem_len >> 20);
  613. /* Record a reference to the driver data */
  614. pci_set_drvdata(dev, info);
  615. #ifdef CONFIG_MTRR
  616. if (mtrr) {
  617. par->mtrr_reg = -1;
  618. par->mtrr_reg = mtrr_add(info->fix.smem_start, info->fix.smem_len, MTRR_TYPE_WRCOMB, 1);
  619. }
  620. #endif
  621. return 0;
  622. /* Error handling */
  623. err_reg_fb:
  624. fb_dealloc_cmap(&info->cmap);
  625. err_alloc_cmap:
  626. err_find_mode:
  627. pci_iounmap(dev, par->mmio_base);
  628. err_iomap_2:
  629. pci_iounmap(dev, info->screen_base);
  630. err_iomap_1:
  631. pci_release_regions(dev);
  632. err_request_regions:
  633. /* pci_disable_device(dev); */
  634. err_enable_device:
  635. framebuffer_release(info);
  636. return rc;
  637. }
  638. /* PCI remove */
  639. static void __devexit vt8623_pci_remove(struct pci_dev *dev)
  640. {
  641. struct fb_info *info = pci_get_drvdata(dev);
  642. if (info) {
  643. struct vt8623fb_info *par = info->par;
  644. #ifdef CONFIG_MTRR
  645. if (par->mtrr_reg >= 0) {
  646. mtrr_del(par->mtrr_reg, 0, 0);
  647. par->mtrr_reg = -1;
  648. }
  649. #endif
  650. unregister_framebuffer(info);
  651. fb_dealloc_cmap(&info->cmap);
  652. pci_iounmap(dev, info->screen_base);
  653. pci_iounmap(dev, par->mmio_base);
  654. pci_release_regions(dev);
  655. /* pci_disable_device(dev); */
  656. pci_set_drvdata(dev, NULL);
  657. framebuffer_release(info);
  658. }
  659. }
  660. #ifdef CONFIG_PM
  661. /* PCI suspend */
  662. static int vt8623_pci_suspend(struct pci_dev* dev, pm_message_t state)
  663. {
  664. struct fb_info *info = pci_get_drvdata(dev);
  665. struct vt8623fb_info *par = info->par;
  666. dev_info(info->dev, "suspend\n");
  667. acquire_console_sem();
  668. mutex_lock(&(par->open_lock));
  669. if ((state.event == PM_EVENT_FREEZE) || (par->ref_count == 0)) {
  670. mutex_unlock(&(par->open_lock));
  671. release_console_sem();
  672. return 0;
  673. }
  674. fb_set_suspend(info, 1);
  675. pci_save_state(dev);
  676. pci_disable_device(dev);
  677. pci_set_power_state(dev, pci_choose_state(dev, state));
  678. mutex_unlock(&(par->open_lock));
  679. release_console_sem();
  680. return 0;
  681. }
  682. /* PCI resume */
  683. static int vt8623_pci_resume(struct pci_dev* dev)
  684. {
  685. struct fb_info *info = pci_get_drvdata(dev);
  686. struct vt8623fb_info *par = info->par;
  687. dev_info(info->dev, "resume\n");
  688. acquire_console_sem();
  689. mutex_lock(&(par->open_lock));
  690. if (par->ref_count == 0) {
  691. mutex_unlock(&(par->open_lock));
  692. release_console_sem();
  693. return 0;
  694. }
  695. pci_set_power_state(dev, PCI_D0);
  696. pci_restore_state(dev);
  697. if (pci_enable_device(dev))
  698. goto fail;
  699. pci_set_master(dev);
  700. vt8623fb_set_par(info);
  701. fb_set_suspend(info, 0);
  702. mutex_unlock(&(par->open_lock));
  703. fail:
  704. release_console_sem();
  705. return 0;
  706. }
  707. #else
  708. #define vt8623_pci_suspend NULL
  709. #define vt8623_pci_resume NULL
  710. #endif /* CONFIG_PM */
  711. /* List of boards that we are trying to support */
  712. static struct pci_device_id vt8623_devices[] __devinitdata = {
  713. {PCI_DEVICE(PCI_VENDOR_ID_VIA, 0x3122)},
  714. {0, 0, 0, 0, 0, 0, 0}
  715. };
  716. MODULE_DEVICE_TABLE(pci, vt8623_devices);
  717. static struct pci_driver vt8623fb_pci_driver = {
  718. .name = "vt8623fb",
  719. .id_table = vt8623_devices,
  720. .probe = vt8623_pci_probe,
  721. .remove = __devexit_p(vt8623_pci_remove),
  722. .suspend = vt8623_pci_suspend,
  723. .resume = vt8623_pci_resume,
  724. };
  725. /* Cleanup */
  726. static void __exit vt8623fb_cleanup(void)
  727. {
  728. pr_debug("vt8623fb: cleaning up\n");
  729. pci_unregister_driver(&vt8623fb_pci_driver);
  730. }
  731. /* Driver Initialisation */
  732. static int __init vt8623fb_init(void)
  733. {
  734. #ifndef MODULE
  735. char *option = NULL;
  736. if (fb_get_options("vt8623fb", &option))
  737. return -ENODEV;
  738. if (option && *option)
  739. mode_option = option;
  740. #endif
  741. pr_debug("vt8623fb: initializing\n");
  742. return pci_register_driver(&vt8623fb_pci_driver);
  743. }
  744. /* ------------------------------------------------------------------------- */
  745. /* Modularization */
  746. module_init(vt8623fb_init);
  747. module_exit(vt8623fb_cleanup);