mxc_ipuv3_fb.c 14 KB

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