68328fb.c 13 KB

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