68328fb.c 13 KB

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