vt8623fb.c 27 KB

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