68328fb.c 13 KB

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