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 <linux/errno.h> 16 #include <asm/arch/crm_regs.h> 17 #include <asm/global_data.h> 18 #include <linux/string.h> 19 #include <linux/list.h> 20 #include <linux/fb.h> 21 #include <asm/io.h> 22 #include <malloc.h> 23 #include <video_fb.h> 24 #include "videomodes.h" 25 #include "ipu.h" 26 #include "mxcfb.h" 27 #include "ipu_regs.h" 28 29 DECLARE_GLOBAL_DATA_PTR; 30 31 static int mxcfb_map_video_memory(struct fb_info *fbi); 32 static int mxcfb_unmap_video_memory(struct fb_info *fbi); 33 34 /* graphics setup */ 35 static GraphicDevice panel; 36 static struct fb_videomode const *gmode; 37 static uint8_t gdisp; 38 static uint32_t gpixfmt; 39 40 static void fb_videomode_to_var(struct fb_var_screeninfo *var, 41 const struct fb_videomode *mode) 42 { 43 var->xres = mode->xres; 44 var->yres = mode->yres; 45 var->xres_virtual = mode->xres; 46 var->yres_virtual = mode->yres; 47 var->xoffset = 0; 48 var->yoffset = 0; 49 var->pixclock = mode->pixclock; 50 var->left_margin = mode->left_margin; 51 var->right_margin = mode->right_margin; 52 var->upper_margin = mode->upper_margin; 53 var->lower_margin = mode->lower_margin; 54 var->hsync_len = mode->hsync_len; 55 var->vsync_len = mode->vsync_len; 56 var->sync = mode->sync; 57 var->vmode = mode->vmode & FB_VMODE_MASK; 58 } 59 60 /* 61 * Structure containing the MXC specific framebuffer information. 62 */ 63 struct mxcfb_info { 64 int blank; 65 ipu_channel_t ipu_ch; 66 int ipu_di; 67 u32 ipu_di_pix_fmt; 68 unsigned char overlay; 69 unsigned char alpha_chan_en; 70 dma_addr_t alpha_phy_addr0; 71 dma_addr_t alpha_phy_addr1; 72 void *alpha_virt_addr0; 73 void *alpha_virt_addr1; 74 uint32_t alpha_mem_len; 75 uint32_t cur_ipu_buf; 76 uint32_t cur_ipu_alpha_buf; 77 78 u32 pseudo_palette[16]; 79 }; 80 81 enum { 82 BOTH_ON, 83 SRC_ON, 84 TGT_ON, 85 BOTH_OFF 86 }; 87 88 static unsigned long default_bpp = 16; 89 static unsigned char g_dp_in_use; 90 static struct fb_info *mxcfb_info[3]; 91 static int ext_clk_used; 92 93 static uint32_t bpp_to_pixfmt(struct fb_info *fbi) 94 { 95 uint32_t pixfmt = 0; 96 97 debug("bpp_to_pixfmt: %d\n", fbi->var.bits_per_pixel); 98 99 if (fbi->var.nonstd) 100 return fbi->var.nonstd; 101 102 switch (fbi->var.bits_per_pixel) { 103 case 24: 104 pixfmt = IPU_PIX_FMT_BGR24; 105 break; 106 case 32: 107 pixfmt = IPU_PIX_FMT_BGR32; 108 break; 109 case 16: 110 pixfmt = IPU_PIX_FMT_RGB565; 111 break; 112 } 113 return pixfmt; 114 } 115 116 /* 117 * Set fixed framebuffer parameters based on variable settings. 118 * 119 * @param info framebuffer information pointer 120 */ 121 static int mxcfb_set_fix(struct fb_info *info) 122 { 123 struct fb_fix_screeninfo *fix = &info->fix; 124 struct fb_var_screeninfo *var = &info->var; 125 126 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8; 127 128 fix->type = FB_TYPE_PACKED_PIXELS; 129 fix->accel = FB_ACCEL_NONE; 130 fix->visual = FB_VISUAL_TRUECOLOR; 131 fix->xpanstep = 1; 132 fix->ypanstep = 1; 133 134 return 0; 135 } 136 137 static int setup_disp_channel1(struct fb_info *fbi) 138 { 139 ipu_channel_params_t params; 140 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par; 141 142 memset(¶ms, 0, sizeof(params)); 143 params.mem_dp_bg_sync.di = mxc_fbi->ipu_di; 144 145 debug("%s called\n", __func__); 146 /* 147 * Assuming interlaced means yuv output, below setting also 148 * valid for mem_dc_sync. FG should have the same vmode as BG. 149 */ 150 if (fbi->var.vmode & FB_VMODE_INTERLACED) { 151 params.mem_dp_bg_sync.interlaced = 1; 152 params.mem_dp_bg_sync.out_pixel_fmt = 153 IPU_PIX_FMT_YUV444; 154 } else { 155 if (mxc_fbi->ipu_di_pix_fmt) { 156 params.mem_dp_bg_sync.out_pixel_fmt = 157 mxc_fbi->ipu_di_pix_fmt; 158 } else { 159 params.mem_dp_bg_sync.out_pixel_fmt = 160 IPU_PIX_FMT_RGB666; 161 } 162 } 163 params.mem_dp_bg_sync.in_pixel_fmt = bpp_to_pixfmt(fbi); 164 if (mxc_fbi->alpha_chan_en) 165 params.mem_dp_bg_sync.alpha_chan_en = 1; 166 167 ipu_init_channel(mxc_fbi->ipu_ch, ¶ms); 168 169 return 0; 170 } 171 172 static int setup_disp_channel2(struct fb_info *fbi) 173 { 174 int retval = 0; 175 struct mxcfb_info *mxc_fbi = (struct mxcfb_info *)fbi->par; 176 177 mxc_fbi->cur_ipu_buf = 1; 178 if (mxc_fbi->alpha_chan_en) 179 mxc_fbi->cur_ipu_alpha_buf = 1; 180 181 fbi->var.xoffset = fbi->var.yoffset = 0; 182 183 debug("%s: %x %d %d %d %lx %lx\n", 184 __func__, 185 mxc_fbi->ipu_ch, 186 fbi->var.xres, 187 fbi->var.yres, 188 fbi->fix.line_length, 189 fbi->fix.smem_start, 190 fbi->fix.smem_start + 191 (fbi->fix.line_length * fbi->var.yres)); 192 193 retval = ipu_init_channel_buffer(mxc_fbi->ipu_ch, IPU_INPUT_BUFFER, 194 bpp_to_pixfmt(fbi), 195 fbi->var.xres, fbi->var.yres, 196 fbi->fix.line_length, 197 fbi->fix.smem_start + 198 (fbi->fix.line_length * fbi->var.yres), 199 fbi->fix.smem_start, 200 0, 0); 201 if (retval) 202 printf("ipu_init_channel_buffer error %d\n", retval); 203 204 return retval; 205 } 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 220 ipu_disable_channel(mxc_fbi->ipu_ch); 221 ipu_uninit_channel(mxc_fbi->ipu_ch); 222 mxcfb_set_fix(fbi); 223 224 mem_len = fbi->var.yres_virtual * fbi->fix.line_length; 225 if (!fbi->fix.smem_start || (mem_len > fbi->fix.smem_len)) { 226 if (fbi->fix.smem_start) 227 mxcfb_unmap_video_memory(fbi); 228 229 if (mxcfb_map_video_memory(fbi) < 0) 230 return -ENOMEM; 231 } 232 233 setup_disp_channel1(fbi); 234 235 memset(&sig_cfg, 0, sizeof(sig_cfg)); 236 if (fbi->var.vmode & FB_VMODE_INTERLACED) { 237 sig_cfg.interlaced = 1; 238 out_pixel_fmt = IPU_PIX_FMT_YUV444; 239 } else { 240 if (mxc_fbi->ipu_di_pix_fmt) 241 out_pixel_fmt = mxc_fbi->ipu_di_pix_fmt; 242 else 243 out_pixel_fmt = IPU_PIX_FMT_RGB666; 244 } 245 if (fbi->var.vmode & FB_VMODE_ODD_FLD_FIRST) /* PAL */ 246 sig_cfg.odd_field_first = 1; 247 if ((fbi->var.sync & FB_SYNC_EXT) || ext_clk_used) 248 sig_cfg.ext_clk = 1; 249 if (fbi->var.sync & FB_SYNC_HOR_HIGH_ACT) 250 sig_cfg.Hsync_pol = 1; 251 if (fbi->var.sync & FB_SYNC_VERT_HIGH_ACT) 252 sig_cfg.Vsync_pol = 1; 253 if (!(fbi->var.sync & FB_SYNC_CLK_LAT_FALL)) 254 sig_cfg.clk_pol = 1; 255 if (fbi->var.sync & FB_SYNC_DATA_INVERT) 256 sig_cfg.data_pol = 1; 257 if (!(fbi->var.sync & FB_SYNC_OE_LOW_ACT)) 258 sig_cfg.enable_pol = 1; 259 if (fbi->var.sync & FB_SYNC_CLK_IDLE_EN) 260 sig_cfg.clkidle_en = 1; 261 262 debug("pixclock = %lu Hz\n", 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 /* allocate 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 struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR; 573 struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT; 574 u32 reg; 575 int i; 576 577 /* 578 * Check if IPU clock was enabled before. Won't access 579 * IPU registers if clock is not enabled. 580 */ 581 reg = readl(&mxc_ccm->CCGR3); 582 if ((reg & MXC_CCM_CCGR3_IPU1_IPU_MASK) == 0) 583 return; 584 585 for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) { 586 struct fb_info *fbi = mxcfb_info[i]; 587 if (fbi) { 588 struct mxcfb_info *mxc_fbi = fbi->par; 589 ipu_disable_channel(mxc_fbi->ipu_ch); 590 ipu_uninit_channel(mxc_fbi->ipu_ch); 591 } 592 } 593 for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) { 594 __raw_writel(__raw_readl(&stat->int_stat[i]), 595 &stat->int_stat[i]); 596 } 597 } 598 599 void *video_hw_init(void) 600 { 601 int ret; 602 603 ret = ipu_probe(); 604 if (ret) 605 puts("Error initializing IPU\n"); 606 607 ret = mxcfb_probe(gpixfmt, gdisp, gmode); 608 debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs); 609 610 return (void *)&panel; 611 } 612 613 int ipuv3_fb_init(struct fb_videomode const *mode, 614 uint8_t disp, 615 uint32_t pixfmt) 616 { 617 gmode = mode; 618 gdisp = disp; 619 gpixfmt = pixfmt; 620 621 return 0; 622 } 623