fsl_diu_fb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc.
  3. * Authors: York Sun <yorksun@freescale.com>
  4. * Timur Tabi <timur@freescale.com>
  5. *
  6. * FSL DIU Framebuffer driver
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <malloc.h>
  28. #include <asm/io.h>
  29. #include "videomodes.h"
  30. #include <video_fb.h>
  31. #include <fsl_diu_fb.h>
  32. #include <linux/list.h>
  33. #include <linux/fb.h>
  34. /* This setting is used for the ifm pdm360ng with PRIMEVIEW PM070WL3 */
  35. static struct fb_videomode fsl_diu_mode_800_480 = {
  36. .name = "800x480-60",
  37. .refresh = 60,
  38. .xres = 800,
  39. .yres = 480,
  40. .pixclock = 31250,
  41. .left_margin = 86,
  42. .right_margin = 42,
  43. .upper_margin = 33,
  44. .lower_margin = 10,
  45. .hsync_len = 128,
  46. .vsync_len = 2,
  47. .sync = 0,
  48. .vmode = FB_VMODE_NONINTERLACED
  49. };
  50. /* For the SHARP LQ084S3LG01, used on the P1022DS board */
  51. static struct fb_videomode fsl_diu_mode_800_600 = {
  52. .name = "800x600-60",
  53. .refresh = 60,
  54. .xres = 800,
  55. .yres = 600,
  56. .pixclock = 25000,
  57. .left_margin = 88,
  58. .right_margin = 40,
  59. .upper_margin = 23,
  60. .lower_margin = 1,
  61. .hsync_len = 128,
  62. .vsync_len = 4,
  63. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  64. .vmode = FB_VMODE_NONINTERLACED
  65. };
  66. /*
  67. * These parameters give default parameters
  68. * for video output 1024x768,
  69. * FIXME - change timing to proper amounts
  70. * hsync 31.5kHz, vsync 60Hz
  71. */
  72. static struct fb_videomode fsl_diu_mode_1024_768 = {
  73. .name = "1024x768-60",
  74. .refresh = 60,
  75. .xres = 1024,
  76. .yres = 768,
  77. .pixclock = 15385,
  78. .left_margin = 160,
  79. .right_margin = 24,
  80. .upper_margin = 29,
  81. .lower_margin = 3,
  82. .hsync_len = 136,
  83. .vsync_len = 6,
  84. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  85. .vmode = FB_VMODE_NONINTERLACED
  86. };
  87. static struct fb_videomode fsl_diu_mode_1280_1024 = {
  88. .name = "1280x1024-60",
  89. .refresh = 60,
  90. .xres = 1280,
  91. .yres = 1024,
  92. .pixclock = 9375,
  93. .left_margin = 38,
  94. .right_margin = 128,
  95. .upper_margin = 2,
  96. .lower_margin = 7,
  97. .hsync_len = 216,
  98. .vsync_len = 37,
  99. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  100. .vmode = FB_VMODE_NONINTERLACED
  101. };
  102. /*
  103. * These are the fields of area descriptor(in DDR memory) for every plane
  104. */
  105. struct diu_ad {
  106. /* Word 0(32-bit) in DDR memory */
  107. __le32 pix_fmt; /* hard coding pixel format */
  108. /* Word 1(32-bit) in DDR memory */
  109. __le32 addr;
  110. /* Word 2(32-bit) in DDR memory */
  111. __le32 src_size_g_alpha;
  112. /* Word 3(32-bit) in DDR memory */
  113. __le32 aoi_size;
  114. /* Word 4(32-bit) in DDR memory */
  115. __le32 offset_xyi;
  116. /* Word 5(32-bit) in DDR memory */
  117. __le32 offset_xyd;
  118. /* Word 6(32-bit) in DDR memory */
  119. __le32 ckmax_r:8;
  120. __le32 ckmax_g:8;
  121. __le32 ckmax_b:8;
  122. __le32 res9:8;
  123. /* Word 7(32-bit) in DDR memory */
  124. __le32 ckmin_r:8;
  125. __le32 ckmin_g:8;
  126. __le32 ckmin_b:8;
  127. __le32 res10:8;
  128. /* Word 8(32-bit) in DDR memory */
  129. __le32 next_ad;
  130. /* Word 9(32-bit) in DDR memory, just for 64-bit aligned */
  131. __le32 res[3];
  132. } __attribute__ ((packed));
  133. /*
  134. * DIU register map
  135. */
  136. struct diu {
  137. __be32 desc[3];
  138. __be32 gamma;
  139. __be32 pallete;
  140. __be32 cursor;
  141. __be32 curs_pos;
  142. __be32 diu_mode;
  143. __be32 bgnd;
  144. __be32 bgnd_wb;
  145. __be32 disp_size;
  146. __be32 wb_size;
  147. __be32 wb_mem_addr;
  148. __be32 hsyn_para;
  149. __be32 vsyn_para;
  150. __be32 syn_pol;
  151. __be32 thresholds;
  152. __be32 int_status;
  153. __be32 int_mask;
  154. __be32 colorbar[8];
  155. __be32 filling;
  156. __be32 plut;
  157. } __attribute__ ((packed));
  158. struct diu_addr {
  159. void *vaddr; /* Virtual address */
  160. u32 paddr; /* 32-bit physical address */
  161. unsigned int offset; /* Alignment offset */
  162. };
  163. static struct fb_info info;
  164. /*
  165. * Align to 64-bit(8-byte), 32-byte, etc.
  166. */
  167. static int allocate_buf(struct diu_addr *buf, u32 size, u32 bytes_align)
  168. {
  169. u32 offset, ssize;
  170. u32 mask;
  171. ssize = size + bytes_align;
  172. buf->vaddr = malloc(ssize);
  173. if (!buf->vaddr)
  174. return -1;
  175. memset(buf->vaddr, 0, ssize);
  176. mask = bytes_align - 1;
  177. offset = (u32)buf->vaddr & mask;
  178. if (offset) {
  179. buf->offset = bytes_align - offset;
  180. buf->vaddr += offset;
  181. } else
  182. buf->offset = 0;
  183. buf->paddr = virt_to_phys(buf->vaddr);
  184. return 0;
  185. }
  186. /*
  187. * Allocate a framebuffer and an Area Descriptor that points to it. Both
  188. * are created in the same memory block. The Area Descriptor is updated to
  189. * point to the framebuffer memory. Memory is aligned as needed.
  190. */
  191. static struct diu_ad *allocate_fb(unsigned int xres, unsigned int yres,
  192. unsigned int depth, char **fb)
  193. {
  194. unsigned long size = xres * yres * depth;
  195. struct diu_addr addr;
  196. struct diu_ad *ad;
  197. size_t ad_size = roundup(sizeof(struct diu_ad), 32);
  198. /*
  199. * Allocate a memory block that holds the Area Descriptor and the
  200. * frame buffer right behind it. To keep the code simple, everything
  201. * is aligned on a 32-byte address.
  202. */
  203. if (allocate_buf(&addr, ad_size + size, 32) < 0)
  204. return NULL;
  205. ad = addr.vaddr;
  206. ad->addr = cpu_to_le32(addr.paddr + ad_size);
  207. ad->aoi_size = cpu_to_le32((yres << 16) | xres);
  208. ad->src_size_g_alpha = cpu_to_le32((yres << 12) | xres);
  209. ad->offset_xyi = 0;
  210. ad->offset_xyd = 0;
  211. if (fb)
  212. *fb = addr.vaddr + ad_size;
  213. return ad;
  214. }
  215. int fsl_diu_init(u16 xres, u16 yres, u32 pixel_format, int gamma_fix)
  216. {
  217. struct fb_videomode *fsl_diu_mode_db;
  218. struct diu_ad *ad;
  219. struct diu *hw = (struct diu *)CONFIG_SYS_DIU_ADDR;
  220. u8 *gamma_table_base;
  221. unsigned int i, j;
  222. struct diu_ad *dummy_ad;
  223. struct diu_addr gamma;
  224. struct diu_addr cursor;
  225. /* Convert the X,Y resolution pair into a single number */
  226. #define RESOLUTION(x, y) (((u32)(x) << 16) | (y))
  227. switch (RESOLUTION(xres, yres)) {
  228. case RESOLUTION(800, 480):
  229. fsl_diu_mode_db = &fsl_diu_mode_800_480;
  230. break;
  231. case RESOLUTION(800, 600):
  232. fsl_diu_mode_db = &fsl_diu_mode_800_600;
  233. case RESOLUTION(1024, 768):
  234. fsl_diu_mode_db = &fsl_diu_mode_1024_768;
  235. case RESOLUTION(1280, 1024):
  236. fsl_diu_mode_db = &fsl_diu_mode_1280_1024;
  237. break;
  238. default:
  239. printf("DIU: Unsupported resolution %ux%u\n", xres, yres);
  240. return -1;
  241. }
  242. /* The AD struct for the dummy framebuffer and the FB itself */
  243. dummy_ad = allocate_fb(2, 4, 4, NULL);
  244. if (!dummy_ad) {
  245. printf("DIU: Out of memory\n");
  246. return -1;
  247. }
  248. dummy_ad->pix_fmt = 0x88883316;
  249. /* read mode info */
  250. info.var.xres = fsl_diu_mode_db->xres;
  251. info.var.yres = fsl_diu_mode_db->yres;
  252. info.var.bits_per_pixel = 32;
  253. info.var.pixclock = fsl_diu_mode_db->pixclock;
  254. info.var.left_margin = fsl_diu_mode_db->left_margin;
  255. info.var.right_margin = fsl_diu_mode_db->right_margin;
  256. info.var.upper_margin = fsl_diu_mode_db->upper_margin;
  257. info.var.lower_margin = fsl_diu_mode_db->lower_margin;
  258. info.var.hsync_len = fsl_diu_mode_db->hsync_len;
  259. info.var.vsync_len = fsl_diu_mode_db->vsync_len;
  260. info.var.sync = fsl_diu_mode_db->sync;
  261. info.var.vmode = fsl_diu_mode_db->vmode;
  262. info.fix.line_length = info.var.xres * info.var.bits_per_pixel / 8;
  263. /* Memory allocation for framebuffer */
  264. info.screen_size =
  265. info.var.xres * info.var.yres * (info.var.bits_per_pixel / 8);
  266. ad = allocate_fb(info.var.xres, info.var.yres,
  267. info.var.bits_per_pixel / 8, &info.screen_base);
  268. if (!ad) {
  269. printf("DIU: Out of memory\n");
  270. return -1;
  271. }
  272. ad->pix_fmt = pixel_format;
  273. /* Disable chroma keying function */
  274. ad->ckmax_r = 0;
  275. ad->ckmax_g = 0;
  276. ad->ckmax_b = 0;
  277. ad->ckmin_r = 255;
  278. ad->ckmin_g = 255;
  279. ad->ckmin_b = 255;
  280. /* Initialize the gamma table */
  281. if (allocate_buf(&gamma, 256 * 3, 32) < 0) {
  282. printf("DIU: Out of memory\n");
  283. return -1;
  284. }
  285. gamma_table_base = gamma.vaddr;
  286. for (i = 0; i <= 2; i++)
  287. for (j = 0; j < 256; j++)
  288. *gamma_table_base++ = j;
  289. if (gamma_fix == 1) { /* fix the gamma */
  290. gamma_table_base = gamma.vaddr;
  291. for (i = 0; i < 256 * 3; i++) {
  292. gamma_table_base[i] = (gamma_table_base[i] << 2)
  293. | ((gamma_table_base[i] >> 6) & 0x03);
  294. }
  295. }
  296. /* Initialize the cursor */
  297. if (allocate_buf(&cursor, 32 * 32 * 2, 32) < 0) {
  298. printf("DIU: Can't alloc cursor data\n");
  299. return -1;
  300. }
  301. /* Program DIU registers */
  302. out_be32(&hw->diu_mode, 0); /* Temporarily disable the DIU */
  303. out_be32(&hw->gamma, gamma.paddr);
  304. out_be32(&hw->cursor, cursor.paddr);
  305. out_be32(&hw->bgnd, 0x007F7F7F);
  306. out_be32(&hw->bgnd_wb, 0);
  307. out_be32(&hw->disp_size, info.var.yres << 16 | info.var.xres);
  308. out_be32(&hw->wb_size, 0);
  309. out_be32(&hw->wb_mem_addr, 0);
  310. out_be32(&hw->hsyn_para, info.var.left_margin << 22 |
  311. info.var.hsync_len << 11 |
  312. info.var.right_margin);
  313. out_be32(&hw->vsyn_para, info.var.upper_margin << 22 |
  314. info.var.vsync_len << 11 |
  315. info.var.lower_margin);
  316. out_be32(&hw->syn_pol, 0);
  317. out_be32(&hw->thresholds, 0x00037800);
  318. out_be32(&hw->int_status, 0);
  319. out_be32(&hw->int_mask, 0);
  320. out_be32(&hw->plut, 0x01F5F666);
  321. /* Pixel Clock configuration */
  322. diu_set_pixel_clock(info.var.pixclock);
  323. /* Set the frame buffers */
  324. out_be32(&hw->desc[0], virt_to_phys(ad));
  325. out_be32(&hw->desc[1], virt_to_phys(dummy_ad));
  326. out_be32(&hw->desc[2], virt_to_phys(dummy_ad));
  327. /* Enable the DIU, set display to all three planes */
  328. out_be32(&hw->diu_mode, 1);
  329. return 0;
  330. }
  331. void *video_hw_init(void)
  332. {
  333. static GraphicDevice ctfb;
  334. const char *options;
  335. unsigned int depth = 0, freq = 0;
  336. if (!video_get_video_mode(&ctfb.winSizeX, &ctfb.winSizeY, &depth, &freq,
  337. &options))
  338. return NULL;
  339. /* Find the monitor port, which is a required option */
  340. if (!options)
  341. return NULL;
  342. if (strncmp(options, "monitor=", 8) != 0)
  343. return NULL;
  344. if (platform_diu_init(ctfb.winSizeX, ctfb.winSizeY, options + 8) < 0)
  345. return NULL;
  346. /* fill in Graphic device struct */
  347. sprintf(ctfb.modeIdent, "%ix%ix%i %ikHz %iHz",
  348. ctfb.winSizeX, ctfb.winSizeY, depth, 64, freq);
  349. ctfb.frameAdrs = (unsigned int)info.screen_base;
  350. ctfb.plnSizeX = ctfb.winSizeX;
  351. ctfb.plnSizeY = ctfb.winSizeY;
  352. ctfb.gdfBytesPP = 4;
  353. ctfb.gdfIndex = GDF_32BIT_X888RGB;
  354. ctfb.isaBase = 0;
  355. ctfb.pciBase = 0;
  356. ctfb.memSize = info.screen_size;
  357. /* Cursor Start Address */
  358. ctfb.dprBase = 0;
  359. ctfb.vprBase = 0;
  360. ctfb.cprBase = 0;
  361. return &ctfb;
  362. }