mxc_ipuv3_fb.c 15 KB

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