68328fb.c 13 KB

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