68328fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 <kjahds@kjahds.com>,
  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 <asm/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 struct fb_fix_screeninfo mc68x328fb_fix __initdata = {
  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. int mc68x328fb_init(void);
  81. int mc68x328fb_setup(char *);
  82. static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
  83. struct fb_info *info);
  84. static int mc68x328fb_set_par(struct fb_info *info);
  85. static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  86. u_int transp, struct fb_info *info);
  87. static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
  88. struct fb_info *info);
  89. static int mc68x328fb_mmap(struct fb_info *info, struct vm_area_struct *vma);
  90. static struct fb_ops mc68x328fb_ops = {
  91. .fb_check_var = mc68x328fb_check_var,
  92. .fb_set_par = mc68x328fb_set_par,
  93. .fb_setcolreg = mc68x328fb_setcolreg,
  94. .fb_pan_display = mc68x328fb_pan_display,
  95. .fb_fillrect = cfb_fillrect,
  96. .fb_copyarea = cfb_copyarea,
  97. .fb_imageblit = cfb_imageblit,
  98. .fb_mmap = mc68x328fb_mmap,
  99. };
  100. /*
  101. * Internal routines
  102. */
  103. static u_long get_line_length(int xres_virtual, int bpp)
  104. {
  105. u_long length;
  106. length = xres_virtual * bpp;
  107. length = (length + 31) & ~31;
  108. length >>= 3;
  109. return (length);
  110. }
  111. /*
  112. * Setting the video mode has been split into two parts.
  113. * First part, xxxfb_check_var, must not write anything
  114. * to hardware, it should only verify and adjust var.
  115. * This means it doesn't alter par but it does use hardware
  116. * data from it to check this var.
  117. */
  118. static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
  119. struct fb_info *info)
  120. {
  121. u_long line_length;
  122. /*
  123. * FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
  124. * as FB_VMODE_SMOOTH_XPAN is only used internally
  125. */
  126. if (var->vmode & FB_VMODE_CONUPDATE) {
  127. var->vmode |= FB_VMODE_YWRAP;
  128. var->xoffset = info->var.xoffset;
  129. var->yoffset = info->var.yoffset;
  130. }
  131. /*
  132. * Some very basic checks
  133. */
  134. if (!var->xres)
  135. var->xres = 1;
  136. if (!var->yres)
  137. var->yres = 1;
  138. if (var->xres > var->xres_virtual)
  139. var->xres_virtual = var->xres;
  140. if (var->yres > var->yres_virtual)
  141. var->yres_virtual = var->yres;
  142. if (var->bits_per_pixel <= 1)
  143. var->bits_per_pixel = 1;
  144. else if (var->bits_per_pixel <= 8)
  145. var->bits_per_pixel = 8;
  146. else if (var->bits_per_pixel <= 16)
  147. var->bits_per_pixel = 16;
  148. else if (var->bits_per_pixel <= 24)
  149. var->bits_per_pixel = 24;
  150. else if (var->bits_per_pixel <= 32)
  151. var->bits_per_pixel = 32;
  152. else
  153. return -EINVAL;
  154. if (var->xres_virtual < var->xoffset + var->xres)
  155. var->xres_virtual = var->xoffset + var->xres;
  156. if (var->yres_virtual < var->yoffset + var->yres)
  157. var->yres_virtual = var->yoffset + var->yres;
  158. /*
  159. * Memory limit
  160. */
  161. line_length =
  162. get_line_length(var->xres_virtual, var->bits_per_pixel);
  163. if (line_length * var->yres_virtual > videomemorysize)
  164. return -ENOMEM;
  165. /*
  166. * Now that we checked it we alter var. The reason being is that the video
  167. * mode passed in might not work but slight changes to it might make it
  168. * work. This way we let the user know what is acceptable.
  169. */
  170. switch (var->bits_per_pixel) {
  171. case 1:
  172. var->red.offset = 0;
  173. var->red.length = 1;
  174. var->green.offset = 0;
  175. var->green.length = 1;
  176. var->blue.offset = 0;
  177. var->blue.length = 1;
  178. var->transp.offset = 0;
  179. var->transp.length = 0;
  180. break;
  181. case 8:
  182. var->red.offset = 0;
  183. var->red.length = 8;
  184. var->green.offset = 0;
  185. var->green.length = 8;
  186. var->blue.offset = 0;
  187. var->blue.length = 8;
  188. var->transp.offset = 0;
  189. var->transp.length = 0;
  190. break;
  191. case 16: /* RGBA 5551 */
  192. if (var->transp.length) {
  193. var->red.offset = 0;
  194. var->red.length = 5;
  195. var->green.offset = 5;
  196. var->green.length = 5;
  197. var->blue.offset = 10;
  198. var->blue.length = 5;
  199. var->transp.offset = 15;
  200. var->transp.length = 1;
  201. } else { /* RGB 565 */
  202. var->red.offset = 0;
  203. var->red.length = 5;
  204. var->green.offset = 5;
  205. var->green.length = 6;
  206. var->blue.offset = 11;
  207. var->blue.length = 5;
  208. var->transp.offset = 0;
  209. var->transp.length = 0;
  210. }
  211. break;
  212. case 24: /* RGB 888 */
  213. var->red.offset = 0;
  214. var->red.length = 8;
  215. var->green.offset = 8;
  216. var->green.length = 8;
  217. var->blue.offset = 16;
  218. var->blue.length = 8;
  219. var->transp.offset = 0;
  220. var->transp.length = 0;
  221. break;
  222. case 32: /* RGBA 8888 */
  223. var->red.offset = 0;
  224. var->red.length = 8;
  225. var->green.offset = 8;
  226. var->green.length = 8;
  227. var->blue.offset = 16;
  228. var->blue.length = 8;
  229. var->transp.offset = 24;
  230. var->transp.length = 8;
  231. break;
  232. }
  233. var->red.msb_right = 0;
  234. var->green.msb_right = 0;
  235. var->blue.msb_right = 0;
  236. var->transp.msb_right = 0;
  237. return 0;
  238. }
  239. /* This routine actually sets the video mode. It's in here where we
  240. * the hardware state info->par and fix which can be affected by the
  241. * change in par. For this driver it doesn't do much.
  242. */
  243. static int mc68x328fb_set_par(struct fb_info *info)
  244. {
  245. info->fix.line_length = get_line_length(info->var.xres_virtual,
  246. info->var.bits_per_pixel);
  247. return 0;
  248. }
  249. /*
  250. * Set a single color register. The values supplied are already
  251. * rounded down to the hardware's capabilities (according to the
  252. * entries in the var structure). Return != 0 for invalid regno.
  253. */
  254. static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  255. u_int transp, struct fb_info *info)
  256. {
  257. if (regno >= 256) /* no. of hw registers */
  258. return 1;
  259. /*
  260. * Program hardware... do anything you want with transp
  261. */
  262. /* grayscale works only partially under directcolor */
  263. if (info->var.grayscale) {
  264. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  265. red = green = blue =
  266. (red * 77 + green * 151 + blue * 28) >> 8;
  267. }
  268. /* Directcolor:
  269. * var->{color}.offset contains start of bitfield
  270. * var->{color}.length contains length of bitfield
  271. * {hardwarespecific} contains width of RAMDAC
  272. * cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
  273. * RAMDAC[X] is programmed to (red, green, blue)
  274. *
  275. * Pseudocolor:
  276. * uses offset = 0 && length = RAMDAC register width.
  277. * var->{color}.offset is 0
  278. * var->{color}.length contains width of DAC
  279. * cmap is not used
  280. * RAMDAC[X] is programmed to (red, green, blue)
  281. * Truecolor:
  282. * does not use DAC. Usually 3 are present.
  283. * var->{color}.offset contains start of bitfield
  284. * var->{color}.length contains length of bitfield
  285. * cmap is programmed to (red << red.offset) | (green << green.offset) |
  286. * (blue << blue.offset) | (transp << transp.offset)
  287. * RAMDAC does not exist
  288. */
  289. #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
  290. switch (info->fix.visual) {
  291. case FB_VISUAL_TRUECOLOR:
  292. case FB_VISUAL_PSEUDOCOLOR:
  293. red = CNVT_TOHW(red, info->var.red.length);
  294. green = CNVT_TOHW(green, info->var.green.length);
  295. blue = CNVT_TOHW(blue, info->var.blue.length);
  296. transp = CNVT_TOHW(transp, info->var.transp.length);
  297. break;
  298. case FB_VISUAL_DIRECTCOLOR:
  299. red = CNVT_TOHW(red, 8); /* expect 8 bit DAC */
  300. green = CNVT_TOHW(green, 8);
  301. blue = CNVT_TOHW(blue, 8);
  302. /* hey, there is bug in transp handling... */
  303. transp = CNVT_TOHW(transp, 8);
  304. break;
  305. }
  306. #undef CNVT_TOHW
  307. /* Truecolor has hardware independent palette */
  308. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  309. u32 v;
  310. if (regno >= 16)
  311. return 1;
  312. v = (red << info->var.red.offset) |
  313. (green << info->var.green.offset) |
  314. (blue << info->var.blue.offset) |
  315. (transp << info->var.transp.offset);
  316. switch (info->var.bits_per_pixel) {
  317. case 8:
  318. break;
  319. case 16:
  320. ((u32 *) (info->pseudo_palette))[regno] = v;
  321. break;
  322. case 24:
  323. case 32:
  324. ((u32 *) (info->pseudo_palette))[regno] = v;
  325. break;
  326. }
  327. return 0;
  328. }
  329. return 0;
  330. }
  331. /*
  332. * Pan or Wrap the Display
  333. *
  334. * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
  335. */
  336. static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
  337. struct fb_info *info)
  338. {
  339. if (var->vmode & FB_VMODE_YWRAP) {
  340. if (var->yoffset < 0
  341. || var->yoffset >= info->var.yres_virtual
  342. || var->xoffset)
  343. return -EINVAL;
  344. } else {
  345. if (var->xoffset + info->var.xres > info->var.xres_virtual ||
  346. var->yoffset + info->var.yres > info->var.yres_virtual)
  347. return -EINVAL;
  348. }
  349. info->var.xoffset = var->xoffset;
  350. info->var.yoffset = var->yoffset;
  351. if (var->vmode & FB_VMODE_YWRAP)
  352. info->var.vmode |= FB_VMODE_YWRAP;
  353. else
  354. info->var.vmode &= ~FB_VMODE_YWRAP;
  355. return 0;
  356. }
  357. /*
  358. * Most drivers don't need their own mmap function
  359. */
  360. static int mc68x328fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  361. {
  362. #ifndef MMU
  363. /* this is uClinux (no MMU) specific code */
  364. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  365. vma->vm_start = videomemory;
  366. return 0;
  367. #else
  368. return -EINVAL;
  369. #endif
  370. }
  371. int __init mc68x328fb_setup(char *options)
  372. {
  373. #if 0
  374. char *this_opt;
  375. #endif
  376. if (!options || !*options)
  377. return 1;
  378. #if 0
  379. while ((this_opt = strsep(&options, ",")) != NULL) {
  380. if (!*this_opt)
  381. continue;
  382. if (!strncmp(this_opt, "disable", 7))
  383. mc68x328fb_enable = 0;
  384. }
  385. #endif
  386. return 1;
  387. }
  388. /*
  389. * Initialisation
  390. */
  391. int __init mc68x328fb_init(void)
  392. {
  393. #ifndef MODULE
  394. char *option = NULL;
  395. if (fb_get_options("68328fb", &option))
  396. return -ENODEV;
  397. mc68x328fb_setup(option);
  398. #endif
  399. /*
  400. * initialize the default mode from the LCD controller registers
  401. */
  402. mc68x328fb_default.xres = LXMAX;
  403. mc68x328fb_default.yres = LYMAX+1;
  404. mc68x328fb_default.xres_virtual = mc68x328fb_default.xres;
  405. mc68x328fb_default.yres_virtual = mc68x328fb_default.yres;
  406. mc68x328fb_default.bits_per_pixel = 1 + (LPICF & 0x01);
  407. videomemory = LSSA;
  408. videomemorysize = (mc68x328fb_default.xres_virtual+7) / 8 *
  409. mc68x328fb_default.yres_virtual * mc68x328fb_default.bits_per_pixel;
  410. fb_info.screen_base = (void *)videomemory;
  411. fb_info.fbops = &mc68x328fb_ops;
  412. fb_info.var = mc68x328fb_default;
  413. fb_info.fix = mc68x328fb_fix;
  414. fb_info.fix.smem_start = videomemory;
  415. fb_info.fix.smem_len = videomemorysize;
  416. fb_info.fix.line_length =
  417. get_line_length(mc68x328fb_default.xres_virtual, mc68x328fb_default.bits_per_pixel);
  418. fb_info.fix.visual = (mc68x328fb_default.bits_per_pixel) == 1 ?
  419. FB_VISUAL_MONO10 : FB_VISUAL_PSEUDOCOLOR;
  420. if (fb_info.var.bits_per_pixel == 1) {
  421. fb_info.var.red.length = fb_info.var.green.length = fb_info.var.blue.length = 1;
  422. fb_info.var.red.offset = fb_info.var.green.offset = fb_info.var.blue.offset = 0;
  423. }
  424. fb_info.pseudo_palette = &mc68x328fb_pseudo_palette;
  425. fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
  426. if (fb_alloc_cmap(&fb_info.cmap, 256, 0))
  427. return -ENOMEM;
  428. if (register_framebuffer(&fb_info) < 0) {
  429. fb_dealloc_cmap(&fb_info.cmap);
  430. return -EINVAL;
  431. }
  432. fb_info(&fb_info, "%s frame buffer device\n", fb_info.fix.id);
  433. fb_info(&fb_info, "%dx%dx%d at 0x%08lx\n",
  434. mc68x328fb_default.xres_virtual,
  435. mc68x328fb_default.yres_virtual,
  436. 1 << mc68x328fb_default.bits_per_pixel, videomemory);
  437. return 0;
  438. }
  439. module_init(mc68x328fb_init);
  440. #ifdef MODULE
  441. static void __exit mc68x328fb_cleanup(void)
  442. {
  443. unregister_framebuffer(&fb_info);
  444. fb_dealloc_cmap(&fb_info.cmap);
  445. }
  446. module_exit(mc68x328fb_cleanup);
  447. MODULE_LICENSE("GPL");
  448. #endif /* MODULE */