mxc_ipuv3_fb.c 14 KB

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