mxc_ipuv3_fb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 <asm/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. 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 = %ul Hz\n",
  227. (u32) (PICOS2KHZ(fbi->var.pixclock) * 1000UL));
  228. if (ipu_init_sync_panel(mxc_fbi->ipu_di,
  229. (PICOS2KHZ(fbi->var.pixclock)) * 1000UL,
  230. fbi->var.xres, fbi->var.yres,
  231. out_pixel_fmt,
  232. fbi->var.left_margin,
  233. fbi->var.hsync_len,
  234. fbi->var.right_margin,
  235. fbi->var.upper_margin,
  236. fbi->var.vsync_len,
  237. fbi->var.lower_margin,
  238. 0, sig_cfg) != 0) {
  239. puts("mxcfb: Error initializing panel.\n");
  240. return -EINVAL;
  241. }
  242. retval = setup_disp_channel2(fbi);
  243. if (retval)
  244. return retval;
  245. if (mxc_fbi->blank == FB_BLANK_UNBLANK)
  246. ipu_enable_channel(mxc_fbi->ipu_ch);
  247. return retval;
  248. }
  249. /*
  250. * Check framebuffer variable parameters and adjust to valid values.
  251. *
  252. * @param var framebuffer variable parameters
  253. *
  254. * @param info framebuffer information pointer
  255. */
  256. static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  257. {
  258. u32 vtotal;
  259. u32 htotal;
  260. if (var->xres_virtual < var->xres)
  261. var->xres_virtual = var->xres;
  262. if (var->yres_virtual < var->yres)
  263. var->yres_virtual = var->yres;
  264. if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
  265. (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8))
  266. var->bits_per_pixel = default_bpp;
  267. switch (var->bits_per_pixel) {
  268. case 8:
  269. var->red.length = 3;
  270. var->red.offset = 5;
  271. var->red.msb_right = 0;
  272. var->green.length = 3;
  273. var->green.offset = 2;
  274. var->green.msb_right = 0;
  275. var->blue.length = 2;
  276. var->blue.offset = 0;
  277. var->blue.msb_right = 0;
  278. var->transp.length = 0;
  279. var->transp.offset = 0;
  280. var->transp.msb_right = 0;
  281. break;
  282. case 16:
  283. var->red.length = 5;
  284. var->red.offset = 11;
  285. var->red.msb_right = 0;
  286. var->green.length = 6;
  287. var->green.offset = 5;
  288. var->green.msb_right = 0;
  289. var->blue.length = 5;
  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 24:
  297. var->red.length = 8;
  298. var->red.offset = 16;
  299. var->red.msb_right = 0;
  300. var->green.length = 8;
  301. var->green.offset = 8;
  302. var->green.msb_right = 0;
  303. var->blue.length = 8;
  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 32:
  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 = 8;
  321. var->transp.offset = 24;
  322. var->transp.msb_right = 0;
  323. break;
  324. }
  325. if (var->pixclock < 1000) {
  326. htotal = var->xres + var->right_margin + var->hsync_len +
  327. var->left_margin;
  328. vtotal = var->yres + var->lower_margin + var->vsync_len +
  329. var->upper_margin;
  330. var->pixclock = (vtotal * htotal * 6UL) / 100UL;
  331. var->pixclock = KHZ2PICOS(var->pixclock);
  332. printf("pixclock set for 60Hz refresh = %u ps\n",
  333. var->pixclock);
  334. }
  335. var->height = -1;
  336. var->width = -1;
  337. var->grayscale = 0;
  338. return 0;
  339. }
  340. static int mxcfb_map_video_memory(struct fb_info *fbi)
  341. {
  342. if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) {
  343. fbi->fix.smem_len = fbi->var.yres_virtual *
  344. fbi->fix.line_length;
  345. }
  346. fbi->fix.smem_len = roundup(fbi->fix.smem_len, ARCH_DMA_MINALIGN);
  347. fbi->screen_base = (char *)memalign(ARCH_DMA_MINALIGN,
  348. fbi->fix.smem_len);
  349. fbi->fix.smem_start = (unsigned long)fbi->screen_base;
  350. if (fbi->screen_base == 0) {
  351. puts("Unable to allocate framebuffer memory\n");
  352. fbi->fix.smem_len = 0;
  353. fbi->fix.smem_start = 0;
  354. return -EBUSY;
  355. }
  356. debug("allocated fb @ paddr=0x%08X, size=%d.\n",
  357. (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len);
  358. fbi->screen_size = fbi->fix.smem_len;
  359. gd->fb_base = fbi->fix.smem_start;
  360. /* Clear the screen */
  361. memset((char *)fbi->screen_base, 0, fbi->fix.smem_len);
  362. return 0;
  363. }
  364. static int mxcfb_unmap_video_memory(struct fb_info *fbi)
  365. {
  366. fbi->screen_base = 0;
  367. fbi->fix.smem_start = 0;
  368. fbi->fix.smem_len = 0;
  369. return 0;
  370. }
  371. /*
  372. * Initializes the framebuffer information pointer. After allocating
  373. * sufficient memory for the framebuffer structure, the fields are
  374. * filled with custom information passed in from the configurable
  375. * structures. This includes information such as bits per pixel,
  376. * color maps, screen width/height and RGBA offsets.
  377. *
  378. * @return Framebuffer structure initialized with our information
  379. */
  380. static struct fb_info *mxcfb_init_fbinfo(void)
  381. {
  382. #define BYTES_PER_LONG 4
  383. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
  384. struct fb_info *fbi;
  385. struct mxcfb_info *mxcfbi;
  386. char *p;
  387. int size = sizeof(struct mxcfb_info) + PADDING +
  388. sizeof(struct fb_info);
  389. debug("%s: %d %d %d %d\n",
  390. __func__,
  391. PADDING,
  392. size,
  393. sizeof(struct mxcfb_info),
  394. sizeof(struct fb_info));
  395. /*
  396. * Allocate sufficient memory for the fb structure
  397. */
  398. p = malloc(size);
  399. if (!p)
  400. return NULL;
  401. memset(p, 0, size);
  402. fbi = (struct fb_info *)p;
  403. fbi->par = p + sizeof(struct fb_info) + PADDING;
  404. mxcfbi = (struct mxcfb_info *)fbi->par;
  405. debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n",
  406. (unsigned int)fbi, (unsigned int)mxcfbi);
  407. fbi->var.activate = FB_ACTIVATE_NOW;
  408. fbi->flags = FBINFO_FLAG_DEFAULT;
  409. fbi->pseudo_palette = mxcfbi->pseudo_palette;
  410. return fbi;
  411. }
  412. /*
  413. * Probe routine for the framebuffer driver. It is called during the
  414. * driver binding process. The following functions are performed in
  415. * this routine: Framebuffer initialization, Memory allocation and
  416. * mapping, Framebuffer registration, IPU initialization.
  417. *
  418. * @return Appropriate error code to the kernel common code
  419. */
  420. static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp,
  421. struct fb_videomode const *mode)
  422. {
  423. struct fb_info *fbi;
  424. struct mxcfb_info *mxcfbi;
  425. int ret = 0;
  426. /*
  427. * Initialize FB structures
  428. */
  429. fbi = mxcfb_init_fbinfo();
  430. if (!fbi) {
  431. ret = -ENOMEM;
  432. goto err0;
  433. }
  434. mxcfbi = (struct mxcfb_info *)fbi->par;
  435. if (!g_dp_in_use) {
  436. mxcfbi->ipu_ch = MEM_BG_SYNC;
  437. mxcfbi->blank = FB_BLANK_UNBLANK;
  438. } else {
  439. mxcfbi->ipu_ch = MEM_DC_SYNC;
  440. mxcfbi->blank = FB_BLANK_POWERDOWN;
  441. }
  442. mxcfbi->ipu_di = disp;
  443. ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80);
  444. ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0);
  445. strcpy(fbi->fix.id, "DISP3 BG");
  446. g_dp_in_use = 1;
  447. mxcfb_info[mxcfbi->ipu_di] = fbi;
  448. /* Need dummy values until real panel is configured */
  449. mxcfbi->ipu_di_pix_fmt = interface_pix_fmt;
  450. fb_videomode_to_var(&fbi->var, mode);
  451. fbi->var.bits_per_pixel = 16;
  452. fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8);
  453. fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length;
  454. mxcfb_check_var(&fbi->var, fbi);
  455. /* Default Y virtual size is 2x panel size */
  456. fbi->var.yres_virtual = fbi->var.yres * 2;
  457. mxcfb_set_fix(fbi);
  458. /* alocate fb first */
  459. if (mxcfb_map_video_memory(fbi) < 0)
  460. return -ENOMEM;
  461. mxcfb_set_par(fbi);
  462. panel.winSizeX = mode->xres;
  463. panel.winSizeY = mode->yres;
  464. panel.plnSizeX = mode->xres;
  465. panel.plnSizeY = mode->yres;
  466. panel.frameAdrs = (u32)fbi->screen_base;
  467. panel.memSize = fbi->screen_size;
  468. panel.gdfBytesPP = 2;
  469. panel.gdfIndex = GDF_16BIT_565RGB;
  470. ipu_dump_registers();
  471. return 0;
  472. err0:
  473. return ret;
  474. }
  475. void ipuv3_fb_shutdown(void)
  476. {
  477. int i;
  478. struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT;
  479. for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) {
  480. struct fb_info *fbi = mxcfb_info[i];
  481. if (fbi) {
  482. struct mxcfb_info *mxc_fbi = fbi->par;
  483. ipu_disable_channel(mxc_fbi->ipu_ch);
  484. ipu_uninit_channel(mxc_fbi->ipu_ch);
  485. }
  486. }
  487. for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) {
  488. __raw_writel(__raw_readl(&stat->int_stat[i]),
  489. &stat->int_stat[i]);
  490. }
  491. }
  492. void *video_hw_init(void)
  493. {
  494. int ret;
  495. ret = ipu_probe();
  496. if (ret)
  497. puts("Error initializing IPU\n");
  498. ret = mxcfb_probe(gpixfmt, gdisp, gmode);
  499. debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs);
  500. return (void *)&panel;
  501. }
  502. void video_set_lut(unsigned int index, /* color number */
  503. unsigned char r, /* red */
  504. unsigned char g, /* green */
  505. unsigned char b /* blue */
  506. )
  507. {
  508. return;
  509. }
  510. int ipuv3_fb_init(struct fb_videomode const *mode,
  511. uint8_t disp,
  512. uint32_t pixfmt)
  513. {
  514. gmode = mode;
  515. gdisp = disp;
  516. gpixfmt = pixfmt;
  517. return 0;
  518. }