1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc.
4 * Authors: York Sun <yorksun@freescale.com>
5 * Timur Tabi <timur@freescale.com>
7 * FSL DIU Framebuffer driver
14 #include "videomodes.h"
16 #include <fsl_diu_fb.h>
17 #include <linux/list.h>
20 /* This setting is used for the ifm pdm360ng with PRIMEVIEW PM070WL3 */
21 static struct fb_videomode fsl_diu_mode_800_480 = {
34 .vmode = FB_VMODE_NONINTERLACED
37 /* For the SHARP LQ084S3LG01, used on the P1022DS board */
38 static struct fb_videomode fsl_diu_mode_800_600 = {
50 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
51 .vmode = FB_VMODE_NONINTERLACED
55 * These parameters give default parameters
56 * for video output 1024x768,
57 * FIXME - change timing to proper amounts
58 * hsync 31.5kHz, vsync 60Hz
60 static struct fb_videomode fsl_diu_mode_1024_768 = {
61 .name = "1024x768-60",
72 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
73 .vmode = FB_VMODE_NONINTERLACED
76 static struct fb_videomode fsl_diu_mode_1280_1024 = {
77 .name = "1280x1024-60",
88 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
89 .vmode = FB_VMODE_NONINTERLACED
92 static struct fb_videomode fsl_diu_mode_1280_720 = {
93 .name = "1280x720-60",
104 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
105 .vmode = FB_VMODE_NONINTERLACED
108 static struct fb_videomode fsl_diu_mode_1920_1080 = {
109 .name = "1920x1080-60",
120 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
121 .vmode = FB_VMODE_NONINTERLACED
125 * These are the fields of area descriptor(in DDR memory) for every plane
128 /* Word 0(32-bit) in DDR memory */
129 __le32 pix_fmt; /* hard coding pixel format */
130 /* Word 1(32-bit) in DDR memory */
132 /* Word 2(32-bit) in DDR memory */
133 __le32 src_size_g_alpha;
134 /* Word 3(32-bit) in DDR memory */
136 /* Word 4(32-bit) in DDR memory */
138 /* Word 5(32-bit) in DDR memory */
140 /* Word 6(32-bit) in DDR memory */
145 /* Word 7(32-bit) in DDR memory */
150 /* Word 8(32-bit) in DDR memory */
152 /* Word 9(32-bit) in DDR memory, just for 64-bit aligned */
154 } __attribute__ ((packed));
180 } __attribute__ ((packed));
183 void *vaddr; /* Virtual address */
184 u32 paddr; /* 32-bit physical address */
185 unsigned int offset; /* Alignment offset */
188 static struct fb_info info;
191 * Align to 64-bit(8-byte), 32-byte, etc.
193 static int allocate_buf(struct diu_addr *buf, u32 size, u32 bytes_align)
198 ssize = size + bytes_align;
199 buf->vaddr = malloc(ssize);
203 memset(buf->vaddr, 0, ssize);
204 mask = bytes_align - 1;
205 offset = (u32)buf->vaddr & mask;
207 buf->offset = bytes_align - offset;
208 buf->vaddr += offset;
212 buf->paddr = virt_to_phys(buf->vaddr);
217 * Allocate a framebuffer and an Area Descriptor that points to it. Both
218 * are created in the same memory block. The Area Descriptor is updated to
219 * point to the framebuffer memory. Memory is aligned as needed.
221 static struct diu_ad *allocate_fb(unsigned int xres, unsigned int yres,
222 unsigned int depth, char **fb)
224 unsigned long size = xres * yres * depth;
225 struct diu_addr addr;
227 size_t ad_size = roundup(sizeof(struct diu_ad), 32);
230 * Allocate a memory block that holds the Area Descriptor and the
231 * frame buffer right behind it. To keep the code simple, everything
232 * is aligned on a 32-byte address.
234 if (allocate_buf(&addr, ad_size + size, 32) < 0)
238 ad->addr = cpu_to_le32(addr.paddr + ad_size);
239 ad->aoi_size = cpu_to_le32((yres << 16) | xres);
240 ad->src_size_g_alpha = cpu_to_le32((yres << 12) | xres);
245 *fb = addr.vaddr + ad_size;
250 int fsl_diu_init(u16 xres, u16 yres, u32 pixel_format, int gamma_fix)
252 struct fb_videomode *fsl_diu_mode_db;
254 struct diu *hw = (struct diu *)CONFIG_SYS_DIU_ADDR;
255 u8 *gamma_table_base;
257 struct diu_addr gamma;
258 struct diu_addr cursor;
260 /* Convert the X,Y resolution pair into a single number */
261 #define RESOLUTION(x, y) (((u32)(x) << 16) | (y))
263 switch (RESOLUTION(xres, yres)) {
264 case RESOLUTION(800, 480):
265 fsl_diu_mode_db = &fsl_diu_mode_800_480;
267 case RESOLUTION(800, 600):
268 fsl_diu_mode_db = &fsl_diu_mode_800_600;
270 case RESOLUTION(1024, 768):
271 fsl_diu_mode_db = &fsl_diu_mode_1024_768;
273 case RESOLUTION(1280, 1024):
274 fsl_diu_mode_db = &fsl_diu_mode_1280_1024;
276 case RESOLUTION(1280, 720):
277 fsl_diu_mode_db = &fsl_diu_mode_1280_720;
279 case RESOLUTION(1920, 1080):
280 fsl_diu_mode_db = &fsl_diu_mode_1920_1080;
283 printf("DIU: Unsupported resolution %ux%u\n", xres, yres);
288 info.var.xres = fsl_diu_mode_db->xres;
289 info.var.yres = fsl_diu_mode_db->yres;
290 info.var.bits_per_pixel = 32;
291 info.var.pixclock = fsl_diu_mode_db->pixclock;
292 info.var.left_margin = fsl_diu_mode_db->left_margin;
293 info.var.right_margin = fsl_diu_mode_db->right_margin;
294 info.var.upper_margin = fsl_diu_mode_db->upper_margin;
295 info.var.lower_margin = fsl_diu_mode_db->lower_margin;
296 info.var.hsync_len = fsl_diu_mode_db->hsync_len;
297 info.var.vsync_len = fsl_diu_mode_db->vsync_len;
298 info.var.sync = fsl_diu_mode_db->sync;
299 info.var.vmode = fsl_diu_mode_db->vmode;
300 info.fix.line_length = info.var.xres * info.var.bits_per_pixel / 8;
302 /* Memory allocation for framebuffer */
304 info.var.xres * info.var.yres * (info.var.bits_per_pixel / 8);
305 ad = allocate_fb(info.var.xres, info.var.yres,
306 info.var.bits_per_pixel / 8, &info.screen_base);
308 printf("DIU: Out of memory\n");
312 ad->pix_fmt = pixel_format;
314 /* Disable chroma keying function */
323 /* Initialize the gamma table */
324 if (allocate_buf(&gamma, 256 * 3, 32) < 0) {
325 printf("DIU: Out of memory\n");
328 gamma_table_base = gamma.vaddr;
329 for (i = 0; i <= 2; i++)
330 for (j = 0; j < 256; j++)
331 *gamma_table_base++ = j;
333 if (gamma_fix == 1) { /* fix the gamma */
334 gamma_table_base = gamma.vaddr;
335 for (i = 0; i < 256 * 3; i++) {
336 gamma_table_base[i] = (gamma_table_base[i] << 2)
337 | ((gamma_table_base[i] >> 6) & 0x03);
341 /* Initialize the cursor */
342 if (allocate_buf(&cursor, 32 * 32 * 2, 32) < 0) {
343 printf("DIU: Can't alloc cursor data\n");
347 /* Program DIU registers */
348 out_be32(&hw->diu_mode, 0); /* Temporarily disable the DIU */
350 out_be32(&hw->gamma, gamma.paddr);
351 out_be32(&hw->cursor, cursor.paddr);
352 out_be32(&hw->bgnd, 0x007F7F7F);
353 out_be32(&hw->disp_size, info.var.yres << 16 | info.var.xres);
354 out_be32(&hw->hsyn_para, info.var.left_margin << 22 |
355 info.var.hsync_len << 11 |
356 info.var.right_margin);
358 out_be32(&hw->vsyn_para, info.var.upper_margin << 22 |
359 info.var.vsync_len << 11 |
360 info.var.lower_margin);
362 /* Pixel Clock configuration */
363 diu_set_pixel_clock(info.var.pixclock);
365 /* Set the frame buffers */
366 out_be32(&hw->desc[0], virt_to_phys(ad));
367 out_be32(&hw->desc[1], 0);
368 out_be32(&hw->desc[2], 0);
370 /* Enable the DIU, set display to all three planes */
371 out_be32(&hw->diu_mode, 1);
376 void *video_hw_init(void)
378 static GraphicDevice ctfb;
380 unsigned int depth = 0, freq = 0;
382 if (!video_get_video_mode(&ctfb.winSizeX, &ctfb.winSizeY, &depth, &freq,
386 /* Find the monitor port, which is a required option */
389 if (strncmp(options, "monitor=", 8) != 0)
392 if (platform_diu_init(ctfb.winSizeX, ctfb.winSizeY, options + 8) < 0)
395 /* fill in Graphic device struct */
396 sprintf(ctfb.modeIdent, "%ix%ix%i %ikHz %iHz",
397 ctfb.winSizeX, ctfb.winSizeY, depth, 64, freq);
399 ctfb.frameAdrs = (unsigned int)info.screen_base;
400 ctfb.plnSizeX = ctfb.winSizeX;
401 ctfb.plnSizeY = ctfb.winSizeY;
404 ctfb.gdfIndex = GDF_32BIT_X888RGB;
408 ctfb.memSize = info.screen_size;
410 /* Cursor Start Address */