mxc_ipuv3_fb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Porting to u-boot:
  3. *
  4. * (C) Copyright 2010
  5. * Stefano Babic, DENX Software Engineering, sbabic@denx.de
  6. *
  7. * MX51 Linux framebuffer:
  8. *
  9. * (C) Copyright 2004-2010 Freescale Semiconductor, Inc.
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #include <asm/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/list.h>
  33. #include <linux/fb.h>
  34. #include <asm/io.h>
  35. #include <malloc.h>
  36. #include <video_fb.h>
  37. #include "videomodes.h"
  38. #include "ipu.h"
  39. #include "mxcfb.h"
  40. static int mxcfb_map_video_memory(struct fb_info *fbi);
  41. static int mxcfb_unmap_video_memory(struct fb_info *fbi);
  42. /* graphics setup */
  43. static GraphicDevice panel;
  44. struct fb_videomode *gmode;
  45. void fb_videomode_to_var(struct fb_var_screeninfo *var,
  46. const struct fb_videomode *mode)
  47. {
  48. var->xres = mode->xres;
  49. var->yres = mode->yres;
  50. var->xres_virtual = mode->xres;
  51. var->yres_virtual = mode->yres;
  52. var->xoffset = 0;
  53. var->yoffset = 0;
  54. var->pixclock = mode->pixclock;
  55. var->left_margin = mode->left_margin;
  56. var->right_margin = mode->right_margin;
  57. var->upper_margin = mode->upper_margin;
  58. var->lower_margin = mode->lower_margin;
  59. var->hsync_len = mode->hsync_len;
  60. var->vsync_len = mode->vsync_len;
  61. var->sync = mode->sync;
  62. var->vmode = mode->vmode & FB_VMODE_MASK;
  63. }
  64. /*
  65. * Structure containing the MXC specific framebuffer information.
  66. */
  67. struct mxcfb_info {
  68. int blank;
  69. ipu_channel_t ipu_ch;
  70. int ipu_di;
  71. u32 ipu_di_pix_fmt;
  72. unsigned char overlay;
  73. unsigned char alpha_chan_en;
  74. dma_addr_t alpha_phy_addr0;
  75. dma_addr_t alpha_phy_addr1;
  76. void *alpha_virt_addr0;
  77. void *alpha_virt_addr1;
  78. uint32_t alpha_mem_len;
  79. uint32_t cur_ipu_buf;
  80. uint32_t cur_ipu_alpha_buf;
  81. u32 pseudo_palette[16];
  82. };
  83. enum {
  84. BOTH_ON,
  85. SRC_ON,
  86. TGT_ON,
  87. BOTH_OFF
  88. };
  89. static unsigned long default_bpp = 16;
  90. static unsigned char g_dp_in_use;
  91. static struct fb_info *mxcfb_info[3];
  92. static int ext_clk_used;
  93. static uint32_t bpp_to_pixfmt(struct fb_info *fbi)
  94. {
  95. uint32_t pixfmt = 0;
  96. debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel);
  97. if (fbi->var.nonstd)
  98. return fbi->var.nonstd;
  99. switch (fbi->var.bits_per_pixel) {
  100. case 24:
  101. pixfmt = IPU_PIX_FMT_BGR24;
  102. break;
  103. case 32:
  104. pixfmt = IPU_PIX_FMT_BGR32;
  105. break;
  106. case 16:
  107. pixfmt = IPU_PIX_FMT_RGB565;
  108. break;
  109. }
  110. return pixfmt;
  111. }
  112. /*
  113. * Set fixed framebuffer parameters based on variable settings.
  114. *
  115. * @param info framebuffer information pointer
  116. */
  117. static int mxcfb_set_fix(struct fb_info *info)
  118. {
  119. struct fb_fix_screeninfo *fix = &info->fix;
  120. struct fb_var_screeninfo *var = &info->var;
  121. fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
  122. fix->type = FB_TYPE_PACKED_PIXELS;
  123. fix->accel = FB_ACCEL_NONE;
  124. fix->visual = FB_VISUAL_TRUECOLOR;
  125. fix->xpanstep = 1;
  126. fix->ypanstep = 1;
  127. return 0;
  128. }
  129. static int setup_disp_channel1(struct fb_info *fbi)
  130. {
  131. ipu_channel_params_t params;
  132. struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
  133. memset(&params, 0, sizeof(params));
  134. params.mem_dp_bg_sync.di = mxc_fbi->ipu_di;
  135. debug("%s called\n", __func__);
  136. /*
  137. * Assuming interlaced means yuv output, below setting also
  138. * valid for mem_dc_sync. FG should have the same vmode as BG.
  139. */
  140. if (fbi->var.vmode & FB_VMODE_INTERLACED) {
  141. params.mem_dp_bg_sync.interlaced = 1;
  142. params.mem_dp_bg_sync.out_pixel_fmt =
  143. IPU_PIX_FMT_YUV444;
  144. } else {
  145. if (mxc_fbi->ipu_di_pix_fmt) {
  146. params.mem_dp_bg_sync.out_pixel_fmt =
  147. mxc_fbi->ipu_di_pix_fmt;
  148. } else {
  149. params.mem_dp_bg_sync.out_pixel_fmt =
  150. IPU_PIX_FMT_RGB666;
  151. }
  152. }
  153. params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi);
  154. if (mxc_fbi->alpha_chan_en)
  155. params.mem_dp_bg_sync.alpha_chan_en = 1;
  156. ipu_init_channel(mxc_fbi->ipu_ch, &params);
  157. return 0;
  158. }
  159. static int setup_disp_channel2(struct fb_info *fbi)
  160. {
  161. int retval = 0;
  162. struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
  163. mxc_fbi->cur_ipu_buf = 1;
  164. if (mxc_fbi->alpha_chan_en)
  165. mxc_fbi->cur_ipu_alpha_buf = 1;
  166. fbi->var.xoffset = fbi->var.yoffset = 0;
  167. debug("%s: %x %d %d %d %lx %lx\n",
  168. __func__,
  169. mxc_fbi->ipu_ch,
  170. fbi->var.xres,
  171. fbi->var.yres,
  172. fbi->fix.line_length,
  173. fbi->fix.smem_start,
  174. fbi->fix.smem_start +
  175. (fbi->fix.line_length * fbi->var.yres));
  176. retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER,
  177. bpp_to_pixfmt(fbi),
  178. fbi->var.xres, fbi->var.yres,
  179. fbi->fix.line_length,
  180. fbi->fix.smem_start +
  181. (fbi->fix.line_length * fbi->var.yres),
  182. fbi->fix.smem_start,
  183. 0, 0);
  184. if (retval)
  185. printf("ipu_init_channel_buffer error %d\n", retval);
  186. return retval;
  187. }
  188. /*
  189. * Set framebuffer parameters and change the operating mode.
  190. *
  191. * @param info framebuffer information pointer
  192. */
  193. static int mxcfb_set_par(struct fb_info *fbi)
  194. {
  195. int retval = 0;
  196. u32 mem_len;
  197. ipu_di_signal_cfg_t sig_cfg;
  198. struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par;
  199. uint32_t out_pixel_fmt;
  200. ipu_disable_channel(mxc_fbi->ipu_ch);
  201. ipu_uninit_channel(mxc_fbi->ipu_ch);
  202. mxcfb_set_fix(fbi);
  203. mem_len = fbi->var.yres_virtual * fbi->fix.line_length;
  204. if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) {
  205. if (fbi->fix.smem_start)
  206. mxcfb_unmap_video_memory(fbi);
  207. if (mxcfb_map_video_memory(fbi) < 0)
  208. return -ENOMEM;
  209. }
  210. setup_disp_channel1(fbi);
  211. memset(&sig_cfg, 0, sizeof(sig_cfg));
  212. if (fbi->var.vmode & FB_VMODE_INTERLACED) {
  213. sig_cfg.interlaced = 1;
  214. out_pixel_fmt = IPU_PIX_FMT_YUV444;
  215. } else {
  216. if (mxc_fbi->ipu_di_pix_fmt)
  217. out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt;
  218. else
  219. out_pixel_fmt = IPU_PIX_FMT_RGB666;
  220. }
  221. if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */
  222. sig_cfg.odd_field_first = 1;
  223. if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used)
  224. sig_cfg.ext_clk = 1;
  225. if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT)
  226. sig_cfg.Hsync_pol = 1;
  227. if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT)
  228. sig_cfg.Vsync_pol = 1;
  229. if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL))
  230. sig_cfg.clk_pol = 1;
  231. if (fbi->var.sync & FB_SYNC_DATA_INVERT)
  232. sig_cfg.data_pol = 1;
  233. if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT))
  234. sig_cfg.enable_pol = 1;
  235. if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN)
  236. sig_cfg.clkidle_en = 1;
  237. debug("pixclock = %ul Hz\n",
  238. (u32) (PICOS2KHZ(fbi->var.pixclock) * 1000UL));
  239. if (ipu_init_sync_panel(mxc_fbi->ipu_di,
  240. (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
  241. fbi->var.xres, fbi->var.yres,
  242. out_pixel_fmt,
  243. fbi->var.left_margin,
  244. fbi->var.hsync_len,
  245. fbi->var.right_margin,
  246. fbi->var.upper_margin,
  247. fbi->var.vsync_len,
  248. fbi->var.lower_margin,
  249. 0, sig_cfg) != 0) {
  250. puts("mxcfb: Error initializing panel.\n");
  251. return -EINVAL;
  252. }
  253. retval = setup_disp_channel2(fbi);
  254. if (retval)
  255. return retval;
  256. if (mxc_fbi->blank == FB_BLANK_UNBLANK)
  257. ipu_enable_channel(mxc_fbi->ipu_ch);
  258. return retval;
  259. }
  260. /*
  261. * Check framebuffer variable parameters and adjust to valid values.
  262. *
  263. * @param var framebuffer variable parameters
  264. *
  265. * @param info framebuffer information pointer
  266. */
  267. static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  268. {
  269. u32 vtotal;
  270. u32 htotal;
  271. if (var->xres_virtual < var->xres)
  272. var->xres_virtual = var->xres;
  273. if (var->yres_virtual < var->yres)
  274. var->yres_virtual = var->yres;
  275. if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
  276. (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
  277. var->bits_per_pixel = default_bpp;
  278. switch (var->bits_per_pixel) {
  279. case 8:
  280. var->red.length = 3;
  281. var->red.offset = 5;
  282. var->red.msb_right = 0;
  283. var->green.length = 3;
  284. var->green.offset = 2;
  285. var->green.msb_right = 0;
  286. var->blue.length = 2;
  287. var->blue.offset = 0;
  288. var->blue.msb_right = 0;
  289. var->transp.length = 0;
  290. var->transp.offset = 0;
  291. var->transp.msb_right = 0;
  292. break;
  293. case 16:
  294. var->red.length = 5;
  295. var->red.offset = 11;
  296. var->red.msb_right = 0;
  297. var->green.length = 6;
  298. var->green.offset = 5;
  299. var->green.msb_right = 0;
  300. var->blue.length = 5;
  301. var->blue.offset = 0;
  302. var->blue.msb_right = 0;
  303. var->transp.length = 0;
  304. var->transp.offset = 0;
  305. var->transp.msb_right = 0;
  306. break;
  307. case 24:
  308. var->red.length = 8;
  309. var->red.offset = 16;
  310. var->red.msb_right = 0;
  311. var->green.length = 8;
  312. var->green.offset = 8;
  313. var->green.msb_right = 0;
  314. var->blue.length = 8;
  315. var->blue.offset = 0;
  316. var->blue.msb_right = 0;
  317. var->transp.length = 0;
  318. var->transp.offset = 0;
  319. var->transp.msb_right = 0;
  320. break;
  321. case 32:
  322. var->red.length = 8;
  323. var->red.offset = 16;
  324. var->red.msb_right = 0;
  325. var->green.length = 8;
  326. var->green.offset = 8;
  327. var->green.msb_right = 0;
  328. var->blue.length = 8;
  329. var->blue.offset = 0;
  330. var->blue.msb_right = 0;
  331. var->transp.length = 8;
  332. var->transp.offset = 24;
  333. var->transp.msb_right = 0;
  334. break;
  335. }
  336. if (var->pixclock < 1000) {
  337. htotal = var->xres + var->right_margin + var->hsync_len +
  338. var->left_margin;
  339. vtotal = var->yres + var->lower_margin + var->vsync_len +
  340. var->upper_margin;
  341. var->pixclock = (vtotal * htotal * 6UL) / 100UL;
  342. var->pixclock = KHZ2PICOS(var->pixclock);
  343. printf("pixclock set for 60Hz refresh = %u ps\n",
  344. var->pixclock);
  345. }
  346. var->height = -1;
  347. var->width = -1;
  348. var->grayscale = 0;
  349. return 0;
  350. }
  351. static int mxcfb_map_video_memory(struct fb_info *fbi)
  352. {
  353. if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
  354. fbi->fix.smem_len = fbi->var.yres_virtual *
  355. fbi->fix.line_length;
  356. }
  357. fbi->screen_base = (char *)malloc(fbi->fix.smem_len);
  358. fbi->fix.smem_start = (unsigned long)fbi->screen_base;
  359. if (fbi->screen_base == 0) {
  360. puts("Unable to allocate framebuffer memory\n");
  361. fbi->fix.smem_len = 0;
  362. fbi->fix.smem_start = 0;
  363. return -EBUSY;
  364. }
  365. debug("allocated fb @ paddr=0x%08X, size=%d.\n",
  366. (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
  367. fbi->screen_size = fbi->fix.smem_len;
  368. /* Clear the screen */
  369. memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
  370. return 0;
  371. }
  372. static int mxcfb_unmap_video_memory(struct fb_info *fbi)
  373. {
  374. fbi->screen_base = 0;
  375. fbi->fix.smem_start = 0;
  376. fbi->fix.smem_len = 0;
  377. return 0;
  378. }
  379. /*
  380. * Initializes the framebuffer information pointer. After allocating
  381. * sufficient memory for the framebuffer structure, the fields are
  382. * filled with custom information passed in from the configurable
  383. * structures. This includes information such as bits per pixel,
  384. * color maps, screen width/height and RGBA offsets.
  385. *
  386. * @return Framebuffer structure initialized with our information
  387. */
  388. static struct fb_info *mxcfb_init_fbinfo(void)
  389. {
  390. #define BYTES_PER_LONG 4
  391. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
  392. struct fb_info *fbi;
  393. struct mxcfb_info *mxcfbi;
  394. char *p;
  395. int size = sizeof(struct mxcfb_info) + PADDING +
  396. sizeof(struct fb_info);
  397. debug("%s: %d %d %d %d\n",
  398. __func__,
  399. PADDING,
  400. size,
  401. sizeof(struct mxcfb_info),
  402. sizeof(struct fb_info));
  403. /*
  404. * Allocate sufficient memory for the fb structure
  405. */
  406. p = malloc(size);
  407. if (!p)
  408. return NULL;
  409. memset(p, 0, size);
  410. fbi = (struct fb_info *)p;
  411. fbi->par = p + sizeof(struct fb_info) + PADDING;
  412. mxcfbi = (struct mxcfb_info *)fbi->par;
  413. debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
  414. (unsigned int)fbi, (unsigned int)mxcfbi);
  415. fbi->var.activate = FB_ACTIVATE_NOW;
  416. fbi->flags = FBINFO_FLAG_DEFAULT;
  417. fbi->pseudo_palette = mxcfbi->pseudo_palette;
  418. return fbi;
  419. }
  420. /*
  421. * Probe routine for the framebuffer driver. It is called during the
  422. * driver binding process. The following functions are performed in
  423. * this routine: Framebuffer initialization, Memory allocation and
  424. * mapping, Framebuffer registration, IPU initialization.
  425. *
  426. * @return Appropriate error code to the kernel common code
  427. */
  428. static int mxcfb_probe(u32 interface_pix_fmt, struct fb_videomode *mode)
  429. {
  430. struct fb_info *fbi;
  431. struct mxcfb_info *mxcfbi;
  432. int ret = 0;
  433. /*
  434. * Initialize FB structures
  435. */
  436. fbi = mxcfb_init_fbinfo();
  437. if (!fbi) {
  438. ret = -ENOMEM;
  439. goto err0;
  440. }
  441. mxcfbi = (struct mxcfb_info *)fbi->par;
  442. if (!g_dp_in_use) {
  443. mxcfbi->ipu_ch = MEM_BG_SYNC;
  444. mxcfbi->blank = FB_BLANK_UNBLANK;
  445. } else {
  446. mxcfbi->ipu_ch = MEM_DC_SYNC;
  447. mxcfbi->blank = FB_BLANK_POWERDOWN;
  448. }
  449. mxcfbi->ipu_di = 0;
  450. ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
  451. ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
  452. strcpy(fbi->fix.id, "DISP3 BG");
  453. g_dp_in_use = 1;
  454. mxcfb_info[mxcfbi->ipu_di] = fbi;
  455. /* Need dummy values until real panel is configured */
  456. mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
  457. fb_videomode_to_var(&fbi->var, mode);
  458. fbi->var.bits_per_pixel = 16;
  459. fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
  460. fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
  461. mxcfb_check_var(&fbi->var, fbi);
  462. /* Default Y virtual size is 2x panel size */
  463. fbi->var.yres_virtual = fbi->var.yres * 2;
  464. mxcfb_set_fix(fbi);
  465. /* alocate fb first */
  466. if (mxcfb_map_video_memory(fbi) < 0)
  467. return -ENOMEM;
  468. mxcfb_set_par(fbi);
  469. panel.winSizeX = mode->xres;
  470. panel.winSizeY = mode->yres;
  471. panel.plnSizeX = mode->xres;
  472. panel.plnSizeY = mode->yres;
  473. panel.frameAdrs = (u32)fbi->screen_base;
  474. panel.memSize = fbi->screen_size;
  475. panel.gdfBytesPP = 2;
  476. panel.gdfIndex = GDF_16BIT_565RGB;
  477. ipu_dump_registers();
  478. return 0;
  479. err0:
  480. return ret;
  481. }
  482. void *video_hw_init(void)
  483. {
  484. int ret;
  485. ret = ipu_probe();
  486. if (ret)
  487. puts("Error initializing IPU\n");
  488. ret = mxcfb_probe(IPU_PIX_FMT_RGB666, gmode);
  489. debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
  490. return (void *)&panel;
  491. }
  492. void video_set_lut(unsigned int index, /* color number */
  493. unsigned char r, /* red */
  494. unsigned char g, /* green */
  495. unsigned char b /* blue */
  496. )
  497. {
  498. return;
  499. }
  500. int mx51_fb_init(struct fb_videomode *mode)
  501. {
  502. gmode = mode;
  503. return 0;
  504. }