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 static 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 = %lu Hz\n", PICOS2KHZ(fbi->var.pixclock) * 1000UL); 262 263 if (ipu_init_sync_panel(mxc_fbi->ipu_di, 264 (PICOS2KHZ(fbi->var.pixclock)) * 1000UL, 265 fbi->var.xres, fbi->var.yres, 266 out_pixel_fmt, 267 fbi->var.left_margin, 268 fbi->var.hsync_len, 269 fbi->var.right_margin, 270 fbi->var.upper_margin, 271 fbi->var.vsync_len, 272 fbi->var.lower_margin, 273 0, sig_cfg) != 0) { 274 puts("mxcfb: Error initializing panel.\n"); 275 return -EINVAL; 276 } 277 278 retval = setup_disp_channel2(fbi); 279 if (retval) 280 return retval; 281 282 if (mxc_fbi->blank == FB_BLANK_UNBLANK) 283 ipu_enable_channel(mxc_fbi->ipu_ch); 284 285 return retval; 286 } 287 288 /* 289 * Check framebuffer variable parameters and adjust to valid values. 290 * 291 * @param var framebuffer variable parameters 292 * 293 * @param info framebuffer information pointer 294 */ 295 static int mxcfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info) 296 { 297 u32 vtotal; 298 u32 htotal; 299 300 if (var->xres_virtual < var->xres) 301 var->xres_virtual = var->xres; 302 if (var->yres_virtual < var->yres) 303 var->yres_virtual = var->yres; 304 305 if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) && 306 (var->bits_per_pixel != 16) && (var->bits_per_pixel != 8)) 307 var->bits_per_pixel = default_bpp; 308 309 switch (var->bits_per_pixel) { 310 case 8: 311 var->red.length = 3; 312 var->red.offset = 5; 313 var->red.msb_right = 0; 314 315 var->green.length = 3; 316 var->green.offset = 2; 317 var->green.msb_right = 0; 318 319 var->blue.length = 2; 320 var->blue.offset = 0; 321 var->blue.msb_right = 0; 322 323 var->transp.length = 0; 324 var->transp.offset = 0; 325 var->transp.msb_right = 0; 326 break; 327 case 16: 328 var->red.length = 5; 329 var->red.offset = 11; 330 var->red.msb_right = 0; 331 332 var->green.length = 6; 333 var->green.offset = 5; 334 var->green.msb_right = 0; 335 336 var->blue.length = 5; 337 var->blue.offset = 0; 338 var->blue.msb_right = 0; 339 340 var->transp.length = 0; 341 var->transp.offset = 0; 342 var->transp.msb_right = 0; 343 break; 344 case 24: 345 var->red.length = 8; 346 var->red.offset = 16; 347 var->red.msb_right = 0; 348 349 var->green.length = 8; 350 var->green.offset = 8; 351 var->green.msb_right = 0; 352 353 var->blue.length = 8; 354 var->blue.offset = 0; 355 var->blue.msb_right = 0; 356 357 var->transp.length = 0; 358 var->transp.offset = 0; 359 var->transp.msb_right = 0; 360 break; 361 case 32: 362 var->red.length = 8; 363 var->red.offset = 16; 364 var->red.msb_right = 0; 365 366 var->green.length = 8; 367 var->green.offset = 8; 368 var->green.msb_right = 0; 369 370 var->blue.length = 8; 371 var->blue.offset = 0; 372 var->blue.msb_right = 0; 373 374 var->transp.length = 8; 375 var->transp.offset = 24; 376 var->transp.msb_right = 0; 377 break; 378 } 379 380 if (var->pixclock < 1000) { 381 htotal = var->xres + var->right_margin + var->hsync_len + 382 var->left_margin; 383 vtotal = var->yres + var->lower_margin + var->vsync_len + 384 var->upper_margin; 385 var->pixclock = (vtotal * htotal * 6UL) / 100UL; 386 var->pixclock = KHZ2PICOS(var->pixclock); 387 printf("pixclock set for 60Hz refresh = %u ps\n", 388 var->pixclock); 389 } 390 391 var->height = -1; 392 var->width = -1; 393 var->grayscale = 0; 394 395 return 0; 396 } 397 398 static int mxcfb_map_video_memory(struct fb_info *fbi) 399 { 400 if (fbi->fix.smem_len < fbi->var.yres_virtual * fbi->fix.line_length) { 401 fbi->fix.smem_len = fbi->var.yres_virtual * 402 fbi->fix.line_length; 403 } 404 fbi->fix.smem_len = roundup(fbi->fix.smem_len, ARCH_DMA_MINALIGN); 405 fbi->screen_base = (char *)memalign(ARCH_DMA_MINALIGN, 406 fbi->fix.smem_len); 407 fbi->fix.smem_start = (unsigned long)fbi->screen_base; 408 if (fbi->screen_base == 0) { 409 puts("Unable to allocate framebuffer memory\n"); 410 fbi->fix.smem_len = 0; 411 fbi->fix.smem_start = 0; 412 return -EBUSY; 413 } 414 415 debug("allocated fb @ paddr=0x%08X, size=%d.\n", 416 (uint32_t) fbi->fix.smem_start, fbi->fix.smem_len); 417 418 fbi->screen_size = fbi->fix.smem_len; 419 420 gd->fb_base = fbi->fix.smem_start; 421 422 /* Clear the screen */ 423 memset((char *)fbi->screen_base, 0, fbi->fix.smem_len); 424 425 return 0; 426 } 427 428 static int mxcfb_unmap_video_memory(struct fb_info *fbi) 429 { 430 fbi->screen_base = 0; 431 fbi->fix.smem_start = 0; 432 fbi->fix.smem_len = 0; 433 return 0; 434 } 435 436 /* 437 * Initializes the framebuffer information pointer. After allocating 438 * sufficient memory for the framebuffer structure, the fields are 439 * filled with custom information passed in from the configurable 440 * structures. This includes information such as bits per pixel, 441 * color maps, screen width/height and RGBA offsets. 442 * 443 * @return Framebuffer structure initialized with our information 444 */ 445 static struct fb_info *mxcfb_init_fbinfo(void) 446 { 447 #define BYTES_PER_LONG 4 448 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG)) 449 struct fb_info *fbi; 450 struct mxcfb_info *mxcfbi; 451 char *p; 452 int size = sizeof(struct mxcfb_info) + PADDING + 453 sizeof(struct fb_info); 454 455 debug("%s: %d %d %d %d\n", 456 __func__, 457 PADDING, 458 size, 459 sizeof(struct mxcfb_info), 460 sizeof(struct fb_info)); 461 /* 462 * Allocate sufficient memory for the fb structure 463 */ 464 465 p = malloc(size); 466 if (!p) 467 return NULL; 468 469 memset(p, 0, size); 470 471 fbi = (struct fb_info *)p; 472 fbi->par = p + sizeof(struct fb_info) + PADDING; 473 474 mxcfbi = (struct mxcfb_info *)fbi->par; 475 debug("Framebuffer structures at: fbi=0x%x mxcfbi=0x%x\n", 476 (unsigned int)fbi, (unsigned int)mxcfbi); 477 478 fbi->var.activate = FB_ACTIVATE_NOW; 479 480 fbi->flags = FBINFO_FLAG_DEFAULT; 481 fbi->pseudo_palette = mxcfbi->pseudo_palette; 482 483 return fbi; 484 } 485 486 /* 487 * Probe routine for the framebuffer driver. It is called during the 488 * driver binding process. The following functions are performed in 489 * this routine: Framebuffer initialization, Memory allocation and 490 * mapping, Framebuffer registration, IPU initialization. 491 * 492 * @return Appropriate error code to the kernel common code 493 */ 494 static int mxcfb_probe(u32 interface_pix_fmt, uint8_t disp, 495 struct fb_videomode const *mode) 496 { 497 struct fb_info *fbi; 498 struct mxcfb_info *mxcfbi; 499 int ret = 0; 500 501 /* 502 * Initialize FB structures 503 */ 504 fbi = mxcfb_init_fbinfo(); 505 if (!fbi) { 506 ret = -ENOMEM; 507 goto err0; 508 } 509 mxcfbi = (struct mxcfb_info *)fbi->par; 510 511 if (!g_dp_in_use) { 512 mxcfbi->ipu_ch = MEM_BG_SYNC; 513 mxcfbi->blank = FB_BLANK_UNBLANK; 514 } else { 515 mxcfbi->ipu_ch = MEM_DC_SYNC; 516 mxcfbi->blank = FB_BLANK_POWERDOWN; 517 } 518 519 mxcfbi->ipu_di = disp; 520 521 ipu_disp_set_global_alpha(mxcfbi->ipu_ch, 1, 0x80); 522 ipu_disp_set_color_key(mxcfbi->ipu_ch, 0, 0); 523 strcpy(fbi->fix.id, "DISP3 BG"); 524 525 g_dp_in_use = 1; 526 527 mxcfb_info[mxcfbi->ipu_di] = fbi; 528 529 /* Need dummy values until real panel is configured */ 530 531 mxcfbi->ipu_di_pix_fmt = interface_pix_fmt; 532 fb_videomode_to_var(&fbi->var, mode); 533 fbi->var.bits_per_pixel = 16; 534 fbi->fix.line_length = fbi->var.xres * (fbi->var.bits_per_pixel / 8); 535 fbi->fix.smem_len = fbi->var.yres_virtual * fbi->fix.line_length; 536 537 mxcfb_check_var(&fbi->var, fbi); 538 539 /* Default Y virtual size is 2x panel size */ 540 fbi->var.yres_virtual = fbi->var.yres * 2; 541 542 mxcfb_set_fix(fbi); 543 544 /* allocate fb first */ 545 if (mxcfb_map_video_memory(fbi) < 0) 546 return -ENOMEM; 547 548 mxcfb_set_par(fbi); 549 550 panel.winSizeX = mode->xres; 551 panel.winSizeY = mode->yres; 552 panel.plnSizeX = mode->xres; 553 panel.plnSizeY = mode->yres; 554 555 panel.frameAdrs = (u32)fbi->screen_base; 556 panel.memSize = fbi->screen_size; 557 558 panel.gdfBytesPP = 2; 559 panel.gdfIndex = GDF_16BIT_565RGB; 560 561 ipu_dump_registers(); 562 563 return 0; 564 565 err0: 566 return ret; 567 } 568 569 void ipuv3_fb_shutdown(void) 570 { 571 int i; 572 struct ipu_stat *stat = (struct ipu_stat *)IPU_STAT; 573 574 for (i = 0; i < ARRAY_SIZE(mxcfb_info); i++) { 575 struct fb_info *fbi = mxcfb_info[i]; 576 if (fbi) { 577 struct mxcfb_info *mxc_fbi = fbi->par; 578 ipu_disable_channel(mxc_fbi->ipu_ch); 579 ipu_uninit_channel(mxc_fbi->ipu_ch); 580 } 581 } 582 for (i = 0; i < ARRAY_SIZE(stat->int_stat); i++) { 583 __raw_writel(__raw_readl(&stat->int_stat[i]), 584 &stat->int_stat[i]); 585 } 586 } 587 588 void *video_hw_init(void) 589 { 590 int ret; 591 592 ret = ipu_probe(); 593 if (ret) 594 puts("Error initializing IPU\n"); 595 596 ret = mxcfb_probe(gpixfmt, gdisp, gmode); 597 debug("Framebuffer at 0x%x\n", (unsigned int)panel.frameAdrs); 598 599 return (void *)&panel; 600 } 601 602 void video_set_lut(unsigned int index, /* color number */ 603 unsigned char r, /* red */ 604 unsigned char g, /* green */ 605 unsigned char b /* blue */ 606 ) 607 { 608 return; 609 } 610 611 int ipuv3_fb_init(struct fb_videomode const *mode, 612 uint8_t disp, 613 uint32_t pixfmt) 614 { 615 gmode = mode; 616 gdisp = disp; 617 gpixfmt = pixfmt; 618 619 return 0; 620 } 621