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