vt8623fb.c 26 KB

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