fb-puv3.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Frame Buffer Driver for PKUnity-v3 Unigfx
  3. * Code specific to PKUnity SoC and UniCore ISA
  4. *
  5. * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
  6. * Copyright (C) 2001-2010 Guan Xuetao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/clk.h>
  18. #include <linux/fb.h>
  19. #include <linux/init.h>
  20. #include <linux/console.h>
  21. #include <asm/sizes.h>
  22. #include <mach/hardware.h>
  23. /* Platform_data reserved for unifb registers. */
  24. #define UNIFB_REGS_NUM 10
  25. /* RAM reserved for the frame buffer. */
  26. #define UNIFB_MEMSIZE (SZ_4M) /* 4 MB for 1024*768*32b */
  27. /*
  28. * cause UNIGFX don not have EDID
  29. * all the modes are organized as follow
  30. */
  31. static const struct fb_videomode unifb_modes[] = {
  32. /* 0 640x480-60 VESA */
  33. { "640x480@60", 60, 640, 480, 25175000, 48, 16, 34, 10, 96, 1,
  34. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  35. /* 1 640x480-75 VESA */
  36. { "640x480@75", 75, 640, 480, 31500000, 120, 16, 18, 1, 64, 1,
  37. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  38. /* 2 800x600-60 VESA */
  39. { "800x600@60", 60, 800, 600, 40000000, 88, 40, 26, 1, 128, 1,
  40. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  41. /* 3 800x600-75 VESA */
  42. { "800x600@75", 75, 800, 600, 49500000, 160, 16, 23, 1, 80, 1,
  43. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  44. /* 4 1024x768-60 VESA */
  45. { "1024x768@60", 60, 1024, 768, 65000000, 160, 24, 34, 3, 136, 1,
  46. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  47. /* 5 1024x768-75 VESA */
  48. { "1024x768@75", 75, 1024, 768, 78750000, 176, 16, 30, 1, 96, 1,
  49. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  50. /* 6 1280x960-60 VESA */
  51. { "1280x960@60", 60, 1280, 960, 108000000, 312, 96, 38, 1, 112, 1,
  52. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  53. /* 7 1440x900-60 VESA */
  54. { "1440x900@60", 60, 1440, 900, 106500000, 232, 80, 30, 3, 152, 1,
  55. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  56. /* 8 FIXME 9 1024x600-60 VESA UNTESTED */
  57. { "1024x600@60", 60, 1024, 600, 50650000, 160, 24, 26, 1, 136, 1,
  58. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  59. /* 9 FIXME 10 1024x600-75 VESA UNTESTED */
  60. { "1024x600@75", 75, 1024, 600, 61500000, 176, 16, 23, 1, 96, 1,
  61. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  62. /* 10 FIXME 11 1366x768-60 VESA UNTESTED */
  63. { "1366x768@60", 60, 1366, 768, 85500000, 256, 58, 18, 1, 112, 3,
  64. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  65. };
  66. static struct fb_var_screeninfo unifb_default = {
  67. .xres = 640,
  68. .yres = 480,
  69. .xres_virtual = 640,
  70. .yres_virtual = 480,
  71. .bits_per_pixel = 16,
  72. .red = { 11, 5, 0 },
  73. .green = { 5, 6, 0 },
  74. .blue = { 0, 5, 0 },
  75. .activate = FB_ACTIVATE_NOW,
  76. .height = -1,
  77. .width = -1,
  78. .pixclock = 25175000,
  79. .left_margin = 48,
  80. .right_margin = 16,
  81. .upper_margin = 33,
  82. .lower_margin = 10,
  83. .hsync_len = 96,
  84. .vsync_len = 2,
  85. .vmode = FB_VMODE_NONINTERLACED,
  86. };
  87. static struct fb_fix_screeninfo unifb_fix = {
  88. .id = "UNIGFX FB",
  89. .type = FB_TYPE_PACKED_PIXELS,
  90. .visual = FB_VISUAL_TRUECOLOR,
  91. .xpanstep = 1,
  92. .ypanstep = 1,
  93. .ywrapstep = 1,
  94. .accel = FB_ACCEL_NONE,
  95. };
  96. static void unifb_sync(struct fb_info *info)
  97. {
  98. /* TODO: may, this can be replaced by interrupt */
  99. int cnt;
  100. for (cnt = 0; cnt < 0x10000000; cnt++) {
  101. if (readl(UGE_COMMAND) & 0x1000000)
  102. return;
  103. }
  104. if (cnt > 0x8000000)
  105. dev_warn(info->device, "Warning: UniGFX GE time out ...\n");
  106. }
  107. static void unifb_prim_fillrect(struct fb_info *info,
  108. const struct fb_fillrect *region)
  109. {
  110. int awidth = region->width;
  111. int aheight = region->height;
  112. int m_iBpp = info->var.bits_per_pixel;
  113. int screen_width = info->var.xres;
  114. int src_sel = 1; /* from fg_color */
  115. int pat_sel = 1;
  116. int src_x0 = 0;
  117. int dst_x0 = region->dx;
  118. int src_y0 = 0;
  119. int dst_y0 = region->dy;
  120. int rop_alpha_sel = 0;
  121. int rop_alpha_code = 0xCC;
  122. int x_dir = 1;
  123. int y_dir = 1;
  124. int alpha_r = 0;
  125. int alpha_sel = 0;
  126. int dst_pitch = screen_width * (m_iBpp / 8);
  127. int dst_offset = dst_y0 * dst_pitch + dst_x0 * (m_iBpp / 8);
  128. int src_pitch = screen_width * (m_iBpp / 8);
  129. int src_offset = src_y0 * src_pitch + src_x0 * (m_iBpp / 8);
  130. unsigned int command = 0;
  131. int clip_region = 0;
  132. int clip_en = 0;
  133. int tp_en = 0;
  134. int fg_color = 0;
  135. int bottom = info->var.yres - 1;
  136. int right = info->var.xres - 1;
  137. int top = 0;
  138. bottom = (bottom << 16) | right;
  139. command = (rop_alpha_sel << 26) | (pat_sel << 18) | (src_sel << 16)
  140. | (x_dir << 20) | (y_dir << 21) | (command << 24)
  141. | (clip_region << 23) | (clip_en << 22) | (tp_en << 27);
  142. src_pitch = (dst_pitch << 16) | src_pitch;
  143. awidth = awidth | (aheight << 16);
  144. alpha_r = ((rop_alpha_code & 0xff) << 8) | (alpha_r & 0xff)
  145. | (alpha_sel << 16);
  146. src_x0 = (src_x0 & 0x1fff) | ((src_y0 & 0x1fff) << 16);
  147. dst_x0 = (dst_x0 & 0x1fff) | ((dst_y0 & 0x1fff) << 16);
  148. fg_color = region->color;
  149. unifb_sync(info);
  150. writel(((u32 *)(info->pseudo_palette))[fg_color], UGE_FCOLOR);
  151. writel(0, UGE_BCOLOR);
  152. writel(src_pitch, UGE_PITCH);
  153. writel(src_offset, UGE_SRCSTART);
  154. writel(dst_offset, UGE_DSTSTART);
  155. writel(awidth, UGE_WIDHEIGHT);
  156. writel(top, UGE_CLIP0);
  157. writel(bottom, UGE_CLIP1);
  158. writel(alpha_r, UGE_ROPALPHA);
  159. writel(src_x0, UGE_SRCXY);
  160. writel(dst_x0, UGE_DSTXY);
  161. writel(command, UGE_COMMAND);
  162. }
  163. static void unifb_fillrect(struct fb_info *info,
  164. const struct fb_fillrect *region)
  165. {
  166. struct fb_fillrect modded;
  167. int vxres, vyres;
  168. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  169. sys_fillrect(info, region);
  170. return;
  171. }
  172. vxres = info->var.xres_virtual;
  173. vyres = info->var.yres_virtual;
  174. memcpy(&modded, region, sizeof(struct fb_fillrect));
  175. if (!modded.width || !modded.height ||
  176. modded.dx >= vxres || modded.dy >= vyres)
  177. return;
  178. if (modded.dx + modded.width > vxres)
  179. modded.width = vxres - modded.dx;
  180. if (modded.dy + modded.height > vyres)
  181. modded.height = vyres - modded.dy;
  182. unifb_prim_fillrect(info, &modded);
  183. }
  184. static void unifb_prim_copyarea(struct fb_info *info,
  185. const struct fb_copyarea *area)
  186. {
  187. int awidth = area->width;
  188. int aheight = area->height;
  189. int m_iBpp = info->var.bits_per_pixel;
  190. int screen_width = info->var.xres;
  191. int src_sel = 2; /* from mem */
  192. int pat_sel = 0;
  193. int src_x0 = area->sx;
  194. int dst_x0 = area->dx;
  195. int src_y0 = area->sy;
  196. int dst_y0 = area->dy;
  197. int rop_alpha_sel = 0;
  198. int rop_alpha_code = 0xCC;
  199. int x_dir = 1;
  200. int y_dir = 1;
  201. int alpha_r = 0;
  202. int alpha_sel = 0;
  203. int dst_pitch = screen_width * (m_iBpp / 8);
  204. int dst_offset = dst_y0 * dst_pitch + dst_x0 * (m_iBpp / 8);
  205. int src_pitch = screen_width * (m_iBpp / 8);
  206. int src_offset = src_y0 * src_pitch + src_x0 * (m_iBpp / 8);
  207. unsigned int command = 0;
  208. int clip_region = 0;
  209. int clip_en = 1;
  210. int tp_en = 0;
  211. int top = 0;
  212. int bottom = info->var.yres;
  213. int right = info->var.xres;
  214. int fg_color = 0;
  215. int bg_color = 0;
  216. if (src_x0 < 0)
  217. src_x0 = 0;
  218. if (src_y0 < 0)
  219. src_y0 = 0;
  220. if (src_y0 - dst_y0 > 0) {
  221. y_dir = 1;
  222. } else {
  223. y_dir = 0;
  224. src_offset = (src_y0 + aheight) * src_pitch +
  225. src_x0 * (m_iBpp / 8);
  226. dst_offset = (dst_y0 + aheight) * dst_pitch +
  227. dst_x0 * (m_iBpp / 8);
  228. src_y0 += aheight;
  229. dst_y0 += aheight;
  230. }
  231. command = (rop_alpha_sel << 26) | (pat_sel << 18) | (src_sel << 16) |
  232. (x_dir << 20) | (y_dir << 21) | (command << 24) |
  233. (clip_region << 23) | (clip_en << 22) | (tp_en << 27);
  234. src_pitch = (dst_pitch << 16) | src_pitch;
  235. awidth = awidth | (aheight << 16);
  236. alpha_r = ((rop_alpha_code & 0xff) << 8) | (alpha_r & 0xff) |
  237. (alpha_sel << 16);
  238. src_x0 = (src_x0 & 0x1fff) | ((src_y0 & 0x1fff) << 16);
  239. dst_x0 = (dst_x0 & 0x1fff) | ((dst_y0 & 0x1fff) << 16);
  240. bottom = (bottom << 16) | right;
  241. unifb_sync(info);
  242. writel(src_pitch, UGE_PITCH);
  243. writel(src_offset, UGE_SRCSTART);
  244. writel(dst_offset, UGE_DSTSTART);
  245. writel(awidth, UGE_WIDHEIGHT);
  246. writel(top, UGE_CLIP0);
  247. writel(bottom, UGE_CLIP1);
  248. writel(bg_color, UGE_BCOLOR);
  249. writel(fg_color, UGE_FCOLOR);
  250. writel(alpha_r, UGE_ROPALPHA);
  251. writel(src_x0, UGE_SRCXY);
  252. writel(dst_x0, UGE_DSTXY);
  253. writel(command, UGE_COMMAND);
  254. }
  255. static void unifb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  256. {
  257. struct fb_copyarea modded;
  258. u32 vxres, vyres;
  259. modded.sx = area->sx;
  260. modded.sy = area->sy;
  261. modded.dx = area->dx;
  262. modded.dy = area->dy;
  263. modded.width = area->width;
  264. modded.height = area->height;
  265. if (info->flags & FBINFO_HWACCEL_DISABLED) {
  266. sys_copyarea(info, area);
  267. return;
  268. }
  269. vxres = info->var.xres_virtual;
  270. vyres = info->var.yres_virtual;
  271. if (!modded.width || !modded.height ||
  272. modded.sx >= vxres || modded.sy >= vyres ||
  273. modded.dx >= vxres || modded.dy >= vyres)
  274. return;
  275. if (modded.sx + modded.width > vxres)
  276. modded.width = vxres - modded.sx;
  277. if (modded.dx + modded.width > vxres)
  278. modded.width = vxres - modded.dx;
  279. if (modded.sy + modded.height > vyres)
  280. modded.height = vyres - modded.sy;
  281. if (modded.dy + modded.height > vyres)
  282. modded.height = vyres - modded.dy;
  283. unifb_prim_copyarea(info, &modded);
  284. }
  285. static void unifb_imageblit(struct fb_info *info, const struct fb_image *image)
  286. {
  287. sys_imageblit(info, image);
  288. }
  289. static u_long get_line_length(int xres_virtual, int bpp)
  290. {
  291. u_long length;
  292. length = xres_virtual * bpp;
  293. length = (length + 31) & ~31;
  294. length >>= 3;
  295. return length;
  296. }
  297. /*
  298. * Setting the video mode has been split into two parts.
  299. * First part, xxxfb_check_var, must not write anything
  300. * to hardware, it should only verify and adjust var.
  301. * This means it doesn't alter par but it does use hardware
  302. * data from it to check this var.
  303. */
  304. static int unifb_check_var(struct fb_var_screeninfo *var,
  305. struct fb_info *info)
  306. {
  307. u_long line_length;
  308. /*
  309. * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
  310. * as FB_VMODE_SMOOTH_XPAN is only used internally
  311. */
  312. if (var->vmode & FB_VMODE_CONUPDATE) {
  313. var->vmode |= FB_VMODE_YWRAP;
  314. var->xoffset = info->var.xoffset;
  315. var->yoffset = info->var.yoffset;
  316. }
  317. /*
  318. * Some very basic checks
  319. */
  320. if (!var->xres)
  321. var->xres = 1;
  322. if (!var->yres)
  323. var->yres = 1;
  324. if (var->xres > var->xres_virtual)
  325. var->xres_virtual = var->xres;
  326. if (var->yres > var->yres_virtual)
  327. var->yres_virtual = var->yres;
  328. if (var->bits_per_pixel <= 1)
  329. var->bits_per_pixel = 1;
  330. else if (var->bits_per_pixel <= 8)
  331. var->bits_per_pixel = 8;
  332. else if (var->bits_per_pixel <= 16)
  333. var->bits_per_pixel = 16;
  334. else if (var->bits_per_pixel <= 24)
  335. var->bits_per_pixel = 24;
  336. else if (var->bits_per_pixel <= 32)
  337. var->bits_per_pixel = 32;
  338. else
  339. return -EINVAL;
  340. if (var->xres_virtual < var->xoffset + var->xres)
  341. var->xres_virtual = var->xoffset + var->xres;
  342. if (var->yres_virtual < var->yoffset + var->yres)
  343. var->yres_virtual = var->yoffset + var->yres;
  344. /*
  345. * Memory limit
  346. */
  347. line_length =
  348. get_line_length(var->xres_virtual, var->bits_per_pixel);
  349. if (line_length * var->yres_virtual > UNIFB_MEMSIZE)
  350. return -ENOMEM;
  351. /*
  352. * Now that we checked it we alter var. The reason being is that the
  353. * video mode passed in might not work but slight changes to it might
  354. * make it work. This way we let the user know what is acceptable.
  355. */
  356. switch (var->bits_per_pixel) {
  357. case 1:
  358. case 8:
  359. var->red.offset = 0;
  360. var->red.length = 8;
  361. var->green.offset = 0;
  362. var->green.length = 8;
  363. var->blue.offset = 0;
  364. var->blue.length = 8;
  365. var->transp.offset = 0;
  366. var->transp.length = 0;
  367. break;
  368. case 16: /* RGBA 5551 */
  369. if (var->transp.length) {
  370. var->red.offset = 0;
  371. var->red.length = 5;
  372. var->green.offset = 5;
  373. var->green.length = 5;
  374. var->blue.offset = 10;
  375. var->blue.length = 5;
  376. var->transp.offset = 15;
  377. var->transp.length = 1;
  378. } else { /* RGB 565 */
  379. var->red.offset = 11;
  380. var->red.length = 5;
  381. var->green.offset = 5;
  382. var->green.length = 6;
  383. var->blue.offset = 0;
  384. var->blue.length = 5;
  385. var->transp.offset = 0;
  386. var->transp.length = 0;
  387. }
  388. break;
  389. case 24: /* RGB 888 */
  390. var->red.offset = 0;
  391. var->red.length = 8;
  392. var->green.offset = 8;
  393. var->green.length = 8;
  394. var->blue.offset = 16;
  395. var->blue.length = 8;
  396. var->transp.offset = 0;
  397. var->transp.length = 0;
  398. break;
  399. case 32: /* RGBA 8888 */
  400. var->red.offset = 16;
  401. var->red.length = 8;
  402. var->green.offset = 8;
  403. var->green.length = 8;
  404. var->blue.offset = 0;
  405. var->blue.length = 8;
  406. var->transp.offset = 24;
  407. var->transp.length = 8;
  408. break;
  409. }
  410. var->red.msb_right = 0;
  411. var->green.msb_right = 0;
  412. var->blue.msb_right = 0;
  413. var->transp.msb_right = 0;
  414. return 0;
  415. }
  416. /*
  417. * This routine actually sets the video mode. It's in here where we
  418. * the hardware state info->par and fix which can be affected by the
  419. * change in par. For this driver it doesn't do much.
  420. */
  421. static int unifb_set_par(struct fb_info *info)
  422. {
  423. int hTotal, vTotal, hSyncStart, hSyncEnd, vSyncStart, vSyncEnd;
  424. int format;
  425. #ifdef CONFIG_PUV3_PM
  426. struct clk *clk_vga;
  427. u32 pixclk = 0;
  428. int i;
  429. for (i = 0; i <= 10; i++) {
  430. if (info->var.xres == unifb_modes[i].xres
  431. && info->var.yres == unifb_modes[i].yres
  432. && info->var.upper_margin == unifb_modes[i].upper_margin
  433. && info->var.lower_margin == unifb_modes[i].lower_margin
  434. && info->var.left_margin == unifb_modes[i].left_margin
  435. && info->var.right_margin == unifb_modes[i].right_margin
  436. && info->var.hsync_len == unifb_modes[i].hsync_len
  437. && info->var.vsync_len == unifb_modes[i].vsync_len) {
  438. pixclk = unifb_modes[i].pixclock;
  439. break;
  440. }
  441. }
  442. /* set clock rate */
  443. clk_vga = clk_get(info->device, "VGA_CLK");
  444. if (clk_vga == ERR_PTR(-ENOENT))
  445. return -ENOENT;
  446. if (pixclk != 0) {
  447. if (clk_set_rate(clk_vga, pixclk)) { /* set clock failed */
  448. info->fix = unifb_fix;
  449. info->var = unifb_default;
  450. if (clk_set_rate(clk_vga, unifb_default.pixclock))
  451. return -EINVAL;
  452. }
  453. }
  454. #endif
  455. info->fix.line_length = get_line_length(info->var.xres_virtual,
  456. info->var.bits_per_pixel);
  457. hSyncStart = info->var.xres + info->var.right_margin;
  458. hSyncEnd = hSyncStart + info->var.hsync_len;
  459. hTotal = hSyncEnd + info->var.left_margin;
  460. vSyncStart = info->var.yres + info->var.lower_margin;
  461. vSyncEnd = vSyncStart + info->var.vsync_len;
  462. vTotal = vSyncEnd + info->var.upper_margin;
  463. switch (info->var.bits_per_pixel) {
  464. case 8:
  465. format = UDE_CFG_DST8;
  466. break;
  467. case 16:
  468. format = UDE_CFG_DST16;
  469. break;
  470. case 24:
  471. format = UDE_CFG_DST24;
  472. break;
  473. case 32:
  474. format = UDE_CFG_DST32;
  475. break;
  476. default:
  477. return -EINVAL;
  478. }
  479. writel(PKUNITY_UNIGFX_MMAP_BASE, UDE_FSA);
  480. writel(info->var.yres, UDE_LS);
  481. writel(get_line_length(info->var.xres,
  482. info->var.bits_per_pixel) >> 3, UDE_PS);
  483. /* >> 3 for hardware required. */
  484. writel((hTotal << 16) | (info->var.xres), UDE_HAT);
  485. writel(((hTotal - 1) << 16) | (info->var.xres - 1), UDE_HBT);
  486. writel(((hSyncEnd - 1) << 16) | (hSyncStart - 1), UDE_HST);
  487. writel((vTotal << 16) | (info->var.yres), UDE_VAT);
  488. writel(((vTotal - 1) << 16) | (info->var.yres - 1), UDE_VBT);
  489. writel(((vSyncEnd - 1) << 16) | (vSyncStart - 1), UDE_VST);
  490. writel(UDE_CFG_GDEN_ENABLE | UDE_CFG_TIMEUP_ENABLE
  491. | format | 0xC0000001, UDE_CFG);
  492. return 0;
  493. }
  494. /*
  495. * Set a single color register. The values supplied are already
  496. * rounded down to the hardware's capabilities (according to the
  497. * entries in the var structure). Return != 0 for invalid regno.
  498. */
  499. static int unifb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  500. u_int transp, struct fb_info *info)
  501. {
  502. if (regno >= 256) /* no. of hw registers */
  503. return 1;
  504. /* grayscale works only partially under directcolor */
  505. if (info->var.grayscale) {
  506. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  507. red = green = blue =
  508. (red * 77 + green * 151 + blue * 28) >> 8;
  509. }
  510. #define CNVT_TOHW(val, width) ((((val)<<(width))+0x7FFF-(val))>>16)
  511. switch (info->fix.visual) {
  512. case FB_VISUAL_TRUECOLOR:
  513. case FB_VISUAL_PSEUDOCOLOR:
  514. red = CNVT_TOHW(red, info->var.red.length);
  515. green = CNVT_TOHW(green, info->var.green.length);
  516. blue = CNVT_TOHW(blue, info->var.blue.length);
  517. transp = CNVT_TOHW(transp, info->var.transp.length);
  518. break;
  519. case FB_VISUAL_DIRECTCOLOR:
  520. red = CNVT_TOHW(red, 8); /* expect 8 bit DAC */
  521. green = CNVT_TOHW(green, 8);
  522. blue = CNVT_TOHW(blue, 8);
  523. /* hey, there is bug in transp handling... */
  524. transp = CNVT_TOHW(transp, 8);
  525. break;
  526. }
  527. #undef CNVT_TOHW
  528. /* Truecolor has hardware independent palette */
  529. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  530. u32 v;
  531. if (regno >= 16)
  532. return 1;
  533. v = (red << info->var.red.offset) |
  534. (green << info->var.green.offset) |
  535. (blue << info->var.blue.offset) |
  536. (transp << info->var.transp.offset);
  537. switch (info->var.bits_per_pixel) {
  538. case 8:
  539. break;
  540. case 16:
  541. case 24:
  542. case 32:
  543. ((u32 *) (info->pseudo_palette))[regno] = v;
  544. break;
  545. default:
  546. return 1;
  547. }
  548. return 0;
  549. }
  550. return 0;
  551. }
  552. /*
  553. * Pan or Wrap the Display
  554. *
  555. * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
  556. */
  557. static int unifb_pan_display(struct fb_var_screeninfo *var,
  558. struct fb_info *info)
  559. {
  560. if (var->vmode & FB_VMODE_YWRAP) {
  561. if (var->yoffset < 0
  562. || var->yoffset >= info->var.yres_virtual
  563. || var->xoffset)
  564. return -EINVAL;
  565. } else {
  566. if (var->xoffset + var->xres > info->var.xres_virtual ||
  567. var->yoffset + var->yres > info->var.yres_virtual)
  568. return -EINVAL;
  569. }
  570. info->var.xoffset = var->xoffset;
  571. info->var.yoffset = var->yoffset;
  572. if (var->vmode & FB_VMODE_YWRAP)
  573. info->var.vmode |= FB_VMODE_YWRAP;
  574. else
  575. info->var.vmode &= ~FB_VMODE_YWRAP;
  576. return 0;
  577. }
  578. int unifb_mmap(struct fb_info *info,
  579. struct vm_area_struct *vma)
  580. {
  581. unsigned long size = vma->vm_end - vma->vm_start;
  582. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  583. unsigned long pos = info->fix.smem_start + offset;
  584. if (offset + size > info->fix.smem_len)
  585. return -EINVAL;
  586. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  587. if (io_remap_pfn_range(vma, vma->vm_start, pos >> PAGE_SHIFT, size,
  588. vma->vm_page_prot))
  589. return -EAGAIN;
  590. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  591. return 0;
  592. }
  593. static struct fb_ops unifb_ops = {
  594. .fb_read = fb_sys_read,
  595. .fb_write = fb_sys_write,
  596. .fb_check_var = unifb_check_var,
  597. .fb_set_par = unifb_set_par,
  598. .fb_setcolreg = unifb_setcolreg,
  599. .fb_pan_display = unifb_pan_display,
  600. .fb_fillrect = unifb_fillrect,
  601. .fb_copyarea = unifb_copyarea,
  602. .fb_imageblit = unifb_imageblit,
  603. .fb_mmap = unifb_mmap,
  604. };
  605. /*
  606. * Initialisation
  607. */
  608. static int unifb_probe(struct platform_device *dev)
  609. {
  610. struct fb_info *info;
  611. u32 unifb_regs[UNIFB_REGS_NUM];
  612. int retval = -ENOMEM;
  613. struct resource *iomem, *mapmem;
  614. info = framebuffer_alloc(sizeof(u32)*256, &dev->dev);
  615. if (!info)
  616. goto err;
  617. info->screen_base = (char __iomem *)KUSER_UNIGFX_BASE;
  618. info->fbops = &unifb_ops;
  619. retval = fb_find_mode(&info->var, info, NULL,
  620. unifb_modes, 10, &unifb_modes[0], 16);
  621. if (!retval || (retval == 4))
  622. info->var = unifb_default;
  623. iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  624. unifb_fix.mmio_start = iomem->start;
  625. mapmem = platform_get_resource(dev, IORESOURCE_MEM, 1);
  626. unifb_fix.smem_start = mapmem->start;
  627. unifb_fix.smem_len = UNIFB_MEMSIZE;
  628. info->fix = unifb_fix;
  629. info->pseudo_palette = info->par;
  630. info->par = NULL;
  631. info->flags = FBINFO_FLAG_DEFAULT;
  632. #ifdef FB_ACCEL_PUV3_UNIGFX
  633. info->fix.accel = FB_ACCEL_PUV3_UNIGFX;
  634. #endif
  635. retval = fb_alloc_cmap(&info->cmap, 256, 0);
  636. if (retval < 0)
  637. goto err1;
  638. retval = register_framebuffer(info);
  639. if (retval < 0)
  640. goto err2;
  641. platform_set_drvdata(dev, info);
  642. platform_device_add_data(dev, unifb_regs, sizeof(u32) * UNIFB_REGS_NUM);
  643. printk(KERN_INFO
  644. "fb%d: Virtual frame buffer device, using %dM of video memory\n",
  645. info->node, UNIFB_MEMSIZE >> 20);
  646. return 0;
  647. err2:
  648. fb_dealloc_cmap(&info->cmap);
  649. err1:
  650. framebuffer_release(info);
  651. err:
  652. return retval;
  653. }
  654. static int unifb_remove(struct platform_device *dev)
  655. {
  656. struct fb_info *info = platform_get_drvdata(dev);
  657. if (info) {
  658. unregister_framebuffer(info);
  659. fb_dealloc_cmap(&info->cmap);
  660. framebuffer_release(info);
  661. }
  662. return 0;
  663. }
  664. #ifdef CONFIG_PM
  665. static int unifb_resume(struct platform_device *dev)
  666. {
  667. int rc = 0;
  668. u32 *unifb_regs = dev->dev.platform_data;
  669. if (dev->dev.power.power_state.event == PM_EVENT_ON)
  670. return 0;
  671. console_lock();
  672. if (dev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  673. writel(unifb_regs[0], UDE_FSA);
  674. writel(unifb_regs[1], UDE_LS);
  675. writel(unifb_regs[2], UDE_PS);
  676. writel(unifb_regs[3], UDE_HAT);
  677. writel(unifb_regs[4], UDE_HBT);
  678. writel(unifb_regs[5], UDE_HST);
  679. writel(unifb_regs[6], UDE_VAT);
  680. writel(unifb_regs[7], UDE_VBT);
  681. writel(unifb_regs[8], UDE_VST);
  682. writel(unifb_regs[9], UDE_CFG);
  683. }
  684. dev->dev.power.power_state = PMSG_ON;
  685. console_unlock();
  686. return rc;
  687. }
  688. static int unifb_suspend(struct platform_device *dev, pm_message_t mesg)
  689. {
  690. u32 *unifb_regs = dev->dev.platform_data;
  691. unifb_regs[0] = readl(UDE_FSA);
  692. unifb_regs[1] = readl(UDE_LS);
  693. unifb_regs[2] = readl(UDE_PS);
  694. unifb_regs[3] = readl(UDE_HAT);
  695. unifb_regs[4] = readl(UDE_HBT);
  696. unifb_regs[5] = readl(UDE_HST);
  697. unifb_regs[6] = readl(UDE_VAT);
  698. unifb_regs[7] = readl(UDE_VBT);
  699. unifb_regs[8] = readl(UDE_VST);
  700. unifb_regs[9] = readl(UDE_CFG);
  701. if (mesg.event == dev->dev.power.power_state.event)
  702. return 0;
  703. switch (mesg.event) {
  704. case PM_EVENT_FREEZE: /* about to take snapshot */
  705. case PM_EVENT_PRETHAW: /* before restoring snapshot */
  706. goto done;
  707. }
  708. console_lock();
  709. /* do nothing... */
  710. console_unlock();
  711. done:
  712. dev->dev.power.power_state = mesg;
  713. return 0;
  714. }
  715. #else
  716. #define unifb_resume NULL
  717. #define unifb_suspend NULL
  718. #endif
  719. static struct platform_driver unifb_driver = {
  720. .probe = unifb_probe,
  721. .remove = unifb_remove,
  722. .resume = unifb_resume,
  723. .suspend = unifb_suspend,
  724. .driver = {
  725. .name = "PKUnity-v3-UNIGFX",
  726. },
  727. };
  728. static int __init unifb_init(void)
  729. {
  730. #ifndef MODULE
  731. if (fb_get_options("unifb", NULL))
  732. return -ENODEV;
  733. #endif
  734. return platform_driver_register(&unifb_driver);
  735. }
  736. module_init(unifb_init);
  737. static void __exit unifb_exit(void)
  738. {
  739. platform_driver_unregister(&unifb_driver);
  740. }
  741. module_exit(unifb_exit);
  742. MODULE_LICENSE("GPL v2");