vt8623fb.c 26 KB

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