fsl_diu_fb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. static struct fb_videomode fsl_diu_mode_1280_720 = {
  103. .name = "1280x720-60",
  104. .refresh = 60,
  105. .xres = 1280,
  106. .yres = 720,
  107. .pixclock = 13426,
  108. .left_margin = 192,
  109. .right_margin = 64,
  110. .upper_margin = 22,
  111. .lower_margin = 1,
  112. .hsync_len = 136,
  113. .vsync_len = 3,
  114. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  115. .vmode = FB_VMODE_NONINTERLACED
  116. };
  117. static struct fb_videomode fsl_diu_mode_1920_1080 = {
  118. .name = "1920x1080-60",
  119. .refresh = 60,
  120. .xres = 1920,
  121. .yres = 1080,
  122. .pixclock = 5787,
  123. .left_margin = 328,
  124. .right_margin = 120,
  125. .upper_margin = 34,
  126. .lower_margin = 1,
  127. .hsync_len = 208,
  128. .vsync_len = 3,
  129. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  130. .vmode = FB_VMODE_NONINTERLACED
  131. };
  132. /*
  133. * These are the fields of area descriptor(in DDR memory) for every plane
  134. */
  135. struct diu_ad {
  136. /* Word 0(32-bit) in DDR memory */
  137. __le32 pix_fmt; /* hard coding pixel format */
  138. /* Word 1(32-bit) in DDR memory */
  139. __le32 addr;
  140. /* Word 2(32-bit) in DDR memory */
  141. __le32 src_size_g_alpha;
  142. /* Word 3(32-bit) in DDR memory */
  143. __le32 aoi_size;
  144. /* Word 4(32-bit) in DDR memory */
  145. __le32 offset_xyi;
  146. /* Word 5(32-bit) in DDR memory */
  147. __le32 offset_xyd;
  148. /* Word 6(32-bit) in DDR memory */
  149. __le32 ckmax_r:8;
  150. __le32 ckmax_g:8;
  151. __le32 ckmax_b:8;
  152. __le32 res9:8;
  153. /* Word 7(32-bit) in DDR memory */
  154. __le32 ckmin_r:8;
  155. __le32 ckmin_g:8;
  156. __le32 ckmin_b:8;
  157. __le32 res10:8;
  158. /* Word 8(32-bit) in DDR memory */
  159. __le32 next_ad;
  160. /* Word 9(32-bit) in DDR memory, just for 64-bit aligned */
  161. __le32 res[3];
  162. } __attribute__ ((packed));
  163. /*
  164. * DIU register map
  165. */
  166. struct diu {
  167. __be32 desc[3];
  168. __be32 gamma;
  169. __be32 pallete;
  170. __be32 cursor;
  171. __be32 curs_pos;
  172. __be32 diu_mode;
  173. __be32 bgnd;
  174. __be32 bgnd_wb;
  175. __be32 disp_size;
  176. __be32 wb_size;
  177. __be32 wb_mem_addr;
  178. __be32 hsyn_para;
  179. __be32 vsyn_para;
  180. __be32 syn_pol;
  181. __be32 thresholds;
  182. __be32 int_status;
  183. __be32 int_mask;
  184. __be32 colorbar[8];
  185. __be32 filling;
  186. __be32 plut;
  187. } __attribute__ ((packed));
  188. struct diu_addr {
  189. void *vaddr; /* Virtual address */
  190. u32 paddr; /* 32-bit physical address */
  191. unsigned int offset; /* Alignment offset */
  192. };
  193. static struct fb_info info;
  194. /*
  195. * Align to 64-bit(8-byte), 32-byte, etc.
  196. */
  197. static int allocate_buf(struct diu_addr *buf, u32 size, u32 bytes_align)
  198. {
  199. u32 offset, ssize;
  200. u32 mask;
  201. ssize = size + bytes_align;
  202. buf->vaddr = malloc(ssize);
  203. if (!buf->vaddr)
  204. return -1;
  205. memset(buf->vaddr, 0, ssize);
  206. mask = bytes_align - 1;
  207. offset = (u32)buf->vaddr & mask;
  208. if (offset) {
  209. buf->offset = bytes_align - offset;
  210. buf->vaddr += offset;
  211. } else
  212. buf->offset = 0;
  213. buf->paddr = virt_to_phys(buf->vaddr);
  214. return 0;
  215. }
  216. /*
  217. * Allocate a framebuffer and an Area Descriptor that points to it. Both
  218. * are created in the same memory block. The Area Descriptor is updated to
  219. * point to the framebuffer memory. Memory is aligned as needed.
  220. */
  221. static struct diu_ad *allocate_fb(unsigned int xres, unsigned int yres,
  222. unsigned int depth, char **fb)
  223. {
  224. unsigned long size = xres * yres * depth;
  225. struct diu_addr addr;
  226. struct diu_ad *ad;
  227. size_t ad_size = roundup(sizeof(struct diu_ad), 32);
  228. /*
  229. * Allocate a memory block that holds the Area Descriptor and the
  230. * frame buffer right behind it. To keep the code simple, everything
  231. * is aligned on a 32-byte address.
  232. */
  233. if (allocate_buf(&addr, ad_size + size, 32) < 0)
  234. return NULL;
  235. ad = addr.vaddr;
  236. ad->addr = cpu_to_le32(addr.paddr + ad_size);
  237. ad->aoi_size = cpu_to_le32((yres << 16) | xres);
  238. ad->src_size_g_alpha = cpu_to_le32((yres << 12) | xres);
  239. ad->offset_xyi = 0;
  240. ad->offset_xyd = 0;
  241. if (fb)
  242. *fb = addr.vaddr + ad_size;
  243. return ad;
  244. }
  245. int fsl_diu_init(u16 xres, u16 yres, u32 pixel_format, int gamma_fix)
  246. {
  247. struct fb_videomode *fsl_diu_mode_db;
  248. struct diu_ad *ad;
  249. struct diu *hw = (struct diu *)CONFIG_SYS_DIU_ADDR;
  250. u8 *gamma_table_base;
  251. unsigned int i, j;
  252. struct diu_addr gamma;
  253. struct diu_addr cursor;
  254. /* Convert the X,Y resolution pair into a single number */
  255. #define RESOLUTION(x, y) (((u32)(x) << 16) | (y))
  256. switch (RESOLUTION(xres, yres)) {
  257. case RESOLUTION(800, 480):
  258. fsl_diu_mode_db = &fsl_diu_mode_800_480;
  259. break;
  260. case RESOLUTION(800, 600):
  261. fsl_diu_mode_db = &fsl_diu_mode_800_600;
  262. break;
  263. case RESOLUTION(1024, 768):
  264. fsl_diu_mode_db = &fsl_diu_mode_1024_768;
  265. break;
  266. case RESOLUTION(1280, 1024):
  267. fsl_diu_mode_db = &fsl_diu_mode_1280_1024;
  268. break;
  269. case RESOLUTION(1280, 720):
  270. fsl_diu_mode_db = &fsl_diu_mode_1280_720;
  271. break;
  272. case RESOLUTION(1920, 1080):
  273. fsl_diu_mode_db = &fsl_diu_mode_1920_1080;
  274. break;
  275. default:
  276. printf("DIU: Unsupported resolution %ux%u\n", xres, yres);
  277. return -1;
  278. }
  279. /* read mode info */
  280. info.var.xres = fsl_diu_mode_db->xres;
  281. info.var.yres = fsl_diu_mode_db->yres;
  282. info.var.bits_per_pixel = 32;
  283. info.var.pixclock = fsl_diu_mode_db->pixclock;
  284. info.var.left_margin = fsl_diu_mode_db->left_margin;
  285. info.var.right_margin = fsl_diu_mode_db->right_margin;
  286. info.var.upper_margin = fsl_diu_mode_db->upper_margin;
  287. info.var.lower_margin = fsl_diu_mode_db->lower_margin;
  288. info.var.hsync_len = fsl_diu_mode_db->hsync_len;
  289. info.var.vsync_len = fsl_diu_mode_db->vsync_len;
  290. info.var.sync = fsl_diu_mode_db->sync;
  291. info.var.vmode = fsl_diu_mode_db->vmode;
  292. info.fix.line_length = info.var.xres * info.var.bits_per_pixel / 8;
  293. /* Memory allocation for framebuffer */
  294. info.screen_size =
  295. info.var.xres * info.var.yres * (info.var.bits_per_pixel / 8);
  296. ad = allocate_fb(info.var.xres, info.var.yres,
  297. info.var.bits_per_pixel / 8, &info.screen_base);
  298. if (!ad) {
  299. printf("DIU: Out of memory\n");
  300. return -1;
  301. }
  302. ad->pix_fmt = pixel_format;
  303. /* Disable chroma keying function */
  304. ad->ckmax_r = 0;
  305. ad->ckmax_g = 0;
  306. ad->ckmax_b = 0;
  307. ad->ckmin_r = 255;
  308. ad->ckmin_g = 255;
  309. ad->ckmin_b = 255;
  310. /* Initialize the gamma table */
  311. if (allocate_buf(&gamma, 256 * 3, 32) < 0) {
  312. printf("DIU: Out of memory\n");
  313. return -1;
  314. }
  315. gamma_table_base = gamma.vaddr;
  316. for (i = 0; i <= 2; i++)
  317. for (j = 0; j < 256; j++)
  318. *gamma_table_base++ = j;
  319. if (gamma_fix == 1) { /* fix the gamma */
  320. gamma_table_base = gamma.vaddr;
  321. for (i = 0; i < 256 * 3; i++) {
  322. gamma_table_base[i] = (gamma_table_base[i] << 2)
  323. | ((gamma_table_base[i] >> 6) & 0x03);
  324. }
  325. }
  326. /* Initialize the cursor */
  327. if (allocate_buf(&cursor, 32 * 32 * 2, 32) < 0) {
  328. printf("DIU: Can't alloc cursor data\n");
  329. return -1;
  330. }
  331. /* Program DIU registers */
  332. out_be32(&hw->diu_mode, 0); /* Temporarily disable the DIU */
  333. out_be32(&hw->gamma, gamma.paddr);
  334. out_be32(&hw->cursor, cursor.paddr);
  335. out_be32(&hw->bgnd, 0x007F7F7F);
  336. out_be32(&hw->disp_size, info.var.yres << 16 | info.var.xres);
  337. out_be32(&hw->hsyn_para, info.var.left_margin << 22 |
  338. info.var.hsync_len << 11 |
  339. info.var.right_margin);
  340. out_be32(&hw->vsyn_para, info.var.upper_margin << 22 |
  341. info.var.vsync_len << 11 |
  342. info.var.lower_margin);
  343. /* Pixel Clock configuration */
  344. diu_set_pixel_clock(info.var.pixclock);
  345. /* Set the frame buffers */
  346. out_be32(&hw->desc[0], virt_to_phys(ad));
  347. out_be32(&hw->desc[1], 0);
  348. out_be32(&hw->desc[2], 0);
  349. /* Enable the DIU, set display to all three planes */
  350. out_be32(&hw->diu_mode, 1);
  351. return 0;
  352. }
  353. void *video_hw_init(void)
  354. {
  355. static GraphicDevice ctfb;
  356. const char *options;
  357. unsigned int depth = 0, freq = 0;
  358. if (!video_get_video_mode(&ctfb.winSizeX, &ctfb.winSizeY, &depth, &freq,
  359. &options))
  360. return NULL;
  361. /* Find the monitor port, which is a required option */
  362. if (!options)
  363. return NULL;
  364. if (strncmp(options, "monitor=", 8) != 0)
  365. return NULL;
  366. if (platform_diu_init(ctfb.winSizeX, ctfb.winSizeY, options + 8) < 0)
  367. return NULL;
  368. /* fill in Graphic device struct */
  369. sprintf(ctfb.modeIdent, "%ix%ix%i %ikHz %iHz",
  370. ctfb.winSizeX, ctfb.winSizeY, depth, 64, freq);
  371. ctfb.frameAdrs = (unsigned int)info.screen_base;
  372. ctfb.plnSizeX = ctfb.winSizeX;
  373. ctfb.plnSizeY = ctfb.winSizeY;
  374. ctfb.gdfBytesPP = 4;
  375. ctfb.gdfIndex = GDF_32BIT_X888RGB;
  376. ctfb.isaBase = 0;
  377. ctfb.pciBase = 0;
  378. ctfb.memSize = info.screen_size;
  379. /* Cursor Start Address */
  380. ctfb.dprBase = 0;
  381. ctfb.vprBase = 0;
  382. ctfb.cprBase = 0;
  383. return &ctfb;
  384. }