68328fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * linux/drivers/video/68328fb.c -- Low level implementation of the
  3. * mc68x328 LCD frame buffer device
  4. *
  5. * Copyright (C) 2003 Georges Menie
  6. *
  7. * This driver assumes an already configured controller (e.g. from config.c)
  8. * Keep the code clean of board specific initialization.
  9. *
  10. * This code has not been tested with colors, colormap management functions
  11. * are minimal (no colormap data written to the 68328 registers...)
  12. *
  13. * initial version of this driver:
  14. * Copyright (C) 1998,1999 Kenneth Albanowski <[email protected]>,
  15. * The Silver Hammer Group, Ltd.
  16. *
  17. * this version is based on :
  18. *
  19. * linux/drivers/video/vfb.c -- Virtual frame buffer device
  20. *
  21. * Copyright (C) 2002 James Simmons
  22. *
  23. * Copyright (C) 1997 Geert Uytterhoeven
  24. *
  25. * This file is subject to the terms and conditions of the GNU General Public
  26. * License. See the file COPYING in the main directory of this archive for
  27. * more details.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/errno.h>
  32. #include <linux/string.h>
  33. #include <linux/mm.h>
  34. #include <linux/vmalloc.h>
  35. #include <linux/delay.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/fb.h>
  39. #include <linux/init.h>
  40. #if defined(CONFIG_M68VZ328)
  41. #include <asm/MC68VZ328.h>
  42. #elif defined(CONFIG_M68EZ328)
  43. #include <asm/MC68EZ328.h>
  44. #elif defined(CONFIG_M68328)
  45. #include <asm/MC68328.h>
  46. #else
  47. #error wrong architecture for the MC68x328 frame buffer device
  48. #endif
  49. static u_long videomemory;
  50. static u_long videomemorysize;
  51. static struct fb_info fb_info;
  52. static u32 mc68x328fb_pseudo_palette[16];
  53. static struct fb_var_screeninfo mc68x328fb_default __initdata = {
  54. .red = { 0, 8, 0 },
  55. .green = { 0, 8, 0 },
  56. .blue = { 0, 8, 0 },
  57. .activate = FB_ACTIVATE_TEST,
  58. .height = -1,
  59. .width = -1,
  60. .pixclock = 20000,
  61. .left_margin = 64,
  62. .right_margin = 64,
  63. .upper_margin = 32,
  64. .lower_margin = 32,
  65. .hsync_len = 64,
  66. .vsync_len = 2,
  67. .vmode = FB_VMODE_NONINTERLACED,
  68. };
  69. static const struct fb_fix_screeninfo mc68x328fb_fix __initconst = {
  70. .id = "68328fb",
  71. .type = FB_TYPE_PACKED_PIXELS,
  72. .xpanstep = 1,
  73. .ypanstep = 1,
  74. .ywrapstep = 1,
  75. .accel = FB_ACCEL_NONE,
  76. };
  77. /*
  78. * Interface used by the world
  79. */
  80. static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
  81. struct fb_info *info);
  82. static int mc68x328fb_set_par(struct fb_info *info);
  83. static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  84. u_int transp, struct fb_info *info);
  85. static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
  86. struct fb_info *info);
  87. static int mc68x328fb_mmap(struct fb_info *info, struct vm_area_struct *vma);
  88. static const struct fb_ops mc68x328fb_ops = {
  89. .fb_check_var = mc68x328fb_check_var,
  90. .fb_set_par = mc68x328fb_set_par,
  91. .fb_setcolreg = mc68x328fb_setcolreg,
  92. .fb_pan_display = mc68x328fb_pan_display,
  93. .fb_fillrect = cfb_fillrect,
  94. .fb_copyarea = cfb_copyarea,
  95. .fb_imageblit = cfb_imageblit,
  96. .fb_mmap = mc68x328fb_mmap,
  97. };
  98. /*
  99. * Internal routines
  100. */
  101. static u_long get_line_length(int xres_virtual, int bpp)
  102. {
  103. u_long length;
  104. length = xres_virtual * bpp;
  105. length = (length + 31) & ~31;
  106. length >>= 3;
  107. return (length);
  108. }
  109. /*
  110. * Setting the video mode has been split into two parts.
  111. * First part, xxxfb_check_var, must not write anything
  112. * to hardware, it should only verify and adjust var.
  113. * This means it doesn't alter par but it does use hardware
  114. * data from it to check this var.
  115. */
  116. static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
  117. struct fb_info *info)
  118. {
  119. u_long line_length;
  120. /*
  121. * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
  122. * as FB_VMODE_SMOOTH_XPAN is only used internally
  123. */
  124. if (var->vmode & FB_VMODE_CONUPDATE) {
  125. var->vmode |= FB_VMODE_YWRAP;
  126. var->xoffset = info->var.xoffset;
  127. var->yoffset = info->var.yoffset;
  128. }
  129. /*
  130. * Some very basic checks
  131. */
  132. if (!var->xres)
  133. var->xres = 1;
  134. if (!var->yres)
  135. var->yres = 1;
  136. if (var->xres > var->xres_virtual)
  137. var->xres_virtual = var->xres;
  138. if (var->yres > var->yres_virtual)
  139. var->yres_virtual = var->yres;
  140. if (var->bits_per_pixel <= 1)
  141. var->bits_per_pixel = 1;
  142. else if (var->bits_per_pixel <= 8)
  143. var->bits_per_pixel = 8;
  144. else if (var->bits_per_pixel <= 16)
  145. var->bits_per_pixel = 16;
  146. else if (var->bits_per_pixel <= 24)
  147. var->bits_per_pixel = 24;
  148. else if (var->bits_per_pixel <= 32)
  149. var->bits_per_pixel = 32;
  150. else
  151. return -EINVAL;
  152. if (var->xres_virtual < var->xoffset + var->xres)
  153. var->xres_virtual = var->xoffset + var->xres;
  154. if (var->yres_virtual < var->yoffset + var->yres)
  155. var->yres_virtual = var->yoffset + var->yres;
  156. /*
  157. * Memory limit
  158. */
  159. line_length =
  160. get_line_length(var->xres_virtual, var->bits_per_pixel);
  161. if (line_length * var->yres_virtual > videomemorysize)
  162. return -ENOMEM;
  163. /*
  164. * Now that we checked it we alter var. The reason being is that the video
  165. * mode passed in might not work but slight changes to it might make it
  166. * work. This way we let the user know what is acceptable.
  167. */
  168. switch (var->bits_per_pixel) {
  169. case 1:
  170. var->red.offset = 0;
  171. var->red.length = 1;
  172. var->green.offset = 0;
  173. var->green.length = 1;
  174. var->blue.offset = 0;
  175. var->blue.length = 1;
  176. var->transp.offset = 0;
  177. var->transp.length = 0;
  178. break;
  179. case 8:
  180. var->red.offset = 0;
  181. var->red.length = 8;
  182. var->green.offset = 0;
  183. var->green.length = 8;
  184. var->blue.offset = 0;
  185. var->blue.length = 8;
  186. var->transp.offset = 0;
  187. var->transp.length = 0;
  188. break;
  189. case 16: /* RGBA 5551 */
  190. if (var->transp.length) {
  191. var->red.offset = 0;
  192. var->red.length = 5;
  193. var->green.offset = 5;
  194. var->green.length = 5;
  195. var->blue.offset = 10;
  196. var->blue.length = 5;
  197. var->transp.offset = 15;
  198. var->transp.length = 1;
  199. } else { /* RGB 565 */
  200. var->red.offset = 0;
  201. var->red.length = 5;
  202. var->green.offset = 5;
  203. var->green.length = 6;
  204. var->blue.offset = 11;
  205. var->blue.length = 5;
  206. var->transp.offset = 0;
  207. var->transp.length = 0;
  208. }
  209. break;
  210. case 24: /* RGB 888 */
  211. var->red.offset = 0;
  212. var->red.length = 8;
  213. var->green.offset = 8;
  214. var->green.length = 8;
  215. var->blue.offset = 16;
  216. var->blue.length = 8;
  217. var->transp.offset = 0;
  218. var->transp.length = 0;
  219. break;
  220. case 32: /* RGBA 8888 */
  221. var->red.offset = 0;
  222. var->red.length = 8;
  223. var->green.offset = 8;
  224. var->green.length = 8;
  225. var->blue.offset = 16;
  226. var->blue.length = 8;
  227. var->transp.offset = 24;
  228. var->transp.length = 8;
  229. break;
  230. }
  231. var->red.msb_right = 0;
  232. var->green.msb_right = 0;
  233. var->blue.msb_right = 0;
  234. var->transp.msb_right = 0;
  235. return 0;
  236. }
  237. /* This routine actually sets the video mode. It's in here where we
  238. * the hardware state info->par and fix which can be affected by the
  239. * change in par. For this driver it doesn't do much.
  240. */
  241. static int mc68x328fb_set_par(struct fb_info *info)
  242. {
  243. info->fix.line_length = get_line_length(info->var.xres_virtual,
  244. info->var.bits_per_pixel);
  245. return 0;
  246. }
  247. /*
  248. * Set a single color register. The values supplied are already
  249. * rounded down to the hardware's capabilities (according to the
  250. * entries in the var structure). Return != 0 for invalid regno.
  251. */
  252. static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  253. u_int transp, struct fb_info *info)
  254. {
  255. if (regno >= 256) /* no. of hw registers */
  256. return 1;
  257. /*
  258. * Program hardware... do anything you want with transp
  259. */
  260. /* grayscale works only partially under directcolor */
  261. if (info->var.grayscale) {
  262. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  263. red = green = blue =
  264. (red * 77 + green * 151 + blue * 28) >> 8;
  265. }
  266. /* Directcolor:
  267. * var->{color}.offset contains start of bitfield
  268. * var->{color}.length contains length of bitfield
  269. * {hardwarespecific} contains width of RAMDAC
  270. * cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
  271. * RAMDAC[X] is programmed to (red, green, blue)
  272. *
  273. * Pseudocolor:
  274. * uses offset = 0 && length = RAMDAC register width.
  275. * var->{color}.offset is 0
  276. * var->{color}.length contains width of DAC
  277. * cmap is not used
  278. * RAMDAC[X] is programmed to (red, green, blue)
  279. * Truecolor:
  280. * does not use DAC. Usually 3 are present.
  281. * var->{color}.offset contains start of bitfield
  282. * var->{color}.length contains length of bitfield
  283. * cmap is programmed to (red << red.offset) | (green << green.offset) |
  284. * (blue << blue.offset) | (transp << transp.offset)
  285. * RAMDAC does not exist
  286. */
  287. #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
  288. switch (info->fix.visual) {
  289. case FB_VISUAL_TRUECOLOR:
  290. case FB_VISUAL_PSEUDOCOLOR:
  291. red = CNVT_TOHW(red, info->var.red.length);
  292. green = CNVT_TOHW(green, info->var.green.length);
  293. blue = CNVT_TOHW(blue, info->var.blue.length);
  294. transp = CNVT_TOHW(transp, info->var.transp.length);
  295. break;
  296. case FB_VISUAL_DIRECTCOLOR:
  297. red = CNVT_TOHW(red, 8); /* expect 8 bit DAC */
  298. green = CNVT_TOHW(green, 8);
  299. blue = CNVT_TOHW(blue, 8);
  300. /* hey, there is bug in transp handling... */
  301. transp = CNVT_TOHW(transp, 8);
  302. break;
  303. }
  304. #undef CNVT_TOHW
  305. /* Truecolor has hardware independent palette */
  306. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  307. u32 v;
  308. if (regno >= 16)
  309. return 1;
  310. v = (red << info->var.red.offset) |
  311. (green << info->var.green.offset) |
  312. (blue << info->var.blue.offset) |
  313. (transp << info->var.transp.offset);
  314. switch (info->var.bits_per_pixel) {
  315. case 8:
  316. break;
  317. case 16:
  318. ((u32 *) (info->pseudo_palette))[regno] = v;
  319. break;
  320. case 24:
  321. case 32:
  322. ((u32 *) (info->pseudo_palette))[regno] = v;
  323. break;
  324. }
  325. return 0;
  326. }
  327. return 0;
  328. }
  329. /*
  330. * Pan or Wrap the Display
  331. *
  332. * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
  333. */
  334. static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
  335. struct fb_info *info)
  336. {
  337. if (var->vmode & FB_VMODE_YWRAP) {
  338. if (var->yoffset < 0
  339. || var->yoffset >= info->var.yres_virtual
  340. || var->xoffset)
  341. return -EINVAL;
  342. } else {
  343. if (var->xoffset + info->var.xres > info->var.xres_virtual ||
  344. var->yoffset + info->var.yres > info->var.yres_virtual)
  345. return -EINVAL;
  346. }
  347. info->var.xoffset = var->xoffset;
  348. info->var.yoffset = var->yoffset;
  349. if (var->vmode & FB_VMODE_YWRAP)
  350. info->var.vmode |= FB_VMODE_YWRAP;
  351. else
  352. info->var.vmode &= ~FB_VMODE_YWRAP;
  353. return 0;
  354. }
  355. /*
  356. * Most drivers don't need their own mmap function
  357. */
  358. static int mc68x328fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  359. {
  360. #ifndef MMU
  361. /* this is uClinux (no MMU) specific code */
  362. vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
  363. vma->vm_start = videomemory;
  364. return 0;
  365. #else
  366. return -EINVAL;
  367. #endif
  368. }
  369. static int __init mc68x328fb_setup(char *options)
  370. {
  371. if (!options || !*options)
  372. return 1;
  373. return 1;
  374. }
  375. /*
  376. * Initialisation
  377. */
  378. static int __init mc68x328fb_init(void)
  379. {
  380. #ifndef MODULE
  381. char *option = NULL;
  382. if (fb_get_options("68328fb", &option))
  383. return -ENODEV;
  384. mc68x328fb_setup(option);
  385. #endif
  386. /*
  387. * initialize the default mode from the LCD controller registers
  388. */
  389. mc68x328fb_default.xres = LXMAX;
  390. mc68x328fb_default.yres = LYMAX+1;
  391. mc68x328fb_default.xres_virtual = mc68x328fb_default.xres;
  392. mc68x328fb_default.yres_virtual = mc68x328fb_default.yres;
  393. mc68x328fb_default.bits_per_pixel = 1 + (LPICF & 0x01);
  394. videomemory = LSSA;
  395. videomemorysize = (mc68x328fb_default.xres_virtual+7) / 8 *
  396. mc68x328fb_default.yres_virtual * mc68x328fb_default.bits_per_pixel;
  397. fb_info.screen_base = (void *)videomemory;
  398. fb_info.fbops = &mc68x328fb_ops;
  399. fb_info.var = mc68x328fb_default;
  400. fb_info.fix = mc68x328fb_fix;
  401. fb_info.fix.smem_start = videomemory;
  402. fb_info.fix.smem_len = videomemorysize;
  403. fb_info.fix.line_length =
  404. get_line_length(mc68x328fb_default.xres_virtual, mc68x328fb_default.bits_per_pixel);
  405. fb_info.fix.visual = (mc68x328fb_default.bits_per_pixel) == 1 ?
  406. FB_VISUAL_MONO10 : FB_VISUAL_PSEUDOCOLOR;
  407. if (fb_info.var.bits_per_pixel == 1) {
  408. fb_info.var.red.length = fb_info.var.green.length = fb_info.var.blue.length = 1;
  409. fb_info.var.red.offset = fb_info.var.green.offset = fb_info.var.blue.offset = 0;
  410. }
  411. fb_info.pseudo_palette = &mc68x328fb_pseudo_palette;
  412. fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  413. if (fb_alloc_cmap(&fb_info.cmap, 256, 0))
  414. return -ENOMEM;
  415. if (register_framebuffer(&fb_info) < 0) {
  416. fb_dealloc_cmap(&fb_info.cmap);
  417. return -EINVAL;
  418. }
  419. fb_info(&fb_info, "%s frame buffer device\n", fb_info.fix.id);
  420. fb_info(&fb_info, "%dx%dx%d at 0x%08lx\n",
  421. mc68x328fb_default.xres_virtual,
  422. mc68x328fb_default.yres_virtual,
  423. 1 << mc68x328fb_default.bits_per_pixel, videomemory);
  424. return 0;
  425. }
  426. module_init(mc68x328fb_init);
  427. #ifdef MODULE
  428. static void __exit mc68x328fb_cleanup(void)
  429. {
  430. unregister_framebuffer(&fb_info);
  431. fb_dealloc_cmap(&fb_info.cmap);
  432. }
  433. module_exit(mc68x328fb_cleanup);
  434. MODULE_LICENSE("GPL");
  435. #endif /* MODULE */