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