mxc_ipuv3_fb.c 15 KB

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